| Elliott Hughes | 18ddd42 | 2013-11-14 17:06:46 -0800 | [diff] [blame] | 1 | /* | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 2 | * Copyright 2006, 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 | */ | 
| Bruce Beare | 6cc4923 | 2010-10-13 16:11:15 -0700 | [diff] [blame] | 16 |  | 
| Christopher Ferris | b36b592 | 2015-06-17 18:35:59 -0700 | [diff] [blame] | 17 | #define LOG_TAG "DEBUG" | 
|  | 18 |  | 
| Bruce Beare | 6cc4923 | 2010-10-13 16:11:15 -0700 | [diff] [blame] | 19 | #include <errno.h> | 
| Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 20 | #include <stdint.h> | 
|  | 21 | #include <string.h> | 
| Bruce Beare | 6cc4923 | 2010-10-13 16:11:15 -0700 | [diff] [blame] | 22 | #include <sys/ptrace.h> | 
| Bruce Beare | 6cc4923 | 2010-10-13 16:11:15 -0700 | [diff] [blame] | 23 |  | 
| Mark Salyzyn | ff2dcd9 | 2016-09-28 15:54:45 -0700 | [diff] [blame] | 24 | #include <android/log.h> | 
| Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 25 | #include <backtrace/Backtrace.h> | 
| Bruce Beare | 6cc4923 | 2010-10-13 16:11:15 -0700 | [diff] [blame] | 26 |  | 
| Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 27 | #include "machine.h" | 
|  | 28 | #include "utility.h" | 
|  | 29 |  | 
|  | 30 | void dump_memory_and_code(log_t* log, Backtrace* backtrace) { | 
|  | 31 | struct pt_regs r; | 
|  | 32 | if (ptrace(PTRACE_GETREGS, backtrace->Tid(), 0, &r) == -1) { | 
| Christopher Ferris | b36b592 | 2015-06-17 18:35:59 -0700 | [diff] [blame] | 33 | ALOGE("cannot get registers: %s\n", strerror(errno)); | 
| Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 34 | return; | 
|  | 35 | } | 
|  | 36 |  | 
|  | 37 | dump_memory(log, backtrace, static_cast<uintptr_t>(r.eax), "memory near eax:"); | 
|  | 38 | dump_memory(log, backtrace, static_cast<uintptr_t>(r.ebx), "memory near ebx:"); | 
|  | 39 | dump_memory(log, backtrace, static_cast<uintptr_t>(r.ecx), "memory near ecx:"); | 
|  | 40 | dump_memory(log, backtrace, static_cast<uintptr_t>(r.edx), "memory near edx:"); | 
|  | 41 | dump_memory(log, backtrace, static_cast<uintptr_t>(r.esi), "memory near esi:"); | 
|  | 42 | dump_memory(log, backtrace, static_cast<uintptr_t>(r.edi), "memory near edi:"); | 
|  | 43 |  | 
|  | 44 | dump_memory(log, backtrace, static_cast<uintptr_t>(r.eip), "code around eip:"); | 
| Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 45 | } | 
|  | 46 |  | 
| Brigid Smith | 62ba489 | 2014-06-10 11:53:08 -0700 | [diff] [blame] | 47 | void dump_registers(log_t* log, pid_t tid) { | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 48 | struct pt_regs r; | 
|  | 49 | if (ptrace(PTRACE_GETREGS, tid, 0, &r) == -1) { | 
| Christopher Ferris | b36b592 | 2015-06-17 18:35:59 -0700 | [diff] [blame] | 50 | ALOGE("cannot get registers: %s\n", strerror(errno)); | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 51 | return; | 
|  | 52 | } | 
| Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 53 |  | 
| Brigid Smith | 62ba489 | 2014-06-10 11:53:08 -0700 | [diff] [blame] | 54 | _LOG(log, logtype::REGISTERS, "    eax %08lx  ebx %08lx  ecx %08lx  edx %08lx\n", | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 55 | r.eax, r.ebx, r.ecx, r.edx); | 
| Brigid Smith | 62ba489 | 2014-06-10 11:53:08 -0700 | [diff] [blame] | 56 | _LOG(log, logtype::REGISTERS, "    esi %08lx  edi %08lx\n", | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 57 | r.esi, r.edi); | 
| Brigid Smith | 62ba489 | 2014-06-10 11:53:08 -0700 | [diff] [blame] | 58 | _LOG(log, logtype::REGISTERS, "    xcs %08x  xds %08x  xes %08x  xfs %08x  xss %08x\n", | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 59 | r.xcs, r.xds, r.xes, r.xfs, r.xss); | 
| Brigid Smith | 62ba489 | 2014-06-10 11:53:08 -0700 | [diff] [blame] | 60 | _LOG(log, logtype::REGISTERS, "    eip %08lx  ebp %08lx  esp %08lx  flags %08lx\n", | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 61 | r.eip, r.ebp, r.esp, r.eflags); | 
| Bruce Beare | 6cc4923 | 2010-10-13 16:11:15 -0700 | [diff] [blame] | 62 | } |