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 39static void dump_memory(log_t* log, pid_t tid, uintptr_t addr, bool at_fault) { 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'; 95 _LOG(log, !at_fault, " %s %s\n", code_buffer, ascii_buffer); 96 } 97} 98 99/* 100 * If configured to do so, dump memory around *all* registers 101 * for the crashing thread. 102 */ 103void 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 110 if (at_fault && DUMP_MEMORY_FOR_ALL_REGISTERS) { 111 static const char REG_NAMES[] = "$0atv0v1a0a1a2a3t0t1t2t3t4t5t6t7s0s1s2s3s4s5s6s7t8t9k0k1gpsps8ra"; 112 113 for (int reg = 0; reg < 32; reg++) { 114 /* skip uninteresting registers */ 115 if (reg == 0 /* $0 */ 116 || reg == 26 /* $k0 */ 117 || reg == 27 /* $k1 */ 118 || reg == 31 /* $ra (done below) */ 119 ) 120 continue; 121 122 uintptr_t addr = R(r.regs[reg]); 123 124 /* 125 * Don't bother if it looks like a small int or ~= null, or if 126 * it's in the kernel area. 127 */ 128 if (addr < 4096 || addr >= 0x80000000) { 129 continue; 130 } 131 132 _LOG(log, false, "\nmemory near %.2s:\n", ®_NAMES[reg * 2]); 133 dump_memory(log, tid, addr, at_fault); 134 } 135 } 136 137 unsigned int pc = R(r.cp0_epc); 138 unsigned int ra = R(r.regs[31]); 139 140 _LOG(log, !at_fault, "\ncode around pc:\n"); 141 dump_memory(log, tid, (uintptr_t)pc, at_fault); 142 143 if (pc != ra) { 144 _LOG(log, !at_fault, "\ncode around ra:\n"); 145 dump_memory(log, tid, (uintptr_t)ra, at_fault); 146 } 147} 148 149void dump_registers(const ptrace_context_t* context __attribute((unused)), 150 log_t* log, pid_t tid, bool at_fault) 151{ 152 pt_regs_mips_t r; 153 bool only_in_tombstone = !at_fault; 154 155 if(ptrace(PTRACE_GETREGS, tid, 0, &r)) { 156 _LOG(log, only_in_tombstone, "cannot get registers: %s\n", strerror(errno)); 157 return; 158 } 159 160 _LOG(log, only_in_tombstone, " zr %08x at %08x v0 %08x v1 %08x\n", 161 R(r.regs[0]), R(r.regs[1]), R(r.regs[2]), R(r.regs[3])); 162 _LOG(log, only_in_tombstone, " a0 %08x a1 %08x a2 %08x a3 %08x\n", 163 R(r.regs[4]), R(r.regs[5]), R(r.regs[6]), R(r.regs[7])); 164 _LOG(log, only_in_tombstone, " t0 %08x t1 %08x t2 %08x t3 %08x\n", 165 R(r.regs[8]), R(r.regs[9]), R(r.regs[10]), R(r.regs[11])); 166 _LOG(log, only_in_tombstone, " t4 %08x t5 %08x t6 %08x t7 %08x\n", 167 R(r.regs[12]), R(r.regs[13]), R(r.regs[14]), R(r.regs[15])); 168 _LOG(log, only_in_tombstone, " s0 %08x s1 %08x s2 %08x s3 %08x\n", 169 R(r.regs[16]), R(r.regs[17]), R(r.regs[18]), R(r.regs[19])); 170 _LOG(log, only_in_tombstone, " s4 %08x s5 %08x s6 %08x s7 %08x\n", 171 R(r.regs[20]), R(r.regs[21]), R(r.regs[22]), R(r.regs[23])); 172 _LOG(log, only_in_tombstone, " t8 %08x t9 %08x k0 %08x k1 %08x\n", 173 R(r.regs[24]), R(r.regs[25]), R(r.regs[26]), R(r.regs[27])); 174 _LOG(log, only_in_tombstone, " gp %08x sp %08x s8 %08x ra %08x\n", 175 R(r.regs[28]), R(r.regs[29]), R(r.regs[30]), R(r.regs[31])); 176 _LOG(log, only_in_tombstone, " hi %08x lo %08x bva %08x epc %08x\n", 177 R(r.hi), R(r.lo), R(r.cp0_badvaddr), R(r.cp0_epc)); 178} 179