#How to debug "command terminated unexpectedly"

1 messages · Page 1 of 1 (latest)

onyx condor
#

Hello! I started exploring Zig (0.12.0-dev.1270+6c9d34bce) last week and I am giving it a shot for a toy project. However, I frequently get an unexpected termination when executing zig build. I started a project by running zig init-exe and did not touch the build file. Right now, when calling zig build run, I get the following error:

$ zig build run
zig build-exe zigeme Debug native: error: the following command terminated unexpectedly:
/usr/local/zig/zig build-exe /path/src/main.zig --cache-dir /path/zig-cache --global-cache-dir /path/.cache/zig --name zigeme --listen=- 
Build Summary: 0/5 steps succeeded; 1 failed (disable with --summary none)
run transitive failure
└─ run zigeme transitive failure
   ├─ zig build-exe zigeme Debug native failure
   └─ install transitive failure
      └─ install zigeme transitive failure
         └─ zig build-exe zigeme Debug native (reused)
error: the following build command failed with exit code 1:
/path/zig-cache/o/32f15f55419546f3e7ccfe97451d53f8/build /usr/local/zig/zig /path /path/zig-cache /path/.cache/zig --seed 0x2e736222 run

From my understanding, there is some error in my main.zigthat makes the compiler crash instead of gracefully showing an error message. But how do you usually debug such errors? Is there a log that allows me to see where the compiler was when it crashed?

Thanks!

dull mantle
#

I don't know of any great way to debug compiler crashes. Each time that I've run into something like this, I ended up having to comment out large chunks to narrow down the root cause of the crash.

onyx condor
#

That's unfortunate... I tried that but I seem to have multiple errors in my code since there isn't one specific function that can be removed to fix the crash.

Maybe my bug is somewhat related to this: https://github.com/ziglang/zig/issues/17703

I know that's a lot to ask, but does anyone happen to see an obvious error in this code? I'll update if I find a minimal example and, if it turns out to be unrelated to the issue above, I'll submit another issue.

const std = @import("std");

const Allocator = std.mem.Allocator;

const Sexp = union(enum) {
    symbol: []u8,
    integer: i64,
    nil,
    cons: *[2]Sexp,
};

fn printConsTail(cdr: *Sexp) void {
    switch (cdr.*) {
        Sexp.cons => {
            printSexp(&cdr.*.cons[0]);
            printConsTail(&cdr.*.cons[1]);
        },
        Sexp.nil => std.debug.print(")", .{}),
        else => {
            std.debug.print(" . ", .{});
            printSexp(cdr);
            std.debug.print(")", .{});
        },
    }
}

fn printSexp(psexp: *Sexp) void {
    var sexp = psexp.*;

    switch (sexp) {
        Sexp.symbol => std.debug.print("{s}", .{sexp.symbol}),
        Sexp.integer => std.debug.print("{d}", .{sexp.integer}),
        Sexp.nil => std.debug.print("()", .{}),
        Sexp.cons => {
            std.debug.print("(", .{});
            printSexp(&sexp.cons[0]);
            printConsTail(&sexp.cons[1]);
        },
    }
}

fn parse(allocator: Allocator) !*Sexp {
    // allocate s-expressions
    const result = try allocator.alloc(Sexp, 1);
    result[0] = Sexp{ .nil = {} };
    return &result[0];
}

pub fn main() !void {
    const allocator = std.heap.page_allocator;

    var sexp = try parse(allocator);

    printSexp(sexp);
}

GitHub

Zig Version 0.12.0-dev.1127+32bc07767 Steps to Reproduce and Observed Behavior OS: Linux My original program is more complicated, but this is the minimal example I could create that leads to the bu...

vivid scarab
#

There's nothing wrong with your code, it's crashing due to the *[2]Sexp field (which should be valid)
Minimal repro:

const Sexp = struct {
    cons: *[2]Sexp,
};

pub fn main() void {
    var s: Sexp = undefined;
    _ = s;
}

Compiler segfaults on 0.12.0-dev.1396+f6de3ec96