44
44
import java .util .List ;
45
45
import java .util .ListIterator ;
46
46
47
+ import com .oracle .truffle .api .CompilerAsserts ;
47
48
import com .oracle .truffle .api .CompilerDirectives .TruffleBoundary ;
49
+ import com .oracle .truffle .api .RootCallTarget ;
50
+ import com .oracle .truffle .api .Truffle ;
48
51
import com .oracle .truffle .api .TruffleStackTrace ;
49
52
import com .oracle .truffle .api .TruffleStackTraceElement ;
53
+ import com .oracle .truffle .api .frame .Frame ;
54
+ import com .oracle .truffle .api .frame .FrameInstance ;
55
+ import com .oracle .truffle .api .frame .FrameInstanceVisitor ;
50
56
import com .oracle .truffle .api .nodes .Node ;
57
+ import com .oracle .truffle .api .nodes .RootNode ;
51
58
import com .oracle .truffle .api .source .SourceSection ;
52
59
53
60
public final class ExceptionUtils {
54
61
private ExceptionUtils () {
55
62
}
56
63
64
+ @ TruffleBoundary
65
+ public static void printPythonLikeStackTrace () {
66
+ CompilerAsserts .neverPartOfCompilation ("printPythonLikeStackTrace is a debug method" );
67
+ final ArrayList <String > stack = new ArrayList <>();
68
+ Truffle .getRuntime ().iterateFrames (new FrameInstanceVisitor <Frame >() {
69
+ public Frame visitFrame (FrameInstance frameInstance ) {
70
+ RootCallTarget target = (RootCallTarget ) frameInstance .getCallTarget ();
71
+ RootNode rootNode = target .getRootNode ();
72
+ Node location = frameInstance .getCallNode ();
73
+ if (location == null ) {
74
+ location = rootNode ;
75
+ }
76
+ appendStackLine (stack , location , rootNode , true );
77
+ return null ;
78
+ }
79
+ });
80
+ printStack (stack );
81
+ }
82
+
57
83
/**
58
84
* this method is similar to 'PyErr_WriteUnraisable'
59
85
*/
@@ -63,32 +89,44 @@ public static void printPythonLikeStackTrace(Throwable e) {
63
89
if (stackTrace != null ) {
64
90
ArrayList <String > stack = new ArrayList <>();
65
91
for (TruffleStackTraceElement frame : stackTrace ) {
66
-
67
- StringBuilder sb = new StringBuilder ();
68
92
Node location = frame .getLocation ();
69
- SourceSection sourceSection = location != null ? location .getSourceSection () : null ;
70
- String rootName = frame .getTarget ().getRootNode ().getName ();
71
- if (sourceSection != null ) {
72
- sb .append (" " );
73
- String path = sourceSection .getSource ().getPath ();
74
- if (path != null ) {
75
- sb .append ("File " );
76
- }
77
- sb .append ('"' );
78
- sb .append (sourceSection .getSource ().getName ());
79
- sb .append ("\" , line " );
80
- sb .append (sourceSection .getStartLine ());
81
- sb .append (", in " );
82
- sb .append (rootName );
83
- stack .add (sb .toString ());
84
- }
85
- }
86
- System .err .println ("Traceback (most recent call last):" );
87
- ListIterator <String > listIterator = stack .listIterator (stack .size ());
88
- while (listIterator .hasPrevious ()) {
89
- System .err .println (listIterator .previous ());
93
+ RootNode rootNode = frame .getTarget ().getRootNode ();
94
+ appendStackLine (stack , location , rootNode , false );
90
95
}
96
+ printStack (stack );
91
97
}
92
98
System .err .println (e .getMessage ());
93
99
}
100
+
101
+ private static void appendStackLine (ArrayList <String > stack , Node location , RootNode rootNode , boolean evenWithoutSource ) {
102
+ StringBuilder sb = new StringBuilder ();
103
+ SourceSection sourceSection = location != null ? location .getSourceSection () : null ;
104
+ String rootName = rootNode .getName ();
105
+ if (sourceSection != null ) {
106
+ sb .append (" " );
107
+ String path = sourceSection .getSource ().getPath ();
108
+ if (path != null ) {
109
+ sb .append ("File " );
110
+ }
111
+ sb .append ('"' );
112
+ sb .append (sourceSection .getSource ().getName ());
113
+ sb .append ("\" , line " );
114
+ sb .append (sourceSection .getStartLine ());
115
+ sb .append (", in " );
116
+ } else if (evenWithoutSource ) {
117
+ sb .append ("unknown location in " );
118
+ }
119
+ if (sourceSection != null || evenWithoutSource ) {
120
+ sb .append (rootName );
121
+ stack .add (sb .toString ());
122
+ }
123
+ }
124
+
125
+ private static void printStack (final ArrayList <String > stack ) {
126
+ System .err .println ("Traceback (most recent call last):" );
127
+ ListIterator <String > listIterator = stack .listIterator (stack .size ());
128
+ while (listIterator .hasPrevious ()) {
129
+ System .err .println (listIterator .previous ());
130
+ }
131
+ }
94
132
}
0 commit comments