gdb and a Haskell binary
Why gdb sees little
A Haskell binary is a C program only at its edges. The RTS is C, the entry point is a small C function, and every Haskell function is a closure with a code pointer; the stack that gdb walks is the C stack, and the evaluation of a lazy program happens on a heap structure it does not know. A backtrace therefore stops at the runtime and shows a frame or two, however many Haskell functions are running.
That is not a reason to ignore gdb: it is the tool for a crash in the FFI, a deadlock in the runtime, or a segmentation fault in a foreign call. For a wrong value in pure code, the GHCi debugger and Debug.Trace of the two previous lessons are the tools.
A program built with -g
module Main (main) where
fact :: Integer -> Integer
fact 0 = 1
fact n = n * fact (n - 1)
main :: IO ()
main = print (fact 20)
ghc -Wall -O0 -g -o loop loop.hs
./loop
2432902008176640000
-g keeps the debug information and -O0 keeps the functions separate: at -O1 the small fact would be inlined and there would be nothing to break on. The symbol table still shows the names with their module prefix:
nm loop | grep ' Main_'
00000000004e40a8 D Main_main_closure
0000000000409bf0 T Main_main_info
00000000004e4020 D Main_zdtrModule_closure
Main_main_info is the entry point of the program and ZCMain_main_info is the wrapper the C runtime calls. A user function such as fact is not in this list, because GHC specialised it into the Integer arithmetic of main: that is the inlining the next section mentions.
A breakpoint on a Haskell entry point
gdb -batch -ex 'break ZCMain_main_info' -ex run -ex 'bt 6' ./loop
Breakpoint 1 at 0x409c5f (2 locations)
warning: Error disabling address space randomization: Operation not permitted
[New LWP 2395]
Thread 1 "loop" hit Breakpoint 1.2, 0x0000000000409c60 in ZCMain_main_info () at loop.hs:8
8 main = print (fact 20)
#0 0x0000000000409c60 in ZCMain_main_info () at loop.hs:8
#1 0x0000000000000000 in ?? ()
The breakpoint landed on line 8, the source line of main, which is real information. The backtrace is the interesting part: frame 0 is the entry point and frame 1 is address zero, because the C stack ends there. The Haskell stack is not a chain of return addresses on this stack, so bt cannot show the callers of main, and there are none to show for the entry point anyway.
The warning about address space randomization is the container refusing personality(2); the addresses above are from this run and change between runs. Neither affects the lesson: a breakpoint by symbol name works regardless of the addresses.
An exception is not a signal
A Haskell program that divides by zero does not segfault: the RTS raises a Haskell exception, prints it and exits with status 1. gdb sees none of it, because no signal is delivered and no frame is unwound by the C runtime. The program below is the whole test:
module Main (main) where
main :: IO ()
main = print (1 `div` 0)
ghc -Wall -O0 -g -o crash crash.hs
./crash
echo "status: $?"
crash: divide by zero
status: 1
The program exits with status 1, and under gdb the same run simply ends:
gdb -batch -ex run -ex 'bt 4' ./crash
warning: Error disabling address space randomization: Operation not permitted
[New LWP 2423]
crash: divide by zero
[LWP 2423 exited]
[Inferior 1 (process 2420) exited with code 01]
No stack.
That is the shape of the problem: no stop, no frame, and the message gdb prints is the one the RTS wrote. A breakpoint on rts_... functions or on the C function that raises the exception is possible, and it shows the runtime's frames rather than the Haskell ones.
What to use instead
- A wrong value in pure code: a GHCi breakpoint, :print and :step, as in the debugger lesson.
- A function called at the wrong time: Debug.Trace, with the buffering caveat of the trace lesson.
- A space leak or a slow program: +RTS -s and the timing dump, as in the lessons on space leaks and flags.
- A crash in C code called through the FFI: gdb is the right tool, and -g on both the Haskell and the C side is what makes it useful.
The next lesson changes subject to the language itself: the difference between Haskell 2010, which plain ghc uses, and GHC2021, which is the extension set a modern Cabal file asks for.
Exercises
- List the Main_ symbols of a program of your own and find the entry point.
- Set a breakpoint on Main_main_info and print bt, then explain why it is shorter than you expected.
- Build the same program with -O1 and compare the symbols with -O0.
- Run the divide-by-zero program under gdb with a breakpoint on rts_yield and describe what happens.
- Explain why the arithmetic exception is not a segmentation fault.
- Decide for three bugs (a wrong sum, a deadlock, a segfault in a C call) which tool of this part of the course you would use.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
Why does gdb show only a few frames for a Haskell program?
Answer
The C stack ends at the runtime and the Haskell evaluation is on the heap
Haskell calls are not C return addresses, so the C backtrace stops at the entry point.
-
Which flags keep a function visible to gdb?
Answer
-gand-O0Debug information plus no optimisation keeps the functions separate and named.
-
What happens to a Haskell arithmetic exception under gdb?
Answer
The RTS handles it and the program exits, with no signal for gdb
The exception is raised and printed by the runtime, which then exits with status 1.
-
What does this program print? ``
module Main (main) where main :: IO () main = print (6div2)``Answer
3
Six divided by two is three and print shows it without quotes.
-
What does
nmlist for a Haskell binary?Answer
The symbols, with names like Main_main_info and Main_zdtrModule_closure
The mangled names encode the module and the definition, and nm lists them from the symbol table.
-
What does this program print?
module Main (main) where fact :: Integer -> Integer fact n = if n == 0 then 1 else n * fact (n - 1) main :: IO () main = print (fact 5)Answer
120
Five factorial is 120, and the recursive definition computes it.
-
What is the right tool for a wrong value in a pure function?
Answer
A GHCi breakpoint or Debug.Trace
gdb does not see the Haskell evaluation, so the debugger and the trace are the tools.
-
What does
btprint at ZCMain_main_info in the lesson?Answer
One frame and then address zero
The C stack ends at the entry point, so frame 1 is 0x0 and gdb prints ?? ().
-
Why does the container print a warning about address space randomization?
Answer
gdb cannot disable ASLR without a permission the container does not grant
gdb asks the kernel to turn ASLR off and the container refuses the personality call.
-
Which case is gdb still the right tool for?
Answer
A segmentation fault inside a C function called through the FFI
A C-level fault happens in the C stack, which is exactly what gdb walks.
The code on this page was built with GHC 9.10.3 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev