php轨迹,php如何打印程序的执行轨迹?

如题,php如何打印程序的执行轨迹?

经测试,debug_backtrace()只能应用在函数中,打印出从程序开始一直到当前函数中的执行轨迹。

但是,如果在程序的主文件的结尾处使用debug_backtrace(),无法记录之前调用过并已经执行结束的函数。

主要的需求是要在主文件的结尾打印程序执行过程中调用过的全部函数,请问是否有实现的可能。

ps:除了自己在每个函数都添加日志的方法。

可以考虑使用 Xdebug 来实现,有几个目录需要注意一下

一、trace_output_dir 函数调用监测信息的输出文件目录

二、profiler_output_dir 效能监测信息的输出文件目录

这个目录下的文件内容并不为人类轻易理解,所以我们还需要一个工具:wincachegrind。

① 下载安装wincachegrind

② 安装运行后,设定你的working folder(php.ini里xdebug.profiler_output_dir的值)

附:下载链接 https://sourceforge.net/proje…

三、输出的目录,文件夹须手动创建

Xhprof – PHP函数级分层性能分析工具

//from http://blog.csdn.net/u013707844/article/details/26453551

wget http://pecl.php.net/get/xhprof-0.9.2.tgz

tar zxvf  xhprof-0.9.2.tgz

cp ./xhprof-0.9.2.tgz ./www         //xhprof自身带有一个web版的分析页面,放到我的web服务器下面

cd xhprof-0.9.2/extension

/usr/local/php/bin/phpize

./configure --enable-xhprof --with-php-config=/usr/local/php/bin/php-config

make && make install

vi php.ini

[xhprof]

extension=xhprof.so

xhprof.output_dir=/home/zhangy/xhprof  //如果不加存放目录的话,默认是放在/tmp下面

function bar($x) {

if ($x > 0) {

bar($x -1);

}

}

function foo() {

for ($idx = 0; $idx < 5; $idx++) {

bar($idx);

$x = strlen("abc");

}

}

//启动xhprof

xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY);

//调用foo函数,也是我们要分析的函数

foo();

//停止xhprof

$xhprof_data = xhprof_disable();

//取得统计数据

print_r($xhprof_data);

$XHPROF_ROOT = realpath(dirname(__FILE__) . '/..');

include_once $XHPROF_ROOT . "/xhprof_lib/utils/xhprof_lib.php";

include_once $XHPROF_ROOT . "/xhprof_lib/utils/xhprof_runs.php";

//保存统计数据,生成统计ID和source名称

$xhprof_runs = new XHProfRuns_Default();

$run_id = $xhprof_runs->save_run($xhprof_data, "xhprof_foo"); //source名称是xhprof_foo

//弹出一个统计窗口,查看统计信息

echo "<script language='javascript'>window.open('../xhprof_html/index.php?run=" . $run_id . "&source=xhprof_foo');</script>";

搜索了下,看看这个 https://segmentfault.com/q/10…

7f16f4357ecfd0f985f574518ef2f82a.png

xdebug.profiler

输出的信息

cachegrind.out

使用

kcachegrind

查看也可以看到PHP函数调用关系和耗时.

PHP内置的一些魔术常量:

http://php.net/manual/zh/lang…

__DIR__,__FILE__,__LINE__,__FUNCTION__,__CLASS__

__DIR__是5.3开始加入的,等价于dirname(__FILE__).

查看xdebug扩展包含的函数:

php -r 'print_r(get_extension_funcs("xdebug"));'

http://www.xdebug.org/docs/all

http://www.xdebug.org/docs/all_functions

xdebug_get_stack_depth

xdebug_get_function_stack

xdebug_get_formatted_function_stack

xdebug_print_function_stack 显示当前函数栈,内容包括各个函数的CPU和内存变动

xdebug_get_declared_vars

xdebug_call_class

xdebug_call_function

xdebug_call_file

xdebug_call_line

xdebug_var_dump

xdebug_debug_zval

xdebug_debug_zval_stdout

xdebug_enable

xdebug_disable

xdebug_is_enabled

xdebug_break

xdebug_start_trace

xdebug_stop_trace

xdebug_get_tracefile_name

xdebug_get_profiler_filename

xdebug_dump_aggr_profiling_data

xdebug_clear_aggr_profiling_data

xdebug_memory_usage

xdebug_peak_memory_usage

xdebug_time_index

xdebug_start_error_collection

xdebug_stop_error_collection

xdebug_get_collected_errors

xdebug_start_code_coverage

xdebug_stop_code_coverage

xdebug_get_code_coverage

xdebug_code_coverage_started

xdebug_get_function_count

xdebug_dump_superglobals

xdebug_get_headers

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值