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