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