Debug Program with GDB (c)Category: Programming tools 2003-07-21 18:36 26020 people read review (10) Favorite Report Thread delete file function signal Unix
Four, maintenance stop point
It says how to set the stop point of the program, and the stop point in GDB is the three categories mentioned above. In GDB, you can use the Delete, clear, disable, enable commands to maintain a defined stop point if you feel it is useless.
Clear
Clears all of the defined stop points.
Clear <function>
Clear <filename:function>
Clears all settings on the function's stop point.
Clear <linenum>
Clear <filename:linenum>
Clears all stops that are set on the specified line.
delete [breakpoints] [range ...]
Deletes the specified breakpoint, breakpoints the breakpoint number. If you do not specify a breakpoint number, all breakpoints are deleted. Range represents the range of breakpoint numbers (for example: 3-7). Its shorthand command is D.
A better way than delete is disable stop point, disable the stop point, GDB will not delete, when you need, enable, just like the Recycle Bin.
disable [breakpoints] [range ...]
Disable the stop point specified by the breakpoints as the stop point number. If nothing is specified, all disable stops are indicated. The shorthand command is dis.
Enable [breakpoints] [range ...]
The stop point specified by enable, breakpoints is the stop number.
Enable [breakpoints] once range ...
Enable the stop point specified once, when the program stops, the stop is automatically disable by GDB.
Enable [breakpoints] Delete range ...
Enable the stop point specified once, when the program stops, the stop is automatically deleted by GDB.
Five, stop condition maintenance
In front of the set breakpoint, we mentioned that we can set a condition, when the condition is set up, the program automatically stop, this is a very powerful function, here, I would like to specifically say that the conditions of the relevant maintenance command. Generally, to set a condition for a breakpoint, we use the IF keyword followed by its breakpoint condition. And, when the conditions are set, we can use the condition command to modify the condition of the breakpoint. (Only the break and watch command support If,catch currently does not support if)
Condition <bnum> <expression>
The stop condition that modifies the breakpoint number to Bnum is expression.
Condition <bnum>
Clears the stop condition where the breakpoint number is bnum.
There is also a special maintenance command ignore that you can specify when the program runs, ignoring the stop condition several times.
Ignore <bnum> <count>
Represents a stop condition that ignores a breakpoint number of bnum count times.
Six, set the Run command for the stop point
We can use the command command provided by GDB to set the run of the stop point. In other words, when the running program is stopped, we can let it run some other commands automatically, which is very advantageous for automated debugging. is a powerful support for GDB-based automated debugging.
commands [Bnum]
..... command-list ...
End
Write a list of commands for the breakpoint number bnum. When the program is stopped by the breakpoint, GDB runs the command in the command list in turn.
For example:
Break Foo if x>0
Commands
printf "x is%d/n", X
Continue
End
Breakpoint set in function foo, the breakpoint condition is x>0, if the program is broken, that is, once the value of x in the Foo function is greater than 0,GDB will automatically print out the value of X, and continue to run the program.
If you want to clear a sequence of commands on a breakpoint, simply execute the commands command and just hit the end of the line.
Seven, breakpoint menu
In C + +, a function with the same name may be repeated several times (function overloading), in which case break <function> cannot tell gdb which function to stop at the entrance. Of course, you can use break <function (type) > to tell gdb the argument type of the function to specify a function. Otherwise, GDB will give you a list of broken orders for you to choose the breakpoints you need. You just have to enter the number in your menu list. such as:
(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 0xafcc:file string.cc, line 846.
Multiple breakpoints were set.
Use the ' delete ' command to delete unwanted
Breakpoints.
(GDB)
As you can see, GDB lists all the overloaded functions in after, so you could select the list number. 0 means to discard the set breakpoint, and 1 to indicate that all functions have breakpoints set.
viii. Recovery program run and single-step debugging
When the program is stopped, you can use the Continue command to resume running the program until the program ends, or the next breakpoint arrives. You can also use the step or next command to track the program.
Continue [Ignore-count]
c [Ignore-count]
FG [Ignore-count]
The recovery program runs until the program finishes, or the next breakpoint arrives. Ignore-count indicates that the number of subsequent breakpoints is ignored. CONTINUE,C,FG three commands are the same meaning.
Step <count>
Single-step tracking, if there is a function call, he will enter the function. The prerequisite for entering a function is that this function is compiled with debug information. Very much like the VC and other tools in the step in. The following can be added to count or not, without adding a line to execute, plus the execution of the following count instructions, and then stop.
Next <count>
Similarly single-step tracking, if there is a function call, he will not enter the function. Very much like the VC and other tools in step over. The following can be added to count or not, without adding a line to execute, plus the execution of the following count instructions, and then stop.
Set Step-mode
Set Step-mode on
Turn on Step-mode mode, so the program does not stop because there is no debug information when you are stepping through the tracks. This parameter has a good view of the machine code.
Set Step-mod off
Turn off Step-mode mode.
Finish
Run the program until the current function finishes returning. and prints information such as the stack address and return value and parameter values when the function returns.
Until or U
When you're tired of stepping through a loop, this command can run the program until you exit the loop body.
Stepi or Si
Nexti or NI
Single-step tracking of a machine instruction. A program code may be completed by a number of machine instructions, STEPI and Nexti can step into the machine instructions. The same function as the command is "display/i $pc", when the command is finished, the single-step tracking will be typed in the program code while the machine instructions (that is, assembly code)
ix. Signal (signals)
A signal is a soft interrupt and is a method of handling asynchronous events. In general, the operating system supports many signals. In particular, UNIX, a more important application typically processes signals. UNIX defines a number of signals, such as SIGINT to interrupt the character signal, that is, the CTRL + C signal, Sigbus indicates a hardware failure signal, SIGCHLD indicates the child process status change signal, sigkill means to terminate the program running signal, and so on. Semaphore programming is a very important technology under UNIX.
GDB has the ability to handle any kind of signal when you debug a program, and you can tell GDB which signal to handle. You can ask GDB to stop the running program as soon as it receives the signal you specified for debugging. You can use GDB's handle command to accomplish this function.
Handle <signal> <keywords...>
Define a signal processing in GDB. Signal <signal> can start with the sig or do not start with the sig, you can define a range to process the signal (e.g., sigio-sigkill, the signal processing from the SIGIO signal to SIGKILL, including Sigio,sigiot, Sigkill three signals), you can also use the keyword all to indicate that you want to process all the signals. Once the program being debugged receives the signal, the running program is immediately stopped by GDB for debugging. Its <keywords> can be one or more of the following keywords.
Nostop
When the program being debugged receives a signal, GDB does not stop the program from running, but it will give you a message telling you to receive the signal.
Stop
When the program being debugged receives a signal, GDB stops your program.
Print
When the program being debugged receives a signal, GDB shows a message.
Noprint
When the program being debugged receives a signal, GDB will not tell you the message that you received the signal.
Pass
Noignore
When the program being debugged receives a signal, GDB does not process the signal. This means that GDB will give this signal to the debug program to handle.
NoPass
Ignore
When the program being debugged receives a signal, GDB does not let the debugger process the signal.
Info signals
Info handle
See what signals are being detected by GDB.
10. Threads (thread Stops)
If your program is multithreaded, you can define whether your breakpoint is on all threads, or on a particular thread. GDB is easy to help you with this work.
Break <linespec> Thread <threadno>
Break <linespec> thread <threadno> If ...
LINESPEC Specifies the line number of the source program to which the breakpoint is set. THREADNO Specifies the ID of the thread, note that this ID is assigned by GDB, and you can view the thread information in the running program through the "Info Threads" command. If you do not specify thread <threadno> it means that your breakpoint is set on all threads. You can also specify a breakpoint condition for a thread. Such as:
(gdb) Break frik.c:13 thread Bartab > Lim
When your program is stopped by GDB, all running threads will be stopped. This facilitates you to view the overall situation of the running program. And when you resume the program, all the threads will be restored to run. That is even when the main process is being debugged.
<-next page,
(All rights reserved, please specify the author and source when reproduced)