strace的簡單用法-ubuntu下演示

說下pidof這個方法,挺好用的php

root@tb:/usr/share/nginx/html# pidof nginx
1034 1033 1032 1031 1030
root@tb:/usr/share/nginx/html# ps -aux|grep nginx
root      1030  0.0  0.0  85892  2880 ?        Ss   09:00   0:00 nginx: master process /usr/sbin/nginx
www-data  1031  0.0  0.1  86572  4688 ?        S    09:00   0:00 nginx: worker process
www-data  1032  0.0  0.0  86280  3604 ?        S    09:00   0:01 nginx: worker process
www-data  1033  0.0  0.0  86280  3604 ?        S    09:00   0:01 nginx: worker process
www-data  1034  0.0  0.0  86280  3604 ?        S    09:00   0:01 nginx: worker process
root      4393  0.0  0.0  15964  2248 pts/0    S+   16:44   0:00 grep --color=autonginx

root@tb:/usr/share/nginx/html# pidof php5-fpm
1051 1050 1041
root@tb:/usr/share/nginx/html# ps -aux|grep php-fpm
root      1041  0.0  0.5 240308 21192 ?        Ss   09:00   0:00 php-fpm: master process (/etc/php5/fpm/php-fpm.conf)                    
www-data  1050  0.0  0.3 240420 12456 ?        S    09:00   0:00 php-fpm: pool www                                                       
www-data  1051  0.0  0.3 240476 12780 ?        S    09:00   0:00 php-fpm: pool www                                                       
root      4399  0.0  0.0  15964  2232 pts/0    S+   16:45   0:00 grep --color=auto php-fpm

具體說下stracehtml

這個是跟蹤的php執行fuck.php所跟蹤的stracelinux

strace -o fuck.strace.txt -tt php -f fuck.php

fuck.strace.txt部份內容以下:nginx

16:42:18.124016 execve("/usr/bin/php", ["php", "-f", "fuck.php"], [/* 36 vars */]) = 0
16:42:18.124472 brk(0)                  = 0x2b8f000
16:42:18.124549 access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
16:42:18.124623 mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f96febd3000
16:42:18.124695 access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory)
16:42:18.124767 open("/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
16:42:18.124829 fstat(3, {st_mode=S_IFREG|0644, st_size=94715, ...}) = 0
16:42:18.124886 mmap(NULL, 94715, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7f96febbb000
16:42:18.124939 close(3)                = 0
16:42:18.124991 access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
16:42:18.125048 open("/lib/x86_64-linux-gnu/libz.so.1", O_RDONLY|O_CLOEXEC) = 3
16:42:18.125105 read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\0\36\0\0\0\0\0\0"..., 832) = 832
16:42:18.125165 fstat(3, {st_mode=S_IFREG|0644, st_size=100728, ...}) = 0
16:42:18.125222 mmap(NULL, 2195784, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f96fe79a000

這是跟蹤某個進程網絡

strace -o php-fpm.strace.txt -tt -p 1041

-o 輸出到文件
-tt顯示毫秒級別時間
-p指定跟蹤進程pid
-s 當系統調用的某個參數是字符串時,最多輸出指定長度的內容,默認是32個字節
-e 這個用的應該比較多,雖然我還不會用 功能爲 控制要跟蹤的事件和跟蹤行爲,好比指定要跟蹤的系統調用名稱less

簡單列下strace中`-e`的選項
  -e trace=file 跟蹤和文件訪問相關的調用,即參數中有文件名
  -e trace=process 和進程管理相關的調用,好比fork/exec/exit_group
  -e trace=ipc 進程間通訊相關 ,好比shmget
  -e trace=desc 和文件描述符相關,好比write/read/select/epoll
  -e trace=network 和網絡通訊相關調用,好比socket/sendto/connect

-f 跟蹤目標進程,以及目標集成建立的全部子進程
-v 對於某些相關調用,把完整的幻境變量,文件stat結構打印出來
-T顯示每次系統調用所花費時間socket

關於更多linux中系統調用函數等,能夠參考這篇文章linux系統調用
對-o的文件能夠用less查看具體內容函數

演示:php-fpm

strace -tt -T -f -e trace=file -o nginx_strace.txt -s 1024 nginx

看上面的,nginx_strace.txt的部份內容以下:.net

4526  17:00:11.384699 execve("/usr/sbin/nginx", ["nginx"], [/* 36 vars */]) = 0 <0.000234>
4526  17:00:11.385161 access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory) <0.000020>
4526  17:00:11.385278 access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory) <0.000018>
4526  17:00:11.385346 open("/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3 <0.000041>
4526  17:00:11.385515 access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory) <0.000018>
4526  17:00:11.385578 open("/lib/x86_64-linux-gnu/libpthread.so.0", O_RDONLY|O_CLOEXEC) = 3 <0.000020>
4526  17:00:11.385871 access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory) <0.000019>

其中4526爲pid
後面17:00:11點後面的數字爲毫秒級時間
最後面的<0.000041>爲調用所花時間,爲-T造出來的
這裏的輸出只顯示和文章訪問有相關的內容,由於-e trace =file

相關文章
相關標籤/搜索