Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1 | /* |
Felipe Leme | 343175a | 2016-08-02 18:57:37 -0700 | [diff] [blame] | 2 | * Copyright (C) 2009 The Android Open Source Project |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 3 | * |
Felipe Leme | 343175a | 2016-08-02 18:57:37 -0700 | [diff] [blame] | 4 | * 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 Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 15 | */ |
| 16 | |
Josh Gao | 4b8f6f9 | 2016-02-29 16:20:34 -0800 | [diff] [blame] | 17 | #include <algorithm> |
| 18 | #include <chrono> |
Kevin Rocard | 430e079 | 2017-08-14 20:40:24 -0700 | [diff] [blame] | 19 | #include <iomanip> |
Josh Gao | 4b8f6f9 | 2016-02-29 16:20:34 -0800 | [diff] [blame] | 20 | #include <thread> |
| 21 | |
| 22 | #include <android-base/file.h> |
mukesh agrawal | 2f1eb1c | 2016-06-08 18:16:36 -0700 | [diff] [blame] | 23 | #include <android-base/stringprintf.h> |
Josh Gao | 4b8f6f9 | 2016-02-29 16:20:34 -0800 | [diff] [blame] | 24 | #include <android-base/unique_fd.h> |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 25 | #include <binder/Parcel.h> |
| 26 | #include <binder/ProcessState.h> |
Mathias Agopian | 002e1e5 | 2013-05-06 20:20:50 -0700 | [diff] [blame] | 27 | #include <binder/TextOutput.h> |
Vishnu Nair | 780b128 | 2017-10-10 13:57:24 -0700 | [diff] [blame] | 28 | #include <serviceutils/PriorityDumper.h> |
Josh Gao | 4b8f6f9 | 2016-02-29 16:20:34 -0800 | [diff] [blame] | 29 | #include <utils/Log.h> |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 30 | #include <utils/Vector.h> |
| 31 | |
Steven Moreland | 3657ee5 | 2020-01-31 12:09:15 -0800 | [diff] [blame] | 32 | #include <iostream> |
Josh Gao | 4b8f6f9 | 2016-02-29 16:20:34 -0800 | [diff] [blame] | 33 | #include <fcntl.h> |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 34 | #include <getopt.h> |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 35 | #include <stdio.h> |
Josh Gao | 4b8f6f9 | 2016-02-29 16:20:34 -0800 | [diff] [blame] | 36 | #include <stdlib.h> |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 37 | #include <string.h> |
Josh Gao | 4b8f6f9 | 2016-02-29 16:20:34 -0800 | [diff] [blame] | 38 | #include <sys/poll.h> |
| 39 | #include <sys/socket.h> |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 40 | #include <sys/time.h> |
Josh Gao | 4b8f6f9 | 2016-02-29 16:20:34 -0800 | [diff] [blame] | 41 | #include <sys/types.h> |
| 42 | #include <unistd.h> |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 43 | |
Felipe Leme | 343175a | 2016-08-02 18:57:37 -0700 | [diff] [blame] | 44 | #include "dumpsys.h" |
| 45 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 46 | using namespace android; |
Vishnu Nair | e4f6174 | 2017-12-21 08:30:28 -0800 | [diff] [blame] | 47 | using ::android::base::StringAppendF; |
| 48 | using ::android::base::StringPrintf; |
| 49 | using ::android::base::unique_fd; |
| 50 | using ::android::base::WriteFully; |
| 51 | using ::android::base::WriteStringToFd; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 52 | |
Steven Moreland | 2c3cd83 | 2017-02-13 23:44:17 +0000 | [diff] [blame] | 53 | static int sort_func(const String16* lhs, const String16* rhs) |
| 54 | { |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 55 | return lhs->compare(*rhs); |
| 56 | } |
| 57 | |
Felipe Leme | bbfd2b8 | 2016-02-03 11:16:27 -0800 | [diff] [blame] | 58 | static void usage() { |
| 59 | fprintf(stderr, |
Vishnu Nair | f56042d | 2017-09-19 15:25:10 -0700 | [diff] [blame] | 60 | "usage: dumpsys\n" |
Felipe Leme | bbfd2b8 | 2016-02-03 11:16:27 -0800 | [diff] [blame] | 61 | " To dump all services.\n" |
| 62 | "or:\n" |
Steven Moreland | 5a30d34 | 2019-10-08 13:53:28 -0700 | [diff] [blame] | 63 | " dumpsys [-t TIMEOUT] [--priority LEVEL] [--pid] [--help | -l | --skip SERVICES " |
| 64 | "| SERVICE [ARGS]]\n" |
Felipe Leme | bbfd2b8 | 2016-02-03 11:16:27 -0800 | [diff] [blame] | 65 | " --help: shows this help\n" |
| 66 | " -l: only list services, do not dump them\n" |
Vishnu Nair | 6921f80 | 2017-11-22 09:17:23 -0800 | [diff] [blame] | 67 | " -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 Moreland | 5a30d34 | 2019-10-08 13:53:28 -0700 | [diff] [blame] | 69 | " --pid: dump PID instead of usual dump\n" |
Ng Zhi An | cf2d01b | 2018-09-14 09:47:43 -0700 | [diff] [blame] | 70 | " --proto: filter services that support dumping data in proto format. Dumps\n" |
Vishnu Nair | 6a40853 | 2017-10-24 09:11:27 -0700 | [diff] [blame] | 71 | " will be in proto format.\n" |
Vishnu Nair | f56042d | 2017-09-19 15:25:10 -0700 | [diff] [blame] | 72 | " --priority LEVEL: filter services based on specified priority\n" |
| 73 | " LEVEL must be one of CRITICAL | HIGH | NORMAL\n" |
Felipe Leme | 859aef6 | 2016-02-03 12:17:10 -0800 | [diff] [blame] | 74 | " --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 Leme | 343175a | 2016-08-02 18:57:37 -0700 | [diff] [blame] | 78 | static bool IsSkipped(const Vector<String16>& skipped, const String16& service) { |
Felipe Leme | 859aef6 | 2016-02-03 12:17:10 -0800 | [diff] [blame] | 79 | for (const auto& candidate : skipped) { |
| 80 | if (candidate == service) { |
| 81 | return true; |
| 82 | } |
| 83 | } |
| 84 | return false; |
Felipe Leme | bbfd2b8 | 2016-02-03 11:16:27 -0800 | [diff] [blame] | 85 | } |
| 86 | |
Vishnu Nair | 6a40853 | 2017-10-24 09:11:27 -0700 | [diff] [blame] | 87 | static bool ConvertPriorityTypeToBitmask(const String16& type, int& bitmask) { |
| 88 | if (type == PriorityDumper::PRIORITY_ARG_CRITICAL) { |
| 89 | bitmask = IServiceManager::DUMP_FLAG_PRIORITY_CRITICAL; |
Vishnu Nair | 780b128 | 2017-10-10 13:57:24 -0700 | [diff] [blame] | 90 | return true; |
| 91 | } |
Vishnu Nair | 6a40853 | 2017-10-24 09:11:27 -0700 | [diff] [blame] | 92 | if (type == PriorityDumper::PRIORITY_ARG_HIGH) { |
| 93 | bitmask = IServiceManager::DUMP_FLAG_PRIORITY_HIGH; |
Vishnu Nair | 780b128 | 2017-10-10 13:57:24 -0700 | [diff] [blame] | 94 | return true; |
| 95 | } |
Vishnu Nair | 6a40853 | 2017-10-24 09:11:27 -0700 | [diff] [blame] | 96 | if (type == PriorityDumper::PRIORITY_ARG_NORMAL) { |
| 97 | bitmask = IServiceManager::DUMP_FLAG_PRIORITY_NORMAL; |
Vishnu Nair | 780b128 | 2017-10-10 13:57:24 -0700 | [diff] [blame] | 98 | return true; |
| 99 | } |
| 100 | return false; |
| 101 | } |
| 102 | |
Vishnu Nair | e4f6174 | 2017-12-21 08:30:28 -0800 | [diff] [blame] | 103 | String16 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 Leme | 343175a | 2016-08-02 18:57:37 -0700 | [diff] [blame] | 116 | int Dumpsys::main(int argc, char* const argv[]) { |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 117 | Vector<String16> services; |
| 118 | Vector<String16> args; |
Vishnu Nair | 780b128 | 2017-10-10 13:57:24 -0700 | [diff] [blame] | 119 | String16 priorityType; |
Felipe Leme | 859aef6 | 2016-02-03 12:17:10 -0800 | [diff] [blame] | 120 | Vector<String16> skippedServices; |
Vishnu Nair | 6a40853 | 2017-10-24 09:11:27 -0700 | [diff] [blame] | 121 | Vector<String16> protoServices; |
keunyoung | caad555 | 2013-06-13 15:08:51 -0700 | [diff] [blame] | 122 | bool showListOnly = false; |
Thierry Strudel | 8b78b75 | 2016-03-22 10:25:44 -0700 | [diff] [blame] | 123 | bool skipServices = false; |
Vishnu Nair | e4f6174 | 2017-12-21 08:30:28 -0800 | [diff] [blame] | 124 | bool asProto = false; |
Steven Moreland | 5a30d34 | 2019-10-08 13:53:28 -0700 | [diff] [blame] | 125 | Type type = Type::DUMP; |
Vishnu Nair | 6921f80 | 2017-11-22 09:17:23 -0800 | [diff] [blame] | 126 | int timeoutArgMs = 10000; |
Vishnu Nair | e4f6174 | 2017-12-21 08:30:28 -0800 | [diff] [blame] | 127 | int priorityFlags = IServiceManager::DUMP_FLAG_PRIORITY_ALL; |
Steven Moreland | 5a30d34 | 2019-10-08 13:53:28 -0700 | [diff] [blame] | 128 | static struct option longOptions[] = {{"pid", no_argument, 0, 0}, |
| 129 | {"priority", required_argument, 0, 0}, |
Vishnu Nair | 6a40853 | 2017-10-24 09:11:27 -0700 | [diff] [blame] | 130 | {"proto", no_argument, 0, 0}, |
Vishnu Nair | f56042d | 2017-09-19 15:25:10 -0700 | [diff] [blame] | 131 | {"skip", no_argument, 0, 0}, |
| 132 | {"help", no_argument, 0, 0}, |
| 133 | {0, 0, 0, 0}}; |
Thierry Strudel | 8b78b75 | 2016-03-22 10:25:44 -0700 | [diff] [blame] | 134 | |
Felipe Leme | 343175a | 2016-08-02 18:57:37 -0700 | [diff] [blame] | 135 | // Must reset optind, otherwise subsequent calls will fail (wouldn't happen on main.cpp, but |
| 136 | // happens on test cases). |
| 137 | optind = 1; |
Thierry Strudel | 8b78b75 | 2016-03-22 10:25:44 -0700 | [diff] [blame] | 138 | while (1) { |
| 139 | int c; |
| 140 | int optionIndex = 0; |
| 141 | |
Vishnu Nair | 6921f80 | 2017-11-22 09:17:23 -0800 | [diff] [blame] | 142 | c = getopt_long(argc, argv, "+t:T:l", longOptions, &optionIndex); |
Thierry Strudel | 8b78b75 | 2016-03-22 10:25:44 -0700 | [diff] [blame] | 143 | |
| 144 | if (c == -1) { |
| 145 | break; |
Felipe Leme | bbfd2b8 | 2016-02-03 11:16:27 -0800 | [diff] [blame] | 146 | } |
Thierry Strudel | 8b78b75 | 2016-03-22 10:25:44 -0700 | [diff] [blame] | 147 | |
| 148 | switch (c) { |
| 149 | case 0: |
| 150 | if (!strcmp(longOptions[optionIndex].name, "skip")) { |
| 151 | skipServices = true; |
Vishnu Nair | 6a40853 | 2017-10-24 09:11:27 -0700 | [diff] [blame] | 152 | } else if (!strcmp(longOptions[optionIndex].name, "proto")) { |
Vishnu Nair | e4f6174 | 2017-12-21 08:30:28 -0800 | [diff] [blame] | 153 | asProto = true; |
Thierry Strudel | 8b78b75 | 2016-03-22 10:25:44 -0700 | [diff] [blame] | 154 | } else if (!strcmp(longOptions[optionIndex].name, "help")) { |
| 155 | usage(); |
| 156 | return 0; |
Vishnu Nair | f56042d | 2017-09-19 15:25:10 -0700 | [diff] [blame] | 157 | } else if (!strcmp(longOptions[optionIndex].name, "priority")) { |
Vishnu Nair | 780b128 | 2017-10-10 13:57:24 -0700 | [diff] [blame] | 158 | priorityType = String16(String8(optarg)); |
Vishnu Nair | e4f6174 | 2017-12-21 08:30:28 -0800 | [diff] [blame] | 159 | if (!ConvertPriorityTypeToBitmask(priorityType, priorityFlags)) { |
Vishnu Nair | f56042d | 2017-09-19 15:25:10 -0700 | [diff] [blame] | 160 | fprintf(stderr, "\n"); |
| 161 | usage(); |
| 162 | return -1; |
| 163 | } |
Steven Moreland | 5a30d34 | 2019-10-08 13:53:28 -0700 | [diff] [blame] | 164 | } else if (!strcmp(longOptions[optionIndex].name, "pid")) { |
| 165 | type = Type::PID; |
Thierry Strudel | 8b78b75 | 2016-03-22 10:25:44 -0700 | [diff] [blame] | 166 | } |
| 167 | break; |
| 168 | |
| 169 | case 't': |
| 170 | { |
Vishnu Nair | 6921f80 | 2017-11-22 09:17:23 -0800 | [diff] [blame] | 171 | 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 Strudel | 8b78b75 | 2016-03-22 10:25:44 -0700 | [diff] [blame] | 187 | return -1; |
| 188 | } |
| 189 | } |
| 190 | break; |
| 191 | |
| 192 | case 'l': |
Felipe Leme | bbfd2b8 | 2016-02-03 11:16:27 -0800 | [diff] [blame] | 193 | showListOnly = true; |
Thierry Strudel | 8b78b75 | 2016-03-22 10:25:44 -0700 | [diff] [blame] | 194 | break; |
| 195 | |
| 196 | default: |
| 197 | fprintf(stderr, "\n"); |
| 198 | usage(); |
| 199 | return -1; |
Felipe Leme | bbfd2b8 | 2016-02-03 11:16:27 -0800 | [diff] [blame] | 200 | } |
keunyoung | caad555 | 2013-06-13 15:08:51 -0700 | [diff] [blame] | 201 | } |
Thierry Strudel | 8b78b75 | 2016-03-22 10:25:44 -0700 | [diff] [blame] | 202 | |
| 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 Hsu | c66abb2 | 2019-09-27 16:10:21 +0800 | [diff] [blame] | 210 | 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 Leme | 859aef6 | 2016-02-03 12:17:10 -0800 | [diff] [blame] | 217 | } |
| 218 | } |
| 219 | } |
Thierry Strudel | 8b78b75 | 2016-03-22 10:25:44 -0700 | [diff] [blame] | 220 | |
| 221 | if ((skipServices && skippedServices.empty()) || |
Steven Moreland | 2c3cd83 | 2017-02-13 23:44:17 +0000 | [diff] [blame] | 222 | (showListOnly && (!services.empty() || !skippedServices.empty()))) { |
Thierry Strudel | 8b78b75 | 2016-03-22 10:25:44 -0700 | [diff] [blame] | 223 | usage(); |
| 224 | return -1; |
| 225 | } |
| 226 | |
Thierry Strudel | 159a832 | 2016-03-23 11:22:34 -0700 | [diff] [blame] | 227 | if (services.empty() || showListOnly) { |
Vishnu Nair | e4f6174 | 2017-12-21 08:30:28 -0800 | [diff] [blame] | 228 | services = listServices(priorityFlags, asProto); |
| 229 | setServiceArgs(args, asProto, priorityFlags); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | const size_t N = services.size(); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 233 | if (N > 1) { |
| 234 | // first print a list of the current services |
Steven Moreland | 3657ee5 | 2020-01-31 12:09:15 -0800 | [diff] [blame] | 235 | std::cout << "Currently running services:" << std::endl; |
Felipe Leme | bbfd2b8 | 2016-02-03 11:16:27 -0800 | [diff] [blame] | 236 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 237 | for (size_t i=0; i<N; i++) { |
Felipe Leme | 343175a | 2016-08-02 18:57:37 -0700 | [diff] [blame] | 238 | sp<IBinder> service = sm_->checkService(services[i]); |
| 239 | |
| 240 | if (service != nullptr) { |
Felipe Leme | 859aef6 | 2016-02-03 12:17:10 -0800 | [diff] [blame] | 241 | bool skipped = IsSkipped(skippedServices, services[i]); |
Steven Moreland | 3657ee5 | 2020-01-31 12:09:15 -0800 | [diff] [blame] | 242 | std::cout << " " << services[i] << (skipped ? " (skipped)" : "") << std::endl; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 243 | } |
| 244 | } |
| 245 | } |
| 246 | |
keunyoung | caad555 | 2013-06-13 15:08:51 -0700 | [diff] [blame] | 247 | if (showListOnly) { |
| 248 | return 0; |
| 249 | } |
| 250 | |
Josh Gao | 4b8f6f9 | 2016-02-29 16:20:34 -0800 | [diff] [blame] | 251 | for (size_t i = 0; i < N; i++) { |
Vishnu Nair | e4f6174 | 2017-12-21 08:30:28 -0800 | [diff] [blame] | 252 | const String16& serviceName = services[i]; |
| 253 | if (IsSkipped(skippedServices, serviceName)) continue; |
Felipe Leme | 859aef6 | 2016-02-03 12:17:10 -0800 | [diff] [blame] | 254 | |
Steven Moreland | 5a30d34 | 2019-10-08 13:53:28 -0700 | [diff] [blame] | 255 | if (startDumpThread(type, serviceName, args) == OK) { |
Vishnu Nair | e4f6174 | 2017-12-21 08:30:28 -0800 | [diff] [blame] | 256 | bool addSeparator = (N > 1); |
| 257 | if (addSeparator) { |
| 258 | writeDumpHeader(STDOUT_FILENO, serviceName, priorityFlags); |
Josh Gao | 4b8f6f9 | 2016-02-29 16:20:34 -0800 | [diff] [blame] | 259 | } |
Vishnu Nair | e4f6174 | 2017-12-21 08:30:28 -0800 | [diff] [blame] | 260 | 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 Gao | 4b8f6f9 | 2016-02-29 16:20:34 -0800 | [diff] [blame] | 265 | |
Vishnu Nair | e4f6174 | 2017-12-21 08:30:28 -0800 | [diff] [blame] | 266 | if (status == TIMED_OUT) { |
Steven Moreland | 3657ee5 | 2020-01-31 12:09:15 -0800 | [diff] [blame] | 267 | std::cout << std::endl |
Vishnu Nair | e4f6174 | 2017-12-21 08:30:28 -0800 | [diff] [blame] | 268 | << "*** SERVICE '" << serviceName << "' DUMP TIMEOUT (" << timeoutArgMs |
Steven Moreland | 3657ee5 | 2020-01-31 12:09:15 -0800 | [diff] [blame] | 269 | << "ms) EXPIRED ***" << std::endl |
| 270 | << std::endl; |
Josh Gao | 4b8f6f9 | 2016-02-29 16:20:34 -0800 | [diff] [blame] | 271 | } |
| 272 | |
Vishnu Nair | e4f6174 | 2017-12-21 08:30:28 -0800 | [diff] [blame] | 273 | if (addSeparator) { |
| 274 | writeDumpFooter(STDOUT_FILENO, serviceName, elapsedDuration); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 275 | } |
Vishnu Nair | e4f6174 | 2017-12-21 08:30:28 -0800 | [diff] [blame] | 276 | bool dumpComplete = (status == OK); |
| 277 | stopDumpThread(dumpComplete); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 278 | } |
| 279 | } |
| 280 | |
| 281 | return 0; |
| 282 | } |
Vishnu Nair | e4f6174 | 2017-12-21 08:30:28 -0800 | [diff] [blame] | 283 | |
| 284 | Vector<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 Nair | e97d612 | 2018-01-18 13:58:56 -0800 | [diff] [blame] | 298 | void Dumpsys::setServiceArgs(Vector<String16>& args, bool asProto, int priorityFlags) { |
Vishnu Nair | 64afc02 | 2018-02-01 15:29:34 -0800 | [diff] [blame] | 299 | // Add proto flag if dumping service as proto. |
Vishnu Nair | e4f6174 | 2017-12-21 08:30:28 -0800 | [diff] [blame] | 300 | if (asProto) { |
| 301 | args.insertAt(String16(PriorityDumper::PROTO_ARG), 0); |
| 302 | } |
Vishnu Nair | 64afc02 | 2018-02-01 15:29:34 -0800 | [diff] [blame] | 303 | |
| 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 Nair | e4f6174 | 2017-12-21 08:30:28 -0800 | [diff] [blame] | 316 | String16 priorityType = ConvertBitmaskToPriorityType(priorityFlags); |
| 317 | args.insertAt(String16(PriorityDumper::PRIORITY_ARG), 0); |
| 318 | args.insertAt(priorityType, 1); |
| 319 | } |
| 320 | } |
| 321 | |
Steven Moreland | 5a30d34 | 2019-10-08 13:53:28 -0700 | [diff] [blame] | 322 | static 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 | |
| 332 | status_t Dumpsys::startDumpThread(Type type, const String16& serviceName, |
| 333 | const Vector<String16>& args) { |
Vishnu Nair | e4f6174 | 2017-12-21 08:30:28 -0800 | [diff] [blame] | 334 | sp<IBinder> service = sm_->checkService(serviceName); |
| 335 | if (service == nullptr) { |
Steven Moreland | 3657ee5 | 2020-01-31 12:09:15 -0800 | [diff] [blame] | 336 | std::cerr << "Can't find service: " << serviceName << std::endl; |
Vishnu Nair | e4f6174 | 2017-12-21 08:30:28 -0800 | [diff] [blame] | 337 | return NAME_NOT_FOUND; |
| 338 | } |
| 339 | |
| 340 | int sfd[2]; |
| 341 | if (pipe(sfd) != 0) { |
Steven Moreland | 3657ee5 | 2020-01-31 12:09:15 -0800 | [diff] [blame] | 342 | std::cerr << "Failed to create pipe to dump service info for " << serviceName << ": " |
| 343 | << strerror(errno) << std::endl; |
Vishnu Nair | e4f6174 | 2017-12-21 08:30:28 -0800 | [diff] [blame] | 344 | 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 Moreland | 5a30d34 | 2019-10-08 13:53:28 -0700 | [diff] [blame] | 353 | status_t err = 0; |
Vishnu Nair | e4f6174 | 2017-12-21 08:30:28 -0800 | [diff] [blame] | 354 | |
Steven Moreland | 5a30d34 | 2019-10-08 13:53:28 -0700 | [diff] [blame] | 355 | 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 Moreland | 3657ee5 | 2020-01-31 12:09:15 -0800 | [diff] [blame] | 363 | std::cerr << "Unknown dump type" << static_cast<int>(type) << std::endl; |
Steven Moreland | 5a30d34 | 2019-10-08 13:53:28 -0700 | [diff] [blame] | 364 | return; |
| 365 | } |
| 366 | |
| 367 | if (err != OK) { |
Steven Moreland | 3657ee5 | 2020-01-31 12:09:15 -0800 | [diff] [blame] | 368 | std::cerr << "Error dumping service info status_t: " << statusToString(err) << " " |
| 369 | << serviceName << std::endl; |
Vishnu Nair | e4f6174 | 2017-12-21 08:30:28 -0800 | [diff] [blame] | 370 | } |
| 371 | }); |
| 372 | return OK; |
| 373 | } |
| 374 | |
| 375 | void 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 | |
| 385 | void 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 Nair | 64afc02 | 2018-02-01 15:29:34 -0800 | [diff] [blame] | 390 | priorityFlags == IServiceManager::DUMP_FLAG_PRIORITY_NORMAL || |
| 391 | priorityFlags == IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT) { |
Vishnu Nair | e4f6174 | 2017-12-21 08:30:28 -0800 | [diff] [blame] | 392 | 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 | |
| 401 | status_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 Hsieh | 9f2d531 | 2018-12-11 15:34:17 -0800 | [diff] [blame] | 421 | return std::max(diff.count(), 0LL); |
Vishnu Nair | e4f6174 | 2017-12-21 08:30:28 -0800 | [diff] [blame] | 422 | }; |
| 423 | |
| 424 | int rc = TEMP_FAILURE_RETRY(poll(&pfd, 1, time_left_ms())); |
| 425 | if (rc < 0) { |
Steven Moreland | 3657ee5 | 2020-01-31 12:09:15 -0800 | [diff] [blame] | 426 | std::cerr << "Error in poll while dumping service " << serviceName << " : " |
| 427 | << strerror(errno) << std::endl; |
Vishnu Nair | e4f6174 | 2017-12-21 08:30:28 -0800 | [diff] [blame] | 428 | 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 Moreland | 3657ee5 | 2020-01-31 12:09:15 -0800 | [diff] [blame] | 438 | std::cerr << "Failed to read while dumping service " << serviceName << ": " |
| 439 | << strerror(errno) << std::endl; |
Vishnu Nair | e4f6174 | 2017-12-21 08:30:28 -0800 | [diff] [blame] | 440 | status = -errno; |
| 441 | break; |
| 442 | } else if (rc == 0) { |
| 443 | // EOF. |
| 444 | break; |
| 445 | } |
| 446 | |
| 447 | if (!WriteFully(fd, buf, rc)) { |
Steven Moreland | 3657ee5 | 2020-01-31 12:09:15 -0800 | [diff] [blame] | 448 | std::cerr << "Failed to write while dumping service " << serviceName << ": " |
| 449 | << strerror(errno) << std::endl; |
Vishnu Nair | e4f6174 | 2017-12-21 08:30:28 -0800 | [diff] [blame] | 450 | 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 | |
| 467 | void 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 | } |