blob: c326bb6bfa964101cc03b09a5e21970ad6d7e0eb [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>
Nandana Duttd2f5f082019-01-18 17:13:52 +000030#include <android/os/BnIncidentAuthListener.h>
Nandana Dutt197661d2018-11-16 16:40:21 +000031#include <android/os/IDumpstate.h>
Jiyong Parkb22e65d2017-06-23 21:23:16 +090032#include <android/os/IDumpstateListener.h>
33#include <utils/StrongPointer.h>
Felipe Lemec6bc8bc2016-10-27 15:58:27 -070034#include <ziparchive/zip_writer.h>
35
Felipe Lemebda15a02016-11-16 17:48:25 -080036#include "DumpstateUtil.h"
Felipe Leme75876a22016-10-27 16:31:27 -070037
Felipe Leme35c94f32016-08-12 10:51:54 -070038// Workaround for const char *args[MAX_ARGS_ARRAY_SIZE] variables until they're converted to
39// std::vector<std::string>
Felipe Leme30dbfa12016-09-02 12:43:26 -070040// TODO: remove once not used
Felipe Leme35c94f32016-08-12 10:51:54 -070041#define MAX_ARGS_ARRAY_SIZE 1000
42
Felipe Leme47e9be22016-12-21 15:37:07 -080043// TODO: move everything under this namespace
44// TODO: and then remove explicitly android::os::dumpstate:: prefixes
45namespace android {
46namespace os {
Nandana Dutt58d72e22018-11-16 10:30:48 +000047
48struct DumpstateOptions;
49
Felipe Leme47e9be22016-12-21 15:37:07 -080050namespace dumpstate {
51
52class DumpstateTest;
53class ProgressTest;
54
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:
Chih-Hung Hsiehd0937e62018-12-20 15:43:57 -080076 explicit DurationReporter(const std::string& title, bool log_only = false);
Felipe Leme30dbfa12016-09-02 12:43:26 -070077
78 ~DurationReporter();
79
Felipe Leme30dbfa12016-09-02 12:43:26 -070080 private:
Felipe Leme678727a2016-09-21 17:22:11 -070081 std::string title_;
Felipe Leme46b85da2016-11-21 17:40:45 -080082 bool log_only_;
Felipe Lemeb0f669d2016-09-26 18:26:11 -070083 uint64_t started_;
Felipe Leme062926e2016-10-27 15:51:12 -070084
85 DISALLOW_COPY_AND_ASSIGN(DurationReporter);
Felipe Leme30dbfa12016-09-02 12:43:26 -070086};
87
88/*
Felipe Leme7447d7c2016-11-03 18:12:22 -070089 * Keeps track of current progress and estimated max, saving stats on file to tune up future runs.
Felipe Leme71bbfc52015-11-23 14:14:51 -080090 *
Felipe Leme7447d7c2016-11-03 18:12:22 -070091 * Each `dumpstate` section contributes to the total weight by an individual weight, so the overall
92 * progress can be calculated by dividing the estimate max progress by the current progress.
Felipe Leme71bbfc52015-11-23 14:14:51 -080093 *
Felipe Leme7447d7c2016-11-03 18:12:22 -070094 * The estimated max progress is initially set to a value (`kDefaultMax) defined empirically, but
95 * it's adjusted after each dumpstate run by storing the average duration in a file.
Felipe Lemead5f6c42015-11-30 14:26:46 -080096 *
Felipe Leme71bbfc52015-11-23 14:14:51 -080097 */
Felipe Leme7447d7c2016-11-03 18:12:22 -070098class Progress {
Felipe Leme47e9be22016-12-21 15:37:07 -080099 friend class android::os::dumpstate::ProgressTest;
100 friend class android::os::dumpstate::DumpstateTest;
Felipe Leme7447d7c2016-11-03 18:12:22 -0700101
102 public:
103 /*
104 * Default estimation of the max duration of a bugreport generation.
105 *
106 * It does not need to match the exact sum of all sections, but ideally it should to be slight
107 * more than such sum: a value too high will cause the bugreport to finish before the user
108 * expected (for example, jumping from 70% to 100%), while a value too low will cause the
109 * progress to get stuck at an almost-finished value (like 99%) for a while.
110 *
111 * This constant is only used when the average duration from previous runs cannot be used.
112 */
113 static const int kDefaultMax;
114
Chih-Hung Hsiehd0937e62018-12-20 15:43:57 -0800115 explicit Progress(const std::string& path = "");
Felipe Leme7447d7c2016-11-03 18:12:22 -0700116
117 // Gets the current progress.
118 int32_t Get() const;
119
120 // Gets the current estimated max progress.
121 int32_t GetMax() const;
122
123 // Gets the initial estimated max progress.
124 int32_t GetInitialMax() const;
125
126 // Increments progress (ignored if not positive).
127 // Returns `true` if the max progress increased as well.
128 bool Inc(int32_t delta);
129
130 // Persist the stats.
131 void Save();
132
133 void Dump(int fd, const std::string& prefix) const;
134
135 private:
136 Progress(int32_t initial_max, float growth_factor,
137 const std::string& path = ""); // Used by test cases.
138 Progress(int32_t initial_max, int32_t progress, float growth_factor); // Used by test cases.
139 void Load();
140 int32_t initial_max_;
141 int32_t progress_;
142 int32_t max_;
143 float growth_factor_;
144 int32_t n_runs_;
145 int32_t average_max_;
Nandana Dutt979388e2018-11-30 16:48:55 +0000146 std::string path_;
Felipe Leme7447d7c2016-11-03 18:12:22 -0700147};
Felipe Leme71bbfc52015-11-23 14:14:51 -0800148
Felipe Leme71bbfc52015-11-23 14:14:51 -0800149/*
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700150 * List of supported zip format versions.
151 *
152 * See bugreport-format.md for more info.
153 */
Vishnu Nair64afc022018-02-01 15:29:34 -0800154static std::string VERSION_CURRENT = "2.0";
Felipe Lemed071c682016-10-20 16:48:00 -0700155
156/*
Felipe Lemee184f662016-10-27 10:04:47 -0700157 * Temporary version that adds a anr-traces.txt entry. Once tools support it, the current version
Vishnu Nair64afc022018-02-01 15:29:34 -0800158 * will be bumped to 3.0.
Felipe Lemee184f662016-10-27 10:04:47 -0700159 */
Vishnu Nair64afc022018-02-01 15:29:34 -0800160static std::string VERSION_SPLIT_ANR = "3.0-dev-split-anr";
Felipe Lemee184f662016-10-27 10:04:47 -0700161
162/*
Felipe Lemed071c682016-10-20 16:48:00 -0700163 * "Alias" for the current version.
164 */
165static std::string VERSION_DEFAULT = "default";
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700166
167/*
Nandana Dutt979388e2018-11-30 16:48:55 +0000168 * Directory used by Dumpstate binary to keep its local files.
169 */
170static const std::string DUMPSTATE_DIRECTORY = "/bugreports";
171
172/*
Luis Hector Chavez91c2ae52018-03-14 15:12:46 -0700173 * Structure that contains the information of an open dump file.
174 */
175struct DumpData {
176 // Path of the file.
177 std::string name;
178
179 // Open file descriptor for the file.
180 android::base::unique_fd fd;
181
182 // Modification time of the file.
183 time_t mtime;
184};
185
186/*
Felipe Lemee844a9d2016-09-21 15:01:39 -0700187 * Main class driving a bugreport generation.
188 *
189 * Currently, it only contains variables that are accessed externally, but gradually the functions
190 * that are spread accross utils.cpp and dumpstate.cpp will be moved to it.
191 */
192class Dumpstate {
Felipe Leme4c2d6632016-09-28 14:32:00 -0700193 friend class DumpstateTest;
194
Felipe Lemee844a9d2016-09-21 15:01:39 -0700195 public:
Nandana Duttd2f5f082019-01-18 17:13:52 +0000196 enum RunStatus { OK, HELP, INVALID_INPUT, ERROR, USER_CONSENT_DENIED, USER_CONSENT_TIMED_OUT };
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100197
Nandana Dutt58d72e22018-11-16 10:30:48 +0000198 // The mode under which the bugreport should be run. Each mode encapsulates a few options.
199 enum BugreportMode {
Nandana Dutt197661d2018-11-16 16:40:21 +0000200 BUGREPORT_FULL = android::os::IDumpstate::BUGREPORT_MODE_FULL,
201 BUGREPORT_INTERACTIVE = android::os::IDumpstate::BUGREPORT_MODE_INTERACTIVE,
202 BUGREPORT_REMOTE = android::os::IDumpstate::BUGREPORT_MODE_REMOTE,
203 BUGREPORT_WEAR = android::os::IDumpstate::BUGREPORT_MODE_WEAR,
204 BUGREPORT_TELEPHONY = android::os::IDumpstate::BUGREPORT_MODE_TELEPHONY,
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000205 BUGREPORT_WIFI = android::os::IDumpstate::BUGREPORT_MODE_WIFI,
206 BUGREPORT_DEFAULT = android::os::IDumpstate::BUGREPORT_MODE_DEFAULT
Nandana Dutt58d72e22018-11-16 10:30:48 +0000207 };
208
Felipe Leme47e9be22016-12-21 15:37:07 -0800209 static android::os::dumpstate::CommandOptions DEFAULT_DUMPSYS;
Felipe Lemebda15a02016-11-16 17:48:25 -0800210
Felipe Lemee844a9d2016-09-21 15:01:39 -0700211 static Dumpstate& GetInstance();
Felipe Leme71bbfc52015-11-23 14:14:51 -0800212
Felipe Leme1d486fe2016-10-14 18:06:47 -0700213 /* Checkes whether dumpstate is generating a zipped bugreport. */
214 bool IsZipping() const;
215
Felipe Leme6ad9c062016-09-28 11:58:36 -0700216 /*
Felipe Leme678727a2016-09-21 17:22:11 -0700217 * Forks a command, waits for it to finish, and returns its status.
218 *
219 * |title| description of the command printed on `stdout` (or empty to skip
220 * description).
221 * |full_command| array containing the command (first entry) and its arguments.
222 * Must contain at least one element.
223 * |options| optional argument defining the command's behavior.
224 */
225 int RunCommand(const std::string& title, const std::vector<std::string>& fullCommand,
Felipe Leme47e9be22016-12-21 15:37:07 -0800226 const android::os::dumpstate::CommandOptions& options =
227 android::os::dumpstate::CommandOptions::DEFAULT);
Felipe Leme678727a2016-09-21 17:22:11 -0700228
229 /*
230 * Runs `dumpsys` with the given arguments, automatically setting its timeout
Vishnu Nair6921f802017-11-22 09:17:23 -0800231 * (`-T` argument)
Felipe Leme678727a2016-09-21 17:22:11 -0700232 * according to the command options.
233 *
234 * |title| description of the command printed on `stdout` (or empty to skip
235 * description).
236 * |dumpsys_args| `dumpsys` arguments (except `-t`).
237 * |options| optional argument defining the command's behavior.
Vishnu Nair6921f802017-11-22 09:17:23 -0800238 * |dumpsys_timeout| when > 0, defines the value passed to `dumpsys -T` (otherwise it uses the
Felipe Leme6ad9c062016-09-28 11:58:36 -0700239 * timeout from `options`)
Felipe Leme678727a2016-09-21 17:22:11 -0700240 */
Felipe Leme9a523ae2016-10-20 15:10:33 -0700241 void RunDumpsys(const std::string& title, const std::vector<std::string>& dumpsys_args,
Felipe Leme47e9be22016-12-21 15:37:07 -0800242 const android::os::dumpstate::CommandOptions& options = DEFAULT_DUMPSYS,
Vishnu Nair6921f802017-11-22 09:17:23 -0800243 long dumpsys_timeout_ms = 0);
Felipe Leme678727a2016-09-21 17:22:11 -0700244
245 /*
246 * Prints the contents of a file.
247 *
248 * |title| description of the command printed on `stdout` (or empty to skip
249 * description).
250 * |path| location of the file to be dumped.
251 */
252 int DumpFile(const std::string& title, const std::string& path);
253
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700254 /*
Felipe Leme1d486fe2016-10-14 18:06:47 -0700255 * Adds a new entry to the existing zip file.
256 * */
257 bool AddZipEntry(const std::string& entry_name, const std::string& entry_path);
258
259 /*
260 * Adds a new entry to the existing zip file.
Vishnu Naire97d6122018-01-18 13:58:56 -0800261 *
262 * |entry_name| destination path of the new entry.
263 * |fd| file descriptor to read from.
264 * |timeout| timeout to terminate the read if not completed. Set
265 * value of 0s (default) to disable timeout.
Felipe Leme1d486fe2016-10-14 18:06:47 -0700266 */
Vishnu Naire97d6122018-01-18 13:58:56 -0800267 android::status_t AddZipEntryFromFd(const std::string& entry_name, int fd,
268 std::chrono::milliseconds timeout);
Felipe Leme1d486fe2016-10-14 18:06:47 -0700269
270 /*
Nandana Dutta7db6342018-11-21 14:53:34 +0000271 * Adds a text entry to the existing zip file.
Felipe Leme1d486fe2016-10-14 18:06:47 -0700272 */
273 bool AddTextZipEntry(const std::string& entry_name, const std::string& content);
274
275 /*
276 * Adds all files from a directory to the zipped bugreport file.
277 */
278 void AddDir(const std::string& dir, bool recursive);
279
280 /*
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700281 * Takes a screenshot and save it to the given `path`.
282 *
283 * If `path` is empty, uses a standard path based on the bugreport name.
284 */
285 void TakeScreenshot(const std::string& path = "");
286
Felipe Leme7447d7c2016-11-03 18:12:22 -0700287 /////////////////////////////////////////////////////////////////////
288 // TODO: members below should be private once refactor is finished //
289 /////////////////////////////////////////////////////////////////////
290
291 // TODO: temporary method until Dumpstate object is properly set
292 void SetProgress(std::unique_ptr<Progress> progress);
Felipe Lemed80e6b62016-10-03 13:08:14 -0700293
Felipe Leme6f674ae2016-11-18 17:10:33 -0800294 void DumpstateBoard();
295
Felipe Lemed80e6b62016-10-03 13:08:14 -0700296 /*
297 * Updates the overall progress of the bugreport generation by the given weight increment.
298 */
Felipe Leme7447d7c2016-11-03 18:12:22 -0700299 void UpdateProgress(int32_t delta);
Felipe Lemed80e6b62016-10-03 13:08:14 -0700300
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700301 /* Prints the dumpstate header on `stdout`. */
Felipe Leme2b9b06c2016-10-14 09:13:06 -0700302 void PrintHeader() const;
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700303
Felipe Leme1d486fe2016-10-14 18:06:47 -0700304 /*
305 * Adds the temporary report to the existing .zip file, closes the .zip file, and removes the
306 * temporary file.
307 */
308 bool FinishZipFile();
309
Nandana Dutt979388e2018-11-30 16:48:55 +0000310 /* Constructs a full path inside directory with file name formatted using the given suffix. */
311 std::string GetPath(const std::string& directory, const std::string& suffix) const;
312
313 /* Constructs a full path inside bugreport_internal_dir_ with file name formatted using the
314 * given suffix. */
Felipe Leme2b9b06c2016-10-14 09:13:06 -0700315 std::string GetPath(const std::string& suffix) const;
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700316
Vishnu Nair780b1282017-10-10 13:57:24 -0700317 /* Returns true if the current version supports priority dump feature. */
318 bool CurrentVersionSupportsPriorityDumps() const;
319
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100320 struct DumpOptions;
Felipe Lemee844a9d2016-09-21 15:01:39 -0700321
Nandana Dutt197661d2018-11-16 16:40:21 +0000322 /* Main entry point for running a complete bugreport. */
Nandana Duttd2f5f082019-01-18 17:13:52 +0000323 RunStatus Run(int32_t calling_uid, const std::string& calling_package);
Nandana Dutt197661d2018-11-16 16:40:21 +0000324
Nandana Duttf02564e2019-02-15 15:24:24 +0000325 RunStatus ParseCommandlineAndRun(int argc, char* argv[]);
326
Nandana Dutt197661d2018-11-16 16:40:21 +0000327 /* Sets runtime options. */
328 void SetOptions(std::unique_ptr<DumpOptions> options);
Nandana Dutt3f8c7172018-09-25 12:01:54 +0100329
Nandana Dutt3f8c7172018-09-25 12:01:54 +0100330 /*
331 * Structure to hold options that determine the behavior of dumpstate.
332 */
333 struct DumpOptions {
334 bool do_add_date = false;
335 bool do_zip_file = false;
336 bool do_vibrate = true;
Nandana Dutt9a76d202019-01-21 15:56:48 +0000337 // Writes bugreport content to a socket; only flatfile format is supported.
Nandana Dutt3f8c7172018-09-25 12:01:54 +0100338 bool use_socket = false;
339 bool use_control_socket = false;
340 bool do_fb = false;
341 bool do_broadcast = false;
342 bool is_remote_mode = false;
343 bool show_header_only = false;
344 bool do_start_service = false;
345 bool telephony_only = false;
346 bool wifi_only = false;
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100347 // Whether progress updates should be published.
348 bool do_progress_updates = false;
Nandana Dutt9a76d202019-01-21 15:56:48 +0000349 // File descriptor to output zip file.
Nandana Dutt54dbd672019-01-11 12:58:05 +0000350 android::base::unique_fd bugreport_fd;
351 // File descriptor to screenshot file.
Nandana Dutt54dbd672019-01-11 12:58:05 +0000352 android::base::unique_fd screenshot_fd;
Nandana Dutt58d72e22018-11-16 10:30:48 +0000353 // TODO: rename to MODE.
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100354 // Extra options passed as system property.
355 std::string extra_options;
356 // Command-line arguments as string
357 std::string args;
358 // Notification title and description
359 std::string notification_title;
360 std::string notification_description;
361
362 /* Initializes options from commandline arguments and system properties. */
363 RunStatus Initialize(int argc, char* argv[]);
364
Nandana Dutt58d72e22018-11-16 10:30:48 +0000365 /* Initializes options from the requested mode. */
Nandana Dutt54dbd672019-01-11 12:58:05 +0000366 void Initialize(BugreportMode bugreport_mode, const android::base::unique_fd& bugreport_fd,
367 const android::base::unique_fd& screenshot_fd);
Nandana Dutt58d72e22018-11-16 10:30:48 +0000368
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100369 /* Returns true if the options set so far are consistent. */
370 bool ValidateOptions() const;
Nandana Dutt9a76d202019-01-21 15:56:48 +0000371
372 /* Returns if options specified require writing bugreport to a file */
373 bool OutputToFile() const {
374 // If we are not writing to socket, we will write to a file. If bugreport_fd is
375 // specified, it is preferred. If not bugreport is written to /bugreports.
376 return !use_socket;
377 }
Nandana Dutt3f8c7172018-09-25 12:01:54 +0100378 };
379
380 // TODO: initialize fields on constructor
Felipe Lemee844a9d2016-09-21 15:01:39 -0700381 // dumpstate id - unique after each device reboot.
Felipe Leme7447d7c2016-11-03 18:12:22 -0700382 uint32_t id_;
Felipe Lemee844a9d2016-09-21 15:01:39 -0700383
Felipe Leme75876a22016-10-27 16:31:27 -0700384 // dumpstate pid
385 pid_t pid_;
386
Nandana Dutt3f8c7172018-09-25 12:01:54 +0100387 // Runtime options.
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100388 std::unique_ptr<DumpOptions> options_;
Felipe Lemee844a9d2016-09-21 15:01:39 -0700389
Felipe Leme009ecbb2016-11-07 10:18:44 -0800390 // How frequently the progess should be updated;the listener will only be notificated when the
391 // delta from the previous update is more than the threshold.
392 int32_t update_progress_threshold_ = 100;
393
394 // Last progress that triggered a listener updated
395 int32_t last_updated_progress_;
396
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700397 // Whether it should take an screenshot earlier in the process.
Felipe Leme9a523ae2016-10-20 15:10:33 -0700398 bool do_early_screenshot_ = false;
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700399
Felipe Leme7447d7c2016-11-03 18:12:22 -0700400 std::unique_ptr<Progress> progress_;
Felipe Lemee844a9d2016-09-21 15:01:39 -0700401
402 // When set, defines a socket file-descriptor use to report progress to bugreportz.
Felipe Leme9a523ae2016-10-20 15:10:33 -0700403 int control_socket_fd_ = -1;
Felipe Lemee844a9d2016-09-21 15:01:39 -0700404
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700405 // Bugreport format version;
Felipe Lemed071c682016-10-20 16:48:00 -0700406 std::string version_ = VERSION_CURRENT;
Felipe Lemee844a9d2016-09-21 15:01:39 -0700407
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700408 time_t now_;
409
Felipe Leme2b9b06c2016-10-14 09:13:06 -0700410 // Base name (without suffix or extensions) of the bugreport files, typically
411 // `bugreport-BUILD_ID`.
Felipe Leme9a523ae2016-10-20 15:10:33 -0700412 std::string base_name_;
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700413
Felipe Leme2b9b06c2016-10-14 09:13:06 -0700414 // Name is the suffix part of the bugreport files - it's typically the date (when invoked with
415 // `-d`), but it could be changed by the user..
416 std::string name_;
417
Nandana Dutt979388e2018-11-30 16:48:55 +0000418 std::string bugreport_internal_dir_ = DUMPSTATE_DIRECTORY;
419
420 // Full path of the temporary file containing the bugreport, inside bugreport_internal_dir_.
421 // At the very end this file is pulled into the zip file.
Felipe Leme9a523ae2016-10-20 15:10:33 -0700422 std::string tmp_path_;
Felipe Leme1d486fe2016-10-14 18:06:47 -0700423
Nandana Dutt979388e2018-11-30 16:48:55 +0000424 // Full path of the file containing the dumpstate logs, inside bugreport_internal_dir_.
425 // This is useful for debugging.
Felipe Leme9a523ae2016-10-20 15:10:33 -0700426 std::string log_path_;
Felipe Leme1d486fe2016-10-14 18:06:47 -0700427
Nandana Dutt979388e2018-11-30 16:48:55 +0000428 // Full path of the bugreport file, be it zip or text, inside bugreport_internal_dir_.
Felipe Leme9a523ae2016-10-20 15:10:33 -0700429 std::string path_;
Felipe Leme1d486fe2016-10-14 18:06:47 -0700430
Nandana Dutt979388e2018-11-30 16:48:55 +0000431 // TODO: If temporary this should be removed at the end.
432 // Full path of the temporary file containing the screenshot (when requested).
433 std::string screenshot_path_;
434
Felipe Leme1d486fe2016-10-14 18:06:47 -0700435 // Pointer to the zipped file.
436 std::unique_ptr<FILE, int (*)(FILE*)> zip_file{nullptr, fclose};
437
Felipe Lemec6bc8bc2016-10-27 15:58:27 -0700438 // Pointer to the zip structure.
439 std::unique_ptr<ZipWriter> zip_writer_;
440
Vishnu Nair20cf5032018-01-05 13:15:49 -0800441 // Binder object listening to progress.
Felipe Leme75876a22016-10-27 16:31:27 -0700442 android::sp<android::os::IDumpstateListener> listener_;
443 std::string listener_name_;
Vishnu Nair20cf5032018-01-05 13:15:49 -0800444 bool report_section_;
Felipe Leme75876a22016-10-27 16:31:27 -0700445
Luis Hector Chavez91c2ae52018-03-14 15:12:46 -0700446 // List of open tombstone dump files.
447 std::vector<DumpData> tombstone_data_;
448
449 // List of open ANR dump files.
450 std::vector<DumpData> anr_data_;
451
Nandana Duttd2f5f082019-01-18 17:13:52 +0000452 // A callback to IncidentCompanion service, which checks user consent for sharing the
453 // bugreport with the calling app. If the user has not responded yet to the dialog it will
454 // be neither confirmed nor denied.
455 class ConsentCallback : public android::os::BnIncidentAuthListener {
456 public:
457 ConsentCallback();
458 android::binder::Status onReportApproved() override;
459 android::binder::Status onReportDenied() override;
460
461 enum ConsentResult { APPROVED, DENIED, UNAVAILABLE };
462
463 ConsentResult getResult();
464
465 // Returns the time since creating this listener
466 uint64_t getElapsedTimeMs() const;
467
468 private:
469 ConsentResult result_;
470 uint64_t start_time_;
471 std::mutex lock_;
472 };
473
Felipe Lemee844a9d2016-09-21 15:01:39 -0700474 private:
Nandana Duttd2f5f082019-01-18 17:13:52 +0000475 RunStatus RunInternal(int32_t calling_uid, const std::string& calling_package);
476
477 void CheckUserConsent(int32_t calling_uid, const android::String16& calling_package);
478
479 // Removes the in progress files output files (tmp file, zip/txt file, screenshot),
480 // but leaves the log file alone.
481 void CleanupFiles();
482
483 RunStatus HandleUserConsentDenied();
484
485 // Copies bugreport artifacts over to the caller's directories provided there is user consent.
486 RunStatus CopyBugreportIfUserConsented();
Nandana Duttbabf6c72019-01-15 14:11:12 +0000487
Felipe Lemed80e6b62016-10-03 13:08:14 -0700488 // Used by GetInstance() only.
Chih-Hung Hsiehd0937e62018-12-20 15:43:57 -0800489 explicit Dumpstate(const std::string& version = VERSION_CURRENT);
Felipe Leme062926e2016-10-27 15:51:12 -0700490
Nandana Duttd2f5f082019-01-18 17:13:52 +0000491 android::sp<ConsentCallback> consent_callback_;
492
Felipe Leme062926e2016-10-27 15:51:12 -0700493 DISALLOW_COPY_AND_ASSIGN(Dumpstate);
Felipe Lemee844a9d2016-09-21 15:01:39 -0700494};
495
496// for_each_pid_func = void (*)(int, const char*);
497// for_each_tid_func = void (*)(int, int, const char*);
498
499typedef void(for_each_pid_func)(int, const char*);
500typedef void(for_each_tid_func)(int, int, const char*);
Felipe Leme71ca15e2016-05-19 16:18:17 -0700501
Felipe Leme71a74ac2016-03-17 15:43:25 -0700502/* saves the the contents of a file as a long */
503int read_file_as_long(const char *path, long int *output);
504
Christopher Ferris54bcc5f2015-02-10 12:15:01 -0800505/* prints the contents of the fd
506 * fd must have been opened with the flag O_NONBLOCK.
507 */
Christopher Ferris1fe61072014-07-22 16:08:19 -0700508int dump_file_from_fd(const char *title, const char *path, int fd);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700509
Mark Salyzyn326842f2015-04-30 09:49:41 -0700510/* calls skip to gate calling dump_from_fd recursively
511 * in the specified directory. dump_from_fd defaults to
512 * dump_file_from_fd above when set to NULL. skip defaults
513 * to false when set to NULL. dump_from_fd will always be
514 * called with title NULL.
515 */
Felipe Leme678727a2016-09-21 17:22:11 -0700516int dump_files(const std::string& title, const char* dir, bool (*skip)(const char* path),
517 int (*dump_from_fd)(const char* title, const char* path, int fd));
Mark Salyzyn326842f2015-04-30 09:49:41 -0700518
Felipe Leme2628e9e2016-04-12 16:36:51 -0700519/** opens a socket and returns its file descriptor */
520int open_socket(const char *service);
521
Nandana Dutta344cb62019-02-22 15:12:35 +0000522/*
523 * Redirects 'redirect' to a service control socket.
524 *
525 * Returns true if redirect succeeds.
526 */
527bool redirect_to_socket(FILE* redirect, const char* service);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700528
Nandana Dutta344cb62019-02-22 15:12:35 +0000529/*
530 * Redirects 'redirect' to a file indicated by 'path', truncating it.
531 *
532 * Returns true if redirect succeeds.
533 */
534bool redirect_to_file(FILE* redirect, char* path);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700535
Nandana Dutta344cb62019-02-22 15:12:35 +0000536/*
537 * Redirects 'redirect' to an existing file indicated by 'path', appending it.
538 *
539 * Returns true if redirect succeeds.
540 */
541bool redirect_to_existing_file(FILE* redirect, char* path);
Felipe Leme0f3fb202016-06-10 17:10:53 -0700542
Felipe Leme111b9d02016-02-03 09:28:24 -0800543/* create leading directories, if necessary */
544void create_parent_dirs(const char *path);
545
Jeff Brownbf7f4922012-06-07 16:40:01 -0700546/* dump Dalvik and native stack traces, return the trace file location (NULL if none) */
547const char *dump_traces();
Colin Crossf45fa6b2012-03-26 12:38:26 -0700548
549/* for each process in the system, run the specified function */
Colin Cross0c22e8b2012-11-02 15:46:56 -0700550void for_each_pid(for_each_pid_func func, const char *header);
551
552/* for each thread in the system, run the specified function */
553void for_each_tid(for_each_tid_func func, const char *header);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700554
555/* Displays a blocked processes in-kernel wait channel */
Colin Cross0c22e8b2012-11-02 15:46:56 -0700556void show_wchan(int pid, int tid, const char *name);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700557
Mark Salyzyna297c322016-02-05 15:33:17 -0800558/* Displays a processes times */
559void show_showtime(int pid, const char *name);
560
Colin Crossf45fa6b2012-03-26 12:38:26 -0700561/* Runs "showmap" for a process */
562void do_showmap(int pid, const char *name);
563
564/* Gets the dmesg output for the kernel */
565void do_dmesg();
566
Sreeram Ramachandran2b3bba32014-07-08 15:40:55 -0700567/* Prints the contents of all the routing tables, both IPv4 and IPv6. */
568void dump_route_tables();
569
Colin Crossf45fa6b2012-03-26 12:38:26 -0700570/* Play a sound via Stagefright */
Christopher Ferris1fe61072014-07-22 16:08:19 -0700571void play_sound(const char *path);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700572
Felipe Leme0c80cf02016-01-05 13:25:34 -0800573/* Checks if a given path is a directory. */
574bool is_dir(const char* pathname);
575
576/** Gets the last modification time of a file, or default time if file is not found. */
577time_t get_mtime(int fd, time_t default_mtime);
578
Calvin On249beee2016-06-03 15:17:07 -0700579/* Dumps eMMC Extended CSD data. */
Felipe Leme78f2c862015-12-21 09:55:22 -0800580void dump_emmc_ecsd(const char *ext_csd_path);
581
Calvin On249beee2016-06-03 15:17:07 -0700582/** Gets command-line arguments. */
Felipe Lemea34efb72016-03-11 09:33:32 -0800583void format_args(int argc, const char *argv[], std::string *args);
Felipe Leme88c79332016-02-22 11:06:49 -0800584
Vishnu Nair20cf5032018-01-05 13:15:49 -0800585/** Main entry point for dumpstate. */
586int run_main(int argc, char* argv[]);
587
Felipe Leme8620bb42015-11-10 11:04:45 -0800588#ifdef __cplusplus
589}
590#endif
591
Felipe Leme8268ed22016-08-02 18:18:25 -0700592#endif /* FRAMEWORK_NATIVE_CMD_DUMPSTATE_H_ */