Basic debug command:
l: Show the code of the source file
b linenum: Add breakpoints to the designated line
b func (function name): add break point to function func
info B: View breakpoint information
n: Single -step execution
r: Run, stop at the breakpoint
C: Execute to the next breakpoint
s: Enter the function to be called to execute
Finish: Jumping the function
d breakpoint number: delete the specified breakpoint (the breakpoint number is obtained by Info B just now)
P Val: Print Val’s value
P & Val: Print the address of Val
P ARR (array name): The elements used in the print array
p [email protected]: Print the element of the array with a pointer to the array (P is the pointer of the array)
BT: Display function call stack
Q: Introducing debugging
Debugging procedures
Use the PS command to find the process PID, and then use itgdb attach pidcommand debugging is running the process
Use GDB to debug multiple processes
- attach pid
- SET FOLLOW-FORK-MODE Child
- SET FOLLOE-FORK-MODE PARENT
Use GDB to debug the multi -thread
- info threads :Display the currently debugged threads, each thread will have a ID of GDB allocated, with “*” is currently debugging
- thread id:Switch to the specified thread
- set scheduler-locking off | on | step:
OFF: Without locking any threads, the threads used will be executed
ON: Only the currently commissioned thread will be executed
STEP: When performing a single step, only the current thread will execute
- show scheduler-locking: View the current lock thread mode
- thread apply all command(Can be BT): Let all of the debug thread execute commands
Let’s take a look at the Core file (for debugging the procedure of collapsed)
View the command of the system resource limit is ulimit -a
Now Core file size can see 0, and then now modify its valueulimit -c unlimited
Now writing a program to collapse it can quickly find the collapsed place through the core file.
Now editing a program that will collapse
After running, a core file will be generated, LS, a file in the current directory
Seeing that there will be a Core.3802 file, this is the information that collapsed, now check it out
Use command: GDB./core core.3802 (Here because the file I wrote is called Core, ./core should be the file you want)
result is as follows:
You can see that the 6th line of line P [2] = ’A’ is displayed earlier, wrong;
I will check the BT command next, showing the error of the last line. It can directly locate the wrong row, which is very convenient.
See a few more commands below
strace —–> Tracking system call
ltrace —–> Track library function
Strace -P PID tracking where is the block (that is, the system call function)
ltrace ./test Tracking procedure which library functions have been called
You can also set the affinity of the CPU, and you can specify which CPU is executed by the process (provided that the multi -core processor is set on the virtual machine)
taskset -p pid :View the process of PID runs on that CPU (if it is 4 cores, the CPU number is 0, 1, 2, 3)
Set CPU affinity:taskset -p 8 (mask) pid(The binary of 8 is0100, indicating that the process corresponding to PID has been executed on CPU No. 3. .
PS -LF: View the number of threads and ID
TOP -H: Represents a thread display