authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-04-19 14:12:13-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2022-04-19 14:12:13-04:00
log9c2cbe39c2caa9137e1123fcdf2f326282cad1b5
treeda31a6f7307bcf03a87be3c7497600667b3a7c25
parent78f26b970e1c5c288e45d0edd2ab2f229e6fe924
parentbe08d2bdbd2f64ccb9ef0f985a57a4bf89b9aebb
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #11461 from Luukdegram/wasm-debug-info

Wasm: add support for debug information

6 files changed, 45 insertions(+), 4 deletions(-)

lib/std/debug.zig+36
...@@ -113,6 +113,13 @@ pub fn detectTTYConfig() TTY.Config {...@@ -113,6 +113,13 @@ pub fn detectTTYConfig() TTY.Config {
113/// TODO multithreaded awareness113/// TODO multithreaded awareness
114pub fn dumpCurrentStackTrace(start_addr: ?usize) void {114pub fn dumpCurrentStackTrace(start_addr: ?usize) void {
115 nosuspend {115 nosuspend {
116 if (comptime builtin.target.isWasm()) {
117 if (native_os == .wasi) {
118 const stderr = io.getStdErr().writer();
119 stderr.print("Unable to dump stack trace: not implemented for Wasm\n", .{}) catch return;
120 }
121 return;
122 }
116 const stderr = io.getStdErr().writer();123 const stderr = io.getStdErr().writer();
117 if (builtin.strip_debug_info) {124 if (builtin.strip_debug_info) {
118 stderr.print("Unable to dump stack trace: debug info stripped\n", .{}) catch return;125 stderr.print("Unable to dump stack trace: debug info stripped\n", .{}) catch return;
...@@ -134,6 +141,13 @@ pub fn dumpCurrentStackTrace(start_addr: ?usize) void {...@@ -134,6 +141,13 @@ pub fn dumpCurrentStackTrace(start_addr: ?usize) void {
134/// TODO multithreaded awareness141/// TODO multithreaded awareness
135pub fn dumpStackTraceFromBase(bp: usize, ip: usize) void {142pub fn dumpStackTraceFromBase(bp: usize, ip: usize) void {
136 nosuspend {143 nosuspend {
144 if (comptime builtin.target.isWasm()) {
145 if (native_os == .wasi) {
146 const stderr = io.getStdErr().writer();
147 stderr.print("Unable to dump stack trace: not implemented for Wasm\n", .{}) catch return;
148 }
149 return;
150 }
137 const stderr = io.getStdErr().writer();151 const stderr = io.getStdErr().writer();
138 if (builtin.strip_debug_info) {152 if (builtin.strip_debug_info) {
139 stderr.print("Unable to dump stack trace: debug info stripped\n", .{}) catch return;153 stderr.print("Unable to dump stack trace: debug info stripped\n", .{}) catch return;
...@@ -204,6 +218,13 @@ pub fn captureStackTrace(first_address: ?usize, stack_trace: *std.builtin.StackT...@@ -204,6 +218,13 @@ pub fn captureStackTrace(first_address: ?usize, stack_trace: *std.builtin.StackT
204/// TODO multithreaded awareness218/// TODO multithreaded awareness
205pub fn dumpStackTrace(stack_trace: std.builtin.StackTrace) void {219pub fn dumpStackTrace(stack_trace: std.builtin.StackTrace) void {
206 nosuspend {220 nosuspend {
221 if (comptime builtin.target.isWasm()) {
222 if (native_os == .wasi) {
223 const stderr = io.getStdErr().writer();
224 stderr.print("Unable to dump stack trace: not implemented for Wasm\n", .{}) catch return;
225 }
226 return;
227 }
207 const stderr = io.getStdErr().writer();228 const stderr = io.getStdErr().writer();
208 if (builtin.strip_debug_info) {229 if (builtin.strip_debug_info) {
209 stderr.print("Unable to dump stack trace: debug info stripped\n", .{}) catch return;230 stderr.print("Unable to dump stack trace: debug info stripped\n", .{}) catch return;
...@@ -1134,6 +1155,8 @@ pub const DebugInfo = struct {...@@ -1134,6 +1155,8 @@ pub const DebugInfo = struct {
1134 return self.lookupModuleWin32(address);1155 return self.lookupModuleWin32(address);
1135 } else if (native_os == .haiku) {1156 } else if (native_os == .haiku) {
1136 return self.lookupModuleHaiku(address);1157 return self.lookupModuleHaiku(address);
1158 } else if (comptime builtin.target.isWasm()) {
1159 return self.lookupModuleWasm(address);
1137 } else {1160 } else {
1138 return self.lookupModuleDl(address);1161 return self.lookupModuleDl(address);
1139 }1162 }
...@@ -1349,6 +1372,12 @@ pub const DebugInfo = struct {...@@ -1349,6 +1372,12 @@ pub const DebugInfo = struct {
1349 _ = address;1372 _ = address;
1350 @panic("TODO implement lookup module for Haiku");1373 @panic("TODO implement lookup module for Haiku");
1351 }1374 }
1375
1376 fn lookupModuleWasm(self: *DebugInfo, address: usize) !*ModuleDebugInfo {
1377 _ = self;
1378 _ = address;
1379 @panic("TODO implement lookup module for Wasm");
1380 }
1352};1381};
13531382
1354pub const ModuleDebugInfo = switch (native_os) {1383pub const ModuleDebugInfo = switch (native_os) {
...@@ -1628,6 +1657,13 @@ pub const ModuleDebugInfo = switch (native_os) {...@@ -1628,6 +1657,13 @@ pub const ModuleDebugInfo = switch (native_os) {
1628 return getSymbolFromDwarf(relocated_address, &self.dwarf);1657 return getSymbolFromDwarf(relocated_address, &self.dwarf);
1629 }1658 }
1630 },1659 },
1660 .wasi => struct {
1661 pub fn getSymbolAtAddress(self: *@This(), address: usize) !SymbolInfo {
1662 _ = self;
1663 _ = address;
1664 return SymbolInfo{};
1665 }
1666 },
1631 else => DW.DwarfInfo,1667 else => DW.DwarfInfo,
1632};1668};
16331669
src/arch/wasm/CodeGen.zig+2-1
...@@ -871,7 +871,8 @@ fn genFunc(self: *Self) InnerError!void {...@@ -871,7 +871,8 @@ fn genFunc(self: *Self) InnerError!void {
871 // we emit an unreachable instruction to tell the stack validator that part will never be reached.871 // we emit an unreachable instruction to tell the stack validator that part will never be reached.
872 if (func_type.returns.len != 0 and self.air.instructions.len > 0) {872 if (func_type.returns.len != 0 and self.air.instructions.len > 0) {
873 const inst = @intCast(u32, self.air.instructions.len - 1);873 const inst = @intCast(u32, self.air.instructions.len - 1);
874 if (self.air.typeOfIndex(inst).isNoReturn()) {874 const last_inst_ty = self.air.typeOfIndex(inst);
875 if (!last_inst_ty.hasRuntimeBitsIgnoreComptime() or last_inst_ty.isNoReturn()) {
875 try self.addTag(.@"unreachable");876 try self.addTag(.@"unreachable");
876 }877 }
877 }878 }
src/link/Wasm.zig+1-1
...@@ -1897,7 +1897,7 @@ pub fn flushModule(self: *Wasm, comp: *Compilation, prog_node: *std.Progress.Nod...@@ -1897,7 +1897,7 @@ pub fn flushModule(self: *Wasm, comp: *Compilation, prog_node: *std.Progress.Nod
1897 if (data_section_index) |data_index| {1897 if (data_section_index) |data_index| {
1898 try self.emitDataRelocations(file, arena, data_index, symbol_table);1898 try self.emitDataRelocations(file, arena, data_index, symbol_table);
1899 }1899 }
1900 } else {1900 } else if (!self.base.options.strip) {
1901 try self.emitNameSection(file, arena);1901 try self.emitNameSection(file, arena);
1902 }1902 }
1903}1903}
src/stage1/codegen.cpp+3
...@@ -2645,6 +2645,9 @@ static LLVMValueRef ir_render_save_err_ret_addr(CodeGen *g, Stage1Air *executabl...@@ -2645,6 +2645,9 @@ static LLVMValueRef ir_render_save_err_ret_addr(CodeGen *g, Stage1Air *executabl
2645 Stage1AirInstSaveErrRetAddr *save_err_ret_addr_instruction)2645 Stage1AirInstSaveErrRetAddr *save_err_ret_addr_instruction)
2646{2646{
2647 assert(g->have_err_ret_tracing);2647 assert(g->have_err_ret_tracing);
2648 if ((target_is_wasm(g->zig_target) && g->zig_target->os != OsEmscripten) || target_is_bpf(g->zig_target)) {
2649 return nullptr;
2650 }
26482651
2649 LLVMValueRef return_err_fn = get_return_err_fn(g);2652 LLVMValueRef return_err_fn = get_return_err_fn(g);
2650 bool is_llvm_alloca;2653 bool is_llvm_alloca;
src/stage1/target.cpp+1-1
...@@ -1000,7 +1000,7 @@ ZigLLVM_EnvironmentType target_default_abi(ZigLLVM_ArchType arch, Os os) {...@@ -1000,7 +1000,7 @@ ZigLLVM_EnvironmentType target_default_abi(ZigLLVM_ArchType arch, Os os) {
1000}1000}
10011001
1002bool target_has_debug_info(const ZigTarget *target) {1002bool target_has_debug_info(const ZigTarget *target) {
1003 return !target_is_wasm(target);1003 return true;
1004}1004}
10051005
1006bool target_long_double_is_f128(const ZigTarget *target) {1006bool target_long_double_is_f128(const ZigTarget *target) {
src/target.zig+2-1
...@@ -455,7 +455,8 @@ pub fn classifyCompilerRtLibName(target: std.Target, name: []const u8) CompilerR...@@ -455,7 +455,8 @@ pub fn classifyCompilerRtLibName(target: std.Target, name: []const u8) CompilerR
455}455}
456456
457pub fn hasDebugInfo(target: std.Target) bool {457pub fn hasDebugInfo(target: std.Target) bool {
458 return !target.cpu.arch.isWasm();458 _ = target;
459 return true;
459}460}
460461
461pub fn defaultCompilerRtOptimizeMode(target: std.Target) std.builtin.Mode {462pub fn defaultCompilerRtOptimizeMode(target: std.Target) std.builtin.Mode {