BitMagic The Complete Development Environment for the Commander X16.

Debugging Prog8

This page covers what the debugger does with your Prog8 program once it’s running, and the few things about Prog8 that show up while you debug.

Breakpoints

Click to the left of a line number to set a breakpoint. A line with no code (a blank line, a declaration without a value, a brace) moves the breakpoint to the next line that has some.

Right click a breakpoint to make it conditional:

Kind Example Stops when
Expression i == 5 the Prog8 expression is true
Hit count 3, >= 10, % 2 it’s been reached that many times
Log message i is {i}, p is {p.x} never; it writes the message to the Debug Console

A %breakpoint in your program stops as well, on the line after it. Turn that off with breakpointDirectives in Settings.

Breakpoints are set only where they’re needed, so the program runs at full speed between them.

Stepping

Key Step Does
F10 Over Runs to the next line in this subroutine. Calls run without stopping.
F11 Into Stops at the next line anywhere, so in a subroutine the line calls.
Shift+F11 Out Runs until this subroutine returns, and stops in the caller.

A loop on one line (a for with a one line body) stops on that line each time round, so you can watch it go.

Calls into the Prog8 library (txt, floats, conv and the rest) run through by default. Turn on stepIntoLibrary to step into them; the extension writes the library’s source out (with prog8c -libdump) so you see it.

An asmsub or a %asm block is one line, so stepping goes over all of it. To step its instructions, use the debugger’s disassembly view.

The call stack

Each subroutine on the stack shows the line it’s on, with its full name (main.outer.middle for a subroutine nested in another). Click a frame to see its variables.

Frames that aren’t your program, such as the library without its source or the kernal, are shown greyed out, and clicking one opens the disassembly. The last frame is the program’s entry point, where BASIC’s SYS went.

Variables

The Variables pane has these scopes for the selected frame:

Scope Shows
Locals The subroutine’s parameters and variables, and those of any subroutine it’s nested in.
Block The variables and constants of the subroutine’s block (Block main).
Other blocks Your other blocks’ variables, by their full names.
Registers PC, A, X, Y, SP, the RAM bank, and cx16.r0 to r15 as words.

Structs and arrays expand, and a pointer expands to what it points at (a pointer to a struct straight to its fields). Large arrays come in pages of 100. Right click a value and pick Set Value to change it, using any Prog8 expression. Constants can’t be changed.

Add to Watch works on anything in the pane, or type your own Prog8 expression in the Watch pane:

points[i].y * 2          ; arithmetic, which wraps at the type's size as on the X16
total as uword + 300     ; casts
ptr^^                    ; what a pointer points at
lsb(address)             ; lsb, msb, mkword, len, sizeof and abs
gUbyte > 100 and not done

A name is looked up as your code would see it: in the subroutine, then the subroutines and block around it. Anything else can be named in full, eg game.score or cx16.r0.

Stopping on a BRK

A BRK in your program stops the debugger at the Prog8 line it was on, with the call stack and variables as they were. Continue, and it goes on to the X16’s monitor as it would on the machine.

What Prog8 does that you’ll notice

  • Variables have one place. Prog8 subroutines aren’t re-entrant, so every variable lives at a fixed address. A caller’s frame shows its variables as they are now, and a subroutine that’s returned still has its last values.
  • Unused variables are left out, even without optimisation, and a variable nothing writes to becomes a constant. Those show their value and can’t be changed.
  • Block variables are set before main.start. Their starting values are put in by the startup code, so there’s nowhere to stop on their declarations.
  • A call at the end of a line returns to the next one. If the call is the last thing a line does, stepping out of it stops on the line after.
  • Lines come out of order. A when puts its branches after the else, and a while jumps back to its condition, so stepping can go back to a line it’s been on.

With optimisation Prog8 inlines small subroutines and turns a call at the end of a subroutine into a jump, so lines and stack frames go missing. The extension compiles with -noopt unless you turn optimise on.