blob: f8fdaa094ef5d5fa91f0990f1725d48897e5c138 [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>
Steven Morelande5a6a872021-05-19 22:11:37 +000027#include <binder/Stability.h>
Mathias Agopian002e1e52013-05-06 20:20:50 -070028#include <binder/TextOutput.h>
Devin Moorecfeeda42020-12-08 12:50:58 -080029#include <binderdebug/BinderDebug.h>
Vishnu Nair780b1282017-10-10 13:57:24 -070030#include <serviceutils/PriorityDumper.h>
Josh Gao4b8f6f92016-02-29 16:20:34 -080031#include <utils/Log.h>
Colin Crossf45fa6b2012-03-26 12:38:26 -070032#include <utils/Vector.h>
33
Steven Moreland3657ee52020-01-31 12:09:15 -080034#include <iostream>
Josh Gao4b8f6f92016-02-29 16:20:34 -080035#include <fcntl.h>
Colin Crossf45fa6b2012-03-26 12:38:26 -070036#include <getopt.h>
Colin Crossf45fa6b2012-03-26 12:38:26 -070037#include <stdio.h>
Josh Gao4b8f6f92016-02-29 16:20:34 -080038#include <stdlib.h>
Colin Crossf45fa6b2012-03-26 12:38:26 -070039#include <string.h>
Josh Gao4b8f6f92016-02-29 16:20:34 -080040#include <sys/poll.h>
41#include <sys/socket.h>
Colin Crossf45fa6b2012-03-26 12:38:26 -070042#include <sys/time.h>
Josh Gao4b8f6f92016-02-29 16:20:34 -080043#include <sys/types.h>
44#include <unistd.h>
Colin Crossf45fa6b2012-03-26 12:38:26 -070045
Felipe Leme343175a2016-08-02 18:57:37 -070046#include "dumpsys.h"
47
Colin Crossf45fa6b2012-03-26 12:38:26 -070048using namespace android;
Vishnu Naire4f61742017-12-21 08:30:28 -080049using ::android::base::StringAppendF;
50using ::android::base::StringPrintf;
51using ::android::base::unique_fd;
52using ::android::base::WriteFully;
53using ::android::base::WriteStringToFd;
Colin Crossf45fa6b2012-03-26 12:38:26 -070054
Steven Moreland2c3cd832017-02-13 23:44:17 +000055static int sort_func(const String16* lhs, const String16* rhs)
56{
Colin Crossf45fa6b2012-03-26 12:38:26 -070057 return lhs->compare(*rhs);
58}
59
Felipe Lemebbfd2b82016-02-03 11:16:27 -080060static void usage() {
61 fprintf(stderr,
Vishnu Nairf56042d2017-09-19 15:25:10 -070062 "usage: dumpsys\n"
Felipe Lemebbfd2b82016-02-03 11:16:27 -080063 " To dump all services.\n"
64 "or:\n"
Steven Morelandcbd69fc2021-07-20 20:45:43 +000065 " dumpsys [-t TIMEOUT] [--priority LEVEL] [--dump] [--pid] [--thread] [--help | "
66 "-l | --skip SERVICES "
Steven Moreland5a30d342019-10-08 13:53:28 -070067 "| SERVICE [ARGS]]\n"
Felipe Lemebbfd2b82016-02-03 11:16:27 -080068 " --help: shows this help\n"
69 " -l: only list services, do not dump them\n"
Vishnu Nair6921f802017-11-22 09:17:23 -080070 " -t TIMEOUT_SEC: TIMEOUT to use in seconds instead of default 10 seconds\n"
71 " -T TIMEOUT_MS: TIMEOUT to use in milliseconds instead of default 10 seconds\n"
Steven Morelandcbd69fc2021-07-20 20:45:43 +000072 " --dump: ask the service to dump itself (this is the default)\n"
Steven Moreland5a30d342019-10-08 13:53:28 -070073 " --pid: dump PID instead of usual dump\n"
Ng Zhi Ancf2d01b2018-09-14 09:47:43 -070074 " --proto: filter services that support dumping data in proto format. Dumps\n"
Vishnu Nair6a408532017-10-24 09:11:27 -070075 " will be in proto format.\n"
Vishnu Nairf56042d2017-09-19 15:25:10 -070076 " --priority LEVEL: filter services based on specified priority\n"
77 " LEVEL must be one of CRITICAL | HIGH | NORMAL\n"
Felipe Leme859aef62016-02-03 12:17:10 -080078 " --skip SERVICES: dumps all services but SERVICES (comma-separated list)\n"
Steven Morelande5a6a872021-05-19 22:11:37 +000079 " --stability: dump binder stability information instead of usual dump\n"
80 " --thread: dump thread usage instead of usual dump\n"
Felipe Leme859aef62016-02-03 12:17:10 -080081 " SERVICE [ARGS]: dumps only service SERVICE, optionally passing ARGS to it\n");
82}
83
Felipe Leme343175a2016-08-02 18:57:37 -070084static bool IsSkipped(const Vector<String16>& skipped, const String16& service) {
Felipe Leme859aef62016-02-03 12:17:10 -080085 for (const auto& candidate : skipped) {
86 if (candidate == service) {
87 return true;
88 }
89 }
90 return false;
Felipe Lemebbfd2b82016-02-03 11:16:27 -080091}
92
Vishnu Nair6a408532017-10-24 09:11:27 -070093static bool ConvertPriorityTypeToBitmask(const String16& type, int& bitmask) {
94 if (type == PriorityDumper::PRIORITY_ARG_CRITICAL) {
95 bitmask = IServiceManager::DUMP_FLAG_PRIORITY_CRITICAL;
Vishnu Nair780b1282017-10-10 13:57:24 -070096 return true;
97 }
Vishnu Nair6a408532017-10-24 09:11:27 -070098 if (type == PriorityDumper::PRIORITY_ARG_HIGH) {
99 bitmask = IServiceManager::DUMP_FLAG_PRIORITY_HIGH;
Vishnu Nair780b1282017-10-10 13:57:24 -0700100 return true;
101 }
Vishnu Nair6a408532017-10-24 09:11:27 -0700102 if (type == PriorityDumper::PRIORITY_ARG_NORMAL) {
103 bitmask = IServiceManager::DUMP_FLAG_PRIORITY_NORMAL;
Vishnu Nair780b1282017-10-10 13:57:24 -0700104 return true;
105 }
106 return false;
107}
108
Vishnu Naire4f61742017-12-21 08:30:28 -0800109String16 ConvertBitmaskToPriorityType(int bitmask) {
110 if (bitmask == IServiceManager::DUMP_FLAG_PRIORITY_CRITICAL) {
111 return String16(PriorityDumper::PRIORITY_ARG_CRITICAL);
112 }
113 if (bitmask == IServiceManager::DUMP_FLAG_PRIORITY_HIGH) {
114 return String16(PriorityDumper::PRIORITY_ARG_HIGH);
115 }
116 if (bitmask == IServiceManager::DUMP_FLAG_PRIORITY_NORMAL) {
117 return String16(PriorityDumper::PRIORITY_ARG_NORMAL);
118 }
119 return String16("");
120}
121
Felipe Leme343175a2016-08-02 18:57:37 -0700122int Dumpsys::main(int argc, char* const argv[]) {
Colin Crossf45fa6b2012-03-26 12:38:26 -0700123 Vector<String16> services;
124 Vector<String16> args;
Vishnu Nair780b1282017-10-10 13:57:24 -0700125 String16 priorityType;
Felipe Leme859aef62016-02-03 12:17:10 -0800126 Vector<String16> skippedServices;
Vishnu Nair6a408532017-10-24 09:11:27 -0700127 Vector<String16> protoServices;
keunyoungcaad5552013-06-13 15:08:51 -0700128 bool showListOnly = false;
Thierry Strudel8b78b752016-03-22 10:25:44 -0700129 bool skipServices = false;
Vishnu Naire4f61742017-12-21 08:30:28 -0800130 bool asProto = false;
Steven Morelandcbd69fc2021-07-20 20:45:43 +0000131 int dumpTypeFlags = 0;
Vishnu Nair6921f802017-11-22 09:17:23 -0800132 int timeoutArgMs = 10000;
Vishnu Naire4f61742017-12-21 08:30:28 -0800133 int priorityFlags = IServiceManager::DUMP_FLAG_PRIORITY_ALL;
Steven Morelande5a6a872021-05-19 22:11:37 +0000134 static struct option longOptions[] = {{"help", no_argument, 0, 0},
Steven Morelandcbd69fc2021-07-20 20:45:43 +0000135 {"dump", no_argument, 0, 0},
Devin Moorecfeeda42020-12-08 12:50:58 -0800136 {"pid", no_argument, 0, 0},
Steven Moreland5a30d342019-10-08 13:53:28 -0700137 {"priority", required_argument, 0, 0},
Vishnu Nair6a408532017-10-24 09:11:27 -0700138 {"proto", no_argument, 0, 0},
Vishnu Nairf56042d2017-09-19 15:25:10 -0700139 {"skip", no_argument, 0, 0},
Steven Morelande5a6a872021-05-19 22:11:37 +0000140 {"stability", no_argument, 0, 0},
141 {"thread", no_argument, 0, 0},
Vishnu Nairf56042d2017-09-19 15:25:10 -0700142 {0, 0, 0, 0}};
Thierry Strudel8b78b752016-03-22 10:25:44 -0700143
Felipe Leme343175a2016-08-02 18:57:37 -0700144 // Must reset optind, otherwise subsequent calls will fail (wouldn't happen on main.cpp, but
145 // happens on test cases).
146 optind = 1;
Thierry Strudel8b78b752016-03-22 10:25:44 -0700147 while (1) {
148 int c;
149 int optionIndex = 0;
150
Vishnu Nair6921f802017-11-22 09:17:23 -0800151 c = getopt_long(argc, argv, "+t:T:l", longOptions, &optionIndex);
Thierry Strudel8b78b752016-03-22 10:25:44 -0700152
153 if (c == -1) {
154 break;
Felipe Lemebbfd2b82016-02-03 11:16:27 -0800155 }
Thierry Strudel8b78b752016-03-22 10:25:44 -0700156
157 switch (c) {
158 case 0:
159 if (!strcmp(longOptions[optionIndex].name, "skip")) {
160 skipServices = true;
Vishnu Nair6a408532017-10-24 09:11:27 -0700161 } else if (!strcmp(longOptions[optionIndex].name, "proto")) {
Vishnu Naire4f61742017-12-21 08:30:28 -0800162 asProto = true;
Thierry Strudel8b78b752016-03-22 10:25:44 -0700163 } else if (!strcmp(longOptions[optionIndex].name, "help")) {
164 usage();
165 return 0;
Vishnu Nairf56042d2017-09-19 15:25:10 -0700166 } else if (!strcmp(longOptions[optionIndex].name, "priority")) {
Vishnu Nair780b1282017-10-10 13:57:24 -0700167 priorityType = String16(String8(optarg));
Vishnu Naire4f61742017-12-21 08:30:28 -0800168 if (!ConvertPriorityTypeToBitmask(priorityType, priorityFlags)) {
Vishnu Nairf56042d2017-09-19 15:25:10 -0700169 fprintf(stderr, "\n");
170 usage();
171 return -1;
172 }
Steven Morelandcbd69fc2021-07-20 20:45:43 +0000173 } else if (!strcmp(longOptions[optionIndex].name, "dump")) {
174 dumpTypeFlags |= TYPE_DUMP;
Steven Moreland5a30d342019-10-08 13:53:28 -0700175 } else if (!strcmp(longOptions[optionIndex].name, "pid")) {
Steven Morelandcbd69fc2021-07-20 20:45:43 +0000176 dumpTypeFlags |= TYPE_PID;
Steven Morelande5a6a872021-05-19 22:11:37 +0000177 } else if (!strcmp(longOptions[optionIndex].name, "stability")) {
Steven Morelandcbd69fc2021-07-20 20:45:43 +0000178 dumpTypeFlags |= TYPE_STABILITY;
Devin Moorecfeeda42020-12-08 12:50:58 -0800179 } else if (!strcmp(longOptions[optionIndex].name, "thread")) {
Steven Morelandcbd69fc2021-07-20 20:45:43 +0000180 dumpTypeFlags |= TYPE_THREAD;
Thierry Strudel8b78b752016-03-22 10:25:44 -0700181 }
182 break;
183
184 case 't':
185 {
Vishnu Nair6921f802017-11-22 09:17:23 -0800186 char* endptr;
187 timeoutArgMs = strtol(optarg, &endptr, 10);
188 timeoutArgMs = timeoutArgMs * 1000;
189 if (*endptr != '\0' || timeoutArgMs <= 0) {
190 fprintf(stderr, "Error: invalid timeout(seconds) number: '%s'\n", optarg);
191 return -1;
192 }
193 }
194 break;
195
196 case 'T':
197 {
198 char* endptr;
199 timeoutArgMs = strtol(optarg, &endptr, 10);
200 if (*endptr != '\0' || timeoutArgMs <= 0) {
201 fprintf(stderr, "Error: invalid timeout(milliseconds) number: '%s'\n", optarg);
Thierry Strudel8b78b752016-03-22 10:25:44 -0700202 return -1;
203 }
204 }
205 break;
206
207 case 'l':
Felipe Lemebbfd2b82016-02-03 11:16:27 -0800208 showListOnly = true;
Thierry Strudel8b78b752016-03-22 10:25:44 -0700209 break;
210
211 default:
212 fprintf(stderr, "\n");
213 usage();
214 return -1;
Felipe Lemebbfd2b82016-02-03 11:16:27 -0800215 }
keunyoungcaad5552013-06-13 15:08:51 -0700216 }
Thierry Strudel8b78b752016-03-22 10:25:44 -0700217
Steven Morelandcbd69fc2021-07-20 20:45:43 +0000218 if (dumpTypeFlags == 0) {
219 dumpTypeFlags = TYPE_DUMP;
220 }
221
Thierry Strudel8b78b752016-03-22 10:25:44 -0700222 for (int i = optind; i < argc; i++) {
223 if (skipServices) {
224 skippedServices.add(String16(argv[i]));
225 } else {
226 if (i == optind) {
227 services.add(String16(argv[i]));
228 } else {
Riddle Hsuc66abb22019-09-27 16:10:21 +0800229 const String16 arg(argv[i]);
230 args.add(arg);
231 // For backward compatible, if the proto argument is passed to the service, the
232 // dump request is also considered to use proto.
233 if (!asProto && !arg.compare(String16(PriorityDumper::PROTO_ARG))) {
234 asProto = true;
235 }
Felipe Leme859aef62016-02-03 12:17:10 -0800236 }
237 }
238 }
Thierry Strudel8b78b752016-03-22 10:25:44 -0700239
240 if ((skipServices && skippedServices.empty()) ||
Steven Moreland2c3cd832017-02-13 23:44:17 +0000241 (showListOnly && (!services.empty() || !skippedServices.empty()))) {
Thierry Strudel8b78b752016-03-22 10:25:44 -0700242 usage();
243 return -1;
244 }
245
Thierry Strudel159a8322016-03-23 11:22:34 -0700246 if (services.empty() || showListOnly) {
Vishnu Naire4f61742017-12-21 08:30:28 -0800247 services = listServices(priorityFlags, asProto);
248 setServiceArgs(args, asProto, priorityFlags);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700249 }
250
251 const size_t N = services.size();
Steven Moreland31dac352020-03-05 09:46:45 -0800252 if (N > 1 || showListOnly) {
Colin Crossf45fa6b2012-03-26 12:38:26 -0700253 // first print a list of the current services
Steven Moreland3657ee52020-01-31 12:09:15 -0800254 std::cout << "Currently running services:" << std::endl;
Felipe Lemebbfd2b82016-02-03 11:16:27 -0800255
Colin Crossf45fa6b2012-03-26 12:38:26 -0700256 for (size_t i=0; i<N; i++) {
Felipe Leme343175a2016-08-02 18:57:37 -0700257 sp<IBinder> service = sm_->checkService(services[i]);
258
259 if (service != nullptr) {
Felipe Leme859aef62016-02-03 12:17:10 -0800260 bool skipped = IsSkipped(skippedServices, services[i]);
Steven Moreland3657ee52020-01-31 12:09:15 -0800261 std::cout << " " << services[i] << (skipped ? " (skipped)" : "") << std::endl;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700262 }
263 }
264 }
265
keunyoungcaad5552013-06-13 15:08:51 -0700266 if (showListOnly) {
267 return 0;
268 }
269
Josh Gao4b8f6f92016-02-29 16:20:34 -0800270 for (size_t i = 0; i < N; i++) {
Vishnu Naire4f61742017-12-21 08:30:28 -0800271 const String16& serviceName = services[i];
272 if (IsSkipped(skippedServices, serviceName)) continue;
Felipe Leme859aef62016-02-03 12:17:10 -0800273
Steven Morelandcbd69fc2021-07-20 20:45:43 +0000274 if (startDumpThread(dumpTypeFlags, serviceName, args) == OK) {
Vishnu Naire4f61742017-12-21 08:30:28 -0800275 bool addSeparator = (N > 1);
276 if (addSeparator) {
277 writeDumpHeader(STDOUT_FILENO, serviceName, priorityFlags);
Josh Gao4b8f6f92016-02-29 16:20:34 -0800278 }
Vishnu Naire4f61742017-12-21 08:30:28 -0800279 std::chrono::duration<double> elapsedDuration;
280 size_t bytesWritten = 0;
281 status_t status =
282 writeDump(STDOUT_FILENO, serviceName, std::chrono::milliseconds(timeoutArgMs),
283 asProto, elapsedDuration, bytesWritten);
Josh Gao4b8f6f92016-02-29 16:20:34 -0800284
Vishnu Naire4f61742017-12-21 08:30:28 -0800285 if (status == TIMED_OUT) {
Steven Moreland3657ee52020-01-31 12:09:15 -0800286 std::cout << std::endl
Vishnu Naire4f61742017-12-21 08:30:28 -0800287 << "*** SERVICE '" << serviceName << "' DUMP TIMEOUT (" << timeoutArgMs
Steven Moreland3657ee52020-01-31 12:09:15 -0800288 << "ms) EXPIRED ***" << std::endl
289 << std::endl;
Josh Gao4b8f6f92016-02-29 16:20:34 -0800290 }
291
Vishnu Naire4f61742017-12-21 08:30:28 -0800292 if (addSeparator) {
293 writeDumpFooter(STDOUT_FILENO, serviceName, elapsedDuration);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700294 }
Vishnu Naire4f61742017-12-21 08:30:28 -0800295 bool dumpComplete = (status == OK);
296 stopDumpThread(dumpComplete);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700297 }
298 }
299
300 return 0;
301}
Vishnu Naire4f61742017-12-21 08:30:28 -0800302
303Vector<String16> Dumpsys::listServices(int priorityFilterFlags, bool filterByProto) const {
304 Vector<String16> services = sm_->listServices(priorityFilterFlags);
305 services.sort(sort_func);
306 if (filterByProto) {
307 Vector<String16> protoServices = sm_->listServices(IServiceManager::DUMP_FLAG_PROTO);
308 protoServices.sort(sort_func);
309 Vector<String16> intersection;
310 std::set_intersection(services.begin(), services.end(), protoServices.begin(),
311 protoServices.end(), std::back_inserter(intersection));
312 services = std::move(intersection);
313 }
314 return services;
315}
316
Vishnu Naire97d6122018-01-18 13:58:56 -0800317void Dumpsys::setServiceArgs(Vector<String16>& args, bool asProto, int priorityFlags) {
Vishnu Nair64afc022018-02-01 15:29:34 -0800318 // Add proto flag if dumping service as proto.
Vishnu Naire4f61742017-12-21 08:30:28 -0800319 if (asProto) {
320 args.insertAt(String16(PriorityDumper::PROTO_ARG), 0);
321 }
Vishnu Nair64afc022018-02-01 15:29:34 -0800322
323 // Add -a (dump all) flag if dumping all services, dumping normal services or
324 // services not explicitly registered to a priority bucket (default services).
325 if ((priorityFlags == IServiceManager::DUMP_FLAG_PRIORITY_ALL) ||
326 (priorityFlags == IServiceManager::DUMP_FLAG_PRIORITY_NORMAL) ||
327 (priorityFlags == IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT)) {
328 args.insertAt(String16("-a"), 0);
329 }
330
331 // Add priority flags when dumping services registered to a specific priority bucket.
332 if ((priorityFlags == IServiceManager::DUMP_FLAG_PRIORITY_CRITICAL) ||
333 (priorityFlags == IServiceManager::DUMP_FLAG_PRIORITY_HIGH) ||
334 (priorityFlags == IServiceManager::DUMP_FLAG_PRIORITY_NORMAL)) {
Vishnu Naire4f61742017-12-21 08:30:28 -0800335 String16 priorityType = ConvertBitmaskToPriorityType(priorityFlags);
336 args.insertAt(String16(PriorityDumper::PRIORITY_ARG), 0);
337 args.insertAt(priorityType, 1);
338 }
339}
340
Steven Morelandcbd69fc2021-07-20 20:45:43 +0000341static status_t dumpPidToFd(const sp<IBinder>& service, const unique_fd& fd, bool exclusive) {
Steven Moreland5a30d342019-10-08 13:53:28 -0700342 pid_t pid;
343 status_t status = service->getDebugPid(&pid);
344 if (status != OK) {
345 return status;
346 }
Steven Morelandcbd69fc2021-07-20 20:45:43 +0000347 if (!exclusive) {
348 WriteStringToFd("Service host process PID: ", fd.get());
349 }
Steven Moreland5a30d342019-10-08 13:53:28 -0700350 WriteStringToFd(std::to_string(pid) + "\n", fd.get());
351 return OK;
352}
353
Steven Morelande5a6a872021-05-19 22:11:37 +0000354static status_t dumpStabilityToFd(const sp<IBinder>& service, const unique_fd& fd) {
Steven Morelandcbd69fc2021-07-20 20:45:43 +0000355 WriteStringToFd("Stability: " + internal::Stability::debugToString(service) + "\n", fd);
Steven Morelande5a6a872021-05-19 22:11:37 +0000356 return OK;
357}
358
Devin Moorecfeeda42020-12-08 12:50:58 -0800359static status_t dumpThreadsToFd(const sp<IBinder>& service, const unique_fd& fd) {
360 pid_t pid;
361 status_t status = service->getDebugPid(&pid);
362 if (status != OK) {
363 return status;
364 }
365 BinderPidInfo pidInfo;
366 status = getBinderPidInfo(BinderDebugContext::BINDER, pid, &pidInfo);
367 if (status != OK) {
368 return status;
369 }
370 WriteStringToFd("Threads in use: " + std::to_string(pidInfo.threadUsage) + "/" +
371 std::to_string(pidInfo.threadCount) + "\n",
372 fd.get());
373 return OK;
374}
375
Steven Morelandcbd69fc2021-07-20 20:45:43 +0000376static void reportDumpError(const String16& serviceName, status_t error, const char* context) {
377 if (error == OK) return;
378
379 std::cerr << "Error with service '" << serviceName << "' while " << context << ": "
380 << statusToString(error) << std::endl;
381}
382
383status_t Dumpsys::startDumpThread(int dumpTypeFlags, const String16& serviceName,
Steven Moreland5a30d342019-10-08 13:53:28 -0700384 const Vector<String16>& args) {
Vishnu Naire4f61742017-12-21 08:30:28 -0800385 sp<IBinder> service = sm_->checkService(serviceName);
386 if (service == nullptr) {
Steven Moreland3657ee52020-01-31 12:09:15 -0800387 std::cerr << "Can't find service: " << serviceName << std::endl;
Vishnu Naire4f61742017-12-21 08:30:28 -0800388 return NAME_NOT_FOUND;
389 }
390
391 int sfd[2];
392 if (pipe(sfd) != 0) {
Steven Moreland3657ee52020-01-31 12:09:15 -0800393 std::cerr << "Failed to create pipe to dump service info for " << serviceName << ": "
394 << strerror(errno) << std::endl;
Vishnu Naire4f61742017-12-21 08:30:28 -0800395 return -errno;
396 }
397
398 redirectFd_ = unique_fd(sfd[0]);
399 unique_fd remote_end(sfd[1]);
400 sfd[0] = sfd[1] = -1;
401
402 // dump blocks until completion, so spawn a thread..
403 activeThread_ = std::thread([=, remote_end{std::move(remote_end)}]() mutable {
Steven Morelandcbd69fc2021-07-20 20:45:43 +0000404 if (dumpTypeFlags & TYPE_PID) {
405 status_t err = dumpPidToFd(service, remote_end, dumpTypeFlags == TYPE_PID);
406 reportDumpError(serviceName, err, "dumping PID");
407 }
408 if (dumpTypeFlags & TYPE_STABILITY) {
409 status_t err = dumpStabilityToFd(service, remote_end);
410 reportDumpError(serviceName, err, "dumping stability");
411 }
412 if (dumpTypeFlags & TYPE_THREAD) {
413 status_t err = dumpThreadsToFd(service, remote_end);
414 reportDumpError(serviceName, err, "dumping thread info");
Steven Moreland5a30d342019-10-08 13:53:28 -0700415 }
416
Steven Morelandcbd69fc2021-07-20 20:45:43 +0000417 // other types always act as a header, this is usually longer
418 if (dumpTypeFlags & TYPE_DUMP) {
419 status_t err = service->dump(remote_end.get(), args);
420 reportDumpError(serviceName, err, "dumping");
Vishnu Naire4f61742017-12-21 08:30:28 -0800421 }
422 });
423 return OK;
424}
425
426void Dumpsys::stopDumpThread(bool dumpComplete) {
427 if (dumpComplete) {
428 activeThread_.join();
429 } else {
430 activeThread_.detach();
431 }
432 /* close read end of the dump output redirection pipe */
433 redirectFd_.reset();
434}
435
436void Dumpsys::writeDumpHeader(int fd, const String16& serviceName, int priorityFlags) const {
437 std::string msg(
438 "----------------------------------------"
439 "---------------------------------------\n");
440 if (priorityFlags == IServiceManager::DUMP_FLAG_PRIORITY_ALL ||
Vishnu Nair64afc022018-02-01 15:29:34 -0800441 priorityFlags == IServiceManager::DUMP_FLAG_PRIORITY_NORMAL ||
442 priorityFlags == IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT) {
Vishnu Naire4f61742017-12-21 08:30:28 -0800443 StringAppendF(&msg, "DUMP OF SERVICE %s:\n", String8(serviceName).c_str());
444 } else {
445 String16 priorityType = ConvertBitmaskToPriorityType(priorityFlags);
446 StringAppendF(&msg, "DUMP OF SERVICE %s %s:\n", String8(priorityType).c_str(),
447 String8(serviceName).c_str());
448 }
449 WriteStringToFd(msg, fd);
450}
451
452status_t Dumpsys::writeDump(int fd, const String16& serviceName, std::chrono::milliseconds timeout,
453 bool asProto, std::chrono::duration<double>& elapsedDuration,
454 size_t& bytesWritten) const {
455 status_t status = OK;
456 size_t totalBytes = 0;
457 auto start = std::chrono::steady_clock::now();
458 auto end = start + timeout;
459
460 int serviceDumpFd = redirectFd_.get();
461 if (serviceDumpFd == -1) {
462 return INVALID_OPERATION;
463 }
464
465 struct pollfd pfd = {.fd = serviceDumpFd, .events = POLLIN};
466
467 while (true) {
468 // Wrap this in a lambda so that TEMP_FAILURE_RETRY recalculates the timeout.
469 auto time_left_ms = [end]() {
470 auto now = std::chrono::steady_clock::now();
471 auto diff = std::chrono::duration_cast<std::chrono::milliseconds>(end - now);
Chih-Hung Hsieh9f2d5312018-12-11 15:34:17 -0800472 return std::max(diff.count(), 0LL);
Vishnu Naire4f61742017-12-21 08:30:28 -0800473 };
474
475 int rc = TEMP_FAILURE_RETRY(poll(&pfd, 1, time_left_ms()));
476 if (rc < 0) {
Steven Moreland3657ee52020-01-31 12:09:15 -0800477 std::cerr << "Error in poll while dumping service " << serviceName << " : "
478 << strerror(errno) << std::endl;
Vishnu Naire4f61742017-12-21 08:30:28 -0800479 status = -errno;
480 break;
Rhed Jaoa19145f2020-12-22 20:42:34 +0800481 } else if (rc == 0 || time_left_ms() == 0) {
Vishnu Naire4f61742017-12-21 08:30:28 -0800482 status = TIMED_OUT;
483 break;
484 }
485
486 char buf[4096];
487 rc = TEMP_FAILURE_RETRY(read(redirectFd_.get(), buf, sizeof(buf)));
488 if (rc < 0) {
Steven Moreland3657ee52020-01-31 12:09:15 -0800489 std::cerr << "Failed to read while dumping service " << serviceName << ": "
490 << strerror(errno) << std::endl;
Vishnu Naire4f61742017-12-21 08:30:28 -0800491 status = -errno;
492 break;
493 } else if (rc == 0) {
494 // EOF.
495 break;
496 }
497
498 if (!WriteFully(fd, buf, rc)) {
Steven Moreland3657ee52020-01-31 12:09:15 -0800499 std::cerr << "Failed to write while dumping service " << serviceName << ": "
500 << strerror(errno) << std::endl;
Vishnu Naire4f61742017-12-21 08:30:28 -0800501 status = -errno;
502 break;
503 }
504 totalBytes += rc;
505 }
506
507 if ((status == TIMED_OUT) && (!asProto)) {
508 std::string msg = StringPrintf("\n*** SERVICE '%s' DUMP TIMEOUT (%llums) EXPIRED ***\n\n",
509 String8(serviceName).string(), timeout.count());
510 WriteStringToFd(msg, fd);
511 }
512
513 elapsedDuration = std::chrono::steady_clock::now() - start;
514 bytesWritten = totalBytes;
515 return status;
516}
517
518void Dumpsys::writeDumpFooter(int fd, const String16& serviceName,
519 const std::chrono::duration<double>& elapsedDuration) const {
520 using std::chrono::system_clock;
521 const auto finish = system_clock::to_time_t(system_clock::now());
522 std::tm finish_tm;
523 localtime_r(&finish, &finish_tm);
524 std::stringstream oss;
525 oss << std::put_time(&finish_tm, "%Y-%m-%d %H:%M:%S");
526 std::string msg =
527 StringPrintf("--------- %.3fs was the duration of dumpsys %s, ending at: %s\n",
528 elapsedDuration.count(), String8(serviceName).string(), oss.str().c_str());
529 WriteStringToFd(msg, fd);
530}