Felipe Leme | 343175a | 2016-08-02 18:57:37 -0700 | [diff] [blame] | 1 | /* |
| 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 Nair | e4f6174 | 2017-12-21 08:30:28 -0800 | [diff] [blame] | 20 | #include <thread> |
| 21 | |
| 22 | #include <android-base/unique_fd.h> |
Felipe Leme | 343175a | 2016-08-02 18:57:37 -0700 | [diff] [blame] | 23 | #include <binder/IServiceManager.h> |
| 24 | |
| 25 | namespace android { |
| 26 | |
| 27 | class Dumpsys { |
| 28 | public: |
Chih-Hung Hsieh | d0937e6 | 2018-12-20 15:43:57 -0800 | [diff] [blame] | 29 | explicit Dumpsys(android::IServiceManager* sm) : sm_(sm) { |
Felipe Leme | 343175a | 2016-08-02 18:57:37 -0700 | [diff] [blame] | 30 | } |
Vishnu Nair | e4f6174 | 2017-12-21 08:30:28 -0800 | [diff] [blame] | 31 | /** |
| 32 | * Main entry point into dumpsys. |
| 33 | */ |
Felipe Leme | 343175a | 2016-08-02 18:57:37 -0700 | [diff] [blame] | 34 | int main(int argc, char* const argv[]); |
| 35 | |
Vishnu Nair | e4f6174 | 2017-12-21 08:30:28 -0800 | [diff] [blame] | 36 | /** |
| 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 Nair | e97d612 | 2018-01-18 13:58:56 -0800 | [diff] [blame] | 52 | static void setServiceArgs(Vector<String16>& args, bool asProto, int priorityFlags); |
Vishnu Nair | e4f6174 | 2017-12-21 08:30:28 -0800 | [diff] [blame] | 53 | |
Steven Moreland | 5a30d34 | 2019-10-08 13:53:28 -0700 | [diff] [blame] | 54 | enum class Type { |
Devin Moore | cfeeda4 | 2020-12-08 12:50:58 -0800 | [diff] [blame^] | 55 | DUMP, // dump using `dump` function |
| 56 | PID, // dump pid of server only |
| 57 | THREAD, // dump thread usage of server only |
Steven Moreland | 5a30d34 | 2019-10-08 13:53:28 -0700 | [diff] [blame] | 58 | }; |
| 59 | |
Vishnu Nair | e4f6174 | 2017-12-21 08:30:28 -0800 | [diff] [blame] | 60 | /** |
| 61 | * Starts a thread to connect to a service and get its dump output. The thread redirects |
Devin Moore | cfeeda4 | 2020-12-08 12:50:58 -0800 | [diff] [blame^] | 62 | * the output to a pipe. Thread must be stopped by a subsequent call to {@code |
Vishnu Nair | e4f6174 | 2017-12-21 08:30:28 -0800 | [diff] [blame] | 63 | * stopDumpThread}. |
| 64 | * @param serviceName |
| 65 | * @param args list of arguments to pass to service dump method. |
| 66 | * @return {@code OK} thread is started successfully. |
| 67 | * {@code NAME_NOT_FOUND} service could not be found. |
| 68 | * {@code != OK} error |
| 69 | */ |
Steven Moreland | 5a30d34 | 2019-10-08 13:53:28 -0700 | [diff] [blame] | 70 | status_t startDumpThread(Type type, const String16& serviceName, |
| 71 | const Vector<String16>& args); |
Vishnu Nair | e4f6174 | 2017-12-21 08:30:28 -0800 | [diff] [blame] | 72 | |
| 73 | /** |
| 74 | * Writes a section header to a file descriptor. |
| 75 | * @param fd file descriptor to write data |
| 76 | * @param serviceName |
| 77 | * @param priorityFlags dump priority specified |
| 78 | */ |
| 79 | void writeDumpHeader(int fd, const String16& serviceName, int priorityFlags) const; |
| 80 | |
| 81 | /** |
| 82 | * Redirects service dump to a file descriptor. This requires |
| 83 | * {@code startDumpThread} to be called successfully otherwise the function will |
| 84 | * return {@code INVALID_OPERATION}. |
| 85 | * @param fd file descriptor to write data |
| 86 | * @param serviceName |
| 87 | * @param timeout timeout to terminate the dump if not completed |
| 88 | * @param asProto used to supresses additional output to the fd such as timeout |
| 89 | * error messages |
| 90 | * @param elapsedDuration returns elapsed time in seconds |
| 91 | * @param bytesWritten returns number of bytes written |
| 92 | * @return {@code OK} if successful |
| 93 | * {@code TIMED_OUT} dump timed out |
| 94 | * {@code INVALID_OPERATION} invalid state |
| 95 | * {@code != OK} error |
| 96 | */ |
| 97 | status_t writeDump(int fd, const String16& serviceName, std::chrono::milliseconds timeout, |
| 98 | bool asProto, std::chrono::duration<double>& elapsedDuration, |
| 99 | size_t& bytesWritten) const; |
| 100 | |
| 101 | /** |
| 102 | * Writes a section footer to a file descriptor with duration info. |
| 103 | * @param fd file descriptor to write data |
| 104 | * @param serviceName |
| 105 | * @param elapsedDuration duration of dump |
| 106 | */ |
| 107 | void writeDumpFooter(int fd, const String16& serviceName, |
| 108 | const std::chrono::duration<double>& elapsedDuration) const; |
| 109 | |
| 110 | /** |
| 111 | * Terminates dump thread. |
| 112 | * @param dumpComplete If {@code true}, indicates the dump was successfully completed and |
| 113 | * tries to join the thread. Otherwise thread is detached. |
| 114 | */ |
| 115 | void stopDumpThread(bool dumpComplete); |
| 116 | |
| 117 | /** |
| 118 | * Returns file descriptor of the pipe used to dump service data. This assumes |
| 119 | * {@code startDumpThread} was called successfully. |
| 120 | */ |
| 121 | int getDumpFd() const { |
| 122 | return redirectFd_.get(); |
| 123 | } |
| 124 | |
Felipe Leme | 343175a | 2016-08-02 18:57:37 -0700 | [diff] [blame] | 125 | private: |
| 126 | android::IServiceManager* sm_; |
Vishnu Nair | e4f6174 | 2017-12-21 08:30:28 -0800 | [diff] [blame] | 127 | std::thread activeThread_; |
| 128 | mutable android::base::unique_fd redirectFd_; |
Felipe Leme | 343175a | 2016-08-02 18:57:37 -0700 | [diff] [blame] | 129 | }; |
| 130 | } |
| 131 | |
| 132 | #endif // FRAMEWORK_NATIVE_CMD_DUMPSYS_H_ |