strace 多进程跟踪

strace 多进程跟踪

追踪多个进程方法
当有多个子进程的情况下,比如php-fpm、nginx等,用strace追踪显得很不方便。可以使用下面的方法来追踪所有的子进程。

# vim /root/.bashrc //添加以下内容
function straceall {
    strace $(pidof "${1}" | sed 's/\([0-9]*\)/-p \1/g')
}
source /root/.bashrc
traceall php-fpm //监控phpfpm

或者

strace -tt -T $(pidof 'php-fpm: pool www' | sed 's/\([0-9]*\)/\-p \1/g')

追踪web服务

strace -f -F -s 1024 -o nginx-strace /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
strace -f -F -o php-fpm-strace /usr/local/php/sbin/php-fpm -y /usr/local/php/etc/php-fpm.conf

追踪mysql

strace -f -F -ff -o mysqld-strace -s 1024 -p mysql_pid
find ./ -name "mysqld-strace*" -type f -print |xargs grep -n "SELECT.*FROM"

查看程序做了什么

#!/bin/bash
# This script is from http://poormansprofiler.org/
nsamples=1
sleeptime=0
pid=$(pidof $1)

for x in $(seq 1 $nsamples)
  do
    gdb -ex "set pagination 0" -ex "thread apply all bt" -batch -p $pid
    sleep $sleeptime
  done | \
awk '
  BEGIN { s = ""; } 
  /^Thread/ { print s; s = ""; } 
  /^\#/ { if (s != "" ) { s = s "," $4} else { s = $4 } } 
  END { print s }' | \
sort | uniq -c | sort -r -n -k 1,1

phpstrace
phpstrace追踪php进程

Usage: ./php-strace [ options ]
-h|--help               show this help
-l|--lines <integer>    output the last N lines of a stacktrace. Default: 100
--process-name <string> name of running php processes. Default: autodetect
--live                  search while running for new upcoming pid's