| 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> | 
| Steven Moreland | e5a6a87 | 2021-05-19 22:11:37 +0000 | [diff] [blame] | 27 | #include <binder/Stability.h> | 
| Mathias Agopian | 002e1e5 | 2013-05-06 20:20:50 -0700 | [diff] [blame] | 28 | #include <binder/TextOutput.h> | 
| Devin Moore | cfeeda4 | 2020-12-08 12:50:58 -0800 | [diff] [blame] | 29 | #include <binderdebug/BinderDebug.h> | 
| Vishnu Nair | 780b128 | 2017-10-10 13:57:24 -0700 | [diff] [blame] | 30 | #include <serviceutils/PriorityDumper.h> | 
| Josh Gao | 4b8f6f9 | 2016-02-29 16:20:34 -0800 | [diff] [blame] | 31 | #include <utils/Log.h> | 
| Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 32 | #include <utils/Vector.h> | 
|  | 33 |  | 
| Steven Moreland | 3657ee5 | 2020-01-31 12:09:15 -0800 | [diff] [blame] | 34 | #include <iostream> | 
| Josh Gao | 4b8f6f9 | 2016-02-29 16:20:34 -0800 | [diff] [blame] | 35 | #include <fcntl.h> | 
| Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 36 | #include <getopt.h> | 
| Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 37 | #include <stdio.h> | 
| Josh Gao | 4b8f6f9 | 2016-02-29 16:20:34 -0800 | [diff] [blame] | 38 | #include <stdlib.h> | 
| Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 39 | #include <string.h> | 
| Josh Gao | 4b8f6f9 | 2016-02-29 16:20:34 -0800 | [diff] [blame] | 40 | #include <sys/poll.h> | 
|  | 41 | #include <sys/socket.h> | 
| Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 42 | #include <sys/time.h> | 
| Josh Gao | 4b8f6f9 | 2016-02-29 16:20:34 -0800 | [diff] [blame] | 43 | #include <sys/types.h> | 
|  | 44 | #include <unistd.h> | 
| Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 45 |  | 
| Felipe Leme | 343175a | 2016-08-02 18:57:37 -0700 | [diff] [blame] | 46 | #include "dumpsys.h" | 
|  | 47 |  | 
| Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 48 | using namespace android; | 
| Vishnu Nair | e4f6174 | 2017-12-21 08:30:28 -0800 | [diff] [blame] | 49 | using ::android::base::StringAppendF; | 
|  | 50 | using ::android::base::StringPrintf; | 
|  | 51 | using ::android::base::unique_fd; | 
|  | 52 | using ::android::base::WriteFully; | 
|  | 53 | using ::android::base::WriteStringToFd; | 
| Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 54 |  | 
| Steven Moreland | 2c3cd83 | 2017-02-13 23:44:17 +0000 | [diff] [blame] | 55 | static int sort_func(const String16* lhs, const String16* rhs) | 
|  | 56 | { | 
| Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 57 | return lhs->compare(*rhs); | 
|  | 58 | } | 
|  | 59 |  | 
| Felipe Leme | bbfd2b8 | 2016-02-03 11:16:27 -0800 | [diff] [blame] | 60 | static void usage() { | 
|  | 61 | fprintf(stderr, | 
| Vishnu Nair | f56042d | 2017-09-19 15:25:10 -0700 | [diff] [blame] | 62 | "usage: dumpsys\n" | 
| Felipe Leme | bbfd2b8 | 2016-02-03 11:16:27 -0800 | [diff] [blame] | 63 | "         To dump all services.\n" | 
|  | 64 | "or:\n" | 
| Steven Moreland | cbd69fc | 2021-07-20 20:45:43 +0000 | [diff] [blame] | 65 | "       dumpsys [-t TIMEOUT] [--priority LEVEL] [--dump] [--pid] [--thread] [--help | " | 
|  | 66 | "-l | --skip SERVICES " | 
| Steven Moreland | 5a30d34 | 2019-10-08 13:53:28 -0700 | [diff] [blame] | 67 | "| SERVICE [ARGS]]\n" | 
| Felipe Leme | bbfd2b8 | 2016-02-03 11:16:27 -0800 | [diff] [blame] | 68 | "         --help: shows this help\n" | 
|  | 69 | "         -l: only list services, do not dump them\n" | 
| Vishnu Nair | 6921f80 | 2017-11-22 09:17:23 -0800 | [diff] [blame] | 70 | "         -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 Moreland | cbd69fc | 2021-07-20 20:45:43 +0000 | [diff] [blame] | 72 | "         --dump: ask the service to dump itself (this is the default)\n" | 
| Steven Moreland | 5a30d34 | 2019-10-08 13:53:28 -0700 | [diff] [blame] | 73 | "         --pid: dump PID instead of usual dump\n" | 
| Ng Zhi An | cf2d01b | 2018-09-14 09:47:43 -0700 | [diff] [blame] | 74 | "         --proto: filter services that support dumping data in proto format. Dumps\n" | 
| Vishnu Nair | 6a40853 | 2017-10-24 09:11:27 -0700 | [diff] [blame] | 75 | "               will be in proto format.\n" | 
| Vishnu Nair | f56042d | 2017-09-19 15:25:10 -0700 | [diff] [blame] | 76 | "         --priority LEVEL: filter services based on specified priority\n" | 
|  | 77 | "               LEVEL must be one of CRITICAL | HIGH | NORMAL\n" | 
| Felipe Leme | 859aef6 | 2016-02-03 12:17:10 -0800 | [diff] [blame] | 78 | "         --skip SERVICES: dumps all services but SERVICES (comma-separated list)\n" | 
| Steven Moreland | e5a6a87 | 2021-05-19 22:11:37 +0000 | [diff] [blame] | 79 | "         --stability: dump binder stability information instead of usual dump\n" | 
|  | 80 | "         --thread: dump thread usage instead of usual dump\n" | 
| Felipe Leme | 859aef6 | 2016-02-03 12:17:10 -0800 | [diff] [blame] | 81 | "         SERVICE [ARGS]: dumps only service SERVICE, optionally passing ARGS to it\n"); | 
|  | 82 | } | 
|  | 83 |  | 
| Felipe Leme | 343175a | 2016-08-02 18:57:37 -0700 | [diff] [blame] | 84 | static bool IsSkipped(const Vector<String16>& skipped, const String16& service) { | 
| Felipe Leme | 859aef6 | 2016-02-03 12:17:10 -0800 | [diff] [blame] | 85 | for (const auto& candidate : skipped) { | 
|  | 86 | if (candidate == service) { | 
|  | 87 | return true; | 
|  | 88 | } | 
|  | 89 | } | 
|  | 90 | return false; | 
| Felipe Leme | bbfd2b8 | 2016-02-03 11:16:27 -0800 | [diff] [blame] | 91 | } | 
|  | 92 |  | 
| Vishnu Nair | 6a40853 | 2017-10-24 09:11:27 -0700 | [diff] [blame] | 93 | static bool ConvertPriorityTypeToBitmask(const String16& type, int& bitmask) { | 
|  | 94 | if (type == PriorityDumper::PRIORITY_ARG_CRITICAL) { | 
|  | 95 | bitmask = IServiceManager::DUMP_FLAG_PRIORITY_CRITICAL; | 
| Vishnu Nair | 780b128 | 2017-10-10 13:57:24 -0700 | [diff] [blame] | 96 | return true; | 
|  | 97 | } | 
| Vishnu Nair | 6a40853 | 2017-10-24 09:11:27 -0700 | [diff] [blame] | 98 | if (type == PriorityDumper::PRIORITY_ARG_HIGH) { | 
|  | 99 | bitmask = IServiceManager::DUMP_FLAG_PRIORITY_HIGH; | 
| Vishnu Nair | 780b128 | 2017-10-10 13:57:24 -0700 | [diff] [blame] | 100 | return true; | 
|  | 101 | } | 
| Vishnu Nair | 6a40853 | 2017-10-24 09:11:27 -0700 | [diff] [blame] | 102 | if (type == PriorityDumper::PRIORITY_ARG_NORMAL) { | 
|  | 103 | bitmask = IServiceManager::DUMP_FLAG_PRIORITY_NORMAL; | 
| Vishnu Nair | 780b128 | 2017-10-10 13:57:24 -0700 | [diff] [blame] | 104 | return true; | 
|  | 105 | } | 
|  | 106 | return false; | 
|  | 107 | } | 
|  | 108 |  | 
| Vishnu Nair | e4f6174 | 2017-12-21 08:30:28 -0800 | [diff] [blame] | 109 | String16 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 Leme | 343175a | 2016-08-02 18:57:37 -0700 | [diff] [blame] | 122 | int Dumpsys::main(int argc, char* const argv[]) { | 
| Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 123 | Vector<String16> services; | 
|  | 124 | Vector<String16> args; | 
| Vishnu Nair | 780b128 | 2017-10-10 13:57:24 -0700 | [diff] [blame] | 125 | String16 priorityType; | 
| Felipe Leme | 859aef6 | 2016-02-03 12:17:10 -0800 | [diff] [blame] | 126 | Vector<String16> skippedServices; | 
| Vishnu Nair | 6a40853 | 2017-10-24 09:11:27 -0700 | [diff] [blame] | 127 | Vector<String16> protoServices; | 
| keunyoung | caad555 | 2013-06-13 15:08:51 -0700 | [diff] [blame] | 128 | bool showListOnly = false; | 
| Thierry Strudel | 8b78b75 | 2016-03-22 10:25:44 -0700 | [diff] [blame] | 129 | bool skipServices = false; | 
| Vishnu Nair | e4f6174 | 2017-12-21 08:30:28 -0800 | [diff] [blame] | 130 | bool asProto = false; | 
| Steven Moreland | cbd69fc | 2021-07-20 20:45:43 +0000 | [diff] [blame] | 131 | int dumpTypeFlags = 0; | 
| Vishnu Nair | 6921f80 | 2017-11-22 09:17:23 -0800 | [diff] [blame] | 132 | int timeoutArgMs = 10000; | 
| Vishnu Nair | e4f6174 | 2017-12-21 08:30:28 -0800 | [diff] [blame] | 133 | int priorityFlags = IServiceManager::DUMP_FLAG_PRIORITY_ALL; | 
| Steven Moreland | e5a6a87 | 2021-05-19 22:11:37 +0000 | [diff] [blame] | 134 | static struct option longOptions[] = {{"help", no_argument, 0, 0}, | 
| Steven Moreland | cbd69fc | 2021-07-20 20:45:43 +0000 | [diff] [blame] | 135 | {"dump", no_argument, 0, 0}, | 
| Devin Moore | cfeeda4 | 2020-12-08 12:50:58 -0800 | [diff] [blame] | 136 | {"pid", no_argument, 0, 0}, | 
| Steven Moreland | 5a30d34 | 2019-10-08 13:53:28 -0700 | [diff] [blame] | 137 | {"priority", required_argument, 0, 0}, | 
| Vishnu Nair | 6a40853 | 2017-10-24 09:11:27 -0700 | [diff] [blame] | 138 | {"proto", no_argument, 0, 0}, | 
| Vishnu Nair | f56042d | 2017-09-19 15:25:10 -0700 | [diff] [blame] | 139 | {"skip", no_argument, 0, 0}, | 
| Steven Moreland | e5a6a87 | 2021-05-19 22:11:37 +0000 | [diff] [blame] | 140 | {"stability", no_argument, 0, 0}, | 
|  | 141 | {"thread", no_argument, 0, 0}, | 
| Vishnu Nair | f56042d | 2017-09-19 15:25:10 -0700 | [diff] [blame] | 142 | {0, 0, 0, 0}}; | 
| Thierry Strudel | 8b78b75 | 2016-03-22 10:25:44 -0700 | [diff] [blame] | 143 |  | 
| Felipe Leme | 343175a | 2016-08-02 18:57:37 -0700 | [diff] [blame] | 144 | // Must reset optind, otherwise subsequent calls will fail (wouldn't happen on main.cpp, but | 
|  | 145 | // happens on test cases). | 
|  | 146 | optind = 1; | 
| Thierry Strudel | 8b78b75 | 2016-03-22 10:25:44 -0700 | [diff] [blame] | 147 | while (1) { | 
|  | 148 | int c; | 
|  | 149 | int optionIndex = 0; | 
|  | 150 |  | 
| Vishnu Nair | 6921f80 | 2017-11-22 09:17:23 -0800 | [diff] [blame] | 151 | c = getopt_long(argc, argv, "+t:T:l", longOptions, &optionIndex); | 
| Thierry Strudel | 8b78b75 | 2016-03-22 10:25:44 -0700 | [diff] [blame] | 152 |  | 
|  | 153 | if (c == -1) { | 
|  | 154 | break; | 
| Felipe Leme | bbfd2b8 | 2016-02-03 11:16:27 -0800 | [diff] [blame] | 155 | } | 
| Thierry Strudel | 8b78b75 | 2016-03-22 10:25:44 -0700 | [diff] [blame] | 156 |  | 
|  | 157 | switch (c) { | 
|  | 158 | case 0: | 
|  | 159 | if (!strcmp(longOptions[optionIndex].name, "skip")) { | 
|  | 160 | skipServices = true; | 
| Vishnu Nair | 6a40853 | 2017-10-24 09:11:27 -0700 | [diff] [blame] | 161 | } else if (!strcmp(longOptions[optionIndex].name, "proto")) { | 
| Vishnu Nair | e4f6174 | 2017-12-21 08:30:28 -0800 | [diff] [blame] | 162 | asProto = true; | 
| Thierry Strudel | 8b78b75 | 2016-03-22 10:25:44 -0700 | [diff] [blame] | 163 | } else if (!strcmp(longOptions[optionIndex].name, "help")) { | 
|  | 164 | usage(); | 
|  | 165 | return 0; | 
| Vishnu Nair | f56042d | 2017-09-19 15:25:10 -0700 | [diff] [blame] | 166 | } else if (!strcmp(longOptions[optionIndex].name, "priority")) { | 
| Vishnu Nair | 780b128 | 2017-10-10 13:57:24 -0700 | [diff] [blame] | 167 | priorityType = String16(String8(optarg)); | 
| Vishnu Nair | e4f6174 | 2017-12-21 08:30:28 -0800 | [diff] [blame] | 168 | if (!ConvertPriorityTypeToBitmask(priorityType, priorityFlags)) { | 
| Vishnu Nair | f56042d | 2017-09-19 15:25:10 -0700 | [diff] [blame] | 169 | fprintf(stderr, "\n"); | 
|  | 170 | usage(); | 
|  | 171 | return -1; | 
|  | 172 | } | 
| Steven Moreland | cbd69fc | 2021-07-20 20:45:43 +0000 | [diff] [blame] | 173 | } else if (!strcmp(longOptions[optionIndex].name, "dump")) { | 
|  | 174 | dumpTypeFlags |= TYPE_DUMP; | 
| Steven Moreland | 5a30d34 | 2019-10-08 13:53:28 -0700 | [diff] [blame] | 175 | } else if (!strcmp(longOptions[optionIndex].name, "pid")) { | 
| Steven Moreland | cbd69fc | 2021-07-20 20:45:43 +0000 | [diff] [blame] | 176 | dumpTypeFlags |= TYPE_PID; | 
| Steven Moreland | e5a6a87 | 2021-05-19 22:11:37 +0000 | [diff] [blame] | 177 | } else if (!strcmp(longOptions[optionIndex].name, "stability")) { | 
| Steven Moreland | cbd69fc | 2021-07-20 20:45:43 +0000 | [diff] [blame] | 178 | dumpTypeFlags |= TYPE_STABILITY; | 
| Devin Moore | cfeeda4 | 2020-12-08 12:50:58 -0800 | [diff] [blame] | 179 | } else if (!strcmp(longOptions[optionIndex].name, "thread")) { | 
| Steven Moreland | cbd69fc | 2021-07-20 20:45:43 +0000 | [diff] [blame] | 180 | dumpTypeFlags |= TYPE_THREAD; | 
| Thierry Strudel | 8b78b75 | 2016-03-22 10:25:44 -0700 | [diff] [blame] | 181 | } | 
|  | 182 | break; | 
|  | 183 |  | 
|  | 184 | case 't': | 
|  | 185 | { | 
| Vishnu Nair | 6921f80 | 2017-11-22 09:17:23 -0800 | [diff] [blame] | 186 | 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 Strudel | 8b78b75 | 2016-03-22 10:25:44 -0700 | [diff] [blame] | 202 | return -1; | 
|  | 203 | } | 
|  | 204 | } | 
|  | 205 | break; | 
|  | 206 |  | 
|  | 207 | case 'l': | 
| Felipe Leme | bbfd2b8 | 2016-02-03 11:16:27 -0800 | [diff] [blame] | 208 | showListOnly = true; | 
| Thierry Strudel | 8b78b75 | 2016-03-22 10:25:44 -0700 | [diff] [blame] | 209 | break; | 
|  | 210 |  | 
|  | 211 | default: | 
|  | 212 | fprintf(stderr, "\n"); | 
|  | 213 | usage(); | 
|  | 214 | return -1; | 
| Felipe Leme | bbfd2b8 | 2016-02-03 11:16:27 -0800 | [diff] [blame] | 215 | } | 
| keunyoung | caad555 | 2013-06-13 15:08:51 -0700 | [diff] [blame] | 216 | } | 
| Thierry Strudel | 8b78b75 | 2016-03-22 10:25:44 -0700 | [diff] [blame] | 217 |  | 
| Steven Moreland | cbd69fc | 2021-07-20 20:45:43 +0000 | [diff] [blame] | 218 | if (dumpTypeFlags == 0) { | 
|  | 219 | dumpTypeFlags = TYPE_DUMP; | 
|  | 220 | } | 
|  | 221 |  | 
| Thierry Strudel | 8b78b75 | 2016-03-22 10:25:44 -0700 | [diff] [blame] | 222 | 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 Hsu | c66abb2 | 2019-09-27 16:10:21 +0800 | [diff] [blame] | 229 | 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 Leme | 859aef6 | 2016-02-03 12:17:10 -0800 | [diff] [blame] | 236 | } | 
|  | 237 | } | 
|  | 238 | } | 
| Thierry Strudel | 8b78b75 | 2016-03-22 10:25:44 -0700 | [diff] [blame] | 239 |  | 
|  | 240 | if ((skipServices && skippedServices.empty()) || | 
| Steven Moreland | 2c3cd83 | 2017-02-13 23:44:17 +0000 | [diff] [blame] | 241 | (showListOnly && (!services.empty() || !skippedServices.empty()))) { | 
| Thierry Strudel | 8b78b75 | 2016-03-22 10:25:44 -0700 | [diff] [blame] | 242 | usage(); | 
|  | 243 | return -1; | 
|  | 244 | } | 
|  | 245 |  | 
| Thierry Strudel | 159a832 | 2016-03-23 11:22:34 -0700 | [diff] [blame] | 246 | if (services.empty() || showListOnly) { | 
| Vishnu Nair | e4f6174 | 2017-12-21 08:30:28 -0800 | [diff] [blame] | 247 | services = listServices(priorityFlags, asProto); | 
|  | 248 | setServiceArgs(args, asProto, priorityFlags); | 
| Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 249 | } | 
|  | 250 |  | 
|  | 251 | const size_t N = services.size(); | 
| Steven Moreland | 31dac35 | 2020-03-05 09:46:45 -0800 | [diff] [blame] | 252 | if (N > 1 || showListOnly) { | 
| Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 253 | // first print a list of the current services | 
| Steven Moreland | 3657ee5 | 2020-01-31 12:09:15 -0800 | [diff] [blame] | 254 | std::cout << "Currently running services:" << std::endl; | 
| Felipe Leme | bbfd2b8 | 2016-02-03 11:16:27 -0800 | [diff] [blame] | 255 |  | 
| Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 256 | for (size_t i=0; i<N; i++) { | 
| Felipe Leme | 343175a | 2016-08-02 18:57:37 -0700 | [diff] [blame] | 257 | sp<IBinder> service = sm_->checkService(services[i]); | 
|  | 258 |  | 
|  | 259 | if (service != nullptr) { | 
| Felipe Leme | 859aef6 | 2016-02-03 12:17:10 -0800 | [diff] [blame] | 260 | bool skipped = IsSkipped(skippedServices, services[i]); | 
| Steven Moreland | 3657ee5 | 2020-01-31 12:09:15 -0800 | [diff] [blame] | 261 | std::cout << "  " << services[i] << (skipped ? " (skipped)" : "") << std::endl; | 
| Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 262 | } | 
|  | 263 | } | 
|  | 264 | } | 
|  | 265 |  | 
| keunyoung | caad555 | 2013-06-13 15:08:51 -0700 | [diff] [blame] | 266 | if (showListOnly) { | 
|  | 267 | return 0; | 
|  | 268 | } | 
|  | 269 |  | 
| Josh Gao | 4b8f6f9 | 2016-02-29 16:20:34 -0800 | [diff] [blame] | 270 | for (size_t i = 0; i < N; i++) { | 
| Vishnu Nair | e4f6174 | 2017-12-21 08:30:28 -0800 | [diff] [blame] | 271 | const String16& serviceName = services[i]; | 
|  | 272 | if (IsSkipped(skippedServices, serviceName)) continue; | 
| Felipe Leme | 859aef6 | 2016-02-03 12:17:10 -0800 | [diff] [blame] | 273 |  | 
| Steven Moreland | cbd69fc | 2021-07-20 20:45:43 +0000 | [diff] [blame] | 274 | if (startDumpThread(dumpTypeFlags, serviceName, args) == OK) { | 
| Vishnu Nair | e4f6174 | 2017-12-21 08:30:28 -0800 | [diff] [blame] | 275 | bool addSeparator = (N > 1); | 
|  | 276 | if (addSeparator) { | 
|  | 277 | writeDumpHeader(STDOUT_FILENO, serviceName, priorityFlags); | 
| Josh Gao | 4b8f6f9 | 2016-02-29 16:20:34 -0800 | [diff] [blame] | 278 | } | 
| Vishnu Nair | e4f6174 | 2017-12-21 08:30:28 -0800 | [diff] [blame] | 279 | 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 Gao | 4b8f6f9 | 2016-02-29 16:20:34 -0800 | [diff] [blame] | 284 |  | 
| Vishnu Nair | e4f6174 | 2017-12-21 08:30:28 -0800 | [diff] [blame] | 285 | if (status == TIMED_OUT) { | 
| Steven Moreland | 3657ee5 | 2020-01-31 12:09:15 -0800 | [diff] [blame] | 286 | std::cout << std::endl | 
| Vishnu Nair | e4f6174 | 2017-12-21 08:30:28 -0800 | [diff] [blame] | 287 | << "*** SERVICE '" << serviceName << "' DUMP TIMEOUT (" << timeoutArgMs | 
| Steven Moreland | 3657ee5 | 2020-01-31 12:09:15 -0800 | [diff] [blame] | 288 | << "ms) EXPIRED ***" << std::endl | 
|  | 289 | << std::endl; | 
| Josh Gao | 4b8f6f9 | 2016-02-29 16:20:34 -0800 | [diff] [blame] | 290 | } | 
|  | 291 |  | 
| Vishnu Nair | e4f6174 | 2017-12-21 08:30:28 -0800 | [diff] [blame] | 292 | if (addSeparator) { | 
|  | 293 | writeDumpFooter(STDOUT_FILENO, serviceName, elapsedDuration); | 
| Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 294 | } | 
| Vishnu Nair | e4f6174 | 2017-12-21 08:30:28 -0800 | [diff] [blame] | 295 | bool dumpComplete = (status == OK); | 
|  | 296 | stopDumpThread(dumpComplete); | 
| Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 297 | } | 
|  | 298 | } | 
|  | 299 |  | 
|  | 300 | return 0; | 
|  | 301 | } | 
| Vishnu Nair | e4f6174 | 2017-12-21 08:30:28 -0800 | [diff] [blame] | 302 |  | 
|  | 303 | Vector<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 Nair | e97d612 | 2018-01-18 13:58:56 -0800 | [diff] [blame] | 317 | void Dumpsys::setServiceArgs(Vector<String16>& args, bool asProto, int priorityFlags) { | 
| Vishnu Nair | 64afc02 | 2018-02-01 15:29:34 -0800 | [diff] [blame] | 318 | // Add proto flag if dumping service as proto. | 
| Vishnu Nair | e4f6174 | 2017-12-21 08:30:28 -0800 | [diff] [blame] | 319 | if (asProto) { | 
|  | 320 | args.insertAt(String16(PriorityDumper::PROTO_ARG), 0); | 
|  | 321 | } | 
| Vishnu Nair | 64afc02 | 2018-02-01 15:29:34 -0800 | [diff] [blame] | 322 |  | 
|  | 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 Nair | e4f6174 | 2017-12-21 08:30:28 -0800 | [diff] [blame] | 335 | String16 priorityType = ConvertBitmaskToPriorityType(priorityFlags); | 
|  | 336 | args.insertAt(String16(PriorityDumper::PRIORITY_ARG), 0); | 
|  | 337 | args.insertAt(priorityType, 1); | 
|  | 338 | } | 
|  | 339 | } | 
|  | 340 |  | 
| Steven Moreland | cbd69fc | 2021-07-20 20:45:43 +0000 | [diff] [blame] | 341 | static status_t dumpPidToFd(const sp<IBinder>& service, const unique_fd& fd, bool exclusive) { | 
| Steven Moreland | 5a30d34 | 2019-10-08 13:53:28 -0700 | [diff] [blame] | 342 | pid_t pid; | 
|  | 343 | status_t status = service->getDebugPid(&pid); | 
|  | 344 | if (status != OK) { | 
|  | 345 | return status; | 
|  | 346 | } | 
| Steven Moreland | cbd69fc | 2021-07-20 20:45:43 +0000 | [diff] [blame] | 347 | if (!exclusive) { | 
|  | 348 | WriteStringToFd("Service host process PID: ", fd.get()); | 
|  | 349 | } | 
| Steven Moreland | 5a30d34 | 2019-10-08 13:53:28 -0700 | [diff] [blame] | 350 | WriteStringToFd(std::to_string(pid) + "\n", fd.get()); | 
|  | 351 | return OK; | 
|  | 352 | } | 
|  | 353 |  | 
| Steven Moreland | e5a6a87 | 2021-05-19 22:11:37 +0000 | [diff] [blame] | 354 | static status_t dumpStabilityToFd(const sp<IBinder>& service, const unique_fd& fd) { | 
| Steven Moreland | cbd69fc | 2021-07-20 20:45:43 +0000 | [diff] [blame] | 355 | WriteStringToFd("Stability: " + internal::Stability::debugToString(service) + "\n", fd); | 
| Steven Moreland | e5a6a87 | 2021-05-19 22:11:37 +0000 | [diff] [blame] | 356 | return OK; | 
|  | 357 | } | 
|  | 358 |  | 
| Devin Moore | cfeeda4 | 2020-12-08 12:50:58 -0800 | [diff] [blame] | 359 | static 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 Moreland | cbd69fc | 2021-07-20 20:45:43 +0000 | [diff] [blame] | 376 | static 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 |  | 
|  | 383 | status_t Dumpsys::startDumpThread(int dumpTypeFlags, const String16& serviceName, | 
| Steven Moreland | 5a30d34 | 2019-10-08 13:53:28 -0700 | [diff] [blame] | 384 | const Vector<String16>& args) { | 
| Vishnu Nair | e4f6174 | 2017-12-21 08:30:28 -0800 | [diff] [blame] | 385 | sp<IBinder> service = sm_->checkService(serviceName); | 
|  | 386 | if (service == nullptr) { | 
| Steven Moreland | 3657ee5 | 2020-01-31 12:09:15 -0800 | [diff] [blame] | 387 | std::cerr << "Can't find service: " << serviceName << std::endl; | 
| Vishnu Nair | e4f6174 | 2017-12-21 08:30:28 -0800 | [diff] [blame] | 388 | return NAME_NOT_FOUND; | 
|  | 389 | } | 
|  | 390 |  | 
|  | 391 | int sfd[2]; | 
|  | 392 | if (pipe(sfd) != 0) { | 
| Steven Moreland | 3657ee5 | 2020-01-31 12:09:15 -0800 | [diff] [blame] | 393 | std::cerr << "Failed to create pipe to dump service info for " << serviceName << ": " | 
|  | 394 | << strerror(errno) << std::endl; | 
| Vishnu Nair | e4f6174 | 2017-12-21 08:30:28 -0800 | [diff] [blame] | 395 | 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 Moreland | cbd69fc | 2021-07-20 20:45:43 +0000 | [diff] [blame] | 404 | 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 Moreland | 5a30d34 | 2019-10-08 13:53:28 -0700 | [diff] [blame] | 415 | } | 
|  | 416 |  | 
| Steven Moreland | cbd69fc | 2021-07-20 20:45:43 +0000 | [diff] [blame] | 417 | // 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 Nair | e4f6174 | 2017-12-21 08:30:28 -0800 | [diff] [blame] | 421 | } | 
|  | 422 | }); | 
|  | 423 | return OK; | 
|  | 424 | } | 
|  | 425 |  | 
|  | 426 | void 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 |  | 
|  | 436 | void 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 Nair | 64afc02 | 2018-02-01 15:29:34 -0800 | [diff] [blame] | 441 | priorityFlags == IServiceManager::DUMP_FLAG_PRIORITY_NORMAL || | 
|  | 442 | priorityFlags == IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT) { | 
| Vishnu Nair | e4f6174 | 2017-12-21 08:30:28 -0800 | [diff] [blame] | 443 | 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 |  | 
|  | 452 | status_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 Hsieh | 9f2d531 | 2018-12-11 15:34:17 -0800 | [diff] [blame] | 472 | return std::max(diff.count(), 0LL); | 
| Vishnu Nair | e4f6174 | 2017-12-21 08:30:28 -0800 | [diff] [blame] | 473 | }; | 
|  | 474 |  | 
|  | 475 | int rc = TEMP_FAILURE_RETRY(poll(&pfd, 1, time_left_ms())); | 
|  | 476 | if (rc < 0) { | 
| Steven Moreland | 3657ee5 | 2020-01-31 12:09:15 -0800 | [diff] [blame] | 477 | std::cerr << "Error in poll while dumping service " << serviceName << " : " | 
|  | 478 | << strerror(errno) << std::endl; | 
| Vishnu Nair | e4f6174 | 2017-12-21 08:30:28 -0800 | [diff] [blame] | 479 | status = -errno; | 
|  | 480 | break; | 
| Rhed Jao | a19145f | 2020-12-22 20:42:34 +0800 | [diff] [blame] | 481 | } else if (rc == 0 || time_left_ms() == 0) { | 
| Vishnu Nair | e4f6174 | 2017-12-21 08:30:28 -0800 | [diff] [blame] | 482 | 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 Moreland | 3657ee5 | 2020-01-31 12:09:15 -0800 | [diff] [blame] | 489 | std::cerr << "Failed to read while dumping service " << serviceName << ": " | 
|  | 490 | << strerror(errno) << std::endl; | 
| Vishnu Nair | e4f6174 | 2017-12-21 08:30:28 -0800 | [diff] [blame] | 491 | status = -errno; | 
|  | 492 | break; | 
|  | 493 | } else if (rc == 0) { | 
|  | 494 | // EOF. | 
|  | 495 | break; | 
|  | 496 | } | 
|  | 497 |  | 
|  | 498 | if (!WriteFully(fd, buf, rc)) { | 
| Steven Moreland | 3657ee5 | 2020-01-31 12:09:15 -0800 | [diff] [blame] | 499 | std::cerr << "Failed to write while dumping service " << serviceName << ": " | 
|  | 500 | << strerror(errno) << std::endl; | 
| Vishnu Nair | e4f6174 | 2017-12-21 08:30:28 -0800 | [diff] [blame] | 501 | 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 |  | 
|  | 518 | void 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 | } |