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