blob: 1327cfd155a033ee01d25caa3ff894bb366de60d [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
Steven Moreland3657ee52020-01-31 12:09:15 -080032#include <iostream>
Josh Gao4b8f6f92016-02-29 16:20:34 -080033#include <fcntl.h>
Colin Crossf45fa6b2012-03-26 12:38:26 -070034#include <getopt.h>
Colin Crossf45fa6b2012-03-26 12:38:26 -070035#include <stdio.h>
Josh Gao4b8f6f92016-02-29 16:20:34 -080036#include <stdlib.h>
Colin Crossf45fa6b2012-03-26 12:38:26 -070037#include <string.h>
Josh Gao4b8f6f92016-02-29 16:20:34 -080038#include <sys/poll.h>
39#include <sys/socket.h>
Colin Crossf45fa6b2012-03-26 12:38:26 -070040#include <sys/time.h>
Josh Gao4b8f6f92016-02-29 16:20:34 -080041#include <sys/types.h>
42#include <unistd.h>
Colin Crossf45fa6b2012-03-26 12:38:26 -070043
Felipe Leme343175a2016-08-02 18:57:37 -070044#include "dumpsys.h"
45
Colin Crossf45fa6b2012-03-26 12:38:26 -070046using namespace android;
Vishnu Naire4f61742017-12-21 08:30:28 -080047using ::android::base::StringAppendF;
48using ::android::base::StringPrintf;
49using ::android::base::unique_fd;
50using ::android::base::WriteFully;
51using ::android::base::WriteStringToFd;
Colin Crossf45fa6b2012-03-26 12:38:26 -070052
Steven Moreland2c3cd832017-02-13 23:44:17 +000053static int sort_func(const String16* lhs, const String16* rhs)
54{
Colin Crossf45fa6b2012-03-26 12:38:26 -070055 return lhs->compare(*rhs);
56}
57
Felipe Lemebbfd2b82016-02-03 11:16:27 -080058static void usage() {
59 fprintf(stderr,
Vishnu Nairf56042d2017-09-19 15:25:10 -070060 "usage: dumpsys\n"
Felipe Lemebbfd2b82016-02-03 11:16:27 -080061 " To dump all services.\n"
62 "or:\n"
Steven Moreland5a30d342019-10-08 13:53:28 -070063 " dumpsys [-t TIMEOUT] [--priority LEVEL] [--pid] [--help | -l | --skip SERVICES "
64 "| SERVICE [ARGS]]\n"
Felipe Lemebbfd2b82016-02-03 11:16:27 -080065 " --help: shows this help\n"
66 " -l: only list services, do not dump them\n"
Vishnu Nair6921f802017-11-22 09:17:23 -080067 " -t TIMEOUT_SEC: TIMEOUT to use in seconds instead of default 10 seconds\n"
68 " -T TIMEOUT_MS: TIMEOUT to use in milliseconds instead of default 10 seconds\n"
Steven Moreland5a30d342019-10-08 13:53:28 -070069 " --pid: dump PID instead of usual dump\n"
Ng Zhi Ancf2d01b2018-09-14 09:47:43 -070070 " --proto: filter services that support dumping data in proto format. Dumps\n"
Vishnu Nair6a408532017-10-24 09:11:27 -070071 " will be in proto format.\n"
Vishnu Nairf56042d2017-09-19 15:25:10 -070072 " --priority LEVEL: filter services based on specified priority\n"
73 " LEVEL must be one of CRITICAL | HIGH | NORMAL\n"
Felipe Leme859aef62016-02-03 12:17:10 -080074 " --skip SERVICES: dumps all services but SERVICES (comma-separated list)\n"
75 " SERVICE [ARGS]: dumps only service SERVICE, optionally passing ARGS to it\n");
76}
77
Felipe Leme343175a2016-08-02 18:57:37 -070078static bool IsSkipped(const Vector<String16>& skipped, const String16& service) {
Felipe Leme859aef62016-02-03 12:17:10 -080079 for (const auto& candidate : skipped) {
80 if (candidate == service) {
81 return true;
82 }
83 }
84 return false;
Felipe Lemebbfd2b82016-02-03 11:16:27 -080085}
86
Vishnu Nair6a408532017-10-24 09:11:27 -070087static bool ConvertPriorityTypeToBitmask(const String16& type, int& bitmask) {
88 if (type == PriorityDumper::PRIORITY_ARG_CRITICAL) {
89 bitmask = IServiceManager::DUMP_FLAG_PRIORITY_CRITICAL;
Vishnu Nair780b1282017-10-10 13:57:24 -070090 return true;
91 }
Vishnu Nair6a408532017-10-24 09:11:27 -070092 if (type == PriorityDumper::PRIORITY_ARG_HIGH) {
93 bitmask = IServiceManager::DUMP_FLAG_PRIORITY_HIGH;
Vishnu Nair780b1282017-10-10 13:57:24 -070094 return true;
95 }
Vishnu Nair6a408532017-10-24 09:11:27 -070096 if (type == PriorityDumper::PRIORITY_ARG_NORMAL) {
97 bitmask = IServiceManager::DUMP_FLAG_PRIORITY_NORMAL;
Vishnu Nair780b1282017-10-10 13:57:24 -070098 return true;
99 }
100 return false;
101}
102
Vishnu Naire4f61742017-12-21 08:30:28 -0800103String16 ConvertBitmaskToPriorityType(int bitmask) {
104 if (bitmask == IServiceManager::DUMP_FLAG_PRIORITY_CRITICAL) {
105 return String16(PriorityDumper::PRIORITY_ARG_CRITICAL);
106 }
107 if (bitmask == IServiceManager::DUMP_FLAG_PRIORITY_HIGH) {
108 return String16(PriorityDumper::PRIORITY_ARG_HIGH);
109 }
110 if (bitmask == IServiceManager::DUMP_FLAG_PRIORITY_NORMAL) {
111 return String16(PriorityDumper::PRIORITY_ARG_NORMAL);
112 }
113 return String16("");
114}
115
Felipe Leme343175a2016-08-02 18:57:37 -0700116int Dumpsys::main(int argc, char* const argv[]) {
Colin Crossf45fa6b2012-03-26 12:38:26 -0700117 Vector<String16> services;
118 Vector<String16> args;
Vishnu Nair780b1282017-10-10 13:57:24 -0700119 String16 priorityType;
Felipe Leme859aef62016-02-03 12:17:10 -0800120 Vector<String16> skippedServices;
Vishnu Nair6a408532017-10-24 09:11:27 -0700121 Vector<String16> protoServices;
keunyoungcaad5552013-06-13 15:08:51 -0700122 bool showListOnly = false;
Thierry Strudel8b78b752016-03-22 10:25:44 -0700123 bool skipServices = false;
Vishnu Naire4f61742017-12-21 08:30:28 -0800124 bool asProto = false;
Steven Moreland5a30d342019-10-08 13:53:28 -0700125 Type type = Type::DUMP;
Vishnu Nair6921f802017-11-22 09:17:23 -0800126 int timeoutArgMs = 10000;
Vishnu Naire4f61742017-12-21 08:30:28 -0800127 int priorityFlags = IServiceManager::DUMP_FLAG_PRIORITY_ALL;
Steven Moreland5a30d342019-10-08 13:53:28 -0700128 static struct option longOptions[] = {{"pid", no_argument, 0, 0},
129 {"priority", required_argument, 0, 0},
Vishnu Nair6a408532017-10-24 09:11:27 -0700130 {"proto", no_argument, 0, 0},
Vishnu Nairf56042d2017-09-19 15:25:10 -0700131 {"skip", no_argument, 0, 0},
132 {"help", no_argument, 0, 0},
133 {0, 0, 0, 0}};
Thierry Strudel8b78b752016-03-22 10:25:44 -0700134
Felipe Leme343175a2016-08-02 18:57:37 -0700135 // Must reset optind, otherwise subsequent calls will fail (wouldn't happen on main.cpp, but
136 // happens on test cases).
137 optind = 1;
Thierry Strudel8b78b752016-03-22 10:25:44 -0700138 while (1) {
139 int c;
140 int optionIndex = 0;
141
Vishnu Nair6921f802017-11-22 09:17:23 -0800142 c = getopt_long(argc, argv, "+t:T:l", longOptions, &optionIndex);
Thierry Strudel8b78b752016-03-22 10:25:44 -0700143
144 if (c == -1) {
145 break;
Felipe Lemebbfd2b82016-02-03 11:16:27 -0800146 }
Thierry Strudel8b78b752016-03-22 10:25:44 -0700147
148 switch (c) {
149 case 0:
150 if (!strcmp(longOptions[optionIndex].name, "skip")) {
151 skipServices = true;
Vishnu Nair6a408532017-10-24 09:11:27 -0700152 } else if (!strcmp(longOptions[optionIndex].name, "proto")) {
Vishnu Naire4f61742017-12-21 08:30:28 -0800153 asProto = true;
Thierry Strudel8b78b752016-03-22 10:25:44 -0700154 } else if (!strcmp(longOptions[optionIndex].name, "help")) {
155 usage();
156 return 0;
Vishnu Nairf56042d2017-09-19 15:25:10 -0700157 } else if (!strcmp(longOptions[optionIndex].name, "priority")) {
Vishnu Nair780b1282017-10-10 13:57:24 -0700158 priorityType = String16(String8(optarg));
Vishnu Naire4f61742017-12-21 08:30:28 -0800159 if (!ConvertPriorityTypeToBitmask(priorityType, priorityFlags)) {
Vishnu Nairf56042d2017-09-19 15:25:10 -0700160 fprintf(stderr, "\n");
161 usage();
162 return -1;
163 }
Steven Moreland5a30d342019-10-08 13:53:28 -0700164 } else if (!strcmp(longOptions[optionIndex].name, "pid")) {
165 type = Type::PID;
Thierry Strudel8b78b752016-03-22 10:25:44 -0700166 }
167 break;
168
169 case 't':
170 {
Vishnu Nair6921f802017-11-22 09:17:23 -0800171 char* endptr;
172 timeoutArgMs = strtol(optarg, &endptr, 10);
173 timeoutArgMs = timeoutArgMs * 1000;
174 if (*endptr != '\0' || timeoutArgMs <= 0) {
175 fprintf(stderr, "Error: invalid timeout(seconds) number: '%s'\n", optarg);
176 return -1;
177 }
178 }
179 break;
180
181 case 'T':
182 {
183 char* endptr;
184 timeoutArgMs = strtol(optarg, &endptr, 10);
185 if (*endptr != '\0' || timeoutArgMs <= 0) {
186 fprintf(stderr, "Error: invalid timeout(milliseconds) number: '%s'\n", optarg);
Thierry Strudel8b78b752016-03-22 10:25:44 -0700187 return -1;
188 }
189 }
190 break;
191
192 case 'l':
Felipe Lemebbfd2b82016-02-03 11:16:27 -0800193 showListOnly = true;
Thierry Strudel8b78b752016-03-22 10:25:44 -0700194 break;
195
196 default:
197 fprintf(stderr, "\n");
198 usage();
199 return -1;
Felipe Lemebbfd2b82016-02-03 11:16:27 -0800200 }
keunyoungcaad5552013-06-13 15:08:51 -0700201 }
Thierry Strudel8b78b752016-03-22 10:25:44 -0700202
203 for (int i = optind; i < argc; i++) {
204 if (skipServices) {
205 skippedServices.add(String16(argv[i]));
206 } else {
207 if (i == optind) {
208 services.add(String16(argv[i]));
209 } else {
Riddle Hsuc66abb22019-09-27 16:10:21 +0800210 const String16 arg(argv[i]);
211 args.add(arg);
212 // For backward compatible, if the proto argument is passed to the service, the
213 // dump request is also considered to use proto.
214 if (!asProto && !arg.compare(String16(PriorityDumper::PROTO_ARG))) {
215 asProto = true;
216 }
Felipe Leme859aef62016-02-03 12:17:10 -0800217 }
218 }
219 }
Thierry Strudel8b78b752016-03-22 10:25:44 -0700220
221 if ((skipServices && skippedServices.empty()) ||
Steven Moreland2c3cd832017-02-13 23:44:17 +0000222 (showListOnly && (!services.empty() || !skippedServices.empty()))) {
Thierry Strudel8b78b752016-03-22 10:25:44 -0700223 usage();
224 return -1;
225 }
226
Thierry Strudel159a8322016-03-23 11:22:34 -0700227 if (services.empty() || showListOnly) {
Vishnu Naire4f61742017-12-21 08:30:28 -0800228 services = listServices(priorityFlags, asProto);
229 setServiceArgs(args, asProto, priorityFlags);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700230 }
231
232 const size_t N = services.size();
Steven Moreland31dac352020-03-05 09:46:45 -0800233 if (N > 1 || showListOnly) {
Colin Crossf45fa6b2012-03-26 12:38:26 -0700234 // first print a list of the current services
Steven Moreland3657ee52020-01-31 12:09:15 -0800235 std::cout << "Currently running services:" << std::endl;
Felipe Lemebbfd2b82016-02-03 11:16:27 -0800236
Colin Crossf45fa6b2012-03-26 12:38:26 -0700237 for (size_t i=0; i<N; i++) {
Felipe Leme343175a2016-08-02 18:57:37 -0700238 sp<IBinder> service = sm_->checkService(services[i]);
239
240 if (service != nullptr) {
Felipe Leme859aef62016-02-03 12:17:10 -0800241 bool skipped = IsSkipped(skippedServices, services[i]);
Steven Moreland3657ee52020-01-31 12:09:15 -0800242 std::cout << " " << services[i] << (skipped ? " (skipped)" : "") << std::endl;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700243 }
244 }
245 }
246
keunyoungcaad5552013-06-13 15:08:51 -0700247 if (showListOnly) {
248 return 0;
249 }
250
Josh Gao4b8f6f92016-02-29 16:20:34 -0800251 for (size_t i = 0; i < N; i++) {
Vishnu Naire4f61742017-12-21 08:30:28 -0800252 const String16& serviceName = services[i];
253 if (IsSkipped(skippedServices, serviceName)) continue;
Felipe Leme859aef62016-02-03 12:17:10 -0800254
Steven Moreland5a30d342019-10-08 13:53:28 -0700255 if (startDumpThread(type, serviceName, args) == OK) {
Vishnu Naire4f61742017-12-21 08:30:28 -0800256 bool addSeparator = (N > 1);
257 if (addSeparator) {
258 writeDumpHeader(STDOUT_FILENO, serviceName, priorityFlags);
Josh Gao4b8f6f92016-02-29 16:20:34 -0800259 }
Vishnu Naire4f61742017-12-21 08:30:28 -0800260 std::chrono::duration<double> elapsedDuration;
261 size_t bytesWritten = 0;
262 status_t status =
263 writeDump(STDOUT_FILENO, serviceName, std::chrono::milliseconds(timeoutArgMs),
264 asProto, elapsedDuration, bytesWritten);
Josh Gao4b8f6f92016-02-29 16:20:34 -0800265
Vishnu Naire4f61742017-12-21 08:30:28 -0800266 if (status == TIMED_OUT) {
Steven Moreland3657ee52020-01-31 12:09:15 -0800267 std::cout << std::endl
Vishnu Naire4f61742017-12-21 08:30:28 -0800268 << "*** SERVICE '" << serviceName << "' DUMP TIMEOUT (" << timeoutArgMs
Steven Moreland3657ee52020-01-31 12:09:15 -0800269 << "ms) EXPIRED ***" << std::endl
270 << std::endl;
Josh Gao4b8f6f92016-02-29 16:20:34 -0800271 }
272
Vishnu Naire4f61742017-12-21 08:30:28 -0800273 if (addSeparator) {
274 writeDumpFooter(STDOUT_FILENO, serviceName, elapsedDuration);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700275 }
Vishnu Naire4f61742017-12-21 08:30:28 -0800276 bool dumpComplete = (status == OK);
277 stopDumpThread(dumpComplete);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700278 }
279 }
280
281 return 0;
282}
Vishnu Naire4f61742017-12-21 08:30:28 -0800283
284Vector<String16> Dumpsys::listServices(int priorityFilterFlags, bool filterByProto) const {
285 Vector<String16> services = sm_->listServices(priorityFilterFlags);
286 services.sort(sort_func);
287 if (filterByProto) {
288 Vector<String16> protoServices = sm_->listServices(IServiceManager::DUMP_FLAG_PROTO);
289 protoServices.sort(sort_func);
290 Vector<String16> intersection;
291 std::set_intersection(services.begin(), services.end(), protoServices.begin(),
292 protoServices.end(), std::back_inserter(intersection));
293 services = std::move(intersection);
294 }
295 return services;
296}
297
Vishnu Naire97d6122018-01-18 13:58:56 -0800298void Dumpsys::setServiceArgs(Vector<String16>& args, bool asProto, int priorityFlags) {
Vishnu Nair64afc022018-02-01 15:29:34 -0800299 // Add proto flag if dumping service as proto.
Vishnu Naire4f61742017-12-21 08:30:28 -0800300 if (asProto) {
301 args.insertAt(String16(PriorityDumper::PROTO_ARG), 0);
302 }
Vishnu Nair64afc022018-02-01 15:29:34 -0800303
304 // Add -a (dump all) flag if dumping all services, dumping normal services or
305 // services not explicitly registered to a priority bucket (default services).
306 if ((priorityFlags == IServiceManager::DUMP_FLAG_PRIORITY_ALL) ||
307 (priorityFlags == IServiceManager::DUMP_FLAG_PRIORITY_NORMAL) ||
308 (priorityFlags == IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT)) {
309 args.insertAt(String16("-a"), 0);
310 }
311
312 // Add priority flags when dumping services registered to a specific priority bucket.
313 if ((priorityFlags == IServiceManager::DUMP_FLAG_PRIORITY_CRITICAL) ||
314 (priorityFlags == IServiceManager::DUMP_FLAG_PRIORITY_HIGH) ||
315 (priorityFlags == IServiceManager::DUMP_FLAG_PRIORITY_NORMAL)) {
Vishnu Naire4f61742017-12-21 08:30:28 -0800316 String16 priorityType = ConvertBitmaskToPriorityType(priorityFlags);
317 args.insertAt(String16(PriorityDumper::PRIORITY_ARG), 0);
318 args.insertAt(priorityType, 1);
319 }
320}
321
Steven Moreland5a30d342019-10-08 13:53:28 -0700322static status_t dumpPidToFd(const sp<IBinder>& service, const unique_fd& fd) {
323 pid_t pid;
324 status_t status = service->getDebugPid(&pid);
325 if (status != OK) {
326 return status;
327 }
328 WriteStringToFd(std::to_string(pid) + "\n", fd.get());
329 return OK;
330}
331
332status_t Dumpsys::startDumpThread(Type type, const String16& serviceName,
333 const Vector<String16>& args) {
Vishnu Naire4f61742017-12-21 08:30:28 -0800334 sp<IBinder> service = sm_->checkService(serviceName);
335 if (service == nullptr) {
Steven Moreland3657ee52020-01-31 12:09:15 -0800336 std::cerr << "Can't find service: " << serviceName << std::endl;
Vishnu Naire4f61742017-12-21 08:30:28 -0800337 return NAME_NOT_FOUND;
338 }
339
340 int sfd[2];
341 if (pipe(sfd) != 0) {
Steven Moreland3657ee52020-01-31 12:09:15 -0800342 std::cerr << "Failed to create pipe to dump service info for " << serviceName << ": "
343 << strerror(errno) << std::endl;
Vishnu Naire4f61742017-12-21 08:30:28 -0800344 return -errno;
345 }
346
347 redirectFd_ = unique_fd(sfd[0]);
348 unique_fd remote_end(sfd[1]);
349 sfd[0] = sfd[1] = -1;
350
351 // dump blocks until completion, so spawn a thread..
352 activeThread_ = std::thread([=, remote_end{std::move(remote_end)}]() mutable {
Steven Moreland5a30d342019-10-08 13:53:28 -0700353 status_t err = 0;
Vishnu Naire4f61742017-12-21 08:30:28 -0800354
Steven Moreland5a30d342019-10-08 13:53:28 -0700355 switch (type) {
356 case Type::DUMP:
357 err = service->dump(remote_end.get(), args);
358 break;
359 case Type::PID:
360 err = dumpPidToFd(service, remote_end);
361 break;
362 default:
Steven Moreland3657ee52020-01-31 12:09:15 -0800363 std::cerr << "Unknown dump type" << static_cast<int>(type) << std::endl;
Steven Moreland5a30d342019-10-08 13:53:28 -0700364 return;
365 }
366
367 if (err != OK) {
Steven Moreland3657ee52020-01-31 12:09:15 -0800368 std::cerr << "Error dumping service info status_t: " << statusToString(err) << " "
369 << serviceName << std::endl;
Vishnu Naire4f61742017-12-21 08:30:28 -0800370 }
371 });
372 return OK;
373}
374
375void Dumpsys::stopDumpThread(bool dumpComplete) {
376 if (dumpComplete) {
377 activeThread_.join();
378 } else {
379 activeThread_.detach();
380 }
381 /* close read end of the dump output redirection pipe */
382 redirectFd_.reset();
383}
384
385void Dumpsys::writeDumpHeader(int fd, const String16& serviceName, int priorityFlags) const {
386 std::string msg(
387 "----------------------------------------"
388 "---------------------------------------\n");
389 if (priorityFlags == IServiceManager::DUMP_FLAG_PRIORITY_ALL ||
Vishnu Nair64afc022018-02-01 15:29:34 -0800390 priorityFlags == IServiceManager::DUMP_FLAG_PRIORITY_NORMAL ||
391 priorityFlags == IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT) {
Vishnu Naire4f61742017-12-21 08:30:28 -0800392 StringAppendF(&msg, "DUMP OF SERVICE %s:\n", String8(serviceName).c_str());
393 } else {
394 String16 priorityType = ConvertBitmaskToPriorityType(priorityFlags);
395 StringAppendF(&msg, "DUMP OF SERVICE %s %s:\n", String8(priorityType).c_str(),
396 String8(serviceName).c_str());
397 }
398 WriteStringToFd(msg, fd);
399}
400
401status_t Dumpsys::writeDump(int fd, const String16& serviceName, std::chrono::milliseconds timeout,
402 bool asProto, std::chrono::duration<double>& elapsedDuration,
403 size_t& bytesWritten) const {
404 status_t status = OK;
405 size_t totalBytes = 0;
406 auto start = std::chrono::steady_clock::now();
407 auto end = start + timeout;
408
409 int serviceDumpFd = redirectFd_.get();
410 if (serviceDumpFd == -1) {
411 return INVALID_OPERATION;
412 }
413
414 struct pollfd pfd = {.fd = serviceDumpFd, .events = POLLIN};
415
416 while (true) {
417 // Wrap this in a lambda so that TEMP_FAILURE_RETRY recalculates the timeout.
418 auto time_left_ms = [end]() {
419 auto now = std::chrono::steady_clock::now();
420 auto diff = std::chrono::duration_cast<std::chrono::milliseconds>(end - now);
Chih-Hung Hsieh9f2d5312018-12-11 15:34:17 -0800421 return std::max(diff.count(), 0LL);
Vishnu Naire4f61742017-12-21 08:30:28 -0800422 };
423
424 int rc = TEMP_FAILURE_RETRY(poll(&pfd, 1, time_left_ms()));
425 if (rc < 0) {
Steven Moreland3657ee52020-01-31 12:09:15 -0800426 std::cerr << "Error in poll while dumping service " << serviceName << " : "
427 << strerror(errno) << std::endl;
Vishnu Naire4f61742017-12-21 08:30:28 -0800428 status = -errno;
429 break;
430 } else if (rc == 0) {
431 status = TIMED_OUT;
432 break;
433 }
434
435 char buf[4096];
436 rc = TEMP_FAILURE_RETRY(read(redirectFd_.get(), buf, sizeof(buf)));
437 if (rc < 0) {
Steven Moreland3657ee52020-01-31 12:09:15 -0800438 std::cerr << "Failed to read while dumping service " << serviceName << ": "
439 << strerror(errno) << std::endl;
Vishnu Naire4f61742017-12-21 08:30:28 -0800440 status = -errno;
441 break;
442 } else if (rc == 0) {
443 // EOF.
444 break;
445 }
446
447 if (!WriteFully(fd, buf, rc)) {
Steven Moreland3657ee52020-01-31 12:09:15 -0800448 std::cerr << "Failed to write while dumping service " << serviceName << ": "
449 << strerror(errno) << std::endl;
Vishnu Naire4f61742017-12-21 08:30:28 -0800450 status = -errno;
451 break;
452 }
453 totalBytes += rc;
454 }
455
456 if ((status == TIMED_OUT) && (!asProto)) {
457 std::string msg = StringPrintf("\n*** SERVICE '%s' DUMP TIMEOUT (%llums) EXPIRED ***\n\n",
458 String8(serviceName).string(), timeout.count());
459 WriteStringToFd(msg, fd);
460 }
461
462 elapsedDuration = std::chrono::steady_clock::now() - start;
463 bytesWritten = totalBytes;
464 return status;
465}
466
467void Dumpsys::writeDumpFooter(int fd, const String16& serviceName,
468 const std::chrono::duration<double>& elapsedDuration) const {
469 using std::chrono::system_clock;
470 const auto finish = system_clock::to_time_t(system_clock::now());
471 std::tm finish_tm;
472 localtime_r(&finish, &finish_tm);
473 std::stringstream oss;
474 oss << std::put_time(&finish_tm, "%Y-%m-%d %H:%M:%S");
475 std::string msg =
476 StringPrintf("--------- %.3fs was the duration of dumpsys %s, ending at: %s\n",
477 elapsedDuration.count(), String8(serviceName).string(), oss.str().c_str());
478 WriteStringToFd(msg, fd);
479}