blob: f75a668b3b92dd4d72b94745a3d763ded21555bc [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>
Jiyong Parkb22e65d2017-06-23 21:23:16 +090030#include <android/os/IDumpstateListener.h>
31#include <utils/StrongPointer.h>
Felipe Lemec6bc8bc2016-10-27 15:58:27 -070032#include <ziparchive/zip_writer.h>
33
Felipe Lemebda15a02016-11-16 17:48:25 -080034#include "DumpstateUtil.h"
Felipe Leme75876a22016-10-27 16:31:27 -070035
Felipe Leme35c94f32016-08-12 10:51:54 -070036// Workaround for const char *args[MAX_ARGS_ARRAY_SIZE] variables until they're converted to
37// std::vector<std::string>
Felipe Leme30dbfa12016-09-02 12:43:26 -070038// TODO: remove once not used
Felipe Leme35c94f32016-08-12 10:51:54 -070039#define MAX_ARGS_ARRAY_SIZE 1000
40
Felipe Leme47e9be22016-12-21 15:37:07 -080041// TODO: move everything under this namespace
42// TODO: and then remove explicitly android::os::dumpstate:: prefixes
43namespace android {
44namespace os {
Nandana Dutt58d72e22018-11-16 10:30:48 +000045
46struct DumpstateOptions;
47
Felipe Leme47e9be22016-12-21 15:37:07 -080048namespace dumpstate {
49
50class DumpstateTest;
51class ProgressTest;
52
53} // namespace dumpstate
54} // namespace os
55} // namespace android
56
Jiyong Parkb22e65d2017-06-23 21:23:16 +090057class ZipWriter;
58
Felipe Leme30dbfa12016-09-02 12:43:26 -070059// TODO: remove once moved to HAL
Felipe Leme8620bb42015-11-10 11:04:45 -080060#ifdef __cplusplus
61extern "C" {
62#endif
63
Felipe Leme30dbfa12016-09-02 12:43:26 -070064/*
Felipe Leme30dbfa12016-09-02 12:43:26 -070065 * Helper class used to report how long it takes for a section to finish.
66 *
67 * Typical usage:
68 *
69 * DurationReporter duration_reporter(title);
70 *
71 */
72class DurationReporter {
73 public:
Felipe Leme46b85da2016-11-21 17:40:45 -080074 DurationReporter(const std::string& title, bool log_only = false);
Felipe Leme30dbfa12016-09-02 12:43:26 -070075
76 ~DurationReporter();
77
Felipe Leme30dbfa12016-09-02 12:43:26 -070078 private:
Felipe Leme678727a2016-09-21 17:22:11 -070079 std::string title_;
Felipe Leme46b85da2016-11-21 17:40:45 -080080 bool log_only_;
Felipe Lemeb0f669d2016-09-26 18:26:11 -070081 uint64_t started_;
Felipe Leme062926e2016-10-27 15:51:12 -070082
83 DISALLOW_COPY_AND_ASSIGN(DurationReporter);
Felipe Leme30dbfa12016-09-02 12:43:26 -070084};
85
86/*
Felipe Leme7447d7c2016-11-03 18:12:22 -070087 * Keeps track of current progress and estimated max, saving stats on file to tune up future runs.
Felipe Leme71bbfc52015-11-23 14:14:51 -080088 *
Felipe Leme7447d7c2016-11-03 18:12:22 -070089 * Each `dumpstate` section contributes to the total weight by an individual weight, so the overall
90 * progress can be calculated by dividing the estimate max progress by the current progress.
Felipe Leme71bbfc52015-11-23 14:14:51 -080091 *
Felipe Leme7447d7c2016-11-03 18:12:22 -070092 * The estimated max progress is initially set to a value (`kDefaultMax) defined empirically, but
93 * it's adjusted after each dumpstate run by storing the average duration in a file.
Felipe Lemead5f6c42015-11-30 14:26:46 -080094 *
Felipe Leme71bbfc52015-11-23 14:14:51 -080095 */
Felipe Leme7447d7c2016-11-03 18:12:22 -070096class Progress {
Felipe Leme47e9be22016-12-21 15:37:07 -080097 friend class android::os::dumpstate::ProgressTest;
98 friend class android::os::dumpstate::DumpstateTest;
Felipe Leme7447d7c2016-11-03 18:12:22 -070099
100 public:
101 /*
102 * Default estimation of the max duration of a bugreport generation.
103 *
104 * It does not need to match the exact sum of all sections, but ideally it should to be slight
105 * more than such sum: a value too high will cause the bugreport to finish before the user
106 * expected (for example, jumping from 70% to 100%), while a value too low will cause the
107 * progress to get stuck at an almost-finished value (like 99%) for a while.
108 *
109 * This constant is only used when the average duration from previous runs cannot be used.
110 */
111 static const int kDefaultMax;
112
113 Progress(const std::string& path = "");
114
115 // Gets the current progress.
116 int32_t Get() const;
117
118 // Gets the current estimated max progress.
119 int32_t GetMax() const;
120
121 // Gets the initial estimated max progress.
122 int32_t GetInitialMax() const;
123
124 // Increments progress (ignored if not positive).
125 // Returns `true` if the max progress increased as well.
126 bool Inc(int32_t delta);
127
128 // Persist the stats.
129 void Save();
130
131 void Dump(int fd, const std::string& prefix) const;
132
133 private:
134 Progress(int32_t initial_max, float growth_factor,
135 const std::string& path = ""); // Used by test cases.
136 Progress(int32_t initial_max, int32_t progress, float growth_factor); // Used by test cases.
137 void Load();
138 int32_t initial_max_;
139 int32_t progress_;
140 int32_t max_;
141 float growth_factor_;
142 int32_t n_runs_;
143 int32_t average_max_;
144 const std::string& path_;
145};
Felipe Leme71bbfc52015-11-23 14:14:51 -0800146
Felipe Leme71bbfc52015-11-23 14:14:51 -0800147/*
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700148 * List of supported zip format versions.
149 *
150 * See bugreport-format.md for more info.
151 */
Vishnu Nair64afc022018-02-01 15:29:34 -0800152static std::string VERSION_CURRENT = "2.0";
Felipe Lemed071c682016-10-20 16:48:00 -0700153
154/*
Felipe Lemee184f662016-10-27 10:04:47 -0700155 * Temporary version that adds a anr-traces.txt entry. Once tools support it, the current version
Vishnu Nair64afc022018-02-01 15:29:34 -0800156 * will be bumped to 3.0.
Felipe Lemee184f662016-10-27 10:04:47 -0700157 */
Vishnu Nair64afc022018-02-01 15:29:34 -0800158static std::string VERSION_SPLIT_ANR = "3.0-dev-split-anr";
Felipe Lemee184f662016-10-27 10:04:47 -0700159
160/*
Felipe Lemed071c682016-10-20 16:48:00 -0700161 * "Alias" for the current version.
162 */
163static std::string VERSION_DEFAULT = "default";
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700164
165/*
Luis Hector Chavez91c2ae52018-03-14 15:12:46 -0700166 * Structure that contains the information of an open dump file.
167 */
168struct DumpData {
169 // Path of the file.
170 std::string name;
171
172 // Open file descriptor for the file.
173 android::base::unique_fd fd;
174
175 // Modification time of the file.
176 time_t mtime;
177};
178
179/*
Felipe Lemee844a9d2016-09-21 15:01:39 -0700180 * Main class driving a bugreport generation.
181 *
182 * Currently, it only contains variables that are accessed externally, but gradually the functions
183 * that are spread accross utils.cpp and dumpstate.cpp will be moved to it.
184 */
185class Dumpstate {
Felipe Leme4c2d6632016-09-28 14:32:00 -0700186 friend class DumpstateTest;
187
Felipe Lemee844a9d2016-09-21 15:01:39 -0700188 public:
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100189 enum RunStatus { OK, HELP, INVALID_INPUT, ERROR };
190
Nandana Dutt58d72e22018-11-16 10:30:48 +0000191 // TODO(117177665): should enumerate constants in the AIDL file.
192 // The mode under which the bugreport should be run. Each mode encapsulates a few options.
193 enum BugreportMode {
194 BUGREPORT_FULL,
195 BUGREPORT_INTERACTIVE,
196 BUGREPORT_REMOTE,
197 BUGREPORT_WEAR,
198 BUGREPORT_TELEPHONY,
199 BUGREPORT_WIFI
200 };
201
Felipe Leme47e9be22016-12-21 15:37:07 -0800202 static android::os::dumpstate::CommandOptions DEFAULT_DUMPSYS;
Felipe Lemebda15a02016-11-16 17:48:25 -0800203
Felipe Lemee844a9d2016-09-21 15:01:39 -0700204 static Dumpstate& GetInstance();
Felipe Leme71bbfc52015-11-23 14:14:51 -0800205
Felipe Leme1d486fe2016-10-14 18:06:47 -0700206 /* Checkes whether dumpstate is generating a zipped bugreport. */
207 bool IsZipping() const;
208
Felipe Leme6ad9c062016-09-28 11:58:36 -0700209 /*
Felipe Leme678727a2016-09-21 17:22:11 -0700210 * Forks a command, waits for it to finish, and returns its status.
211 *
212 * |title| description of the command printed on `stdout` (or empty to skip
213 * description).
214 * |full_command| array containing the command (first entry) and its arguments.
215 * Must contain at least one element.
216 * |options| optional argument defining the command's behavior.
217 */
218 int RunCommand(const std::string& title, const std::vector<std::string>& fullCommand,
Felipe Leme47e9be22016-12-21 15:37:07 -0800219 const android::os::dumpstate::CommandOptions& options =
220 android::os::dumpstate::CommandOptions::DEFAULT);
Felipe Leme678727a2016-09-21 17:22:11 -0700221
222 /*
223 * Runs `dumpsys` with the given arguments, automatically setting its timeout
Vishnu Nair6921f802017-11-22 09:17:23 -0800224 * (`-T` argument)
Felipe Leme678727a2016-09-21 17:22:11 -0700225 * according to the command options.
226 *
227 * |title| description of the command printed on `stdout` (or empty to skip
228 * description).
229 * |dumpsys_args| `dumpsys` arguments (except `-t`).
230 * |options| optional argument defining the command's behavior.
Vishnu Nair6921f802017-11-22 09:17:23 -0800231 * |dumpsys_timeout| when > 0, defines the value passed to `dumpsys -T` (otherwise it uses the
Felipe Leme6ad9c062016-09-28 11:58:36 -0700232 * timeout from `options`)
Felipe Leme678727a2016-09-21 17:22:11 -0700233 */
Felipe Leme9a523ae2016-10-20 15:10:33 -0700234 void RunDumpsys(const std::string& title, const std::vector<std::string>& dumpsys_args,
Felipe Leme47e9be22016-12-21 15:37:07 -0800235 const android::os::dumpstate::CommandOptions& options = DEFAULT_DUMPSYS,
Vishnu Nair6921f802017-11-22 09:17:23 -0800236 long dumpsys_timeout_ms = 0);
Felipe Leme678727a2016-09-21 17:22:11 -0700237
238 /*
239 * Prints the contents of a file.
240 *
241 * |title| description of the command printed on `stdout` (or empty to skip
242 * description).
243 * |path| location of the file to be dumped.
244 */
245 int DumpFile(const std::string& title, const std::string& path);
246
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700247 /*
Felipe Leme1d486fe2016-10-14 18:06:47 -0700248 * Adds a new entry to the existing zip file.
249 * */
250 bool AddZipEntry(const std::string& entry_name, const std::string& entry_path);
251
252 /*
253 * Adds a new entry to the existing zip file.
Vishnu Naire97d6122018-01-18 13:58:56 -0800254 *
255 * |entry_name| destination path of the new entry.
256 * |fd| file descriptor to read from.
257 * |timeout| timeout to terminate the read if not completed. Set
258 * value of 0s (default) to disable timeout.
Felipe Leme1d486fe2016-10-14 18:06:47 -0700259 */
Vishnu Naire97d6122018-01-18 13:58:56 -0800260 android::status_t AddZipEntryFromFd(const std::string& entry_name, int fd,
261 std::chrono::milliseconds timeout);
Felipe Leme1d486fe2016-10-14 18:06:47 -0700262
263 /*
264 * Adds a text entry entry to the existing zip file.
265 */
266 bool AddTextZipEntry(const std::string& entry_name, const std::string& content);
267
268 /*
269 * Adds all files from a directory to the zipped bugreport file.
270 */
271 void AddDir(const std::string& dir, bool recursive);
272
273 /*
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700274 * Takes a screenshot and save it to the given `path`.
275 *
276 * If `path` is empty, uses a standard path based on the bugreport name.
277 */
278 void TakeScreenshot(const std::string& path = "");
279
Felipe Leme7447d7c2016-11-03 18:12:22 -0700280 /////////////////////////////////////////////////////////////////////
281 // TODO: members below should be private once refactor is finished //
282 /////////////////////////////////////////////////////////////////////
283
284 // TODO: temporary method until Dumpstate object is properly set
285 void SetProgress(std::unique_ptr<Progress> progress);
Felipe Lemed80e6b62016-10-03 13:08:14 -0700286
Felipe Leme6f674ae2016-11-18 17:10:33 -0800287 void DumpstateBoard();
288
Felipe Lemed80e6b62016-10-03 13:08:14 -0700289 /*
290 * Updates the overall progress of the bugreport generation by the given weight increment.
291 */
Felipe Leme7447d7c2016-11-03 18:12:22 -0700292 void UpdateProgress(int32_t delta);
Felipe Lemed80e6b62016-10-03 13:08:14 -0700293
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700294 /* Prints the dumpstate header on `stdout`. */
Felipe Leme2b9b06c2016-10-14 09:13:06 -0700295 void PrintHeader() const;
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700296
Felipe Leme1d486fe2016-10-14 18:06:47 -0700297 /*
298 * Adds the temporary report to the existing .zip file, closes the .zip file, and removes the
299 * temporary file.
300 */
301 bool FinishZipFile();
302
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700303 /* Gets the path of a bugreport file with the given suffix. */
Felipe Leme2b9b06c2016-10-14 09:13:06 -0700304 std::string GetPath(const std::string& suffix) const;
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700305
Vishnu Nair780b1282017-10-10 13:57:24 -0700306 /* Returns true if the current version supports priority dump feature. */
307 bool CurrentVersionSupportsPriorityDumps() const;
308
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100309 struct DumpOptions;
Felipe Lemee844a9d2016-09-21 15:01:39 -0700310
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100311 /* Main entry point for running a complete bugreport. Takes ownership of options. */
312 RunStatus RunWithOptions(std::unique_ptr<DumpOptions> options);
Nandana Dutt3f8c7172018-09-25 12:01:54 +0100313
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100314 // TODO: add other options from DumpState.
Nandana Dutt3f8c7172018-09-25 12:01:54 +0100315 /*
316 * Structure to hold options that determine the behavior of dumpstate.
317 */
318 struct DumpOptions {
319 bool do_add_date = false;
320 bool do_zip_file = false;
321 bool do_vibrate = true;
322 bool use_socket = false;
323 bool use_control_socket = false;
324 bool do_fb = false;
325 bool do_broadcast = false;
326 bool is_remote_mode = false;
327 bool show_header_only = false;
328 bool do_start_service = false;
329 bool telephony_only = false;
330 bool wifi_only = false;
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100331 // Whether progress updates should be published.
332 bool do_progress_updates = false;
Nandana Dutt3f8c7172018-09-25 12:01:54 +0100333 std::string use_outfile;
Nandana Dutt58d72e22018-11-16 10:30:48 +0000334 // TODO: rename to MODE.
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100335 // Extra options passed as system property.
336 std::string extra_options;
337 // Command-line arguments as string
338 std::string args;
339 // Notification title and description
340 std::string notification_title;
341 std::string notification_description;
342
343 /* Initializes options from commandline arguments and system properties. */
344 RunStatus Initialize(int argc, char* argv[]);
345
Nandana Dutt58d72e22018-11-16 10:30:48 +0000346 /* Initializes options from the requested mode. */
347 void Initialize(BugreportMode bugreport_mode);
348
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100349 /* Returns true if the options set so far are consistent. */
350 bool ValidateOptions() const;
Nandana Dutt3f8c7172018-09-25 12:01:54 +0100351 };
352
353 // TODO: initialize fields on constructor
Felipe Lemee844a9d2016-09-21 15:01:39 -0700354 // dumpstate id - unique after each device reboot.
Felipe Leme7447d7c2016-11-03 18:12:22 -0700355 uint32_t id_;
Felipe Lemee844a9d2016-09-21 15:01:39 -0700356
Felipe Leme75876a22016-10-27 16:31:27 -0700357 // dumpstate pid
358 pid_t pid_;
359
Nandana Dutt3f8c7172018-09-25 12:01:54 +0100360 // Runtime options.
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100361 std::unique_ptr<DumpOptions> options_;
Felipe Lemee844a9d2016-09-21 15:01:39 -0700362
Felipe Leme009ecbb2016-11-07 10:18:44 -0800363 // How frequently the progess should be updated;the listener will only be notificated when the
364 // delta from the previous update is more than the threshold.
365 int32_t update_progress_threshold_ = 100;
366
367 // Last progress that triggered a listener updated
368 int32_t last_updated_progress_;
369
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700370 // Whether it should take an screenshot earlier in the process.
Felipe Leme9a523ae2016-10-20 15:10:33 -0700371 bool do_early_screenshot_ = false;
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700372
Felipe Leme7447d7c2016-11-03 18:12:22 -0700373 std::unique_ptr<Progress> progress_;
Felipe Lemee844a9d2016-09-21 15:01:39 -0700374
375 // When set, defines a socket file-descriptor use to report progress to bugreportz.
Felipe Leme9a523ae2016-10-20 15:10:33 -0700376 int control_socket_fd_ = -1;
Felipe Lemee844a9d2016-09-21 15:01:39 -0700377
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700378 // Bugreport format version;
Felipe Lemed071c682016-10-20 16:48:00 -0700379 std::string version_ = VERSION_CURRENT;
Felipe Lemee844a9d2016-09-21 15:01:39 -0700380
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700381 // Full path of the directory where the bugreport files will be written.
Felipe Leme9a523ae2016-10-20 15:10:33 -0700382 std::string bugreport_dir_;
Felipe Lemee844a9d2016-09-21 15:01:39 -0700383
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700384 // Full path of the temporary file containing the screenshot (when requested).
Felipe Leme9a523ae2016-10-20 15:10:33 -0700385 std::string screenshot_path_;
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700386
387 time_t now_;
388
Felipe Leme2b9b06c2016-10-14 09:13:06 -0700389 // Base name (without suffix or extensions) of the bugreport files, typically
390 // `bugreport-BUILD_ID`.
Felipe Leme9a523ae2016-10-20 15:10:33 -0700391 std::string base_name_;
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700392
Felipe Leme2b9b06c2016-10-14 09:13:06 -0700393 // Name is the suffix part of the bugreport files - it's typically the date (when invoked with
394 // `-d`), but it could be changed by the user..
395 std::string name_;
396
Felipe Leme1d486fe2016-10-14 18:06:47 -0700397 // Full path of the temporary file containing the bugreport.
Felipe Leme9a523ae2016-10-20 15:10:33 -0700398 std::string tmp_path_;
Felipe Leme1d486fe2016-10-14 18:06:47 -0700399
400 // Full path of the file containing the dumpstate logs.
Felipe Leme9a523ae2016-10-20 15:10:33 -0700401 std::string log_path_;
Felipe Leme1d486fe2016-10-14 18:06:47 -0700402
403 // Pointer to the actual path, be it zip or text.
Felipe Leme9a523ae2016-10-20 15:10:33 -0700404 std::string path_;
Felipe Leme1d486fe2016-10-14 18:06:47 -0700405
406 // Pointer to the zipped file.
407 std::unique_ptr<FILE, int (*)(FILE*)> zip_file{nullptr, fclose};
408
Felipe Lemec6bc8bc2016-10-27 15:58:27 -0700409 // Pointer to the zip structure.
410 std::unique_ptr<ZipWriter> zip_writer_;
411
Vishnu Nair20cf5032018-01-05 13:15:49 -0800412 // Binder object listening to progress.
Felipe Leme75876a22016-10-27 16:31:27 -0700413 android::sp<android::os::IDumpstateListener> listener_;
414 std::string listener_name_;
Vishnu Nair20cf5032018-01-05 13:15:49 -0800415 bool report_section_;
Felipe Leme75876a22016-10-27 16:31:27 -0700416
Luis Hector Chavez91c2ae52018-03-14 15:12:46 -0700417 // List of open tombstone dump files.
418 std::vector<DumpData> tombstone_data_;
419
420 // List of open ANR dump files.
421 std::vector<DumpData> anr_data_;
422
Felipe Lemee844a9d2016-09-21 15:01:39 -0700423 private:
Felipe Lemed80e6b62016-10-03 13:08:14 -0700424 // Used by GetInstance() only.
Felipe Lemef0292972016-11-22 13:57:05 -0800425 Dumpstate(const std::string& version = VERSION_CURRENT);
Felipe Leme062926e2016-10-27 15:51:12 -0700426
427 DISALLOW_COPY_AND_ASSIGN(Dumpstate);
Felipe Lemee844a9d2016-09-21 15:01:39 -0700428};
429
430// for_each_pid_func = void (*)(int, const char*);
431// for_each_tid_func = void (*)(int, int, const char*);
432
433typedef void(for_each_pid_func)(int, const char*);
434typedef void(for_each_tid_func)(int, int, const char*);
Felipe Leme71ca15e2016-05-19 16:18:17 -0700435
Felipe Leme71a74ac2016-03-17 15:43:25 -0700436/* saves the the contents of a file as a long */
437int read_file_as_long(const char *path, long int *output);
438
Christopher Ferris54bcc5f2015-02-10 12:15:01 -0800439/* prints the contents of the fd
440 * fd must have been opened with the flag O_NONBLOCK.
441 */
Christopher Ferris1fe61072014-07-22 16:08:19 -0700442int dump_file_from_fd(const char *title, const char *path, int fd);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700443
Mark Salyzyn326842f2015-04-30 09:49:41 -0700444/* calls skip to gate calling dump_from_fd recursively
445 * in the specified directory. dump_from_fd defaults to
446 * dump_file_from_fd above when set to NULL. skip defaults
447 * to false when set to NULL. dump_from_fd will always be
448 * called with title NULL.
449 */
Felipe Leme678727a2016-09-21 17:22:11 -0700450int dump_files(const std::string& title, const char* dir, bool (*skip)(const char* path),
451 int (*dump_from_fd)(const char* title, const char* path, int fd));
Mark Salyzyn326842f2015-04-30 09:49:41 -0700452
Felipe Leme2628e9e2016-04-12 16:36:51 -0700453/** opens a socket and returns its file descriptor */
454int open_socket(const char *service);
455
Colin Crossf45fa6b2012-03-26 12:38:26 -0700456/* redirect output to a service control socket */
457void redirect_to_socket(FILE *redirect, const char *service);
458
Felipe Leme0f3fb202016-06-10 17:10:53 -0700459/* redirect output to a new file */
Christopher Ferrisff4a4dc2015-02-09 16:24:47 -0800460void redirect_to_file(FILE *redirect, char *path);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700461
Felipe Leme0f3fb202016-06-10 17:10:53 -0700462/* redirect output to an existing file */
463void redirect_to_existing_file(FILE *redirect, char *path);
464
Felipe Leme111b9d02016-02-03 09:28:24 -0800465/* create leading directories, if necessary */
466void create_parent_dirs(const char *path);
467
Jeff Brownbf7f4922012-06-07 16:40:01 -0700468/* dump Dalvik and native stack traces, return the trace file location (NULL if none) */
469const char *dump_traces();
Colin Crossf45fa6b2012-03-26 12:38:26 -0700470
471/* for each process in the system, run the specified function */
Colin Cross0c22e8b2012-11-02 15:46:56 -0700472void for_each_pid(for_each_pid_func func, const char *header);
473
474/* for each thread in the system, run the specified function */
475void for_each_tid(for_each_tid_func func, const char *header);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700476
477/* Displays a blocked processes in-kernel wait channel */
Colin Cross0c22e8b2012-11-02 15:46:56 -0700478void show_wchan(int pid, int tid, const char *name);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700479
Mark Salyzyna297c322016-02-05 15:33:17 -0800480/* Displays a processes times */
481void show_showtime(int pid, const char *name);
482
Colin Crossf45fa6b2012-03-26 12:38:26 -0700483/* Runs "showmap" for a process */
484void do_showmap(int pid, const char *name);
485
486/* Gets the dmesg output for the kernel */
487void do_dmesg();
488
Sreeram Ramachandran2b3bba32014-07-08 15:40:55 -0700489/* Prints the contents of all the routing tables, both IPv4 and IPv6. */
490void dump_route_tables();
491
Colin Crossf45fa6b2012-03-26 12:38:26 -0700492/* Play a sound via Stagefright */
Christopher Ferris1fe61072014-07-22 16:08:19 -0700493void play_sound(const char *path);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700494
Felipe Leme0c80cf02016-01-05 13:25:34 -0800495/* Checks if a given path is a directory. */
496bool is_dir(const char* pathname);
497
498/** Gets the last modification time of a file, or default time if file is not found. */
499time_t get_mtime(int fd, time_t default_mtime);
500
Calvin On249beee2016-06-03 15:17:07 -0700501/* Dumps eMMC Extended CSD data. */
Felipe Leme78f2c862015-12-21 09:55:22 -0800502void dump_emmc_ecsd(const char *ext_csd_path);
503
Calvin On249beee2016-06-03 15:17:07 -0700504/** Gets command-line arguments. */
Felipe Lemea34efb72016-03-11 09:33:32 -0800505void format_args(int argc, const char *argv[], std::string *args);
Felipe Leme88c79332016-02-22 11:06:49 -0800506
Vishnu Nair20cf5032018-01-05 13:15:49 -0800507/** Main entry point for dumpstate. */
508int run_main(int argc, char* argv[]);
509
Felipe Leme8620bb42015-11-10 11:04:45 -0800510#ifdef __cplusplus
511}
512#endif
513
Felipe Leme8268ed22016-08-02 18:18:25 -0700514#endif /* FRAMEWORK_NATIVE_CMD_DUMPSTATE_H_ */