Debug the program with GDB (6)

Source: Internet
Author: User
Tags print object

 

7. Set display options

There are many display options in GDB. Here I will only list most of the commonly used options.

Set print address
Set print address on
Open the address output. When the program displays the function information, GDB displays the function parameter address. The system is enabled by default, such:

(GDB) f
#0 set_quotes (SCSI = 0x34c78 "<", RQ = 0x34c88 "> ")
At input. C: 530
530 if (lquote! = Def_lquote)

Set print address off
Disable function parameter address display, for example:

(GDB) set print ADDR off
(GDB) f
#0 set_quotes (SCSI = "<", RQ = ">") at input. C: 530
530 if (lquote! = Def_lquote)

Show print address
Check whether the current address display option is enabled.

Set print Array
Set print array on
Open the array display. When the array is displayed, each element occupies one row. If it is not opened, each element is separated by a comma. This option is disabled by default. The two related commands are as follows.

Set print array off
Show print Array

Set print elements <number-of-elements>
This option mainly sets the array. If your array is too large, you can specify a <number-of-elements> to specify the maximum length of the data display, when this length is reached, GDB will no longer display it down. If it is set to 0, it indicates no restriction.

Show print Elements
View the options of print elements.

Set print null-Stop <on/off>
If this option is enabled, when the string is displayed, the end character is displayed. This option is off by default.

Set print pretty on
If you enable the printf pretty option, it looks pretty when GDB displays the struct. For example:

$1 = {
Next = 0x0,
Flags = {
Sweet = 1,
Sour = 1
},
Meat = 0x54 "pork"
}

Set print pretty off
Disable the printf pretty option. GDB displays the struct as follows:

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

Show print pretty
View how GDB displays struct.


Set print sevenbit-strings <ON/OFF>
Set whether to display characters in the/NNN format. If yes, the string or character data is displayed in the/NNN format, for example, "/065 ".

Show print sevenbit-strings
Check whether the character display switch is enabled.

Set print Union <on/off>
Sets whether to explicitly display the consortium data in the struct. For example, the following data structure is available:

Typedef Enum {tree, bug} species;
Typedef Enum {big_tree, Acorn, seedling} tree_forms;
Typedef Enum {Caterpillar, cocoon, butterfly}
Bug_forms;

Struct thing {
Species it;
Union {
Tree_forms tree;
Bug_forms bug;
} Form;
};

Struct thing Foo = {tree, {acorn }};

When this switch is enabled, the following information is displayed after the P Foo command is executed:
$1 = {It = tree, form = {tree = Acorn, bug = cocoon }}

When the P Foo command is executed, the following information is displayed:
$1 = {It = tree, form = {...}}

Show print Union
View the display mode of the consortium data

Set Print Object <On/Off>
In C ++, if an object pointer points to its derived class, if this option is enabled, GDB will automatically display the output according to the rules of the virtual method call. If this option is disabled, GDB does not care about the virtual function table. This option is off by default.

Show Print Object
View the settings of object options.

Set print static-members <ON/OFF>
This option indicates whether static data members are displayed when the content of a C ++ object is displayed. The default value is on.

Show print static-members
View the static data member option settings.

Set print vtbl <On/Off>
When this option is enabled, GDB will display the virtual function table in a more regular format. It is disabled by default.

Show print vtbl
View the options of the virtual function display format.


VIII. History

When you use GDB print to view the data when the program runs, each print will be recorded by GDB. GDB will compile the numbers for each print command in the form of $1, $2, $3. Therefore, you can use this number to access previous expressions, such as $1. The benefit of this function is that if you have previously entered a long expression and want to view the value of this expression, you can use the history to access it, saves Repeated input.


IX. GDB Environment Variables

You can define your own variables in the debugging environment of GDB to save running data in some debugging programs. It is easy to define a GDB variable. Use the SET command of GDB. The environment variable of GDB is the same as that of UNIX, and starts with $. For example:

Set $ Foo = * object_ptr

When using environment variables, GDB will create this variable when you use it for the first time. In future use, it will directly swap its values. The environment variable has no type. You can define any type for the environment variable. Including struct and array.

Show convenience
This command is used to view all the environment variables currently set.

This is a powerful function. The interaction between environment variables and program variables makes program debugging more flexible and convenient. For example:

Set $ I = 0
Print bar [$ I ++]-> Contents

Therefore, you do not need to enter the command in print bar [0]-> contents, print bar [1]-> contents. After you enter such a command, you only need to press enter to repeat the previous statement and the environment variables will be automatically accumulated to complete the output-by-one function.


10. View registers

To view the register value, run the following command:

Info registers
View the register information. (Except floating-point registers)

Info all-registers
View All registers. (Including floating-point registers)

Info registers <regname...>
View the status of the specified register.

The register contains the data when the program is running, such as the instruction address (IP) that the program is currently running, and the current stack address (SP) of the program. You can also use the print command to access the register. You only need to add a $ symbol before the register name. For example, p $ EIP.

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