Stack traces: Difference between revisions

Content added Content deleted
m (→‎{{header|Phix}}: added personal tag)
m (syntax highlighting fixup automation)
Line 19: Line 19:


The provided solution is specific to the [[GNAT]] Ada compiler. Further it is restricted to some platforms. See the description of the package GNAT.Traceback supplied with [[GNAT]]. The switch -g must be used in order to include debug information into the executable.
The provided solution is specific to the [[GNAT]] Ada compiler. Further it is restricted to some platforms. See the description of the package GNAT.Traceback supplied with [[GNAT]]. The switch -g must be used in order to include debug information into the executable.
<lang Ada>with Ada.Text_IO; use Ada.Text_IO;
<syntaxhighlight lang="ada">with Ada.Text_IO; use Ada.Text_IO;
with GNAT.Traceback;
with GNAT.Traceback;
with GNAT.Traceback.Symbolic;
with GNAT.Traceback.Symbolic;
Line 49: Line 49:
begin
begin
Outer (2,3,5);
Outer (2,3,5);
end Test_Stack_Trace;</lang>
end Test_Stack_Trace;</syntaxhighlight>
Sample output:
Sample output:
<pre>
<pre>
Line 70: Line 70:
ListLines can be turned on or off... with:
ListLines can be turned on or off... with:
<br> ListLines, On|Off
<br> ListLines, On|Off
<lang autohotkey>f()
<syntaxhighlight lang="autohotkey">f()
return
return
Line 86: Line 86:
msgbox, variable bindings
msgbox, variable bindings
}
}
#Persistent</lang>
#Persistent</syntaxhighlight>
Output:
Output:
<lang autohotkey>001: f()
<syntaxhighlight lang="autohotkey">001: f()
006: Return,g()
006: Return,g()
011: ListLines (0.05)
011: ListLines (0.05)
Line 102: Line 102:
0[1 of 3]: 0
0[1 of 3]: 0
ErrorLevel[1 of 3]: 0
ErrorLevel[1 of 3]: 0
</syntaxhighlight>
</lang>


=={{header|BASIC}}==
=={{header|BASIC}}==
Line 183: Line 183:
In order to be able to see the symbols, we need to link with an option telling to export symbols names in the dynamic section (for ELF and targets supporting it); for gcc, it means using the option <tt>-rdynamic</tt> (or <tt>-export-dynamic</tt> in the GNU linker). Otherwise we see only addresses. Static functions will always have their names "hidden".
In order to be able to see the symbols, we need to link with an option telling to export symbols names in the dynamic section (for ELF and targets supporting it); for gcc, it means using the option <tt>-rdynamic</tt> (or <tt>-export-dynamic</tt> in the GNU linker). Otherwise we see only addresses. Static functions will always have their names "hidden".


<lang c>#include <stdio.h>
<syntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
#include <stdlib.h>
#include <unistd.h>
#include <unistd.h>
Line 221: Line 221:
outer(2,3,5);
outer(2,3,5);
return EXIT_SUCCESS;
return EXIT_SUCCESS;
}</lang>
}</syntaxhighlight>


Sample output on my system:
Sample output on my system:
Line 251: Line 251:


'''==> stack_trace.h <=='''
'''==> stack_trace.h <=='''
<syntaxhighlight lang="c">
<lang C>
/* stack_trace.c - macros for hinting/tracing where a program crashed
/* stack_trace.c - macros for hinting/tracing where a program crashed
on a system _without_ any form of debugger.
on a system _without_ any form of debugger.
Line 330: Line 330:
#define END }
#define END }


#endif</lang>
#endif</syntaxhighlight>
'''==> stack_trace.c <=='''
'''==> stack_trace.c <=='''
<lang C>#include <stdio.h>
<syntaxhighlight lang="c">#include <stdio.h>
#include <stddef.h>
#include <stddef.h>


Line 416: Line 416:
print_indent(); fprintf(stderr, "--- (depth %d) ---\n", stack_trace.stack.depth);
print_indent(); fprintf(stderr, "--- (depth %d) ---\n", stack_trace.stack.depth);
}
}
}</lang>
}</syntaxhighlight>


'''==> stack_trace_test.c <=='''
'''==> stack_trace_test.c <=='''
Line 422: Line 422:
The following code demonstrates the usage of the macros. Note that the initial and last curly brackets have been changed to BEGIN(procedure_name) and END. This is sometimes called '''macro magic''' and is unfashionable.
The following code demonstrates the usage of the macros. Note that the initial and last curly brackets have been changed to BEGIN(procedure_name) and END. This is sometimes called '''macro magic''' and is unfashionable.


<lang C>#include <stdio.h>
<syntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
#include <stdlib.h>


Line 450: Line 450:
stack_trace.on = FALSE;
stack_trace.on = FALSE;
RETURN(EXIT_SUCCESS);
RETURN(EXIT_SUCCESS);
END</lang>
END</syntaxhighlight>


{{out}}
{{out}}
Line 469: Line 469:


=={{header|C sharp|C#}}==
=={{header|C sharp|C#}}==
<lang csharp>using System;
<syntaxhighlight lang="csharp">using System;
using System.Diagnostics;
using System.Diagnostics;


Line 493: Line 493:
Outer();
Outer();
}
}
}</lang>
}</syntaxhighlight>
Sample output:
Sample output:
<lang>at Program.Inner()
<syntaxhighlight lang="text">at Program.Inner()
at Program.Middle()
at Program.Middle()
at Program.Outer()
at Program.Outer()
at Program.Main()</lang>
at Program.Main()</syntaxhighlight>


=={{header|Clojure}}==
=={{header|Clojure}}==
Line 505: Line 505:
[http://docs.oracle.com/javase/8/docs/api/java/lang/management/ThreadMXBean.html ThreadMXBean] can be used to show you the stack of all live threads.
[http://docs.oracle.com/javase/8/docs/api/java/lang/management/ThreadMXBean.html ThreadMXBean] can be used to show you the stack of all live threads.


<lang clojure>
<syntaxhighlight lang="clojure">
(doall
(doall
(map println (.dumpAllThreads (java.lang.management.ManagementFactory/getThreadMXBean) false false)))
(map println (.dumpAllThreads (java.lang.management.ManagementFactory/getThreadMXBean) false false)))
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 529: Line 529:
Here we use SWANK, which a component of SLIME, a Common Lisp IDE (it is the library loaded into the target Lisp system to enable interaction and remote debugging), to make use of its portable debugging facilities:
Here we use SWANK, which a component of SLIME, a Common Lisp IDE (it is the library loaded into the target Lisp system to enable interaction and remote debugging), to make use of its portable debugging facilities:


<lang lisp>(swank-backend:call-with-debugging-environment
<syntaxhighlight lang="lisp">(swank-backend:call-with-debugging-environment
(lambda ()
(lambda ()
(swank:backtrace 0 nil)))</lang>
(swank:backtrace 0 nil)))</syntaxhighlight>


Here are a few lines of the result when running under [[SBCL]], the rest is omitted since it's long and boring:
Here are a few lines of the result when running under [[SBCL]], the rest is omitted since it's long and boring:
<lang lisp>((0 "((LAMBDA (SWANK-BACKEND::DEBUGGER-LOOP-FN)) #<FUNCTION (LAMBDA #) {100459BBC9}>)")
<syntaxhighlight lang="lisp">((0 "((LAMBDA (SWANK-BACKEND::DEBUGGER-LOOP-FN)) #<FUNCTION (LAMBDA #) {100459BBC9}>)")
(1 "(SB-INT:SIMPLE-EVAL-IN-LEXENV (SWANK-BACKEND:CALL-WITH-DEBUGGING-ENVIRONMENT (LAMBDA () (SWANK:BACKTRACE 0 NIL))) #<NULL-LEXENV>)")
(1 "(SB-INT:SIMPLE-EVAL-IN-LEXENV (SWANK-BACKEND:CALL-WITH-DEBUGGING-ENVIRONMENT (LAMBDA () (SWANK:BACKTRACE 0 NIL))) #<NULL-LEXENV>)")
(2 "(SWANK::EVAL-REGION \"(swank-backend:call-with-debugging-environment\\n (lambda ()\\n (swank:backtrace 0 nil)))\\n\\n\")")
(2 "(SWANK::EVAL-REGION \"(swank-backend:call-with-debugging-environment\\n (lambda ()\\n (swank:backtrace 0 nil)))\\n\\n\")")
(3 "((LAMBDA ()))") ...)</lang>
(3 "((LAMBDA ()))") ...)</syntaxhighlight>


Note that this is a data structure containing the backtrace, not a format intended for presentation. In [[SBCL]], executing <code>(sb-debug:backtrace 7)</code> produces output like this (run from the SLIME-REPL, which is why it still contains mentions of SWANK):
Note that this is a data structure containing the backtrace, not a format intended for presentation. In [[SBCL]], executing <code>(sb-debug:backtrace 7)</code> produces output like this (run from the SLIME-REPL, which is why it still contains mentions of SWANK):


<lang lisp>CL-USER> (sb-debug:backtrace 7)
<syntaxhighlight lang="lisp">CL-USER> (sb-debug:backtrace 7)
0: (SB-DEBUG::MAP-BACKTRACE
0: (SB-DEBUG::MAP-BACKTRACE
#<CLOSURE (LAMBDA (SB-DEBUG::FRAME)) {1193EFCD}>)[:EXTERNAL]
#<CLOSURE (LAMBDA (SB-DEBUG::FRAME)) {1193EFCD}>)[:EXTERNAL]
Line 557: Line 557:
6: (SWANK::CALL-WITH-RETRY-RESTART
6: (SWANK::CALL-WITH-RETRY-RESTART
"Retry SLIME REPL evaluation request."
"Retry SLIME REPL evaluation request."
#<CLOSURE (LAMBDA ()) {1193EC4D}>)</lang>
#<CLOSURE (LAMBDA ()) {1193EC4D}>)</syntaxhighlight>


SBCL's backtraces consist entirely of lists of the form <code>(<var>function-name</var> <var>args...</var>)</code>.
SBCL's backtraces consist entirely of lists of the form <code>(<var>function-name</var> <var>args...</var>)</code>.
Line 564: Line 564:
Compiled with the dmd compiler using the -g switch.
Compiled with the dmd compiler using the -g switch.
{{trans|Java}}
{{trans|Java}}
<lang d>import std.stdio, core.runtime;
<syntaxhighlight lang="d">import std.stdio, core.runtime;


void inner() { defaultTraceHandler.writeln; }
void inner() { defaultTraceHandler.writeln; }
Line 573: Line 573:
outer;
outer;
"After the stack trace.".writeln;
"After the stack trace.".writeln;
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>0x00404FBE in core.sys.windows.stacktrace.StackTrace core.sys.windows.stacktrace.StackTrace.__ctor(uint, core.sys.windows.windows.CONTEXT*) at E:\dmd2\src\druntime\import\core\sys\windows\stacktrace.d(69)
<pre>0x00404FBE in core.sys.windows.stacktrace.StackTrace core.sys.windows.stacktrace.StackTrace.__ctor(uint, core.sys.windows.windows.CONTEXT*) at E:\dmd2\src\druntime\import\core\sys\windows\stacktrace.d(69)
Line 594: Line 594:
=={{header|DWScript}}==
=={{header|DWScript}}==
Stack traces can be obtained from exception objects
Stack traces can be obtained from exception objects
<lang delphi>procedure Inner;
<syntaxhighlight lang="delphi">procedure Inner;
begin
begin
try
try
Line 614: Line 614:
end;
end;


Outer;</lang>
Outer;</syntaxhighlight>
Output:<pre>Inner [line: 4, column: 23]
Output:<pre>Inner [line: 4, column: 23]
Middle [line: 13, column: 4]
Middle [line: 13, column: 4]
Line 623: Line 623:
{{trans|C#}}
{{trans|C#}}
ELENA 5.0 :
ELENA 5.0 :
<lang elena>import extensions;
<syntaxhighlight lang="elena">import extensions;
public singleton program
public singleton program
Line 647: Line 647:
program.outer()
program.outer()
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 660: Line 660:
=={{header|Elixir}}==
=={{header|Elixir}}==
{{trans|Erlang}}
{{trans|Erlang}}
<lang elixir>defmodule Stack_traces do
<syntaxhighlight lang="elixir">defmodule Stack_traces do
def main do
def main do
{:ok, a} = outer
{:ok, a} = outer
Line 684: Line 684:
end
end


Stack_traces.main</lang>
Stack_traces.main</syntaxhighlight>


{{out}}
{{out}}
Line 702: Line 702:
{{trans|Java}}
{{trans|Java}}
Stack traces only can be obtained inside a catch block. Additionally, it doesn't work for tail calls.
Stack traces only can be obtained inside a catch block. Additionally, it doesn't work for tail calls.
<lang erlang>-module(stack_traces).
<syntaxhighlight lang="erlang">-module(stack_traces).


-export([main/0]).
-export([main/0]).
Line 719: Line 719:


inner() ->
inner() ->
try throw(42) catch 42 -> {ok,erlang:get_stacktrace()} end.</lang>
try throw(42) catch 42 -> {ok,erlang:get_stacktrace()} end.</syntaxhighlight>
{{out}}
{{out}}
<pre>[{stack_traces,inner,0,[{file,"stack_traces.erl"},{line,18}]},
<pre>[{stack_traces,inner,0,[{file,"stack_traces.erl"},{line,18}]},
Line 730: Line 730:
=={{header|F_Sharp|F#}}==
=={{header|F_Sharp|F#}}==
{{trans|C#}}
{{trans|C#}}
<lang fsharp>open System.Diagnostics
<syntaxhighlight lang="fsharp">open System.Diagnostics


type myClass() =
type myClass() =
Line 741: Line 741:
let that = new myClass()
let that = new myClass()
that.outer()
that.outer()
0</lang>
0</syntaxhighlight>
Output
Output
<pre> at Rosetta.myClass.inner()
<pre> at Rosetta.myClass.inner()
Line 750: Line 750:
=={{header|Factor}}==
=={{header|Factor}}==
This stack trace shows the listener — Factor's Read-Eval-Print-Loop. The current execution point in each call frame is indicated by <code>=></code>. Press ctrl+w in the listener to walk through the call stack one step at a time and watch how it affects the data stack. You can even step backward in most cases. This is handy for debugging.
This stack trace shows the listener — Factor's Read-Eval-Print-Loop. The current execution point in each call frame is indicated by <code>=></code>. Press ctrl+w in the listener to walk through the call stack one step at a time and watch how it affects the data stack. You can even step backward in most cases. This is handy for debugging.
<lang factor>USE: prettyprint
<syntaxhighlight lang="factor">USE: prettyprint
get-callstack callstack.</lang>
get-callstack callstack.</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 775: Line 775:


{{works with|4tH|3.60.0}}
{{works with|4tH|3.60.0}}
<lang forth>[UNDEFINED] R.S [IF]
<syntaxhighlight lang="forth">[UNDEFINED] R.S [IF]
\ Return stack counterpart of DEPTH
\ Return stack counterpart of DEPTH
\ Note the STACK-CELLS correction is there to hide RDEPTH itself
\ Note the STACK-CELLS correction is there to hide RDEPTH itself
Line 788: Line 788:
BEGIN DUP WHILE ROT DUP . >R 1- REPEAT DROP
BEGIN DUP WHILE ROT DUP . >R 1- REPEAT DROP
THEN ." (TORS) " DROP >R ;
THEN ." (TORS) " DROP >R ;
[THEN]</lang>
[THEN]</syntaxhighlight>


=={{header|Fortran}}==
=={{header|Fortran}}==
Line 811: Line 811:


=={{header|Go}}==
=={{header|Go}}==
<lang go>package main
<syntaxhighlight lang="go">package main


import (
import (
Line 824: Line 824:
fmt.Printf("%s\n", stackTrace)
fmt.Printf("%s\n", stackTrace)
fmt.Printf("(%d bytes)\n", len(stackTrace))
fmt.Printf("(%d bytes)\n", len(stackTrace))
}</lang>
}</syntaxhighlight>
outputs:
outputs:
<pre>
<pre>
Line 842: Line 842:
=={{header|Groovy}}==
=={{header|Groovy}}==
Solution:
Solution:
<lang groovy>def rawTrace = { Thread.currentThread().stackTrace }</lang>
<syntaxhighlight lang="groovy">def rawTrace = { Thread.currentThread().stackTrace }</syntaxhighlight>


Test: (demonstrates, among other things, continued execution after generating stack trace)
Test: (demonstrates, among other things, continued execution after generating stack trace)
<lang groovy>def trace = rawTrace().collect {
<syntaxhighlight lang="groovy">def trace = rawTrace().collect {
def props = it.properties
def props = it.properties
def keys = (it.properties.keySet() - (new Object().properties.keySet()))
def keys = (it.properties.keySet() - (new Object().properties.keySet()))
Line 858: Line 858:
trace.each {
trace.each {
propNames.eachWithIndex{ name, i -> printf("%-${propWidths[i]}s ", it[name].toString()) }; println ''
propNames.eachWithIndex{ name, i -> printf("%-${propWidths[i]}s ", it[name].toString()) }; println ''
}</lang>
}</syntaxhighlight>


Output:
Output:
Line 932: Line 932:
{{libheader|Unicon Code Library}}
{{libheader|Unicon Code Library}}
[http://tapestry.tucson.az.us/unilib/pack_Utils.html the following code for buildStackTrace in Utils is taken verbatim and shown below the main program]
[http://tapestry.tucson.az.us/unilib/pack_Utils.html the following code for buildStackTrace in Utils is taken verbatim and shown below the main program]
<syntaxhighlight lang="unicon">
<lang Unicon>
import Utils # for buildStackTrace
import Utils # for buildStackTrace


Line 948: Line 948:
# Using 1 as argument omits the trace of buildStackTrace itself
# Using 1 as argument omits the trace of buildStackTrace itself
every write("\t",!buildStackTrace(1))
every write("\t",!buildStackTrace(1))
end</lang>
end</syntaxhighlight>


<syntaxhighlight lang="unicon">#<p>
<lang Unicon>#<p>
# Compute the current stack trace. Starting at level <i>n</i> above
# Compute the current stack trace. Starting at level <i>n</i> above
# the current procedure. Here, <i>n</i> defaults to 0, which will
# the current procedure. Here, <i>n</i> defaults to 0, which will
Line 971: Line 971:
}
}
return L
return L
end</lang>
end</syntaxhighlight>


The output of this example is:
The output of this example is:
Line 988: Line 988:


To enable suspension and record subsequent stack frames:
To enable suspension and record subsequent stack frames:
<lang j> 13!:0]1</lang>
<syntaxhighlight lang="j"> 13!:0]1</syntaxhighlight>


To retrieve a current stack trace:
To retrieve a current stack trace:
<lang j> 13!:13''</lang>
<syntaxhighlight lang="j"> 13!:13''</syntaxhighlight>


See also: http://www.jsoftware.com/help/dictionary/dx013.htm
See also: http://www.jsoftware.com/help/dictionary/dx013.htm
Line 997: Line 997:
Example:
Example:


<lang j> f=:g
<syntaxhighlight lang="j"> f=:g
g=:13!:13 bind ''
g=:13!:13 bind ''
f 7 NB. empty stack trace because debugging has not been enabled
f 7 NB. empty stack trace because debugging has not been enabled
Line 1,010: Line 1,010:
│ │ │ │ │ │││7││ │ │
│ │ │ │ │ │││7││ │ │
│ │ │ │ │ ││└─┘│ │ │
│ │ │ │ │ ││└─┘│ │ │
└─┴─┴─┴─┴─────────────┴┴───┴──┴─┘</lang>
└─┴─┴─┴─┴─────────────┴┴───┴──┴─┘</syntaxhighlight>


Technical note: the stack trace is not displayed, here, until after the stack has been discarded. This is because we have returned the stack trace as a result and relied on J's implicit display of the result of an expression to display the stack trace.
Technical note: the stack trace is not displayed, here, until after the stack has been discarded. This is because we have returned the stack trace as a result and relied on J's implicit display of the result of an expression to display the stack trace.
Line 1,016: Line 1,016:
=={{header|Java}}==
=={{header|Java}}==
{{works with|Java|1.5+}}
{{works with|Java|1.5+}}
<lang java5>public class StackTracer {
<syntaxhighlight lang="java5">public class StackTracer {
public static void printStackTrace() {
public static void printStackTrace() {
StackTraceElement[] elems = Thread.currentThread().getStackTrace();
StackTraceElement[] elems = Thread.currentThread().getStackTrace();
Line 1,026: Line 1,026:
}
}
}
}
}</lang>
}</syntaxhighlight>
Demonstration code:
Demonstration code:
<lang java5>public class StackTraceDemo {
<syntaxhighlight lang="java5">public class StackTraceDemo {
static void inner() {
static void inner() {
StackTracer.printStackTrace();
StackTracer.printStackTrace();
Line 1,041: Line 1,041:
outer();
outer();
}
}
}</lang>
}</syntaxhighlight>
Output:
Output:
<pre>Stack trace:
<pre>Stack trace:
Line 1,052: Line 1,052:
There is no standard way to do this, but some implementations provide it.<br />
There is no standard way to do this, but some implementations provide it.<br />
{{works with|Firefox}}
{{works with|Firefox}}
<lang javascript>try {
<syntaxhighlight lang="javascript">try {
throw new Error;
throw new Error;
} catch(e) {
} catch(e) {
alert(e.stack);
alert(e.stack);
}</lang>
}</syntaxhighlight>


The following version works in many browsers but it infinitely loops when there is recursion:
The following version works in many browsers but it infinitely loops when there is recursion:
<lang javascript>function foo () {
<syntaxhighlight lang="javascript">function foo () {
var stack = "Stack trace:";
var stack = "Stack trace:";
for (var f = arguments.callee // current function
for (var f = arguments.callee // current function
Line 1,067: Line 1,067:
alert(stack);
alert(stack);
}
}
foo();</lang>
foo();</syntaxhighlight>


=={{header|Julia}}==
=={{header|Julia}}==
{{works with|Julia|0.6}}
{{works with|Julia|0.6}}


<lang julia>f() = g()
<syntaxhighlight lang="julia">f() = g()
g() = println.(stacktrace())
g() = println.(stacktrace())


f()</lang>
f()</syntaxhighlight>


{{out}}
{{out}}
Line 1,089: Line 1,089:


=={{header|Kotlin}}==
=={{header|Kotlin}}==
<lang scala>// version 1.1.2 (stacktrace.kt which compiles to StacktraceKt.class)
<syntaxhighlight lang="scala">// version 1.1.2 (stacktrace.kt which compiles to StacktraceKt.class)


fun myFunc() {
fun myFunc() {
Line 1,098: Line 1,098:
myFunc()
myFunc()
println("\nContinuing ... ")
println("\nContinuing ... ")
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 1,111: Line 1,111:
By default Lasso tracks the file path, line and column numbers. You can create a trace method to track type and method names illustrated below or use one of the public libraries like L-Debug [https://github.com/zeroloop/l-debug].
By default Lasso tracks the file path, line and column numbers. You can create a trace method to track type and method names illustrated below or use one of the public libraries like L-Debug [https://github.com/zeroloop/l-debug].


<lang Lasso>// Define our own trace method
<syntaxhighlight lang="lasso">// Define our own trace method
define trace => {
define trace => {
local(gb) = givenblock
local(gb) = givenblock
Line 1,134: Line 1,134:
)
)
return #gb()
return #gb()
}</lang>
}</syntaxhighlight>


;Use Trace:
;Use Trace:


<lang Lasso>define stackexample => type {
<syntaxhighlight lang="lasso">define stackexample => type {
public oncreate => trace => { return self }
public oncreate => trace => { return self }
public inner => trace => { }
public inner => trace => { }
Line 1,145: Line 1,145:
}
}


stackexample->outer</lang>
stackexample->outer</syntaxhighlight>


{{out}}
{{out}}
Line 1,155: Line 1,155:


=={{header|Lua}}==
=={{header|Lua}}==
<lang lua>function Inner( k )
<syntaxhighlight lang="lua">function Inner( k )
print( debug.traceback() )
print( debug.traceback() )
print "Program continues..."
print "Program continues..."
Line 1,168: Line 1,168:
end
end


Outer( 2, 3, 5 )</lang>
Outer( 2, 3, 5 )</syntaxhighlight>
<pre>stack traceback:
<pre>stack traceback:
./prog:4: in function 'Inner'
./prog:4: in function 'Inner'
Line 1,179: Line 1,179:
=={{header|Mathematica}}/{{header|Wolfram Language}}==
=={{header|Mathematica}}/{{header|Wolfram Language}}==
Built-in function Stack does the task, example I:
Built-in function Stack does the task, example I:
<lang Mathematica>f[g[1, Print[Stack[]]; 2]]</lang>
<syntaxhighlight lang="mathematica">f[g[1, Print[Stack[]]; 2]]</syntaxhighlight>
prints, gives back:
prints, gives back:
<lang Mathematica>{f,g,CompoundExpression,Print}
<syntaxhighlight lang="mathematica">{f,g,CompoundExpression,Print}
f[g[1, 2]]</lang>
f[g[1, 2]]</syntaxhighlight>
Example II:
Example II:
<lang Mathematica>f[g[1, Print[Stack[_]]; 2]]</lang>
<syntaxhighlight lang="mathematica">f[g[1, Print[Stack[_]]; 2]]</syntaxhighlight>
prints, gives back:
prints, gives back:
<lang Mathematica>{f[g[1,Print[Stack[_]];2]],g[1,Print[Stack[_]];2],Print[Stack[_]];2,Print[Stack[_]]}
<syntaxhighlight lang="mathematica">{f[g[1,Print[Stack[_]];2]],g[1,Print[Stack[_]];2],Print[Stack[_]];2,Print[Stack[_]]}
f[g[1, 2]]</lang>
f[g[1, 2]]</syntaxhighlight>
Related and similar functions are: Trace, TracePrint, TraceScan,TraceDialog, Monitor, StackInhibit, StackBegin, StackComplete. In the manual look for 'guide/SymbolicExecutionHistory'.
Related and similar functions are: Trace, TracePrint, TraceScan,TraceDialog, Monitor, StackInhibit, StackBegin, StackComplete. In the manual look for 'guide/SymbolicExecutionHistory'.


=={{header|Nanoquery}}==
=={{header|Nanoquery}}==
__calls__ contains a list that represents a trace of calls that have been made.
__calls__ contains a list that represents a trace of calls that have been made.
<lang nanoquery>def print_stack()
<syntaxhighlight lang="nanoquery">def print_stack()
global __calls__
global __calls__


Line 1,209: Line 1,209:


println
println
println "The program would continue."</lang>
println "The program would continue."</syntaxhighlight>
{{out}}
{{out}}
<pre>stack trace:
<pre>stack trace:
Line 1,222: Line 1,222:
=={{header|NetRexx}}==
=={{header|NetRexx}}==
{{trans|Java}}
{{trans|Java}}
<lang NetRexx>/* NetRexx */
<syntaxhighlight lang="netrexx">/* NetRexx */
options replace format comments java crossref symbols nobinary
options replace format comments java crossref symbols nobinary


Line 1,244: Line 1,244:
j_ = j_ + 2
j_ = j_ + 2
end i_
end i_
</syntaxhighlight>
</lang>
'''Output:'''
'''Output:'''
<pre>
<pre>
Line 1,256: Line 1,256:
=={{header|Nim}}==
=={{header|Nim}}==
In (normal) debug builds stacktraces are enabled, while in release builds stacktraces are disabled by default, but can be enabled like this: <code>nim c -d:release --stacktrace:on --linetrace:on file.nim</code>
In (normal) debug builds stacktraces are enabled, while in release builds stacktraces are disabled by default, but can be enabled like this: <code>nim c -d:release --stacktrace:on --linetrace:on file.nim</code>
<lang nim>proc g() =
<syntaxhighlight lang="nim">proc g() =
# Writes the current stack trace to stderr.
# Writes the current stack trace to stderr.
writeStackTrace()
writeStackTrace()
Line 1,267: Line 1,267:
g()
g()


f()</lang>
f()</syntaxhighlight>
{{out}}
{{out}}
For a release build:
For a release build:
Line 1,282: Line 1,282:


=={{header|Objective-C}}==
=={{header|Objective-C}}==
<lang objc>#include <execinfo.h>
<syntaxhighlight lang="objc">#include <execinfo.h>


void *frames[128];
void *frames[128];
Line 1,290: Line 1,290:
NSLog(@"%s", symbols[i]);
NSLog(@"%s", symbols[i]);
}
}
free(symbols);</lang>
free(symbols);</syntaxhighlight>


Or in Mac OS X 10.6+:
Or in Mac OS X 10.6+:
<lang objc>NSArray *symbols = [NSThread callStackSymbols];
<syntaxhighlight lang="objc">NSArray *symbols = [NSThread callStackSymbols];
for (NSString *symbol in symbols) {
for (NSString *symbol in symbols) {
NSLog(@"%@", symbol);
NSLog(@"%@", symbol);
}</lang>
}</syntaxhighlight>


=={{header|OCaml}}==
=={{header|OCaml}}==
{{works with|OCaml|3.11+}}
{{works with|OCaml|3.11+}}
<lang ocaml>let div a b = a / b
<syntaxhighlight lang="ocaml">let div a b = a / b


let () =
let () =
Line 1,307: Line 1,307:
prerr_endline(Printexc.to_string e);
prerr_endline(Printexc.to_string e);
Printexc.print_backtrace stderr;
Printexc.print_backtrace stderr;
;;</lang>
;;</syntaxhighlight>


outputs:
outputs:
Line 1,318: Line 1,318:


Then the code doesn't need additional statements:
Then the code doesn't need additional statements:
<lang ocaml>let div a b = a / b
<syntaxhighlight lang="ocaml">let div a b = a / b


let () =
let () =
let _ = div 3 0 in ()
let _ = div 3 0 in ()
;;</lang>
;;</syntaxhighlight>
outputs:
outputs:
Fatal error: exception Division_by_zero
Fatal error: exception Division_by_zero
Line 1,337: Line 1,337:
Otherwise no stack trace is available nor printed.
Otherwise no stack trace is available nor printed.


<lang Oforth>: f1 Exception throw("An exception") ;
<syntaxhighlight lang="oforth">: f1 Exception throw("An exception") ;
Integer method: f2 self f1 ;
Integer method: f2 self f1 ;
: f3 f2 ;
: f3 f2 ;
: f4 f3 ;
: f4 f3 ;


10 f4</lang>
10 f4</syntaxhighlight>


{{out}}
{{out}}
Line 1,355: Line 1,355:


=={{header|OxygenBasic}}==
=={{header|OxygenBasic}}==
<lang oxygenbasic>
<syntaxhighlight lang="oxygenbasic">
'32bit x86
'32bit x86


Line 1,436: Line 1,436:
0017FE1C 07 00000000
0017FE1C 07 00000000
*/
*/
</syntaxhighlight>
</lang>


=={{header|Oz}}==
=={{header|Oz}}==
System exceptions contain the current stack at the nested feature <code>debug.stack</code>. For example:
System exceptions contain the current stack at the nested feature <code>debug.stack</code>. For example:
<lang oz>declare
<syntaxhighlight lang="oz">declare
proc {Test}
proc {Test}
_ = 1 div 0
_ = 1 div 0
Line 1,449: Line 1,449:
catch E then
catch E then
{Inspect E}
{Inspect E}
end</lang>
end</syntaxhighlight>
Output:
Output:
[[File:Oz_stacktrace1.png|center|Stack trace of a system exception in Oz.]]
[[File:Oz_stacktrace1.png|center|Stack trace of a system exception in Oz.]]
Line 1,456: Line 1,456:


To access the stack trace directly, you can use the undocumented internal Debug module. Its <code>getTaskStack</code> function takes a thread, a depth value and a boolean "verbose" flag. It returns a list of stack frames. Example:
To access the stack trace directly, you can use the undocumented internal Debug module. Its <code>getTaskStack</code> function takes a thread, a depth value and a boolean "verbose" flag. It returns a list of stack frames. Example:
<lang oz>%% make sure that simple function calls are not optimized away
<syntaxhighlight lang="oz">%% make sure that simple function calls are not optimized away
\switch +controlflowinfo
\switch +controlflowinfo


Line 1,470: Line 1,470:
end
end
in
in
{F}</lang>
{F}</syntaxhighlight>


Output:
Output:
Line 1,476: Line 1,476:


=={{header|Perl}}==
=={{header|Perl}}==
<lang perl>use Carp 'cluck';
<syntaxhighlight lang="perl">use Carp 'cluck';


sub g {cluck 'Hello from &g';}
sub g {cluck 'Hello from &g';}
sub f {g;}
sub f {g;}


f;</lang>
f;</syntaxhighlight>


This prints:
This prints:
Line 1,491: Line 1,491:
=={{header|Phix}}==
=={{header|Phix}}==
There no standard method of obtaining a stack trace mid-run (as yet, non-fatally that is), but we can quickly cobble something together:
There no standard method of obtaining a stack trace mid-run (as yet, non-fatally that is), but we can quickly cobble something together:
<!--<lang Phix>(notonline)-->
<!--<syntaxhighlight lang="phix">(notonline)-->
<span style="color: #008080;">constant</span> <span style="color: #000000;">W</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">machine_word</span><span style="color: #0000FF;">(),</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">W</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">machine_word</span><span style="color: #0000FF;">(),</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">RTN</span><span style="color: #0000FF;">,</span><span style="color: #000000;">PREVEBP</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">W</span><span style="color: #0000FF;">=</span><span style="color: #000000;">4</span><span style="color: #0000FF;">?{</span><span style="color: #000000;">8</span><span style="color: #0000FF;">,</span><span style="color: #000000;">20</span><span style="color: #0000FF;">}:{</span><span style="color: #000000;">16</span><span style="color: #0000FF;">,</span><span style="color: #000000;">40</span><span style="color: #0000FF;">})</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">RTN</span><span style="color: #0000FF;">,</span><span style="color: #000000;">PREVEBP</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">W</span><span style="color: #0000FF;">=</span><span style="color: #000000;">4</span><span style="color: #0000FF;">?{</span><span style="color: #000000;">8</span><span style="color: #0000FF;">,</span><span style="color: #000000;">20</span><span style="color: #0000FF;">}:{</span><span style="color: #000000;">16</span><span style="color: #0000FF;">,</span><span style="color: #000000;">40</span><span style="color: #0000FF;">})</span>
Line 1,548: Line 1,548:
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">routine_id</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"dummy"</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- see note below</span>
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">routine_id</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"dummy"</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- see note below</span>
<span style="color: #000000;">one</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">one</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
<!--</lang>-->
<!--</syntaxhighlight>-->
During compilation, the symbol table entries hold an integer ternary tree index rather than a string name, and normally
During compilation, the symbol table entries hold an integer ternary tree index rather than a string name, and normally
things are left like that during interpretation (the proper string names are always written out when an executable is
things are left like that during interpretation (the proper string names are always written out when an executable is
Line 1,581: Line 1,581:
Alternatively, but only when interpreting (whereas the above works as shown both when interpreting and pre-compiled), the debugger is
Alternatively, but only when interpreting (whereas the above works as shown both when interpreting and pre-compiled), the debugger is
started with the much saner
started with the much saner
<!--<lang Phix>-->
<!--<syntaxhighlight lang="phix">-->
<span style="color: #008080;">trace</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">trace</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
<!--</lang>-->
<!--</syntaxhighlight>-->
though as yet it offers no means of examining the stack trace (resume-ably), the above suggests it should be relative easy to add.
though as yet it offers no means of examining the stack trace (resume-ably), the above suggests it should be relative easy to add.


=={{header|PHP}}==
=={{header|PHP}}==
<lang php><?php
<syntaxhighlight lang="php"><?php
class StackTraceDemo {
class StackTraceDemo {
static function inner() {
static function inner() {
Line 1,601: Line 1,601:


StackTraceDemo::outer();
StackTraceDemo::outer();
?></lang>
?></syntaxhighlight>


<pre>#0 StackTraceDemo::inner() called at [/home/cweiske/Dev/cvs/test/php-stacktrace.php:7]
<pre>#0 StackTraceDemo::inner() called at [/home/cweiske/Dev/cvs/test/php-stacktrace.php:7]
Line 1,613: Line 1,613:


As this mechanism uses 'let' to hold the stack frames, it is robust also across catch/throw, coroutines and error handling.
As this mechanism uses 'let' to hold the stack frames, it is robust also across catch/throw, coroutines and error handling.
<lang PicoLisp>(off "Stack")
<syntaxhighlight lang="picolisp">(off "Stack")


(de $$ "Prg"
(de $$ "Prg"
Line 1,648: Line 1,648:
(de dumpStack ()
(de dumpStack ()
(more (reverse (cdr "Stack")))
(more (reverse (cdr "Stack")))
T )</lang>
T )</syntaxhighlight>
Test:
Test:
<lang PicoLisp>(de foo (A B)
<syntaxhighlight lang="picolisp">(de foo (A B)
(let C 3
(let C 3
(bar (inc 'A) (inc 'B) (inc 'C)) ) )
(bar (inc 'A) (inc 'B) (inc 'C)) ) )
Line 1,658: Line 1,658:
(! println A B C) ) ) # Set a breakpoint before (println A B C)
(! println A B C) ) ) # Set a breakpoint before (println A B C)


(stackAll)</lang>
(stackAll)</syntaxhighlight>
<pre>: (foo 1 2) # Call 'foo'
<pre>: (foo 1 2) # Call 'foo'
(println A B C) # Stopped at breakpoint in 'bar'
(println A B C) # Stopped at breakpoint in 'bar'
Line 1,671: Line 1,671:


=={{header|PL/I}}==
=={{header|PL/I}}==
<syntaxhighlight lang="pl/i">
<lang PL/I>
/* The SNAP option in the ON statement is sufficient to obtain */
/* The SNAP option in the ON statement is sufficient to obtain */
/* a traceback. The SYSTEM option specifies that standard */
/* a traceback. The SYSTEM option specifies that standard */
Line 1,679: Line 1,679:
...
...
signal condition(traceback);
signal condition(traceback);
</syntaxhighlight>
</lang>


=={{header|PureBasic}}==
=={{header|PureBasic}}==
The [http://www.purebasic.com/documentation/debugger/showcallstack.html ShowCallstack()]command opens a interactive display allowing viewing of the procedures in the calling path an all their local variables.
The [http://www.purebasic.com/documentation/debugger/showcallstack.html ShowCallstack()]command opens a interactive display allowing viewing of the procedures in the calling path an all their local variables.
<lang PureBasic>Procedure Three()
<syntaxhighlight lang="purebasic">Procedure Three()
a=7
a=7
ShowCallstack()
ShowCallstack()
Line 1,699: Line 1,699:
EndProcedure
EndProcedure


One()</lang>
One()</syntaxhighlight>


=={{header|Python}}==
=={{header|Python}}==


See the [http://docs.python.org/library/traceback.html traceback] module
See the [http://docs.python.org/library/traceback.html traceback] module
<lang python>import traceback
<syntaxhighlight lang="python">import traceback


def f(): return g()
def f(): return g()
def g(): traceback.print_stack()
def g(): traceback.print_stack()


f()</lang>
f()</syntaxhighlight>


Sample output from a session in the Idle IDE:
Sample output from a session in the Idle IDE:
Line 1,807: Line 1,807:


=={{header|R}}==
=={{header|R}}==
<lang R>foo <- function()
<syntaxhighlight lang="r">foo <- function()
{
{
bar <- function()
bar <- function()
Line 1,816: Line 1,816:
}
}


foo()</lang>
foo()</syntaxhighlight>
<nowiki>[[1]]</nowiki>
<nowiki>[[1]]</nowiki>
foo()
foo()
Line 1,824: Line 1,824:


You can also see the stack trace when a function is called (or as it exits) using the trace function.
You can also see the stack trace when a function is called (or as it exits) using the trace function.
<lang R>trace("foo", recover)
<syntaxhighlight lang="r">trace("foo", recover)
foo()</lang>
foo()</syntaxhighlight>
<pre>
<pre>
Tracing foo() on entry
Tracing foo() on entry
Line 1,837: Line 1,837:


=={{header|Racket}}==
=={{header|Racket}}==
<syntaxhighlight lang="racket">
<lang Racket>
#lang racket
#lang racket


Line 1,855: Line 1,855:
(when (car s) (printf "~s: ~s\n" i (car s)))))
(when (car s) (printf "~s: ~s\n" i (car s)))))
(foo)
(foo)
</syntaxhighlight>
</lang>


Output:
Output:
Line 1,868: Line 1,868:
(formerly Perl 6)
(formerly Perl 6)
{{works with|Rakudo|2013-03-03}}
{{works with|Rakudo|2013-03-03}}
<lang perl6>sub g { say Backtrace.new.concise }
<syntaxhighlight lang="raku" line>sub g { say Backtrace.new.concise }
sub f { g }
sub f { g }
sub MAIN { f }</lang>
sub MAIN { f }</syntaxhighlight>
{{out}}
{{out}}
<pre> in sub g at bt:1
<pre> in sub g at bt:1
Line 1,877: Line 1,877:


=={{header|Raven}}==
=={{header|Raven}}==
<lang Raven> [1 2 3 4] 42 { 'a' 1 'b' 2 'c' 3 } 34.1234 ( -1 -2 -3 ) "The quick brown fox" FILE dump</lang>
<syntaxhighlight lang="raven"> [1 2 3 4] 42 { 'a' 1 'b' 2 'c' 3 } 34.1234 ( -1 -2 -3 ) "The quick brown fox" FILE dump</syntaxhighlight>
{{out}}
{{out}}
<pre>hash (4 items)
<pre>hash (4 items)
Line 1,899: Line 1,899:
=={{header|REXX}}==
=={{header|REXX}}==
{{works with|Regina}}
{{works with|Regina}}
<lang rexx>/* call stack */
<syntaxhighlight lang="rexx">/* call stack */
say 'Call A'
say 'Call A'
call A '123'
call A '123'
Line 1,928: Line 1,928:
say format(line, 3) ':' left(func, 9) ': source "' || sourceline(line) || '"'
say format(line, 3) ':' left(func, 9) ': source "' || sourceline(line) || '"'
end
end
return cs.0</lang>
return cs.0</syntaxhighlight>


{{out}}
{{out}}
Line 1,946: Line 1,946:
Regina will also dump a call stack during certain error conditions. For instance, if the code listing above raised an unhandled signal (simulated here with "signal noname" in routine C). This is not a recoverable scenario though, and the program will not continue.
Regina will also dump a call stack during certain error conditions. For instance, if the code listing above raised an unhandled signal (simulated here with "signal noname" in routine C). This is not a recoverable scenario though, and the program will not continue.


<lang rexx>C:
<syntaxhighlight lang="rexx">C:
signal noname
signal noname
call callstack
call callstack
return ARG(1)</lang>
return ARG(1)</syntaxhighlight>


{{out}}
{{out}}
Line 1,964: Line 1,964:


=={{header|Ruby}}==
=={{header|Ruby}}==
<lang ruby>def outer(a,b,c)
<syntaxhighlight lang="ruby">def outer(a,b,c)
middle a+b, b+c
middle a+b, b+c
end
end
Line 1,977: Line 1,977:
end
end


outer 2,3,5</lang>
outer 2,3,5</syntaxhighlight>
<pre>$ ruby stacktrace.rb
<pre>$ ruby stacktrace.rb
stacktrace.rb:10:in `inner'
stacktrace.rb:10:in `inner'
Line 1,986: Line 1,986:


Exceptions caught in a rescue clause contain the trace information:
Exceptions caught in a rescue clause contain the trace information:
<lang ruby>def outer(a,b,c)
<syntaxhighlight lang="ruby">def outer(a,b,c)
middle a+b, b+c
middle a+b, b+c
end
end
Line 2,004: Line 2,004:
puts e.backtrace
puts e.backtrace
end
end
puts "continuing after the rescue..."</lang>
puts "continuing after the rescue..."</syntaxhighlight>
<pre>stacktrace.rb:10:in `inner'
<pre>stacktrace.rb:10:in `inner'
stacktrace.rb:6:in `middle'
stacktrace.rb:6:in `middle'
Line 2,011: Line 2,011:
continuing after the rescue...</pre>
continuing after the rescue...</pre>
Thread has a backtrace method:
Thread has a backtrace method:
<lang ruby>p Thread.current.backtrace</lang>
<syntaxhighlight lang="ruby">p Thread.current.backtrace</syntaxhighlight>


=={{header|Scala}}==
=={{header|Scala}}==
Line 2,017: Line 2,017:
the way, could be used from Java as well, with minor modifications.
the way, could be used from Java as well, with minor modifications.


<lang scala>def callStack = try { error("exception") } catch { case ex => ex.getStackTrace drop 2 }
<syntaxhighlight lang="scala">def callStack = try { error("exception") } catch { case ex => ex.getStackTrace drop 2 }


def printStackTrace = callStack drop 1 /* don't print ourselves! */ foreach println</lang>
def printStackTrace = callStack drop 1 /* don't print ourselves! */ foreach println</syntaxhighlight>


Usage example:
Usage example:
Line 2,072: Line 2,072:
The following #printCurrentStack is already defined in the base slate image but it is replicated here.
The following #printCurrentStack is already defined in the base slate image but it is replicated here.


<lang slate>slate[1]> d@(Debugger traits) printCurrentStack &limit: limit &stream: out &showLocation: showLocation
<syntaxhighlight lang="slate">slate[1]> d@(Debugger traits) printCurrentStack &limit: limit &stream: out &showLocation: showLocation
[
[
d clone `>> [baseFramePointer: (d interpreter framePointerOf: #printCurrentStack).
d clone `>> [baseFramePointer: (d interpreter framePointerOf: #printCurrentStack).
Line 2,079: Line 2,079:
].
].
Defining function 'printCurrentStack' on: 'Debugger traits'
Defining function 'printCurrentStack' on: 'Debugger traits'
[printCurrentStack &limit: &stream: &showLocation:]</lang>
[printCurrentStack &limit: &stream: &showLocation:]</syntaxhighlight>


The output from calling the function:
The output from calling the function:


<lang slate>slate[2]> Debugger printCurrentStack.
<syntaxhighlight lang="slate">slate[2]> Debugger printCurrentStack.
Backtrace (method @ source):
Backtrace (method @ source):
frame: 0 [printCurrentStack &limit: &stream: &showLocation:] @ stdin:0
frame: 0 [printCurrentStack &limit: &stream: &showLocation:] @ stdin:0
Line 2,096: Line 2,096:
frame: 9 [start &resource:] @ src/lib/repl.slate:185
frame: 9 [start &resource:] @ src/lib/repl.slate:185
frame: 10 [start] @ src/mobius/prelude.slate:38
frame: 10 [start] @ src/mobius/prelude.slate:38
Nil</lang>
Nil</syntaxhighlight>


=={{header|Smalltalk}}==
=={{header|Smalltalk}}==
Line 2,103: Line 2,103:
A backtrace is normally sent when some error occurs; however, it can be "forced":
A backtrace is normally sent when some error occurs; however, it can be "forced":


<lang smalltalk>Object subclass: Container [
<syntaxhighlight lang="smalltalk">Object subclass: Container [
Container class >> outer: a and: b and: c [
Container class >> outer: a and: b and: c [
self middle: (a+b) and: (b+c)
self middle: (a+b) and: (b+c)
Line 2,117: Line 2,117:
Container outer: 2 and: 3 and: 5.
Container outer: 2 and: 3 and: 5.


'Anyway, we continue with it' displayNl.</lang>
'Anyway, we continue with it' displayNl.</syntaxhighlight>


Output:
Output:
Line 2,128: Line 2,128:


=={{header|Tcl}}==
=={{header|Tcl}}==
<lang tcl>proc printStackTrace {} {
<syntaxhighlight lang="tcl">proc printStackTrace {} {
puts "Stack trace:"
puts "Stack trace:"
for {set i 1} {$i < [info level]} {incr i} {
for {set i 1} {$i < [info level]} {incr i} {
puts [string repeat " " $i][info level $i]
puts [string repeat " " $i][info level $i]
}
}
}</lang>
}</syntaxhighlight>
Demonstration code:
Demonstration code:
<lang tcl>proc outer {a b c} {
<syntaxhighlight lang="tcl">proc outer {a b c} {
middle [expr {$a+$b}] [expr {$b+$c}]
middle [expr {$a+$b}] [expr {$b+$c}]
}
}
Line 2,144: Line 2,144:
printStackTrace
printStackTrace
}
}
outer 2 3 5</lang>
outer 2 3 5</syntaxhighlight>
Produces this output:
Produces this output:
<pre>Stack trace:
<pre>Stack trace:
Line 2,158: Line 2,158:


However, it is not possible to continue execution of the script afterwards. Whilst one can 'catch' such an error using Fiber.try() this will only allow you to inspect the error itself, not the chain of function calls that led up to it.
However, it is not possible to continue execution of the script afterwards. Whilst one can 'catch' such an error using Fiber.try() this will only allow you to inspect the error itself, not the chain of function calls that led up to it.
<lang ecmascript>var func2 = Fn.new {
<syntaxhighlight lang="ecmascript">var func2 = Fn.new {
Fiber.abort("Forced error.")
Fiber.abort("Forced error.")
}
}
Line 2,166: Line 2,166:
}
}


func1.call()</lang>
func1.call()</syntaxhighlight>


{{out}}
{{out}}
Line 2,177: Line 2,177:


=={{header|zkl}}==
=={{header|zkl}}==
<lang zkl>fcn f{println("F");vm.stackTrace().println()} fcn g{println("G")}
<syntaxhighlight lang="zkl">fcn f{println("F");vm.stackTrace().println()} fcn g{println("G")}
f();g();</lang>
f();g();</syntaxhighlight>
{{out}}
{{out}}
stackTrace just returns a string. You don't get to futz with the stack.
stackTrace just returns a string. You don't get to futz with the stack.