Glossary

Stack trace

What is a stack trace?

A stack trace is the list of function calls that were in progress when an error happened, each with the file and line it was made from. It shows not only where the program failed but the path it took to get there, which is often where the cause is.

How it works

Each function call is added to the call stack and removed when it returns. When an exception is created, the runtime records the stack at that moment. PHP prints it like this for an uncaught exception:

Fatal error: Uncaught RuntimeException: Payment declined for order 1042 in /var/www/app/Checkout.php:11
Stack trace:
#0 /var/www/app/OrderController.php(9): App\Checkout->charge(1042)
#1 /var/www/index.php(6): App\OrderController->store(Object(App\Checkout))
#2 {main}
  thrown in /var/www/app/Checkout.php on line 11

Read it from the top. The first line names the exception class, its message, and the file and line where it was thrown. Each numbered frame below is one call on the way there: frame #0 says that line 9 of OrderController.php called charge(1042), and frame #1 is the call that one sits inside. {main} is the top-level script where the chain began. MDN describes the same order for JavaScript: the call that directly caused the error at the top, the one that started the chain at the bottom.

PHP also gives the trace as data. getTrace() returns the frames as an array, and getPrevious() returns the exception that caused this one when code wrapped one exception in another.

Why it matters

The message says what went wrong; the trace says which path through your code led there. A helper with twenty callers throws the same exception for all of them; only the trace says which caller passed the bad value. This is why every error tracking report carries one.

What can go wrong

  • In a framework application most frames belong to the framework and vendor packages, and the frame that matters, the first one in your own code, sits far down the list.
  • Code catches an exception and throws a new one without passing the original as the previous exception, so the trace of the real cause is gone.
  • Argument values in a trace can expose personal data or secrets. PHP's zend.exception_ignore_args setting leaves them out, and zend.exception_string_param_max_len cuts long string arguments short (15 characters by default).

Check it

Locally, PHP prints the trace of an uncaught exception, and $e->getTraceAsString() gives it inside a catch block. Ranetrace's error monitoring opens each PHP stack trace on the first frame in your own code, folds runs of vendor frames into one line, and shows the source lines around each frame in your own code, secret-scrubbed before they leave your app.

Start monitoring today

Websites go down and code throws errors. Ranetrace tells you when it happens to yours, why it matters and what to do next.

Start your 30-day free trial From $9 a month. No credit card required.