Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Your first debugging session (C++)

This page walks through a short debugging session with a PROS program. It assumes you’ve finished Installation and setup (C++).

What a debugger does

A debugger lets you pause your program while it runs on the brain, look at the values of your variables, and then unpause it or run one line at a time. Instead of adding printf calls and re-uploading, you can pause at the line you care about and inspect everything.

Some common terms used with debuggers include:

  • Breakpoint: a line or function where the debugger pauses the program.
  • Step: run one line of code, then pause again.
  • Continue: unpause the program, letting it run normally until it reaches the next breakpoint.

Example program

Suppose src/main.cpp has this driver control loop:

#include "main.h"
#include "v5gdb/v5gdb.h"

pros::Controller ctrl(pros::E_CONTROLLER_MASTER);
pros::MotorGroup left_mg({1, 2});
pros::MotorGroup right_mg({-3, -4});

void initialize() {
    v5gdb::install({.stop_motors_on_break = true});
}

void opcontrol() {
    while (true) {
        int forward = ctrl.get_analog(ANALOG_LEFT_Y);
        int turn = ctrl.get_analog(ANALOG_RIGHT_X);
        int left = forward + turn;
        int right = forward - turn;
        left_mg.move(left);
        right_mg.move(right);
        pros::delay(20);
    }
}

Upload it with pros mu, start it, then run v5gdb. The program will pause as soon as GDB connects, and you will see a (gdb) prompt.

1. Set a breakpoint

Tell GDB to pause on the line that computes left:

(gdb) break src/main.cpp:18
Breakpoint 1 at 0x...: file src/main.cpp, line 16.

You can also put a breakpoint on a whole function, e.g. break opcontrol.

2. Continue until the breakpoint

(gdb) continue
Continuing.

Thread 5 hit Breakpoint 1, opcontrol () at src/main.cpp:16
16              int left = forward + turn;

The program is now paused just before line 16 runs. Because of stop_motors_on_break, the drivetrain has stopped too.

3. Look at variables

You can view the values of each variable you have by running info locals, or do calculations with them by running print.

(gdb) info locals
forward = 127
turn = -40
left = <optimized out>
right = <optimized out>
(gdb) print forward + turn
$1 = 87

left and right haven’t been set yet because line 16 hasn’t run, so they might show as <optimized out> for you. This means the compiler didn’t keep track of them at this point. See Considerations for more information about that.

4. Step through the code

(gdb) next
17              int right = forward - turn;
(gdb) print left
$2 = 87

next runs one line of code. If the line calls a function, next runs the entire function. Use step instead to go inside the function, and finish to run until the current function returns.

To see how you got here, use backtrace, which lists the function that called this one, the function that called that one, and so on.

5. Test a competition mode

You can use v5gdb to test autonomous code without a field controller or competition switch:

(gdb) monitor comp auton
(gdb) continue

Use monitor comp real to go back to the real competition state. See Monitor Commands for more robot commands, such as monitor devices and monitor battery.

6. Finish up

  • Pressing Ctrl-C pauses the program wherever it is, even inside long loops. This is very helpful if you forgot to set a breakpoint or if your code has gotten stuck!
  • delete removes all breakpoints.
  • quit exits the debugger.

More commands

The Home page has a “cheat sheet” of commonly used GDB commands.