View runtime data of GDB debugging program

Source: Internet
Author: User

This article reposted from: http://www.cnblogs.com/yangce/archive/2011/11/20/2256482.html In addition, this blog technology content is very high, worthy of taste.

When you use GDB to debug a program, when the program runs to a previously set breakpoint, you can easily view the value of the current variable, GDB can easily meet your needs ~~

The most common command in GDB is print (abbreviated as P). The specific format is as follows:

Print <expr> Print/F <expr> f Represents the output format X Display variables in hexadecimal format D Display variables in decimal format U display unsigned integer o display variables in octal format t display variables in binary format a display variables in hexadecimal format C display variables in character format F display variables in floating point format
Expression

The print command can accept expressions. The expression definition follows the C/C ++ syntax. Note that the macro expression defined in the program cannot appear in the expression. In the gdb expression, three Special operators are also supported:

@ Is an array-related operator. It will be described in more detail later: specify a variable in a file or function. Note that it is in the C ++ Syntax :: operator differentiation {} indicates an object pointing to the type of the memory address
Locating program variables

In GDB, view the values of the following three variables:

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 the local variables and global variables conflict with each other, the local variables overwrite the global variables, you can use the: delimiter to view global variables.

file::variablefunction::variable

Note: If the-O3 option is added during compilation, the compiler modifies your program and may view different variables, we recommend that you disable the optimization option during debugging, that is,-O0.

Dynamic Array

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:

p *array@len

The left side of @ is the value of the first address of the array, that is, the content pointed to by the variable array, and the right side is the length of the data. It is saved in the variable Len and the output result is, it looks like this:

(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}
View memory

In GDB, you can use the EXAMINE command to view the memory content, which is abbreviated as X. The usage is as follows:

X/<n/f/u> <ADDR> 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 byte, and G represents an octal node. 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. <ADDR> indicates a memory address.

For example

X/3uh 0x54320 reads content from the memory address 0x54320. h indicates that the two bytes are used as a unit. 3 indicates three units. The u table is displayed in hexadecimal format.
Auto display

In GDB, you can set to automatically display the variable content when the program stops at the breakpoint, that is, the display command, using the following:

Display <expr> display/<FMT> <ADDR> expr is an expression. FMT indicates the display format, and ADDR indicates the memory address.

A very useful command that shows the correspondence between the source code and the machine code:

Display/I $ PC is the environment variable of GDB, indicating the instruction address./I indicates that the output format is machine instruction code, that is, assembly. So when the program stops, the source code will correspond to the machine script code.

Commands related to display management:

Undisplaydelete displaydisable displayenable displayinfo display
View registers
Info registers: Check the register information. (Except floating-point registers) info all-registers view all registers. (Including floating-point registers) info registers: Check the status of the specified register

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.