php 函数追踪扩展 phptrace

介绍

phptrace 是一个低开销的用于跟踪、分析 php 运行情况的工具。

它可以跟踪 php 在运行时的函数调用、请求信息、执行流程、并且提供有过滤器、统计信息、当前状态等实用功能。

在任何环境下,它都能很好的定位阻塞的问题以及在高负载下进行 denbug

官方源码:https://github.com/Qihoo360/phptrace。

安装

pecl 方式安装

$ pecl install trace

源码方式安装

$ wget https://pecl.php.net/get/trace-1.0.0.tgz # 下载源码
$ tar -xf trace-1.0.0.tgz # 解压文件
$ cd trace-1.0.0/extension # 进入扩展目录

源码编译

$ whereis php-config # 找到 php-config 的路径
$ phpize
$ ./configure --with-php-config=/usr/bin/php-config # 这里的 --with-php-config 是上一步找到的路径
$ make # 编辑
$ make test # 编译测试
$ make cli # 命令行工具
$ make install-all # 安装 php 扩展,命令行工具到 php 目录

修改 php.ini

php.ini 配置文件中增加以下配置信息。

[phptrace]
extension=trace.so
phptrace.enabled=1

注:修改 php.ini 后,需要手动重启 php-fpm

检查是否生效

root@639ca1f15214:~# php -m | grep trace
trace

实际测试

root@639ca1f15214:/var/www# php -r 'for ($i = 0; $i < 20; $i++) { sleep(5); }' &
[1] 361
root@639ca1f15214:/var/www# phptrace -p $!
process attached
[pid 361] > sleep(5) called at [Command line code:1]
[pid 361] < sleep(5) = 0 called at [Command line code:1] ~ 5.001s 5.001s
[pid 361] > sleep(5) called at [Command line code:1]
[pid 361] < sleep(5) = 0 called at [Command line code:1] ~ 5.000s 5.000s
[pid 361] > sleep(5) called at [Command line code:1]
[pid 361] < sleep(5) = 0 called at [Command line code:1] ~ 5.000s 5.000s
[pid 361] > sleep(5) called at [Command line code:1]
[pid 361] < sleep(5) = 0 called at [Command line code:1] ~ 4.966s 4.966s
[pid 361] > sleep(5) called at [Command line code:1]
[pid 361] < sleep(5) = 0 called at [Command line code:1] ~ 5.000s 5.000s
[pid 361] > sleep(5) called at [Command line code:1]
[pid 361] < sleep(5) = 0 called at [Command line code:1] ~ 5.002s 5.002s
[pid 361] > sleep(5) called at [Command line code:1]
[pid 361] < sleep(5) = 0 called at [Command line code:1] ~ 5.001s 5.001s
[pid 361] > sleep(5) called at [Command line code:1]
[pid 361] < sleep(5) = 0 called at [Command line code:1] ~ 5.002s 5.002s
[pid 361] > sleep(5) called at [Command line code:1]
[pid 361] < sleep(5) = 0 called at [Command line code:1] ~ 5.001s 5.001s
[pid 361] > sleep(5) called at [Command line code:1]
[pid 361] < sleep(5) = 0 called at [Command line code:1] ~ 4.966s 4.966s
[pid 361] > sleep(5) called at [Command line code:1]
[pid 361] < sleep(5) = 0 called at [Command line code:1] ~ 5.000s 5.000s
[pid 361] > sleep(5) called at [Command line code:1]
[pid 361] < sleep(5) = 0 called at [Command line code:1] ~ 5.003s 5.003s
[pid 361] > sleep(5) called at [Command line code:1]
[pid 361] < sleep(5) = 0 called at [Command line code:1] ~ 5.000s 5.000s
[pid 361] > sleep(5) called at [Command line code:1]
[pid 361] < sleep(5) = 0 called at [Command line code:1] ~ 5.001s 5.001s
[pid 361] > sleep(5) called at [Command line code:1]
[pid 361] < sleep(5) = 0 called at [Command line code:1] ~ 5.001s 5.001s
[pid 361] > sleep(5) called at [Command line code:1]
[pid 361] < sleep(5) = 0 called at [Command line code:1] ~ 4.967s 4.967s
[pid 361]> swoole_call_user_shutdown_begin() called at [(null):0]
[pid 361]< swoole_call_user_shutdown_begin() = true called at [(null):0] ~ 0.000s -79.912s
[pid 361]< cli php for ($i = 0; $i < 20; $i++) { sleep(5); }
process detached
05-11 20:14