blob: abd1f128796e828b0514b568b91e42cfa29bb79b [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.";
David Srbeckyd8ab48b2021-05-13 00:03:26 +0100105 } else if ((map_info->flags() & (PROT_READ | PROT_WRITE)) != (PROT_READ | PROT_WRITE)) {
Josh Gao76e1e302021-01-26 15:53:11 -0800106 return "stack pointer is not in a rw map; likely due to stack overflow.";
David Srbeckyd8ab48b2021-05-13 00:03:26 +0100107 } else if ((sp - map_info->start()) <= kMaxDifferenceBytes) {
Josh Gao76e1e302021-01-26 15:53:11 -0800108 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);
David Srbeckyd8ab48b2021-05-13 00:03:26 +0100224 if (map_info != nullptr && map_info->flags() == PROT_EXEC) {
Josh Gao76e1e302021-01-26 15:53:11 -0800225 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) {
David Srbeckyd8ab48b2021-05-13 00:03:26 +0100362 dump.set_mapping_name(map_info->name());
Josh Gao76e1e302021-01-26 15:53:11 -0800363 }
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 {
Christopher Ferrisfe751c52021-04-16 09:40:40 -0700398 if (unwinder->elf_from_memory_not_file()) {
399 auto backtrace_note = thread.mutable_backtrace_note();
400 *backtrace_note->Add() =
401 "Function names and BuildId information is missing for some frames due";
402 *backtrace_note->Add() =
403 "to unreadable libraries. For unwinds of apps, only shared libraries";
404 *backtrace_note->Add() = "found under the lib/ directory are readable.";
405 *backtrace_note->Add() = "On this device, run setenforce 0 to make the libraries readable.";
406 }
Josh Gao76e1e302021-01-26 15:53:11 -0800407 unwinder->SetDisplayBuildID(true);
408 for (const auto& frame : unwinder->frames()) {
409 BacktraceFrame* f = thread.add_current_backtrace();
Peter Collingbourne1a1f7d72021-03-08 16:53:54 -0800410 fill_in_backtrace_frame(f, frame, maps);
Josh Gao76e1e302021-01-26 15:53:11 -0800411 }
412 }
413
414 auto& threads = *tombstone->mutable_threads();
415 threads[thread_info.tid] = thread;
416}
417
418static void dump_main_thread(Tombstone* tombstone, unwindstack::Unwinder* unwinder,
419 const ThreadInfo& thread_info) {
420 dump_thread(tombstone, unwinder, thread_info, true);
421}
422
423static void dump_mappings(Tombstone* tombstone, unwindstack::Unwinder* unwinder) {
424 unwindstack::Maps* maps = unwinder->GetMaps();
425 std::shared_ptr<unwindstack::Memory> process_memory = unwinder->GetProcessMemory();
426
427 for (const auto& map_info : *maps) {
428 auto* map = tombstone->add_memory_mappings();
David Srbeckyd8ab48b2021-05-13 00:03:26 +0100429 map->set_begin_address(map_info->start());
430 map->set_end_address(map_info->end());
431 map->set_offset(map_info->offset());
Josh Gao76e1e302021-01-26 15:53:11 -0800432
David Srbeckyd8ab48b2021-05-13 00:03:26 +0100433 if (map_info->flags() & PROT_READ) {
Josh Gao76e1e302021-01-26 15:53:11 -0800434 map->set_read(true);
435 }
David Srbeckyd8ab48b2021-05-13 00:03:26 +0100436 if (map_info->flags() & PROT_WRITE) {
Josh Gao76e1e302021-01-26 15:53:11 -0800437 map->set_write(true);
438 }
David Srbeckyd8ab48b2021-05-13 00:03:26 +0100439 if (map_info->flags() & PROT_EXEC) {
Josh Gao76e1e302021-01-26 15:53:11 -0800440 map->set_execute(true);
441 }
442
David Srbeckyd8ab48b2021-05-13 00:03:26 +0100443 map->set_mapping_name(map_info->name());
Josh Gao76e1e302021-01-26 15:53:11 -0800444
445 std::string build_id = map_info->GetPrintableBuildID();
446 if (!build_id.empty()) {
447 map->set_build_id(build_id);
448 }
449
450 map->set_load_bias(map_info->GetLoadBias(process_memory));
451 }
452}
453
454static void dump_log_file(Tombstone* tombstone, const char* logger, pid_t pid) {
455 logger_list* logger_list =
456 android_logger_list_open(android_name_to_log_id(logger), ANDROID_LOG_NONBLOCK, 0, pid);
457
458 LogBuffer buffer;
459
460 while (true) {
461 log_msg log_entry;
462 ssize_t actual = android_logger_list_read(logger_list, &log_entry);
463
464 if (actual < 0) {
465 if (actual == -EINTR) {
466 // interrupted by signal, retry
467 continue;
468 }
469 if (actual == -EAGAIN) {
470 // non-blocking EOF; we're done
471 break;
472 } else {
Josh Gao76e1e302021-01-26 15:53:11 -0800473 break;
474 }
475 } else if (actual == 0) {
Josh Gao76e1e302021-01-26 15:53:11 -0800476 break;
477 }
478
479 char timestamp_secs[32];
480 time_t sec = static_cast<time_t>(log_entry.entry.sec);
481 tm tm;
482 localtime_r(&sec, &tm);
483 strftime(timestamp_secs, sizeof(timestamp_secs), "%m-%d %H:%M:%S", &tm);
484 std::string timestamp =
485 StringPrintf("%s.%03d", timestamp_secs, log_entry.entry.nsec / 1'000'000);
486
487 // Msg format is: <priority:1><tag:N>\0<message:N>\0
488 char* msg = log_entry.msg();
489 if (msg == nullptr) {
490 continue;
491 }
492
493 unsigned char prio = msg[0];
494 char* tag = msg + 1;
495 msg = tag + strlen(tag) + 1;
496
497 // consume any trailing newlines
498 char* nl = msg + strlen(msg) - 1;
499 while (nl >= msg && *nl == '\n') {
500 *nl-- = '\0';
501 }
502
503 // Look for line breaks ('\n') and display each text line
504 // on a separate line, prefixed with the header, like logcat does.
505 do {
506 nl = strchr(msg, '\n');
507 if (nl != nullptr) {
508 *nl = '\0';
509 ++nl;
510 }
511
512 LogMessage* log_msg = buffer.add_logs();
513 log_msg->set_timestamp(timestamp);
514 log_msg->set_pid(log_entry.entry.pid);
515 log_msg->set_tid(log_entry.entry.tid);
516 log_msg->set_priority(prio);
517 log_msg->set_tag(tag);
518 log_msg->set_message(msg);
519 } while ((msg = nl));
520 }
521 android_logger_list_free(logger_list);
522
523 if (!buffer.logs().empty()) {
524 buffer.set_name(logger);
525 *tombstone->add_log_buffers() = std::move(buffer);
526 }
527}
528
529static void dump_logcat(Tombstone* tombstone, pid_t pid) {
530 dump_log_file(tombstone, "system", pid);
531 dump_log_file(tombstone, "main", pid);
532}
533
Josh Gaodbb83de2021-03-01 23:13:13 -0800534static std::optional<uint64_t> read_uptime_secs() {
535 std::string uptime;
536 if (!android::base::ReadFileToString("/proc/uptime", &uptime)) {
537 return {};
538 }
539 return strtoll(uptime.c_str(), nullptr, 10);
540}
541
Josh Gao76e1e302021-01-26 15:53:11 -0800542void engrave_tombstone_proto(Tombstone* tombstone, unwindstack::Unwinder* unwinder,
543 const std::map<pid_t, ThreadInfo>& threads, pid_t target_thread,
544 const ProcessInfo& process_info, const OpenFilesList* open_files) {
545 Tombstone result;
546
547 result.set_arch(get_arch());
548 result.set_build_fingerprint(android::base::GetProperty("ro.build.fingerprint", "unknown"));
549 result.set_revision(android::base::GetProperty("ro.revision", "unknown"));
550 result.set_timestamp(get_timestamp());
551
Josh Gaodbb83de2021-03-01 23:13:13 -0800552 std::optional<uint64_t> system_uptime = read_uptime_secs();
553 if (system_uptime) {
554 android::procinfo::ProcessInfo proc_info;
555 std::string error;
556 if (android::procinfo::GetProcessInfo(target_thread, &proc_info, &error)) {
557 uint64_t starttime = proc_info.starttime / sysconf(_SC_CLK_TCK);
558 result.set_process_uptime(*system_uptime - starttime);
559 } else {
560 async_safe_format_log(ANDROID_LOG_ERROR, LOG_TAG, "failed to read process info: %s",
561 error.c_str());
562 }
563 } else {
564 async_safe_format_log(ANDROID_LOG_ERROR, LOG_TAG, "failed to read /proc/uptime: %s",
565 strerror(errno));
566 }
567
Josh Gao76e1e302021-01-26 15:53:11 -0800568 const ThreadInfo& main_thread = threads.at(target_thread);
569 result.set_pid(main_thread.pid);
570 result.set_tid(main_thread.tid);
571 result.set_uid(main_thread.uid);
572 result.set_selinux_label(main_thread.selinux_label);
573
Josh Gao31348a72021-03-29 21:53:42 -0700574 auto cmd_line = result.mutable_command_line();
575 for (const auto& arg : main_thread.command_line) {
576 *cmd_line->Add() = arg;
577 }
578
Josh Gao618cea32021-01-26 17:45:43 -0800579 if (!main_thread.siginfo) {
580 async_safe_fatal("siginfo missing");
581 }
Josh Gao76e1e302021-01-26 15:53:11 -0800582
583 Signal sig;
584 sig.set_number(main_thread.signo);
585 sig.set_name(get_signame(main_thread.siginfo));
586 sig.set_code(main_thread.siginfo->si_code);
587 sig.set_code_name(get_sigcode(main_thread.siginfo));
588
589 if (signal_has_sender(main_thread.siginfo, main_thread.pid)) {
590 sig.set_has_sender(true);
591 sig.set_sender_uid(main_thread.siginfo->si_uid);
592 sig.set_sender_pid(main_thread.siginfo->si_pid);
593 }
594
595 if (process_info.has_fault_address) {
596 sig.set_has_fault_address(true);
Peter Collingbourne1a1f7d72021-03-08 16:53:54 -0800597 sig.set_fault_address(process_info.maybe_tagged_fault_address);
Josh Gao76e1e302021-01-26 15:53:11 -0800598 }
599
600 *result.mutable_signal_info() = sig;
601
602 dump_abort_message(&result, unwinder, process_info);
603
604 dump_main_thread(&result, unwinder, main_thread);
605
606 for (const auto& [tid, thread_info] : threads) {
607 if (tid != target_thread) {
608 dump_thread(&result, unwinder, thread_info);
609 }
610 }
611
Peter Collingbourne1a1f7d72021-03-08 16:53:54 -0800612 dump_probable_cause(&result, unwinder, process_info, main_thread);
Josh Gao76e1e302021-01-26 15:53:11 -0800613
614 dump_mappings(&result, unwinder);
615
616 // Only dump logs on debuggable devices.
617 if (android::base::GetBoolProperty("ro.debuggable", false)) {
618 dump_logcat(&result, main_thread.pid);
619 }
620
621 dump_open_fds(&result, open_files);
622
623 *tombstone = std::move(result);
624}