linux strace
linux strace
reference:
man strace
https://strace.io/
https://man7.org/linux/man-pages/man1/strace.1.html
Linux strace命令:https://www.cnblogs.com/ggjucheng/archive/2012/01/08/2316692.html
Linux中的strace命令:https://zhuanlan.zhihu.com/p/517938702
strace实现原理:ptrace系统调用:https://blog.csdn.net/Rong_Toa/article/details/109825818
strace实现原理:https://blog.csdn.net/shandianling/article/details/17033299
example:
timeout 10 strace -c -f -p 1577
root@SOC:/wyk$
root@SOC:/wyk$ strace -tt -T cat /proc/uptime
15:03:46.874313 execve("/bin/cat", ["cat", "/proc/uptime"], 0xffffdce864f8 /* 18 vars */) = 0 <0.000487>
15:03:46.875340 brk(NULL) = 0x166d0000 <0.000049>
15:03:46.875511 faccessat(AT_FDCWD, "/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory) <0.000076>
15:03:46.875839 openat(AT_FDCWD, "/app/lib/tls/aarch64/libresolv.so.2", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory) <0.000078>
15:03:46.876110 newfstatat(AT_FDCWD, "/app/lib/tls/aarch64", 0xfffff050acb0, 0) = -1 ENOENT (No such file or directory) <0.000047>
15:03:46.876249 openat(AT_FDCWD, "/app/lib/tls/libresolv.so.2", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory) <0.000049>
15:03:46.876407 newfstatat(AT_FDCWD, "/app/lib/tls", 0xfffff050acb0, 0) = -1 ENOENT (No such file or directory) <0.000046>
15:03:46.876538 openat(AT_FDCWD, "/app/lib/aarch64/libresolv.so.2", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory) <0.000060>
15:03:46.876677 newfstatat(AT_FDCWD, "/app/lib/aarch64", 0xfffff050acb0, 0) = -1 ENOENT (No such file or directory) <0.000045>
15:03:46.876803 openat(AT_FDCWD, "/app/lib/libresolv.so.2", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory) <0.000047>
15:03:46.876932 newfstatat(AT_FDCWD, "/app/lib", {st_mode=S_IFDIR|0755, st_size=4096, ...}, 0) = 0 <0.000051>
15:03:46.877108 openat(AT_FDCWD, "/app/base/lib/tls/aarch64/libresolv.so.2", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory) <0.000051>
15:03:46.877246 newfstatat(AT_FDCWD, "/app/base/lib/tls/aarch64", 0xfffff050acb0, 0) = -1 ENOENT (No such file or directory) <0.000044>
15:03:46.877366 openat(AT_FDCWD, "/app/base/lib/tls/libresolv.so.2", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory) <0.000046>
15:03:46.877494 newfstatat(AT_FDCWD, "/app/base/lib/tls", 0xfffff050acb0, 0) = -1 ENOENT (No such file or directory) <0.000050>
15:03:46.877629 openat(AT_FDCWD, "/app/base/lib/aarch64/libresolv.so.2", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory) <0.000045>
15:03:46.877776 newfstatat(AT_FDCWD, "/app/base/lib/aarch64", 0xfffff050acb0, 0) = -1 ENOENT (No such file or directory) <0.000047>
15:03:46.877906 openat(AT_FDCWD, "/app/base/lib/libresolv.so.2", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory) <0.000045>
15:03:46.878042 newfstatat(AT_FDCWD, "/app/base/lib", {st_mode=S_IFDIR|0775, st_size=4096, ...}, 0) = 0 <0.000048>
15:03:46.878191 openat(AT_FDCWD, "/app/algo/lib/tls/aarch64/libresolv.so.2", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory) <0.000061>
15:03:46.878334 newfstatat(AT_FDCWD, "/app/algo/lib/tls/aarch64", 0xfffff050acb0, 0) = -1 ENOENT (No such file or directory) <0.000044>
15:03:46.878676 openat(AT_FDCWD, "/app/algo/lib/tls/libresolv.so.2", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory) <0.000067>
15:03:46.878846 newfstatat(AT_FDCWD, "/app/algo/lib/tls", 0xfffff050acb0, 0) = -1 ENOENT (No such file or directory) <0.000045>
15:03:46.878974 openat(AT_FDCWD, "/app/algo/lib/aarch64/libresolv.so.2", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory) <0.000048>
15:03:46.879111 newfstatat(AT_FDCWD, "/app/algo/lib/aarch64", 0xfffff050acb0, 0) = -1 ENOENT (No such file or directory) <0.000089>
15:03:46.879289 openat(AT_FDCWD, "/app/algo/lib/libresolv.so.2", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory) <0.000049>
15:03:46.879423 newfstatat(AT_FDCWD, "/app/algo/lib", {st_mode=S_IFDIR|0775, st_size=4096, ...}, 0) = 0 <0.000050>
15:03:46.879581 openat(AT_FDCWD, "tls/aarch64/libresolv.so.2", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory) <0.000052>
15:03:46.879716 openat(AT_FDCWD, "tls/libresolv.so.2", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory) <0.000043>
15:03:46.879850 openat(AT_FDCWD, "aarch64/libresolv.so.2", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory) <0.000034>
15:03:46.879977 openat(AT_FDCWD, "libresolv.so.2", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory) <0.000056>
15:03:46.880113 openat(AT_FDCWD, "/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3 <0.000055>
15:03:46.880272 fstat(3, {st_mode=S_IFREG|0644, st_size=43290, ...}) = 0 <0.000041>
15:03:46.880408 mmap(NULL, 43290, PROT_READ, MAP_PRIVATE, 3, 0) = 0xffff8f9c0000 <0.000054>
15:03:46.880528 close(3) = 0 <0.000040>
15:03:46.880648 openat(AT_FDCWD, "/lib/libresolv.so.2", O_RDONLY|O_CLOEXEC) = 3 <0.000059>
15:03:46.880820 read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0\267\0\1\0\0\00008\0\0\0\0\0\0"..., 832) = 832 <0.000049>
15:03:46.880969 fstat(3, {st_mode=S_IFREG|0755, st_size=80504, ...}) = 0 <0.000042>
15:03:46.881097 mmap(NULL, 154080, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0xffff8f990000 <0.000050>
15:03:46.881256 mmap(0xffff8f9b0000, 65536, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x10000) = 0xffff8f9b0000 <0.000067>
15:03:46.881463 close(3) = 0 <0.000047>
15:03:46.881609 openat(AT_FDCWD, "/app/lib/libc.so.6", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory) <0.000057>
15:03:46.881762 openat(AT_FDCWD, "/app/base/lib/libc.so.6", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory) <0.000048>
15:03:46.881898 openat(AT_FDCWD, "/app/algo/lib/libc.so.6", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory) <0.000049>
15:03:46.882036 openat(AT_FDCWD, "tls/aarch64/libc.so.6", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory) <0.000050>
15:03:46.882173 openat(AT_FDCWD, "tls/libc.so.6", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory) <0.000043>
15:03:46.882312 openat(AT_FDCWD, "aarch64/libc.so.6", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory) <0.000046>
15:03:46.882567 openat(AT_FDCWD, "libc.so.6", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory) <0.000061>
15:03:46.882747 openat(AT_FDCWD, "/lib/libc.so.6", O_RDONLY|O_CLOEXEC) = 3 <0.000056>
15:03:46.882894 read(3, "\177ELF\2\1\1\3\0\0\0\0\0\0\0\0\3\0\267\0\1\0\0\0\360\20\2\0\0\0\0\0"..., 832) = 832 <0.000092>
15:03:46.883087 fstat(3, {st_mode=S_IFREG|0755, st_size=1405808, ...}) = 0 <0.000045>
15:03:46.883245 mmap(NULL, 1478664, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0xffff8f820000 <0.000054>
15:03:46.883420 mmap(0xffff8f980000, 65536, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x150000) = 0xffff8f980000 <0.000079>
15:03:46.883671 close(3) = 0 <0.000072>
15:03:46.883996 mprotect(0xffff8fa10000, 65536, PROT_READ) = 0 <0.000059>
15:03:46.884136 munmap(0xffff8f9c0000, 43290) = 0 <0.000048>
15:03:46.884307 getuid() = 0 <0.000046>
15:03:46.884458 openat(AT_FDCWD, "/proc/uptime", O_RDONLY) = 3 <0.000080>
15:03:46.884666 sendfile(1, 3, NULL, 16777216311.67 522.52
) = 14 <0.000091>
15:03:46.884837 sendfile(1, 3, NULL, 16777216) = 0 <0.000033>
15:03:46.884977 close(3) = 0 <0.000054>
15:03:46.885251 exit_group(0) = ?
15:03:46.885408 +++ exited with 0 +++
root@SOC:/wyk$
root@SOC:/wyk$
root@SOC:/wyk$
root@SOC:/wyk$
root@SOC:/wyk$ strace -tt -T -e trace=openat cat /proc/uptime
15:04:38.908857 openat(AT_FDCWD, "/app/lib/tls/aarch64/libresolv.so.2", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory) <0.000149>
15:04:38.909245 openat(AT_FDCWD, "/app/lib/tls/libresolv.so.2", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory) <0.000035>
15:04:38.909375 openat(AT_FDCWD, "/app/lib/aarch64/libresolv.so.2", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory) <0.000018>
15:04:38.909468 openat(AT_FDCWD, "/app/lib/libresolv.so.2", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory) <0.000016>
15:04:38.909557 openat(AT_FDCWD, "/app/base/lib/tls/aarch64/libresolv.so.2", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory) <0.000016>
15:04:38.909643 openat(AT_FDCWD, "/app/base/lib/tls/libresolv.so.2", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory) <0.000015>
15:04:38.909725 openat(AT_FDCWD, "/app/base/lib/aarch64/libresolv.so.2", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory) <0.000015>
15:04:38.909805 openat(AT_FDCWD, "/app/base/lib/libresolv.so.2", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory) <0.000015>
15:04:38.909886 openat(AT_FDCWD, "/app/algo/lib/tls/aarch64/libresolv.so.2", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory) <0.000016>
15:04:38.909967 openat(AT_FDCWD, "/app/algo/lib/tls/libresolv.so.2", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory) <0.000014>
15:04:38.910048 openat(AT_FDCWD, "/app/algo/lib/aarch64/libresolv.so.2", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory) <0.000015>
15:04:38.910131 openat(AT_FDCWD, "/app/algo/lib/libresolv.so.2", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory) <0.000016>
15:04:38.910218 openat(AT_FDCWD, "tls/aarch64/libresolv.so.2", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory) <0.000014>
15:04:38.910278 openat(AT_FDCWD, "tls/libresolv.so.2", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory) <0.000014>
15:04:38.910356 openat(AT_FDCWD, "aarch64/libresolv.so.2", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory) <0.000017>
15:04:38.910420 openat(AT_FDCWD, "libresolv.so.2", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory) <0.000046>
15:04:38.910513 openat(AT_FDCWD, "/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3 <0.000026>
15:04:38.910766 openat(AT_FDCWD, "/lib/libresolv.so.2", O_RDONLY|O_CLOEXEC) = 3 <0.000024>
15:04:38.911381 openat(AT_FDCWD, "/app/lib/libc.so.6", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory) <0.000106>
15:04:38.911635 openat(AT_FDCWD, "/app/base/lib/libc.so.6", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory) <0.000061>
15:04:38.911788 openat(AT_FDCWD, "/app/algo/lib/libc.so.6", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory) <0.000054>
15:04:38.911927 openat(AT_FDCWD, "tls/aarch64/libc.so.6", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory) <0.000046>
15:04:38.912077 openat(AT_FDCWD, "tls/libc.so.6", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory) <0.000059>
15:04:38.912219 openat(AT_FDCWD, "aarch64/libc.so.6", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory) <0.000044>
15:04:38.912340 openat(AT_FDCWD, "libc.so.6", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory) <0.000060>
15:04:38.912506 openat(AT_FDCWD, "/lib/libc.so.6", O_RDONLY|O_CLOEXEC) = 3 <0.000074>
15:04:38.914180 openat(AT_FDCWD, "/proc/uptime", O_RDONLY) = 3 <0.000148>
363.70 613.36
15:04:38.914824 +++ exited with 0 +++
root@SOC:/wyk$
root@SOC:/wyk$
timeout 1 strace -c -f -p $PID
chassis pid:1476
strace: Process 1476 attached with 9 threads
strace: Process 1476 detached
strace: Process 1635 detached
strace: Process 1636 detached
strace: Process 1637 detached
strace: Process 1640 detached
strace: Process 1673 detached
strace: Process 1674 detached
strace: Process 1675 detached
strace: Process 1676 detached
% time seconds usecs/call calls errors syscall
------ ----------- ----------- --------- --------- ----------------
40.42 0.026376 4396 6 ppoll
35.71 0.023302 1294 18 nanosleep
19.74 0.012882 15 821 read
4.13 0.002696 16 162 sendmsg
0.00 0.000000 0 1 restart_syscall
------ ----------- ----------- --------- --------- ----------------
100.00 0.065256 1008 total
Terminated
% time: 当前行的系统调用花费的时间占所有系统调用的百分比
seconds: 当前行的系统调度花费的时间(单位是秒)
usecs/call: 当前行的系统调用的平均每次调用花费的时间(单位是微秒)
calls: 当前行的系统调用的次数
errors: 当前行的系统调用发生错误的次数
errors: 当前行的系统调用的名称