Today I will share with you a summary of commonly used commands based on the Linux debugging tools strace and gdb

Today I will share with you a summary of commonly used commands based on the Linux debugging tools strace and gdb

Strace and gdb are two commonly used debugging tools in the Linux environment. Here is a summary of the commonly used parameters of these two tools during personal use, which is reserved for future use.

strace debugging tool

The strace tool is used to track system calls and received signals during process execution, including parameters, return values, and execution time. In Linux, a user program must switch from user mode to kernel mode to access system equipment, which is initiated and completed through system calls.

Common parameters of strace:

-c Count the execution time, number of calls, and number of errors for each system call, and report when the program exits

-p pid track the specified process, you can use multiple -p to track multiple processes at the same time

-o filename strace outputs to stdout by default, -o can write the output to the specified file

-f traces the system calls of the child process spawned by fork

-ff is often used with the -o option, system calls generated by different processes (subprocesses) are output to each file

-F Try to trace the vfork child process system call. Note: When used with -f at the same time, vfork will not be traced

-e expr output filter expression, you can filter out strace results that you don't want to output

-e trace=set specifies the system call in the trace set

-e trace=network trace all system calls related to the network

-e strace=signal track all system calls related to system signals

-e trace=ipc trace all system calls related to process communication

-e signal=set specifies the signal in the trace set

-e read=set output the data read from the specified file, for example -e read=3,5

-e write=set output the data written to the specified file, for example -e write=1

-r print the relative time of each system call

-t Add time information before each line in the output

-tt Add time information before each line in the output, the time is accurate to the microsecond level

-ttt Add time information before each line in the output, and the output is relative time

-s specifies the length of each line of output string (the default is 32)

Examples of strace usage:

strace -t whoami #Track the whoami executable program, print the execution time before each line of output

strace -p 17151 -p 17152 -p 17153 #Tracking processes 17151, 17152, 17153 at the same time

strace -f -e trace=read,write -p 17151 -o log #track process 17151 and the read and write system calls in the child process, output to the log file

gdb debugging tool

GDB is a powerful UNIX program debugging tool released by the GNU open source organization. Adding the -g parameter when gcc is compiled can add gdb debugging information to the executable program.

(1) info

Abbreviation: i, lists the information of gdb subcommands, such as info break, info variables, info stack, etc.

(2) list [file:]function

Abbreviation: l, view the context of the current line, the default is 10 lines, you can also set to list the source code at a certain function.

(3) edit [file:]function

Abbreviation: e, edit the current line, or edit the source code of a function.

(4) break [file:]function

Abbreviation: b, set a breakpoint, which can be set at a certain line or a certain function.

(5) run [arglist]

Abbreviation: r, run the program to stop at the breakpoint, after the run command, you can add the parameters needed by the debugger.

(6) next

Abbreviation: n, single statement execution.

(7) continue

Abbreviation: c, continue to run the program to the next breakpoint.

(8) print

Abbreviation: p, print the value of the variable.

(9) bt

View function stack information.

(10) enter

Press Enter to repeat the last debugging command.

(11) help [name]

Display the help information of the specified gdb command.

(12) quit

Abbreviation: q, exit gdb.