Debug Program with GDB (v)Category: Programming tools 2003-07-09 08:30 36503 People read review (4) Favorites report compiler language optimization C + + DELETE function
Viewing run-time data
———————
When you debug a program, when the program is stopped, you can use the Print command (the Shorthand command is P), or the synonym command inspect to see the current program's running data. The format of the Print command is:
Print <expr>
Print/<f> <expr>
<expr> is an expression that is the expression of the language of the program you are debugging (GDB can debug a variety of programming languages),<f> is the output format, for example, if you want to output an expression in a 16-based format, then the/x.
I. Expressions
Print, like many gdb commands, can accept an expression, and gdb evaluates the expression based on the data that the current program is running, and since it is an expression, it can be a const constant, variable, function, and so on in the current program run. Unfortunately, GDB cannot use macros that you have defined in your program.
The syntax of the expression should be the syntax of the language currently being debugged, because C + + is a popular type of language, so the examples in this article are about C/s. (and the section on debugging other languages with GDB, which I'll cover later)
In an expression, there are several operators supported by GDB, which can be used in any language.
@
is an array-related operator, which is described in more detail later.
::
Specify a variable in the file or in a function.
{<type>} <addr>
Represents an object that points to a type of memory address <addr>.
Second, the program variable
In GdB, you can view the values of the following three variables at any time:
1. Global variables (all files visible)
2. Static global variable (current file is visible)
3. Local variables (visible at current scope)
If you have a local variable that conflicts with a global variable (that is, a duplicate name), a local variable typically hides the global variable, that is, if a global variable and a local variable in a function have the same names, if the current stop is in the function, the value of the variable shown 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, view the value of global variable x in file f2.c:
GDB) P ' f2.c ':: X
Of course, the "::" operator will conflict with C + +, and GDB will automatically recognize "::" Whether it is a C + + operator, so you don't have to worry about exceptions when debugging C + + programs.
Also, it is important to note that if your program compiles with the optimization option turned on, some variables may not be accessible or value error codes can occur when debugging a program that has been optimized with GDB. This is normal, because the optimizer will delete your program, organize your program's sentence sequence, eliminate some meaningless variables, etc., so when GDB debugging such a program, the runtime of the instructions and you have written the instructions are different, there will be the results you can not imagine. When dealing with this situation, you need to turn off compilation optimizations when compiling the program. In general, almost all compilers support compile-optimized switches, for example, the GNU/C + + compiler, GCC, you can use the "-gstabs" option to solve this problem. See the compiler's usage documentation for compiler parameters as well.
three, array
Sometimes you need to look at the value of a contiguous amount of memory space. For example, a paragraph of an array, or the size of dynamically allocated data. You can use GDB's "@" operator, the left side of "@" is the value of the address of the first memory, and the right side of "@" you want to see the length of the memory. For example, there is a statement in your program that says:
int *array = (int *) malloc (len * sizeof (int));
So, during GDB debugging, you can display the value of this dynamic array as follows:
P *array@len
The left side of @ is the value of the array's first address, which is what the variable array points to, and the length of the data on the right, which is stored in the variable len, and its output is about 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 display the contents of all the data in the array directly with the print array name.
iv. output Format
in general, 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 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.
u Displays unsigned integers in hexadecimal format.
o Displays variables in octal format.
t displays variables in binary format.
a Displays the variable in hexadecimal format.
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 as x) to see the values in the memory address. The syntax for the x command is as follows:
X/<n/f/u> <addr>
N, F, and u are optional parameters.
n is a positive integer that represents the length of the displayed memory, which means that the contents of several addresses are displayed backwards from the current address.
F represents the format of the display, see above. If the address refers to a string, then the format can be s, if the ground ten 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 substituted with the following character, 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 by referring to the memory address, reads and writes the specified byte, and takes it as a value.
<addr> represents a memory address.
N/f/u three parameters can be used together. For example:
Command: X/3uh 0x54320, which reads from memory address 0x54320, h means double-byte units, 3 for three units, and U for hexadecimal display.
VI, automatic display
You can set up some automatically displayed variables that are automatically displayed when the program stops, or when you step through the tracks. The associated GDB command is display.
Display <expr>
Display/<fmt> <expr>
Display/<fmt> <addr>
Expr is an expression, FMT represents the format shown, addr represents a memory address, and when you set up one or more expressions with display, GDB automatically displays the values of the expressions you set as long as your program is stopped.
The formats I and S are also supported by display, and a very useful command is:
display/i $pc
$PC is the GDB environment variable, indicating the address of the instruction,/I means the output format is the machine script, that is, the assembly. So when the program stops, there will be the source code and machine instructions corresponding to the situation, this is a very interesting feature.
Here are some of the GDB commands associated with display:
Undisplay <dnums...>
Delete Display <dnums...>
Delete auto-display, dnums means the automatic explicit number that is set up. If you want to delete several, the number can be separated by a space, if you want to delete a range of numbers, you can use a minus sign (for example: 2-5)
Disable Display <dnums...>
Enable Display <dnums...>
Disable and Enalbe do not remove the settings that are automatically displayed, but just let them fail and resume.
Info display
View the information displayed automatically for display settings. GDB will print out a form to report to you, of course, how many auto-display settings are set in the debug, including, set number, expression, whether enable.
<-next page,
(All rights reserved, please specify the author and source when reproduced)