diff options
Diffstat (limited to 'src/trace')
| -rw-r--r-- | src/trace/Debugger.zig | 52 | ||||
| -rw-r--r-- | src/trace/Symbols.zig | 193 | ||||
| -rw-r--r-- | src/trace/debugger_test.zig | 200 | ||||
| -rw-r--r-- | src/trace/heap.zig | 53 | ||||
| -rw-r--r-- | src/trace/heap_test.zig | 7 | ||||
| -rw-r--r-- | src/trace/telescope.zig | 37 |
6 files changed, 503 insertions, 39 deletions
diff --git a/src/trace/Debugger.zig b/src/trace/Debugger.zig index f4f945b..886b0fc 100644 --- a/src/trace/Debugger.zig +++ b/src/trace/Debugger.zig @@ -219,9 +219,28 @@ pub fn cont(d: *Debugger) !Stop { return d.report(try d.run(d.free())); } -/// One instruction. +/// One instruction; a call or jump into the vDSO runs to its return, as +/// one (`stepOne`). pub fn stepi(d: *Debugger) !Stop { - return d.report(try d.run(P.SINGLESTEP)); + return d.report(try d.stepOne()); +} + +/// One instruction, but one that enters the vDSO (the kernel's code mapped +/// into the process: `clock_gettime`, `gettimeofday`) from outside runs it +/// through to where it returns, the return address on top of the stack: +/// its loops retry while the kernel's time data changes under them, which +/// a stop at each instruction makes them do for as long as stops are slow. +fn stepOne(d: *Debugger) !Stop { + const from = d.regs.rip; + const stop = try d.run(P.SINGLESTEP); + if (stop != .step or !d.inVdso(d.regs.rip) or d.inVdso(from)) return stop; + var word: [8]u8 = undefined; + if (d.read(d.regs.rsp, &word) != 8) return stop; + return d.runTo(std.mem.readInt(u64, &word, .little), d.regs.rsp); +} + +fn inVdso(d: *const Debugger, addr: u64) bool { + return if (d.syms.mappingAt(addr)) |m| std.mem.eql(u8, m.name, "[vdso]") else false; } /// To the next source line: instructions until the line is another, a call @@ -231,7 +250,7 @@ pub fn step(d: *Debugger) !Stop { const tid = d.proc.tid; while (true) { const before = d.regs; - var stop = try d.run(P.SINGLESTEP); + var stop = try d.stepOne(); if (stop == .step and d.proc.tid == tid) { if (d.lineAt(d.regs.rip)) |here| { if (from == null or from.?.key != here.key or from.?.line != here.line) return d.report(.step); @@ -538,6 +557,8 @@ fn emitCode(d: *Debugger) !void { const target: ?u64 = if (from) |addr| (if (d.read(addr, &word) == 8) std.mem.readInt(u64, &word, .little) else null) else b.target; if (b.kind != 0) if (target) |to| { try d.nameAddress(to); + // Through memory: the slot it reads, where it has a name ("open@got"). + if (b.indirect != 0 and b.kind != 3) if (from) |slot| if (d.syms.symbolAt(slot) != null) try d.nameAddress(slot); try d.emit(.{ .kind = .branch, .aux = b.kind, .a = .{ to, b.taken, d.regs.rip, 0, 0, 0 } }, ""); }; } @@ -643,12 +664,14 @@ fn emitTelescope(d: *Debugger) !void { } } -/// The call that returns to `ret`, and the instruction there, recorded as -/// instructions: an outer frame is on its call's line, `back` from `ret`. +/// 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`. fn emitCall(d: *Debugger, ret: u64) !void { const insnAt = d.disasm orelse return; - if (d.code_len.contains(ret)) return; - const sym = d.syms.symbolAt(ret) orelse return; + if ((try d.described.getOrPut(d.gpa, event.codeId(ret))).found_existing) return; + // Where its function begins: its symbol's, else its call frame information's (a stripped library's). + const sym = d.syms.symbolAt(ret) orelse if (d.syms.functionAt(ret -% 1)) |f| Symbols.Symbol{ .name = "", .addr = f.start, .size = f.len } else return; if (sym.size == 0 or sym.size > 1 << 16 or ret == sym.addr) return; const starts = try d.instructions(sym, insnAt); const k = std.sort.binarySearch(u32, starts, @as(u32, @intCast(ret - sym.addr)), struct { @@ -657,8 +680,7 @@ fn emitCall(d: *Debugger, ret: u64) !void { } }.order) orelse return; if (k == 0) return; - _ = try d.emitInstruction(insnAt, sym.addr + starts[k - 1], if (k > 1) @intCast(starts[k - 1] - starts[k - 2]) else 0); - _ = try d.emitInstruction(insnAt, ret, @intCast(starts[k] - starts[k - 1])); + for (k -| 2..k + 1) |n| _ = try d.emitInstruction(insnAt, sym.addr + starts[n], if (n > 0) @intCast(starts[n] - starts[n - 1]) else 0); } /// Follows `value` as a pointer: a pointer into code gets its name and @@ -678,12 +700,18 @@ fn chase(d: *Debugger, value: u64) !void { } } -/// The string whose id is `addr`: its name ("main+4"), recorded once. +/// The string whose id is `addr`: its name ("main+4"), recorded once. An +/// address in a mapping not read yet (the syscalls that made it not +/// followed) has its mappings read first, and one with no name is tried +/// again the next time. fn nameAddress(d: *Debugger, addr: u64) !void { - if ((try d.described.getOrPut(d.gpa, addr)).found_existing) return; + if (d.described.contains(addr)) return; + d.know(addr); var buf: [256]u8 = undefined; var w: std.Io.Writer = .fixed(&buf); - if (d.syms.label(&w, addr) catch false) try d.emit(.{ .kind = .string, .a = .{ addr, 0, 0, 0, 0, 0 } }, w.buffered()); + if (!(d.syms.label(&w, addr) catch false)) return; + try d.described.put(d.gpa, addr, {}); + try d.emit(.{ .kind = .string, .a = .{ addr, 0, 0, 0, 0, 0 } }, w.buffered()); } fn emitStop(d: *Debugger, stop: Stop) !void { diff --git a/src/trace/Symbols.zig b/src/trace/Symbols.zig index 3ebd418..1459503 100644 --- a/src/trace/Symbols.zig +++ b/src/trace/Symbols.zig @@ -19,6 +19,9 @@ row: struct { start: u64 = 0, len: u64 = 0, line: ?Line = null } = .{}, /// Names are then shown and found so, without their parameters: /// "v8::internal::Heap::CollectGarbage" for "_ZN2v88internal4Heap14Coll...". demangle: ?*const fn (name: []const u8, buf: []u8) ?[]const u8 = null, +/// The names the modules' relocations import: what a caller calls a +/// function that has several (`open`, not `__open64`). +imported: std.StringHashMapUnmanaged(void) = .empty, pub const Mapping = struct { start: u64, @@ -66,9 +69,14 @@ pub const Module = struct { unwinds: [2]Dwarf.Unwind = undefined, unwinds_len: u8 = 0, unwinds_tried: bool = false, + /// .eh_frame with the zero entry that ends it, where the file has none (ld.so). + eh_frame: []u8 = &.{}, /// Each function's rows of it, decoded on first use, by its first /// address in the file. cfi: std.AutoHashMapUnmanaged(u64, struct { len: u64, rows: []Row }) = .empty, + /// The names made for it (`readPlt`'s), and its PLT's sections. + names: std.heap.ArenaAllocator.State = .{}, + plts: [3]Plt = @splat(.{}), /// The call frame information, prepared on first use. fn unwindTables(m: *Module, gpa: std.mem.Allocator) []Dwarf.Unwind { @@ -80,7 +88,16 @@ pub const Module = struct { var u: Dwarf.Unwind = .initSection(id, s.vaddr, s.bytes); u.prepare(gpa, 8, .little, true, false) catch { u.deinit(gpa); - continue; + // ld.so's .eh_frame ends without the zero entry the unwinder needs: given one. + if (id != .eh_frame) continue; + m.eh_frame = gpa.alloc(u8, s.bytes.len + 4) catch continue; + @memcpy(m.eh_frame[0..s.bytes.len], s.bytes); + @memset(m.eh_frame[s.bytes.len..], 0); + u = .initSection(id, s.vaddr, m.eh_frame); + u.prepare(gpa, 8, .little, true, false) catch { + u.deinit(gpa); + continue; + }; }; m.unwinds[m.unwinds_len] = u; m.unwinds_len += 1; @@ -88,9 +105,12 @@ pub const Module = struct { return m.unwinds[0..m.unwinds_len]; } + /// A function's rows, where it begins (in the file) and how long it is. + const Rows = struct { start: u64, len: u64, rows: []const Row }; + /// The function whose call frame information holds `pc`, an address in - /// the file: where it begins (in the file) and its rows. - fn rowsAt(m: *Module, gpa: std.mem.Allocator, pc: u64) ?struct { start: u64, len: u64, rows: []const Row } { + /// the file: by the unwind tables, else (a PLT entry) by its code. + fn rowsAt(m: *Module, gpa: std.mem.Allocator, pc: u64) ?Rows { for (m.unwindTables(gpa)) |*u| { const at = (u.lookupPc(pc, 8, .little) catch null) orelse continue; const cie, const fde = u.getFde(at, .little) catch continue; @@ -101,7 +121,29 @@ pub const Module = struct { if (entry.value_ptr.rows.len == 0) return null; return .{ .start = fde.pc_begin, .len = entry.value_ptr.len, .rows = entry.value_ptr.rows }; } - return null; + return m.pltRows(gpa, pc); + } + + /// A PLT entry's rows where the unwind tables have none (Zig's linker + /// writes none), from its code, as GNU ld's expression says them: + /// rsp + 8, and rsp + 16 after its push; the first entry's rsp + 16, and + /// rsp + 24 after its push. + fn pltRows(m: *Module, gpa: std.mem.Allocator, pc: u64) ?Rows { + const file = m.file orelse return null; + const p = for (m.plts) |p| { + if (pc >= p.addr and pc - p.addr < p.size) break p; + } else return null; + const entry = p.addr + (pc - p.addr) / p.entsize * p.entsize; + const cached = m.cfi.getOrPut(gpa, entry) catch return null; + if (!cached.found_existing) { + const s = stub(file.mapped_file[p.offset + (entry - p.addr) ..][0..@min(p.entsize, p.addr + p.size - entry)], entry); + const returns: [saved_registers.len]i8 = .{ 0, 0, 0, 0, 0, 0, -1 }; + const cfa: i32 = if (s.first) 16 else 8; + const rows = [_]Row{ .{ .offset = 0, .cfa = cfa, .cfa_reg = 7, .saved = returns }, .{ .offset = s.pushed orelse 0, .cfa = cfa + 8, .cfa_reg = 7, .saved = returns } }; + cached.value_ptr.* = .{ .len = p.entsize, .rows = gpa.dupe(Row, rows[0 .. @as(usize, 1) + @intFromBool(s.pushed != null)]) catch &.{} }; + } + if (cached.value_ptr.rows.len == 0) return null; + return .{ .start = entry, .len = cached.value_ptr.len, .rows = cached.value_ptr.rows }; } /// The line tables, opened on first use. Null without debug info. @@ -154,10 +196,13 @@ pub fn deinit(s: *Symbols) void { var decoded = m.cfi.valueIterator(); while (decoded.next()) |f| s.gpa.free(f.rows); m.cfi.deinit(s.gpa); + m.names.promote(s.gpa).deinit(); + s.gpa.free(m.eh_frame); s.gpa.free(m.symbols); s.gpa.free(m.path); } s.modules.deinit(s.gpa); + s.imported.deinit(s.gpa); s.free(s.maps); } @@ -249,6 +294,23 @@ fn load(s: *Symbols, path: []const u8) !void { error.OutOfMemory => |e| return e, else => &.{}, }; + // And its PLT's stubs and GOT's slots, named. + var names = m.names.promote(s.gpa); + defer m.names = names.state; + const made = readPlt(s.gpa, names.allocator(), f.mapped_file, m.symbols, &s.imported, &m.plts) catch |err| switch (err) { + error.OutOfMemory => |e| return e, + else => &.{}, + }; + if (made.len > 0) { + const before = m.symbols.len; + m.symbols = try s.gpa.realloc(m.symbols, before + made.len); + @memcpy(m.symbols[before..], made); + std.mem.sortUnstable(Symbol, m.symbols, {}, struct { + fn lessThan(_: void, a: Symbol, b: Symbol) bool { + return a.addr < b.addr; + } + }.lessThan); + } if (firstLoad(f.mapped_file)) |first| { m.entry = first.entry; // Where the file's first segment landed tells where all of it did. @@ -344,11 +406,132 @@ pub fn symbolAt(s: *Symbols, addr: u64) ?Symbol { var i = after; while (i > 0 and after - i < 8) : (i -= 1) { const sym = m.symbols[i - 1]; - if (pc - sym.addr < @max(sym.size, 1)) return .{ .name = sym.name, .addr = sym.addr + m.bias, .size = sym.size }; + if (pc - sym.addr >= @max(sym.size, 1)) continue; + // Of the names one place has, the one a module calls it by. + var j = i; + const name = while (j > 0 and m.symbols[j - 1].addr == sym.addr) : (j -= 1) { + if (s.imported.contains(m.symbols[j - 1].name)) break m.symbols[j - 1].name; + } else sym.name; + return .{ .name = name, .addr = sym.addr + m.bias, .size = sym.size }; } return null; } +/// A PLT's section, as addresses in the file: `entsize` bytes an entry. +const Plt = struct { addr: u64 = 0, offset: u64 = 0, size: u64 = 0, entsize: u64 = 16 }; + +/// The PLT's stubs and the GOT's slots of a file, as symbols named for the +/// function they lead to, from its relocations and dynamic symbols: +/// `open@plt`, `open@got`; an IFUNC's (an IRELATIVE relocation, which +/// names none) by the function its resolver is (`strlen@plt`). A stub is +/// known by the slot its `jmp` reads, or, in a PLT whose stubs jump +/// elsewhere (`.plt.sec`'s other half), by the relocation it pushes. Puts +/// the names the relocations import in `imported`, and the PLT's sections +/// in `plts`; the names are `arena`'s. +fn readPlt(gpa: std.mem.Allocator, arena: std.mem.Allocator, bytes: []const u8, symbols: []const Symbol, imported: *std.StringHashMapUnmanaged(void), plts: *[3]Plt) ![]Symbol { + var r: std.Io.Reader = .fixed(bytes); + const header = try elf.Header.read(&r); + if (!header.is_64 or header.endian != .little) return &.{}; + const names_at = header.shoff + @as(u64, header.shstrndx) * header.shentsize; + if (names_at + @sizeOf(elf.Elf64_Shdr) > bytes.len) return error.TruncatedElfFile; + const names_sh = std.mem.bytesToValue(elf.Elf64_Shdr, bytes[names_at..][0..@sizeOf(elf.Elf64_Shdr)]); + if (names_sh.sh_offset + names_sh.sh_size > bytes.len) return error.TruncatedElfFile; + const section_names = bytes[names_sh.sh_offset..][0..names_sh.sh_size]; + var relas: [2]?elf.Elf64_Shdr = @splat(null); + var dynsym: ?elf.Elf64_Shdr = null; + var it = header.iterateSectionHeadersBuffer(bytes); + while (try it.next()) |sh| { + if (sh.sh_name >= section_names.len or sh.sh_offset + sh.sh_size > bytes.len) continue; + const name = std.mem.sliceTo(section_names[sh.sh_name..], 0); + if (sh.sh_type == elf.SHT_DYNSYM) dynsym = sh; + if (sh.sh_type == elf.SHT_RELA) for ([_][]const u8{ ".rela.plt", ".rela.dyn" }, &relas) |want, *slot| { + if (std.mem.eql(u8, name, want)) slot.* = sh; + }; + for ([_][]const u8{ ".plt", ".plt.sec", ".plt.got" }, plts) |want, *p| { + if (std.mem.eql(u8, name, want) and sh.sh_type == elf.SHT_PROGBITS) p.* = .{ .addr = sh.sh_addr, .offset = sh.sh_offset, .size = sh.sh_size, .entsize = if (sh.sh_entsize != 0) sh.sh_entsize else 16 }; + } + } + const syms_sh = dynsym orelse return &.{}; + const strs_at = header.shoff + @as(u64, syms_sh.sh_link) * header.shentsize; + if (strs_at + @sizeOf(elf.Elf64_Shdr) > bytes.len) return error.TruncatedElfFile; + const strs_sh = std.mem.bytesToValue(elf.Elf64_Shdr, bytes[strs_at..][0..@sizeOf(elf.Elf64_Shdr)]); + if (strs_sh.sh_offset + strs_sh.sh_size > bytes.len) return error.TruncatedElfFile; + const strs = bytes[strs_sh.sh_offset..][0..strs_sh.sh_size]; + const syms = std.mem.bytesAsSlice(elf.Elf64.Sym, bytes[syms_sh.sh_offset..][0 .. syms_sh.sh_size / @sizeOf(elf.Elf64.Sym) * @sizeOf(elf.Elf64.Sym)]); + + var out: std.ArrayList(Symbol) = .empty; + // The function each GOT slot leads to, and each of .rela.plt's in order (what a lazy stub pushes). + var slots: std.AutoHashMapUnmanaged(u64, []const u8) = .empty; + defer slots.deinit(gpa); + var pushed: std.ArrayList(?[]const u8) = .empty; + defer pushed.deinit(gpa); + for (relas, 0..) |rela, which| { + const sh = rela orelse continue; + for (std.mem.bytesAsSlice(elf.Elf64_Rela, bytes[sh.sh_offset..][0 .. sh.sh_size / @sizeOf(elf.Elf64_Rela) * @sizeOf(elf.Elf64_Rela)])) |rel| { + const R = elf.R_X86_64; + const kind = std.enums.fromInt(R, rel.r_type()) orelse .NONE; + const name: ?[]const u8 = switch (kind) { + .JUMP_SLOT, .GLOB_DAT => if (rel.r_sym() < syms.len and syms[rel.r_sym()].name < strs.len) std.mem.sliceTo(strs[syms[rel.r_sym()].name..], 0) else null, + .IRELATIVE => for (symbols) |sym| { + if (sym.addr == @as(u64, @bitCast(rel.r_addend))) break sym.name; + } else null, + else => null, + }; + if (which == 0) try pushed.append(gpa, name); + const n = name orelse continue; + if (n.len == 0) continue; + if (kind != .IRELATIVE) try imported.put(gpa, n, {}); + try slots.put(gpa, rel.r_offset, n); + try out.append(arena, .{ .name = try std.fmt.allocPrint(arena, "{s}@got", .{n}), .addr = rel.r_offset, .size = 8 }); + } + } + for (plts) |p| { + var entry = p.addr; + while (entry + p.entsize <= p.addr + p.size) : (entry += p.entsize) { + const s = stub(bytes[p.offset + (entry - p.addr) ..][0..p.entsize], entry); + if (s.first) continue; + const n = (if (s.slot) |slot| slots.get(slot) else null) orelse (if (s.index) |i| if (i < pushed.items.len) pushed.items[i] else null else null) orelse continue; + try out.append(arena, .{ .name = try std.fmt.allocPrint(arena, "{s}@plt", .{n}), .addr = entry, .size = p.entsize }); + } + } + return out.items; +} + +/// A PLT entry, decoded: the GOT slot its `jmp` reads; for lazy binding, +/// the relocation it pushes and where its push ends; whether it is the +/// PLT's first entry, which pushes the GOT's second word instead. +const Stub = struct { slot: ?u64 = null, index: ?u32 = null, pushed: ?u32 = null, first: bool = false }; + +fn stub(code: []const u8, addr: u64) Stub { + var s: Stub = .{}; + var i: usize = 0; + while (i < code.len) { + const rest = code[i..]; + if (std.mem.startsWith(u8, rest, &.{ 0xf3, 0x0f, 0x1e, 0xfa })) { + i += 4; // endbr64 + } else if (rest[0] == 0xf2) { + i += 1; // bnd + } else if (rest.len >= 6 and rest[0] == 0xff and (rest[1] == 0x25 or rest[1] == 0x35)) { + // jmp or push [rip + disp32] + const at = addr +% (i + 6) +% @as(u64, @bitCast(@as(i64, std.mem.readInt(i32, rest[2..6], .little)))); + if (rest[1] == 0x25) { + if (s.slot == null) s.slot = at; + } else { + s.first = true; + s.pushed = @intCast(i + 6); + } + i += 6; + } else if (rest.len >= 5 and rest[0] == 0x68) { + s.index = std.mem.readInt(u32, rest[1..5], .little); + s.pushed = @intCast(i + 5); + i += 5; + } else if (rest.len >= 5 and rest[0] == 0xe9) { + i += 5; // jmp rel32 + } else break; + } + return s; +} + /// A symbol by name: the executable's first, then the libraries'. As the /// linker has it, else (with `demangle`) as the source does. pub fn find(s: *Symbols, name: []const u8) ?Symbol { diff --git a/src/trace/debugger_test.zig b/src/trace/debugger_test.zig index e20f864..15ecc71 100644 --- a/src/trace/debugger_test.zig +++ b/src/trace/debugger_test.zig @@ -144,6 +144,185 @@ test "a recording by `ztrace record` has the line of each instruction round the try std.testing.expect(caller.line != null); } +test "PLT stubs and GOT slots are named for the function they lead to, in the program and in libc" { + var d = try spawn(targets.hello, null, null); + defer d.deinit(); + try d.toEntry(); + // Not in the program: the mappings are read again, libc's with them. + try std.testing.expect(d.symbol("open") != null); + var buf: [256]u8 = undefined; + var w: std.Io.Writer = .fixed(&buf); + for ([_][]const u8{ "open@plt", "read@plt", "close@plt", "open@got", "read@got" }) |name| { + w.end = 0; + try std.testing.expect(try d.syms.label(&w, d.symbol(name).?.addr)); + try std.testing.expectEqualStrings(name, w.buffered()[0 .. w.buffered().len - 2]); + } + // The function a caller calls, of the four names libc gives open. + w.end = 0; + _ = try d.syms.label(&w, d.symbol("__open64").?.addr); + try std.testing.expectEqualStrings("libc.so.6!open+0", w.buffered()); + // libc's own stubs are its IFUNCs', whose relocations name nothing: by the function each resolver is. + const libc = for (d.syms.modules.items) |*m| { + if (std.mem.endsWith(u8, m.path, "/libc.so.6")) break m; + } else return error.NoLibc; + var stubs: usize = 0; + for (libc.symbols) |sym| if (std.mem.endsWith(u8, sym.name, "@plt")) { + stubs += 1; + try std.testing.expect(d.symbol(sym.name[0 .. sym.name.len - "@plt".len]) != null); + w.end = 0; + _ = try d.syms.label(&w, sym.addr + libc.bias); + try std.testing.expect(std.mem.startsWith(u8, w.buffered(), "libc.so.6!") and std.mem.endsWith(u8, w.buffered(), "@plt+0")); + }; + try std.testing.expect(stubs > 10); +} + +test "a call followed down by instruction: through the PLT, the loader's resolver and libc to the kernel, and back" { + var trace = try zt.Trace.open(gpa, io, targets.hello_recording); + defer trace.close(); + const tids = try (try trace.moment(0)).threads(gpa); + defer gpa.free(tids); + const named_ = struct { + fn at(m: zt.store.Moment, addr: u64) []const u8 { + return m.trace.strings.get(addr) orelse ""; + } + }.at; + const inMain = struct { + fn at(m: zt.store.Moment, addr: u64) bool { + return std.mem.startsWith(u8, m.trace.strings.get(addr) orelse "", "main+"); + } + }.at; + // From main's first instruction to its last: the calls it makes, open, read twice and close. + var first: ?u64 = null; + var last: u64 = 0; + for (0..trace.len) |i| { + const regs = (try (try trace.moment(i)).regs(tids[0])) orelse continue; + if (!inMain(try trace.moment(i), regs.rip)) continue; + if (first == null) first = i; + last = i; + } + var stubs: [4][]const u8 = undefined; + var n_stubs: usize = 0; + var entries: [4][]const u8 = undefined; + var n_entries: usize = 0; + var crossings: [4]u16 = undefined; + var n_crossings: usize = 0; + var returns: usize = 0; + var resolver: usize = 0; + var back_to: u64 = 0; + var was_in_main = true; + var text: [256]u8 = undefined; + for (first.?..last + 1) |i| { + const m = try trace.moment(i); + const regs = (try m.regs(tids[0])).?; + const here = named_(m, regs.rip); + var chain_buf: [32]u64 = undefined; + const chain = try zt.telescope.backtrace(m, ®s, &chain_buf); + const in_main = inMain(m, regs.rip); + if (in_main) { + // After the ret: the instruction after the call. + if (!was_in_main) { + try std.testing.expectEqual(back_to, regs.rip); + returns += 1; + } + } else { + // main waits under every frame of the call, the resolver's too. + for (chain[1..]) |pc| { + if (inMain(m, pc)) break; + } else { + std.debug.print("moment {d} at {s}: main lost\n", .{ i, here }); + return error.MainLost; + } + if (std.mem.startsWith(u8, here, "ld-linux")) resolver += 1; + } + was_in_main = in_main; + // The stub's jmp, named for the function it leads to, its GOT slot + // too; lazy at a function's first call, bound at the second read's. + if (std.mem.endsWith(u8, here, "@plt+0")) { + stubs[n_stubs] = here; + n_stubs += 1; + try std.testing.expect(inMain(m, chain[1])); + const stub = zt.telescope.function(m, regs.rip).?; + try std.testing.expectEqual(regs.rip, stub.start); + try std.testing.expectEqual(@as(u64, 16), stub.len); + var events = try m.events(gpa); + defer events.deinit(gpa); + const slot = while (events.next()) |item| { + if (item.ev.kind == .touch and item.ev.a[2] == regs.rip) break item.ev.a[0]; + } else return error.NoTouch; + const function_name = here[0 .. here.len - "@plt+0".len]; + try std.testing.expectEqualStrings(try std.fmt.bufPrint(&text, "{s}@got+0", .{function_name}), named_(m, slot)); + const held = (try zt.telescope.got(m, slot)).?; + try std.testing.expectEqual(@as(?bool, n_stubs != 3), held.lazy); + } + // The callee's first instruction, by the name its caller used. + if (std.mem.startsWith(u8, here, "libc.so.6!") and std.mem.endsWith(u8, here, "+0")) { + entries[n_entries] = here; + n_entries += 1; + try std.testing.expect(inMain(m, chain[1])); + back_to = chain[1]; + // The caller's three rows: the one before the call, the call, the one it returns to. + const after = (try zt.telescope.instruction(m, back_to, &text)).?; + const call_at = back_to - after.back; + const call = (try zt.telescope.instruction(m, call_at, &text)).?; + try std.testing.expect(std.mem.startsWith(u8, call.text, "call")); + try std.testing.expect(call.back != 0 and try zt.telescope.instruction(m, call_at - call.back, &text) != null); + // main's bounds, from the recording: where its name says it begins. + const f = zt.telescope.function(m, back_to - 1).?; + try std.testing.expectEqual(back_to - try std.fmt.parseInt(u64, named_(m, back_to)["main+".len..], 10), f.start); + try std.testing.expect(back_to < f.start + f.len); + } + // At a syscall instruction: the next moment is the call, entry and + // exit, with the registers after it. + if (i + 1 < trace.len) { + const next = try trace.moment(i + 1); + var events = try next.events(gpa); + defer events.deinit(gpa); + var enter: ?zt.store.Events.Item = null; + var exit: ?zt.store.Events.Item = null; + while (events.next()) |item| switch (item.ev.kind) { + .syscall_enter => enter = item, + .syscall_exit => exit = item, + else => {}, + }; + if (enter) |e| { + const nr = e.ev.aux; + crossings[n_crossings] = nr; + n_crossings += 1; + try std.testing.expectEqual(nr, exit.?.ev.aux); + try std.testing.expectEqual(@as(u64, nr), regs.rax); + try std.testing.expect(!inMain(m, regs.rip)); + const after = (try next.regs(tids[0])).?; + // The instruction's own clobbers: rcx the return address, r11 the flags (with the step's trap flag). + try std.testing.expectEqual(regs.rip + 2, after.rcx); + try std.testing.expectEqual(regs.rip + 2, after.rip); + try std.testing.expectEqual(regs.eflags | 0x100, after.r11 | 0x100); + try std.testing.expectEqual(exit.?.ev.a[0], after.rax); + var out: std.Io.Writer = .fixed(&text); + try zt.syscalls.formatEnter(&out, &e.ev, e.payload); + try zt.syscalls.formatExit(&out, &exit.?.ev); + const call = out.buffered(); + if (std.mem.eql(u8, zt.syscalls.name(nr).?, "openat")) { + try std.testing.expect(std.mem.startsWith(u8, call, "openat(-100, \"/")); + try std.testing.expect(std.mem.endsWith(u8, call, "/hello\", 0x0, 0x0) = 3")); + } + if (std.mem.eql(u8, zt.syscalls.name(nr).?, "read")) try std.testing.expect(std.mem.startsWith(u8, call, "read(3, 0x") and std.mem.endsWith(u8, call, ", 16) = 16")); + } + } + } + const want_stubs = [_][]const u8{ "open@plt+0", "read@plt+0", "read@plt+0", "close@plt+0" }; + const want_entries = [_][]const u8{ "libc.so.6!open+0", "libc.so.6!read+0", "libc.so.6!read+0", "libc.so.6!close+0" }; + try std.testing.expectEqual(want_stubs.len, n_stubs); + for (want_stubs, stubs, want_entries, entries) |ws, s, we, e| { + try std.testing.expectEqualStrings(ws, s); + try std.testing.expectEqualStrings(we, e); + } + try std.testing.expectEqual(@as(usize, 4), n_crossings); + for ([_][]const u8{ "openat", "read", "read", "close" }, crossings) |want, nr| try std.testing.expectEqualStrings(want, zt.syscalls.name(nr).?); + try std.testing.expectEqual(@as(usize, 4), returns); + // Three first calls ran the resolver, hundreds of instructions each. + try std.testing.expect(resolver > 300); +} + 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(); @@ -319,6 +498,27 @@ test "a recording tells each stack word's role: saved registers, return addresse try std.testing.expectEqual(@as(u64, 2), std.mem.readInt(u64, &argc, .little)); } +test "by instruction, a call into the vDSO is run through to its return: its retry loop is no stops, whatever a stop costs" { + var counts: [2]usize = undefined; + for ([_]usize{ 1024, 0 }, &counts) |stack, *count| { + var log: Log = .{}; + defer log.deinit(); + var d = try spawn(targets.clock, null, log.sink()); + defer d.deinit(); + d.telescope = 3; + d.stack_watch = stack; + _ = try d.breakAt(try d.resolve("main"), null); + _ = try d.cont(); + count.* = 0; + while (try d.stepi() != .exited) : (count.* += 1) { + const in = d.syms.mappingAt(d.regs.rip) orelse continue; + try std.testing.expect(!std.mem.eql(u8, in.name, "[vdso]")); + } + } + // The same stops however long a stop takes. + try std.testing.expectEqual(counts[0], counts[1]); +} + test "the telescope records the region round what a pointer leads to" { var log: Log = .{}; defer log.deinit(); diff --git a/src/trace/heap.zig b/src/trace/heap.zig index 8f0dc68..d74e07a 100644 --- a/src/trace/heap.zig +++ b/src/trace/heap.zig @@ -180,6 +180,13 @@ pub const Tcache = struct { slot: u64, /// The bins that hold anything. bins: []Chain, + /// How many chunks a bin holds when full, where a bin's count is the + /// slots it has left (`Layout.tcache_slots`): the count of a bin that + /// is empty, every small bin having the same (`glibc.malloc.tcache_count`, + /// 16 by default). A bin holds `capacity - count`. Null otherwise: the + /// count is what a bin holds, and glibc keeps the capacity elsewhere (7 + /// by default). + capacity: ?u16 = null, }; pub const Heap = struct { @@ -192,8 +199,11 @@ pub const Heap = struct { cut: End = .end, cut_at: u64 = 0, /// Null: the thread has none yet (glibc 2.42 on makes it at the first - /// free), or its pointer is not known. + /// free), or its pointer is not known; `tcache_dummy` says which. tcache: ?Tcache = null, + /// No tcache yet: the thread's pointer aims at glibc's dummy, zeros in + /// the C library's read-only data, until its first free. + tcache_dummy: bool = false, /// The fastbins and the bins that hold anything. fastbins: []Chain = &.{}, bins: []Chain = &.{}, @@ -257,11 +267,13 @@ pub fn walk(arena: std.mem.Allocator, mem: anytype, g: Glibc, slot: ?u64) !?Heap .max_system_mem = field(ab, l.system_mem + 8), } }; - // The tcache, when the thread's pointer leads to a chunk of its size. - if (slot) |at| if (word(mem, at)) |t| if (t % 16 == 0) if (word(mem, t -% 8)) |size| if (size & ~@as(u64, 7) == l.tcache_chunk) { + // The tcache, when the thread's pointer leads to a chunk of its size; + // else, all zeros where it leads, the dummy it aims at until there is one. + if (slot) |at| if (word(mem, at)) |t| if (t != 0) { var tcache_buf: [max_struct]u8 = undefined; const tb = tcache_buf[0 .. l.tcache_entries + 8 * @as(u64, l.tcache_bins)]; - if (mem.read(t, tb) == tb.len) { + const chunk = t % 16 == 0 and if (word(mem, t -% 8)) |size| size & ~@as(u64, 7) == l.tcache_chunk else false; + if (!chunk) h.tcache_dummy = mem.read(t, tb) == tb.len and std.mem.allEqual(u8, tb, 0) else if (mem.read(t, tb) == tb.len) { var bins: std.ArrayList(Chain) = .empty; // shortcut: 2.44's large bins (64 on, used only when the tunable glibc.malloc.tcache_max is raised) are walked as the small ones are, untested; upgrade with a target that raises it. for (0..l.tcache_bins) |i| { @@ -272,7 +284,12 @@ pub fn walk(arena: std.mem.Allocator, mem: anytype, g: Glibc, slot: ?u64) !?Heap try follow(arena, mem, &c, head, l, 0); try bins.append(arena, c); } - h.tcache = .{ .addr = t, .slot = at, .bins = bins.items }; + var capacity: ?u16 = null; + if (l.tcache_slots) for (0..@min(64, l.tcache_bins)) |i| if (field(tb, l.tcache_entries + 8 * i) == 0) { + capacity = std.mem.readInt(u16, tb[2 * i ..][0..2], .little); + break; + }; + h.tcache = .{ .addr = t, .slot = at, .bins = bins.items, .capacity = capacity }; } }; @@ -404,8 +421,11 @@ pub fn print(h: Heap, w: *std.Io.Writer) !void { i += n; } if (h.cut != .end) try w.print(" ... {t} at {x}: no further\n", .{ h.cut, h.cut_at }); + if (h.tcache_dummy) try w.writeAll("tcache: none yet, the thread's pointer on glibc's dummy\n"); if (h.tcache) |t| { - try w.print("tcache {x}\n", .{t.addr}); + try w.print("tcache {x}", .{t.addr}); + if (t.capacity) |n| try w.print(", {d} a bin", .{n}); + try w.writeAll("\n"); for (t.bins) |c| try printChain(w, c, h.layout); } for (h.fastbins) |c| try printChain(w, c, h.layout); @@ -554,23 +574,32 @@ fn banner(bytes: []const u8) ?[2]u16 { /// Where the stopped thread keeps its tcache pointer, from its thread /// pointer: in the static TLS below it (64 KiB of it at most: a library /// preloaded has its block between the program's and libc's), the word -/// that points at a chunk of the tcache's size in the main heap. Null until -/// the tcache is made. +/// that points at a chunk of the tcache's size in the main heap, or, before +/// there is one (glibc 2.42 on), at its dummy: zeros of the tcache's size in +/// the read-only data of the C library. Null until either is there. fn findTcache(d: *Debugger, g: Glibc, l: Layout) !?i64 { const tp = d.regs.fs_base; var w: [8]u8 = undefined; const top = if (d.read(g.arena + l.top, &w) == 8) std.mem.readInt(u64, &w, .little) else return null; const system_mem = if (d.read(g.arena + l.system_mem, &w) == 8) std.mem.readInt(u64, &w, .little) else return null; - if (system_mem == 0 or top < system_mem) return null; + const heap = system_mem != 0 and top >= system_mem; + const lib = d.syms.moduleAt(g.arena); const m = d.syms.mappingAt(tp -% 8) orelse return null; const from = @max(m.start, tp -| (64 << 10)) & ~@as(u64, 7); const tls = try d.gpa.alloc(u64, (tp - from) / 8); defer d.gpa.free(tls); const n = d.read(from, std.mem.sliceAsBytes(tls)) / 8; + var zeros: [max_struct]u8 = undefined; + const size = l.tcache_entries + 8 * @as(u64, l.tcache_bins); for (tls[0..n], 0..) |p, i| { - if (p % 16 != 0 or p < top -| system_mem or p >= top) continue; - if (d.read(p - 8, &w) != 8 or std.mem.readInt(u64, &w, .little) & ~@as(u64, 7) != l.tcache_chunk) continue; - return @as(i64, @intCast(from + 8 * i)) - @as(i64, @intCast(tp)); + const found = if (heap and p % 16 == 0 and p >= top -| system_mem and p < top) + d.read(p - 8, &w) == 8 and std.mem.readInt(u64, &w, .little) & ~@as(u64, 7) == l.tcache_chunk + else dummy: { + const in = d.syms.mappingAt(p) orelse break :dummy false; + if (in.prot & 2 != 0 or lib == null or !std.mem.eql(u8, in.name, lib.?.path)) break :dummy false; + break :dummy d.read(p, zeros[0..size]) == size and std.mem.allEqual(u8, zeros[0..size], 0); + }; + if (found) return @as(i64, @intCast(from + 8 * i)) - @as(i64, @intCast(tp)); } return null; } diff --git a/src/trace/heap_test.zig b/src/trace/heap_test.zig index 2cb5996..aca05b0 100644 --- a/src/trace/heap_test.zig +++ b/src/trace/heap_test.zig @@ -110,14 +110,17 @@ fn check(argv: []const []const u8) !void { } try std.testing.expectEqual(@as(usize, 17), smalls); try std.testing.expect(heaps[0].fastbins.len == 0 and heaps[0].bins.len == 0); - // glibc 2.44 makes the tcache at the first free. - if (heaps[0].tcache) |t| try std.testing.expectEqual(@as(usize, 0), t.bins.len) else try std.testing.expect(l.tcache_slots); + // glibc 2.44 makes the tcache at the first free: until then the thread's pointer is on its dummy. + if (heaps[0].tcache) |t| try std.testing.expectEqual(@as(usize, 0), t.bins.len) else try std.testing.expect(l.tcache_slots and heaps[0].tcache_dummy); + try std.testing.expect(!heaps[1].tcache_dummy); // 2: the tcache's 0x30 bin full, the last freed first, each link // protected by its own address; the rest in fastbin 0x30 where there are // fastbins, else in a bin. const t = heaps[1].tcache.?; try std.testing.expectEqual(@as(usize, 1), t.bins.len); + // Where a count is the slots left, an empty bin's is the capacity. + try std.testing.expectEqual(if (l.tcache_slots) @as(?u16, @intCast(fill)) else null, t.capacity); const bin = t.bins[0]; try std.testing.expectEqual(heap.Where{ .tcache = 1 }, bin.in); try std.testing.expectEqual(fill, bin.entries.len); diff --git a/src/trace/telescope.zig b/src/trace/telescope.zig index 61736fd..6ab2929 100644 --- a/src/trace/telescope.zig +++ b/src/trace/telescope.zig @@ -254,10 +254,14 @@ const Memory = struct { } }; -/// A function's frame as the recording has it (`Debugger.emitFunction`): -/// its rows and its locals, each end to end. -const Function = struct { +/// A function's frame as the recording has it (`Debugger.emitFunction`), +/// for every function a recorded walk went through (each frame's): where +/// it begins and how long it is, by its call frame information (a PLT +/// entry's 16 bytes for a stub); what its locals are from +/// (`Symbols.Function.base`); its rows and its locals, each end to end. +pub const Function = struct { start: u64, + len: u64, base: u8, rows: []const u8, locals: []const u8, @@ -276,7 +280,7 @@ const Function = struct { }; /// The recorded frame of the function that holds `pc`. -fn function(m: Moment, pc: u64) ?Function { +pub fn function(m: Moment, pc: u64) ?Function { const found = m.trace.stringBelow(event.frameId(pc)) orelse return null; // Below every function recorded is another kind of string, or the process's start. if (found.id < event.frameId(1)) return null; @@ -284,8 +288,25 @@ fn function(m: Moment, pc: u64) ?Function { if (t.len < 7) return null; const start = found.id - event.frameId(0); const rows = @as(usize, std.mem.readInt(u16, t[5..7], .little)) * @sizeOf(Symbols.Row); - if (pc - start >= std.mem.readInt(u32, t[0..4], .little) or rows == 0 or t.len < 7 + rows) return null; - return .{ .start = start, .base = t[4], .rows = t[7..][0..rows], .locals = t[7 + rows ..] }; + const len = std.mem.readInt(u32, t[0..4], .little); + if (pc - start >= len or rows == 0 or t.len < 7 + rows) return null; + return .{ .start = start, .len = len, .base = t[4], .rows = t[7..][0..rows], .locals = t[7 + rows ..] }; +} + +/// 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 +/// recording does not have the slot's word; `lazy` null where it has no +/// name for what the word holds. The slot's own name is `open@got` +/// (`Moment.string` of its address), the stub's `open@plt`. +pub fn got(m: Moment, slot: u64) !?struct { value: u64, lazy: ?bool } { + var bytes: [8]u8 = undefined; + var known: [8]bool = undefined; + try m.read(slot, &bytes, &known); + if (std.mem.indexOfScalar(bool, &known, false) != null) return null; + const value = std.mem.readInt(u64, &bytes, .little); + const name = m.trace.strings.get(value) orelse return .{ .value = value, .lazy = null }; + return .{ .value = value, .lazy = std.mem.indexOf(u8, name, "@plt") != null }; } /// At a function's first instruction its frame is not made yet: the @@ -479,7 +500,7 @@ test "frames walked by their call frame information; each stack word's role" { }, try frames(m, ®s, &out)); var words: [16]Slot = undefined; - const got = try slots(m, ®s, 0x7ff100, &words); + const roles = try slots(m, ®s, 0x7ff100, &words); const local = struct { fn of(name: []const u8, size: u32, offset: i32) Role { return .{ .local = .{ .name = name, .size = size, .offset = offset } }; @@ -493,7 +514,7 @@ test "frames walked by their call frame information; each stack word's role" { .{ .{ .argv = 0 }, null }, .{ .{ .argv = 1 }, null }, .{ .{ .envp = 0 }, null }, .{ .{ .envp = 1 }, null }, }; - for (want, got) |w, have| { + for (want, roles) |w, have| { try std.testing.expectEqual(w[1], have.frame); try std.testing.expectEqual(std.meta.activeTag(w[0]), std.meta.activeTag(have.role)); switch (w[0]) { |
