blob: 749752b1cfefe78f5d21fd4628d74720470799af [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
Elliott Hughes855fcc32014-04-25 16:05:34 -070058static bool signal_has_si_addr(int sig) {
Christopher Ferris20303f82014-01-10 16:33:16 -080059 switch (sig) {
Jeff Brown053b8652012-06-06 16:25:03 -070060 case SIGILL:
Christopher Ferris20303f82014-01-10 16:33:16 -080061 case SIGFPE:
62 case SIGSEGV:
63 case SIGBUS:
64 return true;
65 default:
66 return false;
67 }
68}
69
70static const char* get_signame(int sig) {
71 switch(sig) {
72 case SIGILL: return "SIGILL";
73 case SIGABRT: return "SIGABRT";
74 case SIGBUS: return "SIGBUS";
75 case SIGFPE: return "SIGFPE";
76 case SIGSEGV: return "SIGSEGV";
77 case SIGPIPE: return "SIGPIPE";
Elliott Hughes855fcc32014-04-25 16:05:34 -070078#if defined(SIGSTKFLT)
Christopher Ferris20303f82014-01-10 16:33:16 -080079 case SIGSTKFLT: return "SIGSTKFLT";
80#endif
81 case SIGSTOP: return "SIGSTOP";
82 default: return "?";
83 }
84}
85
86static const char* get_sigcode(int signo, int code) {
87 // Try the signal-specific codes...
88 switch (signo) {
89 case SIGILL:
90 switch (code) {
Jeff Brown053b8652012-06-06 16:25:03 -070091 case ILL_ILLOPC: return "ILL_ILLOPC";
92 case ILL_ILLOPN: return "ILL_ILLOPN";
93 case ILL_ILLADR: return "ILL_ILLADR";
94 case ILL_ILLTRP: return "ILL_ILLTRP";
95 case ILL_PRVOPC: return "ILL_PRVOPC";
96 case ILL_PRVREG: return "ILL_PRVREG";
97 case ILL_COPROC: return "ILL_COPROC";
98 case ILL_BADSTK: return "ILL_BADSTK";
Christopher Ferris20303f82014-01-10 16:33:16 -080099 }
Elliott Hughesbd395b92014-04-24 13:53:22 -0700100 static_assert(NSIGILL == ILL_BADSTK, "missing ILL_* si_code");
Christopher Ferris20303f82014-01-10 16:33:16 -0800101 break;
Jeff Brown053b8652012-06-06 16:25:03 -0700102 case SIGBUS:
Christopher Ferris20303f82014-01-10 16:33:16 -0800103 switch (code) {
Jeff Brown053b8652012-06-06 16:25:03 -0700104 case BUS_ADRALN: return "BUS_ADRALN";
105 case BUS_ADRERR: return "BUS_ADRERR";
106 case BUS_OBJERR: return "BUS_OBJERR";
Elliott Hughesbd395b92014-04-24 13:53:22 -0700107 case BUS_MCEERR_AR: return "BUS_MCEERR_AR";
108 case BUS_MCEERR_AO: return "BUS_MCEERR_AO";
Christopher Ferris20303f82014-01-10 16:33:16 -0800109 }
Elliott Hughesbd395b92014-04-24 13:53:22 -0700110 static_assert(NSIGBUS == BUS_MCEERR_AO, "missing BUS_* si_code");
Christopher Ferris20303f82014-01-10 16:33:16 -0800111 break;
Jeff Brown053b8652012-06-06 16:25:03 -0700112 case SIGFPE:
Christopher Ferris20303f82014-01-10 16:33:16 -0800113 switch (code) {
Jeff Brown053b8652012-06-06 16:25:03 -0700114 case FPE_INTDIV: return "FPE_INTDIV";
115 case FPE_INTOVF: return "FPE_INTOVF";
116 case FPE_FLTDIV: return "FPE_FLTDIV";
117 case FPE_FLTOVF: return "FPE_FLTOVF";
118 case FPE_FLTUND: return "FPE_FLTUND";
119 case FPE_FLTRES: return "FPE_FLTRES";
120 case FPE_FLTINV: return "FPE_FLTINV";
121 case FPE_FLTSUB: return "FPE_FLTSUB";
Christopher Ferris20303f82014-01-10 16:33:16 -0800122 }
Elliott Hughesbd395b92014-04-24 13:53:22 -0700123 static_assert(NSIGFPE == FPE_FLTSUB, "missing FPE_* si_code");
Christopher Ferris20303f82014-01-10 16:33:16 -0800124 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 }
Elliott Hughesbd395b92014-04-24 13:53:22 -0700130 static_assert(NSIGSEGV == SEGV_ACCERR, "missing SEGV_* si_code");
Christopher Ferris20303f82014-01-10 16:33:16 -0800131 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";
Elliott Hughesbd395b92014-04-24 13:53:22 -0700136 case TRAP_BRANCH: return "TRAP_BRANCH";
137 case TRAP_HWBKPT: return "TRAP_HWBKPT";
Christopher Ferris20303f82014-01-10 16:33:16 -0800138 }
Elliott Hughesbd395b92014-04-24 13:53:22 -0700139 static_assert(NSIGTRAP == TRAP_HWBKPT, "missing TRAP_* si_code");
Christopher Ferris20303f82014-01-10 16:33:16 -0800140 break;
141 }
142 // Then the other codes...
143 switch (code) {
144 case SI_USER: return "SI_USER";
Christopher Ferris20303f82014-01-10 16:33:16 -0800145 case SI_KERNEL: return "SI_KERNEL";
Christopher Ferris20303f82014-01-10 16:33:16 -0800146 case SI_QUEUE: return "SI_QUEUE";
147 case SI_TIMER: return "SI_TIMER";
148 case SI_MESGQ: return "SI_MESGQ";
Elliott Hughes8f7d4432012-12-10 10:29:05 -0800149 case SI_ASYNCIO: return "SI_ASYNCIO";
Christopher Ferris20303f82014-01-10 16:33:16 -0800150 case SI_SIGIO: return "SI_SIGIO";
Christopher Ferris20303f82014-01-10 16:33:16 -0800151 case SI_TKILL: return "SI_TKILL";
Elliott Hughesbd395b92014-04-24 13:53:22 -0700152 case SI_DETHREAD: return "SI_DETHREAD";
Christopher Ferris20303f82014-01-10 16:33:16 -0800153 }
154 // Then give up...
155 return "?";
Jeff Brown053b8652012-06-06 16:25:03 -0700156}
157
Christopher Ferris20303f82014-01-10 16:33:16 -0800158static void dump_revision_info(log_t* log) {
159 char revision[PROPERTY_VALUE_MAX];
Ben Chengd7760c12012-09-19 16:04:01 -0700160
Christopher Ferris20303f82014-01-10 16:33:16 -0800161 property_get("ro.revision", revision, "unknown");
Ben Chengd7760c12012-09-19 16:04:01 -0700162
Christopher Ferris20303f82014-01-10 16:33:16 -0800163 _LOG(log, SCOPE_AT_FAULT, "Revision: '%s'\n", revision);
Ben Chengd7760c12012-09-19 16:04:01 -0700164}
165
Christopher Ferris20303f82014-01-10 16:33:16 -0800166static void dump_build_info(log_t* log) {
167 char fingerprint[PROPERTY_VALUE_MAX];
Jeff Brown053b8652012-06-06 16:25:03 -0700168
Christopher Ferris20303f82014-01-10 16:33:16 -0800169 property_get("ro.build.fingerprint", fingerprint, "unknown");
Jeff Brown053b8652012-06-06 16:25:03 -0700170
Christopher Ferris20303f82014-01-10 16:33:16 -0800171 _LOG(log, SCOPE_AT_FAULT, "Build fingerprint: '%s'\n", fingerprint);
Jeff Brown053b8652012-06-06 16:25:03 -0700172}
173
Elliott Hughes855fcc32014-04-25 16:05:34 -0700174static void dump_signal_info(log_t* log, pid_t tid, int signal, int si_code) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800175 siginfo_t si;
Christopher Ferris20303f82014-01-10 16:33:16 -0800176 memset(&si, 0, sizeof(si));
Elliott Hughes855fcc32014-04-25 16:05:34 -0700177 if (ptrace(PTRACE_GETSIGINFO, tid, 0, &si) == -1) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800178 _LOG(log, SCOPE_AT_FAULT, "cannot get siginfo: %s\n", strerror(errno));
Elliott Hughes855fcc32014-04-25 16:05:34 -0700179 return;
Christopher Ferris20303f82014-01-10 16:33:16 -0800180 }
Elliott Hughes855fcc32014-04-25 16:05:34 -0700181
182 // bionic has to re-raise some signals, which overwrites the si_code with SI_TKILL.
183 si.si_code = si_code;
184
185 char addr_desc[32]; // ", fault addr 0x1234"
186 if (signal_has_si_addr(signal)) {
187 snprintf(addr_desc, sizeof(addr_desc), "%p", si.si_addr);
188 } else {
189 snprintf(addr_desc, sizeof(addr_desc), "--------");
190 }
191
192 _LOG(log, SCOPE_AT_FAULT, "signal %d (%s), code %d (%s), fault addr %s\n",
193 signal, get_signame(signal), si.si_code, get_sigcode(signal, si.si_code), addr_desc);
Jeff Brown053b8652012-06-06 16:25:03 -0700194}
195
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700196static void dump_thread_info(log_t* log, pid_t pid, pid_t tid, int scope_flags) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800197 char path[64];
198 char threadnamebuf[1024];
199 char* threadname = NULL;
200 FILE *fp;
Jeff Brown053b8652012-06-06 16:25:03 -0700201
Christopher Ferris20303f82014-01-10 16:33:16 -0800202 snprintf(path, sizeof(path), "/proc/%d/comm", tid);
203 if ((fp = fopen(path, "r"))) {
204 threadname = fgets(threadnamebuf, sizeof(threadnamebuf), fp);
205 fclose(fp);
206 if (threadname) {
207 size_t len = strlen(threadname);
208 if (len && threadname[len - 1] == '\n') {
209 threadname[len - 1] = '\0';
210 }
211 }
212 }
213
214 if (IS_AT_FAULT(scope_flags)) {
215 char procnamebuf[1024];
216 char* procname = NULL;
217
218 snprintf(path, sizeof(path), "/proc/%d/cmdline", pid);
Jeff Brown053b8652012-06-06 16:25:03 -0700219 if ((fp = fopen(path, "r"))) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800220 procname = fgets(procnamebuf, sizeof(procnamebuf), fp);
221 fclose(fp);
Jeff Brown053b8652012-06-06 16:25:03 -0700222 }
223
Christopher Ferris20303f82014-01-10 16:33:16 -0800224 _LOG(log, SCOPE_AT_FAULT, "pid: %d, tid: %d, name: %s >>> %s <<<\n", pid, tid,
225 threadname ? threadname : "UNKNOWN", procname ? procname : "UNKNOWN");
226 } else {
227 _LOG(log, 0, "pid: %d, tid: %d, name: %s\n", pid, tid, threadname ? threadname : "UNKNOWN");
228 }
229}
Jeff Brown053b8652012-06-06 16:25:03 -0700230
Christopher Ferris20303f82014-01-10 16:33:16 -0800231static void dump_stack_segment(
232 Backtrace* backtrace, log_t* log, int scope_flags, uintptr_t* sp, size_t words, int label) {
233 for (size_t i = 0; i < words; i++) {
Pavel Chupinc6c194c2013-11-21 23:17:20 +0400234 word_t stack_content;
Christopher Ferris20303f82014-01-10 16:33:16 -0800235 if (!backtrace->ReadWord(*sp, &stack_content)) {
236 break;
237 }
238
Christopher Ferris46756822014-01-14 20:16:30 -0800239 const backtrace_map_t* map = backtrace->FindMap(stack_content);
240 const char* map_name;
241 if (!map) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800242 map_name = "";
Christopher Ferris46756822014-01-14 20:16:30 -0800243 } else {
244 map_name = map->name.c_str();
Christopher Ferris20303f82014-01-10 16:33:16 -0800245 }
246 uintptr_t offset = 0;
247 std::string func_name(backtrace->GetFunctionName(stack_content, &offset));
248 if (!func_name.empty()) {
249 if (!i && label >= 0) {
250 if (offset) {
Elliott Hughesf7b4b512014-01-31 23:13:55 -0800251 _LOG(log, scope_flags, " #%02d %" PRIPTR " %" PRIPTR " %s (%s+%" PRIuPTR ")\n",
Christopher Ferris20303f82014-01-10 16:33:16 -0800252 label, *sp, stack_content, map_name, func_name.c_str(), offset);
253 } else {
Pavel Chupinc6c194c2013-11-21 23:17:20 +0400254 _LOG(log, scope_flags, " #%02d %" PRIPTR " %" PRIPTR " %s (%s)\n",
Christopher Ferris20303f82014-01-10 16:33:16 -0800255 label, *sp, stack_content, map_name, func_name.c_str());
Jeff Brown053b8652012-06-06 16:25:03 -0700256 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800257 } else {
258 if (offset) {
Elliott Hughesf7b4b512014-01-31 23:13:55 -0800259 _LOG(log, scope_flags, " %" PRIPTR " %" PRIPTR " %s (%s+%" PRIuPTR ")\n",
Christopher Ferris20303f82014-01-10 16:33:16 -0800260 *sp, stack_content, map_name, func_name.c_str(), offset);
261 } else {
Pavel Chupinc6c194c2013-11-21 23:17:20 +0400262 _LOG(log, scope_flags, " %" PRIPTR " %" PRIPTR " %s (%s)\n",
Christopher Ferris20303f82014-01-10 16:33:16 -0800263 *sp, stack_content, map_name, func_name.c_str());
264 }
265 }
Jeff Brown053b8652012-06-06 16:25:03 -0700266 } else {
Christopher Ferris20303f82014-01-10 16:33:16 -0800267 if (!i && label >= 0) {
Pavel Chupinc6c194c2013-11-21 23:17:20 +0400268 _LOG(log, scope_flags, " #%02d %" PRIPTR " %" PRIPTR " %s\n",
Christopher Ferris20303f82014-01-10 16:33:16 -0800269 label, *sp, stack_content, map_name);
270 } else {
Pavel Chupinc6c194c2013-11-21 23:17:20 +0400271 _LOG(log, scope_flags, " %" PRIPTR " %" PRIPTR " %s\n",
Christopher Ferris20303f82014-01-10 16:33:16 -0800272 *sp, stack_content, map_name);
273 }
Jeff Brown053b8652012-06-06 16:25:03 -0700274 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800275
Pavel Chupinc6c194c2013-11-21 23:17:20 +0400276 *sp += sizeof(word_t);
Christopher Ferris20303f82014-01-10 16:33:16 -0800277 }
Jeff Brown053b8652012-06-06 16:25:03 -0700278}
279
Christopher Ferris20303f82014-01-10 16:33:16 -0800280static void dump_stack(Backtrace* backtrace, log_t* log, int scope_flags) {
281 size_t first = 0, last;
282 for (size_t i = 0; i < backtrace->NumFrames(); i++) {
283 const backtrace_frame_data_t* frame = backtrace->GetFrame(i);
284 if (frame->sp) {
285 if (!first) {
286 first = i+1;
287 }
288 last = i;
Jeff Brown053b8652012-06-06 16:25:03 -0700289 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800290 }
291 if (!first) {
292 return;
293 }
294 first--;
295
296 scope_flags |= SCOPE_SENSITIVE;
297
298 // Dump a few words before the first frame.
Pavel Chupinc6c194c2013-11-21 23:17:20 +0400299 word_t sp = backtrace->GetFrame(first)->sp - STACK_WORDS * sizeof(word_t);
Christopher Ferris20303f82014-01-10 16:33:16 -0800300 dump_stack_segment(backtrace, log, scope_flags, &sp, STACK_WORDS, -1);
301
302 // Dump a few words from all successive frames.
303 // Only log the first 3 frames, put the rest in the tombstone.
304 for (size_t i = first; i <= last; i++) {
305 const backtrace_frame_data_t* frame = backtrace->GetFrame(i);
306 if (sp != frame->sp) {
307 _LOG(log, scope_flags, " ........ ........\n");
308 sp = frame->sp;
309 }
310 if (i - first == 3) {
311 scope_flags &= (~SCOPE_AT_FAULT);
312 }
313 if (i == last) {
314 dump_stack_segment(backtrace, log, scope_flags, &sp, STACK_WORDS, i);
315 if (sp < frame->sp + frame->stack_size) {
316 _LOG(log, scope_flags, " ........ ........\n");
317 }
318 } else {
Pavel Chupinc6c194c2013-11-21 23:17:20 +0400319 size_t words = frame->stack_size / sizeof(word_t);
Christopher Ferris20303f82014-01-10 16:33:16 -0800320 if (words == 0) {
321 words = 1;
322 } else if (words > STACK_WORDS) {
323 words = STACK_WORDS;
324 }
325 dump_stack_segment(backtrace, log, scope_flags, &sp, words, i);
326 }
327 }
Jeff Brown053b8652012-06-06 16:25:03 -0700328}
329
Christopher Ferris20303f82014-01-10 16:33:16 -0800330static void dump_backtrace_and_stack(Backtrace* backtrace, log_t* log, int scope_flags) {
331 if (backtrace->NumFrames()) {
332 _LOG(log, scope_flags, "\nbacktrace:\n");
333 dump_backtrace_to_log(backtrace, log, scope_flags, " ");
Jeff Brown053b8652012-06-06 16:25:03 -0700334
Christopher Ferris20303f82014-01-10 16:33:16 -0800335 _LOG(log, scope_flags, "\nstack:\n");
336 dump_stack(backtrace, log, scope_flags);
337 }
Jeff Brown053b8652012-06-06 16:25:03 -0700338}
339
Christopher Ferris46756822014-01-14 20:16:30 -0800340static void dump_map(log_t* log, const backtrace_map_t* map, const char* what, int scope_flags) {
341 if (map != NULL) {
Pavel Chupinc6c194c2013-11-21 23:17:20 +0400342 _LOG(log, scope_flags, " %" PRIPTR "-%" PRIPTR " %c%c%c %s\n", map->start, map->end,
Christopher Ferris46756822014-01-14 20:16:30 -0800343 (map->flags & PROT_READ) ? 'r' : '-', (map->flags & PROT_WRITE) ? 'w' : '-',
344 (map->flags & PROT_EXEC) ? 'x' : '-', map->name.c_str());
Christopher Ferris20303f82014-01-10 16:33:16 -0800345 } else {
346 _LOG(log, scope_flags, " (no %s)\n", what);
347 }
Elliott Hughesd1420be2013-01-03 13:39:57 -0800348}
349
Christopher Ferris46756822014-01-14 20:16:30 -0800350static void dump_nearby_maps(BacktraceMap* map, log_t* log, pid_t tid, int scope_flags) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800351 scope_flags |= SCOPE_SENSITIVE;
352 siginfo_t si;
353 memset(&si, 0, sizeof(si));
354 if (ptrace(PTRACE_GETSIGINFO, tid, 0, &si)) {
355 _LOG(log, scope_flags, "cannot get siginfo for %d: %s\n", tid, strerror(errno));
356 return;
357 }
Elliott Hughes855fcc32014-04-25 16:05:34 -0700358 if (!signal_has_si_addr(si.si_signo)) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800359 return;
360 }
361
Christopher Ferris46756822014-01-14 20:16:30 -0800362 uintptr_t addr = reinterpret_cast<uintptr_t>(si.si_addr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800363 addr &= ~0xfff; // round to 4K page boundary
364 if (addr == 0) { // null-pointer deref
365 return;
366 }
367
Kévin PETITabc60c22013-12-19 12:36:59 +0000368 _LOG(log, scope_flags, "\nmemory map around fault addr %" PRIPTR ":\n",
Christopher Ferris20303f82014-01-10 16:33:16 -0800369 reinterpret_cast<uintptr_t>(si.si_addr));
370
371 // Search for a match, or for a hole where the match would be. The list
372 // is backward from the file content, so it starts at high addresses.
Christopher Ferris46756822014-01-14 20:16:30 -0800373 const backtrace_map_t* cur_map = NULL;
374 const backtrace_map_t* next_map = NULL;
375 const backtrace_map_t* prev_map = NULL;
376 for (BacktraceMap::const_iterator it = map->begin(); it != map->end(); ++it) {
377 if (addr >= it->start && addr < it->end) {
378 cur_map = &*it;
379 if (it != map->begin()) {
380 prev_map = &*(it-1);
381 }
382 if (++it != map->end()) {
383 next_map = &*it;
384 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800385 break;
Jeff Brown053b8652012-06-06 16:25:03 -0700386 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800387 }
Jeff Brown053b8652012-06-06 16:25:03 -0700388
Christopher Ferris46756822014-01-14 20:16:30 -0800389 // Show the map address in ascending order (like /proc/pid/maps).
390 dump_map(log, prev_map, "map below", scope_flags);
391 dump_map(log, cur_map, "map for address", scope_flags);
392 dump_map(log, next_map, "map above", scope_flags);
Jeff Brown053b8652012-06-06 16:25:03 -0700393}
394
Christopher Ferris20303f82014-01-10 16:33:16 -0800395static void dump_thread(
396 Backtrace* backtrace, log_t* log, int scope_flags, int* total_sleep_time_usec) {
397 wait_for_stop(backtrace->Tid(), total_sleep_time_usec);
Jeff Brown053b8652012-06-06 16:25:03 -0700398
Christopher Ferris20303f82014-01-10 16:33:16 -0800399 dump_registers(log, backtrace->Tid(), scope_flags);
400 dump_backtrace_and_stack(backtrace, log, scope_flags);
401 if (IS_AT_FAULT(scope_flags)) {
402 dump_memory_and_code(log, backtrace->Tid(), scope_flags);
Christopher Ferris46756822014-01-14 20:16:30 -0800403 dump_nearby_maps(backtrace->GetMap(), log, backtrace->Tid(), scope_flags);
Christopher Ferris20303f82014-01-10 16:33:16 -0800404 }
Jeff Brown053b8652012-06-06 16:25:03 -0700405}
406
Christopher Ferris20303f82014-01-10 16:33:16 -0800407// Return true if some thread is not detached cleanly
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700408static bool dump_sibling_thread_report(
Christopher Ferris46756822014-01-14 20:16:30 -0800409 log_t* log, pid_t pid, pid_t tid, int* total_sleep_time_usec, BacktraceMap* map) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800410 char task_path[64];
411 snprintf(task_path, sizeof(task_path), "/proc/%d/task", pid);
Jeff Brown053b8652012-06-06 16:25:03 -0700412
Christopher Ferris20303f82014-01-10 16:33:16 -0800413 DIR* d = opendir(task_path);
414 // Bail early if the task directory cannot be opened
415 if (d == NULL) {
416 XLOG("Cannot open /proc/%d/task\n", pid);
417 return false;
418 }
419
420 bool detach_failed = false;
421 struct dirent* de;
422 while ((de = readdir(d)) != NULL) {
423 // Ignore "." and ".."
424 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, "..")) {
425 continue;
Jeff Brown053b8652012-06-06 16:25:03 -0700426 }
427
Christopher Ferris20303f82014-01-10 16:33:16 -0800428 // The main thread at fault has been handled individually
429 char* end;
430 pid_t new_tid = strtoul(de->d_name, &end, 10);
431 if (*end || new_tid == tid) {
432 continue;
Jeff Brown053b8652012-06-06 16:25:03 -0700433 }
434
Christopher Ferris20303f82014-01-10 16:33:16 -0800435 // Skip this thread if cannot ptrace it
436 if (ptrace(PTRACE_ATTACH, new_tid, 0, 0) < 0) {
437 continue;
438 }
439
440 _LOG(log, 0, "--- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---\n");
441 dump_thread_info(log, pid, new_tid, 0);
442
Christopher Ferris46756822014-01-14 20:16:30 -0800443 UniquePtr<Backtrace> backtrace(Backtrace::Create(pid, new_tid, map));
Christopher Ferris20303f82014-01-10 16:33:16 -0800444 if (backtrace->Unwind(0)) {
445 dump_thread(backtrace.get(), log, 0, total_sleep_time_usec);
446 }
447
448 if (ptrace(PTRACE_DETACH, new_tid, 0, 0) != 0) {
449 LOG("ptrace detach from %d failed: %s\n", new_tid, strerror(errno));
450 detach_failed = true;
451 }
452 }
453
454 closedir(d);
455 return detach_failed;
Jeff Brown053b8652012-06-06 16:25:03 -0700456}
457
Christopher Ferris20303f82014-01-10 16:33:16 -0800458// Reads the contents of the specified log device, filters out the entries
459// that don't match the specified pid, and writes them to the tombstone file.
460//
Christopher Ferris4167d7e2014-01-13 18:12:04 -0800461// If "tail" is non-zero, log the last "tail" number of lines.
462static void dump_log_file(
463 log_t* log, pid_t pid, const char* filename, unsigned int tail) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800464 bool first = true;
Christopher Ferris4167d7e2014-01-13 18:12:04 -0800465 struct logger_list* logger_list;
Jeff Brown053b8652012-06-06 16:25:03 -0700466
Christopher Ferris4167d7e2014-01-13 18:12:04 -0800467 logger_list = android_logger_list_open(
468 android_name_to_log_id(filename), O_RDONLY | O_NONBLOCK, tail, pid);
Jeff Brown053b8652012-06-06 16:25:03 -0700469
Christopher Ferris4167d7e2014-01-13 18:12:04 -0800470 if (!logger_list) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800471 XLOG("Unable to open %s: %s\n", filename, strerror(errno));
472 return;
473 }
474
Christopher Ferris4167d7e2014-01-13 18:12:04 -0800475 struct log_msg log_entry;
Christopher Ferris20303f82014-01-10 16:33:16 -0800476
477 while (true) {
Christopher Ferris4167d7e2014-01-13 18:12:04 -0800478 ssize_t actual = android_logger_list_read(logger_list, &log_entry);
479 struct logger_entry* entry;
480
Christopher Ferris20303f82014-01-10 16:33:16 -0800481 if (actual < 0) {
Christopher Ferris4167d7e2014-01-13 18:12:04 -0800482 if (actual == -EINTR) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800483 // interrupted by signal, retry
484 continue;
Christopher Ferris4167d7e2014-01-13 18:12:04 -0800485 } else if (actual == -EAGAIN) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800486 // non-blocking EOF; we're done
487 break;
488 } else {
Christopher Ferris4167d7e2014-01-13 18:12:04 -0800489 _LOG(log, 0, "Error while reading log: %s\n", strerror(-actual));
Christopher Ferris20303f82014-01-10 16:33:16 -0800490 break;
491 }
492 } else if (actual == 0) {
Mark Salyzyn22b5cef2013-11-22 10:53:34 -0800493 _LOG(log, 0, "Got zero bytes while reading log: %s\n",
494 strerror(errno));
Christopher Ferris20303f82014-01-10 16:33:16 -0800495 break;
Jeff Brown053b8652012-06-06 16:25:03 -0700496 }
497
Christopher Ferris20303f82014-01-10 16:33:16 -0800498 // NOTE: if you XLOG something here, this will spin forever,
499 // because you will be writing as fast as you're reading. Any
500 // high-frequency debug diagnostics should just be written to
501 // the tombstone file.
Jeff Brown053b8652012-06-06 16:25:03 -0700502
Christopher Ferris4167d7e2014-01-13 18:12:04 -0800503 entry = &log_entry.entry_v1;
Mark Salyzyna63f9272013-11-22 10:53:34 -0800504
Christopher Ferris20303f82014-01-10 16:33:16 -0800505 if (first) {
Christopher Ferris4167d7e2014-01-13 18:12:04 -0800506 _LOG(log, 0, "--------- %slog %s\n", tail ? "tail end of " : "", filename);
Christopher Ferris20303f82014-01-10 16:33:16 -0800507 first = false;
Jeff Brown053b8652012-06-06 16:25:03 -0700508 }
509
Christopher Ferris20303f82014-01-10 16:33:16 -0800510 // Msg format is: <priority:1><tag:N>\0<message:N>\0
511 //
512 // We want to display it in the same format as "logcat -v threadtime"
513 // (although in this case the pid is redundant).
Christopher Ferris20303f82014-01-10 16:33:16 -0800514 static const char* kPrioChars = "!.VDIWEFS";
Christopher Ferris4167d7e2014-01-13 18:12:04 -0800515 unsigned hdr_size = log_entry.entry.hdr_size;
516 if (!hdr_size) {
517 hdr_size = sizeof(log_entry.entry_v1);
518 }
519 char* msg = reinterpret_cast<char*>(log_entry.buf) + hdr_size;
520 unsigned char prio = msg[0];
521 char* tag = msg + 1;
522 msg = tag + strlen(tag) + 1;
Christopher Ferris20303f82014-01-10 16:33:16 -0800523
524 // consume any trailing newlines
Mark Salyzyn706fad22013-12-12 12:21:20 -0800525 char* nl = msg + strlen(msg) - 1;
526 while (nl >= msg && *nl == '\n') {
Mark Salyzyn8e4cdc12014-01-28 13:51:46 -0800527 *nl-- = '\0';
Christopher Ferris20303f82014-01-10 16:33:16 -0800528 }
529
530 char prioChar = (prio < strlen(kPrioChars) ? kPrioChars[prio] : '?');
531
532 char timeBuf[32];
533 time_t sec = static_cast<time_t>(entry->sec);
534 struct tm tmBuf;
535 struct tm* ptm;
536 ptm = localtime_r(&sec, &tmBuf);
537 strftime(timeBuf, sizeof(timeBuf), "%m-%d %H:%M:%S", ptm);
538
Mark Salyzyn706fad22013-12-12 12:21:20 -0800539 // Look for line breaks ('\n') and display each text line
540 // on a separate line, prefixed with the header, like logcat does.
541 do {
Mark Salyzynfca0bd12013-12-12 12:21:20 -0800542 nl = strchr(msg, '\n');
543 if (nl) {
544 *nl = '\0';
545 ++nl;
546 }
Mark Salyzyn706fad22013-12-12 12:21:20 -0800547
Mark Salyzynfca0bd12013-12-12 12:21:20 -0800548 _LOG(log, 0, "%s.%03d %5d %5d %c %-8s: %s\n",
549 timeBuf, entry->nsec / 1000000, entry->pid, entry->tid,
550 prioChar, tag, msg);
Mark Salyzyn706fad22013-12-12 12:21:20 -0800551 } while ((msg = nl));
Christopher Ferris20303f82014-01-10 16:33:16 -0800552 }
Jeff Brown053b8652012-06-06 16:25:03 -0700553
Christopher Ferris4167d7e2014-01-13 18:12:04 -0800554 android_logger_list_free(logger_list);
Jeff Brown053b8652012-06-06 16:25:03 -0700555}
556
Christopher Ferris20303f82014-01-10 16:33:16 -0800557// Dumps the logs generated by the specified pid to the tombstone, from both
558// "system" and "main" log devices. Ideally we'd interleave the output.
Christopher Ferris4167d7e2014-01-13 18:12:04 -0800559static void dump_logs(log_t* log, pid_t pid, unsigned int tail) {
560 dump_log_file(log, pid, "system", tail);
561 dump_log_file(log, pid, "main", tail);
Jeff Brown053b8652012-06-06 16:25:03 -0700562}
563
Christopher Ferris20303f82014-01-10 16:33:16 -0800564static void dump_abort_message(Backtrace* backtrace, log_t* log, uintptr_t address) {
Elliott Hughese5f8a692013-04-04 13:52:01 -0700565 if (address == 0) {
566 return;
567 }
568
569 address += sizeof(size_t); // Skip the buffer length.
570
571 char msg[512];
572 memset(msg, 0, sizeof(msg));
573 char* p = &msg[0];
574 while (p < &msg[sizeof(msg)]) {
Pavel Chupinc6c194c2013-11-21 23:17:20 +0400575 word_t data;
576 size_t len = sizeof(word_t);
Christopher Ferris20303f82014-01-10 16:33:16 -0800577 if (!backtrace->ReadWord(address, &data)) {
Elliott Hughese5f8a692013-04-04 13:52:01 -0700578 break;
579 }
Pavel Chupinc6c194c2013-11-21 23:17:20 +0400580 address += sizeof(word_t);
Elliott Hughese5f8a692013-04-04 13:52:01 -0700581
Pavel Chupinc6c194c2013-11-21 23:17:20 +0400582 while (len > 0 && (*p++ = (data >> (sizeof(word_t) - len) * 8) & 0xff) != 0)
583 len--;
Elliott Hughese5f8a692013-04-04 13:52:01 -0700584 }
585 msg[sizeof(msg) - 1] = '\0';
586
Christopher Tate7716aef2013-04-02 14:00:27 -0700587 _LOG(log, SCOPE_AT_FAULT, "Abort message: '%s'\n", msg);
Elliott Hughese5f8a692013-04-04 13:52:01 -0700588}
589
Christopher Ferris20303f82014-01-10 16:33:16 -0800590// Dumps all information about the specified pid to the tombstone.
Elliott Hughes855fcc32014-04-25 16:05:34 -0700591static bool dump_crash(log_t* log, pid_t pid, pid_t tid, int signal, int si_code,
592 uintptr_t abort_msg_address, bool dump_sibling_threads,
593 int* total_sleep_time_usec) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800594 // don't copy log messages to tombstone unless this is a dev device
595 char value[PROPERTY_VALUE_MAX];
596 property_get("ro.debuggable", value, "0");
597 bool want_logs = (value[0] == '1');
Jeff Brown053b8652012-06-06 16:25:03 -0700598
Christopher Ferris20303f82014-01-10 16:33:16 -0800599 if (log->amfd >= 0) {
600 // Activity Manager protocol: binary 32-bit network-byte-order ints for the
601 // pid and signal number, followed by the raw text of the dump, culminating
602 // in a zero byte that marks end-of-data.
603 uint32_t datum = htonl(pid);
604 TEMP_FAILURE_RETRY( write(log->amfd, &datum, 4) );
605 datum = htonl(signal);
606 TEMP_FAILURE_RETRY( write(log->amfd, &datum, 4) );
607 }
Christopher Tateded2e5a2013-03-19 13:12:23 -0700608
Christopher Ferris20303f82014-01-10 16:33:16 -0800609 _LOG(log, SCOPE_AT_FAULT,
610 "*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***\n");
611 dump_build_info(log);
612 dump_revision_info(log);
613 dump_thread_info(log, pid, tid, SCOPE_AT_FAULT);
614 if (signal) {
Elliott Hughes855fcc32014-04-25 16:05:34 -0700615 dump_signal_info(log, tid, signal, si_code);
Christopher Ferris20303f82014-01-10 16:33:16 -0800616 }
Jeff Brown053b8652012-06-06 16:25:03 -0700617
Christopher Ferrisdf290612014-01-22 19:21:07 -0800618 UniquePtr<BacktraceMap> map(BacktraceMap::Create(pid));
619 UniquePtr<Backtrace> backtrace(Backtrace::Create(pid, tid, map.get()));
Christopher Ferris20303f82014-01-10 16:33:16 -0800620 if (backtrace->Unwind(0)) {
621 dump_abort_message(backtrace.get(), log, abort_msg_address);
622 dump_thread(backtrace.get(), log, SCOPE_AT_FAULT, total_sleep_time_usec);
623 }
Jeff Brown053b8652012-06-06 16:25:03 -0700624
Christopher Ferris20303f82014-01-10 16:33:16 -0800625 if (want_logs) {
Christopher Ferris4167d7e2014-01-13 18:12:04 -0800626 dump_logs(log, pid, 5);
Christopher Ferris20303f82014-01-10 16:33:16 -0800627 }
Jeff Brown053b8652012-06-06 16:25:03 -0700628
Christopher Ferris20303f82014-01-10 16:33:16 -0800629 bool detach_failed = false;
630 if (dump_sibling_threads) {
Christopher Ferrisdf290612014-01-22 19:21:07 -0800631 detach_failed = dump_sibling_thread_report(log, pid, tid, total_sleep_time_usec, map.get());
Christopher Ferris20303f82014-01-10 16:33:16 -0800632 }
Christopher Ferris98464972014-01-06 19:16:33 -0800633
Christopher Ferris20303f82014-01-10 16:33:16 -0800634 if (want_logs) {
Christopher Ferris4167d7e2014-01-13 18:12:04 -0800635 dump_logs(log, pid, 0);
Christopher Ferris20303f82014-01-10 16:33:16 -0800636 }
Christopher Tateded2e5a2013-03-19 13:12:23 -0700637
Christopher Ferris20303f82014-01-10 16:33:16 -0800638 // send EOD to the Activity Manager, then wait for its ack to avoid racing ahead
639 // and killing the target out from under it
640 if (log->amfd >= 0) {
641 uint8_t eodMarker = 0;
642 TEMP_FAILURE_RETRY( write(log->amfd, &eodMarker, 1) );
643 // 3 sec timeout reading the ack; we're fine if that happens
644 TEMP_FAILURE_RETRY( read(log->amfd, &eodMarker, 1) );
645 }
646
647 return detach_failed;
Jeff Brown053b8652012-06-06 16:25:03 -0700648}
649
Christopher Ferris20303f82014-01-10 16:33:16 -0800650// find_and_open_tombstone - find an available tombstone slot, if any, of the
651// form tombstone_XX where XX is 00 to MAX_TOMBSTONES-1, inclusive. If no
652// file is available, we reuse the least-recently-modified file.
653//
654// Returns the path of the tombstone file, allocated using malloc(). Caller must free() it.
655static char* find_and_open_tombstone(int* fd) {
Christopher Ferris68bd59f2014-02-07 13:25:26 -0800656 // In a single pass, find an available slot and, in case none
Christopher Ferris20303f82014-01-10 16:33:16 -0800657 // exist, find and record the least-recently-modified file.
658 char path[128];
Christopher Ferris68bd59f2014-02-07 13:25:26 -0800659 int oldest = -1;
660 struct stat oldest_sb;
Christopher Ferris20303f82014-01-10 16:33:16 -0800661 for (int i = 0; i < MAX_TOMBSTONES; i++) {
Kévin PETIT4bb47722013-12-18 16:44:24 +0000662 snprintf(path, sizeof(path), TOMBSTONE_TEMPLATE, i);
Jeff Brown053b8652012-06-06 16:25:03 -0700663
Christopher Ferris68bd59f2014-02-07 13:25:26 -0800664 struct stat sb;
Christopher Ferris20303f82014-01-10 16:33:16 -0800665 if (!stat(path, &sb)) {
Christopher Ferris68bd59f2014-02-07 13:25:26 -0800666 if (oldest < 0 || sb.st_mtime < oldest_sb.st_mtime) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800667 oldest = i;
Christopher Ferris68bd59f2014-02-07 13:25:26 -0800668 oldest_sb.st_mtime = sb.st_mtime;
Christopher Ferris20303f82014-01-10 16:33:16 -0800669 }
670 continue;
Jeff Brown053b8652012-06-06 16:25:03 -0700671 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800672 if (errno != ENOENT)
673 continue;
Jeff Brown053b8652012-06-06 16:25:03 -0700674
Christopher Ferris20303f82014-01-10 16:33:16 -0800675 *fd = open(path, O_CREAT | O_EXCL | O_WRONLY, 0600);
676 if (*fd < 0)
677 continue; // raced ?
678
Jeff Brown053b8652012-06-06 16:25:03 -0700679 fchown(*fd, AID_SYSTEM, AID_SYSTEM);
680 return strdup(path);
Christopher Ferris20303f82014-01-10 16:33:16 -0800681 }
682
Christopher Ferris68bd59f2014-02-07 13:25:26 -0800683 if (oldest < 0) {
684 LOG("Failed to find a valid tombstone, default to using tombstone 0.\n");
685 oldest = 0;
686 }
687
Christopher Ferris20303f82014-01-10 16:33:16 -0800688 // we didn't find an available file, so we clobber the oldest one
Kévin PETIT4bb47722013-12-18 16:44:24 +0000689 snprintf(path, sizeof(path), TOMBSTONE_TEMPLATE, oldest);
Christopher Ferris20303f82014-01-10 16:33:16 -0800690 *fd = open(path, O_CREAT | O_TRUNC | O_WRONLY, 0600);
691 if (*fd < 0) {
692 LOG("failed to open tombstone file '%s': %s\n", path, strerror(errno));
693 return NULL;
694 }
695 fchown(*fd, AID_SYSTEM, AID_SYSTEM);
696 return strdup(path);
Jeff Brown053b8652012-06-06 16:25:03 -0700697}
698
Christopher Tateded2e5a2013-03-19 13:12:23 -0700699static int activity_manager_connect() {
Christopher Ferris20303f82014-01-10 16:33:16 -0800700 int amfd = socket(PF_UNIX, SOCK_STREAM, 0);
701 if (amfd >= 0) {
702 struct sockaddr_un address;
703 int err;
Christopher Tateded2e5a2013-03-19 13:12:23 -0700704
Christopher Ferris20303f82014-01-10 16:33:16 -0800705 memset(&address, 0, sizeof(address));
706 address.sun_family = AF_UNIX;
707 strncpy(address.sun_path, NCRASH_SOCKET_PATH, sizeof(address.sun_path));
708 err = TEMP_FAILURE_RETRY(connect(
709 amfd, reinterpret_cast<struct sockaddr*>(&address), sizeof(address)));
710 if (!err) {
711 struct timeval tv;
712 memset(&tv, 0, sizeof(tv));
713 tv.tv_sec = 1; // tight leash
714 err = setsockopt(amfd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
715 if (!err) {
716 tv.tv_sec = 3; // 3 seconds on handshake read
717 err = setsockopt(amfd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
718 }
Christopher Tateded2e5a2013-03-19 13:12:23 -0700719 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800720 if (err) {
721 close(amfd);
722 amfd = -1;
723 }
724 }
Christopher Tateded2e5a2013-03-19 13:12:23 -0700725
Christopher Ferris20303f82014-01-10 16:33:16 -0800726 return amfd;
Christopher Tateded2e5a2013-03-19 13:12:23 -0700727}
728
Elliott Hughes855fcc32014-04-25 16:05:34 -0700729char* engrave_tombstone(pid_t pid, pid_t tid, int signal, int original_si_code,
730 uintptr_t abort_msg_address, bool dump_sibling_threads, bool quiet,
731 bool* detach_failed, int* total_sleep_time_usec) {
Kévin PETIT4bb47722013-12-18 16:44:24 +0000732 if ((mkdir(TOMBSTONE_DIR, 0755) == -1) && (errno != EEXIST)) {
Christopher Ferrisc5bb49a2014-05-05 11:16:47 -0700733 LOG("failed to create %s: %s\n", TOMBSTONE_DIR, strerror(errno));
Kévin PETIT4bb47722013-12-18 16:44:24 +0000734 }
735
736 if (chown(TOMBSTONE_DIR, AID_SYSTEM, AID_SYSTEM) == -1) {
Christopher Ferrisc5bb49a2014-05-05 11:16:47 -0700737 LOG("failed to change ownership of %s: %s\n", TOMBSTONE_DIR, strerror(errno));
Kévin PETIT4bb47722013-12-18 16:44:24 +0000738 }
Jeff Brown053b8652012-06-06 16:25:03 -0700739
Christopher Ferrisc5bb49a2014-05-05 11:16:47 -0700740 int fd = -1;
741 char* path = NULL;
742 if (selinux_android_restorecon(TOMBSTONE_DIR, 0) == 0) {
743 path = find_and_open_tombstone(&fd);
744 } else {
745 LOG("Failed to restore security context, not writing tombstone.\n");
Christopher Ferris20303f82014-01-10 16:33:16 -0800746 }
rpcraigf1186f32012-07-19 09:38:06 -0400747
Christopher Ferrisc5bb49a2014-05-05 11:16:47 -0700748 if (fd < 0 && quiet) {
749 LOG("Skipping tombstone write, nothing to do.\n");
Christopher Ferris20303f82014-01-10 16:33:16 -0800750 *detach_failed = false;
751 return NULL;
752 }
Jeff Brown053b8652012-06-06 16:25:03 -0700753
Christopher Ferris20303f82014-01-10 16:33:16 -0800754 log_t log;
755 log.tfd = fd;
Christopher Ferrisc5bb49a2014-05-05 11:16:47 -0700756 // Preserve amfd since it can be modified through the calls below without
757 // being closed.
758 int amfd = activity_manager_connect();
759 log.amfd = amfd;
Christopher Ferris20303f82014-01-10 16:33:16 -0800760 log.quiet = quiet;
Elliott Hughes855fcc32014-04-25 16:05:34 -0700761 *detach_failed = dump_crash(&log, pid, tid, signal, original_si_code, abort_msg_address,
762 dump_sibling_threads, total_sleep_time_usec);
Jeff Brown053b8652012-06-06 16:25:03 -0700763
Christopher Ferrisc5bb49a2014-05-05 11:16:47 -0700764 // Either of these file descriptors can be -1, any error is ignored.
765 close(amfd);
Christopher Ferris20303f82014-01-10 16:33:16 -0800766 close(fd);
Christopher Ferrisc5bb49a2014-05-05 11:16:47 -0700767
Christopher Ferris20303f82014-01-10 16:33:16 -0800768 return path;
Jeff Brown053b8652012-06-06 16:25:03 -0700769}