blob: 83a52b85a32eccfd92a9a026743f7bdee2947842 [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>
Steven Morelande5a6a872021-05-19 22:11:37 +000027#include <binder/Stability.h>
Mathias Agopian002e1e52013-05-06 20:20:50 -070028#include <binder/TextOutput.h>
Devin Moorecfeeda42020-12-08 12:50:58 -080029#include <binderdebug/BinderDebug.h>
Vishnu Nair780b1282017-10-10 13:57:24 -070030#include <serviceutils/PriorityDumper.h>
Josh Gao4b8f6f92016-02-29 16:20:34 -080031#include <utils/Log.h>
Colin Crossf45fa6b2012-03-26 12:38:26 -070032#include <utils/Vector.h>
33
Steven Moreland3657ee52020-01-31 12:09:15 -080034#include <iostream>
Josh Gao4b8f6f92016-02-29 16:20:34 -080035#include <fcntl.h>
Colin Crossf45fa6b2012-03-26 12:38:26 -070036#include <getopt.h>
Colin Crossf45fa6b2012-03-26 12:38:26 -070037#include <stdio.h>
Josh Gao4b8f6f92016-02-29 16:20:34 -080038#include <stdlib.h>
Colin Crossf45fa6b2012-03-26 12:38:26 -070039#include <string.h>
Josh Gao4b8f6f92016-02-29 16:20:34 -080040#include <sys/poll.h>
41#include <sys/socket.h>
Colin Crossf45fa6b2012-03-26 12:38:26 -070042#include <sys/time.h>
Josh Gao4b8f6f92016-02-29 16:20:34 -080043#include <sys/types.h>
44#include <unistd.h>
Colin Crossf45fa6b2012-03-26 12:38:26 -070045
Felipe Leme343175a2016-08-02 18:57:37 -070046#include "dumpsys.h"
47
Colin Crossf45fa6b2012-03-26 12:38:26 -070048using namespace android;
Vishnu Naire4f61742017-12-21 08:30:28 -080049using ::android::base::StringAppendF;
50using ::android::base::StringPrintf;
51using ::android::base::unique_fd;
52using ::android::base::WriteFully;
53using ::android::base::WriteStringToFd;
Colin Crossf45fa6b2012-03-26 12:38:26 -070054
Steven Moreland2c3cd832017-02-13 23:44:17 +000055static int sort_func(const String16* lhs, const String16* rhs)
56{
Colin Crossf45fa6b2012-03-26 12:38:26 -070057 return lhs->compare(*rhs);
58}
59
Felipe Lemebbfd2b82016-02-03 11:16:27 -080060static void usage() {
61 fprintf(stderr,
Vishnu Nairf56042d2017-09-19 15:25:10 -070062 "usage: dumpsys\n"
Felipe Lemebbfd2b82016-02-03 11:16:27 -080063 " To dump all services.\n"
64 "or:\n"
Devin Moorecfeeda42020-12-08 12:50:58 -080065 " dumpsys [-t TIMEOUT] [--priority LEVEL] [--pid] [--thread] [--help | -l | "
66 "--skip SERVICES "
Steven Moreland5a30d342019-10-08 13:53:28 -070067 "| SERVICE [ARGS]]\n"
Felipe Lemebbfd2b82016-02-03 11:16:27 -080068 " --help: shows this help\n"
69 " -l: only list services, do not dump them\n"
Vishnu Nair6921f802017-11-22 09:17:23 -080070 " -t TIMEOUT_SEC: TIMEOUT to use in seconds instead of default 10 seconds\n"
71 " -T TIMEOUT_MS: TIMEOUT to use in milliseconds instead of default 10 seconds\n"
Steven Moreland5a30d342019-10-08 13:53:28 -070072 " --pid: dump PID instead of usual dump\n"
Ng Zhi Ancf2d01b2018-09-14 09:47:43 -070073 " --proto: filter services that support dumping data in proto format. Dumps\n"
Vishnu Nair6a408532017-10-24 09:11:27 -070074 " will be in proto format.\n"
Vishnu Nairf56042d2017-09-19 15:25:10 -070075 " --priority LEVEL: filter services based on specified priority\n"
76 " LEVEL must be one of CRITICAL | HIGH | NORMAL\n"
Felipe Leme859aef62016-02-03 12:17:10 -080077 " --skip SERVICES: dumps all services but SERVICES (comma-separated list)\n"
Steven Morelande5a6a872021-05-19 22:11:37 +000078 " --stability: dump binder stability information instead of usual dump\n"
79 " --thread: dump thread usage instead of usual dump\n"
Felipe Leme859aef62016-02-03 12:17:10 -080080 " SERVICE [ARGS]: dumps only service SERVICE, optionally passing ARGS to it\n");
81}
82
Felipe Leme343175a2016-08-02 18:57:37 -070083static bool IsSkipped(const Vector<String16>& skipped, const String16& service) {
Felipe Leme859aef62016-02-03 12:17:10 -080084 for (const auto& candidate : skipped) {
85 if (candidate == service) {
86 return true;
87 }
88 }
89 return false;
Felipe Lemebbfd2b82016-02-03 11:16:27 -080090}
91
Vishnu Nair6a408532017-10-24 09:11:27 -070092static bool ConvertPriorityTypeToBitmask(const String16& type, int& bitmask) {
93 if (type == PriorityDumper::PRIORITY_ARG_CRITICAL) {
94 bitmask = IServiceManager::DUMP_FLAG_PRIORITY_CRITICAL;
Vishnu Nair780b1282017-10-10 13:57:24 -070095 return true;
96 }
Vishnu Nair6a408532017-10-24 09:11:27 -070097 if (type == PriorityDumper::PRIORITY_ARG_HIGH) {
98 bitmask = IServiceManager::DUMP_FLAG_PRIORITY_HIGH;
Vishnu Nair780b1282017-10-10 13:57:24 -070099 return true;
100 }
Vishnu Nair6a408532017-10-24 09:11:27 -0700101 if (type == PriorityDumper::PRIORITY_ARG_NORMAL) {
102 bitmask = IServiceManager::DUMP_FLAG_PRIORITY_NORMAL;
Vishnu Nair780b1282017-10-10 13:57:24 -0700103 return true;
104 }
105 return false;
106}
107
Vishnu Naire4f61742017-12-21 08:30:28 -0800108String16 ConvertBitmaskToPriorityType(int bitmask) {
109 if (bitmask == IServiceManager::DUMP_FLAG_PRIORITY_CRITICAL) {
110 return String16(PriorityDumper::PRIORITY_ARG_CRITICAL);
111 }
112 if (bitmask == IServiceManager::DUMP_FLAG_PRIORITY_HIGH) {
113 return String16(PriorityDumper::PRIORITY_ARG_HIGH);
114 }
115 if (bitmask == IServiceManager::DUMP_FLAG_PRIORITY_NORMAL) {
116 return String16(PriorityDumper::PRIORITY_ARG_NORMAL);
117 }
118 return String16("");
119}
120
Felipe Leme343175a2016-08-02 18:57:37 -0700121int Dumpsys::main(int argc, char* const argv[]) {
Colin Crossf45fa6b2012-03-26 12:38:26 -0700122 Vector<String16> services;
123 Vector<String16> args;
Vishnu Nair780b1282017-10-10 13:57:24 -0700124 String16 priorityType;
Felipe Leme859aef62016-02-03 12:17:10 -0800125 Vector<String16> skippedServices;
Vishnu Nair6a408532017-10-24 09:11:27 -0700126 Vector<String16> protoServices;
keunyoungcaad5552013-06-13 15:08:51 -0700127 bool showListOnly = false;
Thierry Strudel8b78b752016-03-22 10:25:44 -0700128 bool skipServices = false;
Vishnu Naire4f61742017-12-21 08:30:28 -0800129 bool asProto = false;
Steven Moreland5a30d342019-10-08 13:53:28 -0700130 Type type = Type::DUMP;
Vishnu Nair6921f802017-11-22 09:17:23 -0800131 int timeoutArgMs = 10000;
Vishnu Naire4f61742017-12-21 08:30:28 -0800132 int priorityFlags = IServiceManager::DUMP_FLAG_PRIORITY_ALL;
Steven Morelande5a6a872021-05-19 22:11:37 +0000133 static struct option longOptions[] = {{"help", no_argument, 0, 0},
Devin Moorecfeeda42020-12-08 12:50:58 -0800134 {"pid", no_argument, 0, 0},
Steven Moreland5a30d342019-10-08 13:53:28 -0700135 {"priority", required_argument, 0, 0},
Vishnu Nair6a408532017-10-24 09:11:27 -0700136 {"proto", no_argument, 0, 0},
Vishnu Nairf56042d2017-09-19 15:25:10 -0700137 {"skip", no_argument, 0, 0},
Steven Morelande5a6a872021-05-19 22:11:37 +0000138 {"stability", no_argument, 0, 0},
139 {"thread", no_argument, 0, 0},
Vishnu Nairf56042d2017-09-19 15:25:10 -0700140 {0, 0, 0, 0}};
Thierry Strudel8b78b752016-03-22 10:25:44 -0700141
Felipe Leme343175a2016-08-02 18:57:37 -0700142 // Must reset optind, otherwise subsequent calls will fail (wouldn't happen on main.cpp, but
143 // happens on test cases).
144 optind = 1;
Thierry Strudel8b78b752016-03-22 10:25:44 -0700145 while (1) {
146 int c;
147 int optionIndex = 0;
148
Vishnu Nair6921f802017-11-22 09:17:23 -0800149 c = getopt_long(argc, argv, "+t:T:l", longOptions, &optionIndex);
Thierry Strudel8b78b752016-03-22 10:25:44 -0700150
151 if (c == -1) {
152 break;
Felipe Lemebbfd2b82016-02-03 11:16:27 -0800153 }
Thierry Strudel8b78b752016-03-22 10:25:44 -0700154
155 switch (c) {
156 case 0:
157 if (!strcmp(longOptions[optionIndex].name, "skip")) {
158 skipServices = true;
Vishnu Nair6a408532017-10-24 09:11:27 -0700159 } else if (!strcmp(longOptions[optionIndex].name, "proto")) {
Vishnu Naire4f61742017-12-21 08:30:28 -0800160 asProto = true;
Thierry Strudel8b78b752016-03-22 10:25:44 -0700161 } else if (!strcmp(longOptions[optionIndex].name, "help")) {
162 usage();
163 return 0;
Vishnu Nairf56042d2017-09-19 15:25:10 -0700164 } else if (!strcmp(longOptions[optionIndex].name, "priority")) {
Vishnu Nair780b1282017-10-10 13:57:24 -0700165 priorityType = String16(String8(optarg));
Vishnu Naire4f61742017-12-21 08:30:28 -0800166 if (!ConvertPriorityTypeToBitmask(priorityType, priorityFlags)) {
Vishnu Nairf56042d2017-09-19 15:25:10 -0700167 fprintf(stderr, "\n");
168 usage();
169 return -1;
170 }
Steven Moreland5a30d342019-10-08 13:53:28 -0700171 } else if (!strcmp(longOptions[optionIndex].name, "pid")) {
172 type = Type::PID;
Steven Morelande5a6a872021-05-19 22:11:37 +0000173 } else if (!strcmp(longOptions[optionIndex].name, "stability")) {
174 type = Type::STABILITY;
Devin Moorecfeeda42020-12-08 12:50:58 -0800175 } else if (!strcmp(longOptions[optionIndex].name, "thread")) {
176 type = Type::THREAD;
Thierry Strudel8b78b752016-03-22 10:25:44 -0700177 }
178 break;
179
180 case 't':
181 {
Vishnu Nair6921f802017-11-22 09:17:23 -0800182 char* endptr;
183 timeoutArgMs = strtol(optarg, &endptr, 10);
184 timeoutArgMs = timeoutArgMs * 1000;
185 if (*endptr != '\0' || timeoutArgMs <= 0) {
186 fprintf(stderr, "Error: invalid timeout(seconds) number: '%s'\n", optarg);
187 return -1;
188 }
189 }
190 break;
191
192 case 'T':
193 {
194 char* endptr;
195 timeoutArgMs = strtol(optarg, &endptr, 10);
196 if (*endptr != '\0' || timeoutArgMs <= 0) {
197 fprintf(stderr, "Error: invalid timeout(milliseconds) number: '%s'\n", optarg);
Thierry Strudel8b78b752016-03-22 10:25:44 -0700198 return -1;
199 }
200 }
201 break;
202
203 case 'l':
Felipe Lemebbfd2b82016-02-03 11:16:27 -0800204 showListOnly = true;
Thierry Strudel8b78b752016-03-22 10:25:44 -0700205 break;
206
207 default:
208 fprintf(stderr, "\n");
209 usage();
210 return -1;
Felipe Lemebbfd2b82016-02-03 11:16:27 -0800211 }
keunyoungcaad5552013-06-13 15:08:51 -0700212 }
Thierry Strudel8b78b752016-03-22 10:25:44 -0700213
214 for (int i = optind; i < argc; i++) {
215 if (skipServices) {
216 skippedServices.add(String16(argv[i]));
217 } else {
218 if (i == optind) {
219 services.add(String16(argv[i]));
220 } else {
Riddle Hsuc66abb22019-09-27 16:10:21 +0800221 const String16 arg(argv[i]);
222 args.add(arg);
223 // For backward compatible, if the proto argument is passed to the service, the
224 // dump request is also considered to use proto.
225 if (!asProto && !arg.compare(String16(PriorityDumper::PROTO_ARG))) {
226 asProto = true;
227 }
Felipe Leme859aef62016-02-03 12:17:10 -0800228 }
229 }
230 }
Thierry Strudel8b78b752016-03-22 10:25:44 -0700231
232 if ((skipServices && skippedServices.empty()) ||
Steven Moreland2c3cd832017-02-13 23:44:17 +0000233 (showListOnly && (!services.empty() || !skippedServices.empty()))) {
Thierry Strudel8b78b752016-03-22 10:25:44 -0700234 usage();
235 return -1;
236 }
237
Thierry Strudel159a8322016-03-23 11:22:34 -0700238 if (services.empty() || showListOnly) {
Vishnu Naire4f61742017-12-21 08:30:28 -0800239 services = listServices(priorityFlags, asProto);
240 setServiceArgs(args, asProto, priorityFlags);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700241 }
242
243 const size_t N = services.size();
Steven Moreland31dac352020-03-05 09:46:45 -0800244 if (N > 1 || showListOnly) {
Colin Crossf45fa6b2012-03-26 12:38:26 -0700245 // first print a list of the current services
Steven Moreland3657ee52020-01-31 12:09:15 -0800246 std::cout << "Currently running services:" << std::endl;
Felipe Lemebbfd2b82016-02-03 11:16:27 -0800247
Colin Crossf45fa6b2012-03-26 12:38:26 -0700248 for (size_t i=0; i<N; i++) {
Felipe Leme343175a2016-08-02 18:57:37 -0700249 sp<IBinder> service = sm_->checkService(services[i]);
250
251 if (service != nullptr) {
Felipe Leme859aef62016-02-03 12:17:10 -0800252 bool skipped = IsSkipped(skippedServices, services[i]);
Steven Moreland3657ee52020-01-31 12:09:15 -0800253 std::cout << " " << services[i] << (skipped ? " (skipped)" : "") << std::endl;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700254 }
255 }
256 }
257
keunyoungcaad5552013-06-13 15:08:51 -0700258 if (showListOnly) {
259 return 0;
260 }
261
Josh Gao4b8f6f92016-02-29 16:20:34 -0800262 for (size_t i = 0; i < N; i++) {
Vishnu Naire4f61742017-12-21 08:30:28 -0800263 const String16& serviceName = services[i];
264 if (IsSkipped(skippedServices, serviceName)) continue;
Felipe Leme859aef62016-02-03 12:17:10 -0800265
Steven Moreland5a30d342019-10-08 13:53:28 -0700266 if (startDumpThread(type, serviceName, args) == OK) {
Vishnu Naire4f61742017-12-21 08:30:28 -0800267 bool addSeparator = (N > 1);
268 if (addSeparator) {
269 writeDumpHeader(STDOUT_FILENO, serviceName, priorityFlags);
Josh Gao4b8f6f92016-02-29 16:20:34 -0800270 }
Vishnu Naire4f61742017-12-21 08:30:28 -0800271 std::chrono::duration<double> elapsedDuration;
272 size_t bytesWritten = 0;
273 status_t status =
274 writeDump(STDOUT_FILENO, serviceName, std::chrono::milliseconds(timeoutArgMs),
275 asProto, elapsedDuration, bytesWritten);
Josh Gao4b8f6f92016-02-29 16:20:34 -0800276
Vishnu Naire4f61742017-12-21 08:30:28 -0800277 if (status == TIMED_OUT) {
Steven Moreland3657ee52020-01-31 12:09:15 -0800278 std::cout << std::endl
Vishnu Naire4f61742017-12-21 08:30:28 -0800279 << "*** SERVICE '" << serviceName << "' DUMP TIMEOUT (" << timeoutArgMs
Steven Moreland3657ee52020-01-31 12:09:15 -0800280 << "ms) EXPIRED ***" << std::endl
281 << std::endl;
Josh Gao4b8f6f92016-02-29 16:20:34 -0800282 }
283
Vishnu Naire4f61742017-12-21 08:30:28 -0800284 if (addSeparator) {
285 writeDumpFooter(STDOUT_FILENO, serviceName, elapsedDuration);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700286 }
Vishnu Naire4f61742017-12-21 08:30:28 -0800287 bool dumpComplete = (status == OK);
288 stopDumpThread(dumpComplete);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700289 }
290 }
291
292 return 0;
293}
Vishnu Naire4f61742017-12-21 08:30:28 -0800294
295Vector<String16> Dumpsys::listServices(int priorityFilterFlags, bool filterByProto) const {
296 Vector<String16> services = sm_->listServices(priorityFilterFlags);
297 services.sort(sort_func);
298 if (filterByProto) {
299 Vector<String16> protoServices = sm_->listServices(IServiceManager::DUMP_FLAG_PROTO);
300 protoServices.sort(sort_func);
301 Vector<String16> intersection;
302 std::set_intersection(services.begin(), services.end(), protoServices.begin(),
303 protoServices.end(), std::back_inserter(intersection));
304 services = std::move(intersection);
305 }
306 return services;
307}
308
Vishnu Naire97d6122018-01-18 13:58:56 -0800309void Dumpsys::setServiceArgs(Vector<String16>& args, bool asProto, int priorityFlags) {
Vishnu Nair64afc022018-02-01 15:29:34 -0800310 // Add proto flag if dumping service as proto.
Vishnu Naire4f61742017-12-21 08:30:28 -0800311 if (asProto) {
312 args.insertAt(String16(PriorityDumper::PROTO_ARG), 0);
313 }
Vishnu Nair64afc022018-02-01 15:29:34 -0800314
315 // Add -a (dump all) flag if dumping all services, dumping normal services or
316 // services not explicitly registered to a priority bucket (default services).
317 if ((priorityFlags == IServiceManager::DUMP_FLAG_PRIORITY_ALL) ||
318 (priorityFlags == IServiceManager::DUMP_FLAG_PRIORITY_NORMAL) ||
319 (priorityFlags == IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT)) {
320 args.insertAt(String16("-a"), 0);
321 }
322
323 // Add priority flags when dumping services registered to a specific priority bucket.
324 if ((priorityFlags == IServiceManager::DUMP_FLAG_PRIORITY_CRITICAL) ||
325 (priorityFlags == IServiceManager::DUMP_FLAG_PRIORITY_HIGH) ||
326 (priorityFlags == IServiceManager::DUMP_FLAG_PRIORITY_NORMAL)) {
Vishnu Naire4f61742017-12-21 08:30:28 -0800327 String16 priorityType = ConvertBitmaskToPriorityType(priorityFlags);
328 args.insertAt(String16(PriorityDumper::PRIORITY_ARG), 0);
329 args.insertAt(priorityType, 1);
330 }
331}
332
Steven Moreland5a30d342019-10-08 13:53:28 -0700333static status_t dumpPidToFd(const sp<IBinder>& service, const unique_fd& fd) {
334 pid_t pid;
335 status_t status = service->getDebugPid(&pid);
336 if (status != OK) {
337 return status;
338 }
339 WriteStringToFd(std::to_string(pid) + "\n", fd.get());
340 return OK;
341}
342
Steven Morelande5a6a872021-05-19 22:11:37 +0000343static status_t dumpStabilityToFd(const sp<IBinder>& service, const unique_fd& fd) {
344 WriteStringToFd(internal::Stability::debugToString(service) + "\n", fd);
345 return OK;
346}
347
Devin Moorecfeeda42020-12-08 12:50:58 -0800348static status_t dumpThreadsToFd(const sp<IBinder>& service, const unique_fd& fd) {
349 pid_t pid;
350 status_t status = service->getDebugPid(&pid);
351 if (status != OK) {
352 return status;
353 }
354 BinderPidInfo pidInfo;
355 status = getBinderPidInfo(BinderDebugContext::BINDER, pid, &pidInfo);
356 if (status != OK) {
357 return status;
358 }
359 WriteStringToFd("Threads in use: " + std::to_string(pidInfo.threadUsage) + "/" +
360 std::to_string(pidInfo.threadCount) + "\n",
361 fd.get());
362 return OK;
363}
364
Steven Moreland5a30d342019-10-08 13:53:28 -0700365status_t Dumpsys::startDumpThread(Type type, const String16& serviceName,
366 const Vector<String16>& args) {
Vishnu Naire4f61742017-12-21 08:30:28 -0800367 sp<IBinder> service = sm_->checkService(serviceName);
368 if (service == nullptr) {
Steven Moreland3657ee52020-01-31 12:09:15 -0800369 std::cerr << "Can't find service: " << serviceName << std::endl;
Vishnu Naire4f61742017-12-21 08:30:28 -0800370 return NAME_NOT_FOUND;
371 }
372
373 int sfd[2];
374 if (pipe(sfd) != 0) {
Steven Moreland3657ee52020-01-31 12:09:15 -0800375 std::cerr << "Failed to create pipe to dump service info for " << serviceName << ": "
376 << strerror(errno) << std::endl;
Vishnu Naire4f61742017-12-21 08:30:28 -0800377 return -errno;
378 }
379
380 redirectFd_ = unique_fd(sfd[0]);
381 unique_fd remote_end(sfd[1]);
382 sfd[0] = sfd[1] = -1;
383
384 // dump blocks until completion, so spawn a thread..
385 activeThread_ = std::thread([=, remote_end{std::move(remote_end)}]() mutable {
Steven Moreland5a30d342019-10-08 13:53:28 -0700386 status_t err = 0;
Vishnu Naire4f61742017-12-21 08:30:28 -0800387
Steven Moreland5a30d342019-10-08 13:53:28 -0700388 switch (type) {
389 case Type::DUMP:
390 err = service->dump(remote_end.get(), args);
391 break;
392 case Type::PID:
393 err = dumpPidToFd(service, remote_end);
394 break;
Steven Morelande5a6a872021-05-19 22:11:37 +0000395 case Type::STABILITY:
396 err = dumpStabilityToFd(service, remote_end);
397 break;
Devin Moorecfeeda42020-12-08 12:50:58 -0800398 case Type::THREAD:
399 err = dumpThreadsToFd(service, remote_end);
400 break;
Steven Moreland5a30d342019-10-08 13:53:28 -0700401 default:
Steven Moreland3657ee52020-01-31 12:09:15 -0800402 std::cerr << "Unknown dump type" << static_cast<int>(type) << std::endl;
Steven Moreland5a30d342019-10-08 13:53:28 -0700403 return;
404 }
405
406 if (err != OK) {
Steven Moreland3657ee52020-01-31 12:09:15 -0800407 std::cerr << "Error dumping service info status_t: " << statusToString(err) << " "
408 << serviceName << std::endl;
Vishnu Naire4f61742017-12-21 08:30:28 -0800409 }
410 });
411 return OK;
412}
413
414void Dumpsys::stopDumpThread(bool dumpComplete) {
415 if (dumpComplete) {
416 activeThread_.join();
417 } else {
418 activeThread_.detach();
419 }
420 /* close read end of the dump output redirection pipe */
421 redirectFd_.reset();
422}
423
424void Dumpsys::writeDumpHeader(int fd, const String16& serviceName, int priorityFlags) const {
425 std::string msg(
426 "----------------------------------------"
427 "---------------------------------------\n");
428 if (priorityFlags == IServiceManager::DUMP_FLAG_PRIORITY_ALL ||
Vishnu Nair64afc022018-02-01 15:29:34 -0800429 priorityFlags == IServiceManager::DUMP_FLAG_PRIORITY_NORMAL ||
430 priorityFlags == IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT) {
Vishnu Naire4f61742017-12-21 08:30:28 -0800431 StringAppendF(&msg, "DUMP OF SERVICE %s:\n", String8(serviceName).c_str());
432 } else {
433 String16 priorityType = ConvertBitmaskToPriorityType(priorityFlags);
434 StringAppendF(&msg, "DUMP OF SERVICE %s %s:\n", String8(priorityType).c_str(),
435 String8(serviceName).c_str());
436 }
437 WriteStringToFd(msg, fd);
438}
439
440status_t Dumpsys::writeDump(int fd, const String16& serviceName, std::chrono::milliseconds timeout,
441 bool asProto, std::chrono::duration<double>& elapsedDuration,
442 size_t& bytesWritten) const {
443 status_t status = OK;
444 size_t totalBytes = 0;
445 auto start = std::chrono::steady_clock::now();
446 auto end = start + timeout;
447
448 int serviceDumpFd = redirectFd_.get();
449 if (serviceDumpFd == -1) {
450 return INVALID_OPERATION;
451 }
452
453 struct pollfd pfd = {.fd = serviceDumpFd, .events = POLLIN};
454
455 while (true) {
456 // Wrap this in a lambda so that TEMP_FAILURE_RETRY recalculates the timeout.
457 auto time_left_ms = [end]() {
458 auto now = std::chrono::steady_clock::now();
459 auto diff = std::chrono::duration_cast<std::chrono::milliseconds>(end - now);
Chih-Hung Hsieh9f2d5312018-12-11 15:34:17 -0800460 return std::max(diff.count(), 0LL);
Vishnu Naire4f61742017-12-21 08:30:28 -0800461 };
462
463 int rc = TEMP_FAILURE_RETRY(poll(&pfd, 1, time_left_ms()));
464 if (rc < 0) {
Steven Moreland3657ee52020-01-31 12:09:15 -0800465 std::cerr << "Error in poll while dumping service " << serviceName << " : "
466 << strerror(errno) << std::endl;
Vishnu Naire4f61742017-12-21 08:30:28 -0800467 status = -errno;
468 break;
Rhed Jaoa19145f2020-12-22 20:42:34 +0800469 } else if (rc == 0 || time_left_ms() == 0) {
Vishnu Naire4f61742017-12-21 08:30:28 -0800470 status = TIMED_OUT;
471 break;
472 }
473
474 char buf[4096];
475 rc = TEMP_FAILURE_RETRY(read(redirectFd_.get(), buf, sizeof(buf)));
476 if (rc < 0) {
Steven Moreland3657ee52020-01-31 12:09:15 -0800477 std::cerr << "Failed to read while dumping service " << serviceName << ": "
478 << strerror(errno) << std::endl;
Vishnu Naire4f61742017-12-21 08:30:28 -0800479 status = -errno;
480 break;
481 } else if (rc == 0) {
482 // EOF.
483 break;
484 }
485
486 if (!WriteFully(fd, buf, rc)) {
Steven Moreland3657ee52020-01-31 12:09:15 -0800487 std::cerr << "Failed to write while dumping service " << serviceName << ": "
488 << strerror(errno) << std::endl;
Vishnu Naire4f61742017-12-21 08:30:28 -0800489 status = -errno;
490 break;
491 }
492 totalBytes += rc;
493 }
494
495 if ((status == TIMED_OUT) && (!asProto)) {
496 std::string msg = StringPrintf("\n*** SERVICE '%s' DUMP TIMEOUT (%llums) EXPIRED ***\n\n",
497 String8(serviceName).string(), timeout.count());
498 WriteStringToFd(msg, fd);
499 }
500
501 elapsedDuration = std::chrono::steady_clock::now() - start;
502 bytesWritten = totalBytes;
503 return status;
504}
505
506void Dumpsys::writeDumpFooter(int fd, const String16& serviceName,
507 const std::chrono::duration<double>& elapsedDuration) const {
508 using std::chrono::system_clock;
509 const auto finish = system_clock::to_time_t(system_clock::now());
510 std::tm finish_tm;
511 localtime_r(&finish, &finish_tm);
512 std::stringstream oss;
513 oss << std::put_time(&finish_tm, "%Y-%m-%d %H:%M:%S");
514 std::string msg =
515 StringPrintf("--------- %.3fs was the duration of dumpsys %s, ending at: %s\n",
516 elapsedDuration.count(), String8(serviceName).string(), oss.str().c_str());
517 WriteStringToFd(msg, fd);
518}