blob: abdf168233e7dce083762e8e9182116ec2d62be9 [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;
Vishnu Naire4f61742017-12-21 08:30:28 -080046using ::android::base::StringAppendF;
47using ::android::base::StringPrintf;
48using ::android::base::unique_fd;
49using ::android::base::WriteFully;
50using ::android::base::WriteStringToFd;
Colin Crossf45fa6b2012-03-26 12:38:26 -070051
Steven Moreland2c3cd832017-02-13 23:44:17 +000052static int sort_func(const String16* lhs, const String16* rhs)
53{
Colin Crossf45fa6b2012-03-26 12:38:26 -070054 return lhs->compare(*rhs);
55}
56
Felipe Lemebbfd2b82016-02-03 11:16:27 -080057static void usage() {
58 fprintf(stderr,
Vishnu Nairf56042d2017-09-19 15:25:10 -070059 "usage: dumpsys\n"
Felipe Lemebbfd2b82016-02-03 11:16:27 -080060 " To dump all services.\n"
61 "or:\n"
Steven Moreland5a30d342019-10-08 13:53:28 -070062 " dumpsys [-t TIMEOUT] [--priority LEVEL] [--pid] [--help | -l | --skip SERVICES "
63 "| SERVICE [ARGS]]\n"
Felipe Lemebbfd2b82016-02-03 11:16:27 -080064 " --help: shows this help\n"
65 " -l: only list services, do not dump them\n"
Vishnu Nair6921f802017-11-22 09:17:23 -080066 " -t TIMEOUT_SEC: TIMEOUT to use in seconds instead of default 10 seconds\n"
67 " -T TIMEOUT_MS: TIMEOUT to use in milliseconds instead of default 10 seconds\n"
Steven Moreland5a30d342019-10-08 13:53:28 -070068 " --pid: dump PID instead of usual dump\n"
Ng Zhi Ancf2d01b2018-09-14 09:47:43 -070069 " --proto: filter services that support dumping data in proto format. Dumps\n"
Vishnu Nair6a408532017-10-24 09:11:27 -070070 " will be in proto format.\n"
Vishnu Nairf56042d2017-09-19 15:25:10 -070071 " --priority LEVEL: filter services based on specified priority\n"
72 " LEVEL must be one of CRITICAL | HIGH | NORMAL\n"
Felipe Leme859aef62016-02-03 12:17:10 -080073 " --skip SERVICES: dumps all services but SERVICES (comma-separated list)\n"
74 " SERVICE [ARGS]: dumps only service SERVICE, optionally passing ARGS to it\n");
75}
76
Felipe Leme343175a2016-08-02 18:57:37 -070077static bool IsSkipped(const Vector<String16>& skipped, const String16& service) {
Felipe Leme859aef62016-02-03 12:17:10 -080078 for (const auto& candidate : skipped) {
79 if (candidate == service) {
80 return true;
81 }
82 }
83 return false;
Felipe Lemebbfd2b82016-02-03 11:16:27 -080084}
85
Vishnu Nair6a408532017-10-24 09:11:27 -070086static bool ConvertPriorityTypeToBitmask(const String16& type, int& bitmask) {
87 if (type == PriorityDumper::PRIORITY_ARG_CRITICAL) {
88 bitmask = IServiceManager::DUMP_FLAG_PRIORITY_CRITICAL;
Vishnu Nair780b1282017-10-10 13:57:24 -070089 return true;
90 }
Vishnu Nair6a408532017-10-24 09:11:27 -070091 if (type == PriorityDumper::PRIORITY_ARG_HIGH) {
92 bitmask = IServiceManager::DUMP_FLAG_PRIORITY_HIGH;
Vishnu Nair780b1282017-10-10 13:57:24 -070093 return true;
94 }
Vishnu Nair6a408532017-10-24 09:11:27 -070095 if (type == PriorityDumper::PRIORITY_ARG_NORMAL) {
96 bitmask = IServiceManager::DUMP_FLAG_PRIORITY_NORMAL;
Vishnu Nair780b1282017-10-10 13:57:24 -070097 return true;
98 }
99 return false;
100}
101
Vishnu Naire4f61742017-12-21 08:30:28 -0800102String16 ConvertBitmaskToPriorityType(int bitmask) {
103 if (bitmask == IServiceManager::DUMP_FLAG_PRIORITY_CRITICAL) {
104 return String16(PriorityDumper::PRIORITY_ARG_CRITICAL);
105 }
106 if (bitmask == IServiceManager::DUMP_FLAG_PRIORITY_HIGH) {
107 return String16(PriorityDumper::PRIORITY_ARG_HIGH);
108 }
109 if (bitmask == IServiceManager::DUMP_FLAG_PRIORITY_NORMAL) {
110 return String16(PriorityDumper::PRIORITY_ARG_NORMAL);
111 }
112 return String16("");
113}
114
Felipe Leme343175a2016-08-02 18:57:37 -0700115int Dumpsys::main(int argc, char* const argv[]) {
Colin Crossf45fa6b2012-03-26 12:38:26 -0700116 Vector<String16> services;
117 Vector<String16> args;
Vishnu Nair780b1282017-10-10 13:57:24 -0700118 String16 priorityType;
Felipe Leme859aef62016-02-03 12:17:10 -0800119 Vector<String16> skippedServices;
Vishnu Nair6a408532017-10-24 09:11:27 -0700120 Vector<String16> protoServices;
keunyoungcaad5552013-06-13 15:08:51 -0700121 bool showListOnly = false;
Thierry Strudel8b78b752016-03-22 10:25:44 -0700122 bool skipServices = false;
Vishnu Naire4f61742017-12-21 08:30:28 -0800123 bool asProto = false;
Steven Moreland5a30d342019-10-08 13:53:28 -0700124 Type type = Type::DUMP;
Vishnu Nair6921f802017-11-22 09:17:23 -0800125 int timeoutArgMs = 10000;
Vishnu Naire4f61742017-12-21 08:30:28 -0800126 int priorityFlags = IServiceManager::DUMP_FLAG_PRIORITY_ALL;
Steven Moreland5a30d342019-10-08 13:53:28 -0700127 static struct option longOptions[] = {{"pid", no_argument, 0, 0},
128 {"priority", required_argument, 0, 0},
Vishnu Nair6a408532017-10-24 09:11:27 -0700129 {"proto", no_argument, 0, 0},
Vishnu Nairf56042d2017-09-19 15:25:10 -0700130 {"skip", no_argument, 0, 0},
131 {"help", no_argument, 0, 0},
132 {0, 0, 0, 0}};
Thierry Strudel8b78b752016-03-22 10:25:44 -0700133
Felipe Leme343175a2016-08-02 18:57:37 -0700134 // Must reset optind, otherwise subsequent calls will fail (wouldn't happen on main.cpp, but
135 // happens on test cases).
136 optind = 1;
Thierry Strudel8b78b752016-03-22 10:25:44 -0700137 while (1) {
138 int c;
139 int optionIndex = 0;
140
Vishnu Nair6921f802017-11-22 09:17:23 -0800141 c = getopt_long(argc, argv, "+t:T:l", longOptions, &optionIndex);
Thierry Strudel8b78b752016-03-22 10:25:44 -0700142
143 if (c == -1) {
144 break;
Felipe Lemebbfd2b82016-02-03 11:16:27 -0800145 }
Thierry Strudel8b78b752016-03-22 10:25:44 -0700146
147 switch (c) {
148 case 0:
149 if (!strcmp(longOptions[optionIndex].name, "skip")) {
150 skipServices = true;
Vishnu Nair6a408532017-10-24 09:11:27 -0700151 } else if (!strcmp(longOptions[optionIndex].name, "proto")) {
Vishnu Naire4f61742017-12-21 08:30:28 -0800152 asProto = true;
Thierry Strudel8b78b752016-03-22 10:25:44 -0700153 } else if (!strcmp(longOptions[optionIndex].name, "help")) {
154 usage();
155 return 0;
Vishnu Nairf56042d2017-09-19 15:25:10 -0700156 } else if (!strcmp(longOptions[optionIndex].name, "priority")) {
Vishnu Nair780b1282017-10-10 13:57:24 -0700157 priorityType = String16(String8(optarg));
Vishnu Naire4f61742017-12-21 08:30:28 -0800158 if (!ConvertPriorityTypeToBitmask(priorityType, priorityFlags)) {
Vishnu Nairf56042d2017-09-19 15:25:10 -0700159 fprintf(stderr, "\n");
160 usage();
161 return -1;
162 }
Steven Moreland5a30d342019-10-08 13:53:28 -0700163 } else if (!strcmp(longOptions[optionIndex].name, "pid")) {
164 type = Type::PID;
Thierry Strudel8b78b752016-03-22 10:25:44 -0700165 }
166 break;
167
168 case 't':
169 {
Vishnu Nair6921f802017-11-22 09:17:23 -0800170 char* endptr;
171 timeoutArgMs = strtol(optarg, &endptr, 10);
172 timeoutArgMs = timeoutArgMs * 1000;
173 if (*endptr != '\0' || timeoutArgMs <= 0) {
174 fprintf(stderr, "Error: invalid timeout(seconds) number: '%s'\n", optarg);
175 return -1;
176 }
177 }
178 break;
179
180 case 'T':
181 {
182 char* endptr;
183 timeoutArgMs = strtol(optarg, &endptr, 10);
184 if (*endptr != '\0' || timeoutArgMs <= 0) {
185 fprintf(stderr, "Error: invalid timeout(milliseconds) number: '%s'\n", optarg);
Thierry Strudel8b78b752016-03-22 10:25:44 -0700186 return -1;
187 }
188 }
189 break;
190
191 case 'l':
Felipe Lemebbfd2b82016-02-03 11:16:27 -0800192 showListOnly = true;
Thierry Strudel8b78b752016-03-22 10:25:44 -0700193 break;
194
195 default:
196 fprintf(stderr, "\n");
197 usage();
198 return -1;
Felipe Lemebbfd2b82016-02-03 11:16:27 -0800199 }
keunyoungcaad5552013-06-13 15:08:51 -0700200 }
Thierry Strudel8b78b752016-03-22 10:25:44 -0700201
202 for (int i = optind; i < argc; i++) {
203 if (skipServices) {
204 skippedServices.add(String16(argv[i]));
205 } else {
206 if (i == optind) {
207 services.add(String16(argv[i]));
208 } else {
Riddle Hsuc66abb22019-09-27 16:10:21 +0800209 const String16 arg(argv[i]);
210 args.add(arg);
211 // For backward compatible, if the proto argument is passed to the service, the
212 // dump request is also considered to use proto.
213 if (!asProto && !arg.compare(String16(PriorityDumper::PROTO_ARG))) {
214 asProto = true;
215 }
Felipe Leme859aef62016-02-03 12:17:10 -0800216 }
217 }
218 }
Thierry Strudel8b78b752016-03-22 10:25:44 -0700219
220 if ((skipServices && skippedServices.empty()) ||
Steven Moreland2c3cd832017-02-13 23:44:17 +0000221 (showListOnly && (!services.empty() || !skippedServices.empty()))) {
Thierry Strudel8b78b752016-03-22 10:25:44 -0700222 usage();
223 return -1;
224 }
225
Thierry Strudel159a8322016-03-23 11:22:34 -0700226 if (services.empty() || showListOnly) {
Vishnu Naire4f61742017-12-21 08:30:28 -0800227 services = listServices(priorityFlags, asProto);
228 setServiceArgs(args, asProto, priorityFlags);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700229 }
230
231 const size_t N = services.size();
Colin Crossf45fa6b2012-03-26 12:38:26 -0700232 if (N > 1) {
233 // first print a list of the current services
234 aout << "Currently running services:" << endl;
Felipe Lemebbfd2b82016-02-03 11:16:27 -0800235
Colin Crossf45fa6b2012-03-26 12:38:26 -0700236 for (size_t i=0; i<N; i++) {
Felipe Leme343175a2016-08-02 18:57:37 -0700237 sp<IBinder> service = sm_->checkService(services[i]);
238
239 if (service != nullptr) {
Felipe Leme859aef62016-02-03 12:17:10 -0800240 bool skipped = IsSkipped(skippedServices, services[i]);
241 aout << " " << services[i] << (skipped ? " (skipped)" : "") << endl;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700242 }
243 }
244 }
245
keunyoungcaad5552013-06-13 15:08:51 -0700246 if (showListOnly) {
247 return 0;
248 }
249
Josh Gao4b8f6f92016-02-29 16:20:34 -0800250 for (size_t i = 0; i < N; i++) {
Vishnu Naire4f61742017-12-21 08:30:28 -0800251 const String16& serviceName = services[i];
252 if (IsSkipped(skippedServices, serviceName)) continue;
Felipe Leme859aef62016-02-03 12:17:10 -0800253
Steven Moreland5a30d342019-10-08 13:53:28 -0700254 if (startDumpThread(type, serviceName, args) == OK) {
Vishnu Naire4f61742017-12-21 08:30:28 -0800255 bool addSeparator = (N > 1);
256 if (addSeparator) {
257 writeDumpHeader(STDOUT_FILENO, serviceName, priorityFlags);
Josh Gao4b8f6f92016-02-29 16:20:34 -0800258 }
Vishnu Naire4f61742017-12-21 08:30:28 -0800259 std::chrono::duration<double> elapsedDuration;
260 size_t bytesWritten = 0;
261 status_t status =
262 writeDump(STDOUT_FILENO, serviceName, std::chrono::milliseconds(timeoutArgMs),
263 asProto, elapsedDuration, bytesWritten);
Josh Gao4b8f6f92016-02-29 16:20:34 -0800264
Vishnu Naire4f61742017-12-21 08:30:28 -0800265 if (status == TIMED_OUT) {
Felipe Leme343175a2016-08-02 18:57:37 -0700266 aout << endl
Vishnu Naire4f61742017-12-21 08:30:28 -0800267 << "*** SERVICE '" << serviceName << "' DUMP TIMEOUT (" << timeoutArgMs
Vishnu Nair6921f802017-11-22 09:17:23 -0800268 << "ms) EXPIRED ***" << endl
Felipe Leme343175a2016-08-02 18:57:37 -0700269 << endl;
Josh Gao4b8f6f92016-02-29 16:20:34 -0800270 }
271
Vishnu Naire4f61742017-12-21 08:30:28 -0800272 if (addSeparator) {
273 writeDumpFooter(STDOUT_FILENO, serviceName, elapsedDuration);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700274 }
Vishnu Naire4f61742017-12-21 08:30:28 -0800275 bool dumpComplete = (status == OK);
276 stopDumpThread(dumpComplete);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700277 }
278 }
279
280 return 0;
281}
Vishnu Naire4f61742017-12-21 08:30:28 -0800282
283Vector<String16> Dumpsys::listServices(int priorityFilterFlags, bool filterByProto) const {
284 Vector<String16> services = sm_->listServices(priorityFilterFlags);
285 services.sort(sort_func);
286 if (filterByProto) {
287 Vector<String16> protoServices = sm_->listServices(IServiceManager::DUMP_FLAG_PROTO);
288 protoServices.sort(sort_func);
289 Vector<String16> intersection;
290 std::set_intersection(services.begin(), services.end(), protoServices.begin(),
291 protoServices.end(), std::back_inserter(intersection));
292 services = std::move(intersection);
293 }
294 return services;
295}
296
Vishnu Naire97d6122018-01-18 13:58:56 -0800297void Dumpsys::setServiceArgs(Vector<String16>& args, bool asProto, int priorityFlags) {
Vishnu Nair64afc022018-02-01 15:29:34 -0800298 // Add proto flag if dumping service as proto.
Vishnu Naire4f61742017-12-21 08:30:28 -0800299 if (asProto) {
300 args.insertAt(String16(PriorityDumper::PROTO_ARG), 0);
301 }
Vishnu Nair64afc022018-02-01 15:29:34 -0800302
303 // Add -a (dump all) flag if dumping all services, dumping normal services or
304 // services not explicitly registered to a priority bucket (default services).
305 if ((priorityFlags == IServiceManager::DUMP_FLAG_PRIORITY_ALL) ||
306 (priorityFlags == IServiceManager::DUMP_FLAG_PRIORITY_NORMAL) ||
307 (priorityFlags == IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT)) {
308 args.insertAt(String16("-a"), 0);
309 }
310
311 // Add priority flags when dumping services registered to a specific priority bucket.
312 if ((priorityFlags == IServiceManager::DUMP_FLAG_PRIORITY_CRITICAL) ||
313 (priorityFlags == IServiceManager::DUMP_FLAG_PRIORITY_HIGH) ||
314 (priorityFlags == IServiceManager::DUMP_FLAG_PRIORITY_NORMAL)) {
Vishnu Naire4f61742017-12-21 08:30:28 -0800315 String16 priorityType = ConvertBitmaskToPriorityType(priorityFlags);
316 args.insertAt(String16(PriorityDumper::PRIORITY_ARG), 0);
317 args.insertAt(priorityType, 1);
318 }
319}
320
Steven Moreland5a30d342019-10-08 13:53:28 -0700321static status_t dumpPidToFd(const sp<IBinder>& service, const unique_fd& fd) {
322 pid_t pid;
323 status_t status = service->getDebugPid(&pid);
324 if (status != OK) {
325 return status;
326 }
327 WriteStringToFd(std::to_string(pid) + "\n", fd.get());
328 return OK;
329}
330
331status_t Dumpsys::startDumpThread(Type type, const String16& serviceName,
332 const Vector<String16>& args) {
Vishnu Naire4f61742017-12-21 08:30:28 -0800333 sp<IBinder> service = sm_->checkService(serviceName);
334 if (service == nullptr) {
335 aerr << "Can't find service: " << serviceName << endl;
336 return NAME_NOT_FOUND;
337 }
338
339 int sfd[2];
340 if (pipe(sfd) != 0) {
341 aerr << "Failed to create pipe to dump service info for " << serviceName << ": "
342 << strerror(errno) << endl;
343 return -errno;
344 }
345
346 redirectFd_ = unique_fd(sfd[0]);
347 unique_fd remote_end(sfd[1]);
348 sfd[0] = sfd[1] = -1;
349
350 // dump blocks until completion, so spawn a thread..
351 activeThread_ = std::thread([=, remote_end{std::move(remote_end)}]() mutable {
Steven Moreland5a30d342019-10-08 13:53:28 -0700352 status_t err = 0;
Vishnu Naire4f61742017-12-21 08:30:28 -0800353
Steven Moreland5a30d342019-10-08 13:53:28 -0700354 switch (type) {
355 case Type::DUMP:
356 err = service->dump(remote_end.get(), args);
357 break;
358 case Type::PID:
359 err = dumpPidToFd(service, remote_end);
360 break;
361 default:
362 aerr << "Unknown dump type" << static_cast<int>(type) << endl;
363 return;
364 }
365
366 if (err != OK) {
367 aerr << "Error dumping service info status_t: (" << err << ") "
Vishnu Naire4f61742017-12-21 08:30:28 -0800368 << serviceName << endl;
369 }
370 });
371 return OK;
372}
373
374void Dumpsys::stopDumpThread(bool dumpComplete) {
375 if (dumpComplete) {
376 activeThread_.join();
377 } else {
378 activeThread_.detach();
379 }
380 /* close read end of the dump output redirection pipe */
381 redirectFd_.reset();
382}
383
384void Dumpsys::writeDumpHeader(int fd, const String16& serviceName, int priorityFlags) const {
385 std::string msg(
386 "----------------------------------------"
387 "---------------------------------------\n");
388 if (priorityFlags == IServiceManager::DUMP_FLAG_PRIORITY_ALL ||
Vishnu Nair64afc022018-02-01 15:29:34 -0800389 priorityFlags == IServiceManager::DUMP_FLAG_PRIORITY_NORMAL ||
390 priorityFlags == IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT) {
Vishnu Naire4f61742017-12-21 08:30:28 -0800391 StringAppendF(&msg, "DUMP OF SERVICE %s:\n", String8(serviceName).c_str());
392 } else {
393 String16 priorityType = ConvertBitmaskToPriorityType(priorityFlags);
394 StringAppendF(&msg, "DUMP OF SERVICE %s %s:\n", String8(priorityType).c_str(),
395 String8(serviceName).c_str());
396 }
397 WriteStringToFd(msg, fd);
398}
399
400status_t Dumpsys::writeDump(int fd, const String16& serviceName, std::chrono::milliseconds timeout,
401 bool asProto, std::chrono::duration<double>& elapsedDuration,
402 size_t& bytesWritten) const {
403 status_t status = OK;
404 size_t totalBytes = 0;
405 auto start = std::chrono::steady_clock::now();
406 auto end = start + timeout;
407
408 int serviceDumpFd = redirectFd_.get();
409 if (serviceDumpFd == -1) {
410 return INVALID_OPERATION;
411 }
412
413 struct pollfd pfd = {.fd = serviceDumpFd, .events = POLLIN};
414
415 while (true) {
416 // Wrap this in a lambda so that TEMP_FAILURE_RETRY recalculates the timeout.
417 auto time_left_ms = [end]() {
418 auto now = std::chrono::steady_clock::now();
419 auto diff = std::chrono::duration_cast<std::chrono::milliseconds>(end - now);
Chih-Hung Hsieh9f2d5312018-12-11 15:34:17 -0800420 return std::max(diff.count(), 0LL);
Vishnu Naire4f61742017-12-21 08:30:28 -0800421 };
422
423 int rc = TEMP_FAILURE_RETRY(poll(&pfd, 1, time_left_ms()));
424 if (rc < 0) {
425 aerr << "Error in poll while dumping service " << serviceName << " : "
426 << strerror(errno) << endl;
427 status = -errno;
428 break;
429 } else if (rc == 0) {
430 status = TIMED_OUT;
431 break;
432 }
433
434 char buf[4096];
435 rc = TEMP_FAILURE_RETRY(read(redirectFd_.get(), buf, sizeof(buf)));
436 if (rc < 0) {
437 aerr << "Failed to read while dumping service " << serviceName << ": "
438 << strerror(errno) << endl;
439 status = -errno;
440 break;
441 } else if (rc == 0) {
442 // EOF.
443 break;
444 }
445
446 if (!WriteFully(fd, buf, rc)) {
447 aerr << "Failed to write while dumping service " << serviceName << ": "
448 << strerror(errno) << endl;
449 status = -errno;
450 break;
451 }
452 totalBytes += rc;
453 }
454
455 if ((status == TIMED_OUT) && (!asProto)) {
456 std::string msg = StringPrintf("\n*** SERVICE '%s' DUMP TIMEOUT (%llums) EXPIRED ***\n\n",
457 String8(serviceName).string(), timeout.count());
458 WriteStringToFd(msg, fd);
459 }
460
461 elapsedDuration = std::chrono::steady_clock::now() - start;
462 bytesWritten = totalBytes;
463 return status;
464}
465
466void Dumpsys::writeDumpFooter(int fd, const String16& serviceName,
467 const std::chrono::duration<double>& elapsedDuration) const {
468 using std::chrono::system_clock;
469 const auto finish = system_clock::to_time_t(system_clock::now());
470 std::tm finish_tm;
471 localtime_r(&finish, &finish_tm);
472 std::stringstream oss;
473 oss << std::put_time(&finish_tm, "%Y-%m-%d %H:%M:%S");
474 std::string msg =
475 StringPrintf("--------- %.3fs was the duration of dumpsys %s, ending at: %s\n",
476 elapsedDuration.count(), String8(serviceName).string(), oss.str().c_str());
477 WriteStringToFd(msg, fd);
478}