diff options
Diffstat (limited to 'debuggerd/libdebuggerd')
30 files changed, 3935 insertions, 0 deletions
diff --git a/debuggerd/libdebuggerd/arm/machine.cpp b/debuggerd/libdebuggerd/arm/machine.cpp new file mode 100644 index 000000000..78c230615 --- /dev/null +++ b/debuggerd/libdebuggerd/arm/machine.cpp @@ -0,0 +1,83 @@ +/* + * + * Copyright 2006, The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#define LOG_TAG "DEBUG" + +#include <errno.h> +#include <stdint.h> +#include <string.h> +#include <sys/ptrace.h> + +#include <backtrace/Backtrace.h> +#include <log/log.h> + +#include "machine.h" +#include "utility.h" + +void dump_memory_and_code(log_t* log, Backtrace* backtrace) { + pt_regs regs; + if (ptrace(PTRACE_GETREGS, backtrace->Tid(), 0, ®s)) { + ALOGE("cannot get registers: %s\n", strerror(errno)); + return; + } + + static const char reg_names[] = "r0r1r2r3r4r5r6r7r8r9slfpipsp"; + + for (int reg = 0; reg < 14; reg++) { + dump_memory(log, backtrace, regs.uregs[reg], "memory near %.2s:", ®_names[reg * 2]); + } + + dump_memory(log, backtrace, static_cast<uintptr_t>(regs.ARM_pc), "code around pc:"); + + if (regs.ARM_pc != regs.ARM_lr) { + dump_memory(log, backtrace, static_cast<uintptr_t>(regs.ARM_lr), "code around lr:"); + } +} + +void dump_registers(log_t* log, pid_t tid) { + pt_regs r; + if (ptrace(PTRACE_GETREGS, tid, 0, &r)) { + ALOGE("cannot get registers: %s\n", strerror(errno)); + return; + } + + _LOG(log, logtype::REGISTERS, " r0 %08x r1 %08x r2 %08x r3 %08x\n", + static_cast<uint32_t>(r.ARM_r0), static_cast<uint32_t>(r.ARM_r1), + static_cast<uint32_t>(r.ARM_r2), static_cast<uint32_t>(r.ARM_r3)); + _LOG(log, logtype::REGISTERS, " r4 %08x r5 %08x r6 %08x r7 %08x\n", + static_cast<uint32_t>(r.ARM_r4), static_cast<uint32_t>(r.ARM_r5), + static_cast<uint32_t>(r.ARM_r6), static_cast<uint32_t>(r.ARM_r7)); + _LOG(log, logtype::REGISTERS, " r8 %08x r9 %08x sl %08x fp %08x\n", + static_cast<uint32_t>(r.ARM_r8), static_cast<uint32_t>(r.ARM_r9), + static_cast<uint32_t>(r.ARM_r10), static_cast<uint32_t>(r.ARM_fp)); + _LOG(log, logtype::REGISTERS, " ip %08x sp %08x lr %08x pc %08x cpsr %08x\n", + static_cast<uint32_t>(r.ARM_ip), static_cast<uint32_t>(r.ARM_sp), + static_cast<uint32_t>(r.ARM_lr), static_cast<uint32_t>(r.ARM_pc), + static_cast<uint32_t>(r.ARM_cpsr)); + + user_vfp vfp_regs; + if (ptrace(PTRACE_GETVFPREGS, tid, 0, &vfp_regs)) { + ALOGE("cannot get FP registers: %s\n", strerror(errno)); + return; + } + + for (size_t i = 0; i < 32; i += 2) { + _LOG(log, logtype::FP_REGISTERS, " d%-2d %016llx d%-2d %016llx\n", + i, vfp_regs.fpregs[i], i+1, vfp_regs.fpregs[i+1]); + } + _LOG(log, logtype::FP_REGISTERS, " scr %08lx\n", vfp_regs.fpscr); +} diff --git a/debuggerd/libdebuggerd/arm64/machine.cpp b/debuggerd/libdebuggerd/arm64/machine.cpp new file mode 100644 index 000000000..e7bf79a8f --- /dev/null +++ b/debuggerd/libdebuggerd/arm64/machine.cpp @@ -0,0 +1,101 @@ +/* + * + * Copyright 2014, The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#define LOG_TAG "DEBUG" + +#include <elf.h> +#include <errno.h> +#include <stdint.h> +#include <string.h> +#include <sys/ptrace.h> +#include <sys/uio.h> + +#include <backtrace/Backtrace.h> +#include <log/log.h> + +#include "machine.h" +#include "utility.h" + +void dump_memory_and_code(log_t* log, Backtrace* backtrace) { + struct user_pt_regs regs; + struct iovec io; + io.iov_base = ®s; + io.iov_len = sizeof(regs); + + if (ptrace(PTRACE_GETREGSET, backtrace->Tid(), reinterpret_cast<void*>(NT_PRSTATUS), &io) == -1) { + ALOGE("ptrace failed to get registers: %s", strerror(errno)); + return; + } + + for (int reg = 0; reg < 31; reg++) { + dump_memory(log, backtrace, regs.regs[reg], "memory near x%d:", reg); + } + + dump_memory(log, backtrace, static_cast<uintptr_t>(regs.pc), "code around pc:"); + + if (regs.pc != regs.sp) { + dump_memory(log, backtrace, static_cast<uintptr_t>(regs.sp), "code around sp:"); + } +} + +void dump_registers(log_t* log, pid_t tid) { + struct user_pt_regs r; + struct iovec io; + io.iov_base = &r; + io.iov_len = sizeof(r); + + if (ptrace(PTRACE_GETREGSET, tid, (void*) NT_PRSTATUS, (void*) &io) == -1) { + ALOGE("ptrace error: %s\n", strerror(errno)); + return; + } + + for (int i = 0; i < 28; i += 4) { + _LOG(log, logtype::REGISTERS, + " x%-2d %016llx x%-2d %016llx x%-2d %016llx x%-2d %016llx\n", + i, r.regs[i], + i+1, r.regs[i+1], + i+2, r.regs[i+2], + i+3, r.regs[i+3]); + } + + _LOG(log, logtype::REGISTERS, " x28 %016llx x29 %016llx x30 %016llx\n", + r.regs[28], r.regs[29], r.regs[30]); + + _LOG(log, logtype::REGISTERS, " sp %016llx pc %016llx pstate %016llx\n", + r.sp, r.pc, r.pstate); + + struct user_fpsimd_state f; + io.iov_base = &f; + io.iov_len = sizeof(f); + + if (ptrace(PTRACE_GETREGSET, tid, (void*) NT_PRFPREG, (void*) &io) == -1) { + ALOGE("ptrace error: %s\n", strerror(errno)); + return; + } + + for (int i = 0; i < 32; i += 2) { + _LOG(log, logtype::FP_REGISTERS, + " v%-2d %016" PRIx64 "%016" PRIx64 " v%-2d %016" PRIx64 "%016" PRIx64 "\n", + i, + static_cast<uint64_t>(f.vregs[i] >> 64), + static_cast<uint64_t>(f.vregs[i]), + i+1, + static_cast<uint64_t>(f.vregs[i+1] >> 64), + static_cast<uint64_t>(f.vregs[i+1])); + } + _LOG(log, logtype::FP_REGISTERS, " fpsr %08x fpcr %08x\n", f.fpsr, f.fpcr); +} diff --git a/debuggerd/libdebuggerd/backtrace.cpp b/debuggerd/libdebuggerd/backtrace.cpp new file mode 100644 index 000000000..066444242 --- /dev/null +++ b/debuggerd/libdebuggerd/backtrace.cpp @@ -0,0 +1,119 @@ +/* + * Copyright (C) 2012 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#define LOG_TAG "DEBUG" + +#include <errno.h> +#include <dirent.h> +#include <limits.h> +#include <stddef.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/ptrace.h> +#include <sys/types.h> +#include <time.h> +#include <unistd.h> + +#include <memory> +#include <string> + +#include <backtrace/Backtrace.h> +#include <log/log.h> + +#include "backtrace.h" + +#include "utility.h" + +static void dump_process_header(log_t* log, pid_t pid) { + char path[PATH_MAX]; + char procnamebuf[1024]; + char* procname = NULL; + FILE* fp; + + snprintf(path, sizeof(path), "/proc/%d/cmdline", pid); + if ((fp = fopen(path, "r"))) { + procname = fgets(procnamebuf, sizeof(procnamebuf), fp); + fclose(fp); + } + + time_t t = time(NULL); + struct tm tm; + localtime_r(&t, &tm); + char timestr[64]; + strftime(timestr, sizeof(timestr), "%F %T", &tm); + _LOG(log, logtype::BACKTRACE, "\n\n----- pid %d at %s -----\n", pid, timestr); + + if (procname) { + _LOG(log, logtype::BACKTRACE, "Cmd line: %s\n", procname); + } + _LOG(log, logtype::BACKTRACE, "ABI: '%s'\n", ABI_STRING); +} + +static void dump_process_footer(log_t* log, pid_t pid) { + _LOG(log, logtype::BACKTRACE, "\n----- end %d -----\n", pid); +} + +static void dump_thread(log_t* log, BacktraceMap* map, pid_t pid, pid_t tid) { + char path[PATH_MAX]; + char threadnamebuf[1024]; + char* threadname = NULL; + FILE* fp; + + snprintf(path, sizeof(path), "/proc/%d/comm", tid); + if ((fp = fopen(path, "r"))) { + threadname = fgets(threadnamebuf, sizeof(threadnamebuf), fp); + fclose(fp); + if (threadname) { + size_t len = strlen(threadname); + if (len && threadname[len - 1] == '\n') { + threadname[len - 1] = '\0'; + } + } + } + + _LOG(log, logtype::BACKTRACE, "\n\"%s\" sysTid=%d\n", threadname ? threadname : "<unknown>", tid); + + std::unique_ptr<Backtrace> backtrace(Backtrace::Create(pid, tid, map)); + if (backtrace->Unwind(0)) { + dump_backtrace_to_log(backtrace.get(), log, " "); + } else { + ALOGE("Unwind failed: tid = %d: %s", tid, + backtrace->GetErrorString(backtrace->GetError()).c_str()); + } +} + +void dump_backtrace(int fd, BacktraceMap* map, pid_t pid, pid_t tid, + const std::set<pid_t>& siblings, std::string* amfd_data) { + log_t log; + log.tfd = fd; + log.amfd_data = amfd_data; + + dump_process_header(&log, pid); + dump_thread(&log, map, pid, tid); + + for (pid_t sibling : siblings) { + dump_thread(&log, map, pid, sibling); + } + + dump_process_footer(&log, pid); +} + +void dump_backtrace_to_log(Backtrace* backtrace, log_t* log, const char* prefix) { + for (size_t i = 0; i < backtrace->NumFrames(); i++) { + _LOG(log, logtype::BACKTRACE, "%s%s\n", prefix, backtrace->FormatFrameData(i).c_str()); + } +} diff --git a/debuggerd/libdebuggerd/elf_utils.cpp b/debuggerd/libdebuggerd/elf_utils.cpp new file mode 100644 index 000000000..4e798e243 --- /dev/null +++ b/debuggerd/libdebuggerd/elf_utils.cpp @@ -0,0 +1,121 @@ +/* + * Copyright (C) 2015 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#define LOG_TAG "DEBUG" + +#include <elf.h> +#include <stdint.h> +#include <stdlib.h> +#include <string.h> + +#include <string> + +#include <android-base/stringprintf.h> +#include <backtrace/Backtrace.h> +#include <log/log.h> + +#include "elf_utils.h" + +#define NOTE_ALIGN(size) (((size) + 3) & ~3) + +template <typename HdrType, typename PhdrType, typename NhdrType> +static bool get_build_id( + Backtrace* backtrace, uintptr_t base_addr, uint8_t* e_ident, std::string* build_id) { + HdrType hdr; + + memcpy(&hdr.e_ident[0], e_ident, EI_NIDENT); + + // First read the rest of the header. + if (backtrace->Read(base_addr + EI_NIDENT, reinterpret_cast<uint8_t*>(&hdr) + EI_NIDENT, + sizeof(HdrType) - EI_NIDENT) != sizeof(HdrType) - EI_NIDENT) { + return false; + } + + for (size_t i = 0; i < hdr.e_phnum; i++) { + PhdrType phdr; + if (backtrace->Read(base_addr + hdr.e_phoff + i * hdr.e_phentsize, + reinterpret_cast<uint8_t*>(&phdr), sizeof(phdr)) != sizeof(phdr)) { + return false; + } + // Looking for the .note.gnu.build-id note. + if (phdr.p_type == PT_NOTE) { + size_t hdr_size = phdr.p_filesz; + uintptr_t addr = base_addr + phdr.p_offset; + while (hdr_size >= sizeof(NhdrType)) { + NhdrType nhdr; + if (backtrace->Read(addr, reinterpret_cast<uint8_t*>(&nhdr), sizeof(nhdr)) != sizeof(nhdr)) { + return false; + } + addr += sizeof(nhdr); + if (nhdr.n_type == NT_GNU_BUILD_ID) { + // Skip the name (which is the owner and should be "GNU"). + addr += NOTE_ALIGN(nhdr.n_namesz); + uint8_t build_id_data[160]; + if (nhdr.n_descsz > sizeof(build_id_data)) { + ALOGE("Possible corrupted note, desc size value is too large: %u", + nhdr.n_descsz); + return false; + } + if (backtrace->Read(addr, build_id_data, nhdr.n_descsz) != nhdr.n_descsz) { + return false; + } + + build_id->clear(); + for (size_t bytes = 0; bytes < nhdr.n_descsz; bytes++) { + *build_id += android::base::StringPrintf("%02x", build_id_data[bytes]); + } + + return true; + } else { + // Move past the extra note data. + hdr_size -= sizeof(nhdr); + size_t skip_bytes = NOTE_ALIGN(nhdr.n_namesz) + NOTE_ALIGN(nhdr.n_descsz); + addr += skip_bytes; + if (hdr_size < skip_bytes) { + break; + } + hdr_size -= skip_bytes; + } + } + } + } + return false; +} + +bool elf_get_build_id(Backtrace* backtrace, uintptr_t addr, std::string* build_id) { + // Read and verify the elf magic number first. + uint8_t e_ident[EI_NIDENT]; + if (backtrace->Read(addr, e_ident, SELFMAG) != SELFMAG) { + return false; + } + + if (memcmp(e_ident, ELFMAG, SELFMAG) != 0) { + return false; + } + + // Read the rest of EI_NIDENT. + if (backtrace->Read(addr + SELFMAG, e_ident + SELFMAG, EI_NIDENT - SELFMAG) != EI_NIDENT - SELFMAG) { + return false; + } + + if (e_ident[EI_CLASS] == ELFCLASS32) { + return get_build_id<Elf32_Ehdr, Elf32_Phdr, Elf32_Nhdr>(backtrace, addr, e_ident, build_id); + } else if (e_ident[EI_CLASS] == ELFCLASS64) { + return get_build_id<Elf64_Ehdr, Elf64_Phdr, Elf64_Nhdr>(backtrace, addr, e_ident, build_id); + } + + return false; +} diff --git a/debuggerd/libdebuggerd/include/backtrace.h b/debuggerd/libdebuggerd/include/backtrace.h new file mode 100644 index 000000000..acd5eaac6 --- /dev/null +++ b/debuggerd/libdebuggerd/include/backtrace.h @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2012 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef _DEBUGGERD_BACKTRACE_H +#define _DEBUGGERD_BACKTRACE_H + +#include <sys/types.h> + +#include <set> +#include <string> + +#include "utility.h" + +class Backtrace; +class BacktraceMap; + +// Dumps a backtrace using a format similar to what Dalvik uses so that the result +// can be intermixed in a bug report. +void dump_backtrace(int fd, BacktraceMap* map, pid_t pid, pid_t tid, + const std::set<pid_t>& siblings, std::string* amfd_data); + +/* Dumps the backtrace in the backtrace data structure to the log. */ +void dump_backtrace_to_log(Backtrace* backtrace, log_t* log, const char* prefix); + +#endif // _DEBUGGERD_BACKTRACE_H diff --git a/debuggerd/libdebuggerd/include/elf_utils.h b/debuggerd/libdebuggerd/include/elf_utils.h new file mode 100644 index 000000000..11d0a4348 --- /dev/null +++ b/debuggerd/libdebuggerd/include/elf_utils.h @@ -0,0 +1,27 @@ +/* + * Copyright (C) 2015 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef _DEBUGGERD_ELF_UTILS_H +#define _DEBUGGERD_ELF_UTILS_H + +#include <stdint.h> +#include <string> + +class Backtrace; + +bool elf_get_build_id(Backtrace*, uintptr_t, std::string*); + +#endif // _DEBUGGERD_ELF_UTILS_H diff --git a/debuggerd/libdebuggerd/include/machine.h b/debuggerd/libdebuggerd/include/machine.h new file mode 100644 index 000000000..e65b147f6 --- /dev/null +++ b/debuggerd/libdebuggerd/include/machine.h @@ -0,0 +1,29 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef _DEBUGGERD_MACHINE_H +#define _DEBUGGERD_MACHINE_H + +#include <sys/types.h> + +#include <backtrace/Backtrace.h> + +#include "utility.h" + +void dump_memory_and_code(log_t* log, Backtrace* backtrace); +void dump_registers(log_t* log, pid_t tid); + +#endif // _DEBUGGERD_MACHINE_H diff --git a/debuggerd/libdebuggerd/include/open_files_list.h b/debuggerd/libdebuggerd/include/open_files_list.h new file mode 100644 index 000000000..b37228d03 --- /dev/null +++ b/debuggerd/libdebuggerd/include/open_files_list.h @@ -0,0 +1,36 @@ +/* + * Copyright (C) 2016 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef _DEBUGGERD_OPEN_FILES_LIST_H +#define _DEBUGGERD_OPEN_FILES_LIST_H + +#include <sys/types.h> + +#include <string> +#include <utility> +#include <vector> + +#include "utility.h" + +typedef std::vector<std::pair<int, std::string>> OpenFilesList; + +/* Populates the given list with open files for the given process. */ +void populate_open_files_list(pid_t pid, OpenFilesList* list); + +/* Dumps the open files list to the log. */ +void dump_open_files_list_to_log(const OpenFilesList& files, log_t* log, const char* prefix); + +#endif // _DEBUGGERD_OPEN_FILES_LIST_H diff --git a/debuggerd/libdebuggerd/include/tombstone.h b/debuggerd/libdebuggerd/include/tombstone.h new file mode 100644 index 000000000..4ff24af26 --- /dev/null +++ b/debuggerd/libdebuggerd/include/tombstone.h @@ -0,0 +1,42 @@ +/* + * Copyright (C) 2012 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef _DEBUGGERD_TOMBSTONE_H +#define _DEBUGGERD_TOMBSTONE_H + +#include <stdbool.h> +#include <stddef.h> +#include <sys/types.h> +#include <set> +#include <string> + +#include "open_files_list.h" + +class BacktraceMap; + +/* Create and open a tombstone file for writing. + * Returns a writable file descriptor, or -1 with errno set appropriately. + * If out_path is non-null, *out_path is set to the path of the tombstone file. + */ +int open_tombstone(std::string* path); + +/* Creates a tombstone file and writes the crash dump to it. */ +void engrave_tombstone(int tombstone_fd, BacktraceMap* map, + const OpenFilesList& open_files, pid_t pid, pid_t tid, + const std::set<pid_t>& siblings, uintptr_t abort_msg_address, + std::string* amfd_data); + +#endif // _DEBUGGERD_TOMBSTONE_H diff --git a/debuggerd/libdebuggerd/include/utility.h b/debuggerd/libdebuggerd/include/utility.h new file mode 100644 index 000000000..bbc45468b --- /dev/null +++ b/debuggerd/libdebuggerd/include/utility.h @@ -0,0 +1,86 @@ +/* system/debuggerd/utility.h +** +** Copyright 2008, The Android Open Source Project +** +** Licensed under the Apache License, Version 2.0 (the "License"); +** you may not use this file except in compliance with the License. +** You may obtain a copy of the License at +** +** http://www.apache.org/licenses/LICENSE-2.0 +** +** Unless required by applicable law or agreed to in writing, software +** distributed under the License is distributed on an "AS IS" BASIS, +** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +** See the License for the specific language governing permissions and +** limitations under the License. +*/ + +#ifndef _DEBUGGERD_UTILITY_H +#define _DEBUGGERD_UTILITY_H + +#include <signal.h> +#include <stdbool.h> +#include <sys/types.h> + +#include <string> + +#include <backtrace/Backtrace.h> + +// Figure out the abi based on defined macros. +#if defined(__arm__) +#define ABI_STRING "arm" +#elif defined(__aarch64__) +#define ABI_STRING "arm64" +#elif defined(__mips__) && !defined(__LP64__) +#define ABI_STRING "mips" +#elif defined(__mips__) && defined(__LP64__) +#define ABI_STRING "mips64" +#elif defined(__i386__) +#define ABI_STRING "x86" +#elif defined(__x86_64__) +#define ABI_STRING "x86_64" +#else +#error "Unsupported ABI" +#endif + + +struct log_t{ + // Tombstone file descriptor. + int tfd; + // Data to be sent to the Activity Manager. + std::string* amfd_data; + // The tid of the thread that crashed. + pid_t crashed_tid; + // The tid of the thread we are currently working with. + pid_t current_tid; + // logd daemon crash, can block asking for logcat data, allow suppression. + bool should_retrieve_logcat; + + log_t() + : tfd(-1), amfd_data(nullptr), crashed_tid(-1), current_tid(-1), + should_retrieve_logcat(true) {} +}; + +// List of types of logs to simplify the logging decision in _LOG +enum logtype { + HEADER, + THREAD, + REGISTERS, + FP_REGISTERS, + BACKTRACE, + MAPS, + MEMORY, + STACK, + LOGS, + OPEN_FILES +}; + +// Log information onto the tombstone. +void _LOG(log_t* log, logtype ltype, const char *fmt, ...) + __attribute__ ((format(printf, 3, 4))); + +bool wait_for_signal(pid_t tid, siginfo_t* siginfo); + +void dump_memory(log_t* log, Backtrace* backtrace, uintptr_t addr, const char* fmt, ...); + +#endif // _DEBUGGERD_UTILITY_H diff --git a/debuggerd/libdebuggerd/mips/machine.cpp b/debuggerd/libdebuggerd/mips/machine.cpp new file mode 100644 index 000000000..cbf272a36 --- /dev/null +++ b/debuggerd/libdebuggerd/mips/machine.cpp @@ -0,0 +1,98 @@ +/* + * Copyright 2012, The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#define LOG_TAG "DEBUG" + +#include <errno.h> +#include <inttypes.h> +#include <stdint.h> +#include <string.h> +#include <sys/ptrace.h> + +#include <backtrace/Backtrace.h> +#include <log/log.h> + +#include "machine.h" +#include "utility.h" + +#define R(x) (static_cast<uintptr_t>(x)) + +// If configured to do so, dump memory around *all* registers +// for the crashing thread. +void dump_memory_and_code(log_t* log, Backtrace* backtrace) { + pt_regs r; + if (ptrace(PTRACE_GETREGS, backtrace->Tid(), 0, &r)) { + ALOGE("cannot get registers: %s\n", strerror(errno)); + return; + } + + static const char reg_names[] = "$0atv0v1a0a1a2a3t0t1t2t3t4t5t6t7s0s1s2s3s4s5s6s7t8t9k0k1gpsps8ra"; + + for (int reg = 0; reg < 32; reg++) { + // skip uninteresting registers + if (reg == 0 // $0 + || reg == 26 // $k0 + || reg == 27 // $k1 + || reg == 31 // $ra (done below) + ) + continue; + + dump_memory(log, backtrace, R(r.regs[reg]), "memory near %.2s:", ®_names[reg * 2]); + } + + uintptr_t pc = R(r.cp0_epc); + uintptr_t ra = R(r.regs[31]); + dump_memory(log, backtrace, pc, "code around pc:"); + if (pc != ra) { + dump_memory(log, backtrace, ra, "code around ra:"); + } +} + +void dump_registers(log_t* log, pid_t tid) { + pt_regs r; + if(ptrace(PTRACE_GETREGS, tid, 0, &r)) { + ALOGE("cannot get registers: %s\n", strerror(errno)); + return; + } + + _LOG(log, logtype::REGISTERS, " zr %08" PRIxPTR " at %08" PRIxPTR + " v0 %08" PRIxPTR " v1 %08" PRIxPTR "\n", + R(r.regs[0]), R(r.regs[1]), R(r.regs[2]), R(r.regs[3])); + _LOG(log, logtype::REGISTERS, " a0 %08" PRIxPTR " a1 %08" PRIxPTR + " a2 %08" PRIxPTR " a3 %08" PRIxPTR "\n", + R(r.regs[4]), R(r.regs[5]), R(r.regs[6]), R(r.regs[7])); + _LOG(log, logtype::REGISTERS, " t0 %08" PRIxPTR " t1 %08" PRIxPTR + " t2 %08" PRIxPTR " t3 %08" PRIxPTR "\n", + R(r.regs[8]), R(r.regs[9]), R(r.regs[10]), R(r.regs[11])); + _LOG(log, logtype::REGISTERS, " t4 %08" PRIxPTR " t5 %08" PRIxPTR + " t6 %08" PRIxPTR " t7 %08" PRIxPTR "\n", + R(r.regs[12]), R(r.regs[13]), R(r.regs[14]), R(r.regs[15])); + _LOG(log, logtype::REGISTERS, " s0 %08" PRIxPTR " s1 %08" PRIxPTR + " s2 %08" PRIxPTR " s3 %08" PRIxPTR "\n", + R(r.regs[16]), R(r.regs[17]), R(r.regs[18]), R(r.regs[19])); + _LOG(log, logtype::REGISTERS, " s4 %08" PRIxPTR " s5 %08" PRIxPTR + " s6 %08" PRIxPTR " s7 %08" PRIxPTR "\n", + R(r.regs[20]), R(r.regs[21]), R(r.regs[22]), R(r.regs[23])); + _LOG(log, logtype::REGISTERS, " t8 %08" PRIxPTR " t9 %08" PRIxPTR + " k0 %08" PRIxPTR " k1 %08" PRIxPTR "\n", + R(r.regs[24]), R(r.regs[25]), R(r.regs[26]), R(r.regs[27])); + _LOG(log, logtype::REGISTERS, " gp %08" PRIxPTR " sp %08" PRIxPTR + " s8 %08" PRIxPTR " ra %08" PRIxPTR "\n", + R(r.regs[28]), R(r.regs[29]), R(r.regs[30]), R(r.regs[31])); + _LOG(log, logtype::REGISTERS, " hi %08" PRIxPTR " lo %08" PRIxPTR + " bva %08" PRIxPTR " epc %08" PRIxPTR "\n", + R(r.hi), R(r.lo), R(r.cp0_badvaddr), R(r.cp0_epc)); +} diff --git a/debuggerd/libdebuggerd/mips64/machine.cpp b/debuggerd/libdebuggerd/mips64/machine.cpp new file mode 100644 index 000000000..0a8d53257 --- /dev/null +++ b/debuggerd/libdebuggerd/mips64/machine.cpp @@ -0,0 +1,98 @@ +/* + * Copyright 2014, The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#define LOG_TAG "DEBUG" + +#include <errno.h> +#include <inttypes.h> +#include <stdint.h> +#include <string.h> +#include <sys/ptrace.h> + +#include <backtrace/Backtrace.h> +#include <log/log.h> + +#include "machine.h" +#include "utility.h" + +#define R(x) (static_cast<uintptr_t>(x)) + +// If configured to do so, dump memory around *all* registers +// for the crashing thread. +void dump_memory_and_code(log_t* log, Backtrace* backtrace) { + pt_regs r; + if (ptrace(PTRACE_GETREGS, backtrace->Tid(), 0, &r)) { + ALOGE("cannot get registers: %s\n", strerror(errno)); + return; + } + + static const char reg_names[] = "$0atv0v1a0a1a2a3a4a5a6a7t0t1t2t3s0s1s2s3s4s5s6s7t8t9k0k1gpsps8ra"; + + for (int reg = 0; reg < 32; reg++) { + // skip uninteresting registers + if (reg == 0 // $0 + || reg == 26 // $k0 + || reg == 27 // $k1 + || reg == 31 // $ra (done below) + ) + continue; + + dump_memory(log, backtrace, R(r.regs[reg]), "memory near %.2s:", ®_names[reg * 2]); + } + + uintptr_t pc = R(r.cp0_epc); + uintptr_t ra = R(r.regs[31]); + dump_memory(log, backtrace, pc, "code around pc:"); + if (pc != ra) { + dump_memory(log, backtrace, ra, "code around ra:"); + } +} + +void dump_registers(log_t* log, pid_t tid) { + pt_regs r; + if(ptrace(PTRACE_GETREGS, tid, 0, &r)) { + ALOGE("cannot get registers: %s\n", strerror(errno)); + return; + } + + _LOG(log, logtype::REGISTERS, " zr %016" PRIxPTR " at %016" PRIxPTR + " v0 %016" PRIxPTR " v1 %016" PRIxPTR "\n", + R(r.regs[0]), R(r.regs[1]), R(r.regs[2]), R(r.regs[3])); + _LOG(log, logtype::REGISTERS, " a0 %016" PRIxPTR " a1 %016" PRIxPTR + " a2 %016" PRIxPTR " a3 %016" PRIxPTR "\n", + R(r.regs[4]), R(r.regs[5]), R(r.regs[6]), R(r.regs[7])); + _LOG(log, logtype::REGISTERS, " a4 %016" PRIxPTR " a5 %016" PRIxPTR + " a6 %016" PRIxPTR " a7 %016" PRIxPTR "\n", + R(r.regs[8]), R(r.regs[9]), R(r.regs[10]), R(r.regs[11])); + _LOG(log, logtype::REGISTERS, " t0 %016" PRIxPTR " t1 %016" PRIxPTR + " t2 %016" PRIxPTR " t3 %016" PRIxPTR "\n", + R(r.regs[12]), R(r.regs[13]), R(r.regs[14]), R(r.regs[15])); + _LOG(log, logtype::REGISTERS, " s0 %016" PRIxPTR " s1 %016" PRIxPTR + " s2 %016" PRIxPTR " s3 %016" PRIxPTR "\n", + R(r.regs[16]), R(r.regs[17]), R(r.regs[18]), R(r.regs[19])); + _LOG(log, logtype::REGISTERS, " s4 %016" PRIxPTR " s5 %016" PRIxPTR + " s6 %016" PRIxPTR " s7 %016" PRIxPTR "\n", + R(r.regs[20]), R(r.regs[21]), R(r.regs[22]), R(r.regs[23])); + _LOG(log, logtype::REGISTERS, " t8 %016" PRIxPTR " t9 %016" PRIxPTR + " k0 %016" PRIxPTR " k1 %016" PRIxPTR "\n", + R(r.regs[24]), R(r.regs[25]), R(r.regs[26]), R(r.regs[27])); + _LOG(log, logtype::REGISTERS, " gp %016" PRIxPTR " sp %016" PRIxPTR + " s8 %016" PRIxPTR " ra %016" PRIxPTR "\n", + R(r.regs[28]), R(r.regs[29]), R(r.regs[30]), R(r.regs[31])); + _LOG(log, logtype::REGISTERS, " hi %016" PRIxPTR " lo %016" PRIxPTR + " bva %016" PRIxPTR " epc %016" PRIxPTR "\n", + R(r.hi), R(r.lo), R(r.cp0_badvaddr), R(r.cp0_epc)); +} diff --git a/debuggerd/libdebuggerd/open_files_list.cpp b/debuggerd/libdebuggerd/open_files_list.cpp new file mode 100644 index 000000000..5c7ea7083 --- /dev/null +++ b/debuggerd/libdebuggerd/open_files_list.cpp @@ -0,0 +1,69 @@ +/* + * Copyright (C) 2016 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#define LOG_TAG "DEBUG" + +#include <dirent.h> +#include <errno.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/types.h> +#include <unistd.h> + +#include <string> +#include <utility> +#include <vector> + +#include <android-base/file.h> +#include <log/log.h> + +#include "open_files_list.h" + +#include "utility.h" + +void populate_open_files_list(pid_t pid, OpenFilesList* list) { + std::string fd_dir_name = "/proc/" + std::to_string(pid) + "/fd"; + std::unique_ptr<DIR, int (*)(DIR*)> dir(opendir(fd_dir_name.c_str()), closedir); + if (dir == nullptr) { + ALOGE("failed to open directory %s: %s", fd_dir_name.c_str(), strerror(errno)); + return; + } + + struct dirent* de; + while ((de = readdir(dir.get())) != nullptr) { + if (*de->d_name == '.') { + continue; + } + + int fd = atoi(de->d_name); + std::string path = fd_dir_name + "/" + std::string(de->d_name); + std::string target; + if (android::base::Readlink(path, &target)) { + list->emplace_back(fd, target); + } else { + ALOGE("failed to readlink %s: %s", path.c_str(), strerror(errno)); + list->emplace_back(fd, "???"); + } + } +} + +void dump_open_files_list_to_log(const OpenFilesList& files, log_t* log, const char* prefix) { + for (auto& file : files) { + _LOG(log, logtype::OPEN_FILES, "%sfd %i: %s\n", prefix, file.first, file.second.c_str()); + } +} + diff --git a/debuggerd/libdebuggerd/test/BacktraceMock.h b/debuggerd/libdebuggerd/test/BacktraceMock.h new file mode 100644 index 000000000..6104f7e0d --- /dev/null +++ b/debuggerd/libdebuggerd/test/BacktraceMock.h @@ -0,0 +1,106 @@ +/* + * Copyright (C) 2015 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef _DEBUGGERD_TEST_BACKTRACE_MOCK_H +#define _DEBUGGERD_TEST_BACKTRACE_MOCK_H + +#include <stdint.h> +#include <stdlib.h> +#include <string.h> +#include <sys/ucontext.h> + +#include <string> +#include <vector> + +#include <backtrace/Backtrace.h> +#include <backtrace/BacktraceMap.h> + +class BacktraceMapMock : public BacktraceMap { + public: + BacktraceMapMock() : BacktraceMap(0) {} + virtual ~BacktraceMapMock() {} + + void AddMap(backtrace_map_t& map) { + maps_.push_back(map); + } +}; + + +class BacktraceMock : public Backtrace { + public: + explicit BacktraceMock(BacktraceMapMock* map) : Backtrace(0, 0, map) { + if (map_ == nullptr) { + abort(); + } + } + virtual ~BacktraceMock() {} + + virtual bool Unwind(size_t, ucontext_t*) { return false; } + virtual bool ReadWord(uintptr_t, word_t*) { return false;} + + virtual std::string GetFunctionNameRaw(uintptr_t, uintptr_t*) { return ""; } + + virtual size_t Read(uintptr_t addr, uint8_t* buffer, size_t bytes) { + size_t offset = 0; + if (last_read_addr_ > 0) { + offset = addr - last_read_addr_; + } + size_t bytes_available = buffer_.size() - offset; + + if (do_partial_read_) { + // Do a partial read. + if (bytes > bytes_partial_read_) { + bytes = bytes_partial_read_; + } + bytes_partial_read_ -= bytes; + // Only support a single partial read. + do_partial_read_ = false; + } else if (bytes > bytes_available) { + bytes = bytes_available; + } + + if (bytes > 0) { + memcpy(buffer, buffer_.data() + offset, bytes); + } + + last_read_addr_ = addr; + return bytes; + } + + void SetReadData(uint8_t* buffer, size_t bytes) { + buffer_.resize(bytes); + memcpy(buffer_.data(), buffer, bytes); + bytes_partial_read_ = 0; + do_partial_read_ = false; + last_read_addr_ = 0; + } + + void SetPartialReadAmount(size_t bytes) { + if (bytes > buffer_.size()) { + abort(); + } + bytes_partial_read_ = bytes; + do_partial_read_ = true; + } + + private: + std::vector<uint8_t> buffer_; + size_t bytes_partial_read_ = 0; + uintptr_t last_read_addr_ = 0; + bool do_partial_read_ = false; +}; + +#endif // _DEBUGGERD_TEST_BACKTRACE_MOCK_H diff --git a/debuggerd/libdebuggerd/test/dump_memory_test.cpp b/debuggerd/libdebuggerd/test/dump_memory_test.cpp new file mode 100644 index 000000000..49f369018 --- /dev/null +++ b/debuggerd/libdebuggerd/test/dump_memory_test.cpp @@ -0,0 +1,740 @@ +/* + * Copyright (C) 2015 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <stdlib.h> + +#include <memory> +#include <string> + +#include <gtest/gtest.h> +#include <android-base/file.h> + +#include "BacktraceMock.h" +#include "log_fake.h" +#include "utility.h" + +const char g_expected_full_dump[] = +"\nmemory near r1:\n" +#if defined(__LP64__) +" 0000000012345658 0706050403020100 0f0e0d0c0b0a0908 ................\n" +" 0000000012345668 1716151413121110 1f1e1d1c1b1a1918 ................\n" +" 0000000012345678 2726252423222120 2f2e2d2c2b2a2928 !\"#$%&'()*+,-./\n" +" 0000000012345688 3736353433323130 3f3e3d3c3b3a3938 0123456789:;<=>?\n" +" 0000000012345698 4746454443424140 4f4e4d4c4b4a4948 @ABCDEFGHIJKLMNO\n" +" 00000000123456a8 5756555453525150 5f5e5d5c5b5a5958 PQRSTUVWXYZ[\\]^_\n" +" 00000000123456b8 6766656463626160 6f6e6d6c6b6a6968 `abcdefghijklmno\n" +" 00000000123456c8 7776757473727170 7f7e7d7c7b7a7978 pqrstuvwxyz{|}~.\n" +" 00000000123456d8 8786858483828180 8f8e8d8c8b8a8988 ................\n" +" 00000000123456e8 9796959493929190 9f9e9d9c9b9a9998 ................\n" +" 00000000123456f8 a7a6a5a4a3a2a1a0 afaeadacabaaa9a8 ................\n" +" 0000000012345708 b7b6b5b4b3b2b1b0 bfbebdbcbbbab9b8 ................\n" +" 0000000012345718 c7c6c5c4c3c2c1c0 cfcecdcccbcac9c8 ................\n" +" 0000000012345728 d7d6d5d4d3d2d1d0 dfdedddcdbdad9d8 ................\n" +" 0000000012345738 e7e6e5e4e3e2e1e0 efeeedecebeae9e8 ................\n" +" 0000000012345748 f7f6f5f4f3f2f1f0 fffefdfcfbfaf9f8 ................\n"; +#else +" 12345658 03020100 07060504 0b0a0908 0f0e0d0c ................\n" +" 12345668 13121110 17161514 1b1a1918 1f1e1d1c ................\n" +" 12345678 23222120 27262524 2b2a2928 2f2e2d2c !\"#$%&'()*+,-./\n" +" 12345688 33323130 37363534 3b3a3938 3f3e3d3c 0123456789:;<=>?\n" +" 12345698 43424140 47464544 4b4a4948 4f4e4d4c @ABCDEFGHIJKLMNO\n" +" 123456a8 53525150 57565554 5b5a5958 5f5e5d5c PQRSTUVWXYZ[\\]^_\n" +" 123456b8 63626160 67666564 6b6a6968 6f6e6d6c `abcdefghijklmno\n" +" 123456c8 73727170 77767574 7b7a7978 7f7e7d7c pqrstuvwxyz{|}~.\n" +" 123456d8 83828180 87868584 8b8a8988 8f8e8d8c ................\n" +" 123456e8 93929190 97969594 9b9a9998 9f9e9d9c ................\n" +" 123456f8 a3a2a1a0 a7a6a5a4 abaaa9a8 afaeadac ................\n" +" 12345708 b3b2b1b0 b7b6b5b4 bbbab9b8 bfbebdbc ................\n" +" 12345718 c3c2c1c0 c7c6c5c4 cbcac9c8 cfcecdcc ................\n" +" 12345728 d3d2d1d0 d7d6d5d4 dbdad9d8 dfdedddc ................\n" +" 12345738 e3e2e1e0 e7e6e5e4 ebeae9e8 efeeedec ................\n" +" 12345748 f3f2f1f0 f7f6f5f4 fbfaf9f8 fffefdfc ................\n"; +#endif + +const char g_expected_partial_dump[] = \ +"\nmemory near pc:\n" +#if defined(__LP64__) +" 00000000123455e0 0706050403020100 0f0e0d0c0b0a0908 ................\n" +" 00000000123455f0 1716151413121110 1f1e1d1c1b1a1918 ................\n" +" 0000000012345600 2726252423222120 2f2e2d2c2b2a2928 !\"#$%&'()*+,-./\n" +" 0000000012345610 3736353433323130 3f3e3d3c3b3a3938 0123456789:;<=>?\n" +" 0000000012345620 4746454443424140 4f4e4d4c4b4a4948 @ABCDEFGHIJKLMNO\n" +" 0000000012345630 5756555453525150 5f5e5d5c5b5a5958 PQRSTUVWXYZ[\\]^_\n" +" 0000000012345640 6766656463626160 ---------------- `abcdefg........\n" +" 0000000012345650 ---------------- ---------------- ................\n" +" 0000000012345660 ---------------- ---------------- ................\n" +" 0000000012345670 ---------------- ---------------- ................\n" +" 0000000012345680 ---------------- ---------------- ................\n" +" 0000000012345690 ---------------- ---------------- ................\n" +" 00000000123456a0 ---------------- ---------------- ................\n" +" 00000000123456b0 ---------------- ---------------- ................\n" +" 00000000123456c0 ---------------- ---------------- ................\n" +" 00000000123456d0 ---------------- ---------------- ................\n"; +#else +" 123455e0 03020100 07060504 0b0a0908 0f0e0d0c ................\n" +" 123455f0 13121110 17161514 1b1a1918 1f1e1d1c ................\n" +" 12345600 23222120 27262524 2b2a2928 2f2e2d2c !\"#$%&'()*+,-./\n" +" 12345610 33323130 37363534 3b3a3938 3f3e3d3c 0123456789:;<=>?\n" +" 12345620 43424140 47464544 4b4a4948 4f4e4d4c @ABCDEFGHIJKLMNO\n" +" 12345630 53525150 57565554 5b5a5958 5f5e5d5c PQRSTUVWXYZ[\\]^_\n" +" 12345640 63626160 67666564 -------- -------- `abcdefg........\n" +" 12345650 -------- -------- -------- -------- ................\n" +" 12345660 -------- -------- -------- -------- ................\n" +" 12345670 -------- -------- -------- -------- ................\n" +" 12345680 -------- -------- -------- -------- ................\n" +" 12345690 -------- -------- -------- -------- ................\n" +" 123456a0 -------- -------- -------- -------- ................\n" +" 123456b0 -------- -------- -------- -------- ................\n" +" 123456c0 -------- -------- -------- -------- ................\n" +" 123456d0 -------- -------- -------- -------- ................\n"; +#endif + +class DumpMemoryTest : public ::testing::Test { + protected: + virtual void SetUp() { + map_mock_.reset(new BacktraceMapMock()); + backtrace_mock_.reset(new BacktraceMock(map_mock_.get())); + + char tmp_file[256]; + const char data_template[] = "/data/local/tmp/debuggerd_memory_testXXXXXX"; + memcpy(tmp_file, data_template, sizeof(data_template)); + int tombstone_fd = mkstemp(tmp_file); + if (tombstone_fd == -1) { + const char tmp_template[] = "/tmp/debuggerd_memory_testXXXXXX"; + memcpy(tmp_file, tmp_template, sizeof(tmp_template)); + tombstone_fd = mkstemp(tmp_file); + if (tombstone_fd == -1) { + abort(); + } + } + if (unlink(tmp_file) == -1) { + abort(); + } + + log_.tfd = tombstone_fd; + log_.amfd_data = nullptr; + log_.crashed_tid = 12; + log_.current_tid = 12; + log_.should_retrieve_logcat = false; + + resetLogs(); + } + + virtual void TearDown() { + if (log_.tfd >= 0) { + close(log_.tfd); + } + } + + std::unique_ptr<BacktraceMapMock> map_mock_; + std::unique_ptr<BacktraceMock> backtrace_mock_; + + log_t log_; +}; + +TEST_F(DumpMemoryTest, aligned_addr) { + uint8_t buffer[256]; + for (size_t i = 0; i < sizeof(buffer); i++) { + buffer[i] = i; + } + backtrace_mock_->SetReadData(buffer, sizeof(buffer)); + + dump_memory(&log_, backtrace_mock_.get(), 0x12345678, "memory near %.2s:", "r1"); + + std::string tombstone_contents; + ASSERT_TRUE(lseek(log_.tfd, 0, SEEK_SET) == 0); + ASSERT_TRUE(android::base::ReadFdToString(log_.tfd, &tombstone_contents)); + ASSERT_STREQ(g_expected_full_dump, tombstone_contents.c_str()); + + // Verify that the log buf is empty, and no error messages. + ASSERT_STREQ("", getFakeLogBuf().c_str()); + ASSERT_STREQ("", getFakeLogPrint().c_str()); +} + +TEST_F(DumpMemoryTest, partial_read) { + uint8_t buffer[256]; + for (size_t i = 0; i < sizeof(buffer); i++) { + buffer[i] = i; + } + backtrace_mock_->SetReadData(buffer, sizeof(buffer)); + backtrace_mock_->SetPartialReadAmount(96); + + dump_memory(&log_, backtrace_mock_.get(), 0x12345679, "memory near %.2s:", "r1"); + + std::string tombstone_contents; + ASSERT_TRUE(lseek(log_.tfd, 0, SEEK_SET) == 0); + ASSERT_TRUE(android::base::ReadFdToString(log_.tfd, &tombstone_contents)); + ASSERT_STREQ(g_expected_full_dump, tombstone_contents.c_str()); + + // Verify that the log buf is empty, and no error messages. + ASSERT_STREQ("", getFakeLogBuf().c_str()); + ASSERT_STREQ("", getFakeLogPrint().c_str()); +} + +TEST_F(DumpMemoryTest, unaligned_addr) { + uint8_t buffer[256]; + for (size_t i = 0; i < sizeof(buffer); i++) { + buffer[i] = i; + } + backtrace_mock_->SetReadData(buffer, sizeof(buffer)); + + dump_memory(&log_, backtrace_mock_.get(), 0x12345679, "memory near %.2s:", "r1"); + + std::string tombstone_contents; + ASSERT_TRUE(lseek(log_.tfd, 0, SEEK_SET) == 0); + ASSERT_TRUE(android::base::ReadFdToString(log_.tfd, &tombstone_contents)); + ASSERT_STREQ(g_expected_full_dump, tombstone_contents.c_str()); + + // Verify that the log buf is empty, and no error messages. + ASSERT_STREQ("", getFakeLogBuf().c_str()); + ASSERT_STREQ("", getFakeLogPrint().c_str()); +} + +TEST_F(DumpMemoryTest, memory_unreadable) { + dump_memory(&log_, backtrace_mock_.get(), 0xa2345678, "memory near pc:"); + + std::string tombstone_contents; + ASSERT_TRUE(lseek(log_.tfd, 0, SEEK_SET) == 0); + ASSERT_TRUE(android::base::ReadFdToString(log_.tfd, &tombstone_contents)); + const char* expected_dump = \ +"\nmemory near pc:\n" +#if defined(__LP64__) +" 00000000a2345658 ---------------- ---------------- ................\n" +" 00000000a2345668 ---------------- ---------------- ................\n" +" 00000000a2345678 ---------------- ---------------- ................\n" +" 00000000a2345688 ---------------- ---------------- ................\n" +" 00000000a2345698 ---------------- ---------------- ................\n" +" 00000000a23456a8 ---------------- ---------------- ................\n" +" 00000000a23456b8 ---------------- ---------------- ................\n" +" 00000000a23456c8 ---------------- ---------------- ................\n" +" 00000000a23456d8 ---------------- ---------------- ................\n" +" 00000000a23456e8 ---------------- ---------------- ................\n" +" 00000000a23456f8 ---------------- ---------------- ................\n" +" 00000000a2345708 ---------------- ---------------- ................\n" +" 00000000a2345718 ---------------- ---------------- ................\n" +" 00000000a2345728 ---------------- ---------------- ................\n" +" 00000000a2345738 ---------------- ---------------- ................\n" +" 00000000a2345748 ---------------- ---------------- ................\n"; +#else +" a2345658 -------- -------- -------- -------- ................\n" +" a2345668 -------- -------- -------- -------- ................\n" +" a2345678 -------- -------- -------- -------- ................\n" +" a2345688 -------- -------- -------- -------- ................\n" +" a2345698 -------- -------- -------- -------- ................\n" +" a23456a8 -------- -------- -------- -------- ................\n" +" a23456b8 -------- -------- -------- -------- ................\n" +" a23456c8 -------- -------- -------- -------- ................\n" +" a23456d8 -------- -------- -------- -------- ................\n" +" a23456e8 -------- -------- -------- -------- ................\n" +" a23456f8 -------- -------- -------- -------- ................\n" +" a2345708 -------- -------- -------- -------- ................\n" +" a2345718 -------- -------- -------- -------- ................\n" +" a2345728 -------- -------- -------- -------- ................\n" +" a2345738 -------- -------- -------- -------- ................\n" +" a2345748 -------- -------- -------- -------- ................\n"; +#endif + ASSERT_STREQ(expected_dump, tombstone_contents.c_str()); + + // Verify that the log buf is empty, and no error messages. + ASSERT_STREQ("", getFakeLogBuf().c_str()); + ASSERT_STREQ("", getFakeLogPrint().c_str()); +} + +TEST_F(DumpMemoryTest, memory_partially_unreadable) { + uint8_t buffer[104]; + for (size_t i = 0; i < sizeof(buffer); i++) { + buffer[i] = i; + } + backtrace_mock_->SetReadData(buffer, sizeof(buffer)); + + dump_memory(&log_, backtrace_mock_.get(), 0x12345600, "memory near pc:"); + + std::string tombstone_contents; + ASSERT_TRUE(lseek(log_.tfd, 0, SEEK_SET) == 0); + ASSERT_TRUE(android::base::ReadFdToString(log_.tfd, &tombstone_contents)); + ASSERT_STREQ(g_expected_partial_dump, tombstone_contents.c_str()); + + // Verify that the log buf is empty, and no error messages. + ASSERT_STREQ("", getFakeLogBuf().c_str()); + ASSERT_STREQ("", getFakeLogPrint().c_str()); +} + +TEST_F(DumpMemoryTest, memory_partially_unreadable_unaligned_return) { + uint8_t buffer[104]; + for (size_t i = 0; i < sizeof(buffer); i++) { + buffer[i] = i; + } + backtrace_mock_->SetReadData(buffer, sizeof(buffer)); + backtrace_mock_->SetPartialReadAmount(102); + + dump_memory(&log_, backtrace_mock_.get(), 0x12345600, "memory near pc:"); + + std::string tombstone_contents; + ASSERT_TRUE(lseek(log_.tfd, 0, SEEK_SET) == 0); + ASSERT_TRUE(android::base::ReadFdToString(log_.tfd, &tombstone_contents)); + ASSERT_STREQ(g_expected_partial_dump, tombstone_contents.c_str()); + +#if defined(__LP64__) + ASSERT_STREQ("6 DEBUG Bytes read 102, is not a multiple of 8\n", getFakeLogPrint().c_str()); +#else + ASSERT_STREQ("6 DEBUG Bytes read 102, is not a multiple of 4\n", getFakeLogPrint().c_str()); +#endif + + // Verify that the log buf is empty, and no error messages. + ASSERT_STREQ("", getFakeLogBuf().c_str()); +} + +TEST_F(DumpMemoryTest, memory_partially_unreadable_two_unaligned_reads) { + uint8_t buffer[106]; + for (size_t i = 0; i < sizeof(buffer); i++) { + buffer[i] = i; + } + backtrace_mock_->SetReadData(buffer, sizeof(buffer)); + backtrace_mock_->SetPartialReadAmount(45); + + dump_memory(&log_, backtrace_mock_.get(), 0x12345600, "memory near pc:"); + + std::string tombstone_contents; + ASSERT_TRUE(lseek(log_.tfd, 0, SEEK_SET) == 0); + ASSERT_TRUE(android::base::ReadFdToString(log_.tfd, &tombstone_contents)); + ASSERT_STREQ(g_expected_partial_dump, tombstone_contents.c_str()); + +#if defined(__LP64__) + ASSERT_STREQ("6 DEBUG Bytes read 45, is not a multiple of 8\n" + "6 DEBUG Bytes after second read 106, is not a multiple of 8\n", + getFakeLogPrint().c_str()); +#else + ASSERT_STREQ("6 DEBUG Bytes read 45, is not a multiple of 4\n" + "6 DEBUG Bytes after second read 106, is not a multiple of 4\n", + getFakeLogPrint().c_str()); +#endif + + // Verify that the log buf is empty, and no error messages. + ASSERT_STREQ("", getFakeLogBuf().c_str()); +} + +TEST_F(DumpMemoryTest, address_low_fence) { + uint8_t buffer[256]; + memset(buffer, 0, sizeof(buffer)); + backtrace_mock_->SetReadData(buffer, sizeof(buffer)); + + dump_memory(&log_, backtrace_mock_.get(), 0x1000, "memory near %.2s:", "r1"); + + std::string tombstone_contents; + ASSERT_TRUE(lseek(log_.tfd, 0, SEEK_SET) == 0); + ASSERT_TRUE(android::base::ReadFdToString(log_.tfd, &tombstone_contents)); + const char* expected_dump = \ +"\nmemory near r1:\n" +#if defined(__LP64__) +" 0000000000001000 0000000000000000 0000000000000000 ................\n" +" 0000000000001010 0000000000000000 0000000000000000 ................\n" +" 0000000000001020 0000000000000000 0000000000000000 ................\n" +" 0000000000001030 0000000000000000 0000000000000000 ................\n" +" 0000000000001040 0000000000000000 0000000000000000 ................\n" +" 0000000000001050 0000000000000000 0000000000000000 ................\n" +" 0000000000001060 0000000000000000 0000000000000000 ................\n" +" 0000000000001070 0000000000000000 0000000000000000 ................\n" +" 0000000000001080 0000000000000000 0000000000000000 ................\n" +" 0000000000001090 0000000000000000 0000000000000000 ................\n" +" 00000000000010a0 0000000000000000 0000000000000000 ................\n" +" 00000000000010b0 0000000000000000 0000000000000000 ................\n" +" 00000000000010c0 0000000000000000 0000000000000000 ................\n" +" 00000000000010d0 0000000000000000 0000000000000000 ................\n" +" 00000000000010e0 0000000000000000 0000000000000000 ................\n" +" 00000000000010f0 0000000000000000 0000000000000000 ................\n"; +#else +" 00001000 00000000 00000000 00000000 00000000 ................\n" +" 00001010 00000000 00000000 00000000 00000000 ................\n" +" 00001020 00000000 00000000 00000000 00000000 ................\n" +" 00001030 00000000 00000000 00000000 00000000 ................\n" +" 00001040 00000000 00000000 00000000 00000000 ................\n" +" 00001050 00000000 00000000 00000000 00000000 ................\n" +" 00001060 00000000 00000000 00000000 00000000 ................\n" +" 00001070 00000000 00000000 00000000 00000000 ................\n" +" 00001080 00000000 00000000 00000000 00000000 ................\n" +" 00001090 00000000 00000000 00000000 00000000 ................\n" +" 000010a0 00000000 00000000 00000000 00000000 ................\n" +" 000010b0 00000000 00000000 00000000 00000000 ................\n" +" 000010c0 00000000 00000000 00000000 00000000 ................\n" +" 000010d0 00000000 00000000 00000000 00000000 ................\n" +" 000010e0 00000000 00000000 00000000 00000000 ................\n" +" 000010f0 00000000 00000000 00000000 00000000 ................\n"; +#endif + ASSERT_STREQ(expected_dump, tombstone_contents.c_str()); + + // Verify that the log buf is empty, and no error messages. + ASSERT_STREQ("", getFakeLogBuf().c_str()); + ASSERT_STREQ("", getFakeLogPrint().c_str()); +} + +TEST_F(DumpMemoryTest, memory_address_too_low) { + uint8_t buffer[256]; + memset(buffer, 0, sizeof(buffer)); + backtrace_mock_->SetReadData(buffer, sizeof(buffer)); + + dump_memory(&log_, backtrace_mock_.get(), 0, "memory near %.2s:", "r1"); + + std::string tombstone_contents; + ASSERT_TRUE(lseek(log_.tfd, 0, SEEK_SET) == 0); + ASSERT_TRUE(android::base::ReadFdToString(log_.tfd, &tombstone_contents)); + ASSERT_STREQ("", tombstone_contents.c_str()); + + // Verify that the log buf is empty, and no error messages. + ASSERT_STREQ("", getFakeLogBuf().c_str()); + ASSERT_STREQ("", getFakeLogPrint().c_str()); +} + +TEST_F(DumpMemoryTest, memory_address_too_high) { + uint8_t buffer[256]; + memset(buffer, 0, sizeof(buffer)); + backtrace_mock_->SetReadData(buffer, sizeof(buffer)); + +#if defined(__LP64__) + dump_memory(&log_, backtrace_mock_.get(), 0x4000000000000000UL, "memory near %.2s:", "r1"); + dump_memory(&log_, backtrace_mock_.get(), 0x4000000000000000UL - 32, "memory near %.2s:", "r1"); + dump_memory(&log_, backtrace_mock_.get(), 0x4000000000000000UL - 216, "memory near %.2s:", "r1"); +#else + dump_memory(&log_, backtrace_mock_.get(), 0xffff0000, "memory near %.2s:", "r1"); + dump_memory(&log_, backtrace_mock_.get(), 0xffff0000 - 32, "memory near %.2s:", "r1"); + dump_memory(&log_, backtrace_mock_.get(), 0xffff0000 - 220, "memory near %.2s:", "r1"); +#endif + + std::string tombstone_contents; + ASSERT_TRUE(lseek(log_.tfd, 0, SEEK_SET) == 0); + ASSERT_TRUE(android::base::ReadFdToString(log_.tfd, &tombstone_contents)); + ASSERT_STREQ("", tombstone_contents.c_str()); + + // Verify that the log buf is empty, and no error messages. + ASSERT_STREQ("", getFakeLogBuf().c_str()); + ASSERT_STREQ("", getFakeLogPrint().c_str()); +} + +TEST_F(DumpMemoryTest, memory_address_would_overflow) { + uint8_t buffer[256]; + memset(buffer, 0, sizeof(buffer)); + backtrace_mock_->SetReadData(buffer, sizeof(buffer)); + +#if defined(__LP64__) + dump_memory(&log_, backtrace_mock_.get(), 0xfffffffffffffff0, "memory near %.2s:", "r1"); +#else + dump_memory(&log_, backtrace_mock_.get(), 0xfffffff0, "memory near %.2s:", "r1"); +#endif + + std::string tombstone_contents; + ASSERT_TRUE(lseek(log_.tfd, 0, SEEK_SET) == 0); + ASSERT_TRUE(android::base::ReadFdToString(log_.tfd, &tombstone_contents)); + ASSERT_STREQ("", tombstone_contents.c_str()); + + // Verify that the log buf is empty, and no error messages. + ASSERT_STREQ("", getFakeLogBuf().c_str()); + ASSERT_STREQ("", getFakeLogPrint().c_str()); +} + +TEST_F(DumpMemoryTest, memory_address_nearly_too_high) { + uint8_t buffer[256]; + for (size_t i = 0; i < sizeof(buffer); i++) { + buffer[i] = i; + } + backtrace_mock_->SetReadData(buffer, sizeof(buffer)); + +#if defined(__LP64__) + dump_memory(&log_, backtrace_mock_.get(), 0x4000000000000000UL - 224, "memory near %.2s:", "r4"); +#else + dump_memory(&log_, backtrace_mock_.get(), 0xffff0000 - 224, "memory near %.2s:", "r4"); +#endif + + std::string tombstone_contents; + ASSERT_TRUE(lseek(log_.tfd, 0, SEEK_SET) == 0); + ASSERT_TRUE(android::base::ReadFdToString(log_.tfd, &tombstone_contents)); + const char* expected_dump = \ +"\nmemory near r4:\n" +#if defined(__LP64__) +" 3fffffffffffff00 0706050403020100 0f0e0d0c0b0a0908 ................\n" +" 3fffffffffffff10 1716151413121110 1f1e1d1c1b1a1918 ................\n" +" 3fffffffffffff20 2726252423222120 2f2e2d2c2b2a2928 !\"#$%&'()*+,-./\n" +" 3fffffffffffff30 3736353433323130 3f3e3d3c3b3a3938 0123456789:;<=>?\n" +" 3fffffffffffff40 4746454443424140 4f4e4d4c4b4a4948 @ABCDEFGHIJKLMNO\n" +" 3fffffffffffff50 5756555453525150 5f5e5d5c5b5a5958 PQRSTUVWXYZ[\\]^_\n" +" 3fffffffffffff60 6766656463626160 6f6e6d6c6b6a6968 `abcdefghijklmno\n" +" 3fffffffffffff70 7776757473727170 7f7e7d7c7b7a7978 pqrstuvwxyz{|}~.\n" +" 3fffffffffffff80 8786858483828180 8f8e8d8c8b8a8988 ................\n" +" 3fffffffffffff90 9796959493929190 9f9e9d9c9b9a9998 ................\n" +" 3fffffffffffffa0 a7a6a5a4a3a2a1a0 afaeadacabaaa9a8 ................\n" +" 3fffffffffffffb0 b7b6b5b4b3b2b1b0 bfbebdbcbbbab9b8 ................\n" +" 3fffffffffffffc0 c7c6c5c4c3c2c1c0 cfcecdcccbcac9c8 ................\n" +" 3fffffffffffffd0 d7d6d5d4d3d2d1d0 dfdedddcdbdad9d8 ................\n" +" 3fffffffffffffe0 e7e6e5e4e3e2e1e0 efeeedecebeae9e8 ................\n" +" 3ffffffffffffff0 f7f6f5f4f3f2f1f0 fffefdfcfbfaf9f8 ................\n"; +#else +" fffeff00 03020100 07060504 0b0a0908 0f0e0d0c ................\n" +" fffeff10 13121110 17161514 1b1a1918 1f1e1d1c ................\n" +" fffeff20 23222120 27262524 2b2a2928 2f2e2d2c !\"#$%&'()*+,-./\n" +" fffeff30 33323130 37363534 3b3a3938 3f3e3d3c 0123456789:;<=>?\n" +" fffeff40 43424140 47464544 4b4a4948 4f4e4d4c @ABCDEFGHIJKLMNO\n" +" fffeff50 53525150 57565554 5b5a5958 5f5e5d5c PQRSTUVWXYZ[\\]^_\n" +" fffeff60 63626160 67666564 6b6a6968 6f6e6d6c `abcdefghijklmno\n" +" fffeff70 73727170 77767574 7b7a7978 7f7e7d7c pqrstuvwxyz{|}~.\n" +" fffeff80 83828180 87868584 8b8a8988 8f8e8d8c ................\n" +" fffeff90 93929190 97969594 9b9a9998 9f9e9d9c ................\n" +" fffeffa0 a3a2a1a0 a7a6a5a4 abaaa9a8 afaeadac ................\n" +" fffeffb0 b3b2b1b0 b7b6b5b4 bbbab9b8 bfbebdbc ................\n" +" fffeffc0 c3c2c1c0 c7c6c5c4 cbcac9c8 cfcecdcc ................\n" +" fffeffd0 d3d2d1d0 d7d6d5d4 dbdad9d8 dfdedddc ................\n" +" fffeffe0 e3e2e1e0 e7e6e5e4 ebeae9e8 efeeedec ................\n" +" fffefff0 f3f2f1f0 f7f6f5f4 fbfaf9f8 fffefdfc ................\n"; +#endif + ASSERT_STREQ(expected_dump, tombstone_contents.c_str()); + + // Verify that the log buf is empty, and no error messages. + ASSERT_STREQ("", getFakeLogBuf().c_str()); + ASSERT_STREQ("", getFakeLogPrint().c_str()); +} + +TEST_F(DumpMemoryTest, first_read_empty) { + uint8_t buffer[256]; + for (size_t i = 0; i < sizeof(buffer); i++) { + buffer[i] = i; + } + backtrace_mock_->SetReadData(buffer, sizeof(buffer)); + backtrace_mock_->SetPartialReadAmount(0); + + size_t page_size = sysconf(_SC_PAGE_SIZE); + uintptr_t addr = 0x10000020 + page_size - 120; + dump_memory(&log_, backtrace_mock_.get(), addr, "memory near %.2s:", "r4"); + + std::string tombstone_contents; + ASSERT_TRUE(lseek(log_.tfd, 0, SEEK_SET) == 0); + ASSERT_TRUE(android::base::ReadFdToString(log_.tfd, &tombstone_contents)); + const char* expected_dump = \ +"\nmemory near r4:\n" +#if defined(__LP64__) +" 0000000010000f88 ---------------- ---------------- ................\n" +" 0000000010000f98 ---------------- ---------------- ................\n" +" 0000000010000fa8 ---------------- ---------------- ................\n" +" 0000000010000fb8 ---------------- ---------------- ................\n" +" 0000000010000fc8 ---------------- ---------------- ................\n" +" 0000000010000fd8 ---------------- ---------------- ................\n" +" 0000000010000fe8 ---------------- ---------------- ................\n" +" 0000000010000ff8 ---------------- 7f7e7d7c7b7a7978 ........xyz{|}~.\n" +" 0000000010001008 8786858483828180 8f8e8d8c8b8a8988 ................\n" +" 0000000010001018 9796959493929190 9f9e9d9c9b9a9998 ................\n" +" 0000000010001028 a7a6a5a4a3a2a1a0 afaeadacabaaa9a8 ................\n" +" 0000000010001038 b7b6b5b4b3b2b1b0 bfbebdbcbbbab9b8 ................\n" +" 0000000010001048 c7c6c5c4c3c2c1c0 cfcecdcccbcac9c8 ................\n" +" 0000000010001058 d7d6d5d4d3d2d1d0 dfdedddcdbdad9d8 ................\n" +" 0000000010001068 e7e6e5e4e3e2e1e0 efeeedecebeae9e8 ................\n" +" 0000000010001078 f7f6f5f4f3f2f1f0 fffefdfcfbfaf9f8 ................\n"; +#else +" 10000f88 -------- -------- -------- -------- ................\n" +" 10000f98 -------- -------- -------- -------- ................\n" +" 10000fa8 -------- -------- -------- -------- ................\n" +" 10000fb8 -------- -------- -------- -------- ................\n" +" 10000fc8 -------- -------- -------- -------- ................\n" +" 10000fd8 -------- -------- -------- -------- ................\n" +" 10000fe8 -------- -------- -------- -------- ................\n" +" 10000ff8 -------- -------- 7b7a7978 7f7e7d7c ........xyz{|}~.\n" +" 10001008 83828180 87868584 8b8a8988 8f8e8d8c ................\n" +" 10001018 93929190 97969594 9b9a9998 9f9e9d9c ................\n" +" 10001028 a3a2a1a0 a7a6a5a4 abaaa9a8 afaeadac ................\n" +" 10001038 b3b2b1b0 b7b6b5b4 bbbab9b8 bfbebdbc ................\n" +" 10001048 c3c2c1c0 c7c6c5c4 cbcac9c8 cfcecdcc ................\n" +" 10001058 d3d2d1d0 d7d6d5d4 dbdad9d8 dfdedddc ................\n" +" 10001068 e3e2e1e0 e7e6e5e4 ebeae9e8 efeeedec ................\n" +" 10001078 f3f2f1f0 f7f6f5f4 fbfaf9f8 fffefdfc ................\n"; +#endif + ASSERT_STREQ(expected_dump, tombstone_contents.c_str()); + + // Verify that the log buf is empty, and no error messages. + ASSERT_STREQ("", getFakeLogBuf().c_str()); + ASSERT_STREQ("", getFakeLogPrint().c_str()); +} + +TEST_F(DumpMemoryTest, first_read_empty_second_read_stops) { + uint8_t buffer[224]; + for (size_t i = 0; i < sizeof(buffer); i++) { + buffer[i] = i; + } + backtrace_mock_->SetReadData(buffer, sizeof(buffer)); + backtrace_mock_->SetPartialReadAmount(0); + + size_t page_size = sysconf(_SC_PAGE_SIZE); + uintptr_t addr = 0x10000020 + page_size - 192; + dump_memory(&log_, backtrace_mock_.get(), addr, "memory near %.2s:", "r4"); + + std::string tombstone_contents; + ASSERT_TRUE(lseek(log_.tfd, 0, SEEK_SET) == 0); + ASSERT_TRUE(android::base::ReadFdToString(log_.tfd, &tombstone_contents)); + const char* expected_dump = \ +"\nmemory near r4:\n" +#if defined(__LP64__) +" 0000000010000f40 ---------------- ---------------- ................\n" +" 0000000010000f50 ---------------- ---------------- ................\n" +" 0000000010000f60 ---------------- ---------------- ................\n" +" 0000000010000f70 ---------------- ---------------- ................\n" +" 0000000010000f80 ---------------- ---------------- ................\n" +" 0000000010000f90 ---------------- ---------------- ................\n" +" 0000000010000fa0 ---------------- ---------------- ................\n" +" 0000000010000fb0 ---------------- ---------------- ................\n" +" 0000000010000fc0 ---------------- ---------------- ................\n" +" 0000000010000fd0 ---------------- ---------------- ................\n" +" 0000000010000fe0 ---------------- ---------------- ................\n" +" 0000000010000ff0 ---------------- ---------------- ................\n" +" 0000000010001000 c7c6c5c4c3c2c1c0 cfcecdcccbcac9c8 ................\n" +" 0000000010001010 d7d6d5d4d3d2d1d0 dfdedddcdbdad9d8 ................\n" +" 0000000010001020 ---------------- ---------------- ................\n" +" 0000000010001030 ---------------- ---------------- ................\n"; +#else +" 10000f40 -------- -------- -------- -------- ................\n" +" 10000f50 -------- -------- -------- -------- ................\n" +" 10000f60 -------- -------- -------- -------- ................\n" +" 10000f70 -------- -------- -------- -------- ................\n" +" 10000f80 -------- -------- -------- -------- ................\n" +" 10000f90 -------- -------- -------- -------- ................\n" +" 10000fa0 -------- -------- -------- -------- ................\n" +" 10000fb0 -------- -------- -------- -------- ................\n" +" 10000fc0 -------- -------- -------- -------- ................\n" +" 10000fd0 -------- -------- -------- -------- ................\n" +" 10000fe0 -------- -------- -------- -------- ................\n" +" 10000ff0 -------- -------- -------- -------- ................\n" +" 10001000 c3c2c1c0 c7c6c5c4 cbcac9c8 cfcecdcc ................\n" +" 10001010 d3d2d1d0 d7d6d5d4 dbdad9d8 dfdedddc ................\n" +" 10001020 -------- -------- -------- -------- ................\n" +" 10001030 -------- -------- -------- -------- ................\n"; +#endif + ASSERT_STREQ(expected_dump, tombstone_contents.c_str()); + + // Verify that the log buf is empty, and no error messages. + ASSERT_STREQ("", getFakeLogBuf().c_str()); + ASSERT_STREQ("", getFakeLogPrint().c_str()); +} + +TEST_F(DumpMemoryTest, first_read_empty_next_page_out_of_range) { + uint8_t buffer[256]; + for (size_t i = 0; i < sizeof(buffer); i++) { + buffer[i] = i; + } + backtrace_mock_->SetReadData(buffer, sizeof(buffer)); + backtrace_mock_->SetPartialReadAmount(0); + + uintptr_t addr = 0x10000020; + dump_memory(&log_, backtrace_mock_.get(), addr, "memory near %.2s:", "r4"); + + std::string tombstone_contents; + ASSERT_TRUE(lseek(log_.tfd, 0, SEEK_SET) == 0); + ASSERT_TRUE(android::base::ReadFdToString(log_.tfd, &tombstone_contents)); + const char* expected_dump = \ +"\nmemory near r4:\n" +#if defined(__LP64__) +" 0000000010000000 ---------------- ---------------- ................\n" +" 0000000010000010 ---------------- ---------------- ................\n" +" 0000000010000020 ---------------- ---------------- ................\n" +" 0000000010000030 ---------------- ---------------- ................\n" +" 0000000010000040 ---------------- ---------------- ................\n" +" 0000000010000050 ---------------- ---------------- ................\n" +" 0000000010000060 ---------------- ---------------- ................\n" +" 0000000010000070 ---------------- ---------------- ................\n" +" 0000000010000080 ---------------- ---------------- ................\n" +" 0000000010000090 ---------------- ---------------- ................\n" +" 00000000100000a0 ---------------- ---------------- ................\n" +" 00000000100000b0 ---------------- ---------------- ................\n" +" 00000000100000c0 ---------------- ---------------- ................\n" +" 00000000100000d0 ---------------- ---------------- ................\n" +" 00000000100000e0 ---------------- ---------------- ................\n" +" 00000000100000f0 ---------------- ---------------- ................\n"; +#else +" 10000000 -------- -------- -------- -------- ................\n" +" 10000010 -------- -------- -------- -------- ................\n" +" 10000020 -------- -------- -------- -------- ................\n" +" 10000030 -------- -------- -------- -------- ................\n" +" 10000040 -------- -------- -------- -------- ................\n" +" 10000050 -------- -------- -------- -------- ................\n" +" 10000060 -------- -------- -------- -------- ................\n" +" 10000070 -------- -------- -------- -------- ................\n" +" 10000080 -------- -------- -------- -------- ................\n" +" 10000090 -------- -------- -------- -------- ................\n" +" 100000a0 -------- -------- -------- -------- ................\n" +" 100000b0 -------- -------- -------- -------- ................\n" +" 100000c0 -------- -------- -------- -------- ................\n" +" 100000d0 -------- -------- -------- -------- ................\n" +" 100000e0 -------- -------- -------- -------- ................\n" +" 100000f0 -------- -------- -------- -------- ................\n"; +#endif + ASSERT_STREQ(expected_dump, tombstone_contents.c_str()); + + // Verify that the log buf is empty, and no error messages. + ASSERT_STREQ("", getFakeLogBuf().c_str()); + ASSERT_STREQ("", getFakeLogPrint().c_str()); +} + +TEST_F(DumpMemoryTest, first_read_empty_next_page_out_of_range_fence_post) { + uint8_t buffer[256]; + for (size_t i = 0; i < sizeof(buffer); i++) { + buffer[i] = i; + } + backtrace_mock_->SetReadData(buffer, sizeof(buffer)); + backtrace_mock_->SetPartialReadAmount(0); + + size_t page_size = sysconf(_SC_PAGE_SIZE); + uintptr_t addr = 0x10000020 + page_size - 256; + + dump_memory(&log_, backtrace_mock_.get(), addr, "memory near %.2s:", "r4"); + + std::string tombstone_contents; + ASSERT_TRUE(lseek(log_.tfd, 0, SEEK_SET) == 0); + ASSERT_TRUE(android::base::ReadFdToString(log_.tfd, &tombstone_contents)); + const char* expected_dump = \ +"\nmemory near r4:\n" +#if defined(__LP64__) +" 0000000010000f00 ---------------- ---------------- ................\n" +" 0000000010000f10 ---------------- ---------------- ................\n" +" 0000000010000f20 ---------------- ---------------- ................\n" +" 0000000010000f30 ---------------- ---------------- ................\n" +" 0000000010000f40 ---------------- ---------------- ................\n" +" 0000000010000f50 ---------------- ---------------- ................\n" +" 0000000010000f60 ---------------- ---------------- ................\n" +" 0000000010000f70 ---------------- ---------------- ................\n" +" 0000000010000f80 ---------------- ---------------- ................\n" +" 0000000010000f90 ---------------- ---------------- ................\n" +" 0000000010000fa0 ---------------- ---------------- ................\n" +" 0000000010000fb0 ---------------- ---------------- ................\n" +" 0000000010000fc0 ---------------- ---------------- ................\n" +" 0000000010000fd0 ---------------- ---------------- ................\n" +" 0000000010000fe0 ---------------- ---------------- ................\n" +" 0000000010000ff0 ---------------- ---------------- ................\n"; +#else +" 10000f00 -------- -------- -------- -------- ................\n" +" 10000f10 -------- -------- -------- -------- ................\n" +" 10000f20 -------- -------- -------- -------- ................\n" +" 10000f30 -------- -------- -------- -------- ................\n" +" 10000f40 -------- -------- -------- -------- ................\n" +" 10000f50 -------- -------- -------- -------- ................\n" +" 10000f60 -------- -------- -------- -------- ................\n" +" 10000f70 -------- -------- -------- -------- ................\n" +" 10000f80 -------- -------- -------- -------- ................\n" +" 10000f90 -------- -------- -------- -------- ................\n" +" 10000fa0 -------- -------- -------- -------- ................\n" +" 10000fb0 -------- -------- -------- -------- ................\n" +" 10000fc0 -------- -------- -------- -------- ................\n" +" 10000fd0 -------- -------- -------- -------- ................\n" +" 10000fe0 -------- -------- -------- -------- ................\n" +" 10000ff0 -------- -------- -------- -------- ................\n"; +#endif + ASSERT_STREQ(expected_dump, tombstone_contents.c_str()); + + // Verify that the log buf is empty, and no error messages. + ASSERT_STREQ("", getFakeLogBuf().c_str()); + ASSERT_STREQ("", getFakeLogPrint().c_str()); +} diff --git a/debuggerd/libdebuggerd/test/elf_fake.cpp b/debuggerd/libdebuggerd/test/elf_fake.cpp new file mode 100644 index 000000000..bb52b59c9 --- /dev/null +++ b/debuggerd/libdebuggerd/test/elf_fake.cpp @@ -0,0 +1,35 @@ +/* + * Copyright (C) 2015 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <stdint.h> + +#include <string> + +class Backtrace; + +std::string g_build_id; + +void elf_set_fake_build_id(const std::string& build_id) { + g_build_id = build_id; +} + +bool elf_get_build_id(Backtrace*, uintptr_t, std::string* build_id) { + if (g_build_id != "") { + *build_id = g_build_id; + return true; + } + return false; +} diff --git a/debuggerd/libdebuggerd/test/elf_fake.h b/debuggerd/libdebuggerd/test/elf_fake.h new file mode 100644 index 000000000..08a8454a4 --- /dev/null +++ b/debuggerd/libdebuggerd/test/elf_fake.h @@ -0,0 +1,24 @@ +/* + * Copyright (C) 2015 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef _DEBUGGERD_TEST_ELF_FAKE_H +#define _DEBUGGERD_TEST_ELF_FAKE_H + +#include <string> + +void elf_set_fake_build_id(const std::string&); + +#endif // _DEBUGGERD_TEST_ELF_FAKE_H diff --git a/debuggerd/libdebuggerd/test/host_signal_fixup.h b/debuggerd/libdebuggerd/test/host_signal_fixup.h new file mode 100644 index 000000000..762bae5fb --- /dev/null +++ b/debuggerd/libdebuggerd/test/host_signal_fixup.h @@ -0,0 +1,65 @@ +/* + * Copyright (C) 2015 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef _DEBUGGERD_TEST_HOST_SIGNAL_FIXUP_H +#define _DEBUGGERD_TEST_HOST_SIGNAL_FIXUP_H + +#include <signal.h> + +#if !defined(__BIONIC__) + +// In order to compile parts of debuggerd for the host, we need to +// define these values. + +#if !defined(NSIGILL) +#define NSIGILL ILL_BADSTK +#endif + +#if !defined(BUS_MCEERR_AR) +#define BUS_MCEERR_AR 4 +#endif +#if !defined(BUS_MCEERR_AO) +#define BUS_MCEERR_AO 5 +#endif +#if !defined(NSIGBUS) +#define NSIGBUS BUS_MCEERR_AO +#endif + +#if !defined(NSIGFPE) +#define NSIGFPE FPE_FLTSUB +#endif + +#if !defined(NSIGSEGV) +#define NSIGSEGV SEGV_ACCERR +#endif + +#if !defined(TRAP_BRANCH) +#define TRAP_BRANCH 3 +#endif +#if !defined(TRAP_HWBKPT) +#define TRAP_HWBKPT 4 +#endif +#if !defined(NSIGTRAP) +#define NSIGTRAP TRAP_HWBKPT +#endif + +#if !defined(SI_DETHREAD) +#define SI_DETHREAD (-7) +#endif + +#endif + +#endif // _DEBUGGERD_TEST_HOST_SIGNAL_FIXUP_H diff --git a/debuggerd/libdebuggerd/test/log_fake.cpp b/debuggerd/libdebuggerd/test/log_fake.cpp new file mode 100644 index 000000000..3336bcb50 --- /dev/null +++ b/debuggerd/libdebuggerd/test/log_fake.cpp @@ -0,0 +1,94 @@ +/* + * Copyright (C) 2015 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <errno.h> +#include <stdarg.h> + +#include <string> + +#include <android-base/stringprintf.h> +#include <log/log.h> + +// Forward declarations. +class Backtrace; +struct EventTagMap; +struct AndroidLogEntry; + +std::string g_fake_log_buf; + +std::string g_fake_log_print; + +void resetLogs() { + g_fake_log_buf = ""; + g_fake_log_print = ""; +} + +std::string getFakeLogBuf() { + return g_fake_log_buf; +} + +std::string getFakeLogPrint() { + return g_fake_log_print; +} + +extern "C" int __android_log_buf_write(int bufId, int prio, const char* tag, const char* msg) { + g_fake_log_buf += std::to_string(bufId) + ' ' + std::to_string(prio) + ' '; + g_fake_log_buf += tag; + g_fake_log_buf += ' '; + g_fake_log_buf += msg; + return 1; +} + +extern "C" int __android_log_print(int prio, const char* tag, const char* fmt, ...) { + g_fake_log_print += std::to_string(prio) + ' '; + g_fake_log_print += tag; + g_fake_log_print += ' '; + + va_list ap; + va_start(ap, fmt); + android::base::StringAppendV(&g_fake_log_print, fmt, ap); + va_end(ap); + + g_fake_log_print += '\n'; + + return 1; +} + +extern "C" log_id_t android_name_to_log_id(const char*) { + return LOG_ID_SYSTEM; +} + +extern "C" struct logger_list* android_logger_list_open(log_id_t, int, unsigned int, pid_t) { + errno = EACCES; + return nullptr; +} + +extern "C" int android_logger_list_read(struct logger_list*, struct log_msg*) { + return 0; +} + +extern "C" EventTagMap* android_openEventTagMap(const char*) { + return nullptr; +} + +extern "C" int android_log_processBinaryLogBuffer( + struct logger_entry*, + AndroidLogEntry*, const EventTagMap*, char*, int) { + return 0; +} + +extern "C" void android_logger_list_free(struct logger_list*) { +} diff --git a/debuggerd/libdebuggerd/test/log_fake.h b/debuggerd/libdebuggerd/test/log_fake.h new file mode 100644 index 000000000..5418fce64 --- /dev/null +++ b/debuggerd/libdebuggerd/test/log_fake.h @@ -0,0 +1,26 @@ +/* + * Copyright (C) 2015 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef _DEBUGGERD_TEST_LOG_FAKE_H +#define _DEBUGGERD_TEST_LOG_FAKE_H + +#include <string> + +void resetLogs(); +std::string getFakeLogBuf(); +std::string getFakeLogPrint(); + +#endif // _DEBUGGERD_TEST_LOG_FAKE_H diff --git a/debuggerd/libdebuggerd/test/open_files_list_test.cpp b/debuggerd/libdebuggerd/test/open_files_list_test.cpp new file mode 100644 index 000000000..85e069510 --- /dev/null +++ b/debuggerd/libdebuggerd/test/open_files_list_test.cpp @@ -0,0 +1,49 @@ +/* + * Copyright (C) 2016 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +#include <string> + +#include <gtest/gtest.h> + +#include "android-base/test_utils.h" + +#include "open_files_list.h" + +// Check that we can produce a list of open files for the current process, and +// that it includes a known open file. +TEST(OpenFilesListTest, BasicTest) { + // Open a temporary file that we can check for in the list of open files. + TemporaryFile tf; + + // Get the list of open files for this process. + OpenFilesList list; + populate_open_files_list(getpid(), &list); + + // Verify our open file is in the list. + bool found = false; + for (auto& file : list) { + if (file.first == tf.fd) { + EXPECT_EQ(file.second, std::string(tf.path)); + found = true; + break; + } + } + EXPECT_TRUE(found); +} diff --git a/debuggerd/libdebuggerd/test/property_fake.cpp b/debuggerd/libdebuggerd/test/property_fake.cpp new file mode 100644 index 000000000..02069f1af --- /dev/null +++ b/debuggerd/libdebuggerd/test/property_fake.cpp @@ -0,0 +1,45 @@ +/* + * Copyright (C) 2015 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <string.h> + +#include <string> +#include <unordered_map> + +#include <sys/system_properties.h> + +std::unordered_map<std::string, std::string> g_properties; + +extern "C" int property_set(const char* name, const char* value) { + if (g_properties.count(name) != 0) { + g_properties.erase(name); + } + g_properties[name] = value; + return 0; +} + +extern "C" int property_get(const char* key, char* value, const char* default_value) { + if (g_properties.count(key) == 0) { + if (default_value == nullptr) { + return 0; + } + strncpy(value, default_value, PROP_VALUE_MAX-1); + } else { + strncpy(value, g_properties[key].c_str(), PROP_VALUE_MAX-1); + } + value[PROP_VALUE_MAX-1] = '\0'; + return strlen(value); +} diff --git a/debuggerd/libdebuggerd/test/ptrace_fake.cpp b/debuggerd/libdebuggerd/test/ptrace_fake.cpp new file mode 100644 index 000000000..f40cbd429 --- /dev/null +++ b/debuggerd/libdebuggerd/test/ptrace_fake.cpp @@ -0,0 +1,53 @@ +/* + * Copyright (C) 2015 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <errno.h> +#include <signal.h> +#include <stdarg.h> +#include <sys/ptrace.h> + +#include <string> + +#include "ptrace_fake.h" + +siginfo_t g_fake_si = {.si_signo = 0}; + +void ptrace_set_fake_getsiginfo(const siginfo_t& si) { + g_fake_si = si; +} + +#if !defined(__BIONIC__) +extern "C" long ptrace_fake(enum __ptrace_request request, ...) { +#else +extern "C" long ptrace_fake(int request, ...) { +#endif + if (request == PTRACE_GETSIGINFO) { + if (g_fake_si.si_signo == 0) { + errno = EFAULT; + return -1; + } + + va_list ap; + va_start(ap, request); + va_arg(ap, int); + va_arg(ap, int); + siginfo_t* si = va_arg(ap, siginfo*); + va_end(ap); + *si = g_fake_si; + return 0; + } + return -1; +} diff --git a/debuggerd/libdebuggerd/test/ptrace_fake.h b/debuggerd/libdebuggerd/test/ptrace_fake.h new file mode 100644 index 000000000..fdbb66361 --- /dev/null +++ b/debuggerd/libdebuggerd/test/ptrace_fake.h @@ -0,0 +1,24 @@ +/* + * Copyright (C) 2015 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef _DEBUGGERD_TEST_PTRACE_FAKE_H +#define _DEBUGGERD_TEST_PTRACE_FAKE_H + +#include <signal.h> + +void ptrace_set_fake_getsiginfo(const siginfo_t&); + +#endif // _DEBUGGERD_TEST_PTRACE_FAKE_H diff --git a/debuggerd/libdebuggerd/test/sys/system_properties.h b/debuggerd/libdebuggerd/test/sys/system_properties.h new file mode 100644 index 000000000..9d4434530 --- /dev/null +++ b/debuggerd/libdebuggerd/test/sys/system_properties.h @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2015 The Android Open Source Project + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef _DEBUGGERD_TEST_SYS_SYSTEM_PROPERTIES_H +#define _DEBUGGERD_TEST_SYS_SYSTEM_PROPERTIES_H + +// This is just enough to get the property code to compile on +// the host. + +#define PROP_NAME_MAX 32 +#define PROP_VALUE_MAX 92 + +#endif // _DEBUGGERD_TEST_SYS_SYSTEM_PROPERTIES_H diff --git a/debuggerd/libdebuggerd/test/tombstone_test.cpp b/debuggerd/libdebuggerd/test/tombstone_test.cpp new file mode 100644 index 000000000..5ee07cda1 --- /dev/null +++ b/debuggerd/libdebuggerd/test/tombstone_test.cpp @@ -0,0 +1,633 @@ +/* + * Copyright (C) 2015 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <stdlib.h> + +#include <memory> +#include <string> + +#include <gtest/gtest.h> +#include <android-base/file.h> + +#include "utility.h" + +#include "BacktraceMock.h" +#include "elf_fake.h" +#include "host_signal_fixup.h" +#include "log_fake.h" +#include "ptrace_fake.h" + +// In order to test this code, we need to include the tombstone.cpp code. +// Including it, also allows us to override the ptrace function. +#define ptrace ptrace_fake + +#include "tombstone.cpp" + +void dump_registers(log_t*, pid_t) { +} + +void dump_memory_and_code(log_t*, Backtrace*) { +} + +void dump_backtrace_to_log(Backtrace*, log_t*, char const*) { +} + +class TombstoneTest : public ::testing::Test { + protected: + virtual void SetUp() { + map_mock_.reset(new BacktraceMapMock()); + backtrace_mock_.reset(new BacktraceMock(map_mock_.get())); + + char tmp_file[256]; + const char data_template[] = "/data/local/tmp/debuggerd_memory_testXXXXXX"; + memcpy(tmp_file, data_template, sizeof(data_template)); + int tombstone_fd = mkstemp(tmp_file); + if (tombstone_fd == -1) { + const char tmp_template[] = "/tmp/debuggerd_memory_testXXXXXX"; + memcpy(tmp_file, tmp_template, sizeof(tmp_template)); + tombstone_fd = mkstemp(tmp_file); + if (tombstone_fd == -1) { + abort(); + } + } + if (unlink(tmp_file) == -1) { + abort(); + } + + log_.tfd = tombstone_fd; + amfd_data_.clear(); + log_.amfd_data = &amfd_data_; + log_.crashed_tid = 12; + log_.current_tid = 12; + log_.should_retrieve_logcat = false; + + resetLogs(); + elf_set_fake_build_id(""); + siginfo_t si; + si.si_signo = SIGABRT; + ptrace_set_fake_getsiginfo(si); + } + + virtual void TearDown() { + if (log_.tfd >= 0) { + close(log_.tfd); + } + } + + std::unique_ptr<BacktraceMapMock> map_mock_; + std::unique_ptr<BacktraceMock> backtrace_mock_; + + log_t log_; + std::string amfd_data_; +}; + +TEST_F(TombstoneTest, single_map) { + backtrace_map_t map; +#if defined(__LP64__) + map.start = 0x123456789abcd000UL; + map.end = 0x123456789abdf000UL; +#else + map.start = 0x1234000; + map.end = 0x1235000; +#endif + map_mock_->AddMap(map); + + dump_all_maps(backtrace_mock_.get(), map_mock_.get(), &log_, 100); + + std::string tombstone_contents; + ASSERT_TRUE(lseek(log_.tfd, 0, SEEK_SET) == 0); + ASSERT_TRUE(android::base::ReadFdToString(log_.tfd, &tombstone_contents)); + const char* expected_dump = \ +"\nmemory map:\n" +#if defined(__LP64__) +" 12345678'9abcd000-12345678'9abdefff --- 0 12000\n"; +#else +" 01234000-01234fff --- 0 1000\n"; +#endif + ASSERT_STREQ(expected_dump, tombstone_contents.c_str()); + + ASSERT_STREQ("", amfd_data_.c_str()); + + // Verify that the log buf is empty, and no error messages. + ASSERT_STREQ("", getFakeLogBuf().c_str()); + ASSERT_STREQ("", getFakeLogPrint().c_str()); +} + +TEST_F(TombstoneTest, single_map_elf_build_id) { + backtrace_map_t map; +#if defined(__LP64__) + map.start = 0x123456789abcd000UL; + map.end = 0x123456789abdf000UL; +#else + map.start = 0x1234000; + map.end = 0x1235000; +#endif + map.flags = PROT_READ; + map.name = "/system/lib/libfake.so"; + map_mock_->AddMap(map); + + elf_set_fake_build_id("abcdef1234567890abcdef1234567890"); + dump_all_maps(backtrace_mock_.get(), map_mock_.get(), &log_, 100); + + std::string tombstone_contents; + ASSERT_TRUE(lseek(log_.tfd, 0, SEEK_SET) == 0); + ASSERT_TRUE(android::base::ReadFdToString(log_.tfd, &tombstone_contents)); + const char* expected_dump = \ +"\nmemory map:\n" +#if defined(__LP64__) +" 12345678'9abcd000-12345678'9abdefff r-- 0 12000 /system/lib/libfake.so (BuildId: abcdef1234567890abcdef1234567890)\n"; +#else +" 01234000-01234fff r-- 0 1000 /system/lib/libfake.so (BuildId: abcdef1234567890abcdef1234567890)\n"; +#endif + ASSERT_STREQ(expected_dump, tombstone_contents.c_str()); + + ASSERT_STREQ("", amfd_data_.c_str()); + + // Verify that the log buf is empty, and no error messages. + ASSERT_STREQ("", getFakeLogBuf().c_str()); + ASSERT_STREQ("", getFakeLogPrint().c_str()); +} + +// Even though build id is present, it should not be printed in either of +// these cases. +TEST_F(TombstoneTest, single_map_no_build_id) { + backtrace_map_t map; +#if defined(__LP64__) + map.start = 0x123456789abcd000UL; + map.end = 0x123456789abdf000UL; +#else + map.start = 0x1234000; + map.end = 0x1235000; +#endif + map.flags = PROT_WRITE; + map_mock_->AddMap(map); + + map.name = "/system/lib/libfake.so"; + map_mock_->AddMap(map); + + elf_set_fake_build_id("abcdef1234567890abcdef1234567890"); + dump_all_maps(backtrace_mock_.get(), map_mock_.get(), &log_, 100); + + std::string tombstone_contents; + ASSERT_TRUE(lseek(log_.tfd, 0, SEEK_SET) == 0); + ASSERT_TRUE(android::base::ReadFdToString(log_.tfd, &tombstone_contents)); + const char* expected_dump = \ +"\nmemory map:\n" +#if defined(__LP64__) +" 12345678'9abcd000-12345678'9abdefff -w- 0 12000\n" +" 12345678'9abcd000-12345678'9abdefff -w- 0 12000 /system/lib/libfake.so\n"; +#else +" 01234000-01234fff -w- 0 1000\n" +" 01234000-01234fff -w- 0 1000 /system/lib/libfake.so\n"; +#endif + ASSERT_STREQ(expected_dump, tombstone_contents.c_str()); + + ASSERT_STREQ("", amfd_data_.c_str()); + + // Verify that the log buf is empty, and no error messages. + ASSERT_STREQ("", getFakeLogBuf().c_str()); + ASSERT_STREQ("", getFakeLogPrint().c_str()); +} + +TEST_F(TombstoneTest, multiple_maps) { + backtrace_map_t map; + + map.start = 0xa234000; + map.end = 0xa235000; + map_mock_->AddMap(map); + + map.start = 0xa334000; + map.end = 0xa335000; + map.offset = 0xf000; + map.flags = PROT_READ; + map_mock_->AddMap(map); + + map.start = 0xa434000; + map.end = 0xa435000; + map.offset = 0x1000; + map.load_base = 0xd000; + map.flags = PROT_WRITE; + map_mock_->AddMap(map); + + map.start = 0xa534000; + map.end = 0xa535000; + map.offset = 0x3000; + map.load_base = 0x2000; + map.flags = PROT_EXEC; + map_mock_->AddMap(map); + + map.start = 0xa634000; + map.end = 0xa635000; + map.offset = 0; + map.load_base = 0; + map.flags = PROT_READ | PROT_WRITE | PROT_EXEC; + map.name = "/system/lib/fake.so"; + map_mock_->AddMap(map); + + dump_all_maps(backtrace_mock_.get(), map_mock_.get(), &log_, 100); + + std::string tombstone_contents; + ASSERT_TRUE(lseek(log_.tfd, 0, SEEK_SET) == 0); + ASSERT_TRUE(android::base::ReadFdToString(log_.tfd, &tombstone_contents)); + const char* expected_dump = \ +"\nmemory map:\n" +#if defined(__LP64__) +" 00000000'0a234000-00000000'0a234fff --- 0 1000\n" +" 00000000'0a334000-00000000'0a334fff r-- f000 1000\n" +" 00000000'0a434000-00000000'0a434fff -w- 1000 1000 (load base 0xd000)\n" +" 00000000'0a534000-00000000'0a534fff --x 3000 1000 (load base 0x2000)\n" +" 00000000'0a634000-00000000'0a634fff rwx 0 1000 /system/lib/fake.so\n"; +#else +" 0a234000-0a234fff --- 0 1000\n" +" 0a334000-0a334fff r-- f000 1000\n" +" 0a434000-0a434fff -w- 1000 1000 (load base 0xd000)\n" +" 0a534000-0a534fff --x 3000 1000 (load base 0x2000)\n" +" 0a634000-0a634fff rwx 0 1000 /system/lib/fake.so\n"; +#endif + ASSERT_STREQ(expected_dump, tombstone_contents.c_str()); + + ASSERT_STREQ("", amfd_data_.c_str()); + + // Verify that the log buf is empty, and no error messages. + ASSERT_STREQ("", getFakeLogBuf().c_str()); + ASSERT_STREQ("", getFakeLogPrint().c_str()); +} + +TEST_F(TombstoneTest, multiple_maps_fault_address_before) { + backtrace_map_t map; + + map.start = 0xa434000; + map.end = 0xa435000; + map.offset = 0x1000; + map.load_base = 0xd000; + map.flags = PROT_WRITE; + map_mock_->AddMap(map); + + map.start = 0xa534000; + map.end = 0xa535000; + map.offset = 0x3000; + map.load_base = 0x2000; + map.flags = PROT_EXEC; + map_mock_->AddMap(map); + + map.start = 0xa634000; + map.end = 0xa635000; + map.offset = 0; + map.load_base = 0; + map.flags = PROT_READ | PROT_WRITE | PROT_EXEC; + map.name = "/system/lib/fake.so"; + map_mock_->AddMap(map); + + siginfo_t si; + si.si_signo = SIGBUS; + si.si_addr = reinterpret_cast<void*>(0x1000); + ptrace_set_fake_getsiginfo(si); + dump_all_maps(backtrace_mock_.get(), map_mock_.get(), &log_, 100); + + std::string tombstone_contents; + ASSERT_TRUE(lseek(log_.tfd, 0, SEEK_SET) == 0); + ASSERT_TRUE(android::base::ReadFdToString(log_.tfd, &tombstone_contents)); + const char* expected_dump = \ +"\nmemory map: (fault address prefixed with --->)\n" +#if defined(__LP64__) +"--->Fault address falls at 00000000'00001000 before any mapped regions\n" +" 00000000'0a434000-00000000'0a434fff -w- 1000 1000 (load base 0xd000)\n" +" 00000000'0a534000-00000000'0a534fff --x 3000 1000 (load base 0x2000)\n" +" 00000000'0a634000-00000000'0a634fff rwx 0 1000 /system/lib/fake.so\n"; +#else +"--->Fault address falls at 00001000 before any mapped regions\n" +" 0a434000-0a434fff -w- 1000 1000 (load base 0xd000)\n" +" 0a534000-0a534fff --x 3000 1000 (load base 0x2000)\n" +" 0a634000-0a634fff rwx 0 1000 /system/lib/fake.so\n"; +#endif + ASSERT_STREQ(expected_dump, tombstone_contents.c_str()); + + ASSERT_STREQ("", amfd_data_.c_str()); + + // Verify that the log buf is empty, and no error messages. + ASSERT_STREQ("", getFakeLogBuf().c_str()); + ASSERT_STREQ("", getFakeLogPrint().c_str()); +} + +TEST_F(TombstoneTest, multiple_maps_fault_address_between) { + backtrace_map_t map; + + map.start = 0xa434000; + map.end = 0xa435000; + map.offset = 0x1000; + map.load_base = 0xd000; + map.flags = PROT_WRITE; + map_mock_->AddMap(map); + + map.start = 0xa534000; + map.end = 0xa535000; + map.offset = 0x3000; + map.load_base = 0x2000; + map.flags = PROT_EXEC; + map_mock_->AddMap(map); + + map.start = 0xa634000; + map.end = 0xa635000; + map.offset = 0; + map.load_base = 0; + map.flags = PROT_READ | PROT_WRITE | PROT_EXEC; + map.name = "/system/lib/fake.so"; + map_mock_->AddMap(map); + + siginfo_t si; + si.si_signo = SIGBUS; + si.si_addr = reinterpret_cast<void*>(0xa533000); + ptrace_set_fake_getsiginfo(si); + dump_all_maps(backtrace_mock_.get(), map_mock_.get(), &log_, 100); + + std::string tombstone_contents; + ASSERT_TRUE(lseek(log_.tfd, 0, SEEK_SET) == 0); + ASSERT_TRUE(android::base::ReadFdToString(log_.tfd, &tombstone_contents)); + const char* expected_dump = \ +"\nmemory map: (fault address prefixed with --->)\n" +#if defined(__LP64__) +" 00000000'0a434000-00000000'0a434fff -w- 1000 1000 (load base 0xd000)\n" +"--->Fault address falls at 00000000'0a533000 between mapped regions\n" +" 00000000'0a534000-00000000'0a534fff --x 3000 1000 (load base 0x2000)\n" +" 00000000'0a634000-00000000'0a634fff rwx 0 1000 /system/lib/fake.so\n"; +#else +" 0a434000-0a434fff -w- 1000 1000 (load base 0xd000)\n" +"--->Fault address falls at 0a533000 between mapped regions\n" +" 0a534000-0a534fff --x 3000 1000 (load base 0x2000)\n" +" 0a634000-0a634fff rwx 0 1000 /system/lib/fake.so\n"; +#endif + ASSERT_STREQ(expected_dump, tombstone_contents.c_str()); + + ASSERT_STREQ("", amfd_data_.c_str()); + + // Verify that the log buf is empty, and no error messages. + ASSERT_STREQ("", getFakeLogBuf().c_str()); + ASSERT_STREQ("", getFakeLogPrint().c_str()); +} + +TEST_F(TombstoneTest, multiple_maps_fault_address_in_map) { + backtrace_map_t map; + + map.start = 0xa434000; + map.end = 0xa435000; + map.offset = 0x1000; + map.load_base = 0xd000; + map.flags = PROT_WRITE; + map_mock_->AddMap(map); + + map.start = 0xa534000; + map.end = 0xa535000; + map.offset = 0x3000; + map.load_base = 0x2000; + map.flags = PROT_EXEC; + map_mock_->AddMap(map); + + map.start = 0xa634000; + map.end = 0xa635000; + map.offset = 0; + map.load_base = 0; + map.flags = PROT_READ | PROT_WRITE | PROT_EXEC; + map.name = "/system/lib/fake.so"; + map_mock_->AddMap(map); + + siginfo_t si; + si.si_signo = SIGBUS; + si.si_addr = reinterpret_cast<void*>(0xa534040); + ptrace_set_fake_getsiginfo(si); + dump_all_maps(backtrace_mock_.get(), map_mock_.get(), &log_, 100); + + std::string tombstone_contents; + ASSERT_TRUE(lseek(log_.tfd, 0, SEEK_SET) == 0); + ASSERT_TRUE(android::base::ReadFdToString(log_.tfd, &tombstone_contents)); + const char* expected_dump = \ +"\nmemory map: (fault address prefixed with --->)\n" +#if defined(__LP64__) +" 00000000'0a434000-00000000'0a434fff -w- 1000 1000 (load base 0xd000)\n" +"--->00000000'0a534000-00000000'0a534fff --x 3000 1000 (load base 0x2000)\n" +" 00000000'0a634000-00000000'0a634fff rwx 0 1000 /system/lib/fake.so\n"; +#else +" 0a434000-0a434fff -w- 1000 1000 (load base 0xd000)\n" +"--->0a534000-0a534fff --x 3000 1000 (load base 0x2000)\n" +" 0a634000-0a634fff rwx 0 1000 /system/lib/fake.so\n"; +#endif + ASSERT_STREQ(expected_dump, tombstone_contents.c_str()); + + ASSERT_STREQ("", amfd_data_.c_str()); + + // Verify that the log buf is empty, and no error messages. + ASSERT_STREQ("", getFakeLogBuf().c_str()); + ASSERT_STREQ("", getFakeLogPrint().c_str()); +} + +TEST_F(TombstoneTest, multiple_maps_fault_address_after) { + backtrace_map_t map; + + map.start = 0xa434000; + map.end = 0xa435000; + map.offset = 0x1000; + map.load_base = 0xd000; + map.flags = PROT_WRITE; + map_mock_->AddMap(map); + + map.start = 0xa534000; + map.end = 0xa535000; + map.offset = 0x3000; + map.load_base = 0x2000; + map.flags = PROT_EXEC; + map_mock_->AddMap(map); + + map.start = 0xa634000; + map.end = 0xa635000; + map.offset = 0; + map.load_base = 0; + map.flags = PROT_READ | PROT_WRITE | PROT_EXEC; + map.name = "/system/lib/fake.so"; + map_mock_->AddMap(map); + + siginfo_t si; + si.si_signo = SIGBUS; +#if defined(__LP64__) + si.si_addr = reinterpret_cast<void*>(0x12345a534040UL); +#else + si.si_addr = reinterpret_cast<void*>(0xf534040UL); +#endif + ptrace_set_fake_getsiginfo(si); + dump_all_maps(backtrace_mock_.get(), map_mock_.get(), &log_, 100); + + std::string tombstone_contents; + ASSERT_TRUE(lseek(log_.tfd, 0, SEEK_SET) == 0); + ASSERT_TRUE(android::base::ReadFdToString(log_.tfd, &tombstone_contents)); + const char* expected_dump = \ +"\nmemory map: (fault address prefixed with --->)\n" +#if defined(__LP64__) +" 00000000'0a434000-00000000'0a434fff -w- 1000 1000 (load base 0xd000)\n" +" 00000000'0a534000-00000000'0a534fff --x 3000 1000 (load base 0x2000)\n" +" 00000000'0a634000-00000000'0a634fff rwx 0 1000 /system/lib/fake.so\n" +"--->Fault address falls at 00001234'5a534040 after any mapped regions\n"; +#else +" 0a434000-0a434fff -w- 1000 1000 (load base 0xd000)\n" +" 0a534000-0a534fff --x 3000 1000 (load base 0x2000)\n" +" 0a634000-0a634fff rwx 0 1000 /system/lib/fake.so\n" +"--->Fault address falls at 0f534040 after any mapped regions\n"; +#endif + ASSERT_STREQ(expected_dump, tombstone_contents.c_str()); + + ASSERT_STREQ("", amfd_data_.c_str()); + + // Verify that the log buf is empty, and no error messages. + ASSERT_STREQ("", getFakeLogBuf().c_str()); + ASSERT_STREQ("", getFakeLogPrint().c_str()); +} + +TEST_F(TombstoneTest, multiple_maps_getsiginfo_fail) { + backtrace_map_t map; + + map.start = 0xa434000; + map.end = 0xa435000; + map.offset = 0x1000; + map.load_base = 0xd000; + map.flags = PROT_WRITE; + map_mock_->AddMap(map); + + siginfo_t si; + si.si_signo = 0; + ptrace_set_fake_getsiginfo(si); + dump_all_maps(backtrace_mock_.get(), map_mock_.get(), &log_, 100); + + std::string tombstone_contents; + ASSERT_TRUE(lseek(log_.tfd, 0, SEEK_SET) == 0); + ASSERT_TRUE(android::base::ReadFdToString(log_.tfd, &tombstone_contents)); + const char* expected_dump = \ +"\nmemory map:\n" +#if defined(__LP64__) +" 00000000'0a434000-00000000'0a434fff -w- 1000 1000 (load base 0xd000)\n"; +#else +" 0a434000-0a434fff -w- 1000 1000 (load base 0xd000)\n"; +#endif + ASSERT_STREQ(expected_dump, tombstone_contents.c_str()); + + ASSERT_STREQ("", amfd_data_.c_str()); + + // Verify that the log buf is empty, and no error messages. + ASSERT_STREQ("", getFakeLogBuf().c_str()); + ASSERT_STREQ("6 DEBUG Cannot get siginfo for 100: Bad address\n\n", getFakeLogPrint().c_str()); +} + +TEST_F(TombstoneTest, multiple_maps_check_signal_has_si_addr) { + backtrace_map_t map; + + map.start = 0xa434000; + map.end = 0xa435000; + map.flags = PROT_WRITE; + map_mock_->AddMap(map); + + for (int i = 1; i < 255; i++) { + ASSERT_TRUE(ftruncate(log_.tfd, 0) == 0); + ASSERT_TRUE(lseek(log_.tfd, 0, SEEK_SET) == 0); + + siginfo_t si; + si.si_signo = i; + si.si_addr = reinterpret_cast<void*>(0x1000); + ptrace_set_fake_getsiginfo(si); + dump_all_maps(backtrace_mock_.get(), map_mock_.get(), &log_, 100); + + std::string tombstone_contents; + ASSERT_TRUE(lseek(log_.tfd, 0, SEEK_SET) == 0); + ASSERT_TRUE(android::base::ReadFdToString(log_.tfd, &tombstone_contents)); + bool has_addr = false; + switch (si.si_signo) { + case SIGBUS: + case SIGFPE: + case SIGILL: + case SIGSEGV: + case SIGTRAP: + has_addr = true; + break; + } + + const char* expected_addr_dump = \ +"\nmemory map: (fault address prefixed with --->)\n" +#if defined(__LP64__) +"--->Fault address falls at 00000000'00001000 before any mapped regions\n" +" 00000000'0a434000-00000000'0a434fff -w- 0 1000\n"; +#else +"--->Fault address falls at 00001000 before any mapped regions\n" +" 0a434000-0a434fff -w- 0 1000\n"; +#endif + const char* expected_dump = \ +"\nmemory map:\n" +#if defined(__LP64__) +" 00000000'0a434000-00000000'0a434fff -w- 0 1000\n"; +#else +" 0a434000-0a434fff -w- 0 1000\n"; +#endif + if (has_addr) { + ASSERT_STREQ(expected_addr_dump, tombstone_contents.c_str()) + << "Signal " << si.si_signo << " expected to include an address."; + } else { + ASSERT_STREQ(expected_dump, tombstone_contents.c_str()) + << "Signal " << si.si_signo << " is not expected to include an address."; + } + + ASSERT_STREQ("", amfd_data_.c_str()); + + // Verify that the log buf is empty, and no error messages. + ASSERT_STREQ("", getFakeLogBuf().c_str()); + ASSERT_STREQ("", getFakeLogPrint().c_str()); + } +} + +TEST_F(TombstoneTest, dump_signal_info_error) { + siginfo_t si; + si.si_signo = 0; + ptrace_set_fake_getsiginfo(si); + + dump_signal_info(&log_, 123); + + std::string tombstone_contents; + ASSERT_TRUE(lseek(log_.tfd, 0, SEEK_SET) == 0); + ASSERT_TRUE(android::base::ReadFdToString(log_.tfd, &tombstone_contents)); + ASSERT_STREQ("", tombstone_contents.c_str()); + + ASSERT_STREQ("", getFakeLogBuf().c_str()); + ASSERT_STREQ("6 DEBUG cannot get siginfo: Bad address\n\n", getFakeLogPrint().c_str()); + + ASSERT_STREQ("", amfd_data_.c_str()); +} + +TEST_F(TombstoneTest, dump_log_file_error) { + log_.should_retrieve_logcat = true; + dump_log_file(&log_, 123, "/fake/filename", 10); + + std::string tombstone_contents; + ASSERT_TRUE(lseek(log_.tfd, 0, SEEK_SET) == 0); + ASSERT_TRUE(android::base::ReadFdToString(log_.tfd, &tombstone_contents)); + ASSERT_STREQ("", tombstone_contents.c_str()); + + ASSERT_STREQ("", getFakeLogBuf().c_str()); + ASSERT_STREQ("6 DEBUG Unable to open /fake/filename: Permission denied\n\n", + getFakeLogPrint().c_str()); + + ASSERT_STREQ("", amfd_data_.c_str()); +} + +TEST_F(TombstoneTest, dump_header_info) { + dump_header_info(&log_); + + std::string expected = "Build fingerprint: 'unknown'\nRevision: 'unknown'\n"; + expected += android::base::StringPrintf("ABI: '%s'\n", ABI_STRING); + ASSERT_STREQ(expected.c_str(), amfd_data_.c_str()); +} diff --git a/debuggerd/libdebuggerd/tombstone.cpp b/debuggerd/libdebuggerd/tombstone.cpp new file mode 100644 index 000000000..01e9cf69b --- /dev/null +++ b/debuggerd/libdebuggerd/tombstone.cpp @@ -0,0 +1,724 @@ +/* + * Copyright (C) 2012-2014 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#define LOG_TAG "DEBUG" + +#include <dirent.h> +#include <errno.h> +#include <fcntl.h> +#include <inttypes.h> +#include <signal.h> +#include <stddef.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/ptrace.h> +#include <sys/stat.h> +#include <time.h> + +#include <memory> +#include <string> + +#include <android/log.h> +#include <android-base/stringprintf.h> +#include <backtrace/Backtrace.h> +#include <backtrace/BacktraceMap.h> +#include <cutils/properties.h> +#include <log/log.h> +#include <log/logprint.h> +#include <private/android_filesystem_config.h> + +#include "debuggerd/handler.h" + +#include "backtrace.h" +#include "elf_utils.h" +#include "machine.h" +#include "open_files_list.h" +#include "tombstone.h" + +#define STACK_WORDS 16 + +#define MAX_TOMBSTONES 10 +#define TOMBSTONE_DIR "/data/tombstones" +#define TOMBSTONE_TEMPLATE (TOMBSTONE_DIR"/tombstone_%02d") + +static bool signal_has_si_addr(int si_signo, int si_code) { + // Manually sent signals won't have si_addr. + if (si_code == SI_USER || si_code == SI_QUEUE || si_code == SI_TKILL) { + return false; + } + + switch (si_signo) { + case SIGBUS: + case SIGFPE: + case SIGILL: + case SIGSEGV: + case SIGTRAP: + return true; + default: + return false; + } +} + +static const char* get_signame(int sig) { + switch(sig) { + case SIGABRT: return "SIGABRT"; + case SIGBUS: return "SIGBUS"; + case SIGFPE: return "SIGFPE"; + case SIGILL: return "SIGILL"; + case SIGSEGV: return "SIGSEGV"; +#if defined(SIGSTKFLT) + case SIGSTKFLT: return "SIGSTKFLT"; +#endif + case SIGSTOP: return "SIGSTOP"; + case SIGSYS: return "SIGSYS"; + case SIGTRAP: return "SIGTRAP"; + case DEBUGGER_SIGNAL: return "<debuggerd signal>"; + default: return "?"; + } +} + +static const char* get_sigcode(int signo, int code) { + // Try the signal-specific codes... + switch (signo) { + case SIGILL: + switch (code) { + case ILL_ILLOPC: return "ILL_ILLOPC"; + case ILL_ILLOPN: return "ILL_ILLOPN"; + case ILL_ILLADR: return "ILL_ILLADR"; + case ILL_ILLTRP: return "ILL_ILLTRP"; + case ILL_PRVOPC: return "ILL_PRVOPC"; + case ILL_PRVREG: return "ILL_PRVREG"; + case ILL_COPROC: return "ILL_COPROC"; + case ILL_BADSTK: return "ILL_BADSTK"; + } + static_assert(NSIGILL == ILL_BADSTK, "missing ILL_* si_code"); + break; + case SIGBUS: + switch (code) { + case BUS_ADRALN: return "BUS_ADRALN"; + case BUS_ADRERR: return "BUS_ADRERR"; + case BUS_OBJERR: return "BUS_OBJERR"; + case BUS_MCEERR_AR: return "BUS_MCEERR_AR"; + case BUS_MCEERR_AO: return "BUS_MCEERR_AO"; + } + static_assert(NSIGBUS == BUS_MCEERR_AO, "missing BUS_* si_code"); + break; + case SIGFPE: + switch (code) { + case FPE_INTDIV: return "FPE_INTDIV"; + case FPE_INTOVF: return "FPE_INTOVF"; + case FPE_FLTDIV: return "FPE_FLTDIV"; + case FPE_FLTOVF: return "FPE_FLTOVF"; + case FPE_FLTUND: return "FPE_FLTUND"; + case FPE_FLTRES: return "FPE_FLTRES"; + case FPE_FLTINV: return "FPE_FLTINV"; + case FPE_FLTSUB: return "FPE_FLTSUB"; + } + static_assert(NSIGFPE == FPE_FLTSUB, "missing FPE_* si_code"); + break; + case SIGSEGV: + switch (code) { + case SEGV_MAPERR: return "SEGV_MAPERR"; + case SEGV_ACCERR: return "SEGV_ACCERR"; +#if defined(SEGV_BNDERR) + case SEGV_BNDERR: return "SEGV_BNDERR"; +#endif +#if defined(SEGV_PKUERR) + case SEGV_PKUERR: return "SEGV_PKUERR"; +#endif + } +#if defined(SEGV_PKUERR) + static_assert(NSIGSEGV == SEGV_PKUERR, "missing SEGV_* si_code"); +#elif defined(SEGV_BNDERR) + static_assert(NSIGSEGV == SEGV_BNDERR, "missing SEGV_* si_code"); +#else + static_assert(NSIGSEGV == SEGV_ACCERR, "missing SEGV_* si_code"); +#endif + break; +#if defined(SYS_SECCOMP) // Our glibc is too old, and we build this for the host too. + case SIGSYS: + switch (code) { + case SYS_SECCOMP: return "SYS_SECCOMP"; + } + static_assert(NSIGSYS == SYS_SECCOMP, "missing SYS_* si_code"); + break; +#endif + case SIGTRAP: + switch (code) { + case TRAP_BRKPT: return "TRAP_BRKPT"; + case TRAP_TRACE: return "TRAP_TRACE"; + case TRAP_BRANCH: return "TRAP_BRANCH"; + case TRAP_HWBKPT: return "TRAP_HWBKPT"; + } + static_assert(NSIGTRAP == TRAP_HWBKPT, "missing TRAP_* si_code"); + break; + } + // Then the other codes... + switch (code) { + case SI_USER: return "SI_USER"; + case SI_KERNEL: return "SI_KERNEL"; + case SI_QUEUE: return "SI_QUEUE"; + case SI_TIMER: return "SI_TIMER"; + case SI_MESGQ: return "SI_MESGQ"; + case SI_ASYNCIO: return "SI_ASYNCIO"; + case SI_SIGIO: return "SI_SIGIO"; + case SI_TKILL: return "SI_TKILL"; + case SI_DETHREAD: return "SI_DETHREAD"; + } + // Then give up... + return "?"; +} + +static void dump_header_info(log_t* log) { + char fingerprint[PROPERTY_VALUE_MAX]; + char revision[PROPERTY_VALUE_MAX]; + + property_get("ro.build.fingerprint", fingerprint, "unknown"); + property_get("ro.revision", revision, "unknown"); + + _LOG(log, logtype::HEADER, "Build fingerprint: '%s'\n", fingerprint); + _LOG(log, logtype::HEADER, "Revision: '%s'\n", revision); + _LOG(log, logtype::HEADER, "ABI: '%s'\n", ABI_STRING); +} + +static void dump_signal_info(log_t* log, pid_t tid) { + siginfo_t si; + memset(&si, 0, sizeof(si)); + if (ptrace(PTRACE_GETSIGINFO, tid, 0, &si) == -1) { + ALOGE("cannot get siginfo: %s\n", strerror(errno)); + return; + } + + char addr_desc[32]; // ", fault addr 0x1234" + if (signal_has_si_addr(si.si_signo, si.si_code)) { + snprintf(addr_desc, sizeof(addr_desc), "%p", si.si_addr); + } else { + snprintf(addr_desc, sizeof(addr_desc), "--------"); + } + + _LOG(log, logtype::HEADER, "signal %d (%s), code %d (%s), fault addr %s\n", si.si_signo, + get_signame(si.si_signo), si.si_code, get_sigcode(si.si_signo, si.si_code), addr_desc); +} + +static void dump_thread_info(log_t* log, pid_t pid, pid_t tid) { + char path[64]; + char threadnamebuf[1024]; + char* threadname = nullptr; + FILE *fp; + + snprintf(path, sizeof(path), "/proc/%d/comm", tid); + if ((fp = fopen(path, "r"))) { + threadname = fgets(threadnamebuf, sizeof(threadnamebuf), fp); + fclose(fp); + if (threadname) { + size_t len = strlen(threadname); + if (len && threadname[len - 1] == '\n') { + threadname[len - 1] = '\0'; + } + } + } + // Blacklist logd, logd.reader, logd.writer, logd.auditd, logd.control ... + static const char logd[] = "logd"; + if (threadname != nullptr && !strncmp(threadname, logd, sizeof(logd) - 1) + && (!threadname[sizeof(logd) - 1] || (threadname[sizeof(logd) - 1] == '.'))) { + log->should_retrieve_logcat = false; + } + + char procnamebuf[1024]; + char* procname = nullptr; + + snprintf(path, sizeof(path), "/proc/%d/cmdline", pid); + if ((fp = fopen(path, "r"))) { + procname = fgets(procnamebuf, sizeof(procnamebuf), fp); + fclose(fp); + } + + _LOG(log, logtype::HEADER, "pid: %d, tid: %d, name: %s >>> %s <<<\n", pid, tid, + threadname ? threadname : "UNKNOWN", procname ? procname : "UNKNOWN"); +} + +static void dump_stack_segment( + Backtrace* backtrace, log_t* log, uintptr_t* sp, size_t words, int label) { + // Read the data all at once. + word_t stack_data[words]; + size_t bytes_read = backtrace->Read(*sp, reinterpret_cast<uint8_t*>(&stack_data[0]), sizeof(word_t) * words); + words = bytes_read / sizeof(word_t); + std::string line; + for (size_t i = 0; i < words; i++) { + line = " "; + if (i == 0 && label >= 0) { + // Print the label once. + line += android::base::StringPrintf("#%02d ", label); + } else { + line += " "; + } + line += android::base::StringPrintf("%" PRIPTR " %" PRIPTR, *sp, stack_data[i]); + + backtrace_map_t map; + backtrace->FillInMap(stack_data[i], &map); + if (BacktraceMap::IsValid(map) && !map.name.empty()) { + line += " " + map.name; + uintptr_t offset = 0; + std::string func_name(backtrace->GetFunctionName(stack_data[i], &offset)); + if (!func_name.empty()) { + line += " (" + func_name; + if (offset) { + line += android::base::StringPrintf("+%" PRIuPTR, offset); + } + line += ')'; + } + } + _LOG(log, logtype::STACK, "%s\n", line.c_str()); + + *sp += sizeof(word_t); + } +} + +static void dump_stack(Backtrace* backtrace, log_t* log) { + size_t first = 0, last; + for (size_t i = 0; i < backtrace->NumFrames(); i++) { + const backtrace_frame_data_t* frame = backtrace->GetFrame(i); + if (frame->sp) { + if (!first) { + first = i+1; + } + last = i; + } + } + if (!first) { + return; + } + first--; + + // Dump a few words before the first frame. + word_t sp = backtrace->GetFrame(first)->sp - STACK_WORDS * sizeof(word_t); + dump_stack_segment(backtrace, log, &sp, STACK_WORDS, -1); + + // Dump a few words from all successive frames. + // Only log the first 3 frames, put the rest in the tombstone. + for (size_t i = first; i <= last; i++) { + const backtrace_frame_data_t* frame = backtrace->GetFrame(i); + if (sp != frame->sp) { + _LOG(log, logtype::STACK, " ........ ........\n"); + sp = frame->sp; + } + if (i == last) { + dump_stack_segment(backtrace, log, &sp, STACK_WORDS, i); + if (sp < frame->sp + frame->stack_size) { + _LOG(log, logtype::STACK, " ........ ........\n"); + } + } else { + size_t words = frame->stack_size / sizeof(word_t); + if (words == 0) { + words = 1; + } else if (words > STACK_WORDS) { + words = STACK_WORDS; + } + dump_stack_segment(backtrace, log, &sp, words, i); + } + } +} + +static std::string get_addr_string(uintptr_t addr) { + std::string addr_str; +#if defined(__LP64__) + addr_str = android::base::StringPrintf("%08x'%08x", + static_cast<uint32_t>(addr >> 32), + static_cast<uint32_t>(addr & 0xffffffff)); +#else + addr_str = android::base::StringPrintf("%08x", addr); +#endif + return addr_str; +} + +static void dump_abort_message(Backtrace* backtrace, log_t* log, uintptr_t address) { + if (address == 0) { + return; + } + + address += sizeof(size_t); // Skip the buffer length. + + char msg[512]; + memset(msg, 0, sizeof(msg)); + char* p = &msg[0]; + while (p < &msg[sizeof(msg)]) { + word_t data; + size_t len = sizeof(word_t); + if (!backtrace->ReadWord(address, &data)) { + break; + } + address += sizeof(word_t); + + while (len > 0 && (*p++ = (data >> (sizeof(word_t) - len) * 8) & 0xff) != 0) { + len--; + } + } + msg[sizeof(msg) - 1] = '\0'; + + _LOG(log, logtype::HEADER, "Abort message: '%s'\n", msg); +} + +static void dump_all_maps(Backtrace* backtrace, BacktraceMap* map, log_t* log, pid_t tid) { + bool print_fault_address_marker = false; + uintptr_t addr = 0; + siginfo_t si; + memset(&si, 0, sizeof(si)); + if (ptrace(PTRACE_GETSIGINFO, tid, 0, &si) != -1) { + print_fault_address_marker = signal_has_si_addr(si.si_signo, si.si_code); + addr = reinterpret_cast<uintptr_t>(si.si_addr); + } else { + ALOGE("Cannot get siginfo for %d: %s\n", tid, strerror(errno)); + } + + ScopedBacktraceMapIteratorLock lock(map); + _LOG(log, logtype::MAPS, "\n"); + if (!print_fault_address_marker) { + _LOG(log, logtype::MAPS, "memory map:\n"); + } else { + _LOG(log, logtype::MAPS, "memory map: (fault address prefixed with --->)\n"); + if (map->begin() != map->end() && addr < map->begin()->start) { + _LOG(log, logtype::MAPS, "--->Fault address falls at %s before any mapped regions\n", + get_addr_string(addr).c_str()); + print_fault_address_marker = false; + } + } + + std::string line; + for (BacktraceMap::const_iterator it = map->begin(); it != map->end(); ++it) { + line = " "; + if (print_fault_address_marker) { + if (addr < it->start) { + _LOG(log, logtype::MAPS, "--->Fault address falls at %s between mapped regions\n", + get_addr_string(addr).c_str()); + print_fault_address_marker = false; + } else if (addr >= it->start && addr < it->end) { + line = "--->"; + print_fault_address_marker = false; + } + } + line += get_addr_string(it->start) + '-' + get_addr_string(it->end - 1) + ' '; + if (it->flags & PROT_READ) { + line += 'r'; + } else { + line += '-'; + } + if (it->flags & PROT_WRITE) { + line += 'w'; + } else { + line += '-'; + } + if (it->flags & PROT_EXEC) { + line += 'x'; + } else { + line += '-'; + } + line += android::base::StringPrintf(" %8" PRIxPTR " %8" PRIxPTR, + it->offset, it->end - it->start); + bool space_needed = true; + if (it->name.length() > 0) { + space_needed = false; + line += " " + it->name; + std::string build_id; + if ((it->flags & PROT_READ) && elf_get_build_id(backtrace, it->start, &build_id)) { + line += " (BuildId: " + build_id + ")"; + } + } + if (it->load_base != 0) { + if (space_needed) { + line += ' '; + } + line += android::base::StringPrintf(" (load base 0x%" PRIxPTR ")", it->load_base); + } + _LOG(log, logtype::MAPS, "%s\n", line.c_str()); + } + if (print_fault_address_marker) { + _LOG(log, logtype::MAPS, "--->Fault address falls at %s after any mapped regions\n", + get_addr_string(addr).c_str()); + } +} + +static void dump_backtrace_and_stack(Backtrace* backtrace, log_t* log) { + if (backtrace->NumFrames()) { + _LOG(log, logtype::BACKTRACE, "\nbacktrace:\n"); + dump_backtrace_to_log(backtrace, log, " "); + + _LOG(log, logtype::STACK, "\nstack:\n"); + dump_stack(backtrace, log); + } +} + +static void dump_thread(log_t* log, pid_t pid, pid_t tid, BacktraceMap* map, + uintptr_t abort_msg_address, bool primary_thread) { + log->current_tid = tid; + if (!primary_thread) { + _LOG(log, logtype::THREAD, "--- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---\n"); + } + dump_thread_info(log, pid, tid); + dump_signal_info(log, tid); + + std::unique_ptr<Backtrace> backtrace(Backtrace::Create(pid, tid, map)); + if (primary_thread) { + dump_abort_message(backtrace.get(), log, abort_msg_address); + } + dump_registers(log, tid); + if (backtrace->Unwind(0)) { + dump_backtrace_and_stack(backtrace.get(), log); + } else { + ALOGE("Unwind failed: pid = %d, tid = %d", pid, tid); + } + + if (primary_thread) { + dump_memory_and_code(log, backtrace.get()); + if (map) { + dump_all_maps(backtrace.get(), map, log, tid); + } + } + + log->current_tid = log->crashed_tid; +} + +// Reads the contents of the specified log device, filters out the entries +// that don't match the specified pid, and writes them to the tombstone file. +// +// If "tail" is non-zero, log the last "tail" number of lines. +static EventTagMap* g_eventTagMap = NULL; + +static void dump_log_file( + log_t* log, pid_t pid, const char* filename, unsigned int tail) { + bool first = true; + struct logger_list* logger_list; + + if (!log->should_retrieve_logcat) { + return; + } + + logger_list = android_logger_list_open( + android_name_to_log_id(filename), ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK, tail, pid); + + if (!logger_list) { + ALOGE("Unable to open %s: %s\n", filename, strerror(errno)); + return; + } + + struct log_msg log_entry; + + while (true) { + ssize_t actual = android_logger_list_read(logger_list, &log_entry); + struct logger_entry* entry; + + if (actual < 0) { + if (actual == -EINTR) { + // interrupted by signal, retry + continue; + } else if (actual == -EAGAIN) { + // non-blocking EOF; we're done + break; + } else { + ALOGE("Error while reading log: %s\n", strerror(-actual)); + break; + } + } else if (actual == 0) { + ALOGE("Got zero bytes while reading log: %s\n", strerror(errno)); + break; + } + + // NOTE: if you ALOGV something here, this will spin forever, + // because you will be writing as fast as you're reading. Any + // high-frequency debug diagnostics should just be written to + // the tombstone file. + + entry = &log_entry.entry_v1; + + if (first) { + _LOG(log, logtype::LOGS, "--------- %slog %s\n", + tail ? "tail end of " : "", filename); + first = false; + } + + // Msg format is: <priority:1><tag:N>\0<message:N>\0 + // + // We want to display it in the same format as "logcat -v threadtime" + // (although in this case the pid is redundant). + static const char* kPrioChars = "!.VDIWEFS"; + unsigned hdr_size = log_entry.entry.hdr_size; + if (!hdr_size) { + hdr_size = sizeof(log_entry.entry_v1); + } + if ((hdr_size < sizeof(log_entry.entry_v1)) || + (hdr_size > sizeof(log_entry.entry))) { + continue; + } + char* msg = reinterpret_cast<char*>(log_entry.buf) + hdr_size; + + char timeBuf[32]; + time_t sec = static_cast<time_t>(entry->sec); + struct tm tmBuf; + struct tm* ptm; + ptm = localtime_r(&sec, &tmBuf); + strftime(timeBuf, sizeof(timeBuf), "%m-%d %H:%M:%S", ptm); + + if (log_entry.id() == LOG_ID_EVENTS) { + if (!g_eventTagMap) { + g_eventTagMap = android_openEventTagMap(NULL); + } + AndroidLogEntry e; + char buf[512]; + android_log_processBinaryLogBuffer(entry, &e, g_eventTagMap, buf, sizeof(buf)); + _LOG(log, logtype::LOGS, "%s.%03d %5d %5d %c %-8.*s: %s\n", + timeBuf, entry->nsec / 1000000, entry->pid, entry->tid, + 'I', (int)e.tagLen, e.tag, e.message); + continue; + } + + unsigned char prio = msg[0]; + char* tag = msg + 1; + msg = tag + strlen(tag) + 1; + + // consume any trailing newlines + char* nl = msg + strlen(msg) - 1; + while (nl >= msg && *nl == '\n') { + *nl-- = '\0'; + } + + char prioChar = (prio < strlen(kPrioChars) ? kPrioChars[prio] : '?'); + + // Look for line breaks ('\n') and display each text line + // on a separate line, prefixed with the header, like logcat does. + do { + nl = strchr(msg, '\n'); + if (nl) { + *nl = '\0'; + ++nl; + } + + _LOG(log, logtype::LOGS, "%s.%03d %5d %5d %c %-8s: %s\n", + timeBuf, entry->nsec / 1000000, entry->pid, entry->tid, + prioChar, tag, msg); + } while ((msg = nl)); + } + + android_logger_list_free(logger_list); +} + +// Dumps the logs generated by the specified pid to the tombstone, from both +// "system" and "main" log devices. Ideally we'd interleave the output. +static void dump_logs(log_t* log, pid_t pid, unsigned int tail) { + dump_log_file(log, pid, "system", tail); + dump_log_file(log, pid, "main", tail); +} + +// Dumps all information about the specified pid to the tombstone. +static void dump_crash(log_t* log, BacktraceMap* map, + const OpenFilesList& open_files, pid_t pid, pid_t tid, + const std::set<pid_t>& siblings, uintptr_t abort_msg_address) { + // don't copy log messages to tombstone unless this is a dev device + char value[PROPERTY_VALUE_MAX]; + property_get("ro.debuggable", value, "0"); + bool want_logs = (value[0] == '1'); + + _LOG(log, logtype::HEADER, + "*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***\n"); + dump_header_info(log); + dump_thread(log, pid, tid, map, abort_msg_address, true); + if (want_logs) { + dump_logs(log, pid, 5); + } + + if (!siblings.empty()) { + for (pid_t sibling : siblings) { + dump_thread(log, pid, sibling, map, 0, false); + } + } + + _LOG(log, logtype::OPEN_FILES, "\nopen files:\n"); + dump_open_files_list_to_log(open_files, log, " "); + + if (want_logs) { + dump_logs(log, pid, 0); + } +} + +// open_tombstone - find an available tombstone slot, if any, of the +// form tombstone_XX where XX is 00 to MAX_TOMBSTONES-1, inclusive. If no +// file is available, we reuse the least-recently-modified file. +int open_tombstone(std::string* out_path) { + // In a single pass, find an available slot and, in case none + // exist, find and record the least-recently-modified file. + char path[128]; + int fd = -1; + int oldest = -1; + struct stat oldest_sb; + for (int i = 0; i < MAX_TOMBSTONES; i++) { + snprintf(path, sizeof(path), TOMBSTONE_TEMPLATE, i); + + struct stat sb; + if (stat(path, &sb) == 0) { + if (oldest < 0 || sb.st_mtime < oldest_sb.st_mtime) { + oldest = i; + oldest_sb.st_mtime = sb.st_mtime; + } + continue; + } + if (errno != ENOENT) continue; + + fd = open(path, O_CREAT | O_EXCL | O_WRONLY | O_NOFOLLOW | O_CLOEXEC, 0600); + if (fd < 0) continue; // raced ? + + if (out_path) { + *out_path = path; + } + fchown(fd, AID_SYSTEM, AID_SYSTEM); + return fd; + } + + if (oldest < 0) { + ALOGE("debuggerd: failed to find a valid tombstone, default to using tombstone 0.\n"); + oldest = 0; + } + + // we didn't find an available file, so we clobber the oldest one + snprintf(path, sizeof(path), TOMBSTONE_TEMPLATE, oldest); + fd = open(path, O_CREAT | O_TRUNC | O_WRONLY | O_NOFOLLOW | O_CLOEXEC, 0600); + if (fd < 0) { + ALOGE("debuggerd: failed to open tombstone file '%s': %s\n", path, strerror(errno)); + return -1; + } + + if (out_path) { + *out_path = path; + } + fchown(fd, AID_SYSTEM, AID_SYSTEM); + return fd; +} + +void engrave_tombstone(int tombstone_fd, BacktraceMap* map, + const OpenFilesList& open_files, pid_t pid, pid_t tid, + const std::set<pid_t>& siblings, uintptr_t abort_msg_address, + std::string* amfd_data) { + log_t log; + log.current_tid = tid; + log.crashed_tid = tid; + + if (tombstone_fd < 0) { + ALOGE("debuggerd: skipping tombstone write, nothing to do.\n"); + return; + } + + log.tfd = tombstone_fd; + log.amfd_data = amfd_data; + dump_crash(&log, map, open_files, pid, tid, siblings, abort_msg_address); +} diff --git a/debuggerd/libdebuggerd/utility.cpp b/debuggerd/libdebuggerd/utility.cpp new file mode 100644 index 000000000..57209aaef --- /dev/null +++ b/debuggerd/libdebuggerd/utility.cpp @@ -0,0 +1,203 @@ +/* + * Copyright 2008, The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#define LOG_TAG "DEBUG" + +#include "utility.h" + +#include <errno.h> +#include <signal.h> +#include <string.h> +#include <sys/ptrace.h> +#include <sys/wait.h> +#include <unistd.h> + +#include <string> + +#include <android-base/stringprintf.h> +#include <backtrace/Backtrace.h> +#include <log/log.h> + +// Whitelist output desired in the logcat output. +bool is_allowed_in_logcat(enum logtype ltype) { + if ((ltype == HEADER) + || (ltype == REGISTERS) + || (ltype == BACKTRACE)) { + return true; + } + return false; +} + +void _LOG(log_t* log, enum logtype ltype, const char* fmt, ...) { + bool write_to_tombstone = (log->tfd != -1); + bool write_to_logcat = is_allowed_in_logcat(ltype) + && log->crashed_tid != -1 + && log->current_tid != -1 + && (log->crashed_tid == log->current_tid); + + char buf[512]; + va_list ap; + va_start(ap, fmt); + vsnprintf(buf, sizeof(buf), fmt, ap); + va_end(ap); + + size_t len = strlen(buf); + if (len <= 0) { + return; + } + + if (write_to_tombstone) { + TEMP_FAILURE_RETRY(write(log->tfd, buf, len)); + } + + if (write_to_logcat) { + __android_log_buf_write(LOG_ID_CRASH, ANDROID_LOG_FATAL, LOG_TAG, buf); + if (log->amfd_data != nullptr) { + *log->amfd_data += buf; + } + } +} + +bool wait_for_signal(pid_t tid, siginfo_t* siginfo) { + while (true) { + int status; + pid_t n = TEMP_FAILURE_RETRY(waitpid(tid, &status, __WALL)); + if (n == -1) { + ALOGE("waitpid failed: tid %d, %s", tid, strerror(errno)); + return false; + } else if (n == tid) { + if (WIFSTOPPED(status)) { + if (ptrace(PTRACE_GETSIGINFO, tid, nullptr, siginfo) != 0) { + ALOGE("PTRACE_GETSIGINFO failed: %s", strerror(errno)); + return false; + } + return true; + } else { + ALOGE("unexpected waitpid response: n=%d, status=%08x\n", n, status); + // This is the only circumstance under which we can allow a detach + // to fail with ESRCH, which indicates the tid has exited. + return false; + } + } + } +} + +#define MEMORY_BYTES_TO_DUMP 256 +#define MEMORY_BYTES_PER_LINE 16 + +void dump_memory(log_t* log, Backtrace* backtrace, uintptr_t addr, const char* fmt, ...) { + std::string log_msg; + va_list ap; + va_start(ap, fmt); + android::base::StringAppendV(&log_msg, fmt, ap); + va_end(ap); + + // Align the address to sizeof(long) and start 32 bytes before the address. + addr &= ~(sizeof(long) - 1); + if (addr >= 4128) { + addr -= 32; + } + + // Don't bother if the address looks too low, or looks too high. + if (addr < 4096 || +#if defined(__LP64__) + addr > 0x4000000000000000UL - MEMORY_BYTES_TO_DUMP) { +#else + addr > 0xffff0000 - MEMORY_BYTES_TO_DUMP) { +#endif + return; + } + + _LOG(log, logtype::MEMORY, "\n%s\n", log_msg.c_str()); + + // Dump 256 bytes + uintptr_t data[MEMORY_BYTES_TO_DUMP/sizeof(uintptr_t)]; + memset(data, 0, MEMORY_BYTES_TO_DUMP); + size_t bytes = backtrace->Read(addr, reinterpret_cast<uint8_t*>(data), sizeof(data)); + if (bytes % sizeof(uintptr_t) != 0) { + // This should never happen, but just in case. + ALOGE("Bytes read %zu, is not a multiple of %zu", bytes, sizeof(uintptr_t)); + bytes &= ~(sizeof(uintptr_t) - 1); + } + + uintptr_t start = 0; + bool skip_2nd_read = false; + if (bytes == 0) { + // In this case, we might want to try another read at the beginning of + // the next page only if it's within the amount of memory we would have + // read. + size_t page_size = sysconf(_SC_PAGE_SIZE); + start = ((addr + (page_size - 1)) & ~(page_size - 1)) - addr; + if (start == 0 || start >= MEMORY_BYTES_TO_DUMP) { + skip_2nd_read = true; + } + } + + if (bytes < MEMORY_BYTES_TO_DUMP && !skip_2nd_read) { + // Try to do one more read. This could happen if a read crosses a map, + // but the maps do not have any break between them. Or it could happen + // if reading from an unreadable map, but the read would cross back + // into a readable map. Only requires one extra read because a map has + // to contain at least one page, and the total number of bytes to dump + // is smaller than a page. + size_t bytes2 = backtrace->Read(addr + start + bytes, reinterpret_cast<uint8_t*>(data) + bytes, + sizeof(data) - bytes - start); + bytes += bytes2; + if (bytes2 > 0 && bytes % sizeof(uintptr_t) != 0) { + // This should never happen, but we'll try and continue any way. + ALOGE("Bytes after second read %zu, is not a multiple of %zu", bytes, sizeof(uintptr_t)); + bytes &= ~(sizeof(uintptr_t) - 1); + } + } + + // Dump the code around memory as: + // addr contents ascii + // 0000000000008d34 ef000000e8bd0090 e1b00000512fff1e ............../Q + // 0000000000008d44 ea00b1f9e92d0090 e3a070fcef000000 ......-..p...... + // On 32-bit machines, there are still 16 bytes per line but addresses and + // words are of course presented differently. + uintptr_t* data_ptr = data; + size_t current = 0; + size_t total_bytes = start + bytes; + for (size_t line = 0; line < MEMORY_BYTES_TO_DUMP / MEMORY_BYTES_PER_LINE; line++) { + std::string logline; + android::base::StringAppendF(&logline, " %" PRIPTR, addr); + + addr += MEMORY_BYTES_PER_LINE; + std::string ascii; + for (size_t i = 0; i < MEMORY_BYTES_PER_LINE / sizeof(uintptr_t); i++) { + if (current >= start && current + sizeof(uintptr_t) <= total_bytes) { + android::base::StringAppendF(&logline, " %" PRIPTR, *data_ptr); + + // Fill out the ascii string from the data. + uint8_t* ptr = reinterpret_cast<uint8_t*>(data_ptr); + for (size_t val = 0; val < sizeof(uintptr_t); val++, ptr++) { + if (*ptr >= 0x20 && *ptr < 0x7f) { + ascii += *ptr; + } else { + ascii += '.'; + } + } + data_ptr++; + } else { + logline += ' ' + std::string(sizeof(uintptr_t) * 2, '-'); + ascii += std::string(sizeof(uintptr_t), '.'); + } + current += sizeof(uintptr_t); + } + _LOG(log, logtype::MEMORY, "%s %s\n", logline.c_str(), ascii.c_str()); + } +} diff --git a/debuggerd/libdebuggerd/x86/machine.cpp b/debuggerd/libdebuggerd/x86/machine.cpp new file mode 100644 index 000000000..af10817a4 --- /dev/null +++ b/debuggerd/libdebuggerd/x86/machine.cpp @@ -0,0 +1,62 @@ +/* + * Copyright 2006, The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#define LOG_TAG "DEBUG" + +#include <errno.h> +#include <stdint.h> +#include <string.h> +#include <sys/ptrace.h> + +#include <backtrace/Backtrace.h> +#include <log/log.h> + +#include "machine.h" +#include "utility.h" + +void dump_memory_and_code(log_t* log, Backtrace* backtrace) { + struct pt_regs r; + if (ptrace(PTRACE_GETREGS, backtrace->Tid(), 0, &r) == -1) { + ALOGE("cannot get registers: %s\n", strerror(errno)); + return; + } + + dump_memory(log, backtrace, static_cast<uintptr_t>(r.eax), "memory near eax:"); + dump_memory(log, backtrace, static_cast<uintptr_t>(r.ebx), "memory near ebx:"); + dump_memory(log, backtrace, static_cast<uintptr_t>(r.ecx), "memory near ecx:"); + dump_memory(log, backtrace, static_cast<uintptr_t>(r.edx), "memory near edx:"); + dump_memory(log, backtrace, static_cast<uintptr_t>(r.esi), "memory near esi:"); + dump_memory(log, backtrace, static_cast<uintptr_t>(r.edi), "memory near edi:"); + + dump_memory(log, backtrace, static_cast<uintptr_t>(r.eip), "code around eip:"); +} + +void dump_registers(log_t* log, pid_t tid) { + struct pt_regs r; + if (ptrace(PTRACE_GETREGS, tid, 0, &r) == -1) { + ALOGE("cannot get registers: %s\n", strerror(errno)); + return; + } + + _LOG(log, logtype::REGISTERS, " eax %08lx ebx %08lx ecx %08lx edx %08lx\n", + r.eax, r.ebx, r.ecx, r.edx); + _LOG(log, logtype::REGISTERS, " esi %08lx edi %08lx\n", + r.esi, r.edi); + _LOG(log, logtype::REGISTERS, " xcs %08x xds %08x xes %08x xfs %08x xss %08x\n", + r.xcs, r.xds, r.xes, r.xfs, r.xss); + _LOG(log, logtype::REGISTERS, " eip %08lx ebp %08lx esp %08lx flags %08lx\n", + r.eip, r.ebp, r.esp, r.eflags); +} diff --git a/debuggerd/libdebuggerd/x86_64/machine.cpp b/debuggerd/libdebuggerd/x86_64/machine.cpp new file mode 100644 index 000000000..bf2c2b4e0 --- /dev/null +++ b/debuggerd/libdebuggerd/x86_64/machine.cpp @@ -0,0 +1,67 @@ +/* +** Copyright 2013, The Android Open Source Project +** +** Licensed under the Apache License, Version 2.0 (the "License"); +** you may not use this file except in compliance with the License. +** You may obtain a copy of the License at +** +** http://www.apache.org/licenses/LICENSE-2.0 +** +** Unless required by applicable law or agreed to in writing, software +** distributed under the License is distributed on an "AS IS" BASIS, +** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +** See the License for the specific language governing permissions and +** limitations under the License. +*/ + +#define LOG_TAG "DEBUG" + +#include <errno.h> +#include <stdint.h> +#include <string.h> +#include <sys/ptrace.h> +#include <sys/user.h> + +#include <backtrace/Backtrace.h> +#include <log/log.h> + +#include "machine.h" +#include "utility.h" + +void dump_memory_and_code(log_t* log, Backtrace* backtrace) { + struct user_regs_struct r; + if (ptrace(PTRACE_GETREGS, backtrace->Tid(), 0, &r) == -1) { + ALOGE("cannot get registers: %s\n", strerror(errno)); + return; + } + + dump_memory(log, backtrace, static_cast<uintptr_t>(r.rax), "memory near rax:"); + dump_memory(log, backtrace, static_cast<uintptr_t>(r.rbx), "memory near rbx:"); + dump_memory(log, backtrace, static_cast<uintptr_t>(r.rcx), "memory near rcx:"); + dump_memory(log, backtrace, static_cast<uintptr_t>(r.rdx), "memory near rdx:"); + dump_memory(log, backtrace, static_cast<uintptr_t>(r.rsi), "memory near rsi:"); + dump_memory(log, backtrace, static_cast<uintptr_t>(r.rdi), "memory near rdi:"); + + dump_memory(log, backtrace, static_cast<uintptr_t>(r.rip), "code around rip:"); +} + +void dump_registers(log_t* log, pid_t tid) { + struct user_regs_struct r; + if (ptrace(PTRACE_GETREGS, tid, 0, &r) == -1) { + ALOGE("cannot get registers: %s\n", strerror(errno)); + return; + } + + _LOG(log, logtype::REGISTERS, " rax %016lx rbx %016lx rcx %016lx rdx %016lx\n", + r.rax, r.rbx, r.rcx, r.rdx); + _LOG(log, logtype::REGISTERS, " rsi %016lx rdi %016lx\n", + r.rsi, r.rdi); + _LOG(log, logtype::REGISTERS, " r8 %016lx r9 %016lx r10 %016lx r11 %016lx\n", + r.r8, r.r9, r.r10, r.r11); + _LOG(log, logtype::REGISTERS, " r12 %016lx r13 %016lx r14 %016lx r15 %016lx\n", + r.r12, r.r13, r.r14, r.r15); + _LOG(log, logtype::REGISTERS, " cs %016lx ss %016lx\n", + r.cs, r.ss); + _LOG(log, logtype::REGISTERS, " rip %016lx rbp %016lx rsp %016lx eflags %016lx\n", + r.rip, r.rbp, r.rsp, r.eflags); +} |