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