print backtraces on panic on mac and linux

pull/1769/head
Delyan Angelov 2019-08-27 23:29:13 +03:00 committed by Alexander Medvednikov
parent 02fc7e14cd
commit 73c6bae480
3 changed files with 33 additions and 6 deletions

View File

@ -39,6 +39,11 @@ fn (v mut V) cc() {
else {
a << '-g'
}
if v.pref.is_debug {
a << ' -rdynamic ' // needed for nicer symbolic backtraces
}
if v.os != .msvc && v.os != .freebsd {
a << '-Werror=implicit-function-declaration'
}

View File

@ -21,6 +21,15 @@ CommonCHeaders = '
#include <execinfo.h> // backtrace and backtrace_symbols_fd
#endif
#ifdef __linux__
#include <execinfo.h> // backtrace and backtrace_symbols_fd
#endif
#ifdef __linux__
#include <sys/types.h>
#include <sys/wait.h> // os__wait uses wait on nix
#endif
#define EMPTY_STRUCT_DECLARATION
#define OPTION_CAST(x) (x)

View File

@ -17,15 +17,28 @@ fn on_panic(f fn (int) int) {
// TODO
}
pub fn print_backtrace() {
if true {
return // TODO
}
pub fn print_backtrace_skipping_top_frames(skipframes int) {
$if mac {
buffer := [100]voidptr
nr_ptrs := C.backtrace(buffer, 100)
C.backtrace_symbols_fd(buffer, nr_ptrs, 1)
C.backtrace_symbols_fd(&buffer[skipframes], nr_ptrs-skipframes, 1)
return
}
$if linux {
buffer := [100]voidptr
nr_ptrs := C.backtrace(buffer, 100)
C.backtrace_symbols_fd(&buffer[skipframes], nr_ptrs-skipframes, 1)
return
}
C.printf('print_backtrace_skipping_top_frames is not implemented on this platform for now...\n')
}
pub fn print_backtrace(){
// at the time of backtrace_symbols_fd call, the C stack would look something like this:
// 1 frame for print_backtrace_skipping_top_frames
// 1 frame for print_backtrace itself
// ... print the rest of the backtrace frames ...
// => top 2 frames should be skipped, since they will not be informative to the developer
print_backtrace_skipping_top_frames(2)
}
// replaces panic when -debug arg is passed
@ -37,7 +50,7 @@ fn _panic_debug(line_no int, file, mod, fn_name, s string) {
println(' line: ' + line_no.str())
println(' message: $s')
println('=========================================')
print_backtrace()
print_backtrace_skipping_top_frames(1)
C.exit(1)
}