MyNixOS website logo
Description

Write your GDB scripts in Haskell.

Please see the README on GitHub at https://github.com/luc-tielen/debugger-hs#readme

debugger-hs

A Haskell library for creating/metaprogramming GDB scripts.

Why?

GDB has Python integration, and while it is powerful, it has some problems:

  • Python code can end up interleaved in the GDB code, making it complicated really quickly.
  • Accessing variables between GDB and Python can get complicated too.

Besides that, also some things in GDB don't work as expected, such as commands that behave differently inside a user-defined command ("define"), which can lead to unexpected and buggy results.

If we use Haskell however, we get the following nice things:

  • A typesystem,
  • Can use full Haskell ecosystem for metaprogramming,
  • The generated output will be 100% GDB script only:
    • easier to use afterwards,
    • inspectable,
    • no need for this package once the script has been generated.

How?

Partial evaluation/staged programming. You write a Haskell file that makes use of the DSL this package provides, and render it to a script that can be passed in to GDB like you would normally.

Here's an example of how you can use this library to generate a GDB script:

#!/usr/bin/env cabal
{- cabal:
build-depends: base, debugger-hs
-}

{-# LANGUAGE OverloadedStrings #-}
module Main where

import qualified Debugger.Builder as D
import qualified Debugger.Render as D
import qualified Debugger.Statement as D

-- First we build up a Haskell value that represents our GDB scripts.
script :: D.Builder ()
script = do
  bp <- D.break (D.Function "main")
  D.command bp $ do
    D.print "42"
    D.continue

-- And then we can render the GDB script to stdout:
main :: IO ()
main = do
  let gdbScript = D.runBuilder script
  D.renderToStdOut gdbScript

If you save this file as "GDB.hs" and run it using cabal run GDB.hs, this will render the following GDB script:

break main
set $var0 = $bpnum
command $var0
  print "42"
  continue
end

Now you can use the generated script with GDB as follows:

# Replace $PROGRAM with the program you want to debug.
$ gdb $PROGRAM <<< $(< cabal run GDB.hs)
# or if your shell doesn't support the previous command:
$ cabal run GDB.hs > ./script.gdb
$ gdb $PROGRAM -ex "source ./script.gdb"

TODO

  • [x] Create core AST datatype
  • [x] Create builder monad for easily constructing GDB scripts using Haskell do-syntax.
  • [x] Write function for compiling AST -> GDB script
  • [ ] Add helper functions for easily adding breakpoints (using tools like grep)
  • [ ] Extend core AST datatype to support more functionality
  • [ ] Add LLDB support also?
  • [ ] DSL (to support GDB and LLDB at same time)?
Metadata

Version

0.1.1.0

Platforms (77)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-freebsd
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64-windows
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows