Skip to content

Commit f010a31

Browse files
authored
Merge pull request #25516 from alexrp/std-debug
`std.debug`: greatly expand target support for segfault handling/unwinding, and remove public `ucontext_t` completely
2 parents 07b6dbf + 36dbe66 commit f010a31

24 files changed

+931
-1649
lines changed

lib/std/c.zig

Lines changed: 1 addition & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -1841,110 +1841,6 @@ pub const PROT = switch (native_os) {
18411841
else => void,
18421842
};
18431843

1844-
pub const REG = switch (native_os) {
1845-
.linux => linux.REG,
1846-
.emscripten => emscripten.REG,
1847-
.freebsd => switch (builtin.cpu.arch) {
1848-
.aarch64 => struct {
1849-
pub const FP = 29;
1850-
pub const SP = 31;
1851-
pub const PC = 32;
1852-
},
1853-
.arm => struct {
1854-
pub const FP = 11;
1855-
pub const SP = 13;
1856-
pub const PC = 15;
1857-
},
1858-
.x86_64 => struct {
1859-
pub const RBP = 12;
1860-
pub const RIP = 21;
1861-
pub const RSP = 24;
1862-
},
1863-
else => struct {},
1864-
},
1865-
.solaris, .illumos => struct {
1866-
pub const R15 = 0;
1867-
pub const R14 = 1;
1868-
pub const R13 = 2;
1869-
pub const R12 = 3;
1870-
pub const R11 = 4;
1871-
pub const R10 = 5;
1872-
pub const R9 = 6;
1873-
pub const R8 = 7;
1874-
pub const RDI = 8;
1875-
pub const RSI = 9;
1876-
pub const RBP = 10;
1877-
pub const RBX = 11;
1878-
pub const RDX = 12;
1879-
pub const RCX = 13;
1880-
pub const RAX = 14;
1881-
pub const RIP = 17;
1882-
pub const RSP = 20;
1883-
},
1884-
.netbsd => switch (builtin.cpu.arch) {
1885-
.aarch64, .aarch64_be => struct {
1886-
pub const FP = 29;
1887-
pub const SP = 31;
1888-
pub const PC = 32;
1889-
},
1890-
.arm, .armeb => struct {
1891-
pub const FP = 11;
1892-
pub const SP = 13;
1893-
pub const PC = 15;
1894-
},
1895-
.x86 => struct {
1896-
pub const GS = 0;
1897-
pub const FS = 1;
1898-
pub const ES = 2;
1899-
pub const DS = 3;
1900-
pub const EDI = 4;
1901-
pub const ESI = 5;
1902-
pub const EBP = 6;
1903-
pub const ESP = 7;
1904-
pub const EBX = 8;
1905-
pub const EDX = 9;
1906-
pub const ECX = 10;
1907-
pub const EAX = 11;
1908-
pub const TRAPNO = 12;
1909-
pub const ERR = 13;
1910-
pub const EIP = 14;
1911-
pub const CS = 15;
1912-
pub const EFL = 16;
1913-
pub const UESP = 17;
1914-
pub const SS = 18;
1915-
},
1916-
.x86_64 => struct {
1917-
pub const RDI = 0;
1918-
pub const RSI = 1;
1919-
pub const RDX = 2;
1920-
pub const RCX = 3;
1921-
pub const R8 = 4;
1922-
pub const R9 = 5;
1923-
pub const R10 = 6;
1924-
pub const R11 = 7;
1925-
pub const R12 = 8;
1926-
pub const R13 = 9;
1927-
pub const R14 = 10;
1928-
pub const R15 = 11;
1929-
pub const RBP = 12;
1930-
pub const RBX = 13;
1931-
pub const RAX = 14;
1932-
pub const GS = 15;
1933-
pub const FS = 16;
1934-
pub const ES = 17;
1935-
pub const DS = 18;
1936-
pub const TRAPNO = 19;
1937-
pub const ERR = 20;
1938-
pub const RIP = 21;
1939-
pub const CS = 22;
1940-
pub const RFLAGS = 23;
1941-
pub const RSP = 24;
1942-
pub const SS = 25;
1943-
},
1944-
else => struct {},
1945-
},
1946-
else => struct {},
1947-
};
19481844
pub const RLIM = switch (native_os) {
19491845
.linux => linux.RLIM,
19501846
.emscripten => emscripten.RLIM,
@@ -4553,7 +4449,7 @@ pub const rusage = switch (native_os) {
45534449
pub const siginfo_t = switch (native_os) {
45544450
.linux => linux.siginfo_t,
45554451
.emscripten => emscripten.siginfo_t,
4556-
.macos, .ios, .tvos, .watchos, .visionos => extern struct {
4452+
.driverkit, .macos, .ios, .tvos, .watchos, .visionos => extern struct {
45574453
signo: c_int,
45584454
errno: c_int,
45594455
code: c_int,
@@ -11084,7 +10980,6 @@ pub const SETUSTACK = solaris.GETUSTACK;
1108410980
pub const SFD = solaris.SFD;
1108510981
pub const ctid_t = solaris.ctid_t;
1108610982
pub const file_obj = solaris.file_obj;
11087-
pub const fpregset_t = solaris.fpregset_t;
1108810983
pub const id_t = solaris.id_t;
1108910984
pub const lif_ifinfo_req = solaris.lif_ifinfo_req;
1109010985
pub const lif_nd_req = solaris.lif_nd_req;

lib/std/c/solaris.zig

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -31,29 +31,6 @@ pub const poolid_t = id_t;
3131
pub const zoneid_t = id_t;
3232
pub const ctid_t = id_t;
3333

34-
pub const fpregset_t = extern union {
35-
regs: [130]u32,
36-
chip_state: extern struct {
37-
cw: u16,
38-
sw: u16,
39-
fctw: u8,
40-
__fx_rsvd: u8,
41-
fop: u16,
42-
rip: u64,
43-
rdp: u64,
44-
mxcsr: u32,
45-
mxcsr_mask: u32,
46-
st: [8]extern union {
47-
fpr_16: [5]u16,
48-
__fpr_pad: u128,
49-
},
50-
xmm: [16]u128,
51-
__fx_ign2: [6]u128,
52-
status: u32,
53-
xstatus: u32,
54-
},
55-
};
56-
5734
pub const GETCONTEXT = 0;
5835
pub const SETCONTEXT = 1;
5936
pub const GETUSTACK = 2;

lib/std/debug.zig

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,10 @@ pub const SelfInfo = if (@hasDecl(root, "debug") and @hasDecl(root.debug, "SelfI
6363
root.debug.SelfInfo
6464
else switch (std.Target.ObjectFormat.default(native_os, native_arch)) {
6565
.coff => if (native_os == .windows) @import("debug/SelfInfo/Windows.zig") else void,
66-
.elf => @import("debug/SelfInfo/Elf.zig"),
66+
.elf => switch (native_os) {
67+
.freestanding, .other => void,
68+
else => @import("debug/SelfInfo/Elf.zig"),
69+
},
6770
.macho => @import("debug/SelfInfo/MachO.zig"),
6871
.goff, .plan9, .spirv, .wasm, .xcoff => void,
6972
.c, .hex, .raw => unreachable,
@@ -985,15 +988,15 @@ const StackIterator = union(enum) {
985988
// On RISC-V the frame pointer points to the top of the saved register
986989
// area, on pretty much every other architecture it points to the stack
987990
// slot where the previous frame pointer is saved.
988-
if (native_arch.isRISCV()) break :off -2 * @sizeOf(usize);
991+
if (native_arch.isLoongArch() or native_arch.isRISCV()) break :off -2 * @sizeOf(usize);
989992
// On SPARC the previous frame pointer is stored at 14 slots past %fp+BIAS.
990993
if (native_arch.isSPARC()) break :off 14 * @sizeOf(usize);
991994
break :off 0;
992995
};
993996

994997
/// Offset of the saved return address wrt the frame pointer.
995998
const ra_offset = off: {
996-
if (native_arch.isRISCV()) break :off -1 * @sizeOf(usize);
999+
if (native_arch.isLoongArch() or native_arch.isRISCV()) break :off -1 * @sizeOf(usize);
9971000
if (native_arch.isSPARC()) break :off 15 * @sizeOf(usize);
9981001
if (native_arch.isPowerPC64()) break :off 2 * @sizeOf(usize);
9991002
// On s390x, r14 is the link register and we need to grab it from its customary slot in the
@@ -1312,15 +1315,26 @@ fn getDebugInfoAllocator() Allocator {
13121315

13131316
/// Whether or not the current target can print useful debug information when a segfault occurs.
13141317
pub const have_segfault_handling_support = switch (native_os) {
1318+
.haiku,
13151319
.linux,
1316-
.macos,
1320+
.serenity,
1321+
1322+
.dragonfly,
1323+
.freebsd,
13171324
.netbsd,
1318-
.solaris,
1325+
.openbsd,
1326+
1327+
.driverkit,
1328+
.ios,
1329+
.macos,
1330+
.tvos,
1331+
.visionos,
1332+
.watchos,
1333+
13191334
.illumos,
1335+
.solaris,
1336+
13201337
.windows,
1321-
.freebsd,
1322-
.openbsd,
1323-
.serenity,
13241338
=> true,
13251339

13261340
else => false,
@@ -1403,11 +1417,26 @@ fn handleSegfaultPosix(sig: i32, info: *const posix.siginfo_t, ctx_ptr: ?*anyopa
14031417
}
14041418
}
14051419
const addr: usize = switch (native_os) {
1406-
.linux => @intFromPtr(info.fields.sigfault.addr),
1407-
.freebsd, .macos, .serenity => @intFromPtr(info.addr),
1408-
.netbsd => @intFromPtr(info.info.reason.fault.addr),
1409-
.openbsd => @intFromPtr(info.data.fault.addr),
1410-
.solaris, .illumos => @intFromPtr(info.reason.fault.addr),
1420+
.serenity,
1421+
.dragonfly,
1422+
.freebsd,
1423+
.driverkit,
1424+
.ios,
1425+
.macos,
1426+
.tvos,
1427+
.visionos,
1428+
.watchos,
1429+
=> @intFromPtr(info.addr),
1430+
.linux,
1431+
=> @intFromPtr(info.fields.sigfault.addr),
1432+
.netbsd,
1433+
=> @intFromPtr(info.info.reason.fault.addr),
1434+
.haiku,
1435+
.openbsd,
1436+
=> @intFromPtr(info.data.fault.addr),
1437+
.illumos,
1438+
.solaris,
1439+
=> @intFromPtr(info.reason.fault.addr),
14111440
else => comptime unreachable,
14121441
};
14131442
const name = switch (sig) {

lib/std/debug/SelfInfo/Elf.zig

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,15 @@ pub const can_unwind: bool = s: {
9494
// Notably, we are yet to support unwinding on ARM. There, unwinding is not done through
9595
// `.eh_frame`, but instead with the `.ARM.exidx` section, which has a different format.
9696
const archs: []const std.Target.Cpu.Arch = switch (builtin.target.os.tag) {
97+
// Not supported yet: arm, m68k, sparc64
98+
.haiku => &.{
99+
.aarch64,
100+
.powerpc,
101+
.riscv64,
102+
.x86,
103+
.x86_64,
104+
},
105+
// Not supported yet: arc, arm/armeb/thumb/thumbeb, csky, m68k, or1k, sparc/sparc64, xtensa
97106
.linux => &.{
98107
.aarch64,
99108
.aarch64_be,
@@ -113,31 +122,54 @@ pub const can_unwind: bool = s: {
113122
.x86,
114123
.x86_64,
115124
},
116-
.netbsd => &.{
125+
.serenity => &.{
117126
.aarch64,
118-
.aarch64_be,
119-
.x86,
120127
.x86_64,
128+
.riscv64,
121129
},
122-
.freebsd => &.{
130+
131+
.dragonfly => &.{
123132
.x86_64,
133+
},
134+
// Not supported yet: arm
135+
.freebsd => &.{
124136
.aarch64,
137+
.powerpc64,
138+
.powerpc64le,
139+
.riscv64,
140+
.x86_64,
125141
},
126-
.openbsd => &.{
142+
// Not supported yet: arm/armeb, m68k, mips64/mips64el, sparc/sparc64
143+
.netbsd => &.{
144+
.aarch64,
145+
.aarch64_be,
146+
.mips,
147+
.mipsel,
148+
.powerpc,
149+
.x86,
127150
.x86_64,
128151
},
129-
.solaris => &.{
152+
// Not supported yet: arm, sparc64
153+
.openbsd => &.{
154+
.aarch64,
155+
.mips64,
156+
.mips64el,
157+
.powerpc,
158+
.powerpc64,
159+
.riscv64,
160+
.x86,
130161
.x86_64,
131162
},
163+
132164
.illumos => &.{
133165
.x86,
134166
.x86_64,
135167
},
136-
.serenity => &.{
168+
// Not supported yet: sparc64
169+
.solaris => &.{
137170
.x86_64,
138-
.aarch64,
139-
.riscv64,
140171
},
172+
141173
else => unreachable,
142174
};
143175
for (archs) |a| {

0 commit comments

Comments
 (0)