View runtime data ------ When you debug the program, when the program is parked, you can use the print command (the shortup command is p), or syncing the command inspect to view the current program running data. The format of the print command is: Print
PRINT and many GDB commands, you can accept an expression, and GDB calculates this expression according to the data running in the current program. Since it is an expression, it can be a CONST constant, variable, function, etc. in the current program. content. Unfortunately, GDB can't use the macro you defined in the program. The expression of the expression should be the syntax of the language currently debugged, since C / C is a public type of language, so the examples in this article are about C / C . (About the chapter of the other language with GDB, I will introduce later) in the expression, there are several gdb supported operators, which can be used in any language. @ Is an operator related to an array, which will have a more detailed description. :: Specify a variable in a file or a function. {
In GDB, you can view the values of the following three variables at any time: 1. Global variables (all files visible) 2, static global variables (current file visible) 3, local variables (current scope visible) if your part Variables and global variables have conflicts (which are heavy), in general, local variables hide global variables, that is, if a global variable and local variable in a function are the same name, if the current stop point is in the function, The value of the variable displayed with Print is the value of the local variable in the function. If you want to view the value of global variables, you can use the "::" operator: File :: variable function :: variable can specify the variable you want to view, which file is in or Which function is in. For example, view the value of the global variable x in file f2.c: GDB) P 'f2.c' :: x Of course, "::" Operations and C conflicts, GDB can automatically identify "::" Whether the operator of C , so you don't have to worry that there will be exceptions when debug C programs. In addition, it is important to note that if your program is compiled, the optimized option is turned on, then some variables may not be accessed when using GDB to be optimized, or the value of the value is incorrect. This is normal, because the optimizer will delete your program, organize your program's order, eliminate some meaningless variables, so when you debug this program, the runtime instruction and you write instructions. Different, there will be no results you think. When this is done, it is necessary to turn the compilation optimization when the compiler is compiled. In general, almost all compilers support compilation optimized switches, for example, GNU's C / C compiler GCC, you can use the "-gstabs" option to solve this problem. For parameters for the compiler, please check the instructions documentation for the compiler. Third, array
Sometimes you need to see a value of a continuous memory space. For example, a parameter of an array or the size of the data that is dynamically allocated. You can use the "@" operator, "@" operator, "@" is the value of the first memory address, "@", you want to see the length of memory. For example, there is such a statement in your program: int * array = (int *) malloc (len * sizeof (int)); then, in the GDB debugging process, you can display this dynamic array with the following command. :
P * array @ le
@ 的 边 边 数 地址 内容 内容 是 是 是 是 是 是 是 是 是 是 是 中 中 中 中 中 中 中 中 中 中 中 中 中 中 中 中 中 中 的 的 的 的 的 中 的 的 的 的 的 的Array @ le $ 1 = {2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 34, 36, 38, 40}
If it is a static array, you can display all the contents of all the data in the array can be displayed directly with the Print array name.
Fourth, output format
In general, GDB outputs the value of variables according to the type of variable. But you can also customize the format of the output of GDB. For example, you want to output an integer hexadecimal, or binary to view the situation in this integer variable. To do this, you can use GDB data display format: x Press hexadecimal format to display variables. D Display variables by decimal format. U Display unsigned integer in the hexadecimal format. O Display variables in eight-en-binary format. T Press binary format to display variables. A Display variables in hexadecimal format. C Press the character format display variable. F Display variables in floating point. (GDB) P / $ 22 = 0x65 (GDB) P / CI $ 23 = 101 'E' (GDB) P / Fi $ 24 = 1.41531145E-43 (GDB) P / Xi $ 25 = 0x65 ( GDB) P / Ti $ 26 = 1100101
Five, check the memory
You can use the examine command (short-write Yes x) to see the value in the memory address. The syntax of the x command is as follows: X /
The three parameters of n / f / u can be used together. For example: command: x / 3uh 0x54320 is represented, from the memory address 0x54320 read content, H is indicated by a double-byte as one unit, 3 represents three units, u means that pressing hexadecimal display. Six, automatic display
You can set some of the automatically displayed variables, or when the program is stopped, or when you are single-step tracking, these variables are automatically displayed. The associated GDB command is Display. Display