authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-05-03 20:42:36-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-05-03 20:42:36-07:00
log2910b10033f955a99aacc2e833421ac47512c9b7
treeda8dd7fbcaee5e85edc40ca52450f30ca2bafc4d
parent2ae72c6f091716a4c1a30928dc1a49c9a1ed312a

build.zig: add -Dmem-leak-frames option

to configure how many stack frames to save with every allocation, in case it leaks and needs to get printed on process termination.

2 files changed, 11 insertions(+), 1 deletions(-)

build.zig+8
......@@ -86,6 +86,12 @@ pub fn build(b: *Builder) !void {
8686 const link_libc = b.option(bool, "force-link-libc", "Force self-hosted compiler to link libc") orelse enable_llvm;
8787 const strip = b.option(bool, "strip", "Omit debug information") orelse false;
8888
89 const mem_leak_frames: u32 = b.option(u32, "mem-leak-frames", "How many stack frames to print when a memory leak occurs. Tests get 2x this amount.") orelse blk: {
90 if (strip) break :blk @as(u32, 0);
91 if (mode != .Debug) break :blk 0;
92 break :blk 4;
93 };
94
8995 const main_file = if (is_stage1) "src/stage1.zig" else "src/main.zig";
9096
9197 var exe = b.addExecutable("zig", main_file);
......@@ -96,6 +102,7 @@ pub fn build(b: *Builder) !void {
96102 test_step.dependOn(&exe.step);
97103 b.default_step.dependOn(&exe.step);
98104
105 exe.addBuildOption(u32, "mem_leak_frames", mem_leak_frames);
99106 exe.addBuildOption(bool, "skip_non_native", skip_non_native);
100107 exe.addBuildOption(bool, "have_llvm", enable_llvm);
101108 if (enable_llvm) {
......@@ -230,6 +237,7 @@ pub fn build(b: *Builder) !void {
230237 test_stage2.addBuildOption(bool, "enable_qemu", is_qemu_enabled);
231238 test_stage2.addBuildOption(bool, "enable_wine", is_wine_enabled);
232239 test_stage2.addBuildOption(bool, "enable_wasmtime", is_wasmtime_enabled);
240 test_stage2.addBuildOption(u32, "mem_leak_frames", mem_leak_frames * 2);
233241 test_stage2.addBuildOption(?[]const u8, "glibc_multi_install_dir", glibc_multi_dir);
234242 test_stage2.addBuildOption([]const u8, "version", version);
235243
src/main.zig+3-1
......@@ -120,7 +120,9 @@ pub fn log(
120120 std.debug.print(prefix1 ++ prefix2 ++ format ++ "\n", args);
121121}
122122
123var general_purpose_allocator = std.heap.GeneralPurposeAllocator(.{}){};
123var general_purpose_allocator = std.heap.GeneralPurposeAllocator(.{
124 .stack_trace_frames = build_options.mem_leak_frames,
125}){};
124126
125127pub fn main() anyerror!void {
126128 const gpa = if (std.builtin.link_libc)