Docs · Standard library
@trace
@trace reads the calls in progress: the file, line, and column of each. It
reads the same call-location table that an uncaught panic prints. Guide
08, defer, recover, and panic
introduces it.
import { trace, currentPos, currentFile } from "@trace"
import { Frame, Pos } from "@trace"Exports
| Name | Kind | Summary |
|---|---|---|
trace | function | every recorded call in progress, outermost first |
currentPos | function | the line and column of one call in progress |
currentFile | function | the file of one call in progress |
Frame | struct | one call: file, line, col |
Pos | struct | a line and col |
What a frame is
Each call in progress has one frame: the location of the call expression,
where the calling function called the next one. main itself has no frame;
the first frame is the call that main made.
fileis the file's path relative to the program's import root (the entry file's directory, or the package root), as diagnostics show it:main.vyi,lib/where.vyi.lineandcolcount from 1 and point at the start of the call: the function's name, or the receiver of a method call.
Each function takes a skip that counts frames from the innermost, starting
at the call to that function itself.
Frame and Pos
| Type | Fields |
|---|---|
Frame | pub file: [*con]u8, pub line: i32, pub col: i32 |
Pos | pub line: i32, pub col: i32 |
file is a view into the program's own data and stays valid for the whole
program.
Functions
| Function | Signature | Result |
|---|---|---|
trace | fn trace(skip: i32) [*con]Frame | the recorded frames, outermost first, without the call to trace and without the skip innermost frames below it |
currentPos | fn currentPos(skip: i32) Pos | the line and column of frame skip: currentPos(0) is the call to currentPos, currentPos(1) the call into the function that called it |
currentFile | fn currentFile(skip: i32) [*con]u8 | the file of the same frame |
trace(0)is the whole stack of the function that callstrace, and its last entry is the call into that function.- When frame
skipdoes not exist, or was not recorded,currentPosreturns line 0 and column 0, andcurrentFilereturns"". Whenskipis past every frame,tracereturns an empty view. - None of them allocates. The view
tracereturns points into the table and shows the stack as it is when you read it: read it before the function returns, and copy what you keep (ascapturefrom@errorsdoes).
import { println } from "@fmt"
import { trace, currentPos } from "@trace"
fn check(ok: bool) {
if ok { return }
con at = currentPos(1)
println($"check failed at line ${at.line}")
}
fn depth() i32 {
return trace(0).len
}
fn main() !u8 {
check(2 + 2 == 5) // prints check failed at line 15
con calls = depth()
println(calls) // prints 1
return Ok(0)
}Limits
The call-location table has a fixed size: 4,096 calls on wasm, and 2,730 on
x86-64-linux and arm64-linux. Deeper calls run normally but are not
recorded, so the table always holds the outermost calls.
| In recursion deeper than the table | Result |
|---|---|
trace(skip) | stops at the deepest recorded frame: at most the table's size |
currentPos(skip) for an unrecorded frame | line 0, column 0 |
currentFile(skip) for an unrecorded frame | "" |
See also: @errors for recording a stack inside an error, and
guide 08, defer, recover, and panic.