Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a Pretty Print Stack Dump?

Tags:

php

debugging

Let's face it, debug_backtrace() output is not very pretty. Did anyone code a wrapper?

And what's your favourite pretty var_dump() (which is usable in commercial projects, so no GPL (although LGPL is ok))

See also: A more pretty/informative Var_dump alternative in PHP?


Six years - and ten thousand views of this question - later, and I am still using this. It isn't pretty in a way that looks good on screen, like Kint (which is excellent).

It is plain text, which I can email to myself in in automated error reports and can display in the browser's developer console using ChromePhp.

/**
 * @brief Returns an HTML formatted string showing details of the backtrace
 * 
 * Example:
 * 
 *    F:\Dropbox\programs\Xampp\htdocs\api\q.php:48 e(373, 'beer', 'curry')
 *    F:\Dropbox\programs\Xampp\htdocs\api\q.php:53 d(26366, 28255, 8364)
 *    F:\Dropbox\programs\Xampp\htdocs\api\q.php:58 c()
 *    F:\Dropbox\programs\Xampp\htdocs\api\q.php:63 b(1283, 15488, 29369)
 *    F:\Dropbox\programs\Xampp\htdocs\api\q.php:72 a(788, 6077, 25010)
 */
function FormatBacktrace()
{
   $result = '<h4>Backtrace</h4>';

   foreach (debug_backtrace() as $trace)
   {
      if ($trace['function'] ==__FUNCTION__)
          continue;

      $parameters = is_array($trace['args']) ? implode(", ",$trace['args']) : "";

      if (array_key_exists('class', $trace))
         $result .= sprintf("%s:%s %s::%s(%s)<br>",   
                              $trace['file'],   
                              $trace['line'],    
                              $trace['class'],  
                              $trace['function'],  
                              $parameters);
      else
         $result .= sprintf("%s:%s %s(%s)<br>", 
                              $trace['file'], 
                              $trace['line'], 
                              $trace['function'], 
                              $parameters);
    }

    return $result;
}
like image 551
Mawg says reinstate Monica Avatar asked Nov 26 '10 02:11

Mawg says reinstate Monica


People also ask

How do you share a Stacktrace?

If you want to save a stack trace for later use or create a link to it, click the Share button store the stack trace. Sharing stack traces on elmah.io works pretty much like gists on GitHub, but are nicely formatted and provides the visitor of your stack trace with the options for copying and saving a stack trace.

Is stack trace top to bottom?

This also implies that a stack trace is printed top-down. The stack trace first prints the function call that caused the error and then prints the previous underlying calls that led up to the faulty call. Therefore, reading the first line of the stack trace shows you the exact function call that threw an error.

How do I print a call stack in Linux?

2, Call the function to output the call stack before the phenomena. In Linux kernel, just call dump_stack() function and you will find your way. Dump_stack() in Linux Kernel is used to output call stack information when there is a kernel crash/panic but we can also use it for debugging/tracing.


2 Answers

You also have kint (github repo) which has a composer package on the packagist repository

So either download the library manually or with composer, it's just a matter of :

$ composer init
$ composer require raveren/kint
$ composer install

Then, instead of ini_set('display_errors', 'On');, I prefer to use this simple handler in my main (first) include file :

if (  getenv('__project_env__') === 'DEV') {

  error_reporting(E_ALL | E_STRICT);

  function shutdown_handler() {
    $error = error_get_last();
    Kint::trace();
    Kint::dump($error);
  }
  register_shutdown_function('shutdown_handler');

} else {
 ...
}

With __project_env__ being set in Apache's Virtualhost (SetEnv __project_env__ "DEV") so as not to pollute the different branches of the git repository where the project lives with configuration items which are by essence environmental

  • In DEV : i get my debugging
  • In PROD, it's silent by default

Here is a screenshot of how the trace looks (each step is collapsible):

Kint stack trace
(source: github.io)

like image 95
Peter Host Avatar answered Sep 21 '22 08:09

Peter Host


The Xdebug extension can print stacktraces with a configurable degree of verbosity.

Xdebug stacktrace image

It also offers some additional var_dump() features such as syntax coloring:

Colored var_dump()

Edit:

Regarding the inclusion of Xdebug in a commercial project.

The Xdebug license has only a few terms and seems pretty permissive.

Xdebug is a C extension. As such re-distributing it or part of it in your project may be somewhat difficult. Depending on your requirements I see a few options:

  • Have your end user install Xdebug from a Linux distribution package or a DLL from the site
  • Distribute .dll and .so files for all supported platforms
  • Have your end user build the source code
  • Distribute a custom build of PHP
like image 45
Alex Jasmin Avatar answered Sep 20 '22 08:09

Alex Jasmin