Classic GDB debug commands, including viewing variables, viewing memory

Source: Internet
Author: User
Tags print object

Classic GDB debug commands, including viewing variables, viewing memory

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:

printprint /

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.

Second, the program variable

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 (the current file is 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
You can use the :: operator:

file::variablefunction::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 the f2.c global variable x in the file:

gdb) p ‘f2.c‘::x

Of course, the :: operator will conflict with C + + and GDB will automatically recognize :: the C + + operator, so you don't have to worry about an exception when debugging a C + + program.

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, such as the GNU/C + + compiler, GCC, and you can use -gstabs options 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 *[email protected]

@To the left is the value of the first address of the array, 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 *[email protected]$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 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$25 = 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 indicates that the content is read from the memory address 0x54320 , H represents a unit in double-byte, 3 represents three units, and U represents a hexadecimal display.

VI, automatic 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.

displaydisplay/display/

Expr is an expression, FMT represents the format shown, addr represents a memory address, and when you set up one or more expressions with display, GDB automatically displays the values of the expressions you set as long as your program is stopped.
The formats I and S are also supported by display, and a very useful command is:

display/i $pc

$pcis the GDB environment variable, indicating the address of the instruction, /i 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:

undisplaydelete display

Delete auto-display, dnums means the automatic explicit number that is 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 displayenable display

Disable and Enalbe do not remove the settings that are automatically displayed, but just let them fail and resume.

info display

View the information displayed automatically for 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.

Vii. Setting display Options

GDB has more options on display, here I just cite most common options.

set print addressset print address on

Open address output, when the program displays the function information, GDB will show the function's parameter address. The system is turned on by default, such as:

(gdb) f#0 set_quotes (lq=0x34c78 "<<", rq=0x34c88 ">>")at input.c:530530 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:530530 if (lquote != def_lquote)show print address

See if the current address display option is turned on.

set print arrayset 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 offshow print arrayset 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:

$1 = {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:

$1 = {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 command is executed p foo as shown below:

$1 = {it = Tree, form = {tree = Acorn, bug = Cocoon}}

When the switch is closed, the p foo command is executed as shown below:

$1 = {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 make a $1, $2, $3 ..... number for each of your print commands in this way. You can then use this number to access the previous expression, such as $1 . 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 variables, like UNIX, also start with the $ beginning. 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 = 0print bar[$i++]->contents

So, when you do not have to, to print bar[0]->contents print bar[1]->contents 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 symbol before the register name $ . such as: p $eip .

Classic GDB debug commands, including viewing variables, viewing memory

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.