authorgravatar for ascottcameron@gmail.comAlex Cameron <ascottcameron@gmail.com> 2020-12-19 10:21:55+00:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-12-23 11:02:05+02:00
log60020fd545e508453ee599c9b792ddb6e164b115
treeb947e98852242e6addfb027d9c8d7e5f2ee2f776
parentd9fe7ea81592e0978166a2fcfb5edb01ee7d48ef

Enable segfault handling on FreeBSD.


2 files changed, 60 insertions(+), 2 deletions(-)

lib/std/debug.zig+12-2
......@@ -1696,6 +1696,7 @@ fn getDebugInfoAllocator() *mem.Allocator {
16961696pub const have_segfault_handling_support = switch (builtin.os.tag) {
16971697 .linux, .netbsd => true,
16981698 .windows => true,
1699 .freebsd => @hasDecl(os, "ucontext_t"),
16991700 else => false,
17001701};
17011702pub const enable_segfault_handler: bool = if (@hasDecl(root, "enable_segfault_handler"))
......@@ -1757,6 +1758,7 @@ fn handleSegfaultLinux(sig: i32, info: *const os.siginfo_t, ctx_ptr: ?*const c_v
17571758
17581759 const addr = switch (builtin.os.tag) {
17591760 .linux => @ptrToInt(info.fields.sigfault.addr),
1761 .freebsd => @ptrToInt(info.addr),
17601762 .netbsd => @ptrToInt(info.info.reason.fault.addr),
17611763 else => unreachable,
17621764 };
......@@ -1781,8 +1783,16 @@ fn handleSegfaultLinux(sig: i32, info: *const os.siginfo_t, ctx_ptr: ?*const c_v
17811783 },
17821784 .x86_64 => {
17831785 const ctx = @ptrCast(*const os.ucontext_t, @alignCast(@alignOf(os.ucontext_t), ctx_ptr));
1784 const ip = @intCast(usize, ctx.mcontext.gregs[os.REG_RIP]);
1785 const bp = @intCast(usize, ctx.mcontext.gregs[os.REG_RBP]);
1786 const ip = switch (builtin.os.tag) {
1787 .linux, .netbsd => @intCast(usize, ctx.mcontext.gregs[os.REG_RIP]),
1788 .freebsd => @intCast(usize, ctx.mcontext.rip),
1789 else => unreachable,
1790 };
1791 const bp = switch (builtin.os.tag) {
1792 .linux, .netbsd => @intCast(usize, ctx.mcontext.gregs[os.REG_RBP]),
1793 .freebsd => @intCast(usize, ctx.mcontext.rbp),
1794 else => unreachable,
1795 };
17861796 dumpStackTraceFromBase(bp, ip);
17871797 },
17881798 .arm => {
lib/std/os/bits/freebsd.zig+48
......@@ -4,6 +4,7 @@
44// The MIT license requires this copyright notice to be included in all copies
55// and substantial portions of the software.
66const std = @import("../../std.zig");
7const builtin = std.builtin;
78const maxInt = std.math.maxInt;
89
910// See https://svnweb.freebsd.org/base/head/sys/sys/_types.h?view=co
......@@ -815,6 +816,53 @@ pub const sigset_t = extern struct {
815816
816817pub const empty_sigset = sigset_t{ .__bits = [_]u32{0} ** _SIG_WORDS };
817818
819pub usingnamespace switch (builtin.arch) {
820 .x86_64 => struct {
821 pub const ucontext_t = extern struct {
822 sigmask: sigset_t,
823 mcontext: mcontext_t,
824 link: ?*ucontext_t,
825 stack: stack_t,
826 flags: c_int,
827 __spare__: [4]c_int,
828 };
829
830 /// XXX x86_64 specific
831 pub const mcontext_t = extern struct {
832 onstack: u64,
833 rdi: u64,
834 rsi: u64,
835 rdx: u64,
836 rcx: u64,
837 r8: u64,
838 r9: u64,
839 rax: u64,
840 rbx: u64,
841 rbp: u64,
842 r10: u64,
843 r11: u64,
844 r12: u64,
845 r13: u64,
846 r14: u64,
847 r15: u64,
848 trapno: u32,
849 fs: u16,
850 gs: u16,
851 addr: u64,
852 flags: u32,
853 es: u16,
854 ds: u16,
855 err: u64,
856 rip: u64,
857 cs: u64,
858 rflags: u64,
859 rsp: u64,
860 ss: u64,
861 };
862 },
863 else => struct {},
864};
865
818866pub const EPERM = 1; // Operation not permitted
819867pub const ENOENT = 2; // No such file or directory
820868pub const ESRCH = 3; // No such process