| Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1 | /* | 
| Felipe Leme | 343175a | 2016-08-02 18:57:37 -0700 | [diff] [blame] | 2 | * Copyright (C) 2009 The Android Open Source Project | 
| Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 3 | * | 
| Felipe Leme | 343175a | 2016-08-02 18:57:37 -0700 | [diff] [blame] | 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. | 
| Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 15 | */ | 
|  | 16 |  | 
| Josh Gao | 4b8f6f9 | 2016-02-29 16:20:34 -0800 | [diff] [blame] | 17 | #include <algorithm> | 
|  | 18 | #include <chrono> | 
| Kevin Rocard | 430e079 | 2017-08-14 20:40:24 -0700 | [diff] [blame] | 19 | #include <iomanip> | 
| Josh Gao | 4b8f6f9 | 2016-02-29 16:20:34 -0800 | [diff] [blame] | 20 | #include <thread> | 
|  | 21 |  | 
|  | 22 | #include <android-base/file.h> | 
| mukesh agrawal | 2f1eb1c | 2016-06-08 18:16:36 -0700 | [diff] [blame] | 23 | #include <android-base/stringprintf.h> | 
| Josh Gao | 4b8f6f9 | 2016-02-29 16:20:34 -0800 | [diff] [blame] | 24 | #include <android-base/unique_fd.h> | 
| Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 25 | #include <binder/Parcel.h> | 
|  | 26 | #include <binder/ProcessState.h> | 
| Mathias Agopian | 002e1e5 | 2013-05-06 20:20:50 -0700 | [diff] [blame] | 27 | #include <binder/TextOutput.h> | 
| Josh Gao | 4b8f6f9 | 2016-02-29 16:20:34 -0800 | [diff] [blame] | 28 | #include <utils/Log.h> | 
| Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 29 | #include <utils/Vector.h> | 
|  | 30 |  | 
| Josh Gao | 4b8f6f9 | 2016-02-29 16:20:34 -0800 | [diff] [blame] | 31 | #include <fcntl.h> | 
| Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 32 | #include <getopt.h> | 
| Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 33 | #include <stdio.h> | 
| Josh Gao | 4b8f6f9 | 2016-02-29 16:20:34 -0800 | [diff] [blame] | 34 | #include <stdlib.h> | 
| Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 35 | #include <string.h> | 
| Josh Gao | 4b8f6f9 | 2016-02-29 16:20:34 -0800 | [diff] [blame] | 36 | #include <sys/poll.h> | 
|  | 37 | #include <sys/socket.h> | 
| Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 38 | #include <sys/time.h> | 
| Josh Gao | 4b8f6f9 | 2016-02-29 16:20:34 -0800 | [diff] [blame] | 39 | #include <sys/types.h> | 
|  | 40 | #include <unistd.h> | 
| Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 41 |  | 
| Felipe Leme | 343175a | 2016-08-02 18:57:37 -0700 | [diff] [blame] | 42 | #include "dumpsys.h" | 
|  | 43 |  | 
| Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 44 | using namespace android; | 
| mukesh agrawal | 2f1eb1c | 2016-06-08 18:16:36 -0700 | [diff] [blame] | 45 | using android::base::StringPrintf; | 
| Josh Gao | 4b8f6f9 | 2016-02-29 16:20:34 -0800 | [diff] [blame] | 46 | using android::base::unique_fd; | 
|  | 47 | using android::base::WriteFully; | 
| Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 48 |  | 
| Steven Moreland | 2c3cd83 | 2017-02-13 23:44:17 +0000 | [diff] [blame] | 49 | static int sort_func(const String16* lhs, const String16* rhs) | 
|  | 50 | { | 
| Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 51 | return lhs->compare(*rhs); | 
|  | 52 | } | 
|  | 53 |  | 
| Felipe Leme | bbfd2b8 | 2016-02-03 11:16:27 -0800 | [diff] [blame] | 54 | static void usage() { | 
|  | 55 | fprintf(stderr, | 
|  | 56 | "usage: dumpsys\n" | 
|  | 57 | "         To dump all services.\n" | 
|  | 58 | "or:\n" | 
| Steven Moreland | 2c3cd83 | 2017-02-13 23:44:17 +0000 | [diff] [blame] | 59 | "       dumpsys [-t TIMEOUT] [--help | -l | --skip SERVICES | SERVICE [ARGS]]\n" | 
| Felipe Leme | bbfd2b8 | 2016-02-03 11:16:27 -0800 | [diff] [blame] | 60 | "         --help: shows this help\n" | 
|  | 61 | "         -l: only list services, do not dump them\n" | 
| Thierry Strudel | 8b78b75 | 2016-03-22 10:25:44 -0700 | [diff] [blame] | 62 | "         -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] | 63 | "         --skip SERVICES: dumps all services but SERVICES (comma-separated list)\n" | 
|  | 64 | "         SERVICE [ARGS]: dumps only service SERVICE, optionally passing ARGS to it\n"); | 
|  | 65 | } | 
|  | 66 |  | 
| Felipe Leme | 343175a | 2016-08-02 18:57:37 -0700 | [diff] [blame] | 67 | static bool IsSkipped(const Vector<String16>& skipped, const String16& service) { | 
| Felipe Leme | 859aef6 | 2016-02-03 12:17:10 -0800 | [diff] [blame] | 68 | for (const auto& candidate : skipped) { | 
|  | 69 | if (candidate == service) { | 
|  | 70 | return true; | 
|  | 71 | } | 
|  | 72 | } | 
|  | 73 | return false; | 
| Felipe Leme | bbfd2b8 | 2016-02-03 11:16:27 -0800 | [diff] [blame] | 74 | } | 
|  | 75 |  | 
| Felipe Leme | 343175a | 2016-08-02 18:57:37 -0700 | [diff] [blame] | 76 | int Dumpsys::main(int argc, char* const argv[]) { | 
| Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 77 | Vector<String16> services; | 
|  | 78 | Vector<String16> args; | 
| Felipe Leme | 859aef6 | 2016-02-03 12:17:10 -0800 | [diff] [blame] | 79 | Vector<String16> skippedServices; | 
| keunyoung | caad555 | 2013-06-13 15:08:51 -0700 | [diff] [blame] | 80 | bool showListOnly = false; | 
| Thierry Strudel | 8b78b75 | 2016-03-22 10:25:44 -0700 | [diff] [blame] | 81 | bool skipServices = false; | 
|  | 82 | int timeoutArg = 10; | 
|  | 83 | static struct option longOptions[] = { | 
|  | 84 | {"skip", no_argument, 0,  0 }, | 
|  | 85 | {"help", no_argument, 0,  0 }, | 
|  | 86 | {     0,           0, 0,  0 } | 
|  | 87 | }; | 
|  | 88 |  | 
| Felipe Leme | 343175a | 2016-08-02 18:57:37 -0700 | [diff] [blame] | 89 | // Must reset optind, otherwise subsequent calls will fail (wouldn't happen on main.cpp, but | 
|  | 90 | // happens on test cases). | 
|  | 91 | optind = 1; | 
| Thierry Strudel | 8b78b75 | 2016-03-22 10:25:44 -0700 | [diff] [blame] | 92 | while (1) { | 
|  | 93 | int c; | 
|  | 94 | int optionIndex = 0; | 
|  | 95 |  | 
|  | 96 | c = getopt_long(argc, argv, "+t:l", longOptions, &optionIndex); | 
|  | 97 |  | 
|  | 98 | if (c == -1) { | 
|  | 99 | break; | 
| Felipe Leme | bbfd2b8 | 2016-02-03 11:16:27 -0800 | [diff] [blame] | 100 | } | 
| Thierry Strudel | 8b78b75 | 2016-03-22 10:25:44 -0700 | [diff] [blame] | 101 |  | 
|  | 102 | switch (c) { | 
|  | 103 | case 0: | 
|  | 104 | if (!strcmp(longOptions[optionIndex].name, "skip")) { | 
|  | 105 | skipServices = true; | 
|  | 106 | } else if (!strcmp(longOptions[optionIndex].name, "help")) { | 
|  | 107 | usage(); | 
|  | 108 | return 0; | 
|  | 109 | } | 
|  | 110 | break; | 
|  | 111 |  | 
|  | 112 | case 't': | 
|  | 113 | { | 
|  | 114 | char *endptr; | 
|  | 115 | timeoutArg = strtol(optarg, &endptr, 10); | 
|  | 116 | if (*endptr != '\0' || timeoutArg <= 0) { | 
|  | 117 | fprintf(stderr, "Error: invalid timeout number: '%s'\n", optarg); | 
|  | 118 | return -1; | 
|  | 119 | } | 
|  | 120 | } | 
|  | 121 | break; | 
|  | 122 |  | 
|  | 123 | case 'l': | 
| Felipe Leme | bbfd2b8 | 2016-02-03 11:16:27 -0800 | [diff] [blame] | 124 | showListOnly = true; | 
| Thierry Strudel | 8b78b75 | 2016-03-22 10:25:44 -0700 | [diff] [blame] | 125 | break; | 
|  | 126 |  | 
|  | 127 | default: | 
|  | 128 | fprintf(stderr, "\n"); | 
|  | 129 | usage(); | 
|  | 130 | return -1; | 
| Felipe Leme | bbfd2b8 | 2016-02-03 11:16:27 -0800 | [diff] [blame] | 131 | } | 
| keunyoung | caad555 | 2013-06-13 15:08:51 -0700 | [diff] [blame] | 132 | } | 
| Thierry Strudel | 8b78b75 | 2016-03-22 10:25:44 -0700 | [diff] [blame] | 133 |  | 
|  | 134 | for (int i = optind; i < argc; i++) { | 
|  | 135 | if (skipServices) { | 
|  | 136 | skippedServices.add(String16(argv[i])); | 
|  | 137 | } else { | 
|  | 138 | if (i == optind) { | 
|  | 139 | services.add(String16(argv[i])); | 
|  | 140 | } else { | 
|  | 141 | args.add(String16(argv[i])); | 
| Felipe Leme | 859aef6 | 2016-02-03 12:17:10 -0800 | [diff] [blame] | 142 | } | 
|  | 143 | } | 
|  | 144 | } | 
| Thierry Strudel | 8b78b75 | 2016-03-22 10:25:44 -0700 | [diff] [blame] | 145 |  | 
|  | 146 | if ((skipServices && skippedServices.empty()) || | 
| Steven Moreland | 2c3cd83 | 2017-02-13 23:44:17 +0000 | [diff] [blame] | 147 | (showListOnly && (!services.empty() || !skippedServices.empty()))) { | 
| Thierry Strudel | 8b78b75 | 2016-03-22 10:25:44 -0700 | [diff] [blame] | 148 | usage(); | 
|  | 149 | return -1; | 
|  | 150 | } | 
|  | 151 |  | 
| Thierry Strudel | 159a832 | 2016-03-23 11:22:34 -0700 | [diff] [blame] | 152 | if (services.empty() || showListOnly) { | 
| Felipe Leme | 859aef6 | 2016-02-03 12:17:10 -0800 | [diff] [blame] | 153 | // gets all services | 
| Felipe Leme | 343175a | 2016-08-02 18:57:37 -0700 | [diff] [blame] | 154 | services = sm_->listServices(); | 
| Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 155 | services.sort(sort_func); | 
|  | 156 | args.add(String16("-a")); | 
| Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 157 | } | 
|  | 158 |  | 
|  | 159 | const size_t N = services.size(); | 
|  | 160 |  | 
|  | 161 | if (N > 1) { | 
|  | 162 | // first print a list of the current services | 
|  | 163 | aout << "Currently running services:" << endl; | 
| Felipe Leme | bbfd2b8 | 2016-02-03 11:16:27 -0800 | [diff] [blame] | 164 |  | 
| Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 165 | for (size_t i=0; i<N; i++) { | 
| Felipe Leme | 343175a | 2016-08-02 18:57:37 -0700 | [diff] [blame] | 166 | sp<IBinder> service = sm_->checkService(services[i]); | 
|  | 167 |  | 
|  | 168 | if (service != nullptr) { | 
| Felipe Leme | 859aef6 | 2016-02-03 12:17:10 -0800 | [diff] [blame] | 169 | bool skipped = IsSkipped(skippedServices, services[i]); | 
|  | 170 | aout << "  " << services[i] << (skipped ? " (skipped)" : "") << endl; | 
| Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 171 | } | 
|  | 172 | } | 
|  | 173 | } | 
|  | 174 |  | 
| keunyoung | caad555 | 2013-06-13 15:08:51 -0700 | [diff] [blame] | 175 | if (showListOnly) { | 
|  | 176 | return 0; | 
|  | 177 | } | 
|  | 178 |  | 
| Josh Gao | 4b8f6f9 | 2016-02-29 16:20:34 -0800 | [diff] [blame] | 179 | for (size_t i = 0; i < N; i++) { | 
| Chih-Hung Hsieh | cb057c2 | 2017-08-03 15:48:25 -0700 | [diff] [blame] | 180 | const String16& service_name = std::move(services[i]); | 
| Josh Gao | 4b8f6f9 | 2016-02-29 16:20:34 -0800 | [diff] [blame] | 181 | if (IsSkipped(skippedServices, service_name)) continue; | 
| Felipe Leme | 859aef6 | 2016-02-03 12:17:10 -0800 | [diff] [blame] | 182 |  | 
| Felipe Leme | 343175a | 2016-08-02 18:57:37 -0700 | [diff] [blame] | 183 | sp<IBinder> service = sm_->checkService(service_name); | 
|  | 184 | if (service != nullptr) { | 
| Josh Gao | 4b8f6f9 | 2016-02-29 16:20:34 -0800 | [diff] [blame] | 185 | int sfd[2]; | 
|  | 186 |  | 
| Josh Gao | 49f0a0c | 2016-03-04 13:12:29 -0800 | [diff] [blame] | 187 | if (pipe(sfd) != 0) { | 
|  | 188 | aerr << "Failed to create pipe to dump service info for " << service_name | 
| Josh Gao | 4b8f6f9 | 2016-02-29 16:20:34 -0800 | [diff] [blame] | 189 | << ": " << strerror(errno) << endl; | 
|  | 190 | continue; | 
|  | 191 | } | 
|  | 192 |  | 
|  | 193 | unique_fd local_end(sfd[0]); | 
|  | 194 | unique_fd remote_end(sfd[1]); | 
|  | 195 | sfd[0] = sfd[1] = -1; | 
|  | 196 |  | 
| Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 197 | if (N > 1) { | 
|  | 198 | aout << "------------------------------------------------------------" | 
|  | 199 | "-------------------" << endl; | 
| Josh Gao | 4b8f6f9 | 2016-02-29 16:20:34 -0800 | [diff] [blame] | 200 | aout << "DUMP OF SERVICE " << service_name << ":" << endl; | 
| Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 201 | } | 
| Josh Gao | 4b8f6f9 | 2016-02-29 16:20:34 -0800 | [diff] [blame] | 202 |  | 
|  | 203 | // dump blocks until completion, so spawn a thread.. | 
|  | 204 | std::thread dump_thread([=, remote_end { std::move(remote_end) }]() mutable { | 
|  | 205 | int err = service->dump(remote_end.get(), args); | 
|  | 206 |  | 
|  | 207 | // It'd be nice to be able to close the remote end of the socketpair before the dump | 
|  | 208 | // call returns, to terminate our reads if the other end closes their copy of the | 
|  | 209 | // file descriptor, but then hangs for some reason. There doesn't seem to be a good | 
|  | 210 | // way to do this, though. | 
| Josh Gao | 9656be1 | 2016-09-19 12:44:50 -0700 | [diff] [blame] | 211 | remote_end.reset(); | 
| Josh Gao | 4b8f6f9 | 2016-02-29 16:20:34 -0800 | [diff] [blame] | 212 |  | 
|  | 213 | if (err != 0) { | 
|  | 214 | aerr << "Error dumping service info: (" << strerror(err) << ") " << service_name | 
|  | 215 | << endl; | 
|  | 216 | } | 
|  | 217 | }); | 
|  | 218 |  | 
| Thierry Strudel | 8b78b75 | 2016-03-22 10:25:44 -0700 | [diff] [blame] | 219 | auto timeout = std::chrono::seconds(timeoutArg); | 
| mukesh agrawal | 2f1eb1c | 2016-06-08 18:16:36 -0700 | [diff] [blame] | 220 | auto start = std::chrono::steady_clock::now(); | 
|  | 221 | auto end = start + timeout; | 
| Josh Gao | 4b8f6f9 | 2016-02-29 16:20:34 -0800 | [diff] [blame] | 222 |  | 
|  | 223 | struct pollfd pfd = { | 
|  | 224 | .fd = local_end.get(), | 
|  | 225 | .events = POLLIN | 
|  | 226 | }; | 
|  | 227 |  | 
|  | 228 | bool timed_out = false; | 
|  | 229 | bool error = false; | 
|  | 230 | while (true) { | 
|  | 231 | // Wrap this in a lambda so that TEMP_FAILURE_RETRY recalculates the timeout. | 
|  | 232 | auto time_left_ms = [end]() { | 
|  | 233 | auto now = std::chrono::steady_clock::now(); | 
|  | 234 | auto diff = std::chrono::duration_cast<std::chrono::milliseconds>(end - now); | 
|  | 235 | return std::max(diff.count(), 0ll); | 
|  | 236 | }; | 
|  | 237 |  | 
|  | 238 | int rc = TEMP_FAILURE_RETRY(poll(&pfd, 1, time_left_ms())); | 
|  | 239 | if (rc < 0) { | 
|  | 240 | aerr << "Error in poll while dumping service " << service_name << " : " | 
|  | 241 | << strerror(errno) << endl; | 
|  | 242 | error = true; | 
|  | 243 | break; | 
|  | 244 | } else if (rc == 0) { | 
|  | 245 | timed_out = true; | 
|  | 246 | break; | 
|  | 247 | } | 
|  | 248 |  | 
|  | 249 | char buf[4096]; | 
|  | 250 | rc = TEMP_FAILURE_RETRY(read(local_end.get(), buf, sizeof(buf))); | 
|  | 251 | if (rc < 0) { | 
|  | 252 | aerr << "Failed to read while dumping service " << service_name << ": " | 
|  | 253 | << strerror(errno) << endl; | 
|  | 254 | error = true; | 
|  | 255 | break; | 
|  | 256 | } else if (rc == 0) { | 
|  | 257 | // EOF. | 
|  | 258 | break; | 
|  | 259 | } | 
|  | 260 |  | 
|  | 261 | if (!WriteFully(STDOUT_FILENO, buf, rc)) { | 
|  | 262 | aerr << "Failed to write while dumping service " << service_name << ": " | 
|  | 263 | << strerror(errno) << endl; | 
|  | 264 | error = true; | 
|  | 265 | break; | 
|  | 266 | } | 
|  | 267 | } | 
|  | 268 |  | 
|  | 269 | if (timed_out) { | 
| Felipe Leme | 343175a | 2016-08-02 18:57:37 -0700 | [diff] [blame] | 270 | aout << endl | 
|  | 271 | << "*** SERVICE '" << service_name << "' DUMP TIMEOUT (" << timeoutArg | 
|  | 272 | << "s) EXPIRED ***" << endl | 
|  | 273 | << endl; | 
| Josh Gao | 4b8f6f9 | 2016-02-29 16:20:34 -0800 | [diff] [blame] | 274 | } | 
|  | 275 |  | 
|  | 276 | if (timed_out || error) { | 
|  | 277 | dump_thread.detach(); | 
|  | 278 | } else { | 
|  | 279 | dump_thread.join(); | 
| Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 280 | } | 
| mukesh agrawal | 2f1eb1c | 2016-06-08 18:16:36 -0700 | [diff] [blame] | 281 |  | 
|  | 282 | if (N > 1) { | 
|  | 283 | std::chrono::duration<double> elapsed_seconds = | 
|  | 284 | std::chrono::steady_clock::now() - start; | 
| mukesh agrawal | bca287d | 2016-07-27 12:01:49 -0700 | [diff] [blame] | 285 | aout << StringPrintf("--------- %.3fs ", elapsed_seconds.count()).c_str() | 
| Kevin Rocard | 430e079 | 2017-08-14 20:40:24 -0700 | [diff] [blame] | 286 | << "was the duration of dumpsys " << service_name; | 
|  | 287 |  | 
|  | 288 | using std::chrono::system_clock; | 
|  | 289 | const auto finish = system_clock::to_time_t(system_clock::now()); | 
|  | 290 | std::tm finish_tm; | 
|  | 291 | localtime_r(&finish, &finish_tm); | 
|  | 292 | aout << ", ending at: " << std::put_time(&finish_tm, "%Y-%m-%d %H:%M:%S") | 
|  | 293 | << endl; | 
| mukesh agrawal | 2f1eb1c | 2016-06-08 18:16:36 -0700 | [diff] [blame] | 294 | } | 
| Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 295 | } else { | 
| Josh Gao | 4b8f6f9 | 2016-02-29 16:20:34 -0800 | [diff] [blame] | 296 | aerr << "Can't find service: " << service_name << endl; | 
| Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 297 | } | 
|  | 298 | } | 
|  | 299 |  | 
|  | 300 | return 0; | 
|  | 301 | } |