From be0eb3ace7e2c74274e5bab7095621df9634d740 Mon Sep 17 00:00:00 2001 From: John Cortell Date: Fri, 18 Mar 2016 12:57:49 -0500 Subject: [PATCH] Fix issue #102. Do not prevent user from relying on gdbserver in PATH and specifying exe as simple name --- .../debug/gdbjtag/pyocd/ui/TabDebugger.java | 31 ++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/ilg.gnuarmeclipse.debug.gdbjtag.pyocd/src/ilg/gnuarmeclipse/debug/gdbjtag/pyocd/ui/TabDebugger.java b/ilg.gnuarmeclipse.debug.gdbjtag.pyocd/src/ilg/gnuarmeclipse/debug/gdbjtag/pyocd/ui/TabDebugger.java index d789d6c53..e65f0a4ac 100644 --- a/ilg.gnuarmeclipse.debug.gdbjtag.pyocd/src/ilg/gnuarmeclipse/debug/gdbjtag/pyocd/ui/TabDebugger.java +++ b/ilg.gnuarmeclipse.debug.gdbjtag.pyocd/src/ilg/gnuarmeclipse/debug/gdbjtag/pyocd/ui/TabDebugger.java @@ -31,6 +31,7 @@ import ilg.gnuarmeclipse.debug.gdbjtag.pyocd.PyOCD; import java.io.File; +import java.io.IOException; import java.util.ArrayList; import java.util.Collections; import java.util.LinkedHashSet; @@ -803,6 +804,13 @@ private void overrideTargetChanged() { fGdbServerTargetName.setEnabled(enabled); } + /** + * Resolve the string in the gdbserver field and validate it. Return the + * result if valid, otherwise return null. + * + * @return an absolute path, relative path or just the name of the + * executable (if it's in PATH) + */ private String getPyOCDExecutablePath() { String path = null; @@ -824,10 +832,31 @@ private String getPyOCDExecutablePath() { if (Activator.getInstance().isDebugging()) { System.out.printf("pyOCD resolved path = %s\n", path); } + // Validate path. + + // First check using the most efficient means: see if the file + // exists. If it does, that's good enough. File file = new File(path); - if (!file.exists() || file.isDirectory()) { + if (!file.exists()) { + // Support pyOCD being in PATH and specified sans path (issue# 102) + try { + Process process = Runtime.getRuntime().exec(path + " --version"); + // If no exception, then it's an executable in PATH + try { + process.waitFor(); + } catch (InterruptedException e) { + // No harm, no foul + } + } catch (IOException e) { + if (Activator.getInstance().isDebugging()) { + System.out.printf("pyOCD path is invalid\n"); + } + return null; + } + } + else if (file.isDirectory()) { // TODO: Use java.nio.Files when we move to Java 7 to also check // that file is executable if (Activator.getInstance().isDebugging()) {