authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-11-30 22:51:39+02:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2022-11-30 22:51:39+02:00
log34f96c5fd068aeadfdbd1630fdea44fda2af1fdf
tree903f8785c8a87a62e1541edaabb16c1249439eab
parent44ee1c885f056eb8d7163a6b455af5c268a83a76
parentfb4a5ccdeeddf56a0849b4168fe7a9525f36b107
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

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,
......@@ -2686,6 +2687,10 @@ pub const LibExeObjStep = struct {
26862687 try zig_args.append(log_scope);
26872688 }
26882689
2690 if (builder.debug_compile_errors) {
2691 try zig_args.append("--debug-compile-errors");
2692 }
2693
26892694 if (builder.verbose_cimport) zig_args.append("--verbose-cimport") catch unreachable;
26902695 if (builder.verbose_air) zig_args.append("--verbose-air") catch unreachable;
26912696 if (builder.verbose_llvm_ir) zig_args.append("--verbose-llvm-ir") catch unreachable;
src/Sema.zig+9-3
......@@ -2189,10 +2189,16 @@ fn failWithOwnedErrorMsg(sema: *Sema, err_msg: *Module.ErrorMsg) CompileError {
21892189 @setCold(true);
21902190
21912191 if (crash_report.is_enabled and sema.mod.comp.debug_compile_errors) {
2192 std.debug.print("compile error during Sema: {s}, src: {s}:{}\n", .{
2192 const err_path = err_msg.src_loc.file_scope.fullPath(sema.mod.gpa) catch unreachable;
2193 const err_source = err_msg.src_loc.file_scope.getSource(sema.mod.gpa) catch unreachable;
2194 const err_span = err_msg.src_loc.span(sema.mod.gpa) catch unreachable;
2195 const err_loc = std.zig.findLineColumn(err_source.bytes, err_span.main);
2196 std.debug.print("compile error during Sema:\n{s}:{d}:{d}: error: {s}\n{s}\n\n", .{
2197 err_path,
2198 err_loc.line + 1,
2199 err_loc.column + 1,
21932200 err_msg.msg,
2194 err_msg.src_loc.file_scope.sub_file_path,
2195 err_msg.src_loc.lazy,
2201 err_loc.source_line,
21962202 });
21972203 crash_report.compilerPanic("unexpected compile error occurred", null, null);
21982204 }
src/codegen/llvm.zig+12
......@@ -6088,6 +6088,12 @@ pub const FuncGen = struct {
60886088 const insert_block = self.builder.getInsertBlock();
60896089 if (isByRef(operand_ty)) {
60906090 _ = dib.insertDeclareAtEnd(operand, di_local_var, debug_loc, insert_block);
6091 } else if (self.dg.module.comp.bin_file.options.optimize_mode == .Debug) {
6092 const alignment = operand_ty.abiAlignment(self.dg.module.getTarget());
6093 const alloca = self.buildAlloca(operand.typeOf(), alignment);
6094 const store_inst = self.builder.buildStore(operand, alloca);
6095 store_inst.setAlignment(alignment);
6096 _ = dib.insertDeclareAtEnd(alloca, di_local_var, debug_loc, insert_block);
60916097 } else {
60926098 _ = dib.insertDbgValueIntrinsicAtEnd(operand, di_local_var, debug_loc, insert_block);
60936099 }
......@@ -8026,6 +8032,12 @@ pub const FuncGen = struct {
80268032 const insert_block = self.builder.getInsertBlock();
80278033 if (isByRef(inst_ty)) {
80288034 _ = dib.insertDeclareAtEnd(arg_val, di_local_var, debug_loc, insert_block);
8035 } else if (self.dg.module.comp.bin_file.options.optimize_mode == .Debug) {
8036 const alignment = inst_ty.abiAlignment(self.dg.module.getTarget());
8037 const alloca = self.buildAlloca(arg_val.typeOf(), alignment);
8038 const store_inst = self.builder.buildStore(arg_val, alloca);
8039 store_inst.setAlignment(alignment);
8040 _ = dib.insertDeclareAtEnd(alloca, di_local_var, debug_loc, insert_block);
80298041 } else {
80308042 _ = dib.insertDbgValueIntrinsicAtEnd(arg_val, di_local_var, debug_loc, insert_block);
80318043 }
src/main.zig+5
......@@ -3778,6 +3778,7 @@ pub fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !voi
37783778 var override_local_cache_dir: ?[]const u8 = try optionalStringEnvVar(arena, "ZIG_LOCAL_CACHE_DIR");
37793779 var child_argv = std.ArrayList([]const u8).init(arena);
37803780 var reference_trace: ?u32 = null;
3781 var debug_compile_errors = false;
37813782
37823783 const argv_index_exe = child_argv.items.len;
37833784 _ = try child_argv.addOne();
......@@ -3839,6 +3840,9 @@ pub fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !voi
38393840 } else if (mem.eql(u8, arg, "-fno-reference-trace")) {
38403841 try child_argv.append(arg);
38413842 reference_trace = null;
3843 } else if (mem.eql(u8, arg, "--debug-compile-errors")) {
3844 try child_argv.append(arg);
3845 debug_compile_errors = true;
38423846 }
38433847 }
38443848 try child_argv.append(arg);
......@@ -3973,6 +3977,7 @@ pub fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !voi
39733977 .use_stage1 = use_stage1,
39743978 .cache_mode = .whole,
39753979 .reference_trace = reference_trace,
3980 .debug_compile_errors = debug_compile_errors,
39763981 }) catch |err| {
39773982 fatal("unable to create compilation: {s}", .{@errorName(err)});
39783983 };