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

Copyright(c) 2012 Joachim Breitner
MaintainerJoachim Breitner <>
Safe HaskellNone




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


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

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


Functor GenClosure Source # 


fmap :: (a -> b) -> GenClosure a -> GenClosure b #

(<$) :: a -> GenClosure b -> GenClosure a #

Foldable GenClosure Source # 


fold :: 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 #

Traversable GenClosure Source # 


traverse :: 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 # 

allPtrs :: GenClosure b -> [b] Source #

For generic code, this function returns all referenced closures.

data ClosureType Source #

A closure type enumeration, in order matching the actual value on the heap. Needs to be synchronized with

data StgInfoTable Source #

This is a somewhat faithful representation of an info table. See for more details on this data structure. Note that the Storable instance provided here does _not_ support writing.



Reading from the heap

getClosureData :: a -> IO Closure Source #

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.

getBoxedClosureData :: Box -> IO Closure Source #

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.

buildHeapTree :: Int -> Box -> IO HeapTree Source #

Constructing an HeapTree from a boxed value. It takes a depth parameter that prevents it from running ad infinitum for cyclic or infinite structures.

ppHeapTree :: HeapTree -> String Source #

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"])]

data HeapGraphEntry a Source #

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.

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.


HeapGraph (IntMap (HeapGraphEntry a)) 


buildHeapGraph Source #


:: 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.

multiBuildHeapGraph Source #


:: 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.

addHeapGraph Source #


:: 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.

updateHeapGraph :: Monoid a => Int -> HeapGraph a -> IO (HeapGraph a, HeapGraphIndex -> HeapGraphIndex) Source #

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

ppHeapGraph :: HeapGraph a -> String Source #

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)


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.


Box Any 


Show Box Source # 


showsPrec :: 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

areBoxesEqual :: Box -> Box -> IO Bool Source #

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


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