Ghidra is a software reverse engineering (SRE) framework
GP-1076: Adding diagnostic messages, esp., "new session" to GDB connector.
Dan18f3075201a4
diff --git a/Ghidra/Debug/Debugger-agent-gdb/src/main/java/agent/gdb/GdbInJvmDebuggerModelFactory.java b/Ghidra/Debug/Debugger-agent-gdb/src/main/java/agent/gdb/GdbInJvmDebuggerModelFactory.javaindex e3fcde49a1..f7c949b86c 100644--- a/Ghidra/Debug/Debugger-agent-gdb/src/main/java/agent/gdb/GdbInJvmDebuggerModelFactory.java+++ b/Ghidra/Debug/Debugger-agent-gdb/src/main/java/agent/gdb/GdbInJvmDebuggerModelFactory.java@@ -48,7 +48,7 @@ public class GdbInJvmDebuggerModelFactory implements DebuggerModelFactory {public CompletableFuture<? extends DebuggerObjectModel> build() {// TODO: Choose Linux or Windows pty based on host OSGdbModelImpl model = new GdbModelImpl(new LinuxPtyFactory());- return model.startGDB(gdbCmd, new String[] {}).thenApply(__ -> model);+ return model.startGDB(existing ? null : gdbCmd, new String[] {}).thenApply(__ -> model);}@Overridediff --git a/Ghidra/Debug/Debugger-agent-gdb/src/main/java/agent/gdb/GdbOverSshDebuggerModelFactory.java b/Ghidra/Debug/Debugger-agent-gdb/src/main/java/agent/gdb/GdbOverSshDebuggerModelFactory.javaindex df2abe239b..fe47af9cf3 100644--- a/Ghidra/Debug/Debugger-agent-gdb/src/main/java/agent/gdb/GdbOverSshDebuggerModelFactory.java+++ b/Ghidra/Debug/Debugger-agent-gdb/src/main/java/agent/gdb/GdbOverSshDebuggerModelFactory.java@@ -22,7 +22,6 @@ import agent.gdb.pty.ssh.GhidraSshPtyFactory;import ghidra.dbg.DebuggerModelFactory;import ghidra.dbg.DebuggerObjectModel;import ghidra.dbg.util.ConfigurableFactory.FactoryDescription;-import ghidra.dbg.util.ConfigurableFactory.FactoryOption;@FactoryDescription(brief = "GNU gdb via SSH",@@ -69,7 +68,7 @@ public class GdbOverSshDebuggerModelFactory implements DebuggerModelFactory {factory.setUsername(username);return new GdbModelImpl(factory);}).thenCompose(model -> {- return model.startGDB(gdbCmd, new String[] {}).thenApply(__ -> model);+ return model.startGDB(existing ? null : gdbCmd, new String[] {}).thenApply(__ -> model);});}diff --git a/Ghidra/Debug/Debugger-agent-gdb/src/main/java/agent/gdb/manager/impl/GdbManagerImpl.java b/Ghidra/Debug/Debugger-agent-gdb/src/main/java/agent/gdb/manager/impl/GdbManagerImpl.javaindex 47658f03cb..f4e261450b 100644--- a/Ghidra/Debug/Debugger-agent-gdb/src/main/java/agent/gdb/manager/impl/GdbManagerImpl.java+++ b/Ghidra/Debug/Debugger-agent-gdb/src/main/java/agent/gdb/manager/impl/GdbManagerImpl.java@@ -15,13 +15,15 @@*/package agent.gdb.manager.impl;-import static ghidra.async.AsyncUtils.*;+import static ghidra.async.AsyncUtils.loop;import java.io.*;import java.util.*;import java.util.concurrent.*;import java.util.concurrent.atomic.*;+import javax.swing.JOptionPane;+import org.apache.commons.lang3.exception.ExceptionUtils;import org.python.core.PyDictionary;import org.python.util.InteractiveConsole;@@ -612,12 +614,20 @@ public class GdbManagerImpl implements GdbManager {}else {Pty mi2Pty = ptyFactory.openpty();- Msg.info(this, "Agent is waiting for GDB/MI v2 interpreter at " +- mi2Pty.getChild().nullSession());+ String mi2PtyName = mi2Pty.getChild().nullSession();+ Msg.info(this, "Agent is waiting for GDB/MI v2 interpreter at " + mi2PtyName);mi2Thread = new PtyThread(mi2Pty, Channel.STDOUT, Interpreter.MI2);mi2Thread.setName("GDB Read MI2");mi2Thread.start();++ int choice = JOptionPane.showConfirmDialog(null,+ "Please enter \"new-ui mi2 " + mi2PtyName + "\" in an existing gdb session. " ++ "Alternatively, disable 'use existing session' to launch a new session.",+ "Waiting for GDB/MI session", JOptionPane.OK_CANCEL_OPTION);+ if (choice == JOptionPane.CANCEL_OPTION) {+ terminate();+ }}}diff --git a/Ghidra/Debug/Debugger-agent-gdb/src/main/java/agent/gdb/pty/linux/LinuxPtyChild.java b/Ghidra/Debug/Debugger-agent-gdb/src/main/java/agent/gdb/pty/linux/LinuxPtyChild.javaindex 497328cd25..cf44c99a1a 100644--- a/Ghidra/Debug/Debugger-agent-gdb/src/main/java/agent/gdb/pty/linux/LinuxPtyChild.java+++ b/Ghidra/Debug/Debugger-agent-gdb/src/main/java/agent/gdb/pty/linux/LinuxPtyChild.java@@ -24,6 +24,7 @@ import java.util.*;import agent.gdb.pty.PtyChild;import agent.gdb.pty.PtySession;import agent.gdb.pty.local.LocalProcessPtySession;+import ghidra.util.Msg;public class LinuxPtyChild extends LinuxPtyEndpoint implements PtyChild {private final String name;@@ -81,7 +82,13 @@ public class LinuxPtyChild extends LinuxPtyEndpoint implements PtyChild {}builder.inheritIO();- return new LocalProcessPtySession(builder.start());+ try {+ return new LocalProcessPtySession(builder.start());+ }+ catch (Exception e) {+ Msg.error(this, "Could not start process with args " + args, e);+ throw e;+ }}protected PtySession sessionUsingPythonLeader(String[] args, Map<String, String> env)diff --git a/Ghidra/Debug/Debugger-agent-gdb/src/main/java/agent/gdb/pty/linux/LinuxPtySessionLeader.java b/Ghidra/Debug/Debugger-agent-gdb/src/main/java/agent/gdb/pty/linux/LinuxPtySessionLeader.javaindex 695e50c30d..be2a6c27b6 100644--- a/Ghidra/Debug/Debugger-agent-gdb/src/main/java/agent/gdb/pty/linux/LinuxPtySessionLeader.java+++ b/Ghidra/Debug/Debugger-agent-gdb/src/main/java/agent/gdb/pty/linux/LinuxPtySessionLeader.java@@ -18,6 +18,7 @@ package agent.gdb.pty.linux;import java.util.List;import java.util.concurrent.Callable;+import ghidra.util.Msg;import jnr.posix.POSIX;import jnr.posix.POSIXFactory;@@ -80,6 +81,7 @@ public class LinuxPtySessionLeader {checkErr(() -> LIB_POSIX.execv(subArgs.get(0), subArgs.toArray(new String[0])));}catch (Throwable t) {+ Msg.error(this, "Could not execv with args " + subArgs, t);try {checkErr(() -> LIB_POSIX.dup2(bk, 2));}diff --git a/Ghidra/Debug/Debugger-agent-gdb/src/main/java/agent/gdb/pty/ssh/GhidraSshPtyFactory.java b/Ghidra/Debug/Debugger-agent-gdb/src/main/java/agent/gdb/pty/ssh/GhidraSshPtyFactory.javaindex 104c0f65e2..727ebece88 100644--- a/Ghidra/Debug/Debugger-agent-gdb/src/main/java/agent/gdb/pty/ssh/GhidraSshPtyFactory.java+++ b/Ghidra/Debug/Debugger-agent-gdb/src/main/java/agent/gdb/pty/ssh/GhidraSshPtyFactory.java@@ -24,6 +24,7 @@ import ch.ethz.ssh2.Connection;import ch.ethz.ssh2.KnownHosts;import docking.DockingWindowManager;import docking.widgets.PasswordDialog;+import ghidra.util.Msg;import ghidra.util.exception.CancelledException;public class GhidraSshPtyFactory implements PtyFactory {@@ -97,7 +98,8 @@ public class GhidraSshPtyFactory implements PtyFactory {// TODO: Find an API that uses char[] so I can clear it!String password = new String(promptPassword(hostname, "Password for " + username));if (!sshConn.authenticateWithPassword(username, password)) {- throw new IOException("Authentication failed");+ Msg.error(this, "SSH password authentication failed");+ throw new IOException("SSH password authentication failed");}}else {@@ -108,14 +110,16 @@ public class GhidraSshPtyFactory implements PtyFactory {}String password = new String(promptPassword(hostname, "Password for " + pemFile));if (!sshConn.authenticateWithPublicKey(username, pemFile, password)) {- throw new IOException("Authentication failed");+ Msg.error(this, "SSH pukey authentication failed");+ throw new IOException("SSH pukey authentication failed");}}success = true;return sshConn;}catch (CancelledException e) {- throw new IOException("User cancelled", e);+ Msg.error(this, "SSH connection/authentication cancelled by user");+ throw new IOException("SSH connection/authentication cancelled by user", e);}finally {if (!success) {diff --git a/Ghidra/Debug/Debugger-agent-gdb/src/main/java/agent/gdb/pty/ssh/SshPtyChild.java b/Ghidra/Debug/Debugger-agent-gdb/src/main/java/agent/gdb/pty/ssh/SshPtyChild.javaindex aaebf53782..6fe81db06f 100644--- a/Ghidra/Debug/Debugger-agent-gdb/src/main/java/agent/gdb/pty/ssh/SshPtyChild.java+++ b/Ghidra/Debug/Debugger-agent-gdb/src/main/java/agent/gdb/pty/ssh/SshPtyChild.java@@ -49,7 +49,13 @@ public class SshPtyChild extends SshPtyEndpoint implements PtyChild {.collect(Collectors.joining(" ")) +" ";String cmdStr = Stream.of(args).collect(Collectors.joining(" "));- session.execCommand(envStr + cmdStr);+ try {+ session.execCommand(envStr + cmdStr);+ }+ catch (Throwable t) {+ Msg.error(this, "Could not execute remote command: " + envStr + cmdStr, t);+ throw t;+ }return new SshPtySession(session);}