本页内容

  • Q1. 如何使用 strace 命令?
  • Q2. 如何理解 strace 输出?
  • Q3. 如何让 strace 打印指令指针?
  • Q4. 如何让 strace 为每个系统调用打印时间戳?
  • Q5. 如何为每行输出加上时钟时间前缀?
  • Q6. 如何让 strace 显示在系统调用中花费的时间?
  • Q7. 如何让 strace 打印摘要而非常规输出?
  • 结论

Linux 命令行提供了许多对软件开发者有用的工具,其中之一便是 strace —— 一个用于跟踪 Linux 系统调用和信号的命令。本教程将通过一些通俗易懂的示例,讲解 strace 的基础知识。

不过在此之前,值得一提的是,本文所有示例均已在 Ubuntu 22.04 LTS 和 Debian 11 机器上测试通过。

Linux 中的 strace 命令可让你跟踪系统调用和信号。其语法如下:

strace [OPTIONS] command

该工具的 man 手册页是这样解释它的:

       In  the simplest case strace runs the specified command until it exits.
       It intercepts and records the  system  calls  which  are  called  by  a
       process  and  the signals which are received by a process.  The name of
       each system call, its arguments and its return  value  are  printed  on
       standard error or to the file specified with the -o option.
       strace is a useful diagnostic, instructional, and debugging tool.  Sys?
       tem administrators, diagnosticians and trouble-shooters  will  find  it
       invaluable  for  solving problems with programs for which the source is
       not readily available since they do not need to be recompiled in  order
       to trace them.  Students, hackers and the overly-curious will find that
       a great deal can be learned about a system  and  its  system  calls  by
       tracing  even  ordinary programs.  And programmers will find that since
       system calls and signals are events  that  happen  at  the  user/kernel
       interface,  a close examination of this boundary is very useful for bug
       isolation, sanity checking and attempting to capture race conditions.

下面是一些问答形式的示例,可帮助你更好地理解 strace 命令的工作原理。

安装 strace 命令

在大多数系统上,strace 命令默认并未安装;要在 Debian 和 Ubuntu 上安装它,请运行以下命令:

sudo apt-get install strace

Q1. 如何使用 strace 命令?

基本用法很简单,只需执行 'strace' 并把一条命令作为输入即可。例如,我把它用在了 ls 命令上:

strace ls

以下是在我的系统上产生的输出:

Linux strace 命令入门与系统调用跟踪完整教程

Q2. 如何理解 strace 输出?

正如你在上一节的截图中所见,strace 命令会产生大量输出,因此你需要了解如何理解它。

以下摘自 man 手册页的片段给出了切中要害的解释:

       Each line in the trace contains the system call name, followed  by  its
       arguments  in parentheses and its return value.  An example from strac?
       ing the command "cat /dev/null" is:
           open("/dev/null", O_RDONLY) = 3
       Errors (typically a return value of -1) have the errno symbol and error
       string appended.
           open("/foo/bar", O_RDONLY) = -1 ENOENT (No such file or directory)
       Signals are printed as signal symbol and decoded siginfo structure.  An
       excerpt from stracing and interrupting the command "sleep 666" is:
           sigsuspend([] <unfinished ...>
           --- SIGINT {si_signo=SIGINT, si_code=SI_USER, si_pid=...} ---
           +++ killed by SIGINT +++

Q3. 如何让 strace 打印指令指针?

有一个 -i 选项,可让 strace 在系统调用发生时打印指令指针。

例如:

strace -i ls

输出如下:

Linux strace 命令入门与系统调用跟踪完整教程

可以看到,输出中每一行都打印了指令指针。

Q4. 如何让 strace 为每个系统调用打印时间戳?

有一个 -r 命令行选项,可让 strace 在每次进入系统调用时显示相对时间戳。该工具的 man 手册页称,它记录的是相邻系统调用开始时刻之间的时间差。

例如:

strace -r ls

以下是该命令产生的输出:

Linux strace 命令入门与系统调用跟踪完整教程

可以看到,每一行的开头都生成了一个相对时间戳。

Q5. 如何为每行输出加上时钟时间前缀?

如果希望 strace 输出的每一行都以时钟时间开头,可以使用 -t 命令行选项来实现。

例如:

strace -t ls

以下是该命令在我的系统上的输出:

Linux strace 命令入门与系统调用跟踪完整教程

可以看到,每一行的开头都打印了系统时间。

请注意,strace 还提供了两个相关的选项:

-tt         
If given twice, the time printed will include the microseconds.
-ttt        
If given thrice, the  time  printed  will  include  the microseconds and the leading portion will 
be printed as the number of seconds since the epoch.

Q6. 如何让 strace 显示在系统调用中花费的时间?

这可以通过 -T 命令行选项来实现。

例如:

strace -T ls

输出如下:

Linux strace 命令入门与系统调用跟踪完整教程

可以看到,在系统调用中花费的时间被打印在每一行的末尾。

Q7. 如何让 strace 打印摘要而非常规输出?

如果希望该工具生成摘要,可以使用 -c 命令行选项。

例如,以下命令:

strace -c ls

在我的系统上产生了如下输出:

% time     seconds  usecs/call     calls    errors syscall
------ ----------- ----------- --------- --------- ----------------
 93.66    0.000133           5        28           write
  6.34    0.000009           1        11           close
  0.00    0.000000           0         7           read
  0.00    0.000000           0        10           fstat
  0.00    0.000000           0        17           mmap
  0.00    0.000000           0        12           mprotect
  0.00    0.000000           0         1           munmap
  0.00    0.000000           0         3           brk
  0.00    0.000000           0         2           rt_sigaction
  0.00    0.000000           0         1           rt_sigprocmask
  0.00    0.000000           0         2           ioctl
  0.00    0.000000           0         8         8 access
  0.00    0.000000           0         1           execve
  0.00    0.000000           0         2           getdents
  0.00    0.000000           0         2         2 statfs
  0.00    0.000000           0         1           arch_prctl
  0.00    0.000000           0         1           set_tid_address
  0.00    0.000000           0         9           openat
  0.00    0.000000           0         1           set_robust_list
  0.00    0.000000           0         1           prlimit64
------ ----------- ----------- --------- --------- ----------------
100.00    0.000142                   120        10 total

可以看到,该摘要能让你大致了解每个系统调用发起了多少次调用,以及每个系统调用与时间相关的信息。

结论

这里只是浅尝辄止,因为 strace 命令还提供了许多其他功能。在你练习完本文讨论的内容之后,可查阅 strace 的 man 手册页以进一步了解该工具。