| ... | @@ -135,8 +135,9 @@ pub fn dumpCurrentStackTrace(start_addr: ?usize) void { | ... | @@ -135,8 +135,9 @@ pub fn dumpCurrentStackTrace(start_addr: ?usize) void { |
| 135 | | 135 | |
| 136 | /// Tries to print the stack trace starting from the supplied base pointer to stderr, | 136 | /// Tries to print the stack trace starting from the supplied base pointer to stderr, |
| 137 | /// unbuffered, and ignores any error returned. | 137 | /// unbuffered, and ignores any error returned. |
| | 138 | /// `context` is either *const os.ucontext_t on posix, or the result of CONTEXT.getRegs() on Windows. |
| 138 | /// TODO multithreaded awareness | 139 | /// TODO multithreaded awareness |
| 139 | pub fn dumpStackTraceFromBase(bp: usize, ip: usize) void { | 140 | pub fn dumpStackTraceFromBase(context: anytype) void { |
| 140 | nosuspend { | 141 | nosuspend { |
| 141 | if (comptime builtin.target.isWasm()) { | 142 | if (comptime builtin.target.isWasm()) { |
| 142 | if (native_os == .wasi) { | 143 | if (native_os == .wasi) { |
| ... | @@ -156,12 +157,15 @@ pub fn dumpStackTraceFromBase(bp: usize, ip: usize) void { | ... | @@ -156,12 +157,15 @@ pub fn dumpStackTraceFromBase(bp: usize, ip: usize) void { |
| 156 | }; | 157 | }; |
| 157 | const tty_config = io.tty.detectConfig(io.getStdErr()); | 158 | const tty_config = io.tty.detectConfig(io.getStdErr()); |
| 158 | if (native_os == .windows) { | 159 | if (native_os == .windows) { |
| 159 | writeCurrentStackTraceWindows(stderr, debug_info, tty_config, ip) catch return; | 160 | writeCurrentStackTraceWindows(stderr, debug_info, tty_config, context.ip) catch return; |
| 160 | return; | 161 | return; |
| 161 | } | 162 | } |
| 162 | | 163 | |
| 163 | printSourceAtAddress(debug_info, stderr, ip, tty_config) catch return; | 164 | var it = StackIterator.initWithContext(null, debug_info, context) catch return; |
| 164 | var it = StackIterator.init(null, bp); | 165 | |
| | 166 | // TODO: Should `it.dwarf_context.pc` be `it.getIp()`? (but then the non-dwarf case has to store ip) |
| | 167 | printSourceAtAddress(debug_info, stderr, it.dwarf_context.pc, tty_config) catch return; |
| | 168 | |
| 165 | while (it.next()) |return_address| { | 169 | while (it.next()) |return_address| { |
| 166 | // On arm64 macOS, the address of the last frame is 0x0 rather than 0x1 as on x86_64 macOS, | 170 | // On arm64 macOS, the address of the last frame is 0x0 rather than 0x1 as on x86_64 macOS, |
| 167 | // therefore, we do a check for `return_address == 0` before subtracting 1 from it to avoid | 171 | // therefore, we do a check for `return_address == 0` before subtracting 1 from it to avoid |
| ... | @@ -206,6 +210,7 @@ pub fn captureStackTrace(first_address: ?usize, stack_trace: *std.builtin.StackT | ... | @@ -206,6 +210,7 @@ pub fn captureStackTrace(first_address: ?usize, stack_trace: *std.builtin.StackT |
| 206 | } | 210 | } |
| 207 | stack_trace.index = slice.len; | 211 | stack_trace.index = slice.len; |
| 208 | } else { | 212 | } else { |
| | 213 | // TODO: This should use the dwarf unwinder if it's available |
| 209 | var it = StackIterator.init(first_address, null); | 214 | var it = StackIterator.init(first_address, null); |
| 210 | for (stack_trace.instruction_addresses, 0..) |*addr, i| { | 215 | for (stack_trace.instruction_addresses, 0..) |*addr, i| { |
| 211 | addr.* = it.next() orelse { | 216 | addr.* = it.next() orelse { |
| ... | @@ -405,6 +410,11 @@ pub const StackIterator = struct { | ... | @@ -405,6 +410,11 @@ pub const StackIterator = struct { |
| 405 | // Last known value of the frame pointer register. | 410 | // Last known value of the frame pointer register. |
| 406 | fp: usize, | 411 | fp: usize, |
| 407 | | 412 | |
| | 413 | // When DebugInfo and a register context is available, this iterator can unwind |
| | 414 | // stacks with frames that don't use a frame pointer (ie. -fomit-frame-pointer). |
| | 415 | debug_info: ?*DebugInfo, |
| | 416 | dwarf_context: if (@hasDecl(os, "ucontext_t")) DW.UnwindContext else void = undefined, |
| | 417 | |
| 408 | pub fn init(first_address: ?usize, fp: ?usize) StackIterator { | 418 | pub fn init(first_address: ?usize, fp: ?usize) StackIterator { |
| 409 | if (native_arch == .sparc64) { | 419 | if (native_arch == .sparc64) { |
| 410 | // Flush all the register windows on stack. | 420 | // Flush all the register windows on stack. |
| ... | @@ -416,9 +426,17 @@ pub const StackIterator = struct { | ... | @@ -416,9 +426,17 @@ pub const StackIterator = struct { |
| 416 | return StackIterator{ | 426 | return StackIterator{ |
| 417 | .first_address = first_address, | 427 | .first_address = first_address, |
| 418 | .fp = fp orelse @frameAddress(), | 428 | .fp = fp orelse @frameAddress(), |
| | 429 | .debug_info = null, |
| 419 | }; | 430 | }; |
| 420 | } | 431 | } |
| 421 | | 432 | |
| | 433 | pub fn initWithContext(first_address: ?usize, debug_info: *DebugInfo, context: *const os.ucontext_t) !StackIterator { |
| | 434 | var iterator = init(first_address, null); |
| | 435 | iterator.debug_info = debug_info; |
| | 436 | iterator.dwarf_context = try DW.UnwindContext.init(context); |
| | 437 | return iterator; |
| | 438 | } |
| | 439 | |
| 422 | // Offset of the saved BP wrt the frame pointer. | 440 | // Offset of the saved BP wrt the frame pointer. |
| 423 | const fp_offset = if (native_arch.isRISCV()) | 441 | const fp_offset = if (native_arch.isRISCV()) |
| 424 | // On RISC-V the frame pointer points to the top of the saved register | 442 | // On RISC-V the frame pointer points to the top of the saved register |
| ... | @@ -500,7 +518,28 @@ pub const StackIterator = struct { | ... | @@ -500,7 +518,28 @@ pub const StackIterator = struct { |
| 500 | } | 518 | } |
| 501 | } | 519 | } |
| 502 | | 520 | |
| | 521 | fn next_dwarf(self: *StackIterator) !void { |
| | 522 | const module = try self.debug_info.?.getModuleForAddress(self.dwarf_context.pc); |
| | 523 | if (module.getDwarfInfo()) |di| { |
| | 524 | try di.unwindFrame(self.debug_info.?.allocator, &self.dwarf_context, module.base_address); |
| | 525 | } else return error.MissingDebugInfo; |
| | 526 | } |
| | 527 | |
| 503 | fn next_internal(self: *StackIterator) ?usize { | 528 | fn next_internal(self: *StackIterator) ?usize { |
| | 529 | if (self.debug_info != null) { |
| | 530 | if (self.next_dwarf()) |_| { |
| | 531 | return self.dwarf_context.pc; |
| | 532 | } else |err| { |
| | 533 | // Fall back to fp unwinding on the first failure, |
| | 534 | // as the register context won't be updated |
| | 535 | self.fp = self.dwarf_context.getFp() catch 0; |
| | 536 | self.debug_info = null; |
| | 537 | |
| | 538 | // TODO: Remove |
| | 539 | print("\ndwarf unwind error {}, placing fp at 0x{x}\n\n", .{err, self.fp}); |
| | 540 | } |
| | 541 | } |
| | 542 | |
| 504 | const fp = if (comptime native_arch.isSPARC()) | 543 | const fp = if (comptime native_arch.isSPARC()) |
| 505 | // On SPARC the offset is positive. (!) | 544 | // On SPARC the offset is positive. (!) |
| 506 | math.add(usize, self.fp, fp_offset) catch return null | 545 | math.add(usize, self.fp, fp_offset) catch return null |
| ... | @@ -540,6 +579,8 @@ pub fn writeCurrentStackTrace( | ... | @@ -540,6 +579,8 @@ pub fn writeCurrentStackTrace( |
| 540 | if (native_os == .windows) { | 579 | if (native_os == .windows) { |
| 541 | return writeCurrentStackTraceWindows(out_stream, debug_info, tty_config, start_addr); | 580 | return writeCurrentStackTraceWindows(out_stream, debug_info, tty_config, start_addr); |
| 542 | } | 581 | } |
| | 582 | |
| | 583 | // TODO: Capture a context and use initWithContext |
| 543 | var it = StackIterator.init(start_addr, null); | 584 | var it = StackIterator.init(start_addr, null); |
| 544 | while (it.next()) |return_address| { | 585 | while (it.next()) |return_address| { |
| 545 | // On arm64 macOS, the address of the last frame is 0x0 rather than 0x1 as on x86_64 macOS, | 586 | // On arm64 macOS, the address of the last frame is 0x0 rather than 0x1 as on x86_64 macOS, |
| ... | @@ -800,12 +841,14 @@ fn readCoffDebugInfo(allocator: mem.Allocator, coff_bytes: []const u8) !ModuleDe | ... | @@ -800,12 +841,14 @@ fn readCoffDebugInfo(allocator: mem.Allocator, coff_bytes: []const u8) !ModuleDe |
| 800 | // This coff file has embedded DWARF debug info | 841 | // This coff file has embedded DWARF debug info |
| 801 | _ = sec; | 842 | _ = sec; |
| 802 | | 843 | |
| 803 | const num_sections = std.enums.directEnumArrayLen(DW.DwarfSection, 0); | 844 | var sections: DW.DwarfInfo.SectionArray = DW.DwarfInfo.null_section_array; |
| 804 | var sections: [num_sections]?[]const u8 = [_]?[]const u8{null} ** num_sections; | 845 | errdefer for (sections) |section| if (section) |s| if (s.owned) allocator.free(s.data); |
| 805 | errdefer for (sections) |section| if (section) |s| allocator.free(s); | | |
| 806 | | 846 | |
| 807 | inline for (@typeInfo(DW.DwarfSection).Enum.fields, 0..) |section, i| { | 847 | inline for (@typeInfo(DW.DwarfSection).Enum.fields, 0..) |section, i| { |
| 808 | sections[i] = try coff_obj.getSectionDataAlloc("." ++ section.name, allocator); | 848 | sections[i] = .{ |
| | 849 | .data = try coff_obj.getSectionDataAlloc("." ++ section.name, allocator), |
| | 850 | .owned = true, |
| | 851 | }; |
| 809 | } | 852 | } |
| 810 | | 853 | |
| 811 | var dwarf = DW.DwarfInfo{ | 854 | var dwarf = DW.DwarfInfo{ |
| ... | @@ -813,7 +856,7 @@ fn readCoffDebugInfo(allocator: mem.Allocator, coff_bytes: []const u8) !ModuleDe | ... | @@ -813,7 +856,7 @@ fn readCoffDebugInfo(allocator: mem.Allocator, coff_bytes: []const u8) !ModuleDe |
| 813 | .sections = sections, | 856 | .sections = sections, |
| 814 | }; | 857 | }; |
| 815 | | 858 | |
| 816 | try DW.openDwarfDebugInfo(&dwarf, allocator); | 859 | try DW.openDwarfDebugInfo(&dwarf, allocator, coff_bytes); |
| 817 | di.debug_data = PdbOrDwarf{ .dwarf = dwarf }; | 860 | di.debug_data = PdbOrDwarf{ .dwarf = dwarf }; |
| 818 | return di; | 861 | return di; |
| 819 | } | 862 | } |
| ... | @@ -854,6 +897,8 @@ pub fn readElfDebugInfo( | ... | @@ -854,6 +897,8 @@ pub fn readElfDebugInfo( |
| 854 | elf_filename: ?[]const u8, | 897 | elf_filename: ?[]const u8, |
| 855 | build_id: ?[]const u8, | 898 | build_id: ?[]const u8, |
| 856 | expected_crc: ?u32, | 899 | expected_crc: ?u32, |
| | 900 | parent_sections: *DW.DwarfInfo.SectionArray, |
| | 901 | parent_mapped_mem: ?[]align(mem.page_size) const u8, |
| 857 | ) !ModuleDebugInfo { | 902 | ) !ModuleDebugInfo { |
| 858 | nosuspend { | 903 | nosuspend { |
| 859 | | 904 | |
| ... | @@ -891,10 +936,20 @@ pub fn readElfDebugInfo( | ... | @@ -891,10 +936,20 @@ pub fn readElfDebugInfo( |
| 891 | @ptrCast(@alignCast(&mapped_mem[shoff])), | 936 | @ptrCast(@alignCast(&mapped_mem[shoff])), |
| 892 | )[0..hdr.e_shnum]; | 937 | )[0..hdr.e_shnum]; |
| 893 | | 938 | |
| 894 | const num_sections = std.enums.directEnumArrayLen(DW.DwarfSection, 0); | 939 | var sections: DW.DwarfInfo.SectionArray = DW.DwarfInfo.null_section_array; |
| 895 | var sections: [num_sections]?[]const u8 = [_]?[]const u8{null} ** num_sections; | 940 | |
| 896 | var owned_sections: [num_sections][]const u8 = [_][]const u8{&.{}} ** num_sections; | 941 | // Take ownership over any owned sections from the parent scope |
| 897 | errdefer for (owned_sections) |section| allocator.free(section); | 942 | for (parent_sections, &sections) |*parent, *section| { |
| | 943 | if (parent.*) |*p| { |
| | 944 | section.* = p.*; |
| | 945 | p.owned = false; |
| | 946 | } |
| | 947 | } |
| | 948 | |
| | 949 | errdefer for (sections) |section| if (section) |s| if (s.owned) allocator.free(s.data); |
| | 950 | |
| | 951 | // TODO: This function should take a ptr to GNU_EH_FRAME (which is .eh_frame_hdr) from the ELF headers |
| | 952 | // and prefil sections[.eh_frame_hdr] |
| 898 | | 953 | |
| 899 | var separate_debug_filename: ?[]const u8 = null; | 954 | var separate_debug_filename: ?[]const u8 = null; |
| 900 | var separate_debug_crc: ?u32 = null; | 955 | var separate_debug_crc: ?u32 = null; |
| ... | @@ -920,7 +975,7 @@ pub fn readElfDebugInfo( | ... | @@ -920,7 +975,7 @@ pub fn readElfDebugInfo( |
| 920 | if (section_index == null) continue; | 975 | if (section_index == null) continue; |
| 921 | | 976 | |
| 922 | const section_bytes = try chopSlice(mapped_mem, shdr.sh_offset, shdr.sh_size); | 977 | const section_bytes = try chopSlice(mapped_mem, shdr.sh_offset, shdr.sh_size); |
| 923 | if ((shdr.sh_flags & elf.SHF_COMPRESSED) > 0) { | 978 | sections[section_index.?] = if ((shdr.sh_flags & elf.SHF_COMPRESSED) > 0) blk: { |
| 924 | var section_stream = io.fixedBufferStream(section_bytes); | 979 | var section_stream = io.fixedBufferStream(section_bytes); |
| 925 | var section_reader = section_stream.reader(); | 980 | var section_reader = section_stream.reader(); |
| 926 | const chdr = section_reader.readStruct(elf.Chdr) catch continue; | 981 | const chdr = section_reader.readStruct(elf.Chdr) catch continue; |
| ... | @@ -937,11 +992,14 @@ pub fn readElfDebugInfo( | ... | @@ -937,11 +992,14 @@ pub fn readElfDebugInfo( |
| 937 | const read = zlib_stream.reader().readAll(decompressed_section) catch continue; | 992 | const read = zlib_stream.reader().readAll(decompressed_section) catch continue; |
| 938 | assert(read == decompressed_section.len); | 993 | assert(read == decompressed_section.len); |
| 939 | | 994 | |
| 940 | sections[section_index.?] = decompressed_section; | 995 | break :blk .{ |
| 941 | owned_sections[section_index.?] = decompressed_section; | 996 | .data = decompressed_section, |
| 942 | } else { | 997 | .owned = true, |
| 943 | sections[section_index.?] = section_bytes; | 998 | }; |
| 944 | } | 999 | } else .{ |
| | 1000 | .data = section_bytes, |
| | 1001 | .owned = false, |
| | 1002 | }; |
| 945 | } | 1003 | } |
| 946 | | 1004 | |
| 947 | const missing_debug_info = | 1005 | const missing_debug_info = |
| ... | @@ -953,6 +1011,12 @@ pub fn readElfDebugInfo( | ... | @@ -953,6 +1011,12 @@ pub fn readElfDebugInfo( |
| 953 | // Attempt to load debug info from an external file | 1011 | // Attempt to load debug info from an external file |
| 954 | // See: https://sourceware.org/gdb/onlinedocs/gdb/Separate-Debug-Files.html | 1012 | // See: https://sourceware.org/gdb/onlinedocs/gdb/Separate-Debug-Files.html |
| 955 | if (missing_debug_info) { | 1013 | if (missing_debug_info) { |
| | 1014 | |
| | 1015 | // Only allow one level of debug info nesting |
| | 1016 | if (parent_mapped_mem) |_| { |
| | 1017 | return error.MissingDebugInfo; |
| | 1018 | } |
| | 1019 | |
| 956 | const global_debug_directories = [_][]const u8{ | 1020 | const global_debug_directories = [_][]const u8{ |
| 957 | "/usr/lib/debug", | 1021 | "/usr/lib/debug", |
| 958 | }; | 1022 | }; |
| ... | @@ -977,8 +1041,9 @@ pub fn readElfDebugInfo( | ... | @@ -977,8 +1041,9 @@ pub fn readElfDebugInfo( |
| 977 | // TODO: joinBuf would be ideal (with a fs.MAX_PATH_BYTES buffer) | 1041 | // TODO: joinBuf would be ideal (with a fs.MAX_PATH_BYTES buffer) |
| 978 | const path = try fs.path.join(allocator, &.{ global_directory, ".build-id", &id_prefix_buf, filename }); | 1042 | const path = try fs.path.join(allocator, &.{ global_directory, ".build-id", &id_prefix_buf, filename }); |
| 979 | defer allocator.free(path); | 1043 | defer allocator.free(path); |
| | 1044 | // TODO: Remove |
| 980 | std.debug.print(" Loading external debug info from {s}\n", .{path}); | 1045 | std.debug.print(" Loading external debug info from {s}\n", .{path}); |
| 981 | return readElfDebugInfo(allocator, path, null, separate_debug_crc) catch continue; | 1046 | return readElfDebugInfo(allocator, path, null, separate_debug_crc, &sections, mapped_mem) catch continue; |
| 982 | } | 1047 | } |
| 983 | } | 1048 | } |
| 984 | | 1049 | |
| ... | @@ -987,14 +1052,14 @@ pub fn readElfDebugInfo( | ... | @@ -987,14 +1052,14 @@ pub fn readElfDebugInfo( |
| 987 | if (elf_filename != null and mem.eql(u8, elf_filename.?, separate_filename)) return error.MissingDebugInfo; | 1052 | if (elf_filename != null and mem.eql(u8, elf_filename.?, separate_filename)) return error.MissingDebugInfo; |
| 988 | | 1053 | |
| 989 | // <cwd>/<gnu_debuglink> | 1054 | // <cwd>/<gnu_debuglink> |
| 990 | if (readElfDebugInfo(allocator, separate_filename, null, separate_debug_crc)) |debug_info| return debug_info else |_| {} | 1055 | if (readElfDebugInfo(allocator, separate_filename, null, separate_debug_crc, &sections, mapped_mem)) |debug_info| return debug_info else |_| {} |
| 991 | | 1056 | |
| 992 | // <cwd>/.debug/<gnu_debuglink> | 1057 | // <cwd>/.debug/<gnu_debuglink> |
| 993 | { | 1058 | { |
| 994 | const path = try fs.path.join(allocator, &.{ ".debug", separate_filename }); | 1059 | const path = try fs.path.join(allocator, &.{ ".debug", separate_filename }); |
| 995 | defer allocator.free(path); | 1060 | defer allocator.free(path); |
| 996 | | 1061 | |
| 997 | if (readElfDebugInfo(allocator, path, null, separate_debug_crc)) |debug_info| return debug_info else |_| {} | 1062 | if (readElfDebugInfo(allocator, path, null, separate_debug_crc, &sections, mapped_mem)) |debug_info| return debug_info else |_| {} |
| 998 | } | 1063 | } |
| 999 | | 1064 | |
| 1000 | var cwd_buf: [fs.MAX_PATH_BYTES]u8 = undefined; | 1065 | var cwd_buf: [fs.MAX_PATH_BYTES]u8 = undefined; |
| ... | @@ -1004,7 +1069,7 @@ pub fn readElfDebugInfo( | ... | @@ -1004,7 +1069,7 @@ pub fn readElfDebugInfo( |
| 1004 | for (global_debug_directories) |global_directory| { | 1069 | for (global_debug_directories) |global_directory| { |
| 1005 | const path = try fs.path.join(allocator, &.{ global_directory, cwd_path, separate_filename }); | 1070 | const path = try fs.path.join(allocator, &.{ global_directory, cwd_path, separate_filename }); |
| 1006 | defer allocator.free(path); | 1071 | defer allocator.free(path); |
| 1007 | if (readElfDebugInfo(allocator, path, null, separate_debug_crc)) |debug_info| return debug_info else |_| {} | 1072 | if (readElfDebugInfo(allocator, path, null, separate_debug_crc, &sections, mapped_mem)) |debug_info| return debug_info else |_| {} |
| 1008 | } | 1073 | } |
| 1009 | } | 1074 | } |
| 1010 | | 1075 | |
| ... | @@ -1016,13 +1081,13 @@ pub fn readElfDebugInfo( | ... | @@ -1016,13 +1081,13 @@ pub fn readElfDebugInfo( |
| 1016 | .sections = sections, | 1081 | .sections = sections, |
| 1017 | }; | 1082 | }; |
| 1018 | | 1083 | |
| 1019 | try DW.openDwarfDebugInfo(&di, allocator); | 1084 | try DW.openDwarfDebugInfo(&di, allocator, parent_mapped_mem orelse mapped_mem); |
| 1020 | | 1085 | |
| 1021 | return ModuleDebugInfo{ | 1086 | return ModuleDebugInfo{ |
| 1022 | .base_address = undefined, | 1087 | .base_address = undefined, |
| 1023 | .dwarf = di, | 1088 | .dwarf = di, |
| 1024 | .mapped_memory = mapped_mem, | 1089 | .mapped_memory = parent_mapped_mem orelse mapped_mem, |
| 1025 | .owned_sections = owned_sections, | 1090 | .external_mapped_memory = if (parent_mapped_mem != null) mapped_mem else null, |
| 1026 | }; | 1091 | }; |
| 1027 | } | 1092 | } |
| 1028 | } | 1093 | } |
| ... | @@ -1426,7 +1491,8 @@ pub const DebugInfo = struct { | ... | @@ -1426,7 +1491,8 @@ pub const DebugInfo = struct { |
| 1426 | for (phdrs) |*phdr| { | 1491 | for (phdrs) |*phdr| { |
| 1427 | if (phdr.p_type != elf.PT_LOAD) continue; | 1492 | if (phdr.p_type != elf.PT_LOAD) continue; |
| 1428 | | 1493 | |
| 1429 | const seg_start = info.dlpi_addr + phdr.p_vaddr; | 1494 | // Overflowing addition is used to handle the case of VSDOs having a p_vaddr = 0xffffffffff700000 |
| | 1495 | const seg_start = info.dlpi_addr +% phdr.p_vaddr; |
| 1430 | const seg_end = seg_start + phdr.p_memsz; | 1496 | const seg_end = seg_start + phdr.p_memsz; |
| 1431 | if (context.address >= seg_start and context.address < seg_end) { | 1497 | if (context.address >= seg_start and context.address < seg_end) { |
| 1432 | // Android libc uses NULL instead of an empty string to mark the | 1498 | // Android libc uses NULL instead of an empty string to mark the |
| ... | @@ -1437,6 +1503,8 @@ pub const DebugInfo = struct { | ... | @@ -1437,6 +1503,8 @@ pub const DebugInfo = struct { |
| 1437 | } | 1503 | } |
| 1438 | } else return; | 1504 | } else return; |
| 1439 | | 1505 | |
| | 1506 | // TODO: Look for the GNU_EH_FRAME section and pass it to readElfDebugInfo |
| | 1507 | |
| 1440 | for (info.dlpi_phdr[0..info.dlpi_phnum]) |phdr| { | 1508 | for (info.dlpi_phdr[0..info.dlpi_phnum]) |phdr| { |
| 1441 | if (phdr.p_type != elf.PT_NOTE) continue; | 1509 | if (phdr.p_type != elf.PT_NOTE) continue; |
| 1442 | | 1510 | |
| ... | @@ -1447,7 +1515,7 @@ pub const DebugInfo = struct { | ... | @@ -1447,7 +1515,7 @@ pub const DebugInfo = struct { |
| 1447 | const note_type = mem.readIntSliceNative(u32, note_bytes[8..12]); | 1515 | const note_type = mem.readIntSliceNative(u32, note_bytes[8..12]); |
| 1448 | if (note_type != elf.NT_GNU_BUILD_ID) continue; | 1516 | if (note_type != elf.NT_GNU_BUILD_ID) continue; |
| 1449 | if (!mem.eql(u8, "GNU\x00", note_bytes[12..16])) continue; | 1517 | if (!mem.eql(u8, "GNU\x00", note_bytes[12..16])) continue; |
| 1450 | context.build_id = note_bytes[16 .. 16 + desc_size]; | 1518 | context.build_id = note_bytes[16..][0..desc_size]; |
| 1451 | } | 1519 | } |
| 1452 | | 1520 | |
| 1453 | // Stop the iteration | 1521 | // Stop the iteration |
| ... | @@ -1466,7 +1534,10 @@ pub const DebugInfo = struct { | ... | @@ -1466,7 +1534,10 @@ pub const DebugInfo = struct { |
| 1466 | const obj_di = try self.allocator.create(ModuleDebugInfo); | 1534 | const obj_di = try self.allocator.create(ModuleDebugInfo); |
| 1467 | errdefer self.allocator.destroy(obj_di); | 1535 | errdefer self.allocator.destroy(obj_di); |
| 1468 | | 1536 | |
| 1469 | obj_di.* = try readElfDebugInfo(self.allocator, if (ctx.name.len > 0) ctx.name else null, ctx.build_id, null); | 1537 | var sections: DW.DwarfInfo.SectionArray = DW.DwarfInfo.null_section_array; |
| | 1538 | // TODO: If GNU_EH_FRAME was found, set it in sections |
| | 1539 | |
| | 1540 | obj_di.* = try readElfDebugInfo(self.allocator, if (ctx.name.len > 0) ctx.name else null, ctx.build_id, null, &sections, null); |
| 1470 | obj_di.base_address = ctx.base_address; | 1541 | obj_di.base_address = ctx.base_address; |
| 1471 | | 1542 | |
| 1472 | try self.address_map.putNoClobber(ctx.base_address, obj_di); | 1543 | try self.address_map.putNoClobber(ctx.base_address, obj_di); |
| ... | @@ -1491,6 +1562,7 @@ pub const ModuleDebugInfo = switch (native_os) { | ... | @@ -1491,6 +1562,7 @@ pub const ModuleDebugInfo = switch (native_os) { |
| 1491 | .macos, .ios, .watchos, .tvos => struct { | 1562 | .macos, .ios, .watchos, .tvos => struct { |
| 1492 | base_address: usize, | 1563 | base_address: usize, |
| 1493 | mapped_memory: []align(mem.page_size) const u8, | 1564 | mapped_memory: []align(mem.page_size) const u8, |
| | 1565 | external_mapped_memory: ?[]align(mem.page_size) const u8, |
| 1494 | symbols: []const MachoSymbol, | 1566 | symbols: []const MachoSymbol, |
| 1495 | strings: [:0]const u8, | 1567 | strings: [:0]const u8, |
| 1496 | ofiles: OFileTable, | 1568 | ofiles: OFileTable, |
| ... | @@ -1511,6 +1583,7 @@ pub const ModuleDebugInfo = switch (native_os) { | ... | @@ -1511,6 +1583,7 @@ pub const ModuleDebugInfo = switch (native_os) { |
| 1511 | self.ofiles.deinit(); | 1583 | self.ofiles.deinit(); |
| 1512 | allocator.free(self.symbols); | 1584 | allocator.free(self.symbols); |
| 1513 | os.munmap(self.mapped_memory); | 1585 | os.munmap(self.mapped_memory); |
| | 1586 | if (self.external_mapped_memory) |m| os.munmap(m); |
| 1514 | } | 1587 | } |
| 1515 | | 1588 | |
| 1516 | fn loadOFile(self: *@This(), allocator: mem.Allocator, o_file_path: []const u8) !OFileInfo { | 1589 | fn loadOFile(self: *@This(), allocator: mem.Allocator, o_file_path: []const u8) !OFileInfo { |
| ... | @@ -1723,6 +1796,12 @@ pub const ModuleDebugInfo = switch (native_os) { | ... | @@ -1723,6 +1796,12 @@ pub const ModuleDebugInfo = switch (native_os) { |
| 1723 | unreachable; | 1796 | unreachable; |
| 1724 | } | 1797 | } |
| 1725 | } | 1798 | } |
| | 1799 | |
| | 1800 | pub fn getDwarfInfo(self: *@This()) ?*const DW.DwarfInfo { |
| | 1801 | // TODO: Implement |
| | 1802 | _ = self; |
| | 1803 | return null; |
| | 1804 | } |
| 1726 | }, | 1805 | }, |
| 1727 | .uefi, .windows => struct { | 1806 | .uefi, .windows => struct { |
| 1728 | base_address: usize, | 1807 | base_address: usize, |
| ... | @@ -1803,19 +1882,24 @@ pub const ModuleDebugInfo = switch (native_os) { | ... | @@ -1803,19 +1882,24 @@ pub const ModuleDebugInfo = switch (native_os) { |
| 1803 | .line_info = opt_line_info, | 1882 | .line_info = opt_line_info, |
| 1804 | }; | 1883 | }; |
| 1805 | } | 1884 | } |
| | 1885 | |
| | 1886 | pub fn getDwarfInfo(self: *@This()) ?*const DW.DwarfInfo { |
| | 1887 | return switch (self.debug_data) { |
| | 1888 | .dwarf => |*dwarf| dwarf, |
| | 1889 | else => null, |
| | 1890 | }; |
| | 1891 | } |
| 1806 | }, | 1892 | }, |
| 1807 | .linux, .netbsd, .freebsd, .dragonfly, .openbsd, .haiku, .solaris => struct { | 1893 | .linux, .netbsd, .freebsd, .dragonfly, .openbsd, .haiku, .solaris => struct { |
| 1808 | base_address: usize, | 1894 | base_address: usize, |
| 1809 | dwarf: DW.DwarfInfo, | 1895 | dwarf: DW.DwarfInfo, |
| 1810 | mapped_memory: []align(mem.page_size) const u8, | 1896 | mapped_memory: []align(mem.page_size) const u8, |
| 1811 | owned_sections: [num_sections][]const u8 = [_][]const u8{&.{}} ** num_sections, | 1897 | external_mapped_memory: ?[]align(mem.page_size) const u8, |
| 1812 | | | |
| 1813 | const num_sections = 14; | | |
| 1814 | | 1898 | |
| 1815 | fn deinit(self: *@This(), allocator: mem.Allocator) void { | 1899 | fn deinit(self: *@This(), allocator: mem.Allocator) void { |
| 1816 | self.dwarf.deinit(allocator); | 1900 | self.dwarf.deinit(allocator); |
| 1817 | for (self.owned_sections) |section| allocator.free(section); | | |
| 1818 | os.munmap(self.mapped_memory); | 1901 | os.munmap(self.mapped_memory); |
| | 1902 | if (self.external_mapped_memory) |m| os.munmap(m); |
| 1819 | } | 1903 | } |
| 1820 | | 1904 | |
| 1821 | pub fn getSymbolAtAddress(self: *@This(), allocator: mem.Allocator, address: usize) !SymbolInfo { | 1905 | pub fn getSymbolAtAddress(self: *@This(), allocator: mem.Allocator, address: usize) !SymbolInfo { |
| ... | @@ -1823,6 +1907,10 @@ pub const ModuleDebugInfo = switch (native_os) { | ... | @@ -1823,6 +1907,10 @@ pub const ModuleDebugInfo = switch (native_os) { |
| 1823 | const relocated_address = address - self.base_address; | 1907 | const relocated_address = address - self.base_address; |
| 1824 | return getSymbolFromDwarf(allocator, relocated_address, &self.dwarf); | 1908 | return getSymbolFromDwarf(allocator, relocated_address, &self.dwarf); |
| 1825 | } | 1909 | } |
| | 1910 | |
| | 1911 | pub fn getDwarfInfo(self: *@This()) ?*const DW.DwarfInfo { |
| | 1912 | return &self.dwarf; |
| | 1913 | } |
| 1826 | }, | 1914 | }, |
| 1827 | .wasi => struct { | 1915 | .wasi => struct { |
| 1828 | fn deinit(self: *@This(), allocator: mem.Allocator) void { | 1916 | fn deinit(self: *@This(), allocator: mem.Allocator) void { |
| ... | @@ -1836,6 +1924,11 @@ pub const ModuleDebugInfo = switch (native_os) { | ... | @@ -1836,6 +1924,11 @@ pub const ModuleDebugInfo = switch (native_os) { |
| 1836 | _ = address; | 1924 | _ = address; |
| 1837 | return SymbolInfo{}; | 1925 | return SymbolInfo{}; |
| 1838 | } | 1926 | } |
| | 1927 | |
| | 1928 | pub fn getDwarfInfo(self: *@This()) ?*const DW.DwarfInfo { |
| | 1929 | _ = self; |
| | 1930 | return null; |
| | 1931 | } |
| 1839 | }, | 1932 | }, |
| 1840 | else => DW.DwarfInfo, | 1933 | else => DW.DwarfInfo, |
| 1841 | }; | 1934 | }; |
| ... | @@ -1992,55 +2085,69 @@ fn dumpSegfaultInfoPosix(sig: i32, addr: usize, ctx_ptr: ?*const anyopaque) void | ... | @@ -1992,55 +2085,69 @@ fn dumpSegfaultInfoPosix(sig: i32, addr: usize, ctx_ptr: ?*const anyopaque) void |
| 1992 | } catch os.abort(); | 2085 | } catch os.abort(); |
| 1993 | | 2086 | |
| 1994 | switch (native_arch) { | 2087 | switch (native_arch) { |
| 1995 | .x86 => { | 2088 | .x86, |
| 1996 | const ctx: *const os.ucontext_t = @ptrCast(@alignCast(ctx_ptr)); | 2089 | .x86_64, |
| 1997 | const ip = @as(usize, @intCast(ctx.mcontext.gregs[os.REG.EIP])); | 2090 | .arm, |
| 1998 | const bp = @as(usize, @intCast(ctx.mcontext.gregs[os.REG.EBP])); | 2091 | .aarch64, |
| 1999 | dumpStackTraceFromBase(bp, ip); | 2092 | => { |
| 2000 | }, | 2093 | const ctx = @ptrCast(*const os.ucontext_t, @alignCast(@alignOf(os.ucontext_t), ctx_ptr)); |
| 2001 | .x86_64 => { | 2094 | dumpStackTraceFromBase(ctx); |
| 2002 | const ctx: *const os.ucontext_t = @ptrCast(@alignCast(ctx_ptr)); | | |
| 2003 | const ip = switch (native_os) { | | |
| 2004 | .linux, .netbsd, .solaris => @as(usize, @intCast(ctx.mcontext.gregs[os.REG.RIP])), | | |
| 2005 | .freebsd => @as(usize, @intCast(ctx.mcontext.rip)), | | |
| 2006 | .openbsd => @as(usize, @intCast(ctx.sc_rip)), | | |
| 2007 | .macos => @as(usize, @intCast(ctx.mcontext.ss.rip)), | | |
| 2008 | else => unreachable, | | |
| 2009 | }; | | |
| 2010 | const bp = switch (native_os) { | | |
| 2011 | .linux, .netbsd, .solaris => @as(usize, @intCast(ctx.mcontext.gregs[os.REG.RBP])), | | |
| 2012 | .openbsd => @as(usize, @intCast(ctx.sc_rbp)), | | |
| 2013 | .freebsd => @as(usize, @intCast(ctx.mcontext.rbp)), | | |
| 2014 | .macos => @as(usize, @intCast(ctx.mcontext.ss.rbp)), | | |
| 2015 | else => unreachable, | | |
| 2016 | }; | | |
| 2017 | dumpStackTraceFromBase(bp, ip); | | |
| 2018 | }, | | |
| 2019 | .arm => { | | |
| 2020 | const ctx: *const os.ucontext_t = @ptrCast(@alignCast(ctx_ptr)); | | |
| 2021 | const ip = @as(usize, @intCast(ctx.mcontext.arm_pc)); | | |
| 2022 | const bp = @as(usize, @intCast(ctx.mcontext.arm_fp)); | | |
| 2023 | dumpStackTraceFromBase(bp, ip); | | |
| 2024 | }, | | |
| 2025 | .aarch64 => { | | |
| 2026 | const ctx: *const os.ucontext_t = @ptrCast(@alignCast(ctx_ptr)); | | |
| 2027 | const ip = switch (native_os) { | | |
| 2028 | .macos => @as(usize, @intCast(ctx.mcontext.ss.pc)), | | |
| 2029 | .netbsd => @as(usize, @intCast(ctx.mcontext.gregs[os.REG.PC])), | | |
| 2030 | .freebsd => @as(usize, @intCast(ctx.mcontext.gpregs.elr)), | | |
| 2031 | else => @as(usize, @intCast(ctx.mcontext.pc)), | | |
| 2032 | }; | | |
| 2033 | // x29 is the ABI-designated frame pointer | | |
| 2034 | const bp = switch (native_os) { | | |
| 2035 | .macos => @as(usize, @intCast(ctx.mcontext.ss.fp)), | | |
| 2036 | .netbsd => @as(usize, @intCast(ctx.mcontext.gregs[os.REG.FP])), | | |
| 2037 | .freebsd => @as(usize, @intCast(ctx.mcontext.gpregs.x[os.REG.FP])), | | |
| 2038 | else => @as(usize, @intCast(ctx.mcontext.regs[29])), | | |
| 2039 | }; | | |
| 2040 | dumpStackTraceFromBase(bp, ip); | | |
| 2041 | }, | 2095 | }, |
| 2042 | else => {}, | 2096 | else => {}, |
| 2043 | } | 2097 | } |
| | 2098 | |
| | 2099 | // TODO: Move this logic to dwarf.abi.regBytes |
| | 2100 | |
| | 2101 | // switch (native_arch) { |
| | 2102 | // .x86 => { |
| | 2103 | // const ctx = @ptrCast(*const os.ucontext_t, @alignCast(@alignOf(os.ucontext_t), ctx_ptr)); |
| | 2104 | // const ip = @intCast(usize, ctx.mcontext.gregs[os.REG.EIP]) ; |
| | 2105 | // const bp = @intCast(usize, ctx.mcontext.gregs[os.REG.EBP]); |
| | 2106 | // dumpStackTraceFromBase(bp, ip); |
| | 2107 | // }, |
| | 2108 | // .x86_64 => { |
| | 2109 | // const ctx = @ptrCast(*const os.ucontext_t, @alignCast(@alignOf(os.ucontext_t), ctx_ptr)); |
| | 2110 | // const ip = switch (native_os) { |
| | 2111 | // .linux, .netbsd, .solaris => @intCast(usize, ctx.mcontext.gregs[os.REG.RIP]), |
| | 2112 | // .freebsd => @intCast(usize, ctx.mcontext.rip), |
| | 2113 | // .openbsd => @intCast(usize, ctx.sc_rip), |
| | 2114 | // .macos => @intCast(usize, ctx.mcontext.ss.rip), |
| | 2115 | // else => unreachable, |
| | 2116 | // }; |
| | 2117 | // const bp = switch (native_os) { |
| | 2118 | // .linux, .netbsd, .solaris => @intCast(usize, ctx.mcontext.gregs[os.REG.RBP]), |
| | 2119 | // .openbsd => @intCast(usize, ctx.sc_rbp), |
| | 2120 | // .freebsd => @intCast(usize, ctx.mcontext.rbp), |
| | 2121 | // .macos => @intCast(usize, ctx.mcontext.ss.rbp), |
| | 2122 | // else => unreachable, |
| | 2123 | // }; |
| | 2124 | // dumpStackTraceFromBase(bp, ip); |
| | 2125 | // }, |
| | 2126 | // .arm => { |
| | 2127 | // const ctx = @ptrCast(*const os.ucontext_t, @alignCast(@alignOf(os.ucontext_t), ctx_ptr)); |
| | 2128 | // const ip = @intCast(usize, ctx.mcontext.arm_pc); |
| | 2129 | // const bp = @intCast(usize, ctx.mcontext.arm_fp); |
| | 2130 | // dumpStackTraceFromBase(bp, ip); |
| | 2131 | // }, |
| | 2132 | // .aarch64 => { |
| | 2133 | // const ctx = @ptrCast(*const os.ucontext_t, @alignCast(@alignOf(os.ucontext_t), ctx_ptr)); |
| | 2134 | // const ip = switch (native_os) { |
| | 2135 | // .macos => @intCast(usize, ctx.mcontext.ss.pc), |
| | 2136 | // .netbsd => @intCast(usize, ctx.mcontext.gregs[os.REG.PC]), |
| | 2137 | // .freebsd => @intCast(usize, ctx.mcontext.gpregs.elr), |
| | 2138 | // else => @intCast(usize, ctx.mcontext.pc), |
| | 2139 | // }; |
| | 2140 | // // x29 is the ABI-designated frame pointer |
| | 2141 | // const bp = switch (native_os) { |
| | 2142 | // .macos => @intCast(usize, ctx.mcontext.ss.fp), |
| | 2143 | // .netbsd => @intCast(usize, ctx.mcontext.gregs[os.REG.FP]), |
| | 2144 | // .freebsd => @intCast(usize, ctx.mcontext.gpregs.x[os.REG.FP]), |
| | 2145 | // else => @intCast(usize, ctx.mcontext.regs[29]), |
| | 2146 | // }; |
| | 2147 | // dumpStackTraceFromBase(bp, ip); |
| | 2148 | // }, |
| | 2149 | // else => {}, |
| | 2150 | // } |
| 2044 | } | 2151 | } |
| 2045 | | 2152 | |
| 2046 | fn handleSegfaultWindows(info: *windows.EXCEPTION_POINTERS) callconv(windows.WINAPI) c_long { | 2153 | fn handleSegfaultWindows(info: *windows.EXCEPTION_POINTERS) callconv(windows.WINAPI) c_long { |
| ... | @@ -2105,7 +2212,7 @@ fn dumpSegfaultInfoWindows(info: *windows.EXCEPTION_POINTERS, msg: u8, label: ?[ | ... | @@ -2105,7 +2212,7 @@ fn dumpSegfaultInfoWindows(info: *windows.EXCEPTION_POINTERS, msg: u8, label: ?[ |
| 2105 | else => unreachable, | 2212 | else => unreachable, |
| 2106 | } catch os.abort(); | 2213 | } catch os.abort(); |
| 2107 | | 2214 | |
| 2108 | dumpStackTraceFromBase(regs.bp, regs.ip); | 2215 | dumpStackTraceFromBase(regs); |
| 2109 | } | 2216 | } |
| 2110 | | 2217 | |
| 2111 | pub fn dumpStackPointerAddr(prefix: []const u8) void { | 2218 | pub fn dumpStackPointerAddr(prefix: []const u8) void { |