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> |
Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame^] | 20 | #include <stdint.h> |
Elliott Hughes | b40c503 | 2014-07-14 16:10:15 -0700 | [diff] [blame] | 21 | #include <string.h> |
Kévin PETIT | abc60c2 | 2013-12-19 12:36:59 +0000 | [diff] [blame] | 22 | #include <sys/ptrace.h> |
Kévin PETIT | abc60c2 | 2013-12-19 12:36:59 +0000 | [diff] [blame] | 23 | #include <sys/uio.h> |
Kévin PETIT | abc60c2 | 2013-12-19 12:36:59 +0000 | [diff] [blame] | 24 | |
Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame^] | 25 | #include <backtrace/Backtrace.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 "machine.h" |
| 28 | #include "utility.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 | void dump_memory_and_code(log_t* log, Backtrace* backtrace) { |
| 31 | struct user_pt_regs regs; |
| 32 | struct iovec io; |
| 33 | io.iov_base = ®s; |
| 34 | io.iov_len = sizeof(regs); |
Kévin PETIT | abc60c2 | 2013-12-19 12:36:59 +0000 | [diff] [blame] | 35 | |
Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame^] | 36 | if (ptrace(PTRACE_GETREGSET, backtrace->Tid(), reinterpret_cast<void*>(NT_PRSTATUS), &io) == -1) { |
| 37 | _LOG(log, logtype::ERROR, "%s: ptrace failed to get registers: %s", |
| 38 | __func__, strerror(errno)); |
| 39 | return; |
| 40 | } |
Kévin PETIT | abc60c2 | 2013-12-19 12:36:59 +0000 | [diff] [blame] | 41 | |
Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame^] | 42 | for (int reg = 0; reg < 31; reg++) { |
| 43 | dump_memory(log, backtrace, regs.regs[reg], "memory near x%d:", reg); |
| 44 | } |
Brigid Smith | 62ba489 | 2014-06-10 11:53:08 -0700 | [diff] [blame] | 45 | |
Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame^] | 46 | 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] | 47 | |
Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame^] | 48 | if (regs.pc != regs.sp) { |
| 49 | dump_memory(log, backtrace, static_cast<uintptr_t>(regs.sp), "code around sp:"); |
| 50 | } |
Kévin PETIT | abc60c2 | 2013-12-19 12:36:59 +0000 | [diff] [blame] | 51 | } |
| 52 | |
Brigid Smith | 62ba489 | 2014-06-10 11:53:08 -0700 | [diff] [blame] | 53 | void dump_registers(log_t* log, pid_t tid) { |
Kévin PETIT | abc60c2 | 2013-12-19 12:36:59 +0000 | [diff] [blame] | 54 | struct user_pt_regs r; |
| 55 | struct iovec io; |
| 56 | io.iov_base = &r; |
| 57 | io.iov_len = sizeof(r); |
| 58 | |
Kévin PETIT | abc60c2 | 2013-12-19 12:36:59 +0000 | [diff] [blame] | 59 | if (ptrace(PTRACE_GETREGSET, tid, (void*) NT_PRSTATUS, (void*) &io) == -1) { |
Brigid Smith | e17f267 | 2014-06-19 11:33:38 -0700 | [diff] [blame] | 60 | _LOG(log, logtype::ERROR, "ptrace error: %s\n", strerror(errno)); |
Kévin PETIT | abc60c2 | 2013-12-19 12:36:59 +0000 | [diff] [blame] | 61 | return; |
| 62 | } |
| 63 | |
| 64 | for (int i = 0; i < 28; i += 4) { |
Brigid Smith | 62ba489 | 2014-06-10 11:53:08 -0700 | [diff] [blame] | 65 | _LOG(log, logtype::REGISTERS, |
Elliott Hughes | b40c503 | 2014-07-14 16:10:15 -0700 | [diff] [blame] | 66 | " x%-2d %016llx x%-2d %016llx x%-2d %016llx x%-2d %016llx\n", |
| 67 | i, r.regs[i], |
| 68 | i+1, r.regs[i+1], |
| 69 | i+2, r.regs[i+2], |
| 70 | i+3, r.regs[i+3]); |
Kévin PETIT | abc60c2 | 2013-12-19 12:36:59 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Elliott Hughes | b40c503 | 2014-07-14 16:10:15 -0700 | [diff] [blame] | 73 | _LOG(log, logtype::REGISTERS, " x28 %016llx x29 %016llx x30 %016llx\n", |
| 74 | r.regs[28], r.regs[29], r.regs[30]); |
Kévin PETIT | abc60c2 | 2013-12-19 12:36:59 +0000 | [diff] [blame] | 75 | |
Elliott Hughes | b40c503 | 2014-07-14 16:10:15 -0700 | [diff] [blame] | 76 | _LOG(log, logtype::REGISTERS, " sp %016llx pc %016llx pstate %016llx\n", |
| 77 | r.sp, r.pc, r.pstate); |
Kévin PETIT | abc60c2 | 2013-12-19 12:36:59 +0000 | [diff] [blame] | 78 | |
| 79 | struct user_fpsimd_state f; |
| 80 | io.iov_base = &f; |
| 81 | io.iov_len = sizeof(f); |
| 82 | |
| 83 | if (ptrace(PTRACE_GETREGSET, tid, (void*) NT_PRFPREG, (void*) &io) == -1) { |
Brigid Smith | e17f267 | 2014-06-19 11:33:38 -0700 | [diff] [blame] | 84 | _LOG(log, logtype::ERROR, "ptrace error: %s\n", strerror(errno)); |
Kévin PETIT | abc60c2 | 2013-12-19 12:36:59 +0000 | [diff] [blame] | 85 | return; |
| 86 | } |
| 87 | |
Elliott Hughes | b40c503 | 2014-07-14 16:10:15 -0700 | [diff] [blame] | 88 | for (int i = 0; i < 32; i += 2) { |
| 89 | _LOG(log, logtype::FP_REGISTERS, |
| 90 | " v%-2d %016" PRIx64 "%016" PRIx64 " v%-2d %016" PRIx64 "%016" PRIx64 "\n", |
| 91 | i, |
| 92 | static_cast<uint64_t>(f.vregs[i] >> 64), |
| 93 | static_cast<uint64_t>(f.vregs[i]), |
| 94 | i+1, |
| 95 | static_cast<uint64_t>(f.vregs[i+1] >> 64), |
| 96 | static_cast<uint64_t>(f.vregs[i+1])); |
Kévin PETIT | abc60c2 | 2013-12-19 12:36:59 +0000 | [diff] [blame] | 97 | } |
Elliott Hughes | b40c503 | 2014-07-14 16:10:15 -0700 | [diff] [blame] | 98 | _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] | 99 | } |