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