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