blob: 239a2d5e6960f74e5398e0ccf9350a772ef55369 [file] [log] [blame]
Colin Crossf45fa6b2012-03-26 12:38:26 -07001/*
Felipe Leme343175a2016-08-02 18:57:37 -07002 * Copyright (C) 2009 The Android Open Source Project
Colin Crossf45fa6b2012-03-26 12:38:26 -07003 *
Felipe Leme343175a2016-08-02 18:57:37 -07004 * 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 Crossf45fa6b2012-03-26 12:38:26 -070015 */
16
Josh Gao4b8f6f92016-02-29 16:20:34 -080017#include <algorithm>
18#include <chrono>
Kevin Rocard430e0792017-08-14 20:40:24 -070019#include <iomanip>
Josh Gao4b8f6f92016-02-29 16:20:34 -080020#include <thread>
21
22#include <android-base/file.h>
mukesh agrawal2f1eb1c2016-06-08 18:16:36 -070023#include <android-base/stringprintf.h>
Josh Gao4b8f6f92016-02-29 16:20:34 -080024#include <android-base/unique_fd.h>
Colin Crossf45fa6b2012-03-26 12:38:26 -070025#include <binder/Parcel.h>
26#include <binder/ProcessState.h>
Mathias Agopian002e1e52013-05-06 20:20:50 -070027#include <binder/TextOutput.h>
Vishnu Nair780b1282017-10-10 13:57:24 -070028#include <serviceutils/PriorityDumper.h>
Josh Gao4b8f6f92016-02-29 16:20:34 -080029#include <utils/Log.h>
Colin Crossf45fa6b2012-03-26 12:38:26 -070030#include <utils/Vector.h>
31
Josh Gao4b8f6f92016-02-29 16:20:34 -080032#include <fcntl.h>
Colin Crossf45fa6b2012-03-26 12:38:26 -070033#include <getopt.h>
Colin Crossf45fa6b2012-03-26 12:38:26 -070034#include <stdio.h>
Josh Gao4b8f6f92016-02-29 16:20:34 -080035#include <stdlib.h>
Colin Crossf45fa6b2012-03-26 12:38:26 -070036#include <string.h>
Josh Gao4b8f6f92016-02-29 16:20:34 -080037#include <sys/poll.h>
38#include <sys/socket.h>
Colin Crossf45fa6b2012-03-26 12:38:26 -070039#include <sys/time.h>
Josh Gao4b8f6f92016-02-29 16:20:34 -080040#include <sys/types.h>
41#include <unistd.h>
Colin Crossf45fa6b2012-03-26 12:38:26 -070042
Felipe Leme343175a2016-08-02 18:57:37 -070043#include "dumpsys.h"
44
Colin Crossf45fa6b2012-03-26 12:38:26 -070045using namespace android;
mukesh agrawal2f1eb1c2016-06-08 18:16:36 -070046using android::base::StringPrintf;
Josh Gao4b8f6f92016-02-29 16:20:34 -080047using android::base::unique_fd;
48using android::base::WriteFully;
Colin Crossf45fa6b2012-03-26 12:38:26 -070049
Steven Moreland2c3cd832017-02-13 23:44:17 +000050static int sort_func(const String16* lhs, const String16* rhs)
51{
Colin Crossf45fa6b2012-03-26 12:38:26 -070052 return lhs->compare(*rhs);
53}
54
Felipe Lemebbfd2b82016-02-03 11:16:27 -080055static void usage() {
56 fprintf(stderr,
Vishnu Nairf56042d2017-09-19 15:25:10 -070057 "usage: dumpsys\n"
Felipe Lemebbfd2b82016-02-03 11:16:27 -080058 " To dump all services.\n"
59 "or:\n"
Vishnu Nairf56042d2017-09-19 15:25:10 -070060 " dumpsys [-t TIMEOUT] [--priority LEVEL] [--help | -l | --skip SERVICES | "
61 "SERVICE [ARGS]]\n"
Felipe Lemebbfd2b82016-02-03 11:16:27 -080062 " --help: shows this help\n"
63 " -l: only list services, do not dump them\n"
Thierry Strudel8b78b752016-03-22 10:25:44 -070064 " -t TIMEOUT: TIMEOUT to use in seconds instead of default 10 seconds\n"
Vishnu Nairf56042d2017-09-19 15:25:10 -070065 " --priority LEVEL: filter services based on specified priority\n"
66 " LEVEL must be one of CRITICAL | HIGH | NORMAL\n"
Felipe Leme859aef62016-02-03 12:17:10 -080067 " --skip SERVICES: dumps all services but SERVICES (comma-separated list)\n"
68 " SERVICE [ARGS]: dumps only service SERVICE, optionally passing ARGS to it\n");
69}
70
Felipe Leme343175a2016-08-02 18:57:37 -070071static bool IsSkipped(const Vector<String16>& skipped, const String16& service) {
Felipe Leme859aef62016-02-03 12:17:10 -080072 for (const auto& candidate : skipped) {
73 if (candidate == service) {
74 return true;
75 }
76 }
77 return false;
Felipe Lemebbfd2b82016-02-03 11:16:27 -080078}
79
Vishnu Nair780b1282017-10-10 13:57:24 -070080static bool ConvertPriorityTypeToBitmask(String16& type, int& bitmask) {
81 if (type == PRIORITY_ARG_CRITICAL) {
82 bitmask = IServiceManager::DUMP_PRIORITY_CRITICAL;
83 return true;
84 }
85 if (type == PRIORITY_ARG_HIGH) {
86 bitmask = IServiceManager::DUMP_PRIORITY_HIGH;
87 return true;
88 }
89 if (type == PRIORITY_ARG_NORMAL) {
90 bitmask = IServiceManager::DUMP_PRIORITY_NORMAL;
91 return true;
92 }
93 return false;
94}
95
Felipe Leme343175a2016-08-02 18:57:37 -070096int Dumpsys::main(int argc, char* const argv[]) {
Colin Crossf45fa6b2012-03-26 12:38:26 -070097 Vector<String16> services;
98 Vector<String16> args;
Vishnu Nair780b1282017-10-10 13:57:24 -070099 String16 priorityType;
Felipe Leme859aef62016-02-03 12:17:10 -0800100 Vector<String16> skippedServices;
keunyoungcaad5552013-06-13 15:08:51 -0700101 bool showListOnly = false;
Thierry Strudel8b78b752016-03-22 10:25:44 -0700102 bool skipServices = false;
103 int timeoutArg = 10;
Vishnu Nairf56042d2017-09-19 15:25:10 -0700104 int dumpPriority = IServiceManager::DUMP_PRIORITY_ALL;
105 static struct option longOptions[] = {{"priority", required_argument, 0, 0},
106 {"skip", no_argument, 0, 0},
107 {"help", no_argument, 0, 0},
108 {0, 0, 0, 0}};
Thierry Strudel8b78b752016-03-22 10:25:44 -0700109
Felipe Leme343175a2016-08-02 18:57:37 -0700110 // Must reset optind, otherwise subsequent calls will fail (wouldn't happen on main.cpp, but
111 // happens on test cases).
112 optind = 1;
Thierry Strudel8b78b752016-03-22 10:25:44 -0700113 while (1) {
114 int c;
115 int optionIndex = 0;
116
117 c = getopt_long(argc, argv, "+t:l", longOptions, &optionIndex);
118
119 if (c == -1) {
120 break;
Felipe Lemebbfd2b82016-02-03 11:16:27 -0800121 }
Thierry Strudel8b78b752016-03-22 10:25:44 -0700122
123 switch (c) {
124 case 0:
125 if (!strcmp(longOptions[optionIndex].name, "skip")) {
126 skipServices = true;
127 } else if (!strcmp(longOptions[optionIndex].name, "help")) {
128 usage();
129 return 0;
Vishnu Nairf56042d2017-09-19 15:25:10 -0700130 } else if (!strcmp(longOptions[optionIndex].name, "priority")) {
Vishnu Nair780b1282017-10-10 13:57:24 -0700131 priorityType = String16(String8(optarg));
132 if (!ConvertPriorityTypeToBitmask(priorityType, dumpPriority)) {
Vishnu Nairf56042d2017-09-19 15:25:10 -0700133 fprintf(stderr, "\n");
134 usage();
135 return -1;
136 }
Thierry Strudel8b78b752016-03-22 10:25:44 -0700137 }
138 break;
139
140 case 't':
141 {
142 char *endptr;
143 timeoutArg = strtol(optarg, &endptr, 10);
144 if (*endptr != '\0' || timeoutArg <= 0) {
145 fprintf(stderr, "Error: invalid timeout number: '%s'\n", optarg);
146 return -1;
147 }
148 }
149 break;
150
151 case 'l':
Felipe Lemebbfd2b82016-02-03 11:16:27 -0800152 showListOnly = true;
Thierry Strudel8b78b752016-03-22 10:25:44 -0700153 break;
154
155 default:
156 fprintf(stderr, "\n");
157 usage();
158 return -1;
Felipe Lemebbfd2b82016-02-03 11:16:27 -0800159 }
keunyoungcaad5552013-06-13 15:08:51 -0700160 }
Thierry Strudel8b78b752016-03-22 10:25:44 -0700161
162 for (int i = optind; i < argc; i++) {
163 if (skipServices) {
164 skippedServices.add(String16(argv[i]));
165 } else {
166 if (i == optind) {
167 services.add(String16(argv[i]));
168 } else {
169 args.add(String16(argv[i]));
Felipe Leme859aef62016-02-03 12:17:10 -0800170 }
171 }
172 }
Thierry Strudel8b78b752016-03-22 10:25:44 -0700173
174 if ((skipServices && skippedServices.empty()) ||
Steven Moreland2c3cd832017-02-13 23:44:17 +0000175 (showListOnly && (!services.empty() || !skippedServices.empty()))) {
Thierry Strudel8b78b752016-03-22 10:25:44 -0700176 usage();
177 return -1;
178 }
179
Thierry Strudel159a8322016-03-23 11:22:34 -0700180 if (services.empty() || showListOnly) {
Felipe Leme859aef62016-02-03 12:17:10 -0800181 // gets all services
Vishnu Nairf56042d2017-09-19 15:25:10 -0700182 services = sm_->listServices(dumpPriority);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700183 services.sort(sort_func);
Vishnu Nair780b1282017-10-10 13:57:24 -0700184 if (dumpPriority != IServiceManager::DUMP_PRIORITY_ALL) {
185 args.insertAt(String16(PRIORITY_ARG), 0);
186 args.insertAt(priorityType, 1);
187 } else {
188 args.add(String16("-a"));
189 }
Colin Crossf45fa6b2012-03-26 12:38:26 -0700190 }
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 Lemebbfd2b82016-02-03 11:16:27 -0800197
Colin Crossf45fa6b2012-03-26 12:38:26 -0700198 for (size_t i=0; i<N; i++) {
Felipe Leme343175a2016-08-02 18:57:37 -0700199 sp<IBinder> service = sm_->checkService(services[i]);
200
201 if (service != nullptr) {
Felipe Leme859aef62016-02-03 12:17:10 -0800202 bool skipped = IsSkipped(skippedServices, services[i]);
203 aout << " " << services[i] << (skipped ? " (skipped)" : "") << endl;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700204 }
205 }
206 }
207
keunyoungcaad5552013-06-13 15:08:51 -0700208 if (showListOnly) {
209 return 0;
210 }
211
Josh Gao4b8f6f92016-02-29 16:20:34 -0800212 for (size_t i = 0; i < N; i++) {
Chih-Hung Hsiehcb057c22017-08-03 15:48:25 -0700213 const String16& service_name = std::move(services[i]);
Josh Gao4b8f6f92016-02-29 16:20:34 -0800214 if (IsSkipped(skippedServices, service_name)) continue;
Felipe Leme859aef62016-02-03 12:17:10 -0800215
Felipe Leme343175a2016-08-02 18:57:37 -0700216 sp<IBinder> service = sm_->checkService(service_name);
217 if (service != nullptr) {
Josh Gao4b8f6f92016-02-29 16:20:34 -0800218 int sfd[2];
219
Josh Gao49f0a0c2016-03-04 13:12:29 -0800220 if (pipe(sfd) != 0) {
221 aerr << "Failed to create pipe to dump service info for " << service_name
Josh Gao4b8f6f92016-02-29 16:20:34 -0800222 << ": " << 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 Crossf45fa6b2012-03-26 12:38:26 -0700230 if (N > 1) {
231 aout << "------------------------------------------------------------"
232 "-------------------" << endl;
Vishnu Nair780b1282017-10-10 13:57:24 -0700233 if (dumpPriority == IServiceManager::DUMP_PRIORITY_ALL) {
234 aout << "DUMP OF SERVICE " << service_name << ":" << endl;
235 } else {
236 aout << "DUMP OF SERVICE " << priorityType << " " << service_name << ":" << endl;
237 }
Colin Crossf45fa6b2012-03-26 12:38:26 -0700238 }
Josh Gao4b8f6f92016-02-29 16:20:34 -0800239
240 // dump blocks until completion, so spawn a thread..
241 std::thread dump_thread([=, remote_end { std::move(remote_end) }]() mutable {
242 int err = service->dump(remote_end.get(), args);
243
244 // It'd be nice to be able to close the remote end of the socketpair before the dump
245 // call returns, to terminate our reads if the other end closes their copy of the
246 // file descriptor, but then hangs for some reason. There doesn't seem to be a good
247 // way to do this, though.
Josh Gao9656be12016-09-19 12:44:50 -0700248 remote_end.reset();
Josh Gao4b8f6f92016-02-29 16:20:34 -0800249
250 if (err != 0) {
251 aerr << "Error dumping service info: (" << strerror(err) << ") " << service_name
252 << endl;
253 }
254 });
255
Thierry Strudel8b78b752016-03-22 10:25:44 -0700256 auto timeout = std::chrono::seconds(timeoutArg);
mukesh agrawal2f1eb1c2016-06-08 18:16:36 -0700257 auto start = std::chrono::steady_clock::now();
258 auto end = start + timeout;
Josh Gao4b8f6f92016-02-29 16:20:34 -0800259
260 struct pollfd pfd = {
261 .fd = local_end.get(),
262 .events = POLLIN
263 };
264
265 bool timed_out = false;
266 bool error = false;
267 while (true) {
268 // Wrap this in a lambda so that TEMP_FAILURE_RETRY recalculates the timeout.
269 auto time_left_ms = [end]() {
270 auto now = std::chrono::steady_clock::now();
271 auto diff = std::chrono::duration_cast<std::chrono::milliseconds>(end - now);
272 return std::max(diff.count(), 0ll);
273 };
274
275 int rc = TEMP_FAILURE_RETRY(poll(&pfd, 1, time_left_ms()));
276 if (rc < 0) {
277 aerr << "Error in poll while dumping service " << service_name << " : "
278 << strerror(errno) << endl;
279 error = true;
280 break;
281 } else if (rc == 0) {
282 timed_out = true;
283 break;
284 }
285
286 char buf[4096];
287 rc = TEMP_FAILURE_RETRY(read(local_end.get(), buf, sizeof(buf)));
288 if (rc < 0) {
289 aerr << "Failed to read while dumping service " << service_name << ": "
290 << strerror(errno) << endl;
291 error = true;
292 break;
293 } else if (rc == 0) {
294 // EOF.
295 break;
296 }
297
298 if (!WriteFully(STDOUT_FILENO, buf, rc)) {
299 aerr << "Failed to write while dumping service " << service_name << ": "
300 << strerror(errno) << endl;
301 error = true;
302 break;
303 }
304 }
305
306 if (timed_out) {
Felipe Leme343175a2016-08-02 18:57:37 -0700307 aout << endl
308 << "*** SERVICE '" << service_name << "' DUMP TIMEOUT (" << timeoutArg
309 << "s) EXPIRED ***" << endl
310 << endl;
Josh Gao4b8f6f92016-02-29 16:20:34 -0800311 }
312
313 if (timed_out || error) {
314 dump_thread.detach();
315 } else {
316 dump_thread.join();
Colin Crossf45fa6b2012-03-26 12:38:26 -0700317 }
mukesh agrawal2f1eb1c2016-06-08 18:16:36 -0700318
319 if (N > 1) {
320 std::chrono::duration<double> elapsed_seconds =
321 std::chrono::steady_clock::now() - start;
mukesh agrawalbca287d2016-07-27 12:01:49 -0700322 aout << StringPrintf("--------- %.3fs ", elapsed_seconds.count()).c_str()
Kevin Rocard430e0792017-08-14 20:40:24 -0700323 << "was the duration of dumpsys " << service_name;
324
325 using std::chrono::system_clock;
326 const auto finish = system_clock::to_time_t(system_clock::now());
327 std::tm finish_tm;
328 localtime_r(&finish, &finish_tm);
329 aout << ", ending at: " << std::put_time(&finish_tm, "%Y-%m-%d %H:%M:%S")
330 << endl;
mukesh agrawal2f1eb1c2016-06-08 18:16:36 -0700331 }
Colin Crossf45fa6b2012-03-26 12:38:26 -0700332 } else {
Josh Gao4b8f6f92016-02-29 16:20:34 -0800333 aerr << "Can't find service: " << service_name << endl;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700334 }
335 }
336
337 return 0;
338}