Debugging is a process that takes place after the testing. One needs to remember that tests are carried out not only by application developers, but also customers and users, each time they use the program. The very process of debugging is based on locating the error and its removal. So, we have the result, but there is no cause.

The process of successful debugging can be represented by the following diagram.

In case of failure, we can adopt additional hypotheses, perform tests to confirm these hypotheses and gradually try to locate the root cause of the error.

Identifying the cause

Why is it so difficult?

We often find the cause of an error through intuition and often luck. Skills and experience play an important role here, which results in lower concern about the results. The psychological factor is probably the most important in the context of reluctance to correct errors. Worse still, there’s no scientific method of effective debugging, which further intensifies the frustration.

What’s interesting, there are experiments regarding the effectiveness of debugging. They point to large discrepancies even among equally educated and experienced programmers.

Since there’s no one hundred percent effective technique to remove a bug, how to maximize your chances?

Below I present the 5 most effective tools to help locate and remove the most difficult bugs.

1. Xdebug

The most advanced debugging tool available for PHP environment. It allows working in steps, as well as using so-called breakpoints. It uses the DBGp protocol. It also offers the possibility of profiling and the generation of stack trace.

Xdebug allows to collect such information as memory usage, peak memory usage, runtime, values of transferred variables (applications such as Magento return several GB of data in this mode!), values returned by functions, etc. Furthermore, Xdebug works with all major IDE environments.

2. Xhprof

An excellent tool for profiling created by Facebook, supporting correcting bugs such as segfault. It provides detailed data on the amount of function calls, runtime, memory usage, peak memory usage and CPU utilization.

In addition, “inclusive” values include aggregated data for the given function and all dependent functions called by the function. “Exclusive” values provide data only for the function (without taking into account dependent function calls). Other interesting features include generating a dependency graph, which marks in red the slowest functions and the possibility to compare two different profiling reports.

Xhgui and MongoDB database can be used to acquire and present the results.

3. Monolog

If we’re unable to reproduce a bug, it’s necessary to gather additional information. In order to do that, we can use the so-called loggers, and Monolog is one of the best of them. It allows to compare recorded logs (DEBUG, INFO, NOTICE, WARNING, ERROR, CRITICAL, ALERT, EMERGENCY), sending alerts and emails (HipChat, Flowdock, Slack, Fleep, etc.), logging data into the database (Redis, MongoDB, CouchDB, ElasticSearch, DynamoDb), data formatting (Line, Html, Json, Elastica, Flowdock, MongoDB), and many other interesting features.

4. PHP Debug Bar

PHP Debug Bar is a tool used for a quick view of the application process. It collects and provides data such as: memory usage, request time, recorded messages, data in superglobal tables ($ _GET, $ _POST, $ _REQUEST, $ _SERVER), timeline, reported exceptions and queries to the database. It also contains support for Laravel, Atomik, XOOPS, and Zend Framework 2 and collectors for PDO, CacheCache, Doctrine, Monolog, Propel, Slim, Swift Mailer and Twig. Check Magento Developer Toolbar.

5. Strace

A tool that allows for testing applications at a very low level (the program interacts with the Linux kernel). It registers system calls as well as signals and state changes in the process. It also enables counting and timing of individual calls. Strace is useful in the event of a sudden breakdown of the program, in which other tools aren’t able to collect data.

Requires basic knowledge of system calls.

The script can be run directly under the control of strace or use strace to investigate an already running process.

If you’re really interested in this subject, you should definitely check: Software Engineering: A Practitioner’s Approach by Roger S. Pressman.

Want to know how to deal with PHP bugs in your e-business? Let’s talk!

Tomasz Duda

PHP Developer at Divante eCommerce Software House

Share your comment