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