简单说,strace是可以跟踪一个程序在做什么的命令。
一 它可以监控某个要执行的命令,比如平时最常见的ls命令:
[root@localhost a]# strace ls
execve(“/bin/ls”, [“ls”], [/* 26 vars */]) = 0
brk(0) = 0x11a8000
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f4792b00000
access(“/etc/ld.so.preload”, R_OK) = -1 ENOENT (No such file or directory)
open(“/etc/ld.so.cache”, O_RDONLY) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=63053, …}) = 0
mmap(NULL, 63053, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7f4792af0000
close(3) = 0
open(“/lib64/libselinux.so.1”, O_RDONLY) = 3
….
execve(“/bin/ls”, [“ls”], [/* 26 vars */]) = 0
brk(0) = 0x11a8000
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f4792b00000
access(“/etc/ld.so.preload”, R_OK) = -1 ENOENT (No such file or directory)
open(“/etc/ld.so.cache”, O_RDONLY) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=63053, …}) = 0
mmap(NULL, 63053, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7f4792af0000
close(3) = 0
open(“/lib64/libselinux.so.1”, O_RDONLY) = 3
….
由上面可以看出,打开的是那个文件,还有依赖的文件
二 也可以监控正在执行的命令,比如nginx
[root@localhost a]# ps aux|grep nginx
root 1496 0.0 0.0 127536 1564 ? Ss Sep18 0:00 nginx: master process /Data/apps/nginx/sbin/nginx
daemon 1499 2.2 0.2 162008 36640 ? S Sep18 128:43 nginx: worker process
…
[root@localhost a]# strace -p 1499
Process 1499 attached – interrupt to quit
epoll_wait(25, {{EPOLLIN|EPOLLOUT, {u32=3002737265, u64=140018936586865}}}, 512, 141) = 1
recvfrom(1067, “”, 16384, 0, NULL, NULL) = 0
close(1067) = 0
epoll_wait(25, {{EPOLLIN|EPOLLOUT, {u32=3002704696, u64=140018936554296}}}, 512, 138) = 1
recvfrom(812, “16 13v2 X-Powered-By: PHP/5.2.13″…, 4096, 0, NULL, NULL) = 4096
readv(812, [{“.aslibra.com/” target=”_blank”…, 4096}], 1) = 4096
…
root 1496 0.0 0.0 127536 1564 ? Ss Sep18 0:00 nginx: master process /Data/apps/nginx/sbin/nginx
daemon 1499 2.2 0.2 162008 36640 ? S Sep18 128:43 nginx: worker process
…
[root@localhost a]# strace -p 1499
Process 1499 attached – interrupt to quit
epoll_wait(25, {{EPOLLIN|EPOLLOUT, {u32=3002737265, u64=140018936586865}}}, 512, 141) = 1
recvfrom(1067, “”, 16384, 0, NULL, NULL) = 0
close(1067) = 0
epoll_wait(25, {{EPOLLIN|EPOLLOUT, {u32=3002704696, u64=140018936554296}}}, 512, 138) = 1
recvfrom(812, “16 13v2 X-Powered-By: PHP/5.2.13″…, 4096, 0, NULL, NULL) = 4096
readv(812, [{“.aslibra.com/” target=”_blank”…, 4096}], 1) = 4096
…
由上面可以看出,nginx在从后端的php接收数据
三 用strace细节可以做什么呢?
1 监测程序做什么: strace 命令
2 监测某个进程:strace -p pid
3 参数,man strace可以获知每个参数说明,说一些常用的:
监测每个步骤花了多少时间: -t 秒 -tt 毫秒 -ttt 时间戳
统计每个操作类型的用时 -c
只列举指定的操作 -e,比如 -e open,只列出打开文件的操作
输出到文件 -o filename
记录相对时间 -r
四 操作实例
1 检查程序引用的配置文件和打开的依赖文件,以nginx为例
[root@pic ~]# strace -e open /Data/apps/nginx/sbin/nginx -t
#加载依赖的库文件
open(“/etc/ld.so.cache”, O_RDONLY) = 3
open(“/lib/libcrypt.so.1”, O_RDONLY) = 3
open(“/lib/libpcre.so.0”, O_RDONLY) = 3
open(“/lib/libcrypto.so.4”, O_RDONLY) = 3
open(“/usr/lib/libz.so.1”, O_RDONLY) = 3
open(“/lib/tls/libc.so.6”, O_RDONLY) = 3
open(“/usr/lib/libgssapi_krb5.so.2”, O_RDONLY) = 3
open(“/usr/lib/libkrb5.so.3”, O_RDONLY) = 3
open(“/lib/libcom_err.so.2”, O_RDONLY) = 3
open(“/usr/lib/libk5crypto.so.3”, O_RDONLY) = 3
open(“/lib/libresolv.so.2”, O_RDONLY) = 3
open(“/lib/libdl.so.2”, O_RDONLY) = 3
open(“/etc/localtime”, O_RDONLY) = 3
#加载配置文件和检查系统配置文件
open(“/Data/apps/nginx/logs/error.log”, O_WRONLY|O_APPEND|O_CREAT|O_LARGEFILE, 0644) = 3
open(“/Data/apps/nginx/conf/nginx.conf”, O_RDONLY|O_LARGEFILE) = 4
open(“/etc/nsswitch.conf”, O_RDONLY) = 5
open(“/etc/ld.so.cache”, O_RDONLY) = 5
open(“/lib/libnss_files.so.2”, O_RDONLY) = 5
open(“/etc/passwd”, O_RDONLY) = 5
open(“/etc/group”, O_RDONLY) = 5
open(“/Data/apps/nginx/conf/mime.types”, O_RDONLY|O_LARGEFILE) = 5
open(“/etc/resolv.conf”, O_RDONLY) = 5
open(“/etc/host.conf”, O_RDONLY) = 5
open(“/etc/hosts”, O_RDONLY) = 5
open(“/etc/ld.so.cache”, O_RDONLY) = 5
open(“/lib/libnss_dns.so.2”, O_RDONLY) = 5
open(“/etc/hosts”, O_RDONLY) = 5
open(“/etc/hosts”, O_RDONLY) = 5
open(“/etc/hosts”, O_RDONLY) = 5
#加载自定义的配置文件
open(“/Data/apps/nginx/conf/fastcgi_params”, O_RDONLY|O_LARGEFILE) = 5
open(“/Data/apps/nginx/conf/fastcgi_params”, O_RDONLY|O_LARGEFILE) = 5
open(“/Data/apps/nginx/conf/fastcgi_params”, O_RDONLY|O_LARGEFILE) = 5
open(“/Data/apps/nginx/conf/dyw.cdn.conf”, O_RDONLY|O_LARGEFILE) = 5
open(“/Data/apps/nginx/conf/fastcgi_params”, O_RDONLY|O_LARGEFILE) = 6
the configuration file /Data/apps/nginx/conf/nginx.conf syntax is ok
open(“/Data/apps/nginx/logs/nginx.pid”, O_RDWR|O_CREAT|O_LARGEFILE, 0644) = 4
open(“/Data/logs/error.nginx.log”, O_WRONLY|O_APPEND|O_CREAT|O_LARGEFILE, 0644) = 4
configuration file /Data/apps/nginx/conf/nginx.conf test is successful
Process 18083 detached
#加载依赖的库文件
open(“/etc/ld.so.cache”, O_RDONLY) = 3
open(“/lib/libcrypt.so.1”, O_RDONLY) = 3
open(“/lib/libpcre.so.0”, O_RDONLY) = 3
open(“/lib/libcrypto.so.4”, O_RDONLY) = 3
open(“/usr/lib/libz.so.1”, O_RDONLY) = 3
open(“/lib/tls/libc.so.6”, O_RDONLY) = 3
open(“/usr/lib/libgssapi_krb5.so.2”, O_RDONLY) = 3
open(“/usr/lib/libkrb5.so.3”, O_RDONLY) = 3
open(“/lib/libcom_err.so.2”, O_RDONLY) = 3
open(“/usr/lib/libk5crypto.so.3”, O_RDONLY) = 3
open(“/lib/libresolv.so.2”, O_RDONLY) = 3
open(“/lib/libdl.so.2”, O_RDONLY) = 3
open(“/etc/localtime”, O_RDONLY) = 3
#加载配置文件和检查系统配置文件
open(“/Data/apps/nginx/logs/error.log”, O_WRONLY|O_APPEND|O_CREAT|O_LARGEFILE, 0644) = 3
open(“/Data/apps/nginx/conf/nginx.conf”, O_RDONLY|O_LARGEFILE) = 4
open(“/etc/nsswitch.conf”, O_RDONLY) = 5
open(“/etc/ld.so.cache”, O_RDONLY) = 5
open(“/lib/libnss_files.so.2”, O_RDONLY) = 5
open(“/etc/passwd”, O_RDONLY) = 5
open(“/etc/group”, O_RDONLY) = 5
open(“/Data/apps/nginx/conf/mime.types”, O_RDONLY|O_LARGEFILE) = 5
open(“/etc/resolv.conf”, O_RDONLY) = 5
open(“/etc/host.conf”, O_RDONLY) = 5
open(“/etc/hosts”, O_RDONLY) = 5
open(“/etc/ld.so.cache”, O_RDONLY) = 5
open(“/lib/libnss_dns.so.2”, O_RDONLY) = 5
open(“/etc/hosts”, O_RDONLY) = 5
open(“/etc/hosts”, O_RDONLY) = 5
open(“/etc/hosts”, O_RDONLY) = 5
#加载自定义的配置文件
open(“/Data/apps/nginx/conf/fastcgi_params”, O_RDONLY|O_LARGEFILE) = 5
open(“/Data/apps/nginx/conf/fastcgi_params”, O_RDONLY|O_LARGEFILE) = 5
open(“/Data/apps/nginx/conf/fastcgi_params”, O_RDONLY|O_LARGEFILE) = 5
open(“/Data/apps/nginx/conf/dyw.cdn.conf”, O_RDONLY|O_LARGEFILE) = 5
open(“/Data/apps/nginx/conf/fastcgi_params”, O_RDONLY|O_LARGEFILE) = 6
the configuration file /Data/apps/nginx/conf/nginx.conf syntax is ok
open(“/Data/apps/nginx/logs/nginx.pid”, O_RDWR|O_CREAT|O_LARGEFILE, 0644) = 4
open(“/Data/logs/error.nginx.log”, O_WRONLY|O_APPEND|O_CREAT|O_LARGEFILE, 0644) = 4
configuration file /Data/apps/nginx/conf/nginx.conf test is successful
Process 18083 detached
2 监测程序做了什么,用php为例
写个测试的代码
<?
echo “test”;
file_get_contents(‘http://www.baidu.com’);
sleep(3);
file_put_contents(‘/tmp/test.php.txt’,’test’);
echo “test”;
file_get_contents(‘http://www.baidu.com’);
sleep(3);
file_put_contents(‘/tmp/test.php.txt’,’test’);
先看看执行概况:
strace -c /Data/apps/php/bin/php test.php
test% time seconds usecs/call calls errors syscall
—— ———– ———– ——— ——— —————-
59.91 0.000263 0 7977 select
32.35 0.000142 0 7985 poll
4.10 0.000018 1 29 munmap
2.51 0.000011 0 159 mmap
…
test% time seconds usecs/call calls errors syscall
—— ———– ———– ——— ——— —————-
59.91 0.000263 0 7977 select
32.35 0.000142 0 7985 poll
4.10 0.000018 1 29 munmap
2.51 0.000011 0 159 mmap
…
看上面的代码,select和poll其实是网络操作
strace -tt /Data/apps/php/bin/php test.php12:30:59.371957 execve(“/Data/apps/php/bin/php”, [“/Data/apps/php/bin/php”, “test.php”], [/* 26 vars */]) = 0
…
#尝试加载的路径优先级
12:30:59.372570 open(“/Data/apps/mysql/lib/mysql/tls/x86_64/libcrypt.so.1”, O_RDONLY) = -1 ENOENT (No such file or directory)
12:30:59.372599 stat(“/Data/apps/mysql/lib/mysql/tls/x86_64”, 0x7ffff6f97f40) = -1 ENOENT (No such file or directory)
12:30:59.372626 open(“/Data/apps/mysql/lib/mysql/tls/libcrypt.so.1”, O_RDONLY) = -1 ENOENT (No such file or directory)
12:30:59.372653 stat(“/Data/apps/mysql/lib/mysql/tls”, 0x7ffff6f97f40) = -1 ENOENT (No such file or directory)
12:30:59.372679 open(“/Data/apps/mysql/lib/mysql/x86_64/libcrypt.so.1”, O_RDONLY) = -1 ENOENT (No such file or directory)
12:30:59.372708 stat(“/Data/apps/mysql/lib/mysql/x86_64”, 0x7ffff6f97f40) = -1 ENOENT (No such file or directory)
12:30:59.372737 open(“/Data/apps/mysql/lib/mysql/libcrypt.so.1”, O_RDONLY) = -1 ENOENT (No such file or directory)
12:30:59.372765 stat(“/Data/apps/mysql/lib/mysql”, {st_mode=S_IFDIR|0755, st_size=4096, …}) = 0
12:30:59.372824 open(“/etc/ld.so.cache”, O_RDONLY) = 3
…
12:30:59.389336 access(“/Data/apps/php/bin/php”, X_OK) = 0
#看看读取了哪个配置文件
12:30:59.389380 open(“/Data/apps/php/bin/php-cli.ini”, O_RDONLY) = -1 ENOENT (No such file or directory)
12:30:59.389413 open(“/Data/apps/php/etc/php-cli.ini”, O_RDONLY) = -1 ENOENT (No such file or directory)
12:30:59.389443 open(“/Data/apps/php/bin/php.ini”, O_RDONLY) = -1 ENOENT (No such file or directory)
12:30:59.389472 open(“/Data/apps/php/etc/php.ini”, O_RDONLY) = 3
…
#读取配置文件
12:30:59.389773 read(3, “[PHP]nn;;;;;;;;;;;n; WARNING ;n;”…, 8192) = 8192
12:30:59.389886 read(3, “ay only alter environment variab”…, 8192) = 8192
…
#加载配置文件的模块
12:30:59.391577 open(“/Data/apps/php/lib/php/extensions/no-debug-non-zts-20060613/memcache.so”, O_RDONLY) = 3
12:30:59.391612 read(3, “177ELF2113 3 >
…
#尝试加载的路径优先级
12:30:59.372570 open(“/Data/apps/mysql/lib/mysql/tls/x86_64/libcrypt.so.1”, O_RDONLY) = -1 ENOENT (No such file or directory)
12:30:59.372599 stat(“/Data/apps/mysql/lib/mysql/tls/x86_64”, 0x7ffff6f97f40) = -1 ENOENT (No such file or directory)
12:30:59.372626 open(“/Data/apps/mysql/lib/mysql/tls/libcrypt.so.1”, O_RDONLY) = -1 ENOENT (No such file or directory)
12:30:59.372653 stat(“/Data/apps/mysql/lib/mysql/tls”, 0x7ffff6f97f40) = -1 ENOENT (No such file or directory)
12:30:59.372679 open(“/Data/apps/mysql/lib/mysql/x86_64/libcrypt.so.1”, O_RDONLY) = -1 ENOENT (No such file or directory)
12:30:59.372708 stat(“/Data/apps/mysql/lib/mysql/x86_64”, 0x7ffff6f97f40) = -1 ENOENT (No such file or directory)
12:30:59.372737 open(“/Data/apps/mysql/lib/mysql/libcrypt.so.1”, O_RDONLY) = -1 ENOENT (No such file or directory)
12:30:59.372765 stat(“/Data/apps/mysql/lib/mysql”, {st_mode=S_IFDIR|0755, st_size=4096, …}) = 0
12:30:59.372824 open(“/etc/ld.so.cache”, O_RDONLY) = 3
…
12:30:59.389336 access(“/Data/apps/php/bin/php”, X_OK) = 0
#看看读取了哪个配置文件
12:30:59.389380 open(“/Data/apps/php/bin/php-cli.ini”, O_RDONLY) = -1 ENOENT (No such file or directory)
12:30:59.389413 open(“/Data/apps/php/etc/php-cli.ini”, O_RDONLY) = -1 ENOENT (No such file or directory)
12:30:59.389443 open(“/Data/apps/php/bin/php.ini”, O_RDONLY) = -1 ENOENT (No such file or directory)
12:30:59.389472 open(“/Data/apps/php/etc/php.ini”, O_RDONLY) = 3
…
#读取配置文件
12:30:59.389773 read(3, “[PHP]nn;;;;;;;;;;;n; WARNING ;n;”…, 8192) = 8192
12:30:59.389886 read(3, “ay only alter environment variab”…, 8192) = 8192
…
#加载配置文件的模块
12:30:59.391577 open(“/Data/apps/php/lib/php/extensions/no-debug-non-zts-20060613/memcache.so”, O_RDONLY) = 3
12:30:59.391612 read(3, “177ELF2113 3 >