From eb326e15530dd6dca4ccbe7dbfde7bf048de813e Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Thu, 5 Jul 2018 15:09:02 -0400 Subject: M:N threading * add std.atomic.QueueMpsc.isEmpty * make std.debug.global_allocator thread-safe * std.event.Loop: now you have to choose between - initSingleThreaded - initMultiThreaded * std.event.Loop multiplexes coroutines onto kernel threads * Remove std.event.Loop.stop. Instead the event loop run() function returns once there are no pending coroutines. * fix crash in ir.cpp for calling methods under some conditions * small progress self-hosted compiler, analyzing top level declarations * Introduce std.event.Lock for synchronizing coroutines * introduce std.event.Locked(T) for data that only 1 coroutine should modify at once. * make the self hosted compiler use multi threaded event loop * make std.heap.DirectAllocator thread-safe See #174 TODO: * call sched_getaffinity instead of hard coding thread pool size 4 * support for Windows and MacOS * #1194 * #1197 --- std/debug/index.zig | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'std/debug') diff --git a/std/debug/index.zig b/std/debug/index.zig index 57b2dfc300..a5e1c313f0 100644 --- a/std/debug/index.zig +++ b/std/debug/index.zig @@ -11,6 +11,11 @@ const builtin = @import("builtin"); pub const FailingAllocator = @import("failing_allocator.zig").FailingAllocator; +pub const runtime_safety = switch (builtin.mode) { + builtin.Mode.Debug, builtin.Mode.ReleaseSafe => true, + builtin.Mode.ReleaseFast, builtin.Mode.ReleaseSmall => false, +}; + /// Tries to write to stderr, unbuffered, and ignores any error returned. /// Does not append a newline. /// TODO atomic/multithread support @@ -1098,7 +1103,7 @@ fn readILeb128(in_stream: var) !i64 { /// This should only be used in temporary test programs. pub const global_allocator = &global_fixed_allocator.allocator; -var global_fixed_allocator = std.heap.FixedBufferAllocator.init(global_allocator_mem[0..]); +var global_fixed_allocator = std.heap.ThreadSafeFixedBufferAllocator.init(global_allocator_mem[0..]); var global_allocator_mem: [100 * 1024]u8 = undefined; // TODO make thread safe -- cgit v1.2.3 From ced3aae3b2371479c01b4abba42c751697185d7b Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Sat, 7 Jul 2018 20:31:50 -0400 Subject: cleaner output from zig build when there are compile errors --- std/debug/index.zig | 62 +++++++++++++++++++++++++++++--------------- std/special/build_runner.zig | 9 ++++--- 2 files changed, 47 insertions(+), 24 deletions(-) (limited to 'std/debug') diff --git a/std/debug/index.zig b/std/debug/index.zig index 57b2dfc300..0e2a3a8d39 100644 --- a/std/debug/index.zig +++ b/std/debug/index.zig @@ -156,7 +156,7 @@ pub fn writeStackTrace(stack_trace: *const builtin.StackTrace, out_stream: var, frame_index = (frame_index + 1) % stack_trace.instruction_addresses.len; }) { const return_address = stack_trace.instruction_addresses[frame_index]; - try printSourceAtAddress(debug_info, out_stream, return_address); + try printSourceAtAddress(debug_info, out_stream, return_address, tty_color); } } @@ -189,13 +189,11 @@ pub fn writeCurrentStackTrace(out_stream: var, allocator: *mem.Allocator, debug_ } }, } - try printSourceAtAddress(debug_info, out_stream, return_address); + try printSourceAtAddress(debug_info, out_stream, return_address, tty_color); } } -fn printSourceAtAddress(debug_info: *ElfStackTrace, out_stream: var, address: usize) !void { - const ptr_hex = "0x{x}"; - +fn printSourceAtAddress(debug_info: *ElfStackTrace, out_stream: var, address: usize, tty_color: bool) !void { switch (builtin.os) { builtin.Os.windows => return error.UnsupportedDebugInfo, builtin.Os.macosx => { @@ -209,36 +207,58 @@ fn printSourceAtAddress(debug_info: *ElfStackTrace, out_stream: var, address: us .address = address, }; const symbol = debug_info.symbol_table.search(address) orelse &unknown; - try out_stream.print(WHITE ++ "{}" ++ RESET ++ ": " ++ DIM ++ ptr_hex ++ " in ??? (???)" ++ RESET ++ "\n", symbol.name, address); + try out_stream.print(WHITE ++ "{}" ++ RESET ++ ": " ++ DIM ++ "0x{x}" ++ " in ??? (???)" ++ RESET ++ "\n", symbol.name, address); }, else => { const compile_unit = findCompileUnit(debug_info, address) catch { - try out_stream.print("???:?:?: " ++ DIM ++ ptr_hex ++ " in ??? (???)" ++ RESET ++ "\n ???\n\n", address); + if (tty_color) { + try out_stream.print("???:?:?: " ++ DIM ++ "0x{x} in ??? (???)" ++ RESET ++ "\n ???\n\n", address); + } else { + try out_stream.print("???:?:?: 0x{x} in ??? (???)\n ???\n\n", address); + } return; }; const compile_unit_name = try compile_unit.die.getAttrString(debug_info, DW.AT_name); if (getLineNumberInfo(debug_info, compile_unit, address - 1)) |line_info| { defer line_info.deinit(); - try out_stream.print(WHITE ++ "{}:{}:{}" ++ RESET ++ ": " ++ DIM ++ ptr_hex ++ " in ??? ({})" ++ RESET ++ "\n", line_info.file_name, line_info.line, line_info.column, address, compile_unit_name); - if (printLineFromFile(debug_info.allocator(), out_stream, line_info)) { - if (line_info.column == 0) { - try out_stream.write("\n"); - } else { - { - var col_i: usize = 1; - while (col_i < line_info.column) : (col_i += 1) { - try out_stream.writeByte(' '); + if (tty_color) { + try out_stream.print( + WHITE ++ "{}:{}:{}" ++ RESET ++ ": " ++ DIM ++ "0x{x} in ??? ({})" ++ RESET ++ "\n", + line_info.file_name, + line_info.line, + line_info.column, + address, + compile_unit_name, + ); + if (printLineFromFile(debug_info.allocator(), out_stream, line_info)) { + if (line_info.column == 0) { + try out_stream.write("\n"); + } else { + { + var col_i: usize = 1; + while (col_i < line_info.column) : (col_i += 1) { + try out_stream.writeByte(' '); + } } + try out_stream.write(GREEN ++ "^" ++ RESET ++ "\n"); } - try out_stream.write(GREEN ++ "^" ++ RESET ++ "\n"); + } else |err| switch (err) { + error.EndOfFile => {}, + else => return err, } - } else |err| switch (err) { - error.EndOfFile => {}, - else => return err, + } else { + try out_stream.print( + "{}:{}:{}: 0x{x} in ??? ({})\n", + line_info.file_name, + line_info.line, + line_info.column, + address, + compile_unit_name, + ); } } else |err| switch (err) { error.MissingDebugInfo, error.InvalidDebugInfo => { - try out_stream.print(ptr_hex ++ " in ??? ({})\n", address, compile_unit_name); + try out_stream.print("0x{x} in ??? ({})\n", address, compile_unit_name); }, else => return err, } diff --git a/std/special/build_runner.zig b/std/special/build_runner.zig index e4f04df6d0..2f073b3e98 100644 --- a/std/special/build_runner.zig +++ b/std/special/build_runner.zig @@ -122,10 +122,13 @@ pub fn main() !void { return usageAndErr(&builder, true, try stderr_stream); builder.make(targets.toSliceConst()) catch |err| { - if (err == error.InvalidStepName) { - return usageAndErr(&builder, true, try stderr_stream); + switch (err) { + error.InvalidStepName => { + return usageAndErr(&builder, true, try stderr_stream); + }, + error.UncleanExit => os.exit(1), + else => return err, } - return err; }; } -- cgit v1.2.3 From 05f1ea33d2d2f4ffa2bb6686a6a938d1b7983074 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Mon, 9 Jul 2018 12:12:37 -0400 Subject: ZIG_DEBUG_COLOR=1 overrides tty detection for runtime stack traces --- doc/docgen.zig | 29 ++++++++++++++++------------- std/debug/index.zig | 11 +++++++++-- std/os/index.zig | 14 +++++++++++--- 3 files changed, 36 insertions(+), 18 deletions(-) (limited to 'std/debug') diff --git a/doc/docgen.zig b/doc/docgen.zig index dfda54567f..e2da1fe6cc 100644 --- a/doc/docgen.zig +++ b/doc/docgen.zig @@ -689,7 +689,10 @@ fn termColor(allocator: *mem.Allocator, input: []const u8) ![]u8 { fn genHtml(allocator: *mem.Allocator, tokenizer: *Tokenizer, toc: *Toc, out: var, zig_exe: []const u8) !void { var code_progress_index: usize = 0; - const builtin_code = try escapeHtml(allocator, try getBuiltinCode(allocator, zig_exe)); + var env_map = try os.getEnvMap(allocator); + try env_map.set("ZIG_DEBUG_COLOR", "1"); + + const builtin_code = try escapeHtml(allocator, try getBuiltinCode(allocator, &env_map, zig_exe)); for (toc.nodes) |node| { switch (node) { @@ -778,12 +781,12 @@ fn genHtml(allocator: *mem.Allocator, tokenizer: *Tokenizer, toc: *Toc, out: var try build_args.append("c"); try out.print(" --library c"); } - _ = exec(allocator, build_args.toSliceConst()) catch return parseError(tokenizer, code.source_token, "example failed to compile"); + _ = exec(allocator, &env_map, build_args.toSliceConst()) catch return parseError(tokenizer, code.source_token, "example failed to compile"); const run_args = [][]const u8{tmp_bin_file_name}; const result = if (expected_outcome == ExpectedOutcome.Fail) blk: { - const result = try os.ChildProcess.exec(allocator, run_args, null, null, max_doc_file_size); + const result = try os.ChildProcess.exec(allocator, run_args, null, &env_map, max_doc_file_size); switch (result.term) { os.ChildProcess.Term.Exited => |exit_code| { if (exit_code == 0) { @@ -799,7 +802,7 @@ fn genHtml(allocator: *mem.Allocator, tokenizer: *Tokenizer, toc: *Toc, out: var } break :blk result; } else blk: { - break :blk exec(allocator, run_args) catch return parseError(tokenizer, code.source_token, "example crashed"); + break :blk exec(allocator, &env_map, run_args) catch return parseError(tokenizer, code.source_token, "example crashed"); }; const escaped_stderr = try escapeHtml(allocator, result.stderr); @@ -845,7 +848,7 @@ fn genHtml(allocator: *mem.Allocator, tokenizer: *Tokenizer, toc: *Toc, out: var "msvc", }); } - const result = exec(allocator, test_args.toSliceConst()) catch return parseError(tokenizer, code.source_token, "test failed"); + const result = exec(allocator, &env_map, test_args.toSliceConst()) catch return parseError(tokenizer, code.source_token, "test failed"); const escaped_stderr = try escapeHtml(allocator, result.stderr); const escaped_stdout = try escapeHtml(allocator, result.stdout); try out.print("\n{}{}\n", escaped_stderr, escaped_stdout); @@ -877,7 +880,7 @@ fn genHtml(allocator: *mem.Allocator, tokenizer: *Tokenizer, toc: *Toc, out: var try out.print(" --release-small"); }, } - const result = try os.ChildProcess.exec(allocator, test_args.toSliceConst(), null, null, max_doc_file_size); + const result = try os.ChildProcess.exec(allocator, test_args.toSliceConst(), null, &env_map, max_doc_file_size); switch (result.term) { os.ChildProcess.Term.Exited => |exit_code| { if (exit_code == 0) { @@ -923,7 +926,7 @@ fn genHtml(allocator: *mem.Allocator, tokenizer: *Tokenizer, toc: *Toc, out: var builtin.Mode.ReleaseSmall => try test_args.append("--release-small"), } - const result = try os.ChildProcess.exec(allocator, test_args.toSliceConst(), null, null, max_doc_file_size); + const result = try os.ChildProcess.exec(allocator, test_args.toSliceConst(), null, &env_map, max_doc_file_size); switch (result.term) { os.ChildProcess.Term.Exited => |exit_code| { if (exit_code == 0) { @@ -1000,7 +1003,7 @@ fn genHtml(allocator: *mem.Allocator, tokenizer: *Tokenizer, toc: *Toc, out: var } if (maybe_error_match) |error_match| { - const result = try os.ChildProcess.exec(allocator, build_args.toSliceConst(), null, null, max_doc_file_size); + const result = try os.ChildProcess.exec(allocator, build_args.toSliceConst(), null, &env_map, max_doc_file_size); switch (result.term) { os.ChildProcess.Term.Exited => |exit_code| { if (exit_code == 0) { @@ -1032,7 +1035,7 @@ fn genHtml(allocator: *mem.Allocator, tokenizer: *Tokenizer, toc: *Toc, out: var try out.print("\n"); } } else { - _ = exec(allocator, build_args.toSliceConst()) catch return parseError(tokenizer, code.source_token, "example failed to compile"); + _ = exec(allocator, &env_map, build_args.toSliceConst()) catch return parseError(tokenizer, code.source_token, "example failed to compile"); } if (!code.is_inline) { try out.print("\n"); @@ -1045,8 +1048,8 @@ fn genHtml(allocator: *mem.Allocator, tokenizer: *Tokenizer, toc: *Toc, out: var } } -fn exec(allocator: *mem.Allocator, args: []const []const u8) !os.ChildProcess.ExecResult { - const result = try os.ChildProcess.exec(allocator, args, null, null, max_doc_file_size); +fn exec(allocator: *mem.Allocator, env_map: *std.BufMap, args: []const []const u8) !os.ChildProcess.ExecResult { + const result = try os.ChildProcess.exec(allocator, args, null, env_map, max_doc_file_size); switch (result.term) { os.ChildProcess.Term.Exited => |exit_code| { if (exit_code != 0) { @@ -1070,8 +1073,8 @@ fn exec(allocator: *mem.Allocator, args: []const []const u8) !os.ChildProcess.Ex return result; } -fn getBuiltinCode(allocator: *mem.Allocator, zig_exe: []const u8) ![]const u8 { - const result = try exec(allocator, []const []const u8{ +fn getBuiltinCode(allocator: *mem.Allocator, env_map: *std.BufMap, zig_exe: []const u8) ![]const u8 { + const result = try exec(allocator, env_map, []const []const u8{ zig_exe, "builtin", }); diff --git a/std/debug/index.zig b/std/debug/index.zig index 0e2a3a8d39..54a9af4b9e 100644 --- a/std/debug/index.zig +++ b/std/debug/index.zig @@ -10,6 +10,7 @@ const ArrayList = std.ArrayList; const builtin = @import("builtin"); pub const FailingAllocator = @import("failing_allocator.zig").FailingAllocator; +pub const failing_allocator = FailingAllocator.init(global_allocator, 0); /// Tries to write to stderr, unbuffered, and ignores any error returned. /// Does not append a newline. @@ -44,6 +45,12 @@ pub fn getSelfDebugInfo() !*ElfStackTrace { } } +fn wantTtyColor() bool { + var bytes: [128]u8 = undefined; + const allocator = &std.heap.FixedBufferAllocator.init(bytes[0..]).allocator; + return if (std.os.getEnvVarOwned(allocator, "ZIG_DEBUG_COLOR")) |_| true else |_| stderr_file.isTty(); +} + /// Tries to print the current stack trace to stderr, unbuffered, and ignores any error returned. pub fn dumpCurrentStackTrace(start_addr: ?usize) void { const stderr = getStderrStream() catch return; @@ -51,7 +58,7 @@ pub fn dumpCurrentStackTrace(start_addr: ?usize) void { stderr.print("Unable to dump stack trace: Unable to open debug info: {}\n", @errorName(err)) catch return; return; }; - writeCurrentStackTrace(stderr, getDebugInfoAllocator(), debug_info, stderr_file.isTty(), start_addr) catch |err| { + writeCurrentStackTrace(stderr, getDebugInfoAllocator(), debug_info, wantTtyColor(), start_addr) catch |err| { stderr.print("Unable to dump stack trace: {}\n", @errorName(err)) catch return; return; }; @@ -64,7 +71,7 @@ pub fn dumpStackTrace(stack_trace: *const builtin.StackTrace) void { stderr.print("Unable to dump stack trace: Unable to open debug info: {}\n", @errorName(err)) catch return; return; }; - writeStackTrace(stack_trace, stderr, getDebugInfoAllocator(), debug_info, stderr_file.isTty()) catch |err| { + writeStackTrace(stack_trace, stderr, getDebugInfoAllocator(), debug_info, wantTtyColor()) catch |err| { stderr.print("Unable to dump stack trace: {}\n", @errorName(err)) catch return; return; }; diff --git a/std/os/index.zig b/std/os/index.zig index 52b36c351c..0f9aea914d 100644 --- a/std/os/index.zig +++ b/std/os/index.zig @@ -544,8 +544,13 @@ pub fn getEnvPosix(key: []const u8) ?[]const u8 { return null; } +pub const GetEnvVarOwnedError = error{ + OutOfMemory, + EnvironmentVariableNotFound, +}; + /// Caller must free returned memory. -pub fn getEnvVarOwned(allocator: *mem.Allocator, key: []const u8) ![]u8 { +pub fn getEnvVarOwned(allocator: *mem.Allocator, key: []const u8) GetEnvVarOwnedError![]u8 { if (is_windows) { const key_with_null = try cstr.addNullByte(allocator, key); defer allocator.free(key_with_null); @@ -554,14 +559,17 @@ pub fn getEnvVarOwned(allocator: *mem.Allocator, key: []const u8) ![]u8 { errdefer allocator.free(buf); while (true) { - const windows_buf_len = try math.cast(windows.DWORD, buf.len); + const windows_buf_len = math.cast(windows.DWORD, buf.len) catch return error.OutOfMemory; const result = windows.GetEnvironmentVariableA(key_with_null.ptr, buf.ptr, windows_buf_len); if (result == 0) { const err = windows.GetLastError(); return switch (err) { windows.ERROR.ENVVAR_NOT_FOUND => error.EnvironmentVariableNotFound, - else => unexpectedErrorWindows(err), + else => { + _ = unexpectedErrorWindows(err); + return error.EnvironmentVariableNotFound; + }, }; } -- cgit v1.2.3