blob: c36ab0872a6c597e178ff219266d631e82aeed3b [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 Nair6a408532017-10-24 09:11:27 -070065 " --proto: filter services that support dumping data in proto format. Dumps"
66 " will be in proto format.\n"
Vishnu Nairf56042d2017-09-19 15:25:10 -070067 " --priority LEVEL: filter services based on specified priority\n"
68 " LEVEL must be one of CRITICAL | HIGH | NORMAL\n"
Felipe Leme859aef62016-02-03 12:17:10 -080069 " --skip SERVICES: dumps all services but SERVICES (comma-separated list)\n"
70 " SERVICE [ARGS]: dumps only service SERVICE, optionally passing ARGS to it\n");
71}
72
Felipe Leme343175a2016-08-02 18:57:37 -070073static bool IsSkipped(const Vector<String16>& skipped, const String16& service) {
Felipe Leme859aef62016-02-03 12:17:10 -080074 for (const auto& candidate : skipped) {
75 if (candidate == service) {
76 return true;
77 }
78 }
79 return false;
Felipe Lemebbfd2b82016-02-03 11:16:27 -080080}
81
Vishnu Nair6a408532017-10-24 09:11:27 -070082static bool ConvertPriorityTypeToBitmask(const String16& type, int& bitmask) {
83 if (type == PriorityDumper::PRIORITY_ARG_CRITICAL) {
84 bitmask = IServiceManager::DUMP_FLAG_PRIORITY_CRITICAL;
Vishnu Nair780b1282017-10-10 13:57:24 -070085 return true;
86 }
Vishnu Nair6a408532017-10-24 09:11:27 -070087 if (type == PriorityDumper::PRIORITY_ARG_HIGH) {
88 bitmask = IServiceManager::DUMP_FLAG_PRIORITY_HIGH;
Vishnu Nair780b1282017-10-10 13:57:24 -070089 return true;
90 }
Vishnu Nair6a408532017-10-24 09:11:27 -070091 if (type == PriorityDumper::PRIORITY_ARG_NORMAL) {
92 bitmask = IServiceManager::DUMP_FLAG_PRIORITY_NORMAL;
Vishnu Nair780b1282017-10-10 13:57:24 -070093 return true;
94 }
95 return false;
96}
97
Felipe Leme343175a2016-08-02 18:57:37 -070098int Dumpsys::main(int argc, char* const argv[]) {
Colin Crossf45fa6b2012-03-26 12:38:26 -070099 Vector<String16> services;
100 Vector<String16> args;
Vishnu Nair780b1282017-10-10 13:57:24 -0700101 String16 priorityType;
Felipe Leme859aef62016-02-03 12:17:10 -0800102 Vector<String16> skippedServices;
Vishnu Nair6a408532017-10-24 09:11:27 -0700103 Vector<String16> protoServices;
keunyoungcaad5552013-06-13 15:08:51 -0700104 bool showListOnly = false;
Thierry Strudel8b78b752016-03-22 10:25:44 -0700105 bool skipServices = false;
Vishnu Nair6a408532017-10-24 09:11:27 -0700106 bool filterByProto = false;
Thierry Strudel8b78b752016-03-22 10:25:44 -0700107 int timeoutArg = 10;
Vishnu Nair6a408532017-10-24 09:11:27 -0700108 int dumpPriorityFlags = IServiceManager::DUMP_FLAG_PRIORITY_ALL;
Vishnu Nairf56042d2017-09-19 15:25:10 -0700109 static struct option longOptions[] = {{"priority", required_argument, 0, 0},
Vishnu Nair6a408532017-10-24 09:11:27 -0700110 {"proto", no_argument, 0, 0},
Vishnu Nairf56042d2017-09-19 15:25:10 -0700111 {"skip", no_argument, 0, 0},
112 {"help", no_argument, 0, 0},
113 {0, 0, 0, 0}};
Thierry Strudel8b78b752016-03-22 10:25:44 -0700114
Felipe Leme343175a2016-08-02 18:57:37 -0700115 // Must reset optind, otherwise subsequent calls will fail (wouldn't happen on main.cpp, but
116 // happens on test cases).
117 optind = 1;
Thierry Strudel8b78b752016-03-22 10:25:44 -0700118 while (1) {
119 int c;
120 int optionIndex = 0;
121
122 c = getopt_long(argc, argv, "+t:l", longOptions, &optionIndex);
123
124 if (c == -1) {
125 break;
Felipe Lemebbfd2b82016-02-03 11:16:27 -0800126 }
Thierry Strudel8b78b752016-03-22 10:25:44 -0700127
128 switch (c) {
129 case 0:
130 if (!strcmp(longOptions[optionIndex].name, "skip")) {
131 skipServices = true;
Vishnu Nair6a408532017-10-24 09:11:27 -0700132 } else if (!strcmp(longOptions[optionIndex].name, "proto")) {
133 filterByProto = true;
Thierry Strudel8b78b752016-03-22 10:25:44 -0700134 } else if (!strcmp(longOptions[optionIndex].name, "help")) {
135 usage();
136 return 0;
Vishnu Nairf56042d2017-09-19 15:25:10 -0700137 } else if (!strcmp(longOptions[optionIndex].name, "priority")) {
Vishnu Nair780b1282017-10-10 13:57:24 -0700138 priorityType = String16(String8(optarg));
Vishnu Nair6a408532017-10-24 09:11:27 -0700139 if (!ConvertPriorityTypeToBitmask(priorityType, dumpPriorityFlags)) {
Vishnu Nairf56042d2017-09-19 15:25:10 -0700140 fprintf(stderr, "\n");
141 usage();
142 return -1;
143 }
Thierry Strudel8b78b752016-03-22 10:25:44 -0700144 }
145 break;
146
147 case 't':
148 {
149 char *endptr;
150 timeoutArg = strtol(optarg, &endptr, 10);
151 if (*endptr != '\0' || timeoutArg <= 0) {
152 fprintf(stderr, "Error: invalid timeout number: '%s'\n", optarg);
153 return -1;
154 }
155 }
156 break;
157
158 case 'l':
Felipe Lemebbfd2b82016-02-03 11:16:27 -0800159 showListOnly = true;
Thierry Strudel8b78b752016-03-22 10:25:44 -0700160 break;
161
162 default:
163 fprintf(stderr, "\n");
164 usage();
165 return -1;
Felipe Lemebbfd2b82016-02-03 11:16:27 -0800166 }
keunyoungcaad5552013-06-13 15:08:51 -0700167 }
Thierry Strudel8b78b752016-03-22 10:25:44 -0700168
169 for (int i = optind; i < argc; i++) {
170 if (skipServices) {
171 skippedServices.add(String16(argv[i]));
172 } else {
173 if (i == optind) {
174 services.add(String16(argv[i]));
175 } else {
176 args.add(String16(argv[i]));
Felipe Leme859aef62016-02-03 12:17:10 -0800177 }
178 }
179 }
Thierry Strudel8b78b752016-03-22 10:25:44 -0700180
181 if ((skipServices && skippedServices.empty()) ||
Steven Moreland2c3cd832017-02-13 23:44:17 +0000182 (showListOnly && (!services.empty() || !skippedServices.empty()))) {
Thierry Strudel8b78b752016-03-22 10:25:44 -0700183 usage();
184 return -1;
185 }
186
Thierry Strudel159a8322016-03-23 11:22:34 -0700187 if (services.empty() || showListOnly) {
Felipe Leme859aef62016-02-03 12:17:10 -0800188 // gets all services
Vishnu Nair6a408532017-10-24 09:11:27 -0700189 services = sm_->listServices(dumpPriorityFlags);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700190 services.sort(sort_func);
Vishnu Nair6a408532017-10-24 09:11:27 -0700191 if (filterByProto) {
192 protoServices = sm_->listServices(IServiceManager::DUMP_FLAG_PROTO);
193 protoServices.sort(sort_func);
194 Vector<String16> intersection;
195 std::set_intersection(services.begin(), services.end(), protoServices.begin(),
196 protoServices.end(), std::back_inserter(intersection));
197 services = std::move(intersection);
198 args.insertAt(String16(PriorityDumper::PROTO_ARG), 0);
199 }
200 if (dumpPriorityFlags != IServiceManager::DUMP_FLAG_PRIORITY_ALL) {
201 args.insertAt(String16(PriorityDumper::PRIORITY_ARG), 0);
Vishnu Nair780b1282017-10-10 13:57:24 -0700202 args.insertAt(priorityType, 1);
203 } else {
204 args.add(String16("-a"));
205 }
Colin Crossf45fa6b2012-03-26 12:38:26 -0700206 }
207
208 const size_t N = services.size();
209
210 if (N > 1) {
211 // first print a list of the current services
212 aout << "Currently running services:" << endl;
Felipe Lemebbfd2b82016-02-03 11:16:27 -0800213
Colin Crossf45fa6b2012-03-26 12:38:26 -0700214 for (size_t i=0; i<N; i++) {
Felipe Leme343175a2016-08-02 18:57:37 -0700215 sp<IBinder> service = sm_->checkService(services[i]);
216
217 if (service != nullptr) {
Felipe Leme859aef62016-02-03 12:17:10 -0800218 bool skipped = IsSkipped(skippedServices, services[i]);
219 aout << " " << services[i] << (skipped ? " (skipped)" : "") << endl;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700220 }
221 }
222 }
223
keunyoungcaad5552013-06-13 15:08:51 -0700224 if (showListOnly) {
225 return 0;
226 }
227
Josh Gao4b8f6f92016-02-29 16:20:34 -0800228 for (size_t i = 0; i < N; i++) {
Chih-Hung Hsiehcb057c22017-08-03 15:48:25 -0700229 const String16& service_name = std::move(services[i]);
Josh Gao4b8f6f92016-02-29 16:20:34 -0800230 if (IsSkipped(skippedServices, service_name)) continue;
Felipe Leme859aef62016-02-03 12:17:10 -0800231
Felipe Leme343175a2016-08-02 18:57:37 -0700232 sp<IBinder> service = sm_->checkService(service_name);
233 if (service != nullptr) {
Josh Gao4b8f6f92016-02-29 16:20:34 -0800234 int sfd[2];
235
Josh Gao49f0a0c2016-03-04 13:12:29 -0800236 if (pipe(sfd) != 0) {
237 aerr << "Failed to create pipe to dump service info for " << service_name
Josh Gao4b8f6f92016-02-29 16:20:34 -0800238 << ": " << strerror(errno) << endl;
239 continue;
240 }
241
242 unique_fd local_end(sfd[0]);
243 unique_fd remote_end(sfd[1]);
244 sfd[0] = sfd[1] = -1;
245
Colin Crossf45fa6b2012-03-26 12:38:26 -0700246 if (N > 1) {
247 aout << "------------------------------------------------------------"
248 "-------------------" << endl;
Vishnu Nair6a408532017-10-24 09:11:27 -0700249 if (dumpPriorityFlags == IServiceManager::DUMP_FLAG_PRIORITY_ALL) {
Vishnu Nair780b1282017-10-10 13:57:24 -0700250 aout << "DUMP OF SERVICE " << service_name << ":" << endl;
251 } else {
252 aout << "DUMP OF SERVICE " << priorityType << " " << service_name << ":" << endl;
253 }
Colin Crossf45fa6b2012-03-26 12:38:26 -0700254 }
Josh Gao4b8f6f92016-02-29 16:20:34 -0800255
256 // dump blocks until completion, so spawn a thread..
257 std::thread dump_thread([=, remote_end { std::move(remote_end) }]() mutable {
258 int err = service->dump(remote_end.get(), args);
259
260 // It'd be nice to be able to close the remote end of the socketpair before the dump
261 // call returns, to terminate our reads if the other end closes their copy of the
262 // file descriptor, but then hangs for some reason. There doesn't seem to be a good
263 // way to do this, though.
Josh Gao9656be12016-09-19 12:44:50 -0700264 remote_end.reset();
Josh Gao4b8f6f92016-02-29 16:20:34 -0800265
266 if (err != 0) {
267 aerr << "Error dumping service info: (" << strerror(err) << ") " << service_name
268 << endl;
269 }
270 });
271
Thierry Strudel8b78b752016-03-22 10:25:44 -0700272 auto timeout = std::chrono::seconds(timeoutArg);
mukesh agrawal2f1eb1c2016-06-08 18:16:36 -0700273 auto start = std::chrono::steady_clock::now();
274 auto end = start + timeout;
Josh Gao4b8f6f92016-02-29 16:20:34 -0800275
276 struct pollfd pfd = {
277 .fd = local_end.get(),
278 .events = POLLIN
279 };
280
281 bool timed_out = false;
282 bool error = false;
283 while (true) {
284 // Wrap this in a lambda so that TEMP_FAILURE_RETRY recalculates the timeout.
285 auto time_left_ms = [end]() {
286 auto now = std::chrono::steady_clock::now();
287 auto diff = std::chrono::duration_cast<std::chrono::milliseconds>(end - now);
288 return std::max(diff.count(), 0ll);
289 };
290
291 int rc = TEMP_FAILURE_RETRY(poll(&pfd, 1, time_left_ms()));
292 if (rc < 0) {
293 aerr << "Error in poll while dumping service " << service_name << " : "
294 << strerror(errno) << endl;
295 error = true;
296 break;
297 } else if (rc == 0) {
298 timed_out = true;
299 break;
300 }
301
302 char buf[4096];
303 rc = TEMP_FAILURE_RETRY(read(local_end.get(), buf, sizeof(buf)));
304 if (rc < 0) {
305 aerr << "Failed to read while dumping service " << service_name << ": "
306 << strerror(errno) << endl;
307 error = true;
308 break;
309 } else if (rc == 0) {
310 // EOF.
311 break;
312 }
313
314 if (!WriteFully(STDOUT_FILENO, buf, rc)) {
315 aerr << "Failed to write while dumping service " << service_name << ": "
316 << strerror(errno) << endl;
317 error = true;
318 break;
319 }
320 }
321
322 if (timed_out) {
Felipe Leme343175a2016-08-02 18:57:37 -0700323 aout << endl
324 << "*** SERVICE '" << service_name << "' DUMP TIMEOUT (" << timeoutArg
325 << "s) EXPIRED ***" << endl
326 << endl;
Josh Gao4b8f6f92016-02-29 16:20:34 -0800327 }
328
329 if (timed_out || error) {
330 dump_thread.detach();
331 } else {
332 dump_thread.join();
Colin Crossf45fa6b2012-03-26 12:38:26 -0700333 }
mukesh agrawal2f1eb1c2016-06-08 18:16:36 -0700334
335 if (N > 1) {
336 std::chrono::duration<double> elapsed_seconds =
337 std::chrono::steady_clock::now() - start;
mukesh agrawalbca287d2016-07-27 12:01:49 -0700338 aout << StringPrintf("--------- %.3fs ", elapsed_seconds.count()).c_str()
Kevin Rocard430e0792017-08-14 20:40:24 -0700339 << "was the duration of dumpsys " << service_name;
340
341 using std::chrono::system_clock;
342 const auto finish = system_clock::to_time_t(system_clock::now());
343 std::tm finish_tm;
344 localtime_r(&finish, &finish_tm);
345 aout << ", ending at: " << std::put_time(&finish_tm, "%Y-%m-%d %H:%M:%S")
346 << endl;
mukesh agrawal2f1eb1c2016-06-08 18:16:36 -0700347 }
Colin Crossf45fa6b2012-03-26 12:38:26 -0700348 } else {
Josh Gao4b8f6f92016-02-29 16:20:34 -0800349 aerr << "Can't find service: " << service_name << endl;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700350 }
351 }
352
353 return 0;
354}