Skip to content

std.debug: fix some corner cases - #23927

Closed
rootbeer wants to merge 5 commits into
ziglang:masterfrom
rootbeer:debug-mcontext-stage1
Closed

std.debug: fix some corner cases #23927
rootbeer wants to merge 5 commits into
ziglang:masterfrom
rootbeer:debug-mcontext-stage1

Conversation

@rootbeer

Copy link
Copy Markdown
Contributor

Add infinite loop detection to the std.debug backtraces. Make the backtrace and stacktrace code more robust on corner-case architectures.

Expand the "unwind.zig" test case to exercise std.debug.dumpCurrentStackTrace(). And trigger a signal handler so the test can exercise std.debug.dumpStackTraceFromBase() and std.debug.StackIterator.initWithContext() using a kernel-constructed context.

This is preparation for moving std.debug away from getContext() (#23801).

Comment threadlib/std/debug.zig Outdated
Comment threadlib/std/debug.zig Outdated
Comment threadlib/std/debug.zig Outdated
Comment threadlib/std/debug.zig
Comment threadlib/std/debug.zig Outdated
@alexrpalexrp self-assigned this May 19, 2025
Comment threadtest/standalone/stack_iterator/unwind.zig Outdated
Comment threadtest/standalone/stack_iterator/unwind.zig Outdated
@rootbeer
rootbeerforce-pushed the debug-mcontext-stage1 branch from b95af2b to 37ebc96CompareMay 19, 2025 22:41
@rootbeer
rootbeer marked this pull request as ready for review May 20, 2025 15:49
@rootbeer

Copy link
Copy Markdown
ContributorAuthor

This is ready for a review. I think the actual fixes are all straightforward, but the test is generating a lot of stderr spew (both from the dump-stack-trace functions being tested and my verbose std.debug.print logging. This all shows up in "passing" test output (e.g., https://github.com/ziglang/zig/actions/runs/15124641832/job/42514310709?pr=23927#step:3:2243). Is there something I can do in the build.zig to hide the stderr output when the test passes? Or should I remove my print statements? But, the dump-stack-trace functions are doing most of the logging (and I suspect its generally confusing to see stack traces in passing tests). Should I disable those? (The StackIterator testing covers the meat of the code, but it does seem like the complete routines should get some testing ...)

@alexrp

alexrp commented May 20, 2025

Copy link
Copy Markdown
Member

Is there something I can do in the build.zig to hide the stderr output when the test passes?

You can capture the output from the build script, e.g. by adding an "expected output" check. See for example #23892.

Comment threadlib/std/debug.zig Outdated
Comment threadtest/standalone/stack_iterator/unwind.zig Outdated
Comment threadtest/standalone/stack_iterator/unwind.zig Outdated
Comment threadtest/standalone/stack_iterator/unwind.zig Outdated
Comment threadtest/standalone/stack_iterator/unwind.zig Outdated
Comment threadtest/standalone/stack_iterator/unwind.zig Outdated
Comment threadtest/standalone/stack_iterator/unwind.zig Outdated
@rootbeer

Copy link
Copy Markdown
ContributorAuthor

@alexrp Thanks again for the reviews! One more question before I push a new version up: Should I make changes anywhere to get this test to compile/run against targets other than the default on CI?

@alexrp

Copy link
Copy Markdown
Member

I guess you could just change the test's build.zig to ignore the standard target option and instead build & run for a predefined set of targets? test/llvm_targets.zig is a good resource for a list of targets that are actually relevant/real (although some of them we obviously don't fully support yet).

@rootbeer
rootbeerforce-pushed the debug-mcontext-stage1 branch from 37ebc96 to 74336dfCompareMay 20, 2025 23:40
@rootbeer

Copy link
Copy Markdown
ContributorAuthor

You can capture the output from the build script, e.g. by adding an "expected output" check. See for example #23892.

Excellent! Done.

I guess you could just change the test's build.zig to ignore the standard target option and instead build & run for a predefined set of targets? test/llvm_targets.zig is a good resource for a list of targets that are actually relevant/real (although some of them we obviously don't fully support yet).

I'm going to postpone this for now. I don't want to disable the native testing, and don't really want to test a bunch of non-standard targets redundantly. I'll play around with this for future changes.

Two unrequested changes: (1) I got suspicious that the Darwin-aarch64 alignment hack-around in the test's signal handler was influencing other platforms, so I re-worked the code to more clearly do the hackery only when necessary.

@alexrp

alexrp commented May 21, 2025

Copy link
Copy Markdown
Member

Two unrequested changes: (1) I got suspicious that the Darwin-aarch64 alignment hack-around in the test's signal handler was influencing other platforms, so I re-worked the code to more clearly do the hackery only when necessary.

What's (2)? 👀 Also I'm not entirely sure which change you're referring to.

@rootbeer
rootbeerforce-pushed the debug-mcontext-stage1 branch from 74336df to 3447789CompareMay 21, 2025 16:17
Comment threadtest/standalone/stack_iterator/unwind.zig
@rootbeer

Copy link
Copy Markdown
ContributorAuthor

What's (2)? 👀 Also I'm not entirely sure which change you're referring to.

Oh lol ... I was planning on changing the verification steps in the test a bit, but undid that at the last moment. Didn't quite fix up the message here well enough.

So the only thing in this latest push that needs a quick review is the aarch64-darwin ctx-fixup code in the test's signal handler. (Though wait until the tests pass, of course ...)

@rootbeer
rootbeerforce-pushed the debug-mcontext-stage1 branch 2 times, most recently from 105abc5 to 679ac12CompareMay 21, 2025 19:27
@rootbeer
rootbeerforce-pushed the debug-mcontext-stage1 branch from 679ac12 to e965f5dCompareMay 21, 2025 21:47
@rootbeer

Copy link
Copy Markdown
ContributorAuthor

This is ready for a (hopefully final) review now. Happy to make any other fixes or changes, though.

Comment threadlib/std/debug.zig Outdated

depth += 1;
if (depth > max_stack_trace_depth) {
stderr.print("Abandoned stack trace after {} frames.\n", .{max_stack_trace_depth}) catch {};

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably print an extra line indicating that this is likely a Zig bug.

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed it to:

 // Likely a bug (or an exceptionally deep stack trace).
stderr.print("Abandoned stack trace after {} frames. See Zig Issue #18286.\n", .{max_stack_trace_depth}) catch {};

Let me know if you'd like different wording or a different issue (or no specific issue ...).

Comment threadlib/std/debug.zig Outdated
@alexrp
alexrp requested a review from jacobly0May 29, 2025 12:29
@rootbeer
rootbeerforce-pushed the debug-mcontext-stage1 branch 3 times, most recently from c911f5c to 6c30d3fCompareMay 30, 2025 06:10
This test creates three nested stack frames and then tests stack trace
creation. Add some additional tests of stack traces by invoking
"dumpCurrentStackTrace()" and by using a signal handler's "context"
parameter to feed backtrace construction.
Make the test case at least runnable on a wide variety of systems
(including Windows, and WASI). Because `ucontext_t` and `getcontext` are
not evenly supported everywhere, some systems are expected only get
through parts of the test.
@rootbeer
rootbeerforce-pushed the debug-mcontext-stage1 branch from 6c30d3f to e317a73CompareJune 11, 2025 00:05
@rootbeer
rootbeer marked this pull request as draft August 5, 2025 23:50
@alexrp

Copy link
Copy Markdown
Member

Have you tried experimenting with this directive? https://sourceware.org/binutils/docs/as/CFI-directives.html#g_t_002ecfi_005fsignal_005fframe

@alexrp

Copy link
Copy Markdown
Member

Also this definitely needs a rebase after #24960.

@rootbeer

Copy link
Copy Markdown
ContributorAuthor

I'll come back with a different set of patches after the current work in std.debug slows down (if any of these fixes are still necessary).

@rootbeerrootbeer closed this Sep 4, 2025
@alexrpalexrp removed their assignment Sep 4, 2025
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

@rootbeer@alexrp