Debug the program with GDB
GDB Overview
----
GDB is a powerful Unix program debugging tool released by the GNU open-source organization. Maybe you prefer the GUI, such as Vc, BCB, and other ide debugging methods. But if you are running software on a UNIX platform, you will find that the gdb debugging tool has more powerful functions than the visual debugger of VC and BCB. This is the so-called "have an inch, have a small size and have a short size.
In general, GDB helps you complete the following four functions:
1. Start your program and run it as needed according to your custom requirements.
2. The program to be debugged can be stopped at the breakpoint you specified. (The breakpoint can be a conditional expression)
3. When the program is stopped, you can check what happens in your program.
4. dynamically change the execution environment of your program.
From the above point of view, GDB is similar to a general debugging tool and basically completes these functions. However, in details, you will find that GDB is a powerful debugging tool, you may be used to graphical debugging tools, but sometimes command line debugging tools have functions that cannot be completed by graphical tools. Let's look at it one by one.
A debugging example
------
Source program: TST. c
1 # include <stdio. h>
2
3 int func (int n)
4 {
5 Int sum = 0, I;
6 For (I = 0; I <n; I ++)
7 {
8 sum + = I;
9}
10 return sum;
11}
12
13
14 main ()
15 {
16 int I;
17 long result = 0;
18 For (I = 1; I <= 100; I ++)
19 {
20 result + = I;
21}
22
23 printf ("result [1-100] = % d/N", result );
24 printf ("result [1-250] = % d/N", func (250 ));
25}
Compile and generate the execution file: (in Linux)
Hchen/test> CC-g tst. C-o TST
Debug with GDB:
Hchen/test> gdb tst <---------- start GDB
Gnu gdb 5.1.1
Copyright 2002 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
Welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "i386-suse-linux "...
(GDB) L <-------------------- l command is equivalent to list, the original code is obtained from the first line.
1 # include <stdio. h>
2
3 int func (int n)
4 {
5 Int sum = 0, I;
6 For (I = 0; I <n; I ++)
7 {
8 sum + = I;
9}
10 return sum;
(GDB) <-------------------- press enter to repeat the previous command
11}
12
13
14 main ()
15 {
16 int I;
17 long result = 0;
18 For (I = 1; I <= 100; I ++)
19 {
20 result + = I;
(GDB) break 16 <-------------------- set the breakpoint, at the source program line 16th.
Breakpoint 1 at 0x8048496: file TST. C, line 16.
(GDB) Break func <-------------------- sets the breakpoint at the entry of the function func.
Breakpoint 2 at 0x8048456: file TST. C, line 5.
(GDB) info break <-------------------- view the breakpoint information.
Num type disp ENB address what
1 breakpoint keep Y 0x08048496 in main at TST. C: 16
2 breakpoint keep Y 0x08048456 in func at TST. C: 5
(GDB) r <--------------------- run the program, short for the Run Command
Starting program:/home/hchen/test/TST
Breakpoint 1, main () at TST. C: 17 <---------- stop at the breakpoint.
17 long result = 0;
(GDB) n <--------------------- execute a single statement. The next command is short for execution.
18 For (I = 1; I <= 100; I ++)
(GDB) N
20 result + = I;
(GDB) N
18 For (I = 1; I <= 100; I ++)
(GDB) N
20 result + = I;
(GDB) C <--------------------- to continue running the program, the continue command is abbreviated.
Continuing.
Result [1-100] = 5050 <---------- program output.
Breakpoint 2, func (n = 250) at TST. C: 5
5 Int sum = 0, I;
(GDB) N
6 For (I = 1; I <= N; I ++)
(GDB) p I <--------------------- print the value of variable I. The print command is short.
$1 = 134513808
(GDB) N
8 sum + = I;
(GDB) N
6 For (I = 1; I <= N; I ++)
(GDB) P sum
$2 = 1
(GDB) N
8 sum + = I;
(GDB) p I
$3 = 2
(GDB) N
6 For (I = 1; I <= N; I ++)
(GDB) P sum
$4 = 3
(GDB) BT <--------------------- view the function stack.
#0 func (n = 250) at TST. C: 5
#1 0x080484e4 in main () at TST. C: 24
#2 0x400409ed in _ libc_start_main () from/lib/libc. so.6
(GDB) finish <--------------------- exit the function.
Run till exit from #0 func (n = 250) at TST. C: 5
0x080484e4 in main () at TST. C: 24
24 printf ("result [1-250] = % d/N", func (250 ));
Value returned is $6 = 31375
(GDB) C <--------------------- continue to run.
Continuing.
Result [1-250] = 31375 <---------- program output.
Program exited with code 027. <-------- the program exits and debugging is complete.
(GDB) q <--------------------- exit GDB.
Hchen/test>
Well, with the above perceptual knowledge, let's get to know GDB systematically.
Use GDB
----
Generally, GDB mainly debugs C/C ++ programs. To debug a C/C ++ program, we must add the debugging information to the executable file during compilation. This can be done using the-G parameter of the compiler (CC/GCC/g ++. For example:
> CC-G hello. C-O hello
> G ++-G hello. cpp-O hello
Without-G, you will not be able to see the function name and variable name of the program, instead of the runtime memory address. After you add the debugging information with-G and successfully compile the target code, let's see how to debug it with GDB.
The following methods are used to start GDB:
1. GDB <program>
Program is your execution file, which is usually in the directory of course.
2. GDB <program> Core
Debug a running program and core file with GDB. The core is the file generated after the core is dumped after the program is illegally executed.
3. GDB <program> <pid>
If your program is a service program, you can specify the process ID when the service program runs. GDB automatically attach and debug it. The program should be searched in the PATH environment variable.
When GDB is started, you can add some GDB start switches. For details about the switches, you can use GDB-help to view them. Here are some common parameters:
-Symbols <File>
-S <File>
Read the symbol table from a specified file.
-Se File
Read the symbol table information from the specified file and use it in the executable file.
-Core <File>
-C <File>
Core Dump Core File during debugging.
-Directory <directory>
-D <directory>
Add the search path of a source file. The default search path is the path defined by path in the environment variable.
Next page->
(All Rights Reserved. Please indicate the author and source when reprinting)