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 | |
Elliott Hughes | b40c503 | 2014-07-14 16:10:15 -0700 | [diff] [blame] | 18 | #include <elf.h> |
Kévin PETIT | abc60c2 | 2013-12-19 12:36:59 +0000 | [diff] [blame] | 19 | #include <errno.h> |
Elliott Hughes | b40c503 | 2014-07-14 16:10:15 -0700 | [diff] [blame] | 20 | #include <inttypes.h> |
| 21 | #include <string.h> |
Kévin PETIT | abc60c2 | 2013-12-19 12:36:59 +0000 | [diff] [blame] | 22 | #include <sys/types.h> |
| 23 | #include <sys/ptrace.h> |
| 24 | #include <sys/user.h> |
| 25 | #include <sys/uio.h> |
Kévin PETIT | abc60c2 | 2013-12-19 12:36:59 +0000 | [diff] [blame] | 26 | |
| 27 | #include "../utility.h" |
| 28 | #include "../machine.h" |
| 29 | |
Brigid Smith | 62ba489 | 2014-06-10 11:53:08 -0700 | [diff] [blame] | 30 | void dump_memory_and_code(log_t* log, pid_t tid) { |
Kévin PETIT | abc60c2 | 2013-12-19 12:36:59 +0000 | [diff] [blame] | 31 | struct user_pt_regs regs; |
| 32 | struct iovec io; |
| 33 | io.iov_base = ®s; |
| 34 | io.iov_len = sizeof(regs); |
| 35 | |
| 36 | if (ptrace(PTRACE_GETREGSET, tid, (void*)NT_PRSTATUS, &io) == -1) { |
Brigid Smith | e17f267 | 2014-06-19 11:33:38 -0700 | [diff] [blame] | 37 | _LOG(log, logtype::ERROR, "%s: ptrace failed to get registers: %s\n", |
Kévin PETIT | abc60c2 | 2013-12-19 12:36:59 +0000 | [diff] [blame] | 38 | __func__, strerror(errno)); |
| 39 | return; |
| 40 | } |
| 41 | |
Brigid Smith | 62ba489 | 2014-06-10 11:53:08 -0700 | [diff] [blame] | 42 | for (int reg = 0; reg < 31; reg++) { |
| 43 | uintptr_t addr = regs.regs[reg]; |
Kévin PETIT | abc60c2 | 2013-12-19 12:36:59 +0000 | [diff] [blame] | 44 | |
Brigid Smith | 62ba489 | 2014-06-10 11:53:08 -0700 | [diff] [blame] | 45 | /* |
| 46 | * Don't bother if it looks like a small int or ~= null, or if |
| 47 | * it's in the kernel area. |
| 48 | */ |
| 49 | if (addr < 4096 || addr >= (1UL<<63)) { |
| 50 | continue; |
Kévin PETIT | abc60c2 | 2013-12-19 12:36:59 +0000 | [diff] [blame] | 51 | } |
Brigid Smith | 62ba489 | 2014-06-10 11:53:08 -0700 | [diff] [blame] | 52 | |
| 53 | _LOG(log, logtype::MEMORY, "\nmemory near x%d:\n", reg); |
| 54 | dump_memory(log, tid, addr); |
Kévin PETIT | abc60c2 | 2013-12-19 12:36:59 +0000 | [diff] [blame] | 55 | } |
| 56 | |
Brigid Smith | 62ba489 | 2014-06-10 11:53:08 -0700 | [diff] [blame] | 57 | _LOG(log, logtype::MEMORY, "\ncode around pc:\n"); |
| 58 | dump_memory(log, tid, (uintptr_t)regs.pc); |
Kévin PETIT | abc60c2 | 2013-12-19 12:36:59 +0000 | [diff] [blame] | 59 | |
| 60 | if (regs.pc != regs.sp) { |
Brigid Smith | 62ba489 | 2014-06-10 11:53:08 -0700 | [diff] [blame] | 61 | _LOG(log, logtype::MEMORY, "\ncode around sp:\n"); |
| 62 | dump_memory(log, tid, (uintptr_t)regs.sp); |
Kévin PETIT | abc60c2 | 2013-12-19 12:36:59 +0000 | [diff] [blame] | 63 | } |
| 64 | } |
| 65 | |
Brigid Smith | 62ba489 | 2014-06-10 11:53:08 -0700 | [diff] [blame] | 66 | void dump_registers(log_t* log, pid_t tid) { |
Kévin PETIT | abc60c2 | 2013-12-19 12:36:59 +0000 | [diff] [blame] | 67 | struct user_pt_regs r; |
| 68 | struct iovec io; |
| 69 | io.iov_base = &r; |
| 70 | io.iov_len = sizeof(r); |
| 71 | |
Kévin PETIT | abc60c2 | 2013-12-19 12:36:59 +0000 | [diff] [blame] | 72 | if (ptrace(PTRACE_GETREGSET, tid, (void*) NT_PRSTATUS, (void*) &io) == -1) { |
Brigid Smith | e17f267 | 2014-06-19 11:33:38 -0700 | [diff] [blame] | 73 | _LOG(log, logtype::ERROR, "ptrace error: %s\n", strerror(errno)); |
Kévin PETIT | abc60c2 | 2013-12-19 12:36:59 +0000 | [diff] [blame] | 74 | return; |
| 75 | } |
| 76 | |
| 77 | for (int i = 0; i < 28; i += 4) { |
Brigid Smith | 62ba489 | 2014-06-10 11:53:08 -0700 | [diff] [blame] | 78 | _LOG(log, logtype::REGISTERS, |
Elliott Hughes | b40c503 | 2014-07-14 16:10:15 -0700 | [diff] [blame] | 79 | " x%-2d %016llx x%-2d %016llx x%-2d %016llx x%-2d %016llx\n", |
| 80 | i, r.regs[i], |
| 81 | i+1, r.regs[i+1], |
| 82 | i+2, r.regs[i+2], |
| 83 | i+3, r.regs[i+3]); |
Kévin PETIT | abc60c2 | 2013-12-19 12:36:59 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Elliott Hughes | b40c503 | 2014-07-14 16:10:15 -0700 | [diff] [blame] | 86 | _LOG(log, logtype::REGISTERS, " x28 %016llx x29 %016llx x30 %016llx\n", |
| 87 | r.regs[28], r.regs[29], r.regs[30]); |
Kévin PETIT | abc60c2 | 2013-12-19 12:36:59 +0000 | [diff] [blame] | 88 | |
Elliott Hughes | b40c503 | 2014-07-14 16:10:15 -0700 | [diff] [blame] | 89 | _LOG(log, logtype::REGISTERS, " sp %016llx pc %016llx pstate %016llx\n", |
| 90 | r.sp, r.pc, r.pstate); |
Kévin PETIT | abc60c2 | 2013-12-19 12:36:59 +0000 | [diff] [blame] | 91 | |
| 92 | struct user_fpsimd_state f; |
| 93 | io.iov_base = &f; |
| 94 | io.iov_len = sizeof(f); |
| 95 | |
| 96 | if (ptrace(PTRACE_GETREGSET, tid, (void*) NT_PRFPREG, (void*) &io) == -1) { |
Brigid Smith | e17f267 | 2014-06-19 11:33:38 -0700 | [diff] [blame] | 97 | _LOG(log, logtype::ERROR, "ptrace error: %s\n", strerror(errno)); |
Kévin PETIT | abc60c2 | 2013-12-19 12:36:59 +0000 | [diff] [blame] | 98 | return; |
| 99 | } |
| 100 | |
Elliott Hughes | b40c503 | 2014-07-14 16:10:15 -0700 | [diff] [blame] | 101 | for (int i = 0; i < 32; i += 2) { |
| 102 | _LOG(log, logtype::FP_REGISTERS, |
| 103 | " v%-2d %016" PRIx64 "%016" PRIx64 " v%-2d %016" PRIx64 "%016" PRIx64 "\n", |
| 104 | i, |
| 105 | static_cast<uint64_t>(f.vregs[i] >> 64), |
| 106 | static_cast<uint64_t>(f.vregs[i]), |
| 107 | i+1, |
| 108 | static_cast<uint64_t>(f.vregs[i+1] >> 64), |
| 109 | static_cast<uint64_t>(f.vregs[i+1])); |
Kévin PETIT | abc60c2 | 2013-12-19 12:36:59 +0000 | [diff] [blame] | 110 | } |
Elliott Hughes | b40c503 | 2014-07-14 16:10:15 -0700 | [diff] [blame] | 111 | _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] | 112 | } |