📜 ⬆️ ⬇️

Debugging PHP applications with xdebug

Welcome to part 4 of the xdebug story. Today we will try to understand the debugging of PHP code using xdebug. In this article, we assume that you have installed xdebug on your system a long time ago; if not, the first article in the series will tell you how to do this.


Debugging software is the most hated job developer share. Most use the echo (print_r, var_dump) and exit (die) bundles for debugging, moving from one line to another. However, if the error appears again in this file, debug commands need to be re-written. Of course, debugging can be wrapped inside an if construct, which will work only when a constant is defined, for example DEBUG. But each such if will slow down performance slightly, and it will look very awful in code.
As we already learned in the second part of our narration, the presence of xdebug allows us to create trace logs, a good enough way out for this situation, you will not need to change the program. However, the trace log, especially when created for a part of the program, provides us with a lot of information that has nothing to do with debugging, so using the debugger is the best solution. The debugger will allow you to stop the program for a while, check or modify the current value of the variable, and then continue with the program. By launching the program step by step, you can take a close look at how your code is executed, which will help you find the location of errors.
Before the appearance of the var_dump function, debugging applications in PHP was problematic if you did not have the money to buy a commercial IDE that supported debugging. With the release of xdebug, the application debug problem was theoretically solved. I wrote theoretically because there is no good and free client for debugging for both Windows and Unix.
However, this problem was solved with the release of the Eclipse PDT. Eclipse PDT is a free PHP IDE supporting xdebug. Therefore, let's take a look at the installation of the Eclipse PDT in order to start debugging.

Installing Eclipse PDT
')
Eclipse PDT (PDT is an abbreviation of PHP Development Tools) is written in Java, and therefore will work on most platforms where the Java Runtime Environment is installed. If you don’t have one, you can download it from www.sun.com .
You can download the finished version of the Eclipse PDT from www.eclipse.org/pdt . You can choose the appropriate version for your operating system.
When this article was written, the Eclipse PDT version was R20070917. Select the latest version, click download and go somewhere to eat, since the size of the Eclipse PDT is about 100 MB. After downloading, unzip the files and you can start working.

How does debugging work

Before we dive into configuring xdebug and Eclipse PDT, let's take a look at how debugging in PHP works with xdebug. This will help you better understand the configuration options that we will discuss later.
When debugging is enabled in php.ini, xdebug begins to monitor program execution, which means that xdebug can stop the program at any time, and then start it from a breakpoint. When program execution is stopped, xdebug can get information about the current state of the program, for example, read the current values ​​of variables. Also xdebug provides the ability to change the values ​​of variables during the execution of the script.
The xdebug extension is a server waiting for client connections on a specific port specified in the configuration. There are two protocols that can be used for communication between the xdebug client and the xdebug server (GDB and DBGp). GDB is an old protocol that has been replaced by DBGp. By sending commands to the xdebug server, the xdebug client remotely controls the execution, informs PHP that it has stopped running, running the next line of code, or continuing the program. The client is usually embedded in the editor or IDE (in our case in the Eclipse PDT), so you will not have to deal with the communication protocol directly.
A web server with xdebug can run on a different operating system than the xdebug client. This is why xdebug is called a remote debugger. To simplify, we will configure the server and client on the same computer.
There are two debug session start modes. These modes are controlled in php.ini by setting xdebug.remote_mode. The default value is req, the xdebug client will connect to the server whenever script execution starts. If you want xdebug to connect to the server only when a breakpoint is set or when an error occurs in the script, you can set xdebug.remote_mode to jit. I recommend leaving the default value, it will save you from modifying php.ini.
To start debugging, you must send the XDEBUG_SESSION_START parameter to the script as part of a GET, POST, or COOKIE. The value of this parameter is the name of the debugging session, which must be unique, by the name of the xdebug session it can distinguish different sessions running in parallel. To end a debugging session, you need to send the XDEBUG_SESSION_STOP command to the script.
Instead of manually starting and ending debugging sessions, you can install a special firefox plugin that will help you easily start and end sessions with one click of the mouse.
Using Eclipse PDT, you do not have to worry about the browser plug-in, since the IDE takes care of sending the necessary parameters.

Xdebug setup

Now let's start debugging setup. Add the following settings in php.ini:
xdebug.remote_enable = On
xdebug.remote_host = "localhost"
xdebug.remote_port = 9000
xdebug.remote_handler = "dbgp"

Check that these lines are added after the line zend_extension_ts, which loads xdebug. The first line turns on debug mode. The second line specifies that the debugging client is running on the local computer, in case the client and the server on different computers need to enter the server name or IP address.
The third line indicates the port on which the debugging client will wait for a response from the server. The default value is 9000. Eclipse is set to its default value. If you are going to change this value, do not forget to change it in the Eclipse and php.ini settings.
So, make sure that the firewall will not be an obstacle to debugging. When you start Eclipse, you can see messages that Java is trying to start the server, bother to the port, access the network or perform some terrible operation. Of course, this is not dangerous, xdebug tries to listen to port 9000. If you have any problems, make sure that something does not block the 9000 port between the client and the server.
In the last line, we tell which protocol the client will use. Eclipse PDT uses DBGp.

Configure Eclipse PDT

Create a new PHP project in Eclipse PDT. Let it be called debug_test. Create a debug.php file in the project, add some code and save the file.
Now let's set up Eclipse for debugging. First, we will configure Eclipse to run projects in an external browser in place of an internal one. When this is set up, all debugging sessions will be launched in an external browser. Using an external browser is optional, but I prefer to work in Firefox instead of the internal Eclipse browser. Select Window in the menu, then Preferences (see the screenshot below). Open the General subtree, and select the Web Browser. Now tick Use external browser and click Apply.


Eclipse PDT supports both Zend debugger and xdebug. Zend debugger is selected by default. To change, open the PHP subtree, then the Debug subtree. Then change the PHP debugger to Xdebug and click Apply.
Now, select Run from the menu and select Open Debug Dialog. Then, double click on the PHP Web Page to create a new debugging configuration.
You will see three tabs: Server, Advanced and Common. Select Xdebug as Server Debugger. In the File / Project field, you must select the path to the script that you want to debug. The path must be relative to the current workspace. On my system, this is /debug_test/debug.php. Click Browse and select debug.php in the debug_test directory.
Eclipse needs to know the URL that matches the source script and the path where you call it. This is required to highlight the currently executed line in the source code. The URL text box shows the URL corresponding to the name of the script. By default, the URL field is inactive because the Auto Generate checkbox is active. If the specified URL does not match the URL you entered in File / Project, uncheck Auto Generate and enter the correct URL in the URL text box. If this script requires GET parameters, you can enter them in this field.
Do not forget to click Apply to save the changes. The following screenshot shows how the configuration looks on my system:



Select the Advanced tab and check if Open in Browser and Debug All Pages are selected. Now you can close the settings window and start debugging.

PHP script debugging

To start debugging a PHP script, select Run-> Debug (or press F11). Eclipse will ask you where you want to see debugging information.
The following screenshot shows the Eclipse debug window of my debug.php:



Eclipse opens the browser. You cannot see anything, because by default Eclipse stops the execution of the script on the first line, as if a breakpoint was set on this line. If you want to disable this behavior, uncheck the Break at First Line checkbox in the Breakpoint section of the debugging dialog in the settings window.
As shown in the screenshot, you see the source code of the file being debugged, the current line of the program being executed is marked with an arrow. In the upper right area, you can select multiple tabs. The Variables tab shows the values ​​of all variables in a given scope. Superglobals are valid in all scopes, so they will always be displayed. The Breakpoints tab allows you to see and edit all breakpoints in your script. Eclipse will remember all the breakpoints from your code whenever you close and restart Eclipse.
You can continue program execution to the next breakpoint, execute one line of code, enter the next function, or exit the function by clicking the corresponding Debug button in the upper left area. A step-by-step walkway is very useful when you are trying to localize the location of an error in your code. You can see how the values ​​of variables change at each step.

Change variables at run time

You can also change variable values ​​at run time. To change a variable, click on the current value, change it and press Enter.

Breakpoints

The breakpoint pauses the execution of the program and allows you to examine in detail the state of the variables, then continue the execution of the program. Program execution also stops when an exception is thrown in the program. To set a breakpoint, right-click on the line, then select Toggle Breakpoints in the context menu. You can delete the point in the same way or delete it in the Breakpoints tab.
You can also add breakpoints by conditional breakpoint. Such breakpoints will suspend program execution only when the condition is met. This can be very useful when a certain piece of code is executed many times with different input parameters. To add a similar breakpoint, click the right mouse button on the image of the breakpoint, select Breakpoint Properties.
Check the Enable Set Condition flag and enter a condition in the text field. in my debug.php, the test () function is called on line 11, and the breakpoint is set on this line. If we add the condition $ a! = '' Xdebug will stop the execution of the program in this line only when the local variable a is not empty.
To end debugging, click the Terminate button on the Remote Launch panel. If Eclipse runs the script in an external browser, simply close its window.

Conclusion

Remote debugging is an interactive and unobtrusive way to find errors in your programs. Instead of inserting var_dump () calls into the code or analyzing the trace log for tracking variable values, debugging provides you with a “under the microscope” view of each section of your program.
The next article will be devoted to creating comprehensive statistics using xdebug.

Source: https://habr.com/ru/post/31505/


All Articles