Skip to content

Commit 5082847

Browse files
committed
add convenience functions to print stack during debugging
1 parent 59cc778 commit 5082847

File tree

3 files changed

+78
-23
lines changed

3 files changed

+78
-23
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/PNodeWithContext.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,13 @@
4141
package com.oracle.graal.python.nodes;
4242

4343
import com.oracle.graal.python.PythonLanguage;
44+
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
45+
import com.oracle.graal.python.runtime.exception.ExceptionUtils;
46+
import com.oracle.graal.python.runtime.exception.PException;
47+
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
4448
import com.oracle.truffle.api.Assumption;
4549
import com.oracle.truffle.api.CompilerAsserts;
50+
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
4651
import com.oracle.truffle.api.nodes.Node;
4752

4853
public abstract class PNodeWithContext extends Node {
@@ -59,4 +64,10 @@ protected static Assumption singleContextAssumption() {
5964
protected boolean isUncached() {
6065
return !isAdoptable();
6166
}
67+
68+
@TruffleBoundary
69+
public void printStack() {
70+
// a convenience methods for debugging
71+
ExceptionUtils.printPythonLikeStackTrace();
72+
}
6273
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/exception/ExceptionUtils.java

Lines changed: 61 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,42 @@
4444
import java.util.List;
4545
import java.util.ListIterator;
4646

47+
import com.oracle.truffle.api.CompilerAsserts;
4748
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
49+
import com.oracle.truffle.api.RootCallTarget;
50+
import com.oracle.truffle.api.Truffle;
4851
import com.oracle.truffle.api.TruffleStackTrace;
4952
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;
5056
import com.oracle.truffle.api.nodes.Node;
57+
import com.oracle.truffle.api.nodes.RootNode;
5158
import com.oracle.truffle.api.source.SourceSection;
5259

5360
public 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
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/exception/PException.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,4 +362,10 @@ public void ensureReified() {
362362
public PException getExceptionForReraise() {
363363
return pythonException.getExceptionForReraise(getTraceback());
364364
}
365+
366+
@TruffleBoundary
367+
public void printStack() {
368+
// a convenience methods for debugging
369+
ExceptionUtils.printPythonLikeStackTrace(this);
370+
}
365371
}

0 commit comments

Comments
 (0)