Program debugging with GDB (5)

Source: Internet
Author: User

 

View runtime data
-------


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 <expr>
Print/<F> <expr>
<Expr> is an expression, which is the language expression of the program you debug (GDB can debug multiple programming languages), <F> is the output format, for example, if you want to output the expression in hexadecimal format, it is/X.


I. Expressions

Like many gdb commands, print 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 ++. (I will introduce the chapters on debugging other languages using GDB 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.

{<Type >}< ADDR>
Indicates an object of Type pointing to the memory address <ADDR>.


Ii. 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), the local variables usually hide the 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, to view the value of global variable X in 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:

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}

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 displays 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. view memory

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/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.

The n/f/U parameters can be used together. For example:

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, 3 indicates three units, and the u table is displayed in hexadecimal format.


6. Automatic Display

You can set variables that are automatically displayed when the program stops or when you track them in a single step. The relevant GDB command is display.

Display <expr>
Display/<FMT> <expr>
Display/<FMT> <ADDR>

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

$ PC is the environment variable of GDB, 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 <dnums...>
Delete display <dnums...>
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 (for example, 2-5)

Disable display <dnums...>
Enable display <dnums...>
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.

<-Previous Page->

(All Rights Reserved. Please indicate the author and source when reprinting)

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.