Name
GDB-GNU Debugger
Abstract
GDB [-help] [-NX] [-q] [-Batch] [-Cd = dir] [-F] [-B BPS]
[-Tty = Dev] [-s symfile] [-e prog] [-se prog] [-C
Core] [-x cmds] [-D dir] [prog [core | procid]
Description
The purpose of a debugger (such as GDB) is to allow you to enter a program to check what the program is doing or what it is doing when the program crashes.
GDB can mainly do four main tasks (with some other auxiliary work) to help users find bugs while running the program.
O start your program and list information that may affect its running
O stop your program under certain conditions
O when the program stops, check what happened
O make corresponding adjustments to the program, so that you can try to correct an error and continue to find other errors
You can use GDB to debug programs written in C, C ++, and Modula-2. After the GNU Fortran compiler is ready, GDB will provide support for Fortran.
GDB is executed by entering GDB in the command line mode. After the startup, GDB reads the command from the terminal until you enter the gdb command quit to exit GDB. You can use the gdb Command help to obtain online help.
You can run GDB in the form of no parameter or no option, but usually one or two parameters run GDB, with the name of the executable program to be debugged as the parameter
GDB program name
You can use two parameters to run GDB. the executable program name and core file are not translated ).
GDB program name Core
You can use the process ID as the second parameter to tune a running process.
GDB program 1234
GDB will be attached to process 1234 (unless you have a file named 1234, GDB always searches for the core file first)
The following are some of the most common gdb commands:
File [filename]
Load the executable file to be debugged
Kill [filename]
Terminate the program being debugged
Break Function
Set a breakpoint in the (File file) function.
Clear
To delete a breakpoint, you must specify the code line or function name as the parameter.
Run [Arglist]
Run your program (if Arglist is specified, Arglist is used as the parameter to run the program)
BT backtrace: displays the program stack information.
Print expr
Print the expression value
Continue
Continue to run your program (after stopping, for example, after a breakpoint)
List
List part of the source code that generates the execution File
Next
One-step execution (after stopping); Skip function call
Nexti
Execute an assembly command in the source code of the next line.
Set
Set the variable value. For example, set nval = 54 saves 54 to the nval variable.
Step
One-step execution (after stopping); go to function call
Stepi
Continue to execute the Assembly command in the next line of source code of the program. If it is a function call, this command will enter the function and execute the assembly code in one step.
Watch
Enables you to monitor the value of a variable, regardless of when it is changed.
Rwatch
Specify a variable. If the variable is read, the program is suspended, and information is displayed in the debugger. Wait for the next debugging command. See rwatch and watch commands
Awatch
Specify a variable. If the variable is read or written, the program is suspended and the information is displayed in the debugger. Wait for the next DEBUG command. See rwatch and watch commands
CTRL-C
Stops executing the program at the current position. The breakpoint is in the current row.
Disable
Disable the breakpoint function. This Command requires the disabled breakpoint to be indexed in the breakpoint list as a parameter.
Display
The value of the specified expression is displayed at the place where the breakpoint is stopped. (Display variables)
Undisplay
Delete a display variable. This command requires the index in the display list as a parameter.
Enable
Allows the breakpoint function. This Command requires the allowed breakpoint to be indexed in the breakpoint list as a parameter.
Finish
Continue execution until the current function returns
Ignore
The number of times a breakpoint is set. For example, ignore 4 23 ignores the 23 running times of breakpoint 4 and interrupts the operation 24th times.
Info [name]
View Name Information
Load
Dynamically load an executable file to the debugger
Xbreak
Set a breakpoint at the exit point of the current function
Whatis
Show variable values and types
Ptype
Show variable type
Return
Forcibly return from current function
Txbreak
Set a temporary breakpoint at the exit point of the current function (only once)
Make
This allows you to re-generate executable files without exiting GDB.
Shell
So that you can execute Unix shell commands without leaving GDB
Help [name]
Displays information about the gdb command or the overall information about how to use GDB.
Quit
Exit GDB.
For more information about using GDB, see using GDB: A Guide to the GNU
Source-level debugger, by Richard M. Stallman and Roland
H. pesch. The same article can be seen when you use info.
Option
Any parameter rather than the option specifies an executable file and core file (or process ID );
The parameter of the first unassociated option flag is equivalent to the '-se' option.
And is the name of a file, it is equivalent to the '-C' option. Many options have a long format.
And short format. If you truncate a long format
Can still be recognized. (If you want to, you can use '+' instead '-'.
Record the option parameter, but we still follow the common convention in the example)
-Help
-H: lists all options with a brief description.
-Symbols = File
-S file
Read the symbol table in a file.
-Write
Enable (enable) write permissions to executable files and core files.
-Exec = File
-E File
When appropriate, the file is executed as an executable file to detect data combined with core dump.
-Se File
Read the symbol table from file and use it as an executable file.
-Core File
-C file
Run the file as the core dump.
-Command = File
-X file
Execute the gdb command from file.
-Directory = directory
-D directory
Add dicrctory to the source file search path.
-NX
-N
The command is not executed from any. gdbinit initialization file. Generally, commands in these files are executed only after all command options and parameters are processed.
-Quiet
-Q
"Quiet". Do not enter the description or copyright information. The information output is also disabled in batch mode.
-Batch
Run batch mode. After processing all the command Files specified with the '-x' option (and '. gdbi-nit '. If it is not disabled, the system exits and returns the status code 0. if the command in the command file is
If an error occurs during execution, exit and return a status code other than 0. the batch mode may be useful for running GDB as a filter, for example, downloading from another computer and running a program.
When running in batch mode, the message: Program exited normally. (no matter when a program stops running under the control of GDB, this message will be sent normally.) will not be sent.
-Cd = directory
Run GDB and use directory as its working directory to replace the current working directory.
-Fullname
-F
This option is set when Emacs enables GDB to run as a sub-process. it tells GDB that every time a stack structure (stack frame) is displayed (including every program stop), it uses a standard, recognized method.
The full name and row number of the output file. here, the accepted format looks like two '32' characters, followed by the file name, line number and character position (separated by colons and line breaks ). emacs and GDB use the two '32' characters
As a symbol to display the source code.
-B BPS
Set the speed (baud rate or bits/s). The speed used by GDB in any serial interface during remote debugging.
-Tty = Device
Use device as the standard input and output for your program running.
GDB mainly debugs C/C ++ programs. To debug a C/C ++ program, you must add the debugging information to the executable file during compilation. Use the-G parameter of the compiler (CC/GCC/g ++. For example:
[David @ David] $ gcc-G hello. C-O hello
[David @ David] $ g ++-G hello. cpp-O hello
If there is no-G, the function name and variable name of the program will not be seen, instead, they are all runtime memory addresses. After you use-g to add debugging information and compile the target code, you can use GDB to debug the code.
The following methods are used to start GDB:
1. GDB <program>
The program is the execution file, which is usually in the current directory.
2. GDB <program> Core
Debug a running program and a core file with GDB. The core file is generated after the core is dumped after the program is executed illegally.
3. GDB <program> <pid>
If the program is a service program, you can specify the process ID when the service is running. 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. The following lists some common parameters:
-Symbols <File>
-S <File>
Read the symbol table from a specified file.
-Se File
Read the symbol table information from a specified file and use it in an 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.
4.1.1 GDB command Overview
After you start GDB, you can start to debug the program by running the gdb command. The GDB command can be viewed using the Help Command, as shown below:
[David @ David] $ GDB
Gnu gdb Red Hat Linux (5.3post-0.20021129.18rh)
Copyright 2003 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-redhat-Linux-gnu ".
(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)
There are many gdb commands, and GDB divides them into many types. The HELP command only lists the command types of GDB. If you want to view the command, you can use the help <class> command. For example:
(GDB) Help data
You can also directly use help [command] to view the help of the command.
In GDB, you do not need to enter all commands when entering the command. You only need to enter the first few characters of the command. Of course, the first few characters of a command should mark a unique command. in Linux, you can press the tab key twice to complete the full name of the command, GDB will list all of them.
Example 1: Set a breakpoint when entering the function func. You can enter break func or B func directly.
(GDB) B func
Breakpoint 1 at 0x804832e: file test. C, line 5.
(GDB)
Example 2: enter B and press the tab key twice. You will see all commands starting with B.
(GDB) B
Backtrace break BT
To exit GDB, you only need to enter quit or Q for short.
4.1.2 run the Linux Shell Program in GDB
In the gdb environment, you can run the Linux shell command:
Shell <command string>
Call the Linux Shell to execute <command string>. The Linux Shell defined in the environment variable shell is used to execute <command string>. If the shell is not defined, use the Linux Standard shell:/bin/sh(use command.comor 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>.
4.1.3 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. To run the program, 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. Runtime 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
The terminal mode used by the Info terminal display 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
4.1.4 debug a running program
There are two ways to debug a running program:
● Use PS in Linux (Chapter 1 has introduced PS) to view the PID (process ID) of the running program ), then, use the gdb <program> PID format to mount the running program.
● Use GDB <program> to associate the source code with GDB, use the attach command in GDB to mount the PID of the process, and use detach to cancel the process.
4.1.5 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 the line under which the program is stopped, under what conditions, and when a signal is received, so that you can view the variables and processes at run time.
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, there are several pause Methods: breakpoint, watchpoint, catchpoint, signals, and thread stops ).
To resume the program running, run the C or continue command.
1. Set the breakpoint (breakpoint)
Use the break command to set breakpoints. There are several methods to set breakpoints:
Break <function>
Stops when you enter the specified function. In C ++, you can use the class: function or function (type, type) format to specify the function name.
Break <linenum>
Stops at the specified row number.
Break + offset
Break-offset
Stop the offset row before or after the current row number. Offiset is a natural number.
Break filename: linenum
Stop at the linenum line of the source file filename.
Break filename: Function
Stop at the entrance of the function of the source file filename.
Break * address
Stop at the memory address where the program runs.
Break
If this command does not have a parameter, it means it stops at the next command.
Break... If <condition>
Condition indicates the condition, which is stopped when the condition is set. For example, you can set break if I = 100 in the loop body to stop the program when I is 100.
When viewing breakpoints, you can use the info command as follows (Note: N indicates the breakpoint number ):
Info breakpoints [N]
Info break [N]
2. Set the watchpoint)
The observation point is generally used to observe whether the value of an expression (a variable is also an expression) has changed. If there is any change, stop the program immediately. There are several ways to set observation points:
Watch <expr>
Set an observation point for the expression (variable) expr. Once the expression value changes, stop the program immediately.
Rwatch <expr>
When the expression (variable) expr is read, stop the program.
Awatch <expr>
When the value of an expression (variable) is read or written, stop the program.
Info watchpoints
Lists all the observed points currently set.
3. Set the capturing point (catchpoint)
You can set capture points to catch up with some events when the program is running. For example, an exception occurs when a shared library (Dynamic Link Library) or C ++ is loaded. Set the capture point format:
Catch <event>
When an event occurs, stop the program. Event can be the following content:
● Throw an exception thrown by C ++ (throw is the keyword ).
● Catch an exception caught by C ++ (catch is a keyword ).
● Exec when the system calls exec (Exec is the keyword, which is currently only useful in the HP-UX ).
● Fork calls fork when the system calls fork (fork is the keyword, currently this function is only useful in the HP-UX ).
● Vfork calls the system to call vfork (vfork is the keyword, currently this function is available only in the HP-UX ).
● Load or load <libname> when loading a shared library (dynamically linked Library) (load is a keyword, which is currently only useful in HP-UX ).
● Unload or unload <libname> when detaching a shared library (dynamically linked Library) (unload is a keyword, which is currently only useful in HP-UX ).
Tcatch <event>
Set only one capture point. When the program stops, the point is automatically deleted.
4. Maintenance stop point
The above describes how to set the stop point of the program. The stop point in GDB is also the three types mentioned above. In GDB, if the predefined stop point is useless, you can use the delete, clear, disable, and enable commands for maintenance.
Clear
Clear all defined stop points.
Clear <function>
Clear <FILENAME: function>
Clear all stop points set on the function.
Clear <linenum>
Clear <FILENAME: linenum>
Clear all stop points set on the specified row.
Delete [breakpoints] [range...]
Delete the specified breakpoint. breakpoints is the breakpoint number. If no breakpoint number is specified, all breakpoints are deleted. Range indicates the range of the breakpoint number (for example, 3-7 ). The short command is D.
A better method than deleting is disable stop point. If the stop point is disable, GDB will not delete it. If you still need it, enable it, just like the recycle bin.
Disable [breakpoints] [range...]
The stop point specified by disable. breakpoints is the stop point number. If nothing is specified, it indicates all the stop points of disable. The abbreviated command is dis.
Enable [breakpoints] [range...]
The stop point specified by enable. breakpoints is the stop point number.
Enable [breakpoints] Once range...
Enable: The stop point specified by enable is automatically disable by GDB immediately after the program is stopped.
Enable [breakpoints] Delete range...
The stop point specified by enable is automatically deleted by GDB immediately after the program is stopped.
5. Stop conditional Maintenance
As mentioned earlier, when setting breakpoints, we mentioned that you can set a condition. When the condition is set, the program stops automatically. This is a very powerful function. Here, we will introduce the maintenance commands related to this condition.
Generally, you can use the if keyword to set a condition for a breakpoint, followed by its breakpoint condition. In addition, after the condition is set, you can use the condition command to modify the breakpoint conditions (only the break and watch commands support if, and the catch currently does not support if ).
Condition <bnum> <expression>
Modify the Stop Condition of breakpoint number bnum to expression.
Condition <bnum>
Clear the stop condition with the breakpoint number bnum.
There is also a special maintenance command ignore, which can specify the number of times the stop condition is ignored when the program is running.
Ignore <bnum> <count>
Indicates that the count of the Stop condition with the breakpoint number bnum is ignored.
6. Set the Run Command for the stop point
You can use the command Command provided by GDB to set the running command of the stop point. That is to say, when the running program is stopped, we can let it automatically run some other commands, which is advantageous for automated debugging.
Commands [bnum]
... Command-list...
End
Specify a command list for the breakpoint bnum. When the program is stopped by the breakpoint, GDB runs the commands in the command list in sequence.
For example:
Break Foo if x> 0
Commands
Printf "X is % d/N", X
Continue
End
The breakpoint is set in function Foo. the breakpoint condition is x> 0. If the program is disconnected, that is, once the value of X is greater than 0 in function Foo, GDB automatically prints the value of X and continues to run the program.
To clear the command sequence on the breakpoint, simply execute the commands command and directly input the end command.
7. breakpoint menu
In C ++, functions with the same name may appear multiple times (function overloading ). In this case, break <function> cannot tell GDB which function to stop. Of course, you can use break <function (type)> to tell GDB about the parameter type of the function to specify a function. Otherwise, GDB will list a breakpoint menu for the user to choose from. You only need to enter the number in the menu list. For example:
(GDB) B string: After
[0] cancel
[1] All
[2] File: String. CC; line number: 867
[3] File: String. CC; line number: 860
[4] File: String. CC; line number: 875
[5] File: String. CC; line number: 853
[6] File: String. CC; line number: 846
[7] File: String. CC; line number: 735
> 2 4 6
Breakpoint 1 at 0xb26c: file string. CC, line 867.
Breakpoint 2 at 0xb344: file string. CC, line 875.
Breakpoint 3 at 0 xafcc: file string. CC, line 846.
Multiple breakpoints were set.
Use the "delete" command to delete unwanted
Breakpoints.
(GDB)
It can be seen that GDB lists all the after overload functions. Just select the list number. 0 indicates that the breakpoint is not set. 1 indicates that all functions are set to the breakpoint.
8. Resume program running and single-step debugging
After the program is stopped, you can run the "continue" command to restore the program until the program ends or the next breakpoint arrives. You can also use the step or next command to track a single step.
Continue [ignore-count]
C [ignore-count]
FG [ignore-count]
Resume the program running until the program ends or the next breakpoint arrives. Ignore-count indicates the number of breakpoints that are ignored. The continue, C, and Fg commands all share the same meaning.
Step <count>
One-step tracking. If a function is called, it enters the function. The premise for entering the function is that the function is compiled with debug information. Similar to step in VC and other tools. You can add count or not. If you do not add count, it means to execute the Count command next to it, and then stop.
Next <count>
In the same single-step tracking, if a function is called, it will not enter the function (similar to step over in tools such as Vc ). You can add count or not. If you do not add count, it means to execute the Count command next to it, and then stop.
Set step-Mode
Set step-mode on
Enable step-mode. During a single-step tracking, the program will not stop without debugging information. This parameter is helpful for viewing the machine code.
Set step-mod off
Disable step-mode.
Finish
Run the program until the current function is complete. The stack address, return value, and parameter value returned by the function are printed.
Until or U
When tired of tracking in a loop body in a single step, this command can run the program until it exits the loop body.
Stepi or Si
Nexti or Ni
One machine command is tracked in one step. A program code may be executed by several machine commands. stepi and nexti can execute machine commands in one step. The command with the same functions is display/I $ PC. After running this command, A single-step trace displays the machine commands (assembly code) while displaying the program code ).
9. Signal (signals)
A signal is a soft interrupt and a method for processing asynchronous events.
Generally, the operating system supports many signals, especially Linux. Important applications generally process signals. Linux defines many signals. For example, SIGINT indicates the interrupt character signal, that is, CTRL + C signal, sigbus indicates the hardware fault signal, and sigchld indicates the sub-process status change signal; sigkill indicates the signal to terminate the program running. Semaphore programming is a very important technology in UNIX.
GDB has the ability to process any signal when debugging a program. It can tell GDB which signal to process; it can ask GDB to immediately stop the running program when receiving the specified signal for debugging. You can use the gdb handle command to complete this function.
Handle <signal> <keywords...>
Define a signal processing in GDB. Signal <signal> can start with or not with sig, you can define a range of signals to be processed (such as: SIGIO-SIGKILL, signal processing from sigio signal to sigkill, it includes sigio, sigiot, and sigkill signals. You can also use the keyword "all" to indicate all signals to be processed. Once the debugged program receives a signal, the running program will be immediately stopped by GDB for debugging. Its <keywords> can be one or more of the following keywords.
Nostop
When the program to be debugged receives a signal, GDB does not stop the program running, but a message is displayed indicating that the user receives the signal.
Stop
When the program to be debugged receives a signal, GDB stops the program.
Print
When the program to be debugged receives a signal, GDB displays a message.
Noprint
When the program to be debugged receives a signal, GDB does not tell the user to receive the signal.
Pass
Noignore
When
Reference: http://blog.csdn.net/samehai/archive/2007/09/27/1803521.aspx