0
Fork 0
mirror of https://codeberg.org/ziglang/zig synced 2026-11-18 07:05:05 +00:00
zig/lib/std/debug.zig

1951 lines
78 KiB

2019-05-26 13:17:34 -04:00
2019-05-26 13:17:34 -04:00
2016-08-17 20:11:04 -07:00
std: rework/remove ucontext_t 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-17 18:38:11 +01:00
2026-04-13 00:43:34 -07:00
2025-10-08 20:35:34 -07:00
2025-09-02 15:54:36 +01:00
2025-09-17 23:30:04 +01:00
2025-12-17 15:47:33 -08:00
2025-12-17 15:47:33 -08:00
2025-09-08 14:31:45 +01:00
2025-12-09 18:44:39 -08:00
2025-12-17 15:47:33 -08:00
2025-12-09 18:44:39 -08:00
2025-12-17 15:47:33 -08:00
2025-12-17 17:00:41 -08:00
2025-12-17 17:00:41 -08:00
2025-12-17 15:47:33 -08:00
2025-12-09 18:44:39 -08:00
2025-12-09 18:44:39 -08:00
2025-12-17 15:47:33 -08:00
2025-12-09 18:44:39 -08:00
2025-12-09 18:44:39 -08:00
2025-09-01 16:50:39 +01:00
2025-09-01 16:50:39 +01:00
2025-12-17 17:00:41 -08:00
2025-12-17 15:47:33 -08:00
2025-12-17 15:47:33 -08:00
2026-07-06 12:52:19 +02:00
2025-12-17 15:47:33 -08:00
2025-12-17 15:47:33 -08:00
2025-12-17 15:47:33 -08:00
2025-12-17 15:47:33 -08:00
2025-12-17 15:47:33 -08:00
2025-12-17 15:47:33 -08:00
2025-12-17 15:47:33 -08:00
2025-12-17 15:47:33 -08:00
2025-12-17 15:47:33 -08:00
std: rework/remove ucontext_t 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-17 18:38:11 +01:00
2025-07-18 10:42:04 -07:00
2025-07-18 10:42:04 -07:00
2025-09-08 14:31:45 +01:00
2026-08-07 15:16:01 +08:00
2025-12-21 17:06:02 -08:00
2026-08-26 10:02:45 +03:00
std.os.uefi.tables: ziggify boot and runtime services (#23441) * std.os.uefi.tables: ziggify boot and runtime services * avoid T{} syntax Co-authored-by: linusg <mail@linusgroh.de> * misc fixes * work * self-review quickfixes * dont make MemoryMapSlice generic * more review fixes, work * more work * more work * review fixes * update boot/runtime services references throughout codebase * self-review fixes * couple of fixes i forgot to commit earlier * fixes from integrating in my own project * fixes from refAllDeclsRecursive * Apply suggestions from code review Co-authored-by: truemedian <truemedian@gmail.com> * more fixes from review * fixes from project integration * make natural alignment of Guid align-8 * EventRegistration is a new opaque type * fix getNextHighMonotonicCount * fix locateProtocol * fix exit * partly revert 7372d65 * oops exit data_len is num of bytes * fixes from project integration * MapInfo consistency, MemoryType update per review * turn EventRegistration back into a pointer * forgot to finish updating MemoryType methods * fix IntFittingRange calls * set uefi.Page nat alignment * Back out "set uefi.Page nat alignment" This backs out commit cdd9bd6f7f5fb763f994b8fbe3e1a1c2996a2393. * get rid of some error.NotFound-s * fix .exit call in panic * review comments, add format method * fix resetSystem data alignment * oops, didnt do a final refAllDeclsRecursive i guess * review comments * writergate update MemoryType.format * fix rename --------- Co-authored-by: linusg <mail@linusgroh.de> Co-authored-by: truemedian <truemedian@gmail.com>
2025-07-12 10:18:53 -07:00
2025-12-17 17:00:41 -08:00
2025-12-17 15:47:33 -08:00
2025-12-17 15:47:33 -08:00
2025-12-17 15:47:33 -08:00
2025-12-17 15:47:33 -08:00
2025-12-17 15:47:33 -08:00
2025-12-17 15:47:33 -08:00
2025-12-17 15:47:33 -08:00
2025-12-17 17:00:41 -08:00
2025-12-17 15:47:33 -08:00
std: rework/remove ucontext_t 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-17 18:38:11 +01:00
2025-12-17 15:47:33 -08:00
2026-04-13 00:43:34 -07:00
2025-12-17 15:47:33 -08:00
2025-12-17 15:47:33 -08:00
2025-12-17 15:47:33 -08:00
2025-12-17 15:47:33 -08:00
2025-12-17 15:47:33 -08:00
2025-12-17 15:47:33 -08:00
2025-10-08 20:35:34 -07:00
2025-12-17 15:47:33 -08:00
2025-12-17 15:47:33 -08:00
2025-12-17 15:47:33 -08:00
2025-12-17 15:47:33 -08:00
2025-12-17 15:47:33 -08:00
2025-12-17 15:47:33 -08:00
2026-04-13 00:43:34 -07:00
2025-12-17 17:00:41 -08:00
2025-12-17 15:47:33 -08:00
2025-12-17 15:47:33 -08:00
2025-12-17 15:47:33 -08:00
2025-12-17 15:47:33 -08:00
2026-04-13 00:43:34 -07:00
2025-12-17 15:47:33 -08:00
2025-12-17 15:47:33 -08:00
2025-12-17 15:47:33 -08:00
2025-12-17 15:47:33 -08:00
2025-12-17 15:47:33 -08:00
2026-04-13 00:43:34 -07:00
2025-12-17 17:00:41 -08:00
2025-12-17 15:47:33 -08:00
2018-12-02 18:54:04 -05:00
std: rework/remove ucontext_t 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-17 18:38:11 +01:00
std: rework/remove ucontext_t 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-17 18:38:11 +01:00
std: rework/remove ucontext_t 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-17 18:38:11 +01:00
2018-12-02 18:54:04 -05:00
2018-07-29 23:25:40 -04:00
2018-12-02 18:54:04 -05:00
std.debug: Support FP unwinding on `thumb-*` targets (#37482) When compiling Arm Thumb code, GCC and LLVM ([including zig](https://godbolt.org/z/sfxd37P3x)) use R7 as the frame pointer (except when `-fomit-frame-pointer` is specified, of course) rather than R11 which is used in 32-bit mode. This is mainly due to Thumb1's `PUSH`/`POP` instructions only supporting R0-R7 and R14/R15. Armv7-M does have Thumb2 `PUSH.W`/`POP.W` which can do it, but LLVM still uses R7, even if you explicitly use e.g. `-mcpu cortex_m3`. Note that AAPCS32 doesn't mention using R7 as FP in Thumb mode at all, but it also only barely mentions using R11 as FP: "In some variants r11 (FP) may be used as a frame pointer in order to chain frame activation records into a linked list." `std.debug.cpu_context.Arm` currently always assumes R11 is the frame pointer for both `arm` and `thumb` targets, which means FP unwinding simply doesn't work on thumb targets, including most arm microcontrollers. It would be nice to have stack tracing available through the FP on these constrained platforms since you often don't have enough ROM space to waste on the `.debug_frame` section, and [`.ARM.exidx`/`.ARM.extab` unwinding](https://codeberg.org/ziglang/zig/issues/30931) isn't implemented yet. On some platforms, it's possible to have both thumb and arm functions interleaved within a single call stack. In these cases, [compilers use R11 for the FP in arm functions, and R7 in thumb functions](https://godbolt.org/z/EsaYW8rhh), so neither blindly using R11 nor R7 will guarantee a correct stack unwinding in the general case. The most reliable way to determine which register is the FP for the currently executing stack frame would be by reading `CPSR` using the `MRS` instruction and checking the `T` bit, but since zig currently doesn't have an equivalent to `__attribute__((target("thumb")))` that can be applied on a per-function basis, the thumb-ness of all non-extern functions will always match `@import("builtin").target.arch.isThumb()`. For deeper stack frames, it doesn't matter what register the FP was stored in, since when we unwind we're getting it from the stack instead. As long as all functions were compiled with `-fno-omit-frame-pointer`, the FP will always have been pushed immediately after the LR. So the first change is to have `std.debug.cpu_context.Arm.getFp()` use R7 when `native_arch` is thumb. The current inline assembly for grabbing the CPU context mentions only using `STM` for R0-R12 for thumb compatibility, but it still uses several 32-bit Thumb2 instructions that don't exist on architectures like Cortex M0/M0+. This PR adds a slightly more verbose alternative version that only uses 16-bit instructions if the target doesn't support Thumb2. I thought about skipping R8-R12 entirely on targets that only support Thumb1, since it's highly unlikely that they'll actually be used for anything, but for now they are still being captured. With these two changes, and appropriate `debug` declarations in the root module to enable symbol lookup using a custom minified string table tacked onto the end of the firmware blob, I can get stack traces on an RP2040 microcontroller (Cortex M0+): ``` src/main.zig:135:37: 0x10000b5a in dumpCurrentStack (firmware) src/main.zig:131:5: 0x10000b50 in iAmAFunctionYesIAm (firmware) src/main.zig:100:21: 0x10000a7c in main (firmware) $rpi/src/rp2040/boot.zig:112:16: 0x10000310 in _start (firmware) $rpi/src/rp2040/boot.zig:31:11: 0x100002b6 in _boot3 (firmware) ``` But there's still one more problem: all of the reported addresses point to the instruction immediately following the `BL`/`BLX` instruction that we would expect. For example, here's the disassembly for the top two functions: ``` main.iAmAFunctionYesIAm: 10000b48 80b5 push {r7, lr} 10000b4a 00af add r7, sp, #0 10000b4c 00f001f8 bl #main.dumpCurrentStack 10000b50 80bd pop {r7, pc} main.dumpCurrentStack: 10000b52 80b5 push {r7, lr} 10000b54 00af add r7, sp, #0 10000b56 00f001f8 bl #debug.writeCurrentStackTrace 10000b5a 80bd pop {r7, pc} ``` In this case, none of the resolved symbols or source locations are affected by the incorrect instruction addresses, but if the next instruction after the subroutine happened to be an inlined function, they could be erroneous. We need to make sure that the thumb bit is cleared so that when `std.debug.StackIterator` subtracts 1 from the return address, it ends up in the previous call instruction. I put this in `std.debug.stripInstructionPtrAuthCode` and renamed it to `std.debug.stripInstructionPtrMarkerBits` since I believe everywhere that's currently used should have the thumb bit stripped as well (or at least it shouldn't harm anything to do so). With all of the changes, my RP2040 test now looks correct: ``` src/main.zig:135:37: 0x10000b59 in dumpCurrentStack (firmware) src/main.zig:131:5: 0x10000b4f in iAmAFunctionYesIAm (firmware) src/main.zig:100:21: 0x10000a7b in main (firmware) $rpi/src/rp2040/boot.zig:112:16: 0x1000030f in _start (firmware) $rpi/src/rp2040/boot.zig:31:11: 0x100002b5 in _boot3 (firmware) ``` Reviewed-on: https://codeberg.org/ziglang/zig/pulls/37482 Reviewed-by: Alex Rønne Petersen <alex@alexrp.com>
2026-10-31 12:49:51 +01:00
2025-09-13 10:29:20 +01:00
2018-12-02 18:54:04 -05:00
2018-07-29 23:25:40 -04:00
std.debug: Support FP unwinding on `thumb-*` targets (#37482) When compiling Arm Thumb code, GCC and LLVM ([including zig](https://godbolt.org/z/sfxd37P3x)) use R7 as the frame pointer (except when `-fomit-frame-pointer` is specified, of course) rather than R11 which is used in 32-bit mode. This is mainly due to Thumb1's `PUSH`/`POP` instructions only supporting R0-R7 and R14/R15. Armv7-M does have Thumb2 `PUSH.W`/`POP.W` which can do it, but LLVM still uses R7, even if you explicitly use e.g. `-mcpu cortex_m3`. Note that AAPCS32 doesn't mention using R7 as FP in Thumb mode at all, but it also only barely mentions using R11 as FP: "In some variants r11 (FP) may be used as a frame pointer in order to chain frame activation records into a linked list." `std.debug.cpu_context.Arm` currently always assumes R11 is the frame pointer for both `arm` and `thumb` targets, which means FP unwinding simply doesn't work on thumb targets, including most arm microcontrollers. It would be nice to have stack tracing available through the FP on these constrained platforms since you often don't have enough ROM space to waste on the `.debug_frame` section, and [`.ARM.exidx`/`.ARM.extab` unwinding](https://codeberg.org/ziglang/zig/issues/30931) isn't implemented yet. On some platforms, it's possible to have both thumb and arm functions interleaved within a single call stack. In these cases, [compilers use R11 for the FP in arm functions, and R7 in thumb functions](https://godbolt.org/z/EsaYW8rhh), so neither blindly using R11 nor R7 will guarantee a correct stack unwinding in the general case. The most reliable way to determine which register is the FP for the currently executing stack frame would be by reading `CPSR` using the `MRS` instruction and checking the `T` bit, but since zig currently doesn't have an equivalent to `__attribute__((target("thumb")))` that can be applied on a per-function basis, the thumb-ness of all non-extern functions will always match `@import("builtin").target.arch.isThumb()`. For deeper stack frames, it doesn't matter what register the FP was stored in, since when we unwind we're getting it from the stack instead. As long as all functions were compiled with `-fno-omit-frame-pointer`, the FP will always have been pushed immediately after the LR. So the first change is to have `std.debug.cpu_context.Arm.getFp()` use R7 when `native_arch` is thumb. The current inline assembly for grabbing the CPU context mentions only using `STM` for R0-R12 for thumb compatibility, but it still uses several 32-bit Thumb2 instructions that don't exist on architectures like Cortex M0/M0+. This PR adds a slightly more verbose alternative version that only uses 16-bit instructions if the target doesn't support Thumb2. I thought about skipping R8-R12 entirely on targets that only support Thumb1, since it's highly unlikely that they'll actually be used for anything, but for now they are still being captured. With these two changes, and appropriate `debug` declarations in the root module to enable symbol lookup using a custom minified string table tacked onto the end of the firmware blob, I can get stack traces on an RP2040 microcontroller (Cortex M0+): ``` src/main.zig:135:37: 0x10000b5a in dumpCurrentStack (firmware) src/main.zig:131:5: 0x10000b50 in iAmAFunctionYesIAm (firmware) src/main.zig:100:21: 0x10000a7c in main (firmware) $rpi/src/rp2040/boot.zig:112:16: 0x10000310 in _start (firmware) $rpi/src/rp2040/boot.zig:31:11: 0x100002b6 in _boot3 (firmware) ``` But there's still one more problem: all of the reported addresses point to the instruction immediately following the `BL`/`BLX` instruction that we would expect. For example, here's the disassembly for the top two functions: ``` main.iAmAFunctionYesIAm: 10000b48 80b5 push {r7, lr} 10000b4a 00af add r7, sp, #0 10000b4c 00f001f8 bl #main.dumpCurrentStack 10000b50 80bd pop {r7, pc} main.dumpCurrentStack: 10000b52 80b5 push {r7, lr} 10000b54 00af add r7, sp, #0 10000b56 00f001f8 bl #debug.writeCurrentStackTrace 10000b5a 80bd pop {r7, pc} ``` In this case, none of the resolved symbols or source locations are affected by the incorrect instruction addresses, but if the next instruction after the subroutine happened to be an inlined function, they could be erroneous. We need to make sure that the thumb bit is cleared so that when `std.debug.StackIterator` subtracts 1 from the return address, it ends up in the previous call instruction. I put this in `std.debug.stripInstructionPtrAuthCode` and renamed it to `std.debug.stripInstructionPtrMarkerBits` since I believe everywhere that's currently used should have the thumb bit stripped as well (or at least it shouldn't harm anything to do so). With all of the changes, my RP2040 test now looks correct: ``` src/main.zig:135:37: 0x10000b59 in dumpCurrentStack (firmware) src/main.zig:131:5: 0x10000b4f in iAmAFunctionYesIAm (firmware) src/main.zig:100:21: 0x10000a7b in main (firmware) $rpi/src/rp2040/boot.zig:112:16: 0x1000030f in _start (firmware) $rpi/src/rp2040/boot.zig:31:11: 0x100002b5 in _boot3 (firmware) ``` Reviewed-on: https://codeberg.org/ziglang/zig/pulls/37482 Reviewed-by: Alex Rønne Petersen <alex@alexrp.com>
2026-10-31 12:49:51 +01:00
std.debug: Support FP unwinding on `thumb-*` targets (#37482) When compiling Arm Thumb code, GCC and LLVM ([including zig](https://godbolt.org/z/sfxd37P3x)) use R7 as the frame pointer (except when `-fomit-frame-pointer` is specified, of course) rather than R11 which is used in 32-bit mode. This is mainly due to Thumb1's `PUSH`/`POP` instructions only supporting R0-R7 and R14/R15. Armv7-M does have Thumb2 `PUSH.W`/`POP.W` which can do it, but LLVM still uses R7, even if you explicitly use e.g. `-mcpu cortex_m3`. Note that AAPCS32 doesn't mention using R7 as FP in Thumb mode at all, but it also only barely mentions using R11 as FP: "In some variants r11 (FP) may be used as a frame pointer in order to chain frame activation records into a linked list." `std.debug.cpu_context.Arm` currently always assumes R11 is the frame pointer for both `arm` and `thumb` targets, which means FP unwinding simply doesn't work on thumb targets, including most arm microcontrollers. It would be nice to have stack tracing available through the FP on these constrained platforms since you often don't have enough ROM space to waste on the `.debug_frame` section, and [`.ARM.exidx`/`.ARM.extab` unwinding](https://codeberg.org/ziglang/zig/issues/30931) isn't implemented yet. On some platforms, it's possible to have both thumb and arm functions interleaved within a single call stack. In these cases, [compilers use R11 for the FP in arm functions, and R7 in thumb functions](https://godbolt.org/z/EsaYW8rhh), so neither blindly using R11 nor R7 will guarantee a correct stack unwinding in the general case. The most reliable way to determine which register is the FP for the currently executing stack frame would be by reading `CPSR` using the `MRS` instruction and checking the `T` bit, but since zig currently doesn't have an equivalent to `__attribute__((target("thumb")))` that can be applied on a per-function basis, the thumb-ness of all non-extern functions will always match `@import("builtin").target.arch.isThumb()`. For deeper stack frames, it doesn't matter what register the FP was stored in, since when we unwind we're getting it from the stack instead. As long as all functions were compiled with `-fno-omit-frame-pointer`, the FP will always have been pushed immediately after the LR. So the first change is to have `std.debug.cpu_context.Arm.getFp()` use R7 when `native_arch` is thumb. The current inline assembly for grabbing the CPU context mentions only using `STM` for R0-R12 for thumb compatibility, but it still uses several 32-bit Thumb2 instructions that don't exist on architectures like Cortex M0/M0+. This PR adds a slightly more verbose alternative version that only uses 16-bit instructions if the target doesn't support Thumb2. I thought about skipping R8-R12 entirely on targets that only support Thumb1, since it's highly unlikely that they'll actually be used for anything, but for now they are still being captured. With these two changes, and appropriate `debug` declarations in the root module to enable symbol lookup using a custom minified string table tacked onto the end of the firmware blob, I can get stack traces on an RP2040 microcontroller (Cortex M0+): ``` src/main.zig:135:37: 0x10000b5a in dumpCurrentStack (firmware) src/main.zig:131:5: 0x10000b50 in iAmAFunctionYesIAm (firmware) src/main.zig:100:21: 0x10000a7c in main (firmware) $rpi/src/rp2040/boot.zig:112:16: 0x10000310 in _start (firmware) $rpi/src/rp2040/boot.zig:31:11: 0x100002b6 in _boot3 (firmware) ``` But there's still one more problem: all of the reported addresses point to the instruction immediately following the `BL`/`BLX` instruction that we would expect. For example, here's the disassembly for the top two functions: ``` main.iAmAFunctionYesIAm: 10000b48 80b5 push {r7, lr} 10000b4a 00af add r7, sp, #0 10000b4c 00f001f8 bl #main.dumpCurrentStack 10000b50 80bd pop {r7, pc} main.dumpCurrentStack: 10000b52 80b5 push {r7, lr} 10000b54 00af add r7, sp, #0 10000b56 00f001f8 bl #debug.writeCurrentStackTrace 10000b5a 80bd pop {r7, pc} ``` In this case, none of the resolved symbols or source locations are affected by the incorrect instruction addresses, but if the next instruction after the subroutine happened to be an inlined function, they could be erroneous. We need to make sure that the thumb bit is cleared so that when `std.debug.StackIterator` subtracts 1 from the return address, it ends up in the previous call instruction. I put this in `std.debug.stripInstructionPtrAuthCode` and renamed it to `std.debug.stripInstructionPtrMarkerBits` since I believe everywhere that's currently used should have the thumb bit stripped as well (or at least it shouldn't harm anything to do so). With all of the changes, my RP2040 test now looks correct: ``` src/main.zig:135:37: 0x10000b59 in dumpCurrentStack (firmware) src/main.zig:131:5: 0x10000b4f in iAmAFunctionYesIAm (firmware) src/main.zig:100:21: 0x10000a7b in main (firmware) $rpi/src/rp2040/boot.zig:112:16: 0x1000030f in _start (firmware) $rpi/src/rp2040/boot.zig:31:11: 0x100002b5 in _boot3 (firmware) ``` Reviewed-on: https://codeberg.org/ziglang/zig/pulls/37482 Reviewed-by: Alex Rønne Petersen <alex@alexrp.com>
2026-10-31 12:49:51 +01:00
std.debug: Support FP unwinding on `thumb-*` targets (#37482) When compiling Arm Thumb code, GCC and LLVM ([including zig](https://godbolt.org/z/sfxd37P3x)) use R7 as the frame pointer (except when `-fomit-frame-pointer` is specified, of course) rather than R11 which is used in 32-bit mode. This is mainly due to Thumb1's `PUSH`/`POP` instructions only supporting R0-R7 and R14/R15. Armv7-M does have Thumb2 `PUSH.W`/`POP.W` which can do it, but LLVM still uses R7, even if you explicitly use e.g. `-mcpu cortex_m3`. Note that AAPCS32 doesn't mention using R7 as FP in Thumb mode at all, but it also only barely mentions using R11 as FP: "In some variants r11 (FP) may be used as a frame pointer in order to chain frame activation records into a linked list." `std.debug.cpu_context.Arm` currently always assumes R11 is the frame pointer for both `arm` and `thumb` targets, which means FP unwinding simply doesn't work on thumb targets, including most arm microcontrollers. It would be nice to have stack tracing available through the FP on these constrained platforms since you often don't have enough ROM space to waste on the `.debug_frame` section, and [`.ARM.exidx`/`.ARM.extab` unwinding](https://codeberg.org/ziglang/zig/issues/30931) isn't implemented yet. On some platforms, it's possible to have both thumb and arm functions interleaved within a single call stack. In these cases, [compilers use R11 for the FP in arm functions, and R7 in thumb functions](https://godbolt.org/z/EsaYW8rhh), so neither blindly using R11 nor R7 will guarantee a correct stack unwinding in the general case. The most reliable way to determine which register is the FP for the currently executing stack frame would be by reading `CPSR` using the `MRS` instruction and checking the `T` bit, but since zig currently doesn't have an equivalent to `__attribute__((target("thumb")))` that can be applied on a per-function basis, the thumb-ness of all non-extern functions will always match `@import("builtin").target.arch.isThumb()`. For deeper stack frames, it doesn't matter what register the FP was stored in, since when we unwind we're getting it from the stack instead. As long as all functions were compiled with `-fno-omit-frame-pointer`, the FP will always have been pushed immediately after the LR. So the first change is to have `std.debug.cpu_context.Arm.getFp()` use R7 when `native_arch` is thumb. The current inline assembly for grabbing the CPU context mentions only using `STM` for R0-R12 for thumb compatibility, but it still uses several 32-bit Thumb2 instructions that don't exist on architectures like Cortex M0/M0+. This PR adds a slightly more verbose alternative version that only uses 16-bit instructions if the target doesn't support Thumb2. I thought about skipping R8-R12 entirely on targets that only support Thumb1, since it's highly unlikely that they'll actually be used for anything, but for now they are still being captured. With these two changes, and appropriate `debug` declarations in the root module to enable symbol lookup using a custom minified string table tacked onto the end of the firmware blob, I can get stack traces on an RP2040 microcontroller (Cortex M0+): ``` src/main.zig:135:37: 0x10000b5a in dumpCurrentStack (firmware) src/main.zig:131:5: 0x10000b50 in iAmAFunctionYesIAm (firmware) src/main.zig:100:21: 0x10000a7c in main (firmware) $rpi/src/rp2040/boot.zig:112:16: 0x10000310 in _start (firmware) $rpi/src/rp2040/boot.zig:31:11: 0x100002b6 in _boot3 (firmware) ``` But there's still one more problem: all of the reported addresses point to the instruction immediately following the `BL`/`BLX` instruction that we would expect. For example, here's the disassembly for the top two functions: ``` main.iAmAFunctionYesIAm: 10000b48 80b5 push {r7, lr} 10000b4a 00af add r7, sp, #0 10000b4c 00f001f8 bl #main.dumpCurrentStack 10000b50 80bd pop {r7, pc} main.dumpCurrentStack: 10000b52 80b5 push {r7, lr} 10000b54 00af add r7, sp, #0 10000b56 00f001f8 bl #debug.writeCurrentStackTrace 10000b5a 80bd pop {r7, pc} ``` In this case, none of the resolved symbols or source locations are affected by the incorrect instruction addresses, but if the next instruction after the subroutine happened to be an inlined function, they could be erroneous. We need to make sure that the thumb bit is cleared so that when `std.debug.StackIterator` subtracts 1 from the return address, it ends up in the previous call instruction. I put this in `std.debug.stripInstructionPtrAuthCode` and renamed it to `std.debug.stripInstructionPtrMarkerBits` since I believe everywhere that's currently used should have the thumb bit stripped as well (or at least it shouldn't harm anything to do so). With all of the changes, my RP2040 test now looks correct: ``` src/main.zig:135:37: 0x10000b59 in dumpCurrentStack (firmware) src/main.zig:131:5: 0x10000b4f in iAmAFunctionYesIAm (firmware) src/main.zig:100:21: 0x10000a7b in main (firmware) $rpi/src/rp2040/boot.zig:112:16: 0x1000030f in _start (firmware) $rpi/src/rp2040/boot.zig:31:11: 0x100002b5 in _boot3 (firmware) ``` Reviewed-on: https://codeberg.org/ziglang/zig/pulls/37482 Reviewed-by: Alex Rønne Petersen <alex@alexrp.com>
2026-10-31 12:49:51 +01:00
2026-04-13 00:43:34 -07:00
2026-04-13 00:43:34 -07:00
2026-04-13 00:43:34 -07:00
2026-04-13 00:43:34 -07:00
2026-04-13 00:43:34 -07:00
2026-04-13 00:43:34 -07:00
2025-12-17 15:47:33 -08:00
2026-04-13 00:43:34 -07:00
2026-04-13 00:43:34 -07:00
2026-04-13 00:43:34 -07:00
2026-04-13 00:43:34 -07:00
2025-09-08 14:31:45 +01:00
2017-04-24 12:01:19 -04:00
2017-04-24 12:01:19 -04:00
2017-04-24 12:01:19 -04:00
2025-09-01 16:50:39 +01:00
2025-12-08 23:14:34 -08:00
2025-06-30 15:54:41 -07:00
2025-12-08 17:14:31 -08:00
2025-06-30 15:54:41 -07:00
2025-12-08 17:14:31 -08:00
2025-12-08 17:14:31 -08:00
2025-12-08 23:14:34 -08:00
2025-12-08 17:14:31 -08:00
2017-04-24 12:01:19 -04:00
2025-09-08 14:31:45 +01:00
2025-09-01 16:50:39 +01:00
2025-09-08 14:31:45 +01:00
2025-09-08 14:31:45 +01:00
std.heap.ArenaAllocator: make it threadsafe Modifies the `Allocator` implementation provided by `ArenaAllocator` to be threadsafe using only atomics and no synchronization primitives locked behind an `Io` implementation. At its core this is a lock-free singly linked list which uses CAS loops to exchange the head node. A nice property of `ArenaAllocator` is that the only functions that can ever remove nodes from its linked list are `reset` and `deinit`, both of which are not part of the `Allocator` interface and thus aren't threadsafe, so node-related ABA problems are impossible. There *are* some trade-offs: end index tracking is now per node instead of per allocator instance. It's not possible to publish a head node and its end index at the same time if the latter isn't part of the former. Another compromise had to be made in regards to resizing existing nodes. Annoyingly, `rawResize` of an arbitrary thread-safe child allocator can of course never be guaranteed to be an atomic operation, so only one `alloc` call can ever resize at the same time, other threads have to consider any resizes they attempt during that time failed. This causes slightly less optimal behavior than what could be achieved with a mutex. The LSB of `Node.size` is used to signal that a node is being resized. This means that all nodes have to have an even size. Calls to `alloc` have to allocate new nodes optimistically as they can only know whether any CAS on a head node will succeed after attempting it, and to attempt the CAS they of course already need to know the address of the freshly allocated node they are trying to make the new head. The simplest solution to this would be to just free the new node again if a CAS fails, however this can be expensive and would mean that in practice arenas could only really be used with a GPA as their child allocator. To work around this, this implementation keeps its own free list of nodes which didn't make their CAS to be reused by a later `alloc` invocation. To keep things simple and avoid ABA problems the free list is only ever be accessed beyond its head by 'stealing' the head node (and thus the entire list) with an atomic swap. This makes iteration and removal trivial since there's only ever one thread doing it at a time which also owns all nodes it's holding. When the thread is done it can just push its list onto the free list again. This implementation offers comparable performance to the previous one when only being accessed by a single thread and a slight speedup compared to the previous implementation wrapped into a `ThreadSafeAllocator` up to ~7 threads performing operations on it concurrently. (measured on a base model MacBook Pro M1)
2026-02-15 17:05:00 +01:00
2025-09-08 14:31:45 +01:00
2025-09-08 14:31:45 +01:00
std.debug: detect general protection faults on x86_64-linux ```zig const std = @import("std"); pub fn main() !void { var addr: *u8 = @ptrFromInt(0xaaaaaaaaaaaaaaaa); addr.* = 1; } ``` On x86_64-linux: Before: ``` $ zig run x.zig Segmentation fault at address 0x0 /home/wooster/Desktop/zig/x.zig:5:5: 0x21d887 in main (x) addr.* = 1; ^ /home/wooster/Desktop/zig-linux-x86_64/lib/std/start.zig:583:37: 0x21d847 in posixCallMainAndExit (x) const result = root.main() catch |err| { ^ /home/wooster/Desktop/zig-linux-x86_64/lib/std/start.zig:251:5: 0x21d371 in _start (x) asm volatile (switch (native_arch) { ^ ???:?:?: 0x0 in ??? (???) Aborted (core dumped) ``` After: ``` $ zig run x.zig --zig-lib-dir lib General protection exception /home/wooster/Desktop/zig/x.zig:5:5: 0x21d907 in main (x) addr.* = 1; ^ /home/wooster/Desktop/zig/lib/std/start.zig:583:37: 0x21d8c7 in posixCallMainAndExit (x) const result = root.main() catch |err| { ^ /home/wooster/Desktop/zig/lib/std/start.zig:251:5: 0x21d3f1 in _start (x) asm volatile (switch (native_arch) { ^ ???:?:?: 0x0 in ??? (???) Aborted (core dumped) ``` As @IntegratedQuantum pointed out in <https://github.com/ziglang/zig/issues/17745#issuecomment-1783815386>, it seems that if `code` of the `siginfo_t` instance is a certain value (128), you are able to distinguish between a general protection exception and a segmentation fault. This does not seem to be documented on `man sigaction`: ``` The following values can be placed in si_code for a SIGSEGV signal: SEGV_MAPERR Address not mapped to object. SEGV_ACCERR Invalid permissions for mapped object. SEGV_BNDERR (since Linux 3.19) Failed address bound checks. SEGV_PKUERR (since Linux 4.6) Access was denied by memory protection keys. See pkeys(7). The protection key which applied to this access is available via si_pkey. ``` (those constants are 1, 2, 3, and 4; none of them are the 128) I can't find a lot of documentation about this but it seems to work consistently for me on x86_64-linux. Here is a gist which provides additional evidence that this is a reliable way of checking for a general protection fault: https://gist.github.com/ytoshima/5682393 (read comment in first line) See also: https://stackoverflow.com/questions/64309366/why-is-the-segfault-address-null-when-accessing-memory-that-has-any-of-the-16-mo This only seems to affect x86_64 and on 32-bit x86 this does not seem to be a problem. Helps with #17745 but doesn't close it because the issue still exists on Windows and other POSIX OSs. I also limited this to x86_64-linux for now because that's the only platform where I tested it. Might work on more POSIX OSs.
2023-10-29 19:21:32 +01:00
std: rework/remove ucontext_t 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-17 18:38:11 +01:00
std: rework/remove ucontext_t 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-17 18:38:11 +01:00
std: rework/remove ucontext_t 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-17 18:38:11 +01:00
improvements targeted at improving async functions * Reuse bytes of async function frames when non-async functions make `noasync` calls. This prevents explosive stack growth. * Zig now passes a stack size argument to the linker when linking ELF binaries. Linux ignores this value, but it is available as a program header called GNU_STACK. I prototyped some code that memory maps extra space to the stack using this program header, but there was still a problem when accessing stack memory very far down. Stack probing is needed or not working or something. I also prototyped using `@newStackCall` to call main and that does work around the issue but it also brings its own issues. That code is commented out for now in std/special/start.zig. I'm on a plane with no Internet, but I plan to consult with the musl community for advice when I get a chance. * Added `noasync` to a bunch of function calls in std.debug. It's very messy but it's a workaround that makes stack traces functional with evented I/O enabled. Eventually these will be cleaned up as the root bugs are found and fixed. Programs built in blocking mode are unaffected. * Lowered the default stack size of std.io.InStream (for the async version) to 1 MiB instead of 4. Until we figure out how to get choosing a stack size working (see 2nd bullet point above), 4 MiB tends to cause segfaults due to stack size running out, or usage of stack memory too far apart, or something like that. * Default thread stack size is bumped from 8 MiB to 16 to match the size we give for the main thread. It's planned to eventually remove this hard coded value and have Zig able to determine this value during semantic analysis, with call graph analysis and function pointer annotations and extern function annotations.
2019-09-11 20:22:49 -04:00
std: rework/remove ucontext_t 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-17 18:38:11 +01:00
2025-09-08 14:31:45 +01:00
std: rework/remove ucontext_t 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-17 18:38:11 +01:00
2025-12-17 17:00:41 -08:00
2025-12-17 15:47:33 -08:00
2025-12-17 15:47:33 -08:00
2025-12-17 15:47:33 -08:00
2025-12-17 15:47:33 -08:00
2025-12-17 17:00:41 -08:00
2025-12-17 15:47:33 -08:00
improvements targeted at improving async functions * Reuse bytes of async function frames when non-async functions make `noasync` calls. This prevents explosive stack growth. * Zig now passes a stack size argument to the linker when linking ELF binaries. Linux ignores this value, but it is available as a program header called GNU_STACK. I prototyped some code that memory maps extra space to the stack using this program header, but there was still a problem when accessing stack memory very far down. Stack probing is needed or not working or something. I also prototyped using `@newStackCall` to call main and that does work around the issue but it also brings its own issues. That code is commented out for now in std/special/start.zig. I'm on a plane with no Internet, but I plan to consult with the musl community for advice when I get a chance. * Added `noasync` to a bunch of function calls in std.debug. It's very messy but it's a workaround that makes stack traces functional with evented I/O enabled. Eventually these will be cleaned up as the root bugs are found and fixed. Programs built in blocking mode are unaffected. * Lowered the default stack size of std.io.InStream (for the async version) to 1 MiB instead of 4. Until we figure out how to get choosing a stack size working (see 2nd bullet point above), 4 MiB tends to cause segfaults due to stack size running out, or usage of stack memory too far apart, or something like that. * Default thread stack size is bumped from 8 MiB to 16 to match the size we give for the main thread. It's planned to eventually remove this hard coded value and have Zig able to determine this value during semantic analysis, with call graph analysis and function pointer annotations and extern function annotations.
2019-09-11 20:22:49 -04:00
2019-09-25 18:17:29 +03:00
2019-09-25 18:17:29 +03:00
improvements targeted at improving async functions * Reuse bytes of async function frames when non-async functions make `noasync` calls. This prevents explosive stack growth. * Zig now passes a stack size argument to the linker when linking ELF binaries. Linux ignores this value, but it is available as a program header called GNU_STACK. I prototyped some code that memory maps extra space to the stack using this program header, but there was still a problem when accessing stack memory very far down. Stack probing is needed or not working or something. I also prototyped using `@newStackCall` to call main and that does work around the issue but it also brings its own issues. That code is commented out for now in std/special/start.zig. I'm on a plane with no Internet, but I plan to consult with the musl community for advice when I get a chance. * Added `noasync` to a bunch of function calls in std.debug. It's very messy but it's a workaround that makes stack traces functional with evented I/O enabled. Eventually these will be cleaned up as the root bugs are found and fixed. Programs built in blocking mode are unaffected. * Lowered the default stack size of std.io.InStream (for the async version) to 1 MiB instead of 4. Until we figure out how to get choosing a stack size working (see 2nd bullet point above), 4 MiB tends to cause segfaults due to stack size running out, or usage of stack memory too far apart, or something like that. * Default thread stack size is bumped from 8 MiB to 16 to match the size we give for the main thread. It's planned to eventually remove this hard coded value and have Zig able to determine this value during semantic analysis, with call graph analysis and function pointer annotations and extern function annotations.
2019-09-11 20:22:49 -04:00
2022-04-21 10:14:23 +02:00
2025-12-09 18:44:39 -08:00
2025-09-13 10:29:20 +01:00
2026-04-13 00:43:34 -07:00
2026-04-11 22:27:23 -07:00
2022-04-21 10:14:23 +02:00
2025-12-17 17:00:41 -08:00
2025-12-17 15:47:33 -08:00
2025-12-17 15:47:33 -08:00
2025-12-17 15:47:33 -08:00
2025-12-17 15:47:33 -08:00