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