| Kévin PETIT | abc60c2 | 2013-12-19 12:36:59 +0000 | [diff] [blame] | 1 | /* | 
|  | 2 | * | 
|  | 3 | * Copyright 2014, The Android Open Source Project | 
|  | 4 | * | 
|  | 5 | * Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 6 | * you may not use this file except in compliance with the License. | 
|  | 7 | * You may obtain a copy of the License at | 
|  | 8 | * | 
|  | 9 | *     http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 10 | * | 
|  | 11 | * Unless required by applicable law or agreed to in writing, software | 
|  | 12 | * distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 14 | * See the License for the specific language governing permissions and | 
|  | 15 | * limitations under the License. | 
|  | 16 | */ | 
|  | 17 |  | 
| Christopher Ferris | b36b592 | 2015-06-17 18:35:59 -0700 | [diff] [blame] | 18 | #define LOG_TAG "DEBUG" | 
|  | 19 |  | 
| Elliott Hughes | b40c503 | 2014-07-14 16:10:15 -0700 | [diff] [blame] | 20 | #include <elf.h> | 
| Kévin PETIT | abc60c2 | 2013-12-19 12:36:59 +0000 | [diff] [blame] | 21 | #include <errno.h> | 
| Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 22 | #include <stdint.h> | 
| Elliott Hughes | b40c503 | 2014-07-14 16:10:15 -0700 | [diff] [blame] | 23 | #include <string.h> | 
| Kévin PETIT | abc60c2 | 2013-12-19 12:36:59 +0000 | [diff] [blame] | 24 | #include <sys/ptrace.h> | 
| Kévin PETIT | abc60c2 | 2013-12-19 12:36:59 +0000 | [diff] [blame] | 25 | #include <sys/uio.h> | 
| Kévin PETIT | abc60c2 | 2013-12-19 12:36:59 +0000 | [diff] [blame] | 26 |  | 
| Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 27 | #include <backtrace/Backtrace.h> | 
| Christopher Ferris | b36b592 | 2015-06-17 18:35:59 -0700 | [diff] [blame] | 28 | #include <log/log.h> | 
| Kévin PETIT | abc60c2 | 2013-12-19 12:36:59 +0000 | [diff] [blame] | 29 |  | 
| Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 30 | #include "machine.h" | 
|  | 31 | #include "utility.h" | 
| Kévin PETIT | abc60c2 | 2013-12-19 12:36:59 +0000 | [diff] [blame] | 32 |  | 
| Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 33 | void dump_memory_and_code(log_t* log, Backtrace* backtrace) { | 
|  | 34 | struct user_pt_regs regs; | 
|  | 35 | struct iovec io; | 
|  | 36 | io.iov_base = ®s; | 
|  | 37 | io.iov_len = sizeof(regs); | 
| Kévin PETIT | abc60c2 | 2013-12-19 12:36:59 +0000 | [diff] [blame] | 38 |  | 
| Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 39 | if (ptrace(PTRACE_GETREGSET, backtrace->Tid(), reinterpret_cast<void*>(NT_PRSTATUS), &io) == -1) { | 
| Christopher Ferris | b36b592 | 2015-06-17 18:35:59 -0700 | [diff] [blame] | 40 | ALOGE("ptrace failed to get registers: %s", strerror(errno)); | 
| Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 41 | return; | 
|  | 42 | } | 
| Kévin PETIT | abc60c2 | 2013-12-19 12:36:59 +0000 | [diff] [blame] | 43 |  | 
| Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 44 | for (int reg = 0; reg < 31; reg++) { | 
|  | 45 | dump_memory(log, backtrace, regs.regs[reg], "memory near x%d:", reg); | 
|  | 46 | } | 
| Brigid Smith | 62ba489 | 2014-06-10 11:53:08 -0700 | [diff] [blame] | 47 |  | 
| Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 48 | dump_memory(log, backtrace, static_cast<uintptr_t>(regs.pc), "code around pc:"); | 
| Kévin PETIT | abc60c2 | 2013-12-19 12:36:59 +0000 | [diff] [blame] | 49 |  | 
| Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 50 | if (regs.pc != regs.sp) { | 
|  | 51 | dump_memory(log, backtrace, static_cast<uintptr_t>(regs.sp), "code around sp:"); | 
|  | 52 | } | 
| Kévin PETIT | abc60c2 | 2013-12-19 12:36:59 +0000 | [diff] [blame] | 53 | } | 
|  | 54 |  | 
| Brigid Smith | 62ba489 | 2014-06-10 11:53:08 -0700 | [diff] [blame] | 55 | void dump_registers(log_t* log, pid_t tid) { | 
| Kévin PETIT | abc60c2 | 2013-12-19 12:36:59 +0000 | [diff] [blame] | 56 | struct user_pt_regs r; | 
|  | 57 | struct iovec io; | 
|  | 58 | io.iov_base = &r; | 
|  | 59 | io.iov_len = sizeof(r); | 
|  | 60 |  | 
| Kévin PETIT | abc60c2 | 2013-12-19 12:36:59 +0000 | [diff] [blame] | 61 | if (ptrace(PTRACE_GETREGSET, tid, (void*) NT_PRSTATUS, (void*) &io) == -1) { | 
| Christopher Ferris | b36b592 | 2015-06-17 18:35:59 -0700 | [diff] [blame] | 62 | ALOGE("ptrace error: %s\n", strerror(errno)); | 
| Kévin PETIT | abc60c2 | 2013-12-19 12:36:59 +0000 | [diff] [blame] | 63 | return; | 
|  | 64 | } | 
|  | 65 |  | 
|  | 66 | for (int i = 0; i < 28; i += 4) { | 
| Brigid Smith | 62ba489 | 2014-06-10 11:53:08 -0700 | [diff] [blame] | 67 | _LOG(log, logtype::REGISTERS, | 
| Elliott Hughes | b40c503 | 2014-07-14 16:10:15 -0700 | [diff] [blame] | 68 | "    x%-2d  %016llx  x%-2d  %016llx  x%-2d  %016llx  x%-2d  %016llx\n", | 
|  | 69 | i, r.regs[i], | 
|  | 70 | i+1, r.regs[i+1], | 
|  | 71 | i+2, r.regs[i+2], | 
|  | 72 | i+3, r.regs[i+3]); | 
| Kévin PETIT | abc60c2 | 2013-12-19 12:36:59 +0000 | [diff] [blame] | 73 | } | 
|  | 74 |  | 
| Elliott Hughes | b40c503 | 2014-07-14 16:10:15 -0700 | [diff] [blame] | 75 | _LOG(log, logtype::REGISTERS, "    x28  %016llx  x29  %016llx  x30  %016llx\n", | 
|  | 76 | r.regs[28], r.regs[29], r.regs[30]); | 
| Kévin PETIT | abc60c2 | 2013-12-19 12:36:59 +0000 | [diff] [blame] | 77 |  | 
| Elliott Hughes | b40c503 | 2014-07-14 16:10:15 -0700 | [diff] [blame] | 78 | _LOG(log, logtype::REGISTERS, "    sp   %016llx  pc   %016llx  pstate %016llx\n", | 
|  | 79 | r.sp, r.pc, r.pstate); | 
| Kévin PETIT | abc60c2 | 2013-12-19 12:36:59 +0000 | [diff] [blame] | 80 |  | 
|  | 81 | struct user_fpsimd_state f; | 
|  | 82 | io.iov_base = &f; | 
|  | 83 | io.iov_len = sizeof(f); | 
|  | 84 |  | 
|  | 85 | if (ptrace(PTRACE_GETREGSET, tid, (void*) NT_PRFPREG, (void*) &io) == -1) { | 
| Christopher Ferris | b36b592 | 2015-06-17 18:35:59 -0700 | [diff] [blame] | 86 | ALOGE("ptrace error: %s\n", strerror(errno)); | 
| Kévin PETIT | abc60c2 | 2013-12-19 12:36:59 +0000 | [diff] [blame] | 87 | return; | 
|  | 88 | } | 
|  | 89 |  | 
| Elliott Hughes | b40c503 | 2014-07-14 16:10:15 -0700 | [diff] [blame] | 90 | for (int i = 0; i < 32; i += 2) { | 
|  | 91 | _LOG(log, logtype::FP_REGISTERS, | 
|  | 92 | "    v%-2d  %016" PRIx64 "%016" PRIx64 "  v%-2d  %016" PRIx64 "%016" PRIx64 "\n", | 
|  | 93 | i, | 
|  | 94 | static_cast<uint64_t>(f.vregs[i] >> 64), | 
|  | 95 | static_cast<uint64_t>(f.vregs[i]), | 
|  | 96 | i+1, | 
|  | 97 | static_cast<uint64_t>(f.vregs[i+1] >> 64), | 
|  | 98 | static_cast<uint64_t>(f.vregs[i+1])); | 
| Kévin PETIT | abc60c2 | 2013-12-19 12:36:59 +0000 | [diff] [blame] | 99 | } | 
| Elliott Hughes | b40c503 | 2014-07-14 16:10:15 -0700 | [diff] [blame] | 100 | _LOG(log, logtype::FP_REGISTERS, "    fpsr %08x  fpcr %08x\n", f.fpsr, f.fpcr); | 
| Kévin PETIT | abc60c2 | 2013-12-19 12:36:59 +0000 | [diff] [blame] | 101 | } |