blob: b20014f59969bc023b1bbc5100b99913ede5e2ae [file] [log] [blame]
Jeff Brown053b8652012-06-06 16:25:03 -07001/*
Mark Salyzynfca0bd12013-12-12 12:21:20 -08002 * Copyright (C) 2012-2014 The Android Open Source Project
Jeff Brown053b8652012-06-06 16:25:03 -07003 *
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 Smith62ba4892014-06-10 11:53:08 -070017#define LOG_TAG "DEBUG"
18
Josh Gaoc3706662017-08-29 13:08:32 -070019#include "libdebuggerd/tombstone.h"
20
Jeff Brown053b8652012-06-06 16:25:03 -070021#include <dirent.h>
Kévin PETIT4bb47722013-12-18 16:44:24 +000022#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 Brown053b8652012-06-06 16:25:03 -070030#include <sys/ptrace.h>
31#include <sys/stat.h>
Mark Salyzynff2dcd92016-09-28 15:54:45 -070032#include <time.h>
Jeff Brown053b8652012-06-06 16:25:03 -070033
Christopher Ferris6e964032015-05-15 17:30:21 -070034#include <memory>
35#include <string>
36
Josh Gao57f58f82017-03-15 23:23:22 -070037#include <android-base/file.h>
Josh Gao2b2ae0c2017-08-21 14:31:17 -070038#include <android-base/logging.h>
Tom Cherry31121ca2017-10-10 13:30:57 -070039#include <android-base/properties.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080040#include <android-base/stringprintf.h>
Josh Gao2b2ae0c2017-08-21 14:31:17 -070041#include <android-base/strings.h>
Josh Gao57f58f82017-03-15 23:23:22 -070042#include <android-base/unique_fd.h>
43#include <android/log.h>
Christopher Ferris20303f82014-01-10 16:33:16 -080044#include <backtrace/Backtrace.h>
Christopher Ferris46756822014-01-14 20:16:30 -080045#include <backtrace/BacktraceMap.h>
Mark Salyzyncfd5b082016-10-17 14:28:00 -070046#include <log/log.h>
Mark Salyzynff2dcd92016-09-28 15:54:45 -070047#include <log/logprint.h>
48#include <private/android_filesystem_config.h>
Josh Gao2b2ae0c2017-08-21 14:31:17 -070049#include <unwindstack/Memory.h>
50#include <unwindstack/Regs.h>
Jeff Brown053b8652012-06-06 16:25:03 -070051
Josh Gaoc3706662017-08-29 13:08:32 -070052// Needed to get DEBUGGER_SIGNAL.
Josh Gaocbe70cb2016-10-18 18:17:52 -070053#include "debuggerd/handler.h"
rpcraigf1186f32012-07-19 09:38:06 -040054
Josh Gaoc3706662017-08-29 13:08:32 -070055#include "libdebuggerd/backtrace.h"
56#include "libdebuggerd/elf_utils.h"
Josh Gaoc3706662017-08-29 13:08:32 -070057#include "libdebuggerd/open_files_list.h"
Josh Gao2b2ae0c2017-08-21 14:31:17 -070058#include "libdebuggerd/utility.h"
Jeff Brown053b8652012-06-06 16:25:03 -070059
Tom Cherry31121ca2017-10-10 13:30:57 -070060using android::base::GetBoolProperty;
61using android::base::GetProperty;
Elliott Hughes0ba53592017-02-01 16:59:15 -080062using android::base::StringPrintf;
Josh Gao2b2ae0c2017-08-21 14:31:17 -070063using android::base::unique_fd;
64
65using unwindstack::Memory;
66using unwindstack::Regs;
Elliott Hughes0ba53592017-02-01 16:59:15 -080067
Elliott Hughese1415a52018-02-15 09:18:21 -080068using namespace std::literals::string_literals;
69
Jeff Brown053b8652012-06-06 16:25:03 -070070#define STACK_WORDS 16
71
Brigid Smith9c8dacc2014-06-02 15:02:20 -070072static void dump_header_info(log_t* log) {
Tom Cherry31121ca2017-10-10 13:30:57 -070073 auto fingerprint = GetProperty("ro.build.fingerprint", "unknown");
74 auto revision = GetProperty("ro.revision", "unknown");
Ben Chengd7760c12012-09-19 16:04:01 -070075
Tom Cherry31121ca2017-10-10 13:30:57 -070076 _LOG(log, logtype::HEADER, "Build fingerprint: '%s'\n", fingerprint.c_str());
77 _LOG(log, logtype::HEADER, "Revision: '%s'\n", revision.c_str());
Brigid Smith62ba4892014-06-10 11:53:08 -070078 _LOG(log, logtype::HEADER, "ABI: '%s'\n", ABI_STRING);
Jeff Brown053b8652012-06-06 16:25:03 -070079}
80
Josh Gao6f4644d2018-12-14 13:05:12 -080081static void dump_timestamp(log_t* log, time_t time) {
82 struct tm tm;
83 localtime_r(&time, &tm);
84
85 char buf[strlen("1970-01-01 00:00:00+0830") + 1];
86 strftime(buf, sizeof(buf), "%F %T%z", &tm);
87 _LOG(log, logtype::HEADER, "Timestamp: %s\n", buf);
88}
89
Ivan Lozanodf3cec92018-11-19 10:43:47 -080090static void dump_probable_cause(log_t* log, const siginfo_t* si, BacktraceMap* map) {
Elliott Hughes0ba53592017-02-01 16:59:15 -080091 std::string cause;
Josh Gao2b2ae0c2017-08-21 14:31:17 -070092 if (si->si_signo == SIGSEGV && si->si_code == SEGV_MAPERR) {
93 if (si->si_addr < reinterpret_cast<void*>(4096)) {
Elliott Hughes0ba53592017-02-01 16:59:15 -080094 cause = StringPrintf("null pointer dereference");
Josh Gao2b2ae0c2017-08-21 14:31:17 -070095 } else if (si->si_addr == reinterpret_cast<void*>(0xffff0ffc)) {
Elliott Hughes0ba53592017-02-01 16:59:15 -080096 cause = "call to kuser_helper_version";
Josh Gao2b2ae0c2017-08-21 14:31:17 -070097 } else if (si->si_addr == reinterpret_cast<void*>(0xffff0fe0)) {
Elliott Hughes0ba53592017-02-01 16:59:15 -080098 cause = "call to kuser_get_tls";
Josh Gao2b2ae0c2017-08-21 14:31:17 -070099 } else if (si->si_addr == reinterpret_cast<void*>(0xffff0fc0)) {
Elliott Hughes0ba53592017-02-01 16:59:15 -0800100 cause = "call to kuser_cmpxchg";
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700101 } else if (si->si_addr == reinterpret_cast<void*>(0xffff0fa0)) {
Elliott Hughes0ba53592017-02-01 16:59:15 -0800102 cause = "call to kuser_memory_barrier";
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700103 } else if (si->si_addr == reinterpret_cast<void*>(0xffff0f60)) {
Elliott Hughes0ba53592017-02-01 16:59:15 -0800104 cause = "call to kuser_cmpxchg64";
105 }
Ivan Lozanodf3cec92018-11-19 10:43:47 -0800106 } else if (si->si_signo == SIGSEGV && si->si_code == SEGV_ACCERR) {
107 for (auto it = map->begin(); it != map->end(); ++it) {
108 const backtrace_map_t* entry = *it;
109 if (si->si_addr >= reinterpret_cast<void*>(entry->start) &&
110 si->si_addr < reinterpret_cast<void*>(entry->end) && entry->flags == PROT_EXEC) {
111 cause = "execute-only (no-read) memory access error; likely due to data in .text.";
112 }
113 }
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700114 } else if (si->si_signo == SIGSYS && si->si_code == SYS_SECCOMP) {
115 cause = StringPrintf("seccomp prevented call to disallowed %s system call %d", ABI_STRING,
116 si->si_syscall);
Elliott Hughes0ba53592017-02-01 16:59:15 -0800117 }
118
119 if (!cause.empty()) _LOG(log, logtype::HEADER, "Cause: %s\n", cause.c_str());
120}
121
Elliott Hughes2baf4432018-05-30 12:55:04 -0700122static void dump_signal_info(log_t* log, const ThreadInfo& thread_info, Memory* process_memory) {
123 char addr_desc[64]; // ", fault addr 0x1234"
Elliott Hughes70d8f282018-04-25 17:00:14 -0700124 if (signal_has_si_addr(thread_info.siginfo)) {
Elliott Hughes2baf4432018-05-30 12:55:04 -0700125 void* addr = thread_info.siginfo->si_addr;
126 if (thread_info.siginfo->si_signo == SIGILL) {
127 uint32_t instruction = {};
128 process_memory->Read(reinterpret_cast<uint64_t>(addr), &instruction, sizeof(instruction));
129 snprintf(addr_desc, sizeof(addr_desc), "%p (*pc=%#08x)", addr, instruction);
130 } else {
131 snprintf(addr_desc, sizeof(addr_desc), "%p", addr);
132 }
Elliott Hughes855fcc32014-04-25 16:05:34 -0700133 } else {
134 snprintf(addr_desc, sizeof(addr_desc), "--------");
135 }
136
Elliott Hughes70d8f282018-04-25 17:00:14 -0700137 char sender_desc[32] = {}; // " from pid 1234, uid 666"
138 if (signal_has_sender(thread_info.siginfo, thread_info.pid)) {
139 get_signal_sender(sender_desc, sizeof(sender_desc), thread_info.siginfo);
140 }
Elliott Hughes0ba53592017-02-01 16:59:15 -0800141
Elliott Hughes70d8f282018-04-25 17:00:14 -0700142 _LOG(log, logtype::HEADER, "signal %d (%s), code %d (%s%s), fault addr %s\n",
143 thread_info.siginfo->si_signo, get_signame(thread_info.siginfo),
144 thread_info.siginfo->si_code, get_sigcode(thread_info.siginfo), sender_desc, addr_desc);
Jeff Brown053b8652012-06-06 16:25:03 -0700145}
146
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700147static void dump_thread_info(log_t* log, const ThreadInfo& thread_info) {
Mark Salyzyn45ae4462014-07-25 12:25:48 -0700148 // Blacklist logd, logd.reader, logd.writer, logd.auditd, logd.control ...
Josh Gao57f58f82017-03-15 23:23:22 -0700149 // TODO: Why is this controlled by thread name?
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700150 if (thread_info.thread_name == "logd" ||
151 android::base::StartsWith(thread_info.thread_name, "logd.")) {
Mark Salyzyn45ae4462014-07-25 12:25:48 -0700152 log->should_retrieve_logcat = false;
153 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800154
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700155 _LOG(log, logtype::HEADER, "pid: %d, tid: %d, name: %s >>> %s <<<\n", thread_info.pid,
156 thread_info.tid, thread_info.thread_name.c_str(), thread_info.process_name.c_str());
Christopher Ferris20303f82014-01-10 16:33:16 -0800157}
Jeff Brown053b8652012-06-06 16:25:03 -0700158
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700159static void dump_stack_segment(log_t* log, BacktraceMap* backtrace_map, Memory* process_memory,
Christopher Ferris7937a362018-01-18 11:15:49 -0800160 uint64_t* sp, size_t words, int label) {
Christopher Ferrisa21bd932015-02-27 13:39:47 -0800161 // Read the data all at once.
162 word_t stack_data[words];
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700163
164 // TODO: Do we need to word align this for crashes caused by a misaligned sp?
165 // The process_vm_readv implementation of Memory should handle this appropriately?
166 size_t bytes_read = process_memory->Read(*sp, stack_data, sizeof(word_t) * words);
Christopher Ferrisa21bd932015-02-27 13:39:47 -0800167 words = bytes_read / sizeof(word_t);
168 std::string line;
Christopher Ferris20303f82014-01-10 16:33:16 -0800169 for (size_t i = 0; i < words; i++) {
Christopher Ferrisa21bd932015-02-27 13:39:47 -0800170 line = " ";
171 if (i == 0 && label >= 0) {
172 // Print the label once.
Elliott Hughes0ba53592017-02-01 16:59:15 -0800173 line += StringPrintf("#%02d ", label);
Christopher Ferrisa21bd932015-02-27 13:39:47 -0800174 } else {
175 line += " ";
Christopher Ferris20303f82014-01-10 16:33:16 -0800176 }
Christopher Ferrise36afb02018-01-20 00:59:11 -0800177 line += StringPrintf("%" PRIPTR " %" PRIPTR, *sp, static_cast<uint64_t>(stack_data[i]));
Christopher Ferris20303f82014-01-10 16:33:16 -0800178
Christopher Ferris12385e32015-02-06 13:22:01 -0800179 backtrace_map_t map;
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700180 backtrace_map->FillIn(stack_data[i], &map);
Elliott Hughese1415a52018-02-15 09:18:21 -0800181 std::string map_name{map.Name()};
182 if (BacktraceMap::IsValid(map) && !map_name.empty()) {
183 line += " " + map_name;
Christopher Ferris7937a362018-01-18 11:15:49 -0800184 uint64_t offset = 0;
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700185 std::string func_name = backtrace_map->GetFunctionName(stack_data[i], &offset);
Christopher Ferrisa21bd932015-02-27 13:39:47 -0800186 if (!func_name.empty()) {
187 line += " (" + func_name;
Christopher Ferris20303f82014-01-10 16:33:16 -0800188 if (offset) {
Christopher Ferris7937a362018-01-18 11:15:49 -0800189 line += StringPrintf("+%" PRIu64, offset);
Jeff Brown053b8652012-06-06 16:25:03 -0700190 }
Christopher Ferrisa21bd932015-02-27 13:39:47 -0800191 line += ')';
Christopher Ferris20303f82014-01-10 16:33:16 -0800192 }
Jeff Brown053b8652012-06-06 16:25:03 -0700193 }
Christopher Ferrisa21bd932015-02-27 13:39:47 -0800194 _LOG(log, logtype::STACK, "%s\n", line.c_str());
Christopher Ferris20303f82014-01-10 16:33:16 -0800195
Pavel Chupinc6c194c2013-11-21 23:17:20 +0400196 *sp += sizeof(word_t);
Christopher Ferris20303f82014-01-10 16:33:16 -0800197 }
Jeff Brown053b8652012-06-06 16:25:03 -0700198}
199
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700200static void dump_stack(log_t* log, BacktraceMap* backtrace_map, Memory* process_memory,
201 std::vector<backtrace_frame_data_t>& frames) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800202 size_t first = 0, last;
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700203 for (size_t i = 0; i < frames.size(); i++) {
204 const backtrace_frame_data_t& frame = frames[i];
205 if (frame.sp) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800206 if (!first) {
207 first = i+1;
208 }
209 last = i;
Jeff Brown053b8652012-06-06 16:25:03 -0700210 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800211 }
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700212
Christopher Ferris20303f82014-01-10 16:33:16 -0800213 if (!first) {
214 return;
215 }
216 first--;
217
Christopher Ferris20303f82014-01-10 16:33:16 -0800218 // Dump a few words before the first frame.
Christopher Ferris7937a362018-01-18 11:15:49 -0800219 uint64_t sp = frames[first].sp - STACK_WORDS * sizeof(word_t);
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700220 dump_stack_segment(log, backtrace_map, process_memory, &sp, STACK_WORDS, -1);
Christopher Ferris20303f82014-01-10 16:33:16 -0800221
222 // Dump a few words from all successive frames.
223 // Only log the first 3 frames, put the rest in the tombstone.
224 for (size_t i = first; i <= last; i++) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700225 const backtrace_frame_data_t* frame = &frames[i];
Christopher Ferris20303f82014-01-10 16:33:16 -0800226 if (sp != frame->sp) {
Brigid Smith62ba4892014-06-10 11:53:08 -0700227 _LOG(log, logtype::STACK, " ........ ........\n");
Christopher Ferris20303f82014-01-10 16:33:16 -0800228 sp = frame->sp;
229 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800230 if (i == last) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700231 dump_stack_segment(log, backtrace_map, process_memory, &sp, STACK_WORDS, i);
Christopher Ferris20303f82014-01-10 16:33:16 -0800232 if (sp < frame->sp + frame->stack_size) {
Brigid Smith62ba4892014-06-10 11:53:08 -0700233 _LOG(log, logtype::STACK, " ........ ........\n");
Christopher Ferris20303f82014-01-10 16:33:16 -0800234 }
235 } else {
Pavel Chupinc6c194c2013-11-21 23:17:20 +0400236 size_t words = frame->stack_size / sizeof(word_t);
Christopher Ferris20303f82014-01-10 16:33:16 -0800237 if (words == 0) {
238 words = 1;
239 } else if (words > STACK_WORDS) {
240 words = STACK_WORDS;
241 }
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700242 dump_stack_segment(log, backtrace_map, process_memory, &sp, words, i);
Christopher Ferris20303f82014-01-10 16:33:16 -0800243 }
244 }
Jeff Brown053b8652012-06-06 16:25:03 -0700245}
246
Christopher Ferris7937a362018-01-18 11:15:49 -0800247static std::string get_addr_string(uint64_t addr) {
Christopher Ferris862fe022015-06-02 14:52:44 -0700248 std::string addr_str;
249#if defined(__LP64__)
Elliott Hughes0ba53592017-02-01 16:59:15 -0800250 addr_str = StringPrintf("%08x'%08x",
251 static_cast<uint32_t>(addr >> 32),
252 static_cast<uint32_t>(addr & 0xffffffff));
Christopher Ferris862fe022015-06-02 14:52:44 -0700253#else
Christopher Ferris7937a362018-01-18 11:15:49 -0800254 addr_str = StringPrintf("%08x", static_cast<uint32_t>(addr));
Christopher Ferris862fe022015-06-02 14:52:44 -0700255#endif
256 return addr_str;
257}
258
Christopher Ferris7937a362018-01-18 11:15:49 -0800259static void dump_abort_message(log_t* log, Memory* process_memory, uint64_t address) {
Josh Gao7c89f9e2016-01-13 17:57:14 -0800260 if (address == 0) {
261 return;
262 }
263
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700264 size_t length;
265 if (!process_memory->ReadFully(address, &length, sizeof(length))) {
266 _LOG(log, logtype::HEADER, "Failed to read abort message header: %s\n", strerror(errno));
267 return;
268 }
Josh Gao7c89f9e2016-01-13 17:57:14 -0800269
Josh Gao1cc7bd82018-02-13 13:16:17 -0800270 // The length field includes the length of the length field itself.
271 if (length < sizeof(size_t)) {
272 _LOG(log, logtype::HEADER, "Abort message header malformed: claimed length = %zd\n", length);
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700273 return;
Josh Gao7c89f9e2016-01-13 17:57:14 -0800274 }
Josh Gao7c89f9e2016-01-13 17:57:14 -0800275
Josh Gao1cc7bd82018-02-13 13:16:17 -0800276 length -= sizeof(size_t);
277
Josh Gao83b8ac22018-04-20 17:31:53 -0700278 // The abort message should be null terminated already, but reserve a spot for NUL just in case.
279 std::vector<char> msg(length + 1);
Josh Gao1cc7bd82018-02-13 13:16:17 -0800280 if (!process_memory->ReadFully(address + sizeof(length), &msg[0], length)) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700281 _LOG(log, logtype::HEADER, "Failed to read abort message: %s\n", strerror(errno));
282 return;
283 }
284
Josh Gao1cc7bd82018-02-13 13:16:17 -0800285 _LOG(log, logtype::HEADER, "Abort message: '%s'\n", &msg[0]);
Josh Gao7c89f9e2016-01-13 17:57:14 -0800286}
287
Christopher Ferris7937a362018-01-18 11:15:49 -0800288static void dump_all_maps(log_t* log, BacktraceMap* map, Memory* process_memory, uint64_t addr) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700289 bool print_fault_address_marker = addr;
Christopher Ferris20303f82014-01-10 16:33:16 -0800290
Christopher Ferris3a140042016-06-15 15:49:50 -0700291 ScopedBacktraceMapIteratorLock lock(map);
Elliott Hughes868d39a2017-09-26 11:54:49 -0700292 _LOG(log, logtype::MAPS,
293 "\n"
Josh Gao1ce8e142017-09-27 13:59:42 -0700294 "memory map (%zu entr%s):",
295 map->size(), map->size() == 1 ? "y" : "ies");
Elliott Hughes868d39a2017-09-26 11:54:49 -0700296 if (print_fault_address_marker) {
Christopher Ferrisb7de5f52017-12-01 21:37:37 -0800297 if (map->begin() != map->end() && addr < (*map->begin())->start) {
Josh Gao1ce8e142017-09-27 13:59:42 -0700298 _LOG(log, logtype::MAPS, "\n--->Fault address falls at %s before any mapped regions\n",
Christopher Ferris862fe022015-06-02 14:52:44 -0700299 get_addr_string(addr).c_str());
Christopher Ferrisa21bd932015-02-27 13:39:47 -0800300 print_fault_address_marker = false;
Elliott Hughes868d39a2017-09-26 11:54:49 -0700301 } else {
Josh Gao1ce8e142017-09-27 13:59:42 -0700302 _LOG(log, logtype::MAPS, " (fault address prefixed with --->)\n");
Brigid Smith8606eaa2014-07-07 12:33:50 -0700303 }
Josh Gao1ce8e142017-09-27 13:59:42 -0700304 } else {
305 _LOG(log, logtype::MAPS, "\n");
Brigid Smith8606eaa2014-07-07 12:33:50 -0700306 }
Christopher Ferrisa21bd932015-02-27 13:39:47 -0800307
308 std::string line;
Christopher Ferrisb7de5f52017-12-01 21:37:37 -0800309 for (auto it = map->begin(); it != map->end(); ++it) {
310 const backtrace_map_t* entry = *it;
Christopher Ferrisa21bd932015-02-27 13:39:47 -0800311 line = " ";
312 if (print_fault_address_marker) {
Christopher Ferrisb7de5f52017-12-01 21:37:37 -0800313 if (addr < entry->start) {
Christopher Ferris862fe022015-06-02 14:52:44 -0700314 _LOG(log, logtype::MAPS, "--->Fault address falls at %s between mapped regions\n",
315 get_addr_string(addr).c_str());
Christopher Ferrisa21bd932015-02-27 13:39:47 -0800316 print_fault_address_marker = false;
Christopher Ferrisb7de5f52017-12-01 21:37:37 -0800317 } else if (addr >= entry->start && addr < entry->end) {
Christopher Ferrisa21bd932015-02-27 13:39:47 -0800318 line = "--->";
319 print_fault_address_marker = false;
320 }
321 }
Christopher Ferrisb7de5f52017-12-01 21:37:37 -0800322 line += get_addr_string(entry->start) + '-' + get_addr_string(entry->end - 1) + ' ';
323 if (entry->flags & PROT_READ) {
Christopher Ferrisa21bd932015-02-27 13:39:47 -0800324 line += 'r';
325 } else {
326 line += '-';
327 }
Christopher Ferrisb7de5f52017-12-01 21:37:37 -0800328 if (entry->flags & PROT_WRITE) {
Christopher Ferrisa21bd932015-02-27 13:39:47 -0800329 line += 'w';
330 } else {
331 line += '-';
332 }
Christopher Ferrisb7de5f52017-12-01 21:37:37 -0800333 if (entry->flags & PROT_EXEC) {
Christopher Ferrisa21bd932015-02-27 13:39:47 -0800334 line += 'x';
335 } else {
336 line += '-';
337 }
Christopher Ferris7937a362018-01-18 11:15:49 -0800338 line += StringPrintf(" %8" PRIx64 " %8" PRIx64, entry->offset, entry->end - entry->start);
Christopher Ferris862fe022015-06-02 14:52:44 -0700339 bool space_needed = true;
Christopher Ferrisb7de5f52017-12-01 21:37:37 -0800340 if (entry->name.length() > 0) {
Christopher Ferris862fe022015-06-02 14:52:44 -0700341 space_needed = false;
Christopher Ferrisb7de5f52017-12-01 21:37:37 -0800342 line += " " + entry->name;
Christopher Ferrisa21bd932015-02-27 13:39:47 -0800343 std::string build_id;
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700344 if ((entry->flags & PROT_READ) && elf_get_build_id(process_memory, entry->start, &build_id)) {
Christopher Ferrisa21bd932015-02-27 13:39:47 -0800345 line += " (BuildId: " + build_id + ")";
346 }
347 }
Christopher Ferrisb7de5f52017-12-01 21:37:37 -0800348 if (entry->load_bias != 0) {
Christopher Ferris862fe022015-06-02 14:52:44 -0700349 if (space_needed) {
350 line += ' ';
351 }
Christopher Ferris7937a362018-01-18 11:15:49 -0800352 line += StringPrintf(" (load bias 0x%" PRIx64 ")", entry->load_bias);
Christopher Ferris2106f4b2015-05-01 15:02:03 -0700353 }
Christopher Ferrisa21bd932015-02-27 13:39:47 -0800354 _LOG(log, logtype::MAPS, "%s\n", line.c_str());
355 }
356 if (print_fault_address_marker) {
Christopher Ferris862fe022015-06-02 14:52:44 -0700357 _LOG(log, logtype::MAPS, "--->Fault address falls at %s after any mapped regions\n",
358 get_addr_string(addr).c_str());
Christopher Ferris20303f82014-01-10 16:33:16 -0800359 }
Jeff Brown053b8652012-06-06 16:25:03 -0700360}
361
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700362void dump_backtrace(log_t* log, std::vector<backtrace_frame_data_t>& frames, const char* prefix) {
363 for (auto& frame : frames) {
364 _LOG(log, logtype::BACKTRACE, "%s%s\n", prefix, Backtrace::FormatFrameData(&frame).c_str());
Christopher Ferrisab9e7dc2015-02-09 17:06:27 -0800365 }
Jeff Brown053b8652012-06-06 16:25:03 -0700366}
367
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700368static void print_register_row(log_t* log,
369 const std::vector<std::pair<std::string, uint64_t>>& registers) {
370 std::string output;
371 for (auto& [name, value] : registers) {
Christopher Ferris7937a362018-01-18 11:15:49 -0800372 output += android::base::StringPrintf(" %-3s %0*" PRIx64, name.c_str(),
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700373 static_cast<int>(2 * sizeof(void*)),
Christopher Ferris7937a362018-01-18 11:15:49 -0800374 static_cast<uint64_t>(value));
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700375 }
376
377 _LOG(log, logtype::REGISTERS, " %s\n", output.c_str());
Josh Gao77b00ed2017-05-05 18:11:23 -0700378}
379
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700380void dump_registers(log_t* log, Regs* regs) {
381 // Split lr/sp/pc into their own special row.
382 static constexpr size_t column_count = 4;
383 std::vector<std::pair<std::string, uint64_t>> current_row;
384 std::vector<std::pair<std::string, uint64_t>> special_row;
385
386#if defined(__arm__) || defined(__aarch64__)
387 static constexpr const char* special_registers[] = {"ip", "lr", "sp", "pc"};
388#elif defined(__i386__)
389 static constexpr const char* special_registers[] = {"ebp", "esp", "eip"};
390#elif defined(__x86_64__)
391 static constexpr const char* special_registers[] = {"rbp", "rsp", "rip"};
392#else
393 static constexpr const char* special_registers[] = {};
394#endif
395
396 regs->IterateRegisters([log, &current_row, &special_row](const char* name, uint64_t value) {
397 auto row = &current_row;
398 for (const char* special_name : special_registers) {
399 if (strcmp(special_name, name) == 0) {
400 row = &special_row;
401 break;
402 }
403 }
404
405 row->emplace_back(name, value);
406 if (current_row.size() == column_count) {
407 print_register_row(log, current_row);
408 current_row.clear();
409 }
410 });
411
412 if (!current_row.empty()) {
413 print_register_row(log, current_row);
414 }
415
416 print_register_row(log, special_row);
417}
418
Elliott Hughese1415a52018-02-15 09:18:21 -0800419void dump_memory_and_code(log_t* log, BacktraceMap* map, Memory* memory, Regs* regs) {
420 regs->IterateRegisters([log, map, memory](const char* reg_name, uint64_t reg_value) {
421 std::string label{"memory near "s + reg_name};
422 if (map) {
423 backtrace_map_t map_info;
424 map->FillIn(reg_value, &map_info);
425 std::string map_name{map_info.Name()};
426 if (!map_name.empty()) label += " (" + map_info.Name() + ")";
427 }
428 dump_memory(log, memory, reg_value, label);
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700429 });
430}
431
432static bool dump_thread(log_t* log, BacktraceMap* map, Memory* process_memory,
Christopher Ferris7937a362018-01-18 11:15:49 -0800433 const ThreadInfo& thread_info, uint64_t abort_msg_address,
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700434 bool primary_thread) {
435 UNUSED(process_memory);
436 log->current_tid = thread_info.tid;
Josh Gao7c89f9e2016-01-13 17:57:14 -0800437 if (!primary_thread) {
Brigid Smith62ba4892014-06-10 11:53:08 -0700438 _LOG(log, logtype::THREAD, "--- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---\n");
Josh Gao7c89f9e2016-01-13 17:57:14 -0800439 }
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700440 dump_thread_info(log, thread_info);
Christopher Ferris20303f82014-01-10 16:33:16 -0800441
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700442 if (thread_info.siginfo) {
Elliott Hughes2baf4432018-05-30 12:55:04 -0700443 dump_signal_info(log, thread_info, process_memory);
Ivan Lozanodf3cec92018-11-19 10:43:47 -0800444 dump_probable_cause(log, thread_info.siginfo, map);
Josh Gao7c89f9e2016-01-13 17:57:14 -0800445 }
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700446
Josh Gao34c25562017-12-22 14:18:39 -0800447 if (primary_thread) {
448 dump_abort_message(log, process_memory, abort_msg_address);
449 }
450
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700451 dump_registers(log, thread_info.registers.get());
452
Josh Gao5d1c14f2018-04-20 12:04:41 -0700453 // Unwind will mutate the registers, so make a copy first.
454 std::unique_ptr<Regs> regs_copy(thread_info.registers->Clone());
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700455 std::vector<backtrace_frame_data_t> frames;
Josh Gao5d1c14f2018-04-20 12:04:41 -0700456 if (!Backtrace::Unwind(regs_copy.get(), map, &frames, 0, nullptr)) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700457 _LOG(log, logtype::THREAD, "Failed to unwind");
458 return false;
459 }
460
461 if (!frames.empty()) {
462 _LOG(log, logtype::BACKTRACE, "\nbacktrace:\n");
463 dump_backtrace(log, frames, " ");
464
465 _LOG(log, logtype::STACK, "\nstack:\n");
466 dump_stack(log, map, process_memory, frames);
Josh Gao7c89f9e2016-01-13 17:57:14 -0800467 }
Brigid Smith62ba4892014-06-10 11:53:08 -0700468
Josh Gao7c89f9e2016-01-13 17:57:14 -0800469 if (primary_thread) {
Elliott Hughese1415a52018-02-15 09:18:21 -0800470 dump_memory_and_code(log, map, process_memory, thread_info.registers.get());
Josh Gao7c89f9e2016-01-13 17:57:14 -0800471 if (map) {
Christopher Ferris7937a362018-01-18 11:15:49 -0800472 uint64_t addr = 0;
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700473 siginfo_t* si = thread_info.siginfo;
Elliott Hughes70d8f282018-04-25 17:00:14 -0700474 if (signal_has_si_addr(si)) {
Christopher Ferris7937a362018-01-18 11:15:49 -0800475 addr = reinterpret_cast<uint64_t>(si->si_addr);
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700476 }
477 dump_all_maps(log, map, process_memory, addr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800478 }
479 }
480
Josh Gao7c89f9e2016-01-13 17:57:14 -0800481 log->current_tid = log->crashed_tid;
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700482 return true;
Jeff Brown053b8652012-06-06 16:25:03 -0700483}
484
Christopher Ferris20303f82014-01-10 16:33:16 -0800485// Reads the contents of the specified log device, filters out the entries
486// that don't match the specified pid, and writes them to the tombstone file.
487//
Mark Salyzyn17e85c02014-06-27 15:55:19 -0700488// If "tail" is non-zero, log the last "tail" number of lines.
Mark Salyzyn989980c2014-05-14 12:37:22 -0700489static EventTagMap* g_eventTagMap = NULL;
490
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700491static void dump_log_file(log_t* log, pid_t pid, const char* filename, unsigned int tail) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800492 bool first = true;
Christopher Ferrisc637ada2018-07-13 16:55:38 -0700493 logger_list* logger_list;
Jeff Brown053b8652012-06-06 16:25:03 -0700494
Mark Salyzyn45ae4462014-07-25 12:25:48 -0700495 if (!log->should_retrieve_logcat) {
496 return;
497 }
498
Mark Salyzyn22b5cef2013-11-22 10:53:34 -0800499 logger_list = android_logger_list_open(
Mark Salyzyn2d3f38a2015-01-26 10:46:44 -0800500 android_name_to_log_id(filename), ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK, tail, pid);
Jeff Brown053b8652012-06-06 16:25:03 -0700501
Mark Salyzyn22b5cef2013-11-22 10:53:34 -0800502 if (!logger_list) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700503 ALOGE("Unable to open %s: %s\n", filename, strerror(errno));
Christopher Ferris20303f82014-01-10 16:33:16 -0800504 return;
505 }
506
Christopher Ferris20303f82014-01-10 16:33:16 -0800507 while (true) {
Christopher Ferrisc637ada2018-07-13 16:55:38 -0700508 log_msg log_entry;
Mark Salyzyn22b5cef2013-11-22 10:53:34 -0800509 ssize_t actual = android_logger_list_read(logger_list, &log_entry);
510
Christopher Ferris20303f82014-01-10 16:33:16 -0800511 if (actual < 0) {
Mark Salyzyn22b5cef2013-11-22 10:53:34 -0800512 if (actual == -EINTR) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800513 // interrupted by signal, retry
514 continue;
Mark Salyzyn22b5cef2013-11-22 10:53:34 -0800515 } else if (actual == -EAGAIN) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800516 // non-blocking EOF; we're done
517 break;
518 } else {
Christopher Ferrisb36b5922015-06-17 18:35:59 -0700519 ALOGE("Error while reading log: %s\n", strerror(-actual));
Christopher Ferris20303f82014-01-10 16:33:16 -0800520 break;
521 }
522 } else if (actual == 0) {
Christopher Ferrisb36b5922015-06-17 18:35:59 -0700523 ALOGE("Got zero bytes while reading log: %s\n", strerror(errno));
Christopher Ferris20303f82014-01-10 16:33:16 -0800524 break;
Jeff Brown053b8652012-06-06 16:25:03 -0700525 }
526
Brigid Smith50eb5462014-06-18 14:17:57 -0700527 // NOTE: if you ALOGV something here, this will spin forever,
Christopher Ferris20303f82014-01-10 16:33:16 -0800528 // because you will be writing as fast as you're reading. Any
529 // high-frequency debug diagnostics should just be written to
530 // the tombstone file.
Jeff Brown053b8652012-06-06 16:25:03 -0700531
Christopher Ferris20303f82014-01-10 16:33:16 -0800532 if (first) {
Mark Salyzyne43290d2014-06-27 10:32:22 -0700533 _LOG(log, logtype::LOGS, "--------- %slog %s\n",
Mark Salyzyn22b5cef2013-11-22 10:53:34 -0800534 tail ? "tail end of " : "", filename);
Christopher Ferris20303f82014-01-10 16:33:16 -0800535 first = false;
536 }
537
538 // Msg format is: <priority:1><tag:N>\0<message:N>\0
539 //
540 // We want to display it in the same format as "logcat -v threadtime"
541 // (although in this case the pid is redundant).
Mark Salyzyn989980c2014-05-14 12:37:22 -0700542 char timeBuf[32];
Christopher Ferrisc637ada2018-07-13 16:55:38 -0700543 time_t sec = static_cast<time_t>(log_entry.entry.sec);
Mark Salyzyn989980c2014-05-14 12:37:22 -0700544 struct tm tmBuf;
545 struct tm* ptm;
546 ptm = localtime_r(&sec, &tmBuf);
547 strftime(timeBuf, sizeof(timeBuf), "%m-%d %H:%M:%S", ptm);
548
549 if (log_entry.id() == LOG_ID_EVENTS) {
550 if (!g_eventTagMap) {
Christopher Ferrisc637ada2018-07-13 16:55:38 -0700551 g_eventTagMap = android_openEventTagMap(nullptr);
Mark Salyzyn989980c2014-05-14 12:37:22 -0700552 }
553 AndroidLogEntry e;
554 char buf[512];
Christopher Ferrisc637ada2018-07-13 16:55:38 -0700555 if (android_log_processBinaryLogBuffer(&log_entry.entry_v1, &e, g_eventTagMap, buf,
556 sizeof(buf)) == 0) {
557 _LOG(log, logtype::LOGS, "%s.%03d %5d %5d %c %-8.*s: %s\n", timeBuf,
558 log_entry.entry.nsec / 1000000, log_entry.entry.pid, log_entry.entry.tid, 'I',
559 (int)e.tagLen, e.tag, e.message);
560 }
Mark Salyzyn989980c2014-05-14 12:37:22 -0700561 continue;
562 }
563
Christopher Ferrisc637ada2018-07-13 16:55:38 -0700564 char* msg = log_entry.msg();
565 if (msg == nullptr) {
566 continue;
567 }
Mark Salyzyn22b5cef2013-11-22 10:53:34 -0800568 unsigned char prio = msg[0];
569 char* tag = msg + 1;
570 msg = tag + strlen(tag) + 1;
Christopher Ferris20303f82014-01-10 16:33:16 -0800571
572 // consume any trailing newlines
Mark Salyzynfca0bd12013-12-12 12:21:20 -0800573 char* nl = msg + strlen(msg) - 1;
574 while (nl >= msg && *nl == '\n') {
Mark Salyzyn17e85c02014-06-27 15:55:19 -0700575 *nl-- = '\0';
Christopher Ferris20303f82014-01-10 16:33:16 -0800576 }
577
Christopher Ferrisc637ada2018-07-13 16:55:38 -0700578 static const char* kPrioChars = "!.VDIWEFS";
Christopher Ferris20303f82014-01-10 16:33:16 -0800579 char prioChar = (prio < strlen(kPrioChars) ? kPrioChars[prio] : '?');
580
Mark Salyzynfca0bd12013-12-12 12:21:20 -0800581 // Look for line breaks ('\n') and display each text line
582 // on a separate line, prefixed with the header, like logcat does.
583 do {
584 nl = strchr(msg, '\n');
Christopher Ferrisc637ada2018-07-13 16:55:38 -0700585 if (nl != nullptr) {
Mark Salyzynfca0bd12013-12-12 12:21:20 -0800586 *nl = '\0';
587 ++nl;
588 }
589
Christopher Ferrisc637ada2018-07-13 16:55:38 -0700590 _LOG(log, logtype::LOGS, "%s.%03d %5d %5d %c %-8s: %s\n", timeBuf,
591 log_entry.entry.nsec / 1000000, log_entry.entry.pid, log_entry.entry.tid, prioChar, tag,
592 msg);
Mark Salyzynfca0bd12013-12-12 12:21:20 -0800593 } while ((msg = nl));
Christopher Ferris20303f82014-01-10 16:33:16 -0800594 }
Jeff Brown053b8652012-06-06 16:25:03 -0700595
Mark Salyzyn22b5cef2013-11-22 10:53:34 -0800596 android_logger_list_free(logger_list);
Jeff Brown053b8652012-06-06 16:25:03 -0700597}
598
Christopher Ferris20303f82014-01-10 16:33:16 -0800599// Dumps the logs generated by the specified pid to the tombstone, from both
600// "system" and "main" log devices. Ideally we'd interleave the output.
Mark Salyzyn17e85c02014-06-27 15:55:19 -0700601static void dump_logs(log_t* log, pid_t pid, unsigned int tail) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700602 if (pid == getpid()) {
603 // Cowardly refuse to dump logs while we're running in-process.
604 return;
605 }
606
Mark Salyzyn22b5cef2013-11-22 10:53:34 -0800607 dump_log_file(log, pid, "system", tail);
608 dump_log_file(log, pid, "main", tail);
Jeff Brown053b8652012-06-06 16:25:03 -0700609}
610
Christopher Ferris7937a362018-01-18 11:15:49 -0800611void engrave_tombstone_ucontext(int tombstone_fd, uint64_t abort_msg_address, siginfo_t* siginfo,
Josh Gaoe1aa0ca2017-03-01 17:23:22 -0800612 ucontext_t* ucontext) {
613 pid_t pid = getpid();
614 pid_t tid = gettid();
615
Josh Gaoe73c9322017-02-08 16:06:26 -0800616 log_t log;
617 log.current_tid = tid;
618 log.crashed_tid = tid;
619 log.tfd = tombstone_fd;
620 log.amfd_data = nullptr;
621
Josh Gao57f58f82017-03-15 23:23:22 -0700622 char thread_name[16];
623 char process_name[128];
624
625 read_with_default("/proc/self/comm", thread_name, sizeof(thread_name), "<unknown>");
626 read_with_default("/proc/self/cmdline", process_name, sizeof(process_name), "<unknown>");
627
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700628 std::unique_ptr<Regs> regs(Regs::CreateFromUcontext(Regs::CurrentArch(), ucontext));
Josh Gaoe73c9322017-02-08 16:06:26 -0800629
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700630 std::map<pid_t, ThreadInfo> threads;
631 threads[gettid()] = ThreadInfo{
632 .registers = std::move(regs),
633 .tid = tid,
634 .thread_name = thread_name,
635 .pid = pid,
636 .process_name = process_name,
637 .siginfo = siginfo,
638 };
Josh Gao77b00ed2017-05-05 18:11:23 -0700639
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700640 std::unique_ptr<BacktraceMap> backtrace_map(BacktraceMap::Create(getpid(), false));
641 if (!backtrace_map) {
642 ALOGE("failed to create backtrace map");
643 _exit(1);
Josh Gaoe73c9322017-02-08 16:06:26 -0800644 }
Josh Gaofdc95c92017-09-13 15:33:39 -0700645
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700646 std::shared_ptr<Memory> process_memory = backtrace_map->GetProcessMemory();
647 engrave_tombstone(unique_fd(dup(tombstone_fd)), backtrace_map.get(), process_memory.get(),
648 threads, tid, abort_msg_address, nullptr, nullptr);
649}
650
651void engrave_tombstone(unique_fd output_fd, BacktraceMap* map, Memory* process_memory,
652 const std::map<pid_t, ThreadInfo>& threads, pid_t target_thread,
Christopher Ferris7937a362018-01-18 11:15:49 -0800653 uint64_t abort_msg_address, OpenFilesList* open_files,
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700654 std::string* amfd_data) {
655 // don't copy log messages to tombstone unless this is a dev device
656 bool want_logs = android::base::GetBoolProperty("ro.debuggable", false);
657
658 log_t log;
659 log.current_tid = target_thread;
660 log.crashed_tid = target_thread;
661 log.tfd = output_fd.get();
662 log.amfd_data = amfd_data;
663
664 _LOG(&log, logtype::HEADER, "*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***\n");
665 dump_header_info(&log);
Josh Gao6f4644d2018-12-14 13:05:12 -0800666 dump_timestamp(&log, time(nullptr));
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700667
668 auto it = threads.find(target_thread);
669 if (it == threads.end()) {
670 LOG(FATAL) << "failed to find target thread";
671 }
672 dump_thread(&log, map, process_memory, it->second, abort_msg_address, true);
673
674 if (want_logs) {
chirag honnavardef08882017-04-20 18:22:06 +0900675 dump_logs(&log, it->second.pid, 50);
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700676 }
677
678 for (auto& [tid, thread_info] : threads) {
679 if (tid == target_thread) {
680 continue;
Josh Gaofdc95c92017-09-13 15:33:39 -0700681 }
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700682
683 dump_thread(&log, map, process_memory, thread_info, 0, false);
684 }
685
686 if (open_files) {
687 _LOG(&log, logtype::OPEN_FILES, "\nopen files:\n");
688 dump_open_files_list(&log, *open_files, " ");
689 }
690
691 if (want_logs) {
692 dump_logs(&log, it->second.pid, 0);
Josh Gaofdc95c92017-09-13 15:33:39 -0700693 }
Josh Gaoe73c9322017-02-08 16:06:26 -0800694}