Skip to content

Commit c6cc2d2

Browse files
authored
docs: update v.debug docs - callstack+trace (#20854)
1 parent f472355 commit c6cc2d2

File tree

1 file changed

+78
-1
lines changed

1 file changed

+78
-1
lines changed

doc/docs.md

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,8 @@ by using any of the following commands in a terminal:
178178
* [Compile time types](#compile-time-types)
179179
* [Environment specific files](#environment-specific-files)
180180
* [Debugger](#debugger)
181+
* [Call stack](#call-stack)
182+
* [Trace](#trace)
181183
* [Memory-unsafe code](#memory-unsafe-code)
182184
* [Structs with reference fields](#structs-with-reference-fields)
183185
* [sizeof and __offsetof](#sizeof-and-__offsetof)
@@ -6255,6 +6257,81 @@ You can also see memory usage with `mem` or `memory` command, and
62556257
check if the current context is an anon function (`anon?`), a method (`method?`)
62566258
or a generic method (`generic?`) and clear the terminal window (`clear`).
62576259
6260+
## Call stack
6261+
6262+
You can also show the current call stack with `v.debug`.
6263+
6264+
To enable this feature, add the `-d callstack` switch when building or running
6265+
your code:
6266+
6267+
```v
6268+
import v.debug
6269+
6270+
fn test(i int) {
6271+
if i > 9 {
6272+
debug.dump_callstack()
6273+
}
6274+
}
6275+
6276+
fn do_something() {
6277+
for i := 0; i <= 10; i++ {
6278+
test(i)
6279+
}
6280+
}
6281+
6282+
fn main() {
6283+
do_something()
6284+
}
6285+
```
6286+
6287+
```
6288+
$ v -d callstack run example.v
6289+
Backtrace:
6290+
--------------------------------------------------
6291+
example.v:16 | > main.main
6292+
example.v:11 | > main.do_something
6293+
example.v:5 | > main.test
6294+
--------------------------------------------------
6295+
```
6296+
6297+
## Trace
6298+
6299+
Another feature of `v.debug` is the possibility to add hook functions
6300+
before and after each function call.
6301+
6302+
To enable this feature, add the `-d trace` switch when building or running
6303+
your code:
6304+
6305+
```v
6306+
import v.debug
6307+
6308+
fn main() {
6309+
hook1 := debug.add_before_call(fn (fn_name string) {
6310+
println('> before ${fn_name}')
6311+
})
6312+
hook2 := debug.add_after_call(fn (fn_name string) {
6313+
println('> after ${fn_name}')
6314+
})
6315+
anon := fn () {
6316+
println('call')
6317+
}
6318+
anon()
6319+
6320+
// optionally you can remove the hooks:
6321+
debug.remove_before_call(hook1)
6322+
debug.remove_after_call(hook2)
6323+
anon()
6324+
}
6325+
```
6326+
6327+
```
6328+
$ v -d trace run example.v
6329+
> before anon
6330+
call
6331+
> after anon
6332+
call
6333+
```
6334+
62586335
## Memory-unsafe code
62596336
62606337
Sometimes for efficiency you may want to write low-level code that can potentially
@@ -7546,4 +7623,4 @@ Assignment Operators
75467623
+= -= *= /= %=
75477624
&= |= ^=
75487625
>>= <<= >>>=
7549-
```
7626+
```

0 commit comments

Comments
 (0)