blob: 64e59809ba1047c20b1581a3b44b2bd853e702ea [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001/* system/debuggerd/utility.c
2**
3** Copyright 2008, 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
Andy McFadden136dcc52011-09-22 16:37:06 -070018#include <signal.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080019#include <string.h>
Jeff Brown13e715b2011-10-21 12:14:56 -070020#include <cutils/logd.h>
21#include <sys/ptrace.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080022#include <errno.h>
Jeff Brown13e715b2011-10-21 12:14:56 -070023#include <corkscrew/demangle.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080024
25#include "utility.h"
26
Jeff Brown13e715b2011-10-21 12:14:56 -070027#define STACK_DEPTH 32
28#define STACK_WORDS 16
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080029
Jeff Brown13e715b2011-10-21 12:14:56 -070030void _LOG(int tfd, bool in_tombstone_only, const char *fmt, ...) {
31 char buf[512];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080032
Jeff Brown13e715b2011-10-21 12:14:56 -070033 va_list ap;
34 va_start(ap, fmt);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080035
Jeff Brown13e715b2011-10-21 12:14:56 -070036 if (tfd >= 0) {
37 int len;
38 vsnprintf(buf, sizeof(buf), fmt, ap);
39 len = strlen(buf);
40 if(tfd >= 0) write(tfd, buf, len);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080041 }
42
Jeff Brown13e715b2011-10-21 12:14:56 -070043 if (!in_tombstone_only)
44 __android_log_vprint(ANDROID_LOG_INFO, "DEBUG", fmt, ap);
45 va_end(ap);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080046}
47
Jeff Brown13e715b2011-10-21 12:14:56 -070048bool signal_has_address(int sig) {
Andy McFadden136dcc52011-09-22 16:37:06 -070049 switch (sig) {
50 case SIGILL:
51 case SIGFPE:
52 case SIGSEGV:
53 case SIGBUS:
54 return true;
55 default:
56 return false;
57 }
58}
Jeff Brown13e715b2011-10-21 12:14:56 -070059
Jeff Brownf0c58722011-11-03 17:58:44 -070060static void dump_backtrace(const ptrace_context_t* context __attribute((unused)),
61 int tfd, pid_t tid __attribute((unused)), bool at_fault,
Jeff Brown13e715b2011-10-21 12:14:56 -070062 const backtrace_frame_t* backtrace, size_t frames) {
63 _LOG(tfd, !at_fault, "\nbacktrace:\n");
64
65 backtrace_symbol_t backtrace_symbols[STACK_DEPTH];
66 get_backtrace_symbols_ptrace(context, backtrace, frames, backtrace_symbols);
67 for (size_t i = 0; i < frames; i++) {
68 const backtrace_symbol_t* symbol = &backtrace_symbols[i];
Jeff Brownf0c58722011-11-03 17:58:44 -070069 const char* map_name = symbol->map_name ? symbol->map_name : "<unknown>";
Jeff Brown13e715b2011-10-21 12:14:56 -070070 const char* symbol_name = symbol->demangled_name ? symbol->demangled_name : symbol->name;
71 if (symbol_name) {
72 _LOG(tfd, !at_fault, " #%02d pc %08x %s (%s)\n",
73 (int)i, symbol->relative_pc, map_name, symbol_name);
74 } else {
75 _LOG(tfd, !at_fault, " #%02d pc %08x %s\n",
76 (int)i, symbol->relative_pc, map_name);
77 }
78 }
79 free_backtrace_symbols(backtrace_symbols, frames);
80}
81
Jeff Brownf0c58722011-11-03 17:58:44 -070082static void dump_stack_segment(const ptrace_context_t* context, int tfd, pid_t tid,
Jeff Brown13e715b2011-10-21 12:14:56 -070083 bool only_in_tombstone, uintptr_t* sp, size_t words, int label) {
84 for (size_t i = 0; i < words; i++) {
85 uint32_t stack_content;
Jeff Brownf0c58722011-11-03 17:58:44 -070086 if (!try_get_word_ptrace(tid, *sp, &stack_content)) {
Jeff Brown13e715b2011-10-21 12:14:56 -070087 break;
88 }
89
90 const map_info_t* mi;
91 const symbol_t* symbol;
92 find_symbol_ptrace(context, stack_content, &mi, &symbol);
93
94 if (symbol) {
95 char* demangled_name = demangle_symbol_name(symbol->name);
96 const char* symbol_name = demangled_name ? demangled_name : symbol->name;
97 if (!i && label >= 0) {
98 _LOG(tfd, only_in_tombstone, " #%02d %08x %08x %s (%s)\n",
99 label, *sp, stack_content, mi ? mi->name : "", symbol_name);
100 } else {
101 _LOG(tfd, only_in_tombstone, " %08x %08x %s (%s)\n",
102 *sp, stack_content, mi ? mi->name : "", symbol_name);
103 }
104 free(demangled_name);
105 } else {
106 if (!i && label >= 0) {
107 _LOG(tfd, only_in_tombstone, " #%02d %08x %08x %s\n",
108 label, *sp, stack_content, mi ? mi->name : "");
109 } else {
110 _LOG(tfd, only_in_tombstone, " %08x %08x %s\n",
111 *sp, stack_content, mi ? mi->name : "");
112 }
113 }
114
115 *sp += sizeof(uint32_t);
116 }
117}
118
Jeff Brownf0c58722011-11-03 17:58:44 -0700119static void dump_stack(const ptrace_context_t* context, int tfd, pid_t tid, bool at_fault,
Jeff Brown13e715b2011-10-21 12:14:56 -0700120 const backtrace_frame_t* backtrace, size_t frames) {
121 bool have_first = false;
122 size_t first, last;
123 for (size_t i = 0; i < frames; i++) {
124 if (backtrace[i].stack_top) {
125 if (!have_first) {
126 have_first = true;
127 first = i;
128 }
129 last = i;
130 }
131 }
132 if (!have_first) {
133 return;
134 }
135
136 _LOG(tfd, !at_fault, "\nstack:\n");
137
138 // Dump a few words before the first frame.
139 bool only_in_tombstone = !at_fault;
140 uintptr_t sp = backtrace[first].stack_top - STACK_WORDS * sizeof(uint32_t);
Jeff Brownf0c58722011-11-03 17:58:44 -0700141 dump_stack_segment(context, tfd, tid, only_in_tombstone, &sp, STACK_WORDS, -1);
Jeff Brown13e715b2011-10-21 12:14:56 -0700142
143 // Dump a few words from all successive frames.
144 // Only log the first 3 frames, put the rest in the tombstone.
145 for (size_t i = first; i <= last; i++) {
146 const backtrace_frame_t* frame = &backtrace[i];
147 if (sp != frame->stack_top) {
148 _LOG(tfd, only_in_tombstone, " ........ ........\n");
149 sp = frame->stack_top;
150 }
151 if (i - first == 3) {
152 only_in_tombstone = true;
153 }
154 if (i == last) {
Jeff Brownf0c58722011-11-03 17:58:44 -0700155 dump_stack_segment(context, tfd, tid, only_in_tombstone, &sp, STACK_WORDS, i);
Jeff Brown13e715b2011-10-21 12:14:56 -0700156 if (sp < frame->stack_top + frame->stack_size) {
157 _LOG(tfd, only_in_tombstone, " ........ ........\n");
158 }
159 } else {
160 size_t words = frame->stack_size / sizeof(uint32_t);
161 if (words == 0) {
162 words = 1;
163 } else if (words > STACK_WORDS) {
164 words = STACK_WORDS;
165 }
Jeff Brownf0c58722011-11-03 17:58:44 -0700166 dump_stack_segment(context, tfd, tid, only_in_tombstone, &sp, words, i);
Jeff Brown13e715b2011-10-21 12:14:56 -0700167 }
168 }
169}
170
Jeff Brownf0c58722011-11-03 17:58:44 -0700171void dump_backtrace_and_stack(const ptrace_context_t* context, int tfd, pid_t tid,
172 bool at_fault) {
Jeff Brown13e715b2011-10-21 12:14:56 -0700173 backtrace_frame_t backtrace[STACK_DEPTH];
174 ssize_t frames = unwind_backtrace_ptrace(tid, context, backtrace, 0, STACK_DEPTH);
175 if (frames > 0) {
176 dump_backtrace(context, tfd, tid, at_fault, backtrace, frames);
177 dump_stack(context, tfd, tid, at_fault, backtrace, frames);
178 }
179}
180
181void dump_memory(int tfd, pid_t tid, uintptr_t addr, bool at_fault) {
182 char code_buffer[64]; /* actual 8+1+((8+1)*4) + 1 == 45 */
183 char ascii_buffer[32]; /* actual 16 + 1 == 17 */
184 uintptr_t p, end;
185
186 p = addr & ~3;
187 p -= 32;
188 if (p > addr) {
189 /* catch underflow */
190 p = 0;
191 }
192 end = p + 80;
193 /* catch overflow; 'end - p' has to be multiples of 16 */
194 while (end < p)
195 end -= 16;
196
197 /* Dump the code around PC as:
198 * addr contents ascii
199 * 00008d34 ef000000 e8bd0090 e1b00000 512fff1e ............../Q
200 * 00008d44 ea00b1f9 e92d0090 e3a070fc ef000000 ......-..p......
201 */
202 while (p < end) {
203 char* asc_out = ascii_buffer;
204
205 sprintf(code_buffer, "%08x ", p);
206
207 int i;
208 for (i = 0; i < 4; i++) {
209 /*
210 * If we see (data == -1 && errno != 0), we know that the ptrace
211 * call failed, probably because we're dumping memory in an
212 * unmapped or inaccessible page. I don't know if there's
213 * value in making that explicit in the output -- it likely
214 * just complicates parsing and clarifies nothing for the
215 * enlightened reader.
216 */
217 long data = ptrace(PTRACE_PEEKTEXT, tid, (void*)p, NULL);
218 sprintf(code_buffer + strlen(code_buffer), "%08lx ", data);
219
220 int j;
221 for (j = 0; j < 4; j++) {
222 /*
223 * Our isprint() allows high-ASCII characters that display
224 * differently (often badly) in different viewers, so we
225 * just use a simpler test.
226 */
227 char val = (data >> (j*8)) & 0xff;
228 if (val >= 0x20 && val < 0x7f) {
229 *asc_out++ = val;
230 } else {
231 *asc_out++ = '.';
232 }
233 }
234 p += 4;
235 }
236 *asc_out = '\0';
237 _LOG(tfd, !at_fault, " %s %s\n", code_buffer, ascii_buffer);
238 }
239}
240
Jeff Brownf0c58722011-11-03 17:58:44 -0700241void dump_nearby_maps(const ptrace_context_t* context, int tfd, pid_t tid) {
Jeff Brown13e715b2011-10-21 12:14:56 -0700242 siginfo_t si;
243 memset(&si, 0, sizeof(si));
244 if (ptrace(PTRACE_GETSIGINFO, tid, 0, &si)) {
245 _LOG(tfd, false, "cannot get siginfo for %d: %s\n",
246 tid, strerror(errno));
247 return;
248 }
249 if (!signal_has_address(si.si_signo)) {
250 return;
251 }
252
253 uintptr_t addr = (uintptr_t) si.si_addr;
254 addr &= ~0xfff; /* round to 4K page boundary */
255 if (addr == 0) { /* null-pointer deref */
256 return;
257 }
258
259 _LOG(tfd, false, "\nmemory map around fault addr %08x:\n", (int)si.si_addr);
260
261 /*
262 * Search for a match, or for a hole where the match would be. The list
263 * is backward from the file content, so it starts at high addresses.
264 */
265 bool found = false;
266 map_info_t* map = context->map_info_list;
267 map_info_t *next = NULL;
268 map_info_t *prev = NULL;
269 while (map != NULL) {
270 if (addr >= map->start && addr < map->end) {
271 found = true;
272 next = map->next;
273 break;
274 } else if (addr >= map->end) {
275 /* map would be between "prev" and this entry */
276 next = map;
277 map = NULL;
278 break;
279 }
280
281 prev = map;
282 map = map->next;
283 }
284
285 /*
286 * Show "next" then "match" then "prev" so that the addresses appear in
287 * ascending order (like /proc/pid/maps).
288 */
289 if (next != NULL) {
290 _LOG(tfd, false, " %08x-%08x %s\n", next->start, next->end, next->name);
291 } else {
292 _LOG(tfd, false, " (no map below)\n");
293 }
294 if (map != NULL) {
295 _LOG(tfd, false, " %08x-%08x %s\n", map->start, map->end, map->name);
296 } else {
297 _LOG(tfd, false, " (no map for address)\n");
298 }
299 if (prev != NULL) {
300 _LOG(tfd, false, " %08x-%08x %s\n", prev->start, prev->end, prev->name);
301 } else {
302 _LOG(tfd, false, " (no map above)\n");
303 }
304}