Skip to content

Uncaught exception: a stack trace under the error, always - #840

Merged
ASDAlexander77 merged 2 commits into
mainfrom
uncaught-stack-trace
Nov 13, 2026
Merged

ASDAlexander77 merged 2 commits into
mainfrom
uncaught-stack-trace

Conversation

@ASDAlexander77

@ASDAlexander77 ASDAlexander77 commented Nov 13, 2026 •

Copy link
Copy Markdown
Owner

An uncaught exception now prints the frames it was thrown from under uncaught exception: .... It works from executables and from the JIT, on Windows and Linux. The trace is always printed; --di adds source lines where they are available.

error: uncaught exception: Failure
    at fail (...\throw_through_finally.ts:8)
    at inner (...\throw_through_finally.ts:13)
    at outer (...\throw_through_finally.ts:21)
    at main (...\throw_through_finally.ts:29)
Build Windows Linux
exe, no --di program.exe+0x126c per frame, plus "(compile with --di for function names and source lines)" function names, from the symbol table
exe, --di names and file:line, from the PDB through dbghelp names and file:line, through /usr/bin/addr2line when installed
JIT function names, no lines same

How it works

  • Shared code: a new header, TypeScript/UncaughtStackTrace.h, used by TypeScriptAsyncRuntime/UncaughtException.cpp and tslang/jit.cpp.
  • Walking the stack:
    • On Windows, RtlVirtualUnwind walks from the throw's context. It also covers JIT'd code, which registers its .pdata.
    • On Linux, _Unwind_Backtrace runs from the terminate handler.
  • dbghelp: loaded only when a trace is printed. Its symbol path is the executable's directory, so a failing program never waits on a symbol server.
  • finally on Windows: MSVC lowers finally to catch-all plus rethrow, so the exception that reaches the filter starts at the outermost finally. A vectored handler copies the context of each new throw (not of rethrows), and the trace walks from that first throw. Its frames are still on the stack, under the catch blocks. Linux needs none of this: there a finally is a cleanup, which the search for a handler passes.
  • Left out of the trace:
    • the throw itself: RaiseException, _CxxThrowException, __cxa_throw;
    • the C runtime's startup after main. On Windows, main is found by its address, so this works even when no frame has a name;
    • in the JIT, tslang's own frames.
  • JIT names: a JITEventListener records each JIT'd function's name, address and size.

x86 Windows (second commit)

  • The dialog: a 32-bit program used to end in abort()'s message box, because tslang/runtime/CMakeLists.txt, which builds the x86 runtime, never had UncaughtException.cpp. It now prints the error and a trace, like x64.
  • Description: an x86 throw's record has 3 parameters and absolute pointers, so describeUncaught now reads that form too.
  • Frame pointers: tslang now keeps the frame pointer in every function for 32-bit Windows targets (obj.cpp). LLVM omitted it even at -O0, which dropped callers from the trace. The x86 walk follows the EBP chain.
  • Names: dbghelp names are used only from a PDB that matches the image. A stale PDB from an earlier --di build gave wrong names.
  • Tests: x86 versions of the trace tests, registered under TSLANG_TEST_X86. With x86 tests on, 35 other x86 tests fail, and exactly the same 35 fail with the frame-pointer change turned off.

Known gaps

  • In-memory JIT: no source lines.
  • --jit-native: source lines come from its PDB, but a finally there still starts the trace at the rethrow, because the DLL has its own C runtime.
  • Optimized builds: inlined functions show up as their caller.

Tests

  • A new test program, uncaught/throw_through_finally.ts, run under gc, rc and own:
    • in the JIT, checking the function names;
    • as an executable without --di, checking the expected output for each platform;
    • as an executable with --di, checking file:line on Windows, and on Linux when addr2line is installed.
  • ctest: Windows 7073/7073, Linux (WSL) 7057/7057.
  • Size: an rc --opt executable grows by 5 KB (279,040 → 284,160 bytes).

🤖 Generated with Claude Code

ASDAlexander77 and others added 2 commits November 13, 2026 14:17
Under "uncaught exception: ...", the frames the exception was thrown from, from an
executable's runtime and from the JIT, on Windows and Linux
(TypeScript/UncaughtStackTrace.h). Not only with --di: the frames are walked only on the
way out of a failed program. --di adds what a frame can say:

- Windows exe: without --di there is no PDB, so a frame is `program.exe+0x126c` and a line
  says to compile with --di; with --di dbghelp (loaded only then) gives function and line.
- Linux exe: the symbol table names the functions; with --di addr2line, when installed,
  adds the line.
- JIT: a JITEventListener names the JIT'd functions (no lines).

On Windows a `finally` is a catch-all that throws again, so the exception that reaches the
filter starts where the outermost `finally` is. A vectored handler copies the context of
every throw that is not a rethrow, and the trace is walked from that first throw (the frames
are still on the stack, under the catch blocks). Linux needs nothing: a `finally` is a
cleanup the search passes, and std::terminate() runs where the throw found no catch.

Left out of the trace: the throw itself (RaiseException, _CxxThrowException, __cxa_throw),
the C runtime's startup after main (on Windows found by main's address, even with no names)
and, in the JIT, tslang's own frames.

Tests: throw_through_finally.ts under gc/rc/own - JIT names, and an executable with and
without --di. ctest: Windows 7073/7073, Linux 7057/7057. An rc --opt executable grows by
5 KB (279040 -> 284160).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…)'s dialog

A 32-bit program that threw what nothing caught ended in abort()'s message box, which an
unattended run waits on forever: tslang/runtime/CMakeLists.txt, which builds the x86
runtime, never had UncaughtException.cpp, so exe.cpp did not take the handler.

With the handler, x86 needed three more things:

- describeUncaught: an x86 throw's record has 3 parameters and its ThrowInfo holds
  addresses (a base of 0), not 4 with image-relative references - the description was empty.
- tslang keeps the frame pointer in every function for 32-bit Windows (obj.cpp): LLVM
  omitted it even at -O0, and the caller of such a function was missing from the trace.
  The trace walks the chain of frame pointers; StackWalk64 lost the same frames.
- names from dbghelp only from a PDB that matches the image: a --di build's PDB beside a
  later build without --di, under the same name, named frames after unrelated functions.

Tests: x86 twins of the trace tests (TSLANG_TEST_X86), gc/rc/own with and without --di.
ctest with TSLANG_TEST_X86: the same 35 x86 failures with and without the frame pointers
(@cpp classes on i686, x86 link symbols, 32-bit u64 bugs), none from this change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ASDAlexander77
ASDAlexander77 merged commit 5d9b590 into main Nov 13, 2026
8 checks passed
@ASDAlexander77
ASDAlexander77 deleted the uncaught-stack-trace branch November 13, 2026 15:17
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.

1 participant