views:

56

answers:

3

I have a php script that is rather hairy and I'm trying to troubleshoot it. No errors are happening, but I'm having trouble seeing what execution path it took to create the output I've gotten. Is there a way I can see at what line the script stopped execution?


Folks, sorry I didn't make this clearer. No errors are happening. No exceptions are being raised. From a computer point of view, nothing 'bad' is happening. But the output is not what I'm expected. I'm trying to track down where exactly the script is exiting normally, but it's a challenge. My life would be much easier if it said something like "Script finished parsing at line 422".

A: 

I'm not sure I'm completely understanding the question. An exception is being thrown and the program is terminating but there is no output?

Try turning error reporting on to the most sensitive level: error_reporting(E_ALL)

Brad
Sorry I wasn't clearer. No exception is being thrown. No errors are being raised. There is output, unexpected output, and I want to know where the script terminated normally when generating that output.
A: 

If an error occurs, there is an error message (except for a small number of parse errors). As Brad already suggests, turn up the volume on your error reporting first.

Otherwise, the script will run until its end, or a exit or die() command. I don't think it's possible to find out where a script was exited, but then, it should never be really necessary to.

When debugging hairy errors, either use a debugger and/or debug_backtrace(). Debug_backtrace() can give you the exact call stack. It's most powerful in combination with a custom_error_handler().

Pekka
+1 New Gravitar, same quality answers. (And you keep beating me to it.)
middaparka
Thanks middaparka :) Check my profile for the source of the new Gravatar, there's one for everyone.
Pekka
Sorry! There are no errors. It's just not working the way I think it should.
@unknown, then I think you'll have to walk through every `exit` and `die` in your script, and set up some sort of logging mechanism to see which turn your script takes. Sorry!
Pekka
Or of course, you can create a call stack using `debug_backtrace` at selected positions, that will give you an idea which turns your script takes. But I have been doing this for almost ten years now, and I've never needed to do that in order to find a bug. The usual way of going about this is using a debugger, or making test outputs `die("variable x = $variablex");` or log messages until you find what's wrong.
Pekka
Yeah, but there are a lot of candidate positions to place debut_backtrace(). Actually, rather than that, I should just put `echo __LINE__`s and track it down that way. Also there are no exits or dies in the script.
+1  A: 

Using Xdebug and running a function trace should give you the info you're looking for.

Brock Batsell
This is a mess of elseif statements. Will a function trace help me there?
It's hard to know without actually seeing the code, but it probably will. And it will definitely give you the line # of the last function on the stack.
Brock Batsell
Try it and find out if it helps
mpez0
I mean, there's not all that many function calls, it's just code block after code block of `} elseif (...) { ... } elseif (...) { ... ` -- so knowing what function was called won't help me much.
You should be able to walk through the code step-by-step using XDebug, and see all the variables' states.
Pekka