Chris Dearman | 231e3c8 | 2012-08-10 17:06:20 -0700 | [diff] [blame] | 1 | /* system/debuggerd/debuggerd.c |
| 2 | ** |
| 3 | ** Copyright 2012, The Android Open Source Project |
| 4 | ** |
| 5 | ** Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | ** you may not use this file except in compliance with the License. |
| 7 | ** You may obtain a copy of the License at |
| 8 | ** |
| 9 | ** http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | ** |
| 11 | ** Unless required by applicable law or agreed to in writing, software |
| 12 | ** distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | ** See the License for the specific language governing permissions and |
| 15 | ** limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | #include <stddef.h> |
| 19 | #include <stdbool.h> |
| 20 | #include <stdlib.h> |
| 21 | #include <string.h> |
| 22 | #include <stdio.h> |
| 23 | #include <errno.h> |
| 24 | #include <sys/types.h> |
| 25 | #include <sys/ptrace.h> |
| 26 | |
| 27 | #include <corkscrew/ptrace.h> |
| 28 | |
| 29 | #include <linux/user.h> |
| 30 | |
| 31 | #include "../utility.h" |
| 32 | #include "../machine.h" |
| 33 | |
| 34 | /* enable to dump memory pointed to by every register */ |
| 35 | #define DUMP_MEMORY_FOR_ALL_REGISTERS 1 |
| 36 | |
| 37 | #define R(x) ((unsigned int)(x)) |
| 38 | |
Christopher Tate | 7716aef | 2013-04-02 14:00:27 -0700 | [diff] [blame] | 39 | static void dump_memory(log_t* log, pid_t tid, uintptr_t addr, int scopeFlags) { |
Chris Dearman | 231e3c8 | 2012-08-10 17:06:20 -0700 | [diff] [blame] | 40 | char code_buffer[64]; /* actual 8+1+((8+1)*4) + 1 == 45 */ |
| 41 | char ascii_buffer[32]; /* actual 16 + 1 == 17 */ |
| 42 | uintptr_t p, end; |
| 43 | |
| 44 | p = addr & ~3; |
| 45 | p -= 32; |
| 46 | if (p > addr) { |
| 47 | /* catch underflow */ |
| 48 | p = 0; |
| 49 | } |
| 50 | end = p + 80; |
| 51 | /* catch overflow; 'end - p' has to be multiples of 16 */ |
| 52 | while (end < p) |
| 53 | end -= 16; |
| 54 | |
| 55 | /* Dump the code around PC as: |
| 56 | * addr contents ascii |
| 57 | * 00008d34 ef000000 e8bd0090 e1b00000 512fff1e ............../Q |
| 58 | * 00008d44 ea00b1f9 e92d0090 e3a070fc ef000000 ......-..p...... |
| 59 | */ |
| 60 | while (p < end) { |
| 61 | char* asc_out = ascii_buffer; |
| 62 | |
| 63 | sprintf(code_buffer, "%08x ", p); |
| 64 | |
| 65 | int i; |
| 66 | for (i = 0; i < 4; i++) { |
| 67 | /* |
| 68 | * If we see (data == -1 && errno != 0), we know that the ptrace |
| 69 | * call failed, probably because we're dumping memory in an |
| 70 | * unmapped or inaccessible page. I don't know if there's |
| 71 | * value in making that explicit in the output -- it likely |
| 72 | * just complicates parsing and clarifies nothing for the |
| 73 | * enlightened reader. |
| 74 | */ |
| 75 | long data = ptrace(PTRACE_PEEKTEXT, tid, (void*)p, NULL); |
| 76 | sprintf(code_buffer + strlen(code_buffer), "%08lx ", data); |
| 77 | |
| 78 | int j; |
| 79 | for (j = 0; j < 4; j++) { |
| 80 | /* |
| 81 | * Our isprint() allows high-ASCII characters that display |
| 82 | * differently (often badly) in different viewers, so we |
| 83 | * just use a simpler test. |
| 84 | */ |
| 85 | char val = (data >> (j*8)) & 0xff; |
| 86 | if (val >= 0x20 && val < 0x7f) { |
| 87 | *asc_out++ = val; |
| 88 | } else { |
| 89 | *asc_out++ = '.'; |
| 90 | } |
| 91 | } |
| 92 | p += 4; |
| 93 | } |
| 94 | *asc_out = '\0'; |
Christopher Tate | 7716aef | 2013-04-02 14:00:27 -0700 | [diff] [blame] | 95 | _LOG(log, scopeFlags, " %s %s\n", code_buffer, ascii_buffer); |
Chris Dearman | 231e3c8 | 2012-08-10 17:06:20 -0700 | [diff] [blame] | 96 | } |
| 97 | } |
| 98 | |
| 99 | /* |
| 100 | * If configured to do so, dump memory around *all* registers |
| 101 | * for the crashing thread. |
| 102 | */ |
| 103 | void dump_memory_and_code(const ptrace_context_t* context __attribute((unused)), |
| 104 | log_t* log, pid_t tid, bool at_fault) { |
| 105 | pt_regs_mips_t r; |
| 106 | if(ptrace(PTRACE_GETREGS, tid, 0, &r)) { |
| 107 | return; |
| 108 | } |
| 109 | |
Christopher Tate | 7716aef | 2013-04-02 14:00:27 -0700 | [diff] [blame] | 110 | int scopeFlags = at_fault ? SCOPE_AT_FAULT : 0; |
Chris Dearman | 231e3c8 | 2012-08-10 17:06:20 -0700 | [diff] [blame] | 111 | if (at_fault && DUMP_MEMORY_FOR_ALL_REGISTERS) { |
| 112 | static const char REG_NAMES[] = "$0atv0v1a0a1a2a3t0t1t2t3t4t5t6t7s0s1s2s3s4s5s6s7t8t9k0k1gpsps8ra"; |
| 113 | |
| 114 | for (int reg = 0; reg < 32; reg++) { |
| 115 | /* skip uninteresting registers */ |
| 116 | if (reg == 0 /* $0 */ |
| 117 | || reg == 26 /* $k0 */ |
| 118 | || reg == 27 /* $k1 */ |
| 119 | || reg == 31 /* $ra (done below) */ |
| 120 | ) |
| 121 | continue; |
| 122 | |
| 123 | uintptr_t addr = R(r.regs[reg]); |
| 124 | |
| 125 | /* |
| 126 | * Don't bother if it looks like a small int or ~= null, or if |
| 127 | * it's in the kernel area. |
| 128 | */ |
| 129 | if (addr < 4096 || addr >= 0x80000000) { |
| 130 | continue; |
| 131 | } |
| 132 | |
Christopher Tate | 7716aef | 2013-04-02 14:00:27 -0700 | [diff] [blame] | 133 | _LOG(log, scopeFlags | SCOPE_SENSITIVE, "\nmemory near %.2s:\n", ®_NAMES[reg * 2]); |
| 134 | dump_memory(log, tid, addr, scopeFlags | SCOPE_SENSITIVE); |
Chris Dearman | 231e3c8 | 2012-08-10 17:06:20 -0700 | [diff] [blame] | 135 | } |
| 136 | } |
| 137 | |
| 138 | unsigned int pc = R(r.cp0_epc); |
| 139 | unsigned int ra = R(r.regs[31]); |
| 140 | |
Christopher Tate | 7716aef | 2013-04-02 14:00:27 -0700 | [diff] [blame] | 141 | _LOG(log, scopeFlags, "\ncode around pc:\n"); |
| 142 | dump_memory(log, tid, (uintptr_t)pc, scopeFlags); |
Chris Dearman | 231e3c8 | 2012-08-10 17:06:20 -0700 | [diff] [blame] | 143 | |
| 144 | if (pc != ra) { |
Christopher Tate | 7716aef | 2013-04-02 14:00:27 -0700 | [diff] [blame] | 145 | _LOG(log, scopeFlags, "\ncode around ra:\n"); |
| 146 | dump_memory(log, tid, (uintptr_t)ra, scopeFlags); |
Chris Dearman | 231e3c8 | 2012-08-10 17:06:20 -0700 | [diff] [blame] | 147 | } |
| 148 | } |
| 149 | |
| 150 | void dump_registers(const ptrace_context_t* context __attribute((unused)), |
| 151 | log_t* log, pid_t tid, bool at_fault) |
| 152 | { |
| 153 | pt_regs_mips_t r; |
Christopher Tate | 7716aef | 2013-04-02 14:00:27 -0700 | [diff] [blame] | 154 | int scopeFlags = at_fault ? SCOPE_AT_FAULT : 0; |
Chris Dearman | 231e3c8 | 2012-08-10 17:06:20 -0700 | [diff] [blame] | 155 | |
| 156 | if(ptrace(PTRACE_GETREGS, tid, 0, &r)) { |
Christopher Tate | 7716aef | 2013-04-02 14:00:27 -0700 | [diff] [blame] | 157 | _LOG(log, scopeFlags, "cannot get registers: %s\n", strerror(errno)); |
Chris Dearman | 231e3c8 | 2012-08-10 17:06:20 -0700 | [diff] [blame] | 158 | return; |
| 159 | } |
| 160 | |
Christopher Tate | 7716aef | 2013-04-02 14:00:27 -0700 | [diff] [blame] | 161 | _LOG(log, scopeFlags, " zr %08x at %08x v0 %08x v1 %08x\n", |
Chris Dearman | 231e3c8 | 2012-08-10 17:06:20 -0700 | [diff] [blame] | 162 | R(r.regs[0]), R(r.regs[1]), R(r.regs[2]), R(r.regs[3])); |
Christopher Tate | 7716aef | 2013-04-02 14:00:27 -0700 | [diff] [blame] | 163 | _LOG(log, scopeFlags, " a0 %08x a1 %08x a2 %08x a3 %08x\n", |
Chris Dearman | 231e3c8 | 2012-08-10 17:06:20 -0700 | [diff] [blame] | 164 | R(r.regs[4]), R(r.regs[5]), R(r.regs[6]), R(r.regs[7])); |
Christopher Tate | 7716aef | 2013-04-02 14:00:27 -0700 | [diff] [blame] | 165 | _LOG(log, scopeFlags, " t0 %08x t1 %08x t2 %08x t3 %08x\n", |
Chris Dearman | 231e3c8 | 2012-08-10 17:06:20 -0700 | [diff] [blame] | 166 | R(r.regs[8]), R(r.regs[9]), R(r.regs[10]), R(r.regs[11])); |
Christopher Tate | 7716aef | 2013-04-02 14:00:27 -0700 | [diff] [blame] | 167 | _LOG(log, scopeFlags, " t4 %08x t5 %08x t6 %08x t7 %08x\n", |
Chris Dearman | 231e3c8 | 2012-08-10 17:06:20 -0700 | [diff] [blame] | 168 | R(r.regs[12]), R(r.regs[13]), R(r.regs[14]), R(r.regs[15])); |
Christopher Tate | 7716aef | 2013-04-02 14:00:27 -0700 | [diff] [blame] | 169 | _LOG(log, scopeFlags, " s0 %08x s1 %08x s2 %08x s3 %08x\n", |
Chris Dearman | 231e3c8 | 2012-08-10 17:06:20 -0700 | [diff] [blame] | 170 | R(r.regs[16]), R(r.regs[17]), R(r.regs[18]), R(r.regs[19])); |
Christopher Tate | 7716aef | 2013-04-02 14:00:27 -0700 | [diff] [blame] | 171 | _LOG(log, scopeFlags, " s4 %08x s5 %08x s6 %08x s7 %08x\n", |
Chris Dearman | 231e3c8 | 2012-08-10 17:06:20 -0700 | [diff] [blame] | 172 | R(r.regs[20]), R(r.regs[21]), R(r.regs[22]), R(r.regs[23])); |
Christopher Tate | 7716aef | 2013-04-02 14:00:27 -0700 | [diff] [blame] | 173 | _LOG(log, scopeFlags, " t8 %08x t9 %08x k0 %08x k1 %08x\n", |
Chris Dearman | 231e3c8 | 2012-08-10 17:06:20 -0700 | [diff] [blame] | 174 | R(r.regs[24]), R(r.regs[25]), R(r.regs[26]), R(r.regs[27])); |
Christopher Tate | 7716aef | 2013-04-02 14:00:27 -0700 | [diff] [blame] | 175 | _LOG(log, scopeFlags, " gp %08x sp %08x s8 %08x ra %08x\n", |
Chris Dearman | 231e3c8 | 2012-08-10 17:06:20 -0700 | [diff] [blame] | 176 | R(r.regs[28]), R(r.regs[29]), R(r.regs[30]), R(r.regs[31])); |
Christopher Tate | 7716aef | 2013-04-02 14:00:27 -0700 | [diff] [blame] | 177 | _LOG(log, scopeFlags, " hi %08x lo %08x bva %08x epc %08x\n", |
Chris Dearman | 231e3c8 | 2012-08-10 17:06:20 -0700 | [diff] [blame] | 178 | R(r.hi), R(r.lo), R(r.cp0_badvaddr), R(r.cp0_epc)); |
| 179 | } |