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 | |
| 25 | #include <corkscrew/ptrace.h> |
| 26 | |
Elliott Hughes | 76e7f5e | 2013-11-20 11:31:29 -0800 | [diff] [blame] | 27 | #include <sys/user.h> |
Chris Dearman | 231e3c8 | 2012-08-10 17:06:20 -0700 | [diff] [blame] | 28 | |
| 29 | #include "../utility.h" |
| 30 | #include "../machine.h" |
| 31 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 32 | // enable to dump memory pointed to by every register |
Chris Dearman | 231e3c8 | 2012-08-10 17:06:20 -0700 | [diff] [blame] | 33 | #define DUMP_MEMORY_FOR_ALL_REGISTERS 1 |
| 34 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 35 | #define R(x) (static_cast<unsigned int>(x)) |
Chris Dearman | 231e3c8 | 2012-08-10 17:06:20 -0700 | [diff] [blame] | 36 | |
Christopher Ferris | 365e4ae | 2013-10-02 12:26:48 -0700 | [diff] [blame] | 37 | static void dump_memory(log_t* log, pid_t tid, uintptr_t addr, int scope_flags) { |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 38 | char code_buffer[64]; // actual 8+1+((8+1)*4) + 1 == 45 |
| 39 | char ascii_buffer[32]; // actual 16 + 1 == 17 |
| 40 | uintptr_t p, end; |
Chris Dearman | 231e3c8 | 2012-08-10 17:06:20 -0700 | [diff] [blame] | 41 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 42 | p = addr & ~3; |
| 43 | p -= 32; |
| 44 | if (p > addr) { |
| 45 | // catch underflow |
| 46 | p = 0; |
| 47 | } |
| 48 | end = p + 80; |
| 49 | // catch overflow; 'end - p' has to be multiples of 16 |
| 50 | while (end < p) |
| 51 | end -= 16; |
Chris Dearman | 231e3c8 | 2012-08-10 17:06:20 -0700 | [diff] [blame] | 52 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 53 | // Dump the code around PC as: |
| 54 | // addr contents ascii |
| 55 | // 00008d34 ef000000 e8bd0090 e1b00000 512fff1e ............../Q |
| 56 | // 00008d44 ea00b1f9 e92d0090 e3a070fc ef000000 ......-..p...... |
| 57 | while (p < end) { |
| 58 | char* asc_out = ascii_buffer; |
Chris Dearman | 231e3c8 | 2012-08-10 17:06:20 -0700 | [diff] [blame] | 59 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 60 | sprintf(code_buffer, "%08x ", p); |
Chris Dearman | 231e3c8 | 2012-08-10 17:06:20 -0700 | [diff] [blame] | 61 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 62 | int i; |
| 63 | for (i = 0; i < 4; i++) { |
| 64 | // If we see (data == -1 && errno != 0), we know that the ptrace |
| 65 | // call failed, probably because we're dumping memory in an |
| 66 | // unmapped or inaccessible page. I don't know if there's |
| 67 | // value in making that explicit in the output -- it likely |
| 68 | // just complicates parsing and clarifies nothing for the |
| 69 | // enlightened reader. |
| 70 | long data = ptrace(PTRACE_PEEKTEXT, tid, (void*)p, NULL); |
| 71 | sprintf(code_buffer + strlen(code_buffer), "%08lx ", data); |
Chris Dearman | 231e3c8 | 2012-08-10 17:06:20 -0700 | [diff] [blame] | 72 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 73 | int j; |
| 74 | for (j = 0; j < 4; j++) { |
| 75 | // Our isprint() allows high-ASCII characters that display |
| 76 | // differently (often badly) in different viewers, so we |
| 77 | // just use a simpler test. |
| 78 | char val = (data >> (j*8)) & 0xff; |
| 79 | if (val >= 0x20 && val < 0x7f) { |
| 80 | *asc_out++ = val; |
| 81 | } else { |
| 82 | *asc_out++ = '.'; |
Chris Dearman | 231e3c8 | 2012-08-10 17:06:20 -0700 | [diff] [blame] | 83 | } |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 84 | } |
| 85 | p += 4; |
Chris Dearman | 231e3c8 | 2012-08-10 17:06:20 -0700 | [diff] [blame] | 86 | } |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 87 | *asc_out = '\0'; |
| 88 | _LOG(log, scope_flags, " %s %s\n", code_buffer, ascii_buffer); |
| 89 | } |
Chris Dearman | 231e3c8 | 2012-08-10 17:06:20 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 92 | // If configured to do so, dump memory around *all* registers |
| 93 | // for the crashing thread. |
Christopher Ferris | 365e4ae | 2013-10-02 12:26:48 -0700 | [diff] [blame] | 94 | void dump_memory_and_code(log_t* log, pid_t tid, int scope_flags) { |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 95 | pt_regs_mips_t r; |
| 96 | if (ptrace(PTRACE_GETREGS, tid, 0, &r)) { |
| 97 | return; |
| 98 | } |
| 99 | |
| 100 | if (IS_AT_FAULT(scope_flags) && DUMP_MEMORY_FOR_ALL_REGISTERS) { |
| 101 | static const char REG_NAMES[] = "$0atv0v1a0a1a2a3t0t1t2t3t4t5t6t7s0s1s2s3s4s5s6s7t8t9k0k1gpsps8ra"; |
| 102 | |
| 103 | for (int reg = 0; reg < 32; reg++) { |
| 104 | // skip uninteresting registers |
| 105 | if (reg == 0 // $0 |
| 106 | || reg == 26 // $k0 |
| 107 | || reg == 27 // $k1 |
| 108 | || reg == 31 // $ra (done below) |
| 109 | ) |
| 110 | continue; |
| 111 | |
| 112 | uintptr_t addr = R(r.regs[reg]); |
| 113 | |
| 114 | // Don't bother if it looks like a small int or ~= null, or if |
| 115 | // it's in the kernel area. |
| 116 | if (addr < 4096 || addr >= 0x80000000) { |
| 117 | continue; |
| 118 | } |
| 119 | |
| 120 | _LOG(log, scope_flags | SCOPE_SENSITIVE, "\nmemory near %.2s:\n", ®_NAMES[reg * 2]); |
| 121 | dump_memory(log, tid, addr, scope_flags | SCOPE_SENSITIVE); |
Chris Dearman | 231e3c8 | 2012-08-10 17:06:20 -0700 | [diff] [blame] | 122 | } |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 123 | } |
Chris Dearman | 231e3c8 | 2012-08-10 17:06:20 -0700 | [diff] [blame] | 124 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 125 | unsigned int pc = R(r.cp0_epc); |
| 126 | unsigned int ra = R(r.regs[31]); |
Chris Dearman | 231e3c8 | 2012-08-10 17:06:20 -0700 | [diff] [blame] | 127 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 128 | _LOG(log, scope_flags, "\ncode around pc:\n"); |
| 129 | dump_memory(log, tid, (uintptr_t)pc, scope_flags); |
Chris Dearman | 231e3c8 | 2012-08-10 17:06:20 -0700 | [diff] [blame] | 130 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 131 | if (pc != ra) { |
| 132 | _LOG(log, scope_flags, "\ncode around ra:\n"); |
| 133 | dump_memory(log, tid, (uintptr_t)ra, scope_flags); |
| 134 | } |
Chris Dearman | 231e3c8 | 2012-08-10 17:06:20 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 137 | void dump_registers(log_t* log, pid_t tid, int scope_flags) { |
| 138 | pt_regs_mips_t r; |
| 139 | if(ptrace(PTRACE_GETREGS, tid, 0, &r)) { |
| 140 | _LOG(log, scope_flags, "cannot get registers: %s\n", strerror(errno)); |
| 141 | return; |
| 142 | } |
Chris Dearman | 231e3c8 | 2012-08-10 17:06:20 -0700 | [diff] [blame] | 143 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 144 | _LOG(log, scope_flags, " zr %08x at %08x v0 %08x v1 %08x\n", |
| 145 | R(r.regs[0]), R(r.regs[1]), R(r.regs[2]), R(r.regs[3])); |
| 146 | _LOG(log, scope_flags, " a0 %08x a1 %08x a2 %08x a3 %08x\n", |
| 147 | R(r.regs[4]), R(r.regs[5]), R(r.regs[6]), R(r.regs[7])); |
| 148 | _LOG(log, scope_flags, " t0 %08x t1 %08x t2 %08x t3 %08x\n", |
| 149 | R(r.regs[8]), R(r.regs[9]), R(r.regs[10]), R(r.regs[11])); |
| 150 | _LOG(log, scope_flags, " t4 %08x t5 %08x t6 %08x t7 %08x\n", |
| 151 | R(r.regs[12]), R(r.regs[13]), R(r.regs[14]), R(r.regs[15])); |
| 152 | _LOG(log, scope_flags, " s0 %08x s1 %08x s2 %08x s3 %08x\n", |
| 153 | R(r.regs[16]), R(r.regs[17]), R(r.regs[18]), R(r.regs[19])); |
| 154 | _LOG(log, scope_flags, " s4 %08x s5 %08x s6 %08x s7 %08x\n", |
| 155 | R(r.regs[20]), R(r.regs[21]), R(r.regs[22]), R(r.regs[23])); |
| 156 | _LOG(log, scope_flags, " t8 %08x t9 %08x k0 %08x k1 %08x\n", |
| 157 | R(r.regs[24]), R(r.regs[25]), R(r.regs[26]), R(r.regs[27])); |
| 158 | _LOG(log, scope_flags, " gp %08x sp %08x s8 %08x ra %08x\n", |
| 159 | R(r.regs[28]), R(r.regs[29]), R(r.regs[30]), R(r.regs[31])); |
| 160 | _LOG(log, scope_flags, " hi %08x lo %08x bva %08x epc %08x\n", |
| 161 | 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] | 162 | } |