Debug PHP extensions using gdb
From: http://www.codefrom.com/paper/%E4%BD%BF%E7%94%A8gdb%E8%B0%83%E8%AF%95p...
Php extensions are developed using c/c ++ and can be easily debugged using gdb. The procedure is as follows:
Add the ** -- enable-debug ** parameter when compiling php.
./configure --enable-debugmake && make install
Tested on my ubuntu machine, the extended directory defaults to/usr/local/lib/php/extensions/debug-non-zts-20131226/
In this way, php source code debugging is also very convenient.
Next, create extension, go to the ext Directory of the php source code, and run
./ext_skel --extname=mydebug
The current directory is automatically generated.MydebugDirectory, enter the directory, and editConfig. m4File, remove 10 ~ 12 rows of dnl, as shown below
PHP_ARG_WITH(mydebug, for mydebug support,Make sure that the comment is aligned:[ --with-mydebug Include mydebug support])
Add
if test -z "$PHP_DEBUG"; then AC_ARG_ENABLE(debug, [--enable-debg compile with debugging system], [PHP_DEBUG=$enableval], [PHP_DEBUG=no] )fi
This indicates that the extension can be debugged, then the extension is compiled, and the command is used.
phpize ./configure --enable-debugmake && make install
The phpize and php-config here need to configure the environment variables in advance and then load the extension. The address above my machine is/usr/local/lib/php/extensions/debug-non-zts-20131226 /. Go to the mydebug extension source code directory. the default generated function is confirm_mydebug_compiled, which is defined in mydebug. c. the automatically generated function is extended.
PHP_FUNCTION(confirm_mydebug_compiled){ char *arg = NULL; int arg_len, len; char *strg; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &arg, &arg_len) == FAILURE) { return; } len = spprintf(&strg, 0, "Congratulations! You have successfully modified ext/%.78s/config.m4. Module %.78s is now compiled into PHP.", "mydebug", arg); RETURN_STRINGL(strg, len, 0);}
Obtain the string parameters and combine them into a string to return the result. Run the nm command to view the generated mydebug. so exported symbol.
Run nm mydebug. so and return zif_confirm_mydebug_compiled ......
PHP_FUNCTION is actually adding zif _ before the function name, and then performing gdb debugging
Step 1: run gdb php and then run: break zif_confirm_mydebug_compiled terminal prompt: Function "zif_confirm_mydebug_compiled" not defined. Make breakpoint pending on future shared library load? (Y or [n]) input: y input: run/tmp/test. php will echo: Breakpoint 1, timeout (ht = 1, return_value = 0xb7bf0d44, return_value_ptr = 0xb7bd6104, this_ptr = 0x0, return_value_used = 1) /..... /php-5.6.6/ext/mydebug. c: 56 then input: l Display: 54 PHP_FUNCTION (confirm_mydebug_compiled) 55 {56 char * arg = NULL; 57 int arg_len, len; 58 char * strg; 5960 if (zend_parse_parameters (ZEND_NUM_ARGS () TSRMLS_CC, "s", & arg, & arg_len) = FAILURE ){
The file/tmp/test. php contains the following content:
As you can see, the function source code has come out and can be debugged using the commonly used gdb command.
More exciting original content into http://www.codefrom.com/