blob: 9c5d8c0aa5a3161edc8ce8a3acd10d1af651a603 [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>
Jeff Brown053b8652012-06-06 16:25:03 -070018#include <stdlib.h>
19#include <signal.h>
20#include <string.h>
21#include <stdio.h>
22#include <fcntl.h>
23#include <errno.h>
24#include <dirent.h>
25#include <time.h>
26#include <sys/ptrace.h>
27#include <sys/stat.h>
Christopher Ferris20303f82014-01-10 16:33:16 -080028#include <inttypes.h>
Jeff Brown053b8652012-06-06 16:25:03 -070029
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 Ferris20303f82014-01-10 16:33:16 -080035#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
Christopher Ferris20303f82014-01-10 16:33:16 -080042#include <UniquePtr.h>
43
Jeff Brown053b8652012-06-06 16:25:03 -070044#include "machine.h"
45#include "tombstone.h"
Christopher Ferris365e4ae2013-10-02 12:26:48 -070046#include "backtrace.h"
Jeff Brown053b8652012-06-06 16:25:03 -070047
Jeff Brown053b8652012-06-06 16:25:03 -070048#define STACK_WORDS 16
49
50#define MAX_TOMBSTONES 10
51#define TOMBSTONE_DIR "/data/tombstones"
52
Christopher Ferris20303f82014-01-10 16:33:16 -080053// Must match the path defined in NativeCrashListener.java
Christopher Tateded2e5a2013-03-19 13:12:23 -070054#define NCRASH_SOCKET_PATH "/data/system/ndebugsocket"
55
Jeff Brown053b8652012-06-06 16:25:03 -070056#define typecheck(x,y) { \
Christopher Ferris20303f82014-01-10 16:33:16 -080057 typeof(x) __dummy1; \
58 typeof(y) __dummy2; \
59 (void)(&__dummy1 == &__dummy2); }
Jeff Brown053b8652012-06-06 16:25:03 -070060
61
62static bool signal_has_address(int sig) {
Christopher Ferris20303f82014-01-10 16:33:16 -080063 switch (sig) {
Jeff Brown053b8652012-06-06 16:25:03 -070064 case SIGILL:
Christopher Ferris20303f82014-01-10 16:33:16 -080065 case SIGFPE:
66 case SIGSEGV:
67 case SIGBUS:
68 return true;
69 default:
70 return false;
71 }
72}
73
74static const char* get_signame(int sig) {
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";
82#ifdef SIGSTKFLT
83 case SIGSTKFLT: return "SIGSTKFLT";
84#endif
85 case SIGSTOP: return "SIGSTOP";
86 default: return "?";
87 }
88}
89
90static const char* get_sigcode(int signo, int code) {
91 // Try the signal-specific codes...
92 switch (signo) {
93 case SIGILL:
94 switch (code) {
Jeff Brown053b8652012-06-06 16:25:03 -070095 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";
Christopher Ferris20303f82014-01-10 16:33:16 -0800103 }
104 break;
Jeff Brown053b8652012-06-06 16:25:03 -0700105 case SIGBUS:
Christopher Ferris20303f82014-01-10 16:33:16 -0800106 switch (code) {
Jeff Brown053b8652012-06-06 16:25:03 -0700107 case BUS_ADRALN: return "BUS_ADRALN";
108 case BUS_ADRERR: return "BUS_ADRERR";
109 case BUS_OBJERR: return "BUS_OBJERR";
Christopher Ferris20303f82014-01-10 16:33:16 -0800110 }
111 break;
Jeff Brown053b8652012-06-06 16:25:03 -0700112 case SIGFPE:
Christopher Ferris20303f82014-01-10 16:33:16 -0800113 switch (code) {
Jeff Brown053b8652012-06-06 16:25:03 -0700114 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";
Christopher Ferris20303f82014-01-10 16:33:16 -0800122 }
123 break;
Jeff Brown053b8652012-06-06 16:25:03 -0700124 case SIGSEGV:
Christopher Ferris20303f82014-01-10 16:33:16 -0800125 switch (code) {
Jeff Brown053b8652012-06-06 16:25:03 -0700126 case SEGV_MAPERR: return "SEGV_MAPERR";
127 case SEGV_ACCERR: return "SEGV_ACCERR";
Christopher Ferris20303f82014-01-10 16:33:16 -0800128 }
129 break;
Elliott Hughes8f7d4432012-12-10 10:29:05 -0800130 case SIGTRAP:
Christopher Ferris20303f82014-01-10 16:33:16 -0800131 switch (code) {
Elliott Hughes8f7d4432012-12-10 10:29:05 -0800132 case TRAP_BRKPT: return "TRAP_BRKPT";
133 case TRAP_TRACE: return "TRAP_TRACE";
Christopher Ferris20303f82014-01-10 16:33:16 -0800134 }
135 break;
136 }
137 // Then the other codes...
138 switch (code) {
139 case SI_USER: return "SI_USER";
Elliott Hughes8f7d4432012-12-10 10:29:05 -0800140#if defined(SI_KERNEL)
Christopher Ferris20303f82014-01-10 16:33:16 -0800141 case SI_KERNEL: return "SI_KERNEL";
Elliott Hughes8f7d4432012-12-10 10:29:05 -0800142#endif
Christopher Ferris20303f82014-01-10 16:33:16 -0800143 case SI_QUEUE: return "SI_QUEUE";
144 case SI_TIMER: return "SI_TIMER";
145 case SI_MESGQ: return "SI_MESGQ";
Elliott Hughes8f7d4432012-12-10 10:29:05 -0800146 case SI_ASYNCIO: return "SI_ASYNCIO";
147#if defined(SI_SIGIO)
Christopher Ferris20303f82014-01-10 16:33:16 -0800148 case SI_SIGIO: return "SI_SIGIO";
Elliott Hughes8f7d4432012-12-10 10:29:05 -0800149#endif
150#if defined(SI_TKILL)
Christopher Ferris20303f82014-01-10 16:33:16 -0800151 case SI_TKILL: return "SI_TKILL";
Elliott Hughes8f7d4432012-12-10 10:29:05 -0800152#endif
Christopher Ferris20303f82014-01-10 16:33:16 -0800153 }
154 // Then give up...
155 return "?";
Jeff Brown053b8652012-06-06 16:25:03 -0700156}
157
Christopher Ferris20303f82014-01-10 16:33:16 -0800158static void dump_revision_info(log_t* log) {
159 char revision[PROPERTY_VALUE_MAX];
Ben Chengd7760c12012-09-19 16:04:01 -0700160
Christopher Ferris20303f82014-01-10 16:33:16 -0800161 property_get("ro.revision", revision, "unknown");
Ben Chengd7760c12012-09-19 16:04:01 -0700162
Christopher Ferris20303f82014-01-10 16:33:16 -0800163 _LOG(log, SCOPE_AT_FAULT, "Revision: '%s'\n", revision);
Ben Chengd7760c12012-09-19 16:04:01 -0700164}
165
Christopher Ferris20303f82014-01-10 16:33:16 -0800166static void dump_build_info(log_t* log) {
167 char fingerprint[PROPERTY_VALUE_MAX];
Jeff Brown053b8652012-06-06 16:25:03 -0700168
Christopher Ferris20303f82014-01-10 16:33:16 -0800169 property_get("ro.build.fingerprint", fingerprint, "unknown");
Jeff Brown053b8652012-06-06 16:25:03 -0700170
Christopher Ferris20303f82014-01-10 16:33:16 -0800171 _LOG(log, SCOPE_AT_FAULT, "Build fingerprint: '%s'\n", fingerprint);
Jeff Brown053b8652012-06-06 16:25:03 -0700172}
173
Christopher Ferris20303f82014-01-10 16:33:16 -0800174static void dump_fault_addr(log_t* log, pid_t tid, int sig) {
175 siginfo_t si;
Jeff Brown053b8652012-06-06 16:25:03 -0700176
Christopher Ferris20303f82014-01-10 16:33:16 -0800177 memset(&si, 0, sizeof(si));
178 if (ptrace(PTRACE_GETSIGINFO, tid, 0, &si)){
179 _LOG(log, SCOPE_AT_FAULT, "cannot get siginfo: %s\n", strerror(errno));
180 } else if (signal_has_address(sig)) {
181 _LOG(log, SCOPE_AT_FAULT, "signal %d (%s), code %d (%s), fault addr %0*" PRIxPTR "\n",
182 sig, get_signame(sig), si.si_code, get_sigcode(sig, si.si_code),
183 sizeof(uintptr_t)*2, reinterpret_cast<uintptr_t>(si.si_addr));
184 } else {
185 _LOG(log, SCOPE_AT_FAULT, "signal %d (%s), code %d (%s), fault addr --------\n",
186 sig, get_signame(sig), si.si_code, get_sigcode(sig, si.si_code));
187 }
Jeff Brown053b8652012-06-06 16:25:03 -0700188}
189
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700190static void dump_thread_info(log_t* log, pid_t pid, pid_t tid, int scope_flags) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800191 char path[64];
192 char threadnamebuf[1024];
193 char* threadname = NULL;
194 FILE *fp;
Jeff Brown053b8652012-06-06 16:25:03 -0700195
Christopher Ferris20303f82014-01-10 16:33:16 -0800196 snprintf(path, sizeof(path), "/proc/%d/comm", tid);
197 if ((fp = fopen(path, "r"))) {
198 threadname = fgets(threadnamebuf, sizeof(threadnamebuf), fp);
199 fclose(fp);
200 if (threadname) {
201 size_t len = strlen(threadname);
202 if (len && threadname[len - 1] == '\n') {
203 threadname[len - 1] = '\0';
204 }
205 }
206 }
207
208 if (IS_AT_FAULT(scope_flags)) {
209 char procnamebuf[1024];
210 char* procname = NULL;
211
212 snprintf(path, sizeof(path), "/proc/%d/cmdline", pid);
Jeff Brown053b8652012-06-06 16:25:03 -0700213 if ((fp = fopen(path, "r"))) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800214 procname = fgets(procnamebuf, sizeof(procnamebuf), fp);
215 fclose(fp);
Jeff Brown053b8652012-06-06 16:25:03 -0700216 }
217
Christopher Ferris20303f82014-01-10 16:33:16 -0800218 _LOG(log, SCOPE_AT_FAULT, "pid: %d, tid: %d, name: %s >>> %s <<<\n", pid, tid,
219 threadname ? threadname : "UNKNOWN", procname ? procname : "UNKNOWN");
220 } else {
221 _LOG(log, 0, "pid: %d, tid: %d, name: %s\n", pid, tid, threadname ? threadname : "UNKNOWN");
222 }
223}
Jeff Brown053b8652012-06-06 16:25:03 -0700224
Christopher Ferris20303f82014-01-10 16:33:16 -0800225static void dump_stack_segment(
226 Backtrace* backtrace, log_t* log, int scope_flags, uintptr_t* sp, size_t words, int label) {
227 for (size_t i = 0; i < words; i++) {
228 uint32_t stack_content;
229 if (!backtrace->ReadWord(*sp, &stack_content)) {
230 break;
231 }
232
233 const char* map_name = backtrace->GetMapName(stack_content, NULL);
234 if (!map_name) {
235 map_name = "";
236 }
237 uintptr_t offset = 0;
238 std::string func_name(backtrace->GetFunctionName(stack_content, &offset));
239 if (!func_name.empty()) {
240 if (!i && label >= 0) {
241 if (offset) {
242 _LOG(log, scope_flags, " #%02d %08x %08x %s (%s+%u)\n",
243 label, *sp, stack_content, map_name, func_name.c_str(), offset);
244 } else {
245 _LOG(log, scope_flags, " #%02d %08x %08x %s (%s)\n",
246 label, *sp, stack_content, map_name, func_name.c_str());
Jeff Brown053b8652012-06-06 16:25:03 -0700247 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800248 } else {
249 if (offset) {
250 _LOG(log, scope_flags, " %08x %08x %s (%s+%u)\n",
251 *sp, stack_content, map_name, func_name.c_str(), offset);
252 } else {
253 _LOG(log, scope_flags, " %08x %08x %s (%s)\n",
254 *sp, stack_content, map_name, func_name.c_str());
255 }
256 }
Jeff Brown053b8652012-06-06 16:25:03 -0700257 } else {
Christopher Ferris20303f82014-01-10 16:33:16 -0800258 if (!i && label >= 0) {
259 _LOG(log, scope_flags, " #%02d %08x %08x %s\n",
260 label, *sp, stack_content, map_name);
261 } else {
262 _LOG(log, scope_flags, " %08x %08x %s\n",
263 *sp, stack_content, map_name);
264 }
Jeff Brown053b8652012-06-06 16:25:03 -0700265 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800266
267 *sp += sizeof(uint32_t);
268 }
Jeff Brown053b8652012-06-06 16:25:03 -0700269}
270
Christopher Ferris20303f82014-01-10 16:33:16 -0800271static void dump_stack(Backtrace* backtrace, log_t* log, int scope_flags) {
272 size_t first = 0, last;
273 for (size_t i = 0; i < backtrace->NumFrames(); i++) {
274 const backtrace_frame_data_t* frame = backtrace->GetFrame(i);
275 if (frame->sp) {
276 if (!first) {
277 first = i+1;
278 }
279 last = i;
Jeff Brown053b8652012-06-06 16:25:03 -0700280 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800281 }
282 if (!first) {
283 return;
284 }
285 first--;
286
287 scope_flags |= SCOPE_SENSITIVE;
288
289 // Dump a few words before the first frame.
290 uintptr_t sp = backtrace->GetFrame(first)->sp - STACK_WORDS * sizeof(uint32_t);
291 dump_stack_segment(backtrace, log, scope_flags, &sp, STACK_WORDS, -1);
292
293 // Dump a few words from all successive frames.
294 // Only log the first 3 frames, put the rest in the tombstone.
295 for (size_t i = first; i <= last; i++) {
296 const backtrace_frame_data_t* frame = backtrace->GetFrame(i);
297 if (sp != frame->sp) {
298 _LOG(log, scope_flags, " ........ ........\n");
299 sp = frame->sp;
300 }
301 if (i - first == 3) {
302 scope_flags &= (~SCOPE_AT_FAULT);
303 }
304 if (i == last) {
305 dump_stack_segment(backtrace, log, scope_flags, &sp, STACK_WORDS, i);
306 if (sp < frame->sp + frame->stack_size) {
307 _LOG(log, scope_flags, " ........ ........\n");
308 }
309 } else {
310 size_t words = frame->stack_size / sizeof(uint32_t);
311 if (words == 0) {
312 words = 1;
313 } else if (words > STACK_WORDS) {
314 words = STACK_WORDS;
315 }
316 dump_stack_segment(backtrace, log, scope_flags, &sp, words, i);
317 }
318 }
Jeff Brown053b8652012-06-06 16:25:03 -0700319}
320
Christopher Ferris20303f82014-01-10 16:33:16 -0800321static void dump_backtrace_and_stack(Backtrace* backtrace, log_t* log, int scope_flags) {
322 if (backtrace->NumFrames()) {
323 _LOG(log, scope_flags, "\nbacktrace:\n");
324 dump_backtrace_to_log(backtrace, log, scope_flags, " ");
Jeff Brown053b8652012-06-06 16:25:03 -0700325
Christopher Ferris20303f82014-01-10 16:33:16 -0800326 _LOG(log, scope_flags, "\nstack:\n");
327 dump_stack(backtrace, log, scope_flags);
328 }
Jeff Brown053b8652012-06-06 16:25:03 -0700329}
330
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700331static void dump_map(log_t* log, const backtrace_map_info_t* m, const char* what, int scope_flags) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800332 if (m != NULL) {
333 _LOG(log, scope_flags, " %08x-%08x %c%c%c %s\n", m->start, m->end,
334 m->is_readable ? 'r' : '-', m->is_writable ? 'w' : '-',
335 m->is_executable ? 'x' : '-', m->name);
336 } else {
337 _LOG(log, scope_flags, " (no %s)\n", what);
338 }
Elliott Hughesd1420be2013-01-03 13:39:57 -0800339}
340
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700341static void dump_nearby_maps(const backtrace_map_info_t* map_info_list, log_t* log, pid_t tid, int scope_flags) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800342 scope_flags |= SCOPE_SENSITIVE;
343 siginfo_t si;
344 memset(&si, 0, sizeof(si));
345 if (ptrace(PTRACE_GETSIGINFO, tid, 0, &si)) {
346 _LOG(log, scope_flags, "cannot get siginfo for %d: %s\n", tid, strerror(errno));
347 return;
348 }
349 if (!signal_has_address(si.si_signo)) {
350 return;
351 }
352
353 uintptr_t addr = (uintptr_t) si.si_addr;
354 addr &= ~0xfff; // round to 4K page boundary
355 if (addr == 0) { // null-pointer deref
356 return;
357 }
358
359 _LOG(log, scope_flags, "\nmemory map around fault addr %" PRIxPTR ":\n",
360 reinterpret_cast<uintptr_t>(si.si_addr));
361
362 // Search for a match, or for a hole where the match would be. The list
363 // is backward from the file content, so it starts at high addresses.
364 const backtrace_map_info_t* map = map_info_list;
365 const backtrace_map_info_t* next = NULL;
366 const backtrace_map_info_t* prev = NULL;
367 while (map != NULL) {
368 if (addr >= map->start && addr < map->end) {
369 next = map->next;
370 break;
371 } else if (addr >= map->end) {
372 // map would be between "prev" and this entry
373 next = map;
374 map = NULL;
375 break;
Jeff Brown053b8652012-06-06 16:25:03 -0700376 }
377
Christopher Ferris20303f82014-01-10 16:33:16 -0800378 prev = map;
379 map = map->next;
380 }
Jeff Brown053b8652012-06-06 16:25:03 -0700381
Christopher Ferris20303f82014-01-10 16:33:16 -0800382 // Show "next" then "match" then "prev" so that the addresses appear in
383 // ascending order (like /proc/pid/maps).
384 dump_map(log, next, "map below", scope_flags);
385 dump_map(log, map, "map for address", scope_flags);
386 dump_map(log, prev, "map above", scope_flags);
Jeff Brown053b8652012-06-06 16:25:03 -0700387}
388
Christopher Ferris20303f82014-01-10 16:33:16 -0800389static void dump_thread(
390 Backtrace* backtrace, log_t* log, int scope_flags, int* total_sleep_time_usec) {
391 wait_for_stop(backtrace->Tid(), total_sleep_time_usec);
Jeff Brown053b8652012-06-06 16:25:03 -0700392
Christopher Ferris20303f82014-01-10 16:33:16 -0800393 dump_registers(log, backtrace->Tid(), scope_flags);
394 dump_backtrace_and_stack(backtrace, log, scope_flags);
395 if (IS_AT_FAULT(scope_flags)) {
396 dump_memory_and_code(log, backtrace->Tid(), scope_flags);
397 dump_nearby_maps(backtrace->GetMapList(), log, backtrace->Tid(), scope_flags);
398 }
Jeff Brown053b8652012-06-06 16:25:03 -0700399}
400
Christopher Ferris20303f82014-01-10 16:33:16 -0800401// Return true if some thread is not detached cleanly
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700402static bool dump_sibling_thread_report(
Christopher Ferris20303f82014-01-10 16:33:16 -0800403 log_t* log, pid_t pid, pid_t tid, int* total_sleep_time_usec, backtrace_map_info_t* map_info) {
404 char task_path[64];
405 snprintf(task_path, sizeof(task_path), "/proc/%d/task", pid);
Jeff Brown053b8652012-06-06 16:25:03 -0700406
Christopher Ferris20303f82014-01-10 16:33:16 -0800407 DIR* d = opendir(task_path);
408 // Bail early if the task directory cannot be opened
409 if (d == NULL) {
410 XLOG("Cannot open /proc/%d/task\n", pid);
411 return false;
412 }
413
414 bool detach_failed = false;
415 struct dirent* de;
416 while ((de = readdir(d)) != NULL) {
417 // Ignore "." and ".."
418 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, "..")) {
419 continue;
Jeff Brown053b8652012-06-06 16:25:03 -0700420 }
421
Christopher Ferris20303f82014-01-10 16:33:16 -0800422 // The main thread at fault has been handled individually
423 char* end;
424 pid_t new_tid = strtoul(de->d_name, &end, 10);
425 if (*end || new_tid == tid) {
426 continue;
Jeff Brown053b8652012-06-06 16:25:03 -0700427 }
428
Christopher Ferris20303f82014-01-10 16:33:16 -0800429 // Skip this thread if cannot ptrace it
430 if (ptrace(PTRACE_ATTACH, new_tid, 0, 0) < 0) {
431 continue;
432 }
433
434 _LOG(log, 0, "--- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---\n");
435 dump_thread_info(log, pid, new_tid, 0);
436
437 UniquePtr<Backtrace> backtrace(Backtrace::Create(pid, new_tid, map_info));
438 if (backtrace->Unwind(0)) {
439 dump_thread(backtrace.get(), log, 0, total_sleep_time_usec);
440 }
441
442 if (ptrace(PTRACE_DETACH, new_tid, 0, 0) != 0) {
443 LOG("ptrace detach from %d failed: %s\n", new_tid, strerror(errno));
444 detach_failed = true;
445 }
446 }
447
448 closedir(d);
449 return detach_failed;
Jeff Brown053b8652012-06-06 16:25:03 -0700450}
451
Christopher Ferris20303f82014-01-10 16:33:16 -0800452// Reads the contents of the specified log device, filters out the entries
453// that don't match the specified pid, and writes them to the tombstone file.
454//
455// If "tailOnly" is set, we only print the last few lines.
456static void dump_log_file(log_t* log, pid_t pid, const char* filename, bool tailOnly) {
457 bool first = true;
Jeff Brown053b8652012-06-06 16:25:03 -0700458
Christopher Ferris20303f82014-01-10 16:33:16 -0800459 // circular buffer, for "tailOnly" mode
460 const int kShortLogMaxLines = 5;
461 const int kShortLogLineLen = 256;
462 char shortLog[kShortLogMaxLines][kShortLogLineLen];
463 int shortLogCount = 0;
464 int shortLogNext = 0;
Jeff Brown053b8652012-06-06 16:25:03 -0700465
Christopher Ferris20303f82014-01-10 16:33:16 -0800466 int logfd = open(filename, O_RDONLY | O_NONBLOCK);
467 if (logfd < 0) {
468 XLOG("Unable to open %s: %s\n", filename, strerror(errno));
469 return;
470 }
471
472 union {
473 unsigned char buf[LOGGER_ENTRY_MAX_LEN + 1];
474 struct logger_entry entry;
475 } log_entry;
476
477 while (true) {
478 ssize_t actual = read(logfd, log_entry.buf, LOGGER_ENTRY_MAX_LEN);
479 if (actual < 0) {
480 if (errno == EINTR) {
481 // interrupted by signal, retry
482 continue;
483 } else if (errno == EAGAIN) {
484 // non-blocking EOF; we're done
485 break;
486 } else {
487 _LOG(log, 0, "Error while reading log: %s\n", strerror(errno));
488 break;
489 }
490 } else if (actual == 0) {
491 _LOG(log, 0, "Got zero bytes while reading log: %s\n", strerror(errno));
492 break;
Jeff Brown053b8652012-06-06 16:25:03 -0700493 }
494
Christopher Ferris20303f82014-01-10 16:33:16 -0800495 // NOTE: if you XLOG something here, this will spin forever,
496 // because you will be writing as fast as you're reading. Any
497 // high-frequency debug diagnostics should just be written to
498 // the tombstone file.
499 struct logger_entry* entry = &log_entry.entry;
Jeff Brown053b8652012-06-06 16:25:03 -0700500
Christopher Ferris20303f82014-01-10 16:33:16 -0800501 if (entry->pid != static_cast<int32_t>(pid)) {
502 // wrong pid, ignore
503 continue;
Jeff Brown053b8652012-06-06 16:25:03 -0700504 }
505
Christopher Ferris20303f82014-01-10 16:33:16 -0800506 if (first) {
507 _LOG(log, 0, "--------- %slog %s\n", tailOnly ? "tail end of " : "", filename);
508 first = false;
509 }
510
511 // Msg format is: <priority:1><tag:N>\0<message:N>\0
512 //
513 // We want to display it in the same format as "logcat -v threadtime"
514 // (although in this case the pid is redundant).
515 //
516 // TODO: scan for line breaks ('\n') and display each text line
517 // on a separate line, prefixed with the header, like logcat does.
518 static const char* kPrioChars = "!.VDIWEFS";
519 unsigned char prio = entry->msg[0];
520 char* tag = entry->msg + 1;
521 char* msg = tag + strlen(tag) + 1;
522
523 // consume any trailing newlines
524 char* eatnl = msg + strlen(msg) - 1;
525 while (eatnl >= msg && *eatnl == '\n') {
526 *eatnl-- = '\0';
527 }
528
529 char prioChar = (prio < strlen(kPrioChars) ? kPrioChars[prio] : '?');
530
531 char timeBuf[32];
532 time_t sec = static_cast<time_t>(entry->sec);
533 struct tm tmBuf;
534 struct tm* ptm;
535 ptm = localtime_r(&sec, &tmBuf);
536 strftime(timeBuf, sizeof(timeBuf), "%m-%d %H:%M:%S", ptm);
537
Jeff Brown053b8652012-06-06 16:25:03 -0700538 if (tailOnly) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800539 snprintf(shortLog[shortLogNext], kShortLogLineLen,
540 "%s.%03d %5d %5d %c %-8s: %s",
541 timeBuf, entry->nsec / 1000000, entry->pid, entry->tid,
542 prioChar, tag, msg);
543 shortLogNext = (shortLogNext + 1) % kShortLogMaxLines;
544 shortLogCount++;
545 } else {
546 _LOG(log, 0, "%s.%03d %5d %5d %c %-8s: %s\n",
547 timeBuf, entry->nsec / 1000000, entry->pid, entry->tid, prioChar, tag, msg);
548 }
549 }
Jeff Brown053b8652012-06-06 16:25:03 -0700550
Christopher Ferris20303f82014-01-10 16:33:16 -0800551 if (tailOnly) {
552 int i;
Jeff Brown053b8652012-06-06 16:25:03 -0700553
Christopher Ferris20303f82014-01-10 16:33:16 -0800554 // If we filled the buffer, we want to start at "next", which has
555 // the oldest entry. If we didn't, we want to start at zero.
556 if (shortLogCount < kShortLogMaxLines) {
557 shortLogNext = 0;
558 } else {
559 shortLogCount = kShortLogMaxLines; // cap at window size
Jeff Brown053b8652012-06-06 16:25:03 -0700560 }
561
Christopher Ferris20303f82014-01-10 16:33:16 -0800562 for (i = 0; i < shortLogCount; i++) {
563 _LOG(log, 0, "%s\n", shortLog[shortLogNext]);
564 shortLogNext = (shortLogNext + 1) % kShortLogMaxLines;
565 }
566 }
567
568 close(logfd);
Jeff Brown053b8652012-06-06 16:25:03 -0700569}
570
Christopher Ferris20303f82014-01-10 16:33:16 -0800571// Dumps the logs generated by the specified pid to the tombstone, from both
572// "system" and "main" log devices. Ideally we'd interleave the output.
573static void dump_logs(log_t* log, pid_t pid, bool tailOnly) {
574 dump_log_file(log, pid, "/dev/log/system", tailOnly);
575 dump_log_file(log, pid, "/dev/log/main", tailOnly);
Jeff Brown053b8652012-06-06 16:25:03 -0700576}
577
Christopher Ferris20303f82014-01-10 16:33:16 -0800578static void dump_abort_message(Backtrace* backtrace, log_t* log, uintptr_t address) {
Elliott Hughese5f8a692013-04-04 13:52:01 -0700579 if (address == 0) {
580 return;
581 }
582
583 address += sizeof(size_t); // Skip the buffer length.
584
585 char msg[512];
586 memset(msg, 0, sizeof(msg));
587 char* p = &msg[0];
588 while (p < &msg[sizeof(msg)]) {
589 uint32_t data;
Christopher Ferris20303f82014-01-10 16:33:16 -0800590 if (!backtrace->ReadWord(address, &data)) {
Elliott Hughese5f8a692013-04-04 13:52:01 -0700591 break;
592 }
593 address += sizeof(uint32_t);
594
595 if ((*p++ = (data >> 0) & 0xff) == 0) {
596 break;
597 }
598 if ((*p++ = (data >> 8) & 0xff) == 0) {
599 break;
600 }
601 if ((*p++ = (data >> 16) & 0xff) == 0) {
602 break;
603 }
604 if ((*p++ = (data >> 24) & 0xff) == 0) {
605 break;
606 }
607 }
608 msg[sizeof(msg) - 1] = '\0';
609
Christopher Tate7716aef2013-04-02 14:00:27 -0700610 _LOG(log, SCOPE_AT_FAULT, "Abort message: '%s'\n", msg);
Elliott Hughese5f8a692013-04-04 13:52:01 -0700611}
612
Christopher Ferris20303f82014-01-10 16:33:16 -0800613// Dumps all information about the specified pid to the tombstone.
Elliott Hughese5f8a692013-04-04 13:52:01 -0700614static bool dump_crash(log_t* log, pid_t pid, pid_t tid, int signal, uintptr_t abort_msg_address,
Christopher Ferris20303f82014-01-10 16:33:16 -0800615 bool dump_sibling_threads, int* total_sleep_time_usec) {
616 // don't copy log messages to tombstone unless this is a dev device
617 char value[PROPERTY_VALUE_MAX];
618 property_get("ro.debuggable", value, "0");
619 bool want_logs = (value[0] == '1');
Jeff Brown053b8652012-06-06 16:25:03 -0700620
Christopher Ferris20303f82014-01-10 16:33:16 -0800621 if (log->amfd >= 0) {
622 // Activity Manager protocol: binary 32-bit network-byte-order ints for the
623 // pid and signal number, followed by the raw text of the dump, culminating
624 // in a zero byte that marks end-of-data.
625 uint32_t datum = htonl(pid);
626 TEMP_FAILURE_RETRY( write(log->amfd, &datum, 4) );
627 datum = htonl(signal);
628 TEMP_FAILURE_RETRY( write(log->amfd, &datum, 4) );
629 }
Christopher Tateded2e5a2013-03-19 13:12:23 -0700630
Christopher Ferris20303f82014-01-10 16:33:16 -0800631 _LOG(log, SCOPE_AT_FAULT,
632 "*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***\n");
633 dump_build_info(log);
634 dump_revision_info(log);
635 dump_thread_info(log, pid, tid, SCOPE_AT_FAULT);
636 if (signal) {
637 dump_fault_addr(log, tid, signal);
638 }
Jeff Brown053b8652012-06-06 16:25:03 -0700639
Christopher Ferris20303f82014-01-10 16:33:16 -0800640 // Gather the map info once for all this process' threads.
641 backtrace_map_info_t* map_info = backtrace_create_map_info_list(pid);
Jeff Brown053b8652012-06-06 16:25:03 -0700642
Christopher Ferris20303f82014-01-10 16:33:16 -0800643 UniquePtr<Backtrace> backtrace(Backtrace::Create(pid, tid, map_info));
644 if (backtrace->Unwind(0)) {
645 dump_abort_message(backtrace.get(), log, abort_msg_address);
646 dump_thread(backtrace.get(), log, SCOPE_AT_FAULT, total_sleep_time_usec);
647 }
Jeff Brown053b8652012-06-06 16:25:03 -0700648
Christopher Ferris20303f82014-01-10 16:33:16 -0800649 if (want_logs) {
650 dump_logs(log, pid, true);
651 }
Jeff Brown053b8652012-06-06 16:25:03 -0700652
Christopher Ferris20303f82014-01-10 16:33:16 -0800653 bool detach_failed = false;
654 if (dump_sibling_threads) {
655 detach_failed = dump_sibling_thread_report(log, pid, tid, total_sleep_time_usec, map_info);
656 }
Christopher Ferris98464972014-01-06 19:16:33 -0800657
Christopher Ferris20303f82014-01-10 16:33:16 -0800658 // Destroy the previously created map info.
659 backtrace_destroy_map_info_list(map_info);
Christopher Tateded2e5a2013-03-19 13:12:23 -0700660
Christopher Ferris20303f82014-01-10 16:33:16 -0800661 if (want_logs) {
662 dump_logs(log, pid, false);
663 }
Christopher Tateded2e5a2013-03-19 13:12:23 -0700664
Christopher Ferris20303f82014-01-10 16:33:16 -0800665 // send EOD to the Activity Manager, then wait for its ack to avoid racing ahead
666 // and killing the target out from under it
667 if (log->amfd >= 0) {
668 uint8_t eodMarker = 0;
669 TEMP_FAILURE_RETRY( write(log->amfd, &eodMarker, 1) );
670 // 3 sec timeout reading the ack; we're fine if that happens
671 TEMP_FAILURE_RETRY( read(log->amfd, &eodMarker, 1) );
672 }
673
674 return detach_failed;
Jeff Brown053b8652012-06-06 16:25:03 -0700675}
676
Christopher Ferris20303f82014-01-10 16:33:16 -0800677// find_and_open_tombstone - find an available tombstone slot, if any, of the
678// form tombstone_XX where XX is 00 to MAX_TOMBSTONES-1, inclusive. If no
679// file is available, we reuse the least-recently-modified file.
680//
681// Returns the path of the tombstone file, allocated using malloc(). Caller must free() it.
682static char* find_and_open_tombstone(int* fd) {
683 unsigned long mtime = ULONG_MAX;
684 struct stat sb;
Jeff Brown053b8652012-06-06 16:25:03 -0700685
Christopher Ferris20303f82014-01-10 16:33:16 -0800686 // XXX: Our stat.st_mtime isn't time_t. If it changes, as it probably ought
687 // to, our logic breaks. This check will generate a warning if that happens.
688 typecheck(mtime, sb.st_mtime);
Jeff Brown053b8652012-06-06 16:25:03 -0700689
Christopher Ferris20303f82014-01-10 16:33:16 -0800690 // In a single wolf-like pass, find an available slot and, in case none
691 // exist, find and record the least-recently-modified file.
692 char path[128];
693 int oldest = 0;
694 for (int i = 0; i < MAX_TOMBSTONES; i++) {
695 snprintf(path, sizeof(path), TOMBSTONE_DIR"/tombstone_%02d", i);
Jeff Brown053b8652012-06-06 16:25:03 -0700696
Christopher Ferris20303f82014-01-10 16:33:16 -0800697 if (!stat(path, &sb)) {
698 if (sb.st_mtime < mtime) {
699 oldest = i;
700 mtime = sb.st_mtime;
701 }
702 continue;
Jeff Brown053b8652012-06-06 16:25:03 -0700703 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800704 if (errno != ENOENT)
705 continue;
Jeff Brown053b8652012-06-06 16:25:03 -0700706
Christopher Ferris20303f82014-01-10 16:33:16 -0800707 *fd = open(path, O_CREAT | O_EXCL | O_WRONLY, 0600);
708 if (*fd < 0)
709 continue; // raced ?
710
Jeff Brown053b8652012-06-06 16:25:03 -0700711 fchown(*fd, AID_SYSTEM, AID_SYSTEM);
712 return strdup(path);
Christopher Ferris20303f82014-01-10 16:33:16 -0800713 }
714
715 // we didn't find an available file, so we clobber the oldest one
716 snprintf(path, sizeof(path), TOMBSTONE_DIR"/tombstone_%02d", oldest);
717 *fd = open(path, O_CREAT | O_TRUNC | O_WRONLY, 0600);
718 if (*fd < 0) {
719 LOG("failed to open tombstone file '%s': %s\n", path, strerror(errno));
720 return NULL;
721 }
722 fchown(*fd, AID_SYSTEM, AID_SYSTEM);
723 return strdup(path);
Jeff Brown053b8652012-06-06 16:25:03 -0700724}
725
Christopher Tateded2e5a2013-03-19 13:12:23 -0700726static int activity_manager_connect() {
Christopher Ferris20303f82014-01-10 16:33:16 -0800727 int amfd = socket(PF_UNIX, SOCK_STREAM, 0);
728 if (amfd >= 0) {
729 struct sockaddr_un address;
730 int err;
Christopher Tateded2e5a2013-03-19 13:12:23 -0700731
Christopher Ferris20303f82014-01-10 16:33:16 -0800732 memset(&address, 0, sizeof(address));
733 address.sun_family = AF_UNIX;
734 strncpy(address.sun_path, NCRASH_SOCKET_PATH, sizeof(address.sun_path));
735 err = TEMP_FAILURE_RETRY(connect(
736 amfd, reinterpret_cast<struct sockaddr*>(&address), sizeof(address)));
737 if (!err) {
738 struct timeval tv;
739 memset(&tv, 0, sizeof(tv));
740 tv.tv_sec = 1; // tight leash
741 err = setsockopt(amfd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
742 if (!err) {
743 tv.tv_sec = 3; // 3 seconds on handshake read
744 err = setsockopt(amfd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
745 }
Christopher Tateded2e5a2013-03-19 13:12:23 -0700746 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800747 if (err) {
748 close(amfd);
749 amfd = -1;
750 }
751 }
Christopher Tateded2e5a2013-03-19 13:12:23 -0700752
Christopher Ferris20303f82014-01-10 16:33:16 -0800753 return amfd;
Christopher Tateded2e5a2013-03-19 13:12:23 -0700754}
755
Christopher Ferris20303f82014-01-10 16:33:16 -0800756char* engrave_tombstone(
757 pid_t pid, pid_t tid, int signal, uintptr_t abort_msg_address, bool dump_sibling_threads,
758 bool quiet, bool* detach_failed, int* total_sleep_time_usec) {
759 mkdir(TOMBSTONE_DIR, 0755);
760 chown(TOMBSTONE_DIR, AID_SYSTEM, AID_SYSTEM);
Jeff Brown053b8652012-06-06 16:25:03 -0700761
Christopher Ferris20303f82014-01-10 16:33:16 -0800762 if (selinux_android_restorecon(TOMBSTONE_DIR) == -1) {
763 *detach_failed = false;
764 return NULL;
765 }
rpcraigf1186f32012-07-19 09:38:06 -0400766
Christopher Ferris20303f82014-01-10 16:33:16 -0800767 int fd;
768 char* path = find_and_open_tombstone(&fd);
769 if (!path) {
770 *detach_failed = false;
771 return NULL;
772 }
Jeff Brown053b8652012-06-06 16:25:03 -0700773
Christopher Ferris20303f82014-01-10 16:33:16 -0800774 log_t log;
775 log.tfd = fd;
776 log.amfd = activity_manager_connect();
777 log.quiet = quiet;
778 *detach_failed = dump_crash(
779 &log, pid, tid, signal, abort_msg_address, dump_sibling_threads, total_sleep_time_usec);
Jeff Brown053b8652012-06-06 16:25:03 -0700780
Christopher Ferris20303f82014-01-10 16:33:16 -0800781 close(log.amfd);
782 close(fd);
783 return path;
Jeff Brown053b8652012-06-06 16:25:03 -0700784}