How to Pinpoint the PHP Code Behind CPU Spikes Using GDB
When a PHP process suddenly consumes near‑100% CPU, this guide shows how to locate the exact file, function and line causing the spike by inspecting Zend executor globals with GDB, using both manual commands and a ready‑made .gdbinit script.
When a PHP process that normally uses little CPU suddenly spikes to almost 100%, you need to identify the exact code line responsible. The approach is to inspect the Zend executor globals to find the currently executing file, function, and line number.
The key structures are executor_globals.active_op_array and executor_globals.current_execute_data. active_op_array holds the filename and function_name, while current_execute_data contains the currently executing opline whose lineno field gives the line number.
<code>struct _zend_executor_globals { ... zend_op_array *active_op_array; struct _zend_execute_data *current_execute_data; ... }; </code>
<code>struct _zend_op_array { char *filename; char *function_name; zend_op *opcodes; ... }; </code>
<code>struct _zend_execute_data { struct _zend_op *opline; zend_op_array *op_array; ... }; </code>
<code>struct _zend_op { uint lineno; // source line number zend_uchar opcode; // opcode identifier ... }; </code>
Using GDB, attach to the PHP process (e.g., PID 14973) and run:
print (char *)executor_globals.active_op_array->filename
print (char *)executor_globals.active_op_array->function_name
print executor_globals->current_execute_data->opline->linenoThe output shows the script path, function name, and line number currently being executed, allowing you to pinpoint the hot loop (e.g., a sleep(1) call on line 4).
If typing these commands each time is cumbersome, place the following in a .gdbinit file in your home directory (the file is included in the PHP source tree):
source /home/youruser/.gdbinit
zbacktraceThe custom zbacktrace command prints a backtrace with file names and line numbers, e.g.:
[0xa453f34] sleep(1) /home/xinhailong/test/php/test.php:4
[0xa453ed0] test1() /home/xinhailong/test/php/test.php:7From PHP 5.6 onward, the built‑in phpdbg debugger offers similar capabilities for PHP scripts.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
21CTO
21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
