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 {...@@ -86,6 +86,12 @@ pub fn build(b: *Builder) !void {
86 const link_libc = b.option(bool, "force-link-libc", "Force self-hosted compiler to link libc") orelse enable_llvm;86 const link_libc = b.option(bool, "force-link-libc", "Force self-hosted compiler to link libc") orelse enable_llvm;
87 const strip = b.option(bool, "strip", "Omit debug information") orelse false;87 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
89 const main_file = if (is_stage1) "src/stage1.zig" else "src/main.zig";95 const main_file = if (is_stage1) "src/stage1.zig" else "src/main.zig";
9096
91 var exe = b.addExecutable("zig", main_file);97 var exe = b.addExecutable("zig", main_file);
...@@ -96,6 +102,7 @@ pub fn build(b: *Builder) !void {...@@ -96,6 +102,7 @@ pub fn build(b: *Builder) !void {
96 test_step.dependOn(&exe.step);102 test_step.dependOn(&exe.step);
97 b.default_step.dependOn(&exe.step);103 b.default_step.dependOn(&exe.step);
98104
105 exe.addBuildOption(u32, "mem_leak_frames", mem_leak_frames);
99 exe.addBuildOption(bool, "skip_non_native", skip_non_native);106 exe.addBuildOption(bool, "skip_non_native", skip_non_native);
100 exe.addBuildOption(bool, "have_llvm", enable_llvm);107 exe.addBuildOption(bool, "have_llvm", enable_llvm);
101 if (enable_llvm) {108 if (enable_llvm) {
...@@ -230,6 +237,7 @@ pub fn build(b: *Builder) !void {...@@ -230,6 +237,7 @@ pub fn build(b: *Builder) !void {
230 test_stage2.addBuildOption(bool, "enable_qemu", is_qemu_enabled);237 test_stage2.addBuildOption(bool, "enable_qemu", is_qemu_enabled);
231 test_stage2.addBuildOption(bool, "enable_wine", is_wine_enabled);238 test_stage2.addBuildOption(bool, "enable_wine", is_wine_enabled);
232 test_stage2.addBuildOption(bool, "enable_wasmtime", is_wasmtime_enabled);239 test_stage2.addBuildOption(bool, "enable_wasmtime", is_wasmtime_enabled);
240 test_stage2.addBuildOption(u32, "mem_leak_frames", mem_leak_frames * 2);
233 test_stage2.addBuildOption(?[]const u8, "glibc_multi_install_dir", glibc_multi_dir);241 test_stage2.addBuildOption(?[]const u8, "glibc_multi_install_dir", glibc_multi_dir);
234 test_stage2.addBuildOption([]const u8, "version", version);242 test_stage2.addBuildOption([]const u8, "version", version);
235243
src/main.zig+3-1
...@@ -120,7 +120,9 @@ pub fn log(...@@ -120,7 +120,9 @@ pub fn log(
120 std.debug.print(prefix1 ++ prefix2 ++ format ++ "\n", args);120 std.debug.print(prefix1 ++ prefix2 ++ format ++ "\n", args);
121}121}
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
125pub fn main() anyerror!void {127pub fn main() anyerror!void {
126 const gpa = if (std.builtin.link_libc)128 const gpa = if (std.builtin.link_libc)