7

I cannot figure out where an application is exiting. I'd rather not bother with a debugger, and adding declare(ticks=1); to each file would be a pain (I'm not in the mood for dealing with sed). A similar question has been asked, but not with these constraints.

How can I figure out where the code is exiting?

Clarification:

While this question is similar to Fastest way to determine where PHP script exits, I'd like to find a solution that works without a debugger. I know how to do this with a debugger, but I don't always have access to such tools.

Community
  • 1
  • 1
Zenexer
  • 18,788
  • 9
  • 71
  • 77
  • possible duplicate of [Fastest way to determine where PHP script exits](http://stackoverflow.com/questions/216963/fastest-way-to-determine-where-php-script-exits) – jrd1 Aug 10 '13 at 06:47
  • 1
    Thanks @jrd1; while that question certainly is relevant, I'm looking for a different sort of solution. I'll add clarification. – Zenexer Aug 10 '13 at 07:13
  • 1
    Out of curiosity, why don't you want to use a debugger? – jrd1 Aug 10 '13 at 07:14
  • @jrd1 I don't always have root access to my clients' servers. Sometimes they use shared hosting for quick development. I used Xdebug this time, but in the future, I'd like to have a code-based solution. – Zenexer Aug 10 '13 at 07:16
  • Ah! I see. Makes sense. I'll try to find a better solution, too. And my sincere apologies. – jrd1 Aug 10 '13 at 07:18
  • @jrd1 No problem. I greatly appreciate your help. Sorry for not making that subtle different clearer. – Zenexer Aug 10 '13 at 07:20
  • 1
    My advice to you is to only ever have one exit point, that solves your problem instantly. Alternatively, you can use `exit($code)` to specify a code the program returns, and check that. – Madara's Ghost Aug 10 '13 at 07:32
  • @MadaraUchiha That would be ideal, but I'm debugging applications that I haven't programmed. If had I programmed these applications, they wouldn't be exiting without reason when problems occur. – Zenexer Aug 10 '13 at 18:15
  • @Zenexer: In which case, a debugger is probably your only option. – Madara's Ghost Aug 10 '13 at 20:09

2 Answers2

1

You don't need to add declare(ticks) to all your files. one entry point would be enough:

<?php
function my_tick()
{
    echo 'tick';
}

register_tick_function('my_tick');
declare (ticks=1)
{
    include("lib.php");
    echo "1";
    test();
}

and lib.php:

<?php

echo "2";

function test(){

  echo "3";

}

and as you are looking for a code-based solution i assume your sources do provide single entry point.

fsw
  • 3,595
  • 3
  • 20
  • 34
  • This sounds plausible. It's slightly different from what I was attempting: I was including the `declare` statement (early), which didn't work. I'll give it a try. – Zenexer Aug 10 '13 at 20:20
1

I usually instrument my code using a variable to log events then decide what to do at the exit point by registering a shutdown function:

class flightRecoder {
   var $data;
   var $err;
   function __constructor() {
     $this->data=array();
   }
   function error($errno, $errstr, $errfile, $errline)
   {
      if ($this->err<$errno) {
         $this->err=$errno;
      }
      $this->note("ERROR! $errno $errstr", $errfile, $errline);
   }
   function note($note, $infile, $atline) {
      $this->data[]="$note in $infile at $atline";
   }
   function finish() {
       if ($this->errno || rand(1,20)==19) {
           ....
       }
   }
}
$log=new flightRecorder();
register_shutdown_function(array($log, 'finish'));
set_error_handler(array($log, 'error'));

In your case it would simply be a matter of ensuring that error_logging was enabled (to catch fatal errors) then injecting a note before any exit statement.

symcbean
  • 47,736
  • 6
  • 59
  • 94