blob: 765700156b2047582194d3e3309f7134609880c5 [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"
21#include "libdebuggerd/scudo.h"
Josh Gao76e1e302021-01-26 15:53:11 -080022
23#include <errno.h>
24#include <fcntl.h>
25#include <inttypes.h>
26#include <signal.h>
27#include <stddef.h>
28#include <stdlib.h>
29#include <string.h>
30#include <sys/mman.h>
31#include <time.h>
32
33#include <memory>
Josh Gaodbb83de2021-03-01 23:13:13 -080034#include <optional>
Josh Gao76e1e302021-01-26 15:53:11 -080035#include <string>
36
Josh Gao618cea32021-01-26 17:45:43 -080037#include <async_safe/log.h>
38
Josh Gaodbb83de2021-03-01 23:13:13 -080039#include <android-base/file.h>
Josh Gao76e1e302021-01-26 15:53:11 -080040#include <android-base/properties.h>
41#include <android-base/stringprintf.h>
42#include <android-base/strings.h>
43#include <android-base/unique_fd.h>
44
45#include <android/log.h>
Peter Collingbourne0ea08c22021-02-05 14:59:08 -080046#include <bionic/macros.h>
Josh Gao76e1e302021-01-26 15:53:11 -080047#include <log/log.h>
48#include <log/log_read.h>
49#include <log/logprint.h>
50#include <private/android_filesystem_config.h>
51
Josh Gaodbb83de2021-03-01 23:13:13 -080052#include <procinfo/process.h>
Josh Gao76e1e302021-01-26 15:53:11 -080053#include <unwindstack/Maps.h>
54#include <unwindstack/Memory.h>
55#include <unwindstack/Regs.h>
56#include <unwindstack/Unwinder.h>
57
58#include "libdebuggerd/open_files_list.h"
59#include "libdebuggerd/utility.h"
60#include "util.h"
61
62#include "tombstone.pb.h"
63
64using android::base::StringPrintf;
65
66// Use the demangler from libc++.
67extern "C" char* __cxa_demangle(const char*, char*, size_t*, int* status);
68
69static Architecture get_arch() {
70#if defined(__arm__)
71 return Architecture::ARM32;
72#elif defined(__aarch64__)
73 return Architecture::ARM64;
74#elif defined(__i386__)
75 return Architecture::X86;
76#elif defined(__x86_64__)
77 return Architecture::X86_64;
78#else
79#error Unknown architecture!
80#endif
81}
82
83static std::optional<std::string> get_stack_overflow_cause(uint64_t fault_addr, uint64_t sp,
84 unwindstack::Maps* maps) {
85 static constexpr uint64_t kMaxDifferenceBytes = 256;
86 uint64_t difference;
87 if (sp >= fault_addr) {
88 difference = sp - fault_addr;
89 } else {
90 difference = fault_addr - sp;
91 }
92 if (difference <= kMaxDifferenceBytes) {
93 // The faulting address is close to the current sp, check if the sp
94 // indicates a stack overflow.
95 // On arm, the sp does not get updated when the instruction faults.
96 // In this case, the sp will still be in a valid map, which is the
97 // last case below.
98 // On aarch64, the sp does get updated when the instruction faults.
99 // In this case, the sp will be in either an invalid map if triggered
100 // on the main thread, or in a guard map if in another thread, which
101 // will be the first case or second case from below.
102 unwindstack::MapInfo* map_info = maps->Find(sp);
103 if (map_info == nullptr) {
104 return "stack pointer is in a non-existent map; likely due to stack overflow.";
105 } else if ((map_info->flags & (PROT_READ | PROT_WRITE)) != (PROT_READ | PROT_WRITE)) {
106 return "stack pointer is not in a rw map; likely due to stack overflow.";
107 } else if ((sp - map_info->start) <= kMaxDifferenceBytes) {
108 return "stack pointer is close to top of stack; likely stack overflow.";
109 }
110 }
111 return {};
112}
113
Peter Collingbourne1a1f7d72021-03-08 16:53:54 -0800114void set_human_readable_cause(Cause* cause, uint64_t fault_addr) {
115 if (!cause->has_memory_error() || !cause->memory_error().has_heap()) {
116 return;
117 }
118
119 const MemoryError& memory_error = cause->memory_error();
120 const HeapObject& heap_object = memory_error.heap();
121
122 const char *tool_str;
123 switch (memory_error.tool()) {
124 case MemoryError_Tool_GWP_ASAN:
125 tool_str = "GWP-ASan";
126 break;
127 case MemoryError_Tool_SCUDO:
128 tool_str = "MTE";
129 break;
130 default:
131 tool_str = "Unknown";
132 break;
133 }
134
135 const char *error_type_str;
136 switch (memory_error.type()) {
137 case MemoryError_Type_USE_AFTER_FREE:
138 error_type_str = "Use After Free";
139 break;
140 case MemoryError_Type_DOUBLE_FREE:
141 error_type_str = "Double Free";
142 break;
143 case MemoryError_Type_INVALID_FREE:
144 error_type_str = "Invalid (Wild) Free";
145 break;
146 case MemoryError_Type_BUFFER_OVERFLOW:
147 error_type_str = "Buffer Overflow";
148 break;
149 case MemoryError_Type_BUFFER_UNDERFLOW:
150 error_type_str = "Buffer Underflow";
151 break;
152 default:
153 cause->set_human_readable(
154 StringPrintf("[%s]: Unknown error occurred at 0x%" PRIx64 ".", tool_str, fault_addr));
155 return;
156 }
157
158 uint64_t diff;
159 const char* location_str;
160
161 if (fault_addr < heap_object.address()) {
162 // Buffer Underflow, 6 bytes left of a 41-byte allocation at 0xdeadbeef.
163 location_str = "left of";
164 diff = heap_object.address() - fault_addr;
165 } else if (fault_addr - heap_object.address() < heap_object.size()) {
166 // Use After Free, 40 bytes into a 41-byte allocation at 0xdeadbeef.
167 location_str = "into";
168 diff = fault_addr - heap_object.address();
169 } else {
170 // Buffer Overflow, 6 bytes right of a 41-byte allocation at 0xdeadbeef.
171 location_str = "right of";
172 diff = fault_addr - heap_object.address() - heap_object.size();
173 }
174
175 // Suffix of 'bytes', i.e. 4 bytes' vs. '1 byte'.
176 const char* byte_suffix = "s";
177 if (diff == 1) {
178 byte_suffix = "";
179 }
180
181 cause->set_human_readable(StringPrintf(
182 "[%s]: %s, %" PRIu64 " byte%s %s a %" PRIu64 "-byte allocation at 0x%" PRIx64, tool_str,
183 error_type_str, diff, byte_suffix, location_str, heap_object.size(), heap_object.address()));
184}
185
186static void dump_probable_cause(Tombstone* tombstone, unwindstack::Unwinder* unwinder,
187 const ProcessInfo& process_info, const ThreadInfo& main_thread) {
188 ScudoCrashData scudo_crash_data(unwinder->GetProcessMemory().get(), process_info);
189 if (scudo_crash_data.CrashIsMine()) {
190 scudo_crash_data.AddCauseProtos(tombstone, unwinder);
191 return;
192 }
193
194 GwpAsanCrashData gwp_asan_crash_data(unwinder->GetProcessMemory().get(), process_info,
195 main_thread);
196 if (gwp_asan_crash_data.CrashIsMine()) {
197 gwp_asan_crash_data.AddCauseProtos(tombstone, unwinder);
198 return;
199 }
200
201 const siginfo *si = main_thread.siginfo;
202 auto fault_addr = reinterpret_cast<uint64_t>(si->si_addr);
203 unwindstack::Maps* maps = unwinder->GetMaps();
204
Josh Gao76e1e302021-01-26 15:53:11 -0800205 std::optional<std::string> cause;
206 if (si->si_signo == SIGSEGV && si->si_code == SEGV_MAPERR) {
Peter Collingbourne1a1f7d72021-03-08 16:53:54 -0800207 if (fault_addr < 4096) {
Josh Gao76e1e302021-01-26 15:53:11 -0800208 cause = "null pointer dereference";
Peter Collingbourne1a1f7d72021-03-08 16:53:54 -0800209 } else if (fault_addr == 0xffff0ffc) {
Josh Gao76e1e302021-01-26 15:53:11 -0800210 cause = "call to kuser_helper_version";
Peter Collingbourne1a1f7d72021-03-08 16:53:54 -0800211 } else if (fault_addr == 0xffff0fe0) {
Josh Gao76e1e302021-01-26 15:53:11 -0800212 cause = "call to kuser_get_tls";
Peter Collingbourne1a1f7d72021-03-08 16:53:54 -0800213 } else if (fault_addr == 0xffff0fc0) {
Josh Gao76e1e302021-01-26 15:53:11 -0800214 cause = "call to kuser_cmpxchg";
Peter Collingbourne1a1f7d72021-03-08 16:53:54 -0800215 } else if (fault_addr == 0xffff0fa0) {
Josh Gao76e1e302021-01-26 15:53:11 -0800216 cause = "call to kuser_memory_barrier";
Peter Collingbourne1a1f7d72021-03-08 16:53:54 -0800217 } else if (fault_addr == 0xffff0f60) {
Josh Gao76e1e302021-01-26 15:53:11 -0800218 cause = "call to kuser_cmpxchg64";
219 } else {
Peter Collingbourne1a1f7d72021-03-08 16:53:54 -0800220 cause = get_stack_overflow_cause(fault_addr, main_thread.registers->sp(), maps);
Josh Gao76e1e302021-01-26 15:53:11 -0800221 }
222 } else if (si->si_signo == SIGSEGV && si->si_code == SEGV_ACCERR) {
Josh Gao76e1e302021-01-26 15:53:11 -0800223 unwindstack::MapInfo* map_info = maps->Find(fault_addr);
224 if (map_info != nullptr && map_info->flags == PROT_EXEC) {
225 cause = "execute-only (no-read) memory access error; likely due to data in .text.";
226 } else {
Peter Collingbourne1a1f7d72021-03-08 16:53:54 -0800227 cause = get_stack_overflow_cause(fault_addr, main_thread.registers->sp(), maps);
Josh Gao76e1e302021-01-26 15:53:11 -0800228 }
229 } else if (si->si_signo == SIGSYS && si->si_code == SYS_SECCOMP) {
230 cause = StringPrintf("seccomp prevented call to disallowed %s system call %d", ABI_STRING,
231 si->si_syscall);
232 }
233
234 if (cause) {
Peter Collingbourne1a1f7d72021-03-08 16:53:54 -0800235 Cause *cause_proto = tombstone->add_causes();
236 cause_proto->set_human_readable(*cause);
Josh Gao76e1e302021-01-26 15:53:11 -0800237 }
238}
239
240static void dump_abort_message(Tombstone* tombstone, unwindstack::Unwinder* unwinder,
241 const ProcessInfo& process_info) {
242 std::shared_ptr<unwindstack::Memory> process_memory = unwinder->GetProcessMemory();
243 uintptr_t address = process_info.abort_msg_address;
244 if (address == 0) {
245 return;
246 }
247
248 size_t length;
249 if (!process_memory->ReadFully(address, &length, sizeof(length))) {
Josh Gao618cea32021-01-26 17:45:43 -0800250 async_safe_format_log(ANDROID_LOG_ERROR, LOG_TAG, "failed to read abort message header: %s",
251 strerror(errno));
Josh Gao76e1e302021-01-26 15:53:11 -0800252 return;
253 }
254
255 // The length field includes the length of the length field itself.
256 if (length < sizeof(size_t)) {
Josh Gao618cea32021-01-26 17:45:43 -0800257 async_safe_format_log(ANDROID_LOG_ERROR, LOG_TAG,
258 "abort message header malformed: claimed length = %zu", length);
Josh Gao76e1e302021-01-26 15:53:11 -0800259 return;
260 }
261
262 length -= sizeof(size_t);
263
264 // The abort message should be null terminated already, but reserve a spot for NUL just in case.
265 std::string msg;
266 msg.resize(length);
267
268 if (!process_memory->ReadFully(address + sizeof(length), &msg[0], length)) {
Josh Gao618cea32021-01-26 17:45:43 -0800269 async_safe_format_log(ANDROID_LOG_ERROR, LOG_TAG, "failed to read abort message header: %s",
270 strerror(errno));
Josh Gao76e1e302021-01-26 15:53:11 -0800271 return;
272 }
273
274 tombstone->set_abort_message(msg);
275}
276
277static void dump_open_fds(Tombstone* tombstone, const OpenFilesList* open_files) {
278 if (open_files) {
279 for (auto& [fd, entry] : *open_files) {
280 FD f;
281
282 f.set_fd(fd);
283
284 const std::optional<std::string>& path = entry.path;
285 if (path) {
286 f.set_path(*path);
287 }
288
289 const std::optional<uint64_t>& fdsan_owner = entry.fdsan_owner;
290 if (fdsan_owner) {
291 const char* type = android_fdsan_get_tag_type(*fdsan_owner);
292 uint64_t value = android_fdsan_get_tag_value(*fdsan_owner);
293 f.set_owner(type);
294 f.set_tag(value);
295 }
296
297 *tombstone->add_open_fds() = f;
298 }
299 }
300}
301
Peter Collingbourne1a1f7d72021-03-08 16:53:54 -0800302void fill_in_backtrace_frame(BacktraceFrame* f, const unwindstack::FrameData& frame,
303 unwindstack::Maps* maps) {
304 f->set_rel_pc(frame.rel_pc);
305 f->set_pc(frame.pc);
306 f->set_sp(frame.sp);
307
308 if (!frame.function_name.empty()) {
309 // TODO: Should this happen here, or on the display side?
310 char* demangled_name = __cxa_demangle(frame.function_name.c_str(), nullptr, nullptr, nullptr);
311 if (demangled_name) {
312 f->set_function_name(demangled_name);
313 free(demangled_name);
314 } else {
315 f->set_function_name(frame.function_name);
316 }
317 }
318
319 f->set_function_offset(frame.function_offset);
320
321 if (frame.map_start == frame.map_end) {
322 // No valid map associated with this frame.
323 f->set_file_name("<unknown>");
324 } else if (!frame.map_name.empty()) {
325 f->set_file_name(frame.map_name);
326 } else {
327 f->set_file_name(StringPrintf("<anonymous:%" PRIx64 ">", frame.map_start));
328 }
329
330 f->set_file_map_offset(frame.map_elf_start_offset);
331
332 unwindstack::MapInfo* map_info = maps->Find(frame.map_start);
333 if (map_info) {
334 f->set_build_id(map_info->GetPrintableBuildID());
335 }
336}
337
Josh Gao76e1e302021-01-26 15:53:11 -0800338static void dump_thread(Tombstone* tombstone, unwindstack::Unwinder* unwinder,
339 const ThreadInfo& thread_info, bool memory_dump = false) {
340 Thread thread;
341
342 thread.set_id(thread_info.tid);
343 thread.set_name(thread_info.thread_name);
Peter Collingbourne1a1f7d72021-03-08 16:53:54 -0800344 thread.set_tagged_addr_ctrl(thread_info.tagged_addr_ctrl);
Josh Gao76e1e302021-01-26 15:53:11 -0800345
346 unwindstack::Maps* maps = unwinder->GetMaps();
347 unwindstack::Memory* memory = unwinder->GetProcessMemory().get();
348
349 thread_info.registers->IterateRegisters(
350 [&thread, memory_dump, maps, memory](const char* name, uint64_t value) {
351 Register r;
352 r.set_name(name);
353 r.set_u64(value);
354 *thread.add_registers() = r;
355
356 if (memory_dump) {
357 MemoryDump dump;
358
Josh Gao76e1e302021-01-26 15:53:11 -0800359 dump.set_register_name(name);
Peter Collingbourne0ea08c22021-02-05 14:59:08 -0800360 unwindstack::MapInfo* map_info = maps->Find(untag_address(value));
Josh Gao76e1e302021-01-26 15:53:11 -0800361 if (map_info) {
362 dump.set_mapping_name(map_info->name);
363 }
364
Peter Collingbourne1a1f7d72021-03-08 16:53:54 -0800365 char buf[256];
366 uint8_t tags[256 / kTagGranuleSize];
367 size_t start_offset = 0;
368 ssize_t bytes = dump_memory(buf, sizeof(buf), tags, sizeof(tags), &value, memory);
369 if (bytes == -1) {
370 return;
371 }
Josh Gao76e1e302021-01-26 15:53:11 -0800372 dump.set_begin_address(value);
373
Josh Gao618cea32021-01-26 17:45:43 -0800374 if (start_offset + bytes > sizeof(buf)) {
375 async_safe_fatal("dump_memory overflowed? start offset = %zu, bytes read = %zd",
376 start_offset, bytes);
377 }
378
Peter Collingbourne1a1f7d72021-03-08 16:53:54 -0800379 dump.set_memory(buf, bytes);
380 dump.set_tags(tags, bytes / kTagGranuleSize);
Josh Gao76e1e302021-01-26 15:53:11 -0800381
382 *thread.add_memory_dump() = std::move(dump);
383 }
384 });
385
386 std::unique_ptr<unwindstack::Regs> regs_copy(thread_info.registers->Clone());
387 unwinder->SetRegs(regs_copy.get());
388 unwinder->Unwind();
389 if (unwinder->NumFrames() == 0) {
Josh Gao618cea32021-01-26 17:45:43 -0800390 async_safe_format_log(ANDROID_LOG_ERROR, LOG_TAG, "failed to unwind");
Josh Gao76e1e302021-01-26 15:53:11 -0800391 if (unwinder->LastErrorCode() != unwindstack::ERROR_NONE) {
Josh Gao618cea32021-01-26 17:45:43 -0800392 async_safe_format_log(ANDROID_LOG_ERROR, LOG_TAG, " error code: %s",
393 unwinder->LastErrorCodeString());
394 async_safe_format_log(ANDROID_LOG_ERROR, LOG_TAG, " error address: 0x%" PRIx64,
395 unwinder->LastErrorAddress());
Josh Gao76e1e302021-01-26 15:53:11 -0800396 }
397 } else {
398 unwinder->SetDisplayBuildID(true);
399 for (const auto& frame : unwinder->frames()) {
400 BacktraceFrame* f = thread.add_current_backtrace();
Peter Collingbourne1a1f7d72021-03-08 16:53:54 -0800401 fill_in_backtrace_frame(f, frame, maps);
Josh Gao76e1e302021-01-26 15:53:11 -0800402 }
403 }
404
405 auto& threads = *tombstone->mutable_threads();
406 threads[thread_info.tid] = thread;
407}
408
409static void dump_main_thread(Tombstone* tombstone, unwindstack::Unwinder* unwinder,
410 const ThreadInfo& thread_info) {
411 dump_thread(tombstone, unwinder, thread_info, true);
412}
413
414static void dump_mappings(Tombstone* tombstone, unwindstack::Unwinder* unwinder) {
415 unwindstack::Maps* maps = unwinder->GetMaps();
416 std::shared_ptr<unwindstack::Memory> process_memory = unwinder->GetProcessMemory();
417
418 for (const auto& map_info : *maps) {
419 auto* map = tombstone->add_memory_mappings();
420 map->set_begin_address(map_info->start);
421 map->set_end_address(map_info->end);
422 map->set_offset(map_info->offset);
423
424 if (map_info->flags & PROT_READ) {
425 map->set_read(true);
426 }
427 if (map_info->flags & PROT_WRITE) {
428 map->set_write(true);
429 }
430 if (map_info->flags & PROT_EXEC) {
431 map->set_execute(true);
432 }
433
434 map->set_mapping_name(map_info->name);
435
436 std::string build_id = map_info->GetPrintableBuildID();
437 if (!build_id.empty()) {
438 map->set_build_id(build_id);
439 }
440
441 map->set_load_bias(map_info->GetLoadBias(process_memory));
442 }
443}
444
445static void dump_log_file(Tombstone* tombstone, const char* logger, pid_t pid) {
446 logger_list* logger_list =
447 android_logger_list_open(android_name_to_log_id(logger), ANDROID_LOG_NONBLOCK, 0, pid);
448
449 LogBuffer buffer;
450
451 while (true) {
452 log_msg log_entry;
453 ssize_t actual = android_logger_list_read(logger_list, &log_entry);
454
455 if (actual < 0) {
456 if (actual == -EINTR) {
457 // interrupted by signal, retry
458 continue;
459 }
460 if (actual == -EAGAIN) {
461 // non-blocking EOF; we're done
462 break;
463 } else {
Josh Gao76e1e302021-01-26 15:53:11 -0800464 break;
465 }
466 } else if (actual == 0) {
Josh Gao76e1e302021-01-26 15:53:11 -0800467 break;
468 }
469
470 char timestamp_secs[32];
471 time_t sec = static_cast<time_t>(log_entry.entry.sec);
472 tm tm;
473 localtime_r(&sec, &tm);
474 strftime(timestamp_secs, sizeof(timestamp_secs), "%m-%d %H:%M:%S", &tm);
475 std::string timestamp =
476 StringPrintf("%s.%03d", timestamp_secs, log_entry.entry.nsec / 1'000'000);
477
478 // Msg format is: <priority:1><tag:N>\0<message:N>\0
479 char* msg = log_entry.msg();
480 if (msg == nullptr) {
481 continue;
482 }
483
484 unsigned char prio = msg[0];
485 char* tag = msg + 1;
486 msg = tag + strlen(tag) + 1;
487
488 // consume any trailing newlines
489 char* nl = msg + strlen(msg) - 1;
490 while (nl >= msg && *nl == '\n') {
491 *nl-- = '\0';
492 }
493
494 // Look for line breaks ('\n') and display each text line
495 // on a separate line, prefixed with the header, like logcat does.
496 do {
497 nl = strchr(msg, '\n');
498 if (nl != nullptr) {
499 *nl = '\0';
500 ++nl;
501 }
502
503 LogMessage* log_msg = buffer.add_logs();
504 log_msg->set_timestamp(timestamp);
505 log_msg->set_pid(log_entry.entry.pid);
506 log_msg->set_tid(log_entry.entry.tid);
507 log_msg->set_priority(prio);
508 log_msg->set_tag(tag);
509 log_msg->set_message(msg);
510 } while ((msg = nl));
511 }
512 android_logger_list_free(logger_list);
513
514 if (!buffer.logs().empty()) {
515 buffer.set_name(logger);
516 *tombstone->add_log_buffers() = std::move(buffer);
517 }
518}
519
520static void dump_logcat(Tombstone* tombstone, pid_t pid) {
521 dump_log_file(tombstone, "system", pid);
522 dump_log_file(tombstone, "main", pid);
523}
524
Josh Gaodbb83de2021-03-01 23:13:13 -0800525static std::optional<uint64_t> read_uptime_secs() {
526 std::string uptime;
527 if (!android::base::ReadFileToString("/proc/uptime", &uptime)) {
528 return {};
529 }
530 return strtoll(uptime.c_str(), nullptr, 10);
531}
532
Josh Gao76e1e302021-01-26 15:53:11 -0800533void engrave_tombstone_proto(Tombstone* tombstone, unwindstack::Unwinder* unwinder,
534 const std::map<pid_t, ThreadInfo>& threads, pid_t target_thread,
535 const ProcessInfo& process_info, const OpenFilesList* open_files) {
536 Tombstone result;
537
538 result.set_arch(get_arch());
539 result.set_build_fingerprint(android::base::GetProperty("ro.build.fingerprint", "unknown"));
540 result.set_revision(android::base::GetProperty("ro.revision", "unknown"));
541 result.set_timestamp(get_timestamp());
542
Josh Gaodbb83de2021-03-01 23:13:13 -0800543 std::optional<uint64_t> system_uptime = read_uptime_secs();
544 if (system_uptime) {
545 android::procinfo::ProcessInfo proc_info;
546 std::string error;
547 if (android::procinfo::GetProcessInfo(target_thread, &proc_info, &error)) {
548 uint64_t starttime = proc_info.starttime / sysconf(_SC_CLK_TCK);
549 result.set_process_uptime(*system_uptime - starttime);
550 } else {
551 async_safe_format_log(ANDROID_LOG_ERROR, LOG_TAG, "failed to read process info: %s",
552 error.c_str());
553 }
554 } else {
555 async_safe_format_log(ANDROID_LOG_ERROR, LOG_TAG, "failed to read /proc/uptime: %s",
556 strerror(errno));
557 }
558
Josh Gao76e1e302021-01-26 15:53:11 -0800559 const ThreadInfo& main_thread = threads.at(target_thread);
560 result.set_pid(main_thread.pid);
561 result.set_tid(main_thread.tid);
562 result.set_uid(main_thread.uid);
563 result.set_selinux_label(main_thread.selinux_label);
564
Josh Gao31348a72021-03-29 21:53:42 -0700565 auto cmd_line = result.mutable_command_line();
566 for (const auto& arg : main_thread.command_line) {
567 *cmd_line->Add() = arg;
568 }
569
Josh Gao618cea32021-01-26 17:45:43 -0800570 if (!main_thread.siginfo) {
571 async_safe_fatal("siginfo missing");
572 }
Josh Gao76e1e302021-01-26 15:53:11 -0800573
574 Signal sig;
575 sig.set_number(main_thread.signo);
576 sig.set_name(get_signame(main_thread.siginfo));
577 sig.set_code(main_thread.siginfo->si_code);
578 sig.set_code_name(get_sigcode(main_thread.siginfo));
579
580 if (signal_has_sender(main_thread.siginfo, main_thread.pid)) {
581 sig.set_has_sender(true);
582 sig.set_sender_uid(main_thread.siginfo->si_uid);
583 sig.set_sender_pid(main_thread.siginfo->si_pid);
584 }
585
586 if (process_info.has_fault_address) {
587 sig.set_has_fault_address(true);
Peter Collingbourne1a1f7d72021-03-08 16:53:54 -0800588 sig.set_fault_address(process_info.maybe_tagged_fault_address);
Josh Gao76e1e302021-01-26 15:53:11 -0800589 }
590
591 *result.mutable_signal_info() = sig;
592
593 dump_abort_message(&result, unwinder, process_info);
594
595 dump_main_thread(&result, unwinder, main_thread);
596
597 for (const auto& [tid, thread_info] : threads) {
598 if (tid != target_thread) {
599 dump_thread(&result, unwinder, thread_info);
600 }
601 }
602
Peter Collingbourne1a1f7d72021-03-08 16:53:54 -0800603 dump_probable_cause(&result, unwinder, process_info, main_thread);
Josh Gao76e1e302021-01-26 15:53:11 -0800604
605 dump_mappings(&result, unwinder);
606
607 // Only dump logs on debuggable devices.
608 if (android::base::GetBoolProperty("ro.debuggable", false)) {
609 dump_logcat(&result, main_thread.pid);
610 }
611
612 dump_open_fds(&result, open_files);
613
614 *tombstone = std::move(result);
615}