Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Felipe Leme | 8268ed2 | 2016-08-02 18:18:25 -0700 | [diff] [blame] | 17 | #ifndef FRAMEWORK_NATIVE_CMD_DUMPSTATE_H_ |
| 18 | #define FRAMEWORK_NATIVE_CMD_DUMPSTATE_H_ |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 19 | |
Felipe Leme | cbce55d | 2016-02-08 09:53:18 -0800 | [diff] [blame] | 20 | #ifndef MYLOGD |
Felipe Leme | 60869c9 | 2016-02-09 16:07:20 -0800 | [diff] [blame] | 21 | #define MYLOGD(...) fprintf(stderr, __VA_ARGS__); ALOGD(__VA_ARGS__); |
Felipe Leme | cbce55d | 2016-02-08 09:53:18 -0800 | [diff] [blame] | 22 | #endif |
| 23 | |
| 24 | #ifndef MYLOGI |
Felipe Leme | 60869c9 | 2016-02-09 16:07:20 -0800 | [diff] [blame] | 25 | #define MYLOGI(...) fprintf(stderr, __VA_ARGS__); ALOGI(__VA_ARGS__); |
Felipe Leme | cbce55d | 2016-02-08 09:53:18 -0800 | [diff] [blame] | 26 | #endif |
| 27 | |
| 28 | #ifndef MYLOGE |
Felipe Leme | 60869c9 | 2016-02-09 16:07:20 -0800 | [diff] [blame] | 29 | #define MYLOGE(...) fprintf(stderr, __VA_ARGS__); ALOGE(__VA_ARGS__); |
Felipe Leme | cbce55d | 2016-02-08 09:53:18 -0800 | [diff] [blame] | 30 | #endif |
Felipe Leme | 93d705b | 2015-11-10 20:10:25 -0800 | [diff] [blame] | 31 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 32 | #include <time.h> |
| 33 | #include <unistd.h> |
Colin Cross | 0c22e8b | 2012-11-02 15:46:56 -0700 | [diff] [blame] | 34 | #include <stdbool.h> |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 35 | #include <stdio.h> |
Felipe Leme | 30dbfa1 | 2016-09-02 12:43:26 -0700 | [diff] [blame] | 36 | |
| 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 | c6bc8bc | 2016-10-27 15:58:27 -0700 | [diff] [blame] | 40 | #include <android-base/macros.h> |
| 41 | #include <ziparchive/zip_writer.h> |
| 42 | |
Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame^] | 43 | #include "android/os/BnDumpstate.h" |
| 44 | |
Felipe Leme | 35c94f3 | 2016-08-12 10:51:54 -0700 | [diff] [blame] | 45 | // Workaround for const char *args[MAX_ARGS_ARRAY_SIZE] variables until they're converted to |
| 46 | // std::vector<std::string> |
Felipe Leme | 30dbfa1 | 2016-09-02 12:43:26 -0700 | [diff] [blame] | 47 | // TODO: remove once not used |
Felipe Leme | 35c94f3 | 2016-08-12 10:51:54 -0700 | [diff] [blame] | 48 | #define MAX_ARGS_ARRAY_SIZE 1000 |
| 49 | |
Felipe Leme | 30dbfa1 | 2016-09-02 12:43:26 -0700 | [diff] [blame] | 50 | // TODO: remove once moved to HAL |
Felipe Leme | 8620bb4 | 2015-11-10 11:04:45 -0800 | [diff] [blame] | 51 | #ifdef __cplusplus |
| 52 | extern "C" { |
| 53 | #endif |
| 54 | |
Felipe Leme | 30dbfa1 | 2016-09-02 12:43:26 -0700 | [diff] [blame] | 55 | /* |
| 56 | * Defines the Linux user that should be executing a command. |
| 57 | */ |
| 58 | enum RootMode { |
| 59 | /* Explicitly change the `uid` and `gid` to be `shell`.*/ |
| 60 | DROP_ROOT, |
| 61 | /* Don't change the `uid` and `gid`. */ |
| 62 | DONT_DROP_ROOT, |
| 63 | /* Prefix the command with `/PATH/TO/su root`. Won't work non user builds. */ |
| 64 | SU_ROOT |
| 65 | }; |
| 66 | |
| 67 | /* |
| 68 | * Defines what should happen with the `stdout` stream of a command. |
| 69 | */ |
| 70 | enum StdoutMode { |
| 71 | /* Don't change `stdout`. */ |
| 72 | NORMAL_STDOUT, |
| 73 | /* Redirect `stdout` to `stderr`. */ |
| 74 | REDIRECT_TO_STDERR |
| 75 | }; |
| 76 | |
| 77 | /* |
| 78 | * Helper class used to report how long it takes for a section to finish. |
| 79 | * |
| 80 | * Typical usage: |
| 81 | * |
| 82 | * DurationReporter duration_reporter(title); |
| 83 | * |
| 84 | */ |
| 85 | class DurationReporter { |
| 86 | public: |
Felipe Leme | 678727a | 2016-09-21 17:22:11 -0700 | [diff] [blame] | 87 | DurationReporter(const std::string& title); |
| 88 | DurationReporter(const std::string& title, FILE* out); |
Felipe Leme | 30dbfa1 | 2016-09-02 12:43:26 -0700 | [diff] [blame] | 89 | |
| 90 | ~DurationReporter(); |
| 91 | |
Felipe Leme | b0f669d | 2016-09-26 18:26:11 -0700 | [diff] [blame] | 92 | static uint64_t Nanotime(); |
Felipe Leme | 30dbfa1 | 2016-09-02 12:43:26 -0700 | [diff] [blame] | 93 | |
| 94 | private: |
| 95 | // TODO: use std::string for title, once dump_files() and other places that pass a char* are |
| 96 | // refactored as well. |
Felipe Leme | 678727a | 2016-09-21 17:22:11 -0700 | [diff] [blame] | 97 | std::string title_; |
Felipe Leme | b0f669d | 2016-09-26 18:26:11 -0700 | [diff] [blame] | 98 | FILE* out_; |
| 99 | uint64_t started_; |
Felipe Leme | 062926e | 2016-10-27 15:51:12 -0700 | [diff] [blame] | 100 | |
| 101 | DISALLOW_COPY_AND_ASSIGN(DurationReporter); |
Felipe Leme | 30dbfa1 | 2016-09-02 12:43:26 -0700 | [diff] [blame] | 102 | }; |
| 103 | |
| 104 | /* |
| 105 | * Value object used to set command options. |
| 106 | * |
| 107 | * Typically constructed using a builder with chained setters. Examples: |
| 108 | * |
| 109 | * CommandOptions::WithTimeout(20).AsRoot().Build(); |
| 110 | * CommandOptions::WithTimeout(10).Always().RedirectStderr().Build(); |
| 111 | * |
| 112 | * Although the builder could be used to dynamically set values. Example: |
| 113 | * |
| 114 | * CommandOptions::CommandOptionsBuilder options = |
| 115 | * CommandOptions::WithTimeout(10); |
| 116 | * if (!is_user_build()) { |
| 117 | * options.AsRoot(); |
| 118 | * } |
Felipe Leme | b0f669d | 2016-09-26 18:26:11 -0700 | [diff] [blame] | 119 | * RunCommand("command", {"args"}, options.Build()); |
Felipe Leme | 30dbfa1 | 2016-09-02 12:43:26 -0700 | [diff] [blame] | 120 | */ |
| 121 | class CommandOptions { |
| 122 | private: |
| 123 | class CommandOptionsValues { |
| 124 | private: |
| 125 | CommandOptionsValues(long timeout); |
| 126 | |
Felipe Leme | b0f669d | 2016-09-26 18:26:11 -0700 | [diff] [blame] | 127 | long timeout_; |
| 128 | bool always_; |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 129 | RootMode root_mode_; |
| 130 | StdoutMode stdout_mode_; |
| 131 | std::string logging_message_; |
Felipe Leme | 30dbfa1 | 2016-09-02 12:43:26 -0700 | [diff] [blame] | 132 | |
| 133 | friend class CommandOptions; |
| 134 | friend class CommandOptionsBuilder; |
| 135 | }; |
| 136 | |
| 137 | CommandOptions(const CommandOptionsValues& values); |
| 138 | |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 139 | const CommandOptionsValues values; |
Felipe Leme | 30dbfa1 | 2016-09-02 12:43:26 -0700 | [diff] [blame] | 140 | |
| 141 | public: |
| 142 | class CommandOptionsBuilder { |
| 143 | public: |
| 144 | /* Sets the command to always run, even on `dry-run` mode. */ |
| 145 | CommandOptionsBuilder& Always(); |
| 146 | /* Sets the command's RootMode as `SU_ROOT` */ |
| 147 | CommandOptionsBuilder& AsRoot(); |
| 148 | /* Sets the command's RootMode as `DROP_ROOT` */ |
| 149 | CommandOptionsBuilder& DropRoot(); |
| 150 | /* Sets the command's StdoutMode `REDIRECT_TO_STDERR` */ |
| 151 | CommandOptionsBuilder& RedirectStderr(); |
| 152 | /* When not empty, logs a message before executing the command. |
| 153 | * Must contain a `%s`, which will be replaced by the full command line, and end on `\n`. */ |
| 154 | CommandOptionsBuilder& Log(const std::string& message); |
| 155 | /* Builds the command options. */ |
| 156 | CommandOptions Build(); |
| 157 | |
| 158 | private: |
| 159 | CommandOptionsBuilder(long timeout); |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 160 | CommandOptionsValues values; |
Felipe Leme | 30dbfa1 | 2016-09-02 12:43:26 -0700 | [diff] [blame] | 161 | friend class CommandOptions; |
| 162 | }; |
| 163 | |
| 164 | /** Gets the command timeout, in seconds. */ |
| 165 | long Timeout() const; |
| 166 | /* Checks whether the command should always be run, even on dry-run mode. */ |
| 167 | bool Always() const; |
| 168 | /** Gets the RootMode of the command. */ |
| 169 | RootMode RootMode() const; |
| 170 | /** Gets the StdoutMode of the command. */ |
| 171 | StdoutMode StdoutMode() const; |
| 172 | /** Gets the logging message header, it any. */ |
| 173 | std::string LoggingMessage() const; |
| 174 | |
| 175 | /** Creates a builder with the requied timeout. */ |
| 176 | static CommandOptionsBuilder WithTimeout(long timeout); |
| 177 | |
| 178 | // Common options. |
| 179 | static CommandOptions DEFAULT; |
| 180 | static CommandOptions DEFAULT_DUMPSYS; |
| 181 | static CommandOptions AS_ROOT_5; |
| 182 | static CommandOptions AS_ROOT_10; |
| 183 | static CommandOptions AS_ROOT_20; |
| 184 | }; |
| 185 | |
Felipe Leme | e844a9d | 2016-09-21 15:01:39 -0700 | [diff] [blame] | 186 | /* |
| 187 | * Estimated total weight of bugreport generation. |
Felipe Leme | 71bbfc5 | 2015-11-23 14:14:51 -0800 | [diff] [blame] | 188 | * |
| 189 | * Each section contributes to the total weight by an individual weight, so the overall progress |
| 190 | * can be calculated by dividing the all completed weight by the total weight. |
| 191 | * |
| 192 | * This value is defined empirically and it need to be adjusted as more sections are added. |
Felipe Leme | ad5f6c4 | 2015-11-30 14:26:46 -0800 | [diff] [blame] | 193 | * |
| 194 | * It does not need to match the exact sum of all sections, but ideally it should to be slight more |
| 195 | * than such sum: a value too high will cause the bugreport to finish before the user expected (for |
Felipe Leme | faf67e3 | 2016-03-28 11:15:22 -0700 | [diff] [blame] | 196 | * example, jumping from 70% to 100%), while a value too low will cause the progress to get stuck |
| 197 | * at an almost-finished value (like 99%) for a while. |
Felipe Leme | 71bbfc5 | 2015-11-23 14:14:51 -0800 | [diff] [blame] | 198 | */ |
Felipe Leme | e844a9d | 2016-09-21 15:01:39 -0700 | [diff] [blame] | 199 | // TODO: move to dumpstate.cpp / utils.cpp once it's used in just one file |
Felipe Leme | faf67e3 | 2016-03-28 11:15:22 -0700 | [diff] [blame] | 200 | static const int WEIGHT_TOTAL = 6500; |
Felipe Leme | 71bbfc5 | 2015-11-23 14:14:51 -0800 | [diff] [blame] | 201 | |
Felipe Leme | 71bbfc5 | 2015-11-23 14:14:51 -0800 | [diff] [blame] | 202 | /* |
Felipe Leme | bbaf3c1 | 2016-10-11 14:32:25 -0700 | [diff] [blame] | 203 | * List of supported zip format versions. |
| 204 | * |
| 205 | * See bugreport-format.md for more info. |
| 206 | */ |
Felipe Leme | d071c68 | 2016-10-20 16:48:00 -0700 | [diff] [blame] | 207 | static std::string VERSION_CURRENT = "1.0"; |
| 208 | |
| 209 | /* |
Felipe Leme | e184f66 | 2016-10-27 10:04:47 -0700 | [diff] [blame] | 210 | * Temporary version that adds a anr-traces.txt entry. Once tools support it, the current version |
| 211 | * will be bumped to 2.0-dev-1. |
| 212 | */ |
| 213 | static std::string VERSION_SPLIT_ANR = "2.0-dev-1"; |
| 214 | |
| 215 | /* |
Felipe Leme | d071c68 | 2016-10-20 16:48:00 -0700 | [diff] [blame] | 216 | * "Alias" for the current version. |
| 217 | */ |
| 218 | static std::string VERSION_DEFAULT = "default"; |
Felipe Leme | bbaf3c1 | 2016-10-11 14:32:25 -0700 | [diff] [blame] | 219 | |
| 220 | /* |
Felipe Leme | e844a9d | 2016-09-21 15:01:39 -0700 | [diff] [blame] | 221 | * Main class driving a bugreport generation. |
| 222 | * |
| 223 | * Currently, it only contains variables that are accessed externally, but gradually the functions |
| 224 | * that are spread accross utils.cpp and dumpstate.cpp will be moved to it. |
| 225 | */ |
| 226 | class Dumpstate { |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 227 | friend class DumpstateTest; |
| 228 | |
Felipe Leme | e844a9d | 2016-09-21 15:01:39 -0700 | [diff] [blame] | 229 | public: |
| 230 | static Dumpstate& GetInstance(); |
Felipe Leme | 71bbfc5 | 2015-11-23 14:14:51 -0800 | [diff] [blame] | 231 | |
Felipe Leme | e844a9d | 2016-09-21 15:01:39 -0700 | [diff] [blame] | 232 | /* |
| 233 | * When running in dry-run mode, skips the real dumps and just print the section headers. |
| 234 | * |
| 235 | * Useful when debugging dumpstate or other bugreport-related activities. |
| 236 | * |
| 237 | * Dry-run mode is enabled by setting the system property dumpstate.dry_run to true. |
| 238 | */ |
Felipe Leme | 2b9b06c | 2016-10-14 09:13:06 -0700 | [diff] [blame] | 239 | bool IsDryRun() const; |
Felipe Leme | 71ca15e | 2016-05-19 16:18:17 -0700 | [diff] [blame] | 240 | |
Felipe Leme | 678727a | 2016-09-21 17:22:11 -0700 | [diff] [blame] | 241 | /* |
Felipe Leme | 6ad9c06 | 2016-09-28 11:58:36 -0700 | [diff] [blame] | 242 | * Gets whether device is running a `user` build. |
| 243 | */ |
Felipe Leme | 2b9b06c | 2016-10-14 09:13:06 -0700 | [diff] [blame] | 244 | bool IsUserBuild() const; |
Felipe Leme | 6ad9c06 | 2016-09-28 11:58:36 -0700 | [diff] [blame] | 245 | |
Felipe Leme | 1d486fe | 2016-10-14 18:06:47 -0700 | [diff] [blame] | 246 | /* Checkes whether dumpstate is generating a zipped bugreport. */ |
| 247 | bool IsZipping() const; |
| 248 | |
Felipe Leme | 6ad9c06 | 2016-09-28 11:58:36 -0700 | [diff] [blame] | 249 | /* |
Felipe Leme | 678727a | 2016-09-21 17:22:11 -0700 | [diff] [blame] | 250 | * Forks a command, waits for it to finish, and returns its status. |
| 251 | * |
| 252 | * |title| description of the command printed on `stdout` (or empty to skip |
| 253 | * description). |
| 254 | * |full_command| array containing the command (first entry) and its arguments. |
| 255 | * Must contain at least one element. |
| 256 | * |options| optional argument defining the command's behavior. |
| 257 | */ |
| 258 | int RunCommand(const std::string& title, const std::vector<std::string>& fullCommand, |
| 259 | const CommandOptions& options = CommandOptions::DEFAULT); |
| 260 | |
| 261 | /* |
| 262 | * Runs `dumpsys` with the given arguments, automatically setting its timeout |
| 263 | * (`-t` argument) |
| 264 | * according to the command options. |
| 265 | * |
| 266 | * |title| description of the command printed on `stdout` (or empty to skip |
| 267 | * description). |
| 268 | * |dumpsys_args| `dumpsys` arguments (except `-t`). |
| 269 | * |options| optional argument defining the command's behavior. |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 270 | * |dumpsys_timeout| when > 0, defines the value passed to `dumpsys -t` (otherwise it uses the |
Felipe Leme | 6ad9c06 | 2016-09-28 11:58:36 -0700 | [diff] [blame] | 271 | * timeout from `options`) |
Felipe Leme | 678727a | 2016-09-21 17:22:11 -0700 | [diff] [blame] | 272 | */ |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 273 | void RunDumpsys(const std::string& title, const std::vector<std::string>& dumpsys_args, |
Felipe Leme | 678727a | 2016-09-21 17:22:11 -0700 | [diff] [blame] | 274 | const CommandOptions& options = CommandOptions::DEFAULT_DUMPSYS, |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 275 | long dumpsys_timeout = 0); |
Felipe Leme | 678727a | 2016-09-21 17:22:11 -0700 | [diff] [blame] | 276 | |
| 277 | /* |
| 278 | * Prints the contents of a file. |
| 279 | * |
| 280 | * |title| description of the command printed on `stdout` (or empty to skip |
| 281 | * description). |
| 282 | * |path| location of the file to be dumped. |
| 283 | */ |
| 284 | int DumpFile(const std::string& title, const std::string& path); |
| 285 | |
Felipe Leme | bbaf3c1 | 2016-10-11 14:32:25 -0700 | [diff] [blame] | 286 | /* |
Felipe Leme | 1d486fe | 2016-10-14 18:06:47 -0700 | [diff] [blame] | 287 | * Adds a new entry to the existing zip file. |
| 288 | * */ |
| 289 | bool AddZipEntry(const std::string& entry_name, const std::string& entry_path); |
| 290 | |
| 291 | /* |
| 292 | * Adds a new entry to the existing zip file. |
| 293 | */ |
| 294 | bool AddZipEntryFromFd(const std::string& entry_name, int fd); |
| 295 | |
| 296 | /* |
| 297 | * Adds a text entry entry to the existing zip file. |
| 298 | */ |
| 299 | bool AddTextZipEntry(const std::string& entry_name, const std::string& content); |
| 300 | |
| 301 | /* |
| 302 | * Adds all files from a directory to the zipped bugreport file. |
| 303 | */ |
| 304 | void AddDir(const std::string& dir, bool recursive); |
| 305 | |
| 306 | /* |
Felipe Leme | bbaf3c1 | 2016-10-11 14:32:25 -0700 | [diff] [blame] | 307 | * Takes a screenshot and save it to the given `path`. |
| 308 | * |
| 309 | * If `path` is empty, uses a standard path based on the bugreport name. |
| 310 | */ |
| 311 | void TakeScreenshot(const std::string& path = ""); |
| 312 | |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 313 | // TODO: members below should be private once refactor is finished |
| 314 | |
| 315 | /* |
| 316 | * Updates the overall progress of the bugreport generation by the given weight increment. |
| 317 | */ |
| 318 | void UpdateProgress(int delta); |
| 319 | |
Felipe Leme | bbaf3c1 | 2016-10-11 14:32:25 -0700 | [diff] [blame] | 320 | /* Prints the dumpstate header on `stdout`. */ |
Felipe Leme | 2b9b06c | 2016-10-14 09:13:06 -0700 | [diff] [blame] | 321 | void PrintHeader() const; |
Felipe Leme | bbaf3c1 | 2016-10-11 14:32:25 -0700 | [diff] [blame] | 322 | |
Felipe Leme | 1d486fe | 2016-10-14 18:06:47 -0700 | [diff] [blame] | 323 | /* |
| 324 | * Adds the temporary report to the existing .zip file, closes the .zip file, and removes the |
| 325 | * temporary file. |
| 326 | */ |
| 327 | bool FinishZipFile(); |
| 328 | |
Felipe Leme | bbaf3c1 | 2016-10-11 14:32:25 -0700 | [diff] [blame] | 329 | /* Gets the path of a bugreport file with the given suffix. */ |
Felipe Leme | 2b9b06c | 2016-10-14 09:13:06 -0700 | [diff] [blame] | 330 | std::string GetPath(const std::string& suffix) const; |
Felipe Leme | bbaf3c1 | 2016-10-11 14:32:25 -0700 | [diff] [blame] | 331 | |
Felipe Leme | e844a9d | 2016-09-21 15:01:39 -0700 | [diff] [blame] | 332 | // TODO: initialize fields on constructor |
| 333 | |
| 334 | // dumpstate id - unique after each device reboot. |
| 335 | unsigned long id_; |
| 336 | |
Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame^] | 337 | // dumpstate pid |
| 338 | pid_t pid_; |
| 339 | |
Felipe Leme | e844a9d | 2016-09-21 15:01:39 -0700 | [diff] [blame] | 340 | // Whether progress updates should be published. |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 341 | bool update_progress_ = false; |
Felipe Leme | e844a9d | 2016-09-21 15:01:39 -0700 | [diff] [blame] | 342 | |
Felipe Leme | bbaf3c1 | 2016-10-11 14:32:25 -0700 | [diff] [blame] | 343 | // Whether it should take an screenshot earlier in the process. |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 344 | bool do_early_screenshot_ = false; |
Felipe Leme | bbaf3c1 | 2016-10-11 14:32:25 -0700 | [diff] [blame] | 345 | |
Felipe Leme | e844a9d | 2016-09-21 15:01:39 -0700 | [diff] [blame] | 346 | // Currrent progress. |
| 347 | int progress_ = 0; |
| 348 | |
| 349 | // Total estimated progress. |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 350 | int weight_total_ = WEIGHT_TOTAL; |
Felipe Leme | e844a9d | 2016-09-21 15:01:39 -0700 | [diff] [blame] | 351 | |
| 352 | // When set, defines a socket file-descriptor use to report progress to bugreportz. |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 353 | int control_socket_fd_ = -1; |
Felipe Leme | e844a9d | 2016-09-21 15:01:39 -0700 | [diff] [blame] | 354 | |
Felipe Leme | bbaf3c1 | 2016-10-11 14:32:25 -0700 | [diff] [blame] | 355 | // Bugreport format version; |
Felipe Leme | d071c68 | 2016-10-20 16:48:00 -0700 | [diff] [blame] | 356 | std::string version_ = VERSION_CURRENT; |
Felipe Leme | e844a9d | 2016-09-21 15:01:39 -0700 | [diff] [blame] | 357 | |
Felipe Leme | bbaf3c1 | 2016-10-11 14:32:25 -0700 | [diff] [blame] | 358 | // Command-line arguments as string |
| 359 | std::string args_; |
| 360 | |
| 361 | // Extra options passed as system property. |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 362 | std::string extra_options_; |
Felipe Leme | bbaf3c1 | 2016-10-11 14:32:25 -0700 | [diff] [blame] | 363 | |
| 364 | // Full path of the directory where the bugreport files will be written. |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 365 | std::string bugreport_dir_; |
Felipe Leme | e844a9d | 2016-09-21 15:01:39 -0700 | [diff] [blame] | 366 | |
Felipe Leme | bbaf3c1 | 2016-10-11 14:32:25 -0700 | [diff] [blame] | 367 | // Full path of the temporary file containing the screenshot (when requested). |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 368 | std::string screenshot_path_; |
Felipe Leme | bbaf3c1 | 2016-10-11 14:32:25 -0700 | [diff] [blame] | 369 | |
| 370 | time_t now_; |
| 371 | |
Felipe Leme | 2b9b06c | 2016-10-14 09:13:06 -0700 | [diff] [blame] | 372 | // Base name (without suffix or extensions) of the bugreport files, typically |
| 373 | // `bugreport-BUILD_ID`. |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 374 | std::string base_name_; |
Felipe Leme | bbaf3c1 | 2016-10-11 14:32:25 -0700 | [diff] [blame] | 375 | |
Felipe Leme | 2b9b06c | 2016-10-14 09:13:06 -0700 | [diff] [blame] | 376 | // Name is the suffix part of the bugreport files - it's typically the date (when invoked with |
| 377 | // `-d`), but it could be changed by the user.. |
| 378 | std::string name_; |
| 379 | |
Felipe Leme | 1d486fe | 2016-10-14 18:06:47 -0700 | [diff] [blame] | 380 | // Full path of the temporary file containing the bugreport. |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 381 | std::string tmp_path_; |
Felipe Leme | 1d486fe | 2016-10-14 18:06:47 -0700 | [diff] [blame] | 382 | |
| 383 | // Full path of the file containing the dumpstate logs. |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 384 | std::string log_path_; |
Felipe Leme | 1d486fe | 2016-10-14 18:06:47 -0700 | [diff] [blame] | 385 | |
| 386 | // Pointer to the actual path, be it zip or text. |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 387 | std::string path_; |
Felipe Leme | 1d486fe | 2016-10-14 18:06:47 -0700 | [diff] [blame] | 388 | |
| 389 | // Pointer to the zipped file. |
| 390 | std::unique_ptr<FILE, int (*)(FILE*)> zip_file{nullptr, fclose}; |
| 391 | |
Felipe Leme | c6bc8bc | 2016-10-27 15:58:27 -0700 | [diff] [blame] | 392 | // Pointer to the zip structure. |
| 393 | std::unique_ptr<ZipWriter> zip_writer_; |
| 394 | |
Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame^] | 395 | // Binder object listing to progress. |
| 396 | android::sp<android::os::IDumpstateListener> listener_; |
| 397 | std::string listener_name_; |
| 398 | |
Felipe Leme | e844a9d | 2016-09-21 15:01:39 -0700 | [diff] [blame] | 399 | private: |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 400 | // Used by GetInstance() only. |
Felipe Leme | d071c68 | 2016-10-20 16:48:00 -0700 | [diff] [blame] | 401 | Dumpstate(const std::string& version = VERSION_CURRENT, bool dry_run = false, |
| 402 | const std::string& build_type = "user"); |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 403 | |
Felipe Leme | 2b9b06c | 2016-10-14 09:13:06 -0700 | [diff] [blame] | 404 | // Internal version of RunCommand that just runs it, without updating progress. |
| 405 | int JustRunCommand(const char* command, const char* path, std::vector<const char*>& args, |
| 406 | const CommandOptions& options) const; |
| 407 | |
| 408 | // Internal version of RunCommand that just dumps it, without updating progress. |
| 409 | int JustDumpFile(const std::string& title, const std::string& path) const; |
| 410 | |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 411 | // Whether this is a dry run. |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 412 | bool dry_run_; |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 413 | |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 414 | // Build type (such as 'user' or 'eng'). |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 415 | std::string build_type_; |
Felipe Leme | 062926e | 2016-10-27 15:51:12 -0700 | [diff] [blame] | 416 | |
| 417 | DISALLOW_COPY_AND_ASSIGN(Dumpstate); |
Felipe Leme | e844a9d | 2016-09-21 15:01:39 -0700 | [diff] [blame] | 418 | }; |
| 419 | |
| 420 | // for_each_pid_func = void (*)(int, const char*); |
| 421 | // for_each_tid_func = void (*)(int, int, const char*); |
| 422 | |
| 423 | typedef void(for_each_pid_func)(int, const char*); |
| 424 | typedef void(for_each_tid_func)(int, int, const char*); |
Felipe Leme | 71ca15e | 2016-05-19 16:18:17 -0700 | [diff] [blame] | 425 | |
Felipe Leme | 71a74ac | 2016-03-17 15:43:25 -0700 | [diff] [blame] | 426 | /* saves the the contents of a file as a long */ |
| 427 | int read_file_as_long(const char *path, long int *output); |
| 428 | |
Christopher Ferris | 54bcc5f | 2015-02-10 12:15:01 -0800 | [diff] [blame] | 429 | /* prints the contents of the fd |
| 430 | * fd must have been opened with the flag O_NONBLOCK. |
| 431 | */ |
Christopher Ferris | 1fe6107 | 2014-07-22 16:08:19 -0700 | [diff] [blame] | 432 | int dump_file_from_fd(const char *title, const char *path, int fd); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 433 | |
Mark Salyzyn | 326842f | 2015-04-30 09:49:41 -0700 | [diff] [blame] | 434 | /* calls skip to gate calling dump_from_fd recursively |
| 435 | * in the specified directory. dump_from_fd defaults to |
| 436 | * dump_file_from_fd above when set to NULL. skip defaults |
| 437 | * to false when set to NULL. dump_from_fd will always be |
| 438 | * called with title NULL. |
| 439 | */ |
Felipe Leme | 678727a | 2016-09-21 17:22:11 -0700 | [diff] [blame] | 440 | int dump_files(const std::string& title, const char* dir, bool (*skip)(const char* path), |
| 441 | int (*dump_from_fd)(const char* title, const char* path, int fd)); |
Mark Salyzyn | 326842f | 2015-04-30 09:49:41 -0700 | [diff] [blame] | 442 | |
Felipe Leme | cf6a8b4 | 2016-03-11 10:38:19 -0800 | [diff] [blame] | 443 | /* switch to non-root user and group */ |
| 444 | bool drop_root_user(); |
Felipe Leme | 93d705b | 2015-11-10 20:10:25 -0800 | [diff] [blame] | 445 | |
Felipe Leme | 36b3f6f | 2015-11-19 15:41:04 -0800 | [diff] [blame] | 446 | /* sends a broadcast using Activity Manager */ |
| 447 | void send_broadcast(const std::string& action, const std::vector<std::string>& args); |
| 448 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 449 | /* prints all the system properties */ |
| 450 | void print_properties(); |
| 451 | |
Felipe Leme | 2628e9e | 2016-04-12 16:36:51 -0700 | [diff] [blame] | 452 | /** opens a socket and returns its file descriptor */ |
| 453 | int open_socket(const char *service); |
| 454 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 455 | /* redirect output to a service control socket */ |
| 456 | void redirect_to_socket(FILE *redirect, const char *service); |
| 457 | |
Felipe Leme | 0f3fb20 | 2016-06-10 17:10:53 -0700 | [diff] [blame] | 458 | /* redirect output to a new file */ |
Christopher Ferris | ff4a4dc | 2015-02-09 16:24:47 -0800 | [diff] [blame] | 459 | void redirect_to_file(FILE *redirect, char *path); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 460 | |
Felipe Leme | 0f3fb20 | 2016-06-10 17:10:53 -0700 | [diff] [blame] | 461 | /* redirect output to an existing file */ |
| 462 | void redirect_to_existing_file(FILE *redirect, char *path); |
| 463 | |
Felipe Leme | 111b9d0 | 2016-02-03 09:28:24 -0800 | [diff] [blame] | 464 | /* create leading directories, if necessary */ |
| 465 | void create_parent_dirs(const char *path); |
| 466 | |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 467 | /* dump Dalvik and native stack traces, return the trace file location (NULL if none) */ |
| 468 | const char *dump_traces(); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 469 | |
| 470 | /* for each process in the system, run the specified function */ |
Colin Cross | 0c22e8b | 2012-11-02 15:46:56 -0700 | [diff] [blame] | 471 | void for_each_pid(for_each_pid_func func, const char *header); |
| 472 | |
| 473 | /* for each thread in the system, run the specified function */ |
| 474 | void for_each_tid(for_each_tid_func func, const char *header); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 475 | |
| 476 | /* Displays a blocked processes in-kernel wait channel */ |
Colin Cross | 0c22e8b | 2012-11-02 15:46:56 -0700 | [diff] [blame] | 477 | void show_wchan(int pid, int tid, const char *name); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 478 | |
Mark Salyzyn | a297c32 | 2016-02-05 15:33:17 -0800 | [diff] [blame] | 479 | /* Displays a processes times */ |
| 480 | void show_showtime(int pid, const char *name); |
| 481 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 482 | /* Runs "showmap" for a process */ |
| 483 | void do_showmap(int pid, const char *name); |
| 484 | |
| 485 | /* Gets the dmesg output for the kernel */ |
| 486 | void do_dmesg(); |
| 487 | |
Sreeram Ramachandran | 2b3bba3 | 2014-07-08 15:40:55 -0700 | [diff] [blame] | 488 | /* Prints the contents of all the routing tables, both IPv4 and IPv6. */ |
| 489 | void dump_route_tables(); |
| 490 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 491 | /* Play a sound via Stagefright */ |
Christopher Ferris | 1fe6107 | 2014-07-22 16:08:19 -0700 | [diff] [blame] | 492 | void play_sound(const char *path); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 493 | |
| 494 | /* Implemented by libdumpstate_board to dump board-specific info */ |
| 495 | void dumpstate_board(); |
| 496 | |
Felipe Leme | 0c80cf0 | 2016-01-05 13:25:34 -0800 | [diff] [blame] | 497 | /* Vibrates for a given durating (in milliseconds). */ |
| 498 | void vibrate(FILE* vibrator, int ms); |
| 499 | |
| 500 | /* Checks if a given path is a directory. */ |
| 501 | bool is_dir(const char* pathname); |
| 502 | |
| 503 | /** Gets the last modification time of a file, or default time if file is not found. */ |
| 504 | time_t get_mtime(int fd, time_t default_mtime); |
| 505 | |
Calvin On | 249beee | 2016-06-03 15:17:07 -0700 | [diff] [blame] | 506 | /* Dumps eMMC Extended CSD data. */ |
Felipe Leme | 78f2c86 | 2015-12-21 09:55:22 -0800 | [diff] [blame] | 507 | void dump_emmc_ecsd(const char *ext_csd_path); |
| 508 | |
Calvin On | 249beee | 2016-06-03 15:17:07 -0700 | [diff] [blame] | 509 | /** Gets command-line arguments. */ |
Felipe Leme | a34efb7 | 2016-03-11 09:33:32 -0800 | [diff] [blame] | 510 | void format_args(int argc, const char *argv[], std::string *args); |
Felipe Leme | 88c7933 | 2016-02-22 11:06:49 -0800 | [diff] [blame] | 511 | |
Calvin On | 249beee | 2016-06-03 15:17:07 -0700 | [diff] [blame] | 512 | /** Tells if the device is running a user build. */ |
| 513 | bool is_user_build(); |
| 514 | |
Felipe Leme | 8620bb4 | 2015-11-10 11:04:45 -0800 | [diff] [blame] | 515 | #ifdef __cplusplus |
| 516 | } |
| 517 | #endif |
| 518 | |
Felipe Leme | 8268ed2 | 2016-08-02 18:18:25 -0700 | [diff] [blame] | 519 | #endif /* FRAMEWORK_NATIVE_CMD_DUMPSTATE_H_ */ |