authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-09-23 02:00:23-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-09-23 02:00:23-04:00
log46eb77dbb200756b96bfae4c5166397fefba66d0
tree618f0117563dfabbe0e4253f6fa817e526b5a64c
parent4b902b44a244e29106637f9777aa71b1cd4c22d2

stack trace is able to figure out compilation unit

each address is contained within also fix a bug having to do with codegen for enum value initialization expressions

9 files changed, 376 insertions(+), 174 deletions(-)

src/codegen.cpp+1-1
...@@ -729,7 +729,6 @@ static LLVMValueRef gen_enum_value_expr(CodeGen *g, AstNode *node, TypeTableEntr...@@ -729,7 +729,6 @@ static LLVMValueRef gen_enum_value_expr(CodeGen *g, AstNode *node, TypeTableEntr
729 LLVMValueRef new_union_val = gen_expr(g, arg_node);729 LLVMValueRef new_union_val = gen_expr(g, arg_node);
730 if (arg_node) {730 if (arg_node) {
731 arg_node_type = get_expr_type(arg_node);731 arg_node_type = get_expr_type(arg_node);
732 new_union_val = gen_expr(g, arg_node);
733 } else {732 } else {
734 arg_node_type = g->builtin_types.entry_void;733 arg_node_type = g->builtin_types.entry_void;
735 }734 }
...@@ -3460,6 +3459,7 @@ static LLVMValueRef gen_switch_expr(CodeGen *g, AstNode *node) {...@@ -3460,6 +3459,7 @@ static LLVMValueRef gen_switch_expr(CodeGen *g, AstNode *node) {
3460 zig_unreachable();3459 zig_unreachable();
3461 }3460 }
3462 if (make_item_blocks) {3461 if (make_item_blocks) {
3462 set_debug_source_node(g, var_node);
3463 LLVMBuildBr(g->builder, prong_block);3463 LLVMBuildBr(g->builder, prong_block);
3464 }3464 }
3465 } else {3465 } else {
std/cstr.zig+19-17
...@@ -38,33 +38,37 @@ pub struct CBuf {...@@ -38,33 +38,37 @@ pub struct CBuf {
38 list: List(u8),38 list: List(u8),
3939
40 /// Must deinitialize with deinit.40 /// Must deinitialize with deinit.
41 pub fn init(self: &CBuf, allocator: &Allocator) {41 pub fn initEmpty(allocator: &Allocator) -> %CBuf {
42 self.list.init(allocator);42 const self = CBuf {
43 // This resize is guaranteed to not have an error because we use a list43 .list = List(u8).init(allocator),
44 // with preallocated memory of at least 1 byte.44 };
45 %%self.resize(0);45 %return self.resize(0);
46 return self;
46 }47 }
4748
48 /// Must deinitialize with deinit.49 /// Must deinitialize with deinit.
49 pub fn initFromMem(self: &CBuf, allocator: &Allocator, m: []const u8) -> %void {50 pub fn initFromMem(allocator: &Allocator, m: []const u8) -> %CBuf {
50 self.init(allocator);51 const self = CBuf {
52 .list = List(u8).init(allocator),
53 };
51 %return self.resize(m.len);54 %return self.resize(m.len);
52 mem.copy(u8, self.list.items, m);55 mem.copy(u8, self.list.items, m);
56 return self;
53 }57 }
5458
55 /// Must deinitialize with deinit.59 /// Must deinitialize with deinit.
56 pub fn initFromCStr(self: &CBuf, allocator: &Allocator, s: &const u8) -> %void {60 pub fn initFromCStr(allocator: &Allocator, s: &const u8) -> %CBuf {
57 self.initFromMem(allocator, s[0...strlen(s)])61 return CBuf.initFromMem(allocator, s[0...strlen(s)]);
58 }62 }
5963
60 /// Must deinitialize with deinit.64 /// Must deinitialize with deinit.
61 pub fn initFromCBuf(self: &CBuf, cbuf: &const CBuf) -> %void {65 pub fn initFromCBuf(cbuf: &const CBuf) -> %CBuf {
62 self.initFromMem(cbuf.list.allocator, cbuf.list.items[0...cbuf.len()])66 return CBuf.initFromMem(cbuf.list.allocator, cbuf.list.items[0...cbuf.len()]);
63 }67 }
6468
65 /// Must deinitialize with deinit.69 /// Must deinitialize with deinit.
66 pub fn initFromSlice(self: &CBuf, other: &const CBuf, start: usize, end: usize) -> %void {70 pub fn initFromSlice(other: &const CBuf, start: usize, end: usize) -> %CBuf {
67 self.initFromMem(other.list.allocator, other.list.items[start...end])71 return CBuf.initFromMem(other.list.allocator, other.list.items[start...end]);
68 }72 }
6973
70 pub fn deinit(self: &CBuf) {74 pub fn deinit(self: &CBuf) {
...@@ -124,8 +128,7 @@ pub struct CBuf {...@@ -124,8 +128,7 @@ pub struct CBuf {
124128
125#attribute("test")129#attribute("test")
126fn testSimpleCBuf() {130fn testSimpleCBuf() {
127 var buf: CBuf = undefined;131 var buf = %%CBuf.initEmpty(&debug.global_allocator);
128 buf.init(&debug.global_allocator);
129 assert(buf.len() == 0);132 assert(buf.len() == 0);
130 %%buf.appendCStr(c"hello");133 %%buf.appendCStr(c"hello");
131 %%buf.appendChar(' ');134 %%buf.appendChar(' ');
...@@ -133,8 +136,7 @@ fn testSimpleCBuf() {...@@ -133,8 +136,7 @@ fn testSimpleCBuf() {
133 assert(buf.eqlCStr(c"hello world"));136 assert(buf.eqlCStr(c"hello world"));
134 assert(buf.eqlMem("hello world"));137 assert(buf.eqlMem("hello world"));
135138
136 var buf2: CBuf = undefined;139 var buf2 = %%CBuf.initFromCBuf(&buf);
137 %%buf2.initFromCBuf(&buf);
138 assert(buf.eqlCBuf(&buf2));140 assert(buf.eqlCBuf(&buf2));
139141
140 assert(buf.startsWithMem("hell"));142 assert(buf.startsWithMem("hell"));
std/debug.zig+263-112
...@@ -1,4 +1,4 @@...@@ -1,4 +1,4 @@
1const Allocator = @import("mem.zig").Allocator;1const mem = @import("mem.zig");
2const io = @import("io.zig");2const io = @import("io.zig");
3const os = @import("os.zig");3const os = @import("os.zig");
4const elf = @import("elf.zig");4const elf = @import("elf.zig");
...@@ -21,28 +21,38 @@ pub fn printStackTrace() -> %void {...@@ -21,28 +21,38 @@ pub fn printStackTrace() -> %void {
21pub fn writeStackTrace(out_stream: &io.OutStream) -> %void {21pub fn writeStackTrace(out_stream: &io.OutStream) -> %void {
22 switch (@compileVar("object_format")) {22 switch (@compileVar("object_format")) {
23 elf => {23 elf => {
24 var st: ElfStackTrace = undefined;24 var stack_trace = ElfStackTrace {
25 .self_exe_stream = undefined,
26 .elf = undefined,
27 .debug_info = undefined,
28 .debug_abbrev = undefined,
29 .debug_str = undefined,
30 .abbrev_table_list = List(AbbrevTableHeader).init(&global_allocator),
31 .compile_unit_list = List(CompileUnit).init(&global_allocator),
32 };
33 const st = &stack_trace;
25 %return io.openSelfExe(&st.self_exe_stream);34 %return io.openSelfExe(&st.self_exe_stream);
26 defer %return st.self_exe_stream.close();35 defer st.self_exe_stream.close() %% {};
2736
28 %return st.elf.openStream(&global_allocator, &st.self_exe_stream);37 %return st.elf.openStream(&global_allocator, &st.self_exe_stream);
29 defer %return st.elf.close();38 defer %return st.elf.close();
3039
31 st.aranges = %return st.elf.findSection(".debug_aranges");
32 st.debug_info = (%return st.elf.findSection(".debug_info")) ?? return error.MissingDebugInfo;40 st.debug_info = (%return st.elf.findSection(".debug_info")) ?? return error.MissingDebugInfo;
33 st.debug_abbrev = (%return st.elf.findSection(".debug_abbrev")) ?? return error.MissingDebugInfo;41 st.debug_abbrev = (%return st.elf.findSection(".debug_abbrev")) ?? return error.MissingDebugInfo;
42 st.debug_str = (%return st.elf.findSection(".debug_str")) ?? return error.MissingDebugInfo;
43 %return scanAllCompileUnits(st);
3444
35 var maybe_fp: ?&const u8 = @frameAddress();45 var maybe_fp: ?&const u8 = @frameAddress();
36 while (true) {46 while (true) {
37 const fp = maybe_fp ?? break;47 const fp = maybe_fp ?? break;
38 const return_address = *(&const usize)(usize(fp) + @sizeOf(usize));48 const return_address = *(&const usize)(usize(fp) + @sizeOf(usize));
3949
40 // read .debug_aranges to find out which compile unit the address is in50 const compile_unit = findCompileUnit(st, return_address) ?? return error.MissingDebugInfo;
41 const compile_unit_offset = %return findCompileUnitOffset(&st, return_address);51 const name = %return compile_unit.die.getAttrString(st, DW.AT_name);
4252
43 %return out_stream.printInt(usize, return_address);53 %return out_stream.printInt(usize, return_address);
44 %return out_stream.printf(" -> ");54 %return out_stream.printf(" -> ");
45 %return out_stream.printInt(u64, compile_unit_offset);55 %return out_stream.printf(name);
46 %return out_stream.printf("\n");56 %return out_stream.printf("\n");
47 maybe_fp = *(&const ?&const u8)(fp);57 maybe_fp = *(&const ?&const u8)(fp);
48 }58 }
...@@ -62,9 +72,38 @@ pub fn writeStackTrace(out_stream: &io.OutStream) -> %void {...@@ -62,9 +72,38 @@ pub fn writeStackTrace(out_stream: &io.OutStream) -> %void {
62struct ElfStackTrace {72struct ElfStackTrace {
63 self_exe_stream: io.InStream,73 self_exe_stream: io.InStream,
64 elf: elf.Elf,74 elf: elf.Elf,
65 aranges: ?&elf.SectionHeader,
66 debug_info: &elf.SectionHeader,75 debug_info: &elf.SectionHeader,
67 debug_abbrev: &elf.SectionHeader,76 debug_abbrev: &elf.SectionHeader,
77 debug_str: &elf.SectionHeader,
78 abbrev_table_list: List(AbbrevTableHeader),
79 compile_unit_list: List(CompileUnit),
80}
81
82struct CompileUnit {
83 is_64: bool,
84 die: &Die,
85 pc_start: u64,
86 pc_end: u64,
87}
88
89const AbbrevTable = List(AbbrevTableEntry);
90
91struct AbbrevTableHeader {
92 // offset from .debug_abbrev
93 offset: u64,
94 table: AbbrevTable,
95}
96
97struct AbbrevTableEntry {
98 has_children: bool,
99 abbrev_code: u64,
100 tag_id: u64,
101 attrs: List(AbbrevAttr),
102}
103
104struct AbbrevAttr {
105 attr_id: u64,
106 form_id: u64,
68}107}
69108
70enum FormValue {109enum FormValue {
...@@ -84,8 +123,76 @@ enum FormValue {...@@ -84,8 +123,76 @@ enum FormValue {
84struct Constant {123struct Constant {
85 payload: []u8,124 payload: []u8,
86 signed: bool,125 signed: bool,
126
127 fn asUnsignedLe(self: &const Constant) -> %u64 {
128 if (self.payload.len > @sizeOf(u64))
129 return error.InvalidDebugInfo;
130 if (self.signed)
131 return error.InvalidDebugInfo;
132 return mem.sliceAsInt(self.payload, false, u64);
133 }
134}
135
136struct Die {
137 tag_id: u64,
138 has_children: bool,
139 attrs: List(Attr),
140
141 struct Attr {
142 id: u64,
143 value: FormValue,
144 }
145
146 fn getAttr(self: &const Die, id: u64) -> ?&const FormValue {
147 for (self.attrs.toSlice()) |*attr| {
148 if (attr.id == id)
149 return &attr.value;
150 }
151 return null;
152 }
153
154 fn getAttrAddr(self: &const Die, id: u64) -> %u64 {
155 const form_value = self.getAttr(id) ?? return error.InvalidDebugInfo;
156 return switch (*form_value) {
157 Address => |value| value,
158 else => error.InvalidDebugInfo,
159 };
160 }
161
162 fn getAttrUnsignedLe(self: &const Die, id: u64) -> %u64 {
163 const form_value = self.getAttr(id) ?? return error.InvalidDebugInfo;
164 return switch (*form_value) {
165 Const => |value| value.asUnsignedLe(),
166 else => error.InvalidDebugInfo,
167 };
168 }
169
170 fn getAttrString(self: &const Die, st: &ElfStackTrace, id: u64) -> %[]u8 {
171 const form_value = self.getAttr(id) ?? return error.InvalidDebugInfo;
172 return switch (*form_value) {
173 String => |value| value,
174 StrPtr => |offset| getString(st, offset),
175 else => error.InvalidDebugInfo,
176 }
177 }
87}178}
88179
180fn readString(in_stream: &io.InStream) -> %[]u8 {
181 var buf = List(u8).init(&global_allocator);
182 while (true) {
183 const byte = %return in_stream.readByte();
184 if (byte == 0)
185 break;
186 %return buf.append(byte);
187 }
188 return buf.items;
189}
190
191fn getString(st: &ElfStackTrace, offset: u64) -> %[]u8 {
192 const pos = st.debug_str.offset + offset;
193 %return st.self_exe_stream.seekTo(pos);
194 return readString(&st.self_exe_stream);
195}
89196
90fn readAllocBytes(in_stream: &io.InStream, size: usize) -> %[]u8 {197fn readAllocBytes(in_stream: &io.InStream, size: usize) -> %[]u8 {
91 const buf = %return global_allocator.alloc(u8, size);198 const buf = %return global_allocator.alloc(u8, size);
...@@ -99,25 +206,19 @@ fn parseFormValueBlockLen(in_stream: &io.InStream, size: usize) -> %FormValue {...@@ -99,25 +206,19 @@ fn parseFormValueBlockLen(in_stream: &io.InStream, size: usize) -> %FormValue {
99 return FormValue.Block { buf };206 return FormValue.Block { buf };
100}207}
101208
102fn parseFormValueBlock(in_stream: &io.InStream, inline T: type) -> %FormValue {209fn parseFormValueBlock(in_stream: &io.InStream, size: usize) -> %FormValue {
103 const block_len = %return in_stream.readIntLe(T);210 const block_len = %return in_stream.readVarInt(false, usize, size);
104 return parseFormValueBlockLen(in_stream, block_len);211 return parseFormValueBlockLen(in_stream, block_len);
105}212}
106213
107fn parseFormValueConstantLen(in_stream: &io.InStream, signed: bool, size: usize) -> %FormValue {214fn parseFormValueConstant(in_stream: &io.InStream, signed: bool, size: usize) -> %FormValue {
108 const buf = %return readAllocBytes(in_stream, size);215 FormValue.Const { Constant {
109 return FormValue.Const { Constant {
110 .signed = signed,216 .signed = signed,
111 .payload = buf,217 .payload = %return readAllocBytes(in_stream, size),
112 }};218 }}
113}219}
114220
115fn parseFormValueConstant(in_stream: &io.InStream, signed: bool, inline T: type) -> %FormValue {221fn parseFormValueDwarfOffsetSize(in_stream: &io.InStream, is_64: bool) -> %u64 {
116 const block_len = %return in_stream.readIntLe(T);
117 return parseFormValueConstantLen(in_stream, signed, block_len);
118}
119
120fn parseFormValueAddrSize(in_stream: &io.InStream, is_64: bool) -> %u64 {
121 return if (is_64) {222 return if (is_64) {
122 %return in_stream.readIntLe(u64)223 %return in_stream.readIntLe(u64)
123 } else {224 } else {
...@@ -125,6 +226,16 @@ fn parseFormValueAddrSize(in_stream: &io.InStream, is_64: bool) -> %u64 {...@@ -125,6 +226,16 @@ fn parseFormValueAddrSize(in_stream: &io.InStream, is_64: bool) -> %u64 {
125 };226 };
126}227}
127228
229fn parseFormValueTargetAddrSize(in_stream: &io.InStream) -> %u64 {
230 return if (@sizeOf(usize) == 4) {
231 u64(%return in_stream.readIntLe(u32))
232 } else if (@sizeOf(usize) == 8) {
233 %return in_stream.readIntLe(u64)
234 } else {
235 @unreachable();
236 };
237}
238
128fn parseFormValueRefLen(in_stream: &io.InStream, size: usize) -> %FormValue {239fn parseFormValueRefLen(in_stream: &io.InStream, size: usize) -> %FormValue {
129 const buf = %return readAllocBytes(in_stream, size);240 const buf = %return readAllocBytes(in_stream, size);
130 return FormValue.Ref { buf };241 return FormValue.Ref { buf };
...@@ -137,24 +248,22 @@ fn parseFormValueRef(in_stream: &io.InStream, inline T: type) -> %FormValue {...@@ -137,24 +248,22 @@ fn parseFormValueRef(in_stream: &io.InStream, inline T: type) -> %FormValue {
137248
138fn parseFormValue(in_stream: &io.InStream, form_id: u64, is_64: bool) -> %FormValue {249fn parseFormValue(in_stream: &io.InStream, form_id: u64, is_64: bool) -> %FormValue {
139 return switch (form_id) {250 return switch (form_id) {
140 DW.FORM_addr => FormValue.Address {251 DW.FORM_addr => FormValue.Address { %return parseFormValueTargetAddrSize(in_stream) },
141 %return parseFormValueAddrSize(in_stream, is_64)252 DW.FORM_block1 => parseFormValueBlock(in_stream, 1),
142 },253 DW.FORM_block2 => parseFormValueBlock(in_stream, 2),
143 DW.FORM_block1 => parseFormValueBlock(in_stream, u8),254 DW.FORM_block4 => parseFormValueBlock(in_stream, 4),
144 DW.FORM_block2 => parseFormValueBlock(in_stream, u16),
145 DW.FORM_block4 => parseFormValueBlock(in_stream, u32),
146 DW.FORM_block => {255 DW.FORM_block => {
147 const block_len = %return readULeb128(in_stream);256 const block_len = %return readULeb128(in_stream);
148 parseFormValueBlockLen(in_stream, block_len)257 parseFormValueBlockLen(in_stream, block_len)
149 },258 },
150 DW.FORM_data1 => parseFormValueConstant(in_stream, false, u8),259 DW.FORM_data1 => parseFormValueConstant(in_stream, false, 1),
151 DW.FORM_data2 => parseFormValueConstant(in_stream, false, u16),260 DW.FORM_data2 => parseFormValueConstant(in_stream, false, 2),
152 DW.FORM_data4 => parseFormValueConstant(in_stream, false, u32),261 DW.FORM_data4 => parseFormValueConstant(in_stream, false, 4),
153 DW.FORM_data8 => parseFormValueConstant(in_stream, false, u64),262 DW.FORM_data8 => parseFormValueConstant(in_stream, false, 8),
154 DW.FORM_udata, DW.FORM_sdata => {263 DW.FORM_udata, DW.FORM_sdata => {
155 const block_len = %return readULeb128(in_stream);264 const block_len = %return readULeb128(in_stream);
156 const signed = form_id == DW.FORM_sdata;265 const signed = form_id == DW.FORM_sdata;
157 parseFormValueConstantLen(in_stream, signed, block_len)266 parseFormValueConstant(in_stream, signed, block_len)
158 },267 },
159 DW.FORM_exprloc => {268 DW.FORM_exprloc => {
160 const size = %return readULeb128(in_stream);269 const size = %return readULeb128(in_stream);
...@@ -164,7 +273,7 @@ fn parseFormValue(in_stream: &io.InStream, form_id: u64, is_64: bool) -> %FormVa...@@ -164,7 +273,7 @@ fn parseFormValue(in_stream: &io.InStream, form_id: u64, is_64: bool) -> %FormVa
164 DW.FORM_flag => FormValue.Flag { (%return in_stream.readByte()) != 0 },273 DW.FORM_flag => FormValue.Flag { (%return in_stream.readByte()) != 0 },
165 DW.FORM_flag_present => FormValue.Flag { true },274 DW.FORM_flag_present => FormValue.Flag { true },
166 DW.FORM_sec_offset => FormValue.SecOffset {275 DW.FORM_sec_offset => FormValue.SecOffset {
167 %return parseFormValueAddrSize(in_stream, is_64)276 %return parseFormValueDwarfOffsetSize(in_stream, is_64)
168 },277 },
169278
170 DW.FORM_ref1 => parseFormValueRef(in_stream, u8),279 DW.FORM_ref1 => parseFormValueRef(in_stream, u8),
...@@ -176,22 +285,11 @@ fn parseFormValue(in_stream: &io.InStream, form_id: u64, is_64: bool) -> %FormVa...@@ -176,22 +285,11 @@ fn parseFormValue(in_stream: &io.InStream, form_id: u64, is_64: bool) -> %FormVa
176 parseFormValueRefLen(in_stream, ref_len)285 parseFormValueRefLen(in_stream, ref_len)
177 },286 },
178287
179 DW.FORM_ref_addr => FormValue.RefAddr { %return parseFormValueAddrSize(in_stream, is_64) },288 DW.FORM_ref_addr => FormValue.RefAddr { %return parseFormValueDwarfOffsetSize(in_stream, is_64) },
180 DW.FORM_ref_sig8 => FormValue.RefSig8 { %return in_stream.readIntLe(u64) },289 DW.FORM_ref_sig8 => FormValue.RefSig8 { %return in_stream.readIntLe(u64) },
181290
182 DW.FORM_string => {291 DW.FORM_string => FormValue.String { %return readString(in_stream) },
183 var buf: List(u8) = undefined; 292 DW.FORM_strp => FormValue.StrPtr { %return parseFormValueDwarfOffsetSize(in_stream, is_64) },
184 buf.init(&global_allocator);
185 while (true) {
186 const byte = %return in_stream.readByte();
187 if (byte == 0)
188 break;
189 %return buf.append(byte);
190 }
191
192 FormValue.String { buf.items }
193 },
194 DW.FORM_strp => FormValue.StrPtr { %return parseFormValueAddrSize(in_stream, is_64) },
195 DW.FORM_indirect => {293 DW.FORM_indirect => {
196 const child_form_id = %return readULeb128(in_stream);294 const child_form_id = %return readULeb128(in_stream);
197 parseFormValue(in_stream, child_form_id, is_64)295 parseFormValue(in_stream, child_form_id, is_64)
...@@ -200,16 +298,87 @@ fn parseFormValue(in_stream: &io.InStream, form_id: u64, is_64: bool) -> %FormVa...@@ -200,16 +298,87 @@ fn parseFormValue(in_stream: &io.InStream, form_id: u64, is_64: bool) -> %FormVa
200 }298 }
201}299}
202300
203fn findCompileUnitOffset(st: &ElfStackTrace, target_address: usize) -> %u64 {301fn parseAbbrevTable(in_stream: &io.InStream) -> %AbbrevTable {
204 if (const result ?= %return arangesOffset(st, target_address))302 var result = AbbrevTable.init(&global_allocator);
205 return result;303 while (true) {
304 const abbrev_code = %return readULeb128(in_stream);
305 if (abbrev_code == 0)
306 return result;
307 %return result.append(AbbrevTableEntry {
308 .abbrev_code = abbrev_code,
309 .tag_id = %return readULeb128(in_stream),
310 .has_children = (%return in_stream.readByte()) == DW.CHILDREN_yes,
311 .attrs = List(AbbrevAttr).init(&global_allocator),
312 });
313 const attrs = &result.items[result.len - 1].attrs;
206314
207 // iterate over compile units looking for a match with the low pc and high pc315 while (true) {
208 %return st.elf.seekToSection(st.debug_info);316 const attr_id = %return readULeb128(in_stream);
317 const form_id = %return readULeb128(in_stream);
318 if (attr_id == 0 && form_id == 0)
319 break;
320 %return attrs.append(AbbrevAttr {
321 .attr_id = attr_id,
322 .form_id = form_id,
323 });
324 }
325 }
326}
327
328/// Gets an already existing AbbrevTable given the abbrev_offset, or if not found,
329/// seeks in the stream and parses it.
330fn getAbbrevTable(st: &ElfStackTrace, abbrev_offset: u64) -> %&AbbrevTable {
331 for (st.abbrev_table_list.toSlice()) |header| {
332 if (header.offset == abbrev_offset) {
333 return &header.table;
334 }
335 }
336 %return st.self_exe_stream.seekTo(st.debug_abbrev.offset + abbrev_offset);
337 %return st.abbrev_table_list.append(AbbrevTableHeader {
338 .offset = abbrev_offset,
339 .table = %return parseAbbrevTable(&st.self_exe_stream),
340 });
341 return &st.abbrev_table_list.items[st.abbrev_table_list.len - 1].table;
342}
343
344fn getAbbrevTableEntry(abbrev_table: &const AbbrevTable, abbrev_code: u64) -> ?&const AbbrevTableEntry {
345 for (abbrev_table.toSlice()) |*table_entry| {
346 if (table_entry.abbrev_code == abbrev_code)
347 return table_entry;
348 }
349 return null;
350}
351
352fn parseDie(in_stream: &io.InStream, abbrev_table: &const AbbrevTable, is_64: bool) -> %Die {
353 const abbrev_code = %return readULeb128(in_stream);
354 const table_entry = getAbbrevTableEntry(abbrev_table, abbrev_code) ?? return error.InvalidDebugInfo;
355
356 var result = Die {
357 .tag_id = table_entry.tag_id,
358 .has_children = table_entry.has_children,
359 .attrs = List(Die.Attr).init(&global_allocator),
360 };
361 %return result.attrs.resize(table_entry.attrs.len);
362 for (table_entry.attrs.toSlice()) |attr, i| {
363 result.attrs.items[i] = Die.Attr {
364 .id = attr.attr_id,
365 .value = %return parseFormValue(in_stream, attr.form_id, is_64),
366 };
367 }
368 return result;
369}
370
371fn scanAllCompileUnits(st: &ElfStackTrace) -> %void {
372 const debug_info_end = st.debug_info.offset + st.debug_info.size;
373 var this_unit_offset = st.debug_info.offset;
374 while (this_unit_offset < debug_info_end) {
375 %return st.self_exe_stream.seekTo(this_unit_offset);
209376
210 while (true) {
211 var is_64: bool = undefined;377 var is_64: bool = undefined;
212 const unit_length = %return readInitialLength(&st.self_exe_stream, &is_64);378 const unit_length = %return readInitialLength(&st.self_exe_stream, &is_64);
379 if (unit_length == 0)
380 return;
381 const next_offset = unit_length + (if (is_64) usize(12) else usize(4));
213382
214 const version = %return st.self_exe_stream.readInt(st.elf.is_big_endian, u16);383 const version = %return st.self_exe_stream.readInt(st.elf.is_big_endian, u16);
215 if (version != 4) return error.InvalidDebugInfo;384 if (version != 4) return error.InvalidDebugInfo;
...@@ -223,10 +392,45 @@ fn findCompileUnitOffset(st: &ElfStackTrace, target_address: usize) -> %u64 {...@@ -223,10 +392,45 @@ fn findCompileUnitOffset(st: &ElfStackTrace, target_address: usize) -> %u64 {
223 const address_size = %return st.self_exe_stream.readByte();392 const address_size = %return st.self_exe_stream.readByte();
224 if (address_size != @sizeOf(usize)) return error.InvalidDebugInfo;393 if (address_size != @sizeOf(usize)) return error.InvalidDebugInfo;
225394
226 const abbrev_tag_id = %return st.self_exe_stream.readByte();395 const compile_unit_pos = %return st.self_exe_stream.getPos();
396 const abbrev_table = %return getAbbrevTable(st, debug_abbrev_offset);
397
398 %return st.self_exe_stream.seekTo(compile_unit_pos);
227399
400 const compile_unit_die = (%return global_allocator.alloc(Die, 1)).ptr;
401 *compile_unit_die = %return parseDie(&st.self_exe_stream, abbrev_table, is_64);
228402
403 if (compile_unit_die.tag_id != DW.TAG_compile_unit)
404 return error.InvalidDebugInfo;
405 const low_pc = %return compile_unit_die.getAttrAddr(DW.AT_low_pc);
406
407 const high_pc_value = compile_unit_die.getAttr(DW.AT_high_pc) ?? return error.MissingDebugInfo;
408 const pc_end = switch (*high_pc_value) {
409 Address => |value| value,
410 Const => |value| {
411 const offset = %return value.asUnsignedLe();
412 low_pc + offset
413 },
414 else => return error.InvalidDebugInfo,
415 };
416
417 %return st.compile_unit_list.append(CompileUnit {
418 .is_64 = is_64,
419 .pc_start = low_pc,
420 .pc_end = pc_end,
421 .die = compile_unit_die,
422 });
423
424 this_unit_offset += next_offset;
425 }
426}
427
428fn findCompileUnit(st: &ElfStackTrace, target_address: u64) -> ?&const CompileUnit {
429 for (st.compile_unit_list.toSlice()) |*compile_unit| {
430 if (target_address >= compile_unit.pc_start && target_address < compile_unit.pc_end)
431 return compile_unit;
229 }432 }
433 return null;
230}434}
231435
232fn readInitialLength(in_stream: &io.InStream, is_64: &bool) -> %u64 {436fn readInitialLength(in_stream: &io.InStream, is_64: &bool) -> %u64 {
...@@ -283,79 +487,26 @@ fn readILeb128(in_stream: &io.InStream) -> %i64 {...@@ -283,79 +487,26 @@ fn readILeb128(in_stream: &io.InStream) -> %i64 {
283 }487 }
284}488}
285489
286fn arangesOffset(st: &ElfStackTrace, target_address: usize) -> %?u64 {490pub var global_allocator = mem.Allocator {
287 // TODO ability to implicitly cast null to %?T
288 const aranges = st.aranges ?? return (?u64)(null);
289
290 %return st.elf.seekToSection(aranges);
291
292 const first_32_bits = %return st.self_exe_stream.readIntLe(u32);
293 var is_64: bool = undefined;
294 const unit_length = %return readInitialLength(&st.self_exe_stream, &is_64);
295 var unit_index: u64 = 0;
296
297 while (unit_index < unit_length) {
298 const version = %return st.self_exe_stream.readIntLe(u16);
299 if (version != 2) return error.InvalidDebugInfo;
300 unit_index += 2;
301
302 const debug_info_offset = if (is_64) {
303 unit_index += 4;
304 %return st.self_exe_stream.readIntLe(u64)
305 } else {
306 unit_index += 2;
307 %return st.self_exe_stream.readIntLe(u32)
308 };
309
310 const address_size = %return st.self_exe_stream.readByte();
311 if (address_size > 8) return error.UnsupportedDebugInfo;
312 unit_index += 1;
313
314 const segment_size = %return st.self_exe_stream.readByte();
315 if (segment_size > 0) return error.UnsupportedDebugInfo;
316 unit_index += 1;
317
318 const align = segment_size + 2 * address_size;
319 const padding = (%return st.self_exe_stream.getPos()) % align;
320 %return st.self_exe_stream.seekForward(padding);
321 unit_index += padding;
322
323 while (true) {
324 const address = %return st.self_exe_stream.readVarInt(false, u64, address_size);
325 const length = %return st.self_exe_stream.readVarInt(false, u64, address_size);
326 unit_index += align;
327 if (address == 0 && length == 0) break;
328
329 if (target_address >= address && target_address < address + length) {
330 // TODO ability to implicitly cast T to %?T
331 return (?u64)(debug_info_offset);
332 }
333 }
334 }
335
336 return error.MissingDebugInfo;
337}
338
339pub var global_allocator = Allocator {
340 .allocFn = globalAlloc,491 .allocFn = globalAlloc,
341 .reallocFn = globalRealloc,492 .reallocFn = globalRealloc,
342 .freeFn = globalFree,493 .freeFn = globalFree,
343 .context = null,494 .context = null,
344};495};
345496
346var some_mem: [10 * 1024]u8 = undefined;497var some_mem: [100 * 1024]u8 = undefined;
347var some_mem_index: usize = 0;498var some_mem_index: usize = 0;
348499
349fn globalAlloc(self: &Allocator, n: usize) -> %[]u8 {500fn globalAlloc(self: &mem.Allocator, n: usize) -> %[]u8 {
350 const result = some_mem[some_mem_index ... some_mem_index + n];501 const result = some_mem[some_mem_index ... some_mem_index + n];
351 some_mem_index += n;502 some_mem_index += n;
352 return result;503 return result;
353}504}
354505
355fn globalRealloc(self: &Allocator, old_mem: []u8, new_size: usize) -> %[]u8 {506fn globalRealloc(self: &mem.Allocator, old_mem: []u8, new_size: usize) -> %[]u8 {
356 const result = %return globalAlloc(self, new_size);507 const result = %return globalAlloc(self, new_size);
357 @memcpy(result.ptr, old_mem.ptr, old_mem.len);508 @memcpy(result.ptr, old_mem.ptr, old_mem.len);
358 return result;509 return result;
359}510}
360511
361fn globalFree(self: &Allocator, old_mem: []u8) { }512fn globalFree(self: &mem.Allocator, old_mem: []u8) { }
std/dwarf.zig+3
...@@ -617,3 +617,6 @@ pub const CFA_MIPS_advance_loc8 = 0x1d;...@@ -617,3 +617,6 @@ pub const CFA_MIPS_advance_loc8 = 0x1d;
617pub const CFA_GNU_window_save = 0x2d;617pub const CFA_GNU_window_save = 0x2d;
618pub const CFA_GNU_args_size = 0x2e;618pub const CFA_GNU_args_size = 0x2e;
619pub const CFA_GNU_negative_offset_extended = 0x2f;619pub const CFA_GNU_negative_offset_extended = 0x2f;
620
621pub const CHILDREN_no = 0x00;
622pub const CHILDREN_yes = 0x01;
std/io.zig+11-16
...@@ -10,6 +10,7 @@ const endian = @import("endian.zig");...@@ -10,6 +10,7 @@ const endian = @import("endian.zig");
10const debug = @import("debug.zig");10const debug = @import("debug.zig");
11const assert = debug.assert;11const assert = debug.assert;
12const os = @import("os.zig");12const os = @import("os.zig");
13const mem = @import("mem.zig");
1314
14pub const stdin_fileno = 0;15pub const stdin_fileno = 0;
15pub const stdout_fileno = 1;16pub const stdout_fileno = 1;
...@@ -261,34 +262,28 @@ pub struct InStream {...@@ -261,34 +262,28 @@ pub struct InStream {
261 return result[0];262 return result[0];
262 }263 }
263264
264 pub inline fn readIntLe(is: &InStream, inline T: type) -> %T {265 pub fn readIntLe(is: &InStream, inline T: type) -> %T {
265 is.readInt(false, T)266 is.readInt(false, T)
266 }267 }
267268
268 pub inline fn readIntBe(is: &InStream, inline T: type) -> %T {269 pub fn readIntBe(is: &InStream, inline T: type) -> %T {
269 is.readInt(true, T)270 is.readInt(true, T)
270 }271 }
271272
272 pub inline fn readInt(is: &InStream, is_be: bool, inline T: type) -> %T {273 pub fn readInt(is: &InStream, is_be: bool, inline T: type) -> %T {
273 var result: T = undefined;274 var result: T = undefined;
274 const result_slice = ([]u8)((&result)[0...1]);275 const result_slice = ([]u8)((&result)[0...1]);
275 %return is.readNoEof(result_slice);276 %return is.readNoEof(result_slice);
276 return endian.swapIf(!is_be, T, result);277 return endian.swapIf(!is_be, T, result);
277 }278 }
278279
279 pub inline fn readVarInt(is: &InStream, is_be: bool, inline T: type, size: usize) -> %T {280 pub fn readVarInt(is: &InStream, is_be: bool, inline T: type, size: usize) -> %T {
280 var result: T = zeroes;281 assert(size <= @sizeOf(T));
281 const result_slice = ([]u8)((&result)[0...1]);282 assert(size <= 8);
282 const padding = @sizeOf(T) - size;283 var input_buf: [8]u8 = undefined;
283 {var i: usize = 0; while (i < size; i += 1) {284 const input_slice = input_buf[0...size];
284 const index = if (is_be == @compileVar("is_big_endian")) {285 %return is.readNoEof(input_slice);
285 padding + i286 return mem.sliceAsInt(input_slice, is_be, T);
286 } else {
287 result_slice.len - i - 1 - padding
288 };
289 result_slice[index] = %return is.readByte();
290 }}
291 return result;
292 }287 }
293288
294 pub fn seekForward(is: &InStream, amount: usize) -> %void {289 pub fn seekForward(is: &InStream, amount: usize) -> %void {
std/list.zig+19-28
...@@ -3,30 +3,27 @@ const assert = debug.assert;...@@ -3,30 +3,27 @@ const assert = debug.assert;
3const mem = @import("mem.zig");3const mem = @import("mem.zig");
4const Allocator = mem.Allocator;4const Allocator = mem.Allocator;
55
6pub fn List(inline T: type) -> type {6pub struct List(T: type) {
7 SmallList(T, @sizeOf(usize))7 const Self = List(T);
8}
9
10// TODO: make sure that setting static_size to 0 codegens to the same code
11// as if this were programmed without static_size at all.
12pub struct SmallList(T: type, static_size: usize) {
13 const Self = SmallList(T, static_size);
148
15 items: []T,9 items: []T,
16 len: usize,10 len: usize,
17 prealloc_items: [static_size]T,
18 allocator: &Allocator,11 allocator: &Allocator,
1912
20 pub fn init(l: &Self, allocator: &Allocator) {13 pub fn init(allocator: &Allocator) -> Self {
21 l.items = l.prealloc_items[0...];14 Self {
22 l.len = 0;15 .items = zeroes,
23 l.allocator = allocator;16 .len = 0,
17 .allocator = allocator,
18 }
24 }19 }
2520
26 pub fn deinit(l: &Self) {21 pub fn deinit(l: &Self) {
27 if (l.items.ptr != &l.prealloc_items[0]) {22 l.allocator.free(T, l.items);
28 l.allocator.free(T, l.items);23 }
29 }24
25 pub fn toSlice(l: &Self) -> []T {
26 return l.items[0...l.len];
30 }27 }
3128
32 pub fn append(l: &Self, item: T) -> %void {29 pub fn append(l: &Self, item: T) -> %void {
...@@ -43,24 +40,18 @@ pub struct SmallList(T: type, static_size: usize) {...@@ -43,24 +40,18 @@ pub struct SmallList(T: type, static_size: usize) {
4340
44 pub fn ensureCapacity(l: &Self, new_capacity: usize) -> %void {41 pub fn ensureCapacity(l: &Self, new_capacity: usize) -> %void {
45 var better_capacity = l.items.len;42 var better_capacity = l.items.len;
46 while (better_capacity < new_capacity) {43 if (better_capacity >= new_capacity) return;
47 better_capacity *= 2;44 while (true) {
48 }45 better_capacity += better_capacity / 2 + 8;
49 if (better_capacity != l.items.len) {46 if (better_capacity >= new_capacity) break;
50 if (l.items.ptr == &l.prealloc_items[0]) {
51 l.items = %return l.allocator.alloc(T, better_capacity);
52 mem.copy(T, l.items, l.prealloc_items[0...l.len]);
53 } else {
54 l.items = %return l.allocator.realloc(T, l.items, better_capacity);
55 }
56 }47 }
48 l.items = %return l.allocator.realloc(T, l.items, better_capacity);
57 }49 }
58}50}
5951
60#attribute("test")52#attribute("test")
61fn basicListTest() {53fn basicListTest() {
62 var list: List(i32) = undefined;54 var list = List(i32).init(&debug.global_allocator);
63 list.init(&debug.global_allocator);
64 defer list.deinit();55 defer list.deinit();
6556
66 {var i: usize = 0; while (i < 10; i += 1) {57 {var i: usize = 0; while (i < 10; i += 1) {
std/mem.zig+30
...@@ -60,3 +60,33 @@ pub fn cmp(inline T: type, a: []const T, b: []const T) -> Cmp {...@@ -60,3 +60,33 @@ pub fn cmp(inline T: type, a: []const T, b: []const T) -> Cmp {
6060
61 return if (a.len > b.len) Cmp.Greater else if (a.len < b.len) Cmp.Less else Cmp.Equal;61 return if (a.len > b.len) Cmp.Greater else if (a.len < b.len) Cmp.Less else Cmp.Equal;
62}62}
63
64pub fn sliceAsInt(buf: []u8, is_be: bool, inline T: type) -> T {
65 var result: T = zeroes;
66 const result_slice = ([]u8)((&result)[0...1]);
67 const padding = @sizeOf(T) - buf.len;
68
69 if (is_be == @compileVar("is_big_endian")) {
70 copy(u8, result_slice, buf);
71 } else {
72 for (buf) |b, i| {
73 const index = result_slice.len - i - 1 - padding;
74 result_slice[index] = b;
75 }
76 }
77 return result;
78}
79
80#attribute("test")
81fn testSliceAsInt() {
82 {
83 const buf = []u8{0x00, 0x00, 0x12, 0x34};
84 const answer = sliceAsInt(buf[0...], true, u64);
85 assert(answer == 0x00001234);
86 }
87 {
88 const buf = []u8{0x12, 0x34, 0x00, 0x00};
89 const answer = sliceAsInt(buf[0...], false, u64);
90 assert(answer == 0x00003412);
91 }
92}
test/cases/switch_prong_err_enum.zig created+29
...@@ -0,0 +1,29 @@
1const assert = @import("std").debug.assert;
2
3var read_count: u64 = 0;
4
5fn readOnce() -> %u64 {
6 read_count += 1;
7 return read_count;
8}
9
10error InvalidDebugInfo;
11
12enum FormValue {
13 Address: u64,
14 Other: bool,
15}
16
17#static_eval_enable(false)
18fn doThing(form_id: u64) -> %FormValue {
19 return switch (form_id) {
20 17 => FormValue.Address { %return readOnce() },
21 else => error.InvalidDebugInfo,
22 }
23}
24
25#attribute("test")
26fn switchProngReturnsErrorEnum() {
27 %%doThing(17);
28 assert(read_count == 1);
29}
test/self_hosted.zig+1
...@@ -12,6 +12,7 @@ const test_max_value_type = @import("cases/max_value_type.zig");...@@ -12,6 +12,7 @@ const test_max_value_type = @import("cases/max_value_type.zig");
12const test_var_params = @import("cases/var_params.zig");12const test_var_params = @import("cases/var_params.zig");
13const test_const_slice_child = @import("cases/const_slice_child.zig");13const test_const_slice_child = @import("cases/const_slice_child.zig");
14const test_switch_prong_implicit_cast = @import("cases/switch_prong_implicit_cast.zig");14const test_switch_prong_implicit_cast = @import("cases/switch_prong_implicit_cast.zig");
15const test_switch_prong_err_enum = @import("cases/switch_prong_err_enum.zig");
1516
16// normal comment17// normal comment
17/// this is a documentation comment18/// this is a documentation comment