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