crash_dump: fetch process/thread names before dropping privileges.

Processes that don't have dumpable set to 1 cannot have their
process/thread names read by processes that don't have all of their
capabilities. Fetch these names in crash_dump before dropping
privileges.

Bug: http://b/36237221
Test: debuggerd_test
Test: debuggerd -b `pidof android.hardware.bluetooth@1.0-service`
Change-Id: I174769e7b3c1ea9f11f9c8cbdff83028a4225783
diff --git a/debuggerd/libdebuggerd/utility.cpp b/debuggerd/libdebuggerd/utility.cpp
index 744cd72..22fde5e 100644
--- a/debuggerd/libdebuggerd/utility.cpp
+++ b/debuggerd/libdebuggerd/utility.cpp
@@ -28,6 +28,7 @@
 #include <string>
 
 #include <android-base/stringprintf.h>
+#include <android-base/unique_fd.h>
 #include <backtrace/Backtrace.h>
 #include <log/log.h>
 
@@ -202,3 +203,20 @@
     _LOG(log, logtype::MEMORY, "%s  %s\n", logline.c_str(), ascii.c_str());
   }
 }
+
+void read_with_default(const char* path, char* buf, size_t len, const char* default_value) {
+  android::base::unique_fd fd(open(path, O_RDONLY));
+  if (fd != -1) {
+    int rc = TEMP_FAILURE_RETRY(read(fd.get(), buf, len - 1));
+    if (rc != -1) {
+      buf[rc] = '\0';
+
+      // Trim trailing newlines.
+      if (rc > 0 && buf[rc - 1] == '\n') {
+        buf[rc - 1] = '\0';
+      }
+      return;
+    }
+  }
+  strcpy(buf, default_value);
+}