-c 统计每一系统调用的所执行的时间,次数和出错的次数等. -d 输出strace关于标准错误的调试信息.-f 跟踪由fork调用所产生的子进程. -ff 如果提供-o filename,则所有进程的跟踪结果输出到相应的filename.pid中,pid是各进程的进程号.-F 尝试跟踪vfork调用.在-f时,vfork不被跟踪. -h 输出简要的帮助信息. -i 输出系统调用的入口指针.-q 禁止输出关于脱离的消息.-r 打印出相对时间关于,,每一个系统调用. -t 在输出中的每一行前加上时间信息. -tt 在输出中的每一行前加上时间信息,微秒级.-ttt 微秒级输出,以秒了表示时间.-T 显示每一调用所耗的时间. -v 输出所有的系统调用.一些调用关于环境变量,状态,输入输出等调用由于使用频繁,默认不输出. -V 输出strace的版本信息. -x 以十六进制形式输出非标准字符串-xx 所有字符串以十六进制形式输出. -a column 设置返回值的输出位置.默认 为40. -e expr 指定一个表达式,用来控制如何跟踪.格式:[qualifier=][!]value1[,value2]... qualifier只能是 trace,abbrev,verbose,raw,signal,read,write其中之一.value是用来限定的符号或数字.默认的 qualifier是 trace.感叹号是否定符号.例如:-eopen等价于 -e trace=open,表示只跟踪open调用.而-etrace!=open 表示跟踪除了open以外的其他调用.有两个特殊的符号 all 和 none.注意有些shell使用!来执行历史记录里的命令,所以要使用\\. -e trace=set 只跟踪指定的系统 调用.例如:-e trace=open,close,rean,write表示只跟踪这四个系统调用.默认的为set=all.-e trace=file 只跟踪有关文件操作的系统调用.-e trace=process 只跟踪有关进程控制的系统调用. -e trace=network 跟踪与网络有关的所有系统调用. -e strace=signal 跟踪所有与系统信号有关的 系统调用-e trace=ipc 跟踪所有与进程通讯有关的系统调用-e abbrev=set 设定strace输出的系统调用的结果集.-v 等与 abbrev=none.默认为abbrev=all.-e raw=set 将指定的系统调用的参数以十六进制显示.-e signal=set 指定跟踪的系统信号.默认为all.如 signal=!SIGIO(或者signal=!io),表示不跟踪SIGIO信号. -e read=set 输出从指定文件中读出 的数据.例如: -e read=3,5 -e write=set 输出写入到指定文件中的数据.-o filename 将strace的输出写入文件filename -p pid 跟踪指定的进程pid.-s strsize 指定输出的字符串的最大长度.默认为32.文件名一直全部输出. -u username 以username的UID和GID执行被跟踪的命令
1。查看CPU运行时间
[guest@localhost tmp]$ strace -c ./b.out % time seconds usecs/call calls errors syscall ------ ----------- ----------- --------- --------- ---------------- 42.72 0.000692 87 8 open 38.40 0.000622 622 1 set_thread_area 16.85 0.000273 137 2 munmap 2.04 0.000033 5 7 read 0.00 0.000000 0 8 close 0.00 0.000000 0 1 execve 0.00 0.000000 0 1 access 0.00 0.000000 0 3 brk 0.00 0.000000 0 4 mprotect 0.00 0.000000 0 17 mmap2 0.00 0.000000 0 7 fstat64 ------ ----------- ----------- --------- --------- ---------------- 100.00 0.001620 59 total 2。查看相对运行时间 [guest@localhost tmp]$ strace -r ./b.out 0.000560 mprotect(0xb91000, 8192, PROT_READ) = 0 0.000196 mprotect(0x45c000, 4096, PROT_READ) = 0 0.000884 mprotect(0x42a000, 16384, PROT_READ) = 0 0.000191 mprotect(0xa4a000, 4096, PROT_READ) = 0 0.000144 munmap(0xb7f12000, 45307) = 0 0.000577 brk(0) = 0x8c2b000 0.000171 brk(0x8c4c000) = 0x8c4c000 0.000183 open("sleeper.cpp", O_RDONLY|O_LARGEFILE) = 3 0.000225 read(3, "#include <unistd.h>\n#include <io"..., 8191) = 355 0.000372 read(3, "", 8191) = 0 0.000298 close(3) = 0 0.000192 exit_group(0) = ? 3。查看文件变动相关的信号 [guest@localhost tmp]$ strace -e trace=desc ./b.out open("/etc/ld.so.preload", O_RDONLY) = 3 fstat64(3, {st_mode=S_IFREG|0444, st_size=17, ...}) = 0 close(3) = 0 open("/lib/libcwait.so", O_RDONLY) = 3 read(3, "\177ELF\1\1\1\0\0\0\0\0\0\0\0\0\3\0\3\0\1\0\0\0\20\3\0\0004\0\0\0"..., 512) = 512 fstat64(3, {st_mode=S_IFREG|0755, st_size=2572, ...}) = 0 close(3) = 0 open("/etc/ld.so.cache", O_RDONLY) = 3 fstat64(3, {st_mode=S_IFREG|0644, st_size=45307, ...}) = 0 close(3) = 0 open("/usr/lib/libstdc++.so.6", O_RDONLY) = 3 read(3, "\177ELF\1\1\1\0\0\0\0\0\0\0\0\0\3\0\3\0\1\0\0\0P\2348\0004\0\0\0"..., 512) = 512 fstat64(3, {st_mode=S_IFREG|0755, st_size=936908, ...}) = 0 close(3) = 0 open("/lib/libm.so.6", O_RDONLY) = 3 4。按调用过程来排序,并显示相对时间 guest@localhost tmp]$ strace -r -S calls ./b.out 0.000000 execve("./b.out", ["./b.out"], [/* 20 vars */]) = 0 0.001860 brk(0) = 0x86b4000 0.000236 access("/etc/ld.so.preload", R_OK) = 0 0.001263 open("/etc/ld.so.preload", O_RDONLY) = 3 0.000497 fstat64(3, {st_mode=S_IFREG|0444, st_size=17, ...}) = 0 0.000211 mmap2(NULL, 17, PROT_READ|PROT_WRITE, MAP_PRIVATE, 3, 0) = 0xb7ff3000 0.000756 close(3) = 0 0.000173 open("/lib/libcwait.so", O_RDONLY) = 3 0.001096 read(3, "\177ELF\1\1\1\0\0\0\0\0\0\0\0\0\3\0\3\0\1\0\0\0\20\3\0\0004\0\0\0"..., 512) = 512 0.000164 fstat64(3, {st_mode=S_IFREG|0755, st_size=2572, ...}) = 0 0.000610 mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb7ff2000 0.000165 mmap2(NULL, 5544, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x76d000 0.000757 mmap2(0x76e000, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0) = 0x76e000 0.000159 close(3) = 0 0.000762 munmap(0xb7ff3000, 17) = 0 0.000160 open("/etc/ld.so.cache", O_RDONLY) = 3 5。调试vsftpd,其进程号为21712,在下例中-f参数对子进程也进行了跟踪。 [root@test1 ~]# strace -f -T -p 21712 Process 21712 attached - interrupt to quit 注:此时从另外一终端访问ftp accept(3, {sa_family=AF_INET, sin_port=htons(4138), sin_addr=inet_addr("192.168.203.137")}, [16]) = 0 <10.148879> rt_sigprocmask(SIG_BLOCK, [CHLD], NULL, = 0 <0.000050> rt_sigprocmask(SIG_BLOCK, [HUP], NULL, = 0 <0.000046> clone(Process 21732 attached (waiting for parent) Process 21732 resumed (parent 21712 ready) child_stack=0, flags=CLONE_CHILD_CLEARTID|CLONE_CHILD_SETTID|SIGCHLD, child_tidptr=0xb7fcd718) = 21732 <0.000514> [pid 21712] close(0) = 0 <0.000046> [pid 21712] rt_sigprocmask(SIG_UNBLOCK, [CHLD], NULL, = 0 <0.000049> [pid 21712] rt_sigprocmask(SIG_UNBLOCK, [HUP], NULL, = 0 <0.000039> [pid 21712] accept(3, <unfinished ...> [pid 21732] close(3) = 0 <0.000055> [pid 21732] dup2(0, 1) = 1 <0.000041> [pid 21732] dup2(0, 2) = 2 <0.000040> [pid 21732] getpeername(0, {sa_family=AF_INET, sin_port=htons(4138), sin_addr=inet_addr("192.168.203.137")}, [16]) = 0 <0.000046> [pid 21732] getsockname(0, {sa_family=AF_INET, sin_port=htons(21), sin_addr=inet_addr("192.168.203.139")}, [16]) = 0 <0.000042> [pid 21732] open("/etc/hosts.allow", O_RDONLY) = 3 <0.000065> [pid 21732] fstat64(3, {st_mode=S_IFREG|0644, st_size=161, ...}) = 0 <0.000043> [pid 21732] mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb7fdc000 <0.000054> [pid 21732] read(3, "#\n# hosts.allow\tThis file descri"..., 4096) = 161 <0.000102> [pid 21732] read(3, "", 4096) = 0 <0.000044> [pid 21732] close(3) = 0 <0.000044> [pid 21732] munmap(0xb7fdc000, 4096) = 0 <0.000072> [pid 21732] open("/etc/hosts.deny", O_RDONLY) = 3 <0.000050> [pid 21732] fstat64(3, {st_mode=S_IFREG|0644, st_size=347, ...}) = 0 <0.000041>
6.
[root@server-mysql ~]# strace -e trace=file ls
execve("/bin/ls", ["ls"], [/* 35 vars */]) = 0access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory)open("/etc/ld.so.cache", O_RDONLY) = 3open("/lib64/libselinux.so.1", O_RDONLY) = 3open("/lib64/librt.so.1", O_RDONLY) = 3open("/lib64/libcap.so.2", O_RDONLY) = 3open("/lib64/libacl.so.1", O_RDONLY) = 3open("/lib64/libc.so.6", O_RDONLY) = 3open("/lib64/libdl.so.2", O_RDONLY) = 3open("/lib64/libpthread.so.0", O_RDONLY) = 3open("/lib64/libattr.so.1", O_RDONLY) = 3statfs("/selinux", {f_type=0xf97cff8c, f_bsize=4096, f_blocks=0, f_bfree=0, f_bavail=0, f_files=0, f_ffree=0, f_fsid={0, 0}, f_namelen=255, f_frsize=4096}) = 0open("/usr/lib/locale/locale-archive", O_RDONLY) = 3open(".", O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC) = 3