zig icon indicating copy to clipboard operation
zig copied to clipboard

Issue with std.debug.dumpCurrentStackTrace called from C.

Open ArnaudValensi opened this issue 2 years ago • 3 comments

Zig Version

0.11.0

Steps to Reproduce and Observed Behavior

Hi,

I'm trying to get the zig stacktrace from C like Andrew is showing here:

https://youtu.be/YXrb-DqsBNU?si=rdDXlcW7tZiqQiix&t=991

It works for windows, but I only see the C part of the stacktrace, not the zig part:

???:?:?: 0x7ff6233315be in ??? (???)
???:?:?: 0x7ff6233314c5 in ??? (???)
D:/dev-2/kick-crafter/repos/stacktrace\main.c:7:9: 0x7ff6233314a7 in foo (main.c)
        dump_stack_trace();
        ^
D:/dev-2/kick-crafter/repos/stacktrace\main.c:15:5: 0x7ff623331484 in main (main.c)
    foo(1);
    ^
C:/M/B/src/mingw-w64/mingw-w64-crt/crt\crtexe.c:272:15: 0x7ff6233312ed in __tmainCRTStartup (C:/M/B/src/mingw-w64/mingw-w64-crt/crt/crtexe.c)
C:/M/B/src/mingw-w64/mingw-w64-crt/crt\crtexe.c:193:9: 0x7ff623331405 in mainCRTStartup (C:/M/B/src/mingw-w64/mingw-w64-crt/crt/crtexe.c)
???:?:?: 0x7ff91d667613 in ??? (KERNEL32.DLL)
???:?:?: 0x7ff91d9826b0 in ??? (ntdll.dll)

The two first lines are supposed to show zig calls.

My second issue is that I cannot link at all for linux, I get this:

/usr/bin/ld: stacktrace.o: relocation R_X86_64_32S against `.rodata.cst16' can not be used when making a PIE object; recompile with -fPIE
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Here is my code and how I build:

// stacktrace.zig
const std = @import("std");

export fn dump_stack_trace() void {
    std.debug.dumpCurrentStackTrace(null);
}

pub const _start = void;
// main.c
#include <stdio.h>

void dump_stack_trace(void);

static void foo(int dump) {
    if (dump) {
        dump_stack_trace();
    }
}

int main(int argc, char **argv) {
    (void) argc;
    (void) argv;

    foo(1);
    return 0;
}

Building for Windows:

zig.exe build-obj stacktrace.zig
clang.exe -c main.c -g
clang.exe main.o stacktrace.obj -o main.exe -lntdll

Building for Linux:

zig build-obj stacktrace.zig
clang -c main.c -g
clang stacktrace.o main.o -o main

I also tried to build everything with zig with the following build.zig:

// build.zig
const std = @import("std");

pub fn build(b: *std.build.Builder) void {
    const target = b.standardTargetOptions(.{});
    const optimize = b.standardOptimizeOption(.{});

    const exe = b.addExecutable(.{
        .name = "foobar",
        .root_source_file = .{ .path = "stacktrace.zig" },
        .target = target,
        .optimize = optimize,
    });
    exe.addCSourceFile(.{
        .file = .{ .path = "main.c" },
        .flags = &.{"-std=c99"},
    });
    exe.linkLibC();
    b.installArtifact(exe);
}

It works perfectly on linux with all the stacktrace (zig and C), but I cannot build for windows, I get this:

$ zig.exe build
zig build-exe foobar Debug native: error: the following command failed with 1 compilation errors:
C:\Users\arnau.DESKTOP-2ODO5PA\dev\zig\zig-windows-x86_64-0.11.0\zig.exe build-exe D:\dev-2\kick-crafter\repos\stacktrace\stacktrace.zig -cflags -std=c99 -- D:\dev-2\kick-crafter\repos\stacktrace\main.c -lc --cache-dir D:\dev-2\kick-crafter\repos\stacktrace\zig-cache --
global-cache-dir C:\Users\arnau.DESKTOP-2ODO5PA\AppData\Local\zig --name foobar --listen=-
Build Summary: 0/3 steps succeeded; 1 failed (disable with --summary none)
install transitive failure
└─ install foobar transitive failure
   └─ zig build-exe foobar Debug native 1 errors
C:\Users\arnau.DESKTOP-2ODO5PA\dev\zig\zig-windows-x86_64-0.11.0\lib\std\start.zig:559:45: error: root struct of file 'stacktrace' has no member named 'main'
    switch (@typeInfo(@typeInfo(@TypeOf(root.main)).Fn.return_type.?)) {
                                        ~~~~^~~~~
C:\Users\arnau.DESKTOP-2ODO5PA\dev\zig\zig-windows-x86_64-0.11.0\lib\std\start.zig:508:12: note: called from here
    return @call(.always_inline, callMain, .{});
           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
C:\Users\arnau.DESKTOP-2ODO5PA\dev\zig\zig-windows-x86_64-0.11.0\lib\std\start.zig:339:65: note: called from here
    std.os.windows.kernel32.ExitProcess(initEventLoopAndCallMain());
                                        ~~~~~~~~~~~~~~~~~~~~~~~~^~

So it seems to have three different issues.

There a reproduction repo here: https://github.com/ArnaudValensi/zig-stacktrace-issue/tree/master

Expected Behavior

I expect everything to work with both Linux and Windows, and with both using zig build system and clang.

ArnaudValensi avatar Aug 27 '23 09:08 ArnaudValensi

Update: still exists in latest master (0.16.0-dev.1424+d3e20e71b).

I have 16984_stacktrace.zig:

// stacktrace.zig
const std = @import("std");

export fn dump_stack_trace() void {
    std.debug.dumpCurrentStackTrace(.{ .allow_unsafe_unwind = true });
}

pub const _start = void;

and 16984_main.c:

#include <stdio.h>

void dump_stack_trace(void);

static void foo(int dump) {
    if (dump) {
        dump_stack_trace();
    }
}

int main(int argc, char **argv) {
    foo(1);
    return 0;
}

Compiled together (zig build-obj 16984_stacktrace.zig, zig cc -c 16984_main.c -g, zig cc 16984_stacktrace.o 16984_main.o -o 16984) it segfaults. Stack trace obtained via GDB (coredumpctl):

#0  0x000000000104e5a9 in posix.getenv (key=...)
#1  0x00000000010267cb in process.hasNonEmptyEnvVarConstant__anon_4358 (key=...)
#2  0x0000000001020e94 in Io.tty.Config.detect (file=...)
#3  0x000000000101df85 in debug.lockStderrWriter (buffer=...)
#4  0x000000000101dcf1 in debug.dumpCurrentStackTrace (options=...) at /home/sfiedler/.zvm/master/lib/std/debug.zig:745
#5  0x000000000101dc83 in dump_stack_trace () at 16984_stacktrace.zig:5
#6  0x000000000101dc26 in foo (dump=1) at 16984_main.c:7
#7  0x000000000101dc00 in main (argc=1, argv=0x7fffaeaa6678) at 16984_main.c:12

sfiedler0 avatar Nov 23 '25 10:11 sfiedler0

in my experience this is fixed by adding -lc to your zig build-obj calls

nektro avatar Nov 23 '25 17:11 nektro

in my experience this is fixed by adding -lc to your zig build-obj calls

can confirm

sfiedler0 avatar Nov 23 '25 17:11 sfiedler0