The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1 | /* system/debuggerd/debuggerd.c |
| 2 | ** |
| 3 | ** Copyright 2006, The Android Open Source Project |
| 4 | ** |
Ben Cheng | 09e7137 | 2009-09-28 11:06:09 -0700 | [diff] [blame] | 5 | ** Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | ** you may not use this file except in compliance with the License. |
| 7 | ** You may obtain a copy of the License at |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 8 | ** |
Ben Cheng | 09e7137 | 2009-09-28 11:06:09 -0700 | [diff] [blame] | 9 | ** http://www.apache.org/licenses/LICENSE-2.0 |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 10 | ** |
Ben Cheng | 09e7137 | 2009-09-28 11:06:09 -0700 | [diff] [blame] | 11 | ** Unless required by applicable law or agreed to in writing, software |
| 12 | ** distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | ** See the License for the specific language governing permissions and |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 15 | ** limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | #include <stdio.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 19 | #include <errno.h> |
| 20 | #include <signal.h> |
| 21 | #include <pthread.h> |
| 22 | #include <stdarg.h> |
| 23 | #include <fcntl.h> |
| 24 | #include <sys/types.h> |
| 25 | #include <dirent.h> |
| 26 | |
| 27 | #include <sys/ptrace.h> |
| 28 | #include <sys/wait.h> |
| 29 | #include <sys/exec_elf.h> |
| 30 | #include <sys/stat.h> |
| 31 | |
| 32 | #include <cutils/sockets.h> |
| 33 | #include <cutils/logd.h> |
Andy McFadden | 41e0cef | 2011-10-13 16:05:08 -0700 | [diff] [blame] | 34 | #include <cutils/logger.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 35 | #include <cutils/properties.h> |
| 36 | |
Jeff Brown | 13e715b | 2011-10-21 12:14:56 -0700 | [diff] [blame^] | 37 | #include <corkscrew/backtrace.h> |
| 38 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 39 | #include <linux/input.h> |
| 40 | |
| 41 | #include <private/android_filesystem_config.h> |
| 42 | |
Jeff Brown | 13e715b | 2011-10-21 12:14:56 -0700 | [diff] [blame^] | 43 | #include "getevent.h" |
| 44 | #include "machine.h" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 45 | #include "utility.h" |
| 46 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 47 | #define ANDROID_LOG_INFO 4 |
| 48 | |
Jeff Brown | 13e715b | 2011-10-21 12:14:56 -0700 | [diff] [blame^] | 49 | static void dump_build_info(int tfd) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 50 | { |
| 51 | char fingerprint[PROPERTY_VALUE_MAX]; |
| 52 | |
| 53 | property_get("ro.build.fingerprint", fingerprint, "unknown"); |
| 54 | |
| 55 | _LOG(tfd, false, "Build fingerprint: '%s'\n", fingerprint); |
| 56 | } |
| 57 | |
Jeff Brown | 13e715b | 2011-10-21 12:14:56 -0700 | [diff] [blame^] | 58 | static const char *get_signame(int sig) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 59 | { |
| 60 | switch(sig) { |
| 61 | case SIGILL: return "SIGILL"; |
| 62 | case SIGABRT: return "SIGABRT"; |
| 63 | case SIGBUS: return "SIGBUS"; |
| 64 | case SIGFPE: return "SIGFPE"; |
| 65 | case SIGSEGV: return "SIGSEGV"; |
| 66 | case SIGSTKFLT: return "SIGSTKFLT"; |
| 67 | default: return "?"; |
| 68 | } |
| 69 | } |
| 70 | |
Jeff Brown | 13e715b | 2011-10-21 12:14:56 -0700 | [diff] [blame^] | 71 | static const char *get_sigcode(int signo, int code) |
Carl Shapiro | 83c6b05 | 2010-10-08 18:10:24 -0700 | [diff] [blame] | 72 | { |
| 73 | switch (signo) { |
| 74 | case SIGILL: |
| 75 | switch (code) { |
| 76 | case ILL_ILLOPC: return "ILL_ILLOPC"; |
| 77 | case ILL_ILLOPN: return "ILL_ILLOPN"; |
| 78 | case ILL_ILLADR: return "ILL_ILLADR"; |
| 79 | case ILL_ILLTRP: return "ILL_ILLTRP"; |
| 80 | case ILL_PRVOPC: return "ILL_PRVOPC"; |
| 81 | case ILL_PRVREG: return "ILL_PRVREG"; |
| 82 | case ILL_COPROC: return "ILL_COPROC"; |
| 83 | case ILL_BADSTK: return "ILL_BADSTK"; |
| 84 | } |
| 85 | break; |
| 86 | case SIGBUS: |
| 87 | switch (code) { |
| 88 | case BUS_ADRALN: return "BUS_ADRALN"; |
| 89 | case BUS_ADRERR: return "BUS_ADRERR"; |
| 90 | case BUS_OBJERR: return "BUS_OBJERR"; |
| 91 | } |
| 92 | break; |
| 93 | case SIGFPE: |
| 94 | switch (code) { |
| 95 | case FPE_INTDIV: return "FPE_INTDIV"; |
| 96 | case FPE_INTOVF: return "FPE_INTOVF"; |
| 97 | case FPE_FLTDIV: return "FPE_FLTDIV"; |
| 98 | case FPE_FLTOVF: return "FPE_FLTOVF"; |
| 99 | case FPE_FLTUND: return "FPE_FLTUND"; |
| 100 | case FPE_FLTRES: return "FPE_FLTRES"; |
| 101 | case FPE_FLTINV: return "FPE_FLTINV"; |
| 102 | case FPE_FLTSUB: return "FPE_FLTSUB"; |
| 103 | } |
| 104 | break; |
| 105 | case SIGSEGV: |
| 106 | switch (code) { |
| 107 | case SEGV_MAPERR: return "SEGV_MAPERR"; |
| 108 | case SEGV_ACCERR: return "SEGV_ACCERR"; |
| 109 | } |
| 110 | break; |
| 111 | } |
| 112 | return "?"; |
| 113 | } |
| 114 | |
Jeff Brown | 13e715b | 2011-10-21 12:14:56 -0700 | [diff] [blame^] | 115 | static void dump_fault_addr(int tfd, pid_t pid, int sig) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 116 | { |
| 117 | siginfo_t si; |
Ben Cheng | 09e7137 | 2009-09-28 11:06:09 -0700 | [diff] [blame] | 118 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 119 | memset(&si, 0, sizeof(si)); |
| 120 | if(ptrace(PTRACE_GETSIGINFO, pid, 0, &si)){ |
| 121 | _LOG(tfd, false, "cannot get siginfo: %s\n", strerror(errno)); |
Andy McFadden | 136dcc5 | 2011-09-22 16:37:06 -0700 | [diff] [blame] | 122 | } else if (signal_has_address(sig)) { |
Carl Shapiro | 83c6b05 | 2010-10-08 18:10:24 -0700 | [diff] [blame] | 123 | _LOG(tfd, false, "signal %d (%s), code %d (%s), fault addr %08x\n", |
| 124 | sig, get_signame(sig), |
| 125 | si.si_code, get_sigcode(sig, si.si_code), |
Andy McFadden | e5cc539 | 2011-10-18 20:03:07 -0700 | [diff] [blame] | 126 | (uintptr_t) si.si_addr); |
Andy McFadden | 136dcc5 | 2011-09-22 16:37:06 -0700 | [diff] [blame] | 127 | } else { |
| 128 | _LOG(tfd, false, "signal %d (%s), code %d (%s), fault addr --------\n", |
| 129 | sig, get_signame(sig), si.si_code, get_sigcode(sig, si.si_code)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | |
Jeff Brown | 13e715b | 2011-10-21 12:14:56 -0700 | [diff] [blame^] | 133 | static void dump_crash_banner(int tfd, pid_t pid, pid_t tid, int sig) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 134 | { |
| 135 | char data[1024]; |
| 136 | char *x = 0; |
| 137 | FILE *fp; |
Ben Cheng | 09e7137 | 2009-09-28 11:06:09 -0700 | [diff] [blame] | 138 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 139 | sprintf(data, "/proc/%d/cmdline", pid); |
| 140 | fp = fopen(data, "r"); |
| 141 | if(fp) { |
| 142 | x = fgets(data, 1024, fp); |
| 143 | fclose(fp); |
| 144 | } |
Ben Cheng | 09e7137 | 2009-09-28 11:06:09 -0700 | [diff] [blame] | 145 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 146 | _LOG(tfd, false, |
Jeff Brown | 13e715b | 2011-10-21 12:14:56 -0700 | [diff] [blame^] | 147 | "*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***\n"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 148 | dump_build_info(tfd); |
| 149 | _LOG(tfd, false, "pid: %d, tid: %d >>> %s <<<\n", |
| 150 | pid, tid, x ? x : "UNKNOWN"); |
Ben Cheng | 09e7137 | 2009-09-28 11:06:09 -0700 | [diff] [blame] | 151 | |
Jeff Brown | 13e715b | 2011-10-21 12:14:56 -0700 | [diff] [blame^] | 152 | if(sig) { |
| 153 | dump_fault_addr(tfd, tid, sig); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 154 | } |
| 155 | } |
| 156 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 157 | /* Return true if some thread is not detached cleanly */ |
Jeff Brown | 13e715b | 2011-10-21 12:14:56 -0700 | [diff] [blame^] | 158 | static bool dump_sibling_thread_report(ptrace_context_t* context, |
| 159 | int tfd, pid_t pid, pid_t tid) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 160 | { |
| 161 | char task_path[1024]; |
| 162 | |
| 163 | sprintf(task_path, "/proc/%d/task", pid); |
| 164 | DIR *d; |
| 165 | struct dirent *de; |
| 166 | int need_cleanup = 0; |
| 167 | |
| 168 | d = opendir(task_path); |
| 169 | /* Bail early if cannot open the task directory */ |
| 170 | if (d == NULL) { |
| 171 | XLOG("Cannot open /proc/%d/task\n", pid); |
| 172 | return false; |
| 173 | } |
| 174 | while ((de = readdir(d)) != NULL) { |
Jeff Brown | 13e715b | 2011-10-21 12:14:56 -0700 | [diff] [blame^] | 175 | pid_t new_tid; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 176 | /* Ignore "." and ".." */ |
Ben Cheng | 09e7137 | 2009-09-28 11:06:09 -0700 | [diff] [blame] | 177 | if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, "..")) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 178 | continue; |
| 179 | new_tid = atoi(de->d_name); |
| 180 | /* The main thread at fault has been handled individually */ |
| 181 | if (new_tid == tid) |
| 182 | continue; |
| 183 | |
| 184 | /* Skip this thread if cannot ptrace it */ |
| 185 | if (ptrace(PTRACE_ATTACH, new_tid, 0, 0) < 0) |
| 186 | continue; |
| 187 | |
Jeff Brown | 13e715b | 2011-10-21 12:14:56 -0700 | [diff] [blame^] | 188 | _LOG(tfd, true, |
| 189 | "--- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---\n"); |
| 190 | _LOG(tfd, true, "pid: %d, tid: %d\n", pid, new_tid); |
| 191 | |
| 192 | dump_thread(context, tfd, new_tid, false); |
Andy McFadden | e5cc539 | 2011-10-18 20:03:07 -0700 | [diff] [blame] | 193 | |
| 194 | if (ptrace(PTRACE_DETACH, new_tid, 0, 0) != 0) { |
| 195 | XLOG("detach of tid %d failed: %s\n", new_tid, strerror(errno)); |
| 196 | need_cleanup = 1; |
| 197 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 198 | } |
| 199 | closedir(d); |
Andy McFadden | e5cc539 | 2011-10-18 20:03:07 -0700 | [diff] [blame] | 200 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 201 | return need_cleanup != 0; |
| 202 | } |
| 203 | |
Andy McFadden | 41e0cef | 2011-10-13 16:05:08 -0700 | [diff] [blame] | 204 | /* |
| 205 | * Reads the contents of the specified log device, filters out the entries |
| 206 | * that don't match the specified pid, and writes them to the tombstone file. |
Andy McFadden | e5cc539 | 2011-10-18 20:03:07 -0700 | [diff] [blame] | 207 | * |
| 208 | * If "tailOnly" is set, we only print the last few lines. |
Andy McFadden | 41e0cef | 2011-10-13 16:05:08 -0700 | [diff] [blame] | 209 | */ |
Jeff Brown | 13e715b | 2011-10-21 12:14:56 -0700 | [diff] [blame^] | 210 | static void dump_log_file(int tfd, pid_t pid, const char* filename, |
Andy McFadden | e5cc539 | 2011-10-18 20:03:07 -0700 | [diff] [blame] | 211 | bool tailOnly) |
Andy McFadden | 41e0cef | 2011-10-13 16:05:08 -0700 | [diff] [blame] | 212 | { |
Andy McFadden | e5cc539 | 2011-10-18 20:03:07 -0700 | [diff] [blame] | 213 | bool first = true; |
| 214 | |
| 215 | /* circular buffer, for "tailOnly" mode */ |
| 216 | const int kShortLogMaxLines = 5; |
| 217 | const int kShortLogLineLen = 256; |
| 218 | char shortLog[kShortLogMaxLines][kShortLogLineLen]; |
| 219 | int shortLogCount = 0; |
| 220 | int shortLogNext = 0; |
| 221 | |
Andy McFadden | 41e0cef | 2011-10-13 16:05:08 -0700 | [diff] [blame] | 222 | int logfd = open(filename, O_RDONLY | O_NONBLOCK); |
| 223 | if (logfd < 0) { |
| 224 | XLOG("Unable to open %s: %s\n", filename, strerror(errno)); |
| 225 | return; |
| 226 | } |
Andy McFadden | 41e0cef | 2011-10-13 16:05:08 -0700 | [diff] [blame] | 227 | |
| 228 | union { |
| 229 | unsigned char buf[LOGGER_ENTRY_MAX_LEN + 1]; |
| 230 | struct logger_entry entry; |
| 231 | } log_entry; |
| 232 | |
| 233 | while (true) { |
| 234 | ssize_t actual = read(logfd, log_entry.buf, LOGGER_ENTRY_MAX_LEN); |
| 235 | if (actual < 0) { |
| 236 | if (errno == EINTR) { |
| 237 | /* interrupted by signal, retry */ |
| 238 | continue; |
| 239 | } else if (errno == EAGAIN) { |
| 240 | /* non-blocking EOF; we're done */ |
| 241 | break; |
| 242 | } else { |
| 243 | _LOG(tfd, true, "Error while reading log: %s\n", |
| 244 | strerror(errno)); |
| 245 | break; |
| 246 | } |
| 247 | } else if (actual == 0) { |
| 248 | _LOG(tfd, true, "Got zero bytes while reading log: %s\n", |
| 249 | strerror(errno)); |
| 250 | break; |
| 251 | } |
| 252 | |
| 253 | /* |
| 254 | * NOTE: if you XLOG something here, this will spin forever, |
| 255 | * because you will be writing as fast as you're reading. Any |
| 256 | * high-frequency debug diagnostics should just be written to |
| 257 | * the tombstone file. |
| 258 | */ |
| 259 | |
| 260 | struct logger_entry* entry = &log_entry.entry; |
| 261 | |
| 262 | if (entry->pid != (int32_t) pid) { |
| 263 | /* wrong pid, ignore */ |
| 264 | continue; |
| 265 | } |
| 266 | |
Andy McFadden | e5cc539 | 2011-10-18 20:03:07 -0700 | [diff] [blame] | 267 | if (first) { |
| 268 | _LOG(tfd, true, "--------- %slog %s\n", |
| 269 | tailOnly ? "tail end of " : "", filename); |
| 270 | first = false; |
| 271 | } |
| 272 | |
Andy McFadden | 41e0cef | 2011-10-13 16:05:08 -0700 | [diff] [blame] | 273 | /* |
| 274 | * Msg format is: <priority:1><tag:N>\0<message:N>\0 |
| 275 | * |
| 276 | * We want to display it in the same format as "logcat -v threadtime" |
| 277 | * (although in this case the pid is redundant). |
| 278 | * |
| 279 | * TODO: scan for line breaks ('\n') and display each text line |
| 280 | * on a separate line, prefixed with the header, like logcat does. |
| 281 | */ |
| 282 | static const char* kPrioChars = "!.VDIWEFS"; |
| 283 | unsigned char prio = entry->msg[0]; |
Andy McFadden | e5cc539 | 2011-10-18 20:03:07 -0700 | [diff] [blame] | 284 | char* tag = entry->msg + 1; |
| 285 | char* msg = tag + strlen(tag) + 1; |
Andy McFadden | 41e0cef | 2011-10-13 16:05:08 -0700 | [diff] [blame] | 286 | |
Andy McFadden | e5cc539 | 2011-10-18 20:03:07 -0700 | [diff] [blame] | 287 | /* consume any trailing newlines */ |
| 288 | char* eatnl = msg + strlen(msg) - 1; |
| 289 | while (eatnl >= msg && *eatnl == '\n') { |
| 290 | *eatnl-- = '\0'; |
| 291 | } |
| 292 | |
| 293 | char prioChar = (prio < strlen(kPrioChars) ? kPrioChars[prio] : '?'); |
Andy McFadden | 41e0cef | 2011-10-13 16:05:08 -0700 | [diff] [blame] | 294 | |
| 295 | char timeBuf[32]; |
| 296 | time_t sec = (time_t) entry->sec; |
| 297 | struct tm tmBuf; |
| 298 | struct tm* ptm; |
| 299 | ptm = localtime_r(&sec, &tmBuf); |
| 300 | strftime(timeBuf, sizeof(timeBuf), "%m-%d %H:%M:%S", ptm); |
| 301 | |
Andy McFadden | e5cc539 | 2011-10-18 20:03:07 -0700 | [diff] [blame] | 302 | if (tailOnly) { |
| 303 | snprintf(shortLog[shortLogNext], kShortLogLineLen, |
| 304 | "%s.%03d %5d %5d %c %-8s: %s", |
| 305 | timeBuf, entry->nsec / 1000000, entry->pid, entry->tid, |
| 306 | prioChar, tag, msg); |
| 307 | shortLogNext = (shortLogNext + 1) % kShortLogMaxLines; |
| 308 | shortLogCount++; |
| 309 | } else { |
| 310 | _LOG(tfd, true, "%s.%03d %5d %5d %c %-8s: %s\n", |
| 311 | timeBuf, entry->nsec / 1000000, entry->pid, entry->tid, |
| 312 | prioChar, tag, msg); |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | if (tailOnly) { |
| 317 | int i; |
| 318 | |
| 319 | /* |
| 320 | * If we filled the buffer, we want to start at "next", which has |
| 321 | * the oldest entry. If we didn't, we want to start at zero. |
| 322 | */ |
| 323 | if (shortLogCount < kShortLogMaxLines) { |
| 324 | shortLogNext = 0; |
| 325 | } else { |
| 326 | shortLogCount = kShortLogMaxLines; /* cap at window size */ |
| 327 | } |
| 328 | |
| 329 | for (i = 0; i < shortLogCount; i++) { |
| 330 | _LOG(tfd, true, "%s\n", shortLog[shortLogNext]); |
| 331 | shortLogNext = (shortLogNext + 1) % kShortLogMaxLines; |
| 332 | } |
Andy McFadden | 41e0cef | 2011-10-13 16:05:08 -0700 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | close(logfd); |
| 336 | } |
| 337 | |
| 338 | /* |
| 339 | * Dumps the logs generated by the specified pid to the tombstone, from both |
| 340 | * "system" and "main" log devices. Ideally we'd interleave the output. |
| 341 | */ |
Jeff Brown | 13e715b | 2011-10-21 12:14:56 -0700 | [diff] [blame^] | 342 | static void dump_logs(int tfd, pid_t pid, bool tailOnly) |
Andy McFadden | 41e0cef | 2011-10-13 16:05:08 -0700 | [diff] [blame] | 343 | { |
Andy McFadden | e5cc539 | 2011-10-18 20:03:07 -0700 | [diff] [blame] | 344 | dump_log_file(tfd, pid, "/dev/log/system", tailOnly); |
| 345 | dump_log_file(tfd, pid, "/dev/log/main", tailOnly); |
Andy McFadden | 41e0cef | 2011-10-13 16:05:08 -0700 | [diff] [blame] | 346 | } |
| 347 | |
Jeff Brown | 13e715b | 2011-10-21 12:14:56 -0700 | [diff] [blame^] | 348 | /* |
| 349 | * Dumps all information about the specified pid to the tombstone. |
| 350 | */ |
| 351 | static bool dump_crash(int tfd, pid_t pid, pid_t tid, int signal, |
| 352 | bool dump_sibling_threads) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 353 | { |
Andy McFadden | e5cc539 | 2011-10-18 20:03:07 -0700 | [diff] [blame] | 354 | /* don't copy log messages to tombstone unless this is a dev device */ |
| 355 | char value[PROPERTY_VALUE_MAX]; |
| 356 | property_get("ro.debuggable", value, "0"); |
| 357 | bool wantLogs = (value[0] == '1'); |
Jeff Brown | 13e715b | 2011-10-21 12:14:56 -0700 | [diff] [blame^] | 358 | bool need_cleanup = false; |
| 359 | |
| 360 | dump_crash_banner(tfd, pid, tid, signal); |
| 361 | |
| 362 | ptrace_context_t* context = load_ptrace_context(pid); |
| 363 | |
| 364 | dump_thread(context, tfd, tid, true); |
| 365 | |
| 366 | if (wantLogs) { |
| 367 | dump_logs(tfd, pid, true); |
| 368 | } |
| 369 | |
| 370 | if (dump_sibling_threads) { |
| 371 | need_cleanup = dump_sibling_thread_report(context, tfd, pid, tid); |
| 372 | } |
| 373 | |
| 374 | free_ptrace_context(context); |
| 375 | |
| 376 | if (wantLogs) { |
| 377 | dump_logs(tfd, pid, false); |
| 378 | } |
| 379 | return need_cleanup; |
| 380 | } |
| 381 | |
| 382 | #define MAX_TOMBSTONES 10 |
| 383 | |
| 384 | #define typecheck(x,y) { \ |
| 385 | typeof(x) __dummy1; \ |
| 386 | typeof(y) __dummy2; \ |
| 387 | (void)(&__dummy1 == &__dummy2); } |
| 388 | |
| 389 | #define TOMBSTONE_DIR "/data/tombstones" |
| 390 | |
| 391 | /* |
| 392 | * find_and_open_tombstone - find an available tombstone slot, if any, of the |
| 393 | * form tombstone_XX where XX is 00 to MAX_TOMBSTONES-1, inclusive. If no |
| 394 | * file is available, we reuse the least-recently-modified file. |
| 395 | */ |
| 396 | static int find_and_open_tombstone(void) |
| 397 | { |
| 398 | unsigned long mtime = ULONG_MAX; |
| 399 | struct stat sb; |
| 400 | char path[128]; |
| 401 | int fd, i, oldest = 0; |
| 402 | |
| 403 | /* |
| 404 | * XXX: Our stat.st_mtime isn't time_t. If it changes, as it probably ought |
| 405 | * to, our logic breaks. This check will generate a warning if that happens. |
| 406 | */ |
| 407 | typecheck(mtime, sb.st_mtime); |
| 408 | |
| 409 | /* |
| 410 | * In a single wolf-like pass, find an available slot and, in case none |
| 411 | * exist, find and record the least-recently-modified file. |
| 412 | */ |
| 413 | for (i = 0; i < MAX_TOMBSTONES; i++) { |
| 414 | snprintf(path, sizeof(path), TOMBSTONE_DIR"/tombstone_%02d", i); |
| 415 | |
| 416 | if (!stat(path, &sb)) { |
| 417 | if (sb.st_mtime < mtime) { |
| 418 | oldest = i; |
| 419 | mtime = sb.st_mtime; |
| 420 | } |
| 421 | continue; |
| 422 | } |
| 423 | if (errno != ENOENT) |
| 424 | continue; |
| 425 | |
| 426 | fd = open(path, O_CREAT | O_EXCL | O_WRONLY, 0600); |
| 427 | if (fd < 0) |
| 428 | continue; /* raced ? */ |
| 429 | |
| 430 | fchown(fd, AID_SYSTEM, AID_SYSTEM); |
| 431 | return fd; |
| 432 | } |
| 433 | |
| 434 | /* we didn't find an available file, so we clobber the oldest one */ |
| 435 | snprintf(path, sizeof(path), TOMBSTONE_DIR"/tombstone_%02d", oldest); |
| 436 | fd = open(path, O_CREAT | O_TRUNC | O_WRONLY, 0600); |
| 437 | fchown(fd, AID_SYSTEM, AID_SYSTEM); |
| 438 | |
| 439 | return fd; |
| 440 | } |
| 441 | |
| 442 | /* Return true if some thread is not detached cleanly */ |
| 443 | static bool engrave_tombstone(pid_t pid, pid_t tid, int signal, |
| 444 | bool dump_sibling_threads) |
| 445 | { |
| 446 | int fd; |
| 447 | bool need_cleanup = false; |
Andy McFadden | e5cc539 | 2011-10-18 20:03:07 -0700 | [diff] [blame] | 448 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 449 | mkdir(TOMBSTONE_DIR, 0755); |
| 450 | chown(TOMBSTONE_DIR, AID_SYSTEM, AID_SYSTEM); |
| 451 | |
| 452 | fd = find_and_open_tombstone(); |
| 453 | if (fd < 0) |
| 454 | return need_cleanup; |
| 455 | |
Jeff Brown | 13e715b | 2011-10-21 12:14:56 -0700 | [diff] [blame^] | 456 | need_cleanup = dump_crash(fd, pid, tid, signal, dump_sibling_threads); |
Andy McFadden | 41e0cef | 2011-10-13 16:05:08 -0700 | [diff] [blame] | 457 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 458 | close(fd); |
| 459 | return need_cleanup; |
| 460 | } |
| 461 | |
| 462 | static int |
| 463 | write_string(const char* file, const char* string) |
| 464 | { |
| 465 | int len; |
| 466 | int fd; |
| 467 | ssize_t amt; |
| 468 | fd = open(file, O_RDWR); |
| 469 | len = strlen(string); |
| 470 | if (fd < 0) |
| 471 | return -errno; |
| 472 | amt = write(fd, string, len); |
| 473 | close(fd); |
| 474 | return amt >= 0 ? 0 : -errno; |
| 475 | } |
| 476 | |
| 477 | static |
| 478 | void init_debug_led(void) |
| 479 | { |
| 480 | // trout leds |
| 481 | write_string("/sys/class/leds/red/brightness", "0"); |
| 482 | write_string("/sys/class/leds/green/brightness", "0"); |
| 483 | write_string("/sys/class/leds/blue/brightness", "0"); |
| 484 | write_string("/sys/class/leds/red/device/blink", "0"); |
| 485 | // sardine leds |
| 486 | write_string("/sys/class/leds/left/cadence", "0,0"); |
| 487 | } |
| 488 | |
| 489 | static |
| 490 | void enable_debug_led(void) |
| 491 | { |
| 492 | // trout leds |
| 493 | write_string("/sys/class/leds/red/brightness", "255"); |
| 494 | // sardine leds |
| 495 | write_string("/sys/class/leds/left/cadence", "1,0"); |
| 496 | } |
| 497 | |
| 498 | static |
| 499 | void disable_debug_led(void) |
| 500 | { |
| 501 | // trout leds |
| 502 | write_string("/sys/class/leds/red/brightness", "0"); |
| 503 | // sardine leds |
| 504 | write_string("/sys/class/leds/left/cadence", "0,0"); |
| 505 | } |
| 506 | |
Jeff Brown | 13e715b | 2011-10-21 12:14:56 -0700 | [diff] [blame^] | 507 | static void wait_for_user_action(pid_t tid, struct ucred* cr) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 508 | { |
| 509 | (void)tid; |
| 510 | /* First log a helpful message */ |
| 511 | LOG( "********************************************************\n" |
Andy McFadden | 3bfdcc9 | 2009-12-01 12:37:26 -0800 | [diff] [blame] | 512 | "* Process %d has been suspended while crashing. To\n" |
| 513 | "* attach gdbserver for a gdb connection on port 5039:\n" |
| 514 | "*\n" |
| 515 | "* adb shell gdbserver :5039 --attach %d &\n" |
| 516 | "*\n" |
| 517 | "* Press HOME key to let the process continue crashing.\n" |
Ben Cheng | 09e7137 | 2009-09-28 11:06:09 -0700 | [diff] [blame] | 518 | "********************************************************\n", |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 519 | cr->pid, cr->pid); |
| 520 | |
Andy McFadden | 3bfdcc9 | 2009-12-01 12:37:26 -0800 | [diff] [blame] | 521 | /* wait for HOME key (TODO: something useful for devices w/o HOME key) */ |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 522 | if (init_getevent() == 0) { |
| 523 | int ms = 1200 / 10; |
| 524 | int dit = 1; |
| 525 | int dah = 3*dit; |
| 526 | int _ = -dit; |
| 527 | int ___ = 3*_; |
| 528 | int _______ = 7*_; |
| 529 | const signed char codes[] = { |
| 530 | dit,_,dit,_,dit,___,dah,_,dah,_,dah,___,dit,_,dit,_,dit,_______ |
| 531 | }; |
| 532 | size_t s = 0; |
| 533 | struct input_event e; |
| 534 | int home = 0; |
| 535 | init_debug_led(); |
| 536 | enable_debug_led(); |
| 537 | do { |
| 538 | int timeout = abs((int)(codes[s])) * ms; |
| 539 | int res = get_event(&e, timeout); |
| 540 | if (res == 0) { |
| 541 | if (e.type==EV_KEY && e.code==KEY_HOME && e.value==0) |
| 542 | home = 1; |
| 543 | } else if (res == 1) { |
| 544 | if (++s >= sizeof(codes)/sizeof(*codes)) |
| 545 | s = 0; |
| 546 | if (codes[s] > 0) { |
| 547 | enable_debug_led(); |
| 548 | } else { |
| 549 | disable_debug_led(); |
| 550 | } |
| 551 | } |
Ben Cheng | 09e7137 | 2009-09-28 11:06:09 -0700 | [diff] [blame] | 552 | } while (!home); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 553 | uninit_getevent(); |
| 554 | } |
| 555 | |
| 556 | /* don't forget to turn debug led off */ |
| 557 | disable_debug_led(); |
Ben Cheng | 09e7137 | 2009-09-28 11:06:09 -0700 | [diff] [blame] | 558 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 559 | /* close filedescriptor */ |
| 560 | LOG("debuggerd resuming process %d", cr->pid); |
| 561 | } |
| 562 | |
| 563 | static void handle_crashing_process(int fd) |
| 564 | { |
| 565 | char buf[64]; |
| 566 | struct stat s; |
Jeff Brown | 13e715b | 2011-10-21 12:14:56 -0700 | [diff] [blame^] | 567 | pid_t tid; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 568 | struct ucred cr; |
Ben Cheng | 09e7137 | 2009-09-28 11:06:09 -0700 | [diff] [blame] | 569 | int n, len, status; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 570 | int tid_attach_status = -1; |
| 571 | unsigned retry = 30; |
| 572 | bool need_cleanup = false; |
| 573 | |
Jeff Brown | 13e715b | 2011-10-21 12:14:56 -0700 | [diff] [blame^] | 574 | XLOG("handle_crashing_process(%d)\n", fd); |
| 575 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 576 | char value[PROPERTY_VALUE_MAX]; |
| 577 | property_get("debug.db.uid", value, "-1"); |
| 578 | int debug_uid = atoi(value); |
Ben Cheng | 09e7137 | 2009-09-28 11:06:09 -0700 | [diff] [blame] | 579 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 580 | len = sizeof(cr); |
| 581 | n = getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &cr, &len); |
| 582 | if(n != 0) { |
| 583 | LOG("cannot get credentials\n"); |
| 584 | goto done; |
| 585 | } |
| 586 | |
Ben Cheng | 09e7137 | 2009-09-28 11:06:09 -0700 | [diff] [blame] | 587 | XLOG("reading tid\n"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 588 | fcntl(fd, F_SETFL, O_NONBLOCK); |
Jeff Brown | 13e715b | 2011-10-21 12:14:56 -0700 | [diff] [blame^] | 589 | while((n = read(fd, &tid, sizeof(pid_t))) != sizeof(pid_t)) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 590 | if(errno == EINTR) continue; |
| 591 | if(errno == EWOULDBLOCK) { |
| 592 | if(retry-- > 0) { |
| 593 | usleep(100 * 1000); |
| 594 | continue; |
| 595 | } |
| 596 | LOG("timed out reading tid\n"); |
| 597 | goto done; |
| 598 | } |
| 599 | LOG("read failure? %s\n", strerror(errno)); |
| 600 | goto done; |
| 601 | } |
| 602 | |
David 'Digit' Turner | 02526d4 | 2011-01-21 04:27:12 +0100 | [diff] [blame] | 603 | snprintf(buf, sizeof buf, "/proc/%d/task/%d", cr.pid, tid); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 604 | if(stat(buf, &s)) { |
Andy McFadden | 3bfdcc9 | 2009-12-01 12:37:26 -0800 | [diff] [blame] | 605 | LOG("tid %d does not exist in pid %d. ignoring debug request\n", |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 606 | tid, cr.pid); |
| 607 | close(fd); |
| 608 | return; |
| 609 | } |
Ben Cheng | 09e7137 | 2009-09-28 11:06:09 -0700 | [diff] [blame] | 610 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 611 | XLOG("BOOM: pid=%d uid=%d gid=%d tid=%d\n", cr.pid, cr.uid, cr.gid, tid); |
| 612 | |
Jeff Brown | 13e715b | 2011-10-21 12:14:56 -0700 | [diff] [blame^] | 613 | /* |
| 614 | * If the user has requested to attach gdb, don't collect the per-thread |
| 615 | * information as it increases the chance to lose track of the process. |
| 616 | */ |
| 617 | bool dump_sibling_threads = (signed)cr.pid > debug_uid; |
| 618 | |
David 'Digit' Turner | 02526d4 | 2011-01-21 04:27:12 +0100 | [diff] [blame] | 619 | /* Note that at this point, the target thread's signal handler |
| 620 | * is blocked in a read() call. This gives us the time to PTRACE_ATTACH |
| 621 | * to it before it has a chance to really fault. |
| 622 | * |
Andy McFadden | e5cc539 | 2011-10-18 20:03:07 -0700 | [diff] [blame] | 623 | * The PTRACE_ATTACH sends a SIGSTOP to the target process, but it |
| 624 | * won't necessarily have stopped by the time ptrace() returns. (We |
| 625 | * currently assume it does.) We write to the file descriptor to |
| 626 | * ensure that it can run as soon as we call PTRACE_CONT below. |
| 627 | * See details in bionic/libc/linker/debugger.c, in function |
David 'Digit' Turner | 02526d4 | 2011-01-21 04:27:12 +0100 | [diff] [blame] | 628 | * debugger_signal_handler(). |
| 629 | */ |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 630 | tid_attach_status = ptrace(PTRACE_ATTACH, tid, 0, 0); |
Andy McFadden | 655835b | 2011-07-26 07:50:37 -0700 | [diff] [blame] | 631 | int ptrace_error = errno; |
David 'Digit' Turner | 02526d4 | 2011-01-21 04:27:12 +0100 | [diff] [blame] | 632 | |
Andy McFadden | 655835b | 2011-07-26 07:50:37 -0700 | [diff] [blame] | 633 | if (TEMP_FAILURE_RETRY(write(fd, &tid, 1)) != 1) { |
| 634 | XLOG("failed responding to client: %s\n", |
| 635 | strerror(errno)); |
| 636 | goto done; |
| 637 | } |
David 'Digit' Turner | 02526d4 | 2011-01-21 04:27:12 +0100 | [diff] [blame] | 638 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 639 | if(tid_attach_status < 0) { |
Andy McFadden | 655835b | 2011-07-26 07:50:37 -0700 | [diff] [blame] | 640 | LOG("ptrace attach failed: %s\n", strerror(ptrace_error)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 641 | goto done; |
| 642 | } |
| 643 | |
| 644 | close(fd); |
| 645 | fd = -1; |
| 646 | |
Andy McFadden | 655835b | 2011-07-26 07:50:37 -0700 | [diff] [blame] | 647 | const int sleep_time_usec = 200000; /* 0.2 seconds */ |
| 648 | const int max_total_sleep_usec = 3000000; /* 3 seconds */ |
| 649 | int loop_limit = max_total_sleep_usec / sleep_time_usec; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 650 | for(;;) { |
Andy McFadden | 655835b | 2011-07-26 07:50:37 -0700 | [diff] [blame] | 651 | if (loop_limit-- == 0) { |
| 652 | LOG("timed out waiting for pid=%d tid=%d uid=%d to die\n", |
| 653 | cr.pid, tid, cr.uid); |
| 654 | goto done; |
| 655 | } |
| 656 | n = waitpid(tid, &status, __WALL | WNOHANG); |
| 657 | |
| 658 | if (n == 0) { |
| 659 | /* not ready yet */ |
| 660 | XLOG("not ready yet\n"); |
| 661 | usleep(sleep_time_usec); |
| 662 | continue; |
| 663 | } |
Ben Cheng | 09e7137 | 2009-09-28 11:06:09 -0700 | [diff] [blame] | 664 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 665 | if(n < 0) { |
| 666 | if(errno == EAGAIN) continue; |
| 667 | LOG("waitpid failed: %s\n", strerror(errno)); |
| 668 | goto done; |
| 669 | } |
| 670 | |
| 671 | XLOG("waitpid: n=%d status=%08x\n", n, status); |
| 672 | |
| 673 | if(WIFSTOPPED(status)){ |
| 674 | n = WSTOPSIG(status); |
| 675 | switch(n) { |
| 676 | case SIGSTOP: |
| 677 | XLOG("stopped -- continuing\n"); |
| 678 | n = ptrace(PTRACE_CONT, tid, 0, 0); |
| 679 | if(n) { |
| 680 | LOG("ptrace failed: %s\n", strerror(errno)); |
| 681 | goto done; |
| 682 | } |
| 683 | continue; |
Ben Cheng | 09e7137 | 2009-09-28 11:06:09 -0700 | [diff] [blame] | 684 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 685 | case SIGILL: |
| 686 | case SIGABRT: |
| 687 | case SIGBUS: |
| 688 | case SIGFPE: |
| 689 | case SIGSEGV: |
| 690 | case SIGSTKFLT: { |
| 691 | XLOG("stopped -- fatal signal\n"); |
Jeff Brown | 13e715b | 2011-10-21 12:14:56 -0700 | [diff] [blame^] | 692 | need_cleanup = engrave_tombstone(cr.pid, tid, n, |
| 693 | dump_sibling_threads); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 694 | kill(tid, SIGSTOP); |
| 695 | goto done; |
| 696 | } |
| 697 | |
| 698 | default: |
| 699 | XLOG("stopped -- unexpected signal\n"); |
| 700 | goto done; |
| 701 | } |
| 702 | } else { |
| 703 | XLOG("unexpected waitpid response\n"); |
| 704 | goto done; |
| 705 | } |
| 706 | } |
Ben Cheng | 09e7137 | 2009-09-28 11:06:09 -0700 | [diff] [blame] | 707 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 708 | done: |
| 709 | XLOG("detaching\n"); |
Ben Cheng | 09e7137 | 2009-09-28 11:06:09 -0700 | [diff] [blame] | 710 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 711 | /* stop the process so we can debug */ |
| 712 | kill(cr.pid, SIGSTOP); |
| 713 | |
Ben Cheng | 09e7137 | 2009-09-28 11:06:09 -0700 | [diff] [blame] | 714 | /* |
| 715 | * If a thread has been attached by ptrace, make sure it is detached |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 716 | * successfully otherwise we will get a zombie. |
Ben Cheng | 09e7137 | 2009-09-28 11:06:09 -0700 | [diff] [blame] | 717 | */ |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 718 | if (tid_attach_status == 0) { |
| 719 | int detach_status; |
| 720 | /* detach so we can attach gdbserver */ |
| 721 | detach_status = ptrace(PTRACE_DETACH, tid, 0, 0); |
| 722 | need_cleanup |= (detach_status != 0); |
| 723 | } |
| 724 | |
| 725 | /* |
| 726 | * if debug.db.uid is set, its value indicates if we should wait |
| 727 | * for user action for the crashing process. |
| 728 | * in this case, we log a message and turn the debug LED on |
| 729 | * waiting for a gdb connection (for instance) |
| 730 | */ |
| 731 | |
| 732 | if ((signed)cr.uid <= debug_uid) { |
| 733 | wait_for_user_action(tid, &cr); |
| 734 | } |
| 735 | |
Andy McFadden | e5cc539 | 2011-10-18 20:03:07 -0700 | [diff] [blame] | 736 | /* |
| 737 | * Resume stopped process (so it can crash in peace). If we didn't |
| 738 | * successfully detach, we're still the parent, and the actual parent |
| 739 | * won't receive a death notification via wait(2). At this point |
| 740 | * there's not much we can do about that. |
| 741 | */ |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 742 | kill(cr.pid, SIGCONT); |
| 743 | |
| 744 | if (need_cleanup) { |
| 745 | LOG("debuggerd committing suicide to free the zombie!\n"); |
| 746 | kill(getpid(), SIGKILL); |
| 747 | } |
| 748 | |
| 749 | if(fd != -1) close(fd); |
| 750 | } |
| 751 | |
Bruce Beare | 8492490 | 2010-10-13 14:21:30 -0700 | [diff] [blame] | 752 | |
Ben Cheng | 09e7137 | 2009-09-28 11:06:09 -0700 | [diff] [blame] | 753 | int main() |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 754 | { |
| 755 | int s; |
| 756 | struct sigaction act; |
Bruce Beare | 8492490 | 2010-10-13 14:21:30 -0700 | [diff] [blame] | 757 | int logsocket = -1; |
Ben Cheng | 09e7137 | 2009-09-28 11:06:09 -0700 | [diff] [blame] | 758 | |
Andy McFadden | 44e12ec | 2011-07-29 12:36:47 -0700 | [diff] [blame] | 759 | /* |
| 760 | * debuggerd crashes can't be reported to debuggerd. Reset all of the |
| 761 | * crash handlers. |
| 762 | */ |
| 763 | signal(SIGILL, SIG_DFL); |
| 764 | signal(SIGABRT, SIG_DFL); |
| 765 | signal(SIGBUS, SIG_DFL); |
| 766 | signal(SIGFPE, SIG_DFL); |
| 767 | signal(SIGSEGV, SIG_DFL); |
| 768 | signal(SIGSTKFLT, SIG_DFL); |
| 769 | signal(SIGPIPE, SIG_DFL); |
| 770 | |
Ben Cheng | 09e7137 | 2009-09-28 11:06:09 -0700 | [diff] [blame] | 771 | logsocket = socket_local_client("logd", |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 772 | ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_DGRAM); |
| 773 | if(logsocket < 0) { |
| 774 | logsocket = -1; |
| 775 | } else { |
| 776 | fcntl(logsocket, F_SETFD, FD_CLOEXEC); |
| 777 | } |
| 778 | |
| 779 | act.sa_handler = SIG_DFL; |
| 780 | sigemptyset(&act.sa_mask); |
| 781 | sigaddset(&act.sa_mask,SIGCHLD); |
| 782 | act.sa_flags = SA_NOCLDWAIT; |
| 783 | sigaction(SIGCHLD, &act, 0); |
Ben Cheng | 09e7137 | 2009-09-28 11:06:09 -0700 | [diff] [blame] | 784 | |
| 785 | s = socket_local_server("android:debuggerd", |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 786 | ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM); |
| 787 | if(s < 0) return -1; |
| 788 | fcntl(s, F_SETFD, FD_CLOEXEC); |
| 789 | |
| 790 | LOG("debuggerd: " __DATE__ " " __TIME__ "\n"); |
Ben Cheng | 09e7137 | 2009-09-28 11:06:09 -0700 | [diff] [blame] | 791 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 792 | for(;;) { |
| 793 | struct sockaddr addr; |
| 794 | socklen_t alen; |
| 795 | int fd; |
Ben Cheng | 09e7137 | 2009-09-28 11:06:09 -0700 | [diff] [blame] | 796 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 797 | alen = sizeof(addr); |
Andy McFadden | 655835b | 2011-07-26 07:50:37 -0700 | [diff] [blame] | 798 | XLOG("waiting for connection\n"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 799 | fd = accept(s, &addr, &alen); |
Andy McFadden | 655835b | 2011-07-26 07:50:37 -0700 | [diff] [blame] | 800 | if(fd < 0) { |
| 801 | XLOG("accept failed: %s\n", strerror(errno)); |
| 802 | continue; |
| 803 | } |
Ben Cheng | 09e7137 | 2009-09-28 11:06:09 -0700 | [diff] [blame] | 804 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 805 | fcntl(fd, F_SETFD, FD_CLOEXEC); |
| 806 | |
| 807 | handle_crashing_process(fd); |
| 808 | } |
| 809 | return 0; |
| 810 | } |