diff --git a/compiler/cc.v b/compiler/cc.v index 750535059b..c0f89a17f4 100644 --- a/compiler/cc.v +++ b/compiler/cc.v @@ -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' } diff --git a/compiler/cheaders.v b/compiler/cheaders.v index 001625519b..0d1a542d7c 100644 --- a/compiler/cheaders.v +++ b/compiler/cheaders.v @@ -21,6 +21,15 @@ CommonCHeaders = ' #include // backtrace and backtrace_symbols_fd #endif +#ifdef __linux__ +#include // backtrace and backtrace_symbols_fd +#endif + +#ifdef __linux__ +#include +#include // os__wait uses wait on nix +#endif + #define EMPTY_STRUCT_DECLARATION #define OPTION_CAST(x) (x) diff --git a/vlib/builtin/builtin.v b/vlib/builtin/builtin.v index a035f4e963..aa6dd4b2b5 100644 --- a/vlib/builtin/builtin.v +++ b/vlib/builtin/builtin.v @@ -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) }