| author | |
| committer | |
| log | 4cb84f8e486426e66e17cc91dd880d9bdcabc680 |
| tree | cf3e953e019f14491fcb2f2002fb5c025b240020 |
| parent | 51d08f4b9b051d66534b77462a3bfb9bace9f1fb |
| signature |
4 files changed, 96 insertions(+), 84 deletions(-)
test/standalone/coff_dwarf/build.zig+7-6| ... | ... | @@ -1,9 +1,10 @@ |
| 1 | 1 | const std = @import("std"); |
| 2 | const builtin = @import("builtin"); | |
| 3 | 2 | |
| 4 | 3 | /// This tests the path where DWARF information is embedded in a COFF binary |
| 5 | 4 | pub fn build(b: *std.Build) void { |
| 6 | switch (builtin.cpu.arch) { | |
| 5 | const host = b.graph.host; | |
| 6 | ||
| 7 | switch (host.result.cpu.arch) { | |
| 7 | 8 | .aarch64, |
| 8 | 9 | .x86, |
| 9 | 10 | .x86_64, |
| ... | ... | @@ -15,10 +16,10 @@ pub fn build(b: *std.Build) void { |
| 15 | 16 | b.default_step = test_step; |
| 16 | 17 | |
| 17 | 18 | const optimize: std.builtin.OptimizeMode = .Debug; |
| 18 | const target = if (builtin.os.tag == .windows) | |
| 19 | b.standardTargetOptions(.{}) | |
| 20 | else | |
| 21 | b.resolveTargetQuery(.{ .os_tag = .windows }); | |
| 19 | const target = switch (host.result.os.tag) { | |
| 20 | .windows => host, | |
| 21 | else => b.resolveTargetQuery(.{ .os_tag = .windows }), | |
| 22 | }; | |
| 22 | 23 | |
| 23 | 24 | const exe = b.addExecutable(.{ |
| 24 | 25 | .name = "main", |
test/standalone/coff_dwarf/main.zig+23-16| ... | ... | @@ -1,27 +1,34 @@ |
| 1 | 1 | const std = @import("std"); |
| 2 | const assert = std.debug.assert; | |
| 3 | const testing = std.testing; | |
| 2 | const fatal = std.process.fatal; | |
| 4 | 3 | |
| 5 | 4 | extern fn add(a: u32, b: u32, addr: *usize) u32; |
| 6 | 5 | |
| 7 | pub fn main() !void { | |
| 8 | var gpa: std.heap.GeneralPurposeAllocator(.{}) = .init; | |
| 9 | defer assert(gpa.deinit() == .ok); | |
| 10 | const allocator = gpa.allocator(); | |
| 6 | pub fn main() void { | |
| 7 | var debug_alloc_inst: std.heap.DebugAllocator(.{}) = .init; | |
| 8 | defer std.debug.assert(debug_alloc_inst.deinit() == .ok); | |
| 9 | const gpa = debug_alloc_inst.allocator(); | |
| 11 | 10 | |
| 12 | var debug_info = try std.debug.SelfInfo.open(allocator); | |
| 13 | defer debug_info.deinit(); | |
| 11 | var di: std.debug.SelfInfo = .init; | |
| 12 | defer di.deinit(gpa); | |
| 14 | 13 | |
| 15 | 14 | var add_addr: usize = undefined; |
| 16 | 15 | _ = add(1, 2, &add_addr); |
| 17 | 16 | |
| 18 | const module = try debug_info.getModuleForAddress(add_addr); | |
| 19 | const symbol = try module.getSymbolAtAddress(allocator, add_addr); | |
| 20 | defer if (symbol.source_location) |sl| allocator.free(sl.file_name); | |
| 17 | const symbol = di.getSymbolAtAddress(gpa, add_addr) catch |err| fatal("failed to get symbol: {t}", .{err}); | |
| 18 | defer if (symbol.source_location) |sl| gpa.free(sl.file_name); | |
| 21 | 19 | |
| 22 | try testing.expectEqualStrings("add", symbol.name); | |
| 23 | try testing.expect(symbol.source_location != null); | |
| 24 | try testing.expectEqualStrings("shared_lib.c", std.fs.path.basename(symbol.source_location.?.file_name)); | |
| 25 | try testing.expectEqual(@as(u64, 3), symbol.source_location.?.line); | |
| 26 | try testing.expectEqual(@as(u64, 0), symbol.source_location.?.column); | |
| 20 | if (symbol.name == null) fatal("failed to resolve symbol name", .{}); | |
| 21 | if (symbol.compile_unit_name == null) fatal("failed to resolve compile unit", .{}); | |
| 22 | if (symbol.source_location == null) fatal("failed to resolve source location", .{}); | |
| 23 | ||
| 24 | if (!std.mem.eql(u8, symbol.name.?, "add")) { | |
| 25 | fatal("incorrect symbol name '{s}'", .{symbol.name.?}); | |
| 26 | } | |
| 27 | const sl = &symbol.source_location.?; | |
| 28 | if (!std.mem.eql(u8, std.fs.path.basename(sl.file_name), "shared_lib.c")) { | |
| 29 | fatal("incorrect file name '{s}'", .{sl.file_name}); | |
| 30 | } | |
| 31 | if (sl.line != 3 or sl.column != 0) { | |
| 32 | fatal("incorrect line/column :{d}:{d}", .{ sl.line, sl.column }); | |
| 33 | } | |
| 27 | 34 | } |
test/standalone/stack_iterator/unwind.zig+31-29| ... | ... | @@ -1,27 +1,19 @@ |
| 1 | 1 | const std = @import("std"); |
| 2 | 2 | const builtin = @import("builtin"); |
| 3 | const debug = std.debug; | |
| 4 | const testing = std.testing; | |
| 3 | const fatal = std.process.fatal; | |
| 5 | 4 | |
| 6 | noinline fn frame3(expected: *[4]usize, unwound: *[4]usize) void { | |
| 5 | noinline fn frame3(expected: *[4]usize, addr_buf: *[4]usize) std.builtin.StackTrace { | |
| 7 | 6 | expected[0] = @returnAddress(); |
| 8 | ||
| 9 | var context: debug.ThreadContext = undefined; | |
| 10 | testing.expect(debug.getContext(&context)) catch @panic("failed to getContext"); | |
| 11 | ||
| 12 | const debug_info = debug.getSelfDebugInfo() catch @panic("failed to openSelfDebugInfo"); | |
| 13 | var it = debug.StackIterator.initWithContext(expected[0], debug_info, &context, @frameAddress()) catch @panic("failed to initWithContext"); | |
| 14 | defer it.deinit(); | |
| 15 | ||
| 16 | for (unwound) |*addr| { | |
| 17 | if (it.next()) |return_address| addr.* = return_address; | |
| 18 | } | |
| 7 | return std.debug.captureCurrentStackTrace(.{ | |
| 8 | .first_address = @returnAddress(), | |
| 9 | .allow_unsafe_unwind = true, | |
| 10 | }, addr_buf); | |
| 19 | 11 | } |
| 20 | 12 | |
| 21 | noinline fn frame2(expected: *[4]usize, unwound: *[4]usize) void { | |
| 13 | noinline fn frame2(expected: *[4]usize, addr_buf: *[4]usize) std.builtin.StackTrace { | |
| 22 | 14 | // Exercise different __unwind_info / DWARF CFI encodings by forcing some registers to be restored |
| 23 | 15 | if (builtin.target.ofmt != .c) { |
| 24 | switch (builtin.cpu.arch) { | |
| 16 | switch (builtin.target.cpu.arch) { | |
| 25 | 17 | .x86 => { |
| 26 | 18 | if (builtin.omit_frame_pointer) { |
| 27 | 19 | asm volatile ( |
| ... | ... | @@ -67,10 +59,10 @@ noinline fn frame2(expected: *[4]usize, unwound: *[4]usize) void { |
| 67 | 59 | } |
| 68 | 60 | |
| 69 | 61 | expected[1] = @returnAddress(); |
| 70 | frame3(expected, unwound); | |
| 62 | return frame3(expected, addr_buf); | |
| 71 | 63 | } |
| 72 | 64 | |
| 73 | noinline fn frame1(expected: *[4]usize, unwound: *[4]usize) void { | |
| 65 | noinline fn frame1(expected: *[4]usize, addr_buf: *[4]usize) std.builtin.StackTrace { | |
| 74 | 66 | expected[2] = @returnAddress(); |
| 75 | 67 | |
| 76 | 68 | // Use a stack frame that is too big to encode in __unwind_info's stack-immediate encoding |
| ... | ... | @@ -78,22 +70,32 @@ noinline fn frame1(expected: *[4]usize, unwound: *[4]usize) void { |
| 78 | 70 | var pad: [std.math.maxInt(u8) * @sizeOf(usize) + 1]u8 = undefined; |
| 79 | 71 | _ = std.mem.doNotOptimizeAway(&pad); |
| 80 | 72 | |
| 81 | frame2(expected, unwound); | |
| 73 | return frame2(expected, addr_buf); | |
| 82 | 74 | } |
| 83 | 75 | |
| 84 | noinline fn frame0(expected: *[4]usize, unwound: *[4]usize) void { | |
| 76 | noinline fn frame0(expected: *[4]usize, addr_buf: *[4]usize) std.builtin.StackTrace { | |
| 85 | 77 | expected[3] = @returnAddress(); |
| 86 | frame1(expected, unwound); | |
| 78 | return frame1(expected, addr_buf); | |
| 87 | 79 | } |
| 88 | 80 | |
| 89 | pub fn main() !void { | |
| 90 | // Disabled until the DWARF unwinder bugs on .aarch64 are solved | |
| 91 | if (builtin.omit_frame_pointer and comptime builtin.target.os.tag.isDarwin() and builtin.cpu.arch == .aarch64) return; | |
| 92 | ||
| 93 | if (!std.debug.have_ucontext or !std.debug.have_getcontext) return; | |
| 81 | pub fn main() void { | |
| 82 | if (std.posix.ucontext_t == void and builtin.omit_frame_pointer) { | |
| 83 | // Stack unwinding is impossible. | |
| 84 | return; | |
| 85 | } | |
| 94 | 86 | |
| 95 | 87 | var expected: [4]usize = undefined; |
| 96 | var unwound: [4]usize = undefined; | |
| 97 | frame0(&expected, &unwound); | |
| 98 | try testing.expectEqual(expected, unwound); | |
| 88 | var addr_buf: [4]usize = undefined; | |
| 89 | const trace = frame0(&expected, &addr_buf); | |
| 90 | // There may be *more* than 4 frames (due to the caller of `main`); that's okay. | |
| 91 | if (trace.index < 4) { | |
| 92 | fatal("expected at least 4 frames, got '{d}':\n{f}", .{ trace.index, &trace }); | |
| 93 | } | |
| 94 | if (!std.mem.eql(usize, trace.instruction_addresses, &expected)) { | |
| 95 | const expected_trace: std.builtin.StackTrace = .{ | |
| 96 | .index = 4, | |
| 97 | .instruction_addresses = &expected, | |
| 98 | }; | |
| 99 | fatal("expected trace:\n{f}\nactual trace:\n{f}", .{ &expected_trace, &trace }); | |
| 100 | } | |
| 99 | 101 | } |
test/standalone/stack_iterator/unwind_freestanding.zig+35-33| ... | ... | @@ -1,26 +1,21 @@ |
| 1 | /// Test StackIterator on 'freestanding' target. Based on unwind.zig. | |
| 1 | //! Test StackIterator on 'freestanding' target. Based on unwind.zig. | |
| 2 | ||
| 2 | 3 | const std = @import("std"); |
| 3 | const builtin = @import("builtin"); | |
| 4 | const debug = std.debug; | |
| 5 | 4 | |
| 6 | noinline fn frame3(expected: *[4]usize, unwound: *[4]usize) void { | |
| 5 | noinline fn frame3(expected: *[4]usize, addr_buf: *[4]usize) std.builtin.StackTrace { | |
| 7 | 6 | expected[0] = @returnAddress(); |
| 8 | ||
| 9 | var it = debug.StackIterator.init(@returnAddress(), @frameAddress()); | |
| 10 | defer it.deinit(); | |
| 11 | ||
| 12 | // Save StackIterator's frame addresses into `unwound`: | |
| 13 | for (unwound) |*addr| { | |
| 14 | if (it.next()) |return_address| addr.* = return_address; | |
| 15 | } | |
| 7 | return std.debug.captureCurrentStackTrace(.{ | |
| 8 | .first_address = @returnAddress(), | |
| 9 | .allow_unsafe_unwind = true, | |
| 10 | }, addr_buf); | |
| 16 | 11 | } |
| 17 | 12 | |
| 18 | noinline fn frame2(expected: *[4]usize, unwound: *[4]usize) void { | |
| 13 | noinline fn frame2(expected: *[4]usize, addr_buf: *[4]usize) std.builtin.StackTrace { | |
| 19 | 14 | expected[1] = @returnAddress(); |
| 20 | frame3(expected, unwound); | |
| 15 | return frame3(expected, addr_buf); | |
| 21 | 16 | } |
| 22 | 17 | |
| 23 | noinline fn frame1(expected: *[4]usize, unwound: *[4]usize) void { | |
| 18 | noinline fn frame1(expected: *[4]usize, addr_buf: *[4]usize) std.builtin.StackTrace { | |
| 24 | 19 | expected[2] = @returnAddress(); |
| 25 | 20 | |
| 26 | 21 | // Use a stack frame that is too big to encode in __unwind_info's stack-immediate encoding |
| ... | ... | @@ -28,37 +23,44 @@ noinline fn frame1(expected: *[4]usize, unwound: *[4]usize) void { |
| 28 | 23 | var pad: [std.math.maxInt(u8) * @sizeOf(usize) + 1]u8 = undefined; |
| 29 | 24 | _ = std.mem.doNotOptimizeAway(&pad); |
| 30 | 25 | |
| 31 | frame2(expected, unwound); | |
| 26 | return frame2(expected, addr_buf); | |
| 32 | 27 | } |
| 33 | 28 | |
| 34 | noinline fn frame0(expected: *[4]usize, unwound: *[4]usize) void { | |
| 29 | noinline fn frame0(expected: *[4]usize, addr_buf: *[4]usize) std.builtin.StackTrace { | |
| 35 | 30 | expected[3] = @returnAddress(); |
| 36 | frame1(expected, unwound); | |
| 31 | return frame1(expected, addr_buf); | |
| 37 | 32 | } |
| 38 | 33 | |
| 39 | // No-OS entrypoint | |
| 34 | /// No-OS entrypoint | |
| 40 | 35 | export fn _start() callconv(.withStackAlign(.c, 1)) noreturn { |
| 41 | 36 | var expected: [4]usize = undefined; |
| 42 | var unwound: [4]usize = undefined; | |
| 43 | frame0(&expected, &unwound); | |
| 37 | var addr_buf: [4]usize = undefined; | |
| 38 | const trace = frame0(&expected, &addr_buf); | |
| 44 | 39 | |
| 45 | // Verify result (no std.testing in freestanding) | |
| 46 | var missed: c_int = 0; | |
| 47 | for (expected, unwound) |expectFA, actualFA| { | |
| 48 | if (expectFA != actualFA) { | |
| 49 | missed += 1; | |
| 50 | } | |
| 51 | } | |
| 40 | // Since we can't print from this freestanding test, we'll just use the exit | |
| 41 | // code to communicate error conditions. | |
| 42 | ||
| 43 | // Unlike `unwind.zig`, we can expect *exactly* 4 frames, as we are the | |
| 44 | // actual entry point and the frame pointer will be 0 on entry. | |
| 45 | if (trace.index != 4) exit(1); | |
| 46 | if (trace.instruction_addresses[0] != expected[0]) exit(2); | |
| 47 | if (trace.instruction_addresses[1] != expected[1]) exit(3); | |
| 48 | if (trace.instruction_addresses[2] != expected[2]) exit(4); | |
| 49 | if (trace.instruction_addresses[3] != expected[3]) exit(5); | |
| 50 | ||
| 51 | exit(0); | |
| 52 | } | |
| 52 | 53 | |
| 53 | // Need to compile with the target OS as "freestanding" or "other" to | |
| 54 | // exercise the StackIterator code, but when run as a regression test | |
| 55 | // need to actually exit. So assume we're running on x86_64-linux ... | |
| 54 | fn exit(code: u8) noreturn { | |
| 55 | // We are intentionally compiling with the target OS being "freestanding" to | |
| 56 | // exercise std.debug, but we still need to exit the process somehow; so do | |
| 57 | // the appropriate x86_64-linux syscall. | |
| 56 | 58 | asm volatile ( |
| 57 | 59 | \\movl $60, %%eax |
| 58 | 60 | \\syscall |
| 59 | 61 | : |
| 60 | : [missed] "{edi}" (missed), | |
| 62 | : [code] "{edi}" (code), | |
| 61 | 63 | : .{ .edi = true, .eax = true }); |
| 62 | 64 | |
| 63 | while (true) {} // unreached | |
| 65 | unreachable; | |
| 64 | 66 | } |