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