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