builtin: remove commented code; improve print_backtrace with tcc

pull/10182/head
Delyan Angelov 2021-05-23 12:37:23 +03:00
parent a08cbd364a
commit a27e7f7675
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
3 changed files with 54 additions and 69 deletions

View File

@ -34,24 +34,22 @@ fn panic_debug(line_no int, file string, mod string, fn_name string, s string) {
eprintln('=========================================') eprintln('=========================================')
$if exit_after_panic_message ? { $if exit_after_panic_message ? {
C.exit(1) C.exit(1)
} $else $if no_backtrace ? {
C.exit(1)
} $else { } $else {
$if no_backtrace ? { $if tinyc {
C.exit(1)
} $else {
$if tinyc {
$if panics_break_into_debugger ? {
break_if_debugger_attached()
} $else {
C.tcc_backtrace(c'Backtrace')
}
C.exit(1)
}
print_backtrace_skipping_top_frames(1)
$if panics_break_into_debugger ? { $if panics_break_into_debugger ? {
break_if_debugger_attached() break_if_debugger_attached()
} $else {
C.tcc_backtrace(c'Backtrace')
} }
C.exit(1) C.exit(1)
} }
print_backtrace_skipping_top_frames(1)
$if panics_break_into_debugger ? {
break_if_debugger_attached()
}
C.exit(1)
} }
} }
} }
@ -71,24 +69,22 @@ pub fn panic(s string) {
eprintln('v hash: $vcommithash()') eprintln('v hash: $vcommithash()')
$if exit_after_panic_message ? { $if exit_after_panic_message ? {
C.exit(1) C.exit(1)
} $else $if no_backtrace ? {
C.exit(1)
} $else { } $else {
$if no_backtrace ? { $if tinyc {
C.exit(1)
} $else {
$if tinyc {
$if panics_break_into_debugger ? {
break_if_debugger_attached()
} $else {
C.tcc_backtrace(c'Backtrace')
}
C.exit(1)
}
print_backtrace_skipping_top_frames(1)
$if panics_break_into_debugger ? { $if panics_break_into_debugger ? {
break_if_debugger_attached() break_if_debugger_attached()
} $else {
C.tcc_backtrace(c'Backtrace')
} }
C.exit(1) C.exit(1)
} }
print_backtrace_skipping_top_frames(1)
$if panics_break_into_debugger ? {
break_if_debugger_attached()
}
C.exit(1)
} }
} }
} }
@ -170,8 +166,7 @@ pub fn eprint(s string) {
pub fn print(s string) { pub fn print(s string) {
$if android { $if android {
C.fprintf(C.stdout, c'%.*s', s.len, s.str) C.fprintf(C.stdout, c'%.*s', s.len, s.str)
} } $else $if ios {
$if ios {
// TODO: Implement a buffer as NSLog doesn't have a "print" // TODO: Implement a buffer as NSLog doesn't have a "print"
C.WrappedNSLog(s.str) C.WrappedNSLog(s.str)
} $else $if freestanding { } $else $if freestanding {
@ -181,13 +176,6 @@ pub fn print(s string) {
} }
} }
/*
#include "@VEXEROOT/vlib/darwin/darwin.m"
fn C.nsstring2(s string) voidptr
fn C.NSLog(x voidptr)
#include <asl.h>
fn C.asl_log(voidptr, voidptr, int, charptr)
*/
// println prints a message with a line end, to stdout. stdout is flushed. // println prints a message with a line end, to stdout. stdout is flushed.
pub fn println(s string) { pub fn println(s string) {
if s.str == 0 { if s.str == 0 {
@ -196,7 +184,6 @@ pub fn println(s string) {
} $else $if ios { } $else $if ios {
C.WrappedNSLog(c'println(NIL)') C.WrappedNSLog(c'println(NIL)')
} $else $if freestanding { } $else $if freestanding {
bare_print(s.str, u64(s.len))
bare_print(c'println(NIL)\n', 13) bare_print(c'println(NIL)\n', 13)
} $else { } $else {
_ = C.write(1, c'println(NIL)\n', 13) _ = C.write(1, c'println(NIL)\n', 13)
@ -205,8 +192,7 @@ pub fn println(s string) {
} }
$if android { $if android {
C.fprintf(C.stdout, c'%.*s\n', s.len, s.str) C.fprintf(C.stdout, c'%.*s\n', s.len, s.str)
} } $else $if ios {
$if ios {
C.WrappedNSLog(s.str) C.WrappedNSLog(s.str)
} $else $if freestanding { } $else $if freestanding {
bare_print(s.str, u64(s.len)) bare_print(s.str, u64(s.len))
@ -238,7 +224,7 @@ pub fn malloc(n int) &byte {
} }
mut res := &byte(0) mut res := &byte(0)
$if prealloc { $if prealloc {
res = unsafe { prealloc_malloc(n) } return unsafe { prealloc_malloc(n) }
} $else $if gcboehm ? { } $else $if gcboehm ? {
unsafe { unsafe {
res = C.GC_MALLOC(n) res = C.GC_MALLOC(n)
@ -265,10 +251,6 @@ pub fn malloc(n int) &byte {
return res return res
} }
/*
#include <malloc/malloc.h>
fn malloc_size(b byteptr) int
*/
// v_realloc resizes the memory block `b` with `n` bytes. // v_realloc resizes the memory block `b` with `n` bytes.
// The `b byteptr` must be a pointer to an existing memory block // The `b byteptr` must be a pointer to an existing memory block
// previously allocated with `malloc`, `v_calloc` or `vcalloc`. // previously allocated with `malloc`, `v_calloc` or `vcalloc`.
@ -281,15 +263,14 @@ pub fn v_realloc(b &byte, n int) &byte {
new_ptr = malloc(n) new_ptr = malloc(n)
C.memcpy(new_ptr, b, n) C.memcpy(new_ptr, b, n)
} }
return new_ptr
} $else $if gcboehm ? {
new_ptr = unsafe { C.GC_REALLOC(b, n) }
} $else { } $else {
$if gcboehm ? { new_ptr = unsafe { C.realloc(b, n) }
new_ptr = unsafe { C.GC_REALLOC(b, n) } }
} $else { if new_ptr == 0 {
new_ptr = unsafe { C.realloc(b, n) } panic('realloc($n) failed')
}
if new_ptr == 0 {
panic('realloc($n) failed')
}
} }
return new_ptr return new_ptr
} }
@ -348,8 +329,7 @@ pub fn vcalloc(n int) &byte {
} }
$if prealloc { $if prealloc {
return unsafe { prealloc_calloc(n) } return unsafe { prealloc_calloc(n) }
} } $else $if gcboehm ? {
$if gcboehm ? {
return unsafe { &byte(C.GC_MALLOC(n)) } return unsafe { &byte(C.GC_MALLOC(n)) }
} $else { } $else {
return unsafe { C.calloc(1, n) } return unsafe { C.calloc(1, n) }
@ -361,8 +341,7 @@ pub fn vcalloc(n int) &byte {
pub fn vcalloc_noscan(n int) &byte { pub fn vcalloc_noscan(n int) &byte {
$if prealloc { $if prealloc {
return unsafe { prealloc_calloc(n) } return unsafe { prealloc_calloc(n) }
} } $else $if gcboehm ? {
$if gcboehm ? {
$if vplayground ? { $if vplayground ? {
if n > 10000 { if n > 10000 {
panic('allocating more than 10 KB is not allowed in the playground') panic('allocating more than 10 KB is not allowed in the playground')
@ -382,8 +361,7 @@ pub fn vcalloc_noscan(n int) &byte {
pub fn free(ptr voidptr) { pub fn free(ptr voidptr) {
$if prealloc { $if prealloc {
return return
} } $else $if gcboehm ? {
$if gcboehm ? {
// It is generally better to leave it to Boehm's gc to free things. // It is generally better to leave it to Boehm's gc to free things.
// Calling C.GC_FREE(ptr) was tried initially, but does not work // Calling C.GC_FREE(ptr) was tried initially, but does not work
// well with programs that do manual management themselves. // well with programs that do manual management themselves.
@ -392,9 +370,9 @@ pub fn free(ptr voidptr) {
$if gcboehm_leak ? { $if gcboehm_leak ? {
unsafe { C.GC_FREE(ptr) } unsafe { C.GC_FREE(ptr) }
} }
return } $else {
C.free(ptr)
} }
C.free(ptr)
} }
// memdup dynamically allocates a `sz` bytes block of memory on the heap // memdup dynamically allocates a `sz` bytes block of memory on the heap

View File

@ -23,15 +23,21 @@ fn on_panic(f fn(int)int) {
// print_backtrace shows a backtrace of the current call stack on stdout // print_backtrace shows a backtrace of the current call stack on stdout
pub fn print_backtrace() { pub fn print_backtrace() {
// at the time of backtrace_symbols_fd call, the C stack would look something like this: // At the time of backtrace_symbols_fd call, the C stack would look something like this:
// 1 frame for print_backtrace_skipping_top_frames // * print_backtrace_skipping_top_frames
// 1 frame for print_backtrace itself // * print_backtrace itself
// ... print the rest of the backtrace frames ... // * the rest of the backtrace frames
// => top 2 frames should be skipped, since they will not be informative to the developer // => top 2 frames should be skipped, since they will not be informative to the developer
$if freestanding { $if !no_backtrace ? {
println(bare_backtrace()) $if freestanding {
} $else { println(bare_backtrace())
print_backtrace_skipping_top_frames(2) } $else {
$if tinyc {
C.tcc_backtrace(c'Backtrace')
} $else {
print_backtrace_skipping_top_frames(2)
}
}
} }
} }

View File

@ -1521,20 +1521,21 @@ pub fn (c byte) is_letter() bool {
} }
// free allows for manually freeing the memory occupied by the string // free allows for manually freeing the memory occupied by the string
[unsafe] [manualfree; unsafe]
pub fn (s &string) free() { pub fn (s &string) free() {
$if prealloc { $if prealloc {
return return
} }
if s.is_lit == -98761234 { if s.is_lit == -98761234 {
dsfree_msg := c'double string.free() detected\n'
$if freestanding { $if freestanding {
bare_eprint(c'double string.free() detected\n', u64(unsafe { C.strlen(c'double string.free() detected\n') })) bare_eprint(dsfree_msg, u64(unsafe { C.strlen(dsfree_msg) }))
} $else { } $else {
C.printf(c'double string.free() detected\n') C.printf(dsfree_msg)
} }
return return
} }
if s.is_lit == 1 || s.len == 0 { if s.is_lit == 1 || s.len == 0 || s.str == 0 {
return return
} }
unsafe { unsafe {