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 | |
Josh Gao | 4b8f6f9 | 2016-02-29 16:20:34 -0800 | [diff] [blame] | 32 | #include <fcntl.h> |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 33 | #include <getopt.h> |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 34 | #include <stdio.h> |
Josh Gao | 4b8f6f9 | 2016-02-29 16:20:34 -0800 | [diff] [blame] | 35 | #include <stdlib.h> |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 36 | #include <string.h> |
Josh Gao | 4b8f6f9 | 2016-02-29 16:20:34 -0800 | [diff] [blame] | 37 | #include <sys/poll.h> |
| 38 | #include <sys/socket.h> |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 39 | #include <sys/time.h> |
Josh Gao | 4b8f6f9 | 2016-02-29 16:20:34 -0800 | [diff] [blame] | 40 | #include <sys/types.h> |
| 41 | #include <unistd.h> |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 42 | |
Felipe Leme | 343175a | 2016-08-02 18:57:37 -0700 | [diff] [blame] | 43 | #include "dumpsys.h" |
| 44 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 45 | using namespace android; |
Vishnu Nair | e4f6174 | 2017-12-21 08:30:28 -0800 | [diff] [blame] | 46 | using ::android::base::StringAppendF; |
| 47 | using ::android::base::StringPrintf; |
| 48 | using ::android::base::unique_fd; |
| 49 | using ::android::base::WriteFully; |
| 50 | using ::android::base::WriteStringToFd; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 51 | |
Steven Moreland | 2c3cd83 | 2017-02-13 23:44:17 +0000 | [diff] [blame] | 52 | static int sort_func(const String16* lhs, const String16* rhs) |
| 53 | { |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 54 | return lhs->compare(*rhs); |
| 55 | } |
| 56 | |
Felipe Leme | bbfd2b8 | 2016-02-03 11:16:27 -0800 | [diff] [blame] | 57 | static void usage() { |
| 58 | fprintf(stderr, |
Vishnu Nair | f56042d | 2017-09-19 15:25:10 -0700 | [diff] [blame] | 59 | "usage: dumpsys\n" |
Felipe Leme | bbfd2b8 | 2016-02-03 11:16:27 -0800 | [diff] [blame] | 60 | " To dump all services.\n" |
| 61 | "or:\n" |
Vishnu Nair | f56042d | 2017-09-19 15:25:10 -0700 | [diff] [blame] | 62 | " dumpsys [-t TIMEOUT] [--priority LEVEL] [--help | -l | --skip SERVICES | " |
| 63 | "SERVICE [ARGS]]\n" |
Felipe Leme | bbfd2b8 | 2016-02-03 11:16:27 -0800 | [diff] [blame] | 64 | " --help: shows this help\n" |
| 65 | " -l: only list services, do not dump them\n" |
Vishnu Nair | 6921f80 | 2017-11-22 09:17:23 -0800 | [diff] [blame] | 66 | " -t TIMEOUT_SEC: TIMEOUT to use in seconds instead of default 10 seconds\n" |
| 67 | " -T TIMEOUT_MS: TIMEOUT to use in milliseconds instead of default 10 seconds\n" |
Vishnu Nair | 6a40853 | 2017-10-24 09:11:27 -0700 | [diff] [blame] | 68 | " --proto: filter services that support dumping data in proto format. Dumps" |
| 69 | " will be in proto format.\n" |
Vishnu Nair | f56042d | 2017-09-19 15:25:10 -0700 | [diff] [blame] | 70 | " --priority LEVEL: filter services based on specified priority\n" |
| 71 | " LEVEL must be one of CRITICAL | HIGH | NORMAL\n" |
Felipe Leme | 859aef6 | 2016-02-03 12:17:10 -0800 | [diff] [blame] | 72 | " --skip SERVICES: dumps all services but SERVICES (comma-separated list)\n" |
| 73 | " SERVICE [ARGS]: dumps only service SERVICE, optionally passing ARGS to it\n"); |
| 74 | } |
| 75 | |
Felipe Leme | 343175a | 2016-08-02 18:57:37 -0700 | [diff] [blame] | 76 | static bool IsSkipped(const Vector<String16>& skipped, const String16& service) { |
Felipe Leme | 859aef6 | 2016-02-03 12:17:10 -0800 | [diff] [blame] | 77 | for (const auto& candidate : skipped) { |
| 78 | if (candidate == service) { |
| 79 | return true; |
| 80 | } |
| 81 | } |
| 82 | return false; |
Felipe Leme | bbfd2b8 | 2016-02-03 11:16:27 -0800 | [diff] [blame] | 83 | } |
| 84 | |
Vishnu Nair | 6a40853 | 2017-10-24 09:11:27 -0700 | [diff] [blame] | 85 | static bool ConvertPriorityTypeToBitmask(const String16& type, int& bitmask) { |
| 86 | if (type == PriorityDumper::PRIORITY_ARG_CRITICAL) { |
| 87 | bitmask = IServiceManager::DUMP_FLAG_PRIORITY_CRITICAL; |
Vishnu Nair | 780b128 | 2017-10-10 13:57:24 -0700 | [diff] [blame] | 88 | return true; |
| 89 | } |
Vishnu Nair | 6a40853 | 2017-10-24 09:11:27 -0700 | [diff] [blame] | 90 | if (type == PriorityDumper::PRIORITY_ARG_HIGH) { |
| 91 | bitmask = IServiceManager::DUMP_FLAG_PRIORITY_HIGH; |
Vishnu Nair | 780b128 | 2017-10-10 13:57:24 -0700 | [diff] [blame] | 92 | return true; |
| 93 | } |
Vishnu Nair | 6a40853 | 2017-10-24 09:11:27 -0700 | [diff] [blame] | 94 | if (type == PriorityDumper::PRIORITY_ARG_NORMAL) { |
| 95 | bitmask = IServiceManager::DUMP_FLAG_PRIORITY_NORMAL; |
Vishnu Nair | 780b128 | 2017-10-10 13:57:24 -0700 | [diff] [blame] | 96 | return true; |
| 97 | } |
| 98 | return false; |
| 99 | } |
| 100 | |
Vishnu Nair | e4f6174 | 2017-12-21 08:30:28 -0800 | [diff] [blame] | 101 | String16 ConvertBitmaskToPriorityType(int bitmask) { |
| 102 | if (bitmask == IServiceManager::DUMP_FLAG_PRIORITY_CRITICAL) { |
| 103 | return String16(PriorityDumper::PRIORITY_ARG_CRITICAL); |
| 104 | } |
| 105 | if (bitmask == IServiceManager::DUMP_FLAG_PRIORITY_HIGH) { |
| 106 | return String16(PriorityDumper::PRIORITY_ARG_HIGH); |
| 107 | } |
| 108 | if (bitmask == IServiceManager::DUMP_FLAG_PRIORITY_NORMAL) { |
| 109 | return String16(PriorityDumper::PRIORITY_ARG_NORMAL); |
| 110 | } |
| 111 | return String16(""); |
| 112 | } |
| 113 | |
Felipe Leme | 343175a | 2016-08-02 18:57:37 -0700 | [diff] [blame] | 114 | int Dumpsys::main(int argc, char* const argv[]) { |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 115 | Vector<String16> services; |
| 116 | Vector<String16> args; |
Vishnu Nair | 780b128 | 2017-10-10 13:57:24 -0700 | [diff] [blame] | 117 | String16 priorityType; |
Felipe Leme | 859aef6 | 2016-02-03 12:17:10 -0800 | [diff] [blame] | 118 | Vector<String16> skippedServices; |
Vishnu Nair | 6a40853 | 2017-10-24 09:11:27 -0700 | [diff] [blame] | 119 | Vector<String16> protoServices; |
keunyoung | caad555 | 2013-06-13 15:08:51 -0700 | [diff] [blame] | 120 | bool showListOnly = false; |
Thierry Strudel | 8b78b75 | 2016-03-22 10:25:44 -0700 | [diff] [blame] | 121 | bool skipServices = false; |
Vishnu Nair | e4f6174 | 2017-12-21 08:30:28 -0800 | [diff] [blame] | 122 | bool asProto = false; |
Vishnu Nair | 6921f80 | 2017-11-22 09:17:23 -0800 | [diff] [blame] | 123 | int timeoutArgMs = 10000; |
Vishnu Nair | e4f6174 | 2017-12-21 08:30:28 -0800 | [diff] [blame] | 124 | int priorityFlags = IServiceManager::DUMP_FLAG_PRIORITY_ALL; |
Vishnu Nair | f56042d | 2017-09-19 15:25:10 -0700 | [diff] [blame] | 125 | static struct option longOptions[] = {{"priority", required_argument, 0, 0}, |
Vishnu Nair | 6a40853 | 2017-10-24 09:11:27 -0700 | [diff] [blame] | 126 | {"proto", no_argument, 0, 0}, |
Vishnu Nair | f56042d | 2017-09-19 15:25:10 -0700 | [diff] [blame] | 127 | {"skip", no_argument, 0, 0}, |
| 128 | {"help", no_argument, 0, 0}, |
| 129 | {0, 0, 0, 0}}; |
Thierry Strudel | 8b78b75 | 2016-03-22 10:25:44 -0700 | [diff] [blame] | 130 | |
Felipe Leme | 343175a | 2016-08-02 18:57:37 -0700 | [diff] [blame] | 131 | // Must reset optind, otherwise subsequent calls will fail (wouldn't happen on main.cpp, but |
| 132 | // happens on test cases). |
| 133 | optind = 1; |
Thierry Strudel | 8b78b75 | 2016-03-22 10:25:44 -0700 | [diff] [blame] | 134 | while (1) { |
| 135 | int c; |
| 136 | int optionIndex = 0; |
| 137 | |
Vishnu Nair | 6921f80 | 2017-11-22 09:17:23 -0800 | [diff] [blame] | 138 | c = getopt_long(argc, argv, "+t:T:l", longOptions, &optionIndex); |
Thierry Strudel | 8b78b75 | 2016-03-22 10:25:44 -0700 | [diff] [blame] | 139 | |
| 140 | if (c == -1) { |
| 141 | break; |
Felipe Leme | bbfd2b8 | 2016-02-03 11:16:27 -0800 | [diff] [blame] | 142 | } |
Thierry Strudel | 8b78b75 | 2016-03-22 10:25:44 -0700 | [diff] [blame] | 143 | |
| 144 | switch (c) { |
| 145 | case 0: |
| 146 | if (!strcmp(longOptions[optionIndex].name, "skip")) { |
| 147 | skipServices = true; |
Vishnu Nair | 6a40853 | 2017-10-24 09:11:27 -0700 | [diff] [blame] | 148 | } else if (!strcmp(longOptions[optionIndex].name, "proto")) { |
Vishnu Nair | e4f6174 | 2017-12-21 08:30:28 -0800 | [diff] [blame] | 149 | asProto = true; |
Thierry Strudel | 8b78b75 | 2016-03-22 10:25:44 -0700 | [diff] [blame] | 150 | } else if (!strcmp(longOptions[optionIndex].name, "help")) { |
| 151 | usage(); |
| 152 | return 0; |
Vishnu Nair | f56042d | 2017-09-19 15:25:10 -0700 | [diff] [blame] | 153 | } else if (!strcmp(longOptions[optionIndex].name, "priority")) { |
Vishnu Nair | 780b128 | 2017-10-10 13:57:24 -0700 | [diff] [blame] | 154 | priorityType = String16(String8(optarg)); |
Vishnu Nair | e4f6174 | 2017-12-21 08:30:28 -0800 | [diff] [blame] | 155 | if (!ConvertPriorityTypeToBitmask(priorityType, priorityFlags)) { |
Vishnu Nair | f56042d | 2017-09-19 15:25:10 -0700 | [diff] [blame] | 156 | fprintf(stderr, "\n"); |
| 157 | usage(); |
| 158 | return -1; |
| 159 | } |
Thierry Strudel | 8b78b75 | 2016-03-22 10:25:44 -0700 | [diff] [blame] | 160 | } |
| 161 | break; |
| 162 | |
| 163 | case 't': |
| 164 | { |
Vishnu Nair | 6921f80 | 2017-11-22 09:17:23 -0800 | [diff] [blame] | 165 | char* endptr; |
| 166 | timeoutArgMs = strtol(optarg, &endptr, 10); |
| 167 | timeoutArgMs = timeoutArgMs * 1000; |
| 168 | if (*endptr != '\0' || timeoutArgMs <= 0) { |
| 169 | fprintf(stderr, "Error: invalid timeout(seconds) number: '%s'\n", optarg); |
| 170 | return -1; |
| 171 | } |
| 172 | } |
| 173 | break; |
| 174 | |
| 175 | case 'T': |
| 176 | { |
| 177 | char* endptr; |
| 178 | timeoutArgMs = strtol(optarg, &endptr, 10); |
| 179 | if (*endptr != '\0' || timeoutArgMs <= 0) { |
| 180 | fprintf(stderr, "Error: invalid timeout(milliseconds) number: '%s'\n", optarg); |
Thierry Strudel | 8b78b75 | 2016-03-22 10:25:44 -0700 | [diff] [blame] | 181 | return -1; |
| 182 | } |
| 183 | } |
| 184 | break; |
| 185 | |
| 186 | case 'l': |
Felipe Leme | bbfd2b8 | 2016-02-03 11:16:27 -0800 | [diff] [blame] | 187 | showListOnly = true; |
Thierry Strudel | 8b78b75 | 2016-03-22 10:25:44 -0700 | [diff] [blame] | 188 | break; |
| 189 | |
| 190 | default: |
| 191 | fprintf(stderr, "\n"); |
| 192 | usage(); |
| 193 | return -1; |
Felipe Leme | bbfd2b8 | 2016-02-03 11:16:27 -0800 | [diff] [blame] | 194 | } |
keunyoung | caad555 | 2013-06-13 15:08:51 -0700 | [diff] [blame] | 195 | } |
Thierry Strudel | 8b78b75 | 2016-03-22 10:25:44 -0700 | [diff] [blame] | 196 | |
| 197 | for (int i = optind; i < argc; i++) { |
| 198 | if (skipServices) { |
| 199 | skippedServices.add(String16(argv[i])); |
| 200 | } else { |
| 201 | if (i == optind) { |
| 202 | services.add(String16(argv[i])); |
| 203 | } else { |
| 204 | args.add(String16(argv[i])); |
Felipe Leme | 859aef6 | 2016-02-03 12:17:10 -0800 | [diff] [blame] | 205 | } |
| 206 | } |
| 207 | } |
Thierry Strudel | 8b78b75 | 2016-03-22 10:25:44 -0700 | [diff] [blame] | 208 | |
| 209 | if ((skipServices && skippedServices.empty()) || |
Steven Moreland | 2c3cd83 | 2017-02-13 23:44:17 +0000 | [diff] [blame] | 210 | (showListOnly && (!services.empty() || !skippedServices.empty()))) { |
Thierry Strudel | 8b78b75 | 2016-03-22 10:25:44 -0700 | [diff] [blame] | 211 | usage(); |
| 212 | return -1; |
| 213 | } |
| 214 | |
Thierry Strudel | 159a832 | 2016-03-23 11:22:34 -0700 | [diff] [blame] | 215 | if (services.empty() || showListOnly) { |
Vishnu Nair | e4f6174 | 2017-12-21 08:30:28 -0800 | [diff] [blame] | 216 | services = listServices(priorityFlags, asProto); |
| 217 | setServiceArgs(args, asProto, priorityFlags); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | const size_t N = services.size(); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 221 | if (N > 1) { |
| 222 | // first print a list of the current services |
| 223 | aout << "Currently running services:" << endl; |
Felipe Leme | bbfd2b8 | 2016-02-03 11:16:27 -0800 | [diff] [blame] | 224 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 225 | for (size_t i=0; i<N; i++) { |
Felipe Leme | 343175a | 2016-08-02 18:57:37 -0700 | [diff] [blame] | 226 | sp<IBinder> service = sm_->checkService(services[i]); |
| 227 | |
| 228 | if (service != nullptr) { |
Felipe Leme | 859aef6 | 2016-02-03 12:17:10 -0800 | [diff] [blame] | 229 | bool skipped = IsSkipped(skippedServices, services[i]); |
| 230 | aout << " " << services[i] << (skipped ? " (skipped)" : "") << endl; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 231 | } |
| 232 | } |
| 233 | } |
| 234 | |
keunyoung | caad555 | 2013-06-13 15:08:51 -0700 | [diff] [blame] | 235 | if (showListOnly) { |
| 236 | return 0; |
| 237 | } |
| 238 | |
Josh Gao | 4b8f6f9 | 2016-02-29 16:20:34 -0800 | [diff] [blame] | 239 | for (size_t i = 0; i < N; i++) { |
Vishnu Nair | e4f6174 | 2017-12-21 08:30:28 -0800 | [diff] [blame] | 240 | const String16& serviceName = services[i]; |
| 241 | if (IsSkipped(skippedServices, serviceName)) continue; |
Felipe Leme | 859aef6 | 2016-02-03 12:17:10 -0800 | [diff] [blame] | 242 | |
Vishnu Nair | e4f6174 | 2017-12-21 08:30:28 -0800 | [diff] [blame] | 243 | if (startDumpThread(serviceName, args) == OK) { |
| 244 | bool addSeparator = (N > 1); |
| 245 | if (addSeparator) { |
| 246 | writeDumpHeader(STDOUT_FILENO, serviceName, priorityFlags); |
Josh Gao | 4b8f6f9 | 2016-02-29 16:20:34 -0800 | [diff] [blame] | 247 | } |
Vishnu Nair | e4f6174 | 2017-12-21 08:30:28 -0800 | [diff] [blame] | 248 | std::chrono::duration<double> elapsedDuration; |
| 249 | size_t bytesWritten = 0; |
| 250 | status_t status = |
| 251 | writeDump(STDOUT_FILENO, serviceName, std::chrono::milliseconds(timeoutArgMs), |
| 252 | asProto, elapsedDuration, bytesWritten); |
Josh Gao | 4b8f6f9 | 2016-02-29 16:20:34 -0800 | [diff] [blame] | 253 | |
Vishnu Nair | e4f6174 | 2017-12-21 08:30:28 -0800 | [diff] [blame] | 254 | if (status == TIMED_OUT) { |
Felipe Leme | 343175a | 2016-08-02 18:57:37 -0700 | [diff] [blame] | 255 | aout << endl |
Vishnu Nair | e4f6174 | 2017-12-21 08:30:28 -0800 | [diff] [blame] | 256 | << "*** SERVICE '" << serviceName << "' DUMP TIMEOUT (" << timeoutArgMs |
Vishnu Nair | 6921f80 | 2017-11-22 09:17:23 -0800 | [diff] [blame] | 257 | << "ms) EXPIRED ***" << endl |
Felipe Leme | 343175a | 2016-08-02 18:57:37 -0700 | [diff] [blame] | 258 | << endl; |
Josh Gao | 4b8f6f9 | 2016-02-29 16:20:34 -0800 | [diff] [blame] | 259 | } |
| 260 | |
Vishnu Nair | e4f6174 | 2017-12-21 08:30:28 -0800 | [diff] [blame] | 261 | if (addSeparator) { |
| 262 | writeDumpFooter(STDOUT_FILENO, serviceName, elapsedDuration); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 263 | } |
Vishnu Nair | e4f6174 | 2017-12-21 08:30:28 -0800 | [diff] [blame] | 264 | bool dumpComplete = (status == OK); |
| 265 | stopDumpThread(dumpComplete); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 266 | } |
| 267 | } |
| 268 | |
| 269 | return 0; |
| 270 | } |
Vishnu Nair | e4f6174 | 2017-12-21 08:30:28 -0800 | [diff] [blame] | 271 | |
| 272 | Vector<String16> Dumpsys::listServices(int priorityFilterFlags, bool filterByProto) const { |
| 273 | Vector<String16> services = sm_->listServices(priorityFilterFlags); |
| 274 | services.sort(sort_func); |
| 275 | if (filterByProto) { |
| 276 | Vector<String16> protoServices = sm_->listServices(IServiceManager::DUMP_FLAG_PROTO); |
| 277 | protoServices.sort(sort_func); |
| 278 | Vector<String16> intersection; |
| 279 | std::set_intersection(services.begin(), services.end(), protoServices.begin(), |
| 280 | protoServices.end(), std::back_inserter(intersection)); |
| 281 | services = std::move(intersection); |
| 282 | } |
| 283 | return services; |
| 284 | } |
| 285 | |
| 286 | void Dumpsys::setServiceArgs(Vector<String16>& args, bool asProto, int priorityFlags) const { |
| 287 | if ((priorityFlags == IServiceManager::DUMP_FLAG_PRIORITY_ALL) || |
| 288 | (priorityFlags == IServiceManager::DUMP_FLAG_PRIORITY_NORMAL)) { |
| 289 | args.add(String16("-a")); |
| 290 | } |
| 291 | if (asProto) { |
| 292 | args.insertAt(String16(PriorityDumper::PROTO_ARG), 0); |
| 293 | } |
| 294 | if (priorityFlags != IServiceManager::DUMP_FLAG_PRIORITY_ALL) { |
| 295 | String16 priorityType = ConvertBitmaskToPriorityType(priorityFlags); |
| 296 | args.insertAt(String16(PriorityDumper::PRIORITY_ARG), 0); |
| 297 | args.insertAt(priorityType, 1); |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | status_t Dumpsys::startDumpThread(const String16& serviceName, const Vector<String16>& args) { |
| 302 | sp<IBinder> service = sm_->checkService(serviceName); |
| 303 | if (service == nullptr) { |
| 304 | aerr << "Can't find service: " << serviceName << endl; |
| 305 | return NAME_NOT_FOUND; |
| 306 | } |
| 307 | |
| 308 | int sfd[2]; |
| 309 | if (pipe(sfd) != 0) { |
| 310 | aerr << "Failed to create pipe to dump service info for " << serviceName << ": " |
| 311 | << strerror(errno) << endl; |
| 312 | return -errno; |
| 313 | } |
| 314 | |
| 315 | redirectFd_ = unique_fd(sfd[0]); |
| 316 | unique_fd remote_end(sfd[1]); |
| 317 | sfd[0] = sfd[1] = -1; |
| 318 | |
| 319 | // dump blocks until completion, so spawn a thread.. |
| 320 | activeThread_ = std::thread([=, remote_end{std::move(remote_end)}]() mutable { |
| 321 | int err = service->dump(remote_end.get(), args); |
| 322 | |
| 323 | // It'd be nice to be able to close the remote end of the socketpair before the dump |
| 324 | // call returns, to terminate our reads if the other end closes their copy of the |
| 325 | // file descriptor, but then hangs for some reason. There doesn't seem to be a good |
| 326 | // way to do this, though. |
| 327 | remote_end.reset(); |
| 328 | |
| 329 | if (err != 0) { |
| 330 | aerr << "Error dumping service info: (" << strerror(err) << ") " |
| 331 | << serviceName << endl; |
| 332 | } |
| 333 | }); |
| 334 | return OK; |
| 335 | } |
| 336 | |
| 337 | void Dumpsys::stopDumpThread(bool dumpComplete) { |
| 338 | if (dumpComplete) { |
| 339 | activeThread_.join(); |
| 340 | } else { |
| 341 | activeThread_.detach(); |
| 342 | } |
| 343 | /* close read end of the dump output redirection pipe */ |
| 344 | redirectFd_.reset(); |
| 345 | } |
| 346 | |
| 347 | void Dumpsys::writeDumpHeader(int fd, const String16& serviceName, int priorityFlags) const { |
| 348 | std::string msg( |
| 349 | "----------------------------------------" |
| 350 | "---------------------------------------\n"); |
| 351 | if (priorityFlags == IServiceManager::DUMP_FLAG_PRIORITY_ALL || |
| 352 | priorityFlags == IServiceManager::DUMP_FLAG_PRIORITY_NORMAL) { |
| 353 | StringAppendF(&msg, "DUMP OF SERVICE %s:\n", String8(serviceName).c_str()); |
| 354 | } else { |
| 355 | String16 priorityType = ConvertBitmaskToPriorityType(priorityFlags); |
| 356 | StringAppendF(&msg, "DUMP OF SERVICE %s %s:\n", String8(priorityType).c_str(), |
| 357 | String8(serviceName).c_str()); |
| 358 | } |
| 359 | WriteStringToFd(msg, fd); |
| 360 | } |
| 361 | |
| 362 | status_t Dumpsys::writeDump(int fd, const String16& serviceName, std::chrono::milliseconds timeout, |
| 363 | bool asProto, std::chrono::duration<double>& elapsedDuration, |
| 364 | size_t& bytesWritten) const { |
| 365 | status_t status = OK; |
| 366 | size_t totalBytes = 0; |
| 367 | auto start = std::chrono::steady_clock::now(); |
| 368 | auto end = start + timeout; |
| 369 | |
| 370 | int serviceDumpFd = redirectFd_.get(); |
| 371 | if (serviceDumpFd == -1) { |
| 372 | return INVALID_OPERATION; |
| 373 | } |
| 374 | |
| 375 | struct pollfd pfd = {.fd = serviceDumpFd, .events = POLLIN}; |
| 376 | |
| 377 | while (true) { |
| 378 | // Wrap this in a lambda so that TEMP_FAILURE_RETRY recalculates the timeout. |
| 379 | auto time_left_ms = [end]() { |
| 380 | auto now = std::chrono::steady_clock::now(); |
| 381 | auto diff = std::chrono::duration_cast<std::chrono::milliseconds>(end - now); |
| 382 | return std::max(diff.count(), 0ll); |
| 383 | }; |
| 384 | |
| 385 | int rc = TEMP_FAILURE_RETRY(poll(&pfd, 1, time_left_ms())); |
| 386 | if (rc < 0) { |
| 387 | aerr << "Error in poll while dumping service " << serviceName << " : " |
| 388 | << strerror(errno) << endl; |
| 389 | status = -errno; |
| 390 | break; |
| 391 | } else if (rc == 0) { |
| 392 | status = TIMED_OUT; |
| 393 | break; |
| 394 | } |
| 395 | |
| 396 | char buf[4096]; |
| 397 | rc = TEMP_FAILURE_RETRY(read(redirectFd_.get(), buf, sizeof(buf))); |
| 398 | if (rc < 0) { |
| 399 | aerr << "Failed to read while dumping service " << serviceName << ": " |
| 400 | << strerror(errno) << endl; |
| 401 | status = -errno; |
| 402 | break; |
| 403 | } else if (rc == 0) { |
| 404 | // EOF. |
| 405 | break; |
| 406 | } |
| 407 | |
| 408 | if (!WriteFully(fd, buf, rc)) { |
| 409 | aerr << "Failed to write while dumping service " << serviceName << ": " |
| 410 | << strerror(errno) << endl; |
| 411 | status = -errno; |
| 412 | break; |
| 413 | } |
| 414 | totalBytes += rc; |
| 415 | } |
| 416 | |
| 417 | if ((status == TIMED_OUT) && (!asProto)) { |
| 418 | std::string msg = StringPrintf("\n*** SERVICE '%s' DUMP TIMEOUT (%llums) EXPIRED ***\n\n", |
| 419 | String8(serviceName).string(), timeout.count()); |
| 420 | WriteStringToFd(msg, fd); |
| 421 | } |
| 422 | |
| 423 | elapsedDuration = std::chrono::steady_clock::now() - start; |
| 424 | bytesWritten = totalBytes; |
| 425 | return status; |
| 426 | } |
| 427 | |
| 428 | void Dumpsys::writeDumpFooter(int fd, const String16& serviceName, |
| 429 | const std::chrono::duration<double>& elapsedDuration) const { |
| 430 | using std::chrono::system_clock; |
| 431 | const auto finish = system_clock::to_time_t(system_clock::now()); |
| 432 | std::tm finish_tm; |
| 433 | localtime_r(&finish, &finish_tm); |
| 434 | std::stringstream oss; |
| 435 | oss << std::put_time(&finish_tm, "%Y-%m-%d %H:%M:%S"); |
| 436 | std::string msg = |
| 437 | StringPrintf("--------- %.3fs was the duration of dumpsys %s, ending at: %s\n", |
| 438 | elapsedDuration.count(), String8(serviceName).string(), oss.str().c_str()); |
| 439 | WriteStringToFd(msg, fd); |
| 440 | } |