Stack trace

In computing, a stack trace (also called stack backtrace[1] or stack traceback[2]) 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, 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 runtime 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 is allocated on top of the runtime stack called the activation record (or stack pointer.) At a high level, an activation record allocates memory for the function's parameters and local variables declared in the function.

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.

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

 1 def a():
 2     i = 0
 3     j = b(i)
 4     return j
 5 
 6 def b(z):
 7     k = 5
 8     if z == 0:
 9         c()
10     return k + z
11 
12 def c():
13     error()
14 
15 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 <module>
    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.

Language support

Many programming languages, including Java and C#, have built-in support for retrieving the current stack trace via system calls. C++ 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.

gollark: I don't know, they might already have done it.
gollark: I wonder if the UK has made you require ID for buying SIM cards for some triangular reason yet.
gollark: That is... definitely *a* moral stance.
gollark: ...
gollark: ...

See also

References

  1. "libc manual: backtraces". gnu.org. Retrieved 8 July 2014.
  2. "traceback — Print or retrieve a stack traceback". python.org. Retrieved 8 July 2014.
This article is issued from Wikipedia. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.