您好,登錄后才能下訂單哦!
這篇文章主要講解了“Linux系統調試工具strace怎么用”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“Linux系統調試工具strace怎么用”吧!
strace是Linux系統中一款調試程序的軟件,是一個簡單的跟蹤系統調用的執行工具,可以檢查每一個應用程序所使用的系統調用。
官方網址:http://sourceforge.net/project/showfiles.php?group_id=2861&package_id=2819
[root@RedHat_test ~]# yum install strace
[root@RedHat_test ~]# man strace -tt : 在每行輸出的前面,顯示毫秒級別的時間 -T : 顯示每次系統調用所花費的時間 -v : 對于某些相關調用,把完整的環境變量,文件stat結構等打出來。 -f : 跟蹤目標進程,以及目標進程創建的所有子進程 -e : 控制要跟蹤的事件和跟蹤行為,比如指定要跟蹤的系統調用名稱 -o : 把strace的輸出單獨寫到指定的文件 -s : 當系統調用的某個參數是字符串時,最多輸出指定長度的內容,默認是32個字節 -p : 指定要跟蹤的進程pid, 要同時跟蹤多個pid, 重復多次-p選項即可。
[root@RedHat_test opt]# strace ls execve("/bin/ls", ["ls"], [/* 46vars */]) =0 brk(0) =0x1ab9000 access("/etc/ld.so.nohwcap", F_OK) =-1ENOENT (No such file or directory) mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) =0x7fcf695fb000 access("/etc/ld.so.preload", R_OK) =-1ENOENT (No such file or directory) open("/etc/ld.so.cache", O_RDONLY) =3 fstat(3, {st_mode=S_IFREG|0644, st_size=102786, ...}) =0 mmap(NULL, 102786, PROT_READ, MAP_PRIVATE, 3, 0) =0x7fcf695e1000 close(3) =0 access("/etc/ld.so.nohwcap", F_OK) =-1ENOENT (No such file or directory) open("/lib/librt.so.1", O_RDONLY) =3
[root@RedHat_test ~]# strace php 2>&1 | grep php.ini
[root@RedHat_test ~]# strace -e open cat dead.letter open("/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) =3 open("/lib64/libc.so.6", O_RDONLY|O_CLOEXEC) =3 open("/usr/lib/locale/locale-archive", O_RDONLY|O_CLOEXEC) =3 open("dead.letter", O_RDONLY) =-1ENOENT (No such file or directory) cat: dead.letteropen("/usr/share/locale/locale.alias", O_RDONLY|O_CLOEXEC) =3 open("/usr/share/locale/zh_CN.UTF-8/LC_MESSAGES/libc.mo", O_RDONLY) =-1ENOENT (No such file or directory) open("/usr/share/locale/zh_CN.utf8/LC_MESSAGES/libc.mo", O_RDONLY) =-1ENOENT (No such file or directory) open("/usr/share/locale/zh_CN/LC_MESSAGES/libc.mo", O_RDONLY) =3 open("/usr/lib64/gconv/gconv-modules.cache", O_RDONLY) =3 : 沒有那個文件或目錄 +++exited with 1+++
[root@RedHat_test ~]# strace -p 2208 strace: Process 2208attached restart_syscall(... resuming interrupted poll ...> ) =1 read(5, "\1\0\0\0\0\0\0\0", 16) =8 futex(0x560eaba76640, FUTEX_WAKE_PRIVATE, 1) =1 poll([{fd=5, events=POLLIN}, {fd=8, events=POLLIN}], 2, -1) =1([{fd=5, revents=POLLIN}]) read(5, "\1\0\0\0\0\0\0\0", 16) =8 poll([{fd=5, events=POLLIN}, {fd=8, events=POLLIN}], 2, -1) =1([{fd=5, revents=POLLIN}]) read(5, "\1\0\0\0\0\0\0\0", 16) =8 poll([{fd=5, events=POLLIN}, {fd=8, events=POLLIN}], 2, -1) =1([{fd=8, revents=POLLIN}]) write(5, "\1\0\0\0\0\0\0\0", 8) =8 write(7, "\1\0\0\0\0\0\0\0", 8) =8 futex(0x560eaba6b9e0, FUTEX_WAKE_PRIVATE, 1) =1 futex(0x560eaba861c0, FUTEX_WAKE_PRIVATE, 1) =1 futex(0x560eaba6d198, FUTEX_WAKE_PRIVATE, 1) =1 poll([{fd=8, events=POLLIN}], 1, 0) =1([{fd=8, revents=POLLIN}]) read(8, "\2\0\0\0\200\0\0\0}\327\5\0\20\0\0\00018452\0\0\0\0\0\0\0\0\0\0\0"..., 2048) =64 ---------------------------------------------------------------------------------------- 常見使用方法 [root@RedHat_test ~]# strace -T -e access -p 2208 ---------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------------
[root@RedHat_test ~]# strace -c ls 1.pyfile.log scripts tsar correct jdk-8u91-linux-x64.tar.gz test yunweimao % time seconds usecs/call calls errors syscall -------------------------------------------------------------- 26.43 0.000315 12 27 mmap 15.52 0.000185 19 10 open 9.98 0.000119 7 18 mprotect 6.71 0.000080 10 8 read 6.71 0.000080 6 13 close 6.21 0.000074 7 11 fstat 4.45 0.000053 27 2 munmap 3.86 0.000046 23 2 statfs 2.85 0.000034 34 1 stat 2.68 0.000032 16 2 getdents 2.60 0.000031 31 1 openat 2.18 0.000026 13 2 write 2.10 0.000025 13 2 1access 1.85 0.000022 22 1 set_tid_address 1.59 0.000019 6 3 brk 1.26 0.000015 8 2 ioctl 1.01 0.000012 6 2 rt_sigaction 0.59 0.000007 7 1 execve 0.50 0.000006 6 1 getrlimit 0.50 0.000006 6 1 set_robust_list 0.42 0.000005 5 1 rt_sigprocmask 0.00 0.000000 0 1 arch_prctl -------------------------------------------------------------- 100.00 0.001192 112 1total
[root@RedHat_test ~]# strace -o process_strace -p 2208 [root@RedHat_test ~]# ls process_strace [root@RedHat_test ~]# tail -f 10 process_strace tail: 無法打開"100"讀取數據: 沒有那個文件或目錄 ==> process_strace futex(0x560eaba6b9e0, FUTEX_WAKE_PRIVATE, 1) =1 futex(0x560eaba861c0, FUTEX_WAKE_PRIVATE, 1) =1 futex(0x560eaba6d198, FUTEX_WAKE_PRIVATE, 1) =1 poll([{fd=8, events=POLLIN}], 1, 0) =1([{fd=8, revents=POLLIN}]) read(8, "\2\0\0\0\0\2\0\0\0\0\0\0\20\0\0\00018457\0\0\0\0\0\0\0\0\0\0\0"..., 2048) =64 poll([{fd=8, events=POLLIN}], 1, 0) =0(Timeout) write(5, "\1\0\0\0\0\0\0\0", 8) =8 poll([{fd=5, events=POLLIN}, {fd=8, events=POLLIN}], 2, -1) =1([{fd=5, revents=POLLIN}]) read(5, "\2\0\0\0\0\0\0\0", 16) =8 poll([{fd=5, events=POLLIN}, {fd=8, events=POLLIN}], 2, -1detached ...>
[root@RedHat_test ~]# strace -tt -T -f -e trace=file -o /data/log/strace.log -s 1024 service nginx restart [root@RedHat_test ~]# cat /data/log/strace.log 3259713:51:52.873281 execve("/usr/sbin/service", ["service", "nginx", "restart"], [/* 27vars */]) =00.000319> 3259713:51:52.874064 access("/etc/ld.so.preload", R_OK) =-1ENOENT (No such file or directory) 0.000017> 3259713:51:52.874184 open("/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) =30.000017> 3259713:51:52.874343 open("/lib64/libtinfo.so.5", O_RDONLY|O_CLOEXEC) =30.000017> 3259713:51:52.874549 open("/lib64/libdl.so.2", O_RDONLY|O_CLOEXEC) =30.000017> 3259713:51:52.874750 open("/lib64/libc.so.6", O_RDONLY|O_CLOEXEC) =30.000017> # 每一行輸出的最后尖括號中的數據表示執行耗時,單位是秒
[root@RedHat_test ~]# strace -t ls 11:48:02 execve("/usr/bin/ls", ["ls"], [/* 27vars */]) =0 11:48:02 brk(NULL) =0x23ba000 11:48:02 mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) =0x7fa562bae000 11:48:02 access("/etc/ld.so.preload", R_OK) =-1ENOENT (No such file or directory) 11:48:02 open("/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) =3 11:48:02 fstat(3, {st_mode=S_IFREG|0644, st_size=35957, ...}) =0 11:48:02 mmap(NULL, 35957, PROT_READ, MAP_PRIVATE, 3, 0) =0x7fa562ba5000 11:48:02 close(3) =0 11:48:02 open("/lib64/libselinux.so.1", O_RDONLY|O_CLOEXEC) =3 11:48:02 read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\320i\0\0\0\0\0\0"..., 832) =832 11:48:02 fstat(3, {st_mode=S_IFREG|0755, st_size=155784, ...}) =0 11:48:02 mmap(NULL, 2255184, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) =0x7fa562767000 11:48:02 mprotect(0x7fa56278b000, 2093056, PROT_NONE) =0 11:48:02 mmap(0x7fa56298a000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x23000) =0x7fa56298a000 11:48:02 mmap(0x7fa56298c000, 6480, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) =0x7fa56298c000 11:48:02 close(3) =0 11:48:02 open("/lib64/libcap.so.2", O_RDONLY|O_CLOEXEC) =3 11:48:02 read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0 \26\0\0\0\0\0\0"..., 832) =832 11:48:02 fstat(3, {st_mode=S_IFREG|0755, st_size=20032, ...}) =0 ---------------------------------------------------------------------------------------- -tt : 展示微秒級別的時間戳 [root@RedHat_test ~]# strace -tt ls -ttt : 展示微秒級的時間戳,但是它并不是打印當前時間,而是顯示自從epoch(譯注:1970年1月1日00:00:00 UTC)以來的所經過的秒數 [root@RedHat_test ~]# strace -ttt ls -r : 展示相對時間戳 [root@RedHat_test ~]# strace -r ls ----------------------------------------------------------------------------------------
感謝各位的閱讀,以上就是“Linux系統調試工具strace怎么用”的內容了,經過本文的學習后,相信大家對Linux系統調試工具strace怎么用這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。