tags:

views:

59

answers:

3

I'm curious about how some built in functions are implemented,but it's very time consuming to look it up directly in the source,is there a tool that can automate this?

EDIT

Or is there a tool that can debug into the c code that's actually executed?

+1  A: 

Most (all?) of the functions that can be accessed from PHP are defined under the ext/ directory in the PHP source code. If you have a recursive search tool, search for PHP_FUNCTION - if you saved the results of that search into a text file, it would be a pretty good "index" for figuring out where a PHP builtin is defined.

The really core stuff is in ext/standard.

Some rare "functions" are implemented directly as opcodes in the Zend virtual machine that PHP compiles to, so there isn't a well defined C function as such. I think strlen is such a function, for instance.

Andrew
Isn't ext/ for functions in extensions?What about those functions that doesn't need any extension,like `get_class`?
The vast majority of stuff is in "extensions," even those things that are considered part of the core language.I'm pretty sure that get_class is another one of those functions that compiles down to an opcode. You can use vld ( http://derickrethans.nl/projects.html#vld ) to see the opcodes that a particular PHP script is compiled to. The actual code that is behind those opcodes can be found in Zend/zend_vm_def.h. Be sure to read the comments at the top of that file if you edit it, there are some extra build steps required.
Andrew
A: 

I believe that you should take a look at this.
Facebook has developed a tool to convert PHP code into c++.
So I guess it can handle C as well to some extent.

the_drow
Why the downvote? Did I misinterpret the question?
the_drow
A: 

About the debugging the C code that's executed, I suppose it's possible to use something like dbg ; you'll first have to recompile PHP with the --enable-debug mode, though.

For more informations, you can take a look at :


I've never used this to debug PHP itself, but I've used those two pages to generate some backtraces of a crash I had with an extension, and it worked OK, from what I remember.


As a sidenote : using a PHP compiled with --enable-debug, you might have to recompile some of the extensions you're using and change the way they're loaded (it's the case for Xdebug, for instance) ; and some other might just not work at all anymore.

Pascal MARTIN
Is there an easier and safer approach to dig deeper into PHP?
I don't see what is "unsafe" here : if you want to debug/develop, you know what you're doing, obviously, and building PHP with `--enable-debug` will allow you to know more about what's going wrong, when something does. ;;; about the easier part, not sure ; I'd say going through the source code would be the best solution, but I have to admit, it won't be "easy" -- it's almost never "easy" to discover such a big code base...
Pascal MARTIN
I mean "some other might just not work at all anymore" should not ever happen by "safer"..
Humph, oh, yes, there is this ^^ ;;; Well, most extensions should work OK, if you recompile them ;; but some are not necessarily open source, which means you won't be able to recompile them ;-) ;;; anyway, in most cases, it should be OK for a development environment *(and you'll never use `--enable-debug` on a production server)*
Pascal MARTIN