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" |
Thierry Strudel | 8b78b75 | 2016-03-22 10:25:44 -0700 | [diff] [blame^] | 46 | " dumpsys [-t TIMEOUT] [--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" |
Thierry Strudel | 8b78b75 | 2016-03-22 10:25:44 -0700 | [diff] [blame^] | 49 | " -t TIMEOUT: TIMEOUT to use in seconds instead of default 10 seconds\n" |
Felipe Leme | 859aef6 | 2016-02-03 12:17:10 -0800 | [diff] [blame] | 50 | " --skip SERVICES: dumps all services but SERVICES (comma-separated list)\n" |
| 51 | " SERVICE [ARGS]: dumps only service SERVICE, optionally passing ARGS to it\n"); |
| 52 | } |
| 53 | |
| 54 | bool IsSkipped(const Vector<String16>& skipped, const String16& service) { |
| 55 | for (const auto& candidate : skipped) { |
| 56 | if (candidate == service) { |
| 57 | return true; |
| 58 | } |
| 59 | } |
| 60 | return false; |
Felipe Leme | bbfd2b8 | 2016-02-03 11:16:27 -0800 | [diff] [blame] | 61 | } |
| 62 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 63 | int main(int argc, char* const argv[]) |
| 64 | { |
JP Abgrall | 3e03d3f | 2012-05-11 14:14:09 -0700 | [diff] [blame] | 65 | signal(SIGPIPE, SIG_IGN); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 66 | sp<IServiceManager> sm = defaultServiceManager(); |
| 67 | fflush(stdout); |
| 68 | if (sm == NULL) { |
Felipe Leme | bbfd2b8 | 2016-02-03 11:16:27 -0800 | [diff] [blame] | 69 | ALOGE("Unable to get default service manager!"); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 70 | aerr << "dumpsys: Unable to get default service manager!" << endl; |
| 71 | return 20; |
| 72 | } |
| 73 | |
| 74 | Vector<String16> services; |
| 75 | Vector<String16> args; |
Felipe Leme | 859aef6 | 2016-02-03 12:17:10 -0800 | [diff] [blame] | 76 | Vector<String16> skippedServices; |
keunyoung | caad555 | 2013-06-13 15:08:51 -0700 | [diff] [blame] | 77 | bool showListOnly = false; |
Thierry Strudel | 8b78b75 | 2016-03-22 10:25:44 -0700 | [diff] [blame^] | 78 | bool skipServices = false; |
| 79 | int timeoutArg = 10; |
| 80 | static struct option longOptions[] = { |
| 81 | {"skip", no_argument, 0, 0 }, |
| 82 | {"help", no_argument, 0, 0 }, |
| 83 | { 0, 0, 0, 0 } |
| 84 | }; |
| 85 | |
| 86 | while (1) { |
| 87 | int c; |
| 88 | int optionIndex = 0; |
| 89 | |
| 90 | c = getopt_long(argc, argv, "+t:l", longOptions, &optionIndex); |
| 91 | |
| 92 | if (c == -1) { |
| 93 | break; |
Felipe Leme | bbfd2b8 | 2016-02-03 11:16:27 -0800 | [diff] [blame] | 94 | } |
Thierry Strudel | 8b78b75 | 2016-03-22 10:25:44 -0700 | [diff] [blame^] | 95 | |
| 96 | switch (c) { |
| 97 | case 0: |
| 98 | if (!strcmp(longOptions[optionIndex].name, "skip")) { |
| 99 | skipServices = true; |
| 100 | } else if (!strcmp(longOptions[optionIndex].name, "help")) { |
| 101 | usage(); |
| 102 | return 0; |
| 103 | } |
| 104 | break; |
| 105 | |
| 106 | case 't': |
| 107 | { |
| 108 | char *endptr; |
| 109 | timeoutArg = strtol(optarg, &endptr, 10); |
| 110 | if (*endptr != '\0' || timeoutArg <= 0) { |
| 111 | fprintf(stderr, "Error: invalid timeout number: '%s'\n", optarg); |
| 112 | return -1; |
| 113 | } |
| 114 | } |
| 115 | break; |
| 116 | |
| 117 | case 'l': |
Felipe Leme | bbfd2b8 | 2016-02-03 11:16:27 -0800 | [diff] [blame] | 118 | showListOnly = true; |
Thierry Strudel | 8b78b75 | 2016-03-22 10:25:44 -0700 | [diff] [blame^] | 119 | break; |
| 120 | |
| 121 | default: |
| 122 | fprintf(stderr, "\n"); |
| 123 | usage(); |
| 124 | return -1; |
Felipe Leme | bbfd2b8 | 2016-02-03 11:16:27 -0800 | [diff] [blame] | 125 | } |
keunyoung | caad555 | 2013-06-13 15:08:51 -0700 | [diff] [blame] | 126 | } |
Thierry Strudel | 8b78b75 | 2016-03-22 10:25:44 -0700 | [diff] [blame^] | 127 | |
| 128 | for (int i = optind; i < argc; i++) { |
| 129 | if (skipServices) { |
| 130 | skippedServices.add(String16(argv[i])); |
| 131 | } else { |
| 132 | if (i == optind) { |
| 133 | services.add(String16(argv[i])); |
| 134 | } else { |
| 135 | args.add(String16(argv[i])); |
Felipe Leme | 859aef6 | 2016-02-03 12:17:10 -0800 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | } |
Thierry Strudel | 8b78b75 | 2016-03-22 10:25:44 -0700 | [diff] [blame^] | 139 | |
| 140 | if ((skipServices && skippedServices.empty()) || |
| 141 | (!skipServices && !showListOnly && services.empty()) || |
| 142 | (showListOnly && (!services.empty() || !skippedServices.empty()))) { |
| 143 | usage(); |
| 144 | return -1; |
| 145 | } |
| 146 | |
| 147 | if (!skippedServices.empty() || showListOnly) { |
Felipe Leme | 859aef6 | 2016-02-03 12:17:10 -0800 | [diff] [blame] | 148 | // gets all services |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 149 | services = sm->listServices(); |
| 150 | services.sort(sort_func); |
| 151 | args.add(String16("-a")); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | const size_t N = services.size(); |
| 155 | |
| 156 | if (N > 1) { |
| 157 | // first print a list of the current services |
| 158 | aout << "Currently running services:" << endl; |
Felipe Leme | bbfd2b8 | 2016-02-03 11:16:27 -0800 | [diff] [blame] | 159 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 160 | for (size_t i=0; i<N; i++) { |
| 161 | sp<IBinder> service = sm->checkService(services[i]); |
| 162 | if (service != NULL) { |
Felipe Leme | 859aef6 | 2016-02-03 12:17:10 -0800 | [diff] [blame] | 163 | bool skipped = IsSkipped(skippedServices, services[i]); |
| 164 | aout << " " << services[i] << (skipped ? " (skipped)" : "") << endl; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 165 | } |
| 166 | } |
| 167 | } |
| 168 | |
keunyoung | caad555 | 2013-06-13 15:08:51 -0700 | [diff] [blame] | 169 | if (showListOnly) { |
| 170 | return 0; |
| 171 | } |
| 172 | |
Josh Gao | 4b8f6f9 | 2016-02-29 16:20:34 -0800 | [diff] [blame] | 173 | for (size_t i = 0; i < N; i++) { |
| 174 | String16 service_name = std::move(services[i]); |
| 175 | if (IsSkipped(skippedServices, service_name)) continue; |
Felipe Leme | 859aef6 | 2016-02-03 12:17:10 -0800 | [diff] [blame] | 176 | |
Josh Gao | 4b8f6f9 | 2016-02-29 16:20:34 -0800 | [diff] [blame] | 177 | sp<IBinder> service = sm->checkService(service_name); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 178 | if (service != NULL) { |
Josh Gao | 4b8f6f9 | 2016-02-29 16:20:34 -0800 | [diff] [blame] | 179 | int sfd[2]; |
| 180 | |
Josh Gao | 49f0a0c | 2016-03-04 13:12:29 -0800 | [diff] [blame] | 181 | if (pipe(sfd) != 0) { |
| 182 | aerr << "Failed to create pipe to dump service info for " << service_name |
Josh Gao | 4b8f6f9 | 2016-02-29 16:20:34 -0800 | [diff] [blame] | 183 | << ": " << strerror(errno) << endl; |
| 184 | continue; |
| 185 | } |
| 186 | |
| 187 | unique_fd local_end(sfd[0]); |
| 188 | unique_fd remote_end(sfd[1]); |
| 189 | sfd[0] = sfd[1] = -1; |
| 190 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 191 | if (N > 1) { |
| 192 | aout << "------------------------------------------------------------" |
| 193 | "-------------------" << endl; |
Josh Gao | 4b8f6f9 | 2016-02-29 16:20:34 -0800 | [diff] [blame] | 194 | aout << "DUMP OF SERVICE " << service_name << ":" << endl; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 195 | } |
Josh Gao | 4b8f6f9 | 2016-02-29 16:20:34 -0800 | [diff] [blame] | 196 | |
| 197 | // dump blocks until completion, so spawn a thread.. |
| 198 | std::thread dump_thread([=, remote_end { std::move(remote_end) }]() mutable { |
| 199 | int err = service->dump(remote_end.get(), args); |
| 200 | |
| 201 | // It'd be nice to be able to close the remote end of the socketpair before the dump |
| 202 | // call returns, to terminate our reads if the other end closes their copy of the |
| 203 | // file descriptor, but then hangs for some reason. There doesn't seem to be a good |
| 204 | // way to do this, though. |
| 205 | remote_end.clear(); |
| 206 | |
| 207 | if (err != 0) { |
| 208 | aerr << "Error dumping service info: (" << strerror(err) << ") " << service_name |
| 209 | << endl; |
| 210 | } |
| 211 | }); |
| 212 | |
Thierry Strudel | 8b78b75 | 2016-03-22 10:25:44 -0700 | [diff] [blame^] | 213 | auto timeout = std::chrono::seconds(timeoutArg); |
Josh Gao | 4b8f6f9 | 2016-02-29 16:20:34 -0800 | [diff] [blame] | 214 | auto end = std::chrono::steady_clock::now() + timeout; |
| 215 | |
| 216 | struct pollfd pfd = { |
| 217 | .fd = local_end.get(), |
| 218 | .events = POLLIN |
| 219 | }; |
| 220 | |
| 221 | bool timed_out = false; |
| 222 | bool error = false; |
| 223 | while (true) { |
| 224 | // Wrap this in a lambda so that TEMP_FAILURE_RETRY recalculates the timeout. |
| 225 | auto time_left_ms = [end]() { |
| 226 | auto now = std::chrono::steady_clock::now(); |
| 227 | auto diff = std::chrono::duration_cast<std::chrono::milliseconds>(end - now); |
| 228 | return std::max(diff.count(), 0ll); |
| 229 | }; |
| 230 | |
| 231 | int rc = TEMP_FAILURE_RETRY(poll(&pfd, 1, time_left_ms())); |
| 232 | if (rc < 0) { |
| 233 | aerr << "Error in poll while dumping service " << service_name << " : " |
| 234 | << strerror(errno) << endl; |
| 235 | error = true; |
| 236 | break; |
| 237 | } else if (rc == 0) { |
| 238 | timed_out = true; |
| 239 | break; |
| 240 | } |
| 241 | |
| 242 | char buf[4096]; |
| 243 | rc = TEMP_FAILURE_RETRY(read(local_end.get(), buf, sizeof(buf))); |
| 244 | if (rc < 0) { |
| 245 | aerr << "Failed to read while dumping service " << service_name << ": " |
| 246 | << strerror(errno) << endl; |
| 247 | error = true; |
| 248 | break; |
| 249 | } else if (rc == 0) { |
| 250 | // EOF. |
| 251 | break; |
| 252 | } |
| 253 | |
| 254 | if (!WriteFully(STDOUT_FILENO, buf, rc)) { |
| 255 | aerr << "Failed to write while dumping service " << service_name << ": " |
| 256 | << strerror(errno) << endl; |
| 257 | error = true; |
| 258 | break; |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | if (timed_out) { |
| 263 | aout << endl << "*** SERVICE DUMP TIMEOUT EXPIRED ***" << endl << endl; |
| 264 | } |
| 265 | |
| 266 | if (timed_out || error) { |
| 267 | dump_thread.detach(); |
| 268 | } else { |
| 269 | dump_thread.join(); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 270 | } |
| 271 | } else { |
Josh Gao | 4b8f6f9 | 2016-02-29 16:20:34 -0800 | [diff] [blame] | 272 | aerr << "Can't find service: " << service_name << endl; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 273 | } |
| 274 | } |
| 275 | |
| 276 | return 0; |
| 277 | } |