Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
v: add callstack support on v.debug (#20680)
- Loading branch information
Showing
9 changed files
with
243 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Copyright (c) 2019-2024 Felipe Pena. All rights reserved. | ||
// Use of this source code is governed by an MIT license that can be found in the LICENSE file. | ||
@[has_globals] | ||
module debug | ||
|
||
// function call location trace | ||
@[markused] | ||
pub struct FnTrace { | ||
pub: | ||
name string | ||
file string | ||
line i64 | ||
} | ||
|
||
@[markused] | ||
__global g_callstack = []FnTrace{} | ||
|
||
// dump_callstack dumps callstack to the user | ||
@[markused] | ||
pub fn dump_callstack() { | ||
bar := '-'.repeat(50).str | ||
C.printf(c'Backtrace:\n') | ||
C.printf(c'%s\n', bar) | ||
callstack_len := g_callstack.len | ||
for i := 0; i < callstack_len; i++ { | ||
item := g_callstack[i] | ||
C.printf(c'%s:%-4d | %s> %s\n', &char(item.file.str), item.line, ' '.repeat(i).str, | ||
item.name) | ||
} | ||
C.printf(c'%s\n', bar) | ||
} | ||
|
||
// callstack retrieves the supplied stack frame based on supplied depth | ||
@[markused] | ||
pub fn callstack(depth int) ?FnTrace { | ||
if depth >= g_callstack.len { | ||
return none | ||
} | ||
return g_callstack[depth] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
121enter |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
module main | ||
|
||
import v.debug | ||
|
||
// vtest vflags: -d callstack | ||
|
||
struct Test {} | ||
|
||
fn (t Test) test_method() { | ||
fn_test4() | ||
} | ||
|
||
fn Test.test_static_method() { | ||
fn_test3() | ||
} | ||
|
||
fn fn_test2() ? { | ||
a := 1 | ||
// debug.dump_callstack() | ||
print(a) | ||
assert debug.callstack(1)?.line == 43 | ||
assert debug.callstack(0)?.line == 51 | ||
} | ||
|
||
fn fn_test3() ? { | ||
// debug.dump_callstack() | ||
assert debug.callstack(3)?.line == 8 | ||
assert debug.callstack(1)?.line == 41 | ||
assert debug.callstack(0)?.line == 51 | ||
return | ||
} | ||
|
||
fn fn_test4() ? { | ||
// debug.dump_callstack() | ||
assert debug.callstack(1)?.line == 10 | ||
} | ||
|
||
fn fn_test_anon(cb fn ()) { | ||
cb() | ||
} | ||
|
||
fn fn_test() ? { | ||
fn_test2() | ||
print('enter') | ||
fn_test3() | ||
assert debug.callstack(0)?.line == 51 | ||
} | ||
|
||
fn main() { | ||
print(12) | ||
fn_test() | ||
// debug.dump_callstack() | ||
// dump(debug.callstack(0)) | ||
mut ret := debug.callstack(0) | ||
Test{}.test_method() | ||
Test.test_static_method() | ||
fn_test_anon(fn () { | ||
// debug.dump_callstack() | ||
ret := debug.callstack(3) or { return } | ||
res := ret.name.starts_with('anon_fn') | ||
assert res | ||
}) | ||
|
||
ret = debug.callstack(0) | ||
assert ret == none | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters