summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/cli/show.zig25
-rw-r--r--src/trace/Debugger.zig20
-rw-r--r--src/trace/Symbols.zig63
-rw-r--r--src/trace/debugger_test.zig99
-rw-r--r--src/trace/event.zig6
-rw-r--r--src/trace/heap_test.zig2
-rw-r--r--src/trace/telescope.zig10
7 files changed, 214 insertions, 11 deletions
diff --git a/src/cli/show.zig b/src/cli/show.zig
index 7019113..ef05e7a 100644
--- a/src/cli/show.zig
+++ b/src/cli/show.zig
@@ -135,7 +135,16 @@ pub fn state(arena: std.mem.Allocator, trace: *zt.Trace, index: u64, w: *std.Io.
if (chain.len > 1) try w.writeAll("\n");
if (chain.len > 1) for (chain, 0..) |pc, depth| {
var text: [256]u8 = undefined;
- try w.print(" #{d} {x} {s}\n", .{ depth, pc, (try m.string(pc, &text)) orelse "" });
+ try w.print(" #{d} {x} {s}", .{ depth, pc, (try m.string(pc, &text)) orelse "" });
+ // Its line, as `bt` gives it: the pc's, a caller's call's (recorded `back` from where it returns).
+ var at = pc;
+ if (depth > 0) if (try zt.telescope.instruction(m, pc, &text)) |after| {
+ at -= after.back;
+ };
+ if (try zt.telescope.instruction(m, at, &text)) |insn| if (insn.line) |l| {
+ try w.print(" ({s}:{d})", .{ std.fs.path.basename(try name(m, arena, l.file)), l.line });
+ };
+ try w.writeAll("\n");
};
}
for (try m.stack(arena, tid), 0..) |frame, depth| {
@@ -169,10 +178,22 @@ pub fn state(arena: std.mem.Allocator, trace: *zt.Trace, index: u64, w: *std.Io.
const mappings = try m.mappings(arena);
if (mappings.len > 0) try w.writeAll("\nmappings\n");
+ var last: []const u8 = "";
for (mappings) |region| {
var path: []const u8 = "";
var path_buf: [std.Io.Dir.max_path_bytes]u8 = undefined;
_ = try m.mapping(region.addr, &path, &path_buf);
- try w.print(" {x:0>12}-{x:0>12} {s}\n", .{ region.addr, region.addr + region.len, path });
+ try w.print(" {x:0>12}-{x:0>12} {s}", .{ region.addr, region.addr + region.len, path });
+ // A module, once: which build it is, and whether its names come from its own debug info.
+ if (!std.mem.eql(u8, path, last)) if (zt.telescope.module(m, path)) |module| {
+ if (module.build_id.len > 0) try w.print(" build-id {x},", .{module.build_id}) else try w.writeAll(" no build id,");
+ try w.print(" debug info {s}", .{switch (module.debug) {
+ .none => "none",
+ .own => "in the file",
+ .separate => "separate, of the same build",
+ } });
+ };
+ last = try arena.dupe(u8, path);
+ try w.writeAll("\n");
}
}
diff --git a/src/trace/Debugger.zig b/src/trace/Debugger.zig
index d312dec..2cef5ca 100644
--- a/src/trace/Debugger.zig
+++ b/src/trace/Debugger.zig
@@ -794,6 +794,20 @@ fn emitTelescope(d: *Debugger) !void {
}
}
+/// A module's build id and where its debug info came from, recorded once as
+/// the string `event.moduleId` of its path: a byte (`Symbols.Debug`), then
+/// the id: which library a recording is of, and whether its names and
+/// lines come from debug info that is its own.
+fn emitModule(d: *Debugger, m: *const Symbols.Module) !void {
+ const id = event.moduleId(m.path);
+ if ((try d.described.getOrPut(d.gpa, id)).found_existing) return;
+ var text: [1 + 64]u8 = undefined;
+ text[0] = @intFromEnum(m.debug);
+ const n = @min(m.build_id.len, 64);
+ @memcpy(text[1..][0..n], m.build_id[0..n]);
+ try d.emit(.{ .kind = .string, .a = .{ id, 0, 0, 0, 0, 0 } }, text[0 .. 1 + n]);
+}
+
/// The call that returns to `ret`, the instruction before it and the one
/// at `ret`, recorded as instructions, once: a caller's rows in a descent,
/// and an outer frame is on its call's line, `back` from `ret`.
@@ -893,8 +907,9 @@ pub fn decode(d: *Debugger, nr: u16) ![]const u8 {
/// The string id of a line's file: its path, recorded the first time.
fn fileId(d: *Debugger, l: Symbols.Line) !u64 {
if (d.file_ids.get(l.key)) |id| return id;
- // A file compiled by its full path names itself whole.
- const path = if (std.fs.path.isAbsolute(l.name)) try d.gpa.dupe(u8, l.name) else try std.fs.path.join(d.gpa, &.{ l.dir, l.name });
+ // As the compilation saw it: its directory, the file's, its name, each
+ // relative to the one before (glibc's: /usr/src/debug/glibc/glibc/csu/../sysdeps/...).
+ const path = try std.fs.path.resolveAlloc(d.gpa, &.{ l.base, l.dir, l.name });
const named = d.files.getOrPut(d.gpa, path) catch |err| {
d.gpa.free(path);
return err;
@@ -922,6 +937,7 @@ pub fn refreshMaps(d: *Debugger) !void {
defer d.syms.free(old);
d.unwound.clearRetainingCapacity();
if (d.sink == null) return;
+ for (d.syms.modules.items) |*m| try d.emitModule(m);
const now = d.syms.maps.list;
for (old.list) |o| {
for (now) |m| {
diff --git a/src/trace/Symbols.zig b/src/trace/Symbols.zig
index 1459503..da13047 100644
--- a/src/trace/Symbols.zig
+++ b/src/trace/Symbols.zig
@@ -42,9 +42,15 @@ pub const Maps = struct {
pub const Symbol = struct { name: []const u8, addr: u64, size: u64 };
+/// Where a module's debug info is: none, in the file, or in a separate
+/// file whose build id is the module's (or, without one, whose CRC is what
+/// its .gnu_debuglink says).
+pub const Debug = enum(u8) { none, own, separate };
+
/// A row of a line table. `key` is the same for the same file of the same
-/// compilation unit, and `dir` and `name` together are its path.
-pub const Line = struct { key: usize, dir: []const u8, name: []const u8, line: u32, column: u32 };
+/// compilation unit, and `base` (the compilation's directory), `dir` and
+/// `name` together are its path, each relative to the one before it.
+pub const Line = struct { key: usize, base: []const u8 = "", dir: []const u8, name: []const u8, line: u32, column: u32 };
pub const Module = struct {
path: []const u8,
@@ -57,6 +63,9 @@ pub const Module = struct {
entry: u64 = 0,
/// Null when the file could not be read as ELF.
file: ?std.debug.ElfFile = null,
+ /// Its GNU build id (empty: none), and where its debug info came from.
+ build_id: []const u8 = "",
+ debug: Debug = .none,
/// Functions and objects by address in the file.
symbols: []Symbol = &.{},
dwarf_tried: bool = false,
@@ -184,7 +193,7 @@ fn line(table: *const Dwarf.CompileUnit.SrcLocCache, entry: Dwarf.CompileUnit.Sr
if (index >= table.files.len) return null;
const file = &table.files[index];
if (file.dir_index >= table.directories.len) return null;
- return .{ .key = @intFromPtr(file), .dir = table.directories[file.dir_index].path, .name = file.path, .line = entry.line, .column = entry.column };
+ return .{ .key = @intFromPtr(file), .base = table.directories[0].path, .dir = table.directories[file.dir_index].path, .name = file.path, .line = entry.line, .column = entry.column };
}
pub fn deinit(s: *Symbols) void {
@@ -287,10 +296,32 @@ fn load(s: *Symbols, path: []const u8) !void {
if (std.Io.Dir.cwd().openFile(s.io, path, .{})) |file| {
defer file.close(s.io);
m.file = std.debug.ElfFile.load(s.gpa, s.io, file, null, &.none) catch null;
+ // Stripped (a distribution's libc): its debug info from a separate
+ // file where one is on disk, by build id in the debuginfod client's
+ // cache or /usr/lib/debug/.build-id, or by .gnu_debuglink beside it.
+ // Never fetched.
+ // Strict: a file found by build id is used only when its own note
+ // says that id (not the cache's directory name), never half.
+ if (m.file) |*f| if (f.dwarf == null) {
+ const paths: std.debug.ElfFile.DebugInfoSearchPaths = .native(path);
+ const id = buildId(f.mapped_file);
+ if (std.debug.ElfFile.load(s.gpa, s.io, file, id, &paths)) |richer| {
+ var found = richer;
+ if (found.mapped_debug_file) |debug| if (id == null or std.mem.eql(u8, buildId(debug) orelse "", id.?)) std.mem.swap(std.debug.ElfFile, f, &found);
+ found.deinit(s.gpa);
+ } else |_| {}
+ };
} else |_| {}
if (m.file) |*f| {
errdefer f.deinit(s.gpa);
- m.symbols = readSymbols(s.gpa, f.mapped_file) catch |err| switch (err) {
+ m.build_id = buildId(f.mapped_file) orelse "";
+ m.debug = if (f.mapped_debug_file != null) .separate else if (f.dwarf != null) .own else .none;
+ // The debug file's symbol table, where there is one: every function, static ones too.
+ const debug_symbols: []Symbol = if (f.mapped_debug_file) |bytes| readSymbols(s.gpa, bytes) catch |err| switch (err) {
+ error.OutOfMemory => |e| return e,
+ else => &.{},
+ } else &.{};
+ m.symbols = if (debug_symbols.len > 0) debug_symbols else readSymbols(s.gpa, f.mapped_file) catch |err| switch (err) {
error.OutOfMemory => |e| return e,
else => &.{},
};
@@ -325,6 +356,26 @@ fn load(s: *Symbols, path: []const u8) !void {
s.modules.appendAssumeCapacity(m);
}
+/// The GNU build id a file's note gives it: what its separate debug file is found by.
+fn buildId(bytes: []const u8) ?[]const u8 {
+ var r: std.Io.Reader = .fixed(bytes);
+ const header = elf.Header.read(&r) catch return null;
+ var it = header.iterateSectionHeadersBuffer(bytes);
+ while (it.next() catch return null) |sh| {
+ if (sh.sh_type != elf.SHT_NOTE or sh.sh_offset + sh.sh_size > bytes.len) continue;
+ var notes: std.Io.Reader = .fixed(bytes[sh.sh_offset..][0..sh.sh_size]);
+ while (notes.seek + 12 <= notes.buffer.len) {
+ const name_len = notes.takeInt(u32, .little) catch return null;
+ const desc_len = notes.takeInt(u32, .little) catch return null;
+ const kind = notes.takeInt(u32, .little) catch return null;
+ const name = notes.take(std.mem.alignForward(usize, name_len, 4)) catch return null;
+ const desc = notes.take(std.mem.alignForward(usize, desc_len, 4)) catch return null;
+ if (kind == elf.NT_GNU_BUILD_ID and std.mem.eql(u8, name[0..@min(name.len, 4)], "GNU\x00")) return desc[0..desc_len];
+ }
+ }
+ return null;
+}
+
/// The file's entry point and its first loaded segment.
fn firstLoad(bytes: []const u8) ?struct { entry: u64, load: elf.Elf64.Phdr } {
var r: std.Io.Reader = .fixed(bytes);
@@ -362,7 +413,9 @@ fn readSymbols(gpa: std.mem.Allocator, bytes: []const u8) ![]Symbol {
.FUNC, .OBJECT, .GNU_IFUNC, .NOTYPE => {},
else => continue,
}
- try out.append(gpa, .{ .name = std.mem.sliceTo(names[sym.name..], 0), .addr = sym.value, .size = sym.size });
+ // A version is no part of a name: `__libc_start_main@@GLIBC_2.34`.
+ const name = std.mem.sliceTo(names[sym.name..], 0);
+ try out.append(gpa, .{ .name = name[0 .. std.mem.indexOfScalar(u8, name, '@') orelse name.len], .addr = sym.value, .size = sym.size });
}
std.mem.sortUnstable(Symbol, out.items, {}, struct {
fn lessThan(_: void, a: Symbol, b: Symbol) bool {
diff --git a/src/trace/debugger_test.zig b/src/trace/debugger_test.zig
index bd35560..ac54356 100644
--- a/src/trace/debugger_test.zig
+++ b/src/trace/debugger_test.zig
@@ -604,6 +604,101 @@ test "the same overflow built with ASan, by gcc and by clang: its shadow round t
if (built == 0) return error.SkipZigTest;
}
+fn moduleEndingIn(d: *Debugger, name: []const u8) ?*zt.Symbols.Module {
+ for (d.syms.modules.items) |*m| if (std.mem.endsWith(u8, m.path, name)) return m;
+ return null;
+}
+
+test "a stripped libc named by its separate debug info of the same build: static functions, lines, the loader's resolver" {
+ var tmp = std.testing.tmpDir(.{});
+ defer tmp.cleanup();
+ var dir_buf: [std.Io.Dir.max_path_bytes]u8 = undefined;
+ const dir = dir_buf[0..try tmp.dir.realPath(io, &dir_buf)];
+ const path = try std.fs.path.join(gpa, &.{ dir, "libc.ztrace" });
+ defer gpa.free(path);
+ var rec = try zt.Recorder.create(gpa, io, path);
+ var libc_path: [std.Io.Dir.max_path_bytes]u8 = undefined;
+ const libc_len = len: {
+ errdefer rec.close() catch {};
+ var d = try spawn(targets.hello, null, rec.sink());
+ defer d.deinit();
+ d.syscalls = .record;
+ _ = try d.breakAt(try d.resolve("main"), null);
+ _ = try d.cont();
+ const libc = moduleEndingIn(&d, "/libc.so.6").?;
+ // The debug file is in the debuginfod client's cache once something fetched it (gdb, debuginfod-find).
+ if (libc.debug != .separate) return error.SkipZigTest;
+ try std.testing.expect(libc.build_id.len == 20);
+ // What only the debug file's symbol table has: static functions, the main arena.
+ for ([_][]const u8{ "_int_malloc", "main_arena", "__libc_open64" }) |name| try std.testing.expect(d.symbol(name) != null);
+ if (moduleEndingIn(&d, "/ld-linux-x86-64.so.2").?.debug == .separate) try std.testing.expect(d.symbol("_dl_runtime_resolve_xsavec") != null);
+ // In open, called from main: every frame named, libc's with their lines.
+ _ = try d.breakAt(try d.resolve("open"), null);
+ _ = try d.cont();
+ var chain_buf: [8]u64 = undefined;
+ const chain = Debugger.backtrace(&d, &d.regs, false, &chain_buf);
+ for ([_][]const u8{ "open", "main", "__libc_start_call_main", "__libc_start_main" }, chain[0..4]) |name, pc| {
+ try std.testing.expectEqualStrings(name, named(&d, pc));
+ try std.testing.expect(d.lineAt(pc -% @intFromBool(pc != chain[0])) != null);
+ }
+ _ = try finish(&d);
+ @memcpy(libc_path[0..libc.path.len], libc.path);
+ break :len libc.path.len;
+ };
+ try rec.close();
+
+ // The recording says which libc it is of, and that its names are from its own build's debug info.
+ var trace = try zt.Trace.open(gpa, io, path);
+ defer trace.close();
+ const m = try trace.moment(trace.len - 1);
+ const libc = zt.telescope.module(m, libc_path[0..libc_len]).?;
+ try std.testing.expectEqual(zt.Symbols.Debug.separate, libc.debug);
+ try std.testing.expectEqual(@as(usize, 20), libc.build_id.len);
+ // Its source files by the paths of the build, whole: no `..` left.
+ var files: usize = 0;
+ var strings = trace.strings.valueIterator();
+ while (strings.next()) |text| if (std.mem.endsWith(u8, text.*, "/open64.c")) {
+ try std.testing.expect(std.fs.path.isAbsolute(text.*) and std.mem.indexOf(u8, text.*, "..") == null);
+ files += 1;
+ };
+ try std.testing.expectEqual(@as(usize, 1), files);
+}
+
+test "a debug file of another build, under the library's id, is not used" {
+ // Its matching debug files, to take a wrong one from: the loader's, put where libc's would be.
+ var d = try spawn(targets.hello, null, null);
+ defer d.deinit();
+ try d.toEntry();
+ _ = d.symbol("open");
+ const libc = moduleEndingIn(&d, "/libc.so.6").?;
+ const loader = moduleEndingIn(&d, "/ld-linux-x86-64.so.2").?;
+ if (libc.build_id.len == 0 or loader.debug != .separate) return error.SkipZigTest;
+ var tmp = std.testing.tmpDir(.{});
+ defer tmp.cleanup();
+ var dir_buf: [std.Io.Dir.max_path_bytes]u8 = undefined;
+ const dir = dir_buf[0..try tmp.dir.realPath(io, &dir_buf)];
+ var hex: [40]u8 = undefined;
+ const id = try std.fmt.bufPrint(&hex, "{x}", .{libc.build_id});
+ try tmp.dir.createDirPath(io, id);
+ var name: [64]u8 = undefined;
+ var wrong = try tmp.dir.createFile(io, try std.fmt.bufPrint(&name, "{s}/debuginfo", .{id}), .{});
+ try wrong.writeStreamingAll(io, loader.file.?.mapped_debug_file.?);
+ wrong.close(io);
+
+ const path = try std.fs.path.join(gpa, &.{ dir, "wrong.ztrace" });
+ defer gpa.free(path);
+ var env = std.process.Environ.Map.init(gpa);
+ defer env.deinit();
+ try env.put("DEBUGINFOD_CACHE_PATH", dir);
+ try std.testing.expectEqual(@as(?u8, 0), exited(try std.process.run(gpa, io, .{ .argv = &.{ targets.ztrace, "record", "--by", "break", "-b", "main", "-o", path, "--", targets.hello }, .environ_map = &env })));
+ var trace = try zt.Trace.open(gpa, io, path);
+ defer trace.close();
+ const m = try trace.moment(trace.len - 1);
+ const said = zt.telescope.module(m, libc.path).?;
+ try std.testing.expectEqualSlices(u8, libc.build_id, said.build_id);
+ try std.testing.expectEqual(zt.Symbols.Debug.none, said.debug);
+}
+
test "step walks the source lines, in and out of calls, as gdb does" {
var d = try spawn(targets.list, "2", null);
defer d.deinit();
@@ -934,10 +1029,10 @@ test "a watch sends a word when it changes, and each moment keeps the region as
}
try std.testing.expectEqual(@as(usize, 9), writes);
try std.testing.expect(log.count(.step) > 100);
- // Lines come from one file here, defined once; the one other string is where the stack began.
+ // Lines come from one file here, defined once; the other strings are where the stack began and the program's build.
try std.testing.expect(log.count(.line) > 100);
for (log.events.items) |e| {
- if (e.ev.kind == .string and e.ev.a[0] != zt.event.frameId(0)) try std.testing.expect(std.mem.endsWith(u8, e.payload, ".zig"));
+ if (e.ev.kind == .string and e.ev.a[0] != zt.event.frameId(0) and e.ev.a[0] != zt.event.moduleId(targets.graph)) try std.testing.expect(std.mem.endsWith(u8, e.payload, ".zig"));
}
var trace = try zt.Trace.open(gpa, io, path);
diff --git a/src/trace/event.zig b/src/trace/event.zig
index 24caff4..1447c8e 100644
--- a/src/trace/event.zig
+++ b/src/trace/event.zig
@@ -198,6 +198,12 @@ pub fn frameId(addr: u64) u64 {
return addr | 1 << 61;
}
+/// The id of the string that tells of the file mapped at `path`: its build
+/// id and where its debug info came from (`Debugger.emitModule`).
+pub fn moduleId(path: []const u8) u64 {
+ return std.hash.Wyhash.hash(1, path) | 1 << 63;
+}
+
/// The id of the string that holds the basic blocks of the function that
/// begins at `addr` (`Debugger.emitBlocks`), found as a frame's is: the
/// greatest at or below the pc's. Bits 61 and 62 both.
diff --git a/src/trace/heap_test.zig b/src/trace/heap_test.zig
index aca05b0..f7919e2 100644
--- a/src/trace/heap_test.zig
+++ b/src/trace/heap_test.zig
@@ -68,6 +68,8 @@ fn record(arena: std.mem.Allocator, path: []const u8, argv: []const []const u8)
else => {},
};
try std.testing.expectEqual(@as(usize, 5), n);
+ // Where the symbol main_arena is known (the C library's own, or its separate debug info's), the arena is it.
+ if (dbg.syms.find("main_arena")) |sym| try std.testing.expectEqual(sym.addr, dbg.heap.?.glibc.?.arena);
try rec.close();
return stops;
}
diff --git a/src/trace/telescope.zig b/src/trace/telescope.zig
index 006bb22..f7e4a3d 100644
--- a/src/trace/telescope.zig
+++ b/src/trace/telescope.zig
@@ -359,6 +359,16 @@ pub fn shadow(m: Moment, addr: u64) !?struct { addr: u64, value: ?u8 } {
return .{ .addr = at, .value = if (known[0]) byte[0] else null };
}
+/// What the recording says of the file mapped at `path` (`Debugger.emitModule`):
+/// its build id, and where its debug info came from: `separate`, a debug
+/// file of the same build id (its names and lines are the library's own),
+/// `own`, in the file, or `none`. Null for a path that is no module.
+pub fn module(m: Moment, path: []const u8) ?struct { build_id: []const u8, debug: Symbols.Debug } {
+ const text = m.trace.strings.get(event.moduleId(path)) orelse return null;
+ if (text.len < 1) return null;
+ return .{ .build_id = text[1..], .debug = std.enums.fromInt(Symbols.Debug, text[0]) orelse .none };
+}
+
/// A GOT slot at a moment: what it holds, and whether that is still the way
/// back into the PLT (`lazy`: the next call through it runs the loader's
/// resolver, which binds it) rather than the function. Null where the