blob: 94e31910873f0c53010b73d1ba6ba93a653d8147 [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 Dutt197661d2018-11-16 16:40:21 +000030#include <android/os/IDumpstate.h>
Jiyong Parkb22e65d2017-06-23 21:23:16 +090031#include <android/os/IDumpstateListener.h>
32#include <utils/StrongPointer.h>
Felipe Lemec6bc8bc2016-10-27 15:58:27 -070033#include <ziparchive/zip_writer.h>
34
Felipe Lemebda15a02016-11-16 17:48:25 -080035#include "DumpstateUtil.h"
Felipe Leme75876a22016-10-27 16:31:27 -070036
Felipe Leme35c94f32016-08-12 10:51:54 -070037// Workaround for const char *args[MAX_ARGS_ARRAY_SIZE] variables until they're converted to
38// std::vector<std::string>
Felipe Leme30dbfa12016-09-02 12:43:26 -070039// TODO: remove once not used
Felipe Leme35c94f32016-08-12 10:51:54 -070040#define MAX_ARGS_ARRAY_SIZE 1000
41
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;
53
54} // namespace dumpstate
55} // namespace os
56} // namespace android
57
Jiyong Parkb22e65d2017-06-23 21:23:16 +090058class ZipWriter;
59
Felipe Leme30dbfa12016-09-02 12:43:26 -070060// TODO: remove once moved to HAL
Felipe Leme8620bb42015-11-10 11:04:45 -080061#ifdef __cplusplus
62extern "C" {
63#endif
64
Felipe Leme30dbfa12016-09-02 12:43:26 -070065/*
Felipe Leme30dbfa12016-09-02 12:43:26 -070066 * Helper class used to report how long it takes for a section to finish.
67 *
68 * Typical usage:
69 *
70 * DurationReporter duration_reporter(title);
71 *
72 */
73class DurationReporter {
74 public:
Felipe Leme46b85da2016-11-21 17:40:45 -080075 DurationReporter(const std::string& title, bool log_only = false);
Felipe Leme30dbfa12016-09-02 12:43:26 -070076
77 ~DurationReporter();
78
Felipe Leme30dbfa12016-09-02 12:43:26 -070079 private:
Felipe Leme678727a2016-09-21 17:22:11 -070080 std::string title_;
Felipe Leme46b85da2016-11-21 17:40:45 -080081 bool log_only_;
Felipe Lemeb0f669d2016-09-26 18:26:11 -070082 uint64_t started_;
Felipe Leme062926e2016-10-27 15:51:12 -070083
84 DISALLOW_COPY_AND_ASSIGN(DurationReporter);
Felipe Leme30dbfa12016-09-02 12:43:26 -070085};
86
87/*
Felipe Leme7447d7c2016-11-03 18:12:22 -070088 * Keeps track of current progress and estimated max, saving stats on file to tune up future runs.
Felipe Leme71bbfc52015-11-23 14:14:51 -080089 *
Felipe Leme7447d7c2016-11-03 18:12:22 -070090 * Each `dumpstate` section contributes to the total weight by an individual weight, so the overall
91 * progress can be calculated by dividing the estimate max progress by the current progress.
Felipe Leme71bbfc52015-11-23 14:14:51 -080092 *
Felipe Leme7447d7c2016-11-03 18:12:22 -070093 * The estimated max progress is initially set to a value (`kDefaultMax) defined empirically, but
94 * it's adjusted after each dumpstate run by storing the average duration in a file.
Felipe Lemead5f6c42015-11-30 14:26:46 -080095 *
Felipe Leme71bbfc52015-11-23 14:14:51 -080096 */
Felipe Leme7447d7c2016-11-03 18:12:22 -070097class Progress {
Felipe Leme47e9be22016-12-21 15:37:07 -080098 friend class android::os::dumpstate::ProgressTest;
99 friend class android::os::dumpstate::DumpstateTest;
Felipe Leme7447d7c2016-11-03 18:12:22 -0700100
101 public:
102 /*
103 * Default estimation of the max duration of a bugreport generation.
104 *
105 * It does not need to match the exact sum of all sections, but ideally it should to be slight
106 * more than such sum: a value too high will cause the bugreport to finish before the user
107 * expected (for example, jumping from 70% to 100%), while a value too low will cause the
108 * progress to get stuck at an almost-finished value (like 99%) for a while.
109 *
110 * This constant is only used when the average duration from previous runs cannot be used.
111 */
112 static const int kDefaultMax;
113
114 Progress(const std::string& path = "");
115
116 // Gets the current progress.
117 int32_t Get() const;
118
119 // Gets the current estimated max progress.
120 int32_t GetMax() const;
121
122 // Gets the initial estimated max progress.
123 int32_t GetInitialMax() const;
124
125 // Increments progress (ignored if not positive).
126 // Returns `true` if the max progress increased as well.
127 bool Inc(int32_t delta);
128
129 // Persist the stats.
130 void Save();
131
132 void Dump(int fd, const std::string& prefix) const;
133
134 private:
135 Progress(int32_t initial_max, float growth_factor,
136 const std::string& path = ""); // Used by test cases.
137 Progress(int32_t initial_max, int32_t progress, float growth_factor); // Used by test cases.
138 void Load();
139 int32_t initial_max_;
140 int32_t progress_;
141 int32_t max_;
142 float growth_factor_;
143 int32_t n_runs_;
144 int32_t average_max_;
Nandana Dutt979388e2018-11-30 16:48:55 +0000145 std::string path_;
Felipe Leme7447d7c2016-11-03 18:12:22 -0700146};
Felipe Leme71bbfc52015-11-23 14:14:51 -0800147
Felipe Leme71bbfc52015-11-23 14:14:51 -0800148/*
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700149 * List of supported zip format versions.
150 *
151 * See bugreport-format.md for more info.
152 */
Vishnu Nair64afc022018-02-01 15:29:34 -0800153static std::string VERSION_CURRENT = "2.0";
Felipe Lemed071c682016-10-20 16:48:00 -0700154
155/*
Felipe Lemee184f662016-10-27 10:04:47 -0700156 * Temporary version that adds a anr-traces.txt entry. Once tools support it, the current version
Vishnu Nair64afc022018-02-01 15:29:34 -0800157 * will be bumped to 3.0.
Felipe Lemee184f662016-10-27 10:04:47 -0700158 */
Vishnu Nair64afc022018-02-01 15:29:34 -0800159static std::string VERSION_SPLIT_ANR = "3.0-dev-split-anr";
Felipe Lemee184f662016-10-27 10:04:47 -0700160
161/*
Felipe Lemed071c682016-10-20 16:48:00 -0700162 * "Alias" for the current version.
163 */
164static std::string VERSION_DEFAULT = "default";
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700165
166/*
Nandana Dutt979388e2018-11-30 16:48:55 +0000167 * Directory used by Dumpstate binary to keep its local files.
168 */
169static const std::string DUMPSTATE_DIRECTORY = "/bugreports";
170
171/*
Luis Hector Chavez91c2ae52018-03-14 15:12:46 -0700172 * Structure that contains the information of an open dump file.
173 */
174struct DumpData {
175 // Path of the file.
176 std::string name;
177
178 // Open file descriptor for the file.
179 android::base::unique_fd fd;
180
181 // Modification time of the file.
182 time_t mtime;
183};
184
185/*
Felipe Lemee844a9d2016-09-21 15:01:39 -0700186 * Main class driving a bugreport generation.
187 *
188 * Currently, it only contains variables that are accessed externally, but gradually the functions
189 * that are spread accross utils.cpp and dumpstate.cpp will be moved to it.
190 */
191class Dumpstate {
Felipe Leme4c2d6632016-09-28 14:32:00 -0700192 friend class DumpstateTest;
193
Felipe Lemee844a9d2016-09-21 15:01:39 -0700194 public:
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100195 enum RunStatus { OK, HELP, INVALID_INPUT, ERROR };
196
Nandana Dutt58d72e22018-11-16 10:30:48 +0000197 // The mode under which the bugreport should be run. Each mode encapsulates a few options.
198 enum BugreportMode {
Nandana Dutt197661d2018-11-16 16:40:21 +0000199 BUGREPORT_FULL = android::os::IDumpstate::BUGREPORT_MODE_FULL,
200 BUGREPORT_INTERACTIVE = android::os::IDumpstate::BUGREPORT_MODE_INTERACTIVE,
201 BUGREPORT_REMOTE = android::os::IDumpstate::BUGREPORT_MODE_REMOTE,
202 BUGREPORT_WEAR = android::os::IDumpstate::BUGREPORT_MODE_WEAR,
203 BUGREPORT_TELEPHONY = android::os::IDumpstate::BUGREPORT_MODE_TELEPHONY,
204 BUGREPORT_WIFI = android::os::IDumpstate::BUGREPORT_MODE_WIFI
Nandana Dutt58d72e22018-11-16 10:30:48 +0000205 };
206
Felipe Leme47e9be22016-12-21 15:37:07 -0800207 static android::os::dumpstate::CommandOptions DEFAULT_DUMPSYS;
Felipe Lemebda15a02016-11-16 17:48:25 -0800208
Felipe Lemee844a9d2016-09-21 15:01:39 -0700209 static Dumpstate& GetInstance();
Felipe Leme71bbfc52015-11-23 14:14:51 -0800210
Felipe Leme1d486fe2016-10-14 18:06:47 -0700211 /* Checkes whether dumpstate is generating a zipped bugreport. */
212 bool IsZipping() const;
213
Felipe Leme6ad9c062016-09-28 11:58:36 -0700214 /*
Felipe Leme678727a2016-09-21 17:22:11 -0700215 * Forks a command, waits for it to finish, and returns its status.
216 *
217 * |title| description of the command printed on `stdout` (or empty to skip
218 * description).
219 * |full_command| array containing the command (first entry) and its arguments.
220 * Must contain at least one element.
221 * |options| optional argument defining the command's behavior.
222 */
223 int RunCommand(const std::string& title, const std::vector<std::string>& fullCommand,
Felipe Leme47e9be22016-12-21 15:37:07 -0800224 const android::os::dumpstate::CommandOptions& options =
225 android::os::dumpstate::CommandOptions::DEFAULT);
Felipe Leme678727a2016-09-21 17:22:11 -0700226
227 /*
228 * Runs `dumpsys` with the given arguments, automatically setting its timeout
Vishnu Nair6921f802017-11-22 09:17:23 -0800229 * (`-T` argument)
Felipe Leme678727a2016-09-21 17:22:11 -0700230 * according to the command options.
231 *
232 * |title| description of the command printed on `stdout` (or empty to skip
233 * description).
234 * |dumpsys_args| `dumpsys` arguments (except `-t`).
235 * |options| optional argument defining the command's behavior.
Vishnu Nair6921f802017-11-22 09:17:23 -0800236 * |dumpsys_timeout| when > 0, defines the value passed to `dumpsys -T` (otherwise it uses the
Felipe Leme6ad9c062016-09-28 11:58:36 -0700237 * timeout from `options`)
Felipe Leme678727a2016-09-21 17:22:11 -0700238 */
Felipe Leme9a523ae2016-10-20 15:10:33 -0700239 void RunDumpsys(const std::string& title, const std::vector<std::string>& dumpsys_args,
Felipe Leme47e9be22016-12-21 15:37:07 -0800240 const android::os::dumpstate::CommandOptions& options = DEFAULT_DUMPSYS,
Vishnu Nair6921f802017-11-22 09:17:23 -0800241 long dumpsys_timeout_ms = 0);
Felipe Leme678727a2016-09-21 17:22:11 -0700242
243 /*
244 * Prints the contents of a file.
245 *
246 * |title| description of the command printed on `stdout` (or empty to skip
247 * description).
248 * |path| location of the file to be dumped.
249 */
250 int DumpFile(const std::string& title, const std::string& path);
251
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700252 /*
Felipe Leme1d486fe2016-10-14 18:06:47 -0700253 * Adds a new entry to the existing zip file.
254 * */
255 bool AddZipEntry(const std::string& entry_name, const std::string& entry_path);
256
257 /*
258 * Adds a new entry to the existing zip file.
Vishnu Naire97d6122018-01-18 13:58:56 -0800259 *
260 * |entry_name| destination path of the new entry.
261 * |fd| file descriptor to read from.
262 * |timeout| timeout to terminate the read if not completed. Set
263 * value of 0s (default) to disable timeout.
Felipe Leme1d486fe2016-10-14 18:06:47 -0700264 */
Vishnu Naire97d6122018-01-18 13:58:56 -0800265 android::status_t AddZipEntryFromFd(const std::string& entry_name, int fd,
266 std::chrono::milliseconds timeout);
Felipe Leme1d486fe2016-10-14 18:06:47 -0700267
268 /*
Nandana Dutta7db6342018-11-21 14:53:34 +0000269 * Adds a text entry to the existing zip file.
Felipe Leme1d486fe2016-10-14 18:06:47 -0700270 */
271 bool AddTextZipEntry(const std::string& entry_name, const std::string& content);
272
273 /*
274 * Adds all files from a directory to the zipped bugreport file.
275 */
276 void AddDir(const std::string& dir, bool recursive);
277
278 /*
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700279 * Takes a screenshot and save it to the given `path`.
280 *
281 * If `path` is empty, uses a standard path based on the bugreport name.
282 */
283 void TakeScreenshot(const std::string& path = "");
284
Felipe Leme7447d7c2016-11-03 18:12:22 -0700285 /////////////////////////////////////////////////////////////////////
286 // TODO: members below should be private once refactor is finished //
287 /////////////////////////////////////////////////////////////////////
288
289 // TODO: temporary method until Dumpstate object is properly set
290 void SetProgress(std::unique_ptr<Progress> progress);
Felipe Lemed80e6b62016-10-03 13:08:14 -0700291
Felipe Leme6f674ae2016-11-18 17:10:33 -0800292 void DumpstateBoard();
293
Felipe Lemed80e6b62016-10-03 13:08:14 -0700294 /*
295 * Updates the overall progress of the bugreport generation by the given weight increment.
296 */
Felipe Leme7447d7c2016-11-03 18:12:22 -0700297 void UpdateProgress(int32_t delta);
Felipe Lemed80e6b62016-10-03 13:08:14 -0700298
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700299 /* Prints the dumpstate header on `stdout`. */
Felipe Leme2b9b06c2016-10-14 09:13:06 -0700300 void PrintHeader() const;
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700301
Felipe Leme1d486fe2016-10-14 18:06:47 -0700302 /*
303 * Adds the temporary report to the existing .zip file, closes the .zip file, and removes the
304 * temporary file.
305 */
306 bool FinishZipFile();
307
Nandana Dutt979388e2018-11-30 16:48:55 +0000308 /* Constructs a full path inside directory with file name formatted using the given suffix. */
309 std::string GetPath(const std::string& directory, const std::string& suffix) const;
310
311 /* Constructs a full path inside bugreport_internal_dir_ with file name formatted using the
312 * given suffix. */
Felipe Leme2b9b06c2016-10-14 09:13:06 -0700313 std::string GetPath(const std::string& suffix) const;
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700314
Vishnu Nair780b1282017-10-10 13:57:24 -0700315 /* Returns true if the current version supports priority dump feature. */
316 bool CurrentVersionSupportsPriorityDumps() const;
317
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100318 struct DumpOptions;
Felipe Lemee844a9d2016-09-21 15:01:39 -0700319
Nandana Dutt197661d2018-11-16 16:40:21 +0000320 /* Main entry point for running a complete bugreport. */
321 RunStatus Run();
322
323 /* Sets runtime options. */
324 void SetOptions(std::unique_ptr<DumpOptions> options);
Nandana Dutt3f8c7172018-09-25 12:01:54 +0100325
Nandana Dutt3f8c7172018-09-25 12:01:54 +0100326 /*
327 * Structure to hold options that determine the behavior of dumpstate.
328 */
329 struct DumpOptions {
330 bool do_add_date = false;
331 bool do_zip_file = false;
332 bool do_vibrate = true;
333 bool use_socket = false;
334 bool use_control_socket = false;
335 bool do_fb = false;
336 bool do_broadcast = false;
337 bool is_remote_mode = false;
338 bool show_header_only = false;
339 bool do_start_service = false;
340 bool telephony_only = false;
341 bool wifi_only = false;
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100342 // Whether progress updates should be published.
343 bool do_progress_updates = false;
Nandana Dutt979388e2018-11-30 16:48:55 +0000344 // File descriptor to output zip file. -1 indicates not set. Takes precedence over
345 // use_outfile.
346 int fd = -1;
347 // Partial path to output file.
Nandana Dutt3f8c7172018-09-25 12:01:54 +0100348 std::string use_outfile;
Nandana Dutt58d72e22018-11-16 10:30:48 +0000349 // TODO: rename to MODE.
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100350 // Extra options passed as system property.
351 std::string extra_options;
352 // Command-line arguments as string
353 std::string args;
354 // Notification title and description
355 std::string notification_title;
356 std::string notification_description;
357
358 /* Initializes options from commandline arguments and system properties. */
359 RunStatus Initialize(int argc, char* argv[]);
360
Nandana Dutt58d72e22018-11-16 10:30:48 +0000361 /* Initializes options from the requested mode. */
362 void Initialize(BugreportMode bugreport_mode);
363
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100364 /* Returns true if the options set so far are consistent. */
365 bool ValidateOptions() const;
Nandana Dutt3f8c7172018-09-25 12:01:54 +0100366 };
367
368 // TODO: initialize fields on constructor
Felipe Lemee844a9d2016-09-21 15:01:39 -0700369 // dumpstate id - unique after each device reboot.
Felipe Leme7447d7c2016-11-03 18:12:22 -0700370 uint32_t id_;
Felipe Lemee844a9d2016-09-21 15:01:39 -0700371
Felipe Leme75876a22016-10-27 16:31:27 -0700372 // dumpstate pid
373 pid_t pid_;
374
Nandana Dutt3f8c7172018-09-25 12:01:54 +0100375 // Runtime options.
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100376 std::unique_ptr<DumpOptions> options_;
Felipe Lemee844a9d2016-09-21 15:01:39 -0700377
Felipe Leme009ecbb2016-11-07 10:18:44 -0800378 // How frequently the progess should be updated;the listener will only be notificated when the
379 // delta from the previous update is more than the threshold.
380 int32_t update_progress_threshold_ = 100;
381
382 // Last progress that triggered a listener updated
383 int32_t last_updated_progress_;
384
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700385 // Whether it should take an screenshot earlier in the process.
Felipe Leme9a523ae2016-10-20 15:10:33 -0700386 bool do_early_screenshot_ = false;
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700387
Felipe Leme7447d7c2016-11-03 18:12:22 -0700388 std::unique_ptr<Progress> progress_;
Felipe Lemee844a9d2016-09-21 15:01:39 -0700389
390 // When set, defines a socket file-descriptor use to report progress to bugreportz.
Felipe Leme9a523ae2016-10-20 15:10:33 -0700391 int control_socket_fd_ = -1;
Felipe Lemee844a9d2016-09-21 15:01:39 -0700392
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700393 // Bugreport format version;
Felipe Lemed071c682016-10-20 16:48:00 -0700394 std::string version_ = VERSION_CURRENT;
Felipe Lemee844a9d2016-09-21 15:01:39 -0700395
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700396 time_t now_;
397
Felipe Leme2b9b06c2016-10-14 09:13:06 -0700398 // Base name (without suffix or extensions) of the bugreport files, typically
399 // `bugreport-BUILD_ID`.
Felipe Leme9a523ae2016-10-20 15:10:33 -0700400 std::string base_name_;
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700401
Felipe Leme2b9b06c2016-10-14 09:13:06 -0700402 // Name is the suffix part of the bugreport files - it's typically the date (when invoked with
403 // `-d`), but it could be changed by the user..
404 std::string name_;
405
Nandana Dutt979388e2018-11-30 16:48:55 +0000406 std::string bugreport_internal_dir_ = DUMPSTATE_DIRECTORY;
407
408 // Full path of the temporary file containing the bugreport, inside bugreport_internal_dir_.
409 // At the very end this file is pulled into the zip file.
Felipe Leme9a523ae2016-10-20 15:10:33 -0700410 std::string tmp_path_;
Felipe Leme1d486fe2016-10-14 18:06:47 -0700411
Nandana Dutt979388e2018-11-30 16:48:55 +0000412 // Full path of the file containing the dumpstate logs, inside bugreport_internal_dir_.
413 // This is useful for debugging.
Felipe Leme9a523ae2016-10-20 15:10:33 -0700414 std::string log_path_;
Felipe Leme1d486fe2016-10-14 18:06:47 -0700415
Nandana Dutt979388e2018-11-30 16:48:55 +0000416 // Full path of the bugreport file, be it zip or text, inside bugreport_internal_dir_.
Felipe Leme9a523ae2016-10-20 15:10:33 -0700417 std::string path_;
Felipe Leme1d486fe2016-10-14 18:06:47 -0700418
Nandana Dutt979388e2018-11-30 16:48:55 +0000419 // TODO: If temporary this should be removed at the end.
420 // Full path of the temporary file containing the screenshot (when requested).
421 std::string screenshot_path_;
422
423 // TODO(b/111441001): remove when obsolete.
424 // Full path of the final zip file inside the caller-specified directory, if available.
425 std::string final_path_;
426
427 // The caller-specified directory, if available.
428 std::string bugreport_dir_;
429
Felipe Leme1d486fe2016-10-14 18:06:47 -0700430 // Pointer to the zipped file.
431 std::unique_ptr<FILE, int (*)(FILE*)> zip_file{nullptr, fclose};
432
Felipe Lemec6bc8bc2016-10-27 15:58:27 -0700433 // Pointer to the zip structure.
434 std::unique_ptr<ZipWriter> zip_writer_;
435
Vishnu Nair20cf5032018-01-05 13:15:49 -0800436 // Binder object listening to progress.
Felipe Leme75876a22016-10-27 16:31:27 -0700437 android::sp<android::os::IDumpstateListener> listener_;
438 std::string listener_name_;
Vishnu Nair20cf5032018-01-05 13:15:49 -0800439 bool report_section_;
Felipe Leme75876a22016-10-27 16:31:27 -0700440
Luis Hector Chavez91c2ae52018-03-14 15:12:46 -0700441 // List of open tombstone dump files.
442 std::vector<DumpData> tombstone_data_;
443
444 // List of open ANR dump files.
445 std::vector<DumpData> anr_data_;
446
Felipe Lemee844a9d2016-09-21 15:01:39 -0700447 private:
Felipe Lemed80e6b62016-10-03 13:08:14 -0700448 // Used by GetInstance() only.
Felipe Lemef0292972016-11-22 13:57:05 -0800449 Dumpstate(const std::string& version = VERSION_CURRENT);
Felipe Leme062926e2016-10-27 15:51:12 -0700450
451 DISALLOW_COPY_AND_ASSIGN(Dumpstate);
Felipe Lemee844a9d2016-09-21 15:01:39 -0700452};
453
454// for_each_pid_func = void (*)(int, const char*);
455// for_each_tid_func = void (*)(int, int, const char*);
456
457typedef void(for_each_pid_func)(int, const char*);
458typedef void(for_each_tid_func)(int, int, const char*);
Felipe Leme71ca15e2016-05-19 16:18:17 -0700459
Felipe Leme71a74ac2016-03-17 15:43:25 -0700460/* saves the the contents of a file as a long */
461int read_file_as_long(const char *path, long int *output);
462
Christopher Ferris54bcc5f2015-02-10 12:15:01 -0800463/* prints the contents of the fd
464 * fd must have been opened with the flag O_NONBLOCK.
465 */
Christopher Ferris1fe61072014-07-22 16:08:19 -0700466int dump_file_from_fd(const char *title, const char *path, int fd);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700467
Mark Salyzyn326842f2015-04-30 09:49:41 -0700468/* calls skip to gate calling dump_from_fd recursively
469 * in the specified directory. dump_from_fd defaults to
470 * dump_file_from_fd above when set to NULL. skip defaults
471 * to false when set to NULL. dump_from_fd will always be
472 * called with title NULL.
473 */
Felipe Leme678727a2016-09-21 17:22:11 -0700474int dump_files(const std::string& title, const char* dir, bool (*skip)(const char* path),
475 int (*dump_from_fd)(const char* title, const char* path, int fd));
Mark Salyzyn326842f2015-04-30 09:49:41 -0700476
Felipe Leme2628e9e2016-04-12 16:36:51 -0700477/** opens a socket and returns its file descriptor */
478int open_socket(const char *service);
479
Colin Crossf45fa6b2012-03-26 12:38:26 -0700480/* redirect output to a service control socket */
481void redirect_to_socket(FILE *redirect, const char *service);
482
Felipe Leme0f3fb202016-06-10 17:10:53 -0700483/* redirect output to a new file */
Christopher Ferrisff4a4dc2015-02-09 16:24:47 -0800484void redirect_to_file(FILE *redirect, char *path);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700485
Felipe Leme0f3fb202016-06-10 17:10:53 -0700486/* redirect output to an existing file */
487void redirect_to_existing_file(FILE *redirect, char *path);
488
Felipe Leme111b9d02016-02-03 09:28:24 -0800489/* create leading directories, if necessary */
490void create_parent_dirs(const char *path);
491
Jeff Brownbf7f4922012-06-07 16:40:01 -0700492/* dump Dalvik and native stack traces, return the trace file location (NULL if none) */
493const char *dump_traces();
Colin Crossf45fa6b2012-03-26 12:38:26 -0700494
495/* for each process in the system, run the specified function */
Colin Cross0c22e8b2012-11-02 15:46:56 -0700496void for_each_pid(for_each_pid_func func, const char *header);
497
498/* for each thread in the system, run the specified function */
499void for_each_tid(for_each_tid_func func, const char *header);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700500
501/* Displays a blocked processes in-kernel wait channel */
Colin Cross0c22e8b2012-11-02 15:46:56 -0700502void show_wchan(int pid, int tid, const char *name);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700503
Mark Salyzyna297c322016-02-05 15:33:17 -0800504/* Displays a processes times */
505void show_showtime(int pid, const char *name);
506
Colin Crossf45fa6b2012-03-26 12:38:26 -0700507/* Runs "showmap" for a process */
508void do_showmap(int pid, const char *name);
509
510/* Gets the dmesg output for the kernel */
511void do_dmesg();
512
Sreeram Ramachandran2b3bba32014-07-08 15:40:55 -0700513/* Prints the contents of all the routing tables, both IPv4 and IPv6. */
514void dump_route_tables();
515
Colin Crossf45fa6b2012-03-26 12:38:26 -0700516/* Play a sound via Stagefright */
Christopher Ferris1fe61072014-07-22 16:08:19 -0700517void play_sound(const char *path);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700518
Felipe Leme0c80cf02016-01-05 13:25:34 -0800519/* Checks if a given path is a directory. */
520bool is_dir(const char* pathname);
521
522/** Gets the last modification time of a file, or default time if file is not found. */
523time_t get_mtime(int fd, time_t default_mtime);
524
Calvin On249beee2016-06-03 15:17:07 -0700525/* Dumps eMMC Extended CSD data. */
Felipe Leme78f2c862015-12-21 09:55:22 -0800526void dump_emmc_ecsd(const char *ext_csd_path);
527
Calvin On249beee2016-06-03 15:17:07 -0700528/** Gets command-line arguments. */
Felipe Lemea34efb72016-03-11 09:33:32 -0800529void format_args(int argc, const char *argv[], std::string *args);
Felipe Leme88c79332016-02-22 11:06:49 -0800530
Vishnu Nair20cf5032018-01-05 13:15:49 -0800531/** Main entry point for dumpstate. */
532int run_main(int argc, char* argv[]);
533
Felipe Leme8620bb42015-11-10 11:04:45 -0800534#ifdef __cplusplus
535}
536#endif
537
Felipe Leme8268ed22016-08-02 18:18:25 -0700538#endif /* FRAMEWORK_NATIVE_CMD_DUMPSTATE_H_ */