blob: fcb8cf3c07e7a43f70ff6ce3e98fd73e9b0a6061 [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
Kedar Chitnis9fd8c052021-11-16 09:09:22 +000028#include <aidl/android/hardware/dumpstate/IDumpstateDevice.h>
Felipe Lemec6bc8bc2016-10-27 15:58:27 -070029#include <android-base/macros.h>
Luis Hector Chavez91c2ae52018-03-14 15:12:46 -070030#include <android-base/unique_fd.h>
Hunter Knepshield8540faf2020-02-04 19:47:20 -080031#include <android/hardware/dumpstate/1.1/types.h>
Nandana Duttd2f5f082019-01-18 17:13:52 +000032#include <android/os/BnIncidentAuthListener.h>
Nandana Dutt197661d2018-11-16 16:40:21 +000033#include <android/os/IDumpstate.h>
Jiyong Parkb22e65d2017-06-23 21:23:16 +090034#include <android/os/IDumpstateListener.h>
35#include <utils/StrongPointer.h>
Felipe Lemec6bc8bc2016-10-27 15:58:27 -070036#include <ziparchive/zip_writer.h>
37
Felipe Lemebda15a02016-11-16 17:48:25 -080038#include "DumpstateUtil.h"
Rhed Jao1c855122020-07-16 17:37:39 +080039#include "DumpPool.h"
Rhed Jao4875aa62020-07-20 17:46:29 +080040#include "TaskQueue.h"
Felipe Leme75876a22016-10-27 16:31:27 -070041
Felipe Leme47e9be22016-12-21 15:37:07 -080042// TODO: move everything under this namespace
43// TODO: and then remove explicitly android::os::dumpstate:: prefixes
44namespace android {
45namespace os {
Nandana Dutt58d72e22018-11-16 10:30:48 +000046
47struct DumpstateOptions;
48
Felipe Leme47e9be22016-12-21 15:37:07 -080049namespace dumpstate {
50
51class DumpstateTest;
52class ProgressTest;
Dieter Hsu105ad0c2020-09-29 15:23:33 +080053class ZippedBugReportStreamTest;
Felipe Leme47e9be22016-12-21 15:37:07 -080054
55} // namespace dumpstate
56} // namespace os
57} // namespace android
58
Jiyong Parkb22e65d2017-06-23 21:23:16 +090059class ZipWriter;
60
Felipe Leme30dbfa12016-09-02 12:43:26 -070061// TODO: remove once moved to HAL
Felipe Leme8620bb42015-11-10 11:04:45 -080062#ifdef __cplusplus
63extern "C" {
64#endif
65
Felipe Leme30dbfa12016-09-02 12:43:26 -070066/*
Felipe Leme30dbfa12016-09-02 12:43:26 -070067 * 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 */
74class DurationReporter {
75 public:
Nandana Dutt8d945c02019-08-14 13:30:07 +010076 explicit DurationReporter(const std::string& title, bool logcat_only = false,
Rhed Jao1c855122020-07-16 17:37:39 +080077 bool verbose = false, int duration_fd = STDOUT_FILENO);
Felipe Leme30dbfa12016-09-02 12:43:26 -070078
79 ~DurationReporter();
80
Felipe Leme30dbfa12016-09-02 12:43:26 -070081 private:
Felipe Leme678727a2016-09-21 17:22:11 -070082 std::string title_;
Nandana Dutta8470b82019-03-11 11:00:58 +000083 bool logcat_only_;
Nandana Dutt8d945c02019-08-14 13:30:07 +010084 bool verbose_;
Felipe Lemeb0f669d2016-09-26 18:26:11 -070085 uint64_t started_;
Rhed Jao1c855122020-07-16 17:37:39 +080086 int duration_fd_;
Felipe Leme062926e2016-10-27 15:51:12 -070087
88 DISALLOW_COPY_AND_ASSIGN(DurationReporter);
Felipe Leme30dbfa12016-09-02 12:43:26 -070089};
90
91/*
Felipe Leme7447d7c2016-11-03 18:12:22 -070092 * Keeps track of current progress and estimated max, saving stats on file to tune up future runs.
Felipe Leme71bbfc52015-11-23 14:14:51 -080093 *
Felipe Leme7447d7c2016-11-03 18:12:22 -070094 * 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 Leme71bbfc52015-11-23 14:14:51 -080096 *
Felipe Leme7447d7c2016-11-03 18:12:22 -070097 * 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 Lemead5f6c42015-11-30 14:26:46 -080099 *
Felipe Leme71bbfc52015-11-23 14:14:51 -0800100 */
Felipe Leme7447d7c2016-11-03 18:12:22 -0700101class Progress {
Felipe Leme47e9be22016-12-21 15:37:07 -0800102 friend class android::os::dumpstate::ProgressTest;
103 friend class android::os::dumpstate::DumpstateTest;
Felipe Leme7447d7c2016-11-03 18:12:22 -0700104
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 Hsiehd0937e62018-12-20 15:43:57 -0800118 explicit Progress(const std::string& path = "");
Felipe Leme7447d7c2016-11-03 18:12:22 -0700119
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 Dutt979388e2018-11-30 16:48:55 +0000149 std::string path_;
Felipe Leme7447d7c2016-11-03 18:12:22 -0700150};
Felipe Leme71bbfc52015-11-23 14:14:51 -0800151
Felipe Leme71bbfc52015-11-23 14:14:51 -0800152/*
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700153 * List of supported zip format versions.
154 *
155 * See bugreport-format.md for more info.
156 */
Vishnu Nair64afc022018-02-01 15:29:34 -0800157static std::string VERSION_CURRENT = "2.0";
Felipe Lemed071c682016-10-20 16:48:00 -0700158
159/*
160 * "Alias" for the current version.
161 */
162static std::string VERSION_DEFAULT = "default";
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700163
164/*
Nandana Dutt979388e2018-11-30 16:48:55 +0000165 * Directory used by Dumpstate binary to keep its local files.
166 */
167static const std::string DUMPSTATE_DIRECTORY = "/bugreports";
168
169/*
Luis Hector Chavez91c2ae52018-03-14 15:12:46 -0700170 * Structure that contains the information of an open dump file.
171 */
172struct 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 Lemee844a9d2016-09-21 15:01:39 -0700184 * 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 */
189class Dumpstate {
Rhed Jao1c855122020-07-16 17:37:39 +0800190 friend class android::os::dumpstate::DumpstateTest;
Dieter Hsu105ad0c2020-09-29 15:23:33 +0800191 friend class android::os::dumpstate::ZippedBugReportStreamTest;
Felipe Leme4c2d6632016-09-28 14:32:00 -0700192
Felipe Lemee844a9d2016-09-21 15:01:39 -0700193 public:
Nandana Duttd2f5f082019-01-18 17:13:52 +0000194 enum RunStatus { OK, HELP, INVALID_INPUT, ERROR, USER_CONSENT_DENIED, USER_CONSENT_TIMED_OUT };
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100195
Nandana Dutt58d72e22018-11-16 10:30:48 +0000196 // The mode under which the bugreport should be run. Each mode encapsulates a few options.
197 enum BugreportMode {
Nandana Dutt197661d2018-11-16 16:40:21 +0000198 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 Kaur904e0e02018-12-05 14:03:01 +0000203 BUGREPORT_WIFI = android::os::IDumpstate::BUGREPORT_MODE_WIFI,
Elis Elliott8e401ad2023-08-08 11:18:59 +0000204 BUGREPORT_ONBOARDING = android::os::IDumpstate::BUGREPORT_MODE_ONBOARDING,
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000205 BUGREPORT_DEFAULT = android::os::IDumpstate::BUGREPORT_MODE_DEFAULT
Nandana Dutt58d72e22018-11-16 10:30:48 +0000206 };
207
Kean Mariotti306633e2022-09-05 16:30:47 +0000208 // The flags used to customize bugreport requests.
209 enum BugreportFlag {
210 BUGREPORT_USE_PREDUMPED_UI_DATA =
Gavin Corkerya44686c2022-11-23 18:16:51 +0000211 android::os::IDumpstate::BUGREPORT_FLAG_USE_PREDUMPED_UI_DATA,
212 BUGREPORT_FLAG_DEFER_CONSENT =
Kholoud Mohamed7c3fb7c2023-10-18 12:56:22 +0000213 android::os::IDumpstate::BUGREPORT_FLAG_DEFER_CONSENT,
214 BUGREPORT_FLAG_KEEP_BUGREPORT_ON_RETRIEVAL =
215 android::os::IDumpstate::BUGREPORT_FLAG_KEEP_BUGREPORT_ON_RETRIEVAL
Kean Mariotti306633e2022-09-05 16:30:47 +0000216 };
217
Felipe Leme47e9be22016-12-21 15:37:07 -0800218 static android::os::dumpstate::CommandOptions DEFAULT_DUMPSYS;
Felipe Lemebda15a02016-11-16 17:48:25 -0800219
Felipe Lemee844a9d2016-09-21 15:01:39 -0700220 static Dumpstate& GetInstance();
Felipe Leme71bbfc52015-11-23 14:14:51 -0800221
Abhijeet Kaura407fb82020-03-27 12:51:12 +0000222 /* Initialize dumpstate fields before starting bugreport generation */
223 void Initialize();
224
Felipe Leme6ad9c062016-09-28 11:58:36 -0700225 /*
Felipe Leme678727a2016-09-21 17:22:11 -0700226 * Forks a command, waits for it to finish, and returns its status.
227 *
228 * |title| description of the command printed on `stdout` (or empty to skip
229 * description).
230 * |full_command| array containing the command (first entry) and its arguments.
231 * Must contain at least one element.
232 * |options| optional argument defining the command's behavior.
Rhed Jao4875aa62020-07-20 17:46:29 +0800233 * |out_fd| A fd to support the DumpPool to output results to a temporary
234 * file. Using STDOUT_FILENO if it's not running in the parallel task.
Felipe Leme678727a2016-09-21 17:22:11 -0700235 */
236 int RunCommand(const std::string& title, const std::vector<std::string>& fullCommand,
Felipe Leme47e9be22016-12-21 15:37:07 -0800237 const android::os::dumpstate::CommandOptions& options =
Nandana Dutt8d945c02019-08-14 13:30:07 +0100238 android::os::dumpstate::CommandOptions::DEFAULT,
Rhed Jao4875aa62020-07-20 17:46:29 +0800239 bool verbose_duration = false, int out_fd = STDOUT_FILENO);
Felipe Leme678727a2016-09-21 17:22:11 -0700240
241 /*
242 * Runs `dumpsys` with the given arguments, automatically setting its timeout
Vishnu Nair6921f802017-11-22 09:17:23 -0800243 * (`-T` argument)
Felipe Leme678727a2016-09-21 17:22:11 -0700244 * according to the command options.
245 *
246 * |title| description of the command printed on `stdout` (or empty to skip
247 * description).
248 * |dumpsys_args| `dumpsys` arguments (except `-t`).
249 * |options| optional argument defining the command's behavior.
Vishnu Nair6921f802017-11-22 09:17:23 -0800250 * |dumpsys_timeout| when > 0, defines the value passed to `dumpsys -T` (otherwise it uses the
Felipe Leme6ad9c062016-09-28 11:58:36 -0700251 * timeout from `options`)
Rhed Jao6b1ea2d2020-07-21 17:58:41 +0800252 * |out_fd| A fd to support the DumpPool to output results to a temporary
253 * file. Using STDOUT_FILENO if it's not running in the parallel task.
Felipe Leme678727a2016-09-21 17:22:11 -0700254 */
Felipe Leme9a523ae2016-10-20 15:10:33 -0700255 void RunDumpsys(const std::string& title, const std::vector<std::string>& dumpsys_args,
Felipe Leme47e9be22016-12-21 15:37:07 -0800256 const android::os::dumpstate::CommandOptions& options = DEFAULT_DUMPSYS,
Rhed Jao6b1ea2d2020-07-21 17:58:41 +0800257 long dumpsys_timeout_ms = 0, int out_fd = STDOUT_FILENO);
Felipe Leme678727a2016-09-21 17:22:11 -0700258
259 /*
260 * Prints the contents of a file.
261 *
262 * |title| description of the command printed on `stdout` (or empty to skip
263 * description).
264 * |path| location of the file to be dumped.
265 */
266 int DumpFile(const std::string& title, const std::string& path);
267
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700268 /*
Felipe Leme1d486fe2016-10-14 18:06:47 -0700269 * Adds a new entry to the existing zip file.
270 * */
271 bool AddZipEntry(const std::string& entry_name, const std::string& entry_path);
272
273 /*
274 * Adds a new entry to the existing zip file.
Vishnu Naire97d6122018-01-18 13:58:56 -0800275 *
276 * |entry_name| destination path of the new entry.
277 * |fd| file descriptor to read from.
278 * |timeout| timeout to terminate the read if not completed. Set
279 * value of 0s (default) to disable timeout.
Felipe Leme1d486fe2016-10-14 18:06:47 -0700280 */
Vishnu Naire97d6122018-01-18 13:58:56 -0800281 android::status_t AddZipEntryFromFd(const std::string& entry_name, int fd,
282 std::chrono::milliseconds timeout);
Felipe Leme1d486fe2016-10-14 18:06:47 -0700283
284 /*
Nandana Dutta7db6342018-11-21 14:53:34 +0000285 * Adds a text entry to the existing zip file.
Felipe Leme1d486fe2016-10-14 18:06:47 -0700286 */
287 bool AddTextZipEntry(const std::string& entry_name, const std::string& content);
288
289 /*
290 * Adds all files from a directory to the zipped bugreport file.
291 */
292 void AddDir(const std::string& dir, bool recursive);
293
294 /*
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700295 * Takes a screenshot and save it to the given `path`.
296 *
297 * If `path` is empty, uses a standard path based on the bugreport name.
298 */
299 void TakeScreenshot(const std::string& path = "");
300
Felipe Leme7447d7c2016-11-03 18:12:22 -0700301 /////////////////////////////////////////////////////////////////////
302 // TODO: members below should be private once refactor is finished //
303 /////////////////////////////////////////////////////////////////////
304
305 // TODO: temporary method until Dumpstate object is properly set
306 void SetProgress(std::unique_ptr<Progress> progress);
Felipe Lemed80e6b62016-10-03 13:08:14 -0700307
Nandana Duttcf419a72019-03-14 10:40:17 +0000308 // Dumps Dalvik and native stack traces, sets the trace file location to path
309 // if it succeeded.
310 // Note that it returns early if user consent is denied with status USER_CONSENT_DENIED.
311 // Returns OK in all other cases.
312 RunStatus DumpTraces(const char** path);
Nandana Duttfaafd522019-03-11 09:23:09 +0000313
Rhed Jao4875aa62020-07-20 17:46:29 +0800314 /*
315 * |out_fd| A fd to support the DumpPool to output results to a temporary file.
316 * Dumpstate can pick up later and output to the bugreport. Using STDOUT_FILENO
317 * if it's not running in the parallel task.
318 */
319 void DumpstateBoard(int out_fd = STDOUT_FILENO);
Felipe Leme6f674ae2016-11-18 17:10:33 -0800320
Felipe Lemed80e6b62016-10-03 13:08:14 -0700321 /*
322 * Updates the overall progress of the bugreport generation by the given weight increment.
323 */
Felipe Leme7447d7c2016-11-03 18:12:22 -0700324 void UpdateProgress(int32_t delta);
Felipe Lemed80e6b62016-10-03 13:08:14 -0700325
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700326 /* Prints the dumpstate header on `stdout`. */
Felipe Leme2b9b06c2016-10-14 09:13:06 -0700327 void PrintHeader() const;
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700328
Felipe Leme1d486fe2016-10-14 18:06:47 -0700329 /*
330 * Adds the temporary report to the existing .zip file, closes the .zip file, and removes the
331 * temporary file.
332 */
333 bool FinishZipFile();
334
Nandana Dutt979388e2018-11-30 16:48:55 +0000335 /* Constructs a full path inside directory with file name formatted using the given suffix. */
336 std::string GetPath(const std::string& directory, const std::string& suffix) const;
337
338 /* Constructs a full path inside bugreport_internal_dir_ with file name formatted using the
339 * given suffix. */
Felipe Leme2b9b06c2016-10-14 09:13:06 -0700340 std::string GetPath(const std::string& suffix) const;
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700341
Vishnu Nair780b1282017-10-10 13:57:24 -0700342 /* Returns true if the current version supports priority dump feature. */
343 bool CurrentVersionSupportsPriorityDumps() const;
344
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100345 struct DumpOptions;
Felipe Lemee844a9d2016-09-21 15:01:39 -0700346
Kean Mariotti306633e2022-09-05 16:30:47 +0000347 /**
348 * Pre-dump critical UI data, e.g. data stored in short ring buffers that might get lost
349 * by the time the actual bugreport is requested.
350 */
351 void PreDumpUiData();
352
Abhijeet Kaura407fb82020-03-27 12:51:12 +0000353 /*
354 * Main entry point for running a complete bugreport.
355 *
356 * Initialize() dumpstate before calling this method.
357 *
358 */
Nandana Duttd2f5f082019-01-18 17:13:52 +0000359 RunStatus Run(int32_t calling_uid, const std::string& calling_package);
Nandana Dutt197661d2018-11-16 16:40:21 +0000360
Gavin Corkerya44686c2022-11-23 18:16:51 +0000361 /*
362 * Entry point for retrieving a previous-generated bugreport.
363 *
364 * Initialize() dumpstate before calling this method.
365 */
Kholoud Mohamed7c3fb7c2023-10-18 12:56:22 +0000366 RunStatus Retrieve(int32_t calling_uid, const std::string& calling_package,
Kholoud Mohameddef4e432024-05-15 12:47:46 +0000367 const bool keep_bugreport_on_retrieval, const bool skip_user_consent);
Gavin Corkerya44686c2022-11-23 18:16:51 +0000368
369
370
Nandana Duttf02564e2019-02-15 15:24:24 +0000371 RunStatus ParseCommandlineAndRun(int argc, char* argv[]);
372
Nandana Dutt8ae16e62020-03-27 10:20:22 +0000373 /* Deletes in-progress files */
374 void Cancel();
375
Nandana Dutt197661d2018-11-16 16:40:21 +0000376 /* Sets runtime options. */
377 void SetOptions(std::unique_ptr<DumpOptions> options);
Nandana Dutt3f8c7172018-09-25 12:01:54 +0100378
Nandana Dutt3f8c7172018-09-25 12:01:54 +0100379 /*
Nandana Duttbbdb5b42019-03-12 10:52:56 +0000380 * Returns true if user consent is necessary and has been denied.
381 * Consent is only necessary if the caller has asked to copy over the bugreport to a file they
382 * provided.
383 */
384 bool IsUserConsentDenied() const;
385
386 /*
Abhijeet Kaur359b1ff2019-07-26 16:01:36 +0100387 * Returns true if dumpstate is called by bugreporting API
388 */
389 bool CalledByApi() const;
390
391 /*
Rhed Jao4875aa62020-07-20 17:46:29 +0800392 * Enqueues a task to the dumpstate's TaskQueue if the parallel run is enabled,
393 * otherwise invokes it immediately. The task adds file at path entry_path
394 * as a zip file entry with name entry_name. Unlinks entry_path when done.
395 *
396 * All enqueued tasks will be executed in the dumpstate's FinishZipFile method
397 * before the zip file is finished. Tasks will be cancelled in dumpstate's
398 * ShutdownDumpPool method if they have never been called.
399 */
400 void EnqueueAddZipEntryAndCleanupIfNeeded(const std::string& entry_name,
401 const std::string& entry_path);
402
403 /*
Nandana Dutt3f8c7172018-09-25 12:01:54 +0100404 * Structure to hold options that determine the behavior of dumpstate.
405 */
406 struct DumpOptions {
Nandana Dutt3f8c7172018-09-25 12:01:54 +0100407 bool do_vibrate = true;
Dieter Hsu105ad0c2020-09-29 15:23:33 +0800408 // Writes bugreport zipped file to a socket.
409 bool stream_to_socket = false;
410 // Writes generation progress updates to a socket.
411 bool progress_updates_to_socket = false;
Paul Chang0d2aad72020-02-13 20:04:03 +0800412 bool do_screenshot = false;
413 bool is_screenshot_copied = false;
Gavin Corkerya44686c2022-11-23 18:16:51 +0000414 bool is_consent_deferred = false;
Kholoud Mohameddef4e432024-05-15 12:47:46 +0000415 bool skip_user_consent = false;
Nandana Dutt3f8c7172018-09-25 12:01:54 +0100416 bool is_remote_mode = false;
417 bool show_header_only = false;
Nandana Dutt3f8c7172018-09-25 12:01:54 +0100418 bool telephony_only = false;
419 bool wifi_only = false;
Elis Elliott8e401ad2023-08-08 11:18:59 +0000420 bool onboarding_only = false;
mhasankd451a472020-05-26 18:02:39 -0700421 // Trimmed-down version of dumpstate to only include whitelisted logs.
422 bool limited_only = false;
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100423 // Whether progress updates should be published.
424 bool do_progress_updates = false;
Kedar Chitnis9fd8c052021-11-16 09:09:22 +0000425 // this is used to derive dumpstate HAL bug report mode
Hunter Knepshield8540faf2020-02-04 19:47:20 -0800426 // TODO(b/148168577) get rid of the AIDL values, replace them with the HAL values instead.
427 // The HAL is actually an API surface that can be validated, while the AIDL is not (@hide).
Kedar Chitnis9fd8c052021-11-16 09:09:22 +0000428 BugreportMode bugreport_mode = Dumpstate::BugreportMode::BUGREPORT_DEFAULT;
Kean Mariotti306633e2022-09-05 16:30:47 +0000429 // Will use data collected through a previous call to PreDumpUiData().
430 bool use_predumped_ui_data;
mhasank3a4cfb42020-06-15 18:06:43 -0700431 // File descriptor to output zip file. Takes precedence over out_dir.
Nandana Dutt54dbd672019-01-11 12:58:05 +0000432 android::base::unique_fd bugreport_fd;
433 // File descriptor to screenshot file.
Nandana Dutt54dbd672019-01-11 12:58:05 +0000434 android::base::unique_fd screenshot_fd;
mhasank3a4cfb42020-06-15 18:06:43 -0700435 // Custom output directory.
436 std::string out_dir;
Kedar Chitnis9fd8c052021-11-16 09:09:22 +0000437 // Bugreport mode of the bugreport as a string
438 std::string bugreport_mode_string;
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100439 // Command-line arguments as string
440 std::string args;
441 // Notification title and description
442 std::string notification_title;
443 std::string notification_description;
444
Abhijeet Kaure370d682019-10-01 16:49:30 +0100445 /* Initializes options from commandline arguments. */
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100446 RunStatus Initialize(int argc, char* argv[]);
447
Nandana Dutt58d72e22018-11-16 10:30:48 +0000448 /* Initializes options from the requested mode. */
Kean Mariotti306633e2022-09-05 16:30:47 +0000449 void Initialize(BugreportMode bugreport_mode, int bugreport_flags,
450 const android::base::unique_fd& bugreport_fd,
Paul Changf59c2b72020-03-10 02:08:55 +0800451 const android::base::unique_fd& screenshot_fd,
Kholoud Mohameddef4e432024-05-15 12:47:46 +0000452 bool is_screenshot_requested,
453 bool skip_user_consent);
Nandana Dutt58d72e22018-11-16 10:30:48 +0000454
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100455 /* Returns true if the options set so far are consistent. */
456 bool ValidateOptions() const;
Nandana Dutt9a76d202019-01-21 15:56:48 +0000457
mhasank2d75c442020-06-11 15:05:25 -0700458 /* Returns if options specified require writing to custom file location */
459 bool OutputToCustomFile() {
460 // Custom location is only honored in limited mode.
mhasank3a4cfb42020-06-15 18:06:43 -0700461 return limited_only && !out_dir.empty() && bugreport_fd.get() == -1;
mhasank2d75c442020-06-11 15:05:25 -0700462 }
Nandana Dutt3f8c7172018-09-25 12:01:54 +0100463 };
464
465 // TODO: initialize fields on constructor
Felipe Lemee844a9d2016-09-21 15:01:39 -0700466 // dumpstate id - unique after each device reboot.
Felipe Leme7447d7c2016-11-03 18:12:22 -0700467 uint32_t id_;
Felipe Lemee844a9d2016-09-21 15:01:39 -0700468
Felipe Leme75876a22016-10-27 16:31:27 -0700469 // dumpstate pid
470 pid_t pid_;
471
Nandana Dutt3f8c7172018-09-25 12:01:54 +0100472 // Runtime options.
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100473 std::unique_ptr<DumpOptions> options_;
Felipe Lemee844a9d2016-09-21 15:01:39 -0700474
Nandana Dutt402a8392019-06-14 14:25:13 +0100475 // Last progress that was sent to the listener [0-100].
476 int last_reported_percent_progress_ = 0;
Felipe Leme009ecbb2016-11-07 10:18:44 -0800477
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700478 // Whether it should take an screenshot earlier in the process.
Felipe Leme9a523ae2016-10-20 15:10:33 -0700479 bool do_early_screenshot_ = false;
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700480
Felipe Leme7447d7c2016-11-03 18:12:22 -0700481 std::unique_ptr<Progress> progress_;
Felipe Lemee844a9d2016-09-21 15:01:39 -0700482
Dieter Hsu105ad0c2020-09-29 15:23:33 +0800483 // When set, defines a socket file-descriptor use to report progress to bugreportz
484 // or to stream the zipped file to.
Felipe Leme9a523ae2016-10-20 15:10:33 -0700485 int control_socket_fd_ = -1;
Felipe Lemee844a9d2016-09-21 15:01:39 -0700486
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700487 // Bugreport format version;
Felipe Lemed071c682016-10-20 16:48:00 -0700488 std::string version_ = VERSION_CURRENT;
Felipe Lemee844a9d2016-09-21 15:01:39 -0700489
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700490 time_t now_;
491
Felipe Leme2b9b06c2016-10-14 09:13:06 -0700492 // Base name (without suffix or extensions) of the bugreport files, typically
493 // `bugreport-BUILD_ID`.
Felipe Leme9a523ae2016-10-20 15:10:33 -0700494 std::string base_name_;
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700495
Dieter Hsu105ad0c2020-09-29 15:23:33 +0800496 // Name is the suffix part of the bugreport files - it's typically the date,
497 // but it could be changed by the user..
Felipe Leme2b9b06c2016-10-14 09:13:06 -0700498 std::string name_;
499
Nandana Dutt979388e2018-11-30 16:48:55 +0000500 std::string bugreport_internal_dir_ = DUMPSTATE_DIRECTORY;
501
502 // Full path of the temporary file containing the bugreport, inside bugreport_internal_dir_.
503 // At the very end this file is pulled into the zip file.
Felipe Leme9a523ae2016-10-20 15:10:33 -0700504 std::string tmp_path_;
Felipe Leme1d486fe2016-10-14 18:06:47 -0700505
Nandana Dutt979388e2018-11-30 16:48:55 +0000506 // Full path of the file containing the dumpstate logs, inside bugreport_internal_dir_.
507 // This is useful for debugging.
Felipe Leme9a523ae2016-10-20 15:10:33 -0700508 std::string log_path_;
Felipe Leme1d486fe2016-10-14 18:06:47 -0700509
Chris Morinbc223142022-02-04 14:17:11 -0800510 // Full path of the bugreport zip file inside bugreport_internal_dir_.
Felipe Leme9a523ae2016-10-20 15:10:33 -0700511 std::string path_;
Felipe Leme1d486fe2016-10-14 18:06:47 -0700512
Nandana Duttf531f4b2019-06-14 12:20:03 +0100513 // Full path of the file containing the screenshot (when requested).
Nandana Dutt979388e2018-11-30 16:48:55 +0000514 std::string screenshot_path_;
515
Felipe Leme1d486fe2016-10-14 18:06:47 -0700516 // Pointer to the zipped file.
517 std::unique_ptr<FILE, int (*)(FILE*)> zip_file{nullptr, fclose};
518
Felipe Lemec6bc8bc2016-10-27 15:58:27 -0700519 // Pointer to the zip structure.
520 std::unique_ptr<ZipWriter> zip_writer_;
521
Vishnu Nair20cf5032018-01-05 13:15:49 -0800522 // Binder object listening to progress.
Felipe Leme75876a22016-10-27 16:31:27 -0700523 android::sp<android::os::IDumpstateListener> listener_;
Felipe Leme75876a22016-10-27 16:31:27 -0700524
Luis Hector Chavez91c2ae52018-03-14 15:12:46 -0700525 // List of open tombstone dump files.
526 std::vector<DumpData> tombstone_data_;
527
528 // List of open ANR dump files.
529 std::vector<DumpData> anr_data_;
530
Andy Hungd62f7e62024-01-11 15:47:52 -0800531 // List of open Java traces files in the anr directory.
532 std::vector<DumpData> anr_trace_data_;
533
Woody Lin20767a92022-11-29 15:50:24 +0800534 // List of open shutdown checkpoint files.
535 std::vector<DumpData> shutdown_checkpoints_;
536
Rhed Jao1c855122020-07-16 17:37:39 +0800537 // A thread pool to execute dump tasks simultaneously if the parallel run is enabled.
538 std::unique_ptr<android::os::dumpstate::DumpPool> dump_pool_;
539
Rhed Jao4875aa62020-07-20 17:46:29 +0800540 // A task queue to collect adding zip entry tasks inside dump tasks if the
541 // parallel run is enabled.
542 std::unique_ptr<android::os::dumpstate::TaskQueue> zip_entry_tasks_;
543
Nandana Duttd2f5f082019-01-18 17:13:52 +0000544 // A callback to IncidentCompanion service, which checks user consent for sharing the
545 // bugreport with the calling app. If the user has not responded yet to the dialog it will
546 // be neither confirmed nor denied.
547 class ConsentCallback : public android::os::BnIncidentAuthListener {
548 public:
549 ConsentCallback();
550 android::binder::Status onReportApproved() override;
551 android::binder::Status onReportDenied() override;
552
553 enum ConsentResult { APPROVED, DENIED, UNAVAILABLE };
554
555 ConsentResult getResult();
556
557 // Returns the time since creating this listener
558 uint64_t getElapsedTimeMs() const;
559
560 private:
561 ConsentResult result_;
562 uint64_t start_time_;
563 std::mutex lock_;
564 };
565
Felipe Lemee844a9d2016-09-21 15:01:39 -0700566 private:
Nandana Duttd2f5f082019-01-18 17:13:52 +0000567 RunStatus RunInternal(int32_t calling_uid, const std::string& calling_package);
Kholoud Mohamed7c3fb7c2023-10-18 12:56:22 +0000568 RunStatus RetrieveInternal(int32_t calling_uid, const std::string& calling_package,
Kholoud Mohameddef4e432024-05-15 12:47:46 +0000569 const bool keep_bugreport_on_retrieval,
570 const bool skip_user_consent);
Nandana Duttd2f5f082019-01-18 17:13:52 +0000571
Jichao Lie89d9c12019-11-21 19:02:51 -0800572 RunStatus DumpstateDefaultAfterCritical();
Kean Mariotti306633e2022-09-05 16:30:47 +0000573 RunStatus dumpstate();
Jichao Lie89d9c12019-11-21 19:02:51 -0800574
Paul Chang0d2aad72020-02-13 20:04:03 +0800575 void MaybeTakeEarlyScreenshot();
Kean Mariottica20f2d2023-12-15 09:34:25 +0000576 std::future<std::string> MaybeSnapshotSystemTraceAsync();
577 void MaybeWaitForSnapshotSystemTrace(std::future<std::string> task);
Kean Mariotti306633e2022-09-05 16:30:47 +0000578 void MaybeSnapshotUiTraces();
Kean Mariotti306633e2022-09-05 16:30:47 +0000579 void MaybeAddUiTracesToZip();
Paul Chang0d2aad72020-02-13 20:04:03 +0800580
Paul Chang5702b482020-05-28 22:05:47 +0800581 void onUiIntensiveBugreportDumpsFinished(int32_t calling_uid);
Paul Changc490e662020-04-11 18:14:09 +0800582
Jichao Lie89d9c12019-11-21 19:02:51 -0800583 void MaybeCheckUserConsent(int32_t calling_uid, const std::string& calling_package);
Nandana Duttd2f5f082019-01-18 17:13:52 +0000584
585 // Removes the in progress files output files (tmp file, zip/txt file, screenshot),
586 // but leaves the log file alone.
Nandana Dutt8ae16e62020-03-27 10:20:22 +0000587 void CleanupTmpFiles();
Nandana Duttd2f5f082019-01-18 17:13:52 +0000588
Rhed Jao1c855122020-07-16 17:37:39 +0800589 // Create the thread pool to enable the parallel run function.
590 void EnableParallelRunIfNeeded();
591 void ShutdownDumpPool();
592
Nandana Duttd2f5f082019-01-18 17:13:52 +0000593 RunStatus HandleUserConsentDenied();
594
Gavin Corkerya44686c2022-11-23 18:16:51 +0000595 void HandleRunStatus(RunStatus status);
596
Abhijeet Kaur3172b532019-10-15 15:07:03 +0100597 // Copies bugreport artifacts over to the caller's directories provided there is user consent or
598 // called by Shell.
599 RunStatus CopyBugreportIfUserConsented(int32_t calling_uid);
Nandana Duttbabf6c72019-01-15 14:11:12 +0000600
Dieter Hsu105ad0c2020-09-29 15:23:33 +0800601 std::function<int(const char *)> open_socket_fn_;
602
Felipe Lemed80e6b62016-10-03 13:08:14 -0700603 // Used by GetInstance() only.
Chih-Hung Hsiehd0937e62018-12-20 15:43:57 -0800604 explicit Dumpstate(const std::string& version = VERSION_CURRENT);
Felipe Leme062926e2016-10-27 15:51:12 -0700605
Nandana Duttd2f5f082019-01-18 17:13:52 +0000606 android::sp<ConsentCallback> consent_callback_;
607
Rhed Jao2cc4eec2020-07-21 15:42:55 +0800608 std::recursive_mutex mutex_;
609
Felipe Leme062926e2016-10-27 15:51:12 -0700610 DISALLOW_COPY_AND_ASSIGN(Dumpstate);
Felipe Lemee844a9d2016-09-21 15:01:39 -0700611};
612
613// for_each_pid_func = void (*)(int, const char*);
614// for_each_tid_func = void (*)(int, int, const char*);
615
616typedef void(for_each_pid_func)(int, const char*);
617typedef void(for_each_tid_func)(int, int, const char*);
Felipe Leme71ca15e2016-05-19 16:18:17 -0700618
Felipe Leme71a74ac2016-03-17 15:43:25 -0700619/* saves the the contents of a file as a long */
620int read_file_as_long(const char *path, long int *output);
621
Christopher Ferris54bcc5f2015-02-10 12:15:01 -0800622/* prints the contents of the fd
623 * fd must have been opened with the flag O_NONBLOCK.
624 */
Christopher Ferris1fe61072014-07-22 16:08:19 -0700625int dump_file_from_fd(const char *title, const char *path, int fd);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700626
Mark Salyzyn326842f2015-04-30 09:49:41 -0700627/* calls skip to gate calling dump_from_fd recursively
628 * in the specified directory. dump_from_fd defaults to
629 * dump_file_from_fd above when set to NULL. skip defaults
630 * to false when set to NULL. dump_from_fd will always be
631 * called with title NULL.
632 */
Felipe Leme678727a2016-09-21 17:22:11 -0700633int dump_files(const std::string& title, const char* dir, bool (*skip)(const char* path),
634 int (*dump_from_fd)(const char* title, const char* path, int fd));
Mark Salyzyn326842f2015-04-30 09:49:41 -0700635
Nandana Dutta344cb62019-02-22 15:12:35 +0000636/*
637 * Redirects 'redirect' to a file indicated by 'path', truncating it.
638 *
639 * Returns true if redirect succeeds.
640 */
641bool redirect_to_file(FILE* redirect, char* path);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700642
Nandana Dutta344cb62019-02-22 15:12:35 +0000643/*
644 * Redirects 'redirect' to an existing file indicated by 'path', appending it.
645 *
646 * Returns true if redirect succeeds.
647 */
648bool redirect_to_existing_file(FILE* redirect, char* path);
Felipe Leme0f3fb202016-06-10 17:10:53 -0700649
Felipe Leme111b9d02016-02-03 09:28:24 -0800650/* create leading directories, if necessary */
651void create_parent_dirs(const char *path);
652
Colin Crossf45fa6b2012-03-26 12:38:26 -0700653/* for each process in the system, run the specified function */
Colin Cross0c22e8b2012-11-02 15:46:56 -0700654void for_each_pid(for_each_pid_func func, const char *header);
655
656/* for each thread in the system, run the specified function */
657void for_each_tid(for_each_tid_func func, const char *header);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700658
659/* Displays a blocked processes in-kernel wait channel */
Colin Cross0c22e8b2012-11-02 15:46:56 -0700660void show_wchan(int pid, int tid, const char *name);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700661
Mark Salyzyna297c322016-02-05 15:33:17 -0800662/* Displays a processes times */
663void show_showtime(int pid, const char *name);
664
Colin Crossf45fa6b2012-03-26 12:38:26 -0700665/* Gets the dmesg output for the kernel */
666void do_dmesg();
667
Sreeram Ramachandran2b3bba32014-07-08 15:40:55 -0700668/* Prints the contents of all the routing tables, both IPv4 and IPv6. */
669void dump_route_tables();
670
Li Li830179f2022-01-04 12:53:29 -0800671/* Dump subdirectories of cgroupfs if the corresponding process is frozen */
672void dump_frozen_cgroupfs();
673
Colin Crossf45fa6b2012-03-26 12:38:26 -0700674/* Play a sound via Stagefright */
Christopher Ferris1fe61072014-07-22 16:08:19 -0700675void play_sound(const char *path);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700676
Felipe Leme0c80cf02016-01-05 13:25:34 -0800677/* Checks if a given path is a directory. */
678bool is_dir(const char* pathname);
679
680/** Gets the last modification time of a file, or default time if file is not found. */
681time_t get_mtime(int fd, time_t default_mtime);
682
Calvin On249beee2016-06-03 15:17:07 -0700683/** Gets command-line arguments. */
Felipe Lemea34efb72016-03-11 09:33:32 -0800684void format_args(int argc, const char *argv[], std::string *args);
Felipe Leme88c79332016-02-22 11:06:49 -0800685
Vishnu Nair20cf5032018-01-05 13:15:49 -0800686/** Main entry point for dumpstate. */
687int run_main(int argc, char* argv[]);
688
Felipe Leme8620bb42015-11-10 11:04:45 -0800689#ifdef __cplusplus
690}
691#endif
692
Felipe Leme8268ed22016-08-02 18:18:25 -0700693#endif /* FRAMEWORK_NATIVE_CMD_DUMPSTATE_H_ */