dwarf: clean up allocations in std.dwarf module

While this code probably could do with some love and a redesign,
this commit fixes the allocations by making sure we explicitly
pass an allocator where required, and we use arenas for temporary
or narrowly-scoped objects such as a `Die` (for `Die` in particular,
not every `FormValue` will be allocated - we could duplicate, or
we can use an arena which is the proposal of this commit).
This commit is contained in:
Jakub Konka 2022-04-21 00:06:52 +02:00
parent 1a1b5ee264
commit 26153ce73a
3 changed files with 185 additions and 94 deletions

View File

@ -30,10 +30,8 @@ pub const LineInfo = struct {
line: u64, line: u64,
column: u64, column: u64,
file_name: []const u8, file_name: []const u8,
allocator: ?mem.Allocator,
pub fn deinit(self: LineInfo) void { pub fn deinit(self: LineInfo, allocator: mem.Allocator) void {
const allocator = self.allocator orelse return;
allocator.free(self.file_name); allocator.free(self.file_name);
} }
}; };
@ -43,9 +41,9 @@ pub const SymbolInfo = struct {
compile_unit_name: []const u8 = "???", compile_unit_name: []const u8 = "???",
line_info: ?LineInfo = null, line_info: ?LineInfo = null,
pub fn deinit(self: @This()) void { pub fn deinit(self: @This(), allocator: mem.Allocator) void {
if (self.line_info) |li| { if (self.line_info) |li| {
li.deinit(); li.deinit(allocator);
} }
} }
}; };
@ -695,7 +693,7 @@ pub fn printSourceAtAddress(debug_info: *DebugInfo, out_stream: anytype, address
}; };
const symbol_info = try module.getSymbolAtAddress(address); const symbol_info = try module.getSymbolAtAddress(address);
defer symbol_info.deinit(); defer symbol_info.deinit(debug_info.allocator);
return printLineInfo( return printLineInfo(
out_stream, out_stream,
@ -1568,10 +1566,17 @@ pub const ModuleDebugInfo = switch (native_os) {
if (o_file_di.findCompileUnit(relocated_address_o)) |compile_unit| { if (o_file_di.findCompileUnit(relocated_address_o)) |compile_unit| {
return SymbolInfo{ return SymbolInfo{
.symbol_name = o_file_di.getSymbolName(relocated_address_o) orelse "???", .symbol_name = o_file_di.getSymbolName(relocated_address_o) orelse "???",
.compile_unit_name = compile_unit.die.getAttrString(o_file_di, DW.AT.name) catch |err| switch (err) { .compile_unit_name = compile_unit.die.getAttrString(
o_file_di,
DW.AT.name,
) catch |err| switch (err) {
error.MissingDebugInfo, error.InvalidDebugInfo => "???", error.MissingDebugInfo, error.InvalidDebugInfo => "???",
}, },
.line_info = o_file_di.getLineNumberInfo(compile_unit.*, relocated_address_o + addr_off) catch |err| switch (err) { .line_info = o_file_di.getLineNumberInfo(
self.allocator(),
compile_unit.*,
relocated_address_o + addr_off,
) catch |err| switch (err) {
error.MissingDebugInfo, error.InvalidDebugInfo => null, error.MissingDebugInfo, error.InvalidDebugInfo => null,
else => return err, else => return err,
}, },

View File

@ -7,8 +7,6 @@ const mem = std.mem;
const math = std.math; const math = std.math;
const leb = @import("leb128.zig"); const leb = @import("leb128.zig");
const ArrayList = std.ArrayList;
pub const TAG = @import("dwarf/TAG.zig"); pub const TAG = @import("dwarf/TAG.zig");
pub const AT = @import("dwarf/AT.zig"); pub const AT = @import("dwarf/AT.zig");
pub const OP = @import("dwarf/OP.zig"); pub const OP = @import("dwarf/OP.zig");
@ -157,6 +155,12 @@ const PcRange = struct {
const Func = struct { const Func = struct {
pc_range: ?PcRange, pc_range: ?PcRange,
name: ?[]const u8, name: ?[]const u8,
fn deinit(func: *Func, allocator: mem.Allocator) void {
if (func.name) |name| {
allocator.free(name);
}
}
}; };
const CompileUnit = struct { const CompileUnit = struct {
@ -166,19 +170,30 @@ const CompileUnit = struct {
pc_range: ?PcRange, pc_range: ?PcRange,
}; };
const AbbrevTable = ArrayList(AbbrevTableEntry); const AbbrevTable = std.ArrayList(AbbrevTableEntry);
const AbbrevTableHeader = struct { const AbbrevTableHeader = struct {
// offset from .debug_abbrev // offset from .debug_abbrev
offset: u64, offset: u64,
table: AbbrevTable, table: AbbrevTable,
fn deinit(header: *AbbrevTableHeader) void {
for (header.table.items) |*entry| {
entry.deinit();
}
header.table.deinit();
}
}; };
const AbbrevTableEntry = struct { const AbbrevTableEntry = struct {
has_children: bool, has_children: bool,
abbrev_code: u64, abbrev_code: u64,
tag_id: u64, tag_id: u64,
attrs: ArrayList(AbbrevAttr), attrs: std.ArrayList(AbbrevAttr),
fn deinit(entry: *AbbrevTableEntry) void {
entry.attrs.deinit();
}
}; };
const AbbrevAttr = struct { const AbbrevAttr = struct {
@ -213,15 +228,22 @@ const Constant = struct {
}; };
const Die = struct { const Die = struct {
// Arena for Die's Attr's and FormValue's.
arena: std.heap.ArenaAllocator,
tag_id: u64, tag_id: u64,
has_children: bool, has_children: bool,
attrs: ArrayList(Attr), attrs: std.ArrayListUnmanaged(Attr) = .{},
const Attr = struct { const Attr = struct {
id: u64, id: u64,
value: FormValue, value: FormValue,
}; };
fn deinit(self: *Die, allocator: mem.Allocator) void {
self.arena.deinit();
self.attrs.deinit(allocator);
}
fn getAttr(self: *const Die, id: u64) ?*const FormValue { fn getAttr(self: *const Die, id: u64) ?*const FormValue {
for (self.attrs.items) |*attr| { for (self.attrs.items) |*attr| {
if (attr.id == id) return &attr.value; if (attr.id == id) return &attr.value;
@ -292,7 +314,6 @@ const LineNumberProgram = struct {
default_is_stmt: bool, default_is_stmt: bool,
target_address: u64, target_address: u64,
include_dirs: []const []const u8, include_dirs: []const []const u8,
file_entries: *ArrayList(FileEntry),
prev_valid: bool, prev_valid: bool,
prev_address: u64, prev_address: u64,
@ -323,7 +344,7 @@ const LineNumberProgram = struct {
self.prev_end_sequence = undefined; self.prev_end_sequence = undefined;
} }
pub fn init(is_stmt: bool, include_dirs: []const []const u8, file_entries: *ArrayList(FileEntry), target_address: u64) LineNumberProgram { pub fn init(is_stmt: bool, include_dirs: []const []const u8, target_address: u64) LineNumberProgram {
return LineNumberProgram{ return LineNumberProgram{
.address = 0, .address = 0,
.file = 1, .file = 1,
@ -333,7 +354,6 @@ const LineNumberProgram = struct {
.basic_block = false, .basic_block = false,
.end_sequence = false, .end_sequence = false,
.include_dirs = include_dirs, .include_dirs = include_dirs,
.file_entries = file_entries,
.default_is_stmt = is_stmt, .default_is_stmt = is_stmt,
.target_address = target_address, .target_address = target_address,
.prev_valid = false, .prev_valid = false,
@ -347,24 +367,28 @@ const LineNumberProgram = struct {
}; };
} }
pub fn checkLineMatch(self: *LineNumberProgram) !?debug.LineInfo { pub fn checkLineMatch(
self: *LineNumberProgram,
allocator: mem.Allocator,
file_entries: []const FileEntry,
) !?debug.LineInfo {
if (self.prev_valid and self.target_address >= self.prev_address and self.target_address < self.address) { if (self.prev_valid and self.target_address >= self.prev_address and self.target_address < self.address) {
const file_entry = if (self.prev_file == 0) { const file_entry = if (self.prev_file == 0) {
return error.MissingDebugInfo; return error.MissingDebugInfo;
} else if (self.prev_file - 1 >= self.file_entries.items.len) { } else if (self.prev_file - 1 >= file_entries.len) {
return error.InvalidDebugInfo; return error.InvalidDebugInfo;
} else &self.file_entries.items[self.prev_file - 1]; } else &file_entries[self.prev_file - 1];
const dir_name = if (file_entry.dir_index >= self.include_dirs.len) { const dir_name = if (file_entry.dir_index >= self.include_dirs.len) {
return error.InvalidDebugInfo; return error.InvalidDebugInfo;
} else self.include_dirs[file_entry.dir_index]; } else self.include_dirs[file_entry.dir_index];
const file_name = try fs.path.join(self.file_entries.allocator, &[_][]const u8{ dir_name, file_entry.file_name });
errdefer self.file_entries.allocator.free(file_name); const file_name = try fs.path.join(allocator, &[_][]const u8{ dir_name, file_entry.file_name });
return debug.LineInfo{ return debug.LineInfo{
.line = if (self.prev_line >= 0) @intCast(u64, self.prev_line) else 0, .line = if (self.prev_line >= 0) @intCast(u64, self.prev_line) else 0,
.column = self.prev_column, .column = self.prev_column,
.file_name = file_name, .file_name = file_name,
.allocator = self.file_entries.allocator,
}; };
} }
@ -419,8 +443,7 @@ fn parseFormValueBlock(allocator: mem.Allocator, in_stream: anytype, endian: std
return parseFormValueBlockLen(allocator, in_stream, block_len); return parseFormValueBlockLen(allocator, in_stream, block_len);
} }
fn parseFormValueConstant(allocator: mem.Allocator, in_stream: anytype, signed: bool, endian: std.builtin.Endian, comptime size: i32) !FormValue { fn parseFormValueConstant(in_stream: anytype, signed: bool, endian: std.builtin.Endian, comptime size: i32) !FormValue {
_ = allocator;
// TODO: Please forgive me, I've worked around zig not properly spilling some intermediate values here. // TODO: Please forgive me, I've worked around zig not properly spilling some intermediate values here.
// `nosuspend` should be removed from all the function calls once it is fixed. // `nosuspend` should be removed from all the function calls once it is fixed.
return FormValue{ return FormValue{
@ -447,8 +470,7 @@ fn parseFormValueConstant(allocator: mem.Allocator, in_stream: anytype, signed:
} }
// TODO the nosuspends here are workarounds // TODO the nosuspends here are workarounds
fn parseFormValueRef(allocator: mem.Allocator, in_stream: anytype, endian: std.builtin.Endian, size: i32) !FormValue { fn parseFormValueRef(in_stream: anytype, endian: std.builtin.Endian, size: i32) !FormValue {
_ = allocator;
return FormValue{ return FormValue{
.Ref = switch (size) { .Ref = switch (size) {
1 => try nosuspend in_stream.readInt(u8, endian), 1 => try nosuspend in_stream.readInt(u8, endian),
@ -472,13 +494,13 @@ fn parseFormValue(allocator: mem.Allocator, in_stream: anytype, form_id: u64, en
const block_len = try nosuspend leb.readULEB128(usize, in_stream); const block_len = try nosuspend leb.readULEB128(usize, in_stream);
return parseFormValueBlockLen(allocator, in_stream, block_len); return parseFormValueBlockLen(allocator, in_stream, block_len);
}, },
FORM.data1 => parseFormValueConstant(allocator, in_stream, false, endian, 1), FORM.data1 => parseFormValueConstant(in_stream, false, endian, 1),
FORM.data2 => parseFormValueConstant(allocator, in_stream, false, endian, 2), FORM.data2 => parseFormValueConstant(in_stream, false, endian, 2),
FORM.data4 => parseFormValueConstant(allocator, in_stream, false, endian, 4), FORM.data4 => parseFormValueConstant(in_stream, false, endian, 4),
FORM.data8 => parseFormValueConstant(allocator, in_stream, false, endian, 8), FORM.data8 => parseFormValueConstant(in_stream, false, endian, 8),
FORM.udata, FORM.sdata => { FORM.udata, FORM.sdata => {
const signed = form_id == FORM.sdata; const signed = form_id == FORM.sdata;
return parseFormValueConstant(allocator, in_stream, signed, endian, -1); return parseFormValueConstant(in_stream, signed, endian, -1);
}, },
FORM.exprloc => { FORM.exprloc => {
const size = try nosuspend leb.readULEB128(usize, in_stream); const size = try nosuspend leb.readULEB128(usize, in_stream);
@ -489,11 +511,11 @@ fn parseFormValue(allocator: mem.Allocator, in_stream: anytype, form_id: u64, en
FORM.flag_present => FormValue{ .Flag = true }, FORM.flag_present => FormValue{ .Flag = true },
FORM.sec_offset => FormValue{ .SecOffset = try readAddress(in_stream, endian, is_64) }, FORM.sec_offset => FormValue{ .SecOffset = try readAddress(in_stream, endian, is_64) },
FORM.ref1 => parseFormValueRef(allocator, in_stream, endian, 1), FORM.ref1 => parseFormValueRef(in_stream, endian, 1),
FORM.ref2 => parseFormValueRef(allocator, in_stream, endian, 2), FORM.ref2 => parseFormValueRef(in_stream, endian, 2),
FORM.ref4 => parseFormValueRef(allocator, in_stream, endian, 4), FORM.ref4 => parseFormValueRef(in_stream, endian, 4),
FORM.ref8 => parseFormValueRef(allocator, in_stream, endian, 8), FORM.ref8 => parseFormValueRef(in_stream, endian, 8),
FORM.ref_udata => parseFormValueRef(allocator, in_stream, endian, -1), FORM.ref_udata => parseFormValueRef(in_stream, endian, -1),
FORM.ref_addr => FormValue{ .RefAddr = try readAddress(in_stream, endian, is_64) }, FORM.ref_addr => FormValue{ .RefAddr = try readAddress(in_stream, endian, is_64) },
FORM.ref_sig8 => FormValue{ .Ref = try nosuspend in_stream.readInt(u64, endian) }, FORM.ref_sig8 => FormValue{ .Ref = try nosuspend in_stream.readInt(u64, endian) },
@ -536,12 +558,24 @@ pub const DwarfInfo = struct {
debug_line_str: ?[]const u8, debug_line_str: ?[]const u8,
debug_ranges: ?[]const u8, debug_ranges: ?[]const u8,
// Filled later by the initializer // Filled later by the initializer
abbrev_table_list: ArrayList(AbbrevTableHeader) = undefined, abbrev_table_list: std.ArrayListUnmanaged(AbbrevTableHeader) = .{},
compile_unit_list: ArrayList(CompileUnit) = undefined, compile_unit_list: std.ArrayListUnmanaged(CompileUnit) = .{},
func_list: ArrayList(Func) = undefined, func_list: std.ArrayListUnmanaged(Func) = .{},
pub fn allocator(self: DwarfInfo) mem.Allocator { pub fn deinit(di: *DwarfInfo, allocator: mem.Allocator) void {
return self.abbrev_table_list.allocator; for (di.abbrev_table_list.items) |*abbrev| {
abbrev.deinit();
}
di.abbrev_table_list.deinit(allocator);
for (di.compile_unit_list.items) |*cu| {
cu.die.deinit(allocator);
allocator.destroy(cu.die);
}
di.compile_unit_list.deinit(allocator);
for (di.func_list.items) |*func| {
func.deinit(allocator);
}
di.func_list.deinit(allocator);
} }
pub fn getSymbolName(di: *DwarfInfo, address: u64) ?[]const u8 { pub fn getSymbolName(di: *DwarfInfo, address: u64) ?[]const u8 {
@ -556,12 +590,16 @@ pub const DwarfInfo = struct {
return null; return null;
} }
fn scanAllFunctions(di: *DwarfInfo) !void { fn scanAllFunctions(di: *DwarfInfo, allocator: mem.Allocator) !void {
var stream = io.fixedBufferStream(di.debug_info); var stream = io.fixedBufferStream(di.debug_info);
const in = &stream.reader(); const in = &stream.reader();
const seekable = &stream.seekableStream(); const seekable = &stream.seekableStream();
var this_unit_offset: u64 = 0; var this_unit_offset: u64 = 0;
var tmp_arena = std.heap.ArenaAllocator.init(allocator);
defer tmp_arena.deinit();
const arena = tmp_arena.allocator();
while (this_unit_offset < try seekable.getEndPos()) { while (this_unit_offset < try seekable.getEndPos()) {
try seekable.seekTo(this_unit_offset); try seekable.seekTo(this_unit_offset);
@ -580,26 +618,30 @@ pub const DwarfInfo = struct {
const unit_type = try in.readInt(u8, di.endian); const unit_type = try in.readInt(u8, di.endian);
if (unit_type != UT.compile) return error.InvalidDebugInfo; if (unit_type != UT.compile) return error.InvalidDebugInfo;
address_size = try in.readByte(); address_size = try in.readByte();
debug_abbrev_offset = if (is_64) try in.readInt(u64, di.endian) else try in.readInt(u32, di.endian); debug_abbrev_offset = if (is_64)
try in.readInt(u64, di.endian)
else
try in.readInt(u32, di.endian);
}, },
else => { else => {
debug_abbrev_offset = if (is_64) try in.readInt(u64, di.endian) else try in.readInt(u32, di.endian); debug_abbrev_offset = if (is_64)
try in.readInt(u64, di.endian)
else
try in.readInt(u32, di.endian);
address_size = try in.readByte(); address_size = try in.readByte();
}, },
} }
if (address_size != @sizeOf(usize)) return error.InvalidDebugInfo; if (address_size != @sizeOf(usize)) return error.InvalidDebugInfo;
const compile_unit_pos = try seekable.getPos(); const compile_unit_pos = try seekable.getPos();
const abbrev_table = try di.getAbbrevTable(debug_abbrev_offset); const abbrev_table = try di.getAbbrevTable(allocator, debug_abbrev_offset);
try seekable.seekTo(compile_unit_pos); try seekable.seekTo(compile_unit_pos);
const next_unit_pos = this_unit_offset + next_offset; const next_unit_pos = this_unit_offset + next_offset;
while ((try seekable.getPos()) < next_unit_pos) { while ((try seekable.getPos()) < next_unit_pos) {
const die_obj = (try di.parseDie(in, abbrev_table, is_64)) orelse continue; const die_obj = (try di.parseDie(arena, in, abbrev_table, is_64)) orelse continue;
defer die_obj.attrs.deinit();
const after_die_offset = try seekable.getPos(); const after_die_offset = try seekable.getPos();
switch (die_obj.tag_id) { switch (die_obj.tag_id) {
@ -607,23 +649,33 @@ pub const DwarfInfo = struct {
const fn_name = x: { const fn_name = x: {
var depth: i32 = 3; var depth: i32 = 3;
var this_die_obj = die_obj; var this_die_obj = die_obj;
// Prenvent endless loops // Prevent endless loops
while (depth > 0) : (depth -= 1) { while (depth > 0) : (depth -= 1) {
if (this_die_obj.getAttr(AT.name)) |_| { if (this_die_obj.getAttr(AT.name)) |_| {
const name = try this_die_obj.getAttrString(di, AT.name); const name = try this_die_obj.getAttrString(di, AT.name);
break :x name; break :x try allocator.dupe(u8, name);
} else if (this_die_obj.getAttr(AT.abstract_origin)) |_| { } else if (this_die_obj.getAttr(AT.abstract_origin)) |_| {
// Follow the DIE it points to and repeat // Follow the DIE it points to and repeat
const ref_offset = try this_die_obj.getAttrRef(AT.abstract_origin); const ref_offset = try this_die_obj.getAttrRef(AT.abstract_origin);
if (ref_offset > next_offset) return error.InvalidDebugInfo; if (ref_offset > next_offset) return error.InvalidDebugInfo;
try seekable.seekTo(this_unit_offset + ref_offset); try seekable.seekTo(this_unit_offset + ref_offset);
this_die_obj = (try di.parseDie(in, abbrev_table, is_64)) orelse return error.InvalidDebugInfo; this_die_obj = (try di.parseDie(
arena,
in,
abbrev_table,
is_64,
)) orelse return error.InvalidDebugInfo;
} else if (this_die_obj.getAttr(AT.specification)) |_| { } else if (this_die_obj.getAttr(AT.specification)) |_| {
// Follow the DIE it points to and repeat // Follow the DIE it points to and repeat
const ref_offset = try this_die_obj.getAttrRef(AT.specification); const ref_offset = try this_die_obj.getAttrRef(AT.specification);
if (ref_offset > next_offset) return error.InvalidDebugInfo; if (ref_offset > next_offset) return error.InvalidDebugInfo;
try seekable.seekTo(this_unit_offset + ref_offset); try seekable.seekTo(this_unit_offset + ref_offset);
this_die_obj = (try di.parseDie(in, abbrev_table, is_64)) orelse return error.InvalidDebugInfo; this_die_obj = (try di.parseDie(
arena,
in,
abbrev_table,
is_64,
)) orelse return error.InvalidDebugInfo;
} else { } else {
break :x null; break :x null;
} }
@ -656,7 +708,7 @@ pub const DwarfInfo = struct {
} }
}; };
try di.func_list.append(Func{ try di.func_list.append(allocator, Func{
.name = fn_name, .name = fn_name,
.pc_range = pc_range, .pc_range = pc_range,
}); });
@ -671,7 +723,7 @@ pub const DwarfInfo = struct {
} }
} }
fn scanAllCompileUnits(di: *DwarfInfo) !void { fn scanAllCompileUnits(di: *DwarfInfo, allocator: mem.Allocator) !void {
var stream = io.fixedBufferStream(di.debug_info); var stream = io.fixedBufferStream(di.debug_info);
const in = &stream.reader(); const in = &stream.reader();
const seekable = &stream.seekableStream(); const seekable = &stream.seekableStream();
@ -695,22 +747,30 @@ pub const DwarfInfo = struct {
const unit_type = try in.readInt(u8, di.endian); const unit_type = try in.readInt(u8, di.endian);
if (unit_type != UT.compile) return error.InvalidDebugInfo; if (unit_type != UT.compile) return error.InvalidDebugInfo;
address_size = try in.readByte(); address_size = try in.readByte();
debug_abbrev_offset = if (is_64) try in.readInt(u64, di.endian) else try in.readInt(u32, di.endian); debug_abbrev_offset = if (is_64)
try in.readInt(u64, di.endian)
else
try in.readInt(u32, di.endian);
}, },
else => { else => {
debug_abbrev_offset = if (is_64) try in.readInt(u64, di.endian) else try in.readInt(u32, di.endian); debug_abbrev_offset = if (is_64)
try in.readInt(u64, di.endian)
else
try in.readInt(u32, di.endian);
address_size = try in.readByte(); address_size = try in.readByte();
}, },
} }
if (address_size != @sizeOf(usize)) return error.InvalidDebugInfo; if (address_size != @sizeOf(usize)) return error.InvalidDebugInfo;
const compile_unit_pos = try seekable.getPos(); const compile_unit_pos = try seekable.getPos();
const abbrev_table = try di.getAbbrevTable(debug_abbrev_offset); const abbrev_table = try di.getAbbrevTable(allocator, debug_abbrev_offset);
try seekable.seekTo(compile_unit_pos); try seekable.seekTo(compile_unit_pos);
const compile_unit_die = try di.allocator().create(Die); const compile_unit_die = try allocator.create(Die);
compile_unit_die.* = (try di.parseDie(in, abbrev_table, is_64)) orelse return error.InvalidDebugInfo; errdefer allocator.destroy(compile_unit_die);
compile_unit_die.* = (try di.parseDie(allocator, in, abbrev_table, is_64)) orelse
return error.InvalidDebugInfo;
if (compile_unit_die.tag_id != TAG.compile_unit) return error.InvalidDebugInfo; if (compile_unit_die.tag_id != TAG.compile_unit) return error.InvalidDebugInfo;
@ -738,7 +798,7 @@ pub const DwarfInfo = struct {
} }
}; };
try di.compile_unit_list.append(CompileUnit{ try di.compile_unit_list.append(allocator, CompileUnit{
.version = version, .version = version,
.is_64 = is_64, .is_64 = is_64,
.pc_range = pc_range, .pc_range = pc_range,
@ -797,27 +857,33 @@ pub const DwarfInfo = struct {
/// Gets an already existing AbbrevTable given the abbrev_offset, or if not found, /// Gets an already existing AbbrevTable given the abbrev_offset, or if not found,
/// seeks in the stream and parses it. /// seeks in the stream and parses it.
fn getAbbrevTable(di: *DwarfInfo, abbrev_offset: u64) !*const AbbrevTable { fn getAbbrevTable(di: *DwarfInfo, allocator: mem.Allocator, abbrev_offset: u64) !*const AbbrevTable {
for (di.abbrev_table_list.items) |*header| { for (di.abbrev_table_list.items) |*header| {
if (header.offset == abbrev_offset) { if (header.offset == abbrev_offset) {
return &header.table; return &header.table;
} }
} }
try di.abbrev_table_list.append(AbbrevTableHeader{ try di.abbrev_table_list.append(allocator, AbbrevTableHeader{
.offset = abbrev_offset, .offset = abbrev_offset,
.table = try di.parseAbbrevTable(abbrev_offset), .table = try di.parseAbbrevTable(allocator, abbrev_offset),
}); });
return &di.abbrev_table_list.items[di.abbrev_table_list.items.len - 1].table; return &di.abbrev_table_list.items[di.abbrev_table_list.items.len - 1].table;
} }
fn parseAbbrevTable(di: *DwarfInfo, offset: u64) !AbbrevTable { fn parseAbbrevTable(di: *DwarfInfo, allocator: mem.Allocator, offset: u64) !AbbrevTable {
var stream = io.fixedBufferStream(di.debug_abbrev); var stream = io.fixedBufferStream(di.debug_abbrev);
const in = &stream.reader(); const in = &stream.reader();
const seekable = &stream.seekableStream(); const seekable = &stream.seekableStream();
try seekable.seekTo(offset); try seekable.seekTo(offset);
var result = AbbrevTable.init(di.allocator()); var result = AbbrevTable.init(allocator);
errdefer result.deinit(); errdefer {
for (result.items) |*entry| {
entry.attrs.deinit();
}
result.deinit();
}
while (true) { while (true) {
const abbrev_code = try leb.readULEB128(u64, in); const abbrev_code = try leb.readULEB128(u64, in);
if (abbrev_code == 0) return result; if (abbrev_code == 0) return result;
@ -825,7 +891,7 @@ pub const DwarfInfo = struct {
.abbrev_code = abbrev_code, .abbrev_code = abbrev_code,
.tag_id = try leb.readULEB128(u64, in), .tag_id = try leb.readULEB128(u64, in),
.has_children = (try in.readByte()) == CHILDREN.yes, .has_children = (try in.readByte()) == CHILDREN.yes,
.attrs = ArrayList(AbbrevAttr).init(di.allocator()), .attrs = std.ArrayList(AbbrevAttr).init(allocator),
}); });
const attrs = &result.items[result.items.len - 1].attrs; const attrs = &result.items[result.items.len - 1].attrs;
@ -844,21 +910,34 @@ pub const DwarfInfo = struct {
} }
} }
fn parseDie(di: *DwarfInfo, in_stream: anytype, abbrev_table: *const AbbrevTable, is_64: bool) !?Die { fn parseDie(
di: *DwarfInfo,
allocator: mem.Allocator,
in_stream: anytype,
abbrev_table: *const AbbrevTable,
is_64: bool,
) !?Die {
const abbrev_code = try leb.readULEB128(u64, in_stream); const abbrev_code = try leb.readULEB128(u64, in_stream);
if (abbrev_code == 0) return null; if (abbrev_code == 0) return null;
const table_entry = getAbbrevTableEntry(abbrev_table, abbrev_code) orelse return error.InvalidDebugInfo; const table_entry = getAbbrevTableEntry(abbrev_table, abbrev_code) orelse return error.InvalidDebugInfo;
var result = Die{ var result = Die{
// Lives as long as the Die.
.arena = std.heap.ArenaAllocator.init(allocator),
.tag_id = table_entry.tag_id, .tag_id = table_entry.tag_id,
.has_children = table_entry.has_children, .has_children = table_entry.has_children,
.attrs = ArrayList(Die.Attr).init(di.allocator()),
}; };
try result.attrs.resize(table_entry.attrs.items.len); try result.attrs.resize(allocator, table_entry.attrs.items.len);
for (table_entry.attrs.items) |attr, i| { for (table_entry.attrs.items) |attr, i| {
result.attrs.items[i] = Die.Attr{ result.attrs.items[i] = Die.Attr{
.id = attr.attr_id, .id = attr.attr_id,
.value = try parseFormValue(di.allocator(), in_stream, attr.form_id, di.endian, is_64), .value = try parseFormValue(
result.arena.allocator(),
in_stream,
attr.form_id,
di.endian,
is_64,
),
}; };
if (attr.form_id == FORM.implicit_const) { if (attr.form_id == FORM.implicit_const) {
result.attrs.items[i].value.Const.payload = @bitCast(u64, attr.payload); result.attrs.items[i].value.Const.payload = @bitCast(u64, attr.payload);
@ -867,7 +946,12 @@ pub const DwarfInfo = struct {
return result; return result;
} }
pub fn getLineNumberInfo(di: *DwarfInfo, compile_unit: CompileUnit, target_address: u64) !debug.LineInfo { pub fn getLineNumberInfo(
di: *DwarfInfo,
allocator: mem.Allocator,
compile_unit: CompileUnit,
target_address: u64,
) !debug.LineInfo {
var stream = io.fixedBufferStream(di.debug_line); var stream = io.fixedBufferStream(di.debug_line);
const in = &stream.reader(); const in = &stream.reader();
const seekable = &stream.seekableStream(); const seekable = &stream.seekableStream();
@ -906,8 +990,8 @@ pub const DwarfInfo = struct {
const opcode_base = try in.readByte(); const opcode_base = try in.readByte();
const standard_opcode_lengths = try di.allocator().alloc(u8, opcode_base - 1); const standard_opcode_lengths = try allocator.alloc(u8, opcode_base - 1);
defer di.allocator().free(standard_opcode_lengths); defer allocator.free(standard_opcode_lengths);
{ {
var i: usize = 0; var i: usize = 0;
@ -916,19 +1000,28 @@ pub const DwarfInfo = struct {
} }
} }
var include_directories = ArrayList([]const u8).init(di.allocator()); var tmp_arena = std.heap.ArenaAllocator.init(allocator);
defer tmp_arena.deinit();
const arena = tmp_arena.allocator();
var include_directories = std.ArrayList([]const u8).init(arena);
try include_directories.append(compile_unit_cwd); try include_directories.append(compile_unit_cwd);
while (true) { while (true) {
const dir = try in.readUntilDelimiterAlloc(di.allocator(), 0, math.maxInt(usize)); const dir = try in.readUntilDelimiterAlloc(arena, 0, math.maxInt(usize));
if (dir.len == 0) break; if (dir.len == 0) break;
try include_directories.append(dir); try include_directories.append(dir);
} }
var file_entries = ArrayList(FileEntry).init(di.allocator()); var file_entries = std.ArrayList(FileEntry).init(arena);
var prog = LineNumberProgram.init(default_is_stmt, include_directories.items, &file_entries, target_address); var prog = LineNumberProgram.init(
default_is_stmt,
include_directories.items,
target_address,
);
while (true) { while (true) {
const file_name = try in.readUntilDelimiterAlloc(di.allocator(), 0, math.maxInt(usize)); const file_name = try in.readUntilDelimiterAlloc(arena, 0, math.maxInt(usize));
if (file_name.len == 0) break; if (file_name.len == 0) break;
const dir_index = try leb.readULEB128(usize, in); const dir_index = try leb.readULEB128(usize, in);
const mtime = try leb.readULEB128(usize, in); const mtime = try leb.readULEB128(usize, in);
@ -955,7 +1048,7 @@ pub const DwarfInfo = struct {
switch (sub_op) { switch (sub_op) {
LNE.end_sequence => { LNE.end_sequence => {
prog.end_sequence = true; prog.end_sequence = true;
if (try prog.checkLineMatch()) |info| return info; if (try prog.checkLineMatch(allocator, file_entries.items)) |info| return info;
prog.reset(); prog.reset();
}, },
LNE.set_address => { LNE.set_address => {
@ -963,7 +1056,7 @@ pub const DwarfInfo = struct {
prog.address = addr; prog.address = addr;
}, },
LNE.define_file => { LNE.define_file => {
const file_name = try in.readUntilDelimiterAlloc(di.allocator(), 0, math.maxInt(usize)); const file_name = try in.readUntilDelimiterAlloc(arena, 0, math.maxInt(usize));
const dir_index = try leb.readULEB128(usize, in); const dir_index = try leb.readULEB128(usize, in);
const mtime = try leb.readULEB128(usize, in); const mtime = try leb.readULEB128(usize, in);
const len_bytes = try leb.readULEB128(usize, in); const len_bytes = try leb.readULEB128(usize, in);
@ -986,12 +1079,12 @@ pub const DwarfInfo = struct {
const inc_line = @as(i32, line_base) + @as(i32, adjusted_opcode % line_range); const inc_line = @as(i32, line_base) + @as(i32, adjusted_opcode % line_range);
prog.line += inc_line; prog.line += inc_line;
prog.address += inc_addr; prog.address += inc_addr;
if (try prog.checkLineMatch()) |info| return info; if (try prog.checkLineMatch(allocator, file_entries.items)) |info| return info;
prog.basic_block = false; prog.basic_block = false;
} else { } else {
switch (opcode) { switch (opcode) {
LNS.copy => { LNS.copy => {
if (try prog.checkLineMatch()) |info| return info; if (try prog.checkLineMatch(allocator, file_entries.items)) |info| return info;
prog.basic_block = false; prog.basic_block = false;
}, },
LNS.advance_pc => { LNS.advance_pc => {
@ -1068,13 +1161,8 @@ pub const DwarfInfo = struct {
}; };
/// Initialize DWARF info. The caller has the responsibility to initialize most /// Initialize DWARF info. The caller has the responsibility to initialize most
/// the DwarfInfo fields before calling. These fields can be left undefined: /// the DwarfInfo fields before calling.
/// * abbrev_table_list
/// * compile_unit_list
pub fn openDwarfDebugInfo(di: *DwarfInfo, allocator: mem.Allocator) !void { pub fn openDwarfDebugInfo(di: *DwarfInfo, allocator: mem.Allocator) !void {
di.abbrev_table_list = ArrayList(AbbrevTableHeader).init(allocator); try di.scanAllFunctions(allocator);
di.compile_unit_list = ArrayList(CompileUnit).init(allocator); try di.scanAllCompileUnits(allocator);
di.func_list = ArrayList(Func).init(allocator);
try di.scanAllFunctions();
try di.scanAllCompileUnits();
} }

View File

@ -131,9 +131,7 @@ const DebugInfo = struct {
allocator.free(self.debug_line); allocator.free(self.debug_line);
allocator.free(self.debug_line_str); allocator.free(self.debug_line_str);
allocator.free(self.debug_ranges); allocator.free(self.debug_ranges);
self.inner.abbrev_table_list.deinit(); self.inner.deinit(allocator);
self.inner.compile_unit_list.deinit();
self.inner.func_list.deinit();
} }
}; };