1. Main program:
#include
Void Emit (INT i);
Int main () {
INT A = 0;
INT I;
For (i = 0; i <10; i ) {
Emit (i);
}
Printf ("exit");
Return 0;
}
Void Emit (INT I) {
Printf ("I IS% D / N", I);
}
2. Compile
In order to generate programs that can be commissioned using GDB, we need to add "-g" options. GCC -G Test.c -o test.out
3. Debug
a) Program loading
You can use two methods to load procedures:
i. Directly load the debugger from the command line, such as: GDB Test.out:
NickCen @ susecompaq: ~ / test> GDB Test.out
GNU GDB 5.0RH-5 Red Hat Linux 7.1
...
THIS GDB WAS Configured AS "i386-redhat-linux" ...
(GDB)
II. Load from GDB, that is, loaded with File Test.out:
NickCen @ susecompaq: ~ / test> GDB
GNU GDB 5.0RH-5 Red Hat Linux 7.1
...
THIS GDB WAS Configured AS "i386-redhat-linux".
(GDB) File Test.out
Reading Symbols from test.out ... done.
(GDB)
b) Program start
We can launch the program through the "Run (r)" command:
(GDB) R
Starting Program: /Data1/USERS/nickCen/test/test.out
I is 0
...
I is 9
Exit
Program evted Normal.
(GDB)
c) Display procedure:
We can display the source code by "List (L)" so that the breakpoint is set, and the "list" command shows 10 line source code each time. The "list" command can be used as a parameter, ie "L 10" means starting from the 10th row, display.
(GDB) L
1 #include
2
3 void emit (INT I);
4
5 int main () {
6 int A = 0;
7 INT I;
8 for (i = 0; i <10; i ) {
9 EMIT (i);
10}
(GDB)
d) Set breakpoints:
We can set breakpoints through the "Break (b)" command. The "BREAK" command can be used as a parameter using the line number, function name, or the name of the expression.
(GDB) B main
Breakpoint 3 at 0x8048416: File Test.c, Line 6.
(GDB) B 9
Breakpoint 4 at 0x8048430: File Test.c, line 9.
(GDB)
e) Display all breakpoints:
We can display all setup breakpoints through the "Info B" command.
(GDB) INFO B
Num Type Disp Enb Address What3 BreakPoint Keep Y 0X
08048416 in
Main at test.c: 6
4 BreakPoint Keep Y 0x
08048430 in
Main at test.c: 9
f) Disable / enable breakpoints:
We can disable and start breakpoints through the "Disable" and "Enable" commands. The parameters of the two commands are numbered numbers.
(GDB) INFO B
Num Type DINB Address What
3 BreakPoint Keep Y 0x
08048416 in
Main at test.c: 6
4 BreakPoint Keep Y 0x
08048430 in
Main at test.c: 9
(GDB) Disable 3
(GDB) INFO B
Num Type DINB Address What
3 BreakPoint Keep N 0x
08048416 in
Main at test.c: 6
4 BreakPoint Keep Y 0x
08048430 in
Main at test.c: 9
(GDB) ENABLE 3
(GDB) INFO B
Num Type DINB Address What
3 BreakPoint Keep Y 0x
08048416 in
Main at test.c: 6
4 BreakPoint Keep Y 0x
08048430 in
Main at test.c: 9
(GDB)
g) Delete breakpoints:
We can delete breakpoints through the "Clear" and "DELETE (D)" commands. The parameter values of "Clear" are the same as "BREAK", which can only be a function name, line number, expression, etc., and the parameter value of the "delete" command is the number of breakpoints.
(GDB) INFO B
Num Type DINB Address What
5 BreakPoint Keep Y 0x
08048416 in
Main at test.c: 6
6 BreakPoint Keep Y 0x
08048430 in
Main at test.c: 9
7 BreakPoint Keep Y 0x
08048466 in
Emit At Test.c: 16
(GDB) Clear 9
Deleded Breakpoint 6
(GDB) D 7
(GDB) INFO B
Num Type DINB Address What
5 BreakPoint Keep Y 0x
08048416 in
Main at test.c: 6
(GDB)
h) breakpoint operation:
We can perform breakpoints through "Step (s)" and "NEXT (N) command. The "Step" represents a single step, and "next" means a single step.
(GDB) N
Main () at test.c: 8
8 for (i = 0; i <10; i ) {
(GDB) N
9 EMIT (i);
(GDB) N
I is 5
8 for (i = 0; i <10; i ) {
(GDB) N
9 EMIT (i);
(GDB) S
Emit (i = 6) at Test.c: 16
16 Printf ("I IS% D / N", I);
(GDB) N
I is 6
17}
(GDB) N
Main () at test.c: 8
8 for (i = 0; i <10; i ) {
(GDB)
i) print variable value:
With the "Print (P)" command, we can print a value of a certain variable.
(GDB) P i
$ 1 = 6
(GDB)
j) Print the value in the stack:
We can print the values in the current stack by "BackTrace (BT)".
(GDB) BT
# 0 main () at Test.c: 9
# 1 0x
40054C
6F
IN __LIBC_START_MAIN () from /lib/libc.so.6
(GDB)
K) exit
We can exit the current debugging environment via the "QUIT (Q)" command.
(GDB) Q
NickCen @ SUSECMMPAQ: ~ / TEST>
l) use help
We can use the "Help" command to get help. Where "Help" parameter value is the content that needs help.
(GDB) Help Breakpoints
Making Program Stop At Certain Points.
List of commands:
AWATCH - SET A WATCHPOINT for An Expression
Break - Set Breakpoint At Specified Line Or Function
...
XBreak - Set BreakPoint At Procedure Exit
Type "Help" Followed by Command Name for Full Documentation.
Command name abbreviations are allowed if unambiguous.
(GDB)