blob: 6ab1a7dcb5623f90b9c1f014d42b5a4cc1369630 [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 Morelandcbd69fc2021-07-20 20:45:43 +000054 enum Type {
Devin Mooredef9fae2021-08-05 19:05:45 +000055 TYPE_DUMP = 0x1, // dump using `dump` function
56 TYPE_PID = 0x2, // dump pid of server only
57 TYPE_STABILITY = 0x4, // dump stability information of server
58 TYPE_THREAD = 0x8, // dump thread usage of server only
59 TYPE_CLIENTS = 0x10, // dump pid of clients
Steven Moreland5a30d342019-10-08 13:53:28 -070060 };
61
Vishnu Naire4f61742017-12-21 08:30:28 -080062 /**
63 * Starts a thread to connect to a service and get its dump output. The thread redirects
Devin Moorecfeeda42020-12-08 12:50:58 -080064 * the output to a pipe. Thread must be stopped by a subsequent call to {@code
Vishnu Naire4f61742017-12-21 08:30:28 -080065 * stopDumpThread}.
Steven Morelandcbd69fc2021-07-20 20:45:43 +000066 * @param dumpTypeFlags operations to perform
Vishnu Naire4f61742017-12-21 08:30:28 -080067 * @param serviceName
68 * @param args list of arguments to pass to service dump method.
69 * @return {@code OK} thread is started successfully.
70 * {@code NAME_NOT_FOUND} service could not be found.
71 * {@code != OK} error
72 */
Steven Morelandcbd69fc2021-07-20 20:45:43 +000073 status_t startDumpThread(int dumpTypeFlags, const String16& serviceName,
Steven Moreland5a30d342019-10-08 13:53:28 -070074 const Vector<String16>& args);
Vishnu Naire4f61742017-12-21 08:30:28 -080075
76 /**
77 * Writes a section header to a file descriptor.
78 * @param fd file descriptor to write data
79 * @param serviceName
80 * @param priorityFlags dump priority specified
81 */
82 void writeDumpHeader(int fd, const String16& serviceName, int priorityFlags) const;
83
84 /**
85 * Redirects service dump to a file descriptor. This requires
86 * {@code startDumpThread} to be called successfully otherwise the function will
87 * return {@code INVALID_OPERATION}.
88 * @param fd file descriptor to write data
89 * @param serviceName
90 * @param timeout timeout to terminate the dump if not completed
91 * @param asProto used to supresses additional output to the fd such as timeout
92 * error messages
93 * @param elapsedDuration returns elapsed time in seconds
94 * @param bytesWritten returns number of bytes written
95 * @return {@code OK} if successful
96 * {@code TIMED_OUT} dump timed out
97 * {@code INVALID_OPERATION} invalid state
98 * {@code != OK} error
99 */
100 status_t writeDump(int fd, const String16& serviceName, std::chrono::milliseconds timeout,
101 bool asProto, std::chrono::duration<double>& elapsedDuration,
102 size_t& bytesWritten) const;
103
104 /**
105 * Writes a section footer to a file descriptor with duration info.
106 * @param fd file descriptor to write data
107 * @param serviceName
108 * @param elapsedDuration duration of dump
109 */
110 void writeDumpFooter(int fd, const String16& serviceName,
111 const std::chrono::duration<double>& elapsedDuration) const;
112
113 /**
114 * Terminates dump thread.
115 * @param dumpComplete If {@code true}, indicates the dump was successfully completed and
116 * tries to join the thread. Otherwise thread is detached.
117 */
118 void stopDumpThread(bool dumpComplete);
119
120 /**
121 * Returns file descriptor of the pipe used to dump service data. This assumes
122 * {@code startDumpThread} was called successfully.
123 */
124 int getDumpFd() const {
125 return redirectFd_.get();
126 }
127
Felipe Leme343175a2016-08-02 18:57:37 -0700128 private:
129 android::IServiceManager* sm_;
Vishnu Naire4f61742017-12-21 08:30:28 -0800130 std::thread activeThread_;
131 mutable android::base::unique_fd redirectFd_;
Felipe Leme343175a2016-08-02 18:57:37 -0700132};
133}
134
135#endif // FRAMEWORK_NATIVE_CMD_DUMPSYS_H_