2021-01-18 13:20:06 +01:00
|
|
|
// Copyright (c) 2019-2021 Alexander Medvednikov. All rights reserved.
|
2019-11-13 09:05:06 +01:00
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
module builtin
|
|
|
|
|
2021-03-24 19:39:59 +01:00
|
|
|
// pub fn vsyscall(id int
|
2019-12-31 08:53:53 +01:00
|
|
|
//
|
|
|
|
|
|
|
|
/*
|
|
|
|
pub const (
|
|
|
|
sys_write = 1
|
|
|
|
sys_mkdir = 83
|
|
|
|
)
|
|
|
|
const (
|
|
|
|
stdin_value = 0
|
|
|
|
stdout_value = 1
|
|
|
|
stderr_value = 2
|
|
|
|
)
|
|
|
|
*/
|
|
|
|
|
2020-04-05 16:08:16 +02:00
|
|
|
fn builtin_init() {
|
2020-04-03 13:27:19 +02:00
|
|
|
// Do nothing
|
|
|
|
}
|
|
|
|
|
2020-04-20 20:59:08 +02:00
|
|
|
fn print_backtrace_skipping_top_frames(xskipframes int) bool {
|
2021-07-17 14:16:44 +02:00
|
|
|
$if no_backtrace ? {
|
|
|
|
return false
|
|
|
|
} $else {
|
|
|
|
skipframes := xskipframes + 2
|
|
|
|
$if macos || freebsd || openbsd || netbsd {
|
|
|
|
return print_backtrace_skipping_top_frames_bsd(skipframes)
|
|
|
|
} $else $if linux {
|
|
|
|
return print_backtrace_skipping_top_frames_linux(skipframes)
|
|
|
|
} $else {
|
|
|
|
println('print_backtrace_skipping_top_frames is not implemented. skipframes: $skipframes')
|
2020-08-06 18:50:25 +02:00
|
|
|
}
|
2019-11-14 22:46:40 +01:00
|
|
|
}
|
2021-07-17 17:09:16 +02:00
|
|
|
return false
|
2019-11-13 09:05:06 +01:00
|
|
|
}
|
|
|
|
|
2021-07-17 14:16:44 +02:00
|
|
|
// 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_bsd(skipframes int) bool {
|
|
|
|
$if no_backtrace ? {
|
|
|
|
return false
|
|
|
|
} $else {
|
|
|
|
$if macos || freebsd || openbsd || netbsd {
|
|
|
|
buffer := [100]voidptr{}
|
|
|
|
nr_ptrs := C.backtrace(&buffer[0], 100)
|
|
|
|
if nr_ptrs < 2 {
|
|
|
|
eprintln('C.backtrace returned less than 2 frames')
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
C.backtrace_symbols_fd(&buffer[skipframes], nr_ptrs - skipframes, 2)
|
2020-08-06 18:50:25 +02:00
|
|
|
}
|
2021-07-17 14:16:44 +02:00
|
|
|
return true
|
2019-12-19 21:52:45 +01:00
|
|
|
}
|
2019-11-25 11:54:56 +01:00
|
|
|
}
|
|
|
|
|
2021-04-05 20:39:32 +02:00
|
|
|
fn C.tcc_backtrace(fmt &char) int
|
2019-11-13 09:05:06 +01:00
|
|
|
fn print_backtrace_skipping_top_frames_linux(skipframes int) bool {
|
2020-05-15 14:23:58 +02: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 16:57:51 +01:00
|
|
|
$if no_backtrace ? {
|
2020-08-06 18:50:25 +02:00
|
|
|
return false
|
2020-11-19 16:57:51 +01:00
|
|
|
} $else {
|
2021-06-01 21:28:30 +02:00
|
|
|
$if linux && !freestanding {
|
2021-04-14 07:50:50 +02:00
|
|
|
$if tinyc {
|
|
|
|
C.tcc_backtrace(c'Backtrace')
|
|
|
|
return false
|
2020-11-19 16:57:51 +01:00
|
|
|
}
|
2021-04-14 07:50:50 +02:00
|
|
|
buffer := [100]voidptr{}
|
|
|
|
nr_ptrs := C.backtrace(&buffer[0], 100)
|
|
|
|
if nr_ptrs < 2 {
|
|
|
|
eprintln('C.backtrace returned less than 2 frames')
|
|
|
|
return false
|
2020-11-19 16:57:51 +01:00
|
|
|
}
|
2021-04-14 07:50:50 +02: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(&byte(csymbols[i])) }
|
2020-11-19 16:57:51 +01:00
|
|
|
}
|
2021-04-14 07:50:50 +02: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(&char(cmd.str), c'r')
|
2021-07-01 05:21:03 +02:00
|
|
|
if isnil(f) {
|
2021-04-14 07:50:50 +02:00
|
|
|
eprintln(sframe)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
buf := [1000]byte{}
|
|
|
|
mut output := ''
|
|
|
|
unsafe {
|
|
|
|
bp := &buf[0]
|
|
|
|
for C.fgets(&char(bp), 1000, f) != 0 {
|
|
|
|
output += tos(bp, vstrlen(bp))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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:-55s} | ${addr:14s} | $beforeaddr')
|
2020-11-19 16:57:51 +01:00
|
|
|
}
|
2020-05-15 14:23:58 +02:00
|
|
|
}
|
2019-11-13 09:05:06 +01:00
|
|
|
}
|
2020-05-15 14:23:58 +02:00
|
|
|
return true
|
2019-11-13 09:05:06 +01:00
|
|
|
}
|
2020-05-31 12:57:26 +02:00
|
|
|
|
|
|
|
fn break_if_debugger_attached() {
|
2020-06-03 14:11:23 +02:00
|
|
|
unsafe {
|
2020-07-23 23:16:36 +02:00
|
|
|
mut ptr := &voidptr(0)
|
2020-12-12 10:42:07 +01:00
|
|
|
*ptr = voidptr(0)
|
2021-04-23 12:33:48 +02:00
|
|
|
_ = ptr
|
2020-06-03 14:11:23 +02:00
|
|
|
}
|
|
|
|
}
|
2021-07-25 00:13:34 +02:00
|
|
|
|
|
|
|
// These functions are Windows specific - provide dummys for *nix
|
|
|
|
pub fn winapi_lasterr_str() string {
|
|
|
|
return ''
|
|
|
|
}
|
|
|
|
|
|
|
|
[noreturn]
|
|
|
|
pub fn panic_lasterr() {}
|