Use GDB debugging procedures (5)

zhaozj2021-02-08  210

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 / is an expression, is the expression of the language you debugging (GDB can debug multiple programming languages), is Output format, for example, if you want to output an expression in the format of the 16-in-1, then / x. First, expressions

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. {} represents an object that pointing to the memory address type Type. Second, program variables

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 / n, f, u is an optional parameter. N is a positive integer that represents the length of the memory, that is, the content of several addresses from the current address. F represents the format displayed, see above. If the address refers to a string, the format can be s, if the ten is the command address, then the format can be i. U Represents the number of bytes requested from the current address, if not specified, the GDB defaults to 4 Bytes. u Parameters can be replaced with the following characters, b represents a single byte, h represents double bytes, w represents four bytes, g represents eight bytes. When we specify the length of the byte length, the GDB will start from the memory address, read and write the specified byte, and take it as a value. represents a memory address.

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 Display / display / expr is an expression, FMT represents the format of the display, and addr represents the memory address, when you set up one or more expressions with Display After the style, as long as your program is stopped, GDB will automatically display the values ​​of these expressions you set. Format I and S are also supported by Display, a very useful command is: Display / i $ PC $ PC is the environment variable of GDB, indicating the address of the instruction, and / i indicates the output format of the machine command code, which is compilation. So when the program is stopped, the source code and the machine command code correspond to the case, which is a very interesting feature. Here is some GDB commands related to Display: undisplay Delete Display Delete automatically displayed, DNUMS means the set automatic explicit number. If you want to delete a few, the number can be separated by space. If you want to delete a range, you can use a minus sign (eg 2-5) Disable display enable display Disable and ENALBE do not delete automatic display settings, but just make it fails and recovered. Info Display View automatic display of Display settings. GDB will play a form and report how many automatic display settings set in the debug, which includes, set numbers, expressions, whether eNABLE. <- Previous Next -> (All rights reserved, please indicate the author and source)

转载请注明原文地址:https://www.9cbs.com/read-1117.html

New Post(0)