Backtrace Security
   HOME

TheInfoList



OR:

In
computing Computing is any goal-oriented activity requiring, benefiting from, or creating computing machinery. It includes the study and experimentation of algorithmic processes, and development of both hardware and software. Computing has scientific, e ...
, a stack trace (also called stack backtrace or stack traceback) is a report of the active
stack frame In computer science, a call stack is a stack data structure that stores information about the active subroutines of a computer program. This kind of stack is also known as an execution stack, program stack, control stack, run-time stack, or mac ...
s at a certain point in time during the execution of a
program Program, programme, programmer, or programming may refer to: Business and management * Program management, the process of managing several related projects * Time management * Program, a part of planning Arts and entertainment Audio * Progra ...
. When a program is run, memory is often dynamically allocated in two places; the
stack Stack may refer to: Places * Stack Island, an island game reserve in Bass Strait, south-eastern Australia, in Tasmania’s Hunter Island Group * Blue Stack Mountains, in Co. Donegal, Ireland People * Stack (surname) (including a list of people ...
and the
heap Heap or HEAP may refer to: Computing and mathematics * Heap (data structure), a data structure commonly used to implement a priority queue * Heap (mathematics), a generalization of a group * Heap (programming) (or free store), an area of memory f ...
. Memory is continuously allocated on a stack but not on a heap, thus reflective of their names. 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 before 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 In computer science, a calling convention is an implementation-level (low-level) scheme for how subroutines or functions receive parameters from their caller and how they return a result. When some code calls a function, design choices have been ...
. Programmers commonly use stack tracing during interactive and post-mortem
debugging In computer programming and software development, debugging is the process of finding and resolving '' bugs'' (defects or problems that prevent correct operation) within computer programs, software, or systems. Debugging tactics can involve in ...
. End-users may see a stack trace displayed as part of an
error message An error message is information displayed when an unforeseen occurs, usually on a computer or other device. On modern operating systems with graphical user interfaces, error messages are often displayed using dialog boxes. Error messages are used ...
, which the user can then report to a programmer. A stack trace allows tracking the sequence of
nested function In computer programming, a nested function (or nested procedure or subroutine) is a function which is defined within another function, the ''enclosing function''. Due to simple recursive scope rules, a nested function is itself invisible outside o ...
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). Sibling calls do not appear in a stack trace.


Language support

Many programming languages, including
Java Java (; id, Jawa, ; jv, ꦗꦮ; su, ) is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea to the north. With a population of 151.6 million people, Java is the world's List ...
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 C23 or C-23 may refer to: Vehicles ;Aircraft * Beechcraft C23 Musketeer, an American civil utility aircraft * Caspar C 23, a German sport aircraft * Caudron C.23, a French bomber biplane * Lockheed C-23 Altair, an American military transport * S ...
has no built-in support for doing this, but C++ users can retrieve stack traces with (for example) th
stacktrace
library A library is a collection of materials, books or media that are accessible for use and not just for display purposes. A library provides physical (hard copies) or digital access (soft copies) materials, and may be a physical location or a vir ...
. In
JavaScript JavaScript (), often abbreviated as JS, is a programming language that is one of the core technologies of the World Wide Web, alongside HTML and CSS. As of 2022, 98% of Website, websites use JavaScript on the Client (computing), client side ...
, exceptions hold a stack property that contain the stack from the place where it was thrown.


Python

As an example, the following
Python Python may refer to: Snakes * Pythonidae, a family of nonvenomous snakes found in Africa, Asia, and Australia ** ''Python'' (genus), a genus of Pythonidae found in Africa and Asia * Python (mythology), a mythical serpent Computing * Python (pro ...
program contains an error. def a(): i = 0 j = b(i) return j def b(z): k = 5 if z

0: c() return k + z def c(): error() a()
Running the program under the standard Python interpreter produces the following error message. Traceback (most recent call last): File "tb.py", line 15, in a() File "tb.py", line 3, in a j = b(i) File "tb.py", line 9, in b c() File "tb.py", line 13, in c error() NameError: name 'error' is not defined 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 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 a function would occupy the bottom of the stack and the c function would occupy the top of the stack.


Java

In
Java Java (; id, Jawa, ; jv, ꦗꦮ; su, ) is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea to the north. With a population of 151.6 million people, Java is the world's List ...
, stack traces can be dumped manually with Thread.dumpStack() Take the following input: public class Main The exception lists functions in descending order, so the most-inner call is first. java.lang.Exception: Stack trace at java.lang.Thread.dumpStack(Thread.java:1336) at Main.demo3(Main.java:15) at Main.demo2(Main.java:12) at Main.demo1(Main.java:9) at Main.demo(Main.java:6) at Main.main(Main.java:3)


C and C++

Both C and
C++ C++ (pronounced "C plus plus") is a high-level general-purpose programming language created by Danish computer scientist Bjarne Stroustrup as an extension of the C programming language, or "C with Classes". The language has expanded significan ...
(pre-
C++23 C23 or C-23 may refer to: Vehicles ;Aircraft * Beechcraft C23 Musketeer, an American civil utility aircraft * Caspar C 23, a German sport aircraft * Caudron C.23, a French bomber biplane * Lockheed C-23 Altair, an American military transport * S ...
) do not have native support for obtaining stack traces, but libraries such as
glibc The GNU C Library, commonly known as glibc, is the GNU Project's implementation of the C standard library. Despite its name, it now also directly supports C++ (and, indirectly, other programming languages). It was started in the 1980s by ...
and boost provide this functionality. In these languages, some compiler optimizations may interfere with the call stack information that can be recovered at runtime. For instance,
inlining In computing, inline expansion, or inlining, is a manual or compiler optimization that replaces a function call site with the body of the called function. Inline expansion is similar to macro expansion, but occurs during compilation, without cha ...
can cause missing stack frames,
tail call In computer science, a tail call is a subroutine call performed as the final action of a procedure. If the target of a tail is the same subroutine, the subroutine is said to be tail recursive, which is a special case of direct recursion. Tail recur ...
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. ./a.out()
x40067f X4, X-4 or x4 may refer to: * BMW X4, a German crossover automobile * Sehol X4, a Chinese crossover automobile * Honda X4, a Japanese motorcycle * Moto X4, an Android Smartphone * ''Mega Man X4'', a video game * Naish X4, a kitesurfing kite * The ...
./a.out()
x4006fe X4, X-4 or x4 may refer to: * BMW X4, a German crossover automobile * Sehol X4, a Chinese crossover automobile * Honda X4, a Japanese motorcycle * Moto X4, an Android Smartphone * ''Mega Man X4'', a video game * Naish X4, a kitesurfing kite * Th ...
./a.out()
x40070a X4, X-4 or x4 may refer to: * BMW X4, a German crossover automobile * Sehol X4, a Chinese crossover automobile * Honda X4, a Japanese motorcycle * Moto X4, an Android Smartphone * ''Mega Man X4'', a video game * Naish X4, a kitesurfing kite * The ...
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf5) x7f7e60738f45./a.out()
x400599 X4, X-4 or x4 may refer to: * BMW X4, a German crossover automobile * Sehol X4, a Chinese crossover automobile * Honda X4, a Japanese motorcycle * Moto X4, an Android Smartphone * ''Mega Man X4'', a video game * Naish X4, a kitesurfing kite * Th ...
As of
C++23 C23 or C-23 may refer to: Vehicles ;Aircraft * Beechcraft C23 Musketeer, an American civil utility aircraft * Caspar C 23, a German sport aircraft * Caudron C.23, a French bomber biplane * Lockheed C-23 Altair, an American military transport * S ...
, stack traces can be dumped manually by printing the value returned by static member function std::stacktrace::current(): std::cout << std::stacktrace::current() << '\n';


Rust

Rust Rust is an iron oxide, a usually reddish-brown oxide formed by the reaction of iron and oxygen in the catalytic presence of water or air moisture. Rust consists of hydrous iron(III) oxides (Fe2O3·nH2O) and iron(III) oxide-hydroxide (FeO(OH ...
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 Rust is an iron oxide, a usually reddish-brown oxide formed by the reaction of iron and oxygen in the catalytic presence of water or air moisture. Rust consists of hydrous iron(III) oxides (Fe2O3·nH2O) and iron(III) oxide-hydroxide (FeO(OH ...
has experimental support for stack traces on unrecoverable errors. Rust supports printing to
stderr In computer programming, standard streams are interconnected input and output communication channels between a computer program and its environment when it begins execution. The three input/output (I/O) connections are called standard input (stdin ...
when a thread panics, but it must be enabled by setting the RUST_BACKTRACE
environment variable An environment variable is a dynamic-named value that can affect the way running processes will behave on a computer. They are part of the environment in which a process runs. For example, a running process can query the value of the TEMP env ...
. When enabled, such backtraces look similar to below, with the most recent call first. thread 'main' panicked at 'execute_to_panic', main.rs:3 stack backtrace: 0: std::sys::imp::backtrace::tracing::imp::unwind_backtrace 1: std::panicking::default_hook:: 2: std::panicking::default_hook 3: std::panicking::rust_panic_with_hook 4: std::panicking::begin_panic 5: futures::task_impl::with 6: futures::task_impl::park ...


See also

*
Tail call In computer science, a tail call is a subroutine call performed as the final action of a procedure. If the target of a tail is the same subroutine, the subroutine is said to be tail recursive, which is a special case of direct recursion. Tail recur ...
*
Context (computing) In computer science, a task context is the minimal set of data used by a task (which may be a process, thread, or fiber) that must be saved to allow a task to be interrupted, and later continued from the same point. The concept of context assume ...
*
Stack overflow In software, a stack overflow occurs if the call stack pointer exceeds the stack bound. The call stack may consist of a limited amount of address space, often determined at the start of the program. The size of the call stack depends on many fac ...
*
Exception handling In computing and computer programming, exception handling is the process of responding to the occurrence of ''exceptions'' – anomalous or exceptional conditions requiring special processing – during the execution of a program. In general, an ...
*
Call stack In computer science, a call stack is a stack data structure that stores information about the active subroutines of a computer program. This kind of stack is also known as an execution stack, program stack, control stack, run-time stack, or ma ...


References

{{DEFAULTSORT:Stack Trace Debugging Articles with example Python (programming language) code