Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 The Android Open Source Project |
| 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 | |
| 17 | #include <stddef.h> |
| 18 | #include <stdbool.h> |
| 19 | #include <stdlib.h> |
| 20 | #include <signal.h> |
| 21 | #include <string.h> |
| 22 | #include <stdio.h> |
| 23 | #include <fcntl.h> |
| 24 | #include <errno.h> |
| 25 | #include <dirent.h> |
| 26 | #include <time.h> |
| 27 | #include <sys/ptrace.h> |
| 28 | #include <sys/stat.h> |
| 29 | |
| 30 | #include <private/android_filesystem_config.h> |
| 31 | |
| 32 | #include <cutils/logger.h> |
| 33 | #include <cutils/properties.h> |
| 34 | |
Christopher Ferris | 365e4ae | 2013-10-02 12:26:48 -0700 | [diff] [blame^] | 35 | #include <backtrace/backtrace.h> |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 36 | |
Christopher Tate | ded2e5a | 2013-03-19 13:12:23 -0700 | [diff] [blame] | 37 | #include <sys/socket.h> |
| 38 | #include <linux/un.h> |
| 39 | |
rpcraig | f1186f3 | 2012-07-19 09:38:06 -0400 | [diff] [blame] | 40 | #include <selinux/android.h> |
rpcraig | f1186f3 | 2012-07-19 09:38:06 -0400 | [diff] [blame] | 41 | |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 42 | #include "machine.h" |
| 43 | #include "tombstone.h" |
Christopher Ferris | 365e4ae | 2013-10-02 12:26:48 -0700 | [diff] [blame^] | 44 | #include "backtrace.h" |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 45 | |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 46 | #define STACK_WORDS 16 |
| 47 | |
| 48 | #define MAX_TOMBSTONES 10 |
| 49 | #define TOMBSTONE_DIR "/data/tombstones" |
| 50 | |
Christopher Tate | ded2e5a | 2013-03-19 13:12:23 -0700 | [diff] [blame] | 51 | /* Must match the path defined in NativeCrashListener.java */ |
| 52 | #define NCRASH_SOCKET_PATH "/data/system/ndebugsocket" |
| 53 | |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 54 | #define typecheck(x,y) { \ |
| 55 | typeof(x) __dummy1; \ |
| 56 | typeof(y) __dummy2; \ |
| 57 | (void)(&__dummy1 == &__dummy2); } |
| 58 | |
| 59 | |
| 60 | static bool signal_has_address(int sig) { |
| 61 | switch (sig) { |
| 62 | case SIGILL: |
| 63 | case SIGFPE: |
| 64 | case SIGSEGV: |
| 65 | case SIGBUS: |
| 66 | return true; |
| 67 | default: |
| 68 | return false; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | static const char *get_signame(int sig) |
| 73 | { |
| 74 | switch(sig) { |
| 75 | case SIGILL: return "SIGILL"; |
| 76 | case SIGABRT: return "SIGABRT"; |
| 77 | case SIGBUS: return "SIGBUS"; |
| 78 | case SIGFPE: return "SIGFPE"; |
| 79 | case SIGSEGV: return "SIGSEGV"; |
| 80 | case SIGPIPE: return "SIGPIPE"; |
Chris Dearman | 231e3c8 | 2012-08-10 17:06:20 -0700 | [diff] [blame] | 81 | #ifdef SIGSTKFLT |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 82 | case SIGSTKFLT: return "SIGSTKFLT"; |
Chris Dearman | 231e3c8 | 2012-08-10 17:06:20 -0700 | [diff] [blame] | 83 | #endif |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 84 | case SIGSTOP: return "SIGSTOP"; |
| 85 | default: return "?"; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | static const char *get_sigcode(int signo, int code) |
| 90 | { |
Elliott Hughes | 8f7d443 | 2012-12-10 10:29:05 -0800 | [diff] [blame] | 91 | // Try the signal-specific codes... |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 92 | switch (signo) { |
| 93 | case SIGILL: |
| 94 | switch (code) { |
| 95 | case ILL_ILLOPC: return "ILL_ILLOPC"; |
| 96 | case ILL_ILLOPN: return "ILL_ILLOPN"; |
| 97 | case ILL_ILLADR: return "ILL_ILLADR"; |
| 98 | case ILL_ILLTRP: return "ILL_ILLTRP"; |
| 99 | case ILL_PRVOPC: return "ILL_PRVOPC"; |
| 100 | case ILL_PRVREG: return "ILL_PRVREG"; |
| 101 | case ILL_COPROC: return "ILL_COPROC"; |
| 102 | case ILL_BADSTK: return "ILL_BADSTK"; |
| 103 | } |
| 104 | break; |
| 105 | case SIGBUS: |
| 106 | switch (code) { |
| 107 | case BUS_ADRALN: return "BUS_ADRALN"; |
| 108 | case BUS_ADRERR: return "BUS_ADRERR"; |
| 109 | case BUS_OBJERR: return "BUS_OBJERR"; |
| 110 | } |
| 111 | break; |
| 112 | case SIGFPE: |
| 113 | switch (code) { |
| 114 | case FPE_INTDIV: return "FPE_INTDIV"; |
| 115 | case FPE_INTOVF: return "FPE_INTOVF"; |
| 116 | case FPE_FLTDIV: return "FPE_FLTDIV"; |
| 117 | case FPE_FLTOVF: return "FPE_FLTOVF"; |
| 118 | case FPE_FLTUND: return "FPE_FLTUND"; |
| 119 | case FPE_FLTRES: return "FPE_FLTRES"; |
| 120 | case FPE_FLTINV: return "FPE_FLTINV"; |
| 121 | case FPE_FLTSUB: return "FPE_FLTSUB"; |
| 122 | } |
| 123 | break; |
| 124 | case SIGSEGV: |
| 125 | switch (code) { |
| 126 | case SEGV_MAPERR: return "SEGV_MAPERR"; |
| 127 | case SEGV_ACCERR: return "SEGV_ACCERR"; |
| 128 | } |
| 129 | break; |
Elliott Hughes | 8f7d443 | 2012-12-10 10:29:05 -0800 | [diff] [blame] | 130 | case SIGTRAP: |
| 131 | switch (code) { |
| 132 | case TRAP_BRKPT: return "TRAP_BRKPT"; |
| 133 | case TRAP_TRACE: return "TRAP_TRACE"; |
| 134 | } |
| 135 | break; |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 136 | } |
Elliott Hughes | 8f7d443 | 2012-12-10 10:29:05 -0800 | [diff] [blame] | 137 | // Then the other codes... |
| 138 | switch (code) { |
| 139 | case SI_USER: return "SI_USER"; |
| 140 | #if defined(SI_KERNEL) |
| 141 | case SI_KERNEL: return "SI_KERNEL"; |
| 142 | #endif |
| 143 | case SI_QUEUE: return "SI_QUEUE"; |
| 144 | case SI_TIMER: return "SI_TIMER"; |
| 145 | case SI_MESGQ: return "SI_MESGQ"; |
| 146 | case SI_ASYNCIO: return "SI_ASYNCIO"; |
| 147 | #if defined(SI_SIGIO) |
| 148 | case SI_SIGIO: return "SI_SIGIO"; |
| 149 | #endif |
| 150 | #if defined(SI_TKILL) |
| 151 | case SI_TKILL: return "SI_TKILL"; |
| 152 | #endif |
| 153 | } |
| 154 | // Then give up... |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 155 | return "?"; |
| 156 | } |
| 157 | |
Ben Cheng | d7760c1 | 2012-09-19 16:04:01 -0700 | [diff] [blame] | 158 | static void dump_revision_info(log_t* log) |
| 159 | { |
| 160 | char revision[PROPERTY_VALUE_MAX]; |
| 161 | |
| 162 | property_get("ro.revision", revision, "unknown"); |
| 163 | |
Christopher Tate | 7716aef | 2013-04-02 14:00:27 -0700 | [diff] [blame] | 164 | _LOG(log, SCOPE_AT_FAULT, "Revision: '%s'\n", revision); |
Ben Cheng | d7760c1 | 2012-09-19 16:04:01 -0700 | [diff] [blame] | 165 | } |
| 166 | |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 167 | static void dump_build_info(log_t* log) |
| 168 | { |
| 169 | char fingerprint[PROPERTY_VALUE_MAX]; |
| 170 | |
| 171 | property_get("ro.build.fingerprint", fingerprint, "unknown"); |
| 172 | |
Christopher Tate | 7716aef | 2013-04-02 14:00:27 -0700 | [diff] [blame] | 173 | _LOG(log, SCOPE_AT_FAULT, "Build fingerprint: '%s'\n", fingerprint); |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | static void dump_fault_addr(log_t* log, pid_t tid, int sig) |
| 177 | { |
| 178 | siginfo_t si; |
| 179 | |
| 180 | memset(&si, 0, sizeof(si)); |
Christopher Tate | 7716aef | 2013-04-02 14:00:27 -0700 | [diff] [blame] | 181 | if(ptrace(PTRACE_GETSIGINFO, tid, 0, &si)){ |
| 182 | _LOG(log, SCOPE_AT_FAULT, "cannot get siginfo: %s\n", strerror(errno)); |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 183 | } else if (signal_has_address(sig)) { |
Christopher Tate | 7716aef | 2013-04-02 14:00:27 -0700 | [diff] [blame] | 184 | _LOG(log, SCOPE_AT_FAULT, "signal %d (%s), code %d (%s), fault addr %08x\n", |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 185 | sig, get_signame(sig), |
| 186 | si.si_code, get_sigcode(sig, si.si_code), |
| 187 | (uintptr_t) si.si_addr); |
| 188 | } else { |
Christopher Tate | 7716aef | 2013-04-02 14:00:27 -0700 | [diff] [blame] | 189 | _LOG(log, SCOPE_AT_FAULT, "signal %d (%s), code %d (%s), fault addr --------\n", |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 190 | sig, get_signame(sig), si.si_code, get_sigcode(sig, si.si_code)); |
| 191 | } |
| 192 | } |
| 193 | |
Christopher Ferris | 365e4ae | 2013-10-02 12:26:48 -0700 | [diff] [blame^] | 194 | static void dump_thread_info(log_t* log, pid_t pid, pid_t tid, int scope_flags) { |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 195 | char path[64]; |
| 196 | char threadnamebuf[1024]; |
| 197 | char* threadname = NULL; |
| 198 | FILE *fp; |
| 199 | |
| 200 | snprintf(path, sizeof(path), "/proc/%d/comm", tid); |
| 201 | if ((fp = fopen(path, "r"))) { |
| 202 | threadname = fgets(threadnamebuf, sizeof(threadnamebuf), fp); |
| 203 | fclose(fp); |
| 204 | if (threadname) { |
| 205 | size_t len = strlen(threadname); |
| 206 | if (len && threadname[len - 1] == '\n') { |
| 207 | threadname[len - 1] = '\0'; |
| 208 | } |
| 209 | } |
| 210 | } |
| 211 | |
Christopher Ferris | 365e4ae | 2013-10-02 12:26:48 -0700 | [diff] [blame^] | 212 | if (IS_AT_FAULT(scope_flags)) { |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 213 | char procnamebuf[1024]; |
| 214 | char* procname = NULL; |
| 215 | |
| 216 | snprintf(path, sizeof(path), "/proc/%d/cmdline", pid); |
| 217 | if ((fp = fopen(path, "r"))) { |
| 218 | procname = fgets(procnamebuf, sizeof(procnamebuf), fp); |
| 219 | fclose(fp); |
| 220 | } |
| 221 | |
Christopher Tate | 7716aef | 2013-04-02 14:00:27 -0700 | [diff] [blame] | 222 | _LOG(log, SCOPE_AT_FAULT, "pid: %d, tid: %d, name: %s >>> %s <<<\n", pid, tid, |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 223 | threadname ? threadname : "UNKNOWN", |
| 224 | procname ? procname : "UNKNOWN"); |
| 225 | } else { |
Christopher Tate | 7716aef | 2013-04-02 14:00:27 -0700 | [diff] [blame] | 226 | _LOG(log, 0, "pid: %d, tid: %d, name: %s\n", |
| 227 | pid, tid, threadname ? threadname : "UNKNOWN"); |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 228 | } |
| 229 | } |
| 230 | |
Christopher Ferris | 365e4ae | 2013-10-02 12:26:48 -0700 | [diff] [blame^] | 231 | static void dump_stack_segment(const backtrace_t* backtrace, log_t* log, |
| 232 | int scope_flags, uintptr_t *sp, size_t words, int label) { |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 233 | for (size_t i = 0; i < words; i++) { |
| 234 | uint32_t stack_content; |
Christopher Ferris | 365e4ae | 2013-10-02 12:26:48 -0700 | [diff] [blame^] | 235 | if (!backtrace_read_word(backtrace, *sp, &stack_content)) { |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 236 | break; |
| 237 | } |
| 238 | |
Christopher Ferris | 365e4ae | 2013-10-02 12:26:48 -0700 | [diff] [blame^] | 239 | const char* map_name = backtrace_get_map_info(backtrace, stack_content, NULL); |
| 240 | if (!map_name) { |
| 241 | map_name = ""; |
| 242 | } |
| 243 | uintptr_t offset = 0; |
| 244 | char* proc_name = backtrace_get_proc_name(backtrace, stack_content, &offset); |
| 245 | if (proc_name) { |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 246 | if (!i && label >= 0) { |
| 247 | if (offset) { |
Christopher Ferris | 365e4ae | 2013-10-02 12:26:48 -0700 | [diff] [blame^] | 248 | _LOG(log, scope_flags, " #%02d %08x %08x %s (%s+%u)\n", |
| 249 | label, *sp, stack_content, map_name, proc_name, offset); |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 250 | } else { |
Christopher Ferris | 365e4ae | 2013-10-02 12:26:48 -0700 | [diff] [blame^] | 251 | _LOG(log, scope_flags, " #%02d %08x %08x %s (%s)\n", |
| 252 | label, *sp, stack_content, map_name, proc_name); |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 253 | } |
| 254 | } else { |
| 255 | if (offset) { |
Christopher Ferris | 365e4ae | 2013-10-02 12:26:48 -0700 | [diff] [blame^] | 256 | _LOG(log, scope_flags, " %08x %08x %s (%s+%u)\n", |
| 257 | *sp, stack_content, map_name, proc_name, offset); |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 258 | } else { |
Christopher Ferris | 365e4ae | 2013-10-02 12:26:48 -0700 | [diff] [blame^] | 259 | _LOG(log, scope_flags, " %08x %08x %s (%s)\n", |
| 260 | *sp, stack_content, map_name, proc_name); |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 261 | } |
| 262 | } |
Christopher Ferris | 365e4ae | 2013-10-02 12:26:48 -0700 | [diff] [blame^] | 263 | free(proc_name); |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 264 | } else { |
| 265 | if (!i && label >= 0) { |
Christopher Ferris | 365e4ae | 2013-10-02 12:26:48 -0700 | [diff] [blame^] | 266 | _LOG(log, scope_flags, " #%02d %08x %08x %s\n", |
| 267 | label, *sp, stack_content, map_name); |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 268 | } else { |
Christopher Ferris | 365e4ae | 2013-10-02 12:26:48 -0700 | [diff] [blame^] | 269 | _LOG(log, scope_flags, " %08x %08x %s\n", |
| 270 | *sp, stack_content, map_name); |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 271 | } |
| 272 | } |
| 273 | |
| 274 | *sp += sizeof(uint32_t); |
| 275 | } |
| 276 | } |
| 277 | |
Christopher Ferris | 365e4ae | 2013-10-02 12:26:48 -0700 | [diff] [blame^] | 278 | static void dump_stack(const backtrace_t* backtrace, log_t* log, int scope_flags) { |
| 279 | size_t first = 0, last; |
| 280 | for (size_t i = 0; i < backtrace->num_frames; i++) { |
| 281 | if (backtrace->frames[i].sp) { |
| 282 | if (!first) { |
| 283 | first = i+1; |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 284 | } |
| 285 | last = i; |
| 286 | } |
| 287 | } |
Christopher Ferris | 365e4ae | 2013-10-02 12:26:48 -0700 | [diff] [blame^] | 288 | if (!first) { |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 289 | return; |
| 290 | } |
Christopher Ferris | 365e4ae | 2013-10-02 12:26:48 -0700 | [diff] [blame^] | 291 | first--; |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 292 | |
Christopher Ferris | 365e4ae | 2013-10-02 12:26:48 -0700 | [diff] [blame^] | 293 | scope_flags |= SCOPE_SENSITIVE; |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 294 | |
| 295 | // Dump a few words before the first frame. |
Christopher Ferris | 365e4ae | 2013-10-02 12:26:48 -0700 | [diff] [blame^] | 296 | uintptr_t sp = backtrace->frames[first].sp - STACK_WORDS * sizeof(uint32_t); |
| 297 | dump_stack_segment(backtrace, log, scope_flags, &sp, STACK_WORDS, -1); |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 298 | |
| 299 | // Dump a few words from all successive frames. |
| 300 | // Only log the first 3 frames, put the rest in the tombstone. |
| 301 | for (size_t i = first; i <= last; i++) { |
Christopher Ferris | 365e4ae | 2013-10-02 12:26:48 -0700 | [diff] [blame^] | 302 | const backtrace_frame_data_t* frame = &backtrace->frames[i]; |
| 303 | if (sp != frame->sp) { |
| 304 | _LOG(log, scope_flags, " ........ ........\n"); |
| 305 | sp = frame->sp; |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 306 | } |
| 307 | if (i - first == 3) { |
Christopher Ferris | 365e4ae | 2013-10-02 12:26:48 -0700 | [diff] [blame^] | 308 | scope_flags &= (~SCOPE_AT_FAULT); |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 309 | } |
| 310 | if (i == last) { |
Christopher Ferris | 365e4ae | 2013-10-02 12:26:48 -0700 | [diff] [blame^] | 311 | dump_stack_segment(backtrace, log, scope_flags, &sp, STACK_WORDS, i); |
| 312 | if (sp < frame->sp + frame->stack_size) { |
| 313 | _LOG(log, scope_flags, " ........ ........\n"); |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 314 | } |
| 315 | } else { |
| 316 | size_t words = frame->stack_size / sizeof(uint32_t); |
| 317 | if (words == 0) { |
| 318 | words = 1; |
| 319 | } else if (words > STACK_WORDS) { |
| 320 | words = STACK_WORDS; |
| 321 | } |
Christopher Ferris | 365e4ae | 2013-10-02 12:26:48 -0700 | [diff] [blame^] | 322 | dump_stack_segment(backtrace, log, scope_flags, &sp, words, i); |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 323 | } |
| 324 | } |
| 325 | } |
| 326 | |
Christopher Ferris | 365e4ae | 2013-10-02 12:26:48 -0700 | [diff] [blame^] | 327 | static void dump_backtrace_and_stack(const backtrace_t* backtrace, log_t* log, |
| 328 | int scope_flags) { |
| 329 | if (backtrace->num_frames) { |
| 330 | _LOG(log, scope_flags, "\nbacktrace:\n"); |
| 331 | dump_backtrace_to_log(backtrace, log, scope_flags, " "); |
| 332 | |
| 333 | _LOG(log, scope_flags, "\nstack:\n"); |
| 334 | dump_stack(backtrace, log, scope_flags); |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 335 | } |
| 336 | } |
| 337 | |
Christopher Ferris | 365e4ae | 2013-10-02 12:26:48 -0700 | [diff] [blame^] | 338 | static void dump_map(log_t* log, const backtrace_map_info_t* m, const char* what, int scope_flags) { |
Elliott Hughes | d1420be | 2013-01-03 13:39:57 -0800 | [diff] [blame] | 339 | if (m != NULL) { |
Christopher Ferris | 365e4ae | 2013-10-02 12:26:48 -0700 | [diff] [blame^] | 340 | _LOG(log, scope_flags, " %08x-%08x %c%c%c %s\n", m->start, m->end, |
Elliott Hughes | d1420be | 2013-01-03 13:39:57 -0800 | [diff] [blame] | 341 | m->is_readable ? 'r' : '-', |
| 342 | m->is_writable ? 'w' : '-', |
| 343 | m->is_executable ? 'x' : '-', |
| 344 | m->name); |
| 345 | } else { |
Christopher Ferris | 365e4ae | 2013-10-02 12:26:48 -0700 | [diff] [blame^] | 346 | _LOG(log, scope_flags, " (no %s)\n", what); |
Elliott Hughes | d1420be | 2013-01-03 13:39:57 -0800 | [diff] [blame] | 347 | } |
| 348 | } |
| 349 | |
Christopher Ferris | 365e4ae | 2013-10-02 12:26:48 -0700 | [diff] [blame^] | 350 | static void dump_nearby_maps(const backtrace_map_info_t* map_info_list, log_t* log, pid_t tid, int scope_flags) { |
| 351 | scope_flags |= SCOPE_SENSITIVE; |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 352 | siginfo_t si; |
| 353 | memset(&si, 0, sizeof(si)); |
| 354 | if (ptrace(PTRACE_GETSIGINFO, tid, 0, &si)) { |
Christopher Ferris | 365e4ae | 2013-10-02 12:26:48 -0700 | [diff] [blame^] | 355 | _LOG(log, scope_flags, "cannot get siginfo for %d: %s\n", |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 356 | tid, strerror(errno)); |
| 357 | return; |
| 358 | } |
| 359 | if (!signal_has_address(si.si_signo)) { |
| 360 | return; |
| 361 | } |
| 362 | |
| 363 | uintptr_t addr = (uintptr_t) si.si_addr; |
| 364 | addr &= ~0xfff; /* round to 4K page boundary */ |
| 365 | if (addr == 0) { /* null-pointer deref */ |
| 366 | return; |
| 367 | } |
| 368 | |
Christopher Ferris | 365e4ae | 2013-10-02 12:26:48 -0700 | [diff] [blame^] | 369 | _LOG(log, scope_flags, "\nmemory map around fault addr %08x:\n", (int)si.si_addr); |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 370 | |
| 371 | /* |
| 372 | * Search for a match, or for a hole where the match would be. The list |
| 373 | * is backward from the file content, so it starts at high addresses. |
| 374 | */ |
Christopher Ferris | 365e4ae | 2013-10-02 12:26:48 -0700 | [diff] [blame^] | 375 | const backtrace_map_info_t* map = map_info_list; |
| 376 | const backtrace_map_info_t* next = NULL; |
| 377 | const backtrace_map_info_t* prev = NULL; |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 378 | while (map != NULL) { |
| 379 | if (addr >= map->start && addr < map->end) { |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 380 | next = map->next; |
| 381 | break; |
| 382 | } else if (addr >= map->end) { |
| 383 | /* map would be between "prev" and this entry */ |
| 384 | next = map; |
| 385 | map = NULL; |
| 386 | break; |
| 387 | } |
| 388 | |
| 389 | prev = map; |
| 390 | map = map->next; |
| 391 | } |
| 392 | |
| 393 | /* |
| 394 | * Show "next" then "match" then "prev" so that the addresses appear in |
| 395 | * ascending order (like /proc/pid/maps). |
| 396 | */ |
Christopher Ferris | 365e4ae | 2013-10-02 12:26:48 -0700 | [diff] [blame^] | 397 | dump_map(log, next, "map below", scope_flags); |
| 398 | dump_map(log, map, "map for address", scope_flags); |
| 399 | dump_map(log, prev, "map above", scope_flags); |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 400 | } |
| 401 | |
Christopher Ferris | 365e4ae | 2013-10-02 12:26:48 -0700 | [diff] [blame^] | 402 | static void dump_thread(const backtrace_t* backtrace, log_t* log, int scope_flags, |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 403 | int* total_sleep_time_usec) { |
Christopher Ferris | 365e4ae | 2013-10-02 12:26:48 -0700 | [diff] [blame^] | 404 | wait_for_stop(backtrace->tid, total_sleep_time_usec); |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 405 | |
Christopher Ferris | 365e4ae | 2013-10-02 12:26:48 -0700 | [diff] [blame^] | 406 | dump_registers(log, backtrace->tid, scope_flags); |
| 407 | dump_backtrace_and_stack(backtrace, log, scope_flags); |
| 408 | if (IS_AT_FAULT(scope_flags)) { |
| 409 | dump_memory_and_code(log, backtrace->tid, scope_flags); |
| 410 | dump_nearby_maps(backtrace->map_info_list, log, backtrace->tid, scope_flags); |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 411 | } |
| 412 | } |
| 413 | |
| 414 | /* Return true if some thread is not detached cleanly */ |
Christopher Ferris | 365e4ae | 2013-10-02 12:26:48 -0700 | [diff] [blame^] | 415 | static bool dump_sibling_thread_report( |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 416 | log_t* log, pid_t pid, pid_t tid, int* total_sleep_time_usec) { |
| 417 | char task_path[64]; |
| 418 | snprintf(task_path, sizeof(task_path), "/proc/%d/task", pid); |
| 419 | |
| 420 | DIR* d = opendir(task_path); |
Christopher Ferris | 365e4ae | 2013-10-02 12:26:48 -0700 | [diff] [blame^] | 421 | /* Bail early if the task directory cannot be opened */ |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 422 | if (d == NULL) { |
| 423 | XLOG("Cannot open /proc/%d/task\n", pid); |
| 424 | return false; |
| 425 | } |
| 426 | |
| 427 | bool detach_failed = false; |
Elliott Hughes | c463d2c | 2012-10-26 16:47:09 -0700 | [diff] [blame] | 428 | struct dirent* de; |
| 429 | while ((de = readdir(d)) != NULL) { |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 430 | /* Ignore "." and ".." */ |
| 431 | if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, "..")) { |
| 432 | continue; |
| 433 | } |
| 434 | |
| 435 | /* The main thread at fault has been handled individually */ |
| 436 | char* end; |
| 437 | pid_t new_tid = strtoul(de->d_name, &end, 10); |
| 438 | if (*end || new_tid == tid) { |
| 439 | continue; |
| 440 | } |
| 441 | |
| 442 | /* Skip this thread if cannot ptrace it */ |
| 443 | if (ptrace(PTRACE_ATTACH, new_tid, 0, 0) < 0) { |
| 444 | continue; |
| 445 | } |
| 446 | |
Christopher Tate | 7716aef | 2013-04-02 14:00:27 -0700 | [diff] [blame] | 447 | _LOG(log, 0, "--- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---\n"); |
Christopher Ferris | 365e4ae | 2013-10-02 12:26:48 -0700 | [diff] [blame^] | 448 | dump_thread_info(log, pid, new_tid, 0); |
| 449 | backtrace_t new_backtrace; |
| 450 | if (backtrace_get_data(&new_backtrace, new_tid)) { |
| 451 | dump_thread(&new_backtrace, log, 0, total_sleep_time_usec); |
| 452 | } |
| 453 | backtrace_free_data(&new_backtrace); |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 454 | |
| 455 | if (ptrace(PTRACE_DETACH, new_tid, 0, 0) != 0) { |
| 456 | LOG("ptrace detach from %d failed: %s\n", new_tid, strerror(errno)); |
| 457 | detach_failed = true; |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | closedir(d); |
| 462 | return detach_failed; |
| 463 | } |
| 464 | |
| 465 | /* |
| 466 | * Reads the contents of the specified log device, filters out the entries |
| 467 | * that don't match the specified pid, and writes them to the tombstone file. |
| 468 | * |
| 469 | * If "tailOnly" is set, we only print the last few lines. |
| 470 | */ |
| 471 | static void dump_log_file(log_t* log, pid_t pid, const char* filename, |
| 472 | bool tailOnly) |
| 473 | { |
| 474 | bool first = true; |
| 475 | |
| 476 | /* circular buffer, for "tailOnly" mode */ |
| 477 | const int kShortLogMaxLines = 5; |
| 478 | const int kShortLogLineLen = 256; |
| 479 | char shortLog[kShortLogMaxLines][kShortLogLineLen]; |
| 480 | int shortLogCount = 0; |
| 481 | int shortLogNext = 0; |
| 482 | |
| 483 | int logfd = open(filename, O_RDONLY | O_NONBLOCK); |
| 484 | if (logfd < 0) { |
| 485 | XLOG("Unable to open %s: %s\n", filename, strerror(errno)); |
| 486 | return; |
| 487 | } |
| 488 | |
| 489 | union { |
| 490 | unsigned char buf[LOGGER_ENTRY_MAX_LEN + 1]; |
| 491 | struct logger_entry entry; |
| 492 | } log_entry; |
| 493 | |
| 494 | while (true) { |
| 495 | ssize_t actual = read(logfd, log_entry.buf, LOGGER_ENTRY_MAX_LEN); |
| 496 | if (actual < 0) { |
| 497 | if (errno == EINTR) { |
| 498 | /* interrupted by signal, retry */ |
| 499 | continue; |
| 500 | } else if (errno == EAGAIN) { |
| 501 | /* non-blocking EOF; we're done */ |
| 502 | break; |
| 503 | } else { |
Christopher Tate | 7716aef | 2013-04-02 14:00:27 -0700 | [diff] [blame] | 504 | _LOG(log, 0, "Error while reading log: %s\n", |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 505 | strerror(errno)); |
| 506 | break; |
| 507 | } |
| 508 | } else if (actual == 0) { |
Christopher Tate | 7716aef | 2013-04-02 14:00:27 -0700 | [diff] [blame] | 509 | _LOG(log, 0, "Got zero bytes while reading log: %s\n", |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 510 | strerror(errno)); |
| 511 | break; |
| 512 | } |
| 513 | |
| 514 | /* |
| 515 | * NOTE: if you XLOG something here, this will spin forever, |
| 516 | * because you will be writing as fast as you're reading. Any |
| 517 | * high-frequency debug diagnostics should just be written to |
| 518 | * the tombstone file. |
| 519 | */ |
| 520 | |
| 521 | struct logger_entry* entry = &log_entry.entry; |
| 522 | |
| 523 | if (entry->pid != (int32_t) pid) { |
| 524 | /* wrong pid, ignore */ |
| 525 | continue; |
| 526 | } |
| 527 | |
| 528 | if (first) { |
Christopher Tate | 7716aef | 2013-04-02 14:00:27 -0700 | [diff] [blame] | 529 | _LOG(log, 0, "--------- %slog %s\n", |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 530 | tailOnly ? "tail end of " : "", filename); |
| 531 | first = false; |
| 532 | } |
| 533 | |
| 534 | /* |
| 535 | * Msg format is: <priority:1><tag:N>\0<message:N>\0 |
| 536 | * |
| 537 | * We want to display it in the same format as "logcat -v threadtime" |
| 538 | * (although in this case the pid is redundant). |
| 539 | * |
| 540 | * TODO: scan for line breaks ('\n') and display each text line |
| 541 | * on a separate line, prefixed with the header, like logcat does. |
| 542 | */ |
| 543 | static const char* kPrioChars = "!.VDIWEFS"; |
| 544 | unsigned char prio = entry->msg[0]; |
| 545 | char* tag = entry->msg + 1; |
| 546 | char* msg = tag + strlen(tag) + 1; |
| 547 | |
| 548 | /* consume any trailing newlines */ |
| 549 | char* eatnl = msg + strlen(msg) - 1; |
| 550 | while (eatnl >= msg && *eatnl == '\n') { |
| 551 | *eatnl-- = '\0'; |
| 552 | } |
| 553 | |
| 554 | char prioChar = (prio < strlen(kPrioChars) ? kPrioChars[prio] : '?'); |
| 555 | |
| 556 | char timeBuf[32]; |
| 557 | time_t sec = (time_t) entry->sec; |
| 558 | struct tm tmBuf; |
| 559 | struct tm* ptm; |
| 560 | ptm = localtime_r(&sec, &tmBuf); |
| 561 | strftime(timeBuf, sizeof(timeBuf), "%m-%d %H:%M:%S", ptm); |
| 562 | |
| 563 | if (tailOnly) { |
| 564 | snprintf(shortLog[shortLogNext], kShortLogLineLen, |
| 565 | "%s.%03d %5d %5d %c %-8s: %s", |
| 566 | timeBuf, entry->nsec / 1000000, entry->pid, entry->tid, |
| 567 | prioChar, tag, msg); |
| 568 | shortLogNext = (shortLogNext + 1) % kShortLogMaxLines; |
| 569 | shortLogCount++; |
| 570 | } else { |
Christopher Tate | 7716aef | 2013-04-02 14:00:27 -0700 | [diff] [blame] | 571 | _LOG(log, 0, "%s.%03d %5d %5d %c %-8s: %s\n", |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 572 | timeBuf, entry->nsec / 1000000, entry->pid, entry->tid, |
| 573 | prioChar, tag, msg); |
| 574 | } |
| 575 | } |
| 576 | |
| 577 | if (tailOnly) { |
| 578 | int i; |
| 579 | |
| 580 | /* |
| 581 | * If we filled the buffer, we want to start at "next", which has |
| 582 | * the oldest entry. If we didn't, we want to start at zero. |
| 583 | */ |
| 584 | if (shortLogCount < kShortLogMaxLines) { |
| 585 | shortLogNext = 0; |
| 586 | } else { |
| 587 | shortLogCount = kShortLogMaxLines; /* cap at window size */ |
| 588 | } |
| 589 | |
| 590 | for (i = 0; i < shortLogCount; i++) { |
Christopher Tate | 7716aef | 2013-04-02 14:00:27 -0700 | [diff] [blame] | 591 | _LOG(log, 0, "%s\n", shortLog[shortLogNext]); |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 592 | shortLogNext = (shortLogNext + 1) % kShortLogMaxLines; |
| 593 | } |
| 594 | } |
| 595 | |
| 596 | close(logfd); |
| 597 | } |
| 598 | |
| 599 | /* |
| 600 | * Dumps the logs generated by the specified pid to the tombstone, from both |
| 601 | * "system" and "main" log devices. Ideally we'd interleave the output. |
| 602 | */ |
| 603 | static void dump_logs(log_t* log, pid_t pid, bool tailOnly) |
| 604 | { |
| 605 | dump_log_file(log, pid, "/dev/log/system", tailOnly); |
| 606 | dump_log_file(log, pid, "/dev/log/main", tailOnly); |
| 607 | } |
| 608 | |
Christopher Ferris | 365e4ae | 2013-10-02 12:26:48 -0700 | [diff] [blame^] | 609 | static void dump_abort_message(const backtrace_t* backtrace, log_t* log, uintptr_t address) { |
Elliott Hughes | e5f8a69 | 2013-04-04 13:52:01 -0700 | [diff] [blame] | 610 | if (address == 0) { |
| 611 | return; |
| 612 | } |
| 613 | |
| 614 | address += sizeof(size_t); // Skip the buffer length. |
| 615 | |
| 616 | char msg[512]; |
| 617 | memset(msg, 0, sizeof(msg)); |
| 618 | char* p = &msg[0]; |
| 619 | while (p < &msg[sizeof(msg)]) { |
| 620 | uint32_t data; |
Christopher Ferris | 365e4ae | 2013-10-02 12:26:48 -0700 | [diff] [blame^] | 621 | if (!backtrace_read_word(backtrace, address, &data)) { |
Elliott Hughes | e5f8a69 | 2013-04-04 13:52:01 -0700 | [diff] [blame] | 622 | break; |
| 623 | } |
Christopher Ferris | 365e4ae | 2013-10-02 12:26:48 -0700 | [diff] [blame^] | 624 | data = 0; |
Elliott Hughes | e5f8a69 | 2013-04-04 13:52:01 -0700 | [diff] [blame] | 625 | address += sizeof(uint32_t); |
| 626 | |
| 627 | if ((*p++ = (data >> 0) & 0xff) == 0) { |
| 628 | break; |
| 629 | } |
| 630 | if ((*p++ = (data >> 8) & 0xff) == 0) { |
| 631 | break; |
| 632 | } |
| 633 | if ((*p++ = (data >> 16) & 0xff) == 0) { |
| 634 | break; |
| 635 | } |
| 636 | if ((*p++ = (data >> 24) & 0xff) == 0) { |
| 637 | break; |
| 638 | } |
| 639 | } |
| 640 | msg[sizeof(msg) - 1] = '\0'; |
| 641 | |
Christopher Tate | 7716aef | 2013-04-02 14:00:27 -0700 | [diff] [blame] | 642 | _LOG(log, SCOPE_AT_FAULT, "Abort message: '%s'\n", msg); |
Elliott Hughes | e5f8a69 | 2013-04-04 13:52:01 -0700 | [diff] [blame] | 643 | } |
| 644 | |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 645 | /* |
| 646 | * Dumps all information about the specified pid to the tombstone. |
| 647 | */ |
Elliott Hughes | e5f8a69 | 2013-04-04 13:52:01 -0700 | [diff] [blame] | 648 | static bool dump_crash(log_t* log, pid_t pid, pid_t tid, int signal, uintptr_t abort_msg_address, |
| 649 | bool dump_sibling_threads, int* total_sleep_time_usec) |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 650 | { |
| 651 | /* don't copy log messages to tombstone unless this is a dev device */ |
| 652 | char value[PROPERTY_VALUE_MAX]; |
| 653 | property_get("ro.debuggable", value, "0"); |
| 654 | bool want_logs = (value[0] == '1'); |
| 655 | |
Christopher Tate | ded2e5a | 2013-03-19 13:12:23 -0700 | [diff] [blame] | 656 | if (log->amfd >= 0) { |
| 657 | /* |
| 658 | * Activity Manager protocol: binary 32-bit network-byte-order ints for the |
| 659 | * pid and signal number, followed by the raw text of the dump, culminating |
| 660 | * in a zero byte that marks end-of-data. |
| 661 | */ |
| 662 | uint32_t datum = htonl(pid); |
| 663 | TEMP_FAILURE_RETRY( write(log->amfd, &datum, 4) ); |
| 664 | datum = htonl(signal); |
| 665 | TEMP_FAILURE_RETRY( write(log->amfd, &datum, 4) ); |
| 666 | } |
| 667 | |
Christopher Tate | 7716aef | 2013-04-02 14:00:27 -0700 | [diff] [blame] | 668 | _LOG(log, SCOPE_AT_FAULT, |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 669 | "*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***\n"); |
| 670 | dump_build_info(log); |
Ben Cheng | d7760c1 | 2012-09-19 16:04:01 -0700 | [diff] [blame] | 671 | dump_revision_info(log); |
Christopher Ferris | 365e4ae | 2013-10-02 12:26:48 -0700 | [diff] [blame^] | 672 | dump_thread_info(log, pid, tid, SCOPE_AT_FAULT); |
Elliott Hughes | e5f8a69 | 2013-04-04 13:52:01 -0700 | [diff] [blame] | 673 | if (signal) { |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 674 | dump_fault_addr(log, tid, signal); |
| 675 | } |
| 676 | |
Christopher Ferris | 365e4ae | 2013-10-02 12:26:48 -0700 | [diff] [blame^] | 677 | backtrace_t backtrace; |
| 678 | if (backtrace_get_data(&backtrace, tid)) { |
| 679 | dump_abort_message(&backtrace, log, abort_msg_address); |
| 680 | dump_thread(&backtrace, log, SCOPE_AT_FAULT, total_sleep_time_usec); |
| 681 | backtrace_free_data(&backtrace); |
| 682 | } |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 683 | |
| 684 | if (want_logs) { |
| 685 | dump_logs(log, pid, true); |
| 686 | } |
| 687 | |
| 688 | bool detach_failed = false; |
| 689 | if (dump_sibling_threads) { |
Christopher Ferris | 365e4ae | 2013-10-02 12:26:48 -0700 | [diff] [blame^] | 690 | detach_failed = dump_sibling_thread_report(log, pid, tid, total_sleep_time_usec); |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 691 | } |
| 692 | |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 693 | if (want_logs) { |
| 694 | dump_logs(log, pid, false); |
| 695 | } |
Christopher Tate | ded2e5a | 2013-03-19 13:12:23 -0700 | [diff] [blame] | 696 | |
| 697 | /* send EOD to the Activity Manager, then wait for its ack to avoid racing ahead |
| 698 | * and killing the target out from under it */ |
| 699 | if (log->amfd >= 0) { |
| 700 | uint8_t eodMarker = 0; |
| 701 | TEMP_FAILURE_RETRY( write(log->amfd, &eodMarker, 1) ); |
| 702 | /* 3 sec timeout reading the ack; we're fine if that happens */ |
| 703 | TEMP_FAILURE_RETRY( read(log->amfd, &eodMarker, 1) ); |
| 704 | } |
| 705 | |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 706 | return detach_failed; |
| 707 | } |
| 708 | |
| 709 | /* |
| 710 | * find_and_open_tombstone - find an available tombstone slot, if any, of the |
| 711 | * form tombstone_XX where XX is 00 to MAX_TOMBSTONES-1, inclusive. If no |
| 712 | * file is available, we reuse the least-recently-modified file. |
| 713 | * |
| 714 | * Returns the path of the tombstone file, allocated using malloc(). Caller must free() it. |
| 715 | */ |
| 716 | static char* find_and_open_tombstone(int* fd) |
| 717 | { |
| 718 | unsigned long mtime = ULONG_MAX; |
| 719 | struct stat sb; |
| 720 | |
| 721 | /* |
| 722 | * XXX: Our stat.st_mtime isn't time_t. If it changes, as it probably ought |
| 723 | * to, our logic breaks. This check will generate a warning if that happens. |
| 724 | */ |
| 725 | typecheck(mtime, sb.st_mtime); |
| 726 | |
| 727 | /* |
| 728 | * In a single wolf-like pass, find an available slot and, in case none |
| 729 | * exist, find and record the least-recently-modified file. |
| 730 | */ |
| 731 | char path[128]; |
| 732 | int oldest = 0; |
| 733 | for (int i = 0; i < MAX_TOMBSTONES; i++) { |
| 734 | snprintf(path, sizeof(path), TOMBSTONE_DIR"/tombstone_%02d", i); |
| 735 | |
| 736 | if (!stat(path, &sb)) { |
| 737 | if (sb.st_mtime < mtime) { |
| 738 | oldest = i; |
| 739 | mtime = sb.st_mtime; |
| 740 | } |
| 741 | continue; |
| 742 | } |
| 743 | if (errno != ENOENT) |
| 744 | continue; |
| 745 | |
| 746 | *fd = open(path, O_CREAT | O_EXCL | O_WRONLY, 0600); |
| 747 | if (*fd < 0) |
| 748 | continue; /* raced ? */ |
| 749 | |
| 750 | fchown(*fd, AID_SYSTEM, AID_SYSTEM); |
| 751 | return strdup(path); |
| 752 | } |
| 753 | |
| 754 | /* we didn't find an available file, so we clobber the oldest one */ |
| 755 | snprintf(path, sizeof(path), TOMBSTONE_DIR"/tombstone_%02d", oldest); |
| 756 | *fd = open(path, O_CREAT | O_TRUNC | O_WRONLY, 0600); |
| 757 | if (*fd < 0) { |
| 758 | LOG("failed to open tombstone file '%s': %s\n", path, strerror(errno)); |
| 759 | return NULL; |
| 760 | } |
| 761 | fchown(*fd, AID_SYSTEM, AID_SYSTEM); |
| 762 | return strdup(path); |
| 763 | } |
| 764 | |
Christopher Tate | ded2e5a | 2013-03-19 13:12:23 -0700 | [diff] [blame] | 765 | static int activity_manager_connect() { |
| 766 | int amfd = socket(PF_UNIX, SOCK_STREAM, 0); |
| 767 | if (amfd >= 0) { |
| 768 | struct sockaddr_un address; |
| 769 | int err; |
| 770 | |
| 771 | memset(&address, 0, sizeof(address)); |
| 772 | address.sun_family = AF_UNIX; |
| 773 | strncpy(address.sun_path, NCRASH_SOCKET_PATH, sizeof(address.sun_path)); |
| 774 | err = TEMP_FAILURE_RETRY( connect(amfd, (struct sockaddr*) &address, sizeof(address)) ); |
| 775 | if (!err) { |
| 776 | struct timeval tv; |
| 777 | memset(&tv, 0, sizeof(tv)); |
| 778 | tv.tv_sec = 1; // tight leash |
| 779 | err = setsockopt(amfd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)); |
| 780 | if (!err) { |
| 781 | tv.tv_sec = 3; // 3 seconds on handshake read |
| 782 | err = setsockopt(amfd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)); |
| 783 | } |
| 784 | } |
| 785 | if (err) { |
| 786 | close(amfd); |
| 787 | amfd = -1; |
| 788 | } |
| 789 | } |
| 790 | |
| 791 | return amfd; |
| 792 | } |
| 793 | |
Elliott Hughes | e5f8a69 | 2013-04-04 13:52:01 -0700 | [diff] [blame] | 794 | char* engrave_tombstone(pid_t pid, pid_t tid, int signal, uintptr_t abort_msg_address, |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 795 | bool dump_sibling_threads, bool quiet, bool* detach_failed, |
| 796 | int* total_sleep_time_usec) { |
| 797 | mkdir(TOMBSTONE_DIR, 0755); |
| 798 | chown(TOMBSTONE_DIR, AID_SYSTEM, AID_SYSTEM); |
| 799 | |
rpcraig | f1186f3 | 2012-07-19 09:38:06 -0400 | [diff] [blame] | 800 | if (selinux_android_restorecon(TOMBSTONE_DIR) == -1) { |
| 801 | *detach_failed = false; |
| 802 | return NULL; |
| 803 | } |
rpcraig | f1186f3 | 2012-07-19 09:38:06 -0400 | [diff] [blame] | 804 | |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 805 | int fd; |
| 806 | char* path = find_and_open_tombstone(&fd); |
| 807 | if (!path) { |
| 808 | *detach_failed = false; |
| 809 | return NULL; |
| 810 | } |
| 811 | |
| 812 | log_t log; |
| 813 | log.tfd = fd; |
Christopher Tate | ded2e5a | 2013-03-19 13:12:23 -0700 | [diff] [blame] | 814 | log.amfd = activity_manager_connect(); |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 815 | log.quiet = quiet; |
Elliott Hughes | e5f8a69 | 2013-04-04 13:52:01 -0700 | [diff] [blame] | 816 | *detach_failed = dump_crash(&log, pid, tid, signal, abort_msg_address, dump_sibling_threads, |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 817 | total_sleep_time_usec); |
| 818 | |
Christopher Tate | ded2e5a | 2013-03-19 13:12:23 -0700 | [diff] [blame] | 819 | close(log.amfd); |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 820 | close(fd); |
| 821 | return path; |
| 822 | } |