Debugging

Modules and applications used to debug Raku programs

Core debugging features

The trace pragma

The trace pragma causes the program to print out step-by-step which lines get executed:

use trace;
sub foo { say "hi" }
foo;
# OUTPUT: 
# 2 (/tmp/script.p6 line 2) 
# sub foo { say "hi" } 
# 5 (/tmp/script.p6 line 3) 
# foo 
# 3 (/tmp/script.p6 line 2) 
# say "hi" 
# hi

Dumper function (dd)

[1]

The Tiny Data Dumper: This function takes the input list of variables and notes them (on $*ERR) in an easy to read format, along with the name of the variable. For example, this prints to the standard error stream the variables passed as arguments:

my $a = 42;
my %hash = "a" => 1"b" => 2"c" => 3;
 
dd %hash$a;
#`( OUTPUT:
    Hash %hash = {:a(1), :b(2), :c(3)}
    Int $a = 42
)

Please note that dd will ignore named parameters. You can use a Capture or Array to force it to dump everything passed to it.

dd \((:a(1), :b(2)), :c(3));
dd [(:a(1), :b(2)), :c(3)];

If you don't specify any parameters at all, it will just print the type and name of the current subroutine / method to the standard error stream:

sub a { dd }a   # OUTPUT: «sub a()␤» 

This can be handy as a cheap trace function.

Using backtraces

The Backtrace class gets the current call stack, and can return it as a string:

my $trace = Backtrace.new;
sub inner { say ~Backtrace.new}
sub outer { inner}
outer;
 
# OUTPUT: 
# perl6 /tmp/script.p6 
#   in sub inner at /tmp/script.p6 line 2 
#   in sub outer at /tmp/script.p6 line 3 
#   in block <unit> at /tmp/script.p6 line 4

Environment variables

See Raku Environment Variables and Running rakudo from the command line for more information.

Ecosystem debugging modules

There are at least two useful debuggers and two tracers available for Rakudo (the Raku compiler). For more information on these modules, please refer to their documentation.

Historically other modules have existed and others are likely to be written in the future. Please check the Raku Modules website for more modules like these.

Debugger::UI::CommandLine

A command-line debugger frontend for Rakudo. This module installs the perl6-debug-m command-line utility, and is bundled with the Rakudo Star distributions. Please check its repository for instructions and a tutorial.

Grammar::Debugger (and Grammar::Tracer in the same distribution)

Simple tracing and debugging support for Raku grammars.

Trait::Traced

This provides the is traced trait, which automatically traces usage of features of whatever the trait is applied to. What features of whatever the trace is applied to get traced and how traces are handled are customizable. Refer to its documentation for more information on how this works.