blob: fcf0683b249ac618c774b85869d4605397dedf38 [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>
29#include <ziparchive/zip_writer.h>
30
Felipe Lemebda15a02016-11-16 17:48:25 -080031#include "DumpstateUtil.h"
Felipe Leme75876a22016-10-27 16:31:27 -070032#include "android/os/BnDumpstate.h"
33
Felipe Leme35c94f32016-08-12 10:51:54 -070034// Workaround for const char *args[MAX_ARGS_ARRAY_SIZE] variables until they're converted to
35// std::vector<std::string>
Felipe Leme30dbfa12016-09-02 12:43:26 -070036// TODO: remove once not used
Felipe Leme35c94f32016-08-12 10:51:54 -070037#define MAX_ARGS_ARRAY_SIZE 1000
38
Felipe Leme30dbfa12016-09-02 12:43:26 -070039// TODO: remove once moved to HAL
Felipe Leme8620bb42015-11-10 11:04:45 -080040#ifdef __cplusplus
41extern "C" {
42#endif
43
Felipe Leme30dbfa12016-09-02 12:43:26 -070044/*
Felipe Leme30dbfa12016-09-02 12:43:26 -070045 * Helper class used to report how long it takes for a section to finish.
46 *
47 * Typical usage:
48 *
49 * DurationReporter duration_reporter(title);
50 *
51 */
52class DurationReporter {
53 public:
Felipe Leme46b85da2016-11-21 17:40:45 -080054 DurationReporter(const std::string& title, bool log_only = false);
Felipe Leme30dbfa12016-09-02 12:43:26 -070055
56 ~DurationReporter();
57
Felipe Leme30dbfa12016-09-02 12:43:26 -070058 private:
Felipe Leme678727a2016-09-21 17:22:11 -070059 std::string title_;
Felipe Leme46b85da2016-11-21 17:40:45 -080060 bool log_only_;
Felipe Lemeb0f669d2016-09-26 18:26:11 -070061 uint64_t started_;
Felipe Leme062926e2016-10-27 15:51:12 -070062
63 DISALLOW_COPY_AND_ASSIGN(DurationReporter);
Felipe Leme30dbfa12016-09-02 12:43:26 -070064};
65
66/*
Felipe Leme7447d7c2016-11-03 18:12:22 -070067 * Keeps track of current progress and estimated max, saving stats on file to tune up future runs.
Felipe Leme71bbfc52015-11-23 14:14:51 -080068 *
Felipe Leme7447d7c2016-11-03 18:12:22 -070069 * Each `dumpstate` section contributes to the total weight by an individual weight, so the overall
70 * progress can be calculated by dividing the estimate max progress by the current progress.
Felipe Leme71bbfc52015-11-23 14:14:51 -080071 *
Felipe Leme7447d7c2016-11-03 18:12:22 -070072 * The estimated max progress is initially set to a value (`kDefaultMax) defined empirically, but
73 * it's adjusted after each dumpstate run by storing the average duration in a file.
Felipe Lemead5f6c42015-11-30 14:26:46 -080074 *
Felipe Leme71bbfc52015-11-23 14:14:51 -080075 */
Felipe Leme7447d7c2016-11-03 18:12:22 -070076class Progress {
77 friend class ProgressTest;
78 friend class DumpstateTest;
79
80 public:
81 /*
82 * Default estimation of the max duration of a bugreport generation.
83 *
84 * It does not need to match the exact sum of all sections, but ideally it should to be slight
85 * more than such sum: a value too high will cause the bugreport to finish before the user
86 * expected (for example, jumping from 70% to 100%), while a value too low will cause the
87 * progress to get stuck at an almost-finished value (like 99%) for a while.
88 *
89 * This constant is only used when the average duration from previous runs cannot be used.
90 */
91 static const int kDefaultMax;
92
93 Progress(const std::string& path = "");
94
95 // Gets the current progress.
96 int32_t Get() const;
97
98 // Gets the current estimated max progress.
99 int32_t GetMax() const;
100
101 // Gets the initial estimated max progress.
102 int32_t GetInitialMax() const;
103
104 // Increments progress (ignored if not positive).
105 // Returns `true` if the max progress increased as well.
106 bool Inc(int32_t delta);
107
108 // Persist the stats.
109 void Save();
110
111 void Dump(int fd, const std::string& prefix) const;
112
113 private:
114 Progress(int32_t initial_max, float growth_factor,
115 const std::string& path = ""); // Used by test cases.
116 Progress(int32_t initial_max, int32_t progress, float growth_factor); // Used by test cases.
117 void Load();
118 int32_t initial_max_;
119 int32_t progress_;
120 int32_t max_;
121 float growth_factor_;
122 int32_t n_runs_;
123 int32_t average_max_;
124 const std::string& path_;
125};
Felipe Leme71bbfc52015-11-23 14:14:51 -0800126
Felipe Leme71bbfc52015-11-23 14:14:51 -0800127/*
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700128 * List of supported zip format versions.
129 *
130 * See bugreport-format.md for more info.
131 */
Felipe Lemed071c682016-10-20 16:48:00 -0700132static std::string VERSION_CURRENT = "1.0";
133
134/*
Felipe Lemee184f662016-10-27 10:04:47 -0700135 * Temporary version that adds a anr-traces.txt entry. Once tools support it, the current version
136 * will be bumped to 2.0-dev-1.
137 */
138static std::string VERSION_SPLIT_ANR = "2.0-dev-1";
139
140/*
Felipe Lemed071c682016-10-20 16:48:00 -0700141 * "Alias" for the current version.
142 */
143static std::string VERSION_DEFAULT = "default";
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700144
145/*
Felipe Lemee844a9d2016-09-21 15:01:39 -0700146 * Main class driving a bugreport generation.
147 *
148 * Currently, it only contains variables that are accessed externally, but gradually the functions
149 * that are spread accross utils.cpp and dumpstate.cpp will be moved to it.
150 */
151class Dumpstate {
Felipe Leme4c2d6632016-09-28 14:32:00 -0700152 friend class DumpstateTest;
153
Felipe Lemee844a9d2016-09-21 15:01:39 -0700154 public:
Felipe Lemebda15a02016-11-16 17:48:25 -0800155 static CommandOptions DEFAULT_DUMPSYS;
156
Felipe Lemee844a9d2016-09-21 15:01:39 -0700157 static Dumpstate& GetInstance();
Felipe Leme71bbfc52015-11-23 14:14:51 -0800158
Felipe Lemef0292972016-11-22 13:57:05 -0800159 // TODO: temporary function until device code uses PropertiesHelper::IsUserBuild()
160 bool IsUserBuild();
Felipe Leme6ad9c062016-09-28 11:58:36 -0700161
Felipe Leme1d486fe2016-10-14 18:06:47 -0700162 /* Checkes whether dumpstate is generating a zipped bugreport. */
163 bool IsZipping() const;
164
Felipe Leme6ad9c062016-09-28 11:58:36 -0700165 /*
Felipe Leme678727a2016-09-21 17:22:11 -0700166 * Forks a command, waits for it to finish, and returns its status.
167 *
168 * |title| description of the command printed on `stdout` (or empty to skip
169 * description).
170 * |full_command| array containing the command (first entry) and its arguments.
171 * Must contain at least one element.
172 * |options| optional argument defining the command's behavior.
173 */
174 int RunCommand(const std::string& title, const std::vector<std::string>& fullCommand,
175 const CommandOptions& options = CommandOptions::DEFAULT);
176
177 /*
178 * Runs `dumpsys` with the given arguments, automatically setting its timeout
179 * (`-t` argument)
180 * according to the command options.
181 *
182 * |title| description of the command printed on `stdout` (or empty to skip
183 * description).
184 * |dumpsys_args| `dumpsys` arguments (except `-t`).
185 * |options| optional argument defining the command's behavior.
Felipe Leme9a523ae2016-10-20 15:10:33 -0700186 * |dumpsys_timeout| when > 0, defines the value passed to `dumpsys -t` (otherwise it uses the
Felipe Leme6ad9c062016-09-28 11:58:36 -0700187 * timeout from `options`)
Felipe Leme678727a2016-09-21 17:22:11 -0700188 */
Felipe Leme9a523ae2016-10-20 15:10:33 -0700189 void RunDumpsys(const std::string& title, const std::vector<std::string>& dumpsys_args,
Felipe Lemebda15a02016-11-16 17:48:25 -0800190 const CommandOptions& options = DEFAULT_DUMPSYS, long dumpsys_timeout = 0);
Felipe Leme678727a2016-09-21 17:22:11 -0700191
192 /*
193 * Prints the contents of a file.
194 *
195 * |title| description of the command printed on `stdout` (or empty to skip
196 * description).
197 * |path| location of the file to be dumped.
198 */
199 int DumpFile(const std::string& title, const std::string& path);
200
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700201 /*
Felipe Leme1d486fe2016-10-14 18:06:47 -0700202 * Adds a new entry to the existing zip file.
203 * */
204 bool AddZipEntry(const std::string& entry_name, const std::string& entry_path);
205
206 /*
207 * Adds a new entry to the existing zip file.
208 */
209 bool AddZipEntryFromFd(const std::string& entry_name, int fd);
210
211 /*
212 * Adds a text entry entry to the existing zip file.
213 */
214 bool AddTextZipEntry(const std::string& entry_name, const std::string& content);
215
216 /*
217 * Adds all files from a directory to the zipped bugreport file.
218 */
219 void AddDir(const std::string& dir, bool recursive);
220
221 /*
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700222 * Takes a screenshot and save it to the given `path`.
223 *
224 * If `path` is empty, uses a standard path based on the bugreport name.
225 */
226 void TakeScreenshot(const std::string& path = "");
227
Felipe Leme7447d7c2016-11-03 18:12:22 -0700228 /////////////////////////////////////////////////////////////////////
229 // TODO: members below should be private once refactor is finished //
230 /////////////////////////////////////////////////////////////////////
231
232 // TODO: temporary method until Dumpstate object is properly set
233 void SetProgress(std::unique_ptr<Progress> progress);
Felipe Lemed80e6b62016-10-03 13:08:14 -0700234
Felipe Leme6f674ae2016-11-18 17:10:33 -0800235 void DumpstateBoard();
236
Felipe Lemed80e6b62016-10-03 13:08:14 -0700237 /*
238 * Updates the overall progress of the bugreport generation by the given weight increment.
239 */
Felipe Leme7447d7c2016-11-03 18:12:22 -0700240 void UpdateProgress(int32_t delta);
Felipe Lemed80e6b62016-10-03 13:08:14 -0700241
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700242 /* Prints the dumpstate header on `stdout`. */
Felipe Leme2b9b06c2016-10-14 09:13:06 -0700243 void PrintHeader() const;
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700244
Felipe Leme1d486fe2016-10-14 18:06:47 -0700245 /*
246 * Adds the temporary report to the existing .zip file, closes the .zip file, and removes the
247 * temporary file.
248 */
249 bool FinishZipFile();
250
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700251 /* Gets the path of a bugreport file with the given suffix. */
Felipe Leme2b9b06c2016-10-14 09:13:06 -0700252 std::string GetPath(const std::string& suffix) const;
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700253
Felipe Lemee844a9d2016-09-21 15:01:39 -0700254 // TODO: initialize fields on constructor
255
256 // dumpstate id - unique after each device reboot.
Felipe Leme7447d7c2016-11-03 18:12:22 -0700257 uint32_t id_;
Felipe Lemee844a9d2016-09-21 15:01:39 -0700258
Felipe Leme75876a22016-10-27 16:31:27 -0700259 // dumpstate pid
260 pid_t pid_;
261
Felipe Lemee844a9d2016-09-21 15:01:39 -0700262 // Whether progress updates should be published.
Felipe Leme9a523ae2016-10-20 15:10:33 -0700263 bool update_progress_ = false;
Felipe Lemee844a9d2016-09-21 15:01:39 -0700264
Felipe Leme009ecbb2016-11-07 10:18:44 -0800265 // How frequently the progess should be updated;the listener will only be notificated when the
266 // delta from the previous update is more than the threshold.
267 int32_t update_progress_threshold_ = 100;
268
269 // Last progress that triggered a listener updated
270 int32_t last_updated_progress_;
271
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700272 // Whether it should take an screenshot earlier in the process.
Felipe Leme9a523ae2016-10-20 15:10:33 -0700273 bool do_early_screenshot_ = false;
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700274
Felipe Leme7447d7c2016-11-03 18:12:22 -0700275 std::unique_ptr<Progress> progress_;
Felipe Lemee844a9d2016-09-21 15:01:39 -0700276
277 // When set, defines a socket file-descriptor use to report progress to bugreportz.
Felipe Leme9a523ae2016-10-20 15:10:33 -0700278 int control_socket_fd_ = -1;
Felipe Lemee844a9d2016-09-21 15:01:39 -0700279
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700280 // Bugreport format version;
Felipe Lemed071c682016-10-20 16:48:00 -0700281 std::string version_ = VERSION_CURRENT;
Felipe Lemee844a9d2016-09-21 15:01:39 -0700282
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700283 // Command-line arguments as string
284 std::string args_;
285
286 // Extra options passed as system property.
Felipe Leme9a523ae2016-10-20 15:10:33 -0700287 std::string extra_options_;
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700288
289 // Full path of the directory where the bugreport files will be written.
Felipe Leme9a523ae2016-10-20 15:10:33 -0700290 std::string bugreport_dir_;
Felipe Lemee844a9d2016-09-21 15:01:39 -0700291
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700292 // Full path of the temporary file containing the screenshot (when requested).
Felipe Leme9a523ae2016-10-20 15:10:33 -0700293 std::string screenshot_path_;
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700294
295 time_t now_;
296
Felipe Leme2b9b06c2016-10-14 09:13:06 -0700297 // Base name (without suffix or extensions) of the bugreport files, typically
298 // `bugreport-BUILD_ID`.
Felipe Leme9a523ae2016-10-20 15:10:33 -0700299 std::string base_name_;
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700300
Felipe Leme2b9b06c2016-10-14 09:13:06 -0700301 // Name is the suffix part of the bugreport files - it's typically the date (when invoked with
302 // `-d`), but it could be changed by the user..
303 std::string name_;
304
Felipe Leme1d486fe2016-10-14 18:06:47 -0700305 // Full path of the temporary file containing the bugreport.
Felipe Leme9a523ae2016-10-20 15:10:33 -0700306 std::string tmp_path_;
Felipe Leme1d486fe2016-10-14 18:06:47 -0700307
308 // Full path of the file containing the dumpstate logs.
Felipe Leme9a523ae2016-10-20 15:10:33 -0700309 std::string log_path_;
Felipe Leme1d486fe2016-10-14 18:06:47 -0700310
311 // Pointer to the actual path, be it zip or text.
Felipe Leme9a523ae2016-10-20 15:10:33 -0700312 std::string path_;
Felipe Leme1d486fe2016-10-14 18:06:47 -0700313
314 // Pointer to the zipped file.
315 std::unique_ptr<FILE, int (*)(FILE*)> zip_file{nullptr, fclose};
316
Felipe Lemec6bc8bc2016-10-27 15:58:27 -0700317 // Pointer to the zip structure.
318 std::unique_ptr<ZipWriter> zip_writer_;
319
Felipe Leme75876a22016-10-27 16:31:27 -0700320 // Binder object listing to progress.
321 android::sp<android::os::IDumpstateListener> listener_;
322 std::string listener_name_;
323
Felipe Lemee844a9d2016-09-21 15:01:39 -0700324 private:
Felipe Lemed80e6b62016-10-03 13:08:14 -0700325 // Used by GetInstance() only.
Felipe Lemef0292972016-11-22 13:57:05 -0800326 Dumpstate(const std::string& version = VERSION_CURRENT);
Felipe Leme062926e2016-10-27 15:51:12 -0700327
328 DISALLOW_COPY_AND_ASSIGN(Dumpstate);
Felipe Lemee844a9d2016-09-21 15:01:39 -0700329};
330
331// for_each_pid_func = void (*)(int, const char*);
332// for_each_tid_func = void (*)(int, int, const char*);
333
334typedef void(for_each_pid_func)(int, const char*);
335typedef void(for_each_tid_func)(int, int, const char*);
Felipe Leme71ca15e2016-05-19 16:18:17 -0700336
Felipe Leme71a74ac2016-03-17 15:43:25 -0700337/* saves the the contents of a file as a long */
338int read_file_as_long(const char *path, long int *output);
339
Christopher Ferris54bcc5f2015-02-10 12:15:01 -0800340/* prints the contents of the fd
341 * fd must have been opened with the flag O_NONBLOCK.
342 */
Christopher Ferris1fe61072014-07-22 16:08:19 -0700343int dump_file_from_fd(const char *title, const char *path, int fd);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700344
Mark Salyzyn326842f2015-04-30 09:49:41 -0700345/* calls skip to gate calling dump_from_fd recursively
346 * in the specified directory. dump_from_fd defaults to
347 * dump_file_from_fd above when set to NULL. skip defaults
348 * to false when set to NULL. dump_from_fd will always be
349 * called with title NULL.
350 */
Felipe Leme678727a2016-09-21 17:22:11 -0700351int dump_files(const std::string& title, const char* dir, bool (*skip)(const char* path),
352 int (*dump_from_fd)(const char* title, const char* path, int fd));
Mark Salyzyn326842f2015-04-30 09:49:41 -0700353
Felipe Leme36b3f6f2015-11-19 15:41:04 -0800354/* sends a broadcast using Activity Manager */
355void send_broadcast(const std::string& action, const std::vector<std::string>& args);
356
Colin Crossf45fa6b2012-03-26 12:38:26 -0700357/* prints all the system properties */
358void print_properties();
359
Felipe Leme2628e9e2016-04-12 16:36:51 -0700360/** opens a socket and returns its file descriptor */
361int open_socket(const char *service);
362
Colin Crossf45fa6b2012-03-26 12:38:26 -0700363/* redirect output to a service control socket */
364void redirect_to_socket(FILE *redirect, const char *service);
365
Felipe Leme0f3fb202016-06-10 17:10:53 -0700366/* redirect output to a new file */
Christopher Ferrisff4a4dc2015-02-09 16:24:47 -0800367void redirect_to_file(FILE *redirect, char *path);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700368
Felipe Leme0f3fb202016-06-10 17:10:53 -0700369/* redirect output to an existing file */
370void redirect_to_existing_file(FILE *redirect, char *path);
371
Felipe Leme111b9d02016-02-03 09:28:24 -0800372/* create leading directories, if necessary */
373void create_parent_dirs(const char *path);
374
Jeff Brownbf7f4922012-06-07 16:40:01 -0700375/* dump Dalvik and native stack traces, return the trace file location (NULL if none) */
376const char *dump_traces();
Colin Crossf45fa6b2012-03-26 12:38:26 -0700377
378/* for each process in the system, run the specified function */
Colin Cross0c22e8b2012-11-02 15:46:56 -0700379void for_each_pid(for_each_pid_func func, const char *header);
380
381/* for each thread in the system, run the specified function */
382void for_each_tid(for_each_tid_func func, const char *header);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700383
384/* Displays a blocked processes in-kernel wait channel */
Colin Cross0c22e8b2012-11-02 15:46:56 -0700385void show_wchan(int pid, int tid, const char *name);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700386
Mark Salyzyna297c322016-02-05 15:33:17 -0800387/* Displays a processes times */
388void show_showtime(int pid, const char *name);
389
Colin Crossf45fa6b2012-03-26 12:38:26 -0700390/* Runs "showmap" for a process */
391void do_showmap(int pid, const char *name);
392
393/* Gets the dmesg output for the kernel */
394void do_dmesg();
395
Sreeram Ramachandran2b3bba32014-07-08 15:40:55 -0700396/* Prints the contents of all the routing tables, both IPv4 and IPv6. */
397void dump_route_tables();
398
Colin Crossf45fa6b2012-03-26 12:38:26 -0700399/* Play a sound via Stagefright */
Christopher Ferris1fe61072014-07-22 16:08:19 -0700400void play_sound(const char *path);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700401
402/* Implemented by libdumpstate_board to dump board-specific info */
403void dumpstate_board();
404
Felipe Leme0c80cf02016-01-05 13:25:34 -0800405/* Vibrates for a given durating (in milliseconds). */
406void vibrate(FILE* vibrator, int ms);
407
408/* Checks if a given path is a directory. */
409bool is_dir(const char* pathname);
410
411/** Gets the last modification time of a file, or default time if file is not found. */
412time_t get_mtime(int fd, time_t default_mtime);
413
Calvin On249beee2016-06-03 15:17:07 -0700414/* Dumps eMMC Extended CSD data. */
Felipe Leme78f2c862015-12-21 09:55:22 -0800415void dump_emmc_ecsd(const char *ext_csd_path);
416
Calvin On249beee2016-06-03 15:17:07 -0700417/** Gets command-line arguments. */
Felipe Lemea34efb72016-03-11 09:33:32 -0800418void format_args(int argc, const char *argv[], std::string *args);
Felipe Leme88c79332016-02-22 11:06:49 -0800419
Felipe Leme8620bb42015-11-10 11:04:45 -0800420#ifdef __cplusplus
421}
422#endif
423
Felipe Leme8268ed22016-08-02 18:18:25 -0700424#endif /* FRAMEWORK_NATIVE_CMD_DUMPSTATE_H_ */