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> |
| 29 | #include <sys/time.h> |
| 30 | #include <sys/wait.h> |
| 31 | #include <sys/klog.h> |
| 32 | #include <time.h> |
| 33 | #include <unistd.h> |
John Michelau | e7b6cf1 | 2013-03-07 15:35:35 -0600 | [diff] [blame] | 34 | #include <sys/prctl.h> |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 35 | |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 36 | #include <cutils/debugger.h> |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 37 | #include <cutils/properties.h> |
| 38 | #include <cutils/sockets.h> |
| 39 | #include <private/android_filesystem_config.h> |
| 40 | |
Robert Craig | 9579837 | 2013-04-04 06:33:10 -0400 | [diff] [blame] | 41 | #include <selinux/android.h> |
| 42 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 43 | #include "dumpstate.h" |
| 44 | |
Jeff Brown | 1dc94e3 | 2014-09-11 14:15:27 -0700 | [diff] [blame] | 45 | static const int64_t NANOS_PER_SEC = 1000000000; |
| 46 | |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 47 | /* list of native processes to include in the native dumps */ |
| 48 | static const char* native_processes_to_dump[] = { |
James Dong | 1fc4f80 | 2012-09-10 16:08:48 -0700 | [diff] [blame] | 49 | "/system/bin/drmserver", |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 50 | "/system/bin/mediaserver", |
| 51 | "/system/bin/sdcard", |
| 52 | "/system/bin/surfaceflinger", |
keunyoung | d907b32 | 2015-10-16 15:21:43 -0700 | [diff] [blame^] | 53 | "/system/bin/vehicle_network_service", |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 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 | 93d705b | 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 | |
| 107 | sprintf(cmdpath,"/proc/%d/cmdline", pid); |
| 108 | memset(cmdline, 0, sizeof(cmdline)); |
Nick Kralevich | cd67e9f | 2015-03-19 11:30:59 -0700 | [diff] [blame] | 109 | if ((fd = TEMP_FAILURE_RETRY(open(cmdpath, O_RDONLY | O_CLOEXEC))) < 0) { |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 110 | strcpy(cmdline, "N/A"); |
| 111 | } else { |
Colin Cross | 0c22e8b | 2012-11-02 15:46:56 -0700 | [diff] [blame] | 112 | read(fd, cmdline, sizeof(cmdline) - 1); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 113 | close(fd); |
| 114 | } |
Colin Cross | 0c22e8b | 2012-11-02 15:46:56 -0700 | [diff] [blame] | 115 | helper(pid, cmdline, arg); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | closedir(d); |
| 119 | } |
| 120 | |
Colin Cross | 0c22e8b | 2012-11-02 15:46:56 -0700 | [diff] [blame] | 121 | static void for_each_pid_helper(int pid, const char *cmdline, void *arg) { |
Felipe Leme | 8620bb4 | 2015-11-10 11:04:45 -0800 | [diff] [blame] | 122 | for_each_pid_func *func = (for_each_pid_func*) arg; |
Colin Cross | 0c22e8b | 2012-11-02 15:46:56 -0700 | [diff] [blame] | 123 | func(pid, cmdline); |
| 124 | } |
| 125 | |
| 126 | void for_each_pid(for_each_pid_func func, const char *header) { |
Felipe Leme | 93d705b | 2015-11-10 20:10:25 -0800 | [diff] [blame] | 127 | ON_DRY_RUN_RETURN(); |
Felipe Leme | 8620bb4 | 2015-11-10 11:04:45 -0800 | [diff] [blame] | 128 | __for_each_pid(for_each_pid_helper, header, (void *)func); |
Colin Cross | 0c22e8b | 2012-11-02 15:46:56 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | static void for_each_tid_helper(int pid, const char *cmdline, void *arg) { |
| 132 | DIR *d; |
| 133 | struct dirent *de; |
| 134 | char taskpath[255]; |
Felipe Leme | 8620bb4 | 2015-11-10 11:04:45 -0800 | [diff] [blame] | 135 | for_each_tid_func *func = (for_each_tid_func *) arg; |
Colin Cross | 0c22e8b | 2012-11-02 15:46:56 -0700 | [diff] [blame] | 136 | |
| 137 | sprintf(taskpath, "/proc/%d/task", pid); |
| 138 | |
| 139 | if (!(d = opendir(taskpath))) { |
| 140 | printf("Failed to open %s (%s)\n", taskpath, strerror(errno)); |
| 141 | return; |
| 142 | } |
| 143 | |
| 144 | func(pid, pid, cmdline); |
| 145 | |
| 146 | while ((de = readdir(d))) { |
| 147 | int tid; |
| 148 | int fd; |
| 149 | char commpath[255]; |
| 150 | char comm[255]; |
| 151 | |
| 152 | if (!(tid = atoi(de->d_name))) { |
| 153 | continue; |
| 154 | } |
| 155 | |
| 156 | if (tid == pid) |
| 157 | continue; |
| 158 | |
| 159 | sprintf(commpath,"/proc/%d/comm", tid); |
Colin Cross | 1493a39 | 2012-11-07 11:25:31 -0800 | [diff] [blame] | 160 | memset(comm, 0, sizeof(comm)); |
Nick Kralevich | cd67e9f | 2015-03-19 11:30:59 -0700 | [diff] [blame] | 161 | if ((fd = TEMP_FAILURE_RETRY(open(commpath, O_RDONLY | O_CLOEXEC))) < 0) { |
Colin Cross | 0c22e8b | 2012-11-02 15:46:56 -0700 | [diff] [blame] | 162 | strcpy(comm, "N/A"); |
| 163 | } else { |
| 164 | char *c; |
| 165 | read(fd, comm, sizeof(comm) - 1); |
| 166 | close(fd); |
| 167 | |
| 168 | c = strrchr(comm, '\n'); |
| 169 | if (c) { |
| 170 | *c = '\0'; |
| 171 | } |
| 172 | } |
| 173 | func(pid, tid, comm); |
| 174 | } |
| 175 | |
| 176 | closedir(d); |
| 177 | } |
| 178 | |
| 179 | void for_each_tid(for_each_tid_func func, const char *header) { |
Felipe Leme | 93d705b | 2015-11-10 20:10:25 -0800 | [diff] [blame] | 180 | ON_DRY_RUN_RETURN(); |
Felipe Leme | 8620bb4 | 2015-11-10 11:04:45 -0800 | [diff] [blame] | 181 | __for_each_pid(for_each_tid_helper, header, (void *) func); |
Colin Cross | 0c22e8b | 2012-11-02 15:46:56 -0700 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | void show_wchan(int pid, int tid, const char *name) { |
Felipe Leme | 93d705b | 2015-11-10 20:10:25 -0800 | [diff] [blame] | 185 | ON_DRY_RUN_RETURN(); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 186 | char path[255]; |
| 187 | char buffer[255]; |
| 188 | int fd; |
Colin Cross | 0c22e8b | 2012-11-02 15:46:56 -0700 | [diff] [blame] | 189 | char name_buffer[255]; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 190 | |
| 191 | memset(buffer, 0, sizeof(buffer)); |
| 192 | |
Colin Cross | 0c22e8b | 2012-11-02 15:46:56 -0700 | [diff] [blame] | 193 | sprintf(path, "/proc/%d/wchan", tid); |
Nick Kralevich | cd67e9f | 2015-03-19 11:30:59 -0700 | [diff] [blame] | 194 | if ((fd = TEMP_FAILURE_RETRY(open(path, O_RDONLY | O_CLOEXEC))) < 0) { |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 195 | printf("Failed to open '%s' (%s)\n", path, strerror(errno)); |
| 196 | return; |
| 197 | } |
| 198 | |
| 199 | if (read(fd, buffer, sizeof(buffer)) < 0) { |
| 200 | printf("Failed to read '%s' (%s)\n", path, strerror(errno)); |
| 201 | goto out_close; |
| 202 | } |
| 203 | |
Colin Cross | 0c22e8b | 2012-11-02 15:46:56 -0700 | [diff] [blame] | 204 | snprintf(name_buffer, sizeof(name_buffer), "%*s%s", |
| 205 | pid == tid ? 0 : 3, "", name); |
| 206 | |
| 207 | printf("%-7d %-32s %s\n", tid, name_buffer, buffer); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 208 | |
| 209 | out_close: |
| 210 | close(fd); |
| 211 | return; |
| 212 | } |
| 213 | |
| 214 | void do_dmesg() { |
| 215 | printf("------ KERNEL LOG (dmesg) ------\n"); |
Felipe Leme | 93d705b | 2015-11-10 20:10:25 -0800 | [diff] [blame] | 216 | ON_DRY_RUN_RETURN(); |
Elliott Hughes | 5f87b31 | 2012-09-17 11:43:40 -0700 | [diff] [blame] | 217 | /* Get size of kernel buffer */ |
| 218 | int size = klogctl(KLOG_SIZE_BUFFER, NULL, 0); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 219 | if (size <= 0) { |
| 220 | printf("Unexpected klogctl return value: %d\n\n", size); |
| 221 | return; |
| 222 | } |
| 223 | char *buf = (char *) malloc(size + 1); |
| 224 | if (buf == NULL) { |
| 225 | printf("memory allocation failed\n\n"); |
| 226 | return; |
| 227 | } |
| 228 | int retval = klogctl(KLOG_READ_ALL, buf, size); |
| 229 | if (retval < 0) { |
| 230 | printf("klogctl failure\n\n"); |
| 231 | free(buf); |
| 232 | return; |
| 233 | } |
| 234 | buf[retval] = '\0'; |
| 235 | printf("%s\n\n", buf); |
| 236 | free(buf); |
| 237 | return; |
| 238 | } |
| 239 | |
| 240 | void do_showmap(int pid, const char *name) { |
| 241 | char title[255]; |
| 242 | char arg[255]; |
| 243 | |
| 244 | sprintf(title, "SHOW MAP %d (%s)", pid, name); |
| 245 | sprintf(arg, "%d", pid); |
| 246 | run_command(title, 10, SU_PATH, "root", "showmap", arg, NULL); |
| 247 | } |
| 248 | |
Christopher Ferris | 54bcc5f | 2015-02-10 12:15:01 -0800 | [diff] [blame] | 249 | 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] | 250 | if (title) printf("------ %s (%s", title, path); |
| 251 | |
| 252 | if (title) { |
| 253 | struct stat st; |
| 254 | if (memcmp(path, "/proc/", 6) && memcmp(path, "/sys/", 5) && !fstat(fd, &st)) { |
| 255 | char stamp[80]; |
| 256 | time_t mtime = st.st_mtime; |
| 257 | strftime(stamp, sizeof(stamp), "%Y-%m-%d %H:%M:%S", localtime(&mtime)); |
| 258 | printf(": %s", stamp); |
| 259 | } |
| 260 | printf(") ------\n"); |
| 261 | } |
| 262 | |
Christopher Ferris | 54bcc5f | 2015-02-10 12:15:01 -0800 | [diff] [blame] | 263 | bool newline = false; |
| 264 | fd_set read_set; |
| 265 | struct timeval tm; |
| 266 | while (1) { |
| 267 | FD_ZERO(&read_set); |
| 268 | FD_SET(fd, &read_set); |
| 269 | /* Timeout if no data is read for 30 seconds. */ |
| 270 | tm.tv_sec = 30; |
| 271 | tm.tv_usec = 0; |
| 272 | uint64_t elapsed = nanotime(); |
| 273 | int ret = TEMP_FAILURE_RETRY(select(fd + 1, &read_set, NULL, NULL, &tm)); |
| 274 | if (ret == -1) { |
| 275 | printf("*** %s: select failed: %s\n", path, strerror(errno)); |
| 276 | newline = true; |
| 277 | break; |
| 278 | } else if (ret == 0) { |
| 279 | elapsed = nanotime() - elapsed; |
| 280 | printf("*** %s: Timed out after %.3fs\n", path, |
| 281 | (float) elapsed / NANOS_PER_SEC); |
| 282 | newline = true; |
| 283 | break; |
| 284 | } else { |
| 285 | char buffer[65536]; |
| 286 | ssize_t bytes_read = TEMP_FAILURE_RETRY(read(fd, buffer, sizeof(buffer))); |
| 287 | if (bytes_read > 0) { |
| 288 | fwrite(buffer, bytes_read, 1, stdout); |
| 289 | newline = (buffer[bytes_read-1] == '\n'); |
| 290 | } else { |
| 291 | if (bytes_read == -1) { |
| 292 | printf("*** %s: Failed to read from fd: %s", path, strerror(errno)); |
| 293 | newline = true; |
| 294 | } |
| 295 | break; |
| 296 | } |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 297 | } |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 298 | } |
Elliott Hughes | 997abb6 | 2015-05-15 17:05:40 -0700 | [diff] [blame] | 299 | close(fd); |
Christopher Ferris | 7dc7f32 | 2014-07-22 16:08:19 -0700 | [diff] [blame] | 300 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 301 | if (!newline) printf("\n"); |
| 302 | if (title) printf("\n"); |
| 303 | return 0; |
| 304 | } |
| 305 | |
Christopher Ferris | 54bcc5f | 2015-02-10 12:15:01 -0800 | [diff] [blame] | 306 | /* prints the contents of a file */ |
| 307 | int dump_file(const char *title, const char *path) { |
Felipe Leme | 93d705b | 2015-11-10 20:10:25 -0800 | [diff] [blame] | 308 | if (title) printf("------ %s (%s) ------\n", title, path); |
| 309 | ON_DRY_RUN_RETURN(0); |
| 310 | |
Christopher Ferris | 54bcc5f | 2015-02-10 12:15:01 -0800 | [diff] [blame] | 311 | int fd = TEMP_FAILURE_RETRY(open(path, O_RDONLY | O_NONBLOCK | O_CLOEXEC)); |
| 312 | if (fd < 0) { |
| 313 | int err = errno; |
Christopher Ferris | 54bcc5f | 2015-02-10 12:15:01 -0800 | [diff] [blame] | 314 | printf("*** %s: %s\n", path, strerror(err)); |
| 315 | if (title) printf("\n"); |
| 316 | return -1; |
| 317 | } |
| 318 | return _dump_file_from_fd(title, path, fd); |
| 319 | } |
| 320 | |
Mark Salyzyn | 326842f | 2015-04-30 09:49:41 -0700 | [diff] [blame] | 321 | /* calls skip to gate calling dump_from_fd recursively |
| 322 | * in the specified directory. dump_from_fd defaults to |
| 323 | * dump_file_from_fd above when set to NULL. skip defaults |
| 324 | * to false when set to NULL. dump_from_fd will always be |
| 325 | * called with title NULL. |
| 326 | */ |
| 327 | int dump_files(const char *title, const char *dir, |
| 328 | bool (*skip)(const char *path), |
| 329 | int (*dump_from_fd)(const char *title, const char *path, int fd)) { |
| 330 | DIR *dirp; |
| 331 | struct dirent *d; |
| 332 | char *newpath = NULL; |
Felipe Leme | 8620bb4 | 2015-11-10 11:04:45 -0800 | [diff] [blame] | 333 | const char *slash = "/"; |
Mark Salyzyn | 326842f | 2015-04-30 09:49:41 -0700 | [diff] [blame] | 334 | int fd, retval = 0; |
| 335 | |
| 336 | if (title) { |
| 337 | printf("------ %s (%s) ------\n", title, dir); |
| 338 | } |
Felipe Leme | 93d705b | 2015-11-10 20:10:25 -0800 | [diff] [blame] | 339 | ON_DRY_RUN_RETURN(0); |
Mark Salyzyn | 326842f | 2015-04-30 09:49:41 -0700 | [diff] [blame] | 340 | |
| 341 | if (dir[strlen(dir) - 1] == '/') { |
| 342 | ++slash; |
| 343 | } |
| 344 | dirp = opendir(dir); |
| 345 | if (dirp == NULL) { |
| 346 | retval = -errno; |
| 347 | fprintf(stderr, "%s: %s\n", dir, strerror(errno)); |
| 348 | return retval; |
| 349 | } |
| 350 | |
| 351 | if (!dump_from_fd) { |
| 352 | dump_from_fd = dump_file_from_fd; |
| 353 | } |
| 354 | for (; ((d = readdir(dirp))); free(newpath), newpath = NULL) { |
| 355 | if ((d->d_name[0] == '.') |
| 356 | && (((d->d_name[1] == '.') && (d->d_name[2] == '\0')) |
| 357 | || (d->d_name[1] == '\0'))) { |
| 358 | continue; |
| 359 | } |
| 360 | asprintf(&newpath, "%s%s%s%s", dir, slash, d->d_name, |
| 361 | (d->d_type == DT_DIR) ? "/" : ""); |
| 362 | if (!newpath) { |
| 363 | retval = -errno; |
| 364 | continue; |
| 365 | } |
| 366 | if (skip && (*skip)(newpath)) { |
| 367 | continue; |
| 368 | } |
| 369 | if (d->d_type == DT_DIR) { |
| 370 | int ret = dump_files(NULL, newpath, skip, dump_from_fd); |
| 371 | if (ret < 0) { |
| 372 | retval = ret; |
| 373 | } |
| 374 | continue; |
| 375 | } |
| 376 | fd = TEMP_FAILURE_RETRY(open(newpath, O_RDONLY | O_NONBLOCK | O_CLOEXEC)); |
| 377 | if (fd < 0) { |
| 378 | retval = fd; |
| 379 | printf("*** %s: %s\n", newpath, strerror(errno)); |
| 380 | continue; |
| 381 | } |
| 382 | (*dump_from_fd)(NULL, newpath, fd); |
| 383 | } |
| 384 | closedir(dirp); |
| 385 | if (title) { |
| 386 | printf("\n"); |
| 387 | } |
| 388 | return retval; |
| 389 | } |
| 390 | |
Christopher Ferris | 54bcc5f | 2015-02-10 12:15:01 -0800 | [diff] [blame] | 391 | /* fd must have been opened with the flag O_NONBLOCK. With this flag set, |
| 392 | * it's possible to avoid issues where opening the file itself can get |
| 393 | * stuck. |
| 394 | */ |
| 395 | int dump_file_from_fd(const char *title, const char *path, int fd) { |
Felipe Leme | 93d705b | 2015-11-10 20:10:25 -0800 | [diff] [blame] | 396 | ON_DRY_RUN_RETURN(0); |
Christopher Ferris | 54bcc5f | 2015-02-10 12:15:01 -0800 | [diff] [blame] | 397 | int flags = fcntl(fd, F_GETFL); |
| 398 | if (flags == -1) { |
| 399 | printf("*** %s: failed to get flags on fd %d: %s\n", path, fd, strerror(errno)); |
| 400 | return -1; |
| 401 | } else if (!(flags & O_NONBLOCK)) { |
| 402 | printf("*** %s: fd must have O_NONBLOCK set.\n", path); |
| 403 | return -1; |
| 404 | } |
| 405 | return _dump_file_from_fd(title, path, fd); |
Jeff Brown | 1dc94e3 | 2014-09-11 14:15:27 -0700 | [diff] [blame] | 406 | } |
| 407 | |
Christopher Ferris | 1a9a338 | 2015-01-30 11:00:52 -0800 | [diff] [blame] | 408 | bool waitpid_with_timeout(pid_t pid, int timeout_seconds, int* status) { |
| 409 | sigset_t child_mask, old_mask; |
| 410 | sigemptyset(&child_mask); |
| 411 | sigaddset(&child_mask, SIGCHLD); |
| 412 | |
| 413 | if (sigprocmask(SIG_BLOCK, &child_mask, &old_mask) == -1) { |
| 414 | printf("*** sigprocmask failed: %s\n", strerror(errno)); |
| 415 | return false; |
| 416 | } |
| 417 | |
| 418 | struct timespec ts; |
| 419 | ts.tv_sec = timeout_seconds; |
| 420 | ts.tv_nsec = 0; |
| 421 | int ret = TEMP_FAILURE_RETRY(sigtimedwait(&child_mask, NULL, &ts)); |
| 422 | int saved_errno = errno; |
| 423 | // Set the signals back the way they were. |
| 424 | if (sigprocmask(SIG_SETMASK, &old_mask, NULL) == -1) { |
| 425 | printf("*** sigprocmask failed: %s\n", strerror(errno)); |
| 426 | if (ret == 0) { |
| 427 | return false; |
| 428 | } |
| 429 | } |
| 430 | if (ret == -1) { |
| 431 | errno = saved_errno; |
| 432 | if (errno == EAGAIN) { |
| 433 | errno = ETIMEDOUT; |
| 434 | } else { |
| 435 | printf("*** sigtimedwait failed: %s\n", strerror(errno)); |
| 436 | } |
| 437 | return false; |
| 438 | } |
| 439 | |
| 440 | pid_t child_pid = waitpid(pid, status, WNOHANG); |
| 441 | if (child_pid != pid) { |
| 442 | if (child_pid != -1) { |
| 443 | printf("*** Waiting for pid %d, got pid %d instead\n", pid, child_pid); |
| 444 | } else { |
| 445 | printf("*** waitpid failed: %s\n", strerror(errno)); |
| 446 | } |
| 447 | return false; |
| 448 | } |
| 449 | return true; |
| 450 | } |
| 451 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 452 | /* forks a command and waits for it to finish */ |
| 453 | int run_command(const char *title, int timeout_seconds, const char *command, ...) { |
| 454 | fflush(stdout); |
Felipe Leme | 93d705b | 2015-11-10 20:10:25 -0800 | [diff] [blame] | 455 | |
| 456 | const char *args[1024] = {command}; |
| 457 | size_t arg; |
| 458 | va_list ap; |
| 459 | va_start(ap, command); |
| 460 | if (title) printf("------ %s (%s", title, command); |
| 461 | for (arg = 1; arg < sizeof(args) / sizeof(args[0]); ++arg) { |
| 462 | args[arg] = va_arg(ap, const char *); |
| 463 | if (args[arg] == NULL) break; |
| 464 | if (title) printf(" %s", args[arg]); |
| 465 | } |
| 466 | if (title) printf(") ------\n"); |
| 467 | fflush(stdout); |
| 468 | |
| 469 | ON_DRY_RUN_RETURN(0); |
| 470 | |
| 471 | return run_command_always(title, timeout_seconds, args); |
| 472 | } |
| 473 | |
| 474 | /* forks a command and waits for it to finish */ |
| 475 | int run_command_always(const char *title, int timeout_seconds, const char *args[]) { |
| 476 | const char *command = args[0]; |
| 477 | |
Christopher Ferris | 54bcc5f | 2015-02-10 12:15:01 -0800 | [diff] [blame] | 478 | uint64_t start = nanotime(); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 479 | pid_t pid = fork(); |
| 480 | |
| 481 | /* handle error case */ |
| 482 | if (pid < 0) { |
| 483 | printf("*** fork: %s\n", strerror(errno)); |
| 484 | return pid; |
| 485 | } |
| 486 | |
| 487 | /* handle child case */ |
| 488 | if (pid == 0) { |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 489 | |
John Michelau | e7b6cf1 | 2013-03-07 15:35:35 -0600 | [diff] [blame] | 490 | /* make sure the child dies when dumpstate dies */ |
| 491 | prctl(PR_SET_PDEATHSIG, SIGKILL); |
| 492 | |
Andres Morales | 2e671bb | 2014-08-21 12:38:22 -0700 | [diff] [blame] | 493 | /* just ignore SIGPIPE, will go down with parent's */ |
| 494 | struct sigaction sigact; |
| 495 | memset(&sigact, 0, sizeof(sigact)); |
| 496 | sigact.sa_handler = SIG_IGN; |
| 497 | sigaction(SIGPIPE, &sigact, NULL); |
| 498 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 499 | execvp(command, (char**) args); |
| 500 | printf("*** exec(%s): %s\n", command, strerror(errno)); |
| 501 | fflush(stdout); |
| 502 | _exit(-1); |
| 503 | } |
| 504 | |
| 505 | /* handle parent case */ |
Christopher Ferris | 1a9a338 | 2015-01-30 11:00:52 -0800 | [diff] [blame] | 506 | int status; |
| 507 | bool ret = waitpid_with_timeout(pid, timeout_seconds, &status); |
| 508 | uint64_t elapsed = nanotime() - start; |
| 509 | if (!ret) { |
| 510 | if (errno == ETIMEDOUT) { |
| 511 | printf("*** %s: Timed out after %.3fs (killing pid %d)\n", command, |
| 512 | (float) elapsed / NANOS_PER_SEC, pid); |
| 513 | } else { |
| 514 | printf("*** %s: Error after %.4fs (killing pid %d)\n", command, |
| 515 | (float) elapsed / NANOS_PER_SEC, pid); |
| 516 | } |
| 517 | kill(pid, SIGTERM); |
| 518 | if (!waitpid_with_timeout(pid, 5, NULL)) { |
| 519 | kill(pid, SIGKILL); |
| 520 | if (!waitpid_with_timeout(pid, 5, NULL)) { |
| 521 | printf("*** %s: Cannot kill %d even with SIGKILL.\n", command, pid); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 522 | } |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 523 | } |
Christopher Ferris | 1a9a338 | 2015-01-30 11:00:52 -0800 | [diff] [blame] | 524 | return -1; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 525 | } |
Christopher Ferris | 1a9a338 | 2015-01-30 11:00:52 -0800 | [diff] [blame] | 526 | |
| 527 | if (WIFSIGNALED(status)) { |
| 528 | printf("*** %s: Killed by signal %d\n", command, WTERMSIG(status)); |
| 529 | } else if (WIFEXITED(status) && WEXITSTATUS(status) > 0) { |
| 530 | printf("*** %s: Exit code %d\n", command, WEXITSTATUS(status)); |
| 531 | } |
| 532 | if (title) printf("[%s: %.3fs elapsed]\n\n", command, (float)elapsed / NANOS_PER_SEC); |
| 533 | |
| 534 | return status; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | size_t num_props = 0; |
| 538 | static char* props[2000]; |
| 539 | |
| 540 | static void print_prop(const char *key, const char *name, void *user) { |
| 541 | (void) user; |
| 542 | if (num_props < sizeof(props) / sizeof(props[0])) { |
| 543 | char buf[PROPERTY_KEY_MAX + PROPERTY_VALUE_MAX + 10]; |
| 544 | snprintf(buf, sizeof(buf), "[%s]: [%s]\n", key, name); |
| 545 | props[num_props++] = strdup(buf); |
| 546 | } |
| 547 | } |
| 548 | |
| 549 | static int compare_prop(const void *a, const void *b) { |
| 550 | return strcmp(*(char * const *) a, *(char * const *) b); |
| 551 | } |
| 552 | |
| 553 | /* prints all the system properties */ |
| 554 | void print_properties() { |
Felipe Leme | 93d705b | 2015-11-10 20:10:25 -0800 | [diff] [blame] | 555 | printf("------ SYSTEM PROPERTIES ------\n"); |
| 556 | ON_DRY_RUN_RETURN(); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 557 | size_t i; |
| 558 | num_props = 0; |
| 559 | property_list(print_prop, NULL); |
| 560 | qsort(&props, num_props, sizeof(props[0]), compare_prop); |
| 561 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 562 | for (i = 0; i < num_props; ++i) { |
| 563 | fputs(props[i], stdout); |
| 564 | free(props[i]); |
| 565 | } |
| 566 | printf("\n"); |
| 567 | } |
| 568 | |
| 569 | /* redirect output to a service control socket */ |
| 570 | void redirect_to_socket(FILE *redirect, const char *service) { |
| 571 | int s = android_get_control_socket(service); |
| 572 | if (s < 0) { |
| 573 | fprintf(stderr, "android_get_control_socket(%s): %s\n", service, strerror(errno)); |
| 574 | exit(1); |
| 575 | } |
Nick Kralevich | cd67e9f | 2015-03-19 11:30:59 -0700 | [diff] [blame] | 576 | fcntl(s, F_SETFD, FD_CLOEXEC); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 577 | if (listen(s, 4) < 0) { |
| 578 | fprintf(stderr, "listen(control socket): %s\n", strerror(errno)); |
| 579 | exit(1); |
| 580 | } |
| 581 | |
| 582 | struct sockaddr addr; |
| 583 | socklen_t alen = sizeof(addr); |
| 584 | int fd = accept(s, &addr, &alen); |
| 585 | if (fd < 0) { |
| 586 | fprintf(stderr, "accept(control socket): %s\n", strerror(errno)); |
| 587 | exit(1); |
| 588 | } |
| 589 | |
| 590 | fflush(redirect); |
| 591 | dup2(fd, fileno(redirect)); |
| 592 | close(fd); |
| 593 | } |
| 594 | |
Christopher Ferris | ff4a4dc | 2015-02-09 16:24:47 -0800 | [diff] [blame] | 595 | /* redirect output to a file */ |
| 596 | void redirect_to_file(FILE *redirect, char *path) { |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 597 | char *chp = path; |
| 598 | |
| 599 | /* skip initial slash */ |
| 600 | if (chp[0] == '/') |
| 601 | chp++; |
| 602 | |
| 603 | /* create leading directories, if necessary */ |
| 604 | while (chp && chp[0]) { |
| 605 | chp = strchr(chp, '/'); |
| 606 | if (chp) { |
| 607 | *chp = 0; |
Jeff Sharkey | 27f9e6d | 2013-03-13 15:45:50 -0700 | [diff] [blame] | 608 | mkdir(path, 0770); /* drwxrwx--- */ |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 609 | *chp++ = '/'; |
| 610 | } |
| 611 | } |
| 612 | |
Nick Kralevich | cd67e9f | 2015-03-19 11:30:59 -0700 | [diff] [blame] | 613 | 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] | 614 | S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 615 | if (fd < 0) { |
| 616 | fprintf(stderr, "%s: %s\n", path, strerror(errno)); |
| 617 | exit(1); |
| 618 | } |
| 619 | |
Christopher Ferris | ff4a4dc | 2015-02-09 16:24:47 -0800 | [diff] [blame] | 620 | TEMP_FAILURE_RETRY(dup2(fd, fileno(redirect))); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 621 | close(fd); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 622 | } |
| 623 | |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 624 | static bool should_dump_native_traces(const char* path) { |
| 625 | for (const char** p = native_processes_to_dump; *p; p++) { |
| 626 | if (!strcmp(*p, path)) { |
| 627 | return true; |
| 628 | } |
| 629 | } |
| 630 | return false; |
| 631 | } |
| 632 | |
| 633 | /* dump Dalvik and native stack traces, return the trace file location (NULL if none) */ |
| 634 | const char *dump_traces() { |
Felipe Leme | 93d705b | 2015-11-10 20:10:25 -0800 | [diff] [blame] | 635 | ON_DRY_RUN_RETURN(NULL); |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 636 | const char* result = NULL; |
| 637 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 638 | char traces_path[PROPERTY_VALUE_MAX] = ""; |
| 639 | property_get("dalvik.vm.stack-trace-file", traces_path, ""); |
| 640 | if (!traces_path[0]) return NULL; |
| 641 | |
| 642 | /* move the old traces.txt (if any) out of the way temporarily */ |
| 643 | char anr_traces_path[PATH_MAX]; |
| 644 | strlcpy(anr_traces_path, traces_path, sizeof(anr_traces_path)); |
| 645 | strlcat(anr_traces_path, ".anr", sizeof(anr_traces_path)); |
| 646 | if (rename(traces_path, anr_traces_path) && errno != ENOENT) { |
| 647 | fprintf(stderr, "rename(%s, %s): %s\n", traces_path, anr_traces_path, strerror(errno)); |
| 648 | return NULL; // Can't rename old traces.txt -- no permission? -- leave it alone instead |
| 649 | } |
| 650 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 651 | /* create a new, empty traces.txt file to receive stack dumps */ |
Nick Kralevich | cd67e9f | 2015-03-19 11:30:59 -0700 | [diff] [blame] | 652 | 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] | 653 | 0666)); /* -rw-rw-rw- */ |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 654 | if (fd < 0) { |
| 655 | fprintf(stderr, "%s: %s\n", traces_path, strerror(errno)); |
| 656 | return NULL; |
| 657 | } |
Nick Kralevich | c7f1fe2 | 2012-04-06 09:31:28 -0700 | [diff] [blame] | 658 | int chmod_ret = fchmod(fd, 0666); |
| 659 | if (chmod_ret < 0) { |
| 660 | fprintf(stderr, "fchmod on %s failed: %s\n", traces_path, strerror(errno)); |
| 661 | close(fd); |
| 662 | return NULL; |
| 663 | } |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 664 | |
Felipe Leme | 8620bb4 | 2015-11-10 11:04:45 -0800 | [diff] [blame] | 665 | /* Variables below must be initialized before 'goto' statements */ |
| 666 | int dalvik_found = 0; |
| 667 | int ifd, wfd = -1; |
| 668 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 669 | /* walk /proc and kill -QUIT all Dalvik processes */ |
| 670 | DIR *proc = opendir("/proc"); |
| 671 | if (proc == NULL) { |
| 672 | fprintf(stderr, "/proc: %s\n", strerror(errno)); |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 673 | goto error_close_fd; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 674 | } |
| 675 | |
| 676 | /* use inotify to find when processes are done dumping */ |
Felipe Leme | 8620bb4 | 2015-11-10 11:04:45 -0800 | [diff] [blame] | 677 | ifd = inotify_init(); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 678 | if (ifd < 0) { |
| 679 | fprintf(stderr, "inotify_init: %s\n", strerror(errno)); |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 680 | goto error_close_fd; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 681 | } |
| 682 | |
Felipe Leme | 8620bb4 | 2015-11-10 11:04:45 -0800 | [diff] [blame] | 683 | wfd = inotify_add_watch(ifd, traces_path, IN_CLOSE_WRITE); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 684 | if (wfd < 0) { |
| 685 | fprintf(stderr, "inotify_add_watch(%s): %s\n", traces_path, strerror(errno)); |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 686 | goto error_close_ifd; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 687 | } |
| 688 | |
| 689 | struct dirent *d; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 690 | while ((d = readdir(proc))) { |
| 691 | int pid = atoi(d->d_name); |
| 692 | if (pid <= 0) continue; |
| 693 | |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 694 | char path[PATH_MAX]; |
| 695 | char data[PATH_MAX]; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 696 | snprintf(path, sizeof(path), "/proc/%d/exe", pid); |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 697 | ssize_t len = readlink(path, data, sizeof(data) - 1); |
| 698 | if (len <= 0) { |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 699 | continue; |
| 700 | } |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 701 | data[len] = '\0'; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 702 | |
Colin Cross | 0d6180f | 2014-07-16 19:00:46 -0700 | [diff] [blame] | 703 | if (!strncmp(data, "/system/bin/app_process", strlen("/system/bin/app_process"))) { |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 704 | /* skip zygote -- it won't dump its stack anyway */ |
| 705 | snprintf(path, sizeof(path), "/proc/%d/cmdline", pid); |
Nick Kralevich | cd67e9f | 2015-03-19 11:30:59 -0700 | [diff] [blame] | 706 | int cfd = TEMP_FAILURE_RETRY(open(path, O_RDONLY | O_CLOEXEC)); |
Jeff Brown | 1dc94e3 | 2014-09-11 14:15:27 -0700 | [diff] [blame] | 707 | len = read(cfd, data, sizeof(data) - 1); |
| 708 | close(cfd); |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 709 | if (len <= 0) { |
| 710 | continue; |
| 711 | } |
| 712 | data[len] = '\0'; |
Colin Cross | 0d6180f | 2014-07-16 19:00:46 -0700 | [diff] [blame] | 713 | if (!strncmp(data, "zygote", strlen("zygote"))) { |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 714 | continue; |
| 715 | } |
| 716 | |
| 717 | ++dalvik_found; |
Christopher Ferris | 54bcc5f | 2015-02-10 12:15:01 -0800 | [diff] [blame] | 718 | uint64_t start = nanotime(); |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 719 | if (kill(pid, SIGQUIT)) { |
| 720 | fprintf(stderr, "kill(%d, SIGQUIT): %s\n", pid, strerror(errno)); |
| 721 | continue; |
| 722 | } |
| 723 | |
| 724 | /* wait for the writable-close notification from inotify */ |
| 725 | struct pollfd pfd = { ifd, POLLIN, 0 }; |
Nick Vaccaro | 85453ec | 2014-04-30 11:19:23 -0700 | [diff] [blame] | 726 | int ret = poll(&pfd, 1, 5000); /* 5 sec timeout */ |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 727 | if (ret < 0) { |
| 728 | fprintf(stderr, "poll: %s\n", strerror(errno)); |
| 729 | } else if (ret == 0) { |
| 730 | fprintf(stderr, "warning: timed out dumping pid %d\n", pid); |
| 731 | } else { |
| 732 | struct inotify_event ie; |
| 733 | read(ifd, &ie, sizeof(ie)); |
| 734 | } |
Jeff Brown | 1dc94e3 | 2014-09-11 14:15:27 -0700 | [diff] [blame] | 735 | |
| 736 | if (lseek(fd, 0, SEEK_END) < 0) { |
| 737 | fprintf(stderr, "lseek: %s\n", strerror(errno)); |
| 738 | } else { |
Christopher Ferris | 31ef855 | 2015-01-14 13:23:30 -0800 | [diff] [blame] | 739 | dprintf(fd, "[dump dalvik stack %d: %.3fs elapsed]\n", |
Jeff Brown | 1dc94e3 | 2014-09-11 14:15:27 -0700 | [diff] [blame] | 740 | pid, (float)(nanotime() - start) / NANOS_PER_SEC); |
Jeff Brown | 1dc94e3 | 2014-09-11 14:15:27 -0700 | [diff] [blame] | 741 | } |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 742 | } else if (should_dump_native_traces(data)) { |
| 743 | /* dump native process if appropriate */ |
| 744 | if (lseek(fd, 0, SEEK_END) < 0) { |
| 745 | fprintf(stderr, "lseek: %s\n", strerror(errno)); |
| 746 | } else { |
Christopher Ferris | 31ef855 | 2015-01-14 13:23:30 -0800 | [diff] [blame] | 747 | static uint16_t timeout_failures = 0; |
Christopher Ferris | 54bcc5f | 2015-02-10 12:15:01 -0800 | [diff] [blame] | 748 | uint64_t start = nanotime(); |
Christopher Ferris | 31ef855 | 2015-01-14 13:23:30 -0800 | [diff] [blame] | 749 | |
| 750 | /* If 3 backtrace dumps fail in a row, consider debuggerd dead. */ |
| 751 | if (timeout_failures == 3) { |
| 752 | dprintf(fd, "too many stack dump failures, skipping...\n"); |
| 753 | } else if (dump_backtrace_to_file_timeout(pid, fd, 20) == -1) { |
| 754 | dprintf(fd, "dumping failed, likely due to a timeout\n"); |
| 755 | timeout_failures++; |
| 756 | } else { |
| 757 | timeout_failures = 0; |
| 758 | } |
| 759 | dprintf(fd, "[dump native stack %d: %.3fs elapsed]\n", |
Jeff Brown | 1dc94e3 | 2014-09-11 14:15:27 -0700 | [diff] [blame] | 760 | pid, (float)(nanotime() - start) / NANOS_PER_SEC); |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 761 | } |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 762 | } |
| 763 | } |
| 764 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 765 | if (dalvik_found == 0) { |
| 766 | fprintf(stderr, "Warning: no Dalvik processes found to dump stacks\n"); |
| 767 | } |
| 768 | |
| 769 | static char dump_traces_path[PATH_MAX]; |
| 770 | strlcpy(dump_traces_path, traces_path, sizeof(dump_traces_path)); |
| 771 | strlcat(dump_traces_path, ".bugreport", sizeof(dump_traces_path)); |
| 772 | if (rename(traces_path, dump_traces_path)) { |
| 773 | 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] | 774 | goto error_close_ifd; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 775 | } |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 776 | result = dump_traces_path; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 777 | |
| 778 | /* replace the saved [ANR] traces.txt file */ |
| 779 | rename(anr_traces_path, traces_path); |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 780 | |
| 781 | error_close_ifd: |
| 782 | close(ifd); |
| 783 | error_close_fd: |
| 784 | close(fd); |
| 785 | return result; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 786 | } |
| 787 | |
Sreeram Ramachandran | 2b3bba3 | 2014-07-08 15:40:55 -0700 | [diff] [blame] | 788 | void dump_route_tables() { |
Felipe Leme | 93d705b | 2015-11-10 20:10:25 -0800 | [diff] [blame] | 789 | ON_DRY_RUN_RETURN(); |
Sreeram Ramachandran | 2b3bba3 | 2014-07-08 15:40:55 -0700 | [diff] [blame] | 790 | const char* const RT_TABLES_PATH = "/data/misc/net/rt_tables"; |
| 791 | dump_file("RT_TABLES", RT_TABLES_PATH); |
Nick Kralevich | cd67e9f | 2015-03-19 11:30:59 -0700 | [diff] [blame] | 792 | FILE* fp = fopen(RT_TABLES_PATH, "re"); |
Sreeram Ramachandran | 2b3bba3 | 2014-07-08 15:40:55 -0700 | [diff] [blame] | 793 | if (!fp) { |
| 794 | printf("*** %s: %s\n", RT_TABLES_PATH, strerror(errno)); |
| 795 | return; |
| 796 | } |
| 797 | char table[16]; |
| 798 | // Each line has an integer (the table number), a space, and a string (the table name). We only |
| 799 | // need the table number. It's a 32-bit unsigned number, so max 10 chars. Skip the table name. |
| 800 | // Add a fixed max limit so this doesn't go awry. |
| 801 | for (int i = 0; i < 64 && fscanf(fp, " %10s %*s", table) == 1; ++i) { |
| 802 | run_command("ROUTE TABLE IPv4", 10, "ip", "-4", "route", "show", "table", table, NULL); |
| 803 | run_command("ROUTE TABLE IPv6", 10, "ip", "-6", "route", "show", "table", table, NULL); |
| 804 | } |
| 805 | fclose(fp); |
| 806 | } |