Merge changes I7d394c66,I737d66e8
am: bc8433d582

Change-Id: I40a4e7f44dad478a7796a4f591d46369ad95b6be
diff --git a/debuggerd/crash_dump.cpp b/debuggerd/crash_dump.cpp
index 6585424..57a6c44 100644
--- a/debuggerd/crash_dump.cpp
+++ b/debuggerd/crash_dump.cpp
@@ -148,7 +148,12 @@
     }
   }
 
-  dprintf(output_fd.get(), "crash_dump failed to dump process %d: %s\n", target, abort_msg);
+  dprintf(output_fd.get(), "crash_dump failed to dump process");
+  if (target != 1) {
+    dprintf(output_fd.get(), " %d: %s\n", target, abort_msg);
+  } else {
+    dprintf(output_fd.get(), ": %s\n", abort_msg);
+  }
 
   _exit(1);
 }
@@ -195,7 +200,7 @@
   pid_t pseudothread_tid;
 
   if (target == 1) {
-    LOG(FATAL) << "target died before we could attach";
+    LOG(FATAL) << "target died before we could attach (received main tid = " << main_tid << ")";
   }
 
   if (!android::base::ParseInt(argv[1], &main_tid, 1, std::numeric_limits<pid_t>::max())) {
diff --git a/debuggerd/handler/debuggerd_handler.cpp b/debuggerd/handler/debuggerd_handler.cpp
index c09c2f3..cf24d57 100644
--- a/debuggerd/handler/debuggerd_handler.cpp
+++ b/debuggerd/handler/debuggerd_handler.cpp
@@ -62,6 +62,19 @@
 
 #define CRASH_DUMP_PATH "/system/bin/" CRASH_DUMP_NAME
 
+class ErrnoRestorer {
+ public:
+  ErrnoRestorer() : saved_errno_(errno) {
+  }
+
+  ~ErrnoRestorer() {
+    errno = saved_errno_;
+  }
+
+ private:
+  int saved_errno_;
+};
+
 extern "C" void debuggerd_fallback_handler(siginfo_t*, ucontext_t*, void*);
 
 static debuggerd_callbacks_t g_callbacks;
@@ -328,6 +341,10 @@
 // Handler that does crash dumping by forking and doing the processing in the child.
 // Do this by ptracing the relevant thread, and then execing debuggerd to do the actual dump.
 static void debuggerd_signal_handler(int signal_number, siginfo_t* info, void* context) {
+  // Make sure we don't change the value of errno, in case a signal comes in between the process
+  // making a syscall and checking errno.
+  ErrnoRestorer restorer;
+
   // It's possible somebody cleared the SA_SIGINFO flag, which would mean
   // our "info" arg holds an undefined value.
   if (!have_siginfo(signal_number)) {