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