Skip to content

Win64 libunwind replacement for stack backtrace. - #1277

Draft
jaykrell wants to merge 1 commit into
modula3:masterfrom
jaykrell:one-source
Draft

jaykrell wants to merge 1 commit into
modula3:masterfrom
jaykrell:one-source

Conversation

@jaykrell

Copy link
Copy Markdown
Contributor

No description provided.

@jaykrell
jaykrell force-pushed the one-source branch 6 times, most recently from e2153e6 to 3efb5b2 Compare November 21, 2026 10:22
@hosking

hosking commented Nov 21, 2026

Copy link
Copy Markdown
Member

Thanks, Jay. Folding the three near-identical RTStackC.c files into one is a good idea. This overlaps heavily with #1271, though: both move _M3Exc/RTStack__ThrowM3Exc out of the per-target files, both let other C-backend targets use ex_stack, and both touch the same files. I'd like to combine the two rather than pick one:

  • From this PR: the single libunwind walker in common/ under #if M3_LIBUNWIND, replacing the per-target RTStackC.c files and directories, with stubs when there's no libunwind.
  • From m3core: make ex_stack target-independent #1271:
    • Selection that follows the compiler. ex_stack is used when M3_USE_STACK_WALKER is set and M3_BACKEND_MODE = "C". That's the setting Builder.m3 reads for Target.Has_stack_walker, so the runtime always matches the code the front end generates.
    • Raising separate from walking. RTStack__ThrowM3Exc lives in ex_stack/ and is all a raise needs. The walker is only for stack dumps, and PrintStack prints "no stack dump" when Has_walker() is FALSE.
    • The README-ex_stack.md update.
  • Ideally, one list of walker platforms. Today the #if decides in C, and quake separately decides whether to link UNWIND. If quake made the choice once and passed M3_LIBUNWIND to the C compile, the two couldn't drift apart.

Why I don't think this PR can stand alone as written:

  1. The runtime and the compiler would disagree on the EH scheme. The new else if equal (M3_BACKEND_MODE, "C") branch picks ex_stack for C-backend targets that don't set M3_USE_STACK_WALKER. The compiler leaves Target.Has_stack_walker FALSE for them, so their TRYs compile to ex_frame code (TryStmt.Compile2), while m3core raises with a C++ throw that nothing catches.
  2. The walker targets look like they lose their build. Their branch still does include_dir(TARGET), but those directories now have m3makefile-old and #error in RTStackC.c. With the common file serving them, that branch only needs import_sys_lib("UNWIND").

(This version also spells M3_HOST_AMD64_LINUX correctly, which fixes the #1276 typo.)

I'm happy to do the combining: I'd rework #1271 on top of your branch, or open a fresh PR that includes your commits, keeping your authorship. Or you can fold the #1271 pieces in here, whichever you prefer.

@jaykrell

Copy link
Copy Markdown
Contributor Author

@hosking yes I can see, my PR is both very good, and heavily stomps on your work.
That's why I'm sitting here with it uncommited, waiting and wondering and letting us slowly talk it over, etc., and working through using your PR to use stackwalking on Windows.
It is going well. And my PR here isn't critical. :)

Comment thread m3-libs/m3core/src/runtime/m3makefile Outdated
end
else
include_dir("ex_frame")
else if equal (M3_BACKEND_MODE, "C")

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@hosking Right this part was very speculative of me.
You had the hint that we "need" to set M3_USE_STACK_WALKER=TRUE AND C++ backend. Or at least, m3middle/m3front need to know the equivalence.
Furthermore, what you have commited, while a little messy, works.
What I had here in the PR, was cleanup, but completely missed the goal of actually moving to stack walk.

Ideally, one list of walker platforms

This is a nit I was going to ignore, but yes agreed.

So in temporary conclusion:

  • Take your PR.
  • This part of my PR I'll revert.
  • I'll rebase, whatever, no big deal.
  • And I'll work through actually getting ex_stack working for me on AMD64_NT.
  • I am not sure how "good" my efforts will be, because I'm using my preferred boot1.py. The two step bootstrap isn't because old compiler cannot build new stuff, but because there needs to be one runtime chosen through compiler and what runtime it uses.
  • Um, I am not sure what people or CI are practised in. I am practised in boot1.py.
  • We will have to work this out in CI I think. Perhaps it already works though.

@jaykrell
jaykrell force-pushed the one-source branch 2 times, most recently from 6cb41e2 to 3726627 Compare November 22, 2026 11:53
@hosking

hosking commented Nov 22, 2026

Copy link
Copy Markdown
Member

Thanks Jay — folding the three libunwind files into one is right; they had drifted only in comments. And a libunwind-shaped shim over RtlVirtualUnwind is a nice way to bring Windows in. CI is red across the board, though, for a few separate reasons:

  1. Darwin (both): runtime/m3makefile:70 still does include_dir (TARGET) under M3CORE_TARGET_WALKER, but the per-target m3makefiles are renamed to m3makefile-old, so quake can't open them. I think the per-target directories (RTMachine.i3, RTStackC.c, m3makefile) can simply be deleted, rather than left with a bare "no longer used" line or an #error.
  2. I386_LINUX and other non-walker targets: common/RTStackCommon.c now defines the stub RTStack__* routines, and no_walker/RTStackC.c still does too, hence the duplicate definitions. One home for the stubs, please; no_walker seems the natural one.
  3. AMD64_LINUX bootstrap: RTStackCommon.c is always compiled, so <libunwind.h> is now included even in ex_frame and bootstrap builds where libunwind isn't installed. The include should stay behind the same condition that links UNWIND.
  4. I386_NT: 32-bit x86 Windows has no table-based unwind (RtlLookupFunctionEntry, RtlVirtualUnwind and UNWIND_HISTORY_TABLE exist only on x64 and ARM64), so _WIN32 is the wrong guard.

Not yet visible in CI:

  • FrameLock is 0x1234567890 stored in an INTEGER. On a 32-bit target it's truncated, so the lock != FrameLock checks would always abort.
  • On Windows, RTStack__Unwind ends up defined on neither path, while Has_walker returns TRUE. RTHeapStats/RTAllocStats would then start walking. I'd keep Has_walker FALSE on Windows until the walker is complete.
  • The stub PrevFrame zeroes callee (READONLY) rather than caller (the OUT parameter).
  • unwind-test.c (with a main) probably belongs outside m3core's source directory.

Would you consider splitting it in two? (1) the pure consolidation, which should be quick to get green, and (2) the Windows walker for AMD64_NT/ARM64_NT on top of it. That way the first can land while the second gets its own testing.

@jaykrell
jaykrell force-pushed the one-source branch 4 times, most recently from 8f04465 to 4c57d30 Compare November 22, 2026 13:09
@jaykrell jaykrell changed the title RTStack consolidation. RTStack consolidation (draft). Nov 22, 2026
@jaykrell

Copy link
Copy Markdown
Contributor Author

I split it up.
I commited most, but left the final step for approval.
This PR remains draft.

@VictorMiasnikov

Copy link
Copy Markdown
Contributor

= =
{3AA9EEAE-0A59-49E7-96A1-7786417B7B7E}
= =

@jaykrell jaykrell changed the title RTStack consolidation (draft). Win64 libunwind replacement for stack backtrace. Nov 22, 2026
@jaykrell
jaykrell force-pushed the one-source branch 8 times, most recently from f1c473b to 9692385 Compare November 22, 2026 19:15
@hosking

hosking commented Nov 23, 2026

Copy link
Copy Markdown
Member

Jay, the Darwin failures here are just the include in walker/RTStackC.c: #include "m3unwind" should be #include "m3unwind.h" (fatal error: 'm3unwind' file not found). With that fixed, CI should show whether the shim leaves Darwin and Linux backtraces unchanged. Happy to review once it's green. Note that #1355 also touches walker/RTStackC.c, but only a comment, so the two won't conflict.

🤖 Generated with Claude Code

@@ -0,0 +1,59 @@
/* A quick libunwind abstraction and Windows implementation to satisfy Modula-3.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This should probably be its own little github repository.
With a good license.
And possibly consumed as a submodule.
Or we should see if Cygwin's libunwind works and is well licensed.

This branch has not been deployed

No deployments
Sign up for free to 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.

3 participants