authorgravatar for kcbanner@gmail.comCasey Banner <kcbanner@gmail.com> 2023-07-16 16:44:00-04:00
committergravatar for kcbanner@gmail.comCasey Banner <kcbanner@gmail.com> 2023-07-20 22:58:16-04:00
logbdb0a6fa77cc7eff7d03b022210328b24f9b6662
tree19faadcb6ea960f34a31f51f197244aaf4e640e9
parent2c76020e772787c3bc8bf4bcf56e2e0f41016b26

test: add a test for dwarf embedded in coff


4 files changed, 72 insertions(+), 0 deletions(-)

test/standalone.zig+4
......@@ -234,6 +234,10 @@ pub const build_cases = [_]BuildCase{
234234 .build_root = "test/standalone/stack_iterator",
235235 .import = @import("standalone/stack_iterator/build.zig"),
236236 },
237 .{
238 .build_root = "test/standalone/coff_dwarf",
239 .import = @import("standalone/coff_dwarf/build.zig"),
240 },
237241};
238242
239243const std = @import("std");
test/standalone/coff_dwarf/build.zig created+35
......@@ -0,0 +1,35 @@
1const std = @import("std");
2const builtin = @import("builtin");
3
4/// This tests the path where DWARF information is embedded in a COFF binary
5pub fn build(b: *std.Build) void {
6 const test_step = b.step("test", "Test it");
7 b.default_step = test_step;
8
9 const optimize: std.builtin.OptimizeMode = .Debug;
10 const target = b.standardTargetOptions(.{});
11
12 if (builtin.os.tag != .windows) return;
13
14 const exe = b.addExecutable(.{
15 .name = "main",
16 .root_source_file = .{ .path = "main.zig" },
17 .optimize = optimize,
18 .target = target,
19 });
20
21 const lib = b.addSharedLibrary(.{
22 .name = "shared_lib",
23 .optimize = optimize,
24 .target = target,
25 });
26 lib.addCSourceFile("shared_lib.c", &.{"-gdwarf"});
27 lib.linkLibC();
28 exe.linkLibrary(lib);
29
30 const run = b.addRunArtifact(exe);
31 run.expectExitCode(0);
32 run.skip_foreign_checks = true;
33
34 test_step.dependOn(&run.step);
35}
test/standalone/coff_dwarf/main.zig created+27
......@@ -0,0 +1,27 @@
1const std = @import("std");
2const assert = std.debug.assert;
3const testing = std.testing;
4
5extern fn add(a: u32, b: u32, addr: *usize) u32;
6
7pub fn main() !void {
8 var gpa = std.heap.GeneralPurposeAllocator(.{}){};
9 defer assert(gpa.deinit() == .ok);
10 const allocator = gpa.allocator();
11
12 var debug_info = try std.debug.openSelfDebugInfo(allocator);
13 defer debug_info.deinit();
14
15 var add_addr: usize = undefined;
16 _ = add(1, 2, &add_addr);
17
18 const module = try debug_info.getModuleForAddress(add_addr);
19 const symbol = try module.getSymbolAtAddress(allocator, add_addr);
20 defer symbol.deinit(allocator);
21
22 try testing.expectEqualStrings("add", symbol.symbol_name);
23 try testing.expect(symbol.line_info != null);
24 try testing.expectEqualStrings("shared_lib.c", std.fs.path.basename(symbol.line_info.?.file_name));
25 try testing.expectEqual(@as(u64, 3), symbol.line_info.?.line);
26 try testing.expectEqual(@as(u64, 0), symbol.line_info.?.column);
27}
test/standalone/coff_dwarf/shared_lib.c created+6
......@@ -0,0 +1,6 @@
1#include <stdint.h>
2
3__declspec(dllexport) uint32_t add(uint32_t a, uint32_t b, uintptr_t* addr) {
4 *addr = (uintptr_t)&add;
5 return a + b;
6}