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 {...@@ -1696,6 +1696,7 @@ fn getDebugInfoAllocator() *mem.Allocator {
1696pub const have_segfault_handling_support = switch (builtin.os.tag) {1696pub const have_segfault_handling_support = switch (builtin.os.tag) {
1697 .linux, .netbsd => true,1697 .linux, .netbsd => true,
1698 .windows => true,1698 .windows => true,
1699 .freebsd => @hasDecl(os, "ucontext_t"),
1699 else => false,1700 else => false,
1700};1701};
1701pub const enable_segfault_handler: bool = if (@hasDecl(root, "enable_segfault_handler"))1702pub 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...@@ -1757,6 +1758,7 @@ fn handleSegfaultLinux(sig: i32, info: *const os.siginfo_t, ctx_ptr: ?*const c_v
17571758
1758 const addr = switch (builtin.os.tag) {1759 const addr = switch (builtin.os.tag) {
1759 .linux => @ptrToInt(info.fields.sigfault.addr),1760 .linux => @ptrToInt(info.fields.sigfault.addr),
1761 .freebsd => @ptrToInt(info.addr),
1760 .netbsd => @ptrToInt(info.info.reason.fault.addr),1762 .netbsd => @ptrToInt(info.info.reason.fault.addr),
1761 else => unreachable,1763 else => unreachable,
1762 };1764 };
...@@ -1781,8 +1783,16 @@ fn handleSegfaultLinux(sig: i32, info: *const os.siginfo_t, ctx_ptr: ?*const c_v...@@ -1781,8 +1783,16 @@ fn handleSegfaultLinux(sig: i32, info: *const os.siginfo_t, ctx_ptr: ?*const c_v
1781 },1783 },
1782 .x86_64 => {1784 .x86_64 => {
1783 const ctx = @ptrCast(*const os.ucontext_t, @alignCast(@alignOf(os.ucontext_t), ctx_ptr));1785 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]);1786 const ip = switch (builtin.os.tag) {
1785 const bp = @intCast(usize, ctx.mcontext.gregs[os.REG_RBP]);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 };
1786 dumpStackTraceFromBase(bp, ip);1796 dumpStackTraceFromBase(bp, ip);
1787 },1797 },
1788 .arm => {1798 .arm => {
lib/std/os/bits/freebsd.zig+48
...@@ -4,6 +4,7 @@...@@ -4,6 +4,7 @@
4// The MIT license requires this copyright notice to be included in all copies4// The MIT license requires this copyright notice to be included in all copies
5// and substantial portions of the software.5// and substantial portions of the software.
6const std = @import("../../std.zig");6const std = @import("../../std.zig");
7const builtin = std.builtin;
7const maxInt = std.math.maxInt;8const maxInt = std.math.maxInt;
89
9// See https://svnweb.freebsd.org/base/head/sys/sys/_types.h?view=co10// See https://svnweb.freebsd.org/base/head/sys/sys/_types.h?view=co
...@@ -815,6 +816,53 @@ pub const sigset_t = extern struct {...@@ -815,6 +816,53 @@ pub const sigset_t = extern struct {
815816
816pub const empty_sigset = sigset_t{ .__bits = [_]u32{0} ** _SIG_WORDS };817pub 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
818pub const EPERM = 1; // Operation not permitted866pub const EPERM = 1; // Operation not permitted
819pub const ENOENT = 2; // No such file or directory867pub const ENOENT = 2; // No such file or directory
820pub const ESRCH = 3; // No such process868pub const ESRCH = 3; // No such process