Category Archives: virtualization

image

Debugging PHP code to fix a WordPress problem

How do you debug a PHP application? Traditionally developers resort to outputting variable values to HTML, or peering through logs, but why not set breakpoints and step through code just as you would in C# or Java? Maybe because it can take some effort to set this up, as I was reminded today.

I was motivated by an annoying WordPress problem which I’ve blogged about before. For historical reasons, I have a lot of subscribers to an old RSS url which delivers the feed in the now deprecated RSS 0.92 format. I prefer to have a full text feed, and this used to work fine with WordPress, which placed the entire blog post in the element of the feed.

At some point this stopped working, and subscribers got a summary only. In fact, the feed broke completely for a while, after I switched to pretty permalinks; but even after fixing that, I still had the problem with summary items. I tried upping the length of the description, but it was delivered without any HTML formatting so that did not work.

Next I tried the WordPress support forums. There are lots of good folk there; but if you review the posts its clear that many queries go unanswered. That’s nobody’s fault; it is a community, and for whatever reason there seem to be more people seeking help than there are experts with the time to give free advice.

So how about debugging the PHP code and working out what was happening? It seemed a good opportunity to try the latest Eclipse Ganymede, released a couple of weeks ago, along with the PHP Development Tools (PDT). I also figured it would be easier to set this up on Linux, to match what I use on the web server. I used the same Ubuntu on VirtualBox setup that worked well for trying out SproutCore. It worked…

Debugging PHP with Eclipse Ganymede

…but I can’t pretend it was wholly straightforward. Here’s how it went. I installed the latest Ubuntu distro versions of Apache, MySql and PHP – easy. Ubuntu’s Eclipse is not the latest, so I downloaded it from the Eclipse site and used some tips to set it up tidily. Note: make sure Sun Java is installed; I set it as the default JVM. Adding the PHP development tools was more fiddly. I’d half expected this to be part of a standard Eclipse download by now, but it is not, and if you try to install it into Ganymede using the standard update site is does not work because of dependency issues (a big problem with Eclipse). You have to download a 2.0.0 build from here instead.

I’d decided to use the Zend debugger – that’s a separate Eclipse update too, as explained here. Note that even after updating Eclipse, you still have to install the separate Zend debugger server from here, if you want to debug real web applications. I had a few problems getting this working, mainly because of the zend_debugger.allow_hosts directive which you have to edit in php.ini, and which is not brilliantly documented.

I replicated my blog on the Ubuntu virtual box – easy. But how do you get your Eclipse PHP project pointing at this existing code? The method I settled on after a couple of experiments was to start a new PHP project, uncheck the Use default option for project contents, and select the blog directory in /var/www. You then get a scary dialog which observes that files already exist. You can either create your project as a subdirectory, in which case you cannot debug with the existing files, or else pass the scary warning:

Create project in /var/www/blog
(Deleting the project will delete the entire /var/www/blog folder)

I mis-read this at first, thinking it would delete all the files when creating the project. That’s not what it says. Everything was a backup anyway, so I took the plunge; it worked fine. In fact, if you look closely at the screenshot above (click for a full size image) you can see that it is nicely done. You can see the call stack at top left, current variable values, output as it is being generated, and the usual options to step into or over the code.

That said, I did have some problems with Step Into. Just when it was going to be most useful, it bombed out with a message that said Error. If you looked at the detail, that also just said Error. The only fix I found was to set breakpoints in the actual file I needed to debug.

Still, it worked. I found that by adding a single argument to a line in feed-rss.php I could get my full text feed back. I’ve duly reported this in the WordPress support forums.

A couple of observations.

First, I don’t much like the WordPress code. Sorry, because the product is marvelous, but the code seems like a typical PHP tangle. Using pretty permalinks, which I regret, makes it worse.

Second, are there not plenty of developers who use both Java and PHP and would like it to be a tiny bit easier to set up in Eclipse? I’m being a little unfair, since Ganymede is just out and I guess the PDT will integrate better with it soon. Even so, Eclipse is still not quite the smooth plug-in dream that I once hoped it would become.

Note that if you don’t mind paying, you can have Zend Studio which I should think makes life easier. Or perhaps Delphi for PHP.

Wine is 1.0

Congratulations to the Wine community which has released Wine 1.0:

The Wine team is proud to announce that Wine 1.0 is now available. This is the first stable release of Wine after 15 years of development and beta testing.

Wine is a compatibility layer that enables Windows applications to run on Linux, Mac OS X, and other Unix-like systems.

Although in beta, Wine has been used in numerous commercial applications over the years. Two I can think of: WordPerfect 9 from Corel, and the Kylix IDE from Borland. More accurately, Kylix used Winelib, which uses the Wine API at compile-time rather than at runtime.

Another notable example is Google’s Picasa for Linux.

Despite this landmark, Wine seems less significant now than it did a few years back. For one thing, it is easier to write a cross-platform application. For another, web applications have grown in importance, reducing the number of native applications we need to run. Finally, virtualization is now a better, more compatible route to running Windows apps on Unix in many scenarios.

Technorati tags: , , , ,

VirtualBox: get today’s update

If you are trying VirtualBox (which I recommend) make sure you get today’s update, version 1.6.2.

The story: I tested VirtualBox 1.5.6 which works great. I thought I should try the latest version, the first with Sun xVM branding, so downloaded it yesterday. Then I wasted an hour trying to get the networking working. With VirtualBox, the default is NAT networking with your host PC, which enables the Internet but means your virtual machine is detached from your local network. If you want it on your local network, you have to create a Virtual Host Interface and then bridge it with your real network connection. Not difficult; but version 1.6.0 broke something so that the Virtual Host Interface could not transmit data. I tinkered with various settings and drivers until I discovered this changelog for 1.6.2:

  • Networking: fixed a host interface networking regression introduced in 1.6.0

Right. Installed the update and everything is fine. The good news: VirtualBox is just as fast as before. In fact, it scored slightly higher on PassMark: 631.

I also updated Virtual PC 2007 to SP1. In my test this was fractionally slower than before, with a score of 387. Neither score is significantly different though.

VirtualBox is amazing, 50% faster than Virtual PC on my PC

It was only when Sun acquired it that I got round to trying VirtualBox, a free open source virtualization utility. I was immediately impressed, not least by its performance. It just felt snappy, something I’ve never been able to say about Microsoft’s Virtual PC, useful though it is. When I needed to set up a new virtual machine in order to do some Delphi 7 development, I decided to use VirtualBox rather than Virtual PC. Again, I’ve been very impressed. I thought it would be interesting to see if my perception of good performance would be verified by a test suite, so I dug out the PassMark suite and ran a few tests.

Note that both Virtual PC and VirtualBox can use Intel’s Virtualization Technology CPU extensions (AMD have similar extensions, but I’m running on an Intel Core 2 Quad). I ran PassMark on XP Pro with SP3, under both Virtual PC and VirtualBox, with hardware virtualization first enabled, and then disabled. I ran it full screen, with as little as possible running on the underlying OS (Vista 32-bit). PC additions were installed. Both virtual machines were given 512MB RAM. Here are the surprising (to me) results:

  • Virtual PC 2007 with hardware virtualization: 399.6
  • Virtual PC 2007 without hardware virtualization: 345.9
  • VirtualBox 1.5.6 with hardware virtualization:  542.9
  • VirtualBox 1.5.6 without hardware virtualization: 616.4

So on my machine (your results may vary) VirtualBox is faster without hardware virtualization, and more than 50% faster than the best result from Virtual PC.

I drilled into the results a little. On the CPU tests there was not a big difference; in some cases Virtual PC was ahead. On the Graphics 2D tests though, VirtualBox was dramatically faster – more than twice as fast on the GUI test, for example. It was also dramatically faster on disk I/O. For example:

Disk- Sequential Read: VirtualBox 143.4 MB per second vs Virtual PC 90.8 MB per second

Disk – Sequential Write: VirtualBox 97.4 MB per second vs Virtual PC 6.8 MB per second

I’m not surprised that this makes a big difference to perceived performance, since Windows spends much of its time reading and writing temporary files. This may also be why VirtualBox seems to start up and shut down much more quickly.

I don’t claim that my informal tests prove that VirtualBox is a faster performer in every case. Maybe there is some setting I could change that would improve Virtual PC’s speed; or maybe Virtual PC likes some hardware better than others. Still, it is a real-world experience, and enough to make me suggest that you give VirtualBox a try if you have yet to do so. By the way, both these products are free.

Finally, let me note that Vista running directly on the hardware scores a PassMark of around 1100 on this machine. Even VirtualBox is a lot slower than the real thing, as it were.

Update: If you try VirtualBox, make sure you get at least version 1.6.2 (6th June 2008), as 1.6.0 has broken networking on Windows.