开发者

How to debug into internal c code of PHP?

Has anyone here tried it or is it possib开发者_开发技巧le?

I've been using PHP for quite a few years but never know exactly the underlying c scripts.

Is there a way to go into it?


I've done a bit of hacking on Zend PHP. I find it to be overly clever , some people go as far as calling it deliberately obfuscated in plain view. The source code to PHP is a mind altering (or breaking) substance, depending on how good you are at deciphering very cryptic macros. That's my impression of the core.

Writing extensions, however, is a breeze once you get the hang of the Zend helpers, most people with advanced-beginner / intermediate knowledge of C could get through a basic extension. There's also plenty of examples. One of the best parts of PHP is how organized the build system is, dropping in new stuff is relatively painless. With a little work and patience, almost any C library is rather easily extended to PHP.

If you aren't well versed in C (and what borders on abuse of the preprocessor), a glance at the PHP core is not going to give you much insight, nor is it a good thing to reference if you are learning C on your own.

Moving On:

Don't let anything I've said, or what anyone else has to say discourage you from grabbing the code and looking for yourself. That being said, as for debugging goes:

  • Valgrind (unless you use a lot of suppressions) is not very helpful. PHP (to the best of my knowledge) uses arch optimized reads, similar to new versions of glibc. I.e. its going to read 32 bits even if it only swallows 8 bits and a trailing NULL.

  • I have never found GDB to be very helpful with PHP. A lot of the magic is in macros that are very hard to trace.

  • You will quickly see the Zend error logging functions, and their version of assertions. Use those, printf() debugging is pretty much useless unless your debugging a CLI app.

  • Garbage collection can make you see odd things when using tools like valgrind's massif. Profiling heap use in PHP is an art I have not yet discovered.

Finally, I'd like to say its always nice to see someone take a look under the hood of their language. SO could use some questions that helps de-mystify the PHP core, so please feel free to post more as you go :)

Also, remember, Zend isn't the only forge that makes php. While compatibility with Zend is paramount if you hope for it to be adopted, everyone is still free to do their own thing.


I've never debugged the C code of PHP (nor extensions), but I've sometimes generated backtraces, in cases of crashes in PHP extensions.

This page might help, about that : Generating a gdb backtrace.

Starting from there, maybe you'll be able to go farther...


From some of your questions it sounds like you don't know that PHP is open-source, you can download the complete source code of it and look through all the C functions. If you want backtracing and debugging ability, you have to do what Pascal MARTIN said.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜