Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to debug a PHP script that never finishes loading?

Tags:

php

debugging

I have been tasked with setting up a website on various environments for different stages of evaluation (dev/test/staging/etc).

On our staging environment however, it seems there is some difference preventing the PHP script from finishing, so the page is never delivered to the browser.

I'm wondering if there is a way I can output to log some sort of stack trace or backtrace upon cutting the connection, or is there some other method to find out what exactly PHP is doing at any given point in the script's life cycle?

It's a Drupal site, so it involves a lot of code I'm not familiar with, and could take hours to sprinkle die; commands throughout to see where the script is loading to.

I understand I should probably be looking at the differences in environments, however all should have very similar configuration (Ubuntu 11.04) and the staging environment seems entirely happy to serve other PHP sites whilst this particular site is refusing to finish. If anything this staging site has more resources available that other environments which are not having problems.

UPDATE: Sorry all, found the problem in the end. The staging environment was on a VLAN that was not permitted to access itself via public IP, and for whatever reason (still confused about this) it was trying to access itself as part of the page load and never completing the request. Setting a hosts file entry for 127.0.0.1 fixed the issue.

like image 437
DanH Avatar asked Feb 28 '13 10:02

DanH


People also ask

How do you debug a PHP script?

Launch the Debugger Before launching the debugger, make sure that either a breakpoint is set or the Break at first line in PHP scripts option is enabled on the Debug page of the Settings/Preferences dialog Ctrl+Alt+S . on the PhpStorm toolbar. Press Alt+Shift+F9 . Select Run | Debug from the main menu.

Why is PHP debugging difficult?

While it is true that, from a user's perspective, the application gives the impression of a monolithic unity, that effect is maintained by artifacts such as cookies or hidden fields. The presence of such artifacts does not inform the debugger of a transaction-spanning Web session.

How do I debug PhpStorm?

On the PhpStorm toolbar, toggle. to start listening for incoming PHP debug connections, or choose Run | Start Listening for PHP Debug Connections from the main menu. Set a breakpoint in your code. Breakpoints can be set in the PHP context inside PHP, HTML, TWIG, BLADE, and files of other types.

Why is Xdebug not working?

Xdebug cannot connect to PhpStorm This means that Xdebug tries to connect to the host and can't make the connection. To fix the issue, set xdebug. remote_connect_back=0 ( xdebug. discover_client_host=false for Xdebug 3) and make sure that xdebug.


2 Answers

Debugging an issue like this step-by-step using a tool like xDebug is an option, but will probably take a long time -- finding where to put the breakpoints is going to be on about the same level as working out where to put die statements around the code. The debugger option is a better way of doing it, but won't save much in comparison, when you have a problem like this where you have an unknown blocker somewhere in large amounts of unknown code.

But xDebug also has a profiler tool which can show you what functions were called during the program run, how long they took, and highlight where the bottlenecks are. This will probably be a better place to start. Just configure xDebug to generate a profiler trace, and then use kCacheGrind to view the trace in a graphical environment.

If your program is getting stuck in a loop or something specific is taking a long time to complete, this will pinpoint the problem almost straight away; you'll be able to see exactly which function is taking the time, and what the call chain looks like to get to it.

It's quite possible that once you've seen that, you'll be able to find the problem just by looking at the relevant code. But if you can't, you can then use xDebug's step-thru debugger to analyse the function as it runs and see what the variables are set to to see why it's looping.

xDebug can be found here: http://www.xdebug.org/

like image 96
SDC Avatar answered Sep 28 '22 19:09

SDC


Use xDebug.

Its very easy to install and use. it has few options like breakpoints and step by step to track status of PHP script before finishes loading

and you can download xDebug from here http://www.xdebug.org/

step by step tutoril for set up xdebug is availble at sachithsays.blogspot.com/

like image 39
Kamidu Avatar answered Sep 28 '22 19:09

Kamidu