Debugging PHP Code with debug_backtrace - php

I love to save time using someone else's code. And I want to start effectively debugging my scripts, as well as scripts I inherit from other developers.
I've been reading up on debug_backtrace(), and I'm not sure if it's what I'm looking for.
Basically,when a class is instantiated I want to know what methods are being fired.
Truthfully, I'd like to know as much as possible, but knowing what's going on inside a single class would be fantastic.
<?php
require('aHugeComplicatedClass.php'); // sooooo many methods
try {
$obj = new aHugeComplicatedClass($params);
}
catch(Exception $ex){
var_dump($ex);
}
?>
From PHP's docs about debug_backtrace, it looks like I need to place the debug_backtrace() function inside each method/function within any and all classes, just to see how it was reached.
I gotta be reading this too literal. That would be a ton of modifications.
So, if I have a php file, that instantiates a class, and I know this class is extended from other classes, what's the simpliest way to debug that Object?

I would install XDebug and hook up the remote debugging to your IDE (e.g PhpStorm or Eclipse), that way you will get nice stack dumps on all errors, plus the ability to breakpoint your code and inspect the stack and all object internals at your leisure.
http://xdebug.org/
You can also use it to profile your application call chains without making any code changes (which sounds more like what you are wanting). By using the profiling options, which generate big log files, you can then load these logs into webgrind and visually inspect who's calling what in nice tree structures.
https://code.google.com/p/webgrind/
The Zend tool chain would also provide this kind of deeper debugging functionality out of the box.
Alternatively install an Application Performance Monitoring agent such as App Dynamics or New Relic for similar code-profiling. This is most useful for remote installations (i.e. production) where debugging isn't an option and profiling is expensive.

We use NuSphere PhpED for getting all such things. It can trigger debugger to stop on specified exceptions and/or errors and shows complete call-stack that may include php functions calls, php method calls, embedded functions calls and embedded method calls.
http://www.nusphere.com/products/phped.htm
I was told in the beginning that their debugger is the best and can confirm this. It stems from the OSS project
http://sourceforge.net/projects/dbg2/
With PhpED IDE we run full cycle of development -- coding, debugging, profiling, testing and uploading to the production server.

Related

PHP code analyzer to determine classes/extensions used

Problem
I have a legacy codebase I need to analyze and determine dependencies. Particularly the dependencies on classes (internal/external) and extensions (Memcache, PDO, etc).
What I've Tried
I have reviewed the tools listed in Is there a static code analyzer for PHP files?. Unfortunately, this post is dated and most of the promising tools like phpCallGraph no longer work.
My thought was to analyze the code lexically and look for class tokens. Reviewing a list of these would at least allow me to visually determine dependencies. However finding OtherClass in the following code may be complex:
$classname = 'OtherClass';
echo $classname::doubleColon();
In the end, I doubt I'm the first to need this. I'm sure a tool or combination of tools exist to provide what I need. So I'm asking the SO community before writing this myself.
Update
Ideally this tool will analyze multiple files with complete code coverage. As such, tools like Xdebug, while great, are not solutions to this exact problem.
Instead of phpCallGraph you could use Gopal Vijayaraghavan's inclued extension which in combination with Graphviz gives you a nice looking graph of all included files for a certain execution path.
Example:
Moreover, I'd recommend Xdebug (a PHP debugger) which offers a profiler that outputs data consumable by Valgrind. Use the debugger with a compatible IDE to follow the execution path (which helped me a lot to wade thru e.g. Drupal's massive call-stack).
Combine both and you should get a fairly thourough overview.
EDIT
Searched the web and found nWire for PHP - an eclipse plugin that looks like it could be the right tool for you (30 day free trial which should be enough to give you a head start).
I think PhpCodeAnalyzer is exactly what you're looking for - https://github.com/wapmorgan/PhpCodeAnalyzer
It print list of all used external extensions in code base.

Flow3 Debugging

I'm currently evaluating Flow3 for an upcoming project. The AOP Pattern and Dependency Injection would be just ideal for our purpose.
Now what I can't figure out is how to debug some results in a controller Action.
public function testAction() {
$beans = $this->coffeeBeanRepository->findAll();
var_dump($beans); // doesn't work, browser crashes
}
What I've tried:
Debugging with PHPStorm and XDEBUG (this is how I do it usually)
var_dump / print_r / print
You want to use FLOW3's var_dump, which deals with some of the recursion that comes from deeply nested objects and causes your browser to cache:
\TYPO3\FLOW3\var_dump()
XDebug can be tricky in FLOW3 because FLOW3 creates proxy classes for your classes in order to make all the AOP magic happen. Still, I know the developers here are using xdebug_break() successfully in PHPStorm on their FLOW3 projects, so it's definitely doable.
Xdebug is not so tricky, your cached PHP files must be used to debug purpose, if you need to debug MyController, search for a MyController_Original in the cache directory and put your break point in this file.
You can also use debugproxy.php to improve the usage of xDebug with Flow:
https://github.com/sandstorm/debugproxy
In the newest version of Typo3 Flow you have to use:
\TYPO3\Flow\var_dump('test');

Is there a way to get internal data from php?

I know there are PHP debugging tools available, but I'm curious about doing something like this myself.
Is there a way to obtain the data that is being processed by PHP for debugging purposes? For example, without having to change the code of my PHP application, is there some way, when I run a function, I can see what variables exist within that function, what called that function, what the return value was etc?
The solution doesn't specifically doesn't need to be in PHP, ie this could be somethig that is written in C etc
Well, may I suggest you look into adding firePHP to your code. FirePHP has 2 components, a server side component, and a browser component for firefox, it uses the firebug addon. Once installed properly you can do things like FB::Log($variable) and you will see this information inside of the log portion of firebug, when you click on it, it shows the entire variable broken out. Also if you were to install the error handler, when you get a caught exception, you are able to see the entire stack trace of how this exception was invoked.
Highly recommended.
try PHP xdebug module - http://xdebug.org/
u can log the debugging info,
or output as HTML
changes might required to initiate xdebug
You can use some introspective functions like debug_backtrace, but these will only get you so far. To gather any kind of information, you would need to hook into PHP itself, which you would most likely do with an extension written in C. I'd suggest you check out one of the existing debuggers to see how they do it, for example xdebug.

PHP Debugging with Breakpoints - case studies, examples..?

I'd really like to get deeper into my php scripts and use things like breakpoints, as I'm doing with JS with firebug.
I'd like to know more about what techniques people use, and some solid examples of how to debug with breakpoints a php project.
Thing's I'd like to be able to see..
Properties of objects
Class hierarchies.. where objects are coming from, file names etc.. (useful in ZF/Magento)
Variables, types, content..
headers, post data, get data, session data, cookies..
Network / filesystem status..
I know a lot of this can be done with logging and print_r/vardump etc, but its a bit raw.. and I'd like to be able to use a "continue"/"step-over" etc command on code after hitting a breakpoint, like with firebug.
from php.ini:
zend_extension_ts = c:\wamp\bin\php\php5.2.11\ext\php_xdebug-2.1.0-5.2-vc6.dll;
xdebug.remote_enable=On;
xdebug.remote_host="localhost";
xdebug.remote_port=9000;
xdebug.remote_handler="dbgp";
xdebug + remote debugging + one of the supported clients
Use XDebug, it does most of what you require (not network/filesystem), and with it you can debug from eclipse, zend studio, pdt, or even notepad++
I keep debugging again and again when dealing with Magento, and it is super useful in this case, since Magento's function call stack is very deep.
PHP is interpreted and server side scripting language. So, there are only few editors that supports the break point in PHP. And if you are doing the server side scripting then there is no way to debug your script using break points.
However if you are planning to have basic scripting & debugging then you can go with http://www.firephp.org/. Also if you use Zend Studio editor then you will have many options on hand for debugging your script. Zend Studio supports the break point, run & debug options.
I prefer to log the execution of my script in file.
Thanks

Magento Debugging Environment

I looked for ideas on setting up a Magento development environment when we first started using it for our site last year. I didn't find anything that work really well, so I stayed with var_dump'ing using the log files.
Now that Magento has another year under its belt and several hundred more developers I was wondering if anyone has found a better solution for debugging Magento.
We use Eclipse as our development environment. We tried a pre-2.0 release of PDT with the Zend Debugger and didn't have much luck.
How about using FirePHP?
http://ajzele.net/utilize-firebug-and-firephp-to-speed-up-magento-development
It made my life a hell of a lot easier.
I use a combination of var_dump with xDebug and Magento's Mage::Log method. Mage::Log is particularly nice, as it'll do some auto-expanding and pretty printing of objects if you pass them in (I'm not sure if that's the logger, or just Magento's __toString implementation).
If I'm on my local development box I use Console.app to keep an eye on the log file, otherwise it's a simple
tail -f /path/to/log/file
That combined with some custom modules I've built for debugging the config and layout keep me happy. (although I prefer a light weight text editor toolchain vs. the One True IDE tool chain, so your results may vary)
logging $object->getData() rather than the $object itself is normally more useful, and everything built into Magento has it as a method (everything extends Varien_Object)
$object->debug() is often quite helpful too, although it doesn't exist on all objects.
Here are my most commonly logged statements:
Mage::log( $object->debug() )
Mage::log( $object->getData() )
Mage::log( get_class($object) ) # name of class
Mage::log( get_class_methods($object) ) # methods of class
I also use FirePHP but find this extension easier.
Give it a try to Magneto-Debug: https://github.com/madalinoprea/magneto-debug (only for dev environments).
Video doesn't contain features added in the latest version: http://www.youtube.com/watch?v=aqvgrmebcu4
- display layout updates from DB
- display blocks' rendering time
NuSphere is also good debugger for magento here is link
NuSphere

Categories