Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 1 | /* |
Mark Salyzyn | fca0bd1 | 2013-12-12 12:21:20 -0800 | [diff] [blame] | 2 | * Copyright (C) 2012-2014 The Android Open Source Project |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 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 | |
Brigid Smith | 62ba489 | 2014-06-10 11:53:08 -0700 | [diff] [blame] | 17 | #define LOG_TAG "DEBUG" |
| 18 | |
Josh Gao | c370666 | 2017-08-29 13:08:32 -0700 | [diff] [blame] | 19 | #include "libdebuggerd/tombstone.h" |
| 20 | |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 21 | #include <dirent.h> |
Kévin PETIT | 4bb4772 | 2013-12-18 16:44:24 +0000 | [diff] [blame] | 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> |
Christopher Ferris | 3336c7b | 2019-04-08 17:18:24 -0700 | [diff] [blame] | 30 | #include <sys/mman.h> |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 31 | #include <sys/ptrace.h> |
| 32 | #include <sys/stat.h> |
Mark Salyzyn | ff2dcd9 | 2016-09-28 15:54:45 -0700 | [diff] [blame] | 33 | #include <time.h> |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 34 | |
Christopher Ferris | 6e96403 | 2015-05-15 17:30:21 -0700 | [diff] [blame] | 35 | #include <memory> |
| 36 | #include <string> |
| 37 | |
Josh Gao | 57f58f8 | 2017-03-15 23:23:22 -0700 | [diff] [blame] | 38 | #include <android-base/file.h> |
Christopher Ferris | 2b98c82 | 2021-09-28 15:50:04 -0700 | [diff] [blame] | 39 | #include <android-base/logging.h> |
Tom Cherry | 31121ca | 2017-10-10 13:30:57 -0700 | [diff] [blame] | 40 | #include <android-base/properties.h> |
Elliott Hughes | 4f71319 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 41 | #include <android-base/stringprintf.h> |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 42 | #include <android-base/strings.h> |
Josh Gao | 57f58f8 | 2017-03-15 23:23:22 -0700 | [diff] [blame] | 43 | #include <android-base/unique_fd.h> |
| 44 | #include <android/log.h> |
Josh Gao | 618cea3 | 2021-01-26 17:45:43 -0800 | [diff] [blame] | 45 | #include <async_safe/log.h> |
Peter Collingbourne | 0ea08c2 | 2021-02-05 14:59:08 -0800 | [diff] [blame] | 46 | #include <bionic/macros.h> |
Mark Salyzyn | cfd5b08 | 2016-10-17 14:28:00 -0700 | [diff] [blame] | 47 | #include <log/log.h> |
Tom Cherry | c5c6d7d | 2020-04-17 09:38:55 -0700 | [diff] [blame] | 48 | #include <log/log_read.h> |
Mark Salyzyn | ff2dcd9 | 2016-09-28 15:54:45 -0700 | [diff] [blame] | 49 | #include <log/logprint.h> |
| 50 | #include <private/android_filesystem_config.h> |
David Srbecky | b9cc4fb | 2019-04-05 18:23:32 +0000 | [diff] [blame] | 51 | #include <unwindstack/DexFiles.h> |
Christopher Ferris | 60eb197 | 2019-01-15 15:18:43 -0800 | [diff] [blame] | 52 | #include <unwindstack/JitDebug.h> |
| 53 | #include <unwindstack/Maps.h> |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 54 | #include <unwindstack/Memory.h> |
| 55 | #include <unwindstack/Regs.h> |
Christopher Ferris | 60eb197 | 2019-01-15 15:18:43 -0800 | [diff] [blame] | 56 | #include <unwindstack/Unwinder.h> |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 57 | |
Josh Gao | c370666 | 2017-08-29 13:08:32 -0700 | [diff] [blame] | 58 | #include "libdebuggerd/backtrace.h" |
Mitch Phillips | e0b4bb1 | 2020-02-14 14:54:31 -0800 | [diff] [blame] | 59 | #include "libdebuggerd/gwp_asan.h" |
Josh Gao | c370666 | 2017-08-29 13:08:32 -0700 | [diff] [blame] | 60 | #include "libdebuggerd/open_files_list.h" |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 61 | #include "libdebuggerd/utility.h" |
Elliott Hughes | a660cb3 | 2020-07-23 15:26:10 -0700 | [diff] [blame] | 62 | #include "util.h" |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 63 | |
Christopher Ferris | 7aad256 | 2021-09-24 00:06:38 -0700 | [diff] [blame] | 64 | #if defined(USE_SCUDO) |
| 65 | #include "libdebuggerd/scudo.h" |
| 66 | #endif |
| 67 | |
Mitch Phillips | e0b4bb1 | 2020-02-14 14:54:31 -0800 | [diff] [blame] | 68 | #include "gwp_asan/common.h" |
| 69 | #include "gwp_asan/crash_handler.h" |
| 70 | |
Josh Gao | 76e1e30 | 2021-01-26 15:53:11 -0800 | [diff] [blame] | 71 | #include "tombstone.pb.h" |
| 72 | |
Tom Cherry | 31121ca | 2017-10-10 13:30:57 -0700 | [diff] [blame] | 73 | using android::base::GetBoolProperty; |
| 74 | using android::base::GetProperty; |
Elliott Hughes | 0ba5359 | 2017-02-01 16:59:15 -0800 | [diff] [blame] | 75 | using android::base::StringPrintf; |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 76 | using android::base::unique_fd; |
| 77 | |
Elliott Hughes | e1415a5 | 2018-02-15 09:18:21 -0800 | [diff] [blame] | 78 | using namespace std::literals::string_literals; |
| 79 | |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 80 | #define STACK_WORDS 16 |
| 81 | |
Brigid Smith | 9c8dacc | 2014-06-02 15:02:20 -0700 | [diff] [blame] | 82 | static void dump_header_info(log_t* log) { |
Tom Cherry | 31121ca | 2017-10-10 13:30:57 -0700 | [diff] [blame] | 83 | auto fingerprint = GetProperty("ro.build.fingerprint", "unknown"); |
| 84 | auto revision = GetProperty("ro.revision", "unknown"); |
Ben Cheng | d7760c1 | 2012-09-19 16:04:01 -0700 | [diff] [blame] | 85 | |
Tom Cherry | 31121ca | 2017-10-10 13:30:57 -0700 | [diff] [blame] | 86 | _LOG(log, logtype::HEADER, "Build fingerprint: '%s'\n", fingerprint.c_str()); |
| 87 | _LOG(log, logtype::HEADER, "Revision: '%s'\n", revision.c_str()); |
Brigid Smith | 62ba489 | 2014-06-10 11:53:08 -0700 | [diff] [blame] | 88 | _LOG(log, logtype::HEADER, "ABI: '%s'\n", ABI_STRING); |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Christopher Ferris | 481e837 | 2019-07-15 17:13:24 -0700 | [diff] [blame] | 91 | static std::string get_stack_overflow_cause(uint64_t fault_addr, uint64_t sp, |
| 92 | unwindstack::Maps* maps) { |
| 93 | static constexpr uint64_t kMaxDifferenceBytes = 256; |
| 94 | uint64_t difference; |
| 95 | if (sp >= fault_addr) { |
| 96 | difference = sp - fault_addr; |
| 97 | } else { |
| 98 | difference = fault_addr - sp; |
| 99 | } |
| 100 | if (difference <= kMaxDifferenceBytes) { |
| 101 | // The faulting address is close to the current sp, check if the sp |
| 102 | // indicates a stack overflow. |
| 103 | // On arm, the sp does not get updated when the instruction faults. |
| 104 | // In this case, the sp will still be in a valid map, which is the |
| 105 | // last case below. |
| 106 | // On aarch64, the sp does get updated when the instruction faults. |
| 107 | // In this case, the sp will be in either an invalid map if triggered |
| 108 | // on the main thread, or in a guard map if in another thread, which |
| 109 | // will be the first case or second case from below. |
Christopher Ferris | c677606 | 2021-01-15 15:59:28 -0800 | [diff] [blame^] | 110 | auto map_info = maps->Find(sp); |
Christopher Ferris | 481e837 | 2019-07-15 17:13:24 -0700 | [diff] [blame] | 111 | if (map_info == nullptr) { |
| 112 | return "stack pointer is in a non-existent map; likely due to stack overflow."; |
David Srbecky | d8ab48b | 2021-05-13 00:03:26 +0100 | [diff] [blame] | 113 | } else if ((map_info->flags() & (PROT_READ | PROT_WRITE)) != (PROT_READ | PROT_WRITE)) { |
Christopher Ferris | 481e837 | 2019-07-15 17:13:24 -0700 | [diff] [blame] | 114 | return "stack pointer is not in a rw map; likely due to stack overflow."; |
David Srbecky | d8ab48b | 2021-05-13 00:03:26 +0100 | [diff] [blame] | 115 | } else if ((sp - map_info->start()) <= kMaxDifferenceBytes) { |
Christopher Ferris | 481e837 | 2019-07-15 17:13:24 -0700 | [diff] [blame] | 116 | return "stack pointer is close to top of stack; likely stack overflow."; |
| 117 | } |
| 118 | } |
| 119 | return ""; |
| 120 | } |
| 121 | |
Christopher Ferris | 7aad256 | 2021-09-24 00:06:38 -0700 | [diff] [blame] | 122 | static void dump_probable_cause(log_t* log, unwindstack::Unwinder* unwinder, |
| 123 | const ProcessInfo& process_info, const ThreadInfo& main_thread) { |
| 124 | #if defined(USE_SCUDO) |
| 125 | ScudoCrashData scudo_crash_data(unwinder->GetProcessMemory().get(), process_info); |
| 126 | if (scudo_crash_data.CrashIsMine()) { |
| 127 | scudo_crash_data.DumpCause(log, unwinder); |
| 128 | return; |
| 129 | } |
| 130 | #endif |
| 131 | |
| 132 | GwpAsanCrashData gwp_asan_crash_data(unwinder->GetProcessMemory().get(), process_info, |
| 133 | main_thread); |
| 134 | if (gwp_asan_crash_data.CrashIsMine()) { |
| 135 | gwp_asan_crash_data.DumpCause(log); |
| 136 | return; |
| 137 | } |
| 138 | |
| 139 | unwindstack::Maps* maps = unwinder->GetMaps(); |
| 140 | unwindstack::Regs* regs = main_thread.registers.get(); |
| 141 | const siginfo_t* si = main_thread.siginfo; |
Elliott Hughes | 0ba5359 | 2017-02-01 16:59:15 -0800 | [diff] [blame] | 142 | std::string cause; |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 143 | if (si->si_signo == SIGSEGV && si->si_code == SEGV_MAPERR) { |
| 144 | if (si->si_addr < reinterpret_cast<void*>(4096)) { |
Elliott Hughes | 0ba5359 | 2017-02-01 16:59:15 -0800 | [diff] [blame] | 145 | cause = StringPrintf("null pointer dereference"); |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 146 | } else if (si->si_addr == reinterpret_cast<void*>(0xffff0ffc)) { |
Elliott Hughes | 0ba5359 | 2017-02-01 16:59:15 -0800 | [diff] [blame] | 147 | cause = "call to kuser_helper_version"; |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 148 | } else if (si->si_addr == reinterpret_cast<void*>(0xffff0fe0)) { |
Elliott Hughes | 0ba5359 | 2017-02-01 16:59:15 -0800 | [diff] [blame] | 149 | cause = "call to kuser_get_tls"; |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 150 | } else if (si->si_addr == reinterpret_cast<void*>(0xffff0fc0)) { |
Elliott Hughes | 0ba5359 | 2017-02-01 16:59:15 -0800 | [diff] [blame] | 151 | cause = "call to kuser_cmpxchg"; |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 152 | } else if (si->si_addr == reinterpret_cast<void*>(0xffff0fa0)) { |
Elliott Hughes | 0ba5359 | 2017-02-01 16:59:15 -0800 | [diff] [blame] | 153 | cause = "call to kuser_memory_barrier"; |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 154 | } else if (si->si_addr == reinterpret_cast<void*>(0xffff0f60)) { |
Elliott Hughes | 0ba5359 | 2017-02-01 16:59:15 -0800 | [diff] [blame] | 155 | cause = "call to kuser_cmpxchg64"; |
Christopher Ferris | 481e837 | 2019-07-15 17:13:24 -0700 | [diff] [blame] | 156 | } else { |
| 157 | cause = get_stack_overflow_cause(reinterpret_cast<uint64_t>(si->si_addr), regs->sp(), maps); |
Elliott Hughes | 0ba5359 | 2017-02-01 16:59:15 -0800 | [diff] [blame] | 158 | } |
Ivan Lozano | df3cec9 | 2018-11-19 10:43:47 -0800 | [diff] [blame] | 159 | } else if (si->si_signo == SIGSEGV && si->si_code == SEGV_ACCERR) { |
Christopher Ferris | 481e837 | 2019-07-15 17:13:24 -0700 | [diff] [blame] | 160 | uint64_t fault_addr = reinterpret_cast<uint64_t>(si->si_addr); |
Christopher Ferris | c677606 | 2021-01-15 15:59:28 -0800 | [diff] [blame^] | 161 | auto map_info = maps->Find(fault_addr); |
David Srbecky | d8ab48b | 2021-05-13 00:03:26 +0100 | [diff] [blame] | 162 | if (map_info != nullptr && map_info->flags() == PROT_EXEC) { |
Christopher Ferris | 60eb197 | 2019-01-15 15:18:43 -0800 | [diff] [blame] | 163 | cause = "execute-only (no-read) memory access error; likely due to data in .text."; |
Christopher Ferris | 481e837 | 2019-07-15 17:13:24 -0700 | [diff] [blame] | 164 | } else { |
| 165 | cause = get_stack_overflow_cause(fault_addr, regs->sp(), maps); |
Ivan Lozano | df3cec9 | 2018-11-19 10:43:47 -0800 | [diff] [blame] | 166 | } |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 167 | } else if (si->si_signo == SIGSYS && si->si_code == SYS_SECCOMP) { |
| 168 | cause = StringPrintf("seccomp prevented call to disallowed %s system call %d", ABI_STRING, |
| 169 | si->si_syscall); |
Elliott Hughes | 0ba5359 | 2017-02-01 16:59:15 -0800 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | if (!cause.empty()) _LOG(log, logtype::HEADER, "Cause: %s\n", cause.c_str()); |
| 173 | } |
| 174 | |
Christopher Ferris | 60eb197 | 2019-01-15 15:18:43 -0800 | [diff] [blame] | 175 | static void dump_signal_info(log_t* log, const ThreadInfo& thread_info, |
Peter Collingbourne | f03af88 | 2020-03-20 18:09:00 -0700 | [diff] [blame] | 176 | const ProcessInfo& process_info, unwindstack::Memory* process_memory) { |
Elliott Hughes | 2baf443 | 2018-05-30 12:55:04 -0700 | [diff] [blame] | 177 | char addr_desc[64]; // ", fault addr 0x1234" |
Peter Collingbourne | f03af88 | 2020-03-20 18:09:00 -0700 | [diff] [blame] | 178 | if (process_info.has_fault_address) { |
Mitch Phillips | e4adff0 | 2021-01-21 20:41:50 -0800 | [diff] [blame] | 179 | // SIGILL faults will never have tagged addresses, so okay to |
| 180 | // indiscriminately use the tagged address here. |
| 181 | size_t addr = process_info.maybe_tagged_fault_address; |
Elliott Hughes | 2baf443 | 2018-05-30 12:55:04 -0700 | [diff] [blame] | 182 | if (thread_info.siginfo->si_signo == SIGILL) { |
| 183 | uint32_t instruction = {}; |
Peter Collingbourne | f03af88 | 2020-03-20 18:09:00 -0700 | [diff] [blame] | 184 | process_memory->Read(addr, &instruction, sizeof(instruction)); |
| 185 | snprintf(addr_desc, sizeof(addr_desc), "0x%zx (*pc=%#08x)", addr, instruction); |
Elliott Hughes | 2baf443 | 2018-05-30 12:55:04 -0700 | [diff] [blame] | 186 | } else { |
Peter Collingbourne | f03af88 | 2020-03-20 18:09:00 -0700 | [diff] [blame] | 187 | snprintf(addr_desc, sizeof(addr_desc), "0x%zx", addr); |
Elliott Hughes | 2baf443 | 2018-05-30 12:55:04 -0700 | [diff] [blame] | 188 | } |
Elliott Hughes | 855fcc3 | 2014-04-25 16:05:34 -0700 | [diff] [blame] | 189 | } else { |
| 190 | snprintf(addr_desc, sizeof(addr_desc), "--------"); |
| 191 | } |
| 192 | |
Elliott Hughes | 70d8f28 | 2018-04-25 17:00:14 -0700 | [diff] [blame] | 193 | char sender_desc[32] = {}; // " from pid 1234, uid 666" |
| 194 | if (signal_has_sender(thread_info.siginfo, thread_info.pid)) { |
| 195 | get_signal_sender(sender_desc, sizeof(sender_desc), thread_info.siginfo); |
| 196 | } |
Elliott Hughes | 0ba5359 | 2017-02-01 16:59:15 -0800 | [diff] [blame] | 197 | |
Elliott Hughes | 70d8f28 | 2018-04-25 17:00:14 -0700 | [diff] [blame] | 198 | _LOG(log, logtype::HEADER, "signal %d (%s), code %d (%s%s), fault addr %s\n", |
| 199 | thread_info.siginfo->si_signo, get_signame(thread_info.siginfo), |
| 200 | thread_info.siginfo->si_code, get_sigcode(thread_info.siginfo), sender_desc, addr_desc); |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 201 | } |
| 202 | |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 203 | static void dump_thread_info(log_t* log, const ThreadInfo& thread_info) { |
Elliott Hughes | 713ec25 | 2020-08-03 08:43:43 -0700 | [diff] [blame] | 204 | // Don't try to collect logs from the threads that implement the logging system itself. |
| 205 | if (thread_info.uid == AID_LOGD) log->should_retrieve_logcat = false; |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 206 | |
Josh Gao | 31348a7 | 2021-03-29 21:53:42 -0700 | [diff] [blame] | 207 | const char* process_name = "<unknown>"; |
| 208 | if (!thread_info.command_line.empty()) { |
| 209 | process_name = thread_info.command_line[0].c_str(); |
| 210 | } |
| 211 | |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 212 | _LOG(log, logtype::HEADER, "pid: %d, tid: %d, name: %s >>> %s <<<\n", thread_info.pid, |
Josh Gao | 31348a7 | 2021-03-29 21:53:42 -0700 | [diff] [blame] | 213 | thread_info.tid, thread_info.thread_name.c_str(), process_name); |
Misha Wagner | 39c5b8c | 2019-04-18 16:07:33 +0100 | [diff] [blame] | 214 | _LOG(log, logtype::HEADER, "uid: %d\n", thread_info.uid); |
Peter Collingbourne | 864f15d | 2020-09-14 20:27:36 -0700 | [diff] [blame] | 215 | if (thread_info.tagged_addr_ctrl != -1) { |
| 216 | _LOG(log, logtype::HEADER, "tagged_addr_ctrl: %016lx\n", thread_info.tagged_addr_ctrl); |
| 217 | } |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 218 | } |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 219 | |
Christopher Ferris | 7937a36 | 2018-01-18 11:15:49 -0800 | [diff] [blame] | 220 | static std::string get_addr_string(uint64_t addr) { |
Christopher Ferris | 862fe02 | 2015-06-02 14:52:44 -0700 | [diff] [blame] | 221 | std::string addr_str; |
| 222 | #if defined(__LP64__) |
Josh Gao | 76e1e30 | 2021-01-26 15:53:11 -0800 | [diff] [blame] | 223 | addr_str = StringPrintf("%08x'%08x", static_cast<uint32_t>(addr >> 32), |
Elliott Hughes | 0ba5359 | 2017-02-01 16:59:15 -0800 | [diff] [blame] | 224 | static_cast<uint32_t>(addr & 0xffffffff)); |
Christopher Ferris | 862fe02 | 2015-06-02 14:52:44 -0700 | [diff] [blame] | 225 | #else |
Christopher Ferris | 7937a36 | 2018-01-18 11:15:49 -0800 | [diff] [blame] | 226 | addr_str = StringPrintf("%08x", static_cast<uint32_t>(addr)); |
Christopher Ferris | 862fe02 | 2015-06-02 14:52:44 -0700 | [diff] [blame] | 227 | #endif |
| 228 | return addr_str; |
| 229 | } |
| 230 | |
Christopher Ferris | 60eb197 | 2019-01-15 15:18:43 -0800 | [diff] [blame] | 231 | static void dump_abort_message(log_t* log, unwindstack::Memory* process_memory, uint64_t address) { |
Josh Gao | 7c89f9e | 2016-01-13 17:57:14 -0800 | [diff] [blame] | 232 | if (address == 0) { |
| 233 | return; |
| 234 | } |
| 235 | |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 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 | } |
Josh Gao | 7c89f9e | 2016-01-13 17:57:14 -0800 | [diff] [blame] | 241 | |
Josh Gao | 1cc7bd8 | 2018-02-13 13:16:17 -0800 | [diff] [blame] | 242 | // The length field includes the length of the length field itself. |
| 243 | if (length < sizeof(size_t)) { |
| 244 | _LOG(log, logtype::HEADER, "Abort message header malformed: claimed length = %zd\n", length); |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 245 | return; |
Josh Gao | 7c89f9e | 2016-01-13 17:57:14 -0800 | [diff] [blame] | 246 | } |
Josh Gao | 7c89f9e | 2016-01-13 17:57:14 -0800 | [diff] [blame] | 247 | |
Josh Gao | 1cc7bd8 | 2018-02-13 13:16:17 -0800 | [diff] [blame] | 248 | length -= sizeof(size_t); |
| 249 | |
Josh Gao | 83b8ac2 | 2018-04-20 17:31:53 -0700 | [diff] [blame] | 250 | // The abort message should be null terminated already, but reserve a spot for NUL just in case. |
| 251 | std::vector<char> msg(length + 1); |
Josh Gao | 1cc7bd8 | 2018-02-13 13:16:17 -0800 | [diff] [blame] | 252 | if (!process_memory->ReadFully(address + sizeof(length), &msg[0], length)) { |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 253 | _LOG(log, logtype::HEADER, "Failed to read abort message: %s\n", strerror(errno)); |
| 254 | return; |
| 255 | } |
| 256 | |
Christopher Ferris | e889145 | 2021-08-17 17:34:53 -0700 | [diff] [blame] | 257 | // Remove any trailing newlines. |
| 258 | size_t index = length; |
| 259 | while (index > 0 && (msg[index - 1] == '\0' || msg[index - 1] == '\n')) { |
| 260 | --index; |
| 261 | } |
| 262 | msg[index] = '\0'; |
Josh Gao | 1cc7bd8 | 2018-02-13 13:16:17 -0800 | [diff] [blame] | 263 | _LOG(log, logtype::HEADER, "Abort message: '%s'\n", &msg[0]); |
Josh Gao | 7c89f9e | 2016-01-13 17:57:14 -0800 | [diff] [blame] | 264 | } |
| 265 | |
Christopher Ferris | 60eb197 | 2019-01-15 15:18:43 -0800 | [diff] [blame] | 266 | static void dump_all_maps(log_t* log, unwindstack::Unwinder* unwinder, uint64_t addr) { |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 267 | bool print_fault_address_marker = addr; |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 268 | |
Christopher Ferris | 60eb197 | 2019-01-15 15:18:43 -0800 | [diff] [blame] | 269 | unwindstack::Maps* maps = unwinder->GetMaps(); |
Elliott Hughes | 868d39a | 2017-09-26 11:54:49 -0700 | [diff] [blame] | 270 | _LOG(log, logtype::MAPS, |
| 271 | "\n" |
Josh Gao | 1ce8e14 | 2017-09-27 13:59:42 -0700 | [diff] [blame] | 272 | "memory map (%zu entr%s):", |
Christopher Ferris | 60eb197 | 2019-01-15 15:18:43 -0800 | [diff] [blame] | 273 | maps->Total(), maps->Total() == 1 ? "y" : "ies"); |
Elliott Hughes | 868d39a | 2017-09-26 11:54:49 -0700 | [diff] [blame] | 274 | if (print_fault_address_marker) { |
David Srbecky | d8ab48b | 2021-05-13 00:03:26 +0100 | [diff] [blame] | 275 | if (maps->Total() != 0 && addr < maps->Get(0)->start()) { |
Josh Gao | 1ce8e14 | 2017-09-27 13:59:42 -0700 | [diff] [blame] | 276 | _LOG(log, logtype::MAPS, "\n--->Fault address falls at %s before any mapped regions\n", |
Christopher Ferris | 862fe02 | 2015-06-02 14:52:44 -0700 | [diff] [blame] | 277 | get_addr_string(addr).c_str()); |
Christopher Ferris | a21bd93 | 2015-02-27 13:39:47 -0800 | [diff] [blame] | 278 | print_fault_address_marker = false; |
Elliott Hughes | 868d39a | 2017-09-26 11:54:49 -0700 | [diff] [blame] | 279 | } else { |
Josh Gao | 1ce8e14 | 2017-09-27 13:59:42 -0700 | [diff] [blame] | 280 | _LOG(log, logtype::MAPS, " (fault address prefixed with --->)\n"); |
Brigid Smith | 8606eaa | 2014-07-07 12:33:50 -0700 | [diff] [blame] | 281 | } |
Josh Gao | 1ce8e14 | 2017-09-27 13:59:42 -0700 | [diff] [blame] | 282 | } else { |
| 283 | _LOG(log, logtype::MAPS, "\n"); |
Brigid Smith | 8606eaa | 2014-07-07 12:33:50 -0700 | [diff] [blame] | 284 | } |
Christopher Ferris | a21bd93 | 2015-02-27 13:39:47 -0800 | [diff] [blame] | 285 | |
Christopher Ferris | 60eb197 | 2019-01-15 15:18:43 -0800 | [diff] [blame] | 286 | std::shared_ptr<unwindstack::Memory>& process_memory = unwinder->GetProcessMemory(); |
| 287 | |
Christopher Ferris | a21bd93 | 2015-02-27 13:39:47 -0800 | [diff] [blame] | 288 | std::string line; |
Florian Mayer | 3d67d34 | 2019-02-27 18:00:37 +0000 | [diff] [blame] | 289 | for (auto const& map_info : *maps) { |
Christopher Ferris | a21bd93 | 2015-02-27 13:39:47 -0800 | [diff] [blame] | 290 | line = " "; |
| 291 | if (print_fault_address_marker) { |
David Srbecky | d8ab48b | 2021-05-13 00:03:26 +0100 | [diff] [blame] | 292 | if (addr < map_info->start()) { |
Christopher Ferris | 862fe02 | 2015-06-02 14:52:44 -0700 | [diff] [blame] | 293 | _LOG(log, logtype::MAPS, "--->Fault address falls at %s between mapped regions\n", |
| 294 | get_addr_string(addr).c_str()); |
Christopher Ferris | a21bd93 | 2015-02-27 13:39:47 -0800 | [diff] [blame] | 295 | print_fault_address_marker = false; |
David Srbecky | d8ab48b | 2021-05-13 00:03:26 +0100 | [diff] [blame] | 296 | } else if (addr >= map_info->start() && addr < map_info->end()) { |
Christopher Ferris | a21bd93 | 2015-02-27 13:39:47 -0800 | [diff] [blame] | 297 | line = "--->"; |
| 298 | print_fault_address_marker = false; |
| 299 | } |
| 300 | } |
David Srbecky | d8ab48b | 2021-05-13 00:03:26 +0100 | [diff] [blame] | 301 | line += get_addr_string(map_info->start()) + '-' + get_addr_string(map_info->end() - 1) + ' '; |
| 302 | if (map_info->flags() & PROT_READ) { |
Christopher Ferris | a21bd93 | 2015-02-27 13:39:47 -0800 | [diff] [blame] | 303 | line += 'r'; |
| 304 | } else { |
| 305 | line += '-'; |
| 306 | } |
David Srbecky | d8ab48b | 2021-05-13 00:03:26 +0100 | [diff] [blame] | 307 | if (map_info->flags() & PROT_WRITE) { |
Christopher Ferris | a21bd93 | 2015-02-27 13:39:47 -0800 | [diff] [blame] | 308 | line += 'w'; |
| 309 | } else { |
| 310 | line += '-'; |
| 311 | } |
David Srbecky | d8ab48b | 2021-05-13 00:03:26 +0100 | [diff] [blame] | 312 | if (map_info->flags() & PROT_EXEC) { |
Christopher Ferris | a21bd93 | 2015-02-27 13:39:47 -0800 | [diff] [blame] | 313 | line += 'x'; |
| 314 | } else { |
| 315 | line += '-'; |
| 316 | } |
David Srbecky | d8ab48b | 2021-05-13 00:03:26 +0100 | [diff] [blame] | 317 | line += StringPrintf(" %8" PRIx64 " %8" PRIx64, map_info->offset(), |
| 318 | map_info->end() - map_info->start()); |
Christopher Ferris | 862fe02 | 2015-06-02 14:52:44 -0700 | [diff] [blame] | 319 | bool space_needed = true; |
David Srbecky | d8ab48b | 2021-05-13 00:03:26 +0100 | [diff] [blame] | 320 | if (!map_info->name().empty()) { |
Christopher Ferris | 862fe02 | 2015-06-02 14:52:44 -0700 | [diff] [blame] | 321 | space_needed = false; |
David Srbecky | d8ab48b | 2021-05-13 00:03:26 +0100 | [diff] [blame] | 322 | line += " " + map_info->name(); |
Christopher Ferris | 60eb197 | 2019-01-15 15:18:43 -0800 | [diff] [blame] | 323 | std::string build_id = map_info->GetPrintableBuildID(); |
| 324 | if (!build_id.empty()) { |
Christopher Ferris | a21bd93 | 2015-02-27 13:39:47 -0800 | [diff] [blame] | 325 | line += " (BuildId: " + build_id + ")"; |
| 326 | } |
| 327 | } |
Christopher Ferris | 60eb197 | 2019-01-15 15:18:43 -0800 | [diff] [blame] | 328 | uint64_t load_bias = map_info->GetLoadBias(process_memory); |
| 329 | if (load_bias != 0) { |
Christopher Ferris | 862fe02 | 2015-06-02 14:52:44 -0700 | [diff] [blame] | 330 | if (space_needed) { |
| 331 | line += ' '; |
| 332 | } |
Christopher Ferris | 60eb197 | 2019-01-15 15:18:43 -0800 | [diff] [blame] | 333 | line += StringPrintf(" (load bias 0x%" PRIx64 ")", load_bias); |
Christopher Ferris | 2106f4b | 2015-05-01 15:02:03 -0700 | [diff] [blame] | 334 | } |
Christopher Ferris | a21bd93 | 2015-02-27 13:39:47 -0800 | [diff] [blame] | 335 | _LOG(log, logtype::MAPS, "%s\n", line.c_str()); |
| 336 | } |
| 337 | if (print_fault_address_marker) { |
Christopher Ferris | 862fe02 | 2015-06-02 14:52:44 -0700 | [diff] [blame] | 338 | _LOG(log, logtype::MAPS, "--->Fault address falls at %s after any mapped regions\n", |
| 339 | get_addr_string(addr).c_str()); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 340 | } |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 341 | } |
| 342 | |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 343 | static void print_register_row(log_t* log, |
| 344 | const std::vector<std::pair<std::string, uint64_t>>& registers) { |
| 345 | std::string output; |
| 346 | for (auto& [name, value] : registers) { |
Christopher Ferris | 7937a36 | 2018-01-18 11:15:49 -0800 | [diff] [blame] | 347 | output += android::base::StringPrintf(" %-3s %0*" PRIx64, name.c_str(), |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 348 | static_cast<int>(2 * sizeof(void*)), |
Christopher Ferris | 7937a36 | 2018-01-18 11:15:49 -0800 | [diff] [blame] | 349 | static_cast<uint64_t>(value)); |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | _LOG(log, logtype::REGISTERS, " %s\n", output.c_str()); |
Josh Gao | 77b00ed | 2017-05-05 18:11:23 -0700 | [diff] [blame] | 353 | } |
| 354 | |
Christopher Ferris | 60eb197 | 2019-01-15 15:18:43 -0800 | [diff] [blame] | 355 | void dump_registers(log_t* log, unwindstack::Regs* regs) { |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 356 | // Split lr/sp/pc into their own special row. |
| 357 | static constexpr size_t column_count = 4; |
| 358 | std::vector<std::pair<std::string, uint64_t>> current_row; |
| 359 | std::vector<std::pair<std::string, uint64_t>> special_row; |
| 360 | |
| 361 | #if defined(__arm__) || defined(__aarch64__) |
Peter Collingbourne | bb2f941 | 2019-11-18 12:36:50 -0800 | [diff] [blame] | 362 | static constexpr const char* special_registers[] = {"ip", "lr", "sp", "pc", "pst"}; |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 363 | #elif defined(__i386__) |
| 364 | static constexpr const char* special_registers[] = {"ebp", "esp", "eip"}; |
| 365 | #elif defined(__x86_64__) |
| 366 | static constexpr const char* special_registers[] = {"rbp", "rsp", "rip"}; |
| 367 | #else |
| 368 | static constexpr const char* special_registers[] = {}; |
| 369 | #endif |
| 370 | |
| 371 | regs->IterateRegisters([log, ¤t_row, &special_row](const char* name, uint64_t value) { |
| 372 | auto row = ¤t_row; |
| 373 | for (const char* special_name : special_registers) { |
| 374 | if (strcmp(special_name, name) == 0) { |
| 375 | row = &special_row; |
| 376 | break; |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | row->emplace_back(name, value); |
| 381 | if (current_row.size() == column_count) { |
| 382 | print_register_row(log, current_row); |
| 383 | current_row.clear(); |
| 384 | } |
| 385 | }); |
| 386 | |
| 387 | if (!current_row.empty()) { |
| 388 | print_register_row(log, current_row); |
| 389 | } |
| 390 | |
| 391 | print_register_row(log, special_row); |
| 392 | } |
| 393 | |
Christopher Ferris | 60eb197 | 2019-01-15 15:18:43 -0800 | [diff] [blame] | 394 | void dump_memory_and_code(log_t* log, unwindstack::Maps* maps, unwindstack::Memory* memory, |
| 395 | unwindstack::Regs* regs) { |
| 396 | regs->IterateRegisters([log, maps, memory](const char* reg_name, uint64_t reg_value) { |
Elliott Hughes | e1415a5 | 2018-02-15 09:18:21 -0800 | [diff] [blame] | 397 | std::string label{"memory near "s + reg_name}; |
Christopher Ferris | 60eb197 | 2019-01-15 15:18:43 -0800 | [diff] [blame] | 398 | if (maps) { |
Christopher Ferris | c677606 | 2021-01-15 15:59:28 -0800 | [diff] [blame^] | 399 | auto map_info = maps->Find(untag_address(reg_value)); |
David Srbecky | d8ab48b | 2021-05-13 00:03:26 +0100 | [diff] [blame] | 400 | if (map_info != nullptr && !map_info->name().empty()) { |
| 401 | label += " (" + map_info->name() + ")"; |
Christopher Ferris | 60eb197 | 2019-01-15 15:18:43 -0800 | [diff] [blame] | 402 | } |
Elliott Hughes | e1415a5 | 2018-02-15 09:18:21 -0800 | [diff] [blame] | 403 | } |
| 404 | dump_memory(log, memory, reg_value, label); |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 405 | }); |
| 406 | } |
| 407 | |
Christopher Ferris | 60eb197 | 2019-01-15 15:18:43 -0800 | [diff] [blame] | 408 | static bool dump_thread(log_t* log, unwindstack::Unwinder* unwinder, const ThreadInfo& thread_info, |
Peter Collingbourne | 843f7e6 | 2020-02-28 19:07:33 -0800 | [diff] [blame] | 409 | const ProcessInfo& process_info, bool primary_thread) { |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 410 | log->current_tid = thread_info.tid; |
Josh Gao | 7c89f9e | 2016-01-13 17:57:14 -0800 | [diff] [blame] | 411 | if (!primary_thread) { |
Brigid Smith | 62ba489 | 2014-06-10 11:53:08 -0700 | [diff] [blame] | 412 | _LOG(log, logtype::THREAD, "--- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---\n"); |
Josh Gao | 7c89f9e | 2016-01-13 17:57:14 -0800 | [diff] [blame] | 413 | } |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 414 | dump_thread_info(log, thread_info); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 415 | |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 416 | if (thread_info.siginfo) { |
Peter Collingbourne | f03af88 | 2020-03-20 18:09:00 -0700 | [diff] [blame] | 417 | dump_signal_info(log, thread_info, process_info, unwinder->GetProcessMemory().get()); |
Mitch Phillips | e0b4bb1 | 2020-02-14 14:54:31 -0800 | [diff] [blame] | 418 | } |
| 419 | |
Peter Collingbourne | 843f7e6 | 2020-02-28 19:07:33 -0800 | [diff] [blame] | 420 | if (primary_thread) { |
Christopher Ferris | 2b98c82 | 2021-09-28 15:50:04 -0700 | [diff] [blame] | 421 | // The main thread must have a valid siginfo. |
| 422 | CHECK(thread_info.siginfo != nullptr); |
Christopher Ferris | 7aad256 | 2021-09-24 00:06:38 -0700 | [diff] [blame] | 423 | dump_probable_cause(log, unwinder, process_info, thread_info); |
Peter Collingbourne | 843f7e6 | 2020-02-28 19:07:33 -0800 | [diff] [blame] | 424 | |
Peter Collingbourne | 843f7e6 | 2020-02-28 19:07:33 -0800 | [diff] [blame] | 425 | dump_abort_message(log, unwinder->GetProcessMemory().get(), process_info.abort_msg_address); |
Josh Gao | 34c2556 | 2017-12-22 14:18:39 -0800 | [diff] [blame] | 426 | } |
| 427 | |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 428 | dump_registers(log, thread_info.registers.get()); |
| 429 | |
Josh Gao | 5d1c14f | 2018-04-20 12:04:41 -0700 | [diff] [blame] | 430 | // Unwind will mutate the registers, so make a copy first. |
Christopher Ferris | 60eb197 | 2019-01-15 15:18:43 -0800 | [diff] [blame] | 431 | std::unique_ptr<unwindstack::Regs> regs_copy(thread_info.registers->Clone()); |
| 432 | unwinder->SetRegs(regs_copy.get()); |
| 433 | unwinder->Unwind(); |
| 434 | if (unwinder->NumFrames() == 0) { |
Christopher Ferris | b05c472 | 2020-09-23 15:51:46 -0700 | [diff] [blame] | 435 | _LOG(log, logtype::THREAD, "Failed to unwind\n"); |
| 436 | if (unwinder->LastErrorCode() != unwindstack::ERROR_NONE) { |
| 437 | _LOG(log, logtype::THREAD, " Error code: %s\n", unwinder->LastErrorCodeString()); |
| 438 | _LOG(log, logtype::THREAD, " Error address: 0x%" PRIx64 "\n", unwinder->LastErrorAddress()); |
| 439 | } |
Christopher Ferris | 60eb197 | 2019-01-15 15:18:43 -0800 | [diff] [blame] | 440 | } else { |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 441 | _LOG(log, logtype::BACKTRACE, "\nbacktrace:\n"); |
Christopher Ferris | 4ae266c | 2019-04-03 09:27:12 -0700 | [diff] [blame] | 442 | log_backtrace(log, unwinder, " "); |
Josh Gao | 7c89f9e | 2016-01-13 17:57:14 -0800 | [diff] [blame] | 443 | } |
Brigid Smith | 62ba489 | 2014-06-10 11:53:08 -0700 | [diff] [blame] | 444 | |
Josh Gao | 7c89f9e | 2016-01-13 17:57:14 -0800 | [diff] [blame] | 445 | if (primary_thread) { |
Christopher Ferris | 7aad256 | 2021-09-24 00:06:38 -0700 | [diff] [blame] | 446 | GwpAsanCrashData gwp_asan_crash_data(unwinder->GetProcessMemory().get(), process_info, |
| 447 | thread_info); |
| 448 | |
| 449 | if (gwp_asan_crash_data.HasDeallocationTrace()) { |
| 450 | gwp_asan_crash_data.DumpDeallocationTrace(log, unwinder); |
Mitch Phillips | e0b4bb1 | 2020-02-14 14:54:31 -0800 | [diff] [blame] | 451 | } |
| 452 | |
Christopher Ferris | 7aad256 | 2021-09-24 00:06:38 -0700 | [diff] [blame] | 453 | if (gwp_asan_crash_data.HasAllocationTrace()) { |
| 454 | gwp_asan_crash_data.DumpAllocationTrace(log, unwinder); |
Mitch Phillips | e0b4bb1 | 2020-02-14 14:54:31 -0800 | [diff] [blame] | 455 | } |
| 456 | |
Christopher Ferris | 60eb197 | 2019-01-15 15:18:43 -0800 | [diff] [blame] | 457 | unwindstack::Maps* maps = unwinder->GetMaps(); |
| 458 | dump_memory_and_code(log, maps, unwinder->GetProcessMemory().get(), |
| 459 | thread_info.registers.get()); |
| 460 | if (maps != nullptr) { |
Christopher Ferris | 7937a36 | 2018-01-18 11:15:49 -0800 | [diff] [blame] | 461 | uint64_t addr = 0; |
Mitch Phillips | e4adff0 | 2021-01-21 20:41:50 -0800 | [diff] [blame] | 462 | if (process_info.has_fault_address) { |
| 463 | addr = process_info.untagged_fault_address; |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 464 | } |
Christopher Ferris | 60eb197 | 2019-01-15 15:18:43 -0800 | [diff] [blame] | 465 | dump_all_maps(log, unwinder, addr); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 466 | } |
| 467 | } |
| 468 | |
Josh Gao | 7c89f9e | 2016-01-13 17:57:14 -0800 | [diff] [blame] | 469 | log->current_tid = log->crashed_tid; |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 470 | return true; |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 471 | } |
| 472 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 473 | // Reads the contents of the specified log device, filters out the entries |
| 474 | // that don't match the specified pid, and writes them to the tombstone file. |
| 475 | // |
Mark Salyzyn | 17e85c0 | 2014-06-27 15:55:19 -0700 | [diff] [blame] | 476 | // If "tail" is non-zero, log the last "tail" number of lines. |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 477 | static void dump_log_file(log_t* log, pid_t pid, const char* filename, unsigned int tail) { |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 478 | bool first = true; |
Christopher Ferris | c637ada | 2018-07-13 16:55:38 -0700 | [diff] [blame] | 479 | logger_list* logger_list; |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 480 | |
Mark Salyzyn | 45ae446 | 2014-07-25 12:25:48 -0700 | [diff] [blame] | 481 | if (!log->should_retrieve_logcat) { |
| 482 | return; |
| 483 | } |
| 484 | |
Tom Cherry | 907b2d0 | 2020-03-23 13:40:10 -0700 | [diff] [blame] | 485 | logger_list = |
| 486 | android_logger_list_open(android_name_to_log_id(filename), ANDROID_LOG_NONBLOCK, tail, pid); |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 487 | |
Mark Salyzyn | 22b5cef | 2013-11-22 10:53:34 -0800 | [diff] [blame] | 488 | if (!logger_list) { |
Brigid Smith | 50eb546 | 2014-06-18 14:17:57 -0700 | [diff] [blame] | 489 | ALOGE("Unable to open %s: %s\n", filename, strerror(errno)); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 490 | return; |
| 491 | } |
| 492 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 493 | while (true) { |
Christopher Ferris | c637ada | 2018-07-13 16:55:38 -0700 | [diff] [blame] | 494 | log_msg log_entry; |
Mark Salyzyn | 22b5cef | 2013-11-22 10:53:34 -0800 | [diff] [blame] | 495 | ssize_t actual = android_logger_list_read(logger_list, &log_entry); |
| 496 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 497 | if (actual < 0) { |
Mark Salyzyn | 22b5cef | 2013-11-22 10:53:34 -0800 | [diff] [blame] | 498 | if (actual == -EINTR) { |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 499 | // interrupted by signal, retry |
| 500 | continue; |
Mark Salyzyn | 22b5cef | 2013-11-22 10:53:34 -0800 | [diff] [blame] | 501 | } else if (actual == -EAGAIN) { |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 502 | // non-blocking EOF; we're done |
| 503 | break; |
| 504 | } else { |
Christopher Ferris | b36b592 | 2015-06-17 18:35:59 -0700 | [diff] [blame] | 505 | ALOGE("Error while reading log: %s\n", strerror(-actual)); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 506 | break; |
| 507 | } |
| 508 | } else if (actual == 0) { |
Christopher Ferris | b36b592 | 2015-06-17 18:35:59 -0700 | [diff] [blame] | 509 | ALOGE("Got zero bytes while reading log: %s\n", strerror(errno)); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 510 | break; |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 511 | } |
| 512 | |
Brigid Smith | 50eb546 | 2014-06-18 14:17:57 -0700 | [diff] [blame] | 513 | // NOTE: if you ALOGV something here, this will spin forever, |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 514 | // because you will be writing as fast as you're reading. Any |
| 515 | // high-frequency debug diagnostics should just be written to |
| 516 | // the tombstone file. |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 517 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 518 | if (first) { |
Josh Gao | 76e1e30 | 2021-01-26 15:53:11 -0800 | [diff] [blame] | 519 | _LOG(log, logtype::LOGS, "--------- %slog %s\n", tail ? "tail end of " : "", filename); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 520 | first = false; |
| 521 | } |
| 522 | |
| 523 | // Msg format is: <priority:1><tag:N>\0<message:N>\0 |
| 524 | // |
| 525 | // We want to display it in the same format as "logcat -v threadtime" |
| 526 | // (although in this case the pid is redundant). |
Mark Salyzyn | 989980c | 2014-05-14 12:37:22 -0700 | [diff] [blame] | 527 | char timeBuf[32]; |
Christopher Ferris | c637ada | 2018-07-13 16:55:38 -0700 | [diff] [blame] | 528 | time_t sec = static_cast<time_t>(log_entry.entry.sec); |
Elliott Hughes | a660cb3 | 2020-07-23 15:26:10 -0700 | [diff] [blame] | 529 | tm tm; |
| 530 | localtime_r(&sec, &tm); |
| 531 | strftime(timeBuf, sizeof(timeBuf), "%m-%d %H:%M:%S", &tm); |
Mark Salyzyn | 989980c | 2014-05-14 12:37:22 -0700 | [diff] [blame] | 532 | |
Christopher Ferris | c637ada | 2018-07-13 16:55:38 -0700 | [diff] [blame] | 533 | char* msg = log_entry.msg(); |
| 534 | if (msg == nullptr) { |
| 535 | continue; |
| 536 | } |
Mark Salyzyn | 22b5cef | 2013-11-22 10:53:34 -0800 | [diff] [blame] | 537 | unsigned char prio = msg[0]; |
| 538 | char* tag = msg + 1; |
| 539 | msg = tag + strlen(tag) + 1; |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 540 | |
| 541 | // consume any trailing newlines |
Mark Salyzyn | fca0bd1 | 2013-12-12 12:21:20 -0800 | [diff] [blame] | 542 | char* nl = msg + strlen(msg) - 1; |
| 543 | while (nl >= msg && *nl == '\n') { |
Mark Salyzyn | 17e85c0 | 2014-06-27 15:55:19 -0700 | [diff] [blame] | 544 | *nl-- = '\0'; |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 545 | } |
| 546 | |
Christopher Ferris | c637ada | 2018-07-13 16:55:38 -0700 | [diff] [blame] | 547 | static const char* kPrioChars = "!.VDIWEFS"; |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 548 | char prioChar = (prio < strlen(kPrioChars) ? kPrioChars[prio] : '?'); |
| 549 | |
Mark Salyzyn | fca0bd1 | 2013-12-12 12:21:20 -0800 | [diff] [blame] | 550 | // Look for line breaks ('\n') and display each text line |
| 551 | // on a separate line, prefixed with the header, like logcat does. |
| 552 | do { |
| 553 | nl = strchr(msg, '\n'); |
Christopher Ferris | c637ada | 2018-07-13 16:55:38 -0700 | [diff] [blame] | 554 | if (nl != nullptr) { |
Mark Salyzyn | fca0bd1 | 2013-12-12 12:21:20 -0800 | [diff] [blame] | 555 | *nl = '\0'; |
| 556 | ++nl; |
| 557 | } |
| 558 | |
Christopher Ferris | c637ada | 2018-07-13 16:55:38 -0700 | [diff] [blame] | 559 | _LOG(log, logtype::LOGS, "%s.%03d %5d %5d %c %-8s: %s\n", timeBuf, |
| 560 | log_entry.entry.nsec / 1000000, log_entry.entry.pid, log_entry.entry.tid, prioChar, tag, |
| 561 | msg); |
Mark Salyzyn | fca0bd1 | 2013-12-12 12:21:20 -0800 | [diff] [blame] | 562 | } while ((msg = nl)); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 563 | } |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 564 | |
Mark Salyzyn | 22b5cef | 2013-11-22 10:53:34 -0800 | [diff] [blame] | 565 | android_logger_list_free(logger_list); |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 566 | } |
| 567 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 568 | // Dumps the logs generated by the specified pid to the tombstone, from both |
| 569 | // "system" and "main" log devices. Ideally we'd interleave the output. |
Mark Salyzyn | 17e85c0 | 2014-06-27 15:55:19 -0700 | [diff] [blame] | 570 | static void dump_logs(log_t* log, pid_t pid, unsigned int tail) { |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 571 | if (pid == getpid()) { |
| 572 | // Cowardly refuse to dump logs while we're running in-process. |
| 573 | return; |
| 574 | } |
| 575 | |
Mark Salyzyn | 22b5cef | 2013-11-22 10:53:34 -0800 | [diff] [blame] | 576 | dump_log_file(log, pid, "system", tail); |
| 577 | dump_log_file(log, pid, "main", tail); |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 578 | } |
| 579 | |
Josh Gao | 76e1e30 | 2021-01-26 15:53:11 -0800 | [diff] [blame] | 580 | void engrave_tombstone_ucontext(int tombstone_fd, int proto_fd, uint64_t abort_msg_address, |
| 581 | siginfo_t* siginfo, ucontext_t* ucontext) { |
Misha Wagner | 39c5b8c | 2019-04-18 16:07:33 +0100 | [diff] [blame] | 582 | pid_t uid = getuid(); |
Josh Gao | e1aa0ca | 2017-03-01 17:23:22 -0800 | [diff] [blame] | 583 | pid_t pid = getpid(); |
| 584 | pid_t tid = gettid(); |
| 585 | |
Josh Gao | e73c932 | 2017-02-08 16:06:26 -0800 | [diff] [blame] | 586 | log_t log; |
| 587 | log.current_tid = tid; |
| 588 | log.crashed_tid = tid; |
| 589 | log.tfd = tombstone_fd; |
| 590 | log.amfd_data = nullptr; |
| 591 | |
Elliott Hughes | a660cb3 | 2020-07-23 15:26:10 -0700 | [diff] [blame] | 592 | std::string thread_name = get_thread_name(tid); |
Josh Gao | 31348a7 | 2021-03-29 21:53:42 -0700 | [diff] [blame] | 593 | std::vector<std::string> command_line = get_command_line(pid); |
Josh Gao | 57f58f8 | 2017-03-15 23:23:22 -0700 | [diff] [blame] | 594 | |
Christopher Ferris | 60eb197 | 2019-01-15 15:18:43 -0800 | [diff] [blame] | 595 | std::unique_ptr<unwindstack::Regs> regs( |
| 596 | unwindstack::Regs::CreateFromUcontext(unwindstack::Regs::CurrentArch(), ucontext)); |
Josh Gao | e73c932 | 2017-02-08 16:06:26 -0800 | [diff] [blame] | 597 | |
Josh Gao | 76e1e30 | 2021-01-26 15:53:11 -0800 | [diff] [blame] | 598 | std::string selinux_label; |
| 599 | android::base::ReadFileToString("/proc/self/attr/current", &selinux_label); |
| 600 | |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 601 | std::map<pid_t, ThreadInfo> threads; |
Elliott Hughes | a660cb3 | 2020-07-23 15:26:10 -0700 | [diff] [blame] | 602 | threads[tid] = ThreadInfo{ |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 603 | .registers = std::move(regs), |
Misha Wagner | 39c5b8c | 2019-04-18 16:07:33 +0100 | [diff] [blame] | 604 | .uid = uid, |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 605 | .tid = tid, |
Josh Gao | 76e1e30 | 2021-01-26 15:53:11 -0800 | [diff] [blame] | 606 | .thread_name = std::move(thread_name), |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 607 | .pid = pid, |
Josh Gao | 31348a7 | 2021-03-29 21:53:42 -0700 | [diff] [blame] | 608 | .command_line = std::move(command_line), |
Josh Gao | 76e1e30 | 2021-01-26 15:53:11 -0800 | [diff] [blame] | 609 | .selinux_label = std::move(selinux_label), |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 610 | .siginfo = siginfo, |
| 611 | }; |
Josh Gao | 77b00ed | 2017-05-05 18:11:23 -0700 | [diff] [blame] | 612 | |
Christopher Ferris | b05c472 | 2020-09-23 15:51:46 -0700 | [diff] [blame] | 613 | unwindstack::UnwinderFromPid unwinder(kMaxFrames, pid, unwindstack::Regs::CurrentArch()); |
yidong zhang | cbf7c46 | 2021-06-24 17:51:56 +0800 | [diff] [blame] | 614 | auto process_memory = |
| 615 | unwindstack::Memory::CreateProcessMemoryCached(getpid()); |
| 616 | unwinder.SetProcessMemory(process_memory); |
Christopher Ferris | b05c472 | 2020-09-23 15:51:46 -0700 | [diff] [blame] | 617 | if (!unwinder.Init()) { |
Josh Gao | 618cea3 | 2021-01-26 17:45:43 -0800 | [diff] [blame] | 618 | async_safe_fatal("failed to init unwinder object"); |
Josh Gao | e73c932 | 2017-02-08 16:06:26 -0800 | [diff] [blame] | 619 | } |
Josh Gao | fdc95c9 | 2017-09-13 15:33:39 -0700 | [diff] [blame] | 620 | |
Peter Collingbourne | 843f7e6 | 2020-02-28 19:07:33 -0800 | [diff] [blame] | 621 | ProcessInfo process_info; |
| 622 | process_info.abort_msg_address = abort_msg_address; |
Josh Gao | 76e1e30 | 2021-01-26 15:53:11 -0800 | [diff] [blame] | 623 | engrave_tombstone(unique_fd(dup(tombstone_fd)), unique_fd(dup(proto_fd)), &unwinder, threads, tid, |
| 624 | process_info, nullptr, nullptr); |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 625 | } |
| 626 | |
Josh Gao | 76e1e30 | 2021-01-26 15:53:11 -0800 | [diff] [blame] | 627 | void engrave_tombstone(unique_fd output_fd, unique_fd proto_fd, unwindstack::Unwinder* unwinder, |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 628 | const std::map<pid_t, ThreadInfo>& threads, pid_t target_thread, |
Peter Collingbourne | 843f7e6 | 2020-02-28 19:07:33 -0800 | [diff] [blame] | 629 | const ProcessInfo& process_info, OpenFilesList* open_files, |
| 630 | std::string* amfd_data) { |
Elliott Hughes | a660cb3 | 2020-07-23 15:26:10 -0700 | [diff] [blame] | 631 | // Don't copy log messages to tombstone unless this is a development device. |
Josh Gao | 76e1e30 | 2021-01-26 15:53:11 -0800 | [diff] [blame] | 632 | Tombstone tombstone; |
| 633 | engrave_tombstone_proto(&tombstone, unwinder, threads, target_thread, process_info, open_files); |
| 634 | |
Josh Gao | 618cea3 | 2021-01-26 17:45:43 -0800 | [diff] [blame] | 635 | if (proto_fd != -1) { |
| 636 | if (!tombstone.SerializeToFileDescriptor(proto_fd.get())) { |
| 637 | async_safe_format_log(ANDROID_LOG_ERROR, LOG_TAG, "failed to write proto tombstone: %s", |
| 638 | strerror(errno)); |
| 639 | } |
Josh Gao | 76e1e30 | 2021-01-26 15:53:11 -0800 | [diff] [blame] | 640 | } |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 641 | |
| 642 | log_t log; |
| 643 | log.current_tid = target_thread; |
| 644 | log.crashed_tid = target_thread; |
| 645 | log.tfd = output_fd.get(); |
| 646 | log.amfd_data = amfd_data; |
| 647 | |
Josh Gao | 3be6874 | 2021-02-18 16:56:48 -0800 | [diff] [blame] | 648 | bool translate_proto = GetBoolProperty("debug.debuggerd.translate_proto_to_text", true); |
Josh Gao | 76e1e30 | 2021-01-26 15:53:11 -0800 | [diff] [blame] | 649 | if (translate_proto) { |
| 650 | tombstone_proto_to_text(tombstone, [&log](const std::string& line, bool should_log) { |
| 651 | _LOG(&log, should_log ? logtype::HEADER : logtype::LOGS, "%s\n", line.c_str()); |
| 652 | }); |
| 653 | } else { |
| 654 | bool want_logs = GetBoolProperty("ro.debuggable", false); |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 655 | |
Josh Gao | 76e1e30 | 2021-01-26 15:53:11 -0800 | [diff] [blame] | 656 | _LOG(&log, logtype::HEADER, |
| 657 | "*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***\n"); |
| 658 | dump_header_info(&log); |
| 659 | _LOG(&log, logtype::HEADER, "Timestamp: %s\n", get_timestamp().c_str()); |
Mitch Phillips | e0b4bb1 | 2020-02-14 14:54:31 -0800 | [diff] [blame] | 660 | |
Josh Gao | 76e1e30 | 2021-01-26 15:53:11 -0800 | [diff] [blame] | 661 | auto it = threads.find(target_thread); |
| 662 | if (it == threads.end()) { |
Josh Gao | 618cea3 | 2021-01-26 17:45:43 -0800 | [diff] [blame] | 663 | async_safe_fatal("failed to find target thread"); |
Josh Gao | fdc95c9 | 2017-09-13 15:33:39 -0700 | [diff] [blame] | 664 | } |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 665 | |
Josh Gao | 76e1e30 | 2021-01-26 15:53:11 -0800 | [diff] [blame] | 666 | dump_thread(&log, unwinder, it->second, process_info, true); |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 667 | |
Josh Gao | 76e1e30 | 2021-01-26 15:53:11 -0800 | [diff] [blame] | 668 | if (want_logs) { |
| 669 | dump_logs(&log, it->second.pid, 50); |
| 670 | } |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 671 | |
Josh Gao | 76e1e30 | 2021-01-26 15:53:11 -0800 | [diff] [blame] | 672 | for (auto& [tid, thread_info] : threads) { |
| 673 | if (tid == target_thread) { |
| 674 | continue; |
| 675 | } |
| 676 | |
| 677 | dump_thread(&log, unwinder, thread_info, process_info, false); |
| 678 | } |
| 679 | |
| 680 | if (open_files) { |
| 681 | _LOG(&log, logtype::OPEN_FILES, "\nopen files:\n"); |
| 682 | dump_open_files_list(&log, *open_files, " "); |
| 683 | } |
| 684 | |
| 685 | if (want_logs) { |
| 686 | dump_logs(&log, it->second.pid, 0); |
| 687 | } |
Josh Gao | fdc95c9 | 2017-09-13 15:33:39 -0700 | [diff] [blame] | 688 | } |
Josh Gao | e73c932 | 2017-02-08 16:06:26 -0800 | [diff] [blame] | 689 | } |