| ... | ... | @@ -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; |
| ... | ... | @@ -568,7 +569,7 @@ pub const TTY = struct { |
| 568 | 569 | |
| 569 | 570 | fn machoSearchSymbols(symbols: []const MachoSymbol, address: usize) ?*const MachoSymbol { |
| 570 | 571 | var min: usize = 0; |
| 571 | | var max: usize = symbols.len; |
| 572 | var max: usize = symbols.len - 1; |
| 572 | 573 | while (min < max) { |
| 573 | 574 | const mid = min + (max - min) / 2; |
| 574 | 575 | const curr = &symbols[mid]; |
| ... | ... | @@ -581,9 +582,36 @@ fn machoSearchSymbols(symbols: []const MachoSymbol, address: usize) ?*const Mach |
| 581 | 582 | return curr; |
| 582 | 583 | } |
| 583 | 584 | } |
| 585 | |
| 586 | const max_sym = &symbols[symbols.len - 1]; |
| 587 | if (address >= max_sym.address()) |
| 588 | return max_sym; |
| 589 | |
| 584 | 590 | return null; |
| 585 | 591 | } |
| 586 | 592 | |
| 593 | test "machoSearchSymbols" { |
| 594 | const symbols = [_]MachoSymbol{ |
| 595 | .{ .addr = 100, .strx = undefined, .size = undefined, .ofile = undefined }, |
| 596 | .{ .addr = 200, .strx = undefined, .size = undefined, .ofile = undefined }, |
| 597 | .{ .addr = 300, .strx = undefined, .size = undefined, .ofile = undefined }, |
| 598 | }; |
| 599 | |
| 600 | try testing.expectEqual(@as(?*const MachoSymbol, null), machoSearchSymbols(&symbols, 0)); |
| 601 | try testing.expectEqual(@as(?*const MachoSymbol, null), machoSearchSymbols(&symbols, 99)); |
| 602 | try testing.expectEqual(&symbols[0], machoSearchSymbols(&symbols, 100).?); |
| 603 | try testing.expectEqual(&symbols[0], machoSearchSymbols(&symbols, 150).?); |
| 604 | try testing.expectEqual(&symbols[0], machoSearchSymbols(&symbols, 199).?); |
| 605 | |
| 606 | try testing.expectEqual(&symbols[1], machoSearchSymbols(&symbols, 200).?); |
| 607 | try testing.expectEqual(&symbols[1], machoSearchSymbols(&symbols, 250).?); |
| 608 | try testing.expectEqual(&symbols[1], machoSearchSymbols(&symbols, 299).?); |
| 609 | |
| 610 | try testing.expectEqual(&symbols[2], machoSearchSymbols(&symbols, 300).?); |
| 611 | try testing.expectEqual(&symbols[2], machoSearchSymbols(&symbols, 301).?); |
| 612 | try testing.expectEqual(&symbols[2], machoSearchSymbols(&symbols, 5000).?); |
| 613 | } |
| 614 | |
| 587 | 615 | /// TODO resources https://github.com/ziglang/zig/issues/4353 |
| 588 | 616 | pub fn printSourceAtAddress(debug_info: *DebugInfo, out_stream: anytype, address: usize, tty_config: TTY.Config) !void { |
| 589 | 617 | const module = debug_info.getModuleForAddress(address) catch |err| switch (err) { |