Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine where PHP is exiting using code, not debugger

Tags:

php

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.

like image 447
Zenexer Avatar asked Aug 10 '13 06:08

Zenexer


2 Answers

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.

like image 85
fsw Avatar answered Nov 12 '22 01:11

fsw


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.

like image 35
symcbean Avatar answered Nov 12 '22 00:11

symcbean