1. Set a breakpoint)
We use the break command to set breakpoints. There are several ways to set breakpoints on the front:
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 the break command does not have a parameter, it indicates that it stops at the next command.
Break... If <condition>
... It can be the preceding parameter. condition indicates the condition and stops when the condition is set. For example, you can set break if I = 100 in the Environment 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)
An observation point is usually used to check whether the value of an expression (a variable is also an expression) has changed. If the value changes, stop the program immediately. We have the following methods to set observation points:
Watch <expr>
Set an observation point for the expression (variable) expr. When 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 a capture point)
You can set capture points to catch up with some events when the program is running. For example, load the Shared Library (Dynamic Link Library) or C ++ exception. Set the capture point format:
Catch <event>
When an event occurs, stop the program. Event can be the following content:
1. Throw an exception thrown by C ++. (Throw is the keyword)
2. Catch an exception caught by C ++. (Catch is a keyword)
3. When exec is called by the system. (Exec is a keyword, which is currently only useful in HP-UX)
4. When fork calls fork. (Fork is a keyword, which is currently only useful in HP-UX)
5. When vfork calls vfork. (Vfork is a keyword, which is currently only useful in HP-UX)
6. load or load <libname> when loading a shared library (dynamic link library. (Load is a keyword, which is currently only useful in HP-UX)
7. When unload or unload <libname> unloads a shared library (Dynamic Link 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 you think 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. GDB does not delete the stop point after disable. When 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 condition maintenance
As mentioned earlier, when setting breakpoints, we mentioned that you can set a condition. When a condition is set, the program stops automatically. This is a very powerful function. Here, I want to talk about the maintenance commands for this condition.
Generally, we use the if keyword to set a condition for a breakpoint, followed by its breakpoint condition. In addition, after the conditions are set, we can use condition
Command to modify the breakpoint conditions. (Only the break and watch commands support if. 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, you can specify the number of times when the program runs, ignore the stop condition.
Ignore <bnum> <count>
Indicates that the count of the Stop condition with the breakpoint number bnum is ignored.
6. Set the running command for the stop point
We 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 automatic debugging. It provides powerful support for automated debugging Based on GDB.
Commands [bnum]
... Command-list...
End
For the breakpoint bnum, write a command list. 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.
If you want to clear the command sequence on the breakpoint, you only need to simply execute the commands command and directly create an end.
VII. breakpoint menu
In C ++, a function with the same name may appear multiple times (function overloading). In this case, break <function> cannot tell GDB
The entry of the function to be parked. Of course, you can use break <function (type)> to tell GDB the parameter type of the function.
To specify a function. Otherwise, GDB will list a breakpoint menu for you to choose the breakpoint you need. You only need to enter the number in your 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. You can 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
When 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>
Similarly, if a function is called, it does not enter the function. Similar to step over 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.
Set step-Mode
Set step-mode on
When the step-mode is enabled, the program does not stop without the debug information during the single-step tracking. 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 you get tired of one-step tracking in a loop body, this command can run the program until you exit the loop body.
Stepi or Si
Nexti or Ni
Track One machine command 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 function is"
Display/I $ PC ". After running this command, a single-step trace will execute machine commands (that is, assembly code) while executing program code)
9. Signal)
A signal is a soft interrupt and a method for processing asynchronous events. Generally, the operating system supports many signals. Especially for UNIX, important applications generally process signals. UNIX
Many signals are defined. For example, SIGINT indicates the interrupt character signal, that is, CTRL + C signal, sigbus indicates the hardware fault signal, and sigchld
Indicates the signal for changing the sub-process status; sigkill indicates the signal for terminating the program running, and so on. Semaphore programming is a very important technology in UNIX.
GDB can process any signal when you debug the program. You can tell GDB which signal to process. You can ask GDB to immediately stop the running program when receiving the signal you specified 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
Start with, you can define a range of signals to be processed (for example: SIGIO-SIGKILL, representing a signal from sigio signal to sigkill, which 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>
It can be one or more of the following keywords.
Nostop
When the program to be debugged receives a signal, GDB will not stop the program running, but will send a message to tell you to receive this signal.
Stop
When the program to be debugged receives a signal, GDB stops your 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 will not tell you the signal information.
Pass
Noignore
When the program to be debugged receives a signal, GDB does not process the signal. This indicates that GDB will hand over the signal to the debugged program for processing.
Nopass
Ignore
When the program to be debugged receives a signal, GDB will not let the program to be debugged process the signal.
Info Signals
Info handle
Check which signals are being detected by GDB.
10. Thread stops)
If your program is multi-threaded, you can define whether your breakpoint is on all threads or on a specific thread. GDB can easily help you complete this task.
Break <linespec> thread <threadno>
Break <linespec> thread <threadno> if...
Linespec specifies the line number of the source program where the breakpoint is set. Threadno specifies the thread ID. Note that this ID is allocated by GDB.
Info threads command to view the thread information in the running program. If you do not specify thread <threadno>
It indicates that your breakpoint is set on all threads. You can also specify breakpoint conditions for a thread. For example:
(GDB) Break Frik. C: 13 thread 28 If bartab> Lim
When your program is stopped by GDB, all running threads are stopped. This allows you to view the overall situation of the running program. When you resume the program running, all threads will be resumed. Even when the main process is being debugged in a single step.
View stack information
-----
When the program is stopped, the first thing you need to do is to check where the program stops. When your program calls a function, the function address, function parameters, and local variables in the function will be pushed into the stack. You can use the gdb command to view information in the current stack.
The following are some gdb commands for viewing the stack information of function calls:
Backtrace
BT
Print all information about the current function call stack. For example:
(GDB) BT
#0 func (n = 250) at TST. C: 6
#1 0x08048524 in main (argc = 1, argv = 0xbffff674) at TST. C: 30
#2 0x400409ed in _ libc_start_main () from/lib/libc. so.6
From the above, we can see the call stack information of the function: _ libc_start_main --> main () --> func ()
Backtrace <n>
BT <n>
N is a positive integer, indicating that only stack information of N layers on top of the stack is printed.
Backtrace <-N>
BT <-N>
-N indicates that only stack information of N layers under the stack is printed.
If you want to view the information of a certain layer, you need to switch the current stack. Generally, when the program stops, the top stack is the current stack, if you want to view the details of the layer below the stack, you must first switch the current stack.
Frame <n>
F <n>
N is an integer starting from 0 and a layer number in the stack. For example, frame 0 indicates the top of the stack, frame 1 indicates the second layer of the stack.
Up <n>
It indicates moving N layers to the top of the stack. If n is not required, it indicates moving up a layer.
Down <n>
It indicates moving N layers to the bottom of the stack. If n is not used, it indicates moving down a layer.
The above command will print the information to be moved to the stack layer. If you do not want them to output information. You can use these three commands:
Select-frame <n> corresponds to the frame command.
Up-silently <n> corresponds to the up command.
Down-silently <n> corresponds to the down command.
To view information about the current stack layer, run the following GDB command:
Frame or F
The following information is printed: the stack layer number, the current function name, function parameter value, the file and row number of the function, and the statement executed by the function.
Info Frame
Info F
This command prints more detailed information about the current stack layer, except that most of the information is the inner address of the runtime. For example, the function address, the address of the called function, the address of the called function, the current program language of the function, the address and value of the function parameter, the address of the local variable, and so on. For example:
(GDB) info F
Stack level 0, frame at 0xbffff5d4:
EIP = 0x804845d in func (TST. C: 6); saved EIP 0x8048524
Called by frame at 0xbffff60c
Source Language C.
Arglist at 0xbffff5d4, argS: N = 250
Locals at 0xbffff5d4, previous frame's SP is 0x0
Saved registers:
EBP at 0xbffff5d4, EIP at 0xbffff5d8
Info ARGs
Print the parameter name and value of the current function.
Info locals
Print all local variables and their values in the current function.
Info catch
Print the exception handling information in the current function.
View Source program
-----
1. display source code
GDB can print the source code of the program to be debugged. Of course,-G must be added during program compilation.
To compile the source program information into the execution file. Otherwise, you will not be able to see the source program. When the program stops, GDB will report the row on which the program stops. You can use list
Command to print the source code of the program. Let's take a look at the source code's GDB command.
List <linenum>
Displays the source code around the linenum line of the program.
List <function>
Displays the source program of the function named function.
List
Displays the source code behind the current row.
List-
Displays the source code before the current row.
It is usually used to print the top 5 rows and the next 5 rows of the current row. If the display function is the top 2 and the next 8 rows, the default value is 10. Of course, you can also customize the display range. You can use the following command to set the number of lines that display the source program at a time.
Set listsize <count>
Set the number of lines for displaying source code at a time.
Show listsize
View the current listsize settings.
The list command also has the following usage:
List <first>, <last>
Displays the source code from the first row to the last row.
List, <last>
Displays the source code from the current row to the last row.
List +
The source code is displayed later.
Generally, the following parameters can be followed after the list:
<Linenum> row number.
<+ Offset> the positive offset of the current row number.
<-Offset> the negative offset of the current row number.
<FILENAME: linenum> specifies the row of the file.
<Function> function name.
<FILENAME: function> the function in the file.
<* Address> the address of the statement in the memory when the program runs.
2. Search source code
Besides, GDB provides the following source code search commands:
Forward-search <Regexp>
Search <Regexp>
Search forward.
Reverse-search <Regexp>
Search all.
Here, <Regexp> is a regular expression, which also specifies the matching mode of a string. I will not talk about the regular expression here. Please check the relevant information.
3. Specify the source file path
In some cases, the execution program compiled with-G only contains the name of the source file without a path name. GDB provides commands that allow you to specify the path of the source file for GDB to search.
Directory <dirname...>
Dir <dirname...>
Add a source file path to the front of the current path. If you want to specify multiple paths, you can use ":" in UNIX, and ";" in windows.
Directory
Clear all custom source file search paths.
Show Directories
Displays the defined source file search path.