The-G option for 1.GCC
If you want to debug using GDB, you must include the-G option in GCC at compile time, using the parameter-G to compile the source code information into the executable file .
If you do not use the-G option:
[CPP]View Plaincopy
- #include <stdio.h>
- int main (void)
- {
- int i = 1;
- i = i + 1;
- printf ("i =%d\n", i);
- return 0;
- }
GCC main.c
GDB a.out
(GDB) B Main
Breakpoint 1 at 0x4004f8
(GDB) R
Starting program:/home/yanwenjie/ctest/a.out
Warning:no loadable sections found in added Symbol-file system-supplied DSO at 0x7ffff7ffa000
Breakpoint 1, 0x00000000004004f8 in Main ()
(GDB) n
Single stepping until exit from function Main,
which has No. line number information.
i = 2
0X00007FFFF7A3B76D in __libc_start_main () from/lib/x86_64-linux-gnu/libc.so.6
(GDB)
If you use the-G option:
Gcc-g MAIN.C
[Email protected]:~/ctest$ gdb a.out
(GDB) B main
Breakpoint 1 at 0x4004fc:file main.c, line 5.
(GDB) R
Starting program:/home/yanwenjie/ctest/a.out
Warning:no loadable sections found in added Symbol-file system-supplied DSO at 0x7ffff7ffa000
Breakpoint 1, Main () at Main.c:5
5 int i = 1;
(GDB) n
6 i = i + 1;
(GDB)
7 printf ("I =%d\n", i);
(GDB)
i = 2
8 return 0;
(GDB)
You can see the program statement information.
2.TUI (Terminal User Interface)
Beginning with version 6.1, GDB has provided a compromise between text-based interaction and graphical interaction in a pattern called Tui. The command is:
Gdbtui a.out
As shown above, the corresponding code statement is displayed, below the usual GDB command pattern. The other operations are consistent with the command line GDB.
Plus a # b main
3.Move Call stack up/down//not familiarDuring a function call, the run information associated with the call is stored in the memory area of the stack frame. The frame contains the value of the local variable of the function, its formal parameters, and the positional record that called the function.each time a function call occurs, a new frame is created and pushed to a system-maintained stack, and the topmost frame of the stack represents the function being executed, when the function exits, the frame is popped and released. In GdB, you can view previous frames with the following command:
[Plain]View Plaincopy
- Frame 1
When you execute the GDB frame command, the currentThe frame of the function being executed is numbered 0 and its parent frame is numbered 1, the parent frames of the parent frame are numbered 2, and so on. The GDB up command takes you to the next parent frame in the call frame, and down leads to the opposite direction. Such an operation is useful because the values of local variables in the stack frame are based on the previous part. GDB'sThe backtrace command displays the entire stack, which is the collection of all frames that currently exist. MAIN.C:
[CPP]View Plaincopy
- #include <stdio.h>
-
- void display (int i)
- {
- printf (
- }
- &NBSP;&NBSP;
- int main (void)
- {&NBSP;&NBSP;
- int i = 1;
- i = i + 1;
- display (i);
- return 0;
- }
GDB debugging of the above program. (GDB) b display
Breakpoint 1 at 0x4004ff:file main.c, line 5.
(GDB) frame
No stack.
(GDB) R
Starting program:/home/yanwenjie/ctest/a.out
Warning:no loadable sections found in added Symbol-file system-supplied DSO at 0x7ffff7ffa000
Breakpoint 1, display (i=2) at Main.c:5
5 printf ("I =%d\n", i);
(GDB) frame
#0 display (i=2) at Main.c:5
5 printf ("I =%d\n", i);
(GDB) Frame 0
#0 display (i=2) at Main.c:5
5 printf ("I =%d\n", i);
(GDB) Frame 1
#1 0x0000000000400535 in Main () at Main.c:12
Display (i);(gdb) frame 0
#0 display (i=2) at Main.c:5
5 printf ("I =%d\n", i);
(GDB) up
#1 0x0000000000400535 in Main () at Main.c:12
Display (i);
(GDB) down
#0 display (i=2) at Main.c:5
5 printf ("I =%d\n", i);
(GDB) bt
#0 display (i=2) at Main.c:5
#1 0x0000000000400535 in Main () at Main.c:12 4.gdbinit
When using GDB to debug a program, sometimes you need to set multiple breakpoints, repeat some operations, and these operations are more cumbersome to write, you can use the Gdbinit script .
The following script:
[CPP]View Plaincopy
- #filename:. Gdbinit
- File a.out
- b Display
- R
There are two ways to make use of this script:
1) When you start gdb
When GDB starts, it looks for the ". Gdbinit" file in the current directory and interprets its contents as a GDB command , so if I name the script ". Gdbinit",
This will process these commands at startup.
Save the script as. Gdbinit, and in the MAIN.C same directory, execute the GDB command:
[Email protected]:~/ctest$ gdb
Breakpoint 1, display (i=2) at Main.c:5
5 printf ("I =%d\n", i);
(GDB)
2) GDB Run time
You can use source Script-file to interpret the GDB command script Script-file
Save the script as Gdbscriptand put it in the same directory as the MAIN.C:
[Email protected]:~/ctest$ gdb
(gdb) Source Gdbscript
Breakpoint 1 at 0x4004ff:file main.c, line 5.
Breakpoint 1, display (i=2) at Main.c:5
5 printf ("I =%d\n", i);
(GDB)
GDB Pre-knowledge