2019-11-13 11:05:06 +03:00
|
|
|
// Copyright (c) 2019 Alexander Medvednikov. All rights reserved.
|
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
module builtin
|
|
|
|
|
2019-12-31 10:53:53 +03:00
|
|
|
|
|
|
|
//pub fn vsyscall(id int
|
|
|
|
//
|
|
|
|
|
|
|
|
/*
|
|
|
|
pub const (
|
|
|
|
sys_write = 1
|
|
|
|
sys_mkdir = 83
|
|
|
|
)
|
|
|
|
const (
|
|
|
|
stdin_value = 0
|
|
|
|
stdout_value = 1
|
|
|
|
stderr_value = 2
|
|
|
|
)
|
|
|
|
|
|
|
|
fn C.puts(charptr)
|
|
|
|
*/
|
|
|
|
|
2019-12-12 04:09:31 +03:00
|
|
|
pub fn println(s string) {
|
2019-12-31 10:53:53 +03:00
|
|
|
/*
|
|
|
|
$if linux {
|
|
|
|
snl := s + '\n'
|
|
|
|
C.syscall(sys_write, stdout_value, snl.str, s.len+1)
|
|
|
|
}
|
|
|
|
$else {
|
|
|
|
*/
|
|
|
|
C.printf('%.*s\n', s.len, s.str)
|
|
|
|
//}
|
2019-12-12 04:09:31 +03:00
|
|
|
}
|
|
|
|
|
2019-11-13 11:05:06 +03:00
|
|
|
fn print_backtrace_skipping_top_frames_msvc(skipframes int) bool {
|
|
|
|
println('not implemented, see builtin_windows.v')
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
fn print_backtrace_skipping_top_frames_mingw(skipframes int) bool {
|
|
|
|
println('not implemented, see builtin_windows.v')
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
fn print_backtrace_skipping_top_frames_nix(xskipframes int) bool {
|
|
|
|
skipframes := xskipframes + 2
|
2019-12-19 23:52:45 +03:00
|
|
|
$if macos {
|
|
|
|
return print_backtrace_skipping_top_frames_mac(skipframes)
|
|
|
|
}
|
|
|
|
$if linux {
|
|
|
|
return print_backtrace_skipping_top_frames_linux(skipframes)
|
|
|
|
}
|
|
|
|
$if freebsd {
|
|
|
|
return print_backtrace_skipping_top_frames_freebsd(skipframes)
|
|
|
|
}
|
2019-11-13 11:05:06 +03:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// the functions below are not called outside this file,
|
|
|
|
// so there is no need to have their twins in builtin_windows.v
|
|
|
|
fn print_backtrace_skipping_top_frames_mac(skipframes int) bool {
|
2019-12-03 16:29:24 +03:00
|
|
|
$if macos {
|
2019-12-19 23:52:45 +03:00
|
|
|
buffer := [100]byteptr
|
|
|
|
nr_ptrs := C.backtrace(*voidptr(buffer), 100)
|
|
|
|
C.backtrace_symbols_fd(*voidptr(&buffer[skipframes]), nr_ptrs - skipframes, 1)
|
2019-11-15 00:46:40 +03:00
|
|
|
}
|
2019-11-13 11:05:06 +03:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-11-25 13:54:56 +03:00
|
|
|
fn print_backtrace_skipping_top_frames_freebsd(skipframes int) bool {
|
|
|
|
$if freebsd {
|
2019-12-19 23:52:45 +03:00
|
|
|
buffer := [100]byteptr
|
|
|
|
nr_ptrs := C.backtrace(*voidptr(buffer), 100)
|
|
|
|
C.backtrace_symbols_fd(*voidptr(&buffer[skipframes]), nr_ptrs - skipframes, 1)
|
|
|
|
}
|
|
|
|
return true
|
2019-11-25 13:54:56 +03:00
|
|
|
}
|
|
|
|
|
2019-11-13 11:05:06 +03:00
|
|
|
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" .')
|
|
|
|
return false
|
|
|
|
}
|
2019-12-19 23:52:45 +03:00
|
|
|
$if !android {
|
|
|
|
// backtrace is not available on Android.
|
2019-11-13 11:05:06 +03:00
|
|
|
$if glibc {
|
|
|
|
buffer := [100]byteptr
|
|
|
|
nr_ptrs := C.backtrace(*voidptr(buffer), 100)
|
2019-12-19 23:52:45 +03:00
|
|
|
nr_actual_frames := nr_ptrs - skipframes
|
2019-11-13 11:05:06 +03:00
|
|
|
mut sframes := []string
|
2019-12-19 23:52:45 +03:00
|
|
|
csymbols := C.backtrace_symbols(*voidptr(&buffer[skipframes]), nr_actual_frames)
|
|
|
|
for i in 0 .. nr_actual_frames {
|
|
|
|
sframes << tos2(csymbols[i])
|
|
|
|
}
|
2019-11-13 11:05:06 +03:00
|
|
|
for sframe in sframes {
|
|
|
|
executable := sframe.all_before('(')
|
|
|
|
addr := sframe.all_after('[').all_before(']')
|
2019-11-23 14:22:55 +03:00
|
|
|
beforeaddr := sframe.all_before('[')
|
2019-11-13 11:05:06 +03:00
|
|
|
cmd := 'addr2line -e $executable $addr'
|
|
|
|
// taken from os, to avoid depending on the os module inside builtin.v
|
|
|
|
f := C.popen(cmd.str, 'r')
|
|
|
|
if isnil(f) {
|
2019-12-19 23:52:45 +03:00
|
|
|
println(sframe)
|
|
|
|
continue
|
2019-11-13 11:05:06 +03:00
|
|
|
}
|
|
|
|
buf := [1000]byte
|
|
|
|
mut output := ''
|
|
|
|
for C.fgets(voidptr(buf), 1000, f) != 0 {
|
|
|
|
output += tos(buf, vstrlen(buf))
|
|
|
|
}
|
2019-12-19 23:52:45 +03:00
|
|
|
output = output.trim_space() + ':'
|
2019-12-07 15:31:56 +03:00
|
|
|
if 0 != C.pclose(f) {
|
2019-12-19 23:52:45 +03:00
|
|
|
println(sframe)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if output in ['??:0:', '??:?:'] {
|
|
|
|
output = ''
|
2019-11-13 11:05:06 +03:00
|
|
|
}
|
2019-12-19 23:52:45 +03:00
|
|
|
println('${output:-46s} | ${addr:14s} | $beforeaddr')
|
2019-11-13 11:05:06 +03:00
|
|
|
}
|
2019-12-19 23:52:45 +03:00
|
|
|
// C.backtrace_symbols_fd(*voidptr(&buffer[skipframes]), nr_actual_frames, 1)
|
2019-11-13 11:05:06 +03:00
|
|
|
return true
|
2019-12-19 23:52:45 +03:00
|
|
|
} $else {
|
2019-12-31 10:53:53 +03:00
|
|
|
println('backtrace_symbols_fd is missing, so printing backtraces is not available.\n')
|
|
|
|
println('Some libc implementations like musl simply do not provide it.')
|
2019-11-13 11:05:06 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2019-12-19 23:52:45 +03:00
|
|
|
|