@@ -178,6 +178,8 @@ by using any of the following commands in a terminal:
178
178
* [ Compile time types] ( #compile-time-types )
179
179
* [ Environment specific files] ( #environment-specific-files )
180
180
* [Debugger](#debugger)
181
+ * [ Call stack] ( #call-stack )
182
+ * [ Trace] ( #trace )
181
183
* [ Memory-unsafe code] ( #memory-unsafe-code )
182
184
* [ Structs with reference fields] ( #structs-with-reference-fields )
183
185
* [ sizeof and __ offsetof] ( #sizeof-and-__offsetof )
@@ -6255,6 +6257,81 @@ You can also see memory usage with `mem` or `memory` command, and
6255
6257
check if the current context is an anon function (`anon?`), a method (`method?`)
6256
6258
or a generic method (`generic?`) and clear the terminal window (`clear`).
6257
6259
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
+
6258
6335
## Memory-unsafe code
6259
6336
6260
6337
Sometimes for efficiency you may want to write low-level code that can potentially
@@ -7546,4 +7623,4 @@ Assignment Operators
7546
7623
+= -= *= /= %=
7547
7624
&= |= ^=
7548
7625
>>= <<= >>>=
7549
- ` ` `
7626
+ ` ` `
0 commit comments