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.

Vyi
import { trace, currentPos, currentFile } from "@trace"
import { Frame, Pos } from "@trace"

Exports

NameKindSummary
tracefunctionevery recorded call in progress, outermost first
currentPosfunctionthe line and column of one call in progress
currentFilefunctionthe file of one call in progress
Framestructone call: file, line, col
Posstructa 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.

  • file is 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.
  • line and col count 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

TypeFields
Framepub file: [*con]u8, pub line: i32, pub col: i32
Pospub line: i32, pub col: i32

file is a view into the program's own data and stays valid for the whole program.

Functions

FunctionSignatureResult
tracefn trace(skip: i32) [*con]Framethe recorded frames, outermost first, without the call to trace and without the skip innermost frames below it
currentPosfn currentPos(skip: i32) Posthe line and column of frame skip: currentPos(0) is the call to currentPos, currentPos(1) the call into the function that called it
currentFilefn currentFile(skip: i32) [*con]u8the file of the same frame
  • trace(0) is the whole stack of the function that calls trace, and its last entry is the call into that function.
  • When frame skip does not exist, or was not recorded, currentPos returns line 0 and column 0, and currentFile returns "". When skip is past every frame, trace returns an empty view.
  • None of them allocates. The view trace returns 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 (as capture from @errors does).
Vyi
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 tableResult
trace(skip)stops at the deepest recorded frame: at most the table's size
currentPos(skip) for an unrecorded frameline 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.