4444import java .util .List ;
4545import java .util .ListIterator ;
4646
47+ import com .oracle .truffle .api .CompilerAsserts ;
4748import com .oracle .truffle .api .CompilerDirectives .TruffleBoundary ;
49+ import com .oracle .truffle .api .RootCallTarget ;
50+ import com .oracle .truffle .api .Truffle ;
4851import com .oracle .truffle .api .TruffleStackTrace ;
4952import 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 ;
5056import com .oracle .truffle .api .nodes .Node ;
57+ import com .oracle .truffle .api .nodes .RootNode ;
5158import com .oracle .truffle .api .source .SourceSection ;
5259
5360public final class ExceptionUtils {
5461 private ExceptionUtils () {
5562 }
5663
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+
5783 /**
5884 * this method is similar to 'PyErr_WriteUnraisable'
5985 */
@@ -63,32 +89,44 @@ public static void printPythonLikeStackTrace(Throwable e) {
6389 if (stackTrace != null ) {
6490 ArrayList <String > stack = new ArrayList <>();
6591 for (TruffleStackTraceElement frame : stackTrace ) {
66-
67- StringBuilder sb = new StringBuilder ();
6892 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 );
9095 }
96+ printStack (stack );
9197 }
9298 System .err .println (e .getMessage ());
9399 }
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+ }
94132}
0 commit comments