blob: fb5f02a4de5b4208f05be3662969fc05be987ae8 [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
Brigid Smith9c8dacc2014-06-02 15:02:20 -070059// Figure out the abi based on defined macros.
60#if defined(__arm__)
61#define ABI_STRING "arm"
62#elif defined(__aarch64__)
63#define ABI_STRING "arm64"
64#elif defined(__mips__)
65#define ABI_STRING "mips"
66#elif defined(__i386__)
67#define ABI_STRING "x86"
68#elif defined(__x86_64__)
69#define ABI_STRING "x86_64"
70#else
71#error "Unsupported ABI"
72#endif
73
Elliott Hughes855fcc32014-04-25 16:05:34 -070074static bool signal_has_si_addr(int sig) {
Christopher Ferris20303f82014-01-10 16:33:16 -080075 switch (sig) {
Christopher Ferris20303f82014-01-10 16:33:16 -080076 case SIGBUS:
Elliott Hughesa323b502014-05-16 21:12:17 -070077 case SIGFPE:
78 case SIGILL:
79 case SIGSEGV:
Christopher Ferris20303f82014-01-10 16:33:16 -080080 return true;
81 default:
82 return false;
83 }
84}
85
86static const char* get_signame(int sig) {
87 switch(sig) {
Christopher Ferris20303f82014-01-10 16:33:16 -080088 case SIGABRT: return "SIGABRT";
89 case SIGBUS: return "SIGBUS";
90 case SIGFPE: return "SIGFPE";
Elliott Hughesa323b502014-05-16 21:12:17 -070091 case SIGILL: return "SIGILL";
Christopher Ferris20303f82014-01-10 16:33:16 -080092 case SIGPIPE: return "SIGPIPE";
Elliott Hughesa323b502014-05-16 21:12:17 -070093 case SIGSEGV: return "SIGSEGV";
Elliott Hughes855fcc32014-04-25 16:05:34 -070094#if defined(SIGSTKFLT)
Christopher Ferris20303f82014-01-10 16:33:16 -080095 case SIGSTKFLT: return "SIGSTKFLT";
96#endif
97 case SIGSTOP: return "SIGSTOP";
Elliott Hughesa323b502014-05-16 21:12:17 -070098 case SIGTRAP: return "SIGTRAP";
Christopher Ferris20303f82014-01-10 16:33:16 -080099 default: return "?";
100 }
101}
102
103static const char* get_sigcode(int signo, int code) {
104 // Try the signal-specific codes...
105 switch (signo) {
106 case SIGILL:
107 switch (code) {
Jeff Brown053b8652012-06-06 16:25:03 -0700108 case ILL_ILLOPC: return "ILL_ILLOPC";
109 case ILL_ILLOPN: return "ILL_ILLOPN";
110 case ILL_ILLADR: return "ILL_ILLADR";
111 case ILL_ILLTRP: return "ILL_ILLTRP";
112 case ILL_PRVOPC: return "ILL_PRVOPC";
113 case ILL_PRVREG: return "ILL_PRVREG";
114 case ILL_COPROC: return "ILL_COPROC";
115 case ILL_BADSTK: return "ILL_BADSTK";
Christopher Ferris20303f82014-01-10 16:33:16 -0800116 }
Elliott Hughesbd395b92014-04-24 13:53:22 -0700117 static_assert(NSIGILL == ILL_BADSTK, "missing ILL_* si_code");
Christopher Ferris20303f82014-01-10 16:33:16 -0800118 break;
Jeff Brown053b8652012-06-06 16:25:03 -0700119 case SIGBUS:
Christopher Ferris20303f82014-01-10 16:33:16 -0800120 switch (code) {
Jeff Brown053b8652012-06-06 16:25:03 -0700121 case BUS_ADRALN: return "BUS_ADRALN";
122 case BUS_ADRERR: return "BUS_ADRERR";
123 case BUS_OBJERR: return "BUS_OBJERR";
Elliott Hughesbd395b92014-04-24 13:53:22 -0700124 case BUS_MCEERR_AR: return "BUS_MCEERR_AR";
125 case BUS_MCEERR_AO: return "BUS_MCEERR_AO";
Christopher Ferris20303f82014-01-10 16:33:16 -0800126 }
Elliott Hughesbd395b92014-04-24 13:53:22 -0700127 static_assert(NSIGBUS == BUS_MCEERR_AO, "missing BUS_* si_code");
Christopher Ferris20303f82014-01-10 16:33:16 -0800128 break;
Jeff Brown053b8652012-06-06 16:25:03 -0700129 case SIGFPE:
Christopher Ferris20303f82014-01-10 16:33:16 -0800130 switch (code) {
Jeff Brown053b8652012-06-06 16:25:03 -0700131 case FPE_INTDIV: return "FPE_INTDIV";
132 case FPE_INTOVF: return "FPE_INTOVF";
133 case FPE_FLTDIV: return "FPE_FLTDIV";
134 case FPE_FLTOVF: return "FPE_FLTOVF";
135 case FPE_FLTUND: return "FPE_FLTUND";
136 case FPE_FLTRES: return "FPE_FLTRES";
137 case FPE_FLTINV: return "FPE_FLTINV";
138 case FPE_FLTSUB: return "FPE_FLTSUB";
Christopher Ferris20303f82014-01-10 16:33:16 -0800139 }
Elliott Hughesbd395b92014-04-24 13:53:22 -0700140 static_assert(NSIGFPE == FPE_FLTSUB, "missing FPE_* si_code");
Christopher Ferris20303f82014-01-10 16:33:16 -0800141 break;
Jeff Brown053b8652012-06-06 16:25:03 -0700142 case SIGSEGV:
Christopher Ferris20303f82014-01-10 16:33:16 -0800143 switch (code) {
Jeff Brown053b8652012-06-06 16:25:03 -0700144 case SEGV_MAPERR: return "SEGV_MAPERR";
145 case SEGV_ACCERR: return "SEGV_ACCERR";
Christopher Ferris20303f82014-01-10 16:33:16 -0800146 }
Elliott Hughesbd395b92014-04-24 13:53:22 -0700147 static_assert(NSIGSEGV == SEGV_ACCERR, "missing SEGV_* si_code");
Christopher Ferris20303f82014-01-10 16:33:16 -0800148 break;
Elliott Hughes8f7d4432012-12-10 10:29:05 -0800149 case SIGTRAP:
Christopher Ferris20303f82014-01-10 16:33:16 -0800150 switch (code) {
Elliott Hughes8f7d4432012-12-10 10:29:05 -0800151 case TRAP_BRKPT: return "TRAP_BRKPT";
152 case TRAP_TRACE: return "TRAP_TRACE";
Elliott Hughesbd395b92014-04-24 13:53:22 -0700153 case TRAP_BRANCH: return "TRAP_BRANCH";
154 case TRAP_HWBKPT: return "TRAP_HWBKPT";
Christopher Ferris20303f82014-01-10 16:33:16 -0800155 }
Elliott Hughesbd395b92014-04-24 13:53:22 -0700156 static_assert(NSIGTRAP == TRAP_HWBKPT, "missing TRAP_* si_code");
Christopher Ferris20303f82014-01-10 16:33:16 -0800157 break;
158 }
159 // Then the other codes...
160 switch (code) {
161 case SI_USER: return "SI_USER";
Christopher Ferris20303f82014-01-10 16:33:16 -0800162 case SI_KERNEL: return "SI_KERNEL";
Christopher Ferris20303f82014-01-10 16:33:16 -0800163 case SI_QUEUE: return "SI_QUEUE";
164 case SI_TIMER: return "SI_TIMER";
165 case SI_MESGQ: return "SI_MESGQ";
Elliott Hughes8f7d4432012-12-10 10:29:05 -0800166 case SI_ASYNCIO: return "SI_ASYNCIO";
Christopher Ferris20303f82014-01-10 16:33:16 -0800167 case SI_SIGIO: return "SI_SIGIO";
Christopher Ferris20303f82014-01-10 16:33:16 -0800168 case SI_TKILL: return "SI_TKILL";
Elliott Hughesbd395b92014-04-24 13:53:22 -0700169 case SI_DETHREAD: return "SI_DETHREAD";
Christopher Ferris20303f82014-01-10 16:33:16 -0800170 }
171 // Then give up...
172 return "?";
Jeff Brown053b8652012-06-06 16:25:03 -0700173}
174
Brigid Smith9c8dacc2014-06-02 15:02:20 -0700175static void dump_header_info(log_t* log) {
176 char fingerprint[PROPERTY_VALUE_MAX];
Christopher Ferris20303f82014-01-10 16:33:16 -0800177 char revision[PROPERTY_VALUE_MAX];
Ben Chengd7760c12012-09-19 16:04:01 -0700178
Brigid Smith9c8dacc2014-06-02 15:02:20 -0700179 property_get("ro.build.fingerprint", fingerprint, "unknown");
Christopher Ferris20303f82014-01-10 16:33:16 -0800180 property_get("ro.revision", revision, "unknown");
Ben Chengd7760c12012-09-19 16:04:01 -0700181
Christopher Ferris20303f82014-01-10 16:33:16 -0800182 _LOG(log, SCOPE_AT_FAULT, "Build fingerprint: '%s'\n", fingerprint);
Brigid Smith9c8dacc2014-06-02 15:02:20 -0700183 _LOG(log, SCOPE_AT_FAULT, "Revision: '%s'\n", revision);
184 _LOG(log, SCOPE_AT_FAULT, "ABI: '%s'\n", ABI_STRING);
Jeff Brown053b8652012-06-06 16:25:03 -0700185}
186
Elliott Hughes855fcc32014-04-25 16:05:34 -0700187static void dump_signal_info(log_t* log, pid_t tid, int signal, int si_code) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800188 siginfo_t si;
Christopher Ferris20303f82014-01-10 16:33:16 -0800189 memset(&si, 0, sizeof(si));
Elliott Hughes855fcc32014-04-25 16:05:34 -0700190 if (ptrace(PTRACE_GETSIGINFO, tid, 0, &si) == -1) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800191 _LOG(log, SCOPE_AT_FAULT, "cannot get siginfo: %s\n", strerror(errno));
Elliott Hughes855fcc32014-04-25 16:05:34 -0700192 return;
Christopher Ferris20303f82014-01-10 16:33:16 -0800193 }
Elliott Hughes855fcc32014-04-25 16:05:34 -0700194
195 // bionic has to re-raise some signals, which overwrites the si_code with SI_TKILL.
196 si.si_code = si_code;
197
198 char addr_desc[32]; // ", fault addr 0x1234"
199 if (signal_has_si_addr(signal)) {
200 snprintf(addr_desc, sizeof(addr_desc), "%p", si.si_addr);
201 } else {
202 snprintf(addr_desc, sizeof(addr_desc), "--------");
203 }
204
205 _LOG(log, SCOPE_AT_FAULT, "signal %d (%s), code %d (%s), fault addr %s\n",
206 signal, get_signame(signal), si.si_code, get_sigcode(signal, si.si_code), addr_desc);
Jeff Brown053b8652012-06-06 16:25:03 -0700207}
208
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700209static void dump_thread_info(log_t* log, pid_t pid, pid_t tid, int scope_flags) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800210 char path[64];
211 char threadnamebuf[1024];
212 char* threadname = NULL;
213 FILE *fp;
Jeff Brown053b8652012-06-06 16:25:03 -0700214
Christopher Ferris20303f82014-01-10 16:33:16 -0800215 snprintf(path, sizeof(path), "/proc/%d/comm", tid);
216 if ((fp = fopen(path, "r"))) {
217 threadname = fgets(threadnamebuf, sizeof(threadnamebuf), fp);
218 fclose(fp);
219 if (threadname) {
220 size_t len = strlen(threadname);
221 if (len && threadname[len - 1] == '\n') {
222 threadname[len - 1] = '\0';
223 }
224 }
225 }
226
227 if (IS_AT_FAULT(scope_flags)) {
228 char procnamebuf[1024];
229 char* procname = NULL;
230
231 snprintf(path, sizeof(path), "/proc/%d/cmdline", pid);
Jeff Brown053b8652012-06-06 16:25:03 -0700232 if ((fp = fopen(path, "r"))) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800233 procname = fgets(procnamebuf, sizeof(procnamebuf), fp);
234 fclose(fp);
Jeff Brown053b8652012-06-06 16:25:03 -0700235 }
236
Christopher Ferris20303f82014-01-10 16:33:16 -0800237 _LOG(log, SCOPE_AT_FAULT, "pid: %d, tid: %d, name: %s >>> %s <<<\n", pid, tid,
238 threadname ? threadname : "UNKNOWN", procname ? procname : "UNKNOWN");
239 } else {
240 _LOG(log, 0, "pid: %d, tid: %d, name: %s\n", pid, tid, threadname ? threadname : "UNKNOWN");
241 }
242}
Jeff Brown053b8652012-06-06 16:25:03 -0700243
Christopher Ferris20303f82014-01-10 16:33:16 -0800244static void dump_stack_segment(
245 Backtrace* backtrace, log_t* log, int scope_flags, uintptr_t* sp, size_t words, int label) {
246 for (size_t i = 0; i < words; i++) {
Pavel Chupinc6c194c2013-11-21 23:17:20 +0400247 word_t stack_content;
Christopher Ferris20303f82014-01-10 16:33:16 -0800248 if (!backtrace->ReadWord(*sp, &stack_content)) {
249 break;
250 }
251
Christopher Ferris46756822014-01-14 20:16:30 -0800252 const backtrace_map_t* map = backtrace->FindMap(stack_content);
253 const char* map_name;
254 if (!map) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800255 map_name = "";
Christopher Ferris46756822014-01-14 20:16:30 -0800256 } else {
257 map_name = map->name.c_str();
Christopher Ferris20303f82014-01-10 16:33:16 -0800258 }
259 uintptr_t offset = 0;
260 std::string func_name(backtrace->GetFunctionName(stack_content, &offset));
261 if (!func_name.empty()) {
262 if (!i && label >= 0) {
263 if (offset) {
Elliott Hughesf7b4b512014-01-31 23:13:55 -0800264 _LOG(log, scope_flags, " #%02d %" PRIPTR " %" PRIPTR " %s (%s+%" PRIuPTR ")\n",
Christopher Ferris20303f82014-01-10 16:33:16 -0800265 label, *sp, stack_content, map_name, func_name.c_str(), offset);
266 } else {
Pavel Chupinc6c194c2013-11-21 23:17:20 +0400267 _LOG(log, scope_flags, " #%02d %" PRIPTR " %" PRIPTR " %s (%s)\n",
Christopher Ferris20303f82014-01-10 16:33:16 -0800268 label, *sp, stack_content, map_name, func_name.c_str());
Jeff Brown053b8652012-06-06 16:25:03 -0700269 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800270 } else {
271 if (offset) {
Elliott Hughesf7b4b512014-01-31 23:13:55 -0800272 _LOG(log, scope_flags, " %" PRIPTR " %" PRIPTR " %s (%s+%" PRIuPTR ")\n",
Christopher Ferris20303f82014-01-10 16:33:16 -0800273 *sp, stack_content, map_name, func_name.c_str(), offset);
274 } else {
Pavel Chupinc6c194c2013-11-21 23:17:20 +0400275 _LOG(log, scope_flags, " %" PRIPTR " %" PRIPTR " %s (%s)\n",
Christopher Ferris20303f82014-01-10 16:33:16 -0800276 *sp, stack_content, map_name, func_name.c_str());
277 }
278 }
Jeff Brown053b8652012-06-06 16:25:03 -0700279 } else {
Christopher Ferris20303f82014-01-10 16:33:16 -0800280 if (!i && label >= 0) {
Pavel Chupinc6c194c2013-11-21 23:17:20 +0400281 _LOG(log, scope_flags, " #%02d %" PRIPTR " %" PRIPTR " %s\n",
Christopher Ferris20303f82014-01-10 16:33:16 -0800282 label, *sp, stack_content, map_name);
283 } else {
Pavel Chupinc6c194c2013-11-21 23:17:20 +0400284 _LOG(log, scope_flags, " %" PRIPTR " %" PRIPTR " %s\n",
Christopher Ferris20303f82014-01-10 16:33:16 -0800285 *sp, stack_content, map_name);
286 }
Jeff Brown053b8652012-06-06 16:25:03 -0700287 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800288
Pavel Chupinc6c194c2013-11-21 23:17:20 +0400289 *sp += sizeof(word_t);
Christopher Ferris20303f82014-01-10 16:33:16 -0800290 }
Jeff Brown053b8652012-06-06 16:25:03 -0700291}
292
Christopher Ferris20303f82014-01-10 16:33:16 -0800293static void dump_stack(Backtrace* backtrace, log_t* log, int scope_flags) {
294 size_t first = 0, last;
295 for (size_t i = 0; i < backtrace->NumFrames(); i++) {
296 const backtrace_frame_data_t* frame = backtrace->GetFrame(i);
297 if (frame->sp) {
298 if (!first) {
299 first = i+1;
300 }
301 last = i;
Jeff Brown053b8652012-06-06 16:25:03 -0700302 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800303 }
304 if (!first) {
305 return;
306 }
307 first--;
308
309 scope_flags |= SCOPE_SENSITIVE;
310
311 // Dump a few words before the first frame.
Pavel Chupinc6c194c2013-11-21 23:17:20 +0400312 word_t sp = backtrace->GetFrame(first)->sp - STACK_WORDS * sizeof(word_t);
Christopher Ferris20303f82014-01-10 16:33:16 -0800313 dump_stack_segment(backtrace, log, scope_flags, &sp, STACK_WORDS, -1);
314
315 // Dump a few words from all successive frames.
316 // Only log the first 3 frames, put the rest in the tombstone.
317 for (size_t i = first; i <= last; i++) {
318 const backtrace_frame_data_t* frame = backtrace->GetFrame(i);
319 if (sp != frame->sp) {
320 _LOG(log, scope_flags, " ........ ........\n");
321 sp = frame->sp;
322 }
323 if (i - first == 3) {
324 scope_flags &= (~SCOPE_AT_FAULT);
325 }
326 if (i == last) {
327 dump_stack_segment(backtrace, log, scope_flags, &sp, STACK_WORDS, i);
328 if (sp < frame->sp + frame->stack_size) {
329 _LOG(log, scope_flags, " ........ ........\n");
330 }
331 } else {
Pavel Chupinc6c194c2013-11-21 23:17:20 +0400332 size_t words = frame->stack_size / sizeof(word_t);
Christopher Ferris20303f82014-01-10 16:33:16 -0800333 if (words == 0) {
334 words = 1;
335 } else if (words > STACK_WORDS) {
336 words = STACK_WORDS;
337 }
338 dump_stack_segment(backtrace, log, scope_flags, &sp, words, i);
339 }
340 }
Jeff Brown053b8652012-06-06 16:25:03 -0700341}
342
Christopher Ferris20303f82014-01-10 16:33:16 -0800343static void dump_backtrace_and_stack(Backtrace* backtrace, log_t* log, int scope_flags) {
344 if (backtrace->NumFrames()) {
345 _LOG(log, scope_flags, "\nbacktrace:\n");
346 dump_backtrace_to_log(backtrace, log, scope_flags, " ");
Jeff Brown053b8652012-06-06 16:25:03 -0700347
Christopher Ferris20303f82014-01-10 16:33:16 -0800348 _LOG(log, scope_flags, "\nstack:\n");
349 dump_stack(backtrace, log, scope_flags);
350 }
Jeff Brown053b8652012-06-06 16:25:03 -0700351}
352
Christopher Ferris46756822014-01-14 20:16:30 -0800353static void dump_map(log_t* log, const backtrace_map_t* map, const char* what, int scope_flags) {
354 if (map != NULL) {
Pavel Chupinc6c194c2013-11-21 23:17:20 +0400355 _LOG(log, scope_flags, " %" PRIPTR "-%" PRIPTR " %c%c%c %s\n", map->start, map->end,
Christopher Ferris46756822014-01-14 20:16:30 -0800356 (map->flags & PROT_READ) ? 'r' : '-', (map->flags & PROT_WRITE) ? 'w' : '-',
357 (map->flags & PROT_EXEC) ? 'x' : '-', map->name.c_str());
Christopher Ferris20303f82014-01-10 16:33:16 -0800358 } else {
359 _LOG(log, scope_flags, " (no %s)\n", what);
360 }
Elliott Hughesd1420be2013-01-03 13:39:57 -0800361}
362
Christopher Ferris46756822014-01-14 20:16:30 -0800363static void dump_nearby_maps(BacktraceMap* map, log_t* log, pid_t tid, int scope_flags) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800364 scope_flags |= SCOPE_SENSITIVE;
365 siginfo_t si;
366 memset(&si, 0, sizeof(si));
367 if (ptrace(PTRACE_GETSIGINFO, tid, 0, &si)) {
368 _LOG(log, scope_flags, "cannot get siginfo for %d: %s\n", tid, strerror(errno));
369 return;
370 }
Elliott Hughes855fcc32014-04-25 16:05:34 -0700371 if (!signal_has_si_addr(si.si_signo)) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800372 return;
373 }
374
Christopher Ferris46756822014-01-14 20:16:30 -0800375 uintptr_t addr = reinterpret_cast<uintptr_t>(si.si_addr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800376 addr &= ~0xfff; // round to 4K page boundary
377 if (addr == 0) { // null-pointer deref
378 return;
379 }
380
Kévin PETITabc60c22013-12-19 12:36:59 +0000381 _LOG(log, scope_flags, "\nmemory map around fault addr %" PRIPTR ":\n",
Christopher Ferris20303f82014-01-10 16:33:16 -0800382 reinterpret_cast<uintptr_t>(si.si_addr));
383
384 // Search for a match, or for a hole where the match would be. The list
385 // is backward from the file content, so it starts at high addresses.
Christopher Ferris46756822014-01-14 20:16:30 -0800386 const backtrace_map_t* cur_map = NULL;
387 const backtrace_map_t* next_map = NULL;
388 const backtrace_map_t* prev_map = NULL;
389 for (BacktraceMap::const_iterator it = map->begin(); it != map->end(); ++it) {
390 if (addr >= it->start && addr < it->end) {
391 cur_map = &*it;
392 if (it != map->begin()) {
393 prev_map = &*(it-1);
394 }
395 if (++it != map->end()) {
396 next_map = &*it;
397 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800398 break;
Jeff Brown053b8652012-06-06 16:25:03 -0700399 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800400 }
Jeff Brown053b8652012-06-06 16:25:03 -0700401
Christopher Ferris46756822014-01-14 20:16:30 -0800402 // Show the map address in ascending order (like /proc/pid/maps).
403 dump_map(log, prev_map, "map below", scope_flags);
404 dump_map(log, cur_map, "map for address", scope_flags);
405 dump_map(log, next_map, "map above", scope_flags);
Jeff Brown053b8652012-06-06 16:25:03 -0700406}
407
Christopher Ferris20303f82014-01-10 16:33:16 -0800408static void dump_thread(
409 Backtrace* backtrace, log_t* log, int scope_flags, int* total_sleep_time_usec) {
410 wait_for_stop(backtrace->Tid(), total_sleep_time_usec);
Jeff Brown053b8652012-06-06 16:25:03 -0700411
Christopher Ferris20303f82014-01-10 16:33:16 -0800412 dump_registers(log, backtrace->Tid(), scope_flags);
413 dump_backtrace_and_stack(backtrace, log, scope_flags);
414 if (IS_AT_FAULT(scope_flags)) {
415 dump_memory_and_code(log, backtrace->Tid(), scope_flags);
Christopher Ferris46756822014-01-14 20:16:30 -0800416 dump_nearby_maps(backtrace->GetMap(), log, backtrace->Tid(), scope_flags);
Christopher Ferris20303f82014-01-10 16:33:16 -0800417 }
Jeff Brown053b8652012-06-06 16:25:03 -0700418}
419
Christopher Ferris20303f82014-01-10 16:33:16 -0800420// Return true if some thread is not detached cleanly
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700421static bool dump_sibling_thread_report(
Christopher Ferris46756822014-01-14 20:16:30 -0800422 log_t* log, pid_t pid, pid_t tid, int* total_sleep_time_usec, BacktraceMap* map) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800423 char task_path[64];
424 snprintf(task_path, sizeof(task_path), "/proc/%d/task", pid);
Jeff Brown053b8652012-06-06 16:25:03 -0700425
Christopher Ferris20303f82014-01-10 16:33:16 -0800426 DIR* d = opendir(task_path);
427 // Bail early if the task directory cannot be opened
428 if (d == NULL) {
429 XLOG("Cannot open /proc/%d/task\n", pid);
430 return false;
431 }
432
433 bool detach_failed = false;
434 struct dirent* de;
435 while ((de = readdir(d)) != NULL) {
436 // Ignore "." and ".."
437 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, "..")) {
438 continue;
Jeff Brown053b8652012-06-06 16:25:03 -0700439 }
440
Christopher Ferris20303f82014-01-10 16:33:16 -0800441 // The main thread at fault has been handled individually
442 char* end;
443 pid_t new_tid = strtoul(de->d_name, &end, 10);
444 if (*end || new_tid == tid) {
445 continue;
Jeff Brown053b8652012-06-06 16:25:03 -0700446 }
447
Christopher Ferris20303f82014-01-10 16:33:16 -0800448 // Skip this thread if cannot ptrace it
449 if (ptrace(PTRACE_ATTACH, new_tid, 0, 0) < 0) {
450 continue;
451 }
452
453 _LOG(log, 0, "--- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---\n");
454 dump_thread_info(log, pid, new_tid, 0);
455
Christopher Ferris46756822014-01-14 20:16:30 -0800456 UniquePtr<Backtrace> backtrace(Backtrace::Create(pid, new_tid, map));
Christopher Ferris20303f82014-01-10 16:33:16 -0800457 if (backtrace->Unwind(0)) {
458 dump_thread(backtrace.get(), log, 0, total_sleep_time_usec);
459 }
460
461 if (ptrace(PTRACE_DETACH, new_tid, 0, 0) != 0) {
462 LOG("ptrace detach from %d failed: %s\n", new_tid, strerror(errno));
463 detach_failed = true;
464 }
465 }
466
467 closedir(d);
468 return detach_failed;
Jeff Brown053b8652012-06-06 16:25:03 -0700469}
470
Christopher Ferris20303f82014-01-10 16:33:16 -0800471// Reads the contents of the specified log device, filters out the entries
472// that don't match the specified pid, and writes them to the tombstone file.
473//
Mark Salyzyn22b5cef2013-11-22 10:53:34 -0800474// If "tail" is set, we only print the last few lines.
Mark Salyzyn989980c2014-05-14 12:37:22 -0700475static EventTagMap* g_eventTagMap = NULL;
476
Mark Salyzyn22b5cef2013-11-22 10:53:34 -0800477static void dump_log_file(log_t* log, pid_t pid, const char* filename,
478 unsigned int tail) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800479 bool first = true;
Mark Salyzyn22b5cef2013-11-22 10:53:34 -0800480 struct logger_list *logger_list;
Jeff Brown053b8652012-06-06 16:25:03 -0700481
Mark Salyzyn22b5cef2013-11-22 10:53:34 -0800482 logger_list = android_logger_list_open(
483 android_name_to_log_id(filename), O_RDONLY | O_NONBLOCK, tail, pid);
Jeff Brown053b8652012-06-06 16:25:03 -0700484
Mark Salyzyn22b5cef2013-11-22 10:53:34 -0800485 if (!logger_list) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800486 XLOG("Unable to open %s: %s\n", filename, strerror(errno));
487 return;
488 }
489
Mark Salyzyn22b5cef2013-11-22 10:53:34 -0800490 struct log_msg log_entry;
Christopher Ferris20303f82014-01-10 16:33:16 -0800491
492 while (true) {
Mark Salyzyn22b5cef2013-11-22 10:53:34 -0800493 ssize_t actual = android_logger_list_read(logger_list, &log_entry);
494
Christopher Ferris20303f82014-01-10 16:33:16 -0800495 if (actual < 0) {
Mark Salyzyn22b5cef2013-11-22 10:53:34 -0800496 if (actual == -EINTR) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800497 // interrupted by signal, retry
498 continue;
Mark Salyzyn22b5cef2013-11-22 10:53:34 -0800499 } else if (actual == -EAGAIN) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800500 // non-blocking EOF; we're done
501 break;
502 } else {
Mark Salyzyn22b5cef2013-11-22 10:53:34 -0800503 _LOG(log, 0, "Error while reading log: %s\n",
504 strerror(-actual));
Christopher Ferris20303f82014-01-10 16:33:16 -0800505 break;
506 }
507 } else if (actual == 0) {
Mark Salyzyn22b5cef2013-11-22 10:53:34 -0800508 _LOG(log, 0, "Got zero bytes while reading log: %s\n",
509 strerror(errno));
Christopher Ferris20303f82014-01-10 16:33:16 -0800510 break;
Jeff Brown053b8652012-06-06 16:25:03 -0700511 }
512
Christopher Ferris20303f82014-01-10 16:33:16 -0800513 // NOTE: if you XLOG something here, this will spin forever,
514 // because you will be writing as fast as you're reading. Any
515 // high-frequency debug diagnostics should just be written to
516 // the tombstone file.
Mark Salyzyn22b5cef2013-11-22 10:53:34 -0800517 struct logger_entry* entry = &log_entry.entry_v1;
Jeff Brown053b8652012-06-06 16:25:03 -0700518
Christopher Ferris20303f82014-01-10 16:33:16 -0800519 if (entry->pid != static_cast<int32_t>(pid)) {
520 // wrong pid, ignore
521 continue;
Jeff Brown053b8652012-06-06 16:25:03 -0700522 }
523
Christopher Ferris20303f82014-01-10 16:33:16 -0800524 if (first) {
Mark Salyzyn22b5cef2013-11-22 10:53:34 -0800525 _LOG(log, 0, "--------- %slog %s\n",
526 tail ? "tail end of " : "", filename);
Christopher Ferris20303f82014-01-10 16:33:16 -0800527 first = false;
528 }
529
530 // Msg format is: <priority:1><tag:N>\0<message:N>\0
531 //
532 // We want to display it in the same format as "logcat -v threadtime"
533 // (although in this case the pid is redundant).
Christopher Ferris20303f82014-01-10 16:33:16 -0800534 static const char* kPrioChars = "!.VDIWEFS";
Mark Salyzyn22b5cef2013-11-22 10:53:34 -0800535 unsigned hdr_size = log_entry.entry.hdr_size;
536 if (!hdr_size) {
537 hdr_size = sizeof(log_entry.entry_v1);
538 }
Mark Salyzyn989980c2014-05-14 12:37:22 -0700539 char* msg = reinterpret_cast<char*>(log_entry.buf) + hdr_size;
540
541 char timeBuf[32];
542 time_t sec = static_cast<time_t>(entry->sec);
543 struct tm tmBuf;
544 struct tm* ptm;
545 ptm = localtime_r(&sec, &tmBuf);
546 strftime(timeBuf, sizeof(timeBuf), "%m-%d %H:%M:%S", ptm);
547
548 if (log_entry.id() == LOG_ID_EVENTS) {
549 if (!g_eventTagMap) {
550 g_eventTagMap = android_openEventTagMap(EVENT_TAG_MAP_FILE);
551 }
552 AndroidLogEntry e;
553 char buf[512];
554 android_log_processBinaryLogBuffer(entry, &e, g_eventTagMap, buf, sizeof(buf));
555 _LOG(log, 0, "%s.%03d %5d %5d %c %-8s: %s\n",
556 timeBuf, entry->nsec / 1000000, entry->pid, entry->tid,
557 'I', e.tag, e.message);
558 continue;
559 }
560
Mark Salyzyn22b5cef2013-11-22 10:53:34 -0800561 unsigned char prio = msg[0];
562 char* tag = msg + 1;
563 msg = tag + strlen(tag) + 1;
Christopher Ferris20303f82014-01-10 16:33:16 -0800564
565 // consume any trailing newlines
Mark Salyzynfca0bd12013-12-12 12:21:20 -0800566 char* nl = msg + strlen(msg) - 1;
567 while (nl >= msg && *nl == '\n') {
568 *nl-- = '\0';
Christopher Ferris20303f82014-01-10 16:33:16 -0800569 }
570
571 char prioChar = (prio < strlen(kPrioChars) ? kPrioChars[prio] : '?');
572
Mark Salyzynfca0bd12013-12-12 12:21:20 -0800573 // Look for line breaks ('\n') and display each text line
574 // on a separate line, prefixed with the header, like logcat does.
575 do {
576 nl = strchr(msg, '\n');
577 if (nl) {
578 *nl = '\0';
579 ++nl;
580 }
581
582 _LOG(log, 0, "%s.%03d %5d %5d %c %-8s: %s\n",
583 timeBuf, entry->nsec / 1000000, entry->pid, entry->tid,
584 prioChar, tag, msg);
585
586 } while ((msg = nl));
Christopher Ferris20303f82014-01-10 16:33:16 -0800587 }
Jeff Brown053b8652012-06-06 16:25:03 -0700588
Mark Salyzyn22b5cef2013-11-22 10:53:34 -0800589 android_logger_list_free(logger_list);
Jeff Brown053b8652012-06-06 16:25:03 -0700590}
591
Christopher Ferris20303f82014-01-10 16:33:16 -0800592// Dumps the logs generated by the specified pid to the tombstone, from both
593// "system" and "main" log devices. Ideally we'd interleave the output.
Mark Salyzyn22b5cef2013-11-22 10:53:34 -0800594static void dump_logs(log_t* log, pid_t pid, unsigned tail) {
595 dump_log_file(log, pid, "system", tail);
596 dump_log_file(log, pid, "main", tail);
Jeff Brown053b8652012-06-06 16:25:03 -0700597}
598
Christopher Ferris20303f82014-01-10 16:33:16 -0800599static void dump_abort_message(Backtrace* backtrace, log_t* log, uintptr_t address) {
Elliott Hughese5f8a692013-04-04 13:52:01 -0700600 if (address == 0) {
601 return;
602 }
603
604 address += sizeof(size_t); // Skip the buffer length.
605
606 char msg[512];
607 memset(msg, 0, sizeof(msg));
608 char* p = &msg[0];
609 while (p < &msg[sizeof(msg)]) {
Pavel Chupinc6c194c2013-11-21 23:17:20 +0400610 word_t data;
611 size_t len = sizeof(word_t);
Christopher Ferris20303f82014-01-10 16:33:16 -0800612 if (!backtrace->ReadWord(address, &data)) {
Elliott Hughese5f8a692013-04-04 13:52:01 -0700613 break;
614 }
Pavel Chupinc6c194c2013-11-21 23:17:20 +0400615 address += sizeof(word_t);
Elliott Hughese5f8a692013-04-04 13:52:01 -0700616
Pavel Chupinc6c194c2013-11-21 23:17:20 +0400617 while (len > 0 && (*p++ = (data >> (sizeof(word_t) - len) * 8) & 0xff) != 0)
618 len--;
Elliott Hughese5f8a692013-04-04 13:52:01 -0700619 }
620 msg[sizeof(msg) - 1] = '\0';
621
Christopher Tate7716aef2013-04-02 14:00:27 -0700622 _LOG(log, SCOPE_AT_FAULT, "Abort message: '%s'\n", msg);
Elliott Hughese5f8a692013-04-04 13:52:01 -0700623}
624
Christopher Ferris20303f82014-01-10 16:33:16 -0800625// Dumps all information about the specified pid to the tombstone.
Elliott Hughes855fcc32014-04-25 16:05:34 -0700626static bool dump_crash(log_t* log, pid_t pid, pid_t tid, int signal, int si_code,
627 uintptr_t abort_msg_address, bool dump_sibling_threads,
628 int* total_sleep_time_usec) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800629 // don't copy log messages to tombstone unless this is a dev device
630 char value[PROPERTY_VALUE_MAX];
631 property_get("ro.debuggable", value, "0");
632 bool want_logs = (value[0] == '1');
Jeff Brown053b8652012-06-06 16:25:03 -0700633
Christopher Ferris20303f82014-01-10 16:33:16 -0800634 if (log->amfd >= 0) {
635 // Activity Manager protocol: binary 32-bit network-byte-order ints for the
636 // pid and signal number, followed by the raw text of the dump, culminating
637 // in a zero byte that marks end-of-data.
638 uint32_t datum = htonl(pid);
639 TEMP_FAILURE_RETRY( write(log->amfd, &datum, 4) );
640 datum = htonl(signal);
641 TEMP_FAILURE_RETRY( write(log->amfd, &datum, 4) );
642 }
Christopher Tateded2e5a2013-03-19 13:12:23 -0700643
Christopher Ferris20303f82014-01-10 16:33:16 -0800644 _LOG(log, SCOPE_AT_FAULT,
645 "*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***\n");
Brigid Smith9c8dacc2014-06-02 15:02:20 -0700646 dump_header_info(log);
Christopher Ferris20303f82014-01-10 16:33:16 -0800647 dump_thread_info(log, pid, tid, SCOPE_AT_FAULT);
648 if (signal) {
Elliott Hughes855fcc32014-04-25 16:05:34 -0700649 dump_signal_info(log, tid, signal, si_code);
Christopher Ferris20303f82014-01-10 16:33:16 -0800650 }
Jeff Brown053b8652012-06-06 16:25:03 -0700651
Christopher Ferrisdf290612014-01-22 19:21:07 -0800652 UniquePtr<BacktraceMap> map(BacktraceMap::Create(pid));
653 UniquePtr<Backtrace> backtrace(Backtrace::Create(pid, tid, map.get()));
Christopher Ferris20303f82014-01-10 16:33:16 -0800654 if (backtrace->Unwind(0)) {
655 dump_abort_message(backtrace.get(), log, abort_msg_address);
656 dump_thread(backtrace.get(), log, SCOPE_AT_FAULT, total_sleep_time_usec);
657 }
Jeff Brown053b8652012-06-06 16:25:03 -0700658
Christopher Ferris20303f82014-01-10 16:33:16 -0800659 if (want_logs) {
Mark Salyzyn22b5cef2013-11-22 10:53:34 -0800660 dump_logs(log, pid, 5);
Christopher Ferris20303f82014-01-10 16:33:16 -0800661 }
Jeff Brown053b8652012-06-06 16:25:03 -0700662
Christopher Ferris20303f82014-01-10 16:33:16 -0800663 bool detach_failed = false;
664 if (dump_sibling_threads) {
Christopher Ferrisdf290612014-01-22 19:21:07 -0800665 detach_failed = dump_sibling_thread_report(log, pid, tid, total_sleep_time_usec, map.get());
Christopher Ferris20303f82014-01-10 16:33:16 -0800666 }
Christopher Ferris98464972014-01-06 19:16:33 -0800667
Christopher Ferris20303f82014-01-10 16:33:16 -0800668 if (want_logs) {
Mark Salyzyn22b5cef2013-11-22 10:53:34 -0800669 dump_logs(log, pid, 0);
Christopher Ferris20303f82014-01-10 16:33:16 -0800670 }
Christopher Tateded2e5a2013-03-19 13:12:23 -0700671
Christopher Ferris20303f82014-01-10 16:33:16 -0800672 // send EOD to the Activity Manager, then wait for its ack to avoid racing ahead
673 // and killing the target out from under it
674 if (log->amfd >= 0) {
675 uint8_t eodMarker = 0;
676 TEMP_FAILURE_RETRY( write(log->amfd, &eodMarker, 1) );
677 // 3 sec timeout reading the ack; we're fine if that happens
678 TEMP_FAILURE_RETRY( read(log->amfd, &eodMarker, 1) );
679 }
680
681 return detach_failed;
Jeff Brown053b8652012-06-06 16:25:03 -0700682}
683
Christopher Ferris20303f82014-01-10 16:33:16 -0800684// find_and_open_tombstone - find an available tombstone slot, if any, of the
685// form tombstone_XX where XX is 00 to MAX_TOMBSTONES-1, inclusive. If no
686// file is available, we reuse the least-recently-modified file.
687//
688// Returns the path of the tombstone file, allocated using malloc(). Caller must free() it.
689static char* find_and_open_tombstone(int* fd) {
Christopher Ferris68bd59f2014-02-07 13:25:26 -0800690 // In a single pass, find an available slot and, in case none
Christopher Ferris20303f82014-01-10 16:33:16 -0800691 // exist, find and record the least-recently-modified file.
692 char path[128];
Christopher Ferris68bd59f2014-02-07 13:25:26 -0800693 int oldest = -1;
694 struct stat oldest_sb;
Christopher Ferris20303f82014-01-10 16:33:16 -0800695 for (int i = 0; i < MAX_TOMBSTONES; i++) {
Kévin PETIT4bb47722013-12-18 16:44:24 +0000696 snprintf(path, sizeof(path), TOMBSTONE_TEMPLATE, i);
Jeff Brown053b8652012-06-06 16:25:03 -0700697
Christopher Ferris68bd59f2014-02-07 13:25:26 -0800698 struct stat sb;
Christopher Ferris20303f82014-01-10 16:33:16 -0800699 if (!stat(path, &sb)) {
Christopher Ferris68bd59f2014-02-07 13:25:26 -0800700 if (oldest < 0 || sb.st_mtime < oldest_sb.st_mtime) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800701 oldest = i;
Christopher Ferris68bd59f2014-02-07 13:25:26 -0800702 oldest_sb.st_mtime = sb.st_mtime;
Christopher Ferris20303f82014-01-10 16:33:16 -0800703 }
704 continue;
Jeff Brown053b8652012-06-06 16:25:03 -0700705 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800706 if (errno != ENOENT)
707 continue;
Jeff Brown053b8652012-06-06 16:25:03 -0700708
Christopher Ferris20303f82014-01-10 16:33:16 -0800709 *fd = open(path, O_CREAT | O_EXCL | O_WRONLY, 0600);
710 if (*fd < 0)
711 continue; // raced ?
712
Jeff Brown053b8652012-06-06 16:25:03 -0700713 fchown(*fd, AID_SYSTEM, AID_SYSTEM);
714 return strdup(path);
Christopher Ferris20303f82014-01-10 16:33:16 -0800715 }
716
Christopher Ferris68bd59f2014-02-07 13:25:26 -0800717 if (oldest < 0) {
718 LOG("Failed to find a valid tombstone, default to using tombstone 0.\n");
719 oldest = 0;
720 }
721
Christopher Ferris20303f82014-01-10 16:33:16 -0800722 // we didn't find an available file, so we clobber the oldest one
Kévin PETIT4bb47722013-12-18 16:44:24 +0000723 snprintf(path, sizeof(path), TOMBSTONE_TEMPLATE, oldest);
Christopher Ferris20303f82014-01-10 16:33:16 -0800724 *fd = open(path, O_CREAT | O_TRUNC | O_WRONLY, 0600);
725 if (*fd < 0) {
726 LOG("failed to open tombstone file '%s': %s\n", path, strerror(errno));
727 return NULL;
728 }
729 fchown(*fd, AID_SYSTEM, AID_SYSTEM);
730 return strdup(path);
Jeff Brown053b8652012-06-06 16:25:03 -0700731}
732
Christopher Tateded2e5a2013-03-19 13:12:23 -0700733static int activity_manager_connect() {
Christopher Ferris20303f82014-01-10 16:33:16 -0800734 int amfd = socket(PF_UNIX, SOCK_STREAM, 0);
735 if (amfd >= 0) {
736 struct sockaddr_un address;
737 int err;
Christopher Tateded2e5a2013-03-19 13:12:23 -0700738
Christopher Ferris20303f82014-01-10 16:33:16 -0800739 memset(&address, 0, sizeof(address));
740 address.sun_family = AF_UNIX;
741 strncpy(address.sun_path, NCRASH_SOCKET_PATH, sizeof(address.sun_path));
742 err = TEMP_FAILURE_RETRY(connect(
743 amfd, reinterpret_cast<struct sockaddr*>(&address), sizeof(address)));
744 if (!err) {
745 struct timeval tv;
746 memset(&tv, 0, sizeof(tv));
747 tv.tv_sec = 1; // tight leash
748 err = setsockopt(amfd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
749 if (!err) {
750 tv.tv_sec = 3; // 3 seconds on handshake read
751 err = setsockopt(amfd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
752 }
Christopher Tateded2e5a2013-03-19 13:12:23 -0700753 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800754 if (err) {
755 close(amfd);
756 amfd = -1;
757 }
758 }
Christopher Tateded2e5a2013-03-19 13:12:23 -0700759
Christopher Ferris20303f82014-01-10 16:33:16 -0800760 return amfd;
Christopher Tateded2e5a2013-03-19 13:12:23 -0700761}
762
Elliott Hughes855fcc32014-04-25 16:05:34 -0700763char* engrave_tombstone(pid_t pid, pid_t tid, int signal, int original_si_code,
764 uintptr_t abort_msg_address, bool dump_sibling_threads, bool quiet,
765 bool* detach_failed, int* total_sleep_time_usec) {
Kévin PETIT4bb47722013-12-18 16:44:24 +0000766 if ((mkdir(TOMBSTONE_DIR, 0755) == -1) && (errno != EEXIST)) {
Christopher Ferrisc5bb49a2014-05-05 11:16:47 -0700767 LOG("failed to create %s: %s\n", TOMBSTONE_DIR, strerror(errno));
Kévin PETIT4bb47722013-12-18 16:44:24 +0000768 }
769
770 if (chown(TOMBSTONE_DIR, AID_SYSTEM, AID_SYSTEM) == -1) {
Christopher Ferrisc5bb49a2014-05-05 11:16:47 -0700771 LOG("failed to change ownership of %s: %s\n", TOMBSTONE_DIR, strerror(errno));
Kévin PETIT4bb47722013-12-18 16:44:24 +0000772 }
Jeff Brown053b8652012-06-06 16:25:03 -0700773
Christopher Ferrisc5bb49a2014-05-05 11:16:47 -0700774 int fd = -1;
775 char* path = NULL;
776 if (selinux_android_restorecon(TOMBSTONE_DIR, 0) == 0) {
777 path = find_and_open_tombstone(&fd);
778 } else {
779 LOG("Failed to restore security context, not writing tombstone.\n");
Christopher Ferris20303f82014-01-10 16:33:16 -0800780 }
rpcraigf1186f32012-07-19 09:38:06 -0400781
Christopher Ferrisc5bb49a2014-05-05 11:16:47 -0700782 if (fd < 0 && quiet) {
783 LOG("Skipping tombstone write, nothing to do.\n");
Christopher Ferris20303f82014-01-10 16:33:16 -0800784 *detach_failed = false;
785 return NULL;
786 }
Jeff Brown053b8652012-06-06 16:25:03 -0700787
Christopher Ferris20303f82014-01-10 16:33:16 -0800788 log_t log;
789 log.tfd = fd;
Christopher Ferrisc5bb49a2014-05-05 11:16:47 -0700790 // Preserve amfd since it can be modified through the calls below without
791 // being closed.
792 int amfd = activity_manager_connect();
793 log.amfd = amfd;
Christopher Ferris20303f82014-01-10 16:33:16 -0800794 log.quiet = quiet;
Elliott Hughes855fcc32014-04-25 16:05:34 -0700795 *detach_failed = dump_crash(&log, pid, tid, signal, original_si_code, abort_msg_address,
796 dump_sibling_threads, total_sleep_time_usec);
Jeff Brown053b8652012-06-06 16:25:03 -0700797
Christopher Ferrisc5bb49a2014-05-05 11:16:47 -0700798 // Either of these file descriptors can be -1, any error is ignored.
799 close(amfd);
Christopher Ferris20303f82014-01-10 16:33:16 -0800800 close(fd);
Christopher Ferrisc5bb49a2014-05-05 11:16:47 -0700801
Christopher Ferris20303f82014-01-10 16:33:16 -0800802 return path;
Jeff Brown053b8652012-06-06 16:25:03 -0700803}