How to set breakpoint on user defined PHP functions with dbg?
I can break on built-in functions like preg_replace
by break on php_pcre_replace
,
but my own function(function func(){...}
),how can开发者_运维技巧 I set breakpoint on it?
If your function is defined in the PHP interpreter itself, and is basically interpreted and evaluated at run-time rather than being a pre-complied C-based plug-in, then you're not going to have access to it from gdb unless you were going to step through the actual PHP interpreter execution loop itself, and even then, I'm not sure how you would know exactly when it was your function being called. Some possibilities that could work are either monitoring all calls to PHP's execute()
, or possibly issue a SIGTRAP
using posix_kill
and posix_getpid
on the PHP processes itself inside your function in order to stop gdb at the point when your function executes. The built-in functions on the other-hand are actually implemented in C, so when the interpreter calls those functions, it is making a call into compiled code, and therefore can easily have breakpoints set in gdb.
So for instance, adding this line inside your PHP function (probably right at the start)
posix_kill(posix_getpid(), SIGTRAP);
should stop the PHP process you're monitoring with gdb at the point-of-call, which would be inside your user-defined PHP function in the interpreter. Depending on your OS, the value of SIGTRAP may not be defined, so you may have to look in your OS's signal.h file to see what signal values would be appropriate. But SIGTRAP is available on any POSIX-compliant OS.
Finally, be sure to remove this line when you are not using gdb, otherwise you'll just terminate the PHP process running your script when the signal is raised.
精彩评论