blob: d3ac49a17f8d836726dbece0a1fb607f003a97fb [file] [log] [blame]
Josh Gao76e1e302021-01-26 15:53:11 -08001/*
2 * Copyright (C) 2020 The Android Open Source Project
3 *
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
17#define LOG_TAG "DEBUG"
18
19#include "libdebuggerd/tombstone.h"
Peter Collingbourne1a1f7d72021-03-08 16:53:54 -080020#include "libdebuggerd/gwp_asan.h"
Christopher Ferris7aad2562021-09-24 00:06:38 -070021#if defined(USE_SCUDO)
Peter Collingbourne1a1f7d72021-03-08 16:53:54 -080022#include "libdebuggerd/scudo.h"
Christopher Ferris7aad2562021-09-24 00:06:38 -070023#endif
Josh Gao76e1e302021-01-26 15:53:11 -080024
25#include <errno.h>
26#include <fcntl.h>
27#include <inttypes.h>
28#include <signal.h>
29#include <stddef.h>
Florian Mayere3e7bc72024-04-26 09:38:50 -070030#include <stdint.h>
Josh Gao76e1e302021-01-26 15:53:11 -080031#include <stdlib.h>
32#include <string.h>
33#include <sys/mman.h>
Elliott Hughes32d3cdd2021-09-21 12:20:53 -070034#include <sys/sysinfo.h>
Josh Gao76e1e302021-01-26 15:53:11 -080035#include <time.h>
36
Christopher Ferris6a5db682024-11-15 01:06:55 +000037#include <map>
Josh Gao76e1e302021-01-26 15:53:11 -080038#include <memory>
Josh Gaodbb83de2021-03-01 23:13:13 -080039#include <optional>
Christopher Ferrisc95047d2022-03-14 15:02:11 -070040#include <set>
Josh Gao76e1e302021-01-26 15:53:11 -080041#include <string>
Christopher Ferris6a5db682024-11-15 01:06:55 +000042#include <utility>
43#include <vector>
Josh Gao76e1e302021-01-26 15:53:11 -080044
Josh Gao618cea32021-01-26 17:45:43 -080045#include <async_safe/log.h>
46
Josh Gaodbb83de2021-03-01 23:13:13 -080047#include <android-base/file.h>
Christopher Ferris2b98c822021-09-28 15:50:04 -070048#include <android-base/logging.h>
Josh Gao76e1e302021-01-26 15:53:11 -080049#include <android-base/properties.h>
50#include <android-base/stringprintf.h>
51#include <android-base/strings.h>
52#include <android-base/unique_fd.h>
53
54#include <android/log.h>
Florian Mayer5fa66632024-02-07 16:42:23 -080055#include <android/set_abort_message.h>
Florian Mayer920d95b2024-02-14 12:57:09 -080056#include <bionic/crash_detail_internal.h>
Florian Mayere3e7bc72024-04-26 09:38:50 -070057#include <bionic/macros.h>
58#include <bionic/mte.h>
59#include <bionic/reserved_signals.h>
60#include <bionic/tls_defines.h>
Josh Gao76e1e302021-01-26 15:53:11 -080061#include <log/log.h>
62#include <log/log_read.h>
63#include <log/logprint.h>
64#include <private/android_filesystem_config.h>
65
Josh Gaodbb83de2021-03-01 23:13:13 -080066#include <procinfo/process.h>
Christopher Ferris3b7b7ba2022-03-15 16:56:09 -070067#include <unwindstack/AndroidUnwinder.h>
68#include <unwindstack/Error.h>
69#include <unwindstack/MapInfo.h>
Josh Gao76e1e302021-01-26 15:53:11 -080070#include <unwindstack/Maps.h>
Josh Gao76e1e302021-01-26 15:53:11 -080071#include <unwindstack/Regs.h>
Josh Gao76e1e302021-01-26 15:53:11 -080072
73#include "libdebuggerd/open_files_list.h"
74#include "libdebuggerd/utility.h"
Peter Collingbourne39a17302024-03-07 17:50:10 -080075#include "libdebuggerd/utility_host.h"
Josh Gao76e1e302021-01-26 15:53:11 -080076#include "util.h"
77
78#include "tombstone.pb.h"
79
80using android::base::StringPrintf;
81
Christopher Ferris98d62422023-05-24 20:01:10 +000082// The maximum number of messages to save in the protobuf per file.
83static constexpr size_t kMaxLogMessages = 500;
84
Josh Gao76e1e302021-01-26 15:53:11 -080085// Use the demangler from libc++.
86extern "C" char* __cxa_demangle(const char*, char*, size_t*, int* status);
87
88static Architecture get_arch() {
89#if defined(__arm__)
90 return Architecture::ARM32;
91#elif defined(__aarch64__)
92 return Architecture::ARM64;
93#elif defined(__i386__)
94 return Architecture::X86;
95#elif defined(__x86_64__)
96 return Architecture::X86_64;
Liu Cunyuan8c0101b2022-10-12 22:35:51 +080097#elif defined(__riscv) && (__riscv_xlen == 64)
98 return Architecture::RISCV64;
Josh Gao76e1e302021-01-26 15:53:11 -080099#else
100#error Unknown architecture!
101#endif
102}
103
104static std::optional<std::string> get_stack_overflow_cause(uint64_t fault_addr, uint64_t sp,
105 unwindstack::Maps* maps) {
Mitch Phillipsbf2d6dd2024-02-06 15:18:04 +0100106 // Under stack MTE the stack pointer and/or the fault address can be tagged.
107 // In order to calculate deltas between them, strip off the tags off both
108 // addresses.
109 fault_addr = untag_address(fault_addr);
110 sp = untag_address(sp);
Josh Gao76e1e302021-01-26 15:53:11 -0800111 static constexpr uint64_t kMaxDifferenceBytes = 256;
112 uint64_t difference;
113 if (sp >= fault_addr) {
114 difference = sp - fault_addr;
115 } else {
116 difference = fault_addr - sp;
117 }
118 if (difference <= kMaxDifferenceBytes) {
119 // The faulting address is close to the current sp, check if the sp
120 // indicates a stack overflow.
121 // On arm, the sp does not get updated when the instruction faults.
122 // In this case, the sp will still be in a valid map, which is the
123 // last case below.
124 // On aarch64, the sp does get updated when the instruction faults.
125 // In this case, the sp will be in either an invalid map if triggered
126 // on the main thread, or in a guard map if in another thread, which
127 // will be the first case or second case from below.
Christopher Ferrisc6776062021-01-15 15:59:28 -0800128 std::shared_ptr<unwindstack::MapInfo> map_info = maps->Find(sp);
Josh Gao76e1e302021-01-26 15:53:11 -0800129 if (map_info == nullptr) {
130 return "stack pointer is in a non-existent map; likely due to stack overflow.";
David Srbeckyd8ab48b2021-05-13 00:03:26 +0100131 } else if ((map_info->flags() & (PROT_READ | PROT_WRITE)) != (PROT_READ | PROT_WRITE)) {
Josh Gao76e1e302021-01-26 15:53:11 -0800132 return "stack pointer is not in a rw map; likely due to stack overflow.";
David Srbeckyd8ab48b2021-05-13 00:03:26 +0100133 } else if ((sp - map_info->start()) <= kMaxDifferenceBytes) {
Josh Gao76e1e302021-01-26 15:53:11 -0800134 return "stack pointer is close to top of stack; likely stack overflow.";
135 }
136 }
137 return {};
138}
139
Peter Collingbourne1a1f7d72021-03-08 16:53:54 -0800140void set_human_readable_cause(Cause* cause, uint64_t fault_addr) {
141 if (!cause->has_memory_error() || !cause->memory_error().has_heap()) {
142 return;
143 }
144
145 const MemoryError& memory_error = cause->memory_error();
146 const HeapObject& heap_object = memory_error.heap();
147
148 const char *tool_str;
149 switch (memory_error.tool()) {
150 case MemoryError_Tool_GWP_ASAN:
151 tool_str = "GWP-ASan";
152 break;
153 case MemoryError_Tool_SCUDO:
154 tool_str = "MTE";
155 break;
156 default:
157 tool_str = "Unknown";
158 break;
159 }
160
161 const char *error_type_str;
162 switch (memory_error.type()) {
163 case MemoryError_Type_USE_AFTER_FREE:
164 error_type_str = "Use After Free";
165 break;
166 case MemoryError_Type_DOUBLE_FREE:
167 error_type_str = "Double Free";
168 break;
169 case MemoryError_Type_INVALID_FREE:
170 error_type_str = "Invalid (Wild) Free";
171 break;
172 case MemoryError_Type_BUFFER_OVERFLOW:
173 error_type_str = "Buffer Overflow";
174 break;
175 case MemoryError_Type_BUFFER_UNDERFLOW:
176 error_type_str = "Buffer Underflow";
177 break;
178 default:
179 cause->set_human_readable(
180 StringPrintf("[%s]: Unknown error occurred at 0x%" PRIx64 ".", tool_str, fault_addr));
181 return;
182 }
183
184 uint64_t diff;
185 const char* location_str;
186
187 if (fault_addr < heap_object.address()) {
188 // Buffer Underflow, 6 bytes left of a 41-byte allocation at 0xdeadbeef.
189 location_str = "left of";
190 diff = heap_object.address() - fault_addr;
191 } else if (fault_addr - heap_object.address() < heap_object.size()) {
192 // Use After Free, 40 bytes into a 41-byte allocation at 0xdeadbeef.
193 location_str = "into";
194 diff = fault_addr - heap_object.address();
195 } else {
196 // Buffer Overflow, 6 bytes right of a 41-byte allocation at 0xdeadbeef.
197 location_str = "right of";
198 diff = fault_addr - heap_object.address() - heap_object.size();
199 }
200
201 // Suffix of 'bytes', i.e. 4 bytes' vs. '1 byte'.
202 const char* byte_suffix = "s";
203 if (diff == 1) {
204 byte_suffix = "";
205 }
206
207 cause->set_human_readable(StringPrintf(
208 "[%s]: %s, %" PRIu64 " byte%s %s a %" PRIu64 "-byte allocation at 0x%" PRIx64, tool_str,
209 error_type_str, diff, byte_suffix, location_str, heap_object.size(), heap_object.address()));
210}
211
Florian Mayere3e7bc72024-04-26 09:38:50 -0700212#if defined(__aarch64__)
213void dump_stack_history(unwindstack::AndroidUnwinder* unwinder, uintptr_t target_tls,
214 StackHistoryBuffer& shb_obj, bool nounwind) {
215 auto process_memory = unwinder->GetProcessMemory();
216 target_tls += sizeof(void*) * TLS_SLOT_STACK_MTE;
217 uintptr_t stack_mte;
218 if (!process_memory->ReadFully(target_tls, &stack_mte, sizeof(stack_mte))) {
219 async_safe_format_log(ANDROID_LOG_ERROR, LOG_TAG,
220 "dump_stack_history: failed to read TLS_SLOT_STACK_MTE: %m");
221 return;
222 }
223 if (stack_mte == 0) {
224 async_safe_format_log(ANDROID_LOG_DEBUG, LOG_TAG,
225 "dump_stack_history: stack history buffer is null");
226 return;
227 }
228 uintptr_t untagged_stack_mte = untag_address(stack_mte);
229 uintptr_t buf_size = stack_mte_ringbuffer_size_from_pointer(stack_mte);
230 if (buf_size == 0) {
231 async_safe_format_log(ANDROID_LOG_ERROR, LOG_TAG, "dump_stack_history: empty size");
232 return;
233 }
234 uintptr_t buf_start = untagged_stack_mte & ~(buf_size - 1ULL);
235 std::vector<char> buf(buf_size);
236 if (!process_memory->ReadFully(buf_start, buf.data(), buf.size())) {
237 async_safe_format_log(ANDROID_LOG_ERROR, LOG_TAG,
238 "dump_stack_history: failed to read stack history: %m");
239 return;
240 }
241 uintptr_t original_off = untagged_stack_mte - buf_start;
242 if (original_off % 16 || original_off > buf_size) {
243 async_safe_format_log(ANDROID_LOG_ERROR, LOG_TAG,
244 "dump_stack_history: invalid offset: %" PRIuPTR, original_off);
245 return;
246 }
247
248 // The original_off is the next slot that would have been written, so the last
249 // slot that was written is the previous one.
250 for (uintptr_t idx = 16; idx <= buf_size; idx += 16) {
251 int64_t off = original_off - idx;
252 if (off < 0) off += buf_size;
253 uintptr_t pc, taggedfp;
254 memcpy(&pc, &(buf[off]), sizeof(pc));
255 memcpy(&taggedfp, &(buf[off + sizeof(pc)]), sizeof(taggedfp));
256
257 if (pc == 0) break;
258 uintptr_t fp = untag_address(taggedfp);
259 uintptr_t tag = taggedfp >> 56;
260
261 unwindstack::FrameData frame_data;
262
263 if (nounwind) {
264 frame_data.pc = pc;
265 } else {
266 // +4 is to counteract the "pc adjustment" in BuildFrameFromPcOnly.
267 // BuildFrameFromPcOnly assumes we are unwinding, so it needs to correct for that
268 // the PC is the return address. That is not the case here.
269 // It doesn't really matter, because either should be in the correct function, but
270 // this is more correct (and consistent with the nounwind case).
271 frame_data = unwinder->BuildFrameFromPcOnly(pc);
272 frame_data.pc += 4;
273 frame_data.rel_pc += 4;
274 }
275
276 StackHistoryBufferEntry* entry = shb_obj.add_entries();
277 fill_in_backtrace_frame(entry->mutable_addr(), frame_data);
278 entry->set_fp(fp);
279 entry->set_tag(tag);
280 }
281}
282
283static pid_t get_containing_thread(unwindstack::MapInfo* map_info, pid_t main_tid) {
284 if (map_info == nullptr) return 0;
285
286 std::string name = map_info->name();
287 if (name == "[stack]") {
288 return main_tid;
289 }
290 int tid;
291 if (sscanf(name.c_str(), "[anon:stack_and_tls:%d", &tid) != 1) {
292 return 0;
293 }
294 return tid;
295}
296
297static std::optional<std::string> maybe_stack_mte_cause(
298 Tombstone* tombstone, unwindstack::AndroidUnwinder* unwinder, const ThreadInfo& target_thread,
299 [[maybe_unused]] const std::map<pid_t, ThreadInfo>& threads, uint64_t fault_addr) {
300 unwindstack::Maps* maps = unwinder->GetMaps();
301 auto map_info = maps->Find(untag_address(fault_addr));
302 pid_t tid = get_containing_thread(map_info.get(), target_thread.tid);
303 if (!tid) {
304 return std::nullopt;
305 }
306 auto it = threads.find(tid);
307 if (it != threads.end()) {
308 StackHistoryBuffer* shb = tombstone->mutable_stack_history_buffer();
309 shb->set_tid(tid);
310 dump_stack_history(unwinder, it->second.tls, *shb);
311 } else {
312 async_safe_format_log(ANDROID_LOG_ERROR, LOG_TAG,
313 "dump_probable_cause: unknown target thread %d", tid);
314 }
315 return StringPrintf("stack tag-mismatch on thread %u", tid);
316}
317
318#endif
319
Christopher Ferris3b7b7ba2022-03-15 16:56:09 -0700320static void dump_probable_cause(Tombstone* tombstone, unwindstack::AndroidUnwinder* unwinder,
Florian Mayere3e7bc72024-04-26 09:38:50 -0700321 const ProcessInfo& process_info, const ThreadInfo& target_thread,
322 [[maybe_unused]] const std::map<pid_t, ThreadInfo>& threads) {
Christopher Ferris7aad2562021-09-24 00:06:38 -0700323#if defined(USE_SCUDO)
Peter Collingbourne78279912022-06-30 18:39:54 -0700324 ScudoCrashData scudo_crash_data(unwinder->GetProcessMemory().get(), process_info);
325 if (scudo_crash_data.CrashIsMine()) {
Peter Collingbourne1a1f7d72021-03-08 16:53:54 -0800326 scudo_crash_data.AddCauseProtos(tombstone, unwinder);
327 return;
328 }
Christopher Ferris7aad2562021-09-24 00:06:38 -0700329#endif
Peter Collingbourne1a1f7d72021-03-08 16:53:54 -0800330
331 GwpAsanCrashData gwp_asan_crash_data(unwinder->GetProcessMemory().get(), process_info,
Florian Mayer2d453312024-04-22 23:43:59 +0000332 target_thread);
Peter Collingbourne1a1f7d72021-03-08 16:53:54 -0800333 if (gwp_asan_crash_data.CrashIsMine()) {
334 gwp_asan_crash_data.AddCauseProtos(tombstone, unwinder);
335 return;
336 }
337
Florian Mayer2d453312024-04-22 23:43:59 +0000338 const siginfo *si = target_thread.siginfo;
Peter Collingbourne1a1f7d72021-03-08 16:53:54 -0800339 auto fault_addr = reinterpret_cast<uint64_t>(si->si_addr);
340 unwindstack::Maps* maps = unwinder->GetMaps();
341
Josh Gao76e1e302021-01-26 15:53:11 -0800342 std::optional<std::string> cause;
343 if (si->si_signo == SIGSEGV && si->si_code == SEGV_MAPERR) {
Peter Collingbourne1a1f7d72021-03-08 16:53:54 -0800344 if (fault_addr < 4096) {
Josh Gao76e1e302021-01-26 15:53:11 -0800345 cause = "null pointer dereference";
Peter Collingbourne1a1f7d72021-03-08 16:53:54 -0800346 } else if (fault_addr == 0xffff0ffc) {
Josh Gao76e1e302021-01-26 15:53:11 -0800347 cause = "call to kuser_helper_version";
Peter Collingbourne1a1f7d72021-03-08 16:53:54 -0800348 } else if (fault_addr == 0xffff0fe0) {
Josh Gao76e1e302021-01-26 15:53:11 -0800349 cause = "call to kuser_get_tls";
Peter Collingbourne1a1f7d72021-03-08 16:53:54 -0800350 } else if (fault_addr == 0xffff0fc0) {
Josh Gao76e1e302021-01-26 15:53:11 -0800351 cause = "call to kuser_cmpxchg";
Peter Collingbourne1a1f7d72021-03-08 16:53:54 -0800352 } else if (fault_addr == 0xffff0fa0) {
Josh Gao76e1e302021-01-26 15:53:11 -0800353 cause = "call to kuser_memory_barrier";
Peter Collingbourne1a1f7d72021-03-08 16:53:54 -0800354 } else if (fault_addr == 0xffff0f60) {
Josh Gao76e1e302021-01-26 15:53:11 -0800355 cause = "call to kuser_cmpxchg64";
356 } else {
Florian Mayer2d453312024-04-22 23:43:59 +0000357 cause = get_stack_overflow_cause(fault_addr, target_thread.registers->sp(), maps);
Josh Gao76e1e302021-01-26 15:53:11 -0800358 }
359 } else if (si->si_signo == SIGSEGV && si->si_code == SEGV_ACCERR) {
Christopher Ferrisc6776062021-01-15 15:59:28 -0800360 auto map_info = maps->Find(fault_addr);
David Srbeckyd8ab48b2021-05-13 00:03:26 +0100361 if (map_info != nullptr && map_info->flags() == PROT_EXEC) {
Josh Gao76e1e302021-01-26 15:53:11 -0800362 cause = "execute-only (no-read) memory access error; likely due to data in .text.";
Elliott Hughesb17d7be2024-09-11 16:23:18 +0000363 } else if (fault_addr == target_thread.registers->pc() &&
364 map_info != nullptr && (map_info->flags() & PROT_EXEC) == 0) {
365 cause = "trying to execute non-executable memory.";
Josh Gao76e1e302021-01-26 15:53:11 -0800366 } else {
Florian Mayer2d453312024-04-22 23:43:59 +0000367 cause = get_stack_overflow_cause(fault_addr, target_thread.registers->sp(), maps);
Josh Gao76e1e302021-01-26 15:53:11 -0800368 }
Florian Mayere3e7bc72024-04-26 09:38:50 -0700369 }
370#if defined(__aarch64__) && defined(SEGV_MTESERR)
371 else if (si->si_signo == SIGSEGV && si->si_code == SEGV_MTESERR) {
372 // If this was a heap MTE crash, it would have been handled by scudo. Checking whether it
373 // is a stack one.
374 cause = maybe_stack_mte_cause(tombstone, unwinder, target_thread, threads, fault_addr);
375 }
376#endif
377 else if (si->si_signo == SIGSYS && si->si_code == SYS_SECCOMP) {
Josh Gao76e1e302021-01-26 15:53:11 -0800378 cause = StringPrintf("seccomp prevented call to disallowed %s system call %d", ABI_STRING,
379 si->si_syscall);
380 }
381
382 if (cause) {
Peter Collingbourne1a1f7d72021-03-08 16:53:54 -0800383 Cause *cause_proto = tombstone->add_causes();
384 cause_proto->set_human_readable(*cause);
Josh Gao76e1e302021-01-26 15:53:11 -0800385 }
386}
387
Florian Mayer5fa66632024-02-07 16:42:23 -0800388static void dump_crash_details(Tombstone* tombstone,
389 std::shared_ptr<unwindstack::Memory>& process_memory,
390 const ProcessInfo& process_info) {
391 uintptr_t address = process_info.crash_detail_page;
392 while (address) {
393 struct crash_detail_page_t page;
394 if (!process_memory->ReadFully(address, &page, sizeof(page))) {
395 async_safe_format_log(ANDROID_LOG_ERROR, LOG_TAG, "failed to read crash detail page: %m");
396 break;
397 }
398 if (page.used > kNumCrashDetails) {
399 async_safe_format_log(ANDROID_LOG_ERROR, LOG_TAG, "crash detail: page corrupted");
400 break;
401 }
402 for (size_t i = 0; i < page.used; ++i) {
403 const crash_detail_t& crash_detail = page.crash_details[i];
404 if (!crash_detail.data) {
405 continue;
406 }
407 std::string name(crash_detail.name_size, '\0');
408 if (!process_memory->ReadFully(reinterpret_cast<uintptr_t>(crash_detail.name), name.data(),
409 crash_detail.name_size)) {
410 async_safe_format_log(ANDROID_LOG_ERROR, LOG_TAG, "crash detail: failed to read name: %m");
411 continue;
412 }
413 std::string data(crash_detail.data_size, '\0');
414 if (!process_memory->ReadFully(reinterpret_cast<uintptr_t>(crash_detail.data), data.data(),
415 crash_detail.data_size)) {
416 async_safe_format_log(ANDROID_LOG_ERROR, LOG_TAG,
417 "crash detail: failed to read data for %s: %m", name.c_str());
418 continue;
419 }
420 auto* proto_detail = tombstone->add_crash_details();
421 proto_detail->set_name(name);
422 proto_detail->set_data(data);
423 }
424 address = reinterpret_cast<uintptr_t>(page.prev);
425 }
426}
427
Christopher Ferris3b7b7ba2022-03-15 16:56:09 -0700428static void dump_abort_message(Tombstone* tombstone,
429 std::shared_ptr<unwindstack::Memory>& process_memory,
Josh Gao76e1e302021-01-26 15:53:11 -0800430 const ProcessInfo& process_info) {
Josh Gao76e1e302021-01-26 15:53:11 -0800431 uintptr_t address = process_info.abort_msg_address;
432 if (address == 0) {
433 return;
434 }
435
436 size_t length;
437 if (!process_memory->ReadFully(address, &length, sizeof(length))) {
Josh Gao618cea32021-01-26 17:45:43 -0800438 async_safe_format_log(ANDROID_LOG_ERROR, LOG_TAG, "failed to read abort message header: %s",
439 strerror(errno));
Josh Gao76e1e302021-01-26 15:53:11 -0800440 return;
441 }
442
443 // The length field includes the length of the length field itself.
444 if (length < sizeof(size_t)) {
Josh Gao618cea32021-01-26 17:45:43 -0800445 async_safe_format_log(ANDROID_LOG_ERROR, LOG_TAG,
446 "abort message header malformed: claimed length = %zu", length);
Josh Gao76e1e302021-01-26 15:53:11 -0800447 return;
448 }
449
450 length -= sizeof(size_t);
451
452 // The abort message should be null terminated already, but reserve a spot for NUL just in case.
453 std::string msg;
454 msg.resize(length);
455
456 if (!process_memory->ReadFully(address + sizeof(length), &msg[0], length)) {
Josh Gao618cea32021-01-26 17:45:43 -0800457 async_safe_format_log(ANDROID_LOG_ERROR, LOG_TAG, "failed to read abort message header: %s",
458 strerror(errno));
Josh Gao76e1e302021-01-26 15:53:11 -0800459 return;
460 }
461
Christopher Ferrise8891452021-08-17 17:34:53 -0700462 // Remove any trailing newlines.
463 size_t index = msg.size();
464 while (index > 0 && (msg[index - 1] == '\0' || msg[index - 1] == '\n')) {
465 --index;
466 }
467 msg.resize(index);
468
Christopher Ferris6a5db682024-11-15 01:06:55 +0000469 // Make sure only UTF8 characters are present since abort_message is a string.
Christopher Ferris69210a62024-12-06 09:35:21 -0800470 tombstone->set_abort_message(oct_encode_non_ascii_printable(msg));
Josh Gao76e1e302021-01-26 15:53:11 -0800471}
472
473static void dump_open_fds(Tombstone* tombstone, const OpenFilesList* open_files) {
474 if (open_files) {
475 for (auto& [fd, entry] : *open_files) {
476 FD f;
477
478 f.set_fd(fd);
479
480 const std::optional<std::string>& path = entry.path;
481 if (path) {
482 f.set_path(*path);
483 }
484
485 const std::optional<uint64_t>& fdsan_owner = entry.fdsan_owner;
486 if (fdsan_owner) {
487 const char* type = android_fdsan_get_tag_type(*fdsan_owner);
488 uint64_t value = android_fdsan_get_tag_value(*fdsan_owner);
489 f.set_owner(type);
490 f.set_tag(value);
491 }
492
493 *tombstone->add_open_fds() = f;
494 }
495 }
496}
497
Christopher Ferris22ad09b2021-11-10 17:25:11 -0800498void fill_in_backtrace_frame(BacktraceFrame* f, const unwindstack::FrameData& frame) {
Peter Collingbourne1a1f7d72021-03-08 16:53:54 -0800499 f->set_rel_pc(frame.rel_pc);
500 f->set_pc(frame.pc);
501 f->set_sp(frame.sp);
502
503 if (!frame.function_name.empty()) {
504 // TODO: Should this happen here, or on the display side?
505 char* demangled_name = __cxa_demangle(frame.function_name.c_str(), nullptr, nullptr, nullptr);
506 if (demangled_name) {
507 f->set_function_name(demangled_name);
508 free(demangled_name);
509 } else {
510 f->set_function_name(frame.function_name);
511 }
512 }
513
514 f->set_function_offset(frame.function_offset);
515
Christopher Ferris22ad09b2021-11-10 17:25:11 -0800516 if (frame.map_info == nullptr) {
Peter Collingbourne1a1f7d72021-03-08 16:53:54 -0800517 // No valid map associated with this frame.
518 f->set_file_name("<unknown>");
Christopher Ferris22ad09b2021-11-10 17:25:11 -0800519 return;
520 }
521
522 if (!frame.map_info->name().empty()) {
523 f->set_file_name(frame.map_info->GetFullName());
Peter Collingbourne1a1f7d72021-03-08 16:53:54 -0800524 } else {
Christopher Ferris22ad09b2021-11-10 17:25:11 -0800525 f->set_file_name(StringPrintf("<anonymous:%" PRIx64 ">", frame.map_info->start()));
Peter Collingbourne1a1f7d72021-03-08 16:53:54 -0800526 }
Christopher Ferris22ad09b2021-11-10 17:25:11 -0800527 f->set_file_map_offset(frame.map_info->elf_start_offset());
Peter Collingbourne1a1f7d72021-03-08 16:53:54 -0800528
Christopher Ferris22ad09b2021-11-10 17:25:11 -0800529 f->set_build_id(frame.map_info->GetPrintableBuildID());
Peter Collingbourne1a1f7d72021-03-08 16:53:54 -0800530}
531
Christopher Ferris3b7b7ba2022-03-15 16:56:09 -0700532static void dump_registers(unwindstack::AndroidUnwinder* unwinder,
Christopher Ferrisb999b822022-02-09 17:57:21 -0800533 const std::unique_ptr<unwindstack::Regs>& regs, Thread& thread,
534 bool memory_dump) {
535 if (regs == nullptr) {
536 return;
537 }
538
539 unwindstack::Maps* maps = unwinder->GetMaps();
540 unwindstack::Memory* memory = unwinder->GetProcessMemory().get();
541
542 regs->IterateRegisters([&thread, memory_dump, maps, memory](const char* name, uint64_t value) {
543 Register r;
544 r.set_name(name);
545 r.set_u64(value);
546 *thread.add_registers() = r;
547
548 if (memory_dump) {
549 MemoryDump dump;
550
551 dump.set_register_name(name);
552 std::shared_ptr<unwindstack::MapInfo> map_info = maps->Find(untag_address(value));
553 if (map_info) {
554 dump.set_mapping_name(map_info->name());
555 }
556
557 constexpr size_t kNumBytesAroundRegister = 256;
558 constexpr size_t kNumTagsAroundRegister = kNumBytesAroundRegister / kTagGranuleSize;
559 char buf[kNumBytesAroundRegister];
560 uint8_t tags[kNumTagsAroundRegister];
561 ssize_t bytes = dump_memory(buf, sizeof(buf), tags, sizeof(tags), &value, memory);
562 if (bytes == -1) {
563 return;
564 }
565 dump.set_begin_address(value);
566 dump.set_memory(buf, bytes);
567
568 bool has_tags = false;
569#if defined(__aarch64__)
570 for (size_t i = 0; i < kNumTagsAroundRegister; ++i) {
571 if (tags[i] != 0) {
572 has_tags = true;
573 }
574 }
575#endif // defined(__aarch64__)
576
577 if (has_tags) {
578 dump.mutable_arm_mte_metadata()->set_memory_tags(tags, kNumTagsAroundRegister);
579 }
580
581 *thread.add_memory_dump() = std::move(dump);
582 }
583 });
584}
585
Christopher Ferris3b7b7ba2022-03-15 16:56:09 -0700586static void dump_thread_backtrace(std::vector<unwindstack::FrameData>& frames, Thread& thread) {
Christopher Ferrisc95047d2022-03-14 15:02:11 -0700587 std::set<std::string> unreadable_elf_files;
Christopher Ferris3b7b7ba2022-03-15 16:56:09 -0700588 for (const auto& frame : frames) {
Christopher Ferrisc95047d2022-03-14 15:02:11 -0700589 BacktraceFrame* f = thread.add_current_backtrace();
590 fill_in_backtrace_frame(f, frame);
591 if (frame.map_info != nullptr && frame.map_info->ElfFileNotReadable()) {
592 unreadable_elf_files.emplace(frame.map_info->name());
593 }
594 }
595
596 if (!unreadable_elf_files.empty()) {
597 auto unreadable_elf_files_proto = thread.mutable_unreadable_elf_files();
Christopher Ferrisb999b822022-02-09 17:57:21 -0800598 auto backtrace_note = thread.mutable_backtrace_note();
599 *backtrace_note->Add() =
600 "Function names and BuildId information is missing for some frames due";
601 *backtrace_note->Add() = "to unreadable libraries. For unwinds of apps, only shared libraries";
602 *backtrace_note->Add() = "found under the lib/ directory are readable.";
603 *backtrace_note->Add() = "On this device, run setenforce 0 to make the libraries readable.";
Christopher Ferrisc95047d2022-03-14 15:02:11 -0700604 *backtrace_note->Add() = "Unreadable libraries:";
605 for (auto& name : unreadable_elf_files) {
606 *backtrace_note->Add() = " " + name;
607 *unreadable_elf_files_proto->Add() = name;
608 }
Christopher Ferrisb999b822022-02-09 17:57:21 -0800609 }
610}
611
Christopher Ferris3b7b7ba2022-03-15 16:56:09 -0700612static void dump_thread(Tombstone* tombstone, unwindstack::AndroidUnwinder* unwinder,
Sijie Chenc8027932024-05-10 18:40:48 +0000613 const ThreadInfo& thread_info, bool memory_dump = false,
614 unwindstack::AndroidUnwinder* guest_unwinder = nullptr) {
Josh Gao76e1e302021-01-26 15:53:11 -0800615 Thread thread;
616
617 thread.set_id(thread_info.tid);
618 thread.set_name(thread_info.thread_name);
Peter Collingbourne1a1f7d72021-03-08 16:53:54 -0800619 thread.set_tagged_addr_ctrl(thread_info.tagged_addr_ctrl);
Elliott Hughesd13ea522022-01-13 09:20:26 -0800620 thread.set_pac_enabled_keys(thread_info.pac_enabled_keys);
Josh Gao76e1e302021-01-26 15:53:11 -0800621
Christopher Ferris3b7b7ba2022-03-15 16:56:09 -0700622 unwindstack::AndroidUnwinderData data;
623 // Indicate we want a copy of the initial registers.
624 data.saved_initial_regs = std::make_optional<std::unique_ptr<unwindstack::Regs>>();
625 bool unwind_ret;
626 if (thread_info.registers != nullptr) {
627 unwind_ret = unwinder->Unwind(thread_info.registers.get(), data);
Josh Gao76e1e302021-01-26 15:53:11 -0800628 } else {
Christopher Ferris3b7b7ba2022-03-15 16:56:09 -0700629 unwind_ret = unwinder->Unwind(thread_info.tid, data);
Josh Gao76e1e302021-01-26 15:53:11 -0800630 }
Christopher Ferris3b7b7ba2022-03-15 16:56:09 -0700631 if (!unwind_ret) {
632 async_safe_format_log(ANDROID_LOG_ERROR, LOG_TAG, "Unwind failed for tid %d: Error %s",
633 thread_info.tid, data.GetErrorString().c_str());
634 } else {
635 dump_thread_backtrace(data.frames, thread);
636 }
637 dump_registers(unwinder, *data.saved_initial_regs, thread, memory_dump);
Josh Gao76e1e302021-01-26 15:53:11 -0800638
639 auto& threads = *tombstone->mutable_threads();
640 threads[thread_info.tid] = thread;
Sijie Chenc8027932024-05-10 18:40:48 +0000641
642 if (guest_unwinder) {
643 if (!thread_info.guest_registers) {
644 async_safe_format_log(ANDROID_LOG_INFO, LOG_TAG,
645 "No guest state registers information for tid %d", thread_info.tid);
646 return;
647 }
648 Thread guest_thread;
649 unwindstack::AndroidUnwinderData guest_data;
650 guest_data.saved_initial_regs = std::make_optional<std::unique_ptr<unwindstack::Regs>>();
651 if (guest_unwinder->Unwind(thread_info.guest_registers.get(), guest_data)) {
652 dump_thread_backtrace(guest_data.frames, guest_thread);
653 } else {
654 async_safe_format_log(ANDROID_LOG_ERROR, LOG_TAG,
655 "Unwind guest state registers failed for tid %d: Error %s",
656 thread_info.tid, guest_data.GetErrorString().c_str());
657 }
658 dump_registers(guest_unwinder, *guest_data.saved_initial_regs, guest_thread, memory_dump);
659 auto& guest_threads = *tombstone->mutable_guest_threads();
660 guest_threads[thread_info.tid] = guest_thread;
661 }
Josh Gao76e1e302021-01-26 15:53:11 -0800662}
663
Christopher Ferris3b7b7ba2022-03-15 16:56:09 -0700664static void dump_mappings(Tombstone* tombstone, unwindstack::Maps* maps,
665 std::shared_ptr<unwindstack::Memory>& process_memory) {
Josh Gao76e1e302021-01-26 15:53:11 -0800666 for (const auto& map_info : *maps) {
667 auto* map = tombstone->add_memory_mappings();
David Srbeckyd8ab48b2021-05-13 00:03:26 +0100668 map->set_begin_address(map_info->start());
669 map->set_end_address(map_info->end());
670 map->set_offset(map_info->offset());
Josh Gao76e1e302021-01-26 15:53:11 -0800671
David Srbeckyd8ab48b2021-05-13 00:03:26 +0100672 if (map_info->flags() & PROT_READ) {
Josh Gao76e1e302021-01-26 15:53:11 -0800673 map->set_read(true);
674 }
David Srbeckyd8ab48b2021-05-13 00:03:26 +0100675 if (map_info->flags() & PROT_WRITE) {
Josh Gao76e1e302021-01-26 15:53:11 -0800676 map->set_write(true);
677 }
David Srbeckyd8ab48b2021-05-13 00:03:26 +0100678 if (map_info->flags() & PROT_EXEC) {
Josh Gao76e1e302021-01-26 15:53:11 -0800679 map->set_execute(true);
680 }
681
David Srbeckyd8ab48b2021-05-13 00:03:26 +0100682 map->set_mapping_name(map_info->name());
Josh Gao76e1e302021-01-26 15:53:11 -0800683
684 std::string build_id = map_info->GetPrintableBuildID();
685 if (!build_id.empty()) {
686 map->set_build_id(build_id);
687 }
688
689 map->set_load_bias(map_info->GetLoadBias(process_memory));
690 }
691}
692
Christopher Ferris3a0833c2023-07-28 13:07:53 -0700693// This creates a fake log message that indicates an error occurred when
694// reading the log.
695static void add_error_log_msg(Tombstone* tombstone, const std::string&& error_msg) {
696 LogBuffer buffer;
697 buffer.set_name("ERROR");
698
699 LogMessage* log_msg = buffer.add_logs();
700 log_msg->set_timestamp("00-00 00:00:00.000");
701 log_msg->set_pid(0);
702 log_msg->set_tid(0);
703 log_msg->set_priority(ANDROID_LOG_ERROR);
704 log_msg->set_tag("");
705 log_msg->set_message(error_msg);
706
707 *tombstone->add_log_buffers() = std::move(buffer);
708
709 async_safe_format_log(ANDROID_LOG_ERROR, LOG_TAG, "%s", error_msg.c_str());
710}
711
Josh Gao76e1e302021-01-26 15:53:11 -0800712static void dump_log_file(Tombstone* tombstone, const char* logger, pid_t pid) {
Christopher Ferris98d62422023-05-24 20:01:10 +0000713 logger_list* logger_list = android_logger_list_open(android_name_to_log_id(logger),
714 ANDROID_LOG_NONBLOCK, kMaxLogMessages, pid);
Christopher Ferris3a0833c2023-07-28 13:07:53 -0700715 if (logger_list == nullptr) {
716 add_error_log_msg(tombstone, android::base::StringPrintf("Cannot open log file %s", logger));
717 return;
718 }
Josh Gao76e1e302021-01-26 15:53:11 -0800719
720 LogBuffer buffer;
Josh Gao76e1e302021-01-26 15:53:11 -0800721 while (true) {
722 log_msg log_entry;
723 ssize_t actual = android_logger_list_read(logger_list, &log_entry);
Josh Gao76e1e302021-01-26 15:53:11 -0800724 if (actual < 0) {
725 if (actual == -EINTR) {
726 // interrupted by signal, retry
727 continue;
728 }
Christopher Ferris3a0833c2023-07-28 13:07:53 -0700729 // Don't consider EAGAIN an error since this is a non-blocking call.
730 if (actual != -EAGAIN) {
731 add_error_log_msg(tombstone, android::base::StringPrintf("reading log %s failed (%s)",
732 logger, strerror(-actual)));
Josh Gao76e1e302021-01-26 15:53:11 -0800733 }
Christopher Ferris3a0833c2023-07-28 13:07:53 -0700734 break;
Josh Gao76e1e302021-01-26 15:53:11 -0800735 } else if (actual == 0) {
Josh Gao76e1e302021-01-26 15:53:11 -0800736 break;
737 }
738
739 char timestamp_secs[32];
740 time_t sec = static_cast<time_t>(log_entry.entry.sec);
741 tm tm;
742 localtime_r(&sec, &tm);
743 strftime(timestamp_secs, sizeof(timestamp_secs), "%m-%d %H:%M:%S", &tm);
744 std::string timestamp =
745 StringPrintf("%s.%03d", timestamp_secs, log_entry.entry.nsec / 1'000'000);
746
747 // Msg format is: <priority:1><tag:N>\0<message:N>\0
748 char* msg = log_entry.msg();
749 if (msg == nullptr) {
750 continue;
751 }
752
753 unsigned char prio = msg[0];
754 char* tag = msg + 1;
755 msg = tag + strlen(tag) + 1;
756
757 // consume any trailing newlines
758 char* nl = msg + strlen(msg) - 1;
759 while (nl >= msg && *nl == '\n') {
760 *nl-- = '\0';
761 }
762
763 // Look for line breaks ('\n') and display each text line
764 // on a separate line, prefixed with the header, like logcat does.
765 do {
766 nl = strchr(msg, '\n');
767 if (nl != nullptr) {
768 *nl = '\0';
769 ++nl;
770 }
771
772 LogMessage* log_msg = buffer.add_logs();
773 log_msg->set_timestamp(timestamp);
774 log_msg->set_pid(log_entry.entry.pid);
775 log_msg->set_tid(log_entry.entry.tid);
776 log_msg->set_priority(prio);
777 log_msg->set_tag(tag);
Christopher Ferris6a5db682024-11-15 01:06:55 +0000778 // Make sure only UTF8 characters are present since message is a string.
Christopher Ferris69210a62024-12-06 09:35:21 -0800779 log_msg->set_message(oct_encode_non_ascii_printable(msg));
Josh Gao76e1e302021-01-26 15:53:11 -0800780 } while ((msg = nl));
781 }
782 android_logger_list_free(logger_list);
783
784 if (!buffer.logs().empty()) {
785 buffer.set_name(logger);
786 *tombstone->add_log_buffers() = std::move(buffer);
787 }
788}
789
790static void dump_logcat(Tombstone* tombstone, pid_t pid) {
791 dump_log_file(tombstone, "system", pid);
792 dump_log_file(tombstone, "main", pid);
793}
794
Mitch Phillips5ddcea22021-04-19 09:59:17 -0700795static void dump_tags_around_fault_addr(Signal* signal, const Tombstone& tombstone,
Christopher Ferris3b7b7ba2022-03-15 16:56:09 -0700796 std::shared_ptr<unwindstack::Memory>& process_memory,
797 uintptr_t fault_addr) {
Mitch Phillips5ddcea22021-04-19 09:59:17 -0700798 if (tombstone.arch() != Architecture::ARM64) return;
799
800 fault_addr = untag_address(fault_addr);
801 constexpr size_t kNumGranules = kNumTagRows * kNumTagColumns;
802 constexpr size_t kBytesToRead = kNumGranules * kTagGranuleSize;
803
804 // If the low part of the tag dump would underflow to the high address space, it's probably not
805 // a valid address for us to dump tags from.
806 if (fault_addr < kBytesToRead / 2) return;
807
Mitch Phillips5ddcea22021-04-19 09:59:17 -0700808 constexpr uintptr_t kRowStartMask = ~(kNumTagColumns * kTagGranuleSize - 1);
809 size_t start_address = (fault_addr & kRowStartMask) - kBytesToRead / 2;
810 MemoryDump tag_dump;
811 size_t granules_to_read = kNumGranules;
812
813 // Attempt to read the first tag. If reading fails, this likely indicates the
814 // lowest touched page is inaccessible or not marked with PROT_MTE.
815 // Fast-forward over pages until one has tags, or we exhaust the search range.
Christopher Ferris3b7b7ba2022-03-15 16:56:09 -0700816 while (process_memory->ReadTag(start_address) < 0) {
Mitch Phillips5ddcea22021-04-19 09:59:17 -0700817 size_t page_size = sysconf(_SC_PAGE_SIZE);
818 size_t bytes_to_next_page = page_size - (start_address % page_size);
819 if (bytes_to_next_page >= granules_to_read * kTagGranuleSize) return;
820 start_address += bytes_to_next_page;
821 granules_to_read -= bytes_to_next_page / kTagGranuleSize;
822 }
823 tag_dump.set_begin_address(start_address);
824
825 std::string* mte_tags = tag_dump.mutable_arm_mte_metadata()->mutable_memory_tags();
826
827 for (size_t i = 0; i < granules_to_read; ++i) {
Christopher Ferris3b7b7ba2022-03-15 16:56:09 -0700828 long tag = process_memory->ReadTag(start_address + i * kTagGranuleSize);
Mitch Phillips5ddcea22021-04-19 09:59:17 -0700829 if (tag < 0) break;
830 mte_tags->push_back(static_cast<uint8_t>(tag));
831 }
832
833 if (!mte_tags->empty()) {
834 *signal->mutable_fault_adjacent_metadata() = tag_dump;
835 }
836}
837
Christopher Ferris3b7b7ba2022-03-15 16:56:09 -0700838void engrave_tombstone_proto(Tombstone* tombstone, unwindstack::AndroidUnwinder* unwinder,
Florian Mayer2d453312024-04-22 23:43:59 +0000839 const std::map<pid_t, ThreadInfo>& threads, pid_t target_tid,
Sijie Chenc8027932024-05-10 18:40:48 +0000840 const ProcessInfo& process_info, const OpenFilesList* open_files,
841 const Architecture* guest_arch,
842 unwindstack::AndroidUnwinder* guest_unwinder) {
Josh Gao76e1e302021-01-26 15:53:11 -0800843 Tombstone result;
844
845 result.set_arch(get_arch());
Sijie Chenc8027932024-05-10 18:40:48 +0000846 if (guest_arch != nullptr) {
847 result.set_guest_arch(*guest_arch);
848 } else {
849 result.set_guest_arch(Architecture::NONE);
850 }
Josh Gao76e1e302021-01-26 15:53:11 -0800851 result.set_build_fingerprint(android::base::GetProperty("ro.build.fingerprint", "unknown"));
852 result.set_revision(android::base::GetProperty("ro.revision", "unknown"));
853 result.set_timestamp(get_timestamp());
854
Florian Mayer2d453312024-04-22 23:43:59 +0000855 const ThreadInfo& target_thread = threads.at(target_tid);
856 result.set_pid(target_thread.pid);
857 result.set_tid(target_thread.tid);
858 result.set_uid(target_thread.uid);
859 result.set_selinux_label(target_thread.selinux_label);
Christopher Ferris2b98c822021-09-28 15:50:04 -0700860 // The main thread must have a valid siginfo.
Florian Mayer2d453312024-04-22 23:43:59 +0000861 CHECK(target_thread.siginfo != nullptr);
Josh Gao76e1e302021-01-26 15:53:11 -0800862
Elliott Hughes32d3cdd2021-09-21 12:20:53 -0700863 struct sysinfo si;
864 sysinfo(&si);
865 android::procinfo::ProcessInfo proc_info;
866 std::string error;
Florian Mayer2d453312024-04-22 23:43:59 +0000867 if (android::procinfo::GetProcessInfo(target_thread.pid, &proc_info, &error)) {
Elliott Hughes32d3cdd2021-09-21 12:20:53 -0700868 uint64_t starttime = proc_info.starttime / sysconf(_SC_CLK_TCK);
869 result.set_process_uptime(si.uptime - starttime);
870 } else {
871 async_safe_format_log(ANDROID_LOG_ERROR, LOG_TAG, "failed to read process info: %s",
872 error.c_str());
873 }
874
Devin Moore4647b6b2024-05-03 00:02:24 +0000875 result.set_page_size(getpagesize());
876 result.set_has_been_16kb_mode(android::base::GetBoolProperty("ro.misctrl.16kb_before", false));
877
Josh Gao31348a72021-03-29 21:53:42 -0700878 auto cmd_line = result.mutable_command_line();
Florian Mayer2d453312024-04-22 23:43:59 +0000879 for (const auto& arg : target_thread.command_line) {
Josh Gao31348a72021-03-29 21:53:42 -0700880 *cmd_line->Add() = arg;
881 }
882
Florian Mayer2d453312024-04-22 23:43:59 +0000883 if (!target_thread.siginfo) {
Josh Gao618cea32021-01-26 17:45:43 -0800884 async_safe_fatal("siginfo missing");
885 }
Josh Gao76e1e302021-01-26 15:53:11 -0800886
887 Signal sig;
Florian Mayer2d453312024-04-22 23:43:59 +0000888 sig.set_number(target_thread.signo);
889 sig.set_name(get_signame(target_thread.siginfo));
890 sig.set_code(target_thread.siginfo->si_code);
891 sig.set_code_name(get_sigcode(target_thread.siginfo));
Josh Gao76e1e302021-01-26 15:53:11 -0800892
Florian Mayer2d453312024-04-22 23:43:59 +0000893 if (signal_has_sender(target_thread.siginfo, target_thread.pid)) {
Josh Gao76e1e302021-01-26 15:53:11 -0800894 sig.set_has_sender(true);
Florian Mayer2d453312024-04-22 23:43:59 +0000895 sig.set_sender_uid(target_thread.siginfo->si_uid);
896 sig.set_sender_pid(target_thread.siginfo->si_pid);
Josh Gao76e1e302021-01-26 15:53:11 -0800897 }
898
899 if (process_info.has_fault_address) {
900 sig.set_has_fault_address(true);
Mitch Phillips5ddcea22021-04-19 09:59:17 -0700901 uintptr_t fault_addr = process_info.maybe_tagged_fault_address;
902 sig.set_fault_address(fault_addr);
Christopher Ferris3b7b7ba2022-03-15 16:56:09 -0700903 dump_tags_around_fault_addr(&sig, result, unwinder->GetProcessMemory(), fault_addr);
Josh Gao76e1e302021-01-26 15:53:11 -0800904 }
905
906 *result.mutable_signal_info() = sig;
907
Christopher Ferris3b7b7ba2022-03-15 16:56:09 -0700908 dump_abort_message(&result, unwinder->GetProcessMemory(), process_info);
Florian Mayer5fa66632024-02-07 16:42:23 -0800909 dump_crash_details(&result, unwinder->GetProcessMemory(), process_info);
Florian Mayer2d453312024-04-22 23:43:59 +0000910 // Dump the target thread, but save the memory around the registers.
Sijie Chenc8027932024-05-10 18:40:48 +0000911 dump_thread(&result, unwinder, target_thread, /* memory_dump */ true, guest_unwinder);
Josh Gao76e1e302021-01-26 15:53:11 -0800912
913 for (const auto& [tid, thread_info] : threads) {
Florian Mayer2d453312024-04-22 23:43:59 +0000914 if (tid != target_tid) {
Sijie Chenc8027932024-05-10 18:40:48 +0000915 dump_thread(&result, unwinder, thread_info, /* memory_dump */ false, guest_unwinder);
Josh Gao76e1e302021-01-26 15:53:11 -0800916 }
917 }
918
Florian Mayere3e7bc72024-04-26 09:38:50 -0700919 dump_probable_cause(&result, unwinder, process_info, target_thread, threads);
Josh Gao76e1e302021-01-26 15:53:11 -0800920
Christopher Ferris3b7b7ba2022-03-15 16:56:09 -0700921 dump_mappings(&result, unwinder->GetMaps(), unwinder->GetProcessMemory());
Josh Gao76e1e302021-01-26 15:53:11 -0800922
923 // Only dump logs on debuggable devices.
924 if (android::base::GetBoolProperty("ro.debuggable", false)) {
Christopher Ferrisbda10642023-04-24 18:14:53 -0700925 // Get the thread that corresponds to the main pid of the process.
Florian Mayer2d453312024-04-22 23:43:59 +0000926 const ThreadInfo& thread = threads.at(target_thread.pid);
Christopher Ferrisbda10642023-04-24 18:14:53 -0700927
928 // Do not attempt to dump logs of the logd process because the gathering
929 // of logs can hang until a timeout occurs.
930 if (thread.thread_name != "logd") {
Florian Mayer2d453312024-04-22 23:43:59 +0000931 dump_logcat(&result, target_thread.pid);
Christopher Ferrisbda10642023-04-24 18:14:53 -0700932 }
Josh Gao76e1e302021-01-26 15:53:11 -0800933 }
934
935 dump_open_fds(&result, open_files);
936
937 *tombstone = std::move(result);
938}