1*38e8c45fSAndroid Build Coastguard Worker /*
2*38e8c45fSAndroid Build Coastguard Worker * Copyright (C) 2009 The Android Open Source Project
3*38e8c45fSAndroid Build Coastguard Worker *
4*38e8c45fSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*38e8c45fSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*38e8c45fSAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*38e8c45fSAndroid Build Coastguard Worker *
8*38e8c45fSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*38e8c45fSAndroid Build Coastguard Worker *
10*38e8c45fSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*38e8c45fSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*38e8c45fSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*38e8c45fSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*38e8c45fSAndroid Build Coastguard Worker * limitations under the License.
15*38e8c45fSAndroid Build Coastguard Worker */
16*38e8c45fSAndroid Build Coastguard Worker
17*38e8c45fSAndroid Build Coastguard Worker #include <algorithm>
18*38e8c45fSAndroid Build Coastguard Worker #include <chrono>
19*38e8c45fSAndroid Build Coastguard Worker #include <iomanip>
20*38e8c45fSAndroid Build Coastguard Worker #include <thread>
21*38e8c45fSAndroid Build Coastguard Worker
22*38e8c45fSAndroid Build Coastguard Worker #include <android-base/file.h>
23*38e8c45fSAndroid Build Coastguard Worker #include <android-base/stringprintf.h>
24*38e8c45fSAndroid Build Coastguard Worker #include <android-base/strings.h>
25*38e8c45fSAndroid Build Coastguard Worker #include <android-base/unique_fd.h>
26*38e8c45fSAndroid Build Coastguard Worker #include <binder/BpBinder.h>
27*38e8c45fSAndroid Build Coastguard Worker #include <binder/Parcel.h>
28*38e8c45fSAndroid Build Coastguard Worker #include <binder/ProcessState.h>
29*38e8c45fSAndroid Build Coastguard Worker #include <binder/Stability.h>
30*38e8c45fSAndroid Build Coastguard Worker #include <binder/TextOutput.h>
31*38e8c45fSAndroid Build Coastguard Worker #include <binderdebug/BinderDebug.h>
32*38e8c45fSAndroid Build Coastguard Worker #include <serviceutils/PriorityDumper.h>
33*38e8c45fSAndroid Build Coastguard Worker #include <utils/Log.h>
34*38e8c45fSAndroid Build Coastguard Worker #include <utils/Vector.h>
35*38e8c45fSAndroid Build Coastguard Worker
36*38e8c45fSAndroid Build Coastguard Worker #include <iostream>
37*38e8c45fSAndroid Build Coastguard Worker #include <fcntl.h>
38*38e8c45fSAndroid Build Coastguard Worker #include <getopt.h>
39*38e8c45fSAndroid Build Coastguard Worker #include <stdio.h>
40*38e8c45fSAndroid Build Coastguard Worker #include <stdlib.h>
41*38e8c45fSAndroid Build Coastguard Worker #include <string.h>
42*38e8c45fSAndroid Build Coastguard Worker #include <sys/poll.h>
43*38e8c45fSAndroid Build Coastguard Worker #include <sys/socket.h>
44*38e8c45fSAndroid Build Coastguard Worker #include <sys/time.h>
45*38e8c45fSAndroid Build Coastguard Worker #include <sys/types.h>
46*38e8c45fSAndroid Build Coastguard Worker #include <unistd.h>
47*38e8c45fSAndroid Build Coastguard Worker
48*38e8c45fSAndroid Build Coastguard Worker #include "dumpsys.h"
49*38e8c45fSAndroid Build Coastguard Worker
50*38e8c45fSAndroid Build Coastguard Worker using namespace android;
51*38e8c45fSAndroid Build Coastguard Worker using ::android::base::StringAppendF;
52*38e8c45fSAndroid Build Coastguard Worker using ::android::base::StringPrintf;
53*38e8c45fSAndroid Build Coastguard Worker using ::android::base::unique_fd;
54*38e8c45fSAndroid Build Coastguard Worker using ::android::base::WriteFully;
55*38e8c45fSAndroid Build Coastguard Worker using ::android::base::WriteStringToFd;
56*38e8c45fSAndroid Build Coastguard Worker
sort_func(const String16 * lhs,const String16 * rhs)57*38e8c45fSAndroid Build Coastguard Worker static int sort_func(const String16* lhs, const String16* rhs)
58*38e8c45fSAndroid Build Coastguard Worker {
59*38e8c45fSAndroid Build Coastguard Worker return lhs->compare(*rhs);
60*38e8c45fSAndroid Build Coastguard Worker }
61*38e8c45fSAndroid Build Coastguard Worker
usage()62*38e8c45fSAndroid Build Coastguard Worker static void usage() {
63*38e8c45fSAndroid Build Coastguard Worker fprintf(
64*38e8c45fSAndroid Build Coastguard Worker stderr,
65*38e8c45fSAndroid Build Coastguard Worker "usage: dumpsys\n"
66*38e8c45fSAndroid Build Coastguard Worker " To dump all services.\n"
67*38e8c45fSAndroid Build Coastguard Worker "or:\n"
68*38e8c45fSAndroid Build Coastguard Worker " dumpsys [-t TIMEOUT] [--priority LEVEL] [--clients] [--dump] [--pid] [--thread] "
69*38e8c45fSAndroid Build Coastguard Worker "[--help | "
70*38e8c45fSAndroid Build Coastguard Worker "-l | --skip SERVICES "
71*38e8c45fSAndroid Build Coastguard Worker "| SERVICE [ARGS]]\n"
72*38e8c45fSAndroid Build Coastguard Worker " --help: shows this help\n"
73*38e8c45fSAndroid Build Coastguard Worker " -l: only list services, do not dump them\n"
74*38e8c45fSAndroid Build Coastguard Worker " -t TIMEOUT_SEC: TIMEOUT to use in seconds instead of default 10 seconds\n"
75*38e8c45fSAndroid Build Coastguard Worker " -T TIMEOUT_MS: TIMEOUT to use in milliseconds instead of default 10 seconds\n"
76*38e8c45fSAndroid Build Coastguard Worker " --clients: dump client PIDs instead of usual dump\n"
77*38e8c45fSAndroid Build Coastguard Worker " --dump: ask the service to dump itself (this is the default)\n"
78*38e8c45fSAndroid Build Coastguard Worker " --pid: dump PID instead of usual dump\n"
79*38e8c45fSAndroid Build Coastguard Worker " --proto: filter services that support dumping data in proto format. Dumps\n"
80*38e8c45fSAndroid Build Coastguard Worker " will be in proto format.\n"
81*38e8c45fSAndroid Build Coastguard Worker " --priority LEVEL: filter services based on specified priority\n"
82*38e8c45fSAndroid Build Coastguard Worker " LEVEL must be one of CRITICAL | HIGH | NORMAL\n"
83*38e8c45fSAndroid Build Coastguard Worker " --skip SERVICES: dumps all services but SERVICES (comma-separated list)\n"
84*38e8c45fSAndroid Build Coastguard Worker " --stability: dump binder stability information instead of usual dump\n"
85*38e8c45fSAndroid Build Coastguard Worker " --thread: dump thread usage instead of usual dump\n"
86*38e8c45fSAndroid Build Coastguard Worker " SERVICE [ARGS]: dumps only service SERVICE, optionally passing ARGS to it\n");
87*38e8c45fSAndroid Build Coastguard Worker }
88*38e8c45fSAndroid Build Coastguard Worker
IsSkipped(const Vector<String16> & skipped,const String16 & service)89*38e8c45fSAndroid Build Coastguard Worker static bool IsSkipped(const Vector<String16>& skipped, const String16& service) {
90*38e8c45fSAndroid Build Coastguard Worker for (const auto& candidate : skipped) {
91*38e8c45fSAndroid Build Coastguard Worker if (candidate == service) {
92*38e8c45fSAndroid Build Coastguard Worker return true;
93*38e8c45fSAndroid Build Coastguard Worker }
94*38e8c45fSAndroid Build Coastguard Worker }
95*38e8c45fSAndroid Build Coastguard Worker return false;
96*38e8c45fSAndroid Build Coastguard Worker }
97*38e8c45fSAndroid Build Coastguard Worker
ConvertPriorityTypeToBitmask(const String16 & type,int & bitmask)98*38e8c45fSAndroid Build Coastguard Worker static bool ConvertPriorityTypeToBitmask(const String16& type, int& bitmask) {
99*38e8c45fSAndroid Build Coastguard Worker if (type == PriorityDumper::PRIORITY_ARG_CRITICAL) {
100*38e8c45fSAndroid Build Coastguard Worker bitmask = IServiceManager::DUMP_FLAG_PRIORITY_CRITICAL;
101*38e8c45fSAndroid Build Coastguard Worker return true;
102*38e8c45fSAndroid Build Coastguard Worker }
103*38e8c45fSAndroid Build Coastguard Worker if (type == PriorityDumper::PRIORITY_ARG_HIGH) {
104*38e8c45fSAndroid Build Coastguard Worker bitmask = IServiceManager::DUMP_FLAG_PRIORITY_HIGH;
105*38e8c45fSAndroid Build Coastguard Worker return true;
106*38e8c45fSAndroid Build Coastguard Worker }
107*38e8c45fSAndroid Build Coastguard Worker if (type == PriorityDumper::PRIORITY_ARG_NORMAL) {
108*38e8c45fSAndroid Build Coastguard Worker bitmask = IServiceManager::DUMP_FLAG_PRIORITY_NORMAL;
109*38e8c45fSAndroid Build Coastguard Worker return true;
110*38e8c45fSAndroid Build Coastguard Worker }
111*38e8c45fSAndroid Build Coastguard Worker return false;
112*38e8c45fSAndroid Build Coastguard Worker }
113*38e8c45fSAndroid Build Coastguard Worker
ConvertBitmaskToPriorityType(int bitmask)114*38e8c45fSAndroid Build Coastguard Worker String16 ConvertBitmaskToPriorityType(int bitmask) {
115*38e8c45fSAndroid Build Coastguard Worker if (bitmask == IServiceManager::DUMP_FLAG_PRIORITY_CRITICAL) {
116*38e8c45fSAndroid Build Coastguard Worker return String16(PriorityDumper::PRIORITY_ARG_CRITICAL);
117*38e8c45fSAndroid Build Coastguard Worker }
118*38e8c45fSAndroid Build Coastguard Worker if (bitmask == IServiceManager::DUMP_FLAG_PRIORITY_HIGH) {
119*38e8c45fSAndroid Build Coastguard Worker return String16(PriorityDumper::PRIORITY_ARG_HIGH);
120*38e8c45fSAndroid Build Coastguard Worker }
121*38e8c45fSAndroid Build Coastguard Worker if (bitmask == IServiceManager::DUMP_FLAG_PRIORITY_NORMAL) {
122*38e8c45fSAndroid Build Coastguard Worker return String16(PriorityDumper::PRIORITY_ARG_NORMAL);
123*38e8c45fSAndroid Build Coastguard Worker }
124*38e8c45fSAndroid Build Coastguard Worker return String16("");
125*38e8c45fSAndroid Build Coastguard Worker }
126*38e8c45fSAndroid Build Coastguard Worker
main(int argc,char * const argv[])127*38e8c45fSAndroid Build Coastguard Worker int Dumpsys::main(int argc, char* const argv[]) {
128*38e8c45fSAndroid Build Coastguard Worker Vector<String16> services;
129*38e8c45fSAndroid Build Coastguard Worker Vector<String16> args;
130*38e8c45fSAndroid Build Coastguard Worker String16 priorityType;
131*38e8c45fSAndroid Build Coastguard Worker Vector<String16> skippedServices;
132*38e8c45fSAndroid Build Coastguard Worker Vector<String16> protoServices;
133*38e8c45fSAndroid Build Coastguard Worker bool showListOnly = false;
134*38e8c45fSAndroid Build Coastguard Worker bool skipServices = false;
135*38e8c45fSAndroid Build Coastguard Worker bool asProto = false;
136*38e8c45fSAndroid Build Coastguard Worker int dumpTypeFlags = 0;
137*38e8c45fSAndroid Build Coastguard Worker int timeoutArgMs = 10000;
138*38e8c45fSAndroid Build Coastguard Worker int priorityFlags = IServiceManager::DUMP_FLAG_PRIORITY_ALL;
139*38e8c45fSAndroid Build Coastguard Worker static struct option longOptions[] = {
140*38e8c45fSAndroid Build Coastguard Worker {"help", no_argument, 0, 0}, {"clients", no_argument, 0, 0},
141*38e8c45fSAndroid Build Coastguard Worker {"dump", no_argument, 0, 0}, {"pid", no_argument, 0, 0},
142*38e8c45fSAndroid Build Coastguard Worker {"priority", required_argument, 0, 0}, {"proto", no_argument, 0, 0},
143*38e8c45fSAndroid Build Coastguard Worker {"skip", no_argument, 0, 0}, {"stability", no_argument, 0, 0},
144*38e8c45fSAndroid Build Coastguard Worker {"thread", no_argument, 0, 0}, {0, 0, 0, 0}};
145*38e8c45fSAndroid Build Coastguard Worker
146*38e8c45fSAndroid Build Coastguard Worker // Must reset optind, otherwise subsequent calls will fail (wouldn't happen on main.cpp, but
147*38e8c45fSAndroid Build Coastguard Worker // happens on test cases).
148*38e8c45fSAndroid Build Coastguard Worker optind = 1;
149*38e8c45fSAndroid Build Coastguard Worker while (1) {
150*38e8c45fSAndroid Build Coastguard Worker int c;
151*38e8c45fSAndroid Build Coastguard Worker int optionIndex = 0;
152*38e8c45fSAndroid Build Coastguard Worker
153*38e8c45fSAndroid Build Coastguard Worker c = getopt_long(argc, argv, "+t:T:l", longOptions, &optionIndex);
154*38e8c45fSAndroid Build Coastguard Worker
155*38e8c45fSAndroid Build Coastguard Worker if (c == -1) {
156*38e8c45fSAndroid Build Coastguard Worker break;
157*38e8c45fSAndroid Build Coastguard Worker }
158*38e8c45fSAndroid Build Coastguard Worker
159*38e8c45fSAndroid Build Coastguard Worker switch (c) {
160*38e8c45fSAndroid Build Coastguard Worker case 0:
161*38e8c45fSAndroid Build Coastguard Worker if (!strcmp(longOptions[optionIndex].name, "skip")) {
162*38e8c45fSAndroid Build Coastguard Worker skipServices = true;
163*38e8c45fSAndroid Build Coastguard Worker } else if (!strcmp(longOptions[optionIndex].name, "proto")) {
164*38e8c45fSAndroid Build Coastguard Worker asProto = true;
165*38e8c45fSAndroid Build Coastguard Worker } else if (!strcmp(longOptions[optionIndex].name, "help")) {
166*38e8c45fSAndroid Build Coastguard Worker usage();
167*38e8c45fSAndroid Build Coastguard Worker return 0;
168*38e8c45fSAndroid Build Coastguard Worker } else if (!strcmp(longOptions[optionIndex].name, "priority")) {
169*38e8c45fSAndroid Build Coastguard Worker priorityType = String16(String8(optarg));
170*38e8c45fSAndroid Build Coastguard Worker if (!ConvertPriorityTypeToBitmask(priorityType, priorityFlags)) {
171*38e8c45fSAndroid Build Coastguard Worker fprintf(stderr, "\n");
172*38e8c45fSAndroid Build Coastguard Worker usage();
173*38e8c45fSAndroid Build Coastguard Worker return -1;
174*38e8c45fSAndroid Build Coastguard Worker }
175*38e8c45fSAndroid Build Coastguard Worker } else if (!strcmp(longOptions[optionIndex].name, "dump")) {
176*38e8c45fSAndroid Build Coastguard Worker dumpTypeFlags |= TYPE_DUMP;
177*38e8c45fSAndroid Build Coastguard Worker } else if (!strcmp(longOptions[optionIndex].name, "pid")) {
178*38e8c45fSAndroid Build Coastguard Worker dumpTypeFlags |= TYPE_PID;
179*38e8c45fSAndroid Build Coastguard Worker } else if (!strcmp(longOptions[optionIndex].name, "stability")) {
180*38e8c45fSAndroid Build Coastguard Worker dumpTypeFlags |= TYPE_STABILITY;
181*38e8c45fSAndroid Build Coastguard Worker } else if (!strcmp(longOptions[optionIndex].name, "thread")) {
182*38e8c45fSAndroid Build Coastguard Worker dumpTypeFlags |= TYPE_THREAD;
183*38e8c45fSAndroid Build Coastguard Worker } else if (!strcmp(longOptions[optionIndex].name, "clients")) {
184*38e8c45fSAndroid Build Coastguard Worker dumpTypeFlags |= TYPE_CLIENTS;
185*38e8c45fSAndroid Build Coastguard Worker }
186*38e8c45fSAndroid Build Coastguard Worker break;
187*38e8c45fSAndroid Build Coastguard Worker
188*38e8c45fSAndroid Build Coastguard Worker case 't':
189*38e8c45fSAndroid Build Coastguard Worker {
190*38e8c45fSAndroid Build Coastguard Worker char* endptr;
191*38e8c45fSAndroid Build Coastguard Worker timeoutArgMs = strtol(optarg, &endptr, 10);
192*38e8c45fSAndroid Build Coastguard Worker timeoutArgMs = timeoutArgMs * 1000;
193*38e8c45fSAndroid Build Coastguard Worker if (*endptr != '\0' || timeoutArgMs <= 0) {
194*38e8c45fSAndroid Build Coastguard Worker fprintf(stderr, "Error: invalid timeout(seconds) number: '%s'\n", optarg);
195*38e8c45fSAndroid Build Coastguard Worker return -1;
196*38e8c45fSAndroid Build Coastguard Worker }
197*38e8c45fSAndroid Build Coastguard Worker }
198*38e8c45fSAndroid Build Coastguard Worker break;
199*38e8c45fSAndroid Build Coastguard Worker
200*38e8c45fSAndroid Build Coastguard Worker case 'T':
201*38e8c45fSAndroid Build Coastguard Worker {
202*38e8c45fSAndroid Build Coastguard Worker char* endptr;
203*38e8c45fSAndroid Build Coastguard Worker timeoutArgMs = strtol(optarg, &endptr, 10);
204*38e8c45fSAndroid Build Coastguard Worker if (*endptr != '\0' || timeoutArgMs <= 0) {
205*38e8c45fSAndroid Build Coastguard Worker fprintf(stderr, "Error: invalid timeout(milliseconds) number: '%s'\n", optarg);
206*38e8c45fSAndroid Build Coastguard Worker return -1;
207*38e8c45fSAndroid Build Coastguard Worker }
208*38e8c45fSAndroid Build Coastguard Worker }
209*38e8c45fSAndroid Build Coastguard Worker break;
210*38e8c45fSAndroid Build Coastguard Worker
211*38e8c45fSAndroid Build Coastguard Worker case 'l':
212*38e8c45fSAndroid Build Coastguard Worker showListOnly = true;
213*38e8c45fSAndroid Build Coastguard Worker break;
214*38e8c45fSAndroid Build Coastguard Worker
215*38e8c45fSAndroid Build Coastguard Worker default:
216*38e8c45fSAndroid Build Coastguard Worker fprintf(stderr, "\n");
217*38e8c45fSAndroid Build Coastguard Worker usage();
218*38e8c45fSAndroid Build Coastguard Worker return -1;
219*38e8c45fSAndroid Build Coastguard Worker }
220*38e8c45fSAndroid Build Coastguard Worker }
221*38e8c45fSAndroid Build Coastguard Worker
222*38e8c45fSAndroid Build Coastguard Worker if (dumpTypeFlags == 0) {
223*38e8c45fSAndroid Build Coastguard Worker dumpTypeFlags = TYPE_DUMP;
224*38e8c45fSAndroid Build Coastguard Worker }
225*38e8c45fSAndroid Build Coastguard Worker
226*38e8c45fSAndroid Build Coastguard Worker for (int i = optind; i < argc; i++) {
227*38e8c45fSAndroid Build Coastguard Worker if (skipServices) {
228*38e8c45fSAndroid Build Coastguard Worker skippedServices.add(String16(argv[i]));
229*38e8c45fSAndroid Build Coastguard Worker } else {
230*38e8c45fSAndroid Build Coastguard Worker if (i == optind) {
231*38e8c45fSAndroid Build Coastguard Worker services.add(String16(argv[i]));
232*38e8c45fSAndroid Build Coastguard Worker } else {
233*38e8c45fSAndroid Build Coastguard Worker const String16 arg(argv[i]);
234*38e8c45fSAndroid Build Coastguard Worker args.add(arg);
235*38e8c45fSAndroid Build Coastguard Worker // For backward compatible, if the proto argument is passed to the service, the
236*38e8c45fSAndroid Build Coastguard Worker // dump request is also considered to use proto.
237*38e8c45fSAndroid Build Coastguard Worker if (!asProto && !arg.compare(String16(PriorityDumper::PROTO_ARG))) {
238*38e8c45fSAndroid Build Coastguard Worker asProto = true;
239*38e8c45fSAndroid Build Coastguard Worker }
240*38e8c45fSAndroid Build Coastguard Worker }
241*38e8c45fSAndroid Build Coastguard Worker }
242*38e8c45fSAndroid Build Coastguard Worker }
243*38e8c45fSAndroid Build Coastguard Worker
244*38e8c45fSAndroid Build Coastguard Worker if ((skipServices && skippedServices.empty()) ||
245*38e8c45fSAndroid Build Coastguard Worker (showListOnly && (!services.empty() || !skippedServices.empty()))) {
246*38e8c45fSAndroid Build Coastguard Worker usage();
247*38e8c45fSAndroid Build Coastguard Worker return -1;
248*38e8c45fSAndroid Build Coastguard Worker }
249*38e8c45fSAndroid Build Coastguard Worker
250*38e8c45fSAndroid Build Coastguard Worker if (services.empty() || showListOnly) {
251*38e8c45fSAndroid Build Coastguard Worker services = listServices(priorityFlags, asProto);
252*38e8c45fSAndroid Build Coastguard Worker setServiceArgs(args, asProto, priorityFlags);
253*38e8c45fSAndroid Build Coastguard Worker }
254*38e8c45fSAndroid Build Coastguard Worker
255*38e8c45fSAndroid Build Coastguard Worker const size_t N = services.size();
256*38e8c45fSAndroid Build Coastguard Worker if (N > 1 || showListOnly) {
257*38e8c45fSAndroid Build Coastguard Worker // first print a list of the current services
258*38e8c45fSAndroid Build Coastguard Worker std::cout << "Currently running services:" << std::endl;
259*38e8c45fSAndroid Build Coastguard Worker
260*38e8c45fSAndroid Build Coastguard Worker for (size_t i=0; i<N; i++) {
261*38e8c45fSAndroid Build Coastguard Worker sp<IBinder> service = sm_->checkService(services[i]);
262*38e8c45fSAndroid Build Coastguard Worker
263*38e8c45fSAndroid Build Coastguard Worker if (service != nullptr) {
264*38e8c45fSAndroid Build Coastguard Worker bool skipped = IsSkipped(skippedServices, services[i]);
265*38e8c45fSAndroid Build Coastguard Worker std::cout << " " << services[i] << (skipped ? " (skipped)" : "") << std::endl;
266*38e8c45fSAndroid Build Coastguard Worker }
267*38e8c45fSAndroid Build Coastguard Worker }
268*38e8c45fSAndroid Build Coastguard Worker }
269*38e8c45fSAndroid Build Coastguard Worker
270*38e8c45fSAndroid Build Coastguard Worker if (showListOnly) {
271*38e8c45fSAndroid Build Coastguard Worker return 0;
272*38e8c45fSAndroid Build Coastguard Worker }
273*38e8c45fSAndroid Build Coastguard Worker
274*38e8c45fSAndroid Build Coastguard Worker for (size_t i = 0; i < N; i++) {
275*38e8c45fSAndroid Build Coastguard Worker const String16& serviceName = services[i];
276*38e8c45fSAndroid Build Coastguard Worker if (IsSkipped(skippedServices, serviceName)) continue;
277*38e8c45fSAndroid Build Coastguard Worker
278*38e8c45fSAndroid Build Coastguard Worker if (startDumpThread(dumpTypeFlags, serviceName, args) == OK) {
279*38e8c45fSAndroid Build Coastguard Worker bool addSeparator = (N > 1);
280*38e8c45fSAndroid Build Coastguard Worker if (addSeparator) {
281*38e8c45fSAndroid Build Coastguard Worker writeDumpHeader(STDOUT_FILENO, serviceName, priorityFlags);
282*38e8c45fSAndroid Build Coastguard Worker }
283*38e8c45fSAndroid Build Coastguard Worker std::chrono::duration<double> elapsedDuration;
284*38e8c45fSAndroid Build Coastguard Worker size_t bytesWritten = 0;
285*38e8c45fSAndroid Build Coastguard Worker status_t status =
286*38e8c45fSAndroid Build Coastguard Worker writeDump(STDOUT_FILENO, serviceName, std::chrono::milliseconds(timeoutArgMs),
287*38e8c45fSAndroid Build Coastguard Worker asProto, elapsedDuration, bytesWritten);
288*38e8c45fSAndroid Build Coastguard Worker
289*38e8c45fSAndroid Build Coastguard Worker if (status == TIMED_OUT) {
290*38e8c45fSAndroid Build Coastguard Worker std::cout << std::endl
291*38e8c45fSAndroid Build Coastguard Worker << "*** SERVICE '" << serviceName << "' DUMP TIMEOUT (" << timeoutArgMs
292*38e8c45fSAndroid Build Coastguard Worker << "ms) EXPIRED ***" << std::endl
293*38e8c45fSAndroid Build Coastguard Worker << std::endl;
294*38e8c45fSAndroid Build Coastguard Worker }
295*38e8c45fSAndroid Build Coastguard Worker
296*38e8c45fSAndroid Build Coastguard Worker if (addSeparator) {
297*38e8c45fSAndroid Build Coastguard Worker writeDumpFooter(STDOUT_FILENO, serviceName, elapsedDuration);
298*38e8c45fSAndroid Build Coastguard Worker }
299*38e8c45fSAndroid Build Coastguard Worker bool dumpComplete = (status == OK);
300*38e8c45fSAndroid Build Coastguard Worker stopDumpThread(dumpComplete);
301*38e8c45fSAndroid Build Coastguard Worker }
302*38e8c45fSAndroid Build Coastguard Worker }
303*38e8c45fSAndroid Build Coastguard Worker
304*38e8c45fSAndroid Build Coastguard Worker return 0;
305*38e8c45fSAndroid Build Coastguard Worker }
306*38e8c45fSAndroid Build Coastguard Worker
listServices(int priorityFilterFlags,bool filterByProto) const307*38e8c45fSAndroid Build Coastguard Worker Vector<String16> Dumpsys::listServices(int priorityFilterFlags, bool filterByProto) const {
308*38e8c45fSAndroid Build Coastguard Worker Vector<String16> services = sm_->listServices(priorityFilterFlags);
309*38e8c45fSAndroid Build Coastguard Worker services.sort(sort_func);
310*38e8c45fSAndroid Build Coastguard Worker if (filterByProto) {
311*38e8c45fSAndroid Build Coastguard Worker Vector<String16> protoServices = sm_->listServices(IServiceManager::DUMP_FLAG_PROTO);
312*38e8c45fSAndroid Build Coastguard Worker protoServices.sort(sort_func);
313*38e8c45fSAndroid Build Coastguard Worker Vector<String16> intersection;
314*38e8c45fSAndroid Build Coastguard Worker std::set_intersection(services.begin(), services.end(), protoServices.begin(),
315*38e8c45fSAndroid Build Coastguard Worker protoServices.end(), std::back_inserter(intersection));
316*38e8c45fSAndroid Build Coastguard Worker services = std::move(intersection);
317*38e8c45fSAndroid Build Coastguard Worker }
318*38e8c45fSAndroid Build Coastguard Worker return services;
319*38e8c45fSAndroid Build Coastguard Worker }
320*38e8c45fSAndroid Build Coastguard Worker
setServiceArgs(Vector<String16> & args,bool asProto,int priorityFlags)321*38e8c45fSAndroid Build Coastguard Worker void Dumpsys::setServiceArgs(Vector<String16>& args, bool asProto, int priorityFlags) {
322*38e8c45fSAndroid Build Coastguard Worker // Add proto flag if dumping service as proto.
323*38e8c45fSAndroid Build Coastguard Worker if (asProto) {
324*38e8c45fSAndroid Build Coastguard Worker args.insertAt(String16(PriorityDumper::PROTO_ARG), 0);
325*38e8c45fSAndroid Build Coastguard Worker }
326*38e8c45fSAndroid Build Coastguard Worker
327*38e8c45fSAndroid Build Coastguard Worker // Add -a (dump all) flag if dumping all services, dumping normal services or
328*38e8c45fSAndroid Build Coastguard Worker // services not explicitly registered to a priority bucket (default services).
329*38e8c45fSAndroid Build Coastguard Worker if ((priorityFlags == IServiceManager::DUMP_FLAG_PRIORITY_ALL) ||
330*38e8c45fSAndroid Build Coastguard Worker (priorityFlags == IServiceManager::DUMP_FLAG_PRIORITY_NORMAL) ||
331*38e8c45fSAndroid Build Coastguard Worker (priorityFlags == IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT)) {
332*38e8c45fSAndroid Build Coastguard Worker args.insertAt(String16("-a"), 0);
333*38e8c45fSAndroid Build Coastguard Worker }
334*38e8c45fSAndroid Build Coastguard Worker
335*38e8c45fSAndroid Build Coastguard Worker // Add priority flags when dumping services registered to a specific priority bucket.
336*38e8c45fSAndroid Build Coastguard Worker if ((priorityFlags == IServiceManager::DUMP_FLAG_PRIORITY_CRITICAL) ||
337*38e8c45fSAndroid Build Coastguard Worker (priorityFlags == IServiceManager::DUMP_FLAG_PRIORITY_HIGH) ||
338*38e8c45fSAndroid Build Coastguard Worker (priorityFlags == IServiceManager::DUMP_FLAG_PRIORITY_NORMAL)) {
339*38e8c45fSAndroid Build Coastguard Worker String16 priorityType = ConvertBitmaskToPriorityType(priorityFlags);
340*38e8c45fSAndroid Build Coastguard Worker args.insertAt(String16(PriorityDumper::PRIORITY_ARG), 0);
341*38e8c45fSAndroid Build Coastguard Worker args.insertAt(priorityType, 1);
342*38e8c45fSAndroid Build Coastguard Worker }
343*38e8c45fSAndroid Build Coastguard Worker }
344*38e8c45fSAndroid Build Coastguard Worker
dumpPidToFd(const sp<IBinder> & service,const unique_fd & fd,bool exclusive)345*38e8c45fSAndroid Build Coastguard Worker static status_t dumpPidToFd(const sp<IBinder>& service, const unique_fd& fd, bool exclusive) {
346*38e8c45fSAndroid Build Coastguard Worker pid_t pid;
347*38e8c45fSAndroid Build Coastguard Worker status_t status = service->getDebugPid(&pid);
348*38e8c45fSAndroid Build Coastguard Worker if (status != OK) {
349*38e8c45fSAndroid Build Coastguard Worker return status;
350*38e8c45fSAndroid Build Coastguard Worker }
351*38e8c45fSAndroid Build Coastguard Worker if (!exclusive) {
352*38e8c45fSAndroid Build Coastguard Worker WriteStringToFd("Service host process PID: ", fd.get());
353*38e8c45fSAndroid Build Coastguard Worker }
354*38e8c45fSAndroid Build Coastguard Worker WriteStringToFd(std::to_string(pid) + "\n", fd.get());
355*38e8c45fSAndroid Build Coastguard Worker return OK;
356*38e8c45fSAndroid Build Coastguard Worker }
357*38e8c45fSAndroid Build Coastguard Worker
dumpStabilityToFd(const sp<IBinder> & service,const unique_fd & fd)358*38e8c45fSAndroid Build Coastguard Worker static status_t dumpStabilityToFd(const sp<IBinder>& service, const unique_fd& fd) {
359*38e8c45fSAndroid Build Coastguard Worker WriteStringToFd("Stability: " + internal::Stability::debugToString(service) + "\n", fd);
360*38e8c45fSAndroid Build Coastguard Worker return OK;
361*38e8c45fSAndroid Build Coastguard Worker }
362*38e8c45fSAndroid Build Coastguard Worker
dumpThreadsToFd(const sp<IBinder> & service,const unique_fd & fd)363*38e8c45fSAndroid Build Coastguard Worker static status_t dumpThreadsToFd(const sp<IBinder>& service, const unique_fd& fd) {
364*38e8c45fSAndroid Build Coastguard Worker pid_t pid;
365*38e8c45fSAndroid Build Coastguard Worker status_t status = service->getDebugPid(&pid);
366*38e8c45fSAndroid Build Coastguard Worker if (status != OK) {
367*38e8c45fSAndroid Build Coastguard Worker return status;
368*38e8c45fSAndroid Build Coastguard Worker }
369*38e8c45fSAndroid Build Coastguard Worker BinderPidInfo pidInfo;
370*38e8c45fSAndroid Build Coastguard Worker status = getBinderPidInfo(BinderDebugContext::BINDER, pid, &pidInfo);
371*38e8c45fSAndroid Build Coastguard Worker if (status != OK) {
372*38e8c45fSAndroid Build Coastguard Worker return status;
373*38e8c45fSAndroid Build Coastguard Worker }
374*38e8c45fSAndroid Build Coastguard Worker WriteStringToFd("Threads in use: " + std::to_string(pidInfo.threadUsage) + "/" +
375*38e8c45fSAndroid Build Coastguard Worker std::to_string(pidInfo.threadCount) + "\n",
376*38e8c45fSAndroid Build Coastguard Worker fd.get());
377*38e8c45fSAndroid Build Coastguard Worker return OK;
378*38e8c45fSAndroid Build Coastguard Worker }
379*38e8c45fSAndroid Build Coastguard Worker
dumpClientsToFd(const sp<IBinder> & service,const unique_fd & fd)380*38e8c45fSAndroid Build Coastguard Worker static status_t dumpClientsToFd(const sp<IBinder>& service, const unique_fd& fd) {
381*38e8c45fSAndroid Build Coastguard Worker std::string clientPids;
382*38e8c45fSAndroid Build Coastguard Worker const auto remoteBinder = service->remoteBinder();
383*38e8c45fSAndroid Build Coastguard Worker if (remoteBinder == nullptr) {
384*38e8c45fSAndroid Build Coastguard Worker WriteStringToFd("Client PIDs are not available for local binders.\n", fd.get());
385*38e8c45fSAndroid Build Coastguard Worker return OK;
386*38e8c45fSAndroid Build Coastguard Worker }
387*38e8c45fSAndroid Build Coastguard Worker const auto handle = remoteBinder->getDebugBinderHandle();
388*38e8c45fSAndroid Build Coastguard Worker if (handle == std::nullopt) {
389*38e8c45fSAndroid Build Coastguard Worker return OK;
390*38e8c45fSAndroid Build Coastguard Worker }
391*38e8c45fSAndroid Build Coastguard Worker std::vector<pid_t> pids;
392*38e8c45fSAndroid Build Coastguard Worker pid_t myPid = getpid();
393*38e8c45fSAndroid Build Coastguard Worker pid_t servicePid;
394*38e8c45fSAndroid Build Coastguard Worker status_t status = service->getDebugPid(&servicePid);
395*38e8c45fSAndroid Build Coastguard Worker if (status != OK) {
396*38e8c45fSAndroid Build Coastguard Worker return status;
397*38e8c45fSAndroid Build Coastguard Worker }
398*38e8c45fSAndroid Build Coastguard Worker status =
399*38e8c45fSAndroid Build Coastguard Worker getBinderClientPids(BinderDebugContext::BINDER, myPid, servicePid, handle.value(), &pids);
400*38e8c45fSAndroid Build Coastguard Worker if (status != OK) {
401*38e8c45fSAndroid Build Coastguard Worker return status;
402*38e8c45fSAndroid Build Coastguard Worker }
403*38e8c45fSAndroid Build Coastguard Worker pids.erase(std::remove_if(pids.begin(), pids.end(), [&](pid_t pid) { return pid == myPid; }),
404*38e8c45fSAndroid Build Coastguard Worker pids.end());
405*38e8c45fSAndroid Build Coastguard Worker WriteStringToFd("Client PIDs: " + ::android::base::Join(pids, ", ") + "\n", fd.get());
406*38e8c45fSAndroid Build Coastguard Worker return OK;
407*38e8c45fSAndroid Build Coastguard Worker }
408*38e8c45fSAndroid Build Coastguard Worker
reportDumpError(const String16 & serviceName,status_t error,const char * context)409*38e8c45fSAndroid Build Coastguard Worker static void reportDumpError(const String16& serviceName, status_t error, const char* context) {
410*38e8c45fSAndroid Build Coastguard Worker if (error == OK) return;
411*38e8c45fSAndroid Build Coastguard Worker
412*38e8c45fSAndroid Build Coastguard Worker std::cerr << "Error with service '" << serviceName << "' while " << context << ": "
413*38e8c45fSAndroid Build Coastguard Worker << statusToString(error) << std::endl;
414*38e8c45fSAndroid Build Coastguard Worker }
415*38e8c45fSAndroid Build Coastguard Worker
startDumpThread(int dumpTypeFlags,const String16 & serviceName,const Vector<String16> & args)416*38e8c45fSAndroid Build Coastguard Worker status_t Dumpsys::startDumpThread(int dumpTypeFlags, const String16& serviceName,
417*38e8c45fSAndroid Build Coastguard Worker const Vector<String16>& args) {
418*38e8c45fSAndroid Build Coastguard Worker sp<IBinder> service = sm_->checkService(serviceName);
419*38e8c45fSAndroid Build Coastguard Worker if (service == nullptr) {
420*38e8c45fSAndroid Build Coastguard Worker std::cerr << "Can't find service: " << serviceName << std::endl;
421*38e8c45fSAndroid Build Coastguard Worker return NAME_NOT_FOUND;
422*38e8c45fSAndroid Build Coastguard Worker }
423*38e8c45fSAndroid Build Coastguard Worker
424*38e8c45fSAndroid Build Coastguard Worker int sfd[2];
425*38e8c45fSAndroid Build Coastguard Worker if (pipe(sfd) != 0) {
426*38e8c45fSAndroid Build Coastguard Worker std::cerr << "Failed to create pipe to dump service info for " << serviceName << ": "
427*38e8c45fSAndroid Build Coastguard Worker << strerror(errno) << std::endl;
428*38e8c45fSAndroid Build Coastguard Worker return -errno;
429*38e8c45fSAndroid Build Coastguard Worker }
430*38e8c45fSAndroid Build Coastguard Worker
431*38e8c45fSAndroid Build Coastguard Worker redirectFd_ = unique_fd(sfd[0]);
432*38e8c45fSAndroid Build Coastguard Worker unique_fd remote_end(sfd[1]);
433*38e8c45fSAndroid Build Coastguard Worker sfd[0] = sfd[1] = -1;
434*38e8c45fSAndroid Build Coastguard Worker
435*38e8c45fSAndroid Build Coastguard Worker // dump blocks until completion, so spawn a thread..
436*38e8c45fSAndroid Build Coastguard Worker activeThread_ = std::thread([=, remote_end{std::move(remote_end)}]() mutable {
437*38e8c45fSAndroid Build Coastguard Worker if (dumpTypeFlags & TYPE_PID) {
438*38e8c45fSAndroid Build Coastguard Worker status_t err = dumpPidToFd(service, remote_end, dumpTypeFlags == TYPE_PID);
439*38e8c45fSAndroid Build Coastguard Worker reportDumpError(serviceName, err, "dumping PID");
440*38e8c45fSAndroid Build Coastguard Worker }
441*38e8c45fSAndroid Build Coastguard Worker if (dumpTypeFlags & TYPE_STABILITY) {
442*38e8c45fSAndroid Build Coastguard Worker status_t err = dumpStabilityToFd(service, remote_end);
443*38e8c45fSAndroid Build Coastguard Worker reportDumpError(serviceName, err, "dumping stability");
444*38e8c45fSAndroid Build Coastguard Worker }
445*38e8c45fSAndroid Build Coastguard Worker if (dumpTypeFlags & TYPE_THREAD) {
446*38e8c45fSAndroid Build Coastguard Worker status_t err = dumpThreadsToFd(service, remote_end);
447*38e8c45fSAndroid Build Coastguard Worker reportDumpError(serviceName, err, "dumping thread info");
448*38e8c45fSAndroid Build Coastguard Worker }
449*38e8c45fSAndroid Build Coastguard Worker if (dumpTypeFlags & TYPE_CLIENTS) {
450*38e8c45fSAndroid Build Coastguard Worker status_t err = dumpClientsToFd(service, remote_end);
451*38e8c45fSAndroid Build Coastguard Worker reportDumpError(serviceName, err, "dumping clients info");
452*38e8c45fSAndroid Build Coastguard Worker }
453*38e8c45fSAndroid Build Coastguard Worker
454*38e8c45fSAndroid Build Coastguard Worker // other types always act as a header, this is usually longer
455*38e8c45fSAndroid Build Coastguard Worker if (dumpTypeFlags & TYPE_DUMP) {
456*38e8c45fSAndroid Build Coastguard Worker status_t err = service->dump(remote_end.get(), args);
457*38e8c45fSAndroid Build Coastguard Worker reportDumpError(serviceName, err, "dumping");
458*38e8c45fSAndroid Build Coastguard Worker }
459*38e8c45fSAndroid Build Coastguard Worker });
460*38e8c45fSAndroid Build Coastguard Worker return OK;
461*38e8c45fSAndroid Build Coastguard Worker }
462*38e8c45fSAndroid Build Coastguard Worker
stopDumpThread(bool dumpComplete)463*38e8c45fSAndroid Build Coastguard Worker void Dumpsys::stopDumpThread(bool dumpComplete) {
464*38e8c45fSAndroid Build Coastguard Worker if (dumpComplete) {
465*38e8c45fSAndroid Build Coastguard Worker activeThread_.join();
466*38e8c45fSAndroid Build Coastguard Worker } else {
467*38e8c45fSAndroid Build Coastguard Worker activeThread_.detach();
468*38e8c45fSAndroid Build Coastguard Worker }
469*38e8c45fSAndroid Build Coastguard Worker /* close read end of the dump output redirection pipe */
470*38e8c45fSAndroid Build Coastguard Worker redirectFd_.reset();
471*38e8c45fSAndroid Build Coastguard Worker }
472*38e8c45fSAndroid Build Coastguard Worker
writeDumpHeader(int fd,const String16 & serviceName,int priorityFlags) const473*38e8c45fSAndroid Build Coastguard Worker void Dumpsys::writeDumpHeader(int fd, const String16& serviceName, int priorityFlags) const {
474*38e8c45fSAndroid Build Coastguard Worker std::string msg(
475*38e8c45fSAndroid Build Coastguard Worker "----------------------------------------"
476*38e8c45fSAndroid Build Coastguard Worker "---------------------------------------\n");
477*38e8c45fSAndroid Build Coastguard Worker if (priorityFlags == IServiceManager::DUMP_FLAG_PRIORITY_ALL ||
478*38e8c45fSAndroid Build Coastguard Worker priorityFlags == IServiceManager::DUMP_FLAG_PRIORITY_NORMAL ||
479*38e8c45fSAndroid Build Coastguard Worker priorityFlags == IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT) {
480*38e8c45fSAndroid Build Coastguard Worker StringAppendF(&msg, "DUMP OF SERVICE %s:\n", String8(serviceName).c_str());
481*38e8c45fSAndroid Build Coastguard Worker } else {
482*38e8c45fSAndroid Build Coastguard Worker String16 priorityType = ConvertBitmaskToPriorityType(priorityFlags);
483*38e8c45fSAndroid Build Coastguard Worker StringAppendF(&msg, "DUMP OF SERVICE %s %s:\n", String8(priorityType).c_str(),
484*38e8c45fSAndroid Build Coastguard Worker String8(serviceName).c_str());
485*38e8c45fSAndroid Build Coastguard Worker }
486*38e8c45fSAndroid Build Coastguard Worker WriteStringToFd(msg, fd);
487*38e8c45fSAndroid Build Coastguard Worker }
488*38e8c45fSAndroid Build Coastguard Worker
writeDump(int fd,const String16 & serviceName,std::chrono::milliseconds timeout,bool asProto,std::chrono::duration<double> & elapsedDuration,size_t & bytesWritten) const489*38e8c45fSAndroid Build Coastguard Worker status_t Dumpsys::writeDump(int fd, const String16& serviceName, std::chrono::milliseconds timeout,
490*38e8c45fSAndroid Build Coastguard Worker bool asProto, std::chrono::duration<double>& elapsedDuration,
491*38e8c45fSAndroid Build Coastguard Worker size_t& bytesWritten) const {
492*38e8c45fSAndroid Build Coastguard Worker status_t status = OK;
493*38e8c45fSAndroid Build Coastguard Worker size_t totalBytes = 0;
494*38e8c45fSAndroid Build Coastguard Worker auto start = std::chrono::steady_clock::now();
495*38e8c45fSAndroid Build Coastguard Worker auto end = start + timeout;
496*38e8c45fSAndroid Build Coastguard Worker
497*38e8c45fSAndroid Build Coastguard Worker int serviceDumpFd = redirectFd_.get();
498*38e8c45fSAndroid Build Coastguard Worker if (serviceDumpFd == -1) {
499*38e8c45fSAndroid Build Coastguard Worker return INVALID_OPERATION;
500*38e8c45fSAndroid Build Coastguard Worker }
501*38e8c45fSAndroid Build Coastguard Worker
502*38e8c45fSAndroid Build Coastguard Worker struct pollfd pfd = {.fd = serviceDumpFd, .events = POLLIN};
503*38e8c45fSAndroid Build Coastguard Worker
504*38e8c45fSAndroid Build Coastguard Worker while (true) {
505*38e8c45fSAndroid Build Coastguard Worker // Wrap this in a lambda so that TEMP_FAILURE_RETRY recalculates the timeout.
506*38e8c45fSAndroid Build Coastguard Worker auto time_left_ms = [end]() {
507*38e8c45fSAndroid Build Coastguard Worker auto now = std::chrono::steady_clock::now();
508*38e8c45fSAndroid Build Coastguard Worker auto diff = std::chrono::duration_cast<std::chrono::milliseconds>(end - now);
509*38e8c45fSAndroid Build Coastguard Worker return std::max(diff.count(), 0LL);
510*38e8c45fSAndroid Build Coastguard Worker };
511*38e8c45fSAndroid Build Coastguard Worker
512*38e8c45fSAndroid Build Coastguard Worker int rc = TEMP_FAILURE_RETRY(poll(&pfd, 1, time_left_ms()));
513*38e8c45fSAndroid Build Coastguard Worker if (rc < 0) {
514*38e8c45fSAndroid Build Coastguard Worker std::cerr << "Error in poll while dumping service " << serviceName << " : "
515*38e8c45fSAndroid Build Coastguard Worker << strerror(errno) << std::endl;
516*38e8c45fSAndroid Build Coastguard Worker status = -errno;
517*38e8c45fSAndroid Build Coastguard Worker break;
518*38e8c45fSAndroid Build Coastguard Worker } else if (rc == 0 || time_left_ms() == 0) {
519*38e8c45fSAndroid Build Coastguard Worker status = TIMED_OUT;
520*38e8c45fSAndroid Build Coastguard Worker break;
521*38e8c45fSAndroid Build Coastguard Worker }
522*38e8c45fSAndroid Build Coastguard Worker
523*38e8c45fSAndroid Build Coastguard Worker char buf[4096];
524*38e8c45fSAndroid Build Coastguard Worker rc = TEMP_FAILURE_RETRY(read(redirectFd_.get(), buf, sizeof(buf)));
525*38e8c45fSAndroid Build Coastguard Worker if (rc < 0) {
526*38e8c45fSAndroid Build Coastguard Worker std::cerr << "Failed to read while dumping service " << serviceName << ": "
527*38e8c45fSAndroid Build Coastguard Worker << strerror(errno) << std::endl;
528*38e8c45fSAndroid Build Coastguard Worker status = -errno;
529*38e8c45fSAndroid Build Coastguard Worker break;
530*38e8c45fSAndroid Build Coastguard Worker } else if (rc == 0) {
531*38e8c45fSAndroid Build Coastguard Worker // EOF.
532*38e8c45fSAndroid Build Coastguard Worker break;
533*38e8c45fSAndroid Build Coastguard Worker }
534*38e8c45fSAndroid Build Coastguard Worker
535*38e8c45fSAndroid Build Coastguard Worker if (!WriteFully(fd, buf, rc)) {
536*38e8c45fSAndroid Build Coastguard Worker std::cerr << "Failed to write while dumping service " << serviceName << ": "
537*38e8c45fSAndroid Build Coastguard Worker << strerror(errno) << std::endl;
538*38e8c45fSAndroid Build Coastguard Worker status = -errno;
539*38e8c45fSAndroid Build Coastguard Worker break;
540*38e8c45fSAndroid Build Coastguard Worker }
541*38e8c45fSAndroid Build Coastguard Worker totalBytes += rc;
542*38e8c45fSAndroid Build Coastguard Worker }
543*38e8c45fSAndroid Build Coastguard Worker
544*38e8c45fSAndroid Build Coastguard Worker if ((status == TIMED_OUT) && (!asProto)) {
545*38e8c45fSAndroid Build Coastguard Worker std::string msg = StringPrintf("\n*** SERVICE '%s' DUMP TIMEOUT (%llums) EXPIRED ***\n\n",
546*38e8c45fSAndroid Build Coastguard Worker String8(serviceName).c_str(), timeout.count());
547*38e8c45fSAndroid Build Coastguard Worker WriteStringToFd(msg, fd);
548*38e8c45fSAndroid Build Coastguard Worker }
549*38e8c45fSAndroid Build Coastguard Worker
550*38e8c45fSAndroid Build Coastguard Worker elapsedDuration = std::chrono::steady_clock::now() - start;
551*38e8c45fSAndroid Build Coastguard Worker bytesWritten = totalBytes;
552*38e8c45fSAndroid Build Coastguard Worker return status;
553*38e8c45fSAndroid Build Coastguard Worker }
554*38e8c45fSAndroid Build Coastguard Worker
writeDumpFooter(int fd,const String16 & serviceName,const std::chrono::duration<double> & elapsedDuration) const555*38e8c45fSAndroid Build Coastguard Worker void Dumpsys::writeDumpFooter(int fd, const String16& serviceName,
556*38e8c45fSAndroid Build Coastguard Worker const std::chrono::duration<double>& elapsedDuration) const {
557*38e8c45fSAndroid Build Coastguard Worker using std::chrono::system_clock;
558*38e8c45fSAndroid Build Coastguard Worker const auto finish = system_clock::to_time_t(system_clock::now());
559*38e8c45fSAndroid Build Coastguard Worker std::tm finish_tm;
560*38e8c45fSAndroid Build Coastguard Worker localtime_r(&finish, &finish_tm);
561*38e8c45fSAndroid Build Coastguard Worker std::stringstream oss;
562*38e8c45fSAndroid Build Coastguard Worker oss << std::put_time(&finish_tm, "%Y-%m-%d %H:%M:%S");
563*38e8c45fSAndroid Build Coastguard Worker std::string msg =
564*38e8c45fSAndroid Build Coastguard Worker StringPrintf("--------- %.3fs was the duration of dumpsys %s, ending at: %s\n",
565*38e8c45fSAndroid Build Coastguard Worker elapsedDuration.count(), String8(serviceName).c_str(), oss.str().c_str());
566*38e8c45fSAndroid Build Coastguard Worker WriteStringToFd(msg, fd);
567*38e8c45fSAndroid Build Coastguard Worker }
568