Douglas Leung | 2ea9a32 | 2015-03-09 18:41:32 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014, 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 | |
| 17 | #include <stddef.h> |
| 18 | #include <stdlib.h> |
| 19 | #include <string.h> |
| 20 | #include <stdio.h> |
| 21 | #include <errno.h> |
| 22 | #include <sys/types.h> |
| 23 | #include <sys/ptrace.h> |
| 24 | |
| 25 | #include <sys/user.h> |
| 26 | |
| 27 | #include "../utility.h" |
| 28 | #include "../machine.h" |
| 29 | |
| 30 | #define R(x) (static_cast<unsigned long>(x)) |
| 31 | |
| 32 | // If configured to do so, dump memory around *all* registers |
| 33 | // for the crashing thread. |
| 34 | void dump_memory_and_code(log_t* log, pid_t tid) { |
| 35 | pt_regs r; |
| 36 | if (ptrace(PTRACE_GETREGS, tid, 0, &r)) { |
| 37 | return; |
| 38 | } |
| 39 | |
| 40 | static const char REG_NAMES[] = "$0atv0v1a0a1a2a3a4a5a6a7t0t1t2t3s0s1s2s3s4s5s6s7t8t9k0k1gpsps8ra"; |
| 41 | |
| 42 | for (int reg = 0; reg < 32; reg++) { |
| 43 | // skip uninteresting registers |
| 44 | if (reg == 0 // $0 |
| 45 | || reg == 26 // $k0 |
| 46 | || reg == 27 // $k1 |
| 47 | || reg == 31 // $ra (done below) |
| 48 | ) |
| 49 | continue; |
| 50 | |
| 51 | uintptr_t addr = R(r.regs[reg]); |
| 52 | |
| 53 | // Don't bother if it looks like a small int or ~= null, or if |
| 54 | // it's in the kernel area. |
| 55 | if (addr < 4096 || addr >= 0x4000000000000000) { |
| 56 | continue; |
| 57 | } |
| 58 | |
| 59 | _LOG(log, logtype::MEMORY, "\nmemory near %.2s:\n", ®_NAMES[reg * 2]); |
| 60 | dump_memory(log, tid, addr); |
| 61 | } |
| 62 | |
| 63 | unsigned long pc = R(r.cp0_epc); |
| 64 | unsigned long ra = R(r.regs[31]); |
| 65 | |
| 66 | _LOG(log, logtype::MEMORY, "\ncode around pc:\n"); |
| 67 | dump_memory(log, tid, (uintptr_t)pc); |
| 68 | |
| 69 | if (pc != ra) { |
| 70 | _LOG(log, logtype::MEMORY, "\ncode around ra:\n"); |
| 71 | dump_memory(log, tid, (uintptr_t)ra); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | void dump_registers(log_t* log, pid_t tid) { |
| 76 | pt_regs r; |
| 77 | if(ptrace(PTRACE_GETREGS, tid, 0, &r)) { |
| 78 | _LOG(log, logtype::ERROR, "cannot get registers: %s\n", strerror(errno)); |
| 79 | return; |
| 80 | } |
| 81 | |
| 82 | _LOG(log, logtype::REGISTERS, " zr %016lx at %016lx v0 %016lx v1 %016lx\n", |
| 83 | R(r.regs[0]), R(r.regs[1]), R(r.regs[2]), R(r.regs[3])); |
| 84 | _LOG(log, logtype::REGISTERS, " a0 %016lx a1 %016lx a2 %016lx a3 %016lx\n", |
| 85 | R(r.regs[4]), R(r.regs[5]), R(r.regs[6]), R(r.regs[7])); |
| 86 | _LOG(log, logtype::REGISTERS, " a4 %016lx a5 %016lx a6 %016lx a7 %016lx\n", |
| 87 | R(r.regs[8]), R(r.regs[9]), R(r.regs[10]), R(r.regs[11])); |
| 88 | _LOG(log, logtype::REGISTERS, " t0 %016lx t1 %016lx t2 %016lx t3 %016lx\n", |
| 89 | R(r.regs[12]), R(r.regs[13]), R(r.regs[14]), R(r.regs[15])); |
| 90 | _LOG(log, logtype::REGISTERS, " s0 %016lx s1 %016lx s2 %016lx s3 %016lx\n", |
| 91 | R(r.regs[16]), R(r.regs[17]), R(r.regs[18]), R(r.regs[19])); |
| 92 | _LOG(log, logtype::REGISTERS, " s4 %016lx s5 %016lx s6 %016lx s7 %016lx\n", |
| 93 | R(r.regs[20]), R(r.regs[21]), R(r.regs[22]), R(r.regs[23])); |
| 94 | _LOG(log, logtype::REGISTERS, " t8 %016lx t9 %016lx k0 %016lx k1 %016lx\n", |
| 95 | R(r.regs[24]), R(r.regs[25]), R(r.regs[26]), R(r.regs[27])); |
| 96 | _LOG(log, logtype::REGISTERS, " gp %016lx sp %016lx s8 %016lx ra %016lx\n", |
| 97 | R(r.regs[28]), R(r.regs[29]), R(r.regs[30]), R(r.regs[31])); |
| 98 | _LOG(log, logtype::REGISTERS, " hi %016lx lo %016lx bva %016lx epc %016lx\n", |
| 99 | R(r.hi), R(r.lo), R(r.cp0_badvaddr), R(r.cp0_epc)); |
| 100 | } |