Skip to content

Core\Debug::startTrace

Core\Debug::startTrace(string $path): voidCore\Debug::startTrace(
    string $path,
): void

Starts writing a record of every call your program makes to a file, until Core\Debug::stopTrace.

When a call returns, Core\Debug::startTrace adds one line to the file. Each line is one JSON object. callee is the name of the method or function, and status is ok, or thrown for a call that threw an error. start and end are clock readings in nanoseconds. The difference between them is how long the call took. A call that is still running when the trace stops has no line.

The configuration must allow a trace. [debug] mode must include "trace", and [capabilities.debug] trace must include the folder of the file. Permission to write files is not enough. Otherwise startTrace throws a RuntimeError. A second startTrace before the stop throws a LogicError.

With [debug] store on, nvs debug opens a web page that lists each trace file. It shows the file and line where startTrace was called. The traces started at the same line are listed together.

The examples below list the methods a checkout calls, count how often one method runs, and show where a trace may go and that only one trace runs at a time.

Parameters

  • $pathstringThe file to write. The capability debug.trace must grant this path. Permission to write files is not enough.

Return value

Returns void. Nothing. The trace goes on until stopTrace.

Errors

  • RuntimeError[debug] mode in nvs.toml does not include the name this method needs.
  • RuntimeErrorThe configuration does not grant debug.trace for this path.
  • LogicErrorA trace started by startTrace is already running.

Changelog

VersionChange
0.0.6With [debug] store = true, each profile and trace is recorded with the file and line that started it. The Debug page of the debug viewer lists them.

Examples

The examples below run with this configuration file.

nvs.toml
# The examples write a trace of their calls to a file in this folder.
# `mode` allows a program to start a trace.
[debug]
mode = ["trace"]
# The program may write a trace file in this folder, and nowhere else.
# A path in `[capabilities]` is relative to this file.
[capabilities.debug]
trace = ["."]

See which functions ran

<?nvs
// Writes a trace of the methods a small checkout calls, and prints their names.
class Checkout
{
public static function price(int $cents, int $count): int
{
return $cents * $count;
}
public static function total(array<int> $lines): int
{
int $sum = 0;
foreach ($lines as int $line) {
$sum = $sum + $line;
}
return $sum;
}
}
// The trace file goes in the folder of this program. `nvs.toml` allows a trace there.
string $file = Core\Path::thisDirectory('checkout.jsonl');
// From here on, each call adds one line to the file when it returns.
Core\Debug::startTrace($file);
int $sum = Checkout::total([Checkout::price(250, 2), Checkout::price(1200, 1)]);
Core\Debug::stopTrace();
echo 'total: ', $sum, "\n";
// Each line of the file is one JSON object. `callee` is the method that ran.
// The first line is `startTrace` itself, because it returns after the trace starts.
foreach (Core\IO::lines($file) as string $line) {
var $call = Core\Json::decode($line) as array<mixed>;
echo 'called: ', $call['callee'] as string, "\n";
}
// This deletes the trace file, so the next run starts with an empty folder.
Core\IO::remove($file);
Output
total: 1700
called: Core\Debug::startTrace
called: Checkout::price
called: Checkout::price
called: Checkout::total

Count how often a method runs

<?nvs
// Uses a trace to find a method that runs more often than you expect.
class Catalog
{
// In a real shop, this lookup reads a database, so each call is slow.
public static function priceOf(string $sku): int
{
return $sku == 'tea' ? 450 : 900;
}
// This loop looks up the price again for every single unit.
public static function invoice(array<int> $cart): int
{
int $sum = 0;
foreach ($cart as string $sku => int $count) {
for (int $i = 0; $i < $count; $i = $i + 1) {
$sum = $sum + Catalog::priceOf($sku);
}
}
return $sum;
}
}
string $file = Core\Path::thisDirectory('invoice.jsonl');
Core\Debug::startTrace($file);
int $sum = Catalog::invoice(['tea' => 3, 'honey' => 2]);
Core\Debug::stopTrace();
echo 'invoice: ', $sum, "\n";
// This counts the lines for `priceOf`. The cart has five units, so this prints `5`.
int $lookups = 0;
foreach (Core\IO::lines($file) as string $line) {
var $call = Core\Json::decode($line) as array<mixed>;
if ($call['callee'] as string == 'Catalog::priceOf') {
$lookups = $lookups + 1;
}
}
echo 'price lookups: ', $lookups, "\n";
// This deletes the trace file, so the next run starts with an empty folder.
Core\IO::remove($file);
Output
invoice: 3150
price lookups: 5

A trace only goes where it is allowed

<?nvs
// Shows that a trace file must be in a folder that `nvs.toml` allows for traces.
// A temporary folder is not in `[capabilities.debug] trace`, so this start throws a `RuntimeError`.
// A program run with `nvs run` may write files there, but that is not enough for a trace.
string $outside = Core\Path::join(Core\IO::createTemporaryDirectory(), 'calls.jsonl');
try {
Core\Debug::startTrace($outside);
echo "trace started\n";
} catch (RuntimeError $denied) {
echo "not allowed: the folder is not in [capabilities.debug] trace\n";
}
// The folder of this program is allowed, so this trace starts.
string $inside = Core\Path::thisDirectory('calls.jsonl');
Core\Debug::startTrace($inside);
echo "trace started\n";
// Only one trace can run at a time. A second start throws a `LogicError`.
try {
Core\Debug::startTrace($inside);
} catch (LogicError $running) {
echo "not allowed: a trace is already running\n";
}
Core\Debug::stopTrace();
// This deletes the trace file, so the next run starts with an empty folder.
Core\IO::remove($inside);
Output
not allowed: the folder is not in [capabilities.debug] trace
trace started
not allowed: a trace is already running