blob: af8072e76c2fa2d95b93786c0b06ead4e0067090 [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 Gao2b2ae0c2017-08-21 14:31:17 -070081static void dump_probable_cause(log_t* log, const siginfo_t* si) {
Elliott Hughes0ba53592017-02-01 16:59:15 -080082 std::string cause;
Josh Gao2b2ae0c2017-08-21 14:31:17 -070083 if (si->si_signo == SIGSEGV && si->si_code == SEGV_MAPERR) {
84 if (si->si_addr < reinterpret_cast<void*>(4096)) {
Elliott Hughes0ba53592017-02-01 16:59:15 -080085 cause = StringPrintf("null pointer dereference");
Josh Gao2b2ae0c2017-08-21 14:31:17 -070086 } else if (si->si_addr == reinterpret_cast<void*>(0xffff0ffc)) {
Elliott Hughes0ba53592017-02-01 16:59:15 -080087 cause = "call to kuser_helper_version";
Josh Gao2b2ae0c2017-08-21 14:31:17 -070088 } else if (si->si_addr == reinterpret_cast<void*>(0xffff0fe0)) {
Elliott Hughes0ba53592017-02-01 16:59:15 -080089 cause = "call to kuser_get_tls";
Josh Gao2b2ae0c2017-08-21 14:31:17 -070090 } else if (si->si_addr == reinterpret_cast<void*>(0xffff0fc0)) {
Elliott Hughes0ba53592017-02-01 16:59:15 -080091 cause = "call to kuser_cmpxchg";
Josh Gao2b2ae0c2017-08-21 14:31:17 -070092 } else if (si->si_addr == reinterpret_cast<void*>(0xffff0fa0)) {
Elliott Hughes0ba53592017-02-01 16:59:15 -080093 cause = "call to kuser_memory_barrier";
Josh Gao2b2ae0c2017-08-21 14:31:17 -070094 } else if (si->si_addr == reinterpret_cast<void*>(0xffff0f60)) {
Elliott Hughes0ba53592017-02-01 16:59:15 -080095 cause = "call to kuser_cmpxchg64";
96 }
Josh Gao2b2ae0c2017-08-21 14:31:17 -070097 } 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 Hughes0ba53592017-02-01 16:59:15 -0800100 }
101
102 if (!cause.empty()) _LOG(log, logtype::HEADER, "Cause: %s\n", cause.c_str());
103}
104
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700105static void dump_signal_info(log_t* log, const siginfo_t* si) {
Elliott Hughes855fcc32014-04-25 16:05:34 -0700106 char addr_desc[32]; // ", fault addr 0x1234"
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700107 if (signal_has_si_addr(si->si_signo, si->si_code)) {
108 snprintf(addr_desc, sizeof(addr_desc), "%p", si->si_addr);
Elliott Hughes855fcc32014-04-25 16:05:34 -0700109 } else {
110 snprintf(addr_desc, sizeof(addr_desc), "--------");
111 }
112
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700113 _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 Hughes0ba53592017-02-01 16:59:15 -0800115
116 dump_probable_cause(log, si);
Jeff Brown053b8652012-06-06 16:25:03 -0700117}
118
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700119static void dump_thread_info(log_t* log, const ThreadInfo& thread_info) {
Mark Salyzyn45ae4462014-07-25 12:25:48 -0700120 // Blacklist logd, logd.reader, logd.writer, logd.auditd, logd.control ...
Josh Gao57f58f82017-03-15 23:23:22 -0700121 // TODO: Why is this controlled by thread name?
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700122 if (thread_info.thread_name == "logd" ||
123 android::base::StartsWith(thread_info.thread_name, "logd.")) {
Mark Salyzyn45ae4462014-07-25 12:25:48 -0700124 log->should_retrieve_logcat = false;
125 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800126
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700127 _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 Ferris20303f82014-01-10 16:33:16 -0800129}
Jeff Brown053b8652012-06-06 16:25:03 -0700130
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700131static void dump_stack_segment(log_t* log, BacktraceMap* backtrace_map, Memory* process_memory,
Christopher Ferris7937a362018-01-18 11:15:49 -0800132 uint64_t* sp, size_t words, int label) {
Christopher Ferrisa21bd932015-02-27 13:39:47 -0800133 // Read the data all at once.
134 word_t stack_data[words];
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700135
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 Ferrisa21bd932015-02-27 13:39:47 -0800139 words = bytes_read / sizeof(word_t);
140 std::string line;
Christopher Ferris20303f82014-01-10 16:33:16 -0800141 for (size_t i = 0; i < words; i++) {
Christopher Ferrisa21bd932015-02-27 13:39:47 -0800142 line = " ";
143 if (i == 0 && label >= 0) {
144 // Print the label once.
Elliott Hughes0ba53592017-02-01 16:59:15 -0800145 line += StringPrintf("#%02d ", label);
Christopher Ferrisa21bd932015-02-27 13:39:47 -0800146 } else {
147 line += " ";
Christopher Ferris20303f82014-01-10 16:33:16 -0800148 }
Christopher Ferrise36afb02018-01-20 00:59:11 -0800149 line += StringPrintf("%" PRIPTR " %" PRIPTR, *sp, static_cast<uint64_t>(stack_data[i]));
Christopher Ferris20303f82014-01-10 16:33:16 -0800150
Christopher Ferris12385e32015-02-06 13:22:01 -0800151 backtrace_map_t map;
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700152 backtrace_map->FillIn(stack_data[i], &map);
Elliott Hughese1415a52018-02-15 09:18:21 -0800153 std::string map_name{map.Name()};
154 if (BacktraceMap::IsValid(map) && !map_name.empty()) {
155 line += " " + map_name;
Christopher Ferris7937a362018-01-18 11:15:49 -0800156 uint64_t offset = 0;
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700157 std::string func_name = backtrace_map->GetFunctionName(stack_data[i], &offset);
Christopher Ferrisa21bd932015-02-27 13:39:47 -0800158 if (!func_name.empty()) {
159 line += " (" + func_name;
Christopher Ferris20303f82014-01-10 16:33:16 -0800160 if (offset) {
Christopher Ferris7937a362018-01-18 11:15:49 -0800161 line += StringPrintf("+%" PRIu64, offset);
Jeff Brown053b8652012-06-06 16:25:03 -0700162 }
Christopher Ferrisa21bd932015-02-27 13:39:47 -0800163 line += ')';
Christopher Ferris20303f82014-01-10 16:33:16 -0800164 }
Jeff Brown053b8652012-06-06 16:25:03 -0700165 }
Christopher Ferrisa21bd932015-02-27 13:39:47 -0800166 _LOG(log, logtype::STACK, "%s\n", line.c_str());
Christopher Ferris20303f82014-01-10 16:33:16 -0800167
Pavel Chupinc6c194c2013-11-21 23:17:20 +0400168 *sp += sizeof(word_t);
Christopher Ferris20303f82014-01-10 16:33:16 -0800169 }
Jeff Brown053b8652012-06-06 16:25:03 -0700170}
171
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700172static void dump_stack(log_t* log, BacktraceMap* backtrace_map, Memory* process_memory,
173 std::vector<backtrace_frame_data_t>& frames) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800174 size_t first = 0, last;
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700175 for (size_t i = 0; i < frames.size(); i++) {
176 const backtrace_frame_data_t& frame = frames[i];
177 if (frame.sp) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800178 if (!first) {
179 first = i+1;
180 }
181 last = i;
Jeff Brown053b8652012-06-06 16:25:03 -0700182 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800183 }
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700184
Christopher Ferris20303f82014-01-10 16:33:16 -0800185 if (!first) {
186 return;
187 }
188 first--;
189
Christopher Ferris20303f82014-01-10 16:33:16 -0800190 // Dump a few words before the first frame.
Christopher Ferris7937a362018-01-18 11:15:49 -0800191 uint64_t sp = frames[first].sp - STACK_WORDS * sizeof(word_t);
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700192 dump_stack_segment(log, backtrace_map, process_memory, &sp, STACK_WORDS, -1);
Christopher Ferris20303f82014-01-10 16:33:16 -0800193
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 Gao2b2ae0c2017-08-21 14:31:17 -0700197 const backtrace_frame_data_t* frame = &frames[i];
Christopher Ferris20303f82014-01-10 16:33:16 -0800198 if (sp != frame->sp) {
Brigid Smith62ba4892014-06-10 11:53:08 -0700199 _LOG(log, logtype::STACK, " ........ ........\n");
Christopher Ferris20303f82014-01-10 16:33:16 -0800200 sp = frame->sp;
201 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800202 if (i == last) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700203 dump_stack_segment(log, backtrace_map, process_memory, &sp, STACK_WORDS, i);
Christopher Ferris20303f82014-01-10 16:33:16 -0800204 if (sp < frame->sp + frame->stack_size) {
Brigid Smith62ba4892014-06-10 11:53:08 -0700205 _LOG(log, logtype::STACK, " ........ ........\n");
Christopher Ferris20303f82014-01-10 16:33:16 -0800206 }
207 } else {
Pavel Chupinc6c194c2013-11-21 23:17:20 +0400208 size_t words = frame->stack_size / sizeof(word_t);
Christopher Ferris20303f82014-01-10 16:33:16 -0800209 if (words == 0) {
210 words = 1;
211 } else if (words > STACK_WORDS) {
212 words = STACK_WORDS;
213 }
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700214 dump_stack_segment(log, backtrace_map, process_memory, &sp, words, i);
Christopher Ferris20303f82014-01-10 16:33:16 -0800215 }
216 }
Jeff Brown053b8652012-06-06 16:25:03 -0700217}
218
Christopher Ferris7937a362018-01-18 11:15:49 -0800219static std::string get_addr_string(uint64_t addr) {
Christopher Ferris862fe022015-06-02 14:52:44 -0700220 std::string addr_str;
221#if defined(__LP64__)
Elliott Hughes0ba53592017-02-01 16:59:15 -0800222 addr_str = StringPrintf("%08x'%08x",
223 static_cast<uint32_t>(addr >> 32),
224 static_cast<uint32_t>(addr & 0xffffffff));
Christopher Ferris862fe022015-06-02 14:52:44 -0700225#else
Christopher Ferris7937a362018-01-18 11:15:49 -0800226 addr_str = StringPrintf("%08x", static_cast<uint32_t>(addr));
Christopher Ferris862fe022015-06-02 14:52:44 -0700227#endif
228 return addr_str;
229}
230
Christopher Ferris7937a362018-01-18 11:15:49 -0800231static void dump_abort_message(log_t* log, Memory* process_memory, uint64_t address) {
Josh Gao7c89f9e2016-01-13 17:57:14 -0800232 if (address == 0) {
233 return;
234 }
235
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700236 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 Gao7c89f9e2016-01-13 17:57:14 -0800241
Josh Gao1cc7bd82018-02-13 13:16:17 -0800242 // The length field includes the length of the length field itself.
243 if (length < sizeof(size_t)) {
244 _LOG(log, logtype::HEADER, "Abort message header malformed: claimed length = %zd\n", length);
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700245 return;
Josh Gao7c89f9e2016-01-13 17:57:14 -0800246 }
Josh Gao7c89f9e2016-01-13 17:57:14 -0800247
Josh Gao1cc7bd82018-02-13 13:16:17 -0800248 length -= sizeof(size_t);
249
Josh Gao83b8ac22018-04-20 17:31:53 -0700250 // The abort message should be null terminated already, but reserve a spot for NUL just in case.
251 std::vector<char> msg(length + 1);
Josh Gao1cc7bd82018-02-13 13:16:17 -0800252 if (!process_memory->ReadFully(address + sizeof(length), &msg[0], length)) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700253 _LOG(log, logtype::HEADER, "Failed to read abort message: %s\n", strerror(errno));
254 return;
255 }
256
Josh Gao1cc7bd82018-02-13 13:16:17 -0800257 _LOG(log, logtype::HEADER, "Abort message: '%s'\n", &msg[0]);
Josh Gao7c89f9e2016-01-13 17:57:14 -0800258}
259
Christopher Ferris7937a362018-01-18 11:15:49 -0800260static void dump_all_maps(log_t* log, BacktraceMap* map, Memory* process_memory, uint64_t addr) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700261 bool print_fault_address_marker = addr;
Christopher Ferris20303f82014-01-10 16:33:16 -0800262
Christopher Ferris3a140042016-06-15 15:49:50 -0700263 ScopedBacktraceMapIteratorLock lock(map);
Elliott Hughes868d39a2017-09-26 11:54:49 -0700264 _LOG(log, logtype::MAPS,
265 "\n"
Josh Gao1ce8e142017-09-27 13:59:42 -0700266 "memory map (%zu entr%s):",
267 map->size(), map->size() == 1 ? "y" : "ies");
Elliott Hughes868d39a2017-09-26 11:54:49 -0700268 if (print_fault_address_marker) {
Christopher Ferrisb7de5f52017-12-01 21:37:37 -0800269 if (map->begin() != map->end() && addr < (*map->begin())->start) {
Josh Gao1ce8e142017-09-27 13:59:42 -0700270 _LOG(log, logtype::MAPS, "\n--->Fault address falls at %s before any mapped regions\n",
Christopher Ferris862fe022015-06-02 14:52:44 -0700271 get_addr_string(addr).c_str());
Christopher Ferrisa21bd932015-02-27 13:39:47 -0800272 print_fault_address_marker = false;
Elliott Hughes868d39a2017-09-26 11:54:49 -0700273 } else {
Josh Gao1ce8e142017-09-27 13:59:42 -0700274 _LOG(log, logtype::MAPS, " (fault address prefixed with --->)\n");
Brigid Smith8606eaa2014-07-07 12:33:50 -0700275 }
Josh Gao1ce8e142017-09-27 13:59:42 -0700276 } else {
277 _LOG(log, logtype::MAPS, "\n");
Brigid Smith8606eaa2014-07-07 12:33:50 -0700278 }
Christopher Ferrisa21bd932015-02-27 13:39:47 -0800279
280 std::string line;
Christopher Ferrisb7de5f52017-12-01 21:37:37 -0800281 for (auto it = map->begin(); it != map->end(); ++it) {
282 const backtrace_map_t* entry = *it;
Christopher Ferrisa21bd932015-02-27 13:39:47 -0800283 line = " ";
284 if (print_fault_address_marker) {
Christopher Ferrisb7de5f52017-12-01 21:37:37 -0800285 if (addr < entry->start) {
Christopher Ferris862fe022015-06-02 14:52:44 -0700286 _LOG(log, logtype::MAPS, "--->Fault address falls at %s between mapped regions\n",
287 get_addr_string(addr).c_str());
Christopher Ferrisa21bd932015-02-27 13:39:47 -0800288 print_fault_address_marker = false;
Christopher Ferrisb7de5f52017-12-01 21:37:37 -0800289 } else if (addr >= entry->start && addr < entry->end) {
Christopher Ferrisa21bd932015-02-27 13:39:47 -0800290 line = "--->";
291 print_fault_address_marker = false;
292 }
293 }
Christopher Ferrisb7de5f52017-12-01 21:37:37 -0800294 line += get_addr_string(entry->start) + '-' + get_addr_string(entry->end - 1) + ' ';
295 if (entry->flags & PROT_READ) {
Christopher Ferrisa21bd932015-02-27 13:39:47 -0800296 line += 'r';
297 } else {
298 line += '-';
299 }
Christopher Ferrisb7de5f52017-12-01 21:37:37 -0800300 if (entry->flags & PROT_WRITE) {
Christopher Ferrisa21bd932015-02-27 13:39:47 -0800301 line += 'w';
302 } else {
303 line += '-';
304 }
Christopher Ferrisb7de5f52017-12-01 21:37:37 -0800305 if (entry->flags & PROT_EXEC) {
Christopher Ferrisa21bd932015-02-27 13:39:47 -0800306 line += 'x';
307 } else {
308 line += '-';
309 }
Christopher Ferris7937a362018-01-18 11:15:49 -0800310 line += StringPrintf(" %8" PRIx64 " %8" PRIx64, entry->offset, entry->end - entry->start);
Christopher Ferris862fe022015-06-02 14:52:44 -0700311 bool space_needed = true;
Christopher Ferrisb7de5f52017-12-01 21:37:37 -0800312 if (entry->name.length() > 0) {
Christopher Ferris862fe022015-06-02 14:52:44 -0700313 space_needed = false;
Christopher Ferrisb7de5f52017-12-01 21:37:37 -0800314 line += " " + entry->name;
Christopher Ferrisa21bd932015-02-27 13:39:47 -0800315 std::string build_id;
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700316 if ((entry->flags & PROT_READ) && elf_get_build_id(process_memory, entry->start, &build_id)) {
Christopher Ferrisa21bd932015-02-27 13:39:47 -0800317 line += " (BuildId: " + build_id + ")";
318 }
319 }
Christopher Ferrisb7de5f52017-12-01 21:37:37 -0800320 if (entry->load_bias != 0) {
Christopher Ferris862fe022015-06-02 14:52:44 -0700321 if (space_needed) {
322 line += ' ';
323 }
Christopher Ferris7937a362018-01-18 11:15:49 -0800324 line += StringPrintf(" (load bias 0x%" PRIx64 ")", entry->load_bias);
Christopher Ferris2106f4b2015-05-01 15:02:03 -0700325 }
Christopher Ferrisa21bd932015-02-27 13:39:47 -0800326 _LOG(log, logtype::MAPS, "%s\n", line.c_str());
327 }
328 if (print_fault_address_marker) {
Christopher Ferris862fe022015-06-02 14:52:44 -0700329 _LOG(log, logtype::MAPS, "--->Fault address falls at %s after any mapped regions\n",
330 get_addr_string(addr).c_str());
Christopher Ferris20303f82014-01-10 16:33:16 -0800331 }
Jeff Brown053b8652012-06-06 16:25:03 -0700332}
333
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700334void dump_backtrace(log_t* log, std::vector<backtrace_frame_data_t>& frames, const char* prefix) {
335 for (auto& frame : frames) {
336 _LOG(log, logtype::BACKTRACE, "%s%s\n", prefix, Backtrace::FormatFrameData(&frame).c_str());
Christopher Ferrisab9e7dc2015-02-09 17:06:27 -0800337 }
Jeff Brown053b8652012-06-06 16:25:03 -0700338}
339
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700340static void print_register_row(log_t* log,
341 const std::vector<std::pair<std::string, uint64_t>>& registers) {
342 std::string output;
343 for (auto& [name, value] : registers) {
Christopher Ferris7937a362018-01-18 11:15:49 -0800344 output += android::base::StringPrintf(" %-3s %0*" PRIx64, name.c_str(),
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700345 static_cast<int>(2 * sizeof(void*)),
Christopher Ferris7937a362018-01-18 11:15:49 -0800346 static_cast<uint64_t>(value));
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700347 }
348
349 _LOG(log, logtype::REGISTERS, " %s\n", output.c_str());
Josh Gao77b00ed2017-05-05 18:11:23 -0700350}
351
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700352void dump_registers(log_t* log, Regs* regs) {
353 // Split lr/sp/pc into their own special row.
354 static constexpr size_t column_count = 4;
355 std::vector<std::pair<std::string, uint64_t>> current_row;
356 std::vector<std::pair<std::string, uint64_t>> special_row;
357
358#if defined(__arm__) || defined(__aarch64__)
359 static constexpr const char* special_registers[] = {"ip", "lr", "sp", "pc"};
360#elif defined(__i386__)
361 static constexpr const char* special_registers[] = {"ebp", "esp", "eip"};
362#elif defined(__x86_64__)
363 static constexpr const char* special_registers[] = {"rbp", "rsp", "rip"};
364#else
365 static constexpr const char* special_registers[] = {};
366#endif
367
368 regs->IterateRegisters([log, &current_row, &special_row](const char* name, uint64_t value) {
369 auto row = &current_row;
370 for (const char* special_name : special_registers) {
371 if (strcmp(special_name, name) == 0) {
372 row = &special_row;
373 break;
374 }
375 }
376
377 row->emplace_back(name, value);
378 if (current_row.size() == column_count) {
379 print_register_row(log, current_row);
380 current_row.clear();
381 }
382 });
383
384 if (!current_row.empty()) {
385 print_register_row(log, current_row);
386 }
387
388 print_register_row(log, special_row);
389}
390
Elliott Hughese1415a52018-02-15 09:18:21 -0800391void dump_memory_and_code(log_t* log, BacktraceMap* map, Memory* memory, Regs* regs) {
392 regs->IterateRegisters([log, map, memory](const char* reg_name, uint64_t reg_value) {
393 std::string label{"memory near "s + reg_name};
394 if (map) {
395 backtrace_map_t map_info;
396 map->FillIn(reg_value, &map_info);
397 std::string map_name{map_info.Name()};
398 if (!map_name.empty()) label += " (" + map_info.Name() + ")";
399 }
400 dump_memory(log, memory, reg_value, label);
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700401 });
402}
403
404static bool dump_thread(log_t* log, BacktraceMap* map, Memory* process_memory,
Christopher Ferris7937a362018-01-18 11:15:49 -0800405 const ThreadInfo& thread_info, uint64_t abort_msg_address,
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700406 bool primary_thread) {
407 UNUSED(process_memory);
408 log->current_tid = thread_info.tid;
Josh Gao7c89f9e2016-01-13 17:57:14 -0800409 if (!primary_thread) {
Brigid Smith62ba4892014-06-10 11:53:08 -0700410 _LOG(log, logtype::THREAD, "--- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---\n");
Josh Gao7c89f9e2016-01-13 17:57:14 -0800411 }
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700412 dump_thread_info(log, thread_info);
Christopher Ferris20303f82014-01-10 16:33:16 -0800413
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700414 if (thread_info.siginfo) {
415 dump_signal_info(log, thread_info.siginfo);
Josh Gao7c89f9e2016-01-13 17:57:14 -0800416 }
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700417
Josh Gao34c25562017-12-22 14:18:39 -0800418 if (primary_thread) {
419 dump_abort_message(log, process_memory, abort_msg_address);
420 }
421
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700422 dump_registers(log, thread_info.registers.get());
423
Josh Gao5d1c14f2018-04-20 12:04:41 -0700424 // Unwind will mutate the registers, so make a copy first.
425 std::unique_ptr<Regs> regs_copy(thread_info.registers->Clone());
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700426 std::vector<backtrace_frame_data_t> frames;
Josh Gao5d1c14f2018-04-20 12:04:41 -0700427 if (!Backtrace::Unwind(regs_copy.get(), map, &frames, 0, nullptr)) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700428 _LOG(log, logtype::THREAD, "Failed to unwind");
429 return false;
430 }
431
432 if (!frames.empty()) {
433 _LOG(log, logtype::BACKTRACE, "\nbacktrace:\n");
434 dump_backtrace(log, frames, " ");
435
436 _LOG(log, logtype::STACK, "\nstack:\n");
437 dump_stack(log, map, process_memory, frames);
Josh Gao7c89f9e2016-01-13 17:57:14 -0800438 }
Brigid Smith62ba4892014-06-10 11:53:08 -0700439
Josh Gao7c89f9e2016-01-13 17:57:14 -0800440 if (primary_thread) {
Elliott Hughese1415a52018-02-15 09:18:21 -0800441 dump_memory_and_code(log, map, process_memory, thread_info.registers.get());
Josh Gao7c89f9e2016-01-13 17:57:14 -0800442 if (map) {
Christopher Ferris7937a362018-01-18 11:15:49 -0800443 uint64_t addr = 0;
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700444 siginfo_t* si = thread_info.siginfo;
445 if (signal_has_si_addr(si->si_signo, si->si_code)) {
Christopher Ferris7937a362018-01-18 11:15:49 -0800446 addr = reinterpret_cast<uint64_t>(si->si_addr);
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700447 }
448 dump_all_maps(log, map, process_memory, addr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800449 }
450 }
451
Josh Gao7c89f9e2016-01-13 17:57:14 -0800452 log->current_tid = log->crashed_tid;
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700453 return true;
Jeff Brown053b8652012-06-06 16:25:03 -0700454}
455
Christopher Ferris20303f82014-01-10 16:33:16 -0800456// Reads the contents of the specified log device, filters out the entries
457// that don't match the specified pid, and writes them to the tombstone file.
458//
Mark Salyzyn17e85c02014-06-27 15:55:19 -0700459// If "tail" is non-zero, log the last "tail" number of lines.
Mark Salyzyn989980c2014-05-14 12:37:22 -0700460static EventTagMap* g_eventTagMap = NULL;
461
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700462static void dump_log_file(log_t* log, pid_t pid, const char* filename, unsigned int tail) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800463 bool first = true;
Mark Salyzyn17e85c02014-06-27 15:55:19 -0700464 struct logger_list* logger_list;
Jeff Brown053b8652012-06-06 16:25:03 -0700465
Mark Salyzyn45ae4462014-07-25 12:25:48 -0700466 if (!log->should_retrieve_logcat) {
467 return;
468 }
469
Mark Salyzyn22b5cef2013-11-22 10:53:34 -0800470 logger_list = android_logger_list_open(
Mark Salyzyn2d3f38a2015-01-26 10:46:44 -0800471 android_name_to_log_id(filename), ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK, tail, pid);
Jeff Brown053b8652012-06-06 16:25:03 -0700472
Mark Salyzyn22b5cef2013-11-22 10:53:34 -0800473 if (!logger_list) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700474 ALOGE("Unable to open %s: %s\n", filename, strerror(errno));
Christopher Ferris20303f82014-01-10 16:33:16 -0800475 return;
476 }
477
Mark Salyzyn22b5cef2013-11-22 10:53:34 -0800478 struct log_msg log_entry;
Christopher Ferris20303f82014-01-10 16:33:16 -0800479
480 while (true) {
Mark Salyzyn22b5cef2013-11-22 10:53:34 -0800481 ssize_t actual = android_logger_list_read(logger_list, &log_entry);
Mark Salyzyn17e85c02014-06-27 15:55:19 -0700482 struct logger_entry* entry;
Mark Salyzyn22b5cef2013-11-22 10:53:34 -0800483
Christopher Ferris20303f82014-01-10 16:33:16 -0800484 if (actual < 0) {
Mark Salyzyn22b5cef2013-11-22 10:53:34 -0800485 if (actual == -EINTR) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800486 // interrupted by signal, retry
487 continue;
Mark Salyzyn22b5cef2013-11-22 10:53:34 -0800488 } else if (actual == -EAGAIN) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800489 // non-blocking EOF; we're done
490 break;
491 } else {
Christopher Ferrisb36b5922015-06-17 18:35:59 -0700492 ALOGE("Error while reading log: %s\n", strerror(-actual));
Christopher Ferris20303f82014-01-10 16:33:16 -0800493 break;
494 }
495 } else if (actual == 0) {
Christopher Ferrisb36b5922015-06-17 18:35:59 -0700496 ALOGE("Got zero bytes while reading log: %s\n", strerror(errno));
Christopher Ferris20303f82014-01-10 16:33:16 -0800497 break;
Jeff Brown053b8652012-06-06 16:25:03 -0700498 }
499
Brigid Smith50eb5462014-06-18 14:17:57 -0700500 // NOTE: if you ALOGV something here, this will spin forever,
Christopher Ferris20303f82014-01-10 16:33:16 -0800501 // because you will be writing as fast as you're reading. Any
502 // high-frequency debug diagnostics should just be written to
503 // the tombstone file.
Jeff Brown053b8652012-06-06 16:25:03 -0700504
Mark Salyzyn17e85c02014-06-27 15:55:19 -0700505 entry = &log_entry.entry_v1;
Jeff Brown053b8652012-06-06 16:25:03 -0700506
Christopher Ferris20303f82014-01-10 16:33:16 -0800507 if (first) {
Mark Salyzyne43290d2014-06-27 10:32:22 -0700508 _LOG(log, logtype::LOGS, "--------- %slog %s\n",
Mark Salyzyn22b5cef2013-11-22 10:53:34 -0800509 tail ? "tail end of " : "", filename);
Christopher Ferris20303f82014-01-10 16:33:16 -0800510 first = false;
511 }
512
513 // Msg format is: <priority:1><tag:N>\0<message:N>\0
514 //
515 // We want to display it in the same format as "logcat -v threadtime"
516 // (although in this case the pid is redundant).
Christopher Ferris20303f82014-01-10 16:33:16 -0800517 static const char* kPrioChars = "!.VDIWEFS";
Mark Salyzyn22b5cef2013-11-22 10:53:34 -0800518 unsigned hdr_size = log_entry.entry.hdr_size;
519 if (!hdr_size) {
520 hdr_size = sizeof(log_entry.entry_v1);
521 }
Mark Salyzyn305374c2016-08-18 14:59:41 -0700522 if ((hdr_size < sizeof(log_entry.entry_v1)) ||
523 (hdr_size > sizeof(log_entry.entry))) {
524 continue;
525 }
Mark Salyzyn989980c2014-05-14 12:37:22 -0700526 char* msg = reinterpret_cast<char*>(log_entry.buf) + hdr_size;
527
528 char timeBuf[32];
529 time_t sec = static_cast<time_t>(entry->sec);
530 struct tm tmBuf;
531 struct tm* ptm;
532 ptm = localtime_r(&sec, &tmBuf);
533 strftime(timeBuf, sizeof(timeBuf), "%m-%d %H:%M:%S", ptm);
534
535 if (log_entry.id() == LOG_ID_EVENTS) {
536 if (!g_eventTagMap) {
Mark Salyzyn1179eb82016-11-11 09:48:56 -0800537 g_eventTagMap = android_openEventTagMap(NULL);
Mark Salyzyn989980c2014-05-14 12:37:22 -0700538 }
539 AndroidLogEntry e;
540 char buf[512];
541 android_log_processBinaryLogBuffer(entry, &e, g_eventTagMap, buf, sizeof(buf));
Mark Salyzyn9f53cac2016-10-24 13:11:46 -0700542 _LOG(log, logtype::LOGS, "%s.%03d %5d %5d %c %-8.*s: %s\n",
Mark Salyzyn989980c2014-05-14 12:37:22 -0700543 timeBuf, entry->nsec / 1000000, entry->pid, entry->tid,
Mark Salyzyn9f53cac2016-10-24 13:11:46 -0700544 'I', (int)e.tagLen, e.tag, e.message);
Mark Salyzyn989980c2014-05-14 12:37:22 -0700545 continue;
546 }
547
Mark Salyzyn22b5cef2013-11-22 10:53:34 -0800548 unsigned char prio = msg[0];
549 char* tag = msg + 1;
550 msg = tag + strlen(tag) + 1;
Christopher Ferris20303f82014-01-10 16:33:16 -0800551
552 // consume any trailing newlines
Mark Salyzynfca0bd12013-12-12 12:21:20 -0800553 char* nl = msg + strlen(msg) - 1;
554 while (nl >= msg && *nl == '\n') {
Mark Salyzyn17e85c02014-06-27 15:55:19 -0700555 *nl-- = '\0';
Christopher Ferris20303f82014-01-10 16:33:16 -0800556 }
557
558 char prioChar = (prio < strlen(kPrioChars) ? kPrioChars[prio] : '?');
559
Mark Salyzynfca0bd12013-12-12 12:21:20 -0800560 // Look for line breaks ('\n') and display each text line
561 // on a separate line, prefixed with the header, like logcat does.
562 do {
563 nl = strchr(msg, '\n');
564 if (nl) {
565 *nl = '\0';
566 ++nl;
567 }
568
Brigid Smith62ba4892014-06-10 11:53:08 -0700569 _LOG(log, logtype::LOGS, "%s.%03d %5d %5d %c %-8s: %s\n",
Mark Salyzynfca0bd12013-12-12 12:21:20 -0800570 timeBuf, entry->nsec / 1000000, entry->pid, entry->tid,
571 prioChar, tag, msg);
Mark Salyzynfca0bd12013-12-12 12:21:20 -0800572 } while ((msg = nl));
Christopher Ferris20303f82014-01-10 16:33:16 -0800573 }
Jeff Brown053b8652012-06-06 16:25:03 -0700574
Mark Salyzyn22b5cef2013-11-22 10:53:34 -0800575 android_logger_list_free(logger_list);
Jeff Brown053b8652012-06-06 16:25:03 -0700576}
577
Christopher Ferris20303f82014-01-10 16:33:16 -0800578// Dumps the logs generated by the specified pid to the tombstone, from both
579// "system" and "main" log devices. Ideally we'd interleave the output.
Mark Salyzyn17e85c02014-06-27 15:55:19 -0700580static void dump_logs(log_t* log, pid_t pid, unsigned int tail) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700581 if (pid == getpid()) {
582 // Cowardly refuse to dump logs while we're running in-process.
583 return;
584 }
585
Mark Salyzyn22b5cef2013-11-22 10:53:34 -0800586 dump_log_file(log, pid, "system", tail);
587 dump_log_file(log, pid, "main", tail);
Jeff Brown053b8652012-06-06 16:25:03 -0700588}
589
Christopher Ferris7937a362018-01-18 11:15:49 -0800590void engrave_tombstone_ucontext(int tombstone_fd, uint64_t abort_msg_address, siginfo_t* siginfo,
Josh Gaoe1aa0ca2017-03-01 17:23:22 -0800591 ucontext_t* ucontext) {
592 pid_t pid = getpid();
593 pid_t tid = gettid();
594
Josh Gaoe73c9322017-02-08 16:06:26 -0800595 log_t log;
596 log.current_tid = tid;
597 log.crashed_tid = tid;
598 log.tfd = tombstone_fd;
599 log.amfd_data = nullptr;
600
Josh Gao57f58f82017-03-15 23:23:22 -0700601 char thread_name[16];
602 char process_name[128];
603
604 read_with_default("/proc/self/comm", thread_name, sizeof(thread_name), "<unknown>");
605 read_with_default("/proc/self/cmdline", process_name, sizeof(process_name), "<unknown>");
606
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700607 std::unique_ptr<Regs> regs(Regs::CreateFromUcontext(Regs::CurrentArch(), ucontext));
Josh Gaoe73c9322017-02-08 16:06:26 -0800608
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700609 std::map<pid_t, ThreadInfo> threads;
610 threads[gettid()] = ThreadInfo{
611 .registers = std::move(regs),
612 .tid = tid,
613 .thread_name = thread_name,
614 .pid = pid,
615 .process_name = process_name,
616 .siginfo = siginfo,
617 };
Josh Gao77b00ed2017-05-05 18:11:23 -0700618
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700619 std::unique_ptr<BacktraceMap> backtrace_map(BacktraceMap::Create(getpid(), false));
620 if (!backtrace_map) {
621 ALOGE("failed to create backtrace map");
622 _exit(1);
Josh Gaoe73c9322017-02-08 16:06:26 -0800623 }
Josh Gaofdc95c92017-09-13 15:33:39 -0700624
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700625 std::shared_ptr<Memory> process_memory = backtrace_map->GetProcessMemory();
626 engrave_tombstone(unique_fd(dup(tombstone_fd)), backtrace_map.get(), process_memory.get(),
627 threads, tid, abort_msg_address, nullptr, nullptr);
628}
629
630void engrave_tombstone(unique_fd output_fd, BacktraceMap* map, Memory* process_memory,
631 const std::map<pid_t, ThreadInfo>& threads, pid_t target_thread,
Christopher Ferris7937a362018-01-18 11:15:49 -0800632 uint64_t abort_msg_address, OpenFilesList* open_files,
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700633 std::string* amfd_data) {
634 // don't copy log messages to tombstone unless this is a dev device
635 bool want_logs = android::base::GetBoolProperty("ro.debuggable", false);
636
637 log_t log;
638 log.current_tid = target_thread;
639 log.crashed_tid = target_thread;
640 log.tfd = output_fd.get();
641 log.amfd_data = amfd_data;
642
643 _LOG(&log, logtype::HEADER, "*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***\n");
644 dump_header_info(&log);
645
646 auto it = threads.find(target_thread);
647 if (it == threads.end()) {
648 LOG(FATAL) << "failed to find target thread";
649 }
650 dump_thread(&log, map, process_memory, it->second, abort_msg_address, true);
651
652 if (want_logs) {
chirag honnavardef08882017-04-20 18:22:06 +0900653 dump_logs(&log, it->second.pid, 50);
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700654 }
655
656 for (auto& [tid, thread_info] : threads) {
657 if (tid == target_thread) {
658 continue;
Josh Gaofdc95c92017-09-13 15:33:39 -0700659 }
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700660
661 dump_thread(&log, map, process_memory, thread_info, 0, false);
662 }
663
664 if (open_files) {
665 _LOG(&log, logtype::OPEN_FILES, "\nopen files:\n");
666 dump_open_files_list(&log, *open_files, " ");
667 }
668
669 if (want_logs) {
670 dump_logs(&log, it->second.pid, 0);
Josh Gaofdc95c92017-09-13 15:33:39 -0700671 }
Josh Gaoe73c9322017-02-08 16:06:26 -0800672}