Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 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 <dirent.h> |
| 18 | #include <errno.h> |
| 19 | #include <fcntl.h> |
| 20 | #include <limits.h> |
| 21 | #include <poll.h> |
| 22 | #include <signal.h> |
| 23 | #include <stdarg.h> |
| 24 | #include <stdio.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <string.h> |
| 27 | #include <sys/inotify.h> |
| 28 | #include <sys/stat.h> |
Mark Salyzyn | 0751efa | 2016-02-05 15:33:17 -0800 | [diff] [blame^] | 29 | #include <sys/sysconf.h> |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 30 | #include <sys/time.h> |
| 31 | #include <sys/wait.h> |
| 32 | #include <sys/klog.h> |
| 33 | #include <time.h> |
| 34 | #include <unistd.h> |
John Michelau | e7b6cf1 | 2013-03-07 15:35:35 -0600 | [diff] [blame] | 35 | #include <sys/prctl.h> |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 36 | |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 37 | #include <cutils/debugger.h> |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 38 | #include <cutils/properties.h> |
| 39 | #include <cutils/sockets.h> |
| 40 | #include <private/android_filesystem_config.h> |
| 41 | |
Robert Craig | 9579837 | 2013-04-04 06:33:10 -0400 | [diff] [blame] | 42 | #include <selinux/android.h> |
| 43 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 44 | #include "dumpstate.h" |
| 45 | |
Jeff Brown | 1dc94e3 | 2014-09-11 14:15:27 -0700 | [diff] [blame] | 46 | static const int64_t NANOS_PER_SEC = 1000000000; |
| 47 | |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 48 | /* list of native processes to include in the native dumps */ |
| 49 | static const char* native_processes_to_dump[] = { |
James Dong | 1fc4f80 | 2012-09-10 16:08:48 -0700 | [diff] [blame] | 50 | "/system/bin/drmserver", |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 51 | "/system/bin/mediaserver", |
| 52 | "/system/bin/sdcard", |
| 53 | "/system/bin/surfaceflinger", |
| 54 | NULL, |
| 55 | }; |
| 56 | |
Christopher Ferris | 54bcc5f | 2015-02-10 12:15:01 -0800 | [diff] [blame] | 57 | static uint64_t nanotime() { |
| 58 | struct timespec ts; |
| 59 | clock_gettime(CLOCK_MONOTONIC, &ts); |
| 60 | return (uint64_t)ts.tv_sec * NANOS_PER_SEC + ts.tv_nsec; |
| 61 | } |
| 62 | |
John Spurlock | 5ecd4be | 2014-01-29 14:14:40 -0500 | [diff] [blame] | 63 | void for_each_userid(void (*func)(int), const char *header) { |
Felipe Leme | 6811616 | 2015-11-10 20:10:25 -0800 | [diff] [blame] | 64 | ON_DRY_RUN_RETURN(); |
John Spurlock | 5ecd4be | 2014-01-29 14:14:40 -0500 | [diff] [blame] | 65 | DIR *d; |
| 66 | struct dirent *de; |
| 67 | |
| 68 | if (header) printf("\n------ %s ------\n", header); |
| 69 | func(0); |
| 70 | |
| 71 | if (!(d = opendir("/data/system/users"))) { |
| 72 | printf("Failed to open /data/system/users (%s)\n", strerror(errno)); |
| 73 | return; |
| 74 | } |
| 75 | |
| 76 | while ((de = readdir(d))) { |
| 77 | int userid; |
| 78 | if (de->d_type != DT_DIR || !(userid = atoi(de->d_name))) { |
| 79 | continue; |
| 80 | } |
| 81 | func(userid); |
| 82 | } |
| 83 | |
| 84 | closedir(d); |
| 85 | } |
| 86 | |
Colin Cross | 0c22e8b | 2012-11-02 15:46:56 -0700 | [diff] [blame] | 87 | static void __for_each_pid(void (*helper)(int, const char *, void *), const char *header, void *arg) { |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 88 | DIR *d; |
| 89 | struct dirent *de; |
| 90 | |
| 91 | if (!(d = opendir("/proc"))) { |
| 92 | printf("Failed to open /proc (%s)\n", strerror(errno)); |
| 93 | return; |
| 94 | } |
| 95 | |
| 96 | printf("\n------ %s ------\n", header); |
| 97 | while ((de = readdir(d))) { |
| 98 | int pid; |
| 99 | int fd; |
| 100 | char cmdpath[255]; |
| 101 | char cmdline[255]; |
| 102 | |
| 103 | if (!(pid = atoi(de->d_name))) { |
| 104 | continue; |
| 105 | } |
| 106 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 107 | memset(cmdline, 0, sizeof(cmdline)); |
Mark Salyzyn | 0751efa | 2016-02-05 15:33:17 -0800 | [diff] [blame^] | 108 | |
| 109 | snprintf(cmdpath, sizeof(cmdpath), "/proc/%d/cmdline", pid); |
| 110 | if ((fd = TEMP_FAILURE_RETRY(open(cmdpath, O_RDONLY | O_CLOEXEC))) >= 0) { |
| 111 | TEMP_FAILURE_RETRY(read(fd, cmdline, sizeof(cmdline) - 2)); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 112 | close(fd); |
Mark Salyzyn | 0751efa | 2016-02-05 15:33:17 -0800 | [diff] [blame^] | 113 | if (cmdline[0]) { |
| 114 | helper(pid, cmdline, arg); |
| 115 | continue; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | // if no cmdline, a kernel thread has comm |
| 120 | snprintf(cmdpath, sizeof(cmdpath), "/proc/%d/comm", pid); |
| 121 | if ((fd = TEMP_FAILURE_RETRY(open(cmdpath, O_RDONLY | O_CLOEXEC))) >= 0) { |
| 122 | TEMP_FAILURE_RETRY(read(fd, cmdline + 1, sizeof(cmdline) - 4)); |
| 123 | close(fd); |
| 124 | if (cmdline[1]) { |
| 125 | cmdline[0] = '['; |
| 126 | size_t len = strcspn(cmdline, "\f\b\r\n"); |
| 127 | cmdline[len] = ']'; |
| 128 | cmdline[len+1] = '\0'; |
| 129 | } |
| 130 | } |
| 131 | if (!cmdline[0]) { |
| 132 | strcpy(cmdline, "N/A"); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 133 | } |
Colin Cross | 0c22e8b | 2012-11-02 15:46:56 -0700 | [diff] [blame] | 134 | helper(pid, cmdline, arg); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | closedir(d); |
| 138 | } |
| 139 | |
Colin Cross | 0c22e8b | 2012-11-02 15:46:56 -0700 | [diff] [blame] | 140 | static void for_each_pid_helper(int pid, const char *cmdline, void *arg) { |
Felipe Leme | 515eb0d | 2015-12-14 15:09:56 -0800 | [diff] [blame] | 141 | for_each_pid_func *func = (for_each_pid_func*) arg; |
Colin Cross | 0c22e8b | 2012-11-02 15:46:56 -0700 | [diff] [blame] | 142 | func(pid, cmdline); |
| 143 | } |
| 144 | |
| 145 | void for_each_pid(for_each_pid_func func, const char *header) { |
Felipe Leme | 6811616 | 2015-11-10 20:10:25 -0800 | [diff] [blame] | 146 | ON_DRY_RUN_RETURN(); |
Felipe Leme | 515eb0d | 2015-12-14 15:09:56 -0800 | [diff] [blame] | 147 | __for_each_pid(for_each_pid_helper, header, (void *) func); |
Colin Cross | 0c22e8b | 2012-11-02 15:46:56 -0700 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | static void for_each_tid_helper(int pid, const char *cmdline, void *arg) { |
| 151 | DIR *d; |
| 152 | struct dirent *de; |
| 153 | char taskpath[255]; |
Felipe Leme | 515eb0d | 2015-12-14 15:09:56 -0800 | [diff] [blame] | 154 | for_each_tid_func *func = (for_each_tid_func*) arg; |
Colin Cross | 0c22e8b | 2012-11-02 15:46:56 -0700 | [diff] [blame] | 155 | |
| 156 | sprintf(taskpath, "/proc/%d/task", pid); |
| 157 | |
| 158 | if (!(d = opendir(taskpath))) { |
| 159 | printf("Failed to open %s (%s)\n", taskpath, strerror(errno)); |
| 160 | return; |
| 161 | } |
| 162 | |
| 163 | func(pid, pid, cmdline); |
| 164 | |
| 165 | while ((de = readdir(d))) { |
| 166 | int tid; |
| 167 | int fd; |
| 168 | char commpath[255]; |
| 169 | char comm[255]; |
| 170 | |
| 171 | if (!(tid = atoi(de->d_name))) { |
| 172 | continue; |
| 173 | } |
| 174 | |
| 175 | if (tid == pid) |
| 176 | continue; |
| 177 | |
| 178 | sprintf(commpath,"/proc/%d/comm", tid); |
Colin Cross | 1493a39 | 2012-11-07 11:25:31 -0800 | [diff] [blame] | 179 | memset(comm, 0, sizeof(comm)); |
Nick Kralevich | cd67e9f | 2015-03-19 11:30:59 -0700 | [diff] [blame] | 180 | if ((fd = TEMP_FAILURE_RETRY(open(commpath, O_RDONLY | O_CLOEXEC))) < 0) { |
Colin Cross | 0c22e8b | 2012-11-02 15:46:56 -0700 | [diff] [blame] | 181 | strcpy(comm, "N/A"); |
| 182 | } else { |
| 183 | char *c; |
Mark Salyzyn | 0751efa | 2016-02-05 15:33:17 -0800 | [diff] [blame^] | 184 | TEMP_FAILURE_RETRY(read(fd, comm, sizeof(comm) - 2)); |
Colin Cross | 0c22e8b | 2012-11-02 15:46:56 -0700 | [diff] [blame] | 185 | close(fd); |
| 186 | |
| 187 | c = strrchr(comm, '\n'); |
| 188 | if (c) { |
| 189 | *c = '\0'; |
| 190 | } |
| 191 | } |
| 192 | func(pid, tid, comm); |
| 193 | } |
| 194 | |
| 195 | closedir(d); |
| 196 | } |
| 197 | |
| 198 | void for_each_tid(for_each_tid_func func, const char *header) { |
Felipe Leme | 6811616 | 2015-11-10 20:10:25 -0800 | [diff] [blame] | 199 | ON_DRY_RUN_RETURN(); |
Felipe Leme | 515eb0d | 2015-12-14 15:09:56 -0800 | [diff] [blame] | 200 | __for_each_pid(for_each_tid_helper, header, (void *) func); |
Colin Cross | 0c22e8b | 2012-11-02 15:46:56 -0700 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | void show_wchan(int pid, int tid, const char *name) { |
Felipe Leme | 6811616 | 2015-11-10 20:10:25 -0800 | [diff] [blame] | 204 | ON_DRY_RUN_RETURN(); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 205 | char path[255]; |
| 206 | char buffer[255]; |
Mark Salyzyn | 0751efa | 2016-02-05 15:33:17 -0800 | [diff] [blame^] | 207 | int fd, ret, save_errno; |
Colin Cross | 0c22e8b | 2012-11-02 15:46:56 -0700 | [diff] [blame] | 208 | char name_buffer[255]; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 209 | |
| 210 | memset(buffer, 0, sizeof(buffer)); |
| 211 | |
Colin Cross | 0c22e8b | 2012-11-02 15:46:56 -0700 | [diff] [blame] | 212 | sprintf(path, "/proc/%d/wchan", tid); |
Nick Kralevich | cd67e9f | 2015-03-19 11:30:59 -0700 | [diff] [blame] | 213 | if ((fd = TEMP_FAILURE_RETRY(open(path, O_RDONLY | O_CLOEXEC))) < 0) { |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 214 | printf("Failed to open '%s' (%s)\n", path, strerror(errno)); |
| 215 | return; |
| 216 | } |
| 217 | |
Mark Salyzyn | 0751efa | 2016-02-05 15:33:17 -0800 | [diff] [blame^] | 218 | ret = TEMP_FAILURE_RETRY(read(fd, buffer, sizeof(buffer))); |
| 219 | save_errno = errno; |
| 220 | close(fd); |
| 221 | |
| 222 | if (ret < 0) { |
| 223 | printf("Failed to read '%s' (%s)\n", path, strerror(save_errno)); |
| 224 | return; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 225 | } |
| 226 | |
Colin Cross | 0c22e8b | 2012-11-02 15:46:56 -0700 | [diff] [blame] | 227 | snprintf(name_buffer, sizeof(name_buffer), "%*s%s", |
| 228 | pid == tid ? 0 : 3, "", name); |
| 229 | |
| 230 | printf("%-7d %-32s %s\n", tid, name_buffer, buffer); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 231 | |
Mark Salyzyn | 0751efa | 2016-02-05 15:33:17 -0800 | [diff] [blame^] | 232 | return; |
| 233 | } |
| 234 | |
| 235 | // print time in centiseconds |
| 236 | static void snprcent(char *buffer, size_t len, size_t spc, |
| 237 | unsigned long long time) { |
| 238 | static long hz; // cache discovered hz |
| 239 | |
| 240 | if (hz <= 0) { |
| 241 | hz = sysconf(_SC_CLK_TCK); |
| 242 | if (hz <= 0) { |
| 243 | hz = 1000; |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | // convert to centiseconds |
| 248 | time = (time * 100 + (hz / 2)) / hz; |
| 249 | |
| 250 | char str[16]; |
| 251 | |
| 252 | snprintf(str, sizeof(str), " %llu.%02u", |
| 253 | time / 100, (unsigned)(time % 100)); |
| 254 | size_t offset = strlen(buffer); |
| 255 | snprintf(buffer + offset, (len > offset) ? len - offset : 0, |
| 256 | "%*s", (spc > offset) ? (int)(spc - offset) : 0, str); |
| 257 | } |
| 258 | |
| 259 | // print permille as a percent |
| 260 | static void snprdec(char *buffer, size_t len, size_t spc, unsigned permille) { |
| 261 | char str[16]; |
| 262 | |
| 263 | snprintf(str, sizeof(str), " %u.%u%%", permille / 10, permille % 10); |
| 264 | size_t offset = strlen(buffer); |
| 265 | snprintf(buffer + offset, (len > offset) ? len - offset : 0, |
| 266 | "%*s", (spc > offset) ? (int)(spc - offset) : 0, str); |
| 267 | } |
| 268 | |
| 269 | void show_showtime(int pid, const char *name) { |
| 270 | ON_DRY_RUN_RETURN(); |
| 271 | char path[255]; |
| 272 | char buffer[1023]; |
| 273 | int fd, ret, save_errno; |
| 274 | |
| 275 | memset(buffer, 0, sizeof(buffer)); |
| 276 | |
| 277 | sprintf(path, "/proc/%d/stat", pid); |
| 278 | if ((fd = TEMP_FAILURE_RETRY(open(path, O_RDONLY | O_CLOEXEC))) < 0) { |
| 279 | printf("Failed to open '%s' (%s)\n", path, strerror(errno)); |
| 280 | return; |
| 281 | } |
| 282 | |
| 283 | ret = TEMP_FAILURE_RETRY(read(fd, buffer, sizeof(buffer))); |
| 284 | save_errno = errno; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 285 | close(fd); |
Mark Salyzyn | 0751efa | 2016-02-05 15:33:17 -0800 | [diff] [blame^] | 286 | |
| 287 | if (ret < 0) { |
| 288 | printf("Failed to read '%s' (%s)\n", path, strerror(save_errno)); |
| 289 | return; |
| 290 | } |
| 291 | |
| 292 | // field 14 is utime |
| 293 | // field 15 is stime |
| 294 | // field 42 is iotime |
| 295 | unsigned long long utime = 0, stime = 0, iotime = 0; |
| 296 | if (sscanf(buffer, |
| 297 | "%*llu %*s %*s %*lld %*lld %*lld %*lld %*lld %*lld %*lld %*lld " |
| 298 | "%*lld %*lld %llu %llu %*lld %*lld %*lld %*lld %*lld %*lld " |
| 299 | "%*lld %*lld %*lld %*lld %*lld %*lld %*lld %*lld %*lld %*lld " |
| 300 | "%*lld %*lld %*lld %*lld %*lld %*lld %*lld %*lld %*lld %llu ", |
| 301 | &utime, &stime, &iotime) != 3) { |
| 302 | return; |
| 303 | } |
| 304 | |
| 305 | unsigned long long total = utime + stime; |
| 306 | if (!total) { |
| 307 | return; |
| 308 | } |
| 309 | |
| 310 | unsigned permille = (iotime * 1000 + (total / 2)) / total; |
| 311 | if (permille > 1000) { |
| 312 | permille = 1000; |
| 313 | } |
| 314 | |
| 315 | // try to beautify and stabilize columns at <80 characters |
| 316 | snprintf(buffer, sizeof(buffer), "%-6d%s", pid, name); |
| 317 | if ((name[0] != '[') || utime) { |
| 318 | snprcent(buffer, sizeof(buffer), 57, utime); |
| 319 | } |
| 320 | snprcent(buffer, sizeof(buffer), 65, stime); |
| 321 | if ((name[0] != '[') || iotime) { |
| 322 | snprcent(buffer, sizeof(buffer), 73, iotime); |
| 323 | } |
| 324 | if (iotime) { |
| 325 | snprdec(buffer, sizeof(buffer), 79, permille); |
| 326 | } |
| 327 | puts(buffer); // adds a trailing newline |
| 328 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 329 | return; |
| 330 | } |
| 331 | |
| 332 | void do_dmesg() { |
| 333 | printf("------ KERNEL LOG (dmesg) ------\n"); |
Felipe Leme | 6811616 | 2015-11-10 20:10:25 -0800 | [diff] [blame] | 334 | ON_DRY_RUN_RETURN(); |
Elliott Hughes | 5f87b31 | 2012-09-17 11:43:40 -0700 | [diff] [blame] | 335 | /* Get size of kernel buffer */ |
| 336 | int size = klogctl(KLOG_SIZE_BUFFER, NULL, 0); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 337 | if (size <= 0) { |
| 338 | printf("Unexpected klogctl return value: %d\n\n", size); |
| 339 | return; |
| 340 | } |
| 341 | char *buf = (char *) malloc(size + 1); |
| 342 | if (buf == NULL) { |
| 343 | printf("memory allocation failed\n\n"); |
| 344 | return; |
| 345 | } |
| 346 | int retval = klogctl(KLOG_READ_ALL, buf, size); |
| 347 | if (retval < 0) { |
| 348 | printf("klogctl failure\n\n"); |
| 349 | free(buf); |
| 350 | return; |
| 351 | } |
| 352 | buf[retval] = '\0'; |
| 353 | printf("%s\n\n", buf); |
| 354 | free(buf); |
| 355 | return; |
| 356 | } |
| 357 | |
| 358 | void do_showmap(int pid, const char *name) { |
| 359 | char title[255]; |
| 360 | char arg[255]; |
| 361 | |
| 362 | sprintf(title, "SHOW MAP %d (%s)", pid, name); |
| 363 | sprintf(arg, "%d", pid); |
| 364 | run_command(title, 10, SU_PATH, "root", "showmap", arg, NULL); |
| 365 | } |
| 366 | |
Christopher Ferris | 54bcc5f | 2015-02-10 12:15:01 -0800 | [diff] [blame] | 367 | static int _dump_file_from_fd(const char *title, const char *path, int fd) { |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 368 | if (title) printf("------ %s (%s", title, path); |
| 369 | |
| 370 | if (title) { |
| 371 | struct stat st; |
| 372 | if (memcmp(path, "/proc/", 6) && memcmp(path, "/sys/", 5) && !fstat(fd, &st)) { |
| 373 | char stamp[80]; |
| 374 | time_t mtime = st.st_mtime; |
| 375 | strftime(stamp, sizeof(stamp), "%Y-%m-%d %H:%M:%S", localtime(&mtime)); |
| 376 | printf(": %s", stamp); |
| 377 | } |
| 378 | printf(") ------\n"); |
| 379 | } |
| 380 | |
Christopher Ferris | 54bcc5f | 2015-02-10 12:15:01 -0800 | [diff] [blame] | 381 | bool newline = false; |
| 382 | fd_set read_set; |
| 383 | struct timeval tm; |
| 384 | while (1) { |
| 385 | FD_ZERO(&read_set); |
| 386 | FD_SET(fd, &read_set); |
| 387 | /* Timeout if no data is read for 30 seconds. */ |
| 388 | tm.tv_sec = 30; |
| 389 | tm.tv_usec = 0; |
| 390 | uint64_t elapsed = nanotime(); |
| 391 | int ret = TEMP_FAILURE_RETRY(select(fd + 1, &read_set, NULL, NULL, &tm)); |
| 392 | if (ret == -1) { |
| 393 | printf("*** %s: select failed: %s\n", path, strerror(errno)); |
| 394 | newline = true; |
| 395 | break; |
| 396 | } else if (ret == 0) { |
| 397 | elapsed = nanotime() - elapsed; |
| 398 | printf("*** %s: Timed out after %.3fs\n", path, |
| 399 | (float) elapsed / NANOS_PER_SEC); |
| 400 | newline = true; |
| 401 | break; |
| 402 | } else { |
| 403 | char buffer[65536]; |
| 404 | ssize_t bytes_read = TEMP_FAILURE_RETRY(read(fd, buffer, sizeof(buffer))); |
| 405 | if (bytes_read > 0) { |
| 406 | fwrite(buffer, bytes_read, 1, stdout); |
| 407 | newline = (buffer[bytes_read-1] == '\n'); |
| 408 | } else { |
| 409 | if (bytes_read == -1) { |
| 410 | printf("*** %s: Failed to read from fd: %s", path, strerror(errno)); |
| 411 | newline = true; |
| 412 | } |
| 413 | break; |
| 414 | } |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 415 | } |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 416 | } |
Elliott Hughes | 997abb6 | 2015-05-15 17:05:40 -0700 | [diff] [blame] | 417 | close(fd); |
Christopher Ferris | 7dc7f32 | 2014-07-22 16:08:19 -0700 | [diff] [blame] | 418 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 419 | if (!newline) printf("\n"); |
| 420 | if (title) printf("\n"); |
| 421 | return 0; |
| 422 | } |
| 423 | |
Christopher Ferris | 54bcc5f | 2015-02-10 12:15:01 -0800 | [diff] [blame] | 424 | /* prints the contents of a file */ |
| 425 | int dump_file(const char *title, const char *path) { |
Felipe Leme | 6811616 | 2015-11-10 20:10:25 -0800 | [diff] [blame] | 426 | if (title) printf("------ %s (%s) ------\n", title, path); |
| 427 | ON_DRY_RUN_RETURN(0); |
| 428 | |
Christopher Ferris | 54bcc5f | 2015-02-10 12:15:01 -0800 | [diff] [blame] | 429 | int fd = TEMP_FAILURE_RETRY(open(path, O_RDONLY | O_NONBLOCK | O_CLOEXEC)); |
| 430 | if (fd < 0) { |
| 431 | int err = errno; |
Christopher Ferris | 54bcc5f | 2015-02-10 12:15:01 -0800 | [diff] [blame] | 432 | printf("*** %s: %s\n", path, strerror(err)); |
| 433 | if (title) printf("\n"); |
| 434 | return -1; |
| 435 | } |
| 436 | return _dump_file_from_fd(title, path, fd); |
| 437 | } |
| 438 | |
Mark Salyzyn | 326842f | 2015-04-30 09:49:41 -0700 | [diff] [blame] | 439 | /* calls skip to gate calling dump_from_fd recursively |
| 440 | * in the specified directory. dump_from_fd defaults to |
| 441 | * dump_file_from_fd above when set to NULL. skip defaults |
| 442 | * to false when set to NULL. dump_from_fd will always be |
| 443 | * called with title NULL. |
| 444 | */ |
| 445 | int dump_files(const char *title, const char *dir, |
| 446 | bool (*skip)(const char *path), |
| 447 | int (*dump_from_fd)(const char *title, const char *path, int fd)) { |
| 448 | DIR *dirp; |
| 449 | struct dirent *d; |
| 450 | char *newpath = NULL; |
Felipe Leme | 515eb0d | 2015-12-14 15:09:56 -0800 | [diff] [blame] | 451 | const char *slash = "/"; |
Mark Salyzyn | 326842f | 2015-04-30 09:49:41 -0700 | [diff] [blame] | 452 | int fd, retval = 0; |
| 453 | |
| 454 | if (title) { |
| 455 | printf("------ %s (%s) ------\n", title, dir); |
| 456 | } |
Felipe Leme | 6811616 | 2015-11-10 20:10:25 -0800 | [diff] [blame] | 457 | ON_DRY_RUN_RETURN(0); |
Mark Salyzyn | 326842f | 2015-04-30 09:49:41 -0700 | [diff] [blame] | 458 | |
| 459 | if (dir[strlen(dir) - 1] == '/') { |
| 460 | ++slash; |
| 461 | } |
| 462 | dirp = opendir(dir); |
| 463 | if (dirp == NULL) { |
| 464 | retval = -errno; |
| 465 | fprintf(stderr, "%s: %s\n", dir, strerror(errno)); |
| 466 | return retval; |
| 467 | } |
| 468 | |
| 469 | if (!dump_from_fd) { |
| 470 | dump_from_fd = dump_file_from_fd; |
| 471 | } |
| 472 | for (; ((d = readdir(dirp))); free(newpath), newpath = NULL) { |
| 473 | if ((d->d_name[0] == '.') |
| 474 | && (((d->d_name[1] == '.') && (d->d_name[2] == '\0')) |
| 475 | || (d->d_name[1] == '\0'))) { |
| 476 | continue; |
| 477 | } |
| 478 | asprintf(&newpath, "%s%s%s%s", dir, slash, d->d_name, |
| 479 | (d->d_type == DT_DIR) ? "/" : ""); |
| 480 | if (!newpath) { |
| 481 | retval = -errno; |
| 482 | continue; |
| 483 | } |
| 484 | if (skip && (*skip)(newpath)) { |
| 485 | continue; |
| 486 | } |
| 487 | if (d->d_type == DT_DIR) { |
| 488 | int ret = dump_files(NULL, newpath, skip, dump_from_fd); |
| 489 | if (ret < 0) { |
| 490 | retval = ret; |
| 491 | } |
| 492 | continue; |
| 493 | } |
| 494 | fd = TEMP_FAILURE_RETRY(open(newpath, O_RDONLY | O_NONBLOCK | O_CLOEXEC)); |
| 495 | if (fd < 0) { |
| 496 | retval = fd; |
| 497 | printf("*** %s: %s\n", newpath, strerror(errno)); |
| 498 | continue; |
| 499 | } |
| 500 | (*dump_from_fd)(NULL, newpath, fd); |
| 501 | } |
| 502 | closedir(dirp); |
| 503 | if (title) { |
| 504 | printf("\n"); |
| 505 | } |
| 506 | return retval; |
| 507 | } |
| 508 | |
Christopher Ferris | 54bcc5f | 2015-02-10 12:15:01 -0800 | [diff] [blame] | 509 | /* fd must have been opened with the flag O_NONBLOCK. With this flag set, |
| 510 | * it's possible to avoid issues where opening the file itself can get |
| 511 | * stuck. |
| 512 | */ |
| 513 | int dump_file_from_fd(const char *title, const char *path, int fd) { |
Felipe Leme | 6811616 | 2015-11-10 20:10:25 -0800 | [diff] [blame] | 514 | ON_DRY_RUN_RETURN(0); |
Christopher Ferris | 54bcc5f | 2015-02-10 12:15:01 -0800 | [diff] [blame] | 515 | int flags = fcntl(fd, F_GETFL); |
| 516 | if (flags == -1) { |
| 517 | printf("*** %s: failed to get flags on fd %d: %s\n", path, fd, strerror(errno)); |
| 518 | return -1; |
| 519 | } else if (!(flags & O_NONBLOCK)) { |
| 520 | printf("*** %s: fd must have O_NONBLOCK set.\n", path); |
| 521 | return -1; |
| 522 | } |
| 523 | return _dump_file_from_fd(title, path, fd); |
Jeff Brown | 1dc94e3 | 2014-09-11 14:15:27 -0700 | [diff] [blame] | 524 | } |
| 525 | |
Christopher Ferris | 1a9a338 | 2015-01-30 11:00:52 -0800 | [diff] [blame] | 526 | bool waitpid_with_timeout(pid_t pid, int timeout_seconds, int* status) { |
| 527 | sigset_t child_mask, old_mask; |
| 528 | sigemptyset(&child_mask); |
| 529 | sigaddset(&child_mask, SIGCHLD); |
| 530 | |
| 531 | if (sigprocmask(SIG_BLOCK, &child_mask, &old_mask) == -1) { |
| 532 | printf("*** sigprocmask failed: %s\n", strerror(errno)); |
| 533 | return false; |
| 534 | } |
| 535 | |
| 536 | struct timespec ts; |
| 537 | ts.tv_sec = timeout_seconds; |
| 538 | ts.tv_nsec = 0; |
| 539 | int ret = TEMP_FAILURE_RETRY(sigtimedwait(&child_mask, NULL, &ts)); |
| 540 | int saved_errno = errno; |
| 541 | // Set the signals back the way they were. |
| 542 | if (sigprocmask(SIG_SETMASK, &old_mask, NULL) == -1) { |
| 543 | printf("*** sigprocmask failed: %s\n", strerror(errno)); |
| 544 | if (ret == 0) { |
| 545 | return false; |
| 546 | } |
| 547 | } |
| 548 | if (ret == -1) { |
| 549 | errno = saved_errno; |
| 550 | if (errno == EAGAIN) { |
| 551 | errno = ETIMEDOUT; |
| 552 | } else { |
| 553 | printf("*** sigtimedwait failed: %s\n", strerror(errno)); |
| 554 | } |
| 555 | return false; |
| 556 | } |
| 557 | |
| 558 | pid_t child_pid = waitpid(pid, status, WNOHANG); |
| 559 | if (child_pid != pid) { |
| 560 | if (child_pid != -1) { |
| 561 | printf("*** Waiting for pid %d, got pid %d instead\n", pid, child_pid); |
| 562 | } else { |
| 563 | printf("*** waitpid failed: %s\n", strerror(errno)); |
| 564 | } |
| 565 | return false; |
| 566 | } |
| 567 | return true; |
| 568 | } |
| 569 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 570 | /* forks a command and waits for it to finish */ |
| 571 | int run_command(const char *title, int timeout_seconds, const char *command, ...) { |
| 572 | fflush(stdout); |
Felipe Leme | 6811616 | 2015-11-10 20:10:25 -0800 | [diff] [blame] | 573 | |
| 574 | const char *args[1024] = {command}; |
| 575 | size_t arg; |
| 576 | va_list ap; |
| 577 | va_start(ap, command); |
| 578 | if (title) printf("------ %s (%s", title, command); |
| 579 | for (arg = 1; arg < sizeof(args) / sizeof(args[0]); ++arg) { |
| 580 | args[arg] = va_arg(ap, const char *); |
| 581 | if (args[arg] == NULL) break; |
| 582 | if (title) printf(" %s", args[arg]); |
| 583 | } |
| 584 | if (title) printf(") ------\n"); |
| 585 | fflush(stdout); |
| 586 | |
| 587 | ON_DRY_RUN_RETURN(0); |
| 588 | |
| 589 | return run_command_always(title, timeout_seconds, args); |
| 590 | } |
| 591 | |
| 592 | /* forks a command and waits for it to finish */ |
| 593 | int run_command_always(const char *title, int timeout_seconds, const char *args[]) { |
| 594 | const char *command = args[0]; |
| 595 | |
Christopher Ferris | 54bcc5f | 2015-02-10 12:15:01 -0800 | [diff] [blame] | 596 | uint64_t start = nanotime(); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 597 | pid_t pid = fork(); |
| 598 | |
| 599 | /* handle error case */ |
| 600 | if (pid < 0) { |
| 601 | printf("*** fork: %s\n", strerror(errno)); |
| 602 | return pid; |
| 603 | } |
| 604 | |
| 605 | /* handle child case */ |
| 606 | if (pid == 0) { |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 607 | |
John Michelau | e7b6cf1 | 2013-03-07 15:35:35 -0600 | [diff] [blame] | 608 | /* make sure the child dies when dumpstate dies */ |
| 609 | prctl(PR_SET_PDEATHSIG, SIGKILL); |
| 610 | |
Andres Morales | 2e671bb | 2014-08-21 12:38:22 -0700 | [diff] [blame] | 611 | /* just ignore SIGPIPE, will go down with parent's */ |
| 612 | struct sigaction sigact; |
| 613 | memset(&sigact, 0, sizeof(sigact)); |
| 614 | sigact.sa_handler = SIG_IGN; |
| 615 | sigaction(SIGPIPE, &sigact, NULL); |
| 616 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 617 | execvp(command, (char**) args); |
| 618 | printf("*** exec(%s): %s\n", command, strerror(errno)); |
| 619 | fflush(stdout); |
| 620 | _exit(-1); |
| 621 | } |
| 622 | |
| 623 | /* handle parent case */ |
Christopher Ferris | 1a9a338 | 2015-01-30 11:00:52 -0800 | [diff] [blame] | 624 | int status; |
| 625 | bool ret = waitpid_with_timeout(pid, timeout_seconds, &status); |
| 626 | uint64_t elapsed = nanotime() - start; |
| 627 | if (!ret) { |
| 628 | if (errno == ETIMEDOUT) { |
| 629 | printf("*** %s: Timed out after %.3fs (killing pid %d)\n", command, |
| 630 | (float) elapsed / NANOS_PER_SEC, pid); |
| 631 | } else { |
| 632 | printf("*** %s: Error after %.4fs (killing pid %d)\n", command, |
| 633 | (float) elapsed / NANOS_PER_SEC, pid); |
| 634 | } |
| 635 | kill(pid, SIGTERM); |
| 636 | if (!waitpid_with_timeout(pid, 5, NULL)) { |
| 637 | kill(pid, SIGKILL); |
| 638 | if (!waitpid_with_timeout(pid, 5, NULL)) { |
| 639 | printf("*** %s: Cannot kill %d even with SIGKILL.\n", command, pid); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 640 | } |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 641 | } |
Christopher Ferris | 1a9a338 | 2015-01-30 11:00:52 -0800 | [diff] [blame] | 642 | return -1; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 643 | } |
Christopher Ferris | 1a9a338 | 2015-01-30 11:00:52 -0800 | [diff] [blame] | 644 | |
| 645 | if (WIFSIGNALED(status)) { |
| 646 | printf("*** %s: Killed by signal %d\n", command, WTERMSIG(status)); |
| 647 | } else if (WIFEXITED(status) && WEXITSTATUS(status) > 0) { |
| 648 | printf("*** %s: Exit code %d\n", command, WEXITSTATUS(status)); |
| 649 | } |
| 650 | if (title) printf("[%s: %.3fs elapsed]\n\n", command, (float)elapsed / NANOS_PER_SEC); |
| 651 | |
| 652 | return status; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 653 | } |
| 654 | |
| 655 | size_t num_props = 0; |
| 656 | static char* props[2000]; |
| 657 | |
| 658 | static void print_prop(const char *key, const char *name, void *user) { |
| 659 | (void) user; |
| 660 | if (num_props < sizeof(props) / sizeof(props[0])) { |
| 661 | char buf[PROPERTY_KEY_MAX + PROPERTY_VALUE_MAX + 10]; |
| 662 | snprintf(buf, sizeof(buf), "[%s]: [%s]\n", key, name); |
| 663 | props[num_props++] = strdup(buf); |
| 664 | } |
| 665 | } |
| 666 | |
| 667 | static int compare_prop(const void *a, const void *b) { |
| 668 | return strcmp(*(char * const *) a, *(char * const *) b); |
| 669 | } |
| 670 | |
| 671 | /* prints all the system properties */ |
| 672 | void print_properties() { |
Felipe Leme | 6811616 | 2015-11-10 20:10:25 -0800 | [diff] [blame] | 673 | printf("------ SYSTEM PROPERTIES ------\n"); |
| 674 | ON_DRY_RUN_RETURN(); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 675 | size_t i; |
| 676 | num_props = 0; |
| 677 | property_list(print_prop, NULL); |
| 678 | qsort(&props, num_props, sizeof(props[0]), compare_prop); |
| 679 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 680 | for (i = 0; i < num_props; ++i) { |
| 681 | fputs(props[i], stdout); |
| 682 | free(props[i]); |
| 683 | } |
| 684 | printf("\n"); |
| 685 | } |
| 686 | |
| 687 | /* redirect output to a service control socket */ |
| 688 | void redirect_to_socket(FILE *redirect, const char *service) { |
| 689 | int s = android_get_control_socket(service); |
| 690 | if (s < 0) { |
| 691 | fprintf(stderr, "android_get_control_socket(%s): %s\n", service, strerror(errno)); |
| 692 | exit(1); |
| 693 | } |
Nick Kralevich | cd67e9f | 2015-03-19 11:30:59 -0700 | [diff] [blame] | 694 | fcntl(s, F_SETFD, FD_CLOEXEC); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 695 | if (listen(s, 4) < 0) { |
| 696 | fprintf(stderr, "listen(control socket): %s\n", strerror(errno)); |
| 697 | exit(1); |
| 698 | } |
| 699 | |
| 700 | struct sockaddr addr; |
| 701 | socklen_t alen = sizeof(addr); |
| 702 | int fd = accept(s, &addr, &alen); |
| 703 | if (fd < 0) { |
| 704 | fprintf(stderr, "accept(control socket): %s\n", strerror(errno)); |
| 705 | exit(1); |
| 706 | } |
| 707 | |
| 708 | fflush(redirect); |
| 709 | dup2(fd, fileno(redirect)); |
| 710 | close(fd); |
| 711 | } |
| 712 | |
Christopher Ferris | ff4a4dc | 2015-02-09 16:24:47 -0800 | [diff] [blame] | 713 | /* redirect output to a file */ |
| 714 | void redirect_to_file(FILE *redirect, char *path) { |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 715 | char *chp = path; |
| 716 | |
| 717 | /* skip initial slash */ |
| 718 | if (chp[0] == '/') |
| 719 | chp++; |
| 720 | |
| 721 | /* create leading directories, if necessary */ |
| 722 | while (chp && chp[0]) { |
| 723 | chp = strchr(chp, '/'); |
| 724 | if (chp) { |
| 725 | *chp = 0; |
Jeff Sharkey | 27f9e6d | 2013-03-13 15:45:50 -0700 | [diff] [blame] | 726 | mkdir(path, 0770); /* drwxrwx--- */ |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 727 | *chp++ = '/'; |
| 728 | } |
| 729 | } |
| 730 | |
Nick Kralevich | cd67e9f | 2015-03-19 11:30:59 -0700 | [diff] [blame] | 731 | int fd = TEMP_FAILURE_RETRY(open(path, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, |
Christopher Ferris | ff4a4dc | 2015-02-09 16:24:47 -0800 | [diff] [blame] | 732 | S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 733 | if (fd < 0) { |
| 734 | fprintf(stderr, "%s: %s\n", path, strerror(errno)); |
| 735 | exit(1); |
| 736 | } |
| 737 | |
Christopher Ferris | ff4a4dc | 2015-02-09 16:24:47 -0800 | [diff] [blame] | 738 | TEMP_FAILURE_RETRY(dup2(fd, fileno(redirect))); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 739 | close(fd); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 740 | } |
| 741 | |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 742 | static bool should_dump_native_traces(const char* path) { |
| 743 | for (const char** p = native_processes_to_dump; *p; p++) { |
| 744 | if (!strcmp(*p, path)) { |
| 745 | return true; |
| 746 | } |
| 747 | } |
| 748 | return false; |
| 749 | } |
| 750 | |
| 751 | /* dump Dalvik and native stack traces, return the trace file location (NULL if none) */ |
| 752 | const char *dump_traces() { |
Felipe Leme | 6811616 | 2015-11-10 20:10:25 -0800 | [diff] [blame] | 753 | ON_DRY_RUN_RETURN(NULL); |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 754 | const char* result = NULL; |
| 755 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 756 | char traces_path[PROPERTY_VALUE_MAX] = ""; |
| 757 | property_get("dalvik.vm.stack-trace-file", traces_path, ""); |
| 758 | if (!traces_path[0]) return NULL; |
| 759 | |
| 760 | /* move the old traces.txt (if any) out of the way temporarily */ |
| 761 | char anr_traces_path[PATH_MAX]; |
| 762 | strlcpy(anr_traces_path, traces_path, sizeof(anr_traces_path)); |
| 763 | strlcat(anr_traces_path, ".anr", sizeof(anr_traces_path)); |
| 764 | if (rename(traces_path, anr_traces_path) && errno != ENOENT) { |
| 765 | fprintf(stderr, "rename(%s, %s): %s\n", traces_path, anr_traces_path, strerror(errno)); |
| 766 | return NULL; // Can't rename old traces.txt -- no permission? -- leave it alone instead |
| 767 | } |
| 768 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 769 | /* create a new, empty traces.txt file to receive stack dumps */ |
Nick Kralevich | cd67e9f | 2015-03-19 11:30:59 -0700 | [diff] [blame] | 770 | int fd = TEMP_FAILURE_RETRY(open(traces_path, O_CREAT | O_WRONLY | O_TRUNC | O_NOFOLLOW | O_CLOEXEC, |
Christopher Ferris | 54bcc5f | 2015-02-10 12:15:01 -0800 | [diff] [blame] | 771 | 0666)); /* -rw-rw-rw- */ |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 772 | if (fd < 0) { |
| 773 | fprintf(stderr, "%s: %s\n", traces_path, strerror(errno)); |
| 774 | return NULL; |
| 775 | } |
Nick Kralevich | c7f1fe2 | 2012-04-06 09:31:28 -0700 | [diff] [blame] | 776 | int chmod_ret = fchmod(fd, 0666); |
| 777 | if (chmod_ret < 0) { |
| 778 | fprintf(stderr, "fchmod on %s failed: %s\n", traces_path, strerror(errno)); |
| 779 | close(fd); |
| 780 | return NULL; |
| 781 | } |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 782 | |
Felipe Leme | 515eb0d | 2015-12-14 15:09:56 -0800 | [diff] [blame] | 783 | /* Variables below must be initialized before 'goto' statements */ |
| 784 | int dalvik_found = 0; |
| 785 | int ifd, wfd = -1; |
| 786 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 787 | /* walk /proc and kill -QUIT all Dalvik processes */ |
| 788 | DIR *proc = opendir("/proc"); |
| 789 | if (proc == NULL) { |
| 790 | fprintf(stderr, "/proc: %s\n", strerror(errno)); |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 791 | goto error_close_fd; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 792 | } |
| 793 | |
| 794 | /* use inotify to find when processes are done dumping */ |
Felipe Leme | 515eb0d | 2015-12-14 15:09:56 -0800 | [diff] [blame] | 795 | ifd = inotify_init(); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 796 | if (ifd < 0) { |
| 797 | fprintf(stderr, "inotify_init: %s\n", strerror(errno)); |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 798 | goto error_close_fd; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 799 | } |
| 800 | |
Felipe Leme | 515eb0d | 2015-12-14 15:09:56 -0800 | [diff] [blame] | 801 | wfd = inotify_add_watch(ifd, traces_path, IN_CLOSE_WRITE); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 802 | if (wfd < 0) { |
| 803 | fprintf(stderr, "inotify_add_watch(%s): %s\n", traces_path, strerror(errno)); |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 804 | goto error_close_ifd; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 805 | } |
| 806 | |
| 807 | struct dirent *d; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 808 | while ((d = readdir(proc))) { |
| 809 | int pid = atoi(d->d_name); |
| 810 | if (pid <= 0) continue; |
| 811 | |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 812 | char path[PATH_MAX]; |
| 813 | char data[PATH_MAX]; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 814 | snprintf(path, sizeof(path), "/proc/%d/exe", pid); |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 815 | ssize_t len = readlink(path, data, sizeof(data) - 1); |
| 816 | if (len <= 0) { |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 817 | continue; |
| 818 | } |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 819 | data[len] = '\0'; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 820 | |
Colin Cross | 0d6180f | 2014-07-16 19:00:46 -0700 | [diff] [blame] | 821 | if (!strncmp(data, "/system/bin/app_process", strlen("/system/bin/app_process"))) { |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 822 | /* skip zygote -- it won't dump its stack anyway */ |
| 823 | snprintf(path, sizeof(path), "/proc/%d/cmdline", pid); |
Nick Kralevich | cd67e9f | 2015-03-19 11:30:59 -0700 | [diff] [blame] | 824 | int cfd = TEMP_FAILURE_RETRY(open(path, O_RDONLY | O_CLOEXEC)); |
Jeff Brown | 1dc94e3 | 2014-09-11 14:15:27 -0700 | [diff] [blame] | 825 | len = read(cfd, data, sizeof(data) - 1); |
| 826 | close(cfd); |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 827 | if (len <= 0) { |
| 828 | continue; |
| 829 | } |
| 830 | data[len] = '\0'; |
Colin Cross | 0d6180f | 2014-07-16 19:00:46 -0700 | [diff] [blame] | 831 | if (!strncmp(data, "zygote", strlen("zygote"))) { |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 832 | continue; |
| 833 | } |
| 834 | |
| 835 | ++dalvik_found; |
Christopher Ferris | 54bcc5f | 2015-02-10 12:15:01 -0800 | [diff] [blame] | 836 | uint64_t start = nanotime(); |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 837 | if (kill(pid, SIGQUIT)) { |
| 838 | fprintf(stderr, "kill(%d, SIGQUIT): %s\n", pid, strerror(errno)); |
| 839 | continue; |
| 840 | } |
| 841 | |
| 842 | /* wait for the writable-close notification from inotify */ |
| 843 | struct pollfd pfd = { ifd, POLLIN, 0 }; |
Nick Vaccaro | 85453ec | 2014-04-30 11:19:23 -0700 | [diff] [blame] | 844 | int ret = poll(&pfd, 1, 5000); /* 5 sec timeout */ |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 845 | if (ret < 0) { |
| 846 | fprintf(stderr, "poll: %s\n", strerror(errno)); |
| 847 | } else if (ret == 0) { |
| 848 | fprintf(stderr, "warning: timed out dumping pid %d\n", pid); |
| 849 | } else { |
| 850 | struct inotify_event ie; |
| 851 | read(ifd, &ie, sizeof(ie)); |
| 852 | } |
Jeff Brown | 1dc94e3 | 2014-09-11 14:15:27 -0700 | [diff] [blame] | 853 | |
| 854 | if (lseek(fd, 0, SEEK_END) < 0) { |
| 855 | fprintf(stderr, "lseek: %s\n", strerror(errno)); |
| 856 | } else { |
Christopher Ferris | 31ef855 | 2015-01-14 13:23:30 -0800 | [diff] [blame] | 857 | dprintf(fd, "[dump dalvik stack %d: %.3fs elapsed]\n", |
Jeff Brown | 1dc94e3 | 2014-09-11 14:15:27 -0700 | [diff] [blame] | 858 | pid, (float)(nanotime() - start) / NANOS_PER_SEC); |
Jeff Brown | 1dc94e3 | 2014-09-11 14:15:27 -0700 | [diff] [blame] | 859 | } |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 860 | } else if (should_dump_native_traces(data)) { |
| 861 | /* dump native process if appropriate */ |
| 862 | if (lseek(fd, 0, SEEK_END) < 0) { |
| 863 | fprintf(stderr, "lseek: %s\n", strerror(errno)); |
| 864 | } else { |
Christopher Ferris | 31ef855 | 2015-01-14 13:23:30 -0800 | [diff] [blame] | 865 | static uint16_t timeout_failures = 0; |
Christopher Ferris | 54bcc5f | 2015-02-10 12:15:01 -0800 | [diff] [blame] | 866 | uint64_t start = nanotime(); |
Christopher Ferris | 31ef855 | 2015-01-14 13:23:30 -0800 | [diff] [blame] | 867 | |
| 868 | /* If 3 backtrace dumps fail in a row, consider debuggerd dead. */ |
| 869 | if (timeout_failures == 3) { |
| 870 | dprintf(fd, "too many stack dump failures, skipping...\n"); |
| 871 | } else if (dump_backtrace_to_file_timeout(pid, fd, 20) == -1) { |
| 872 | dprintf(fd, "dumping failed, likely due to a timeout\n"); |
| 873 | timeout_failures++; |
| 874 | } else { |
| 875 | timeout_failures = 0; |
| 876 | } |
| 877 | dprintf(fd, "[dump native stack %d: %.3fs elapsed]\n", |
Jeff Brown | 1dc94e3 | 2014-09-11 14:15:27 -0700 | [diff] [blame] | 878 | pid, (float)(nanotime() - start) / NANOS_PER_SEC); |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 879 | } |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 880 | } |
| 881 | } |
| 882 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 883 | if (dalvik_found == 0) { |
| 884 | fprintf(stderr, "Warning: no Dalvik processes found to dump stacks\n"); |
| 885 | } |
| 886 | |
| 887 | static char dump_traces_path[PATH_MAX]; |
| 888 | strlcpy(dump_traces_path, traces_path, sizeof(dump_traces_path)); |
| 889 | strlcat(dump_traces_path, ".bugreport", sizeof(dump_traces_path)); |
| 890 | if (rename(traces_path, dump_traces_path)) { |
| 891 | fprintf(stderr, "rename(%s, %s): %s\n", traces_path, dump_traces_path, strerror(errno)); |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 892 | goto error_close_ifd; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 893 | } |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 894 | result = dump_traces_path; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 895 | |
| 896 | /* replace the saved [ANR] traces.txt file */ |
| 897 | rename(anr_traces_path, traces_path); |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 898 | |
| 899 | error_close_ifd: |
| 900 | close(ifd); |
| 901 | error_close_fd: |
| 902 | close(fd); |
| 903 | return result; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 904 | } |
| 905 | |
Sreeram Ramachandran | 2b3bba3 | 2014-07-08 15:40:55 -0700 | [diff] [blame] | 906 | void dump_route_tables() { |
Felipe Leme | 6811616 | 2015-11-10 20:10:25 -0800 | [diff] [blame] | 907 | ON_DRY_RUN_RETURN(); |
Sreeram Ramachandran | 2b3bba3 | 2014-07-08 15:40:55 -0700 | [diff] [blame] | 908 | const char* const RT_TABLES_PATH = "/data/misc/net/rt_tables"; |
| 909 | dump_file("RT_TABLES", RT_TABLES_PATH); |
Nick Kralevich | cd67e9f | 2015-03-19 11:30:59 -0700 | [diff] [blame] | 910 | FILE* fp = fopen(RT_TABLES_PATH, "re"); |
Sreeram Ramachandran | 2b3bba3 | 2014-07-08 15:40:55 -0700 | [diff] [blame] | 911 | if (!fp) { |
| 912 | printf("*** %s: %s\n", RT_TABLES_PATH, strerror(errno)); |
| 913 | return; |
| 914 | } |
| 915 | char table[16]; |
| 916 | // Each line has an integer (the table number), a space, and a string (the table name). We only |
| 917 | // need the table number. It's a 32-bit unsigned number, so max 10 chars. Skip the table name. |
| 918 | // Add a fixed max limit so this doesn't go awry. |
| 919 | for (int i = 0; i < 64 && fscanf(fp, " %10s %*s", table) == 1; ++i) { |
| 920 | run_command("ROUTE TABLE IPv4", 10, "ip", "-4", "route", "show", "table", table, NULL); |
| 921 | run_command("ROUTE TABLE IPv6", 10, "ip", "-6", "route", "show", "table", table, NULL); |
| 922 | } |
| 923 | fclose(fp); |
| 924 | } |
Mark Salyzyn | 8c8130e | 2015-12-09 11:21:28 -0800 | [diff] [blame] | 925 | |
| 926 | void dump_emmc_ecsd(const char *ext_csd_path) { |
| 927 | static const size_t EXT_CSD_REV = 192; |
| 928 | static const size_t EXT_PRE_EOL_INFO = 267; |
| 929 | static const size_t EXT_DEVICE_LIFE_TIME_EST_TYP_A = 268; |
| 930 | static const size_t EXT_DEVICE_LIFE_TIME_EST_TYP_B = 269; |
| 931 | struct hex { |
| 932 | char str[2]; |
| 933 | } buffer[512]; |
| 934 | int fd, ext_csd_rev, ext_pre_eol_info; |
| 935 | ssize_t bytes_read; |
| 936 | static const char *ver_str[] = { |
| 937 | "4.0", "4.1", "4.2", "4.3", "Obsolete", "4.41", "4.5", "5.0" |
| 938 | }; |
| 939 | static const char *eol_str[] = { |
| 940 | "Undefined", |
| 941 | "Normal", |
| 942 | "Warning (consumed 80% of reserve)", |
Mark Salyzyn | 4b45d67 | 2015-12-11 10:41:52 -0800 | [diff] [blame] | 943 | "Urgent (consumed 90% of reserve)" |
Mark Salyzyn | 8c8130e | 2015-12-09 11:21:28 -0800 | [diff] [blame] | 944 | }; |
| 945 | |
| 946 | printf("------ %s Extended CSD ------\n", ext_csd_path); |
| 947 | |
| 948 | fd = TEMP_FAILURE_RETRY(open(ext_csd_path, |
| 949 | O_RDONLY | O_NONBLOCK | O_CLOEXEC)); |
| 950 | if (fd < 0) { |
| 951 | printf("*** %s: %s\n\n", ext_csd_path, strerror(errno)); |
| 952 | return; |
| 953 | } |
| 954 | |
| 955 | bytes_read = TEMP_FAILURE_RETRY(read(fd, buffer, sizeof(buffer))); |
| 956 | close(fd); |
| 957 | if (bytes_read < 0) { |
| 958 | printf("*** %s: %s\n\n", ext_csd_path, strerror(errno)); |
| 959 | return; |
| 960 | } |
Mark Salyzyn | 4b45d67 | 2015-12-11 10:41:52 -0800 | [diff] [blame] | 961 | if (bytes_read < (ssize_t)(EXT_CSD_REV * sizeof(struct hex))) { |
Mark Salyzyn | 8c8130e | 2015-12-09 11:21:28 -0800 | [diff] [blame] | 962 | printf("*** %s: truncated content %zd\n\n", ext_csd_path, bytes_read); |
| 963 | return; |
| 964 | } |
| 965 | |
| 966 | ext_csd_rev = 0; |
| 967 | if (sscanf(buffer[EXT_CSD_REV].str, "%02x", &ext_csd_rev) != 1) { |
| 968 | printf("*** %s: EXT_CSD_REV parse error \"%.2s\"\n\n", |
| 969 | ext_csd_path, buffer[EXT_CSD_REV].str); |
| 970 | return; |
| 971 | } |
| 972 | |
| 973 | printf("rev 1.%d (MMC %s)\n", |
| 974 | ext_csd_rev, |
| 975 | (ext_csd_rev < (int)(sizeof(ver_str) / sizeof(ver_str[0]))) ? |
| 976 | ver_str[ext_csd_rev] : |
| 977 | "Unknown"); |
| 978 | if (ext_csd_rev < 7) { |
| 979 | printf("\n"); |
| 980 | return; |
| 981 | } |
| 982 | |
Mark Salyzyn | 4b45d67 | 2015-12-11 10:41:52 -0800 | [diff] [blame] | 983 | if (bytes_read < (ssize_t)(EXT_PRE_EOL_INFO * sizeof(struct hex))) { |
Mark Salyzyn | 8c8130e | 2015-12-09 11:21:28 -0800 | [diff] [blame] | 984 | printf("*** %s: truncated content %zd\n\n", ext_csd_path, bytes_read); |
| 985 | return; |
| 986 | } |
| 987 | |
| 988 | ext_pre_eol_info = 0; |
| 989 | if (sscanf(buffer[EXT_PRE_EOL_INFO].str, "%02x", &ext_pre_eol_info) != 1) { |
| 990 | printf("*** %s: PRE_EOL_INFO parse error \"%.2s\"\n\n", |
| 991 | ext_csd_path, buffer[EXT_PRE_EOL_INFO].str); |
| 992 | return; |
| 993 | } |
| 994 | printf("PRE_EOL_INFO %d (MMC %s)\n", |
| 995 | ext_pre_eol_info, |
| 996 | eol_str[(ext_pre_eol_info < (int) |
| 997 | (sizeof(eol_str) / sizeof(eol_str[0]))) ? |
| 998 | ext_pre_eol_info : 0]); |
| 999 | |
| 1000 | for (size_t lifetime = EXT_DEVICE_LIFE_TIME_EST_TYP_A; |
| 1001 | lifetime <= EXT_DEVICE_LIFE_TIME_EST_TYP_B; |
| 1002 | ++lifetime) { |
| 1003 | int ext_device_life_time_est; |
| 1004 | static const char *est_str[] = { |
| 1005 | "Undefined", |
| 1006 | "0-10% of device lifetime used", |
| 1007 | "10-20% of device lifetime used", |
| 1008 | "20-30% of device lifetime used", |
| 1009 | "30-40% of device lifetime used", |
| 1010 | "40-50% of device lifetime used", |
| 1011 | "50-60% of device lifetime used", |
| 1012 | "60-70% of device lifetime used", |
| 1013 | "70-80% of device lifetime used", |
| 1014 | "80-90% of device lifetime used", |
| 1015 | "90-100% of device lifetime used", |
| 1016 | "Exceeded the maximum estimated device lifetime", |
| 1017 | }; |
| 1018 | |
Mark Salyzyn | 4b45d67 | 2015-12-11 10:41:52 -0800 | [diff] [blame] | 1019 | if (bytes_read < (ssize_t)(lifetime * sizeof(struct hex))) { |
Mark Salyzyn | 8c8130e | 2015-12-09 11:21:28 -0800 | [diff] [blame] | 1020 | printf("*** %s: truncated content %zd\n", ext_csd_path, bytes_read); |
| 1021 | break; |
| 1022 | } |
| 1023 | |
| 1024 | ext_device_life_time_est = 0; |
| 1025 | if (sscanf(buffer[lifetime].str, "%02x", &ext_device_life_time_est) != 1) { |
| 1026 | printf("*** %s: DEVICE_LIFE_TIME_EST_TYP_%c parse error \"%.2s\"\n", |
| 1027 | ext_csd_path, |
| 1028 | (unsigned)(lifetime - EXT_DEVICE_LIFE_TIME_EST_TYP_A) + 'A', |
| 1029 | buffer[lifetime].str); |
| 1030 | continue; |
| 1031 | } |
| 1032 | printf("DEVICE_LIFE_TIME_EST_TYP_%c %d (MMC %s)\n", |
| 1033 | (unsigned)(lifetime - EXT_DEVICE_LIFE_TIME_EST_TYP_A) + 'A', |
| 1034 | ext_device_life_time_est, |
| 1035 | est_str[(ext_device_life_time_est < (int) |
| 1036 | (sizeof(est_str) / sizeof(est_str[0]))) ? |
| 1037 | ext_device_life_time_est : 0]); |
| 1038 | } |
| 1039 | |
| 1040 | printf("\n"); |
| 1041 | } |