1. What is GDB?
The GDB full name is GNU Project Debugger, and when the program executes, lets you go inside the other program to view it, or what it is doing when a program crashes.
GDB can help you do the following four main aspects to help you catch a bug:
1. Start your program and specify things that might affect your behavior (set some initial variables)
2. Suspend your program execution during special conditions (you can pause at the endpoint, or suspend the condition)
3. When the program is paused, you can see what happened at this time (query stack, variable)
4. By changing the parameters in the program, you can experiment to derive the impact of a bug, and then continue to execute (through different settings, to get different results, I understand as testing)
This debug program can be written by Ada, C, C + +, objective-c, Pascal (or other languages). This program can be executed on the same machine or on a remote machine.
2. Debug with GDB (shortcut key ctrl+d can exit GDB)
1. First of all, of course, the installation of GDB is a statement:
1 sudo apt-get install GDB
2. Next this article revolves around a C language file to start debugging
No nonsense, no stickers.
#include <stdio.h>int Add (int a,int b) { return a+b;} int main () { int i =0; int c =0; while (i<100) { c=add (i,c); printf ("C%d", c); i++; } return c;}
$ gcc-g Gdbtest.c-o mytest
Raw mytest file, then debug
$ gdb MyTest
At the end of the main, run to main, debugging, where breakpoint is the whole of break, run is the whole of R
(GDB) Break Main (GDB) r
Run to main at pause
Then you can follow it one step at a later, S (step)
(GDB) s
After one step, you can view the remaining code L (list)
(GDB) L4 return a+b;5 }6 int main () 7 {8 int i =0;9 int c =0;10 while (i<100) one {12 C=add (i,c); printf ("C%d", c);
After stepping into the function, you can view the stack
(GDB) bt
#0 Add (a=0, b=0) at Gdbtest.c:4
#1 0x0000000000400568 in Main () at Gdbtest.c:12
You can print the value of a variable using print
(GDB) Print C
$ = 1
Save Breakpoint (file name is FIG.DP)
(GDB) Save Breakpoint FIG.DP
3. One Step Closer
Breakpoints can be set by the function name, by the line number in the file, by specifying the file name, specifying the line number, or by specifying an offset to the paused position, or by addressing it.
(GDB) Break function name
(GDB) Break line number
(gdb) Break file name: line number
(gdb) Break file name: function name
(GDB) Break + offset
(GDB) Break-offset
(GDB) Break * address
If you do not specify a location, the breakpoint is below the next line in the current row
(GDB) Break one//at the lower end of 11 lines Breakpoint 2 at 0x400559:file gdbtest.c, line 11.//Endpoint 2 information (GDB) s// single Step while (i< (GDB)//Execute the statement (//continue) C , run directly, to reach the breakpoint stop continuing. Breakpoint 2, Main () at gdbtest.c:1212 C=add (i,c);(gdb) Break +2 //breakpoint under current run under two lines Breakpoint 3 at 0x40057f:file GD BTEST.C, line 14. (GDB) break-1 //Note:breakpoint 2 also set at PC 0x400559 with the next breakpoint on the previous line of the current row. Breakpoint 4 at 0x400559:file gdbtest.c, line 11. (GDB) Break * //At the address of 0x2 Breakpoint 5 at 0x2
Next, we introduce some conditional breakpoints
Conditional breakpoint (GDB) Break breakpoint If condition (GDB) Break if c>4 ///When the variable C is greater than 4, the breakpoint stops at 14 lines only when it breaks under certain conditions. For a breakpoint that already exists, you can use condition to add a condition to it. (GDB) Break breakpoint number condition and delete the trigger condition of the specified number breakpoint also use condition. (GDB) Condition breakpoint Number (GDB) Condition 2Breakpoint 2 now unconditional
Querying breakpoints
(GDB) Info break
Monitoring point
To find out where the variable is being changed, you can use the Watch command (monitor point, watchpoint).
(GDB) Watch < expressions >
< pause operation when the expression > change occurs. < expression > means constants or variables.
(GDB) Awatch < expressions >
< pause when expression > is accessed and changed.
(GDB) Rwatch < expressions >
< pause running when expression > is accessed.
(GDB) Watch I >10hardware watchpoint 4:i >10 (gdb) ccontinuing.hardware watchpoint 4:i >10old value = 0New value = 1main () at gdbtest.c:1010 while (i<100) (gdb) Print i$1 = 11
1 (GDB) Awatch i2Hardware Access (read/write) watchpoint5: I3 (GDB) C4 continuing.5Hardware Access (read/write) watchpoint5: I6 7Old value = One8New value = A9Main () at gdbtest.c:TenTen Ten while(i< -)
Delete breakpoints and watch points
Use the Delete command to delete breakpoints and watch points.
(GDB) Delete < numbering >
Show Stack Frame
The backtrace command displays the stack frame when a breakpoint is encountered and pauses execution. In addition, the BackTrace alias also has a where and info stack.
(GDB) BackTrace
displays all stack frames.
(GDB) BackTrace N
Displays only the first N stack frames.
(GDB) Backtrace-n
Only the last N stack frames are displayed.
(GDB) BackTrace full
(GDB) BackTrace full N
(GDB) BackTrace full-n
Not only display BackTrace, but also show local variables.
After displaying the stack frame, you can see where the program stops (that is, the location of the breakpoint), and the program's call path.
Display Register
Info registers can display registers.
(GDB) Info Registers
Single Step execution
Single-step execution refers to the execution of a line of code, with two components S and N, where S is a single step, can enter a function, and n is a single step, but not inside the function.
Read the saved breakpoint when debugging
GDB hello2-x FIG.DP
Remember to add the parameter-X
And then check to see if there are any breakpoints.setting parameters when debugging
1 Set args a b c 2 (GDB) r
Four. Summary
Five. Reference articles
http://blog.csdn.net/u011068702/article/details/53931648
http://blog.csdn.net/hzhsan/article/details/7554328
Debugging the GDB code under Ubuntu