blob: 436b845da0ab7e12941052d522e4e79c3e68e294 [file] [log] [blame]
Jeff Brown053b8652012-06-06 16:25:03 -07001/*
Mark Salyzyn22b5cef2013-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>
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
Mark Salyzyn22b5cef2013-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 Ferris20303f82014-01-10 16:33:16 -080036#include <backtrace/Backtrace.h>
Christopher Ferris46756822014-01-14 20:16:30 -080037#include <backtrace/BacktraceMap.h>
Jeff Brown053b8652012-06-06 16:25:03 -070038
Christopher Tateded2e5a2013-03-19 13:12:23 -070039#include <sys/socket.h>
40#include <linux/un.h>
41
rpcraigf1186f32012-07-19 09:38:06 -040042#include <selinux/android.h>
rpcraigf1186f32012-07-19 09:38:06 -040043
Christopher Ferris20303f82014-01-10 16:33:16 -080044#include <UniquePtr.h>
45
Jeff Brown053b8652012-06-06 16:25:03 -070046#include "machine.h"
47#include "tombstone.h"
Christopher Ferris365e4ae2013-10-02 12:26:48 -070048#include "backtrace.h"
Jeff Brown053b8652012-06-06 16:25:03 -070049
Jeff Brown053b8652012-06-06 16:25:03 -070050#define STACK_WORDS 16
51
52#define MAX_TOMBSTONES 10
53#define TOMBSTONE_DIR "/data/tombstones"
54
Christopher Ferris20303f82014-01-10 16:33:16 -080055// Must match the path defined in NativeCrashListener.java
Christopher Tateded2e5a2013-03-19 13:12:23 -070056#define NCRASH_SOCKET_PATH "/data/system/ndebugsocket"
57
Jeff Brown053b8652012-06-06 16:25:03 -070058#define typecheck(x,y) { \
Christopher Ferris20303f82014-01-10 16:33:16 -080059 typeof(x) __dummy1; \
60 typeof(y) __dummy2; \
61 (void)(&__dummy1 == &__dummy2); }
Jeff Brown053b8652012-06-06 16:25:03 -070062
63
64static bool signal_has_address(int sig) {
Christopher Ferris20303f82014-01-10 16:33:16 -080065 switch (sig) {
Jeff Brown053b8652012-06-06 16:25:03 -070066 case SIGILL:
Christopher Ferris20303f82014-01-10 16:33:16 -080067 case SIGFPE:
68 case SIGSEGV:
69 case SIGBUS:
70 return true;
71 default:
72 return false;
73 }
74}
75
76static const char* get_signame(int sig) {
77 switch(sig) {
78 case SIGILL: return "SIGILL";
79 case SIGABRT: return "SIGABRT";
80 case SIGBUS: return "SIGBUS";
81 case SIGFPE: return "SIGFPE";
82 case SIGSEGV: return "SIGSEGV";
83 case SIGPIPE: return "SIGPIPE";
84#ifdef SIGSTKFLT
85 case SIGSTKFLT: return "SIGSTKFLT";
86#endif
87 case SIGSTOP: return "SIGSTOP";
88 default: return "?";
89 }
90}
91
92static const char* get_sigcode(int signo, int code) {
93 // Try the signal-specific codes...
94 switch (signo) {
95 case SIGILL:
96 switch (code) {
Jeff Brown053b8652012-06-06 16:25:03 -070097 case ILL_ILLOPC: return "ILL_ILLOPC";
98 case ILL_ILLOPN: return "ILL_ILLOPN";
99 case ILL_ILLADR: return "ILL_ILLADR";
100 case ILL_ILLTRP: return "ILL_ILLTRP";
101 case ILL_PRVOPC: return "ILL_PRVOPC";
102 case ILL_PRVREG: return "ILL_PRVREG";
103 case ILL_COPROC: return "ILL_COPROC";
104 case ILL_BADSTK: return "ILL_BADSTK";
Christopher Ferris20303f82014-01-10 16:33:16 -0800105 }
106 break;
Jeff Brown053b8652012-06-06 16:25:03 -0700107 case SIGBUS:
Christopher Ferris20303f82014-01-10 16:33:16 -0800108 switch (code) {
Jeff Brown053b8652012-06-06 16:25:03 -0700109 case BUS_ADRALN: return "BUS_ADRALN";
110 case BUS_ADRERR: return "BUS_ADRERR";
111 case BUS_OBJERR: return "BUS_OBJERR";
Christopher Ferris20303f82014-01-10 16:33:16 -0800112 }
113 break;
Jeff Brown053b8652012-06-06 16:25:03 -0700114 case SIGFPE:
Christopher Ferris20303f82014-01-10 16:33:16 -0800115 switch (code) {
Jeff Brown053b8652012-06-06 16:25:03 -0700116 case FPE_INTDIV: return "FPE_INTDIV";
117 case FPE_INTOVF: return "FPE_INTOVF";
118 case FPE_FLTDIV: return "FPE_FLTDIV";
119 case FPE_FLTOVF: return "FPE_FLTOVF";
120 case FPE_FLTUND: return "FPE_FLTUND";
121 case FPE_FLTRES: return "FPE_FLTRES";
122 case FPE_FLTINV: return "FPE_FLTINV";
123 case FPE_FLTSUB: return "FPE_FLTSUB";
Christopher Ferris20303f82014-01-10 16:33:16 -0800124 }
125 break;
Jeff Brown053b8652012-06-06 16:25:03 -0700126 case SIGSEGV:
Christopher Ferris20303f82014-01-10 16:33:16 -0800127 switch (code) {
Jeff Brown053b8652012-06-06 16:25:03 -0700128 case SEGV_MAPERR: return "SEGV_MAPERR";
129 case SEGV_ACCERR: return "SEGV_ACCERR";
Christopher Ferris20303f82014-01-10 16:33:16 -0800130 }
131 break;
Elliott Hughes8f7d4432012-12-10 10:29:05 -0800132 case SIGTRAP:
Christopher Ferris20303f82014-01-10 16:33:16 -0800133 switch (code) {
Elliott Hughes8f7d4432012-12-10 10:29:05 -0800134 case TRAP_BRKPT: return "TRAP_BRKPT";
135 case TRAP_TRACE: return "TRAP_TRACE";
Christopher Ferris20303f82014-01-10 16:33:16 -0800136 }
137 break;
138 }
139 // Then the other codes...
140 switch (code) {
141 case SI_USER: return "SI_USER";
Elliott Hughes8f7d4432012-12-10 10:29:05 -0800142#if defined(SI_KERNEL)
Christopher Ferris20303f82014-01-10 16:33:16 -0800143 case SI_KERNEL: return "SI_KERNEL";
Elliott Hughes8f7d4432012-12-10 10:29:05 -0800144#endif
Christopher Ferris20303f82014-01-10 16:33:16 -0800145 case SI_QUEUE: return "SI_QUEUE";
146 case SI_TIMER: return "SI_TIMER";
147 case SI_MESGQ: return "SI_MESGQ";
Elliott Hughes8f7d4432012-12-10 10:29:05 -0800148 case SI_ASYNCIO: return "SI_ASYNCIO";
149#if defined(SI_SIGIO)
Christopher Ferris20303f82014-01-10 16:33:16 -0800150 case SI_SIGIO: return "SI_SIGIO";
Elliott Hughes8f7d4432012-12-10 10:29:05 -0800151#endif
152#if defined(SI_TKILL)
Christopher Ferris20303f82014-01-10 16:33:16 -0800153 case SI_TKILL: return "SI_TKILL";
Elliott Hughes8f7d4432012-12-10 10:29:05 -0800154#endif
Christopher Ferris20303f82014-01-10 16:33:16 -0800155 }
156 // Then give up...
157 return "?";
Jeff Brown053b8652012-06-06 16:25:03 -0700158}
159
Christopher Ferris20303f82014-01-10 16:33:16 -0800160static void dump_revision_info(log_t* log) {
161 char revision[PROPERTY_VALUE_MAX];
Ben Chengd7760c12012-09-19 16:04:01 -0700162
Christopher Ferris20303f82014-01-10 16:33:16 -0800163 property_get("ro.revision", revision, "unknown");
Ben Chengd7760c12012-09-19 16:04:01 -0700164
Christopher Ferris20303f82014-01-10 16:33:16 -0800165 _LOG(log, SCOPE_AT_FAULT, "Revision: '%s'\n", revision);
Ben Chengd7760c12012-09-19 16:04:01 -0700166}
167
Christopher Ferris20303f82014-01-10 16:33:16 -0800168static void dump_build_info(log_t* log) {
169 char fingerprint[PROPERTY_VALUE_MAX];
Jeff Brown053b8652012-06-06 16:25:03 -0700170
Christopher Ferris20303f82014-01-10 16:33:16 -0800171 property_get("ro.build.fingerprint", fingerprint, "unknown");
Jeff Brown053b8652012-06-06 16:25:03 -0700172
Christopher Ferris20303f82014-01-10 16:33:16 -0800173 _LOG(log, SCOPE_AT_FAULT, "Build fingerprint: '%s'\n", fingerprint);
Jeff Brown053b8652012-06-06 16:25:03 -0700174}
175
Christopher Ferris20303f82014-01-10 16:33:16 -0800176static void dump_fault_addr(log_t* log, pid_t tid, int sig) {
177 siginfo_t si;
Jeff Brown053b8652012-06-06 16:25:03 -0700178
Christopher Ferris20303f82014-01-10 16:33:16 -0800179 memset(&si, 0, sizeof(si));
180 if (ptrace(PTRACE_GETSIGINFO, tid, 0, &si)){
181 _LOG(log, SCOPE_AT_FAULT, "cannot get siginfo: %s\n", strerror(errno));
182 } else if (signal_has_address(sig)) {
183 _LOG(log, SCOPE_AT_FAULT, "signal %d (%s), code %d (%s), fault addr %0*" PRIxPTR "\n",
184 sig, get_signame(sig), si.si_code, get_sigcode(sig, si.si_code),
185 sizeof(uintptr_t)*2, reinterpret_cast<uintptr_t>(si.si_addr));
186 } else {
187 _LOG(log, SCOPE_AT_FAULT, "signal %d (%s), code %d (%s), fault addr --------\n",
188 sig, get_signame(sig), si.si_code, get_sigcode(sig, si.si_code));
189 }
Jeff Brown053b8652012-06-06 16:25:03 -0700190}
191
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700192static void dump_thread_info(log_t* log, pid_t pid, pid_t tid, int scope_flags) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800193 char path[64];
194 char threadnamebuf[1024];
195 char* threadname = NULL;
196 FILE *fp;
Jeff Brown053b8652012-06-06 16:25:03 -0700197
Christopher Ferris20303f82014-01-10 16:33:16 -0800198 snprintf(path, sizeof(path), "/proc/%d/comm", tid);
199 if ((fp = fopen(path, "r"))) {
200 threadname = fgets(threadnamebuf, sizeof(threadnamebuf), fp);
201 fclose(fp);
202 if (threadname) {
203 size_t len = strlen(threadname);
204 if (len && threadname[len - 1] == '\n') {
205 threadname[len - 1] = '\0';
206 }
207 }
208 }
209
210 if (IS_AT_FAULT(scope_flags)) {
211 char procnamebuf[1024];
212 char* procname = NULL;
213
214 snprintf(path, sizeof(path), "/proc/%d/cmdline", pid);
Jeff Brown053b8652012-06-06 16:25:03 -0700215 if ((fp = fopen(path, "r"))) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800216 procname = fgets(procnamebuf, sizeof(procnamebuf), fp);
217 fclose(fp);
Jeff Brown053b8652012-06-06 16:25:03 -0700218 }
219
Christopher Ferris20303f82014-01-10 16:33:16 -0800220 _LOG(log, SCOPE_AT_FAULT, "pid: %d, tid: %d, name: %s >>> %s <<<\n", pid, tid,
221 threadname ? threadname : "UNKNOWN", procname ? procname : "UNKNOWN");
222 } else {
223 _LOG(log, 0, "pid: %d, tid: %d, name: %s\n", pid, tid, threadname ? threadname : "UNKNOWN");
224 }
225}
Jeff Brown053b8652012-06-06 16:25:03 -0700226
Christopher Ferris20303f82014-01-10 16:33:16 -0800227static void dump_stack_segment(
228 Backtrace* backtrace, log_t* log, int scope_flags, uintptr_t* sp, size_t words, int label) {
229 for (size_t i = 0; i < words; i++) {
230 uint32_t stack_content;
231 if (!backtrace->ReadWord(*sp, &stack_content)) {
232 break;
233 }
234
Christopher Ferris46756822014-01-14 20:16:30 -0800235 const backtrace_map_t* map = backtrace->FindMap(stack_content);
236 const char* map_name;
237 if (!map) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800238 map_name = "";
Christopher Ferris46756822014-01-14 20:16:30 -0800239 } else {
240 map_name = map->name.c_str();
Christopher Ferris20303f82014-01-10 16:33:16 -0800241 }
242 uintptr_t offset = 0;
243 std::string func_name(backtrace->GetFunctionName(stack_content, &offset));
244 if (!func_name.empty()) {
245 if (!i && label >= 0) {
246 if (offset) {
247 _LOG(log, scope_flags, " #%02d %08x %08x %s (%s+%u)\n",
248 label, *sp, stack_content, map_name, func_name.c_str(), offset);
249 } else {
250 _LOG(log, scope_flags, " #%02d %08x %08x %s (%s)\n",
251 label, *sp, stack_content, map_name, func_name.c_str());
Jeff Brown053b8652012-06-06 16:25:03 -0700252 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800253 } else {
254 if (offset) {
255 _LOG(log, scope_flags, " %08x %08x %s (%s+%u)\n",
256 *sp, stack_content, map_name, func_name.c_str(), offset);
257 } else {
258 _LOG(log, scope_flags, " %08x %08x %s (%s)\n",
259 *sp, stack_content, map_name, func_name.c_str());
260 }
261 }
Jeff Brown053b8652012-06-06 16:25:03 -0700262 } else {
Christopher Ferris20303f82014-01-10 16:33:16 -0800263 if (!i && label >= 0) {
264 _LOG(log, scope_flags, " #%02d %08x %08x %s\n",
265 label, *sp, stack_content, map_name);
266 } else {
267 _LOG(log, scope_flags, " %08x %08x %s\n",
268 *sp, stack_content, map_name);
269 }
Jeff Brown053b8652012-06-06 16:25:03 -0700270 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800271
272 *sp += sizeof(uint32_t);
273 }
Jeff Brown053b8652012-06-06 16:25:03 -0700274}
275
Christopher Ferris20303f82014-01-10 16:33:16 -0800276static void dump_stack(Backtrace* backtrace, log_t* log, int scope_flags) {
277 size_t first = 0, last;
278 for (size_t i = 0; i < backtrace->NumFrames(); i++) {
279 const backtrace_frame_data_t* frame = backtrace->GetFrame(i);
280 if (frame->sp) {
281 if (!first) {
282 first = i+1;
283 }
284 last = i;
Jeff Brown053b8652012-06-06 16:25:03 -0700285 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800286 }
287 if (!first) {
288 return;
289 }
290 first--;
291
292 scope_flags |= SCOPE_SENSITIVE;
293
294 // Dump a few words before the first frame.
295 uintptr_t sp = backtrace->GetFrame(first)->sp - STACK_WORDS * sizeof(uint32_t);
296 dump_stack_segment(backtrace, log, scope_flags, &sp, STACK_WORDS, -1);
297
298 // Dump a few words from all successive frames.
299 // Only log the first 3 frames, put the rest in the tombstone.
300 for (size_t i = first; i <= last; i++) {
301 const backtrace_frame_data_t* frame = backtrace->GetFrame(i);
302 if (sp != frame->sp) {
303 _LOG(log, scope_flags, " ........ ........\n");
304 sp = frame->sp;
305 }
306 if (i - first == 3) {
307 scope_flags &= (~SCOPE_AT_FAULT);
308 }
309 if (i == last) {
310 dump_stack_segment(backtrace, log, scope_flags, &sp, STACK_WORDS, i);
311 if (sp < frame->sp + frame->stack_size) {
312 _LOG(log, scope_flags, " ........ ........\n");
313 }
314 } else {
315 size_t words = frame->stack_size / sizeof(uint32_t);
316 if (words == 0) {
317 words = 1;
318 } else if (words > STACK_WORDS) {
319 words = STACK_WORDS;
320 }
321 dump_stack_segment(backtrace, log, scope_flags, &sp, words, i);
322 }
323 }
Jeff Brown053b8652012-06-06 16:25:03 -0700324}
325
Christopher Ferris20303f82014-01-10 16:33:16 -0800326static void dump_backtrace_and_stack(Backtrace* backtrace, log_t* log, int scope_flags) {
327 if (backtrace->NumFrames()) {
328 _LOG(log, scope_flags, "\nbacktrace:\n");
329 dump_backtrace_to_log(backtrace, log, scope_flags, " ");
Jeff Brown053b8652012-06-06 16:25:03 -0700330
Christopher Ferris20303f82014-01-10 16:33:16 -0800331 _LOG(log, scope_flags, "\nstack:\n");
332 dump_stack(backtrace, log, scope_flags);
333 }
Jeff Brown053b8652012-06-06 16:25:03 -0700334}
335
Christopher Ferris46756822014-01-14 20:16:30 -0800336static void dump_map(log_t* log, const backtrace_map_t* map, const char* what, int scope_flags) {
337 if (map != NULL) {
338 _LOG(log, scope_flags, " %08x-%08x %c%c%c %s\n", map->start, map->end,
339 (map->flags & PROT_READ) ? 'r' : '-', (map->flags & PROT_WRITE) ? 'w' : '-',
340 (map->flags & PROT_EXEC) ? 'x' : '-', map->name.c_str());
Christopher Ferris20303f82014-01-10 16:33:16 -0800341 } else {
342 _LOG(log, scope_flags, " (no %s)\n", what);
343 }
Elliott Hughesd1420be2013-01-03 13:39:57 -0800344}
345
Christopher Ferris46756822014-01-14 20:16:30 -0800346static void dump_nearby_maps(BacktraceMap* map, log_t* log, pid_t tid, int scope_flags) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800347 scope_flags |= SCOPE_SENSITIVE;
348 siginfo_t si;
349 memset(&si, 0, sizeof(si));
350 if (ptrace(PTRACE_GETSIGINFO, tid, 0, &si)) {
351 _LOG(log, scope_flags, "cannot get siginfo for %d: %s\n", tid, strerror(errno));
352 return;
353 }
354 if (!signal_has_address(si.si_signo)) {
355 return;
356 }
357
Christopher Ferris46756822014-01-14 20:16:30 -0800358 uintptr_t addr = reinterpret_cast<uintptr_t>(si.si_addr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800359 addr &= ~0xfff; // round to 4K page boundary
360 if (addr == 0) { // null-pointer deref
361 return;
362 }
363
364 _LOG(log, scope_flags, "\nmemory map around fault addr %" PRIxPTR ":\n",
365 reinterpret_cast<uintptr_t>(si.si_addr));
366
367 // Search for a match, or for a hole where the match would be. The list
368 // is backward from the file content, so it starts at high addresses.
Christopher Ferris46756822014-01-14 20:16:30 -0800369 const backtrace_map_t* cur_map = NULL;
370 const backtrace_map_t* next_map = NULL;
371 const backtrace_map_t* prev_map = NULL;
372 for (BacktraceMap::const_iterator it = map->begin(); it != map->end(); ++it) {
373 if (addr >= it->start && addr < it->end) {
374 cur_map = &*it;
375 if (it != map->begin()) {
376 prev_map = &*(it-1);
377 }
378 if (++it != map->end()) {
379 next_map = &*it;
380 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800381 break;
Jeff Brown053b8652012-06-06 16:25:03 -0700382 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800383 }
Jeff Brown053b8652012-06-06 16:25:03 -0700384
Christopher Ferris46756822014-01-14 20:16:30 -0800385 // Show the map address in ascending order (like /proc/pid/maps).
386 dump_map(log, prev_map, "map below", scope_flags);
387 dump_map(log, cur_map, "map for address", scope_flags);
388 dump_map(log, next_map, "map above", scope_flags);
Jeff Brown053b8652012-06-06 16:25:03 -0700389}
390
Christopher Ferris20303f82014-01-10 16:33:16 -0800391static void dump_thread(
392 Backtrace* backtrace, log_t* log, int scope_flags, int* total_sleep_time_usec) {
393 wait_for_stop(backtrace->Tid(), total_sleep_time_usec);
Jeff Brown053b8652012-06-06 16:25:03 -0700394
Christopher Ferris20303f82014-01-10 16:33:16 -0800395 dump_registers(log, backtrace->Tid(), scope_flags);
396 dump_backtrace_and_stack(backtrace, log, scope_flags);
397 if (IS_AT_FAULT(scope_flags)) {
398 dump_memory_and_code(log, backtrace->Tid(), scope_flags);
Christopher Ferris46756822014-01-14 20:16:30 -0800399 dump_nearby_maps(backtrace->GetMap(), log, backtrace->Tid(), scope_flags);
Christopher Ferris20303f82014-01-10 16:33:16 -0800400 }
Jeff Brown053b8652012-06-06 16:25:03 -0700401}
402
Christopher Ferris20303f82014-01-10 16:33:16 -0800403// Return true if some thread is not detached cleanly
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700404static bool dump_sibling_thread_report(
Christopher Ferris46756822014-01-14 20:16:30 -0800405 log_t* log, pid_t pid, pid_t tid, int* total_sleep_time_usec, BacktraceMap* map) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800406 char task_path[64];
407 snprintf(task_path, sizeof(task_path), "/proc/%d/task", pid);
Jeff Brown053b8652012-06-06 16:25:03 -0700408
Christopher Ferris20303f82014-01-10 16:33:16 -0800409 DIR* d = opendir(task_path);
410 // Bail early if the task directory cannot be opened
411 if (d == NULL) {
412 XLOG("Cannot open /proc/%d/task\n", pid);
413 return false;
414 }
415
416 bool detach_failed = false;
417 struct dirent* de;
418 while ((de = readdir(d)) != NULL) {
419 // Ignore "." and ".."
420 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, "..")) {
421 continue;
Jeff Brown053b8652012-06-06 16:25:03 -0700422 }
423
Christopher Ferris20303f82014-01-10 16:33:16 -0800424 // The main thread at fault has been handled individually
425 char* end;
426 pid_t new_tid = strtoul(de->d_name, &end, 10);
427 if (*end || new_tid == tid) {
428 continue;
Jeff Brown053b8652012-06-06 16:25:03 -0700429 }
430
Christopher Ferris20303f82014-01-10 16:33:16 -0800431 // Skip this thread if cannot ptrace it
432 if (ptrace(PTRACE_ATTACH, new_tid, 0, 0) < 0) {
433 continue;
434 }
435
436 _LOG(log, 0, "--- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---\n");
437 dump_thread_info(log, pid, new_tid, 0);
438
Christopher Ferris46756822014-01-14 20:16:30 -0800439 UniquePtr<Backtrace> backtrace(Backtrace::Create(pid, new_tid, map));
Christopher Ferris20303f82014-01-10 16:33:16 -0800440 if (backtrace->Unwind(0)) {
441 dump_thread(backtrace.get(), log, 0, total_sleep_time_usec);
442 }
443
444 if (ptrace(PTRACE_DETACH, new_tid, 0, 0) != 0) {
445 LOG("ptrace detach from %d failed: %s\n", new_tid, strerror(errno));
446 detach_failed = true;
447 }
448 }
449
450 closedir(d);
451 return detach_failed;
Jeff Brown053b8652012-06-06 16:25:03 -0700452}
453
Christopher Ferris20303f82014-01-10 16:33:16 -0800454// Reads the contents of the specified log device, filters out the entries
455// that don't match the specified pid, and writes them to the tombstone file.
456//
Mark Salyzyn22b5cef2013-11-22 10:53:34 -0800457// If "tail" is set, we only print the last few lines.
458static void dump_log_file(log_t* log, pid_t pid, const char* filename,
459 unsigned int tail) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800460 bool first = true;
Mark Salyzyn22b5cef2013-11-22 10:53:34 -0800461 struct logger_list *logger_list;
Jeff Brown053b8652012-06-06 16:25:03 -0700462
Mark Salyzyn22b5cef2013-11-22 10:53:34 -0800463 logger_list = android_logger_list_open(
464 android_name_to_log_id(filename), O_RDONLY | O_NONBLOCK, tail, pid);
Jeff Brown053b8652012-06-06 16:25:03 -0700465
Mark Salyzyn22b5cef2013-11-22 10:53:34 -0800466 if (!logger_list) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800467 XLOG("Unable to open %s: %s\n", filename, strerror(errno));
468 return;
469 }
470
Mark Salyzyn22b5cef2013-11-22 10:53:34 -0800471 struct log_msg log_entry;
Christopher Ferris20303f82014-01-10 16:33:16 -0800472
473 while (true) {
Mark Salyzyn22b5cef2013-11-22 10:53:34 -0800474 ssize_t actual = android_logger_list_read(logger_list, &log_entry);
475
Christopher Ferris20303f82014-01-10 16:33:16 -0800476 if (actual < 0) {
Mark Salyzyn22b5cef2013-11-22 10:53:34 -0800477 if (actual == -EINTR) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800478 // interrupted by signal, retry
479 continue;
Mark Salyzyn22b5cef2013-11-22 10:53:34 -0800480 } else if (actual == -EAGAIN) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800481 // non-blocking EOF; we're done
482 break;
483 } else {
Mark Salyzyn22b5cef2013-11-22 10:53:34 -0800484 _LOG(log, 0, "Error while reading log: %s\n",
485 strerror(-actual));
Christopher Ferris20303f82014-01-10 16:33:16 -0800486 break;
487 }
488 } else if (actual == 0) {
Mark Salyzyn22b5cef2013-11-22 10:53:34 -0800489 _LOG(log, 0, "Got zero bytes while reading log: %s\n",
490 strerror(errno));
Christopher Ferris20303f82014-01-10 16:33:16 -0800491 break;
Jeff Brown053b8652012-06-06 16:25:03 -0700492 }
493
Christopher Ferris20303f82014-01-10 16:33:16 -0800494 // NOTE: if you XLOG something here, this will spin forever,
495 // because you will be writing as fast as you're reading. Any
496 // high-frequency debug diagnostics should just be written to
497 // the tombstone file.
Mark Salyzyn22b5cef2013-11-22 10:53:34 -0800498 struct logger_entry* entry = &log_entry.entry_v1;
Jeff Brown053b8652012-06-06 16:25:03 -0700499
Christopher Ferris20303f82014-01-10 16:33:16 -0800500 if (entry->pid != static_cast<int32_t>(pid)) {
501 // wrong pid, ignore
502 continue;
Jeff Brown053b8652012-06-06 16:25:03 -0700503 }
504
Christopher Ferris20303f82014-01-10 16:33:16 -0800505 if (first) {
Mark Salyzyn22b5cef2013-11-22 10:53:34 -0800506 _LOG(log, 0, "--------- %slog %s\n",
507 tail ? "tail end of " : "", filename);
Christopher Ferris20303f82014-01-10 16:33:16 -0800508 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";
Mark Salyzyn22b5cef2013-11-22 10:53:34 -0800519 unsigned hdr_size = log_entry.entry.hdr_size;
520 if (!hdr_size) {
521 hdr_size = sizeof(log_entry.entry_v1);
522 }
523 char* msg = (char *)log_entry.buf + hdr_size;
524 unsigned char prio = msg[0];
525 char* tag = msg + 1;
526 msg = tag + strlen(tag) + 1;
Christopher Ferris20303f82014-01-10 16:33:16 -0800527
528 // consume any trailing newlines
529 char* eatnl = msg + strlen(msg) - 1;
530 while (eatnl >= msg && *eatnl == '\n') {
531 *eatnl-- = '\0';
532 }
533
534 char prioChar = (prio < strlen(kPrioChars) ? kPrioChars[prio] : '?');
535
536 char timeBuf[32];
537 time_t sec = static_cast<time_t>(entry->sec);
538 struct tm tmBuf;
539 struct tm* ptm;
540 ptm = localtime_r(&sec, &tmBuf);
541 strftime(timeBuf, sizeof(timeBuf), "%m-%d %H:%M:%S", ptm);
542
Mark Salyzyn22b5cef2013-11-22 10:53:34 -0800543 _LOG(log, 0, "%s.%03d %5d %5d %c %-8s: %s\n",
544 timeBuf, entry->nsec / 1000000, entry->pid, entry->tid,
545 prioChar, tag, msg);
Christopher Ferris20303f82014-01-10 16:33:16 -0800546 }
Jeff Brown053b8652012-06-06 16:25:03 -0700547
Mark Salyzyn22b5cef2013-11-22 10:53:34 -0800548 android_logger_list_free(logger_list);
Jeff Brown053b8652012-06-06 16:25:03 -0700549}
550
Christopher Ferris20303f82014-01-10 16:33:16 -0800551// Dumps the logs generated by the specified pid to the tombstone, from both
552// "system" and "main" log devices. Ideally we'd interleave the output.
Mark Salyzyn22b5cef2013-11-22 10:53:34 -0800553static void dump_logs(log_t* log, pid_t pid, unsigned tail) {
554 dump_log_file(log, pid, "system", tail);
555 dump_log_file(log, pid, "main", tail);
Jeff Brown053b8652012-06-06 16:25:03 -0700556}
557
Christopher Ferris20303f82014-01-10 16:33:16 -0800558static void dump_abort_message(Backtrace* backtrace, log_t* log, uintptr_t address) {
Elliott Hughese5f8a692013-04-04 13:52:01 -0700559 if (address == 0) {
560 return;
561 }
562
563 address += sizeof(size_t); // Skip the buffer length.
564
565 char msg[512];
566 memset(msg, 0, sizeof(msg));
567 char* p = &msg[0];
568 while (p < &msg[sizeof(msg)]) {
569 uint32_t data;
Christopher Ferris20303f82014-01-10 16:33:16 -0800570 if (!backtrace->ReadWord(address, &data)) {
Elliott Hughese5f8a692013-04-04 13:52:01 -0700571 break;
572 }
573 address += sizeof(uint32_t);
574
575 if ((*p++ = (data >> 0) & 0xff) == 0) {
576 break;
577 }
578 if ((*p++ = (data >> 8) & 0xff) == 0) {
579 break;
580 }
581 if ((*p++ = (data >> 16) & 0xff) == 0) {
582 break;
583 }
584 if ((*p++ = (data >> 24) & 0xff) == 0) {
585 break;
586 }
587 }
588 msg[sizeof(msg) - 1] = '\0';
589
Christopher Tate7716aef2013-04-02 14:00:27 -0700590 _LOG(log, SCOPE_AT_FAULT, "Abort message: '%s'\n", msg);
Elliott Hughese5f8a692013-04-04 13:52:01 -0700591}
592
Christopher Ferris20303f82014-01-10 16:33:16 -0800593// Dumps all information about the specified pid to the tombstone.
Elliott Hughese5f8a692013-04-04 13:52:01 -0700594static 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 -0800595 bool dump_sibling_threads, int* total_sleep_time_usec) {
596 // don't copy log messages to tombstone unless this is a dev device
597 char value[PROPERTY_VALUE_MAX];
598 property_get("ro.debuggable", value, "0");
599 bool want_logs = (value[0] == '1');
Jeff Brown053b8652012-06-06 16:25:03 -0700600
Christopher Ferris20303f82014-01-10 16:33:16 -0800601 if (log->amfd >= 0) {
602 // Activity Manager protocol: binary 32-bit network-byte-order ints for the
603 // pid and signal number, followed by the raw text of the dump, culminating
604 // in a zero byte that marks end-of-data.
605 uint32_t datum = htonl(pid);
606 TEMP_FAILURE_RETRY( write(log->amfd, &datum, 4) );
607 datum = htonl(signal);
608 TEMP_FAILURE_RETRY( write(log->amfd, &datum, 4) );
609 }
Christopher Tateded2e5a2013-03-19 13:12:23 -0700610
Christopher Ferris20303f82014-01-10 16:33:16 -0800611 _LOG(log, SCOPE_AT_FAULT,
612 "*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***\n");
613 dump_build_info(log);
614 dump_revision_info(log);
615 dump_thread_info(log, pid, tid, SCOPE_AT_FAULT);
616 if (signal) {
617 dump_fault_addr(log, tid, signal);
618 }
Jeff Brown053b8652012-06-06 16:25:03 -0700619
Christopher Ferris46756822014-01-14 20:16:30 -0800620 BacktraceMap* map = NULL;
621 UniquePtr<Backtrace> backtrace(Backtrace::Create(pid, tid));
Christopher Ferris20303f82014-01-10 16:33:16 -0800622 if (backtrace->Unwind(0)) {
Christopher Ferris46756822014-01-14 20:16:30 -0800623 // Grab the map that was created and share it with the siblings.
624 map = backtrace->TakeMapOwnership();
625
Christopher Ferris20303f82014-01-10 16:33:16 -0800626 dump_abort_message(backtrace.get(), log, abort_msg_address);
627 dump_thread(backtrace.get(), log, SCOPE_AT_FAULT, total_sleep_time_usec);
628 }
Jeff Brown053b8652012-06-06 16:25:03 -0700629
Christopher Ferris20303f82014-01-10 16:33:16 -0800630 if (want_logs) {
Mark Salyzyn22b5cef2013-11-22 10:53:34 -0800631 dump_logs(log, pid, 5);
Christopher Ferris20303f82014-01-10 16:33:16 -0800632 }
Jeff Brown053b8652012-06-06 16:25:03 -0700633
Christopher Ferris20303f82014-01-10 16:33:16 -0800634 bool detach_failed = false;
635 if (dump_sibling_threads) {
Christopher Ferris46756822014-01-14 20:16:30 -0800636 detach_failed = dump_sibling_thread_report(log, pid, tid, total_sleep_time_usec, map);
Christopher Ferris20303f82014-01-10 16:33:16 -0800637 }
Christopher Ferris98464972014-01-06 19:16:33 -0800638
Christopher Ferris46756822014-01-14 20:16:30 -0800639 // Destroy the BacktraceMap object.
640 delete map;
Christopher Tateded2e5a2013-03-19 13:12:23 -0700641
Christopher Ferris20303f82014-01-10 16:33:16 -0800642 if (want_logs) {
Mark Salyzyn22b5cef2013-11-22 10:53:34 -0800643 dump_logs(log, pid, 0);
Christopher Ferris20303f82014-01-10 16:33:16 -0800644 }
Christopher Tateded2e5a2013-03-19 13:12:23 -0700645
Christopher Ferris20303f82014-01-10 16:33:16 -0800646 // send EOD to the Activity Manager, then wait for its ack to avoid racing ahead
647 // and killing the target out from under it
648 if (log->amfd >= 0) {
649 uint8_t eodMarker = 0;
650 TEMP_FAILURE_RETRY( write(log->amfd, &eodMarker, 1) );
651 // 3 sec timeout reading the ack; we're fine if that happens
652 TEMP_FAILURE_RETRY( read(log->amfd, &eodMarker, 1) );
653 }
654
655 return detach_failed;
Jeff Brown053b8652012-06-06 16:25:03 -0700656}
657
Christopher Ferris20303f82014-01-10 16:33:16 -0800658// find_and_open_tombstone - find an available tombstone slot, if any, of the
659// form tombstone_XX where XX is 00 to MAX_TOMBSTONES-1, inclusive. If no
660// file is available, we reuse the least-recently-modified file.
661//
662// Returns the path of the tombstone file, allocated using malloc(). Caller must free() it.
663static char* find_and_open_tombstone(int* fd) {
664 unsigned long mtime = ULONG_MAX;
665 struct stat sb;
Jeff Brown053b8652012-06-06 16:25:03 -0700666
Christopher Ferris20303f82014-01-10 16:33:16 -0800667 // XXX: Our stat.st_mtime isn't time_t. If it changes, as it probably ought
668 // to, our logic breaks. This check will generate a warning if that happens.
669 typecheck(mtime, sb.st_mtime);
Jeff Brown053b8652012-06-06 16:25:03 -0700670
Christopher Ferris20303f82014-01-10 16:33:16 -0800671 // In a single wolf-like pass, find an available slot and, in case none
672 // exist, find and record the least-recently-modified file.
673 char path[128];
674 int oldest = 0;
675 for (int i = 0; i < MAX_TOMBSTONES; i++) {
676 snprintf(path, sizeof(path), TOMBSTONE_DIR"/tombstone_%02d", i);
Jeff Brown053b8652012-06-06 16:25:03 -0700677
Christopher Ferris20303f82014-01-10 16:33:16 -0800678 if (!stat(path, &sb)) {
679 if (sb.st_mtime < mtime) {
680 oldest = i;
681 mtime = sb.st_mtime;
682 }
683 continue;
Jeff Brown053b8652012-06-06 16:25:03 -0700684 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800685 if (errno != ENOENT)
686 continue;
Jeff Brown053b8652012-06-06 16:25:03 -0700687
Christopher Ferris20303f82014-01-10 16:33:16 -0800688 *fd = open(path, O_CREAT | O_EXCL | O_WRONLY, 0600);
689 if (*fd < 0)
690 continue; // raced ?
691
Jeff Brown053b8652012-06-06 16:25:03 -0700692 fchown(*fd, AID_SYSTEM, AID_SYSTEM);
693 return strdup(path);
Christopher Ferris20303f82014-01-10 16:33:16 -0800694 }
695
696 // we didn't find an available file, so we clobber the oldest one
697 snprintf(path, sizeof(path), TOMBSTONE_DIR"/tombstone_%02d", oldest);
698 *fd = open(path, O_CREAT | O_TRUNC | O_WRONLY, 0600);
699 if (*fd < 0) {
700 LOG("failed to open tombstone file '%s': %s\n", path, strerror(errno));
701 return NULL;
702 }
703 fchown(*fd, AID_SYSTEM, AID_SYSTEM);
704 return strdup(path);
Jeff Brown053b8652012-06-06 16:25:03 -0700705}
706
Christopher Tateded2e5a2013-03-19 13:12:23 -0700707static int activity_manager_connect() {
Christopher Ferris20303f82014-01-10 16:33:16 -0800708 int amfd = socket(PF_UNIX, SOCK_STREAM, 0);
709 if (amfd >= 0) {
710 struct sockaddr_un address;
711 int err;
Christopher Tateded2e5a2013-03-19 13:12:23 -0700712
Christopher Ferris20303f82014-01-10 16:33:16 -0800713 memset(&address, 0, sizeof(address));
714 address.sun_family = AF_UNIX;
715 strncpy(address.sun_path, NCRASH_SOCKET_PATH, sizeof(address.sun_path));
716 err = TEMP_FAILURE_RETRY(connect(
717 amfd, reinterpret_cast<struct sockaddr*>(&address), sizeof(address)));
718 if (!err) {
719 struct timeval tv;
720 memset(&tv, 0, sizeof(tv));
721 tv.tv_sec = 1; // tight leash
722 err = setsockopt(amfd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
723 if (!err) {
724 tv.tv_sec = 3; // 3 seconds on handshake read
725 err = setsockopt(amfd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
726 }
Christopher Tateded2e5a2013-03-19 13:12:23 -0700727 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800728 if (err) {
729 close(amfd);
730 amfd = -1;
731 }
732 }
Christopher Tateded2e5a2013-03-19 13:12:23 -0700733
Christopher Ferris20303f82014-01-10 16:33:16 -0800734 return amfd;
Christopher Tateded2e5a2013-03-19 13:12:23 -0700735}
736
Christopher Ferris20303f82014-01-10 16:33:16 -0800737char* engrave_tombstone(
738 pid_t pid, pid_t tid, int signal, uintptr_t abort_msg_address, bool dump_sibling_threads,
739 bool quiet, bool* detach_failed, int* total_sleep_time_usec) {
740 mkdir(TOMBSTONE_DIR, 0755);
741 chown(TOMBSTONE_DIR, AID_SYSTEM, AID_SYSTEM);
Jeff Brown053b8652012-06-06 16:25:03 -0700742
Christopher Ferris20303f82014-01-10 16:33:16 -0800743 if (selinux_android_restorecon(TOMBSTONE_DIR) == -1) {
744 *detach_failed = false;
745 return NULL;
746 }
rpcraigf1186f32012-07-19 09:38:06 -0400747
Christopher Ferris20303f82014-01-10 16:33:16 -0800748 int fd;
749 char* path = find_and_open_tombstone(&fd);
750 if (!path) {
751 *detach_failed = false;
752 return NULL;
753 }
Jeff Brown053b8652012-06-06 16:25:03 -0700754
Christopher Ferris20303f82014-01-10 16:33:16 -0800755 log_t log;
756 log.tfd = fd;
757 log.amfd = activity_manager_connect();
758 log.quiet = quiet;
759 *detach_failed = dump_crash(
760 &log, pid, tid, signal, abort_msg_address, dump_sibling_threads, total_sleep_time_usec);
Jeff Brown053b8652012-06-06 16:25:03 -0700761
Christopher Ferris20303f82014-01-10 16:33:16 -0800762 close(log.amfd);
763 close(fd);
764 return path;
Jeff Brown053b8652012-06-06 16:25:03 -0700765}