blob: 7646285222c76b1727613b063af2a579e36f9d09 [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"
Vishnu Nairf56042d2017-09-19 15:25:10 -070062 " dumpsys [-t TIMEOUT] [--priority LEVEL] [--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"
Ng Zhi Ancf2d01b2018-09-14 09:47:43 -070068 " --proto: filter services that support dumping data in proto format. Dumps\n"
Vishnu Nair6a408532017-10-24 09:11:27 -070069 " will be in proto format.\n"
Vishnu Nairf56042d2017-09-19 15:25:10 -070070 " --priority LEVEL: filter services based on specified priority\n"
71 " LEVEL must be one of CRITICAL | HIGH | NORMAL\n"
Felipe Leme859aef62016-02-03 12:17:10 -080072 " --skip SERVICES: dumps all services but SERVICES (comma-separated list)\n"
73 " SERVICE [ARGS]: dumps only service SERVICE, optionally passing ARGS to it\n");
74}
75
Felipe Leme343175a2016-08-02 18:57:37 -070076static bool IsSkipped(const Vector<String16>& skipped, const String16& service) {
Felipe Leme859aef62016-02-03 12:17:10 -080077 for (const auto& candidate : skipped) {
78 if (candidate == service) {
79 return true;
80 }
81 }
82 return false;
Felipe Lemebbfd2b82016-02-03 11:16:27 -080083}
84
Vishnu Nair6a408532017-10-24 09:11:27 -070085static bool ConvertPriorityTypeToBitmask(const String16& type, int& bitmask) {
86 if (type == PriorityDumper::PRIORITY_ARG_CRITICAL) {
87 bitmask = IServiceManager::DUMP_FLAG_PRIORITY_CRITICAL;
Vishnu Nair780b1282017-10-10 13:57:24 -070088 return true;
89 }
Vishnu Nair6a408532017-10-24 09:11:27 -070090 if (type == PriorityDumper::PRIORITY_ARG_HIGH) {
91 bitmask = IServiceManager::DUMP_FLAG_PRIORITY_HIGH;
Vishnu Nair780b1282017-10-10 13:57:24 -070092 return true;
93 }
Vishnu Nair6a408532017-10-24 09:11:27 -070094 if (type == PriorityDumper::PRIORITY_ARG_NORMAL) {
95 bitmask = IServiceManager::DUMP_FLAG_PRIORITY_NORMAL;
Vishnu Nair780b1282017-10-10 13:57:24 -070096 return true;
97 }
98 return false;
99}
100
Vishnu Naire4f61742017-12-21 08:30:28 -0800101String16 ConvertBitmaskToPriorityType(int bitmask) {
102 if (bitmask == IServiceManager::DUMP_FLAG_PRIORITY_CRITICAL) {
103 return String16(PriorityDumper::PRIORITY_ARG_CRITICAL);
104 }
105 if (bitmask == IServiceManager::DUMP_FLAG_PRIORITY_HIGH) {
106 return String16(PriorityDumper::PRIORITY_ARG_HIGH);
107 }
108 if (bitmask == IServiceManager::DUMP_FLAG_PRIORITY_NORMAL) {
109 return String16(PriorityDumper::PRIORITY_ARG_NORMAL);
110 }
111 return String16("");
112}
113
Felipe Leme343175a2016-08-02 18:57:37 -0700114int Dumpsys::main(int argc, char* const argv[]) {
Colin Crossf45fa6b2012-03-26 12:38:26 -0700115 Vector<String16> services;
116 Vector<String16> args;
Vishnu Nair780b1282017-10-10 13:57:24 -0700117 String16 priorityType;
Felipe Leme859aef62016-02-03 12:17:10 -0800118 Vector<String16> skippedServices;
Vishnu Nair6a408532017-10-24 09:11:27 -0700119 Vector<String16> protoServices;
keunyoungcaad5552013-06-13 15:08:51 -0700120 bool showListOnly = false;
Thierry Strudel8b78b752016-03-22 10:25:44 -0700121 bool skipServices = false;
Vishnu Naire4f61742017-12-21 08:30:28 -0800122 bool asProto = false;
Vishnu Nair6921f802017-11-22 09:17:23 -0800123 int timeoutArgMs = 10000;
Vishnu Naire4f61742017-12-21 08:30:28 -0800124 int priorityFlags = IServiceManager::DUMP_FLAG_PRIORITY_ALL;
Vishnu Nairf56042d2017-09-19 15:25:10 -0700125 static struct option longOptions[] = {{"priority", required_argument, 0, 0},
Vishnu Nair6a408532017-10-24 09:11:27 -0700126 {"proto", no_argument, 0, 0},
Vishnu Nairf56042d2017-09-19 15:25:10 -0700127 {"skip", no_argument, 0, 0},
128 {"help", no_argument, 0, 0},
129 {0, 0, 0, 0}};
Thierry Strudel8b78b752016-03-22 10:25:44 -0700130
Felipe Leme343175a2016-08-02 18:57:37 -0700131 // Must reset optind, otherwise subsequent calls will fail (wouldn't happen on main.cpp, but
132 // happens on test cases).
133 optind = 1;
Thierry Strudel8b78b752016-03-22 10:25:44 -0700134 while (1) {
135 int c;
136 int optionIndex = 0;
137
Vishnu Nair6921f802017-11-22 09:17:23 -0800138 c = getopt_long(argc, argv, "+t:T:l", longOptions, &optionIndex);
Thierry Strudel8b78b752016-03-22 10:25:44 -0700139
140 if (c == -1) {
141 break;
Felipe Lemebbfd2b82016-02-03 11:16:27 -0800142 }
Thierry Strudel8b78b752016-03-22 10:25:44 -0700143
144 switch (c) {
145 case 0:
146 if (!strcmp(longOptions[optionIndex].name, "skip")) {
147 skipServices = true;
Vishnu Nair6a408532017-10-24 09:11:27 -0700148 } else if (!strcmp(longOptions[optionIndex].name, "proto")) {
Vishnu Naire4f61742017-12-21 08:30:28 -0800149 asProto = true;
Thierry Strudel8b78b752016-03-22 10:25:44 -0700150 } else if (!strcmp(longOptions[optionIndex].name, "help")) {
151 usage();
152 return 0;
Vishnu Nairf56042d2017-09-19 15:25:10 -0700153 } else if (!strcmp(longOptions[optionIndex].name, "priority")) {
Vishnu Nair780b1282017-10-10 13:57:24 -0700154 priorityType = String16(String8(optarg));
Vishnu Naire4f61742017-12-21 08:30:28 -0800155 if (!ConvertPriorityTypeToBitmask(priorityType, priorityFlags)) {
Vishnu Nairf56042d2017-09-19 15:25:10 -0700156 fprintf(stderr, "\n");
157 usage();
158 return -1;
159 }
Thierry Strudel8b78b752016-03-22 10:25:44 -0700160 }
161 break;
162
163 case 't':
164 {
Vishnu Nair6921f802017-11-22 09:17:23 -0800165 char* endptr;
166 timeoutArgMs = strtol(optarg, &endptr, 10);
167 timeoutArgMs = timeoutArgMs * 1000;
168 if (*endptr != '\0' || timeoutArgMs <= 0) {
169 fprintf(stderr, "Error: invalid timeout(seconds) number: '%s'\n", optarg);
170 return -1;
171 }
172 }
173 break;
174
175 case 'T':
176 {
177 char* endptr;
178 timeoutArgMs = strtol(optarg, &endptr, 10);
179 if (*endptr != '\0' || timeoutArgMs <= 0) {
180 fprintf(stderr, "Error: invalid timeout(milliseconds) number: '%s'\n", optarg);
Thierry Strudel8b78b752016-03-22 10:25:44 -0700181 return -1;
182 }
183 }
184 break;
185
186 case 'l':
Felipe Lemebbfd2b82016-02-03 11:16:27 -0800187 showListOnly = true;
Thierry Strudel8b78b752016-03-22 10:25:44 -0700188 break;
189
190 default:
191 fprintf(stderr, "\n");
192 usage();
193 return -1;
Felipe Lemebbfd2b82016-02-03 11:16:27 -0800194 }
keunyoungcaad5552013-06-13 15:08:51 -0700195 }
Thierry Strudel8b78b752016-03-22 10:25:44 -0700196
197 for (int i = optind; i < argc; i++) {
198 if (skipServices) {
199 skippedServices.add(String16(argv[i]));
200 } else {
201 if (i == optind) {
202 services.add(String16(argv[i]));
203 } else {
Riddle Hsuc66abb22019-09-27 16:10:21 +0800204 const String16 arg(argv[i]);
205 args.add(arg);
206 // For backward compatible, if the proto argument is passed to the service, the
207 // dump request is also considered to use proto.
208 if (!asProto && !arg.compare(String16(PriorityDumper::PROTO_ARG))) {
209 asProto = true;
210 }
Felipe Leme859aef62016-02-03 12:17:10 -0800211 }
212 }
213 }
Thierry Strudel8b78b752016-03-22 10:25:44 -0700214
215 if ((skipServices && skippedServices.empty()) ||
Steven Moreland2c3cd832017-02-13 23:44:17 +0000216 (showListOnly && (!services.empty() || !skippedServices.empty()))) {
Thierry Strudel8b78b752016-03-22 10:25:44 -0700217 usage();
218 return -1;
219 }
220
Thierry Strudel159a8322016-03-23 11:22:34 -0700221 if (services.empty() || showListOnly) {
Vishnu Naire4f61742017-12-21 08:30:28 -0800222 services = listServices(priorityFlags, asProto);
223 setServiceArgs(args, asProto, priorityFlags);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700224 }
225
226 const size_t N = services.size();
Colin Crossf45fa6b2012-03-26 12:38:26 -0700227 if (N > 1) {
228 // first print a list of the current services
229 aout << "Currently running services:" << endl;
Felipe Lemebbfd2b82016-02-03 11:16:27 -0800230
Colin Crossf45fa6b2012-03-26 12:38:26 -0700231 for (size_t i=0; i<N; i++) {
Felipe Leme343175a2016-08-02 18:57:37 -0700232 sp<IBinder> service = sm_->checkService(services[i]);
233
234 if (service != nullptr) {
Felipe Leme859aef62016-02-03 12:17:10 -0800235 bool skipped = IsSkipped(skippedServices, services[i]);
236 aout << " " << services[i] << (skipped ? " (skipped)" : "") << endl;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700237 }
238 }
239 }
240
keunyoungcaad5552013-06-13 15:08:51 -0700241 if (showListOnly) {
242 return 0;
243 }
244
Steven Morelandf5ea44c2019-09-23 15:42:01 -0700245 const bool dumpPid = !asProto;
246
Josh Gao4b8f6f92016-02-29 16:20:34 -0800247 for (size_t i = 0; i < N; i++) {
Vishnu Naire4f61742017-12-21 08:30:28 -0800248 const String16& serviceName = services[i];
249 if (IsSkipped(skippedServices, serviceName)) continue;
Felipe Leme859aef62016-02-03 12:17:10 -0800250
Steven Morelandf5ea44c2019-09-23 15:42:01 -0700251 if (startDumpThread(serviceName, dumpPid, args) == OK) {
Vishnu Naire4f61742017-12-21 08:30:28 -0800252 bool addSeparator = (N > 1);
253 if (addSeparator) {
254 writeDumpHeader(STDOUT_FILENO, serviceName, priorityFlags);
Josh Gao4b8f6f92016-02-29 16:20:34 -0800255 }
Vishnu Naire4f61742017-12-21 08:30:28 -0800256 std::chrono::duration<double> elapsedDuration;
257 size_t bytesWritten = 0;
258 status_t status =
259 writeDump(STDOUT_FILENO, serviceName, std::chrono::milliseconds(timeoutArgMs),
260 asProto, elapsedDuration, bytesWritten);
Josh Gao4b8f6f92016-02-29 16:20:34 -0800261
Vishnu Naire4f61742017-12-21 08:30:28 -0800262 if (status == TIMED_OUT) {
Felipe Leme343175a2016-08-02 18:57:37 -0700263 aout << endl
Vishnu Naire4f61742017-12-21 08:30:28 -0800264 << "*** SERVICE '" << serviceName << "' DUMP TIMEOUT (" << timeoutArgMs
Vishnu Nair6921f802017-11-22 09:17:23 -0800265 << "ms) EXPIRED ***" << endl
Felipe Leme343175a2016-08-02 18:57:37 -0700266 << endl;
Josh Gao4b8f6f92016-02-29 16:20:34 -0800267 }
268
Vishnu Naire4f61742017-12-21 08:30:28 -0800269 if (addSeparator) {
270 writeDumpFooter(STDOUT_FILENO, serviceName, elapsedDuration);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700271 }
Vishnu Naire4f61742017-12-21 08:30:28 -0800272 bool dumpComplete = (status == OK);
273 stopDumpThread(dumpComplete);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700274 }
275 }
276
277 return 0;
278}
Vishnu Naire4f61742017-12-21 08:30:28 -0800279
280Vector<String16> Dumpsys::listServices(int priorityFilterFlags, bool filterByProto) const {
281 Vector<String16> services = sm_->listServices(priorityFilterFlags);
282 services.sort(sort_func);
283 if (filterByProto) {
284 Vector<String16> protoServices = sm_->listServices(IServiceManager::DUMP_FLAG_PROTO);
285 protoServices.sort(sort_func);
286 Vector<String16> intersection;
287 std::set_intersection(services.begin(), services.end(), protoServices.begin(),
288 protoServices.end(), std::back_inserter(intersection));
289 services = std::move(intersection);
290 }
291 return services;
292}
293
Vishnu Naire97d6122018-01-18 13:58:56 -0800294void Dumpsys::setServiceArgs(Vector<String16>& args, bool asProto, int priorityFlags) {
Vishnu Nair64afc022018-02-01 15:29:34 -0800295 // Add proto flag if dumping service as proto.
Vishnu Naire4f61742017-12-21 08:30:28 -0800296 if (asProto) {
297 args.insertAt(String16(PriorityDumper::PROTO_ARG), 0);
298 }
Vishnu Nair64afc022018-02-01 15:29:34 -0800299
300 // Add -a (dump all) flag if dumping all services, dumping normal services or
301 // services not explicitly registered to a priority bucket (default services).
302 if ((priorityFlags == IServiceManager::DUMP_FLAG_PRIORITY_ALL) ||
303 (priorityFlags == IServiceManager::DUMP_FLAG_PRIORITY_NORMAL) ||
304 (priorityFlags == IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT)) {
305 args.insertAt(String16("-a"), 0);
306 }
307
308 // Add priority flags when dumping services registered to a specific priority bucket.
309 if ((priorityFlags == IServiceManager::DUMP_FLAG_PRIORITY_CRITICAL) ||
310 (priorityFlags == IServiceManager::DUMP_FLAG_PRIORITY_HIGH) ||
311 (priorityFlags == IServiceManager::DUMP_FLAG_PRIORITY_NORMAL)) {
Vishnu Naire4f61742017-12-21 08:30:28 -0800312 String16 priorityType = ConvertBitmaskToPriorityType(priorityFlags);
313 args.insertAt(String16(PriorityDumper::PRIORITY_ARG), 0);
314 args.insertAt(priorityType, 1);
315 }
316}
317
Steven Morelandf5ea44c2019-09-23 15:42:01 -0700318status_t Dumpsys::startDumpThread(const String16& serviceName, bool dumpPid, const Vector<String16>& args) {
Vishnu Naire4f61742017-12-21 08:30:28 -0800319 sp<IBinder> service = sm_->checkService(serviceName);
320 if (service == nullptr) {
321 aerr << "Can't find service: " << serviceName << endl;
322 return NAME_NOT_FOUND;
323 }
324
325 int sfd[2];
326 if (pipe(sfd) != 0) {
327 aerr << "Failed to create pipe to dump service info for " << serviceName << ": "
328 << strerror(errno) << endl;
329 return -errno;
330 }
331
332 redirectFd_ = unique_fd(sfd[0]);
333 unique_fd remote_end(sfd[1]);
334 sfd[0] = sfd[1] = -1;
335
336 // dump blocks until completion, so spawn a thread..
337 activeThread_ = std::thread([=, remote_end{std::move(remote_end)}]() mutable {
Steven Morelandf5ea44c2019-09-23 15:42:01 -0700338 if (dumpPid) {
339 pid_t pid;
340 status_t status = service->getDebugPid(&pid);
341 if (status == OK) {
342 std::ostringstream pidinfo;
343 pidinfo << "Pid: " << pid << std::endl;
344 WriteStringToFd(pidinfo.str(), remote_end.get());
345 } else {
346 aerr << "Error getting pid status_t (" << status << "): "
347 << serviceName << endl;
348 }
349 }
350
351 status_t err = service->dump(remote_end.get(), args);
Vishnu Naire4f61742017-12-21 08:30:28 -0800352
353 // It'd be nice to be able to close the remote end of the socketpair before the dump
354 // call returns, to terminate our reads if the other end closes their copy of the
355 // file descriptor, but then hangs for some reason. There doesn't seem to be a good
356 // way to do this, though.
357 remote_end.reset();
358
Steven Morelandf5ea44c2019-09-23 15:42:01 -0700359 if (err != OK) {
360 aerr << "Error dumping service info status_t (" << err << "): "
Vishnu Naire4f61742017-12-21 08:30:28 -0800361 << serviceName << endl;
362 }
363 });
364 return OK;
365}
366
367void Dumpsys::stopDumpThread(bool dumpComplete) {
368 if (dumpComplete) {
369 activeThread_.join();
370 } else {
371 activeThread_.detach();
372 }
373 /* close read end of the dump output redirection pipe */
374 redirectFd_.reset();
375}
376
377void Dumpsys::writeDumpHeader(int fd, const String16& serviceName, int priorityFlags) const {
378 std::string msg(
379 "----------------------------------------"
380 "---------------------------------------\n");
381 if (priorityFlags == IServiceManager::DUMP_FLAG_PRIORITY_ALL ||
Vishnu Nair64afc022018-02-01 15:29:34 -0800382 priorityFlags == IServiceManager::DUMP_FLAG_PRIORITY_NORMAL ||
383 priorityFlags == IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT) {
Vishnu Naire4f61742017-12-21 08:30:28 -0800384 StringAppendF(&msg, "DUMP OF SERVICE %s:\n", String8(serviceName).c_str());
385 } else {
386 String16 priorityType = ConvertBitmaskToPriorityType(priorityFlags);
387 StringAppendF(&msg, "DUMP OF SERVICE %s %s:\n", String8(priorityType).c_str(),
388 String8(serviceName).c_str());
389 }
390 WriteStringToFd(msg, fd);
391}
392
393status_t Dumpsys::writeDump(int fd, const String16& serviceName, std::chrono::milliseconds timeout,
394 bool asProto, std::chrono::duration<double>& elapsedDuration,
395 size_t& bytesWritten) const {
396 status_t status = OK;
397 size_t totalBytes = 0;
398 auto start = std::chrono::steady_clock::now();
399 auto end = start + timeout;
400
401 int serviceDumpFd = redirectFd_.get();
402 if (serviceDumpFd == -1) {
403 return INVALID_OPERATION;
404 }
405
406 struct pollfd pfd = {.fd = serviceDumpFd, .events = POLLIN};
407
408 while (true) {
409 // Wrap this in a lambda so that TEMP_FAILURE_RETRY recalculates the timeout.
410 auto time_left_ms = [end]() {
411 auto now = std::chrono::steady_clock::now();
412 auto diff = std::chrono::duration_cast<std::chrono::milliseconds>(end - now);
Chih-Hung Hsieh9f2d5312018-12-11 15:34:17 -0800413 return std::max(diff.count(), 0LL);
Vishnu Naire4f61742017-12-21 08:30:28 -0800414 };
415
416 int rc = TEMP_FAILURE_RETRY(poll(&pfd, 1, time_left_ms()));
417 if (rc < 0) {
418 aerr << "Error in poll while dumping service " << serviceName << " : "
419 << strerror(errno) << endl;
420 status = -errno;
421 break;
422 } else if (rc == 0) {
423 status = TIMED_OUT;
424 break;
425 }
426
427 char buf[4096];
428 rc = TEMP_FAILURE_RETRY(read(redirectFd_.get(), buf, sizeof(buf)));
429 if (rc < 0) {
430 aerr << "Failed to read while dumping service " << serviceName << ": "
431 << strerror(errno) << endl;
432 status = -errno;
433 break;
434 } else if (rc == 0) {
435 // EOF.
436 break;
437 }
438
439 if (!WriteFully(fd, buf, rc)) {
440 aerr << "Failed to write while dumping service " << serviceName << ": "
441 << strerror(errno) << endl;
442 status = -errno;
443 break;
444 }
445 totalBytes += rc;
446 }
447
448 if ((status == TIMED_OUT) && (!asProto)) {
449 std::string msg = StringPrintf("\n*** SERVICE '%s' DUMP TIMEOUT (%llums) EXPIRED ***\n\n",
450 String8(serviceName).string(), timeout.count());
451 WriteStringToFd(msg, fd);
452 }
453
454 elapsedDuration = std::chrono::steady_clock::now() - start;
455 bytesWritten = totalBytes;
456 return status;
457}
458
459void Dumpsys::writeDumpFooter(int fd, const String16& serviceName,
460 const std::chrono::duration<double>& elapsedDuration) const {
461 using std::chrono::system_clock;
462 const auto finish = system_clock::to_time_t(system_clock::now());
463 std::tm finish_tm;
464 localtime_r(&finish, &finish_tm);
465 std::stringstream oss;
466 oss << std::put_time(&finish_tm, "%Y-%m-%d %H:%M:%S");
467 std::string msg =
468 StringPrintf("--------- %.3fs was the duration of dumpsys %s, ending at: %s\n",
469 elapsedDuration.count(), String8(serviceName).string(), oss.str().c_str());
470 WriteStringToFd(msg, fd);
471}