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