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