blob: 1b08e8e1296ef986823248839e7372d3e1e79457 [file] [log] [blame]
Jeff Brown053b8652012-06-06 16:25:03 -07001/*
2 * Copyright (C) 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 */
16
17#include <stddef.h>
18#include <stdbool.h>
19#include <stdlib.h>
20#include <signal.h>
21#include <string.h>
22#include <stdio.h>
23#include <fcntl.h>
24#include <errno.h>
25#include <dirent.h>
26#include <time.h>
27#include <sys/ptrace.h>
28#include <sys/stat.h>
29
30#include <private/android_filesystem_config.h>
31
Colin Cross9227bd32013-07-23 16:59:20 -070032#include <log/logger.h>
Jeff Brown053b8652012-06-06 16:25:03 -070033#include <cutils/properties.h>
34
Christopher Ferris365e4ae2013-10-02 12:26:48 -070035#include <backtrace/backtrace.h>
Jeff Brown053b8652012-06-06 16:25:03 -070036
Christopher Tateded2e5a2013-03-19 13:12:23 -070037#include <sys/socket.h>
38#include <linux/un.h>
39
rpcraigf1186f32012-07-19 09:38:06 -040040#include <selinux/android.h>
rpcraigf1186f32012-07-19 09:38:06 -040041
Jeff Brown053b8652012-06-06 16:25:03 -070042#include "machine.h"
43#include "tombstone.h"
Christopher Ferris365e4ae2013-10-02 12:26:48 -070044#include "backtrace.h"
Jeff Brown053b8652012-06-06 16:25:03 -070045
Jeff Brown053b8652012-06-06 16:25:03 -070046#define STACK_WORDS 16
47
48#define MAX_TOMBSTONES 10
49#define TOMBSTONE_DIR "/data/tombstones"
50
Christopher Tateded2e5a2013-03-19 13:12:23 -070051/* Must match the path defined in NativeCrashListener.java */
52#define NCRASH_SOCKET_PATH "/data/system/ndebugsocket"
53
Jeff Brown053b8652012-06-06 16:25:03 -070054#define typecheck(x,y) { \
55 typeof(x) __dummy1; \
56 typeof(y) __dummy2; \
57 (void)(&__dummy1 == &__dummy2); }
58
59
60static bool signal_has_address(int sig) {
61 switch (sig) {
62 case SIGILL:
63 case SIGFPE:
64 case SIGSEGV:
65 case SIGBUS:
66 return true;
67 default:
68 return false;
69 }
70}
71
72static const char *get_signame(int sig)
73{
74 switch(sig) {
75 case SIGILL: return "SIGILL";
76 case SIGABRT: return "SIGABRT";
77 case SIGBUS: return "SIGBUS";
78 case SIGFPE: return "SIGFPE";
79 case SIGSEGV: return "SIGSEGV";
80 case SIGPIPE: return "SIGPIPE";
Chris Dearman231e3c82012-08-10 17:06:20 -070081#ifdef SIGSTKFLT
Jeff Brown053b8652012-06-06 16:25:03 -070082 case SIGSTKFLT: return "SIGSTKFLT";
Chris Dearman231e3c82012-08-10 17:06:20 -070083#endif
Jeff Brown053b8652012-06-06 16:25:03 -070084 case SIGSTOP: return "SIGSTOP";
85 default: return "?";
86 }
87}
88
89static const char *get_sigcode(int signo, int code)
90{
Elliott Hughes8f7d4432012-12-10 10:29:05 -080091 // Try the signal-specific codes...
Jeff Brown053b8652012-06-06 16:25:03 -070092 switch (signo) {
93 case SIGILL:
94 switch (code) {
95 case ILL_ILLOPC: return "ILL_ILLOPC";
96 case ILL_ILLOPN: return "ILL_ILLOPN";
97 case ILL_ILLADR: return "ILL_ILLADR";
98 case ILL_ILLTRP: return "ILL_ILLTRP";
99 case ILL_PRVOPC: return "ILL_PRVOPC";
100 case ILL_PRVREG: return "ILL_PRVREG";
101 case ILL_COPROC: return "ILL_COPROC";
102 case ILL_BADSTK: return "ILL_BADSTK";
103 }
104 break;
105 case SIGBUS:
106 switch (code) {
107 case BUS_ADRALN: return "BUS_ADRALN";
108 case BUS_ADRERR: return "BUS_ADRERR";
109 case BUS_OBJERR: return "BUS_OBJERR";
110 }
111 break;
112 case SIGFPE:
113 switch (code) {
114 case FPE_INTDIV: return "FPE_INTDIV";
115 case FPE_INTOVF: return "FPE_INTOVF";
116 case FPE_FLTDIV: return "FPE_FLTDIV";
117 case FPE_FLTOVF: return "FPE_FLTOVF";
118 case FPE_FLTUND: return "FPE_FLTUND";
119 case FPE_FLTRES: return "FPE_FLTRES";
120 case FPE_FLTINV: return "FPE_FLTINV";
121 case FPE_FLTSUB: return "FPE_FLTSUB";
122 }
123 break;
124 case SIGSEGV:
125 switch (code) {
126 case SEGV_MAPERR: return "SEGV_MAPERR";
127 case SEGV_ACCERR: return "SEGV_ACCERR";
128 }
129 break;
Elliott Hughes8f7d4432012-12-10 10:29:05 -0800130 case SIGTRAP:
131 switch (code) {
132 case TRAP_BRKPT: return "TRAP_BRKPT";
133 case TRAP_TRACE: return "TRAP_TRACE";
134 }
135 break;
Jeff Brown053b8652012-06-06 16:25:03 -0700136 }
Elliott Hughes8f7d4432012-12-10 10:29:05 -0800137 // Then the other codes...
138 switch (code) {
139 case SI_USER: return "SI_USER";
140#if defined(SI_KERNEL)
141 case SI_KERNEL: return "SI_KERNEL";
142#endif
143 case SI_QUEUE: return "SI_QUEUE";
144 case SI_TIMER: return "SI_TIMER";
145 case SI_MESGQ: return "SI_MESGQ";
146 case SI_ASYNCIO: return "SI_ASYNCIO";
147#if defined(SI_SIGIO)
148 case SI_SIGIO: return "SI_SIGIO";
149#endif
150#if defined(SI_TKILL)
151 case SI_TKILL: return "SI_TKILL";
152#endif
153 }
154 // Then give up...
Jeff Brown053b8652012-06-06 16:25:03 -0700155 return "?";
156}
157
Ben Chengd7760c12012-09-19 16:04:01 -0700158static void dump_revision_info(log_t* log)
159{
160 char revision[PROPERTY_VALUE_MAX];
161
162 property_get("ro.revision", revision, "unknown");
163
Christopher Tate7716aef2013-04-02 14:00:27 -0700164 _LOG(log, SCOPE_AT_FAULT, "Revision: '%s'\n", revision);
Ben Chengd7760c12012-09-19 16:04:01 -0700165}
166
Jeff Brown053b8652012-06-06 16:25:03 -0700167static void dump_build_info(log_t* log)
168{
169 char fingerprint[PROPERTY_VALUE_MAX];
170
171 property_get("ro.build.fingerprint", fingerprint, "unknown");
172
Christopher Tate7716aef2013-04-02 14:00:27 -0700173 _LOG(log, SCOPE_AT_FAULT, "Build fingerprint: '%s'\n", fingerprint);
Jeff Brown053b8652012-06-06 16:25:03 -0700174}
175
176static void dump_fault_addr(log_t* log, pid_t tid, int sig)
177{
178 siginfo_t si;
179
180 memset(&si, 0, sizeof(si));
Christopher Tate7716aef2013-04-02 14:00:27 -0700181 if(ptrace(PTRACE_GETSIGINFO, tid, 0, &si)){
182 _LOG(log, SCOPE_AT_FAULT, "cannot get siginfo: %s\n", strerror(errno));
Jeff Brown053b8652012-06-06 16:25:03 -0700183 } else if (signal_has_address(sig)) {
Christopher Tate7716aef2013-04-02 14:00:27 -0700184 _LOG(log, SCOPE_AT_FAULT, "signal %d (%s), code %d (%s), fault addr %08x\n",
Jeff Brown053b8652012-06-06 16:25:03 -0700185 sig, get_signame(sig),
186 si.si_code, get_sigcode(sig, si.si_code),
187 (uintptr_t) si.si_addr);
188 } else {
Christopher Tate7716aef2013-04-02 14:00:27 -0700189 _LOG(log, SCOPE_AT_FAULT, "signal %d (%s), code %d (%s), fault addr --------\n",
Jeff Brown053b8652012-06-06 16:25:03 -0700190 sig, get_signame(sig), si.si_code, get_sigcode(sig, si.si_code));
191 }
192}
193
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700194static void dump_thread_info(log_t* log, pid_t pid, pid_t tid, int scope_flags) {
Jeff Brown053b8652012-06-06 16:25:03 -0700195 char path[64];
196 char threadnamebuf[1024];
197 char* threadname = NULL;
198 FILE *fp;
199
200 snprintf(path, sizeof(path), "/proc/%d/comm", tid);
201 if ((fp = fopen(path, "r"))) {
202 threadname = fgets(threadnamebuf, sizeof(threadnamebuf), fp);
203 fclose(fp);
204 if (threadname) {
205 size_t len = strlen(threadname);
206 if (len && threadname[len - 1] == '\n') {
207 threadname[len - 1] = '\0';
208 }
209 }
210 }
211
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700212 if (IS_AT_FAULT(scope_flags)) {
Jeff Brown053b8652012-06-06 16:25:03 -0700213 char procnamebuf[1024];
214 char* procname = NULL;
215
216 snprintf(path, sizeof(path), "/proc/%d/cmdline", pid);
217 if ((fp = fopen(path, "r"))) {
218 procname = fgets(procnamebuf, sizeof(procnamebuf), fp);
219 fclose(fp);
220 }
221
Christopher Tate7716aef2013-04-02 14:00:27 -0700222 _LOG(log, SCOPE_AT_FAULT, "pid: %d, tid: %d, name: %s >>> %s <<<\n", pid, tid,
Jeff Brown053b8652012-06-06 16:25:03 -0700223 threadname ? threadname : "UNKNOWN",
224 procname ? procname : "UNKNOWN");
225 } else {
Christopher Tate7716aef2013-04-02 14:00:27 -0700226 _LOG(log, 0, "pid: %d, tid: %d, name: %s\n",
227 pid, tid, threadname ? threadname : "UNKNOWN");
Jeff Brown053b8652012-06-06 16:25:03 -0700228 }
229}
230
Christopher Ferris17e91d42013-10-21 13:30:52 -0700231static void dump_stack_segment(const backtrace_context_t* context, log_t* log,
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700232 int scope_flags, uintptr_t *sp, size_t words, int label) {
Jeff Brown053b8652012-06-06 16:25:03 -0700233 for (size_t i = 0; i < words; i++) {
234 uint32_t stack_content;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700235 if (!backtrace_read_word(context, *sp, &stack_content)) {
Jeff Brown053b8652012-06-06 16:25:03 -0700236 break;
237 }
238
Christopher Ferris17e91d42013-10-21 13:30:52 -0700239 const char* map_name = backtrace_get_map_name(context, stack_content, NULL);
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700240 if (!map_name) {
241 map_name = "";
242 }
243 uintptr_t offset = 0;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700244 char* func_name = backtrace_get_func_name(context, stack_content, &offset);
245 if (func_name) {
Jeff Brown053b8652012-06-06 16:25:03 -0700246 if (!i && label >= 0) {
247 if (offset) {
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700248 _LOG(log, scope_flags, " #%02d %08x %08x %s (%s+%u)\n",
Christopher Ferris17e91d42013-10-21 13:30:52 -0700249 label, *sp, stack_content, map_name, func_name, offset);
Jeff Brown053b8652012-06-06 16:25:03 -0700250 } else {
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700251 _LOG(log, scope_flags, " #%02d %08x %08x %s (%s)\n",
Christopher Ferris17e91d42013-10-21 13:30:52 -0700252 label, *sp, stack_content, map_name, func_name);
Jeff Brown053b8652012-06-06 16:25:03 -0700253 }
254 } else {
255 if (offset) {
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700256 _LOG(log, scope_flags, " %08x %08x %s (%s+%u)\n",
Christopher Ferris17e91d42013-10-21 13:30:52 -0700257 *sp, stack_content, map_name, func_name, offset);
Jeff Brown053b8652012-06-06 16:25:03 -0700258 } else {
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700259 _LOG(log, scope_flags, " %08x %08x %s (%s)\n",
Christopher Ferris17e91d42013-10-21 13:30:52 -0700260 *sp, stack_content, map_name, func_name);
Jeff Brown053b8652012-06-06 16:25:03 -0700261 }
262 }
Christopher Ferris17e91d42013-10-21 13:30:52 -0700263 free(func_name);
Jeff Brown053b8652012-06-06 16:25:03 -0700264 } else {
265 if (!i && label >= 0) {
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700266 _LOG(log, scope_flags, " #%02d %08x %08x %s\n",
267 label, *sp, stack_content, map_name);
Jeff Brown053b8652012-06-06 16:25:03 -0700268 } else {
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700269 _LOG(log, scope_flags, " %08x %08x %s\n",
270 *sp, stack_content, map_name);
Jeff Brown053b8652012-06-06 16:25:03 -0700271 }
272 }
273
274 *sp += sizeof(uint32_t);
275 }
276}
277
Christopher Ferris17e91d42013-10-21 13:30:52 -0700278static void dump_stack(const backtrace_context_t* context, log_t* log, int scope_flags) {
279 const backtrace_t* backtrace = context->backtrace;
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700280 size_t first = 0, last;
281 for (size_t i = 0; i < backtrace->num_frames; i++) {
282 if (backtrace->frames[i].sp) {
283 if (!first) {
284 first = i+1;
Jeff Brown053b8652012-06-06 16:25:03 -0700285 }
286 last = i;
287 }
288 }
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700289 if (!first) {
Jeff Brown053b8652012-06-06 16:25:03 -0700290 return;
291 }
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700292 first--;
Jeff Brown053b8652012-06-06 16:25:03 -0700293
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700294 scope_flags |= SCOPE_SENSITIVE;
Jeff Brown053b8652012-06-06 16:25:03 -0700295
296 // Dump a few words before the first frame.
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700297 uintptr_t sp = backtrace->frames[first].sp - STACK_WORDS * sizeof(uint32_t);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700298 dump_stack_segment(context, log, scope_flags, &sp, STACK_WORDS, -1);
Jeff Brown053b8652012-06-06 16:25:03 -0700299
300 // Dump a few words from all successive frames.
301 // Only log the first 3 frames, put the rest in the tombstone.
302 for (size_t i = first; i <= last; i++) {
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700303 const backtrace_frame_data_t* frame = &backtrace->frames[i];
304 if (sp != frame->sp) {
305 _LOG(log, scope_flags, " ........ ........\n");
306 sp = frame->sp;
Jeff Brown053b8652012-06-06 16:25:03 -0700307 }
308 if (i - first == 3) {
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700309 scope_flags &= (~SCOPE_AT_FAULT);
Jeff Brown053b8652012-06-06 16:25:03 -0700310 }
311 if (i == last) {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700312 dump_stack_segment(context, log, scope_flags, &sp, STACK_WORDS, i);
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700313 if (sp < frame->sp + frame->stack_size) {
314 _LOG(log, scope_flags, " ........ ........\n");
Jeff Brown053b8652012-06-06 16:25:03 -0700315 }
316 } else {
317 size_t words = frame->stack_size / sizeof(uint32_t);
318 if (words == 0) {
319 words = 1;
320 } else if (words > STACK_WORDS) {
321 words = STACK_WORDS;
322 }
Christopher Ferris17e91d42013-10-21 13:30:52 -0700323 dump_stack_segment(context, log, scope_flags, &sp, words, i);
Jeff Brown053b8652012-06-06 16:25:03 -0700324 }
325 }
326}
327
Christopher Ferris17e91d42013-10-21 13:30:52 -0700328static void dump_backtrace_and_stack(const backtrace_context_t* context,
329 log_t* log, int scope_flags) {
330 if (context->backtrace->num_frames) {
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700331 _LOG(log, scope_flags, "\nbacktrace:\n");
Christopher Ferris17e91d42013-10-21 13:30:52 -0700332 dump_backtrace_to_log(context, log, scope_flags, " ");
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700333
334 _LOG(log, scope_flags, "\nstack:\n");
Christopher Ferris17e91d42013-10-21 13:30:52 -0700335 dump_stack(context, log, scope_flags);
Jeff Brown053b8652012-06-06 16:25:03 -0700336 }
337}
338
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700339static void dump_map(log_t* log, const backtrace_map_info_t* m, const char* what, int scope_flags) {
Elliott Hughesd1420be2013-01-03 13:39:57 -0800340 if (m != NULL) {
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700341 _LOG(log, scope_flags, " %08x-%08x %c%c%c %s\n", m->start, m->end,
Elliott Hughesd1420be2013-01-03 13:39:57 -0800342 m->is_readable ? 'r' : '-',
343 m->is_writable ? 'w' : '-',
344 m->is_executable ? 'x' : '-',
345 m->name);
346 } else {
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700347 _LOG(log, scope_flags, " (no %s)\n", what);
Elliott Hughesd1420be2013-01-03 13:39:57 -0800348 }
349}
350
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700351static void dump_nearby_maps(const backtrace_map_info_t* map_info_list, log_t* log, pid_t tid, int scope_flags) {
352 scope_flags |= SCOPE_SENSITIVE;
Jeff Brown053b8652012-06-06 16:25:03 -0700353 siginfo_t si;
354 memset(&si, 0, sizeof(si));
355 if (ptrace(PTRACE_GETSIGINFO, tid, 0, &si)) {
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700356 _LOG(log, scope_flags, "cannot get siginfo for %d: %s\n",
Jeff Brown053b8652012-06-06 16:25:03 -0700357 tid, strerror(errno));
358 return;
359 }
360 if (!signal_has_address(si.si_signo)) {
361 return;
362 }
363
364 uintptr_t addr = (uintptr_t) si.si_addr;
365 addr &= ~0xfff; /* round to 4K page boundary */
366 if (addr == 0) { /* null-pointer deref */
367 return;
368 }
369
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700370 _LOG(log, scope_flags, "\nmemory map around fault addr %08x:\n", (int)si.si_addr);
Jeff Brown053b8652012-06-06 16:25:03 -0700371
372 /*
373 * Search for a match, or for a hole where the match would be. The list
374 * is backward from the file content, so it starts at high addresses.
375 */
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700376 const backtrace_map_info_t* map = map_info_list;
377 const backtrace_map_info_t* next = NULL;
378 const backtrace_map_info_t* prev = NULL;
Jeff Brown053b8652012-06-06 16:25:03 -0700379 while (map != NULL) {
380 if (addr >= map->start && addr < map->end) {
Jeff Brown053b8652012-06-06 16:25:03 -0700381 next = map->next;
382 break;
383 } else if (addr >= map->end) {
384 /* map would be between "prev" and this entry */
385 next = map;
386 map = NULL;
387 break;
388 }
389
390 prev = map;
391 map = map->next;
392 }
393
394 /*
395 * Show "next" then "match" then "prev" so that the addresses appear in
396 * ascending order (like /proc/pid/maps).
397 */
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700398 dump_map(log, next, "map below", scope_flags);
399 dump_map(log, map, "map for address", scope_flags);
400 dump_map(log, prev, "map above", scope_flags);
Jeff Brown053b8652012-06-06 16:25:03 -0700401}
402
Christopher Ferris17e91d42013-10-21 13:30:52 -0700403static void dump_thread(const backtrace_context_t* context, log_t* log,
404 int scope_flags, int* total_sleep_time_usec) {
405 const backtrace_t* backtrace = context->backtrace;
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700406 wait_for_stop(backtrace->tid, total_sleep_time_usec);
Jeff Brown053b8652012-06-06 16:25:03 -0700407
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700408 dump_registers(log, backtrace->tid, scope_flags);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700409 dump_backtrace_and_stack(context, log, scope_flags);
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700410 if (IS_AT_FAULT(scope_flags)) {
411 dump_memory_and_code(log, backtrace->tid, scope_flags);
412 dump_nearby_maps(backtrace->map_info_list, log, backtrace->tid, scope_flags);
Jeff Brown053b8652012-06-06 16:25:03 -0700413 }
414}
415
416/* Return true if some thread is not detached cleanly */
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700417static bool dump_sibling_thread_report(
Christopher Ferris98464972014-01-06 19:16:33 -0800418 log_t* log, pid_t pid, pid_t tid, int* total_sleep_time_usec, backtrace_map_info_t* map_info) {
Jeff Brown053b8652012-06-06 16:25:03 -0700419 char task_path[64];
420 snprintf(task_path, sizeof(task_path), "/proc/%d/task", pid);
421
422 DIR* d = opendir(task_path);
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700423 /* Bail early if the task directory cannot be opened */
Jeff Brown053b8652012-06-06 16:25:03 -0700424 if (d == NULL) {
425 XLOG("Cannot open /proc/%d/task\n", pid);
426 return false;
427 }
428
429 bool detach_failed = false;
Elliott Hughesc463d2c2012-10-26 16:47:09 -0700430 struct dirent* de;
431 while ((de = readdir(d)) != NULL) {
Jeff Brown053b8652012-06-06 16:25:03 -0700432 /* Ignore "." and ".." */
433 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, "..")) {
434 continue;
435 }
436
437 /* The main thread at fault has been handled individually */
438 char* end;
439 pid_t new_tid = strtoul(de->d_name, &end, 10);
440 if (*end || new_tid == tid) {
441 continue;
442 }
443
444 /* Skip this thread if cannot ptrace it */
445 if (ptrace(PTRACE_ATTACH, new_tid, 0, 0) < 0) {
446 continue;
447 }
448
Christopher Tate7716aef2013-04-02 14:00:27 -0700449 _LOG(log, 0, "--- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---\n");
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700450 dump_thread_info(log, pid, new_tid, 0);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700451 backtrace_context_t new_context;
Christopher Ferris98464972014-01-06 19:16:33 -0800452 if (backtrace_create_context_with_map(&new_context, pid, new_tid, 0, map_info)) {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700453 dump_thread(&new_context, log, 0, total_sleep_time_usec);
454 backtrace_destroy_context(&new_context);
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700455 }
Jeff Brown053b8652012-06-06 16:25:03 -0700456
457 if (ptrace(PTRACE_DETACH, new_tid, 0, 0) != 0) {
458 LOG("ptrace detach from %d failed: %s\n", new_tid, strerror(errno));
459 detach_failed = true;
460 }
461 }
462
463 closedir(d);
464 return detach_failed;
465}
466
467/*
468 * Reads the contents of the specified log device, filters out the entries
469 * that don't match the specified pid, and writes them to the tombstone file.
470 *
471 * If "tailOnly" is set, we only print the last few lines.
472 */
473static void dump_log_file(log_t* log, pid_t pid, const char* filename,
474 bool tailOnly)
475{
476 bool first = true;
477
478 /* circular buffer, for "tailOnly" mode */
479 const int kShortLogMaxLines = 5;
480 const int kShortLogLineLen = 256;
481 char shortLog[kShortLogMaxLines][kShortLogLineLen];
482 int shortLogCount = 0;
483 int shortLogNext = 0;
484
485 int logfd = open(filename, O_RDONLY | O_NONBLOCK);
486 if (logfd < 0) {
487 XLOG("Unable to open %s: %s\n", filename, strerror(errno));
488 return;
489 }
490
491 union {
492 unsigned char buf[LOGGER_ENTRY_MAX_LEN + 1];
493 struct logger_entry entry;
494 } log_entry;
495
496 while (true) {
497 ssize_t actual = read(logfd, log_entry.buf, LOGGER_ENTRY_MAX_LEN);
498 if (actual < 0) {
499 if (errno == EINTR) {
500 /* interrupted by signal, retry */
501 continue;
502 } else if (errno == EAGAIN) {
503 /* non-blocking EOF; we're done */
504 break;
505 } else {
Christopher Tate7716aef2013-04-02 14:00:27 -0700506 _LOG(log, 0, "Error while reading log: %s\n",
Jeff Brown053b8652012-06-06 16:25:03 -0700507 strerror(errno));
508 break;
509 }
510 } else if (actual == 0) {
Christopher Tate7716aef2013-04-02 14:00:27 -0700511 _LOG(log, 0, "Got zero bytes while reading log: %s\n",
Jeff Brown053b8652012-06-06 16:25:03 -0700512 strerror(errno));
513 break;
514 }
515
516 /*
517 * NOTE: if you XLOG something here, this will spin forever,
518 * because you will be writing as fast as you're reading. Any
519 * high-frequency debug diagnostics should just be written to
520 * the tombstone file.
521 */
522
523 struct logger_entry* entry = &log_entry.entry;
524
525 if (entry->pid != (int32_t) pid) {
526 /* wrong pid, ignore */
527 continue;
528 }
529
530 if (first) {
Christopher Tate7716aef2013-04-02 14:00:27 -0700531 _LOG(log, 0, "--------- %slog %s\n",
Jeff Brown053b8652012-06-06 16:25:03 -0700532 tailOnly ? "tail end of " : "", filename);
533 first = false;
534 }
535
536 /*
537 * Msg format is: <priority:1><tag:N>\0<message:N>\0
538 *
539 * We want to display it in the same format as "logcat -v threadtime"
540 * (although in this case the pid is redundant).
541 *
542 * TODO: scan for line breaks ('\n') and display each text line
543 * on a separate line, prefixed with the header, like logcat does.
544 */
545 static const char* kPrioChars = "!.VDIWEFS";
546 unsigned char prio = entry->msg[0];
547 char* tag = entry->msg + 1;
548 char* msg = tag + strlen(tag) + 1;
549
550 /* consume any trailing newlines */
551 char* eatnl = msg + strlen(msg) - 1;
552 while (eatnl >= msg && *eatnl == '\n') {
553 *eatnl-- = '\0';
554 }
555
556 char prioChar = (prio < strlen(kPrioChars) ? kPrioChars[prio] : '?');
557
558 char timeBuf[32];
559 time_t sec = (time_t) entry->sec;
560 struct tm tmBuf;
561 struct tm* ptm;
562 ptm = localtime_r(&sec, &tmBuf);
563 strftime(timeBuf, sizeof(timeBuf), "%m-%d %H:%M:%S", ptm);
564
565 if (tailOnly) {
566 snprintf(shortLog[shortLogNext], kShortLogLineLen,
567 "%s.%03d %5d %5d %c %-8s: %s",
568 timeBuf, entry->nsec / 1000000, entry->pid, entry->tid,
569 prioChar, tag, msg);
570 shortLogNext = (shortLogNext + 1) % kShortLogMaxLines;
571 shortLogCount++;
572 } else {
Christopher Tate7716aef2013-04-02 14:00:27 -0700573 _LOG(log, 0, "%s.%03d %5d %5d %c %-8s: %s\n",
Jeff Brown053b8652012-06-06 16:25:03 -0700574 timeBuf, entry->nsec / 1000000, entry->pid, entry->tid,
575 prioChar, tag, msg);
576 }
577 }
578
579 if (tailOnly) {
580 int i;
581
582 /*
583 * If we filled the buffer, we want to start at "next", which has
584 * the oldest entry. If we didn't, we want to start at zero.
585 */
586 if (shortLogCount < kShortLogMaxLines) {
587 shortLogNext = 0;
588 } else {
589 shortLogCount = kShortLogMaxLines; /* cap at window size */
590 }
591
592 for (i = 0; i < shortLogCount; i++) {
Christopher Tate7716aef2013-04-02 14:00:27 -0700593 _LOG(log, 0, "%s\n", shortLog[shortLogNext]);
Jeff Brown053b8652012-06-06 16:25:03 -0700594 shortLogNext = (shortLogNext + 1) % kShortLogMaxLines;
595 }
596 }
597
598 close(logfd);
599}
600
601/*
602 * Dumps the logs generated by the specified pid to the tombstone, from both
603 * "system" and "main" log devices. Ideally we'd interleave the output.
604 */
605static void dump_logs(log_t* log, pid_t pid, bool tailOnly)
606{
607 dump_log_file(log, pid, "/dev/log/system", tailOnly);
608 dump_log_file(log, pid, "/dev/log/main", tailOnly);
609}
610
Christopher Ferris17e91d42013-10-21 13:30:52 -0700611static void dump_abort_message(const backtrace_context_t* context, log_t* log, uintptr_t address) {
Elliott Hughese5f8a692013-04-04 13:52:01 -0700612 if (address == 0) {
613 return;
614 }
615
616 address += sizeof(size_t); // Skip the buffer length.
617
618 char msg[512];
619 memset(msg, 0, sizeof(msg));
620 char* p = &msg[0];
621 while (p < &msg[sizeof(msg)]) {
622 uint32_t data;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700623 if (!backtrace_read_word(context, address, &data)) {
Elliott Hughese5f8a692013-04-04 13:52:01 -0700624 break;
625 }
626 address += sizeof(uint32_t);
627
628 if ((*p++ = (data >> 0) & 0xff) == 0) {
629 break;
630 }
631 if ((*p++ = (data >> 8) & 0xff) == 0) {
632 break;
633 }
634 if ((*p++ = (data >> 16) & 0xff) == 0) {
635 break;
636 }
637 if ((*p++ = (data >> 24) & 0xff) == 0) {
638 break;
639 }
640 }
641 msg[sizeof(msg) - 1] = '\0';
642
Christopher Tate7716aef2013-04-02 14:00:27 -0700643 _LOG(log, SCOPE_AT_FAULT, "Abort message: '%s'\n", msg);
Elliott Hughese5f8a692013-04-04 13:52:01 -0700644}
645
Jeff Brown053b8652012-06-06 16:25:03 -0700646/*
647 * Dumps all information about the specified pid to the tombstone.
648 */
Elliott Hughese5f8a692013-04-04 13:52:01 -0700649static bool dump_crash(log_t* log, pid_t pid, pid_t tid, int signal, uintptr_t abort_msg_address,
650 bool dump_sibling_threads, int* total_sleep_time_usec)
Jeff Brown053b8652012-06-06 16:25:03 -0700651{
652 /* don't copy log messages to tombstone unless this is a dev device */
653 char value[PROPERTY_VALUE_MAX];
654 property_get("ro.debuggable", value, "0");
655 bool want_logs = (value[0] == '1');
656
Christopher Tateded2e5a2013-03-19 13:12:23 -0700657 if (log->amfd >= 0) {
658 /*
659 * Activity Manager protocol: binary 32-bit network-byte-order ints for the
660 * pid and signal number, followed by the raw text of the dump, culminating
661 * in a zero byte that marks end-of-data.
662 */
663 uint32_t datum = htonl(pid);
664 TEMP_FAILURE_RETRY( write(log->amfd, &datum, 4) );
665 datum = htonl(signal);
666 TEMP_FAILURE_RETRY( write(log->amfd, &datum, 4) );
667 }
668
Christopher Tate7716aef2013-04-02 14:00:27 -0700669 _LOG(log, SCOPE_AT_FAULT,
Jeff Brown053b8652012-06-06 16:25:03 -0700670 "*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***\n");
671 dump_build_info(log);
Ben Chengd7760c12012-09-19 16:04:01 -0700672 dump_revision_info(log);
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700673 dump_thread_info(log, pid, tid, SCOPE_AT_FAULT);
Elliott Hughese5f8a692013-04-04 13:52:01 -0700674 if (signal) {
Jeff Brown053b8652012-06-06 16:25:03 -0700675 dump_fault_addr(log, tid, signal);
676 }
677
Christopher Ferris17e91d42013-10-21 13:30:52 -0700678 backtrace_context_t context;
Christopher Ferris98464972014-01-06 19:16:33 -0800679 /* Gather the map info once for all this process' threads. */
680 backtrace_map_info_t* map_info = backtrace_create_map_info_list(pid);
681 if (backtrace_create_context_with_map(&context, pid, tid, 0, map_info)) {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700682 dump_abort_message(&context, log, abort_msg_address);
683 dump_thread(&context, log, SCOPE_AT_FAULT, total_sleep_time_usec);
684 backtrace_destroy_context(&context);
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700685 }
Jeff Brown053b8652012-06-06 16:25:03 -0700686
687 if (want_logs) {
688 dump_logs(log, pid, true);
689 }
690
691 bool detach_failed = false;
692 if (dump_sibling_threads) {
Christopher Ferris98464972014-01-06 19:16:33 -0800693 detach_failed = dump_sibling_thread_report(log, pid, tid, total_sleep_time_usec, map_info);
Jeff Brown053b8652012-06-06 16:25:03 -0700694 }
695
Christopher Ferris98464972014-01-06 19:16:33 -0800696 /* Destroy the previously created map info. */
697 backtrace_destroy_map_info_list(map_info);
698
Jeff Brown053b8652012-06-06 16:25:03 -0700699 if (want_logs) {
700 dump_logs(log, pid, false);
701 }
Christopher Tateded2e5a2013-03-19 13:12:23 -0700702
703 /* send EOD to the Activity Manager, then wait for its ack to avoid racing ahead
704 * and killing the target out from under it */
705 if (log->amfd >= 0) {
706 uint8_t eodMarker = 0;
707 TEMP_FAILURE_RETRY( write(log->amfd, &eodMarker, 1) );
708 /* 3 sec timeout reading the ack; we're fine if that happens */
709 TEMP_FAILURE_RETRY( read(log->amfd, &eodMarker, 1) );
710 }
711
Jeff Brown053b8652012-06-06 16:25:03 -0700712 return detach_failed;
713}
714
715/*
716 * find_and_open_tombstone - find an available tombstone slot, if any, of the
717 * form tombstone_XX where XX is 00 to MAX_TOMBSTONES-1, inclusive. If no
718 * file is available, we reuse the least-recently-modified file.
719 *
720 * Returns the path of the tombstone file, allocated using malloc(). Caller must free() it.
721 */
722static char* find_and_open_tombstone(int* fd)
723{
724 unsigned long mtime = ULONG_MAX;
725 struct stat sb;
726
727 /*
728 * XXX: Our stat.st_mtime isn't time_t. If it changes, as it probably ought
729 * to, our logic breaks. This check will generate a warning if that happens.
730 */
731 typecheck(mtime, sb.st_mtime);
732
733 /*
734 * In a single wolf-like pass, find an available slot and, in case none
735 * exist, find and record the least-recently-modified file.
736 */
737 char path[128];
738 int oldest = 0;
739 for (int i = 0; i < MAX_TOMBSTONES; i++) {
740 snprintf(path, sizeof(path), TOMBSTONE_DIR"/tombstone_%02d", i);
741
742 if (!stat(path, &sb)) {
743 if (sb.st_mtime < mtime) {
744 oldest = i;
745 mtime = sb.st_mtime;
746 }
747 continue;
748 }
749 if (errno != ENOENT)
750 continue;
751
752 *fd = open(path, O_CREAT | O_EXCL | O_WRONLY, 0600);
753 if (*fd < 0)
754 continue; /* raced ? */
755
756 fchown(*fd, AID_SYSTEM, AID_SYSTEM);
757 return strdup(path);
758 }
759
760 /* we didn't find an available file, so we clobber the oldest one */
761 snprintf(path, sizeof(path), TOMBSTONE_DIR"/tombstone_%02d", oldest);
762 *fd = open(path, O_CREAT | O_TRUNC | O_WRONLY, 0600);
763 if (*fd < 0) {
764 LOG("failed to open tombstone file '%s': %s\n", path, strerror(errno));
765 return NULL;
766 }
767 fchown(*fd, AID_SYSTEM, AID_SYSTEM);
768 return strdup(path);
769}
770
Christopher Tateded2e5a2013-03-19 13:12:23 -0700771static int activity_manager_connect() {
772 int amfd = socket(PF_UNIX, SOCK_STREAM, 0);
773 if (amfd >= 0) {
774 struct sockaddr_un address;
775 int err;
776
777 memset(&address, 0, sizeof(address));
778 address.sun_family = AF_UNIX;
779 strncpy(address.sun_path, NCRASH_SOCKET_PATH, sizeof(address.sun_path));
780 err = TEMP_FAILURE_RETRY( connect(amfd, (struct sockaddr*) &address, sizeof(address)) );
781 if (!err) {
782 struct timeval tv;
783 memset(&tv, 0, sizeof(tv));
784 tv.tv_sec = 1; // tight leash
785 err = setsockopt(amfd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
786 if (!err) {
787 tv.tv_sec = 3; // 3 seconds on handshake read
788 err = setsockopt(amfd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
789 }
790 }
791 if (err) {
792 close(amfd);
793 amfd = -1;
794 }
795 }
796
797 return amfd;
798}
799
Elliott Hughese5f8a692013-04-04 13:52:01 -0700800char* engrave_tombstone(pid_t pid, pid_t tid, int signal, uintptr_t abort_msg_address,
Jeff Brown053b8652012-06-06 16:25:03 -0700801 bool dump_sibling_threads, bool quiet, bool* detach_failed,
802 int* total_sleep_time_usec) {
803 mkdir(TOMBSTONE_DIR, 0755);
804 chown(TOMBSTONE_DIR, AID_SYSTEM, AID_SYSTEM);
805
rpcraigf1186f32012-07-19 09:38:06 -0400806 if (selinux_android_restorecon(TOMBSTONE_DIR) == -1) {
807 *detach_failed = false;
808 return NULL;
809 }
rpcraigf1186f32012-07-19 09:38:06 -0400810
Jeff Brown053b8652012-06-06 16:25:03 -0700811 int fd;
812 char* path = find_and_open_tombstone(&fd);
813 if (!path) {
814 *detach_failed = false;
815 return NULL;
816 }
817
818 log_t log;
819 log.tfd = fd;
Christopher Tateded2e5a2013-03-19 13:12:23 -0700820 log.amfd = activity_manager_connect();
Jeff Brown053b8652012-06-06 16:25:03 -0700821 log.quiet = quiet;
Elliott Hughese5f8a692013-04-04 13:52:01 -0700822 *detach_failed = dump_crash(&log, pid, tid, signal, abort_msg_address, dump_sibling_threads,
Jeff Brown053b8652012-06-06 16:25:03 -0700823 total_sleep_time_usec);
824
Christopher Tateded2e5a2013-03-19 13:12:23 -0700825 close(log.amfd);
Jeff Brown053b8652012-06-06 16:25:03 -0700826 close(fd);
827 return path;
828}