Debug memory of PHP module

Posted on 2008-07-09 21:55:41 EEST.

The first question I ask to myself. What's the difference between PHP module and extension? Is it like, that PHP has modules while Zend extensions? If yes, why? If not, why there are both types? There are no comments allowed, so questions remain unanswered. Well... as usual, I would say. That's nature of Zend/PHP support.

Anyway, you wrote PHP module and want to debug or profile its memory. Or you just want to catch all memory leaks. The first action is to run php via valgrind, but in such case, the log file is far from being helpfull:

==17786== 4,096 bytes in 1 blocks are definitely lost in loss record 843 of 860
==17786==    at 0x4C20FEB: malloc (vg_replace_malloc.c:207)
==17786==    by 0x4C21134: realloc (vg_replace_malloc.c:429)
==17786==    by 0xCFAD9C8: ???
==17786==    by 0xCF93A59: ???
==17786==    by 0xCD533D6: ???
==17786==    by 0xCAFB918: ???
==17786==    by 0xDEE3D40: ???
==17786==    by 0xBA69642: ???
==17786==    by 0xBA6E924: ???
==17786==    by 0xBA67E89: ???

This is not what you expected. And to be honest, this is something which tells you nothing but nothing :)

PHP just unloads your module, so there's nothing which can be mentioned in log file. Luckilly, I found that module ( zend_module_entry ) has handle member which, if set to 0 tells zend, that your module should not be unloaded.

In practice, you should add similiar code to RSHUTDOWN:

zend_module_entry *module;
int rv = zend_hash_find(&module_registry, "module_name", strlen("module_name")+1, (void**)&module);

if(rv == SUCCESS)
    module->handle = 0;

Once it's added, recompile module and run php via valgrind. Keep in mind, this code should be used for debug purposes only.

Back

Layout Copyright © 2006 Finnish Teleservice Center Ltd Oy - Site Powered by Midgard CMS