Output correct gdbserver instructions from debuggerd.

Bug: 15021938
Change-Id: I2df433d939f5f83ed2a2a30af357b83e4d8e5331
diff --git a/debuggerd/debuggerd.cpp b/debuggerd/debuggerd.cpp
index 94232cf..0a1f5ef 100644
--- a/debuggerd/debuggerd.cpp
+++ b/debuggerd/debuggerd.cpp
@@ -92,20 +92,43 @@
 }
 
 static void wait_for_user_action(pid_t pid) {
-  // First log a helpful message
+  // Find out the name of the process that crashed.
+  char path[64];
+  snprintf(path, sizeof(path), "/proc/%d/exe", pid);
+
+  char exe[PATH_MAX];
+  int count;
+  if ((count = readlink(path, exe, sizeof(exe) - 1)) == -1) {
+    LOG("readlink('%s') failed: %s", path, strerror(errno));
+    strlcpy(exe, "unknown", sizeof(exe));
+  } else {
+    exe[count] = '\0';
+  }
+
+  // Turn "/system/bin/app_process" into "app_process".
+  // gdbserver doesn't cope with full paths (though we should fix that
+  // and remove this).
+  char* name = strrchr(exe, '/');
+  if (name == NULL) {
+    name = exe; // No '/' found.
+  } else {
+    ++name; // Skip the '/'.
+  }
+
+  // Explain how to attach the debugger.
   LOG(    "********************************************************\n"
-          "* Process %d has been suspended while crashing.  To\n"
-          "* attach gdbserver for a gdb connection on port 5039\n"
+          "* Process %d has been suspended while crashing.\n"
+          "* To attach gdbserver for a gdb connection on port 5039\n"
           "* and start gdbclient:\n"
           "*\n"
-          "*     gdbclient app_process :5039 %d\n"
+          "*     gdbclient %s :5039 %d\n"
           "*\n"
-          "* Wait for gdb to start, then press HOME or VOLUME DOWN key\n"
+          "* Wait for gdb to start, then press the VOLUME DOWN key\n"
           "* to let the process continue crashing.\n"
           "********************************************************\n",
-          pid, pid);
+          pid, name, pid);
 
-  // wait for HOME or VOLUME DOWN key
+  // Wait for VOLUME DOWN.
   if (init_getevent() == 0) {
     int ms = 1200 / 10;
     int dit = 1;
@@ -118,17 +141,14 @@
     };
     size_t s = 0;
     input_event e;
-    bool done = false;
     init_debug_led();
     enable_debug_led();
-    do {
+    while (true) {
       int timeout = abs(codes[s]) * ms;
       int res = get_event(&e, timeout);
       if (res == 0) {
-        if (e.type == EV_KEY
-            && (e.code == KEY_HOME || e.code == KEY_VOLUMEDOWN)
-            && e.value == 0) {
-          done = true;
+        if (e.type == EV_KEY && e.code == KEY_VOLUMEDOWN && e.value == 0) {
+          break;
         }
       } else if (res == 1) {
         if (++s >= sizeof(codes)/sizeof(*codes))
@@ -139,7 +159,7 @@
           disable_debug_led();
         }
       }
-    } while (!done);
+    }
     uninit_getevent();
   }