debug: add dupeContext, store a pointer to a copy of ThreadContext on UnwindContext

This commit is contained in:
kcbanner
2023-07-07 10:13:48 -04:00
parent 463bbe7807
commit 5c0d4cef1a
4 changed files with 33 additions and 14 deletions

View File

@@ -133,6 +133,9 @@ pub fn dumpCurrentStackTrace(start_addr: ?usize) void {
}
}
/// Platform-specific thread state. This contains register state, and on some platforms
/// information about the stack. This is not safe to trivially copy, because some platforms
/// use internal pointers within this structure. To make a copy, use `dupeContext`.
pub const ThreadContext = blk: {
if (native_os == .windows) {
break :blk std.os.windows.CONTEXT;
@@ -457,6 +460,19 @@ pub inline fn getContext(context: *ThreadContext) bool {
return result;
}
pub fn dupeContext(source: *const ThreadContext, dest: *ThreadContext) void {
if (native_os == .windows) dest.* = source.*;
if (!have_ucontext) return {};
return switch (native_os) {
.macos => {
dest.* = source.*;
dest.mcontext = &dest.__mcontext_data;
},
else => dest.* = source.*,
};
}
pub const UnwindError = if (have_ucontext)
@typeInfo(@typeInfo(@TypeOf(StackIterator.next_dwarf)).Fn.return_type.?).ErrorUnion.error_set
else