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> |
Felipe Leme | cf6a8b4 | 2016-03-11 10:38:19 -0800 | [diff] [blame] | 28 | #include <sys/capability.h> |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 29 | #include <sys/inotify.h> |
| 30 | #include <sys/stat.h> |
| 31 | #include <sys/time.h> |
| 32 | #include <sys/wait.h> |
| 33 | #include <sys/klog.h> |
| 34 | #include <time.h> |
| 35 | #include <unistd.h> |
Felipe Leme | 36b3f6f | 2015-11-19 15:41:04 -0800 | [diff] [blame] | 36 | #include <vector> |
John Michelau | e7b6cf1 | 2013-03-07 15:35:35 -0600 | [diff] [blame] | 37 | #include <sys/prctl.h> |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 38 | |
Felipe Leme | 71bbfc5 | 2015-11-23 14:14:51 -0800 | [diff] [blame] | 39 | #define LOG_TAG "dumpstate" |
Mark Salyzyn | 261a733 | 2016-05-24 12:38:40 -0700 | [diff] [blame] | 40 | |
Mark Salyzyn | 290f4b9 | 2016-05-16 08:33:59 -0700 | [diff] [blame] | 41 | #include <android-base/file.h> |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 42 | #include <cutils/debugger.h> |
Felipe Leme | 71bbfc5 | 2015-11-23 14:14:51 -0800 | [diff] [blame] | 43 | #include <cutils/log.h> |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 44 | #include <cutils/properties.h> |
| 45 | #include <cutils/sockets.h> |
| 46 | #include <private/android_filesystem_config.h> |
| 47 | |
Robert Craig | 9579837 | 2013-04-04 06:33:10 -0400 | [diff] [blame] | 48 | #include <selinux/android.h> |
| 49 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 50 | #include "dumpstate.h" |
| 51 | |
Jeff Brown | 1dc94e3 | 2014-09-11 14:15:27 -0700 | [diff] [blame] | 52 | static const int64_t NANOS_PER_SEC = 1000000000; |
| 53 | |
Felipe Leme | 6188412 | 2016-06-13 09:23:30 -0700 | [diff] [blame^] | 54 | static const int TRACE_DUMP_TIMEOUT_MS = 10000; // 10 seconds |
| 55 | |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 56 | /* list of native processes to include in the native dumps */ |
Andy Hung | 5c04e74 | 2016-04-13 19:35:34 -0700 | [diff] [blame] | 57 | // This matches the /proc/pid/exe link instead of /proc/pid/cmdline. |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 58 | static const char* native_processes_to_dump[] = { |
Andy Hung | 9609bbd | 2015-12-15 12:42:50 -0800 | [diff] [blame] | 59 | "/system/bin/audioserver", |
Chien-Yu Chen | f5248da | 2016-01-28 14:23:03 -0800 | [diff] [blame] | 60 | "/system/bin/cameraserver", |
James Dong | 1fc4f80 | 2012-09-10 16:08:48 -0700 | [diff] [blame] | 61 | "/system/bin/drmserver", |
Andy Hung | 5c04e74 | 2016-04-13 19:35:34 -0700 | [diff] [blame] | 62 | "/system/bin/mediacodec", // media.codec |
| 63 | "/system/bin/mediadrmserver", |
| 64 | "/system/bin/mediaextractor", // media.extractor |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 65 | "/system/bin/mediaserver", |
| 66 | "/system/bin/sdcard", |
| 67 | "/system/bin/surfaceflinger", |
keunyoung | d907b32 | 2015-10-16 15:21:43 -0700 | [diff] [blame] | 68 | "/system/bin/vehicle_network_service", |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 69 | NULL, |
| 70 | }; |
| 71 | |
Felipe Leme | 608385d | 2016-02-01 10:35:38 -0800 | [diff] [blame] | 72 | DurationReporter::DurationReporter(const char *title) : DurationReporter(title, stdout) {} |
| 73 | |
| 74 | DurationReporter::DurationReporter(const char *title, FILE *out) { |
Felipe Leme | 78f2c86 | 2015-12-21 09:55:22 -0800 | [diff] [blame] | 75 | title_ = title; |
| 76 | if (title) { |
| 77 | started_ = DurationReporter::nanotime(); |
| 78 | } |
Felipe Leme | 608385d | 2016-02-01 10:35:38 -0800 | [diff] [blame] | 79 | out_ = out; |
Felipe Leme | 78f2c86 | 2015-12-21 09:55:22 -0800 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | DurationReporter::~DurationReporter() { |
| 83 | if (title_) { |
| 84 | uint64_t elapsed = DurationReporter::nanotime() - started_; |
| 85 | // Use "Yoda grammar" to make it easier to grep|sort sections. |
Felipe Leme | 608385d | 2016-02-01 10:35:38 -0800 | [diff] [blame] | 86 | if (out_) { |
| 87 | fprintf(out_, "------ %.3fs was the duration of '%s' ------\n", |
| 88 | (float) elapsed / NANOS_PER_SEC, title_); |
| 89 | } else { |
Felipe Leme | cbce55d | 2016-02-08 09:53:18 -0800 | [diff] [blame] | 90 | MYLOGD("Duration of '%s': %.3fs\n", title_, (float) elapsed / NANOS_PER_SEC); |
Felipe Leme | 608385d | 2016-02-01 10:35:38 -0800 | [diff] [blame] | 91 | } |
Felipe Leme | 78f2c86 | 2015-12-21 09:55:22 -0800 | [diff] [blame] | 92 | } |
| 93 | } |
| 94 | |
| 95 | uint64_t DurationReporter::DurationReporter::nanotime() { |
Christopher Ferris | 54bcc5f | 2015-02-10 12:15:01 -0800 | [diff] [blame] | 96 | struct timespec ts; |
| 97 | clock_gettime(CLOCK_MONOTONIC, &ts); |
Felipe Leme | 78f2c86 | 2015-12-21 09:55:22 -0800 | [diff] [blame] | 98 | return (uint64_t) ts.tv_sec * NANOS_PER_SEC + ts.tv_nsec; |
Christopher Ferris | 54bcc5f | 2015-02-10 12:15:01 -0800 | [diff] [blame] | 99 | } |
| 100 | |
John Spurlock | 5ecd4be | 2014-01-29 14:14:40 -0500 | [diff] [blame] | 101 | void for_each_userid(void (*func)(int), const char *header) { |
Felipe Leme | 93d705b | 2015-11-10 20:10:25 -0800 | [diff] [blame] | 102 | ON_DRY_RUN_RETURN(); |
John Spurlock | 5ecd4be | 2014-01-29 14:14:40 -0500 | [diff] [blame] | 103 | DIR *d; |
| 104 | struct dirent *de; |
| 105 | |
| 106 | if (header) printf("\n------ %s ------\n", header); |
| 107 | func(0); |
| 108 | |
| 109 | if (!(d = opendir("/data/system/users"))) { |
| 110 | printf("Failed to open /data/system/users (%s)\n", strerror(errno)); |
| 111 | return; |
| 112 | } |
| 113 | |
| 114 | while ((de = readdir(d))) { |
| 115 | int userid; |
| 116 | if (de->d_type != DT_DIR || !(userid = atoi(de->d_name))) { |
| 117 | continue; |
| 118 | } |
| 119 | func(userid); |
| 120 | } |
| 121 | |
| 122 | closedir(d); |
| 123 | } |
| 124 | |
Colin Cross | 0c22e8b | 2012-11-02 15:46:56 -0700 | [diff] [blame] | 125 | static void __for_each_pid(void (*helper)(int, const char *, void *), const char *header, void *arg) { |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 126 | DIR *d; |
| 127 | struct dirent *de; |
| 128 | |
| 129 | if (!(d = opendir("/proc"))) { |
| 130 | printf("Failed to open /proc (%s)\n", strerror(errno)); |
| 131 | return; |
| 132 | } |
| 133 | |
Felipe Leme | 635ca31 | 2016-01-05 14:23:02 -0800 | [diff] [blame] | 134 | if (header) printf("\n------ %s ------\n", header); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 135 | while ((de = readdir(d))) { |
| 136 | int pid; |
| 137 | int fd; |
| 138 | char cmdpath[255]; |
| 139 | char cmdline[255]; |
| 140 | |
| 141 | if (!(pid = atoi(de->d_name))) { |
| 142 | continue; |
| 143 | } |
| 144 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 145 | memset(cmdline, 0, sizeof(cmdline)); |
Mark Salyzyn | 0751efa | 2016-02-05 15:33:17 -0800 | [diff] [blame] | 146 | |
| 147 | snprintf(cmdpath, sizeof(cmdpath), "/proc/%d/cmdline", pid); |
| 148 | if ((fd = TEMP_FAILURE_RETRY(open(cmdpath, O_RDONLY | O_CLOEXEC))) >= 0) { |
| 149 | TEMP_FAILURE_RETRY(read(fd, cmdline, sizeof(cmdline) - 2)); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 150 | close(fd); |
Mark Salyzyn | 0751efa | 2016-02-05 15:33:17 -0800 | [diff] [blame] | 151 | if (cmdline[0]) { |
| 152 | helper(pid, cmdline, arg); |
| 153 | continue; |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | // if no cmdline, a kernel thread has comm |
| 158 | snprintf(cmdpath, sizeof(cmdpath), "/proc/%d/comm", pid); |
| 159 | if ((fd = TEMP_FAILURE_RETRY(open(cmdpath, O_RDONLY | O_CLOEXEC))) >= 0) { |
| 160 | TEMP_FAILURE_RETRY(read(fd, cmdline + 1, sizeof(cmdline) - 4)); |
| 161 | close(fd); |
| 162 | if (cmdline[1]) { |
| 163 | cmdline[0] = '['; |
| 164 | size_t len = strcspn(cmdline, "\f\b\r\n"); |
| 165 | cmdline[len] = ']'; |
| 166 | cmdline[len+1] = '\0'; |
| 167 | } |
| 168 | } |
| 169 | if (!cmdline[0]) { |
| 170 | strcpy(cmdline, "N/A"); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 171 | } |
Colin Cross | 0c22e8b | 2012-11-02 15:46:56 -0700 | [diff] [blame] | 172 | helper(pid, cmdline, arg); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | closedir(d); |
| 176 | } |
| 177 | |
Colin Cross | 0c22e8b | 2012-11-02 15:46:56 -0700 | [diff] [blame] | 178 | 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] | 179 | for_each_pid_func *func = (for_each_pid_func*) arg; |
Colin Cross | 0c22e8b | 2012-11-02 15:46:56 -0700 | [diff] [blame] | 180 | func(pid, cmdline); |
| 181 | } |
| 182 | |
| 183 | void for_each_pid(for_each_pid_func func, const char *header) { |
Felipe Leme | 93d705b | 2015-11-10 20:10:25 -0800 | [diff] [blame] | 184 | ON_DRY_RUN_RETURN(); |
Felipe Leme | 515eb0d | 2015-12-14 15:09:56 -0800 | [diff] [blame] | 185 | __for_each_pid(for_each_pid_helper, header, (void *) func); |
Colin Cross | 0c22e8b | 2012-11-02 15:46:56 -0700 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | static void for_each_tid_helper(int pid, const char *cmdline, void *arg) { |
| 189 | DIR *d; |
| 190 | struct dirent *de; |
| 191 | char taskpath[255]; |
Felipe Leme | 8620bb4 | 2015-11-10 11:04:45 -0800 | [diff] [blame] | 192 | for_each_tid_func *func = (for_each_tid_func *) arg; |
Colin Cross | 0c22e8b | 2012-11-02 15:46:56 -0700 | [diff] [blame] | 193 | |
Nick Kralevich | f0922cc | 2016-05-14 16:47:44 -0700 | [diff] [blame] | 194 | snprintf(taskpath, sizeof(taskpath), "/proc/%d/task", pid); |
Colin Cross | 0c22e8b | 2012-11-02 15:46:56 -0700 | [diff] [blame] | 195 | |
| 196 | if (!(d = opendir(taskpath))) { |
| 197 | printf("Failed to open %s (%s)\n", taskpath, strerror(errno)); |
| 198 | return; |
| 199 | } |
| 200 | |
| 201 | func(pid, pid, cmdline); |
| 202 | |
| 203 | while ((de = readdir(d))) { |
| 204 | int tid; |
| 205 | int fd; |
| 206 | char commpath[255]; |
| 207 | char comm[255]; |
| 208 | |
| 209 | if (!(tid = atoi(de->d_name))) { |
| 210 | continue; |
| 211 | } |
| 212 | |
| 213 | if (tid == pid) |
| 214 | continue; |
| 215 | |
Nick Kralevich | f0922cc | 2016-05-14 16:47:44 -0700 | [diff] [blame] | 216 | snprintf(commpath, sizeof(commpath), "/proc/%d/comm", tid); |
Colin Cross | 1493a39 | 2012-11-07 11:25:31 -0800 | [diff] [blame] | 217 | memset(comm, 0, sizeof(comm)); |
Nick Kralevich | cd67e9f | 2015-03-19 11:30:59 -0700 | [diff] [blame] | 218 | if ((fd = TEMP_FAILURE_RETRY(open(commpath, O_RDONLY | O_CLOEXEC))) < 0) { |
Colin Cross | 0c22e8b | 2012-11-02 15:46:56 -0700 | [diff] [blame] | 219 | strcpy(comm, "N/A"); |
| 220 | } else { |
| 221 | char *c; |
Mark Salyzyn | 0751efa | 2016-02-05 15:33:17 -0800 | [diff] [blame] | 222 | TEMP_FAILURE_RETRY(read(fd, comm, sizeof(comm) - 2)); |
Colin Cross | 0c22e8b | 2012-11-02 15:46:56 -0700 | [diff] [blame] | 223 | close(fd); |
| 224 | |
| 225 | c = strrchr(comm, '\n'); |
| 226 | if (c) { |
| 227 | *c = '\0'; |
| 228 | } |
| 229 | } |
| 230 | func(pid, tid, comm); |
| 231 | } |
| 232 | |
| 233 | closedir(d); |
| 234 | } |
| 235 | |
| 236 | void for_each_tid(for_each_tid_func func, const char *header) { |
Felipe Leme | 93d705b | 2015-11-10 20:10:25 -0800 | [diff] [blame] | 237 | ON_DRY_RUN_RETURN(); |
Felipe Leme | 8620bb4 | 2015-11-10 11:04:45 -0800 | [diff] [blame] | 238 | __for_each_pid(for_each_tid_helper, header, (void *) func); |
Colin Cross | 0c22e8b | 2012-11-02 15:46:56 -0700 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | void show_wchan(int pid, int tid, const char *name) { |
Felipe Leme | 93d705b | 2015-11-10 20:10:25 -0800 | [diff] [blame] | 242 | ON_DRY_RUN_RETURN(); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 243 | char path[255]; |
| 244 | char buffer[255]; |
Mark Salyzyn | 0751efa | 2016-02-05 15:33:17 -0800 | [diff] [blame] | 245 | int fd, ret, save_errno; |
Colin Cross | 0c22e8b | 2012-11-02 15:46:56 -0700 | [diff] [blame] | 246 | char name_buffer[255]; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 247 | |
| 248 | memset(buffer, 0, sizeof(buffer)); |
| 249 | |
Nick Kralevich | f0922cc | 2016-05-14 16:47:44 -0700 | [diff] [blame] | 250 | snprintf(path, sizeof(path), "/proc/%d/wchan", tid); |
Nick Kralevich | cd67e9f | 2015-03-19 11:30:59 -0700 | [diff] [blame] | 251 | if ((fd = TEMP_FAILURE_RETRY(open(path, O_RDONLY | O_CLOEXEC))) < 0) { |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 252 | printf("Failed to open '%s' (%s)\n", path, strerror(errno)); |
| 253 | return; |
| 254 | } |
| 255 | |
Mark Salyzyn | 0751efa | 2016-02-05 15:33:17 -0800 | [diff] [blame] | 256 | ret = TEMP_FAILURE_RETRY(read(fd, buffer, sizeof(buffer))); |
| 257 | save_errno = errno; |
| 258 | close(fd); |
| 259 | |
| 260 | if (ret < 0) { |
| 261 | printf("Failed to read '%s' (%s)\n", path, strerror(save_errno)); |
| 262 | return; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 263 | } |
| 264 | |
Colin Cross | 0c22e8b | 2012-11-02 15:46:56 -0700 | [diff] [blame] | 265 | snprintf(name_buffer, sizeof(name_buffer), "%*s%s", |
| 266 | pid == tid ? 0 : 3, "", name); |
| 267 | |
| 268 | printf("%-7d %-32s %s\n", tid, name_buffer, buffer); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 269 | |
Mark Salyzyn | 0751efa | 2016-02-05 15:33:17 -0800 | [diff] [blame] | 270 | return; |
| 271 | } |
| 272 | |
| 273 | // print time in centiseconds |
| 274 | static void snprcent(char *buffer, size_t len, size_t spc, |
| 275 | unsigned long long time) { |
| 276 | static long hz; // cache discovered hz |
| 277 | |
| 278 | if (hz <= 0) { |
| 279 | hz = sysconf(_SC_CLK_TCK); |
| 280 | if (hz <= 0) { |
| 281 | hz = 1000; |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | // convert to centiseconds |
| 286 | time = (time * 100 + (hz / 2)) / hz; |
| 287 | |
| 288 | char str[16]; |
| 289 | |
| 290 | snprintf(str, sizeof(str), " %llu.%02u", |
| 291 | time / 100, (unsigned)(time % 100)); |
| 292 | size_t offset = strlen(buffer); |
| 293 | snprintf(buffer + offset, (len > offset) ? len - offset : 0, |
| 294 | "%*s", (spc > offset) ? (int)(spc - offset) : 0, str); |
| 295 | } |
| 296 | |
| 297 | // print permille as a percent |
| 298 | static void snprdec(char *buffer, size_t len, size_t spc, unsigned permille) { |
| 299 | char str[16]; |
| 300 | |
| 301 | snprintf(str, sizeof(str), " %u.%u%%", permille / 10, permille % 10); |
| 302 | size_t offset = strlen(buffer); |
| 303 | snprintf(buffer + offset, (len > offset) ? len - offset : 0, |
| 304 | "%*s", (spc > offset) ? (int)(spc - offset) : 0, str); |
| 305 | } |
| 306 | |
| 307 | void show_showtime(int pid, const char *name) { |
| 308 | ON_DRY_RUN_RETURN(); |
| 309 | char path[255]; |
| 310 | char buffer[1023]; |
| 311 | int fd, ret, save_errno; |
| 312 | |
| 313 | memset(buffer, 0, sizeof(buffer)); |
| 314 | |
Nick Kralevich | f0922cc | 2016-05-14 16:47:44 -0700 | [diff] [blame] | 315 | snprintf(path, sizeof(path), "/proc/%d/stat", pid); |
Mark Salyzyn | 0751efa | 2016-02-05 15:33:17 -0800 | [diff] [blame] | 316 | if ((fd = TEMP_FAILURE_RETRY(open(path, O_RDONLY | O_CLOEXEC))) < 0) { |
| 317 | printf("Failed to open '%s' (%s)\n", path, strerror(errno)); |
| 318 | return; |
| 319 | } |
| 320 | |
| 321 | ret = TEMP_FAILURE_RETRY(read(fd, buffer, sizeof(buffer))); |
| 322 | save_errno = errno; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 323 | close(fd); |
Mark Salyzyn | 0751efa | 2016-02-05 15:33:17 -0800 | [diff] [blame] | 324 | |
| 325 | if (ret < 0) { |
| 326 | printf("Failed to read '%s' (%s)\n", path, strerror(save_errno)); |
| 327 | return; |
| 328 | } |
| 329 | |
| 330 | // field 14 is utime |
| 331 | // field 15 is stime |
| 332 | // field 42 is iotime |
| 333 | unsigned long long utime = 0, stime = 0, iotime = 0; |
| 334 | if (sscanf(buffer, |
Mark Salyzyn | 791ddd3 | 2016-02-10 07:41:12 -0800 | [diff] [blame] | 335 | "%*u %*s %*s %*d %*d %*d %*d %*d %*d %*d %*d " |
| 336 | "%*d %*d %llu %llu %*d %*d %*d %*d %*d %*d " |
| 337 | "%*d %*d %*d %*d %*d %*d %*d %*d %*d %*d " |
| 338 | "%*d %*d %*d %*d %*d %*d %*d %*d %*d %llu ", |
Mark Salyzyn | 0751efa | 2016-02-05 15:33:17 -0800 | [diff] [blame] | 339 | &utime, &stime, &iotime) != 3) { |
| 340 | return; |
| 341 | } |
| 342 | |
| 343 | unsigned long long total = utime + stime; |
| 344 | if (!total) { |
| 345 | return; |
| 346 | } |
| 347 | |
| 348 | unsigned permille = (iotime * 1000 + (total / 2)) / total; |
| 349 | if (permille > 1000) { |
| 350 | permille = 1000; |
| 351 | } |
| 352 | |
| 353 | // try to beautify and stabilize columns at <80 characters |
| 354 | snprintf(buffer, sizeof(buffer), "%-6d%s", pid, name); |
| 355 | if ((name[0] != '[') || utime) { |
| 356 | snprcent(buffer, sizeof(buffer), 57, utime); |
| 357 | } |
| 358 | snprcent(buffer, sizeof(buffer), 65, stime); |
| 359 | if ((name[0] != '[') || iotime) { |
| 360 | snprcent(buffer, sizeof(buffer), 73, iotime); |
| 361 | } |
| 362 | if (iotime) { |
| 363 | snprdec(buffer, sizeof(buffer), 79, permille); |
| 364 | } |
| 365 | puts(buffer); // adds a trailing newline |
| 366 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 367 | return; |
| 368 | } |
| 369 | |
| 370 | void do_dmesg() { |
Felipe Leme | 78f2c86 | 2015-12-21 09:55:22 -0800 | [diff] [blame] | 371 | const char *title = "KERNEL LOG (dmesg)"; |
| 372 | DurationReporter duration_reporter(title); |
| 373 | printf("------ %s ------\n", title); |
| 374 | |
Felipe Leme | 93d705b | 2015-11-10 20:10:25 -0800 | [diff] [blame] | 375 | ON_DRY_RUN_RETURN(); |
Elliott Hughes | 5f87b31 | 2012-09-17 11:43:40 -0700 | [diff] [blame] | 376 | /* Get size of kernel buffer */ |
| 377 | int size = klogctl(KLOG_SIZE_BUFFER, NULL, 0); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 378 | if (size <= 0) { |
| 379 | printf("Unexpected klogctl return value: %d\n\n", size); |
| 380 | return; |
| 381 | } |
| 382 | char *buf = (char *) malloc(size + 1); |
| 383 | if (buf == NULL) { |
| 384 | printf("memory allocation failed\n\n"); |
| 385 | return; |
| 386 | } |
| 387 | int retval = klogctl(KLOG_READ_ALL, buf, size); |
| 388 | if (retval < 0) { |
| 389 | printf("klogctl failure\n\n"); |
| 390 | free(buf); |
| 391 | return; |
| 392 | } |
| 393 | buf[retval] = '\0'; |
| 394 | printf("%s\n\n", buf); |
| 395 | free(buf); |
| 396 | return; |
| 397 | } |
| 398 | |
| 399 | void do_showmap(int pid, const char *name) { |
| 400 | char title[255]; |
| 401 | char arg[255]; |
| 402 | |
Nick Kralevich | f0922cc | 2016-05-14 16:47:44 -0700 | [diff] [blame] | 403 | snprintf(title, sizeof(title), "SHOW MAP %d (%s)", pid, name); |
| 404 | snprintf(arg, sizeof(arg), "%d", pid); |
Felipe Leme | 3dba69a | 2016-03-17 14:59:13 -0700 | [diff] [blame] | 405 | run_command(title, 10, SU_PATH, "root", "showmap", "-q", arg, NULL); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 406 | } |
| 407 | |
Christopher Ferris | 54bcc5f | 2015-02-10 12:15:01 -0800 | [diff] [blame] | 408 | 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] | 409 | if (title) { |
Christopher Ferris | ed24d2a | 2015-11-12 14:01:56 -0800 | [diff] [blame] | 410 | printf("------ %s (%s", title, path); |
| 411 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 412 | struct stat st; |
Christopher Ferris | ed24d2a | 2015-11-12 14:01:56 -0800 | [diff] [blame] | 413 | // Only show the modification time of non-device files. |
| 414 | size_t path_len = strlen(path); |
| 415 | if ((path_len < 6 || memcmp(path, "/proc/", 6)) && |
| 416 | (path_len < 5 || memcmp(path, "/sys/", 5)) && |
| 417 | (path_len < 3 || memcmp(path, "/d/", 3)) && |
| 418 | !fstat(fd, &st)) { |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 419 | char stamp[80]; |
| 420 | time_t mtime = st.st_mtime; |
| 421 | strftime(stamp, sizeof(stamp), "%Y-%m-%d %H:%M:%S", localtime(&mtime)); |
| 422 | printf(": %s", stamp); |
| 423 | } |
| 424 | printf(") ------\n"); |
| 425 | } |
Felipe Leme | 71bbfc5 | 2015-11-23 14:14:51 -0800 | [diff] [blame] | 426 | ON_DRY_RUN({ update_progress(WEIGHT_FILE); close(fd); return 0; }); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 427 | |
Christopher Ferris | 54bcc5f | 2015-02-10 12:15:01 -0800 | [diff] [blame] | 428 | bool newline = false; |
| 429 | fd_set read_set; |
| 430 | struct timeval tm; |
| 431 | while (1) { |
| 432 | FD_ZERO(&read_set); |
| 433 | FD_SET(fd, &read_set); |
| 434 | /* Timeout if no data is read for 30 seconds. */ |
| 435 | tm.tv_sec = 30; |
| 436 | tm.tv_usec = 0; |
Felipe Leme | 78f2c86 | 2015-12-21 09:55:22 -0800 | [diff] [blame] | 437 | uint64_t elapsed = DurationReporter::nanotime(); |
Christopher Ferris | 54bcc5f | 2015-02-10 12:15:01 -0800 | [diff] [blame] | 438 | int ret = TEMP_FAILURE_RETRY(select(fd + 1, &read_set, NULL, NULL, &tm)); |
| 439 | if (ret == -1) { |
| 440 | printf("*** %s: select failed: %s\n", path, strerror(errno)); |
| 441 | newline = true; |
| 442 | break; |
| 443 | } else if (ret == 0) { |
Felipe Leme | 78f2c86 | 2015-12-21 09:55:22 -0800 | [diff] [blame] | 444 | elapsed = DurationReporter::nanotime() - elapsed; |
Christopher Ferris | 54bcc5f | 2015-02-10 12:15:01 -0800 | [diff] [blame] | 445 | printf("*** %s: Timed out after %.3fs\n", path, |
| 446 | (float) elapsed / NANOS_PER_SEC); |
| 447 | newline = true; |
| 448 | break; |
| 449 | } else { |
| 450 | char buffer[65536]; |
| 451 | ssize_t bytes_read = TEMP_FAILURE_RETRY(read(fd, buffer, sizeof(buffer))); |
| 452 | if (bytes_read > 0) { |
| 453 | fwrite(buffer, bytes_read, 1, stdout); |
| 454 | newline = (buffer[bytes_read-1] == '\n'); |
| 455 | } else { |
| 456 | if (bytes_read == -1) { |
| 457 | printf("*** %s: Failed to read from fd: %s", path, strerror(errno)); |
| 458 | newline = true; |
| 459 | } |
| 460 | break; |
| 461 | } |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 462 | } |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 463 | } |
Felipe Leme | 71bbfc5 | 2015-11-23 14:14:51 -0800 | [diff] [blame] | 464 | update_progress(WEIGHT_FILE); |
Elliott Hughes | 997abb6 | 2015-05-15 17:05:40 -0700 | [diff] [blame] | 465 | close(fd); |
Christopher Ferris | 7dc7f32 | 2014-07-22 16:08:19 -0700 | [diff] [blame] | 466 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 467 | if (!newline) printf("\n"); |
| 468 | if (title) printf("\n"); |
| 469 | return 0; |
| 470 | } |
| 471 | |
Christopher Ferris | 54bcc5f | 2015-02-10 12:15:01 -0800 | [diff] [blame] | 472 | /* prints the contents of a file */ |
| 473 | int dump_file(const char *title, const char *path) { |
Felipe Leme | 78f2c86 | 2015-12-21 09:55:22 -0800 | [diff] [blame] | 474 | DurationReporter duration_reporter(title); |
Christopher Ferris | 54bcc5f | 2015-02-10 12:15:01 -0800 | [diff] [blame] | 475 | int fd = TEMP_FAILURE_RETRY(open(path, O_RDONLY | O_NONBLOCK | O_CLOEXEC)); |
| 476 | if (fd < 0) { |
| 477 | int err = errno; |
Christopher Ferris | 54bcc5f | 2015-02-10 12:15:01 -0800 | [diff] [blame] | 478 | printf("*** %s: %s\n", path, strerror(err)); |
| 479 | if (title) printf("\n"); |
| 480 | return -1; |
| 481 | } |
| 482 | return _dump_file_from_fd(title, path, fd); |
| 483 | } |
| 484 | |
Felipe Leme | 71a74ac | 2016-03-17 15:43:25 -0700 | [diff] [blame] | 485 | int read_file_as_long(const char *path, long int *output) { |
| 486 | int fd = TEMP_FAILURE_RETRY(open(path, O_RDONLY | O_NONBLOCK | O_CLOEXEC)); |
| 487 | if (fd < 0) { |
| 488 | int err = errno; |
| 489 | MYLOGE("Error opening file descriptor for %s: %s\n", path, strerror(err)); |
| 490 | return -1; |
| 491 | } |
| 492 | char buffer[50]; |
| 493 | ssize_t bytes_read = TEMP_FAILURE_RETRY(read(fd, buffer, sizeof(buffer))); |
| 494 | if (bytes_read == -1) { |
| 495 | MYLOGE("Error reading file %s: %s\n", path, strerror(errno)); |
| 496 | return -2; |
| 497 | } |
| 498 | if (bytes_read == 0) { |
| 499 | MYLOGE("File %s is empty\n", path); |
| 500 | return -3; |
| 501 | } |
| 502 | *output = atoi(buffer); |
| 503 | return 0; |
| 504 | } |
| 505 | |
Mark Salyzyn | 326842f | 2015-04-30 09:49:41 -0700 | [diff] [blame] | 506 | /* calls skip to gate calling dump_from_fd recursively |
| 507 | * in the specified directory. dump_from_fd defaults to |
| 508 | * dump_file_from_fd above when set to NULL. skip defaults |
| 509 | * to false when set to NULL. dump_from_fd will always be |
| 510 | * called with title NULL. |
| 511 | */ |
| 512 | int dump_files(const char *title, const char *dir, |
| 513 | bool (*skip)(const char *path), |
| 514 | int (*dump_from_fd)(const char *title, const char *path, int fd)) { |
Felipe Leme | 78f2c86 | 2015-12-21 09:55:22 -0800 | [diff] [blame] | 515 | DurationReporter duration_reporter(title); |
Mark Salyzyn | 326842f | 2015-04-30 09:49:41 -0700 | [diff] [blame] | 516 | DIR *dirp; |
| 517 | struct dirent *d; |
| 518 | char *newpath = NULL; |
Felipe Leme | 8620bb4 | 2015-11-10 11:04:45 -0800 | [diff] [blame] | 519 | const char *slash = "/"; |
Mark Salyzyn | 326842f | 2015-04-30 09:49:41 -0700 | [diff] [blame] | 520 | int fd, retval = 0; |
| 521 | |
| 522 | if (title) { |
| 523 | printf("------ %s (%s) ------\n", title, dir); |
| 524 | } |
Felipe Leme | 93d705b | 2015-11-10 20:10:25 -0800 | [diff] [blame] | 525 | ON_DRY_RUN_RETURN(0); |
Mark Salyzyn | 326842f | 2015-04-30 09:49:41 -0700 | [diff] [blame] | 526 | |
| 527 | if (dir[strlen(dir) - 1] == '/') { |
| 528 | ++slash; |
| 529 | } |
| 530 | dirp = opendir(dir); |
| 531 | if (dirp == NULL) { |
| 532 | retval = -errno; |
Felipe Leme | 107a05f | 2016-03-08 15:11:15 -0800 | [diff] [blame] | 533 | MYLOGE("%s: %s\n", dir, strerror(errno)); |
Mark Salyzyn | 326842f | 2015-04-30 09:49:41 -0700 | [diff] [blame] | 534 | return retval; |
| 535 | } |
| 536 | |
| 537 | if (!dump_from_fd) { |
| 538 | dump_from_fd = dump_file_from_fd; |
| 539 | } |
| 540 | for (; ((d = readdir(dirp))); free(newpath), newpath = NULL) { |
| 541 | if ((d->d_name[0] == '.') |
| 542 | && (((d->d_name[1] == '.') && (d->d_name[2] == '\0')) |
| 543 | || (d->d_name[1] == '\0'))) { |
| 544 | continue; |
| 545 | } |
| 546 | asprintf(&newpath, "%s%s%s%s", dir, slash, d->d_name, |
| 547 | (d->d_type == DT_DIR) ? "/" : ""); |
| 548 | if (!newpath) { |
| 549 | retval = -errno; |
| 550 | continue; |
| 551 | } |
| 552 | if (skip && (*skip)(newpath)) { |
| 553 | continue; |
| 554 | } |
| 555 | if (d->d_type == DT_DIR) { |
| 556 | int ret = dump_files(NULL, newpath, skip, dump_from_fd); |
| 557 | if (ret < 0) { |
| 558 | retval = ret; |
| 559 | } |
| 560 | continue; |
| 561 | } |
| 562 | fd = TEMP_FAILURE_RETRY(open(newpath, O_RDONLY | O_NONBLOCK | O_CLOEXEC)); |
| 563 | if (fd < 0) { |
| 564 | retval = fd; |
| 565 | printf("*** %s: %s\n", newpath, strerror(errno)); |
| 566 | continue; |
| 567 | } |
| 568 | (*dump_from_fd)(NULL, newpath, fd); |
| 569 | } |
| 570 | closedir(dirp); |
| 571 | if (title) { |
| 572 | printf("\n"); |
| 573 | } |
| 574 | return retval; |
| 575 | } |
| 576 | |
Christopher Ferris | 54bcc5f | 2015-02-10 12:15:01 -0800 | [diff] [blame] | 577 | /* fd must have been opened with the flag O_NONBLOCK. With this flag set, |
| 578 | * it's possible to avoid issues where opening the file itself can get |
| 579 | * stuck. |
| 580 | */ |
| 581 | int dump_file_from_fd(const char *title, const char *path, int fd) { |
Felipe Leme | 6811616 | 2015-11-10 20:10:25 -0800 | [diff] [blame] | 582 | ON_DRY_RUN_RETURN(0); |
Christopher Ferris | 54bcc5f | 2015-02-10 12:15:01 -0800 | [diff] [blame] | 583 | int flags = fcntl(fd, F_GETFL); |
| 584 | if (flags == -1) { |
| 585 | 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] | 586 | close(fd); |
Christopher Ferris | 54bcc5f | 2015-02-10 12:15:01 -0800 | [diff] [blame] | 587 | return -1; |
| 588 | } else if (!(flags & O_NONBLOCK)) { |
| 589 | printf("*** %s: fd must have O_NONBLOCK set.\n", path); |
Christopher Ferris | ed24d2a | 2015-11-12 14:01:56 -0800 | [diff] [blame] | 590 | close(fd); |
Christopher Ferris | 54bcc5f | 2015-02-10 12:15:01 -0800 | [diff] [blame] | 591 | return -1; |
| 592 | } |
| 593 | return _dump_file_from_fd(title, path, fd); |
Jeff Brown | 1dc94e3 | 2014-09-11 14:15:27 -0700 | [diff] [blame] | 594 | } |
| 595 | |
Christopher Ferris | 1a9a338 | 2015-01-30 11:00:52 -0800 | [diff] [blame] | 596 | bool waitpid_with_timeout(pid_t pid, int timeout_seconds, int* status) { |
| 597 | sigset_t child_mask, old_mask; |
| 598 | sigemptyset(&child_mask); |
| 599 | sigaddset(&child_mask, SIGCHLD); |
| 600 | |
| 601 | if (sigprocmask(SIG_BLOCK, &child_mask, &old_mask) == -1) { |
| 602 | printf("*** sigprocmask failed: %s\n", strerror(errno)); |
| 603 | return false; |
| 604 | } |
| 605 | |
| 606 | struct timespec ts; |
| 607 | ts.tv_sec = timeout_seconds; |
| 608 | ts.tv_nsec = 0; |
| 609 | int ret = TEMP_FAILURE_RETRY(sigtimedwait(&child_mask, NULL, &ts)); |
| 610 | int saved_errno = errno; |
| 611 | // Set the signals back the way they were. |
| 612 | if (sigprocmask(SIG_SETMASK, &old_mask, NULL) == -1) { |
| 613 | printf("*** sigprocmask failed: %s\n", strerror(errno)); |
| 614 | if (ret == 0) { |
| 615 | return false; |
| 616 | } |
| 617 | } |
| 618 | if (ret == -1) { |
| 619 | errno = saved_errno; |
| 620 | if (errno == EAGAIN) { |
| 621 | errno = ETIMEDOUT; |
| 622 | } else { |
| 623 | printf("*** sigtimedwait failed: %s\n", strerror(errno)); |
| 624 | } |
| 625 | return false; |
| 626 | } |
| 627 | |
| 628 | pid_t child_pid = waitpid(pid, status, WNOHANG); |
| 629 | if (child_pid != pid) { |
| 630 | if (child_pid != -1) { |
| 631 | printf("*** Waiting for pid %d, got pid %d instead\n", pid, child_pid); |
| 632 | } else { |
| 633 | printf("*** waitpid failed: %s\n", strerror(errno)); |
| 634 | } |
| 635 | return false; |
| 636 | } |
| 637 | return true; |
| 638 | } |
| 639 | |
Felipe Leme | a34efb7 | 2016-03-11 09:33:32 -0800 | [diff] [blame] | 640 | // TODO: refactor all those commands that convert args |
| 641 | void format_args(const char* command, const char *args[], std::string *string); |
| 642 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 643 | int run_command(const char *title, int timeout_seconds, const char *command, ...) { |
Felipe Leme | 78f2c86 | 2015-12-21 09:55:22 -0800 | [diff] [blame] | 644 | DurationReporter duration_reporter(title); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 645 | fflush(stdout); |
Felipe Leme | 93d705b | 2015-11-10 20:10:25 -0800 | [diff] [blame] | 646 | |
| 647 | const char *args[1024] = {command}; |
| 648 | size_t arg; |
| 649 | va_list ap; |
| 650 | va_start(ap, command); |
| 651 | if (title) printf("------ %s (%s", title, command); |
Felipe Leme | a34efb7 | 2016-03-11 09:33:32 -0800 | [diff] [blame] | 652 | bool null_terminated = false; |
Felipe Leme | 93d705b | 2015-11-10 20:10:25 -0800 | [diff] [blame] | 653 | for (arg = 1; arg < sizeof(args) / sizeof(args[0]); ++arg) { |
| 654 | args[arg] = va_arg(ap, const char *); |
Felipe Leme | a34efb7 | 2016-03-11 09:33:32 -0800 | [diff] [blame] | 655 | if (args[arg] == nullptr) { |
| 656 | null_terminated = true; |
| 657 | break; |
| 658 | } |
Felipe Leme | ea160d1 | 2016-03-24 11:29:44 -0700 | [diff] [blame] | 659 | // TODO: null_terminated check is not really working; line below would crash dumpstate if |
| 660 | // nullptr is missing |
Felipe Leme | 93d705b | 2015-11-10 20:10:25 -0800 | [diff] [blame] | 661 | if (title) printf(" %s", args[arg]); |
| 662 | } |
| 663 | if (title) printf(") ------\n"); |
| 664 | fflush(stdout); |
Felipe Leme | a34efb7 | 2016-03-11 09:33:32 -0800 | [diff] [blame] | 665 | if (!null_terminated) { |
| 666 | // Fail now, otherwise execvp() call on run_command_always() might hang. |
| 667 | std::string cmd; |
| 668 | format_args(command, args, &cmd); |
| 669 | MYLOGE("skipping command %s because its args were not NULL-terminated", cmd.c_str()); |
Adam Buchbinder | 8ede547 | 2016-05-23 13:18:52 -0700 | [diff] [blame] | 670 | va_end(ap); |
Felipe Leme | a34efb7 | 2016-03-11 09:33:32 -0800 | [diff] [blame] | 671 | return -1; |
| 672 | } |
Felipe Leme | 93d705b | 2015-11-10 20:10:25 -0800 | [diff] [blame] | 673 | |
Felipe Leme | 71bbfc5 | 2015-11-23 14:14:51 -0800 | [diff] [blame] | 674 | ON_DRY_RUN({ update_progress(timeout_seconds); va_end(ap); return 0; }); |
Felipe Leme | 93d705b | 2015-11-10 20:10:25 -0800 | [diff] [blame] | 675 | |
Felipe Leme | 29c3971 | 2016-04-01 10:02:00 -0700 | [diff] [blame] | 676 | int status = run_command_always(title, DONT_DROP_ROOT, NORMAL_STDOUT, timeout_seconds, args); |
Felipe Leme | cf6a8b4 | 2016-03-11 10:38:19 -0800 | [diff] [blame] | 677 | va_end(ap); |
| 678 | return status; |
| 679 | } |
| 680 | |
| 681 | int run_command_as_shell(const char *title, int timeout_seconds, const char *command, ...) { |
| 682 | DurationReporter duration_reporter(title); |
| 683 | fflush(stdout); |
| 684 | |
| 685 | const char *args[1024] = {command}; |
| 686 | size_t arg; |
| 687 | va_list ap; |
| 688 | va_start(ap, command); |
| 689 | if (title) printf("------ %s (%s", title, command); |
| 690 | bool null_terminated = false; |
| 691 | for (arg = 1; arg < sizeof(args) / sizeof(args[0]); ++arg) { |
| 692 | args[arg] = va_arg(ap, const char *); |
| 693 | if (args[arg] == nullptr) { |
| 694 | null_terminated = true; |
| 695 | break; |
| 696 | } |
Felipe Leme | ea160d1 | 2016-03-24 11:29:44 -0700 | [diff] [blame] | 697 | // TODO: null_terminated check is not really working; line below would crash dumpstate if |
| 698 | // nullptr is missing |
Felipe Leme | cf6a8b4 | 2016-03-11 10:38:19 -0800 | [diff] [blame] | 699 | if (title) printf(" %s", args[arg]); |
| 700 | } |
| 701 | if (title) printf(") ------\n"); |
| 702 | fflush(stdout); |
| 703 | if (!null_terminated) { |
| 704 | // Fail now, otherwise execvp() call on run_command_always() might hang. |
| 705 | std::string cmd; |
| 706 | format_args(command, args, &cmd); |
| 707 | MYLOGE("skipping command %s because its args were not NULL-terminated", cmd.c_str()); |
Adam Buchbinder | 8ede547 | 2016-05-23 13:18:52 -0700 | [diff] [blame] | 708 | va_end(ap); |
Felipe Leme | cf6a8b4 | 2016-03-11 10:38:19 -0800 | [diff] [blame] | 709 | return -1; |
| 710 | } |
| 711 | |
| 712 | ON_DRY_RUN({ update_progress(timeout_seconds); va_end(ap); return 0; }); |
| 713 | |
Felipe Leme | 29c3971 | 2016-04-01 10:02:00 -0700 | [diff] [blame] | 714 | int status = run_command_always(title, DROP_ROOT, NORMAL_STDOUT, timeout_seconds, args); |
Felipe Leme | 71bbfc5 | 2015-11-23 14:14:51 -0800 | [diff] [blame] | 715 | va_end(ap); |
| 716 | return status; |
Felipe Leme | 93d705b | 2015-11-10 20:10:25 -0800 | [diff] [blame] | 717 | } |
| 718 | |
| 719 | /* forks a command and waits for it to finish */ |
Felipe Leme | 29c3971 | 2016-04-01 10:02:00 -0700 | [diff] [blame] | 720 | int run_command_always(const char *title, RootMode root_mode, StdoutMode stdout_mode, |
| 721 | int timeout_seconds, const char *args[]) { |
| 722 | bool silent = (stdout_mode == REDIRECT_TO_STDERR); |
Felipe Leme | ea160d1 | 2016-03-24 11:29:44 -0700 | [diff] [blame] | 723 | // TODO: need to check if args is null-terminated, otherwise execvp will crash dumpstate |
| 724 | |
Felipe Leme | 71bbfc5 | 2015-11-23 14:14:51 -0800 | [diff] [blame] | 725 | /* TODO: for now we're simplifying the progress calculation by using the timeout as the weight. |
| 726 | * It's a good approximation for most cases, except when calling dumpsys, where its weight |
| 727 | * should be much higher proportionally to its timeout. */ |
| 728 | int weight = timeout_seconds; |
Felipe Leme | 93d705b | 2015-11-10 20:10:25 -0800 | [diff] [blame] | 729 | |
Felipe Leme | 36b3f6f | 2015-11-19 15:41:04 -0800 | [diff] [blame] | 730 | const char *command = args[0]; |
Felipe Leme | 78f2c86 | 2015-12-21 09:55:22 -0800 | [diff] [blame] | 731 | uint64_t start = DurationReporter::nanotime(); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 732 | pid_t pid = fork(); |
| 733 | |
| 734 | /* handle error case */ |
| 735 | if (pid < 0) { |
Felipe Leme | 29c3971 | 2016-04-01 10:02:00 -0700 | [diff] [blame] | 736 | if (!silent) printf("*** fork: %s\n", strerror(errno)); |
| 737 | MYLOGE("*** fork: %s\n", strerror(errno)); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 738 | return pid; |
| 739 | } |
| 740 | |
| 741 | /* handle child case */ |
| 742 | if (pid == 0) { |
Felipe Leme | 29c3971 | 2016-04-01 10:02:00 -0700 | [diff] [blame] | 743 | if (root_mode == DROP_ROOT && !drop_root_user()) { |
| 744 | if (!silent) printf("*** fail todrop root before running %s: %s\n", command, |
| 745 | strerror(errno)); |
| 746 | MYLOGE("*** could not drop root before running %s: %s\n", command, strerror(errno)); |
Felipe Leme | 73f731c | 2016-03-23 16:47:00 -0700 | [diff] [blame] | 747 | return -1; |
Felipe Leme | cf6a8b4 | 2016-03-11 10:38:19 -0800 | [diff] [blame] | 748 | } |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 749 | |
Felipe Leme | 29c3971 | 2016-04-01 10:02:00 -0700 | [diff] [blame] | 750 | if (silent) { |
| 751 | // Redirect stderr to stdout |
| 752 | dup2(STDERR_FILENO, STDOUT_FILENO); |
| 753 | } |
| 754 | |
John Michelau | e7b6cf1 | 2013-03-07 15:35:35 -0600 | [diff] [blame] | 755 | /* make sure the child dies when dumpstate dies */ |
| 756 | prctl(PR_SET_PDEATHSIG, SIGKILL); |
| 757 | |
Andres Morales | 2e671bb | 2014-08-21 12:38:22 -0700 | [diff] [blame] | 758 | /* just ignore SIGPIPE, will go down with parent's */ |
| 759 | struct sigaction sigact; |
| 760 | memset(&sigact, 0, sizeof(sigact)); |
| 761 | sigact.sa_handler = SIG_IGN; |
| 762 | sigaction(SIGPIPE, &sigact, NULL); |
| 763 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 764 | execvp(command, (char**) args); |
Felipe Leme | ea160d1 | 2016-03-24 11:29:44 -0700 | [diff] [blame] | 765 | // execvp's result will be handled after waitpid_with_timeout() below, but if it failed, |
| 766 | // it's safer to exit dumpstate. |
| 767 | MYLOGD("execvp on command '%s' failed (error: %s)", command, strerror(errno)); |
Felipe Leme | ec72578 | 2016-03-23 11:47:00 -0700 | [diff] [blame] | 768 | fflush(stdout); |
Felipe Leme | baa85bd | 2016-03-29 13:29:11 -0700 | [diff] [blame] | 769 | // Must call _exit (instead of exit), otherwise it will corrupt the zip file. |
| 770 | _exit(EXIT_FAILURE); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 771 | } |
| 772 | |
| 773 | /* handle parent case */ |
Christopher Ferris | 1a9a338 | 2015-01-30 11:00:52 -0800 | [diff] [blame] | 774 | int status; |
| 775 | bool ret = waitpid_with_timeout(pid, timeout_seconds, &status); |
Felipe Leme | 78f2c86 | 2015-12-21 09:55:22 -0800 | [diff] [blame] | 776 | uint64_t elapsed = DurationReporter::nanotime() - start; |
Felipe Leme | a34efb7 | 2016-03-11 09:33:32 -0800 | [diff] [blame] | 777 | std::string cmd; // used to log command and its args |
Christopher Ferris | 1a9a338 | 2015-01-30 11:00:52 -0800 | [diff] [blame] | 778 | if (!ret) { |
| 779 | if (errno == ETIMEDOUT) { |
Felipe Leme | a34efb7 | 2016-03-11 09:33:32 -0800 | [diff] [blame] | 780 | format_args(command, args, &cmd); |
Felipe Leme | 29c3971 | 2016-04-01 10:02:00 -0700 | [diff] [blame] | 781 | if (!silent) printf("*** command '%s' timed out after %.3fs (killing pid %d)\n", |
| 782 | cmd.c_str(), (float) elapsed / NANOS_PER_SEC, pid); |
Felipe Leme | a34efb7 | 2016-03-11 09:33:32 -0800 | [diff] [blame] | 783 | MYLOGE("command '%s' timed out after %.3fs (killing pid %d)\n", cmd.c_str(), |
Christopher Ferris | 1a9a338 | 2015-01-30 11:00:52 -0800 | [diff] [blame] | 784 | (float) elapsed / NANOS_PER_SEC, pid); |
| 785 | } else { |
Felipe Leme | a34efb7 | 2016-03-11 09:33:32 -0800 | [diff] [blame] | 786 | format_args(command, args, &cmd); |
Felipe Leme | 29c3971 | 2016-04-01 10:02:00 -0700 | [diff] [blame] | 787 | if (!silent) printf("*** command '%s': Error after %.4fs (killing pid %d)\n", |
| 788 | cmd.c_str(), (float) elapsed / NANOS_PER_SEC, pid); |
Felipe Leme | a34efb7 | 2016-03-11 09:33:32 -0800 | [diff] [blame] | 789 | MYLOGE("command '%s': Error after %.4fs (killing pid %d)\n", cmd.c_str(), |
Christopher Ferris | 1a9a338 | 2015-01-30 11:00:52 -0800 | [diff] [blame] | 790 | (float) elapsed / NANOS_PER_SEC, pid); |
| 791 | } |
| 792 | kill(pid, SIGTERM); |
| 793 | if (!waitpid_with_timeout(pid, 5, NULL)) { |
| 794 | kill(pid, SIGKILL); |
| 795 | if (!waitpid_with_timeout(pid, 5, NULL)) { |
Felipe Leme | 29c3971 | 2016-04-01 10:02:00 -0700 | [diff] [blame] | 796 | if (!silent) printf("could not kill command '%s' (pid %d) even with SIGKILL.\n", |
| 797 | command, pid); |
Felipe Leme | 14e034a | 2016-03-30 18:51:03 -0700 | [diff] [blame] | 798 | MYLOGE("could not kill command '%s' (pid %d) even with SIGKILL.\n", command, pid); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 799 | } |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 800 | } |
Christopher Ferris | 1a9a338 | 2015-01-30 11:00:52 -0800 | [diff] [blame] | 801 | return -1; |
Felipe Leme | a34efb7 | 2016-03-11 09:33:32 -0800 | [diff] [blame] | 802 | } else if (status) { |
| 803 | format_args(command, args, &cmd); |
Felipe Leme | 29c3971 | 2016-04-01 10:02:00 -0700 | [diff] [blame] | 804 | if (!silent) printf("*** command '%s' failed: %s\n", cmd.c_str(), strerror(errno)); |
Felipe Leme | a34efb7 | 2016-03-11 09:33:32 -0800 | [diff] [blame] | 805 | MYLOGE("command '%s' failed: %s\n", cmd.c_str(), strerror(errno)); |
| 806 | return -2; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 807 | } |
Christopher Ferris | 1a9a338 | 2015-01-30 11:00:52 -0800 | [diff] [blame] | 808 | |
| 809 | if (WIFSIGNALED(status)) { |
Felipe Leme | 29c3971 | 2016-04-01 10:02:00 -0700 | [diff] [blame] | 810 | if (!silent) printf("*** %s: Killed by signal %d\n", command, WTERMSIG(status)); |
| 811 | MYLOGE("*** %s: Killed by signal %d\n", command, WTERMSIG(status)); |
Christopher Ferris | 1a9a338 | 2015-01-30 11:00:52 -0800 | [diff] [blame] | 812 | } else if (WIFEXITED(status) && WEXITSTATUS(status) > 0) { |
Felipe Leme | 29c3971 | 2016-04-01 10:02:00 -0700 | [diff] [blame] | 813 | if (!silent) printf("*** %s: Exit code %d\n", command, WEXITSTATUS(status)); |
| 814 | MYLOGE("*** %s: Exit code %d\n", command, WEXITSTATUS(status)); |
Christopher Ferris | 1a9a338 | 2015-01-30 11:00:52 -0800 | [diff] [blame] | 815 | } |
Christopher Ferris | 1a9a338 | 2015-01-30 11:00:52 -0800 | [diff] [blame] | 816 | |
Felipe Leme | 71bbfc5 | 2015-11-23 14:14:51 -0800 | [diff] [blame] | 817 | if (weight > 0) { |
| 818 | update_progress(weight); |
| 819 | } |
Christopher Ferris | 1a9a338 | 2015-01-30 11:00:52 -0800 | [diff] [blame] | 820 | return status; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 821 | } |
| 822 | |
Felipe Leme | cf6a8b4 | 2016-03-11 10:38:19 -0800 | [diff] [blame] | 823 | bool drop_root_user() { |
| 824 | if (getgid() == AID_SHELL && getuid() == AID_SHELL) { |
| 825 | MYLOGD("drop_root_user(): already running as Shell"); |
| 826 | return true; |
| 827 | } |
| 828 | /* ensure we will keep capabilities when we drop root */ |
| 829 | if (prctl(PR_SET_KEEPCAPS, 1) < 0) { |
| 830 | MYLOGE("prctl(PR_SET_KEEPCAPS) failed: %s\n", strerror(errno)); |
| 831 | return false; |
| 832 | } |
| 833 | |
| 834 | gid_t groups[] = { AID_LOG, AID_SDCARD_R, AID_SDCARD_RW, |
| 835 | AID_MOUNT, AID_INET, AID_NET_BW_STATS, AID_READPROC }; |
| 836 | if (setgroups(sizeof(groups)/sizeof(groups[0]), groups) != 0) { |
| 837 | MYLOGE("Unable to setgroups, aborting: %s\n", strerror(errno)); |
| 838 | return false; |
| 839 | } |
| 840 | if (setgid(AID_SHELL) != 0) { |
| 841 | MYLOGE("Unable to setgid, aborting: %s\n", strerror(errno)); |
| 842 | return false; |
| 843 | } |
| 844 | if (setuid(AID_SHELL) != 0) { |
| 845 | MYLOGE("Unable to setuid, aborting: %s\n", strerror(errno)); |
| 846 | return false; |
| 847 | } |
| 848 | |
| 849 | struct __user_cap_header_struct capheader; |
| 850 | struct __user_cap_data_struct capdata[2]; |
| 851 | memset(&capheader, 0, sizeof(capheader)); |
| 852 | memset(&capdata, 0, sizeof(capdata)); |
| 853 | capheader.version = _LINUX_CAPABILITY_VERSION_3; |
| 854 | capheader.pid = 0; |
| 855 | |
| 856 | capdata[CAP_TO_INDEX(CAP_SYSLOG)].permitted = CAP_TO_MASK(CAP_SYSLOG); |
| 857 | capdata[CAP_TO_INDEX(CAP_SYSLOG)].effective = CAP_TO_MASK(CAP_SYSLOG); |
| 858 | capdata[0].inheritable = 0; |
| 859 | capdata[1].inheritable = 0; |
| 860 | |
| 861 | if (capset(&capheader, &capdata[0]) < 0) { |
| 862 | MYLOGE("capset failed: %s\n", strerror(errno)); |
| 863 | return false; |
| 864 | } |
| 865 | |
| 866 | return true; |
| 867 | } |
| 868 | |
Felipe Leme | 36b3f6f | 2015-11-19 15:41:04 -0800 | [diff] [blame] | 869 | void send_broadcast(const std::string& action, const std::vector<std::string>& args) { |
| 870 | if (args.size() > 1000) { |
Felipe Leme | 107a05f | 2016-03-08 15:11:15 -0800 | [diff] [blame] | 871 | MYLOGE("send_broadcast: too many arguments (%d)\n", (int) args.size()); |
Felipe Leme | 36b3f6f | 2015-11-19 15:41:04 -0800 | [diff] [blame] | 872 | return; |
| 873 | } |
Felipe Leme | cf6a8b4 | 2016-03-11 10:38:19 -0800 | [diff] [blame] | 874 | const char *am_args[1024] = { "/system/bin/am", "broadcast", "--user", "0", "-a", |
| 875 | action.c_str() }; |
| 876 | size_t am_index = 5; // Starts at the index of last initial value above. |
Felipe Leme | 36b3f6f | 2015-11-19 15:41:04 -0800 | [diff] [blame] | 877 | for (const std::string& arg : args) { |
| 878 | am_args[++am_index] = arg.c_str(); |
| 879 | } |
| 880 | // Always terminate with NULL. |
| 881 | am_args[am_index + 1] = NULL; |
Felipe Leme | a34efb7 | 2016-03-11 09:33:32 -0800 | [diff] [blame] | 882 | std::string args_string; |
| 883 | format_args(am_index + 1, am_args, &args_string); |
| 884 | MYLOGD("send_broadcast command: %s\n", args_string.c_str()); |
Felipe Leme | 29c3971 | 2016-04-01 10:02:00 -0700 | [diff] [blame] | 885 | run_command_always(NULL, DROP_ROOT, REDIRECT_TO_STDERR, 20, am_args); |
Felipe Leme | 36b3f6f | 2015-11-19 15:41:04 -0800 | [diff] [blame] | 886 | } |
| 887 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 888 | size_t num_props = 0; |
| 889 | static char* props[2000]; |
| 890 | |
| 891 | static void print_prop(const char *key, const char *name, void *user) { |
| 892 | (void) user; |
| 893 | if (num_props < sizeof(props) / sizeof(props[0])) { |
| 894 | char buf[PROPERTY_KEY_MAX + PROPERTY_VALUE_MAX + 10]; |
| 895 | snprintf(buf, sizeof(buf), "[%s]: [%s]\n", key, name); |
| 896 | props[num_props++] = strdup(buf); |
| 897 | } |
| 898 | } |
| 899 | |
| 900 | static int compare_prop(const void *a, const void *b) { |
| 901 | return strcmp(*(char * const *) a, *(char * const *) b); |
| 902 | } |
| 903 | |
| 904 | /* prints all the system properties */ |
| 905 | void print_properties() { |
Felipe Leme | 78f2c86 | 2015-12-21 09:55:22 -0800 | [diff] [blame] | 906 | const char* title = "SYSTEM PROPERTIES"; |
| 907 | DurationReporter duration_reporter(title); |
| 908 | printf("------ %s ------\n", title); |
Felipe Leme | 93d705b | 2015-11-10 20:10:25 -0800 | [diff] [blame] | 909 | ON_DRY_RUN_RETURN(); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 910 | size_t i; |
| 911 | num_props = 0; |
| 912 | property_list(print_prop, NULL); |
| 913 | qsort(&props, num_props, sizeof(props[0]), compare_prop); |
| 914 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 915 | for (i = 0; i < num_props; ++i) { |
| 916 | fputs(props[i], stdout); |
| 917 | free(props[i]); |
| 918 | } |
| 919 | printf("\n"); |
| 920 | } |
| 921 | |
Felipe Leme | 2628e9e | 2016-04-12 16:36:51 -0700 | [diff] [blame] | 922 | int open_socket(const char *service) { |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 923 | int s = android_get_control_socket(service); |
| 924 | if (s < 0) { |
Felipe Leme | 107a05f | 2016-03-08 15:11:15 -0800 | [diff] [blame] | 925 | MYLOGE("android_get_control_socket(%s): %s\n", service, strerror(errno)); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 926 | exit(1); |
| 927 | } |
Nick Kralevich | cd67e9f | 2015-03-19 11:30:59 -0700 | [diff] [blame] | 928 | fcntl(s, F_SETFD, FD_CLOEXEC); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 929 | if (listen(s, 4) < 0) { |
Felipe Leme | 107a05f | 2016-03-08 15:11:15 -0800 | [diff] [blame] | 930 | MYLOGE("listen(control socket): %s\n", strerror(errno)); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 931 | exit(1); |
| 932 | } |
| 933 | |
| 934 | struct sockaddr addr; |
| 935 | socklen_t alen = sizeof(addr); |
| 936 | int fd = accept(s, &addr, &alen); |
| 937 | if (fd < 0) { |
Felipe Leme | 107a05f | 2016-03-08 15:11:15 -0800 | [diff] [blame] | 938 | MYLOGE("accept(control socket): %s\n", strerror(errno)); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 939 | exit(1); |
| 940 | } |
| 941 | |
Felipe Leme | 2628e9e | 2016-04-12 16:36:51 -0700 | [diff] [blame] | 942 | return fd; |
| 943 | } |
| 944 | |
| 945 | /* redirect output to a service control socket */ |
| 946 | void redirect_to_socket(FILE *redirect, const char *service) { |
| 947 | int fd = open_socket(service); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 948 | fflush(redirect); |
| 949 | dup2(fd, fileno(redirect)); |
| 950 | close(fd); |
| 951 | } |
| 952 | |
Felipe Leme | 2628e9e | 2016-04-12 16:36:51 -0700 | [diff] [blame] | 953 | // TODO: should call is_valid_output_file and/or be merged into it. |
Felipe Leme | 111b9d0 | 2016-02-03 09:28:24 -0800 | [diff] [blame] | 954 | void create_parent_dirs(const char *path) { |
Srinath Sridharan | fdf52d3 | 2016-02-01 15:50:22 -0800 | [diff] [blame] | 955 | char *chp = const_cast<char *> (path); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 956 | |
| 957 | /* skip initial slash */ |
| 958 | if (chp[0] == '/') |
| 959 | chp++; |
| 960 | |
| 961 | /* create leading directories, if necessary */ |
Felipe Leme | 111b9d0 | 2016-02-03 09:28:24 -0800 | [diff] [blame] | 962 | struct stat dir_stat; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 963 | while (chp && chp[0]) { |
| 964 | chp = strchr(chp, '/'); |
| 965 | if (chp) { |
| 966 | *chp = 0; |
Felipe Leme | 111b9d0 | 2016-02-03 09:28:24 -0800 | [diff] [blame] | 967 | if (stat(path, &dir_stat) == -1 || !S_ISDIR(dir_stat.st_mode)) { |
Felipe Leme | cbce55d | 2016-02-08 09:53:18 -0800 | [diff] [blame] | 968 | MYLOGI("Creating directory %s\n", path); |
Felipe Leme | 111b9d0 | 2016-02-03 09:28:24 -0800 | [diff] [blame] | 969 | if (mkdir(path, 0770)) { /* drwxrwx--- */ |
Felipe Leme | cbce55d | 2016-02-08 09:53:18 -0800 | [diff] [blame] | 970 | MYLOGE("Unable to create directory %s: %s\n", path, strerror(errno)); |
Felipe Leme | 111b9d0 | 2016-02-03 09:28:24 -0800 | [diff] [blame] | 971 | } else if (chown(path, AID_SHELL, AID_SHELL)) { |
Felipe Leme | cbce55d | 2016-02-08 09:53:18 -0800 | [diff] [blame] | 972 | MYLOGE("Unable to change ownership of dir %s: %s\n", path, strerror(errno)); |
Felipe Leme | 111b9d0 | 2016-02-03 09:28:24 -0800 | [diff] [blame] | 973 | } |
| 974 | } |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 975 | *chp++ = '/'; |
| 976 | } |
| 977 | } |
Felipe Leme | 111b9d0 | 2016-02-03 09:28:24 -0800 | [diff] [blame] | 978 | } |
| 979 | |
| 980 | /* redirect output to a file */ |
| 981 | void redirect_to_file(FILE *redirect, char *path) { |
| 982 | create_parent_dirs(path); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 983 | |
Felipe Leme | 608385d | 2016-02-01 10:35:38 -0800 | [diff] [blame] | 984 | int fd = TEMP_FAILURE_RETRY(open(path, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC | O_NOFOLLOW, |
Christopher Ferris | ff4a4dc | 2015-02-09 16:24:47 -0800 | [diff] [blame] | 985 | S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 986 | if (fd < 0) { |
Felipe Leme | 107a05f | 2016-03-08 15:11:15 -0800 | [diff] [blame] | 987 | MYLOGE("%s: %s\n", path, strerror(errno)); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 988 | exit(1); |
| 989 | } |
| 990 | |
Christopher Ferris | ff4a4dc | 2015-02-09 16:24:47 -0800 | [diff] [blame] | 991 | TEMP_FAILURE_RETRY(dup2(fd, fileno(redirect))); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 992 | close(fd); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 993 | } |
| 994 | |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 995 | static bool should_dump_native_traces(const char* path) { |
| 996 | for (const char** p = native_processes_to_dump; *p; p++) { |
| 997 | if (!strcmp(*p, path)) { |
| 998 | return true; |
| 999 | } |
| 1000 | } |
| 1001 | return false; |
| 1002 | } |
| 1003 | |
| 1004 | /* dump Dalvik and native stack traces, return the trace file location (NULL if none) */ |
| 1005 | const char *dump_traces() { |
Felipe Leme | 608385d | 2016-02-01 10:35:38 -0800 | [diff] [blame] | 1006 | DurationReporter duration_reporter("DUMP TRACES", NULL); |
Felipe Leme | 93d705b | 2015-11-10 20:10:25 -0800 | [diff] [blame] | 1007 | ON_DRY_RUN_RETURN(NULL); |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 1008 | const char* result = NULL; |
| 1009 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1010 | char traces_path[PROPERTY_VALUE_MAX] = ""; |
| 1011 | property_get("dalvik.vm.stack-trace-file", traces_path, ""); |
| 1012 | if (!traces_path[0]) return NULL; |
| 1013 | |
| 1014 | /* move the old traces.txt (if any) out of the way temporarily */ |
| 1015 | char anr_traces_path[PATH_MAX]; |
| 1016 | strlcpy(anr_traces_path, traces_path, sizeof(anr_traces_path)); |
| 1017 | strlcat(anr_traces_path, ".anr", sizeof(anr_traces_path)); |
| 1018 | if (rename(traces_path, anr_traces_path) && errno != ENOENT) { |
Felipe Leme | 107a05f | 2016-03-08 15:11:15 -0800 | [diff] [blame] | 1019 | MYLOGE("rename(%s, %s): %s\n", traces_path, anr_traces_path, strerror(errno)); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1020 | return NULL; // Can't rename old traces.txt -- no permission? -- leave it alone instead |
| 1021 | } |
| 1022 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1023 | /* create a new, empty traces.txt file to receive stack dumps */ |
Nick Kralevich | cd67e9f | 2015-03-19 11:30:59 -0700 | [diff] [blame] | 1024 | 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] | 1025 | 0666)); /* -rw-rw-rw- */ |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1026 | if (fd < 0) { |
Felipe Leme | 107a05f | 2016-03-08 15:11:15 -0800 | [diff] [blame] | 1027 | MYLOGE("%s: %s\n", traces_path, strerror(errno)); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1028 | return NULL; |
| 1029 | } |
Nick Kralevich | c7f1fe2 | 2012-04-06 09:31:28 -0700 | [diff] [blame] | 1030 | int chmod_ret = fchmod(fd, 0666); |
| 1031 | if (chmod_ret < 0) { |
Felipe Leme | 107a05f | 2016-03-08 15:11:15 -0800 | [diff] [blame] | 1032 | MYLOGE("fchmod on %s failed: %s\n", traces_path, strerror(errno)); |
Nick Kralevich | c7f1fe2 | 2012-04-06 09:31:28 -0700 | [diff] [blame] | 1033 | close(fd); |
| 1034 | return NULL; |
| 1035 | } |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1036 | |
Felipe Leme | 8620bb4 | 2015-11-10 11:04:45 -0800 | [diff] [blame] | 1037 | /* Variables below must be initialized before 'goto' statements */ |
| 1038 | int dalvik_found = 0; |
| 1039 | int ifd, wfd = -1; |
| 1040 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1041 | /* walk /proc and kill -QUIT all Dalvik processes */ |
| 1042 | DIR *proc = opendir("/proc"); |
| 1043 | if (proc == NULL) { |
Felipe Leme | 107a05f | 2016-03-08 15:11:15 -0800 | [diff] [blame] | 1044 | MYLOGE("/proc: %s\n", strerror(errno)); |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 1045 | goto error_close_fd; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1046 | } |
| 1047 | |
| 1048 | /* use inotify to find when processes are done dumping */ |
Felipe Leme | 8620bb4 | 2015-11-10 11:04:45 -0800 | [diff] [blame] | 1049 | ifd = inotify_init(); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1050 | if (ifd < 0) { |
Felipe Leme | 107a05f | 2016-03-08 15:11:15 -0800 | [diff] [blame] | 1051 | MYLOGE("inotify_init: %s\n", strerror(errno)); |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 1052 | goto error_close_fd; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1053 | } |
| 1054 | |
Felipe Leme | 8620bb4 | 2015-11-10 11:04:45 -0800 | [diff] [blame] | 1055 | wfd = inotify_add_watch(ifd, traces_path, IN_CLOSE_WRITE); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1056 | if (wfd < 0) { |
Felipe Leme | 107a05f | 2016-03-08 15:11:15 -0800 | [diff] [blame] | 1057 | MYLOGE("inotify_add_watch(%s): %s\n", traces_path, strerror(errno)); |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 1058 | goto error_close_ifd; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1059 | } |
| 1060 | |
| 1061 | struct dirent *d; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1062 | while ((d = readdir(proc))) { |
| 1063 | int pid = atoi(d->d_name); |
| 1064 | if (pid <= 0) continue; |
| 1065 | |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 1066 | char path[PATH_MAX]; |
| 1067 | char data[PATH_MAX]; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1068 | snprintf(path, sizeof(path), "/proc/%d/exe", pid); |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 1069 | ssize_t len = readlink(path, data, sizeof(data) - 1); |
| 1070 | if (len <= 0) { |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1071 | continue; |
| 1072 | } |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 1073 | data[len] = '\0'; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1074 | |
Colin Cross | 0d6180f | 2014-07-16 19:00:46 -0700 | [diff] [blame] | 1075 | if (!strncmp(data, "/system/bin/app_process", strlen("/system/bin/app_process"))) { |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 1076 | /* skip zygote -- it won't dump its stack anyway */ |
| 1077 | snprintf(path, sizeof(path), "/proc/%d/cmdline", pid); |
Nick Kralevich | cd67e9f | 2015-03-19 11:30:59 -0700 | [diff] [blame] | 1078 | int cfd = TEMP_FAILURE_RETRY(open(path, O_RDONLY | O_CLOEXEC)); |
Jeff Brown | 1dc94e3 | 2014-09-11 14:15:27 -0700 | [diff] [blame] | 1079 | len = read(cfd, data, sizeof(data) - 1); |
| 1080 | close(cfd); |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 1081 | if (len <= 0) { |
| 1082 | continue; |
| 1083 | } |
| 1084 | data[len] = '\0'; |
Colin Cross | 0d6180f | 2014-07-16 19:00:46 -0700 | [diff] [blame] | 1085 | if (!strncmp(data, "zygote", strlen("zygote"))) { |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 1086 | continue; |
| 1087 | } |
| 1088 | |
| 1089 | ++dalvik_found; |
Felipe Leme | 78f2c86 | 2015-12-21 09:55:22 -0800 | [diff] [blame] | 1090 | uint64_t start = DurationReporter::nanotime(); |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 1091 | if (kill(pid, SIGQUIT)) { |
Felipe Leme | 107a05f | 2016-03-08 15:11:15 -0800 | [diff] [blame] | 1092 | MYLOGE("kill(%d, SIGQUIT): %s\n", pid, strerror(errno)); |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 1093 | continue; |
| 1094 | } |
| 1095 | |
| 1096 | /* wait for the writable-close notification from inotify */ |
| 1097 | struct pollfd pfd = { ifd, POLLIN, 0 }; |
Felipe Leme | 6188412 | 2016-06-13 09:23:30 -0700 | [diff] [blame^] | 1098 | int ret = poll(&pfd, 1, TRACE_DUMP_TIMEOUT_MS); |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 1099 | if (ret < 0) { |
Felipe Leme | 107a05f | 2016-03-08 15:11:15 -0800 | [diff] [blame] | 1100 | MYLOGE("poll: %s\n", strerror(errno)); |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 1101 | } else if (ret == 0) { |
Felipe Leme | 107a05f | 2016-03-08 15:11:15 -0800 | [diff] [blame] | 1102 | MYLOGE("warning: timed out dumping pid %d\n", pid); |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 1103 | } else { |
| 1104 | struct inotify_event ie; |
| 1105 | read(ifd, &ie, sizeof(ie)); |
| 1106 | } |
Jeff Brown | 1dc94e3 | 2014-09-11 14:15:27 -0700 | [diff] [blame] | 1107 | |
| 1108 | if (lseek(fd, 0, SEEK_END) < 0) { |
Felipe Leme | 107a05f | 2016-03-08 15:11:15 -0800 | [diff] [blame] | 1109 | MYLOGE("lseek: %s\n", strerror(errno)); |
Jeff Brown | 1dc94e3 | 2014-09-11 14:15:27 -0700 | [diff] [blame] | 1110 | } else { |
Christopher Ferris | 31ef855 | 2015-01-14 13:23:30 -0800 | [diff] [blame] | 1111 | dprintf(fd, "[dump dalvik stack %d: %.3fs elapsed]\n", |
Felipe Leme | 78f2c86 | 2015-12-21 09:55:22 -0800 | [diff] [blame] | 1112 | pid, (float)(DurationReporter::nanotime() - start) / NANOS_PER_SEC); |
Jeff Brown | 1dc94e3 | 2014-09-11 14:15:27 -0700 | [diff] [blame] | 1113 | } |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 1114 | } else if (should_dump_native_traces(data)) { |
| 1115 | /* dump native process if appropriate */ |
| 1116 | if (lseek(fd, 0, SEEK_END) < 0) { |
Felipe Leme | 107a05f | 2016-03-08 15:11:15 -0800 | [diff] [blame] | 1117 | MYLOGE("lseek: %s\n", strerror(errno)); |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 1118 | } else { |
Christopher Ferris | 31ef855 | 2015-01-14 13:23:30 -0800 | [diff] [blame] | 1119 | static uint16_t timeout_failures = 0; |
Felipe Leme | 78f2c86 | 2015-12-21 09:55:22 -0800 | [diff] [blame] | 1120 | uint64_t start = DurationReporter::nanotime(); |
Christopher Ferris | 31ef855 | 2015-01-14 13:23:30 -0800 | [diff] [blame] | 1121 | |
| 1122 | /* If 3 backtrace dumps fail in a row, consider debuggerd dead. */ |
| 1123 | if (timeout_failures == 3) { |
| 1124 | dprintf(fd, "too many stack dump failures, skipping...\n"); |
| 1125 | } else if (dump_backtrace_to_file_timeout(pid, fd, 20) == -1) { |
| 1126 | dprintf(fd, "dumping failed, likely due to a timeout\n"); |
| 1127 | timeout_failures++; |
| 1128 | } else { |
| 1129 | timeout_failures = 0; |
| 1130 | } |
| 1131 | dprintf(fd, "[dump native stack %d: %.3fs elapsed]\n", |
Felipe Leme | 78f2c86 | 2015-12-21 09:55:22 -0800 | [diff] [blame] | 1132 | pid, (float)(DurationReporter::nanotime() - start) / NANOS_PER_SEC); |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 1133 | } |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1134 | } |
| 1135 | } |
| 1136 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1137 | if (dalvik_found == 0) { |
Felipe Leme | 107a05f | 2016-03-08 15:11:15 -0800 | [diff] [blame] | 1138 | MYLOGE("Warning: no Dalvik processes found to dump stacks\n"); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1139 | } |
| 1140 | |
| 1141 | static char dump_traces_path[PATH_MAX]; |
| 1142 | strlcpy(dump_traces_path, traces_path, sizeof(dump_traces_path)); |
| 1143 | strlcat(dump_traces_path, ".bugreport", sizeof(dump_traces_path)); |
| 1144 | if (rename(traces_path, dump_traces_path)) { |
Felipe Leme | 107a05f | 2016-03-08 15:11:15 -0800 | [diff] [blame] | 1145 | MYLOGE("rename(%s, %s): %s\n", traces_path, dump_traces_path, strerror(errno)); |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 1146 | goto error_close_ifd; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1147 | } |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 1148 | result = dump_traces_path; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1149 | |
| 1150 | /* replace the saved [ANR] traces.txt file */ |
| 1151 | rename(anr_traces_path, traces_path); |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 1152 | |
| 1153 | error_close_ifd: |
| 1154 | close(ifd); |
| 1155 | error_close_fd: |
| 1156 | close(fd); |
| 1157 | return result; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1158 | } |
| 1159 | |
Sreeram Ramachandran | 2b3bba3 | 2014-07-08 15:40:55 -0700 | [diff] [blame] | 1160 | void dump_route_tables() { |
Felipe Leme | 78f2c86 | 2015-12-21 09:55:22 -0800 | [diff] [blame] | 1161 | DurationReporter duration_reporter("DUMP ROUTE TABLES"); |
Felipe Leme | 93d705b | 2015-11-10 20:10:25 -0800 | [diff] [blame] | 1162 | ON_DRY_RUN_RETURN(); |
Sreeram Ramachandran | 2b3bba3 | 2014-07-08 15:40:55 -0700 | [diff] [blame] | 1163 | const char* const RT_TABLES_PATH = "/data/misc/net/rt_tables"; |
| 1164 | dump_file("RT_TABLES", RT_TABLES_PATH); |
Nick Kralevich | cd67e9f | 2015-03-19 11:30:59 -0700 | [diff] [blame] | 1165 | FILE* fp = fopen(RT_TABLES_PATH, "re"); |
Sreeram Ramachandran | 2b3bba3 | 2014-07-08 15:40:55 -0700 | [diff] [blame] | 1166 | if (!fp) { |
| 1167 | printf("*** %s: %s\n", RT_TABLES_PATH, strerror(errno)); |
| 1168 | return; |
| 1169 | } |
| 1170 | char table[16]; |
| 1171 | // Each line has an integer (the table number), a space, and a string (the table name). We only |
| 1172 | // need the table number. It's a 32-bit unsigned number, so max 10 chars. Skip the table name. |
| 1173 | // Add a fixed max limit so this doesn't go awry. |
| 1174 | for (int i = 0; i < 64 && fscanf(fp, " %10s %*s", table) == 1; ++i) { |
| 1175 | run_command("ROUTE TABLE IPv4", 10, "ip", "-4", "route", "show", "table", table, NULL); |
| 1176 | run_command("ROUTE TABLE IPv6", 10, "ip", "-6", "route", "show", "table", table, NULL); |
| 1177 | } |
| 1178 | fclose(fp); |
| 1179 | } |
Felipe Leme | 71bbfc5 | 2015-11-23 14:14:51 -0800 | [diff] [blame] | 1180 | |
| 1181 | /* overall progress */ |
| 1182 | int progress = 0; |
Felipe Leme | 08b5578 | 2015-12-01 08:40:52 -0800 | [diff] [blame] | 1183 | int do_update_progress = 0; // Set by dumpstate.cpp |
Felipe Leme | ad5f6c4 | 2015-11-30 14:26:46 -0800 | [diff] [blame] | 1184 | int weight_total = WEIGHT_TOTAL; |
Felipe Leme | 71bbfc5 | 2015-11-23 14:14:51 -0800 | [diff] [blame] | 1185 | |
| 1186 | // TODO: make this function thread safe if sections are generated in parallel. |
| 1187 | void update_progress(int delta) { |
| 1188 | if (!do_update_progress) return; |
| 1189 | |
| 1190 | progress += delta; |
| 1191 | |
| 1192 | char key[PROPERTY_KEY_MAX]; |
| 1193 | char value[PROPERTY_VALUE_MAX]; |
Felipe Leme | ad5f6c4 | 2015-11-30 14:26:46 -0800 | [diff] [blame] | 1194 | |
| 1195 | // adjusts max on the fly |
| 1196 | if (progress > weight_total) { |
| 1197 | int new_total = weight_total * 1.2; |
Felipe Leme | 107a05f | 2016-03-08 15:11:15 -0800 | [diff] [blame] | 1198 | MYLOGD("Adjusting total weight from %d to %d\n", weight_total, new_total); |
Felipe Leme | ad5f6c4 | 2015-11-30 14:26:46 -0800 | [diff] [blame] | 1199 | weight_total = new_total; |
Nick Kralevich | f0922cc | 2016-05-14 16:47:44 -0700 | [diff] [blame] | 1200 | snprintf(key, sizeof(key), "dumpstate.%d.max", getpid()); |
| 1201 | snprintf(value, sizeof(value), "%d", weight_total); |
Felipe Leme | ad5f6c4 | 2015-11-30 14:26:46 -0800 | [diff] [blame] | 1202 | int status = property_set(key, value); |
| 1203 | if (status) { |
Felipe Leme | cbce55d | 2016-02-08 09:53:18 -0800 | [diff] [blame] | 1204 | MYLOGE("Could not update max weight by setting system property %s to %s: %d\n", |
Felipe Leme | ad5f6c4 | 2015-11-30 14:26:46 -0800 | [diff] [blame] | 1205 | key, value, status); |
| 1206 | } |
| 1207 | } |
| 1208 | |
Nick Kralevich | f0922cc | 2016-05-14 16:47:44 -0700 | [diff] [blame] | 1209 | snprintf(key, sizeof(key), "dumpstate.%d.progress", getpid()); |
| 1210 | snprintf(value, sizeof(value), "%d", progress); |
Felipe Leme | 71bbfc5 | 2015-11-23 14:14:51 -0800 | [diff] [blame] | 1211 | |
Felipe Leme | 107a05f | 2016-03-08 15:11:15 -0800 | [diff] [blame] | 1212 | if (progress % 100 == 0) { |
| 1213 | // We don't want to spam logcat, so only log multiples of 100. |
| 1214 | MYLOGD("Setting progress (%s): %s/%d\n", key, value, weight_total); |
| 1215 | } else { |
| 1216 | // stderr is ignored on normal invocations, but useful when calling /system/bin/dumpstate |
| 1217 | // directly for debuggging. |
| 1218 | fprintf(stderr, "Setting progress (%s): %s/%d\n", key, value, weight_total); |
| 1219 | } |
Felipe Leme | 71bbfc5 | 2015-11-23 14:14:51 -0800 | [diff] [blame] | 1220 | |
| 1221 | int status = property_set(key, value); |
| 1222 | if (status) { |
Felipe Leme | cbce55d | 2016-02-08 09:53:18 -0800 | [diff] [blame] | 1223 | MYLOGE("Could not update progress by setting system property %s to %s: %d\n", |
Felipe Leme | 71bbfc5 | 2015-11-23 14:14:51 -0800 | [diff] [blame] | 1224 | key, value, status); |
| 1225 | } |
| 1226 | } |
Felipe Leme | e338bf6 | 2015-12-07 14:03:50 -0800 | [diff] [blame] | 1227 | |
Felipe Leme | 3634a1e | 2015-12-09 10:11:47 -0800 | [diff] [blame] | 1228 | void take_screenshot(const std::string& path) { |
Felipe Leme | e338bf6 | 2015-12-07 14:03:50 -0800 | [diff] [blame] | 1229 | const char *args[] = { "/system/bin/screencap", "-p", path.c_str(), NULL }; |
Felipe Leme | 29c3971 | 2016-04-01 10:02:00 -0700 | [diff] [blame] | 1230 | run_command_always(NULL, DONT_DROP_ROOT, REDIRECT_TO_STDERR, 10, args); |
Felipe Leme | e338bf6 | 2015-12-07 14:03:50 -0800 | [diff] [blame] | 1231 | } |
Mark Salyzyn | f55d402 | 2015-12-11 07:32:31 -0800 | [diff] [blame] | 1232 | |
Felipe Leme | 0c80cf0 | 2016-01-05 13:25:34 -0800 | [diff] [blame] | 1233 | void vibrate(FILE* vibrator, int ms) { |
| 1234 | fprintf(vibrator, "%d\n", ms); |
| 1235 | fflush(vibrator); |
| 1236 | } |
| 1237 | |
| 1238 | bool is_dir(const char* pathname) { |
| 1239 | struct stat info; |
| 1240 | if (stat(pathname, &info) == -1) { |
| 1241 | return false; |
| 1242 | } |
| 1243 | return S_ISDIR(info.st_mode); |
| 1244 | } |
| 1245 | |
| 1246 | time_t get_mtime(int fd, time_t default_mtime) { |
| 1247 | struct stat info; |
| 1248 | if (fstat(fd, &info) == -1) { |
| 1249 | return default_mtime; |
| 1250 | } |
| 1251 | return info.st_mtime; |
| 1252 | } |
| 1253 | |
Mark Salyzyn | 8c8130e | 2015-12-09 11:21:28 -0800 | [diff] [blame] | 1254 | void dump_emmc_ecsd(const char *ext_csd_path) { |
Mark Salyzyn | 290f4b9 | 2016-05-16 08:33:59 -0700 | [diff] [blame] | 1255 | // List of interesting offsets |
Mark Salyzyn | 8c8130e | 2015-12-09 11:21:28 -0800 | [diff] [blame] | 1256 | struct hex { |
| 1257 | char str[2]; |
Mark Salyzyn | 8c8130e | 2015-12-09 11:21:28 -0800 | [diff] [blame] | 1258 | }; |
Mark Salyzyn | 290f4b9 | 2016-05-16 08:33:59 -0700 | [diff] [blame] | 1259 | static const size_t EXT_CSD_REV = 192 * sizeof(hex); |
| 1260 | static const size_t EXT_PRE_EOL_INFO = 267 * sizeof(hex); |
| 1261 | static const size_t EXT_DEVICE_LIFE_TIME_EST_TYP_A = 268 * sizeof(hex); |
| 1262 | static const size_t EXT_DEVICE_LIFE_TIME_EST_TYP_B = 269 * sizeof(hex); |
| 1263 | |
| 1264 | std::string buffer; |
| 1265 | if (!android::base::ReadFileToString(ext_csd_path, &buffer)) { |
| 1266 | return; |
| 1267 | } |
Mark Salyzyn | 8c8130e | 2015-12-09 11:21:28 -0800 | [diff] [blame] | 1268 | |
| 1269 | printf("------ %s Extended CSD ------\n", ext_csd_path); |
| 1270 | |
Mark Salyzyn | 290f4b9 | 2016-05-16 08:33:59 -0700 | [diff] [blame] | 1271 | if (buffer.length() < (EXT_CSD_REV + sizeof(hex))) { |
| 1272 | printf("*** %s: truncated content %zu\n\n", ext_csd_path, buffer.length()); |
Mark Salyzyn | 8c8130e | 2015-12-09 11:21:28 -0800 | [diff] [blame] | 1273 | return; |
| 1274 | } |
| 1275 | |
Mark Salyzyn | 290f4b9 | 2016-05-16 08:33:59 -0700 | [diff] [blame] | 1276 | int ext_csd_rev = 0; |
| 1277 | std::string sub = buffer.substr(EXT_CSD_REV, sizeof(hex)); |
| 1278 | if (sscanf(sub.c_str(), "%2x", &ext_csd_rev) != 1) { |
| 1279 | printf("*** %s: EXT_CSD_REV parse error \"%s\"\n\n", |
| 1280 | ext_csd_path, sub.c_str()); |
Mark Salyzyn | 8c8130e | 2015-12-09 11:21:28 -0800 | [diff] [blame] | 1281 | return; |
| 1282 | } |
| 1283 | |
Mark Salyzyn | 290f4b9 | 2016-05-16 08:33:59 -0700 | [diff] [blame] | 1284 | static const char *ver_str[] = { |
| 1285 | "4.0", "4.1", "4.2", "4.3", "Obsolete", "4.41", "4.5", "5.0" |
| 1286 | }; |
Mark Salyzyn | 8c8130e | 2015-12-09 11:21:28 -0800 | [diff] [blame] | 1287 | printf("rev 1.%d (MMC %s)\n", |
| 1288 | ext_csd_rev, |
| 1289 | (ext_csd_rev < (int)(sizeof(ver_str) / sizeof(ver_str[0]))) ? |
| 1290 | ver_str[ext_csd_rev] : |
| 1291 | "Unknown"); |
| 1292 | if (ext_csd_rev < 7) { |
| 1293 | printf("\n"); |
| 1294 | return; |
| 1295 | } |
| 1296 | |
Mark Salyzyn | 290f4b9 | 2016-05-16 08:33:59 -0700 | [diff] [blame] | 1297 | if (buffer.length() < (EXT_PRE_EOL_INFO + sizeof(hex))) { |
| 1298 | printf("*** %s: truncated content %zu\n\n", ext_csd_path, buffer.length()); |
Mark Salyzyn | 8c8130e | 2015-12-09 11:21:28 -0800 | [diff] [blame] | 1299 | return; |
| 1300 | } |
| 1301 | |
Mark Salyzyn | 290f4b9 | 2016-05-16 08:33:59 -0700 | [diff] [blame] | 1302 | int ext_pre_eol_info = 0; |
| 1303 | sub = buffer.substr(EXT_PRE_EOL_INFO, sizeof(hex)); |
| 1304 | if (sscanf(sub.c_str(), "%2x", &ext_pre_eol_info) != 1) { |
| 1305 | printf("*** %s: PRE_EOL_INFO parse error \"%s\"\n\n", |
| 1306 | ext_csd_path, sub.c_str()); |
Mark Salyzyn | 8c8130e | 2015-12-09 11:21:28 -0800 | [diff] [blame] | 1307 | return; |
| 1308 | } |
Mark Salyzyn | 290f4b9 | 2016-05-16 08:33:59 -0700 | [diff] [blame] | 1309 | |
| 1310 | static const char *eol_str[] = { |
| 1311 | "Undefined", |
| 1312 | "Normal", |
| 1313 | "Warning (consumed 80% of reserve)", |
| 1314 | "Urgent (consumed 90% of reserve)" |
| 1315 | }; |
Mark Salyzyn | 8c8130e | 2015-12-09 11:21:28 -0800 | [diff] [blame] | 1316 | printf("PRE_EOL_INFO %d (MMC %s)\n", |
| 1317 | ext_pre_eol_info, |
| 1318 | eol_str[(ext_pre_eol_info < (int) |
| 1319 | (sizeof(eol_str) / sizeof(eol_str[0]))) ? |
| 1320 | ext_pre_eol_info : 0]); |
| 1321 | |
| 1322 | for (size_t lifetime = EXT_DEVICE_LIFE_TIME_EST_TYP_A; |
| 1323 | lifetime <= EXT_DEVICE_LIFE_TIME_EST_TYP_B; |
Mark Salyzyn | 290f4b9 | 2016-05-16 08:33:59 -0700 | [diff] [blame] | 1324 | lifetime += sizeof(hex)) { |
Mark Salyzyn | 8c8130e | 2015-12-09 11:21:28 -0800 | [diff] [blame] | 1325 | int ext_device_life_time_est; |
| 1326 | static const char *est_str[] = { |
| 1327 | "Undefined", |
| 1328 | "0-10% of device lifetime used", |
| 1329 | "10-20% of device lifetime used", |
| 1330 | "20-30% of device lifetime used", |
| 1331 | "30-40% of device lifetime used", |
| 1332 | "40-50% of device lifetime used", |
| 1333 | "50-60% of device lifetime used", |
| 1334 | "60-70% of device lifetime used", |
| 1335 | "70-80% of device lifetime used", |
| 1336 | "80-90% of device lifetime used", |
| 1337 | "90-100% of device lifetime used", |
| 1338 | "Exceeded the maximum estimated device lifetime", |
| 1339 | }; |
| 1340 | |
Mark Salyzyn | 290f4b9 | 2016-05-16 08:33:59 -0700 | [diff] [blame] | 1341 | if (buffer.length() < (lifetime + sizeof(hex))) { |
| 1342 | printf("*** %s: truncated content %zu\n", ext_csd_path, buffer.length()); |
Mark Salyzyn | 8c8130e | 2015-12-09 11:21:28 -0800 | [diff] [blame] | 1343 | break; |
| 1344 | } |
| 1345 | |
| 1346 | ext_device_life_time_est = 0; |
Mark Salyzyn | 290f4b9 | 2016-05-16 08:33:59 -0700 | [diff] [blame] | 1347 | sub = buffer.substr(lifetime, sizeof(hex)); |
| 1348 | if (sscanf(sub.c_str(), "%2x", &ext_device_life_time_est) != 1) { |
| 1349 | printf("*** %s: DEVICE_LIFE_TIME_EST_TYP_%c parse error \"%s\"\n", |
Mark Salyzyn | 8c8130e | 2015-12-09 11:21:28 -0800 | [diff] [blame] | 1350 | ext_csd_path, |
Mark Salyzyn | 290f4b9 | 2016-05-16 08:33:59 -0700 | [diff] [blame] | 1351 | (unsigned)((lifetime - EXT_DEVICE_LIFE_TIME_EST_TYP_A) / |
| 1352 | sizeof(hex)) + 'A', |
| 1353 | sub.c_str()); |
Mark Salyzyn | 8c8130e | 2015-12-09 11:21:28 -0800 | [diff] [blame] | 1354 | continue; |
| 1355 | } |
| 1356 | printf("DEVICE_LIFE_TIME_EST_TYP_%c %d (MMC %s)\n", |
Mark Salyzyn | 290f4b9 | 2016-05-16 08:33:59 -0700 | [diff] [blame] | 1357 | (unsigned)((lifetime - EXT_DEVICE_LIFE_TIME_EST_TYP_A) / |
| 1358 | sizeof(hex)) + 'A', |
Mark Salyzyn | 8c8130e | 2015-12-09 11:21:28 -0800 | [diff] [blame] | 1359 | ext_device_life_time_est, |
| 1360 | est_str[(ext_device_life_time_est < (int) |
| 1361 | (sizeof(est_str) / sizeof(est_str[0]))) ? |
| 1362 | ext_device_life_time_est : 0]); |
| 1363 | } |
| 1364 | |
| 1365 | printf("\n"); |
| 1366 | } |
Felipe Leme | 88c7933 | 2016-02-22 11:06:49 -0800 | [diff] [blame] | 1367 | |
Felipe Leme | a34efb7 | 2016-03-11 09:33:32 -0800 | [diff] [blame] | 1368 | // TODO: refactor all those commands that convert args |
| 1369 | void format_args(int argc, const char *argv[], std::string *args) { |
| 1370 | LOG_ALWAYS_FATAL_IF(args == nullptr); |
Felipe Leme | 88c7933 | 2016-02-22 11:06:49 -0800 | [diff] [blame] | 1371 | for (int i = 0; i < argc; i++) { |
Felipe Leme | a34efb7 | 2016-03-11 09:33:32 -0800 | [diff] [blame] | 1372 | args->append(argv[i]); |
| 1373 | if (i < argc -1) { |
| 1374 | args->append(" "); |
| 1375 | } |
Felipe Leme | 88c7933 | 2016-02-22 11:06:49 -0800 | [diff] [blame] | 1376 | } |
Felipe Leme | a34efb7 | 2016-03-11 09:33:32 -0800 | [diff] [blame] | 1377 | } |
| 1378 | void format_args(const char* command, const char *args[], std::string *string) { |
| 1379 | LOG_ALWAYS_FATAL_IF(args == nullptr || command == nullptr); |
| 1380 | string->append(command); |
| 1381 | if (args[0] == nullptr) return; |
| 1382 | string->append(" "); |
| 1383 | |
| 1384 | for (int arg = 1; arg <= 1000; ++arg) { |
| 1385 | if (args[arg] == nullptr) return; |
| 1386 | string->append(args[arg]); |
| 1387 | if (args[arg+1] != nullptr) { |
| 1388 | string->append(" "); |
| 1389 | } |
| 1390 | } |
Felipe Leme | ea160d1 | 2016-03-24 11:29:44 -0700 | [diff] [blame] | 1391 | // TODO: not really working: if NULL is missing, it will crash dumpstate. |
Felipe Leme | a34efb7 | 2016-03-11 09:33:32 -0800 | [diff] [blame] | 1392 | MYLOGE("internal error: missing NULL entry on %s", string->c_str()); |
Felipe Leme | 88c7933 | 2016-02-22 11:06:49 -0800 | [diff] [blame] | 1393 | } |