First, the debugging instructions
1.list command
List LineNum shows the program around the LineNum line
The list function displays the source program for functions that have a program name
List shows the source program after the current line
List-Displays the source program in front of the current line
2.run (R): Run command
The run args Run command can be directly connected to a command-line parameter value, or it can be implemented by set args + parameter values before executing run
3.break (b): Break point
b linenum break point in a line
b +offset/-offset in front of the current line or in the back of offset stop
b filename:linenum break point in a certain line of a file
b filename:function stop at a function entrance of a file
b *address stop at the operating address of the program
b No arguments to stop on the next line
b where if condition stops on the line that satisfies the condition
Close breakpoint: Delete (d) Breakpoint-id
4. Single-Step command:
Step count executes count step at a time with function entry function
Next count executes count steps at once without entering the function
Finish runs the function, knowing that the current function completes the return and prints the stack address, return value, and parameter information when the function returns
Until exiting the loop body
5.continue command:
After the program is stopped, you can use the Continue (c) command to resume the program until the program ends or the next breakpoint is reached
6.print (P) command:
The Print command is for the output format of the variable:
-
- x display variables in hexadecimal format
- d display variables in decimal format
- U display unsigned integer exit in hexadecimal format
- o Display variables in octal format
- t display variables in binary format t displays variables in binary format
- A variable is displayed in hexadecimal format
- c Display variables in character format
- F Display variables by floating-point number format
7.watch command:
The watch command is typically used to observe whether the value of an expression (a variable is an expression) is changed, and if so, stop the program immediately.
How to set the Observer point:
Watch expr set an observer point for expression expr once the value of the expression changes, stop the program immediately
Rwatch expr When an expression is read Yes, stop the program
Awctah expr stops the program when the value of the expression is read or written back
8.exammine command:
View the value of the memory address. The syntax is: x/u addr
9.jump command
The jump command does not change the contents of the program stack, generally only jumps within the same function.
Jump Linespec Specifies the run point of the next statement, Linespec can be linenum,filename+linenum,+offset in several forms
Jump address jumps to the line of code
10.signal command
Use the signal signal name (such as SIGINT) this way to send the signal to the program, if the program registered Signal_handler function, you can also do the appropriate processing, to help debug the program
One .set command
Set args sets command-line arguments
Set ENV Environmentvarname=value sets environment variables. such as: Set env User=benben
12.call command
Call function forces the invocation of certain functions
Forces a function to be called, which displays the function return value (if the function returns a value other than void). The Print command can also complete this function
13.disassemble command
Disassembly command, view the source code of the machine code
Second, multi-process debugging
1. Debugging the child process individually
We can run the program first and then use PS-EF on the other end | grep "Main" (main here is the executable file name) searched for the child process PID and then started GDB, attaching it (attach) to the GDB debugger
Attach child-pid Use this command, run directly, and debug the normal program is no different.
Dettach Disengagement Process
2. Using the debugger option Follow-fork-mode
We know that if you do not set any options, GDB defaults to debugging the parent process. Debugger options Use the following:
Set Follow-fork-mode mode where the selectable values for mode are parent and child, respectively, to debug the parent process and subprocess.
Info inferiors Querying the process being debugged
Inferior processnum Switching process
By default, GDB only debugs the main process when debugging a multi-process program. But GDB (>v7.0) supports multi-process and simultaneous debugging, in other words, GDB can debug multiple programs at the same time. You only need to set Follow-fork-mode (default: Parent) and Detach-on-fork (default: On). We can also use the Catch fork instruction, which stops the program if the fork is abnormal.
Follow-fork-mode DETACH-ON-FORK&NBSP Description
parent &NB Sp on DEBUG main process only (gdb default)
Chil D on Debug sub-processes
parent off Simultaneous commissioning of two processes, gdb with main process, sub-process block at fork position
&NBS P Child off & nbsp Debug two simultaneous processes, GDB with subprocess, main process block at fork position
Setting method: Set Follow-fork-mode [Parent|child] Set detach-on-fork [On|off]
Three: Multi-threaded debugging
GDB debugging generally has two modes: all-stop mode and No-stop mode (no-stop mode is not supported before gdb7.0).
1.all-stop mode
In this mode, when your program stops at GDB for any reason, all threads will stop, not just the current thread. In general, GDB cannot step through all of the threads. Because thread scheduling is beyond the control of GDB. Whenever GDB stops your program, it will automatically switch to the thread that triggered the breakpoint.
2.no-stop mode (commonly used in network programming)
As the name implies, the startup mode is off. When the program is stopped in GDB, only the current thread will be stopped, and the other threads will continue to run. At this time step,next these commands only work on the current thread.
GDB supports two types of life: foreground (synchronous) and background (asynchronous). The difference is simple, synchronous in the output prompt will wait until the program report some thread has terminated the information, asynchronous is directly returned. So we need set target-async 1. Set pagination off do not appear with the Type <return> to continue prompt information. The final step is to open.
The following are common commands:
Info Threads Show All threads
Thread ID switches to the specified threads
Break Filename:linenum thread all sets a breakpoint on the corresponding line of all threads, note that if the main thread does not execute to that row and starts All-stop mode, the main thread executes n or s to switch past
Set scheduler-locking off|on\step default off, execution S or c other threads are also executed synchronously. On, only the current proportionate execution. Step, only when the front-line execution
Show Scheduler-locking shows the current mode
Thread apply all command to execute the consent command for each thread, such as BT. or thread apply 1 3 bt, that is, thread 1, 3 executes BT.
Mainly we want to use on the use of, such as No-stop mode, general multi-threaded debugging is very useful.
GDB debugging multi-threaded multithreaded procedure