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> |
Mark Salyzyn | 4eb1382 | 2017-01-12 13:57:51 -0800 | [diff] [blame] | 53 | #include <log/log.h> |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 54 | #include <private/android_filesystem_config.h> |
| 55 | |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 56 | #include "DumpstateInternal.h" |
Jeff Brown | 1dc94e3 | 2014-09-11 14:15:27 -0700 | [diff] [blame] | 57 | |
Felipe Leme | 47e9be2 | 2016-12-21 15:37:07 -0800 | [diff] [blame] | 58 | // TODO: remove once moved to namespace |
| 59 | using android::os::dumpstate::CommandOptions; |
| 60 | using android::os::dumpstate::DumpFileToFd; |
| 61 | using android::os::dumpstate::PropertiesHelper; |
| 62 | |
Brian Carlstrom | a332275 | 2017-03-19 17:48:01 -0700 | [diff] [blame] | 63 | // Keep in sync with |
| 64 | // frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java |
Felipe Leme | 6188412 | 2016-06-13 09:23:30 -0700 | [diff] [blame] | 65 | static const int TRACE_DUMP_TIMEOUT_MS = 10000; // 10 seconds |
| 66 | |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 67 | /* Most simple commands have 10 as timeout, so 5 is a good estimate */ |
| 68 | static const int32_t WEIGHT_FILE = 5; |
| 69 | |
Felipe Leme | e844a9d | 2016-09-21 15:01:39 -0700 | [diff] [blame] | 70 | // TODO: temporary variables and functions used during C++ refactoring |
| 71 | static Dumpstate& ds = Dumpstate::GetInstance(); |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 72 | 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] | 73 | const CommandOptions& options = CommandOptions::DEFAULT) { |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 74 | return ds.RunCommand(title, full_command, options); |
Felipe Leme | 678727a | 2016-09-21 17:22:11 -0700 | [diff] [blame] | 75 | } |
Felipe Leme | e844a9d | 2016-09-21 15:01:39 -0700 | [diff] [blame] | 76 | |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 77 | // Reasonable value for max stats. |
| 78 | static const int STATS_MAX_N_RUNS = 1000; |
| 79 | static const long STATS_MAX_AVERAGE = 100000; |
| 80 | |
Felipe Leme | bda15a0 | 2016-11-16 17:48:25 -0800 | [diff] [blame] | 81 | CommandOptions Dumpstate::DEFAULT_DUMPSYS = CommandOptions::WithTimeout(30).Build(); |
Felipe Leme | 30dbfa1 | 2016-09-02 12:43:26 -0700 | [diff] [blame] | 82 | |
Nandana Dutt | 197661d | 2018-11-16 16:40:21 +0000 | [diff] [blame] | 83 | // TODO(111441001): Default DumpOptions to sensible values. |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 84 | Dumpstate::Dumpstate(const std::string& version) |
Nandana Dutt | 197661d | 2018-11-16 16:40:21 +0000 | [diff] [blame] | 85 | : pid_(getpid()), |
| 86 | options_(new Dumpstate::DumpOptions()), |
| 87 | version_(version), |
| 88 | now_(time(nullptr)) { |
Felipe Leme | e844a9d | 2016-09-21 15:01:39 -0700 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | Dumpstate& Dumpstate::GetInstance() { |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 92 | static Dumpstate singleton_(android::base::GetProperty("dumpstate.version", VERSION_CURRENT)); |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 93 | return singleton_; |
Felipe Leme | e844a9d | 2016-09-21 15:01:39 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Nandana Dutt | a8470b8 | 2019-03-11 11:00:58 +0000 | [diff] [blame] | 96 | DurationReporter::DurationReporter(const std::string& title, bool logcat_only) |
| 97 | : title_(title), logcat_only_(logcat_only) { |
Felipe Leme | 678727a | 2016-09-21 17:22:11 -0700 | [diff] [blame] | 98 | if (!title_.empty()) { |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 99 | started_ = Nanotime(); |
Felipe Leme | 78f2c86 | 2015-12-21 09:55:22 -0800 | [diff] [blame] | 100 | } |
| 101 | } |
| 102 | |
| 103 | DurationReporter::~DurationReporter() { |
Felipe Leme | 678727a | 2016-09-21 17:22:11 -0700 | [diff] [blame] | 104 | if (!title_.empty()) { |
Nandana Dutt | b86f33b | 2019-03-14 11:04:22 +0000 | [diff] [blame] | 105 | float elapsed = (float)(Nanotime() - started_) / NANOS_PER_SEC; |
| 106 | if (elapsed < .5f) { |
| 107 | return; |
| 108 | } |
| 109 | MYLOGD("Duration of '%s': %.2fs\n", title_.c_str(), elapsed); |
Nandana Dutt | a8470b8 | 2019-03-11 11:00:58 +0000 | [diff] [blame] | 110 | if (logcat_only_) { |
| 111 | return; |
Felipe Leme | 608385d | 2016-02-01 10:35:38 -0800 | [diff] [blame] | 112 | } |
Nandana Dutt | a8470b8 | 2019-03-11 11:00:58 +0000 | [diff] [blame] | 113 | // Use "Yoda grammar" to make it easier to grep|sort sections. |
Nandana Dutt | b86f33b | 2019-03-14 11:04:22 +0000 | [diff] [blame] | 114 | printf("------ %.3fs was the duration of '%s' ------\n", elapsed, title_.c_str()); |
Felipe Leme | 78f2c86 | 2015-12-21 09:55:22 -0800 | [diff] [blame] | 115 | } |
| 116 | } |
| 117 | |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 118 | const int32_t Progress::kDefaultMax = 5000; |
| 119 | |
| 120 | Progress::Progress(const std::string& path) : Progress(Progress::kDefaultMax, 1.1, path) { |
| 121 | } |
| 122 | |
| 123 | Progress::Progress(int32_t initial_max, int32_t progress, float growth_factor) |
| 124 | : Progress(initial_max, growth_factor, "") { |
| 125 | progress_ = progress; |
| 126 | } |
| 127 | |
| 128 | Progress::Progress(int32_t initial_max, float growth_factor, const std::string& path) |
| 129 | : initial_max_(initial_max), |
| 130 | progress_(0), |
| 131 | max_(initial_max), |
| 132 | growth_factor_(growth_factor), |
| 133 | n_runs_(0), |
| 134 | average_max_(0), |
| 135 | path_(path) { |
| 136 | if (!path_.empty()) { |
| 137 | Load(); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | void Progress::Load() { |
| 142 | MYLOGD("Loading stats from %s\n", path_.c_str()); |
| 143 | std::string content; |
| 144 | if (!android::base::ReadFileToString(path_, &content)) { |
| 145 | MYLOGI("Could not read stats from %s; using max of %d\n", path_.c_str(), max_); |
| 146 | return; |
| 147 | } |
| 148 | if (content.empty()) { |
| 149 | MYLOGE("No stats (empty file) on %s; using max of %d\n", path_.c_str(), max_); |
| 150 | return; |
| 151 | } |
| 152 | std::vector<std::string> lines = android::base::Split(content, "\n"); |
| 153 | |
| 154 | if (lines.size() < 1) { |
| 155 | MYLOGE("Invalid stats on file %s: not enough lines (%d). Using max of %d\n", path_.c_str(), |
| 156 | (int)lines.size(), max_); |
| 157 | return; |
| 158 | } |
| 159 | char* ptr; |
| 160 | n_runs_ = strtol(lines[0].c_str(), &ptr, 10); |
| 161 | average_max_ = strtol(ptr, nullptr, 10); |
| 162 | if (n_runs_ <= 0 || average_max_ <= 0 || n_runs_ > STATS_MAX_N_RUNS || |
| 163 | average_max_ > STATS_MAX_AVERAGE) { |
| 164 | MYLOGE("Invalid stats line on file %s: %s\n", path_.c_str(), lines[0].c_str()); |
| 165 | initial_max_ = Progress::kDefaultMax; |
| 166 | } else { |
| 167 | initial_max_ = average_max_; |
| 168 | } |
| 169 | max_ = initial_max_; |
| 170 | |
| 171 | MYLOGI("Average max progress: %d in %d runs; estimated max: %d\n", average_max_, n_runs_, max_); |
| 172 | } |
| 173 | |
| 174 | void Progress::Save() { |
| 175 | int32_t total = n_runs_ * average_max_ + progress_; |
| 176 | int32_t runs = n_runs_ + 1; |
| 177 | int32_t average = floor(((float)total) / runs); |
| 178 | MYLOGI("Saving stats (total=%d, runs=%d, average=%d) on %s\n", total, runs, average, |
| 179 | path_.c_str()); |
| 180 | if (path_.empty()) { |
| 181 | return; |
| 182 | } |
| 183 | |
| 184 | std::string content = android::base::StringPrintf("%d %d\n", runs, average); |
| 185 | if (!android::base::WriteStringToFile(content, path_)) { |
| 186 | MYLOGE("Could not save stats on %s\n", path_.c_str()); |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | int32_t Progress::Get() const { |
| 191 | return progress_; |
| 192 | } |
| 193 | |
Vishnu Nair | 6921f80 | 2017-11-22 09:17:23 -0800 | [diff] [blame] | 194 | bool Progress::Inc(int32_t delta_sec) { |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 195 | bool changed = false; |
Vishnu Nair | 6921f80 | 2017-11-22 09:17:23 -0800 | [diff] [blame] | 196 | if (delta_sec >= 0) { |
| 197 | progress_ += delta_sec; |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 198 | if (progress_ > max_) { |
| 199 | int32_t old_max = max_; |
| 200 | max_ = floor((float)progress_ * growth_factor_); |
| 201 | MYLOGD("Adjusting max progress from %d to %d\n", old_max, max_); |
| 202 | changed = true; |
| 203 | } |
| 204 | } |
| 205 | return changed; |
| 206 | } |
| 207 | |
| 208 | int32_t Progress::GetMax() const { |
| 209 | return max_; |
| 210 | } |
| 211 | |
| 212 | int32_t Progress::GetInitialMax() const { |
| 213 | return initial_max_; |
| 214 | } |
| 215 | |
| 216 | void Progress::Dump(int fd, const std::string& prefix) const { |
| 217 | const char* pr = prefix.c_str(); |
| 218 | dprintf(fd, "%sprogress: %d\n", pr, progress_); |
| 219 | dprintf(fd, "%smax: %d\n", pr, max_); |
| 220 | dprintf(fd, "%sinitial_max: %d\n", pr, initial_max_); |
| 221 | dprintf(fd, "%sgrowth_factor: %0.2f\n", pr, growth_factor_); |
| 222 | dprintf(fd, "%spath: %s\n", pr, path_.c_str()); |
| 223 | dprintf(fd, "%sn_runs: %d\n", pr, n_runs_); |
| 224 | dprintf(fd, "%saverage_max: %d\n", pr, average_max_); |
| 225 | } |
| 226 | |
Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 227 | bool Dumpstate::IsZipping() const { |
| 228 | return zip_writer_ != nullptr; |
| 229 | } |
| 230 | |
Felipe Leme | 2b9b06c | 2016-10-14 09:13:06 -0700 | [diff] [blame] | 231 | std::string Dumpstate::GetPath(const std::string& suffix) const { |
Nandana Dutt | 979388e | 2018-11-30 16:48:55 +0000 | [diff] [blame] | 232 | return GetPath(bugreport_internal_dir_, suffix); |
| 233 | } |
| 234 | |
| 235 | std::string Dumpstate::GetPath(const std::string& directory, const std::string& suffix) const { |
| 236 | return android::base::StringPrintf("%s/%s-%s%s", directory.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))) { |
Nandana Dutt | 5c39003 | 2019-03-12 10:52:56 +0000 | [diff] [blame] | 283 | if (ds.IsUserConsentDenied()) { |
| 284 | MYLOGE( |
| 285 | "Returning early because user denied consent to share bugreport with calling app."); |
| 286 | closedir(d); |
| 287 | return; |
| 288 | } |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 289 | int pid; |
| 290 | int fd; |
| 291 | char cmdpath[255]; |
| 292 | char cmdline[255]; |
| 293 | |
| 294 | if (!(pid = atoi(de->d_name))) { |
| 295 | continue; |
| 296 | } |
| 297 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 298 | memset(cmdline, 0, sizeof(cmdline)); |
Mark Salyzyn | 0751efa | 2016-02-05 15:33:17 -0800 | [diff] [blame] | 299 | |
| 300 | snprintf(cmdpath, sizeof(cmdpath), "/proc/%d/cmdline", pid); |
| 301 | if ((fd = TEMP_FAILURE_RETRY(open(cmdpath, O_RDONLY | O_CLOEXEC))) >= 0) { |
| 302 | TEMP_FAILURE_RETRY(read(fd, cmdline, sizeof(cmdline) - 2)); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 303 | close(fd); |
Mark Salyzyn | 0751efa | 2016-02-05 15:33:17 -0800 | [diff] [blame] | 304 | if (cmdline[0]) { |
| 305 | helper(pid, cmdline, arg); |
| 306 | continue; |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | // if no cmdline, a kernel thread has comm |
| 311 | snprintf(cmdpath, sizeof(cmdpath), "/proc/%d/comm", pid); |
| 312 | if ((fd = TEMP_FAILURE_RETRY(open(cmdpath, O_RDONLY | O_CLOEXEC))) >= 0) { |
| 313 | TEMP_FAILURE_RETRY(read(fd, cmdline + 1, sizeof(cmdline) - 4)); |
| 314 | close(fd); |
| 315 | if (cmdline[1]) { |
| 316 | cmdline[0] = '['; |
| 317 | size_t len = strcspn(cmdline, "\f\b\r\n"); |
| 318 | cmdline[len] = ']'; |
| 319 | cmdline[len+1] = '\0'; |
| 320 | } |
| 321 | } |
| 322 | if (!cmdline[0]) { |
| 323 | strcpy(cmdline, "N/A"); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 324 | } |
Colin Cross | 0c22e8b | 2012-11-02 15:46:56 -0700 | [diff] [blame] | 325 | helper(pid, cmdline, arg); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | closedir(d); |
| 329 | } |
| 330 | |
Colin Cross | 0c22e8b | 2012-11-02 15:46:56 -0700 | [diff] [blame] | 331 | 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] | 332 | for_each_pid_func *func = (for_each_pid_func*) arg; |
Colin Cross | 0c22e8b | 2012-11-02 15:46:56 -0700 | [diff] [blame] | 333 | func(pid, cmdline); |
| 334 | } |
| 335 | |
| 336 | void for_each_pid(for_each_pid_func func, const char *header) { |
Felipe Leme | 8f00ed0 | 2016-12-07 17:42:44 -0800 | [diff] [blame] | 337 | std::string title = header == nullptr ? "for_each_pid" |
| 338 | : android::base::StringPrintf("for_each_pid(%s)", header); |
| 339 | DurationReporter duration_reporter(title); |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 340 | if (PropertiesHelper::IsDryRun()) return; |
Felipe Leme | d402e7d | 2016-08-03 09:22:27 -0700 | [diff] [blame] | 341 | |
Felipe Leme | 515eb0d | 2015-12-14 15:09:56 -0800 | [diff] [blame] | 342 | __for_each_pid(for_each_pid_helper, header, (void *) func); |
Colin Cross | 0c22e8b | 2012-11-02 15:46:56 -0700 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | static void for_each_tid_helper(int pid, const char *cmdline, void *arg) { |
| 346 | DIR *d; |
| 347 | struct dirent *de; |
| 348 | char taskpath[255]; |
Felipe Leme | 8620bb4 | 2015-11-10 11:04:45 -0800 | [diff] [blame] | 349 | for_each_tid_func *func = (for_each_tid_func *) arg; |
Colin Cross | 0c22e8b | 2012-11-02 15:46:56 -0700 | [diff] [blame] | 350 | |
Nick Kralevich | f0922cc | 2016-05-14 16:47:44 -0700 | [diff] [blame] | 351 | snprintf(taskpath, sizeof(taskpath), "/proc/%d/task", pid); |
Colin Cross | 0c22e8b | 2012-11-02 15:46:56 -0700 | [diff] [blame] | 352 | |
| 353 | if (!(d = opendir(taskpath))) { |
Felipe Leme | d8b94e5 | 2016-12-08 10:21:44 -0800 | [diff] [blame] | 354 | printf("Failed to open %s (%s)\n", taskpath, strerror(errno)); |
Colin Cross | 0c22e8b | 2012-11-02 15:46:56 -0700 | [diff] [blame] | 355 | return; |
| 356 | } |
| 357 | |
| 358 | func(pid, pid, cmdline); |
| 359 | |
| 360 | while ((de = readdir(d))) { |
Nandana Dutt | 5c39003 | 2019-03-12 10:52:56 +0000 | [diff] [blame] | 361 | if (ds.IsUserConsentDenied()) { |
| 362 | MYLOGE( |
| 363 | "Returning early because user denied consent to share bugreport with calling app."); |
| 364 | closedir(d); |
| 365 | return; |
| 366 | } |
Colin Cross | 0c22e8b | 2012-11-02 15:46:56 -0700 | [diff] [blame] | 367 | int tid; |
| 368 | int fd; |
| 369 | char commpath[255]; |
| 370 | char comm[255]; |
| 371 | |
| 372 | if (!(tid = atoi(de->d_name))) { |
| 373 | continue; |
| 374 | } |
| 375 | |
| 376 | if (tid == pid) |
| 377 | continue; |
| 378 | |
Nick Kralevich | f0922cc | 2016-05-14 16:47:44 -0700 | [diff] [blame] | 379 | snprintf(commpath, sizeof(commpath), "/proc/%d/comm", tid); |
Colin Cross | 1493a39 | 2012-11-07 11:25:31 -0800 | [diff] [blame] | 380 | memset(comm, 0, sizeof(comm)); |
Nick Kralevich | cd67e9f | 2015-03-19 11:30:59 -0700 | [diff] [blame] | 381 | if ((fd = TEMP_FAILURE_RETRY(open(commpath, O_RDONLY | O_CLOEXEC))) < 0) { |
Colin Cross | 0c22e8b | 2012-11-02 15:46:56 -0700 | [diff] [blame] | 382 | strcpy(comm, "N/A"); |
| 383 | } else { |
| 384 | char *c; |
Mark Salyzyn | 0751efa | 2016-02-05 15:33:17 -0800 | [diff] [blame] | 385 | TEMP_FAILURE_RETRY(read(fd, comm, sizeof(comm) - 2)); |
Colin Cross | 0c22e8b | 2012-11-02 15:46:56 -0700 | [diff] [blame] | 386 | close(fd); |
| 387 | |
| 388 | c = strrchr(comm, '\n'); |
| 389 | if (c) { |
| 390 | *c = '\0'; |
| 391 | } |
| 392 | } |
| 393 | func(pid, tid, comm); |
| 394 | } |
| 395 | |
| 396 | closedir(d); |
| 397 | } |
| 398 | |
| 399 | void for_each_tid(for_each_tid_func func, const char *header) { |
Felipe Leme | 8f00ed0 | 2016-12-07 17:42:44 -0800 | [diff] [blame] | 400 | std::string title = header == nullptr ? "for_each_tid" |
| 401 | : android::base::StringPrintf("for_each_tid(%s)", header); |
| 402 | DurationReporter duration_reporter(title); |
Felipe Leme | d8b94e5 | 2016-12-08 10:21:44 -0800 | [diff] [blame] | 403 | |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 404 | if (PropertiesHelper::IsDryRun()) return; |
Felipe Leme | d402e7d | 2016-08-03 09:22:27 -0700 | [diff] [blame] | 405 | |
Felipe Leme | 8620bb4 | 2015-11-10 11:04:45 -0800 | [diff] [blame] | 406 | __for_each_pid(for_each_tid_helper, header, (void *) func); |
Colin Cross | 0c22e8b | 2012-11-02 15:46:56 -0700 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | void show_wchan(int pid, int tid, const char *name) { |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 410 | if (PropertiesHelper::IsDryRun()) return; |
Felipe Leme | d402e7d | 2016-08-03 09:22:27 -0700 | [diff] [blame] | 411 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 412 | char path[255]; |
| 413 | char buffer[255]; |
Mark Salyzyn | 0751efa | 2016-02-05 15:33:17 -0800 | [diff] [blame] | 414 | int fd, ret, save_errno; |
Colin Cross | 0c22e8b | 2012-11-02 15:46:56 -0700 | [diff] [blame] | 415 | char name_buffer[255]; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 416 | |
| 417 | memset(buffer, 0, sizeof(buffer)); |
| 418 | |
Nick Kralevich | f0922cc | 2016-05-14 16:47:44 -0700 | [diff] [blame] | 419 | snprintf(path, sizeof(path), "/proc/%d/wchan", tid); |
Nick Kralevich | cd67e9f | 2015-03-19 11:30:59 -0700 | [diff] [blame] | 420 | if ((fd = TEMP_FAILURE_RETRY(open(path, O_RDONLY | O_CLOEXEC))) < 0) { |
Felipe Leme | d8b94e5 | 2016-12-08 10:21:44 -0800 | [diff] [blame] | 421 | printf("Failed to open '%s' (%s)\n", path, strerror(errno)); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 422 | return; |
| 423 | } |
| 424 | |
Mark Salyzyn | 0751efa | 2016-02-05 15:33:17 -0800 | [diff] [blame] | 425 | ret = TEMP_FAILURE_RETRY(read(fd, buffer, sizeof(buffer))); |
| 426 | save_errno = errno; |
| 427 | close(fd); |
| 428 | |
| 429 | if (ret < 0) { |
Felipe Leme | d8b94e5 | 2016-12-08 10:21:44 -0800 | [diff] [blame] | 430 | printf("Failed to read '%s' (%s)\n", path, strerror(save_errno)); |
Mark Salyzyn | 0751efa | 2016-02-05 15:33:17 -0800 | [diff] [blame] | 431 | return; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 432 | } |
| 433 | |
Colin Cross | 0c22e8b | 2012-11-02 15:46:56 -0700 | [diff] [blame] | 434 | snprintf(name_buffer, sizeof(name_buffer), "%*s%s", |
| 435 | pid == tid ? 0 : 3, "", name); |
| 436 | |
Felipe Leme | d8b94e5 | 2016-12-08 10:21:44 -0800 | [diff] [blame] | 437 | printf("%-7d %-32s %s\n", tid, name_buffer, buffer); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 438 | |
Mark Salyzyn | 0751efa | 2016-02-05 15:33:17 -0800 | [diff] [blame] | 439 | return; |
| 440 | } |
| 441 | |
| 442 | // print time in centiseconds |
| 443 | static void snprcent(char *buffer, size_t len, size_t spc, |
| 444 | unsigned long long time) { |
| 445 | static long hz; // cache discovered hz |
| 446 | |
| 447 | if (hz <= 0) { |
| 448 | hz = sysconf(_SC_CLK_TCK); |
| 449 | if (hz <= 0) { |
| 450 | hz = 1000; |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | // convert to centiseconds |
| 455 | time = (time * 100 + (hz / 2)) / hz; |
| 456 | |
| 457 | char str[16]; |
| 458 | |
| 459 | snprintf(str, sizeof(str), " %llu.%02u", |
| 460 | time / 100, (unsigned)(time % 100)); |
| 461 | size_t offset = strlen(buffer); |
| 462 | snprintf(buffer + offset, (len > offset) ? len - offset : 0, |
| 463 | "%*s", (spc > offset) ? (int)(spc - offset) : 0, str); |
| 464 | } |
| 465 | |
| 466 | // print permille as a percent |
| 467 | static void snprdec(char *buffer, size_t len, size_t spc, unsigned permille) { |
| 468 | char str[16]; |
| 469 | |
| 470 | snprintf(str, sizeof(str), " %u.%u%%", permille / 10, permille % 10); |
| 471 | size_t offset = strlen(buffer); |
| 472 | snprintf(buffer + offset, (len > offset) ? len - offset : 0, |
| 473 | "%*s", (spc > offset) ? (int)(spc - offset) : 0, str); |
| 474 | } |
| 475 | |
| 476 | void show_showtime(int pid, const char *name) { |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 477 | if (PropertiesHelper::IsDryRun()) return; |
Felipe Leme | d402e7d | 2016-08-03 09:22:27 -0700 | [diff] [blame] | 478 | |
Mark Salyzyn | 0751efa | 2016-02-05 15:33:17 -0800 | [diff] [blame] | 479 | char path[255]; |
| 480 | char buffer[1023]; |
| 481 | int fd, ret, save_errno; |
| 482 | |
| 483 | memset(buffer, 0, sizeof(buffer)); |
| 484 | |
Nick Kralevich | f0922cc | 2016-05-14 16:47:44 -0700 | [diff] [blame] | 485 | snprintf(path, sizeof(path), "/proc/%d/stat", pid); |
Mark Salyzyn | 0751efa | 2016-02-05 15:33:17 -0800 | [diff] [blame] | 486 | if ((fd = TEMP_FAILURE_RETRY(open(path, O_RDONLY | O_CLOEXEC))) < 0) { |
Felipe Leme | d8b94e5 | 2016-12-08 10:21:44 -0800 | [diff] [blame] | 487 | printf("Failed to open '%s' (%s)\n", path, strerror(errno)); |
Mark Salyzyn | 0751efa | 2016-02-05 15:33:17 -0800 | [diff] [blame] | 488 | return; |
| 489 | } |
| 490 | |
| 491 | ret = TEMP_FAILURE_RETRY(read(fd, buffer, sizeof(buffer))); |
| 492 | save_errno = errno; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 493 | close(fd); |
Mark Salyzyn | 0751efa | 2016-02-05 15:33:17 -0800 | [diff] [blame] | 494 | |
| 495 | if (ret < 0) { |
Felipe Leme | d8b94e5 | 2016-12-08 10:21:44 -0800 | [diff] [blame] | 496 | printf("Failed to read '%s' (%s)\n", path, strerror(save_errno)); |
Mark Salyzyn | 0751efa | 2016-02-05 15:33:17 -0800 | [diff] [blame] | 497 | return; |
| 498 | } |
| 499 | |
| 500 | // field 14 is utime |
| 501 | // field 15 is stime |
| 502 | // field 42 is iotime |
| 503 | unsigned long long utime = 0, stime = 0, iotime = 0; |
| 504 | if (sscanf(buffer, |
Mark Salyzyn | 791ddd3 | 2016-02-10 07:41:12 -0800 | [diff] [blame] | 505 | "%*u %*s %*s %*d %*d %*d %*d %*d %*d %*d %*d " |
| 506 | "%*d %*d %llu %llu %*d %*d %*d %*d %*d %*d " |
| 507 | "%*d %*d %*d %*d %*d %*d %*d %*d %*d %*d " |
| 508 | "%*d %*d %*d %*d %*d %*d %*d %*d %*d %llu ", |
Mark Salyzyn | 0751efa | 2016-02-05 15:33:17 -0800 | [diff] [blame] | 509 | &utime, &stime, &iotime) != 3) { |
| 510 | return; |
| 511 | } |
| 512 | |
| 513 | unsigned long long total = utime + stime; |
| 514 | if (!total) { |
| 515 | return; |
| 516 | } |
| 517 | |
| 518 | unsigned permille = (iotime * 1000 + (total / 2)) / total; |
| 519 | if (permille > 1000) { |
| 520 | permille = 1000; |
| 521 | } |
| 522 | |
| 523 | // try to beautify and stabilize columns at <80 characters |
| 524 | snprintf(buffer, sizeof(buffer), "%-6d%s", pid, name); |
| 525 | if ((name[0] != '[') || utime) { |
| 526 | snprcent(buffer, sizeof(buffer), 57, utime); |
| 527 | } |
| 528 | snprcent(buffer, sizeof(buffer), 65, stime); |
| 529 | if ((name[0] != '[') || iotime) { |
| 530 | snprcent(buffer, sizeof(buffer), 73, iotime); |
| 531 | } |
| 532 | if (iotime) { |
| 533 | snprdec(buffer, sizeof(buffer), 79, permille); |
| 534 | } |
Felipe Leme | d8b94e5 | 2016-12-08 10:21:44 -0800 | [diff] [blame] | 535 | puts(buffer); // adds a trailing newline |
Mark Salyzyn | 0751efa | 2016-02-05 15:33:17 -0800 | [diff] [blame] | 536 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 537 | return; |
| 538 | } |
| 539 | |
| 540 | void do_dmesg() { |
Felipe Leme | 78f2c86 | 2015-12-21 09:55:22 -0800 | [diff] [blame] | 541 | const char *title = "KERNEL LOG (dmesg)"; |
| 542 | DurationReporter duration_reporter(title); |
Felipe Leme | d8b94e5 | 2016-12-08 10:21:44 -0800 | [diff] [blame] | 543 | printf("------ %s ------\n", title); |
Felipe Leme | 78f2c86 | 2015-12-21 09:55:22 -0800 | [diff] [blame] | 544 | |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 545 | if (PropertiesHelper::IsDryRun()) return; |
Felipe Leme | d402e7d | 2016-08-03 09:22:27 -0700 | [diff] [blame] | 546 | |
Elliott Hughes | 5f87b31 | 2012-09-17 11:43:40 -0700 | [diff] [blame] | 547 | /* Get size of kernel buffer */ |
Yi Kong | 19d5c00 | 2018-07-20 13:39:55 -0700 | [diff] [blame] | 548 | int size = klogctl(KLOG_SIZE_BUFFER, nullptr, 0); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 549 | if (size <= 0) { |
Felipe Leme | d8b94e5 | 2016-12-08 10:21:44 -0800 | [diff] [blame] | 550 | printf("Unexpected klogctl return value: %d\n\n", size); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 551 | return; |
| 552 | } |
| 553 | char *buf = (char *) malloc(size + 1); |
Yi Kong | 19d5c00 | 2018-07-20 13:39:55 -0700 | [diff] [blame] | 554 | if (buf == nullptr) { |
Felipe Leme | d8b94e5 | 2016-12-08 10:21:44 -0800 | [diff] [blame] | 555 | printf("memory allocation failed\n\n"); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 556 | return; |
| 557 | } |
| 558 | int retval = klogctl(KLOG_READ_ALL, buf, size); |
| 559 | if (retval < 0) { |
Felipe Leme | d8b94e5 | 2016-12-08 10:21:44 -0800 | [diff] [blame] | 560 | printf("klogctl failure\n\n"); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 561 | free(buf); |
| 562 | return; |
| 563 | } |
| 564 | buf[retval] = '\0'; |
Felipe Leme | d8b94e5 | 2016-12-08 10:21:44 -0800 | [diff] [blame] | 565 | printf("%s\n\n", buf); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 566 | free(buf); |
| 567 | return; |
| 568 | } |
| 569 | |
| 570 | void do_showmap(int pid, const char *name) { |
| 571 | char title[255]; |
| 572 | char arg[255]; |
| 573 | |
Nick Kralevich | f0922cc | 2016-05-14 16:47:44 -0700 | [diff] [blame] | 574 | snprintf(title, sizeof(title), "SHOW MAP %d (%s)", pid, name); |
| 575 | snprintf(arg, sizeof(arg), "%d", pid); |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 576 | RunCommand(title, {"showmap", "-q", arg}, CommandOptions::AS_ROOT); |
Felipe Leme | bda15a0 | 2016-11-16 17:48:25 -0800 | [diff] [blame] | 577 | } |
| 578 | |
Felipe Leme | 678727a | 2016-09-21 17:22:11 -0700 | [diff] [blame] | 579 | int Dumpstate::DumpFile(const std::string& title, const std::string& path) { |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 580 | DurationReporter duration_reporter(title); |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 581 | |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 582 | int status = DumpFileToFd(STDOUT_FILENO, title, path); |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 583 | |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 584 | UpdateProgress(WEIGHT_FILE); |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 585 | |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 586 | return status; |
Christopher Ferris | 54bcc5f | 2015-02-10 12:15:01 -0800 | [diff] [blame] | 587 | } |
| 588 | |
Felipe Leme | 71a74ac | 2016-03-17 15:43:25 -0700 | [diff] [blame] | 589 | int read_file_as_long(const char *path, long int *output) { |
| 590 | int fd = TEMP_FAILURE_RETRY(open(path, O_RDONLY | O_NONBLOCK | O_CLOEXEC)); |
| 591 | if (fd < 0) { |
| 592 | int err = errno; |
| 593 | MYLOGE("Error opening file descriptor for %s: %s\n", path, strerror(err)); |
| 594 | return -1; |
| 595 | } |
| 596 | char buffer[50]; |
| 597 | ssize_t bytes_read = TEMP_FAILURE_RETRY(read(fd, buffer, sizeof(buffer))); |
| 598 | if (bytes_read == -1) { |
| 599 | MYLOGE("Error reading file %s: %s\n", path, strerror(errno)); |
| 600 | return -2; |
| 601 | } |
| 602 | if (bytes_read == 0) { |
| 603 | MYLOGE("File %s is empty\n", path); |
| 604 | return -3; |
| 605 | } |
| 606 | *output = atoi(buffer); |
| 607 | return 0; |
| 608 | } |
| 609 | |
Mark Salyzyn | 326842f | 2015-04-30 09:49:41 -0700 | [diff] [blame] | 610 | /* calls skip to gate calling dump_from_fd recursively |
| 611 | * in the specified directory. dump_from_fd defaults to |
| 612 | * dump_file_from_fd above when set to NULL. skip defaults |
| 613 | * to false when set to NULL. dump_from_fd will always be |
| 614 | * called with title NULL. |
| 615 | */ |
Felipe Leme | 678727a | 2016-09-21 17:22:11 -0700 | [diff] [blame] | 616 | int dump_files(const std::string& title, const char* dir, bool (*skip)(const char* path), |
| 617 | int (*dump_from_fd)(const char* title, const char* path, int fd)) { |
Felipe Leme | 78f2c86 | 2015-12-21 09:55:22 -0800 | [diff] [blame] | 618 | DurationReporter duration_reporter(title); |
Mark Salyzyn | 326842f | 2015-04-30 09:49:41 -0700 | [diff] [blame] | 619 | DIR *dirp; |
| 620 | struct dirent *d; |
Yi Kong | 19d5c00 | 2018-07-20 13:39:55 -0700 | [diff] [blame] | 621 | char *newpath = nullptr; |
Felipe Leme | 8620bb4 | 2015-11-10 11:04:45 -0800 | [diff] [blame] | 622 | const char *slash = "/"; |
Narayan Kamath | 6b9516c | 2017-10-27 11:15:51 +0100 | [diff] [blame] | 623 | int retval = 0; |
Mark Salyzyn | 326842f | 2015-04-30 09:49:41 -0700 | [diff] [blame] | 624 | |
Felipe Leme | 678727a | 2016-09-21 17:22:11 -0700 | [diff] [blame] | 625 | if (!title.empty()) { |
Felipe Leme | d8b94e5 | 2016-12-08 10:21:44 -0800 | [diff] [blame] | 626 | printf("------ %s (%s) ------\n", title.c_str(), dir); |
Mark Salyzyn | 326842f | 2015-04-30 09:49:41 -0700 | [diff] [blame] | 627 | } |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 628 | if (PropertiesHelper::IsDryRun()) return 0; |
Mark Salyzyn | 326842f | 2015-04-30 09:49:41 -0700 | [diff] [blame] | 629 | |
| 630 | if (dir[strlen(dir) - 1] == '/') { |
| 631 | ++slash; |
| 632 | } |
| 633 | dirp = opendir(dir); |
Yi Kong | 19d5c00 | 2018-07-20 13:39:55 -0700 | [diff] [blame] | 634 | if (dirp == nullptr) { |
Mark Salyzyn | 326842f | 2015-04-30 09:49:41 -0700 | [diff] [blame] | 635 | retval = -errno; |
Felipe Leme | 107a05f | 2016-03-08 15:11:15 -0800 | [diff] [blame] | 636 | MYLOGE("%s: %s\n", dir, strerror(errno)); |
Mark Salyzyn | 326842f | 2015-04-30 09:49:41 -0700 | [diff] [blame] | 637 | return retval; |
| 638 | } |
| 639 | |
| 640 | if (!dump_from_fd) { |
| 641 | dump_from_fd = dump_file_from_fd; |
| 642 | } |
Yi Kong | 19d5c00 | 2018-07-20 13:39:55 -0700 | [diff] [blame] | 643 | for (; ((d = readdir(dirp))); free(newpath), newpath = nullptr) { |
Mark Salyzyn | 326842f | 2015-04-30 09:49:41 -0700 | [diff] [blame] | 644 | if ((d->d_name[0] == '.') |
| 645 | && (((d->d_name[1] == '.') && (d->d_name[2] == '\0')) |
| 646 | || (d->d_name[1] == '\0'))) { |
| 647 | continue; |
| 648 | } |
| 649 | asprintf(&newpath, "%s%s%s%s", dir, slash, d->d_name, |
| 650 | (d->d_type == DT_DIR) ? "/" : ""); |
| 651 | if (!newpath) { |
| 652 | retval = -errno; |
| 653 | continue; |
| 654 | } |
| 655 | if (skip && (*skip)(newpath)) { |
| 656 | continue; |
| 657 | } |
| 658 | if (d->d_type == DT_DIR) { |
Felipe Leme | 678727a | 2016-09-21 17:22:11 -0700 | [diff] [blame] | 659 | int ret = dump_files("", newpath, skip, dump_from_fd); |
Mark Salyzyn | 326842f | 2015-04-30 09:49:41 -0700 | [diff] [blame] | 660 | if (ret < 0) { |
| 661 | retval = ret; |
| 662 | } |
| 663 | continue; |
| 664 | } |
Narayan Kamath | 6b9516c | 2017-10-27 11:15:51 +0100 | [diff] [blame] | 665 | android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(newpath, O_RDONLY | O_NONBLOCK | O_CLOEXEC))); |
| 666 | if (fd.get() < 0) { |
| 667 | retval = -1; |
Felipe Leme | d8b94e5 | 2016-12-08 10:21:44 -0800 | [diff] [blame] | 668 | printf("*** %s: %s\n", newpath, strerror(errno)); |
Mark Salyzyn | 326842f | 2015-04-30 09:49:41 -0700 | [diff] [blame] | 669 | continue; |
| 670 | } |
Yi Kong | 19d5c00 | 2018-07-20 13:39:55 -0700 | [diff] [blame] | 671 | (*dump_from_fd)(nullptr, newpath, fd.get()); |
Mark Salyzyn | 326842f | 2015-04-30 09:49:41 -0700 | [diff] [blame] | 672 | } |
| 673 | closedir(dirp); |
Felipe Leme | 678727a | 2016-09-21 17:22:11 -0700 | [diff] [blame] | 674 | if (!title.empty()) { |
Felipe Leme | d8b94e5 | 2016-12-08 10:21:44 -0800 | [diff] [blame] | 675 | printf("\n"); |
Mark Salyzyn | 326842f | 2015-04-30 09:49:41 -0700 | [diff] [blame] | 676 | } |
| 677 | return retval; |
| 678 | } |
| 679 | |
Christopher Ferris | 54bcc5f | 2015-02-10 12:15:01 -0800 | [diff] [blame] | 680 | /* fd must have been opened with the flag O_NONBLOCK. With this flag set, |
| 681 | * it's possible to avoid issues where opening the file itself can get |
| 682 | * stuck. |
| 683 | */ |
| 684 | 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] | 685 | if (PropertiesHelper::IsDryRun()) return 0; |
Felipe Leme | d402e7d | 2016-08-03 09:22:27 -0700 | [diff] [blame] | 686 | |
Christopher Ferris | 54bcc5f | 2015-02-10 12:15:01 -0800 | [diff] [blame] | 687 | int flags = fcntl(fd, F_GETFL); |
| 688 | if (flags == -1) { |
Felipe Leme | d8b94e5 | 2016-12-08 10:21:44 -0800 | [diff] [blame] | 689 | 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] | 690 | return -1; |
| 691 | } else if (!(flags & O_NONBLOCK)) { |
Felipe Leme | d8b94e5 | 2016-12-08 10:21:44 -0800 | [diff] [blame] | 692 | printf("*** %s: fd must have O_NONBLOCK set.\n", path); |
Christopher Ferris | 54bcc5f | 2015-02-10 12:15:01 -0800 | [diff] [blame] | 693 | return -1; |
| 694 | } |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 695 | return DumpFileFromFdToFd(title, path, fd, STDOUT_FILENO, PropertiesHelper::IsDryRun()); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 696 | } |
| 697 | |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 698 | int Dumpstate::RunCommand(const std::string& title, const std::vector<std::string>& full_command, |
| 699 | const CommandOptions& options) { |
| 700 | DurationReporter duration_reporter(title); |
| 701 | |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 702 | int status = RunCommandToFd(STDOUT_FILENO, title, full_command, options); |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 703 | |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 704 | /* TODO: for now we're simplifying the progress calculation by using the |
| 705 | * timeout as the weight. It's a good approximation for most cases, except when calling dumpsys, |
| 706 | * where its weight should be much higher proportionally to its timeout. |
| 707 | * Ideally, it should use a options.EstimatedDuration() instead...*/ |
| 708 | UpdateProgress(options.Timeout()); |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 709 | |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 710 | return status; |
| 711 | } |
| 712 | |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 713 | 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] | 714 | const CommandOptions& options, long dumpsysTimeoutMs) { |
| 715 | long timeout_ms = dumpsysTimeoutMs > 0 ? dumpsysTimeoutMs : options.TimeoutInMs(); |
| 716 | 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] | 717 | dumpsys.insert(dumpsys.end(), dumpsys_args.begin(), dumpsys_args.end()); |
Felipe Leme | 678727a | 2016-09-21 17:22:11 -0700 | [diff] [blame] | 718 | RunCommand(title, dumpsys, options); |
Felipe Leme | 30dbfa1 | 2016-09-02 12:43:26 -0700 | [diff] [blame] | 719 | } |
| 720 | |
Felipe Leme | 2628e9e | 2016-04-12 16:36:51 -0700 | [diff] [blame] | 721 | int open_socket(const char *service) { |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 722 | int s = android_get_control_socket(service); |
| 723 | if (s < 0) { |
Felipe Leme | 107a05f | 2016-03-08 15:11:15 -0800 | [diff] [blame] | 724 | MYLOGE("android_get_control_socket(%s): %s\n", service, strerror(errno)); |
Nandana Dutt | a344cb6 | 2019-02-22 15:12:35 +0000 | [diff] [blame] | 725 | return -1; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 726 | } |
Nick Kralevich | cd67e9f | 2015-03-19 11:30:59 -0700 | [diff] [blame] | 727 | fcntl(s, F_SETFD, FD_CLOEXEC); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 728 | if (listen(s, 4) < 0) { |
Felipe Leme | 107a05f | 2016-03-08 15:11:15 -0800 | [diff] [blame] | 729 | MYLOGE("listen(control socket): %s\n", strerror(errno)); |
Nandana Dutt | a344cb6 | 2019-02-22 15:12:35 +0000 | [diff] [blame] | 730 | return -1; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 731 | } |
| 732 | |
| 733 | struct sockaddr addr; |
| 734 | socklen_t alen = sizeof(addr); |
| 735 | int fd = accept(s, &addr, &alen); |
| 736 | if (fd < 0) { |
Felipe Leme | 107a05f | 2016-03-08 15:11:15 -0800 | [diff] [blame] | 737 | MYLOGE("accept(control socket): %s\n", strerror(errno)); |
Nandana Dutt | a344cb6 | 2019-02-22 15:12:35 +0000 | [diff] [blame] | 738 | return -1; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 739 | } |
| 740 | |
Felipe Leme | 2628e9e | 2016-04-12 16:36:51 -0700 | [diff] [blame] | 741 | return fd; |
| 742 | } |
| 743 | |
| 744 | /* redirect output to a service control socket */ |
Nandana Dutt | a344cb6 | 2019-02-22 15:12:35 +0000 | [diff] [blame] | 745 | bool redirect_to_socket(FILE* redirect, const char* service) { |
Felipe Leme | 2628e9e | 2016-04-12 16:36:51 -0700 | [diff] [blame] | 746 | int fd = open_socket(service); |
Nandana Dutt | a344cb6 | 2019-02-22 15:12:35 +0000 | [diff] [blame] | 747 | if (fd == -1) { |
| 748 | return false; |
| 749 | } |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 750 | fflush(redirect); |
Nandana Dutt | a344cb6 | 2019-02-22 15:12:35 +0000 | [diff] [blame] | 751 | // TODO: handle dup2 failure |
| 752 | TEMP_FAILURE_RETRY(dup2(fd, fileno(redirect))); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 753 | close(fd); |
Nandana Dutt | a344cb6 | 2019-02-22 15:12:35 +0000 | [diff] [blame] | 754 | return true; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 755 | } |
| 756 | |
Felipe Leme | 2628e9e | 2016-04-12 16:36:51 -0700 | [diff] [blame] | 757 | // 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] | 758 | void create_parent_dirs(const char *path) { |
Srinath Sridharan | fdf52d3 | 2016-02-01 15:50:22 -0800 | [diff] [blame] | 759 | char *chp = const_cast<char *> (path); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 760 | |
| 761 | /* skip initial slash */ |
| 762 | if (chp[0] == '/') |
| 763 | chp++; |
| 764 | |
| 765 | /* create leading directories, if necessary */ |
Felipe Leme | 111b9d0 | 2016-02-03 09:28:24 -0800 | [diff] [blame] | 766 | struct stat dir_stat; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 767 | while (chp && chp[0]) { |
| 768 | chp = strchr(chp, '/'); |
| 769 | if (chp) { |
| 770 | *chp = 0; |
Felipe Leme | 111b9d0 | 2016-02-03 09:28:24 -0800 | [diff] [blame] | 771 | if (stat(path, &dir_stat) == -1 || !S_ISDIR(dir_stat.st_mode)) { |
Felipe Leme | cbce55d | 2016-02-08 09:53:18 -0800 | [diff] [blame] | 772 | MYLOGI("Creating directory %s\n", path); |
Felipe Leme | 111b9d0 | 2016-02-03 09:28:24 -0800 | [diff] [blame] | 773 | if (mkdir(path, 0770)) { /* drwxrwx--- */ |
Felipe Leme | cbce55d | 2016-02-08 09:53:18 -0800 | [diff] [blame] | 774 | MYLOGE("Unable to create directory %s: %s\n", path, strerror(errno)); |
Felipe Leme | 111b9d0 | 2016-02-03 09:28:24 -0800 | [diff] [blame] | 775 | } else if (chown(path, AID_SHELL, AID_SHELL)) { |
Felipe Leme | cbce55d | 2016-02-08 09:53:18 -0800 | [diff] [blame] | 776 | 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] | 777 | } |
| 778 | } |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 779 | *chp++ = '/'; |
| 780 | } |
| 781 | } |
Felipe Leme | 111b9d0 | 2016-02-03 09:28:24 -0800 | [diff] [blame] | 782 | } |
| 783 | |
Nandana Dutt | a344cb6 | 2019-02-22 15:12:35 +0000 | [diff] [blame] | 784 | bool _redirect_to_file(FILE* redirect, char* path, int truncate_flag) { |
Felipe Leme | 111b9d0 | 2016-02-03 09:28:24 -0800 | [diff] [blame] | 785 | create_parent_dirs(path); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 786 | |
Felipe Leme | 0f3fb20 | 2016-06-10 17:10:53 -0700 | [diff] [blame] | 787 | int fd = TEMP_FAILURE_RETRY(open(path, |
| 788 | O_WRONLY | O_CREAT | truncate_flag | O_CLOEXEC | O_NOFOLLOW, |
Christopher Ferris | ff4a4dc | 2015-02-09 16:24:47 -0800 | [diff] [blame] | 789 | S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 790 | if (fd < 0) { |
Felipe Leme | 107a05f | 2016-03-08 15:11:15 -0800 | [diff] [blame] | 791 | MYLOGE("%s: %s\n", path, strerror(errno)); |
Nandana Dutt | a344cb6 | 2019-02-22 15:12:35 +0000 | [diff] [blame] | 792 | return false; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 793 | } |
| 794 | |
Christopher Ferris | ff4a4dc | 2015-02-09 16:24:47 -0800 | [diff] [blame] | 795 | TEMP_FAILURE_RETRY(dup2(fd, fileno(redirect))); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 796 | close(fd); |
Nandana Dutt | a344cb6 | 2019-02-22 15:12:35 +0000 | [diff] [blame] | 797 | return true; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 798 | } |
| 799 | |
Nandana Dutt | a344cb6 | 2019-02-22 15:12:35 +0000 | [diff] [blame] | 800 | bool redirect_to_file(FILE* redirect, char* path) { |
| 801 | return _redirect_to_file(redirect, path, O_TRUNC); |
Felipe Leme | 0f3fb20 | 2016-06-10 17:10:53 -0700 | [diff] [blame] | 802 | } |
| 803 | |
Nandana Dutt | a344cb6 | 2019-02-22 15:12:35 +0000 | [diff] [blame] | 804 | bool redirect_to_existing_file(FILE* redirect, char* path) { |
| 805 | return _redirect_to_file(redirect, path, O_APPEND); |
Felipe Leme | 0f3fb20 | 2016-06-10 17:10:53 -0700 | [diff] [blame] | 806 | } |
| 807 | |
Sreeram Ramachandran | 2b3bba3 | 2014-07-08 15:40:55 -0700 | [diff] [blame] | 808 | void dump_route_tables() { |
Felipe Leme | 78f2c86 | 2015-12-21 09:55:22 -0800 | [diff] [blame] | 809 | DurationReporter duration_reporter("DUMP ROUTE TABLES"); |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 810 | if (PropertiesHelper::IsDryRun()) return; |
Sreeram Ramachandran | 2b3bba3 | 2014-07-08 15:40:55 -0700 | [diff] [blame] | 811 | const char* const RT_TABLES_PATH = "/data/misc/net/rt_tables"; |
Felipe Leme | c7fe8fe | 2016-09-21 18:13:20 -0700 | [diff] [blame] | 812 | ds.DumpFile("RT_TABLES", RT_TABLES_PATH); |
Nick Kralevich | cd67e9f | 2015-03-19 11:30:59 -0700 | [diff] [blame] | 813 | FILE* fp = fopen(RT_TABLES_PATH, "re"); |
Sreeram Ramachandran | 2b3bba3 | 2014-07-08 15:40:55 -0700 | [diff] [blame] | 814 | if (!fp) { |
Felipe Leme | d8b94e5 | 2016-12-08 10:21:44 -0800 | [diff] [blame] | 815 | printf("*** %s: %s\n", RT_TABLES_PATH, strerror(errno)); |
Sreeram Ramachandran | 2b3bba3 | 2014-07-08 15:40:55 -0700 | [diff] [blame] | 816 | return; |
| 817 | } |
| 818 | char table[16]; |
| 819 | // Each line has an integer (the table number), a space, and a string (the table name). We only |
| 820 | // need the table number. It's a 32-bit unsigned number, so max 10 chars. Skip the table name. |
| 821 | // Add a fixed max limit so this doesn't go awry. |
| 822 | 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] | 823 | RunCommand("ROUTE TABLE IPv4", {"ip", "-4", "route", "show", "table", table}); |
| 824 | RunCommand("ROUTE TABLE IPv6", {"ip", "-6", "route", "show", "table", table}); |
Sreeram Ramachandran | 2b3bba3 | 2014-07-08 15:40:55 -0700 | [diff] [blame] | 825 | } |
| 826 | fclose(fp); |
| 827 | } |
Felipe Leme | 71bbfc5 | 2015-11-23 14:14:51 -0800 | [diff] [blame] | 828 | |
Felipe Leme | 71bbfc5 | 2015-11-23 14:14:51 -0800 | [diff] [blame] | 829 | // TODO: make this function thread safe if sections are generated in parallel. |
Vishnu Nair | 6921f80 | 2017-11-22 09:17:23 -0800 | [diff] [blame] | 830 | void Dumpstate::UpdateProgress(int32_t delta_sec) { |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 831 | if (progress_ == nullptr) { |
| 832 | MYLOGE("UpdateProgress: progress_ not set\n"); |
| 833 | return; |
| 834 | } |
Felipe Leme | 71bbfc5 | 2015-11-23 14:14:51 -0800 | [diff] [blame] | 835 | |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 836 | // Always update progess so stats can be tuned... |
Vishnu Nair | 6921f80 | 2017-11-22 09:17:23 -0800 | [diff] [blame] | 837 | bool max_changed = progress_->Inc(delta_sec); |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 838 | |
| 839 | // ...but only notifiy listeners when necessary. |
Nandana Dutt | 5fb117b | 2018-09-27 09:23:36 +0100 | [diff] [blame] | 840 | if (!options_->do_progress_updates) return; |
Felipe Leme | 71bbfc5 | 2015-11-23 14:14:51 -0800 | [diff] [blame] | 841 | |
Felipe Leme | 009ecbb | 2016-11-07 10:18:44 -0800 | [diff] [blame] | 842 | int progress = progress_->Get(); |
| 843 | int max = progress_->GetMax(); |
Felipe Leme | ad5f6c4 | 2015-11-30 14:26:46 -0800 | [diff] [blame] | 844 | |
| 845 | // adjusts max on the fly |
Felipe Leme | 009ecbb | 2016-11-07 10:18:44 -0800 | [diff] [blame] | 846 | if (max_changed && listener_ != nullptr) { |
| 847 | listener_->onMaxProgressUpdated(max); |
Felipe Leme | ad5f6c4 | 2015-11-30 14:26:46 -0800 | [diff] [blame] | 848 | } |
| 849 | |
Felipe Leme | 009ecbb | 2016-11-07 10:18:44 -0800 | [diff] [blame] | 850 | int32_t last_update_delta = progress - last_updated_progress_; |
| 851 | if (last_updated_progress_ > 0 && last_update_delta < update_progress_threshold_) { |
| 852 | return; |
| 853 | } |
| 854 | last_updated_progress_ = progress; |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 855 | |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 856 | if (control_socket_fd_ >= 0) { |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 857 | dprintf(control_socket_fd_, "PROGRESS:%d/%d\n", progress, max); |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 858 | fsync(control_socket_fd_); |
Felipe Leme | 02b7e00 | 2016-07-22 12:03:20 -0700 | [diff] [blame] | 859 | } |
| 860 | |
Nandana Dutt | babf6c7 | 2019-01-15 14:11:12 +0000 | [diff] [blame] | 861 | int percent = 100 * progress / max; |
Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 862 | if (listener_ != nullptr) { |
Nandana Dutt | babf6c7 | 2019-01-15 14:11:12 +0000 | [diff] [blame] | 863 | if (percent % 5 == 0) { |
| 864 | // We don't want to spam logcat, so only log multiples of 5. |
| 865 | MYLOGD("Setting progress (%s): %d/%d (%d%%)\n", listener_name_.c_str(), progress, max, |
| 866 | percent); |
Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 867 | } else { |
| 868 | // stderr is ignored on normal invocations, but useful when calling |
| 869 | // /system/bin/dumpstate directly for debuggging. |
Nandana Dutt | babf6c7 | 2019-01-15 14:11:12 +0000 | [diff] [blame] | 870 | fprintf(stderr, "Setting progress (%s): %d/%d (%d%%)\n", listener_name_.c_str(), |
| 871 | progress, max, percent); |
Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 872 | } |
Nandana Dutt | babf6c7 | 2019-01-15 14:11:12 +0000 | [diff] [blame] | 873 | // TODO(b/111441001): Remove in favor of onProgress |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 874 | listener_->onProgressUpdated(progress); |
Nandana Dutt | babf6c7 | 2019-01-15 14:11:12 +0000 | [diff] [blame] | 875 | |
| 876 | listener_->onProgress(percent); |
Felipe Leme | 71bbfc5 | 2015-11-23 14:14:51 -0800 | [diff] [blame] | 877 | } |
| 878 | } |
Felipe Leme | e338bf6 | 2015-12-07 14:03:50 -0800 | [diff] [blame] | 879 | |
Felipe Leme | bbaf3c1 | 2016-10-11 14:32:25 -0700 | [diff] [blame] | 880 | void Dumpstate::TakeScreenshot(const std::string& path) { |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 881 | const std::string& real_path = path.empty() ? screenshot_path_ : path; |
Felipe Leme | bbaf3c1 | 2016-10-11 14:32:25 -0700 | [diff] [blame] | 882 | int status = |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 883 | RunCommand("", {"/system/bin/screencap", "-p", real_path}, |
Felipe Leme | bbaf3c1 | 2016-10-11 14:32:25 -0700 | [diff] [blame] | 884 | CommandOptions::WithTimeout(10).Always().DropRoot().RedirectStderr().Build()); |
| 885 | if (status == 0) { |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 886 | MYLOGD("Screenshot saved on %s\n", real_path.c_str()); |
Felipe Leme | bbaf3c1 | 2016-10-11 14:32:25 -0700 | [diff] [blame] | 887 | } else { |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 888 | MYLOGE("Failed to take screenshot on %s\n", real_path.c_str()); |
Felipe Leme | bbaf3c1 | 2016-10-11 14:32:25 -0700 | [diff] [blame] | 889 | } |
Felipe Leme | e338bf6 | 2015-12-07 14:03:50 -0800 | [diff] [blame] | 890 | } |
Mark Salyzyn | f55d402 | 2015-12-11 07:32:31 -0800 | [diff] [blame] | 891 | |
Felipe Leme | 0c80cf0 | 2016-01-05 13:25:34 -0800 | [diff] [blame] | 892 | bool is_dir(const char* pathname) { |
| 893 | struct stat info; |
| 894 | if (stat(pathname, &info) == -1) { |
| 895 | return false; |
| 896 | } |
| 897 | return S_ISDIR(info.st_mode); |
| 898 | } |
| 899 | |
| 900 | time_t get_mtime(int fd, time_t default_mtime) { |
| 901 | struct stat info; |
| 902 | if (fstat(fd, &info) == -1) { |
| 903 | return default_mtime; |
| 904 | } |
| 905 | return info.st_mtime; |
| 906 | } |
| 907 | |
Mark Salyzyn | 8c8130e | 2015-12-09 11:21:28 -0800 | [diff] [blame] | 908 | void dump_emmc_ecsd(const char *ext_csd_path) { |
Mark Salyzyn | 290f4b9 | 2016-05-16 08:33:59 -0700 | [diff] [blame] | 909 | // List of interesting offsets |
Mark Salyzyn | 8c8130e | 2015-12-09 11:21:28 -0800 | [diff] [blame] | 910 | struct hex { |
| 911 | char str[2]; |
Mark Salyzyn | 8c8130e | 2015-12-09 11:21:28 -0800 | [diff] [blame] | 912 | }; |
Mark Salyzyn | 290f4b9 | 2016-05-16 08:33:59 -0700 | [diff] [blame] | 913 | static const size_t EXT_CSD_REV = 192 * sizeof(hex); |
| 914 | static const size_t EXT_PRE_EOL_INFO = 267 * sizeof(hex); |
| 915 | static const size_t EXT_DEVICE_LIFE_TIME_EST_TYP_A = 268 * sizeof(hex); |
| 916 | static const size_t EXT_DEVICE_LIFE_TIME_EST_TYP_B = 269 * sizeof(hex); |
| 917 | |
| 918 | std::string buffer; |
| 919 | if (!android::base::ReadFileToString(ext_csd_path, &buffer)) { |
| 920 | return; |
| 921 | } |
Mark Salyzyn | 8c8130e | 2015-12-09 11:21:28 -0800 | [diff] [blame] | 922 | |
Felipe Leme | d8b94e5 | 2016-12-08 10:21:44 -0800 | [diff] [blame] | 923 | printf("------ %s Extended CSD ------\n", ext_csd_path); |
Mark Salyzyn | 8c8130e | 2015-12-09 11:21:28 -0800 | [diff] [blame] | 924 | |
Mark Salyzyn | 290f4b9 | 2016-05-16 08:33:59 -0700 | [diff] [blame] | 925 | if (buffer.length() < (EXT_CSD_REV + sizeof(hex))) { |
Felipe Leme | d8b94e5 | 2016-12-08 10:21:44 -0800 | [diff] [blame] | 926 | printf("*** %s: truncated content %zu\n\n", ext_csd_path, buffer.length()); |
Mark Salyzyn | 8c8130e | 2015-12-09 11:21:28 -0800 | [diff] [blame] | 927 | return; |
| 928 | } |
| 929 | |
Mark Salyzyn | 290f4b9 | 2016-05-16 08:33:59 -0700 | [diff] [blame] | 930 | int ext_csd_rev = 0; |
| 931 | std::string sub = buffer.substr(EXT_CSD_REV, sizeof(hex)); |
| 932 | if (sscanf(sub.c_str(), "%2x", &ext_csd_rev) != 1) { |
Felipe Leme | d8b94e5 | 2016-12-08 10:21:44 -0800 | [diff] [blame] | 933 | 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] | 934 | return; |
| 935 | } |
| 936 | |
Mark Salyzyn | 290f4b9 | 2016-05-16 08:33:59 -0700 | [diff] [blame] | 937 | static const char *ver_str[] = { |
| 938 | "4.0", "4.1", "4.2", "4.3", "Obsolete", "4.41", "4.5", "5.0" |
| 939 | }; |
Felipe Leme | d8b94e5 | 2016-12-08 10:21:44 -0800 | [diff] [blame] | 940 | printf("rev 1.%d (MMC %s)\n", ext_csd_rev, |
| 941 | (ext_csd_rev < (int)(sizeof(ver_str) / sizeof(ver_str[0]))) ? ver_str[ext_csd_rev] |
| 942 | : "Unknown"); |
Mark Salyzyn | 8c8130e | 2015-12-09 11:21:28 -0800 | [diff] [blame] | 943 | if (ext_csd_rev < 7) { |
Felipe Leme | d8b94e5 | 2016-12-08 10:21:44 -0800 | [diff] [blame] | 944 | printf("\n"); |
Mark Salyzyn | 8c8130e | 2015-12-09 11:21:28 -0800 | [diff] [blame] | 945 | return; |
| 946 | } |
| 947 | |
Mark Salyzyn | 290f4b9 | 2016-05-16 08:33:59 -0700 | [diff] [blame] | 948 | if (buffer.length() < (EXT_PRE_EOL_INFO + sizeof(hex))) { |
Felipe Leme | d8b94e5 | 2016-12-08 10:21:44 -0800 | [diff] [blame] | 949 | printf("*** %s: truncated content %zu\n\n", ext_csd_path, buffer.length()); |
Mark Salyzyn | 8c8130e | 2015-12-09 11:21:28 -0800 | [diff] [blame] | 950 | return; |
| 951 | } |
| 952 | |
Mark Salyzyn | 290f4b9 | 2016-05-16 08:33:59 -0700 | [diff] [blame] | 953 | int ext_pre_eol_info = 0; |
| 954 | sub = buffer.substr(EXT_PRE_EOL_INFO, sizeof(hex)); |
| 955 | if (sscanf(sub.c_str(), "%2x", &ext_pre_eol_info) != 1) { |
Felipe Leme | d8b94e5 | 2016-12-08 10:21:44 -0800 | [diff] [blame] | 956 | 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] | 957 | return; |
| 958 | } |
Mark Salyzyn | 290f4b9 | 2016-05-16 08:33:59 -0700 | [diff] [blame] | 959 | |
| 960 | static const char *eol_str[] = { |
| 961 | "Undefined", |
| 962 | "Normal", |
| 963 | "Warning (consumed 80% of reserve)", |
| 964 | "Urgent (consumed 90% of reserve)" |
| 965 | }; |
Felipe Leme | d8b94e5 | 2016-12-08 10:21:44 -0800 | [diff] [blame] | 966 | printf( |
| 967 | "PRE_EOL_INFO %d (MMC %s)\n", ext_pre_eol_info, |
Felipe Leme | 8f00ed0 | 2016-12-07 17:42:44 -0800 | [diff] [blame] | 968 | eol_str[(ext_pre_eol_info < (int)(sizeof(eol_str) / sizeof(eol_str[0]))) ? ext_pre_eol_info |
| 969 | : 0]); |
Mark Salyzyn | 8c8130e | 2015-12-09 11:21:28 -0800 | [diff] [blame] | 970 | |
| 971 | for (size_t lifetime = EXT_DEVICE_LIFE_TIME_EST_TYP_A; |
| 972 | lifetime <= EXT_DEVICE_LIFE_TIME_EST_TYP_B; |
Mark Salyzyn | 290f4b9 | 2016-05-16 08:33:59 -0700 | [diff] [blame] | 973 | lifetime += sizeof(hex)) { |
Mark Salyzyn | 8c8130e | 2015-12-09 11:21:28 -0800 | [diff] [blame] | 974 | int ext_device_life_time_est; |
| 975 | static const char *est_str[] = { |
| 976 | "Undefined", |
| 977 | "0-10% of device lifetime used", |
| 978 | "10-20% of device lifetime used", |
| 979 | "20-30% of device lifetime used", |
| 980 | "30-40% of device lifetime used", |
| 981 | "40-50% of device lifetime used", |
| 982 | "50-60% of device lifetime used", |
| 983 | "60-70% of device lifetime used", |
| 984 | "70-80% of device lifetime used", |
| 985 | "80-90% of device lifetime used", |
| 986 | "90-100% of device lifetime used", |
| 987 | "Exceeded the maximum estimated device lifetime", |
| 988 | }; |
| 989 | |
Mark Salyzyn | 290f4b9 | 2016-05-16 08:33:59 -0700 | [diff] [blame] | 990 | if (buffer.length() < (lifetime + sizeof(hex))) { |
Felipe Leme | d8b94e5 | 2016-12-08 10:21:44 -0800 | [diff] [blame] | 991 | printf("*** %s: truncated content %zu\n", ext_csd_path, buffer.length()); |
Mark Salyzyn | 8c8130e | 2015-12-09 11:21:28 -0800 | [diff] [blame] | 992 | break; |
| 993 | } |
| 994 | |
| 995 | ext_device_life_time_est = 0; |
Mark Salyzyn | 290f4b9 | 2016-05-16 08:33:59 -0700 | [diff] [blame] | 996 | sub = buffer.substr(lifetime, sizeof(hex)); |
| 997 | if (sscanf(sub.c_str(), "%2x", &ext_device_life_time_est) != 1) { |
Felipe Leme | d8b94e5 | 2016-12-08 10:21:44 -0800 | [diff] [blame] | 998 | printf("*** %s: DEVICE_LIFE_TIME_EST_TYP_%c parse error \"%s\"\n", ext_csd_path, |
| 999 | (unsigned)((lifetime - EXT_DEVICE_LIFE_TIME_EST_TYP_A) / sizeof(hex)) + 'A', |
| 1000 | sub.c_str()); |
Mark Salyzyn | 8c8130e | 2015-12-09 11:21:28 -0800 | [diff] [blame] | 1001 | continue; |
| 1002 | } |
Felipe Leme | d8b94e5 | 2016-12-08 10:21:44 -0800 | [diff] [blame] | 1003 | printf("DEVICE_LIFE_TIME_EST_TYP_%c %d (MMC %s)\n", |
| 1004 | (unsigned)((lifetime - EXT_DEVICE_LIFE_TIME_EST_TYP_A) / sizeof(hex)) + 'A', |
| 1005 | ext_device_life_time_est, |
| 1006 | est_str[(ext_device_life_time_est < (int)(sizeof(est_str) / sizeof(est_str[0]))) |
| 1007 | ? ext_device_life_time_est |
| 1008 | : 0]); |
Mark Salyzyn | 8c8130e | 2015-12-09 11:21:28 -0800 | [diff] [blame] | 1009 | } |
| 1010 | |
Felipe Leme | d8b94e5 | 2016-12-08 10:21:44 -0800 | [diff] [blame] | 1011 | printf("\n"); |
Mark Salyzyn | 8c8130e | 2015-12-09 11:21:28 -0800 | [diff] [blame] | 1012 | } |