comparison graal/com.oracle.truffle.sl/src/com/oracle/truffle/sl/SLException.java @ 17004:158c9ba66e45

SL: added support for guest language stack traces to SLException; added SLAssertionError.
author Christian Humer <christian.humer@gmail.com>
date Mon, 01 Sep 2014 20:08:18 +0200
parents b16ec83edc73
children
comparison
equal deleted inserted replaced
17003:8fd42ea95f64 17004:158c9ba66e45
20 * or visit www.oracle.com if you need additional information or have any 20 * or visit www.oracle.com if you need additional information or have any
21 * questions. 21 * questions.
22 */ 22 */
23 package com.oracle.truffle.sl; 23 package com.oracle.truffle.sl;
24 24
25 import java.util.*;
26
27 import com.oracle.truffle.api.*;
28 import com.oracle.truffle.api.frame.*;
29 import com.oracle.truffle.api.nodes.*;
30 import com.oracle.truffle.api.source.*;
31 import com.oracle.truffle.sl.nodes.*;
32
25 /** 33 /**
26 * SL does not need a sophisticated error checking and reporting mechanism, so all unexpected 34 * SL does not need a sophisticated error checking and reporting mechanism, so all unexpected
27 * conditions just abort execution. This exception class is used when we abort from within the SL 35 * conditions just abort execution. This exception class is used when we abort from within the SL
28 * implementation. 36 * implementation.
29 */ 37 */
30 public class SLException extends RuntimeException { 38 public class SLException extends RuntimeException {
39
31 private static final long serialVersionUID = -6799734410727348507L; 40 private static final long serialVersionUID = -6799734410727348507L;
32 41
33 public SLException(String message) { 42 public SLException(String message) {
34 super(message); 43 super(message);
44 initCause(new Throwable("Java stack trace"));
45 }
46
47 @Override
48 public synchronized Throwable fillInStackTrace() {
49 return fillInSLStackTrace(this);
50 }
51
52 /**
53 * Uses the Truffle API to iterate the stack frames and to create and set Java
54 * {@link StackTraceElement} elements based on the source sections of the call nodes on the
55 * stack.
56 */
57 static Throwable fillInSLStackTrace(Throwable t) {
58 final List<StackTraceElement> stackTrace = new ArrayList<>();
59 Truffle.getRuntime().iterateFrames(new FrameInstanceVisitor<Void>() {
60 public Void visitFrame(FrameInstance frame) {
61 Node callNode = frame.getCallNode();
62 if (callNode == null) {
63 return null;
64 }
65 RootNode root = callNode.getRootNode();
66
67 /*
68 * There should be no RootNodes other than SLRootNodes on the stack. Just for the
69 * case if this would change.
70 */
71 String methodName = "$unknownFunction";
72 if (root instanceof SLRootNode) {
73 methodName = ((SLRootNode) root).getName();
74 }
75
76 SourceSection sourceSection = callNode.getEncapsulatingSourceSection();
77 Source source = sourceSection != null ? sourceSection.getSource() : null;
78 String sourceName = source != null ? source.getName() : null;
79 int lineNumber;
80 try {
81 lineNumber = sourceSection != null ? sourceSection.getLineLocation().getLineNumber() : -1;
82 } catch (UnsupportedOperationException e) {
83 /*
84 * SourceSection#getLineLocation() may throw an UnsupportedOperationException.
85 */
86 lineNumber = -1;
87 }
88 stackTrace.add(new StackTraceElement("SL", methodName, sourceName, lineNumber));
89 return null;
90 }
91 });
92 t.setStackTrace(stackTrace.toArray(new StackTraceElement[stackTrace.size()]));
93 return t;
35 } 94 }
36 } 95 }