# Debugging tests that crash

**URL:** <https://ziggit.dev/t/debugging-tests-that-crash/6604>\
**Category:** Help\
**Tags:** language, memory-management\
**Created:** [October 30, 2024, 8:10pm UTC](https://ziggit.dev/t/debugging-tests-that-crash/6604 "2024-10-30T20:10:21Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![joelreymont](https://ziggit.dev/user_avatar/ziggit.dev/joelreymont/32/3634_2.png) [@joelreymont](https://ziggit.dev/u/joelreymont)\
**Post date:** [October 30, 2024, 8:10pm UTC](https://ziggit.dev/t/debugging-tests-that-crash/6604/1 "2024-10-30T20:10:21Z")

</div>

How do I debug a test that segfaults?

```zig
❯ zig build test --summary all
test
└─ run test failure
error: while executing test 'sleigh.test.defined(id)', the following command terminated with signal 11 (expected exited with code 0):
/Users/joelr/Work/Zig/bebop-zig/.zig-cache/o/43d0e2ef192792eae7de1691cf7d93dc/test --seed=0x4a364834 --cache-dir=/Users/joelr/Work/Zig/bebop-zig/.zig-cache --listen=-
Build Summary: 3/5 steps succeeded; 1 failed; 19/19 tests passed

```

---

<div class="post-metadata">

**Author:** ![dimdin](https://ziggit.dev/user_avatar/ziggit.dev/dimdin/32/1457_2.png) [@dimdin](https://ziggit.dev/u/dimdin)\
**Post date:** [October 30, 2024, 9:36pm UTC](https://ziggit.dev/t/debugging-tests-that-crash/6604/2 "2024-10-30T21:36:52Z")

</div>

> [@Zig Debugging with LLDB](https://ziggit.dev/t/zig-debugging-with-lldb/3931/10):
>
> Handy tip when trying to debug tests: if the test binary crashes it will print the command that you could paste into a debugger. And one way to make it crash is to use @breakpoint() somewhere. Then you kill 2 birds with 1 stone because the debugger will stop at your breakpoint.

---

<div class="post-metadata">

**Author:** ![kristoff](https://ziggit.dev/user_avatar/ziggit.dev/kristoff/32/9_2.png) [@kristoff](https://ziggit.dev/u/kristoff)\
**Post date:** [October 31, 2024, 12:28am UTC](https://ziggit.dev/t/debugging-tests-that-crash/6604/3 "2024-10-31T00:28:03Z")

</div>

To repeat what dimdin said in more concrete terms: run the command that failed directly.

```zig
/Users/joelr/Work/Zig/bebop-zig/.zig-cache/o/43d0e2ef192792eae7de1691cf7d93dc/test --seed=0x4a364834 --cache-dir=/Users/joelr/Work/Zig/bebop-zig/.zig-cache --listen=-

```

And if you want to debug it with LLDB, prefix the command with `lldb -- `

---

<div class="post-metadata">

**Author:** ![andrewrk](https://ziggit.dev/user_avatar/ziggit.dev/andrewrk/32/7011_2.png) [@andrewrk](https://ziggit.dev/u/andrewrk)\
**Post date:** [October 31, 2024, 12:34am UTC](https://ziggit.dev/t/debugging-tests-that-crash/6604/4 "2024-10-31T00:34:42Z")

</div>

Sorry, I neglected to mention, also remove the `--listen` flag unless you’re trying to debug the interface between those processes.

---

<div class="post-metadata">

**Author:** ![joelreymont](https://ziggit.dev/user_avatar/ziggit.dev/joelreymont/32/3634_2.png) [@joelreymont](https://ziggit.dev/u/joelreymont)\
**Post date:** [October 31, 2024, 8:03am UTC](https://ziggit.dev/t/debugging-tests-that-crash/6604/5 "2024-10-31T08:03:05Z")

</div>

Turns out my parser was too recursive. It’s curious that it crashed with bad access and did not run out of memory, though.

```zig
(lldb) r
Process 76795 launched: '/Users/joelr/Work/Zig/bebop-zig/.zig-cache/o/43d0e2ef192792eae7de1691cf7d93dc/test' (arm64)
Process 76795 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=2, address=0x16edfff70)
    frame #0: 0x00000001000d3e74 test`mecha.map__struct_26223.parse(allocator=<unavailable>, str=(ptr = <read memory from 0x16edfff80 failed (0 of 8 bytes read)>, len = <read memory from 0x16edfff88 failed (0 of 8 bytes read)>)) at mecha.zig:614
   611 const Res = Result(ReturnType(@TypeOf(conv)));
   612 typecheckParser(@TypeOf(parser));
   613 return .{ .parse = struct {
-> 614 fn parse(allocator: mem.Allocator, str: []const u8) Error!Res {
   615 const r = try parser.parse(allocator, str);
   616 return Res{ .value = conv(r.value), .rest = r.rest };
   617 }
Target 0: (test) stopped.
(lldb) bt
^C* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=2, address=0x16edfff70)
  * frame #0: 0x00000001000d3e74 test`mecha.map__struct_26223.parse(allocator=<unavailable>, str=(ptr = <read memory from 0x16edfff80 failed (0 of 8 bytes read)>, len = <read memory from 0x16edfff88 failed (0 of 8 bytes read)>)) at mecha.zig:614
    frame #1: 0x00000001000d409c test`mecha.map__struct_26293.parse(allocator=<unavailable>, str=(ptr = " defined (foo)", len = 14)) at mecha.zig:615:39
    frame #2: 0x00000001000d30d8 test`mecha.oneOf__struct_27067.parse(allocator=<unavailable>, str=(ptr = " defined (foo)", len = 14)) at mecha.zig:418:28
    frame #3: 0x00000001000d3784 test`mecha.ref__struct_26046.parse(allocator=mem.Allocator @ 0x0000000100116410, str=(ptr = " defined (foo)", len = 14)) at mecha.zig:937:32
    frame #4: 0x00000001000d387c test`mecha.convert__struct_26109.parse(allocator=<unavailable>, str=(ptr = " defined (foo)", len = 14)) at mecha.zig:499:39
    frame #5: 0x00000001000d3c14 test`mecha.combine__struct_26168.parse(allocator=mem.Allocator @ 0x0000000100116410, str=(ptr = " defined (foo)", len = 14)) at mecha.zig:366:43
    frame #6: 0x00000001000d3ea0 test`mecha.map__struct_26223.parse(allocator=<unavailable>, str=(ptr = " defined (foo)", len = 14)) at mecha.zig:615:39
    frame #7: 0x00000001000d409c test`mecha.map__struct_26293.parse(allocator=<unavailable>, str=(ptr = " defined (foo)", len = 14)) at mecha.zig:615:39
...
    frame #7817: 0x00000001000d3c14 test`mecha.combine__struct_26168.parse(allocator=mem.Allocator @ 0x0000000100116410, str=(ptr = " defined (foo)", len = 14)) at mecha.zig:366:43
    frame #7818: 0x00000001000d3ea0 test`mecha.map__struct_26223.parse(allocator=<unavailable>, str=(ptr = " defined (foo)", len = 14)) at mecha.zig:615:39

... Interrupted.

```
