authorgravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2020-01-21 20:51:57+01:00
committergravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2020-01-21 20:51:57+01:00
logbc82e0f3d3aed55165902c37271af120fcd4f858
tree7342deb2e15f443759c79a2dce542cc13aff3877
parentc522699f28c1df806865c527a7a68a875e606527

Refactor some code in the debug output


1 files changed, 77 insertions(+), 158 deletions(-)

lib/std/debug.zig+77-158
......@@ -379,16 +379,7 @@ fn printSourceAtAddressWindows(di: *DebugInfo, out_stream: var, relocated_addres
379379 }
380380 } else {
381381 // we have no information to add to the address
382 if (tty_color) {
383 try out_stream.print("???:?:?: ", .{});
384 setTtyColor(TtyColor.Dim);
385 try out_stream.print("0x{x} in ??? (???)", .{relocated_address});
386 setTtyColor(TtyColor.Reset);
387 try out_stream.print("\n\n\n", .{});
388 } else {
389 try out_stream.print("???:?:?: 0x{x} in ??? (???)\n\n\n", .{relocated_address});
390 }
391 return;
382 return printLineInfo(out_stream, null, relocated_address, "???", "???", tty_color, printLineFromFileAnyOs);
392383 };
393384
394385 const mod = &di.modules[mod_index];
......@@ -510,66 +501,15 @@ fn printSourceAtAddressWindows(di: *DebugInfo, out_stream: var, relocated_addres
510501 }
511502 };
512503
513 if (tty_color) {
514 setTtyColor(TtyColor.White);
515 if (opt_line_info) |li| {
516 try out_stream.print("{}:{}:{}", .{ li.file_name, li.line, li.column });
517 } else {
518 try out_stream.print("???:?:?", .{});
519 }
520 setTtyColor(TtyColor.Reset);
521 try out_stream.print(": ", .{});
522 setTtyColor(TtyColor.Dim);
523 try out_stream.print("0x{x} in {} ({})", .{ relocated_address, symbol_name, obj_basename });
524 setTtyColor(TtyColor.Reset);
525
526 if (opt_line_info) |line_info| {
527 try out_stream.print("\n", .{});
528 if (printLineFromFileAnyOs(out_stream, line_info)) {
529 if (line_info.column == 0) {
530 try out_stream.write("\n");
531 } else {
532 {
533 var col_i: usize = 1;
534 while (col_i < line_info.column) : (col_i += 1) {
535 try out_stream.writeByte(' ');
536 }
537 }
538 setTtyColor(TtyColor.Green);
539 try out_stream.write("^");
540 setTtyColor(TtyColor.Reset);
541 try out_stream.write("\n");
542 }
543 } else |err| switch (err) {
544 error.EndOfFile => {},
545 error.FileNotFound => {
546 setTtyColor(TtyColor.Dim);
547 try out_stream.write("file not found\n\n");
548 setTtyColor(TtyColor.White);
549 },
550 else => return err,
551 }
552 } else {
553 try out_stream.print("\n\n\n", .{});
554 }
555 } else {
556 if (opt_line_info) |li| {
557 try out_stream.print("{}:{}:{}: 0x{x} in {} ({})\n\n\n", .{
558 li.file_name,
559 li.line,
560 li.column,
561 relocated_address,
562 symbol_name,
563 obj_basename,
564 });
565 } else {
566 try out_stream.print("???:?:?: 0x{x} in {} ({})\n\n\n", .{
567 relocated_address,
568 symbol_name,
569 obj_basename,
570 });
571 }
572 }
504 try printLineInfo(
505 out_stream,
506 opt_line_info,
507 relocated_address,
508 symbol_name,
509 obj_basename,
510 tty_color,
511 printLineFromFileAnyOs,
512 );
573513}
574514
575515const TtyColor = enum {
......@@ -605,7 +545,11 @@ fn setTtyColor(tty_color: TtyColor) void {
605545 stderr_file.write(RESET) catch return;
606546 },
607547 }
608 } else {
548
549 return;
550 }
551
552 if (builtin.os == .windows) {
609553 const S = struct {
610554 var attrs: windows.WORD = undefined;
611555 var init_attrs = false;
......@@ -711,12 +655,7 @@ fn printSourceAtAddressMacOs(di: *DebugInfo, out_stream: var, address: usize, tt
711655 const adjusted_addr = 0x100000000 + (address - base_addr);
712656
713657 const symbol = machoSearchSymbols(di.symbols, adjusted_addr) orelse {
714 if (tty_color) {
715 try out_stream.print("???:?:?: " ++ DIM ++ "0x{x} in ??? (???)" ++ RESET ++ "\n\n\n", .{address});
716 } else {
717 try out_stream.print("???:?:?: 0x{x} in ??? (???)\n\n\n", .{address});
718 }
719 return;
658 return printLineInfo(out_stream, null, address, "???", "???", tty_color, printLineFromFileAnyOs);
720659 };
721660
722661 const symbol_name = mem.toSliceConst(u8, @ptrCast([*:0]const u8, di.strings.ptr + symbol.nlist.n_strx));
......@@ -724,29 +663,22 @@ fn printSourceAtAddressMacOs(di: *DebugInfo, out_stream: var, address: usize, tt
724663 const ofile_path = mem.toSliceConst(u8, @ptrCast([*:0]const u8, di.strings.ptr + ofile.n_strx));
725664 break :blk fs.path.basename(ofile_path);
726665 } else "???";
727 if (getLineNumberInfoMacOs(di, symbol.*, adjusted_addr)) |line_info| {
728 defer line_info.deinit();
729 try printLineInfo(
730 out_stream,
731 line_info,
732 address,
733 symbol_name,
734 compile_unit_name,
735 tty_color,
736 printLineFromFileAnyOs,
737 );
738 } else |err| switch (err) {
739 error.MissingDebugInfo, error.InvalidDebugInfo => {
740 if (tty_color) {
741 try out_stream.print("???:?:?: " ++ DIM ++ "0x{x} in {} ({})" ++ RESET ++ "\n\n\n", .{
742 address, symbol_name, compile_unit_name,
743 });
744 } else {
745 try out_stream.print("???:?:?: 0x{x} in {} ({})\n\n\n", .{ address, symbol_name, compile_unit_name });
746 }
747 },
666
667 const line_info = getLineNumberInfoMacOs(di, symbol.*, adjusted_addr) catch |err| switch (err) {
668 error.MissingDebugInfo, error.InvalidDebugInfo => null,
748669 else => return err,
749 }
670 };
671 defer if (line_info) |li| li.deinit();
672
673 try printLineInfo(
674 out_stream,
675 line_info,
676 address,
677 symbol_name,
678 compile_unit_name,
679 tty_color,
680 printLineFromFileAnyOs,
681 );
750682}
751683
752684pub fn printSourceAtAddressPosix(debug_info: *DebugInfo, out_stream: var, address: usize, tty_color: bool) !void {
......@@ -755,47 +687,46 @@ pub fn printSourceAtAddressPosix(debug_info: *DebugInfo, out_stream: var, addres
755687
756688fn printLineInfo(
757689 out_stream: var,
758 line_info: LineInfo,
690 line_info: ?LineInfo,
759691 address: usize,
760692 symbol_name: []const u8,
761693 compile_unit_name: []const u8,
762694 tty_color: bool,
763695 comptime printLineFromFile: var,
764696) !void {
765 if (tty_color) {
766 try out_stream.print(WHITE ++ "{}:{}:{}" ++ RESET ++ ": " ++ DIM ++ "0x{x} in {} ({})" ++ RESET ++ "\n", .{
767 line_info.file_name,
768 line_info.line,
769 line_info.column,
770 address,
771 symbol_name,
772 compile_unit_name,
773 });
774 if (printLineFromFile(out_stream, line_info)) {
775 if (line_info.column == 0) {
776 try out_stream.write("\n");
777 } else {
778 {
779 var col_i: usize = 1;
780 while (col_i < line_info.column) : (col_i += 1) {
781 try out_stream.writeByte(' ');
782 }
783 }
784 try out_stream.write(GREEN ++ "^" ++ RESET ++ "\n");
697 if (tty_color) setTtyColor(.White);
698
699 if (line_info) |*li| {
700 try out_stream.print("{}:{}:{}", .{ li.file_name, li.line, li.column });
701 } else {
702 try out_stream.print("???:?:?", .{});
703 }
704
705 if (tty_color) setTtyColor(.Reset);
706 try out_stream.write(": ");
707 if (tty_color) setTtyColor(.Dim);
708 try out_stream.print("0x{x} in {} ({})", .{ address, symbol_name, compile_unit_name });
709 if (tty_color) setTtyColor(.Reset);
710 try out_stream.write("\n");
711
712 // Show the matching source code line if possible
713 if (line_info) |li| {
714 if (printLineFromFile(out_stream, li)) {
715 if (li.column > 0) {
716 // The caret already takes one char
717 const space_needed = @intCast(usize, li.column - 1);
718
719 try out_stream.writeByteNTimes(' ', space_needed);
720 if (tty_color) setTtyColor(.Green);
721 try out_stream.write("^");
722 if (tty_color) setTtyColor(.Reset);
785723 }
724 try out_stream.write("\n");
786725 } else |err| switch (err) {
787726 error.EndOfFile, error.FileNotFound => {},
727 error.BadPathName => {},
788728 else => return err,
789729 }
790 } else {
791 try out_stream.print("{}:{}:{}: 0x{x} in {} ({})\n", .{
792 line_info.file_name,
793 line_info.line,
794 line_info.column,
795 address,
796 symbol_name,
797 compile_unit_name,
798 });
799730 }
800731}
801732
......@@ -1240,38 +1171,26 @@ pub const DwarfInfo = struct {
12401171 comptime printLineFromFile: var,
12411172 ) !void {
12421173 const compile_unit = self.findCompileUnit(address) catch {
1243 if (tty_color) {
1244 try out_stream.print("???:?:?: " ++ DIM ++ "0x{x} in ??? (???)" ++ RESET ++ "\n\n\n", .{address});
1245 } else {
1246 try out_stream.print("???:?:?: 0x{x} in ??? (???)\n\n\n", .{address});
1247 }
1248 return;
1174 return printLineInfo(out_stream, null, address, "???", "???", tty_color, printLineFromFile);
12491175 };
1176
12501177 const compile_unit_name = try compile_unit.die.getAttrString(self, DW.AT_name);
1251 if (self.getLineNumberInfo(compile_unit.*, address)) |line_info| {
1252 defer line_info.deinit();
1253 const symbol_name = self.getSymbolName(address) orelse "???";
1254 try printLineInfo(
1255 out_stream,
1256 line_info,
1257 address,
1258 symbol_name,
1259 compile_unit_name,
1260 tty_color,
1261 printLineFromFile,
1262 );
1263 } else |err| switch (err) {
1264 error.MissingDebugInfo, error.InvalidDebugInfo => {
1265 if (tty_color) {
1266 try out_stream.print("???:?:?: " ++ DIM ++ "0x{x} in ??? ({})" ++ RESET ++ "\n\n\n", .{
1267 address, compile_unit_name,
1268 });
1269 } else {
1270 try out_stream.print("???:?:?: 0x{x} in ??? ({})\n\n\n", .{ address, compile_unit_name });
1271 }
1272 },
1178 const symbol_name = self.getSymbolName(address) orelse "???";
1179 const line_info = self.getLineNumberInfo(compile_unit.*, address) catch |err| switch (err) {
1180 error.MissingDebugInfo, error.InvalidDebugInfo => null,
12731181 else => return err,
1274 }
1182 };
1183 defer if (line_info) |li| li.deinit();
1184
1185 try printLineInfo(
1186 out_stream,
1187 line_info,
1188 address,
1189 symbol_name,
1190 compile_unit_name,
1191 tty_color,
1192 printLineFromFile,
1193 );
12751194 }
12761195
12771196 fn getSymbolName(di: *DwarfInfo, address: u64) ?[]const u8 {