2020-01-23 23:04:46 +03:00
|
|
|
// Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved.
|
2019-11-13 11:05:06 +03:00
|
|
|
// 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
|
|
|
|
)
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2020-04-05 17:08:16 +03:00
|
|
|
fn builtin_init() {
|
2020-04-03 14:27:19 +03:00
|
|
|
// Do nothing
|
|
|
|
}
|
|
|
|
|
2020-04-20 21:59:08 +03:00
|
|
|
fn print_backtrace_skipping_top_frames(xskipframes int) bool {
|
2019-11-13 11:05:06 +03:00
|
|
|
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)
|
|
|
|
}
|
2020-03-16 14:10:58 +03:00
|
|
|
$if netbsd {
|
|
|
|
return print_backtrace_skipping_top_frames_freebsd(skipframes)
|
|
|
|
}
|
|
|
|
$if openbsd {
|
|
|
|
return print_backtrace_skipping_top_frames_freebsd(skipframes)
|
|
|
|
}
|
2020-05-04 12:04:54 +03:00
|
|
|
println('print_backtrace_skipping_top_frames is not implemented. skipframes: $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 {
|
2020-08-16 05:54:05 +03:00
|
|
|
buffer := [100]byteptr{}
|
2020-05-15 15:23:58 +03:00
|
|
|
nr_ptrs := C.backtrace(buffer, 100)
|
2020-08-06 19:50:25 +03:00
|
|
|
if nr_ptrs < 2 {
|
|
|
|
eprintln('C.backtrace returned less than 2 frames')
|
|
|
|
return false
|
|
|
|
}
|
2020-05-15 15:23:58 +03:00
|
|
|
C.backtrace_symbols_fd(&buffer[skipframes], nr_ptrs - skipframes, 2)
|
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 {
|
2020-08-16 05:54:05 +03:00
|
|
|
buffer := [100]byteptr{}
|
2020-05-15 15:23:58 +03:00
|
|
|
nr_ptrs := C.backtrace(buffer, 100)
|
2020-08-06 19:50:25 +03:00
|
|
|
if nr_ptrs < 2 {
|
|
|
|
eprintln('C.backtrace returned less than 2 frames')
|
|
|
|
return false
|
|
|
|
}
|
2020-05-15 15:23:58 +03:00
|
|
|
C.backtrace_symbols_fd(&buffer[skipframes], nr_ptrs - skipframes, 2)
|
2019-12-19 23:52:45 +03:00
|
|
|
}
|
|
|
|
return true
|
2019-11-25 13:54:56 +03:00
|
|
|
}
|
|
|
|
|
2020-06-08 10:46:51 +03:00
|
|
|
fn C.tcc_backtrace(fmt charptr, other ...charptr) int
|
2019-11-13 11:05:06 +03:00
|
|
|
fn print_backtrace_skipping_top_frames_linux(skipframes int) bool {
|
2020-05-15 15:23:58 +03:00
|
|
|
$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
|
|
|
|
}
|
2020-11-19 18:57:51 +03:00
|
|
|
$if no_backtrace ? {
|
2020-08-06 19:50:25 +03:00
|
|
|
return false
|
2020-11-19 18:57:51 +03:00
|
|
|
} $else {
|
2020-11-25 16:40:17 +03:00
|
|
|
$if tinyc {
|
|
|
|
C.tcc_backtrace("Backtrace")
|
|
|
|
return false
|
|
|
|
}
|
2020-11-19 18:57:51 +03:00
|
|
|
buffer := [100]byteptr{}
|
|
|
|
nr_ptrs := C.backtrace(voidptr(buffer), 100)
|
|
|
|
if nr_ptrs < 2 {
|
|
|
|
eprintln('C.backtrace returned less than 2 frames')
|
|
|
|
return false
|
2020-05-15 15:23:58 +03:00
|
|
|
}
|
2020-11-19 18:57:51 +03:00
|
|
|
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]) )}
|
2020-05-15 15:23:58 +03:00
|
|
|
}
|
2020-11-19 18:57:51 +03:00
|
|
|
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')
|
2020-05-15 15:23:58 +03:00
|
|
|
}
|
2019-11-13 11:05:06 +03:00
|
|
|
}
|
2020-05-15 15:23:58 +03:00
|
|
|
return true
|
2019-11-13 11:05:06 +03:00
|
|
|
}
|
2020-05-31 13:57:26 +03:00
|
|
|
|
|
|
|
fn break_if_debugger_attached() {
|
2020-06-03 15:11:23 +03:00
|
|
|
unsafe {
|
2020-07-24 00:16:36 +03:00
|
|
|
mut ptr := &voidptr(0)
|
2020-06-03 15:11:23 +03:00
|
|
|
*ptr = 0
|
|
|
|
}
|
|
|
|
}
|