3131import ilg .gnuarmeclipse .debug .gdbjtag .pyocd .PyOCD ;
3232
3333import java .io .File ;
34+ import java .io .IOException ;
3435import java .util .ArrayList ;
3536import java .util .Collections ;
3637import java .util .LinkedHashSet ;
@@ -803,6 +804,13 @@ private void overrideTargetChanged() {
803804 fGdbServerTargetName .setEnabled (enabled );
804805 }
805806
807+ /**
808+ * Resolve the string in the gdbserver field and validate it. Return the
809+ * result if valid, otherwise return null.
810+ *
811+ * @return an absolute path, relative path or just the name of the
812+ * executable (if it's in PATH)
813+ */
806814 private String getPyOCDExecutablePath () {
807815 String path = null ;
808816
@@ -824,10 +832,31 @@ private String getPyOCDExecutablePath() {
824832 if (Activator .getInstance ().isDebugging ()) {
825833 System .out .printf ("pyOCD resolved path = %s\n " , path );
826834 }
835+
827836 // Validate path.
837+
828838
839+ // First check using the most efficient means: see if the file
840+ // exists. If it does, that's good enough.
829841 File file = new File (path );
830- if (!file .exists () || file .isDirectory ()) {
842+ if (!file .exists ()) {
843+ // Support pyOCD being in PATH and specified sans path (issue# 102)
844+ try {
845+ Process process = Runtime .getRuntime ().exec (path + " --version" );
846+ // If no exception, then it's an executable in PATH
847+ try {
848+ process .waitFor ();
849+ } catch (InterruptedException e ) {
850+ // No harm, no foul
851+ }
852+ } catch (IOException e ) {
853+ if (Activator .getInstance ().isDebugging ()) {
854+ System .out .printf ("pyOCD path is invalid\n " );
855+ }
856+ return null ;
857+ }
858+ }
859+ else if (file .isDirectory ()) {
831860 // TODO: Use java.nio.Files when we move to Java 7 to also check
832861 // that file is executable
833862 if (Activator .getInstance ().isDebugging ()) {
0 commit comments