I've been using xdebug to debug and understand code in php projects for a while now, and have sometimes come into situations where it's been unclear what's going on inside of PHP.
Is it possible to set xdebug or gdb up so that i can trace into actual php builtin functions?
If you are using a macosx, solaris or recent freebsd system you can throw a little dtrace at it. It can come in handy for those all too numerous "WTF is PHP doing?" moments.
I doubt it, xdebug is intended for tracing your PHP code, not the internals. The internals are assumed to be bug-free (which obviously they aren't sometimes, but that's beyond the scope of xdebug).
You can always look at the PHP source if you want to know what the built-in functions do, but that's sometimes pretty hairy. The PHP manual docs have always served me well enough when I want to know what they'll do.
You can use gdb to trace in to the C-level code, provided you have php compiled with debug symbols. Have a look here for a start:
http://derickrethans.nl/phps_segmentation_faults_gdbfu.php
One way to test the output of the Zend engine, to peek inside at the opcodes, you can use Derick Rethan's VLD (Vulcan Logic Dissasembler), which also appears to be on PECL. Note: only works on *nix systems (see site for requirements).
Some examples of debugging these opcodes can be found on Sara Golemon's blog, in articles such as Understanding Opcodes and How long is a piece of string?.
There is also great Google Chrome extension PHP Console with php library that allows to:
See errors & exception in Chrome JavaScript console & in notification popups.
Dump any type variable.
Execute PHP code remotely.
Protect access by password.
Group console logs by request.
Jump to error file:line in your text editor.
Copy error/debug data to clipboard (for testers).
Recommend to everyone!
Related
Has anyone configured VS Code to show syntax/semantic errors in PHP development? Coming from full Visual Studio/C# world, I really miss this thing a lot. Currently I actually have to call my PHP function using a client, only to get a HTTP 500 error and then check server logs to see what went wrong. This is a huge pain. Going through all of this only to find for example that I didn't import a require namespace, is so frustrating.
I understand that PHP by design cannot detect all kinds of errors like C# at compile time (so to speak), but detecting functions that do not exist or not passing required parameters should be easy to catch.
Is there a plug-in or setting in VS Code that could handle syntax/semantic error in PHP code? I'm using Laravel on the server-side, if that matters.
Looks like I found something. I uninstalled PHP Intellisense extension this morning and installed PHP Intelephense and all seems to be far better. Not only that the syntax and semantic errors are highlighted, it automatically injects required namespaces too. And if that were not enough, Go To Definition (F12) command has started working correctly all of a suden. :)
Must-have extension for any PHP work in VSCode I'd say.
I am starting a website, thus I'm learning how to program in html/css/php. For other languages I have learnt so far, there has always been an IDE or interactive console that I can type in codes directly to see how they works, access variable types etc., which makes learning and debugging a breeze. (E.g. bash, python, matlab, perl, tcl...)
Can I do the same for html and php? In the spirit stackoverflow's questions style, lemme try to make the questions a little more specific.
I know I can use php interactively on a terminal.
But that is insufficient if I want to see how it works in a html script.
Is there a browser/IDE with a console where I can input commands interactively and see the results?
For PHP:
I'd suggest to use Xdebug.
Read through this article: Debugging techniques for PHP programmers.
Note about debugging production.
For your markup:
Read Firebug-like debugger for Google Chrome.
Also, you might try Web Developer extension.
I find enabling errors is handy for general debugging. With it enabled you can see where the script failed and hone in on the error quite easily. For more advanced stuff Xdebug has it's place.
Combined with a local WAMP installation you have a great start point. Save the php script, execute it and combined with "Firebug" (FF extension) and you have a decent "beginners" tool set for debugging.
you can make your browser your ide. i do, check it out:
handy article showing how to output php to browser console. http://sarfraznawaz.wordpress.com/2012/01/05/outputting-php-to-browser-console/
as for html, download web developer extension for your browser, and validate your markup to test/debug.
You might be interested in something like Cloud9 IDE.
Check them out at: www.cloud9ide.com
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
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?
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.
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 :
Building PHP for extension development
Generating a gdb backtrace
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.
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.
I'm looking for clean way to break my current habit of using print commands in PHP when I want to see what's happening.
I am aware of options such as Zend Debugger but I use Coda for development and I'm not interested in mixing other software or having to do server commands. I just need a console that can be added to my codebase and then turned on/off.
Does anything like this exist? Furthermore, what do you use and why?
EDIT: There was a lot of stuff out there but I needed something even simpler so I ended up coding it myself. It didn't take long (nor is it very pretty) but I've put it up on my server for anyone else interested.
There is very good extension for Google Chrome - PHP Console.
You can use Xdebug in combination with any one of the many options available to view its debugging info.
There's FirePHP which will write your PHP log messages to the Firebug console.
I am aware of options such as Zend Debugger but I use Coda for development and I'm not interested in mixing other software or having to do server commands. I just need a console that can be added to my codebase and then turned on/off.
Not entirely sure if I understand you there, but couldn't you log to a file and then have a console window running with tail -f /path/to/log-file.txt? That works pretty well for me. Of course, you do need a console connection to the server for this.
Using Coda? That means you are using a mac. You probably want to look at XDebug and http://www.bluestatic.org/software/macgdbp/
There is another extension for Chrome available called ChromePHP. It's a little simpler than some of the other solutions:
https://chrome.google.com/webstore/detail/noaneddfkdjfnfdakjjmocngnfkfehhd
Getting started guide available at:
http://www.chromephp.com
During my development career i have not stumbled upon a software that displays the output you describe in your question. It would be a wonderful program or feature i believe but sadly i have not found any.
Like Andrew and nickf have answered, there are some good tools out there and until someone sits down and writes this, it looks like we are out in the cold.
I was looking for similar solution which wasn't heavy and even better I didn't need to include any classes, so I created simple function that outputs php to browser console, you can check it here:
Outputting PHP To Browser Console