GDB Breakpoints by Example
Submitted by admin on Wed, 06/29/2016 - 09:43
GDB provides various ways to set breakpoints. In this article, each breakpoint method is explained with example.
Breakpoint is method to instruct GDB to suspend execution on certain conditions.
Like when program start executing some function, and you want to stop at that point.
Or You may like to suspend execution when you reach at certain line number in source file.
Or You may like to suspend execution when function is passed specific arguments.
Once program hits certain breakpoint, it is suspended temporarily and you can inspect data/variables in program.
For scope of this article lets see various breakpoints method.
Here is breakpoint_example.c to demonstrate usage of breakpoints.
/* File: breakpoint_example.c */
#include <include.h>
int fun_sum(int a, int b)
{
return a+b;
}
int fun_mul(int a, int b)
{
return a*b;
}
int main()
{
int i, sum=0, mul=1;
for(i=1; i<=10; i++) {
sum = fun_sum(sum, i);
mul = fun_mul(mul, i);
}
printf("%d %d\n", sum, mul);
return 0;
}
List of ways to set a breakpoint
- Set a breakpoint at line of source file.
- Set a breakpoint on function
- Set a breakpoint at specific offset from current line
- Set a breakpoint on all functions matching Regular Expression.
- Set a breakpoint on instruction address
- Set a conditional breakpoint
- Set a temporary breakpoint
- Ignore breakpoint for N number of hits
- Enable/Disable breakpoint
- Enable breakpoint for one hit
- Enable breakpoint to delete when hit
- Enable breakpoint for N number of hits
- Set a breakpoint at line of source file.
Example:Syntax break filename:linenumber
(gdb) break breakpoint_example.c:17 Breakpoint 1 at 0x400555: file breakpoint_example.c, line 17. (gdb) run Starting program: /home/megamind/.O/PW/gdbtutorial.com/a.out Breakpoint 1, main () at breakpoint_example.c:17 17 for(i=1; i<=10; i++) { (gdb)
This is one of most frequently used method to set breakpoint. You can set breakpoing by specifying filename and line number as shown in syntax.
In this example, breakpoint is set on line number 17 of breakpoint_example.c file. So it stopped execution when it reached line number 17.
Additionally if you want to check list of currently set breakpoints, you can use info break command. It will display list of currently set breakpoints with other useful information.
[Go to List] - Set a breakpoint on function.
Example:Syntax break function-name
Setting a breakpoint on function, is another command which is used commonly. By setting a breakpoint on function, gdb will stop execution when it hits function.(gdb) file a.out Reading symbols from a.out...done. (gdb) break fun_sum Breakpoint 1 at 0x400530: file breakpoint_example.c, line 6. (gdb) run Starting program: /home/megamind/.O/PW/gdbtutorial.com/a.out Breakpoint 1, fun_sum (a=0, b=1) at breakpoint_example.c:6 6 return a+b; (gdb) continue Continuing. Breakpoint 1, fun_sum (a=1, b=2) at breakpoint_example.c:6 6 return a+b;
As show in example, breakpoint is set on function fun_sum. So every time it hits function fun_sum, it suspends execution.
[Go to List] - Set a breakpoint at specific offset from current line.
Example:Syntax break [+|-]offset-number
Setting a breakpoint on offset will put a breakpoint on line number with specified offset from current line. This command can be easily understood using above example.(gdb) file a.out Reading symbols from a.out...done. (gdb) break 17 Breakpoint 1 at 0x400555: file breakpoint_example.c, line 17. (gdb) run Starting program: /home/megamind/.O/PW/gdbtutorial.com/a.out Breakpoint 1, main () at breakpoint_example.c:17 17 for(i=1; i<=10; i++) { (gdb) break +2 Breakpoint 2 at 0x400570: file breakpoint_example.c, line 19. (gdb) continue Continuing. Breakpoint 2, main () at breakpoint_example.c:19 19 mul = fun_mul(mul, i);
As shown in example, first execution is stopped at line number 17. Then break +2 sets breakpoint for line number 17+2=19.
Here we have given positive offset (+2), in similar way we can give negative offset as well i.e. (-2). It will set breakpoint on (current line number - offset).
[Go to List] - Set a breakpoint on all functions matching Regular Expression.
Example:Syntax rbreak reg-expression
(gdb) file a.out Reading symbols from a.out...done. (gdb) rbreak fun* Breakpoint 1 at 0x400544: file breakpoint_example.c, line 11. int fun_mul(int, int); Breakpoint 2 at 0x400530: file breakpoint_example.c, line 6. int fun_sum(int, int); (gdb) run Starting program: /home/megamind/.O/PW/gdbtutorial.com/a.out Breakpoint 2, fun_sum (a=0, b=1) at breakpoint_example.c:6 6 return a+b; (gdb) continue Continuing. Breakpoint 1, fun_mul (a=1, b=1) at breakpoint_example.c:11 11 return a*b; (gdb)
Setting a breakpoint using regular expression, that is one of cool feature provided in gdb. This is useful when we want to set breakpoint on multiple functions and their names are having some pattern which can be specified via regular expression.
In above example, rbeak fun* sets breakpoint on all function starting with fun. It sets breakpoint for fun_mul and fun_sum.
[Go to List] - Set a breakpoint on instruction address.
Example:Syntax break *instruction-address
(gdb) file a.out Reading symbols from a.out...done. (gdb) print fun_sum $1 = {int (int, int)} 0x400526 <fun_sum> (gdb) break *0x400526 Breakpoint 1 at 0x400526: file breakpoint_example.c, line 5. (gdb) run Starting program: /home/megamind/.O/PW/gdbtutorial.com/a.out Breakpoint 1, fun_sum (a=0, b=0) at breakpoint_example.c:5 5 { (gdb) continue Continuing. Breakpoint 1, fun_sum (a=0, b=1) at breakpoint_example.c:5 5 { (gdb)
GDB lets you set breakpoint by specifying instruction address. This is useful when you don't have debug symbol information, but still you want to debug code.
In this example, we printed address of function fun_sum, which is 0x400526. This is the address of first instruction of function fun_sum.
break *0x400526 sets breakpoint on instruction address 0x400526. Which is equivalent to setting breakpoint on function fun_sum for this example. But in same way, we can set breakpoint on any instruction address provided you know address on which you want to set breakpoint.
[Go to List] - Set a conditional breakpoint.
Example:Syntax break breakpoint condition
(gdb) file a.out Reading symbols from a.out...done. (gdb) break fun_sum if b==2 Breakpoint 1 at 0x400530: file breakpoint_example.c, line 6. (gdb) run Starting program: /home/megamind/.O/PW/gdbtutorial.com/a.out Breakpoint 1, fun_sum (a=1, b=2) at breakpoint_example.c:6 6 return a+b; (gdb) continue Continuing. 55 3628800 [Inferior 1 (process 20317) exited normally]
Conditional breakpoint is normal breakpoint along with some condition. When you want to hit breakpoint only on certain condition then you can specify condition as shown in syntax and example.
In this example, execution stopped at fun_sum only once and it is when value of argument variable b is 2. This is because we put condition if b==2 on breakpoint. So gdb stops execution only when condition is true.
[Go to List] - Set a temporary breakpoint.
Example:Syntax tbreak breakpoint
(gdb) file a.out Reading symbols from a.out...done. (gdb) tbreak fun_sum Temporary breakpoint 1 at 0x400530: file breakpoint_example.c, line 6. (gdb) run Starting program: /home/megamind/.O/PW/gdbtutorial.com/a.out Temporary breakpoint 1, fun_sum (a=0, b=1) at breakpoint_example.c:6 6 return a+b; (gdb) continue Continuing. 55 3628800 [Inferior 1 (process 20601) exited normally]
Temporary breakpoint is such breakpoint which will hit only one time and then it will be deleted automatically. tbreak command is used to set temporary breakpoint in gdb.
As shown in above example, tbreak fun_sum sets temporary breakpoint on fun_sum. This tells gdb to delete this breakpoint once it is hit. So temporary breakpoints hits only once as seen in this example.
[Go to List] - Ignore breakpoint for N number of hits.
Example:Syntax ignore breakpoint-number COUNT
(gdb) file a.out Reading symbols from a.out...done. (gdb) break fun_sum Breakpoint 1 at 0x400530: file breakpoint_example.c, line 6. (gdb) ignore 1 8 Will ignore next 8 crossings of breakpoint 1. (gdb) info break Num Type Disp Enb Address What 1 breakpoint keep y 0x0000000000400530 in fun_sum at breakpoint_example.c:6 ignore next 8 hits (gdb) run Starting program: /home/megamind/.O/PW/gdbtutorial.com/a.out Breakpoint 1, fun_sum (a=36, b=9) at breakpoint_example.c:6 6 return a+b; (gdb) continue Continuing. Breakpoint 1, fun_sum (a=45, b=10) at breakpoint_example.c:6 6 return a+b; (gdb) continue Continuing. 55 3628800 [Inferior 1 (process 20870) exited normally] (gdb) info break Num Type Disp Enb Address What 1 breakpoint keep y 0x0000000000400530 in fun_sum at breakpoint_example.c:6 breakpoint already hit 10 times (gdb)
Ignoring a breakpoint is one of good feature provided in gdb. When breakpoint is marked as ignore, even if breakpoint is hit, gdb won't stop execution and continues until hit count reaches till ignore count of breakpoint.
In this example, we set breakpoint on fun_sum. Then ignore 1 8 tells gdb to ignore next 8 hits of breakpoint no 1. So even if this breakpoint is hit, gdb won't stop execution for next 8 hits.
This behavior is highlighted in example. During program exection fun_sum was hit 10 times, but gdb stopped execution only 2 times.
[Go to List] - Enable/Disable breakpoint.
Example:Syntax enable breakpoint-number
disable breakpoint-number
When certain breakpoint is disabled, gdb keeps that breakpoint in its breakpoint list, but it has not effect on program execution. And you can re-enable breakpoint later upon when required. Disabling/Enabling breakpoints comes very handy when you want to hit breakpoint within certain scope of your program.(gdb) file a.out Reading symbols from a.out...done. (gdb) break fun_sum Breakpoint 1 at 0x400530: file breakpoint_example.c, line 6. (gdb) break fun_mul Breakpoint 2 at 0x400544: file breakpoint_example.c, line 11. (gdb) run Starting program: /home/megamind/.O/PW/gdbtutorial.com/a.out Breakpoint 1, fun_sum (a=0, b=1) at breakpoint_example.c:6 6 return a+b; (gdb) disable 1 (gdb) continue Continuing. Breakpoint 2, fun_mul (a=1, b=1) at breakpoint_example.c:11 11 return a*b; (gdb) continue Continuing. Breakpoint 2, fun_mul (a=1, b=2) at breakpoint_example.c:11 11 return a*b; (gdb) enable 1 (gdb) continue Continuing. Breakpoint 1, fun_sum (a=3, b=3) at breakpoint_example.c:6 6 return a+b;
In the example above, we set two breakpoints on fun_sum and fun_mul in the begining. When started program, it hit breakpoint 1 i.e. fun_sum. Then breakpoint 1 is disabled by command disable 1. On continuing program, we can see it hits only breakpoint 2 i.e. fun_mul. Then breakpoint is enabled by command enable 1. On continuing program, now it hit breakpoint 1. This is because we enabled it again.
[Go to List] - Enable breakpoint for one hit.
Example:Syntax enable once breakpoint-number
This is a vairant of enabling a breakpoint. As name suggests it will enable breakpoint for once only, i.e. for one hit. Once a breakpoint is hit, it will be disabled automatically.(gdb) break fun_sum Breakpoint 1 at 0x400530: file breakpoint_example.c, line 6. (gdb) break fun_mul Breakpoint 2 at 0x400544: file breakpoint_example.c, line 11. (gdb) run Starting program: /home/megamind/.O/PW/gdbtutorial.com/a.out Breakpoint 1, fun_sum (a=0, b=1) at breakpoint_example.c:6 6 return a+b; (gdb) disable 1 (gdb) continue Continuing. Breakpoint 2, fun_mul (a=1, b=1) at breakpoint_example.c:11 11 return a*b; (gdb) enable once 1 (gdb) continue Continuing. Breakpoint 1, fun_sum (a=1, b=2) at breakpoint_example.c:6 6 return a+b; (gdb) Continuing. Breakpoint 2, fun_mul (a=1, b=2) at breakpoint_example.c:11 11 return a*b; (gdb)
In the example above, breakpoint 1 is disabled by disable 1 command. Then we enabled it using enable once 1 command. On continuing further two times, we can see that fun_sum hit only one time and then it gets disabled automatically.
[Go to List] - Enable breakpoint to delete when hit.
Example:Syntax enable delete breakpoint-number
This is another variant of enabling breakpoints. As name suggest "enable delete", it will enable breakpoint and will be deleted when hit next time. In other words, this command converts normal breakpoint to temporary breakpoint.(gdb) file a.out Reading symbols from a.out...done. (gdb) break fun_sum Breakpoint 1 at 0x400530: file breakpoint_example.c, line 6. (gdb) disable 1 (gdb) info break Num Type Disp Enb Address What 1 breakpoint keep n 0x0000000000400530 in fun_sum at breakpoint_example.c:6 (gdb) enable delete 1 (gdb) info break Num Type Disp Enb Address What 1 breakpoint del y 0x0000000000400530 in fun_sum at breakpoint_example.c:6 (gdb) run Starting program: /home/megamind/.O/PW/gdbtutorial.com/a.out Temporary breakpoint 1, fun_sum (a=0, b=1) at breakpoint_example.c:6 6 return a+b; (gdb) info break No breakpoints or watchpoints. (gdb) continue Continuing. 55 3628800 [Inferior 1 (process 4518) exited normally]
Above example demonstrates behavior of this command. First we disable beakpoint 1 i.e. fun_sum. And then we enable with delete using command enable delete 1. On running program, when breakpoint is hit, it mentions Temporary breakpoint 1. And when listed for current breakpoints using info break, it shows no breakpoints. So beakpoint 1 got deleted automatically. And when continued further, program runs till its normal exit.
[Go to List] - Enable breakpoint for N number of hits.
Example:Syntax enable count COUNT breakpoint-number
This variant of enabling breakpoint gives us liberty to enable a breakpoint for certain number of hits only. And after certain number of hits, breakpoint will be disabled automatically. There would be rare situtation where you will need this command.(gdb) file a.out Reading symbols from a.out...done. (gdb) break fun_sum Breakpoint 1 at 0x400530: file breakpoint_example.c, line 6. (gdb) disable 1 (gdb) enable count 2 1 (gdb) info break Num Type Disp Enb Address What 1 breakpoint dis y 0x0000000000400530 in fun_sum at breakpoint_example.c:6 disable after next 2 hits (gdb) run Starting program: /home/megamind/.O/PW/gdbtutorial.com/a.out Breakpoint 1, fun_sum (a=0, b=1) at breakpoint_example.c:6 6 return a+b; (gdb) continue Continuing. Breakpoint 1, fun_sum (a=1, b=2) at breakpoint_example.c:6 6 return a+b; (gdb) continue Continuing. 55 3628800 [Inferior 1 (process 4602) exited normally]
In above example, we set breakpoint on fun_sum i.e. breakpoint 1. Then we disable it and enable using command enable count 2 1. When we list breapoint information, it mentions that breakpoint will be disabled after next 2 hits. To check that we run program and we can see breakpoint 1 hits for 2 times and then program finishes with normal execution.
[Go to List]
Comments
john
Fri, 07/08/2016 - 09:11
Permalink
Thanks for such nice article.