The interactive loop and the debugger

The interactive loop and the debugger

potion and p2 have two interactive modes that share one set of : commands:

Both need a terminal. The line editor keeps its history in history.txt in the current directory.

Starting them

command mode language of the lines
potion loop potion
potion -d loop potion
potion -d file.pn debugger potion
p2 usage, no loop  
p2 -d loop perl5 (p5)
p2 -d file.pl debugger perl5, or p6 for .p6/.raku

The subtle difference: potion without a program starts the loop by itself, p2 only with -d, as perl -d is the debugger entry in perl. p2 without arguments prints its usage. A program that is piped in (p2 < file, p2 -) is a program, not a loop. Use a file to debug it.

-d on a program turns on the instrumented VM (no JIT) and stops before every line. Inside the loops -d is switched off again after startup: the loop is the interactive front end, and the debugger would otherwise read the next input line as its own command.

The loop

Type a line, get => value. q or ^D leave. The first line printed tells the commands.

  potion p2 -d
language potion, evaluated with eval perl5, evaluated like eval STRING
result => value => value, => undef for undef
error the error object is printed (Syntax error before text ...) the text of $@ is printed (boom, Internal parser error: ...)
h not a command (the help is printed at start) prints the help
after an error the loop goes on the loop goes on
variables and subs gone with the line (one program per line) kept between lines, see below
input one line continues on .. lines while a bracket or quote is open, an empty line ends it

Variables persist in the p2 -d loop (they do not in the potion loop: every line is compiled and run as its own program there). The loop sets $P2::repl and the p5 parser (syn/syntax-p5.y, p5_vn) treats the input as a session: a variable that the input does not declare with my is the global $REPL::x (@REPL::a, %REPL::h), so quotes, interpolation, heredocs, slices and local are the parser’s own, and everything that Perl does with a global works:

Limits: local is restored at the end of the input, not of its block; a nested element auto-vivifies nothing ($m{x}{y} = 1 needs $m{x} = {} first, as for my %m); <<~EOT is not supported by the p5 parser; a user sub named like a built-in or an interpreter method replaces it; $a/$b, $_, @ARGV, %ENV, @INC and the punctuation variables are the interpreter’s own; a regex with an unbalanced bracket or a lone quote keeps asking for more input (end it with an empty line).

: commands (loop and debugger)

command meaning
:subs the named subs, sorted
:classes the classes
:methods CLASS the methods of CLASS
:b N, :break N set a breakpoint at line N
:B N delete the breakpoint at line N
:bl list the breakpoints

A line starting with : that is not a command prints unknown command.

The listings show what is defined so far. A perl sub or package is registered when its definition runs, so in a script that is stopped at its first line :subs and :methods are still empty; continue (:c, :n) past the definitions first.

What the listing commands show differs between the two languages, because they look into different registries:

Breakpoints are by line number only (not per file), and are remembered for the whole process. In the loops they can be set, but nothing runs under them: they apply to a program that is debugged in the same process.

The debugger commands

When a script runs under -d, it stops before each line and prints (file:line): source followed by the prompt > .

command meaning
<enter>, :s, :step go to the next line, also into called functions
:n, :next go to the next line of this function, running the calls in between
:c, :cont continue until a breakpoint (or the end of the program)
:b N, :break N, :B N, :bl breakpoints, as above
:q leave the debugger and run the program to the end
:exit leave the debugger and exit
:h the help
:subs, :classes, :methods CLASS as in the loop
/l the locals of the stopped function, $y = 5
/u the upvals, the variables captured from an outer function
/r the registers of the frame
/v the constants (values) of the function
/p the paths (instance variables, /x) of the function
any other line evaluated as code of the language (potion, or perl5 for p2) and printed

:n steps over calls by comparing the register depth of the frame, so it also skips the lines of a recursive call to the same function. A breakpoint inside a skipped call still stops.

Differences between potion and p2

Examples

$ p2 -d
p2 -d: q or ^D quits, h shows this help, any other line is evaluated as perl5, :subs ...
>> 1+2
=> 3
>> die "boom\n"
boom
>> q
$ p2 -d script.pl
(script.pl:6):	my $a = f();
> :break 8
breakpoint at line 8
> :n
> :cont
a=2
breakpoint at line 8
(script.pl:8):	my $b = 2;
> /l
l0 $n = 10
...

Implementation

The : commands are one C function, potion_debug_cmd in core/vm.c. The debugger itself is potion_debug in the same file, called by the VM for every OP_DEBUG instruction (the compiler emits one per new source line). /r /l /u /v /p read the frame layout of the VM, upvals | locals | self | registers. The loops are in front/potion.c and front/p2.c; the p5 registries are exported as p5subnames and p5pkgnames (lib/p5/p5oo.c).