Have sync-tests send PID directly to runner.

The sync tests already have a socket to the testrunner. It should use
it to directly send the tests pid to the runner for stack-trace
dumping if the test fails.

Test: ./art/tools/run-libjdwp-tests.sh --mode=host
Bug: 70838465
Change-Id: Id1cc23be83c3de52e7341f3b430866be8772795c
diff --git a/jdwp/src/test/java/org/apache/harmony/jpda/tests/jdwp/share/JDWPSyncTestCase.java b/jdwp/src/test/java/org/apache/harmony/jpda/tests/jdwp/share/JDWPSyncTestCase.java
index c36d7ac..db86750 100644
--- a/jdwp/src/test/java/org/apache/harmony/jpda/tests/jdwp/share/JDWPSyncTestCase.java
+++ b/jdwp/src/test/java/org/apache/harmony/jpda/tests/jdwp/share/JDWPSyncTestCase.java
@@ -68,6 +68,9 @@
 
         synchronizer.startServer();
         logWriter.println("Established sync connection");
+        // Get the pid
+        String remotePid = synchronizer.receiveMessage();
+        this.debuggeeWrapper.setRealPid(Integer.valueOf(remotePid));
     }
 
     /**
diff --git a/jdwp/src/test/java/org/apache/harmony/jpda/tests/jdwp/share/JDWPUnitDebuggeeProcessWrapper.java b/jdwp/src/test/java/org/apache/harmony/jpda/tests/jdwp/share/JDWPUnitDebuggeeProcessWrapper.java
index 409ac02..8e63e91 100644
--- a/jdwp/src/test/java/org/apache/harmony/jpda/tests/jdwp/share/JDWPUnitDebuggeeProcessWrapper.java
+++ b/jdwp/src/test/java/org/apache/harmony/jpda/tests/jdwp/share/JDWPUnitDebuggeeProcessWrapper.java
@@ -50,6 +50,8 @@
      */
     public Process process;
 
+    protected int realPid = -1;
+
     protected StreamRedirector errRedir;
     protected StreamRedirector outRedir;
 
@@ -78,6 +80,11 @@
         this.expectedExitCode = expectedExitCode;
     }
 
+    public void setRealPid(int pid) {
+      logWriter.println("Got pid: " + pid);
+      this.realPid = pid;
+    }
+
     /**
      * Launches process and redirects output.
      */
@@ -327,6 +334,7 @@
         // these is the PID.
         return Integer.valueOf(new String(Files.readAllBytes(proc.resolve("stat"))).split(" ")[0]);
       } catch (IOException e) {
+        logWriter.printError("Failed to find real pid of process:", e);
         return -1;
       }
     }
@@ -349,6 +357,7 @@
         } else {
           Path children = cur.resolve("task").resolve(Integer.toString(pid)).resolve("children");
           if (!children.toFile().exists()) {
+            logWriter.printError("Failed to find children file");
             return -1;
           } else {
             for (String child_pid : ReadChildPids(children.toFile())) {
@@ -362,6 +371,7 @@
           }
         }
       } catch (Exception e) {
+        logWriter.printError("Failed to find real pid of process:", e);
         return -1;
       }
     }
@@ -389,7 +399,12 @@
     }
 
     private void GetRemoteStackTrace(Process process) throws IOException {
-      String pid = Integer.toString(GetRealPid(process));
+      int pid_number = realPid == -1 ? GetRealPid(process) : realPid;
+      if (pid_number == -1) {
+        logWriter.printError("Could not determine subprocess pid. Cannot dump process");
+        return;
+      }
+      String pid = Integer.toString(realPid);
       logWriter.println("trying to dump " + pid);
       List<String> cmd = new ArrayList<>(Arrays.asList(splitCommandLine(settings.getDumpProcessCommand())));
       cmd.add(pid);
diff --git a/jdwp/src/test/java/org/apache/harmony/jpda/tests/share/SyncDebuggee.java b/jdwp/src/test/java/org/apache/harmony/jpda/tests/share/SyncDebuggee.java
index 822a741..4ee78cb 100644
--- a/jdwp/src/test/java/org/apache/harmony/jpda/tests/share/SyncDebuggee.java
+++ b/jdwp/src/test/java/org/apache/harmony/jpda/tests/share/SyncDebuggee.java
@@ -25,6 +25,9 @@
  */
 package org.apache.harmony.jpda.tests.share;
 
+import java.io.FileReader;
+import java.io.StreamTokenizer;
+
 /**
  * The class extends <code>Debuggee</code> and adds usage of the
  * synchronization channel implemented by <code>JPDADebuggeeSynchronizer</code>.
@@ -36,6 +39,23 @@
      */
     public JPDADebuggeeSynchronizer synchronizer;
 
+    public String getPid() {
+        try {
+          StreamTokenizer toks = new StreamTokenizer(new FileReader("/proc/self/stat"));
+          toks.parseNumbers();
+          if (toks.nextToken() != StreamTokenizer.TT_NUMBER) {
+            System.out.println("Failed to tokenize /proc/self/stat correctly. " +
+                               "First token isn't a number");
+            return "-1";
+          }
+          return Integer.toString((int)toks.nval);
+        } catch (Exception e) {
+            System.out.println("Failed to get pid! " + e);
+            e.printStackTrace(System.out);
+            return "-1";
+        }
+    }
+
     /**
      * Initializes the synchronization channel.
      */
@@ -44,6 +64,7 @@
         super.onStart();
         synchronizer = createSynchronizer();
         synchronizer.startClient();
+        synchronizer.sendMessage(getPid());
     }
 
     /**