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