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 graphical interface, such as Vc, BCB, and other ide
But if you are working on a UNIX platform, you will find that the gdb debugging tool has more powerful functions than the visual debugger of VC and BCB. The so-called"
This is the truth.
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.
GDB command Overview
-------
After starting GDB, you will be brought into the debugging environment of GDB. You can use the gdb command to start debugging the program. The command of GDB can be viewed using the Help Command, as shown below:
/Home/hchen> 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) Help
List of classes of commands:
Aliases -- aliases of other commands
Breakpoints -- making program stop at certain points
Data -- examining data
Files -- specifying and examining files
Internals -- maintenance commands
Obscure -- obscure features
Running -- running the program
Stack -- examining the stack
Status -- Status inquiries
Support -- Support Facilities
Tracepoints -- tracing of program execution without stopping the program
User-Defined -- user-defined commands
Type "help" followed by a class name for a list of commands in that class.
Type "help" followed by command name for full documentation.
Command name abbreviations are allowed if unambiguous.
(GDB)
GDB has many commands, which are divided into many types by GDB. The Help Command is just an example of the command type of GDB. If you want to view the commands in the Command type, you can use help
<Class> command, such as help breakpoints, to view all the commands for setting breakpoints. You can also directly help
<Command> to view the Command help.
In GDB, when you enter a command, you do not need to execute a full command. You only need to use the first few characters of the command. Of course, the first few characters of the command should mark a unique command, in Linux, you can press the tab twice to fill in the full name of the command. If there are duplicates, GDB will give it an example.
Example 1: Set a breakpoint when entering the function func. You can enter break func or B func.
(GDB) B func
Breakpoint 1 at 0x8048458: file hello. C, line 10.
Example 2: Press B twice to press the tab key. You will see all the commands for B headers:
(GDB) B
Backtrace break BT
(GDB)
Example 3: only remember the prefix of the function, which can be as follows:
(GDB) B Make _ <press the tab key>
(Press the tab key again and you will see :)
Make_a_section_from_file make_environ
Make_abs_section make_function_type
Make_blockvector make_pointer_type
Make_cleanup make_reference_type
Make_command make_symbol_completion_list
(GDB) B Make _
GDB will show you all the functions starting with make.
Example 4: When debugging a c ++ program, the function name can be the same. For example:
(GDB) B 'bubble (m -?
Bubble (double, double) bubble (INT, INT)
(GDB) B 'bubble (
You can view all the overload functions and parameters in C ++. (Note: M -? And press the tab key twice)
To exit GDB, you only need to send quit or Q for short.
Running UNIX Shell programs in GDB
------------
In the gdb environment, you can execute the Unix shell command and use the gdb shell command to complete it:
Shell <command string>
Call the Unix shell to execute <command string>. The Unix
Shell will be used to execute <command string>. If the shell is not defined, the standard UNIX shell will be used:
/Bin/sh. (Use command.com or cmd.exe in Windows)
Another GDB command is make:
Make <Make-ARGs>
You can execute the make command in GDB to build your own program again. This command is equivalent to "shell make <Make-ARGs> ".
Run the program in GDB
--------
After GDB is started in GDB <program> mode, GDB searches for the <program> source file in the path and current directory. To check whether GDB reads the source file, run the L or LIST command to check whether GDB can list the source code.
In GDB, run the R or run command. For program running, you may need to set the following four things.
1. program running parameters.
Set ARGs can Specify runtime parameters. (For example, set ARGs 10 20 30 40 50)
Run the show ARGs command to view the set running parameters.
2. Running environment.
Path <dir> specifies the running path of the program.
Show paths to view the program running path.
Set environment varname [= value] To set environment variables. For example, set env user = hchen
Show Environment [varname] to view environment variables.
3. working directory.
CD <dir> is equivalent to the shell CD command.
PWD displays the current directory.
4. input and output of the program.
Info terminal shows the terminal mode used by your program.
Use the redirection control program output. For example, run> OUTFILE
The tty command can be a terminal device that writes input and output data. For example, tty/dev/ttyb.
Debug a running program
--------
Two methods:
1. Run PS on UNIX to view the PID (process ID) of the running program, and then mount the running program in GDB <program> PID format.
2. Associate the source code with GDB <program> and execute GDB. In GDB, use the attach command to mount the PID of the process. Use detach to cancel the attached process.
Pause/resume program running
---------
In the debugging program, it is necessary to pause the running of the program. GDB can conveniently pause the running of the program. You can set where the program stops, under what conditions, and when it receives signals. This allows you to view runtime variables and processes.
When the process is stopped by GDB, you can use info program to check whether the program is running, process number, and reason for suspension.
In GDB, we can use the following pause Methods: breakpoint, watchpoint, and catchpoint.
), Signal (signals), thread stop (thread stops ). To resume the program running, run the C or continue command.