Linux command – strace

Loading

測試環境為 CentOS 8 x86_64

Linux 指令 strace (trace system calls and signals) 用來檢視程式 (Process) 執行時會呼叫那些系統函式庫(system library).

-p pid : 可以依據程式的 PID 來檢視呼叫那些系統函式.下面範例以 systemd (PID=1) 來檢視.

[root@localhost ~]# ps -aux |more
USER         PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root           1  0.0  0.4 187300 15376 ?        Ss   Dec07   0:07 /usr/lib/systemd/systemd --switched-root --system --deserialize 18
[root@localhost ~]# strace -p 1
strace: Process 1 attached
gettid()                                = 1
epoll_wait(4, [{EPOLLIN, {u32=209010656, u64=94141597171680}}], 176, -1) = 1
recvmsg(24, {msg_name=NULL, msg_namelen=0, msg_iov=[{iov_base="WATCHDOG=1", iov_len=4096}], msg_iovlen=1, msg_control=[{cmsg_len=28, cmsg_level=SOL_SOCKET, cmsg_type=SCM_CREDENTIALS, cmsg_data={pid=964, uid=0, gid=0}}], msg_controllen=32, msg_flags=MSG_CMSG_CLOEXEC}, MSG_TRUNC|MSG_DONTWAIT|MSG_CMSG_CLOEXEC) = 10
openat(AT_FDCWD, "/proc/964/cgroup", O_RDONLY|O_CLOEXEC) = 20
fstat(20, {st_mode=S_IFREG|0444, st_size=0, ...}) = 0
read(20, "12:memory:/system.slice/systemd-"..., 1024) = 306
close(20)                               = 0
gettid()                                = 1

-T , –syscall-times[=precision] : Show the time spent in system calls.
-tt , –absolute-timestamps=precision:us : If given twice, the time printed will include the microseconds.
-f , –follow-forks : Trace child processes as they are created by currently traced processes as a result of the fork(2), vfork(2) and clone(2) system calls.

[root@localhost ~]# strace -T -tt -f ls
strace: deprecated option -F ignored
18:09:42.970248 execve("/usr/bin/ls", ["ls"], 0x7ffe63644a00 /* 27 vars */) = 0 <0.000358>
18:09:42.970929 brk(NULL)               = 0x560411aa8000 <0.000068>
18:09:42.971143 arch_prctl(0x3001 /* ARCH_??? */, 0x7ffde8198360) = -1 EINVAL (Invalid argument) <0.000066>
18:09:42.971524 access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory) <0.000043>
18:09:42.971879 openat(AT_FDCWD, "/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3 <0.000084>
18:09:42.972124 fstat(3, {st_mode=S_IFREG|0644, st_size=44047, ...}) = 0 <0.000065>
18:09:42.972568 mmap(NULL, 44047, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7fa8d1e28000 <0.000058>
18:09:42.972871 close(3)                = 0 <0.000092>

-o filename , –output=filename : Write the trace output to the file filename rather than to stderr.

[root@localhost ~]# strace -T -tt -f -o ls.txt ls
沒有解決問題,試試搜尋本站其他內容

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

這個網站採用 Akismet 服務減少垃圾留言。進一步了解 Akismet 如何處理網站訪客的留言資料