Summary of common commands based on Linux debugging tools strace and gdb

  • 2020-06-03 06:04:23
  • OfStack

strace and gdb are two commonly used debugging tools in Linux environment. Here is a summary of common parameters of these two tools in the process of use, for future use.
strace debugging tool
The strace tool is used to track system calls and signals received as the process executes, including parameters, return values, and execution time. In Linux, the user program must switch from user mode to kernel mode in order to access the system device, which is initiated and done through a system call.
Common parameters of strace:
-ES11en counts the execution time, number of calls, number of errors of each system call, and gives a report when the program exits
-ES13en pid tracks the specified process, and you can use multiple -ES15en to track multiple processes simultaneously
-ES17en filenamestrace default output to stdout, -ES21en can write the output to the specified file
-ES23en tracks the system calls of the child processes generated by fork
-ES26en is often used with the -ES27en option 1, where system calls produced by different processes (children) are output to each filename.pid file
-ES31en attempts to trace the SYSTEM call of the vfork child process. Note: When used in conjunction with -ES33en, vfork is not tracked
-ES36en expr outputs a filter expression to filter out unwanted strace results
-ES40en trace=set specifies to track system calls in set
-ES45en trace=network tracks all system calls related to the network
-ES49en strace=signal tracks all system calls associated with system signals
-ES53en trace=ipc tracks all system calls related to process communications
-ES57en signal=set specifies to track signals in set
-e read=set outputs data read from the specified file, for example -e read=3,5
-ES68en write=set outputs data written to the specified file, for example -ES71en write=1
-ES74en prints the relative time of each system call
-ES76en precedes each line in the output with the time information
-ES78en precedes each line in the output with time information to the microsecond level
-ES80en precedes each line in the output with the time information, and the output is a relative time
-ES82en specifies the length of the output string per line (default is 32)
strace Examples:
strace -t whoami # tracks the whoami executable and prints the execution time before each line outputs the result
strace-p 17151-ES93en 17152-ES94en 17153 # Also tracks processes 17151, 17152, 17153
strace-f-e trace=read, ES101en-ES102en 17151-ES103en log # trace process 17151 and the sub-process read and write system call, output to log file
gdb debugging tool
GDB is a powerful program debugging tool released by GNU. gcc compile time with the -ES115en parameter, which adds gdb debug information to the executable.
(1) info
i - lists gdb subcommands such as info break, info variables, info stack, etc.
(2) list [file:]function
l - view the context of the current line, default to 10 lines, or set to list the source code at a function.
(3) edit [file:]function
e, edit the current line, can also edit the source of a function.
(4) break [file:]function
b - Set a breakpoint that can be set on a line or a function.
(5) run [arglist]
r - Run the program until it stops at a breakpoint. The run command can be followed by the parameters required by the debugger.
(6) next
n - Single statement execution.
(7) continue
c, continue to run the program to the next breakpoint.
print (8)
p, which prints the value of the variable.
(9) bt
View function stack information.
enter (10)
Enter key, repeat the previous debug command.
(11) help [name]
Displays help information for the specified gdb command.
quit (12)
q - exit gdb.

Related articles: