Enable debug build for Zig std library

I am rather new to Zig thus is still playing with the hello program generated by zig init on Ubuntu 22.

The default build allows me to use gdb to track program sources, however it seems that I can’t step into Zig standard library functions.

How can we tweak build.zig to pass debug build option to Zig std library sources as well?

this is surprising to me. zig defaults to a single compilation unit per binary, so the situation with the standard library should be the same as with your code. indeed, in LLDB i’m regularly able to step into standard library code as well as other zig dependencies and even C compiled in by the build system.

maybe GDB is having trouble matching symbols to source locations?

I can see some sources from within gdb:

gdb) info sources 
/tmp/aaa/bin/hello:
(Full debug information has not yet been read for this file.)

/opt/zig-x86_64-linux-0.16.0/lib/std/debug/no_panic.zig, /opt/zig-x86_64-linux-0.16.0/lib/std/std.zig, /opt/zig-x86_64-linux-0.16.0/lib/compiler_rt/count0bits.zig, /opt/zig-x86_64-linux-0.16.0/lib/compiler_rt.zig, /tmp/.cache/b/c08c31c8f7a39a3aaf2c053849413f07/builtin.zig, /opt/zig-x86_64-linux-0.16.0/lib/std/debug.zig, /tmp/.cache/b/732180f821dd8acbfb164c6928ad5abc/builtin.zig, /opt/zig-x86_64-linux-0.16.0/lib/ubsan_rt.zig, /tmp/hello/src/root.zig, /tmp/hello/src/main.zig
(gdb) b main.main 
Breakpoint 1 at 0x11a9883: file main.zig, line 8.
(gdb) run
Starting program: /tmp/aaa/bin/hello 

Breakpoint 1, main.main (init=...) at main.zig:8
8	    std.debug.print("All your {s} are belong to us.\n", .{"codebase"});
(gdb) bt
#0  main.main (init=...) at main.zig:8
(gdb) step
All your codebase are belong to us.
11	    const arena: std.mem.Allocator = init.arena.allocator();
(gdb) b std.debug.print
Function "std.debug.print" not defined.

I guess you are right, now I can break at std.debug.print__anon_22496, the symbol name isn’t same as std.debug.print in the source.

(gdb) br debug.print__anon_22496 
Breakpoint 2 at 0x113ecd4
(gdb) run
Starting program: /tmp/aaa/bin/hello 

Breakpoint 2, 0x000000000113ecd4 in debug.print__anon_22496 (fmt=..., args=...)
(gdb) bt
#0  0x000000000113ecd4 in debug.print__anon_22496 (fmt=..., args=...)
#1  0x000000000113d59d in main.main () at main.zig:6

However, I am still not able to step in GDB yet.

I haven’t experienced this problem myself, but I think I’ve seen in other threads that the new non-llvm backend, which is used by default in default/debug mode, is not compatible with the debuggers. If you want to try it, you can add -fllvm when running build-exe and then trying debugging with lldb. I could be wrong.

Or to use zig build see this note in the other thread:

2 Likes