summaryrefslogtreecommitdiff
path: root/src/trace/debugger_test.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/trace/debugger_test.zig')
-rw-r--r--src/trace/debugger_test.zig99
1 files changed, 97 insertions, 2 deletions
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);