blob: 624637a882c93db64558d334ec84b213f711e9f5 [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
Jeff Brown053b8652012-06-06 16:25:03 -070068#define STACK_WORDS 16
69
Brigid Smith9c8dacc2014-06-02 15:02:20 -070070static void dump_header_info(log_t* log) {
Tom Cherry31121ca2017-10-10 13:30:57 -070071 auto fingerprint = GetProperty("ro.build.fingerprint", "unknown");
72 auto revision = GetProperty("ro.revision", "unknown");
Ben Chengd7760c12012-09-19 16:04:01 -070073
Tom Cherry31121ca2017-10-10 13:30:57 -070074 _LOG(log, logtype::HEADER, "Build fingerprint: '%s'\n", fingerprint.c_str());
75 _LOG(log, logtype::HEADER, "Revision: '%s'\n", revision.c_str());
Brigid Smith62ba4892014-06-10 11:53:08 -070076 _LOG(log, logtype::HEADER, "ABI: '%s'\n", ABI_STRING);
Jeff Brown053b8652012-06-06 16:25:03 -070077}
78
Josh Gao2b2ae0c2017-08-21 14:31:17 -070079static void dump_probable_cause(log_t* log, const siginfo_t* si) {
Elliott Hughes0ba53592017-02-01 16:59:15 -080080 std::string cause;
Josh Gao2b2ae0c2017-08-21 14:31:17 -070081 if (si->si_signo == SIGSEGV && si->si_code == SEGV_MAPERR) {
82 if (si->si_addr < reinterpret_cast<void*>(4096)) {
Elliott Hughes0ba53592017-02-01 16:59:15 -080083 cause = StringPrintf("null pointer dereference");
Josh Gao2b2ae0c2017-08-21 14:31:17 -070084 } else if (si->si_addr == reinterpret_cast<void*>(0xffff0ffc)) {
Elliott Hughes0ba53592017-02-01 16:59:15 -080085 cause = "call to kuser_helper_version";
Josh Gao2b2ae0c2017-08-21 14:31:17 -070086 } else if (si->si_addr == reinterpret_cast<void*>(0xffff0fe0)) {
Elliott Hughes0ba53592017-02-01 16:59:15 -080087 cause = "call to kuser_get_tls";
Josh Gao2b2ae0c2017-08-21 14:31:17 -070088 } else if (si->si_addr == reinterpret_cast<void*>(0xffff0fc0)) {
Elliott Hughes0ba53592017-02-01 16:59:15 -080089 cause = "call to kuser_cmpxchg";
Josh Gao2b2ae0c2017-08-21 14:31:17 -070090 } else if (si->si_addr == reinterpret_cast<void*>(0xffff0fa0)) {
Elliott Hughes0ba53592017-02-01 16:59:15 -080091 cause = "call to kuser_memory_barrier";
Josh Gao2b2ae0c2017-08-21 14:31:17 -070092 } else if (si->si_addr == reinterpret_cast<void*>(0xffff0f60)) {
Elliott Hughes0ba53592017-02-01 16:59:15 -080093 cause = "call to kuser_cmpxchg64";
94 }
Josh Gao2b2ae0c2017-08-21 14:31:17 -070095 } else if (si->si_signo == SIGSYS && si->si_code == SYS_SECCOMP) {
96 cause = StringPrintf("seccomp prevented call to disallowed %s system call %d", ABI_STRING,
97 si->si_syscall);
Elliott Hughes0ba53592017-02-01 16:59:15 -080098 }
99
100 if (!cause.empty()) _LOG(log, logtype::HEADER, "Cause: %s\n", cause.c_str());
101}
102
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700103static void dump_signal_info(log_t* log, const siginfo_t* si) {
Elliott Hughes855fcc32014-04-25 16:05:34 -0700104 char addr_desc[32]; // ", fault addr 0x1234"
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700105 if (signal_has_si_addr(si->si_signo, si->si_code)) {
106 snprintf(addr_desc, sizeof(addr_desc), "%p", si->si_addr);
Elliott Hughes855fcc32014-04-25 16:05:34 -0700107 } else {
108 snprintf(addr_desc, sizeof(addr_desc), "--------");
109 }
110
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700111 _LOG(log, logtype::HEADER, "signal %d (%s), code %d (%s), fault addr %s\n", si->si_signo,
112 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 -0800113
114 dump_probable_cause(log, si);
Jeff Brown053b8652012-06-06 16:25:03 -0700115}
116
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700117static void dump_thread_info(log_t* log, const ThreadInfo& thread_info) {
Mark Salyzyn45ae4462014-07-25 12:25:48 -0700118 // Blacklist logd, logd.reader, logd.writer, logd.auditd, logd.control ...
Josh Gao57f58f82017-03-15 23:23:22 -0700119 // TODO: Why is this controlled by thread name?
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700120 if (thread_info.thread_name == "logd" ||
121 android::base::StartsWith(thread_info.thread_name, "logd.")) {
Mark Salyzyn45ae4462014-07-25 12:25:48 -0700122 log->should_retrieve_logcat = false;
123 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800124
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700125 _LOG(log, logtype::HEADER, "pid: %d, tid: %d, name: %s >>> %s <<<\n", thread_info.pid,
126 thread_info.tid, thread_info.thread_name.c_str(), thread_info.process_name.c_str());
Christopher Ferris20303f82014-01-10 16:33:16 -0800127}
Jeff Brown053b8652012-06-06 16:25:03 -0700128
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700129static void dump_stack_segment(log_t* log, BacktraceMap* backtrace_map, Memory* process_memory,
130 uintptr_t* sp, size_t words, int label) {
Christopher Ferrisa21bd932015-02-27 13:39:47 -0800131 // Read the data all at once.
132 word_t stack_data[words];
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700133
134 // TODO: Do we need to word align this for crashes caused by a misaligned sp?
135 // The process_vm_readv implementation of Memory should handle this appropriately?
136 size_t bytes_read = process_memory->Read(*sp, stack_data, sizeof(word_t) * words);
Christopher Ferrisa21bd932015-02-27 13:39:47 -0800137 words = bytes_read / sizeof(word_t);
138 std::string line;
Christopher Ferris20303f82014-01-10 16:33:16 -0800139 for (size_t i = 0; i < words; i++) {
Christopher Ferrisa21bd932015-02-27 13:39:47 -0800140 line = " ";
141 if (i == 0 && label >= 0) {
142 // Print the label once.
Elliott Hughes0ba53592017-02-01 16:59:15 -0800143 line += StringPrintf("#%02d ", label);
Christopher Ferrisa21bd932015-02-27 13:39:47 -0800144 } else {
145 line += " ";
Christopher Ferris20303f82014-01-10 16:33:16 -0800146 }
Elliott Hughes0ba53592017-02-01 16:59:15 -0800147 line += StringPrintf("%" PRIPTR " %" PRIPTR, *sp, stack_data[i]);
Christopher Ferris20303f82014-01-10 16:33:16 -0800148
Christopher Ferris12385e32015-02-06 13:22:01 -0800149 backtrace_map_t map;
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700150 backtrace_map->FillIn(stack_data[i], &map);
Christopher Ferrisa21bd932015-02-27 13:39:47 -0800151 if (BacktraceMap::IsValid(map) && !map.name.empty()) {
152 line += " " + map.name;
153 uintptr_t offset = 0;
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700154 std::string func_name = backtrace_map->GetFunctionName(stack_data[i], &offset);
Christopher Ferrisa21bd932015-02-27 13:39:47 -0800155 if (!func_name.empty()) {
156 line += " (" + func_name;
Christopher Ferris20303f82014-01-10 16:33:16 -0800157 if (offset) {
Elliott Hughes0ba53592017-02-01 16:59:15 -0800158 line += StringPrintf("+%" PRIuPTR, offset);
Jeff Brown053b8652012-06-06 16:25:03 -0700159 }
Christopher Ferrisa21bd932015-02-27 13:39:47 -0800160 line += ')';
Christopher Ferris20303f82014-01-10 16:33:16 -0800161 }
Jeff Brown053b8652012-06-06 16:25:03 -0700162 }
Christopher Ferrisa21bd932015-02-27 13:39:47 -0800163 _LOG(log, logtype::STACK, "%s\n", line.c_str());
Christopher Ferris20303f82014-01-10 16:33:16 -0800164
Pavel Chupinc6c194c2013-11-21 23:17:20 +0400165 *sp += sizeof(word_t);
Christopher Ferris20303f82014-01-10 16:33:16 -0800166 }
Jeff Brown053b8652012-06-06 16:25:03 -0700167}
168
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700169static void dump_stack(log_t* log, BacktraceMap* backtrace_map, Memory* process_memory,
170 std::vector<backtrace_frame_data_t>& frames) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800171 size_t first = 0, last;
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700172 for (size_t i = 0; i < frames.size(); i++) {
173 const backtrace_frame_data_t& frame = frames[i];
174 if (frame.sp) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800175 if (!first) {
176 first = i+1;
177 }
178 last = i;
Jeff Brown053b8652012-06-06 16:25:03 -0700179 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800180 }
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700181
Christopher Ferris20303f82014-01-10 16:33:16 -0800182 if (!first) {
183 return;
184 }
185 first--;
186
Christopher Ferris20303f82014-01-10 16:33:16 -0800187 // Dump a few words before the first frame.
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700188 word_t sp = frames[first].sp - STACK_WORDS * sizeof(word_t);
189 dump_stack_segment(log, backtrace_map, process_memory, &sp, STACK_WORDS, -1);
Christopher Ferris20303f82014-01-10 16:33:16 -0800190
191 // Dump a few words from all successive frames.
192 // Only log the first 3 frames, put the rest in the tombstone.
193 for (size_t i = first; i <= last; i++) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700194 const backtrace_frame_data_t* frame = &frames[i];
Christopher Ferris20303f82014-01-10 16:33:16 -0800195 if (sp != frame->sp) {
Brigid Smith62ba4892014-06-10 11:53:08 -0700196 _LOG(log, logtype::STACK, " ........ ........\n");
Christopher Ferris20303f82014-01-10 16:33:16 -0800197 sp = frame->sp;
198 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800199 if (i == last) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700200 dump_stack_segment(log, backtrace_map, process_memory, &sp, STACK_WORDS, i);
Christopher Ferris20303f82014-01-10 16:33:16 -0800201 if (sp < frame->sp + frame->stack_size) {
Brigid Smith62ba4892014-06-10 11:53:08 -0700202 _LOG(log, logtype::STACK, " ........ ........\n");
Christopher Ferris20303f82014-01-10 16:33:16 -0800203 }
204 } else {
Pavel Chupinc6c194c2013-11-21 23:17:20 +0400205 size_t words = frame->stack_size / sizeof(word_t);
Christopher Ferris20303f82014-01-10 16:33:16 -0800206 if (words == 0) {
207 words = 1;
208 } else if (words > STACK_WORDS) {
209 words = STACK_WORDS;
210 }
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700211 dump_stack_segment(log, backtrace_map, process_memory, &sp, words, i);
Christopher Ferris20303f82014-01-10 16:33:16 -0800212 }
213 }
Jeff Brown053b8652012-06-06 16:25:03 -0700214}
215
Christopher Ferris862fe022015-06-02 14:52:44 -0700216static std::string get_addr_string(uintptr_t addr) {
217 std::string addr_str;
218#if defined(__LP64__)
Elliott Hughes0ba53592017-02-01 16:59:15 -0800219 addr_str = StringPrintf("%08x'%08x",
220 static_cast<uint32_t>(addr >> 32),
221 static_cast<uint32_t>(addr & 0xffffffff));
Christopher Ferris862fe022015-06-02 14:52:44 -0700222#else
Elliott Hughes0ba53592017-02-01 16:59:15 -0800223 addr_str = StringPrintf("%08x", addr);
Christopher Ferris862fe022015-06-02 14:52:44 -0700224#endif
225 return addr_str;
226}
227
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700228static void dump_abort_message(log_t* log, Memory* process_memory, uintptr_t address) {
Josh Gao7c89f9e2016-01-13 17:57:14 -0800229 if (address == 0) {
230 return;
231 }
232
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700233 size_t length;
234 if (!process_memory->ReadFully(address, &length, sizeof(length))) {
235 _LOG(log, logtype::HEADER, "Failed to read abort message header: %s\n", strerror(errno));
236 return;
237 }
Josh Gao7c89f9e2016-01-13 17:57:14 -0800238
239 char msg[512];
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700240 if (length >= sizeof(msg)) {
241 _LOG(log, logtype::HEADER, "Abort message too long: claimed length = %zd\n", length);
242 return;
Josh Gao7c89f9e2016-01-13 17:57:14 -0800243 }
Josh Gao7c89f9e2016-01-13 17:57:14 -0800244
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700245 if (!process_memory->ReadFully(address + sizeof(length), msg, length)) {
246 _LOG(log, logtype::HEADER, "Failed to read abort message: %s\n", strerror(errno));
247 return;
248 }
249
250 msg[length] = '\0';
Josh Gao7c89f9e2016-01-13 17:57:14 -0800251 _LOG(log, logtype::HEADER, "Abort message: '%s'\n", msg);
252}
253
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700254static void dump_all_maps(log_t* log, BacktraceMap* map, Memory* process_memory, uintptr_t addr) {
255 bool print_fault_address_marker = addr;
Christopher Ferris20303f82014-01-10 16:33:16 -0800256
Christopher Ferris3a140042016-06-15 15:49:50 -0700257 ScopedBacktraceMapIteratorLock lock(map);
Elliott Hughes868d39a2017-09-26 11:54:49 -0700258 _LOG(log, logtype::MAPS,
259 "\n"
Josh Gao1ce8e142017-09-27 13:59:42 -0700260 "memory map (%zu entr%s):",
261 map->size(), map->size() == 1 ? "y" : "ies");
Elliott Hughes868d39a2017-09-26 11:54:49 -0700262 if (print_fault_address_marker) {
Christopher Ferrisb7de5f52017-12-01 21:37:37 -0800263 if (map->begin() != map->end() && addr < (*map->begin())->start) {
Josh Gao1ce8e142017-09-27 13:59:42 -0700264 _LOG(log, logtype::MAPS, "\n--->Fault address falls at %s before any mapped regions\n",
Christopher Ferris862fe022015-06-02 14:52:44 -0700265 get_addr_string(addr).c_str());
Christopher Ferrisa21bd932015-02-27 13:39:47 -0800266 print_fault_address_marker = false;
Elliott Hughes868d39a2017-09-26 11:54:49 -0700267 } else {
Josh Gao1ce8e142017-09-27 13:59:42 -0700268 _LOG(log, logtype::MAPS, " (fault address prefixed with --->)\n");
Brigid Smith8606eaa2014-07-07 12:33:50 -0700269 }
Josh Gao1ce8e142017-09-27 13:59:42 -0700270 } else {
271 _LOG(log, logtype::MAPS, "\n");
Brigid Smith8606eaa2014-07-07 12:33:50 -0700272 }
Christopher Ferrisa21bd932015-02-27 13:39:47 -0800273
274 std::string line;
Christopher Ferrisb7de5f52017-12-01 21:37:37 -0800275 for (auto it = map->begin(); it != map->end(); ++it) {
276 const backtrace_map_t* entry = *it;
Christopher Ferrisa21bd932015-02-27 13:39:47 -0800277 line = " ";
278 if (print_fault_address_marker) {
Christopher Ferrisb7de5f52017-12-01 21:37:37 -0800279 if (addr < entry->start) {
Christopher Ferris862fe022015-06-02 14:52:44 -0700280 _LOG(log, logtype::MAPS, "--->Fault address falls at %s between mapped regions\n",
281 get_addr_string(addr).c_str());
Christopher Ferrisa21bd932015-02-27 13:39:47 -0800282 print_fault_address_marker = false;
Christopher Ferrisb7de5f52017-12-01 21:37:37 -0800283 } else if (addr >= entry->start && addr < entry->end) {
Christopher Ferrisa21bd932015-02-27 13:39:47 -0800284 line = "--->";
285 print_fault_address_marker = false;
286 }
287 }
Christopher Ferrisb7de5f52017-12-01 21:37:37 -0800288 line += get_addr_string(entry->start) + '-' + get_addr_string(entry->end - 1) + ' ';
289 if (entry->flags & PROT_READ) {
Christopher Ferrisa21bd932015-02-27 13:39:47 -0800290 line += 'r';
291 } else {
292 line += '-';
293 }
Christopher Ferrisb7de5f52017-12-01 21:37:37 -0800294 if (entry->flags & PROT_WRITE) {
Christopher Ferrisa21bd932015-02-27 13:39:47 -0800295 line += 'w';
296 } else {
297 line += '-';
298 }
Christopher Ferrisb7de5f52017-12-01 21:37:37 -0800299 if (entry->flags & PROT_EXEC) {
Christopher Ferrisa21bd932015-02-27 13:39:47 -0800300 line += 'x';
301 } else {
302 line += '-';
303 }
Christopher Ferrisb7de5f52017-12-01 21:37:37 -0800304 line += StringPrintf(" %8" PRIxPTR " %8" PRIxPTR, entry->offset, entry->end - entry->start);
Christopher Ferris862fe022015-06-02 14:52:44 -0700305 bool space_needed = true;
Christopher Ferrisb7de5f52017-12-01 21:37:37 -0800306 if (entry->name.length() > 0) {
Christopher Ferris862fe022015-06-02 14:52:44 -0700307 space_needed = false;
Christopher Ferrisb7de5f52017-12-01 21:37:37 -0800308 line += " " + entry->name;
Christopher Ferrisa21bd932015-02-27 13:39:47 -0800309 std::string build_id;
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700310 if ((entry->flags & PROT_READ) && elf_get_build_id(process_memory, entry->start, &build_id)) {
Christopher Ferrisa21bd932015-02-27 13:39:47 -0800311 line += " (BuildId: " + build_id + ")";
312 }
313 }
Christopher Ferrisb7de5f52017-12-01 21:37:37 -0800314 if (entry->load_bias != 0) {
Christopher Ferris862fe022015-06-02 14:52:44 -0700315 if (space_needed) {
316 line += ' ';
317 }
Christopher Ferrisb7de5f52017-12-01 21:37:37 -0800318 line += StringPrintf(" (load bias 0x%" PRIxPTR ")", entry->load_bias);
Christopher Ferris2106f4b2015-05-01 15:02:03 -0700319 }
Christopher Ferrisa21bd932015-02-27 13:39:47 -0800320 _LOG(log, logtype::MAPS, "%s\n", line.c_str());
321 }
322 if (print_fault_address_marker) {
Christopher Ferris862fe022015-06-02 14:52:44 -0700323 _LOG(log, logtype::MAPS, "--->Fault address falls at %s after any mapped regions\n",
324 get_addr_string(addr).c_str());
Christopher Ferris20303f82014-01-10 16:33:16 -0800325 }
Jeff Brown053b8652012-06-06 16:25:03 -0700326}
327
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700328void dump_backtrace(log_t* log, std::vector<backtrace_frame_data_t>& frames, const char* prefix) {
329 for (auto& frame : frames) {
330 _LOG(log, logtype::BACKTRACE, "%s%s\n", prefix, Backtrace::FormatFrameData(&frame).c_str());
Christopher Ferrisab9e7dc2015-02-09 17:06:27 -0800331 }
Jeff Brown053b8652012-06-06 16:25:03 -0700332}
333
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700334static void print_register_row(log_t* log,
335 const std::vector<std::pair<std::string, uint64_t>>& registers) {
336 std::string output;
337 for (auto& [name, value] : registers) {
338 output += android::base::StringPrintf(" %-3s %0*" PRIxPTR, name.c_str(),
339 static_cast<int>(2 * sizeof(void*)),
340 static_cast<uintptr_t>(value));
341 }
342
343 _LOG(log, logtype::REGISTERS, " %s\n", output.c_str());
Josh Gao77b00ed2017-05-05 18:11:23 -0700344}
345
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700346void dump_registers(log_t* log, Regs* regs) {
347 // Split lr/sp/pc into their own special row.
348 static constexpr size_t column_count = 4;
349 std::vector<std::pair<std::string, uint64_t>> current_row;
350 std::vector<std::pair<std::string, uint64_t>> special_row;
351
352#if defined(__arm__) || defined(__aarch64__)
353 static constexpr const char* special_registers[] = {"ip", "lr", "sp", "pc"};
354#elif defined(__i386__)
355 static constexpr const char* special_registers[] = {"ebp", "esp", "eip"};
356#elif defined(__x86_64__)
357 static constexpr const char* special_registers[] = {"rbp", "rsp", "rip"};
358#else
359 static constexpr const char* special_registers[] = {};
360#endif
361
362 regs->IterateRegisters([log, &current_row, &special_row](const char* name, uint64_t value) {
363 auto row = &current_row;
364 for (const char* special_name : special_registers) {
365 if (strcmp(special_name, name) == 0) {
366 row = &special_row;
367 break;
368 }
369 }
370
371 row->emplace_back(name, value);
372 if (current_row.size() == column_count) {
373 print_register_row(log, current_row);
374 current_row.clear();
375 }
376 });
377
378 if (!current_row.empty()) {
379 print_register_row(log, current_row);
380 }
381
382 print_register_row(log, special_row);
383}
384
385void dump_memory_and_code(log_t* log, Memory* memory, Regs* regs) {
386 regs->IterateRegisters([log, memory](const char* name, uint64_t value) {
387 dump_memory(log, memory, value, "memory near %s:", name);
388 });
389}
390
391static bool dump_thread(log_t* log, BacktraceMap* map, Memory* process_memory,
392 const ThreadInfo& thread_info, uintptr_t abort_msg_address,
393 bool primary_thread) {
394 UNUSED(process_memory);
395 log->current_tid = thread_info.tid;
Josh Gao7c89f9e2016-01-13 17:57:14 -0800396 if (!primary_thread) {
Brigid Smith62ba4892014-06-10 11:53:08 -0700397 _LOG(log, logtype::THREAD, "--- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---\n");
Josh Gao7c89f9e2016-01-13 17:57:14 -0800398 }
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700399 dump_thread_info(log, thread_info);
Christopher Ferris20303f82014-01-10 16:33:16 -0800400
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700401 if (thread_info.siginfo) {
402 dump_signal_info(log, thread_info.siginfo);
Josh Gao7c89f9e2016-01-13 17:57:14 -0800403 }
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700404
405 dump_registers(log, thread_info.registers.get());
406
407 std::vector<backtrace_frame_data_t> frames;
408 if (!Backtrace::Unwind(thread_info.registers.get(), map, &frames, 0, nullptr)) {
409 _LOG(log, logtype::THREAD, "Failed to unwind");
410 return false;
411 }
412
413 if (!frames.empty()) {
414 _LOG(log, logtype::BACKTRACE, "\nbacktrace:\n");
415 dump_backtrace(log, frames, " ");
416
417 _LOG(log, logtype::STACK, "\nstack:\n");
418 dump_stack(log, map, process_memory, frames);
Josh Gao7c89f9e2016-01-13 17:57:14 -0800419 }
Brigid Smith62ba4892014-06-10 11:53:08 -0700420
Josh Gao7c89f9e2016-01-13 17:57:14 -0800421 if (primary_thread) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700422 dump_abort_message(log, process_memory, abort_msg_address);
423 }
424
425 if (primary_thread) {
426 dump_memory_and_code(log, process_memory, thread_info.registers.get());
Josh Gao7c89f9e2016-01-13 17:57:14 -0800427 if (map) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700428 uintptr_t addr = 0;
429 siginfo_t* si = thread_info.siginfo;
430 if (signal_has_si_addr(si->si_signo, si->si_code)) {
431 addr = reinterpret_cast<uintptr_t>(si->si_addr);
432 }
433 dump_all_maps(log, map, process_memory, addr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800434 }
435 }
436
Josh Gao7c89f9e2016-01-13 17:57:14 -0800437 log->current_tid = log->crashed_tid;
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700438 return true;
Jeff Brown053b8652012-06-06 16:25:03 -0700439}
440
Christopher Ferris20303f82014-01-10 16:33:16 -0800441// Reads the contents of the specified log device, filters out the entries
442// that don't match the specified pid, and writes them to the tombstone file.
443//
Mark Salyzyn17e85c02014-06-27 15:55:19 -0700444// If "tail" is non-zero, log the last "tail" number of lines.
Mark Salyzyn989980c2014-05-14 12:37:22 -0700445static EventTagMap* g_eventTagMap = NULL;
446
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700447static void dump_log_file(log_t* log, pid_t pid, const char* filename, unsigned int tail) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800448 bool first = true;
Mark Salyzyn17e85c02014-06-27 15:55:19 -0700449 struct logger_list* logger_list;
Jeff Brown053b8652012-06-06 16:25:03 -0700450
Mark Salyzyn45ae4462014-07-25 12:25:48 -0700451 if (!log->should_retrieve_logcat) {
452 return;
453 }
454
Mark Salyzyn22b5cef2013-11-22 10:53:34 -0800455 logger_list = android_logger_list_open(
Mark Salyzyn2d3f38a2015-01-26 10:46:44 -0800456 android_name_to_log_id(filename), ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK, tail, pid);
Jeff Brown053b8652012-06-06 16:25:03 -0700457
Mark Salyzyn22b5cef2013-11-22 10:53:34 -0800458 if (!logger_list) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700459 ALOGE("Unable to open %s: %s\n", filename, strerror(errno));
Christopher Ferris20303f82014-01-10 16:33:16 -0800460 return;
461 }
462
Mark Salyzyn22b5cef2013-11-22 10:53:34 -0800463 struct log_msg log_entry;
Christopher Ferris20303f82014-01-10 16:33:16 -0800464
465 while (true) {
Mark Salyzyn22b5cef2013-11-22 10:53:34 -0800466 ssize_t actual = android_logger_list_read(logger_list, &log_entry);
Mark Salyzyn17e85c02014-06-27 15:55:19 -0700467 struct logger_entry* entry;
Mark Salyzyn22b5cef2013-11-22 10:53:34 -0800468
Christopher Ferris20303f82014-01-10 16:33:16 -0800469 if (actual < 0) {
Mark Salyzyn22b5cef2013-11-22 10:53:34 -0800470 if (actual == -EINTR) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800471 // interrupted by signal, retry
472 continue;
Mark Salyzyn22b5cef2013-11-22 10:53:34 -0800473 } else if (actual == -EAGAIN) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800474 // non-blocking EOF; we're done
475 break;
476 } else {
Christopher Ferrisb36b5922015-06-17 18:35:59 -0700477 ALOGE("Error while reading log: %s\n", strerror(-actual));
Christopher Ferris20303f82014-01-10 16:33:16 -0800478 break;
479 }
480 } else if (actual == 0) {
Christopher Ferrisb36b5922015-06-17 18:35:59 -0700481 ALOGE("Got zero bytes while reading log: %s\n", strerror(errno));
Christopher Ferris20303f82014-01-10 16:33:16 -0800482 break;
Jeff Brown053b8652012-06-06 16:25:03 -0700483 }
484
Brigid Smith50eb5462014-06-18 14:17:57 -0700485 // NOTE: if you ALOGV something here, this will spin forever,
Christopher Ferris20303f82014-01-10 16:33:16 -0800486 // because you will be writing as fast as you're reading. Any
487 // high-frequency debug diagnostics should just be written to
488 // the tombstone file.
Jeff Brown053b8652012-06-06 16:25:03 -0700489
Mark Salyzyn17e85c02014-06-27 15:55:19 -0700490 entry = &log_entry.entry_v1;
Jeff Brown053b8652012-06-06 16:25:03 -0700491
Christopher Ferris20303f82014-01-10 16:33:16 -0800492 if (first) {
Mark Salyzyne43290d2014-06-27 10:32:22 -0700493 _LOG(log, logtype::LOGS, "--------- %slog %s\n",
Mark Salyzyn22b5cef2013-11-22 10:53:34 -0800494 tail ? "tail end of " : "", filename);
Christopher Ferris20303f82014-01-10 16:33:16 -0800495 first = false;
496 }
497
498 // Msg format is: <priority:1><tag:N>\0<message:N>\0
499 //
500 // We want to display it in the same format as "logcat -v threadtime"
501 // (although in this case the pid is redundant).
Christopher Ferris20303f82014-01-10 16:33:16 -0800502 static const char* kPrioChars = "!.VDIWEFS";
Mark Salyzyn22b5cef2013-11-22 10:53:34 -0800503 unsigned hdr_size = log_entry.entry.hdr_size;
504 if (!hdr_size) {
505 hdr_size = sizeof(log_entry.entry_v1);
506 }
Mark Salyzyn305374c2016-08-18 14:59:41 -0700507 if ((hdr_size < sizeof(log_entry.entry_v1)) ||
508 (hdr_size > sizeof(log_entry.entry))) {
509 continue;
510 }
Mark Salyzyn989980c2014-05-14 12:37:22 -0700511 char* msg = reinterpret_cast<char*>(log_entry.buf) + hdr_size;
512
513 char timeBuf[32];
514 time_t sec = static_cast<time_t>(entry->sec);
515 struct tm tmBuf;
516 struct tm* ptm;
517 ptm = localtime_r(&sec, &tmBuf);
518 strftime(timeBuf, sizeof(timeBuf), "%m-%d %H:%M:%S", ptm);
519
520 if (log_entry.id() == LOG_ID_EVENTS) {
521 if (!g_eventTagMap) {
Mark Salyzyn1179eb82016-11-11 09:48:56 -0800522 g_eventTagMap = android_openEventTagMap(NULL);
Mark Salyzyn989980c2014-05-14 12:37:22 -0700523 }
524 AndroidLogEntry e;
525 char buf[512];
526 android_log_processBinaryLogBuffer(entry, &e, g_eventTagMap, buf, sizeof(buf));
Mark Salyzyn9f53cac2016-10-24 13:11:46 -0700527 _LOG(log, logtype::LOGS, "%s.%03d %5d %5d %c %-8.*s: %s\n",
Mark Salyzyn989980c2014-05-14 12:37:22 -0700528 timeBuf, entry->nsec / 1000000, entry->pid, entry->tid,
Mark Salyzyn9f53cac2016-10-24 13:11:46 -0700529 'I', (int)e.tagLen, e.tag, e.message);
Mark Salyzyn989980c2014-05-14 12:37:22 -0700530 continue;
531 }
532
Mark Salyzyn22b5cef2013-11-22 10:53:34 -0800533 unsigned char prio = msg[0];
534 char* tag = msg + 1;
535 msg = tag + strlen(tag) + 1;
Christopher Ferris20303f82014-01-10 16:33:16 -0800536
537 // consume any trailing newlines
Mark Salyzynfca0bd12013-12-12 12:21:20 -0800538 char* nl = msg + strlen(msg) - 1;
539 while (nl >= msg && *nl == '\n') {
Mark Salyzyn17e85c02014-06-27 15:55:19 -0700540 *nl-- = '\0';
Christopher Ferris20303f82014-01-10 16:33:16 -0800541 }
542
543 char prioChar = (prio < strlen(kPrioChars) ? kPrioChars[prio] : '?');
544
Mark Salyzynfca0bd12013-12-12 12:21:20 -0800545 // Look for line breaks ('\n') and display each text line
546 // on a separate line, prefixed with the header, like logcat does.
547 do {
548 nl = strchr(msg, '\n');
549 if (nl) {
550 *nl = '\0';
551 ++nl;
552 }
553
Brigid Smith62ba4892014-06-10 11:53:08 -0700554 _LOG(log, logtype::LOGS, "%s.%03d %5d %5d %c %-8s: %s\n",
Mark Salyzynfca0bd12013-12-12 12:21:20 -0800555 timeBuf, entry->nsec / 1000000, entry->pid, entry->tid,
556 prioChar, tag, msg);
Mark Salyzynfca0bd12013-12-12 12:21:20 -0800557 } while ((msg = nl));
Christopher Ferris20303f82014-01-10 16:33:16 -0800558 }
Jeff Brown053b8652012-06-06 16:25:03 -0700559
Mark Salyzyn22b5cef2013-11-22 10:53:34 -0800560 android_logger_list_free(logger_list);
Jeff Brown053b8652012-06-06 16:25:03 -0700561}
562
Christopher Ferris20303f82014-01-10 16:33:16 -0800563// Dumps the logs generated by the specified pid to the tombstone, from both
564// "system" and "main" log devices. Ideally we'd interleave the output.
Mark Salyzyn17e85c02014-06-27 15:55:19 -0700565static void dump_logs(log_t* log, pid_t pid, unsigned int tail) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700566 if (pid == getpid()) {
567 // Cowardly refuse to dump logs while we're running in-process.
568 return;
569 }
570
Mark Salyzyn22b5cef2013-11-22 10:53:34 -0800571 dump_log_file(log, pid, "system", tail);
572 dump_log_file(log, pid, "main", tail);
Jeff Brown053b8652012-06-06 16:25:03 -0700573}
574
Josh Gaoe1aa0ca2017-03-01 17:23:22 -0800575void engrave_tombstone_ucontext(int tombstone_fd, uintptr_t abort_msg_address, siginfo_t* siginfo,
576 ucontext_t* ucontext) {
577 pid_t pid = getpid();
578 pid_t tid = gettid();
579
Josh Gaoe73c9322017-02-08 16:06:26 -0800580 log_t log;
581 log.current_tid = tid;
582 log.crashed_tid = tid;
583 log.tfd = tombstone_fd;
584 log.amfd_data = nullptr;
585
Josh Gao57f58f82017-03-15 23:23:22 -0700586 char thread_name[16];
587 char process_name[128];
588
589 read_with_default("/proc/self/comm", thread_name, sizeof(thread_name), "<unknown>");
590 read_with_default("/proc/self/cmdline", process_name, sizeof(process_name), "<unknown>");
591
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700592 std::unique_ptr<Regs> regs(Regs::CreateFromUcontext(Regs::CurrentArch(), ucontext));
Josh Gaoe73c9322017-02-08 16:06:26 -0800593
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700594 std::map<pid_t, ThreadInfo> threads;
595 threads[gettid()] = ThreadInfo{
596 .registers = std::move(regs),
597 .tid = tid,
598 .thread_name = thread_name,
599 .pid = pid,
600 .process_name = process_name,
601 .siginfo = siginfo,
602 };
Josh Gao77b00ed2017-05-05 18:11:23 -0700603
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700604 std::unique_ptr<BacktraceMap> backtrace_map(BacktraceMap::Create(getpid(), false));
605 if (!backtrace_map) {
606 ALOGE("failed to create backtrace map");
607 _exit(1);
Josh Gaoe73c9322017-02-08 16:06:26 -0800608 }
Josh Gaofdc95c92017-09-13 15:33:39 -0700609
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700610 std::shared_ptr<Memory> process_memory = backtrace_map->GetProcessMemory();
611 engrave_tombstone(unique_fd(dup(tombstone_fd)), backtrace_map.get(), process_memory.get(),
612 threads, tid, abort_msg_address, nullptr, nullptr);
613}
614
615void engrave_tombstone(unique_fd output_fd, BacktraceMap* map, Memory* process_memory,
616 const std::map<pid_t, ThreadInfo>& threads, pid_t target_thread,
617 uintptr_t abort_msg_address, OpenFilesList* open_files,
618 std::string* amfd_data) {
619 // don't copy log messages to tombstone unless this is a dev device
620 bool want_logs = android::base::GetBoolProperty("ro.debuggable", false);
621
622 log_t log;
623 log.current_tid = target_thread;
624 log.crashed_tid = target_thread;
625 log.tfd = output_fd.get();
626 log.amfd_data = amfd_data;
627
628 _LOG(&log, logtype::HEADER, "*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***\n");
629 dump_header_info(&log);
630
631 auto it = threads.find(target_thread);
632 if (it == threads.end()) {
633 LOG(FATAL) << "failed to find target thread";
634 }
635 dump_thread(&log, map, process_memory, it->second, abort_msg_address, true);
636
637 if (want_logs) {
chirag honnavardef08882017-04-20 18:22:06 +0900638 dump_logs(&log, it->second.pid, 50);
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700639 }
640
641 for (auto& [tid, thread_info] : threads) {
642 if (tid == target_thread) {
643 continue;
Josh Gaofdc95c92017-09-13 15:33:39 -0700644 }
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700645
646 dump_thread(&log, map, process_memory, thread_info, 0, false);
647 }
648
649 if (open_files) {
650 _LOG(&log, logtype::OPEN_FILES, "\nopen files:\n");
651 dump_open_files_list(&log, *open_files, " ");
652 }
653
654 if (want_logs) {
655 dump_logs(&log, it->second.pid, 0);
Josh Gaofdc95c92017-09-13 15:33:39 -0700656 }
Josh Gaoe73c9322017-02-08 16:06:26 -0800657}