GDB View Run-time Data

Source: Internet
Author: User
Keywords GDB variable view expression can
Tags compiler data debug debugging example expression file function

When you debug a program, when the program is stopped, you can use the Print command (the Shorthand command is P), or the synonymous command inspect to view the current program's running data. The Print command is in the following format:

Print <expr>
Print/<f> <expr>

<expr> is an expression, is the language of the program you are debugging expression (GDB can debug a variety of programming languages),<f> is the output format, for example, if you want the expression in 16 in the form of output, it is x.

Expression

Print, like many gdb commands, can accept an expression that GDB calculates based on the data that the current program is running, and since it is an expression, it can be a const constant, a variable, a function, etc. in the current program's operation. Unfortunately, GDB cannot use the macros you defined in your program.

The syntax of the expression should be the syntax of the currently debugged language, and since c++/c++ is a popular language, the examples in this article are all about C + +. (As for the section on debugging other languages with GDB, I'll explain it later)

In an expression, there are several operators supported by GDB that can be used in any language.

@

is an array of operator-related operators, which are described in more detail later.

::

Specifies a variable in a file or a function.
&http://www.aliyun.com/zixun/aggregation/37954.html ">NBSP;
{<type>} <addr>

Represents an object that points to the memory address <addr> type.

Program variables

In GdB, you can view the values of the following three variables at any time:

Global variables (visible to all files)
Static global variable (current file visible)
Local variable (current scope visible)
If your local and global variables collide (that is, duplicate names), in general, a local variable hides a global variable, that is, if a global variable has the same name as a local variable in a function, if the current stop point is in the function, the value of the variable displayed 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 the variables you want to see in this form, in which file or in which function. For example, look at the value of global variable x in file f2.c:

(GDB) P ' f2.c ':: X

Of course, the "::" operator will conflict with C + +, GDB can automatically identify the "::" whether C + + operators, so you do not have to worry about debugging C + + programs will occur when the exception.

Also, note that if your program turns on the optimization option when you compile it, some variables may not be accessible or error-coded when you debug the optimized program with GDB. This is normal, because the optimizer will delete your program, organize your program's statement order, eliminate some meaningless variables, etc., so in GDB debugging this program, the Run-time instructions and you write instructions are different, there will be the results you can not imagine. When dealing with this situation, you need to turn off compilation optimization when compiling the program. In general, almost all compilers support the compilation of optimized switches, such as the GNU-C + + compiler gcc, and you can use the "-gstabs" option to solve the problem. Also look at the compiler's usage documentation for the compiler's parameters.

Related Article

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.