GDB command line most basic operation

Source: Internet
Author: User
Tags print object

Program Startup:

A. Cold start
GDB program e.g., GDB./cs
Gdb–p pid e.g., Gdb–p ' PIDOF CS '
GDB program core e.g., gdb./cs core.xxx
B. Hot start
(gdb) Attach PID e.g, (gdb) Attach 2313
C. Incoming command-line arguments
GDB program--args arglist
(GDB) Set args arglist
(GDB) Run arglist
Using shell commands: Shell command
Make:make Make-args (=shell make Make-args)
Set Breakpoints: b linenumber
Run Program: R args1 args2 ...
Complete termination of the program: Kill
Step: N (TIPs1: You can press ENTER to repeat the last action, this feature is useful when you are stepping through debugging).
Single Step Into: s
Continue execution: C
Set temporary breakpoints: TB linenumber can be interpreted as a one-time breakpoint, unlike a breakpoint, where a temporary breakpoint only works when the first execution occurs.
View variables: P
To set the Observer point:
W expression, when expression is a variable name, the variable will stop executing; You can also use conditions to qualify, such as W (z>28), when Z is greater than 28 o'clock, the program stops. Note that observers generally use variables on a larger scale, rather than local variables, because the observer point set on the local variable is canceled at the local end, such as at the end of the function where the variable is executing.
This does not, of course, include the case of main because the program ends after the main function is executed.
To view the stack frame:
Stack frame refers to the place where the run information (containing local variables, parameters, and where functions are called) is stored when a function call is called. Whenever a function is called, a new frame is pressed into a system-maintained frame, at the top of which is the function information that is now running, and is ejected and refactored when the function call ends.
In GdB, frame 0 is the current frame, frame 1 is the parent frame of the current frame, frame 2 is the parent frame of the parent frame, and so on, and the down command is reversed. This is a very useful information because the information in some of the earlier frames may give you some hints.
BackTrace (bt/where) view entire frame stack
Note: Back and forth in the frame does not affect the execution of the program.

View function stack bt, Exit Function finish

In GdB, you can view the values of the following three variables at any time:
1. Global variables (all files visible)
2. Static global variable (current file is visible)
3. Local variables (visible at current scope)
If you have a local variable that conflicts with a global variable (that is, a duplicate name), a local variable typically hides the global variable, that is, if a global variable and a local variable in a function have the same names, if the current stop is in the function, the value of the variable shown in print is the value of the local variable in the function. If you want to see the value of a global variable at this point, you can use the "::" Operator:

File::variable
Function::variable
You can specify the variables you want to see in this form, in which file or in which function. For example, view the value of global variable x in file f2.c:
GDB) P ' f2.c ':: X
Of course, the "::" operator will conflict with C + +, and GDB will automatically recognize "::" Whether it is a C + + operator, so you don't have to worry about exceptions when debugging C + + programs.
Also, it is important to note that if your program compiles with the optimization option turned on, some variables may not be accessible or value error codes can occur when debugging a program that has been optimized with GDB. This is normal, because the optimizer will delete your program, organize your program's sentence sequence, eliminate some meaningless variables, etc., so when GDB debugging such a program, the runtime of the instructions and you have written the instructions are different, there will be the results you can not imagine. When dealing with this situation, you need to turn off compilation optimizations when compiling the program. In general, almost all compilers support compile-optimized switches, for example, the GNU/C + + compiler, GCC, you can use the "-gstabs" option to solve this problem. See the compiler's usage documentation for compiler parameters as well.

Three, array
Sometimes you need to look at the value of a contiguous amount of memory space. For example, a paragraph of an array, or the size of dynamically allocated data. You can use GDB's "@" operator, the left side of "@" is the value of the address of the first memory, and the right side of "@" you want to see the length of the memory. For example, there is a statement in your program that says:
int *array = (int *) malloc (len * sizeof (int));
So, during GDB debugging, you can display the value of this dynamic array as follows:
P
The left side of @ is the value of the array's first address, which is what the variable array points to, and the length of the data on the right, which is stored in the variable len, and its output is about the following:
(GDB) P
$ = {2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40}
If it is a static array, you can display the contents of all the data in the array directly with the print array name.

Iv. output format
In general, GDB outputs the value of a variable based on the type of the variable. But you can also customize the format of GDB's output. For example, you want to output the hexadecimal of an integer, or binary to see the bit in the integer variable. To do this, you can use GDB's data display format:
x Displays the variable in hexadecimal format.
D Displays the variable in decimal format.
u displays unsigned integers in hexadecimal format.
o Displays the variable in octal format.
T displays the variable in binary format.
A displays the variable in hexadecimal format.
C Displays the variable in character format.
F Displays the variable in floating-point number format.
(GDB) P I
$21 = 101
(GDB) p/a I
$22 = 0x65
(GDB) P/C I
$23 = 101 ' e '
(GDB) p/f I
$24 = 1.41531145e-43
(GDB) p/x I
$ = 0x65
(GDB) p/t I
$26 = 1100101

V. View memory
You can use the Examine command (abbreviated as x) to see the values in the memory address. The syntax for the x command is as follows:
x/
N, F, and u are optional parameters.
n is a positive integer that represents the length of the displayed memory, which means that the contents of several addresses are displayed backwards from the current address.
F represents the format of the display, see above. If the address refers to a string, then the format can be s, if the ground ten is the instruction address, then the format can be I.
U represents the number of bytes requested from the current address, and if not specified, gdb defaults to 4 bytes. The u parameter can be substituted with the following character, b for single byte, h for Double Byte, W for four bytes, and G for eight bytes. When we specify the byte length, GDB starts by referring to the memory address, reads and writes the specified byte, and takes it as a value.
Represents a memory address.
N/f/u three parameters can be used together. For example:
Command: X/3uh 0x54320, which reads from memory address 0x54320, h means double-byte units, 3 for three units, and U for hexadecimal display.

Six, auto display
You can set up some automatically displayed variables that are automatically displayed when the program stops, or when you step through the tracks. The associated GDB command is display.
Display
display/
display/
Expr is an expression, FMT represents the format displayed, addr represents a memory address, and when you set one or more expressions with display, as long as your program is stopped, GDB automatically displays the values of these expressions that you set. The
format I and S are also supported by display, a very useful command is:
display/i $pc
$pc is the GDB environment variable, indicating the address of the instruction,/I means the output format is the machine script, that is, the assembly. So when the program stops, there will be the source code and machine instructions corresponding to the situation, this is a very interesting feature.
Here are some of the GDB commands associated with display:
Undisplay
Delete display
To remove the auto-display, dnums the automatic explicit numbering that was set up. If you want to delete several, the number can be separated by a space, if you want to delete a range of numbers, you can use a minus sign (for example: 2-5)
Disable display
Enable display
Disable and Enalbe do not remove the settings that are automatically displayed, but just let them fail and resume.
Info Display
To view the information displayed automatically by the display settings. GDB will print out a form to report to you, of course, how many auto-display settings are set in the debug, including, set number, expression, whether enable.
Seven, set display options
GDB has more options on display, here I just cite most common options.
Set Print address
Set print address on
Open address output, when the program displays the function information, GDB will show the function's parameter address. The system defaults to open, such as:
(GDB) F
#0 set_quotes (lq=0x34c78 "<<", rq=0x34c88 ">>")
at input.c:530
530 if ( Lquote! = def_lquote)

Set Print address off
The parameter address of the closing function is displayed, such as:
(GDB) Set print addr off
(GDB) F
#0 set_quotes (lq= "<<", rq= ">>") at input.c:530
530 if (lquote! = def_lquote)
Show print Address
See if the current address display option is turned on.
Set Print array
Set Print array on
Open the array display, open when the array is displayed, each element occupies a row, if not open, each element is separated by commas. This option is off by default. The two commands associated with it are as follows, and I will not say more.
Set Print array off
Show Print Array
Set Print elements
This option is mainly set to the array, if your array is too large, then you can specify one to specify the maximum length of the data display, when the length is reached, GDB will not show down. If set to 0, no limit is expressed.
Show print elements
View option information for print elements.
Set Print Null-stop
If this option is turned on, a terminator is stopped when the string is displayed. This option is off by default.
Set Print pretty on
If you turn on printf pretty this option, it will be pretty when GDB displays the struct body. Such as:
$ = {
Next = 0x0,
Flags = {
Sweet = 1,
Sour = 1
},
Meat = 0x54 "Pork"
}
Set print pretty off
Turn off the printf pretty option, and GDB displays the structure as follows:
$ = {next = 0x0, flags = {sweet = 1, sour = 1}, meat = 0x54 "Pork"}
Show Print Pretty
See how GDB shows the structure.

Set Print Sevenbit-strings
Sets the character display, whether it is displayed in the format "\nnn", or if open, the string or character data is displayed as \nnn, such as "65".
Show Print Sevenbit-strings
See if the character display switch is turned on.
Set Print Union
Sets whether the Union data is explicit when the structure is displayed. For example, the following data structures are available:
typedef enum {Tree, Bug} species;
typedef enum {big_tree, Acorn, seedling} tree_forms;
typedef enum {Caterpillar, Cocoon, Butterfly}
bug_forms;
struct Thing {
Species it;
Union {
Tree_forms Tree;
Bug_forms Bug;
} form;
};
struct thing foo = {Tree, {Acorn}};
When this switch is turned on, the p foo command is executed as shown below:
$ = {it = tree, form = {tree = Acorn, bug = Cocoon}}
When this switch is turned off, the p foo command is executed as shown below:
$ = {it = Tree, form = {...}}
Show Print Union
See how federation data is displayed
Set Print Object
In C + +, if an object pointer points to its derived class, if this option is turned on, GDB automatically displays the output according to the rules called by the virtual method, and if this option is turned off, GDB does not care about the virtual function table. This option is off by default.
Show Print Object
View settings for object options.
Set Print Static-members
This option indicates whether static data members are displayed when the content in a C + + object is displayed. The default is on.
Show Print Static-members
View the static data member option settings.
Set Print VTBL
When this option is turned on, GDB will display the virtual function table in a more structured format. It is turned off by default.
Show Print VTBL
View the options for the virtual function display format.

Viii. Historical records
When you use GDB's print to view the data that the program is running on, each print will be recorded by GDB. GDB will be $, $, $ ... This way you make a number for each of your print commands. You can then use this number to access the previous expression, such as $ $. The benefit of this feature is that if you have previously entered a long expression, if you want to see the value of the expression, you can use the history to access it, eliminating the need for duplicate input.

Nine, GDB environment variables
You can define your own variables in the debug environment of GDB to hold some of the running data in the debug program. To define a GDB variable is simply a simple one. Use GDB's set command. GDB's environment variable is the same as UNIX, and also begins with $. Such as:
Set $foo = *object_ptr
When you use environment variables, GDB creates this variable the first time you use it, and in future use it assigns a value directly. Environment variables have no type, you can define the type of any for the environment variable. includes struct and array.
Show convenience
This command looks at all of the environment variables that are currently set.
This is a more powerful function, environment variables and program variables interactive use, will make the program debugging more flexible and convenient. For example:
Set $i = 0
Print bar[$i ++]->contents
So, when you don't have to, print bar[0]->contents, print bar[1]->contents to enter the command. After entering such a command, just hit enter, repeat the previous statement, environment variables will automatically accumulate, so as to complete the function of output.

Ten, view register
To see the value of a register, it is simple to use the following command:
Info Registers
Look at the register case. (except floating-point registers)
Info all-registers
View all registers in the case. (including floating-point registers)
Info Registers
View the condition of the specified register.
Registers, such as the program's current command address (IP), the program's current stack address (SP), and so on. You can also use the Print command to access the register, just add a $ sign to the register name. such as: P $EIP.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.