≡ Menu

GDB Breakpoint Example for C – Set, View, Continue and Delete Breakpoints

Gdb is an essential tool to debug C programs.

Breakpoints are the way to tell GDB to stop or pause the program execution at certain line, or function, or address. Once the program is stopped you can examine and change the variable values, continue the program execution from that breakpoint, etc.

If you are new to GDB, you should first understand the basics of how to debug C program using GDB.

This tutorial will use the following code to describe gdb breakpoint with examples.


#include <stdio.h>

int power(int,int);

int main() {

        int i;
        printf("Program to calculate power\n");
        for (i=0;i<10;i++)
                printf("%d %d\n",i, power(2,i));
        return 0;
}

int power (int base, int n) {

        int i,p;
        p=1;
        for (i=1; i<=n; i++)
                p = p*base;
        return p;
}

# cc -g power.c

Now you have an executable which calculates the power of base 2 from 0-9.

1. How to set a breakpoint at function

# gdb
(gdb) file ./a.out
Reading symbols from /home/lakshmanan/a.out...done.

(gdb) b power
Breakpoint 1 at 0x40055d: file power.c, line 16.

(gdb) run
Starting program: /home/lakshmanan/./a.out 
Program to calculate power

Breakpoint 1, power (base=2, n=0) at power.c:17
17		p=1;

We are setting a breakpoint at function power and we run the program. The program will continue execution until the function call to power(). Once the function is encountered, the program will stop.

2. Get the information about the arguments passed to a function

To know the arguments passed to the fucntion, use ‘info args’.

(gdb) info args
base = 2
n = 0

3. Get the information about the local variables

To know the information about the local variables accessible within this function (current stack frame) use ‘info locals’.

(gdb) info locals
i = 32767
p = -7840

4. Continue the program execution until next breakpoint

To continue the program until the next breakpoint occurs, use ‘continue’.

(gdb) continue 
Continuing.
0 1

Breakpoint 1, power (base=2, n=1) at power.c:16
16		int i=0,p=1;

Once we give ‘continue’ the power function got executed and printed the result. The same power function was called again from ‘main()’ for loop, so the program got stopped once again.

5. Get information about all breakpoints

You can get to know the information regarding breakpoints using ‘info breakpoints’.

(gdb) info breakpoints 
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x00000000004005a7 in power at power.c:16
	breakpoint already hit 2 times

Now we have set only one breakpoint and the information regarding the breakpoints are displayed.

6. Delete an existing breakpoint

In order to delete an existing breakpoint use ‘delete breakpoints’

(gdb) delete breakpoints 1

(gdb) info breakpoints 
No breakpoints or watchpoints.

Now we have deleted the breakpoint that was present in the power function.

7. Set a temporary breakpoint

Sometimes you might want to stop the program at a point only one time. You can use temporary breakpoints for that. A temporary breakpoint stops the program only once, and then it will be removed.


(gdb) tbreak power
Temporary breakpoint 3 at 0x4005a7: file power.c, line 16.

(gdb) info breakpoints 
Num     Type           Disp Enb Address            What
3       breakpoint     del  y   0x00000000004005a7 in power at power.c:16

(gdb) continue 
Continuing.
1 2

Temporary breakpoint 3, power (base=2, n=2) at power.c:16
16		int i=0,p=1;

(gdb) info breakpoints 
No breakpoints or watchpoints.

Add your comment

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

Comments on this entry are closed.

  • Bob November 7, 2013, 8:59 am

    Thanks… Great tutorial