Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Command that dumps interesting system state to the log. |
| 3 | * |
| 4 | */ |
| 5 | |
| 6 | #define LOG_TAG "dumpsys" |
| 7 | |
Josh Gao | 4b8f6f9 | 2016-02-29 16:20:34 -0800 | [diff] [blame^] | 8 | #include <algorithm> |
| 9 | #include <chrono> |
| 10 | #include <thread> |
| 11 | |
| 12 | #include <android-base/file.h> |
| 13 | #include <android-base/unique_fd.h> |
| 14 | #include <binder/IServiceManager.h> |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 15 | #include <binder/Parcel.h> |
| 16 | #include <binder/ProcessState.h> |
Mathias Agopian | 002e1e5 | 2013-05-06 20:20:50 -0700 | [diff] [blame] | 17 | #include <binder/TextOutput.h> |
Josh Gao | 4b8f6f9 | 2016-02-29 16:20:34 -0800 | [diff] [blame^] | 18 | #include <utils/Log.h> |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 19 | #include <utils/Vector.h> |
| 20 | |
Josh Gao | 4b8f6f9 | 2016-02-29 16:20:34 -0800 | [diff] [blame^] | 21 | #include <fcntl.h> |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 22 | #include <getopt.h> |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 23 | #include <stdio.h> |
Josh Gao | 4b8f6f9 | 2016-02-29 16:20:34 -0800 | [diff] [blame^] | 24 | #include <stdlib.h> |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 25 | #include <string.h> |
Josh Gao | 4b8f6f9 | 2016-02-29 16:20:34 -0800 | [diff] [blame^] | 26 | #include <sys/poll.h> |
| 27 | #include <sys/socket.h> |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 28 | #include <sys/time.h> |
Josh Gao | 4b8f6f9 | 2016-02-29 16:20:34 -0800 | [diff] [blame^] | 29 | #include <sys/types.h> |
| 30 | #include <unistd.h> |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 31 | |
| 32 | using namespace android; |
Josh Gao | 4b8f6f9 | 2016-02-29 16:20:34 -0800 | [diff] [blame^] | 33 | using android::base::unique_fd; |
| 34 | using android::base::WriteFully; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 35 | |
| 36 | static int sort_func(const String16* lhs, const String16* rhs) |
| 37 | { |
| 38 | return lhs->compare(*rhs); |
| 39 | } |
| 40 | |
Felipe Leme | bbfd2b8 | 2016-02-03 11:16:27 -0800 | [diff] [blame] | 41 | static void usage() { |
| 42 | fprintf(stderr, |
| 43 | "usage: dumpsys\n" |
| 44 | " To dump all services.\n" |
| 45 | "or:\n" |
Felipe Leme | 859aef6 | 2016-02-03 12:17:10 -0800 | [diff] [blame] | 46 | " dumpsys [--help | -l | --skip SERVICES | SERVICE [ARGS]]\n" |
Felipe Leme | bbfd2b8 | 2016-02-03 11:16:27 -0800 | [diff] [blame] | 47 | " --help: shows this help\n" |
| 48 | " -l: only list services, do not dump them\n" |
Felipe Leme | 859aef6 | 2016-02-03 12:17:10 -0800 | [diff] [blame] | 49 | " --skip SERVICES: dumps all services but SERVICES (comma-separated list)\n" |
| 50 | " SERVICE [ARGS]: dumps only service SERVICE, optionally passing ARGS to it\n"); |
| 51 | } |
| 52 | |
| 53 | bool IsSkipped(const Vector<String16>& skipped, const String16& service) { |
| 54 | for (const auto& candidate : skipped) { |
| 55 | if (candidate == service) { |
| 56 | return true; |
| 57 | } |
| 58 | } |
| 59 | return false; |
Felipe Leme | bbfd2b8 | 2016-02-03 11:16:27 -0800 | [diff] [blame] | 60 | } |
| 61 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 62 | int main(int argc, char* const argv[]) |
| 63 | { |
JP Abgrall | 3e03d3f | 2012-05-11 14:14:09 -0700 | [diff] [blame] | 64 | signal(SIGPIPE, SIG_IGN); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 65 | sp<IServiceManager> sm = defaultServiceManager(); |
| 66 | fflush(stdout); |
| 67 | if (sm == NULL) { |
Felipe Leme | bbfd2b8 | 2016-02-03 11:16:27 -0800 | [diff] [blame] | 68 | ALOGE("Unable to get default service manager!"); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 69 | aerr << "dumpsys: Unable to get default service manager!" << endl; |
| 70 | return 20; |
| 71 | } |
| 72 | |
| 73 | Vector<String16> services; |
| 74 | Vector<String16> args; |
Felipe Leme | 859aef6 | 2016-02-03 12:17:10 -0800 | [diff] [blame] | 75 | Vector<String16> skippedServices; |
keunyoung | caad555 | 2013-06-13 15:08:51 -0700 | [diff] [blame] | 76 | bool showListOnly = false; |
Felipe Leme | bbfd2b8 | 2016-02-03 11:16:27 -0800 | [diff] [blame] | 77 | if (argc == 2) { |
Felipe Leme | 859aef6 | 2016-02-03 12:17:10 -0800 | [diff] [blame] | 78 | // 1 argument: check for special cases (-l or --help) |
Felipe Leme | bbfd2b8 | 2016-02-03 11:16:27 -0800 | [diff] [blame] | 79 | if (strcmp(argv[1], "--help") == 0) { |
| 80 | usage(); |
| 81 | return 0; |
| 82 | } |
| 83 | if (strcmp(argv[1], "-l") == 0) { |
| 84 | showListOnly = true; |
| 85 | } |
keunyoung | caad555 | 2013-06-13 15:08:51 -0700 | [diff] [blame] | 86 | } |
Felipe Leme | 859aef6 | 2016-02-03 12:17:10 -0800 | [diff] [blame] | 87 | if (argc == 3) { |
| 88 | // 2 arguments: check for special cases (--skip SERVICES) |
| 89 | if (strcmp(argv[1], "--skip") == 0) { |
| 90 | char* token = strtok(argv[2], ","); |
| 91 | while (token != NULL) { |
| 92 | skippedServices.add(String16(token)); |
| 93 | token = strtok(NULL, ","); |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | bool dumpAll = argc == 1; |
| 98 | if (dumpAll || !skippedServices.empty() || showListOnly) { |
| 99 | // gets all services |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 100 | services = sm->listServices(); |
| 101 | services.sort(sort_func); |
| 102 | args.add(String16("-a")); |
| 103 | } else { |
Felipe Leme | 859aef6 | 2016-02-03 12:17:10 -0800 | [diff] [blame] | 104 | // gets a specific service: |
| 105 | // first check if its name is not a special argument... |
| 106 | if (strcmp(argv[1], "--skip") == 0 || strcmp(argv[1], "-l") == 0) { |
| 107 | usage(); |
| 108 | return -1; |
| 109 | } |
| 110 | // ...then gets its arguments |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 111 | services.add(String16(argv[1])); |
| 112 | for (int i=2; i<argc; i++) { |
| 113 | args.add(String16(argv[i])); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | const size_t N = services.size(); |
| 118 | |
| 119 | if (N > 1) { |
| 120 | // first print a list of the current services |
| 121 | aout << "Currently running services:" << endl; |
Felipe Leme | bbfd2b8 | 2016-02-03 11:16:27 -0800 | [diff] [blame] | 122 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 123 | for (size_t i=0; i<N; i++) { |
| 124 | sp<IBinder> service = sm->checkService(services[i]); |
| 125 | if (service != NULL) { |
Felipe Leme | 859aef6 | 2016-02-03 12:17:10 -0800 | [diff] [blame] | 126 | bool skipped = IsSkipped(skippedServices, services[i]); |
| 127 | aout << " " << services[i] << (skipped ? " (skipped)" : "") << endl; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 128 | } |
| 129 | } |
| 130 | } |
| 131 | |
keunyoung | caad555 | 2013-06-13 15:08:51 -0700 | [diff] [blame] | 132 | if (showListOnly) { |
| 133 | return 0; |
| 134 | } |
| 135 | |
Josh Gao | 4b8f6f9 | 2016-02-29 16:20:34 -0800 | [diff] [blame^] | 136 | for (size_t i = 0; i < N; i++) { |
| 137 | String16 service_name = std::move(services[i]); |
| 138 | if (IsSkipped(skippedServices, service_name)) continue; |
Felipe Leme | 859aef6 | 2016-02-03 12:17:10 -0800 | [diff] [blame] | 139 | |
Josh Gao | 4b8f6f9 | 2016-02-29 16:20:34 -0800 | [diff] [blame^] | 140 | sp<IBinder> service = sm->checkService(service_name); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 141 | if (service != NULL) { |
Josh Gao | 4b8f6f9 | 2016-02-29 16:20:34 -0800 | [diff] [blame^] | 142 | int sfd[2]; |
| 143 | |
| 144 | // Use a socketpair instead of a pipe to avoid sending SIGPIPE to services that timeout. |
| 145 | if (socketpair(AF_UNIX, SOCK_STREAM, 0, sfd) != 0) { |
| 146 | aerr << "Failed to create socketpair to dump service info for " << service_name |
| 147 | << ": " << strerror(errno) << endl; |
| 148 | continue; |
| 149 | } |
| 150 | |
| 151 | unique_fd local_end(sfd[0]); |
| 152 | unique_fd remote_end(sfd[1]); |
| 153 | sfd[0] = sfd[1] = -1; |
| 154 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 155 | if (N > 1) { |
| 156 | aout << "------------------------------------------------------------" |
| 157 | "-------------------" << endl; |
Josh Gao | 4b8f6f9 | 2016-02-29 16:20:34 -0800 | [diff] [blame^] | 158 | aout << "DUMP OF SERVICE " << service_name << ":" << endl; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 159 | } |
Josh Gao | 4b8f6f9 | 2016-02-29 16:20:34 -0800 | [diff] [blame^] | 160 | |
| 161 | // dump blocks until completion, so spawn a thread.. |
| 162 | std::thread dump_thread([=, remote_end { std::move(remote_end) }]() mutable { |
| 163 | int err = service->dump(remote_end.get(), args); |
| 164 | |
| 165 | // It'd be nice to be able to close the remote end of the socketpair before the dump |
| 166 | // call returns, to terminate our reads if the other end closes their copy of the |
| 167 | // file descriptor, but then hangs for some reason. There doesn't seem to be a good |
| 168 | // way to do this, though. |
| 169 | remote_end.clear(); |
| 170 | |
| 171 | if (err != 0) { |
| 172 | aerr << "Error dumping service info: (" << strerror(err) << ") " << service_name |
| 173 | << endl; |
| 174 | } |
| 175 | }); |
| 176 | |
| 177 | // TODO: Make this configurable at runtime. |
| 178 | constexpr auto timeout = std::chrono::seconds(10); |
| 179 | auto end = std::chrono::steady_clock::now() + timeout; |
| 180 | |
| 181 | struct pollfd pfd = { |
| 182 | .fd = local_end.get(), |
| 183 | .events = POLLIN |
| 184 | }; |
| 185 | |
| 186 | bool timed_out = false; |
| 187 | bool error = false; |
| 188 | while (true) { |
| 189 | // Wrap this in a lambda so that TEMP_FAILURE_RETRY recalculates the timeout. |
| 190 | auto time_left_ms = [end]() { |
| 191 | auto now = std::chrono::steady_clock::now(); |
| 192 | auto diff = std::chrono::duration_cast<std::chrono::milliseconds>(end - now); |
| 193 | return std::max(diff.count(), 0ll); |
| 194 | }; |
| 195 | |
| 196 | int rc = TEMP_FAILURE_RETRY(poll(&pfd, 1, time_left_ms())); |
| 197 | if (rc < 0) { |
| 198 | aerr << "Error in poll while dumping service " << service_name << " : " |
| 199 | << strerror(errno) << endl; |
| 200 | error = true; |
| 201 | break; |
| 202 | } else if (rc == 0) { |
| 203 | timed_out = true; |
| 204 | break; |
| 205 | } |
| 206 | |
| 207 | char buf[4096]; |
| 208 | rc = TEMP_FAILURE_RETRY(read(local_end.get(), buf, sizeof(buf))); |
| 209 | if (rc < 0) { |
| 210 | aerr << "Failed to read while dumping service " << service_name << ": " |
| 211 | << strerror(errno) << endl; |
| 212 | error = true; |
| 213 | break; |
| 214 | } else if (rc == 0) { |
| 215 | // EOF. |
| 216 | break; |
| 217 | } |
| 218 | |
| 219 | if (!WriteFully(STDOUT_FILENO, buf, rc)) { |
| 220 | aerr << "Failed to write while dumping service " << service_name << ": " |
| 221 | << strerror(errno) << endl; |
| 222 | error = true; |
| 223 | break; |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | if (timed_out) { |
| 228 | aout << endl << "*** SERVICE DUMP TIMEOUT EXPIRED ***" << endl << endl; |
| 229 | } |
| 230 | |
| 231 | if (timed_out || error) { |
| 232 | dump_thread.detach(); |
| 233 | } else { |
| 234 | dump_thread.join(); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 235 | } |
| 236 | } else { |
Josh Gao | 4b8f6f9 | 2016-02-29 16:20:34 -0800 | [diff] [blame^] | 237 | aerr << "Can't find service: " << service_name << endl; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 238 | } |
| 239 | } |
| 240 | |
| 241 | return 0; |
| 242 | } |