Use GDB debugging procedures (four)

zhaozj2021-02-08  263

View Stack Information -----

When the program is stopped, the first thing you need to do is to check where to stop. When your program calls a function, the address, function parameters, and the local variables in the function will be pressed into the "stack". You can use the GDB command to see the information in the current stack.

Here is some GDB commands for viewing the function call stack information:

Backtrace BT prints all information about the current function call stack. Such as: (gdb) BT # 0 func (n = 250) at tst.c: 6 # 1 0x08048524 in main (argc = 1, argv = 0xBfffff674) At TST.C: 30 # 2 0x400409ed in __libc_start_main () from / lib / lib / lib / lib / lib / lib / lib / lib / lib / /Libc.so.6 can be seen from the function of the call stack information: __ libc_start_main -> main () -> func () backtrace bt n is a positive integer, indicating that only the top of the stack Stack information of the N layer.

BackTrace <-n> bt <-n> -n table a negative integer, indicating that only the stack information of the N layer under the stack. If you want to see a layer of information, you need to switch the current stack, in general, the program is stopped, the top of the stack is the current stack, if you want to check the details of the stack, first you want to do it first. Switch the current stack.

Frame f n is an integer starting from 0 and is a layer number in the stack. For example: frame 0, represents the top of the stack, FRAME 1, indicating the second layer of the stack. Up indicates that the upper surface of the stack moves the N layer, and may not be hit N, indicating that the upward moves. Down indicates that the n layer is moved below, and may not beat N, indicating a downward movement.

The above command will print out information of the moving stack layer. If you don't want it to make it. You can use these three commands: SELECT-FRAME corresponds to the Frame command. Up-Silently corresponds to the UP command. Down-Silently corresponds to the Down command.

View the current stack of information, you can use the following GDB command:

Frame or F will print this information: the stack layer number, the current function name, the function parameter value, the file in the file and the line number, the function executed by the function. Info frame info f This command prints more detailed information of the current stack layer, but most is the internal address of the runtime. For example, the function address, the address of the call function, the address called function, the current function is written by, the function parameter address, the value, the address, and the like of local variables, etc. Such as: (gdb) info f Stack level 0, frame at 0xbffff5d4: eip = 0x804845d in func (tst.c: 6); saved eip 0x8048524 called by frame at 0xbffff60c source language c Arglist at 0xbffff5d4, args:. N = 250 Locals AT 0xBffff5D4, Previous Frame's SP is 0x0 Saved Registers: EBP AT 0xBffff5D4, EIP AT 0xBffFFF5D8 INFO ARGS Prints the parameter name and its value of the current function. INFO LOCALS prints all local variables and their values ​​in the current function. INFO CATCH prints an exception handling information in the current function. View Source ----- First, Display Source Codes

GDB can print out the source code of the debugprue, of course, be sure to add -g parameters when compiling, and compile the source program information into the execution file. Otherwise, you can't see the source program. When the program stops, the GDB will report that the program stopped on the first few lines of the file. You can use the list command to print the source code of the program. Let's take a look at the GDB command to view the source code. List Displays the source program around the Linenum line of the program. List Displays the source program of a function called function name FUNCTION. List displays the source program behind the current line. List - Displays the source program in front of the current row.

Generally, print the top 5 rows of the current row, if the display function is on the top 2 line 8 line, the default is 10 lines, of course, you can customize the range of display, use the following command to set a display source program The number of rows.

Set listsize Sets the number of rows that displays the source code. Show ListSize looks at the current ListSize settings.

The List command has the following usage:

List , Displays the source code between the first line to the LAST line. List, Displays the source code from the current line to the LAST line. List displays the source code.

Generally speaking, you can follow the following parameters after List:

line number. < Offset> The positive offset of the current line number. <-Offset> The negative shift of the current line number. Which file is of which file. Function Name. which function in which file is. <* Address> The address of the statement of the program runs in memory. Second, search source code

Not only that, GDB also provides commands for source code search:

Forward-Search search Search to the front.

REVERSE-Search All Searches. Among them, is the regular expression, and the main string matching mode, about the regular expression, I will not talk about it here, please check the relevant information.

Third, the path to specify the source file

At some point, in the execution program compiled with -g, just includes the name of the source file, no path name. GDB provides commands that allow you to specify the path to the source file to search for GDB.

Directory DIR Add a source file path to the current path. If you want to specify multiple paths, you can use ":", you can use ":" and Windows. Directory clears all custom source file search path information. Show Directories Displays the defined source file search path.

Fourth, the memory of the source code

You can use the Info Line command to view the address of the source code in memory. Info Line, you can follow the "Row", "Function Name", "File Name", "File Name: Function Name", this command prints the memory address of the specified source code at runtime, such as:

(GDB) info line tst.c: func line 5 of "TST.C" Starts at address 0x8048456 and ends at 0x804845d .

There is also a command that you can view the current execution of the source program, this command will take the command dump in the current memory. As shown below, the assembly code of the function FUNC is viewed.

(Gdb) disassemble func Dump of assembler code for function func: 0x8048450 : push% ebp 0x8048451 : mov% esp,% ebp 0x8048453 : sub $ 0x18,% esp 0x8048456 : MOVL $ 0x0 ,0xffffffc (% EBP) 0x804845d : MOVL $ 0x1, 0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff. (% EBP) 0x8048464 : MOV 0xFffffFf8 (% EBP),% EAX 0x8048467 : CMP 0x8 (% EBP),% EAX 0x804846A : jle 0x8048470 0x804846c : JMP 0x8048480 0x804846e : MOV% ESI, % ESI 0X8048470 : MOV 0xfffffffff8 (% EBP),% EAX 0x8048473 : add% EAX, 0xffffffffc (% EBP) 0x8048476 : INCL 0xFFFFFFFF8 (% EBP) 0x8048479 : JMP 0x8048464 0x804847B : NOP 0x804847C : LEA 0x0 (% ESI, 1),% ESI 0x8048480 : MOV 0xFffFFFFC (% EBP) % EDX 0 x8048483 : MOV% EDX,% EAX 0x8048485 : JMP 0x8048487 0x8048487 : MOV% EBP,% ESP 0x8048489 : POP% EBP 0x804848A : Ret end of assembler dump. <- Previous Next-> (copyright, please indicate the author and source)

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

New Post(0)