Pavel Chupin | c6c194c | 2013-11-21 23:17:20 +0400 | [diff] [blame] | 1 | /* |
| 2 | ** Copyright 2013, The Android Open Source Project |
| 3 | ** |
| 4 | ** Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | ** you may not use this file except in compliance with the License. |
| 6 | ** You may obtain a copy of the License at |
| 7 | ** |
| 8 | ** http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | ** |
| 10 | ** Unless required by applicable law or agreed to in writing, software |
| 11 | ** distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | ** See the License for the specific language governing permissions and |
| 14 | ** limitations under the License. |
| 15 | */ |
| 16 | |
Christopher Ferris | b36b592 | 2015-06-17 18:35:59 -0700 | [diff] [blame] | 17 | #define LOG_TAG "DEBUG" |
| 18 | |
Pavel Chupin | c6c194c | 2013-11-21 23:17:20 +0400 | [diff] [blame] | 19 | #include <errno.h> |
Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 20 | #include <stdint.h> |
Pavel Chupin | c6c194c | 2013-11-21 23:17:20 +0400 | [diff] [blame] | 21 | #include <sys/ptrace.h> |
Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 22 | #include <string.h> |
Pavel Chupin | c6c194c | 2013-11-21 23:17:20 +0400 | [diff] [blame] | 23 | #include <sys/user.h> |
| 24 | |
Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 25 | #include <backtrace/Backtrace.h> |
Christopher Ferris | b36b592 | 2015-06-17 18:35:59 -0700 | [diff] [blame] | 26 | #include <log/log.h> |
Pavel Chupin | c6c194c | 2013-11-21 23:17:20 +0400 | [diff] [blame] | 27 | |
Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 28 | #include "machine.h" |
| 29 | #include "utility.h" |
| 30 | |
| 31 | void dump_memory_and_code(log_t* log, Backtrace* backtrace) { |
| 32 | struct user_regs_struct r; |
| 33 | if (ptrace(PTRACE_GETREGS, backtrace->Tid(), 0, &r) == -1) { |
Christopher Ferris | b36b592 | 2015-06-17 18:35:59 -0700 | [diff] [blame] | 34 | ALOGE("cannot get registers: %s\n", strerror(errno)); |
Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 35 | return; |
| 36 | } |
| 37 | |
| 38 | dump_memory(log, backtrace, static_cast<uintptr_t>(r.rax), "memory near rax:"); |
| 39 | dump_memory(log, backtrace, static_cast<uintptr_t>(r.rbx), "memory near rbx:"); |
| 40 | dump_memory(log, backtrace, static_cast<uintptr_t>(r.rcx), "memory near rcx:"); |
| 41 | dump_memory(log, backtrace, static_cast<uintptr_t>(r.rdx), "memory near rdx:"); |
| 42 | dump_memory(log, backtrace, static_cast<uintptr_t>(r.rsi), "memory near rsi:"); |
| 43 | dump_memory(log, backtrace, static_cast<uintptr_t>(r.rdi), "memory near rdi:"); |
| 44 | |
| 45 | dump_memory(log, backtrace, static_cast<uintptr_t>(r.rip), "code around rip:"); |
Pavel Chupin | c6c194c | 2013-11-21 23:17:20 +0400 | [diff] [blame] | 46 | } |
| 47 | |
Brigid Smith | 62ba489 | 2014-06-10 11:53:08 -0700 | [diff] [blame] | 48 | void dump_registers(log_t* log, pid_t tid) { |
Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 49 | struct user_regs_struct r; |
| 50 | if (ptrace(PTRACE_GETREGS, tid, 0, &r) == -1) { |
Christopher Ferris | b36b592 | 2015-06-17 18:35:59 -0700 | [diff] [blame] | 51 | ALOGE("cannot get registers: %s\n", strerror(errno)); |
Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 52 | return; |
| 53 | } |
| 54 | |
| 55 | _LOG(log, logtype::REGISTERS, " rax %016lx rbx %016lx rcx %016lx rdx %016lx\n", |
| 56 | r.rax, r.rbx, r.rcx, r.rdx); |
| 57 | _LOG(log, logtype::REGISTERS, " rsi %016lx rdi %016lx\n", |
| 58 | r.rsi, r.rdi); |
| 59 | _LOG(log, logtype::REGISTERS, " r8 %016lx r9 %016lx r10 %016lx r11 %016lx\n", |
| 60 | r.r8, r.r9, r.r10, r.r11); |
| 61 | _LOG(log, logtype::REGISTERS, " r12 %016lx r13 %016lx r14 %016lx r15 %016lx\n", |
| 62 | r.r12, r.r13, r.r14, r.r15); |
| 63 | _LOG(log, logtype::REGISTERS, " cs %016lx ss %016lx\n", |
| 64 | r.cs, r.ss); |
| 65 | _LOG(log, logtype::REGISTERS, " rip %016lx rbp %016lx rsp %016lx eflags %016lx\n", |
| 66 | r.rip, r.rbp, r.rsp, r.eflags); |
Pavel Chupin | c6c194c | 2013-11-21 23:17:20 +0400 | [diff] [blame] | 67 | } |