Skip to content

Commit c7752ce

Browse files
committed
v: add an -assert aborts/backtraces option to ease debugging
1 parent f0c1e55 commit c7752ce

File tree

3 files changed

+48
-6
lines changed

3 files changed

+48
-6
lines changed

cmd/v/help/build-c.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,3 +205,13 @@ see also `v help build`.
205205
Write all C flags into `file.txt`, one flag per line.
206206
If `file.txt` is `-`, then write the flags to stdout, one flag per line.
207207

208+
-assert aborts
209+
Call abort() after an assertion failure. Debuggers usually
210+
install signal handlers for SIGABRT, so your program will stop and you
211+
will get a backtrace. If you are running your program outside of a
212+
debugger, you will most likely get a core dump file.
213+
214+
-assert backtraces
215+
Call print_backtrace() after an assertion failure. Note that
216+
backtraces are not implemented yet on all combinations of
217+
platform/compiler.

vlib/v/gen/c/assert.v

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,7 @@ fn (mut g Gen) gen_assert_stmt(original_assert_statement ast.AssertStmt) {
3333
g.writeln('\tg_test_fails++;')
3434
metaname_fail := g.gen_assert_metainfo(node)
3535
g.writeln('\tmain__cb_assertion_failed(&$metaname_fail);')
36-
if 'abort_on_assert' in g.pref.compile_defines_all {
37-
g.writeln('\tabort();')
38-
}
36+
g.gen_assert_postfailure_mode(node)
3937
g.writeln('\tlongjmp(g_jump_buffer, 1);')
4038
g.writeln('\t// TODO')
4139
g.writeln('\t// Maybe print all vars in a test function if it fails?')
@@ -48,14 +46,24 @@ fn (mut g Gen) gen_assert_stmt(original_assert_statement ast.AssertStmt) {
4846
g.writeln(' {')
4947
metaname_panic := g.gen_assert_metainfo(node)
5048
g.writeln('\t__print_assert_failure(&$metaname_panic);')
51-
if 'abort_on_assert' in g.pref.compile_defines_all {
52-
g.writeln('\tabort();')
53-
}
49+
g.gen_assert_postfailure_mode(node)
5450
g.writeln('\tv_panic(_SLIT("Assertion failed..."));')
5551
g.writeln('}')
5652
}
5753
}
5854

55+
fn (mut g Gen) gen_assert_postfailure_mode(node ast.AssertStmt) {
56+
match g.pref.assert_failure_mode {
57+
.default {}
58+
.aborts {
59+
g.writeln('\tabort();')
60+
}
61+
.backtraces {
62+
g.writeln('\tprint_backtrace();')
63+
}
64+
}
65+
}
66+
5967
fn (mut g Gen) gen_assert_metainfo(node ast.AssertStmt) string {
6068
mod_path := cestring(g.file.path)
6169
fn_name := g.fn_decl.name

vlib/v/pref/pref.v

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ pub enum BuildMode {
1717
build_module
1818
}
1919

20+
pub enum AssertFailureMode {
21+
default
22+
aborts
23+
backtraces
24+
}
25+
2026
pub enum GarbageCollectionMode {
2127
no_gc
2228
boehm_full // full garbage collection mode
@@ -167,6 +173,7 @@ pub mut:
167173
is_help bool // -h, -help or --help was passed
168174
gc_mode GarbageCollectionMode = .no_gc // .no_gc, .boehm, .boehm_leak, ...
169175
is_cstrict bool // turn on more C warnings; slightly slower
176+
assert_failure_mode AssertFailureMode // whether to call abort() or print_backtrace() after an assertion failure
170177
// checker settings:
171178
checker_match_exhaustive_cutoff_limit int = 10
172179
}
@@ -197,6 +204,23 @@ pub fn parse_args(known_external_commands []string, args []string) (&Preferences
197204
res.arch = target_arch_kind
198205
res.build_options << '$arg $target_arch'
199206
}
207+
'-assert' {
208+
assert_mode := cmdline.option(current_args, '-assert', '')
209+
match assert_mode {
210+
'aborts' {
211+
res.assert_failure_mode = .aborts
212+
}
213+
'backtraces' {
214+
res.assert_failure_mode = .backtraces
215+
}
216+
else {
217+
eprintln('unknown assert mode `-gc $assert_mode`, supported modes are:`')
218+
eprintln(' `-assert aborts` .... calls abort() after assertion failure')
219+
eprintln(' `-assert backtraces` .... calls print_backtrace() after assertion failure')
220+
exit(1)
221+
}
222+
}
223+
}
200224
'-show-timings' {
201225
res.show_timings = true
202226
}

0 commit comments

Comments
 (0)