ghc-heap-view-0.5.10: Extract the heap representation of Haskell values and thunks

GHC.HeapView

Description

With this module, you can investigate the heap representation of Haskell values, i.e. to investigate sharing and lazy evaluation.

Synopsis

# Heap data types

data GenClosure b Source #

This is the main data type of this module, representing a Haskell value on the heap. This reflects http://hackage.haskell.org/trac/ghc/browser/includes/rts/storage/Closures.h

The data type is parametrized by the type to store references in, which is usually a Box with appropriate type synonym Closure.

Constructors

 ConsClosure Fieldsinfo :: StgInfoTable ptrArgs :: [b] dataArgs :: [Word] pkg :: String modl :: String name :: String ThunkClosure Fieldsinfo :: StgInfoTable ptrArgs :: [b] dataArgs :: [Word] SelectorClosure Fieldsinfo :: StgInfoTable selectee :: b IndClosure Fieldsinfo :: StgInfoTable indirectee :: b BlackholeClosure Fieldsinfo :: StgInfoTable indirectee :: b APClosure Fieldsinfo :: StgInfoTable arity :: HalfWord n_args :: HalfWord fun :: b payload :: [b] PAPClosure Fieldsinfo :: StgInfoTable arity :: HalfWord n_args :: HalfWord fun :: b payload :: [b] APStackClosure Fieldsinfo :: StgInfoTable fun :: b payload :: [b] BCOClosure Fieldsinfo :: StgInfoTable instrs :: b literals :: b bcoptrs :: b arity :: HalfWord size :: HalfWord bitmap :: Word ArrWordsClosure Fieldsinfo :: StgInfoTable bytes :: Word arrWords :: [Word] MutArrClosure Fieldsinfo :: StgInfoTable mccPtrs :: Word mccSize :: Word mccPayload :: [b] MutVarClosure Fieldsinfo :: StgInfoTable var :: b MVarClosure Fieldsinfo :: StgInfoTable queueHead :: b queueTail :: b value :: b FunClosure Fieldsinfo :: StgInfoTable ptrArgs :: [b] dataArgs :: [Word] BlockingQueueClosure Fieldsinfo :: StgInfoTable link :: b blackHole :: b owner :: b queue :: b OtherClosure Fieldsinfo :: StgInfoTable hvalues :: [b] rawWords :: [Word] UnsupportedClosure Fieldsinfo :: StgInfoTable

Instances

 Source # Methodsfmap :: (a -> b) -> GenClosure a -> GenClosure b #(<$) :: a -> GenClosure b -> GenClosure a # Source # Methodsfold :: Monoid m => GenClosure m -> m #foldMap :: Monoid m => (a -> m) -> GenClosure a -> m #foldr :: (a -> b -> b) -> b -> GenClosure a -> b #foldr' :: (a -> b -> b) -> b -> GenClosure a -> b #foldl :: (b -> a -> b) -> b -> GenClosure a -> b #foldl' :: (b -> a -> b) -> b -> GenClosure a -> b #foldr1 :: (a -> a -> a) -> GenClosure a -> a #foldl1 :: (a -> a -> a) -> GenClosure a -> a #toList :: GenClosure a -> [a] #null :: GenClosure a -> Bool #length :: GenClosure a -> Int #elem :: Eq a => a -> GenClosure a -> Bool #maximum :: Ord a => GenClosure a -> a #minimum :: Ord a => GenClosure a -> a #sum :: Num a => GenClosure a -> a #product :: Num a => GenClosure a -> a # Source # Methodstraverse :: Applicative f => (a -> f b) -> GenClosure a -> f (GenClosure b) #sequenceA :: Applicative f => GenClosure (f a) -> f (GenClosure a) #mapM :: Monad m => (a -> m b) -> GenClosure a -> m (GenClosure b) #sequence :: Monad m => GenClosure (m a) -> m (GenClosure a) # Show b => Show (GenClosure b) Source # MethodsshowsPrec :: Int -> GenClosure b -> ShowS #show :: GenClosure b -> String #showList :: [GenClosure b] -> ShowS # allPtrs :: GenClosure b -> [b] Source # For generic code, this function returns all referenced closures. A closure type enumeration, in order matching the actual value on the heap. Needs to be synchronized with http://hackage.haskell.org/trac/ghc/browser/includes/rts/storage/ClosureTypes.h Instances  Source # Methods Source # Methods Source # Methods Source # Methods Source # MethodsshowList :: [ClosureType] -> ShowS # This is a somewhat faithful representation of an info table. See http://hackage.haskell.org/trac/ghc/browser/includes/rts/storage/InfoTables.h for more details on this data structure. Note that the Storable instance provided here does _not_ support writing. Constructors  StgInfoTable Fieldsptrs :: HalfWord nptrs :: HalfWord tipe :: ClosureType srtlen :: HalfWord Instances  Source # MethodsshowList :: [StgInfoTable] -> ShowS # Source # MethodspeekByteOff :: Ptr b -> Int -> IO StgInfoTable #pokeByteOff :: Ptr b -> Int -> StgInfoTable -> IO () # # Reading from the heap This function returns parsed heap representation of the argument _at this moment_, even if it is unevaluated or an indirection or other exotic stuff. Beware when passing something to this function, the same caveats as for asBox apply. Like getClosureData, but taking a Box, so it is easier to work with. getClosureRaw :: a -> IO (Ptr StgInfoTable, [Word], [Box]) Source # This returns the raw representation of the given argument. The second component of the triple are the words on the heap, and the third component are those words that are actually pointers. Once back in Haskell word, the Word may be outdated after a garbage collector run, but the corresponding Box will still point to the correct value. # Pretty printing ppClosure :: (Int -> b -> String) -> Int -> GenClosure b -> String Source # A pretty-printer that tries to generate valid Haskell for evalutated data. It assumes that for the included boxes, you already replaced them by Strings using map or, if you need to do IO, mapM. The parameter gives the precedendence, to avoid avoidable parenthesises. # Heap maps For more global views of the heap, you can use heap maps. These come in variations, either a trees or as graphs, depending on whether you want to detect cycles and sharing or not. The entries of a HeapGraph can be annotated with arbitrary values. Most operations expect this to be in the Monoid class: They use mempty to annotate closures added because the passed values reference them, and they use mappend to combine the annotations when two values conincide, e.g. during updateHeapGraph. data HeapTree Source # Heap maps as tree, i.e. no sharing, no cycles. Constructors  HeapTree Box (GenClosure HeapTree) EndOfHeapTree Constructing an HeapTree from a boxed value. It takes a depth parameter that prevents it from running ad infinitum for cyclic or infinite structures. Pretty-Printing a heap Tree Example output for [Just 4, Nothing, *something*], where *something* is an unevaluated expression depending on the command line argument. [Just (I# 4),Nothing,Just (_thunk ["arg1","arg2"])] For heap graphs, i.e. data structures that also represent sharing and cyclic structures, these are the entries. If the referenced value is Nothing, then we do not have that value in the map, most likely due to exceeding the recursion bound passed to buildHeapGraph. Besides a pointer to the stored value and the closure representation we also keep track of whether the value was still alive at the last update of the heap graph. In addition we have a slot for arbitrary data, for the user's convenience. Constructors  HeapGraphEntry FieldshgeBox :: Box hgeClosure :: GenClosure (Maybe HeapGraphIndex) hgeLive :: Bool hgeData :: a Instances  Source # Methodsfmap :: (a -> b) -> HeapGraphEntry a -> HeapGraphEntry b #(<$) :: a -> HeapGraphEntry b -> HeapGraphEntry a # Show a => Show (HeapGraphEntry a) Source # MethodsshowsPrec :: Int -> HeapGraphEntry a -> ShowS #showList :: [HeapGraphEntry a] -> ShowS #

newtype HeapGraph a Source #

The whole graph. The suggested interface is to only use lookupHeapGraph, as the internal representation may change. Nevertheless, we export it here: Sometimes the user knows better what he needs than we do.

Constructors

 HeapGraph (IntMap (HeapGraphEntry a))

Instances

 Show a => Show (HeapGraph a) Source # MethodsshowsPrec :: Int -> HeapGraph a -> ShowS #show :: HeapGraph a -> String #showList :: [HeapGraph a] -> ShowS #

Arguments

 :: Monoid a => Int Search limit -> a Data value for the root -> Box The value to start with -> IO (HeapGraph a)

Creates a HeapGraph for the value in the box, but not recursing further than the given limit. The initial value has index heapGraphRoot.

Arguments

 :: Monoid a => Int Search limit -> [(a, Box)] Starting values with associated data entry -> IO (HeapGraph a, [(a, HeapGraphIndex)])

Creates a HeapGraph for the values in multiple boxes, but not recursing further than the given limit.

Returns the HeapGraph and the indices of initial values. The arbitrary type a can be used to make the connection between the input and the resulting list of indices, and to store additional data.

Arguments

 :: Monoid a => Int Search limit -> a Data to be stored with the added value -> Box Value to add to the graph -> HeapGraph a Graph to extend -> IO (HeapGraphIndex, HeapGraph a)

Adds an entry to an existing HeapGraph.

Returns the updated HeapGraph and the index of the added value.

annotateHeapGraph :: Monoid a => a -> HeapGraphIndex -> HeapGraph a -> HeapGraph a Source #

Adds the given annotation to the entry at the given index, using the mappend operation of its Monoid instance.

This function updates a heap graph to reflect the current state of closures on the heap, conforming to the following specification.

• Every entry whose value has been garbage collected by now is marked as dead by setting hgeLive to False
• Every entry whose value is still live gets the hgeClosure field updated and newly referenced closures are, up to the given depth, added to the graph.
• A map mapping previous indicies to the corresponding new indicies is returned as well.
• The closure at heapGraphRoot stays at heapGraphRoot

Pretty-prints a HeapGraph. The resulting string contains newlines. Example for let s = "Ki" in (s, s, cycle "Ho"):

let x1 = "Ki"
x6 = C# 'H' : C# 'o' : x6
in (x1,x1,x6)

# Boxes

data Box Source #

An arbitrarily Haskell value in a safe Box. The point is that even unevaluated thunks can safely be moved around inside the Box, and when required, e.g. in getBoxedClosureData, the function knows how far it has to evalue the argument.

Constructors

 Box Any

Instances

 Source # MethodsshowsPrec :: Int -> Box -> ShowS #show :: Box -> String #showList :: [Box] -> ShowS #

asBox :: a -> Box Source #

This takes an arbitrary value and puts it into a box. Note that calls like

asBox (head list)

will put the thunk "head list" into the box, not the element at the head of the list. For that, use careful case expressions:

case list of x:_ -> asBox x

Boxes can be compared, but this is not pure, as different heap objects can, after garbage collection, become the same object.

# Disassembler

disassembleBCO :: (a -> Maybe (GenClosure b)) -> GenClosure a -> Maybe [BCI b] Source #

This function integrates the disassembler in GHC.Disassembler. The first argument should a function that dereferences the pointer in the closure to a closure.

If any of these return Nothing, then disassembleBCO returns Nothing