| 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; | 
| mukesh agrawal | 2f1eb1c | 2016-06-08 18:16:36 -0700 | [diff] [blame] | 46 | using android::base::StringPrintf; | 
| Josh Gao | 4b8f6f9 | 2016-02-29 16:20:34 -0800 | [diff] [blame] | 47 | using android::base::unique_fd; | 
|  | 48 | using android::base::WriteFully; | 
| Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 49 |  | 
| Steven Moreland | 2c3cd83 | 2017-02-13 23:44:17 +0000 | [diff] [blame] | 50 | static int sort_func(const String16* lhs, const String16* rhs) | 
|  | 51 | { | 
| Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 52 | return lhs->compare(*rhs); | 
|  | 53 | } | 
|  | 54 |  | 
| Felipe Leme | bbfd2b8 | 2016-02-03 11:16:27 -0800 | [diff] [blame] | 55 | static void usage() { | 
|  | 56 | fprintf(stderr, | 
| Vishnu Nair | f56042d | 2017-09-19 15:25:10 -0700 | [diff] [blame] | 57 | "usage: dumpsys\n" | 
| Felipe Leme | bbfd2b8 | 2016-02-03 11:16:27 -0800 | [diff] [blame] | 58 | "         To dump all services.\n" | 
|  | 59 | "or:\n" | 
| Vishnu Nair | f56042d | 2017-09-19 15:25:10 -0700 | [diff] [blame] | 60 | "       dumpsys [-t TIMEOUT] [--priority LEVEL] [--help | -l | --skip SERVICES | " | 
|  | 61 | "SERVICE [ARGS]]\n" | 
| Felipe Leme | bbfd2b8 | 2016-02-03 11:16:27 -0800 | [diff] [blame] | 62 | "         --help: shows this help\n" | 
|  | 63 | "         -l: only list services, do not dump them\n" | 
| Thierry Strudel | 8b78b75 | 2016-03-22 10:25:44 -0700 | [diff] [blame] | 64 | "         -t TIMEOUT: TIMEOUT to use in seconds instead of default 10 seconds\n" | 
| Vishnu Nair | 6a40853 | 2017-10-24 09:11:27 -0700 | [diff] [blame] | 65 | "         --proto: filter services that support dumping data in proto format. Dumps" | 
|  | 66 | "               will be in proto format.\n" | 
| Vishnu Nair | f56042d | 2017-09-19 15:25:10 -0700 | [diff] [blame] | 67 | "         --priority LEVEL: filter services based on specified priority\n" | 
|  | 68 | "               LEVEL must be one of CRITICAL | HIGH | NORMAL\n" | 
| Felipe Leme | 859aef6 | 2016-02-03 12:17:10 -0800 | [diff] [blame] | 69 | "         --skip SERVICES: dumps all services but SERVICES (comma-separated list)\n" | 
|  | 70 | "         SERVICE [ARGS]: dumps only service SERVICE, optionally passing ARGS to it\n"); | 
|  | 71 | } | 
|  | 72 |  | 
| Felipe Leme | 343175a | 2016-08-02 18:57:37 -0700 | [diff] [blame] | 73 | static bool IsSkipped(const Vector<String16>& skipped, const String16& service) { | 
| Felipe Leme | 859aef6 | 2016-02-03 12:17:10 -0800 | [diff] [blame] | 74 | for (const auto& candidate : skipped) { | 
|  | 75 | if (candidate == service) { | 
|  | 76 | return true; | 
|  | 77 | } | 
|  | 78 | } | 
|  | 79 | return false; | 
| Felipe Leme | bbfd2b8 | 2016-02-03 11:16:27 -0800 | [diff] [blame] | 80 | } | 
|  | 81 |  | 
| Vishnu Nair | 6a40853 | 2017-10-24 09:11:27 -0700 | [diff] [blame] | 82 | static bool ConvertPriorityTypeToBitmask(const String16& type, int& bitmask) { | 
|  | 83 | if (type == PriorityDumper::PRIORITY_ARG_CRITICAL) { | 
|  | 84 | bitmask = IServiceManager::DUMP_FLAG_PRIORITY_CRITICAL; | 
| Vishnu Nair | 780b128 | 2017-10-10 13:57:24 -0700 | [diff] [blame] | 85 | return true; | 
|  | 86 | } | 
| Vishnu Nair | 6a40853 | 2017-10-24 09:11:27 -0700 | [diff] [blame] | 87 | if (type == PriorityDumper::PRIORITY_ARG_HIGH) { | 
|  | 88 | bitmask = IServiceManager::DUMP_FLAG_PRIORITY_HIGH; | 
| 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_NORMAL) { | 
|  | 92 | bitmask = IServiceManager::DUMP_FLAG_PRIORITY_NORMAL; | 
| Vishnu Nair | 780b128 | 2017-10-10 13:57:24 -0700 | [diff] [blame] | 93 | return true; | 
|  | 94 | } | 
|  | 95 | return false; | 
|  | 96 | } | 
|  | 97 |  | 
| Felipe Leme | 343175a | 2016-08-02 18:57:37 -0700 | [diff] [blame] | 98 | int Dumpsys::main(int argc, char* const argv[]) { | 
| Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 99 | Vector<String16> services; | 
|  | 100 | Vector<String16> args; | 
| Vishnu Nair | 780b128 | 2017-10-10 13:57:24 -0700 | [diff] [blame] | 101 | String16 priorityType; | 
| Felipe Leme | 859aef6 | 2016-02-03 12:17:10 -0800 | [diff] [blame] | 102 | Vector<String16> skippedServices; | 
| Vishnu Nair | 6a40853 | 2017-10-24 09:11:27 -0700 | [diff] [blame] | 103 | Vector<String16> protoServices; | 
| keunyoung | caad555 | 2013-06-13 15:08:51 -0700 | [diff] [blame] | 104 | bool showListOnly = false; | 
| Thierry Strudel | 8b78b75 | 2016-03-22 10:25:44 -0700 | [diff] [blame] | 105 | bool skipServices = false; | 
| Vishnu Nair | 6a40853 | 2017-10-24 09:11:27 -0700 | [diff] [blame] | 106 | bool filterByProto = false; | 
| Thierry Strudel | 8b78b75 | 2016-03-22 10:25:44 -0700 | [diff] [blame] | 107 | int timeoutArg = 10; | 
| Vishnu Nair | 6a40853 | 2017-10-24 09:11:27 -0700 | [diff] [blame] | 108 | int dumpPriorityFlags = IServiceManager::DUMP_FLAG_PRIORITY_ALL; | 
| Vishnu Nair | f56042d | 2017-09-19 15:25:10 -0700 | [diff] [blame] | 109 | static struct option longOptions[] = {{"priority", required_argument, 0, 0}, | 
| Vishnu Nair | 6a40853 | 2017-10-24 09:11:27 -0700 | [diff] [blame] | 110 | {"proto", no_argument, 0, 0}, | 
| Vishnu Nair | f56042d | 2017-09-19 15:25:10 -0700 | [diff] [blame] | 111 | {"skip", no_argument, 0, 0}, | 
|  | 112 | {"help", no_argument, 0, 0}, | 
|  | 113 | {0, 0, 0, 0}}; | 
| Thierry Strudel | 8b78b75 | 2016-03-22 10:25:44 -0700 | [diff] [blame] | 114 |  | 
| Felipe Leme | 343175a | 2016-08-02 18:57:37 -0700 | [diff] [blame] | 115 | // Must reset optind, otherwise subsequent calls will fail (wouldn't happen on main.cpp, but | 
|  | 116 | // happens on test cases). | 
|  | 117 | optind = 1; | 
| Thierry Strudel | 8b78b75 | 2016-03-22 10:25:44 -0700 | [diff] [blame] | 118 | while (1) { | 
|  | 119 | int c; | 
|  | 120 | int optionIndex = 0; | 
|  | 121 |  | 
|  | 122 | c = getopt_long(argc, argv, "+t:l", longOptions, &optionIndex); | 
|  | 123 |  | 
|  | 124 | if (c == -1) { | 
|  | 125 | break; | 
| Felipe Leme | bbfd2b8 | 2016-02-03 11:16:27 -0800 | [diff] [blame] | 126 | } | 
| Thierry Strudel | 8b78b75 | 2016-03-22 10:25:44 -0700 | [diff] [blame] | 127 |  | 
|  | 128 | switch (c) { | 
|  | 129 | case 0: | 
|  | 130 | if (!strcmp(longOptions[optionIndex].name, "skip")) { | 
|  | 131 | skipServices = true; | 
| Vishnu Nair | 6a40853 | 2017-10-24 09:11:27 -0700 | [diff] [blame] | 132 | } else if (!strcmp(longOptions[optionIndex].name, "proto")) { | 
|  | 133 | filterByProto = true; | 
| Thierry Strudel | 8b78b75 | 2016-03-22 10:25:44 -0700 | [diff] [blame] | 134 | } else if (!strcmp(longOptions[optionIndex].name, "help")) { | 
|  | 135 | usage(); | 
|  | 136 | return 0; | 
| Vishnu Nair | f56042d | 2017-09-19 15:25:10 -0700 | [diff] [blame] | 137 | } else if (!strcmp(longOptions[optionIndex].name, "priority")) { | 
| Vishnu Nair | 780b128 | 2017-10-10 13:57:24 -0700 | [diff] [blame] | 138 | priorityType = String16(String8(optarg)); | 
| Vishnu Nair | 6a40853 | 2017-10-24 09:11:27 -0700 | [diff] [blame] | 139 | if (!ConvertPriorityTypeToBitmask(priorityType, dumpPriorityFlags)) { | 
| Vishnu Nair | f56042d | 2017-09-19 15:25:10 -0700 | [diff] [blame] | 140 | fprintf(stderr, "\n"); | 
|  | 141 | usage(); | 
|  | 142 | return -1; | 
|  | 143 | } | 
| Thierry Strudel | 8b78b75 | 2016-03-22 10:25:44 -0700 | [diff] [blame] | 144 | } | 
|  | 145 | break; | 
|  | 146 |  | 
|  | 147 | case 't': | 
|  | 148 | { | 
|  | 149 | char *endptr; | 
|  | 150 | timeoutArg = strtol(optarg, &endptr, 10); | 
|  | 151 | if (*endptr != '\0' || timeoutArg <= 0) { | 
|  | 152 | fprintf(stderr, "Error: invalid timeout number: '%s'\n", optarg); | 
|  | 153 | return -1; | 
|  | 154 | } | 
|  | 155 | } | 
|  | 156 | break; | 
|  | 157 |  | 
|  | 158 | case 'l': | 
| Felipe Leme | bbfd2b8 | 2016-02-03 11:16:27 -0800 | [diff] [blame] | 159 | showListOnly = true; | 
| Thierry Strudel | 8b78b75 | 2016-03-22 10:25:44 -0700 | [diff] [blame] | 160 | break; | 
|  | 161 |  | 
|  | 162 | default: | 
|  | 163 | fprintf(stderr, "\n"); | 
|  | 164 | usage(); | 
|  | 165 | return -1; | 
| Felipe Leme | bbfd2b8 | 2016-02-03 11:16:27 -0800 | [diff] [blame] | 166 | } | 
| keunyoung | caad555 | 2013-06-13 15:08:51 -0700 | [diff] [blame] | 167 | } | 
| Thierry Strudel | 8b78b75 | 2016-03-22 10:25:44 -0700 | [diff] [blame] | 168 |  | 
|  | 169 | for (int i = optind; i < argc; i++) { | 
|  | 170 | if (skipServices) { | 
|  | 171 | skippedServices.add(String16(argv[i])); | 
|  | 172 | } else { | 
|  | 173 | if (i == optind) { | 
|  | 174 | services.add(String16(argv[i])); | 
|  | 175 | } else { | 
|  | 176 | args.add(String16(argv[i])); | 
| Felipe Leme | 859aef6 | 2016-02-03 12:17:10 -0800 | [diff] [blame] | 177 | } | 
|  | 178 | } | 
|  | 179 | } | 
| Thierry Strudel | 8b78b75 | 2016-03-22 10:25:44 -0700 | [diff] [blame] | 180 |  | 
|  | 181 | if ((skipServices && skippedServices.empty()) || | 
| Steven Moreland | 2c3cd83 | 2017-02-13 23:44:17 +0000 | [diff] [blame] | 182 | (showListOnly && (!services.empty() || !skippedServices.empty()))) { | 
| Thierry Strudel | 8b78b75 | 2016-03-22 10:25:44 -0700 | [diff] [blame] | 183 | usage(); | 
|  | 184 | return -1; | 
|  | 185 | } | 
|  | 186 |  | 
| Thierry Strudel | 159a832 | 2016-03-23 11:22:34 -0700 | [diff] [blame] | 187 | if (services.empty() || showListOnly) { | 
| Felipe Leme | 859aef6 | 2016-02-03 12:17:10 -0800 | [diff] [blame] | 188 | // gets all services | 
| Vishnu Nair | 6a40853 | 2017-10-24 09:11:27 -0700 | [diff] [blame] | 189 | services = sm_->listServices(dumpPriorityFlags); | 
| Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 190 | services.sort(sort_func); | 
| Vishnu Nair | 6a40853 | 2017-10-24 09:11:27 -0700 | [diff] [blame] | 191 | if (filterByProto) { | 
|  | 192 | protoServices = sm_->listServices(IServiceManager::DUMP_FLAG_PROTO); | 
|  | 193 | protoServices.sort(sort_func); | 
|  | 194 | Vector<String16> intersection; | 
|  | 195 | std::set_intersection(services.begin(), services.end(), protoServices.begin(), | 
|  | 196 | protoServices.end(), std::back_inserter(intersection)); | 
|  | 197 | services = std::move(intersection); | 
|  | 198 | args.insertAt(String16(PriorityDumper::PROTO_ARG), 0); | 
|  | 199 | } | 
|  | 200 | if (dumpPriorityFlags != IServiceManager::DUMP_FLAG_PRIORITY_ALL) { | 
|  | 201 | args.insertAt(String16(PriorityDumper::PRIORITY_ARG), 0); | 
| Vishnu Nair | 780b128 | 2017-10-10 13:57:24 -0700 | [diff] [blame] | 202 | args.insertAt(priorityType, 1); | 
|  | 203 | } else { | 
|  | 204 | args.add(String16("-a")); | 
|  | 205 | } | 
| Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 206 | } | 
|  | 207 |  | 
|  | 208 | const size_t N = services.size(); | 
|  | 209 |  | 
|  | 210 | if (N > 1) { | 
|  | 211 | // first print a list of the current services | 
|  | 212 | aout << "Currently running services:" << endl; | 
| Felipe Leme | bbfd2b8 | 2016-02-03 11:16:27 -0800 | [diff] [blame] | 213 |  | 
| Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 214 | for (size_t i=0; i<N; i++) { | 
| Felipe Leme | 343175a | 2016-08-02 18:57:37 -0700 | [diff] [blame] | 215 | sp<IBinder> service = sm_->checkService(services[i]); | 
|  | 216 |  | 
|  | 217 | if (service != nullptr) { | 
| Felipe Leme | 859aef6 | 2016-02-03 12:17:10 -0800 | [diff] [blame] | 218 | bool skipped = IsSkipped(skippedServices, services[i]); | 
|  | 219 | aout << "  " << services[i] << (skipped ? " (skipped)" : "") << endl; | 
| Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 220 | } | 
|  | 221 | } | 
|  | 222 | } | 
|  | 223 |  | 
| keunyoung | caad555 | 2013-06-13 15:08:51 -0700 | [diff] [blame] | 224 | if (showListOnly) { | 
|  | 225 | return 0; | 
|  | 226 | } | 
|  | 227 |  | 
| Josh Gao | 4b8f6f9 | 2016-02-29 16:20:34 -0800 | [diff] [blame] | 228 | for (size_t i = 0; i < N; i++) { | 
| Chih-Hung Hsieh | cb057c2 | 2017-08-03 15:48:25 -0700 | [diff] [blame] | 229 | const String16& service_name = std::move(services[i]); | 
| Josh Gao | 4b8f6f9 | 2016-02-29 16:20:34 -0800 | [diff] [blame] | 230 | if (IsSkipped(skippedServices, service_name)) continue; | 
| Felipe Leme | 859aef6 | 2016-02-03 12:17:10 -0800 | [diff] [blame] | 231 |  | 
| Felipe Leme | 343175a | 2016-08-02 18:57:37 -0700 | [diff] [blame] | 232 | sp<IBinder> service = sm_->checkService(service_name); | 
|  | 233 | if (service != nullptr) { | 
| Josh Gao | 4b8f6f9 | 2016-02-29 16:20:34 -0800 | [diff] [blame] | 234 | int sfd[2]; | 
|  | 235 |  | 
| Josh Gao | 49f0a0c | 2016-03-04 13:12:29 -0800 | [diff] [blame] | 236 | if (pipe(sfd) != 0) { | 
|  | 237 | aerr << "Failed to create pipe to dump service info for " << service_name | 
| Josh Gao | 4b8f6f9 | 2016-02-29 16:20:34 -0800 | [diff] [blame] | 238 | << ": " << strerror(errno) << endl; | 
|  | 239 | continue; | 
|  | 240 | } | 
|  | 241 |  | 
|  | 242 | unique_fd local_end(sfd[0]); | 
|  | 243 | unique_fd remote_end(sfd[1]); | 
|  | 244 | sfd[0] = sfd[1] = -1; | 
|  | 245 |  | 
| Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 246 | if (N > 1) { | 
|  | 247 | aout << "------------------------------------------------------------" | 
|  | 248 | "-------------------" << endl; | 
| Vishnu Nair | 6a40853 | 2017-10-24 09:11:27 -0700 | [diff] [blame] | 249 | if (dumpPriorityFlags == IServiceManager::DUMP_FLAG_PRIORITY_ALL) { | 
| Vishnu Nair | 780b128 | 2017-10-10 13:57:24 -0700 | [diff] [blame] | 250 | aout << "DUMP OF SERVICE " << service_name << ":" << endl; | 
|  | 251 | } else { | 
|  | 252 | aout << "DUMP OF SERVICE " << priorityType << " " << service_name << ":" << endl; | 
|  | 253 | } | 
| Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 254 | } | 
| Josh Gao | 4b8f6f9 | 2016-02-29 16:20:34 -0800 | [diff] [blame] | 255 |  | 
|  | 256 | // dump blocks until completion, so spawn a thread.. | 
|  | 257 | std::thread dump_thread([=, remote_end { std::move(remote_end) }]() mutable { | 
|  | 258 | int err = service->dump(remote_end.get(), args); | 
|  | 259 |  | 
|  | 260 | // It'd be nice to be able to close the remote end of the socketpair before the dump | 
|  | 261 | // call returns, to terminate our reads if the other end closes their copy of the | 
|  | 262 | // file descriptor, but then hangs for some reason. There doesn't seem to be a good | 
|  | 263 | // way to do this, though. | 
| Josh Gao | 9656be1 | 2016-09-19 12:44:50 -0700 | [diff] [blame] | 264 | remote_end.reset(); | 
| Josh Gao | 4b8f6f9 | 2016-02-29 16:20:34 -0800 | [diff] [blame] | 265 |  | 
|  | 266 | if (err != 0) { | 
|  | 267 | aerr << "Error dumping service info: (" << strerror(err) << ") " << service_name | 
|  | 268 | << endl; | 
|  | 269 | } | 
|  | 270 | }); | 
|  | 271 |  | 
| Thierry Strudel | 8b78b75 | 2016-03-22 10:25:44 -0700 | [diff] [blame] | 272 | auto timeout = std::chrono::seconds(timeoutArg); | 
| mukesh agrawal | 2f1eb1c | 2016-06-08 18:16:36 -0700 | [diff] [blame] | 273 | auto start = std::chrono::steady_clock::now(); | 
|  | 274 | auto end = start + timeout; | 
| Josh Gao | 4b8f6f9 | 2016-02-29 16:20:34 -0800 | [diff] [blame] | 275 |  | 
|  | 276 | struct pollfd pfd = { | 
|  | 277 | .fd = local_end.get(), | 
|  | 278 | .events = POLLIN | 
|  | 279 | }; | 
|  | 280 |  | 
|  | 281 | bool timed_out = false; | 
|  | 282 | bool error = false; | 
|  | 283 | while (true) { | 
|  | 284 | // Wrap this in a lambda so that TEMP_FAILURE_RETRY recalculates the timeout. | 
|  | 285 | auto time_left_ms = [end]() { | 
|  | 286 | auto now = std::chrono::steady_clock::now(); | 
|  | 287 | auto diff = std::chrono::duration_cast<std::chrono::milliseconds>(end - now); | 
|  | 288 | return std::max(diff.count(), 0ll); | 
|  | 289 | }; | 
|  | 290 |  | 
|  | 291 | int rc = TEMP_FAILURE_RETRY(poll(&pfd, 1, time_left_ms())); | 
|  | 292 | if (rc < 0) { | 
|  | 293 | aerr << "Error in poll while dumping service " << service_name << " : " | 
|  | 294 | << strerror(errno) << endl; | 
|  | 295 | error = true; | 
|  | 296 | break; | 
|  | 297 | } else if (rc == 0) { | 
|  | 298 | timed_out = true; | 
|  | 299 | break; | 
|  | 300 | } | 
|  | 301 |  | 
|  | 302 | char buf[4096]; | 
|  | 303 | rc = TEMP_FAILURE_RETRY(read(local_end.get(), buf, sizeof(buf))); | 
|  | 304 | if (rc < 0) { | 
|  | 305 | aerr << "Failed to read while dumping service " << service_name << ": " | 
|  | 306 | << strerror(errno) << endl; | 
|  | 307 | error = true; | 
|  | 308 | break; | 
|  | 309 | } else if (rc == 0) { | 
|  | 310 | // EOF. | 
|  | 311 | break; | 
|  | 312 | } | 
|  | 313 |  | 
|  | 314 | if (!WriteFully(STDOUT_FILENO, buf, rc)) { | 
|  | 315 | aerr << "Failed to write while dumping service " << service_name << ": " | 
|  | 316 | << strerror(errno) << endl; | 
|  | 317 | error = true; | 
|  | 318 | break; | 
|  | 319 | } | 
|  | 320 | } | 
|  | 321 |  | 
|  | 322 | if (timed_out) { | 
| Felipe Leme | 343175a | 2016-08-02 18:57:37 -0700 | [diff] [blame] | 323 | aout << endl | 
|  | 324 | << "*** SERVICE '" << service_name << "' DUMP TIMEOUT (" << timeoutArg | 
|  | 325 | << "s) EXPIRED ***" << endl | 
|  | 326 | << endl; | 
| Josh Gao | 4b8f6f9 | 2016-02-29 16:20:34 -0800 | [diff] [blame] | 327 | } | 
|  | 328 |  | 
|  | 329 | if (timed_out || error) { | 
|  | 330 | dump_thread.detach(); | 
|  | 331 | } else { | 
|  | 332 | dump_thread.join(); | 
| Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 333 | } | 
| mukesh agrawal | 2f1eb1c | 2016-06-08 18:16:36 -0700 | [diff] [blame] | 334 |  | 
|  | 335 | if (N > 1) { | 
|  | 336 | std::chrono::duration<double> elapsed_seconds = | 
|  | 337 | std::chrono::steady_clock::now() - start; | 
| mukesh agrawal | bca287d | 2016-07-27 12:01:49 -0700 | [diff] [blame] | 338 | aout << StringPrintf("--------- %.3fs ", elapsed_seconds.count()).c_str() | 
| Kevin Rocard | 430e079 | 2017-08-14 20:40:24 -0700 | [diff] [blame] | 339 | << "was the duration of dumpsys " << service_name; | 
|  | 340 |  | 
|  | 341 | using std::chrono::system_clock; | 
|  | 342 | const auto finish = system_clock::to_time_t(system_clock::now()); | 
|  | 343 | std::tm finish_tm; | 
|  | 344 | localtime_r(&finish, &finish_tm); | 
|  | 345 | aout << ", ending at: " << std::put_time(&finish_tm, "%Y-%m-%d %H:%M:%S") | 
|  | 346 | << endl; | 
| mukesh agrawal | 2f1eb1c | 2016-06-08 18:16:36 -0700 | [diff] [blame] | 347 | } | 
| Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 348 | } else { | 
| Josh Gao | 4b8f6f9 | 2016-02-29 16:20:34 -0800 | [diff] [blame] | 349 | aerr << "Can't find service: " << service_name << endl; | 
| Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 350 | } | 
|  | 351 | } | 
|  | 352 |  | 
|  | 353 | return 0; | 
|  | 354 | } |