Debian Codegraph @

debug/backtrace.ha

raw
1 // SPDX-License-Identifier: MPL-2.0
2 // (c) Hare authors <https://harelang.org>
3
4 use debug::dwarf;
5 use debug::image;
6 use fmt;
7 use format::elf;
8 use fs;
9 use io;
10 use os;
11
12 def MAX_FRAMES_TOP: size = 16z;
13 def MAX_FRAMES_BOTTOM: size = 16z;
14 def MAX_FRAMES: size = MAX_FRAMES_TOP + MAX_FRAMES_BOTTOM;
15
16 // Prints a backtrace to the standard error.
17 export fn backtrace(self: *image::image, frame: stackframe) void = {
18 let orig = frame;
19 let nframe = 1z;
20 for (true; nframe += 1) {
21 match (next(frame)) {
22 case let next: stackframe =>
23 frame = next;
24 case done => break;
25 };
26 };
27 frame = orig;
28
29 const st = match (os::fstat(self.fd)) {
30 case let st: fs::filestat =>
31 yield st;
32 case fs::error =>
33 yield fs::filestat { mask = 0, ... };
34 };
35
36 static let seen: [MAX_FRAMES]uintptr = [0: uintptr...];
37 let seen = seen[..0];
38
39 for (let i = 0z; i < nframe; i += 1) {
40 if (i < MAX_FRAMES_TOP || i > nframe - MAX_FRAMES_BOTTOM) {
41 printframe(self, &seen, &st, frame);
42 };
43 if (i == MAX_FRAMES_TOP && nframe > MAX_FRAMES) {
44 _ = fmt::errorfln("\t({} additional frames omitted)",
45 nframe - MAX_FRAMES);
46 };
47
48 match (next(frame)) {
49 case let next: stackframe =>
50 frame = next;
51 case done =>
52 break;
53 };
54 };
55 };
56
57 fn printframe(
58 self: *image::image,
59 seen: *[]uintptr,
60 imgstat: *fs::filestat,
61 frame: stackframe,
62 ) void = {
63 const pc = frame_pc(frame);
64
65 // Try to translate the address
66 match (translate(pc: uintptr)) {
67 case let ptr: uintptr =>
68 pc = ptr;
69 case =>
70 void;
71 };
72
73 const sym = match (symbol_byaddr(self, pc)) {
74 case let sym: elf::sym64 =>
75 yield sym;
76 case =>
77 _ = fmt::errorfln("(unknown) [0x{:x}]", pc);
78 return;
79 };
80 const name = match (symbol_name(self, &sym)) {
81 case let name: const str =>
82 yield name;
83 case =>
84 _ = fmt::errorfln("(unknown) [0x{:x}]", pc);
85 return;
86 };
87
88 // Look for DWARF line numbers, if possible
89 const (path, line, col) = match (dwarf::addr_to_line(self, pc)) {
90 case (void | io::error) =>
91 // No line number available, print what we've got
92 _ = fmt::errorfln("{}+0x{:x} [0x{:x}]", symname_to_ident(name),
93 pc - sym.st_value: uintptr, pc);
94 return;
95 case let tuple: (const str, uint, uint) =>
96 yield tuple;
97 };
98
99 _ = fmt::errorf("{}:{}:{} {}+0x{:x} [0x{:x}]",
100 path, line, col, symname_to_ident(name),
101 pc - sym.st_value: uintptr, pc);
102
103 // Skip context on frames we've already printed
104 for (let i = 0z; i < len(seen); i += 1) {
105 if (seen[i] == pc) {
106 _ = fmt::errorfln(" (already shown)");
107 return;
108 };
109 };
110 static append(seen, pc)!;
111 _ = fmt::errorln();
112
113 print_context(path, line, col, imgstat.mtime);
114 };
115
116 fn printframe_with_symbol(
117 sym: *elf::sym64,
118 name: str,
119 path: str,
120 loc: (uint, uint),
121 pc: uintptr,
122 ) void = {
123 _ = fmt::errorfln("{}:{}:{} {}+0x{:x} [0x{:x}]",
124 path, loc.0, loc.1, symname_to_ident(name),
125 pc - sym.st_value: uintptr, pc);
126 _ = fmt::errorln();
127 };