GDB Breakpoints and Watchpoints using awatch rwatch Examples

by Lakshmanan Ganapathy on February 10, 2014

In this tutorial we’ll cover couple of advanced GDB functions including conditional breakpoints, and watchpoints.

We’ve also used a simple C program as an example to explain these GDB debugger concepts.

GDB Conditional Breakpoints

A breakpoint, brakes everytime your program reaches a specific place. You can also specify a condition for breakpoints. A condition is a boolean expression. A conditional breakpoint will break, only if the boolean expression evaluates to TRUE.

Assume that you are calling a function in for loop, and every 500th time, the program is crashing. Conditional breakpoint comes to rescue in those situations, where you can set a condition and break the execution only if the condition matches.

#include <stdio.h>
int main() {
        int i=0;
        for (i=0;i<1000;i++) {
(gdb) b 8 if i=500
Breakpoint 2 at 0x400524: file a.c, line 8.
(gdb) run
Starting program: /home/lakshmanan/./a.out 

Breakpoint 1, main () at a.c:6
6		int i=0;
(gdb) c

Breakpoint 2, main () at a.c:8
8			printf("%d\n",i);
(gdb) p i
$1 = 500

In the above gdb session, the program break when the value of i is 500.

What is a Watchpoint?

Watchpoint are similar to breakpoints which we discussed in our previous GDB – Breakpoints article. But unlike breakpoints which are set for functions or lines of code, watchpoints are set on variables. When those variables are read/written, watchpoint will be triggered and the program execution will stop.

Please note that the variable you want to watch must be in the current scope. So make sure you set a breakpoint within the scope of the variable to watch it.

#include <stdio.h>

int main() {
        int a=10;
        int b=15;
        b = a+b;
$ cc -g watch.c -o watch

Setting Read/Write watchpoint using ‘awatch’

Use the awatch command to set a read/write watchpoint on a variable. The syntax for awatch is, ‘awatch’ followed by the variable name.

$ gdb ./watch

(gdb) break 1
Breakpoint 1 at 0x400514: file a.c, line 1.
(gdb) run
Starting program: /home/lakshmanan/Personal/./a.out 

Breakpoint 1, main () at a.c:4
4		int a=10;
(gdb) awatch b
Hardware access (read/write) watchpoint 2: b
(gdb) n
5		int b=15;
(gdb) n
Hardware access (read/write) watchpoint 2: b

Old value = 0
New value = 15
main () at a.c:7
7		b = a+b;
(gdb) n
Hardware access (read/write) watchpoint 2: b

Old value = 15
New value = 25
main () at a.c:9
9		printf("%d\n",b);

In the above example, whenever the given variable is read/written, gdb prints the old and new value and stops the execution of the program.

Setting a read watchpoint for a variable using ‘rwatch’

rwatch is the command which is used to set a read watchpoint in gdb. Once set, whenever the given variable is read, the program will stop execution. The syntax is similar to ‘awatch’.

(gdb) rwatch b
Hardware read watchpoint 2: b
(gdb) c
Hardware read watchpoint 2: b

Value = 25
0x000000000040052b in main () at a.c:9
9		printf("%d\n",b);

Similarly you can use ‘watch’ to set a write watchpoint in gdb.

Deleting a watchpoint

Watchpoints will be displayed in the breakpoints list.

So, you can use info breakpoints to list the watchpoints and you can delete/disable similar to breakpoints as shown below.

(gdb) info breakpoints 
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x0000000000400514 in main at a.c:1
	breakpoint already hit 1 time
2       hw watchpoint  keep y                      b
	breakpoint already hit 1 time
(gdb) delete 2

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

{ 8 comments… read them below or add one }

1 Vasil February 11, 2014 at 5:49 am

I read every TGS article, which come thrue email.
Thanks a lot for your job.
I live in Ukraine and have not much English practic so be patient for my mistakes
Best regards

2 Jalal Hajigholamali February 12, 2014 at 2:02 am

Thanks a lot for nice article,

i installed GNU gdb (GDB) CentOS (7.0.1-45.el5.centos)
and i could test your example


3 Pratik February 26, 2014 at 11:17 pm

Just a few days back I was playing with gdb and I was trying to control a flash based game running on my browser { chrome } . I could be able to list global variables by using
“info globals” but whenever I tried to look for locals or args it is showing me an error.
“Symbol table info not found ” or something alike.

Please let me know if I am wrong somewhere…


4 lzyerste February 27, 2014 at 11:50 pm

I think there is a mistake.
“(gdb) b 8 if i=500″ should be “(gdb) b 8 if i == 500″.

5 Vasil February 28, 2014 at 12:31 am

Hi, Pratik
To see local variable you have to stop inside scope you wish to see.
Or by step inside the function, or setup breakpoint inside that scope (inside function, for example).
Lifetime of local variable limited. It exists as long as the local function run.
I hope you ask about it

6 Pratik March 4, 2014 at 11:27 pm

Hi Vasil , First of all thanks for your response.
I cannot put a breakpoint as I am trying to debug a running flash game in my browser.
even if I am able to list all program variables that’s enough for me.

again please help me on this


7 Vasil March 5, 2014 at 3:38 am

Dear Pratik.
I don`t knew what browser you use. But all today’s browser (opera,Firefox,chrome… ) have tools to inspect element. I hope you knew about it. For example, Mozilla Firefox, click right mouse button and choose Inspect Element. There open Debugger …
it is very powerful instrument. Take time to read how to use it.
Good luck

8 Pratik March 6, 2014 at 2:41 am

Hi Vasil,
Thanks a lot for your response.
Thats exactly what I needed.

Thanks again :)

Leave a Comment

Previous post:

Next post: