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