Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to debug into internal c code of PHP?

Tags:

c

php

debugging

Has anyone here tried it or is it possible?

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?

like image 960
user198729 Avatar asked Feb 27 '23 08:02

user198729


1 Answers

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.

like image 102
Tim Post Avatar answered Mar 07 '23 23:03

Tim Post