aboutsummaryrefslogtreecommitdiff
path: root/lib/std
AgeCommit message (Collapse)Author
2025-09-30std: rework/remove ucontext_tmlugg
Our usage of `ucontext_t` in the standard library was kind of problematic. We unnecessarily mimiced libc-specific structures, and our `getcontext` implementation was overkill for our use case of stack tracing. This commit introduces a new namespace, `std.debug.cpu_context`, which contains "context" types for various architectures (currently x86, x86_64, ARM, and AARCH64) containing the general-purpose CPU registers; the ones needed in practice for stack unwinding. Each implementation has a function `current` which populates the structure using inline assembly. The structure is user-overrideable, though that should only be necessary if the standard library does not have an implementation for the *architecture*: that is to say, none of this is OS-dependent. Of course, in POSIX signal handlers, we get a `ucontext_t` from the kernel. The function `std.debug.cpu_context.fromPosixSignalContext` converts this to a `std.debug.cpu_context.Native` with a big ol' target switch. This functionality is not exposed from `std.c` or `std.posix`, and neither are `ucontext_t`, `mcontext_t`, or `getcontext`. The rationale is that these types and functions do not conform to a specific ABI, and in fact tend to get updated over time based on CPU features and extensions; in addition, different libcs use different structures which are "partially compatible" with the kernel structure. Overall, it's a mess, but all we need is the kernel context, so we can just define a kernel-compatible structure as long as we don't claim C compatibility by putting it in `std.c` or `std.posix`. This change resulted in a few nice `std.debug` simplifications, but nothing too noteworthy. However, the main benefit of this change is that DWARF unwinding---sometimes necessary for collecting stack traces reliably---now requires far less target-specific integration. Also fix a bug I noticed in `PageAllocator` (I found this due to a bug in my distro's QEMU distribution; thanks, broken QEMU patch!) and I think a couple of minor bugs in `std.debug`. Resolves: #23801 Resolves: #23802
2025-09-30std.start: also don't print error trace targeting `.other`mlugg
This only matters if `callMain` is called by a user, since `std.start` will never itself call `callMain` when `target.os.tag == .other`. However, it *is* a valid use case for a user to call `std.start.callMain` in their own startup logic, so this makes sense.
2025-09-30std.debug.Pdb: fix leakmlugg
2025-09-30fix compile errors and minor bugsmlugg
2025-09-30std.debug: don't attempt SelfInfo unwinding when unsupportedmlugg
2025-09-30SelfInfo: remove x86-windows unwinding pathmlugg
Turns out that RtlCaptureStackBackTrace is actually just doing FP (ebp) unwinding under the hood, making this logic completely redundant with our own FP-walking implementation; see added comment for details.
2025-09-30tests: split up and enhance stack trace testsmlugg
Previously, the `test-stack-traces` step was essentially just testing error traces, and even there we didn't have much coverage. This commit solves that by splitting the "stack trace" tests into two separate harnesses: the "stack trace" tests are for actual stack traces (i.e. involving stack unwinding), while the "error trace" tests are specifically for error return traces. The "stack trace" tests will test different configurations of: * `-lc` * `-fPIE` * `-fomit-frame-pointer` * `-fllvm` * unwind tables (currently disabled) * strip debug info (currently disabled) The main goal there is to test *stack unwinding* under different conditions. Meanwhile, the "error trace" tests will test different configurations of `-O` and `-fllvm`; the main goal here, aside from checking that error traces themselves do not miscompile, is to check whether debug info is still working even in optimized builds. Of course, aggressive optimizations *can* thwart debug info no matter what, so as before, there is a way to disable cases for specific targets / optimize modes. The program which converts stack traces into a more validatable format by removing things like addresses (previously `check-stack-trace.zig`, now `convert-stack-trace.zig`) has been rewritten and simplified. Also, thanks to various fixes in this branch, several workarounds have become unnecessary: for instance, we don't need to ignore the function name printed in stack traces in release modes, because `std.debug.Dwarf` now uses the correct DIE for inlined functions! Neither `test-stack-traces` nor `test-error-traces` does general foreign architecture testing, because it seems that (at least for now) external executors often aren't particularly good at handling stack tracing correctly (looking at you, Wine). Generally, they just test the native target (this matches the old behavior of `test-stack-traces`). However, there is one exception: when on an x86_64 or aarch64 host, we will also test the 32-bit version (x86 or arm) if the OS supports it, because such executables can be trivially tested without an external executor. Oh, also, I wrote a bunch of stack trace tests. Previously there was, erm, *one* test in `test-stack-traces` which wasn't for error traces. Now there are a good few!
2025-09-30std.debug.Dwarf: fix names of inlined functionsmlugg
2025-09-30std: fix os.linux.x86.syscall6mlugg
It was possible for `arg6` to be passed as an operand relative to esp. In that case, the `push` at the top clobbered esp and hence made the reference to arg6 invalid. This was manifesting in this branch as broken stack traces on x86-linux due to an `mmap2` syscall accidentally passing the page offset as non-zero! This commit fixes a bug introduced in cb0e6d8aa.
2025-09-30std: fix 32-bit build and some unsafe castsmlugg
2025-09-30fix bad mergemlugg
The API of `std.debug.Pdb` changed.
2025-09-30std.debug: never attempt FP unwind under fomit-frame-pointermlugg
2025-09-30link.Elf: fix static PIEmlugg
We mustn't emit the DT_PLTGOT entry in `.dynamic` in a statically-linked PIE, because there's no dl to relocate it (and `std.pie.relocate`, or the PIE relocator in libc, won't touch it). In that case, there cannot be any PLT entries, so there's no point emitting the `.got.plt` section at all. If we just don't create that section, `link.Elf` already knows not to add the DT_PLTGOT entry to `.dynamic`. Co-authored-by: Jacob Young <jacobly0@users.noreply.github.com>
2025-09-30Dwarf: use 'gpa' terminologymlugg
2025-09-30compiler: fix new panic handler in release buildsmlugg
2025-09-30std: replace debug.Dwarf.ElfModule with debug.ElfFilemlugg
This abstraction isn't really tied to DWARF at all! Really, we're just loading some information from an ELF file which is useful for debugging. That *includes* DWARF, but it also includes other information. For instance, the other change here: Now, if DWARF information is missing, `debug.SelfInfo.ElfModule` will name symbols by finding a matching symtab entry. We actually already do this on Mach-O, so it makes obvious sense to do the same on ELF! This change is what motivated the restructuring to begin with. The symtab work is derived from #22077. Co-authored-by: geemili <opensource@geemili.xyz>
2025-09-30std.debug: don't include dumpCurrentStackTrace framemlugg
If it's not given, we should set `first_address` to the return address of `dumpCurrentStackTrace` to avoid the call to `writeCurrentStackTrace` appearing in the trace. However, we must only do that if no `context` is given; if there's a context then we're starting the stack unwind elsewhere.
2025-09-30small reasonable changemlugg
2025-09-30std.debug: try removing a probably-redundant conditionmlugg
2025-09-30std.debug: handle ThreadContext slightly bettermlugg
It's now user-overrideable, and uses `noreturn` types to neatly stop analysis.
2025-09-30std.debug: unwinding on Windowsmlugg
...using `RtlVirtualUnwind` on x86_64 and aarch64, and `RtaCaptureStackBackTrace` on x86.
2025-09-30std: fix debug.Info and debug.Coveragemlugg
2025-09-30std: doc comments and tweaksmlugg
2025-09-30fix compiler ftbfs from std.macho and std.dwarf changesmlugg
2025-09-30std: fixesmlugg
2025-09-30SelfInfo.DarwinModule: rename fieldmlugg
2025-09-30SelfInfo: be honest about how general unwinding ismlugg
...in that it isn't: it's currently very specialized to DWARF unwinding. Also, make a type unmanaged.
2025-09-30add freestanding support IN THEORYmlugg
untested because this branch has errors rn
2025-09-30replace usages of old std.debug APIsmlugg
src/crash_handler.zig is still TODO though, i am planning bigger changes there
2025-09-30std.debug: improve the APIs and stuffmlugg
2025-09-30tweaksmlugg
2025-09-30std.posix: mark getcontext as unsupported by defaultmlugg
2025-09-30remove redundant testmlugg
turns out this actually has coverage in std.debug
2025-09-30SelfInfo: load eh_frame/debug_frame from ELF file if eh_frame_hdr omittedmlugg
2025-09-30std.debug.SelfInfo: concrete error setsmlugg
The downside of this commit is that more precise errors are no longer propagated up. However, these errors were pretty useless in isolation due to them having no context; and regardless, we intentionally swallow most of them in `std.debug` anyway. Therefore, this is better in practice, because it allows `std.debug` to give slightly more useful warnings when handling errors. This commit does that for unwind errors, for instance, which differentiate between the unwind info being corrupt vs missing vs inaccessible vs unsupported. A better solution would be to also include more detailed information via the diagnostics pattern, but this commit is an incremental improvement.
2025-09-30doc commentsmlugg
2025-09-30fix aarch64-macos DWARF unwindingmlugg
turns out this isn't technically specific to that target at all; other targets just don't emit mid-function 'ret' instructions as much so certain CFI instruction patterns were only seen on aarch64. thanks to jacob for finding the bug <3
2025-09-30stash? more like nomlugg
2025-09-30SelfInfo deinit magicmlugg
2025-09-30split SelfInfo into a file per implmlugg
2025-09-30std.debug.Dwarf: eliminate host pointer size dependencymlugg
2025-09-30remove TODOs which are done or which i'm not actually gonna do lolmlugg
2025-09-30dont dupe state you silly billymlugg
2025-09-30the world if Dwarf.ElfModule was like REALLY good:mlugg
2025-09-30the world if ElfModule didn't suck:mlugg
2025-09-30me when i did a thingmlugg
2025-09-30less hacky :Dmlugg
2025-09-30namesmlugg
2025-09-30sky pirates! which are even better!mlugg
2025-09-30yet moremlugg