blob: d400dc7127dd3a0d75ac98b65460c3d9e071dd8e [file] [log] [blame]
Colin Crossf45fa6b2012-03-26 12:38:26 -07001/*
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 Leme8268ed22016-08-02 18:18:25 -070017#ifndef FRAMEWORK_NATIVE_CMD_DUMPSTATE_H_
18#define FRAMEWORK_NATIVE_CMD_DUMPSTATE_H_
Colin Crossf45fa6b2012-03-26 12:38:26 -070019
20#include <time.h>
21#include <unistd.h>
Colin Cross0c22e8b2012-11-02 15:46:56 -070022#include <stdbool.h>
Colin Crossf45fa6b2012-03-26 12:38:26 -070023#include <stdio.h>
Felipe Leme30dbfa12016-09-02 12:43:26 -070024
25#include <string>
Felipe Leme36b3f6f2015-11-19 15:41:04 -080026#include <vector>
Colin Crossf45fa6b2012-03-26 12:38:26 -070027
Felipe Lemec6bc8bc2016-10-27 15:58:27 -070028#include <android-base/macros.h>
Luis Hector Chavez91c2ae52018-03-14 15:12:46 -070029#include <android-base/unique_fd.h>
Hunter Knepshield8540faf2020-02-04 19:47:20 -080030#include <android/hardware/dumpstate/1.1/types.h>
Nandana Duttd2f5f082019-01-18 17:13:52 +000031#include <android/os/BnIncidentAuthListener.h>
Nandana Dutt197661d2018-11-16 16:40:21 +000032#include <android/os/IDumpstate.h>
Jiyong Parkb22e65d2017-06-23 21:23:16 +090033#include <android/os/IDumpstateListener.h>
34#include <utils/StrongPointer.h>
Felipe Lemec6bc8bc2016-10-27 15:58:27 -070035#include <ziparchive/zip_writer.h>
36
Felipe Lemebda15a02016-11-16 17:48:25 -080037#include "DumpstateUtil.h"
Rhed Jao1c855122020-07-16 17:37:39 +080038#include "DumpPool.h"
Felipe Leme75876a22016-10-27 16:31:27 -070039
Felipe Leme35c94f32016-08-12 10:51:54 -070040// Workaround for const char *args[MAX_ARGS_ARRAY_SIZE] variables until they're converted to
41// std::vector<std::string>
Felipe Leme30dbfa12016-09-02 12:43:26 -070042// TODO: remove once not used
Felipe Leme35c94f32016-08-12 10:51:54 -070043#define MAX_ARGS_ARRAY_SIZE 1000
44
Felipe Leme47e9be22016-12-21 15:37:07 -080045// TODO: move everything under this namespace
46// TODO: and then remove explicitly android::os::dumpstate:: prefixes
47namespace android {
48namespace os {
Nandana Dutt58d72e22018-11-16 10:30:48 +000049
50struct DumpstateOptions;
51
Felipe Leme47e9be22016-12-21 15:37:07 -080052namespace dumpstate {
53
54class DumpstateTest;
55class ProgressTest;
56
57} // namespace dumpstate
58} // namespace os
59} // namespace android
60
Jiyong Parkb22e65d2017-06-23 21:23:16 +090061class ZipWriter;
62
Felipe Leme30dbfa12016-09-02 12:43:26 -070063// TODO: remove once moved to HAL
Felipe Leme8620bb42015-11-10 11:04:45 -080064#ifdef __cplusplus
65extern "C" {
66#endif
67
Felipe Leme30dbfa12016-09-02 12:43:26 -070068/*
Felipe Leme30dbfa12016-09-02 12:43:26 -070069 * Helper class used to report how long it takes for a section to finish.
70 *
71 * Typical usage:
72 *
73 * DurationReporter duration_reporter(title);
74 *
75 */
76class DurationReporter {
77 public:
Nandana Dutt8d945c02019-08-14 13:30:07 +010078 explicit DurationReporter(const std::string& title, bool logcat_only = false,
Rhed Jao1c855122020-07-16 17:37:39 +080079 bool verbose = false, int duration_fd = STDOUT_FILENO);
Felipe Leme30dbfa12016-09-02 12:43:26 -070080
81 ~DurationReporter();
82
Felipe Leme30dbfa12016-09-02 12:43:26 -070083 private:
Felipe Leme678727a2016-09-21 17:22:11 -070084 std::string title_;
Nandana Dutta8470b82019-03-11 11:00:58 +000085 bool logcat_only_;
Nandana Dutt8d945c02019-08-14 13:30:07 +010086 bool verbose_;
Felipe Lemeb0f669d2016-09-26 18:26:11 -070087 uint64_t started_;
Rhed Jao1c855122020-07-16 17:37:39 +080088 int duration_fd_;
Felipe Leme062926e2016-10-27 15:51:12 -070089
90 DISALLOW_COPY_AND_ASSIGN(DurationReporter);
Felipe Leme30dbfa12016-09-02 12:43:26 -070091};
92
93/*
Felipe Leme7447d7c2016-11-03 18:12:22 -070094 * Keeps track of current progress and estimated max, saving stats on file to tune up future runs.
Felipe Leme71bbfc52015-11-23 14:14:51 -080095 *
Felipe Leme7447d7c2016-11-03 18:12:22 -070096 * Each `dumpstate` section contributes to the total weight by an individual weight, so the overall
97 * progress can be calculated by dividing the estimate max progress by the current progress.
Felipe Leme71bbfc52015-11-23 14:14:51 -080098 *
Felipe Leme7447d7c2016-11-03 18:12:22 -070099 * The estimated max progress is initially set to a value (`kDefaultMax) defined empirically, but
100 * it's adjusted after each dumpstate run by storing the average duration in a file.
Felipe Lemead5f6c42015-11-30 14:26:46 -0800101 *
Felipe Leme71bbfc52015-11-23 14:14:51 -0800102 */
Felipe Leme7447d7c2016-11-03 18:12:22 -0700103class Progress {
Felipe Leme47e9be22016-12-21 15:37:07 -0800104 friend class android::os::dumpstate::ProgressTest;
105 friend class android::os::dumpstate::DumpstateTest;
Felipe Leme7447d7c2016-11-03 18:12:22 -0700106
107 public:
108 /*
109 * Default estimation of the max duration of a bugreport generation.
110 *
111 * It does not need to match the exact sum of all sections, but ideally it should to be slight
112 * more than such sum: a value too high will cause the bugreport to finish before the user
113 * expected (for example, jumping from 70% to 100%), while a value too low will cause the
114 * progress to get stuck at an almost-finished value (like 99%) for a while.
115 *
116 * This constant is only used when the average duration from previous runs cannot be used.
117 */
118 static const int kDefaultMax;
119
Chih-Hung Hsiehd0937e62018-12-20 15:43:57 -0800120 explicit Progress(const std::string& path = "");
Felipe Leme7447d7c2016-11-03 18:12:22 -0700121
122 // Gets the current progress.
123 int32_t Get() const;
124
125 // Gets the current estimated max progress.
126 int32_t GetMax() const;
127
128 // Gets the initial estimated max progress.
129 int32_t GetInitialMax() const;
130
131 // Increments progress (ignored if not positive).
132 // Returns `true` if the max progress increased as well.
133 bool Inc(int32_t delta);
134
135 // Persist the stats.
136 void Save();
137
138 void Dump(int fd, const std::string& prefix) const;
139
140 private:
141 Progress(int32_t initial_max, float growth_factor,
142 const std::string& path = ""); // Used by test cases.
143 Progress(int32_t initial_max, int32_t progress, float growth_factor); // Used by test cases.
144 void Load();
145 int32_t initial_max_;
146 int32_t progress_;
147 int32_t max_;
148 float growth_factor_;
149 int32_t n_runs_;
150 int32_t average_max_;
Nandana Dutt979388e2018-11-30 16:48:55 +0000151 std::string path_;
Felipe Leme7447d7c2016-11-03 18:12:22 -0700152};
Felipe Leme71bbfc52015-11-23 14:14:51 -0800153
Felipe Leme71bbfc52015-11-23 14:14:51 -0800154/*
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700155 * List of supported zip format versions.
156 *
157 * See bugreport-format.md for more info.
158 */
Vishnu Nair64afc022018-02-01 15:29:34 -0800159static std::string VERSION_CURRENT = "2.0";
Felipe Lemed071c682016-10-20 16:48:00 -0700160
161/*
Felipe Lemee184f662016-10-27 10:04:47 -0700162 * Temporary version that adds a anr-traces.txt entry. Once tools support it, the current version
Vishnu Nair64afc022018-02-01 15:29:34 -0800163 * will be bumped to 3.0.
Felipe Lemee184f662016-10-27 10:04:47 -0700164 */
Vishnu Nair64afc022018-02-01 15:29:34 -0800165static std::string VERSION_SPLIT_ANR = "3.0-dev-split-anr";
Felipe Lemee184f662016-10-27 10:04:47 -0700166
167/*
Felipe Lemed071c682016-10-20 16:48:00 -0700168 * "Alias" for the current version.
169 */
170static std::string VERSION_DEFAULT = "default";
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700171
172/*
Nandana Dutt979388e2018-11-30 16:48:55 +0000173 * Directory used by Dumpstate binary to keep its local files.
174 */
175static const std::string DUMPSTATE_DIRECTORY = "/bugreports";
176
177/*
Luis Hector Chavez91c2ae52018-03-14 15:12:46 -0700178 * Structure that contains the information of an open dump file.
179 */
180struct DumpData {
181 // Path of the file.
182 std::string name;
183
184 // Open file descriptor for the file.
185 android::base::unique_fd fd;
186
187 // Modification time of the file.
188 time_t mtime;
189};
190
191/*
Felipe Lemee844a9d2016-09-21 15:01:39 -0700192 * Main class driving a bugreport generation.
193 *
194 * Currently, it only contains variables that are accessed externally, but gradually the functions
195 * that are spread accross utils.cpp and dumpstate.cpp will be moved to it.
196 */
197class Dumpstate {
Rhed Jao1c855122020-07-16 17:37:39 +0800198 friend class android::os::dumpstate::DumpstateTest;
Felipe Leme4c2d6632016-09-28 14:32:00 -0700199
Felipe Lemee844a9d2016-09-21 15:01:39 -0700200 public:
Nandana Duttd2f5f082019-01-18 17:13:52 +0000201 enum RunStatus { OK, HELP, INVALID_INPUT, ERROR, USER_CONSENT_DENIED, USER_CONSENT_TIMED_OUT };
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100202
Nandana Dutt58d72e22018-11-16 10:30:48 +0000203 // The mode under which the bugreport should be run. Each mode encapsulates a few options.
204 enum BugreportMode {
Nandana Dutt197661d2018-11-16 16:40:21 +0000205 BUGREPORT_FULL = android::os::IDumpstate::BUGREPORT_MODE_FULL,
206 BUGREPORT_INTERACTIVE = android::os::IDumpstate::BUGREPORT_MODE_INTERACTIVE,
207 BUGREPORT_REMOTE = android::os::IDumpstate::BUGREPORT_MODE_REMOTE,
208 BUGREPORT_WEAR = android::os::IDumpstate::BUGREPORT_MODE_WEAR,
209 BUGREPORT_TELEPHONY = android::os::IDumpstate::BUGREPORT_MODE_TELEPHONY,
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000210 BUGREPORT_WIFI = android::os::IDumpstate::BUGREPORT_MODE_WIFI,
211 BUGREPORT_DEFAULT = android::os::IDumpstate::BUGREPORT_MODE_DEFAULT
Nandana Dutt58d72e22018-11-16 10:30:48 +0000212 };
213
Felipe Leme47e9be22016-12-21 15:37:07 -0800214 static android::os::dumpstate::CommandOptions DEFAULT_DUMPSYS;
Felipe Lemebda15a02016-11-16 17:48:25 -0800215
Felipe Lemee844a9d2016-09-21 15:01:39 -0700216 static Dumpstate& GetInstance();
Felipe Leme71bbfc52015-11-23 14:14:51 -0800217
Felipe Leme1d486fe2016-10-14 18:06:47 -0700218 /* Checkes whether dumpstate is generating a zipped bugreport. */
219 bool IsZipping() const;
220
Abhijeet Kaura407fb82020-03-27 12:51:12 +0000221 /* Initialize dumpstate fields before starting bugreport generation */
222 void Initialize();
223
Felipe Leme6ad9c062016-09-28 11:58:36 -0700224 /*
Felipe Leme678727a2016-09-21 17:22:11 -0700225 * Forks a command, waits for it to finish, and returns its status.
226 *
227 * |title| description of the command printed on `stdout` (or empty to skip
228 * description).
229 * |full_command| array containing the command (first entry) and its arguments.
230 * Must contain at least one element.
231 * |options| optional argument defining the command's behavior.
232 */
233 int RunCommand(const std::string& title, const std::vector<std::string>& fullCommand,
Felipe Leme47e9be22016-12-21 15:37:07 -0800234 const android::os::dumpstate::CommandOptions& options =
Nandana Dutt8d945c02019-08-14 13:30:07 +0100235 android::os::dumpstate::CommandOptions::DEFAULT,
236 bool verbose_duration = false);
Felipe Leme678727a2016-09-21 17:22:11 -0700237
238 /*
239 * Runs `dumpsys` with the given arguments, automatically setting its timeout
Vishnu Nair6921f802017-11-22 09:17:23 -0800240 * (`-T` argument)
Felipe Leme678727a2016-09-21 17:22:11 -0700241 * 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 Nair6921f802017-11-22 09:17:23 -0800247 * |dumpsys_timeout| when > 0, defines the value passed to `dumpsys -T` (otherwise it uses the
Felipe Leme6ad9c062016-09-28 11:58:36 -0700248 * timeout from `options`)
Felipe Leme678727a2016-09-21 17:22:11 -0700249 */
Felipe Leme9a523ae2016-10-20 15:10:33 -0700250 void RunDumpsys(const std::string& title, const std::vector<std::string>& dumpsys_args,
Felipe Leme47e9be22016-12-21 15:37:07 -0800251 const android::os::dumpstate::CommandOptions& options = DEFAULT_DUMPSYS,
Vishnu Nair6921f802017-11-22 09:17:23 -0800252 long dumpsys_timeout_ms = 0);
Felipe Leme678727a2016-09-21 17:22:11 -0700253
254 /*
255 * Prints the contents of a file.
256 *
257 * |title| description of the command printed on `stdout` (or empty to skip
258 * description).
259 * |path| location of the file to be dumped.
260 */
261 int DumpFile(const std::string& title, const std::string& path);
262
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700263 /*
Felipe Leme1d486fe2016-10-14 18:06:47 -0700264 * Adds a new entry to the existing zip file.
265 * */
266 bool AddZipEntry(const std::string& entry_name, const std::string& entry_path);
267
268 /*
269 * Adds a new entry to the existing zip file.
Vishnu Naire97d6122018-01-18 13:58:56 -0800270 *
271 * |entry_name| destination path of the new entry.
272 * |fd| file descriptor to read from.
273 * |timeout| timeout to terminate the read if not completed. Set
274 * value of 0s (default) to disable timeout.
Felipe Leme1d486fe2016-10-14 18:06:47 -0700275 */
Vishnu Naire97d6122018-01-18 13:58:56 -0800276 android::status_t AddZipEntryFromFd(const std::string& entry_name, int fd,
277 std::chrono::milliseconds timeout);
Felipe Leme1d486fe2016-10-14 18:06:47 -0700278
279 /*
Nandana Dutta7db6342018-11-21 14:53:34 +0000280 * Adds a text entry to the existing zip file.
Felipe Leme1d486fe2016-10-14 18:06:47 -0700281 */
282 bool AddTextZipEntry(const std::string& entry_name, const std::string& content);
283
284 /*
285 * Adds all files from a directory to the zipped bugreport file.
286 */
287 void AddDir(const std::string& dir, bool recursive);
288
289 /*
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700290 * Takes a screenshot and save it to the given `path`.
291 *
292 * If `path` is empty, uses a standard path based on the bugreport name.
293 */
294 void TakeScreenshot(const std::string& path = "");
295
Felipe Leme7447d7c2016-11-03 18:12:22 -0700296 /////////////////////////////////////////////////////////////////////
297 // TODO: members below should be private once refactor is finished //
298 /////////////////////////////////////////////////////////////////////
299
300 // TODO: temporary method until Dumpstate object is properly set
301 void SetProgress(std::unique_ptr<Progress> progress);
Felipe Lemed80e6b62016-10-03 13:08:14 -0700302
Nandana Duttcf419a72019-03-14 10:40:17 +0000303 // Dumps Dalvik and native stack traces, sets the trace file location to path
304 // if it succeeded.
305 // Note that it returns early if user consent is denied with status USER_CONSENT_DENIED.
306 // Returns OK in all other cases.
307 RunStatus DumpTraces(const char** path);
Nandana Duttfaafd522019-03-11 09:23:09 +0000308
Felipe Leme6f674ae2016-11-18 17:10:33 -0800309 void DumpstateBoard();
310
Felipe Lemed80e6b62016-10-03 13:08:14 -0700311 /*
312 * Updates the overall progress of the bugreport generation by the given weight increment.
313 */
Felipe Leme7447d7c2016-11-03 18:12:22 -0700314 void UpdateProgress(int32_t delta);
Felipe Lemed80e6b62016-10-03 13:08:14 -0700315
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700316 /* Prints the dumpstate header on `stdout`. */
Felipe Leme2b9b06c2016-10-14 09:13:06 -0700317 void PrintHeader() const;
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700318
Felipe Leme1d486fe2016-10-14 18:06:47 -0700319 /*
320 * Adds the temporary report to the existing .zip file, closes the .zip file, and removes the
321 * temporary file.
322 */
323 bool FinishZipFile();
324
Nandana Dutt979388e2018-11-30 16:48:55 +0000325 /* Constructs a full path inside directory with file name formatted using the given suffix. */
326 std::string GetPath(const std::string& directory, const std::string& suffix) const;
327
328 /* Constructs a full path inside bugreport_internal_dir_ with file name formatted using the
329 * given suffix. */
Felipe Leme2b9b06c2016-10-14 09:13:06 -0700330 std::string GetPath(const std::string& suffix) const;
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700331
Vishnu Nair780b1282017-10-10 13:57:24 -0700332 /* Returns true if the current version supports priority dump feature. */
333 bool CurrentVersionSupportsPriorityDumps() const;
334
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100335 struct DumpOptions;
Felipe Lemee844a9d2016-09-21 15:01:39 -0700336
Abhijeet Kaura407fb82020-03-27 12:51:12 +0000337 /*
338 * Main entry point for running a complete bugreport.
339 *
340 * Initialize() dumpstate before calling this method.
341 *
342 */
Nandana Duttd2f5f082019-01-18 17:13:52 +0000343 RunStatus Run(int32_t calling_uid, const std::string& calling_package);
Nandana Dutt197661d2018-11-16 16:40:21 +0000344
Nandana Duttf02564e2019-02-15 15:24:24 +0000345 RunStatus ParseCommandlineAndRun(int argc, char* argv[]);
346
Nandana Dutt8ae16e62020-03-27 10:20:22 +0000347 /* Deletes in-progress files */
348 void Cancel();
349
Nandana Dutt197661d2018-11-16 16:40:21 +0000350 /* Sets runtime options. */
351 void SetOptions(std::unique_ptr<DumpOptions> options);
Nandana Dutt3f8c7172018-09-25 12:01:54 +0100352
Nandana Dutt3f8c7172018-09-25 12:01:54 +0100353 /*
Nandana Duttbbdb5b42019-03-12 10:52:56 +0000354 * Returns true if user consent is necessary and has been denied.
355 * Consent is only necessary if the caller has asked to copy over the bugreport to a file they
356 * provided.
357 */
358 bool IsUserConsentDenied() const;
359
360 /*
Abhijeet Kaur359b1ff2019-07-26 16:01:36 +0100361 * Returns true if dumpstate is called by bugreporting API
362 */
363 bool CalledByApi() const;
364
365 /*
Nandana Dutt3f8c7172018-09-25 12:01:54 +0100366 * Structure to hold options that determine the behavior of dumpstate.
367 */
368 struct DumpOptions {
369 bool do_add_date = false;
370 bool do_zip_file = false;
371 bool do_vibrate = true;
Nandana Dutt9a76d202019-01-21 15:56:48 +0000372 // Writes bugreport content to a socket; only flatfile format is supported.
Nandana Dutt3f8c7172018-09-25 12:01:54 +0100373 bool use_socket = false;
374 bool use_control_socket = false;
Paul Chang0d2aad72020-02-13 20:04:03 +0800375 bool do_screenshot = false;
376 bool is_screenshot_copied = false;
Nandana Dutt3f8c7172018-09-25 12:01:54 +0100377 bool is_remote_mode = false;
378 bool show_header_only = false;
379 bool do_start_service = false;
380 bool telephony_only = false;
381 bool wifi_only = false;
mhasankd451a472020-05-26 18:02:39 -0700382 // Trimmed-down version of dumpstate to only include whitelisted logs.
383 bool limited_only = false;
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100384 // Whether progress updates should be published.
385 bool do_progress_updates = false;
Hunter Knepshield8540faf2020-02-04 19:47:20 -0800386 // The mode we'll use when calling IDumpstateDevice::dumpstateBoard.
387 // TODO(b/148168577) get rid of the AIDL values, replace them with the HAL values instead.
388 // The HAL is actually an API surface that can be validated, while the AIDL is not (@hide).
389 ::android::hardware::dumpstate::V1_1::DumpstateMode dumpstate_hal_mode =
390 ::android::hardware::dumpstate::V1_1::DumpstateMode::DEFAULT;
mhasank3a4cfb42020-06-15 18:06:43 -0700391 // File descriptor to output zip file. Takes precedence over out_dir.
Nandana Dutt54dbd672019-01-11 12:58:05 +0000392 android::base::unique_fd bugreport_fd;
393 // File descriptor to screenshot file.
Nandana Dutt54dbd672019-01-11 12:58:05 +0000394 android::base::unique_fd screenshot_fd;
mhasank3a4cfb42020-06-15 18:06:43 -0700395 // Custom output directory.
396 std::string out_dir;
Abhijeet Kaure370d682019-10-01 16:49:30 +0100397 // Bugreport mode of the bugreport.
398 std::string bugreport_mode;
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100399 // Command-line arguments as string
400 std::string args;
401 // Notification title and description
402 std::string notification_title;
403 std::string notification_description;
404
Abhijeet Kaure370d682019-10-01 16:49:30 +0100405 /* Initializes options from commandline arguments. */
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100406 RunStatus Initialize(int argc, char* argv[]);
407
Nandana Dutt58d72e22018-11-16 10:30:48 +0000408 /* Initializes options from the requested mode. */
Nandana Dutt54dbd672019-01-11 12:58:05 +0000409 void Initialize(BugreportMode bugreport_mode, const android::base::unique_fd& bugreport_fd,
Paul Changf59c2b72020-03-10 02:08:55 +0800410 const android::base::unique_fd& screenshot_fd,
411 bool is_screenshot_requested);
Nandana Dutt58d72e22018-11-16 10:30:48 +0000412
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100413 /* Returns true if the options set so far are consistent. */
414 bool ValidateOptions() const;
Nandana Dutt9a76d202019-01-21 15:56:48 +0000415
416 /* Returns if options specified require writing bugreport to a file */
417 bool OutputToFile() const {
418 // If we are not writing to socket, we will write to a file. If bugreport_fd is
419 // specified, it is preferred. If not bugreport is written to /bugreports.
420 return !use_socket;
421 }
mhasank2d75c442020-06-11 15:05:25 -0700422
423 /* Returns if options specified require writing to custom file location */
424 bool OutputToCustomFile() {
425 // Custom location is only honored in limited mode.
mhasank3a4cfb42020-06-15 18:06:43 -0700426 return limited_only && !out_dir.empty() && bugreport_fd.get() == -1;
mhasank2d75c442020-06-11 15:05:25 -0700427 }
Nandana Dutt3f8c7172018-09-25 12:01:54 +0100428 };
429
430 // TODO: initialize fields on constructor
Felipe Lemee844a9d2016-09-21 15:01:39 -0700431 // dumpstate id - unique after each device reboot.
Felipe Leme7447d7c2016-11-03 18:12:22 -0700432 uint32_t id_;
Felipe Lemee844a9d2016-09-21 15:01:39 -0700433
Felipe Leme75876a22016-10-27 16:31:27 -0700434 // dumpstate pid
435 pid_t pid_;
436
Nandana Dutt3f8c7172018-09-25 12:01:54 +0100437 // Runtime options.
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100438 std::unique_ptr<DumpOptions> options_;
Felipe Lemee844a9d2016-09-21 15:01:39 -0700439
Nandana Dutt402a8392019-06-14 14:25:13 +0100440 // Last progress that was sent to the listener [0-100].
441 int last_reported_percent_progress_ = 0;
Felipe Leme009ecbb2016-11-07 10:18:44 -0800442
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700443 // Whether it should take an screenshot earlier in the process.
Felipe Leme9a523ae2016-10-20 15:10:33 -0700444 bool do_early_screenshot_ = false;
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700445
Felipe Leme7447d7c2016-11-03 18:12:22 -0700446 std::unique_ptr<Progress> progress_;
Felipe Lemee844a9d2016-09-21 15:01:39 -0700447
448 // When set, defines a socket file-descriptor use to report progress to bugreportz.
Felipe Leme9a523ae2016-10-20 15:10:33 -0700449 int control_socket_fd_ = -1;
Felipe Lemee844a9d2016-09-21 15:01:39 -0700450
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700451 // Bugreport format version;
Felipe Lemed071c682016-10-20 16:48:00 -0700452 std::string version_ = VERSION_CURRENT;
Felipe Lemee844a9d2016-09-21 15:01:39 -0700453
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700454 time_t now_;
455
Felipe Leme2b9b06c2016-10-14 09:13:06 -0700456 // Base name (without suffix or extensions) of the bugreport files, typically
457 // `bugreport-BUILD_ID`.
Felipe Leme9a523ae2016-10-20 15:10:33 -0700458 std::string base_name_;
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700459
Felipe Leme2b9b06c2016-10-14 09:13:06 -0700460 // Name is the suffix part of the bugreport files - it's typically the date (when invoked with
461 // `-d`), but it could be changed by the user..
462 std::string name_;
463
Nandana Dutt979388e2018-11-30 16:48:55 +0000464 std::string bugreport_internal_dir_ = DUMPSTATE_DIRECTORY;
465
466 // Full path of the temporary file containing the bugreport, inside bugreport_internal_dir_.
467 // At the very end this file is pulled into the zip file.
Felipe Leme9a523ae2016-10-20 15:10:33 -0700468 std::string tmp_path_;
Felipe Leme1d486fe2016-10-14 18:06:47 -0700469
Nandana Dutt979388e2018-11-30 16:48:55 +0000470 // Full path of the file containing the dumpstate logs, inside bugreport_internal_dir_.
471 // This is useful for debugging.
Felipe Leme9a523ae2016-10-20 15:10:33 -0700472 std::string log_path_;
Felipe Leme1d486fe2016-10-14 18:06:47 -0700473
Nandana Dutt979388e2018-11-30 16:48:55 +0000474 // Full path of the bugreport file, be it zip or text, inside bugreport_internal_dir_.
Felipe Leme9a523ae2016-10-20 15:10:33 -0700475 std::string path_;
Felipe Leme1d486fe2016-10-14 18:06:47 -0700476
Nandana Duttf531f4b2019-06-14 12:20:03 +0100477 // Full path of the file containing the screenshot (when requested).
Nandana Dutt979388e2018-11-30 16:48:55 +0000478 std::string screenshot_path_;
479
Felipe Leme1d486fe2016-10-14 18:06:47 -0700480 // Pointer to the zipped file.
481 std::unique_ptr<FILE, int (*)(FILE*)> zip_file{nullptr, fclose};
482
Felipe Lemec6bc8bc2016-10-27 15:58:27 -0700483 // Pointer to the zip structure.
484 std::unique_ptr<ZipWriter> zip_writer_;
485
Vishnu Nair20cf5032018-01-05 13:15:49 -0800486 // Binder object listening to progress.
Felipe Leme75876a22016-10-27 16:31:27 -0700487 android::sp<android::os::IDumpstateListener> listener_;
Felipe Leme75876a22016-10-27 16:31:27 -0700488
Luis Hector Chavez91c2ae52018-03-14 15:12:46 -0700489 // List of open tombstone dump files.
490 std::vector<DumpData> tombstone_data_;
491
492 // List of open ANR dump files.
493 std::vector<DumpData> anr_data_;
494
Rhed Jao1c855122020-07-16 17:37:39 +0800495 // A thread pool to execute dump tasks simultaneously if the parallel run is enabled.
496 std::unique_ptr<android::os::dumpstate::DumpPool> dump_pool_;
497
Nandana Duttd2f5f082019-01-18 17:13:52 +0000498 // A callback to IncidentCompanion service, which checks user consent for sharing the
499 // bugreport with the calling app. If the user has not responded yet to the dialog it will
500 // be neither confirmed nor denied.
501 class ConsentCallback : public android::os::BnIncidentAuthListener {
502 public:
503 ConsentCallback();
504 android::binder::Status onReportApproved() override;
505 android::binder::Status onReportDenied() override;
506
507 enum ConsentResult { APPROVED, DENIED, UNAVAILABLE };
508
509 ConsentResult getResult();
510
511 // Returns the time since creating this listener
512 uint64_t getElapsedTimeMs() const;
513
514 private:
515 ConsentResult result_;
516 uint64_t start_time_;
517 std::mutex lock_;
518 };
519
Felipe Lemee844a9d2016-09-21 15:01:39 -0700520 private:
Nandana Duttd2f5f082019-01-18 17:13:52 +0000521 RunStatus RunInternal(int32_t calling_uid, const std::string& calling_package);
522
Jichao Lie89d9c12019-11-21 19:02:51 -0800523 RunStatus DumpstateDefaultAfterCritical();
524
Paul Chang0d2aad72020-02-13 20:04:03 +0800525 void MaybeTakeEarlyScreenshot();
526
Paul Changc490e662020-04-11 18:14:09 +0800527 void onUiIntensiveBugreportDumpsFinished(int32_t calling_uid,
528 const std::string& calling_package);
529
Jichao Lie89d9c12019-11-21 19:02:51 -0800530 void MaybeCheckUserConsent(int32_t calling_uid, const std::string& calling_package);
Nandana Duttd2f5f082019-01-18 17:13:52 +0000531
532 // Removes the in progress files output files (tmp file, zip/txt file, screenshot),
533 // but leaves the log file alone.
Nandana Dutt8ae16e62020-03-27 10:20:22 +0000534 void CleanupTmpFiles();
Nandana Duttd2f5f082019-01-18 17:13:52 +0000535
Rhed Jao1c855122020-07-16 17:37:39 +0800536 // Create the thread pool to enable the parallel run function.
537 void EnableParallelRunIfNeeded();
538 void ShutdownDumpPool();
539
Nandana Duttd2f5f082019-01-18 17:13:52 +0000540 RunStatus HandleUserConsentDenied();
541
Abhijeet Kaur3172b532019-10-15 15:07:03 +0100542 // Copies bugreport artifacts over to the caller's directories provided there is user consent or
543 // called by Shell.
544 RunStatus CopyBugreportIfUserConsented(int32_t calling_uid);
Nandana Duttbabf6c72019-01-15 14:11:12 +0000545
Felipe Lemed80e6b62016-10-03 13:08:14 -0700546 // Used by GetInstance() only.
Chih-Hung Hsiehd0937e62018-12-20 15:43:57 -0800547 explicit Dumpstate(const std::string& version = VERSION_CURRENT);
Felipe Leme062926e2016-10-27 15:51:12 -0700548
Nandana Duttd2f5f082019-01-18 17:13:52 +0000549 android::sp<ConsentCallback> consent_callback_;
550
Felipe Leme062926e2016-10-27 15:51:12 -0700551 DISALLOW_COPY_AND_ASSIGN(Dumpstate);
Felipe Lemee844a9d2016-09-21 15:01:39 -0700552};
553
554// for_each_pid_func = void (*)(int, const char*);
555// for_each_tid_func = void (*)(int, int, const char*);
556
557typedef void(for_each_pid_func)(int, const char*);
558typedef void(for_each_tid_func)(int, int, const char*);
Felipe Leme71ca15e2016-05-19 16:18:17 -0700559
Felipe Leme71a74ac2016-03-17 15:43:25 -0700560/* saves the the contents of a file as a long */
561int read_file_as_long(const char *path, long int *output);
562
Christopher Ferris54bcc5f2015-02-10 12:15:01 -0800563/* prints the contents of the fd
564 * fd must have been opened with the flag O_NONBLOCK.
565 */
Christopher Ferris1fe61072014-07-22 16:08:19 -0700566int dump_file_from_fd(const char *title, const char *path, int fd);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700567
Mark Salyzyn326842f2015-04-30 09:49:41 -0700568/* calls skip to gate calling dump_from_fd recursively
569 * in the specified directory. dump_from_fd defaults to
570 * dump_file_from_fd above when set to NULL. skip defaults
571 * to false when set to NULL. dump_from_fd will always be
572 * called with title NULL.
573 */
Felipe Leme678727a2016-09-21 17:22:11 -0700574int dump_files(const std::string& title, const char* dir, bool (*skip)(const char* path),
575 int (*dump_from_fd)(const char* title, const char* path, int fd));
Mark Salyzyn326842f2015-04-30 09:49:41 -0700576
Felipe Leme2628e9e2016-04-12 16:36:51 -0700577/** opens a socket and returns its file descriptor */
578int open_socket(const char *service);
579
Nandana Dutta344cb62019-02-22 15:12:35 +0000580/*
581 * Redirects 'redirect' to a service control socket.
582 *
583 * Returns true if redirect succeeds.
584 */
585bool redirect_to_socket(FILE* redirect, const char* service);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700586
Nandana Dutta344cb62019-02-22 15:12:35 +0000587/*
588 * Redirects 'redirect' to a file indicated by 'path', truncating it.
589 *
590 * Returns true if redirect succeeds.
591 */
592bool redirect_to_file(FILE* redirect, char* path);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700593
Nandana Dutta344cb62019-02-22 15:12:35 +0000594/*
595 * Redirects 'redirect' to an existing file indicated by 'path', appending it.
596 *
597 * Returns true if redirect succeeds.
598 */
599bool redirect_to_existing_file(FILE* redirect, char* path);
Felipe Leme0f3fb202016-06-10 17:10:53 -0700600
Felipe Leme111b9d02016-02-03 09:28:24 -0800601/* create leading directories, if necessary */
602void create_parent_dirs(const char *path);
603
Colin Crossf45fa6b2012-03-26 12:38:26 -0700604/* for each process in the system, run the specified function */
Colin Cross0c22e8b2012-11-02 15:46:56 -0700605void for_each_pid(for_each_pid_func func, const char *header);
606
607/* for each thread in the system, run the specified function */
608void for_each_tid(for_each_tid_func func, const char *header);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700609
610/* Displays a blocked processes in-kernel wait channel */
Colin Cross0c22e8b2012-11-02 15:46:56 -0700611void show_wchan(int pid, int tid, const char *name);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700612
Mark Salyzyna297c322016-02-05 15:33:17 -0800613/* Displays a processes times */
614void show_showtime(int pid, const char *name);
615
Colin Crossf45fa6b2012-03-26 12:38:26 -0700616/* Runs "showmap" for a process */
617void do_showmap(int pid, const char *name);
618
619/* Gets the dmesg output for the kernel */
620void do_dmesg();
621
Sreeram Ramachandran2b3bba32014-07-08 15:40:55 -0700622/* Prints the contents of all the routing tables, both IPv4 and IPv6. */
623void dump_route_tables();
624
Colin Crossf45fa6b2012-03-26 12:38:26 -0700625/* Play a sound via Stagefright */
Christopher Ferris1fe61072014-07-22 16:08:19 -0700626void play_sound(const char *path);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700627
Felipe Leme0c80cf02016-01-05 13:25:34 -0800628/* Checks if a given path is a directory. */
629bool is_dir(const char* pathname);
630
631/** Gets the last modification time of a file, or default time if file is not found. */
632time_t get_mtime(int fd, time_t default_mtime);
633
Calvin On249beee2016-06-03 15:17:07 -0700634/** Gets command-line arguments. */
Felipe Lemea34efb72016-03-11 09:33:32 -0800635void format_args(int argc, const char *argv[], std::string *args);
Felipe Leme88c79332016-02-22 11:06:49 -0800636
Vishnu Nair20cf5032018-01-05 13:15:49 -0800637/** Main entry point for dumpstate. */
638int run_main(int argc, char* argv[]);
639
Felipe Leme8620bb42015-11-10 11:04:45 -0800640#ifdef __cplusplus
641}
642#endif
643
Felipe Leme8268ed22016-08-02 18:18:25 -0700644#endif /* FRAMEWORK_NATIVE_CMD_DUMPSTATE_H_ */