blob: 4f75ff138fc32ea3a419e18a080588f45d14495d [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>
Christopher Ferris3336c7b2019-04-08 17:18:24 -070030#include <sys/mman.h>
Jeff Brown053b8652012-06-06 16:25:03 -070031#include <sys/ptrace.h>
32#include <sys/stat.h>
Mark Salyzynff2dcd92016-09-28 15:54:45 -070033#include <time.h>
Jeff Brown053b8652012-06-06 16:25:03 -070034
Christopher Ferris6e964032015-05-15 17:30:21 -070035#include <memory>
36#include <string>
37
Josh Gao57f58f82017-03-15 23:23:22 -070038#include <android-base/file.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>
Josh Gao618cea32021-01-26 17:45:43 -080044#include <async_safe/log.h>
Peter Collingbourne0ea08c22021-02-05 14:59:08 -080045#include <bionic/macros.h>
Mark Salyzyncfd5b082016-10-17 14:28:00 -070046#include <log/log.h>
Tom Cherryc5c6d7d2020-04-17 09:38:55 -070047#include <log/log_read.h>
Mark Salyzynff2dcd92016-09-28 15:54:45 -070048#include <log/logprint.h>
49#include <private/android_filesystem_config.h>
David Srbeckyb9cc4fb2019-04-05 18:23:32 +000050#include <unwindstack/DexFiles.h>
Christopher Ferris60eb1972019-01-15 15:18:43 -080051#include <unwindstack/JitDebug.h>
52#include <unwindstack/Maps.h>
Josh Gao2b2ae0c2017-08-21 14:31:17 -070053#include <unwindstack/Memory.h>
54#include <unwindstack/Regs.h>
Christopher Ferris60eb1972019-01-15 15:18:43 -080055#include <unwindstack/Unwinder.h>
Jeff Brown053b8652012-06-06 16:25:03 -070056
Josh Gaoc3706662017-08-29 13:08:32 -070057#include "libdebuggerd/backtrace.h"
Mitch Phillipse0b4bb12020-02-14 14:54:31 -080058#include "libdebuggerd/gwp_asan.h"
Josh Gaoc3706662017-08-29 13:08:32 -070059#include "libdebuggerd/open_files_list.h"
Peter Collingbournef8622522020-04-07 14:07:32 -070060#include "libdebuggerd/scudo.h"
Josh Gao2b2ae0c2017-08-21 14:31:17 -070061#include "libdebuggerd/utility.h"
Elliott Hughesa660cb32020-07-23 15:26:10 -070062#include "util.h"
Jeff Brown053b8652012-06-06 16:25:03 -070063
Mitch Phillipse0b4bb12020-02-14 14:54:31 -080064#include "gwp_asan/common.h"
65#include "gwp_asan/crash_handler.h"
66
Josh Gao76e1e302021-01-26 15:53:11 -080067#include "tombstone.pb.h"
68
Tom Cherry31121ca2017-10-10 13:30:57 -070069using android::base::GetBoolProperty;
70using android::base::GetProperty;
Elliott Hughes0ba53592017-02-01 16:59:15 -080071using android::base::StringPrintf;
Josh Gao2b2ae0c2017-08-21 14:31:17 -070072using android::base::unique_fd;
73
Elliott Hughese1415a52018-02-15 09:18:21 -080074using namespace std::literals::string_literals;
75
Jeff Brown053b8652012-06-06 16:25:03 -070076#define STACK_WORDS 16
77
Brigid Smith9c8dacc2014-06-02 15:02:20 -070078static void dump_header_info(log_t* log) {
Tom Cherry31121ca2017-10-10 13:30:57 -070079 auto fingerprint = GetProperty("ro.build.fingerprint", "unknown");
80 auto revision = GetProperty("ro.revision", "unknown");
Ben Chengd7760c12012-09-19 16:04:01 -070081
Tom Cherry31121ca2017-10-10 13:30:57 -070082 _LOG(log, logtype::HEADER, "Build fingerprint: '%s'\n", fingerprint.c_str());
83 _LOG(log, logtype::HEADER, "Revision: '%s'\n", revision.c_str());
Brigid Smith62ba4892014-06-10 11:53:08 -070084 _LOG(log, logtype::HEADER, "ABI: '%s'\n", ABI_STRING);
Jeff Brown053b8652012-06-06 16:25:03 -070085}
86
Christopher Ferris481e8372019-07-15 17:13:24 -070087static std::string get_stack_overflow_cause(uint64_t fault_addr, uint64_t sp,
88 unwindstack::Maps* maps) {
89 static constexpr uint64_t kMaxDifferenceBytes = 256;
90 uint64_t difference;
91 if (sp >= fault_addr) {
92 difference = sp - fault_addr;
93 } else {
94 difference = fault_addr - sp;
95 }
96 if (difference <= kMaxDifferenceBytes) {
97 // The faulting address is close to the current sp, check if the sp
98 // indicates a stack overflow.
99 // On arm, the sp does not get updated when the instruction faults.
100 // In this case, the sp will still be in a valid map, which is the
101 // last case below.
102 // On aarch64, the sp does get updated when the instruction faults.
103 // In this case, the sp will be in either an invalid map if triggered
104 // on the main thread, or in a guard map if in another thread, which
105 // will be the first case or second case from below.
106 unwindstack::MapInfo* map_info = maps->Find(sp);
107 if (map_info == nullptr) {
108 return "stack pointer is in a non-existent map; likely due to stack overflow.";
109 } else if ((map_info->flags & (PROT_READ | PROT_WRITE)) != (PROT_READ | PROT_WRITE)) {
110 return "stack pointer is not in a rw map; likely due to stack overflow.";
111 } else if ((sp - map_info->start) <= kMaxDifferenceBytes) {
112 return "stack pointer is close to top of stack; likely stack overflow.";
113 }
114 }
115 return "";
116}
117
118static void dump_probable_cause(log_t* log, const siginfo_t* si, unwindstack::Maps* maps,
119 unwindstack::Regs* regs) {
Elliott Hughes0ba53592017-02-01 16:59:15 -0800120 std::string cause;
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700121 if (si->si_signo == SIGSEGV && si->si_code == SEGV_MAPERR) {
122 if (si->si_addr < reinterpret_cast<void*>(4096)) {
Elliott Hughes0ba53592017-02-01 16:59:15 -0800123 cause = StringPrintf("null pointer dereference");
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700124 } else if (si->si_addr == reinterpret_cast<void*>(0xffff0ffc)) {
Elliott Hughes0ba53592017-02-01 16:59:15 -0800125 cause = "call to kuser_helper_version";
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700126 } else if (si->si_addr == reinterpret_cast<void*>(0xffff0fe0)) {
Elliott Hughes0ba53592017-02-01 16:59:15 -0800127 cause = "call to kuser_get_tls";
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700128 } else if (si->si_addr == reinterpret_cast<void*>(0xffff0fc0)) {
Elliott Hughes0ba53592017-02-01 16:59:15 -0800129 cause = "call to kuser_cmpxchg";
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700130 } else if (si->si_addr == reinterpret_cast<void*>(0xffff0fa0)) {
Elliott Hughes0ba53592017-02-01 16:59:15 -0800131 cause = "call to kuser_memory_barrier";
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700132 } else if (si->si_addr == reinterpret_cast<void*>(0xffff0f60)) {
Elliott Hughes0ba53592017-02-01 16:59:15 -0800133 cause = "call to kuser_cmpxchg64";
Christopher Ferris481e8372019-07-15 17:13:24 -0700134 } else {
135 cause = get_stack_overflow_cause(reinterpret_cast<uint64_t>(si->si_addr), regs->sp(), maps);
Elliott Hughes0ba53592017-02-01 16:59:15 -0800136 }
Ivan Lozanodf3cec92018-11-19 10:43:47 -0800137 } else if (si->si_signo == SIGSEGV && si->si_code == SEGV_ACCERR) {
Christopher Ferris481e8372019-07-15 17:13:24 -0700138 uint64_t fault_addr = reinterpret_cast<uint64_t>(si->si_addr);
139 unwindstack::MapInfo* map_info = maps->Find(fault_addr);
Christopher Ferris60eb1972019-01-15 15:18:43 -0800140 if (map_info != nullptr && map_info->flags == PROT_EXEC) {
141 cause = "execute-only (no-read) memory access error; likely due to data in .text.";
Christopher Ferris481e8372019-07-15 17:13:24 -0700142 } else {
143 cause = get_stack_overflow_cause(fault_addr, regs->sp(), maps);
Ivan Lozanodf3cec92018-11-19 10:43:47 -0800144 }
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700145 } else if (si->si_signo == SIGSYS && si->si_code == SYS_SECCOMP) {
146 cause = StringPrintf("seccomp prevented call to disallowed %s system call %d", ABI_STRING,
147 si->si_syscall);
Elliott Hughes0ba53592017-02-01 16:59:15 -0800148 }
149
150 if (!cause.empty()) _LOG(log, logtype::HEADER, "Cause: %s\n", cause.c_str());
151}
152
Christopher Ferris60eb1972019-01-15 15:18:43 -0800153static void dump_signal_info(log_t* log, const ThreadInfo& thread_info,
Peter Collingbournef03af882020-03-20 18:09:00 -0700154 const ProcessInfo& process_info, unwindstack::Memory* process_memory) {
Elliott Hughes2baf4432018-05-30 12:55:04 -0700155 char addr_desc[64]; // ", fault addr 0x1234"
Peter Collingbournef03af882020-03-20 18:09:00 -0700156 if (process_info.has_fault_address) {
Mitch Phillipse4adff02021-01-21 20:41:50 -0800157 // SIGILL faults will never have tagged addresses, so okay to
158 // indiscriminately use the tagged address here.
159 size_t addr = process_info.maybe_tagged_fault_address;
Elliott Hughes2baf4432018-05-30 12:55:04 -0700160 if (thread_info.siginfo->si_signo == SIGILL) {
161 uint32_t instruction = {};
Peter Collingbournef03af882020-03-20 18:09:00 -0700162 process_memory->Read(addr, &instruction, sizeof(instruction));
163 snprintf(addr_desc, sizeof(addr_desc), "0x%zx (*pc=%#08x)", addr, instruction);
Elliott Hughes2baf4432018-05-30 12:55:04 -0700164 } else {
Peter Collingbournef03af882020-03-20 18:09:00 -0700165 snprintf(addr_desc, sizeof(addr_desc), "0x%zx", addr);
Elliott Hughes2baf4432018-05-30 12:55:04 -0700166 }
Elliott Hughes855fcc32014-04-25 16:05:34 -0700167 } else {
168 snprintf(addr_desc, sizeof(addr_desc), "--------");
169 }
170
Elliott Hughes70d8f282018-04-25 17:00:14 -0700171 char sender_desc[32] = {}; // " from pid 1234, uid 666"
172 if (signal_has_sender(thread_info.siginfo, thread_info.pid)) {
173 get_signal_sender(sender_desc, sizeof(sender_desc), thread_info.siginfo);
174 }
Elliott Hughes0ba53592017-02-01 16:59:15 -0800175
Elliott Hughes70d8f282018-04-25 17:00:14 -0700176 _LOG(log, logtype::HEADER, "signal %d (%s), code %d (%s%s), fault addr %s\n",
177 thread_info.siginfo->si_signo, get_signame(thread_info.siginfo),
178 thread_info.siginfo->si_code, get_sigcode(thread_info.siginfo), sender_desc, addr_desc);
Jeff Brown053b8652012-06-06 16:25:03 -0700179}
180
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700181static void dump_thread_info(log_t* log, const ThreadInfo& thread_info) {
Elliott Hughes713ec252020-08-03 08:43:43 -0700182 // Don't try to collect logs from the threads that implement the logging system itself.
183 if (thread_info.uid == AID_LOGD) log->should_retrieve_logcat = false;
Christopher Ferris20303f82014-01-10 16:33:16 -0800184
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700185 _LOG(log, logtype::HEADER, "pid: %d, tid: %d, name: %s >>> %s <<<\n", thread_info.pid,
186 thread_info.tid, thread_info.thread_name.c_str(), thread_info.process_name.c_str());
Misha Wagner39c5b8c2019-04-18 16:07:33 +0100187 _LOG(log, logtype::HEADER, "uid: %d\n", thread_info.uid);
Peter Collingbourne864f15d2020-09-14 20:27:36 -0700188 if (thread_info.tagged_addr_ctrl != -1) {
189 _LOG(log, logtype::HEADER, "tagged_addr_ctrl: %016lx\n", thread_info.tagged_addr_ctrl);
190 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800191}
Jeff Brown053b8652012-06-06 16:25:03 -0700192
Christopher Ferris7937a362018-01-18 11:15:49 -0800193static std::string get_addr_string(uint64_t addr) {
Christopher Ferris862fe022015-06-02 14:52:44 -0700194 std::string addr_str;
195#if defined(__LP64__)
Josh Gao76e1e302021-01-26 15:53:11 -0800196 addr_str = StringPrintf("%08x'%08x", static_cast<uint32_t>(addr >> 32),
Elliott Hughes0ba53592017-02-01 16:59:15 -0800197 static_cast<uint32_t>(addr & 0xffffffff));
Christopher Ferris862fe022015-06-02 14:52:44 -0700198#else
Christopher Ferris7937a362018-01-18 11:15:49 -0800199 addr_str = StringPrintf("%08x", static_cast<uint32_t>(addr));
Christopher Ferris862fe022015-06-02 14:52:44 -0700200#endif
201 return addr_str;
202}
203
Christopher Ferris60eb1972019-01-15 15:18:43 -0800204static void dump_abort_message(log_t* log, unwindstack::Memory* process_memory, uint64_t address) {
Josh Gao7c89f9e2016-01-13 17:57:14 -0800205 if (address == 0) {
206 return;
207 }
208
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700209 size_t length;
210 if (!process_memory->ReadFully(address, &length, sizeof(length))) {
211 _LOG(log, logtype::HEADER, "Failed to read abort message header: %s\n", strerror(errno));
212 return;
213 }
Josh Gao7c89f9e2016-01-13 17:57:14 -0800214
Josh Gao1cc7bd82018-02-13 13:16:17 -0800215 // The length field includes the length of the length field itself.
216 if (length < sizeof(size_t)) {
217 _LOG(log, logtype::HEADER, "Abort message header malformed: claimed length = %zd\n", length);
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700218 return;
Josh Gao7c89f9e2016-01-13 17:57:14 -0800219 }
Josh Gao7c89f9e2016-01-13 17:57:14 -0800220
Josh Gao1cc7bd82018-02-13 13:16:17 -0800221 length -= sizeof(size_t);
222
Josh Gao83b8ac22018-04-20 17:31:53 -0700223 // The abort message should be null terminated already, but reserve a spot for NUL just in case.
224 std::vector<char> msg(length + 1);
Josh Gao1cc7bd82018-02-13 13:16:17 -0800225 if (!process_memory->ReadFully(address + sizeof(length), &msg[0], length)) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700226 _LOG(log, logtype::HEADER, "Failed to read abort message: %s\n", strerror(errno));
227 return;
228 }
229
Josh Gao1cc7bd82018-02-13 13:16:17 -0800230 _LOG(log, logtype::HEADER, "Abort message: '%s'\n", &msg[0]);
Josh Gao7c89f9e2016-01-13 17:57:14 -0800231}
232
Christopher Ferris60eb1972019-01-15 15:18:43 -0800233static void dump_all_maps(log_t* log, unwindstack::Unwinder* unwinder, uint64_t addr) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700234 bool print_fault_address_marker = addr;
Christopher Ferris20303f82014-01-10 16:33:16 -0800235
Christopher Ferris60eb1972019-01-15 15:18:43 -0800236 unwindstack::Maps* maps = unwinder->GetMaps();
Elliott Hughes868d39a2017-09-26 11:54:49 -0700237 _LOG(log, logtype::MAPS,
238 "\n"
Josh Gao1ce8e142017-09-27 13:59:42 -0700239 "memory map (%zu entr%s):",
Christopher Ferris60eb1972019-01-15 15:18:43 -0800240 maps->Total(), maps->Total() == 1 ? "y" : "ies");
Elliott Hughes868d39a2017-09-26 11:54:49 -0700241 if (print_fault_address_marker) {
Christopher Ferris60eb1972019-01-15 15:18:43 -0800242 if (maps->Total() != 0 && addr < maps->Get(0)->start) {
Josh Gao1ce8e142017-09-27 13:59:42 -0700243 _LOG(log, logtype::MAPS, "\n--->Fault address falls at %s before any mapped regions\n",
Christopher Ferris862fe022015-06-02 14:52:44 -0700244 get_addr_string(addr).c_str());
Christopher Ferrisa21bd932015-02-27 13:39:47 -0800245 print_fault_address_marker = false;
Elliott Hughes868d39a2017-09-26 11:54:49 -0700246 } else {
Josh Gao1ce8e142017-09-27 13:59:42 -0700247 _LOG(log, logtype::MAPS, " (fault address prefixed with --->)\n");
Brigid Smith8606eaa2014-07-07 12:33:50 -0700248 }
Josh Gao1ce8e142017-09-27 13:59:42 -0700249 } else {
250 _LOG(log, logtype::MAPS, "\n");
Brigid Smith8606eaa2014-07-07 12:33:50 -0700251 }
Christopher Ferrisa21bd932015-02-27 13:39:47 -0800252
Christopher Ferris60eb1972019-01-15 15:18:43 -0800253 std::shared_ptr<unwindstack::Memory>& process_memory = unwinder->GetProcessMemory();
254
Christopher Ferrisa21bd932015-02-27 13:39:47 -0800255 std::string line;
Florian Mayer3d67d342019-02-27 18:00:37 +0000256 for (auto const& map_info : *maps) {
Christopher Ferrisa21bd932015-02-27 13:39:47 -0800257 line = " ";
258 if (print_fault_address_marker) {
Christopher Ferris60eb1972019-01-15 15:18:43 -0800259 if (addr < map_info->start) {
Christopher Ferris862fe022015-06-02 14:52:44 -0700260 _LOG(log, logtype::MAPS, "--->Fault address falls at %s between mapped regions\n",
261 get_addr_string(addr).c_str());
Christopher Ferrisa21bd932015-02-27 13:39:47 -0800262 print_fault_address_marker = false;
Christopher Ferris60eb1972019-01-15 15:18:43 -0800263 } else if (addr >= map_info->start && addr < map_info->end) {
Christopher Ferrisa21bd932015-02-27 13:39:47 -0800264 line = "--->";
265 print_fault_address_marker = false;
266 }
267 }
Christopher Ferris60eb1972019-01-15 15:18:43 -0800268 line += get_addr_string(map_info->start) + '-' + get_addr_string(map_info->end - 1) + ' ';
269 if (map_info->flags & PROT_READ) {
Christopher Ferrisa21bd932015-02-27 13:39:47 -0800270 line += 'r';
271 } else {
272 line += '-';
273 }
Christopher Ferris60eb1972019-01-15 15:18:43 -0800274 if (map_info->flags & PROT_WRITE) {
Christopher Ferrisa21bd932015-02-27 13:39:47 -0800275 line += 'w';
276 } else {
277 line += '-';
278 }
Christopher Ferris60eb1972019-01-15 15:18:43 -0800279 if (map_info->flags & PROT_EXEC) {
Christopher Ferrisa21bd932015-02-27 13:39:47 -0800280 line += 'x';
281 } else {
282 line += '-';
283 }
Christopher Ferris60eb1972019-01-15 15:18:43 -0800284 line += StringPrintf(" %8" PRIx64 " %8" PRIx64, map_info->offset,
285 map_info->end - map_info->start);
Christopher Ferris862fe022015-06-02 14:52:44 -0700286 bool space_needed = true;
Christopher Ferris60eb1972019-01-15 15:18:43 -0800287 if (!map_info->name.empty()) {
Christopher Ferris862fe022015-06-02 14:52:44 -0700288 space_needed = false;
Christopher Ferris60eb1972019-01-15 15:18:43 -0800289 line += " " + map_info->name;
290 std::string build_id = map_info->GetPrintableBuildID();
291 if (!build_id.empty()) {
Christopher Ferrisa21bd932015-02-27 13:39:47 -0800292 line += " (BuildId: " + build_id + ")";
293 }
294 }
Christopher Ferris60eb1972019-01-15 15:18:43 -0800295 uint64_t load_bias = map_info->GetLoadBias(process_memory);
296 if (load_bias != 0) {
Christopher Ferris862fe022015-06-02 14:52:44 -0700297 if (space_needed) {
298 line += ' ';
299 }
Christopher Ferris60eb1972019-01-15 15:18:43 -0800300 line += StringPrintf(" (load bias 0x%" PRIx64 ")", load_bias);
Christopher Ferris2106f4b2015-05-01 15:02:03 -0700301 }
Christopher Ferrisa21bd932015-02-27 13:39:47 -0800302 _LOG(log, logtype::MAPS, "%s\n", line.c_str());
303 }
304 if (print_fault_address_marker) {
Christopher Ferris862fe022015-06-02 14:52:44 -0700305 _LOG(log, logtype::MAPS, "--->Fault address falls at %s after any mapped regions\n",
306 get_addr_string(addr).c_str());
Christopher Ferris20303f82014-01-10 16:33:16 -0800307 }
Jeff Brown053b8652012-06-06 16:25:03 -0700308}
309
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700310static void print_register_row(log_t* log,
311 const std::vector<std::pair<std::string, uint64_t>>& registers) {
312 std::string output;
313 for (auto& [name, value] : registers) {
Christopher Ferris7937a362018-01-18 11:15:49 -0800314 output += android::base::StringPrintf(" %-3s %0*" PRIx64, name.c_str(),
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700315 static_cast<int>(2 * sizeof(void*)),
Christopher Ferris7937a362018-01-18 11:15:49 -0800316 static_cast<uint64_t>(value));
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700317 }
318
319 _LOG(log, logtype::REGISTERS, " %s\n", output.c_str());
Josh Gao77b00ed2017-05-05 18:11:23 -0700320}
321
Christopher Ferris60eb1972019-01-15 15:18:43 -0800322void dump_registers(log_t* log, unwindstack::Regs* regs) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700323 // Split lr/sp/pc into their own special row.
324 static constexpr size_t column_count = 4;
325 std::vector<std::pair<std::string, uint64_t>> current_row;
326 std::vector<std::pair<std::string, uint64_t>> special_row;
327
328#if defined(__arm__) || defined(__aarch64__)
Peter Collingbournebb2f9412019-11-18 12:36:50 -0800329 static constexpr const char* special_registers[] = {"ip", "lr", "sp", "pc", "pst"};
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700330#elif defined(__i386__)
331 static constexpr const char* special_registers[] = {"ebp", "esp", "eip"};
332#elif defined(__x86_64__)
333 static constexpr const char* special_registers[] = {"rbp", "rsp", "rip"};
334#else
335 static constexpr const char* special_registers[] = {};
336#endif
337
338 regs->IterateRegisters([log, &current_row, &special_row](const char* name, uint64_t value) {
339 auto row = &current_row;
340 for (const char* special_name : special_registers) {
341 if (strcmp(special_name, name) == 0) {
342 row = &special_row;
343 break;
344 }
345 }
346
347 row->emplace_back(name, value);
348 if (current_row.size() == column_count) {
349 print_register_row(log, current_row);
350 current_row.clear();
351 }
352 });
353
354 if (!current_row.empty()) {
355 print_register_row(log, current_row);
356 }
357
358 print_register_row(log, special_row);
359}
360
Christopher Ferris60eb1972019-01-15 15:18:43 -0800361void dump_memory_and_code(log_t* log, unwindstack::Maps* maps, unwindstack::Memory* memory,
362 unwindstack::Regs* regs) {
363 regs->IterateRegisters([log, maps, memory](const char* reg_name, uint64_t reg_value) {
Elliott Hughese1415a52018-02-15 09:18:21 -0800364 std::string label{"memory near "s + reg_name};
Christopher Ferris60eb1972019-01-15 15:18:43 -0800365 if (maps) {
Peter Collingbourne0ea08c22021-02-05 14:59:08 -0800366 unwindstack::MapInfo* map_info = maps->Find(untag_address(reg_value));
Christopher Ferris60eb1972019-01-15 15:18:43 -0800367 if (map_info != nullptr && !map_info->name.empty()) {
368 label += " (" + map_info->name + ")";
369 }
Elliott Hughese1415a52018-02-15 09:18:21 -0800370 }
371 dump_memory(log, memory, reg_value, label);
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700372 });
373}
374
Christopher Ferris60eb1972019-01-15 15:18:43 -0800375static bool dump_thread(log_t* log, unwindstack::Unwinder* unwinder, const ThreadInfo& thread_info,
Peter Collingbourne843f7e62020-02-28 19:07:33 -0800376 const ProcessInfo& process_info, bool primary_thread) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700377 log->current_tid = thread_info.tid;
Josh Gao7c89f9e2016-01-13 17:57:14 -0800378 if (!primary_thread) {
Brigid Smith62ba4892014-06-10 11:53:08 -0700379 _LOG(log, logtype::THREAD, "--- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---\n");
Josh Gao7c89f9e2016-01-13 17:57:14 -0800380 }
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700381 dump_thread_info(log, thread_info);
Christopher Ferris20303f82014-01-10 16:33:16 -0800382
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700383 if (thread_info.siginfo) {
Peter Collingbournef03af882020-03-20 18:09:00 -0700384 dump_signal_info(log, thread_info, process_info, unwinder->GetProcessMemory().get());
Mitch Phillipse0b4bb12020-02-14 14:54:31 -0800385 }
386
Peter Collingbourne843f7e62020-02-28 19:07:33 -0800387 std::unique_ptr<GwpAsanCrashData> gwp_asan_crash_data;
Peter Collingbournef8622522020-04-07 14:07:32 -0700388 std::unique_ptr<ScudoCrashData> scudo_crash_data;
Peter Collingbourne843f7e62020-02-28 19:07:33 -0800389 if (primary_thread) {
390 gwp_asan_crash_data = std::make_unique<GwpAsanCrashData>(unwinder->GetProcessMemory().get(),
391 process_info, thread_info);
Peter Collingbournef8622522020-04-07 14:07:32 -0700392 scudo_crash_data =
393 std::make_unique<ScudoCrashData>(unwinder->GetProcessMemory().get(), process_info);
Peter Collingbourne843f7e62020-02-28 19:07:33 -0800394 }
395
396 if (primary_thread && gwp_asan_crash_data->CrashIsMine()) {
397 gwp_asan_crash_data->DumpCause(log);
Peter Collingbournef8622522020-04-07 14:07:32 -0700398 } else if (thread_info.siginfo && !(primary_thread && scudo_crash_data->CrashIsMine())) {
Josh Gao76e1e302021-01-26 15:53:11 -0800399 dump_probable_cause(log, thread_info.siginfo, unwinder->GetMaps(), thread_info.registers.get());
Josh Gao7c89f9e2016-01-13 17:57:14 -0800400 }
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700401
Josh Gao34c25562017-12-22 14:18:39 -0800402 if (primary_thread) {
Peter Collingbourne843f7e62020-02-28 19:07:33 -0800403 dump_abort_message(log, unwinder->GetProcessMemory().get(), process_info.abort_msg_address);
Josh Gao34c25562017-12-22 14:18:39 -0800404 }
405
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700406 dump_registers(log, thread_info.registers.get());
407
Josh Gao5d1c14f2018-04-20 12:04:41 -0700408 // Unwind will mutate the registers, so make a copy first.
Christopher Ferris60eb1972019-01-15 15:18:43 -0800409 std::unique_ptr<unwindstack::Regs> regs_copy(thread_info.registers->Clone());
410 unwinder->SetRegs(regs_copy.get());
411 unwinder->Unwind();
412 if (unwinder->NumFrames() == 0) {
Christopher Ferrisb05c4722020-09-23 15:51:46 -0700413 _LOG(log, logtype::THREAD, "Failed to unwind\n");
414 if (unwinder->LastErrorCode() != unwindstack::ERROR_NONE) {
415 _LOG(log, logtype::THREAD, " Error code: %s\n", unwinder->LastErrorCodeString());
416 _LOG(log, logtype::THREAD, " Error address: 0x%" PRIx64 "\n", unwinder->LastErrorAddress());
417 }
Christopher Ferris60eb1972019-01-15 15:18:43 -0800418 } else {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700419 _LOG(log, logtype::BACKTRACE, "\nbacktrace:\n");
Christopher Ferris4ae266c2019-04-03 09:27:12 -0700420 log_backtrace(log, unwinder, " ");
Josh Gao7c89f9e2016-01-13 17:57:14 -0800421 }
Brigid Smith62ba4892014-06-10 11:53:08 -0700422
Josh Gao7c89f9e2016-01-13 17:57:14 -0800423 if (primary_thread) {
Peter Collingbourne843f7e62020-02-28 19:07:33 -0800424 if (gwp_asan_crash_data->HasDeallocationTrace()) {
425 gwp_asan_crash_data->DumpDeallocationTrace(log, unwinder);
Mitch Phillipse0b4bb12020-02-14 14:54:31 -0800426 }
427
Peter Collingbourne843f7e62020-02-28 19:07:33 -0800428 if (gwp_asan_crash_data->HasAllocationTrace()) {
429 gwp_asan_crash_data->DumpAllocationTrace(log, unwinder);
Mitch Phillipse0b4bb12020-02-14 14:54:31 -0800430 }
431
Peter Collingbournef8622522020-04-07 14:07:32 -0700432 scudo_crash_data->DumpCause(log, unwinder);
433
Christopher Ferris60eb1972019-01-15 15:18:43 -0800434 unwindstack::Maps* maps = unwinder->GetMaps();
435 dump_memory_and_code(log, maps, unwinder->GetProcessMemory().get(),
436 thread_info.registers.get());
437 if (maps != nullptr) {
Christopher Ferris7937a362018-01-18 11:15:49 -0800438 uint64_t addr = 0;
Mitch Phillipse4adff02021-01-21 20:41:50 -0800439 if (process_info.has_fault_address) {
440 addr = process_info.untagged_fault_address;
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700441 }
Christopher Ferris60eb1972019-01-15 15:18:43 -0800442 dump_all_maps(log, unwinder, addr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800443 }
444 }
445
Josh Gao7c89f9e2016-01-13 17:57:14 -0800446 log->current_tid = log->crashed_tid;
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700447 return true;
Jeff Brown053b8652012-06-06 16:25:03 -0700448}
449
Christopher Ferris20303f82014-01-10 16:33:16 -0800450// Reads the contents of the specified log device, filters out the entries
451// that don't match the specified pid, and writes them to the tombstone file.
452//
Mark Salyzyn17e85c02014-06-27 15:55:19 -0700453// If "tail" is non-zero, log the last "tail" number of lines.
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700454static void dump_log_file(log_t* log, pid_t pid, const char* filename, unsigned int tail) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800455 bool first = true;
Christopher Ferrisc637ada2018-07-13 16:55:38 -0700456 logger_list* logger_list;
Jeff Brown053b8652012-06-06 16:25:03 -0700457
Mark Salyzyn45ae4462014-07-25 12:25:48 -0700458 if (!log->should_retrieve_logcat) {
459 return;
460 }
461
Tom Cherry907b2d02020-03-23 13:40:10 -0700462 logger_list =
463 android_logger_list_open(android_name_to_log_id(filename), ANDROID_LOG_NONBLOCK, tail, pid);
Jeff Brown053b8652012-06-06 16:25:03 -0700464
Mark Salyzyn22b5cef2013-11-22 10:53:34 -0800465 if (!logger_list) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700466 ALOGE("Unable to open %s: %s\n", filename, strerror(errno));
Christopher Ferris20303f82014-01-10 16:33:16 -0800467 return;
468 }
469
Christopher Ferris20303f82014-01-10 16:33:16 -0800470 while (true) {
Christopher Ferrisc637ada2018-07-13 16:55:38 -0700471 log_msg log_entry;
Mark Salyzyn22b5cef2013-11-22 10:53:34 -0800472 ssize_t actual = android_logger_list_read(logger_list, &log_entry);
473
Christopher Ferris20303f82014-01-10 16:33:16 -0800474 if (actual < 0) {
Mark Salyzyn22b5cef2013-11-22 10:53:34 -0800475 if (actual == -EINTR) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800476 // interrupted by signal, retry
477 continue;
Mark Salyzyn22b5cef2013-11-22 10:53:34 -0800478 } else if (actual == -EAGAIN) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800479 // non-blocking EOF; we're done
480 break;
481 } else {
Christopher Ferrisb36b5922015-06-17 18:35:59 -0700482 ALOGE("Error while reading log: %s\n", strerror(-actual));
Christopher Ferris20303f82014-01-10 16:33:16 -0800483 break;
484 }
485 } else if (actual == 0) {
Christopher Ferrisb36b5922015-06-17 18:35:59 -0700486 ALOGE("Got zero bytes while reading log: %s\n", strerror(errno));
Christopher Ferris20303f82014-01-10 16:33:16 -0800487 break;
Jeff Brown053b8652012-06-06 16:25:03 -0700488 }
489
Brigid Smith50eb5462014-06-18 14:17:57 -0700490 // NOTE: if you ALOGV something here, this will spin forever,
Christopher Ferris20303f82014-01-10 16:33:16 -0800491 // because you will be writing as fast as you're reading. Any
492 // high-frequency debug diagnostics should just be written to
493 // the tombstone file.
Jeff Brown053b8652012-06-06 16:25:03 -0700494
Christopher Ferris20303f82014-01-10 16:33:16 -0800495 if (first) {
Josh Gao76e1e302021-01-26 15:53:11 -0800496 _LOG(log, logtype::LOGS, "--------- %slog %s\n", tail ? "tail end of " : "", filename);
Christopher Ferris20303f82014-01-10 16:33:16 -0800497 first = false;
498 }
499
500 // Msg format is: <priority:1><tag:N>\0<message:N>\0
501 //
502 // We want to display it in the same format as "logcat -v threadtime"
503 // (although in this case the pid is redundant).
Mark Salyzyn989980c2014-05-14 12:37:22 -0700504 char timeBuf[32];
Christopher Ferrisc637ada2018-07-13 16:55:38 -0700505 time_t sec = static_cast<time_t>(log_entry.entry.sec);
Elliott Hughesa660cb32020-07-23 15:26:10 -0700506 tm tm;
507 localtime_r(&sec, &tm);
508 strftime(timeBuf, sizeof(timeBuf), "%m-%d %H:%M:%S", &tm);
Mark Salyzyn989980c2014-05-14 12:37:22 -0700509
Christopher Ferrisc637ada2018-07-13 16:55:38 -0700510 char* msg = log_entry.msg();
511 if (msg == nullptr) {
512 continue;
513 }
Mark Salyzyn22b5cef2013-11-22 10:53:34 -0800514 unsigned char prio = msg[0];
515 char* tag = msg + 1;
516 msg = tag + strlen(tag) + 1;
Christopher Ferris20303f82014-01-10 16:33:16 -0800517
518 // consume any trailing newlines
Mark Salyzynfca0bd12013-12-12 12:21:20 -0800519 char* nl = msg + strlen(msg) - 1;
520 while (nl >= msg && *nl == '\n') {
Mark Salyzyn17e85c02014-06-27 15:55:19 -0700521 *nl-- = '\0';
Christopher Ferris20303f82014-01-10 16:33:16 -0800522 }
523
Christopher Ferrisc637ada2018-07-13 16:55:38 -0700524 static const char* kPrioChars = "!.VDIWEFS";
Christopher Ferris20303f82014-01-10 16:33:16 -0800525 char prioChar = (prio < strlen(kPrioChars) ? kPrioChars[prio] : '?');
526
Mark Salyzynfca0bd12013-12-12 12:21:20 -0800527 // Look for line breaks ('\n') and display each text line
528 // on a separate line, prefixed with the header, like logcat does.
529 do {
530 nl = strchr(msg, '\n');
Christopher Ferrisc637ada2018-07-13 16:55:38 -0700531 if (nl != nullptr) {
Mark Salyzynfca0bd12013-12-12 12:21:20 -0800532 *nl = '\0';
533 ++nl;
534 }
535
Christopher Ferrisc637ada2018-07-13 16:55:38 -0700536 _LOG(log, logtype::LOGS, "%s.%03d %5d %5d %c %-8s: %s\n", timeBuf,
537 log_entry.entry.nsec / 1000000, log_entry.entry.pid, log_entry.entry.tid, prioChar, tag,
538 msg);
Mark Salyzynfca0bd12013-12-12 12:21:20 -0800539 } while ((msg = nl));
Christopher Ferris20303f82014-01-10 16:33:16 -0800540 }
Jeff Brown053b8652012-06-06 16:25:03 -0700541
Mark Salyzyn22b5cef2013-11-22 10:53:34 -0800542 android_logger_list_free(logger_list);
Jeff Brown053b8652012-06-06 16:25:03 -0700543}
544
Christopher Ferris20303f82014-01-10 16:33:16 -0800545// Dumps the logs generated by the specified pid to the tombstone, from both
546// "system" and "main" log devices. Ideally we'd interleave the output.
Mark Salyzyn17e85c02014-06-27 15:55:19 -0700547static void dump_logs(log_t* log, pid_t pid, unsigned int tail) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700548 if (pid == getpid()) {
549 // Cowardly refuse to dump logs while we're running in-process.
550 return;
551 }
552
Mark Salyzyn22b5cef2013-11-22 10:53:34 -0800553 dump_log_file(log, pid, "system", tail);
554 dump_log_file(log, pid, "main", tail);
Jeff Brown053b8652012-06-06 16:25:03 -0700555}
556
Josh Gao76e1e302021-01-26 15:53:11 -0800557void engrave_tombstone_ucontext(int tombstone_fd, int proto_fd, uint64_t abort_msg_address,
558 siginfo_t* siginfo, ucontext_t* ucontext) {
Misha Wagner39c5b8c2019-04-18 16:07:33 +0100559 pid_t uid = getuid();
Josh Gaoe1aa0ca2017-03-01 17:23:22 -0800560 pid_t pid = getpid();
561 pid_t tid = gettid();
562
Josh Gaoe73c9322017-02-08 16:06:26 -0800563 log_t log;
564 log.current_tid = tid;
565 log.crashed_tid = tid;
566 log.tfd = tombstone_fd;
567 log.amfd_data = nullptr;
568
Elliott Hughesa660cb32020-07-23 15:26:10 -0700569 std::string thread_name = get_thread_name(tid);
570 std::string process_name = get_process_name(pid);
Josh Gao57f58f82017-03-15 23:23:22 -0700571
Christopher Ferris60eb1972019-01-15 15:18:43 -0800572 std::unique_ptr<unwindstack::Regs> regs(
573 unwindstack::Regs::CreateFromUcontext(unwindstack::Regs::CurrentArch(), ucontext));
Josh Gaoe73c9322017-02-08 16:06:26 -0800574
Josh Gao76e1e302021-01-26 15:53:11 -0800575 std::string selinux_label;
576 android::base::ReadFileToString("/proc/self/attr/current", &selinux_label);
577
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700578 std::map<pid_t, ThreadInfo> threads;
Elliott Hughesa660cb32020-07-23 15:26:10 -0700579 threads[tid] = ThreadInfo{
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700580 .registers = std::move(regs),
Misha Wagner39c5b8c2019-04-18 16:07:33 +0100581 .uid = uid,
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700582 .tid = tid,
Josh Gao76e1e302021-01-26 15:53:11 -0800583 .thread_name = std::move(thread_name),
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700584 .pid = pid,
Josh Gao76e1e302021-01-26 15:53:11 -0800585 .process_name = std::move(process_name),
586 .selinux_label = std::move(selinux_label),
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700587 .siginfo = siginfo,
588 };
Josh Gao77b00ed2017-05-05 18:11:23 -0700589
Christopher Ferrisb05c4722020-09-23 15:51:46 -0700590 unwindstack::UnwinderFromPid unwinder(kMaxFrames, pid, unwindstack::Regs::CurrentArch());
591 if (!unwinder.Init()) {
Josh Gao618cea32021-01-26 17:45:43 -0800592 async_safe_fatal("failed to init unwinder object");
Josh Gaoe73c9322017-02-08 16:06:26 -0800593 }
Josh Gaofdc95c92017-09-13 15:33:39 -0700594
Peter Collingbourne843f7e62020-02-28 19:07:33 -0800595 ProcessInfo process_info;
596 process_info.abort_msg_address = abort_msg_address;
Josh Gao76e1e302021-01-26 15:53:11 -0800597 engrave_tombstone(unique_fd(dup(tombstone_fd)), unique_fd(dup(proto_fd)), &unwinder, threads, tid,
598 process_info, nullptr, nullptr);
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700599}
600
Josh Gao76e1e302021-01-26 15:53:11 -0800601void engrave_tombstone(unique_fd output_fd, unique_fd proto_fd, unwindstack::Unwinder* unwinder,
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700602 const std::map<pid_t, ThreadInfo>& threads, pid_t target_thread,
Peter Collingbourne843f7e62020-02-28 19:07:33 -0800603 const ProcessInfo& process_info, OpenFilesList* open_files,
604 std::string* amfd_data) {
Elliott Hughesa660cb32020-07-23 15:26:10 -0700605 // Don't copy log messages to tombstone unless this is a development device.
Josh Gao76e1e302021-01-26 15:53:11 -0800606 Tombstone tombstone;
607 engrave_tombstone_proto(&tombstone, unwinder, threads, target_thread, process_info, open_files);
608
Josh Gao618cea32021-01-26 17:45:43 -0800609 if (proto_fd != -1) {
610 if (!tombstone.SerializeToFileDescriptor(proto_fd.get())) {
611 async_safe_format_log(ANDROID_LOG_ERROR, LOG_TAG, "failed to write proto tombstone: %s",
612 strerror(errno));
613 }
Josh Gao76e1e302021-01-26 15:53:11 -0800614 }
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700615
616 log_t log;
617 log.current_tid = target_thread;
618 log.crashed_tid = target_thread;
619 log.tfd = output_fd.get();
620 log.amfd_data = amfd_data;
621
Josh Gao3be68742021-02-18 16:56:48 -0800622 bool translate_proto = GetBoolProperty("debug.debuggerd.translate_proto_to_text", true);
Josh Gao76e1e302021-01-26 15:53:11 -0800623 if (translate_proto) {
624 tombstone_proto_to_text(tombstone, [&log](const std::string& line, bool should_log) {
625 _LOG(&log, should_log ? logtype::HEADER : logtype::LOGS, "%s\n", line.c_str());
626 });
627 } else {
628 bool want_logs = GetBoolProperty("ro.debuggable", false);
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700629
Josh Gao76e1e302021-01-26 15:53:11 -0800630 _LOG(&log, logtype::HEADER,
631 "*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***\n");
632 dump_header_info(&log);
633 _LOG(&log, logtype::HEADER, "Timestamp: %s\n", get_timestamp().c_str());
Mitch Phillipse0b4bb12020-02-14 14:54:31 -0800634
Josh Gao76e1e302021-01-26 15:53:11 -0800635 auto it = threads.find(target_thread);
636 if (it == threads.end()) {
Josh Gao618cea32021-01-26 17:45:43 -0800637 async_safe_fatal("failed to find target thread");
Josh Gaofdc95c92017-09-13 15:33:39 -0700638 }
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700639
Josh Gao76e1e302021-01-26 15:53:11 -0800640 dump_thread(&log, unwinder, it->second, process_info, true);
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700641
Josh Gao76e1e302021-01-26 15:53:11 -0800642 if (want_logs) {
643 dump_logs(&log, it->second.pid, 50);
644 }
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700645
Josh Gao76e1e302021-01-26 15:53:11 -0800646 for (auto& [tid, thread_info] : threads) {
647 if (tid == target_thread) {
648 continue;
649 }
650
651 dump_thread(&log, unwinder, thread_info, process_info, false);
652 }
653
654 if (open_files) {
655 _LOG(&log, logtype::OPEN_FILES, "\nopen files:\n");
656 dump_open_files_list(&log, *open_files, " ");
657 }
658
659 if (want_logs) {
660 dump_logs(&log, it->second.pid, 0);
661 }
Josh Gaofdc95c92017-09-13 15:33:39 -0700662 }
Josh Gaoe73c9322017-02-08 16:06:26 -0800663}