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 | |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 17 | #define LOG_TAG "dumpstate" |
| 18 | |
| 19 | #include "dumpstate.h" |
| 20 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 21 | #include <dirent.h> |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 22 | #include <fcntl.h> |
Felipe Leme | e184f66 | 2016-10-27 10:04:47 -0700 | [diff] [blame] | 23 | #include <libgen.h> |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 24 | #include <math.h> |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 25 | #include <poll.h> |
| 26 | #include <signal.h> |
| 27 | #include <stdarg.h> |
| 28 | #include <stdio.h> |
| 29 | #include <stdlib.h> |
| 30 | #include <string.h> |
Felipe Leme | cf6a8b4 | 2016-03-11 10:38:19 -0800 | [diff] [blame] | 31 | #include <sys/capability.h> |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 32 | #include <sys/inotify.h> |
Felipe Leme | e184f66 | 2016-10-27 10:04:47 -0700 | [diff] [blame] | 33 | #include <sys/klog.h> |
Mark Salyzyn | a5e161b | 2016-09-29 08:08:05 -0700 | [diff] [blame] | 34 | #include <sys/prctl.h> |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 35 | #include <sys/stat.h> |
| 36 | #include <sys/time.h> |
| 37 | #include <sys/wait.h> |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 38 | #include <time.h> |
| 39 | #include <unistd.h> |
Mark Salyzyn | 261a733 | 2016-05-24 12:38:40 -0700 | [diff] [blame] | 40 | |
Mark Salyzyn | a5e161b | 2016-09-29 08:08:05 -0700 | [diff] [blame] | 41 | #include <string> |
Felipe Leme | 36b3f6f | 2015-11-19 15:41:04 -0800 | [diff] [blame] | 42 | #include <vector> |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 43 | |
Mark Salyzyn | 290f4b9 | 2016-05-16 08:33:59 -0700 | [diff] [blame] | 44 | #include <android-base/file.h> |
Felipe Leme | 96c2bbb | 2016-09-26 09:21:21 -0700 | [diff] [blame] | 45 | #include <android-base/properties.h> |
Felipe Leme | 2b9b06c | 2016-10-14 09:13:06 -0700 | [diff] [blame] | 46 | #include <android-base/stringprintf.h> |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 47 | #include <android-base/strings.h> |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 48 | #include <cutils/debugger.h> |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 49 | #include <cutils/properties.h> |
| 50 | #include <cutils/sockets.h> |
Mark Salyzyn | 4eb1382 | 2017-01-12 13:57:51 -0800 | [diff] [blame] | 51 | #include <log/log.h> |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 52 | #include <private/android_filesystem_config.h> |
| 53 | |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 54 | #include "DumpstateInternal.h" |
Jeff Brown | 1dc94e3 | 2014-09-11 14:15:27 -0700 | [diff] [blame] | 55 | |
Felipe Leme | 6188412 | 2016-06-13 09:23:30 -0700 | [diff] [blame] | 56 | static const int TRACE_DUMP_TIMEOUT_MS = 10000; // 10 seconds |
| 57 | |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 58 | /* Most simple commands have 10 as timeout, so 5 is a good estimate */ |
| 59 | static const int32_t WEIGHT_FILE = 5; |
| 60 | |
Felipe Leme | e844a9d | 2016-09-21 15:01:39 -0700 | [diff] [blame] | 61 | // TODO: temporary variables and functions used during C++ refactoring |
| 62 | static Dumpstate& ds = Dumpstate::GetInstance(); |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 63 | static int RunCommand(const std::string& title, const std::vector<std::string>& full_command, |
Felipe Leme | 678727a | 2016-09-21 17:22:11 -0700 | [diff] [blame] | 64 | const CommandOptions& options = CommandOptions::DEFAULT) { |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 65 | return ds.RunCommand(title, full_command, options); |
Felipe Leme | 678727a | 2016-09-21 17:22:11 -0700 | [diff] [blame] | 66 | } |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 67 | bool Dumpstate::IsUserBuild() { |
| 68 | return PropertiesHelper::IsUserBuild(); |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 69 | } |
Felipe Leme | e844a9d | 2016-09-21 15:01:39 -0700 | [diff] [blame] | 70 | |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 71 | /* list of native processes to include in the native dumps */ |
Andy Hung | 5c04e74 | 2016-04-13 19:35:34 -0700 | [diff] [blame] | 72 | // This matches the /proc/pid/exe link instead of /proc/pid/cmdline. |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 73 | static const char* native_processes_to_dump[] = { |
Andy Hung | 9609bbd | 2015-12-15 12:42:50 -0800 | [diff] [blame] | 74 | "/system/bin/audioserver", |
Chien-Yu Chen | f5248da | 2016-01-28 14:23:03 -0800 | [diff] [blame] | 75 | "/system/bin/cameraserver", |
James Dong | 1fc4f80 | 2012-09-10 16:08:48 -0700 | [diff] [blame] | 76 | "/system/bin/drmserver", |
Andy Hung | 5c04e74 | 2016-04-13 19:35:34 -0700 | [diff] [blame] | 77 | "/system/bin/mediacodec", // media.codec |
| 78 | "/system/bin/mediadrmserver", |
| 79 | "/system/bin/mediaextractor", // media.extractor |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 80 | "/system/bin/mediaserver", |
| 81 | "/system/bin/sdcard", |
| 82 | "/system/bin/surfaceflinger", |
keunyoung | d907b32 | 2015-10-16 15:21:43 -0700 | [diff] [blame] | 83 | "/system/bin/vehicle_network_service", |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 84 | NULL, |
| 85 | }; |
| 86 | |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 87 | // Reasonable value for max stats. |
| 88 | static const int STATS_MAX_N_RUNS = 1000; |
| 89 | static const long STATS_MAX_AVERAGE = 100000; |
| 90 | |
Felipe Leme | bda15a0 | 2016-11-16 17:48:25 -0800 | [diff] [blame] | 91 | CommandOptions Dumpstate::DEFAULT_DUMPSYS = CommandOptions::WithTimeout(30).Build(); |
Felipe Leme | 30dbfa1 | 2016-09-02 12:43:26 -0700 | [diff] [blame] | 92 | |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 93 | Dumpstate::Dumpstate(const std::string& version) |
| 94 | : pid_(getpid()), version_(version), now_(time(nullptr)) { |
Felipe Leme | e844a9d | 2016-09-21 15:01:39 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | Dumpstate& Dumpstate::GetInstance() { |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 98 | static Dumpstate singleton_(android::base::GetProperty("dumpstate.version", VERSION_CURRENT)); |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 99 | return singleton_; |
Felipe Leme | e844a9d | 2016-09-21 15:01:39 -0700 | [diff] [blame] | 100 | } |
| 101 | |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 102 | DurationReporter::DurationReporter(const std::string& title, bool log_only) |
| 103 | : title_(title), log_only_(log_only) { |
Felipe Leme | 678727a | 2016-09-21 17:22:11 -0700 | [diff] [blame] | 104 | if (!title_.empty()) { |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 105 | started_ = Nanotime(); |
Felipe Leme | 78f2c86 | 2015-12-21 09:55:22 -0800 | [diff] [blame] | 106 | } |
| 107 | } |
| 108 | |
| 109 | DurationReporter::~DurationReporter() { |
Felipe Leme | 678727a | 2016-09-21 17:22:11 -0700 | [diff] [blame] | 110 | if (!title_.empty()) { |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 111 | uint64_t elapsed = Nanotime() - started_; |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 112 | if (log_only_) { |
Felipe Leme | 678727a | 2016-09-21 17:22:11 -0700 | [diff] [blame] | 113 | MYLOGD("Duration of '%s': %.3fs\n", title_.c_str(), (float)elapsed / NANOS_PER_SEC); |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 114 | } else { |
| 115 | // Use "Yoda grammar" to make it easier to grep|sort sections. |
Felipe Leme | d8b94e5 | 2016-12-08 10:21:44 -0800 | [diff] [blame] | 116 | printf("------ %.3fs was the duration of '%s' ------\n", (float)elapsed / NANOS_PER_SEC, |
| 117 | title_.c_str()); |
Felipe Leme | 608385d | 2016-02-01 10:35:38 -0800 | [diff] [blame] | 118 | } |
Felipe Leme | 78f2c86 | 2015-12-21 09:55:22 -0800 | [diff] [blame] | 119 | } |
| 120 | } |
| 121 | |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 122 | const int32_t Progress::kDefaultMax = 5000; |
| 123 | |
| 124 | Progress::Progress(const std::string& path) : Progress(Progress::kDefaultMax, 1.1, path) { |
| 125 | } |
| 126 | |
| 127 | Progress::Progress(int32_t initial_max, int32_t progress, float growth_factor) |
| 128 | : Progress(initial_max, growth_factor, "") { |
| 129 | progress_ = progress; |
| 130 | } |
| 131 | |
| 132 | Progress::Progress(int32_t initial_max, float growth_factor, const std::string& path) |
| 133 | : initial_max_(initial_max), |
| 134 | progress_(0), |
| 135 | max_(initial_max), |
| 136 | growth_factor_(growth_factor), |
| 137 | n_runs_(0), |
| 138 | average_max_(0), |
| 139 | path_(path) { |
| 140 | if (!path_.empty()) { |
| 141 | Load(); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | void Progress::Load() { |
| 146 | MYLOGD("Loading stats from %s\n", path_.c_str()); |
| 147 | std::string content; |
| 148 | if (!android::base::ReadFileToString(path_, &content)) { |
| 149 | MYLOGI("Could not read stats from %s; using max of %d\n", path_.c_str(), max_); |
| 150 | return; |
| 151 | } |
| 152 | if (content.empty()) { |
| 153 | MYLOGE("No stats (empty file) on %s; using max of %d\n", path_.c_str(), max_); |
| 154 | return; |
| 155 | } |
| 156 | std::vector<std::string> lines = android::base::Split(content, "\n"); |
| 157 | |
| 158 | if (lines.size() < 1) { |
| 159 | MYLOGE("Invalid stats on file %s: not enough lines (%d). Using max of %d\n", path_.c_str(), |
| 160 | (int)lines.size(), max_); |
| 161 | return; |
| 162 | } |
| 163 | char* ptr; |
| 164 | n_runs_ = strtol(lines[0].c_str(), &ptr, 10); |
| 165 | average_max_ = strtol(ptr, nullptr, 10); |
| 166 | if (n_runs_ <= 0 || average_max_ <= 0 || n_runs_ > STATS_MAX_N_RUNS || |
| 167 | average_max_ > STATS_MAX_AVERAGE) { |
| 168 | MYLOGE("Invalid stats line on file %s: %s\n", path_.c_str(), lines[0].c_str()); |
| 169 | initial_max_ = Progress::kDefaultMax; |
| 170 | } else { |
| 171 | initial_max_ = average_max_; |
| 172 | } |
| 173 | max_ = initial_max_; |
| 174 | |
| 175 | MYLOGI("Average max progress: %d in %d runs; estimated max: %d\n", average_max_, n_runs_, max_); |
| 176 | } |
| 177 | |
| 178 | void Progress::Save() { |
| 179 | int32_t total = n_runs_ * average_max_ + progress_; |
| 180 | int32_t runs = n_runs_ + 1; |
| 181 | int32_t average = floor(((float)total) / runs); |
| 182 | MYLOGI("Saving stats (total=%d, runs=%d, average=%d) on %s\n", total, runs, average, |
| 183 | path_.c_str()); |
| 184 | if (path_.empty()) { |
| 185 | return; |
| 186 | } |
| 187 | |
| 188 | std::string content = android::base::StringPrintf("%d %d\n", runs, average); |
| 189 | if (!android::base::WriteStringToFile(content, path_)) { |
| 190 | MYLOGE("Could not save stats on %s\n", path_.c_str()); |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | int32_t Progress::Get() const { |
| 195 | return progress_; |
| 196 | } |
| 197 | |
| 198 | bool Progress::Inc(int32_t delta) { |
| 199 | bool changed = false; |
| 200 | if (delta >= 0) { |
| 201 | progress_ += delta; |
| 202 | if (progress_ > max_) { |
| 203 | int32_t old_max = max_; |
| 204 | max_ = floor((float)progress_ * growth_factor_); |
| 205 | MYLOGD("Adjusting max progress from %d to %d\n", old_max, max_); |
| 206 | changed = true; |
| 207 | } |
| 208 | } |
| 209 | return changed; |
| 210 | } |
| 211 | |
| 212 | int32_t Progress::GetMax() const { |
| 213 | return max_; |
| 214 | } |
| 215 | |
| 216 | int32_t Progress::GetInitialMax() const { |
| 217 | return initial_max_; |
| 218 | } |
| 219 | |
| 220 | void Progress::Dump(int fd, const std::string& prefix) const { |
| 221 | const char* pr = prefix.c_str(); |
| 222 | dprintf(fd, "%sprogress: %d\n", pr, progress_); |
| 223 | dprintf(fd, "%smax: %d\n", pr, max_); |
| 224 | dprintf(fd, "%sinitial_max: %d\n", pr, initial_max_); |
| 225 | dprintf(fd, "%sgrowth_factor: %0.2f\n", pr, growth_factor_); |
| 226 | dprintf(fd, "%spath: %s\n", pr, path_.c_str()); |
| 227 | dprintf(fd, "%sn_runs: %d\n", pr, n_runs_); |
| 228 | dprintf(fd, "%saverage_max: %d\n", pr, average_max_); |
| 229 | } |
| 230 | |
Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 231 | bool Dumpstate::IsZipping() const { |
| 232 | return zip_writer_ != nullptr; |
| 233 | } |
| 234 | |
Felipe Leme | 2b9b06c | 2016-10-14 09:13:06 -0700 | [diff] [blame] | 235 | std::string Dumpstate::GetPath(const std::string& suffix) const { |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 236 | return android::base::StringPrintf("%s/%s-%s%s", bugreport_dir_.c_str(), base_name_.c_str(), |
Felipe Leme | 2b9b06c | 2016-10-14 09:13:06 -0700 | [diff] [blame] | 237 | name_.c_str(), suffix.c_str()); |
Felipe Leme | bbaf3c1 | 2016-10-11 14:32:25 -0700 | [diff] [blame] | 238 | } |
| 239 | |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 240 | void Dumpstate::SetProgress(std::unique_ptr<Progress> progress) { |
| 241 | progress_ = std::move(progress); |
| 242 | } |
| 243 | |
John Spurlock | 5ecd4be | 2014-01-29 14:14:40 -0500 | [diff] [blame] | 244 | void for_each_userid(void (*func)(int), const char *header) { |
Felipe Leme | 8f00ed0 | 2016-12-07 17:42:44 -0800 | [diff] [blame] | 245 | std::string title = header == nullptr ? "for_each_userid" : android::base::StringPrintf( |
| 246 | "for_each_userid(%s)", header); |
| 247 | DurationReporter duration_reporter(title); |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 248 | if (PropertiesHelper::IsDryRun()) return; |
Felipe Leme | d402e7d | 2016-08-03 09:22:27 -0700 | [diff] [blame] | 249 | |
John Spurlock | 5ecd4be | 2014-01-29 14:14:40 -0500 | [diff] [blame] | 250 | DIR *d; |
| 251 | struct dirent *de; |
| 252 | |
Felipe Leme | d8b94e5 | 2016-12-08 10:21:44 -0800 | [diff] [blame] | 253 | if (header) printf("\n------ %s ------\n", header); |
John Spurlock | 5ecd4be | 2014-01-29 14:14:40 -0500 | [diff] [blame] | 254 | func(0); |
| 255 | |
| 256 | if (!(d = opendir("/data/system/users"))) { |
Felipe Leme | d8b94e5 | 2016-12-08 10:21:44 -0800 | [diff] [blame] | 257 | printf("Failed to open /data/system/users (%s)\n", strerror(errno)); |
John Spurlock | 5ecd4be | 2014-01-29 14:14:40 -0500 | [diff] [blame] | 258 | return; |
| 259 | } |
| 260 | |
| 261 | while ((de = readdir(d))) { |
| 262 | int userid; |
| 263 | if (de->d_type != DT_DIR || !(userid = atoi(de->d_name))) { |
| 264 | continue; |
| 265 | } |
| 266 | func(userid); |
| 267 | } |
| 268 | |
| 269 | closedir(d); |
| 270 | } |
| 271 | |
Colin Cross | 0c22e8b | 2012-11-02 15:46:56 -0700 | [diff] [blame] | 272 | 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] | 273 | DIR *d; |
| 274 | struct dirent *de; |
| 275 | |
| 276 | if (!(d = opendir("/proc"))) { |
Felipe Leme | d8b94e5 | 2016-12-08 10:21:44 -0800 | [diff] [blame] | 277 | printf("Failed to open /proc (%s)\n", strerror(errno)); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 278 | return; |
| 279 | } |
| 280 | |
Felipe Leme | d8b94e5 | 2016-12-08 10:21:44 -0800 | [diff] [blame] | 281 | if (header) printf("\n------ %s ------\n", header); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 282 | while ((de = readdir(d))) { |
| 283 | int pid; |
| 284 | int fd; |
| 285 | char cmdpath[255]; |
| 286 | char cmdline[255]; |
| 287 | |
| 288 | if (!(pid = atoi(de->d_name))) { |
| 289 | continue; |
| 290 | } |
| 291 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 292 | memset(cmdline, 0, sizeof(cmdline)); |
Mark Salyzyn | 0751efa | 2016-02-05 15:33:17 -0800 | [diff] [blame] | 293 | |
| 294 | snprintf(cmdpath, sizeof(cmdpath), "/proc/%d/cmdline", pid); |
| 295 | if ((fd = TEMP_FAILURE_RETRY(open(cmdpath, O_RDONLY | O_CLOEXEC))) >= 0) { |
| 296 | TEMP_FAILURE_RETRY(read(fd, cmdline, sizeof(cmdline) - 2)); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 297 | close(fd); |
Mark Salyzyn | 0751efa | 2016-02-05 15:33:17 -0800 | [diff] [blame] | 298 | if (cmdline[0]) { |
| 299 | helper(pid, cmdline, arg); |
| 300 | continue; |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | // if no cmdline, a kernel thread has comm |
| 305 | snprintf(cmdpath, sizeof(cmdpath), "/proc/%d/comm", pid); |
| 306 | if ((fd = TEMP_FAILURE_RETRY(open(cmdpath, O_RDONLY | O_CLOEXEC))) >= 0) { |
| 307 | TEMP_FAILURE_RETRY(read(fd, cmdline + 1, sizeof(cmdline) - 4)); |
| 308 | close(fd); |
| 309 | if (cmdline[1]) { |
| 310 | cmdline[0] = '['; |
| 311 | size_t len = strcspn(cmdline, "\f\b\r\n"); |
| 312 | cmdline[len] = ']'; |
| 313 | cmdline[len+1] = '\0'; |
| 314 | } |
| 315 | } |
| 316 | if (!cmdline[0]) { |
| 317 | strcpy(cmdline, "N/A"); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 318 | } |
Colin Cross | 0c22e8b | 2012-11-02 15:46:56 -0700 | [diff] [blame] | 319 | helper(pid, cmdline, arg); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | closedir(d); |
| 323 | } |
| 324 | |
Colin Cross | 0c22e8b | 2012-11-02 15:46:56 -0700 | [diff] [blame] | 325 | 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] | 326 | for_each_pid_func *func = (for_each_pid_func*) arg; |
Colin Cross | 0c22e8b | 2012-11-02 15:46:56 -0700 | [diff] [blame] | 327 | func(pid, cmdline); |
| 328 | } |
| 329 | |
| 330 | void for_each_pid(for_each_pid_func func, const char *header) { |
Felipe Leme | 8f00ed0 | 2016-12-07 17:42:44 -0800 | [diff] [blame] | 331 | std::string title = header == nullptr ? "for_each_pid" |
| 332 | : android::base::StringPrintf("for_each_pid(%s)", header); |
| 333 | DurationReporter duration_reporter(title); |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 334 | if (PropertiesHelper::IsDryRun()) return; |
Felipe Leme | d402e7d | 2016-08-03 09:22:27 -0700 | [diff] [blame] | 335 | |
Felipe Leme | 515eb0d | 2015-12-14 15:09:56 -0800 | [diff] [blame] | 336 | __for_each_pid(for_each_pid_helper, header, (void *) func); |
Colin Cross | 0c22e8b | 2012-11-02 15:46:56 -0700 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | static void for_each_tid_helper(int pid, const char *cmdline, void *arg) { |
| 340 | DIR *d; |
| 341 | struct dirent *de; |
| 342 | char taskpath[255]; |
Felipe Leme | 8620bb4 | 2015-11-10 11:04:45 -0800 | [diff] [blame] | 343 | for_each_tid_func *func = (for_each_tid_func *) arg; |
Colin Cross | 0c22e8b | 2012-11-02 15:46:56 -0700 | [diff] [blame] | 344 | |
Nick Kralevich | f0922cc | 2016-05-14 16:47:44 -0700 | [diff] [blame] | 345 | snprintf(taskpath, sizeof(taskpath), "/proc/%d/task", pid); |
Colin Cross | 0c22e8b | 2012-11-02 15:46:56 -0700 | [diff] [blame] | 346 | |
| 347 | if (!(d = opendir(taskpath))) { |
Felipe Leme | d8b94e5 | 2016-12-08 10:21:44 -0800 | [diff] [blame] | 348 | printf("Failed to open %s (%s)\n", taskpath, strerror(errno)); |
Colin Cross | 0c22e8b | 2012-11-02 15:46:56 -0700 | [diff] [blame] | 349 | return; |
| 350 | } |
| 351 | |
| 352 | func(pid, pid, cmdline); |
| 353 | |
| 354 | while ((de = readdir(d))) { |
| 355 | int tid; |
| 356 | int fd; |
| 357 | char commpath[255]; |
| 358 | char comm[255]; |
| 359 | |
| 360 | if (!(tid = atoi(de->d_name))) { |
| 361 | continue; |
| 362 | } |
| 363 | |
| 364 | if (tid == pid) |
| 365 | continue; |
| 366 | |
Nick Kralevich | f0922cc | 2016-05-14 16:47:44 -0700 | [diff] [blame] | 367 | snprintf(commpath, sizeof(commpath), "/proc/%d/comm", tid); |
Colin Cross | 1493a39 | 2012-11-07 11:25:31 -0800 | [diff] [blame] | 368 | memset(comm, 0, sizeof(comm)); |
Nick Kralevich | cd67e9f | 2015-03-19 11:30:59 -0700 | [diff] [blame] | 369 | if ((fd = TEMP_FAILURE_RETRY(open(commpath, O_RDONLY | O_CLOEXEC))) < 0) { |
Colin Cross | 0c22e8b | 2012-11-02 15:46:56 -0700 | [diff] [blame] | 370 | strcpy(comm, "N/A"); |
| 371 | } else { |
| 372 | char *c; |
Mark Salyzyn | 0751efa | 2016-02-05 15:33:17 -0800 | [diff] [blame] | 373 | TEMP_FAILURE_RETRY(read(fd, comm, sizeof(comm) - 2)); |
Colin Cross | 0c22e8b | 2012-11-02 15:46:56 -0700 | [diff] [blame] | 374 | close(fd); |
| 375 | |
| 376 | c = strrchr(comm, '\n'); |
| 377 | if (c) { |
| 378 | *c = '\0'; |
| 379 | } |
| 380 | } |
| 381 | func(pid, tid, comm); |
| 382 | } |
| 383 | |
| 384 | closedir(d); |
| 385 | } |
| 386 | |
| 387 | void for_each_tid(for_each_tid_func func, const char *header) { |
Felipe Leme | 8f00ed0 | 2016-12-07 17:42:44 -0800 | [diff] [blame] | 388 | std::string title = header == nullptr ? "for_each_tid" |
| 389 | : android::base::StringPrintf("for_each_tid(%s)", header); |
| 390 | DurationReporter duration_reporter(title); |
Felipe Leme | d8b94e5 | 2016-12-08 10:21:44 -0800 | [diff] [blame] | 391 | |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 392 | if (PropertiesHelper::IsDryRun()) return; |
Felipe Leme | d402e7d | 2016-08-03 09:22:27 -0700 | [diff] [blame] | 393 | |
Felipe Leme | 8620bb4 | 2015-11-10 11:04:45 -0800 | [diff] [blame] | 394 | __for_each_pid(for_each_tid_helper, header, (void *) func); |
Colin Cross | 0c22e8b | 2012-11-02 15:46:56 -0700 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | void show_wchan(int pid, int tid, const char *name) { |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 398 | if (PropertiesHelper::IsDryRun()) return; |
Felipe Leme | d402e7d | 2016-08-03 09:22:27 -0700 | [diff] [blame] | 399 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 400 | char path[255]; |
| 401 | char buffer[255]; |
Mark Salyzyn | 0751efa | 2016-02-05 15:33:17 -0800 | [diff] [blame] | 402 | int fd, ret, save_errno; |
Colin Cross | 0c22e8b | 2012-11-02 15:46:56 -0700 | [diff] [blame] | 403 | char name_buffer[255]; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 404 | |
| 405 | memset(buffer, 0, sizeof(buffer)); |
| 406 | |
Nick Kralevich | f0922cc | 2016-05-14 16:47:44 -0700 | [diff] [blame] | 407 | snprintf(path, sizeof(path), "/proc/%d/wchan", tid); |
Nick Kralevich | cd67e9f | 2015-03-19 11:30:59 -0700 | [diff] [blame] | 408 | if ((fd = TEMP_FAILURE_RETRY(open(path, O_RDONLY | O_CLOEXEC))) < 0) { |
Felipe Leme | d8b94e5 | 2016-12-08 10:21:44 -0800 | [diff] [blame] | 409 | printf("Failed to open '%s' (%s)\n", path, strerror(errno)); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 410 | return; |
| 411 | } |
| 412 | |
Mark Salyzyn | 0751efa | 2016-02-05 15:33:17 -0800 | [diff] [blame] | 413 | ret = TEMP_FAILURE_RETRY(read(fd, buffer, sizeof(buffer))); |
| 414 | save_errno = errno; |
| 415 | close(fd); |
| 416 | |
| 417 | if (ret < 0) { |
Felipe Leme | d8b94e5 | 2016-12-08 10:21:44 -0800 | [diff] [blame] | 418 | printf("Failed to read '%s' (%s)\n", path, strerror(save_errno)); |
Mark Salyzyn | 0751efa | 2016-02-05 15:33:17 -0800 | [diff] [blame] | 419 | return; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 420 | } |
| 421 | |
Colin Cross | 0c22e8b | 2012-11-02 15:46:56 -0700 | [diff] [blame] | 422 | snprintf(name_buffer, sizeof(name_buffer), "%*s%s", |
| 423 | pid == tid ? 0 : 3, "", name); |
| 424 | |
Felipe Leme | d8b94e5 | 2016-12-08 10:21:44 -0800 | [diff] [blame] | 425 | printf("%-7d %-32s %s\n", tid, name_buffer, buffer); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 426 | |
Mark Salyzyn | 0751efa | 2016-02-05 15:33:17 -0800 | [diff] [blame] | 427 | return; |
| 428 | } |
| 429 | |
| 430 | // print time in centiseconds |
| 431 | static void snprcent(char *buffer, size_t len, size_t spc, |
| 432 | unsigned long long time) { |
| 433 | static long hz; // cache discovered hz |
| 434 | |
| 435 | if (hz <= 0) { |
| 436 | hz = sysconf(_SC_CLK_TCK); |
| 437 | if (hz <= 0) { |
| 438 | hz = 1000; |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | // convert to centiseconds |
| 443 | time = (time * 100 + (hz / 2)) / hz; |
| 444 | |
| 445 | char str[16]; |
| 446 | |
| 447 | snprintf(str, sizeof(str), " %llu.%02u", |
| 448 | time / 100, (unsigned)(time % 100)); |
| 449 | size_t offset = strlen(buffer); |
| 450 | snprintf(buffer + offset, (len > offset) ? len - offset : 0, |
| 451 | "%*s", (spc > offset) ? (int)(spc - offset) : 0, str); |
| 452 | } |
| 453 | |
| 454 | // print permille as a percent |
| 455 | static void snprdec(char *buffer, size_t len, size_t spc, unsigned permille) { |
| 456 | char str[16]; |
| 457 | |
| 458 | snprintf(str, sizeof(str), " %u.%u%%", permille / 10, permille % 10); |
| 459 | size_t offset = strlen(buffer); |
| 460 | snprintf(buffer + offset, (len > offset) ? len - offset : 0, |
| 461 | "%*s", (spc > offset) ? (int)(spc - offset) : 0, str); |
| 462 | } |
| 463 | |
| 464 | void show_showtime(int pid, const char *name) { |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 465 | if (PropertiesHelper::IsDryRun()) return; |
Felipe Leme | d402e7d | 2016-08-03 09:22:27 -0700 | [diff] [blame] | 466 | |
Mark Salyzyn | 0751efa | 2016-02-05 15:33:17 -0800 | [diff] [blame] | 467 | char path[255]; |
| 468 | char buffer[1023]; |
| 469 | int fd, ret, save_errno; |
| 470 | |
| 471 | memset(buffer, 0, sizeof(buffer)); |
| 472 | |
Nick Kralevich | f0922cc | 2016-05-14 16:47:44 -0700 | [diff] [blame] | 473 | snprintf(path, sizeof(path), "/proc/%d/stat", pid); |
Mark Salyzyn | 0751efa | 2016-02-05 15:33:17 -0800 | [diff] [blame] | 474 | if ((fd = TEMP_FAILURE_RETRY(open(path, O_RDONLY | O_CLOEXEC))) < 0) { |
Felipe Leme | d8b94e5 | 2016-12-08 10:21:44 -0800 | [diff] [blame] | 475 | printf("Failed to open '%s' (%s)\n", path, strerror(errno)); |
Mark Salyzyn | 0751efa | 2016-02-05 15:33:17 -0800 | [diff] [blame] | 476 | return; |
| 477 | } |
| 478 | |
| 479 | ret = TEMP_FAILURE_RETRY(read(fd, buffer, sizeof(buffer))); |
| 480 | save_errno = errno; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 481 | close(fd); |
Mark Salyzyn | 0751efa | 2016-02-05 15:33:17 -0800 | [diff] [blame] | 482 | |
| 483 | if (ret < 0) { |
Felipe Leme | d8b94e5 | 2016-12-08 10:21:44 -0800 | [diff] [blame] | 484 | printf("Failed to read '%s' (%s)\n", path, strerror(save_errno)); |
Mark Salyzyn | 0751efa | 2016-02-05 15:33:17 -0800 | [diff] [blame] | 485 | return; |
| 486 | } |
| 487 | |
| 488 | // field 14 is utime |
| 489 | // field 15 is stime |
| 490 | // field 42 is iotime |
| 491 | unsigned long long utime = 0, stime = 0, iotime = 0; |
| 492 | if (sscanf(buffer, |
Mark Salyzyn | 791ddd3 | 2016-02-10 07:41:12 -0800 | [diff] [blame] | 493 | "%*u %*s %*s %*d %*d %*d %*d %*d %*d %*d %*d " |
| 494 | "%*d %*d %llu %llu %*d %*d %*d %*d %*d %*d " |
| 495 | "%*d %*d %*d %*d %*d %*d %*d %*d %*d %*d " |
| 496 | "%*d %*d %*d %*d %*d %*d %*d %*d %*d %llu ", |
Mark Salyzyn | 0751efa | 2016-02-05 15:33:17 -0800 | [diff] [blame] | 497 | &utime, &stime, &iotime) != 3) { |
| 498 | return; |
| 499 | } |
| 500 | |
| 501 | unsigned long long total = utime + stime; |
| 502 | if (!total) { |
| 503 | return; |
| 504 | } |
| 505 | |
| 506 | unsigned permille = (iotime * 1000 + (total / 2)) / total; |
| 507 | if (permille > 1000) { |
| 508 | permille = 1000; |
| 509 | } |
| 510 | |
| 511 | // try to beautify and stabilize columns at <80 characters |
| 512 | snprintf(buffer, sizeof(buffer), "%-6d%s", pid, name); |
| 513 | if ((name[0] != '[') || utime) { |
| 514 | snprcent(buffer, sizeof(buffer), 57, utime); |
| 515 | } |
| 516 | snprcent(buffer, sizeof(buffer), 65, stime); |
| 517 | if ((name[0] != '[') || iotime) { |
| 518 | snprcent(buffer, sizeof(buffer), 73, iotime); |
| 519 | } |
| 520 | if (iotime) { |
| 521 | snprdec(buffer, sizeof(buffer), 79, permille); |
| 522 | } |
Felipe Leme | d8b94e5 | 2016-12-08 10:21:44 -0800 | [diff] [blame] | 523 | puts(buffer); // adds a trailing newline |
Mark Salyzyn | 0751efa | 2016-02-05 15:33:17 -0800 | [diff] [blame] | 524 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 525 | return; |
| 526 | } |
| 527 | |
| 528 | void do_dmesg() { |
Felipe Leme | 78f2c86 | 2015-12-21 09:55:22 -0800 | [diff] [blame] | 529 | const char *title = "KERNEL LOG (dmesg)"; |
| 530 | DurationReporter duration_reporter(title); |
Felipe Leme | d8b94e5 | 2016-12-08 10:21:44 -0800 | [diff] [blame] | 531 | printf("------ %s ------\n", title); |
Felipe Leme | 78f2c86 | 2015-12-21 09:55:22 -0800 | [diff] [blame] | 532 | |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 533 | if (PropertiesHelper::IsDryRun()) return; |
Felipe Leme | d402e7d | 2016-08-03 09:22:27 -0700 | [diff] [blame] | 534 | |
Elliott Hughes | 5f87b31 | 2012-09-17 11:43:40 -0700 | [diff] [blame] | 535 | /* Get size of kernel buffer */ |
| 536 | int size = klogctl(KLOG_SIZE_BUFFER, NULL, 0); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 537 | if (size <= 0) { |
Felipe Leme | d8b94e5 | 2016-12-08 10:21:44 -0800 | [diff] [blame] | 538 | printf("Unexpected klogctl return value: %d\n\n", size); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 539 | return; |
| 540 | } |
| 541 | char *buf = (char *) malloc(size + 1); |
| 542 | if (buf == NULL) { |
Felipe Leme | d8b94e5 | 2016-12-08 10:21:44 -0800 | [diff] [blame] | 543 | printf("memory allocation failed\n\n"); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 544 | return; |
| 545 | } |
| 546 | int retval = klogctl(KLOG_READ_ALL, buf, size); |
| 547 | if (retval < 0) { |
Felipe Leme | d8b94e5 | 2016-12-08 10:21:44 -0800 | [diff] [blame] | 548 | printf("klogctl failure\n\n"); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 549 | free(buf); |
| 550 | return; |
| 551 | } |
| 552 | buf[retval] = '\0'; |
Felipe Leme | d8b94e5 | 2016-12-08 10:21:44 -0800 | [diff] [blame] | 553 | printf("%s\n\n", buf); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 554 | free(buf); |
| 555 | return; |
| 556 | } |
| 557 | |
| 558 | void do_showmap(int pid, const char *name) { |
| 559 | char title[255]; |
| 560 | char arg[255]; |
| 561 | |
Nick Kralevich | f0922cc | 2016-05-14 16:47:44 -0700 | [diff] [blame] | 562 | snprintf(title, sizeof(title), "SHOW MAP %d (%s)", pid, name); |
| 563 | snprintf(arg, sizeof(arg), "%d", pid); |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 564 | RunCommand(title, {"showmap", "-q", arg}, CommandOptions::AS_ROOT); |
Felipe Leme | bda15a0 | 2016-11-16 17:48:25 -0800 | [diff] [blame] | 565 | } |
| 566 | |
Felipe Leme | 678727a | 2016-09-21 17:22:11 -0700 | [diff] [blame] | 567 | int Dumpstate::DumpFile(const std::string& title, const std::string& path) { |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 568 | DurationReporter duration_reporter(title); |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 569 | |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 570 | int status = DumpFileToFd(STDOUT_FILENO, title, path); |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 571 | |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 572 | UpdateProgress(WEIGHT_FILE); |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 573 | |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 574 | return status; |
Christopher Ferris | 54bcc5f | 2015-02-10 12:15:01 -0800 | [diff] [blame] | 575 | } |
| 576 | |
Felipe Leme | 71a74ac | 2016-03-17 15:43:25 -0700 | [diff] [blame] | 577 | int read_file_as_long(const char *path, long int *output) { |
| 578 | int fd = TEMP_FAILURE_RETRY(open(path, O_RDONLY | O_NONBLOCK | O_CLOEXEC)); |
| 579 | if (fd < 0) { |
| 580 | int err = errno; |
| 581 | MYLOGE("Error opening file descriptor for %s: %s\n", path, strerror(err)); |
| 582 | return -1; |
| 583 | } |
| 584 | char buffer[50]; |
| 585 | ssize_t bytes_read = TEMP_FAILURE_RETRY(read(fd, buffer, sizeof(buffer))); |
| 586 | if (bytes_read == -1) { |
| 587 | MYLOGE("Error reading file %s: %s\n", path, strerror(errno)); |
| 588 | return -2; |
| 589 | } |
| 590 | if (bytes_read == 0) { |
| 591 | MYLOGE("File %s is empty\n", path); |
| 592 | return -3; |
| 593 | } |
| 594 | *output = atoi(buffer); |
| 595 | return 0; |
| 596 | } |
| 597 | |
Mark Salyzyn | 326842f | 2015-04-30 09:49:41 -0700 | [diff] [blame] | 598 | /* calls skip to gate calling dump_from_fd recursively |
| 599 | * in the specified directory. dump_from_fd defaults to |
| 600 | * dump_file_from_fd above when set to NULL. skip defaults |
| 601 | * to false when set to NULL. dump_from_fd will always be |
| 602 | * called with title NULL. |
| 603 | */ |
Felipe Leme | 678727a | 2016-09-21 17:22:11 -0700 | [diff] [blame] | 604 | int dump_files(const std::string& title, const char* dir, bool (*skip)(const char* path), |
| 605 | int (*dump_from_fd)(const char* title, const char* path, int fd)) { |
Felipe Leme | 78f2c86 | 2015-12-21 09:55:22 -0800 | [diff] [blame] | 606 | DurationReporter duration_reporter(title); |
Mark Salyzyn | 326842f | 2015-04-30 09:49:41 -0700 | [diff] [blame] | 607 | DIR *dirp; |
| 608 | struct dirent *d; |
| 609 | char *newpath = NULL; |
Felipe Leme | 8620bb4 | 2015-11-10 11:04:45 -0800 | [diff] [blame] | 610 | const char *slash = "/"; |
Mark Salyzyn | 326842f | 2015-04-30 09:49:41 -0700 | [diff] [blame] | 611 | int fd, retval = 0; |
| 612 | |
Felipe Leme | 678727a | 2016-09-21 17:22:11 -0700 | [diff] [blame] | 613 | if (!title.empty()) { |
Felipe Leme | d8b94e5 | 2016-12-08 10:21:44 -0800 | [diff] [blame] | 614 | printf("------ %s (%s) ------\n", title.c_str(), dir); |
Mark Salyzyn | 326842f | 2015-04-30 09:49:41 -0700 | [diff] [blame] | 615 | } |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 616 | if (PropertiesHelper::IsDryRun()) return 0; |
Mark Salyzyn | 326842f | 2015-04-30 09:49:41 -0700 | [diff] [blame] | 617 | |
| 618 | if (dir[strlen(dir) - 1] == '/') { |
| 619 | ++slash; |
| 620 | } |
| 621 | dirp = opendir(dir); |
| 622 | if (dirp == NULL) { |
| 623 | retval = -errno; |
Felipe Leme | 107a05f | 2016-03-08 15:11:15 -0800 | [diff] [blame] | 624 | MYLOGE("%s: %s\n", dir, strerror(errno)); |
Mark Salyzyn | 326842f | 2015-04-30 09:49:41 -0700 | [diff] [blame] | 625 | return retval; |
| 626 | } |
| 627 | |
| 628 | if (!dump_from_fd) { |
| 629 | dump_from_fd = dump_file_from_fd; |
| 630 | } |
| 631 | for (; ((d = readdir(dirp))); free(newpath), newpath = NULL) { |
| 632 | if ((d->d_name[0] == '.') |
| 633 | && (((d->d_name[1] == '.') && (d->d_name[2] == '\0')) |
| 634 | || (d->d_name[1] == '\0'))) { |
| 635 | continue; |
| 636 | } |
| 637 | asprintf(&newpath, "%s%s%s%s", dir, slash, d->d_name, |
| 638 | (d->d_type == DT_DIR) ? "/" : ""); |
| 639 | if (!newpath) { |
| 640 | retval = -errno; |
| 641 | continue; |
| 642 | } |
| 643 | if (skip && (*skip)(newpath)) { |
| 644 | continue; |
| 645 | } |
| 646 | if (d->d_type == DT_DIR) { |
Felipe Leme | 678727a | 2016-09-21 17:22:11 -0700 | [diff] [blame] | 647 | int ret = dump_files("", newpath, skip, dump_from_fd); |
Mark Salyzyn | 326842f | 2015-04-30 09:49:41 -0700 | [diff] [blame] | 648 | if (ret < 0) { |
| 649 | retval = ret; |
| 650 | } |
| 651 | continue; |
| 652 | } |
| 653 | fd = TEMP_FAILURE_RETRY(open(newpath, O_RDONLY | O_NONBLOCK | O_CLOEXEC)); |
| 654 | if (fd < 0) { |
| 655 | retval = fd; |
Felipe Leme | d8b94e5 | 2016-12-08 10:21:44 -0800 | [diff] [blame] | 656 | printf("*** %s: %s\n", newpath, strerror(errno)); |
Mark Salyzyn | 326842f | 2015-04-30 09:49:41 -0700 | [diff] [blame] | 657 | continue; |
| 658 | } |
| 659 | (*dump_from_fd)(NULL, newpath, fd); |
| 660 | } |
| 661 | closedir(dirp); |
Felipe Leme | 678727a | 2016-09-21 17:22:11 -0700 | [diff] [blame] | 662 | if (!title.empty()) { |
Felipe Leme | d8b94e5 | 2016-12-08 10:21:44 -0800 | [diff] [blame] | 663 | printf("\n"); |
Mark Salyzyn | 326842f | 2015-04-30 09:49:41 -0700 | [diff] [blame] | 664 | } |
| 665 | return retval; |
| 666 | } |
| 667 | |
Christopher Ferris | 54bcc5f | 2015-02-10 12:15:01 -0800 | [diff] [blame] | 668 | /* fd must have been opened with the flag O_NONBLOCK. With this flag set, |
| 669 | * it's possible to avoid issues where opening the file itself can get |
| 670 | * stuck. |
| 671 | */ |
| 672 | int dump_file_from_fd(const char *title, const char *path, int fd) { |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 673 | if (PropertiesHelper::IsDryRun()) return 0; |
Felipe Leme | d402e7d | 2016-08-03 09:22:27 -0700 | [diff] [blame] | 674 | |
Christopher Ferris | 54bcc5f | 2015-02-10 12:15:01 -0800 | [diff] [blame] | 675 | int flags = fcntl(fd, F_GETFL); |
| 676 | if (flags == -1) { |
Felipe Leme | d8b94e5 | 2016-12-08 10:21:44 -0800 | [diff] [blame] | 677 | 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] | 678 | close(fd); |
Christopher Ferris | 54bcc5f | 2015-02-10 12:15:01 -0800 | [diff] [blame] | 679 | return -1; |
| 680 | } else if (!(flags & O_NONBLOCK)) { |
Felipe Leme | d8b94e5 | 2016-12-08 10:21:44 -0800 | [diff] [blame] | 681 | printf("*** %s: fd must have O_NONBLOCK set.\n", path); |
Christopher Ferris | ed24d2a | 2015-11-12 14:01:56 -0800 | [diff] [blame] | 682 | close(fd); |
Christopher Ferris | 54bcc5f | 2015-02-10 12:15:01 -0800 | [diff] [blame] | 683 | return -1; |
| 684 | } |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 685 | return DumpFileFromFdToFd(title, path, fd, STDOUT_FILENO, PropertiesHelper::IsDryRun()); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 686 | } |
| 687 | |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 688 | int Dumpstate::RunCommand(const std::string& title, const std::vector<std::string>& full_command, |
| 689 | const CommandOptions& options) { |
| 690 | DurationReporter duration_reporter(title); |
| 691 | |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 692 | int status = RunCommandToFd(STDOUT_FILENO, title, full_command, options); |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 693 | |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 694 | /* TODO: for now we're simplifying the progress calculation by using the |
| 695 | * timeout as the weight. It's a good approximation for most cases, except when calling dumpsys, |
| 696 | * where its weight should be much higher proportionally to its timeout. |
| 697 | * Ideally, it should use a options.EstimatedDuration() instead...*/ |
| 698 | UpdateProgress(options.Timeout()); |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 699 | |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 700 | return status; |
| 701 | } |
| 702 | |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 703 | void Dumpstate::RunDumpsys(const std::string& title, const std::vector<std::string>& dumpsys_args, |
Felipe Leme | 678727a | 2016-09-21 17:22:11 -0700 | [diff] [blame] | 704 | const CommandOptions& options, long dumpsysTimeout) { |
Felipe Leme | 5bcce57 | 2016-09-27 09:21:08 -0700 | [diff] [blame] | 705 | long timeout = dumpsysTimeout > 0 ? dumpsysTimeout : options.Timeout(); |
| 706 | std::vector<std::string> dumpsys = {"/system/bin/dumpsys", "-t", std::to_string(timeout)}; |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 707 | dumpsys.insert(dumpsys.end(), dumpsys_args.begin(), dumpsys_args.end()); |
Felipe Leme | 678727a | 2016-09-21 17:22:11 -0700 | [diff] [blame] | 708 | RunCommand(title, dumpsys, options); |
Felipe Leme | 30dbfa1 | 2016-09-02 12:43:26 -0700 | [diff] [blame] | 709 | } |
| 710 | |
Felipe Leme | 36b3f6f | 2015-11-19 15:41:04 -0800 | [diff] [blame] | 711 | 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] | 712 | std::vector<std::string> am = {"/system/bin/am", "broadcast", "--user", "0", "-a", action}; |
| 713 | |
| 714 | am.insert(am.end(), args.begin(), args.end()); |
| 715 | |
Felipe Leme | 678727a | 2016-09-21 17:22:11 -0700 | [diff] [blame] | 716 | RunCommand("", am, CommandOptions::WithTimeout(20) |
| 717 | .Log("Sending broadcast: '%s'\n") |
| 718 | .Always() |
| 719 | .DropRoot() |
| 720 | .RedirectStderr() |
| 721 | .Build()); |
Felipe Leme | 36b3f6f | 2015-11-19 15:41:04 -0800 | [diff] [blame] | 722 | } |
| 723 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 724 | size_t num_props = 0; |
| 725 | static char* props[2000]; |
| 726 | |
| 727 | static void print_prop(const char *key, const char *name, void *user) { |
| 728 | (void) user; |
| 729 | if (num_props < sizeof(props) / sizeof(props[0])) { |
| 730 | char buf[PROPERTY_KEY_MAX + PROPERTY_VALUE_MAX + 10]; |
| 731 | snprintf(buf, sizeof(buf), "[%s]: [%s]\n", key, name); |
| 732 | props[num_props++] = strdup(buf); |
| 733 | } |
| 734 | } |
| 735 | |
| 736 | static int compare_prop(const void *a, const void *b) { |
| 737 | return strcmp(*(char * const *) a, *(char * const *) b); |
| 738 | } |
| 739 | |
| 740 | /* prints all the system properties */ |
| 741 | void print_properties() { |
Felipe Leme | 78f2c86 | 2015-12-21 09:55:22 -0800 | [diff] [blame] | 742 | const char* title = "SYSTEM PROPERTIES"; |
| 743 | DurationReporter duration_reporter(title); |
Felipe Leme | d8b94e5 | 2016-12-08 10:21:44 -0800 | [diff] [blame] | 744 | printf("------ %s ------\n", title); |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 745 | if (PropertiesHelper::IsDryRun()) return; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 746 | size_t i; |
| 747 | num_props = 0; |
| 748 | property_list(print_prop, NULL); |
| 749 | qsort(&props, num_props, sizeof(props[0]), compare_prop); |
| 750 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 751 | for (i = 0; i < num_props; ++i) { |
| 752 | fputs(props[i], stdout); |
| 753 | free(props[i]); |
| 754 | } |
Felipe Leme | d8b94e5 | 2016-12-08 10:21:44 -0800 | [diff] [blame] | 755 | printf("\n"); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 756 | } |
| 757 | |
Felipe Leme | 2628e9e | 2016-04-12 16:36:51 -0700 | [diff] [blame] | 758 | int open_socket(const char *service) { |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 759 | int s = android_get_control_socket(service); |
| 760 | if (s < 0) { |
Felipe Leme | 107a05f | 2016-03-08 15:11:15 -0800 | [diff] [blame] | 761 | MYLOGE("android_get_control_socket(%s): %s\n", service, strerror(errno)); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 762 | exit(1); |
| 763 | } |
Nick Kralevich | cd67e9f | 2015-03-19 11:30:59 -0700 | [diff] [blame] | 764 | fcntl(s, F_SETFD, FD_CLOEXEC); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 765 | if (listen(s, 4) < 0) { |
Felipe Leme | 107a05f | 2016-03-08 15:11:15 -0800 | [diff] [blame] | 766 | MYLOGE("listen(control socket): %s\n", strerror(errno)); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 767 | exit(1); |
| 768 | } |
| 769 | |
| 770 | struct sockaddr addr; |
| 771 | socklen_t alen = sizeof(addr); |
| 772 | int fd = accept(s, &addr, &alen); |
| 773 | if (fd < 0) { |
Felipe Leme | 107a05f | 2016-03-08 15:11:15 -0800 | [diff] [blame] | 774 | MYLOGE("accept(control socket): %s\n", strerror(errno)); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 775 | exit(1); |
| 776 | } |
| 777 | |
Felipe Leme | 2628e9e | 2016-04-12 16:36:51 -0700 | [diff] [blame] | 778 | return fd; |
| 779 | } |
| 780 | |
| 781 | /* redirect output to a service control socket */ |
| 782 | void redirect_to_socket(FILE *redirect, const char *service) { |
| 783 | int fd = open_socket(service); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 784 | fflush(redirect); |
| 785 | dup2(fd, fileno(redirect)); |
| 786 | close(fd); |
| 787 | } |
| 788 | |
Felipe Leme | 2628e9e | 2016-04-12 16:36:51 -0700 | [diff] [blame] | 789 | // 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] | 790 | void create_parent_dirs(const char *path) { |
Srinath Sridharan | fdf52d3 | 2016-02-01 15:50:22 -0800 | [diff] [blame] | 791 | char *chp = const_cast<char *> (path); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 792 | |
| 793 | /* skip initial slash */ |
| 794 | if (chp[0] == '/') |
| 795 | chp++; |
| 796 | |
| 797 | /* create leading directories, if necessary */ |
Felipe Leme | 111b9d0 | 2016-02-03 09:28:24 -0800 | [diff] [blame] | 798 | struct stat dir_stat; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 799 | while (chp && chp[0]) { |
| 800 | chp = strchr(chp, '/'); |
| 801 | if (chp) { |
| 802 | *chp = 0; |
Felipe Leme | 111b9d0 | 2016-02-03 09:28:24 -0800 | [diff] [blame] | 803 | if (stat(path, &dir_stat) == -1 || !S_ISDIR(dir_stat.st_mode)) { |
Felipe Leme | cbce55d | 2016-02-08 09:53:18 -0800 | [diff] [blame] | 804 | MYLOGI("Creating directory %s\n", path); |
Felipe Leme | 111b9d0 | 2016-02-03 09:28:24 -0800 | [diff] [blame] | 805 | if (mkdir(path, 0770)) { /* drwxrwx--- */ |
Felipe Leme | cbce55d | 2016-02-08 09:53:18 -0800 | [diff] [blame] | 806 | MYLOGE("Unable to create directory %s: %s\n", path, strerror(errno)); |
Felipe Leme | 111b9d0 | 2016-02-03 09:28:24 -0800 | [diff] [blame] | 807 | } else if (chown(path, AID_SHELL, AID_SHELL)) { |
Felipe Leme | cbce55d | 2016-02-08 09:53:18 -0800 | [diff] [blame] | 808 | 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] | 809 | } |
| 810 | } |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 811 | *chp++ = '/'; |
| 812 | } |
| 813 | } |
Felipe Leme | 111b9d0 | 2016-02-03 09:28:24 -0800 | [diff] [blame] | 814 | } |
| 815 | |
Felipe Leme | 0f3fb20 | 2016-06-10 17:10:53 -0700 | [diff] [blame] | 816 | void _redirect_to_file(FILE *redirect, char *path, int truncate_flag) { |
Felipe Leme | 111b9d0 | 2016-02-03 09:28:24 -0800 | [diff] [blame] | 817 | create_parent_dirs(path); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 818 | |
Felipe Leme | 0f3fb20 | 2016-06-10 17:10:53 -0700 | [diff] [blame] | 819 | int fd = TEMP_FAILURE_RETRY(open(path, |
| 820 | O_WRONLY | O_CREAT | truncate_flag | O_CLOEXEC | O_NOFOLLOW, |
Christopher Ferris | ff4a4dc | 2015-02-09 16:24:47 -0800 | [diff] [blame] | 821 | S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 822 | if (fd < 0) { |
Felipe Leme | 107a05f | 2016-03-08 15:11:15 -0800 | [diff] [blame] | 823 | MYLOGE("%s: %s\n", path, strerror(errno)); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 824 | exit(1); |
| 825 | } |
| 826 | |
Christopher Ferris | ff4a4dc | 2015-02-09 16:24:47 -0800 | [diff] [blame] | 827 | TEMP_FAILURE_RETRY(dup2(fd, fileno(redirect))); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 828 | close(fd); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 829 | } |
| 830 | |
Felipe Leme | 0f3fb20 | 2016-06-10 17:10:53 -0700 | [diff] [blame] | 831 | void redirect_to_file(FILE *redirect, char *path) { |
| 832 | _redirect_to_file(redirect, path, O_TRUNC); |
| 833 | } |
| 834 | |
| 835 | void redirect_to_existing_file(FILE *redirect, char *path) { |
| 836 | _redirect_to_file(redirect, path, O_APPEND); |
| 837 | } |
| 838 | |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 839 | static bool should_dump_native_traces(const char* path) { |
| 840 | for (const char** p = native_processes_to_dump; *p; p++) { |
| 841 | if (!strcmp(*p, path)) { |
| 842 | return true; |
| 843 | } |
| 844 | } |
| 845 | return false; |
| 846 | } |
| 847 | |
| 848 | /* dump Dalvik and native stack traces, return the trace file location (NULL if none) */ |
| 849 | const char *dump_traces() { |
Felipe Leme | e184f66 | 2016-10-27 10:04:47 -0700 | [diff] [blame] | 850 | DurationReporter duration_reporter("DUMP TRACES"); |
Felipe Leme | d402e7d | 2016-08-03 09:22:27 -0700 | [diff] [blame] | 851 | |
Felipe Leme | 96c2bbb | 2016-09-26 09:21:21 -0700 | [diff] [blame] | 852 | const char* result = nullptr; |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 853 | |
Felipe Leme | e184f66 | 2016-10-27 10:04:47 -0700 | [diff] [blame] | 854 | std::string traces_path = android::base::GetProperty("dalvik.vm.stack-trace-file", ""); |
| 855 | if (traces_path.empty()) return nullptr; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 856 | |
| 857 | /* move the old traces.txt (if any) out of the way temporarily */ |
Felipe Leme | e184f66 | 2016-10-27 10:04:47 -0700 | [diff] [blame] | 858 | std::string anrtraces_path = traces_path + ".anr"; |
| 859 | if (rename(traces_path.c_str(), anrtraces_path.c_str()) && errno != ENOENT) { |
| 860 | MYLOGE("rename(%s, %s): %s\n", traces_path.c_str(), anrtraces_path.c_str(), strerror(errno)); |
Felipe Leme | 96c2bbb | 2016-09-26 09:21:21 -0700 | [diff] [blame] | 861 | 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] | 862 | } |
| 863 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 864 | /* create a new, empty traces.txt file to receive stack dumps */ |
Felipe Leme | e184f66 | 2016-10-27 10:04:47 -0700 | [diff] [blame] | 865 | int fd = TEMP_FAILURE_RETRY(open(traces_path.c_str(), |
Felipe Leme | 96c2bbb | 2016-09-26 09:21:21 -0700 | [diff] [blame] | 866 | O_CREAT | O_WRONLY | O_TRUNC | O_NOFOLLOW | O_CLOEXEC, |
| 867 | 0666)); /* -rw-rw-rw- */ |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 868 | if (fd < 0) { |
Felipe Leme | e184f66 | 2016-10-27 10:04:47 -0700 | [diff] [blame] | 869 | MYLOGE("%s: %s\n", traces_path.c_str(), strerror(errno)); |
Felipe Leme | 96c2bbb | 2016-09-26 09:21:21 -0700 | [diff] [blame] | 870 | return nullptr; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 871 | } |
Nick Kralevich | c7f1fe2 | 2012-04-06 09:31:28 -0700 | [diff] [blame] | 872 | int chmod_ret = fchmod(fd, 0666); |
| 873 | if (chmod_ret < 0) { |
Felipe Leme | e184f66 | 2016-10-27 10:04:47 -0700 | [diff] [blame] | 874 | MYLOGE("fchmod on %s failed: %s\n", traces_path.c_str(), strerror(errno)); |
Nick Kralevich | c7f1fe2 | 2012-04-06 09:31:28 -0700 | [diff] [blame] | 875 | close(fd); |
Felipe Leme | 96c2bbb | 2016-09-26 09:21:21 -0700 | [diff] [blame] | 876 | return nullptr; |
Nick Kralevich | c7f1fe2 | 2012-04-06 09:31:28 -0700 | [diff] [blame] | 877 | } |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 878 | |
Felipe Leme | 8620bb4 | 2015-11-10 11:04:45 -0800 | [diff] [blame] | 879 | /* Variables below must be initialized before 'goto' statements */ |
| 880 | int dalvik_found = 0; |
| 881 | int ifd, wfd = -1; |
| 882 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 883 | /* walk /proc and kill -QUIT all Dalvik processes */ |
| 884 | DIR *proc = opendir("/proc"); |
| 885 | if (proc == NULL) { |
Felipe Leme | 107a05f | 2016-03-08 15:11:15 -0800 | [diff] [blame] | 886 | MYLOGE("/proc: %s\n", strerror(errno)); |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 887 | goto error_close_fd; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 888 | } |
| 889 | |
| 890 | /* use inotify to find when processes are done dumping */ |
Felipe Leme | 8620bb4 | 2015-11-10 11:04:45 -0800 | [diff] [blame] | 891 | ifd = inotify_init(); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 892 | if (ifd < 0) { |
Felipe Leme | 107a05f | 2016-03-08 15:11:15 -0800 | [diff] [blame] | 893 | MYLOGE("inotify_init: %s\n", strerror(errno)); |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 894 | goto error_close_fd; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 895 | } |
| 896 | |
Felipe Leme | e184f66 | 2016-10-27 10:04:47 -0700 | [diff] [blame] | 897 | wfd = inotify_add_watch(ifd, traces_path.c_str(), IN_CLOSE_WRITE); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 898 | if (wfd < 0) { |
Felipe Leme | e184f66 | 2016-10-27 10:04:47 -0700 | [diff] [blame] | 899 | MYLOGE("inotify_add_watch(%s): %s\n", traces_path.c_str(), strerror(errno)); |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 900 | goto error_close_ifd; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 901 | } |
| 902 | |
| 903 | struct dirent *d; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 904 | while ((d = readdir(proc))) { |
| 905 | int pid = atoi(d->d_name); |
| 906 | if (pid <= 0) continue; |
| 907 | |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 908 | char path[PATH_MAX]; |
| 909 | char data[PATH_MAX]; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 910 | snprintf(path, sizeof(path), "/proc/%d/exe", pid); |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 911 | ssize_t len = readlink(path, data, sizeof(data) - 1); |
| 912 | if (len <= 0) { |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 913 | continue; |
| 914 | } |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 915 | data[len] = '\0'; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 916 | |
Colin Cross | 0d6180f | 2014-07-16 19:00:46 -0700 | [diff] [blame] | 917 | if (!strncmp(data, "/system/bin/app_process", strlen("/system/bin/app_process"))) { |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 918 | /* skip zygote -- it won't dump its stack anyway */ |
| 919 | snprintf(path, sizeof(path), "/proc/%d/cmdline", pid); |
Nick Kralevich | cd67e9f | 2015-03-19 11:30:59 -0700 | [diff] [blame] | 920 | int cfd = TEMP_FAILURE_RETRY(open(path, O_RDONLY | O_CLOEXEC)); |
Jeff Brown | 1dc94e3 | 2014-09-11 14:15:27 -0700 | [diff] [blame] | 921 | len = read(cfd, data, sizeof(data) - 1); |
| 922 | close(cfd); |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 923 | if (len <= 0) { |
| 924 | continue; |
| 925 | } |
| 926 | data[len] = '\0'; |
Colin Cross | 0d6180f | 2014-07-16 19:00:46 -0700 | [diff] [blame] | 927 | if (!strncmp(data, "zygote", strlen("zygote"))) { |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 928 | continue; |
| 929 | } |
| 930 | |
| 931 | ++dalvik_found; |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 932 | uint64_t start = Nanotime(); |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 933 | if (kill(pid, SIGQUIT)) { |
Felipe Leme | 107a05f | 2016-03-08 15:11:15 -0800 | [diff] [blame] | 934 | MYLOGE("kill(%d, SIGQUIT): %s\n", pid, strerror(errno)); |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 935 | continue; |
| 936 | } |
| 937 | |
| 938 | /* wait for the writable-close notification from inotify */ |
| 939 | struct pollfd pfd = { ifd, POLLIN, 0 }; |
Felipe Leme | 6188412 | 2016-06-13 09:23:30 -0700 | [diff] [blame] | 940 | int ret = poll(&pfd, 1, TRACE_DUMP_TIMEOUT_MS); |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 941 | if (ret < 0) { |
Felipe Leme | 107a05f | 2016-03-08 15:11:15 -0800 | [diff] [blame] | 942 | MYLOGE("poll: %s\n", strerror(errno)); |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 943 | } else if (ret == 0) { |
Felipe Leme | 107a05f | 2016-03-08 15:11:15 -0800 | [diff] [blame] | 944 | MYLOGE("warning: timed out dumping pid %d\n", pid); |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 945 | } else { |
| 946 | struct inotify_event ie; |
| 947 | read(ifd, &ie, sizeof(ie)); |
| 948 | } |
Jeff Brown | 1dc94e3 | 2014-09-11 14:15:27 -0700 | [diff] [blame] | 949 | |
| 950 | if (lseek(fd, 0, SEEK_END) < 0) { |
Felipe Leme | 107a05f | 2016-03-08 15:11:15 -0800 | [diff] [blame] | 951 | MYLOGE("lseek: %s\n", strerror(errno)); |
Jeff Brown | 1dc94e3 | 2014-09-11 14:15:27 -0700 | [diff] [blame] | 952 | } else { |
Felipe Leme | b0f669d | 2016-09-26 18:26:11 -0700 | [diff] [blame] | 953 | dprintf(fd, "[dump dalvik stack %d: %.3fs elapsed]\n", pid, |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 954 | (float)(Nanotime() - start) / NANOS_PER_SEC); |
Jeff Brown | 1dc94e3 | 2014-09-11 14:15:27 -0700 | [diff] [blame] | 955 | } |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 956 | } else if (should_dump_native_traces(data)) { |
| 957 | /* dump native process if appropriate */ |
| 958 | if (lseek(fd, 0, SEEK_END) < 0) { |
Felipe Leme | 107a05f | 2016-03-08 15:11:15 -0800 | [diff] [blame] | 959 | MYLOGE("lseek: %s\n", strerror(errno)); |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 960 | } else { |
Christopher Ferris | 31ef855 | 2015-01-14 13:23:30 -0800 | [diff] [blame] | 961 | static uint16_t timeout_failures = 0; |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 962 | uint64_t start = Nanotime(); |
Christopher Ferris | 31ef855 | 2015-01-14 13:23:30 -0800 | [diff] [blame] | 963 | |
| 964 | /* If 3 backtrace dumps fail in a row, consider debuggerd dead. */ |
| 965 | if (timeout_failures == 3) { |
| 966 | dprintf(fd, "too many stack dump failures, skipping...\n"); |
| 967 | } else if (dump_backtrace_to_file_timeout(pid, fd, 20) == -1) { |
| 968 | dprintf(fd, "dumping failed, likely due to a timeout\n"); |
| 969 | timeout_failures++; |
| 970 | } else { |
| 971 | timeout_failures = 0; |
| 972 | } |
Felipe Leme | b0f669d | 2016-09-26 18:26:11 -0700 | [diff] [blame] | 973 | dprintf(fd, "[dump native stack %d: %.3fs elapsed]\n", pid, |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 974 | (float)(Nanotime() - start) / NANOS_PER_SEC); |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 975 | } |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 976 | } |
| 977 | } |
| 978 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 979 | if (dalvik_found == 0) { |
Felipe Leme | 107a05f | 2016-03-08 15:11:15 -0800 | [diff] [blame] | 980 | MYLOGE("Warning: no Dalvik processes found to dump stacks\n"); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 981 | } |
| 982 | |
Felipe Leme | e184f66 | 2016-10-27 10:04:47 -0700 | [diff] [blame] | 983 | static std::string dumptraces_path = android::base::StringPrintf( |
| 984 | "%s/bugreport-%s", dirname(traces_path.c_str()), basename(traces_path.c_str())); |
| 985 | if (rename(traces_path.c_str(), dumptraces_path.c_str())) { |
| 986 | MYLOGE("rename(%s, %s): %s\n", traces_path.c_str(), dumptraces_path.c_str(), |
| 987 | strerror(errno)); |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 988 | goto error_close_ifd; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 989 | } |
Felipe Leme | e184f66 | 2016-10-27 10:04:47 -0700 | [diff] [blame] | 990 | result = dumptraces_path.c_str(); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 991 | |
| 992 | /* replace the saved [ANR] traces.txt file */ |
Felipe Leme | e184f66 | 2016-10-27 10:04:47 -0700 | [diff] [blame] | 993 | rename(anrtraces_path.c_str(), traces_path.c_str()); |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 994 | |
| 995 | error_close_ifd: |
| 996 | close(ifd); |
| 997 | error_close_fd: |
| 998 | close(fd); |
| 999 | return result; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1000 | } |
| 1001 | |
Sreeram Ramachandran | 2b3bba3 | 2014-07-08 15:40:55 -0700 | [diff] [blame] | 1002 | void dump_route_tables() { |
Felipe Leme | 78f2c86 | 2015-12-21 09:55:22 -0800 | [diff] [blame] | 1003 | DurationReporter duration_reporter("DUMP ROUTE TABLES"); |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 1004 | if (PropertiesHelper::IsDryRun()) return; |
Sreeram Ramachandran | 2b3bba3 | 2014-07-08 15:40:55 -0700 | [diff] [blame] | 1005 | const char* const RT_TABLES_PATH = "/data/misc/net/rt_tables"; |
Felipe Leme | c7fe8fe | 2016-09-21 18:13:20 -0700 | [diff] [blame] | 1006 | ds.DumpFile("RT_TABLES", RT_TABLES_PATH); |
Nick Kralevich | cd67e9f | 2015-03-19 11:30:59 -0700 | [diff] [blame] | 1007 | FILE* fp = fopen(RT_TABLES_PATH, "re"); |
Sreeram Ramachandran | 2b3bba3 | 2014-07-08 15:40:55 -0700 | [diff] [blame] | 1008 | if (!fp) { |
Felipe Leme | d8b94e5 | 2016-12-08 10:21:44 -0800 | [diff] [blame] | 1009 | printf("*** %s: %s\n", RT_TABLES_PATH, strerror(errno)); |
Sreeram Ramachandran | 2b3bba3 | 2014-07-08 15:40:55 -0700 | [diff] [blame] | 1010 | return; |
| 1011 | } |
| 1012 | char table[16]; |
| 1013 | // Each line has an integer (the table number), a space, and a string (the table name). We only |
| 1014 | // need the table number. It's a 32-bit unsigned number, so max 10 chars. Skip the table name. |
| 1015 | // Add a fixed max limit so this doesn't go awry. |
| 1016 | 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] | 1017 | RunCommand("ROUTE TABLE IPv4", {"ip", "-4", "route", "show", "table", table}); |
| 1018 | RunCommand("ROUTE TABLE IPv6", {"ip", "-6", "route", "show", "table", table}); |
Sreeram Ramachandran | 2b3bba3 | 2014-07-08 15:40:55 -0700 | [diff] [blame] | 1019 | } |
| 1020 | fclose(fp); |
| 1021 | } |
Felipe Leme | 71bbfc5 | 2015-11-23 14:14:51 -0800 | [diff] [blame] | 1022 | |
Felipe Leme | 71bbfc5 | 2015-11-23 14:14:51 -0800 | [diff] [blame] | 1023 | // TODO: make this function thread safe if sections are generated in parallel. |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 1024 | void Dumpstate::UpdateProgress(int32_t delta) { |
| 1025 | if (progress_ == nullptr) { |
| 1026 | MYLOGE("UpdateProgress: progress_ not set\n"); |
| 1027 | return; |
| 1028 | } |
Felipe Leme | 71bbfc5 | 2015-11-23 14:14:51 -0800 | [diff] [blame] | 1029 | |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 1030 | // Always update progess so stats can be tuned... |
| 1031 | bool max_changed = progress_->Inc(delta); |
| 1032 | |
| 1033 | // ...but only notifiy listeners when necessary. |
| 1034 | if (!update_progress_) return; |
Felipe Leme | 71bbfc5 | 2015-11-23 14:14:51 -0800 | [diff] [blame] | 1035 | |
Felipe Leme | 009ecbb | 2016-11-07 10:18:44 -0800 | [diff] [blame] | 1036 | int progress = progress_->Get(); |
| 1037 | int max = progress_->GetMax(); |
Felipe Leme | ad5f6c4 | 2015-11-30 14:26:46 -0800 | [diff] [blame] | 1038 | |
| 1039 | // adjusts max on the fly |
Felipe Leme | 009ecbb | 2016-11-07 10:18:44 -0800 | [diff] [blame] | 1040 | if (max_changed && listener_ != nullptr) { |
| 1041 | listener_->onMaxProgressUpdated(max); |
Felipe Leme | ad5f6c4 | 2015-11-30 14:26:46 -0800 | [diff] [blame] | 1042 | } |
| 1043 | |
Felipe Leme | 009ecbb | 2016-11-07 10:18:44 -0800 | [diff] [blame] | 1044 | int32_t last_update_delta = progress - last_updated_progress_; |
| 1045 | if (last_updated_progress_ > 0 && last_update_delta < update_progress_threshold_) { |
| 1046 | return; |
| 1047 | } |
| 1048 | last_updated_progress_ = progress; |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 1049 | |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 1050 | if (control_socket_fd_ >= 0) { |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 1051 | dprintf(control_socket_fd_, "PROGRESS:%d/%d\n", progress, max); |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 1052 | fsync(control_socket_fd_); |
Felipe Leme | 02b7e00 | 2016-07-22 12:03:20 -0700 | [diff] [blame] | 1053 | } |
| 1054 | |
Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 1055 | if (listener_ != nullptr) { |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 1056 | if (progress % 100 == 0) { |
Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 1057 | // We don't want to spam logcat, so only log multiples of 100. |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 1058 | MYLOGD("Setting progress (%s): %d/%d\n", listener_name_.c_str(), progress, max); |
Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 1059 | } else { |
| 1060 | // stderr is ignored on normal invocations, but useful when calling |
| 1061 | // /system/bin/dumpstate directly for debuggging. |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 1062 | fprintf(stderr, "Setting progress (%s): %d/%d\n", listener_name_.c_str(), progress, max); |
Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 1063 | } |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 1064 | listener_->onProgressUpdated(progress); |
Felipe Leme | 71bbfc5 | 2015-11-23 14:14:51 -0800 | [diff] [blame] | 1065 | } |
| 1066 | } |
Felipe Leme | e338bf6 | 2015-12-07 14:03:50 -0800 | [diff] [blame] | 1067 | |
Felipe Leme | bbaf3c1 | 2016-10-11 14:32:25 -0700 | [diff] [blame] | 1068 | void Dumpstate::TakeScreenshot(const std::string& path) { |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 1069 | const std::string& real_path = path.empty() ? screenshot_path_ : path; |
Felipe Leme | bbaf3c1 | 2016-10-11 14:32:25 -0700 | [diff] [blame] | 1070 | int status = |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 1071 | RunCommand("", {"/system/bin/screencap", "-p", real_path}, |
Felipe Leme | bbaf3c1 | 2016-10-11 14:32:25 -0700 | [diff] [blame] | 1072 | CommandOptions::WithTimeout(10).Always().DropRoot().RedirectStderr().Build()); |
| 1073 | if (status == 0) { |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 1074 | MYLOGD("Screenshot saved on %s\n", real_path.c_str()); |
Felipe Leme | bbaf3c1 | 2016-10-11 14:32:25 -0700 | [diff] [blame] | 1075 | } else { |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 1076 | MYLOGE("Failed to take screenshot on %s\n", real_path.c_str()); |
Felipe Leme | bbaf3c1 | 2016-10-11 14:32:25 -0700 | [diff] [blame] | 1077 | } |
Felipe Leme | e338bf6 | 2015-12-07 14:03:50 -0800 | [diff] [blame] | 1078 | } |
Mark Salyzyn | f55d402 | 2015-12-11 07:32:31 -0800 | [diff] [blame] | 1079 | |
Felipe Leme | 0c80cf0 | 2016-01-05 13:25:34 -0800 | [diff] [blame] | 1080 | bool is_dir(const char* pathname) { |
| 1081 | struct stat info; |
| 1082 | if (stat(pathname, &info) == -1) { |
| 1083 | return false; |
| 1084 | } |
| 1085 | return S_ISDIR(info.st_mode); |
| 1086 | } |
| 1087 | |
| 1088 | time_t get_mtime(int fd, time_t default_mtime) { |
| 1089 | struct stat info; |
| 1090 | if (fstat(fd, &info) == -1) { |
| 1091 | return default_mtime; |
| 1092 | } |
| 1093 | return info.st_mtime; |
| 1094 | } |
| 1095 | |
Mark Salyzyn | 8c8130e | 2015-12-09 11:21:28 -0800 | [diff] [blame] | 1096 | void dump_emmc_ecsd(const char *ext_csd_path) { |
Mark Salyzyn | 290f4b9 | 2016-05-16 08:33:59 -0700 | [diff] [blame] | 1097 | // List of interesting offsets |
Mark Salyzyn | 8c8130e | 2015-12-09 11:21:28 -0800 | [diff] [blame] | 1098 | struct hex { |
| 1099 | char str[2]; |
Mark Salyzyn | 8c8130e | 2015-12-09 11:21:28 -0800 | [diff] [blame] | 1100 | }; |
Mark Salyzyn | 290f4b9 | 2016-05-16 08:33:59 -0700 | [diff] [blame] | 1101 | static const size_t EXT_CSD_REV = 192 * sizeof(hex); |
| 1102 | static const size_t EXT_PRE_EOL_INFO = 267 * sizeof(hex); |
| 1103 | static const size_t EXT_DEVICE_LIFE_TIME_EST_TYP_A = 268 * sizeof(hex); |
| 1104 | static const size_t EXT_DEVICE_LIFE_TIME_EST_TYP_B = 269 * sizeof(hex); |
| 1105 | |
| 1106 | std::string buffer; |
| 1107 | if (!android::base::ReadFileToString(ext_csd_path, &buffer)) { |
| 1108 | return; |
| 1109 | } |
Mark Salyzyn | 8c8130e | 2015-12-09 11:21:28 -0800 | [diff] [blame] | 1110 | |
Felipe Leme | d8b94e5 | 2016-12-08 10:21:44 -0800 | [diff] [blame] | 1111 | printf("------ %s Extended CSD ------\n", ext_csd_path); |
Mark Salyzyn | 8c8130e | 2015-12-09 11:21:28 -0800 | [diff] [blame] | 1112 | |
Mark Salyzyn | 290f4b9 | 2016-05-16 08:33:59 -0700 | [diff] [blame] | 1113 | if (buffer.length() < (EXT_CSD_REV + sizeof(hex))) { |
Felipe Leme | d8b94e5 | 2016-12-08 10:21:44 -0800 | [diff] [blame] | 1114 | printf("*** %s: truncated content %zu\n\n", ext_csd_path, buffer.length()); |
Mark Salyzyn | 8c8130e | 2015-12-09 11:21:28 -0800 | [diff] [blame] | 1115 | return; |
| 1116 | } |
| 1117 | |
Mark Salyzyn | 290f4b9 | 2016-05-16 08:33:59 -0700 | [diff] [blame] | 1118 | int ext_csd_rev = 0; |
| 1119 | std::string sub = buffer.substr(EXT_CSD_REV, sizeof(hex)); |
| 1120 | if (sscanf(sub.c_str(), "%2x", &ext_csd_rev) != 1) { |
Felipe Leme | d8b94e5 | 2016-12-08 10:21:44 -0800 | [diff] [blame] | 1121 | printf("*** %s: EXT_CSD_REV parse error \"%s\"\n\n", ext_csd_path, sub.c_str()); |
Mark Salyzyn | 8c8130e | 2015-12-09 11:21:28 -0800 | [diff] [blame] | 1122 | return; |
| 1123 | } |
| 1124 | |
Mark Salyzyn | 290f4b9 | 2016-05-16 08:33:59 -0700 | [diff] [blame] | 1125 | static const char *ver_str[] = { |
| 1126 | "4.0", "4.1", "4.2", "4.3", "Obsolete", "4.41", "4.5", "5.0" |
| 1127 | }; |
Felipe Leme | d8b94e5 | 2016-12-08 10:21:44 -0800 | [diff] [blame] | 1128 | printf("rev 1.%d (MMC %s)\n", ext_csd_rev, |
| 1129 | (ext_csd_rev < (int)(sizeof(ver_str) / sizeof(ver_str[0]))) ? ver_str[ext_csd_rev] |
| 1130 | : "Unknown"); |
Mark Salyzyn | 8c8130e | 2015-12-09 11:21:28 -0800 | [diff] [blame] | 1131 | if (ext_csd_rev < 7) { |
Felipe Leme | d8b94e5 | 2016-12-08 10:21:44 -0800 | [diff] [blame] | 1132 | printf("\n"); |
Mark Salyzyn | 8c8130e | 2015-12-09 11:21:28 -0800 | [diff] [blame] | 1133 | return; |
| 1134 | } |
| 1135 | |
Mark Salyzyn | 290f4b9 | 2016-05-16 08:33:59 -0700 | [diff] [blame] | 1136 | if (buffer.length() < (EXT_PRE_EOL_INFO + sizeof(hex))) { |
Felipe Leme | d8b94e5 | 2016-12-08 10:21:44 -0800 | [diff] [blame] | 1137 | printf("*** %s: truncated content %zu\n\n", ext_csd_path, buffer.length()); |
Mark Salyzyn | 8c8130e | 2015-12-09 11:21:28 -0800 | [diff] [blame] | 1138 | return; |
| 1139 | } |
| 1140 | |
Mark Salyzyn | 290f4b9 | 2016-05-16 08:33:59 -0700 | [diff] [blame] | 1141 | int ext_pre_eol_info = 0; |
| 1142 | sub = buffer.substr(EXT_PRE_EOL_INFO, sizeof(hex)); |
| 1143 | if (sscanf(sub.c_str(), "%2x", &ext_pre_eol_info) != 1) { |
Felipe Leme | d8b94e5 | 2016-12-08 10:21:44 -0800 | [diff] [blame] | 1144 | printf("*** %s: PRE_EOL_INFO parse error \"%s\"\n\n", ext_csd_path, sub.c_str()); |
Mark Salyzyn | 8c8130e | 2015-12-09 11:21:28 -0800 | [diff] [blame] | 1145 | return; |
| 1146 | } |
Mark Salyzyn | 290f4b9 | 2016-05-16 08:33:59 -0700 | [diff] [blame] | 1147 | |
| 1148 | static const char *eol_str[] = { |
| 1149 | "Undefined", |
| 1150 | "Normal", |
| 1151 | "Warning (consumed 80% of reserve)", |
| 1152 | "Urgent (consumed 90% of reserve)" |
| 1153 | }; |
Felipe Leme | d8b94e5 | 2016-12-08 10:21:44 -0800 | [diff] [blame] | 1154 | printf( |
| 1155 | "PRE_EOL_INFO %d (MMC %s)\n", ext_pre_eol_info, |
Felipe Leme | 8f00ed0 | 2016-12-07 17:42:44 -0800 | [diff] [blame] | 1156 | eol_str[(ext_pre_eol_info < (int)(sizeof(eol_str) / sizeof(eol_str[0]))) ? ext_pre_eol_info |
| 1157 | : 0]); |
Mark Salyzyn | 8c8130e | 2015-12-09 11:21:28 -0800 | [diff] [blame] | 1158 | |
| 1159 | for (size_t lifetime = EXT_DEVICE_LIFE_TIME_EST_TYP_A; |
| 1160 | lifetime <= EXT_DEVICE_LIFE_TIME_EST_TYP_B; |
Mark Salyzyn | 290f4b9 | 2016-05-16 08:33:59 -0700 | [diff] [blame] | 1161 | lifetime += sizeof(hex)) { |
Mark Salyzyn | 8c8130e | 2015-12-09 11:21:28 -0800 | [diff] [blame] | 1162 | int ext_device_life_time_est; |
| 1163 | static const char *est_str[] = { |
| 1164 | "Undefined", |
| 1165 | "0-10% of device lifetime used", |
| 1166 | "10-20% of device lifetime used", |
| 1167 | "20-30% of device lifetime used", |
| 1168 | "30-40% of device lifetime used", |
| 1169 | "40-50% of device lifetime used", |
| 1170 | "50-60% of device lifetime used", |
| 1171 | "60-70% of device lifetime used", |
| 1172 | "70-80% of device lifetime used", |
| 1173 | "80-90% of device lifetime used", |
| 1174 | "90-100% of device lifetime used", |
| 1175 | "Exceeded the maximum estimated device lifetime", |
| 1176 | }; |
| 1177 | |
Mark Salyzyn | 290f4b9 | 2016-05-16 08:33:59 -0700 | [diff] [blame] | 1178 | if (buffer.length() < (lifetime + sizeof(hex))) { |
Felipe Leme | d8b94e5 | 2016-12-08 10:21:44 -0800 | [diff] [blame] | 1179 | printf("*** %s: truncated content %zu\n", ext_csd_path, buffer.length()); |
Mark Salyzyn | 8c8130e | 2015-12-09 11:21:28 -0800 | [diff] [blame] | 1180 | break; |
| 1181 | } |
| 1182 | |
| 1183 | ext_device_life_time_est = 0; |
Mark Salyzyn | 290f4b9 | 2016-05-16 08:33:59 -0700 | [diff] [blame] | 1184 | sub = buffer.substr(lifetime, sizeof(hex)); |
| 1185 | if (sscanf(sub.c_str(), "%2x", &ext_device_life_time_est) != 1) { |
Felipe Leme | d8b94e5 | 2016-12-08 10:21:44 -0800 | [diff] [blame] | 1186 | printf("*** %s: DEVICE_LIFE_TIME_EST_TYP_%c parse error \"%s\"\n", ext_csd_path, |
| 1187 | (unsigned)((lifetime - EXT_DEVICE_LIFE_TIME_EST_TYP_A) / sizeof(hex)) + 'A', |
| 1188 | sub.c_str()); |
Mark Salyzyn | 8c8130e | 2015-12-09 11:21:28 -0800 | [diff] [blame] | 1189 | continue; |
| 1190 | } |
Felipe Leme | d8b94e5 | 2016-12-08 10:21:44 -0800 | [diff] [blame] | 1191 | printf("DEVICE_LIFE_TIME_EST_TYP_%c %d (MMC %s)\n", |
| 1192 | (unsigned)((lifetime - EXT_DEVICE_LIFE_TIME_EST_TYP_A) / sizeof(hex)) + 'A', |
| 1193 | ext_device_life_time_est, |
| 1194 | est_str[(ext_device_life_time_est < (int)(sizeof(est_str) / sizeof(est_str[0]))) |
| 1195 | ? ext_device_life_time_est |
| 1196 | : 0]); |
Mark Salyzyn | 8c8130e | 2015-12-09 11:21:28 -0800 | [diff] [blame] | 1197 | } |
| 1198 | |
Felipe Leme | d8b94e5 | 2016-12-08 10:21:44 -0800 | [diff] [blame] | 1199 | printf("\n"); |
Mark Salyzyn | 8c8130e | 2015-12-09 11:21:28 -0800 | [diff] [blame] | 1200 | } |