blob: 058a6b24edd00c96dc73df4913f7663f73008a66 [file] [log] [blame]
Jeff Brown053b8652012-06-06 16:25:03 -07001/*
Mark Salyzyna63f9272013-11-22 10:53:34 -08002 * Copyright (C) 2012-2013 The Android Open Source Project
Jeff Brown053b8652012-06-06 16:25:03 -07003 *
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
Mark Salyzyna63f9272013-11-22 10:53:34 -080032#include <log/log.h>
Colin Cross9227bd32013-07-23 16:59:20 -070033#include <log/logger.h>
Jeff Brown053b8652012-06-06 16:25:03 -070034#include <cutils/properties.h>
35
Christopher Ferris365e4ae2013-10-02 12:26:48 -070036#include <backtrace/backtrace.h>
Jeff Brown053b8652012-06-06 16:25:03 -070037
Christopher Tateded2e5a2013-03-19 13:12:23 -070038#include <sys/socket.h>
39#include <linux/un.h>
40
rpcraigf1186f32012-07-19 09:38:06 -040041#include <selinux/android.h>
rpcraigf1186f32012-07-19 09:38:06 -040042
Jeff Brown053b8652012-06-06 16:25:03 -070043#include "machine.h"
44#include "tombstone.h"
Christopher Ferris365e4ae2013-10-02 12:26:48 -070045#include "backtrace.h"
Jeff Brown053b8652012-06-06 16:25:03 -070046
Jeff Brown053b8652012-06-06 16:25:03 -070047#define STACK_WORDS 16
48
49#define MAX_TOMBSTONES 10
50#define TOMBSTONE_DIR "/data/tombstones"
51
Christopher Tateded2e5a2013-03-19 13:12:23 -070052/* Must match the path defined in NativeCrashListener.java */
53#define NCRASH_SOCKET_PATH "/data/system/ndebugsocket"
54
Jeff Brown053b8652012-06-06 16:25:03 -070055#define typecheck(x,y) { \
56 typeof(x) __dummy1; \
57 typeof(y) __dummy2; \
58 (void)(&__dummy1 == &__dummy2); }
59
60
61static bool signal_has_address(int sig) {
62 switch (sig) {
63 case SIGILL:
64 case SIGFPE:
65 case SIGSEGV:
66 case SIGBUS:
67 return true;
68 default:
69 return false;
70 }
71}
72
73static const char *get_signame(int sig)
74{
75 switch(sig) {
76 case SIGILL: return "SIGILL";
77 case SIGABRT: return "SIGABRT";
78 case SIGBUS: return "SIGBUS";
79 case SIGFPE: return "SIGFPE";
80 case SIGSEGV: return "SIGSEGV";
81 case SIGPIPE: return "SIGPIPE";
Chris Dearman231e3c82012-08-10 17:06:20 -070082#ifdef SIGSTKFLT
Jeff Brown053b8652012-06-06 16:25:03 -070083 case SIGSTKFLT: return "SIGSTKFLT";
Chris Dearman231e3c82012-08-10 17:06:20 -070084#endif
Jeff Brown053b8652012-06-06 16:25:03 -070085 case SIGSTOP: return "SIGSTOP";
86 default: return "?";
87 }
88}
89
90static const char *get_sigcode(int signo, int code)
91{
Elliott Hughes8f7d4432012-12-10 10:29:05 -080092 // Try the signal-specific codes...
Jeff Brown053b8652012-06-06 16:25:03 -070093 switch (signo) {
94 case SIGILL:
95 switch (code) {
96 case ILL_ILLOPC: return "ILL_ILLOPC";
97 case ILL_ILLOPN: return "ILL_ILLOPN";
98 case ILL_ILLADR: return "ILL_ILLADR";
99 case ILL_ILLTRP: return "ILL_ILLTRP";
100 case ILL_PRVOPC: return "ILL_PRVOPC";
101 case ILL_PRVREG: return "ILL_PRVREG";
102 case ILL_COPROC: return "ILL_COPROC";
103 case ILL_BADSTK: return "ILL_BADSTK";
104 }
105 break;
106 case SIGBUS:
107 switch (code) {
108 case BUS_ADRALN: return "BUS_ADRALN";
109 case BUS_ADRERR: return "BUS_ADRERR";
110 case BUS_OBJERR: return "BUS_OBJERR";
111 }
112 break;
113 case SIGFPE:
114 switch (code) {
115 case FPE_INTDIV: return "FPE_INTDIV";
116 case FPE_INTOVF: return "FPE_INTOVF";
117 case FPE_FLTDIV: return "FPE_FLTDIV";
118 case FPE_FLTOVF: return "FPE_FLTOVF";
119 case FPE_FLTUND: return "FPE_FLTUND";
120 case FPE_FLTRES: return "FPE_FLTRES";
121 case FPE_FLTINV: return "FPE_FLTINV";
122 case FPE_FLTSUB: return "FPE_FLTSUB";
123 }
124 break;
125 case SIGSEGV:
126 switch (code) {
127 case SEGV_MAPERR: return "SEGV_MAPERR";
128 case SEGV_ACCERR: return "SEGV_ACCERR";
129 }
130 break;
Elliott Hughes8f7d4432012-12-10 10:29:05 -0800131 case SIGTRAP:
132 switch (code) {
133 case TRAP_BRKPT: return "TRAP_BRKPT";
134 case TRAP_TRACE: return "TRAP_TRACE";
135 }
136 break;
Jeff Brown053b8652012-06-06 16:25:03 -0700137 }
Elliott Hughes8f7d4432012-12-10 10:29:05 -0800138 // Then the other codes...
139 switch (code) {
140 case SI_USER: return "SI_USER";
141#if defined(SI_KERNEL)
142 case SI_KERNEL: return "SI_KERNEL";
143#endif
144 case SI_QUEUE: return "SI_QUEUE";
145 case SI_TIMER: return "SI_TIMER";
146 case SI_MESGQ: return "SI_MESGQ";
147 case SI_ASYNCIO: return "SI_ASYNCIO";
148#if defined(SI_SIGIO)
149 case SI_SIGIO: return "SI_SIGIO";
150#endif
151#if defined(SI_TKILL)
152 case SI_TKILL: return "SI_TKILL";
153#endif
154 }
155 // Then give up...
Jeff Brown053b8652012-06-06 16:25:03 -0700156 return "?";
157}
158
Ben Chengd7760c12012-09-19 16:04:01 -0700159static void dump_revision_info(log_t* log)
160{
161 char revision[PROPERTY_VALUE_MAX];
162
163 property_get("ro.revision", revision, "unknown");
164
Christopher Tate7716aef2013-04-02 14:00:27 -0700165 _LOG(log, SCOPE_AT_FAULT, "Revision: '%s'\n", revision);
Ben Chengd7760c12012-09-19 16:04:01 -0700166}
167
Jeff Brown053b8652012-06-06 16:25:03 -0700168static void dump_build_info(log_t* log)
169{
170 char fingerprint[PROPERTY_VALUE_MAX];
171
172 property_get("ro.build.fingerprint", fingerprint, "unknown");
173
Christopher Tate7716aef2013-04-02 14:00:27 -0700174 _LOG(log, SCOPE_AT_FAULT, "Build fingerprint: '%s'\n", fingerprint);
Jeff Brown053b8652012-06-06 16:25:03 -0700175}
176
177static void dump_fault_addr(log_t* log, pid_t tid, int sig)
178{
179 siginfo_t si;
180
181 memset(&si, 0, sizeof(si));
Christopher Tate7716aef2013-04-02 14:00:27 -0700182 if(ptrace(PTRACE_GETSIGINFO, tid, 0, &si)){
183 _LOG(log, SCOPE_AT_FAULT, "cannot get siginfo: %s\n", strerror(errno));
Jeff Brown053b8652012-06-06 16:25:03 -0700184 } else if (signal_has_address(sig)) {
Christopher Tate7716aef2013-04-02 14:00:27 -0700185 _LOG(log, SCOPE_AT_FAULT, "signal %d (%s), code %d (%s), fault addr %08x\n",
Jeff Brown053b8652012-06-06 16:25:03 -0700186 sig, get_signame(sig),
187 si.si_code, get_sigcode(sig, si.si_code),
188 (uintptr_t) si.si_addr);
189 } else {
Christopher Tate7716aef2013-04-02 14:00:27 -0700190 _LOG(log, SCOPE_AT_FAULT, "signal %d (%s), code %d (%s), fault addr --------\n",
Jeff Brown053b8652012-06-06 16:25:03 -0700191 sig, get_signame(sig), si.si_code, get_sigcode(sig, si.si_code));
192 }
193}
194
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700195static void dump_thread_info(log_t* log, pid_t pid, pid_t tid, int scope_flags) {
Jeff Brown053b8652012-06-06 16:25:03 -0700196 char path[64];
197 char threadnamebuf[1024];
198 char* threadname = NULL;
199 FILE *fp;
200
201 snprintf(path, sizeof(path), "/proc/%d/comm", tid);
202 if ((fp = fopen(path, "r"))) {
203 threadname = fgets(threadnamebuf, sizeof(threadnamebuf), fp);
204 fclose(fp);
205 if (threadname) {
206 size_t len = strlen(threadname);
207 if (len && threadname[len - 1] == '\n') {
208 threadname[len - 1] = '\0';
209 }
210 }
211 }
212
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700213 if (IS_AT_FAULT(scope_flags)) {
Jeff Brown053b8652012-06-06 16:25:03 -0700214 char procnamebuf[1024];
215 char* procname = NULL;
216
217 snprintf(path, sizeof(path), "/proc/%d/cmdline", pid);
218 if ((fp = fopen(path, "r"))) {
219 procname = fgets(procnamebuf, sizeof(procnamebuf), fp);
220 fclose(fp);
221 }
222
Christopher Tate7716aef2013-04-02 14:00:27 -0700223 _LOG(log, SCOPE_AT_FAULT, "pid: %d, tid: %d, name: %s >>> %s <<<\n", pid, tid,
Jeff Brown053b8652012-06-06 16:25:03 -0700224 threadname ? threadname : "UNKNOWN",
225 procname ? procname : "UNKNOWN");
226 } else {
Christopher Tate7716aef2013-04-02 14:00:27 -0700227 _LOG(log, 0, "pid: %d, tid: %d, name: %s\n",
228 pid, tid, threadname ? threadname : "UNKNOWN");
Jeff Brown053b8652012-06-06 16:25:03 -0700229 }
230}
231
Christopher Ferris17e91d42013-10-21 13:30:52 -0700232static void dump_stack_segment(const backtrace_context_t* context, log_t* log,
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700233 int scope_flags, uintptr_t *sp, size_t words, int label) {
Jeff Brown053b8652012-06-06 16:25:03 -0700234 for (size_t i = 0; i < words; i++) {
235 uint32_t stack_content;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700236 if (!backtrace_read_word(context, *sp, &stack_content)) {
Jeff Brown053b8652012-06-06 16:25:03 -0700237 break;
238 }
239
Christopher Ferris17e91d42013-10-21 13:30:52 -0700240 const char* map_name = backtrace_get_map_name(context, stack_content, NULL);
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700241 if (!map_name) {
242 map_name = "";
243 }
244 uintptr_t offset = 0;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700245 char* func_name = backtrace_get_func_name(context, stack_content, &offset);
246 if (func_name) {
Jeff Brown053b8652012-06-06 16:25:03 -0700247 if (!i && label >= 0) {
248 if (offset) {
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700249 _LOG(log, scope_flags, " #%02d %08x %08x %s (%s+%u)\n",
Christopher Ferris17e91d42013-10-21 13:30:52 -0700250 label, *sp, stack_content, map_name, func_name, offset);
Jeff Brown053b8652012-06-06 16:25:03 -0700251 } else {
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700252 _LOG(log, scope_flags, " #%02d %08x %08x %s (%s)\n",
Christopher Ferris17e91d42013-10-21 13:30:52 -0700253 label, *sp, stack_content, map_name, func_name);
Jeff Brown053b8652012-06-06 16:25:03 -0700254 }
255 } else {
256 if (offset) {
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700257 _LOG(log, scope_flags, " %08x %08x %s (%s+%u)\n",
Christopher Ferris17e91d42013-10-21 13:30:52 -0700258 *sp, stack_content, map_name, func_name, offset);
Jeff Brown053b8652012-06-06 16:25:03 -0700259 } else {
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700260 _LOG(log, scope_flags, " %08x %08x %s (%s)\n",
Christopher Ferris17e91d42013-10-21 13:30:52 -0700261 *sp, stack_content, map_name, func_name);
Jeff Brown053b8652012-06-06 16:25:03 -0700262 }
263 }
Christopher Ferris17e91d42013-10-21 13:30:52 -0700264 free(func_name);
Jeff Brown053b8652012-06-06 16:25:03 -0700265 } else {
266 if (!i && label >= 0) {
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700267 _LOG(log, scope_flags, " #%02d %08x %08x %s\n",
268 label, *sp, stack_content, map_name);
Jeff Brown053b8652012-06-06 16:25:03 -0700269 } else {
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700270 _LOG(log, scope_flags, " %08x %08x %s\n",
271 *sp, stack_content, map_name);
Jeff Brown053b8652012-06-06 16:25:03 -0700272 }
273 }
274
275 *sp += sizeof(uint32_t);
276 }
277}
278
Christopher Ferris17e91d42013-10-21 13:30:52 -0700279static void dump_stack(const backtrace_context_t* context, log_t* log, int scope_flags) {
280 const backtrace_t* backtrace = context->backtrace;
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700281 size_t first = 0, last;
282 for (size_t i = 0; i < backtrace->num_frames; i++) {
283 if (backtrace->frames[i].sp) {
284 if (!first) {
285 first = i+1;
Jeff Brown053b8652012-06-06 16:25:03 -0700286 }
287 last = i;
288 }
289 }
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700290 if (!first) {
Jeff Brown053b8652012-06-06 16:25:03 -0700291 return;
292 }
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700293 first--;
Jeff Brown053b8652012-06-06 16:25:03 -0700294
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700295 scope_flags |= SCOPE_SENSITIVE;
Jeff Brown053b8652012-06-06 16:25:03 -0700296
297 // Dump a few words before the first frame.
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700298 uintptr_t sp = backtrace->frames[first].sp - STACK_WORDS * sizeof(uint32_t);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700299 dump_stack_segment(context, log, scope_flags, &sp, STACK_WORDS, -1);
Jeff Brown053b8652012-06-06 16:25:03 -0700300
301 // Dump a few words from all successive frames.
302 // Only log the first 3 frames, put the rest in the tombstone.
303 for (size_t i = first; i <= last; i++) {
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700304 const backtrace_frame_data_t* frame = &backtrace->frames[i];
305 if (sp != frame->sp) {
306 _LOG(log, scope_flags, " ........ ........\n");
307 sp = frame->sp;
Jeff Brown053b8652012-06-06 16:25:03 -0700308 }
309 if (i - first == 3) {
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700310 scope_flags &= (~SCOPE_AT_FAULT);
Jeff Brown053b8652012-06-06 16:25:03 -0700311 }
312 if (i == last) {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700313 dump_stack_segment(context, log, scope_flags, &sp, STACK_WORDS, i);
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700314 if (sp < frame->sp + frame->stack_size) {
315 _LOG(log, scope_flags, " ........ ........\n");
Jeff Brown053b8652012-06-06 16:25:03 -0700316 }
317 } else {
318 size_t words = frame->stack_size / sizeof(uint32_t);
319 if (words == 0) {
320 words = 1;
321 } else if (words > STACK_WORDS) {
322 words = STACK_WORDS;
323 }
Christopher Ferris17e91d42013-10-21 13:30:52 -0700324 dump_stack_segment(context, log, scope_flags, &sp, words, i);
Jeff Brown053b8652012-06-06 16:25:03 -0700325 }
326 }
327}
328
Christopher Ferris17e91d42013-10-21 13:30:52 -0700329static void dump_backtrace_and_stack(const backtrace_context_t* context,
330 log_t* log, int scope_flags) {
331 if (context->backtrace->num_frames) {
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700332 _LOG(log, scope_flags, "\nbacktrace:\n");
Christopher Ferris17e91d42013-10-21 13:30:52 -0700333 dump_backtrace_to_log(context, log, scope_flags, " ");
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700334
335 _LOG(log, scope_flags, "\nstack:\n");
Christopher Ferris17e91d42013-10-21 13:30:52 -0700336 dump_stack(context, log, scope_flags);
Jeff Brown053b8652012-06-06 16:25:03 -0700337 }
338}
339
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700340static 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 -0800341 if (m != NULL) {
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700342 _LOG(log, scope_flags, " %08x-%08x %c%c%c %s\n", m->start, m->end,
Elliott Hughesd1420be2013-01-03 13:39:57 -0800343 m->is_readable ? 'r' : '-',
344 m->is_writable ? 'w' : '-',
345 m->is_executable ? 'x' : '-',
346 m->name);
347 } else {
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700348 _LOG(log, scope_flags, " (no %s)\n", what);
Elliott Hughesd1420be2013-01-03 13:39:57 -0800349 }
350}
351
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700352static void dump_nearby_maps(const backtrace_map_info_t* map_info_list, log_t* log, pid_t tid, int scope_flags) {
353 scope_flags |= SCOPE_SENSITIVE;
Jeff Brown053b8652012-06-06 16:25:03 -0700354 siginfo_t si;
355 memset(&si, 0, sizeof(si));
356 if (ptrace(PTRACE_GETSIGINFO, tid, 0, &si)) {
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700357 _LOG(log, scope_flags, "cannot get siginfo for %d: %s\n",
Jeff Brown053b8652012-06-06 16:25:03 -0700358 tid, strerror(errno));
359 return;
360 }
361 if (!signal_has_address(si.si_signo)) {
362 return;
363 }
364
365 uintptr_t addr = (uintptr_t) si.si_addr;
366 addr &= ~0xfff; /* round to 4K page boundary */
367 if (addr == 0) { /* null-pointer deref */
368 return;
369 }
370
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700371 _LOG(log, scope_flags, "\nmemory map around fault addr %08x:\n", (int)si.si_addr);
Jeff Brown053b8652012-06-06 16:25:03 -0700372
373 /*
374 * Search for a match, or for a hole where the match would be. The list
375 * is backward from the file content, so it starts at high addresses.
376 */
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700377 const backtrace_map_info_t* map = map_info_list;
378 const backtrace_map_info_t* next = NULL;
379 const backtrace_map_info_t* prev = NULL;
Jeff Brown053b8652012-06-06 16:25:03 -0700380 while (map != NULL) {
381 if (addr >= map->start && addr < map->end) {
Jeff Brown053b8652012-06-06 16:25:03 -0700382 next = map->next;
383 break;
384 } else if (addr >= map->end) {
385 /* map would be between "prev" and this entry */
386 next = map;
387 map = NULL;
388 break;
389 }
390
391 prev = map;
392 map = map->next;
393 }
394
395 /*
396 * Show "next" then "match" then "prev" so that the addresses appear in
397 * ascending order (like /proc/pid/maps).
398 */
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700399 dump_map(log, next, "map below", scope_flags);
400 dump_map(log, map, "map for address", scope_flags);
401 dump_map(log, prev, "map above", scope_flags);
Jeff Brown053b8652012-06-06 16:25:03 -0700402}
403
Christopher Ferris17e91d42013-10-21 13:30:52 -0700404static void dump_thread(const backtrace_context_t* context, log_t* log,
405 int scope_flags, int* total_sleep_time_usec) {
406 const backtrace_t* backtrace = context->backtrace;
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700407 wait_for_stop(backtrace->tid, total_sleep_time_usec);
Jeff Brown053b8652012-06-06 16:25:03 -0700408
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700409 dump_registers(log, backtrace->tid, scope_flags);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700410 dump_backtrace_and_stack(context, log, scope_flags);
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700411 if (IS_AT_FAULT(scope_flags)) {
412 dump_memory_and_code(log, backtrace->tid, scope_flags);
413 dump_nearby_maps(backtrace->map_info_list, log, backtrace->tid, scope_flags);
Jeff Brown053b8652012-06-06 16:25:03 -0700414 }
415}
416
417/* Return true if some thread is not detached cleanly */
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700418static bool dump_sibling_thread_report(
Christopher Ferris98464972014-01-06 19:16:33 -0800419 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 -0700420 char task_path[64];
421 snprintf(task_path, sizeof(task_path), "/proc/%d/task", pid);
422
423 DIR* d = opendir(task_path);
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700424 /* Bail early if the task directory cannot be opened */
Jeff Brown053b8652012-06-06 16:25:03 -0700425 if (d == NULL) {
426 XLOG("Cannot open /proc/%d/task\n", pid);
427 return false;
428 }
429
430 bool detach_failed = false;
Elliott Hughesc463d2c2012-10-26 16:47:09 -0700431 struct dirent* de;
432 while ((de = readdir(d)) != NULL) {
Jeff Brown053b8652012-06-06 16:25:03 -0700433 /* Ignore "." and ".." */
434 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, "..")) {
435 continue;
436 }
437
438 /* The main thread at fault has been handled individually */
439 char* end;
440 pid_t new_tid = strtoul(de->d_name, &end, 10);
441 if (*end || new_tid == tid) {
442 continue;
443 }
444
445 /* Skip this thread if cannot ptrace it */
446 if (ptrace(PTRACE_ATTACH, new_tid, 0, 0) < 0) {
447 continue;
448 }
449
Christopher Tate7716aef2013-04-02 14:00:27 -0700450 _LOG(log, 0, "--- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---\n");
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700451 dump_thread_info(log, pid, new_tid, 0);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700452 backtrace_context_t new_context;
Christopher Ferris98464972014-01-06 19:16:33 -0800453 if (backtrace_create_context_with_map(&new_context, pid, new_tid, 0, map_info)) {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700454 dump_thread(&new_context, log, 0, total_sleep_time_usec);
455 backtrace_destroy_context(&new_context);
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700456 }
Jeff Brown053b8652012-06-06 16:25:03 -0700457
458 if (ptrace(PTRACE_DETACH, new_tid, 0, 0) != 0) {
459 LOG("ptrace detach from %d failed: %s\n", new_tid, strerror(errno));
460 detach_failed = true;
461 }
462 }
463
464 closedir(d);
465 return detach_failed;
466}
467
468/*
469 * Reads the contents of the specified log device, filters out the entries
470 * that don't match the specified pid, and writes them to the tombstone file.
471 *
Mark Salyzyna63f9272013-11-22 10:53:34 -0800472 * If "tail" is set, we only print the last few lines.
Jeff Brown053b8652012-06-06 16:25:03 -0700473 */
474static void dump_log_file(log_t* log, pid_t pid, const char* filename,
Mark Salyzyna63f9272013-11-22 10:53:34 -0800475 unsigned int tail)
Jeff Brown053b8652012-06-06 16:25:03 -0700476{
477 bool first = true;
Mark Salyzyna63f9272013-11-22 10:53:34 -0800478 struct logger_list *logger_list;
Jeff Brown053b8652012-06-06 16:25:03 -0700479
Mark Salyzyna63f9272013-11-22 10:53:34 -0800480 logger_list = android_logger_list_open(
481 android_name_to_log_id(filename), O_RDONLY | O_NONBLOCK, tail, pid);
Jeff Brown053b8652012-06-06 16:25:03 -0700482
Mark Salyzyna63f9272013-11-22 10:53:34 -0800483 if (!logger_list) {
Jeff Brown053b8652012-06-06 16:25:03 -0700484 XLOG("Unable to open %s: %s\n", filename, strerror(errno));
485 return;
486 }
487
Mark Salyzyna63f9272013-11-22 10:53:34 -0800488 struct log_msg log_entry;
Jeff Brown053b8652012-06-06 16:25:03 -0700489
490 while (true) {
Mark Salyzyna63f9272013-11-22 10:53:34 -0800491 ssize_t actual = android_logger_list_read(logger_list, &log_entry);
492 struct logger_entry* entry;
493
Jeff Brown053b8652012-06-06 16:25:03 -0700494 if (actual < 0) {
Mark Salyzyna63f9272013-11-22 10:53:34 -0800495 if (actual == -EINTR) {
Jeff Brown053b8652012-06-06 16:25:03 -0700496 /* interrupted by signal, retry */
497 continue;
Mark Salyzyna63f9272013-11-22 10:53:34 -0800498 } else if (actual == -EAGAIN) {
Jeff Brown053b8652012-06-06 16:25:03 -0700499 /* non-blocking EOF; we're done */
500 break;
501 } else {
Christopher Tate7716aef2013-04-02 14:00:27 -0700502 _LOG(log, 0, "Error while reading log: %s\n",
Mark Salyzyna63f9272013-11-22 10:53:34 -0800503 strerror(-actual));
Jeff Brown053b8652012-06-06 16:25:03 -0700504 break;
505 }
506 } else if (actual == 0) {
Christopher Tate7716aef2013-04-02 14:00:27 -0700507 _LOG(log, 0, "Got zero bytes while reading log: %s\n",
Jeff Brown053b8652012-06-06 16:25:03 -0700508 strerror(errno));
509 break;
510 }
511
512 /*
513 * NOTE: if you XLOG something here, this will spin forever,
514 * because you will be writing as fast as you're reading. Any
515 * high-frequency debug diagnostics should just be written to
516 * the tombstone file.
517 */
518
Mark Salyzyna63f9272013-11-22 10:53:34 -0800519 entry = &log_entry.entry_v1;
Jeff Brown053b8652012-06-06 16:25:03 -0700520
521 if (first) {
Christopher Tate7716aef2013-04-02 14:00:27 -0700522 _LOG(log, 0, "--------- %slog %s\n",
Mark Salyzyna63f9272013-11-22 10:53:34 -0800523 tail ? "tail end of " : "", filename);
Jeff Brown053b8652012-06-06 16:25:03 -0700524 first = false;
525 }
526
527 /*
528 * Msg format is: <priority:1><tag:N>\0<message:N>\0
529 *
530 * We want to display it in the same format as "logcat -v threadtime"
531 * (although in this case the pid is redundant).
532 *
533 * TODO: scan for line breaks ('\n') and display each text line
534 * on a separate line, prefixed with the header, like logcat does.
535 */
536 static const char* kPrioChars = "!.VDIWEFS";
Mark Salyzyna63f9272013-11-22 10:53:34 -0800537 unsigned hdr_size = log_entry.entry.hdr_size;
538 if (!hdr_size) {
539 hdr_size = sizeof(log_entry.entry_v1);
540 }
541 char* msg = (char *)log_entry.buf + hdr_size;
542 unsigned char prio = msg[0];
543 char* tag = msg + 1;
544 msg = tag + strlen(tag) + 1;
Jeff Brown053b8652012-06-06 16:25:03 -0700545
546 /* consume any trailing newlines */
547 char* eatnl = msg + strlen(msg) - 1;
548 while (eatnl >= msg && *eatnl == '\n') {
549 *eatnl-- = '\0';
550 }
551
552 char prioChar = (prio < strlen(kPrioChars) ? kPrioChars[prio] : '?');
553
554 char timeBuf[32];
555 time_t sec = (time_t) entry->sec;
556 struct tm tmBuf;
557 struct tm* ptm;
558 ptm = localtime_r(&sec, &tmBuf);
559 strftime(timeBuf, sizeof(timeBuf), "%m-%d %H:%M:%S", ptm);
560
Mark Salyzyna63f9272013-11-22 10:53:34 -0800561 _LOG(log, 0, "%s.%03d %5d %5d %c %-8s: %s\n",
562 timeBuf, entry->nsec / 1000000, entry->pid, entry->tid,
563 prioChar, tag, msg);
Jeff Brown053b8652012-06-06 16:25:03 -0700564 }
565
Mark Salyzyna63f9272013-11-22 10:53:34 -0800566 android_logger_list_free(logger_list);
Jeff Brown053b8652012-06-06 16:25:03 -0700567}
568
569/*
570 * Dumps the logs generated by the specified pid to the tombstone, from both
571 * "system" and "main" log devices. Ideally we'd interleave the output.
572 */
Mark Salyzyna63f9272013-11-22 10:53:34 -0800573static void dump_logs(log_t* log, pid_t pid, unsigned tail)
Jeff Brown053b8652012-06-06 16:25:03 -0700574{
Mark Salyzyna63f9272013-11-22 10:53:34 -0800575 dump_log_file(log, pid, "system", tail);
576 dump_log_file(log, pid, "main", tail);
Jeff Brown053b8652012-06-06 16:25:03 -0700577}
578
Christopher Ferris17e91d42013-10-21 13:30:52 -0700579static void dump_abort_message(const backtrace_context_t* context, log_t* log, uintptr_t address) {
Elliott Hughese5f8a692013-04-04 13:52:01 -0700580 if (address == 0) {
581 return;
582 }
583
584 address += sizeof(size_t); // Skip the buffer length.
585
586 char msg[512];
587 memset(msg, 0, sizeof(msg));
588 char* p = &msg[0];
589 while (p < &msg[sizeof(msg)]) {
590 uint32_t data;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700591 if (!backtrace_read_word(context, address, &data)) {
Elliott Hughese5f8a692013-04-04 13:52:01 -0700592 break;
593 }
594 address += sizeof(uint32_t);
595
596 if ((*p++ = (data >> 0) & 0xff) == 0) {
597 break;
598 }
599 if ((*p++ = (data >> 8) & 0xff) == 0) {
600 break;
601 }
602 if ((*p++ = (data >> 16) & 0xff) == 0) {
603 break;
604 }
605 if ((*p++ = (data >> 24) & 0xff) == 0) {
606 break;
607 }
608 }
609 msg[sizeof(msg) - 1] = '\0';
610
Christopher Tate7716aef2013-04-02 14:00:27 -0700611 _LOG(log, SCOPE_AT_FAULT, "Abort message: '%s'\n", msg);
Elliott Hughese5f8a692013-04-04 13:52:01 -0700612}
613
Jeff Brown053b8652012-06-06 16:25:03 -0700614/*
615 * Dumps all information about the specified pid to the tombstone.
616 */
Elliott Hughese5f8a692013-04-04 13:52:01 -0700617static bool dump_crash(log_t* log, pid_t pid, pid_t tid, int signal, uintptr_t abort_msg_address,
618 bool dump_sibling_threads, int* total_sleep_time_usec)
Jeff Brown053b8652012-06-06 16:25:03 -0700619{
620 /* don't copy log messages to tombstone unless this is a dev device */
621 char value[PROPERTY_VALUE_MAX];
622 property_get("ro.debuggable", value, "0");
623 bool want_logs = (value[0] == '1');
624
Christopher Tateded2e5a2013-03-19 13:12:23 -0700625 if (log->amfd >= 0) {
626 /*
627 * Activity Manager protocol: binary 32-bit network-byte-order ints for the
628 * pid and signal number, followed by the raw text of the dump, culminating
629 * in a zero byte that marks end-of-data.
630 */
631 uint32_t datum = htonl(pid);
632 TEMP_FAILURE_RETRY( write(log->amfd, &datum, 4) );
633 datum = htonl(signal);
634 TEMP_FAILURE_RETRY( write(log->amfd, &datum, 4) );
635 }
636
Christopher Tate7716aef2013-04-02 14:00:27 -0700637 _LOG(log, SCOPE_AT_FAULT,
Jeff Brown053b8652012-06-06 16:25:03 -0700638 "*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***\n");
639 dump_build_info(log);
Ben Chengd7760c12012-09-19 16:04:01 -0700640 dump_revision_info(log);
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700641 dump_thread_info(log, pid, tid, SCOPE_AT_FAULT);
Elliott Hughese5f8a692013-04-04 13:52:01 -0700642 if (signal) {
Jeff Brown053b8652012-06-06 16:25:03 -0700643 dump_fault_addr(log, tid, signal);
644 }
645
Christopher Ferris17e91d42013-10-21 13:30:52 -0700646 backtrace_context_t context;
Christopher Ferris98464972014-01-06 19:16:33 -0800647 /* Gather the map info once for all this process' threads. */
648 backtrace_map_info_t* map_info = backtrace_create_map_info_list(pid);
649 if (backtrace_create_context_with_map(&context, pid, tid, 0, map_info)) {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700650 dump_abort_message(&context, log, abort_msg_address);
651 dump_thread(&context, log, SCOPE_AT_FAULT, total_sleep_time_usec);
652 backtrace_destroy_context(&context);
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700653 }
Jeff Brown053b8652012-06-06 16:25:03 -0700654
655 if (want_logs) {
Mark Salyzyna63f9272013-11-22 10:53:34 -0800656 dump_logs(log, pid, 5);
Jeff Brown053b8652012-06-06 16:25:03 -0700657 }
658
659 bool detach_failed = false;
660 if (dump_sibling_threads) {
Christopher Ferris98464972014-01-06 19:16:33 -0800661 detach_failed = dump_sibling_thread_report(log, pid, tid, total_sleep_time_usec, map_info);
Jeff Brown053b8652012-06-06 16:25:03 -0700662 }
663
Christopher Ferris98464972014-01-06 19:16:33 -0800664 /* Destroy the previously created map info. */
665 backtrace_destroy_map_info_list(map_info);
666
Jeff Brown053b8652012-06-06 16:25:03 -0700667 if (want_logs) {
Mark Salyzyna63f9272013-11-22 10:53:34 -0800668 dump_logs(log, pid, 0);
Jeff Brown053b8652012-06-06 16:25:03 -0700669 }
Christopher Tateded2e5a2013-03-19 13:12:23 -0700670
671 /* send EOD to the Activity Manager, then wait for its ack to avoid racing ahead
672 * and killing the target out from under it */
673 if (log->amfd >= 0) {
674 uint8_t eodMarker = 0;
675 TEMP_FAILURE_RETRY( write(log->amfd, &eodMarker, 1) );
676 /* 3 sec timeout reading the ack; we're fine if that happens */
677 TEMP_FAILURE_RETRY( read(log->amfd, &eodMarker, 1) );
678 }
679
Jeff Brown053b8652012-06-06 16:25:03 -0700680 return detach_failed;
681}
682
683/*
684 * find_and_open_tombstone - find an available tombstone slot, if any, of the
685 * form tombstone_XX where XX is 00 to MAX_TOMBSTONES-1, inclusive. If no
686 * file is available, we reuse the least-recently-modified file.
687 *
688 * Returns the path of the tombstone file, allocated using malloc(). Caller must free() it.
689 */
690static char* find_and_open_tombstone(int* fd)
691{
692 unsigned long mtime = ULONG_MAX;
693 struct stat sb;
694
695 /*
696 * XXX: Our stat.st_mtime isn't time_t. If it changes, as it probably ought
697 * to, our logic breaks. This check will generate a warning if that happens.
698 */
699 typecheck(mtime, sb.st_mtime);
700
701 /*
702 * In a single wolf-like pass, find an available slot and, in case none
703 * exist, find and record the least-recently-modified file.
704 */
705 char path[128];
706 int oldest = 0;
707 for (int i = 0; i < MAX_TOMBSTONES; i++) {
708 snprintf(path, sizeof(path), TOMBSTONE_DIR"/tombstone_%02d", i);
709
710 if (!stat(path, &sb)) {
711 if (sb.st_mtime < mtime) {
712 oldest = i;
713 mtime = sb.st_mtime;
714 }
715 continue;
716 }
717 if (errno != ENOENT)
718 continue;
719
720 *fd = open(path, O_CREAT | O_EXCL | O_WRONLY, 0600);
721 if (*fd < 0)
722 continue; /* raced ? */
723
724 fchown(*fd, AID_SYSTEM, AID_SYSTEM);
725 return strdup(path);
726 }
727
728 /* we didn't find an available file, so we clobber the oldest one */
729 snprintf(path, sizeof(path), TOMBSTONE_DIR"/tombstone_%02d", oldest);
730 *fd = open(path, O_CREAT | O_TRUNC | O_WRONLY, 0600);
731 if (*fd < 0) {
732 LOG("failed to open tombstone file '%s': %s\n", path, strerror(errno));
733 return NULL;
734 }
735 fchown(*fd, AID_SYSTEM, AID_SYSTEM);
736 return strdup(path);
737}
738
Christopher Tateded2e5a2013-03-19 13:12:23 -0700739static int activity_manager_connect() {
740 int amfd = socket(PF_UNIX, SOCK_STREAM, 0);
741 if (amfd >= 0) {
742 struct sockaddr_un address;
743 int err;
744
745 memset(&address, 0, sizeof(address));
746 address.sun_family = AF_UNIX;
747 strncpy(address.sun_path, NCRASH_SOCKET_PATH, sizeof(address.sun_path));
748 err = TEMP_FAILURE_RETRY( connect(amfd, (struct sockaddr*) &address, sizeof(address)) );
749 if (!err) {
750 struct timeval tv;
751 memset(&tv, 0, sizeof(tv));
752 tv.tv_sec = 1; // tight leash
753 err = setsockopt(amfd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
754 if (!err) {
755 tv.tv_sec = 3; // 3 seconds on handshake read
756 err = setsockopt(amfd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
757 }
758 }
759 if (err) {
760 close(amfd);
761 amfd = -1;
762 }
763 }
764
765 return amfd;
766}
767
Elliott Hughese5f8a692013-04-04 13:52:01 -0700768char* engrave_tombstone(pid_t pid, pid_t tid, int signal, uintptr_t abort_msg_address,
Jeff Brown053b8652012-06-06 16:25:03 -0700769 bool dump_sibling_threads, bool quiet, bool* detach_failed,
770 int* total_sleep_time_usec) {
771 mkdir(TOMBSTONE_DIR, 0755);
772 chown(TOMBSTONE_DIR, AID_SYSTEM, AID_SYSTEM);
773
rpcraigf1186f32012-07-19 09:38:06 -0400774 if (selinux_android_restorecon(TOMBSTONE_DIR) == -1) {
775 *detach_failed = false;
776 return NULL;
777 }
rpcraigf1186f32012-07-19 09:38:06 -0400778
Jeff Brown053b8652012-06-06 16:25:03 -0700779 int fd;
780 char* path = find_and_open_tombstone(&fd);
781 if (!path) {
782 *detach_failed = false;
783 return NULL;
784 }
785
786 log_t log;
787 log.tfd = fd;
Christopher Tateded2e5a2013-03-19 13:12:23 -0700788 log.amfd = activity_manager_connect();
Jeff Brown053b8652012-06-06 16:25:03 -0700789 log.quiet = quiet;
Elliott Hughese5f8a692013-04-04 13:52:01 -0700790 *detach_failed = dump_crash(&log, pid, tid, signal, abort_msg_address, dump_sibling_threads,
Jeff Brown053b8652012-06-06 16:25:03 -0700791 total_sleep_time_usec);
792
Christopher Tateded2e5a2013-03-19 13:12:23 -0700793 close(log.amfd);
Jeff Brown053b8652012-06-06 16:25:03 -0700794 close(fd);
795 return path;
796}