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