xref: /aosp_15_r20/frameworks/native/cmds/dumpstate/dumpstate.h (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
1*38e8c45fSAndroid Build Coastguard Worker /*
2*38e8c45fSAndroid Build Coastguard Worker  * Copyright (C) 2008 The Android Open Source Project
3*38e8c45fSAndroid Build Coastguard Worker  *
4*38e8c45fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*38e8c45fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*38e8c45fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*38e8c45fSAndroid Build Coastguard Worker  *
8*38e8c45fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*38e8c45fSAndroid Build Coastguard Worker  *
10*38e8c45fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*38e8c45fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*38e8c45fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*38e8c45fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*38e8c45fSAndroid Build Coastguard Worker  * limitations under the License.
15*38e8c45fSAndroid Build Coastguard Worker  */
16*38e8c45fSAndroid Build Coastguard Worker 
17*38e8c45fSAndroid Build Coastguard Worker #ifndef FRAMEWORK_NATIVE_CMD_DUMPSTATE_H_
18*38e8c45fSAndroid Build Coastguard Worker #define FRAMEWORK_NATIVE_CMD_DUMPSTATE_H_
19*38e8c45fSAndroid Build Coastguard Worker 
20*38e8c45fSAndroid Build Coastguard Worker #include <time.h>
21*38e8c45fSAndroid Build Coastguard Worker #include <unistd.h>
22*38e8c45fSAndroid Build Coastguard Worker #include <stdbool.h>
23*38e8c45fSAndroid Build Coastguard Worker #include <stdio.h>
24*38e8c45fSAndroid Build Coastguard Worker 
25*38e8c45fSAndroid Build Coastguard Worker #include <string>
26*38e8c45fSAndroid Build Coastguard Worker #include <vector>
27*38e8c45fSAndroid Build Coastguard Worker 
28*38e8c45fSAndroid Build Coastguard Worker #include <aidl/android/hardware/dumpstate/IDumpstateDevice.h>
29*38e8c45fSAndroid Build Coastguard Worker #include <android-base/macros.h>
30*38e8c45fSAndroid Build Coastguard Worker #include <android-base/unique_fd.h>
31*38e8c45fSAndroid Build Coastguard Worker #include <android/hardware/dumpstate/1.1/types.h>
32*38e8c45fSAndroid Build Coastguard Worker #include <android/os/BnIncidentAuthListener.h>
33*38e8c45fSAndroid Build Coastguard Worker #include <android/os/IDumpstate.h>
34*38e8c45fSAndroid Build Coastguard Worker #include <android/os/IDumpstateListener.h>
35*38e8c45fSAndroid Build Coastguard Worker #include <utils/StrongPointer.h>
36*38e8c45fSAndroid Build Coastguard Worker #include <ziparchive/zip_writer.h>
37*38e8c45fSAndroid Build Coastguard Worker 
38*38e8c45fSAndroid Build Coastguard Worker #include "DumpstateUtil.h"
39*38e8c45fSAndroid Build Coastguard Worker #include "DumpPool.h"
40*38e8c45fSAndroid Build Coastguard Worker #include "TaskQueue.h"
41*38e8c45fSAndroid Build Coastguard Worker 
42*38e8c45fSAndroid Build Coastguard Worker // TODO: move everything under this namespace
43*38e8c45fSAndroid Build Coastguard Worker // TODO: and then remove explicitly android::os::dumpstate:: prefixes
44*38e8c45fSAndroid Build Coastguard Worker namespace android {
45*38e8c45fSAndroid Build Coastguard Worker namespace os {
46*38e8c45fSAndroid Build Coastguard Worker 
47*38e8c45fSAndroid Build Coastguard Worker struct DumpstateOptions;
48*38e8c45fSAndroid Build Coastguard Worker 
49*38e8c45fSAndroid Build Coastguard Worker namespace dumpstate {
50*38e8c45fSAndroid Build Coastguard Worker 
51*38e8c45fSAndroid Build Coastguard Worker class DumpstateTest;
52*38e8c45fSAndroid Build Coastguard Worker class ProgressTest;
53*38e8c45fSAndroid Build Coastguard Worker class ZippedBugReportStreamTest;
54*38e8c45fSAndroid Build Coastguard Worker 
55*38e8c45fSAndroid Build Coastguard Worker }  // namespace dumpstate
56*38e8c45fSAndroid Build Coastguard Worker }  // namespace os
57*38e8c45fSAndroid Build Coastguard Worker }  // namespace android
58*38e8c45fSAndroid Build Coastguard Worker 
59*38e8c45fSAndroid Build Coastguard Worker class ZipWriter;
60*38e8c45fSAndroid Build Coastguard Worker 
61*38e8c45fSAndroid Build Coastguard Worker // TODO: remove once moved to HAL
62*38e8c45fSAndroid Build Coastguard Worker #ifdef __cplusplus
63*38e8c45fSAndroid Build Coastguard Worker extern "C" {
64*38e8c45fSAndroid Build Coastguard Worker #endif
65*38e8c45fSAndroid Build Coastguard Worker 
66*38e8c45fSAndroid Build Coastguard Worker /*
67*38e8c45fSAndroid Build Coastguard Worker  * Helper class used to report how long it takes for a section to finish.
68*38e8c45fSAndroid Build Coastguard Worker  *
69*38e8c45fSAndroid Build Coastguard Worker  * Typical usage:
70*38e8c45fSAndroid Build Coastguard Worker  *
71*38e8c45fSAndroid Build Coastguard Worker  *    DurationReporter duration_reporter(title);
72*38e8c45fSAndroid Build Coastguard Worker  *
73*38e8c45fSAndroid Build Coastguard Worker  */
74*38e8c45fSAndroid Build Coastguard Worker class DurationReporter {
75*38e8c45fSAndroid Build Coastguard Worker   public:
76*38e8c45fSAndroid Build Coastguard Worker     explicit DurationReporter(const std::string& title, bool logcat_only = false,
77*38e8c45fSAndroid Build Coastguard Worker                               bool verbose = false, int duration_fd = STDOUT_FILENO);
78*38e8c45fSAndroid Build Coastguard Worker 
79*38e8c45fSAndroid Build Coastguard Worker     ~DurationReporter();
80*38e8c45fSAndroid Build Coastguard Worker 
81*38e8c45fSAndroid Build Coastguard Worker   private:
82*38e8c45fSAndroid Build Coastguard Worker     std::string title_;
83*38e8c45fSAndroid Build Coastguard Worker     bool logcat_only_;
84*38e8c45fSAndroid Build Coastguard Worker     bool verbose_;
85*38e8c45fSAndroid Build Coastguard Worker     uint64_t started_;
86*38e8c45fSAndroid Build Coastguard Worker     int duration_fd_;
87*38e8c45fSAndroid Build Coastguard Worker 
88*38e8c45fSAndroid Build Coastguard Worker     DISALLOW_COPY_AND_ASSIGN(DurationReporter);
89*38e8c45fSAndroid Build Coastguard Worker };
90*38e8c45fSAndroid Build Coastguard Worker 
91*38e8c45fSAndroid Build Coastguard Worker /*
92*38e8c45fSAndroid Build Coastguard Worker  * Keeps track of current progress and estimated max, saving stats on file to tune up future runs.
93*38e8c45fSAndroid Build Coastguard Worker  *
94*38e8c45fSAndroid Build Coastguard Worker  * Each `dumpstate` section contributes to the total weight by an individual weight, so the overall
95*38e8c45fSAndroid Build Coastguard Worker  * progress can be calculated by dividing the estimate max progress by the current progress.
96*38e8c45fSAndroid Build Coastguard Worker  *
97*38e8c45fSAndroid Build Coastguard Worker  * The estimated max progress is initially set to a value (`kDefaultMax) defined empirically, but
98*38e8c45fSAndroid Build Coastguard Worker  * it's adjusted after each dumpstate run by storing the average duration in a file.
99*38e8c45fSAndroid Build Coastguard Worker  *
100*38e8c45fSAndroid Build Coastguard Worker  */
101*38e8c45fSAndroid Build Coastguard Worker class Progress {
102*38e8c45fSAndroid Build Coastguard Worker     friend class android::os::dumpstate::ProgressTest;
103*38e8c45fSAndroid Build Coastguard Worker     friend class android::os::dumpstate::DumpstateTest;
104*38e8c45fSAndroid Build Coastguard Worker 
105*38e8c45fSAndroid Build Coastguard Worker   public:
106*38e8c45fSAndroid Build Coastguard Worker     /*
107*38e8c45fSAndroid Build Coastguard Worker      * Default estimation of the max duration of a bugreport generation.
108*38e8c45fSAndroid Build Coastguard Worker      *
109*38e8c45fSAndroid Build Coastguard Worker      * It does not need to match the exact sum of all sections, but ideally it should to be slight
110*38e8c45fSAndroid Build Coastguard Worker      * more than such sum: a value too high will cause the bugreport to finish before the user
111*38e8c45fSAndroid Build Coastguard Worker      * expected (for example, jumping from 70% to 100%), while a value too low will cause the
112*38e8c45fSAndroid Build Coastguard Worker      * progress to get stuck at an almost-finished value (like 99%) for a while.
113*38e8c45fSAndroid Build Coastguard Worker      *
114*38e8c45fSAndroid Build Coastguard Worker      * This constant is only used when the average duration from previous runs cannot be used.
115*38e8c45fSAndroid Build Coastguard Worker      */
116*38e8c45fSAndroid Build Coastguard Worker     static const int kDefaultMax;
117*38e8c45fSAndroid Build Coastguard Worker 
118*38e8c45fSAndroid Build Coastguard Worker     explicit Progress(const std::string& path = "");
119*38e8c45fSAndroid Build Coastguard Worker 
120*38e8c45fSAndroid Build Coastguard Worker     // Gets the current progress.
121*38e8c45fSAndroid Build Coastguard Worker     int32_t Get() const;
122*38e8c45fSAndroid Build Coastguard Worker 
123*38e8c45fSAndroid Build Coastguard Worker     // Gets the current estimated max progress.
124*38e8c45fSAndroid Build Coastguard Worker     int32_t GetMax() const;
125*38e8c45fSAndroid Build Coastguard Worker 
126*38e8c45fSAndroid Build Coastguard Worker     // Gets the initial estimated max progress.
127*38e8c45fSAndroid Build Coastguard Worker     int32_t GetInitialMax() const;
128*38e8c45fSAndroid Build Coastguard Worker 
129*38e8c45fSAndroid Build Coastguard Worker     // Increments progress (ignored if not positive).
130*38e8c45fSAndroid Build Coastguard Worker     // Returns `true` if the max progress increased as well.
131*38e8c45fSAndroid Build Coastguard Worker     bool Inc(int32_t delta);
132*38e8c45fSAndroid Build Coastguard Worker 
133*38e8c45fSAndroid Build Coastguard Worker     // Persist the stats.
134*38e8c45fSAndroid Build Coastguard Worker     void Save();
135*38e8c45fSAndroid Build Coastguard Worker 
136*38e8c45fSAndroid Build Coastguard Worker     void Dump(int fd, const std::string& prefix) const;
137*38e8c45fSAndroid Build Coastguard Worker 
138*38e8c45fSAndroid Build Coastguard Worker   private:
139*38e8c45fSAndroid Build Coastguard Worker     Progress(int32_t initial_max, float growth_factor,
140*38e8c45fSAndroid Build Coastguard Worker              const std::string& path = "");                                // Used by test cases.
141*38e8c45fSAndroid Build Coastguard Worker     Progress(int32_t initial_max, int32_t progress, float growth_factor);  // Used by test cases.
142*38e8c45fSAndroid Build Coastguard Worker     void Load();
143*38e8c45fSAndroid Build Coastguard Worker     int32_t initial_max_;
144*38e8c45fSAndroid Build Coastguard Worker     int32_t progress_;
145*38e8c45fSAndroid Build Coastguard Worker     int32_t max_;
146*38e8c45fSAndroid Build Coastguard Worker     float growth_factor_;
147*38e8c45fSAndroid Build Coastguard Worker     int32_t n_runs_;
148*38e8c45fSAndroid Build Coastguard Worker     int32_t average_max_;
149*38e8c45fSAndroid Build Coastguard Worker     std::string path_;
150*38e8c45fSAndroid Build Coastguard Worker };
151*38e8c45fSAndroid Build Coastguard Worker 
152*38e8c45fSAndroid Build Coastguard Worker /*
153*38e8c45fSAndroid Build Coastguard Worker  * List of supported zip format versions.
154*38e8c45fSAndroid Build Coastguard Worker  *
155*38e8c45fSAndroid Build Coastguard Worker  * See bugreport-format.md for more info.
156*38e8c45fSAndroid Build Coastguard Worker  */
157*38e8c45fSAndroid Build Coastguard Worker static std::string VERSION_CURRENT = "2.0";
158*38e8c45fSAndroid Build Coastguard Worker 
159*38e8c45fSAndroid Build Coastguard Worker /*
160*38e8c45fSAndroid Build Coastguard Worker  * "Alias" for the current version.
161*38e8c45fSAndroid Build Coastguard Worker  */
162*38e8c45fSAndroid Build Coastguard Worker static std::string VERSION_DEFAULT = "default";
163*38e8c45fSAndroid Build Coastguard Worker 
164*38e8c45fSAndroid Build Coastguard Worker /*
165*38e8c45fSAndroid Build Coastguard Worker  * Directory used by Dumpstate binary to keep its local files.
166*38e8c45fSAndroid Build Coastguard Worker  */
167*38e8c45fSAndroid Build Coastguard Worker static const std::string DUMPSTATE_DIRECTORY = "/bugreports";
168*38e8c45fSAndroid Build Coastguard Worker 
169*38e8c45fSAndroid Build Coastguard Worker /*
170*38e8c45fSAndroid Build Coastguard Worker  * Structure that contains the information of an open dump file.
171*38e8c45fSAndroid Build Coastguard Worker  */
172*38e8c45fSAndroid Build Coastguard Worker struct DumpData {
173*38e8c45fSAndroid Build Coastguard Worker     // Path of the file.
174*38e8c45fSAndroid Build Coastguard Worker     std::string name;
175*38e8c45fSAndroid Build Coastguard Worker 
176*38e8c45fSAndroid Build Coastguard Worker     // Open file descriptor for the file.
177*38e8c45fSAndroid Build Coastguard Worker     android::base::unique_fd fd;
178*38e8c45fSAndroid Build Coastguard Worker 
179*38e8c45fSAndroid Build Coastguard Worker     // Modification time of the file.
180*38e8c45fSAndroid Build Coastguard Worker     time_t mtime;
181*38e8c45fSAndroid Build Coastguard Worker };
182*38e8c45fSAndroid Build Coastguard Worker 
183*38e8c45fSAndroid Build Coastguard Worker /*
184*38e8c45fSAndroid Build Coastguard Worker  * Main class driving a bugreport generation.
185*38e8c45fSAndroid Build Coastguard Worker  *
186*38e8c45fSAndroid Build Coastguard Worker  * Currently, it only contains variables that are accessed externally, but gradually the functions
187*38e8c45fSAndroid Build Coastguard Worker  * that are spread accross utils.cpp and dumpstate.cpp will be moved to it.
188*38e8c45fSAndroid Build Coastguard Worker  */
189*38e8c45fSAndroid Build Coastguard Worker class Dumpstate {
190*38e8c45fSAndroid Build Coastguard Worker     friend class android::os::dumpstate::DumpstateTest;
191*38e8c45fSAndroid Build Coastguard Worker     friend class android::os::dumpstate::ZippedBugReportStreamTest;
192*38e8c45fSAndroid Build Coastguard Worker 
193*38e8c45fSAndroid Build Coastguard Worker   public:
194*38e8c45fSAndroid Build Coastguard Worker     enum RunStatus { OK, HELP, INVALID_INPUT, ERROR, USER_CONSENT_DENIED, USER_CONSENT_TIMED_OUT };
195*38e8c45fSAndroid Build Coastguard Worker 
196*38e8c45fSAndroid Build Coastguard Worker     // The mode under which the bugreport should be run. Each mode encapsulates a few options.
197*38e8c45fSAndroid Build Coastguard Worker     enum BugreportMode {
198*38e8c45fSAndroid Build Coastguard Worker         BUGREPORT_FULL = android::os::IDumpstate::BUGREPORT_MODE_FULL,
199*38e8c45fSAndroid Build Coastguard Worker         BUGREPORT_INTERACTIVE = android::os::IDumpstate::BUGREPORT_MODE_INTERACTIVE,
200*38e8c45fSAndroid Build Coastguard Worker         BUGREPORT_REMOTE = android::os::IDumpstate::BUGREPORT_MODE_REMOTE,
201*38e8c45fSAndroid Build Coastguard Worker         BUGREPORT_WEAR = android::os::IDumpstate::BUGREPORT_MODE_WEAR,
202*38e8c45fSAndroid Build Coastguard Worker         BUGREPORT_TELEPHONY = android::os::IDumpstate::BUGREPORT_MODE_TELEPHONY,
203*38e8c45fSAndroid Build Coastguard Worker         BUGREPORT_WIFI = android::os::IDumpstate::BUGREPORT_MODE_WIFI,
204*38e8c45fSAndroid Build Coastguard Worker         BUGREPORT_ONBOARDING = android::os::IDumpstate::BUGREPORT_MODE_ONBOARDING,
205*38e8c45fSAndroid Build Coastguard Worker         BUGREPORT_DEFAULT = android::os::IDumpstate::BUGREPORT_MODE_DEFAULT
206*38e8c45fSAndroid Build Coastguard Worker     };
207*38e8c45fSAndroid Build Coastguard Worker 
208*38e8c45fSAndroid Build Coastguard Worker     // The flags used to customize bugreport requests.
209*38e8c45fSAndroid Build Coastguard Worker     enum BugreportFlag {
210*38e8c45fSAndroid Build Coastguard Worker         BUGREPORT_USE_PREDUMPED_UI_DATA =
211*38e8c45fSAndroid Build Coastguard Worker           android::os::IDumpstate::BUGREPORT_FLAG_USE_PREDUMPED_UI_DATA,
212*38e8c45fSAndroid Build Coastguard Worker         BUGREPORT_FLAG_DEFER_CONSENT =
213*38e8c45fSAndroid Build Coastguard Worker           android::os::IDumpstate::BUGREPORT_FLAG_DEFER_CONSENT,
214*38e8c45fSAndroid Build Coastguard Worker           BUGREPORT_FLAG_KEEP_BUGREPORT_ON_RETRIEVAL =
215*38e8c45fSAndroid Build Coastguard Worker                     android::os::IDumpstate::BUGREPORT_FLAG_KEEP_BUGREPORT_ON_RETRIEVAL
216*38e8c45fSAndroid Build Coastguard Worker     };
217*38e8c45fSAndroid Build Coastguard Worker 
218*38e8c45fSAndroid Build Coastguard Worker     static android::os::dumpstate::CommandOptions DEFAULT_DUMPSYS;
219*38e8c45fSAndroid Build Coastguard Worker 
220*38e8c45fSAndroid Build Coastguard Worker     static Dumpstate& GetInstance();
221*38e8c45fSAndroid Build Coastguard Worker 
222*38e8c45fSAndroid Build Coastguard Worker     /* Initialize dumpstate fields before starting bugreport generation */
223*38e8c45fSAndroid Build Coastguard Worker     void Initialize();
224*38e8c45fSAndroid Build Coastguard Worker 
225*38e8c45fSAndroid Build Coastguard Worker     /*
226*38e8c45fSAndroid Build Coastguard Worker      * Forks a command, waits for it to finish, and returns its status.
227*38e8c45fSAndroid Build Coastguard Worker      *
228*38e8c45fSAndroid Build Coastguard Worker      * |title| description of the command printed on `stdout` (or empty to skip
229*38e8c45fSAndroid Build Coastguard Worker      * description).
230*38e8c45fSAndroid Build Coastguard Worker      * |full_command| array containing the command (first entry) and its arguments.
231*38e8c45fSAndroid Build Coastguard Worker      * Must contain at least one element.
232*38e8c45fSAndroid Build Coastguard Worker      * |options| optional argument defining the command's behavior.
233*38e8c45fSAndroid Build Coastguard Worker      * |out_fd| A fd to support the DumpPool to output results to a temporary
234*38e8c45fSAndroid Build Coastguard Worker      * file. Using STDOUT_FILENO if it's not running in the parallel task.
235*38e8c45fSAndroid Build Coastguard Worker      */
236*38e8c45fSAndroid Build Coastguard Worker     int RunCommand(const std::string& title, const std::vector<std::string>& fullCommand,
237*38e8c45fSAndroid Build Coastguard Worker                    const android::os::dumpstate::CommandOptions& options =
238*38e8c45fSAndroid Build Coastguard Worker                        android::os::dumpstate::CommandOptions::DEFAULT,
239*38e8c45fSAndroid Build Coastguard Worker                    bool verbose_duration = false, int out_fd = STDOUT_FILENO);
240*38e8c45fSAndroid Build Coastguard Worker 
241*38e8c45fSAndroid Build Coastguard Worker     /*
242*38e8c45fSAndroid Build Coastguard Worker      * Runs `dumpsys` with the given arguments, automatically setting its timeout
243*38e8c45fSAndroid Build Coastguard Worker      * (`-T` argument)
244*38e8c45fSAndroid Build Coastguard Worker      * according to the command options.
245*38e8c45fSAndroid Build Coastguard Worker      *
246*38e8c45fSAndroid Build Coastguard Worker      * |title| description of the command printed on `stdout` (or empty to skip
247*38e8c45fSAndroid Build Coastguard Worker      * description).
248*38e8c45fSAndroid Build Coastguard Worker      * |dumpsys_args| `dumpsys` arguments (except `-t`).
249*38e8c45fSAndroid Build Coastguard Worker      * |options| optional argument defining the command's behavior.
250*38e8c45fSAndroid Build Coastguard Worker      * |dumpsys_timeout| when > 0, defines the value passed to `dumpsys -T` (otherwise it uses the
251*38e8c45fSAndroid Build Coastguard Worker      * timeout from `options`)
252*38e8c45fSAndroid Build Coastguard Worker      * |out_fd| A fd to support the DumpPool to output results to a temporary
253*38e8c45fSAndroid Build Coastguard Worker      * file. Using STDOUT_FILENO if it's not running in the parallel task.
254*38e8c45fSAndroid Build Coastguard Worker      */
255*38e8c45fSAndroid Build Coastguard Worker     void RunDumpsys(const std::string& title, const std::vector<std::string>& dumpsys_args,
256*38e8c45fSAndroid Build Coastguard Worker                     const android::os::dumpstate::CommandOptions& options = DEFAULT_DUMPSYS,
257*38e8c45fSAndroid Build Coastguard Worker                     long dumpsys_timeout_ms = 0, int out_fd = STDOUT_FILENO);
258*38e8c45fSAndroid Build Coastguard Worker 
259*38e8c45fSAndroid Build Coastguard Worker     /*
260*38e8c45fSAndroid Build Coastguard Worker      * Prints the contents of a file.
261*38e8c45fSAndroid Build Coastguard Worker      *
262*38e8c45fSAndroid Build Coastguard Worker      * |title| description of the command printed on `stdout` (or empty to skip
263*38e8c45fSAndroid Build Coastguard Worker      * description).
264*38e8c45fSAndroid Build Coastguard Worker      * |path| location of the file to be dumped.
265*38e8c45fSAndroid Build Coastguard Worker      */
266*38e8c45fSAndroid Build Coastguard Worker     int DumpFile(const std::string& title, const std::string& path);
267*38e8c45fSAndroid Build Coastguard Worker 
268*38e8c45fSAndroid Build Coastguard Worker     /*
269*38e8c45fSAndroid Build Coastguard Worker      * Adds a new entry to the existing zip file.
270*38e8c45fSAndroid Build Coastguard Worker      * */
271*38e8c45fSAndroid Build Coastguard Worker     bool AddZipEntry(const std::string& entry_name, const std::string& entry_path);
272*38e8c45fSAndroid Build Coastguard Worker 
273*38e8c45fSAndroid Build Coastguard Worker     /*
274*38e8c45fSAndroid Build Coastguard Worker      * Adds a new entry to the existing zip file.
275*38e8c45fSAndroid Build Coastguard Worker      *
276*38e8c45fSAndroid Build Coastguard Worker      * |entry_name| destination path of the new entry.
277*38e8c45fSAndroid Build Coastguard Worker      * |fd| file descriptor to read from.
278*38e8c45fSAndroid Build Coastguard Worker      * |timeout| timeout to terminate the read if not completed. Set
279*38e8c45fSAndroid Build Coastguard Worker      * value of 0s (default) to disable timeout.
280*38e8c45fSAndroid Build Coastguard Worker      */
281*38e8c45fSAndroid Build Coastguard Worker     android::status_t AddZipEntryFromFd(const std::string& entry_name, int fd,
282*38e8c45fSAndroid Build Coastguard Worker                                         std::chrono::milliseconds timeout);
283*38e8c45fSAndroid Build Coastguard Worker 
284*38e8c45fSAndroid Build Coastguard Worker     /*
285*38e8c45fSAndroid Build Coastguard Worker      * Adds a text entry to the existing zip file.
286*38e8c45fSAndroid Build Coastguard Worker      */
287*38e8c45fSAndroid Build Coastguard Worker     bool AddTextZipEntry(const std::string& entry_name, const std::string& content);
288*38e8c45fSAndroid Build Coastguard Worker 
289*38e8c45fSAndroid Build Coastguard Worker     /*
290*38e8c45fSAndroid Build Coastguard Worker      * Adds all files from a directory to the zipped bugreport file.
291*38e8c45fSAndroid Build Coastguard Worker      */
292*38e8c45fSAndroid Build Coastguard Worker     void AddDir(const std::string& dir, bool recursive);
293*38e8c45fSAndroid Build Coastguard Worker 
294*38e8c45fSAndroid Build Coastguard Worker     /*
295*38e8c45fSAndroid Build Coastguard Worker      * Takes a screenshot and save it to the given `path`.
296*38e8c45fSAndroid Build Coastguard Worker      *
297*38e8c45fSAndroid Build Coastguard Worker      * If `path` is empty, uses a standard path based on the bugreport name.
298*38e8c45fSAndroid Build Coastguard Worker      */
299*38e8c45fSAndroid Build Coastguard Worker     void TakeScreenshot(const std::string& path = "");
300*38e8c45fSAndroid Build Coastguard Worker 
301*38e8c45fSAndroid Build Coastguard Worker     /////////////////////////////////////////////////////////////////////
302*38e8c45fSAndroid Build Coastguard Worker     // TODO: members below should be private once refactor is finished //
303*38e8c45fSAndroid Build Coastguard Worker     /////////////////////////////////////////////////////////////////////
304*38e8c45fSAndroid Build Coastguard Worker 
305*38e8c45fSAndroid Build Coastguard Worker     // TODO: temporary method until Dumpstate object is properly set
306*38e8c45fSAndroid Build Coastguard Worker     void SetProgress(std::unique_ptr<Progress> progress);
307*38e8c45fSAndroid Build Coastguard Worker 
308*38e8c45fSAndroid Build Coastguard Worker     // Dumps Dalvik and native stack traces, sets the trace file location to path
309*38e8c45fSAndroid Build Coastguard Worker     // if it succeeded.
310*38e8c45fSAndroid Build Coastguard Worker     // Note that it returns early if user consent is denied with status USER_CONSENT_DENIED.
311*38e8c45fSAndroid Build Coastguard Worker     // Returns OK in all other cases.
312*38e8c45fSAndroid Build Coastguard Worker     RunStatus DumpTraces(const char** path);
313*38e8c45fSAndroid Build Coastguard Worker 
314*38e8c45fSAndroid Build Coastguard Worker     /*
315*38e8c45fSAndroid Build Coastguard Worker      * |out_fd| A fd to support the DumpPool to output results to a temporary file.
316*38e8c45fSAndroid Build Coastguard Worker      * Dumpstate can pick up later and output to the bugreport. Using STDOUT_FILENO
317*38e8c45fSAndroid Build Coastguard Worker      * if it's not running in the parallel task.
318*38e8c45fSAndroid Build Coastguard Worker      */
319*38e8c45fSAndroid Build Coastguard Worker     void DumpstateBoard(int out_fd = STDOUT_FILENO);
320*38e8c45fSAndroid Build Coastguard Worker 
321*38e8c45fSAndroid Build Coastguard Worker     /*
322*38e8c45fSAndroid Build Coastguard Worker      * Updates the overall progress of the bugreport generation by the given weight increment.
323*38e8c45fSAndroid Build Coastguard Worker      */
324*38e8c45fSAndroid Build Coastguard Worker     void UpdateProgress(int32_t delta);
325*38e8c45fSAndroid Build Coastguard Worker 
326*38e8c45fSAndroid Build Coastguard Worker     /* Prints the dumpstate header on `stdout`. */
327*38e8c45fSAndroid Build Coastguard Worker     void PrintHeader() const;
328*38e8c45fSAndroid Build Coastguard Worker 
329*38e8c45fSAndroid Build Coastguard Worker     /*
330*38e8c45fSAndroid Build Coastguard Worker      * Adds the temporary report to the existing .zip file, closes the .zip file, and removes the
331*38e8c45fSAndroid Build Coastguard Worker      * temporary file.
332*38e8c45fSAndroid Build Coastguard Worker      */
333*38e8c45fSAndroid Build Coastguard Worker     bool FinishZipFile();
334*38e8c45fSAndroid Build Coastguard Worker 
335*38e8c45fSAndroid Build Coastguard Worker     /* Constructs a full path inside directory with file name formatted using the given suffix. */
336*38e8c45fSAndroid Build Coastguard Worker     std::string GetPath(const std::string& directory, const std::string& suffix) const;
337*38e8c45fSAndroid Build Coastguard Worker 
338*38e8c45fSAndroid Build Coastguard Worker     /* Constructs a full path inside bugreport_internal_dir_ with file name formatted using the
339*38e8c45fSAndroid Build Coastguard Worker      * given suffix. */
340*38e8c45fSAndroid Build Coastguard Worker     std::string GetPath(const std::string& suffix) const;
341*38e8c45fSAndroid Build Coastguard Worker 
342*38e8c45fSAndroid Build Coastguard Worker     /* Returns true if the current version supports priority dump feature. */
343*38e8c45fSAndroid Build Coastguard Worker     bool CurrentVersionSupportsPriorityDumps() const;
344*38e8c45fSAndroid Build Coastguard Worker 
345*38e8c45fSAndroid Build Coastguard Worker     struct DumpOptions;
346*38e8c45fSAndroid Build Coastguard Worker 
347*38e8c45fSAndroid Build Coastguard Worker     /**
348*38e8c45fSAndroid Build Coastguard Worker      * Pre-dump critical UI data, e.g. data stored in short ring buffers that might get lost
349*38e8c45fSAndroid Build Coastguard Worker      * by the time the actual bugreport is requested.
350*38e8c45fSAndroid Build Coastguard Worker      */
351*38e8c45fSAndroid Build Coastguard Worker     void PreDumpUiData();
352*38e8c45fSAndroid Build Coastguard Worker 
353*38e8c45fSAndroid Build Coastguard Worker     /*
354*38e8c45fSAndroid Build Coastguard Worker      * Main entry point for running a complete bugreport.
355*38e8c45fSAndroid Build Coastguard Worker      *
356*38e8c45fSAndroid Build Coastguard Worker      * Initialize() dumpstate before calling this method.
357*38e8c45fSAndroid Build Coastguard Worker      *
358*38e8c45fSAndroid Build Coastguard Worker      */
359*38e8c45fSAndroid Build Coastguard Worker     RunStatus Run(int32_t calling_uid, const std::string& calling_package);
360*38e8c45fSAndroid Build Coastguard Worker 
361*38e8c45fSAndroid Build Coastguard Worker     /*
362*38e8c45fSAndroid Build Coastguard Worker      * Entry point for retrieving a previous-generated bugreport.
363*38e8c45fSAndroid Build Coastguard Worker      *
364*38e8c45fSAndroid Build Coastguard Worker      * Initialize() dumpstate before calling this method.
365*38e8c45fSAndroid Build Coastguard Worker      */
366*38e8c45fSAndroid Build Coastguard Worker     RunStatus Retrieve(int32_t calling_uid, const std::string& calling_package,
367*38e8c45fSAndroid Build Coastguard Worker                         const bool keep_bugreport_on_retrieval, const bool skip_user_consent);
368*38e8c45fSAndroid Build Coastguard Worker 
369*38e8c45fSAndroid Build Coastguard Worker 
370*38e8c45fSAndroid Build Coastguard Worker 
371*38e8c45fSAndroid Build Coastguard Worker     RunStatus ParseCommandlineAndRun(int argc, char* argv[]);
372*38e8c45fSAndroid Build Coastguard Worker 
373*38e8c45fSAndroid Build Coastguard Worker     /* Deletes in-progress files */
374*38e8c45fSAndroid Build Coastguard Worker     void Cancel();
375*38e8c45fSAndroid Build Coastguard Worker 
376*38e8c45fSAndroid Build Coastguard Worker     /* Sets runtime options. */
377*38e8c45fSAndroid Build Coastguard Worker     void SetOptions(std::unique_ptr<DumpOptions> options);
378*38e8c45fSAndroid Build Coastguard Worker 
379*38e8c45fSAndroid Build Coastguard Worker     /*
380*38e8c45fSAndroid Build Coastguard Worker      * Returns true if user consent is necessary and has been denied.
381*38e8c45fSAndroid Build Coastguard Worker      * Consent is only necessary if the caller has asked to copy over the bugreport to a file they
382*38e8c45fSAndroid Build Coastguard Worker      * provided.
383*38e8c45fSAndroid Build Coastguard Worker      */
384*38e8c45fSAndroid Build Coastguard Worker     bool IsUserConsentDenied() const;
385*38e8c45fSAndroid Build Coastguard Worker 
386*38e8c45fSAndroid Build Coastguard Worker     /*
387*38e8c45fSAndroid Build Coastguard Worker      * Returns true if dumpstate is called by bugreporting API
388*38e8c45fSAndroid Build Coastguard Worker      */
389*38e8c45fSAndroid Build Coastguard Worker     bool CalledByApi() const;
390*38e8c45fSAndroid Build Coastguard Worker 
391*38e8c45fSAndroid Build Coastguard Worker     /*
392*38e8c45fSAndroid Build Coastguard Worker      * Enqueues a task to the dumpstate's TaskQueue if the parallel run is enabled,
393*38e8c45fSAndroid Build Coastguard Worker      * otherwise invokes it immediately. The task adds file at path entry_path
394*38e8c45fSAndroid Build Coastguard Worker      * as a zip file entry with name entry_name. Unlinks entry_path when done.
395*38e8c45fSAndroid Build Coastguard Worker      *
396*38e8c45fSAndroid Build Coastguard Worker      * All enqueued tasks will be executed in the dumpstate's FinishZipFile method
397*38e8c45fSAndroid Build Coastguard Worker      * before the zip file is finished. Tasks will be cancelled in dumpstate's
398*38e8c45fSAndroid Build Coastguard Worker      * ShutdownDumpPool method if they have never been called.
399*38e8c45fSAndroid Build Coastguard Worker      */
400*38e8c45fSAndroid Build Coastguard Worker     void EnqueueAddZipEntryAndCleanupIfNeeded(const std::string& entry_name,
401*38e8c45fSAndroid Build Coastguard Worker             const std::string& entry_path);
402*38e8c45fSAndroid Build Coastguard Worker 
403*38e8c45fSAndroid Build Coastguard Worker     /*
404*38e8c45fSAndroid Build Coastguard Worker      * Structure to hold options that determine the behavior of dumpstate.
405*38e8c45fSAndroid Build Coastguard Worker      */
406*38e8c45fSAndroid Build Coastguard Worker     struct DumpOptions {
407*38e8c45fSAndroid Build Coastguard Worker         bool do_vibrate = true;
408*38e8c45fSAndroid Build Coastguard Worker         // Writes bugreport zipped file to a socket.
409*38e8c45fSAndroid Build Coastguard Worker         bool stream_to_socket = false;
410*38e8c45fSAndroid Build Coastguard Worker         // Writes generation progress updates to a socket.
411*38e8c45fSAndroid Build Coastguard Worker         bool progress_updates_to_socket = false;
412*38e8c45fSAndroid Build Coastguard Worker         bool do_screenshot = false;
413*38e8c45fSAndroid Build Coastguard Worker         bool is_screenshot_copied = false;
414*38e8c45fSAndroid Build Coastguard Worker         bool is_consent_deferred = false;
415*38e8c45fSAndroid Build Coastguard Worker         bool skip_user_consent = false;
416*38e8c45fSAndroid Build Coastguard Worker         bool is_remote_mode = false;
417*38e8c45fSAndroid Build Coastguard Worker         bool show_header_only = false;
418*38e8c45fSAndroid Build Coastguard Worker         bool telephony_only = false;
419*38e8c45fSAndroid Build Coastguard Worker         bool wifi_only = false;
420*38e8c45fSAndroid Build Coastguard Worker         bool onboarding_only = false;
421*38e8c45fSAndroid Build Coastguard Worker         // Trimmed-down version of dumpstate to only include whitelisted logs.
422*38e8c45fSAndroid Build Coastguard Worker         bool limited_only = false;
423*38e8c45fSAndroid Build Coastguard Worker         // Whether progress updates should be published.
424*38e8c45fSAndroid Build Coastguard Worker         bool do_progress_updates = false;
425*38e8c45fSAndroid Build Coastguard Worker         // this is used to derive dumpstate HAL bug report mode
426*38e8c45fSAndroid Build Coastguard Worker         // TODO(b/148168577) get rid of the AIDL values, replace them with the HAL values instead.
427*38e8c45fSAndroid Build Coastguard Worker         // The HAL is actually an API surface that can be validated, while the AIDL is not (@hide).
428*38e8c45fSAndroid Build Coastguard Worker         BugreportMode bugreport_mode = Dumpstate::BugreportMode::BUGREPORT_DEFAULT;
429*38e8c45fSAndroid Build Coastguard Worker         // Will use data collected through a previous call to PreDumpUiData().
430*38e8c45fSAndroid Build Coastguard Worker         bool use_predumped_ui_data;
431*38e8c45fSAndroid Build Coastguard Worker         // File descriptor to output zip file. Takes precedence over out_dir.
432*38e8c45fSAndroid Build Coastguard Worker         android::base::unique_fd bugreport_fd;
433*38e8c45fSAndroid Build Coastguard Worker         // File descriptor to screenshot file.
434*38e8c45fSAndroid Build Coastguard Worker         android::base::unique_fd screenshot_fd;
435*38e8c45fSAndroid Build Coastguard Worker         // Custom output directory.
436*38e8c45fSAndroid Build Coastguard Worker         std::string out_dir;
437*38e8c45fSAndroid Build Coastguard Worker         // Bugreport mode of the bugreport as a string
438*38e8c45fSAndroid Build Coastguard Worker         std::string bugreport_mode_string;
439*38e8c45fSAndroid Build Coastguard Worker         // Command-line arguments as string
440*38e8c45fSAndroid Build Coastguard Worker         std::string args;
441*38e8c45fSAndroid Build Coastguard Worker         // Notification title and description
442*38e8c45fSAndroid Build Coastguard Worker         std::string notification_title;
443*38e8c45fSAndroid Build Coastguard Worker         std::string notification_description;
444*38e8c45fSAndroid Build Coastguard Worker 
445*38e8c45fSAndroid Build Coastguard Worker         /* Initializes options from commandline arguments. */
446*38e8c45fSAndroid Build Coastguard Worker         RunStatus Initialize(int argc, char* argv[]);
447*38e8c45fSAndroid Build Coastguard Worker 
448*38e8c45fSAndroid Build Coastguard Worker         /* Initializes options from the requested mode. */
449*38e8c45fSAndroid Build Coastguard Worker         void Initialize(BugreportMode bugreport_mode, int bugreport_flags,
450*38e8c45fSAndroid Build Coastguard Worker                         const android::base::unique_fd& bugreport_fd,
451*38e8c45fSAndroid Build Coastguard Worker                         const android::base::unique_fd& screenshot_fd,
452*38e8c45fSAndroid Build Coastguard Worker                         bool is_screenshot_requested,
453*38e8c45fSAndroid Build Coastguard Worker                         bool skip_user_consent);
454*38e8c45fSAndroid Build Coastguard Worker 
455*38e8c45fSAndroid Build Coastguard Worker         /* Returns true if the options set so far are consistent. */
456*38e8c45fSAndroid Build Coastguard Worker         bool ValidateOptions() const;
457*38e8c45fSAndroid Build Coastguard Worker 
458*38e8c45fSAndroid Build Coastguard Worker         /* Returns if options specified require writing to custom file location */
OutputToCustomFileDumpOptions459*38e8c45fSAndroid Build Coastguard Worker         bool OutputToCustomFile() {
460*38e8c45fSAndroid Build Coastguard Worker             // Custom location is only honored in limited mode.
461*38e8c45fSAndroid Build Coastguard Worker             return limited_only && !out_dir.empty() && bugreport_fd.get() == -1;
462*38e8c45fSAndroid Build Coastguard Worker         }
463*38e8c45fSAndroid Build Coastguard Worker     };
464*38e8c45fSAndroid Build Coastguard Worker 
465*38e8c45fSAndroid Build Coastguard Worker     // TODO: initialize fields on constructor
466*38e8c45fSAndroid Build Coastguard Worker     // dumpstate id - unique after each device reboot.
467*38e8c45fSAndroid Build Coastguard Worker     uint32_t id_;
468*38e8c45fSAndroid Build Coastguard Worker 
469*38e8c45fSAndroid Build Coastguard Worker     // dumpstate pid
470*38e8c45fSAndroid Build Coastguard Worker     pid_t pid_;
471*38e8c45fSAndroid Build Coastguard Worker 
472*38e8c45fSAndroid Build Coastguard Worker     // Runtime options.
473*38e8c45fSAndroid Build Coastguard Worker     std::unique_ptr<DumpOptions> options_;
474*38e8c45fSAndroid Build Coastguard Worker 
475*38e8c45fSAndroid Build Coastguard Worker     // Last progress that was sent to the listener [0-100].
476*38e8c45fSAndroid Build Coastguard Worker     int last_reported_percent_progress_ = 0;
477*38e8c45fSAndroid Build Coastguard Worker 
478*38e8c45fSAndroid Build Coastguard Worker     // Whether it should take an screenshot earlier in the process.
479*38e8c45fSAndroid Build Coastguard Worker     bool do_early_screenshot_ = false;
480*38e8c45fSAndroid Build Coastguard Worker 
481*38e8c45fSAndroid Build Coastguard Worker     std::unique_ptr<Progress> progress_;
482*38e8c45fSAndroid Build Coastguard Worker 
483*38e8c45fSAndroid Build Coastguard Worker     // When set, defines a socket file-descriptor use to report progress to bugreportz
484*38e8c45fSAndroid Build Coastguard Worker     // or to stream the zipped file to.
485*38e8c45fSAndroid Build Coastguard Worker     int control_socket_fd_ = -1;
486*38e8c45fSAndroid Build Coastguard Worker 
487*38e8c45fSAndroid Build Coastguard Worker     // Bugreport format version;
488*38e8c45fSAndroid Build Coastguard Worker     std::string version_ = VERSION_CURRENT;
489*38e8c45fSAndroid Build Coastguard Worker 
490*38e8c45fSAndroid Build Coastguard Worker     time_t now_;
491*38e8c45fSAndroid Build Coastguard Worker 
492*38e8c45fSAndroid Build Coastguard Worker     // Base name (without suffix or extensions) of the bugreport files, typically
493*38e8c45fSAndroid Build Coastguard Worker     // `bugreport-BUILD_ID`.
494*38e8c45fSAndroid Build Coastguard Worker     std::string base_name_;
495*38e8c45fSAndroid Build Coastguard Worker 
496*38e8c45fSAndroid Build Coastguard Worker     // Name is the suffix part of the bugreport files - it's typically the date,
497*38e8c45fSAndroid Build Coastguard Worker     // but it could be changed by the user..
498*38e8c45fSAndroid Build Coastguard Worker     std::string name_;
499*38e8c45fSAndroid Build Coastguard Worker 
500*38e8c45fSAndroid Build Coastguard Worker     std::string bugreport_internal_dir_ = DUMPSTATE_DIRECTORY;
501*38e8c45fSAndroid Build Coastguard Worker 
502*38e8c45fSAndroid Build Coastguard Worker     // Full path of the temporary file containing the bugreport, inside bugreport_internal_dir_.
503*38e8c45fSAndroid Build Coastguard Worker     // At the very end this file is pulled into the zip file.
504*38e8c45fSAndroid Build Coastguard Worker     std::string tmp_path_;
505*38e8c45fSAndroid Build Coastguard Worker 
506*38e8c45fSAndroid Build Coastguard Worker     // Full path of the file containing the dumpstate logs, inside bugreport_internal_dir_.
507*38e8c45fSAndroid Build Coastguard Worker     // This is useful for debugging.
508*38e8c45fSAndroid Build Coastguard Worker     std::string log_path_;
509*38e8c45fSAndroid Build Coastguard Worker 
510*38e8c45fSAndroid Build Coastguard Worker     // Full path of the bugreport zip file inside bugreport_internal_dir_.
511*38e8c45fSAndroid Build Coastguard Worker     std::string path_;
512*38e8c45fSAndroid Build Coastguard Worker 
513*38e8c45fSAndroid Build Coastguard Worker     // Full path of the file containing the screenshot (when requested).
514*38e8c45fSAndroid Build Coastguard Worker     std::string screenshot_path_;
515*38e8c45fSAndroid Build Coastguard Worker 
516*38e8c45fSAndroid Build Coastguard Worker     // Pointer to the zipped file.
517*38e8c45fSAndroid Build Coastguard Worker     std::unique_ptr<FILE, int (*)(FILE*)> zip_file{nullptr, fclose};
518*38e8c45fSAndroid Build Coastguard Worker 
519*38e8c45fSAndroid Build Coastguard Worker     // Pointer to the zip structure.
520*38e8c45fSAndroid Build Coastguard Worker     std::unique_ptr<ZipWriter> zip_writer_;
521*38e8c45fSAndroid Build Coastguard Worker 
522*38e8c45fSAndroid Build Coastguard Worker     // Binder object listening to progress.
523*38e8c45fSAndroid Build Coastguard Worker     android::sp<android::os::IDumpstateListener> listener_;
524*38e8c45fSAndroid Build Coastguard Worker 
525*38e8c45fSAndroid Build Coastguard Worker     // List of open tombstone dump files.
526*38e8c45fSAndroid Build Coastguard Worker     std::vector<DumpData> tombstone_data_;
527*38e8c45fSAndroid Build Coastguard Worker 
528*38e8c45fSAndroid Build Coastguard Worker     // List of open ANR dump files.
529*38e8c45fSAndroid Build Coastguard Worker     std::vector<DumpData> anr_data_;
530*38e8c45fSAndroid Build Coastguard Worker 
531*38e8c45fSAndroid Build Coastguard Worker     // List of open Java traces files in the anr directory.
532*38e8c45fSAndroid Build Coastguard Worker     std::vector<DumpData> anr_trace_data_;
533*38e8c45fSAndroid Build Coastguard Worker 
534*38e8c45fSAndroid Build Coastguard Worker     // List of open shutdown checkpoint files.
535*38e8c45fSAndroid Build Coastguard Worker     std::vector<DumpData> shutdown_checkpoints_;
536*38e8c45fSAndroid Build Coastguard Worker 
537*38e8c45fSAndroid Build Coastguard Worker     // A thread pool to execute dump tasks simultaneously if the parallel run is enabled.
538*38e8c45fSAndroid Build Coastguard Worker     std::unique_ptr<android::os::dumpstate::DumpPool> dump_pool_;
539*38e8c45fSAndroid Build Coastguard Worker 
540*38e8c45fSAndroid Build Coastguard Worker     // A task queue to collect adding zip entry tasks inside dump tasks if the
541*38e8c45fSAndroid Build Coastguard Worker     // parallel run is enabled.
542*38e8c45fSAndroid Build Coastguard Worker     std::unique_ptr<android::os::dumpstate::TaskQueue> zip_entry_tasks_;
543*38e8c45fSAndroid Build Coastguard Worker 
544*38e8c45fSAndroid Build Coastguard Worker     // A callback to IncidentCompanion service, which checks user consent for sharing the
545*38e8c45fSAndroid Build Coastguard Worker     // bugreport with the calling app. If the user has not responded yet to the dialog it will
546*38e8c45fSAndroid Build Coastguard Worker     // be neither confirmed nor denied.
547*38e8c45fSAndroid Build Coastguard Worker     class ConsentCallback : public android::os::BnIncidentAuthListener {
548*38e8c45fSAndroid Build Coastguard Worker       public:
549*38e8c45fSAndroid Build Coastguard Worker         ConsentCallback();
550*38e8c45fSAndroid Build Coastguard Worker         android::binder::Status onReportApproved() override;
551*38e8c45fSAndroid Build Coastguard Worker         android::binder::Status onReportDenied() override;
552*38e8c45fSAndroid Build Coastguard Worker 
553*38e8c45fSAndroid Build Coastguard Worker         enum ConsentResult { APPROVED, DENIED, UNAVAILABLE };
554*38e8c45fSAndroid Build Coastguard Worker 
555*38e8c45fSAndroid Build Coastguard Worker         ConsentResult getResult();
556*38e8c45fSAndroid Build Coastguard Worker 
557*38e8c45fSAndroid Build Coastguard Worker         // Returns the time since creating this listener
558*38e8c45fSAndroid Build Coastguard Worker         uint64_t getElapsedTimeMs() const;
559*38e8c45fSAndroid Build Coastguard Worker 
560*38e8c45fSAndroid Build Coastguard Worker       private:
561*38e8c45fSAndroid Build Coastguard Worker         ConsentResult result_;
562*38e8c45fSAndroid Build Coastguard Worker         uint64_t start_time_;
563*38e8c45fSAndroid Build Coastguard Worker         std::mutex lock_;
564*38e8c45fSAndroid Build Coastguard Worker     };
565*38e8c45fSAndroid Build Coastguard Worker 
566*38e8c45fSAndroid Build Coastguard Worker   private:
567*38e8c45fSAndroid Build Coastguard Worker     RunStatus RunInternal(int32_t calling_uid, const std::string& calling_package);
568*38e8c45fSAndroid Build Coastguard Worker     RunStatus RetrieveInternal(int32_t calling_uid, const std::string& calling_package,
569*38e8c45fSAndroid Build Coastguard Worker                                 const bool keep_bugreport_on_retrieval,
570*38e8c45fSAndroid Build Coastguard Worker                                 const bool skip_user_consent);
571*38e8c45fSAndroid Build Coastguard Worker 
572*38e8c45fSAndroid Build Coastguard Worker     RunStatus DumpstateDefaultAfterCritical();
573*38e8c45fSAndroid Build Coastguard Worker     RunStatus dumpstate();
574*38e8c45fSAndroid Build Coastguard Worker 
575*38e8c45fSAndroid Build Coastguard Worker     void MaybeTakeEarlyScreenshot();
576*38e8c45fSAndroid Build Coastguard Worker     std::future<std::string> MaybeSnapshotSystemTraceAsync();
577*38e8c45fSAndroid Build Coastguard Worker     void MaybeWaitForSnapshotSystemTrace(std::future<std::string> task);
578*38e8c45fSAndroid Build Coastguard Worker     void MaybeSnapshotUiTraces();
579*38e8c45fSAndroid Build Coastguard Worker     void MaybeAddUiTracesToZip();
580*38e8c45fSAndroid Build Coastguard Worker 
581*38e8c45fSAndroid Build Coastguard Worker     void onUiIntensiveBugreportDumpsFinished(int32_t calling_uid);
582*38e8c45fSAndroid Build Coastguard Worker 
583*38e8c45fSAndroid Build Coastguard Worker     void MaybeCheckUserConsent(int32_t calling_uid, const std::string& calling_package);
584*38e8c45fSAndroid Build Coastguard Worker 
585*38e8c45fSAndroid Build Coastguard Worker     // Removes the in progress files output files (tmp file, zip/txt file, screenshot),
586*38e8c45fSAndroid Build Coastguard Worker     // but leaves the log file alone.
587*38e8c45fSAndroid Build Coastguard Worker     void CleanupTmpFiles();
588*38e8c45fSAndroid Build Coastguard Worker 
589*38e8c45fSAndroid Build Coastguard Worker     // Create the thread pool to enable the parallel run function.
590*38e8c45fSAndroid Build Coastguard Worker     void EnableParallelRunIfNeeded();
591*38e8c45fSAndroid Build Coastguard Worker     void ShutdownDumpPool();
592*38e8c45fSAndroid Build Coastguard Worker 
593*38e8c45fSAndroid Build Coastguard Worker     RunStatus HandleUserConsentDenied();
594*38e8c45fSAndroid Build Coastguard Worker 
595*38e8c45fSAndroid Build Coastguard Worker     void HandleRunStatus(RunStatus status);
596*38e8c45fSAndroid Build Coastguard Worker 
597*38e8c45fSAndroid Build Coastguard Worker     // Copies bugreport artifacts over to the caller's directories provided there is user consent or
598*38e8c45fSAndroid Build Coastguard Worker     // called by Shell.
599*38e8c45fSAndroid Build Coastguard Worker     RunStatus CopyBugreportIfUserConsented(int32_t calling_uid);
600*38e8c45fSAndroid Build Coastguard Worker 
601*38e8c45fSAndroid Build Coastguard Worker     std::function<int(const char *)> open_socket_fn_;
602*38e8c45fSAndroid Build Coastguard Worker 
603*38e8c45fSAndroid Build Coastguard Worker     // Used by GetInstance() only.
604*38e8c45fSAndroid Build Coastguard Worker     explicit Dumpstate(const std::string& version = VERSION_CURRENT);
605*38e8c45fSAndroid Build Coastguard Worker 
606*38e8c45fSAndroid Build Coastguard Worker     android::sp<ConsentCallback> consent_callback_;
607*38e8c45fSAndroid Build Coastguard Worker 
608*38e8c45fSAndroid Build Coastguard Worker     std::recursive_mutex mutex_;
609*38e8c45fSAndroid Build Coastguard Worker 
610*38e8c45fSAndroid Build Coastguard Worker     DISALLOW_COPY_AND_ASSIGN(Dumpstate);
611*38e8c45fSAndroid Build Coastguard Worker };
612*38e8c45fSAndroid Build Coastguard Worker 
613*38e8c45fSAndroid Build Coastguard Worker // for_each_pid_func = void (*)(int, const char*);
614*38e8c45fSAndroid Build Coastguard Worker // for_each_tid_func = void (*)(int, int, const char*);
615*38e8c45fSAndroid Build Coastguard Worker 
616*38e8c45fSAndroid Build Coastguard Worker typedef void(for_each_pid_func)(int, const char*);
617*38e8c45fSAndroid Build Coastguard Worker typedef void(for_each_tid_func)(int, int, const char*);
618*38e8c45fSAndroid Build Coastguard Worker 
619*38e8c45fSAndroid Build Coastguard Worker /* saves the the contents of a file as a long */
620*38e8c45fSAndroid Build Coastguard Worker int read_file_as_long(const char *path, long int *output);
621*38e8c45fSAndroid Build Coastguard Worker 
622*38e8c45fSAndroid Build Coastguard Worker /* prints the contents of the fd
623*38e8c45fSAndroid Build Coastguard Worker  * fd must have been opened with the flag O_NONBLOCK.
624*38e8c45fSAndroid Build Coastguard Worker  */
625*38e8c45fSAndroid Build Coastguard Worker int dump_file_from_fd(const char *title, const char *path, int fd);
626*38e8c45fSAndroid Build Coastguard Worker 
627*38e8c45fSAndroid Build Coastguard Worker /* calls skip to gate calling dump_from_fd recursively
628*38e8c45fSAndroid Build Coastguard Worker  * in the specified directory. dump_from_fd defaults to
629*38e8c45fSAndroid Build Coastguard Worker  * dump_file_from_fd above when set to NULL. skip defaults
630*38e8c45fSAndroid Build Coastguard Worker  * to false when set to NULL. dump_from_fd will always be
631*38e8c45fSAndroid Build Coastguard Worker  * called with title NULL.
632*38e8c45fSAndroid Build Coastguard Worker  */
633*38e8c45fSAndroid Build Coastguard Worker int dump_files(const std::string& title, const char* dir, bool (*skip)(const char* path),
634*38e8c45fSAndroid Build Coastguard Worker                int (*dump_from_fd)(const char* title, const char* path, int fd));
635*38e8c45fSAndroid Build Coastguard Worker 
636*38e8c45fSAndroid Build Coastguard Worker /*
637*38e8c45fSAndroid Build Coastguard Worker  * Redirects 'redirect' to a file indicated by 'path', truncating it.
638*38e8c45fSAndroid Build Coastguard Worker  *
639*38e8c45fSAndroid Build Coastguard Worker  * Returns true if redirect succeeds.
640*38e8c45fSAndroid Build Coastguard Worker  */
641*38e8c45fSAndroid Build Coastguard Worker bool redirect_to_file(FILE* redirect, char* path);
642*38e8c45fSAndroid Build Coastguard Worker 
643*38e8c45fSAndroid Build Coastguard Worker /*
644*38e8c45fSAndroid Build Coastguard Worker  * Redirects 'redirect' to an existing file indicated by 'path', appending it.
645*38e8c45fSAndroid Build Coastguard Worker  *
646*38e8c45fSAndroid Build Coastguard Worker  * Returns true if redirect succeeds.
647*38e8c45fSAndroid Build Coastguard Worker  */
648*38e8c45fSAndroid Build Coastguard Worker bool redirect_to_existing_file(FILE* redirect, char* path);
649*38e8c45fSAndroid Build Coastguard Worker 
650*38e8c45fSAndroid Build Coastguard Worker /* create leading directories, if necessary */
651*38e8c45fSAndroid Build Coastguard Worker void create_parent_dirs(const char *path);
652*38e8c45fSAndroid Build Coastguard Worker 
653*38e8c45fSAndroid Build Coastguard Worker /* for each process in the system, run the specified function */
654*38e8c45fSAndroid Build Coastguard Worker void for_each_pid(for_each_pid_func func, const char *header);
655*38e8c45fSAndroid Build Coastguard Worker 
656*38e8c45fSAndroid Build Coastguard Worker /* for each thread in the system, run the specified function */
657*38e8c45fSAndroid Build Coastguard Worker void for_each_tid(for_each_tid_func func, const char *header);
658*38e8c45fSAndroid Build Coastguard Worker 
659*38e8c45fSAndroid Build Coastguard Worker /* Displays a blocked processes in-kernel wait channel */
660*38e8c45fSAndroid Build Coastguard Worker void show_wchan(int pid, int tid, const char *name);
661*38e8c45fSAndroid Build Coastguard Worker 
662*38e8c45fSAndroid Build Coastguard Worker /* Displays a processes times */
663*38e8c45fSAndroid Build Coastguard Worker void show_showtime(int pid, const char *name);
664*38e8c45fSAndroid Build Coastguard Worker 
665*38e8c45fSAndroid Build Coastguard Worker /* Gets the dmesg output for the kernel */
666*38e8c45fSAndroid Build Coastguard Worker void do_dmesg();
667*38e8c45fSAndroid Build Coastguard Worker 
668*38e8c45fSAndroid Build Coastguard Worker /* Prints the contents of all the routing tables, both IPv4 and IPv6. */
669*38e8c45fSAndroid Build Coastguard Worker void dump_route_tables();
670*38e8c45fSAndroid Build Coastguard Worker 
671*38e8c45fSAndroid Build Coastguard Worker /* Dump subdirectories of cgroupfs if the corresponding process is frozen */
672*38e8c45fSAndroid Build Coastguard Worker void dump_frozen_cgroupfs();
673*38e8c45fSAndroid Build Coastguard Worker 
674*38e8c45fSAndroid Build Coastguard Worker /* Play a sound via Stagefright */
675*38e8c45fSAndroid Build Coastguard Worker void play_sound(const char *path);
676*38e8c45fSAndroid Build Coastguard Worker 
677*38e8c45fSAndroid Build Coastguard Worker /* Checks if a given path is a directory. */
678*38e8c45fSAndroid Build Coastguard Worker bool is_dir(const char* pathname);
679*38e8c45fSAndroid Build Coastguard Worker 
680*38e8c45fSAndroid Build Coastguard Worker /** Gets the last modification time of a file, or default time if file is not found. */
681*38e8c45fSAndroid Build Coastguard Worker time_t get_mtime(int fd, time_t default_mtime);
682*38e8c45fSAndroid Build Coastguard Worker 
683*38e8c45fSAndroid Build Coastguard Worker /** Gets command-line arguments. */
684*38e8c45fSAndroid Build Coastguard Worker void format_args(int argc, const char *argv[], std::string *args);
685*38e8c45fSAndroid Build Coastguard Worker 
686*38e8c45fSAndroid Build Coastguard Worker /** Main entry point for dumpstate. */
687*38e8c45fSAndroid Build Coastguard Worker int run_main(int argc, char* argv[]);
688*38e8c45fSAndroid Build Coastguard Worker 
689*38e8c45fSAndroid Build Coastguard Worker #ifdef __cplusplus
690*38e8c45fSAndroid Build Coastguard Worker }
691*38e8c45fSAndroid Build Coastguard Worker #endif
692*38e8c45fSAndroid Build Coastguard Worker 
693*38e8c45fSAndroid Build Coastguard Worker #endif /* FRAMEWORK_NATIVE_CMD_DUMPSTATE_H_ */
694