1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

tests: improve diagnostic output on failure

This commit is contained in:
Delyan Angelov
2020-05-04 11:21:25 +03:00
committed by GitHub
parent e0e064ff08
commit acd80f052b
10 changed files with 90 additions and 42 deletions

View File

@@ -32,26 +32,31 @@ pub fn print_backtrace() {
// replaces panic when -debug arg is passed
fn panic_debug(line_no int, file, mod, fn_name, s string) {
println('================ V panic ================')
println(' module: $mod')
println(' function: ${fn_name}()')
println(' file: $file')
println(' line: ' + line_no.str())
println(' message: $s')
println('=========================================')
// NB: the order here is important for a stabler test output
// module is less likely to change than function, etc...
// During edits, the line number will change most frequently,
// so it is last
eprintln('================ V panic ================')
eprintln(' module: $mod')
eprintln(' function: ${fn_name}()')
eprintln(' message: $s')
eprintln(' file: $file')
eprintln(' line: ' + line_no.str())
eprintln('=========================================')
print_backtrace_skipping_top_frames(1)
C.exit(1)
}
pub fn panic(s string) {
println('V panic: $s')
eprintln('V panic: $s')
print_backtrace()
C.exit(1)
}
pub fn eprintln(s string) {
// eprintln is used in panics, so it should not fail at all
if s.str == 0 {
panic('eprintln(NIL)')
eprintln('eprintln(NIL)')
}
$if !windows {
C.fflush(C.stdout)
@@ -66,7 +71,7 @@ pub fn eprintln(s string) {
pub fn eprint(s string) {
if s.str == 0 {
panic('eprint(NIL)')
eprintln('eprint(NIL)')
}
$if !windows {
C.fflush(C.stdout)

View File

@@ -65,7 +65,7 @@ fn print_backtrace_skipping_top_frames_mac(skipframes int) bool {
$if macos {
buffer := [100]byteptr
nr_ptrs := backtrace(buffer, 100)
backtrace_symbols_fd(&buffer[skipframes], nr_ptrs - skipframes, 1)
backtrace_symbols_fd(&buffer[skipframes], nr_ptrs - skipframes, 2)
}
return true
}
@@ -74,7 +74,7 @@ fn print_backtrace_skipping_top_frames_freebsd(skipframes int) bool {
$if freebsd {
buffer := [100]byteptr
nr_ptrs := backtrace(buffer, 100)
backtrace_symbols_fd(&buffer[skipframes], nr_ptrs - skipframes, 1)
backtrace_symbols_fd(&buffer[skipframes], nr_ptrs - skipframes, 2)
}
return true
}
@@ -104,7 +104,7 @@ fn print_backtrace_skipping_top_frames_linux(skipframes int) bool {
// taken from os, to avoid depending on the os module inside builtin.v
f := C.popen(cmd.str, 'r')
if isnil(f) {
println(sframe)
eprintln(sframe)
continue
}
buf := [1000]byte
@@ -114,7 +114,7 @@ fn print_backtrace_skipping_top_frames_linux(skipframes int) bool {
}
output = output.trim_space() + ':'
if C.pclose(f) != 0 {
println(sframe)
eprintln(sframe)
continue
}
if output in ['??:0:', '??:?:'] {
@@ -124,13 +124,13 @@ fn print_backtrace_skipping_top_frames_linux(skipframes int) bool {
// 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.')
println('${output:-46s} | ${addr:14s} | $beforeaddr')
eprintln('${output:-46s} | ${addr:14s} | $beforeaddr')
}
// backtrace_symbols_fd(*voidptr(&buffer[skipframes]), nr_actual_frames, 1)
return true
} $else {
println('backtrace_symbols_fd is missing, so printing backtraces is not available.\n')
println('Some libc implementations like musl simply do not provide it.')
eprintln('backtrace_symbols_fd is missing, so printing backtraces is not available.\n')
eprintln('Some libc implementations like musl simply do not provide it.')
}
}
return false

View File

@@ -69,12 +69,11 @@ fn builtin_init() {
fn print_backtrace_skipping_top_frames(skipframes int) bool {
$if msvc {
return print_backtrace_skipping_top_frames_msvc(skipframes)
}
$if mingw {
return print_backtrace_skipping_top_frames_mingw(skipframes)
}
println('print_backtrace_skipping_top_frames is not implemented')
eprintln('print_backtrace_skipping_top_frames is not implemented')
return false
}
@@ -97,7 +96,7 @@ $if msvc {
syminitok := C.SymInitialize( handle, 0, 1)
if syminitok != 1 {
println('Failed getting process: Aborting backtrace.\n')
eprintln('Failed getting process: Aborting backtrace.\n')
return true
}
@@ -115,27 +114,29 @@ $if msvc {
lineinfo = '?? : address = 0x${&frame_addr:x}'
}
sfunc := tos3(fname)
println('${nframe:-2d}: ${sfunc:-25s} $lineinfo')
eprintln('${nframe:-2d}: ${sfunc:-25s} $lineinfo')
} else {
// https://docs.microsoft.com/en-us/windows/win32/debug/system-error-codes
cerr := int(C.GetLastError())
if cerr == 87 {
println('SymFromAddr failure: $cerr = The parameter is incorrect)')
eprintln('SymFromAddr failure: $cerr = The parameter is incorrect)')
} else if cerr == 487 {
// probably caused because the .pdb isn't in the executable folder
println('SymFromAddr failure: $cerr = Attempt to access invalid address (Verify that you have the .pdb file in the right folder.)')
eprintln('SymFromAddr failure: $cerr = Attempt to access invalid address (Verify that you have the .pdb file in the right folder.)')
} else {
println('SymFromAddr failure: $cerr (see https://docs.microsoft.com/en-us/windows/win32/debug/system-error-codes)')
eprintln('SymFromAddr failure: $cerr (see https://docs.microsoft.com/en-us/windows/win32/debug/system-error-codes)')
}
}
}
return true
} $else {
eprintln('print_backtrace_skipping_top_frames_msvc must be called only when the compiler is msvc')
return false
}
}
fn print_backtrace_skipping_top_frames_mingw(skipframes int) bool {
eprintln('print_backtrace_skipping_top_frames_mingw is not implemented')
return false
}

View File

@@ -14,7 +14,7 @@ pub fn isnil(v voidptr) bool {
}
pub fn panic(s string) {
println('V panic: ' + s)
eprintln('V panic: ' + s)
exit(1)
}