Skip to content

Commit e5b46ea

Browse files
committed
std: dwarf namespace reorg
std.debug.Dwarf is the parsing/decoding logic. std.dwarf remains the unopinionated types and bits alone. If you look at this diff you can see a lot less redundancy in namespaces.
1 parent 377274e commit e5b46ea

File tree

7 files changed

+2793
-2784
lines changed

7 files changed

+2793
-2784
lines changed

lib/std/debug.zig

Lines changed: 39 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ const native_arch = builtin.cpu.arch;
1818
const native_os = builtin.os.tag;
1919
const native_endian = native_arch.endian();
2020

21+
pub const Dwarf = @import("debug/Dwarf.zig");
22+
2123
pub const runtime_safety = switch (builtin.mode) {
2224
.Debug, .ReleaseSafe => true,
2325
.ReleaseFast, .ReleaseSmall => false,
@@ -67,7 +69,7 @@ pub const SymbolInfo = struct {
6769
};
6870
const PdbOrDwarf = union(enum) {
6971
pdb: pdb.Pdb,
70-
dwarf: DW.DwarfInfo,
72+
dwarf: Dwarf,
7173

7274
fn deinit(self: *PdbOrDwarf, allocator: mem.Allocator) void {
7375
switch (self.*) {
@@ -566,7 +568,7 @@ pub const StackIterator = struct {
566568
// using DWARF and MachO unwind info.
567569
unwind_state: if (have_ucontext) ?struct {
568570
debug_info: *Info,
569-
dwarf_context: DW.UnwindContext,
571+
dwarf_context: Dwarf.UnwindContext,
570572
last_error: ?UnwindError = null,
571573
failed: bool = false,
572574
} else void = if (have_ucontext) null else {},
@@ -599,7 +601,7 @@ pub const StackIterator = struct {
599601
var iterator = init(first_address, null);
600602
iterator.unwind_state = .{
601603
.debug_info = debug_info,
602-
.dwarf_context = try DW.UnwindContext.init(debug_info.allocator, context),
604+
.dwarf_context = try Dwarf.UnwindContext.init(debug_info.allocator, context),
603605
};
604606

605607
return iterator;
@@ -783,7 +785,7 @@ pub const StackIterator = struct {
783785
// __unwind_info is a requirement for unwinding on Darwin. It may fall back to DWARF, but unwinding
784786
// via DWARF before attempting to use the compact unwind info will produce incorrect results.
785787
if (module.unwind_info) |unwind_info| {
786-
if (DW.unwindFrameMachO(&unwind_state.dwarf_context, &it.ma, unwind_info, module.eh_frame, module.base_address)) |return_address| {
788+
if (Dwarf.unwindFrameMachO(&unwind_state.dwarf_context, &it.ma, unwind_info, module.eh_frame, module.base_address)) |return_address| {
787789
return return_address;
788790
} else |err| {
789791
if (err != error.RequiresDWARFUnwind) return err;
@@ -1140,10 +1142,10 @@ fn readCoffDebugInfo(allocator: mem.Allocator, coff_obj: *coff.Coff) !ModuleDebu
11401142

11411143
if (coff_obj.getSectionByName(".debug_info")) |_| {
11421144
// This coff file has embedded DWARF debug info
1143-
var sections: DW.DwarfInfo.SectionArray = DW.DwarfInfo.null_section_array;
1145+
var sections: Dwarf.SectionArray = Dwarf.null_section_array;
11441146
errdefer for (sections) |section| if (section) |s| if (s.owned) allocator.free(s.data);
11451147

1146-
inline for (@typeInfo(DW.DwarfSection).Enum.fields, 0..) |section, i| {
1148+
inline for (@typeInfo(Dwarf.Section.Id).Enum.fields, 0..) |section, i| {
11471149
sections[i] = if (coff_obj.getSectionByName("." ++ section.name)) |section_header| blk: {
11481150
break :blk .{
11491151
.data = try coff_obj.getSectionDataAlloc(section_header, allocator),
@@ -1153,13 +1155,13 @@ fn readCoffDebugInfo(allocator: mem.Allocator, coff_obj: *coff.Coff) !ModuleDebu
11531155
} else null;
11541156
}
11551157

1156-
var dwarf = DW.DwarfInfo{
1158+
var dwarf = Dwarf{
11571159
.endian = native_endian,
11581160
.sections = sections,
11591161
.is_macho = false,
11601162
};
11611163

1162-
try DW.openDwarfDebugInfo(&dwarf, allocator);
1164+
try Dwarf.open(&dwarf, allocator);
11631165
di.dwarf = dwarf;
11641166
}
11651167

@@ -1211,7 +1213,7 @@ pub fn readElfDebugInfo(
12111213
elf_filename: ?[]const u8,
12121214
build_id: ?[]const u8,
12131215
expected_crc: ?u32,
1214-
parent_sections: *DW.DwarfInfo.SectionArray,
1216+
parent_sections: *Dwarf.SectionArray,
12151217
parent_mapped_mem: ?[]align(mem.page_size) const u8,
12161218
) !ModuleDebugInfo {
12171219
nosuspend {
@@ -1245,7 +1247,7 @@ pub fn readElfDebugInfo(
12451247
@ptrCast(@alignCast(&mapped_mem[shoff])),
12461248
)[0..hdr.e_shnum];
12471249

1248-
var sections: DW.DwarfInfo.SectionArray = DW.DwarfInfo.null_section_array;
1250+
var sections: Dwarf.SectionArray = Dwarf.null_section_array;
12491251

12501252
// Combine section list. This takes ownership over any owned sections from the parent scope.
12511253
for (parent_sections, &sections) |*parent, *section| {
@@ -1274,7 +1276,7 @@ pub fn readElfDebugInfo(
12741276
}
12751277

12761278
var section_index: ?usize = null;
1277-
inline for (@typeInfo(DW.DwarfSection).Enum.fields, 0..) |section, i| {
1279+
inline for (@typeInfo(Dwarf.Section.Id).Enum.fields, 0..) |section, i| {
12781280
if (mem.eql(u8, "." ++ section.name, name)) section_index = i;
12791281
}
12801282
if (section_index == null) continue;
@@ -1308,10 +1310,10 @@ pub fn readElfDebugInfo(
13081310
}
13091311

13101312
const missing_debug_info =
1311-
sections[@intFromEnum(DW.DwarfSection.debug_info)] == null or
1312-
sections[@intFromEnum(DW.DwarfSection.debug_abbrev)] == null or
1313-
sections[@intFromEnum(DW.DwarfSection.debug_str)] == null or
1314-
sections[@intFromEnum(DW.DwarfSection.debug_line)] == null;
1313+
sections[@intFromEnum(Dwarf.Section.Id.debug_info)] == null or
1314+
sections[@intFromEnum(Dwarf.Section.Id.debug_abbrev)] == null or
1315+
sections[@intFromEnum(Dwarf.Section.Id.debug_str)] == null or
1316+
sections[@intFromEnum(Dwarf.Section.Id.debug_line)] == null;
13151317

13161318
// Attempt to load debug info from an external file
13171319
// See: https://sourceware.org/gdb/onlinedocs/gdb/Separate-Debug-Files.html
@@ -1379,13 +1381,13 @@ pub fn readElfDebugInfo(
13791381
return error.MissingDebugInfo;
13801382
}
13811383

1382-
var di = DW.DwarfInfo{
1384+
var di = Dwarf{
13831385
.endian = endian,
13841386
.sections = sections,
13851387
.is_macho = false,
13861388
};
13871389

1388-
try DW.openDwarfDebugInfo(&di, allocator);
1390+
try Dwarf.open(&di, allocator);
13891391

13901392
return ModuleDebugInfo{
13911393
.base_address = undefined,
@@ -2168,13 +2170,13 @@ pub const Info = struct {
21682170
const obj_di = try self.allocator.create(ModuleDebugInfo);
21692171
errdefer self.allocator.destroy(obj_di);
21702172

2171-
var sections: DW.DwarfInfo.SectionArray = DW.DwarfInfo.null_section_array;
2173+
var sections: Dwarf.SectionArray = Dwarf.null_section_array;
21722174
if (ctx.gnu_eh_frame) |eh_frame_hdr| {
21732175
// This is a special case - pointer offsets inside .eh_frame_hdr
21742176
// are encoded relative to its base address, so we must use the
21752177
// version that is already memory mapped, and not the one that
21762178
// will be mapped separately from the ELF file.
2177-
sections[@intFromEnum(DW.DwarfSection.eh_frame_hdr)] = .{
2179+
sections[@intFromEnum(Dwarf.Section.Id.eh_frame_hdr)] = .{
21782180
.data = eh_frame_hdr,
21792181
.owned = false,
21802182
};
@@ -2219,7 +2221,7 @@ pub const ModuleDebugInfo = switch (native_os) {
22192221

22202222
const OFileTable = std.StringHashMap(OFileInfo);
22212223
const OFileInfo = struct {
2222-
di: DW.DwarfInfo,
2224+
di: Dwarf,
22232225
addr_table: std.StringHashMap(u64),
22242226
};
22252227

@@ -2278,8 +2280,8 @@ pub const ModuleDebugInfo = switch (native_os) {
22782280
addr_table.putAssumeCapacityNoClobber(sym_name, sym.n_value);
22792281
}
22802282

2281-
var sections: DW.DwarfInfo.SectionArray = DW.DwarfInfo.null_section_array;
2282-
if (self.eh_frame) |eh_frame| sections[@intFromEnum(DW.DwarfSection.eh_frame)] = .{
2283+
var sections: Dwarf.SectionArray = Dwarf.null_section_array;
2284+
if (self.eh_frame) |eh_frame| sections[@intFromEnum(Dwarf.Section.Id.eh_frame)] = .{
22832285
.data = eh_frame,
22842286
.owned = false,
22852287
};
@@ -2288,7 +2290,7 @@ pub const ModuleDebugInfo = switch (native_os) {
22882290
if (!std.mem.eql(u8, "__DWARF", sect.segName())) continue;
22892291

22902292
var section_index: ?usize = null;
2291-
inline for (@typeInfo(DW.DwarfSection).Enum.fields, 0..) |section, i| {
2293+
inline for (@typeInfo(Dwarf.Section.Id).Enum.fields, 0..) |section, i| {
22922294
if (mem.eql(u8, "__" ++ section.name, sect.sectName())) section_index = i;
22932295
}
22942296
if (section_index == null) continue;
@@ -2302,19 +2304,19 @@ pub const ModuleDebugInfo = switch (native_os) {
23022304
}
23032305

23042306
const missing_debug_info =
2305-
sections[@intFromEnum(DW.DwarfSection.debug_info)] == null or
2306-
sections[@intFromEnum(DW.DwarfSection.debug_abbrev)] == null or
2307-
sections[@intFromEnum(DW.DwarfSection.debug_str)] == null or
2308-
sections[@intFromEnum(DW.DwarfSection.debug_line)] == null;
2307+
sections[@intFromEnum(Dwarf.Section.Id.debug_info)] == null or
2308+
sections[@intFromEnum(Dwarf.Section.Id.debug_abbrev)] == null or
2309+
sections[@intFromEnum(Dwarf.Section.Id.debug_str)] == null or
2310+
sections[@intFromEnum(Dwarf.Section.Id.debug_line)] == null;
23092311
if (missing_debug_info) return error.MissingDebugInfo;
23102312

2311-
var di = DW.DwarfInfo{
2313+
var di = Dwarf{
23122314
.endian = .little,
23132315
.sections = sections,
23142316
.is_macho = true,
23152317
};
23162318

2317-
try DW.openDwarfDebugInfo(&di, allocator);
2319+
try Dwarf.open(&di, allocator);
23182320
const info = OFileInfo{
23192321
.di = di,
23202322
.addr_table = addr_table,
@@ -2411,14 +2413,14 @@ pub const ModuleDebugInfo = switch (native_os) {
24112413
}
24122414
}
24132415

2414-
pub fn getDwarfInfoForAddress(self: *@This(), allocator: mem.Allocator, address: usize) !?*const DW.DwarfInfo {
2416+
pub fn getDwarfInfoForAddress(self: *@This(), allocator: mem.Allocator, address: usize) !?*const Dwarf {
24152417
return if ((try self.getOFileInfoForAddress(allocator, address)).o_file_info) |o_file_info| &o_file_info.di else null;
24162418
}
24172419
},
24182420
.uefi, .windows => struct {
24192421
base_address: usize,
24202422
pdb: ?pdb.Pdb = null,
2421-
dwarf: ?DW.DwarfInfo = null,
2423+
dwarf: ?Dwarf = null,
24222424
coff_image_base: u64,
24232425

24242426
/// Only used if pdb is non-null
@@ -2488,7 +2490,7 @@ pub const ModuleDebugInfo = switch (native_os) {
24882490
return SymbolInfo{};
24892491
}
24902492

2491-
pub fn getDwarfInfoForAddress(self: *@This(), allocator: mem.Allocator, address: usize) !?*const DW.DwarfInfo {
2493+
pub fn getDwarfInfoForAddress(self: *@This(), allocator: mem.Allocator, address: usize) !?*const Dwarf {
24922494
_ = allocator;
24932495
_ = address;
24942496

@@ -2500,7 +2502,7 @@ pub const ModuleDebugInfo = switch (native_os) {
25002502
},
25012503
.linux, .netbsd, .freebsd, .dragonfly, .openbsd, .haiku, .solaris, .illumos => struct {
25022504
base_address: usize,
2503-
dwarf: DW.DwarfInfo,
2505+
dwarf: Dwarf,
25042506
mapped_memory: []align(mem.page_size) const u8,
25052507
external_mapped_memory: ?[]align(mem.page_size) const u8,
25062508

@@ -2516,7 +2518,7 @@ pub const ModuleDebugInfo = switch (native_os) {
25162518
return getSymbolFromDwarf(allocator, relocated_address, &self.dwarf);
25172519
}
25182520

2519-
pub fn getDwarfInfoForAddress(self: *@This(), allocator: mem.Allocator, address: usize) !?*const DW.DwarfInfo {
2521+
pub fn getDwarfInfoForAddress(self: *@This(), allocator: mem.Allocator, address: usize) !?*const Dwarf {
25202522
_ = allocator;
25212523
_ = address;
25222524
return &self.dwarf;
@@ -2535,17 +2537,17 @@ pub const ModuleDebugInfo = switch (native_os) {
25352537
return SymbolInfo{};
25362538
}
25372539

2538-
pub fn getDwarfInfoForAddress(self: *@This(), allocator: mem.Allocator, address: usize) !?*const DW.DwarfInfo {
2540+
pub fn getDwarfInfoForAddress(self: *@This(), allocator: mem.Allocator, address: usize) !?*const Dwarf {
25392541
_ = self;
25402542
_ = allocator;
25412543
_ = address;
25422544
return null;
25432545
}
25442546
},
2545-
else => DW.DwarfInfo,
2547+
else => Dwarf,
25462548
};
25472549

2548-
fn getSymbolFromDwarf(allocator: mem.Allocator, address: u64, di: *DW.DwarfInfo) !SymbolInfo {
2550+
fn getSymbolFromDwarf(allocator: mem.Allocator, address: u64, di: *Dwarf) !SymbolInfo {
25492551
if (nosuspend di.findCompileUnit(address)) |compile_unit| {
25502552
return SymbolInfo{
25512553
.symbol_name = nosuspend di.getSymbolName(address) orelse "???",

0 commit comments

Comments
 (0)