9

PHP provides some impressive introspection facilities: get_defined_vars, get_defined_functions, get_defined_constants, debug_backtrace, and others. Essentially, these provide views of the entire program state: the stack and the heap. I wonder how complete a view of the program state one can get using these facilities.

The heap and all defined variables in scope can be modelled as a labelled directed graph. So is it possible, for example, to write something that will give me a Graphviz/DOT depiction of this? I'm imagining something similar to the diagrams in this article about 'How PHP manages variables', or to the diagrams in the PHP manual page on garbage collection.

jameshfisher
  • 34,029
  • 31
  • 121
  • 167
  • I don't think there's any way to variables in another scope, so you will probably be unable to model all of the interesting things. I believe Xdebug can give you the state of all of the variables at runtime; you may be able to patch it to write that info somewhere (or maybe it can already do that). – Waleed Khan Dec 18 '13 at 18:14
  • @WaleedKhan I took a look at Xdebug but it just seems to provide a prettier `var_dump`. (I did find [this GDB extension](https://github.com/mcfunley/php-heap) for analysing the heap, but haven't tried it as it looks like it requires significant setup, and was written three years ago so may not work.) – jameshfisher Dec 18 '13 at 18:27
  • 2
    What is state? I mean I can have a conditional that when true defines class A w/ constant B as "foo" and when false does the same thing but uses "bar" as the constant. Also, you might be interested how PHPUnit does it: https://github.com/sebastianbergmann/phpunit/blob/3.7/PHPUnit/Util/GlobalState.php – Chad Retz Dec 18 '13 at 20:50
  • @ChadRetz see [Wikipedia article on program state](https://en.wikipedia.org/wiki/Program_state#Program_state). Essentially, it's all the memory used by the program: variables, the heap, the stack, constants, functions, and possibly other things. I'm most interested in seeing the heap, or the subset of it that is in scope. – jameshfisher Dec 18 '13 at 21:19
  • @jameshfisher xdebug is so much more than that: https://jtreminio.com/2012/07/xdebug-and-you-why-you-should-be-using-a-real-debugger/ – Juan Treminio Dec 18 '13 at 21:50
  • @JuanTreminio ah, it seems I misunderstood. I've just set it up, and it seems good. Unfortunately the heap inspector doesn't recognise sharing or circular references, or at least doesn't tell me about them. For example, mutual reference between two arrays `$a` and `$b` results in me being able to infinitely expand the "tree". Worse, it seems to lie sometimes. An array `$a` with a reference to itself ($a = []; $a['a'] =& $a;) results in it saying that `$a['a']` has no elements, which isn't true (e.g., I can `var_dump($a['a']['a']);`). Still, a useful tool. – jameshfisher Dec 18 '13 at 22:51
  • (@JuanTreminio it's worth noting that that "lying" behavior is the same as what `var_dump` does when it encounters loops.) – jameshfisher Dec 19 '13 at 01:00

4 Answers4

1

Checkout xdebug plus the xdebug chrome extension. https://chrome.google.com/webstore/detail/xdebug-helper/eadndfjplgieldjbigjakmdgkmoaaaoc?hl=en

The extension can be used to enable profiling / debugging for php. You can link it with eclipse or any other ide that supports xdebug for debugging.

If you generate a profile you can put the results into kcachegrind or wincachegrind to get a map view of memory allocation, time spent in each function and other things.

https://www.youtube.com/watch?v=YHKFdfbcP8U

Rich Wandell
  • 123
  • 1
  • 6
1

If you generate a profile you can put the results into kcachegrind or wincachegrind to get a map view of memory allocation, time spent in each function and other things.

https://www.youtube.com/watch?v=YHKFdfbcP8U

Ravi Chauhan
  • 1,409
  • 13
  • 26
0

I don't know if there are any existing tools for but you should definitely check out the xdebug profiler http://xdebug.org/docs/profiler coupled with kcachegrind. It will give you a visualization of the entire stack: every function that was called, how many times it was called and how long it/they took.

0

get_defined_vars
get_defined_functions
get_defined_constants

Is there a way to get user-defined php functions, variables, constants from a php file? Following functions are not the best way to do so because they get all decalred functions/vars/constants (with hundreds of php's built-in constants and internal php functions

Ravi Chauhan
  • 1,409
  • 13
  • 26