Goto: GDB Related Learning

Source: Internet
Author: User
Tags goto print object

Statement This article goes from: http://www.cnblogs.com/rosesmall/archive/2012/04/12/2444431.html (see the Memory section for a supplement)

Also recommend an entry-level Good article: http://blog.csdn.net/liigo/article/details/582231

When you debug a program, when the program is stopped, you can use the Print command (the Shorthand command is P), or the synonym command inspect to see the current program's running data. The format of the Print command is:
Print
Print/
Is the expression, the expression of the language of the program you are debugging (GDB can debug a variety of programming languages), is the output format, for example, if you want to output the expression in the format of 16, then the/x.

I. Expressions
Print, like many gdb commands, can accept an expression, and gdb evaluates the expression based on the data that the current program is running, and since it is an expression, it can be a const constant, variable, function, and so on in the current program run. Unfortunately, GDB cannot use macros that you have defined in your program.
The syntax of the expression should be the syntax of the language currently being debugged, because C + + is a popular type of language, so the examples in this article are about C/s. (and the section on debugging other languages with GDB, which I'll cover later)
In an expression, there are several operators supported by GDB, which can be used in any language.
@
is an array-related operator, which is described in more detail later.
::
Specify a variable in the file or in a function.
{}
Represents an object with a type that points to a memory address.

Two, program variables
in GDB, you can view the values of the following three variables at any time:
1, global variables (all files visible)
2, static global variables (current file visible)
3, local variables (currently scope visible)
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 in this form the variable you want to see, which file or function it is in. 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 the C + + operator, so you don't have to worry about the exception when debugging a C + + program.
In addition, it should be noted that if your program is compiled with the optimization option turned on, then when debugging a program that has been optimized with GDB, some variables may not be accessible, or a value error code can be taken. 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, your program has such a statement:
int *array = (int *) malloc (len * sizeof (int));
So, during GDB debugging, you can display the value of this dynamic array as follows:
P *[ Email protected]
@ The left side of the array is the value of the first address, that is, the variable array points to the content, the right is the length of the data, which is stored in the variable len, its output, the result is about the following:
(GDB) P *[email protected]
$ = {2, 4, 6, 8, ten, three, three, three, four, three, ten, five, five, five, five,}
If it is a static array, you can directly use the print array name to display the contents of all the data in the array.

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.

View memory with GDB

Format: X/nfu

Description
X is the abbreviation of examine

n indicates the number of memory units to display

F represents the display mode, the following values are preferable
x Displays the variable in hexadecimal format.
D Displays the variable in decimal format.
u displays unsigned integers in decimal format.
o Displays the variable in octal format.
T displays the variable in binary format.
A displays the variable in hexadecimal format.
I instruction address format
C Displays the variable in character format.
F Displays the variable in floating-point number format.

U represents the length of an address unit
b represents a single byte,
H represents double-byte,
W represents four bytes,
g = Eight bytes


Format Letters is O (octal), X (hex), D (decimal), U (unsigned decimal),
T (binary), F (float), a (address), I (instruction), C (char) and S (String).
Size Letters is B (byte), H (Halfword), W (word), G (Giant, 8 bytes)

example
X/3uh buf
means reading from memory address buf,
H means double-byte units,
3 represents three units,
U indicates 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.

Goto: GDB Related Learning

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.