[My Linux, I call the shots! Detailed instructions debugging strace]

Contents:
(a) strace command Introduction
(b) strace command combat


(A) strace command Introduction
(1.1) strace command is mainly used to monitor the implementation of our program, when we run a program in user space Linux system, the program we will be running Linux system packaged in the form of a process and then participate in the operating system scheduler, so we can use the strace command to track our application it is associated with the operation of such interaction monitoring user processes and kernel processes, and tracking process system calls, signal transmission, status change . At this point we can use this command strace to debug specific reasons for our program error.
(1.2) in general our system calls include ① file and device access: open, close, read, write , ioctl , etc.; ② process management: fork, clone, execve, exit, etc.; ③ signal: signal, kill, etc.; ④ memory management: brk, mmap, mlock like; inter-process communication ⑤: semget, semaphores, message queues, and the like; network communication ⑥: socket, connect, etc.
Parameter Meaning and (1.3) strace command is as follows:
-C: execution time of each system call statistics, the number and frequency of errors
-d: strace output on standard error information
-f: trace invocation by a fork child process generated
-ff: If you provide -o filename, then track the results of all processes in the output to the appropriate filename.pid, pid is the process ID of each
-F: try tracking vfork call, when -f, vfork is not tracking
-h: brief output help information
-i: entry pointer output system calls
-q: disable the output from the message on
-r: print out a relative time with respect to each system call
-t: each line in the output before adding information on time
-tt: each line in the output before adding time information microsecond
-ttt: microsecond output, time in seconds
-T: displaying each call will elapse
-v: output of all system calls, some calls about the environment variables, status, input, output, calling the use of frequent, not the default output
-V: strace output version information
-x: Non-standard output string in hexadecimal form
-xx: all strings in hexadecimal output hexadecimal form
-a cloumn: set the return value output location, default 40
-e [expr]: Specifies a expression that is used to control how the track, the format [qualifier on =] VALUE1 [, value2] [!]
qualifier on only is able to trace, abbrev, verbose, raw, singal, [read], [write] one, value is used to define the symbols or numbers, default qualifier is the trace, the exclamation point symbol is negative. For example: -eopen equivalent to -e trace = open, represents the only track open call, -etrace = open express track in addition to open other calls, there are two special symbols all and none, note that some shell to use!! in the command execution history, so you want to use \
-e the trace = [the SET] just tracking the specified system call, for example: -e trace = open, close, rean, write indicates that only four tracking system calls. Default = All of the SET
-e the trace = [File] only trace about file system calls
-e trace = process only track information about the process control system calls
-e trace = network to track all the network related system calls
-e strace = signal trace all system-related signal system call
-e trace = ipc keep track of all the processes related to the communication system calls
-e abbrev = set set strace output result set of system calls, -v, etc. and abbrev = none, default is abbrev = all
parameters of the system call -e raw = set specified in hexadecimal
-e signal = set signal tracking system specified, the default is all, such as signal =! SIGIO, said they did not track SIGIO signal
-e read = set output read out data from a specified file, for example: read = 3,5 -e
-e = SET write data is written to the specified output file
-o filename written to the output file filename strace
-p pid pid tracking process specified
maximum length of the string -s strsize specify the output, the default is 32, has all of the output file name
-u username command to execute username UID and GID to be tracked
(1.4) we have in the system strace.c after editing a program file, and then write the following piece of code (FIG. 1-1), and then using the program strace.c gcc compiler will automatically generate an executable file a.out in the system, then after we execute a.out file system File error occurred and generate a test.dat (FIG. 1-2).
[My Linux, I call the shots!  Detailed instructions debugging strace]
[My Linux, I call the shots!  Detailed instructions debugging strace]
(1.5) then we can use the strace command to view process-related system calls when ./a.out execution.
# Strace ./a.out--- View ./a.out execution time process-related system calls
[My Linux, I call the shots!  Detailed instructions debugging strace]


(二)strace命令实战
(2.1)查看一个程序所有的open、close系统调用。
# strace -e open,close ./a.out---使用-e参数查看a.out执行文件的open和close的调用情况
[My Linux, I call the shots!  Detailed instructions debugging strace]
(2.2)查看每个系统调用消耗的时间。
# strace -T -e open,close ./a.out---我们使用-T参数可以查看系统每个系统调用所消耗的时间
[My Linux, I call the shots!  Detailed instructions debugging strace]
(2.3)统计系统调用次数、错误次数统计。
# strace -c -e open,close ./a.out---使用-c参数查看系统调用次数和错误次数统计
[My Linux, I call the shots!  Detailed instructions debugging strace]
(2.4)打印系统调用的时间戳。
# strace -t -T -e open,close ./a.out---使用-t命令可以查看系统调用的时间戳
# strace -tt -T -e open,close ./a.out---此时我们可以查看微秒级的系统调用的时间戳
[My Linux, I call the shots!  Detailed instructions debugging strace]
[My Linux, I call the shots!  Detailed instructions debugging strace]
(2.5)将跟踪日志保存到log文件中。
# strace -tt -T -e open,close -o log ./a.out---我们可以使用-o参数将跟踪日志保存到log文件中
[My Linux, I call the shots!  Detailed instructions debugging strace]
(2.6)最后我们再来分析一下使用strace -e来查看open系统调用后的问题与状态,当我们打开一个文件是成功的时候,那么系统会返回一个非负整数,所以系统在打开test.dat文件时,会返回一个3值,但是如果我们打开一个文件是失败的,那么系统会返回一个负数,所以此时系统打开并不存在的hello.dat文件时,返回的是-1值。由此我们便可以实现通过strace命令来查找程序运行报错原因的目标。
# Strace -e open ./a.out--- a program to view all of the open system call
[My Linux, I call the shots!  Detailed instructions debugging strace]

------ This concludes the article, thanks for reading ------

Guess you like

Origin blog.51cto.com/13613726/2461841