blob: 489f3c58d85d61bd45b2902ef74b0f300f8f9c56 [file] [log] [blame]
Chris Dearman231e3c82012-08-10 17:06:20 -07001/* 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
Elliott Hughes76e7f5e2013-11-20 11:31:29 -080029#include <sys/user.h>
Chris Dearman231e3c82012-08-10 17:06:20 -070030
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 Ferris365e4ae2013-10-02 12:26:48 -070039static void dump_memory(log_t* log, pid_t tid, uintptr_t addr, int scope_flags) {
Chris Dearman231e3c82012-08-10 17:06:20 -070040 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 Ferris365e4ae2013-10-02 12:26:48 -070095 _LOG(log, scope_flags, " %s %s\n", code_buffer, ascii_buffer);
Chris Dearman231e3c82012-08-10 17:06:20 -070096 }
97}
98
99/*
100 * If configured to do so, dump memory around *all* registers
101 * for the crashing thread.
102 */
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700103void dump_memory_and_code(log_t* log, pid_t tid, int scope_flags) {
Chris Dearman231e3c82012-08-10 17:06:20 -0700104 pt_regs_mips_t r;
105 if(ptrace(PTRACE_GETREGS, tid, 0, &r)) {
106 return;
107 }
108
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700109 if (IS_AT_FAULT(scope_flags) && DUMP_MEMORY_FOR_ALL_REGISTERS) {
Chris Dearman231e3c82012-08-10 17:06:20 -0700110 static const char REG_NAMES[] = "$0atv0v1a0a1a2a3t0t1t2t3t4t5t6t7s0s1s2s3s4s5s6s7t8t9k0k1gpsps8ra";
111
112 for (int reg = 0; reg < 32; reg++) {
113 /* skip uninteresting registers */
114 if (reg == 0 /* $0 */
115 || reg == 26 /* $k0 */
116 || reg == 27 /* $k1 */
117 || reg == 31 /* $ra (done below) */
118 )
119 continue;
120
121 uintptr_t addr = R(r.regs[reg]);
122
123 /*
124 * Don't bother if it looks like a small int or ~= null, or if
125 * it's in the kernel area.
126 */
127 if (addr < 4096 || addr >= 0x80000000) {
128 continue;
129 }
130
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700131 _LOG(log, scope_flags | SCOPE_SENSITIVE, "\nmemory near %.2s:\n", &REG_NAMES[reg * 2]);
132 dump_memory(log, tid, addr, scope_flags | SCOPE_SENSITIVE);
Chris Dearman231e3c82012-08-10 17:06:20 -0700133 }
134 }
135
136 unsigned int pc = R(r.cp0_epc);
137 unsigned int ra = R(r.regs[31]);
138
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700139 _LOG(log, scope_flags, "\ncode around pc:\n");
140 dump_memory(log, tid, (uintptr_t)pc, scope_flags);
Chris Dearman231e3c82012-08-10 17:06:20 -0700141
142 if (pc != ra) {
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700143 _LOG(log, scope_flags, "\ncode around ra:\n");
144 dump_memory(log, tid, (uintptr_t)ra, scope_flags);
Chris Dearman231e3c82012-08-10 17:06:20 -0700145 }
146}
147
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700148void dump_registers(log_t* log, pid_t tid, int scope_flags)
Chris Dearman231e3c82012-08-10 17:06:20 -0700149{
150 pt_regs_mips_t r;
Chris Dearman231e3c82012-08-10 17:06:20 -0700151 if(ptrace(PTRACE_GETREGS, tid, 0, &r)) {
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700152 _LOG(log, scope_flags, "cannot get registers: %s\n", strerror(errno));
Chris Dearman231e3c82012-08-10 17:06:20 -0700153 return;
154 }
155
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700156 _LOG(log, scope_flags, " zr %08x at %08x v0 %08x v1 %08x\n",
Chris Dearman231e3c82012-08-10 17:06:20 -0700157 R(r.regs[0]), R(r.regs[1]), R(r.regs[2]), R(r.regs[3]));
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700158 _LOG(log, scope_flags, " a0 %08x a1 %08x a2 %08x a3 %08x\n",
Chris Dearman231e3c82012-08-10 17:06:20 -0700159 R(r.regs[4]), R(r.regs[5]), R(r.regs[6]), R(r.regs[7]));
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700160 _LOG(log, scope_flags, " t0 %08x t1 %08x t2 %08x t3 %08x\n",
Chris Dearman231e3c82012-08-10 17:06:20 -0700161 R(r.regs[8]), R(r.regs[9]), R(r.regs[10]), R(r.regs[11]));
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700162 _LOG(log, scope_flags, " t4 %08x t5 %08x t6 %08x t7 %08x\n",
Chris Dearman231e3c82012-08-10 17:06:20 -0700163 R(r.regs[12]), R(r.regs[13]), R(r.regs[14]), R(r.regs[15]));
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700164 _LOG(log, scope_flags, " s0 %08x s1 %08x s2 %08x s3 %08x\n",
Chris Dearman231e3c82012-08-10 17:06:20 -0700165 R(r.regs[16]), R(r.regs[17]), R(r.regs[18]), R(r.regs[19]));
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700166 _LOG(log, scope_flags, " s4 %08x s5 %08x s6 %08x s7 %08x\n",
Chris Dearman231e3c82012-08-10 17:06:20 -0700167 R(r.regs[20]), R(r.regs[21]), R(r.regs[22]), R(r.regs[23]));
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700168 _LOG(log, scope_flags, " t8 %08x t9 %08x k0 %08x k1 %08x\n",
Chris Dearman231e3c82012-08-10 17:06:20 -0700169 R(r.regs[24]), R(r.regs[25]), R(r.regs[26]), R(r.regs[27]));
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700170 _LOG(log, scope_flags, " gp %08x sp %08x s8 %08x ra %08x\n",
Chris Dearman231e3c82012-08-10 17:06:20 -0700171 R(r.regs[28]), R(r.regs[29]), R(r.regs[30]), R(r.regs[31]));
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700172 _LOG(log, scope_flags, " hi %08x lo %08x bva %08x epc %08x\n",
Chris Dearman231e3c82012-08-10 17:06:20 -0700173 R(r.hi), R(r.lo), R(r.cp0_badvaddr), R(r.cp0_epc));
174}