@@ -6,11 +6,6 @@ const io = std.io;
6
6
const posix = std .posix ;
7
7
const fs = std .fs ;
8
8
const testing = std .testing ;
9
- const elf = std .elf ;
10
- const DW = std .dwarf ;
11
- const macho = std .macho ;
12
- const coff = std .coff ;
13
- const pdb = std .pdb ;
14
9
const root = @import ("root" );
15
10
const File = std .fs .File ;
16
11
const windows = std .os .windows ;
@@ -19,8 +14,22 @@ const native_os = builtin.os.tag;
19
14
const native_endian = native_arch .endian ();
20
15
21
16
pub const Dwarf = @import ("debug/Dwarf.zig" );
22
- pub const Info = @import ("debug/Info.zig" );
17
+ pub const Pdb = @import ("debug/Pdb.zig" );
18
+ pub const SelfInfo = @import ("debug/SelfInfo.zig" );
19
+
20
+ /// Unresolved source locations can be represented with a single `usize` that
21
+ /// corresponds to a virtual memory address of the program counter. Combined
22
+ /// with debug information, those values can be converted into a resolved
23
+ /// source location, including file, line, and column.
24
+ pub const SourceLocation = struct {
25
+ line : u64 ,
26
+ column : u64 ,
27
+ file_name : []const u8 ,
28
+ };
23
29
30
+ /// Deprecated because it returns the optimization mode of the standard
31
+ /// library, when the caller probably wants to use the optimization mode of
32
+ /// their own module.
24
33
pub const runtime_safety = switch (builtin .mode ) {
25
34
.Debug , .ReleaseSafe = > true ,
26
35
.ReleaseFast , .ReleaseSmall = > false ,
@@ -72,13 +81,13 @@ pub fn getStderrMutex() *std.Thread.Mutex {
72
81
}
73
82
74
83
/// TODO multithreaded awareness
75
- var self_debug_info : ? Info = null ;
84
+ var self_debug_info : ? SelfInfo = null ;
76
85
77
- pub fn getSelfDebugInfo () ! * Info {
86
+ pub fn getSelfDebugInfo () ! * SelfInfo {
78
87
if (self_debug_info ) | * info | {
79
88
return info ;
80
89
} else {
81
- self_debug_info = try Info .openSelf (getDebugInfoAllocator ());
90
+ self_debug_info = try SelfInfo .openSelf (getDebugInfoAllocator ());
82
91
return & self_debug_info .? ;
83
92
}
84
93
}
@@ -316,7 +325,7 @@ pub fn captureStackTrace(first_address: ?usize, stack_trace: *std.builtin.StackT
316
325
stack_trace .index = slice .len ;
317
326
} else {
318
327
// TODO: This should use the DWARF unwinder if .eh_frame_hdr is available (so that full debug info parsing isn't required).
319
- // A new path for loading Info needs to be created which will only attempt to parse in-memory sections, because
328
+ // A new path for loading SelfInfo needs to be created which will only attempt to parse in-memory sections, because
320
329
// stopping to load other debug info (ie. source line info) from disk here is not required for unwinding.
321
330
var it = StackIterator .init (first_address , null );
322
331
defer it .deinit ();
@@ -494,7 +503,7 @@ pub fn writeStackTrace(
494
503
stack_trace : std.builtin.StackTrace ,
495
504
out_stream : anytype ,
496
505
allocator : mem.Allocator ,
497
- debug_info : * Info ,
506
+ debug_info : * SelfInfo ,
498
507
tty_config : io.tty.Config ,
499
508
) ! void {
500
509
_ = allocator ;
@@ -531,11 +540,11 @@ pub const StackIterator = struct {
531
540
fp : usize ,
532
541
ma : MemoryAccessor = MemoryAccessor .init ,
533
542
534
- // When Info and a register context is available, this iterator can unwind
543
+ // When SelfInfo and a register context is available, this iterator can unwind
535
544
// stacks with frames that don't use a frame pointer (ie. -fomit-frame-pointer),
536
545
// using DWARF and MachO unwind info.
537
546
unwind_state : if (have_ucontext ) ? struct {
538
- debug_info : * Info ,
547
+ debug_info : * SelfInfo ,
539
548
dwarf_context : Dwarf.UnwindContext ,
540
549
last_error : ? UnwindError = null ,
541
550
failed : bool = false ,
@@ -560,7 +569,7 @@ pub const StackIterator = struct {
560
569
};
561
570
}
562
571
563
- pub fn initWithContext (first_address : ? usize , debug_info : * Info , context : * const posix.ucontext_t ) ! StackIterator {
572
+ pub fn initWithContext (first_address : ? usize , debug_info : * SelfInfo , context : * const posix.ucontext_t ) ! StackIterator {
564
573
// The implementation of DWARF unwinding on aarch64-macos is not complete. However, Apple mandates that
565
574
// the frame pointer register is always used, so on this platform we can safely use the FP-based unwinder.
566
575
if (comptime builtin .target .isDarwin () and native_arch == .aarch64 ) {
@@ -820,7 +829,7 @@ const have_msync = switch (native_os) {
820
829
821
830
pub fn writeCurrentStackTrace (
822
831
out_stream : anytype ,
823
- debug_info : * Info ,
832
+ debug_info : * SelfInfo ,
824
833
tty_config : io.tty.Config ,
825
834
start_addr : ? usize ,
826
835
) ! void {
@@ -906,7 +915,7 @@ pub noinline fn walkStackWindows(addresses: []usize, existing_context: ?*const w
906
915
907
916
pub fn writeStackTraceWindows (
908
917
out_stream : anytype ,
909
- debug_info : * Info ,
918
+ debug_info : * SelfInfo ,
910
919
tty_config : io.tty.Config ,
911
920
context : * const windows.CONTEXT ,
912
921
start_addr : ? usize ,
@@ -925,7 +934,7 @@ pub fn writeStackTraceWindows(
925
934
}
926
935
}
927
936
928
- fn printUnknownSource (debug_info : * Info , out_stream : anytype , address : usize , tty_config : io.tty.Config ) ! void {
937
+ fn printUnknownSource (debug_info : * SelfInfo , out_stream : anytype , address : usize , tty_config : io.tty.Config ) ! void {
929
938
const module_name = debug_info .getModuleNameForAddress (address );
930
939
return printLineInfo (
931
940
out_stream ,
@@ -938,14 +947,14 @@ fn printUnknownSource(debug_info: *Info, out_stream: anytype, address: usize, tt
938
947
);
939
948
}
940
949
941
- fn printLastUnwindError (it : * StackIterator , debug_info : * Info , out_stream : anytype , tty_config : io.tty.Config ) void {
950
+ fn printLastUnwindError (it : * StackIterator , debug_info : * SelfInfo , out_stream : anytype , tty_config : io.tty.Config ) void {
942
951
if (! have_ucontext ) return ;
943
952
if (it .getLastError ()) | unwind_error | {
944
953
printUnwindError (debug_info , out_stream , unwind_error .address , unwind_error .err , tty_config ) catch {};
945
954
}
946
955
}
947
956
948
- fn printUnwindError (debug_info : * Info , out_stream : anytype , address : usize , err : UnwindError , tty_config : io.tty.Config ) ! void {
957
+ fn printUnwindError (debug_info : * SelfInfo , out_stream : anytype , address : usize , err : UnwindError , tty_config : io.tty.Config ) ! void {
949
958
const module_name = debug_info .getModuleNameForAddress (address ) orelse "???" ;
950
959
try tty_config .setColor (out_stream , .dim );
951
960
if (err == error .MissingDebugInfo ) {
@@ -956,7 +965,7 @@ fn printUnwindError(debug_info: *Info, out_stream: anytype, address: usize, err:
956
965
try tty_config .setColor (out_stream , .reset );
957
966
}
958
967
959
- pub fn printSourceAtAddress (debug_info : * Info , out_stream : anytype , address : usize , tty_config : io.tty.Config ) ! void {
968
+ pub fn printSourceAtAddress (debug_info : * SelfInfo , out_stream : anytype , address : usize , tty_config : io.tty.Config ) ! void {
960
969
const module = debug_info .getModuleForAddress (address ) catch | err | switch (err ) {
961
970
error .MissingDebugInfo , error .InvalidDebugInfo = > return printUnknownSource (debug_info , out_stream , address , tty_config ),
962
971
else = > return err ,
@@ -981,7 +990,7 @@ pub fn printSourceAtAddress(debug_info: *Info, out_stream: anytype, address: usi
981
990
982
991
fn printLineInfo (
983
992
out_stream : anytype ,
984
- line_info : ? Info. SourceLocation ,
993
+ line_info : ? SourceLocation ,
985
994
address : usize ,
986
995
symbol_name : []const u8 ,
987
996
compile_unit_name : []const u8 ,
@@ -1027,7 +1036,7 @@ fn printLineInfo(
1027
1036
}
1028
1037
}
1029
1038
1030
- fn printLineFromFileAnyOs (out_stream : anytype , line_info : Info. SourceLocation ) ! void {
1039
+ fn printLineFromFileAnyOs (out_stream : anytype , line_info : SourceLocation ) ! void {
1031
1040
// Need this to always block even in async I/O mode, because this could potentially
1032
1041
// be called from e.g. the event loop code crashing.
1033
1042
var f = try fs .cwd ().openFile (line_info .file_name , .{});
@@ -1093,7 +1102,7 @@ test printLineFromFileAnyOs {
1093
1102
1094
1103
var test_dir = std .testing .tmpDir (.{});
1095
1104
defer test_dir .cleanup ();
1096
- // Relies on testing.tmpDir internals which is not ideal, but Info. SourceLocation requires paths.
1105
+ // Relies on testing.tmpDir internals which is not ideal, but SourceLocation requires paths.
1097
1106
const test_dir_path = try join (allocator , &.{ ".zig-cache" , "tmp" , test_dir .sub_path [0.. ] });
1098
1107
defer allocator .free (test_dir_path );
1099
1108
@@ -1439,7 +1448,7 @@ test "manage resources correctly" {
1439
1448
}
1440
1449
1441
1450
const writer = std .io .null_writer ;
1442
- var di = try Info .openSelf (testing .allocator );
1451
+ var di = try SelfInfo .openSelf (testing .allocator );
1443
1452
defer di .deinit ();
1444
1453
try printSourceAtAddress (& di , writer , showMyTrace (), io .tty .detectConfig (std .io .getStdErr ()));
1445
1454
}
0 commit comments