Debug program with GDB (v) _gdb

Source: Internet
Author: User

View Run-time Data
———————
   
    When you debug a program, you can use the Print command (the Shorthand command is p) when the program is stopped. or synonymous command inspect to view the running data for the current program. The Print command is in the format
   
    print <expr>
    print/<f> <expr>
        <expr> is an expression that is an expression of the language of the program you are debugging (GDB can debug multiple programming languages),< F> is the format of the output, for example, if you want to output the expression in 16-input format, then it is/x.
       
   
One, 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 the program.

The syntax of an expression should be the syntax of the language currently being debugged, and since C + + is a popular language, the examples in this article are all about C + +. (and 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 in a function.

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


Second, the program variable

In GdB, you can view the values of the following three variables at any time:
1, global variables (all files are visible)
2. Static global variable (current file visible)
3. Local variables (current scope visible)

If your local variables 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 very 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.

Three, array

Sometimes, you need to look at the value of a contiguous memory space. such as a section of an array, or the size of dynamically allocated data. You can use the GDB "@" operator, "@" on the left is the first memory address of the value, "@" to the right you want to see the length of memory. For example, you have a statement in your program that says:

int *array = (int *) malloc (len * sizeof (int));

Therefore, in the GDB debugging process, you can display the following command of this dynamic array of values:

P *array@len

The left side of the @ is the value of the first address of the array, which is what the variable array points to, the right is the length of the data, stored in the variable len, and the output is approximately the following:

(GDB) P *array@len
$ = {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 use the print array name directly to display the contents of all the data in the array.


Four, output format

    Generally, 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 position of the bit in the integer variable. To do this, you can use GDB's data display format:
   
    x  to display variables in hexadecimal format.
    d  displays variables in decimal format. The
    u  displays the unsigned integer in hexadecimal format.
    o  Displays variables in octal format.
    t  displays variables in binary format.
    a  displays variables in hexadecimal format. The
    c  displays variables in character format.
    f  Displays variables 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
        $ = 0x65
& nbsp;      
        (gdb) p/t i
         $26 = 1100101


V. View memory

You can use the Examine command (abbreviated X) to view the values in the memory address. The syntax for the x command looks like this:

X/<n/f/u> <addr>

N, F, and u are optional parameters.

n is a positive integer that indicates the length of memory displayed, that is, the contents of several addresses are displayed backwards from the current address.
The f represents the displayed format, see above. If the address refers to a string, then the format can be S, and if the place 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 replaced with the following characters, 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 with the memory address of the memory, reads and writes the specified byte, and takes it out as a value.

<addr> 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 double byte unit, 3 is three units, and U represents a hexadecimal display.


Six, automatic display you can set up a number of automatically displayed variables, when the program stops, or when you step tracking, these variables will automatically display. The associated GDB command is display.

Display <expr>
Display/<fmt> <expr>
Display/<fmt> <addr>

Expr is an expression that FMT represents the format of the display, addr represents the memory address, and when you set one or more expressions with display, GDB automatically displays the values of the expressions you set up if your program is stopped.

The format I and S are also supported by display, a very useful command is:

display/i $pc

$PC is the GDB environment variable that represents the address of the instruction,/I indicates that the output format is machine script, or assembly. So when the program stops, there will be source code and machine script corresponding to the situation, this is a very interesting function.

Here are some of the GDB commands associated with display:

Undisplay <dnums...>
Delete Display <dnums...>
Delete automatic display, dnums for the set up of the automatic explicit number. If you want to delete several at the same time, the number can be separated by a space, if you want to delete a range of numbers, you can use a minus sign (such as: 2-5)

Disable Display <dnums...>
Enable Display <dnums...>
Disable and Enalbe do not remove the settings that are automatically displayed, but only let them fail and recover.

Info display

View the display settings automatically. GDB will play a table and report to you, of course, how many automatic display settings are set in the debug, including the numbering, the expression, and whether to enable.

Source: http://blog.csdn.net/haoel/article/details/2883

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.