Remove use of libbase logging in libdebuggerd.

libbase logging uses getprogname() to get the default tag, which breaks
for the fallback handler which is statically linked into the dynamic
linker. Switch to libasync_safe for logging.

Test: atest -c CtsSeccompHostTestCases:android.seccomp.cts.SeccompHostJUnit4DeviceTest#testAppZygoteSyscalls
Change-Id: Ieeaf33fb26cff4ba7e1589d1d883ac2fcc74cf47
diff --git a/debuggerd/Android.bp b/debuggerd/Android.bp
index 2390d79..53f85bf 100644
--- a/debuggerd/Android.bp
+++ b/debuggerd/Android.bp
@@ -206,6 +206,7 @@
     ],
 
     whole_static_libs: [
+        "libasync_safe",
         "gwp_asan_crash_handler",
         "libscudo",
         "libtombstone_proto",
diff --git a/debuggerd/libdebuggerd/tombstone.cpp b/debuggerd/libdebuggerd/tombstone.cpp
index 822b74a..185bd6e 100644
--- a/debuggerd/libdebuggerd/tombstone.cpp
+++ b/debuggerd/libdebuggerd/tombstone.cpp
@@ -36,12 +36,12 @@
 #include <string>
 
 #include <android-base/file.h>
-#include <android-base/logging.h>
 #include <android-base/properties.h>
 #include <android-base/stringprintf.h>
 #include <android-base/strings.h>
 #include <android-base/unique_fd.h>
 #include <android/log.h>
+#include <async_safe/log.h>
 #include <log/log.h>
 #include <log/log_read.h>
 #include <log/logprint.h>
@@ -588,7 +588,7 @@
 
   unwindstack::UnwinderFromPid unwinder(kMaxFrames, pid, unwindstack::Regs::CurrentArch());
   if (!unwinder.Init()) {
-    LOG(FATAL) << "Failed to init unwinder object.";
+    async_safe_fatal("failed to init unwinder object");
   }
 
   ProcessInfo process_info;
@@ -606,8 +606,11 @@
   Tombstone tombstone;
   engrave_tombstone_proto(&tombstone, unwinder, threads, target_thread, process_info, open_files);
 
-  if (!tombstone.SerializeToFileDescriptor(proto_fd.get())) {
-    PLOG(ERROR) << "Failed to write proto tombstone";
+  if (proto_fd != -1) {
+    if (!tombstone.SerializeToFileDescriptor(proto_fd.get())) {
+      async_safe_format_log(ANDROID_LOG_ERROR, LOG_TAG, "failed to write proto tombstone: %s",
+                            strerror(errno));
+    }
   }
 
   log_t log;
@@ -631,7 +634,7 @@
 
     auto it = threads.find(target_thread);
     if (it == threads.end()) {
-      LOG(FATAL) << "failed to find target thread";
+      async_safe_fatal("failed to find target thread");
     }
 
     dump_thread(&log, unwinder, it->second, process_info, true);
diff --git a/debuggerd/libdebuggerd/tombstone_proto.cpp b/debuggerd/libdebuggerd/tombstone_proto.cpp
index 801b112..bb3c7ea 100644
--- a/debuggerd/libdebuggerd/tombstone_proto.cpp
+++ b/debuggerd/libdebuggerd/tombstone_proto.cpp
@@ -31,7 +31,8 @@
 #include <memory>
 #include <string>
 
-#include <android-base/logging.h>
+#include <async_safe/log.h>
+
 #include <android-base/properties.h>
 #include <android-base/stringprintf.h>
 #include <android-base/strings.h>
@@ -151,13 +152,15 @@
 
   size_t length;
   if (!process_memory->ReadFully(address, &length, sizeof(length))) {
-    PLOG(ERROR) << "Failed to read abort message header";
+    async_safe_format_log(ANDROID_LOG_ERROR, LOG_TAG, "failed to read abort message header: %s",
+                          strerror(errno));
     return;
   }
 
   // The length field includes the length of the length field itself.
   if (length < sizeof(size_t)) {
-    LOG(ERROR) << "Abort message header malformed: claimed length = " << length;
+    async_safe_format_log(ANDROID_LOG_ERROR, LOG_TAG,
+                          "abort message header malformed: claimed length = %zu", length);
     return;
   }
 
@@ -168,7 +171,8 @@
   msg.resize(length);
 
   if (!process_memory->ReadFully(address + sizeof(length), &msg[0], length)) {
-    PLOG(ERROR) << "Failed to read abort message header";
+    async_safe_format_log(ANDROID_LOG_ERROR, LOG_TAG, "failed to read abort message header: %s",
+                          strerror(errno));
     return;
   }
 
@@ -236,7 +240,11 @@
 
           dump.set_begin_address(value);
 
-          CHECK(start_offset + bytes <= sizeof(buf));
+          if (start_offset + bytes > sizeof(buf)) {
+            async_safe_fatal("dump_memory overflowed? start offset = %zu, bytes read = %zd",
+                             start_offset, bytes);
+          }
+
           dump.set_memory(buf, start_offset + bytes);
 
           *thread.add_memory_dump() = std::move(dump);
@@ -247,10 +255,12 @@
   unwinder->SetRegs(regs_copy.get());
   unwinder->Unwind();
   if (unwinder->NumFrames() == 0) {
-    LOG(ERROR) << "Failed to unwind";
+    async_safe_format_log(ANDROID_LOG_ERROR, LOG_TAG, "failed to unwind");
     if (unwinder->LastErrorCode() != unwindstack::ERROR_NONE) {
-      LOG(ERROR) << "  Error code: " << unwinder->LastErrorCodeString();
-      LOG(ERROR) << "  Error address: " << StringPrintf("0x%" PRIx64, unwinder->LastErrorAddress());
+      async_safe_format_log(ANDROID_LOG_ERROR, LOG_TAG, "  error code: %s",
+                            unwinder->LastErrorCodeString());
+      async_safe_format_log(ANDROID_LOG_ERROR, LOG_TAG, "  error address: 0x%" PRIx64,
+                            unwinder->LastErrorAddress());
     }
   } else {
     unwinder->SetDisplayBuildID(true);
@@ -351,11 +361,9 @@
         // non-blocking EOF; we're done
         break;
       } else {
-        ALOGE("Error while reading log: %s\n", strerror(-actual));
         break;
       }
     } else if (actual == 0) {
-      ALOGE("Got zero bytes while reading log: %s\n", strerror(errno));
       break;
     }
 
@@ -431,7 +439,9 @@
   result.set_selinux_label(main_thread.selinux_label);
 
   result.set_process_name(main_thread.process_name);
-  CHECK(main_thread.siginfo != nullptr);
+  if (!main_thread.siginfo) {
+    async_safe_fatal("siginfo missing");
+  }
 
   Signal sig;
   sig.set_number(main_thread.signo);
diff --git a/debuggerd/libdebuggerd/tombstone_proto_to_text.cpp b/debuggerd/libdebuggerd/tombstone_proto_to_text.cpp
index 5ba0ad6..187379d 100644
--- a/debuggerd/libdebuggerd/tombstone_proto_to_text.cpp
+++ b/debuggerd/libdebuggerd/tombstone_proto_to_text.cpp
@@ -25,9 +25,9 @@
 #include <utility>
 #include <vector>
 
-#include <android-base/logging.h>
 #include <android-base/stringprintf.h>
 #include <android-base/unique_fd.h>
+#include <async_safe/log.h>
 
 #include "tombstone.pb.h"
 
@@ -113,7 +113,7 @@
       break;
 
     default:
-      LOG(FATAL) << "unknown architecture";
+      async_safe_fatal("unknown architecture");
   }
 
   for (const auto& reg : thread.registers()) {
@@ -217,7 +217,6 @@
   }
 
   if (!tombstone.has_signal_info()) {
-    LOG(ERROR) << "signal info missing in tombstone";
     CBL("signal information missing");
   } else {
     std::string fault_addr_desc;
@@ -319,7 +318,7 @@
   const auto& threads = tombstone.threads();
   auto main_thread_it = threads.find(tombstone.tid());
   if (main_thread_it == threads.end()) {
-    LOG(ERROR) << "failed to find entry for main thread in tombstone";
+    CBL("failed to find entry for main thread in tombstone");
     return false;
   }
 
diff --git a/debuggerd/libdebuggerd/utility.cpp b/debuggerd/libdebuggerd/utility.cpp
index f406ad4..f941990 100644
--- a/debuggerd/libdebuggerd/utility.cpp
+++ b/debuggerd/libdebuggerd/utility.cpp
@@ -30,11 +30,11 @@
 
 #include <string>
 
-#include <android-base/logging.h>
 #include <android-base/properties.h>
 #include <android-base/stringprintf.h>
 #include <android-base/strings.h>
 #include <android-base/unique_fd.h>
+#include <async_safe/log.h>
 #include <bionic/reserved_signals.h>
 #include <debuggerd/handler.h>
 #include <log/log.h>
@@ -259,11 +259,11 @@
   memset(&capdata, 0, sizeof(capdata));
 
   if (capset(&capheader, &capdata[0]) == -1) {
-    PLOG(FATAL) << "failed to drop capabilities";
+    async_safe_fatal("failed to drop capabilities: %s", strerror(errno));
   }
 
   if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) != 0) {
-    PLOG(FATAL) << "failed to set PR_SET_NO_NEW_PRIVS";
+    async_safe_fatal("failed to set PR_SET_NO_NEW_PRIVS: %s", strerror(errno));
   }
 }