authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-11-30 22:51:39+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-01-09 14:48:00-07:00
logaa7e65d5ba865132e7080b9d1c8a2fb0eb38cc28
tree6b61d1cd513c57c4914db048162bd76802a037d2
parentc2ad78922a4df1087fb720f79ff800bd85894a8d

Merge pull request #13719 from Vexu/debug

Improve debuggability of programs built by the self hosted compiler

5 files changed, 33 insertions(+), 3 deletions(-)

lib/build_runner.zig+2
......@@ -142,6 +142,8 @@ pub fn main() !void {
142142 return usageAndErr(builder, false, stderr_stream);
143143 };
144144 try debug_log_scopes.append(next_arg);
145 } else if (mem.eql(u8, arg, "--debug-compile-errors")) {
146 builder.debug_compile_errors = true;
145147 } else if (mem.eql(u8, arg, "--glibc-runtimes")) {
146148 builder.glibc_runtimes_dir = nextArg(args, &arg_idx) orelse {
147149 std.debug.print("Expected argument after --glibc-runtimes\n\n", .{});
lib/std/build.zig+5
......@@ -72,6 +72,7 @@ pub const Builder = struct {
7272 pkg_config_pkg_list: ?(PkgConfigError![]const PkgConfigPkg) = null,
7373 args: ?[][]const u8 = null,
7474 debug_log_scopes: []const []const u8 = &.{},
75 debug_compile_errors: bool = false,
7576
7677 /// Experimental. Use system Darling installation to run cross compiled macOS build artifacts.
7778 enable_darling: bool = false,
......@@ -2674,6 +2675,10 @@ pub const LibExeObjStep = struct {
26742675 try zig_args.append(log_scope);
26752676 }
26762677
2678 if (builder.debug_compile_errors) {
2679 try zig_args.append("--debug-compile-errors");
2680 }
2681
26772682 if (builder.verbose_cimport) zig_args.append("--verbose-cimport") catch unreachable;
26782683 if (builder.verbose_air) zig_args.append("--verbose-air") catch unreachable;
26792684 if (builder.verbose_llvm_ir) zig_args.append("--verbose-llvm-ir") catch unreachable;
src/Sema.zig+9-3
......@@ -2091,10 +2091,16 @@ fn failWithOwnedErrorMsg(sema: *Sema, err_msg: *Module.ErrorMsg) CompileError {
20912091 @setCold(true);
20922092
20932093 if (crash_report.is_enabled and sema.mod.comp.debug_compile_errors) {
2094 std.debug.print("compile error during Sema: {s}, src: {s}:{}\n", .{
2094 const err_path = err_msg.src_loc.file_scope.fullPath(sema.mod.gpa) catch unreachable;
2095 const err_source = err_msg.src_loc.file_scope.getSource(sema.mod.gpa) catch unreachable;
2096 const err_span = err_msg.src_loc.span(sema.mod.gpa) catch unreachable;
2097 const err_loc = std.zig.findLineColumn(err_source.bytes, err_span.main);
2098 std.debug.print("compile error during Sema:\n{s}:{d}:{d}: error: {s}\n{s}\n\n", .{
2099 err_path,
2100 err_loc.line + 1,
2101 err_loc.column + 1,
20952102 err_msg.msg,
2096 err_msg.src_loc.file_scope.sub_file_path,
2097 err_msg.src_loc.lazy,
2103 err_loc.source_line,
20982104 });
20992105 crash_report.compilerPanic("unexpected compile error occurred", null, null);
21002106 }
src/codegen/llvm.zig+12
......@@ -6090,6 +6090,12 @@ pub const FuncGen = struct {
60906090 const insert_block = self.builder.getInsertBlock();
60916091 if (isByRef(operand_ty)) {
60926092 _ = dib.insertDeclareAtEnd(operand, di_local_var, debug_loc, insert_block);
6093 } else if (self.dg.module.comp.bin_file.options.optimize_mode == .Debug) {
6094 const alignment = operand_ty.abiAlignment(self.dg.module.getTarget());
6095 const alloca = self.buildAlloca(operand.typeOf(), alignment);
6096 const store_inst = self.builder.buildStore(operand, alloca);
6097 store_inst.setAlignment(alignment);
6098 _ = dib.insertDeclareAtEnd(alloca, di_local_var, debug_loc, insert_block);
60936099 } else {
60946100 _ = dib.insertDbgValueIntrinsicAtEnd(operand, di_local_var, debug_loc, insert_block);
60956101 }
......@@ -8028,6 +8034,12 @@ pub const FuncGen = struct {
80288034 const insert_block = self.builder.getInsertBlock();
80298035 if (isByRef(inst_ty)) {
80308036 _ = dib.insertDeclareAtEnd(arg_val, di_local_var, debug_loc, insert_block);
8037 } else if (self.dg.module.comp.bin_file.options.optimize_mode == .Debug) {
8038 const alignment = inst_ty.abiAlignment(self.dg.module.getTarget());
8039 const alloca = self.buildAlloca(arg_val.typeOf(), alignment);
8040 const store_inst = self.builder.buildStore(arg_val, alloca);
8041 store_inst.setAlignment(alignment);
8042 _ = dib.insertDeclareAtEnd(alloca, di_local_var, debug_loc, insert_block);
80318043 } else {
80328044 _ = dib.insertDbgValueIntrinsicAtEnd(arg_val, di_local_var, debug_loc, insert_block);
80338045 }
src/main.zig+5
......@@ -3752,6 +3752,7 @@ pub fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !voi
37523752 var override_global_cache_dir: ?[]const u8 = try optionalStringEnvVar(arena, "ZIG_GLOBAL_CACHE_DIR");
37533753 var override_local_cache_dir: ?[]const u8 = try optionalStringEnvVar(arena, "ZIG_LOCAL_CACHE_DIR");
37543754 var child_argv = std.ArrayList([]const u8).init(arena);
3755 var debug_compile_errors = false;
37553756
37563757 const argv_index_exe = child_argv.items.len;
37573758 _ = try child_argv.addOne();
......@@ -3807,6 +3808,9 @@ pub fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !voi
38073808 try child_argv.append(arg);
38083809 } else if (mem.eql(u8, arg, "-fno-reference-trace")) {
38093810 try child_argv.append(arg);
3811 } else if (mem.eql(u8, arg, "--debug-compile-errors")) {
3812 try child_argv.append(arg);
3813 debug_compile_errors = true;
38103814 }
38113815 }
38123816 try child_argv.append(arg);
......@@ -3940,6 +3944,7 @@ pub fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !voi
39403944 .thread_pool = &thread_pool,
39413945 .use_stage1 = use_stage1,
39423946 .cache_mode = .whole,
3947 .debug_compile_errors = debug_compile_errors,
39433948 }) catch |err| {
39443949 fatal("unable to create compilation: {s}", .{@errorName(err)});
39453950 };