1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
|
const Allocator = @import("mem.zig").Allocator;
const io = @import("io.zig");
const os = @import("os.zig");
const elf = @import("elf.zig");
const DW = @import("dwarf.zig");
const List = @import("list.zig").List;
pub error MissingDebugInfo;
pub error InvalidDebugInfo;
pub error UnsupportedDebugInfo;
pub fn assert(b: bool) {
if (!b) @unreachable()
}
pub fn printStackTrace() -> %void {
%return writeStackTrace(&io.stderr);
%return io.stderr.flush();
}
pub fn writeStackTrace(out_stream: &io.OutStream) -> %void {
switch (@compileVar("object_format")) {
elf => {
var st: ElfStackTrace = undefined;
%return io.openSelfExe(&st.self_exe_stream);
defer %return st.self_exe_stream.close();
%return st.elf.openStream(&global_allocator, &st.self_exe_stream);
defer %return st.elf.close();
st.aranges = %return st.elf.findSection(".debug_aranges");
st.debug_info = (%return st.elf.findSection(".debug_info")) ?? return error.MissingDebugInfo;
st.debug_abbrev = (%return st.elf.findSection(".debug_abbrev")) ?? return error.MissingDebugInfo;
var maybe_fp: ?&const u8 = @frameAddress();
while (true) {
const fp = maybe_fp ?? break;
const return_address = *(&const usize)(usize(fp) + @sizeOf(usize));
// read .debug_aranges to find out which compile unit the address is in
const compile_unit_offset = %return findCompileUnitOffset(&st, return_address);
%return out_stream.printInt(usize, return_address);
%return out_stream.printf(" -> ");
%return out_stream.printInt(u64, compile_unit_offset);
%return out_stream.printf("\n");
maybe_fp = *(&const ?&const u8)(fp);
}
},
coff => {
out_stream.write("(stack trace unavailable for COFF object format)\n");
},
macho => {
%return out_stream.write("(stack trace unavailable for Mach-O object format)\n");
},
unknown => {
out_stream.write("(stack trace unavailable for unknown object format)\n");
},
}
}
struct ElfStackTrace {
self_exe_stream: io.InStream,
elf: elf.Elf,
aranges: ?&elf.SectionHeader,
debug_info: &elf.SectionHeader,
debug_abbrev: &elf.SectionHeader,
}
enum FormValue {
Address: u64,
Block: []u8,
Const: Constant,
ExprLoc: []u8,
Flag: bool,
SecOffset: u64,
Ref: []u8,
RefAddr: u64,
RefSig8: u64,
String: []u8,
StrPtr: u64,
}
struct Constant {
payload: []u8,
signed: bool,
}
fn readAllocBytes(in_stream: &io.InStream, size: usize) -> %[]u8 {
const buf = %return global_allocator.alloc(u8, size);
%defer global_allocator.free(u8, buf);
%return in_stream.read(buf);
return buf;
}
fn parseFormValueBlockLen(in_stream: &io.InStream, size: usize) -> %FormValue {
const buf = %return readAllocBytes(in_stream, size);
return FormValue.Block { buf };
}
fn parseFormValueBlock(in_stream: &io.InStream, inline T: type) -> %FormValue {
const block_len = %return in_stream.readIntLe(T);
return parseFormValueBlockLen(in_stream, block_len);
}
fn parseFormValueConstantLen(in_stream: &io.InStream, signed: bool, size: usize) -> %FormValue {
const buf = %return readAllocBytes(in_stream, size);
return FormValue.Const { Constant {
.signed = signed,
.payload = buf,
}};
}
fn parseFormValueConstant(in_stream: &io.InStream, signed: bool, inline T: type) -> %FormValue {
const block_len = %return in_stream.readIntLe(T);
return parseFormValueConstantLen(in_stream, signed, block_len);
}
fn parseFormValueAddrSize(in_stream: &io.InStream, is_64: bool) -> %u64 {
return if (is_64) {
%return in_stream.readIntLe(u64)
} else {
u64(%return in_stream.readIntLe(u32))
};
}
fn parseFormValueRefLen(in_stream: &io.InStream, size: usize) -> %FormValue {
const buf = %return readAllocBytes(in_stream, size);
return FormValue.Ref { buf };
}
fn parseFormValueRef(in_stream: &io.InStream, inline T: type) -> %FormValue {
const block_len = %return in_stream.readIntLe(T);
return parseFormValueRefLen(in_stream, block_len);
}
fn parseFormValue(in_stream: &io.InStream, form_id: u64, is_64: bool) -> %FormValue {
return switch (form_id) {
DW.FORM_addr => FormValue.Address {
%return parseFormValueAddrSize(in_stream, is_64)
},
DW.FORM_block1 => parseFormValueBlock(in_stream, u8),
DW.FORM_block2 => parseFormValueBlock(in_stream, u16),
DW.FORM_block4 => parseFormValueBlock(in_stream, u32),
DW.FORM_block => {
const block_len = %return readULeb128(in_stream);
parseFormValueBlockLen(in_stream, block_len)
},
DW.FORM_data1 => parseFormValueConstant(in_stream, false, u8),
DW.FORM_data2 => parseFormValueConstant(in_stream, false, u16),
DW.FORM_data4 => parseFormValueConstant(in_stream, false, u32),
DW.FORM_data8 => parseFormValueConstant(in_stream, false, u64),
DW.FORM_udata, DW.FORM_sdata => {
const block_len = %return readULeb128(in_stream);
const signed = form_id == DW.FORM_sdata;
parseFormValueConstantLen(in_stream, signed, block_len)
},
DW.FORM_exprloc => {
const size = %return readULeb128(in_stream);
const buf = %return readAllocBytes(in_stream, size);
return FormValue.ExprLoc { buf };
},
DW.FORM_flag => FormValue.Flag { (%return in_stream.readByte()) != 0 },
DW.FORM_flag_present => FormValue.Flag { true },
DW.FORM_sec_offset => FormValue.SecOffset {
%return parseFormValueAddrSize(in_stream, is_64)
},
DW.FORM_ref1 => parseFormValueRef(in_stream, u8),
DW.FORM_ref2 => parseFormValueRef(in_stream, u16),
DW.FORM_ref4 => parseFormValueRef(in_stream, u32),
DW.FORM_ref8 => parseFormValueRef(in_stream, u64),
DW.FORM_ref_udata => {
const ref_len = %return readULeb128(in_stream);
parseFormValueRefLen(in_stream, ref_len)
},
DW.FORM_ref_addr => FormValue.RefAddr { %return parseFormValueAddrSize(in_stream, is_64) },
DW.FORM_ref_sig8 => FormValue.RefSig8 { %return in_stream.readIntLe(u64) },
DW.FORM_string => {
var buf: List(u8) = undefined;
buf.init(&global_allocator);
while (true) {
const byte = %return in_stream.readByte();
if (byte == 0)
break;
%return buf.append(byte);
}
FormValue.String { buf.items }
},
DW.FORM_strp => FormValue.StrPtr { %return parseFormValueAddrSize(in_stream, is_64) },
DW.FORM_indirect => {
const child_form_id = %return readULeb128(in_stream);
parseFormValue(in_stream, child_form_id, is_64)
},
else => error.InvalidDebugInfo,
}
}
fn findCompileUnitOffset(st: &ElfStackTrace, target_address: usize) -> %u64 {
if (const result ?= %return arangesOffset(st, target_address))
return result;
// iterate over compile units looking for a match with the low pc and high pc
%return st.elf.seekToSection(st.debug_info);
while (true) {
var is_64: bool = undefined;
const unit_length = %return readInitialLength(&st.self_exe_stream, &is_64);
const version = %return st.self_exe_stream.readInt(st.elf.is_big_endian, u16);
if (version != 4) return error.InvalidDebugInfo;
const debug_abbrev_offset = if (is_64) {
%return st.self_exe_stream.readInt(st.elf.is_big_endian, u64)
} else {
%return st.self_exe_stream.readInt(st.elf.is_big_endian, u32)
};
const address_size = %return st.self_exe_stream.readByte();
if (address_size != @sizeOf(usize)) return error.InvalidDebugInfo;
const abbrev_tag_id = %return st.self_exe_stream.readByte();
}
}
fn readInitialLength(in_stream: &io.InStream, is_64: &bool) -> %u64 {
const first_32_bits = %return in_stream.readIntLe(u32);
*is_64 = (first_32_bits == 0xffffffff);
return if (*is_64) {
%return in_stream.readIntLe(u64)
} else {
if (first_32_bits >= 0xfffffff0) return error.InvalidDebugInfo;
u64(first_32_bits)
};
}
fn readULeb128(in_stream: &io.InStream) -> %u64 {
var result: u64 = 0;
var shift: u64 = 0;
while (true) {
const byte = %return in_stream.readByte();
var operand: u64 = undefined;
if (@shlWithOverflow(u64, byte & 0b01111111, shift, &operand))
return error.InvalidDebugInfo;
result |= operand;
if ((byte & 0b10000000) == 0)
return result;
shift += 7;
}
}
fn readILeb128(in_stream: &io.InStream) -> %i64 {
var result: i64 = 0;
var shift: i64 = 0;
while (true) {
const byte = %return in_stream.readByte();
var operand: i64 = undefined;
if (@shlWithOverflow(i64, byte & 0b01111111, shift, &operand))
return error.InvalidDebugInfo;
result |= operand;
shift += 7;
if ((byte & 0b10000000) == 0) {
if (shift < @sizeOf(i64) * 8 && (byte & 0b01000000) != 0)
result |= -(i64(1) << shift);
return result;
}
}
}
fn arangesOffset(st: &ElfStackTrace, target_address: usize) -> %?u64 {
// TODO ability to implicitly cast null to %?T
const aranges = st.aranges ?? return (?u64)(null);
%return st.elf.seekToSection(aranges);
const first_32_bits = %return st.self_exe_stream.readIntLe(u32);
var is_64: bool = undefined;
const unit_length = %return readInitialLength(&st.self_exe_stream, &is_64);
var unit_index: u64 = 0;
while (unit_index < unit_length) {
const version = %return st.self_exe_stream.readIntLe(u16);
if (version != 2) return error.InvalidDebugInfo;
unit_index += 2;
const debug_info_offset = if (is_64) {
unit_index += 4;
%return st.self_exe_stream.readIntLe(u64)
} else {
unit_index += 2;
%return st.self_exe_stream.readIntLe(u32)
};
const address_size = %return st.self_exe_stream.readByte();
if (address_size > 8) return error.UnsupportedDebugInfo;
unit_index += 1;
const segment_size = %return st.self_exe_stream.readByte();
if (segment_size > 0) return error.UnsupportedDebugInfo;
unit_index += 1;
const align = segment_size + 2 * address_size;
const padding = (%return st.self_exe_stream.getPos()) % align;
%return st.self_exe_stream.seekForward(padding);
unit_index += padding;
while (true) {
const address = %return st.self_exe_stream.readVarInt(false, u64, address_size);
const length = %return st.self_exe_stream.readVarInt(false, u64, address_size);
unit_index += align;
if (address == 0 && length == 0) break;
if (target_address >= address && target_address < address + length) {
// TODO ability to implicitly cast T to %?T
return (?u64)(debug_info_offset);
}
}
}
return error.MissingDebugInfo;
}
pub var global_allocator = Allocator {
.allocFn = globalAlloc,
.reallocFn = globalRealloc,
.freeFn = globalFree,
.context = null,
};
var some_mem: [10 * 1024]u8 = undefined;
var some_mem_index: usize = 0;
fn globalAlloc(self: &Allocator, n: usize) -> %[]u8 {
const result = some_mem[some_mem_index ... some_mem_index + n];
some_mem_index += n;
return result;
}
fn globalRealloc(self: &Allocator, old_mem: []u8, new_size: usize) -> %[]u8 {
const result = %return globalAlloc(self, new_size);
@memcpy(result.ptr, old_mem.ptr, old_mem.len);
return result;
}
fn globalFree(self: &Allocator, old_mem: []u8) { }
|