blob: ba1c449dbfc6df7825500a840214aa9441c36fd1 [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>
Devin Moorecfeeda42020-12-08 12:50:58 -080028#include <binderdebug/BinderDebug.h>
Vishnu Nair780b1282017-10-10 13:57:24 -070029#include <serviceutils/PriorityDumper.h>
Josh Gao4b8f6f92016-02-29 16:20:34 -080030#include <utils/Log.h>
Colin Crossf45fa6b2012-03-26 12:38:26 -070031#include <utils/Vector.h>
32
Steven Moreland3657ee52020-01-31 12:09:15 -080033#include <iostream>
Josh Gao4b8f6f92016-02-29 16:20:34 -080034#include <fcntl.h>
Colin Crossf45fa6b2012-03-26 12:38:26 -070035#include <getopt.h>
Colin Crossf45fa6b2012-03-26 12:38:26 -070036#include <stdio.h>
Josh Gao4b8f6f92016-02-29 16:20:34 -080037#include <stdlib.h>
Colin Crossf45fa6b2012-03-26 12:38:26 -070038#include <string.h>
Josh Gao4b8f6f92016-02-29 16:20:34 -080039#include <sys/poll.h>
40#include <sys/socket.h>
Colin Crossf45fa6b2012-03-26 12:38:26 -070041#include <sys/time.h>
Josh Gao4b8f6f92016-02-29 16:20:34 -080042#include <sys/types.h>
43#include <unistd.h>
Colin Crossf45fa6b2012-03-26 12:38:26 -070044
Felipe Leme343175a2016-08-02 18:57:37 -070045#include "dumpsys.h"
46
Colin Crossf45fa6b2012-03-26 12:38:26 -070047using namespace android;
Vishnu Naire4f61742017-12-21 08:30:28 -080048using ::android::base::StringAppendF;
49using ::android::base::StringPrintf;
50using ::android::base::unique_fd;
51using ::android::base::WriteFully;
52using ::android::base::WriteStringToFd;
Colin Crossf45fa6b2012-03-26 12:38:26 -070053
Steven Moreland2c3cd832017-02-13 23:44:17 +000054static int sort_func(const String16* lhs, const String16* rhs)
55{
Colin Crossf45fa6b2012-03-26 12:38:26 -070056 return lhs->compare(*rhs);
57}
58
Felipe Lemebbfd2b82016-02-03 11:16:27 -080059static void usage() {
60 fprintf(stderr,
Vishnu Nairf56042d2017-09-19 15:25:10 -070061 "usage: dumpsys\n"
Felipe Lemebbfd2b82016-02-03 11:16:27 -080062 " To dump all services.\n"
63 "or:\n"
Devin Moorecfeeda42020-12-08 12:50:58 -080064 " dumpsys [-t TIMEOUT] [--priority LEVEL] [--pid] [--thread] [--help | -l | "
65 "--skip SERVICES "
Steven Moreland5a30d342019-10-08 13:53:28 -070066 "| SERVICE [ARGS]]\n"
Felipe Lemebbfd2b82016-02-03 11:16:27 -080067 " --help: shows this help\n"
68 " -l: only list services, do not dump them\n"
Vishnu Nair6921f802017-11-22 09:17:23 -080069 " -t TIMEOUT_SEC: TIMEOUT to use in seconds instead of default 10 seconds\n"
70 " -T TIMEOUT_MS: TIMEOUT to use in milliseconds instead of default 10 seconds\n"
Steven Moreland5a30d342019-10-08 13:53:28 -070071 " --pid: dump PID instead of usual dump\n"
Devin Moorecfeeda42020-12-08 12:50:58 -080072 " --thread: dump thread usage 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"
78 " SERVICE [ARGS]: dumps only service SERVICE, optionally passing ARGS to it\n");
79}
80
Felipe Leme343175a2016-08-02 18:57:37 -070081static bool IsSkipped(const Vector<String16>& skipped, const String16& service) {
Felipe Leme859aef62016-02-03 12:17:10 -080082 for (const auto& candidate : skipped) {
83 if (candidate == service) {
84 return true;
85 }
86 }
87 return false;
Felipe Lemebbfd2b82016-02-03 11:16:27 -080088}
89
Vishnu Nair6a408532017-10-24 09:11:27 -070090static bool ConvertPriorityTypeToBitmask(const String16& type, int& bitmask) {
91 if (type == PriorityDumper::PRIORITY_ARG_CRITICAL) {
92 bitmask = IServiceManager::DUMP_FLAG_PRIORITY_CRITICAL;
Vishnu Nair780b1282017-10-10 13:57:24 -070093 return true;
94 }
Vishnu Nair6a408532017-10-24 09:11:27 -070095 if (type == PriorityDumper::PRIORITY_ARG_HIGH) {
96 bitmask = IServiceManager::DUMP_FLAG_PRIORITY_HIGH;
Vishnu Nair780b1282017-10-10 13:57:24 -070097 return true;
98 }
Vishnu Nair6a408532017-10-24 09:11:27 -070099 if (type == PriorityDumper::PRIORITY_ARG_NORMAL) {
100 bitmask = IServiceManager::DUMP_FLAG_PRIORITY_NORMAL;
Vishnu Nair780b1282017-10-10 13:57:24 -0700101 return true;
102 }
103 return false;
104}
105
Vishnu Naire4f61742017-12-21 08:30:28 -0800106String16 ConvertBitmaskToPriorityType(int bitmask) {
107 if (bitmask == IServiceManager::DUMP_FLAG_PRIORITY_CRITICAL) {
108 return String16(PriorityDumper::PRIORITY_ARG_CRITICAL);
109 }
110 if (bitmask == IServiceManager::DUMP_FLAG_PRIORITY_HIGH) {
111 return String16(PriorityDumper::PRIORITY_ARG_HIGH);
112 }
113 if (bitmask == IServiceManager::DUMP_FLAG_PRIORITY_NORMAL) {
114 return String16(PriorityDumper::PRIORITY_ARG_NORMAL);
115 }
116 return String16("");
117}
118
Felipe Leme343175a2016-08-02 18:57:37 -0700119int Dumpsys::main(int argc, char* const argv[]) {
Colin Crossf45fa6b2012-03-26 12:38:26 -0700120 Vector<String16> services;
121 Vector<String16> args;
Vishnu Nair780b1282017-10-10 13:57:24 -0700122 String16 priorityType;
Felipe Leme859aef62016-02-03 12:17:10 -0800123 Vector<String16> skippedServices;
Vishnu Nair6a408532017-10-24 09:11:27 -0700124 Vector<String16> protoServices;
keunyoungcaad5552013-06-13 15:08:51 -0700125 bool showListOnly = false;
Thierry Strudel8b78b752016-03-22 10:25:44 -0700126 bool skipServices = false;
Vishnu Naire4f61742017-12-21 08:30:28 -0800127 bool asProto = false;
Steven Moreland5a30d342019-10-08 13:53:28 -0700128 Type type = Type::DUMP;
Vishnu Nair6921f802017-11-22 09:17:23 -0800129 int timeoutArgMs = 10000;
Vishnu Naire4f61742017-12-21 08:30:28 -0800130 int priorityFlags = IServiceManager::DUMP_FLAG_PRIORITY_ALL;
Devin Moorecfeeda42020-12-08 12:50:58 -0800131 static struct option longOptions[] = {{"thread", no_argument, 0, 0},
132 {"pid", no_argument, 0, 0},
Steven Moreland5a30d342019-10-08 13:53:28 -0700133 {"priority", required_argument, 0, 0},
Vishnu Nair6a408532017-10-24 09:11:27 -0700134 {"proto", no_argument, 0, 0},
Vishnu Nairf56042d2017-09-19 15:25:10 -0700135 {"skip", no_argument, 0, 0},
136 {"help", no_argument, 0, 0},
137 {0, 0, 0, 0}};
Thierry Strudel8b78b752016-03-22 10:25:44 -0700138
Felipe Leme343175a2016-08-02 18:57:37 -0700139 // Must reset optind, otherwise subsequent calls will fail (wouldn't happen on main.cpp, but
140 // happens on test cases).
141 optind = 1;
Thierry Strudel8b78b752016-03-22 10:25:44 -0700142 while (1) {
143 int c;
144 int optionIndex = 0;
145
Vishnu Nair6921f802017-11-22 09:17:23 -0800146 c = getopt_long(argc, argv, "+t:T:l", longOptions, &optionIndex);
Thierry Strudel8b78b752016-03-22 10:25:44 -0700147
148 if (c == -1) {
149 break;
Felipe Lemebbfd2b82016-02-03 11:16:27 -0800150 }
Thierry Strudel8b78b752016-03-22 10:25:44 -0700151
152 switch (c) {
153 case 0:
154 if (!strcmp(longOptions[optionIndex].name, "skip")) {
155 skipServices = true;
Vishnu Nair6a408532017-10-24 09:11:27 -0700156 } else if (!strcmp(longOptions[optionIndex].name, "proto")) {
Vishnu Naire4f61742017-12-21 08:30:28 -0800157 asProto = true;
Thierry Strudel8b78b752016-03-22 10:25:44 -0700158 } else if (!strcmp(longOptions[optionIndex].name, "help")) {
159 usage();
160 return 0;
Vishnu Nairf56042d2017-09-19 15:25:10 -0700161 } else if (!strcmp(longOptions[optionIndex].name, "priority")) {
Vishnu Nair780b1282017-10-10 13:57:24 -0700162 priorityType = String16(String8(optarg));
Vishnu Naire4f61742017-12-21 08:30:28 -0800163 if (!ConvertPriorityTypeToBitmask(priorityType, priorityFlags)) {
Vishnu Nairf56042d2017-09-19 15:25:10 -0700164 fprintf(stderr, "\n");
165 usage();
166 return -1;
167 }
Steven Moreland5a30d342019-10-08 13:53:28 -0700168 } else if (!strcmp(longOptions[optionIndex].name, "pid")) {
169 type = Type::PID;
Devin Moorecfeeda42020-12-08 12:50:58 -0800170 } else if (!strcmp(longOptions[optionIndex].name, "thread")) {
171 type = Type::THREAD;
Thierry Strudel8b78b752016-03-22 10:25:44 -0700172 }
173 break;
174
175 case 't':
176 {
Vishnu Nair6921f802017-11-22 09:17:23 -0800177 char* endptr;
178 timeoutArgMs = strtol(optarg, &endptr, 10);
179 timeoutArgMs = timeoutArgMs * 1000;
180 if (*endptr != '\0' || timeoutArgMs <= 0) {
181 fprintf(stderr, "Error: invalid timeout(seconds) number: '%s'\n", optarg);
182 return -1;
183 }
184 }
185 break;
186
187 case 'T':
188 {
189 char* endptr;
190 timeoutArgMs = strtol(optarg, &endptr, 10);
191 if (*endptr != '\0' || timeoutArgMs <= 0) {
192 fprintf(stderr, "Error: invalid timeout(milliseconds) number: '%s'\n", optarg);
Thierry Strudel8b78b752016-03-22 10:25:44 -0700193 return -1;
194 }
195 }
196 break;
197
198 case 'l':
Felipe Lemebbfd2b82016-02-03 11:16:27 -0800199 showListOnly = true;
Thierry Strudel8b78b752016-03-22 10:25:44 -0700200 break;
201
202 default:
203 fprintf(stderr, "\n");
204 usage();
205 return -1;
Felipe Lemebbfd2b82016-02-03 11:16:27 -0800206 }
keunyoungcaad5552013-06-13 15:08:51 -0700207 }
Thierry Strudel8b78b752016-03-22 10:25:44 -0700208
209 for (int i = optind; i < argc; i++) {
210 if (skipServices) {
211 skippedServices.add(String16(argv[i]));
212 } else {
213 if (i == optind) {
214 services.add(String16(argv[i]));
215 } else {
Riddle Hsuc66abb22019-09-27 16:10:21 +0800216 const String16 arg(argv[i]);
217 args.add(arg);
218 // For backward compatible, if the proto argument is passed to the service, the
219 // dump request is also considered to use proto.
220 if (!asProto && !arg.compare(String16(PriorityDumper::PROTO_ARG))) {
221 asProto = true;
222 }
Felipe Leme859aef62016-02-03 12:17:10 -0800223 }
224 }
225 }
Thierry Strudel8b78b752016-03-22 10:25:44 -0700226
227 if ((skipServices && skippedServices.empty()) ||
Steven Moreland2c3cd832017-02-13 23:44:17 +0000228 (showListOnly && (!services.empty() || !skippedServices.empty()))) {
Thierry Strudel8b78b752016-03-22 10:25:44 -0700229 usage();
230 return -1;
231 }
232
Thierry Strudel159a8322016-03-23 11:22:34 -0700233 if (services.empty() || showListOnly) {
Vishnu Naire4f61742017-12-21 08:30:28 -0800234 services = listServices(priorityFlags, asProto);
235 setServiceArgs(args, asProto, priorityFlags);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700236 }
237
238 const size_t N = services.size();
Steven Moreland31dac352020-03-05 09:46:45 -0800239 if (N > 1 || showListOnly) {
Colin Crossf45fa6b2012-03-26 12:38:26 -0700240 // first print a list of the current services
Steven Moreland3657ee52020-01-31 12:09:15 -0800241 std::cout << "Currently running services:" << std::endl;
Felipe Lemebbfd2b82016-02-03 11:16:27 -0800242
Colin Crossf45fa6b2012-03-26 12:38:26 -0700243 for (size_t i=0; i<N; i++) {
Felipe Leme343175a2016-08-02 18:57:37 -0700244 sp<IBinder> service = sm_->checkService(services[i]);
245
246 if (service != nullptr) {
Felipe Leme859aef62016-02-03 12:17:10 -0800247 bool skipped = IsSkipped(skippedServices, services[i]);
Steven Moreland3657ee52020-01-31 12:09:15 -0800248 std::cout << " " << services[i] << (skipped ? " (skipped)" : "") << std::endl;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700249 }
250 }
251 }
252
keunyoungcaad5552013-06-13 15:08:51 -0700253 if (showListOnly) {
254 return 0;
255 }
256
Josh Gao4b8f6f92016-02-29 16:20:34 -0800257 for (size_t i = 0; i < N; i++) {
Vishnu Naire4f61742017-12-21 08:30:28 -0800258 const String16& serviceName = services[i];
259 if (IsSkipped(skippedServices, serviceName)) continue;
Felipe Leme859aef62016-02-03 12:17:10 -0800260
Steven Moreland5a30d342019-10-08 13:53:28 -0700261 if (startDumpThread(type, serviceName, args) == OK) {
Vishnu Naire4f61742017-12-21 08:30:28 -0800262 bool addSeparator = (N > 1);
263 if (addSeparator) {
264 writeDumpHeader(STDOUT_FILENO, serviceName, priorityFlags);
Josh Gao4b8f6f92016-02-29 16:20:34 -0800265 }
Vishnu Naire4f61742017-12-21 08:30:28 -0800266 std::chrono::duration<double> elapsedDuration;
267 size_t bytesWritten = 0;
268 status_t status =
269 writeDump(STDOUT_FILENO, serviceName, std::chrono::milliseconds(timeoutArgMs),
270 asProto, elapsedDuration, bytesWritten);
Josh Gao4b8f6f92016-02-29 16:20:34 -0800271
Vishnu Naire4f61742017-12-21 08:30:28 -0800272 if (status == TIMED_OUT) {
Steven Moreland3657ee52020-01-31 12:09:15 -0800273 std::cout << std::endl
Vishnu Naire4f61742017-12-21 08:30:28 -0800274 << "*** SERVICE '" << serviceName << "' DUMP TIMEOUT (" << timeoutArgMs
Steven Moreland3657ee52020-01-31 12:09:15 -0800275 << "ms) EXPIRED ***" << std::endl
276 << std::endl;
Josh Gao4b8f6f92016-02-29 16:20:34 -0800277 }
278
Vishnu Naire4f61742017-12-21 08:30:28 -0800279 if (addSeparator) {
280 writeDumpFooter(STDOUT_FILENO, serviceName, elapsedDuration);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700281 }
Vishnu Naire4f61742017-12-21 08:30:28 -0800282 bool dumpComplete = (status == OK);
283 stopDumpThread(dumpComplete);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700284 }
285 }
286
287 return 0;
288}
Vishnu Naire4f61742017-12-21 08:30:28 -0800289
290Vector<String16> Dumpsys::listServices(int priorityFilterFlags, bool filterByProto) const {
291 Vector<String16> services = sm_->listServices(priorityFilterFlags);
292 services.sort(sort_func);
293 if (filterByProto) {
294 Vector<String16> protoServices = sm_->listServices(IServiceManager::DUMP_FLAG_PROTO);
295 protoServices.sort(sort_func);
296 Vector<String16> intersection;
297 std::set_intersection(services.begin(), services.end(), protoServices.begin(),
298 protoServices.end(), std::back_inserter(intersection));
299 services = std::move(intersection);
300 }
301 return services;
302}
303
Vishnu Naire97d6122018-01-18 13:58:56 -0800304void Dumpsys::setServiceArgs(Vector<String16>& args, bool asProto, int priorityFlags) {
Vishnu Nair64afc022018-02-01 15:29:34 -0800305 // Add proto flag if dumping service as proto.
Vishnu Naire4f61742017-12-21 08:30:28 -0800306 if (asProto) {
307 args.insertAt(String16(PriorityDumper::PROTO_ARG), 0);
308 }
Vishnu Nair64afc022018-02-01 15:29:34 -0800309
310 // Add -a (dump all) flag if dumping all services, dumping normal services or
311 // services not explicitly registered to a priority bucket (default services).
312 if ((priorityFlags == IServiceManager::DUMP_FLAG_PRIORITY_ALL) ||
313 (priorityFlags == IServiceManager::DUMP_FLAG_PRIORITY_NORMAL) ||
314 (priorityFlags == IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT)) {
315 args.insertAt(String16("-a"), 0);
316 }
317
318 // Add priority flags when dumping services registered to a specific priority bucket.
319 if ((priorityFlags == IServiceManager::DUMP_FLAG_PRIORITY_CRITICAL) ||
320 (priorityFlags == IServiceManager::DUMP_FLAG_PRIORITY_HIGH) ||
321 (priorityFlags == IServiceManager::DUMP_FLAG_PRIORITY_NORMAL)) {
Vishnu Naire4f61742017-12-21 08:30:28 -0800322 String16 priorityType = ConvertBitmaskToPriorityType(priorityFlags);
323 args.insertAt(String16(PriorityDumper::PRIORITY_ARG), 0);
324 args.insertAt(priorityType, 1);
325 }
326}
327
Steven Moreland5a30d342019-10-08 13:53:28 -0700328static status_t dumpPidToFd(const sp<IBinder>& service, const unique_fd& fd) {
329 pid_t pid;
330 status_t status = service->getDebugPid(&pid);
331 if (status != OK) {
332 return status;
333 }
334 WriteStringToFd(std::to_string(pid) + "\n", fd.get());
335 return OK;
336}
337
Devin Moorecfeeda42020-12-08 12:50:58 -0800338static status_t dumpThreadsToFd(const sp<IBinder>& service, const unique_fd& fd) {
339 pid_t pid;
340 status_t status = service->getDebugPid(&pid);
341 if (status != OK) {
342 return status;
343 }
344 BinderPidInfo pidInfo;
345 status = getBinderPidInfo(BinderDebugContext::BINDER, pid, &pidInfo);
346 if (status != OK) {
347 return status;
348 }
349 WriteStringToFd("Threads in use: " + std::to_string(pidInfo.threadUsage) + "/" +
350 std::to_string(pidInfo.threadCount) + "\n",
351 fd.get());
352 return OK;
353}
354
Steven Moreland5a30d342019-10-08 13:53:28 -0700355status_t Dumpsys::startDumpThread(Type type, const String16& serviceName,
356 const Vector<String16>& args) {
Vishnu Naire4f61742017-12-21 08:30:28 -0800357 sp<IBinder> service = sm_->checkService(serviceName);
358 if (service == nullptr) {
Steven Moreland3657ee52020-01-31 12:09:15 -0800359 std::cerr << "Can't find service: " << serviceName << std::endl;
Vishnu Naire4f61742017-12-21 08:30:28 -0800360 return NAME_NOT_FOUND;
361 }
362
363 int sfd[2];
364 if (pipe(sfd) != 0) {
Steven Moreland3657ee52020-01-31 12:09:15 -0800365 std::cerr << "Failed to create pipe to dump service info for " << serviceName << ": "
366 << strerror(errno) << std::endl;
Vishnu Naire4f61742017-12-21 08:30:28 -0800367 return -errno;
368 }
369
370 redirectFd_ = unique_fd(sfd[0]);
371 unique_fd remote_end(sfd[1]);
372 sfd[0] = sfd[1] = -1;
373
374 // dump blocks until completion, so spawn a thread..
375 activeThread_ = std::thread([=, remote_end{std::move(remote_end)}]() mutable {
Steven Moreland5a30d342019-10-08 13:53:28 -0700376 status_t err = 0;
Vishnu Naire4f61742017-12-21 08:30:28 -0800377
Steven Moreland5a30d342019-10-08 13:53:28 -0700378 switch (type) {
379 case Type::DUMP:
380 err = service->dump(remote_end.get(), args);
381 break;
382 case Type::PID:
383 err = dumpPidToFd(service, remote_end);
384 break;
Devin Moorecfeeda42020-12-08 12:50:58 -0800385 case Type::THREAD:
386 err = dumpThreadsToFd(service, remote_end);
387 break;
Steven Moreland5a30d342019-10-08 13:53:28 -0700388 default:
Steven Moreland3657ee52020-01-31 12:09:15 -0800389 std::cerr << "Unknown dump type" << static_cast<int>(type) << std::endl;
Steven Moreland5a30d342019-10-08 13:53:28 -0700390 return;
391 }
392
393 if (err != OK) {
Steven Moreland3657ee52020-01-31 12:09:15 -0800394 std::cerr << "Error dumping service info status_t: " << statusToString(err) << " "
395 << serviceName << std::endl;
Vishnu Naire4f61742017-12-21 08:30:28 -0800396 }
397 });
398 return OK;
399}
400
401void Dumpsys::stopDumpThread(bool dumpComplete) {
402 if (dumpComplete) {
403 activeThread_.join();
404 } else {
405 activeThread_.detach();
406 }
407 /* close read end of the dump output redirection pipe */
408 redirectFd_.reset();
409}
410
411void Dumpsys::writeDumpHeader(int fd, const String16& serviceName, int priorityFlags) const {
412 std::string msg(
413 "----------------------------------------"
414 "---------------------------------------\n");
415 if (priorityFlags == IServiceManager::DUMP_FLAG_PRIORITY_ALL ||
Vishnu Nair64afc022018-02-01 15:29:34 -0800416 priorityFlags == IServiceManager::DUMP_FLAG_PRIORITY_NORMAL ||
417 priorityFlags == IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT) {
Vishnu Naire4f61742017-12-21 08:30:28 -0800418 StringAppendF(&msg, "DUMP OF SERVICE %s:\n", String8(serviceName).c_str());
419 } else {
420 String16 priorityType = ConvertBitmaskToPriorityType(priorityFlags);
421 StringAppendF(&msg, "DUMP OF SERVICE %s %s:\n", String8(priorityType).c_str(),
422 String8(serviceName).c_str());
423 }
424 WriteStringToFd(msg, fd);
425}
426
427status_t Dumpsys::writeDump(int fd, const String16& serviceName, std::chrono::milliseconds timeout,
428 bool asProto, std::chrono::duration<double>& elapsedDuration,
429 size_t& bytesWritten) const {
430 status_t status = OK;
431 size_t totalBytes = 0;
432 auto start = std::chrono::steady_clock::now();
433 auto end = start + timeout;
434
435 int serviceDumpFd = redirectFd_.get();
436 if (serviceDumpFd == -1) {
437 return INVALID_OPERATION;
438 }
439
440 struct pollfd pfd = {.fd = serviceDumpFd, .events = POLLIN};
441
442 while (true) {
443 // Wrap this in a lambda so that TEMP_FAILURE_RETRY recalculates the timeout.
444 auto time_left_ms = [end]() {
445 auto now = std::chrono::steady_clock::now();
446 auto diff = std::chrono::duration_cast<std::chrono::milliseconds>(end - now);
Chih-Hung Hsieh9f2d5312018-12-11 15:34:17 -0800447 return std::max(diff.count(), 0LL);
Vishnu Naire4f61742017-12-21 08:30:28 -0800448 };
449
450 int rc = TEMP_FAILURE_RETRY(poll(&pfd, 1, time_left_ms()));
451 if (rc < 0) {
Steven Moreland3657ee52020-01-31 12:09:15 -0800452 std::cerr << "Error in poll while dumping service " << serviceName << " : "
453 << strerror(errno) << std::endl;
Vishnu Naire4f61742017-12-21 08:30:28 -0800454 status = -errno;
455 break;
Rhed Jaoa19145f2020-12-22 20:42:34 +0800456 } else if (rc == 0 || time_left_ms() == 0) {
Vishnu Naire4f61742017-12-21 08:30:28 -0800457 status = TIMED_OUT;
458 break;
459 }
460
461 char buf[4096];
462 rc = TEMP_FAILURE_RETRY(read(redirectFd_.get(), buf, sizeof(buf)));
463 if (rc < 0) {
Steven Moreland3657ee52020-01-31 12:09:15 -0800464 std::cerr << "Failed to read while dumping service " << serviceName << ": "
465 << strerror(errno) << std::endl;
Vishnu Naire4f61742017-12-21 08:30:28 -0800466 status = -errno;
467 break;
468 } else if (rc == 0) {
469 // EOF.
470 break;
471 }
472
473 if (!WriteFully(fd, buf, rc)) {
Steven Moreland3657ee52020-01-31 12:09:15 -0800474 std::cerr << "Failed to write while dumping service " << serviceName << ": "
475 << strerror(errno) << std::endl;
Vishnu Naire4f61742017-12-21 08:30:28 -0800476 status = -errno;
477 break;
478 }
479 totalBytes += rc;
480 }
481
482 if ((status == TIMED_OUT) && (!asProto)) {
483 std::string msg = StringPrintf("\n*** SERVICE '%s' DUMP TIMEOUT (%llums) EXPIRED ***\n\n",
484 String8(serviceName).string(), timeout.count());
485 WriteStringToFd(msg, fd);
486 }
487
488 elapsedDuration = std::chrono::steady_clock::now() - start;
489 bytesWritten = totalBytes;
490 return status;
491}
492
493void Dumpsys::writeDumpFooter(int fd, const String16& serviceName,
494 const std::chrono::duration<double>& elapsedDuration) const {
495 using std::chrono::system_clock;
496 const auto finish = system_clock::to_time_t(system_clock::now());
497 std::tm finish_tm;
498 localtime_r(&finish, &finish_tm);
499 std::stringstream oss;
500 oss << std::put_time(&finish_tm, "%Y-%m-%d %H:%M:%S");
501 std::string msg =
502 StringPrintf("--------- %.3fs was the duration of dumpsys %s, ending at: %s\n",
503 elapsedDuration.count(), String8(serviceName).string(), oss.str().c_str());
504 WriteStringToFd(msg, fd);
505}