以前,在对php进行性能问题分析时,主要是依赖xhprof对php性能瓶颈进行分析,而这需要侵入代码。今天遇到了需要在生产环境下找出fpm问题的需求,这种情况下,在网上查找了下方案,发现了strace工具。
strace是个功能强大的Linux调试分析诊断工具,可用于跟踪程序执行时进程系统调用(system call)和所接收的信号,尤其是针对源码不可读或源码无法再编译的程序。
指令用法:
strace [-CdffhiqrtttTvVxxy] [-I n] [-e expr]...
[-a column] [-o file] [-s strsize] [-P path]...
-p pid... / [-D] [-E var=val]... [-u username] PROG [ARGS]
或者
strace -c[df] [-I n] [-e expr]... [-O overhead] [-S sortby]
-p pid... / [-D] [-E var=val]... [-u username] PROG [ARGS]
选项说明:
-c -- count time, calls, and errors for each syscall and report summary #计算每次系统调用的次数,以及错误次数
-C -- like -c but also print regular output # 功能与-c相同,但添加了定时输出结果的功能
-d -- enable debug output to stderr #输出trace的debug信息
-D -- run tracer process as a detached grandchild, not as parent #使用子进程来trace需要监控的进程
-f -- follow forks, -ff -- with output into separate files #跟踪当前进程以及子进程
-i -- print instruction pointer at time of syscall #输出每次系统调用方法所在code段的偏移地址
-q -- suppress messages about attaching, detaching, etc. #对附件信息进行压缩处理
-r -- print relative timestamp, -t -- absolute timestamp, -tt -- with usecs 打印执行时间点
-T -- print time spent in each syscall #打印每次系统调用的时间间隔
-v -- verbose mode: print unabbreviated argv, stat, termios, etc. args
-x -- print non-ascii strings in hex, -xx -- print all strings in hex
-y -- print paths associated with file descriptor arguments
-h -- print help message, -V -- print version
-a column -- alignment COLUMN for printing syscall results (default 40)
-b execve -- detach on this syscall
-e expr -- a qualifying expression: option=[!]all or option=[!]val1[,val2]...
options: trace, abbrev, verbose, raw, signal, read, write
-I interruptible --
1: no signals are blocked
2: fatal signals are blocked while decoding syscall (default)
3: fatal signals are always blocked (default if '-o FILE PROG')
4: fatal signals and SIGTSTP (^Z) are always blocked
(useful to make 'strace -o FILE PROG' not stop on ^Z)
-o file -- send trace output to FILE instead of stderr #将调试结果输出的指定文件
-O overhead -- set overhead for tracing syscalls to OVERHEAD usecs
-p pid -- trace process with process id PID, may be repeated #使用pid来trace进程
-s strsize -- limit length of print strings to STRSIZE chars (default 32)
-S sortby -- sort syscall counts by: time, calls, name, nothing (default time)
-u username -- run command as username handling setuid and/or setgid #使用指定用户来执行strace指令
-E var=val -- put var=val in the environment for command
-E var -- remove var from the environment for command
-P path -- trace accesses to path
基本的调试fpm的用法:
strace -f -p 进程pid -e trace=file #跟踪以指定文件名做参数的所有系统调用。
//---to---do