blob: b64e260a964cf40307cd1a2ccd914c2a924b4891 [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>
Mark Salyzynff2dcd92016-09-28 15:54:45 -070046#include <log/logprint.h>
47#include <private/android_filesystem_config.h>
David Srbeckyb9cc4fb2019-04-05 18:23:32 +000048#include <unwindstack/DexFiles.h>
Christopher Ferris60eb1972019-01-15 15:18:43 -080049#include <unwindstack/JitDebug.h>
50#include <unwindstack/Maps.h>
Josh Gao2b2ae0c2017-08-21 14:31:17 -070051#include <unwindstack/Memory.h>
52#include <unwindstack/Regs.h>
Christopher Ferris60eb1972019-01-15 15:18:43 -080053#include <unwindstack/Unwinder.h>
Jeff Brown053b8652012-06-06 16:25:03 -070054
Josh Gaoc3706662017-08-29 13:08:32 -070055#include "libdebuggerd/backtrace.h"
Josh Gaoc3706662017-08-29 13:08:32 -070056#include "libdebuggerd/open_files_list.h"
Josh Gao2b2ae0c2017-08-21 14:31:17 -070057#include "libdebuggerd/utility.h"
Jeff Brown053b8652012-06-06 16:25:03 -070058
Tom Cherry31121ca2017-10-10 13:30:57 -070059using android::base::GetBoolProperty;
60using android::base::GetProperty;
Elliott Hughes0ba53592017-02-01 16:59:15 -080061using android::base::StringPrintf;
Josh Gao2b2ae0c2017-08-21 14:31:17 -070062using android::base::unique_fd;
63
Elliott Hughese1415a52018-02-15 09:18:21 -080064using namespace std::literals::string_literals;
65
Jeff Brown053b8652012-06-06 16:25:03 -070066#define STACK_WORDS 16
67
Brigid Smith9c8dacc2014-06-02 15:02:20 -070068static void dump_header_info(log_t* log) {
Tom Cherry31121ca2017-10-10 13:30:57 -070069 auto fingerprint = GetProperty("ro.build.fingerprint", "unknown");
70 auto revision = GetProperty("ro.revision", "unknown");
Ben Chengd7760c12012-09-19 16:04:01 -070071
Tom Cherry31121ca2017-10-10 13:30:57 -070072 _LOG(log, logtype::HEADER, "Build fingerprint: '%s'\n", fingerprint.c_str());
73 _LOG(log, logtype::HEADER, "Revision: '%s'\n", revision.c_str());
Brigid Smith62ba4892014-06-10 11:53:08 -070074 _LOG(log, logtype::HEADER, "ABI: '%s'\n", ABI_STRING);
Jeff Brown053b8652012-06-06 16:25:03 -070075}
76
Josh Gao6f4644d2018-12-14 13:05:12 -080077static void dump_timestamp(log_t* log, time_t time) {
78 struct tm tm;
79 localtime_r(&time, &tm);
80
81 char buf[strlen("1970-01-01 00:00:00+0830") + 1];
82 strftime(buf, sizeof(buf), "%F %T%z", &tm);
83 _LOG(log, logtype::HEADER, "Timestamp: %s\n", buf);
84}
85
Christopher Ferris481e8372019-07-15 17:13:24 -070086static std::string get_stack_overflow_cause(uint64_t fault_addr, uint64_t sp,
87 unwindstack::Maps* maps) {
88 static constexpr uint64_t kMaxDifferenceBytes = 256;
89 uint64_t difference;
90 if (sp >= fault_addr) {
91 difference = sp - fault_addr;
92 } else {
93 difference = fault_addr - sp;
94 }
95 if (difference <= kMaxDifferenceBytes) {
96 // The faulting address is close to the current sp, check if the sp
97 // indicates a stack overflow.
98 // On arm, the sp does not get updated when the instruction faults.
99 // In this case, the sp will still be in a valid map, which is the
100 // last case below.
101 // On aarch64, the sp does get updated when the instruction faults.
102 // In this case, the sp will be in either an invalid map if triggered
103 // on the main thread, or in a guard map if in another thread, which
104 // will be the first case or second case from below.
105 unwindstack::MapInfo* map_info = maps->Find(sp);
106 if (map_info == nullptr) {
107 return "stack pointer is in a non-existent map; likely due to stack overflow.";
108 } else if ((map_info->flags & (PROT_READ | PROT_WRITE)) != (PROT_READ | PROT_WRITE)) {
109 return "stack pointer is not in a rw map; likely due to stack overflow.";
110 } else if ((sp - map_info->start) <= kMaxDifferenceBytes) {
111 return "stack pointer is close to top of stack; likely stack overflow.";
112 }
113 }
114 return "";
115}
116
117static void dump_probable_cause(log_t* log, const siginfo_t* si, unwindstack::Maps* maps,
118 unwindstack::Regs* regs) {
Elliott Hughes0ba53592017-02-01 16:59:15 -0800119 std::string cause;
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700120 if (si->si_signo == SIGSEGV && si->si_code == SEGV_MAPERR) {
121 if (si->si_addr < reinterpret_cast<void*>(4096)) {
Elliott Hughes0ba53592017-02-01 16:59:15 -0800122 cause = StringPrintf("null pointer dereference");
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700123 } else if (si->si_addr == reinterpret_cast<void*>(0xffff0ffc)) {
Elliott Hughes0ba53592017-02-01 16:59:15 -0800124 cause = "call to kuser_helper_version";
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700125 } else if (si->si_addr == reinterpret_cast<void*>(0xffff0fe0)) {
Elliott Hughes0ba53592017-02-01 16:59:15 -0800126 cause = "call to kuser_get_tls";
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700127 } else if (si->si_addr == reinterpret_cast<void*>(0xffff0fc0)) {
Elliott Hughes0ba53592017-02-01 16:59:15 -0800128 cause = "call to kuser_cmpxchg";
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700129 } else if (si->si_addr == reinterpret_cast<void*>(0xffff0fa0)) {
Elliott Hughes0ba53592017-02-01 16:59:15 -0800130 cause = "call to kuser_memory_barrier";
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700131 } else if (si->si_addr == reinterpret_cast<void*>(0xffff0f60)) {
Elliott Hughes0ba53592017-02-01 16:59:15 -0800132 cause = "call to kuser_cmpxchg64";
Christopher Ferris481e8372019-07-15 17:13:24 -0700133 } else {
134 cause = get_stack_overflow_cause(reinterpret_cast<uint64_t>(si->si_addr), regs->sp(), maps);
Elliott Hughes0ba53592017-02-01 16:59:15 -0800135 }
Ivan Lozanodf3cec92018-11-19 10:43:47 -0800136 } else if (si->si_signo == SIGSEGV && si->si_code == SEGV_ACCERR) {
Christopher Ferris481e8372019-07-15 17:13:24 -0700137 uint64_t fault_addr = reinterpret_cast<uint64_t>(si->si_addr);
138 unwindstack::MapInfo* map_info = maps->Find(fault_addr);
Christopher Ferris60eb1972019-01-15 15:18:43 -0800139 if (map_info != nullptr && map_info->flags == PROT_EXEC) {
140 cause = "execute-only (no-read) memory access error; likely due to data in .text.";
Christopher Ferris481e8372019-07-15 17:13:24 -0700141 } else {
142 cause = get_stack_overflow_cause(fault_addr, regs->sp(), maps);
Ivan Lozanodf3cec92018-11-19 10:43:47 -0800143 }
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700144 } else if (si->si_signo == SIGSYS && si->si_code == SYS_SECCOMP) {
145 cause = StringPrintf("seccomp prevented call to disallowed %s system call %d", ABI_STRING,
146 si->si_syscall);
Elliott Hughes0ba53592017-02-01 16:59:15 -0800147 }
148
149 if (!cause.empty()) _LOG(log, logtype::HEADER, "Cause: %s\n", cause.c_str());
150}
151
Christopher Ferris60eb1972019-01-15 15:18:43 -0800152static void dump_signal_info(log_t* log, const ThreadInfo& thread_info,
153 unwindstack::Memory* process_memory) {
Elliott Hughes2baf4432018-05-30 12:55:04 -0700154 char addr_desc[64]; // ", fault addr 0x1234"
Elliott Hughes70d8f282018-04-25 17:00:14 -0700155 if (signal_has_si_addr(thread_info.siginfo)) {
Elliott Hughes2baf4432018-05-30 12:55:04 -0700156 void* addr = thread_info.siginfo->si_addr;
157 if (thread_info.siginfo->si_signo == SIGILL) {
158 uint32_t instruction = {};
159 process_memory->Read(reinterpret_cast<uint64_t>(addr), &instruction, sizeof(instruction));
160 snprintf(addr_desc, sizeof(addr_desc), "%p (*pc=%#08x)", addr, instruction);
161 } else {
162 snprintf(addr_desc, sizeof(addr_desc), "%p", addr);
163 }
Elliott Hughes855fcc32014-04-25 16:05:34 -0700164 } else {
165 snprintf(addr_desc, sizeof(addr_desc), "--------");
166 }
167
Elliott Hughes70d8f282018-04-25 17:00:14 -0700168 char sender_desc[32] = {}; // " from pid 1234, uid 666"
169 if (signal_has_sender(thread_info.siginfo, thread_info.pid)) {
170 get_signal_sender(sender_desc, sizeof(sender_desc), thread_info.siginfo);
171 }
Elliott Hughes0ba53592017-02-01 16:59:15 -0800172
Elliott Hughes70d8f282018-04-25 17:00:14 -0700173 _LOG(log, logtype::HEADER, "signal %d (%s), code %d (%s%s), fault addr %s\n",
174 thread_info.siginfo->si_signo, get_signame(thread_info.siginfo),
175 thread_info.siginfo->si_code, get_sigcode(thread_info.siginfo), sender_desc, addr_desc);
Jeff Brown053b8652012-06-06 16:25:03 -0700176}
177
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700178static void dump_thread_info(log_t* log, const ThreadInfo& thread_info) {
Mark Salyzyn45ae4462014-07-25 12:25:48 -0700179 // Blacklist logd, logd.reader, logd.writer, logd.auditd, logd.control ...
Josh Gao57f58f82017-03-15 23:23:22 -0700180 // TODO: Why is this controlled by thread name?
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700181 if (thread_info.thread_name == "logd" ||
182 android::base::StartsWith(thread_info.thread_name, "logd.")) {
Mark Salyzyn45ae4462014-07-25 12:25:48 -0700183 log->should_retrieve_logcat = false;
184 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800185
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700186 _LOG(log, logtype::HEADER, "pid: %d, tid: %d, name: %s >>> %s <<<\n", thread_info.pid,
187 thread_info.tid, thread_info.thread_name.c_str(), thread_info.process_name.c_str());
Misha Wagner39c5b8c2019-04-18 16:07:33 +0100188 _LOG(log, logtype::HEADER, "uid: %d\n", thread_info.uid);
Christopher Ferris20303f82014-01-10 16:33:16 -0800189}
Jeff Brown053b8652012-06-06 16:25:03 -0700190
Christopher Ferris60eb1972019-01-15 15:18:43 -0800191static void dump_stack_segment(log_t* log, unwindstack::Maps* maps, unwindstack::Memory* memory,
Christopher Ferris7937a362018-01-18 11:15:49 -0800192 uint64_t* sp, size_t words, int label) {
Christopher Ferrisa21bd932015-02-27 13:39:47 -0800193 // Read the data all at once.
194 word_t stack_data[words];
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700195
196 // TODO: Do we need to word align this for crashes caused by a misaligned sp?
197 // The process_vm_readv implementation of Memory should handle this appropriately?
Christopher Ferris60eb1972019-01-15 15:18:43 -0800198 size_t bytes_read = memory->Read(*sp, stack_data, sizeof(word_t) * words);
Christopher Ferrisa21bd932015-02-27 13:39:47 -0800199 words = bytes_read / sizeof(word_t);
200 std::string line;
Christopher Ferris20303f82014-01-10 16:33:16 -0800201 for (size_t i = 0; i < words; i++) {
Christopher Ferrisa21bd932015-02-27 13:39:47 -0800202 line = " ";
203 if (i == 0 && label >= 0) {
204 // Print the label once.
Elliott Hughes0ba53592017-02-01 16:59:15 -0800205 line += StringPrintf("#%02d ", label);
Christopher Ferrisa21bd932015-02-27 13:39:47 -0800206 } else {
207 line += " ";
Christopher Ferris20303f82014-01-10 16:33:16 -0800208 }
Christopher Ferrise36afb02018-01-20 00:59:11 -0800209 line += StringPrintf("%" PRIPTR " %" PRIPTR, *sp, static_cast<uint64_t>(stack_data[i]));
Christopher Ferris20303f82014-01-10 16:33:16 -0800210
Christopher Ferris60eb1972019-01-15 15:18:43 -0800211 unwindstack::MapInfo* map_info = maps->Find(stack_data[i]);
212 if (map_info != nullptr && !map_info->name.empty()) {
213 line += " " + map_info->name;
214 std::string func_name;
215 uint64_t func_offset = 0;
216 if (map_info->GetFunctionName(stack_data[i], &func_name, &func_offset)) {
Christopher Ferrisa21bd932015-02-27 13:39:47 -0800217 line += " (" + func_name;
Christopher Ferris60eb1972019-01-15 15:18:43 -0800218 if (func_offset) {
219 line += StringPrintf("+%" PRIu64, func_offset);
Jeff Brown053b8652012-06-06 16:25:03 -0700220 }
Christopher Ferrisa21bd932015-02-27 13:39:47 -0800221 line += ')';
Christopher Ferris20303f82014-01-10 16:33:16 -0800222 }
Jeff Brown053b8652012-06-06 16:25:03 -0700223 }
Christopher Ferrisa21bd932015-02-27 13:39:47 -0800224 _LOG(log, logtype::STACK, "%s\n", line.c_str());
Christopher Ferris20303f82014-01-10 16:33:16 -0800225
Pavel Chupinc6c194c2013-11-21 23:17:20 +0400226 *sp += sizeof(word_t);
Christopher Ferris20303f82014-01-10 16:33:16 -0800227 }
Jeff Brown053b8652012-06-06 16:25:03 -0700228}
229
Christopher Ferris60eb1972019-01-15 15:18:43 -0800230static void dump_stack(log_t* log, const std::vector<unwindstack::FrameData>& frames,
231 unwindstack::Maps* maps, unwindstack::Memory* memory) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800232 size_t first = 0, last;
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700233 for (size_t i = 0; i < frames.size(); i++) {
Christopher Ferris60eb1972019-01-15 15:18:43 -0800234 if (frames[i].sp) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800235 if (!first) {
236 first = i+1;
237 }
238 last = i;
Jeff Brown053b8652012-06-06 16:25:03 -0700239 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800240 }
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700241
Christopher Ferris20303f82014-01-10 16:33:16 -0800242 if (!first) {
243 return;
244 }
245 first--;
246
Christopher Ferris20303f82014-01-10 16:33:16 -0800247 // Dump a few words before the first frame.
Christopher Ferris7937a362018-01-18 11:15:49 -0800248 uint64_t sp = frames[first].sp - STACK_WORDS * sizeof(word_t);
Christopher Ferris60eb1972019-01-15 15:18:43 -0800249 dump_stack_segment(log, maps, memory, &sp, STACK_WORDS, -1);
250
251#if defined(__LP64__)
252 static constexpr const char delimiter[] = " ................ ................\n";
253#else
254 static constexpr const char delimiter[] = " ........ ........\n";
255#endif
Christopher Ferris20303f82014-01-10 16:33:16 -0800256
257 // Dump a few words from all successive frames.
Christopher Ferris20303f82014-01-10 16:33:16 -0800258 for (size_t i = first; i <= last; i++) {
Christopher Ferris60eb1972019-01-15 15:18:43 -0800259 auto* frame = &frames[i];
Christopher Ferris20303f82014-01-10 16:33:16 -0800260 if (sp != frame->sp) {
Christopher Ferris60eb1972019-01-15 15:18:43 -0800261 _LOG(log, logtype::STACK, delimiter);
Christopher Ferris20303f82014-01-10 16:33:16 -0800262 sp = frame->sp;
263 }
Christopher Ferris60eb1972019-01-15 15:18:43 -0800264 if (i != last) {
265 // Print stack data up to the stack from the next frame.
266 size_t words;
267 uint64_t next_sp = frames[i + 1].sp;
268 if (next_sp < sp) {
269 // The next frame is probably using a completely different stack,
270 // so dump the max from this stack.
Christopher Ferris20303f82014-01-10 16:33:16 -0800271 words = STACK_WORDS;
Christopher Ferris60eb1972019-01-15 15:18:43 -0800272 } else {
273 words = (next_sp - sp) / sizeof(word_t);
274 if (words == 0) {
275 // The sp is the same as the next frame, print at least
276 // one line for this frame.
277 words = 1;
278 } else if (words > STACK_WORDS) {
279 words = STACK_WORDS;
280 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800281 }
Christopher Ferris60eb1972019-01-15 15:18:43 -0800282 dump_stack_segment(log, maps, memory, &sp, words, i);
283 } else {
284 // Print some number of words past the last stack frame since we
285 // don't know how large the stack is.
286 dump_stack_segment(log, maps, memory, &sp, STACK_WORDS, i);
Christopher Ferris20303f82014-01-10 16:33:16 -0800287 }
288 }
Jeff Brown053b8652012-06-06 16:25:03 -0700289}
290
Christopher Ferris7937a362018-01-18 11:15:49 -0800291static std::string get_addr_string(uint64_t addr) {
Christopher Ferris862fe022015-06-02 14:52:44 -0700292 std::string addr_str;
293#if defined(__LP64__)
Elliott Hughes0ba53592017-02-01 16:59:15 -0800294 addr_str = StringPrintf("%08x'%08x",
295 static_cast<uint32_t>(addr >> 32),
296 static_cast<uint32_t>(addr & 0xffffffff));
Christopher Ferris862fe022015-06-02 14:52:44 -0700297#else
Christopher Ferris7937a362018-01-18 11:15:49 -0800298 addr_str = StringPrintf("%08x", static_cast<uint32_t>(addr));
Christopher Ferris862fe022015-06-02 14:52:44 -0700299#endif
300 return addr_str;
301}
302
Christopher Ferris60eb1972019-01-15 15:18:43 -0800303static void dump_abort_message(log_t* log, unwindstack::Memory* process_memory, uint64_t address) {
Josh Gao7c89f9e2016-01-13 17:57:14 -0800304 if (address == 0) {
305 return;
306 }
307
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700308 size_t length;
309 if (!process_memory->ReadFully(address, &length, sizeof(length))) {
310 _LOG(log, logtype::HEADER, "Failed to read abort message header: %s\n", strerror(errno));
311 return;
312 }
Josh Gao7c89f9e2016-01-13 17:57:14 -0800313
Josh Gao1cc7bd82018-02-13 13:16:17 -0800314 // The length field includes the length of the length field itself.
315 if (length < sizeof(size_t)) {
316 _LOG(log, logtype::HEADER, "Abort message header malformed: claimed length = %zd\n", length);
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700317 return;
Josh Gao7c89f9e2016-01-13 17:57:14 -0800318 }
Josh Gao7c89f9e2016-01-13 17:57:14 -0800319
Josh Gao1cc7bd82018-02-13 13:16:17 -0800320 length -= sizeof(size_t);
321
Josh Gao83b8ac22018-04-20 17:31:53 -0700322 // The abort message should be null terminated already, but reserve a spot for NUL just in case.
323 std::vector<char> msg(length + 1);
Josh Gao1cc7bd82018-02-13 13:16:17 -0800324 if (!process_memory->ReadFully(address + sizeof(length), &msg[0], length)) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700325 _LOG(log, logtype::HEADER, "Failed to read abort message: %s\n", strerror(errno));
326 return;
327 }
328
Josh Gao1cc7bd82018-02-13 13:16:17 -0800329 _LOG(log, logtype::HEADER, "Abort message: '%s'\n", &msg[0]);
Josh Gao7c89f9e2016-01-13 17:57:14 -0800330}
331
Christopher Ferris60eb1972019-01-15 15:18:43 -0800332static void dump_all_maps(log_t* log, unwindstack::Unwinder* unwinder, uint64_t addr) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700333 bool print_fault_address_marker = addr;
Christopher Ferris20303f82014-01-10 16:33:16 -0800334
Christopher Ferris60eb1972019-01-15 15:18:43 -0800335 unwindstack::Maps* maps = unwinder->GetMaps();
Elliott Hughes868d39a2017-09-26 11:54:49 -0700336 _LOG(log, logtype::MAPS,
337 "\n"
Josh Gao1ce8e142017-09-27 13:59:42 -0700338 "memory map (%zu entr%s):",
Christopher Ferris60eb1972019-01-15 15:18:43 -0800339 maps->Total(), maps->Total() == 1 ? "y" : "ies");
Elliott Hughes868d39a2017-09-26 11:54:49 -0700340 if (print_fault_address_marker) {
Christopher Ferris60eb1972019-01-15 15:18:43 -0800341 if (maps->Total() != 0 && addr < maps->Get(0)->start) {
Josh Gao1ce8e142017-09-27 13:59:42 -0700342 _LOG(log, logtype::MAPS, "\n--->Fault address falls at %s before any mapped regions\n",
Christopher Ferris862fe022015-06-02 14:52:44 -0700343 get_addr_string(addr).c_str());
Christopher Ferrisa21bd932015-02-27 13:39:47 -0800344 print_fault_address_marker = false;
Elliott Hughes868d39a2017-09-26 11:54:49 -0700345 } else {
Josh Gao1ce8e142017-09-27 13:59:42 -0700346 _LOG(log, logtype::MAPS, " (fault address prefixed with --->)\n");
Brigid Smith8606eaa2014-07-07 12:33:50 -0700347 }
Josh Gao1ce8e142017-09-27 13:59:42 -0700348 } else {
349 _LOG(log, logtype::MAPS, "\n");
Brigid Smith8606eaa2014-07-07 12:33:50 -0700350 }
Christopher Ferrisa21bd932015-02-27 13:39:47 -0800351
Christopher Ferris60eb1972019-01-15 15:18:43 -0800352 std::shared_ptr<unwindstack::Memory>& process_memory = unwinder->GetProcessMemory();
353
Christopher Ferrisa21bd932015-02-27 13:39:47 -0800354 std::string line;
Florian Mayer3d67d342019-02-27 18:00:37 +0000355 for (auto const& map_info : *maps) {
Christopher Ferrisa21bd932015-02-27 13:39:47 -0800356 line = " ";
357 if (print_fault_address_marker) {
Christopher Ferris60eb1972019-01-15 15:18:43 -0800358 if (addr < map_info->start) {
Christopher Ferris862fe022015-06-02 14:52:44 -0700359 _LOG(log, logtype::MAPS, "--->Fault address falls at %s between mapped regions\n",
360 get_addr_string(addr).c_str());
Christopher Ferrisa21bd932015-02-27 13:39:47 -0800361 print_fault_address_marker = false;
Christopher Ferris60eb1972019-01-15 15:18:43 -0800362 } else if (addr >= map_info->start && addr < map_info->end) {
Christopher Ferrisa21bd932015-02-27 13:39:47 -0800363 line = "--->";
364 print_fault_address_marker = false;
365 }
366 }
Christopher Ferris60eb1972019-01-15 15:18:43 -0800367 line += get_addr_string(map_info->start) + '-' + get_addr_string(map_info->end - 1) + ' ';
368 if (map_info->flags & PROT_READ) {
Christopher Ferrisa21bd932015-02-27 13:39:47 -0800369 line += 'r';
370 } else {
371 line += '-';
372 }
Christopher Ferris60eb1972019-01-15 15:18:43 -0800373 if (map_info->flags & PROT_WRITE) {
Christopher Ferrisa21bd932015-02-27 13:39:47 -0800374 line += 'w';
375 } else {
376 line += '-';
377 }
Christopher Ferris60eb1972019-01-15 15:18:43 -0800378 if (map_info->flags & PROT_EXEC) {
Christopher Ferrisa21bd932015-02-27 13:39:47 -0800379 line += 'x';
380 } else {
381 line += '-';
382 }
Christopher Ferris60eb1972019-01-15 15:18:43 -0800383 line += StringPrintf(" %8" PRIx64 " %8" PRIx64, map_info->offset,
384 map_info->end - map_info->start);
Christopher Ferris862fe022015-06-02 14:52:44 -0700385 bool space_needed = true;
Christopher Ferris60eb1972019-01-15 15:18:43 -0800386 if (!map_info->name.empty()) {
Christopher Ferris862fe022015-06-02 14:52:44 -0700387 space_needed = false;
Christopher Ferris60eb1972019-01-15 15:18:43 -0800388 line += " " + map_info->name;
389 std::string build_id = map_info->GetPrintableBuildID();
390 if (!build_id.empty()) {
Christopher Ferrisa21bd932015-02-27 13:39:47 -0800391 line += " (BuildId: " + build_id + ")";
392 }
393 }
Christopher Ferris60eb1972019-01-15 15:18:43 -0800394 uint64_t load_bias = map_info->GetLoadBias(process_memory);
395 if (load_bias != 0) {
Christopher Ferris862fe022015-06-02 14:52:44 -0700396 if (space_needed) {
397 line += ' ';
398 }
Christopher Ferris60eb1972019-01-15 15:18:43 -0800399 line += StringPrintf(" (load bias 0x%" PRIx64 ")", load_bias);
Christopher Ferris2106f4b2015-05-01 15:02:03 -0700400 }
Christopher Ferrisa21bd932015-02-27 13:39:47 -0800401 _LOG(log, logtype::MAPS, "%s\n", line.c_str());
402 }
403 if (print_fault_address_marker) {
Christopher Ferris862fe022015-06-02 14:52:44 -0700404 _LOG(log, logtype::MAPS, "--->Fault address falls at %s after any mapped regions\n",
405 get_addr_string(addr).c_str());
Christopher Ferris20303f82014-01-10 16:33:16 -0800406 }
Jeff Brown053b8652012-06-06 16:25:03 -0700407}
408
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700409static void print_register_row(log_t* log,
410 const std::vector<std::pair<std::string, uint64_t>>& registers) {
411 std::string output;
412 for (auto& [name, value] : registers) {
Christopher Ferris7937a362018-01-18 11:15:49 -0800413 output += android::base::StringPrintf(" %-3s %0*" PRIx64, name.c_str(),
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700414 static_cast<int>(2 * sizeof(void*)),
Christopher Ferris7937a362018-01-18 11:15:49 -0800415 static_cast<uint64_t>(value));
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700416 }
417
418 _LOG(log, logtype::REGISTERS, " %s\n", output.c_str());
Josh Gao77b00ed2017-05-05 18:11:23 -0700419}
420
Christopher Ferris60eb1972019-01-15 15:18:43 -0800421void dump_registers(log_t* log, unwindstack::Regs* regs) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700422 // Split lr/sp/pc into their own special row.
423 static constexpr size_t column_count = 4;
424 std::vector<std::pair<std::string, uint64_t>> current_row;
425 std::vector<std::pair<std::string, uint64_t>> special_row;
426
427#if defined(__arm__) || defined(__aarch64__)
Peter Collingbournebb2f9412019-11-18 12:36:50 -0800428 static constexpr const char* special_registers[] = {"ip", "lr", "sp", "pc", "pst"};
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700429#elif defined(__i386__)
430 static constexpr const char* special_registers[] = {"ebp", "esp", "eip"};
431#elif defined(__x86_64__)
432 static constexpr const char* special_registers[] = {"rbp", "rsp", "rip"};
433#else
434 static constexpr const char* special_registers[] = {};
435#endif
436
437 regs->IterateRegisters([log, &current_row, &special_row](const char* name, uint64_t value) {
438 auto row = &current_row;
439 for (const char* special_name : special_registers) {
440 if (strcmp(special_name, name) == 0) {
441 row = &special_row;
442 break;
443 }
444 }
445
446 row->emplace_back(name, value);
447 if (current_row.size() == column_count) {
448 print_register_row(log, current_row);
449 current_row.clear();
450 }
451 });
452
453 if (!current_row.empty()) {
454 print_register_row(log, current_row);
455 }
456
457 print_register_row(log, special_row);
458}
459
Christopher Ferris60eb1972019-01-15 15:18:43 -0800460void dump_memory_and_code(log_t* log, unwindstack::Maps* maps, unwindstack::Memory* memory,
461 unwindstack::Regs* regs) {
462 regs->IterateRegisters([log, maps, memory](const char* reg_name, uint64_t reg_value) {
Elliott Hughese1415a52018-02-15 09:18:21 -0800463 std::string label{"memory near "s + reg_name};
Christopher Ferris60eb1972019-01-15 15:18:43 -0800464 if (maps) {
465 unwindstack::MapInfo* map_info = maps->Find(reg_value);
466 if (map_info != nullptr && !map_info->name.empty()) {
467 label += " (" + map_info->name + ")";
468 }
Elliott Hughese1415a52018-02-15 09:18:21 -0800469 }
470 dump_memory(log, memory, reg_value, label);
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700471 });
472}
473
Christopher Ferris60eb1972019-01-15 15:18:43 -0800474static bool dump_thread(log_t* log, unwindstack::Unwinder* unwinder, const ThreadInfo& thread_info,
475 uint64_t abort_msg_address, bool primary_thread) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700476 log->current_tid = thread_info.tid;
Josh Gao7c89f9e2016-01-13 17:57:14 -0800477 if (!primary_thread) {
Brigid Smith62ba4892014-06-10 11:53:08 -0700478 _LOG(log, logtype::THREAD, "--- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---\n");
Josh Gao7c89f9e2016-01-13 17:57:14 -0800479 }
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700480 dump_thread_info(log, thread_info);
Christopher Ferris20303f82014-01-10 16:33:16 -0800481
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700482 if (thread_info.siginfo) {
Christopher Ferris60eb1972019-01-15 15:18:43 -0800483 dump_signal_info(log, thread_info, unwinder->GetProcessMemory().get());
Christopher Ferris481e8372019-07-15 17:13:24 -0700484 dump_probable_cause(log, thread_info.siginfo, unwinder->GetMaps(), thread_info.registers.get());
Josh Gao7c89f9e2016-01-13 17:57:14 -0800485 }
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700486
Josh Gao34c25562017-12-22 14:18:39 -0800487 if (primary_thread) {
Christopher Ferris60eb1972019-01-15 15:18:43 -0800488 dump_abort_message(log, unwinder->GetProcessMemory().get(), abort_msg_address);
Josh Gao34c25562017-12-22 14:18:39 -0800489 }
490
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700491 dump_registers(log, thread_info.registers.get());
492
Josh Gao5d1c14f2018-04-20 12:04:41 -0700493 // Unwind will mutate the registers, so make a copy first.
Christopher Ferris60eb1972019-01-15 15:18:43 -0800494 std::unique_ptr<unwindstack::Regs> regs_copy(thread_info.registers->Clone());
495 unwinder->SetRegs(regs_copy.get());
496 unwinder->Unwind();
497 if (unwinder->NumFrames() == 0) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700498 _LOG(log, logtype::THREAD, "Failed to unwind");
Christopher Ferris60eb1972019-01-15 15:18:43 -0800499 } else {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700500 _LOG(log, logtype::BACKTRACE, "\nbacktrace:\n");
Christopher Ferris4ae266c2019-04-03 09:27:12 -0700501 log_backtrace(log, unwinder, " ");
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700502
503 _LOG(log, logtype::STACK, "\nstack:\n");
Christopher Ferris60eb1972019-01-15 15:18:43 -0800504 dump_stack(log, unwinder->frames(), unwinder->GetMaps(), unwinder->GetProcessMemory().get());
Josh Gao7c89f9e2016-01-13 17:57:14 -0800505 }
Brigid Smith62ba4892014-06-10 11:53:08 -0700506
Josh Gao7c89f9e2016-01-13 17:57:14 -0800507 if (primary_thread) {
Christopher Ferris60eb1972019-01-15 15:18:43 -0800508 unwindstack::Maps* maps = unwinder->GetMaps();
509 dump_memory_and_code(log, maps, unwinder->GetProcessMemory().get(),
510 thread_info.registers.get());
511 if (maps != nullptr) {
Christopher Ferris7937a362018-01-18 11:15:49 -0800512 uint64_t addr = 0;
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700513 siginfo_t* si = thread_info.siginfo;
Elliott Hughes70d8f282018-04-25 17:00:14 -0700514 if (signal_has_si_addr(si)) {
Christopher Ferris7937a362018-01-18 11:15:49 -0800515 addr = reinterpret_cast<uint64_t>(si->si_addr);
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700516 }
Christopher Ferris60eb1972019-01-15 15:18:43 -0800517 dump_all_maps(log, unwinder, addr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800518 }
519 }
520
Josh Gao7c89f9e2016-01-13 17:57:14 -0800521 log->current_tid = log->crashed_tid;
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700522 return true;
Jeff Brown053b8652012-06-06 16:25:03 -0700523}
524
Christopher Ferris20303f82014-01-10 16:33:16 -0800525// Reads the contents of the specified log device, filters out the entries
526// that don't match the specified pid, and writes them to the tombstone file.
527//
Mark Salyzyn17e85c02014-06-27 15:55:19 -0700528// If "tail" is non-zero, log the last "tail" number of lines.
Mark Salyzyn989980c2014-05-14 12:37:22 -0700529static EventTagMap* g_eventTagMap = NULL;
530
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700531static void dump_log_file(log_t* log, pid_t pid, const char* filename, unsigned int tail) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800532 bool first = true;
Christopher Ferrisc637ada2018-07-13 16:55:38 -0700533 logger_list* logger_list;
Jeff Brown053b8652012-06-06 16:25:03 -0700534
Mark Salyzyn45ae4462014-07-25 12:25:48 -0700535 if (!log->should_retrieve_logcat) {
536 return;
537 }
538
Mark Salyzyn22b5cef2013-11-22 10:53:34 -0800539 logger_list = android_logger_list_open(
Mark Salyzyn2d3f38a2015-01-26 10:46:44 -0800540 android_name_to_log_id(filename), ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK, tail, pid);
Jeff Brown053b8652012-06-06 16:25:03 -0700541
Mark Salyzyn22b5cef2013-11-22 10:53:34 -0800542 if (!logger_list) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700543 ALOGE("Unable to open %s: %s\n", filename, strerror(errno));
Christopher Ferris20303f82014-01-10 16:33:16 -0800544 return;
545 }
546
Christopher Ferris20303f82014-01-10 16:33:16 -0800547 while (true) {
Christopher Ferrisc637ada2018-07-13 16:55:38 -0700548 log_msg log_entry;
Mark Salyzyn22b5cef2013-11-22 10:53:34 -0800549 ssize_t actual = android_logger_list_read(logger_list, &log_entry);
550
Christopher Ferris20303f82014-01-10 16:33:16 -0800551 if (actual < 0) {
Mark Salyzyn22b5cef2013-11-22 10:53:34 -0800552 if (actual == -EINTR) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800553 // interrupted by signal, retry
554 continue;
Mark Salyzyn22b5cef2013-11-22 10:53:34 -0800555 } else if (actual == -EAGAIN) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800556 // non-blocking EOF; we're done
557 break;
558 } else {
Christopher Ferrisb36b5922015-06-17 18:35:59 -0700559 ALOGE("Error while reading log: %s\n", strerror(-actual));
Christopher Ferris20303f82014-01-10 16:33:16 -0800560 break;
561 }
562 } else if (actual == 0) {
Christopher Ferrisb36b5922015-06-17 18:35:59 -0700563 ALOGE("Got zero bytes while reading log: %s\n", strerror(errno));
Christopher Ferris20303f82014-01-10 16:33:16 -0800564 break;
Jeff Brown053b8652012-06-06 16:25:03 -0700565 }
566
Brigid Smith50eb5462014-06-18 14:17:57 -0700567 // NOTE: if you ALOGV something here, this will spin forever,
Christopher Ferris20303f82014-01-10 16:33:16 -0800568 // because you will be writing as fast as you're reading. Any
569 // high-frequency debug diagnostics should just be written to
570 // the tombstone file.
Jeff Brown053b8652012-06-06 16:25:03 -0700571
Christopher Ferris20303f82014-01-10 16:33:16 -0800572 if (first) {
Mark Salyzyne43290d2014-06-27 10:32:22 -0700573 _LOG(log, logtype::LOGS, "--------- %slog %s\n",
Mark Salyzyn22b5cef2013-11-22 10:53:34 -0800574 tail ? "tail end of " : "", filename);
Christopher Ferris20303f82014-01-10 16:33:16 -0800575 first = false;
576 }
577
578 // Msg format is: <priority:1><tag:N>\0<message:N>\0
579 //
580 // We want to display it in the same format as "logcat -v threadtime"
581 // (although in this case the pid is redundant).
Mark Salyzyn989980c2014-05-14 12:37:22 -0700582 char timeBuf[32];
Christopher Ferrisc637ada2018-07-13 16:55:38 -0700583 time_t sec = static_cast<time_t>(log_entry.entry.sec);
Mark Salyzyn989980c2014-05-14 12:37:22 -0700584 struct tm tmBuf;
585 struct tm* ptm;
586 ptm = localtime_r(&sec, &tmBuf);
587 strftime(timeBuf, sizeof(timeBuf), "%m-%d %H:%M:%S", ptm);
588
589 if (log_entry.id() == LOG_ID_EVENTS) {
590 if (!g_eventTagMap) {
Christopher Ferrisc637ada2018-07-13 16:55:38 -0700591 g_eventTagMap = android_openEventTagMap(nullptr);
Mark Salyzyn989980c2014-05-14 12:37:22 -0700592 }
593 AndroidLogEntry e;
594 char buf[512];
Tom Cherry441054a2019-10-15 16:53:11 -0700595 if (android_log_processBinaryLogBuffer(&log_entry.entry, &e, g_eventTagMap, buf,
Christopher Ferrisc637ada2018-07-13 16:55:38 -0700596 sizeof(buf)) == 0) {
597 _LOG(log, logtype::LOGS, "%s.%03d %5d %5d %c %-8.*s: %s\n", timeBuf,
598 log_entry.entry.nsec / 1000000, log_entry.entry.pid, log_entry.entry.tid, 'I',
599 (int)e.tagLen, e.tag, e.message);
600 }
Mark Salyzyn989980c2014-05-14 12:37:22 -0700601 continue;
602 }
603
Christopher Ferrisc637ada2018-07-13 16:55:38 -0700604 char* msg = log_entry.msg();
605 if (msg == nullptr) {
606 continue;
607 }
Mark Salyzyn22b5cef2013-11-22 10:53:34 -0800608 unsigned char prio = msg[0];
609 char* tag = msg + 1;
610 msg = tag + strlen(tag) + 1;
Christopher Ferris20303f82014-01-10 16:33:16 -0800611
612 // consume any trailing newlines
Mark Salyzynfca0bd12013-12-12 12:21:20 -0800613 char* nl = msg + strlen(msg) - 1;
614 while (nl >= msg && *nl == '\n') {
Mark Salyzyn17e85c02014-06-27 15:55:19 -0700615 *nl-- = '\0';
Christopher Ferris20303f82014-01-10 16:33:16 -0800616 }
617
Christopher Ferrisc637ada2018-07-13 16:55:38 -0700618 static const char* kPrioChars = "!.VDIWEFS";
Christopher Ferris20303f82014-01-10 16:33:16 -0800619 char prioChar = (prio < strlen(kPrioChars) ? kPrioChars[prio] : '?');
620
Mark Salyzynfca0bd12013-12-12 12:21:20 -0800621 // Look for line breaks ('\n') and display each text line
622 // on a separate line, prefixed with the header, like logcat does.
623 do {
624 nl = strchr(msg, '\n');
Christopher Ferrisc637ada2018-07-13 16:55:38 -0700625 if (nl != nullptr) {
Mark Salyzynfca0bd12013-12-12 12:21:20 -0800626 *nl = '\0';
627 ++nl;
628 }
629
Christopher Ferrisc637ada2018-07-13 16:55:38 -0700630 _LOG(log, logtype::LOGS, "%s.%03d %5d %5d %c %-8s: %s\n", timeBuf,
631 log_entry.entry.nsec / 1000000, log_entry.entry.pid, log_entry.entry.tid, prioChar, tag,
632 msg);
Mark Salyzynfca0bd12013-12-12 12:21:20 -0800633 } while ((msg = nl));
Christopher Ferris20303f82014-01-10 16:33:16 -0800634 }
Jeff Brown053b8652012-06-06 16:25:03 -0700635
Mark Salyzyn22b5cef2013-11-22 10:53:34 -0800636 android_logger_list_free(logger_list);
Jeff Brown053b8652012-06-06 16:25:03 -0700637}
638
Christopher Ferris20303f82014-01-10 16:33:16 -0800639// Dumps the logs generated by the specified pid to the tombstone, from both
640// "system" and "main" log devices. Ideally we'd interleave the output.
Mark Salyzyn17e85c02014-06-27 15:55:19 -0700641static void dump_logs(log_t* log, pid_t pid, unsigned int tail) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700642 if (pid == getpid()) {
643 // Cowardly refuse to dump logs while we're running in-process.
644 return;
645 }
646
Mark Salyzyn22b5cef2013-11-22 10:53:34 -0800647 dump_log_file(log, pid, "system", tail);
648 dump_log_file(log, pid, "main", tail);
Jeff Brown053b8652012-06-06 16:25:03 -0700649}
650
Christopher Ferris7937a362018-01-18 11:15:49 -0800651void engrave_tombstone_ucontext(int tombstone_fd, uint64_t abort_msg_address, siginfo_t* siginfo,
Josh Gaoe1aa0ca2017-03-01 17:23:22 -0800652 ucontext_t* ucontext) {
Misha Wagner39c5b8c2019-04-18 16:07:33 +0100653 pid_t uid = getuid();
Josh Gaoe1aa0ca2017-03-01 17:23:22 -0800654 pid_t pid = getpid();
655 pid_t tid = gettid();
656
Josh Gaoe73c9322017-02-08 16:06:26 -0800657 log_t log;
658 log.current_tid = tid;
659 log.crashed_tid = tid;
660 log.tfd = tombstone_fd;
661 log.amfd_data = nullptr;
662
Josh Gao57f58f82017-03-15 23:23:22 -0700663 char thread_name[16];
664 char process_name[128];
665
666 read_with_default("/proc/self/comm", thread_name, sizeof(thread_name), "<unknown>");
667 read_with_default("/proc/self/cmdline", process_name, sizeof(process_name), "<unknown>");
668
Christopher Ferris60eb1972019-01-15 15:18:43 -0800669 std::unique_ptr<unwindstack::Regs> regs(
670 unwindstack::Regs::CreateFromUcontext(unwindstack::Regs::CurrentArch(), ucontext));
Josh Gaoe73c9322017-02-08 16:06:26 -0800671
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700672 std::map<pid_t, ThreadInfo> threads;
673 threads[gettid()] = ThreadInfo{
674 .registers = std::move(regs),
Misha Wagner39c5b8c2019-04-18 16:07:33 +0100675 .uid = uid,
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700676 .tid = tid,
677 .thread_name = thread_name,
678 .pid = pid,
679 .process_name = process_name,
680 .siginfo = siginfo,
681 };
Josh Gao77b00ed2017-05-05 18:11:23 -0700682
Christopher Ferris60eb1972019-01-15 15:18:43 -0800683 unwindstack::UnwinderFromPid unwinder(kMaxFrames, pid);
David Srbeckyb9cc4fb2019-04-05 18:23:32 +0000684 if (!unwinder.Init(unwindstack::Regs::CurrentArch())) {
Christopher Ferris60eb1972019-01-15 15:18:43 -0800685 LOG(FATAL) << "Failed to init unwinder object.";
Josh Gaoe73c9322017-02-08 16:06:26 -0800686 }
Josh Gaofdc95c92017-09-13 15:33:39 -0700687
Christopher Ferris60eb1972019-01-15 15:18:43 -0800688 engrave_tombstone(unique_fd(dup(tombstone_fd)), &unwinder, threads, tid, abort_msg_address,
689 nullptr, nullptr);
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700690}
691
Christopher Ferris60eb1972019-01-15 15:18:43 -0800692void engrave_tombstone(unique_fd output_fd, unwindstack::Unwinder* unwinder,
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700693 const std::map<pid_t, ThreadInfo>& threads, pid_t target_thread,
Christopher Ferris7937a362018-01-18 11:15:49 -0800694 uint64_t abort_msg_address, OpenFilesList* open_files,
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700695 std::string* amfd_data) {
696 // don't copy log messages to tombstone unless this is a dev device
697 bool want_logs = android::base::GetBoolProperty("ro.debuggable", false);
698
699 log_t log;
700 log.current_tid = target_thread;
701 log.crashed_tid = target_thread;
702 log.tfd = output_fd.get();
703 log.amfd_data = amfd_data;
704
705 _LOG(&log, logtype::HEADER, "*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***\n");
706 dump_header_info(&log);
Josh Gao6f4644d2018-12-14 13:05:12 -0800707 dump_timestamp(&log, time(nullptr));
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700708
709 auto it = threads.find(target_thread);
710 if (it == threads.end()) {
711 LOG(FATAL) << "failed to find target thread";
712 }
Christopher Ferris60eb1972019-01-15 15:18:43 -0800713 dump_thread(&log, unwinder, it->second, abort_msg_address, true);
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700714
715 if (want_logs) {
chirag honnavardef08882017-04-20 18:22:06 +0900716 dump_logs(&log, it->second.pid, 50);
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700717 }
718
719 for (auto& [tid, thread_info] : threads) {
720 if (tid == target_thread) {
721 continue;
Josh Gaofdc95c92017-09-13 15:33:39 -0700722 }
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700723
Christopher Ferris60eb1972019-01-15 15:18:43 -0800724 dump_thread(&log, unwinder, thread_info, 0, false);
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700725 }
726
727 if (open_files) {
728 _LOG(&log, logtype::OPEN_FILES, "\nopen files:\n");
729 dump_open_files_list(&log, *open_files, " ");
730 }
731
732 if (want_logs) {
733 dump_logs(&log, it->second.pid, 0);
Josh Gaofdc95c92017-09-13 15:33:39 -0700734 }
Josh Gaoe73c9322017-02-08 16:06:26 -0800735}