builtin: cleanup by turning backtrace* fns to ordinary C. fns

pull/4906/head
Delyan Angelov 2020-05-15 15:23:58 +03:00
parent e18268e2f4
commit 66cb61c242
3 changed files with 59 additions and 65 deletions

View File

@ -64,8 +64,8 @@ fn print_backtrace_skipping_top_frames(xskipframes int) bool {
fn print_backtrace_skipping_top_frames_mac(skipframes int) bool {
$if macos {
buffer := [100]byteptr
nr_ptrs := backtrace(buffer, 100)
backtrace_symbols_fd(&buffer[skipframes], nr_ptrs - skipframes, 2)
nr_ptrs := C.backtrace(buffer, 100)
C.backtrace_symbols_fd(&buffer[skipframes], nr_ptrs - skipframes, 2)
}
return true
}
@ -73,28 +73,35 @@ fn print_backtrace_skipping_top_frames_mac(skipframes int) bool {
fn print_backtrace_skipping_top_frames_freebsd(skipframes int) bool {
$if freebsd {
buffer := [100]byteptr
nr_ptrs := backtrace(buffer, 100)
backtrace_symbols_fd(&buffer[skipframes], nr_ptrs - skipframes, 2)
nr_ptrs := C.backtrace(buffer, 100)
C.backtrace_symbols_fd(&buffer[skipframes], nr_ptrs - skipframes, 2)
}
return true
}
fn print_backtrace_skipping_top_frames_linux(skipframes int) bool {
$if tinyc {
println('TODO: print_backtrace_skipping_top_frames_linux $skipframes with tcc fails tests with "stack smashing detected" .')
$if android {
eprintln('On Android no backtrace is available.')
return false
}
$if !glibc {
eprintln('backtrace_symbols is missing => printing backtraces is not available.')
eprintln('Some libc implementations like musl simply do not provide it.')
return false
}
$if tinyc {
eprintln('TODO: print_backtrace_skipping_top_frames_linux $skipframes')
eprintln('with tcc fails tests with "stack smashing detected" .')
return false
}
$if !android {
// backtrace is not available on Android.
$if glibc {
buffer := [100]byteptr
nr_ptrs := backtrace(buffer, 100)
nr_ptrs := C.backtrace(buffer, 100)
nr_actual_frames := nr_ptrs - skipframes
mut sframes := []string{}
//////csymbols := backtrace_symbols(*voidptr(&buffer[skipframes]), nr_actual_frames)
csymbols := backtrace_symbols(&buffer[skipframes], nr_actual_frames)
csymbols := C.backtrace_symbols(&buffer[skipframes], nr_actual_frames)
for i in 0 .. nr_actual_frames {
sframes << tos2( byteptr( voidptr(csymbols[i]) ) )
sframes << tos2( byteptr(csymbols[i]) )
}
for sframe in sframes {
executable := sframe.all_before('(')
@ -126,12 +133,5 @@ fn print_backtrace_skipping_top_frames_linux(skipframes int) bool {
output = output.replace(' (discriminator', ': (d.')
eprintln('${output:-46s} | ${addr:14s} | $beforeaddr')
}
// backtrace_symbols_fd(*voidptr(&buffer[skipframes]), nr_actual_frames, 1)
return true
} $else {
eprintln('backtrace_symbols_fd is missing, so printing backtraces is not available.\n')
eprintln('Some libc implementations like musl simply do not provide it.')
}
}
return false
}

View File

@ -26,10 +26,10 @@ fn C.isdigit(s byteptr) bool
fn C.popen(c byteptr, t byteptr) voidptr
// <execinfo.h>
// backtrace functions are not #included, that's why they have to be defined without C.
fn backtrace(a &voidptr, b int) int
fn backtrace_symbols(a &voidptr, size int) &charptr
fn backtrace_symbols_fd(a &voidptr, size int, fd int) void
fn C.backtrace(a &voidptr, size int) int
fn C.backtrace_symbols(a &voidptr, size int) &charptr
fn C.backtrace_symbols_fd(a &voidptr, size int, fd int)
// <libproc.h>
fn proc_pidpath(int, voidptr, int) int

View File

@ -22,12 +22,6 @@ fn (mut g Gen) gen_fn_decl(it ast.FnDecl) {
if g.attr == 'inline' {
g.write('inline ')
}
if it.name in ['backtrace', 'backtrace_symbols', 'backtrace_symbols_fd'] {
// these are actually defined as C functions in execinfo.h
// TODO: remove this check, *after* they are defined as C. in cfns.c.v
// in bootstrap phase 2
return
}
//
is_livefn := g.attr == 'live'
is_livemain := g.pref.is_livemain && is_livefn