title: Why does debug_backtrace() seem to skip over several important "steps", disabling me from debugging where a FATAL error origins?
body: <p>My end goal is to be able to properly log &quot;FATAL&quot; errors, which are sadly not &quot;seen&quot; by <code>set_error_handler</code>.</p>
  <p>Currently, all PHP does is log:</p>
  <pre><code>PHP Fatal error:  Allowed memory size of 134217728 bytes exhausted (tried to allocate 65011744 bytes) in blablabla.php on line 27
  </code></pre>
  <p>That kind of log is virtually useless, since it only tells me the very last &quot;point&quot; where it died. That's why I use a custom error handler to log &quot;all&quot; errors with a <code>debug_backtrace()</code>. But since <code>set_error_handler</code> doesn't &quot;get&quot; FATAL errors, I found (on Stack Exchange) <a href="https://stackoverflow.com/questions/277224/how-do-i-catch-a-php-fatal-e-error-error">an alleged way</a> to handle those errors anyway, by registering a &quot;shutdown function&quot; which checks for the last error, makes a <code>debug_backtrace()</code> and logs that.</p>
  <p>Sadly, when I try it, the <code>debug_backtrace()</code> seems to be very limited and does not include all the &quot;steps&quot; at all. Example:</p>
  <pre><code>function handle_critical_errors()
  {
      $error_data = error_get_last();
      var_dump($error_data);
      var_dump(debug_backtrace());
  }
  
  function shutdown_handler_function()
  {
      handle_critical_errors();
  }
  
  register_shutdown_function('shutdown_handler_function');
  
  function nonbroken_function()
  {
      broken_function();
  }
  
  function broken_function()
  {
      $buffer = '';
  
      for ($i = 0; $i &lt; 999999999999999; $i++)
          $buffer .= 'this is intended to exhaust all availabile memory because trigger_error doesn\'t let me trigger a fatal error for unknown reasons';
  }
  
  nonbroken_function();
  </code></pre>
  <p>When I run this, this is outputted:</p>
  <pre><code>array(4) {
    [&quot;type&quot;]=&gt;
    int(1)
    [&quot;message&quot;]=&gt;
    string(83) &quot;Allowed memory size of 134217728 bytes exhausted (tried to allocate 65011744 bytes)&quot;
    [&quot;file&quot;]=&gt;
    string(54) &quot;blablablabla.php&quot;
    [&quot;line&quot;]=&gt;
    int(27)
  }
  array(2) {
    [0]=&gt;
    array(4) {
      [&quot;file&quot;]=&gt;
      string(54) &quot;blablablabla.php&quot;
      [&quot;line&quot;]=&gt;
      int(12)
      [&quot;function&quot;]=&gt;
      string(22) &quot;handle_critical_errors&quot;
      [&quot;args&quot;]=&gt;
      array(0) {
      }
    }
    [1]=&gt;
    array(2) {
      [&quot;function&quot;]=&gt;
      string(25) &quot;shutdown_handler_function&quot;
      [&quot;args&quot;]=&gt;
      array(0) {
      }
    }
  }
  </code></pre>
  <p>There is no mention of <code>broken_function()</code> or <code>nonbroken_function()</code>, even though these were called. I don't get it. Even though it's obvious what is causing the FATAL error in this minimal test/example, this is never or rarely the case in the &quot;real world&quot;, where it will just point at a generic line somewhere in my complex system and the only &quot;backtrace&quot; consists of just the shutdown handler function and the error handler function. There is no real &quot;context&quot;. I don't understand what I'm doing wrong.</p>
  <p>Furthermore, I don't understand the point of <code>error_get_last()</code> to begin with since PHP logs the FATAL error by default anyway, but again, shows no context even if I use the backtrace function.</p>
  <p>I've re-read the manual about the <code>debug_backtrace</code> function but it says very little about what a backtrace <em>is</em>. I thought I understood what it is, but apparently, I don't?</p>
  <p>Is the &quot;cut-off&quot; backtrace perhaps somehow related to the fact that this is a FATAL error, and this makes even that basic function unable to operate properly? If such this the case, doesn't that mean that it's literally <em>impossible</em> to properly log (and thus debug) FATAL errors without pure guesswork/manual figuring of the code?</p>