blob: 929c55c3646b1b9ee2454037623d184cde3c82e9 [file] [log] [blame]
Felipe Leme343175a2016-08-02 18:57:37 -07001/*
2 * Copyright (C) 2016 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
17#ifndef FRAMEWORK_NATIVE_CMD_DUMPSYS_H_
18#define FRAMEWORK_NATIVE_CMD_DUMPSYS_H_
19
Vishnu Naire4f61742017-12-21 08:30:28 -080020#include <thread>
21
22#include <android-base/unique_fd.h>
Felipe Leme343175a2016-08-02 18:57:37 -070023#include <binder/IServiceManager.h>
24
25namespace android {
26
27class Dumpsys {
28 public:
Chih-Hung Hsiehd0937e62018-12-20 15:43:57 -080029 explicit Dumpsys(android::IServiceManager* sm) : sm_(sm) {
Felipe Leme343175a2016-08-02 18:57:37 -070030 }
Vishnu Naire4f61742017-12-21 08:30:28 -080031 /**
32 * Main entry point into dumpsys.
33 */
Felipe Leme343175a2016-08-02 18:57:37 -070034 int main(int argc, char* const argv[]);
35
Vishnu Naire4f61742017-12-21 08:30:28 -080036 /**
37 * Returns a list of services.
38 * @param priorityFlags filter services by specified priorities
39 * @param supportsProto filter services that support proto dumps
40 * @return list of services
41 */
42 Vector<String16> listServices(int priorityFlags, bool supportsProto) const;
43
44 /**
45 * Modifies @{code args} to add additional arguments to indicate if the service
46 * must dump as proto or dump to a certian priority bucket.
47 * @param args initial list of arguments to pass to service dump method.
48 * @param asProto dump service as proto by passing an additional --proto arg
49 * @param priorityFlags indicates priority of dump by passing additional priority args
50 * to the service
51 */
Vishnu Naire97d6122018-01-18 13:58:56 -080052 static void setServiceArgs(Vector<String16>& args, bool asProto, int priorityFlags);
Vishnu Naire4f61742017-12-21 08:30:28 -080053
Steven Moreland5a30d342019-10-08 13:53:28 -070054 enum class Type {
55 DUMP, // dump using `dump` function
56 PID, // dump pid of server only
57 };
58
Vishnu Naire4f61742017-12-21 08:30:28 -080059 /**
60 * Starts a thread to connect to a service and get its dump output. The thread redirects
61 * the output to a pipe. Thread must be stopped by a subsequent callto {@code
62 * stopDumpThread}.
63 * @param serviceName
64 * @param args list of arguments to pass to service dump method.
65 * @return {@code OK} thread is started successfully.
66 * {@code NAME_NOT_FOUND} service could not be found.
67 * {@code != OK} error
68 */
Steven Moreland5a30d342019-10-08 13:53:28 -070069 status_t startDumpThread(Type type, const String16& serviceName,
70 const Vector<String16>& args);
Vishnu Naire4f61742017-12-21 08:30:28 -080071
72 /**
73 * Writes a section header to a file descriptor.
74 * @param fd file descriptor to write data
75 * @param serviceName
76 * @param priorityFlags dump priority specified
77 */
78 void writeDumpHeader(int fd, const String16& serviceName, int priorityFlags) const;
79
80 /**
81 * Redirects service dump to a file descriptor. This requires
82 * {@code startDumpThread} to be called successfully otherwise the function will
83 * return {@code INVALID_OPERATION}.
84 * @param fd file descriptor to write data
85 * @param serviceName
86 * @param timeout timeout to terminate the dump if not completed
87 * @param asProto used to supresses additional output to the fd such as timeout
88 * error messages
89 * @param elapsedDuration returns elapsed time in seconds
90 * @param bytesWritten returns number of bytes written
91 * @return {@code OK} if successful
92 * {@code TIMED_OUT} dump timed out
93 * {@code INVALID_OPERATION} invalid state
94 * {@code != OK} error
95 */
96 status_t writeDump(int fd, const String16& serviceName, std::chrono::milliseconds timeout,
97 bool asProto, std::chrono::duration<double>& elapsedDuration,
98 size_t& bytesWritten) const;
99
100 /**
101 * Writes a section footer to a file descriptor with duration info.
102 * @param fd file descriptor to write data
103 * @param serviceName
104 * @param elapsedDuration duration of dump
105 */
106 void writeDumpFooter(int fd, const String16& serviceName,
107 const std::chrono::duration<double>& elapsedDuration) const;
108
109 /**
110 * Terminates dump thread.
111 * @param dumpComplete If {@code true}, indicates the dump was successfully completed and
112 * tries to join the thread. Otherwise thread is detached.
113 */
114 void stopDumpThread(bool dumpComplete);
115
116 /**
117 * Returns file descriptor of the pipe used to dump service data. This assumes
118 * {@code startDumpThread} was called successfully.
119 */
120 int getDumpFd() const {
121 return redirectFd_.get();
122 }
123
Felipe Leme343175a2016-08-02 18:57:37 -0700124 private:
125 android::IServiceManager* sm_;
Vishnu Naire4f61742017-12-21 08:30:28 -0800126 std::thread activeThread_;
127 mutable android::base::unique_fd redirectFd_;
Felipe Leme343175a2016-08-02 18:57:37 -0700128};
129}
130
131#endif // FRAMEWORK_NATIVE_CMD_DUMPSYS_H_