authorgravatar for kcbanner@gmail.comCasey Banner <kcbanner@gmail.com> 2023-07-02 02:08:06-04:00
committergravatar for kcbanner@gmail.comCasey Banner <kcbanner@gmail.com> 2023-07-20 22:58:14-04:00
log6a5e2b713fbaa009bb4ec73cf132a0bab6af6205
tree0d7050b374a39cbc510fe1436c0d974187bfdc90
parentccc9f8206898275474b3651a7456ec8b5c6210ff

debug: de-duplicate some code in macos ModuleDebugInfo


1 files changed, 40 insertions(+), 56 deletions(-)

lib/std/debug.zig+40-56
......@@ -1754,7 +1754,7 @@ pub const ModuleDebugInfo = switch (native_os) {
17541754 os.munmap(self.mapped_memory);
17551755 }
17561756
1757 fn loadOFile(self: *@This(), allocator: mem.Allocator, o_file_path: []const u8) !OFileInfo {
1757 fn loadOFile(self: *@This(), allocator: mem.Allocator, o_file_path: []const u8) !*OFileInfo {
17581758 const o_file = try fs.cwd().openFile(o_file_path, .{ .intended_io_mode = .blocking });
17591759 const mapped_mem = try mapWholeFile(o_file);
17601760
......@@ -1799,11 +1799,11 @@ pub const ModuleDebugInfo = switch (native_os) {
17991799
18001800 var sections: DW.DwarfInfo.SectionArray = DW.DwarfInfo.null_section_array;
18011801 for (segcmd.?.getSections()) |sect| {
1802 const name = sect.sectName();
1802 if (!std.mem.eql(u8, "__DWARF", sect.segName())) continue;
18031803
18041804 var section_index: ?usize = null;
18051805 inline for (@typeInfo(DW.DwarfSection).Enum.fields, 0..) |section, i| {
1806 if (mem.eql(u8, "__" ++ section.name, name)) section_index = i;
1806 if (mem.eql(u8, "__" ++ section.name, sect.sectName())) section_index = i;
18071807 }
18081808 if (section_index == null) continue;
18091809
......@@ -1834,65 +1834,31 @@ pub const ModuleDebugInfo = switch (native_os) {
18341834 };
18351835
18361836 // Add the debug info to the cache
1837 try self.ofiles.putNoClobber(o_file_path, info);
1837 const result = try self.ofiles.getOrPut(o_file_path);
1838 assert(!result.found_existing);
1839 result.value_ptr.* = info;
18381840
1839 return info;
1840 }
1841
1842 fn getOFileForAddress(self: *@This(), allocator: mem.Allocator, address: usize) !?*OFileInfo {
1843 nosuspend {
1844 const relocated_address = address - self.base_address;
1845 const symbol = machoSearchSymbols(self.symbols, relocated_address) orelse
1846 return null;
1847
1848 const o_file_path = mem.sliceTo(self.strings[symbol.ofile..], 0);
1849 var o_file_info = self.ofiles.get(o_file_path) orelse
1850 (self.loadOFile(allocator, o_file_path) catch |err| switch (err) {
1851 error.FileNotFound,
1852 error.MissingDebugInfo,
1853 error.InvalidDebugInfo,
1854 => return null,
1855 else => return err,
1856 });
1857
1858 return &o_file_info.di;
1859 }
1841 return result.value_ptr;
18601842 }
18611843
18621844 pub fn getSymbolAtAddress(self: *@This(), allocator: mem.Allocator, address: usize) !SymbolInfo {
18631845 nosuspend {
1864 // Translate the VA into an address into this object
1865 const relocated_address = address - self.base_address;
1866
1867 // Find the .o file where this symbol is defined
1868 const symbol = machoSearchSymbols(self.symbols, relocated_address) orelse
1869 return SymbolInfo{};
1870 const addr_off = relocated_address - symbol.addr;
1846 const result = try self.getOFileInfoForAddress(allocator, address);
1847 if (result.symbol == null) return .{};
18711848
18721849 // Take the symbol name from the N_FUN STAB entry, we're going to
18731850 // use it if we fail to find the DWARF infos
1874 const stab_symbol = mem.sliceTo(self.strings[symbol.strx..], 0);
1875 const o_file_path = mem.sliceTo(self.strings[symbol.ofile..], 0);
1876
1877 // Check if its debug infos are already in the cache
1878 var o_file_info = self.ofiles.get(o_file_path) orelse
1879 (self.loadOFile(allocator, o_file_path) catch |err| switch (err) {
1880 error.FileNotFound,
1881 error.MissingDebugInfo,
1882 error.InvalidDebugInfo,
1883 => {
1884 return SymbolInfo{ .symbol_name = stab_symbol };
1885 },
1886 else => return err,
1887 });
1888 const o_file_di = &o_file_info.di;
1851 const stab_symbol = mem.sliceTo(self.strings[result.symbol.?.strx..], 0);
1852 if (result.o_file_info == null) return .{ .symbol_name = stab_symbol };
18891853
18901854 // Translate again the address, this time into an address inside the
18911855 // .o file
1892 const relocated_address_o = o_file_info.addr_table.get(stab_symbol) orelse return SymbolInfo{
1856 const relocated_address_o = result.o_file_info.?.addr_table.get(stab_symbol) orelse return .{
18931857 .symbol_name = "???",
18941858 };
18951859
1860 const addr_off = result.relocated_address - result.symbol.?.addr;
1861 const o_file_di = &result.o_file_info.?.di;
18961862 if (o_file_di.findCompileUnit(relocated_address_o)) |compile_unit| {
18971863 return SymbolInfo{
18981864 .symbol_name = o_file_di.getSymbolName(relocated_address_o) orelse "???",
......@@ -1919,30 +1885,48 @@ pub const ModuleDebugInfo = switch (native_os) {
19191885 },
19201886 else => return err,
19211887 }
1922
1923 unreachable;
19241888 }
19251889 }
19261890
1927 pub fn getDwarfInfoForAddress(self: *@This(), allocator: mem.Allocator, address: usize) !?*const DW.DwarfInfo {
1891 fn getOFileInfoForAddress(self: *@This(), allocator: mem.Allocator, address: usize) !struct {
1892 relocated_address: usize,
1893 symbol: ?*const MachoSymbol = null,
1894 o_file_info: ?*OFileInfo = null,
1895 } {
19281896 nosuspend {
1897 // Translate the VA into an address into this object
19291898 const relocated_address = address - self.base_address;
1930 const symbol = machoSearchSymbols(self.symbols, relocated_address) orelse
1931 return null;
19321899
1900 // Find the .o file where this symbol is defined
1901 const symbol = machoSearchSymbols(self.symbols, relocated_address) orelse return .{
1902 .relocated_address = relocated_address,
1903 };
1904
1905 // Check if its debug infos are already in the cache
19331906 const o_file_path = mem.sliceTo(self.strings[symbol.ofile..], 0);
1934 var o_file_info = self.ofiles.get(o_file_path) orelse
1907 var o_file_info = self.ofiles.getPtr(o_file_path) orelse
19351908 (self.loadOFile(allocator, o_file_path) catch |err| switch (err) {
19361909 error.FileNotFound,
19371910 error.MissingDebugInfo,
19381911 error.InvalidDebugInfo,
1939 => return null,
1912 => return .{
1913 .relocated_address = relocated_address,
1914 .symbol = symbol,
1915 },
19401916 else => return err,
19411917 });
19421918
1943 return &o_file_info.di;
1919 return .{
1920 .relocated_address = relocated_address,
1921 .symbol = symbol,
1922 .o_file_info = o_file_info,
1923 };
19441924 }
19451925 }
1926
1927 pub fn getDwarfInfoForAddress(self: *@This(), allocator: mem.Allocator, address: usize) !?*const DW.DwarfInfo {
1928 return if ((try self.getOFileInfoForAddress(allocator, address)).o_file_info) |o_file_info| &o_file_info.di else null;
1929 }
19461930 },
19471931 .uefi, .windows => struct {
19481932 base_address: usize,