2022-01-04 12:21:08 +03:00
|
|
|
// Copyright (c) 2019-2022 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.
|
2021-11-16 20:31:42 +03:00
|
|
|
[has_globals]
|
2019-11-13 11:05:06 +03:00
|
|
|
module builtin
|
|
|
|
|
2020-04-16 12:29:36 +03:00
|
|
|
// dbghelp.h is already included in cheaders.v
|
2019-11-13 11:05:06 +03:00
|
|
|
#flag windows -l dbghelp
|
2021-01-25 22:19:15 +03:00
|
|
|
// SymbolInfo is used by print_backtrace_skipping_top_frames_msvc
|
2019-11-13 11:05:06 +03:00
|
|
|
pub struct SymbolInfo {
|
|
|
|
pub mut:
|
|
|
|
f_size_of_struct u32 // must be 88 to be recognised by SymFromAddr
|
2021-01-25 22:19:15 +03:00
|
|
|
f_type_index u32 // Type Index of symbol
|
|
|
|
f_reserved [2]u64
|
|
|
|
f_index u32
|
|
|
|
f_size u32
|
|
|
|
f_mod_base u64 // Base Address of module comtaining this symbol
|
|
|
|
f_flags u32
|
2022-04-15 21:04:10 +03:00
|
|
|
f_value u64 // Value of symbol, ValuePresent should be 1
|
|
|
|
f_address u64 // Address of symbol including base address of module
|
|
|
|
f_register u32 // register holding value or pointer to value
|
|
|
|
f_scope u32 // scope of the symbol
|
|
|
|
f_tag u32 // pdb classification
|
|
|
|
f_name_len u32 // Actual length of name
|
|
|
|
f_max_name_len u32 // must be manually set
|
|
|
|
f_name u8 // must be calloc(f_max_name_len)
|
2019-11-13 11:05:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct SymbolInfoContainer {
|
|
|
|
pub mut:
|
2021-01-25 22:19:15 +03:00
|
|
|
syminfo SymbolInfo
|
2019-11-13 11:05:06 +03:00
|
|
|
f_name_rest [254]char
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Line64 {
|
2020-04-27 23:53:26 +03:00
|
|
|
pub mut:
|
2019-11-13 11:05:06 +03:00
|
|
|
f_size_of_struct u32
|
2021-01-25 22:19:15 +03:00
|
|
|
f_key voidptr
|
|
|
|
f_line_number u32
|
2022-09-15 07:59:31 +03:00
|
|
|
f_file_name &u8 = unsafe { nil }
|
2021-01-25 22:19:15 +03:00
|
|
|
f_address u64
|
2019-11-13 11:05:06 +03:00
|
|
|
}
|
|
|
|
|
2021-01-25 22:19:15 +03:00
|
|
|
// returns the current options mask
|
|
|
|
fn C.SymSetOptions(symoptions u32) u32
|
|
|
|
|
|
|
|
// returns handle
|
|
|
|
fn C.GetCurrentProcess() voidptr
|
|
|
|
|
2022-04-15 15:18:21 +03:00
|
|
|
fn C.SymInitialize(h_process voidptr, p_user_search_path &u8, b_invade_process int) int
|
2021-01-25 22:19:15 +03:00
|
|
|
|
2019-11-13 11:05:06 +03:00
|
|
|
fn C.CaptureStackBackTrace(frames_to_skip u32, frames_to_capture u32, p_backtrace voidptr, p_backtrace_hash voidptr) u16
|
2021-01-25 22:19:15 +03:00
|
|
|
|
2019-11-13 11:05:06 +03:00
|
|
|
fn C.SymFromAddr(h_process voidptr, address u64, p_displacement voidptr, p_symbol voidptr) int
|
2021-01-25 22:19:15 +03:00
|
|
|
|
2019-11-13 11:05:06 +03:00
|
|
|
fn C.SymGetLineFromAddr64(h_process voidptr, address u64, p_displacement voidptr, p_line &Line64) int
|
|
|
|
|
|
|
|
// Ref - https://docs.microsoft.com/en-us/windows/win32/api/dbghelp/nf-dbghelp-symsetoptions
|
|
|
|
const (
|
2021-01-25 22:19:15 +03:00
|
|
|
symopt_undname = 0x00000002
|
|
|
|
symopt_deferred_loads = 0x00000004
|
|
|
|
symopt_no_cpp = 0x00000008
|
|
|
|
symopt_load_lines = 0x00000010
|
2020-05-22 18:36:09 +03:00
|
|
|
symopt_include_32bit_modules = 0x00002000
|
2021-01-25 22:19:15 +03:00
|
|
|
symopt_allow_zero_address = 0x01000000
|
2022-11-06 08:22:28 +03:00
|
|
|
symopt_debug = u32(0x80000000)
|
2019-11-13 11:05:06 +03:00
|
|
|
)
|
|
|
|
|
2020-09-11 13:06:11 +03:00
|
|
|
// g_original_codepage - used to restore the original windows console code page when exiting
|
2021-11-16 20:31:42 +03:00
|
|
|
__global g_original_codepage = u32(0)
|
2021-01-25 22:19:15 +03:00
|
|
|
|
2020-09-11 13:06:11 +03:00
|
|
|
// utf8 to stdout needs C.SetConsoleOutputCP(C.CP_UTF8)
|
|
|
|
fn C.GetConsoleOutputCP() u32
|
2021-01-25 22:19:15 +03:00
|
|
|
|
2020-09-11 13:06:11 +03:00
|
|
|
fn C.SetConsoleOutputCP(wCodePageID u32) bool
|
2021-01-25 22:19:15 +03:00
|
|
|
|
2020-09-11 13:06:11 +03:00
|
|
|
fn restore_codepage() {
|
2021-01-25 22:19:15 +03:00
|
|
|
C.SetConsoleOutputCP(g_original_codepage)
|
2020-09-11 13:06:11 +03:00
|
|
|
}
|
|
|
|
|
2021-04-14 08:50:50 +03:00
|
|
|
fn is_terminal(fd int) int {
|
|
|
|
mut mode := u32(0)
|
|
|
|
osfh := voidptr(C._get_osfhandle(fd))
|
|
|
|
C.GetConsoleMode(osfh, voidptr(&mode))
|
|
|
|
return int(mode)
|
|
|
|
}
|
|
|
|
|
2020-04-05 17:08:16 +03:00
|
|
|
fn builtin_init() {
|
2020-09-11 13:06:11 +03:00
|
|
|
g_original_codepage = C.GetConsoleOutputCP()
|
2020-09-16 17:40:02 +03:00
|
|
|
C.SetConsoleOutputCP(C.CP_UTF8)
|
2020-09-11 13:06:11 +03:00
|
|
|
C.atexit(restore_codepage)
|
2021-04-14 08:50:50 +03:00
|
|
|
if is_terminal(1) > 0 {
|
2021-01-25 18:51:36 +03:00
|
|
|
C.SetConsoleMode(C.GetStdHandle(C.STD_OUTPUT_HANDLE), C.ENABLE_PROCESSED_OUTPUT | C.ENABLE_WRAP_AT_EOL_OUTPUT | 0x0004) // enable_virtual_terminal_processing
|
|
|
|
C.SetConsoleMode(C.GetStdHandle(C.STD_ERROR_HANDLE), C.ENABLE_PROCESSED_OUTPUT | C.ENABLE_WRAP_AT_EOL_OUTPUT | 0x0004) // enable_virtual_terminal_processing
|
2020-07-20 20:06:41 +03:00
|
|
|
unsafe {
|
|
|
|
C.setbuf(C.stdout, 0)
|
2020-09-11 13:06:11 +03:00
|
|
|
C.setbuf(C.stderr, 0)
|
2020-07-20 20:06:41 +03:00
|
|
|
}
|
2020-04-03 14:27:19 +03:00
|
|
|
}
|
2021-06-21 08:01:18 +03:00
|
|
|
$if !no_backtrace ? {
|
|
|
|
add_unhandled_exception_handler()
|
|
|
|
}
|
2020-04-03 14:27:19 +03:00
|
|
|
}
|
|
|
|
|
2020-04-20 21:59:08 +03:00
|
|
|
fn print_backtrace_skipping_top_frames(skipframes int) bool {
|
|
|
|
$if msvc {
|
|
|
|
return print_backtrace_skipping_top_frames_msvc(skipframes)
|
|
|
|
}
|
2020-06-15 00:15:12 +03:00
|
|
|
$if tinyc {
|
|
|
|
return print_backtrace_skipping_top_frames_tcc(skipframes)
|
|
|
|
}
|
2020-04-20 21:59:08 +03:00
|
|
|
$if mingw {
|
|
|
|
return print_backtrace_skipping_top_frames_mingw(skipframes)
|
|
|
|
}
|
2020-05-04 11:21:25 +03:00
|
|
|
eprintln('print_backtrace_skipping_top_frames is not implemented')
|
2020-04-20 21:59:08 +03:00
|
|
|
return false
|
|
|
|
}
|
2019-11-13 11:05:06 +03:00
|
|
|
|
2020-04-20 21:59:08 +03:00
|
|
|
fn print_backtrace_skipping_top_frames_msvc(skipframes int) bool {
|
2021-01-25 22:19:15 +03:00
|
|
|
$if msvc {
|
|
|
|
mut offset := u64(0)
|
|
|
|
backtraces := [100]voidptr{}
|
|
|
|
sic := SymbolInfoContainer{}
|
|
|
|
mut si := &sic.syminfo
|
|
|
|
si.f_size_of_struct = sizeof(SymbolInfo) // Note: C.SYMBOL_INFO is 88
|
|
|
|
si.f_max_name_len = sizeof(SymbolInfoContainer) - sizeof(SymbolInfo) - 1
|
2021-04-04 17:43:32 +03:00
|
|
|
fname := &char(&si.f_name)
|
2021-04-06 02:52:54 +03:00
|
|
|
mut sline64 := Line64{
|
2022-04-15 13:55:56 +03:00
|
|
|
f_file_name: &u8(0)
|
2021-04-06 02:52:54 +03:00
|
|
|
}
|
2021-01-25 22:19:15 +03:00
|
|
|
sline64.f_size_of_struct = sizeof(Line64)
|
2019-11-13 11:05:06 +03:00
|
|
|
|
2021-01-25 22:19:15 +03:00
|
|
|
handle := C.GetCurrentProcess()
|
|
|
|
defer {
|
|
|
|
C.SymCleanup(handle)
|
|
|
|
}
|
|
|
|
|
|
|
|
C.SymSetOptions(symopt_debug | symopt_load_lines | symopt_undname)
|
|
|
|
|
|
|
|
syminitok := C.SymInitialize(handle, 0, 1)
|
|
|
|
if syminitok != 1 {
|
|
|
|
eprintln('Failed getting process: Aborting backtrace.\n')
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-03-15 16:55:07 +03:00
|
|
|
frames := int(C.CaptureStackBackTrace(skipframes + 1, 100, &backtraces[0], 0))
|
2021-01-25 22:19:15 +03:00
|
|
|
if frames < 2 {
|
|
|
|
eprintln('C.CaptureStackBackTrace returned less than 2 frames')
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for i in 0 .. frames {
|
|
|
|
frame_addr := backtraces[i]
|
|
|
|
if C.SymFromAddr(handle, frame_addr, &offset, si) == 1 {
|
|
|
|
nframe := frames - i - 1
|
|
|
|
mut lineinfo := ''
|
|
|
|
if C.SymGetLineFromAddr64(handle, frame_addr, &offset, &sline64) == 1 {
|
2021-02-15 18:15:52 +03:00
|
|
|
file_name := unsafe { tos3(sline64.f_file_name) }
|
2021-01-25 22:19:15 +03:00
|
|
|
lnumber := sline64.f_line_number
|
2022-11-15 16:53:13 +03:00
|
|
|
lineinfo = '${file_name}:${lnumber}'
|
2021-01-25 22:19:15 +03:00
|
|
|
} else {
|
2022-07-23 01:36:27 +03:00
|
|
|
// addr:
|
2021-01-25 22:19:15 +03:00
|
|
|
lineinfo = '?? : address = 0x${(&frame_addr):x}'
|
|
|
|
}
|
2021-02-15 18:15:52 +03:00
|
|
|
sfunc := unsafe { tos3(fname) }
|
2022-11-15 16:53:13 +03:00
|
|
|
eprintln('${nframe:-2d}: ${sfunc:-25s} ${lineinfo}')
|
2020-04-20 21:59:08 +03:00
|
|
|
} else {
|
2021-01-25 22:19:15 +03:00
|
|
|
// https://docs.microsoft.com/en-us/windows/win32/debug/system-error-codes
|
|
|
|
cerr := int(C.GetLastError())
|
|
|
|
if cerr == 87 {
|
2022-11-15 16:53:13 +03:00
|
|
|
eprintln('SymFromAddr failure: ${cerr} = The parameter is incorrect)')
|
2021-01-25 22:19:15 +03:00
|
|
|
} else if cerr == 487 {
|
|
|
|
// probably caused because the .pdb isn't in the executable folder
|
2022-11-15 16:53:13 +03:00
|
|
|
eprintln('SymFromAddr failure: ${cerr} = Attempt to access invalid address (Verify that you have the .pdb file in the right folder.)')
|
2021-01-25 22:19:15 +03:00
|
|
|
} else {
|
2022-11-15 16:53:13 +03:00
|
|
|
eprintln('SymFromAddr failure: ${cerr} (see https://docs.microsoft.com/en-us/windows/win32/debug/system-error-codes)')
|
2021-01-25 22:19:15 +03:00
|
|
|
}
|
2019-11-13 11:05:06 +03:00
|
|
|
}
|
|
|
|
}
|
2021-01-25 22:19:15 +03:00
|
|
|
return true
|
|
|
|
} $else {
|
|
|
|
eprintln('print_backtrace_skipping_top_frames_msvc must be called only when the compiler is msvc')
|
|
|
|
return false
|
2019-11-13 11:05:06 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn print_backtrace_skipping_top_frames_mingw(skipframes int) bool {
|
2020-05-04 11:21:25 +03:00
|
|
|
eprintln('print_backtrace_skipping_top_frames_mingw is not implemented')
|
2019-11-13 11:05:06 +03:00
|
|
|
return false
|
|
|
|
}
|
2020-05-29 04:06:27 +03:00
|
|
|
|
2021-04-05 21:39:32 +03:00
|
|
|
fn C.tcc_backtrace(fmt &char) int
|
2021-01-25 22:19:15 +03:00
|
|
|
|
2020-06-15 00:15:12 +03:00
|
|
|
fn print_backtrace_skipping_top_frames_tcc(skipframes int) bool {
|
|
|
|
$if tinyc {
|
2021-01-25 22:19:15 +03:00
|
|
|
$if no_backtrace ? {
|
|
|
|
eprintln('backtraces are disabled')
|
2021-01-26 09:26:54 +03:00
|
|
|
return false
|
2021-01-25 22:19:15 +03:00
|
|
|
} $else {
|
2021-04-05 21:39:32 +03:00
|
|
|
C.tcc_backtrace(c'Backtrace')
|
2021-01-26 09:26:54 +03:00
|
|
|
return true
|
2021-01-25 22:19:15 +03:00
|
|
|
}
|
2020-06-15 00:15:12 +03:00
|
|
|
} $else {
|
|
|
|
eprintln('print_backtrace_skipping_top_frames_tcc must be called only when the compiler is tcc')
|
|
|
|
return false
|
|
|
|
}
|
2021-01-26 09:26:54 +03:00
|
|
|
// Not reachable, but it looks like it's not detectable by V
|
|
|
|
return false
|
2020-06-15 00:15:12 +03:00
|
|
|
}
|
2020-05-29 04:06:27 +03:00
|
|
|
|
2021-01-25 22:19:15 +03:00
|
|
|
// TODO copypaste from os
|
2020-05-29 04:06:27 +03:00
|
|
|
// we want to be able to use this here without having to `import os`
|
|
|
|
struct ExceptionRecord {
|
|
|
|
pub:
|
|
|
|
// status_ constants
|
2021-01-25 22:19:15 +03:00
|
|
|
code u32
|
|
|
|
flags u32
|
2022-09-15 07:59:31 +03:00
|
|
|
record &ExceptionRecord = unsafe { nil }
|
2021-01-25 22:19:15 +03:00
|
|
|
address voidptr
|
2020-05-29 04:06:27 +03:00
|
|
|
param_count u32
|
|
|
|
// params []voidptr
|
|
|
|
}
|
|
|
|
|
2021-01-25 22:19:15 +03:00
|
|
|
struct ContextRecord {
|
2020-05-29 04:06:27 +03:00
|
|
|
// TODO
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ExceptionPointers {
|
|
|
|
pub:
|
2022-09-15 07:59:31 +03:00
|
|
|
exception_record &ExceptionRecord = unsafe { nil }
|
|
|
|
context_record &ContextRecord = unsafe { nil }
|
2020-05-29 04:06:27 +03:00
|
|
|
}
|
|
|
|
|
2021-01-25 22:19:15 +03:00
|
|
|
type VectoredExceptionHandler = fn (&ExceptionPointers) int
|
2020-05-29 04:06:27 +03:00
|
|
|
|
2020-09-27 15:56:30 +03:00
|
|
|
fn C.AddVectoredExceptionHandler(int, C.PVECTORED_EXCEPTION_HANDLER)
|
2021-01-25 22:19:15 +03:00
|
|
|
|
2020-05-29 04:06:27 +03:00
|
|
|
fn add_vectored_exception_handler(handler VectoredExceptionHandler) {
|
2020-09-16 17:40:02 +03:00
|
|
|
C.AddVectoredExceptionHandler(1, C.PVECTORED_EXCEPTION_HANDLER(handler))
|
2020-05-29 04:06:27 +03:00
|
|
|
}
|
|
|
|
|
2022-04-14 13:28:46 +03:00
|
|
|
[callconv: stdcall]
|
2020-09-27 15:56:30 +03:00
|
|
|
fn unhandled_exception_handler(e &ExceptionPointers) int {
|
2020-05-29 04:06:27 +03:00
|
|
|
match e.exception_record.code {
|
2020-07-24 00:18:45 +03:00
|
|
|
// These are 'used' by the backtrace printer
|
2020-05-29 04:06:27 +03:00
|
|
|
// so we dont want to catch them...
|
2022-04-29 14:55:29 +03:00
|
|
|
0x4001000A, 0x40010006, 0xE06D7363 {
|
2020-05-29 04:06:27 +03:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
println('Unhandled Exception 0x${e.exception_record.code:X}')
|
|
|
|
print_backtrace_skipping_top_frames(5)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2020-05-31 13:57:26 +03:00
|
|
|
fn add_unhandled_exception_handler() {
|
2020-10-02 06:17:33 +03:00
|
|
|
add_vectored_exception_handler(VectoredExceptionHandler(voidptr(unhandled_exception_handler)))
|
2020-05-31 13:57:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn C.IsDebuggerPresent() bool
|
2021-01-25 22:19:15 +03:00
|
|
|
|
2020-05-31 13:57:26 +03:00
|
|
|
fn C.__debugbreak()
|
|
|
|
|
|
|
|
fn break_if_debugger_attached() {
|
2020-06-15 00:15:12 +03:00
|
|
|
$if tinyc {
|
|
|
|
unsafe {
|
2020-07-24 00:18:45 +03:00
|
|
|
mut ptr := &voidptr(0)
|
2022-07-21 21:01:30 +03:00
|
|
|
*ptr = nil
|
2021-04-23 15:17:02 +03:00
|
|
|
_ = ptr
|
2020-06-15 00:15:12 +03:00
|
|
|
}
|
|
|
|
} $else {
|
|
|
|
if C.IsDebuggerPresent() {
|
|
|
|
C.__debugbreak()
|
|
|
|
}
|
2020-05-31 13:57:26 +03:00
|
|
|
}
|
|
|
|
}
|
2021-07-25 01:13:34 +03:00
|
|
|
|
|
|
|
// return an error message generated from WinAPI's `LastError`
|
|
|
|
pub fn winapi_lasterr_str() string {
|
|
|
|
err_msg_id := C.GetLastError()
|
2021-07-25 15:34:48 +03:00
|
|
|
if err_msg_id == 8 {
|
|
|
|
// handle this case special since `FormatMessage()` might not work anymore
|
|
|
|
return 'insufficient memory'
|
|
|
|
}
|
|
|
|
mut msgbuf := &u16(0)
|
|
|
|
res := C.FormatMessage(C.FORMAT_MESSAGE_ALLOCATE_BUFFER | C.FORMAT_MESSAGE_FROM_SYSTEM | C.FORMAT_MESSAGE_IGNORE_INSERTS,
|
|
|
|
C.NULL, err_msg_id, C.MAKELANGID(C.LANG_NEUTRAL, C.SUBLANG_DEFAULT), &msgbuf,
|
|
|
|
0, C.NULL)
|
2021-07-25 01:13:34 +03:00
|
|
|
err_msg := if res == 0 {
|
2022-11-15 16:53:13 +03:00
|
|
|
'Win-API error ${err_msg_id}'
|
2021-07-25 01:13:34 +03:00
|
|
|
} else {
|
2021-07-25 15:34:48 +03:00
|
|
|
unsafe { string_from_wide(msgbuf) }
|
2021-07-25 01:13:34 +03:00
|
|
|
}
|
|
|
|
return err_msg
|
|
|
|
}
|
|
|
|
|
|
|
|
// panic with an error message generated from WinAPI's `LastError`
|
|
|
|
[noreturn]
|
|
|
|
pub fn panic_lasterr(base string) {
|
|
|
|
panic(base + winapi_lasterr_str())
|
|
|
|
}
|