Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012, 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 | */ |
Chris Dearman | 231e3c8 | 2012-08-10 17:06:20 -0700 | [diff] [blame] | 16 | |
| 17 | #include <stddef.h> |
Chris Dearman | 231e3c8 | 2012-08-10 17:06:20 -0700 | [diff] [blame] | 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 | |
Elliott Hughes | 76e7f5e | 2013-11-20 11:31:29 -0800 | [diff] [blame] | 25 | #include <sys/user.h> |
Chris Dearman | 231e3c8 | 2012-08-10 17:06:20 -0700 | [diff] [blame] | 26 | |
| 27 | #include "../utility.h" |
| 28 | #include "../machine.h" |
| 29 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 30 | #define R(x) (static_cast<unsigned int>(x)) |
Chris Dearman | 231e3c8 | 2012-08-10 17:06:20 -0700 | [diff] [blame] | 31 | |
Douglas Leung | 6254f32 | 2014-05-21 11:46:01 -0700 | [diff] [blame] | 32 | // The MIPS uapi ptrace.h has the wrong definition for pt_regs. PTRACE_GETREGS |
| 33 | // writes 64-bit quantities even though the public struct uses 32-bit ones. |
| 34 | struct pt_regs_mips_t { |
| 35 | uint64_t regs[32]; |
| 36 | uint64_t lo; |
| 37 | uint64_t hi; |
| 38 | uint64_t cp0_epc; |
| 39 | uint64_t cp0_badvaddr; |
| 40 | uint64_t cp0_status; |
| 41 | uint64_t cp0_cause; |
| 42 | }; |
| 43 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 44 | // If configured to do so, dump memory around *all* registers |
| 45 | // for the crashing thread. |
Brigid Smith | 62ba489 | 2014-06-10 11:53:08 -0700 | [diff] [blame] | 46 | void dump_memory_and_code(log_t* log, pid_t tid) { |
Douglas Leung | 6254f32 | 2014-05-21 11:46:01 -0700 | [diff] [blame] | 47 | pt_regs_mips_t r; |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 48 | if (ptrace(PTRACE_GETREGS, tid, 0, &r)) { |
| 49 | return; |
| 50 | } |
| 51 | |
Brigid Smith | 62ba489 | 2014-06-10 11:53:08 -0700 | [diff] [blame] | 52 | static const char REG_NAMES[] = "$0atv0v1a0a1a2a3t0t1t2t3t4t5t6t7s0s1s2s3s4s5s6s7t8t9k0k1gpsps8ra"; |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 53 | |
Brigid Smith | 62ba489 | 2014-06-10 11:53:08 -0700 | [diff] [blame] | 54 | for (int reg = 0; reg < 32; reg++) { |
| 55 | // skip uninteresting registers |
| 56 | if (reg == 0 // $0 |
| 57 | || reg == 26 // $k0 |
| 58 | || reg == 27 // $k1 |
| 59 | || reg == 31 // $ra (done below) |
| 60 | ) |
| 61 | continue; |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 62 | |
Brigid Smith | 62ba489 | 2014-06-10 11:53:08 -0700 | [diff] [blame] | 63 | uintptr_t addr = R(r.regs[reg]); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 64 | |
Brigid Smith | 62ba489 | 2014-06-10 11:53:08 -0700 | [diff] [blame] | 65 | // Don't bother if it looks like a small int or ~= null, or if |
| 66 | // it's in the kernel area. |
| 67 | if (addr < 4096 || addr >= 0x80000000) { |
| 68 | continue; |
Chris Dearman | 231e3c8 | 2012-08-10 17:06:20 -0700 | [diff] [blame] | 69 | } |
Brigid Smith | 62ba489 | 2014-06-10 11:53:08 -0700 | [diff] [blame] | 70 | |
| 71 | _LOG(log, logtype::MEMORY, "\nmemory near %.2s:\n", ®_NAMES[reg * 2]); |
| 72 | dump_memory(log, tid, addr); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 73 | } |
Chris Dearman | 231e3c8 | 2012-08-10 17:06:20 -0700 | [diff] [blame] | 74 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 75 | unsigned int pc = R(r.cp0_epc); |
| 76 | unsigned int ra = R(r.regs[31]); |
Chris Dearman | 231e3c8 | 2012-08-10 17:06:20 -0700 | [diff] [blame] | 77 | |
Brigid Smith | 62ba489 | 2014-06-10 11:53:08 -0700 | [diff] [blame] | 78 | _LOG(log, logtype::MEMORY, "\ncode around pc:\n"); |
| 79 | dump_memory(log, tid, (uintptr_t)pc); |
Chris Dearman | 231e3c8 | 2012-08-10 17:06:20 -0700 | [diff] [blame] | 80 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 81 | if (pc != ra) { |
Brigid Smith | 62ba489 | 2014-06-10 11:53:08 -0700 | [diff] [blame] | 82 | _LOG(log, logtype::MEMORY, "\ncode around ra:\n"); |
| 83 | dump_memory(log, tid, (uintptr_t)ra); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 84 | } |
Chris Dearman | 231e3c8 | 2012-08-10 17:06:20 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Brigid Smith | 62ba489 | 2014-06-10 11:53:08 -0700 | [diff] [blame] | 87 | void dump_registers(log_t* log, pid_t tid) { |
Douglas Leung | 6254f32 | 2014-05-21 11:46:01 -0700 | [diff] [blame] | 88 | pt_regs_mips_t r; |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 89 | if(ptrace(PTRACE_GETREGS, tid, 0, &r)) { |
Brigid Smith | e17f267 | 2014-06-19 11:33:38 -0700 | [diff] [blame] | 90 | _LOG(log, logtype::ERROR, "cannot get registers: %s\n", strerror(errno)); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 91 | return; |
| 92 | } |
Chris Dearman | 231e3c8 | 2012-08-10 17:06:20 -0700 | [diff] [blame] | 93 | |
Brigid Smith | 62ba489 | 2014-06-10 11:53:08 -0700 | [diff] [blame] | 94 | _LOG(log, logtype::REGISTERS, " zr %08x at %08x v0 %08x v1 %08x\n", |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 95 | R(r.regs[0]), R(r.regs[1]), R(r.regs[2]), R(r.regs[3])); |
Brigid Smith | 62ba489 | 2014-06-10 11:53:08 -0700 | [diff] [blame] | 96 | _LOG(log, logtype::REGISTERS, " a0 %08x a1 %08x a2 %08x a3 %08x\n", |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 97 | R(r.regs[4]), R(r.regs[5]), R(r.regs[6]), R(r.regs[7])); |
Brigid Smith | 62ba489 | 2014-06-10 11:53:08 -0700 | [diff] [blame] | 98 | _LOG(log, logtype::REGISTERS, " t0 %08x t1 %08x t2 %08x t3 %08x\n", |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 99 | R(r.regs[8]), R(r.regs[9]), R(r.regs[10]), R(r.regs[11])); |
Brigid Smith | 62ba489 | 2014-06-10 11:53:08 -0700 | [diff] [blame] | 100 | _LOG(log, logtype::REGISTERS, " t4 %08x t5 %08x t6 %08x t7 %08x\n", |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 101 | R(r.regs[12]), R(r.regs[13]), R(r.regs[14]), R(r.regs[15])); |
Brigid Smith | 62ba489 | 2014-06-10 11:53:08 -0700 | [diff] [blame] | 102 | _LOG(log, logtype::REGISTERS, " s0 %08x s1 %08x s2 %08x s3 %08x\n", |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 103 | R(r.regs[16]), R(r.regs[17]), R(r.regs[18]), R(r.regs[19])); |
Brigid Smith | 62ba489 | 2014-06-10 11:53:08 -0700 | [diff] [blame] | 104 | _LOG(log, logtype::REGISTERS, " s4 %08x s5 %08x s6 %08x s7 %08x\n", |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 105 | R(r.regs[20]), R(r.regs[21]), R(r.regs[22]), R(r.regs[23])); |
Brigid Smith | 62ba489 | 2014-06-10 11:53:08 -0700 | [diff] [blame] | 106 | _LOG(log, logtype::REGISTERS, " t8 %08x t9 %08x k0 %08x k1 %08x\n", |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 107 | R(r.regs[24]), R(r.regs[25]), R(r.regs[26]), R(r.regs[27])); |
Brigid Smith | 62ba489 | 2014-06-10 11:53:08 -0700 | [diff] [blame] | 108 | _LOG(log, logtype::REGISTERS, " gp %08x sp %08x s8 %08x ra %08x\n", |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 109 | R(r.regs[28]), R(r.regs[29]), R(r.regs[30]), R(r.regs[31])); |
Brigid Smith | 62ba489 | 2014-06-10 11:53:08 -0700 | [diff] [blame] | 110 | _LOG(log, logtype::REGISTERS, " hi %08x lo %08x bva %08x epc %08x\n", |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 111 | R(r.hi), R(r.lo), R(r.cp0_badvaddr), R(r.cp0_epc)); |
Chris Dearman | 231e3c8 | 2012-08-10 17:06:20 -0700 | [diff] [blame] | 112 | } |