| 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 |  | 
|  | 20 | #include <time.h> | 
|  | 21 | #include <unistd.h> | 
| Colin Cross | 0c22e8b | 2012-11-02 15:46:56 -0700 | [diff] [blame] | 22 | #include <stdbool.h> | 
| Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 23 | #include <stdio.h> | 
| Felipe Leme | 30dbfa1 | 2016-09-02 12:43:26 -0700 | [diff] [blame] | 24 |  | 
|  | 25 | #include <string> | 
| Felipe Leme | 36b3f6f | 2015-11-19 15:41:04 -0800 | [diff] [blame] | 26 | #include <vector> | 
| Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 27 |  | 
| Felipe Leme | c6bc8bc | 2016-10-27 15:58:27 -0700 | [diff] [blame] | 28 | #include <android-base/macros.h> | 
| Luis Hector Chavez | 91c2ae5 | 2018-03-14 15:12:46 -0700 | [diff] [blame] | 29 | #include <android-base/unique_fd.h> | 
| Nandana Dutt | 197661d | 2018-11-16 16:40:21 +0000 | [diff] [blame] | 30 | #include <android/os/IDumpstate.h> | 
| Jiyong Park | b22e65d | 2017-06-23 21:23:16 +0900 | [diff] [blame] | 31 | #include <android/os/IDumpstateListener.h> | 
|  | 32 | #include <utils/StrongPointer.h> | 
| Felipe Leme | c6bc8bc | 2016-10-27 15:58:27 -0700 | [diff] [blame] | 33 | #include <ziparchive/zip_writer.h> | 
|  | 34 |  | 
| Felipe Leme | bda15a0 | 2016-11-16 17:48:25 -0800 | [diff] [blame] | 35 | #include "DumpstateUtil.h" | 
| Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 36 |  | 
| Felipe Leme | 35c94f3 | 2016-08-12 10:51:54 -0700 | [diff] [blame] | 37 | // Workaround for const char *args[MAX_ARGS_ARRAY_SIZE] variables until they're converted to | 
|  | 38 | // std::vector<std::string> | 
| Felipe Leme | 30dbfa1 | 2016-09-02 12:43:26 -0700 | [diff] [blame] | 39 | // TODO: remove once not used | 
| Felipe Leme | 35c94f3 | 2016-08-12 10:51:54 -0700 | [diff] [blame] | 40 | #define MAX_ARGS_ARRAY_SIZE 1000 | 
|  | 41 |  | 
| Felipe Leme | 47e9be2 | 2016-12-21 15:37:07 -0800 | [diff] [blame] | 42 | // TODO: move everything under this namespace | 
|  | 43 | // TODO: and then remove explicitly android::os::dumpstate:: prefixes | 
|  | 44 | namespace android { | 
|  | 45 | namespace os { | 
| Nandana Dutt | 58d72e2 | 2018-11-16 10:30:48 +0000 | [diff] [blame] | 46 |  | 
|  | 47 | struct DumpstateOptions; | 
|  | 48 |  | 
| Felipe Leme | 47e9be2 | 2016-12-21 15:37:07 -0800 | [diff] [blame] | 49 | namespace dumpstate { | 
|  | 50 |  | 
|  | 51 | class DumpstateTest; | 
|  | 52 | class ProgressTest; | 
|  | 53 |  | 
|  | 54 | }  // namespace dumpstate | 
|  | 55 | }  // namespace os | 
|  | 56 | }  // namespace android | 
|  | 57 |  | 
| Jiyong Park | b22e65d | 2017-06-23 21:23:16 +0900 | [diff] [blame] | 58 | class ZipWriter; | 
|  | 59 |  | 
| Felipe Leme | 30dbfa1 | 2016-09-02 12:43:26 -0700 | [diff] [blame] | 60 | // TODO: remove once moved to HAL | 
| Felipe Leme | 8620bb4 | 2015-11-10 11:04:45 -0800 | [diff] [blame] | 61 | #ifdef __cplusplus | 
|  | 62 | extern "C" { | 
|  | 63 | #endif | 
|  | 64 |  | 
| Felipe Leme | 30dbfa1 | 2016-09-02 12:43:26 -0700 | [diff] [blame] | 65 | /* | 
| Felipe Leme | 30dbfa1 | 2016-09-02 12:43:26 -0700 | [diff] [blame] | 66 | * Helper class used to report how long it takes for a section to finish. | 
|  | 67 | * | 
|  | 68 | * Typical usage: | 
|  | 69 | * | 
|  | 70 | *    DurationReporter duration_reporter(title); | 
|  | 71 | * | 
|  | 72 | */ | 
|  | 73 | class DurationReporter { | 
|  | 74 | public: | 
| Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 75 | DurationReporter(const std::string& title, bool log_only = false); | 
| Felipe Leme | 30dbfa1 | 2016-09-02 12:43:26 -0700 | [diff] [blame] | 76 |  | 
|  | 77 | ~DurationReporter(); | 
|  | 78 |  | 
| Felipe Leme | 30dbfa1 | 2016-09-02 12:43:26 -0700 | [diff] [blame] | 79 | private: | 
| Felipe Leme | 678727a | 2016-09-21 17:22:11 -0700 | [diff] [blame] | 80 | std::string title_; | 
| Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 81 | bool log_only_; | 
| Felipe Leme | b0f669d | 2016-09-26 18:26:11 -0700 | [diff] [blame] | 82 | uint64_t started_; | 
| Felipe Leme | 062926e | 2016-10-27 15:51:12 -0700 | [diff] [blame] | 83 |  | 
|  | 84 | DISALLOW_COPY_AND_ASSIGN(DurationReporter); | 
| Felipe Leme | 30dbfa1 | 2016-09-02 12:43:26 -0700 | [diff] [blame] | 85 | }; | 
|  | 86 |  | 
|  | 87 | /* | 
| Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 88 | * Keeps track of current progress and estimated max, saving stats on file to tune up future runs. | 
| Felipe Leme | 71bbfc5 | 2015-11-23 14:14:51 -0800 | [diff] [blame] | 89 | * | 
| Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 90 | * Each `dumpstate` section contributes to the total weight by an individual weight, so the overall | 
|  | 91 | * progress can be calculated by dividing the estimate max progress by the current progress. | 
| Felipe Leme | 71bbfc5 | 2015-11-23 14:14:51 -0800 | [diff] [blame] | 92 | * | 
| Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 93 | * The estimated max progress is initially set to a value (`kDefaultMax) defined empirically, but | 
|  | 94 | * it's adjusted after each dumpstate run by storing the average duration in a file. | 
| Felipe Leme | ad5f6c4 | 2015-11-30 14:26:46 -0800 | [diff] [blame] | 95 | * | 
| Felipe Leme | 71bbfc5 | 2015-11-23 14:14:51 -0800 | [diff] [blame] | 96 | */ | 
| Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 97 | class Progress { | 
| Felipe Leme | 47e9be2 | 2016-12-21 15:37:07 -0800 | [diff] [blame] | 98 | friend class android::os::dumpstate::ProgressTest; | 
|  | 99 | friend class android::os::dumpstate::DumpstateTest; | 
| Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 100 |  | 
|  | 101 | public: | 
|  | 102 | /* | 
|  | 103 | * Default estimation of the max duration of a bugreport generation. | 
|  | 104 | * | 
|  | 105 | * It does not need to match the exact sum of all sections, but ideally it should to be slight | 
|  | 106 | * more than such sum: a value too high will cause the bugreport to finish before the user | 
|  | 107 | * expected (for example, jumping from 70% to 100%), while a value too low will cause the | 
|  | 108 | * progress to get stuck at an almost-finished value (like 99%) for a while. | 
|  | 109 | * | 
|  | 110 | * This constant is only used when the average duration from previous runs cannot be used. | 
|  | 111 | */ | 
|  | 112 | static const int kDefaultMax; | 
|  | 113 |  | 
|  | 114 | Progress(const std::string& path = ""); | 
|  | 115 |  | 
|  | 116 | // Gets the current progress. | 
|  | 117 | int32_t Get() const; | 
|  | 118 |  | 
|  | 119 | // Gets the current estimated max progress. | 
|  | 120 | int32_t GetMax() const; | 
|  | 121 |  | 
|  | 122 | // Gets the initial estimated max progress. | 
|  | 123 | int32_t GetInitialMax() const; | 
|  | 124 |  | 
|  | 125 | // Increments progress (ignored if not positive). | 
|  | 126 | // Returns `true` if the max progress increased as well. | 
|  | 127 | bool Inc(int32_t delta); | 
|  | 128 |  | 
|  | 129 | // Persist the stats. | 
|  | 130 | void Save(); | 
|  | 131 |  | 
|  | 132 | void Dump(int fd, const std::string& prefix) const; | 
|  | 133 |  | 
|  | 134 | private: | 
|  | 135 | Progress(int32_t initial_max, float growth_factor, | 
|  | 136 | const std::string& path = "");                                // Used by test cases. | 
|  | 137 | Progress(int32_t initial_max, int32_t progress, float growth_factor);  // Used by test cases. | 
|  | 138 | void Load(); | 
|  | 139 | int32_t initial_max_; | 
|  | 140 | int32_t progress_; | 
|  | 141 | int32_t max_; | 
|  | 142 | float growth_factor_; | 
|  | 143 | int32_t n_runs_; | 
|  | 144 | int32_t average_max_; | 
| Nandana Dutt | 979388e | 2018-11-30 16:48:55 +0000 | [diff] [blame] | 145 | std::string path_; | 
| Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 146 | }; | 
| Felipe Leme | 71bbfc5 | 2015-11-23 14:14:51 -0800 | [diff] [blame] | 147 |  | 
| Felipe Leme | 71bbfc5 | 2015-11-23 14:14:51 -0800 | [diff] [blame] | 148 | /* | 
| Felipe Leme | bbaf3c1 | 2016-10-11 14:32:25 -0700 | [diff] [blame] | 149 | * List of supported zip format versions. | 
|  | 150 | * | 
|  | 151 | * See bugreport-format.md for more info. | 
|  | 152 | */ | 
| Vishnu Nair | 64afc02 | 2018-02-01 15:29:34 -0800 | [diff] [blame] | 153 | static std::string VERSION_CURRENT = "2.0"; | 
| Felipe Leme | d071c68 | 2016-10-20 16:48:00 -0700 | [diff] [blame] | 154 |  | 
|  | 155 | /* | 
| Felipe Leme | e184f66 | 2016-10-27 10:04:47 -0700 | [diff] [blame] | 156 | * Temporary version that adds a anr-traces.txt entry. Once tools support it, the current version | 
| Vishnu Nair | 64afc02 | 2018-02-01 15:29:34 -0800 | [diff] [blame] | 157 | * will be bumped to 3.0. | 
| Felipe Leme | e184f66 | 2016-10-27 10:04:47 -0700 | [diff] [blame] | 158 | */ | 
| Vishnu Nair | 64afc02 | 2018-02-01 15:29:34 -0800 | [diff] [blame] | 159 | static std::string VERSION_SPLIT_ANR = "3.0-dev-split-anr"; | 
| Felipe Leme | e184f66 | 2016-10-27 10:04:47 -0700 | [diff] [blame] | 160 |  | 
|  | 161 | /* | 
| Felipe Leme | d071c68 | 2016-10-20 16:48:00 -0700 | [diff] [blame] | 162 | * "Alias" for the current version. | 
|  | 163 | */ | 
|  | 164 | static std::string VERSION_DEFAULT = "default"; | 
| Felipe Leme | bbaf3c1 | 2016-10-11 14:32:25 -0700 | [diff] [blame] | 165 |  | 
|  | 166 | /* | 
| Nandana Dutt | 979388e | 2018-11-30 16:48:55 +0000 | [diff] [blame] | 167 | * Directory used by Dumpstate binary to keep its local files. | 
|  | 168 | */ | 
|  | 169 | static const std::string DUMPSTATE_DIRECTORY = "/bugreports"; | 
|  | 170 |  | 
|  | 171 | /* | 
| Luis Hector Chavez | 91c2ae5 | 2018-03-14 15:12:46 -0700 | [diff] [blame] | 172 | * Structure that contains the information of an open dump file. | 
|  | 173 | */ | 
|  | 174 | struct DumpData { | 
|  | 175 | // Path of the file. | 
|  | 176 | std::string name; | 
|  | 177 |  | 
|  | 178 | // Open file descriptor for the file. | 
|  | 179 | android::base::unique_fd fd; | 
|  | 180 |  | 
|  | 181 | // Modification time of the file. | 
|  | 182 | time_t mtime; | 
|  | 183 | }; | 
|  | 184 |  | 
|  | 185 | /* | 
| Felipe Leme | e844a9d | 2016-09-21 15:01:39 -0700 | [diff] [blame] | 186 | * Main class driving a bugreport generation. | 
|  | 187 | * | 
|  | 188 | * Currently, it only contains variables that are accessed externally, but gradually the functions | 
|  | 189 | * that are spread accross utils.cpp and dumpstate.cpp will be moved to it. | 
|  | 190 | */ | 
|  | 191 | class Dumpstate { | 
| Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 192 | friend class DumpstateTest; | 
|  | 193 |  | 
| Felipe Leme | e844a9d | 2016-09-21 15:01:39 -0700 | [diff] [blame] | 194 | public: | 
| Nandana Dutt | 5fb117b | 2018-09-27 09:23:36 +0100 | [diff] [blame] | 195 | enum RunStatus { OK, HELP, INVALID_INPUT, ERROR }; | 
|  | 196 |  | 
| Nandana Dutt | 58d72e2 | 2018-11-16 10:30:48 +0000 | [diff] [blame] | 197 | // The mode under which the bugreport should be run. Each mode encapsulates a few options. | 
|  | 198 | enum BugreportMode { | 
| Nandana Dutt | 197661d | 2018-11-16 16:40:21 +0000 | [diff] [blame] | 199 | BUGREPORT_FULL = android::os::IDumpstate::BUGREPORT_MODE_FULL, | 
|  | 200 | BUGREPORT_INTERACTIVE = android::os::IDumpstate::BUGREPORT_MODE_INTERACTIVE, | 
|  | 201 | BUGREPORT_REMOTE = android::os::IDumpstate::BUGREPORT_MODE_REMOTE, | 
|  | 202 | BUGREPORT_WEAR = android::os::IDumpstate::BUGREPORT_MODE_WEAR, | 
|  | 203 | BUGREPORT_TELEPHONY = android::os::IDumpstate::BUGREPORT_MODE_TELEPHONY, | 
| Abhijeet Kaur | 904e0e0 | 2018-12-05 14:03:01 +0000 | [diff] [blame] | 204 | BUGREPORT_WIFI = android::os::IDumpstate::BUGREPORT_MODE_WIFI, | 
|  | 205 | BUGREPORT_DEFAULT = android::os::IDumpstate::BUGREPORT_MODE_DEFAULT | 
| Nandana Dutt | 58d72e2 | 2018-11-16 10:30:48 +0000 | [diff] [blame] | 206 | }; | 
|  | 207 |  | 
| Felipe Leme | 47e9be2 | 2016-12-21 15:37:07 -0800 | [diff] [blame] | 208 | static android::os::dumpstate::CommandOptions DEFAULT_DUMPSYS; | 
| Felipe Leme | bda15a0 | 2016-11-16 17:48:25 -0800 | [diff] [blame] | 209 |  | 
| Felipe Leme | e844a9d | 2016-09-21 15:01:39 -0700 | [diff] [blame] | 210 | static Dumpstate& GetInstance(); | 
| Felipe Leme | 71bbfc5 | 2015-11-23 14:14:51 -0800 | [diff] [blame] | 211 |  | 
| Felipe Leme | 1d486fe | 2016-10-14 18:06:47 -0700 | [diff] [blame] | 212 | /* Checkes whether dumpstate is generating a zipped bugreport. */ | 
|  | 213 | bool IsZipping() const; | 
|  | 214 |  | 
| Felipe Leme | 6ad9c06 | 2016-09-28 11:58:36 -0700 | [diff] [blame] | 215 | /* | 
| Felipe Leme | 678727a | 2016-09-21 17:22:11 -0700 | [diff] [blame] | 216 | * Forks a command, waits for it to finish, and returns its status. | 
|  | 217 | * | 
|  | 218 | * |title| description of the command printed on `stdout` (or empty to skip | 
|  | 219 | * description). | 
|  | 220 | * |full_command| array containing the command (first entry) and its arguments. | 
|  | 221 | * Must contain at least one element. | 
|  | 222 | * |options| optional argument defining the command's behavior. | 
|  | 223 | */ | 
|  | 224 | int RunCommand(const std::string& title, const std::vector<std::string>& fullCommand, | 
| Felipe Leme | 47e9be2 | 2016-12-21 15:37:07 -0800 | [diff] [blame] | 225 | const android::os::dumpstate::CommandOptions& options = | 
|  | 226 | android::os::dumpstate::CommandOptions::DEFAULT); | 
| Felipe Leme | 678727a | 2016-09-21 17:22:11 -0700 | [diff] [blame] | 227 |  | 
|  | 228 | /* | 
|  | 229 | * Runs `dumpsys` with the given arguments, automatically setting its timeout | 
| Vishnu Nair | 6921f80 | 2017-11-22 09:17:23 -0800 | [diff] [blame] | 230 | * (`-T` argument) | 
| Felipe Leme | 678727a | 2016-09-21 17:22:11 -0700 | [diff] [blame] | 231 | * according to the command options. | 
|  | 232 | * | 
|  | 233 | * |title| description of the command printed on `stdout` (or empty to skip | 
|  | 234 | * description). | 
|  | 235 | * |dumpsys_args| `dumpsys` arguments (except `-t`). | 
|  | 236 | * |options| optional argument defining the command's behavior. | 
| Vishnu Nair | 6921f80 | 2017-11-22 09:17:23 -0800 | [diff] [blame] | 237 | * |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] | 238 | * timeout from `options`) | 
| Felipe Leme | 678727a | 2016-09-21 17:22:11 -0700 | [diff] [blame] | 239 | */ | 
| Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 240 | void RunDumpsys(const std::string& title, const std::vector<std::string>& dumpsys_args, | 
| Felipe Leme | 47e9be2 | 2016-12-21 15:37:07 -0800 | [diff] [blame] | 241 | const android::os::dumpstate::CommandOptions& options = DEFAULT_DUMPSYS, | 
| Vishnu Nair | 6921f80 | 2017-11-22 09:17:23 -0800 | [diff] [blame] | 242 | long dumpsys_timeout_ms = 0); | 
| Felipe Leme | 678727a | 2016-09-21 17:22:11 -0700 | [diff] [blame] | 243 |  | 
|  | 244 | /* | 
|  | 245 | * Prints the contents of a file. | 
|  | 246 | * | 
|  | 247 | * |title| description of the command printed on `stdout` (or empty to skip | 
|  | 248 | * description). | 
|  | 249 | * |path| location of the file to be dumped. | 
|  | 250 | */ | 
|  | 251 | int DumpFile(const std::string& title, const std::string& path); | 
|  | 252 |  | 
| Felipe Leme | bbaf3c1 | 2016-10-11 14:32:25 -0700 | [diff] [blame] | 253 | /* | 
| Felipe Leme | 1d486fe | 2016-10-14 18:06:47 -0700 | [diff] [blame] | 254 | * Adds a new entry to the existing zip file. | 
|  | 255 | * */ | 
|  | 256 | bool AddZipEntry(const std::string& entry_name, const std::string& entry_path); | 
|  | 257 |  | 
|  | 258 | /* | 
|  | 259 | * Adds a new entry to the existing zip file. | 
| Vishnu Nair | e97d612 | 2018-01-18 13:58:56 -0800 | [diff] [blame] | 260 | * | 
|  | 261 | * |entry_name| destination path of the new entry. | 
|  | 262 | * |fd| file descriptor to read from. | 
|  | 263 | * |timeout| timeout to terminate the read if not completed. Set | 
|  | 264 | * value of 0s (default) to disable timeout. | 
| Felipe Leme | 1d486fe | 2016-10-14 18:06:47 -0700 | [diff] [blame] | 265 | */ | 
| Vishnu Nair | e97d612 | 2018-01-18 13:58:56 -0800 | [diff] [blame] | 266 | android::status_t AddZipEntryFromFd(const std::string& entry_name, int fd, | 
|  | 267 | std::chrono::milliseconds timeout); | 
| Felipe Leme | 1d486fe | 2016-10-14 18:06:47 -0700 | [diff] [blame] | 268 |  | 
|  | 269 | /* | 
| Nandana Dutt | a7db634 | 2018-11-21 14:53:34 +0000 | [diff] [blame] | 270 | * Adds a text entry to the existing zip file. | 
| Felipe Leme | 1d486fe | 2016-10-14 18:06:47 -0700 | [diff] [blame] | 271 | */ | 
|  | 272 | bool AddTextZipEntry(const std::string& entry_name, const std::string& content); | 
|  | 273 |  | 
|  | 274 | /* | 
|  | 275 | * Adds all files from a directory to the zipped bugreport file. | 
|  | 276 | */ | 
|  | 277 | void AddDir(const std::string& dir, bool recursive); | 
|  | 278 |  | 
|  | 279 | /* | 
| Felipe Leme | bbaf3c1 | 2016-10-11 14:32:25 -0700 | [diff] [blame] | 280 | * Takes a screenshot and save it to the given `path`. | 
|  | 281 | * | 
|  | 282 | * If `path` is empty, uses a standard path based on the bugreport name. | 
|  | 283 | */ | 
|  | 284 | void TakeScreenshot(const std::string& path = ""); | 
|  | 285 |  | 
| Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 286 | ///////////////////////////////////////////////////////////////////// | 
|  | 287 | // TODO: members below should be private once refactor is finished // | 
|  | 288 | ///////////////////////////////////////////////////////////////////// | 
|  | 289 |  | 
|  | 290 | // TODO: temporary method until Dumpstate object is properly set | 
|  | 291 | void SetProgress(std::unique_ptr<Progress> progress); | 
| Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 292 |  | 
| Felipe Leme | 6f674ae | 2016-11-18 17:10:33 -0800 | [diff] [blame] | 293 | void DumpstateBoard(); | 
|  | 294 |  | 
| Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 295 | /* | 
|  | 296 | * Updates the overall progress of the bugreport generation by the given weight increment. | 
|  | 297 | */ | 
| Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 298 | void UpdateProgress(int32_t delta); | 
| Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 299 |  | 
| Felipe Leme | bbaf3c1 | 2016-10-11 14:32:25 -0700 | [diff] [blame] | 300 | /* Prints the dumpstate header on `stdout`. */ | 
| Felipe Leme | 2b9b06c | 2016-10-14 09:13:06 -0700 | [diff] [blame] | 301 | void PrintHeader() const; | 
| Felipe Leme | bbaf3c1 | 2016-10-11 14:32:25 -0700 | [diff] [blame] | 302 |  | 
| Felipe Leme | 1d486fe | 2016-10-14 18:06:47 -0700 | [diff] [blame] | 303 | /* | 
|  | 304 | * Adds the temporary report to the existing .zip file, closes the .zip file, and removes the | 
|  | 305 | * temporary file. | 
|  | 306 | */ | 
|  | 307 | bool FinishZipFile(); | 
|  | 308 |  | 
| Nandana Dutt | 979388e | 2018-11-30 16:48:55 +0000 | [diff] [blame] | 309 | /* Constructs a full path inside directory with file name formatted using the given suffix. */ | 
|  | 310 | std::string GetPath(const std::string& directory, const std::string& suffix) const; | 
|  | 311 |  | 
|  | 312 | /* Constructs a full path inside bugreport_internal_dir_ with file name formatted using the | 
|  | 313 | * given suffix. */ | 
| Felipe Leme | 2b9b06c | 2016-10-14 09:13:06 -0700 | [diff] [blame] | 314 | std::string GetPath(const std::string& suffix) const; | 
| Felipe Leme | bbaf3c1 | 2016-10-11 14:32:25 -0700 | [diff] [blame] | 315 |  | 
| Vishnu Nair | 780b128 | 2017-10-10 13:57:24 -0700 | [diff] [blame] | 316 | /* Returns true if the current version supports priority dump feature. */ | 
|  | 317 | bool CurrentVersionSupportsPriorityDumps() const; | 
|  | 318 |  | 
| Nandana Dutt | 5fb117b | 2018-09-27 09:23:36 +0100 | [diff] [blame] | 319 | struct DumpOptions; | 
| Felipe Leme | e844a9d | 2016-09-21 15:01:39 -0700 | [diff] [blame] | 320 |  | 
| Nandana Dutt | 197661d | 2018-11-16 16:40:21 +0000 | [diff] [blame] | 321 | /* Main entry point for running a complete bugreport. */ | 
|  | 322 | RunStatus Run(); | 
|  | 323 |  | 
|  | 324 | /* Sets runtime options. */ | 
|  | 325 | void SetOptions(std::unique_ptr<DumpOptions> options); | 
| Nandana Dutt | 3f8c717 | 2018-09-25 12:01:54 +0100 | [diff] [blame] | 326 |  | 
| Nandana Dutt | 3f8c717 | 2018-09-25 12:01:54 +0100 | [diff] [blame] | 327 | /* | 
|  | 328 | * Structure to hold options that determine the behavior of dumpstate. | 
|  | 329 | */ | 
|  | 330 | struct DumpOptions { | 
|  | 331 | bool do_add_date = false; | 
|  | 332 | bool do_zip_file = false; | 
|  | 333 | bool do_vibrate = true; | 
|  | 334 | bool use_socket = false; | 
|  | 335 | bool use_control_socket = false; | 
|  | 336 | bool do_fb = false; | 
|  | 337 | bool do_broadcast = false; | 
|  | 338 | bool is_remote_mode = false; | 
|  | 339 | bool show_header_only = false; | 
|  | 340 | bool do_start_service = false; | 
|  | 341 | bool telephony_only = false; | 
|  | 342 | bool wifi_only = false; | 
| Nandana Dutt | 5fb117b | 2018-09-27 09:23:36 +0100 | [diff] [blame] | 343 | // Whether progress updates should be published. | 
|  | 344 | bool do_progress_updates = false; | 
| Nandana Dutt | 979388e | 2018-11-30 16:48:55 +0000 | [diff] [blame] | 345 | // File descriptor to output zip file. -1 indicates not set. Takes precedence over | 
|  | 346 | // use_outfile. | 
|  | 347 | int fd = -1; | 
|  | 348 | // Partial path to output file. | 
| Nandana Dutt | 3f8c717 | 2018-09-25 12:01:54 +0100 | [diff] [blame] | 349 | std::string use_outfile; | 
| Nandana Dutt | 58d72e2 | 2018-11-16 10:30:48 +0000 | [diff] [blame] | 350 | // TODO: rename to MODE. | 
| Nandana Dutt | 5fb117b | 2018-09-27 09:23:36 +0100 | [diff] [blame] | 351 | // Extra options passed as system property. | 
|  | 352 | std::string extra_options; | 
|  | 353 | // Command-line arguments as string | 
|  | 354 | std::string args; | 
|  | 355 | // Notification title and description | 
|  | 356 | std::string notification_title; | 
|  | 357 | std::string notification_description; | 
|  | 358 |  | 
|  | 359 | /* Initializes options from commandline arguments and system properties. */ | 
|  | 360 | RunStatus Initialize(int argc, char* argv[]); | 
|  | 361 |  | 
| Nandana Dutt | 58d72e2 | 2018-11-16 10:30:48 +0000 | [diff] [blame] | 362 | /* Initializes options from the requested mode. */ | 
|  | 363 | void Initialize(BugreportMode bugreport_mode); | 
|  | 364 |  | 
| Nandana Dutt | 5fb117b | 2018-09-27 09:23:36 +0100 | [diff] [blame] | 365 | /* Returns true if the options set so far are consistent. */ | 
|  | 366 | bool ValidateOptions() const; | 
| Nandana Dutt | 3f8c717 | 2018-09-25 12:01:54 +0100 | [diff] [blame] | 367 | }; | 
|  | 368 |  | 
|  | 369 | // TODO: initialize fields on constructor | 
| Felipe Leme | e844a9d | 2016-09-21 15:01:39 -0700 | [diff] [blame] | 370 | // dumpstate id - unique after each device reboot. | 
| Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 371 | uint32_t id_; | 
| Felipe Leme | e844a9d | 2016-09-21 15:01:39 -0700 | [diff] [blame] | 372 |  | 
| Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 373 | // dumpstate pid | 
|  | 374 | pid_t pid_; | 
|  | 375 |  | 
| Nandana Dutt | 3f8c717 | 2018-09-25 12:01:54 +0100 | [diff] [blame] | 376 | // Runtime options. | 
| Nandana Dutt | 5fb117b | 2018-09-27 09:23:36 +0100 | [diff] [blame] | 377 | std::unique_ptr<DumpOptions> options_; | 
| Felipe Leme | e844a9d | 2016-09-21 15:01:39 -0700 | [diff] [blame] | 378 |  | 
| Felipe Leme | 009ecbb | 2016-11-07 10:18:44 -0800 | [diff] [blame] | 379 | // How frequently the progess should be updated;the listener will only be notificated when the | 
|  | 380 | // delta from the previous update is more than the threshold. | 
|  | 381 | int32_t update_progress_threshold_ = 100; | 
|  | 382 |  | 
|  | 383 | // Last progress that triggered a listener updated | 
|  | 384 | int32_t last_updated_progress_; | 
|  | 385 |  | 
| Felipe Leme | bbaf3c1 | 2016-10-11 14:32:25 -0700 | [diff] [blame] | 386 | // Whether it should take an screenshot earlier in the process. | 
| Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 387 | bool do_early_screenshot_ = false; | 
| Felipe Leme | bbaf3c1 | 2016-10-11 14:32:25 -0700 | [diff] [blame] | 388 |  | 
| Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 389 | std::unique_ptr<Progress> progress_; | 
| Felipe Leme | e844a9d | 2016-09-21 15:01:39 -0700 | [diff] [blame] | 390 |  | 
|  | 391 | // 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] | 392 | int control_socket_fd_ = -1; | 
| Felipe Leme | e844a9d | 2016-09-21 15:01:39 -0700 | [diff] [blame] | 393 |  | 
| Felipe Leme | bbaf3c1 | 2016-10-11 14:32:25 -0700 | [diff] [blame] | 394 | // Bugreport format version; | 
| Felipe Leme | d071c68 | 2016-10-20 16:48:00 -0700 | [diff] [blame] | 395 | std::string version_ = VERSION_CURRENT; | 
| Felipe Leme | e844a9d | 2016-09-21 15:01:39 -0700 | [diff] [blame] | 396 |  | 
| Felipe Leme | bbaf3c1 | 2016-10-11 14:32:25 -0700 | [diff] [blame] | 397 | time_t now_; | 
|  | 398 |  | 
| Felipe Leme | 2b9b06c | 2016-10-14 09:13:06 -0700 | [diff] [blame] | 399 | // Base name (without suffix or extensions) of the bugreport files, typically | 
|  | 400 | // `bugreport-BUILD_ID`. | 
| Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 401 | std::string base_name_; | 
| Felipe Leme | bbaf3c1 | 2016-10-11 14:32:25 -0700 | [diff] [blame] | 402 |  | 
| Felipe Leme | 2b9b06c | 2016-10-14 09:13:06 -0700 | [diff] [blame] | 403 | // Name is the suffix part of the bugreport files - it's typically the date (when invoked with | 
|  | 404 | // `-d`), but it could be changed by the user.. | 
|  | 405 | std::string name_; | 
|  | 406 |  | 
| Nandana Dutt | 979388e | 2018-11-30 16:48:55 +0000 | [diff] [blame] | 407 | std::string bugreport_internal_dir_ = DUMPSTATE_DIRECTORY; | 
|  | 408 |  | 
|  | 409 | // Full path of the temporary file containing the bugreport, inside bugreport_internal_dir_. | 
|  | 410 | // At the very end this file is pulled into the zip file. | 
| Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 411 | std::string tmp_path_; | 
| Felipe Leme | 1d486fe | 2016-10-14 18:06:47 -0700 | [diff] [blame] | 412 |  | 
| Nandana Dutt | 979388e | 2018-11-30 16:48:55 +0000 | [diff] [blame] | 413 | // Full path of the file containing the dumpstate logs, inside bugreport_internal_dir_. | 
|  | 414 | // This is useful for debugging. | 
| Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 415 | std::string log_path_; | 
| Felipe Leme | 1d486fe | 2016-10-14 18:06:47 -0700 | [diff] [blame] | 416 |  | 
| Nandana Dutt | 979388e | 2018-11-30 16:48:55 +0000 | [diff] [blame] | 417 | // Full path of the bugreport file, be it zip or text, inside bugreport_internal_dir_. | 
| Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 418 | std::string path_; | 
| Felipe Leme | 1d486fe | 2016-10-14 18:06:47 -0700 | [diff] [blame] | 419 |  | 
| Nandana Dutt | 979388e | 2018-11-30 16:48:55 +0000 | [diff] [blame] | 420 | // TODO: If temporary this should be removed at the end. | 
|  | 421 | // Full path of the temporary file containing the screenshot (when requested). | 
|  | 422 | std::string screenshot_path_; | 
|  | 423 |  | 
|  | 424 | // TODO(b/111441001): remove when obsolete. | 
|  | 425 | // Full path of the final zip file inside the caller-specified directory, if available. | 
|  | 426 | std::string final_path_; | 
|  | 427 |  | 
|  | 428 | // The caller-specified directory, if available. | 
|  | 429 | std::string bugreport_dir_; | 
|  | 430 |  | 
| Felipe Leme | 1d486fe | 2016-10-14 18:06:47 -0700 | [diff] [blame] | 431 | // Pointer to the zipped file. | 
|  | 432 | std::unique_ptr<FILE, int (*)(FILE*)> zip_file{nullptr, fclose}; | 
|  | 433 |  | 
| Felipe Leme | c6bc8bc | 2016-10-27 15:58:27 -0700 | [diff] [blame] | 434 | // Pointer to the zip structure. | 
|  | 435 | std::unique_ptr<ZipWriter> zip_writer_; | 
|  | 436 |  | 
| Vishnu Nair | 20cf503 | 2018-01-05 13:15:49 -0800 | [diff] [blame] | 437 | // Binder object listening to progress. | 
| Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 438 | android::sp<android::os::IDumpstateListener> listener_; | 
|  | 439 | std::string listener_name_; | 
| Vishnu Nair | 20cf503 | 2018-01-05 13:15:49 -0800 | [diff] [blame] | 440 | bool report_section_; | 
| Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 441 |  | 
| Luis Hector Chavez | 91c2ae5 | 2018-03-14 15:12:46 -0700 | [diff] [blame] | 442 | // List of open tombstone dump files. | 
|  | 443 | std::vector<DumpData> tombstone_data_; | 
|  | 444 |  | 
|  | 445 | // List of open ANR dump files. | 
|  | 446 | std::vector<DumpData> anr_data_; | 
|  | 447 |  | 
| Felipe Leme | e844a9d | 2016-09-21 15:01:39 -0700 | [diff] [blame] | 448 | private: | 
| Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 449 | // Used by GetInstance() only. | 
| Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 450 | Dumpstate(const std::string& version = VERSION_CURRENT); | 
| Felipe Leme | 062926e | 2016-10-27 15:51:12 -0700 | [diff] [blame] | 451 |  | 
|  | 452 | DISALLOW_COPY_AND_ASSIGN(Dumpstate); | 
| Felipe Leme | e844a9d | 2016-09-21 15:01:39 -0700 | [diff] [blame] | 453 | }; | 
|  | 454 |  | 
|  | 455 | // for_each_pid_func = void (*)(int, const char*); | 
|  | 456 | // for_each_tid_func = void (*)(int, int, const char*); | 
|  | 457 |  | 
|  | 458 | typedef void(for_each_pid_func)(int, const char*); | 
|  | 459 | typedef void(for_each_tid_func)(int, int, const char*); | 
| Felipe Leme | 71ca15e | 2016-05-19 16:18:17 -0700 | [diff] [blame] | 460 |  | 
| Felipe Leme | 71a74ac | 2016-03-17 15:43:25 -0700 | [diff] [blame] | 461 | /* saves the the contents of a file as a long */ | 
|  | 462 | int read_file_as_long(const char *path, long int *output); | 
|  | 463 |  | 
| Christopher Ferris | 54bcc5f | 2015-02-10 12:15:01 -0800 | [diff] [blame] | 464 | /* prints the contents of the fd | 
|  | 465 | * fd must have been opened with the flag O_NONBLOCK. | 
|  | 466 | */ | 
| Christopher Ferris | 1fe6107 | 2014-07-22 16:08:19 -0700 | [diff] [blame] | 467 | 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] | 468 |  | 
| Mark Salyzyn | 326842f | 2015-04-30 09:49:41 -0700 | [diff] [blame] | 469 | /* calls skip to gate calling dump_from_fd recursively | 
|  | 470 | * in the specified directory. dump_from_fd defaults to | 
|  | 471 | * dump_file_from_fd above when set to NULL. skip defaults | 
|  | 472 | * to false when set to NULL. dump_from_fd will always be | 
|  | 473 | * called with title NULL. | 
|  | 474 | */ | 
| Felipe Leme | 678727a | 2016-09-21 17:22:11 -0700 | [diff] [blame] | 475 | int dump_files(const std::string& title, const char* dir, bool (*skip)(const char* path), | 
|  | 476 | int (*dump_from_fd)(const char* title, const char* path, int fd)); | 
| Mark Salyzyn | 326842f | 2015-04-30 09:49:41 -0700 | [diff] [blame] | 477 |  | 
| Felipe Leme | 2628e9e | 2016-04-12 16:36:51 -0700 | [diff] [blame] | 478 | /** opens a socket and returns its file descriptor */ | 
|  | 479 | int open_socket(const char *service); | 
|  | 480 |  | 
| Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 481 | /* redirect output to a service control socket */ | 
|  | 482 | void redirect_to_socket(FILE *redirect, const char *service); | 
|  | 483 |  | 
| Felipe Leme | 0f3fb20 | 2016-06-10 17:10:53 -0700 | [diff] [blame] | 484 | /* redirect output to a new file */ | 
| Christopher Ferris | ff4a4dc | 2015-02-09 16:24:47 -0800 | [diff] [blame] | 485 | void redirect_to_file(FILE *redirect, char *path); | 
| Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 486 |  | 
| Felipe Leme | 0f3fb20 | 2016-06-10 17:10:53 -0700 | [diff] [blame] | 487 | /* redirect output to an existing file */ | 
|  | 488 | void redirect_to_existing_file(FILE *redirect, char *path); | 
|  | 489 |  | 
| Felipe Leme | 111b9d0 | 2016-02-03 09:28:24 -0800 | [diff] [blame] | 490 | /* create leading directories, if necessary */ | 
|  | 491 | void create_parent_dirs(const char *path); | 
|  | 492 |  | 
| Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 493 | /* dump Dalvik and native stack traces, return the trace file location (NULL if none) */ | 
|  | 494 | const char *dump_traces(); | 
| Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 495 |  | 
|  | 496 | /* for each process in the system, run the specified function */ | 
| Colin Cross | 0c22e8b | 2012-11-02 15:46:56 -0700 | [diff] [blame] | 497 | void for_each_pid(for_each_pid_func func, const char *header); | 
|  | 498 |  | 
|  | 499 | /* for each thread in the system, run the specified function */ | 
|  | 500 | void for_each_tid(for_each_tid_func func, const char *header); | 
| Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 501 |  | 
|  | 502 | /* Displays a blocked processes in-kernel wait channel */ | 
| Colin Cross | 0c22e8b | 2012-11-02 15:46:56 -0700 | [diff] [blame] | 503 | void show_wchan(int pid, int tid, const char *name); | 
| Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 504 |  | 
| Mark Salyzyn | a297c32 | 2016-02-05 15:33:17 -0800 | [diff] [blame] | 505 | /* Displays a processes times */ | 
|  | 506 | void show_showtime(int pid, const char *name); | 
|  | 507 |  | 
| Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 508 | /* Runs "showmap" for a process */ | 
|  | 509 | void do_showmap(int pid, const char *name); | 
|  | 510 |  | 
|  | 511 | /* Gets the dmesg output for the kernel */ | 
|  | 512 | void do_dmesg(); | 
|  | 513 |  | 
| Sreeram Ramachandran | 2b3bba3 | 2014-07-08 15:40:55 -0700 | [diff] [blame] | 514 | /* Prints the contents of all the routing tables, both IPv4 and IPv6. */ | 
|  | 515 | void dump_route_tables(); | 
|  | 516 |  | 
| Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 517 | /* Play a sound via Stagefright */ | 
| Christopher Ferris | 1fe6107 | 2014-07-22 16:08:19 -0700 | [diff] [blame] | 518 | void play_sound(const char *path); | 
| Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 519 |  | 
| Felipe Leme | 0c80cf0 | 2016-01-05 13:25:34 -0800 | [diff] [blame] | 520 | /* Checks if a given path is a directory. */ | 
|  | 521 | bool is_dir(const char* pathname); | 
|  | 522 |  | 
|  | 523 | /** Gets the last modification time of a file, or default time if file is not found. */ | 
|  | 524 | time_t get_mtime(int fd, time_t default_mtime); | 
|  | 525 |  | 
| Calvin On | 249beee | 2016-06-03 15:17:07 -0700 | [diff] [blame] | 526 | /* Dumps eMMC Extended CSD data. */ | 
| Felipe Leme | 78f2c86 | 2015-12-21 09:55:22 -0800 | [diff] [blame] | 527 | void dump_emmc_ecsd(const char *ext_csd_path); | 
|  | 528 |  | 
| Calvin On | 249beee | 2016-06-03 15:17:07 -0700 | [diff] [blame] | 529 | /** Gets command-line arguments. */ | 
| Felipe Leme | a34efb7 | 2016-03-11 09:33:32 -0800 | [diff] [blame] | 530 | void format_args(int argc, const char *argv[], std::string *args); | 
| Felipe Leme | 88c7933 | 2016-02-22 11:06:49 -0800 | [diff] [blame] | 531 |  | 
| Vishnu Nair | 20cf503 | 2018-01-05 13:15:49 -0800 | [diff] [blame] | 532 | /** Main entry point for dumpstate. */ | 
|  | 533 | int run_main(int argc, char* argv[]); | 
|  | 534 |  | 
| Felipe Leme | 8620bb4 | 2015-11-10 11:04:45 -0800 | [diff] [blame] | 535 | #ifdef __cplusplus | 
|  | 536 | } | 
|  | 537 | #endif | 
|  | 538 |  | 
| Felipe Leme | 8268ed2 | 2016-08-02 18:18:25 -0700 | [diff] [blame] | 539 | #endif /* FRAMEWORK_NATIVE_CMD_DUMPSTATE_H_ */ |