/interpreter/tags/at2dist110511/src/edu/vub/at/eval/InvocationStack.java

http://ambienttalk.googlecode.com/ · Java · 190 lines · 113 code · 24 blank · 53 comment · 10 complexity · 11131764605d99ddef01e74c197ecabb MD5 · raw file

  1. /**
  2. * AmbientTalk/2 Project
  3. * InvocationStack.java created on 10-okt-2006 at 14:15:49
  4. * (c) Programming Technology Lab, 2006 - 2007
  5. * Authors: Tom Van Cutsem & Stijn Mostinckx
  6. *
  7. * Permission is hereby granted, free of charge, to any person
  8. * obtaining a copy of this software and associated documentation
  9. * files (the "Software"), to deal in the Software without
  10. * restriction, including without limitation the rights to use,
  11. * copy, modify, merge, publish, distribute, sublicense, and/or
  12. * sell copies of the Software, and to permit persons to whom the
  13. * Software is furnished to do so, subject to the following
  14. * conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be
  17. * included in all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  20. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  21. * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  22. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  23. * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  24. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  25. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  26. * OTHER DEALINGS IN THE SOFTWARE.
  27. */
  28. package edu.vub.at.eval;
  29. import edu.vub.at.exceptions.InterpreterException;
  30. import edu.vub.at.objects.ATAbstractGrammar;
  31. import edu.vub.at.objects.ATClosure;
  32. import edu.vub.at.objects.ATObject;
  33. import edu.vub.at.objects.ATTable;
  34. import edu.vub.at.parser.SourceLocation;
  35. import edu.vub.at.trace.CallSite;
  36. import edu.vub.at.trace.Trace;
  37. import java.io.PrintStream;
  38. import java.io.PrintWriter;
  39. import java.io.Serializable;
  40. import java.util.Iterator;
  41. import java.util.ListIterator;
  42. import java.util.Set;
  43. import java.util.Stack;
  44. import java.util.Vector;
  45. /**
  46. * An InvocationStack instance represents the stack of method invocations and function applications
  47. * that are currently activated in an actor's thread. It is mainly used for debugging purposes
  48. * (e.g. generating stack trace information)
  49. *
  50. * @author tvc
  51. */
  52. public final class InvocationStack implements Cloneable, Serializable {
  53. /**
  54. * A thread-local variable is used to assign a unique invocation stack to
  55. * each separate actor. Each actor that invokes the getInvocationStack()
  56. * method receives its own separate copy of the invocation stack
  57. */
  58. private static final ThreadLocal _INVOCATION_STACK_ = new ThreadLocal() {
  59. protected synchronized Object initialValue() {
  60. return new InvocationStack();
  61. }
  62. };
  63. public static final InvocationStack getInvocationStack() {
  64. return (InvocationStack) _INVOCATION_STACK_.get();
  65. }
  66. public static final InvocationStack captureInvocationStack() {
  67. return (InvocationStack) getInvocationStack().clone();
  68. }
  69. private static class InvocationFrame implements Serializable {
  70. public final ATAbstractGrammar invocation;
  71. public final ATObject receiver;
  72. public final ATTable arguments;
  73. public InvocationFrame(ATAbstractGrammar inv, ATObject rcvr, ATTable args) {
  74. invocation = inv;
  75. receiver = rcvr;
  76. arguments = args;
  77. }
  78. public String toString() {
  79. SourceLocation loc = invocation.impl_getLocation();
  80. return Evaluator.toString(invocation) +
  81. ((loc == null) ? " (unknown source)" : " (" + loc + ")");
  82. }
  83. }
  84. private final Stack invocationStack_;
  85. protected InvocationStack() {
  86. invocationStack_ = new Stack();
  87. }
  88. private InvocationStack(Stack initstack) {
  89. invocationStack_ = initstack;
  90. }
  91. public void methodInvoked(ATAbstractGrammar methodInvocation, ATObject receiver, ATTable args) throws InterpreterException {
  92. invocationStack_.push(new InvocationFrame(methodInvocation, receiver, args));
  93. }
  94. public void functionCalled(ATAbstractGrammar funCall, ATClosure fun, ATTable evaluatedArgs) {
  95. invocationStack_.push(new InvocationFrame(funCall, fun, evaluatedArgs));
  96. }
  97. /**
  98. * @param result if null, the method invocation was aborted via an exception
  99. */
  100. public void methodReturned(ATObject result) {
  101. invocationStack_.pop();
  102. }
  103. /**
  104. * @param result if null, the function call was aborted via an exception
  105. */
  106. public void funcallReturned(ATObject result) {
  107. invocationStack_.pop();
  108. }
  109. public void printStackTrace(PrintStream s) {
  110. if(!invocationStack_.isEmpty()) {
  111. s.println("origin:");
  112. // iterator loops from bottom to top by default
  113. ListIterator i = invocationStack_.listIterator();
  114. while (i.hasNext()) { i.next(); } // skip to last element
  115. while(i.hasPrevious()) { // traverse stack top to bottom
  116. InvocationFrame frame = (InvocationFrame) i.previous();
  117. s.println("at "+frame);
  118. }
  119. }
  120. }
  121. public void printStackTrace(PrintWriter s) {
  122. if(!invocationStack_.isEmpty()) {
  123. s.println("origin:");
  124. // iterator loops from bottom to top by default
  125. ListIterator i = invocationStack_.listIterator();
  126. while (i.hasNext()) { i.next(); } // skip to last element
  127. while(i.hasPrevious()) { // traverse stack top to bottom
  128. InvocationFrame frame = (InvocationFrame) i.previous();
  129. s.println("at "+frame);
  130. }
  131. }
  132. }
  133. public Object clone() {
  134. return new InvocationStack((Stack) invocationStack_.clone());
  135. }
  136. /**
  137. * Generate a stack trace that can be processed by a post-mortem
  138. * debugger such as Causeway.
  139. */
  140. public Trace generateTrace(Set sourceFilter) {
  141. Vector callsites = new Vector(invocationStack_.size());
  142. // iterator loops from bottom to top by default
  143. ListIterator i = invocationStack_.listIterator();
  144. while (i.hasNext()) { i.next(); } // skip to last element
  145. Loop: while(i.hasPrevious()) { // traverse stack top to bottom
  146. InvocationFrame frame = (InvocationFrame) i.previous();
  147. SourceLocation loc = frame.invocation.impl_getLocation();
  148. String source = null;
  149. int[][] span = null;
  150. if (loc != null) {
  151. source = loc.fileName;
  152. // if (sourceFilter.contains(source)) { continue Loop; }
  153. for (Iterator filterIt = sourceFilter.iterator(); filterIt.hasNext();) {
  154. String filteredFileName = (String) filterIt.next();
  155. if (source.endsWith(filteredFileName)) {
  156. continue Loop; // skip this file
  157. }
  158. }
  159. span = new int[][] { new int[] { loc.line, loc.column } };
  160. }
  161. String name = frame.invocation.toString();
  162. callsites.add(new CallSite(name, source, span));
  163. }
  164. return new Trace((CallSite[]) callsites.toArray(new CallSite[callsites.size()]));
  165. }
  166. }