blob: 0f0927cc297447818d0e8261aa161f3d43c58660 [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"
Rhed Jao4875aa62020-07-20 17:46:29 +080039#include "TaskQueue.h"
Felipe Leme75876a22016-10-27 16:31:27 -070040
Felipe Leme35c94f32016-08-12 10:51:54 -070041// Workaround for const char *args[MAX_ARGS_ARRAY_SIZE] variables until they're converted to
42// std::vector<std::string>
Felipe Leme30dbfa12016-09-02 12:43:26 -070043// TODO: remove once not used
Felipe Leme35c94f32016-08-12 10:51:54 -070044#define MAX_ARGS_ARRAY_SIZE 1000
45
Felipe Leme47e9be22016-12-21 15:37:07 -080046// TODO: move everything under this namespace
47// TODO: and then remove explicitly android::os::dumpstate:: prefixes
48namespace android {
49namespace os {
Nandana Dutt58d72e22018-11-16 10:30:48 +000050
51struct DumpstateOptions;
52
Felipe Leme47e9be22016-12-21 15:37:07 -080053namespace dumpstate {
54
55class DumpstateTest;
56class ProgressTest;
57
58} // namespace dumpstate
59} // namespace os
60} // namespace android
61
Jiyong Parkb22e65d2017-06-23 21:23:16 +090062class ZipWriter;
63
Felipe Leme30dbfa12016-09-02 12:43:26 -070064// TODO: remove once moved to HAL
Felipe Leme8620bb42015-11-10 11:04:45 -080065#ifdef __cplusplus
66extern "C" {
67#endif
68
Felipe Leme30dbfa12016-09-02 12:43:26 -070069/*
Felipe Leme30dbfa12016-09-02 12:43:26 -070070 * Helper class used to report how long it takes for a section to finish.
71 *
72 * Typical usage:
73 *
74 * DurationReporter duration_reporter(title);
75 *
76 */
77class DurationReporter {
78 public:
Nandana Dutt8d945c02019-08-14 13:30:07 +010079 explicit DurationReporter(const std::string& title, bool logcat_only = false,
Rhed Jao1c855122020-07-16 17:37:39 +080080 bool verbose = false, int duration_fd = STDOUT_FILENO);
Felipe Leme30dbfa12016-09-02 12:43:26 -070081
82 ~DurationReporter();
83
Felipe Leme30dbfa12016-09-02 12:43:26 -070084 private:
Felipe Leme678727a2016-09-21 17:22:11 -070085 std::string title_;
Nandana Dutta8470b82019-03-11 11:00:58 +000086 bool logcat_only_;
Nandana Dutt8d945c02019-08-14 13:30:07 +010087 bool verbose_;
Felipe Lemeb0f669d2016-09-26 18:26:11 -070088 uint64_t started_;
Rhed Jao1c855122020-07-16 17:37:39 +080089 int duration_fd_;
Felipe Leme062926e2016-10-27 15:51:12 -070090
91 DISALLOW_COPY_AND_ASSIGN(DurationReporter);
Felipe Leme30dbfa12016-09-02 12:43:26 -070092};
93
94/*
Felipe Leme7447d7c2016-11-03 18:12:22 -070095 * Keeps track of current progress and estimated max, saving stats on file to tune up future runs.
Felipe Leme71bbfc52015-11-23 14:14:51 -080096 *
Felipe Leme7447d7c2016-11-03 18:12:22 -070097 * Each `dumpstate` section contributes to the total weight by an individual weight, so the overall
98 * progress can be calculated by dividing the estimate max progress by the current progress.
Felipe Leme71bbfc52015-11-23 14:14:51 -080099 *
Felipe Leme7447d7c2016-11-03 18:12:22 -0700100 * The estimated max progress is initially set to a value (`kDefaultMax) defined empirically, but
101 * it's adjusted after each dumpstate run by storing the average duration in a file.
Felipe Lemead5f6c42015-11-30 14:26:46 -0800102 *
Felipe Leme71bbfc52015-11-23 14:14:51 -0800103 */
Felipe Leme7447d7c2016-11-03 18:12:22 -0700104class Progress {
Felipe Leme47e9be22016-12-21 15:37:07 -0800105 friend class android::os::dumpstate::ProgressTest;
106 friend class android::os::dumpstate::DumpstateTest;
Felipe Leme7447d7c2016-11-03 18:12:22 -0700107
108 public:
109 /*
110 * Default estimation of the max duration of a bugreport generation.
111 *
112 * It does not need to match the exact sum of all sections, but ideally it should to be slight
113 * more than such sum: a value too high will cause the bugreport to finish before the user
114 * expected (for example, jumping from 70% to 100%), while a value too low will cause the
115 * progress to get stuck at an almost-finished value (like 99%) for a while.
116 *
117 * This constant is only used when the average duration from previous runs cannot be used.
118 */
119 static const int kDefaultMax;
120
Chih-Hung Hsiehd0937e62018-12-20 15:43:57 -0800121 explicit Progress(const std::string& path = "");
Felipe Leme7447d7c2016-11-03 18:12:22 -0700122
123 // Gets the current progress.
124 int32_t Get() const;
125
126 // Gets the current estimated max progress.
127 int32_t GetMax() const;
128
129 // Gets the initial estimated max progress.
130 int32_t GetInitialMax() const;
131
132 // Increments progress (ignored if not positive).
133 // Returns `true` if the max progress increased as well.
134 bool Inc(int32_t delta);
135
136 // Persist the stats.
137 void Save();
138
139 void Dump(int fd, const std::string& prefix) const;
140
141 private:
142 Progress(int32_t initial_max, float growth_factor,
143 const std::string& path = ""); // Used by test cases.
144 Progress(int32_t initial_max, int32_t progress, float growth_factor); // Used by test cases.
145 void Load();
146 int32_t initial_max_;
147 int32_t progress_;
148 int32_t max_;
149 float growth_factor_;
150 int32_t n_runs_;
151 int32_t average_max_;
Nandana Dutt979388e2018-11-30 16:48:55 +0000152 std::string path_;
Felipe Leme7447d7c2016-11-03 18:12:22 -0700153};
Felipe Leme71bbfc52015-11-23 14:14:51 -0800154
Felipe Leme71bbfc52015-11-23 14:14:51 -0800155/*
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700156 * List of supported zip format versions.
157 *
158 * See bugreport-format.md for more info.
159 */
Vishnu Nair64afc022018-02-01 15:29:34 -0800160static std::string VERSION_CURRENT = "2.0";
Felipe Lemed071c682016-10-20 16:48:00 -0700161
162/*
Felipe Lemee184f662016-10-27 10:04:47 -0700163 * Temporary version that adds a anr-traces.txt entry. Once tools support it, the current version
Vishnu Nair64afc022018-02-01 15:29:34 -0800164 * will be bumped to 3.0.
Felipe Lemee184f662016-10-27 10:04:47 -0700165 */
Vishnu Nair64afc022018-02-01 15:29:34 -0800166static std::string VERSION_SPLIT_ANR = "3.0-dev-split-anr";
Felipe Lemee184f662016-10-27 10:04:47 -0700167
168/*
Felipe Lemed071c682016-10-20 16:48:00 -0700169 * "Alias" for the current version.
170 */
171static std::string VERSION_DEFAULT = "default";
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700172
173/*
Nandana Dutt979388e2018-11-30 16:48:55 +0000174 * Directory used by Dumpstate binary to keep its local files.
175 */
176static const std::string DUMPSTATE_DIRECTORY = "/bugreports";
177
178/*
Luis Hector Chavez91c2ae52018-03-14 15:12:46 -0700179 * Structure that contains the information of an open dump file.
180 */
181struct DumpData {
182 // Path of the file.
183 std::string name;
184
185 // Open file descriptor for the file.
186 android::base::unique_fd fd;
187
188 // Modification time of the file.
189 time_t mtime;
190};
191
192/*
Felipe Lemee844a9d2016-09-21 15:01:39 -0700193 * Main class driving a bugreport generation.
194 *
195 * Currently, it only contains variables that are accessed externally, but gradually the functions
196 * that are spread accross utils.cpp and dumpstate.cpp will be moved to it.
197 */
198class Dumpstate {
Rhed Jao1c855122020-07-16 17:37:39 +0800199 friend class android::os::dumpstate::DumpstateTest;
Felipe Leme4c2d6632016-09-28 14:32:00 -0700200
Felipe Lemee844a9d2016-09-21 15:01:39 -0700201 public:
Nandana Duttd2f5f082019-01-18 17:13:52 +0000202 enum RunStatus { OK, HELP, INVALID_INPUT, ERROR, USER_CONSENT_DENIED, USER_CONSENT_TIMED_OUT };
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100203
Nandana Dutt58d72e22018-11-16 10:30:48 +0000204 // The mode under which the bugreport should be run. Each mode encapsulates a few options.
205 enum BugreportMode {
Nandana Dutt197661d2018-11-16 16:40:21 +0000206 BUGREPORT_FULL = android::os::IDumpstate::BUGREPORT_MODE_FULL,
207 BUGREPORT_INTERACTIVE = android::os::IDumpstate::BUGREPORT_MODE_INTERACTIVE,
208 BUGREPORT_REMOTE = android::os::IDumpstate::BUGREPORT_MODE_REMOTE,
209 BUGREPORT_WEAR = android::os::IDumpstate::BUGREPORT_MODE_WEAR,
210 BUGREPORT_TELEPHONY = android::os::IDumpstate::BUGREPORT_MODE_TELEPHONY,
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000211 BUGREPORT_WIFI = android::os::IDumpstate::BUGREPORT_MODE_WIFI,
212 BUGREPORT_DEFAULT = android::os::IDumpstate::BUGREPORT_MODE_DEFAULT
Nandana Dutt58d72e22018-11-16 10:30:48 +0000213 };
214
Felipe Leme47e9be22016-12-21 15:37:07 -0800215 static android::os::dumpstate::CommandOptions DEFAULT_DUMPSYS;
Felipe Lemebda15a02016-11-16 17:48:25 -0800216
Felipe Lemee844a9d2016-09-21 15:01:39 -0700217 static Dumpstate& GetInstance();
Felipe Leme71bbfc52015-11-23 14:14:51 -0800218
Felipe Leme1d486fe2016-10-14 18:06:47 -0700219 /* Checkes whether dumpstate is generating a zipped bugreport. */
220 bool IsZipping() const;
221
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`)
Felipe Leme678727a2016-09-21 17:22:11 -0700252 */
Felipe Leme9a523ae2016-10-20 15:10:33 -0700253 void RunDumpsys(const std::string& title, const std::vector<std::string>& dumpsys_args,
Felipe Leme47e9be22016-12-21 15:37:07 -0800254 const android::os::dumpstate::CommandOptions& options = DEFAULT_DUMPSYS,
Vishnu Nair6921f802017-11-22 09:17:23 -0800255 long dumpsys_timeout_ms = 0);
Felipe Leme678727a2016-09-21 17:22:11 -0700256
257 /*
258 * Prints the contents of a file.
259 *
260 * |title| description of the command printed on `stdout` (or empty to skip
261 * description).
262 * |path| location of the file to be dumped.
263 */
264 int DumpFile(const std::string& title, const std::string& path);
265
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700266 /*
Felipe Leme1d486fe2016-10-14 18:06:47 -0700267 * Adds a new entry to the existing zip file.
268 * */
269 bool AddZipEntry(const std::string& entry_name, const std::string& entry_path);
270
271 /*
272 * Adds a new entry to the existing zip file.
Vishnu Naire97d6122018-01-18 13:58:56 -0800273 *
274 * |entry_name| destination path of the new entry.
275 * |fd| file descriptor to read from.
276 * |timeout| timeout to terminate the read if not completed. Set
277 * value of 0s (default) to disable timeout.
Felipe Leme1d486fe2016-10-14 18:06:47 -0700278 */
Vishnu Naire97d6122018-01-18 13:58:56 -0800279 android::status_t AddZipEntryFromFd(const std::string& entry_name, int fd,
280 std::chrono::milliseconds timeout);
Felipe Leme1d486fe2016-10-14 18:06:47 -0700281
282 /*
Nandana Dutta7db6342018-11-21 14:53:34 +0000283 * Adds a text entry to the existing zip file.
Felipe Leme1d486fe2016-10-14 18:06:47 -0700284 */
285 bool AddTextZipEntry(const std::string& entry_name, const std::string& content);
286
287 /*
288 * Adds all files from a directory to the zipped bugreport file.
289 */
290 void AddDir(const std::string& dir, bool recursive);
291
292 /*
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700293 * Takes a screenshot and save it to the given `path`.
294 *
295 * If `path` is empty, uses a standard path based on the bugreport name.
296 */
297 void TakeScreenshot(const std::string& path = "");
298
Felipe Leme7447d7c2016-11-03 18:12:22 -0700299 /////////////////////////////////////////////////////////////////////
300 // TODO: members below should be private once refactor is finished //
301 /////////////////////////////////////////////////////////////////////
302
303 // TODO: temporary method until Dumpstate object is properly set
304 void SetProgress(std::unique_ptr<Progress> progress);
Felipe Lemed80e6b62016-10-03 13:08:14 -0700305
Nandana Duttcf419a72019-03-14 10:40:17 +0000306 // Dumps Dalvik and native stack traces, sets the trace file location to path
307 // if it succeeded.
308 // Note that it returns early if user consent is denied with status USER_CONSENT_DENIED.
309 // Returns OK in all other cases.
310 RunStatus DumpTraces(const char** path);
Nandana Duttfaafd522019-03-11 09:23:09 +0000311
Rhed Jao4875aa62020-07-20 17:46:29 +0800312 /*
313 * |out_fd| A fd to support the DumpPool to output results to a temporary file.
314 * Dumpstate can pick up later and output to the bugreport. Using STDOUT_FILENO
315 * if it's not running in the parallel task.
316 */
317 void DumpstateBoard(int out_fd = STDOUT_FILENO);
Felipe Leme6f674ae2016-11-18 17:10:33 -0800318
Felipe Lemed80e6b62016-10-03 13:08:14 -0700319 /*
320 * Updates the overall progress of the bugreport generation by the given weight increment.
321 */
Felipe Leme7447d7c2016-11-03 18:12:22 -0700322 void UpdateProgress(int32_t delta);
Felipe Lemed80e6b62016-10-03 13:08:14 -0700323
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700324 /* Prints the dumpstate header on `stdout`. */
Felipe Leme2b9b06c2016-10-14 09:13:06 -0700325 void PrintHeader() const;
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700326
Felipe Leme1d486fe2016-10-14 18:06:47 -0700327 /*
328 * Adds the temporary report to the existing .zip file, closes the .zip file, and removes the
329 * temporary file.
330 */
331 bool FinishZipFile();
332
Nandana Dutt979388e2018-11-30 16:48:55 +0000333 /* Constructs a full path inside directory with file name formatted using the given suffix. */
334 std::string GetPath(const std::string& directory, const std::string& suffix) const;
335
336 /* Constructs a full path inside bugreport_internal_dir_ with file name formatted using the
337 * given suffix. */
Felipe Leme2b9b06c2016-10-14 09:13:06 -0700338 std::string GetPath(const std::string& suffix) const;
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700339
Vishnu Nair780b1282017-10-10 13:57:24 -0700340 /* Returns true if the current version supports priority dump feature. */
341 bool CurrentVersionSupportsPriorityDumps() const;
342
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100343 struct DumpOptions;
Felipe Lemee844a9d2016-09-21 15:01:39 -0700344
Abhijeet Kaura407fb82020-03-27 12:51:12 +0000345 /*
346 * Main entry point for running a complete bugreport.
347 *
348 * Initialize() dumpstate before calling this method.
349 *
350 */
Nandana Duttd2f5f082019-01-18 17:13:52 +0000351 RunStatus Run(int32_t calling_uid, const std::string& calling_package);
Nandana Dutt197661d2018-11-16 16:40:21 +0000352
Nandana Duttf02564e2019-02-15 15:24:24 +0000353 RunStatus ParseCommandlineAndRun(int argc, char* argv[]);
354
Nandana Dutt8ae16e62020-03-27 10:20:22 +0000355 /* Deletes in-progress files */
356 void Cancel();
357
Nandana Dutt197661d2018-11-16 16:40:21 +0000358 /* Sets runtime options. */
359 void SetOptions(std::unique_ptr<DumpOptions> options);
Nandana Dutt3f8c7172018-09-25 12:01:54 +0100360
Nandana Dutt3f8c7172018-09-25 12:01:54 +0100361 /*
Nandana Duttbbdb5b42019-03-12 10:52:56 +0000362 * Returns true if user consent is necessary and has been denied.
363 * Consent is only necessary if the caller has asked to copy over the bugreport to a file they
364 * provided.
365 */
366 bool IsUserConsentDenied() const;
367
368 /*
Abhijeet Kaur359b1ff2019-07-26 16:01:36 +0100369 * Returns true if dumpstate is called by bugreporting API
370 */
371 bool CalledByApi() const;
372
373 /*
Rhed Jao4875aa62020-07-20 17:46:29 +0800374 * Enqueues a task to the dumpstate's TaskQueue if the parallel run is enabled,
375 * otherwise invokes it immediately. The task adds file at path entry_path
376 * as a zip file entry with name entry_name. Unlinks entry_path when done.
377 *
378 * All enqueued tasks will be executed in the dumpstate's FinishZipFile method
379 * before the zip file is finished. Tasks will be cancelled in dumpstate's
380 * ShutdownDumpPool method if they have never been called.
381 */
382 void EnqueueAddZipEntryAndCleanupIfNeeded(const std::string& entry_name,
383 const std::string& entry_path);
384
385 /*
Nandana Dutt3f8c7172018-09-25 12:01:54 +0100386 * Structure to hold options that determine the behavior of dumpstate.
387 */
388 struct DumpOptions {
389 bool do_add_date = false;
390 bool do_zip_file = false;
391 bool do_vibrate = true;
Nandana Dutt9a76d202019-01-21 15:56:48 +0000392 // Writes bugreport content to a socket; only flatfile format is supported.
Nandana Dutt3f8c7172018-09-25 12:01:54 +0100393 bool use_socket = false;
394 bool use_control_socket = false;
Paul Chang0d2aad72020-02-13 20:04:03 +0800395 bool do_screenshot = false;
396 bool is_screenshot_copied = false;
Nandana Dutt3f8c7172018-09-25 12:01:54 +0100397 bool is_remote_mode = false;
398 bool show_header_only = false;
399 bool do_start_service = false;
400 bool telephony_only = false;
401 bool wifi_only = false;
mhasankd451a472020-05-26 18:02:39 -0700402 // Trimmed-down version of dumpstate to only include whitelisted logs.
403 bool limited_only = false;
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100404 // Whether progress updates should be published.
405 bool do_progress_updates = false;
Hunter Knepshield8540faf2020-02-04 19:47:20 -0800406 // The mode we'll use when calling IDumpstateDevice::dumpstateBoard.
407 // TODO(b/148168577) get rid of the AIDL values, replace them with the HAL values instead.
408 // The HAL is actually an API surface that can be validated, while the AIDL is not (@hide).
409 ::android::hardware::dumpstate::V1_1::DumpstateMode dumpstate_hal_mode =
410 ::android::hardware::dumpstate::V1_1::DumpstateMode::DEFAULT;
mhasank3a4cfb42020-06-15 18:06:43 -0700411 // File descriptor to output zip file. Takes precedence over out_dir.
Nandana Dutt54dbd672019-01-11 12:58:05 +0000412 android::base::unique_fd bugreport_fd;
413 // File descriptor to screenshot file.
Nandana Dutt54dbd672019-01-11 12:58:05 +0000414 android::base::unique_fd screenshot_fd;
mhasank3a4cfb42020-06-15 18:06:43 -0700415 // Custom output directory.
416 std::string out_dir;
Abhijeet Kaure370d682019-10-01 16:49:30 +0100417 // Bugreport mode of the bugreport.
418 std::string bugreport_mode;
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100419 // Command-line arguments as string
420 std::string args;
421 // Notification title and description
422 std::string notification_title;
423 std::string notification_description;
424
Abhijeet Kaure370d682019-10-01 16:49:30 +0100425 /* Initializes options from commandline arguments. */
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100426 RunStatus Initialize(int argc, char* argv[]);
427
Nandana Dutt58d72e22018-11-16 10:30:48 +0000428 /* Initializes options from the requested mode. */
Nandana Dutt54dbd672019-01-11 12:58:05 +0000429 void Initialize(BugreportMode bugreport_mode, const android::base::unique_fd& bugreport_fd,
Paul Changf59c2b72020-03-10 02:08:55 +0800430 const android::base::unique_fd& screenshot_fd,
431 bool is_screenshot_requested);
Nandana Dutt58d72e22018-11-16 10:30:48 +0000432
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100433 /* Returns true if the options set so far are consistent. */
434 bool ValidateOptions() const;
Nandana Dutt9a76d202019-01-21 15:56:48 +0000435
436 /* Returns if options specified require writing bugreport to a file */
437 bool OutputToFile() const {
438 // If we are not writing to socket, we will write to a file. If bugreport_fd is
439 // specified, it is preferred. If not bugreport is written to /bugreports.
440 return !use_socket;
441 }
mhasank2d75c442020-06-11 15:05:25 -0700442
443 /* Returns if options specified require writing to custom file location */
444 bool OutputToCustomFile() {
445 // Custom location is only honored in limited mode.
mhasank3a4cfb42020-06-15 18:06:43 -0700446 return limited_only && !out_dir.empty() && bugreport_fd.get() == -1;
mhasank2d75c442020-06-11 15:05:25 -0700447 }
Nandana Dutt3f8c7172018-09-25 12:01:54 +0100448 };
449
450 // TODO: initialize fields on constructor
Felipe Lemee844a9d2016-09-21 15:01:39 -0700451 // dumpstate id - unique after each device reboot.
Felipe Leme7447d7c2016-11-03 18:12:22 -0700452 uint32_t id_;
Felipe Lemee844a9d2016-09-21 15:01:39 -0700453
Felipe Leme75876a22016-10-27 16:31:27 -0700454 // dumpstate pid
455 pid_t pid_;
456
Nandana Dutt3f8c7172018-09-25 12:01:54 +0100457 // Runtime options.
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100458 std::unique_ptr<DumpOptions> options_;
Felipe Lemee844a9d2016-09-21 15:01:39 -0700459
Nandana Dutt402a8392019-06-14 14:25:13 +0100460 // Last progress that was sent to the listener [0-100].
461 int last_reported_percent_progress_ = 0;
Felipe Leme009ecbb2016-11-07 10:18:44 -0800462
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700463 // Whether it should take an screenshot earlier in the process.
Felipe Leme9a523ae2016-10-20 15:10:33 -0700464 bool do_early_screenshot_ = false;
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700465
Felipe Leme7447d7c2016-11-03 18:12:22 -0700466 std::unique_ptr<Progress> progress_;
Felipe Lemee844a9d2016-09-21 15:01:39 -0700467
468 // When set, defines a socket file-descriptor use to report progress to bugreportz.
Felipe Leme9a523ae2016-10-20 15:10:33 -0700469 int control_socket_fd_ = -1;
Felipe Lemee844a9d2016-09-21 15:01:39 -0700470
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700471 // Bugreport format version;
Felipe Lemed071c682016-10-20 16:48:00 -0700472 std::string version_ = VERSION_CURRENT;
Felipe Lemee844a9d2016-09-21 15:01:39 -0700473
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700474 time_t now_;
475
Felipe Leme2b9b06c2016-10-14 09:13:06 -0700476 // Base name (without suffix or extensions) of the bugreport files, typically
477 // `bugreport-BUILD_ID`.
Felipe Leme9a523ae2016-10-20 15:10:33 -0700478 std::string base_name_;
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700479
Felipe Leme2b9b06c2016-10-14 09:13:06 -0700480 // Name is the suffix part of the bugreport files - it's typically the date (when invoked with
481 // `-d`), but it could be changed by the user..
482 std::string name_;
483
Nandana Dutt979388e2018-11-30 16:48:55 +0000484 std::string bugreport_internal_dir_ = DUMPSTATE_DIRECTORY;
485
486 // Full path of the temporary file containing the bugreport, inside bugreport_internal_dir_.
487 // At the very end this file is pulled into the zip file.
Felipe Leme9a523ae2016-10-20 15:10:33 -0700488 std::string tmp_path_;
Felipe Leme1d486fe2016-10-14 18:06:47 -0700489
Nandana Dutt979388e2018-11-30 16:48:55 +0000490 // Full path of the file containing the dumpstate logs, inside bugreport_internal_dir_.
491 // This is useful for debugging.
Felipe Leme9a523ae2016-10-20 15:10:33 -0700492 std::string log_path_;
Felipe Leme1d486fe2016-10-14 18:06:47 -0700493
Nandana Dutt979388e2018-11-30 16:48:55 +0000494 // Full path of the bugreport file, be it zip or text, inside bugreport_internal_dir_.
Felipe Leme9a523ae2016-10-20 15:10:33 -0700495 std::string path_;
Felipe Leme1d486fe2016-10-14 18:06:47 -0700496
Nandana Duttf531f4b2019-06-14 12:20:03 +0100497 // Full path of the file containing the screenshot (when requested).
Nandana Dutt979388e2018-11-30 16:48:55 +0000498 std::string screenshot_path_;
499
Felipe Leme1d486fe2016-10-14 18:06:47 -0700500 // Pointer to the zipped file.
501 std::unique_ptr<FILE, int (*)(FILE*)> zip_file{nullptr, fclose};
502
Felipe Lemec6bc8bc2016-10-27 15:58:27 -0700503 // Pointer to the zip structure.
504 std::unique_ptr<ZipWriter> zip_writer_;
505
Vishnu Nair20cf5032018-01-05 13:15:49 -0800506 // Binder object listening to progress.
Felipe Leme75876a22016-10-27 16:31:27 -0700507 android::sp<android::os::IDumpstateListener> listener_;
Felipe Leme75876a22016-10-27 16:31:27 -0700508
Luis Hector Chavez91c2ae52018-03-14 15:12:46 -0700509 // List of open tombstone dump files.
510 std::vector<DumpData> tombstone_data_;
511
512 // List of open ANR dump files.
513 std::vector<DumpData> anr_data_;
514
Rhed Jao1c855122020-07-16 17:37:39 +0800515 // A thread pool to execute dump tasks simultaneously if the parallel run is enabled.
516 std::unique_ptr<android::os::dumpstate::DumpPool> dump_pool_;
517
Rhed Jao4875aa62020-07-20 17:46:29 +0800518 // A task queue to collect adding zip entry tasks inside dump tasks if the
519 // parallel run is enabled.
520 std::unique_ptr<android::os::dumpstate::TaskQueue> zip_entry_tasks_;
521
Nandana Duttd2f5f082019-01-18 17:13:52 +0000522 // A callback to IncidentCompanion service, which checks user consent for sharing the
523 // bugreport with the calling app. If the user has not responded yet to the dialog it will
524 // be neither confirmed nor denied.
525 class ConsentCallback : public android::os::BnIncidentAuthListener {
526 public:
527 ConsentCallback();
528 android::binder::Status onReportApproved() override;
529 android::binder::Status onReportDenied() override;
530
531 enum ConsentResult { APPROVED, DENIED, UNAVAILABLE };
532
533 ConsentResult getResult();
534
535 // Returns the time since creating this listener
536 uint64_t getElapsedTimeMs() const;
537
538 private:
539 ConsentResult result_;
540 uint64_t start_time_;
541 std::mutex lock_;
542 };
543
Felipe Lemee844a9d2016-09-21 15:01:39 -0700544 private:
Nandana Duttd2f5f082019-01-18 17:13:52 +0000545 RunStatus RunInternal(int32_t calling_uid, const std::string& calling_package);
546
Jichao Lie89d9c12019-11-21 19:02:51 -0800547 RunStatus DumpstateDefaultAfterCritical();
548
Paul Chang0d2aad72020-02-13 20:04:03 +0800549 void MaybeTakeEarlyScreenshot();
550
Paul Changc490e662020-04-11 18:14:09 +0800551 void onUiIntensiveBugreportDumpsFinished(int32_t calling_uid,
552 const std::string& calling_package);
553
Jichao Lie89d9c12019-11-21 19:02:51 -0800554 void MaybeCheckUserConsent(int32_t calling_uid, const std::string& calling_package);
Nandana Duttd2f5f082019-01-18 17:13:52 +0000555
556 // Removes the in progress files output files (tmp file, zip/txt file, screenshot),
557 // but leaves the log file alone.
Nandana Dutt8ae16e62020-03-27 10:20:22 +0000558 void CleanupTmpFiles();
Nandana Duttd2f5f082019-01-18 17:13:52 +0000559
Rhed Jao1c855122020-07-16 17:37:39 +0800560 // Create the thread pool to enable the parallel run function.
561 void EnableParallelRunIfNeeded();
562 void ShutdownDumpPool();
563
Nandana Duttd2f5f082019-01-18 17:13:52 +0000564 RunStatus HandleUserConsentDenied();
565
Abhijeet Kaur3172b532019-10-15 15:07:03 +0100566 // Copies bugreport artifacts over to the caller's directories provided there is user consent or
567 // called by Shell.
568 RunStatus CopyBugreportIfUserConsented(int32_t calling_uid);
Nandana Duttbabf6c72019-01-15 14:11:12 +0000569
Felipe Lemed80e6b62016-10-03 13:08:14 -0700570 // Used by GetInstance() only.
Chih-Hung Hsiehd0937e62018-12-20 15:43:57 -0800571 explicit Dumpstate(const std::string& version = VERSION_CURRENT);
Felipe Leme062926e2016-10-27 15:51:12 -0700572
Nandana Duttd2f5f082019-01-18 17:13:52 +0000573 android::sp<ConsentCallback> consent_callback_;
574
Felipe Leme062926e2016-10-27 15:51:12 -0700575 DISALLOW_COPY_AND_ASSIGN(Dumpstate);
Felipe Lemee844a9d2016-09-21 15:01:39 -0700576};
577
578// for_each_pid_func = void (*)(int, const char*);
579// for_each_tid_func = void (*)(int, int, const char*);
580
581typedef void(for_each_pid_func)(int, const char*);
582typedef void(for_each_tid_func)(int, int, const char*);
Felipe Leme71ca15e2016-05-19 16:18:17 -0700583
Felipe Leme71a74ac2016-03-17 15:43:25 -0700584/* saves the the contents of a file as a long */
585int read_file_as_long(const char *path, long int *output);
586
Christopher Ferris54bcc5f2015-02-10 12:15:01 -0800587/* prints the contents of the fd
588 * fd must have been opened with the flag O_NONBLOCK.
589 */
Christopher Ferris1fe61072014-07-22 16:08:19 -0700590int dump_file_from_fd(const char *title, const char *path, int fd);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700591
Mark Salyzyn326842f2015-04-30 09:49:41 -0700592/* calls skip to gate calling dump_from_fd recursively
593 * in the specified directory. dump_from_fd defaults to
594 * dump_file_from_fd above when set to NULL. skip defaults
595 * to false when set to NULL. dump_from_fd will always be
596 * called with title NULL.
597 */
Felipe Leme678727a2016-09-21 17:22:11 -0700598int dump_files(const std::string& title, const char* dir, bool (*skip)(const char* path),
599 int (*dump_from_fd)(const char* title, const char* path, int fd));
Mark Salyzyn326842f2015-04-30 09:49:41 -0700600
Felipe Leme2628e9e2016-04-12 16:36:51 -0700601/** opens a socket and returns its file descriptor */
602int open_socket(const char *service);
603
Nandana Dutta344cb62019-02-22 15:12:35 +0000604/*
605 * Redirects 'redirect' to a service control socket.
606 *
607 * Returns true if redirect succeeds.
608 */
609bool redirect_to_socket(FILE* redirect, const char* service);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700610
Nandana Dutta344cb62019-02-22 15:12:35 +0000611/*
612 * Redirects 'redirect' to a file indicated by 'path', truncating it.
613 *
614 * Returns true if redirect succeeds.
615 */
616bool redirect_to_file(FILE* redirect, char* path);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700617
Nandana Dutta344cb62019-02-22 15:12:35 +0000618/*
619 * Redirects 'redirect' to an existing file indicated by 'path', appending it.
620 *
621 * Returns true if redirect succeeds.
622 */
623bool redirect_to_existing_file(FILE* redirect, char* path);
Felipe Leme0f3fb202016-06-10 17:10:53 -0700624
Felipe Leme111b9d02016-02-03 09:28:24 -0800625/* create leading directories, if necessary */
626void create_parent_dirs(const char *path);
627
Colin Crossf45fa6b2012-03-26 12:38:26 -0700628/* for each process in the system, run the specified function */
Colin Cross0c22e8b2012-11-02 15:46:56 -0700629void for_each_pid(for_each_pid_func func, const char *header);
630
631/* for each thread in the system, run the specified function */
632void for_each_tid(for_each_tid_func func, const char *header);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700633
634/* Displays a blocked processes in-kernel wait channel */
Colin Cross0c22e8b2012-11-02 15:46:56 -0700635void show_wchan(int pid, int tid, const char *name);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700636
Mark Salyzyna297c322016-02-05 15:33:17 -0800637/* Displays a processes times */
638void show_showtime(int pid, const char *name);
639
Colin Crossf45fa6b2012-03-26 12:38:26 -0700640/* Runs "showmap" for a process */
641void do_showmap(int pid, const char *name);
642
643/* Gets the dmesg output for the kernel */
644void do_dmesg();
645
Sreeram Ramachandran2b3bba32014-07-08 15:40:55 -0700646/* Prints the contents of all the routing tables, both IPv4 and IPv6. */
647void dump_route_tables();
648
Colin Crossf45fa6b2012-03-26 12:38:26 -0700649/* Play a sound via Stagefright */
Christopher Ferris1fe61072014-07-22 16:08:19 -0700650void play_sound(const char *path);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700651
Felipe Leme0c80cf02016-01-05 13:25:34 -0800652/* Checks if a given path is a directory. */
653bool is_dir(const char* pathname);
654
655/** Gets the last modification time of a file, or default time if file is not found. */
656time_t get_mtime(int fd, time_t default_mtime);
657
Calvin On249beee2016-06-03 15:17:07 -0700658/** Gets command-line arguments. */
Felipe Lemea34efb72016-03-11 09:33:32 -0800659void format_args(int argc, const char *argv[], std::string *args);
Felipe Leme88c79332016-02-22 11:06:49 -0800660
Vishnu Nair20cf5032018-01-05 13:15:49 -0800661/** Main entry point for dumpstate. */
662int run_main(int argc, char* argv[]);
663
Felipe Leme8620bb42015-11-10 11:04:45 -0800664#ifdef __cplusplus
665}
666#endif
667
Felipe Leme8268ed22016-08-02 18:18:25 -0700668#endif /* FRAMEWORK_NATIVE_CMD_DUMPSTATE_H_ */