|
1 | 1 | {-# LANGUAGE RecordWildCards, LambdaCase, OverloadedStrings #-} |
2 | 2 | module Stg.Interpreter.Debugger.TraverseState |
3 | 3 | ( exportReachableGraph |
| 4 | + , exportHeapGraph |
| 5 | + , getHeapObjectSummary |
| 6 | + , getHeapObjectCategory |
4 | 7 | ) where |
5 | 8 |
|
6 | 9 | import Control.Monad.State |
@@ -121,3 +124,51 @@ getHeapObjectCategory = \case |
121 | 124 | BlackHole{} -> "BlackHole" |
122 | 125 | ApStack{} -> "ApStack" |
123 | 126 | RaiseException{} -> "Exception" |
| 127 | + |
| 128 | +exportHeapGraph :: FilePath -> FilePath -> Heap -> IO () |
| 129 | +exportHeapGraph nodesFname edgesFname heap = do |
| 130 | + withFile edgesFname WriteMode $ \hEdge -> do |
| 131 | + withFile nodesFname WriteMode $ \hNode -> do |
| 132 | + BS8.hPutStrLn hNode $ BS8.intercalate "\t" |
| 133 | + [ "Id" |
| 134 | + , "Label" |
| 135 | + , "partition2" |
| 136 | + ] |
| 137 | + BS8.hPutStrLn hEdge $ BS8.intercalate "\t" |
| 138 | + [ "Source" |
| 139 | + , "Target" |
| 140 | + , "partition2" |
| 141 | + ] |
| 142 | + flip evalStateT Set.empty $ do |
| 143 | + forM_ (IntMap.toList heap) $ \(addr, obj) -> do |
| 144 | + let source = encodeRef addr NS_HeapPtr |
| 145 | + |
| 146 | + genNode node = do |
| 147 | + firstTimeVisit <- mark node |
| 148 | + when firstTimeVisit $ do |
| 149 | + let (ns, idx) = decodeRef node |
| 150 | + (nodeLabel, nodeCategory) = case ns of |
| 151 | + NS_HeapPtr |
| 152 | + | Just ho <- IntMap.lookup idx heap |
| 153 | + -> (getHeapObjectSummary ho, getHeapObjectCategory ho) |
| 154 | + _ -> (drop 3 $ show ns, drop 3 $ show ns) |
| 155 | + -- HINT: write line to node .tsv |
| 156 | + liftIO $ do |
| 157 | + BS8.hPut hNode $ unGCSymbol node |
| 158 | + BS8.hPut hNode "\t" |
| 159 | + hPutStr hNode nodeLabel |
| 160 | + BS8.hPut hNode "\t" |
| 161 | + hPutStr hNode nodeCategory |
| 162 | + BS8.hPut hNode "\n" |
| 163 | + |
| 164 | + genNode source |
| 165 | + flip visitGCRef obj $ \target -> do |
| 166 | + genNode target |
| 167 | + -- HINT: write line to edge .tsv |
| 168 | + liftIO $ do |
| 169 | + BS8.hPut hEdge $ unGCSymbol source |
| 170 | + BS8.hPut hEdge "\t" |
| 171 | + BS8.hPut hEdge $ unGCSymbol target |
| 172 | + BS8.hPut hEdge "\t" |
| 173 | + BS8.hPut hEdge "green" |
| 174 | + BS8.hPut hEdge "\n" |
0 commit comments