| ... | ... | @@ -6,6 +6,7 @@ const io = std.io; |
| 6 | 6 | const os = std.os; |
| 7 | 7 | const fs = std.fs; |
| 8 | 8 | const process = std.process; |
| 9 | const testing = std.testing; |
| 9 | 10 | const elf = std.elf; |
| 10 | 11 | const DW = std.dwarf; |
| 11 | 12 | const macho = std.macho; |
| ... | ... | @@ -559,7 +560,7 @@ pub const TTY = struct { |
| 559 | 560 | |
| 560 | 561 | fn machoSearchSymbols(symbols: []const MachoSymbol, address: usize) ?*const MachoSymbol { |
| 561 | 562 | var min: usize = 0; |
| 562 | | var max: usize = symbols.len; |
| 563 | var max: usize = symbols.len - 1; |
| 563 | 564 | while (min < max) { |
| 564 | 565 | const mid = min + (max - min) / 2; |
| 565 | 566 | const curr = &symbols[mid]; |
| ... | ... | @@ -572,9 +573,36 @@ fn machoSearchSymbols(symbols: []const MachoSymbol, address: usize) ?*const Mach |
| 572 | 573 | return curr; |
| 573 | 574 | } |
| 574 | 575 | } |
| 576 | |
| 577 | const max_sym = &symbols[symbols.len - 1]; |
| 578 | if (address >= max_sym.address()) |
| 579 | return max_sym; |
| 580 | |
| 575 | 581 | return null; |
| 576 | 582 | } |
| 577 | 583 | |
| 584 | test "machoSearchSymbols" { |
| 585 | const symbols = [_]MachoSymbol{ |
| 586 | .{ .addr = 100, .strx = undefined, .size = undefined, .ofile = undefined }, |
| 587 | .{ .addr = 200, .strx = undefined, .size = undefined, .ofile = undefined }, |
| 588 | .{ .addr = 300, .strx = undefined, .size = undefined, .ofile = undefined }, |
| 589 | }; |
| 590 | |
| 591 | try testing.expectEqual(@as(?*const MachoSymbol, null), machoSearchSymbols(&symbols, 0)); |
| 592 | try testing.expectEqual(@as(?*const MachoSymbol, null), machoSearchSymbols(&symbols, 99)); |
| 593 | try testing.expectEqual(&symbols[0], machoSearchSymbols(&symbols, 100).?); |
| 594 | try testing.expectEqual(&symbols[0], machoSearchSymbols(&symbols, 150).?); |
| 595 | try testing.expectEqual(&symbols[0], machoSearchSymbols(&symbols, 199).?); |
| 596 | |
| 597 | try testing.expectEqual(&symbols[1], machoSearchSymbols(&symbols, 200).?); |
| 598 | try testing.expectEqual(&symbols[1], machoSearchSymbols(&symbols, 250).?); |
| 599 | try testing.expectEqual(&symbols[1], machoSearchSymbols(&symbols, 299).?); |
| 600 | |
| 601 | try testing.expectEqual(&symbols[2], machoSearchSymbols(&symbols, 300).?); |
| 602 | try testing.expectEqual(&symbols[2], machoSearchSymbols(&symbols, 301).?); |
| 603 | try testing.expectEqual(&symbols[2], machoSearchSymbols(&symbols, 5000).?); |
| 604 | } |
| 605 | |
| 578 | 606 | /// TODO resources https://github.com/ziglang/zig/issues/4353 |
| 579 | 607 | pub fn printSourceAtAddress(debug_info: *DebugInfo, out_stream: anytype, address: usize, tty_config: TTY.Config) !void { |
| 580 | 608 | const module = debug_info.getModuleForAddress(address) catch |err| switch (err) { |