GDB viewing Variables

Source: Internet
Author: User
Tags print object

When you debug a program, when the program is stopped, you can use the print command (abbreviated as P) or the synonymous Command inspect to view the running data of the current program. The format of the print command is: Print
Print/
Is an expression, is the language expression of the program you debug (GDB can debug multiple programming languages), is the output format, for example, if you want to output the expression in hexadecimal format, it is/X. I. Expression print is the same as many gdb commands and can accept an expression. GDB calculates this expression based on the data of the current program running. Since it is an expression, it can be the const constants, variables, and functions in the current program running. Unfortunately, GDB cannot use the macros you define in the program. The expression syntax should be the syntax of the language currently being debugged. Since C/C ++ is a popular language, the examples in this article are about C/C ++. (For more information about debugging other languages using GDB, I will introduce it later.) In expressions, there are several operators supported by GDB that can be used in any language. @
Is an array-related operator, which will be described in more detail later. ::
Specifies a variable in a file or function. {}
Indicates an object pointing to the memory address type. 2. program variables in GDB, you can view the values of the following three variables at any time:
1. Global variables (visible to all files)
2. Static global variables (visible to the current file)
3. Local variables (visible to the current scope). If your local variables conflict with global variables (that is, duplicate names), local variables usually hide global variables, that is, if a global variable and a local variable in a function have the same name, if the current stop point is in the function, the value of the variable displayed with print will be the value of the local variable in the function. If you want to view the global variable value, you can use the ":" OPERATOR: file: Variable
Function: Variable
You can specify the variable you want to view in this form, which file or function is used. For example, check the value of global variable X in the file f2.c: GDB) P' F2. C ': X, of course, the ":" operator will conflict with the c ++ operator. GDB can automatically identify whether the ":" operator is a C ++ operator, so you don't have to worry about exceptions when debugging C ++ programs. In addition, if optimization options are enabled during program compilation, some variables may be inaccessible when you debug the optimized program using GDB, or a value error code. This is normal, because the optimization program will delete and modify your program, sort out the statement order of your program, and remove some meaningless variables. So when you debug this program in GDB, the running commands are different from the ones you write, and the unexpected results are displayed. To deal with this situation, you need to disable compilation optimization during program compilation. Generally, almost all compilers support Compilation optimization. For example, the gnu c/C ++ compiler GCC can be used to solve this problem. For compiler parameters, see the instructions for use of the compiler. 3. array sometimes, you need to view the value of a continuous memory space. For example, the size of an array or dynamically allocated data. You can use the "@" operator of GDB. The left side of "@" is the value of the first memory address, and the right side of "@" is the length of the memory you want to view. For example, your program has the following statement: int * array = (int *) malloc (LEN * sizeof (INT); therefore, during GDB debugging, you can use the following command to display the value of this dynamic array: The left side of p * array @ Len @ is the value of the first address of the array, that is, the content pointed to by the variable array, on the right side is the data length, which is saved in the Len variable, and the output result is about the following: (GDB) p * array @ Len
$1 = {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 directly use the print array name to display all the data in the array. Iv. Output Format generally, GDB outputs the value of a variable based on the type of the variable. However, you can also customize the output format of GDB. For example, you want to output a hexadecimal integer or a binary value to view the bitwise of the integer variable. To do this, you can use the gdb data display format: X to display variables in hexadecimal format.
D. Display variables in decimal format.
U displays unsigned integers in hexadecimal format.
O display variables in octal format.
T display variables in binary format.
A displays variables in hexadecimal format.
C. Display variables in character format.
F. Display variables in floating point 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.20.31145e-43 (GDB) P/x I
$25 = 0x65 (GDB) P/T I
$26 = 1100101 5. You can use the EXAMINE command (abbreviated as X) to view the value in the memory address. The syntax of the X command is as follows: X/N, f, and u are optional parameters. N is a positive integer that shows the memory length, that is, the content of several addresses is displayed from the current address to the back.
F indicates the display format. See the preceding figure. If the address refers to a string, the format can be S. If location 10 is the instruction address, the format can be I.
U indicates the number of bytes requested from the current address. If not specified, GDB defaults to four bytes. The U parameter can be replaced by the following characters. B represents a single byte, H represents a double byte, W represents a four-character section, and G represents an eight-character section. When we specify the byte length, GDB reads and writes the specified byte starting from the memory address specified by the memory and takes it as a value. Indicates a memory address. The n/f/U parameters can be used together. For example, the command: X/3uh 0x54320 indicates that the content is read from the memory address 0x54320, h indicates that the two bytes are used as a unit, and 3 indicates three units, the u table is displayed in hexadecimal format. 6. Automatic Display you can set some automatically displayed variables. These variables are automatically displayed when the program stops or when you track them in a single step. The relevant GDB command is display. Display
Display/
Display/expr is an expression. FMT indicates the display format. ADDR indicates the memory address. After you set one or more expressions using display, as long as your program is stopped, GDB automatically displays the values of these expressions. Formats I and S are also supported by display. A very useful command is: Display/I $ PC is the gdb environment variable, indicating the instruction address, /I indicates that the output format is machine instruction code, that is, assembly. When the program is stopped, the source code corresponds to the machine script code. This is a very interesting function. The following are some display-related gdb commands: undisplay
Delete display
Delete automatic display. dnums indicates the automatically displayed number. If you want to delete multiple numbers at the same time, they can be separated by spaces. If you want to delete numbers within a range, you can use a minus sign (such as: 2-5) to disable display
Enable display
Disable and enalbe do not delete automatically displayed settings, but only make them invalid and restored. Info display
View the automatic display information set for display. GDB generates a table and reports to you how many automatic display settings are set in debugging, including the number, expression, and enable. 7. Set display options. GDB has many options for display. Here I will only illustrate most of the commonly used options. Set print address
Set print address on
Open the address output. When the program displays the function information, GDB displays the function parameter address. The system is enabled by default, such as: (GDB) f
#0 set_quotes (SCSI = 0x34c78 "<", RQ = 0x34c88 "> ")
At input. C: 530
530 if (lquote! = Def_lquote) set print address off
Disable function parameter address display, for example: (GDB) set print ADDR off
(GDB) f
#0 set_quotes (SCSI = "<", RQ = ">") at input. C: 530
530 if (lquote! = Def_lquote) show print address
Check whether the current address display option is enabled. Set print Array
Set print array on
Open the array display. When the array is displayed, each element occupies one row. If it is not opened, each element is separated by a comma. This option is disabled by default. The two related commands are as follows. Set print array off
Show print arrayset print Elements
This option is mainly used to set an array. If your array is too large, you can specify a maximum length for data display. When this length is reached, GDB is no longer displayed. If it is set to 0, it indicates no restriction. Show print Elements
View the options of print elements. Set print null-stop
If this option is enabled, when the string is displayed, the end character is displayed. This option is off by default. Set print pretty on
If you enable the printf pretty option, it looks pretty when GDB displays the struct. For example, $1 = {
Next = 0x0,
Flags = {
Sweet = 1,
Sour = 1
},
Meat = 0x54 "pork"
} Set print pretty off
Disable the printf pretty option. When GDB displays the struct, it will display the following: $1 = {next = 0x0, flags = {sweet = 1, sour = 1 }, meat = 0x54 "pork"} Show print pretty
View how GDB displays struct. Set print sevenbit-strings
Set whether to display characters in the/NNN format. If yes, the string or character data is displayed in the/NNN format, for example, "65 ". Show print sevenbit-strings
Check whether the character display switch is enabled. Set print Union
Sets whether to explicitly display the consortium data in the struct. For example, the following data structure is 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 enabled, after executing the P Foo command, it will be shown as follows:
$1 = {It = tree, form = {tree = Acorn, bug = cocoon} When you disable this function, run the P Foo command and it will be shown as follows:
$1 = {It = tree, form = {...} Show print Union
View the display mode of the consortium Data Set Print Object
In C ++, if an object pointer points to its derived class, if this option is enabled, GDB will automatically display the output according to the rules of the virtual method call. If this option is disabled, GDB does not care about the virtual function table. This option is off by default. Show Print Object
View the settings of object options. Set print static-members
This option indicates whether static data members are displayed when the content of a C ++ object is displayed. The default value is on. Show print static-members
View the static data member option settings. Set print vtbl
When this option is enabled, GDB will display the virtual function table in a more regular format. It is disabled by default. Show print vtbl
View the options of the virtual function display format. 8. history when you use GDB print to view the data when the program is running, each print will be recorded by GDB. GDB will compile the numbers for each print command in the form of $1, $2, $3. Therefore, you can use this number to access previous expressions, such as $1. The benefit of this function is that if you have previously entered a long expression and want to view the value of this expression, you can use the history to access it, saves Repeated input. 9. You can define your own variables in the gdb debugging environment to save running data in some debugging programs. It is easy to define a GDB variable. Use the SET command of GDB. The environment variable of GDB is the same as that of UNIX, and starts with $. For example, when set $ Foo = * object_ptr uses an environment variable, GDB will create this variable when you use it for the first time. In future use, it will be assigned a value directly. The environment variable has no type. You can define any type for the environment variable. Including struct and array. Show convenience
This command is used to view all the environment variables currently set. This is a powerful function. The interaction between environment variables and program variables makes program debugging more flexible and convenient. Example: Set $ I = 0
Print bar [$ I ++]-> contents, so you don't need to, print bar [0]-> contents, print bar [1]-> contents enter the command. After you enter such a command, you only need to press enter to repeat the previous statement and the environment variables will be automatically accumulated to complete the output-by-one function. 10. To view the register value, run the following command: info registers.
View the register information. (Except for floating-point registers) info all-registers
View All registers. (Including floating-point registers) info registers
View the status of the specified register. The register contains the data when the program is running, such as the instruction address (IP) that the program is currently running, and the current stack address (SP) of the program. You can also use the print command to access the register. You only need to add a $ symbol before the register name. For example, p $ EIP. Print
Print/
Is an expression, is the language expression of the program you debug (GDB can debug multiple programming languages), is the output format, for example, if you want to output the expression in hexadecimal format, it is/X. I. Expression print is the same as many gdb commands and can accept an expression. GDB calculates this expression based on the data of the current program running. Since it is an expression, it can be the const constants, variables, and functions in the current program running. Unfortunately, GDB cannot use the macros you define in the program. The expression syntax should be the syntax of the language currently being debugged. Since C/C ++ is a popular language, the examples in this article are about C/C ++. (For more information about debugging other languages using GDB, I will introduce it later.) In expressions, there are several operators supported by GDB that can be used in any language. @
Is an array-related operator, which will be described in more detail later. ::
Specifies a variable in a file or function. {}
Indicates an object pointing to the memory address type. 2. program variables in GDB, you can view the values of the following three variables at any time:
1. Global variables (visible to all files)
2. Static global variables (visible to the current file)
3. Local variables (visible to the current scope). If your local variables conflict with global variables (that is, duplicate names), local variables usually hide global variables, that is, if a global variable and a local variable in a function have the same name, if the current stop point is in the function, the value of the variable displayed with print will be the value of the local variable in the function. If you want to view the global variable value, you can use the ":" OPERATOR: file: Variable
Function: Variable
You can specify the variable you want to view in this form, which file or function is used. For example, check the value of global variable X in the file f2.c: GDB) P' F2. C ': X, of course, the ":" operator will conflict with the c ++ operator. GDB can automatically identify whether the ":" operator is a C ++ operator, so you don't have to worry about exceptions when debugging C ++ programs. In addition, if optimization options are enabled during program compilation, some variables may be inaccessible when you debug the optimized program using GDB, or a value error code. This is normal, because the optimization program will delete and modify your program, sort out the statement order of your program, and remove some meaningless variables. So when you debug this program in GDB, the running commands are different from the ones you write, and the unexpected results are displayed. To deal with this situation, you need to disable compilation optimization during program compilation. Generally, almost all compilers support Compilation optimization. For example, the gnu c/C ++ compiler GCC can be used to solve this problem. For compiler parameters, see the instructions for use of the compiler. 3. array sometimes, you need to view the value of a continuous memory space. For example, the size of an array or dynamically allocated data. You can use the "@" operator of GDB. The left side of "@" is the value of the first memory address, and the right side of "@" is the length of the memory you want to view. For example, your program has the following statement: int * array = (int *) malloc (LEN * sizeof (INT); therefore, during GDB debugging, you can use the following command to display the value of this dynamic array: The left side of p * array @ Len @ is the value of the first address of the array, that is, the content pointed to by the variable array, on the right side is the data length, which is saved in the Len variable, and the output result is about the following: (GDB) p * array @ Len
$1 = {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 directly use the print array name to display all the data in the array. Iv. Output Format generally, GDB outputs the value of a variable based on the type of the variable. However, you can also customize the output format of GDB. For example, you want to output a hexadecimal integer or a binary value to view the bitwise of the integer variable. To do this, you can use the gdb data display format: X to display variables in hexadecimal format.
D. Display variables in decimal format.
U displays unsigned integers in hexadecimal format.
O display variables in octal format.
T display variables in binary format.
A displays variables in hexadecimal format.
C. Display variables in character format.
F. Display variables in floating point 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.20.31145e-43 (GDB) P/x I
$25 = 0x65 (GDB) P/T I
$26 = 1100101 5. You can use the EXAMINE command (abbreviated as X) to view the value in the memory address. The syntax of the X command is as follows: X/N, f, and u are optional parameters. N is a positive integer that shows the memory length, that is, the content of several addresses is displayed from the current address to the back.
F indicates the display format. See the preceding figure. If the address refers to a string, the format can be S. If location 10 is the instruction address, the format can be I.
U indicates the number of bytes requested from the current address. If not specified, GDB defaults to four bytes. The U parameter can be replaced by the following characters. B represents a single byte, H represents a double byte, W represents a four-character section, and G represents an eight-character section. When we specify the byte length, GDB reads and writes the specified byte starting from the memory address specified by the memory and takes it as a value. Indicates a memory address. The n/f/U parameters can be used together. For example, the command: X/3uh 0x54320 indicates that the content is read from the memory address 0x54320, h indicates that the two bytes are used as a unit, and 3 indicates three units, the u table is displayed in hexadecimal format. 6. Automatic Display you can set some automatically displayed variables. These variables are automatically displayed when the program stops or when you track them in a single step. The relevant GDB command is display. Display
Display/
Display/expr is an expression. FMT indicates the display format. ADDR indicates the memory address. After you set one or more expressions using display, as long as your program is stopped, GDB automatically displays the values of these expressions. Formats I and S are also supported by display. A very useful command is: Display/I $ PC is the gdb environment variable, indicating the instruction address, /I indicates that the output format is machine instruction code, that is, assembly. When the program is stopped, the source code corresponds to the machine script code. This is a very interesting function. The following are some display-related gdb commands: undisplay
Delete display
Delete automatic display. dnums indicates the automatically displayed number. If you want to delete multiple numbers at the same time, they can be separated by spaces. If you want to delete numbers within a range, you can use a minus sign (such as: 2-5) to disable display
Enable display
Disable and enalbe do not delete automatically displayed settings, but only make them invalid and restored. Info display
View the automatic display information set for display. GDB generates a table and reports to you how many automatic display settings are set in debugging, including the number, expression, and enable. 7. Set display options. GDB has many options for display. Here I will only illustrate most of the commonly used options. Set print address
Set print address on
Open the address output. When the program displays the function information, GDB displays the function parameter address. The system is enabled by default, such as: (GDB) f
#0 set_quotes (SCSI = 0x34c78 "<", RQ = 0x34c88 "> ")
At input. C: 530
530 if (lquote! = Def_lquote) set print address off
Disable function parameter address display, for example: (GDB) set print ADDR off
(GDB) f
#0 set_quotes (SCSI = "<", RQ = ">") at input. C: 530
530 if (lquote! = Def_lquote) show print address
Check whether the current address display option is enabled. Set print Array
Set print array on
Open the array display. When the array is displayed, each element occupies one row. If it is not opened, each element is separated by a comma. This option is disabled by default. The two related commands are as follows. Set print array off
Show print arrayset print Elements
This option is mainly used to set an array. If your array is too large, you can specify a maximum length for data display. When this length is reached, GDB is no longer displayed. If it is set to 0, it indicates no restriction. Show print Elements
View the options of print elements. Set print null-stop
If this option is enabled, when the string is displayed, the end character is displayed. This option is off by default. Set print pretty on
If you enable the printf pretty option, it looks pretty when GDB displays the struct. For example, $1 = {
Next = 0x0,
Flags = {
Sweet = 1,
Sour = 1
},
Meat = 0x54 "pork"
} Set print pretty off
Disable the printf pretty option. When GDB displays the struct, it will display the following: $1 = {next = 0x0, flags = {sweet = 1, sour = 1 }, meat = 0x54 "pork"} Show print pretty
View how GDB displays struct. Set print sevenbit-strings
Set whether to display characters in the/NNN format. If yes, the string or character data is displayed in the/NNN format, for example, "65 ". Show print sevenbit-strings
Check whether the character display switch is enabled. Set print Union
Sets whether to explicitly display the consortium data in the struct. For example, the following data structure is 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 enabled, after executing the P Foo command, it will be shown as follows:
$1 = {It = tree, form = {tree = Acorn, bug = cocoon} When you disable this function, run the P Foo command and it will be shown as follows:
$1 = {It = tree, form = {...} Show print Union
View the display mode of the consortium Data Set Print Object
In C ++, if an object pointer points to its derived class, if this option is enabled, GDB will automatically display the output according to the rules of the virtual method call. If this option is disabled, GDB does not care about the virtual function table. This option is off by default. Show Print Object
View the settings of object options. Set print static-members
This option indicates whether static data members are displayed when the content of a C ++ object is displayed. The default value is on. Show print static-members
View the static data member option settings. Set print vtbl
When this option is enabled, GDB will display the virtual function table in a more regular format. It is disabled by default. Show print vtbl
View the options of the virtual function display format. 8. history when you use GDB print to view the data when the program is running, each print will be recorded by GDB. GDB will compile the numbers for each print command in the form of $1, $2, $3. Therefore, you can use this number to access previous expressions, such as $1. The benefit of this function is that if you have previously entered a long expression and want to view the value of this expression, you can use the history to access it, saves Repeated input. 9. You can define your own variables in the gdb debugging environment to save running data in some debugging programs. It is easy to define a GDB variable. Use the SET command of GDB. The environment variable of GDB is the same as that of UNIX, and starts with $. For example, when set $ Foo = * object_ptr uses an environment variable, GDB will create this variable when you use it for the first time. In future use, it will be assigned a value directly. The environment variable has no type. You can define any type for the environment variable. Including struct and array. Show convenience
This command is used to view all the environment variables currently set. This is a powerful function. The interaction between environment variables and program variables makes program debugging more flexible and convenient. Example: Set $ I = 0
Print bar [$ I ++]-> contents, so you don't need to, print bar [0]-> contents, print bar [1]-> contents enter the command. After you enter such a command, you only need to press enter to repeat the previous statement and the environment variables will be automatically accumulated to complete the output-by-one function. 10. To view the register value, run the following command: info registers.
View the register information. (Except for floating-point registers) info all-registers
View All registers. (Including floating-point registers) info registers
View the status of the specified register. The register contains the data when the program is running, such as the instruction address (IP) that the program is currently running, and the current stack address (SP) of the program. You can also use the print command to access the register. You only need to add a $ symbol before the register name. For example, 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.