| author | |
| committer | |
| log | c9756ca0e16a669263e003b02d21152622db42cc |
| tree | 1b1f030c38a833ca431d7968546cf6743e737d60 |
| parent | 3bce5b2f30c2f711a5e8cf481fc8f6381f7b1436 |
Annotate the panic message with the thread ID to know who's the culprit.6 files changed, 45 insertions(+), 0 deletions(-)
lib/std/c/darwin.zig+3| ... | @@ -186,6 +186,9 @@ pub const pthread_attr_t = extern struct { | ... | @@ -186,6 +186,9 @@ pub const pthread_attr_t = extern struct { |
| 186 | __opaque: [56]u8, | 186 | __opaque: [56]u8, |
| 187 | }; | 187 | }; |
| 188 | 188 | ||
| 189 | const pthread_t = std.c.pthread_t; | ||
| 190 | pub extern "c" fn pthread_threadid_np(thread: ?pthread_t, thread_id: *u64) c_int; | ||
| 191 | |||
| 189 | pub extern "c" fn arc4random_buf(buf: [*]u8, len: usize) void; | 192 | pub extern "c" fn arc4random_buf(buf: [*]u8, len: usize) void; |
| 190 | 193 | ||
| 191 | // Grand Central Dispatch is exposed by libSystem. | 194 | // Grand Central Dispatch is exposed by libSystem. |
lib/std/c/freebsd.zig+2| ... | @@ -13,6 +13,8 @@ pub extern "c" fn getdents(fd: c_int, buf_ptr: [*]u8, nbytes: usize) usize; | ... | @@ -13,6 +13,8 @@ pub extern "c" fn getdents(fd: c_int, buf_ptr: [*]u8, nbytes: usize) usize; |
| 13 | pub extern "c" fn sigaltstack(ss: ?*stack_t, old_ss: ?*stack_t) c_int; | 13 | pub extern "c" fn sigaltstack(ss: ?*stack_t, old_ss: ?*stack_t) c_int; |
| 14 | pub extern "c" fn getrandom(buf_ptr: [*]u8, buf_len: usize, flags: c_uint) isize; | 14 | pub extern "c" fn getrandom(buf_ptr: [*]u8, buf_len: usize, flags: c_uint) isize; |
| 15 | 15 | ||
| 16 | pub extern "pthread" fn pthread_getthreadid_np() c_int; | ||
| 17 | |||
| 16 | pub extern "c" fn posix_memalign(memptr: *?*c_void, alignment: usize, size: usize) c_int; | 18 | pub extern "c" fn posix_memalign(memptr: *?*c_void, alignment: usize, size: usize) c_int; |
| 17 | pub extern "c" fn malloc_usable_size(?*const c_void) usize; | 19 | pub extern "c" fn malloc_usable_size(?*const c_void) usize; |
| 18 | 20 |
lib/std/c/netbsd.zig+2| ... | @@ -14,6 +14,8 @@ pub const _errno = __errno; | ... | @@ -14,6 +14,8 @@ pub const _errno = __errno; |
| 14 | pub const dl_iterate_phdr_callback = fn (info: *dl_phdr_info, size: usize, data: ?*c_void) callconv(.C) c_int; | 14 | pub const dl_iterate_phdr_callback = fn (info: *dl_phdr_info, size: usize, data: ?*c_void) callconv(.C) c_int; |
| 15 | pub extern "c" fn dl_iterate_phdr(callback: dl_iterate_phdr_callback, data: ?*c_void) c_int; | 15 | pub extern "c" fn dl_iterate_phdr(callback: dl_iterate_phdr_callback, data: ?*c_void) c_int; |
| 16 | 16 | ||
| 17 | pub extern "c" fn _lwp_self() lwpid_t; | ||
| 18 | |||
| 17 | pub extern "c" fn arc4random_buf(buf: [*]u8, len: usize) void; | 19 | pub extern "c" fn arc4random_buf(buf: [*]u8, len: usize) void; |
| 18 | pub extern "c" fn __fstat50(fd: fd_t, buf: *Stat) c_int; | 20 | pub extern "c" fn __fstat50(fd: fd_t, buf: *Stat) c_int; |
| 19 | pub extern "c" fn __stat50(path: [*:0]const u8, buf: *Stat) c_int; | 21 | pub extern "c" fn __stat50(path: [*:0]const u8, buf: *Stat) c_int; |
lib/std/c/openbsd.zig+2| ... | @@ -16,6 +16,8 @@ pub extern "c" fn dl_iterate_phdr(callback: dl_iterate_phdr_callback, data: ?*c_ | ... | @@ -16,6 +16,8 @@ pub extern "c" fn dl_iterate_phdr(callback: dl_iterate_phdr_callback, data: ?*c_ |
| 16 | 16 | ||
| 17 | pub extern "c" fn arc4random_buf(buf: [*]u8, len: usize) void; | 17 | pub extern "c" fn arc4random_buf(buf: [*]u8, len: usize) void; |
| 18 | 18 | ||
| 19 | pub extern "c" fn getthrid() pid_t; | ||
| 20 | |||
| 19 | pub extern "c" fn getdents(fd: c_int, buf_ptr: [*]u8, nbytes: usize) usize; | 21 | pub extern "c" fn getdents(fd: c_int, buf_ptr: [*]u8, nbytes: usize) usize; |
| 20 | pub extern "c" fn sigaltstack(ss: ?*stack_t, old_ss: ?*stack_t) c_int; | 22 | pub extern "c" fn sigaltstack(ss: ?*stack_t, old_ss: ?*stack_t) c_int; |
| 21 | 23 |
lib/std/debug.zig+6| ... | @@ -262,6 +262,12 @@ pub fn panicExtra(trace: ?*const builtin.StackTrace, first_trace_addr: ?usize, c | ... | @@ -262,6 +262,12 @@ pub fn panicExtra(trace: ?*const builtin.StackTrace, first_trace_addr: ?usize, c |
| 262 | defer held.release(); | 262 | defer held.release(); |
| 263 | 263 | ||
| 264 | const stderr = io.getStdErr().writer(); | 264 | const stderr = io.getStdErr().writer(); |
| 265 | if (builtin.single_threaded) { | ||
| 266 | stderr.print("main thread panicked: ", .{}) catch os.abort(); | ||
| 267 | } else { | ||
| 268 | const current_thread_id = std.Thread.getCurrentThreadId(); | ||
| 269 | stderr.print("thread {d} panicked: ", .{current_thread_id}) catch os.abort(); | ||
| 270 | } | ||
| 265 | stderr.print(format ++ "\n", args) catch os.abort(); | 271 | stderr.print(format ++ "\n", args) catch os.abort(); |
| 266 | if (trace) |t| { | 272 | if (trace) |t| { |
| 267 | dumpStackTrace(t.*); | 273 | dumpStackTrace(t.*); |
lib/std/thread.zig+30| ... | @@ -493,4 +493,34 @@ pub const Thread = struct { | ... | @@ -493,4 +493,34 @@ pub const Thread = struct { |
| 493 | }; | 493 | }; |
| 494 | return @intCast(usize, count); | 494 | return @intCast(usize, count); |
| 495 | } | 495 | } |
| 496 | |||
| 497 | pub fn getCurrentThreadId() u64 { | ||
| 498 | switch (std.Target.current.os.tag) { | ||
| 499 | .linux => { | ||
| 500 | // Use the syscall directly as musl doesn't provide a wrapper. | ||
| 501 | return @bitCast(u32, os.linux.gettid()); | ||
| 502 | }, | ||
| 503 | .windows => { | ||
| 504 | return os.windows.kernel32.GetCurrentThreadId(); | ||
| 505 | }, | ||
| 506 | .macos, .ios, .watchos, .tvos => { | ||
| 507 | var thread_id: u64 = undefined; | ||
| 508 | // Pass thread=null to get the current thread ID. | ||
| 509 | assert(c.pthread_threadid_np(null, &thread_id) == 0); | ||
| 510 | return thread_id; | ||
| 511 | }, | ||
| 512 | .netbsd => { | ||
| 513 | return @bitCast(u32, c._lwp_self()); | ||
| 514 | }, | ||
| 515 | .freebsd => { | ||
| 516 | return @bitCast(u32, c.pthread_getthreadid_np()); | ||
| 517 | }, | ||
| 518 | .openbsd => { | ||
| 519 | return @bitCast(u32, c.getthrid()); | ||
| 520 | }, | ||
| 521 | else => { | ||
| 522 | @compileError("getCurrentThreadId not implemented for this platform"); | ||
| 523 | }, | ||
| 524 | } | ||
| 525 | } | ||
| 496 | }; | 526 | }; |