GDB debugging essence and Examples
Source: Unknown (11:18:05)
I. Column file list
1. List
(GDB) List line1, line2
Ii. Execution Program
To run the program to be debugged, run the Run Command, followed by any parameters sent to the program, including standard input and standard output specifiers (<and>) and shell wildcard (*,? .
If you use the Run Command without parameters, GDB will use the parameter you gave the previous run command again, which is very useful.
You can use the set ARGs command to modify the parameters sent to the program, and use the show ARGs command to view the list of its default parameters.
(GDB) set ARGs-B-x
(GDB) show ARGs
The backtrace command provides the backward trace function for the stack.
The backtrace command generates a list that contains valid processes starting from the most recent process and parameters for calling these processes.
Iii. Display Data
Use the print command to check the values of each variable.
(GDB) print P (P is the variable name)
The whatis command can display the type of a variable.
(GDB) whatis P
Type = int *
Print is a very powerful command of GDB. It can be used to display any valid expressions in the language to be debugged. In addition to variables in your program, expressions can also contain the following:
L call functions in the program
(GDB) print find_entry (1, 0)
L data structures and other complex objects
(GDB) print * table_start
$8 = {e = reference = '\ 000', location = 0x0, next = 0x0}
Historical components of the L value
(GDB) Print $1 ($1 is a history variable and can directly reference the value of $1 in the future)
L artificial Array
An array provides a way to display the content of a memory block (array section or dynamically allocated storage area. Early debugging programs did not have a good way to replace arbitrary pointers with an array. Just like a parameter, let's look at the 10 integers after the variable H in the memory. the syntax of a dynamic array is as follows:
Base @ Length
Therefore, to display the 10 elements after H, use H @ 10:
(GDB) print h @ 10
$13 = (-1,345, 23,-98,345, 0, 10)
4. breakpoint)
The break command (which can be abbreviated as B) can be used to set breakpoints in the debugging program. The command has the following four forms:
L break line-number causes the program to stop just before the specified row is executed.
L break function-name causes the program to stop before entering the specified function.
L break line-or-function if condition if the condition is true, the program stops when it reaches the specified row or function.
L break routine-name sets a breakpoint at the entrance of the specified routine
If the program is composed of many original files, you can set breakpoints in each original file instead of in the current original file. The method is as follows:
(GDB) Break filename: Line-Number
(GDB) Break filename: function-name
To set a conditional breakpoint, you can use the break if command as follows:
(GDB) break line-or-function if expr
Example:
(GDB) break 46 If testsize = 100
Continue running from the breakpoint: countinue command
5. breakpoint Management
1. display the current GDB breakpoint information:
(GDB) info break
It displays all the breakpoint information as follows:
Num type disp ENB address what
1 breakpoint keep y 0x000028bc in init_random at qsort2.c: 155
2 breakpoint keep y 0x0000291c in init_organ at qsort2.c: 168
(GDB)
2. delete a specified breakpoint:
(GDB) delete breakpoint 1
This command will delete the breakpoint numbered 1. If the parameter is not specified, all breakpoints will be deleted.
(GDB) delete breakpoint
3. Disable the use of a breakpoint
(GDB) Disable breakpoint 1
This command will disable breakpoint 1, and the (ENB) field of the breakpoint information will change to n
4. A breakpoint is allowed.
(GDB) Enable breakpoint 1
This command will allow breakpoint 1, and the (ENB) field of the breakpoint information will change to Y
5. Clear all breakpoints on a code line in the original file.
(GDB) Clean number
Note: number indicates the row number of a code line in the original file.
Vi. Check and assign values to variables
L whatis: identifies the array or variable type
L ptype: More powerful than whatis. It can provide a structure definition.
L set variable: Assign the value to the variable
L print not only displays the value of a variable, but also can be used to assign values.
7. One-step execution
L next
Do not enter the single-step execution
L step
One-step execution
If you want to exit a function and return it to its calling function, run the "finish" command.
8. function call
L call name: Call and execute a function.
(GDB) Call gen_and_sork (1234,1, 0)
(GDB) Call printf ("ABCD ")
$1 = 4
L finish the execution of the current function and display its return value (if any)
9. Machine language tools
There is a set of special GDB variables that can be used to check and modify the General registers of the computer. GDB provides the standard names of the four registers actually used in each computer:
L $ PC: program counter
L $ FP: frame pointer (current stack frame)
L $ SP: Stack pointer
L $ PS: processor status
10. Signal
GDB can usually capture most of the signals sent to it. By capturing the signals, it can decide what to do for running processes. For example, sending an interrupt signal to GDB by CTRL-C usually terminates GDB. However, you may not want to interrupt GDB. The real purpose is to interrupt the program running in GDB. Therefore, GDB needs to capture the signal and stop the program running, in this way, you can perform some debugging operations.
The handle command can control signal processing. It has two parameters: one is the signal name and the other is what to do when the signal is received. Several possible parameters are:
L when nostop receives a signal, do not send it to the program or stop the program.
L stop stops program execution when the signal is received to allow program debugging; displays a message indicating that the received signal is received (except for messages prohibited)
L print displays a message when receiving a signal
L when noprint receives the signal, do not display the message (and implicitly do not stop running the program)
L pass sends the signal to the program, allowing your program to process it, stop running, or take other actions.
L nopass stops running the program, but do not send signals to the program.
For example, assume that you intercept the sigpipe signal to prevent the program being debugged from receiving the signal. Once the signal arrives, the program is required to stop and notify you. To complete this task, use the following command:
(GDB) handle sigpipe stop print
Please note that the Unix signal name always uses uppercase letters! You can replace the signal name with the signal number.
If your program needs to perform any signal processing operations, you need to be able to test its signal processing program. Therefore, you need a simple method to send signals to the program, this is the task of the signal command. The parameter of this command is a number or name, such as SIGINT. Assuming your program has set a dedicated SIGINT (keyboard input, or CTRL-C; Signal 2) signal handler to capture a cleanup action, to test the signal handler, you can set a breakpoint and run the following command:
(GDB) Signal 2
Continuing with signal SIGINT (2)
The program continues to run, but the signal is transmitted immediately and the processing program starts to run.
11. Search for the original file
Search Text: displays the next line of the text string in the current file.
Reverse-search text: This command can display the first line containing text.
12. UNIX Interface
The shell command can start the Unix shell, the CTRL-D exit the shell, return to GDB.
13. Command history
To allow the use of historical commands, you can use the set history expansion on command.
(GDB) set history expansion on
Summary: Common gdb commands
Backtrace displays the current position in the program and stack trace indicating how to reach the current position (synonym: Where)
Breakpoint sets a breakpoint in the program
CD changes the current working directory
Clear deletes the breakpoint at the stop.
When commands hits a breakpoint, it lists the commands to be executed
Continue from the breakpoint
Delete deletes a breakpoint or monitoring point. It can also be used with other commands.
When the display program is stopped, variables and expressions are displayed.
Down the stack frame so that another function becomes the current function
Frame select the frame of the next continue command
Info displays various information related to the program
Another point of jump in the source program
Kill terminate the program running under the control of GDB
List lists the content of the original file corresponding to the program being executed
Next, execute the next source code line to execute a function.
Print: displays the value of a variable or expression.
PWD displays the current working directory
Pype displays the content of a Data Structure (such as a structure or C ++ class)
Quit exit GDB
Reverse-search reverse searches for regular expressions in the source file
Run
Search Regular Expressions in source files
Set variable assigns a value to the variable
Signal sends a signal to a running process
Step: Execute the next source code line. If necessary, enter the next function.
Undisplay display command, do not display expression
Until ends the current loop
Up the stack frame to make another function the current function.
Watch sets a monitoring point (Data breakpoint) in the program)
Whatis DISPLAY variable or function type
**************************************** ************
The GNU Debugger is called GDB. This program is an interactive tool that works in character mode. In the X Window System, there is a GDB front-end graphics tool called xxgdb. GDB is a powerful debugging program that can complete the following debugging tasks:
* Set breakpoints;
* The value of the Monitored Program variable;
* One-Step Program Execution;
* Modify the value of a variable.
Before you can use GDB to debug a program, you must use the-G option to compile the source file. You can define the cflags variable in makefile:
Cflags =-G
The following command is often used to run the gdb debugging program:
GDB progname
Enter help at the gdb prompt to list the command categories, including:
* Aliases: Command alias
* Breakpoints: breakpoint definition;
* Data: View data;
* Files: Specify and view files;
* Internals: maintenance command;
* Running: program execution;
* Stack: view the call stack;
* Statu: view the status;
* Tracepoints: trace program execution.
Enter help and the classification name of the command to obtain a detailed list of such commands.
Common gdb commands
Command explanation
Break num sets a breakpoint on the specified row.
BT displays all call stack frames. This command can be used to display the call sequence of a function.
Clear deletes a breakpoint set on a specific source file or line. The usage is clear filename: num.
Continue continues to execute the program being debugged. This command is used when the program stops running because of processing signals or breakpoints.
Display expr: The expression value is displayed after each program is stopped. An expression is composed of variables defined by the program.
File file to load the specified executable file for debugging.
Help name: displays the help information of a specified command.
Info break displays the current breakpoint list, including the number of times the breakpoint is reached.
Info files: displays detailed information about the file to be debugged.
Info func shows all function names.
Info local shows the local variable information in the function.
Info prog displays the execution status of the program to be debugged.
Info var displays all global and static variable names.
Kill the program being debugged.
List shows source code segments.
Make runs the make tool without exiting GDB.
Next executes a source code line forward without entering other functions.
Print expr: the value of the expression expr.
************************
-----------------
Listing a wrong C source code bugging. c
Code:
-----------------
1 # I nclude
2
3 static char buff [256];
4 static char * string;
5 Int main ()
6 {
7 printf ("Please input a string :");
8 gets (string );
9 printf ("\ Nyour string is: % s \ n", string );
10}
-----------------
The above program is very simple, the purpose is to accept the user input, and then print out the user input. This program uses an uninitialized string address string. Therefore, after compilation and running, the segment fault error will occur:
$ Gcc-O bugging-G bugging. c
$./Bugging
Please input a string: asfd
Segmentation fault (core dumped)
To find problems in the program, we use GDB and follow the steps below:
1. Run the gdb bugging command to load the bugging executable file;
2. Run the loaded bugging command;
3. Run the where command to view the errors in the program;
4. Use the LIST command to view the code near the call to the gets function;
5. the only cause of the gets function error is the variable string. Use the print command to view the string value;
6. In GDB, we can directly modify the value of the variable, as long as a valid pointer value is taken for the string, so we can set the breakpoint break 8 at line 8th;
7. Run the program again and stop it at line 8th. Then, we can use the set variable command to modify the value of string;
8. Continue running. The correct program running result is displayed.