Standard library · System and tools

debug

Tools for finding a bug: assertions, a hand-kept call stack, tracing and timing.

import debug
On this page
  1. Functions

The debug package holds a few simple tools for when the program is not doing the right thing. Assert checks a condition that should always hold. Enter and Exit build a call stack by hand, so you know where in the program you are. The timers show which part is slow.

Functions

Assert

func Assert(cond: bool, msg: str)
If the condition is false, prints the message and stops the program.
debug-Enter.salam
import debug

func child():
    debug.Enter("child")
    println debug.Depth()
    println debug.Stack()
    debug.Exit()
end

func main:
    debug.Enter("root")
    child()
    debug.Exit()
    println debug.Depth()
    debug.Assert(1 + 1 == 2, "the arithmetic holds")
    debug.AssertNotNil(debug.MemStats() as void*, "the memory statistics")
    debug.EnableTrace(true)
    println debug.TraceEnabled()
    debug.Trace("a trace message")
    debug.StartTimer("loop")
    mut total := 0
    repeat 1000 in i:
        total += i
    end
    println total, debug.StopTimer("loop") >= 0.0
    if total < 0:
        debug.Break()
    end
end
Output2 root > child 0 true [TRACE] a trace message 499500 true

AssertNotNil

func AssertNotNil(p: void*, msg: str)
If the pointer is null, stops with the message.

The example for this item is shown with the ones above: debug-Enter.salam

Enter

func Enter(frame: str)
Adds a step's name to the hand-kept stack, usually at the start of a function.

The example for this item is shown with the ones above: debug-Enter.salam

Exit

func Exit()
Takes the last step off the stack, at the end of a function.

The example for this item is shown with the ones above: debug-Enter.salam

Stack

func Stack(): str
The current stack as root > child.

The example for this item is shown with the ones above: debug-Enter.salam

Depth

func Depth(): int
The depth of the hand-kept stack.

The example for this item is shown with the ones above: debug-Enter.salam

EnableTrace

func EnableTrace(enabled: bool)
Turns trace messages on or off.

The example for this item is shown with the ones above: debug-Enter.salam

TraceEnabled

func TraceEnabled(): bool
True when tracing is on.

The example for this item is shown with the ones above: debug-Enter.salam

Trace

func Trace(msg: str)
When tracing is on, prints the message with a [TRACE] label.

The example for this item is shown with the ones above: debug-Enter.salam

StartTimer

func StartTimer(name: str)
Starts a timer under the given name.

The example for this item is shown with the ones above: debug-Enter.salam

StopTimer

func StopTimer(name: str): f64
Stops a timer and returns the time it took, in milliseconds.

The example for this item is shown with the ones above: debug-Enter.salam

Break

func Break()
When the program runs under a debugger such as gdb, it stops right here.

The example for this item is shown with the ones above: debug-Enter.salam

MemStats

func MemStats(): str
A summary of the memory statistics as text.

The example for this item is shown with the ones above: debug-Enter.salam