blob: 19f46e2663c2d08ef0d2ee0aef86d63320344a12 [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
Felipe Leme062926e2016-10-27 15:51:12 -070020#include <android-base/macros.h>
21
Felipe Lemecbce55d2016-02-08 09:53:18 -080022#ifndef MYLOGD
Felipe Leme60869c92016-02-09 16:07:20 -080023#define MYLOGD(...) fprintf(stderr, __VA_ARGS__); ALOGD(__VA_ARGS__);
Felipe Lemecbce55d2016-02-08 09:53:18 -080024#endif
25
26#ifndef MYLOGI
Felipe Leme60869c92016-02-09 16:07:20 -080027#define MYLOGI(...) fprintf(stderr, __VA_ARGS__); ALOGI(__VA_ARGS__);
Felipe Lemecbce55d2016-02-08 09:53:18 -080028#endif
29
30#ifndef MYLOGE
Felipe Leme60869c92016-02-09 16:07:20 -080031#define MYLOGE(...) fprintf(stderr, __VA_ARGS__); ALOGE(__VA_ARGS__);
Felipe Lemecbce55d2016-02-08 09:53:18 -080032#endif
Felipe Leme93d705b2015-11-10 20:10:25 -080033
Colin Crossf45fa6b2012-03-26 12:38:26 -070034#include <time.h>
35#include <unistd.h>
Colin Cross0c22e8b2012-11-02 15:46:56 -070036#include <stdbool.h>
Colin Crossf45fa6b2012-03-26 12:38:26 -070037#include <stdio.h>
Felipe Leme30dbfa12016-09-02 12:43:26 -070038
39#include <string>
Felipe Leme36b3f6f2015-11-19 15:41:04 -080040#include <vector>
Colin Crossf45fa6b2012-03-26 12:38:26 -070041
Felipe Leme35c94f32016-08-12 10:51:54 -070042// Workaround for const char *args[MAX_ARGS_ARRAY_SIZE] variables until they're converted to
43// std::vector<std::string>
Felipe Leme30dbfa12016-09-02 12:43:26 -070044// TODO: remove once not used
Felipe Leme35c94f32016-08-12 10:51:54 -070045#define MAX_ARGS_ARRAY_SIZE 1000
46
Felipe Leme30dbfa12016-09-02 12:43:26 -070047// TODO: remove once moved to HAL
Felipe Leme8620bb42015-11-10 11:04:45 -080048#ifdef __cplusplus
49extern "C" {
50#endif
51
Felipe Leme30dbfa12016-09-02 12:43:26 -070052/*
53 * Defines the Linux user that should be executing a command.
54 */
55enum RootMode {
56 /* Explicitly change the `uid` and `gid` to be `shell`.*/
57 DROP_ROOT,
58 /* Don't change the `uid` and `gid`. */
59 DONT_DROP_ROOT,
60 /* Prefix the command with `/PATH/TO/su root`. Won't work non user builds. */
61 SU_ROOT
62};
63
64/*
65 * Defines what should happen with the `stdout` stream of a command.
66 */
67enum StdoutMode {
68 /* Don't change `stdout`. */
69 NORMAL_STDOUT,
70 /* Redirect `stdout` to `stderr`. */
71 REDIRECT_TO_STDERR
72};
73
74/*
75 * Helper class used to report how long it takes for a section to finish.
76 *
77 * Typical usage:
78 *
79 * DurationReporter duration_reporter(title);
80 *
81 */
82class DurationReporter {
83 public:
Felipe Leme678727a2016-09-21 17:22:11 -070084 DurationReporter(const std::string& title);
85 DurationReporter(const std::string& title, FILE* out);
Felipe Leme30dbfa12016-09-02 12:43:26 -070086
87 ~DurationReporter();
88
Felipe Lemeb0f669d2016-09-26 18:26:11 -070089 static uint64_t Nanotime();
Felipe Leme30dbfa12016-09-02 12:43:26 -070090
91 private:
92 // TODO: use std::string for title, once dump_files() and other places that pass a char* are
93 // refactored as well.
Felipe Leme678727a2016-09-21 17:22:11 -070094 std::string title_;
Felipe Lemeb0f669d2016-09-26 18:26:11 -070095 FILE* out_;
96 uint64_t started_;
Felipe Leme062926e2016-10-27 15:51:12 -070097
98 DISALLOW_COPY_AND_ASSIGN(DurationReporter);
Felipe Leme30dbfa12016-09-02 12:43:26 -070099};
100
101/*
102 * Value object used to set command options.
103 *
104 * Typically constructed using a builder with chained setters. Examples:
105 *
106 * CommandOptions::WithTimeout(20).AsRoot().Build();
107 * CommandOptions::WithTimeout(10).Always().RedirectStderr().Build();
108 *
109 * Although the builder could be used to dynamically set values. Example:
110 *
111 * CommandOptions::CommandOptionsBuilder options =
112 * CommandOptions::WithTimeout(10);
113 * if (!is_user_build()) {
114 * options.AsRoot();
115 * }
Felipe Lemeb0f669d2016-09-26 18:26:11 -0700116 * RunCommand("command", {"args"}, options.Build());
Felipe Leme30dbfa12016-09-02 12:43:26 -0700117 */
118class CommandOptions {
119 private:
120 class CommandOptionsValues {
121 private:
122 CommandOptionsValues(long timeout);
123
Felipe Lemeb0f669d2016-09-26 18:26:11 -0700124 long timeout_;
125 bool always_;
Felipe Leme9a523ae2016-10-20 15:10:33 -0700126 RootMode root_mode_;
127 StdoutMode stdout_mode_;
128 std::string logging_message_;
Felipe Leme30dbfa12016-09-02 12:43:26 -0700129
130 friend class CommandOptions;
131 friend class CommandOptionsBuilder;
132 };
133
134 CommandOptions(const CommandOptionsValues& values);
135
Felipe Leme9a523ae2016-10-20 15:10:33 -0700136 const CommandOptionsValues values;
Felipe Leme30dbfa12016-09-02 12:43:26 -0700137
138 public:
139 class CommandOptionsBuilder {
140 public:
141 /* Sets the command to always run, even on `dry-run` mode. */
142 CommandOptionsBuilder& Always();
143 /* Sets the command's RootMode as `SU_ROOT` */
144 CommandOptionsBuilder& AsRoot();
145 /* Sets the command's RootMode as `DROP_ROOT` */
146 CommandOptionsBuilder& DropRoot();
147 /* Sets the command's StdoutMode `REDIRECT_TO_STDERR` */
148 CommandOptionsBuilder& RedirectStderr();
149 /* When not empty, logs a message before executing the command.
150 * Must contain a `%s`, which will be replaced by the full command line, and end on `\n`. */
151 CommandOptionsBuilder& Log(const std::string& message);
152 /* Builds the command options. */
153 CommandOptions Build();
154
155 private:
156 CommandOptionsBuilder(long timeout);
Felipe Leme9a523ae2016-10-20 15:10:33 -0700157 CommandOptionsValues values;
Felipe Leme30dbfa12016-09-02 12:43:26 -0700158 friend class CommandOptions;
159 };
160
161 /** Gets the command timeout, in seconds. */
162 long Timeout() const;
163 /* Checks whether the command should always be run, even on dry-run mode. */
164 bool Always() const;
165 /** Gets the RootMode of the command. */
166 RootMode RootMode() const;
167 /** Gets the StdoutMode of the command. */
168 StdoutMode StdoutMode() const;
169 /** Gets the logging message header, it any. */
170 std::string LoggingMessage() const;
171
172 /** Creates a builder with the requied timeout. */
173 static CommandOptionsBuilder WithTimeout(long timeout);
174
175 // Common options.
176 static CommandOptions DEFAULT;
177 static CommandOptions DEFAULT_DUMPSYS;
178 static CommandOptions AS_ROOT_5;
179 static CommandOptions AS_ROOT_10;
180 static CommandOptions AS_ROOT_20;
181};
182
Felipe Lemee844a9d2016-09-21 15:01:39 -0700183/*
184 * Estimated total weight of bugreport generation.
Felipe Leme71bbfc52015-11-23 14:14:51 -0800185 *
186 * Each section contributes to the total weight by an individual weight, so the overall progress
187 * can be calculated by dividing the all completed weight by the total weight.
188 *
189 * This value is defined empirically and it need to be adjusted as more sections are added.
Felipe Lemead5f6c42015-11-30 14:26:46 -0800190 *
191 * It does not need to match the exact sum of all sections, but ideally it should to be slight more
192 * than such sum: a value too high will cause the bugreport to finish before the user expected (for
Felipe Lemefaf67e32016-03-28 11:15:22 -0700193 * example, jumping from 70% to 100%), while a value too low will cause the progress to get stuck
194 * at an almost-finished value (like 99%) for a while.
Felipe Leme71bbfc52015-11-23 14:14:51 -0800195 */
Felipe Lemee844a9d2016-09-21 15:01:39 -0700196// TODO: move to dumpstate.cpp / utils.cpp once it's used in just one file
Felipe Lemefaf67e32016-03-28 11:15:22 -0700197static const int WEIGHT_TOTAL = 6500;
Felipe Leme71bbfc52015-11-23 14:14:51 -0800198
Felipe Leme71bbfc52015-11-23 14:14:51 -0800199/*
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700200 * List of supported zip format versions.
201 *
202 * See bugreport-format.md for more info.
203 */
Felipe Lemed071c682016-10-20 16:48:00 -0700204static std::string VERSION_CURRENT = "1.0";
205
206/*
Felipe Lemee184f662016-10-27 10:04:47 -0700207 * Temporary version that adds a anr-traces.txt entry. Once tools support it, the current version
208 * will be bumped to 2.0-dev-1.
209 */
210static std::string VERSION_SPLIT_ANR = "2.0-dev-1";
211
212/*
Felipe Lemed071c682016-10-20 16:48:00 -0700213 * "Alias" for the current version.
214 */
215static std::string VERSION_DEFAULT = "default";
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700216
217/*
Felipe Lemee844a9d2016-09-21 15:01:39 -0700218 * Main class driving a bugreport generation.
219 *
220 * Currently, it only contains variables that are accessed externally, but gradually the functions
221 * that are spread accross utils.cpp and dumpstate.cpp will be moved to it.
222 */
223class Dumpstate {
Felipe Leme4c2d6632016-09-28 14:32:00 -0700224 friend class DumpstateTest;
225
Felipe Lemee844a9d2016-09-21 15:01:39 -0700226 public:
227 static Dumpstate& GetInstance();
Felipe Leme71bbfc52015-11-23 14:14:51 -0800228
Felipe Lemee844a9d2016-09-21 15:01:39 -0700229 /*
230 * When running in dry-run mode, skips the real dumps and just print the section headers.
231 *
232 * Useful when debugging dumpstate or other bugreport-related activities.
233 *
234 * Dry-run mode is enabled by setting the system property dumpstate.dry_run to true.
235 */
Felipe Leme2b9b06c2016-10-14 09:13:06 -0700236 bool IsDryRun() const;
Felipe Leme71ca15e2016-05-19 16:18:17 -0700237
Felipe Leme678727a2016-09-21 17:22:11 -0700238 /*
Felipe Leme6ad9c062016-09-28 11:58:36 -0700239 * Gets whether device is running a `user` build.
240 */
Felipe Leme2b9b06c2016-10-14 09:13:06 -0700241 bool IsUserBuild() const;
Felipe Leme6ad9c062016-09-28 11:58:36 -0700242
Felipe Leme1d486fe2016-10-14 18:06:47 -0700243 /* Checkes whether dumpstate is generating a zipped bugreport. */
244 bool IsZipping() const;
245
Felipe Leme6ad9c062016-09-28 11:58:36 -0700246 /*
Felipe Leme678727a2016-09-21 17:22:11 -0700247 * Forks a command, waits for it to finish, and returns its status.
248 *
249 * |title| description of the command printed on `stdout` (or empty to skip
250 * description).
251 * |full_command| array containing the command (first entry) and its arguments.
252 * Must contain at least one element.
253 * |options| optional argument defining the command's behavior.
254 */
255 int RunCommand(const std::string& title, const std::vector<std::string>& fullCommand,
256 const CommandOptions& options = CommandOptions::DEFAULT);
257
258 /*
259 * Runs `dumpsys` with the given arguments, automatically setting its timeout
260 * (`-t` argument)
261 * according to the command options.
262 *
263 * |title| description of the command printed on `stdout` (or empty to skip
264 * description).
265 * |dumpsys_args| `dumpsys` arguments (except `-t`).
266 * |options| optional argument defining the command's behavior.
Felipe Leme9a523ae2016-10-20 15:10:33 -0700267 * |dumpsys_timeout| when > 0, defines the value passed to `dumpsys -t` (otherwise it uses the
Felipe Leme6ad9c062016-09-28 11:58:36 -0700268 * timeout from `options`)
Felipe Leme678727a2016-09-21 17:22:11 -0700269 */
Felipe Leme9a523ae2016-10-20 15:10:33 -0700270 void RunDumpsys(const std::string& title, const std::vector<std::string>& dumpsys_args,
Felipe Leme678727a2016-09-21 17:22:11 -0700271 const CommandOptions& options = CommandOptions::DEFAULT_DUMPSYS,
Felipe Leme9a523ae2016-10-20 15:10:33 -0700272 long dumpsys_timeout = 0);
Felipe Leme678727a2016-09-21 17:22:11 -0700273
274 /*
275 * Prints the contents of a file.
276 *
277 * |title| description of the command printed on `stdout` (or empty to skip
278 * description).
279 * |path| location of the file to be dumped.
280 */
281 int DumpFile(const std::string& title, const std::string& path);
282
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700283 /*
Felipe Leme1d486fe2016-10-14 18:06:47 -0700284 * Adds a new entry to the existing zip file.
285 * */
286 bool AddZipEntry(const std::string& entry_name, const std::string& entry_path);
287
288 /*
289 * Adds a new entry to the existing zip file.
290 */
291 bool AddZipEntryFromFd(const std::string& entry_name, int fd);
292
293 /*
294 * Adds a text entry entry to the existing zip file.
295 */
296 bool AddTextZipEntry(const std::string& entry_name, const std::string& content);
297
298 /*
299 * Adds all files from a directory to the zipped bugreport file.
300 */
301 void AddDir(const std::string& dir, bool recursive);
302
303 /*
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700304 * Takes a screenshot and save it to the given `path`.
305 *
306 * If `path` is empty, uses a standard path based on the bugreport name.
307 */
308 void TakeScreenshot(const std::string& path = "");
309
Felipe Lemed80e6b62016-10-03 13:08:14 -0700310 // TODO: members below should be private once refactor is finished
311
312 /*
313 * Updates the overall progress of the bugreport generation by the given weight increment.
314 */
315 void UpdateProgress(int delta);
316
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700317 /* Prints the dumpstate header on `stdout`. */
Felipe Leme2b9b06c2016-10-14 09:13:06 -0700318 void PrintHeader() const;
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700319
Felipe Leme1d486fe2016-10-14 18:06:47 -0700320 /*
321 * Adds the temporary report to the existing .zip file, closes the .zip file, and removes the
322 * temporary file.
323 */
324 bool FinishZipFile();
325
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700326 /* Gets the path of a bugreport file with the given suffix. */
Felipe Leme2b9b06c2016-10-14 09:13:06 -0700327 std::string GetPath(const std::string& suffix) const;
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700328
Felipe Lemee844a9d2016-09-21 15:01:39 -0700329 // TODO: initialize fields on constructor
330
331 // dumpstate id - unique after each device reboot.
332 unsigned long id_;
333
334 // Whether progress updates should be published.
Felipe Leme9a523ae2016-10-20 15:10:33 -0700335 bool update_progress_ = false;
Felipe Lemee844a9d2016-09-21 15:01:39 -0700336
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700337 // Whether it should take an screenshot earlier in the process.
Felipe Leme9a523ae2016-10-20 15:10:33 -0700338 bool do_early_screenshot_ = false;
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700339
Felipe Lemee844a9d2016-09-21 15:01:39 -0700340 // Currrent progress.
341 int progress_ = 0;
342
343 // Total estimated progress.
Felipe Leme9a523ae2016-10-20 15:10:33 -0700344 int weight_total_ = WEIGHT_TOTAL;
Felipe Lemee844a9d2016-09-21 15:01:39 -0700345
346 // When set, defines a socket file-descriptor use to report progress to bugreportz.
Felipe Leme9a523ae2016-10-20 15:10:33 -0700347 int control_socket_fd_ = -1;
Felipe Lemee844a9d2016-09-21 15:01:39 -0700348
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700349 // Bugreport format version;
Felipe Lemed071c682016-10-20 16:48:00 -0700350 std::string version_ = VERSION_CURRENT;
Felipe Lemee844a9d2016-09-21 15:01:39 -0700351
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700352 // Command-line arguments as string
353 std::string args_;
354
355 // Extra options passed as system property.
Felipe Leme9a523ae2016-10-20 15:10:33 -0700356 std::string extra_options_;
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700357
358 // Full path of the directory where the bugreport files will be written.
Felipe Leme9a523ae2016-10-20 15:10:33 -0700359 std::string bugreport_dir_;
Felipe Lemee844a9d2016-09-21 15:01:39 -0700360
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700361 // Full path of the temporary file containing the screenshot (when requested).
Felipe Leme9a523ae2016-10-20 15:10:33 -0700362 std::string screenshot_path_;
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700363
364 time_t now_;
365
Felipe Leme2b9b06c2016-10-14 09:13:06 -0700366 // Base name (without suffix or extensions) of the bugreport files, typically
367 // `bugreport-BUILD_ID`.
Felipe Leme9a523ae2016-10-20 15:10:33 -0700368 std::string base_name_;
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700369
Felipe Leme2b9b06c2016-10-14 09:13:06 -0700370 // Name is the suffix part of the bugreport files - it's typically the date (when invoked with
371 // `-d`), but it could be changed by the user..
372 std::string name_;
373
Felipe Leme1d486fe2016-10-14 18:06:47 -0700374 // Full path of the temporary file containing the bugreport.
Felipe Leme9a523ae2016-10-20 15:10:33 -0700375 std::string tmp_path_;
Felipe Leme1d486fe2016-10-14 18:06:47 -0700376
377 // Full path of the file containing the dumpstate logs.
Felipe Leme9a523ae2016-10-20 15:10:33 -0700378 std::string log_path_;
Felipe Leme1d486fe2016-10-14 18:06:47 -0700379
380 // Pointer to the actual path, be it zip or text.
Felipe Leme9a523ae2016-10-20 15:10:33 -0700381 std::string path_;
Felipe Leme1d486fe2016-10-14 18:06:47 -0700382
383 // Pointer to the zipped file.
384 std::unique_ptr<FILE, int (*)(FILE*)> zip_file{nullptr, fclose};
385
Felipe Lemee844a9d2016-09-21 15:01:39 -0700386 private:
Felipe Lemed80e6b62016-10-03 13:08:14 -0700387 // Used by GetInstance() only.
Felipe Lemed071c682016-10-20 16:48:00 -0700388 Dumpstate(const std::string& version = VERSION_CURRENT, bool dry_run = false,
389 const std::string& build_type = "user");
Felipe Lemed80e6b62016-10-03 13:08:14 -0700390
Felipe Leme2b9b06c2016-10-14 09:13:06 -0700391 // Internal version of RunCommand that just runs it, without updating progress.
392 int JustRunCommand(const char* command, const char* path, std::vector<const char*>& args,
393 const CommandOptions& options) const;
394
395 // Internal version of RunCommand that just dumps it, without updating progress.
396 int JustDumpFile(const std::string& title, const std::string& path) const;
397
Felipe Leme4c2d6632016-09-28 14:32:00 -0700398 // Whether this is a dry run.
Felipe Leme9a523ae2016-10-20 15:10:33 -0700399 bool dry_run_;
Felipe Leme4c2d6632016-09-28 14:32:00 -0700400
Felipe Lemed80e6b62016-10-03 13:08:14 -0700401 // Build type (such as 'user' or 'eng').
Felipe Leme9a523ae2016-10-20 15:10:33 -0700402 std::string build_type_;
Felipe Leme062926e2016-10-27 15:51:12 -0700403
404 DISALLOW_COPY_AND_ASSIGN(Dumpstate);
Felipe Lemee844a9d2016-09-21 15:01:39 -0700405};
406
407// for_each_pid_func = void (*)(int, const char*);
408// for_each_tid_func = void (*)(int, int, const char*);
409
410typedef void(for_each_pid_func)(int, const char*);
411typedef void(for_each_tid_func)(int, int, const char*);
Felipe Leme71ca15e2016-05-19 16:18:17 -0700412
Felipe Leme71a74ac2016-03-17 15:43:25 -0700413/* saves the the contents of a file as a long */
414int read_file_as_long(const char *path, long int *output);
415
Christopher Ferris54bcc5f2015-02-10 12:15:01 -0800416/* prints the contents of the fd
417 * fd must have been opened with the flag O_NONBLOCK.
418 */
Christopher Ferris1fe61072014-07-22 16:08:19 -0700419int dump_file_from_fd(const char *title, const char *path, int fd);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700420
Mark Salyzyn326842f2015-04-30 09:49:41 -0700421/* calls skip to gate calling dump_from_fd recursively
422 * in the specified directory. dump_from_fd defaults to
423 * dump_file_from_fd above when set to NULL. skip defaults
424 * to false when set to NULL. dump_from_fd will always be
425 * called with title NULL.
426 */
Felipe Leme678727a2016-09-21 17:22:11 -0700427int dump_files(const std::string& title, const char* dir, bool (*skip)(const char* path),
428 int (*dump_from_fd)(const char* title, const char* path, int fd));
Mark Salyzyn326842f2015-04-30 09:49:41 -0700429
Felipe Lemecf6a8b42016-03-11 10:38:19 -0800430/* switch to non-root user and group */
431bool drop_root_user();
Felipe Leme93d705b2015-11-10 20:10:25 -0800432
Felipe Leme36b3f6f2015-11-19 15:41:04 -0800433/* sends a broadcast using Activity Manager */
434void send_broadcast(const std::string& action, const std::vector<std::string>& args);
435
Colin Crossf45fa6b2012-03-26 12:38:26 -0700436/* prints all the system properties */
437void print_properties();
438
Felipe Leme2628e9e2016-04-12 16:36:51 -0700439/** opens a socket and returns its file descriptor */
440int open_socket(const char *service);
441
Colin Crossf45fa6b2012-03-26 12:38:26 -0700442/* redirect output to a service control socket */
443void redirect_to_socket(FILE *redirect, const char *service);
444
Felipe Leme0f3fb202016-06-10 17:10:53 -0700445/* redirect output to a new file */
Christopher Ferrisff4a4dc2015-02-09 16:24:47 -0800446void redirect_to_file(FILE *redirect, char *path);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700447
Felipe Leme0f3fb202016-06-10 17:10:53 -0700448/* redirect output to an existing file */
449void redirect_to_existing_file(FILE *redirect, char *path);
450
Felipe Leme111b9d02016-02-03 09:28:24 -0800451/* create leading directories, if necessary */
452void create_parent_dirs(const char *path);
453
Jeff Brownbf7f4922012-06-07 16:40:01 -0700454/* dump Dalvik and native stack traces, return the trace file location (NULL if none) */
455const char *dump_traces();
Colin Crossf45fa6b2012-03-26 12:38:26 -0700456
457/* for each process in the system, run the specified function */
Colin Cross0c22e8b2012-11-02 15:46:56 -0700458void for_each_pid(for_each_pid_func func, const char *header);
459
460/* for each thread in the system, run the specified function */
461void for_each_tid(for_each_tid_func func, const char *header);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700462
463/* Displays a blocked processes in-kernel wait channel */
Colin Cross0c22e8b2012-11-02 15:46:56 -0700464void show_wchan(int pid, int tid, const char *name);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700465
Mark Salyzyna297c322016-02-05 15:33:17 -0800466/* Displays a processes times */
467void show_showtime(int pid, const char *name);
468
Colin Crossf45fa6b2012-03-26 12:38:26 -0700469/* Runs "showmap" for a process */
470void do_showmap(int pid, const char *name);
471
472/* Gets the dmesg output for the kernel */
473void do_dmesg();
474
Sreeram Ramachandran2b3bba32014-07-08 15:40:55 -0700475/* Prints the contents of all the routing tables, both IPv4 and IPv6. */
476void dump_route_tables();
477
Colin Crossf45fa6b2012-03-26 12:38:26 -0700478/* Play a sound via Stagefright */
Christopher Ferris1fe61072014-07-22 16:08:19 -0700479void play_sound(const char *path);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700480
481/* Implemented by libdumpstate_board to dump board-specific info */
482void dumpstate_board();
483
Felipe Leme0c80cf02016-01-05 13:25:34 -0800484/* Vibrates for a given durating (in milliseconds). */
485void vibrate(FILE* vibrator, int ms);
486
487/* Checks if a given path is a directory. */
488bool is_dir(const char* pathname);
489
490/** Gets the last modification time of a file, or default time if file is not found. */
491time_t get_mtime(int fd, time_t default_mtime);
492
Calvin On249beee2016-06-03 15:17:07 -0700493/* Dumps eMMC Extended CSD data. */
Felipe Leme78f2c862015-12-21 09:55:22 -0800494void dump_emmc_ecsd(const char *ext_csd_path);
495
Calvin On249beee2016-06-03 15:17:07 -0700496/** Gets command-line arguments. */
Felipe Lemea34efb72016-03-11 09:33:32 -0800497void format_args(int argc, const char *argv[], std::string *args);
Felipe Leme88c79332016-02-22 11:06:49 -0800498
Calvin On249beee2016-06-03 15:17:07 -0700499/** Tells if the device is running a user build. */
500bool is_user_build();
501
Felipe Leme8620bb42015-11-10 11:04:45 -0800502#ifdef __cplusplus
503}
504#endif
505
Felipe Leme8268ed22016-08-02 18:18:25 -0700506#endif /* FRAMEWORK_NATIVE_CMD_DUMPSTATE_H_ */