Skip to content

Commit 2d8a1da

Browse files
authored
builtin: add pid: and tid: fields to the panic reports (part 2 of #25808) (#25809)
1 parent defae4d commit 2d8a1da

File tree

4 files changed

+60
-4
lines changed

4 files changed

+60
-4
lines changed

vlib/builtin/builtin.c.v

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,21 @@ fn panic_debug(line_no int, file string, mod string, fn_name string, s string) {
6464
} $else {
6565
// vfmt off
6666
// Note: be carefull to not allocate here, avoid string interpolation
67+
flush_stdout()
6768
eprintln('================ V panic ================')
6869
eprint(' module: '); eprintln(mod)
6970
eprint(' function: '); eprint(fn_name); eprintln('()')
7071
eprint(' message: '); eprintln(s)
7172
eprint(' file: '); eprint(file); eprint(':');
72-
C.fprintf(C.stderr, c'%d\n', line_no)
73+
C.fprintf(C.stderr, c'%d\n', line_no)
7374
eprint(' v hash: '); eprintln(vcurrent_hash())
75+
$if !vinix && !native {
76+
eprint(' pid: '); C.fprintf(C.stderr, c'%p\n', voidptr(v_getpid()))
77+
eprint(' tid: '); C.fprintf(C.stderr, c'%p\n', voidptr(v_gettid()))
78+
}
7479
eprintln('=========================================')
75-
// vfmt on
7680
flush_stdout()
81+
// vfmt on
7782
$if native {
7883
C.exit(1) // TODO: native backtraces
7984
} $else $if exit_after_panic_message ? {
@@ -129,11 +134,18 @@ pub fn panic(s string) {
129134
$if freestanding {
130135
bare_panic(s)
131136
} $else {
137+
// vfmt off
138+
flush_stdout()
132139
eprint('V panic: ')
133140
eprintln(s)
134-
eprint('v hash: ')
141+
eprint(' v hash: ')
135142
eprintln(vcurrent_hash())
143+
$if !vinix && !native {
144+
eprint(' pid: '); C.fprintf(C.stderr, c'%p\n', voidptr(v_getpid()))
145+
eprint(' tid: '); C.fprintf(C.stderr, c'%p\n', voidptr(v_gettid()))
146+
}
136147
flush_stdout()
148+
// vfmt on
137149
$if native {
138150
C.exit(1) // TODO: native backtraces
139151
} $else $if exit_after_panic_message ? {
@@ -948,3 +960,34 @@ pub fn arguments() []string {
948960
}
949961
return res
950962
}
963+
964+
// v_getpid returns a process identifier. It is a number that is guaranteed to
965+
// remain the same while the current process is running. It may or may not be
966+
// equal to the value of v_gettid(). Note: it is *NOT equal on Windows*.
967+
pub fn v_getpid() u64 {
968+
$if windows {
969+
return u64(C.GetCurrentProcessId())
970+
} $else {
971+
return u64(C.getpid())
972+
}
973+
}
974+
975+
// v_gettid retuns a thread identifier. It is a number that is guaranteed to not
976+
// change, while the current thread is running. Different threads, running at
977+
// the same time in the same process, have different thread ids. There is no
978+
// such guarantee for threads running in different processes.
979+
// Important: this will be the same number returned by v_getpid(), but only on
980+
// non windows systems, when the current thread is the main one. It is best to
981+
// *avoid relying on this equivalence*, and use v_gettid and v_getpid only for
982+
// tracing and debugging multithreaded issues, but *NOT for logic decisions*.
983+
pub fn v_gettid() u64 {
984+
$if windows {
985+
return u64(C.GetCurrentThreadId())
986+
} $else $if linux && !musl ? {
987+
return u64(C.gettid())
988+
} $else $if threads {
989+
return u64(C.pthread_self())
990+
} $else {
991+
return v_getpid()
992+
}
993+
}

vlib/builtin/builtin_test.c.v

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,10 @@ fn test_bool_size() {
1818
println(@LOCATION)
1919
assert sizeof(C.BOOL) == 4
2020
}
21+
22+
fn test_v_getpid__and__v_gettid() {
23+
p := v_getpid()
24+
dump(p)
25+
t := v_gettid()
26+
dump(t)
27+
}

vlib/builtin/cfns.c.v

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,14 @@ fn C.strchr(s &char, c int) &char
105105

106106
// process execution, os.process:
107107
@[trusted]
108+
fn C.GetCurrentProcessId() u32
109+
@[trusted]
110+
fn C._getpid() int
111+
@[trusted]
108112
fn C.getpid() int
109113

110114
@[trusted]
111115
fn C.GetCurrentThreadId() u32
112-
113116
@[trusted]
114117
fn C.gettid() u32
115118

vlib/v/gen/native/comptime.v

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,9 @@ fn (mut g Gen) comptime_ident(name string, is_comptime_option bool) bool {
216216
'no_main' {
217217
g.pref.is_script
218218
}
219+
'threads' {
220+
true
221+
}
219222
else {
220223
if is_comptime_option
221224
|| (g.pref.compile_defines_all.len > 0 && name in g.pref.compile_defines_all) {

0 commit comments

Comments
 (0)