builtin: print libbacktrace output to stderr, on panics/segfault crash (#14434)

master
Ned 2022-05-17 19:56:34 +08:00 committed by GitHub
parent da42f0d42b
commit db4b49a5ca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 32 additions and 10 deletions

View File

@ -55,7 +55,7 @@ fn panic_debug(line_no int, file string, mod string, fn_name string, s string) {
C.exit(1)
}
$if use_libbacktrace ? {
print_libbacktrace(1)
eprint_libbacktrace(1)
} $else {
print_backtrace_skipping_top_frames(1)
}
@ -106,7 +106,7 @@ pub fn panic(s string) {
C.exit(1)
}
$if use_libbacktrace ? {
print_libbacktrace(1)
eprint_libbacktrace(1)
} $else {
print_backtrace_skipping_top_frames(1)
}

View File

@ -130,6 +130,10 @@ pub:
[markused]
fn v_segmentation_fault_handler(signal int) {
eprintln('signal 11: segmentation fault')
$if use_libbacktrace ? {
eprint_libbacktrace(1)
} $else {
print_backtrace()
}
exit(128 + 11)
}

View File

@ -33,11 +33,11 @@ fn init_bt_state() &C.backtrace_state {
}
// for bt_error_callback
// struct BacktraceData {
// state &C.backtrace_state
// }
struct BacktraceOptions {
stdin bool = true
}
fn bt_print_callback(data voidptr, pc voidptr, filename_ptr &char, line int, fn_name_ptr &char) int {
fn bt_print_callback(data &BacktraceOptions, pc voidptr, filename_ptr &char, line int, fn_name_ptr &char) int {
filename := if isnil(filename_ptr) { '???' } else { unsafe { filename_ptr.vstring() } }
fn_name := if isnil(fn_name_ptr) {
'???'
@ -46,7 +46,12 @@ fn bt_print_callback(data voidptr, pc voidptr, filename_ptr &char, line int, fn_
}
// keep it for later
// pc_64 := u64(pc)
println('$filename:$line: by $fn_name')
bt_str := '$filename:$line: by $fn_name'
if data.stdin {
println(bt_str)
} else {
eprintln(bt_str)
}
return 0
}
@ -81,6 +86,15 @@ fn print_libbacktrace(frames_to_skip int) {
$if no_backtrace ? {
return
}
// data := &BacktraceData{bt_state}
C.backtrace_full(bt_state, frames_to_skip, bt_print_callback, bt_error_callback, 0)
data := &BacktraceOptions{}
C.backtrace_full(bt_state, frames_to_skip, bt_print_callback, bt_error_callback, data)
}
[noinline]
fn eprint_libbacktrace(frames_to_skip int) {
$if no_backtrace ? {
return
}
data := &BacktraceOptions{stdin: false}
C.backtrace_full(bt_state, frames_to_skip, bt_print_callback, bt_error_callback, data)
}

View File

@ -2,3 +2,7 @@ module builtin
fn print_libbacktrace(frames_to_skip int) {
}
[noinline]
fn eprint_libbacktrace(frames_to_skip int) {
}