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 | |
Kedar Chitnis | 9fd8c05 | 2021-11-16 09:09:22 +0000 | [diff] [blame] | 28 | #include <aidl/android/hardware/dumpstate/IDumpstateDevice.h> |
Felipe Leme | c6bc8bc | 2016-10-27 15:58:27 -0700 | [diff] [blame] | 29 | #include <android-base/macros.h> |
Luis Hector Chavez | 91c2ae5 | 2018-03-14 15:12:46 -0700 | [diff] [blame] | 30 | #include <android-base/unique_fd.h> |
Hunter Knepshield | 8540faf | 2020-02-04 19:47:20 -0800 | [diff] [blame] | 31 | #include <android/hardware/dumpstate/1.1/types.h> |
Nandana Dutt | d2f5f08 | 2019-01-18 17:13:52 +0000 | [diff] [blame] | 32 | #include <android/os/BnIncidentAuthListener.h> |
Nandana Dutt | 197661d | 2018-11-16 16:40:21 +0000 | [diff] [blame] | 33 | #include <android/os/IDumpstate.h> |
Jiyong Park | b22e65d | 2017-06-23 21:23:16 +0900 | [diff] [blame] | 34 | #include <android/os/IDumpstateListener.h> |
| 35 | #include <utils/StrongPointer.h> |
Felipe Leme | c6bc8bc | 2016-10-27 15:58:27 -0700 | [diff] [blame] | 36 | #include <ziparchive/zip_writer.h> |
| 37 | |
Felipe Leme | bda15a0 | 2016-11-16 17:48:25 -0800 | [diff] [blame] | 38 | #include "DumpstateUtil.h" |
Rhed Jao | 1c85512 | 2020-07-16 17:37:39 +0800 | [diff] [blame] | 39 | #include "DumpPool.h" |
Rhed Jao | 4875aa6 | 2020-07-20 17:46:29 +0800 | [diff] [blame] | 40 | #include "TaskQueue.h" |
Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 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; |
Dieter Hsu | 105ad0c | 2020-09-29 15:23:33 +0800 | [diff] [blame] | 53 | class ZippedBugReportStreamTest; |
Felipe Leme | 47e9be2 | 2016-12-21 15:37:07 -0800 | [diff] [blame] | 54 | |
| 55 | } // namespace dumpstate |
| 56 | } // namespace os |
| 57 | } // namespace android |
| 58 | |
Jiyong Park | b22e65d | 2017-06-23 21:23:16 +0900 | [diff] [blame] | 59 | class ZipWriter; |
| 60 | |
Felipe Leme | 30dbfa1 | 2016-09-02 12:43:26 -0700 | [diff] [blame] | 61 | // TODO: remove once moved to HAL |
Felipe Leme | 8620bb4 | 2015-11-10 11:04:45 -0800 | [diff] [blame] | 62 | #ifdef __cplusplus |
| 63 | extern "C" { |
| 64 | #endif |
| 65 | |
Felipe Leme | 30dbfa1 | 2016-09-02 12:43:26 -0700 | [diff] [blame] | 66 | /* |
Felipe Leme | 30dbfa1 | 2016-09-02 12:43:26 -0700 | [diff] [blame] | 67 | * Helper class used to report how long it takes for a section to finish. |
| 68 | * |
| 69 | * Typical usage: |
| 70 | * |
| 71 | * DurationReporter duration_reporter(title); |
| 72 | * |
| 73 | */ |
| 74 | class DurationReporter { |
| 75 | public: |
Nandana Dutt | 8d945c0 | 2019-08-14 13:30:07 +0100 | [diff] [blame] | 76 | explicit DurationReporter(const std::string& title, bool logcat_only = false, |
Rhed Jao | 1c85512 | 2020-07-16 17:37:39 +0800 | [diff] [blame] | 77 | bool verbose = false, int duration_fd = STDOUT_FILENO); |
Felipe Leme | 30dbfa1 | 2016-09-02 12:43:26 -0700 | [diff] [blame] | 78 | |
| 79 | ~DurationReporter(); |
| 80 | |
Felipe Leme | 30dbfa1 | 2016-09-02 12:43:26 -0700 | [diff] [blame] | 81 | private: |
Felipe Leme | 678727a | 2016-09-21 17:22:11 -0700 | [diff] [blame] | 82 | std::string title_; |
Nandana Dutt | a8470b8 | 2019-03-11 11:00:58 +0000 | [diff] [blame] | 83 | bool logcat_only_; |
Nandana Dutt | 8d945c0 | 2019-08-14 13:30:07 +0100 | [diff] [blame] | 84 | bool verbose_; |
Felipe Leme | b0f669d | 2016-09-26 18:26:11 -0700 | [diff] [blame] | 85 | uint64_t started_; |
Rhed Jao | 1c85512 | 2020-07-16 17:37:39 +0800 | [diff] [blame] | 86 | int duration_fd_; |
Felipe Leme | 062926e | 2016-10-27 15:51:12 -0700 | [diff] [blame] | 87 | |
| 88 | DISALLOW_COPY_AND_ASSIGN(DurationReporter); |
Felipe Leme | 30dbfa1 | 2016-09-02 12:43:26 -0700 | [diff] [blame] | 89 | }; |
| 90 | |
| 91 | /* |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 92 | * 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] | 93 | * |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 94 | * Each `dumpstate` section contributes to the total weight by an individual weight, so the overall |
| 95 | * 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] | 96 | * |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 97 | * The estimated max progress is initially set to a value (`kDefaultMax) defined empirically, but |
| 98 | * 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] | 99 | * |
Felipe Leme | 71bbfc5 | 2015-11-23 14:14:51 -0800 | [diff] [blame] | 100 | */ |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 101 | class Progress { |
Felipe Leme | 47e9be2 | 2016-12-21 15:37:07 -0800 | [diff] [blame] | 102 | friend class android::os::dumpstate::ProgressTest; |
| 103 | friend class android::os::dumpstate::DumpstateTest; |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 104 | |
| 105 | public: |
| 106 | /* |
| 107 | * Default estimation of the max duration of a bugreport generation. |
| 108 | * |
| 109 | * It does not need to match the exact sum of all sections, but ideally it should to be slight |
| 110 | * more than such sum: a value too high will cause the bugreport to finish before the user |
| 111 | * expected (for example, jumping from 70% to 100%), while a value too low will cause the |
| 112 | * progress to get stuck at an almost-finished value (like 99%) for a while. |
| 113 | * |
| 114 | * This constant is only used when the average duration from previous runs cannot be used. |
| 115 | */ |
| 116 | static const int kDefaultMax; |
| 117 | |
Chih-Hung Hsieh | d0937e6 | 2018-12-20 15:43:57 -0800 | [diff] [blame] | 118 | explicit Progress(const std::string& path = ""); |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 119 | |
| 120 | // Gets the current progress. |
| 121 | int32_t Get() const; |
| 122 | |
| 123 | // Gets the current estimated max progress. |
| 124 | int32_t GetMax() const; |
| 125 | |
| 126 | // Gets the initial estimated max progress. |
| 127 | int32_t GetInitialMax() const; |
| 128 | |
| 129 | // Increments progress (ignored if not positive). |
| 130 | // Returns `true` if the max progress increased as well. |
| 131 | bool Inc(int32_t delta); |
| 132 | |
| 133 | // Persist the stats. |
| 134 | void Save(); |
| 135 | |
| 136 | void Dump(int fd, const std::string& prefix) const; |
| 137 | |
| 138 | private: |
| 139 | Progress(int32_t initial_max, float growth_factor, |
| 140 | const std::string& path = ""); // Used by test cases. |
| 141 | Progress(int32_t initial_max, int32_t progress, float growth_factor); // Used by test cases. |
| 142 | void Load(); |
| 143 | int32_t initial_max_; |
| 144 | int32_t progress_; |
| 145 | int32_t max_; |
| 146 | float growth_factor_; |
| 147 | int32_t n_runs_; |
| 148 | int32_t average_max_; |
Nandana Dutt | 979388e | 2018-11-30 16:48:55 +0000 | [diff] [blame] | 149 | std::string path_; |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 150 | }; |
Felipe Leme | 71bbfc5 | 2015-11-23 14:14:51 -0800 | [diff] [blame] | 151 | |
Felipe Leme | 71bbfc5 | 2015-11-23 14:14:51 -0800 | [diff] [blame] | 152 | /* |
Felipe Leme | bbaf3c1 | 2016-10-11 14:32:25 -0700 | [diff] [blame] | 153 | * List of supported zip format versions. |
| 154 | * |
| 155 | * See bugreport-format.md for more info. |
| 156 | */ |
Vishnu Nair | 64afc02 | 2018-02-01 15:29:34 -0800 | [diff] [blame] | 157 | static std::string VERSION_CURRENT = "2.0"; |
Felipe Leme | d071c68 | 2016-10-20 16:48:00 -0700 | [diff] [blame] | 158 | |
| 159 | /* |
| 160 | * "Alias" for the current version. |
| 161 | */ |
| 162 | static std::string VERSION_DEFAULT = "default"; |
Felipe Leme | bbaf3c1 | 2016-10-11 14:32:25 -0700 | [diff] [blame] | 163 | |
| 164 | /* |
Nandana Dutt | 979388e | 2018-11-30 16:48:55 +0000 | [diff] [blame] | 165 | * Directory used by Dumpstate binary to keep its local files. |
| 166 | */ |
| 167 | static const std::string DUMPSTATE_DIRECTORY = "/bugreports"; |
| 168 | |
| 169 | /* |
Luis Hector Chavez | 91c2ae5 | 2018-03-14 15:12:46 -0700 | [diff] [blame] | 170 | * Structure that contains the information of an open dump file. |
| 171 | */ |
| 172 | struct DumpData { |
| 173 | // Path of the file. |
| 174 | std::string name; |
| 175 | |
| 176 | // Open file descriptor for the file. |
| 177 | android::base::unique_fd fd; |
| 178 | |
| 179 | // Modification time of the file. |
| 180 | time_t mtime; |
| 181 | }; |
| 182 | |
| 183 | /* |
Felipe Leme | e844a9d | 2016-09-21 15:01:39 -0700 | [diff] [blame] | 184 | * Main class driving a bugreport generation. |
| 185 | * |
| 186 | * Currently, it only contains variables that are accessed externally, but gradually the functions |
| 187 | * that are spread accross utils.cpp and dumpstate.cpp will be moved to it. |
| 188 | */ |
| 189 | class Dumpstate { |
Rhed Jao | 1c85512 | 2020-07-16 17:37:39 +0800 | [diff] [blame] | 190 | friend class android::os::dumpstate::DumpstateTest; |
Dieter Hsu | 105ad0c | 2020-09-29 15:23:33 +0800 | [diff] [blame] | 191 | friend class android::os::dumpstate::ZippedBugReportStreamTest; |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 192 | |
Felipe Leme | e844a9d | 2016-09-21 15:01:39 -0700 | [diff] [blame] | 193 | public: |
Nandana Dutt | d2f5f08 | 2019-01-18 17:13:52 +0000 | [diff] [blame] | 194 | enum RunStatus { OK, HELP, INVALID_INPUT, ERROR, USER_CONSENT_DENIED, USER_CONSENT_TIMED_OUT }; |
Nandana Dutt | 5fb117b | 2018-09-27 09:23:36 +0100 | [diff] [blame] | 195 | |
Nandana Dutt | 58d72e2 | 2018-11-16 10:30:48 +0000 | [diff] [blame] | 196 | // The mode under which the bugreport should be run. Each mode encapsulates a few options. |
| 197 | enum BugreportMode { |
Nandana Dutt | 197661d | 2018-11-16 16:40:21 +0000 | [diff] [blame] | 198 | BUGREPORT_FULL = android::os::IDumpstate::BUGREPORT_MODE_FULL, |
| 199 | BUGREPORT_INTERACTIVE = android::os::IDumpstate::BUGREPORT_MODE_INTERACTIVE, |
| 200 | BUGREPORT_REMOTE = android::os::IDumpstate::BUGREPORT_MODE_REMOTE, |
| 201 | BUGREPORT_WEAR = android::os::IDumpstate::BUGREPORT_MODE_WEAR, |
| 202 | BUGREPORT_TELEPHONY = android::os::IDumpstate::BUGREPORT_MODE_TELEPHONY, |
Abhijeet Kaur | 904e0e0 | 2018-12-05 14:03:01 +0000 | [diff] [blame] | 203 | BUGREPORT_WIFI = android::os::IDumpstate::BUGREPORT_MODE_WIFI, |
| 204 | BUGREPORT_DEFAULT = android::os::IDumpstate::BUGREPORT_MODE_DEFAULT |
Nandana Dutt | 58d72e2 | 2018-11-16 10:30:48 +0000 | [diff] [blame] | 205 | }; |
| 206 | |
Kean Mariotti | 306633e | 2022-09-05 16:30:47 +0000 | [diff] [blame] | 207 | // The flags used to customize bugreport requests. |
| 208 | enum BugreportFlag { |
| 209 | BUGREPORT_USE_PREDUMPED_UI_DATA = |
Gavin Corkery | a44686c | 2022-11-23 18:16:51 +0000 | [diff] [blame] | 210 | android::os::IDumpstate::BUGREPORT_FLAG_USE_PREDUMPED_UI_DATA, |
| 211 | BUGREPORT_FLAG_DEFER_CONSENT = |
| 212 | android::os::IDumpstate::BUGREPORT_FLAG_DEFER_CONSENT |
Kean Mariotti | 306633e | 2022-09-05 16:30:47 +0000 | [diff] [blame] | 213 | }; |
| 214 | |
Felipe Leme | 47e9be2 | 2016-12-21 15:37:07 -0800 | [diff] [blame] | 215 | static android::os::dumpstate::CommandOptions DEFAULT_DUMPSYS; |
Felipe Leme | bda15a0 | 2016-11-16 17:48:25 -0800 | [diff] [blame] | 216 | |
Felipe Leme | e844a9d | 2016-09-21 15:01:39 -0700 | [diff] [blame] | 217 | static Dumpstate& GetInstance(); |
Felipe Leme | 71bbfc5 | 2015-11-23 14:14:51 -0800 | [diff] [blame] | 218 | |
Abhijeet Kaur | a407fb8 | 2020-03-27 12:51:12 +0000 | [diff] [blame] | 219 | /* Initialize dumpstate fields before starting bugreport generation */ |
| 220 | void Initialize(); |
| 221 | |
Felipe Leme | 6ad9c06 | 2016-09-28 11:58:36 -0700 | [diff] [blame] | 222 | /* |
Felipe Leme | 678727a | 2016-09-21 17:22:11 -0700 | [diff] [blame] | 223 | * Forks a command, waits for it to finish, and returns its status. |
| 224 | * |
| 225 | * |title| description of the command printed on `stdout` (or empty to skip |
| 226 | * description). |
| 227 | * |full_command| array containing the command (first entry) and its arguments. |
| 228 | * Must contain at least one element. |
| 229 | * |options| optional argument defining the command's behavior. |
Rhed Jao | 4875aa6 | 2020-07-20 17:46:29 +0800 | [diff] [blame] | 230 | * |out_fd| A fd to support the DumpPool to output results to a temporary |
| 231 | * file. Using STDOUT_FILENO if it's not running in the parallel task. |
Felipe Leme | 678727a | 2016-09-21 17:22:11 -0700 | [diff] [blame] | 232 | */ |
| 233 | int RunCommand(const std::string& title, const std::vector<std::string>& fullCommand, |
Felipe Leme | 47e9be2 | 2016-12-21 15:37:07 -0800 | [diff] [blame] | 234 | const android::os::dumpstate::CommandOptions& options = |
Nandana Dutt | 8d945c0 | 2019-08-14 13:30:07 +0100 | [diff] [blame] | 235 | android::os::dumpstate::CommandOptions::DEFAULT, |
Rhed Jao | 4875aa6 | 2020-07-20 17:46:29 +0800 | [diff] [blame] | 236 | bool verbose_duration = false, int out_fd = STDOUT_FILENO); |
Felipe Leme | 678727a | 2016-09-21 17:22:11 -0700 | [diff] [blame] | 237 | |
| 238 | /* |
| 239 | * Runs `dumpsys` with the given arguments, automatically setting its timeout |
Vishnu Nair | 6921f80 | 2017-11-22 09:17:23 -0800 | [diff] [blame] | 240 | * (`-T` argument) |
Felipe Leme | 678727a | 2016-09-21 17:22:11 -0700 | [diff] [blame] | 241 | * according to the command options. |
| 242 | * |
| 243 | * |title| description of the command printed on `stdout` (or empty to skip |
| 244 | * description). |
| 245 | * |dumpsys_args| `dumpsys` arguments (except `-t`). |
| 246 | * |options| optional argument defining the command's behavior. |
Vishnu Nair | 6921f80 | 2017-11-22 09:17:23 -0800 | [diff] [blame] | 247 | * |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] | 248 | * timeout from `options`) |
Rhed Jao | 6b1ea2d | 2020-07-21 17:58:41 +0800 | [diff] [blame] | 249 | * |out_fd| A fd to support the DumpPool to output results to a temporary |
| 250 | * file. Using STDOUT_FILENO if it's not running in the parallel task. |
Felipe Leme | 678727a | 2016-09-21 17:22:11 -0700 | [diff] [blame] | 251 | */ |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 252 | 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] | 253 | const android::os::dumpstate::CommandOptions& options = DEFAULT_DUMPSYS, |
Rhed Jao | 6b1ea2d | 2020-07-21 17:58:41 +0800 | [diff] [blame] | 254 | long dumpsys_timeout_ms = 0, int out_fd = STDOUT_FILENO); |
Felipe Leme | 678727a | 2016-09-21 17:22:11 -0700 | [diff] [blame] | 255 | |
| 256 | /* |
| 257 | * Prints the contents of a file. |
| 258 | * |
| 259 | * |title| description of the command printed on `stdout` (or empty to skip |
| 260 | * description). |
| 261 | * |path| location of the file to be dumped. |
| 262 | */ |
| 263 | int DumpFile(const std::string& title, const std::string& path); |
| 264 | |
Felipe Leme | bbaf3c1 | 2016-10-11 14:32:25 -0700 | [diff] [blame] | 265 | /* |
Felipe Leme | 1d486fe | 2016-10-14 18:06:47 -0700 | [diff] [blame] | 266 | * Adds a new entry to the existing zip file. |
| 267 | * */ |
| 268 | bool AddZipEntry(const std::string& entry_name, const std::string& entry_path); |
| 269 | |
| 270 | /* |
| 271 | * Adds a new entry to the existing zip file. |
Vishnu Nair | e97d612 | 2018-01-18 13:58:56 -0800 | [diff] [blame] | 272 | * |
| 273 | * |entry_name| destination path of the new entry. |
| 274 | * |fd| file descriptor to read from. |
| 275 | * |timeout| timeout to terminate the read if not completed. Set |
| 276 | * value of 0s (default) to disable timeout. |
Felipe Leme | 1d486fe | 2016-10-14 18:06:47 -0700 | [diff] [blame] | 277 | */ |
Vishnu Nair | e97d612 | 2018-01-18 13:58:56 -0800 | [diff] [blame] | 278 | android::status_t AddZipEntryFromFd(const std::string& entry_name, int fd, |
| 279 | std::chrono::milliseconds timeout); |
Felipe Leme | 1d486fe | 2016-10-14 18:06:47 -0700 | [diff] [blame] | 280 | |
| 281 | /* |
Nandana Dutt | a7db634 | 2018-11-21 14:53:34 +0000 | [diff] [blame] | 282 | * Adds a text entry to the existing zip file. |
Felipe Leme | 1d486fe | 2016-10-14 18:06:47 -0700 | [diff] [blame] | 283 | */ |
| 284 | bool AddTextZipEntry(const std::string& entry_name, const std::string& content); |
| 285 | |
| 286 | /* |
| 287 | * Adds all files from a directory to the zipped bugreport file. |
| 288 | */ |
| 289 | void AddDir(const std::string& dir, bool recursive); |
| 290 | |
| 291 | /* |
Felipe Leme | bbaf3c1 | 2016-10-11 14:32:25 -0700 | [diff] [blame] | 292 | * Takes a screenshot and save it to the given `path`. |
| 293 | * |
| 294 | * If `path` is empty, uses a standard path based on the bugreport name. |
| 295 | */ |
| 296 | void TakeScreenshot(const std::string& path = ""); |
| 297 | |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 298 | ///////////////////////////////////////////////////////////////////// |
| 299 | // TODO: members below should be private once refactor is finished // |
| 300 | ///////////////////////////////////////////////////////////////////// |
| 301 | |
| 302 | // TODO: temporary method until Dumpstate object is properly set |
| 303 | void SetProgress(std::unique_ptr<Progress> progress); |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 304 | |
Nandana Dutt | cf419a7 | 2019-03-14 10:40:17 +0000 | [diff] [blame] | 305 | // Dumps Dalvik and native stack traces, sets the trace file location to path |
| 306 | // if it succeeded. |
| 307 | // Note that it returns early if user consent is denied with status USER_CONSENT_DENIED. |
| 308 | // Returns OK in all other cases. |
| 309 | RunStatus DumpTraces(const char** path); |
Nandana Dutt | faafd52 | 2019-03-11 09:23:09 +0000 | [diff] [blame] | 310 | |
Rhed Jao | 4875aa6 | 2020-07-20 17:46:29 +0800 | [diff] [blame] | 311 | /* |
| 312 | * |out_fd| A fd to support the DumpPool to output results to a temporary file. |
| 313 | * Dumpstate can pick up later and output to the bugreport. Using STDOUT_FILENO |
| 314 | * if it's not running in the parallel task. |
| 315 | */ |
| 316 | void DumpstateBoard(int out_fd = STDOUT_FILENO); |
Felipe Leme | 6f674ae | 2016-11-18 17:10:33 -0800 | [diff] [blame] | 317 | |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 318 | /* |
| 319 | * Updates the overall progress of the bugreport generation by the given weight increment. |
| 320 | */ |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 321 | void UpdateProgress(int32_t delta); |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 322 | |
Felipe Leme | bbaf3c1 | 2016-10-11 14:32:25 -0700 | [diff] [blame] | 323 | /* Prints the dumpstate header on `stdout`. */ |
Felipe Leme | 2b9b06c | 2016-10-14 09:13:06 -0700 | [diff] [blame] | 324 | void PrintHeader() const; |
Felipe Leme | bbaf3c1 | 2016-10-11 14:32:25 -0700 | [diff] [blame] | 325 | |
Felipe Leme | 1d486fe | 2016-10-14 18:06:47 -0700 | [diff] [blame] | 326 | /* |
| 327 | * Adds the temporary report to the existing .zip file, closes the .zip file, and removes the |
| 328 | * temporary file. |
| 329 | */ |
| 330 | bool FinishZipFile(); |
| 331 | |
Nandana Dutt | 979388e | 2018-11-30 16:48:55 +0000 | [diff] [blame] | 332 | /* Constructs a full path inside directory with file name formatted using the given suffix. */ |
| 333 | std::string GetPath(const std::string& directory, const std::string& suffix) const; |
| 334 | |
| 335 | /* Constructs a full path inside bugreport_internal_dir_ with file name formatted using the |
| 336 | * given suffix. */ |
Felipe Leme | 2b9b06c | 2016-10-14 09:13:06 -0700 | [diff] [blame] | 337 | std::string GetPath(const std::string& suffix) const; |
Felipe Leme | bbaf3c1 | 2016-10-11 14:32:25 -0700 | [diff] [blame] | 338 | |
Vishnu Nair | 780b128 | 2017-10-10 13:57:24 -0700 | [diff] [blame] | 339 | /* Returns true if the current version supports priority dump feature. */ |
| 340 | bool CurrentVersionSupportsPriorityDumps() const; |
| 341 | |
Nandana Dutt | 5fb117b | 2018-09-27 09:23:36 +0100 | [diff] [blame] | 342 | struct DumpOptions; |
Felipe Leme | e844a9d | 2016-09-21 15:01:39 -0700 | [diff] [blame] | 343 | |
Kean Mariotti | 306633e | 2022-09-05 16:30:47 +0000 | [diff] [blame] | 344 | /** |
| 345 | * Pre-dump critical UI data, e.g. data stored in short ring buffers that might get lost |
| 346 | * by the time the actual bugreport is requested. |
| 347 | */ |
| 348 | void PreDumpUiData(); |
| 349 | |
Abhijeet Kaur | a407fb8 | 2020-03-27 12:51:12 +0000 | [diff] [blame] | 350 | /* |
| 351 | * Main entry point for running a complete bugreport. |
| 352 | * |
| 353 | * Initialize() dumpstate before calling this method. |
| 354 | * |
| 355 | */ |
Nandana Dutt | d2f5f08 | 2019-01-18 17:13:52 +0000 | [diff] [blame] | 356 | RunStatus Run(int32_t calling_uid, const std::string& calling_package); |
Nandana Dutt | 197661d | 2018-11-16 16:40:21 +0000 | [diff] [blame] | 357 | |
Gavin Corkery | a44686c | 2022-11-23 18:16:51 +0000 | [diff] [blame] | 358 | /* |
| 359 | * Entry point for retrieving a previous-generated bugreport. |
| 360 | * |
| 361 | * Initialize() dumpstate before calling this method. |
| 362 | */ |
| 363 | RunStatus Retrieve(int32_t calling_uid, const std::string& calling_package); |
| 364 | |
| 365 | |
| 366 | |
Nandana Dutt | f02564e | 2019-02-15 15:24:24 +0000 | [diff] [blame] | 367 | RunStatus ParseCommandlineAndRun(int argc, char* argv[]); |
| 368 | |
Nandana Dutt | 8ae16e6 | 2020-03-27 10:20:22 +0000 | [diff] [blame] | 369 | /* Deletes in-progress files */ |
| 370 | void Cancel(); |
| 371 | |
Nandana Dutt | 197661d | 2018-11-16 16:40:21 +0000 | [diff] [blame] | 372 | /* Sets runtime options. */ |
| 373 | void SetOptions(std::unique_ptr<DumpOptions> options); |
Nandana Dutt | 3f8c717 | 2018-09-25 12:01:54 +0100 | [diff] [blame] | 374 | |
Nandana Dutt | 3f8c717 | 2018-09-25 12:01:54 +0100 | [diff] [blame] | 375 | /* |
Nandana Dutt | bbdb5b4 | 2019-03-12 10:52:56 +0000 | [diff] [blame] | 376 | * Returns true if user consent is necessary and has been denied. |
| 377 | * Consent is only necessary if the caller has asked to copy over the bugreport to a file they |
| 378 | * provided. |
| 379 | */ |
| 380 | bool IsUserConsentDenied() const; |
| 381 | |
| 382 | /* |
Abhijeet Kaur | 359b1ff | 2019-07-26 16:01:36 +0100 | [diff] [blame] | 383 | * Returns true if dumpstate is called by bugreporting API |
| 384 | */ |
| 385 | bool CalledByApi() const; |
| 386 | |
| 387 | /* |
Rhed Jao | 4875aa6 | 2020-07-20 17:46:29 +0800 | [diff] [blame] | 388 | * Enqueues a task to the dumpstate's TaskQueue if the parallel run is enabled, |
| 389 | * otherwise invokes it immediately. The task adds file at path entry_path |
| 390 | * as a zip file entry with name entry_name. Unlinks entry_path when done. |
| 391 | * |
| 392 | * All enqueued tasks will be executed in the dumpstate's FinishZipFile method |
| 393 | * before the zip file is finished. Tasks will be cancelled in dumpstate's |
| 394 | * ShutdownDumpPool method if they have never been called. |
| 395 | */ |
| 396 | void EnqueueAddZipEntryAndCleanupIfNeeded(const std::string& entry_name, |
| 397 | const std::string& entry_path); |
| 398 | |
| 399 | /* |
Nandana Dutt | 3f8c717 | 2018-09-25 12:01:54 +0100 | [diff] [blame] | 400 | * Structure to hold options that determine the behavior of dumpstate. |
| 401 | */ |
| 402 | struct DumpOptions { |
Nandana Dutt | 3f8c717 | 2018-09-25 12:01:54 +0100 | [diff] [blame] | 403 | bool do_vibrate = true; |
Dieter Hsu | 105ad0c | 2020-09-29 15:23:33 +0800 | [diff] [blame] | 404 | // Writes bugreport zipped file to a socket. |
| 405 | bool stream_to_socket = false; |
| 406 | // Writes generation progress updates to a socket. |
| 407 | bool progress_updates_to_socket = false; |
Paul Chang | 0d2aad7 | 2020-02-13 20:04:03 +0800 | [diff] [blame] | 408 | bool do_screenshot = false; |
| 409 | bool is_screenshot_copied = false; |
Gavin Corkery | a44686c | 2022-11-23 18:16:51 +0000 | [diff] [blame] | 410 | bool is_consent_deferred = false; |
Nandana Dutt | 3f8c717 | 2018-09-25 12:01:54 +0100 | [diff] [blame] | 411 | bool is_remote_mode = false; |
| 412 | bool show_header_only = false; |
Nandana Dutt | 3f8c717 | 2018-09-25 12:01:54 +0100 | [diff] [blame] | 413 | bool telephony_only = false; |
| 414 | bool wifi_only = false; |
mhasank | d451a47 | 2020-05-26 18:02:39 -0700 | [diff] [blame] | 415 | // Trimmed-down version of dumpstate to only include whitelisted logs. |
| 416 | bool limited_only = false; |
Nandana Dutt | 5fb117b | 2018-09-27 09:23:36 +0100 | [diff] [blame] | 417 | // Whether progress updates should be published. |
| 418 | bool do_progress_updates = false; |
Kedar Chitnis | 9fd8c05 | 2021-11-16 09:09:22 +0000 | [diff] [blame] | 419 | // this is used to derive dumpstate HAL bug report mode |
Hunter Knepshield | 8540faf | 2020-02-04 19:47:20 -0800 | [diff] [blame] | 420 | // TODO(b/148168577) get rid of the AIDL values, replace them with the HAL values instead. |
| 421 | // The HAL is actually an API surface that can be validated, while the AIDL is not (@hide). |
Kedar Chitnis | 9fd8c05 | 2021-11-16 09:09:22 +0000 | [diff] [blame] | 422 | BugreportMode bugreport_mode = Dumpstate::BugreportMode::BUGREPORT_DEFAULT; |
Kean Mariotti | 306633e | 2022-09-05 16:30:47 +0000 | [diff] [blame] | 423 | // Will use data collected through a previous call to PreDumpUiData(). |
| 424 | bool use_predumped_ui_data; |
mhasank | 3a4cfb4 | 2020-06-15 18:06:43 -0700 | [diff] [blame] | 425 | // File descriptor to output zip file. Takes precedence over out_dir. |
Nandana Dutt | 54dbd67 | 2019-01-11 12:58:05 +0000 | [diff] [blame] | 426 | android::base::unique_fd bugreport_fd; |
| 427 | // File descriptor to screenshot file. |
Nandana Dutt | 54dbd67 | 2019-01-11 12:58:05 +0000 | [diff] [blame] | 428 | android::base::unique_fd screenshot_fd; |
mhasank | 3a4cfb4 | 2020-06-15 18:06:43 -0700 | [diff] [blame] | 429 | // Custom output directory. |
| 430 | std::string out_dir; |
Kedar Chitnis | 9fd8c05 | 2021-11-16 09:09:22 +0000 | [diff] [blame] | 431 | // Bugreport mode of the bugreport as a string |
| 432 | std::string bugreport_mode_string; |
Nandana Dutt | 5fb117b | 2018-09-27 09:23:36 +0100 | [diff] [blame] | 433 | // Command-line arguments as string |
| 434 | std::string args; |
| 435 | // Notification title and description |
| 436 | std::string notification_title; |
| 437 | std::string notification_description; |
| 438 | |
Abhijeet Kaur | e370d68 | 2019-10-01 16:49:30 +0100 | [diff] [blame] | 439 | /* Initializes options from commandline arguments. */ |
Nandana Dutt | 5fb117b | 2018-09-27 09:23:36 +0100 | [diff] [blame] | 440 | RunStatus Initialize(int argc, char* argv[]); |
| 441 | |
Nandana Dutt | 58d72e2 | 2018-11-16 10:30:48 +0000 | [diff] [blame] | 442 | /* Initializes options from the requested mode. */ |
Kean Mariotti | 306633e | 2022-09-05 16:30:47 +0000 | [diff] [blame] | 443 | void Initialize(BugreportMode bugreport_mode, int bugreport_flags, |
| 444 | const android::base::unique_fd& bugreport_fd, |
Paul Chang | f59c2b7 | 2020-03-10 02:08:55 +0800 | [diff] [blame] | 445 | const android::base::unique_fd& screenshot_fd, |
| 446 | bool is_screenshot_requested); |
Nandana Dutt | 58d72e2 | 2018-11-16 10:30:48 +0000 | [diff] [blame] | 447 | |
Nandana Dutt | 5fb117b | 2018-09-27 09:23:36 +0100 | [diff] [blame] | 448 | /* Returns true if the options set so far are consistent. */ |
| 449 | bool ValidateOptions() const; |
Nandana Dutt | 9a76d20 | 2019-01-21 15:56:48 +0000 | [diff] [blame] | 450 | |
mhasank | 2d75c44 | 2020-06-11 15:05:25 -0700 | [diff] [blame] | 451 | /* Returns if options specified require writing to custom file location */ |
| 452 | bool OutputToCustomFile() { |
| 453 | // Custom location is only honored in limited mode. |
mhasank | 3a4cfb4 | 2020-06-15 18:06:43 -0700 | [diff] [blame] | 454 | return limited_only && !out_dir.empty() && bugreport_fd.get() == -1; |
mhasank | 2d75c44 | 2020-06-11 15:05:25 -0700 | [diff] [blame] | 455 | } |
Nandana Dutt | 3f8c717 | 2018-09-25 12:01:54 +0100 | [diff] [blame] | 456 | }; |
| 457 | |
| 458 | // TODO: initialize fields on constructor |
Felipe Leme | e844a9d | 2016-09-21 15:01:39 -0700 | [diff] [blame] | 459 | // dumpstate id - unique after each device reboot. |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 460 | uint32_t id_; |
Felipe Leme | e844a9d | 2016-09-21 15:01:39 -0700 | [diff] [blame] | 461 | |
Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 462 | // dumpstate pid |
| 463 | pid_t pid_; |
| 464 | |
Nandana Dutt | 3f8c717 | 2018-09-25 12:01:54 +0100 | [diff] [blame] | 465 | // Runtime options. |
Nandana Dutt | 5fb117b | 2018-09-27 09:23:36 +0100 | [diff] [blame] | 466 | std::unique_ptr<DumpOptions> options_; |
Felipe Leme | e844a9d | 2016-09-21 15:01:39 -0700 | [diff] [blame] | 467 | |
Nandana Dutt | 402a839 | 2019-06-14 14:25:13 +0100 | [diff] [blame] | 468 | // Last progress that was sent to the listener [0-100]. |
| 469 | int last_reported_percent_progress_ = 0; |
Felipe Leme | 009ecbb | 2016-11-07 10:18:44 -0800 | [diff] [blame] | 470 | |
Felipe Leme | bbaf3c1 | 2016-10-11 14:32:25 -0700 | [diff] [blame] | 471 | // Whether it should take an screenshot earlier in the process. |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 472 | bool do_early_screenshot_ = false; |
Felipe Leme | bbaf3c1 | 2016-10-11 14:32:25 -0700 | [diff] [blame] | 473 | |
Primiano Tucci | faaaafb | 2021-01-14 12:26:29 +0000 | [diff] [blame] | 474 | // This is set to true when the trace snapshot request in the early call to |
| 475 | // MaybeSnapshotSystemTrace(). When this is true, the later stages of |
| 476 | // dumpstate will append the trace to the zip archive. |
| 477 | bool has_system_trace_ = false; |
| 478 | |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 479 | std::unique_ptr<Progress> progress_; |
Felipe Leme | e844a9d | 2016-09-21 15:01:39 -0700 | [diff] [blame] | 480 | |
Dieter Hsu | 105ad0c | 2020-09-29 15:23:33 +0800 | [diff] [blame] | 481 | // When set, defines a socket file-descriptor use to report progress to bugreportz |
| 482 | // or to stream the zipped file to. |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 483 | int control_socket_fd_ = -1; |
Felipe Leme | e844a9d | 2016-09-21 15:01:39 -0700 | [diff] [blame] | 484 | |
Felipe Leme | bbaf3c1 | 2016-10-11 14:32:25 -0700 | [diff] [blame] | 485 | // Bugreport format version; |
Felipe Leme | d071c68 | 2016-10-20 16:48:00 -0700 | [diff] [blame] | 486 | std::string version_ = VERSION_CURRENT; |
Felipe Leme | e844a9d | 2016-09-21 15:01:39 -0700 | [diff] [blame] | 487 | |
Felipe Leme | bbaf3c1 | 2016-10-11 14:32:25 -0700 | [diff] [blame] | 488 | time_t now_; |
| 489 | |
Felipe Leme | 2b9b06c | 2016-10-14 09:13:06 -0700 | [diff] [blame] | 490 | // Base name (without suffix or extensions) of the bugreport files, typically |
| 491 | // `bugreport-BUILD_ID`. |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 492 | std::string base_name_; |
Felipe Leme | bbaf3c1 | 2016-10-11 14:32:25 -0700 | [diff] [blame] | 493 | |
Dieter Hsu | 105ad0c | 2020-09-29 15:23:33 +0800 | [diff] [blame] | 494 | // Name is the suffix part of the bugreport files - it's typically the date, |
| 495 | // but it could be changed by the user.. |
Felipe Leme | 2b9b06c | 2016-10-14 09:13:06 -0700 | [diff] [blame] | 496 | std::string name_; |
| 497 | |
Nandana Dutt | 979388e | 2018-11-30 16:48:55 +0000 | [diff] [blame] | 498 | std::string bugreport_internal_dir_ = DUMPSTATE_DIRECTORY; |
| 499 | |
| 500 | // Full path of the temporary file containing the bugreport, inside bugreport_internal_dir_. |
| 501 | // At the very end this file is pulled into the zip file. |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 502 | std::string tmp_path_; |
Felipe Leme | 1d486fe | 2016-10-14 18:06:47 -0700 | [diff] [blame] | 503 | |
Nandana Dutt | 979388e | 2018-11-30 16:48:55 +0000 | [diff] [blame] | 504 | // Full path of the file containing the dumpstate logs, inside bugreport_internal_dir_. |
| 505 | // This is useful for debugging. |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 506 | std::string log_path_; |
Felipe Leme | 1d486fe | 2016-10-14 18:06:47 -0700 | [diff] [blame] | 507 | |
Chris Morin | bc22314 | 2022-02-04 14:17:11 -0800 | [diff] [blame] | 508 | // Full path of the bugreport zip file inside bugreport_internal_dir_. |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 509 | std::string path_; |
Felipe Leme | 1d486fe | 2016-10-14 18:06:47 -0700 | [diff] [blame] | 510 | |
Nandana Dutt | f531f4b | 2019-06-14 12:20:03 +0100 | [diff] [blame] | 511 | // Full path of the file containing the screenshot (when requested). |
Nandana Dutt | 979388e | 2018-11-30 16:48:55 +0000 | [diff] [blame] | 512 | std::string screenshot_path_; |
| 513 | |
Felipe Leme | 1d486fe | 2016-10-14 18:06:47 -0700 | [diff] [blame] | 514 | // Pointer to the zipped file. |
| 515 | std::unique_ptr<FILE, int (*)(FILE*)> zip_file{nullptr, fclose}; |
| 516 | |
Felipe Leme | c6bc8bc | 2016-10-27 15:58:27 -0700 | [diff] [blame] | 517 | // Pointer to the zip structure. |
| 518 | std::unique_ptr<ZipWriter> zip_writer_; |
| 519 | |
Vishnu Nair | 20cf503 | 2018-01-05 13:15:49 -0800 | [diff] [blame] | 520 | // Binder object listening to progress. |
Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 521 | android::sp<android::os::IDumpstateListener> listener_; |
Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 522 | |
Luis Hector Chavez | 91c2ae5 | 2018-03-14 15:12:46 -0700 | [diff] [blame] | 523 | // List of open tombstone dump files. |
| 524 | std::vector<DumpData> tombstone_data_; |
| 525 | |
| 526 | // List of open ANR dump files. |
| 527 | std::vector<DumpData> anr_data_; |
| 528 | |
Andy Hung | d62f7e6 | 2024-01-11 15:47:52 -0800 | [diff] [blame^] | 529 | // List of open Java traces files in the anr directory. |
| 530 | std::vector<DumpData> anr_trace_data_; |
| 531 | |
Woody Lin | 20767a9 | 2022-11-29 15:50:24 +0800 | [diff] [blame] | 532 | // List of open shutdown checkpoint files. |
| 533 | std::vector<DumpData> shutdown_checkpoints_; |
| 534 | |
Rhed Jao | 1c85512 | 2020-07-16 17:37:39 +0800 | [diff] [blame] | 535 | // A thread pool to execute dump tasks simultaneously if the parallel run is enabled. |
| 536 | std::unique_ptr<android::os::dumpstate::DumpPool> dump_pool_; |
| 537 | |
Rhed Jao | 4875aa6 | 2020-07-20 17:46:29 +0800 | [diff] [blame] | 538 | // A task queue to collect adding zip entry tasks inside dump tasks if the |
| 539 | // parallel run is enabled. |
| 540 | std::unique_ptr<android::os::dumpstate::TaskQueue> zip_entry_tasks_; |
| 541 | |
Nandana Dutt | d2f5f08 | 2019-01-18 17:13:52 +0000 | [diff] [blame] | 542 | // A callback to IncidentCompanion service, which checks user consent for sharing the |
| 543 | // bugreport with the calling app. If the user has not responded yet to the dialog it will |
| 544 | // be neither confirmed nor denied. |
| 545 | class ConsentCallback : public android::os::BnIncidentAuthListener { |
| 546 | public: |
| 547 | ConsentCallback(); |
| 548 | android::binder::Status onReportApproved() override; |
| 549 | android::binder::Status onReportDenied() override; |
| 550 | |
| 551 | enum ConsentResult { APPROVED, DENIED, UNAVAILABLE }; |
| 552 | |
| 553 | ConsentResult getResult(); |
| 554 | |
| 555 | // Returns the time since creating this listener |
| 556 | uint64_t getElapsedTimeMs() const; |
| 557 | |
| 558 | private: |
| 559 | ConsentResult result_; |
| 560 | uint64_t start_time_; |
| 561 | std::mutex lock_; |
| 562 | }; |
| 563 | |
Felipe Leme | e844a9d | 2016-09-21 15:01:39 -0700 | [diff] [blame] | 564 | private: |
Nandana Dutt | d2f5f08 | 2019-01-18 17:13:52 +0000 | [diff] [blame] | 565 | RunStatus RunInternal(int32_t calling_uid, const std::string& calling_package); |
Gavin Corkery | a44686c | 2022-11-23 18:16:51 +0000 | [diff] [blame] | 566 | RunStatus RetrieveInternal(int32_t calling_uid, const std::string& calling_package); |
Nandana Dutt | d2f5f08 | 2019-01-18 17:13:52 +0000 | [diff] [blame] | 567 | |
Jichao Li | e89d9c1 | 2019-11-21 19:02:51 -0800 | [diff] [blame] | 568 | RunStatus DumpstateDefaultAfterCritical(); |
Kean Mariotti | 306633e | 2022-09-05 16:30:47 +0000 | [diff] [blame] | 569 | RunStatus dumpstate(); |
Jichao Li | e89d9c1 | 2019-11-21 19:02:51 -0800 | [diff] [blame] | 570 | |
Paul Chang | 0d2aad7 | 2020-02-13 20:04:03 +0800 | [diff] [blame] | 571 | void MaybeTakeEarlyScreenshot(); |
Primiano Tucci | faaaafb | 2021-01-14 12:26:29 +0000 | [diff] [blame] | 572 | void MaybeSnapshotSystemTrace(); |
Kean Mariotti | 306633e | 2022-09-05 16:30:47 +0000 | [diff] [blame] | 573 | void MaybeSnapshotUiTraces(); |
| 574 | void MaybePostProcessUiTraces(); |
| 575 | void MaybeAddUiTracesToZip(); |
Paul Chang | 0d2aad7 | 2020-02-13 20:04:03 +0800 | [diff] [blame] | 576 | |
Paul Chang | 5702b48 | 2020-05-28 22:05:47 +0800 | [diff] [blame] | 577 | void onUiIntensiveBugreportDumpsFinished(int32_t calling_uid); |
Paul Chang | c490e66 | 2020-04-11 18:14:09 +0800 | [diff] [blame] | 578 | |
Jichao Li | e89d9c1 | 2019-11-21 19:02:51 -0800 | [diff] [blame] | 579 | void MaybeCheckUserConsent(int32_t calling_uid, const std::string& calling_package); |
Nandana Dutt | d2f5f08 | 2019-01-18 17:13:52 +0000 | [diff] [blame] | 580 | |
| 581 | // Removes the in progress files output files (tmp file, zip/txt file, screenshot), |
| 582 | // but leaves the log file alone. |
Nandana Dutt | 8ae16e6 | 2020-03-27 10:20:22 +0000 | [diff] [blame] | 583 | void CleanupTmpFiles(); |
Nandana Dutt | d2f5f08 | 2019-01-18 17:13:52 +0000 | [diff] [blame] | 584 | |
Rhed Jao | 1c85512 | 2020-07-16 17:37:39 +0800 | [diff] [blame] | 585 | // Create the thread pool to enable the parallel run function. |
| 586 | void EnableParallelRunIfNeeded(); |
| 587 | void ShutdownDumpPool(); |
| 588 | |
Nandana Dutt | d2f5f08 | 2019-01-18 17:13:52 +0000 | [diff] [blame] | 589 | RunStatus HandleUserConsentDenied(); |
| 590 | |
Gavin Corkery | a44686c | 2022-11-23 18:16:51 +0000 | [diff] [blame] | 591 | void HandleRunStatus(RunStatus status); |
| 592 | |
Abhijeet Kaur | 3172b53 | 2019-10-15 15:07:03 +0100 | [diff] [blame] | 593 | // Copies bugreport artifacts over to the caller's directories provided there is user consent or |
| 594 | // called by Shell. |
| 595 | RunStatus CopyBugreportIfUserConsented(int32_t calling_uid); |
Nandana Dutt | babf6c7 | 2019-01-15 14:11:12 +0000 | [diff] [blame] | 596 | |
Dieter Hsu | 105ad0c | 2020-09-29 15:23:33 +0800 | [diff] [blame] | 597 | std::function<int(const char *)> open_socket_fn_; |
| 598 | |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 599 | // Used by GetInstance() only. |
Chih-Hung Hsieh | d0937e6 | 2018-12-20 15:43:57 -0800 | [diff] [blame] | 600 | explicit Dumpstate(const std::string& version = VERSION_CURRENT); |
Felipe Leme | 062926e | 2016-10-27 15:51:12 -0700 | [diff] [blame] | 601 | |
Nandana Dutt | d2f5f08 | 2019-01-18 17:13:52 +0000 | [diff] [blame] | 602 | android::sp<ConsentCallback> consent_callback_; |
| 603 | |
Rhed Jao | 2cc4eec | 2020-07-21 15:42:55 +0800 | [diff] [blame] | 604 | std::recursive_mutex mutex_; |
| 605 | |
Felipe Leme | 062926e | 2016-10-27 15:51:12 -0700 | [diff] [blame] | 606 | DISALLOW_COPY_AND_ASSIGN(Dumpstate); |
Felipe Leme | e844a9d | 2016-09-21 15:01:39 -0700 | [diff] [blame] | 607 | }; |
| 608 | |
| 609 | // for_each_pid_func = void (*)(int, const char*); |
| 610 | // for_each_tid_func = void (*)(int, int, const char*); |
| 611 | |
| 612 | typedef void(for_each_pid_func)(int, const char*); |
| 613 | typedef void(for_each_tid_func)(int, int, const char*); |
Felipe Leme | 71ca15e | 2016-05-19 16:18:17 -0700 | [diff] [blame] | 614 | |
Felipe Leme | 71a74ac | 2016-03-17 15:43:25 -0700 | [diff] [blame] | 615 | /* saves the the contents of a file as a long */ |
| 616 | int read_file_as_long(const char *path, long int *output); |
| 617 | |
Christopher Ferris | 54bcc5f | 2015-02-10 12:15:01 -0800 | [diff] [blame] | 618 | /* prints the contents of the fd |
| 619 | * fd must have been opened with the flag O_NONBLOCK. |
| 620 | */ |
Christopher Ferris | 1fe6107 | 2014-07-22 16:08:19 -0700 | [diff] [blame] | 621 | 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] | 622 | |
Mark Salyzyn | 326842f | 2015-04-30 09:49:41 -0700 | [diff] [blame] | 623 | /* calls skip to gate calling dump_from_fd recursively |
| 624 | * in the specified directory. dump_from_fd defaults to |
| 625 | * dump_file_from_fd above when set to NULL. skip defaults |
| 626 | * to false when set to NULL. dump_from_fd will always be |
| 627 | * called with title NULL. |
| 628 | */ |
Felipe Leme | 678727a | 2016-09-21 17:22:11 -0700 | [diff] [blame] | 629 | int dump_files(const std::string& title, const char* dir, bool (*skip)(const char* path), |
| 630 | int (*dump_from_fd)(const char* title, const char* path, int fd)); |
Mark Salyzyn | 326842f | 2015-04-30 09:49:41 -0700 | [diff] [blame] | 631 | |
Nandana Dutt | a344cb6 | 2019-02-22 15:12:35 +0000 | [diff] [blame] | 632 | /* |
| 633 | * Redirects 'redirect' to a file indicated by 'path', truncating it. |
| 634 | * |
| 635 | * Returns true if redirect succeeds. |
| 636 | */ |
| 637 | bool redirect_to_file(FILE* redirect, char* path); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 638 | |
Nandana Dutt | a344cb6 | 2019-02-22 15:12:35 +0000 | [diff] [blame] | 639 | /* |
| 640 | * Redirects 'redirect' to an existing file indicated by 'path', appending it. |
| 641 | * |
| 642 | * Returns true if redirect succeeds. |
| 643 | */ |
| 644 | bool redirect_to_existing_file(FILE* redirect, char* path); |
Felipe Leme | 0f3fb20 | 2016-06-10 17:10:53 -0700 | [diff] [blame] | 645 | |
Felipe Leme | 111b9d0 | 2016-02-03 09:28:24 -0800 | [diff] [blame] | 646 | /* create leading directories, if necessary */ |
| 647 | void create_parent_dirs(const char *path); |
| 648 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 649 | /* for each process in the system, run the specified function */ |
Colin Cross | 0c22e8b | 2012-11-02 15:46:56 -0700 | [diff] [blame] | 650 | void for_each_pid(for_each_pid_func func, const char *header); |
| 651 | |
| 652 | /* for each thread in the system, run the specified function */ |
| 653 | void for_each_tid(for_each_tid_func func, const char *header); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 654 | |
| 655 | /* Displays a blocked processes in-kernel wait channel */ |
Colin Cross | 0c22e8b | 2012-11-02 15:46:56 -0700 | [diff] [blame] | 656 | void show_wchan(int pid, int tid, const char *name); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 657 | |
Mark Salyzyn | a297c32 | 2016-02-05 15:33:17 -0800 | [diff] [blame] | 658 | /* Displays a processes times */ |
| 659 | void show_showtime(int pid, const char *name); |
| 660 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 661 | /* Gets the dmesg output for the kernel */ |
| 662 | void do_dmesg(); |
| 663 | |
Sreeram Ramachandran | 2b3bba3 | 2014-07-08 15:40:55 -0700 | [diff] [blame] | 664 | /* Prints the contents of all the routing tables, both IPv4 and IPv6. */ |
| 665 | void dump_route_tables(); |
| 666 | |
Li Li | 830179f | 2022-01-04 12:53:29 -0800 | [diff] [blame] | 667 | /* Dump subdirectories of cgroupfs if the corresponding process is frozen */ |
| 668 | void dump_frozen_cgroupfs(); |
| 669 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 670 | /* Play a sound via Stagefright */ |
Christopher Ferris | 1fe6107 | 2014-07-22 16:08:19 -0700 | [diff] [blame] | 671 | void play_sound(const char *path); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 672 | |
Felipe Leme | 0c80cf0 | 2016-01-05 13:25:34 -0800 | [diff] [blame] | 673 | /* Checks if a given path is a directory. */ |
| 674 | bool is_dir(const char* pathname); |
| 675 | |
| 676 | /** Gets the last modification time of a file, or default time if file is not found. */ |
| 677 | time_t get_mtime(int fd, time_t default_mtime); |
| 678 | |
Calvin On | 249beee | 2016-06-03 15:17:07 -0700 | [diff] [blame] | 679 | /** Gets command-line arguments. */ |
Felipe Leme | a34efb7 | 2016-03-11 09:33:32 -0800 | [diff] [blame] | 680 | void format_args(int argc, const char *argv[], std::string *args); |
Felipe Leme | 88c7933 | 2016-02-22 11:06:49 -0800 | [diff] [blame] | 681 | |
Vishnu Nair | 20cf503 | 2018-01-05 13:15:49 -0800 | [diff] [blame] | 682 | /** Main entry point for dumpstate. */ |
| 683 | int run_main(int argc, char* argv[]); |
| 684 | |
Felipe Leme | 8620bb4 | 2015-11-10 11:04:45 -0800 | [diff] [blame] | 685 | #ifdef __cplusplus |
| 686 | } |
| 687 | #endif |
| 688 | |
Felipe Leme | 8268ed2 | 2016-08-02 18:18:25 -0700 | [diff] [blame] | 689 | #endif /* FRAMEWORK_NATIVE_CMD_DUMPSTATE_H_ */ |