Tidying up breakpoints

Breakpoints pile up. This one in bonus will stop you six times before the loop is done.

Listing, disabling and deleting breakpoints

Level 9 of 13 · interactive GDB exercise · runs in your browser

Start this exercise

What you will learn

List the breakpoints that exist, switch one off temporarily, and remove one for good.

info breakpoints lists them, each with a number. disable 1 switches number 1 off; enable 1 switches it back on; delete 1 removes it for good.

The exercise

Get to line 14 without stopping in bonus again, then remove that breakpoint.

You work in a real GDB session on the program below. Breakpoints on bonus and on line 14 were set and the program was started; it is paused in the first call to bonus.

#include <stdio.h>

int scores[6] = {4, 8, 15, 16, 23, 42};

int bonus(int score) {
    return score / 4;
}

int main(void) {
    int total = 0;
    for (int i = 0; i < 6; i++) {
        total += scores[i] + bonus(scores[i]);
    }
    printf("total = %d\n", total);
    return 0;
}

main.c, compiled with gcc -g -O0 -std=c11 -Wall -fno-omit-frame-pointer -fno-stack-protector -no-pie -fno-pie for 32-bit x86 Linux.

GDB commands you will use

info breakpoints
List breakpoints and watchpoints (i b).
disable [n]
Switch breakpoint n off without removing it. It stays in the list.
enable [n]
Switch a disabled breakpoint back on.
delete [n]
Remove breakpoint or watchpoint number n for good (d 1). With no number: remove them all.
clear <where>
Remove the breakpoints at a function or line, by place instead of by number (clear bonus).
tbreak <where>
One-shot breakpoint: it deletes itself the first time it is hit.
start
Shortcut for tbreak main followed by run.
condition <n> <cond>
Add or change the condition of breakpoint n (condition 1 score > 20). No condition: remove it.
ignore <n> <count>
Skip the next <count> hits of breakpoint n.
continue
Resume until the next breakpoint or exit (c).

Common mistakes

Try it in your browser