📜 ⬆️ ⬇️

Debugging php functions using phpdbg, as an alternative to Xdebug via IDE

Sometimes you have to work with the server through a bunch of firewalls, with a local IP c repository, etc., and for this reason it is quite difficult to configure XDebug to work through NetBeans IDE (and other IDEs). It is easier to lift a local virtual machine. And if XDebug is needed only to quickly get acquainted with a large new project (with a bunch of legacy code), followed by debugging via error_log, then debugging individual scripts is not always convenient in it.

The interruption point, the extra call stack of the connected scripts, etc. do not work properly As an option, to get acquainted with a new project, you can still configure xhprof and quickly review the function call stack on the charts when calling a particular script. In this case, it is good to catch errors of unnecessary calls to the heap of functions in cycles that do not have static variable initialization inside. In particular, calls to the same setting from the database inside the loop (foreach, for, while, do-while). The difference between phpdbg and the tools described above is that it allows you to specifically debug some function in logic on a rare bug. Phpdbg is roughly a command (console) debugging interface, as in NetBeans (xdebug). If in the IDE we click break points (breakpoints) with mouse clicks, then in Phpdbg we need to do this in the form of commands.

Let me give you an example of a simple code.

<?php function EugeneKurilov() { $i = 10; for($j=0;$j<$i;$j++) { //echo $j; } } EugeneKurilov(); ?> 

For the above PHP code, in order to start debugging the EugeneKurilov () function, you need to run the command:
')
 prompt> break EugeneKurilov [Breakpoint #0 added at EugeneKurilov] 

And then,

 prompt> run [Breakpoint #0 in EugeneKurilov() at /root/dbg.php:3, hits: 1] >00003: function EugeneKurilov() { 00004: $i = 10; 00005: for($j=0;$j<$i;$j++) { 


Pressing the s (step) command, we go through the function body, in order to see how the $ j variable changes, we need to execute the watch $ j command:

 [Breakpoint #0 in EugeneKurilov() at /root/dbg.php:3, hits: 1] >00003: function EugeneKurilov() { 00004: $i = 10; 00005: for($j=0;$j<$i;$j++) { prompt> watch $j [Set watchpoint on $j] 
And further, press s (step) for passage.

 [Breaking on watchpoint $j] Old value: New value: 0 >00005: for($j=0;$j<$i;$j++) { 00006: //echo $j; 00007: } prompt> Old value: 0 New value: 1 >00005: for($j=0;$j<$i;$j++) { 00006: //echo $j; 00007: } Old value: 9 New value: 10 >00005: for($j=0;$j<$i;$j++) { 00006: //echo $j; 00007: } prompt> s [L5 0x7f9d0c088100 IS_SMALLER $j $i ~3 /root/dbg.php] [L5 0x7f9d0c088120 EXT_STMT /root/dbg.php] [L5 0x7f9d0c088140 JMPNZ ~3 J6 /root/dbg.php] [L9 0x7f9d0c088160 EXT_STMT /root/dbg.php] >00009: } prompt> s [L11 0x7f9d0c0735e0 RETURN 1 /root/dbg.php] [Script ended normally] 

That is quite simple in this way to see live how the variable value changes and you don’t need to add code like error_log (when viewed in the log) or echo in the browser.

Phpdbg is quite simple (to study the functional you need to enter help) and for this reason it makes no sense to describe in detail all the points. Starting with PHP 5.6, it is enabled by default. My goal for this publication was to show an alternative code debugging option. As my practice shows, passing through various debug-tools on a new project allows you to quickly understand its architecture for the case when there is no documentation, and the project was developed simultaneously by a large number of employees.

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


All Articles