Stack traces: Difference between revisions

From Rosetta Code
Content added Content deleted
(Added new task, together with implementation in Tcl)
 
(→‎Java: Added implementation)
Line 4: Line 4:
This task calls for you to print out (in a manner considered suitable for the platform) the current call stack. The amount of information printed for each frame on the call stack is not constrained, but should include at least the name of the function or method at that level of the stack frame. You may explicitly add a call to produce the stack trace to the (example) code being instrumented for examination.
This task calls for you to print out (in a manner considered suitable for the platform) the current call stack. The amount of information printed for each frame on the call stack is not constrained, but should include at least the name of the function or method at that level of the stack frame. You may explicitly add a call to produce the stack trace to the (example) code being instrumented for examination.
<br clear=all>
<br clear=all>
=={{header|Java}}==
{{works with|Java|5.0}}
<lang java>public class StackTracer {
public static void printStackTrace() {
StackTraceElement elems = Thread.currentThread().getStackTrace();

System.out.println("Stack trace:");
for (int i = elems.length-1, j = 2 ; i >= 1 ; i--, j+=2) {
System.out.printf("%" + j + "s%s.%s%n", "",
elems[i].getClassName(), elems[i].getMethodName());
}
}
}</lang>

=={{header|Tcl}}==
=={{header|Tcl}}==
<lang tcl>proc printStackTrace {} {
<lang tcl>proc printStackTrace {} {

Revision as of 13:02, 18 May 2009

Task
Stack traces
You are encouraged to solve this task according to the task description, using any language you may know.

Many programming languages allow for introspection of the current call stack environment. This can be for a variety of purposes such as enforcing security checks, debugging, or for getting access to the stack frame of callers.

This task calls for you to print out (in a manner considered suitable for the platform) the current call stack. The amount of information printed for each frame on the call stack is not constrained, but should include at least the name of the function or method at that level of the stack frame. You may explicitly add a call to produce the stack trace to the (example) code being instrumented for examination.

Java

Works with: Java version 5.0

<lang java>public class StackTracer {

   public static void printStackTrace() {

StackTraceElement elems = Thread.currentThread().getStackTrace();

System.out.println("Stack trace:"); for (int i = elems.length-1, j = 2 ; i >= 1 ; i--, j+=2) { System.out.printf("%" + j + "s%s.%s%n", "", elems[i].getClassName(), elems[i].getMethodName()); }

   }

}</lang>

Tcl

<lang tcl>proc printStackTrace {} {

   puts "Stack trace:"
   for {set i 1} {$i < [info level]} {incr i} {
       puts [string repeat "  " $i][info level $i]
   }

}</lang> Demonstration code: <lang tcl>proc outer {a b c} {

   middle [expr {$a+$b}] [expr {$b+$c}]

} proc middle {x y} {

   inner [expr {$x*$y}]

} proc inner k {

   printStackTrace

} outer 2 3 5</lang> Produces this output:

Stack trace:
  outer 2 3 5
    middle 5 8
      inner 40