http://www.wuzesheng.com/?p=1327 teaches you to play the GDB (a)--kind: Start gdb start debugging
Written in the first: GDB is a UNIX-related operating system in C/D program Development is an indispensable tool, its powerful, is not comparable to other debuggers. However, in the real world, there are a lot of developers because GDB itself threshold is relatively high, and rejected the door, with such a strong missed. The author in the past two years in the C + + development work, the GDB itself has a little research, here summed up a series of "hands-on teaching you to play gdb," the article, on the one hand when it is a summary of their own experience, on the other hand is really want to be able to contact GDB developer friend to bring some help, Let more people use such powerful tools. Launched the first article today:
The first part kind: Start gdb start debugging 1. Start GdB to start debugging:
(1)gdb Program///The most commonly used by GDB launcher, start debugging the way
(2)gdb program core ///use GDB to view the core dump file, tracking the cause of the core
(3)gdb program PID ///with GDB debugging has begun to run programs, specify PID can
2. The application with command line parameters can be started in the following two ways:
(1) When you start GDB, add the –args option, and then bring the application and its command-line arguments back, in the following format:gdb –args program args
(2) Start GDB by the method described in 1, and then execute the run command, followed by the parameters
3. Exit GdB:
(1)end-of-file (ctrl+d)
(2)quit or q
4. Execute the shell command at GDB Debug Program:
(1)Shell command args(you can also execute Shell commands First, gdb exits to the current shell, executes command , then executes in shell Exit command, you can go back to gdb)
(2) make Make-args(equivalent to shell make Make-args)
5. Get help in GDB:
(1) to execute the Help command in GDB, you can get the assistance information shown in 1:
Figure 1 GDB Help Menu
As can be seen from Figure 1, the commands in GDB can be divided into eight classes: aliases (aliases), breakpoints (breakpoints), data, files, internal (internals), suppressed (obscure), run (running), stacks ( Stack), State (status), support, Trace points (tracepoints), and user customizations (user-defined).
(2) help class-name: See more helpful instructions for this type of command
(3)Help All: Lists detailed instructions for all commands
(4) help command: A detailed description of command commands is listed
(5)apropos word: Lists a detailed description of the commands related to Word
(6) complete args: Lists all commands that use the args prefix
6.info and Show:
(1)info: Information that is used to obtain and debug the application
(2)show: To get some information about GDB's own settings
Hands to teach you to play. GDB (ii)--breakpoint, Watchpoint and CatchpointAugust 22, 2010 Xiao Wuge comment read review
This is the second of a series of "hands-on-the-go" GDB, with the main content being the Breakpoints (breakpoint), the monitoring point (Watchpoint), and the snapping point (catchpoint) that are commonly used in the GDB debugger. Although the functions of these three types of point are different, they are very similar in usage. Therefore, this article will take the break breakpoint as an example, carries on the detailed introduction, about Watchpoint and Catchpoint's introduction is relatively rough, believes the reader friend if can understand breakpoint's part, then can comprehend by analogy, Learn how to use Watchpoint and Catchpoint.
1. Breakpoint: The role is to let the program execution to a specific place to stop running
A. Breakfunction: In the functionfuntionEntrance Settings Breakpoint
B. Break+Offset: The line that is currently stopped in the program is forwardOffsetSet breakpoint at line
C. Break–Offset: The line that is currently stopped in the programOffsetSet breakpoint at line
D. BreakLineNum: In the current source file,LineNumSet breakpoint at line
E. BreakFilename:linenum: In the name offilenameThe source file is the firstLineNumSet breakpoint at line
F. Break filename:function: Set breakpoint at the entry of function functions in the source file named filename
G. Break *address: Set breakpoint at address of the program
H. Break ... if Cond: ... On behalf of any of the possible arguments mentioned above, a breakpoint is set somewhere, but only if cond is true, the program stops
I. tbreak args: Sets a breakpoints that stops only once, with the same args as the break command. Such a breakpoint will be deleted by itself when it is first stopped.
K. rbreak regex: Set breakpoint at all functions that conform to regular expression regex
View information about nth breakpoints, and if n is omitted, displays information about all breakpoints
A breakpoints that sets the location in a dynamic library that is loaded after the program starts debugging
A. Set breakpoint pending auto: gdb default setting, asking the user if you want to set pending breakpoint
B. set breakpoint Pendingon: GDB is not currently recognized breakpoint automatically becomes pending breakpoint
C. set breakpoint pending off: When GDB does not recognize a breakpoint at the moment, the error is directly
D. Show Breakpoint pending: View GDB's behavior on pending breakpoint settings (auto, on, off)
- (4) Deletion of breakpoints:
A. clear: Clears all breakpoints after the next instruction in the current stack frame
B. clear function & clear filename:function: Clear functions function Breakpoints at the entrance
C. Clear linenum & Clear filename:linenum: Clear section linenum Breakpoints at the line
d. delete [breakpoints] [range...]: Delete the breakpoints in the range specified by range. Range range refers to the scope of the breakpoint serial number
- (5) Disable and enable the breakpoints:
A. disable [breakpoints] [range...]: Disables breakpoints in the range specified by range
B. enable [breakpoints] [range...]: Enables breakpoints in the range specified by range
C. Enable [breakpoints] once [range...]: Enabled only once by range Breakpoints within the specified range, and when the program stops, it is automatically set to Disabled
D. Enable [breakpoints] Delete [range...]: Enable range The specified range of breakpoints, and so on after the program stops, these breakpoints are automatically deleted
- (6) Conditions breakpoints related commands:
A. Setting conditions breakpoints can be set by break ... if cond, or by condition bnum expression, where you first set the breakpoints by the command described in (1). The condition command is then used to specify the condition of a breakpoint, which is specified by the Bnum, and the condition is specified by expression.
B. condition bnum: Conditions for the abolition of Bnum Breakpoint
C. Ignore bnum count: First bnum breakpoint Skip count and start in effect
- (7) Specifies that the program stops after a breakpoint to execute a sequence of commands:
A. Format:commands [bnum]
..... command-list ...
End
B. Purpose: Specifies that the program stops at Bnum Breakpoint, executes the command string specified by command-list , and if no bnumis specified, is valid for the last breakpoint
C. List of CANCELLATION commands: commands [bnum]
End
D. Examples:
Break Foo if x>0
Commands
Silent
printf "x is%d\n", X
Continue
End
The above example means: When x>0, stop at the Foo function, then print out the value of X and then continue running the program
2. Watchpoint: Its role is to allow the program to stop running when the value of an expression changes, to "monitor" the expression's purpose
A. watch expr: Set write Watchpoint, when the application writes expr, modifies its value, the program stops running
B. rwatch expr: Set read Watchpoint, when the application reads expression expr , the program stops running
C. awatch expr: Set read-write Watchpoint, when the application reads or writes the expression expr , the program stops running
View Watchpoints related information that is set in the currently debugged program
- (3) Watchpoints and breakpoints are very similar, there are enable/disabe/delete and other operations, using the same method as breakpoints.
3. Catchpoint: The function is to let the program in the event of a time to stop running, such as C + + in the event of an exception, loading dynamic library events
A. catch Event: When event events occur, the program stops running, where the value of the event is:
1) throw:c++ Throw exception
2) catch:c++ catch to Exception
6) Load: Load Dynamic library
7) Load libname: Load a dynamic library named Libname
8) Unload: Uninstalling the dynamic library
9) Unload Libname: unload a dynamic library named Libname
syscall [args]: Invoke system call, args can specify system call number, or system name
B. Tcatch event: Set the Catchpoint to stop only once, the Catchpoint is automatically deleted after the first entry takes effect
(2) Catchpoints and breakpoints are very similar, there are enable/disabe/delete and other operations, using the same method as breakpoints.
Teach you how to play GDB (iii)--common commandsAugust 30, 2010 Xiao Wuge comment read review
This article is a hands-on teaching you to play the third chapter of GDB, the main content is to introduce some of the most commonly used in the process of debugging GDB command, the nonsense is not many words, start today's topic.
1.attach Process-id/detach
- (1)attach process-id: In GdB state, start debugging a running process with a process ID of Process-id
- (2)detach: Stop Debugging a process is currently being debugged, paired with attach trial
2.kill
- (1) Basic function: Kill the sub-process corresponding to the application currently being debugged by GDB
- (2) If you want to not exit GDB and the currently debugging application to recompile, link, you can execute the kill child process in gdb, and so on, after compiling, linking, and then re-execute run, GDB can load new executable program start debugging
3. Multi-threaded program debugging Related:
- (1) thread threadno: Toggles the current thread to the thread specified by Threadno
- (2)Info Threads: View information about each thread of the program that GDB is currently debugging
- (3)thread apply [threadno] [all] args: The specified (or all) thread is executed by the args The specified command
4. Multi-process program debugging related (fork/vfork):
- (1) Default mode: Afterfork/vfork , GDB still debugs the parent process, not related to the child process
- (2) set follow-fork-mode mode: Sets the gdb behavior, whenmode is parent , as in the default case Whenmode is child, afterfork/vfork , GDB enters sub-process debugging, no longer related to parent process
- (3)show follow-fork-mode: View the settings of the current GDB multi-process tracking mode
5.step & Stepi
- (1)step [Count]: If countis not specified, the program continues to execute until it arrives in a different source file than the current source file, and if countis specified, Repeat the process above the row count times
- (2)Stepi [Count]: If countis not specified, proceed to the next machine instruction and stop, or if countis specified, repeat the above procedure count Times
- (3)step is a common application scenario: set a breakpoint at a line of code called by the function func , and wait for the program to stop at the breakpoint, then use the Step command to enter the function's implementation, But the premise is that when the function compiles, the debug information is compiled, and step skips the function.
6.next & Nexti
- (1)next [Count]: If you do not specify count, step to the next line of the program, or if countis specified, step to the next count Line Program
- (2)Nexti [Count]: If countis not specified, step to the next instruction, or if countis specified, the audio performs the next Count Bar Execution
- (3) The difference betweenStepi and nexti :nexti When executing a machine instruction, if the instruction is a function call, then the program execution will not stop until the function call ends.
7.continue [Ignore-count] wakes up the program, continues running until it encounters the next breakpoint, or the program ends. If you specify ignore-count, the program ignores ignore-count breakpoints in the next run.
8.finish & Return
- (1)finish: Continue executing the program until the currently called function ends, and if the function has a return value, print the return value to the console
- (2)return [expression]: aborts the invocation of the current function, and if expressionis specified, the Expresson value is treated as the return value of the current function; Directly ends the current function call
9. Processing of signals
- (1)info signals & info handle: Print all the signal-related information, and the GDB default processing method:
- (2)handle signal Action: Sets the way GDB handles a particular signal. signal can be a signal integer value, or it can be a symbol such as SIGSEGV. The action values are:
A. stop and nostop: nostop indicates that when GDB receives the specified signal, the execution of the Stop program is not applied and only a message is printed that receives the signal, so thenostop The following printis also implied; The stop indicates that when GDB receives the specified signal, the execution of the application is stopped.
B. print and noprint: Print indicates that a message is printed if a specified signal is received; noprint and print express the opposite meaning
C. pass and nopass:Pass indicates that if a given signal is received, the signal is communicated to the application; NoPass means the opposite of pass .
D. ignore and Noignore: ignore and nopass synonymous, the same,Noignore and Pass synonymous
I'll teach you to play. GDB (iv)--– function call stack (calling stack)September 23, 2010 Xiao Wuge comment read review
This article is the fourth of the GDB series, and interested friends can read the first three articles of this series. The main content of this article is how to use GDB to see the function call stack (called stack) information, through the introduction of a number of related commands and their usage, so that readers can gradually understand the various aspects of the call stack, better control the program. Start today's content below.
We know, usually a program run, is nothing more than a function call b,b function call C and so on, and so on, and so on all the calls are completed, the entire program run is OK. In this process, whenever there is a new function call, the system will save some information about the function, including the parameters of the function, and the values of some registers, to the call stack. When the function is finished, the information is ejected from the call stack (POP). As shown, is a full call stack:
In, the whole is called the call stack, and each row is called a frame. Let's take a look at what the frame information consists of:
- (1) Frame number: A number of frames in the call stack, starting from 0, and then increasing
- (2) Pc:program Counter Register, point to the address of the next instruction to be executed in the current frame
- (3) Function name: The name of the function being called in the current frame
- (4) Parameters and values passed in: The parameters and their values that are called in the current frame when the function is called
- (5) Source location: The current frame execution to the source location, the format is File:linenum
Here also need to explain, do not know careful reader friends have found, foo that frame no PC address, gdb through this to indicate that the frame is currently executing to the frame, so we look at the call stack information, we can know where the program execution.
Readers do not think the original function call process there is so much information to know Ah, the following I began to introduce some GDB commands, through these commands you can see the above information, and even more detailed information.
1. View the call stack information: (The content of the specific information, as described in the second section above)
- (1)backtrace: Display the program's call stack information, you can use the bt abbreviation
- (2)backtrace n: Display the program's call stack information, only show stack top n frame (frames)
- (3)backtrace –n: Display the program's call stack information, only show stack bottom n Frame
- (4)set backtrace limit n: Sets the maximum number of frames for the bt display
- (5)where, info stack: All bt aliases, same function
2. View frame information:
- (1)frame n: View the information of the nth frame , frames can be abbreviated with F
- (2)frame addr: View the PC address for addr of the relevant information
- (3) up n: View the information of the nth frame above the current frame
- (4) down n: View the information of the nth frame below the current frame
3. See more detailed information:
- (1) Info frame,info frame n or info frame addr
To view the details of the specified frame, there is a need to do an introduction to the details, as shown in the following:
The information shown in is:
A. Address of the current frame: 0xbffff400
B. Current frame PC:EIP = 0x8048516
C. Current frame function: Bar (test.cpp:16)
D. Caller pc:saved EIP 0x8048535
E. Caller frame address: Called by frames at 0xbffff420
F. Callee frame address: Caller of frames at 0XBFFFF3E0
G. Language of the program used by the source code (c + +): Source language C + +
H. The address and value of the parameters of the current frame: Arglist at 0xbffff3f8, args:name=0x8048621 "Jessie", myname=0x804861c "Jack"
I. Address of local variable in current phase: Locals at 0xbffff3f8, Previous Frame's SP is 0xbffff400
K. Registers stored in the current frame: Saved registers:ebp at 0xbffff3f8, EIP at 0XBFFFF3FC
(2)info args: View parameters in the current frame
(3)info Locals: View local variables in the current frame
(4)info catch: View the exception handler in the current frame (exception handlers)
Again, only when reprint their mark, we need to go to the original blog to see "Small Wuge Blog" There are a lot of information, and again thank Xiao Wu to provide so rich posture sharing learning: http://www.wuzesheng.com/?p=1327,http:/ /www.wuzesheng.com/?p=1362,http://www.wuzesheng.com/?p=1386,http://www.wuzesheng.com/?p=1409
GDB debugging, reproduced a Daniel's Things