-
-
Save zeitt/73f5f97a60e661e90b570736d4705bf2 to your computer and use it in GitHub Desktop.
sprinkling a little zig into a C project to help with debugging
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const std = @import("std"); | |
| pub fn build(b: *std.Build) void { | |
| // Standard target options allows the person running `zig build` to choose | |
| // what target to build for. Here we do not override the defaults, which | |
| // means any target is allowed, and the default is native. Other options | |
| // for restricting supported target set are available. | |
| const target = b.standardTargetOptions(.{}); | |
| // Standard optimization options allow the person running `zig build` to select | |
| // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. Here we do not | |
| // set a preferred release mode, allowing the user to decide how to optimize. | |
| const optimize = b.standardOptimizeOption(.{}); | |
| const exe = b.addExecutable(.{ | |
| .name = "example", | |
| .target = target, | |
| .optimize = optimize, | |
| // In this case the main source file is merely a path, however, in more | |
| // complicated build scripts, this could be a generated file. | |
| .root_source_file = .{ .path = "helper.zig" }, | |
| }); | |
| exe.addCSourceFiles(&.{"foo.c"}, &.{"-std=c99"}); | |
| exe.linkLibC(); | |
| // This declares intent for the executable to be installed into the | |
| // standard location when the user invokes the "install" step (the default | |
| // step when running `zig build`). | |
| exe.install(); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| static void bar() { | |
| char *ptr = 0; | |
| *ptr = 1; | |
| } | |
| void setup_debug_handlers(void); | |
| int main(int argc, char **argv) { | |
| setup_debug_handlers(); | |
| bar(); | |
| return 0; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const std = @import("std"); | |
| export fn setup_debug_handlers() void { | |
| std.debug.maybeEnableSegfaultHandler(); | |
| } | |
| export fn dump_stack_trace() void { | |
| std.debug.dumpCurrentStackTrace(@returnAddress()); | |
| } | |
| /// Communicate to zig that main() will be provided elsewhere. | |
| pub const _start = {}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| andy@ark ~/t/abc> zig build | |
| andy@ark ~/t/abc> ./zig-out/bin/example | |
| Illegal instruction at address 0x20bc5e | |
| /home/andy/tmp/abc/foo.c:3:10: 0x20bc5e in bar (/home/andy/tmp/abc/foo.c) | |
| *ptr = 1; | |
| ^ | |
| /home/andy/tmp/abc/foo.c:10:5: 0x20bc2f in main (/home/andy/tmp/abc/foo.c) | |
| bar(); | |
| ^ | |
| ???:?:?: 0x7fdea03da24d in ??? (???) | |
| ???:?:?: 0x0 in ??? (???) | |
| fish: Job 1, './zig-out/bin/example' terminated by signal SIGABRT (Abort) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment