| Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 1 | /* | 
| Mark Salyzyn | fca0bd1 | 2013-12-12 12:21:20 -0800 | [diff] [blame] | 2 | * Copyright (C) 2012-2014 The Android Open Source Project | 
| Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 3 | * | 
|  | 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 Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 17 | #include <dirent.h> | 
| Kévin PETIT | 4bb4772 | 2013-12-18 16:44:24 +0000 | [diff] [blame] | 18 | #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 Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 26 | #include <time.h> | 
|  | 27 | #include <sys/ptrace.h> | 
| Kévin PETIT | 4bb4772 | 2013-12-18 16:44:24 +0000 | [diff] [blame] | 28 | #include <sys/socket.h> | 
| Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 29 | #include <sys/stat.h> | 
| Kévin PETIT | 4bb4772 | 2013-12-18 16:44:24 +0000 | [diff] [blame] | 30 | #include <sys/un.h> | 
| Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 31 |  | 
|  | 32 | #include <private/android_filesystem_config.h> | 
|  | 33 |  | 
| Mark Salyzyn | 989980c | 2014-05-14 12:37:22 -0700 | [diff] [blame] | 34 | #include <cutils/properties.h> | 
| Mark Salyzyn | 22b5cef | 2013-11-22 10:53:34 -0800 | [diff] [blame] | 35 | #include <log/log.h> | 
| Colin Cross | 9227bd3 | 2013-07-23 16:59:20 -0700 | [diff] [blame] | 36 | #include <log/logger.h> | 
| Mark Salyzyn | 989980c | 2014-05-14 12:37:22 -0700 | [diff] [blame] | 37 | #include <log/logprint.h> | 
| Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 38 |  | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 39 | #include <backtrace/Backtrace.h> | 
| Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 40 | #include <backtrace/BacktraceMap.h> | 
| Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 41 |  | 
| rpcraig | f1186f3 | 2012-07-19 09:38:06 -0400 | [diff] [blame] | 42 | #include <selinux/android.h> | 
| rpcraig | f1186f3 | 2012-07-19 09:38:06 -0400 | [diff] [blame] | 43 |  | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 44 | #include <UniquePtr.h> | 
|  | 45 |  | 
| Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 46 | #include "machine.h" | 
|  | 47 | #include "tombstone.h" | 
| Christopher Ferris | 365e4ae | 2013-10-02 12:26:48 -0700 | [diff] [blame] | 48 | #include "backtrace.h" | 
| Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 49 |  | 
| Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 50 | #define STACK_WORDS 16 | 
|  | 51 |  | 
|  | 52 | #define MAX_TOMBSTONES  10 | 
|  | 53 | #define TOMBSTONE_DIR   "/data/tombstones" | 
| Kévin PETIT | 4bb4772 | 2013-12-18 16:44:24 +0000 | [diff] [blame] | 54 | #define TOMBSTONE_TEMPLATE (TOMBSTONE_DIR"/tombstone_%02d") | 
| Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 55 |  | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 56 | // Must match the path defined in NativeCrashListener.java | 
| Christopher Tate | ded2e5a | 2013-03-19 13:12:23 -0700 | [diff] [blame] | 57 | #define NCRASH_SOCKET_PATH "/data/system/ndebugsocket" | 
|  | 58 |  | 
| Brigid Smith | 9c8dacc | 2014-06-02 15:02:20 -0700 | [diff] [blame] | 59 | // 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 Hughes | 855fcc3 | 2014-04-25 16:05:34 -0700 | [diff] [blame] | 74 | static bool signal_has_si_addr(int sig) { | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 75 | switch (sig) { | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 76 | case SIGBUS: | 
| Elliott Hughes | a323b50 | 2014-05-16 21:12:17 -0700 | [diff] [blame] | 77 | case SIGFPE: | 
|  | 78 | case SIGILL: | 
|  | 79 | case SIGSEGV: | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 80 | return true; | 
|  | 81 | default: | 
|  | 82 | return false; | 
|  | 83 | } | 
|  | 84 | } | 
|  | 85 |  | 
|  | 86 | static const char* get_signame(int sig) { | 
|  | 87 | switch(sig) { | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 88 | case SIGABRT: return "SIGABRT"; | 
|  | 89 | case SIGBUS: return "SIGBUS"; | 
|  | 90 | case SIGFPE: return "SIGFPE"; | 
| Elliott Hughes | a323b50 | 2014-05-16 21:12:17 -0700 | [diff] [blame] | 91 | case SIGILL: return "SIGILL"; | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 92 | case SIGPIPE: return "SIGPIPE"; | 
| Elliott Hughes | a323b50 | 2014-05-16 21:12:17 -0700 | [diff] [blame] | 93 | case SIGSEGV: return "SIGSEGV"; | 
| Elliott Hughes | 855fcc3 | 2014-04-25 16:05:34 -0700 | [diff] [blame] | 94 | #if defined(SIGSTKFLT) | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 95 | case SIGSTKFLT: return "SIGSTKFLT"; | 
|  | 96 | #endif | 
|  | 97 | case SIGSTOP: return "SIGSTOP"; | 
| Elliott Hughes | a323b50 | 2014-05-16 21:12:17 -0700 | [diff] [blame] | 98 | case SIGTRAP: return "SIGTRAP"; | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 99 | default: return "?"; | 
|  | 100 | } | 
|  | 101 | } | 
|  | 102 |  | 
|  | 103 | static const char* get_sigcode(int signo, int code) { | 
|  | 104 | // Try the signal-specific codes... | 
|  | 105 | switch (signo) { | 
|  | 106 | case SIGILL: | 
|  | 107 | switch (code) { | 
| Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 108 | 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 Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 116 | } | 
| Elliott Hughes | bd395b9 | 2014-04-24 13:53:22 -0700 | [diff] [blame] | 117 | static_assert(NSIGILL == ILL_BADSTK, "missing ILL_* si_code"); | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 118 | break; | 
| Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 119 | case SIGBUS: | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 120 | switch (code) { | 
| Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 121 | case BUS_ADRALN: return "BUS_ADRALN"; | 
|  | 122 | case BUS_ADRERR: return "BUS_ADRERR"; | 
|  | 123 | case BUS_OBJERR: return "BUS_OBJERR"; | 
| Elliott Hughes | bd395b9 | 2014-04-24 13:53:22 -0700 | [diff] [blame] | 124 | case BUS_MCEERR_AR: return "BUS_MCEERR_AR"; | 
|  | 125 | case BUS_MCEERR_AO: return "BUS_MCEERR_AO"; | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 126 | } | 
| Elliott Hughes | bd395b9 | 2014-04-24 13:53:22 -0700 | [diff] [blame] | 127 | static_assert(NSIGBUS == BUS_MCEERR_AO, "missing BUS_* si_code"); | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 128 | break; | 
| Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 129 | case SIGFPE: | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 130 | switch (code) { | 
| Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 131 | 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 Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 139 | } | 
| Elliott Hughes | bd395b9 | 2014-04-24 13:53:22 -0700 | [diff] [blame] | 140 | static_assert(NSIGFPE == FPE_FLTSUB, "missing FPE_* si_code"); | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 141 | break; | 
| Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 142 | case SIGSEGV: | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 143 | switch (code) { | 
| Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 144 | case SEGV_MAPERR: return "SEGV_MAPERR"; | 
|  | 145 | case SEGV_ACCERR: return "SEGV_ACCERR"; | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 146 | } | 
| Elliott Hughes | bd395b9 | 2014-04-24 13:53:22 -0700 | [diff] [blame] | 147 | static_assert(NSIGSEGV == SEGV_ACCERR, "missing SEGV_* si_code"); | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 148 | break; | 
| Elliott Hughes | 8f7d443 | 2012-12-10 10:29:05 -0800 | [diff] [blame] | 149 | case SIGTRAP: | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 150 | switch (code) { | 
| Elliott Hughes | 8f7d443 | 2012-12-10 10:29:05 -0800 | [diff] [blame] | 151 | case TRAP_BRKPT: return "TRAP_BRKPT"; | 
|  | 152 | case TRAP_TRACE: return "TRAP_TRACE"; | 
| Elliott Hughes | bd395b9 | 2014-04-24 13:53:22 -0700 | [diff] [blame] | 153 | case TRAP_BRANCH: return "TRAP_BRANCH"; | 
|  | 154 | case TRAP_HWBKPT: return "TRAP_HWBKPT"; | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 155 | } | 
| Elliott Hughes | bd395b9 | 2014-04-24 13:53:22 -0700 | [diff] [blame] | 156 | static_assert(NSIGTRAP == TRAP_HWBKPT, "missing TRAP_* si_code"); | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 157 | break; | 
|  | 158 | } | 
|  | 159 | // Then the other codes... | 
|  | 160 | switch (code) { | 
|  | 161 | case SI_USER: return "SI_USER"; | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 162 | case SI_KERNEL: return "SI_KERNEL"; | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 163 | case SI_QUEUE: return "SI_QUEUE"; | 
|  | 164 | case SI_TIMER: return "SI_TIMER"; | 
|  | 165 | case SI_MESGQ: return "SI_MESGQ"; | 
| Elliott Hughes | 8f7d443 | 2012-12-10 10:29:05 -0800 | [diff] [blame] | 166 | case SI_ASYNCIO: return "SI_ASYNCIO"; | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 167 | case SI_SIGIO: return "SI_SIGIO"; | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 168 | case SI_TKILL: return "SI_TKILL"; | 
| Elliott Hughes | bd395b9 | 2014-04-24 13:53:22 -0700 | [diff] [blame] | 169 | case SI_DETHREAD: return "SI_DETHREAD"; | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 170 | } | 
|  | 171 | // Then give up... | 
|  | 172 | return "?"; | 
| Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 173 | } | 
|  | 174 |  | 
| Brigid Smith | 9c8dacc | 2014-06-02 15:02:20 -0700 | [diff] [blame] | 175 | static void dump_header_info(log_t* log) { | 
|  | 176 | char fingerprint[PROPERTY_VALUE_MAX]; | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 177 | char revision[PROPERTY_VALUE_MAX]; | 
| Ben Cheng | d7760c1 | 2012-09-19 16:04:01 -0700 | [diff] [blame] | 178 |  | 
| Brigid Smith | 9c8dacc | 2014-06-02 15:02:20 -0700 | [diff] [blame] | 179 | property_get("ro.build.fingerprint", fingerprint, "unknown"); | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 180 | property_get("ro.revision", revision, "unknown"); | 
| Ben Cheng | d7760c1 | 2012-09-19 16:04:01 -0700 | [diff] [blame] | 181 |  | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 182 | _LOG(log, SCOPE_AT_FAULT, "Build fingerprint: '%s'\n", fingerprint); | 
| Brigid Smith | 9c8dacc | 2014-06-02 15:02:20 -0700 | [diff] [blame] | 183 | _LOG(log, SCOPE_AT_FAULT, "Revision: '%s'\n", revision); | 
|  | 184 | _LOG(log, SCOPE_AT_FAULT, "ABI: '%s'\n", ABI_STRING); | 
| Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 185 | } | 
|  | 186 |  | 
| Elliott Hughes | 855fcc3 | 2014-04-25 16:05:34 -0700 | [diff] [blame] | 187 | static void dump_signal_info(log_t* log, pid_t tid, int signal, int si_code) { | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 188 | siginfo_t si; | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 189 | memset(&si, 0, sizeof(si)); | 
| Elliott Hughes | 855fcc3 | 2014-04-25 16:05:34 -0700 | [diff] [blame] | 190 | if (ptrace(PTRACE_GETSIGINFO, tid, 0, &si) == -1) { | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 191 | _LOG(log, SCOPE_AT_FAULT, "cannot get siginfo: %s\n", strerror(errno)); | 
| Elliott Hughes | 855fcc3 | 2014-04-25 16:05:34 -0700 | [diff] [blame] | 192 | return; | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 193 | } | 
| Elliott Hughes | 855fcc3 | 2014-04-25 16:05:34 -0700 | [diff] [blame] | 194 |  | 
|  | 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 Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 207 | } | 
|  | 208 |  | 
| Christopher Ferris | 365e4ae | 2013-10-02 12:26:48 -0700 | [diff] [blame] | 209 | static void dump_thread_info(log_t* log, pid_t pid, pid_t tid, int scope_flags) { | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 210 | char path[64]; | 
|  | 211 | char threadnamebuf[1024]; | 
|  | 212 | char* threadname = NULL; | 
|  | 213 | FILE *fp; | 
| Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 214 |  | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 215 | 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 Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 232 | if ((fp = fopen(path, "r"))) { | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 233 | procname = fgets(procnamebuf, sizeof(procnamebuf), fp); | 
|  | 234 | fclose(fp); | 
| Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 235 | } | 
|  | 236 |  | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 237 | _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 Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 243 |  | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 244 | static 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 Chupin | c6c194c | 2013-11-21 23:17:20 +0400 | [diff] [blame] | 247 | word_t stack_content; | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 248 | if (!backtrace->ReadWord(*sp, &stack_content)) { | 
|  | 249 | break; | 
|  | 250 | } | 
|  | 251 |  | 
| Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 252 | const backtrace_map_t* map = backtrace->FindMap(stack_content); | 
|  | 253 | const char* map_name; | 
|  | 254 | if (!map) { | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 255 | map_name = ""; | 
| Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 256 | } else { | 
|  | 257 | map_name = map->name.c_str(); | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 258 | } | 
|  | 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 Hughes | f7b4b51 | 2014-01-31 23:13:55 -0800 | [diff] [blame] | 264 | _LOG(log, scope_flags, "    #%02d  %" PRIPTR "  %" PRIPTR "  %s (%s+%" PRIuPTR ")\n", | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 265 | label, *sp, stack_content, map_name, func_name.c_str(), offset); | 
|  | 266 | } else { | 
| Pavel Chupin | c6c194c | 2013-11-21 23:17:20 +0400 | [diff] [blame] | 267 | _LOG(log, scope_flags, "    #%02d  %" PRIPTR "  %" PRIPTR "  %s (%s)\n", | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 268 | label, *sp, stack_content, map_name, func_name.c_str()); | 
| Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 269 | } | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 270 | } else { | 
|  | 271 | if (offset) { | 
| Elliott Hughes | f7b4b51 | 2014-01-31 23:13:55 -0800 | [diff] [blame] | 272 | _LOG(log, scope_flags, "         %" PRIPTR "  %" PRIPTR "  %s (%s+%" PRIuPTR ")\n", | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 273 | *sp, stack_content, map_name, func_name.c_str(), offset); | 
|  | 274 | } else { | 
| Pavel Chupin | c6c194c | 2013-11-21 23:17:20 +0400 | [diff] [blame] | 275 | _LOG(log, scope_flags, "         %" PRIPTR "  %" PRIPTR "  %s (%s)\n", | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 276 | *sp, stack_content, map_name, func_name.c_str()); | 
|  | 277 | } | 
|  | 278 | } | 
| Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 279 | } else { | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 280 | if (!i && label >= 0) { | 
| Pavel Chupin | c6c194c | 2013-11-21 23:17:20 +0400 | [diff] [blame] | 281 | _LOG(log, scope_flags, "    #%02d  %" PRIPTR "  %" PRIPTR "  %s\n", | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 282 | label, *sp, stack_content, map_name); | 
|  | 283 | } else { | 
| Pavel Chupin | c6c194c | 2013-11-21 23:17:20 +0400 | [diff] [blame] | 284 | _LOG(log, scope_flags, "         %" PRIPTR "  %" PRIPTR "  %s\n", | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 285 | *sp, stack_content, map_name); | 
|  | 286 | } | 
| Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 287 | } | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 288 |  | 
| Pavel Chupin | c6c194c | 2013-11-21 23:17:20 +0400 | [diff] [blame] | 289 | *sp += sizeof(word_t); | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 290 | } | 
| Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 291 | } | 
|  | 292 |  | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 293 | static 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 Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 302 | } | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 303 | } | 
|  | 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 Chupin | c6c194c | 2013-11-21 23:17:20 +0400 | [diff] [blame] | 312 | word_t sp = backtrace->GetFrame(first)->sp - STACK_WORDS * sizeof(word_t); | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 313 | 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 Chupin | c6c194c | 2013-11-21 23:17:20 +0400 | [diff] [blame] | 332 | size_t words = frame->stack_size / sizeof(word_t); | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 333 | 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 Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 341 | } | 
|  | 342 |  | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 343 | static 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 Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 347 |  | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 348 | _LOG(log, scope_flags, "\nstack:\n"); | 
|  | 349 | dump_stack(backtrace, log, scope_flags); | 
|  | 350 | } | 
| Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 351 | } | 
|  | 352 |  | 
| Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 353 | static void dump_map(log_t* log, const backtrace_map_t* map, const char* what, int scope_flags) { | 
|  | 354 | if (map != NULL) { | 
| Pavel Chupin | c6c194c | 2013-11-21 23:17:20 +0400 | [diff] [blame] | 355 | _LOG(log, scope_flags, "    %" PRIPTR "-%" PRIPTR " %c%c%c %s\n", map->start, map->end, | 
| Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 356 | (map->flags & PROT_READ) ? 'r' : '-', (map->flags & PROT_WRITE) ? 'w' : '-', | 
|  | 357 | (map->flags & PROT_EXEC) ? 'x' : '-', map->name.c_str()); | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 358 | } else { | 
|  | 359 | _LOG(log, scope_flags, "    (no %s)\n", what); | 
|  | 360 | } | 
| Elliott Hughes | d1420be | 2013-01-03 13:39:57 -0800 | [diff] [blame] | 361 | } | 
|  | 362 |  | 
| Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 363 | static void dump_nearby_maps(BacktraceMap* map, log_t* log, pid_t tid, int scope_flags) { | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 364 | 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 Hughes | 855fcc3 | 2014-04-25 16:05:34 -0700 | [diff] [blame] | 371 | if (!signal_has_si_addr(si.si_signo)) { | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 372 | return; | 
|  | 373 | } | 
|  | 374 |  | 
| Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 375 | uintptr_t addr = reinterpret_cast<uintptr_t>(si.si_addr); | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 376 | addr &= ~0xfff;     // round to 4K page boundary | 
|  | 377 | if (addr == 0) {    // null-pointer deref | 
|  | 378 | return; | 
|  | 379 | } | 
|  | 380 |  | 
| Kévin PETIT | abc60c2 | 2013-12-19 12:36:59 +0000 | [diff] [blame] | 381 | _LOG(log, scope_flags, "\nmemory map around fault addr %" PRIPTR ":\n", | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 382 | 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 Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 386 | 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 Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 398 | break; | 
| Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 399 | } | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 400 | } | 
| Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 401 |  | 
| Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 402 | // 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 Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 406 | } | 
|  | 407 |  | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 408 | static 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 Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 411 |  | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 412 | 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 Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 416 | dump_nearby_maps(backtrace->GetMap(), log, backtrace->Tid(), scope_flags); | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 417 | } | 
| Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 418 | } | 
|  | 419 |  | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 420 | // Return true if some thread is not detached cleanly | 
| Christopher Ferris | 365e4ae | 2013-10-02 12:26:48 -0700 | [diff] [blame] | 421 | static bool dump_sibling_thread_report( | 
| Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 422 | log_t* log, pid_t pid, pid_t tid, int* total_sleep_time_usec, BacktraceMap* map) { | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 423 | char task_path[64]; | 
|  | 424 | snprintf(task_path, sizeof(task_path), "/proc/%d/task", pid); | 
| Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 425 |  | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 426 | 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 Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 439 | } | 
|  | 440 |  | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 441 | // 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 Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 446 | } | 
|  | 447 |  | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 448 | // 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 Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 456 | UniquePtr<Backtrace> backtrace(Backtrace::Create(pid, new_tid, map)); | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 457 | 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 Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 469 | } | 
|  | 470 |  | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 471 | // 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 Salyzyn | 22b5cef | 2013-11-22 10:53:34 -0800 | [diff] [blame] | 474 | // If "tail" is set, we only print the last few lines. | 
| Mark Salyzyn | 989980c | 2014-05-14 12:37:22 -0700 | [diff] [blame] | 475 | static EventTagMap* g_eventTagMap = NULL; | 
|  | 476 |  | 
| Mark Salyzyn | 22b5cef | 2013-11-22 10:53:34 -0800 | [diff] [blame] | 477 | static void dump_log_file(log_t* log, pid_t pid, const char* filename, | 
|  | 478 | unsigned int tail) { | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 479 | bool first = true; | 
| Mark Salyzyn | 22b5cef | 2013-11-22 10:53:34 -0800 | [diff] [blame] | 480 | struct logger_list *logger_list; | 
| Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 481 |  | 
| Mark Salyzyn | 22b5cef | 2013-11-22 10:53:34 -0800 | [diff] [blame] | 482 | logger_list = android_logger_list_open( | 
|  | 483 | android_name_to_log_id(filename), O_RDONLY | O_NONBLOCK, tail, pid); | 
| Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 484 |  | 
| Mark Salyzyn | 22b5cef | 2013-11-22 10:53:34 -0800 | [diff] [blame] | 485 | if (!logger_list) { | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 486 | XLOG("Unable to open %s: %s\n", filename, strerror(errno)); | 
|  | 487 | return; | 
|  | 488 | } | 
|  | 489 |  | 
| Mark Salyzyn | 22b5cef | 2013-11-22 10:53:34 -0800 | [diff] [blame] | 490 | struct log_msg log_entry; | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 491 |  | 
|  | 492 | while (true) { | 
| Mark Salyzyn | 22b5cef | 2013-11-22 10:53:34 -0800 | [diff] [blame] | 493 | ssize_t actual = android_logger_list_read(logger_list, &log_entry); | 
|  | 494 |  | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 495 | if (actual < 0) { | 
| Mark Salyzyn | 22b5cef | 2013-11-22 10:53:34 -0800 | [diff] [blame] | 496 | if (actual == -EINTR) { | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 497 | // interrupted by signal, retry | 
|  | 498 | continue; | 
| Mark Salyzyn | 22b5cef | 2013-11-22 10:53:34 -0800 | [diff] [blame] | 499 | } else if (actual == -EAGAIN) { | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 500 | // non-blocking EOF; we're done | 
|  | 501 | break; | 
|  | 502 | } else { | 
| Mark Salyzyn | 22b5cef | 2013-11-22 10:53:34 -0800 | [diff] [blame] | 503 | _LOG(log, 0, "Error while reading log: %s\n", | 
|  | 504 | strerror(-actual)); | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 505 | break; | 
|  | 506 | } | 
|  | 507 | } else if (actual == 0) { | 
| Mark Salyzyn | 22b5cef | 2013-11-22 10:53:34 -0800 | [diff] [blame] | 508 | _LOG(log, 0, "Got zero bytes while reading log: %s\n", | 
|  | 509 | strerror(errno)); | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 510 | break; | 
| Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 511 | } | 
|  | 512 |  | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 513 | // 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 Salyzyn | 22b5cef | 2013-11-22 10:53:34 -0800 | [diff] [blame] | 517 | struct logger_entry* entry = &log_entry.entry_v1; | 
| Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 518 |  | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 519 | if (entry->pid != static_cast<int32_t>(pid)) { | 
|  | 520 | // wrong pid, ignore | 
|  | 521 | continue; | 
| Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 522 | } | 
|  | 523 |  | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 524 | if (first) { | 
| Mark Salyzyn | 22b5cef | 2013-11-22 10:53:34 -0800 | [diff] [blame] | 525 | _LOG(log, 0, "--------- %slog %s\n", | 
|  | 526 | tail ? "tail end of " : "", filename); | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 527 | 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 Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 534 | static const char* kPrioChars = "!.VDIWEFS"; | 
| Mark Salyzyn | 22b5cef | 2013-11-22 10:53:34 -0800 | [diff] [blame] | 535 | unsigned hdr_size = log_entry.entry.hdr_size; | 
|  | 536 | if (!hdr_size) { | 
|  | 537 | hdr_size = sizeof(log_entry.entry_v1); | 
|  | 538 | } | 
| Mark Salyzyn | 989980c | 2014-05-14 12:37:22 -0700 | [diff] [blame] | 539 | 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 Salyzyn | 22b5cef | 2013-11-22 10:53:34 -0800 | [diff] [blame] | 561 | unsigned char prio = msg[0]; | 
|  | 562 | char* tag = msg + 1; | 
|  | 563 | msg = tag + strlen(tag) + 1; | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 564 |  | 
|  | 565 | // consume any trailing newlines | 
| Mark Salyzyn | fca0bd1 | 2013-12-12 12:21:20 -0800 | [diff] [blame] | 566 | char* nl = msg + strlen(msg) - 1; | 
|  | 567 | while (nl >= msg && *nl == '\n') { | 
|  | 568 | *nl-- = '\0'; | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 569 | } | 
|  | 570 |  | 
|  | 571 | char prioChar = (prio < strlen(kPrioChars) ? kPrioChars[prio] : '?'); | 
|  | 572 |  | 
| Mark Salyzyn | fca0bd1 | 2013-12-12 12:21:20 -0800 | [diff] [blame] | 573 | // 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 Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 587 | } | 
| Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 588 |  | 
| Mark Salyzyn | 22b5cef | 2013-11-22 10:53:34 -0800 | [diff] [blame] | 589 | android_logger_list_free(logger_list); | 
| Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 590 | } | 
|  | 591 |  | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 592 | // 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 Salyzyn | 22b5cef | 2013-11-22 10:53:34 -0800 | [diff] [blame] | 594 | static 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 Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 597 | } | 
|  | 598 |  | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 599 | static void dump_abort_message(Backtrace* backtrace, log_t* log, uintptr_t address) { | 
| Elliott Hughes | e5f8a69 | 2013-04-04 13:52:01 -0700 | [diff] [blame] | 600 | 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 Chupin | c6c194c | 2013-11-21 23:17:20 +0400 | [diff] [blame] | 610 | word_t data; | 
|  | 611 | size_t len = sizeof(word_t); | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 612 | if (!backtrace->ReadWord(address, &data)) { | 
| Elliott Hughes | e5f8a69 | 2013-04-04 13:52:01 -0700 | [diff] [blame] | 613 | break; | 
|  | 614 | } | 
| Pavel Chupin | c6c194c | 2013-11-21 23:17:20 +0400 | [diff] [blame] | 615 | address += sizeof(word_t); | 
| Elliott Hughes | e5f8a69 | 2013-04-04 13:52:01 -0700 | [diff] [blame] | 616 |  | 
| Pavel Chupin | c6c194c | 2013-11-21 23:17:20 +0400 | [diff] [blame] | 617 | while (len > 0 && (*p++ = (data >> (sizeof(word_t) - len) * 8) & 0xff) != 0) | 
|  | 618 | len--; | 
| Elliott Hughes | e5f8a69 | 2013-04-04 13:52:01 -0700 | [diff] [blame] | 619 | } | 
|  | 620 | msg[sizeof(msg) - 1] = '\0'; | 
|  | 621 |  | 
| Christopher Tate | 7716aef | 2013-04-02 14:00:27 -0700 | [diff] [blame] | 622 | _LOG(log, SCOPE_AT_FAULT, "Abort message: '%s'\n", msg); | 
| Elliott Hughes | e5f8a69 | 2013-04-04 13:52:01 -0700 | [diff] [blame] | 623 | } | 
|  | 624 |  | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 625 | // Dumps all information about the specified pid to the tombstone. | 
| Elliott Hughes | 855fcc3 | 2014-04-25 16:05:34 -0700 | [diff] [blame] | 626 | static 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 Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 629 | // 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 Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 633 |  | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 634 | 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 Tate | ded2e5a | 2013-03-19 13:12:23 -0700 | [diff] [blame] | 643 |  | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 644 | _LOG(log, SCOPE_AT_FAULT, | 
|  | 645 | "*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***\n"); | 
| Brigid Smith | 9c8dacc | 2014-06-02 15:02:20 -0700 | [diff] [blame] | 646 | dump_header_info(log); | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 647 | dump_thread_info(log, pid, tid, SCOPE_AT_FAULT); | 
|  | 648 | if (signal) { | 
| Elliott Hughes | 855fcc3 | 2014-04-25 16:05:34 -0700 | [diff] [blame] | 649 | dump_signal_info(log, tid, signal, si_code); | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 650 | } | 
| Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 651 |  | 
| Christopher Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 652 | UniquePtr<BacktraceMap> map(BacktraceMap::Create(pid)); | 
|  | 653 | UniquePtr<Backtrace> backtrace(Backtrace::Create(pid, tid, map.get())); | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 654 | 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 Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 658 |  | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 659 | if (want_logs) { | 
| Mark Salyzyn | 22b5cef | 2013-11-22 10:53:34 -0800 | [diff] [blame] | 660 | dump_logs(log, pid, 5); | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 661 | } | 
| Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 662 |  | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 663 | bool detach_failed = false; | 
|  | 664 | if (dump_sibling_threads) { | 
| Christopher Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 665 | detach_failed = dump_sibling_thread_report(log, pid, tid, total_sleep_time_usec, map.get()); | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 666 | } | 
| Christopher Ferris | 9846497 | 2014-01-06 19:16:33 -0800 | [diff] [blame] | 667 |  | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 668 | if (want_logs) { | 
| Mark Salyzyn | 22b5cef | 2013-11-22 10:53:34 -0800 | [diff] [blame] | 669 | dump_logs(log, pid, 0); | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 670 | } | 
| Christopher Tate | ded2e5a | 2013-03-19 13:12:23 -0700 | [diff] [blame] | 671 |  | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 672 | // 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 Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 682 | } | 
|  | 683 |  | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 684 | // 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. | 
|  | 689 | static char* find_and_open_tombstone(int* fd) { | 
| Christopher Ferris | 68bd59f | 2014-02-07 13:25:26 -0800 | [diff] [blame] | 690 | // In a single pass, find an available slot and, in case none | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 691 | // exist, find and record the least-recently-modified file. | 
|  | 692 | char path[128]; | 
| Christopher Ferris | 68bd59f | 2014-02-07 13:25:26 -0800 | [diff] [blame] | 693 | int oldest = -1; | 
|  | 694 | struct stat oldest_sb; | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 695 | for (int i = 0; i < MAX_TOMBSTONES; i++) { | 
| Kévin PETIT | 4bb4772 | 2013-12-18 16:44:24 +0000 | [diff] [blame] | 696 | snprintf(path, sizeof(path), TOMBSTONE_TEMPLATE, i); | 
| Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 697 |  | 
| Christopher Ferris | 68bd59f | 2014-02-07 13:25:26 -0800 | [diff] [blame] | 698 | struct stat sb; | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 699 | if (!stat(path, &sb)) { | 
| Christopher Ferris | 68bd59f | 2014-02-07 13:25:26 -0800 | [diff] [blame] | 700 | if (oldest < 0 || sb.st_mtime < oldest_sb.st_mtime) { | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 701 | oldest = i; | 
| Christopher Ferris | 68bd59f | 2014-02-07 13:25:26 -0800 | [diff] [blame] | 702 | oldest_sb.st_mtime = sb.st_mtime; | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 703 | } | 
|  | 704 | continue; | 
| Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 705 | } | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 706 | if (errno != ENOENT) | 
|  | 707 | continue; | 
| Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 708 |  | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 709 | *fd = open(path, O_CREAT | O_EXCL | O_WRONLY, 0600); | 
|  | 710 | if (*fd < 0) | 
|  | 711 | continue;   // raced ? | 
|  | 712 |  | 
| Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 713 | fchown(*fd, AID_SYSTEM, AID_SYSTEM); | 
|  | 714 | return strdup(path); | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 715 | } | 
|  | 716 |  | 
| Christopher Ferris | 68bd59f | 2014-02-07 13:25:26 -0800 | [diff] [blame] | 717 | if (oldest < 0) { | 
|  | 718 | LOG("Failed to find a valid tombstone, default to using tombstone 0.\n"); | 
|  | 719 | oldest = 0; | 
|  | 720 | } | 
|  | 721 |  | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 722 | // we didn't find an available file, so we clobber the oldest one | 
| Kévin PETIT | 4bb4772 | 2013-12-18 16:44:24 +0000 | [diff] [blame] | 723 | snprintf(path, sizeof(path), TOMBSTONE_TEMPLATE, oldest); | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 724 | *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 Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 731 | } | 
|  | 732 |  | 
| Christopher Tate | ded2e5a | 2013-03-19 13:12:23 -0700 | [diff] [blame] | 733 | static int activity_manager_connect() { | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 734 | int amfd = socket(PF_UNIX, SOCK_STREAM, 0); | 
|  | 735 | if (amfd >= 0) { | 
|  | 736 | struct sockaddr_un address; | 
|  | 737 | int err; | 
| Christopher Tate | ded2e5a | 2013-03-19 13:12:23 -0700 | [diff] [blame] | 738 |  | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 739 | 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 Tate | ded2e5a | 2013-03-19 13:12:23 -0700 | [diff] [blame] | 753 | } | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 754 | if (err) { | 
|  | 755 | close(amfd); | 
|  | 756 | amfd = -1; | 
|  | 757 | } | 
|  | 758 | } | 
| Christopher Tate | ded2e5a | 2013-03-19 13:12:23 -0700 | [diff] [blame] | 759 |  | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 760 | return amfd; | 
| Christopher Tate | ded2e5a | 2013-03-19 13:12:23 -0700 | [diff] [blame] | 761 | } | 
|  | 762 |  | 
| Elliott Hughes | 855fcc3 | 2014-04-25 16:05:34 -0700 | [diff] [blame] | 763 | char* 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 PETIT | 4bb4772 | 2013-12-18 16:44:24 +0000 | [diff] [blame] | 766 | if ((mkdir(TOMBSTONE_DIR, 0755) == -1) && (errno != EEXIST)) { | 
| Christopher Ferris | c5bb49a | 2014-05-05 11:16:47 -0700 | [diff] [blame] | 767 | LOG("failed to create %s: %s\n", TOMBSTONE_DIR, strerror(errno)); | 
| Kévin PETIT | 4bb4772 | 2013-12-18 16:44:24 +0000 | [diff] [blame] | 768 | } | 
|  | 769 |  | 
|  | 770 | if (chown(TOMBSTONE_DIR, AID_SYSTEM, AID_SYSTEM) == -1) { | 
| Christopher Ferris | c5bb49a | 2014-05-05 11:16:47 -0700 | [diff] [blame] | 771 | LOG("failed to change ownership of %s: %s\n", TOMBSTONE_DIR, strerror(errno)); | 
| Kévin PETIT | 4bb4772 | 2013-12-18 16:44:24 +0000 | [diff] [blame] | 772 | } | 
| Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 773 |  | 
| Christopher Ferris | c5bb49a | 2014-05-05 11:16:47 -0700 | [diff] [blame] | 774 | 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 Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 780 | } | 
| rpcraig | f1186f3 | 2012-07-19 09:38:06 -0400 | [diff] [blame] | 781 |  | 
| Christopher Ferris | c5bb49a | 2014-05-05 11:16:47 -0700 | [diff] [blame] | 782 | if (fd < 0 && quiet) { | 
|  | 783 | LOG("Skipping tombstone write, nothing to do.\n"); | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 784 | *detach_failed = false; | 
|  | 785 | return NULL; | 
|  | 786 | } | 
| Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 787 |  | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 788 | log_t log; | 
|  | 789 | log.tfd = fd; | 
| Christopher Ferris | c5bb49a | 2014-05-05 11:16:47 -0700 | [diff] [blame] | 790 | // 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 Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 794 | log.quiet = quiet; | 
| Elliott Hughes | 855fcc3 | 2014-04-25 16:05:34 -0700 | [diff] [blame] | 795 | *detach_failed = dump_crash(&log, pid, tid, signal, original_si_code, abort_msg_address, | 
|  | 796 | dump_sibling_threads, total_sleep_time_usec); | 
| Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 797 |  | 
| Christopher Ferris | c5bb49a | 2014-05-05 11:16:47 -0700 | [diff] [blame] | 798 | // Either of these file descriptors can be -1, any error is ignored. | 
|  | 799 | close(amfd); | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 800 | close(fd); | 
| Christopher Ferris | c5bb49a | 2014-05-05 11:16:47 -0700 | [diff] [blame] | 801 |  | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 802 | return path; | 
| Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 803 | } |