From 48b117618d239953acd5019294f6f0d1b41fa6a2 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Thu, 19 Nov 2020 17:57:51 +0200 Subject: [PATCH] builtin: support -d no_backtrace, to ease compiling V code on older distros easier --- vlib/builtin/builtin_nix.c.v | 80 +++++++++++++++++++----------------- 1 file changed, 42 insertions(+), 38 deletions(-) diff --git a/vlib/builtin/builtin_nix.c.v b/vlib/builtin/builtin_nix.c.v index 6648e3e3f6..9a75f4bf18 100644 --- a/vlib/builtin/builtin_nix.c.v +++ b/vlib/builtin/builtin_nix.c.v @@ -87,48 +87,52 @@ fn print_backtrace_skipping_top_frames_linux(skipframes int) bool { C.tcc_backtrace("Backtrace") return false } - buffer := [100]byteptr{} - nr_ptrs := C.backtrace(voidptr(buffer), 100) - if nr_ptrs < 2 { - eprintln('C.backtrace returned less than 2 frames') + $if no_backtrace ? { return false - } - nr_actual_frames := nr_ptrs - skipframes - mut sframes := []string{} - //////csymbols := backtrace_symbols(*voidptr(&buffer[skipframes]), nr_actual_frames) - csymbols := C.backtrace_symbols(voidptr(&buffer[skipframes]), nr_actual_frames) - for i in 0 .. nr_actual_frames { - sframes << unsafe {tos2( byteptr(csymbols[i]) )} - } - for sframe in sframes { - executable := sframe.all_before('(') - addr := sframe.all_after('[').all_before(']') - beforeaddr := sframe.all_before('[') - cmd := 'addr2line -e $executable $addr' - // taken from os, to avoid depending on the os module inside builtin.v - f := C.popen(charptr(cmd.str), 'r') - if isnil(f) { - eprintln(sframe) - continue + } $else { + buffer := [100]byteptr{} + nr_ptrs := C.backtrace(voidptr(buffer), 100) + if nr_ptrs < 2 { + eprintln('C.backtrace returned less than 2 frames') + return false } - buf := [1000]byte{} - mut output := '' - for C.fgets(charptr(buf), 1000, f) != 0 { - output += tos(byteptr(buf), vstrlen(byteptr(buf))) + nr_actual_frames := nr_ptrs - skipframes + mut sframes := []string{} + //////csymbols := backtrace_symbols(*voidptr(&buffer[skipframes]), nr_actual_frames) + csymbols := C.backtrace_symbols(voidptr(&buffer[skipframes]), nr_actual_frames) + for i in 0 .. nr_actual_frames { + sframes << unsafe {tos2( byteptr(csymbols[i]) )} } - output = output.trim_space() + ':' - if C.pclose(f) != 0 { - eprintln(sframe) - continue + for sframe in sframes { + executable := sframe.all_before('(') + addr := sframe.all_after('[').all_before(']') + beforeaddr := sframe.all_before('[') + cmd := 'addr2line -e $executable $addr' + // taken from os, to avoid depending on the os module inside builtin.v + f := C.popen(charptr(cmd.str), 'r') + if isnil(f) { + eprintln(sframe) + continue + } + buf := [1000]byte{} + mut output := '' + for C.fgets(charptr(buf), 1000, f) != 0 { + output += tos(byteptr(buf), vstrlen(byteptr(buf))) + } + output = output.trim_space() + ':' + if C.pclose(f) != 0 { + eprintln(sframe) + continue + } + if output in ['??:0:', '??:?:'] { + output = '' + } + // See http://wiki.dwarfstd.org/index.php?title=Path_Discriminators + // NB: it is shortened here to just d. , just so that it fits, and so + // that the common error file:lineno: line format is enforced. + output = output.replace(' (discriminator', ': (d.') + eprintln('${output:-46s} | ${addr:14s} | $beforeaddr') } - if output in ['??:0:', '??:?:'] { - output = '' - } - // See http://wiki.dwarfstd.org/index.php?title=Path_Discriminators - // NB: it is shortened here to just d. , just so that it fits, and so - // that the common error file:lineno: line format is enforced. - output = output.replace(' (discriminator', ': (d.') - eprintln('${output:-46s} | ${addr:14s} | $beforeaddr') } return true }