v2 / vlib / v / debug / callstack.c.v
40 lines · 36 sloc · 932 bytes · dbc6b50cda7524c70160db55fb3201ce0f776d83
Raw
1// Copyright (c) 2019-2024 Felipe Pena. All rights reserved.
2// Use of this source code is governed by an MIT license that can be found in the LICENSE file.
3@[has_globals]
4module debug
5
6// function call location trace
7@[markused]
8pub struct FnTrace {
9pub:
10 name string
11 file string
12 line i64
13}
14
15@[markused]
16__global g_callstack = []FnTrace{}
17
18// dump_callstack dumps callstack to the user
19@[markused]
20pub fn dump_callstack() {
21 bar := '-'.repeat(50).str
22 C.printf(c'Backtrace:\n')
23 C.printf(c'%s\n', bar)
24 callstack_len := g_callstack.len
25 for i := 0; i < callstack_len; i++ {
26 item := g_callstack[i]
27 C.printf(c'%s:%-4d | %s> %s\n', &char(item.file.str), item.line, ' '.repeat(i).str,
28 item.name)
29 }
30 C.printf(c'%s\n', bar)
31}
32
33// callstack retrieves the supplied stack frame based on supplied depth
34@[markused]
35pub fn callstack(depth int) ?FnTrace {
36 if depth >= g_callstack.len {
37 return none
38 }
39 return g_callstack[depth]
40}
41