2015-09-30

PHP 的 stack trace

若是要追踨單支php的stack,可用如下做法:
$debug = debug_backtrace();
file_put_contents("/path/to/log.txt", date("Y-m-d H:i:s",time())." file:".$debug[1]['file']." php_thread:".getmypid().", mysql_thread:".mysql_thread_id(self::$Link_ID)."(".self::$Link_ID.")\r\n", FILE_APPEND|LOCK_EX);

若是要追踨整個php的stack,可能得用gdb,且前提是php在compile時有加入 --enable-debug 選項:
$ gdb -p $PHP_PID
(gdb) bt     # Get a system-level stacktrace, might already give some info
(gdb) source /path/to/php-src/.gdbinit # Load some useful macros
(gdb) dump_bt executor_globals.current_execute_data
            # Macro from PHP's .gbinit giving PHP stack trace
            # If you for whatever reason are using a thread-safe PHP build you have to do this:
(gdb) ____executor_globals
(gdb) dump_bt $eg.current_execute_data