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 | |
| 17 | #include <dirent.h> |
| 18 | #include <errno.h> |
| 19 | #include <fcntl.h> |
Felipe Leme | e184f66 | 2016-10-27 10:04:47 -0700 | [diff] [blame] | 20 | #include <libgen.h> |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 21 | #include <limits.h> |
| 22 | #include <poll.h> |
| 23 | #include <signal.h> |
| 24 | #include <stdarg.h> |
| 25 | #include <stdio.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <string.h> |
Felipe Leme | cf6a8b4 | 2016-03-11 10:38:19 -0800 | [diff] [blame] | 28 | #include <sys/capability.h> |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 29 | #include <sys/inotify.h> |
Felipe Leme | e184f66 | 2016-10-27 10:04:47 -0700 | [diff] [blame] | 30 | #include <sys/klog.h> |
| 31 | #include <sys/prctl.h> |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 32 | #include <sys/stat.h> |
| 33 | #include <sys/time.h> |
| 34 | #include <sys/wait.h> |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 35 | #include <time.h> |
| 36 | #include <unistd.h> |
Felipe Leme | e184f66 | 2016-10-27 10:04:47 -0700 | [diff] [blame] | 37 | #include <string> |
Felipe Leme | 36b3f6f | 2015-11-19 15:41:04 -0800 | [diff] [blame] | 38 | #include <vector> |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 39 | |
Felipe Leme | 71bbfc5 | 2015-11-23 14:14:51 -0800 | [diff] [blame] | 40 | #define LOG_TAG "dumpstate" |
Mark Salyzyn | 261a733 | 2016-05-24 12:38:40 -0700 | [diff] [blame] | 41 | |
Mark Salyzyn | 290f4b9 | 2016-05-16 08:33:59 -0700 | [diff] [blame] | 42 | #include <android-base/file.h> |
Felipe Leme | 96c2bbb | 2016-09-26 09:21:21 -0700 | [diff] [blame] | 43 | #include <android-base/properties.h> |
Felipe Leme | 2b9b06c | 2016-10-14 09:13:06 -0700 | [diff] [blame] | 44 | #include <android-base/stringprintf.h> |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 45 | #include <cutils/debugger.h> |
Felipe Leme | 71bbfc5 | 2015-11-23 14:14:51 -0800 | [diff] [blame] | 46 | #include <cutils/log.h> |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 47 | #include <cutils/properties.h> |
| 48 | #include <cutils/sockets.h> |
| 49 | #include <private/android_filesystem_config.h> |
| 50 | |
Robert Craig | 9579837 | 2013-04-04 06:33:10 -0400 | [diff] [blame] | 51 | #include <selinux/android.h> |
| 52 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 53 | #include "dumpstate.h" |
| 54 | |
Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 55 | #define SU_PATH "/system/xbin/su" |
| 56 | |
Jeff Brown | 1dc94e3 | 2014-09-11 14:15:27 -0700 | [diff] [blame] | 57 | static const int64_t NANOS_PER_SEC = 1000000000; |
| 58 | |
Felipe Leme | 6188412 | 2016-06-13 09:23:30 -0700 | [diff] [blame] | 59 | static const int TRACE_DUMP_TIMEOUT_MS = 10000; // 10 seconds |
| 60 | |
Felipe Leme | e844a9d | 2016-09-21 15:01:39 -0700 | [diff] [blame] | 61 | // TODO: temporary variables and functions used during C++ refactoring |
| 62 | static Dumpstate& ds = Dumpstate::GetInstance(); |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 63 | 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] | 64 | const CommandOptions& options = CommandOptions::DEFAULT) { |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 65 | return ds.RunCommand(title, full_command, options); |
Felipe Leme | 678727a | 2016-09-21 17:22:11 -0700 | [diff] [blame] | 66 | } |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 67 | static bool IsDryRun() { |
| 68 | return Dumpstate::GetInstance().IsDryRun(); |
| 69 | } |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 70 | static void UpdateProgress(int delta) { |
| 71 | ds.UpdateProgress(delta); |
| 72 | } |
Felipe Leme | e844a9d | 2016-09-21 15:01:39 -0700 | [diff] [blame] | 73 | |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 74 | /* list of native processes to include in the native dumps */ |
Andy Hung | 5c04e74 | 2016-04-13 19:35:34 -0700 | [diff] [blame] | 75 | // This matches the /proc/pid/exe link instead of /proc/pid/cmdline. |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 76 | static const char* native_processes_to_dump[] = { |
Andy Hung | 9609bbd | 2015-12-15 12:42:50 -0800 | [diff] [blame] | 77 | "/system/bin/audioserver", |
Chien-Yu Chen | f5248da | 2016-01-28 14:23:03 -0800 | [diff] [blame] | 78 | "/system/bin/cameraserver", |
James Dong | 1fc4f80 | 2012-09-10 16:08:48 -0700 | [diff] [blame] | 79 | "/system/bin/drmserver", |
Andy Hung | 5c04e74 | 2016-04-13 19:35:34 -0700 | [diff] [blame] | 80 | "/system/bin/mediacodec", // media.codec |
| 81 | "/system/bin/mediadrmserver", |
| 82 | "/system/bin/mediaextractor", // media.extractor |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 83 | "/system/bin/mediaserver", |
| 84 | "/system/bin/sdcard", |
| 85 | "/system/bin/surfaceflinger", |
keunyoung | d907b32 | 2015-10-16 15:21:43 -0700 | [diff] [blame] | 86 | "/system/bin/vehicle_network_service", |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 87 | NULL, |
| 88 | }; |
| 89 | |
Felipe Leme | e844a9d | 2016-09-21 15:01:39 -0700 | [diff] [blame] | 90 | /* Most simple commands have 10 as timeout, so 5 is a good estimate */ |
| 91 | static const int WEIGHT_FILE = 5; |
| 92 | |
Felipe Leme | 30dbfa1 | 2016-09-02 12:43:26 -0700 | [diff] [blame] | 93 | CommandOptions CommandOptions::DEFAULT = CommandOptions::WithTimeout(10).Build(); |
| 94 | CommandOptions CommandOptions::DEFAULT_DUMPSYS = CommandOptions::WithTimeout(30).Build(); |
| 95 | CommandOptions CommandOptions::AS_ROOT_5 = CommandOptions::WithTimeout(5).AsRoot().Build(); |
| 96 | CommandOptions CommandOptions::AS_ROOT_10 = CommandOptions::WithTimeout(10).AsRoot().Build(); |
| 97 | CommandOptions CommandOptions::AS_ROOT_20 = CommandOptions::WithTimeout(20).AsRoot().Build(); |
| 98 | |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 99 | CommandOptions::CommandOptionsBuilder::CommandOptionsBuilder(long timeout) : values(timeout) { |
Felipe Leme | 30dbfa1 | 2016-09-02 12:43:26 -0700 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | CommandOptions::CommandOptionsBuilder& CommandOptions::CommandOptionsBuilder::Always() { |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 103 | values.always_ = true; |
Felipe Leme | 30dbfa1 | 2016-09-02 12:43:26 -0700 | [diff] [blame] | 104 | return *this; |
| 105 | } |
| 106 | |
| 107 | CommandOptions::CommandOptionsBuilder& CommandOptions::CommandOptionsBuilder::AsRoot() { |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 108 | values.root_mode_ = SU_ROOT; |
Felipe Leme | 30dbfa1 | 2016-09-02 12:43:26 -0700 | [diff] [blame] | 109 | return *this; |
| 110 | } |
| 111 | |
| 112 | CommandOptions::CommandOptionsBuilder& CommandOptions::CommandOptionsBuilder::DropRoot() { |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 113 | values.root_mode_ = DROP_ROOT; |
Felipe Leme | 30dbfa1 | 2016-09-02 12:43:26 -0700 | [diff] [blame] | 114 | return *this; |
| 115 | } |
| 116 | |
| 117 | CommandOptions::CommandOptionsBuilder& CommandOptions::CommandOptionsBuilder::RedirectStderr() { |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 118 | values.stdout_mode_ = REDIRECT_TO_STDERR; |
Felipe Leme | 30dbfa1 | 2016-09-02 12:43:26 -0700 | [diff] [blame] | 119 | return *this; |
| 120 | } |
| 121 | |
| 122 | CommandOptions::CommandOptionsBuilder& CommandOptions::CommandOptionsBuilder::Log( |
| 123 | const std::string& message) { |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 124 | values.logging_message_ = message; |
Felipe Leme | 30dbfa1 | 2016-09-02 12:43:26 -0700 | [diff] [blame] | 125 | return *this; |
| 126 | } |
| 127 | |
| 128 | CommandOptions CommandOptions::CommandOptionsBuilder::Build() { |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 129 | return CommandOptions(values); |
Felipe Leme | 30dbfa1 | 2016-09-02 12:43:26 -0700 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | CommandOptions::CommandOptionsValues::CommandOptionsValues(long timeout) |
Felipe Leme | b0f669d | 2016-09-26 18:26:11 -0700 | [diff] [blame] | 133 | : timeout_(timeout), |
| 134 | always_(false), |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 135 | root_mode_(DONT_DROP_ROOT), |
| 136 | stdout_mode_(NORMAL_STDOUT), |
| 137 | logging_message_("") { |
Felipe Leme | 30dbfa1 | 2016-09-02 12:43:26 -0700 | [diff] [blame] | 138 | } |
| 139 | |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 140 | CommandOptions::CommandOptions(const CommandOptionsValues& values) : values(values) { |
Felipe Leme | 30dbfa1 | 2016-09-02 12:43:26 -0700 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | long CommandOptions::Timeout() const { |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 144 | return values.timeout_; |
Felipe Leme | 30dbfa1 | 2016-09-02 12:43:26 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | bool CommandOptions::Always() const { |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 148 | return values.always_; |
Felipe Leme | 30dbfa1 | 2016-09-02 12:43:26 -0700 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | RootMode CommandOptions::RootMode() const { |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 152 | return values.root_mode_; |
Felipe Leme | 30dbfa1 | 2016-09-02 12:43:26 -0700 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | StdoutMode CommandOptions::StdoutMode() const { |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 156 | return values.stdout_mode_; |
Felipe Leme | 30dbfa1 | 2016-09-02 12:43:26 -0700 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | std::string CommandOptions::LoggingMessage() const { |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 160 | return values.logging_message_; |
Felipe Leme | 30dbfa1 | 2016-09-02 12:43:26 -0700 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | CommandOptions::CommandOptionsBuilder CommandOptions::WithTimeout(long timeout) { |
| 164 | return CommandOptions::CommandOptionsBuilder(timeout); |
| 165 | } |
| 166 | |
Felipe Leme | d071c68 | 2016-10-20 16:48:00 -0700 | [diff] [blame] | 167 | Dumpstate::Dumpstate(const std::string& version, bool dry_run, const std::string& build_type) |
Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 168 | : pid_(getpid()), |
| 169 | version_(version), |
| 170 | now_(time(nullptr)), |
| 171 | dry_run_(dry_run), |
| 172 | build_type_(build_type) { |
Felipe Leme | e844a9d | 2016-09-21 15:01:39 -0700 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | Dumpstate& Dumpstate::GetInstance() { |
Felipe Leme | d071c68 | 2016-10-20 16:48:00 -0700 | [diff] [blame] | 176 | static Dumpstate singleton_(android::base::GetProperty("dumpstate.version", VERSION_CURRENT), |
| 177 | android::base::GetBoolProperty("dumpstate.dry_run", false), |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 178 | android::base::GetProperty("ro.build.type", "(unknown)")); |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 179 | return singleton_; |
Felipe Leme | e844a9d | 2016-09-21 15:01:39 -0700 | [diff] [blame] | 180 | } |
| 181 | |
Felipe Leme | 678727a | 2016-09-21 17:22:11 -0700 | [diff] [blame] | 182 | DurationReporter::DurationReporter(const std::string& title) : DurationReporter(title, stdout) { |
| 183 | } |
Felipe Leme | 608385d | 2016-02-01 10:35:38 -0800 | [diff] [blame] | 184 | |
Felipe Leme | 678727a | 2016-09-21 17:22:11 -0700 | [diff] [blame] | 185 | DurationReporter::DurationReporter(const std::string& title, FILE* out) : title_(title), out_(out) { |
| 186 | if (!title_.empty()) { |
Felipe Leme | b0f669d | 2016-09-26 18:26:11 -0700 | [diff] [blame] | 187 | started_ = DurationReporter::Nanotime(); |
Felipe Leme | 78f2c86 | 2015-12-21 09:55:22 -0800 | [diff] [blame] | 188 | } |
| 189 | } |
| 190 | |
| 191 | DurationReporter::~DurationReporter() { |
Felipe Leme | 678727a | 2016-09-21 17:22:11 -0700 | [diff] [blame] | 192 | if (!title_.empty()) { |
Felipe Leme | b0f669d | 2016-09-26 18:26:11 -0700 | [diff] [blame] | 193 | uint64_t elapsed = DurationReporter::Nanotime() - started_; |
Felipe Leme | 78f2c86 | 2015-12-21 09:55:22 -0800 | [diff] [blame] | 194 | // Use "Yoda grammar" to make it easier to grep|sort sections. |
Felipe Leme | b0f669d | 2016-09-26 18:26:11 -0700 | [diff] [blame] | 195 | if (out_ != nullptr) { |
| 196 | fprintf(out_, "------ %.3fs was the duration of '%s' ------\n", |
Felipe Leme | 678727a | 2016-09-21 17:22:11 -0700 | [diff] [blame] | 197 | (float)elapsed / NANOS_PER_SEC, title_.c_str()); |
Felipe Leme | 608385d | 2016-02-01 10:35:38 -0800 | [diff] [blame] | 198 | } else { |
Felipe Leme | 678727a | 2016-09-21 17:22:11 -0700 | [diff] [blame] | 199 | MYLOGD("Duration of '%s': %.3fs\n", title_.c_str(), (float)elapsed / NANOS_PER_SEC); |
Felipe Leme | 608385d | 2016-02-01 10:35:38 -0800 | [diff] [blame] | 200 | } |
Felipe Leme | 78f2c86 | 2015-12-21 09:55:22 -0800 | [diff] [blame] | 201 | } |
| 202 | } |
| 203 | |
Felipe Leme | b0f669d | 2016-09-26 18:26:11 -0700 | [diff] [blame] | 204 | uint64_t DurationReporter::DurationReporter::Nanotime() { |
Christopher Ferris | 54bcc5f | 2015-02-10 12:15:01 -0800 | [diff] [blame] | 205 | struct timespec ts; |
| 206 | clock_gettime(CLOCK_MONOTONIC, &ts); |
Felipe Leme | 78f2c86 | 2015-12-21 09:55:22 -0800 | [diff] [blame] | 207 | return (uint64_t) ts.tv_sec * NANOS_PER_SEC + ts.tv_nsec; |
Christopher Ferris | 54bcc5f | 2015-02-10 12:15:01 -0800 | [diff] [blame] | 208 | } |
| 209 | |
Felipe Leme | 2b9b06c | 2016-10-14 09:13:06 -0700 | [diff] [blame] | 210 | bool Dumpstate::IsDryRun() const { |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 211 | return dry_run_; |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 212 | } |
| 213 | |
Felipe Leme | 2b9b06c | 2016-10-14 09:13:06 -0700 | [diff] [blame] | 214 | bool Dumpstate::IsUserBuild() const { |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 215 | return "user" == build_type_; |
Felipe Leme | e844a9d | 2016-09-21 15:01:39 -0700 | [diff] [blame] | 216 | } |
| 217 | |
Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 218 | bool Dumpstate::IsZipping() const { |
| 219 | return zip_writer_ != nullptr; |
| 220 | } |
| 221 | |
Felipe Leme | 2b9b06c | 2016-10-14 09:13:06 -0700 | [diff] [blame] | 222 | std::string Dumpstate::GetPath(const std::string& suffix) const { |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 223 | 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] | 224 | name_.c_str(), suffix.c_str()); |
Felipe Leme | bbaf3c1 | 2016-10-11 14:32:25 -0700 | [diff] [blame] | 225 | } |
| 226 | |
John Spurlock | 5ecd4be | 2014-01-29 14:14:40 -0500 | [diff] [blame] | 227 | void for_each_userid(void (*func)(int), const char *header) { |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 228 | if (IsDryRun()) return; |
Felipe Leme | d402e7d | 2016-08-03 09:22:27 -0700 | [diff] [blame] | 229 | |
John Spurlock | 5ecd4be | 2014-01-29 14:14:40 -0500 | [diff] [blame] | 230 | DIR *d; |
| 231 | struct dirent *de; |
| 232 | |
| 233 | if (header) printf("\n------ %s ------\n", header); |
| 234 | func(0); |
| 235 | |
| 236 | if (!(d = opendir("/data/system/users"))) { |
| 237 | printf("Failed to open /data/system/users (%s)\n", strerror(errno)); |
| 238 | return; |
| 239 | } |
| 240 | |
| 241 | while ((de = readdir(d))) { |
| 242 | int userid; |
| 243 | if (de->d_type != DT_DIR || !(userid = atoi(de->d_name))) { |
| 244 | continue; |
| 245 | } |
| 246 | func(userid); |
| 247 | } |
| 248 | |
| 249 | closedir(d); |
| 250 | } |
| 251 | |
Colin Cross | 0c22e8b | 2012-11-02 15:46:56 -0700 | [diff] [blame] | 252 | 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] | 253 | DIR *d; |
| 254 | struct dirent *de; |
| 255 | |
| 256 | if (!(d = opendir("/proc"))) { |
| 257 | printf("Failed to open /proc (%s)\n", strerror(errno)); |
| 258 | return; |
| 259 | } |
| 260 | |
Felipe Leme | 635ca31 | 2016-01-05 14:23:02 -0800 | [diff] [blame] | 261 | if (header) printf("\n------ %s ------\n", header); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 262 | while ((de = readdir(d))) { |
| 263 | int pid; |
| 264 | int fd; |
| 265 | char cmdpath[255]; |
| 266 | char cmdline[255]; |
| 267 | |
| 268 | if (!(pid = atoi(de->d_name))) { |
| 269 | continue; |
| 270 | } |
| 271 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 272 | memset(cmdline, 0, sizeof(cmdline)); |
Mark Salyzyn | 0751efa | 2016-02-05 15:33:17 -0800 | [diff] [blame] | 273 | |
| 274 | snprintf(cmdpath, sizeof(cmdpath), "/proc/%d/cmdline", pid); |
| 275 | if ((fd = TEMP_FAILURE_RETRY(open(cmdpath, O_RDONLY | O_CLOEXEC))) >= 0) { |
| 276 | TEMP_FAILURE_RETRY(read(fd, cmdline, sizeof(cmdline) - 2)); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 277 | close(fd); |
Mark Salyzyn | 0751efa | 2016-02-05 15:33:17 -0800 | [diff] [blame] | 278 | if (cmdline[0]) { |
| 279 | helper(pid, cmdline, arg); |
| 280 | continue; |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | // if no cmdline, a kernel thread has comm |
| 285 | snprintf(cmdpath, sizeof(cmdpath), "/proc/%d/comm", pid); |
| 286 | if ((fd = TEMP_FAILURE_RETRY(open(cmdpath, O_RDONLY | O_CLOEXEC))) >= 0) { |
| 287 | TEMP_FAILURE_RETRY(read(fd, cmdline + 1, sizeof(cmdline) - 4)); |
| 288 | close(fd); |
| 289 | if (cmdline[1]) { |
| 290 | cmdline[0] = '['; |
| 291 | size_t len = strcspn(cmdline, "\f\b\r\n"); |
| 292 | cmdline[len] = ']'; |
| 293 | cmdline[len+1] = '\0'; |
| 294 | } |
| 295 | } |
| 296 | if (!cmdline[0]) { |
| 297 | strcpy(cmdline, "N/A"); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 298 | } |
Colin Cross | 0c22e8b | 2012-11-02 15:46:56 -0700 | [diff] [blame] | 299 | helper(pid, cmdline, arg); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | closedir(d); |
| 303 | } |
| 304 | |
Colin Cross | 0c22e8b | 2012-11-02 15:46:56 -0700 | [diff] [blame] | 305 | 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] | 306 | for_each_pid_func *func = (for_each_pid_func*) arg; |
Colin Cross | 0c22e8b | 2012-11-02 15:46:56 -0700 | [diff] [blame] | 307 | func(pid, cmdline); |
| 308 | } |
| 309 | |
| 310 | void for_each_pid(for_each_pid_func func, const char *header) { |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 311 | if (IsDryRun()) return; |
Felipe Leme | d402e7d | 2016-08-03 09:22:27 -0700 | [diff] [blame] | 312 | |
Felipe Leme | 515eb0d | 2015-12-14 15:09:56 -0800 | [diff] [blame] | 313 | __for_each_pid(for_each_pid_helper, header, (void *) func); |
Colin Cross | 0c22e8b | 2012-11-02 15:46:56 -0700 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | static void for_each_tid_helper(int pid, const char *cmdline, void *arg) { |
| 317 | DIR *d; |
| 318 | struct dirent *de; |
| 319 | char taskpath[255]; |
Felipe Leme | 8620bb4 | 2015-11-10 11:04:45 -0800 | [diff] [blame] | 320 | for_each_tid_func *func = (for_each_tid_func *) arg; |
Colin Cross | 0c22e8b | 2012-11-02 15:46:56 -0700 | [diff] [blame] | 321 | |
Nick Kralevich | f0922cc | 2016-05-14 16:47:44 -0700 | [diff] [blame] | 322 | snprintf(taskpath, sizeof(taskpath), "/proc/%d/task", pid); |
Colin Cross | 0c22e8b | 2012-11-02 15:46:56 -0700 | [diff] [blame] | 323 | |
| 324 | if (!(d = opendir(taskpath))) { |
| 325 | printf("Failed to open %s (%s)\n", taskpath, strerror(errno)); |
| 326 | return; |
| 327 | } |
| 328 | |
| 329 | func(pid, pid, cmdline); |
| 330 | |
| 331 | while ((de = readdir(d))) { |
| 332 | int tid; |
| 333 | int fd; |
| 334 | char commpath[255]; |
| 335 | char comm[255]; |
| 336 | |
| 337 | if (!(tid = atoi(de->d_name))) { |
| 338 | continue; |
| 339 | } |
| 340 | |
| 341 | if (tid == pid) |
| 342 | continue; |
| 343 | |
Nick Kralevich | f0922cc | 2016-05-14 16:47:44 -0700 | [diff] [blame] | 344 | snprintf(commpath, sizeof(commpath), "/proc/%d/comm", tid); |
Colin Cross | 1493a39 | 2012-11-07 11:25:31 -0800 | [diff] [blame] | 345 | memset(comm, 0, sizeof(comm)); |
Nick Kralevich | cd67e9f | 2015-03-19 11:30:59 -0700 | [diff] [blame] | 346 | if ((fd = TEMP_FAILURE_RETRY(open(commpath, O_RDONLY | O_CLOEXEC))) < 0) { |
Colin Cross | 0c22e8b | 2012-11-02 15:46:56 -0700 | [diff] [blame] | 347 | strcpy(comm, "N/A"); |
| 348 | } else { |
| 349 | char *c; |
Mark Salyzyn | 0751efa | 2016-02-05 15:33:17 -0800 | [diff] [blame] | 350 | TEMP_FAILURE_RETRY(read(fd, comm, sizeof(comm) - 2)); |
Colin Cross | 0c22e8b | 2012-11-02 15:46:56 -0700 | [diff] [blame] | 351 | close(fd); |
| 352 | |
| 353 | c = strrchr(comm, '\n'); |
| 354 | if (c) { |
| 355 | *c = '\0'; |
| 356 | } |
| 357 | } |
| 358 | func(pid, tid, comm); |
| 359 | } |
| 360 | |
| 361 | closedir(d); |
| 362 | } |
| 363 | |
| 364 | void for_each_tid(for_each_tid_func func, const char *header) { |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 365 | if (IsDryRun()) return; |
Felipe Leme | d402e7d | 2016-08-03 09:22:27 -0700 | [diff] [blame] | 366 | |
Felipe Leme | 8620bb4 | 2015-11-10 11:04:45 -0800 | [diff] [blame] | 367 | __for_each_pid(for_each_tid_helper, header, (void *) func); |
Colin Cross | 0c22e8b | 2012-11-02 15:46:56 -0700 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | void show_wchan(int pid, int tid, const char *name) { |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 371 | if (IsDryRun()) return; |
Felipe Leme | d402e7d | 2016-08-03 09:22:27 -0700 | [diff] [blame] | 372 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 373 | char path[255]; |
| 374 | char buffer[255]; |
Mark Salyzyn | 0751efa | 2016-02-05 15:33:17 -0800 | [diff] [blame] | 375 | int fd, ret, save_errno; |
Colin Cross | 0c22e8b | 2012-11-02 15:46:56 -0700 | [diff] [blame] | 376 | char name_buffer[255]; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 377 | |
| 378 | memset(buffer, 0, sizeof(buffer)); |
| 379 | |
Nick Kralevich | f0922cc | 2016-05-14 16:47:44 -0700 | [diff] [blame] | 380 | snprintf(path, sizeof(path), "/proc/%d/wchan", tid); |
Nick Kralevich | cd67e9f | 2015-03-19 11:30:59 -0700 | [diff] [blame] | 381 | if ((fd = TEMP_FAILURE_RETRY(open(path, O_RDONLY | O_CLOEXEC))) < 0) { |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 382 | printf("Failed to open '%s' (%s)\n", path, strerror(errno)); |
| 383 | return; |
| 384 | } |
| 385 | |
Mark Salyzyn | 0751efa | 2016-02-05 15:33:17 -0800 | [diff] [blame] | 386 | ret = TEMP_FAILURE_RETRY(read(fd, buffer, sizeof(buffer))); |
| 387 | save_errno = errno; |
| 388 | close(fd); |
| 389 | |
| 390 | if (ret < 0) { |
| 391 | printf("Failed to read '%s' (%s)\n", path, strerror(save_errno)); |
| 392 | return; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 393 | } |
| 394 | |
Colin Cross | 0c22e8b | 2012-11-02 15:46:56 -0700 | [diff] [blame] | 395 | snprintf(name_buffer, sizeof(name_buffer), "%*s%s", |
| 396 | pid == tid ? 0 : 3, "", name); |
| 397 | |
| 398 | printf("%-7d %-32s %s\n", tid, name_buffer, buffer); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 399 | |
Mark Salyzyn | 0751efa | 2016-02-05 15:33:17 -0800 | [diff] [blame] | 400 | return; |
| 401 | } |
| 402 | |
| 403 | // print time in centiseconds |
| 404 | static void snprcent(char *buffer, size_t len, size_t spc, |
| 405 | unsigned long long time) { |
| 406 | static long hz; // cache discovered hz |
| 407 | |
| 408 | if (hz <= 0) { |
| 409 | hz = sysconf(_SC_CLK_TCK); |
| 410 | if (hz <= 0) { |
| 411 | hz = 1000; |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | // convert to centiseconds |
| 416 | time = (time * 100 + (hz / 2)) / hz; |
| 417 | |
| 418 | char str[16]; |
| 419 | |
| 420 | snprintf(str, sizeof(str), " %llu.%02u", |
| 421 | time / 100, (unsigned)(time % 100)); |
| 422 | size_t offset = strlen(buffer); |
| 423 | snprintf(buffer + offset, (len > offset) ? len - offset : 0, |
| 424 | "%*s", (spc > offset) ? (int)(spc - offset) : 0, str); |
| 425 | } |
| 426 | |
| 427 | // print permille as a percent |
| 428 | static void snprdec(char *buffer, size_t len, size_t spc, unsigned permille) { |
| 429 | char str[16]; |
| 430 | |
| 431 | snprintf(str, sizeof(str), " %u.%u%%", permille / 10, permille % 10); |
| 432 | size_t offset = strlen(buffer); |
| 433 | snprintf(buffer + offset, (len > offset) ? len - offset : 0, |
| 434 | "%*s", (spc > offset) ? (int)(spc - offset) : 0, str); |
| 435 | } |
| 436 | |
| 437 | void show_showtime(int pid, const char *name) { |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 438 | if (IsDryRun()) return; |
Felipe Leme | d402e7d | 2016-08-03 09:22:27 -0700 | [diff] [blame] | 439 | |
Mark Salyzyn | 0751efa | 2016-02-05 15:33:17 -0800 | [diff] [blame] | 440 | char path[255]; |
| 441 | char buffer[1023]; |
| 442 | int fd, ret, save_errno; |
| 443 | |
| 444 | memset(buffer, 0, sizeof(buffer)); |
| 445 | |
Nick Kralevich | f0922cc | 2016-05-14 16:47:44 -0700 | [diff] [blame] | 446 | snprintf(path, sizeof(path), "/proc/%d/stat", pid); |
Mark Salyzyn | 0751efa | 2016-02-05 15:33:17 -0800 | [diff] [blame] | 447 | if ((fd = TEMP_FAILURE_RETRY(open(path, O_RDONLY | O_CLOEXEC))) < 0) { |
| 448 | printf("Failed to open '%s' (%s)\n", path, strerror(errno)); |
| 449 | return; |
| 450 | } |
| 451 | |
| 452 | ret = TEMP_FAILURE_RETRY(read(fd, buffer, sizeof(buffer))); |
| 453 | save_errno = errno; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 454 | close(fd); |
Mark Salyzyn | 0751efa | 2016-02-05 15:33:17 -0800 | [diff] [blame] | 455 | |
| 456 | if (ret < 0) { |
| 457 | printf("Failed to read '%s' (%s)\n", path, strerror(save_errno)); |
| 458 | return; |
| 459 | } |
| 460 | |
| 461 | // field 14 is utime |
| 462 | // field 15 is stime |
| 463 | // field 42 is iotime |
| 464 | unsigned long long utime = 0, stime = 0, iotime = 0; |
| 465 | if (sscanf(buffer, |
Mark Salyzyn | 791ddd3 | 2016-02-10 07:41:12 -0800 | [diff] [blame] | 466 | "%*u %*s %*s %*d %*d %*d %*d %*d %*d %*d %*d " |
| 467 | "%*d %*d %llu %llu %*d %*d %*d %*d %*d %*d " |
| 468 | "%*d %*d %*d %*d %*d %*d %*d %*d %*d %*d " |
| 469 | "%*d %*d %*d %*d %*d %*d %*d %*d %*d %llu ", |
Mark Salyzyn | 0751efa | 2016-02-05 15:33:17 -0800 | [diff] [blame] | 470 | &utime, &stime, &iotime) != 3) { |
| 471 | return; |
| 472 | } |
| 473 | |
| 474 | unsigned long long total = utime + stime; |
| 475 | if (!total) { |
| 476 | return; |
| 477 | } |
| 478 | |
| 479 | unsigned permille = (iotime * 1000 + (total / 2)) / total; |
| 480 | if (permille > 1000) { |
| 481 | permille = 1000; |
| 482 | } |
| 483 | |
| 484 | // try to beautify and stabilize columns at <80 characters |
| 485 | snprintf(buffer, sizeof(buffer), "%-6d%s", pid, name); |
| 486 | if ((name[0] != '[') || utime) { |
| 487 | snprcent(buffer, sizeof(buffer), 57, utime); |
| 488 | } |
| 489 | snprcent(buffer, sizeof(buffer), 65, stime); |
| 490 | if ((name[0] != '[') || iotime) { |
| 491 | snprcent(buffer, sizeof(buffer), 73, iotime); |
| 492 | } |
| 493 | if (iotime) { |
| 494 | snprdec(buffer, sizeof(buffer), 79, permille); |
| 495 | } |
| 496 | puts(buffer); // adds a trailing newline |
| 497 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 498 | return; |
| 499 | } |
| 500 | |
| 501 | void do_dmesg() { |
Felipe Leme | 78f2c86 | 2015-12-21 09:55:22 -0800 | [diff] [blame] | 502 | const char *title = "KERNEL LOG (dmesg)"; |
| 503 | DurationReporter duration_reporter(title); |
| 504 | printf("------ %s ------\n", title); |
| 505 | |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 506 | if (IsDryRun()) return; |
Felipe Leme | d402e7d | 2016-08-03 09:22:27 -0700 | [diff] [blame] | 507 | |
Elliott Hughes | 5f87b31 | 2012-09-17 11:43:40 -0700 | [diff] [blame] | 508 | /* Get size of kernel buffer */ |
| 509 | int size = klogctl(KLOG_SIZE_BUFFER, NULL, 0); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 510 | if (size <= 0) { |
| 511 | printf("Unexpected klogctl return value: %d\n\n", size); |
| 512 | return; |
| 513 | } |
| 514 | char *buf = (char *) malloc(size + 1); |
| 515 | if (buf == NULL) { |
| 516 | printf("memory allocation failed\n\n"); |
| 517 | return; |
| 518 | } |
| 519 | int retval = klogctl(KLOG_READ_ALL, buf, size); |
| 520 | if (retval < 0) { |
| 521 | printf("klogctl failure\n\n"); |
| 522 | free(buf); |
| 523 | return; |
| 524 | } |
| 525 | buf[retval] = '\0'; |
| 526 | printf("%s\n\n", buf); |
| 527 | free(buf); |
| 528 | return; |
| 529 | } |
| 530 | |
| 531 | void do_showmap(int pid, const char *name) { |
| 532 | char title[255]; |
| 533 | char arg[255]; |
| 534 | |
Nick Kralevich | f0922cc | 2016-05-14 16:47:44 -0700 | [diff] [blame] | 535 | snprintf(title, sizeof(title), "SHOW MAP %d (%s)", pid, name); |
| 536 | snprintf(arg, sizeof(arg), "%d", pid); |
Felipe Leme | b0f669d | 2016-09-26 18:26:11 -0700 | [diff] [blame] | 537 | RunCommand(title, {"showmap", "-q", arg}, CommandOptions::AS_ROOT_10); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 538 | } |
| 539 | |
Felipe Leme | 2b9b06c | 2016-10-14 09:13:06 -0700 | [diff] [blame] | 540 | // TODO: when converted to a Dumpstate function, it should be const |
Felipe Leme | 678727a | 2016-09-21 17:22:11 -0700 | [diff] [blame] | 541 | static int _dump_file_from_fd(const std::string& title, const char* path, int fd) { |
| 542 | if (!title.empty()) { |
| 543 | printf("------ %s (%s", title.c_str(), path); |
Christopher Ferris | ed24d2a | 2015-11-12 14:01:56 -0800 | [diff] [blame] | 544 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 545 | struct stat st; |
Christopher Ferris | ed24d2a | 2015-11-12 14:01:56 -0800 | [diff] [blame] | 546 | // Only show the modification time of non-device files. |
| 547 | size_t path_len = strlen(path); |
| 548 | if ((path_len < 6 || memcmp(path, "/proc/", 6)) && |
| 549 | (path_len < 5 || memcmp(path, "/sys/", 5)) && |
| 550 | (path_len < 3 || memcmp(path, "/d/", 3)) && |
| 551 | !fstat(fd, &st)) { |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 552 | char stamp[80]; |
| 553 | time_t mtime = st.st_mtime; |
| 554 | strftime(stamp, sizeof(stamp), "%Y-%m-%d %H:%M:%S", localtime(&mtime)); |
| 555 | printf(": %s", stamp); |
| 556 | } |
| 557 | printf(") ------\n"); |
| 558 | } |
| 559 | |
Christopher Ferris | 54bcc5f | 2015-02-10 12:15:01 -0800 | [diff] [blame] | 560 | bool newline = false; |
| 561 | fd_set read_set; |
| 562 | struct timeval tm; |
| 563 | while (1) { |
| 564 | FD_ZERO(&read_set); |
| 565 | FD_SET(fd, &read_set); |
| 566 | /* Timeout if no data is read for 30 seconds. */ |
| 567 | tm.tv_sec = 30; |
| 568 | tm.tv_usec = 0; |
Felipe Leme | b0f669d | 2016-09-26 18:26:11 -0700 | [diff] [blame] | 569 | uint64_t elapsed = DurationReporter::Nanotime(); |
Christopher Ferris | 54bcc5f | 2015-02-10 12:15:01 -0800 | [diff] [blame] | 570 | int ret = TEMP_FAILURE_RETRY(select(fd + 1, &read_set, NULL, NULL, &tm)); |
| 571 | if (ret == -1) { |
| 572 | printf("*** %s: select failed: %s\n", path, strerror(errno)); |
| 573 | newline = true; |
| 574 | break; |
| 575 | } else if (ret == 0) { |
Felipe Leme | b0f669d | 2016-09-26 18:26:11 -0700 | [diff] [blame] | 576 | elapsed = DurationReporter::Nanotime() - elapsed; |
Christopher Ferris | 54bcc5f | 2015-02-10 12:15:01 -0800 | [diff] [blame] | 577 | printf("*** %s: Timed out after %.3fs\n", path, |
| 578 | (float) elapsed / NANOS_PER_SEC); |
| 579 | newline = true; |
| 580 | break; |
| 581 | } else { |
| 582 | char buffer[65536]; |
| 583 | ssize_t bytes_read = TEMP_FAILURE_RETRY(read(fd, buffer, sizeof(buffer))); |
| 584 | if (bytes_read > 0) { |
| 585 | fwrite(buffer, bytes_read, 1, stdout); |
| 586 | newline = (buffer[bytes_read-1] == '\n'); |
| 587 | } else { |
| 588 | if (bytes_read == -1) { |
| 589 | printf("*** %s: Failed to read from fd: %s", path, strerror(errno)); |
| 590 | newline = true; |
| 591 | } |
| 592 | break; |
| 593 | } |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 594 | } |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 595 | } |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 596 | UpdateProgress(WEIGHT_FILE); |
Elliott Hughes | 997abb6 | 2015-05-15 17:05:40 -0700 | [diff] [blame] | 597 | close(fd); |
Christopher Ferris | 7dc7f32 | 2014-07-22 16:08:19 -0700 | [diff] [blame] | 598 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 599 | if (!newline) printf("\n"); |
Felipe Leme | 678727a | 2016-09-21 17:22:11 -0700 | [diff] [blame] | 600 | if (!title.empty()) printf("\n"); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 601 | return 0; |
| 602 | } |
| 603 | |
Felipe Leme | 678727a | 2016-09-21 17:22:11 -0700 | [diff] [blame] | 604 | int Dumpstate::DumpFile(const std::string& title, const std::string& path) { |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 605 | DurationReporter duration_reporter(title); |
Felipe Leme | cef0298 | 2016-10-03 17:22:22 -0700 | [diff] [blame] | 606 | if (IsDryRun()) { |
| 607 | if (!title.empty()) { |
| 608 | printf("------ %s (%s) ------\n", title.c_str(), path.c_str()); |
| 609 | printf("\t(skipped on dry run)\n"); |
| 610 | } |
| 611 | UpdateProgress(WEIGHT_FILE); |
| 612 | return 0; |
| 613 | } |
Felipe Leme | 2b9b06c | 2016-10-14 09:13:06 -0700 | [diff] [blame] | 614 | return JustDumpFile(title, path); |
| 615 | } |
Felipe Leme | cef0298 | 2016-10-03 17:22:22 -0700 | [diff] [blame] | 616 | |
Felipe Leme | 2b9b06c | 2016-10-14 09:13:06 -0700 | [diff] [blame] | 617 | int Dumpstate::JustDumpFile(const std::string& title, const std::string& path) const { |
Felipe Leme | 0bcc7ca | 2016-09-13 16:45:56 -0700 | [diff] [blame] | 618 | int fd = TEMP_FAILURE_RETRY(open(path.c_str(), O_RDONLY | O_NONBLOCK | O_CLOEXEC)); |
Christopher Ferris | 54bcc5f | 2015-02-10 12:15:01 -0800 | [diff] [blame] | 619 | if (fd < 0) { |
| 620 | int err = errno; |
Felipe Leme | cef0298 | 2016-10-03 17:22:22 -0700 | [diff] [blame] | 621 | if (title.empty()) { |
| 622 | printf("*** Error dumping %s: %s\n", path.c_str(), strerror(err)); |
| 623 | } else { |
| 624 | printf("*** Error dumping %s (%s): %s\n", path.c_str(), title.c_str(), strerror(err)); |
| 625 | } |
Christopher Ferris | 54bcc5f | 2015-02-10 12:15:01 -0800 | [diff] [blame] | 626 | return -1; |
| 627 | } |
Felipe Leme | 0bcc7ca | 2016-09-13 16:45:56 -0700 | [diff] [blame] | 628 | return _dump_file_from_fd(title, path.c_str(), fd); |
Christopher Ferris | 54bcc5f | 2015-02-10 12:15:01 -0800 | [diff] [blame] | 629 | } |
| 630 | |
Felipe Leme | 71a74ac | 2016-03-17 15:43:25 -0700 | [diff] [blame] | 631 | int read_file_as_long(const char *path, long int *output) { |
| 632 | int fd = TEMP_FAILURE_RETRY(open(path, O_RDONLY | O_NONBLOCK | O_CLOEXEC)); |
| 633 | if (fd < 0) { |
| 634 | int err = errno; |
| 635 | MYLOGE("Error opening file descriptor for %s: %s\n", path, strerror(err)); |
| 636 | return -1; |
| 637 | } |
| 638 | char buffer[50]; |
| 639 | ssize_t bytes_read = TEMP_FAILURE_RETRY(read(fd, buffer, sizeof(buffer))); |
| 640 | if (bytes_read == -1) { |
| 641 | MYLOGE("Error reading file %s: %s\n", path, strerror(errno)); |
| 642 | return -2; |
| 643 | } |
| 644 | if (bytes_read == 0) { |
| 645 | MYLOGE("File %s is empty\n", path); |
| 646 | return -3; |
| 647 | } |
| 648 | *output = atoi(buffer); |
| 649 | return 0; |
| 650 | } |
| 651 | |
Mark Salyzyn | 326842f | 2015-04-30 09:49:41 -0700 | [diff] [blame] | 652 | /* calls skip to gate calling dump_from_fd recursively |
| 653 | * in the specified directory. dump_from_fd defaults to |
| 654 | * dump_file_from_fd above when set to NULL. skip defaults |
| 655 | * to false when set to NULL. dump_from_fd will always be |
| 656 | * called with title NULL. |
| 657 | */ |
Felipe Leme | 678727a | 2016-09-21 17:22:11 -0700 | [diff] [blame] | 658 | int dump_files(const std::string& title, const char* dir, bool (*skip)(const char* path), |
| 659 | int (*dump_from_fd)(const char* title, const char* path, int fd)) { |
Felipe Leme | 78f2c86 | 2015-12-21 09:55:22 -0800 | [diff] [blame] | 660 | DurationReporter duration_reporter(title); |
Mark Salyzyn | 326842f | 2015-04-30 09:49:41 -0700 | [diff] [blame] | 661 | DIR *dirp; |
| 662 | struct dirent *d; |
| 663 | char *newpath = NULL; |
Felipe Leme | 8620bb4 | 2015-11-10 11:04:45 -0800 | [diff] [blame] | 664 | const char *slash = "/"; |
Mark Salyzyn | 326842f | 2015-04-30 09:49:41 -0700 | [diff] [blame] | 665 | int fd, retval = 0; |
| 666 | |
Felipe Leme | 678727a | 2016-09-21 17:22:11 -0700 | [diff] [blame] | 667 | if (!title.empty()) { |
| 668 | printf("------ %s (%s) ------\n", title.c_str(), dir); |
Mark Salyzyn | 326842f | 2015-04-30 09:49:41 -0700 | [diff] [blame] | 669 | } |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 670 | if (IsDryRun()) return 0; |
Mark Salyzyn | 326842f | 2015-04-30 09:49:41 -0700 | [diff] [blame] | 671 | |
| 672 | if (dir[strlen(dir) - 1] == '/') { |
| 673 | ++slash; |
| 674 | } |
| 675 | dirp = opendir(dir); |
| 676 | if (dirp == NULL) { |
| 677 | retval = -errno; |
Felipe Leme | 107a05f | 2016-03-08 15:11:15 -0800 | [diff] [blame] | 678 | MYLOGE("%s: %s\n", dir, strerror(errno)); |
Mark Salyzyn | 326842f | 2015-04-30 09:49:41 -0700 | [diff] [blame] | 679 | return retval; |
| 680 | } |
| 681 | |
| 682 | if (!dump_from_fd) { |
| 683 | dump_from_fd = dump_file_from_fd; |
| 684 | } |
| 685 | for (; ((d = readdir(dirp))); free(newpath), newpath = NULL) { |
| 686 | if ((d->d_name[0] == '.') |
| 687 | && (((d->d_name[1] == '.') && (d->d_name[2] == '\0')) |
| 688 | || (d->d_name[1] == '\0'))) { |
| 689 | continue; |
| 690 | } |
| 691 | asprintf(&newpath, "%s%s%s%s", dir, slash, d->d_name, |
| 692 | (d->d_type == DT_DIR) ? "/" : ""); |
| 693 | if (!newpath) { |
| 694 | retval = -errno; |
| 695 | continue; |
| 696 | } |
| 697 | if (skip && (*skip)(newpath)) { |
| 698 | continue; |
| 699 | } |
| 700 | if (d->d_type == DT_DIR) { |
Felipe Leme | 678727a | 2016-09-21 17:22:11 -0700 | [diff] [blame] | 701 | int ret = dump_files("", newpath, skip, dump_from_fd); |
Mark Salyzyn | 326842f | 2015-04-30 09:49:41 -0700 | [diff] [blame] | 702 | if (ret < 0) { |
| 703 | retval = ret; |
| 704 | } |
| 705 | continue; |
| 706 | } |
| 707 | fd = TEMP_FAILURE_RETRY(open(newpath, O_RDONLY | O_NONBLOCK | O_CLOEXEC)); |
| 708 | if (fd < 0) { |
| 709 | retval = fd; |
| 710 | printf("*** %s: %s\n", newpath, strerror(errno)); |
| 711 | continue; |
| 712 | } |
| 713 | (*dump_from_fd)(NULL, newpath, fd); |
| 714 | } |
| 715 | closedir(dirp); |
Felipe Leme | 678727a | 2016-09-21 17:22:11 -0700 | [diff] [blame] | 716 | if (!title.empty()) { |
Mark Salyzyn | 326842f | 2015-04-30 09:49:41 -0700 | [diff] [blame] | 717 | printf("\n"); |
| 718 | } |
| 719 | return retval; |
| 720 | } |
| 721 | |
Christopher Ferris | 54bcc5f | 2015-02-10 12:15:01 -0800 | [diff] [blame] | 722 | /* fd must have been opened with the flag O_NONBLOCK. With this flag set, |
| 723 | * it's possible to avoid issues where opening the file itself can get |
| 724 | * stuck. |
| 725 | */ |
| 726 | int dump_file_from_fd(const char *title, const char *path, int fd) { |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 727 | if (IsDryRun()) return 0; |
Felipe Leme | d402e7d | 2016-08-03 09:22:27 -0700 | [diff] [blame] | 728 | |
Christopher Ferris | 54bcc5f | 2015-02-10 12:15:01 -0800 | [diff] [blame] | 729 | int flags = fcntl(fd, F_GETFL); |
| 730 | if (flags == -1) { |
| 731 | 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] | 732 | close(fd); |
Christopher Ferris | 54bcc5f | 2015-02-10 12:15:01 -0800 | [diff] [blame] | 733 | return -1; |
| 734 | } else if (!(flags & O_NONBLOCK)) { |
| 735 | printf("*** %s: fd must have O_NONBLOCK set.\n", path); |
Christopher Ferris | ed24d2a | 2015-11-12 14:01:56 -0800 | [diff] [blame] | 736 | close(fd); |
Christopher Ferris | 54bcc5f | 2015-02-10 12:15:01 -0800 | [diff] [blame] | 737 | return -1; |
| 738 | } |
| 739 | return _dump_file_from_fd(title, path, fd); |
Jeff Brown | 1dc94e3 | 2014-09-11 14:15:27 -0700 | [diff] [blame] | 740 | } |
| 741 | |
Christopher Ferris | 1a9a338 | 2015-01-30 11:00:52 -0800 | [diff] [blame] | 742 | bool waitpid_with_timeout(pid_t pid, int timeout_seconds, int* status) { |
| 743 | sigset_t child_mask, old_mask; |
| 744 | sigemptyset(&child_mask); |
| 745 | sigaddset(&child_mask, SIGCHLD); |
| 746 | |
| 747 | if (sigprocmask(SIG_BLOCK, &child_mask, &old_mask) == -1) { |
| 748 | printf("*** sigprocmask failed: %s\n", strerror(errno)); |
| 749 | return false; |
| 750 | } |
| 751 | |
| 752 | struct timespec ts; |
| 753 | ts.tv_sec = timeout_seconds; |
| 754 | ts.tv_nsec = 0; |
| 755 | int ret = TEMP_FAILURE_RETRY(sigtimedwait(&child_mask, NULL, &ts)); |
| 756 | int saved_errno = errno; |
| 757 | // Set the signals back the way they were. |
| 758 | if (sigprocmask(SIG_SETMASK, &old_mask, NULL) == -1) { |
| 759 | printf("*** sigprocmask failed: %s\n", strerror(errno)); |
| 760 | if (ret == 0) { |
| 761 | return false; |
| 762 | } |
| 763 | } |
| 764 | if (ret == -1) { |
| 765 | errno = saved_errno; |
| 766 | if (errno == EAGAIN) { |
| 767 | errno = ETIMEDOUT; |
| 768 | } else { |
| 769 | printf("*** sigtimedwait failed: %s\n", strerror(errno)); |
| 770 | } |
| 771 | return false; |
| 772 | } |
| 773 | |
| 774 | pid_t child_pid = waitpid(pid, status, WNOHANG); |
| 775 | if (child_pid != pid) { |
| 776 | if (child_pid != -1) { |
| 777 | printf("*** Waiting for pid %d, got pid %d instead\n", pid, child_pid); |
| 778 | } else { |
| 779 | printf("*** waitpid failed: %s\n", strerror(errno)); |
| 780 | } |
| 781 | return false; |
| 782 | } |
| 783 | return true; |
| 784 | } |
| 785 | |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 786 | int Dumpstate::RunCommand(const std::string& title, const std::vector<std::string>& full_command, |
Felipe Leme | 678727a | 2016-09-21 17:22:11 -0700 | [diff] [blame] | 787 | const CommandOptions& options) { |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 788 | if (full_command.empty()) { |
Felipe Leme | 678727a | 2016-09-21 17:22:11 -0700 | [diff] [blame] | 789 | MYLOGE("No arguments on command '%s'\n", title.c_str()); |
Felipe Leme | 30dbfa1 | 2016-09-02 12:43:26 -0700 | [diff] [blame] | 790 | return -1; |
| 791 | } |
Felipe Leme | cf6a8b4 | 2016-03-11 10:38:19 -0800 | [diff] [blame] | 792 | |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 793 | int size = full_command.size() + 1; // null terminated |
| 794 | int starting_index = 0; |
Felipe Leme | 30dbfa1 | 2016-09-02 12:43:26 -0700 | [diff] [blame] | 795 | if (options.RootMode() == SU_ROOT) { |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 796 | starting_index = 2; // "su" "root" |
| 797 | size += starting_index; |
Felipe Leme | 30dbfa1 | 2016-09-02 12:43:26 -0700 | [diff] [blame] | 798 | } |
| 799 | |
Felipe Leme | 6ad9c06 | 2016-09-28 11:58:36 -0700 | [diff] [blame] | 800 | std::vector<const char*> args; |
| 801 | args.resize(size); |
Felipe Leme | 30dbfa1 | 2016-09-02 12:43:26 -0700 | [diff] [blame] | 802 | |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 803 | std::string command_string; |
Felipe Leme | 30dbfa1 | 2016-09-02 12:43:26 -0700 | [diff] [blame] | 804 | if (options.RootMode() == SU_ROOT) { |
| 805 | args[0] = SU_PATH; |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 806 | command_string += SU_PATH; |
Felipe Leme | 30dbfa1 | 2016-09-02 12:43:26 -0700 | [diff] [blame] | 807 | args[1] = "root"; |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 808 | command_string += " root "; |
Felipe Leme | 30dbfa1 | 2016-09-02 12:43:26 -0700 | [diff] [blame] | 809 | } |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 810 | int i = starting_index; |
| 811 | for (auto arg = full_command.begin(); arg != full_command.end(); ++arg) { |
Felipe Leme | 30dbfa1 | 2016-09-02 12:43:26 -0700 | [diff] [blame] | 812 | args[i++] = arg->c_str(); |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 813 | command_string += arg->c_str(); |
| 814 | if (arg != full_command.end() - 1) { |
| 815 | command_string += " "; |
Felipe Leme | cf6a8b4 | 2016-03-11 10:38:19 -0800 | [diff] [blame] | 816 | } |
Felipe Leme | cf6a8b4 | 2016-03-11 10:38:19 -0800 | [diff] [blame] | 817 | } |
Felipe Leme | 30dbfa1 | 2016-09-02 12:43:26 -0700 | [diff] [blame] | 818 | args[i] = nullptr; |
| 819 | const char* path = args[0]; |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 820 | const char* command = command_string.c_str(); |
Felipe Leme | c5d6cfc | 2016-09-26 15:57:04 -0700 | [diff] [blame] | 821 | |
Felipe Leme | 6ad9c06 | 2016-09-28 11:58:36 -0700 | [diff] [blame] | 822 | if (options.RootMode() == SU_ROOT && ds.IsUserBuild()) { |
| 823 | printf("Skipping '%s' on user build.\n", command); |
| 824 | return 0; |
| 825 | } |
| 826 | |
Felipe Leme | 678727a | 2016-09-21 17:22:11 -0700 | [diff] [blame] | 827 | if (!title.empty()) { |
Felipe Leme | 6ad9c06 | 2016-09-28 11:58:36 -0700 | [diff] [blame] | 828 | printf("------ %s (%s) ------\n", title.c_str(), command); |
Felipe Leme | c5d6cfc | 2016-09-26 15:57:04 -0700 | [diff] [blame] | 829 | } |
Felipe Leme | 30dbfa1 | 2016-09-02 12:43:26 -0700 | [diff] [blame] | 830 | |
Felipe Leme | cf6a8b4 | 2016-03-11 10:38:19 -0800 | [diff] [blame] | 831 | fflush(stdout); |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 832 | DurationReporter duration_reporter(title); |
Felipe Leme | 30dbfa1 | 2016-09-02 12:43:26 -0700 | [diff] [blame] | 833 | |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 834 | const std::string& logging_message = options.LoggingMessage(); |
| 835 | if (!logging_message.empty()) { |
| 836 | MYLOGI(logging_message.c_str(), command_string.c_str()); |
Felipe Leme | cf6a8b4 | 2016-03-11 10:38:19 -0800 | [diff] [blame] | 837 | } |
| 838 | |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 839 | if (IsDryRun() && !options.Always()) { |
| 840 | if (!title.empty()) { |
| 841 | printf("\t(skipped on dry run)\n"); |
| 842 | } |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 843 | UpdateProgress(options.Timeout()); |
Felipe Leme | 30dbfa1 | 2016-09-02 12:43:26 -0700 | [diff] [blame] | 844 | return 0; |
Felipe Leme | d402e7d | 2016-08-03 09:22:27 -0700 | [diff] [blame] | 845 | } |
Felipe Leme | 93d705b | 2015-11-10 20:10:25 -0800 | [diff] [blame] | 846 | |
Felipe Leme | 2b9b06c | 2016-10-14 09:13:06 -0700 | [diff] [blame] | 847 | int status = JustRunCommand(command, path, args, options); |
Felipe Leme | ea160d1 | 2016-03-24 11:29:44 -0700 | [diff] [blame] | 848 | |
Felipe Leme | 30dbfa1 | 2016-09-02 12:43:26 -0700 | [diff] [blame] | 849 | /* TODO: for now we're simplifying the progress calculation by using the |
| 850 | * timeout as the weight. It's a good approximation for most cases, except when calling dumpsys, |
| 851 | * where its weight should be much higher proportionally to its timeout. |
| 852 | * Ideally, it should use a options.EstimatedDuration() instead...*/ |
| 853 | int weight = options.Timeout(); |
Felipe Leme | 93d705b | 2015-11-10 20:10:25 -0800 | [diff] [blame] | 854 | |
Felipe Leme | 2b9b06c | 2016-10-14 09:13:06 -0700 | [diff] [blame] | 855 | if (weight > 0) { |
| 856 | UpdateProgress(weight); |
| 857 | } |
| 858 | |
| 859 | return status; |
| 860 | } |
| 861 | |
| 862 | int Dumpstate::JustRunCommand(const char* command, const char* path, std::vector<const char*>& args, |
| 863 | const CommandOptions& options) const { |
| 864 | bool silent = (options.StdoutMode() == REDIRECT_TO_STDERR); |
| 865 | |
Felipe Leme | b0f669d | 2016-09-26 18:26:11 -0700 | [diff] [blame] | 866 | uint64_t start = DurationReporter::Nanotime(); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 867 | pid_t pid = fork(); |
| 868 | |
| 869 | /* handle error case */ |
| 870 | if (pid < 0) { |
Felipe Leme | 29c3971 | 2016-04-01 10:02:00 -0700 | [diff] [blame] | 871 | if (!silent) printf("*** fork: %s\n", strerror(errno)); |
| 872 | MYLOGE("*** fork: %s\n", strerror(errno)); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 873 | return pid; |
| 874 | } |
| 875 | |
| 876 | /* handle child case */ |
| 877 | if (pid == 0) { |
Felipe Leme | 30dbfa1 | 2016-09-02 12:43:26 -0700 | [diff] [blame] | 878 | if (options.RootMode() == DROP_ROOT && !drop_root_user()) { |
| 879 | if (!silent) |
| 880 | printf("*** failed to drop root before running %s: %s\n", command, strerror(errno)); |
Felipe Leme | 29c3971 | 2016-04-01 10:02:00 -0700 | [diff] [blame] | 881 | MYLOGE("*** could not drop root before running %s: %s\n", command, strerror(errno)); |
Felipe Leme | 73f731c | 2016-03-23 16:47:00 -0700 | [diff] [blame] | 882 | return -1; |
Felipe Leme | cf6a8b4 | 2016-03-11 10:38:19 -0800 | [diff] [blame] | 883 | } |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 884 | |
Felipe Leme | 29c3971 | 2016-04-01 10:02:00 -0700 | [diff] [blame] | 885 | if (silent) { |
| 886 | // Redirect stderr to stdout |
| 887 | dup2(STDERR_FILENO, STDOUT_FILENO); |
| 888 | } |
| 889 | |
John Michelau | e7b6cf1 | 2013-03-07 15:35:35 -0600 | [diff] [blame] | 890 | /* make sure the child dies when dumpstate dies */ |
| 891 | prctl(PR_SET_PDEATHSIG, SIGKILL); |
| 892 | |
Andres Morales | 2e671bb | 2014-08-21 12:38:22 -0700 | [diff] [blame] | 893 | /* just ignore SIGPIPE, will go down with parent's */ |
| 894 | struct sigaction sigact; |
| 895 | memset(&sigact, 0, sizeof(sigact)); |
| 896 | sigact.sa_handler = SIG_IGN; |
| 897 | sigaction(SIGPIPE, &sigact, NULL); |
| 898 | |
Felipe Leme | 6ad9c06 | 2016-09-28 11:58:36 -0700 | [diff] [blame] | 899 | execvp(path, (char**)args.data()); |
Felipe Leme | 30dbfa1 | 2016-09-02 12:43:26 -0700 | [diff] [blame] | 900 | // execvp's result will be handled after waitpid_with_timeout() below, but |
| 901 | // if it failed, it's safer to exit dumpstate. |
| 902 | MYLOGD("execvp on command '%s' failed (error: %s)\n", command, strerror(errno)); |
Felipe Leme | ec72578 | 2016-03-23 11:47:00 -0700 | [diff] [blame] | 903 | fflush(stdout); |
Felipe Leme | 30dbfa1 | 2016-09-02 12:43:26 -0700 | [diff] [blame] | 904 | // Must call _exit (instead of exit), otherwise it will corrupt the zip |
| 905 | // file. |
Felipe Leme | baa85bd | 2016-03-29 13:29:11 -0700 | [diff] [blame] | 906 | _exit(EXIT_FAILURE); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 907 | } |
| 908 | |
| 909 | /* handle parent case */ |
Christopher Ferris | 1a9a338 | 2015-01-30 11:00:52 -0800 | [diff] [blame] | 910 | int status; |
Felipe Leme | 30dbfa1 | 2016-09-02 12:43:26 -0700 | [diff] [blame] | 911 | bool ret = waitpid_with_timeout(pid, options.Timeout(), &status); |
Felipe Leme | b0f669d | 2016-09-26 18:26:11 -0700 | [diff] [blame] | 912 | uint64_t elapsed = DurationReporter::Nanotime() - start; |
Christopher Ferris | 1a9a338 | 2015-01-30 11:00:52 -0800 | [diff] [blame] | 913 | if (!ret) { |
| 914 | if (errno == ETIMEDOUT) { |
Felipe Leme | 30dbfa1 | 2016-09-02 12:43:26 -0700 | [diff] [blame] | 915 | if (!silent) |
| 916 | printf("*** command '%s' timed out after %.3fs (killing pid %d)\n", command, |
| 917 | (float)elapsed / NANOS_PER_SEC, pid); |
Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 918 | MYLOGE("*** command '%s' timed out after %.3fs (killing pid %d)\n", command, |
Felipe Leme | 30dbfa1 | 2016-09-02 12:43:26 -0700 | [diff] [blame] | 919 | (float)elapsed / NANOS_PER_SEC, pid); |
Christopher Ferris | 1a9a338 | 2015-01-30 11:00:52 -0800 | [diff] [blame] | 920 | } else { |
Felipe Leme | 30dbfa1 | 2016-09-02 12:43:26 -0700 | [diff] [blame] | 921 | if (!silent) |
| 922 | printf("*** command '%s': Error after %.4fs (killing pid %d)\n", command, |
| 923 | (float)elapsed / NANOS_PER_SEC, pid); |
| 924 | MYLOGE("command '%s': Error after %.4fs (killing pid %d)\n", command, |
| 925 | (float)elapsed / NANOS_PER_SEC, pid); |
Christopher Ferris | 1a9a338 | 2015-01-30 11:00:52 -0800 | [diff] [blame] | 926 | } |
| 927 | kill(pid, SIGTERM); |
Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 928 | if (!waitpid_with_timeout(pid, 5, nullptr)) { |
Christopher Ferris | 1a9a338 | 2015-01-30 11:00:52 -0800 | [diff] [blame] | 929 | kill(pid, SIGKILL); |
Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 930 | if (!waitpid_with_timeout(pid, 5, nullptr)) { |
Felipe Leme | 30dbfa1 | 2016-09-02 12:43:26 -0700 | [diff] [blame] | 931 | if (!silent) |
| 932 | printf("could not kill command '%s' (pid %d) even with SIGKILL.\n", command, |
| 933 | pid); |
Felipe Leme | 14e034a | 2016-03-30 18:51:03 -0700 | [diff] [blame] | 934 | MYLOGE("could not kill command '%s' (pid %d) even with SIGKILL.\n", command, pid); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 935 | } |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 936 | } |
Christopher Ferris | 1a9a338 | 2015-01-30 11:00:52 -0800 | [diff] [blame] | 937 | return -1; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 938 | } |
Christopher Ferris | 1a9a338 | 2015-01-30 11:00:52 -0800 | [diff] [blame] | 939 | |
| 940 | if (WIFSIGNALED(status)) { |
Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 941 | if (!silent) |
| 942 | printf("*** command '%s' failed: killed by signal %d\n", command, WTERMSIG(status)); |
| 943 | MYLOGE("*** command '%s' failed: killed by signal %d\n", command, WTERMSIG(status)); |
Christopher Ferris | 1a9a338 | 2015-01-30 11:00:52 -0800 | [diff] [blame] | 944 | } else if (WIFEXITED(status) && WEXITSTATUS(status) > 0) { |
Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 945 | status = WEXITSTATUS(status); |
| 946 | if (!silent) printf("*** command '%s' failed: exit code %d\n", command, status); |
| 947 | MYLOGE("*** command '%s' failed: exit code %d\n", command, status); |
Christopher Ferris | 1a9a338 | 2015-01-30 11:00:52 -0800 | [diff] [blame] | 948 | } |
Christopher Ferris | 1a9a338 | 2015-01-30 11:00:52 -0800 | [diff] [blame] | 949 | |
| 950 | return status; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 951 | } |
| 952 | |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 953 | 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] | 954 | const CommandOptions& options, long dumpsysTimeout) { |
Felipe Leme | 5bcce57 | 2016-09-27 09:21:08 -0700 | [diff] [blame] | 955 | long timeout = dumpsysTimeout > 0 ? dumpsysTimeout : options.Timeout(); |
| 956 | 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] | 957 | dumpsys.insert(dumpsys.end(), dumpsys_args.begin(), dumpsys_args.end()); |
Felipe Leme | 678727a | 2016-09-21 17:22:11 -0700 | [diff] [blame] | 958 | RunCommand(title, dumpsys, options); |
Felipe Leme | 30dbfa1 | 2016-09-02 12:43:26 -0700 | [diff] [blame] | 959 | } |
| 960 | |
Felipe Leme | cf6a8b4 | 2016-03-11 10:38:19 -0800 | [diff] [blame] | 961 | bool drop_root_user() { |
| 962 | if (getgid() == AID_SHELL && getuid() == AID_SHELL) { |
Felipe Leme | 26c4157 | 2016-10-06 14:34:43 -0700 | [diff] [blame] | 963 | MYLOGD("drop_root_user(): already running as Shell\n"); |
Felipe Leme | cf6a8b4 | 2016-03-11 10:38:19 -0800 | [diff] [blame] | 964 | return true; |
| 965 | } |
| 966 | /* ensure we will keep capabilities when we drop root */ |
| 967 | if (prctl(PR_SET_KEEPCAPS, 1) < 0) { |
| 968 | MYLOGE("prctl(PR_SET_KEEPCAPS) failed: %s\n", strerror(errno)); |
| 969 | return false; |
| 970 | } |
| 971 | |
| 972 | gid_t groups[] = { AID_LOG, AID_SDCARD_R, AID_SDCARD_RW, |
Ajay Panicker | d886ec4 | 2016-09-14 12:26:46 -0700 | [diff] [blame] | 973 | AID_MOUNT, AID_INET, AID_NET_BW_STATS, AID_READPROC, AID_WAKELOCK, |
| 974 | AID_BLUETOOTH }; |
Felipe Leme | cf6a8b4 | 2016-03-11 10:38:19 -0800 | [diff] [blame] | 975 | if (setgroups(sizeof(groups)/sizeof(groups[0]), groups) != 0) { |
| 976 | MYLOGE("Unable to setgroups, aborting: %s\n", strerror(errno)); |
| 977 | return false; |
| 978 | } |
| 979 | if (setgid(AID_SHELL) != 0) { |
| 980 | MYLOGE("Unable to setgid, aborting: %s\n", strerror(errno)); |
| 981 | return false; |
| 982 | } |
| 983 | if (setuid(AID_SHELL) != 0) { |
| 984 | MYLOGE("Unable to setuid, aborting: %s\n", strerror(errno)); |
| 985 | return false; |
| 986 | } |
| 987 | |
| 988 | struct __user_cap_header_struct capheader; |
| 989 | struct __user_cap_data_struct capdata[2]; |
| 990 | memset(&capheader, 0, sizeof(capheader)); |
| 991 | memset(&capdata, 0, sizeof(capdata)); |
| 992 | capheader.version = _LINUX_CAPABILITY_VERSION_3; |
| 993 | capheader.pid = 0; |
| 994 | |
Wei Liu | f87959e | 2016-08-26 14:51:42 -0700 | [diff] [blame] | 995 | capdata[CAP_TO_INDEX(CAP_SYSLOG)].permitted = |
| 996 | (CAP_TO_MASK(CAP_SYSLOG) | CAP_TO_MASK(CAP_BLOCK_SUSPEND)); |
| 997 | capdata[CAP_TO_INDEX(CAP_SYSLOG)].effective = |
| 998 | (CAP_TO_MASK(CAP_SYSLOG) | CAP_TO_MASK(CAP_BLOCK_SUSPEND)); |
Felipe Leme | cf6a8b4 | 2016-03-11 10:38:19 -0800 | [diff] [blame] | 999 | capdata[0].inheritable = 0; |
| 1000 | capdata[1].inheritable = 0; |
| 1001 | |
| 1002 | if (capset(&capheader, &capdata[0]) < 0) { |
| 1003 | MYLOGE("capset failed: %s\n", strerror(errno)); |
| 1004 | return false; |
| 1005 | } |
| 1006 | |
| 1007 | return true; |
| 1008 | } |
| 1009 | |
Felipe Leme | 36b3f6f | 2015-11-19 15:41:04 -0800 | [diff] [blame] | 1010 | void send_broadcast(const std::string& action, const std::vector<std::string>& args) { |
Felipe Leme | 30dbfa1 | 2016-09-02 12:43:26 -0700 | [diff] [blame] | 1011 | std::vector<std::string> am = {"/system/bin/am", "broadcast", "--user", "0", "-a", action}; |
| 1012 | |
| 1013 | am.insert(am.end(), args.begin(), args.end()); |
| 1014 | |
Felipe Leme | 678727a | 2016-09-21 17:22:11 -0700 | [diff] [blame] | 1015 | RunCommand("", am, CommandOptions::WithTimeout(20) |
| 1016 | .Log("Sending broadcast: '%s'\n") |
| 1017 | .Always() |
| 1018 | .DropRoot() |
| 1019 | .RedirectStderr() |
| 1020 | .Build()); |
Felipe Leme | 36b3f6f | 2015-11-19 15:41:04 -0800 | [diff] [blame] | 1021 | } |
| 1022 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1023 | size_t num_props = 0; |
| 1024 | static char* props[2000]; |
| 1025 | |
| 1026 | static void print_prop(const char *key, const char *name, void *user) { |
| 1027 | (void) user; |
| 1028 | if (num_props < sizeof(props) / sizeof(props[0])) { |
| 1029 | char buf[PROPERTY_KEY_MAX + PROPERTY_VALUE_MAX + 10]; |
| 1030 | snprintf(buf, sizeof(buf), "[%s]: [%s]\n", key, name); |
| 1031 | props[num_props++] = strdup(buf); |
| 1032 | } |
| 1033 | } |
| 1034 | |
| 1035 | static int compare_prop(const void *a, const void *b) { |
| 1036 | return strcmp(*(char * const *) a, *(char * const *) b); |
| 1037 | } |
| 1038 | |
| 1039 | /* prints all the system properties */ |
| 1040 | void print_properties() { |
Felipe Leme | 78f2c86 | 2015-12-21 09:55:22 -0800 | [diff] [blame] | 1041 | const char* title = "SYSTEM PROPERTIES"; |
| 1042 | DurationReporter duration_reporter(title); |
| 1043 | printf("------ %s ------\n", title); |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 1044 | if (IsDryRun()) return; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1045 | size_t i; |
| 1046 | num_props = 0; |
| 1047 | property_list(print_prop, NULL); |
| 1048 | qsort(&props, num_props, sizeof(props[0]), compare_prop); |
| 1049 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1050 | for (i = 0; i < num_props; ++i) { |
| 1051 | fputs(props[i], stdout); |
| 1052 | free(props[i]); |
| 1053 | } |
| 1054 | printf("\n"); |
| 1055 | } |
| 1056 | |
Felipe Leme | 2628e9e | 2016-04-12 16:36:51 -0700 | [diff] [blame] | 1057 | int open_socket(const char *service) { |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1058 | int s = android_get_control_socket(service); |
| 1059 | if (s < 0) { |
Felipe Leme | 107a05f | 2016-03-08 15:11:15 -0800 | [diff] [blame] | 1060 | MYLOGE("android_get_control_socket(%s): %s\n", service, strerror(errno)); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1061 | exit(1); |
| 1062 | } |
Nick Kralevich | cd67e9f | 2015-03-19 11:30:59 -0700 | [diff] [blame] | 1063 | fcntl(s, F_SETFD, FD_CLOEXEC); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1064 | if (listen(s, 4) < 0) { |
Felipe Leme | 107a05f | 2016-03-08 15:11:15 -0800 | [diff] [blame] | 1065 | MYLOGE("listen(control socket): %s\n", strerror(errno)); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1066 | exit(1); |
| 1067 | } |
| 1068 | |
| 1069 | struct sockaddr addr; |
| 1070 | socklen_t alen = sizeof(addr); |
| 1071 | int fd = accept(s, &addr, &alen); |
| 1072 | if (fd < 0) { |
Felipe Leme | 107a05f | 2016-03-08 15:11:15 -0800 | [diff] [blame] | 1073 | MYLOGE("accept(control socket): %s\n", strerror(errno)); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1074 | exit(1); |
| 1075 | } |
| 1076 | |
Felipe Leme | 2628e9e | 2016-04-12 16:36:51 -0700 | [diff] [blame] | 1077 | return fd; |
| 1078 | } |
| 1079 | |
| 1080 | /* redirect output to a service control socket */ |
| 1081 | void redirect_to_socket(FILE *redirect, const char *service) { |
| 1082 | int fd = open_socket(service); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1083 | fflush(redirect); |
| 1084 | dup2(fd, fileno(redirect)); |
| 1085 | close(fd); |
| 1086 | } |
| 1087 | |
Felipe Leme | 2628e9e | 2016-04-12 16:36:51 -0700 | [diff] [blame] | 1088 | // 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] | 1089 | void create_parent_dirs(const char *path) { |
Srinath Sridharan | fdf52d3 | 2016-02-01 15:50:22 -0800 | [diff] [blame] | 1090 | char *chp = const_cast<char *> (path); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1091 | |
| 1092 | /* skip initial slash */ |
| 1093 | if (chp[0] == '/') |
| 1094 | chp++; |
| 1095 | |
| 1096 | /* create leading directories, if necessary */ |
Felipe Leme | 111b9d0 | 2016-02-03 09:28:24 -0800 | [diff] [blame] | 1097 | struct stat dir_stat; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1098 | while (chp && chp[0]) { |
| 1099 | chp = strchr(chp, '/'); |
| 1100 | if (chp) { |
| 1101 | *chp = 0; |
Felipe Leme | 111b9d0 | 2016-02-03 09:28:24 -0800 | [diff] [blame] | 1102 | if (stat(path, &dir_stat) == -1 || !S_ISDIR(dir_stat.st_mode)) { |
Felipe Leme | cbce55d | 2016-02-08 09:53:18 -0800 | [diff] [blame] | 1103 | MYLOGI("Creating directory %s\n", path); |
Felipe Leme | 111b9d0 | 2016-02-03 09:28:24 -0800 | [diff] [blame] | 1104 | if (mkdir(path, 0770)) { /* drwxrwx--- */ |
Felipe Leme | cbce55d | 2016-02-08 09:53:18 -0800 | [diff] [blame] | 1105 | MYLOGE("Unable to create directory %s: %s\n", path, strerror(errno)); |
Felipe Leme | 111b9d0 | 2016-02-03 09:28:24 -0800 | [diff] [blame] | 1106 | } else if (chown(path, AID_SHELL, AID_SHELL)) { |
Felipe Leme | cbce55d | 2016-02-08 09:53:18 -0800 | [diff] [blame] | 1107 | 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] | 1108 | } |
| 1109 | } |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1110 | *chp++ = '/'; |
| 1111 | } |
| 1112 | } |
Felipe Leme | 111b9d0 | 2016-02-03 09:28:24 -0800 | [diff] [blame] | 1113 | } |
| 1114 | |
Felipe Leme | 0f3fb20 | 2016-06-10 17:10:53 -0700 | [diff] [blame] | 1115 | void _redirect_to_file(FILE *redirect, char *path, int truncate_flag) { |
Felipe Leme | 111b9d0 | 2016-02-03 09:28:24 -0800 | [diff] [blame] | 1116 | create_parent_dirs(path); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1117 | |
Felipe Leme | 0f3fb20 | 2016-06-10 17:10:53 -0700 | [diff] [blame] | 1118 | int fd = TEMP_FAILURE_RETRY(open(path, |
| 1119 | O_WRONLY | O_CREAT | truncate_flag | O_CLOEXEC | O_NOFOLLOW, |
Christopher Ferris | ff4a4dc | 2015-02-09 16:24:47 -0800 | [diff] [blame] | 1120 | S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1121 | if (fd < 0) { |
Felipe Leme | 107a05f | 2016-03-08 15:11:15 -0800 | [diff] [blame] | 1122 | MYLOGE("%s: %s\n", path, strerror(errno)); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1123 | exit(1); |
| 1124 | } |
| 1125 | |
Christopher Ferris | ff4a4dc | 2015-02-09 16:24:47 -0800 | [diff] [blame] | 1126 | TEMP_FAILURE_RETRY(dup2(fd, fileno(redirect))); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1127 | close(fd); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1128 | } |
| 1129 | |
Felipe Leme | 0f3fb20 | 2016-06-10 17:10:53 -0700 | [diff] [blame] | 1130 | void redirect_to_file(FILE *redirect, char *path) { |
| 1131 | _redirect_to_file(redirect, path, O_TRUNC); |
| 1132 | } |
| 1133 | |
| 1134 | void redirect_to_existing_file(FILE *redirect, char *path) { |
| 1135 | _redirect_to_file(redirect, path, O_APPEND); |
| 1136 | } |
| 1137 | |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 1138 | static bool should_dump_native_traces(const char* path) { |
| 1139 | for (const char** p = native_processes_to_dump; *p; p++) { |
| 1140 | if (!strcmp(*p, path)) { |
| 1141 | return true; |
| 1142 | } |
| 1143 | } |
| 1144 | return false; |
| 1145 | } |
| 1146 | |
| 1147 | /* dump Dalvik and native stack traces, return the trace file location (NULL if none) */ |
| 1148 | const char *dump_traces() { |
Felipe Leme | e184f66 | 2016-10-27 10:04:47 -0700 | [diff] [blame] | 1149 | DurationReporter duration_reporter("DUMP TRACES"); |
Felipe Leme | d402e7d | 2016-08-03 09:22:27 -0700 | [diff] [blame] | 1150 | |
Felipe Leme | 96c2bbb | 2016-09-26 09:21:21 -0700 | [diff] [blame] | 1151 | const char* result = nullptr; |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 1152 | |
Felipe Leme | e184f66 | 2016-10-27 10:04:47 -0700 | [diff] [blame] | 1153 | std::string traces_path = android::base::GetProperty("dalvik.vm.stack-trace-file", ""); |
| 1154 | if (traces_path.empty()) return nullptr; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1155 | |
| 1156 | /* move the old traces.txt (if any) out of the way temporarily */ |
Felipe Leme | e184f66 | 2016-10-27 10:04:47 -0700 | [diff] [blame] | 1157 | std::string anrtraces_path = traces_path + ".anr"; |
| 1158 | if (rename(traces_path.c_str(), anrtraces_path.c_str()) && errno != ENOENT) { |
| 1159 | 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] | 1160 | 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] | 1161 | } |
| 1162 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1163 | /* create a new, empty traces.txt file to receive stack dumps */ |
Felipe Leme | e184f66 | 2016-10-27 10:04:47 -0700 | [diff] [blame] | 1164 | int fd = TEMP_FAILURE_RETRY(open(traces_path.c_str(), |
Felipe Leme | 96c2bbb | 2016-09-26 09:21:21 -0700 | [diff] [blame] | 1165 | O_CREAT | O_WRONLY | O_TRUNC | O_NOFOLLOW | O_CLOEXEC, |
| 1166 | 0666)); /* -rw-rw-rw- */ |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1167 | if (fd < 0) { |
Felipe Leme | e184f66 | 2016-10-27 10:04:47 -0700 | [diff] [blame] | 1168 | MYLOGE("%s: %s\n", traces_path.c_str(), strerror(errno)); |
Felipe Leme | 96c2bbb | 2016-09-26 09:21:21 -0700 | [diff] [blame] | 1169 | return nullptr; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1170 | } |
Nick Kralevich | c7f1fe2 | 2012-04-06 09:31:28 -0700 | [diff] [blame] | 1171 | int chmod_ret = fchmod(fd, 0666); |
| 1172 | if (chmod_ret < 0) { |
Felipe Leme | e184f66 | 2016-10-27 10:04:47 -0700 | [diff] [blame] | 1173 | 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] | 1174 | close(fd); |
Felipe Leme | 96c2bbb | 2016-09-26 09:21:21 -0700 | [diff] [blame] | 1175 | return nullptr; |
Nick Kralevich | c7f1fe2 | 2012-04-06 09:31:28 -0700 | [diff] [blame] | 1176 | } |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1177 | |
Felipe Leme | 8620bb4 | 2015-11-10 11:04:45 -0800 | [diff] [blame] | 1178 | /* Variables below must be initialized before 'goto' statements */ |
| 1179 | int dalvik_found = 0; |
| 1180 | int ifd, wfd = -1; |
| 1181 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1182 | /* walk /proc and kill -QUIT all Dalvik processes */ |
| 1183 | DIR *proc = opendir("/proc"); |
| 1184 | if (proc == NULL) { |
Felipe Leme | 107a05f | 2016-03-08 15:11:15 -0800 | [diff] [blame] | 1185 | MYLOGE("/proc: %s\n", strerror(errno)); |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 1186 | goto error_close_fd; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1187 | } |
| 1188 | |
| 1189 | /* use inotify to find when processes are done dumping */ |
Felipe Leme | 8620bb4 | 2015-11-10 11:04:45 -0800 | [diff] [blame] | 1190 | ifd = inotify_init(); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1191 | if (ifd < 0) { |
Felipe Leme | 107a05f | 2016-03-08 15:11:15 -0800 | [diff] [blame] | 1192 | MYLOGE("inotify_init: %s\n", strerror(errno)); |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 1193 | goto error_close_fd; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1194 | } |
| 1195 | |
Felipe Leme | e184f66 | 2016-10-27 10:04:47 -0700 | [diff] [blame] | 1196 | wfd = inotify_add_watch(ifd, traces_path.c_str(), IN_CLOSE_WRITE); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1197 | if (wfd < 0) { |
Felipe Leme | e184f66 | 2016-10-27 10:04:47 -0700 | [diff] [blame] | 1198 | 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] | 1199 | goto error_close_ifd; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1200 | } |
| 1201 | |
| 1202 | struct dirent *d; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1203 | while ((d = readdir(proc))) { |
| 1204 | int pid = atoi(d->d_name); |
| 1205 | if (pid <= 0) continue; |
| 1206 | |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 1207 | char path[PATH_MAX]; |
| 1208 | char data[PATH_MAX]; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1209 | snprintf(path, sizeof(path), "/proc/%d/exe", pid); |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 1210 | ssize_t len = readlink(path, data, sizeof(data) - 1); |
| 1211 | if (len <= 0) { |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1212 | continue; |
| 1213 | } |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 1214 | data[len] = '\0'; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1215 | |
Colin Cross | 0d6180f | 2014-07-16 19:00:46 -0700 | [diff] [blame] | 1216 | if (!strncmp(data, "/system/bin/app_process", strlen("/system/bin/app_process"))) { |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 1217 | /* skip zygote -- it won't dump its stack anyway */ |
| 1218 | snprintf(path, sizeof(path), "/proc/%d/cmdline", pid); |
Nick Kralevich | cd67e9f | 2015-03-19 11:30:59 -0700 | [diff] [blame] | 1219 | int cfd = TEMP_FAILURE_RETRY(open(path, O_RDONLY | O_CLOEXEC)); |
Jeff Brown | 1dc94e3 | 2014-09-11 14:15:27 -0700 | [diff] [blame] | 1220 | len = read(cfd, data, sizeof(data) - 1); |
| 1221 | close(cfd); |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 1222 | if (len <= 0) { |
| 1223 | continue; |
| 1224 | } |
| 1225 | data[len] = '\0'; |
Colin Cross | 0d6180f | 2014-07-16 19:00:46 -0700 | [diff] [blame] | 1226 | if (!strncmp(data, "zygote", strlen("zygote"))) { |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 1227 | continue; |
| 1228 | } |
| 1229 | |
| 1230 | ++dalvik_found; |
Felipe Leme | b0f669d | 2016-09-26 18:26:11 -0700 | [diff] [blame] | 1231 | uint64_t start = DurationReporter::Nanotime(); |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 1232 | if (kill(pid, SIGQUIT)) { |
Felipe Leme | 107a05f | 2016-03-08 15:11:15 -0800 | [diff] [blame] | 1233 | MYLOGE("kill(%d, SIGQUIT): %s\n", pid, strerror(errno)); |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 1234 | continue; |
| 1235 | } |
| 1236 | |
| 1237 | /* wait for the writable-close notification from inotify */ |
| 1238 | struct pollfd pfd = { ifd, POLLIN, 0 }; |
Felipe Leme | 6188412 | 2016-06-13 09:23:30 -0700 | [diff] [blame] | 1239 | int ret = poll(&pfd, 1, TRACE_DUMP_TIMEOUT_MS); |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 1240 | if (ret < 0) { |
Felipe Leme | 107a05f | 2016-03-08 15:11:15 -0800 | [diff] [blame] | 1241 | MYLOGE("poll: %s\n", strerror(errno)); |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 1242 | } else if (ret == 0) { |
Felipe Leme | 107a05f | 2016-03-08 15:11:15 -0800 | [diff] [blame] | 1243 | MYLOGE("warning: timed out dumping pid %d\n", pid); |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 1244 | } else { |
| 1245 | struct inotify_event ie; |
| 1246 | read(ifd, &ie, sizeof(ie)); |
| 1247 | } |
Jeff Brown | 1dc94e3 | 2014-09-11 14:15:27 -0700 | [diff] [blame] | 1248 | |
| 1249 | if (lseek(fd, 0, SEEK_END) < 0) { |
Felipe Leme | 107a05f | 2016-03-08 15:11:15 -0800 | [diff] [blame] | 1250 | MYLOGE("lseek: %s\n", strerror(errno)); |
Jeff Brown | 1dc94e3 | 2014-09-11 14:15:27 -0700 | [diff] [blame] | 1251 | } else { |
Felipe Leme | b0f669d | 2016-09-26 18:26:11 -0700 | [diff] [blame] | 1252 | dprintf(fd, "[dump dalvik stack %d: %.3fs elapsed]\n", pid, |
| 1253 | (float)(DurationReporter::Nanotime() - start) / NANOS_PER_SEC); |
Jeff Brown | 1dc94e3 | 2014-09-11 14:15:27 -0700 | [diff] [blame] | 1254 | } |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 1255 | } else if (should_dump_native_traces(data)) { |
| 1256 | /* dump native process if appropriate */ |
| 1257 | if (lseek(fd, 0, SEEK_END) < 0) { |
Felipe Leme | 107a05f | 2016-03-08 15:11:15 -0800 | [diff] [blame] | 1258 | MYLOGE("lseek: %s\n", strerror(errno)); |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 1259 | } else { |
Christopher Ferris | 31ef855 | 2015-01-14 13:23:30 -0800 | [diff] [blame] | 1260 | static uint16_t timeout_failures = 0; |
Felipe Leme | b0f669d | 2016-09-26 18:26:11 -0700 | [diff] [blame] | 1261 | uint64_t start = DurationReporter::Nanotime(); |
Christopher Ferris | 31ef855 | 2015-01-14 13:23:30 -0800 | [diff] [blame] | 1262 | |
| 1263 | /* If 3 backtrace dumps fail in a row, consider debuggerd dead. */ |
| 1264 | if (timeout_failures == 3) { |
| 1265 | dprintf(fd, "too many stack dump failures, skipping...\n"); |
| 1266 | } else if (dump_backtrace_to_file_timeout(pid, fd, 20) == -1) { |
| 1267 | dprintf(fd, "dumping failed, likely due to a timeout\n"); |
| 1268 | timeout_failures++; |
| 1269 | } else { |
| 1270 | timeout_failures = 0; |
| 1271 | } |
Felipe Leme | b0f669d | 2016-09-26 18:26:11 -0700 | [diff] [blame] | 1272 | dprintf(fd, "[dump native stack %d: %.3fs elapsed]\n", pid, |
| 1273 | (float)(DurationReporter::Nanotime() - start) / NANOS_PER_SEC); |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 1274 | } |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1275 | } |
| 1276 | } |
| 1277 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1278 | if (dalvik_found == 0) { |
Felipe Leme | 107a05f | 2016-03-08 15:11:15 -0800 | [diff] [blame] | 1279 | MYLOGE("Warning: no Dalvik processes found to dump stacks\n"); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1280 | } |
| 1281 | |
Felipe Leme | e184f66 | 2016-10-27 10:04:47 -0700 | [diff] [blame] | 1282 | static std::string dumptraces_path = android::base::StringPrintf( |
| 1283 | "%s/bugreport-%s", dirname(traces_path.c_str()), basename(traces_path.c_str())); |
| 1284 | if (rename(traces_path.c_str(), dumptraces_path.c_str())) { |
| 1285 | MYLOGE("rename(%s, %s): %s\n", traces_path.c_str(), dumptraces_path.c_str(), |
| 1286 | strerror(errno)); |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 1287 | goto error_close_ifd; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1288 | } |
Felipe Leme | e184f66 | 2016-10-27 10:04:47 -0700 | [diff] [blame] | 1289 | result = dumptraces_path.c_str(); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1290 | |
| 1291 | /* replace the saved [ANR] traces.txt file */ |
Felipe Leme | e184f66 | 2016-10-27 10:04:47 -0700 | [diff] [blame] | 1292 | rename(anrtraces_path.c_str(), traces_path.c_str()); |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 1293 | |
| 1294 | error_close_ifd: |
| 1295 | close(ifd); |
| 1296 | error_close_fd: |
| 1297 | close(fd); |
| 1298 | return result; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1299 | } |
| 1300 | |
Sreeram Ramachandran | 2b3bba3 | 2014-07-08 15:40:55 -0700 | [diff] [blame] | 1301 | void dump_route_tables() { |
Felipe Leme | 78f2c86 | 2015-12-21 09:55:22 -0800 | [diff] [blame] | 1302 | DurationReporter duration_reporter("DUMP ROUTE TABLES"); |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 1303 | if (IsDryRun()) return; |
Sreeram Ramachandran | 2b3bba3 | 2014-07-08 15:40:55 -0700 | [diff] [blame] | 1304 | const char* const RT_TABLES_PATH = "/data/misc/net/rt_tables"; |
Felipe Leme | c7fe8fe | 2016-09-21 18:13:20 -0700 | [diff] [blame] | 1305 | ds.DumpFile("RT_TABLES", RT_TABLES_PATH); |
Nick Kralevich | cd67e9f | 2015-03-19 11:30:59 -0700 | [diff] [blame] | 1306 | FILE* fp = fopen(RT_TABLES_PATH, "re"); |
Sreeram Ramachandran | 2b3bba3 | 2014-07-08 15:40:55 -0700 | [diff] [blame] | 1307 | if (!fp) { |
| 1308 | printf("*** %s: %s\n", RT_TABLES_PATH, strerror(errno)); |
| 1309 | return; |
| 1310 | } |
| 1311 | char table[16]; |
| 1312 | // Each line has an integer (the table number), a space, and a string (the table name). We only |
| 1313 | // need the table number. It's a 32-bit unsigned number, so max 10 chars. Skip the table name. |
| 1314 | // Add a fixed max limit so this doesn't go awry. |
| 1315 | 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] | 1316 | RunCommand("ROUTE TABLE IPv4", {"ip", "-4", "route", "show", "table", table}); |
| 1317 | RunCommand("ROUTE TABLE IPv6", {"ip", "-6", "route", "show", "table", table}); |
Sreeram Ramachandran | 2b3bba3 | 2014-07-08 15:40:55 -0700 | [diff] [blame] | 1318 | } |
| 1319 | fclose(fp); |
| 1320 | } |
Felipe Leme | 71bbfc5 | 2015-11-23 14:14:51 -0800 | [diff] [blame] | 1321 | |
Felipe Leme | 71bbfc5 | 2015-11-23 14:14:51 -0800 | [diff] [blame] | 1322 | // TODO: make this function thread safe if sections are generated in parallel. |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 1323 | void Dumpstate::UpdateProgress(int delta) { |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 1324 | if (!update_progress_) return; |
Felipe Leme | 71bbfc5 | 2015-11-23 14:14:51 -0800 | [diff] [blame] | 1325 | |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 1326 | progress_ += delta; |
Felipe Leme | 71bbfc5 | 2015-11-23 14:14:51 -0800 | [diff] [blame] | 1327 | |
Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 1328 | // TODO: remove property support once Shell uses IDumpstateListener |
Felipe Leme | 71bbfc5 | 2015-11-23 14:14:51 -0800 | [diff] [blame] | 1329 | char key[PROPERTY_KEY_MAX]; |
| 1330 | char value[PROPERTY_VALUE_MAX]; |
Felipe Leme | ad5f6c4 | 2015-11-30 14:26:46 -0800 | [diff] [blame] | 1331 | |
| 1332 | // adjusts max on the fly |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 1333 | if (progress_ > weight_total_) { |
| 1334 | int new_total = weight_total_ * 1.2; |
| 1335 | MYLOGD("Adjusting total weight from %d to %d\n", weight_total_, new_total); |
| 1336 | weight_total_ = new_total; |
Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 1337 | |
| 1338 | if (listener_ != nullptr) { |
| 1339 | listener_->onMaxProgressUpdated(weight_total_); |
| 1340 | } else { |
| 1341 | snprintf(key, sizeof(key), "dumpstate.%d.max", pid_); |
| 1342 | snprintf(value, sizeof(value), "%d", weight_total_); |
| 1343 | int status = property_set(key, value); |
| 1344 | if (status != 0) { |
| 1345 | MYLOGE("Could not update max weight by setting system property %s to %s: %d\n", key, |
| 1346 | value, status); |
| 1347 | } |
Felipe Leme | ad5f6c4 | 2015-11-30 14:26:46 -0800 | [diff] [blame] | 1348 | } |
| 1349 | } |
| 1350 | |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 1351 | if (control_socket_fd_ >= 0) { |
| 1352 | dprintf(control_socket_fd_, "PROGRESS:%d/%d\n", progress_, weight_total_); |
| 1353 | fsync(control_socket_fd_); |
Felipe Leme | 02b7e00 | 2016-07-22 12:03:20 -0700 | [diff] [blame] | 1354 | } |
| 1355 | |
Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 1356 | if (listener_ != nullptr) { |
| 1357 | if (progress_ % 100 == 0) { |
| 1358 | // We don't want to spam logcat, so only log multiples of 100. |
| 1359 | MYLOGD("Setting progress (%s): %d/%d\n", listener_name_.c_str(), progress_, |
| 1360 | weight_total_); |
| 1361 | } else { |
| 1362 | // stderr is ignored on normal invocations, but useful when calling |
| 1363 | // /system/bin/dumpstate directly for debuggging. |
| 1364 | fprintf(stderr, "Setting progress (%s): %d/%d\n", listener_name_.c_str(), progress_, |
| 1365 | weight_total_); |
| 1366 | } |
| 1367 | listener_->onProgressUpdated(progress_); |
| 1368 | } else { |
| 1369 | snprintf(key, sizeof(key), "dumpstate.%d.progress", pid_); |
| 1370 | snprintf(value, sizeof(value), "%d", progress_); |
| 1371 | |
| 1372 | if (progress_ % 100 == 0) { |
| 1373 | // We don't want to spam logcat, so only log multiples of 100. |
| 1374 | MYLOGD("Setting progress (%s): %s/%d\n", key, value, weight_total_); |
| 1375 | } else { |
| 1376 | // stderr is ignored on normal invocations, but useful when calling |
| 1377 | // /system/bin/dumpstate directly for debuggging. |
| 1378 | fprintf(stderr, "Setting progress (%s): %s/%d\n", key, value, weight_total_); |
| 1379 | } |
| 1380 | |
| 1381 | int status = property_set(key, value); |
| 1382 | if (status) { |
| 1383 | MYLOGE("Could not update progress by setting system property %s to %s: %d\n", key, |
| 1384 | value, status); |
| 1385 | } |
Felipe Leme | 71bbfc5 | 2015-11-23 14:14:51 -0800 | [diff] [blame] | 1386 | } |
| 1387 | } |
Felipe Leme | e338bf6 | 2015-12-07 14:03:50 -0800 | [diff] [blame] | 1388 | |
Felipe Leme | bbaf3c1 | 2016-10-11 14:32:25 -0700 | [diff] [blame] | 1389 | void Dumpstate::TakeScreenshot(const std::string& path) { |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 1390 | const std::string& real_path = path.empty() ? screenshot_path_ : path; |
Felipe Leme | bbaf3c1 | 2016-10-11 14:32:25 -0700 | [diff] [blame] | 1391 | int status = |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 1392 | RunCommand("", {"/system/bin/screencap", "-p", real_path}, |
Felipe Leme | bbaf3c1 | 2016-10-11 14:32:25 -0700 | [diff] [blame] | 1393 | CommandOptions::WithTimeout(10).Always().DropRoot().RedirectStderr().Build()); |
| 1394 | if (status == 0) { |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 1395 | MYLOGD("Screenshot saved on %s\n", real_path.c_str()); |
Felipe Leme | bbaf3c1 | 2016-10-11 14:32:25 -0700 | [diff] [blame] | 1396 | } else { |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 1397 | MYLOGE("Failed to take screenshot on %s\n", real_path.c_str()); |
Felipe Leme | bbaf3c1 | 2016-10-11 14:32:25 -0700 | [diff] [blame] | 1398 | } |
Felipe Leme | e338bf6 | 2015-12-07 14:03:50 -0800 | [diff] [blame] | 1399 | } |
Mark Salyzyn | f55d402 | 2015-12-11 07:32:31 -0800 | [diff] [blame] | 1400 | |
Felipe Leme | 0c80cf0 | 2016-01-05 13:25:34 -0800 | [diff] [blame] | 1401 | void vibrate(FILE* vibrator, int ms) { |
| 1402 | fprintf(vibrator, "%d\n", ms); |
| 1403 | fflush(vibrator); |
| 1404 | } |
| 1405 | |
| 1406 | bool is_dir(const char* pathname) { |
| 1407 | struct stat info; |
| 1408 | if (stat(pathname, &info) == -1) { |
| 1409 | return false; |
| 1410 | } |
| 1411 | return S_ISDIR(info.st_mode); |
| 1412 | } |
| 1413 | |
| 1414 | time_t get_mtime(int fd, time_t default_mtime) { |
| 1415 | struct stat info; |
| 1416 | if (fstat(fd, &info) == -1) { |
| 1417 | return default_mtime; |
| 1418 | } |
| 1419 | return info.st_mtime; |
| 1420 | } |
| 1421 | |
Mark Salyzyn | 8c8130e | 2015-12-09 11:21:28 -0800 | [diff] [blame] | 1422 | void dump_emmc_ecsd(const char *ext_csd_path) { |
Mark Salyzyn | 290f4b9 | 2016-05-16 08:33:59 -0700 | [diff] [blame] | 1423 | // List of interesting offsets |
Mark Salyzyn | 8c8130e | 2015-12-09 11:21:28 -0800 | [diff] [blame] | 1424 | struct hex { |
| 1425 | char str[2]; |
Mark Salyzyn | 8c8130e | 2015-12-09 11:21:28 -0800 | [diff] [blame] | 1426 | }; |
Mark Salyzyn | 290f4b9 | 2016-05-16 08:33:59 -0700 | [diff] [blame] | 1427 | static const size_t EXT_CSD_REV = 192 * sizeof(hex); |
| 1428 | static const size_t EXT_PRE_EOL_INFO = 267 * sizeof(hex); |
| 1429 | static const size_t EXT_DEVICE_LIFE_TIME_EST_TYP_A = 268 * sizeof(hex); |
| 1430 | static const size_t EXT_DEVICE_LIFE_TIME_EST_TYP_B = 269 * sizeof(hex); |
| 1431 | |
| 1432 | std::string buffer; |
| 1433 | if (!android::base::ReadFileToString(ext_csd_path, &buffer)) { |
| 1434 | return; |
| 1435 | } |
Mark Salyzyn | 8c8130e | 2015-12-09 11:21:28 -0800 | [diff] [blame] | 1436 | |
| 1437 | printf("------ %s Extended CSD ------\n", ext_csd_path); |
| 1438 | |
Mark Salyzyn | 290f4b9 | 2016-05-16 08:33:59 -0700 | [diff] [blame] | 1439 | if (buffer.length() < (EXT_CSD_REV + sizeof(hex))) { |
| 1440 | printf("*** %s: truncated content %zu\n\n", ext_csd_path, buffer.length()); |
Mark Salyzyn | 8c8130e | 2015-12-09 11:21:28 -0800 | [diff] [blame] | 1441 | return; |
| 1442 | } |
| 1443 | |
Mark Salyzyn | 290f4b9 | 2016-05-16 08:33:59 -0700 | [diff] [blame] | 1444 | int ext_csd_rev = 0; |
| 1445 | std::string sub = buffer.substr(EXT_CSD_REV, sizeof(hex)); |
| 1446 | if (sscanf(sub.c_str(), "%2x", &ext_csd_rev) != 1) { |
| 1447 | printf("*** %s: EXT_CSD_REV parse error \"%s\"\n\n", |
| 1448 | ext_csd_path, sub.c_str()); |
Mark Salyzyn | 8c8130e | 2015-12-09 11:21:28 -0800 | [diff] [blame] | 1449 | return; |
| 1450 | } |
| 1451 | |
Mark Salyzyn | 290f4b9 | 2016-05-16 08:33:59 -0700 | [diff] [blame] | 1452 | static const char *ver_str[] = { |
| 1453 | "4.0", "4.1", "4.2", "4.3", "Obsolete", "4.41", "4.5", "5.0" |
| 1454 | }; |
Mark Salyzyn | 8c8130e | 2015-12-09 11:21:28 -0800 | [diff] [blame] | 1455 | printf("rev 1.%d (MMC %s)\n", |
| 1456 | ext_csd_rev, |
| 1457 | (ext_csd_rev < (int)(sizeof(ver_str) / sizeof(ver_str[0]))) ? |
| 1458 | ver_str[ext_csd_rev] : |
| 1459 | "Unknown"); |
| 1460 | if (ext_csd_rev < 7) { |
| 1461 | printf("\n"); |
| 1462 | return; |
| 1463 | } |
| 1464 | |
Mark Salyzyn | 290f4b9 | 2016-05-16 08:33:59 -0700 | [diff] [blame] | 1465 | if (buffer.length() < (EXT_PRE_EOL_INFO + sizeof(hex))) { |
| 1466 | printf("*** %s: truncated content %zu\n\n", ext_csd_path, buffer.length()); |
Mark Salyzyn | 8c8130e | 2015-12-09 11:21:28 -0800 | [diff] [blame] | 1467 | return; |
| 1468 | } |
| 1469 | |
Mark Salyzyn | 290f4b9 | 2016-05-16 08:33:59 -0700 | [diff] [blame] | 1470 | int ext_pre_eol_info = 0; |
| 1471 | sub = buffer.substr(EXT_PRE_EOL_INFO, sizeof(hex)); |
| 1472 | if (sscanf(sub.c_str(), "%2x", &ext_pre_eol_info) != 1) { |
| 1473 | printf("*** %s: PRE_EOL_INFO parse error \"%s\"\n\n", |
| 1474 | ext_csd_path, sub.c_str()); |
Mark Salyzyn | 8c8130e | 2015-12-09 11:21:28 -0800 | [diff] [blame] | 1475 | return; |
| 1476 | } |
Mark Salyzyn | 290f4b9 | 2016-05-16 08:33:59 -0700 | [diff] [blame] | 1477 | |
| 1478 | static const char *eol_str[] = { |
| 1479 | "Undefined", |
| 1480 | "Normal", |
| 1481 | "Warning (consumed 80% of reserve)", |
| 1482 | "Urgent (consumed 90% of reserve)" |
| 1483 | }; |
Mark Salyzyn | 8c8130e | 2015-12-09 11:21:28 -0800 | [diff] [blame] | 1484 | printf("PRE_EOL_INFO %d (MMC %s)\n", |
| 1485 | ext_pre_eol_info, |
| 1486 | eol_str[(ext_pre_eol_info < (int) |
| 1487 | (sizeof(eol_str) / sizeof(eol_str[0]))) ? |
| 1488 | ext_pre_eol_info : 0]); |
| 1489 | |
| 1490 | for (size_t lifetime = EXT_DEVICE_LIFE_TIME_EST_TYP_A; |
| 1491 | lifetime <= EXT_DEVICE_LIFE_TIME_EST_TYP_B; |
Mark Salyzyn | 290f4b9 | 2016-05-16 08:33:59 -0700 | [diff] [blame] | 1492 | lifetime += sizeof(hex)) { |
Mark Salyzyn | 8c8130e | 2015-12-09 11:21:28 -0800 | [diff] [blame] | 1493 | int ext_device_life_time_est; |
| 1494 | static const char *est_str[] = { |
| 1495 | "Undefined", |
| 1496 | "0-10% of device lifetime used", |
| 1497 | "10-20% of device lifetime used", |
| 1498 | "20-30% of device lifetime used", |
| 1499 | "30-40% of device lifetime used", |
| 1500 | "40-50% of device lifetime used", |
| 1501 | "50-60% of device lifetime used", |
| 1502 | "60-70% of device lifetime used", |
| 1503 | "70-80% of device lifetime used", |
| 1504 | "80-90% of device lifetime used", |
| 1505 | "90-100% of device lifetime used", |
| 1506 | "Exceeded the maximum estimated device lifetime", |
| 1507 | }; |
| 1508 | |
Mark Salyzyn | 290f4b9 | 2016-05-16 08:33:59 -0700 | [diff] [blame] | 1509 | if (buffer.length() < (lifetime + sizeof(hex))) { |
| 1510 | printf("*** %s: truncated content %zu\n", ext_csd_path, buffer.length()); |
Mark Salyzyn | 8c8130e | 2015-12-09 11:21:28 -0800 | [diff] [blame] | 1511 | break; |
| 1512 | } |
| 1513 | |
| 1514 | ext_device_life_time_est = 0; |
Mark Salyzyn | 290f4b9 | 2016-05-16 08:33:59 -0700 | [diff] [blame] | 1515 | sub = buffer.substr(lifetime, sizeof(hex)); |
| 1516 | if (sscanf(sub.c_str(), "%2x", &ext_device_life_time_est) != 1) { |
| 1517 | printf("*** %s: DEVICE_LIFE_TIME_EST_TYP_%c parse error \"%s\"\n", |
Mark Salyzyn | 8c8130e | 2015-12-09 11:21:28 -0800 | [diff] [blame] | 1518 | ext_csd_path, |
Mark Salyzyn | 290f4b9 | 2016-05-16 08:33:59 -0700 | [diff] [blame] | 1519 | (unsigned)((lifetime - EXT_DEVICE_LIFE_TIME_EST_TYP_A) / |
| 1520 | sizeof(hex)) + 'A', |
| 1521 | sub.c_str()); |
Mark Salyzyn | 8c8130e | 2015-12-09 11:21:28 -0800 | [diff] [blame] | 1522 | continue; |
| 1523 | } |
| 1524 | printf("DEVICE_LIFE_TIME_EST_TYP_%c %d (MMC %s)\n", |
Mark Salyzyn | 290f4b9 | 2016-05-16 08:33:59 -0700 | [diff] [blame] | 1525 | (unsigned)((lifetime - EXT_DEVICE_LIFE_TIME_EST_TYP_A) / |
| 1526 | sizeof(hex)) + 'A', |
Mark Salyzyn | 8c8130e | 2015-12-09 11:21:28 -0800 | [diff] [blame] | 1527 | ext_device_life_time_est, |
| 1528 | est_str[(ext_device_life_time_est < (int) |
| 1529 | (sizeof(est_str) / sizeof(est_str[0]))) ? |
| 1530 | ext_device_life_time_est : 0]); |
| 1531 | } |
| 1532 | |
| 1533 | printf("\n"); |
| 1534 | } |