ArticleslgStudy

science

Stack trace

Stack trace is a science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Stack trace rather than just read about it. In short: In computing, a stack trace (also called stack backtrace or stack traceback) is a report of the active stack frames at a certain point in time during the execution of a program. When a program is run, memory is often dynamically allocated in two places: the stack and the heap.

Key takeaways

  • Stack trace belongs to science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Stack trace to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Stack trace from memory before moving on to harder problems.

Reference excerpt

In computing, a stack trace (also called stack backtrace or stack traceback) is a report of the active stack frames at a certain point in time during the execution of a program. When a program is run, memory is often dynamically allocated in two places: the stack and the heap. Memory is continuously allocated on a stack but not on a heap. Stack also refers to a programming construct, thus to differentiate it, this stack is referred to as the program's function call stack. Technically, once a block of memory has been allocated on the stack, it cannot be easily removed as there can be other blocks of memory that were allocated after it. Each time a function is called in a program, a block of memory called an activation record is allocated on top of the call stack. Generally, the activation record stores the function's arguments and local variables. What exactly it contains and how it's laid out is determined by the calling convention. Programmers commonly use stack tracing during interactive and post-mortem debugging. End-users may see a stack trace displayed as part of an error message, which the user can then report to a programmer. A stack trace allows tracking the sequence of nested functions called - up to the point where the stack trace is generated. In a post-mortem scenario this extends up to the function where the failure occurred (but was not necessarily caused). Sibling calls do not appear in a stack trace.

Language support Many programming languages, including Java and C#, have built-in support for retrieving the current stack trace via system calls. Before std::stacktrace was added in standard library as a container for std::stacktrace_entry, pre-C++23 has no built-in support for doing this, but C++ users can retrieve stack traces with (for example) the stacktrace library. In JavaScript, exceptions hold a stack property that contain the stack from the place where it was thrown.

Python As an example, the following Python program contains an error.

Running the program under the standard Python interpreter produces the following error message.

The stack trace shows where the error occurs, namely in the c function. It also shows that the c function was called by b, which was called by a, which was in turn called by the code on line 16 (the last line) of the program. The activation records for each of these three functions would be arranged in a stack such that the a function would occupy the bottom of the stack and the c function would occupy the top of the stack.

Java In Java, stack traces can be dumped manually with java.lang.Thread::dumpStack() Take the following input:

The exception lists functions in descending order, so the most-inner call is first.

Java also allows retrieving the current stack trace using java.lang.Thread::getStackTrace(), or displaying the stack trace at an error using java.lang.Throwable::printStackTrace(). A stack trace in Java is represented as java.lang.StackTraceElement[], an array.

C C does not have native support for obtaining stack traces, but libraries such as glibc provide this functionality. In C and C++, some compiler optimizations may interfere with the call stack information that can be recovered at runtime. For instance, inlining can cause missing stack frames, tail call optimizations can replace one stack frame with another, and frame pointer elimination can prevent call stack analysis tools from correctly interpreting the contents of the call stack. For example, glibc's backtrace() function returns an output with the program function and memory address.

C++ Prior to C++23, no mechanisms in C++ existed to obtain stack traces, but third-party libraries like Boost.Stacktrace offered stack traces. C++23 added stack traces with std::stacktrace and std::stacktrace_entry Stack traces can be dumped manually by printing the value returned by static member function std::stacktrace::current():

C# C# offers a System.Diagnostics.StackTrace class.

Rust Rust has two types of errors. Functions that use the panic macro are "unrecoverable" and the current thread will become poisoned experiencing stack unwinding. Functions that return a std::result::Result are "recoverable" and can be handled gracefully. However, recoverable errors cannot generate a stack trace as they are manually added and not a result of a runtime error. As of June 2021, Rust has experimental support for stack traces on unrecoverable errors. Rust supports printing to stderr when a thread panics, but it must be enabled by setting the RUST_BACKTRACE environment variable. When enabled, such backtraces look similar to below, with the most recent call first.

See also Tail call Context (computing) Stack overflow Exception handling Call stack

References

Worked examples

Example 1 — a first encounter with Stack trace

Start with the simplest possible case. Write down what Stack trace claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Stack trace before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Stack trace ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Stack trace

In research
Stack trace appears in science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Stack trace in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Stack trace is common in secondary-school and first-year university syllabi. It links to neighbouring topics Debugging, so understanding it makes those chapters shorter.
In everyday life
Look for Stack trace outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Stack trace” →

How to study Stack trace in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Stack trace means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Stack trace out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Stack trace in simple terms?

In computing, a stack trace (also called stack backtrace or stack traceback) is a report of the active stack frames at a certain point in time during the execution of a program. When a program is run, memory is often dynamically allocated in two places: the stack and the heap.

Why does Stack trace matter?

Because it connects several science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Stack trace?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Stack trace.

Tags

  • Debugging

Keep exploring