zig-debugging
Zig Debugging
Purpose
Guide agents through debugging Zig programs: GDB/LLDB sessions, interpreting Zig panics and error return traces, std.debug.print logging, debug build configuration, and IDE integration.
Triggers
- "How do I debug a Zig program with GDB?"
- "How do I interpret a Zig panic message?"
- "How do I use std.debug.print for debugging?"
- "Zig is showing an error return trace — what does it mean?"
- "How do I set up Zig debugging in VS Code?"
- "How do I get a stack trace from a Zig crash?"
Workflow
1. Build for debugging
# Debug build (default) — full debug info, safety checks
zig build-exe src/main.zig -O Debug
# With build system
zig build # uses Debug by default
zig build -Doptimize=Debug
# Run directly with debug output
zig run src/main.zig
2. GDB with Zig
Zig emits standard DWARF debug information compatible with GDB:
# Build with debug info
zig build-exe src/main.zig -O Debug -femit-bin=myapp
# Launch GDB
gdb ./myapp
# GDB session
(gdb) break main
(gdb) run arg1 arg2
(gdb) next # step over
(gdb) step # step into
(gdb) continue
(gdb) print my_var
(gdb) info locals
(gdb) bt # backtrace
Break on Zig panics:
(gdb) break __zig_panic_start
(gdb) break std.builtin.default_panic
3. LLDB with Zig
lldb ./myapp
(lldb) b main
(lldb) r arg1 arg2
(lldb) n # next
(lldb) s # step into
(lldb) p my_var # print
(lldb) frame variable
(lldb) bt # backtrace
(lldb) c # continue
# Break on panic
(lldb) b __zig_panic
4. Interpreting Zig panics
Zig panics include the source location and reason:
thread 'main' panic: index out of bounds: index 5, len 3
/home/user/src/main.zig:15:14
/home/user/src/main.zig:42:9
???:?:?: (name not available)
Common panic messages:
| Panic | Cause |
|---|---|
index out of bounds: index N, len M |
Slice/array OOB access |
integer overflow |
Arithmetic overflow in Debug/ReleaseSafe |
attempt to unwrap null |
Optional access .? on null |
reached unreachable code |
unreachable executed |
casting... |
Invalid enum tag or union access |
integer cast truncated bits |
@intCast with value out of range |
out of memory |
Allocator failed |
5. Error return traces
Zig tracks where errors propagate with error return traces:
error: FileNotFound
/home/user/src/main.zig:30:20: 0x10a3b in openConfig (main)
const f = try std.fs.openFileAbsolute(path, .{});
^
/home/user/src/main.zig:15:25: 0x10b12 in run (main)
const cfg = try openConfig("/etc/myapp.conf");
^
/home/user/src/main.zig:8:20: 0x10c44 in main (main)
try run();
^
The trace shows the exact try chain where the error propagated. Read bottom-up: main → run → openConfig.
Enable in release builds:
// build.zig
const exe = b.addExecutable(.{
.name = "myapp",
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
.error_tracing = true, // enable even in ReleaseFast
});
6. std.debug.print for tracing
const std = @import("std");
pub fn main() !void {
const x: u32 = 42;
const name = "world";
// Basic print (always to stderr)
std.debug.print("x = {d}, name = {s}\n", .{ x, name });
// Print any value (useful for structs)
const point = Point{ .x = 1, .y = 2 };
std.debug.print("point = {any}\n", .{point});
// Formatted output
std.debug.print("hex: {x}, binary: {b}\n", .{ x, x });
// Log levels (respects compile-time log level)
const log = std.log.scoped(.my_module);
log.debug("debug info: {d}", .{x});
log.info("started processing", .{});
log.warn("unusual condition", .{});
log.err("failed: {s}", .{"reason"});
}
7. std.log configuration
// Override default log level at root
pub const std_options = std.Options{
.log_level = .debug, // .debug | .info | .warn | .err
};
// Custom log handler
pub fn logFn(
comptime level: std.log.Level,
comptime scope: @TypeOf(.enum_literal),
comptime format: []const u8,
args: anytype,
) void {
const prefix = "[" ++ @tagName(level) ++ "] (" ++ @tagName(scope) ++ "): ";
std.debug.print(prefix ++ format ++ "\n", args);
}
pub const std_options = std.Options{
.logFn = logFn,
};
8. VS Code / IDE integration
Install the zig.vscode-zig extension and CodeLLDB.
.vscode/launch.json:
{
"version": "0.2.0",
"configurations": [
{
"type": "lldb",
"request": "launch",
"name": "Debug Zig",
"program": "${workspaceFolder}/zig-out/bin/myapp",
"args": [],
"cwd": "${workspaceFolder}",
"preLaunchTask": "zig build"
}
]
}
.vscode/tasks.json:
{
"version": "2.0.0",
"tasks": [
{
"label": "zig build",
"type": "shell",
"command": "zig build",
"group": { "kind": "build", "isDefault": true },
"problemMatcher": ["$zig"]
}
]
}
Related skills
- Use
skills/zig/zig-compilerfor build modes and debug info flags - Use
skills/debuggers/gdbfor GDB fundamentals - Use
skills/debuggers/lldbfor LLDB fundamentals - Use
skills/zig/zig-cinteropwhen debugging mixed Zig/C code
More from mohitmishra786/low-level-dev-skills
cmake
CMake build system skill for C/C++ projects. Use when writing or refactoring CMakeLists.txt, configuring out-of-source builds, selecting generators (Ninja, Make, VS), managing targets and dependencies with target_link_libraries, integrating external packages via find_package or FetchContent, enabling sanitizers, setting up toolchain files for cross-compilation, or exporting CMake packages. Activates on queries about CMakeLists.txt, cmake configure errors, target properties, install rules, CPack, or CMake presets.
579static-analysis
Static analysis skill for C/C++ codebases. Use when hardening code quality, triaging noisy builds, running clang-tidy, cppcheck, or scan-build, interpreting check categories, suppressing false positives, or integrating static analysis into CI. Activates on queries about clang-tidy checks, cppcheck, scan-build, compile_commands.json, code hardening, or static analysis warnings.
407llvm
LLVM IR and pass pipeline skill. Use when working directly with LLVM Intermediate Representation (IR), running opt passes, generating IR with llc, inspecting or writing LLVM IR for custom passes, or understanding how the LLVM backend lowers IR to assembly. Activates on queries about LLVM IR, opt, llc, llvm-dis, LLVM passes, IR transformations, or building LLVM-based tools.
361gdb
GDB debugger skill for C/C++ programs. Use when starting a GDB session, setting breakpoints, stepping through code, inspecting variables, debugging crashes, using reverse debugging (record/replay), remote debugging with gdbserver, or loading core dumps. Activates on queries about GDB commands, segfaults, hangs, watchpoints, conditional breakpoints, pretty-printers, Python GDB scripting, or multi-threaded debugging.
153linux-perf
Linux perf profiler skill for CPU performance analysis. Use when collecting sampling profiles with perf record, generating perf report, measuring hardware counters (cache misses, branch mispredicts, IPC), identifying hot functions, or feeding perf data into flamegraph tools. Activates on queries about perf, Linux performance counters, PMU events, off-CPU profiling, perf stat, perf annotate, or sampling-based profiling on Linux.
142core-dumps
Core dump analysis skill for production crash triage. Use when loading core files in GDB or LLDB, enabling core dump generation on Linux/macOS, mapping symbols with debuginfo or debuginfod, or extracting backtraces from crashes without re-running the program. Activates on queries about core files, ulimit, coredumpctl, debuginfod, crash triage, or analyzing segfaults from production binaries.
131