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