2014-12-12 Day Study Notes
GDB debugging
First, start DGB
1. Source code Compilation
Action: GCC takes the-G option to have the compiler save the symbol table (which corresponds to the program's variables and the memory address list of the line of code) in the resulting executable file. This allows you to reference the variable names and line numbers in the source code during a debugging session.
Example: Gcc–g–o main MAIN.C
2. Start
There are several ways to start gdb:
(1) gdb + programname
Description: ProgramName is the executable program file, usually in the current directory
Example: GDB main
(2) gdb + programname +core
Note: If a program has an error and a core file is generated, the core file is the file generated by the core dump after the program was executed illegally. A program must have the core file size open to survive core files, and use the Ulimite–a command to view the current executable program's core document size. method to open core file size: Ulimit-c Unlimited.
Example: GDB main core
(3) gdb + programname +pid
Note: If the program is a service program, you can run the service program through the PID,GDB will automatically attach up.
(4) gdb-command=z x
Description: You can specify a startup file when you call GDB, such as: $gdb-command=z x
Indicates that to run GDB on the executable x, you first read the command from File Z.
GDB Startup file
Sometimes we may need to exit GDB before we finish debugging, for example, to leave for a long time and not remain logged on to the computer. In order not to lose some information, you can place breakpoints and other commands set in a GDB startup file, and then load them automatically each time you start GDB.
The GDB startup file is named. Gdbinit by default. You can place a file in the home directory for general purposes, and another file in a directory dedicated to a particular project. For example, you can place a command that sets a breakpoint in the boot file of the latter directory and store some of the common macros developed in the. gdbinit file in the home directory. It is best not to put programming items in the home directory because the project-specific information cannot be placed in the. Gdbinit.
Executable program with parameters
If the executable program has parameters, after starting GDB, set args + parameters, you can set the parameters required for the program to run, show paths to view the running path of the program.
3. Run GDB in TUI mode
Specifying the-TUI option when invoking GDB or using the Ctrl+x+a key combination in gdb while in non-TUI mode allows the terminal screen to be split into the original text window and multiple child windows of the console
Second, view the code
The list command can be used to display the source code at the specified location. The list command affects the current line and the current file. The list command has several ways to specify the range of source code to display, which can be a line number, a function name, or even an instruction address. It is commonly used as follows:
list LineNum: Displays the code near the specified number of rows.
list function: Displays the code near the specified function.
list + lists the following lines of code for the current row.
List- lists the preceding lines of code for the current row.
List * addr: Displays code near the specified address.
By default, GDB displays the 10 lines of code at the specified location and before and after it, but this is a value that can be set.
Set listsize count: The number of source code displayed by the list command is up to count lines, and 0 means no limit on the number of rows.
Show listsize: Displays the value of the listsize.
Third, breakpoint settings
1. Set breakpoints
(1) Break line_number: sets a breakpoint on a line.
(2) Break function(function name): sets a breakpoint at the entry of a function (the first line of executable code).
(3) Break filename:function: sets a breakpoint at the entrance to the function functions of the source code file filename.
(4) conditionbreak_p_num(breakpoint number) cond(condition): turn a normal breakpoint into a conditional breakpoint
Example: Condition index = = 5
(5) Break line_num(line number) if cond(condition): with break If you can combine the break and condition commands into one step: for example: (GDB) Break If index = = 5
2. View Breakpoints
Infobreak: Show All breakpoint information
3. Delete breakpoint:
(1) deletebreak_point_num: Delete breakpoint with number Break_point_num;
(2) Delete: Remove all breakpoints;
(3) Clear function: Delete breakpoints set at function functions
(4) clearfilename:funtion Delete breakpoints set at function function of filename file
(5) Clear Line_number: Delete breakpoint set on Line_number line
(6) clearfilename:line_number: Delete the breakpoint set on the Line_number line of the filename file
4. Disabling and enabling breakpoints
(1) disablebreakpoint-list(is a space-delimited number of breakpoint identifiers): Disables breakpoints
(2) Disable: Disables all existing breakpoints
(3) enablebreakpoint-list: Enable breakpoints
(4) Enable Oncebreakpoint-list: Disable after gdb pauses execution next time
5. Breakpoint Command List
Let GDB automatically complete a task by automatically executing a set of commands each time it arrives at a breakpoint.
To set the command list using the commands command:
Commands Breakpoint_number
...
Commands
...
End
For example, the following code:
I'm going to look at the values and the order passed to the Fibonacci function, but I don't want to insert the printf statement in the program and recompile the code, you can do this:
Set a breakpoint at the entrance of the function and set the command list:
The printf command is similar to the printf function in C, except that parentheses are optional.
Operation Result:
If the GDB output is too verbose, you can use the Silent command, just add it to the beginning of the SET command list.
Four, commissioning
1. Start : Run
2. View Stack frame : Frame num (stack frame number)
Note the stack frame number sequence, the frame of the currently executing function is numbered 0, its parent frame (that is, the caller's stack frame of the function) is numbered 1, the parent frame is numbered 2, and so on.
Jumps to the next parent frame in the call stack: up
leads to the opposite direction: down
Displays the entire stack, which is the collection of all the frames that currently exist: BackTrace
3. Output Current value : Print or P
4. Browse previous GDB commands : Previous ctrl+p, next Ctrl + N
5, you can directly press ENTER to execute the most recently executed command
6. Operation
(1) Stepping: Step (s), Next (n), executes only to the next line of code and then pauses again.
Note the difference: When a function is called, step enters the function, next causes the next pause to appear after the function is called. Next is called a step over (stepping-over) function, and step is called a single-step-into (stepping-into) function.
Both next and step can use an optional numeric parameter that represents the additional number of rows to be executed with next or step.
(2) Implementation of the unconditional recovery program: Continue (c)
Until another breakpoint is encountered or the program ends.
Continue can accept an optional numeric parameter n, which requires GDB to ignore the following n breakpoints.
(3) Restore with Finish (Fin) or until (U) command.
The finish command instructs GDB to resume execution until exactly after the current frame is complete.
The until command is typically used to complete a loop that is being executed without further pausing in the loop (except for mid-break points in the loop). Until executes the remainder of the loop (or pauses if a breakpoint is encountered), allowing GDB to pause at the first line of code following the loop.
The until command can also accept a position in the source code as a parameter, with the use of the break command.
For example, the following code listing SWAPFLAW.C:
If GDB triggers a breakpoint at the entrance of the main function, you can use the following commands to easily make the program execute to the entry of swap ():
Until 13
Until swap
Until Swapflaw.c:13
Until Swapflaw.c:swap
7. Exit : Quit
GDB Common operations