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