Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion data/python/jepeval.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@

jepeval_lines = []

class InterpreterExit(Exception):
"""Signal to Java that the interpreter should exit"""
pass


def jepeval(line):
"""attempt to compile and eval a given Python statement
Expand Down Expand Up @@ -73,7 +77,8 @@ def _jepeval(line):
try:
more_input_needed = _jepeval(line)
except SystemExit as err:
more_input_needed = False
raise InterpreterExit()
# more_input_needed = False
except Exception as err:
# Python exceptions are printed in Python instead of Java to improve error messaging
# in the Ghidra console window
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/ghidrathon/GhidrathonConsoleInputThread.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.concurrent.atomic.AtomicBoolean;
import javax.swing.SwingUtilities;

public class GhidrathonConsoleInputThread extends Thread {

Expand Down Expand Up @@ -159,6 +160,13 @@ private boolean evalPython(String line) throws RuntimeException {
new PrintWriter(console.getStdOut()));

status = python.eval(line, interactiveScript);
} catch (GhidrathonInterpreterExitException e) {
// Gracefully shut down only the interpreter
dispose(); // Stop the input thread
SwingUtilities.invokeLater(() -> {
plugin.shutdownInterpreter();
});
return false;
} finally {
interactiveScript.end(false);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package ghidrathon;

public class GhidrathonInterpreterExitException extends RuntimeException {
public GhidrathonInterpreterExitException() {
super("Ghidrathon interpreter requested exit");
}
}
25 changes: 25 additions & 0 deletions src/main/java/ghidrathon/GhidrathonPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,31 @@ private void resetInterpreterInBackground() {
inputThread.start();
}

void shutdownInterpreter() {
if (inputThread != null) {
inputThread.dispose();
inputThread = null;
}

if (console != null) {
console.dispose();
console = null;
}

interactiveScript = null;
interactiveTaskMonitor = null;

// Recreate a new console instance, so Ghidra re-registers it
SwingUtilities.invokeLater(() -> {
console = getTool()
.getService(InterpreterPanelService.class)
.createInterpreterPanel(this, false);

// Let user activate manually
console.addFirstActivationCallback(() -> resetInterpreter());
});
}

class PythonInteractiveTaskMonitor extends TaskMonitorAdapter {

private PrintWriter output = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,11 @@ public boolean eval(String line, GhidrathonScript script) {

} catch (JepException e) {

// Check for custom Python-side exception `InterpreterExit`
if (e.getMessage() != null && e.getMessage().contains("InterpreterExit")) {
throw new ghidrathon.GhidrathonInterpreterExitException(); // Throw signal Java-side
}

// Python exceptions should be handled in Python land; something bad must have happened
e.printStackTrace(this.err);
throw new RuntimeException(e);
Expand Down