1
1
const builtin = @import ("builtin" );
2
2
const std = @import ("std" );
3
+ const build_options = @import ("build_options" );
3
4
const assert = std .debug .assert ;
4
5
const serial = @import ("serial.zig" );
5
6
@@ -15,40 +16,53 @@ extern var __debug_ranges_start: u8;
15
16
extern var __debug_ranges_end : u8 ;
16
17
17
18
const source_files = [_ ][]const u8 {
18
- "src/debug.zig" ,
19
- "src/video_core_mailboxes.zig" ,
19
+ "src/main.zig" ,
20
20
"src/bootloader.zig" ,
21
+ "src/debug.zig" ,
21
22
"src/serial.zig" ,
23
+ "src/time.zig" ,
24
+ "src/mmio.zig" ,
25
+ "src/exceptions.zig" ,
22
26
"src/video_core_metrics.zig" ,
23
27
"src/video_core_properties.zig" ,
24
28
"src/video_core_frame_buffer.zig" ,
25
- "src/time.zig" ,
26
- "src/main.zig" ,
27
- "src/mmio.zig" ,
29
+ "src/video_core_mailboxes.zig" ,
28
30
"src/slice_iterator.zig" ,
29
31
};
30
32
31
33
var already_panicking : bool = false ;
32
34
33
- pub fn panic (stack_trace : ? * builtin.StackTrace , comptime fmt : []const u8 , args : ... ) noreturn {
35
+ pub fn panic (stack_trace : ? * builtin.StackTrace , comptime fmt : []const u8 , args : var ) noreturn {
34
36
@setCold (true );
35
37
if (already_panicking ) {
36
- serial .log ("\n panicked during kernel panic" );
37
- wfe_hang ();
38
+ serial .log ("panicked during kernel panic!! \n " , .{} );
39
+ wfeHang ();
38
40
}
39
41
already_panicking = true ;
40
42
41
- serial .log ("panic: " ++ fmt , args );
43
+ serial .log ("panic: " ++ fmt ++ " \n " , args );
42
44
43
45
const first_trace_addr = @returnAddress ();
44
- if (stack_trace ) | t | {
45
- dumpStackTrace (t );
46
+ if (build_options .want_stack_traces ) {
47
+ if (stack_trace ) | t | {
48
+ dumpStackTrace (t );
49
+ }
50
+ dumpCurrentStackTrace (first_trace_addr );
51
+ } else {
52
+ serial .log ("Stack trace:\n " , .{});
53
+ var it = std.debug.StackIterator { .first_addr = null , .fp = @frameAddress () };
54
+ while (it .next ()) | return_address | {
55
+ if (return_address == 0 ) break ;
56
+
57
+ serial .log (" 0x{x}\n " , .{return_address - 4 });
58
+ }
59
+ serial .log ("The stack trace can be decoded using `addr2line -e zig-cache/clashos <address>`\n\n " , .{});
60
+ serial .log ("To get stack traces during runtime, enable them using `zig build -Dstacktraces`\n " , .{});
46
61
}
47
- dumpCurrentStackTrace (first_trace_addr );
48
- wfe_hang ();
62
+ wfeHang ();
49
63
}
50
64
51
- pub fn wfe_hang () noreturn {
65
+ pub fn wfeHang () noreturn {
52
66
while (true ) {
53
67
asm volatile ("wfe" );
54
68
}
@@ -116,7 +130,7 @@ fn getSelfDebugInfo() !*std.debug.DwarfInfo {
116
130
.debug_info = dwarfSectionFromSymbol (& __debug_info_start , & __debug_info_end ),
117
131
.debug_abbrev = dwarfSectionFromSymbolAbs (& __debug_abbrev_start , & __debug_abbrev_end ),
118
132
.debug_str = dwarfSectionFromSymbolAbs (& __debug_str_start , & __debug_str_end ),
119
- .debug_line = dwarfSectionFromSymbol (& __debug_line_start , & __debug_line_end ),
133
+ .debug_line = dwarfSectionFromSymbolAbs (& __debug_line_start , & __debug_line_end ),
120
134
.debug_ranges = dwarfSectionFromSymbolAbs (& __debug_ranges_start , & __debug_ranges_end ),
121
135
.abbrev_table_list = undefined ,
122
136
.compile_unit_list = undefined ,
@@ -140,22 +154,22 @@ const kernel_panic_allocator = &kernel_panic_allocator_state.allocator;
140
154
141
155
pub fn dumpStackTrace (stack_trace : * const builtin.StackTrace ) void {
142
156
const dwarf_info = getSelfDebugInfo () catch | err | {
143
- serial .log ("Unable to dump stack trace: Unable to open debug info: {}" , @errorName (err ));
157
+ serial .log ("Unable to dump stack trace: Unable to open debug info: {}" , .{ @errorName (err )} );
144
158
return ;
145
159
};
146
160
writeStackTrace (stack_trace , dwarf_info ) catch | err | {
147
- serial .log ("Unable to dump stack trace: {}" , @errorName (err ));
161
+ serial .log ("Unable to dump stack trace: {}" , .{ @errorName (err )} );
148
162
return ;
149
163
};
150
164
}
151
165
152
166
pub fn dumpCurrentStackTrace (start_addr : ? usize ) void {
153
167
const dwarf_info = getSelfDebugInfo () catch | err | {
154
- serial .log ("Unable to dump stack trace: Unable to open debug info: {}" , @errorName (err ));
168
+ serial .log ("Unable to dump stack trace: Unable to open debug info: {}" , .{ @errorName (err )} );
155
169
return ;
156
170
};
157
171
writeCurrentStackTrace (dwarf_info , start_addr ) catch | err | {
158
- serial .log ("Unable to dump stack trace: {}" , @errorName (err ));
172
+ serial .log ("Unable to dump stack trace: {}" , .{ @errorName (err )} );
159
173
return ;
160
174
};
161
175
}
@@ -189,15 +203,17 @@ fn printLineFromFile(out_stream: var, line_info: std.debug.LineInfo) anyerror!vo
189
203
return ;
190
204
}
191
205
}
192
- try out_stream .print ("(source file {} not added in std/ debug.zig)\n " , line_info .file_name );
206
+ try out_stream .print ("(source file {} not added in std. debug.zig)\n " , .{ line_info .file_name } );
193
207
}
194
208
195
209
fn writeCurrentStackTrace (dwarf_info : * std.debug.DwarfInfo , start_addr : ? usize ) ! void {
196
210
var it = std .debug .StackIterator .init (start_addr );
197
211
while (it .next ()) | return_address | {
212
+ if (return_address == 0 ) break ;
213
+
198
214
try dwarf_info .printSourceAtAddress (
199
215
serial_out_stream ,
200
- return_address ,
216
+ return_address - 4 ,
201
217
true ,
202
218
printLineFromFile ,
203
219
);
0 commit comments