tags:

views:

255

answers:

9

Say you have a large PHP project and suddenly, when attempting to run it, you just end up with a blank page. The script terminates and you want to find excatly where that is with as little effort as possible.

Are there a tool/program/command/IDE that can, on PHP script termination, tell you the location of a script exit?

note: I can't mark my own post as "accepted answer" so look at the bottom to see my solution. If you come up with a better solution I will mark your post as the answer.

A: 

Textmate (and its little Windows brother E) has find-in-project, and I'm absolutely sure other editors/IDEs do too.

eyelidlessness
A: 
grep -n die filename
Paul Nathan
Good answer, but I'm looking for a general solution (for any type of exit), and not just finding die(). Also assume that the project have numerous die and exit and looking trough them all would be an extremly tedious task.
CooPs
A: 

Don't forget to grep for "exit" too.

Edward Z. Yang
+2  A: 

Add this to the top of the file:

function shutdown()
{
    print_r(debug_backtrace());
}


register_shutdown_function('shutdown');

See register_ shutdown_function()

Greg
Doesn't work. The shutdown function occurs after the stack has unwinded. There is no stack information to dump.
troelskn
Yeah, this would have been exactly what I was looking for, if PHP didn't clear the stack trace before calling it...
CooPs
A: 

You can use an interactive debugger to step through the code until you reach the exit point. Other than that, I think you're down to grep'ing the code for exit|die.

troelskn
A: 

xdebug has a nice trace feature that'll allow you to see all the entire trace of your php app execution and it should give you give clue as to where your exit is.

but for the quick and dirty solution a grep/find as mentioned above will do rightly.

flungabunga
+2  A: 

With some inspiration from the nonworking but still right-direction answer from RoBorg, I used the following code in the beginning:

function shutdown() {
 global $dbg_stack_a;
 print_r($dbg_stack_a);
}
register_shutdown_function('shutdown');

And then I made a global conditional breakpoint (global = breakpoint is evaluated on each row), exploiting the fact that it can run code trough eval(), with the following "condition":

eval('
global $dbg_stack_a, $dbg_stack_b, $dbg_stack_c;
$dbg_stack_a = $dbg_stack_b;
$dbg_stack_b = $dbg_stack_c;
$dbg_stack_c = debug_backtrace();
return false;
')

Probably not fast but does the trick! Using this I was able to determine the exact file and line location that raised die(). (This example works in NuSphere.)

CooPs
+1  A: 

Also check the error___logs for "memory_limit" errors in the Apache error_log.

Memory Limit >= 10M Warning: (Set this to 10M or larger in your php.ini file)

In my experience, scripts suddenly end without warning or notice when this happens.

Wimmer
+1  A: 

Make sure that errors are displayed in your development environment (not production).

acrosman