<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.sarg.dev/index.php?action=history&amp;feed=atom&amp;title=Stack_trace</id>
	<title>Stack trace - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.sarg.dev/index.php?action=history&amp;feed=atom&amp;title=Stack_trace"/>
	<link rel="alternate" type="text/html" href="https://wiki.sarg.dev/index.php?title=Stack_trace&amp;action=history"/>
	<updated>2026-04-18T14:16:04Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.44.2</generator>
	<entry>
		<id>https://wiki.sarg.dev/index.php?title=Stack_trace&amp;diff=393251&amp;oldid=prev</id>
		<title>2605:8D80:6C24:88A9:E0B2:AD7C:76A8:4BDF: /* Java */</title>
		<link rel="alternate" type="text/html" href="https://wiki.sarg.dev/index.php?title=Stack_trace&amp;diff=393251&amp;oldid=prev"/>
		<updated>2025-10-19T15:53:24Z</updated>

		<summary type="html">&lt;p&gt;&lt;span class=&quot;autocomment&quot;&gt;Java&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{{Short description|Report of stack frames during program execution}}&lt;br /&gt;
{{Redirect|Backtrace|the 2018 film|Backtrace (film)}}&lt;br /&gt;
&lt;br /&gt;
In [[computing]], a &amp;#039;&amp;#039;&amp;#039;stack trace&amp;#039;&amp;#039;&amp;#039; (also called &amp;#039;&amp;#039;&amp;#039;stack backtrace&amp;#039;&amp;#039;&amp;#039;&amp;lt;ref&amp;gt;{{cite web | url=https://www.gnu.org/software/libc/manual/html_node/Backtraces.html | title=libc manual: backtraces | publisher=gnu.org | accessdate=8 July 2014}}&amp;lt;/ref&amp;gt; or &amp;#039;&amp;#039;&amp;#039;stack traceback&amp;#039;&amp;#039;&amp;#039;&amp;lt;ref&amp;gt;{{cite web | url=https://docs.python.org/3/library/traceback.html | title=traceback — Print or retrieve a stack traceback | publisher=python.org | accessdate=8 July 2014}}&amp;lt;/ref&amp;gt;) is a report of the active [[stack frame]]s at a certain point in time during the execution of a [[computer program|program]]. When a program is run, memory is often dynamically allocated in two places: the [[Stack (abstract data type)#Hardware stack|stack]] and the [[Memory_management#HEAP|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&amp;#039;s &amp;#039;&amp;#039;&amp;#039;[[Call stack|function call stack]]&amp;#039;&amp;#039;&amp;#039;. 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 &amp;#039;&amp;#039;&amp;#039;activation record&amp;#039;&amp;#039;&amp;#039; is allocated on top of the call stack. Generally, the activation record stores the function&amp;#039;s arguments and local variables. What exactly it contains and how it&amp;#039;s laid out is determined by the [[calling convention]].&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
A stack trace allows tracking the sequence of [[nested function]]s 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). [[Tail call|Sibling calls]] do not appear in a stack trace.&lt;br /&gt;
&lt;br /&gt;
== Language support ==&lt;br /&gt;
Many programming languages, including [[Java (programming language)|Java]]&amp;lt;ref&amp;gt;{{cite web | title=Thread (Java SE 16 &amp;amp; JDK 16) | website=Java Platform Standard Edition &amp;amp; Java Development Kit Version 16 API Specification | date=2021-03-04 | url=https://docs.oracle.com/en/java/javase/16/docs/api/java.base/java/lang/Thread.html#getStackTrace() | access-date=2021-07-04}}&amp;lt;/ref&amp;gt; and [[C Sharp (programming language)|C#]],&amp;lt;ref&amp;gt;{{cite web | title=Environment.StackTrace Property (System) | website=Microsoft Docs | date=2021-05-07 | url=https://docs.microsoft.com/en-us/dotnet/api/system.environment.stacktrace | access-date=2021-07-04}}&amp;lt;/ref&amp;gt; have built-in support for retrieving the current stack trace via system calls. Before &amp;lt;code&amp;gt;std::stacktrace&amp;lt;/code&amp;gt; was added in standard library as a container for &amp;lt;code&amp;gt;std::stacktrace_entry&amp;lt;/code&amp;gt;, pre-[[C++23]] has no built-in support for doing this, but C++ users can retrieve stack traces with (for example) the [https://stacktrace.sourceforge.net/ stacktrace] [[library (computing)|library]]. In [[JavaScript]], [[Exception handling|exceptions]] hold a &amp;lt;code&amp;gt;stack&amp;lt;/code&amp;gt; property that contain the stack from the place where it was thrown.&lt;br /&gt;
&lt;br /&gt;
=== Python ===&lt;br /&gt;
As an example, the following [[Python (programming language)|Python]] program contains an error.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot; line=&amp;quot;1&amp;quot; highlight=&amp;quot;3,9,13,16&amp;quot;&amp;gt;&lt;br /&gt;
def a() -&amp;gt; int:&lt;br /&gt;
    i: int = 0&lt;br /&gt;
    j: int = b(i)&lt;br /&gt;
    return j&lt;br /&gt;
&lt;br /&gt;
def b(z: int) -&amp;gt; int:&lt;br /&gt;
    k: int = 5&lt;br /&gt;
    if z == 0:&lt;br /&gt;
        c()&lt;br /&gt;
    return k + z&lt;br /&gt;
&lt;br /&gt;
def c() -&amp;gt; None:&lt;br /&gt;
    error()&lt;br /&gt;
&lt;br /&gt;
if __name__ == &amp;quot;__main__&amp;quot;:&lt;br /&gt;
    a()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Running the program under the standard Python interpreter produces the following error message.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;pytb&amp;quot;&amp;gt;&lt;br /&gt;
Traceback (most recent call last):&lt;br /&gt;
  File &amp;quot;file.py&amp;quot;, line 16, in &amp;lt;module&amp;gt;&lt;br /&gt;
    a()&lt;br /&gt;
  File &amp;quot;file.py&amp;quot;, line 3, in a&lt;br /&gt;
    j = b(i)&lt;br /&gt;
  File &amp;quot;file.py&amp;quot;, line 9, in b&lt;br /&gt;
    c()&lt;br /&gt;
  File &amp;quot;file.py&amp;quot;, line 13, in c&lt;br /&gt;
    error()&lt;br /&gt;
NameError: name &amp;#039;error&amp;#039; is not defined&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
The stack trace shows where the error occurs, namely in the &amp;lt;code&amp;gt;c&amp;lt;/code&amp;gt; function. It also shows that the &amp;lt;code&amp;gt;c&amp;lt;/code&amp;gt; function was called by &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt;, which was called by &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt;, which was in turn called by the code on line 15 (the last line) of the program. The activation records for each of these three functions would be arranged in a stack such that the &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; function would occupy the bottom of the stack and the &amp;lt;code&amp;gt;c&amp;lt;/code&amp;gt; function would occupy the top of the stack.&lt;br /&gt;
&lt;br /&gt;
=== Java ===&lt;br /&gt;
In [[Java (programming language)|Java]], stack traces can be dumped manually with &amp;lt;code&amp;gt;Thread.dumpStack()&amp;lt;/code&amp;gt;&amp;lt;ref&amp;gt;{{Cite web|title=Thread (Java Platform SE 8 )|url=https://docs.oracle.com/javase/8/docs/api/java/lang/Thread.html#dumpStack--|access-date=2021-06-15|website=docs.oracle.com}}&amp;lt;/ref&amp;gt; Take the following input:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot; line=&amp;quot;1&amp;quot; highlight=&amp;quot;3,7,11,19&amp;quot;&amp;gt;&lt;br /&gt;
public class Main {&lt;br /&gt;
    static void demo() {&lt;br /&gt;
        demo1();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    static void demo1() {&lt;br /&gt;
        demo2();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    static void demo2() {&lt;br /&gt;
        demo3();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    static void demo3() {&lt;br /&gt;
        Thread.dumpStack();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    public static void main(String args[]) {&lt;br /&gt;
        demo();&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The exception lists functions in descending order, so the most-inner call is first.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
java.lang.Exception: Stack trace&lt;br /&gt;
        at java.lang.Thread.dumpStack(Thread.java:1336)&lt;br /&gt;
        at Main.demo3(Main.java:15)&lt;br /&gt;
        at Main.demo2(Main.java:11)&lt;br /&gt;
        at Main.demo1(Main.java:7)&lt;br /&gt;
        at Main.demo(Main.java:3)&lt;br /&gt;
        at Main.main(Main.java:19)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== C and C++ ===&lt;br /&gt;
Both [[C (programming language)|C]] and [[C++]] (pre-[[C++23]]) do not have native support for obtaining stack traces, but libraries such as [[GNU C Library|glibc]] and [[Boost (C++ libraries)|boost]] provide this functionality.&amp;lt;ref name=&amp;quot;glibc Backtraces&amp;quot;&amp;gt;{{Cite web|title=Backtraces (The GNU C Library)|url=https://www.gnu.org/software/libc/manual/html_node/Backtraces.html|access-date=2021-06-15|website=www.gnu.org}}&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;{{Cite web|title=Getting Started - 1.76.0|url=https://www.boost.org/doc/libs/1_76_0/doc/html/stacktrace/getting_started.html|access-date=2021-06-15|website=www.boost.org}}&amp;lt;/ref&amp;gt; In these languages, some compiler optimizations may interfere with the call stack information that can be recovered at runtime. For instance, [[Inline expansion|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.&amp;lt;ref name=&amp;quot;glibc Backtraces&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, glibc&amp;#039;s &amp;lt;code&amp;gt;backtrace()&amp;lt;/code&amp;gt; function returns an output with the program function and memory address.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
./a.out() [0x40067f]&lt;br /&gt;
./a.out() [0x4006fe]&lt;br /&gt;
./a.out() [0x40070a]&lt;br /&gt;
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf5) [0x7f7e60738f45]&lt;br /&gt;
./a.out() [0x400599]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As of [[C++23]], stack traces can be dumped manually by printing the value returned by static member function &amp;lt;code&amp;gt;std::stacktrace::current()&amp;lt;/code&amp;gt;:&amp;lt;ref&amp;gt;{{Cite web|date=2021-10-23|title=Working Draft, Standard for Programming Language C++|url=http://open-std.org/JTC1/SC22/WG21/docs/papers/2020/n4901.pdf|website=open-std.org|publisher=ISO/IEC|page=766}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c++&amp;quot;&amp;gt;&lt;br /&gt;
import std;&lt;br /&gt;
&lt;br /&gt;
using std::stacktrace;&lt;br /&gt;
&lt;br /&gt;
void bar() {&lt;br /&gt;
    std::println(&amp;quot;Stacktrace from bar():\n{}&amp;quot;, stacktrace::current());&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void foo() {&lt;br /&gt;
    bar();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main() {&lt;br /&gt;
    foo();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Rust ===&lt;br /&gt;
[[Rust (programming language)|Rust]] has two types of errors. Functions that use the panic [[Macro (computer science)|macro]] are &amp;quot;unrecoverable&amp;quot; and the current thread will become poisoned experiencing stack unwinding. Functions that return a &amp;lt;code&amp;gt;std::result::Result&amp;lt;/code&amp;gt; are &amp;quot;recoverable&amp;quot; and can be handled gracefully.&amp;lt;ref&amp;gt;{{Cite web|title=rustonomicon unwinding - Rust|url=https://doc.rust-lang.org/nomicon/unwinding.html#:~:text=Rust%20has%20a%20tiered%20error,be%20handled%2C%20the%20thread%20panics.|website=doc.rust-lang.org}}&amp;lt;/ref&amp;gt; However, recoverable errors cannot generate a stack trace as they are manually added and not a result of a runtime error.&lt;br /&gt;
&lt;br /&gt;
As of June 2021, [[Rust (programming language)|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 &amp;lt;code&amp;gt;RUST_BACKTRACE&amp;lt;/code&amp;gt; [[environment variable]].&amp;lt;ref&amp;gt;{{Cite web|title=std::backtrace - Rust|url=https://doc.rust-lang.org/std/backtrace/index.html|access-date=2021-06-15|website=doc.rust-lang.org}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When enabled, such backtraces look similar to below, with the most recent call first.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot;&amp;gt;&lt;br /&gt;
thread &amp;#039;main&amp;#039; panicked at &amp;#039;execute_to_panic&amp;#039;, main.rs:3&lt;br /&gt;
stack backtrace:&lt;br /&gt;
   0: std::sys::imp::backtrace::tracing::imp::unwind_backtrace&lt;br /&gt;
   1: std::panicking::default_hook::{{closure}}&lt;br /&gt;
   2: std::panicking::default_hook&lt;br /&gt;
   3: std::panicking::rust_panic_with_hook&lt;br /&gt;
   4: std::panicking::begin_panic&lt;br /&gt;
   5: futures::task_impl::with&lt;br /&gt;
   6: futures::task_impl::park&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
* [[Tail call]]&lt;br /&gt;
* [[Context (computing)]]&lt;br /&gt;
* [[Stack overflow]]&lt;br /&gt;
* [[Exception handling]]&lt;br /&gt;
* [[Call stack]]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
{{reflist}}&lt;br /&gt;
&lt;br /&gt;
{{DEFAULTSORT:Stack Trace}}&lt;br /&gt;
[[Category:Debugging]]&lt;br /&gt;
[[Category:Articles with example Python (programming language) code]]&lt;/div&gt;</summary>
		<author><name>2605:8D80:6C24:88A9:E0B2:AD7C:76A8:4BDF</name></author>
	</entry>
</feed>