strace是什么?

strace是一个用于诊断、调试和教学的Linux用户空间跟踪器。

strace有啥用?

支持跟踪进程执行时的系统调用,可以定位服务无法启动、共享内存异常等问题。
支持跟踪进程执行时接收的信号,可以定位进程异常退出等问题。

strace怎么用?

1.strace -e openat跟踪openat系统调用,可以查看命令执行时打开的文件和返回值。

loongson@linux:~$ strace -e openat lscpu > /dev/null
openat(AT_FDCWD, "/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
openat(AT_FDCWD, "/lib/loongarch64-linux-gnu/libsmartcols.so.1", O_RDONLY|O_CLOEXEC) = 3
openat(AT_FDCWD, "/lib/loongarch64-linux-gnu/libc.so.6", O_RDONLY|O_CLOEXEC) = 3
openat(AT_FDCWD, "/usr/lib/locale/locale-archive", O_RDONLY|O_CLOEXEC) = 3
openat(AT_FDCWD, "/proc", O_RDONLY|O_CLOEXEC) = 3
openat(3, "cpuinfo", O_RDONLY)          = 4
openat(AT_FDCWD, "/sys/devices/system/cpu", O_RDONLY|O_CLOEXEC) = 4
openat(4, "kernel_max", O_RDONLY|O_CLOEXEC) = 5
openat(4, "possible", O_RDONLY|O_CLOEXEC) = 5
openat(4, "present", O_RDONLY|O_CLOEXEC) = 5
openat(4, "online", O_RDONLY|O_CLOEXEC) = 5
openat(4, "dispatching", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
openat(3, "sysinfo", O_RDONLY)          = -1 ENOENT (No such file or directory)
openat(4, "cpu0/topology/thread_siblings", O_RDONLY|O_CLOEXEC) = 5
openat(4, "cpu0/topology/core_siblings", O_RDONLY|O_CLOEXEC) = 5
openat(4, "cpu0/topology/book_siblings", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
openat(4, "cpu0/topology/drawer_siblings", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
openat(4, "cpu0/topology/core_id", O_RDONLY|O_CLOEXEC) = 5
openat(4, "cpu0/topology/physical_package_id", O_RDONLY|O_CLOEXEC) = 5
openat(4, "cpu0/topology/book_id", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
openat(4, "cpu0/topology/drawer_id", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
openat(4, "cpu0/cache/index0/type", O_RDONLY|O_CLOEXEC) = 5
openat(4, "cpu0/cache/index0/level", O_RDONLY|O_CLOEXEC) = 5
openat(4, "cpu0/cache/index0/size", O_RDONLY|O_CLOEXEC) = 5
openat(4, "cpu0/cache/index0/shared_cpu_map", O_RDONLY|O_CLOEXEC) = 5
openat(4, "cpu0/cache/index1/type", O_RDONLY|O_CLOEXEC) = 5
openat(4, "cpu0/cache/index1/level", O_RDONLY|O_CLOEXEC) = 5
openat(4, "cpu0/cache/index1/size", O_RDONLY|O_CLOEXEC) = 5
openat(4, "cpu0/cache/index1/shared_cpu_map", O_RDONLY|O_CLOEXEC) = 5
openat(4, "cpu0/cache/index2/type", O_RDONLY|O_CLOEXEC) = 5
openat(4, "cpu0/cache/index2/level", O_RDONLY|O_CLOEXEC) = 5
openat(4, "cpu0/cache/index2/size", O_RDONLY|O_CLOEXEC) = 5
openat(4, "cpu0/cache/index2/shared_cpu_map", O_RDONLY|O_CLOEXEC) = 5
openat(4, "cpu0/cache/index3/type", O_RDONLY|O_CLOEXEC) = 5
openat(4, "cpu0/cache/index3/level", O_RDONLY|O_CLOEXEC) = 5
openat(4, "cpu0/cache/index3/size", O_RDONLY|O_CLOEXEC) = 5
openat(4, "cpu0/cache/index3/shared_cpu_map", O_RDONLY|O_CLOEXEC) = 5
openat(4, "cpu0/cpufreq/cpuinfo_max_freq", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
openat(4, "cpu0/cpufreq/cpuinfo_min_freq", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
openat(4, "cpu1/topology/thread_siblings", O_RDONLY|O_CLOEXEC) = 5
openat(4, "cpu1/topology/core_siblings", O_RDONLY|O_CLOEXEC) = 5
openat(4, "cpu1/topology/book_siblings", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
openat(4, "cpu1/topology/drawer_siblings", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
openat(4, "cpu1/topology/core_id", O_RDONLY|O_CLOEXEC) = 5
openat(4, "cpu1/topology/physical_package_id", O_RDONLY|O_CLOEXEC) = 5
openat(4, "cpu1/topology/book_id", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
openat(4, "cpu1/topology/drawer_id", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
openat(4, "cpu1/cache/index0/shared_cpu_map", O_RDONLY|O_CLOEXEC) = 5
openat(4, "cpu1/cache/index1/shared_cpu_map", O_RDONLY|O_CLOEXEC) = 5
openat(4, "cpu1/cache/index2/shared_cpu_map", O_RDONLY|O_CLOEXEC) = 5
openat(4, "cpu1/cache/index3/shared_cpu_map", O_RDONLY|O_CLOEXEC) = 5
openat(4, "cpu1/cpufreq/cpuinfo_max_freq", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
openat(4, "cpu1/cpufreq/cpuinfo_min_freq", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
openat(4, "cpu2/topology/thread_siblings", O_RDONLY|O_CLOEXEC) = 5
openat(4, "cpu2/topology/core_siblings", O_RDONLY|O_CLOEXEC) = 5
openat(4, "cpu2/topology/book_siblings", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
openat(4, "cpu2/topology/drawer_siblings", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
openat(4, "cpu2/topology/core_id", O_RDONLY|O_CLOEXEC) = 5
openat(4, "cpu2/topology/physical_package_id", O_RDONLY|O_CLOEXEC) = 5
openat(4, "cpu2/topology/book_id", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
openat(4, "cpu2/topology/drawer_id", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
openat(4, "cpu2/cache/index0/shared_cpu_map", O_RDONLY|O_CLOEXEC) = 5
openat(4, "cpu2/cache/index1/shared_cpu_map", O_RDONLY|O_CLOEXEC) = 5
openat(4, "cpu2/cache/index2/shared_cpu_map", O_RDONLY|O_CLOEXEC) = 5
openat(4, "cpu2/cache/index3/shared_cpu_map", O_RDONLY|O_CLOEXEC) = 5
openat(4, "cpu2/cpufreq/cpuinfo_max_freq", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
openat(4, "cpu2/cpufreq/cpuinfo_min_freq", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
openat(4, "cpu3/topology/thread_siblings", O_RDONLY|O_CLOEXEC) = 5
openat(4, "cpu3/topology/core_siblings", O_RDONLY|O_CLOEXEC) = 5
openat(4, "cpu3/topology/book_siblings", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
openat(4, "cpu3/topology/drawer_siblings", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
openat(4, "cpu3/topology/core_id", O_RDONLY|O_CLOEXEC) = 5
openat(4, "cpu3/topology/physical_package_id", O_RDONLY|O_CLOEXEC) = 5
openat(4, "cpu3/topology/book_id", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
openat(4, "cpu3/topology/drawer_id", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
openat(4, "cpu3/cache/index0/shared_cpu_map", O_RDONLY|O_CLOEXEC) = 5
openat(4, "cpu3/cache/index1/shared_cpu_map", O_RDONLY|O_CLOEXEC) = 5
openat(4, "cpu3/cache/index2/shared_cpu_map", O_RDONLY|O_CLOEXEC) = 5
openat(4, "cpu3/cache/index3/shared_cpu_map", O_RDONLY|O_CLOEXEC) = 5
openat(4, "cpu3/cpufreq/cpuinfo_max_freq", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
openat(4, "cpu3/cpufreq/cpuinfo_min_freq", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/sys/devices/system/node", O_RDONLY|O_CLOEXEC) = 5
openat(AT_FDCWD, "/usr/share/locale/locale.alias", O_RDONLY|O_CLOEXEC) = 7
openat(AT_FDCWD, "/usr/share/locale/en_US.UTF-8/LC_MESSAGES/util-linux.mo", O_RDONLY) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/usr/share/locale/en_US.utf8/LC_MESSAGES/util-linux.mo", O_RDONLY) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/usr/share/locale/en_US/LC_MESSAGES/util-linux.mo", O_RDONLY) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/usr/share/locale/en.UTF-8/LC_MESSAGES/util-linux.mo", O_RDONLY) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/usr/share/locale/en.utf8/LC_MESSAGES/util-linux.mo", O_RDONLY) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/usr/share/locale/en/LC_MESSAGES/util-linux.mo", O_RDONLY) = -1 ENOENT (No such file or directory)
openat(5, "node0/cpumap", O_RDONLY|O_CLOEXEC) = 6
openat(3, "sys/kernel/osrelease", O_RDONLY) = 5
openat(AT_FDCWD, "/sys/firmware/dmi/tables/DMI", O_RDONLY) = -1 EACCES (Permission denied)
openat(3, "device-tree/compatible", O_RDONLY) = -1 ENOENT (No such file or directory)
openat(3, "device-tree/compatible", O_RDONLY) = -1 ENOENT (No such file or directory)
openat(3, "bus/pci/devices", O_RDONLY)  = 5
openat(3, "bus/pci/devices", O_RDONLY)  = 5
openat(3, "bus/pci/devices", O_RDONLY)  = 5
openat(3, "sysinfo", O_RDONLY)          = -1 ENOENT (No such file or directory)
openat(3, "self/status", O_RDONLY)      = 5
openat(3, "sysinfo", O_RDONLY)          = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/usr/lib/loongarch64-linux-gnu/gconv/gconv-modules.cache", O_RDONLY) = 5
+++ exited with 0 +++

2.strace -e trace=signal跟踪信号发送和处理,可以定位进程异常退出。

loongson@linux:~$ strace -e trace=signal kill -9 `pidof wpsd`
rt_sigaction(SIGRTMIN, {sa_handler=0xfff78842a0, sa_mask=[], sa_flags=SA_SIGINFO}, NULL, 16) = 0
rt_sigaction(SIGRT_1, {sa_handler=0xfff7884358, sa_mask=[], sa_flags=SA_RESTART|SA_SIGINFO}, NULL, 16) = 0
rt_sigprocmask(SIG_UNBLOCK, [RTMIN RT_1], NULL, 16) = 0
kill(3639, SIGKILL)                     = 0
+++ exited with 0 +++

3.strace -c 统计系统调用执行的时间、次数等,可用于性能分析。

loongson@linux:~$ strace -c lscpu > /dev/null
% time     seconds  usecs/call     calls    errors syscall
------ ----------- ----------- --------- --------- ----------------
 74.04    0.000288           3        89           read
 11.31    0.000044           0       101        37 openat
  6.68    0.000026           0        67           close
  2.31    0.000009           0        55           fstat
  1.54    0.000006           0         8           mmap
  1.29    0.000005           1         3         3 ioctl
  1.29    0.000005           0        38        14 faccessat
  1.03    0.000004           0        50           fcntl
  0.51    0.000002           2         1           write
  0.00    0.000000           0         3           getdents64
  0.00    0.000000           0         2           lseek
  0.00    0.000000           0         1           newfstatat
  0.00    0.000000           0         1           uname
  0.00    0.000000           0         3           brk
  0.00    0.000000           0         1           munmap
  0.00    0.000000           0         1           execve
  0.00    0.000000           0         5           mprotect
------ ----------- ----------- --------- --------- ----------------
100.00    0.000389           0       429        54 total