blob: 6d511715c4bdbdad3807f35ada354b4552ef97db [file] [log] [blame]
Jeff Brown053b8652012-06-06 16:25:03 -07001/*
Mark Salyzynfca0bd12013-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 Salyzyn989980c2014-05-14 12:37:22 -070034#include <cutils/properties.h>
Mark Salyzyn22b5cef2013-11-22 10:53:34 -080035#include <log/log.h>
Colin Cross9227bd32013-07-23 16:59:20 -070036#include <log/logger.h>
Mark Salyzyn989980c2014-05-14 12:37:22 -070037#include <log/logprint.h>
Jeff Brown053b8652012-06-06 16:25:03 -070038
Christopher Ferris20303f82014-01-10 16:33:16 -080039#include <backtrace/Backtrace.h>
Christopher Ferris46756822014-01-14 20:16:30 -080040#include <backtrace/BacktraceMap.h>
Jeff Brown053b8652012-06-06 16:25:03 -070041
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"
Kévin PETIT4bb47722013-12-18 16:44:24 +000054#define TOMBSTONE_TEMPLATE (TOMBSTONE_DIR"/tombstone_%02d")
Jeff Brown053b8652012-06-06 16:25:03 -070055
Christopher Ferris20303f82014-01-10 16:33:16 -080056// Must match the path defined in NativeCrashListener.java
Christopher Tateded2e5a2013-03-19 13:12:23 -070057#define NCRASH_SOCKET_PATH "/data/system/ndebugsocket"
58
Elliott Hughes855fcc32014-04-25 16:05:34 -070059static bool signal_has_si_addr(int sig) {
Christopher Ferris20303f82014-01-10 16:33:16 -080060 switch (sig) {
Jeff Brown053b8652012-06-06 16:25:03 -070061 case SIGILL:
Christopher Ferris20303f82014-01-10 16:33:16 -080062 case SIGFPE:
63 case SIGSEGV:
64 case SIGBUS:
65 return true;
66 default:
67 return false;
68 }
69}
70
71static const char* get_signame(int sig) {
72 switch(sig) {
73 case SIGILL: return "SIGILL";
74 case SIGABRT: return "SIGABRT";
75 case SIGBUS: return "SIGBUS";
76 case SIGFPE: return "SIGFPE";
77 case SIGSEGV: return "SIGSEGV";
78 case SIGPIPE: return "SIGPIPE";
Elliott Hughes855fcc32014-04-25 16:05:34 -070079#if defined(SIGSTKFLT)
Christopher Ferris20303f82014-01-10 16:33:16 -080080 case SIGSTKFLT: return "SIGSTKFLT";
81#endif
82 case SIGSTOP: return "SIGSTOP";
83 default: return "?";
84 }
85}
86
87static const char* get_sigcode(int signo, int code) {
88 // Try the signal-specific codes...
89 switch (signo) {
90 case SIGILL:
91 switch (code) {
Jeff Brown053b8652012-06-06 16:25:03 -070092 case ILL_ILLOPC: return "ILL_ILLOPC";
93 case ILL_ILLOPN: return "ILL_ILLOPN";
94 case ILL_ILLADR: return "ILL_ILLADR";
95 case ILL_ILLTRP: return "ILL_ILLTRP";
96 case ILL_PRVOPC: return "ILL_PRVOPC";
97 case ILL_PRVREG: return "ILL_PRVREG";
98 case ILL_COPROC: return "ILL_COPROC";
99 case ILL_BADSTK: return "ILL_BADSTK";
Christopher Ferris20303f82014-01-10 16:33:16 -0800100 }
Elliott Hughesbd395b92014-04-24 13:53:22 -0700101 static_assert(NSIGILL == ILL_BADSTK, "missing ILL_* si_code");
Christopher Ferris20303f82014-01-10 16:33:16 -0800102 break;
Jeff Brown053b8652012-06-06 16:25:03 -0700103 case SIGBUS:
Christopher Ferris20303f82014-01-10 16:33:16 -0800104 switch (code) {
Jeff Brown053b8652012-06-06 16:25:03 -0700105 case BUS_ADRALN: return "BUS_ADRALN";
106 case BUS_ADRERR: return "BUS_ADRERR";
107 case BUS_OBJERR: return "BUS_OBJERR";
Elliott Hughesbd395b92014-04-24 13:53:22 -0700108 case BUS_MCEERR_AR: return "BUS_MCEERR_AR";
109 case BUS_MCEERR_AO: return "BUS_MCEERR_AO";
Christopher Ferris20303f82014-01-10 16:33:16 -0800110 }
Elliott Hughesbd395b92014-04-24 13:53:22 -0700111 static_assert(NSIGBUS == BUS_MCEERR_AO, "missing BUS_* si_code");
Christopher Ferris20303f82014-01-10 16:33:16 -0800112 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 }
Elliott Hughesbd395b92014-04-24 13:53:22 -0700124 static_assert(NSIGFPE == FPE_FLTSUB, "missing FPE_* si_code");
Christopher Ferris20303f82014-01-10 16:33:16 -0800125 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 }
Elliott Hughesbd395b92014-04-24 13:53:22 -0700131 static_assert(NSIGSEGV == SEGV_ACCERR, "missing SEGV_* si_code");
Christopher Ferris20303f82014-01-10 16:33:16 -0800132 break;
Elliott Hughes8f7d4432012-12-10 10:29:05 -0800133 case SIGTRAP:
Christopher Ferris20303f82014-01-10 16:33:16 -0800134 switch (code) {
Elliott Hughes8f7d4432012-12-10 10:29:05 -0800135 case TRAP_BRKPT: return "TRAP_BRKPT";
136 case TRAP_TRACE: return "TRAP_TRACE";
Elliott Hughesbd395b92014-04-24 13:53:22 -0700137 case TRAP_BRANCH: return "TRAP_BRANCH";
138 case TRAP_HWBKPT: return "TRAP_HWBKPT";
Christopher Ferris20303f82014-01-10 16:33:16 -0800139 }
Elliott Hughesbd395b92014-04-24 13:53:22 -0700140 static_assert(NSIGTRAP == TRAP_HWBKPT, "missing TRAP_* si_code");
Christopher Ferris20303f82014-01-10 16:33:16 -0800141 break;
142 }
143 // Then the other codes...
144 switch (code) {
145 case SI_USER: return "SI_USER";
Christopher Ferris20303f82014-01-10 16:33:16 -0800146 case SI_KERNEL: return "SI_KERNEL";
Christopher Ferris20303f82014-01-10 16:33:16 -0800147 case SI_QUEUE: return "SI_QUEUE";
148 case SI_TIMER: return "SI_TIMER";
149 case SI_MESGQ: return "SI_MESGQ";
Elliott Hughes8f7d4432012-12-10 10:29:05 -0800150 case SI_ASYNCIO: return "SI_ASYNCIO";
Christopher Ferris20303f82014-01-10 16:33:16 -0800151 case SI_SIGIO: return "SI_SIGIO";
Christopher Ferris20303f82014-01-10 16:33:16 -0800152 case SI_TKILL: return "SI_TKILL";
Elliott Hughesbd395b92014-04-24 13:53:22 -0700153 case SI_DETHREAD: return "SI_DETHREAD";
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
Elliott Hughes855fcc32014-04-25 16:05:34 -0700175static void dump_signal_info(log_t* log, pid_t tid, int signal, int si_code) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800176 siginfo_t si;
Christopher Ferris20303f82014-01-10 16:33:16 -0800177 memset(&si, 0, sizeof(si));
Elliott Hughes855fcc32014-04-25 16:05:34 -0700178 if (ptrace(PTRACE_GETSIGINFO, tid, 0, &si) == -1) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800179 _LOG(log, SCOPE_AT_FAULT, "cannot get siginfo: %s\n", strerror(errno));
Elliott Hughes855fcc32014-04-25 16:05:34 -0700180 return;
Christopher Ferris20303f82014-01-10 16:33:16 -0800181 }
Elliott Hughes855fcc32014-04-25 16:05:34 -0700182
183 // bionic has to re-raise some signals, which overwrites the si_code with SI_TKILL.
184 si.si_code = si_code;
185
186 char addr_desc[32]; // ", fault addr 0x1234"
187 if (signal_has_si_addr(signal)) {
188 snprintf(addr_desc, sizeof(addr_desc), "%p", si.si_addr);
189 } else {
190 snprintf(addr_desc, sizeof(addr_desc), "--------");
191 }
192
193 _LOG(log, SCOPE_AT_FAULT, "signal %d (%s), code %d (%s), fault addr %s\n",
194 signal, get_signame(signal), si.si_code, get_sigcode(signal, si.si_code), addr_desc);
Jeff Brown053b8652012-06-06 16:25:03 -0700195}
196
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700197static void dump_thread_info(log_t* log, pid_t pid, pid_t tid, int scope_flags) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800198 char path[64];
199 char threadnamebuf[1024];
200 char* threadname = NULL;
201 FILE *fp;
Jeff Brown053b8652012-06-06 16:25:03 -0700202
Christopher Ferris20303f82014-01-10 16:33:16 -0800203 snprintf(path, sizeof(path), "/proc/%d/comm", tid);
204 if ((fp = fopen(path, "r"))) {
205 threadname = fgets(threadnamebuf, sizeof(threadnamebuf), fp);
206 fclose(fp);
207 if (threadname) {
208 size_t len = strlen(threadname);
209 if (len && threadname[len - 1] == '\n') {
210 threadname[len - 1] = '\0';
211 }
212 }
213 }
214
215 if (IS_AT_FAULT(scope_flags)) {
216 char procnamebuf[1024];
217 char* procname = NULL;
218
219 snprintf(path, sizeof(path), "/proc/%d/cmdline", pid);
Jeff Brown053b8652012-06-06 16:25:03 -0700220 if ((fp = fopen(path, "r"))) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800221 procname = fgets(procnamebuf, sizeof(procnamebuf), fp);
222 fclose(fp);
Jeff Brown053b8652012-06-06 16:25:03 -0700223 }
224
Christopher Ferris20303f82014-01-10 16:33:16 -0800225 _LOG(log, SCOPE_AT_FAULT, "pid: %d, tid: %d, name: %s >>> %s <<<\n", pid, tid,
226 threadname ? threadname : "UNKNOWN", procname ? procname : "UNKNOWN");
227 } else {
228 _LOG(log, 0, "pid: %d, tid: %d, name: %s\n", pid, tid, threadname ? threadname : "UNKNOWN");
229 }
230}
Jeff Brown053b8652012-06-06 16:25:03 -0700231
Christopher Ferris20303f82014-01-10 16:33:16 -0800232static void dump_stack_segment(
233 Backtrace* backtrace, log_t* log, int scope_flags, uintptr_t* sp, size_t words, int label) {
234 for (size_t i = 0; i < words; i++) {
Pavel Chupinc6c194c2013-11-21 23:17:20 +0400235 word_t stack_content;
Christopher Ferris20303f82014-01-10 16:33:16 -0800236 if (!backtrace->ReadWord(*sp, &stack_content)) {
237 break;
238 }
239
Christopher Ferris46756822014-01-14 20:16:30 -0800240 const backtrace_map_t* map = backtrace->FindMap(stack_content);
241 const char* map_name;
242 if (!map) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800243 map_name = "";
Christopher Ferris46756822014-01-14 20:16:30 -0800244 } else {
245 map_name = map->name.c_str();
Christopher Ferris20303f82014-01-10 16:33:16 -0800246 }
247 uintptr_t offset = 0;
248 std::string func_name(backtrace->GetFunctionName(stack_content, &offset));
249 if (!func_name.empty()) {
250 if (!i && label >= 0) {
251 if (offset) {
Elliott Hughesf7b4b512014-01-31 23:13:55 -0800252 _LOG(log, scope_flags, " #%02d %" PRIPTR " %" PRIPTR " %s (%s+%" PRIuPTR ")\n",
Christopher Ferris20303f82014-01-10 16:33:16 -0800253 label, *sp, stack_content, map_name, func_name.c_str(), offset);
254 } else {
Pavel Chupinc6c194c2013-11-21 23:17:20 +0400255 _LOG(log, scope_flags, " #%02d %" PRIPTR " %" PRIPTR " %s (%s)\n",
Christopher Ferris20303f82014-01-10 16:33:16 -0800256 label, *sp, stack_content, map_name, func_name.c_str());
Jeff Brown053b8652012-06-06 16:25:03 -0700257 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800258 } else {
259 if (offset) {
Elliott Hughesf7b4b512014-01-31 23:13:55 -0800260 _LOG(log, scope_flags, " %" PRIPTR " %" PRIPTR " %s (%s+%" PRIuPTR ")\n",
Christopher Ferris20303f82014-01-10 16:33:16 -0800261 *sp, stack_content, map_name, func_name.c_str(), offset);
262 } else {
Pavel Chupinc6c194c2013-11-21 23:17:20 +0400263 _LOG(log, scope_flags, " %" PRIPTR " %" PRIPTR " %s (%s)\n",
Christopher Ferris20303f82014-01-10 16:33:16 -0800264 *sp, stack_content, map_name, func_name.c_str());
265 }
266 }
Jeff Brown053b8652012-06-06 16:25:03 -0700267 } else {
Christopher Ferris20303f82014-01-10 16:33:16 -0800268 if (!i && label >= 0) {
Pavel Chupinc6c194c2013-11-21 23:17:20 +0400269 _LOG(log, scope_flags, " #%02d %" PRIPTR " %" PRIPTR " %s\n",
Christopher Ferris20303f82014-01-10 16:33:16 -0800270 label, *sp, stack_content, map_name);
271 } else {
Pavel Chupinc6c194c2013-11-21 23:17:20 +0400272 _LOG(log, scope_flags, " %" PRIPTR " %" PRIPTR " %s\n",
Christopher Ferris20303f82014-01-10 16:33:16 -0800273 *sp, stack_content, map_name);
274 }
Jeff Brown053b8652012-06-06 16:25:03 -0700275 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800276
Pavel Chupinc6c194c2013-11-21 23:17:20 +0400277 *sp += sizeof(word_t);
Christopher Ferris20303f82014-01-10 16:33:16 -0800278 }
Jeff Brown053b8652012-06-06 16:25:03 -0700279}
280
Christopher Ferris20303f82014-01-10 16:33:16 -0800281static void dump_stack(Backtrace* backtrace, log_t* log, int scope_flags) {
282 size_t first = 0, last;
283 for (size_t i = 0; i < backtrace->NumFrames(); i++) {
284 const backtrace_frame_data_t* frame = backtrace->GetFrame(i);
285 if (frame->sp) {
286 if (!first) {
287 first = i+1;
288 }
289 last = i;
Jeff Brown053b8652012-06-06 16:25:03 -0700290 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800291 }
292 if (!first) {
293 return;
294 }
295 first--;
296
297 scope_flags |= SCOPE_SENSITIVE;
298
299 // Dump a few words before the first frame.
Pavel Chupinc6c194c2013-11-21 23:17:20 +0400300 word_t sp = backtrace->GetFrame(first)->sp - STACK_WORDS * sizeof(word_t);
Christopher Ferris20303f82014-01-10 16:33:16 -0800301 dump_stack_segment(backtrace, log, scope_flags, &sp, STACK_WORDS, -1);
302
303 // Dump a few words from all successive frames.
304 // Only log the first 3 frames, put the rest in the tombstone.
305 for (size_t i = first; i <= last; i++) {
306 const backtrace_frame_data_t* frame = backtrace->GetFrame(i);
307 if (sp != frame->sp) {
308 _LOG(log, scope_flags, " ........ ........\n");
309 sp = frame->sp;
310 }
311 if (i - first == 3) {
312 scope_flags &= (~SCOPE_AT_FAULT);
313 }
314 if (i == last) {
315 dump_stack_segment(backtrace, log, scope_flags, &sp, STACK_WORDS, i);
316 if (sp < frame->sp + frame->stack_size) {
317 _LOG(log, scope_flags, " ........ ........\n");
318 }
319 } else {
Pavel Chupinc6c194c2013-11-21 23:17:20 +0400320 size_t words = frame->stack_size / sizeof(word_t);
Christopher Ferris20303f82014-01-10 16:33:16 -0800321 if (words == 0) {
322 words = 1;
323 } else if (words > STACK_WORDS) {
324 words = STACK_WORDS;
325 }
326 dump_stack_segment(backtrace, log, scope_flags, &sp, words, i);
327 }
328 }
Jeff Brown053b8652012-06-06 16:25:03 -0700329}
330
Christopher Ferris20303f82014-01-10 16:33:16 -0800331static void dump_backtrace_and_stack(Backtrace* backtrace, log_t* log, int scope_flags) {
332 if (backtrace->NumFrames()) {
333 _LOG(log, scope_flags, "\nbacktrace:\n");
334 dump_backtrace_to_log(backtrace, log, scope_flags, " ");
Jeff Brown053b8652012-06-06 16:25:03 -0700335
Christopher Ferris20303f82014-01-10 16:33:16 -0800336 _LOG(log, scope_flags, "\nstack:\n");
337 dump_stack(backtrace, log, scope_flags);
338 }
Jeff Brown053b8652012-06-06 16:25:03 -0700339}
340
Christopher Ferris46756822014-01-14 20:16:30 -0800341static void dump_map(log_t* log, const backtrace_map_t* map, const char* what, int scope_flags) {
342 if (map != NULL) {
Pavel Chupinc6c194c2013-11-21 23:17:20 +0400343 _LOG(log, scope_flags, " %" PRIPTR "-%" PRIPTR " %c%c%c %s\n", map->start, map->end,
Christopher Ferris46756822014-01-14 20:16:30 -0800344 (map->flags & PROT_READ) ? 'r' : '-', (map->flags & PROT_WRITE) ? 'w' : '-',
345 (map->flags & PROT_EXEC) ? 'x' : '-', map->name.c_str());
Christopher Ferris20303f82014-01-10 16:33:16 -0800346 } else {
347 _LOG(log, scope_flags, " (no %s)\n", what);
348 }
Elliott Hughesd1420be2013-01-03 13:39:57 -0800349}
350
Christopher Ferris46756822014-01-14 20:16:30 -0800351static void dump_nearby_maps(BacktraceMap* map, log_t* log, pid_t tid, int scope_flags) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800352 scope_flags |= SCOPE_SENSITIVE;
353 siginfo_t si;
354 memset(&si, 0, sizeof(si));
355 if (ptrace(PTRACE_GETSIGINFO, tid, 0, &si)) {
356 _LOG(log, scope_flags, "cannot get siginfo for %d: %s\n", tid, strerror(errno));
357 return;
358 }
Elliott Hughes855fcc32014-04-25 16:05:34 -0700359 if (!signal_has_si_addr(si.si_signo)) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800360 return;
361 }
362
Christopher Ferris46756822014-01-14 20:16:30 -0800363 uintptr_t addr = reinterpret_cast<uintptr_t>(si.si_addr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800364 addr &= ~0xfff; // round to 4K page boundary
365 if (addr == 0) { // null-pointer deref
366 return;
367 }
368
Kévin PETITabc60c22013-12-19 12:36:59 +0000369 _LOG(log, scope_flags, "\nmemory map around fault addr %" PRIPTR ":\n",
Christopher Ferris20303f82014-01-10 16:33:16 -0800370 reinterpret_cast<uintptr_t>(si.si_addr));
371
372 // Search for a match, or for a hole where the match would be. The list
373 // is backward from the file content, so it starts at high addresses.
Christopher Ferris46756822014-01-14 20:16:30 -0800374 const backtrace_map_t* cur_map = NULL;
375 const backtrace_map_t* next_map = NULL;
376 const backtrace_map_t* prev_map = NULL;
377 for (BacktraceMap::const_iterator it = map->begin(); it != map->end(); ++it) {
378 if (addr >= it->start && addr < it->end) {
379 cur_map = &*it;
380 if (it != map->begin()) {
381 prev_map = &*(it-1);
382 }
383 if (++it != map->end()) {
384 next_map = &*it;
385 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800386 break;
Jeff Brown053b8652012-06-06 16:25:03 -0700387 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800388 }
Jeff Brown053b8652012-06-06 16:25:03 -0700389
Christopher Ferris46756822014-01-14 20:16:30 -0800390 // Show the map address in ascending order (like /proc/pid/maps).
391 dump_map(log, prev_map, "map below", scope_flags);
392 dump_map(log, cur_map, "map for address", scope_flags);
393 dump_map(log, next_map, "map above", scope_flags);
Jeff Brown053b8652012-06-06 16:25:03 -0700394}
395
Christopher Ferris20303f82014-01-10 16:33:16 -0800396static void dump_thread(
397 Backtrace* backtrace, log_t* log, int scope_flags, int* total_sleep_time_usec) {
398 wait_for_stop(backtrace->Tid(), total_sleep_time_usec);
Jeff Brown053b8652012-06-06 16:25:03 -0700399
Christopher Ferris20303f82014-01-10 16:33:16 -0800400 dump_registers(log, backtrace->Tid(), scope_flags);
401 dump_backtrace_and_stack(backtrace, log, scope_flags);
402 if (IS_AT_FAULT(scope_flags)) {
403 dump_memory_and_code(log, backtrace->Tid(), scope_flags);
Christopher Ferris46756822014-01-14 20:16:30 -0800404 dump_nearby_maps(backtrace->GetMap(), log, backtrace->Tid(), scope_flags);
Christopher Ferris20303f82014-01-10 16:33:16 -0800405 }
Jeff Brown053b8652012-06-06 16:25:03 -0700406}
407
Christopher Ferris20303f82014-01-10 16:33:16 -0800408// Return true if some thread is not detached cleanly
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700409static bool dump_sibling_thread_report(
Christopher Ferris46756822014-01-14 20:16:30 -0800410 log_t* log, pid_t pid, pid_t tid, int* total_sleep_time_usec, BacktraceMap* map) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800411 char task_path[64];
412 snprintf(task_path, sizeof(task_path), "/proc/%d/task", pid);
Jeff Brown053b8652012-06-06 16:25:03 -0700413
Christopher Ferris20303f82014-01-10 16:33:16 -0800414 DIR* d = opendir(task_path);
415 // Bail early if the task directory cannot be opened
416 if (d == NULL) {
417 XLOG("Cannot open /proc/%d/task\n", pid);
418 return false;
419 }
420
421 bool detach_failed = false;
422 struct dirent* de;
423 while ((de = readdir(d)) != NULL) {
424 // Ignore "." and ".."
425 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, "..")) {
426 continue;
Jeff Brown053b8652012-06-06 16:25:03 -0700427 }
428
Christopher Ferris20303f82014-01-10 16:33:16 -0800429 // The main thread at fault has been handled individually
430 char* end;
431 pid_t new_tid = strtoul(de->d_name, &end, 10);
432 if (*end || new_tid == tid) {
433 continue;
Jeff Brown053b8652012-06-06 16:25:03 -0700434 }
435
Christopher Ferris20303f82014-01-10 16:33:16 -0800436 // Skip this thread if cannot ptrace it
437 if (ptrace(PTRACE_ATTACH, new_tid, 0, 0) < 0) {
438 continue;
439 }
440
441 _LOG(log, 0, "--- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---\n");
442 dump_thread_info(log, pid, new_tid, 0);
443
Christopher Ferris46756822014-01-14 20:16:30 -0800444 UniquePtr<Backtrace> backtrace(Backtrace::Create(pid, new_tid, map));
Christopher Ferris20303f82014-01-10 16:33:16 -0800445 if (backtrace->Unwind(0)) {
446 dump_thread(backtrace.get(), log, 0, total_sleep_time_usec);
447 }
448
449 if (ptrace(PTRACE_DETACH, new_tid, 0, 0) != 0) {
450 LOG("ptrace detach from %d failed: %s\n", new_tid, strerror(errno));
451 detach_failed = true;
452 }
453 }
454
455 closedir(d);
456 return detach_failed;
Jeff Brown053b8652012-06-06 16:25:03 -0700457}
458
Christopher Ferris20303f82014-01-10 16:33:16 -0800459// Reads the contents of the specified log device, filters out the entries
460// that don't match the specified pid, and writes them to the tombstone file.
461//
Mark Salyzyn22b5cef2013-11-22 10:53:34 -0800462// If "tail" is set, we only print the last few lines.
Mark Salyzyn989980c2014-05-14 12:37:22 -0700463static EventTagMap* g_eventTagMap = NULL;
464
Mark Salyzyn22b5cef2013-11-22 10:53:34 -0800465static void dump_log_file(log_t* log, pid_t pid, const char* filename,
466 unsigned int tail) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800467 bool first = true;
Mark Salyzyn22b5cef2013-11-22 10:53:34 -0800468 struct logger_list *logger_list;
Jeff Brown053b8652012-06-06 16:25:03 -0700469
Mark Salyzyn22b5cef2013-11-22 10:53:34 -0800470 logger_list = android_logger_list_open(
471 android_name_to_log_id(filename), O_RDONLY | O_NONBLOCK, tail, pid);
Jeff Brown053b8652012-06-06 16:25:03 -0700472
Mark Salyzyn22b5cef2013-11-22 10:53:34 -0800473 if (!logger_list) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800474 XLOG("Unable to open %s: %s\n", filename, strerror(errno));
475 return;
476 }
477
Mark Salyzyn22b5cef2013-11-22 10:53:34 -0800478 struct log_msg log_entry;
Christopher Ferris20303f82014-01-10 16:33:16 -0800479
480 while (true) {
Mark Salyzyn22b5cef2013-11-22 10:53:34 -0800481 ssize_t actual = android_logger_list_read(logger_list, &log_entry);
482
Christopher Ferris20303f82014-01-10 16:33:16 -0800483 if (actual < 0) {
Mark Salyzyn22b5cef2013-11-22 10:53:34 -0800484 if (actual == -EINTR) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800485 // interrupted by signal, retry
486 continue;
Mark Salyzyn22b5cef2013-11-22 10:53:34 -0800487 } else if (actual == -EAGAIN) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800488 // non-blocking EOF; we're done
489 break;
490 } else {
Mark Salyzyn22b5cef2013-11-22 10:53:34 -0800491 _LOG(log, 0, "Error while reading log: %s\n",
492 strerror(-actual));
Christopher Ferris20303f82014-01-10 16:33:16 -0800493 break;
494 }
495 } else if (actual == 0) {
Mark Salyzyn22b5cef2013-11-22 10:53:34 -0800496 _LOG(log, 0, "Got zero bytes while reading log: %s\n",
497 strerror(errno));
Christopher Ferris20303f82014-01-10 16:33:16 -0800498 break;
Jeff Brown053b8652012-06-06 16:25:03 -0700499 }
500
Christopher Ferris20303f82014-01-10 16:33:16 -0800501 // NOTE: if you XLOG something here, this will spin forever,
502 // because you will be writing as fast as you're reading. Any
503 // high-frequency debug diagnostics should just be written to
504 // the tombstone file.
Mark Salyzyn22b5cef2013-11-22 10:53:34 -0800505 struct logger_entry* entry = &log_entry.entry_v1;
Jeff Brown053b8652012-06-06 16:25:03 -0700506
Christopher Ferris20303f82014-01-10 16:33:16 -0800507 if (entry->pid != static_cast<int32_t>(pid)) {
508 // wrong pid, ignore
509 continue;
Jeff Brown053b8652012-06-06 16:25:03 -0700510 }
511
Christopher Ferris20303f82014-01-10 16:33:16 -0800512 if (first) {
Mark Salyzyn22b5cef2013-11-22 10:53:34 -0800513 _LOG(log, 0, "--------- %slog %s\n",
514 tail ? "tail end of " : "", filename);
Christopher Ferris20303f82014-01-10 16:33:16 -0800515 first = false;
516 }
517
518 // Msg format is: <priority:1><tag:N>\0<message:N>\0
519 //
520 // We want to display it in the same format as "logcat -v threadtime"
521 // (although in this case the pid is redundant).
Christopher Ferris20303f82014-01-10 16:33:16 -0800522 static const char* kPrioChars = "!.VDIWEFS";
Mark Salyzyn22b5cef2013-11-22 10:53:34 -0800523 unsigned hdr_size = log_entry.entry.hdr_size;
524 if (!hdr_size) {
525 hdr_size = sizeof(log_entry.entry_v1);
526 }
Mark Salyzyn989980c2014-05-14 12:37:22 -0700527 char* msg = reinterpret_cast<char*>(log_entry.buf) + hdr_size;
528
529 char timeBuf[32];
530 time_t sec = static_cast<time_t>(entry->sec);
531 struct tm tmBuf;
532 struct tm* ptm;
533 ptm = localtime_r(&sec, &tmBuf);
534 strftime(timeBuf, sizeof(timeBuf), "%m-%d %H:%M:%S", ptm);
535
536 if (log_entry.id() == LOG_ID_EVENTS) {
537 if (!g_eventTagMap) {
538 g_eventTagMap = android_openEventTagMap(EVENT_TAG_MAP_FILE);
539 }
540 AndroidLogEntry e;
541 char buf[512];
542 android_log_processBinaryLogBuffer(entry, &e, g_eventTagMap, buf, sizeof(buf));
543 _LOG(log, 0, "%s.%03d %5d %5d %c %-8s: %s\n",
544 timeBuf, entry->nsec / 1000000, entry->pid, entry->tid,
545 'I', e.tag, e.message);
546 continue;
547 }
548
Mark Salyzyn22b5cef2013-11-22 10:53:34 -0800549 unsigned char prio = msg[0];
550 char* tag = msg + 1;
551 msg = tag + strlen(tag) + 1;
Christopher Ferris20303f82014-01-10 16:33:16 -0800552
553 // consume any trailing newlines
Mark Salyzynfca0bd12013-12-12 12:21:20 -0800554 char* nl = msg + strlen(msg) - 1;
555 while (nl >= msg && *nl == '\n') {
556 *nl-- = '\0';
Christopher Ferris20303f82014-01-10 16:33:16 -0800557 }
558
559 char prioChar = (prio < strlen(kPrioChars) ? kPrioChars[prio] : '?');
560
Mark Salyzynfca0bd12013-12-12 12:21:20 -0800561 // Look for line breaks ('\n') and display each text line
562 // on a separate line, prefixed with the header, like logcat does.
563 do {
564 nl = strchr(msg, '\n');
565 if (nl) {
566 *nl = '\0';
567 ++nl;
568 }
569
570 _LOG(log, 0, "%s.%03d %5d %5d %c %-8s: %s\n",
571 timeBuf, entry->nsec / 1000000, entry->pid, entry->tid,
572 prioChar, tag, msg);
573
574 } while ((msg = nl));
Christopher Ferris20303f82014-01-10 16:33:16 -0800575 }
Jeff Brown053b8652012-06-06 16:25:03 -0700576
Mark Salyzyn22b5cef2013-11-22 10:53:34 -0800577 android_logger_list_free(logger_list);
Jeff Brown053b8652012-06-06 16:25:03 -0700578}
579
Christopher Ferris20303f82014-01-10 16:33:16 -0800580// Dumps the logs generated by the specified pid to the tombstone, from both
581// "system" and "main" log devices. Ideally we'd interleave the output.
Mark Salyzyn22b5cef2013-11-22 10:53:34 -0800582static void dump_logs(log_t* log, pid_t pid, unsigned tail) {
583 dump_log_file(log, pid, "system", tail);
584 dump_log_file(log, pid, "main", tail);
Mark Salyzyn989980c2014-05-14 12:37:22 -0700585 dump_log_file(log, pid, "events", tail);
Jeff Brown053b8652012-06-06 16:25:03 -0700586}
587
Christopher Ferris20303f82014-01-10 16:33:16 -0800588static void dump_abort_message(Backtrace* backtrace, log_t* log, uintptr_t address) {
Elliott Hughese5f8a692013-04-04 13:52:01 -0700589 if (address == 0) {
590 return;
591 }
592
593 address += sizeof(size_t); // Skip the buffer length.
594
595 char msg[512];
596 memset(msg, 0, sizeof(msg));
597 char* p = &msg[0];
598 while (p < &msg[sizeof(msg)]) {
Pavel Chupinc6c194c2013-11-21 23:17:20 +0400599 word_t data;
600 size_t len = sizeof(word_t);
Christopher Ferris20303f82014-01-10 16:33:16 -0800601 if (!backtrace->ReadWord(address, &data)) {
Elliott Hughese5f8a692013-04-04 13:52:01 -0700602 break;
603 }
Pavel Chupinc6c194c2013-11-21 23:17:20 +0400604 address += sizeof(word_t);
Elliott Hughese5f8a692013-04-04 13:52:01 -0700605
Pavel Chupinc6c194c2013-11-21 23:17:20 +0400606 while (len > 0 && (*p++ = (data >> (sizeof(word_t) - len) * 8) & 0xff) != 0)
607 len--;
Elliott Hughese5f8a692013-04-04 13:52:01 -0700608 }
609 msg[sizeof(msg) - 1] = '\0';
610
Christopher Tate7716aef2013-04-02 14:00:27 -0700611 _LOG(log, SCOPE_AT_FAULT, "Abort message: '%s'\n", msg);
Elliott Hughese5f8a692013-04-04 13:52:01 -0700612}
613
Christopher Ferris20303f82014-01-10 16:33:16 -0800614// Dumps all information about the specified pid to the tombstone.
Elliott Hughes855fcc32014-04-25 16:05:34 -0700615static bool dump_crash(log_t* log, pid_t pid, pid_t tid, int signal, int si_code,
616 uintptr_t abort_msg_address, bool dump_sibling_threads,
617 int* total_sleep_time_usec) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800618 // don't copy log messages to tombstone unless this is a dev device
619 char value[PROPERTY_VALUE_MAX];
620 property_get("ro.debuggable", value, "0");
621 bool want_logs = (value[0] == '1');
Jeff Brown053b8652012-06-06 16:25:03 -0700622
Christopher Ferris20303f82014-01-10 16:33:16 -0800623 if (log->amfd >= 0) {
624 // Activity Manager protocol: binary 32-bit network-byte-order ints for the
625 // pid and signal number, followed by the raw text of the dump, culminating
626 // in a zero byte that marks end-of-data.
627 uint32_t datum = htonl(pid);
628 TEMP_FAILURE_RETRY( write(log->amfd, &datum, 4) );
629 datum = htonl(signal);
630 TEMP_FAILURE_RETRY( write(log->amfd, &datum, 4) );
631 }
Christopher Tateded2e5a2013-03-19 13:12:23 -0700632
Christopher Ferris20303f82014-01-10 16:33:16 -0800633 _LOG(log, SCOPE_AT_FAULT,
634 "*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***\n");
635 dump_build_info(log);
636 dump_revision_info(log);
637 dump_thread_info(log, pid, tid, SCOPE_AT_FAULT);
638 if (signal) {
Elliott Hughes855fcc32014-04-25 16:05:34 -0700639 dump_signal_info(log, tid, signal, si_code);
Christopher Ferris20303f82014-01-10 16:33:16 -0800640 }
Jeff Brown053b8652012-06-06 16:25:03 -0700641
Christopher Ferrisdf290612014-01-22 19:21:07 -0800642 UniquePtr<BacktraceMap> map(BacktraceMap::Create(pid));
643 UniquePtr<Backtrace> backtrace(Backtrace::Create(pid, tid, map.get()));
Christopher Ferris20303f82014-01-10 16:33:16 -0800644 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) {
Mark Salyzyn22b5cef2013-11-22 10:53:34 -0800650 dump_logs(log, pid, 5);
Christopher Ferris20303f82014-01-10 16:33:16 -0800651 }
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) {
Christopher Ferrisdf290612014-01-22 19:21:07 -0800655 detach_failed = dump_sibling_thread_report(log, pid, tid, total_sleep_time_usec, map.get());
Christopher Ferris20303f82014-01-10 16:33:16 -0800656 }
Christopher Ferris98464972014-01-06 19:16:33 -0800657
Christopher Ferris20303f82014-01-10 16:33:16 -0800658 if (want_logs) {
Mark Salyzyn22b5cef2013-11-22 10:53:34 -0800659 dump_logs(log, pid, 0);
Christopher Ferris20303f82014-01-10 16:33:16 -0800660 }
Christopher Tateded2e5a2013-03-19 13:12:23 -0700661
Christopher Ferris20303f82014-01-10 16:33:16 -0800662 // send EOD to the Activity Manager, then wait for its ack to avoid racing ahead
663 // and killing the target out from under it
664 if (log->amfd >= 0) {
665 uint8_t eodMarker = 0;
666 TEMP_FAILURE_RETRY( write(log->amfd, &eodMarker, 1) );
667 // 3 sec timeout reading the ack; we're fine if that happens
668 TEMP_FAILURE_RETRY( read(log->amfd, &eodMarker, 1) );
669 }
670
671 return detach_failed;
Jeff Brown053b8652012-06-06 16:25:03 -0700672}
673
Christopher Ferris20303f82014-01-10 16:33:16 -0800674// find_and_open_tombstone - find an available tombstone slot, if any, of the
675// form tombstone_XX where XX is 00 to MAX_TOMBSTONES-1, inclusive. If no
676// file is available, we reuse the least-recently-modified file.
677//
678// Returns the path of the tombstone file, allocated using malloc(). Caller must free() it.
679static char* find_and_open_tombstone(int* fd) {
Christopher Ferris68bd59f2014-02-07 13:25:26 -0800680 // In a single pass, find an available slot and, in case none
Christopher Ferris20303f82014-01-10 16:33:16 -0800681 // exist, find and record the least-recently-modified file.
682 char path[128];
Christopher Ferris68bd59f2014-02-07 13:25:26 -0800683 int oldest = -1;
684 struct stat oldest_sb;
Christopher Ferris20303f82014-01-10 16:33:16 -0800685 for (int i = 0; i < MAX_TOMBSTONES; i++) {
Kévin PETIT4bb47722013-12-18 16:44:24 +0000686 snprintf(path, sizeof(path), TOMBSTONE_TEMPLATE, i);
Jeff Brown053b8652012-06-06 16:25:03 -0700687
Christopher Ferris68bd59f2014-02-07 13:25:26 -0800688 struct stat sb;
Christopher Ferris20303f82014-01-10 16:33:16 -0800689 if (!stat(path, &sb)) {
Christopher Ferris68bd59f2014-02-07 13:25:26 -0800690 if (oldest < 0 || sb.st_mtime < oldest_sb.st_mtime) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800691 oldest = i;
Christopher Ferris68bd59f2014-02-07 13:25:26 -0800692 oldest_sb.st_mtime = sb.st_mtime;
Christopher Ferris20303f82014-01-10 16:33:16 -0800693 }
694 continue;
Jeff Brown053b8652012-06-06 16:25:03 -0700695 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800696 if (errno != ENOENT)
697 continue;
Jeff Brown053b8652012-06-06 16:25:03 -0700698
Christopher Ferris20303f82014-01-10 16:33:16 -0800699 *fd = open(path, O_CREAT | O_EXCL | O_WRONLY, 0600);
700 if (*fd < 0)
701 continue; // raced ?
702
Jeff Brown053b8652012-06-06 16:25:03 -0700703 fchown(*fd, AID_SYSTEM, AID_SYSTEM);
704 return strdup(path);
Christopher Ferris20303f82014-01-10 16:33:16 -0800705 }
706
Christopher Ferris68bd59f2014-02-07 13:25:26 -0800707 if (oldest < 0) {
708 LOG("Failed to find a valid tombstone, default to using tombstone 0.\n");
709 oldest = 0;
710 }
711
Christopher Ferris20303f82014-01-10 16:33:16 -0800712 // we didn't find an available file, so we clobber the oldest one
Kévin PETIT4bb47722013-12-18 16:44:24 +0000713 snprintf(path, sizeof(path), TOMBSTONE_TEMPLATE, oldest);
Christopher Ferris20303f82014-01-10 16:33:16 -0800714 *fd = open(path, O_CREAT | O_TRUNC | O_WRONLY, 0600);
715 if (*fd < 0) {
716 LOG("failed to open tombstone file '%s': %s\n", path, strerror(errno));
717 return NULL;
718 }
719 fchown(*fd, AID_SYSTEM, AID_SYSTEM);
720 return strdup(path);
Jeff Brown053b8652012-06-06 16:25:03 -0700721}
722
Christopher Tateded2e5a2013-03-19 13:12:23 -0700723static int activity_manager_connect() {
Christopher Ferris20303f82014-01-10 16:33:16 -0800724 int amfd = socket(PF_UNIX, SOCK_STREAM, 0);
725 if (amfd >= 0) {
726 struct sockaddr_un address;
727 int err;
Christopher Tateded2e5a2013-03-19 13:12:23 -0700728
Christopher Ferris20303f82014-01-10 16:33:16 -0800729 memset(&address, 0, sizeof(address));
730 address.sun_family = AF_UNIX;
731 strncpy(address.sun_path, NCRASH_SOCKET_PATH, sizeof(address.sun_path));
732 err = TEMP_FAILURE_RETRY(connect(
733 amfd, reinterpret_cast<struct sockaddr*>(&address), sizeof(address)));
734 if (!err) {
735 struct timeval tv;
736 memset(&tv, 0, sizeof(tv));
737 tv.tv_sec = 1; // tight leash
738 err = setsockopt(amfd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
739 if (!err) {
740 tv.tv_sec = 3; // 3 seconds on handshake read
741 err = setsockopt(amfd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
742 }
Christopher Tateded2e5a2013-03-19 13:12:23 -0700743 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800744 if (err) {
745 close(amfd);
746 amfd = -1;
747 }
748 }
Christopher Tateded2e5a2013-03-19 13:12:23 -0700749
Christopher Ferris20303f82014-01-10 16:33:16 -0800750 return amfd;
Christopher Tateded2e5a2013-03-19 13:12:23 -0700751}
752
Elliott Hughes855fcc32014-04-25 16:05:34 -0700753char* engrave_tombstone(pid_t pid, pid_t tid, int signal, int original_si_code,
754 uintptr_t abort_msg_address, bool dump_sibling_threads, bool quiet,
755 bool* detach_failed, int* total_sleep_time_usec) {
Kévin PETIT4bb47722013-12-18 16:44:24 +0000756 if ((mkdir(TOMBSTONE_DIR, 0755) == -1) && (errno != EEXIST)) {
Christopher Ferrisc5bb49a2014-05-05 11:16:47 -0700757 LOG("failed to create %s: %s\n", TOMBSTONE_DIR, strerror(errno));
Kévin PETIT4bb47722013-12-18 16:44:24 +0000758 }
759
760 if (chown(TOMBSTONE_DIR, AID_SYSTEM, AID_SYSTEM) == -1) {
Christopher Ferrisc5bb49a2014-05-05 11:16:47 -0700761 LOG("failed to change ownership of %s: %s\n", TOMBSTONE_DIR, strerror(errno));
Kévin PETIT4bb47722013-12-18 16:44:24 +0000762 }
Jeff Brown053b8652012-06-06 16:25:03 -0700763
Christopher Ferrisc5bb49a2014-05-05 11:16:47 -0700764 int fd = -1;
765 char* path = NULL;
766 if (selinux_android_restorecon(TOMBSTONE_DIR, 0) == 0) {
767 path = find_and_open_tombstone(&fd);
768 } else {
769 LOG("Failed to restore security context, not writing tombstone.\n");
Christopher Ferris20303f82014-01-10 16:33:16 -0800770 }
rpcraigf1186f32012-07-19 09:38:06 -0400771
Christopher Ferrisc5bb49a2014-05-05 11:16:47 -0700772 if (fd < 0 && quiet) {
773 LOG("Skipping tombstone write, nothing to do.\n");
Christopher Ferris20303f82014-01-10 16:33:16 -0800774 *detach_failed = false;
775 return NULL;
776 }
Jeff Brown053b8652012-06-06 16:25:03 -0700777
Christopher Ferris20303f82014-01-10 16:33:16 -0800778 log_t log;
779 log.tfd = fd;
Christopher Ferrisc5bb49a2014-05-05 11:16:47 -0700780 // Preserve amfd since it can be modified through the calls below without
781 // being closed.
782 int amfd = activity_manager_connect();
783 log.amfd = amfd;
Christopher Ferris20303f82014-01-10 16:33:16 -0800784 log.quiet = quiet;
Elliott Hughes855fcc32014-04-25 16:05:34 -0700785 *detach_failed = dump_crash(&log, pid, tid, signal, original_si_code, abort_msg_address,
786 dump_sibling_threads, total_sleep_time_usec);
Jeff Brown053b8652012-06-06 16:25:03 -0700787
Christopher Ferrisc5bb49a2014-05-05 11:16:47 -0700788 // Either of these file descriptors can be -1, any error is ignored.
789 close(amfd);
Christopher Ferris20303f82014-01-10 16:33:16 -0800790 close(fd);
Christopher Ferrisc5bb49a2014-05-05 11:16:47 -0700791
Christopher Ferris20303f82014-01-10 16:33:16 -0800792 return path;
Jeff Brown053b8652012-06-06 16:25:03 -0700793}