blob: 5e3d729da725ada8f3a9cec6dbdf6220c25bd5b8 [file] [log] [blame]
Jeff Brown053b8652012-06-06 16:25:03 -07001/*
Mark Salyzyn706fad22013-12-12 12:21:20 -08002 * Copyright (C) 2012-2014 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
Jeff Brown053b8652012-06-06 16:25:03 -070017#include <dirent.h>
Kévin PETIT4bb47722013-12-18 16:44:24 +000018#include <errno.h>
19#include <fcntl.h>
20#include <inttypes.h>
21#include <signal.h>
22#include <stddef.h>
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
Jeff Brown053b8652012-06-06 16:25:03 -070026#include <time.h>
27#include <sys/ptrace.h>
Kévin PETIT4bb47722013-12-18 16:44:24 +000028#include <sys/socket.h>
Jeff Brown053b8652012-06-06 16:25:03 -070029#include <sys/stat.h>
Kévin PETIT4bb47722013-12-18 16:44:24 +000030#include <sys/un.h>
Jeff Brown053b8652012-06-06 16:25:03 -070031
32#include <private/android_filesystem_config.h>
33
Mark Salyzyna63f9272013-11-22 10:53:34 -080034#include <log/log.h>
Colin Cross9227bd32013-07-23 16:59:20 -070035#include <log/logger.h>
Jeff Brown053b8652012-06-06 16:25:03 -070036#include <cutils/properties.h>
37
Christopher Ferris20303f82014-01-10 16:33:16 -080038#include <backtrace/Backtrace.h>
Christopher Ferris46756822014-01-14 20:16:30 -080039#include <backtrace/BacktraceMap.h>
Jeff Brown053b8652012-06-06 16:25:03 -070040
rpcraigf1186f32012-07-19 09:38:06 -040041#include <selinux/android.h>
rpcraigf1186f32012-07-19 09:38:06 -040042
Christopher Ferris20303f82014-01-10 16:33:16 -080043#include <UniquePtr.h>
44
Jeff Brown053b8652012-06-06 16:25:03 -070045#include "machine.h"
46#include "tombstone.h"
Christopher Ferris365e4ae2013-10-02 12:26:48 -070047#include "backtrace.h"
Jeff Brown053b8652012-06-06 16:25:03 -070048
Jeff Brown053b8652012-06-06 16:25:03 -070049#define STACK_WORDS 16
50
51#define MAX_TOMBSTONES 10
52#define TOMBSTONE_DIR "/data/tombstones"
Kévin PETIT4bb47722013-12-18 16:44:24 +000053#define TOMBSTONE_TEMPLATE (TOMBSTONE_DIR"/tombstone_%02d")
Jeff Brown053b8652012-06-06 16:25:03 -070054
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
Jeff Brown053b8652012-06-06 16:25:03 -070063static bool signal_has_address(int sig) {
Christopher Ferris20303f82014-01-10 16:33:16 -080064 switch (sig) {
Jeff Brown053b8652012-06-06 16:25:03 -070065 case SIGILL:
Christopher Ferris20303f82014-01-10 16:33:16 -080066 case SIGFPE:
67 case SIGSEGV:
68 case SIGBUS:
69 return true;
70 default:
71 return false;
72 }
73}
74
75static const char* get_signame(int sig) {
76 switch(sig) {
77 case SIGILL: return "SIGILL";
78 case SIGABRT: return "SIGABRT";
79 case SIGBUS: return "SIGBUS";
80 case SIGFPE: return "SIGFPE";
81 case SIGSEGV: return "SIGSEGV";
82 case SIGPIPE: return "SIGPIPE";
83#ifdef SIGSTKFLT
84 case SIGSTKFLT: return "SIGSTKFLT";
85#endif
86 case SIGSTOP: return "SIGSTOP";
87 default: return "?";
88 }
89}
90
91static const char* get_sigcode(int signo, int code) {
92 // Try the signal-specific codes...
93 switch (signo) {
94 case SIGILL:
95 switch (code) {
Jeff Brown053b8652012-06-06 16:25:03 -070096 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";
Christopher Ferris20303f82014-01-10 16:33:16 -0800104 }
105 break;
Jeff Brown053b8652012-06-06 16:25:03 -0700106 case SIGBUS:
Christopher Ferris20303f82014-01-10 16:33:16 -0800107 switch (code) {
Jeff Brown053b8652012-06-06 16:25:03 -0700108 case BUS_ADRALN: return "BUS_ADRALN";
109 case BUS_ADRERR: return "BUS_ADRERR";
110 case BUS_OBJERR: return "BUS_OBJERR";
Christopher Ferris20303f82014-01-10 16:33:16 -0800111 }
112 break;
Jeff Brown053b8652012-06-06 16:25:03 -0700113 case SIGFPE:
Christopher Ferris20303f82014-01-10 16:33:16 -0800114 switch (code) {
Jeff Brown053b8652012-06-06 16:25:03 -0700115 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";
Christopher Ferris20303f82014-01-10 16:33:16 -0800123 }
124 break;
Jeff Brown053b8652012-06-06 16:25:03 -0700125 case SIGSEGV:
Christopher Ferris20303f82014-01-10 16:33:16 -0800126 switch (code) {
Jeff Brown053b8652012-06-06 16:25:03 -0700127 case SEGV_MAPERR: return "SEGV_MAPERR";
128 case SEGV_ACCERR: return "SEGV_ACCERR";
Christopher Ferris20303f82014-01-10 16:33:16 -0800129 }
130 break;
Elliott Hughes8f7d4432012-12-10 10:29:05 -0800131 case SIGTRAP:
Christopher Ferris20303f82014-01-10 16:33:16 -0800132 switch (code) {
Elliott Hughes8f7d4432012-12-10 10:29:05 -0800133 case TRAP_BRKPT: return "TRAP_BRKPT";
134 case TRAP_TRACE: return "TRAP_TRACE";
Christopher Ferris20303f82014-01-10 16:33:16 -0800135 }
136 break;
137 }
138 // Then the other codes...
139 switch (code) {
140 case SI_USER: return "SI_USER";
Elliott Hughes8f7d4432012-12-10 10:29:05 -0800141#if defined(SI_KERNEL)
Christopher Ferris20303f82014-01-10 16:33:16 -0800142 case SI_KERNEL: return "SI_KERNEL";
Elliott Hughes8f7d4432012-12-10 10:29:05 -0800143#endif
Christopher Ferris20303f82014-01-10 16:33:16 -0800144 case SI_QUEUE: return "SI_QUEUE";
145 case SI_TIMER: return "SI_TIMER";
146 case SI_MESGQ: return "SI_MESGQ";
Elliott Hughes8f7d4432012-12-10 10:29:05 -0800147 case SI_ASYNCIO: return "SI_ASYNCIO";
148#if defined(SI_SIGIO)
Christopher Ferris20303f82014-01-10 16:33:16 -0800149 case SI_SIGIO: return "SI_SIGIO";
Elliott Hughes8f7d4432012-12-10 10:29:05 -0800150#endif
151#if defined(SI_TKILL)
Christopher Ferris20303f82014-01-10 16:33:16 -0800152 case SI_TKILL: return "SI_TKILL";
Elliott Hughes8f7d4432012-12-10 10:29:05 -0800153#endif
Christopher Ferris20303f82014-01-10 16:33:16 -0800154 }
155 // Then give up...
156 return "?";
Jeff Brown053b8652012-06-06 16:25:03 -0700157}
158
Christopher Ferris20303f82014-01-10 16:33:16 -0800159static void dump_revision_info(log_t* log) {
160 char revision[PROPERTY_VALUE_MAX];
Ben Chengd7760c12012-09-19 16:04:01 -0700161
Christopher Ferris20303f82014-01-10 16:33:16 -0800162 property_get("ro.revision", revision, "unknown");
Ben Chengd7760c12012-09-19 16:04:01 -0700163
Christopher Ferris20303f82014-01-10 16:33:16 -0800164 _LOG(log, SCOPE_AT_FAULT, "Revision: '%s'\n", revision);
Ben Chengd7760c12012-09-19 16:04:01 -0700165}
166
Christopher Ferris20303f82014-01-10 16:33:16 -0800167static void dump_build_info(log_t* log) {
168 char fingerprint[PROPERTY_VALUE_MAX];
Jeff Brown053b8652012-06-06 16:25:03 -0700169
Christopher Ferris20303f82014-01-10 16:33:16 -0800170 property_get("ro.build.fingerprint", fingerprint, "unknown");
Jeff Brown053b8652012-06-06 16:25:03 -0700171
Christopher Ferris20303f82014-01-10 16:33:16 -0800172 _LOG(log, SCOPE_AT_FAULT, "Build fingerprint: '%s'\n", fingerprint);
Jeff Brown053b8652012-06-06 16:25:03 -0700173}
174
Christopher Ferris20303f82014-01-10 16:33:16 -0800175static void dump_fault_addr(log_t* log, pid_t tid, int sig) {
176 siginfo_t si;
Jeff Brown053b8652012-06-06 16:25:03 -0700177
Christopher Ferris20303f82014-01-10 16:33:16 -0800178 memset(&si, 0, sizeof(si));
179 if (ptrace(PTRACE_GETSIGINFO, tid, 0, &si)){
180 _LOG(log, SCOPE_AT_FAULT, "cannot get siginfo: %s\n", strerror(errno));
181 } else if (signal_has_address(sig)) {
Pavel Chupinc6c194c2013-11-21 23:17:20 +0400182 _LOG(log, SCOPE_AT_FAULT, "signal %d (%s), code %d (%s), fault addr %" PRIPTR "\n",
Christopher Ferris20303f82014-01-10 16:33:16 -0800183 sig, get_signame(sig), si.si_code, get_sigcode(sig, si.si_code),
Pavel Chupinc6c194c2013-11-21 23:17:20 +0400184 reinterpret_cast<uintptr_t>(si.si_addr));
Christopher Ferris20303f82014-01-10 16:33:16 -0800185 } else {
186 _LOG(log, SCOPE_AT_FAULT, "signal %d (%s), code %d (%s), fault addr --------\n",
187 sig, get_signame(sig), si.si_code, get_sigcode(sig, si.si_code));
188 }
Jeff Brown053b8652012-06-06 16:25:03 -0700189}
190
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700191static void dump_thread_info(log_t* log, pid_t pid, pid_t tid, int scope_flags) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800192 char path[64];
193 char threadnamebuf[1024];
194 char* threadname = NULL;
195 FILE *fp;
Jeff Brown053b8652012-06-06 16:25:03 -0700196
Christopher Ferris20303f82014-01-10 16:33:16 -0800197 snprintf(path, sizeof(path), "/proc/%d/comm", tid);
198 if ((fp = fopen(path, "r"))) {
199 threadname = fgets(threadnamebuf, sizeof(threadnamebuf), fp);
200 fclose(fp);
201 if (threadname) {
202 size_t len = strlen(threadname);
203 if (len && threadname[len - 1] == '\n') {
204 threadname[len - 1] = '\0';
205 }
206 }
207 }
208
209 if (IS_AT_FAULT(scope_flags)) {
210 char procnamebuf[1024];
211 char* procname = NULL;
212
213 snprintf(path, sizeof(path), "/proc/%d/cmdline", pid);
Jeff Brown053b8652012-06-06 16:25:03 -0700214 if ((fp = fopen(path, "r"))) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800215 procname = fgets(procnamebuf, sizeof(procnamebuf), fp);
216 fclose(fp);
Jeff Brown053b8652012-06-06 16:25:03 -0700217 }
218
Christopher Ferris20303f82014-01-10 16:33:16 -0800219 _LOG(log, SCOPE_AT_FAULT, "pid: %d, tid: %d, name: %s >>> %s <<<\n", pid, tid,
220 threadname ? threadname : "UNKNOWN", procname ? procname : "UNKNOWN");
221 } else {
222 _LOG(log, 0, "pid: %d, tid: %d, name: %s\n", pid, tid, threadname ? threadname : "UNKNOWN");
223 }
224}
Jeff Brown053b8652012-06-06 16:25:03 -0700225
Christopher Ferris20303f82014-01-10 16:33:16 -0800226static void dump_stack_segment(
227 Backtrace* backtrace, log_t* log, int scope_flags, uintptr_t* sp, size_t words, int label) {
228 for (size_t i = 0; i < words; i++) {
Pavel Chupinc6c194c2013-11-21 23:17:20 +0400229 word_t stack_content;
Christopher Ferris20303f82014-01-10 16:33:16 -0800230 if (!backtrace->ReadWord(*sp, &stack_content)) {
231 break;
232 }
233
Christopher Ferris46756822014-01-14 20:16:30 -0800234 const backtrace_map_t* map = backtrace->FindMap(stack_content);
235 const char* map_name;
236 if (!map) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800237 map_name = "";
Christopher Ferris46756822014-01-14 20:16:30 -0800238 } else {
239 map_name = map->name.c_str();
Christopher Ferris20303f82014-01-10 16:33:16 -0800240 }
241 uintptr_t offset = 0;
242 std::string func_name(backtrace->GetFunctionName(stack_content, &offset));
243 if (!func_name.empty()) {
244 if (!i && label >= 0) {
245 if (offset) {
Elliott Hughesf7b4b512014-01-31 23:13:55 -0800246 _LOG(log, scope_flags, " #%02d %" PRIPTR " %" PRIPTR " %s (%s+%" PRIuPTR ")\n",
Christopher Ferris20303f82014-01-10 16:33:16 -0800247 label, *sp, stack_content, map_name, func_name.c_str(), offset);
248 } else {
Pavel Chupinc6c194c2013-11-21 23:17:20 +0400249 _LOG(log, scope_flags, " #%02d %" PRIPTR " %" PRIPTR " %s (%s)\n",
Christopher Ferris20303f82014-01-10 16:33:16 -0800250 label, *sp, stack_content, map_name, func_name.c_str());
Jeff Brown053b8652012-06-06 16:25:03 -0700251 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800252 } else {
253 if (offset) {
Elliott Hughesf7b4b512014-01-31 23:13:55 -0800254 _LOG(log, scope_flags, " %" PRIPTR " %" PRIPTR " %s (%s+%" PRIuPTR ")\n",
Christopher Ferris20303f82014-01-10 16:33:16 -0800255 *sp, stack_content, map_name, func_name.c_str(), offset);
256 } else {
Pavel Chupinc6c194c2013-11-21 23:17:20 +0400257 _LOG(log, scope_flags, " %" PRIPTR " %" PRIPTR " %s (%s)\n",
Christopher Ferris20303f82014-01-10 16:33:16 -0800258 *sp, stack_content, map_name, func_name.c_str());
259 }
260 }
Jeff Brown053b8652012-06-06 16:25:03 -0700261 } else {
Christopher Ferris20303f82014-01-10 16:33:16 -0800262 if (!i && label >= 0) {
Pavel Chupinc6c194c2013-11-21 23:17:20 +0400263 _LOG(log, scope_flags, " #%02d %" PRIPTR " %" PRIPTR " %s\n",
Christopher Ferris20303f82014-01-10 16:33:16 -0800264 label, *sp, stack_content, map_name);
265 } else {
Pavel Chupinc6c194c2013-11-21 23:17:20 +0400266 _LOG(log, scope_flags, " %" PRIPTR " %" PRIPTR " %s\n",
Christopher Ferris20303f82014-01-10 16:33:16 -0800267 *sp, stack_content, map_name);
268 }
Jeff Brown053b8652012-06-06 16:25:03 -0700269 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800270
Pavel Chupinc6c194c2013-11-21 23:17:20 +0400271 *sp += sizeof(word_t);
Christopher Ferris20303f82014-01-10 16:33:16 -0800272 }
Jeff Brown053b8652012-06-06 16:25:03 -0700273}
274
Christopher Ferris20303f82014-01-10 16:33:16 -0800275static void dump_stack(Backtrace* backtrace, log_t* log, int scope_flags) {
276 size_t first = 0, last;
277 for (size_t i = 0; i < backtrace->NumFrames(); i++) {
278 const backtrace_frame_data_t* frame = backtrace->GetFrame(i);
279 if (frame->sp) {
280 if (!first) {
281 first = i+1;
282 }
283 last = i;
Jeff Brown053b8652012-06-06 16:25:03 -0700284 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800285 }
286 if (!first) {
287 return;
288 }
289 first--;
290
291 scope_flags |= SCOPE_SENSITIVE;
292
293 // Dump a few words before the first frame.
Pavel Chupinc6c194c2013-11-21 23:17:20 +0400294 word_t sp = backtrace->GetFrame(first)->sp - STACK_WORDS * sizeof(word_t);
Christopher Ferris20303f82014-01-10 16:33:16 -0800295 dump_stack_segment(backtrace, log, scope_flags, &sp, STACK_WORDS, -1);
296
297 // Dump a few words from all successive frames.
298 // Only log the first 3 frames, put the rest in the tombstone.
299 for (size_t i = first; i <= last; i++) {
300 const backtrace_frame_data_t* frame = backtrace->GetFrame(i);
301 if (sp != frame->sp) {
302 _LOG(log, scope_flags, " ........ ........\n");
303 sp = frame->sp;
304 }
305 if (i - first == 3) {
306 scope_flags &= (~SCOPE_AT_FAULT);
307 }
308 if (i == last) {
309 dump_stack_segment(backtrace, log, scope_flags, &sp, STACK_WORDS, i);
310 if (sp < frame->sp + frame->stack_size) {
311 _LOG(log, scope_flags, " ........ ........\n");
312 }
313 } else {
Pavel Chupinc6c194c2013-11-21 23:17:20 +0400314 size_t words = frame->stack_size / sizeof(word_t);
Christopher Ferris20303f82014-01-10 16:33:16 -0800315 if (words == 0) {
316 words = 1;
317 } else if (words > STACK_WORDS) {
318 words = STACK_WORDS;
319 }
320 dump_stack_segment(backtrace, log, scope_flags, &sp, words, i);
321 }
322 }
Jeff Brown053b8652012-06-06 16:25:03 -0700323}
324
Christopher Ferris20303f82014-01-10 16:33:16 -0800325static void dump_backtrace_and_stack(Backtrace* backtrace, log_t* log, int scope_flags) {
326 if (backtrace->NumFrames()) {
327 _LOG(log, scope_flags, "\nbacktrace:\n");
328 dump_backtrace_to_log(backtrace, log, scope_flags, " ");
Jeff Brown053b8652012-06-06 16:25:03 -0700329
Christopher Ferris20303f82014-01-10 16:33:16 -0800330 _LOG(log, scope_flags, "\nstack:\n");
331 dump_stack(backtrace, log, scope_flags);
332 }
Jeff Brown053b8652012-06-06 16:25:03 -0700333}
334
Christopher Ferris46756822014-01-14 20:16:30 -0800335static void dump_map(log_t* log, const backtrace_map_t* map, const char* what, int scope_flags) {
336 if (map != NULL) {
Pavel Chupinc6c194c2013-11-21 23:17:20 +0400337 _LOG(log, scope_flags, " %" PRIPTR "-%" PRIPTR " %c%c%c %s\n", map->start, map->end,
Christopher Ferris46756822014-01-14 20:16:30 -0800338 (map->flags & PROT_READ) ? 'r' : '-', (map->flags & PROT_WRITE) ? 'w' : '-',
339 (map->flags & PROT_EXEC) ? 'x' : '-', map->name.c_str());
Christopher Ferris20303f82014-01-10 16:33:16 -0800340 } else {
341 _LOG(log, scope_flags, " (no %s)\n", what);
342 }
Elliott Hughesd1420be2013-01-03 13:39:57 -0800343}
344
Christopher Ferris46756822014-01-14 20:16:30 -0800345static void dump_nearby_maps(BacktraceMap* map, log_t* log, pid_t tid, int scope_flags) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800346 scope_flags |= SCOPE_SENSITIVE;
347 siginfo_t si;
348 memset(&si, 0, sizeof(si));
349 if (ptrace(PTRACE_GETSIGINFO, tid, 0, &si)) {
350 _LOG(log, scope_flags, "cannot get siginfo for %d: %s\n", tid, strerror(errno));
351 return;
352 }
353 if (!signal_has_address(si.si_signo)) {
354 return;
355 }
356
Christopher Ferris46756822014-01-14 20:16:30 -0800357 uintptr_t addr = reinterpret_cast<uintptr_t>(si.si_addr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800358 addr &= ~0xfff; // round to 4K page boundary
359 if (addr == 0) { // null-pointer deref
360 return;
361 }
362
Kévin PETITabc60c22013-12-19 12:36:59 +0000363 _LOG(log, scope_flags, "\nmemory map around fault addr %" PRIPTR ":\n",
Christopher Ferris20303f82014-01-10 16:33:16 -0800364 reinterpret_cast<uintptr_t>(si.si_addr));
365
366 // Search for a match, or for a hole where the match would be. The list
367 // is backward from the file content, so it starts at high addresses.
Christopher Ferris46756822014-01-14 20:16:30 -0800368 const backtrace_map_t* cur_map = NULL;
369 const backtrace_map_t* next_map = NULL;
370 const backtrace_map_t* prev_map = NULL;
371 for (BacktraceMap::const_iterator it = map->begin(); it != map->end(); ++it) {
372 if (addr >= it->start && addr < it->end) {
373 cur_map = &*it;
374 if (it != map->begin()) {
375 prev_map = &*(it-1);
376 }
377 if (++it != map->end()) {
378 next_map = &*it;
379 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800380 break;
Jeff Brown053b8652012-06-06 16:25:03 -0700381 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800382 }
Jeff Brown053b8652012-06-06 16:25:03 -0700383
Christopher Ferris46756822014-01-14 20:16:30 -0800384 // Show the map address in ascending order (like /proc/pid/maps).
385 dump_map(log, prev_map, "map below", scope_flags);
386 dump_map(log, cur_map, "map for address", scope_flags);
387 dump_map(log, next_map, "map above", scope_flags);
Jeff Brown053b8652012-06-06 16:25:03 -0700388}
389
Christopher Ferris20303f82014-01-10 16:33:16 -0800390static void dump_thread(
391 Backtrace* backtrace, log_t* log, int scope_flags, int* total_sleep_time_usec) {
392 wait_for_stop(backtrace->Tid(), total_sleep_time_usec);
Jeff Brown053b8652012-06-06 16:25:03 -0700393
Christopher Ferris20303f82014-01-10 16:33:16 -0800394 dump_registers(log, backtrace->Tid(), scope_flags);
395 dump_backtrace_and_stack(backtrace, log, scope_flags);
396 if (IS_AT_FAULT(scope_flags)) {
397 dump_memory_and_code(log, backtrace->Tid(), scope_flags);
Christopher Ferris46756822014-01-14 20:16:30 -0800398 dump_nearby_maps(backtrace->GetMap(), log, backtrace->Tid(), scope_flags);
Christopher Ferris20303f82014-01-10 16:33:16 -0800399 }
Jeff Brown053b8652012-06-06 16:25:03 -0700400}
401
Christopher Ferris20303f82014-01-10 16:33:16 -0800402// Return true if some thread is not detached cleanly
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700403static bool dump_sibling_thread_report(
Christopher Ferris46756822014-01-14 20:16:30 -0800404 log_t* log, pid_t pid, pid_t tid, int* total_sleep_time_usec, BacktraceMap* map) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800405 char task_path[64];
406 snprintf(task_path, sizeof(task_path), "/proc/%d/task", pid);
Jeff Brown053b8652012-06-06 16:25:03 -0700407
Christopher Ferris20303f82014-01-10 16:33:16 -0800408 DIR* d = opendir(task_path);
409 // Bail early if the task directory cannot be opened
410 if (d == NULL) {
411 XLOG("Cannot open /proc/%d/task\n", pid);
412 return false;
413 }
414
415 bool detach_failed = false;
416 struct dirent* de;
417 while ((de = readdir(d)) != NULL) {
418 // Ignore "." and ".."
419 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, "..")) {
420 continue;
Jeff Brown053b8652012-06-06 16:25:03 -0700421 }
422
Christopher Ferris20303f82014-01-10 16:33:16 -0800423 // The main thread at fault has been handled individually
424 char* end;
425 pid_t new_tid = strtoul(de->d_name, &end, 10);
426 if (*end || new_tid == tid) {
427 continue;
Jeff Brown053b8652012-06-06 16:25:03 -0700428 }
429
Christopher Ferris20303f82014-01-10 16:33:16 -0800430 // Skip this thread if cannot ptrace it
431 if (ptrace(PTRACE_ATTACH, new_tid, 0, 0) < 0) {
432 continue;
433 }
434
435 _LOG(log, 0, "--- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---\n");
436 dump_thread_info(log, pid, new_tid, 0);
437
Christopher Ferris46756822014-01-14 20:16:30 -0800438 UniquePtr<Backtrace> backtrace(Backtrace::Create(pid, new_tid, map));
Christopher Ferris20303f82014-01-10 16:33:16 -0800439 if (backtrace->Unwind(0)) {
440 dump_thread(backtrace.get(), log, 0, total_sleep_time_usec);
441 }
442
443 if (ptrace(PTRACE_DETACH, new_tid, 0, 0) != 0) {
444 LOG("ptrace detach from %d failed: %s\n", new_tid, strerror(errno));
445 detach_failed = true;
446 }
447 }
448
449 closedir(d);
450 return detach_failed;
Jeff Brown053b8652012-06-06 16:25:03 -0700451}
452
Christopher Ferris20303f82014-01-10 16:33:16 -0800453// Reads the contents of the specified log device, filters out the entries
454// that don't match the specified pid, and writes them to the tombstone file.
455//
Christopher Ferris4167d7e2014-01-13 18:12:04 -0800456// If "tail" is non-zero, log the last "tail" number of lines.
457static void dump_log_file(
458 log_t* log, pid_t pid, const char* filename, unsigned int tail) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800459 bool first = true;
Christopher Ferris4167d7e2014-01-13 18:12:04 -0800460 struct logger_list* logger_list;
Jeff Brown053b8652012-06-06 16:25:03 -0700461
Christopher Ferris4167d7e2014-01-13 18:12:04 -0800462 logger_list = android_logger_list_open(
463 android_name_to_log_id(filename), O_RDONLY | O_NONBLOCK, tail, pid);
Jeff Brown053b8652012-06-06 16:25:03 -0700464
Christopher Ferris4167d7e2014-01-13 18:12:04 -0800465 if (!logger_list) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800466 XLOG("Unable to open %s: %s\n", filename, strerror(errno));
467 return;
468 }
469
Christopher Ferris4167d7e2014-01-13 18:12:04 -0800470 struct log_msg log_entry;
Christopher Ferris20303f82014-01-10 16:33:16 -0800471
472 while (true) {
Christopher Ferris4167d7e2014-01-13 18:12:04 -0800473 ssize_t actual = android_logger_list_read(logger_list, &log_entry);
474 struct logger_entry* entry;
475
Christopher Ferris20303f82014-01-10 16:33:16 -0800476 if (actual < 0) {
Christopher Ferris4167d7e2014-01-13 18:12:04 -0800477 if (actual == -EINTR) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800478 // interrupted by signal, retry
479 continue;
Christopher Ferris4167d7e2014-01-13 18:12:04 -0800480 } else if (actual == -EAGAIN) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800481 // non-blocking EOF; we're done
482 break;
483 } else {
Christopher Ferris4167d7e2014-01-13 18:12:04 -0800484 _LOG(log, 0, "Error while reading log: %s\n", strerror(-actual));
Christopher Ferris20303f82014-01-10 16:33:16 -0800485 break;
486 }
487 } else if (actual == 0) {
Mark Salyzyn22b5cef2013-11-22 10:53:34 -0800488 _LOG(log, 0, "Got zero bytes while reading log: %s\n",
489 strerror(errno));
Christopher Ferris20303f82014-01-10 16:33:16 -0800490 break;
Jeff Brown053b8652012-06-06 16:25:03 -0700491 }
492
Christopher Ferris20303f82014-01-10 16:33:16 -0800493 // NOTE: if you XLOG something here, this will spin forever,
494 // because you will be writing as fast as you're reading. Any
495 // high-frequency debug diagnostics should just be written to
496 // the tombstone file.
Jeff Brown053b8652012-06-06 16:25:03 -0700497
Christopher Ferris4167d7e2014-01-13 18:12:04 -0800498 entry = &log_entry.entry_v1;
Mark Salyzyna63f9272013-11-22 10:53:34 -0800499
Christopher Ferris20303f82014-01-10 16:33:16 -0800500 if (first) {
Christopher Ferris4167d7e2014-01-13 18:12:04 -0800501 _LOG(log, 0, "--------- %slog %s\n", tail ? "tail end of " : "", filename);
Christopher Ferris20303f82014-01-10 16:33:16 -0800502 first = false;
Jeff Brown053b8652012-06-06 16:25:03 -0700503 }
504
Christopher Ferris20303f82014-01-10 16:33:16 -0800505 // Msg format is: <priority:1><tag:N>\0<message:N>\0
506 //
507 // We want to display it in the same format as "logcat -v threadtime"
508 // (although in this case the pid is redundant).
Christopher Ferris20303f82014-01-10 16:33:16 -0800509 static const char* kPrioChars = "!.VDIWEFS";
Christopher Ferris4167d7e2014-01-13 18:12:04 -0800510 unsigned hdr_size = log_entry.entry.hdr_size;
511 if (!hdr_size) {
512 hdr_size = sizeof(log_entry.entry_v1);
513 }
514 char* msg = reinterpret_cast<char*>(log_entry.buf) + hdr_size;
515 unsigned char prio = msg[0];
516 char* tag = msg + 1;
517 msg = tag + strlen(tag) + 1;
Christopher Ferris20303f82014-01-10 16:33:16 -0800518
519 // consume any trailing newlines
Mark Salyzyn706fad22013-12-12 12:21:20 -0800520 char* nl = msg + strlen(msg) - 1;
521 while (nl >= msg && *nl == '\n') {
Mark Salyzyn8e4cdc12014-01-28 13:51:46 -0800522 *nl-- = '\0';
Christopher Ferris20303f82014-01-10 16:33:16 -0800523 }
524
525 char prioChar = (prio < strlen(kPrioChars) ? kPrioChars[prio] : '?');
526
527 char timeBuf[32];
528 time_t sec = static_cast<time_t>(entry->sec);
529 struct tm tmBuf;
530 struct tm* ptm;
531 ptm = localtime_r(&sec, &tmBuf);
532 strftime(timeBuf, sizeof(timeBuf), "%m-%d %H:%M:%S", ptm);
533
Mark Salyzyn706fad22013-12-12 12:21:20 -0800534 // Look for line breaks ('\n') and display each text line
535 // on a separate line, prefixed with the header, like logcat does.
536 do {
Mark Salyzynfca0bd12013-12-12 12:21:20 -0800537 nl = strchr(msg, '\n');
538 if (nl) {
539 *nl = '\0';
540 ++nl;
541 }
Mark Salyzyn706fad22013-12-12 12:21:20 -0800542
Mark Salyzynfca0bd12013-12-12 12:21:20 -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);
Mark Salyzyn706fad22013-12-12 12:21:20 -0800546 } while ((msg = nl));
Christopher Ferris20303f82014-01-10 16:33:16 -0800547 }
Jeff Brown053b8652012-06-06 16:25:03 -0700548
Christopher Ferris4167d7e2014-01-13 18:12:04 -0800549 android_logger_list_free(logger_list);
Jeff Brown053b8652012-06-06 16:25:03 -0700550}
551
Christopher Ferris20303f82014-01-10 16:33:16 -0800552// Dumps the logs generated by the specified pid to the tombstone, from both
553// "system" and "main" log devices. Ideally we'd interleave the output.
Christopher Ferris4167d7e2014-01-13 18:12:04 -0800554static void dump_logs(log_t* log, pid_t pid, unsigned int tail) {
555 dump_log_file(log, pid, "system", tail);
556 dump_log_file(log, pid, "main", tail);
Jeff Brown053b8652012-06-06 16:25:03 -0700557}
558
Christopher Ferris20303f82014-01-10 16:33:16 -0800559static void dump_abort_message(Backtrace* backtrace, log_t* log, uintptr_t address) {
Elliott Hughese5f8a692013-04-04 13:52:01 -0700560 if (address == 0) {
561 return;
562 }
563
564 address += sizeof(size_t); // Skip the buffer length.
565
566 char msg[512];
567 memset(msg, 0, sizeof(msg));
568 char* p = &msg[0];
569 while (p < &msg[sizeof(msg)]) {
Pavel Chupinc6c194c2013-11-21 23:17:20 +0400570 word_t data;
571 size_t len = sizeof(word_t);
Christopher Ferris20303f82014-01-10 16:33:16 -0800572 if (!backtrace->ReadWord(address, &data)) {
Elliott Hughese5f8a692013-04-04 13:52:01 -0700573 break;
574 }
Pavel Chupinc6c194c2013-11-21 23:17:20 +0400575 address += sizeof(word_t);
Elliott Hughese5f8a692013-04-04 13:52:01 -0700576
Pavel Chupinc6c194c2013-11-21 23:17:20 +0400577 while (len > 0 && (*p++ = (data >> (sizeof(word_t) - len) * 8) & 0xff) != 0)
578 len--;
Elliott Hughese5f8a692013-04-04 13:52:01 -0700579 }
580 msg[sizeof(msg) - 1] = '\0';
581
Christopher Tate7716aef2013-04-02 14:00:27 -0700582 _LOG(log, SCOPE_AT_FAULT, "Abort message: '%s'\n", msg);
Elliott Hughese5f8a692013-04-04 13:52:01 -0700583}
584
Christopher Ferris20303f82014-01-10 16:33:16 -0800585// Dumps all information about the specified pid to the tombstone.
Elliott Hughese5f8a692013-04-04 13:52:01 -0700586static 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 -0800587 bool dump_sibling_threads, int* total_sleep_time_usec) {
588 // don't copy log messages to tombstone unless this is a dev device
589 char value[PROPERTY_VALUE_MAX];
590 property_get("ro.debuggable", value, "0");
591 bool want_logs = (value[0] == '1');
Jeff Brown053b8652012-06-06 16:25:03 -0700592
Christopher Ferris20303f82014-01-10 16:33:16 -0800593 if (log->amfd >= 0) {
594 // Activity Manager protocol: binary 32-bit network-byte-order ints for the
595 // pid and signal number, followed by the raw text of the dump, culminating
596 // in a zero byte that marks end-of-data.
597 uint32_t datum = htonl(pid);
598 TEMP_FAILURE_RETRY( write(log->amfd, &datum, 4) );
599 datum = htonl(signal);
600 TEMP_FAILURE_RETRY( write(log->amfd, &datum, 4) );
601 }
Christopher Tateded2e5a2013-03-19 13:12:23 -0700602
Christopher Ferris20303f82014-01-10 16:33:16 -0800603 _LOG(log, SCOPE_AT_FAULT,
604 "*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***\n");
605 dump_build_info(log);
606 dump_revision_info(log);
607 dump_thread_info(log, pid, tid, SCOPE_AT_FAULT);
608 if (signal) {
609 dump_fault_addr(log, tid, signal);
610 }
Jeff Brown053b8652012-06-06 16:25:03 -0700611
Christopher Ferrisdf290612014-01-22 19:21:07 -0800612 UniquePtr<BacktraceMap> map(BacktraceMap::Create(pid));
613 UniquePtr<Backtrace> backtrace(Backtrace::Create(pid, tid, map.get()));
Christopher Ferris20303f82014-01-10 16:33:16 -0800614 if (backtrace->Unwind(0)) {
615 dump_abort_message(backtrace.get(), log, abort_msg_address);
616 dump_thread(backtrace.get(), log, SCOPE_AT_FAULT, total_sleep_time_usec);
617 }
Jeff Brown053b8652012-06-06 16:25:03 -0700618
Christopher Ferris20303f82014-01-10 16:33:16 -0800619 if (want_logs) {
Christopher Ferris4167d7e2014-01-13 18:12:04 -0800620 dump_logs(log, pid, 5);
Christopher Ferris20303f82014-01-10 16:33:16 -0800621 }
Jeff Brown053b8652012-06-06 16:25:03 -0700622
Christopher Ferris20303f82014-01-10 16:33:16 -0800623 bool detach_failed = false;
624 if (dump_sibling_threads) {
Christopher Ferrisdf290612014-01-22 19:21:07 -0800625 detach_failed = dump_sibling_thread_report(log, pid, tid, total_sleep_time_usec, map.get());
Christopher Ferris20303f82014-01-10 16:33:16 -0800626 }
Christopher Ferris98464972014-01-06 19:16:33 -0800627
Christopher Ferris20303f82014-01-10 16:33:16 -0800628 if (want_logs) {
Christopher Ferris4167d7e2014-01-13 18:12:04 -0800629 dump_logs(log, pid, 0);
Christopher Ferris20303f82014-01-10 16:33:16 -0800630 }
Christopher Tateded2e5a2013-03-19 13:12:23 -0700631
Christopher Ferris20303f82014-01-10 16:33:16 -0800632 // send EOD to the Activity Manager, then wait for its ack to avoid racing ahead
633 // and killing the target out from under it
634 if (log->amfd >= 0) {
635 uint8_t eodMarker = 0;
636 TEMP_FAILURE_RETRY( write(log->amfd, &eodMarker, 1) );
637 // 3 sec timeout reading the ack; we're fine if that happens
638 TEMP_FAILURE_RETRY( read(log->amfd, &eodMarker, 1) );
639 }
640
641 return detach_failed;
Jeff Brown053b8652012-06-06 16:25:03 -0700642}
643
Christopher Ferris20303f82014-01-10 16:33:16 -0800644// find_and_open_tombstone - find an available tombstone slot, if any, of the
645// form tombstone_XX where XX is 00 to MAX_TOMBSTONES-1, inclusive. If no
646// file is available, we reuse the least-recently-modified file.
647//
648// Returns the path of the tombstone file, allocated using malloc(). Caller must free() it.
649static char* find_and_open_tombstone(int* fd) {
Kévin PETITabc60c22013-12-19 12:36:59 +0000650#ifdef __aarch64__
651 long mtime = LONG_MAX;
652#else
Christopher Ferris20303f82014-01-10 16:33:16 -0800653 unsigned long mtime = ULONG_MAX;
Kévin PETITabc60c22013-12-19 12:36:59 +0000654#endif
Christopher Ferris20303f82014-01-10 16:33:16 -0800655 struct stat sb;
Jeff Brown053b8652012-06-06 16:25:03 -0700656
Christopher Ferris20303f82014-01-10 16:33:16 -0800657 // XXX: Our stat.st_mtime isn't time_t. If it changes, as it probably ought
658 // to, our logic breaks. This check will generate a warning if that happens.
659 typecheck(mtime, sb.st_mtime);
Jeff Brown053b8652012-06-06 16:25:03 -0700660
Christopher Ferris20303f82014-01-10 16:33:16 -0800661 // In a single wolf-like pass, find an available slot and, in case none
662 // exist, find and record the least-recently-modified file.
663 char path[128];
664 int oldest = 0;
665 for (int i = 0; i < MAX_TOMBSTONES; i++) {
Kévin PETIT4bb47722013-12-18 16:44:24 +0000666 snprintf(path, sizeof(path), TOMBSTONE_TEMPLATE, i);
Jeff Brown053b8652012-06-06 16:25:03 -0700667
Christopher Ferris20303f82014-01-10 16:33:16 -0800668 if (!stat(path, &sb)) {
669 if (sb.st_mtime < mtime) {
670 oldest = i;
671 mtime = sb.st_mtime;
672 }
673 continue;
Jeff Brown053b8652012-06-06 16:25:03 -0700674 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800675 if (errno != ENOENT)
676 continue;
Jeff Brown053b8652012-06-06 16:25:03 -0700677
Christopher Ferris20303f82014-01-10 16:33:16 -0800678 *fd = open(path, O_CREAT | O_EXCL | O_WRONLY, 0600);
679 if (*fd < 0)
680 continue; // raced ?
681
Jeff Brown053b8652012-06-06 16:25:03 -0700682 fchown(*fd, AID_SYSTEM, AID_SYSTEM);
683 return strdup(path);
Christopher Ferris20303f82014-01-10 16:33:16 -0800684 }
685
686 // we didn't find an available file, so we clobber the oldest one
Kévin PETIT4bb47722013-12-18 16:44:24 +0000687 snprintf(path, sizeof(path), TOMBSTONE_TEMPLATE, oldest);
Christopher Ferris20303f82014-01-10 16:33:16 -0800688 *fd = open(path, O_CREAT | O_TRUNC | O_WRONLY, 0600);
689 if (*fd < 0) {
690 LOG("failed to open tombstone file '%s': %s\n", path, strerror(errno));
691 return NULL;
692 }
693 fchown(*fd, AID_SYSTEM, AID_SYSTEM);
694 return strdup(path);
Jeff Brown053b8652012-06-06 16:25:03 -0700695}
696
Christopher Tateded2e5a2013-03-19 13:12:23 -0700697static int activity_manager_connect() {
Christopher Ferris20303f82014-01-10 16:33:16 -0800698 int amfd = socket(PF_UNIX, SOCK_STREAM, 0);
699 if (amfd >= 0) {
700 struct sockaddr_un address;
701 int err;
Christopher Tateded2e5a2013-03-19 13:12:23 -0700702
Christopher Ferris20303f82014-01-10 16:33:16 -0800703 memset(&address, 0, sizeof(address));
704 address.sun_family = AF_UNIX;
705 strncpy(address.sun_path, NCRASH_SOCKET_PATH, sizeof(address.sun_path));
706 err = TEMP_FAILURE_RETRY(connect(
707 amfd, reinterpret_cast<struct sockaddr*>(&address), sizeof(address)));
708 if (!err) {
709 struct timeval tv;
710 memset(&tv, 0, sizeof(tv));
711 tv.tv_sec = 1; // tight leash
712 err = setsockopt(amfd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
713 if (!err) {
714 tv.tv_sec = 3; // 3 seconds on handshake read
715 err = setsockopt(amfd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
716 }
Christopher Tateded2e5a2013-03-19 13:12:23 -0700717 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800718 if (err) {
719 close(amfd);
720 amfd = -1;
721 }
722 }
Christopher Tateded2e5a2013-03-19 13:12:23 -0700723
Christopher Ferris20303f82014-01-10 16:33:16 -0800724 return amfd;
Christopher Tateded2e5a2013-03-19 13:12:23 -0700725}
726
Christopher Ferris20303f82014-01-10 16:33:16 -0800727char* engrave_tombstone(
728 pid_t pid, pid_t tid, int signal, uintptr_t abort_msg_address, bool dump_sibling_threads,
729 bool quiet, bool* detach_failed, int* total_sleep_time_usec) {
Kévin PETIT4bb47722013-12-18 16:44:24 +0000730 if ((mkdir(TOMBSTONE_DIR, 0755) == -1) && (errno != EEXIST)) {
731 LOG("failed to create %s: %s\n", TOMBSTONE_DIR, strerror(errno));
732 }
733
734 if (chown(TOMBSTONE_DIR, AID_SYSTEM, AID_SYSTEM) == -1) {
735 LOG("failed to change ownership of %s: %s\n", TOMBSTONE_DIR, strerror(errno));
736 }
Jeff Brown053b8652012-06-06 16:25:03 -0700737
Christopher Ferris20303f82014-01-10 16:33:16 -0800738 if (selinux_android_restorecon(TOMBSTONE_DIR) == -1) {
739 *detach_failed = false;
740 return NULL;
741 }
rpcraigf1186f32012-07-19 09:38:06 -0400742
Christopher Ferris20303f82014-01-10 16:33:16 -0800743 int fd;
744 char* path = find_and_open_tombstone(&fd);
745 if (!path) {
746 *detach_failed = false;
747 return NULL;
748 }
Jeff Brown053b8652012-06-06 16:25:03 -0700749
Christopher Ferris20303f82014-01-10 16:33:16 -0800750 log_t log;
751 log.tfd = fd;
752 log.amfd = activity_manager_connect();
753 log.quiet = quiet;
754 *detach_failed = dump_crash(
755 &log, pid, tid, signal, abort_msg_address, dump_sibling_threads, total_sleep_time_usec);
Jeff Brown053b8652012-06-06 16:25:03 -0700756
Christopher Ferris20303f82014-01-10 16:33:16 -0800757 close(log.amfd);
758 close(fd);
759 return path;
Jeff Brown053b8652012-06-06 16:25:03 -0700760}