GDB tips (4) - View performance data

View runtime data

When you debug the program, when the program is stopped, you can use the print command (abbreviated command p), or synonymous inspect command to view the performance data of the current program. Format print command is:

  • print <expr>
  • print /<f> <expr>

    <Expr> is an expression, is an expression of the language you are debugging a program (GDB can debug a variety of programming languages), <f> is the output format, for example, if we want to format the output according to the expression hexadecimal , then that is / x.

First, the expression

    print and many GDB commands like, you can accept an expression, GDB will be calculated based on the expression data of the current program running, since it is an expression, then it can be const constant current program running, variables, functions, etc. content. Unfortunately, GDB can not use macros you defined in the program.
   
Expression syntax should be the grammar of the language currently being debugged, because the C / C ++ is a popular type of language, so the examples in this article are on the C / C ++ is. (The chapter on debugging other languages used GDB, I will describe later)

In the expression, GDB supports several operators, they can be used in any language.

  • @

    Is a relevant operator and the array, there will be described in more detail later.
   

  • ::

    Specify a file or a variable function.
   

  • {<type>} <addr>

    It represents a memory address <addr> points to a type of the object type.

 

Second, the program variables

In GDB, you can check at any time the value of the following three variables:
    1, global variables (all files visible)
    2, static global variables (current file visible)
    3, local variables (current Scope visible)
   
    If your local and global variables conflict (that is, the same name), in general, is a local variable hides a global variable, that is to say, if a global variable and a function of local variables of the same name, if the current stopping point in the function, values exhibited by a variable print would be a local variable in a function. At this point if you want to see the value of a global variable, you can use the "::" operator:

  • file::variable
  • function::variable

You can specify the variables you want to see this form, which document or which function. For example, to view files f2.c global variables in the value of x:

  • gdb) p 'f2.c'::x

Of course, the "::" operator will conflict and in C ++, GDB can automatically identify the "::" is C ++ operators, so you do not have to worry about abnormal when debugging C ++ programs.
   
    Also, note that if you turn on the compiler optimization options when you are debugging with GDB when optimized program may occur some variables can not be accessed, the value of the case or the wrong code. This is normal, because the optimizer will excision your program, organize your statement sequence program, excluding meaningless variables, so this program in GDB debugger, the command runs and you have written instructions not the same, it will be the results you can not imagine. When the deal with this situation needs to close compiler optimization when compiling the program. In general, almost all compilers support compiler optimization switch, for example, GNU's C / C ++ compiler GCC, you can use the "-gstabs" option to solve this problem. About the compiler parameters, please see the compiler documentation.
   

Third, the array

  Sometimes, you need to see the value of a contiguous memory space. Such as a piece of the array, or the size of the data dynamically allocated. You can use the GDB "@" operator, left "@" is the first value of the memory address of the right "@" is you you want to see the length of the memory. For example, your program has this statement:

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

   Thus, in the GDB debugging process, you can show the value of this dynamic arrays with the following command:

  •     p *array@len

@ Left is the value of the first address of the array, which is variable array pointed to the content, the right is the length of the data, which is stored in the variable len, the output result, approximately looks like this is:

  •     (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 static arrays, you can directly print the name of the array, you can display the contents of all the data in the array.

   
Fourth, the output format

    Generally, GDB will output variable value in accordance with the type of variables. But you can also customize the format GDB output. For example, you want to output a hexadecimal integer, or binary integer variable to see this in the case of bit. To do this, you can use the GDB data display formats:

x display variables in hexadecimal format.
d show variable decimal format.
u show unsigned integer in hexadecimal format.
o octal variable display format.
display variable t in binary format.
a display variable in hexadecimal format.
c display variables as character format.
f Press display variable 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.41531145e-43
   
    (gdb) p/x i
    $25 = 0x65
   
    (gdb) p/t i
    $26 = 1100101


Fifth, view memory

You can use the examine command (abbreviated is x) to see the value of the memory address. X command syntax is as follows:

  • x/<n/f/u> <addr>

n, f, u is an optional parameter.

  • n is a positive integer representing the length of the display memory, the display content that is to say the current address from the address several rearwardly.
  • f represents the format, see above. If the address string is referred to, then the format may be s, if the instruction address is ten, then the format may be i.
  • u represents the number of bytes from the next address request, if not specified, GDB default is 4 bytes. u by the following parameters can be used instead of characters, b represents a single byte, h represents a two-byte, w represents four bytes, g represents eight bytes. When we specify the byte length, GDB will start from the memory means specified memory address, read and write the specified byte, and the value thereof is taken out as a.
  • <Addr> represents a memory address.

n / f / u three parameters may be used together. E.g:

Command: x / 3uh 0x54320 said read the content from the memory address 0x54320, h denotes a double-byte unit, 3 is three units, u denotes hexadecimal display.

   
Sixth, automatic display

You can set some variables are automatically displayed when the program stopped, or when you step through, these variables will be automatically displayed. GDB command is associated display.

  • display <expr>
  • display/<fmt> <expr>
  • display/<fmt> <addr>

expr is an expression, fmt representation format display, addr indicates a memory address, when you set up the display with one or more expressions, as long as your program is stopped, GDB will automatically show you these settings value of the expression.

I and s format is also supported display, a very useful command is:

  • display/i $pc

$ Pc is GDB environment variable indicating the address of the instruction, / i indicates the output format of machine code instructions, that is assembled. So when the program stops, the situation will be the source code and machine code instructions corresponding to, this is a very interesting feature.

Here are some of GDB commands and display relevant:

undisplay <dnums ...>
the Delete Run the display <dnums ...>
delete the auto show, dnums means the set up of automatic explicit numbers. To delete several simultaneous, numbers can be separated by a space, if you want to delete a number within the range can be expressed by a minus sign (eg: 2-5)

Run the display disable <dnums ...>
enable Run the display <dnums ...>
disable and delete enalbe not set the auto show, but simply let it fail and recovery.

info display
to view the display setting information is automatically displayed. GDB will play a table, of course, commissioning set up your report to show how many automatic settings, including the setting of the number, expression, whether enable.


Seven, set display options

GDB options on the display of more, and here I only mentioned the most commonly used options.

  • set print address
  • set print address on

    Open the address output, when the program information displaying function, GDB will show the address of the function parameters. The system default is open, such as:
   
    (gdb) f
    # 0 set_quotes (LQ = 0x34c78 "<<", RQ = 0x34c88 ">>")
        AT the input.c: 530
    530 IF (! Lquote = def_lquote)


set print address off
    off Parameter address display function, such as:
   
    (GDB) Print addr SET OFF
    (GDB) F
    # 0 set_quotes (LQ = "<<", RQ = ">>") to the input.c AT: 530.
    530. IF (lquote! = def_lquote)

  • show print address

    View the current address of display options are open.
   

  • set print array
  • set print array on

    Open the display array, when the array is open during the display, one row for each element, if it does not open, each element are separated by commas. This option is off by default. Associated with the following two commands, I will not repeat them here.

  • set print array off
  • show print array
  • set print elements <number-of-elements>

    This option is mainly set up the array, if your array is too big, then you can specify a <number-of-elements> to specify the maximum length of the data displayed, when it reaches this length, GDB will no longer show up down . If set to 0, it means no limit.
   

  • show print elements

    Viewing Options print elements of information.
   

  • set print null-stop <on/off>

    If this option is open, then when the display strings encountered terminator stops the show. This option defaults to off.
   

  • set print pretty on

    If you open printf pretty this option, when GDB displays the structure would be more beautiful. Such as:

        $1 = {
          next = 0x0,
          flags = {
            sweet = 1,
            sour = 1
          },
          meat = 0x54 "Pork"
        }

  • set print pretty off

    Printf pretty close this option, GDB will display structure is shown below:
   
        $. 1 = 0x0 = {Next, the flags = Sweet. 1 = {,}. 1 = The sour, Meat = 0x54 "Pork"}
       

  • show print pretty

    GDB is how to view the display structure.
   

  • set print sevenbit-strings <on/off>

    Setting character display, whether by "/ NNN" display format, if open, or character string data in / NNN display, such as "/ 065."

  • show print sevenbit-strings

    View character display is turned on.
   

  • set print union <on/off>

    When the structure display is provided, whether explicitly joint data therein. For example, the following data structure:
   
    typedef enum {Tree, the Bug} Species;
    typedef enum {Big_tree, the Acorn, Seedling} Tree_forms;
    typedef enum {Caterpillar, the Cocoon, Butterfly}
                  Bug_forms;
   
    struct Thing {
      Species IT;
      Union {
        Tree_forms Tree;
        Bug_forms bugs ;
      } form;
    };
   

  •     struct thing foo = {Tree, {Acorn}};

    When open the switch, after executing p foo command, will be displayed as follows:
        $. 1 = {IT = Tree, form = {Tree = the Acorn, bugs = the Cocoon}}
   
    When the switch is disabled after the execution of p foo command, as shown below : $
        = {IT = Tree. 1, form = {...}}

  • show print union

    Check the Commonwealth of data is displayed
   

  • set print object <on/off>

    In C ++, if the object is a pointer to its derived classes, if you open this option, GDB automatically in accordance with the rules of virtual method calls the display output, if you turn off this option, then, GDB on the matter of the virtual function table. This option defaults to off.

  • show print object

    View Settings object options.
   

  • set print static-members <on/off>

    This option means that, when the display content in a C ++ object is, whether to display static data members therein. The default is on.

  • show print static-members

    View static data member option settings.
   

  • set print vtbl <on/off>

    When this option is turned on, GDB will use more structured format when displaying the virtual function table. Which is off by default.
   

  • show print vtbl

    View virtual function display format options.

 

Eight, history

    When you view the data with GDB's print program is running, every print you will be GDB recorded. GDB will be $ 1, $ 2, $ 3 ..... such a way that you print command for each code on the number. So, you can use this number to access the previous expression, such as $ 1. The advantage of this feature brings is that if you previously entered a long expression, if you want to check the value of the expression, you can use history to access, eliminating the need to re-enter.

 

Nine, GDB environment variable

You can define your own variables in GDB debugging environment, the operating data used to save some debugging programs. To define a variable GDB's just very simple. Use the GDB command set. GDB and UNIX environment variables, just as beginning with $. Such as:

  • set $foo = *object_ptr

When using environment variables, GDB will create the variables when you first use, and after use, directly to their assignment. Environment variables do not type, you can define any type of a given environment variable. Includes structures and arrays.

  • show convenience

    The command to view all environment variables currently set.
   
This is a more powerful, interactive use environment variables and program variables, it will make the program more flexible and convenient debugging. E.g:

  •     set $i = 0
  •     print bar[$i++]->contents

Then, when you do not, print bar [0] -> contents, print bar [1] -> contents of the command input. After entering this command, only the knockout round, the previous statement is repeatedly executed, the environment variable is automatically incremented, by one output to complete the function.

 

Ten, register View

 

To see the value of the register, very simple, you can use the following command:

  • info registers

    View register situation. (Except floating point registers)

  • info all-registers

    View of all registers. (Including floating point registers)

  • info registers <regname ...>

    View the register specified circumstances.
   
    Placing the data register when the program runs, such as the current stack address instruction address (ip) program currently running program (sp) and the like. Cases you can also use the print command to access the register, just add a $ symbol before the register name on it. Such as: p $ eip.

 

Original Address: https://blog.csdn.net/haoel/article/details/2883

Published 175 original articles · won praise 262 · views 700 000 +

Guess you like

Origin blog.csdn.net/li_wen01/article/details/105223945