关于 zig-debugging
为调试 Zig 程序的 Agent 提供操作指导,适用于处理崩溃、错误传播和运行时问题。内容包括 GDB、LLDB、日志输出、调试构建及 IDE 配置。
主要功能
- 01使用 GDB 与 LLDB 调试
- 02解读 Zig panic 信息
- 03追踪错误返回链
- 04通过 std.debug.print 输出日志
- 05配置调试构建与日志级别
- 06设置 VS Code 调试
适用场景
- 01定位 Zig 程序崩溃位置
- 02理解错误返回追踪
- 03为程序添加调试日志
- 04配置 VS Code 调试会话
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ mohitmishra786/ low-level-dev-skills --skill zig-debugging安装后在 Claude Code 中输入/zig-debugging确认已加载。
附带文件references/zig-debug-patterns.md
详细介绍
Zig 调试
目的
指导 Agent 调试 Zig 程序:GDB/LLDB 会话、解读 Zig panic 和错误返回跟踪、使用 std.debug.print 记录日志、调试构建配置以及 IDE 集成。
触发条件
- “如何使用 GDB 调试 Zig 程序?”
- “如何解读 Zig panic 消息?”
- “如何使用 std.debug.print 进行调试?”
- “Zig 显示了错误返回跟踪——这是什么意思?”
- “如何在 VS Code 中设置 Zig 调试?”
- “如何获取 Zig 崩溃时的堆栈跟踪?”
工作流
1. 为调试构建
# 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 调试 Zig
Zig 会生成与 GDB 兼容的标准 DWARF 调试信息:
# 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
在 Zig panic 处设置断点:
(gdb) break __zig_panic_start
(gdb) break std.builtin.default_panic
3. 使用 LLDB 调试 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. 解读 Zig panic
Zig panic 会包含源代码位置和原因:
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)
常见的 panic 消息:
| Panic | 原因 |
|---|---|
index out of bounds: index N, len M | 切片/数组越界访问 |
integer overflow | Debug/ReleaseSafe 模式下的算术溢出 |
attempt to unwrap null | 对 null 使用可选值访问 .? |
reached unreachable code | 执行了 unreachable |
casting... | 无效的枚举标签或联合体访问 |
integer cast truncated bits | @intCast 的值超出范围 |
out of memory | 分配器失败 |
5. 错误返回跟踪
Zig 会通过错误返回跟踪记录错误的传播位置:
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();
^
跟踪会显示错误传播所经过的确切 try 调用链。请从下往上阅读:main → run → openConfig。
在发布构建中启用:
// 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 跟踪
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 配置
// 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 集成
安装 zig.vscode-zig 扩展和 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"]
}
]
}
相关技能
- 使用
skills/zig/zig-compiler了解构建模式和调试信息标志 - 使用
skills/debuggers/gdb了解 GDB 基础知识 - 使用
skills/debuggers/lldb了解 LLDB 基础知识 - 调试混合 Zig/C 代码时,使用
skills/zig/zig-cinterop
评论
支持 Markdown