Home | History | Annotate | Download | only in libdebuggerd
      1 /*
      2  * Copyright (C) 2012-2014 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #define LOG_TAG "DEBUG"
     18 
     19 #include "libdebuggerd/tombstone.h"
     20 
     21 #include <dirent.h>
     22 #include <errno.h>
     23 #include <fcntl.h>
     24 #include <inttypes.h>
     25 #include <signal.h>
     26 #include <stddef.h>
     27 #include <stdio.h>
     28 #include <stdlib.h>
     29 #include <string.h>
     30 #include <sys/ptrace.h>
     31 #include <sys/stat.h>
     32 #include <time.h>
     33 
     34 #include <memory>
     35 #include <string>
     36 
     37 #include <android-base/file.h>
     38 #include <android-base/logging.h>
     39 #include <android-base/properties.h>
     40 #include <android-base/stringprintf.h>
     41 #include <android-base/strings.h>
     42 #include <android-base/unique_fd.h>
     43 #include <android/log.h>
     44 #include <backtrace/Backtrace.h>
     45 #include <backtrace/BacktraceMap.h>
     46 #include <log/log.h>
     47 #include <log/logprint.h>
     48 #include <private/android_filesystem_config.h>
     49 #include <unwindstack/Memory.h>
     50 #include <unwindstack/Regs.h>
     51 
     52 // Needed to get DEBUGGER_SIGNAL.
     53 #include "debuggerd/handler.h"
     54 
     55 #include "libdebuggerd/backtrace.h"
     56 #include "libdebuggerd/elf_utils.h"
     57 #include "libdebuggerd/open_files_list.h"
     58 #include "libdebuggerd/utility.h"
     59 
     60 using android::base::GetBoolProperty;
     61 using android::base::GetProperty;
     62 using android::base::StringPrintf;
     63 using android::base::unique_fd;
     64 
     65 using unwindstack::Memory;
     66 using unwindstack::Regs;
     67 
     68 using namespace std::literals::string_literals;
     69 
     70 #define STACK_WORDS 16
     71 
     72 static void dump_header_info(log_t* log) {
     73   auto fingerprint = GetProperty("ro.build.fingerprint", "unknown");
     74   auto revision = GetProperty("ro.revision", "unknown");
     75 
     76   _LOG(log, logtype::HEADER, "Build fingerprint: '%s'\n", fingerprint.c_str());
     77   _LOG(log, logtype::HEADER, "Revision: '%s'\n", revision.c_str());
     78   _LOG(log, logtype::HEADER, "ABI: '%s'\n", ABI_STRING);
     79 }
     80 
     81 static void dump_probable_cause(log_t* log, const siginfo_t* si) {
     82   std::string cause;
     83   if (si->si_signo == SIGSEGV && si->si_code == SEGV_MAPERR) {
     84     if (si->si_addr < reinterpret_cast<void*>(4096)) {
     85       cause = StringPrintf("null pointer dereference");
     86     } else if (si->si_addr == reinterpret_cast<void*>(0xffff0ffc)) {
     87       cause = "call to kuser_helper_version";
     88     } else if (si->si_addr == reinterpret_cast<void*>(0xffff0fe0)) {
     89       cause = "call to kuser_get_tls";
     90     } else if (si->si_addr == reinterpret_cast<void*>(0xffff0fc0)) {
     91       cause = "call to kuser_cmpxchg";
     92     } else if (si->si_addr == reinterpret_cast<void*>(0xffff0fa0)) {
     93       cause = "call to kuser_memory_barrier";
     94     } else if (si->si_addr == reinterpret_cast<void*>(0xffff0f60)) {
     95       cause = "call to kuser_cmpxchg64";
     96     }
     97   } else if (si->si_signo == SIGSYS && si->si_code == SYS_SECCOMP) {
     98     cause = StringPrintf("seccomp prevented call to disallowed %s system call %d", ABI_STRING,
     99                          si->si_syscall);
    100   }
    101 
    102   if (!cause.empty()) _LOG(log, logtype::HEADER, "Cause: %s\n", cause.c_str());
    103 }
    104 
    105 static void dump_signal_info(log_t* log, const siginfo_t* si) {
    106   char addr_desc[32]; // ", fault addr 0x1234"
    107   if (signal_has_si_addr(si->si_signo, si->si_code)) {
    108     snprintf(addr_desc, sizeof(addr_desc), "%p", si->si_addr);
    109   } else {
    110     snprintf(addr_desc, sizeof(addr_desc), "--------");
    111   }
    112 
    113   _LOG(log, logtype::HEADER, "signal %d (%s), code %d (%s), fault addr %s\n", si->si_signo,
    114        get_signame(si->si_signo), si->si_code, get_sigcode(si->si_signo, si->si_code), addr_desc);
    115 
    116   dump_probable_cause(log, si);
    117 }
    118 
    119 static void dump_thread_info(log_t* log, const ThreadInfo& thread_info) {
    120   // Blacklist logd, logd.reader, logd.writer, logd.auditd, logd.control ...
    121   // TODO: Why is this controlled by thread name?
    122   if (thread_info.thread_name == "logd" ||
    123       android::base::StartsWith(thread_info.thread_name, "logd.")) {
    124     log->should_retrieve_logcat = false;
    125   }
    126 
    127   _LOG(log, logtype::HEADER, "pid: %d, tid: %d, name: %s  >>> %s <<<\n", thread_info.pid,
    128        thread_info.tid, thread_info.thread_name.c_str(), thread_info.process_name.c_str());
    129 }
    130 
    131 static void dump_stack_segment(log_t* log, BacktraceMap* backtrace_map, Memory* process_memory,
    132                                uint64_t* sp, size_t words, int label) {
    133   // Read the data all at once.
    134   word_t stack_data[words];
    135 
    136   // TODO: Do we need to word align this for crashes caused by a misaligned sp?
    137   //       The process_vm_readv implementation of Memory should handle this appropriately?
    138   size_t bytes_read = process_memory->Read(*sp, stack_data, sizeof(word_t) * words);
    139   words = bytes_read / sizeof(word_t);
    140   std::string line;
    141   for (size_t i = 0; i < words; i++) {
    142     line = "    ";
    143     if (i == 0 && label >= 0) {
    144       // Print the label once.
    145       line += StringPrintf("#%02d  ", label);
    146     } else {
    147       line += "     ";
    148     }
    149     line += StringPrintf("%" PRIPTR "  %" PRIPTR, *sp, static_cast<uint64_t>(stack_data[i]));
    150 
    151     backtrace_map_t map;
    152     backtrace_map->FillIn(stack_data[i], &map);
    153     std::string map_name{map.Name()};
    154     if (BacktraceMap::IsValid(map) && !map_name.empty()) {
    155       line += "  " + map_name;
    156       uint64_t offset = 0;
    157       std::string func_name = backtrace_map->GetFunctionName(stack_data[i], &offset);
    158       if (!func_name.empty()) {
    159         line += " (" + func_name;
    160         if (offset) {
    161           line += StringPrintf("+%" PRIu64, offset);
    162         }
    163         line += ')';
    164       }
    165     }
    166     _LOG(log, logtype::STACK, "%s\n", line.c_str());
    167 
    168     *sp += sizeof(word_t);
    169   }
    170 }
    171 
    172 static void dump_stack(log_t* log, BacktraceMap* backtrace_map, Memory* process_memory,
    173                        std::vector<backtrace_frame_data_t>& frames) {
    174   size_t first = 0, last;
    175   for (size_t i = 0; i < frames.size(); i++) {
    176     const backtrace_frame_data_t& frame = frames[i];
    177     if (frame.sp) {
    178       if (!first) {
    179         first = i+1;
    180       }
    181       last = i;
    182     }
    183   }
    184 
    185   if (!first) {
    186     return;
    187   }
    188   first--;
    189 
    190   // Dump a few words before the first frame.
    191   uint64_t sp = frames[first].sp - STACK_WORDS * sizeof(word_t);
    192   dump_stack_segment(log, backtrace_map, process_memory, &sp, STACK_WORDS, -1);
    193 
    194   // Dump a few words from all successive frames.
    195   // Only log the first 3 frames, put the rest in the tombstone.
    196   for (size_t i = first; i <= last; i++) {
    197     const backtrace_frame_data_t* frame = &frames[i];
    198     if (sp != frame->sp) {
    199       _LOG(log, logtype::STACK, "         ........  ........\n");
    200       sp = frame->sp;
    201     }
    202     if (i == last) {
    203       dump_stack_segment(log, backtrace_map, process_memory, &sp, STACK_WORDS, i);
    204       if (sp < frame->sp + frame->stack_size) {
    205         _LOG(log, logtype::STACK, "         ........  ........\n");
    206       }
    207     } else {
    208       size_t words = frame->stack_size / sizeof(word_t);
    209       if (words == 0) {
    210         words = 1;
    211       } else if (words > STACK_WORDS) {
    212         words = STACK_WORDS;
    213       }
    214       dump_stack_segment(log, backtrace_map, process_memory, &sp, words, i);
    215     }
    216   }
    217 }
    218 
    219 static std::string get_addr_string(uint64_t addr) {
    220   std::string addr_str;
    221 #if defined(__LP64__)
    222   addr_str = StringPrintf("%08x'%08x",
    223                           static_cast<uint32_t>(addr >> 32),
    224                           static_cast<uint32_t>(addr & 0xffffffff));
    225 #else
    226   addr_str = StringPrintf("%08x", static_cast<uint32_t>(addr));
    227 #endif
    228   return addr_str;
    229 }
    230 
    231 static void dump_abort_message(log_t* log, Memory* process_memory, uint64_t address) {
    232   if (address == 0) {
    233     return;
    234   }
    235 
    236   size_t length;
    237   if (!process_memory->ReadFully(address, &length, sizeof(length))) {
    238     _LOG(log, logtype::HEADER, "Failed to read abort message header: %s\n", strerror(errno));
    239     return;
    240   }
    241 
    242   char msg[512];
    243   if (length >= sizeof(msg)) {
    244     _LOG(log, logtype::HEADER, "Abort message too long: claimed length = %zd\n", length);
    245     return;
    246   }
    247 
    248   if (!process_memory->ReadFully(address + sizeof(length), msg, length)) {
    249     _LOG(log, logtype::HEADER, "Failed to read abort message: %s\n", strerror(errno));
    250     return;
    251   }
    252 
    253   msg[length] = '\0';
    254   _LOG(log, logtype::HEADER, "Abort message: '%s'\n", msg);
    255 }
    256 
    257 static void dump_all_maps(log_t* log, BacktraceMap* map, Memory* process_memory, uint64_t addr) {
    258   bool print_fault_address_marker = addr;
    259 
    260   ScopedBacktraceMapIteratorLock lock(map);
    261   _LOG(log, logtype::MAPS,
    262        "\n"
    263        "memory map (%zu entr%s):",
    264        map->size(), map->size() == 1 ? "y" : "ies");
    265   if (print_fault_address_marker) {
    266     if (map->begin() != map->end() && addr < (*map->begin())->start) {
    267       _LOG(log, logtype::MAPS, "\n--->Fault address falls at %s before any mapped regions\n",
    268            get_addr_string(addr).c_str());
    269       print_fault_address_marker = false;
    270     } else {
    271       _LOG(log, logtype::MAPS, " (fault address prefixed with --->)\n");
    272     }
    273   } else {
    274     _LOG(log, logtype::MAPS, "\n");
    275   }
    276 
    277   std::string line;
    278   for (auto it = map->begin(); it != map->end(); ++it) {
    279     const backtrace_map_t* entry = *it;
    280     line = "    ";
    281     if (print_fault_address_marker) {
    282       if (addr < entry->start) {
    283         _LOG(log, logtype::MAPS, "--->Fault address falls at %s between mapped regions\n",
    284              get_addr_string(addr).c_str());
    285         print_fault_address_marker = false;
    286       } else if (addr >= entry->start && addr < entry->end) {
    287         line = "--->";
    288         print_fault_address_marker = false;
    289       }
    290     }
    291     line += get_addr_string(entry->start) + '-' + get_addr_string(entry->end - 1) + ' ';
    292     if (entry->flags & PROT_READ) {
    293       line += 'r';
    294     } else {
    295       line += '-';
    296     }
    297     if (entry->flags & PROT_WRITE) {
    298       line += 'w';
    299     } else {
    300       line += '-';
    301     }
    302     if (entry->flags & PROT_EXEC) {
    303       line += 'x';
    304     } else {
    305       line += '-';
    306     }
    307     line += StringPrintf("  %8" PRIx64 "  %8" PRIx64, entry->offset, entry->end - entry->start);
    308     bool space_needed = true;
    309     if (entry->name.length() > 0) {
    310       space_needed = false;
    311       line += "  " + entry->name;
    312       std::string build_id;
    313       if ((entry->flags & PROT_READ) && elf_get_build_id(process_memory, entry->start, &build_id)) {
    314         line += " (BuildId: " + build_id + ")";
    315       }
    316     }
    317     if (entry->load_bias != 0) {
    318       if (space_needed) {
    319         line += ' ';
    320       }
    321       line += StringPrintf(" (load bias 0x%" PRIx64 ")", entry->load_bias);
    322     }
    323     _LOG(log, logtype::MAPS, "%s\n", line.c_str());
    324   }
    325   if (print_fault_address_marker) {
    326     _LOG(log, logtype::MAPS, "--->Fault address falls at %s after any mapped regions\n",
    327          get_addr_string(addr).c_str());
    328   }
    329 }
    330 
    331 void dump_backtrace(log_t* log, std::vector<backtrace_frame_data_t>& frames, const char* prefix) {
    332   for (auto& frame : frames) {
    333     _LOG(log, logtype::BACKTRACE, "%s%s\n", prefix, Backtrace::FormatFrameData(&frame).c_str());
    334   }
    335 }
    336 
    337 static void print_register_row(log_t* log,
    338                                const std::vector<std::pair<std::string, uint64_t>>& registers) {
    339   std::string output;
    340   for (auto& [name, value] : registers) {
    341     output += android::base::StringPrintf("  %-3s %0*" PRIx64, name.c_str(),
    342                                           static_cast<int>(2 * sizeof(void*)),
    343                                           static_cast<uint64_t>(value));
    344   }
    345 
    346   _LOG(log, logtype::REGISTERS, "  %s\n", output.c_str());
    347 }
    348 
    349 void dump_registers(log_t* log, Regs* regs) {
    350   // Split lr/sp/pc into their own special row.
    351   static constexpr size_t column_count = 4;
    352   std::vector<std::pair<std::string, uint64_t>> current_row;
    353   std::vector<std::pair<std::string, uint64_t>> special_row;
    354 
    355 #if defined(__arm__) || defined(__aarch64__)
    356   static constexpr const char* special_registers[] = {"ip", "lr", "sp", "pc"};
    357 #elif defined(__i386__)
    358   static constexpr const char* special_registers[] = {"ebp", "esp", "eip"};
    359 #elif defined(__x86_64__)
    360   static constexpr const char* special_registers[] = {"rbp", "rsp", "rip"};
    361 #else
    362   static constexpr const char* special_registers[] = {};
    363 #endif
    364 
    365   regs->IterateRegisters([log, &current_row, &special_row](const char* name, uint64_t value) {
    366     auto row = &current_row;
    367     for (const char* special_name : special_registers) {
    368       if (strcmp(special_name, name) == 0) {
    369         row = &special_row;
    370         break;
    371       }
    372     }
    373 
    374     row->emplace_back(name, value);
    375     if (current_row.size() == column_count) {
    376       print_register_row(log, current_row);
    377       current_row.clear();
    378     }
    379   });
    380 
    381   if (!current_row.empty()) {
    382     print_register_row(log, current_row);
    383   }
    384 
    385   print_register_row(log, special_row);
    386 }
    387 
    388 void dump_memory_and_code(log_t* log, BacktraceMap* map, Memory* memory, Regs* regs) {
    389   regs->IterateRegisters([log, map, memory](const char* reg_name, uint64_t reg_value) {
    390     std::string label{"memory near "s + reg_name};
    391     if (map) {
    392       backtrace_map_t map_info;
    393       map->FillIn(reg_value, &map_info);
    394       std::string map_name{map_info.Name()};
    395       if (!map_name.empty()) label += " (" + map_info.Name() + ")";
    396     }
    397     dump_memory(log, memory, reg_value, label);
    398   });
    399 }
    400 
    401 static bool dump_thread(log_t* log, BacktraceMap* map, Memory* process_memory,
    402                         const ThreadInfo& thread_info, uint64_t abort_msg_address,
    403                         bool primary_thread) {
    404   UNUSED(process_memory);
    405   log->current_tid = thread_info.tid;
    406   if (!primary_thread) {
    407     _LOG(log, logtype::THREAD, "--- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---\n");
    408   }
    409   dump_thread_info(log, thread_info);
    410 
    411   if (thread_info.siginfo) {
    412     dump_signal_info(log, thread_info.siginfo);
    413   }
    414 
    415   if (primary_thread) {
    416     dump_abort_message(log, process_memory, abort_msg_address);
    417   }
    418 
    419   dump_registers(log, thread_info.registers.get());
    420 
    421   // Unwind will mutate the registers, so make a copy first.
    422   std::unique_ptr<Regs> regs_copy(thread_info.registers->Clone());
    423   std::vector<backtrace_frame_data_t> frames;
    424   if (!Backtrace::Unwind(regs_copy.get(), map, &frames, 0, nullptr)) {
    425     _LOG(log, logtype::THREAD, "Failed to unwind");
    426     return false;
    427   }
    428 
    429   if (!frames.empty()) {
    430     _LOG(log, logtype::BACKTRACE, "\nbacktrace:\n");
    431     dump_backtrace(log, frames, "    ");
    432 
    433     _LOG(log, logtype::STACK, "\nstack:\n");
    434     dump_stack(log, map, process_memory, frames);
    435   }
    436 
    437   if (primary_thread) {
    438     dump_memory_and_code(log, map, process_memory, thread_info.registers.get());
    439     if (map) {
    440       uint64_t addr = 0;
    441       siginfo_t* si = thread_info.siginfo;
    442       if (signal_has_si_addr(si->si_signo, si->si_code)) {
    443         addr = reinterpret_cast<uint64_t>(si->si_addr);
    444       }
    445       dump_all_maps(log, map, process_memory, addr);
    446     }
    447   }
    448 
    449   log->current_tid = log->crashed_tid;
    450   return true;
    451 }
    452 
    453 // Reads the contents of the specified log device, filters out the entries
    454 // that don't match the specified pid, and writes them to the tombstone file.
    455 //
    456 // If "tail" is non-zero, log the last "tail" number of lines.
    457 static EventTagMap* g_eventTagMap = NULL;
    458 
    459 static void dump_log_file(log_t* log, pid_t pid, const char* filename, unsigned int tail) {
    460   bool first = true;
    461   struct logger_list* logger_list;
    462 
    463   if (!log->should_retrieve_logcat) {
    464     return;
    465   }
    466 
    467   logger_list = android_logger_list_open(
    468       android_name_to_log_id(filename), ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK, tail, pid);
    469 
    470   if (!logger_list) {
    471     ALOGE("Unable to open %s: %s\n", filename, strerror(errno));
    472     return;
    473   }
    474 
    475   struct log_msg log_entry;
    476 
    477   while (true) {
    478     ssize_t actual = android_logger_list_read(logger_list, &log_entry);
    479     struct logger_entry* entry;
    480 
    481     if (actual < 0) {
    482       if (actual == -EINTR) {
    483         // interrupted by signal, retry
    484         continue;
    485       } else if (actual == -EAGAIN) {
    486         // non-blocking EOF; we're done
    487         break;
    488       } else {
    489         ALOGE("Error while reading log: %s\n", strerror(-actual));
    490         break;
    491       }
    492     } else if (actual == 0) {
    493       ALOGE("Got zero bytes while reading log: %s\n", strerror(errno));
    494       break;
    495     }
    496 
    497     // NOTE: if you ALOGV something here, this will spin forever,
    498     // because you will be writing as fast as you're reading.  Any
    499     // high-frequency debug diagnostics should just be written to
    500     // the tombstone file.
    501 
    502     entry = &log_entry.entry_v1;
    503 
    504     if (first) {
    505       _LOG(log, logtype::LOGS, "--------- %slog %s\n",
    506         tail ? "tail end of " : "", filename);
    507       first = false;
    508     }
    509 
    510     // Msg format is: <priority:1><tag:N>\0<message:N>\0
    511     //
    512     // We want to display it in the same format as "logcat -v threadtime"
    513     // (although in this case the pid is redundant).
    514     static const char* kPrioChars = "!.VDIWEFS";
    515     unsigned hdr_size = log_entry.entry.hdr_size;
    516     if (!hdr_size) {
    517       hdr_size = sizeof(log_entry.entry_v1);
    518     }
    519     if ((hdr_size < sizeof(log_entry.entry_v1)) ||
    520         (hdr_size > sizeof(log_entry.entry))) {
    521       continue;
    522     }
    523     char* msg = reinterpret_cast<char*>(log_entry.buf) + hdr_size;
    524 
    525     char timeBuf[32];
    526     time_t sec = static_cast<time_t>(entry->sec);
    527     struct tm tmBuf;
    528     struct tm* ptm;
    529     ptm = localtime_r(&sec, &tmBuf);
    530     strftime(timeBuf, sizeof(timeBuf), "%m-%d %H:%M:%S", ptm);
    531 
    532     if (log_entry.id() == LOG_ID_EVENTS) {
    533       if (!g_eventTagMap) {
    534         g_eventTagMap = android_openEventTagMap(NULL);
    535       }
    536       AndroidLogEntry e;
    537       char buf[512];
    538       android_log_processBinaryLogBuffer(entry, &e, g_eventTagMap, buf, sizeof(buf));
    539       _LOG(log, logtype::LOGS, "%s.%03d %5d %5d %c %-8.*s: %s\n",
    540          timeBuf, entry->nsec / 1000000, entry->pid, entry->tid,
    541          'I', (int)e.tagLen, e.tag, e.message);
    542       continue;
    543     }
    544 
    545     unsigned char prio = msg[0];
    546     char* tag = msg + 1;
    547     msg = tag + strlen(tag) + 1;
    548 
    549     // consume any trailing newlines
    550     char* nl = msg + strlen(msg) - 1;
    551     while (nl >= msg && *nl == '\n') {
    552       *nl-- = '\0';
    553     }
    554 
    555     char prioChar = (prio < strlen(kPrioChars) ? kPrioChars[prio] : '?');
    556 
    557     // Look for line breaks ('\n') and display each text line
    558     // on a separate line, prefixed with the header, like logcat does.
    559     do {
    560       nl = strchr(msg, '\n');
    561       if (nl) {
    562         *nl = '\0';
    563         ++nl;
    564       }
    565 
    566       _LOG(log, logtype::LOGS, "%s.%03d %5d %5d %c %-8s: %s\n",
    567          timeBuf, entry->nsec / 1000000, entry->pid, entry->tid,
    568          prioChar, tag, msg);
    569     } while ((msg = nl));
    570   }
    571 
    572   android_logger_list_free(logger_list);
    573 }
    574 
    575 // Dumps the logs generated by the specified pid to the tombstone, from both
    576 // "system" and "main" log devices.  Ideally we'd interleave the output.
    577 static void dump_logs(log_t* log, pid_t pid, unsigned int tail) {
    578   if (pid == getpid()) {
    579     // Cowardly refuse to dump logs while we're running in-process.
    580     return;
    581   }
    582 
    583   dump_log_file(log, pid, "system", tail);
    584   dump_log_file(log, pid, "main", tail);
    585 }
    586 
    587 void engrave_tombstone_ucontext(int tombstone_fd, uint64_t abort_msg_address, siginfo_t* siginfo,
    588                                 ucontext_t* ucontext) {
    589   pid_t pid = getpid();
    590   pid_t tid = gettid();
    591 
    592   log_t log;
    593   log.current_tid = tid;
    594   log.crashed_tid = tid;
    595   log.tfd = tombstone_fd;
    596   log.amfd_data = nullptr;
    597 
    598   char thread_name[16];
    599   char process_name[128];
    600 
    601   read_with_default("/proc/self/comm", thread_name, sizeof(thread_name), "<unknown>");
    602   read_with_default("/proc/self/cmdline", process_name, sizeof(process_name), "<unknown>");
    603 
    604   std::unique_ptr<Regs> regs(Regs::CreateFromUcontext(Regs::CurrentArch(), ucontext));
    605 
    606   std::map<pid_t, ThreadInfo> threads;
    607   threads[gettid()] = ThreadInfo{
    608       .registers = std::move(regs),
    609       .tid = tid,
    610       .thread_name = thread_name,
    611       .pid = pid,
    612       .process_name = process_name,
    613       .siginfo = siginfo,
    614   };
    615 
    616   std::unique_ptr<BacktraceMap> backtrace_map(BacktraceMap::Create(getpid(), false));
    617   if (!backtrace_map) {
    618     ALOGE("failed to create backtrace map");
    619     _exit(1);
    620   }
    621 
    622   std::shared_ptr<Memory> process_memory = backtrace_map->GetProcessMemory();
    623   engrave_tombstone(unique_fd(dup(tombstone_fd)), backtrace_map.get(), process_memory.get(),
    624                     threads, tid, abort_msg_address, nullptr, nullptr);
    625 }
    626 
    627 void engrave_tombstone(unique_fd output_fd, BacktraceMap* map, Memory* process_memory,
    628                        const std::map<pid_t, ThreadInfo>& threads, pid_t target_thread,
    629                        uint64_t abort_msg_address, OpenFilesList* open_files,
    630                        std::string* amfd_data) {
    631   // don't copy log messages to tombstone unless this is a dev device
    632   bool want_logs = android::base::GetBoolProperty("ro.debuggable", false);
    633 
    634   log_t log;
    635   log.current_tid = target_thread;
    636   log.crashed_tid = target_thread;
    637   log.tfd = output_fd.get();
    638   log.amfd_data = amfd_data;
    639 
    640   _LOG(&log, logtype::HEADER, "*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***\n");
    641   dump_header_info(&log);
    642 
    643   auto it = threads.find(target_thread);
    644   if (it == threads.end()) {
    645     LOG(FATAL) << "failed to find target thread";
    646   }
    647   dump_thread(&log, map, process_memory, it->second, abort_msg_address, true);
    648 
    649   if (want_logs) {
    650     dump_logs(&log, it->second.pid, 50);
    651   }
    652 
    653   for (auto& [tid, thread_info] : threads) {
    654     if (tid == target_thread) {
    655       continue;
    656     }
    657 
    658     dump_thread(&log, map, process_memory, thread_info, 0, false);
    659   }
    660 
    661   if (open_files) {
    662     _LOG(&log, logtype::OPEN_FILES, "\nopen files:\n");
    663     dump_open_files_list(&log, *open_files, "    ");
    664   }
    665 
    666   if (want_logs) {
    667     dump_logs(&log, it->second.pid, 0);
    668   }
    669 }
    670