GDB uses help to see what kinds of common commands gdb has:
root@ubuntu:/home/lincoln/lchtool# GDB GNU gdb (GDB) 7.1-ubuntu Copyright (C) Free Software Foundation, Inc. ...... (GDB) Help List of classes of commands: Aliases--aliases of the 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 the execution without stopping the program user-defined--user-defined commands Type ' help ' followed by the A class name for a list of the commands in that class. Type ' help all ' for the ' list of all ' commands. Type ' help ' followed by the command name for the full documentation. Type ' apropos word ' to ' search for ' commands related to ' word '. Command name abbreviations are allowed if unambiguous. (GDB) Help breakpoints/* through Help <CLASS>, such as Help breakpoints view all commands for setting breakpoints. |
1 Suspend execution
There are 3 ways to notify GDB to suspend the execution of the program:<1> breakpoints <2> monitoring points <3> capture points
<1> Breakpoint
Notifies GDB to suspend execution at a specific location in the program.
Set breakpoints in a specific "location" in your program, and when that point is reached, the debugger suspends execution of the program. Where "location" can refer to various source code lines, code addresses, line numbers in source code files, or the entry of functions.
<1.1> Set Breakpoints
Command |
Description |
Break <function> |
Pause at the entrance of a specified function |
Break <filename:function> |
Pause at the entry of function functions at source file filename |
Break <linenum> |
Pause at specified line number |
Break <filename:linenum> |
Paused on LineNum line in source file filename |
Break <+offset> |
The offset line is paused before the current line number |
Break <-offset> |
The offset line is paused after the current line number |
Break *address |
Pause at the address where the program runs memory addresses |
Tbreak ditto |
Temporary breakpoint |
Rbreak Regular Expression |
Rbreak list_* that breakpoints are set at all functions where the list_ is the beginning character |
info breakpoints [n] |
View information for all breakpoints or breakpoint n |
<1.2> Set Conditional breakpoints
1> Set a conditional breakpoint
Syntax: Break Break-args if (condition) where condition is an expression that has a Boolean value, including:
1.1> equality, logic, and inequality operators (<, <=, = =,!=, >, >=, &&, | | , etc.), such as:
break180 if String==null && i<0
1.2> bitwise OR SHIFT Operators (&, |, ^, >>, <<, etc.), such as:
Break test.c:34 if (x&y) = = 1
1.3> arithmetic operators (+ 、-、 *,/,%, etc.), such as:
Break MyFunc If I% (j+3)!= 0
The 1.4> function, as long as it is linked to the program, such as:
Break Test.c:myfunc if!check_variable_sanity (i)
1.5> library functions, as long as they are linked to the program, such as:
Break if strlen (mystring) = 0
2> an unconditional breakpoint into a conditional breakpoint
For example, set breakpoint 3 to a conditional breakpoint and add a condition of i==3:
(GDB) Cond 3 i==3
Delete the condition, but keep the breakpoint:
(GDB) Cond 3
<1.3> Breakpoint Command List
When you encounter a GDB breakpoint, you almost always have to look at a variable. If you repeatedly encounter the same breakpoint, you will see the same variable again and again. At this point, you can use the breakpoint command list to have GDB automatically execute a set of commands each time a breakpoint is reached.
To set a list of commands using the commands command:
Commandsbreakpoint-number
...
Commands/* The command can be either printf, or a custom command define*/
...
End
Example:
(GDB) define MY_CMD//custom command Type commands for definition of "my_cmd". End with a line saying just "End". >printf "This are my cmd\n" >printf "i =%d, sum =%d\n", $arg 0, $arg 1 >end (GDB) Commands 1 Type commands for when Breakpoint 1 is hit. End with a line saying just "End". >printf "'ll usr my cmd\n"//printf command >my_cmd I sum//custom command my_cmd, I and sum as parameters >end (GDB) |
<1.4> Remove and disable breakpoints
Command |
Description |
Delete breakpoint_list (number or series of numbers) |
Delete 2//delete second breakpoint Delete 2 4//delete second and fourth breakpoints |
Delete |
Remove All Breakpoints |
Clear |
Remove the breakpoint at the next instruction that GDB will execute |
Clear function Clear Filename:function Clear Line_number Clear Filename:line_number |
Clears breakpoints by location, similar to the break command |
Disable 3 |
Disable a third breakpoint |
Enable 1 5 |
Enable first and fifth breakpoints |
Enable once Breakpoint-list |
Disable this breakpoint the next time it causes GDB to suspend execution |
<2> Monitoring points
Notifies GDB to suspend execution when the value of a particular memory location (or an expression involving one or more locations) changes.
A watch point is a special type of breakpoint, except that the monitor point is not "resident" in a line of source code, but instead instructs GDB to suspend execution whenever an expression changes a value.
Among them, the expression in GDB can contain a lot of content:
> variables that are easy for gdb to use
Any variables within the scope of the > Program
> string, numeric, or character constants of any kind
> Preprocessor Macros
> Conditions, function calls, type casts, and language-defined operators.
Syntax: Watch <expr>
Command |
Description |
Watch I |
GDB pauses when I change value |
Watch (I | j >) && i > && strlen (name) >6 |
When an expression value changes, gdb suspends execution of the program |
Rwatch Expr |
Suspend program execution When an expression is read |
Awatch Expr |
Suspends program execution when the value of an expression is read or written |
<3> capture Points
Notifies GDB to suspend execution when a specific event occurs.
Syntax: Catch <event>
When event occurs, stop the program. The event can be the following:
1>throw an exception thrown by a C + +. (Throw is the key word)
2>catch an exception that is caught by a C + +. (Catch is the key word)
3>exec calls the system call exec. (exec is the keyword, this feature is only useful under HP-UX)
When 4>fork calls the system call fork. (Fork is the keyword, this feature is only useful under HP-UX)
When 5>vfork calls the system call Vfork. (Vfork is the keyword, this feature is only useful under HP-UX)
When 6>load or load<libname> load a shared library (dynamic-link library). (load is the keyword, this feature is only useful under HP-UX)
7>unload or unload<libname> unload a shared library (dynamic-link library). (Unload is the keyword, this feature is only useful under HP-UX)
Grammar:tcatch<event>
Set the capture point only once, and when the program stops, the point is automatically deleted.
<4> View Breakpoints
Command |
Description |
info breakpoints [n] |
View information for all breakpoints or breakpoint n |
Info break [n] |
View information for all breakpoints or breakpoint n |
(GDB) Info break
Num Type Disp ENB address What
1 breakpoint Keep Y 0x000000000040052b in process at Breakif.c:7
which
1> identifier (Num): A unique identifier for a breakpoint, that is, a breakpoint number
2> type: Type of breakpoint: Breakpoint, Watch point, capture point
3> Deployment (DISP): Each breakpoint has a deployment that indicates what happens to the breakpoint the next time the breakpoint causes GDB to suspend the program, and there are 3 possible deployments:
3.1> hold (Keep): Do not change breakpoints the next time you reach a breakpoint. (Default deployment of new breakpoint)
3.2> Delete (del): Deletes the breakpoint the next time it reaches the breakpoint. (This is true of any breakpoints created using the Tbreak command)
3.3> Disabled (DIS): This breakpoint is disabled the next time a breakpoint is reached. (Breakpoints set using the Enableonce command)
4> enabled Status (ENB): Indicates whether the breakpoint is currently enabled or disabled
5> Address: This is where the breakpoint is set in memory. It is primarily intended for use by assembler programmers or attempts to debug executables that are not compiled with an expanded notation table.
6> position (What): A specific line number and file name where the breakpoint is located at the source code location.
2 resuming execution
The recovery execution method after a pause at a breakpoint is:
<1> use step and next to debug
<2> use continue recovery program execution
<3> Use the Finish Recovery program-abbreviated to FIN, to complete the execution of the current function.
<4> use the until recovery program--shorthand u, to complete the executing loop without further pausing in the loop (except for the middle break point in the loop).
Command |
Description |
Next |
Single-step debugging, no entry function |
Step |
Step into the function |
Continue |
The unconditional recovery program executes until it encounters another breakpoint or the end of the program. Continue n//restore execution n times |
Finish |
To complete the execution of the current function, ignoring statements that have not yet been executed |
Return <expr> |
Returns the function as an expression of expr, ignoring statements that have not yet been executed. If there is no expr, return void. |
Until |
Used to complete an executing loop without further pausing in the loop (except for the middle break point in the Loop) |
Jump Line |
The jump to the specified line number executes and does not pause, so it needs to be used in conjunction with break or tbreak. And because jump does not change the current stack of content, so jump from one function to another function, when the function runs back when the stack operation will produce errors, so jump generally in the same function to jump. |
Call |
Force Call function |
3 Viewing and setting variables
<1>p--is used to view the value of a variable, such as P I can also be used to set variables, such as P i=4
<2>set--is used to set the value of a variable, such as set var i = 1; Set args argument1 argument2;
<3>x--used to view the value above the specified memory address
<4>info--is used to view breakpoints, threads, frame, args, locals, catch, registers, all-registers,
View Commands |
Description |
Print |
Display the value of a variable or an expression |
Display |
When a program pauses execution, the value of a mind variable or expression |
Undisplay |
Display's counter command |
Pwd |
Show Current working directory |
PType |
Displays the declared content of a data structure or the type of display variable, function |
Whatis |
To display a variable or function type |
SET Command |
Description |
Print x = 4 |
Set the value of the variable x to 4 |
Set var x = 4 |
Set the value of the variable x to 4 |
Set args 1 2 3 4 |
Set the value of parameter 1, parameter 2, parameter 3, parameter 4 |
Set $myvar = 123 |
Set a custom variable MyVar value of 123 |