mirror of
https://codeberg.org/ziglang/zig.git
synced 2025-12-06 13:54:21 +00:00
debug: fix edge cases in macOS debug symbol lookup
This commit fixes two related things: 1. If the loop goes all the way through the slice without a match, on the last iteration `mid == symbols.len - 1` which causes `&symbols[mid + 1]` to be out of bounds. End one step before that instead. 2. If the address we're looking for is greater than the address of the last symbol in the slice, we now match it to that symbol. Previously, we would miss this case since we only matched if the address was _in between_ the address of two symbols.
This commit is contained in:
parent
eace6906ce
commit
23d148e5c7
1 changed files with 29 additions and 1 deletions
|
|
@ -6,6 +6,7 @@ const io = std.io;
|
||||||
const os = std.os;
|
const os = std.os;
|
||||||
const fs = std.fs;
|
const fs = std.fs;
|
||||||
const process = std.process;
|
const process = std.process;
|
||||||
|
const testing = std.testing;
|
||||||
const elf = std.elf;
|
const elf = std.elf;
|
||||||
const DW = std.dwarf;
|
const DW = std.dwarf;
|
||||||
const macho = std.macho;
|
const macho = std.macho;
|
||||||
|
|
@ -559,7 +560,7 @@ pub const TTY = struct {
|
||||||
|
|
||||||
fn machoSearchSymbols(symbols: []const MachoSymbol, address: usize) ?*const MachoSymbol {
|
fn machoSearchSymbols(symbols: []const MachoSymbol, address: usize) ?*const MachoSymbol {
|
||||||
var min: usize = 0;
|
var min: usize = 0;
|
||||||
var max: usize = symbols.len;
|
var max: usize = symbols.len - 1;
|
||||||
while (min < max) {
|
while (min < max) {
|
||||||
const mid = min + (max - min) / 2;
|
const mid = min + (max - min) / 2;
|
||||||
const curr = &symbols[mid];
|
const curr = &symbols[mid];
|
||||||
|
|
@ -572,9 +573,36 @@ fn machoSearchSymbols(symbols: []const MachoSymbol, address: usize) ?*const Mach
|
||||||
return curr;
|
return curr;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const max_sym = &symbols[symbols.len - 1];
|
||||||
|
if (address >= max_sym.address())
|
||||||
|
return max_sym;
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
test "machoSearchSymbols" {
|
||||||
|
const symbols = [_]MachoSymbol{
|
||||||
|
.{ .addr = 100, .strx = undefined, .size = undefined, .ofile = undefined },
|
||||||
|
.{ .addr = 200, .strx = undefined, .size = undefined, .ofile = undefined },
|
||||||
|
.{ .addr = 300, .strx = undefined, .size = undefined, .ofile = undefined },
|
||||||
|
};
|
||||||
|
|
||||||
|
try testing.expectEqual(@as(?*const MachoSymbol, null), machoSearchSymbols(&symbols, 0));
|
||||||
|
try testing.expectEqual(@as(?*const MachoSymbol, null), machoSearchSymbols(&symbols, 99));
|
||||||
|
try testing.expectEqual(&symbols[0], machoSearchSymbols(&symbols, 100).?);
|
||||||
|
try testing.expectEqual(&symbols[0], machoSearchSymbols(&symbols, 150).?);
|
||||||
|
try testing.expectEqual(&symbols[0], machoSearchSymbols(&symbols, 199).?);
|
||||||
|
|
||||||
|
try testing.expectEqual(&symbols[1], machoSearchSymbols(&symbols, 200).?);
|
||||||
|
try testing.expectEqual(&symbols[1], machoSearchSymbols(&symbols, 250).?);
|
||||||
|
try testing.expectEqual(&symbols[1], machoSearchSymbols(&symbols, 299).?);
|
||||||
|
|
||||||
|
try testing.expectEqual(&symbols[2], machoSearchSymbols(&symbols, 300).?);
|
||||||
|
try testing.expectEqual(&symbols[2], machoSearchSymbols(&symbols, 301).?);
|
||||||
|
try testing.expectEqual(&symbols[2], machoSearchSymbols(&symbols, 5000).?);
|
||||||
|
}
|
||||||
|
|
||||||
/// TODO resources https://github.com/ziglang/zig/issues/4353
|
/// TODO resources https://github.com/ziglang/zig/issues/4353
|
||||||
pub fn printSourceAtAddress(debug_info: *DebugInfo, out_stream: anytype, address: usize, tty_config: TTY.Config) !void {
|
pub fn printSourceAtAddress(debug_info: *DebugInfo, out_stream: anytype, address: usize, tty_config: TTY.Config) !void {
|
||||||
const module = debug_info.getModuleForAddress(address) catch |err| switch (err) {
|
const module = debug_info.getModuleForAddress(address) catch |err| switch (err) {
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue