Haskell Inspector

The missing Haskell devtools

Live inspection of Haskell programs.

Attach to a running program and see what it is doing right now: what every thread runs, what is on the heap and why it is still there.

#Look inside a running program

⚠️ Experimental: Haskell Inspector is under active development. Many features already work with existing GHC releases, while others depend on runtime support we are working to bring to future GHC versions. Expect rough edges and interface changes; feedback and bug reports are very welcome.

Haskell's type system is famously powerful: if it compiles, it works! But what if it works poorly? Perhaps after some upgrades your program keeps eating more memory, and latency spikes are ruining its responsiveness. How do you fix it? What is the root cause? Did GHC miss a specialization? Did you forget a strictness bang? Maybe one of the dependencies is at fault?

These are the kinds of questions that Haskell Inspector aims to answer. Haskell Inspector attaches to the running program and shows you what is happening right now: what each thread is doing, what is on the heap, and what is keeping it alive.

You can use Haskell Inspector through its web interface, which integrates seamlessly into Visual Studio Code and works just as well standalone in any browser. For scripts and automated testing there is an extensive command-line interface.


#Features

Haskell Inspector is built around live monitoring. You connect to your program and see what the threads are doing, how much stack space they use, how much time is spent in the garbage collector. All without disturbing or slowing down your program. If you notice something amiss, you can dig deeper for more detail: Record a profile to see where time is spent, or pause the program to see exactly what data is in memory and on the stack of each thread.

#Live Inspection

After connecting you see what every thread is doing and what it is waiting on. Runtime statistics update live alongside: heap size, allocation rate, GC pressure, and per-capability activity.

The latency monitor watches for sections where a thread cannot be interrupted, such as a long GC or an unsafe foreign call that never yields, and flags them as they occur. Exceptions are collected and shown with a stack trace.

You can also inspect the eventlog directly, in a streaming view that handles millions of rows, with filtering and grouping by event kind.

⚙️ How it works: Most of the timeline and heap data comes directly from the eventlog of your Haskell program. Stack depths and exceptions are tracked by intercepting calls to RTS functions.

#Profile While It Runs

Where is your program spending its time and memory? Start recording a profile and find out as it happens: the report builds up while the program runs.

All of GHC's built-in profiling modes are supported: ticky counters, cost-centre profiling, and heap census grouped by cost centre or info table. The data is the same the static profilers produce, but you read it interactively against the live process instead of waiting for a .prof file after the program exits.

The cost-centre flame graph builds up live against the running program.

And if the overhead of the profiling runtime is too much for your program, there are lightweight sampling modes that work on any binary: an STG-stack sampler that records Haskell call stacks, and a runtime-stack sampler that captures both the C and Haskell call stacks. Neither slows your program down significantly.

⚙️ How it works: Haskell Inspector uses the Haskell RTS to capture cost centre stacks and ticky counters. Runtime stacks are sampled using operating system specific methods, such as an eBPF program or signal handlers on Linux, Mach thread APIs on macOS, SuspendThread on Windows. Names come from IPE (info-table provenance) information, DWARF debugging information or dynamic library symbols.

You can export your recorded profiles to various common formats: speedscope, pprof, Chrome trace, and flamegraph.pl-style folded stacks (--format folded).

#HEQL Your Heap

Pause your program and you get access to detailed heap inspection tools. The heap exploration query language HEQL (pronounced "heckle") lets you quickly answer the most important questions about the state of your running program. "Which data is kept alive by my thread's stack?", "Does my data structure contain long chains of thunks?", "Does the heap contain many duplicated data structures?". It's all just one HEQL away!

HEQL queries have a syntax similar to list comprehensions. Each query returns results in the form of cards or tables. This is a thunk-chain detector: it follows chains of thunks and reports each long one as a finding card, with the worst offenders attached as evidence.

view thunk-chain "Long thunk chains" =
  [ card (first p)
      { title = "Long THUNK chain ({count (nodes p)} deep)"
      , count = count (nodes p), evidence = take 3 (nodes p) }
  | p <- chains /dom[thunkLike]/ thunkLike
  , count (nodes p) >= 4, (first p).retained >= 4KiB ]
Findings reported by the built-in leak diagnostics, each card carrying evidence to drill into.
Findings reported by the built-in leak diagnostics, each card carrying evidence to drill into.

Haskell Inspector comes with a catalog of HEQL queries to quickly analyze your program and to get you started writing your own; the standard library and prelude references document everything available to your queries. The web interface offers live type checking while you work on your query. Each query can return values that feed into new queries, letting you build interactive views of the heap. You can drill down to get the details you need, even down to a hex view of individual bytes! You can also run HEQL queries using the CLI to integrate into scripts or your testing workflow.

⚙️ How it works: HEQL queries get compiled to a query plan with stream fusion on one or more strands of execution. The strands source their information from pre-built indices of the heap or from the results of other strands. Large strands are split into chunks for parallel processing. The query evaluation engine FLAX (Fused Live Analysis eXecutor)1 runs the plan inside the inspector, or directly inside the inspected process.

Prefer writing your heap analysis in Haskell instead? You can also export a heap snapshot that is compatible with the ghc-debug library.


#Installation

#1. Install haskell-inspector

Install the inspector from Hackage:

cabal install inspector

This puts the haskell-inspector binary on your PATH (cabal installs to ~/.cabal/bin by default), along with hins, the same program under a shorter name for interactive use. The web interface and the CLI are both included.

Optional: the VS Code extension embeds the web interface in the editor and lists the inspectable processes of the current workspace. Install it from the Visual Studio Marketplace or Open VSX, or build it yourself from haskell-inspector/extras/vscode-extension in the repository; its README covers building and installing. It expects the haskell-inspector binary on PATH.

#2. Add the connector to your program

Making your program inspectable is easy, just add haskell-inspector-connector package to your component's build-depends:

build-depends:
    ...
    , haskell-inspector-connector

or use the sublibrary of the haskell-inspector package:

build-depends:
    ...
    , haskell-inspector:connector

When your program is linked against the connector library, it will create a descriptor file on startup, in a location where the inspector can find it, and listen on a socket for an inspector to attach.

The inspector works with any build, but it can tell you much more when the program carries debugging metadata: IPE information maps heap objects back to source locations (with a separate table per constructor, so each constructor gets its own allocation site), and DWARF symbols let the runtime-stack sampler resolve names for stack frames. To enable both for your application and all its dependencies, add this to your cabal.project:

package *
  ghc-options: -finfo-table-map -fdistinct-constructor-tables
  debug-info: 2

The package * stanza also covers dependencies, so the first build after adding it recompiles everything.

The connector's startup behaviour can be tuned with environment variables. You can for example start the program paused, or change the socket/descriptor location. See the command line reference for usage information and the full list.

#3. Attach an inspector

If you have installed the VS Code extension, inspectable programs show up in the Haskell Inspector view, filtered to the open workspace, and a badge counts the ones currently running. Click a program to open the inspector next to your code.

Otherwise, just start the web interface from a terminal:

haskell-inspector --web

and open http://localhost:3000. The first view lists every inspectable process discovered on the machine; pick the right one and press Connect:

💡 Tip: use haskell-inspector --web --port 0 to automatically select a free port for the web interface.

The process picker listing the discovered running programs.
The process picker listing the discovered running programs.

#Usage

The web UI aims to be self-documenting. Click around to see the available options. Look for the (i) button in the toolbar to toggle additional information.

Most of the functionality is also available through the CLI. Find the available processes with:

haskell-inspector list

Then connect using a target flag (--pid / --descriptor / --snapshot / --eventlog / --run), pick a control policy (--every / --repeat / --duration / --until-exit), and an output format (--output / --format).

The general format is

haskell-inspector [GLOBAL-OPTS] [TARGET] [COMMAND [COMMAND-OPTS]] [-- PROG-ARGS]

Refer to the CLI reference for more details.

#Examples

For the full, always-current set of worked examples, run haskell-inspector help-examples. The recipes below show the shape: a target, an optional control policy, a command, and an output format:

# Heap consistency check, written to a file, exits non-zero on violations
haskell-inspector -p 12345 consistency --output report.txt

# Heap census by closure type (a built-in HEQL view), as JSON
haskell-inspector -p 12345 heql --tag type-profile --format json

# Live thread status, every 10s, until the program exits
haskell-inspector -p 12345 --every 10s --until-exit threads

# Spawn a program, sample its stacks for 30s, write a speedscope profile
haskell-inspector --run ./myprog --duration 30s \
    record samples profile.json --format speedscope -- --my-flag

# Capture everything (snapshot + eventlog + samples + metrics) into a dir
haskell-inspector -p 12345 --duration 2m record all ./report/

#Roadmap

This roadmap is tentative. Haskell Inspector is an experimental project, and what gets built next depends on what users actually run into: if one of these items would help you, or something important is missing, please open a ticket.


#Frequently Asked Questions

#I have a performance issue with my Haskell program. Can I solve it with Haskell Inspector?

Haskell Inspector is intended to be the first stop for looking into performance issues, memory leaks and latency spikes. If you're not sure how to approach your problem, or Haskell Inspector doesn't give you the insight you need, please open a ticket describing your situation. Real-world cases like yours help us decide which diagnostics to build next.

#Are editors/IDEs other than Visual Studio Code supported?

Haskell Inspector has a VSCode plugin to automatically discover your inspectable Haskell programs. The plugin also allows the Haskell Inspector web interface to link back to your source files.

You can also launch Haskell Inspector directly with haskell-inspector --web. See the command line reference for more information about command line options and environment variables.

If you think Haskell Inspector integration with other development environments could be improved, please open a ticket, or a pull request.

#Which Haskell compilers are supported?

Currently we support GHC 9.6+. We are actively working on improving GHC debugging information and RTS debugging instrumentation to make new versions provide more functionality.

If your favourite Haskell compiler is not supported, please open a ticket!

#Is this whole project just an excuse to write a query language and make some silly puns?

Maybe


#Acknowledgements

Haskell Inspector grew out of Haskell performance work at IOG, and wishing for better tools to do it with. It builds on ideas from many projects and people. Here are some:


Haskell Inspector is licensed under BSD-3-Clause OR Apache-2.0, at your option, written by Luite Stegeman, IOG/ICAN.

Footnotes

  1. No relation to Flax, Google's neural network library for JAX. ↩

  2. ghc-debug, by Ben Gamari, Matthew Pickering, David Eichmann, Hannes Siebenhandl and others. ↩

  3. ghc-debug-client on Hackage. ↩

  4. ThreadScope, by Satnam Singh, Simon Marlow, Donnie Jones, Duncan Coutts et al., described in Parallel Performance Tuning for Haskell (Haskell Symposium 2009). ↩

  5. eventlog2html, by Matthew Pickering, David Binder, and Claude Heiland-Allen. ↩

  6. Brendan Gregg, flame graphs; The Flame Graph, ACM Queue, 2016. ↩

  7. Jamie Wong, speedscope. ↩

  8. Philip Wadler, Comprehending Monads, LFP 1990. ↩

  9. Simon Peyton Jones and Philip Wadler, Comprehensive Comprehensions, Haskell Symposium 2007. ↩

  10. Leonidas Fegaras and David Maier, Optimizing Object Queries Using an Effective Calculus, ACM TODS 2000. ↩

  11. James Cheney, Sam Lindley and Philip Wadler, A Practical Theory of Language-Integrated Query, ICFP 2013. ↩

  12. The GQL standard (ISO/IEC 39075:2024), described by Alin Deutsch et al., Graph Pattern Matching in GQL and SQL/PGQ, SIGMOD 2022. ↩

  13. Renzo Angles et al., G-CORE: A Core for Future Graph Query Languages, SIGMOD 2018. ↩

Haskell Inspector