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