blob: d6b2e2537c8713de86f55f409be1c0a73b7de044 [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>
Josh Gao2b2ae0c2017-08-21 14:31:17 -070039#include <android-base/logging.h>
Tom Cherry31121ca2017-10-10 13:30:57 -070040#include <android-base/properties.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080041#include <android-base/stringprintf.h>
Josh Gao2b2ae0c2017-08-21 14:31:17 -070042#include <android-base/strings.h>
Josh Gao57f58f82017-03-15 23:23:22 -070043#include <android-base/unique_fd.h>
44#include <android/log.h>
Mark Salyzyncfd5b082016-10-17 14:28:00 -070045#include <log/log.h>
Tom Cherryc5c6d7d2020-04-17 09:38:55 -070046#include <log/log_read.h>
Mark Salyzynff2dcd92016-09-28 15:54:45 -070047#include <log/logprint.h>
48#include <private/android_filesystem_config.h>
David Srbeckyb9cc4fb2019-04-05 18:23:32 +000049#include <unwindstack/DexFiles.h>
Christopher Ferris60eb1972019-01-15 15:18:43 -080050#include <unwindstack/JitDebug.h>
51#include <unwindstack/Maps.h>
Josh Gao2b2ae0c2017-08-21 14:31:17 -070052#include <unwindstack/Memory.h>
53#include <unwindstack/Regs.h>
Christopher Ferris60eb1972019-01-15 15:18:43 -080054#include <unwindstack/Unwinder.h>
Jeff Brown053b8652012-06-06 16:25:03 -070055
Josh Gaoc3706662017-08-29 13:08:32 -070056#include "libdebuggerd/backtrace.h"
Mitch Phillipse0b4bb12020-02-14 14:54:31 -080057#include "libdebuggerd/gwp_asan.h"
Josh Gaoc3706662017-08-29 13:08:32 -070058#include "libdebuggerd/open_files_list.h"
Josh Gao2b2ae0c2017-08-21 14:31:17 -070059#include "libdebuggerd/utility.h"
Jeff Brown053b8652012-06-06 16:25:03 -070060
Mitch Phillipse0b4bb12020-02-14 14:54:31 -080061#include "gwp_asan/common.h"
62#include "gwp_asan/crash_handler.h"
63
Tom Cherry31121ca2017-10-10 13:30:57 -070064using android::base::GetBoolProperty;
65using android::base::GetProperty;
Elliott Hughes0ba53592017-02-01 16:59:15 -080066using android::base::StringPrintf;
Josh Gao2b2ae0c2017-08-21 14:31:17 -070067using android::base::unique_fd;
68
Elliott Hughese1415a52018-02-15 09:18:21 -080069using namespace std::literals::string_literals;
70
Jeff Brown053b8652012-06-06 16:25:03 -070071#define STACK_WORDS 16
72
Brigid Smith9c8dacc2014-06-02 15:02:20 -070073static void dump_header_info(log_t* log) {
Tom Cherry31121ca2017-10-10 13:30:57 -070074 auto fingerprint = GetProperty("ro.build.fingerprint", "unknown");
75 auto revision = GetProperty("ro.revision", "unknown");
Ben Chengd7760c12012-09-19 16:04:01 -070076
Tom Cherry31121ca2017-10-10 13:30:57 -070077 _LOG(log, logtype::HEADER, "Build fingerprint: '%s'\n", fingerprint.c_str());
78 _LOG(log, logtype::HEADER, "Revision: '%s'\n", revision.c_str());
Brigid Smith62ba4892014-06-10 11:53:08 -070079 _LOG(log, logtype::HEADER, "ABI: '%s'\n", ABI_STRING);
Jeff Brown053b8652012-06-06 16:25:03 -070080}
81
Josh Gao6f4644d2018-12-14 13:05:12 -080082static void dump_timestamp(log_t* log, time_t time) {
83 struct tm tm;
84 localtime_r(&time, &tm);
85
86 char buf[strlen("1970-01-01 00:00:00+0830") + 1];
87 strftime(buf, sizeof(buf), "%F %T%z", &tm);
88 _LOG(log, logtype::HEADER, "Timestamp: %s\n", buf);
89}
90
Christopher Ferris481e8372019-07-15 17:13:24 -070091static std::string get_stack_overflow_cause(uint64_t fault_addr, uint64_t sp,
92 unwindstack::Maps* maps) {
93 static constexpr uint64_t kMaxDifferenceBytes = 256;
94 uint64_t difference;
95 if (sp >= fault_addr) {
96 difference = sp - fault_addr;
97 } else {
98 difference = fault_addr - sp;
99 }
100 if (difference <= kMaxDifferenceBytes) {
101 // The faulting address is close to the current sp, check if the sp
102 // indicates a stack overflow.
103 // On arm, the sp does not get updated when the instruction faults.
104 // In this case, the sp will still be in a valid map, which is the
105 // last case below.
106 // On aarch64, the sp does get updated when the instruction faults.
107 // In this case, the sp will be in either an invalid map if triggered
108 // on the main thread, or in a guard map if in another thread, which
109 // will be the first case or second case from below.
110 unwindstack::MapInfo* map_info = maps->Find(sp);
111 if (map_info == nullptr) {
112 return "stack pointer is in a non-existent map; likely due to stack overflow.";
113 } else if ((map_info->flags & (PROT_READ | PROT_WRITE)) != (PROT_READ | PROT_WRITE)) {
114 return "stack pointer is not in a rw map; likely due to stack overflow.";
115 } else if ((sp - map_info->start) <= kMaxDifferenceBytes) {
116 return "stack pointer is close to top of stack; likely stack overflow.";
117 }
118 }
119 return "";
120}
121
122static void dump_probable_cause(log_t* log, const siginfo_t* si, unwindstack::Maps* maps,
123 unwindstack::Regs* regs) {
Elliott Hughes0ba53592017-02-01 16:59:15 -0800124 std::string cause;
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700125 if (si->si_signo == SIGSEGV && si->si_code == SEGV_MAPERR) {
126 if (si->si_addr < reinterpret_cast<void*>(4096)) {
Elliott Hughes0ba53592017-02-01 16:59:15 -0800127 cause = StringPrintf("null pointer dereference");
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700128 } else if (si->si_addr == reinterpret_cast<void*>(0xffff0ffc)) {
Elliott Hughes0ba53592017-02-01 16:59:15 -0800129 cause = "call to kuser_helper_version";
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700130 } else if (si->si_addr == reinterpret_cast<void*>(0xffff0fe0)) {
Elliott Hughes0ba53592017-02-01 16:59:15 -0800131 cause = "call to kuser_get_tls";
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700132 } else if (si->si_addr == reinterpret_cast<void*>(0xffff0fc0)) {
Elliott Hughes0ba53592017-02-01 16:59:15 -0800133 cause = "call to kuser_cmpxchg";
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700134 } else if (si->si_addr == reinterpret_cast<void*>(0xffff0fa0)) {
Elliott Hughes0ba53592017-02-01 16:59:15 -0800135 cause = "call to kuser_memory_barrier";
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700136 } else if (si->si_addr == reinterpret_cast<void*>(0xffff0f60)) {
Elliott Hughes0ba53592017-02-01 16:59:15 -0800137 cause = "call to kuser_cmpxchg64";
Christopher Ferris481e8372019-07-15 17:13:24 -0700138 } else {
139 cause = get_stack_overflow_cause(reinterpret_cast<uint64_t>(si->si_addr), regs->sp(), maps);
Elliott Hughes0ba53592017-02-01 16:59:15 -0800140 }
Ivan Lozanodf3cec92018-11-19 10:43:47 -0800141 } else if (si->si_signo == SIGSEGV && si->si_code == SEGV_ACCERR) {
Christopher Ferris481e8372019-07-15 17:13:24 -0700142 uint64_t fault_addr = reinterpret_cast<uint64_t>(si->si_addr);
143 unwindstack::MapInfo* map_info = maps->Find(fault_addr);
Christopher Ferris60eb1972019-01-15 15:18:43 -0800144 if (map_info != nullptr && map_info->flags == PROT_EXEC) {
145 cause = "execute-only (no-read) memory access error; likely due to data in .text.";
Christopher Ferris481e8372019-07-15 17:13:24 -0700146 } else {
147 cause = get_stack_overflow_cause(fault_addr, regs->sp(), maps);
Ivan Lozanodf3cec92018-11-19 10:43:47 -0800148 }
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700149 } else if (si->si_signo == SIGSYS && si->si_code == SYS_SECCOMP) {
150 cause = StringPrintf("seccomp prevented call to disallowed %s system call %d", ABI_STRING,
151 si->si_syscall);
Elliott Hughes0ba53592017-02-01 16:59:15 -0800152 }
153
154 if (!cause.empty()) _LOG(log, logtype::HEADER, "Cause: %s\n", cause.c_str());
155}
156
Christopher Ferris60eb1972019-01-15 15:18:43 -0800157static void dump_signal_info(log_t* log, const ThreadInfo& thread_info,
Peter Collingbournef03af882020-03-20 18:09:00 -0700158 const ProcessInfo& process_info, unwindstack::Memory* process_memory) {
Elliott Hughes2baf4432018-05-30 12:55:04 -0700159 char addr_desc[64]; // ", fault addr 0x1234"
Peter Collingbournef03af882020-03-20 18:09:00 -0700160 if (process_info.has_fault_address) {
161 size_t addr = process_info.fault_address;
Elliott Hughes2baf4432018-05-30 12:55:04 -0700162 if (thread_info.siginfo->si_signo == SIGILL) {
163 uint32_t instruction = {};
Peter Collingbournef03af882020-03-20 18:09:00 -0700164 process_memory->Read(addr, &instruction, sizeof(instruction));
165 snprintf(addr_desc, sizeof(addr_desc), "0x%zx (*pc=%#08x)", addr, instruction);
Elliott Hughes2baf4432018-05-30 12:55:04 -0700166 } else {
Peter Collingbournef03af882020-03-20 18:09:00 -0700167 snprintf(addr_desc, sizeof(addr_desc), "0x%zx", addr);
Elliott Hughes2baf4432018-05-30 12:55:04 -0700168 }
Elliott Hughes855fcc32014-04-25 16:05:34 -0700169 } else {
170 snprintf(addr_desc, sizeof(addr_desc), "--------");
171 }
172
Elliott Hughes70d8f282018-04-25 17:00:14 -0700173 char sender_desc[32] = {}; // " from pid 1234, uid 666"
174 if (signal_has_sender(thread_info.siginfo, thread_info.pid)) {
175 get_signal_sender(sender_desc, sizeof(sender_desc), thread_info.siginfo);
176 }
Elliott Hughes0ba53592017-02-01 16:59:15 -0800177
Elliott Hughes70d8f282018-04-25 17:00:14 -0700178 _LOG(log, logtype::HEADER, "signal %d (%s), code %d (%s%s), fault addr %s\n",
179 thread_info.siginfo->si_signo, get_signame(thread_info.siginfo),
180 thread_info.siginfo->si_code, get_sigcode(thread_info.siginfo), sender_desc, addr_desc);
Jeff Brown053b8652012-06-06 16:25:03 -0700181}
182
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700183static void dump_thread_info(log_t* log, const ThreadInfo& thread_info) {
Mark Salyzyn45ae4462014-07-25 12:25:48 -0700184 // Blacklist logd, logd.reader, logd.writer, logd.auditd, logd.control ...
Josh Gao57f58f82017-03-15 23:23:22 -0700185 // TODO: Why is this controlled by thread name?
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700186 if (thread_info.thread_name == "logd" ||
187 android::base::StartsWith(thread_info.thread_name, "logd.")) {
Mark Salyzyn45ae4462014-07-25 12:25:48 -0700188 log->should_retrieve_logcat = false;
189 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800190
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700191 _LOG(log, logtype::HEADER, "pid: %d, tid: %d, name: %s >>> %s <<<\n", thread_info.pid,
192 thread_info.tid, thread_info.thread_name.c_str(), thread_info.process_name.c_str());
Misha Wagner39c5b8c2019-04-18 16:07:33 +0100193 _LOG(log, logtype::HEADER, "uid: %d\n", thread_info.uid);
Christopher Ferris20303f82014-01-10 16:33:16 -0800194}
Jeff Brown053b8652012-06-06 16:25:03 -0700195
Christopher Ferris7937a362018-01-18 11:15:49 -0800196static std::string get_addr_string(uint64_t addr) {
Christopher Ferris862fe022015-06-02 14:52:44 -0700197 std::string addr_str;
198#if defined(__LP64__)
Elliott Hughes0ba53592017-02-01 16:59:15 -0800199 addr_str = StringPrintf("%08x'%08x",
200 static_cast<uint32_t>(addr >> 32),
201 static_cast<uint32_t>(addr & 0xffffffff));
Christopher Ferris862fe022015-06-02 14:52:44 -0700202#else
Christopher Ferris7937a362018-01-18 11:15:49 -0800203 addr_str = StringPrintf("%08x", static_cast<uint32_t>(addr));
Christopher Ferris862fe022015-06-02 14:52:44 -0700204#endif
205 return addr_str;
206}
207
Christopher Ferris60eb1972019-01-15 15:18:43 -0800208static void dump_abort_message(log_t* log, unwindstack::Memory* process_memory, uint64_t address) {
Josh Gao7c89f9e2016-01-13 17:57:14 -0800209 if (address == 0) {
210 return;
211 }
212
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700213 size_t length;
214 if (!process_memory->ReadFully(address, &length, sizeof(length))) {
215 _LOG(log, logtype::HEADER, "Failed to read abort message header: %s\n", strerror(errno));
216 return;
217 }
Josh Gao7c89f9e2016-01-13 17:57:14 -0800218
Josh Gao1cc7bd82018-02-13 13:16:17 -0800219 // The length field includes the length of the length field itself.
220 if (length < sizeof(size_t)) {
221 _LOG(log, logtype::HEADER, "Abort message header malformed: claimed length = %zd\n", length);
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700222 return;
Josh Gao7c89f9e2016-01-13 17:57:14 -0800223 }
Josh Gao7c89f9e2016-01-13 17:57:14 -0800224
Josh Gao1cc7bd82018-02-13 13:16:17 -0800225 length -= sizeof(size_t);
226
Josh Gao83b8ac22018-04-20 17:31:53 -0700227 // The abort message should be null terminated already, but reserve a spot for NUL just in case.
228 std::vector<char> msg(length + 1);
Josh Gao1cc7bd82018-02-13 13:16:17 -0800229 if (!process_memory->ReadFully(address + sizeof(length), &msg[0], length)) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700230 _LOG(log, logtype::HEADER, "Failed to read abort message: %s\n", strerror(errno));
231 return;
232 }
233
Josh Gao1cc7bd82018-02-13 13:16:17 -0800234 _LOG(log, logtype::HEADER, "Abort message: '%s'\n", &msg[0]);
Josh Gao7c89f9e2016-01-13 17:57:14 -0800235}
236
Christopher Ferris60eb1972019-01-15 15:18:43 -0800237static void dump_all_maps(log_t* log, unwindstack::Unwinder* unwinder, uint64_t addr) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700238 bool print_fault_address_marker = addr;
Christopher Ferris20303f82014-01-10 16:33:16 -0800239
Christopher Ferris60eb1972019-01-15 15:18:43 -0800240 unwindstack::Maps* maps = unwinder->GetMaps();
Elliott Hughes868d39a2017-09-26 11:54:49 -0700241 _LOG(log, logtype::MAPS,
242 "\n"
Josh Gao1ce8e142017-09-27 13:59:42 -0700243 "memory map (%zu entr%s):",
Christopher Ferris60eb1972019-01-15 15:18:43 -0800244 maps->Total(), maps->Total() == 1 ? "y" : "ies");
Elliott Hughes868d39a2017-09-26 11:54:49 -0700245 if (print_fault_address_marker) {
Christopher Ferris60eb1972019-01-15 15:18:43 -0800246 if (maps->Total() != 0 && addr < maps->Get(0)->start) {
Josh Gao1ce8e142017-09-27 13:59:42 -0700247 _LOG(log, logtype::MAPS, "\n--->Fault address falls at %s before any mapped regions\n",
Christopher Ferris862fe022015-06-02 14:52:44 -0700248 get_addr_string(addr).c_str());
Christopher Ferrisa21bd932015-02-27 13:39:47 -0800249 print_fault_address_marker = false;
Elliott Hughes868d39a2017-09-26 11:54:49 -0700250 } else {
Josh Gao1ce8e142017-09-27 13:59:42 -0700251 _LOG(log, logtype::MAPS, " (fault address prefixed with --->)\n");
Brigid Smith8606eaa2014-07-07 12:33:50 -0700252 }
Josh Gao1ce8e142017-09-27 13:59:42 -0700253 } else {
254 _LOG(log, logtype::MAPS, "\n");
Brigid Smith8606eaa2014-07-07 12:33:50 -0700255 }
Christopher Ferrisa21bd932015-02-27 13:39:47 -0800256
Christopher Ferris60eb1972019-01-15 15:18:43 -0800257 std::shared_ptr<unwindstack::Memory>& process_memory = unwinder->GetProcessMemory();
258
Christopher Ferrisa21bd932015-02-27 13:39:47 -0800259 std::string line;
Florian Mayer3d67d342019-02-27 18:00:37 +0000260 for (auto const& map_info : *maps) {
Christopher Ferrisa21bd932015-02-27 13:39:47 -0800261 line = " ";
262 if (print_fault_address_marker) {
Christopher Ferris60eb1972019-01-15 15:18:43 -0800263 if (addr < map_info->start) {
Christopher Ferris862fe022015-06-02 14:52:44 -0700264 _LOG(log, logtype::MAPS, "--->Fault address falls at %s between mapped regions\n",
265 get_addr_string(addr).c_str());
Christopher Ferrisa21bd932015-02-27 13:39:47 -0800266 print_fault_address_marker = false;
Christopher Ferris60eb1972019-01-15 15:18:43 -0800267 } else if (addr >= map_info->start && addr < map_info->end) {
Christopher Ferrisa21bd932015-02-27 13:39:47 -0800268 line = "--->";
269 print_fault_address_marker = false;
270 }
271 }
Christopher Ferris60eb1972019-01-15 15:18:43 -0800272 line += get_addr_string(map_info->start) + '-' + get_addr_string(map_info->end - 1) + ' ';
273 if (map_info->flags & PROT_READ) {
Christopher Ferrisa21bd932015-02-27 13:39:47 -0800274 line += 'r';
275 } else {
276 line += '-';
277 }
Christopher Ferris60eb1972019-01-15 15:18:43 -0800278 if (map_info->flags & PROT_WRITE) {
Christopher Ferrisa21bd932015-02-27 13:39:47 -0800279 line += 'w';
280 } else {
281 line += '-';
282 }
Christopher Ferris60eb1972019-01-15 15:18:43 -0800283 if (map_info->flags & PROT_EXEC) {
Christopher Ferrisa21bd932015-02-27 13:39:47 -0800284 line += 'x';
285 } else {
286 line += '-';
287 }
Christopher Ferris60eb1972019-01-15 15:18:43 -0800288 line += StringPrintf(" %8" PRIx64 " %8" PRIx64, map_info->offset,
289 map_info->end - map_info->start);
Christopher Ferris862fe022015-06-02 14:52:44 -0700290 bool space_needed = true;
Christopher Ferris60eb1972019-01-15 15:18:43 -0800291 if (!map_info->name.empty()) {
Christopher Ferris862fe022015-06-02 14:52:44 -0700292 space_needed = false;
Christopher Ferris60eb1972019-01-15 15:18:43 -0800293 line += " " + map_info->name;
294 std::string build_id = map_info->GetPrintableBuildID();
295 if (!build_id.empty()) {
Christopher Ferrisa21bd932015-02-27 13:39:47 -0800296 line += " (BuildId: " + build_id + ")";
297 }
298 }
Christopher Ferris60eb1972019-01-15 15:18:43 -0800299 uint64_t load_bias = map_info->GetLoadBias(process_memory);
300 if (load_bias != 0) {
Christopher Ferris862fe022015-06-02 14:52:44 -0700301 if (space_needed) {
302 line += ' ';
303 }
Christopher Ferris60eb1972019-01-15 15:18:43 -0800304 line += StringPrintf(" (load bias 0x%" PRIx64 ")", load_bias);
Christopher Ferris2106f4b2015-05-01 15:02:03 -0700305 }
Christopher Ferrisa21bd932015-02-27 13:39:47 -0800306 _LOG(log, logtype::MAPS, "%s\n", line.c_str());
307 }
308 if (print_fault_address_marker) {
Christopher Ferris862fe022015-06-02 14:52:44 -0700309 _LOG(log, logtype::MAPS, "--->Fault address falls at %s after any mapped regions\n",
310 get_addr_string(addr).c_str());
Christopher Ferris20303f82014-01-10 16:33:16 -0800311 }
Jeff Brown053b8652012-06-06 16:25:03 -0700312}
313
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700314static void print_register_row(log_t* log,
315 const std::vector<std::pair<std::string, uint64_t>>& registers) {
316 std::string output;
317 for (auto& [name, value] : registers) {
Christopher Ferris7937a362018-01-18 11:15:49 -0800318 output += android::base::StringPrintf(" %-3s %0*" PRIx64, name.c_str(),
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700319 static_cast<int>(2 * sizeof(void*)),
Christopher Ferris7937a362018-01-18 11:15:49 -0800320 static_cast<uint64_t>(value));
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700321 }
322
323 _LOG(log, logtype::REGISTERS, " %s\n", output.c_str());
Josh Gao77b00ed2017-05-05 18:11:23 -0700324}
325
Christopher Ferris60eb1972019-01-15 15:18:43 -0800326void dump_registers(log_t* log, unwindstack::Regs* regs) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700327 // Split lr/sp/pc into their own special row.
328 static constexpr size_t column_count = 4;
329 std::vector<std::pair<std::string, uint64_t>> current_row;
330 std::vector<std::pair<std::string, uint64_t>> special_row;
331
332#if defined(__arm__) || defined(__aarch64__)
Peter Collingbournebb2f9412019-11-18 12:36:50 -0800333 static constexpr const char* special_registers[] = {"ip", "lr", "sp", "pc", "pst"};
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700334#elif defined(__i386__)
335 static constexpr const char* special_registers[] = {"ebp", "esp", "eip"};
336#elif defined(__x86_64__)
337 static constexpr const char* special_registers[] = {"rbp", "rsp", "rip"};
338#else
339 static constexpr const char* special_registers[] = {};
340#endif
341
342 regs->IterateRegisters([log, &current_row, &special_row](const char* name, uint64_t value) {
343 auto row = &current_row;
344 for (const char* special_name : special_registers) {
345 if (strcmp(special_name, name) == 0) {
346 row = &special_row;
347 break;
348 }
349 }
350
351 row->emplace_back(name, value);
352 if (current_row.size() == column_count) {
353 print_register_row(log, current_row);
354 current_row.clear();
355 }
356 });
357
358 if (!current_row.empty()) {
359 print_register_row(log, current_row);
360 }
361
362 print_register_row(log, special_row);
363}
364
Christopher Ferris60eb1972019-01-15 15:18:43 -0800365void dump_memory_and_code(log_t* log, unwindstack::Maps* maps, unwindstack::Memory* memory,
366 unwindstack::Regs* regs) {
367 regs->IterateRegisters([log, maps, memory](const char* reg_name, uint64_t reg_value) {
Elliott Hughese1415a52018-02-15 09:18:21 -0800368 std::string label{"memory near "s + reg_name};
Christopher Ferris60eb1972019-01-15 15:18:43 -0800369 if (maps) {
370 unwindstack::MapInfo* map_info = maps->Find(reg_value);
371 if (map_info != nullptr && !map_info->name.empty()) {
372 label += " (" + map_info->name + ")";
373 }
Elliott Hughese1415a52018-02-15 09:18:21 -0800374 }
375 dump_memory(log, memory, reg_value, label);
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700376 });
377}
378
Christopher Ferris60eb1972019-01-15 15:18:43 -0800379static bool dump_thread(log_t* log, unwindstack::Unwinder* unwinder, const ThreadInfo& thread_info,
Peter Collingbourne843f7e62020-02-28 19:07:33 -0800380 const ProcessInfo& process_info, bool primary_thread) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700381 log->current_tid = thread_info.tid;
Josh Gao7c89f9e2016-01-13 17:57:14 -0800382 if (!primary_thread) {
Brigid Smith62ba4892014-06-10 11:53:08 -0700383 _LOG(log, logtype::THREAD, "--- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---\n");
Josh Gao7c89f9e2016-01-13 17:57:14 -0800384 }
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700385 dump_thread_info(log, thread_info);
Christopher Ferris20303f82014-01-10 16:33:16 -0800386
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700387 if (thread_info.siginfo) {
Peter Collingbournef03af882020-03-20 18:09:00 -0700388 dump_signal_info(log, thread_info, process_info, unwinder->GetProcessMemory().get());
Mitch Phillipse0b4bb12020-02-14 14:54:31 -0800389 }
390
Peter Collingbourne843f7e62020-02-28 19:07:33 -0800391 std::unique_ptr<GwpAsanCrashData> gwp_asan_crash_data;
392 if (primary_thread) {
393 gwp_asan_crash_data = std::make_unique<GwpAsanCrashData>(unwinder->GetProcessMemory().get(),
394 process_info, thread_info);
395 }
396
397 if (primary_thread && gwp_asan_crash_data->CrashIsMine()) {
398 gwp_asan_crash_data->DumpCause(log);
Mitch Phillipse0b4bb12020-02-14 14:54:31 -0800399 } else if (thread_info.siginfo) {
400 dump_probable_cause(log, thread_info.siginfo, unwinder->GetMaps(),
401 thread_info.registers.get());
Josh Gao7c89f9e2016-01-13 17:57:14 -0800402 }
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700403
Josh Gao34c25562017-12-22 14:18:39 -0800404 if (primary_thread) {
Peter Collingbourne843f7e62020-02-28 19:07:33 -0800405 dump_abort_message(log, unwinder->GetProcessMemory().get(), process_info.abort_msg_address);
Josh Gao34c25562017-12-22 14:18:39 -0800406 }
407
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700408 dump_registers(log, thread_info.registers.get());
409
Josh Gao5d1c14f2018-04-20 12:04:41 -0700410 // Unwind will mutate the registers, so make a copy first.
Christopher Ferris60eb1972019-01-15 15:18:43 -0800411 std::unique_ptr<unwindstack::Regs> regs_copy(thread_info.registers->Clone());
412 unwinder->SetRegs(regs_copy.get());
413 unwinder->Unwind();
414 if (unwinder->NumFrames() == 0) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700415 _LOG(log, logtype::THREAD, "Failed to unwind");
Christopher Ferris60eb1972019-01-15 15:18:43 -0800416 } else {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700417 _LOG(log, logtype::BACKTRACE, "\nbacktrace:\n");
Christopher Ferris4ae266c2019-04-03 09:27:12 -0700418 log_backtrace(log, unwinder, " ");
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) {
Peter Collingbourne843f7e62020-02-28 19:07:33 -0800422 if (gwp_asan_crash_data->HasDeallocationTrace()) {
423 gwp_asan_crash_data->DumpDeallocationTrace(log, unwinder);
Mitch Phillipse0b4bb12020-02-14 14:54:31 -0800424 }
425
Peter Collingbourne843f7e62020-02-28 19:07:33 -0800426 if (gwp_asan_crash_data->HasAllocationTrace()) {
427 gwp_asan_crash_data->DumpAllocationTrace(log, unwinder);
Mitch Phillipse0b4bb12020-02-14 14:54:31 -0800428 }
429
Christopher Ferris60eb1972019-01-15 15:18:43 -0800430 unwindstack::Maps* maps = unwinder->GetMaps();
431 dump_memory_and_code(log, maps, unwinder->GetProcessMemory().get(),
432 thread_info.registers.get());
433 if (maps != nullptr) {
Christopher Ferris7937a362018-01-18 11:15:49 -0800434 uint64_t addr = 0;
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700435 siginfo_t* si = thread_info.siginfo;
Elliott Hughes70d8f282018-04-25 17:00:14 -0700436 if (signal_has_si_addr(si)) {
Christopher Ferris7937a362018-01-18 11:15:49 -0800437 addr = reinterpret_cast<uint64_t>(si->si_addr);
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700438 }
Christopher Ferris60eb1972019-01-15 15:18:43 -0800439 dump_all_maps(log, unwinder, addr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800440 }
441 }
442
Josh Gao7c89f9e2016-01-13 17:57:14 -0800443 log->current_tid = log->crashed_tid;
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700444 return true;
Jeff Brown053b8652012-06-06 16:25:03 -0700445}
446
Christopher Ferris20303f82014-01-10 16:33:16 -0800447// Reads the contents of the specified log device, filters out the entries
448// that don't match the specified pid, and writes them to the tombstone file.
449//
Mark Salyzyn17e85c02014-06-27 15:55:19 -0700450// If "tail" is non-zero, log the last "tail" number of lines.
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700451static void dump_log_file(log_t* log, pid_t pid, const char* filename, unsigned int tail) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800452 bool first = true;
Christopher Ferrisc637ada2018-07-13 16:55:38 -0700453 logger_list* logger_list;
Jeff Brown053b8652012-06-06 16:25:03 -0700454
Mark Salyzyn45ae4462014-07-25 12:25:48 -0700455 if (!log->should_retrieve_logcat) {
456 return;
457 }
458
Tom Cherry907b2d02020-03-23 13:40:10 -0700459 logger_list =
460 android_logger_list_open(android_name_to_log_id(filename), ANDROID_LOG_NONBLOCK, tail, pid);
Jeff Brown053b8652012-06-06 16:25:03 -0700461
Mark Salyzyn22b5cef2013-11-22 10:53:34 -0800462 if (!logger_list) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700463 ALOGE("Unable to open %s: %s\n", filename, strerror(errno));
Christopher Ferris20303f82014-01-10 16:33:16 -0800464 return;
465 }
466
Christopher Ferris20303f82014-01-10 16:33:16 -0800467 while (true) {
Christopher Ferrisc637ada2018-07-13 16:55:38 -0700468 log_msg log_entry;
Mark Salyzyn22b5cef2013-11-22 10:53:34 -0800469 ssize_t actual = android_logger_list_read(logger_list, &log_entry);
470
Christopher Ferris20303f82014-01-10 16:33:16 -0800471 if (actual < 0) {
Mark Salyzyn22b5cef2013-11-22 10:53:34 -0800472 if (actual == -EINTR) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800473 // interrupted by signal, retry
474 continue;
Mark Salyzyn22b5cef2013-11-22 10:53:34 -0800475 } else if (actual == -EAGAIN) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800476 // non-blocking EOF; we're done
477 break;
478 } else {
Christopher Ferrisb36b5922015-06-17 18:35:59 -0700479 ALOGE("Error while reading log: %s\n", strerror(-actual));
Christopher Ferris20303f82014-01-10 16:33:16 -0800480 break;
481 }
482 } else if (actual == 0) {
Christopher Ferrisb36b5922015-06-17 18:35:59 -0700483 ALOGE("Got zero bytes while reading log: %s\n", strerror(errno));
Christopher Ferris20303f82014-01-10 16:33:16 -0800484 break;
Jeff Brown053b8652012-06-06 16:25:03 -0700485 }
486
Brigid Smith50eb5462014-06-18 14:17:57 -0700487 // NOTE: if you ALOGV something here, this will spin forever,
Christopher Ferris20303f82014-01-10 16:33:16 -0800488 // because you will be writing as fast as you're reading. Any
489 // high-frequency debug diagnostics should just be written to
490 // the tombstone file.
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).
Mark Salyzyn989980c2014-05-14 12:37:22 -0700502 char timeBuf[32];
Christopher Ferrisc637ada2018-07-13 16:55:38 -0700503 time_t sec = static_cast<time_t>(log_entry.entry.sec);
Mark Salyzyn989980c2014-05-14 12:37:22 -0700504 struct tm tmBuf;
505 struct tm* ptm;
506 ptm = localtime_r(&sec, &tmBuf);
507 strftime(timeBuf, sizeof(timeBuf), "%m-%d %H:%M:%S", ptm);
508
Christopher Ferrisc637ada2018-07-13 16:55:38 -0700509 char* msg = log_entry.msg();
510 if (msg == nullptr) {
511 continue;
512 }
Mark Salyzyn22b5cef2013-11-22 10:53:34 -0800513 unsigned char prio = msg[0];
514 char* tag = msg + 1;
515 msg = tag + strlen(tag) + 1;
Christopher Ferris20303f82014-01-10 16:33:16 -0800516
517 // consume any trailing newlines
Mark Salyzynfca0bd12013-12-12 12:21:20 -0800518 char* nl = msg + strlen(msg) - 1;
519 while (nl >= msg && *nl == '\n') {
Mark Salyzyn17e85c02014-06-27 15:55:19 -0700520 *nl-- = '\0';
Christopher Ferris20303f82014-01-10 16:33:16 -0800521 }
522
Christopher Ferrisc637ada2018-07-13 16:55:38 -0700523 static const char* kPrioChars = "!.VDIWEFS";
Christopher Ferris20303f82014-01-10 16:33:16 -0800524 char prioChar = (prio < strlen(kPrioChars) ? kPrioChars[prio] : '?');
525
Mark Salyzynfca0bd12013-12-12 12:21:20 -0800526 // Look for line breaks ('\n') and display each text line
527 // on a separate line, prefixed with the header, like logcat does.
528 do {
529 nl = strchr(msg, '\n');
Christopher Ferrisc637ada2018-07-13 16:55:38 -0700530 if (nl != nullptr) {
Mark Salyzynfca0bd12013-12-12 12:21:20 -0800531 *nl = '\0';
532 ++nl;
533 }
534
Christopher Ferrisc637ada2018-07-13 16:55:38 -0700535 _LOG(log, logtype::LOGS, "%s.%03d %5d %5d %c %-8s: %s\n", timeBuf,
536 log_entry.entry.nsec / 1000000, log_entry.entry.pid, log_entry.entry.tid, prioChar, tag,
537 msg);
Mark Salyzynfca0bd12013-12-12 12:21:20 -0800538 } while ((msg = nl));
Christopher Ferris20303f82014-01-10 16:33:16 -0800539 }
Jeff Brown053b8652012-06-06 16:25:03 -0700540
Mark Salyzyn22b5cef2013-11-22 10:53:34 -0800541 android_logger_list_free(logger_list);
Jeff Brown053b8652012-06-06 16:25:03 -0700542}
543
Christopher Ferris20303f82014-01-10 16:33:16 -0800544// Dumps the logs generated by the specified pid to the tombstone, from both
545// "system" and "main" log devices. Ideally we'd interleave the output.
Mark Salyzyn17e85c02014-06-27 15:55:19 -0700546static void dump_logs(log_t* log, pid_t pid, unsigned int tail) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700547 if (pid == getpid()) {
548 // Cowardly refuse to dump logs while we're running in-process.
549 return;
550 }
551
Mark Salyzyn22b5cef2013-11-22 10:53:34 -0800552 dump_log_file(log, pid, "system", tail);
553 dump_log_file(log, pid, "main", tail);
Jeff Brown053b8652012-06-06 16:25:03 -0700554}
555
Christopher Ferris7937a362018-01-18 11:15:49 -0800556void engrave_tombstone_ucontext(int tombstone_fd, uint64_t abort_msg_address, siginfo_t* siginfo,
Josh Gaoe1aa0ca2017-03-01 17:23:22 -0800557 ucontext_t* ucontext) {
Misha Wagner39c5b8c2019-04-18 16:07:33 +0100558 pid_t uid = getuid();
Josh Gaoe1aa0ca2017-03-01 17:23:22 -0800559 pid_t pid = getpid();
560 pid_t tid = gettid();
561
Josh Gaoe73c9322017-02-08 16:06:26 -0800562 log_t log;
563 log.current_tid = tid;
564 log.crashed_tid = tid;
565 log.tfd = tombstone_fd;
566 log.amfd_data = nullptr;
567
Josh Gao57f58f82017-03-15 23:23:22 -0700568 char thread_name[16];
569 char process_name[128];
570
571 read_with_default("/proc/self/comm", thread_name, sizeof(thread_name), "<unknown>");
572 read_with_default("/proc/self/cmdline", process_name, sizeof(process_name), "<unknown>");
573
Christopher Ferris60eb1972019-01-15 15:18:43 -0800574 std::unique_ptr<unwindstack::Regs> regs(
575 unwindstack::Regs::CreateFromUcontext(unwindstack::Regs::CurrentArch(), ucontext));
Josh Gaoe73c9322017-02-08 16:06:26 -0800576
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700577 std::map<pid_t, ThreadInfo> threads;
578 threads[gettid()] = ThreadInfo{
579 .registers = std::move(regs),
Misha Wagner39c5b8c2019-04-18 16:07:33 +0100580 .uid = uid,
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700581 .tid = tid,
582 .thread_name = thread_name,
583 .pid = pid,
584 .process_name = process_name,
585 .siginfo = siginfo,
586 };
Josh Gao77b00ed2017-05-05 18:11:23 -0700587
Christopher Ferris60eb1972019-01-15 15:18:43 -0800588 unwindstack::UnwinderFromPid unwinder(kMaxFrames, pid);
David Srbeckyb9cc4fb2019-04-05 18:23:32 +0000589 if (!unwinder.Init(unwindstack::Regs::CurrentArch())) {
Christopher Ferris60eb1972019-01-15 15:18:43 -0800590 LOG(FATAL) << "Failed to init unwinder object.";
Josh Gaoe73c9322017-02-08 16:06:26 -0800591 }
Josh Gaofdc95c92017-09-13 15:33:39 -0700592
Peter Collingbourne843f7e62020-02-28 19:07:33 -0800593 ProcessInfo process_info;
594 process_info.abort_msg_address = abort_msg_address;
595 engrave_tombstone(unique_fd(dup(tombstone_fd)), &unwinder, threads, tid, process_info, nullptr,
596 nullptr);
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700597}
598
Christopher Ferris60eb1972019-01-15 15:18:43 -0800599void engrave_tombstone(unique_fd output_fd, unwindstack::Unwinder* unwinder,
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700600 const std::map<pid_t, ThreadInfo>& threads, pid_t target_thread,
Peter Collingbourne843f7e62020-02-28 19:07:33 -0800601 const ProcessInfo& process_info, OpenFilesList* open_files,
602 std::string* amfd_data) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700603 // don't copy log messages to tombstone unless this is a dev device
604 bool want_logs = android::base::GetBoolProperty("ro.debuggable", false);
605
606 log_t log;
607 log.current_tid = target_thread;
608 log.crashed_tid = target_thread;
609 log.tfd = output_fd.get();
610 log.amfd_data = amfd_data;
611
612 _LOG(&log, logtype::HEADER, "*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***\n");
613 dump_header_info(&log);
Josh Gao6f4644d2018-12-14 13:05:12 -0800614 dump_timestamp(&log, time(nullptr));
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700615
616 auto it = threads.find(target_thread);
617 if (it == threads.end()) {
618 LOG(FATAL) << "failed to find target thread";
619 }
Mitch Phillipse0b4bb12020-02-14 14:54:31 -0800620
Peter Collingbourne843f7e62020-02-28 19:07:33 -0800621 dump_thread(&log, unwinder, it->second, process_info, true);
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700622
623 if (want_logs) {
chirag honnavardef08882017-04-20 18:22:06 +0900624 dump_logs(&log, it->second.pid, 50);
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700625 }
626
627 for (auto& [tid, thread_info] : threads) {
628 if (tid == target_thread) {
629 continue;
Josh Gaofdc95c92017-09-13 15:33:39 -0700630 }
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700631
Peter Collingbourne843f7e62020-02-28 19:07:33 -0800632 dump_thread(&log, unwinder, thread_info, process_info, false);
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700633 }
634
635 if (open_files) {
636 _LOG(&log, logtype::OPEN_FILES, "\nopen files:\n");
637 dump_open_files_list(&log, *open_files, " ");
638 }
639
640 if (want_logs) {
641 dump_logs(&log, it->second.pid, 0);
Josh Gaofdc95c92017-09-13 15:33:39 -0700642 }
Josh Gaoe73c9322017-02-08 16:06:26 -0800643}