blob: 7e236f19ebbd06e4e4fbc8101c64a7f144b75407 [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 Lemecbce55d2016-02-08 09:53:18 -080020#ifndef MYLOGD
Felipe Leme60869c92016-02-09 16:07:20 -080021#define MYLOGD(...) fprintf(stderr, __VA_ARGS__); ALOGD(__VA_ARGS__);
Felipe Lemecbce55d2016-02-08 09:53:18 -080022#endif
23
24#ifndef MYLOGI
Felipe Leme60869c92016-02-09 16:07:20 -080025#define MYLOGI(...) fprintf(stderr, __VA_ARGS__); ALOGI(__VA_ARGS__);
Felipe Lemecbce55d2016-02-08 09:53:18 -080026#endif
27
28#ifndef MYLOGE
Felipe Leme60869c92016-02-09 16:07:20 -080029#define MYLOGE(...) fprintf(stderr, __VA_ARGS__); ALOGE(__VA_ARGS__);
Felipe Lemecbce55d2016-02-08 09:53:18 -080030#endif
Felipe Leme93d705b2015-11-10 20:10:25 -080031
Colin Crossf45fa6b2012-03-26 12:38:26 -070032#include <time.h>
33#include <unistd.h>
Colin Cross0c22e8b2012-11-02 15:46:56 -070034#include <stdbool.h>
Colin Crossf45fa6b2012-03-26 12:38:26 -070035#include <stdio.h>
Felipe Leme30dbfa12016-09-02 12:43:26 -070036
37#include <string>
Felipe Leme36b3f6f2015-11-19 15:41:04 -080038#include <vector>
Colin Crossf45fa6b2012-03-26 12:38:26 -070039
Felipe Leme35c94f32016-08-12 10:51:54 -070040// Workaround for const char *args[MAX_ARGS_ARRAY_SIZE] variables until they're converted to
41// std::vector<std::string>
Felipe Leme30dbfa12016-09-02 12:43:26 -070042// TODO: remove once not used
Felipe Leme35c94f32016-08-12 10:51:54 -070043#define MAX_ARGS_ARRAY_SIZE 1000
44
Felipe Leme30dbfa12016-09-02 12:43:26 -070045// TODO: remove once moved to HAL
Felipe Leme8620bb42015-11-10 11:04:45 -080046#ifdef __cplusplus
47extern "C" {
48#endif
49
Felipe Leme30dbfa12016-09-02 12:43:26 -070050/*
51 * Defines the Linux user that should be executing a command.
52 */
53enum RootMode {
54 /* Explicitly change the `uid` and `gid` to be `shell`.*/
55 DROP_ROOT,
56 /* Don't change the `uid` and `gid`. */
57 DONT_DROP_ROOT,
58 /* Prefix the command with `/PATH/TO/su root`. Won't work non user builds. */
59 SU_ROOT
60};
61
62/*
63 * Defines what should happen with the `stdout` stream of a command.
64 */
65enum StdoutMode {
66 /* Don't change `stdout`. */
67 NORMAL_STDOUT,
68 /* Redirect `stdout` to `stderr`. */
69 REDIRECT_TO_STDERR
70};
71
72/*
73 * Helper class used to report how long it takes for a section to finish.
74 *
75 * Typical usage:
76 *
77 * DurationReporter duration_reporter(title);
78 *
79 */
80class DurationReporter {
81 public:
Felipe Leme678727a2016-09-21 17:22:11 -070082 DurationReporter(const std::string& title);
83 DurationReporter(const std::string& title, FILE* out);
Felipe Leme30dbfa12016-09-02 12:43:26 -070084
85 ~DurationReporter();
86
Felipe Lemeb0f669d2016-09-26 18:26:11 -070087 static uint64_t Nanotime();
Felipe Leme30dbfa12016-09-02 12:43:26 -070088
89 private:
90 // TODO: use std::string for title, once dump_files() and other places that pass a char* are
91 // refactored as well.
Felipe Leme678727a2016-09-21 17:22:11 -070092 std::string title_;
Felipe Lemeb0f669d2016-09-26 18:26:11 -070093 FILE* out_;
94 uint64_t started_;
Felipe Leme30dbfa12016-09-02 12:43:26 -070095};
96
97/*
98 * Value object used to set command options.
99 *
100 * Typically constructed using a builder with chained setters. Examples:
101 *
102 * CommandOptions::WithTimeout(20).AsRoot().Build();
103 * CommandOptions::WithTimeout(10).Always().RedirectStderr().Build();
104 *
105 * Although the builder could be used to dynamically set values. Example:
106 *
107 * CommandOptions::CommandOptionsBuilder options =
108 * CommandOptions::WithTimeout(10);
109 * if (!is_user_build()) {
110 * options.AsRoot();
111 * }
Felipe Lemeb0f669d2016-09-26 18:26:11 -0700112 * RunCommand("command", {"args"}, options.Build());
Felipe Leme30dbfa12016-09-02 12:43:26 -0700113 */
114class CommandOptions {
115 private:
116 class CommandOptionsValues {
117 private:
118 CommandOptionsValues(long timeout);
119
Felipe Lemeb0f669d2016-09-26 18:26:11 -0700120 long timeout_;
121 bool always_;
122 RootMode rootMode_;
123 StdoutMode stdoutMode_;
124 std::string loggingMessage_;
Felipe Leme30dbfa12016-09-02 12:43:26 -0700125
126 friend class CommandOptions;
127 friend class CommandOptionsBuilder;
128 };
129
130 CommandOptions(const CommandOptionsValues& values);
131
Felipe Lemeb0f669d2016-09-26 18:26:11 -0700132 const CommandOptionsValues values_;
Felipe Leme30dbfa12016-09-02 12:43:26 -0700133
134 public:
135 class CommandOptionsBuilder {
136 public:
137 /* Sets the command to always run, even on `dry-run` mode. */
138 CommandOptionsBuilder& Always();
139 /* Sets the command's RootMode as `SU_ROOT` */
140 CommandOptionsBuilder& AsRoot();
141 /* Sets the command's RootMode as `DROP_ROOT` */
142 CommandOptionsBuilder& DropRoot();
143 /* Sets the command's StdoutMode `REDIRECT_TO_STDERR` */
144 CommandOptionsBuilder& RedirectStderr();
145 /* When not empty, logs a message before executing the command.
146 * Must contain a `%s`, which will be replaced by the full command line, and end on `\n`. */
147 CommandOptionsBuilder& Log(const std::string& message);
148 /* Builds the command options. */
149 CommandOptions Build();
150
151 private:
152 CommandOptionsBuilder(long timeout);
Felipe Lemeb0f669d2016-09-26 18:26:11 -0700153 CommandOptionsValues values_;
Felipe Leme30dbfa12016-09-02 12:43:26 -0700154 friend class CommandOptions;
155 };
156
157 /** Gets the command timeout, in seconds. */
158 long Timeout() const;
159 /* Checks whether the command should always be run, even on dry-run mode. */
160 bool Always() const;
161 /** Gets the RootMode of the command. */
162 RootMode RootMode() const;
163 /** Gets the StdoutMode of the command. */
164 StdoutMode StdoutMode() const;
165 /** Gets the logging message header, it any. */
166 std::string LoggingMessage() const;
167
168 /** Creates a builder with the requied timeout. */
169 static CommandOptionsBuilder WithTimeout(long timeout);
170
171 // Common options.
172 static CommandOptions DEFAULT;
173 static CommandOptions DEFAULT_DUMPSYS;
174 static CommandOptions AS_ROOT_5;
175 static CommandOptions AS_ROOT_10;
176 static CommandOptions AS_ROOT_20;
177};
178
Felipe Lemee844a9d2016-09-21 15:01:39 -0700179/*
180 * Estimated total weight of bugreport generation.
Felipe Leme71bbfc52015-11-23 14:14:51 -0800181 *
182 * Each section contributes to the total weight by an individual weight, so the overall progress
183 * can be calculated by dividing the all completed weight by the total weight.
184 *
185 * This value is defined empirically and it need to be adjusted as more sections are added.
Felipe Lemead5f6c42015-11-30 14:26:46 -0800186 *
187 * It does not need to match the exact sum of all sections, but ideally it should to be slight more
188 * 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 -0700189 * example, jumping from 70% to 100%), while a value too low will cause the progress to get stuck
190 * at an almost-finished value (like 99%) for a while.
Felipe Leme71bbfc52015-11-23 14:14:51 -0800191 */
Felipe Lemee844a9d2016-09-21 15:01:39 -0700192// TODO: move to dumpstate.cpp / utils.cpp once it's used in just one file
Felipe Lemefaf67e32016-03-28 11:15:22 -0700193static const int WEIGHT_TOTAL = 6500;
Felipe Leme71bbfc52015-11-23 14:14:51 -0800194
Felipe Leme71bbfc52015-11-23 14:14:51 -0800195/*
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700196 * List of supported zip format versions.
197 *
198 * See bugreport-format.md for more info.
199 */
200static std::string VERSION_DEFAULT = "1.0";
201
202/*
Felipe Lemee844a9d2016-09-21 15:01:39 -0700203 * Main class driving a bugreport generation.
204 *
205 * Currently, it only contains variables that are accessed externally, but gradually the functions
206 * that are spread accross utils.cpp and dumpstate.cpp will be moved to it.
207 */
208class Dumpstate {
Felipe Leme4c2d6632016-09-28 14:32:00 -0700209 friend class DumpstateTest;
210
Felipe Lemee844a9d2016-09-21 15:01:39 -0700211 public:
212 static Dumpstate& GetInstance();
Felipe Leme71bbfc52015-11-23 14:14:51 -0800213
Felipe Lemee844a9d2016-09-21 15:01:39 -0700214 /*
215 * When running in dry-run mode, skips the real dumps and just print the section headers.
216 *
217 * Useful when debugging dumpstate or other bugreport-related activities.
218 *
219 * Dry-run mode is enabled by setting the system property dumpstate.dry_run to true.
220 */
Felipe Leme2b9b06c2016-10-14 09:13:06 -0700221 bool IsDryRun() const;
Felipe Leme71ca15e2016-05-19 16:18:17 -0700222
Felipe Leme678727a2016-09-21 17:22:11 -0700223 /*
Felipe Leme6ad9c062016-09-28 11:58:36 -0700224 * Gets whether device is running a `user` build.
225 */
Felipe Leme2b9b06c2016-10-14 09:13:06 -0700226 bool IsUserBuild() const;
Felipe Leme6ad9c062016-09-28 11:58:36 -0700227
228 /*
Felipe Leme678727a2016-09-21 17:22:11 -0700229 * Forks a command, waits for it to finish, and returns its status.
230 *
231 * |title| description of the command printed on `stdout` (or empty to skip
232 * description).
233 * |full_command| array containing the command (first entry) and its arguments.
234 * Must contain at least one element.
235 * |options| optional argument defining the command's behavior.
236 */
237 int RunCommand(const std::string& title, const std::vector<std::string>& fullCommand,
238 const CommandOptions& options = CommandOptions::DEFAULT);
239
240 /*
241 * Runs `dumpsys` with the given arguments, automatically setting its timeout
242 * (`-t` argument)
243 * according to the command options.
244 *
245 * |title| description of the command printed on `stdout` (or empty to skip
246 * description).
247 * |dumpsys_args| `dumpsys` arguments (except `-t`).
248 * |options| optional argument defining the command's behavior.
Felipe Leme6ad9c062016-09-28 11:58:36 -0700249 * |dumpsysTimeout| when > 0, defines the value passed to `dumpsys -t` (otherwise it uses the
250 * timeout from `options`)
Felipe Leme678727a2016-09-21 17:22:11 -0700251 */
252 void RunDumpsys(const std::string& title, const std::vector<std::string>& dumpsysArgs,
253 const CommandOptions& options = CommandOptions::DEFAULT_DUMPSYS,
254 long dumpsysTimeout = 0);
255
256 /*
257 * Prints the contents of a file.
258 *
259 * |title| description of the command printed on `stdout` (or empty to skip
260 * description).
261 * |path| location of the file to be dumped.
262 */
263 int DumpFile(const std::string& title, const std::string& path);
264
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700265 /*
266 * Takes a screenshot and save it to the given `path`.
267 *
268 * If `path` is empty, uses a standard path based on the bugreport name.
269 */
270 void TakeScreenshot(const std::string& path = "");
271
Felipe Lemed80e6b62016-10-03 13:08:14 -0700272 // TODO: members below should be private once refactor is finished
273
274 /*
275 * Updates the overall progress of the bugreport generation by the given weight increment.
276 */
277 void UpdateProgress(int delta);
278
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700279 /* Prints the dumpstate header on `stdout`. */
Felipe Leme2b9b06c2016-10-14 09:13:06 -0700280 void PrintHeader() const;
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700281
282 /* Gets the path of a bugreport file with the given suffix. */
Felipe Leme2b9b06c2016-10-14 09:13:06 -0700283 std::string GetPath(const std::string& suffix) const;
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700284
Felipe Lemee844a9d2016-09-21 15:01:39 -0700285 // TODO: initialize fields on constructor
286
287 // dumpstate id - unique after each device reboot.
288 unsigned long id_;
289
290 // Whether progress updates should be published.
291 bool updateProgress_ = false;
292
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700293 // Whether it should take an screenshot earlier in the process.
294 bool doEarlyScreenshot_ = false;
295
Felipe Lemee844a9d2016-09-21 15:01:39 -0700296 // Currrent progress.
297 int progress_ = 0;
298
299 // Total estimated progress.
300 int weightTotal_ = WEIGHT_TOTAL;
301
302 // When set, defines a socket file-descriptor use to report progress to bugreportz.
303 int controlSocketFd_ = -1;
304
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700305 // Bugreport format version;
306 std::string version_ = VERSION_DEFAULT;
Felipe Lemee844a9d2016-09-21 15:01:39 -0700307
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700308 // Command-line arguments as string
309 std::string args_;
310
311 // Extra options passed as system property.
312 std::string extraOptions_;
313
314 // Full path of the directory where the bugreport files will be written.
Felipe Lemee844a9d2016-09-21 15:01:39 -0700315 std::string bugreportDir_;
316
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700317 // Full path of the temporary file containing the screenshot (when requested).
318 std::string screenshotPath_;
319
320 time_t now_;
321
Felipe Leme2b9b06c2016-10-14 09:13:06 -0700322 // Base name (without suffix or extensions) of the bugreport files, typically
323 // `bugreport-BUILD_ID`.
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700324 std::string baseName_;
325
Felipe Leme2b9b06c2016-10-14 09:13:06 -0700326 // Name is the suffix part of the bugreport files - it's typically the date (when invoked with
327 // `-d`), but it could be changed by the user..
328 std::string name_;
329
Felipe Lemee844a9d2016-09-21 15:01:39 -0700330 private:
Felipe Lemed80e6b62016-10-03 13:08:14 -0700331 // Used by GetInstance() only.
332 Dumpstate(bool dryRun = false, const std::string& buildType = "user");
333
Felipe Leme2b9b06c2016-10-14 09:13:06 -0700334 // Internal version of RunCommand that just runs it, without updating progress.
335 int JustRunCommand(const char* command, const char* path, std::vector<const char*>& args,
336 const CommandOptions& options) const;
337
338 // Internal version of RunCommand that just dumps it, without updating progress.
339 int JustDumpFile(const std::string& title, const std::string& path) const;
340
Felipe Leme4c2d6632016-09-28 14:32:00 -0700341 // Whether this is a dry run.
342 bool dryRun_;
343
Felipe Lemed80e6b62016-10-03 13:08:14 -0700344 // Build type (such as 'user' or 'eng').
345 std::string buildType_;
Felipe Lemee844a9d2016-09-21 15:01:39 -0700346};
347
348// for_each_pid_func = void (*)(int, const char*);
349// for_each_tid_func = void (*)(int, int, const char*);
350
351typedef void(for_each_pid_func)(int, const char*);
352typedef void(for_each_tid_func)(int, int, const char*);
Felipe Leme71ca15e2016-05-19 16:18:17 -0700353
354/* adds a new entry to the existing zip file. */
355bool add_zip_entry(const std::string& entry_name, const std::string& entry_path);
356
357/* adds a new entry to the existing zip file. */
358bool add_zip_entry_from_fd(const std::string& entry_name, int fd);
359
Calvin On249beee2016-06-03 15:17:07 -0700360/* adds all files from a directory to the zipped bugreport file */
Felipe Leme678727a2016-09-21 17:22:11 -0700361void add_dir(const std::string& dir, bool recursive);
Calvin On249beee2016-06-03 15:17:07 -0700362
Felipe Leme71a74ac2016-03-17 15:43:25 -0700363/* saves the the contents of a file as a long */
364int read_file_as_long(const char *path, long int *output);
365
Christopher Ferris54bcc5f2015-02-10 12:15:01 -0800366/* prints the contents of the fd
367 * fd must have been opened with the flag O_NONBLOCK.
368 */
Christopher Ferris1fe61072014-07-22 16:08:19 -0700369int dump_file_from_fd(const char *title, const char *path, int fd);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700370
Mark Salyzyn326842f2015-04-30 09:49:41 -0700371/* calls skip to gate calling dump_from_fd recursively
372 * in the specified directory. dump_from_fd defaults to
373 * dump_file_from_fd above when set to NULL. skip defaults
374 * to false when set to NULL. dump_from_fd will always be
375 * called with title NULL.
376 */
Felipe Leme678727a2016-09-21 17:22:11 -0700377int dump_files(const std::string& title, const char* dir, bool (*skip)(const char* path),
378 int (*dump_from_fd)(const char* title, const char* path, int fd));
Mark Salyzyn326842f2015-04-30 09:49:41 -0700379
Felipe Lemecf6a8b42016-03-11 10:38:19 -0800380/* switch to non-root user and group */
381bool drop_root_user();
Felipe Leme93d705b2015-11-10 20:10:25 -0800382
Felipe Leme36b3f6f2015-11-19 15:41:04 -0800383/* sends a broadcast using Activity Manager */
384void send_broadcast(const std::string& action, const std::vector<std::string>& args);
385
Colin Crossf45fa6b2012-03-26 12:38:26 -0700386/* prints all the system properties */
387void print_properties();
388
Felipe Leme2628e9e2016-04-12 16:36:51 -0700389/** opens a socket and returns its file descriptor */
390int open_socket(const char *service);
391
Colin Crossf45fa6b2012-03-26 12:38:26 -0700392/* redirect output to a service control socket */
393void redirect_to_socket(FILE *redirect, const char *service);
394
Felipe Leme0f3fb202016-06-10 17:10:53 -0700395/* redirect output to a new file */
Christopher Ferrisff4a4dc2015-02-09 16:24:47 -0800396void redirect_to_file(FILE *redirect, char *path);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700397
Felipe Leme0f3fb202016-06-10 17:10:53 -0700398/* redirect output to an existing file */
399void redirect_to_existing_file(FILE *redirect, char *path);
400
Felipe Leme111b9d02016-02-03 09:28:24 -0800401/* create leading directories, if necessary */
402void create_parent_dirs(const char *path);
403
Jeff Brownbf7f4922012-06-07 16:40:01 -0700404/* dump Dalvik and native stack traces, return the trace file location (NULL if none) */
405const char *dump_traces();
Colin Crossf45fa6b2012-03-26 12:38:26 -0700406
407/* for each process in the system, run the specified function */
Colin Cross0c22e8b2012-11-02 15:46:56 -0700408void for_each_pid(for_each_pid_func func, const char *header);
409
410/* for each thread in the system, run the specified function */
411void for_each_tid(for_each_tid_func func, const char *header);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700412
413/* Displays a blocked processes in-kernel wait channel */
Colin Cross0c22e8b2012-11-02 15:46:56 -0700414void show_wchan(int pid, int tid, const char *name);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700415
Mark Salyzyna297c322016-02-05 15:33:17 -0800416/* Displays a processes times */
417void show_showtime(int pid, const char *name);
418
Colin Crossf45fa6b2012-03-26 12:38:26 -0700419/* Runs "showmap" for a process */
420void do_showmap(int pid, const char *name);
421
422/* Gets the dmesg output for the kernel */
423void do_dmesg();
424
Sreeram Ramachandran2b3bba32014-07-08 15:40:55 -0700425/* Prints the contents of all the routing tables, both IPv4 and IPv6. */
426void dump_route_tables();
427
Colin Crossf45fa6b2012-03-26 12:38:26 -0700428/* Play a sound via Stagefright */
Christopher Ferris1fe61072014-07-22 16:08:19 -0700429void play_sound(const char *path);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700430
431/* Implemented by libdumpstate_board to dump board-specific info */
432void dumpstate_board();
433
Felipe Leme0c80cf02016-01-05 13:25:34 -0800434/* Vibrates for a given durating (in milliseconds). */
435void vibrate(FILE* vibrator, int ms);
436
437/* Checks if a given path is a directory. */
438bool is_dir(const char* pathname);
439
440/** Gets the last modification time of a file, or default time if file is not found. */
441time_t get_mtime(int fd, time_t default_mtime);
442
Calvin On249beee2016-06-03 15:17:07 -0700443/* Dumps eMMC Extended CSD data. */
Felipe Leme78f2c862015-12-21 09:55:22 -0800444void dump_emmc_ecsd(const char *ext_csd_path);
445
Calvin On249beee2016-06-03 15:17:07 -0700446/** Gets command-line arguments. */
Felipe Lemea34efb72016-03-11 09:33:32 -0800447void format_args(int argc, const char *argv[], std::string *args);
Felipe Leme88c79332016-02-22 11:06:49 -0800448
Calvin On249beee2016-06-03 15:17:07 -0700449/** Tells if the device is running a user build. */
450bool is_user_build();
451
Felipe Leme8620bb42015-11-10 11:04:45 -0800452#ifdef __cplusplus
453}
454#endif
455
Felipe Leme8268ed22016-08-02 18:18:25 -0700456#endif /* FRAMEWORK_NATIVE_CMD_DUMPSTATE_H_ */