Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 The Android Open Source Project |
| 3 | * |
| 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. |
| 15 | */ |
| 16 | |
| 17 | #include "ListCommand.h" |
| 18 | |
| 19 | #include <getopt.h> |
| 20 | |
Nirav Atre | cce988d | 2018-05-16 11:14:46 -0700 | [diff] [blame] | 21 | #include <algorithm> |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 22 | #include <fstream> |
Yifan Hong | b2d096a | 2018-05-01 15:25:23 -0700 | [diff] [blame] | 23 | #include <functional> |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 24 | #include <iomanip> |
| 25 | #include <iostream> |
| 26 | #include <map> |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 27 | #include <regex> |
Yifan Hong | b2d096a | 2018-05-01 15:25:23 -0700 | [diff] [blame] | 28 | #include <sstream> |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 29 | |
Yifan Hong | f31aa05 | 2018-02-02 15:17:51 -0800 | [diff] [blame] | 30 | #include <android-base/file.h> |
Nirav Atre | cce988d | 2018-05-16 11:14:46 -0700 | [diff] [blame] | 31 | #include <android-base/logging.h> |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 32 | #include <android-base/parseint.h> |
| 33 | #include <android/hidl/manager/1.0/IServiceManager.h> |
Yifan Hong | fee209d | 2017-09-14 18:23:38 -0700 | [diff] [blame] | 34 | #include <hidl-hash/Hash.h> |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 35 | #include <hidl-util/FQName.h> |
| 36 | #include <private/android_filesystem_config.h> |
| 37 | #include <sys/stat.h> |
| 38 | #include <vintf/HalManifest.h> |
Yifan Hong | f31aa05 | 2018-02-02 15:17:51 -0800 | [diff] [blame] | 39 | #include <vintf/parse_string.h> |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 40 | #include <vintf/parse_xml.h> |
| 41 | |
| 42 | #include "Lshal.h" |
| 43 | #include "PipeRelay.h" |
| 44 | #include "Timeout.h" |
| 45 | #include "utils.h" |
| 46 | |
| 47 | using ::android::hardware::hidl_string; |
Yifan Hong | 22ea7b8 | 2017-09-14 18:07:43 -0700 | [diff] [blame] | 48 | using ::android::hardware::hidl_vec; |
| 49 | using ::android::hidl::base::V1_0::DebugInfo; |
| 50 | using ::android::hidl::base::V1_0::IBase; |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 51 | using ::android::hidl::manager::V1_0::IServiceManager; |
| 52 | |
| 53 | namespace android { |
| 54 | namespace lshal { |
| 55 | |
Yifan Hong | f31aa05 | 2018-02-02 15:17:51 -0800 | [diff] [blame] | 56 | vintf::SchemaType toSchemaType(Partition p) { |
| 57 | return (p == Partition::SYSTEM) ? vintf::SchemaType::FRAMEWORK : vintf::SchemaType::DEVICE; |
| 58 | } |
| 59 | |
Yifan Hong | 13ba0a9 | 2018-06-25 16:15:56 -0700 | [diff] [blame] | 60 | Partition toPartition(vintf::SchemaType t) { |
| 61 | switch (t) { |
| 62 | case vintf::SchemaType::FRAMEWORK: return Partition::SYSTEM; |
| 63 | // TODO(b/71555570): Device manifest does not distinguish HALs from vendor or ODM. |
| 64 | case vintf::SchemaType::DEVICE: return Partition::VENDOR; |
| 65 | } |
| 66 | return Partition::UNKNOWN; |
| 67 | } |
| 68 | |
| 69 | std::string getPackageAndVersion(const std::string& fqInstance) { |
| 70 | return splitFirst(fqInstance, ':').first; |
| 71 | } |
| 72 | |
Yifan Hong | 76ac14a | 2017-09-08 14:59:04 -0700 | [diff] [blame] | 73 | NullableOStream<std::ostream> ListCommand::out() const { |
| 74 | return mLshal.out(); |
| 75 | } |
| 76 | |
| 77 | NullableOStream<std::ostream> ListCommand::err() const { |
| 78 | return mLshal.err(); |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 79 | } |
| 80 | |
Yifan Hong | 795b6ec | 2017-09-13 11:25:28 -0700 | [diff] [blame] | 81 | std::string ListCommand::GetName() { |
| 82 | return "list"; |
| 83 | } |
| 84 | std::string ListCommand::getSimpleDescription() const { |
| 85 | return "List HALs."; |
| 86 | } |
| 87 | |
Yifan Hong | 8bf7316 | 2017-09-07 18:06:13 -0700 | [diff] [blame] | 88 | std::string ListCommand::parseCmdline(pid_t pid) const { |
Yifan Hong | f31aa05 | 2018-02-02 15:17:51 -0800 | [diff] [blame] | 89 | return android::procpartition::getCmdline(pid); |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | const std::string &ListCommand::getCmdline(pid_t pid) { |
Yifan Hong | 13ba0a9 | 2018-06-25 16:15:56 -0700 | [diff] [blame] | 93 | static const std::string kEmptyString{}; |
| 94 | if (pid == NO_PID) return kEmptyString; |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 95 | auto pair = mCmdlines.find(pid); |
| 96 | if (pair != mCmdlines.end()) { |
| 97 | return pair->second; |
| 98 | } |
Yifan Hong | 8bf7316 | 2017-09-07 18:06:13 -0700 | [diff] [blame] | 99 | mCmdlines[pid] = parseCmdline(pid); |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 100 | return mCmdlines[pid]; |
| 101 | } |
| 102 | |
| 103 | void ListCommand::removeDeadProcesses(Pids *pids) { |
| 104 | static const pid_t myPid = getpid(); |
Yifan Hong | 61fb7bc | 2017-05-12 16:33:57 -0700 | [diff] [blame] | 105 | pids->erase(std::remove_if(pids->begin(), pids->end(), [this](auto pid) { |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 106 | return pid == myPid || this->getCmdline(pid).empty(); |
Yifan Hong | 61fb7bc | 2017-05-12 16:33:57 -0700 | [diff] [blame] | 107 | }), pids->end()); |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Yifan Hong | f31aa05 | 2018-02-02 15:17:51 -0800 | [diff] [blame] | 110 | Partition ListCommand::getPartition(pid_t pid) { |
Yifan Hong | 13ba0a9 | 2018-06-25 16:15:56 -0700 | [diff] [blame] | 111 | if (pid == NO_PID) return Partition::UNKNOWN; |
Yifan Hong | f31aa05 | 2018-02-02 15:17:51 -0800 | [diff] [blame] | 112 | auto it = mPartitions.find(pid); |
| 113 | if (it != mPartitions.end()) { |
| 114 | return it->second; |
| 115 | } |
| 116 | Partition partition = android::procpartition::getPartition(pid); |
| 117 | mPartitions.emplace(pid, partition); |
| 118 | return partition; |
| 119 | } |
| 120 | |
| 121 | // Give sensible defaults when nothing can be inferred from runtime. |
| 122 | // process: Partition inferred from executable location or cmdline. |
Yifan Hong | b2d096a | 2018-05-01 15:25:23 -0700 | [diff] [blame] | 123 | Partition ListCommand::resolvePartition(Partition process, const FqInstance& fqInstance) const { |
| 124 | if (fqInstance.inPackage("vendor") || fqInstance.inPackage("com")) { |
Yifan Hong | f31aa05 | 2018-02-02 15:17:51 -0800 | [diff] [blame] | 125 | return Partition::VENDOR; |
| 126 | } |
| 127 | |
Yifan Hong | b2d096a | 2018-05-01 15:25:23 -0700 | [diff] [blame] | 128 | if (fqInstance.inPackage("android.frameworks") || fqInstance.inPackage("android.system") || |
| 129 | fqInstance.inPackage("android.hidl")) { |
Yifan Hong | f31aa05 | 2018-02-02 15:17:51 -0800 | [diff] [blame] | 130 | return Partition::SYSTEM; |
| 131 | } |
| 132 | |
| 133 | // Some android.hardware HALs are served from system. Check the value from executable |
| 134 | // location / cmdline first. |
Yifan Hong | b2d096a | 2018-05-01 15:25:23 -0700 | [diff] [blame] | 135 | if (fqInstance.inPackage("android.hardware")) { |
Yifan Hong | f31aa05 | 2018-02-02 15:17:51 -0800 | [diff] [blame] | 136 | if (process != Partition::UNKNOWN) { |
| 137 | return process; |
| 138 | } |
| 139 | return Partition::VENDOR; |
| 140 | } |
| 141 | |
| 142 | return process; |
| 143 | } |
| 144 | |
Yifan Hong | bdf44f8 | 2018-05-25 14:20:00 -0700 | [diff] [blame] | 145 | bool match(const vintf::ManifestInstance& instance, const FqInstance& fqInstance, |
| 146 | vintf::TransportArch ta) { |
| 147 | // For hwbinder libs, allow missing arch in manifest. |
| 148 | // For passthrough libs, allow missing interface/instance in table. |
| 149 | return (ta.transport == instance.transport()) && |
| 150 | (ta.transport == vintf::Transport::HWBINDER || |
| 151 | vintf::contains(instance.arch(), ta.arch)) && |
| 152 | (!fqInstance.hasInterface() || fqInstance.getInterface() == instance.interface()) && |
| 153 | (!fqInstance.hasInstance() || fqInstance.getInstance() == instance.instance()); |
| 154 | } |
| 155 | |
| 156 | bool match(const vintf::MatrixInstance& instance, const FqInstance& fqInstance, |
| 157 | vintf::TransportArch /* ta */) { |
| 158 | return (!fqInstance.hasInterface() || fqInstance.getInterface() == instance.interface()) && |
| 159 | (!fqInstance.hasInstance() || instance.matchInstance(fqInstance.getInstance())); |
| 160 | } |
| 161 | |
| 162 | template <typename ObjectType> |
| 163 | VintfInfo getVintfInfo(const std::shared_ptr<const ObjectType>& object, |
| 164 | const FqInstance& fqInstance, vintf::TransportArch ta, VintfInfo value) { |
| 165 | bool found = false; |
Yifan Hong | c346a16 | 2019-09-10 19:35:55 -0700 | [diff] [blame^] | 166 | (void)object->forEachHidlInstanceOfVersion(fqInstance.getPackage(), fqInstance.getVersion(), |
| 167 | [&](const auto& instance) { |
| 168 | found = match(instance, fqInstance, ta); |
| 169 | return !found; // continue if not found |
| 170 | }); |
Yifan Hong | bdf44f8 | 2018-05-25 14:20:00 -0700 | [diff] [blame] | 171 | return found ? value : VINTF_INFO_EMPTY; |
| 172 | } |
| 173 | |
| 174 | std::shared_ptr<const vintf::HalManifest> ListCommand::getDeviceManifest() const { |
| 175 | return vintf::VintfObject::GetDeviceHalManifest(); |
| 176 | } |
| 177 | |
| 178 | std::shared_ptr<const vintf::CompatibilityMatrix> ListCommand::getDeviceMatrix() const { |
| 179 | return vintf::VintfObject::GetDeviceCompatibilityMatrix(); |
| 180 | } |
| 181 | |
| 182 | std::shared_ptr<const vintf::HalManifest> ListCommand::getFrameworkManifest() const { |
| 183 | return vintf::VintfObject::GetFrameworkHalManifest(); |
| 184 | } |
| 185 | |
| 186 | std::shared_ptr<const vintf::CompatibilityMatrix> ListCommand::getFrameworkMatrix() const { |
| 187 | return vintf::VintfObject::GetFrameworkCompatibilityMatrix(); |
| 188 | } |
| 189 | |
| 190 | VintfInfo ListCommand::getVintfInfo(const std::string& fqInstanceName, |
| 191 | vintf::TransportArch ta) const { |
| 192 | FqInstance fqInstance; |
| 193 | if (!fqInstance.setTo(fqInstanceName) && |
| 194 | // Ignore interface / instance for passthrough libs |
Yifan Hong | 13ba0a9 | 2018-06-25 16:15:56 -0700 | [diff] [blame] | 195 | !fqInstance.setTo(getPackageAndVersion(fqInstanceName))) { |
Yifan Hong | bdf44f8 | 2018-05-25 14:20:00 -0700 | [diff] [blame] | 196 | err() << "Warning: Cannot parse '" << fqInstanceName << "'; no VINTF info." << std::endl; |
| 197 | return VINTF_INFO_EMPTY; |
| 198 | } |
| 199 | |
| 200 | return lshal::getVintfInfo(getDeviceManifest(), fqInstance, ta, DEVICE_MANIFEST) | |
| 201 | lshal::getVintfInfo(getFrameworkManifest(), fqInstance, ta, FRAMEWORK_MANIFEST) | |
| 202 | lshal::getVintfInfo(getDeviceMatrix(), fqInstance, ta, DEVICE_MATRIX) | |
| 203 | lshal::getVintfInfo(getFrameworkMatrix(), fqInstance, ta, FRAMEWORK_MATRIX); |
| 204 | } |
| 205 | |
Yifan Hong | 1243dde | 2017-09-14 17:49:30 -0700 | [diff] [blame] | 206 | static bool scanBinderContext(pid_t pid, |
Steven Moreland | d8e2019 | 2017-05-24 11:23:08 -0700 | [diff] [blame] | 207 | const std::string &contextName, |
| 208 | std::function<void(const std::string&)> eachLine) { |
| 209 | std::ifstream ifs("/d/binder/proc/" + std::to_string(pid)); |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 210 | if (!ifs.is_open()) { |
| 211 | return false; |
| 212 | } |
| 213 | |
Steven Moreland | d8e2019 | 2017-05-24 11:23:08 -0700 | [diff] [blame] | 214 | static const std::regex kContextLine("^context (\\w+)$"); |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 215 | |
Steven Moreland | d8e2019 | 2017-05-24 11:23:08 -0700 | [diff] [blame] | 216 | bool isDesiredContext = false; |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 217 | std::string line; |
| 218 | std::smatch match; |
| 219 | while(getline(ifs, line)) { |
Steven Moreland | d8e2019 | 2017-05-24 11:23:08 -0700 | [diff] [blame] | 220 | if (std::regex_search(line, match, kContextLine)) { |
| 221 | isDesiredContext = match.str(1) == contextName; |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 222 | continue; |
| 223 | } |
Steven Moreland | d8e2019 | 2017-05-24 11:23:08 -0700 | [diff] [blame] | 224 | |
| 225 | if (!isDesiredContext) { |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 226 | continue; |
| 227 | } |
Steven Moreland | d8e2019 | 2017-05-24 11:23:08 -0700 | [diff] [blame] | 228 | |
| 229 | eachLine(line); |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 230 | } |
| 231 | return true; |
| 232 | } |
| 233 | |
Steven Moreland | d8e2019 | 2017-05-24 11:23:08 -0700 | [diff] [blame] | 234 | bool ListCommand::getPidInfo( |
| 235 | pid_t serverPid, PidInfo *pidInfo) const { |
| 236 | static const std::regex kReferencePrefix("^\\s*node \\d+:\\s+u([0-9a-f]+)\\s+c([0-9a-f]+)\\s+"); |
| 237 | static const std::regex kThreadPrefix("^\\s*thread \\d+:\\s+l\\s+(\\d)(\\d)"); |
| 238 | |
| 239 | std::smatch match; |
| 240 | return scanBinderContext(serverPid, "hwbinder", [&](const std::string& line) { |
| 241 | if (std::regex_search(line, match, kReferencePrefix)) { |
| 242 | const std::string &ptrString = "0x" + match.str(2); // use number after c |
| 243 | uint64_t ptr; |
| 244 | if (!::android::base::ParseUint(ptrString.c_str(), &ptr)) { |
| 245 | // Should not reach here, but just be tolerant. |
Yifan Hong | 76ac14a | 2017-09-08 14:59:04 -0700 | [diff] [blame] | 246 | err() << "Could not parse number " << ptrString << std::endl; |
Steven Moreland | d8e2019 | 2017-05-24 11:23:08 -0700 | [diff] [blame] | 247 | return; |
| 248 | } |
| 249 | const std::string proc = " proc "; |
| 250 | auto pos = line.rfind(proc); |
| 251 | if (pos != std::string::npos) { |
| 252 | for (const std::string &pidStr : split(line.substr(pos + proc.size()), ' ')) { |
| 253 | int32_t pid; |
| 254 | if (!::android::base::ParseInt(pidStr, &pid)) { |
Yifan Hong | 76ac14a | 2017-09-08 14:59:04 -0700 | [diff] [blame] | 255 | err() << "Could not parse number " << pidStr << std::endl; |
Steven Moreland | d8e2019 | 2017-05-24 11:23:08 -0700 | [diff] [blame] | 256 | return; |
| 257 | } |
| 258 | pidInfo->refPids[ptr].push_back(pid); |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | return; |
| 263 | } |
| 264 | |
| 265 | if (std::regex_search(line, match, kThreadPrefix)) { |
| 266 | // "1" is waiting in binder driver |
| 267 | // "2" is poll. It's impossible to tell if these are in use. |
| 268 | // and HIDL default code doesn't use it. |
| 269 | bool isInUse = match.str(1) != "1"; |
| 270 | // "0" is a thread that has called into binder |
| 271 | // "1" is looper thread |
| 272 | // "2" is main looper thread |
| 273 | bool isHwbinderThread = match.str(2) != "0"; |
| 274 | |
| 275 | if (!isHwbinderThread) { |
| 276 | return; |
| 277 | } |
| 278 | |
| 279 | if (isInUse) { |
| 280 | pidInfo->threadUsage++; |
| 281 | } |
| 282 | |
| 283 | pidInfo->threadCount++; |
| 284 | return; |
| 285 | } |
| 286 | |
| 287 | // not reference or thread line |
| 288 | return; |
| 289 | }); |
| 290 | } |
| 291 | |
Yifan Hong | 1243dde | 2017-09-14 17:49:30 -0700 | [diff] [blame] | 292 | const PidInfo* ListCommand::getPidInfoCached(pid_t serverPid) { |
| 293 | auto pair = mCachedPidInfos.insert({serverPid, PidInfo{}}); |
| 294 | if (pair.second /* did insertion take place? */) { |
| 295 | if (!getPidInfo(serverPid, &pair.first->second)) { |
| 296 | return nullptr; |
| 297 | } |
| 298 | } |
| 299 | return &pair.first->second; |
| 300 | } |
| 301 | |
Yifan Hong | 13ba0a9 | 2018-06-25 16:15:56 -0700 | [diff] [blame] | 302 | bool ListCommand::shouldFetchHalType(const HalType &type) const { |
| 303 | return (std::find(mFetchTypes.begin(), mFetchTypes.end(), type) != mFetchTypes.end()); |
Nirav Atre | cce988d | 2018-05-16 11:14:46 -0700 | [diff] [blame] | 304 | } |
| 305 | |
Yifan Hong | db73053 | 2018-06-25 16:32:01 -0700 | [diff] [blame] | 306 | Table* ListCommand::tableForType(HalType type) { |
| 307 | switch (type) { |
| 308 | case HalType::BINDERIZED_SERVICES: |
| 309 | return &mServicesTable; |
| 310 | case HalType::PASSTHROUGH_CLIENTS: |
| 311 | return &mPassthroughRefTable; |
| 312 | case HalType::PASSTHROUGH_LIBRARIES: |
| 313 | return &mImplementationsTable; |
Yifan Hong | 13ba0a9 | 2018-06-25 16:15:56 -0700 | [diff] [blame] | 314 | case HalType::VINTF_MANIFEST: |
| 315 | return &mManifestHalsTable; |
Yifan Hong | 3212f17 | 2018-06-28 12:39:50 -0700 | [diff] [blame] | 316 | case HalType::LAZY_HALS: |
| 317 | return &mLazyHalsTable; |
Yifan Hong | db73053 | 2018-06-25 16:32:01 -0700 | [diff] [blame] | 318 | default: |
| 319 | LOG(FATAL) << "Unknown HAL type " << static_cast<int64_t>(type); |
| 320 | return nullptr; |
| 321 | } |
| 322 | } |
| 323 | const Table* ListCommand::tableForType(HalType type) const { |
| 324 | return const_cast<ListCommand*>(this)->tableForType(type); |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | void ListCommand::forEachTable(const std::function<void(Table &)> &f) { |
Nirav Atre | cce988d | 2018-05-16 11:14:46 -0700 | [diff] [blame] | 328 | for (const auto& type : mListTypes) { |
Yifan Hong | db73053 | 2018-06-25 16:32:01 -0700 | [diff] [blame] | 329 | f(*tableForType(type)); |
Nirav Atre | cce988d | 2018-05-16 11:14:46 -0700 | [diff] [blame] | 330 | } |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 331 | } |
| 332 | void ListCommand::forEachTable(const std::function<void(const Table &)> &f) const { |
Nirav Atre | cce988d | 2018-05-16 11:14:46 -0700 | [diff] [blame] | 333 | for (const auto& type : mListTypes) { |
Yifan Hong | db73053 | 2018-06-25 16:32:01 -0700 | [diff] [blame] | 334 | f(*tableForType(type)); |
Nirav Atre | cce988d | 2018-05-16 11:14:46 -0700 | [diff] [blame] | 335 | } |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | void ListCommand::postprocess() { |
| 339 | forEachTable([this](Table &table) { |
| 340 | if (mSortColumn) { |
| 341 | std::sort(table.begin(), table.end(), mSortColumn); |
| 342 | } |
| 343 | for (TableEntry &entry : table) { |
| 344 | entry.serverCmdline = getCmdline(entry.serverPid); |
| 345 | removeDeadProcesses(&entry.clientPids); |
| 346 | for (auto pid : entry.clientPids) { |
| 347 | entry.clientCmdlines.push_back(this->getCmdline(pid)); |
| 348 | } |
| 349 | } |
Yifan Hong | f31aa05 | 2018-02-02 15:17:51 -0800 | [diff] [blame] | 350 | for (TableEntry& entry : table) { |
Yifan Hong | 13ba0a9 | 2018-06-25 16:15:56 -0700 | [diff] [blame] | 351 | if (entry.partition == Partition::UNKNOWN) { |
| 352 | entry.partition = getPartition(entry.serverPid); |
| 353 | } |
Yifan Hong | bdf44f8 | 2018-05-25 14:20:00 -0700 | [diff] [blame] | 354 | entry.vintfInfo = getVintfInfo(entry.interfaceName, {entry.transport, entry.arch}); |
Yifan Hong | f31aa05 | 2018-02-02 15:17:51 -0800 | [diff] [blame] | 355 | } |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 356 | }); |
| 357 | // use a double for loop here because lshal doesn't care about efficiency. |
| 358 | for (TableEntry &packageEntry : mImplementationsTable) { |
| 359 | std::string packageName = packageEntry.interfaceName; |
Steven Moreland | d4f32b3 | 2018-03-06 14:47:58 -0800 | [diff] [blame] | 360 | FQName fqPackageName; |
| 361 | if (!FQName::parse(packageName.substr(0, packageName.find("::")), &fqPackageName)) { |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 362 | continue; |
| 363 | } |
| 364 | for (TableEntry &interfaceEntry : mPassthroughRefTable) { |
Yifan Hong | 0ad64f5 | 2018-05-25 15:29:17 -0700 | [diff] [blame] | 365 | if (interfaceEntry.arch != vintf::Arch::ARCH_EMPTY) { |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 366 | continue; |
| 367 | } |
Steven Moreland | d4f32b3 | 2018-03-06 14:47:58 -0800 | [diff] [blame] | 368 | FQName interfaceName; |
| 369 | if (!FQName::parse(splitFirst(interfaceEntry.interfaceName, '/').first, &interfaceName)) { |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 370 | continue; |
| 371 | } |
| 372 | if (interfaceName.getPackageAndVersion() == fqPackageName) { |
| 373 | interfaceEntry.arch = packageEntry.arch; |
| 374 | } |
| 375 | } |
| 376 | } |
Yifan Hong | ca3b660 | 2017-09-07 16:44:27 -0700 | [diff] [blame] | 377 | |
| 378 | mServicesTable.setDescription( |
Steven Moreland | 8e0f539 | 2018-12-12 14:27:24 -0800 | [diff] [blame] | 379 | "| All binderized services (registered with hwservicemanager)"); |
Yifan Hong | ca3b660 | 2017-09-07 16:44:27 -0700 | [diff] [blame] | 380 | mPassthroughRefTable.setDescription( |
Steven Moreland | 8e0f539 | 2018-12-12 14:27:24 -0800 | [diff] [blame] | 381 | "| All interfaces that getService() has ever returned as a passthrough interface;\n" |
| 382 | "| PIDs / processes shown below might be inaccurate because the process\n" |
| 383 | "| might have relinquished the interface or might have died.\n" |
| 384 | "| The Server / Server CMD column can be ignored.\n" |
| 385 | "| The Clients / Clients CMD column shows all process that have ever dlopen'ed \n" |
| 386 | "| the library and successfully fetched the passthrough implementation."); |
Yifan Hong | ca3b660 | 2017-09-07 16:44:27 -0700 | [diff] [blame] | 387 | mImplementationsTable.setDescription( |
Steven Moreland | 8e0f539 | 2018-12-12 14:27:24 -0800 | [diff] [blame] | 388 | "| All available passthrough implementations (all -impl.so files).\n" |
| 389 | "| These may return subclasses through their respective HIDL_FETCH_I* functions."); |
Yifan Hong | 13ba0a9 | 2018-06-25 16:15:56 -0700 | [diff] [blame] | 390 | mManifestHalsTable.setDescription( |
Steven Moreland | 8e0f539 | 2018-12-12 14:27:24 -0800 | [diff] [blame] | 391 | "| All HALs that are in VINTF manifest."); |
Yifan Hong | 3212f17 | 2018-06-28 12:39:50 -0700 | [diff] [blame] | 392 | mLazyHalsTable.setDescription( |
Steven Moreland | 8e0f539 | 2018-12-12 14:27:24 -0800 | [diff] [blame] | 393 | "| All HALs that are declared in VINTF manifest:\n" |
| 394 | "| - as hwbinder HALs but are not registered to hwservicemanager, and\n" |
| 395 | "| - as hwbinder/passthrough HALs with no implementation."); |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 396 | } |
| 397 | |
Yifan Hong | b2d096a | 2018-05-01 15:25:23 -0700 | [diff] [blame] | 398 | bool ListCommand::addEntryWithInstance(const TableEntry& entry, |
| 399 | vintf::HalManifest* manifest) const { |
| 400 | FqInstance fqInstance; |
| 401 | if (!fqInstance.setTo(entry.interfaceName)) { |
| 402 | err() << "Warning: '" << entry.interfaceName << "' is not a valid FqInstance." << std::endl; |
| 403 | return false; |
Yifan Hong | 77c8782 | 2017-06-19 15:47:39 -0700 | [diff] [blame] | 404 | } |
Yifan Hong | b2d096a | 2018-05-01 15:25:23 -0700 | [diff] [blame] | 405 | |
| 406 | if (fqInstance.getPackage() == gIBaseFqName.package()) { |
| 407 | return true; // always remove IBase from manifest |
| 408 | } |
| 409 | |
| 410 | Partition partition = resolvePartition(entry.partition, fqInstance); |
| 411 | |
| 412 | if (partition == Partition::UNKNOWN) { |
| 413 | err() << "Warning: Cannot guess the partition of FqInstance " << fqInstance.string() |
| 414 | << std::endl; |
| 415 | return false; |
| 416 | } |
| 417 | |
| 418 | if (partition != mVintfPartition) { |
| 419 | return true; // strip out instances that is in a different partition. |
| 420 | } |
| 421 | |
Yifan Hong | b2d096a | 2018-05-01 15:25:23 -0700 | [diff] [blame] | 422 | vintf::Arch arch; |
Yifan Hong | 8304e41 | 2018-05-25 15:05:36 -0700 | [diff] [blame] | 423 | if (entry.transport == vintf::Transport::HWBINDER) { |
Yifan Hong | 0ad64f5 | 2018-05-25 15:29:17 -0700 | [diff] [blame] | 424 | arch = vintf::Arch::ARCH_EMPTY; // no need to specify arch in manifest |
Yifan Hong | 8304e41 | 2018-05-25 15:05:36 -0700 | [diff] [blame] | 425 | } else if (entry.transport == vintf::Transport::PASSTHROUGH) { |
Yifan Hong | 0ad64f5 | 2018-05-25 15:29:17 -0700 | [diff] [blame] | 426 | if (entry.arch == vintf::Arch::ARCH_EMPTY) { |
| 427 | err() << "Warning: '" << entry.interfaceName << "' doesn't have bitness info."; |
| 428 | return false; |
Yifan Hong | b2d096a | 2018-05-01 15:25:23 -0700 | [diff] [blame] | 429 | } |
Yifan Hong | 0ad64f5 | 2018-05-25 15:29:17 -0700 | [diff] [blame] | 430 | arch = entry.arch; |
Yifan Hong | b2d096a | 2018-05-01 15:25:23 -0700 | [diff] [blame] | 431 | } else { |
| 432 | err() << "Warning: '" << entry.transport << "' is not a valid transport." << std::endl; |
| 433 | return false; |
| 434 | } |
| 435 | |
| 436 | std::string e; |
Yifan Hong | 8304e41 | 2018-05-25 15:05:36 -0700 | [diff] [blame] | 437 | if (!manifest->insertInstance(fqInstance, entry.transport, arch, vintf::HalFormat::HIDL, &e)) { |
Yifan Hong | b2d096a | 2018-05-01 15:25:23 -0700 | [diff] [blame] | 438 | err() << "Warning: Cannot insert '" << fqInstance.string() << ": " << e << std::endl; |
| 439 | return false; |
| 440 | } |
| 441 | return true; |
| 442 | } |
| 443 | |
| 444 | bool ListCommand::addEntryWithoutInstance(const TableEntry& entry, |
| 445 | const vintf::HalManifest* manifest) const { |
Yifan Hong | 13ba0a9 | 2018-06-25 16:15:56 -0700 | [diff] [blame] | 446 | const auto& packageAndVersion = splitFirst(getPackageAndVersion(entry.interfaceName), '@'); |
Yifan Hong | b2d096a | 2018-05-01 15:25:23 -0700 | [diff] [blame] | 447 | const auto& package = packageAndVersion.first; |
| 448 | vintf::Version version; |
| 449 | if (!vintf::parse(packageAndVersion.second, &version)) { |
| 450 | err() << "Warning: Cannot parse version '" << packageAndVersion.second << "' for entry '" |
| 451 | << entry.interfaceName << "'" << std::endl; |
| 452 | return false; |
| 453 | } |
| 454 | |
| 455 | bool found = false; |
Yifan Hong | c346a16 | 2019-09-10 19:35:55 -0700 | [diff] [blame^] | 456 | (void)manifest->forEachHidlInstanceOfVersion(package, version, [&found](const auto&) { |
Yifan Hong | b2d096a | 2018-05-01 15:25:23 -0700 | [diff] [blame] | 457 | found = true; |
| 458 | return false; // break |
| 459 | }); |
| 460 | return found; |
Yifan Hong | 77c8782 | 2017-06-19 15:47:39 -0700 | [diff] [blame] | 461 | } |
| 462 | |
Yifan Hong | ca3b660 | 2017-09-07 16:44:27 -0700 | [diff] [blame] | 463 | void ListCommand::dumpVintf(const NullableOStream<std::ostream>& out) const { |
Yifan Hong | 236301c | 2017-06-19 12:27:08 -0700 | [diff] [blame] | 464 | using vintf::operator|=; |
Yifan Hong | f31aa05 | 2018-02-02 15:17:51 -0800 | [diff] [blame] | 465 | using vintf::operator<<; |
Yifan Hong | b2d096a | 2018-05-01 15:25:23 -0700 | [diff] [blame] | 466 | using namespace std::placeholders; |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 467 | |
| 468 | vintf::HalManifest manifest; |
Yifan Hong | f31aa05 | 2018-02-02 15:17:51 -0800 | [diff] [blame] | 469 | manifest.setType(toSchemaType(mVintfPartition)); |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 470 | |
Yifan Hong | b2d096a | 2018-05-01 15:25:23 -0700 | [diff] [blame] | 471 | std::vector<std::string> error; |
| 472 | for (const TableEntry& entry : mServicesTable) |
| 473 | if (!addEntryWithInstance(entry, &manifest)) error.push_back(entry.interfaceName); |
| 474 | for (const TableEntry& entry : mPassthroughRefTable) |
| 475 | if (!addEntryWithInstance(entry, &manifest)) error.push_back(entry.interfaceName); |
Yifan Hong | 13ba0a9 | 2018-06-25 16:15:56 -0700 | [diff] [blame] | 476 | for (const TableEntry& entry : mManifestHalsTable) |
| 477 | if (!addEntryWithInstance(entry, &manifest)) error.push_back(entry.interfaceName); |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 478 | |
Yifan Hong | b2d096a | 2018-05-01 15:25:23 -0700 | [diff] [blame] | 479 | std::vector<std::string> passthrough; |
| 480 | for (const TableEntry& entry : mImplementationsTable) |
| 481 | if (!addEntryWithoutInstance(entry, &manifest)) passthrough.push_back(entry.interfaceName); |
Yifan Hong | f31aa05 | 2018-02-02 15:17:51 -0800 | [diff] [blame] | 482 | |
Yifan Hong | f31aa05 | 2018-02-02 15:17:51 -0800 | [diff] [blame] | 483 | out << "<!-- " << std::endl |
Yifan Hong | b2d096a | 2018-05-01 15:25:23 -0700 | [diff] [blame] | 484 | << " This is a skeleton " << manifest.type() << " manifest. Notes: " << std::endl |
| 485 | << INIT_VINTF_NOTES; |
| 486 | if (!error.empty()) { |
| 487 | out << std::endl << " The following HALs are not added; see warnings." << std::endl; |
| 488 | for (const auto& e : error) { |
| 489 | out << " " << e << std::endl; |
| 490 | } |
| 491 | } |
| 492 | if (!passthrough.empty()) { |
| 493 | out << std::endl |
| 494 | << " The following HALs are passthrough and no interface or instance " << std::endl |
| 495 | << " names can be inferred." << std::endl; |
| 496 | for (const auto& e : passthrough) { |
| 497 | out << " " << e << std::endl; |
| 498 | } |
| 499 | } |
| 500 | out << "-->" << std::endl; |
Yifan Hong | 8697168 | 2018-08-07 17:02:53 -0700 | [diff] [blame] | 501 | out << vintf::gHalManifestConverter(manifest, vintf::SerializeFlags::HALS_ONLY); |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 502 | } |
| 503 | |
Yifan Hong | f31aa05 | 2018-02-02 15:17:51 -0800 | [diff] [blame] | 504 | std::string ListCommand::INIT_VINTF_NOTES{ |
Yifan Hong | b2d096a | 2018-05-01 15:25:23 -0700 | [diff] [blame] | 505 | " 1. If a HAL is supported in both hwbinder and passthrough transport,\n" |
Yifan Hong | f31aa05 | 2018-02-02 15:17:51 -0800 | [diff] [blame] | 506 | " only hwbinder is shown.\n" |
| 507 | " 2. It is likely that HALs in passthrough transport does not have\n" |
| 508 | " <interface> declared; users will have to write them by hand.\n" |
| 509 | " 3. A HAL with lower minor version can be overridden by a HAL with\n" |
| 510 | " higher minor version if they have the same name and major version.\n" |
Yifan Hong | b2d096a | 2018-05-01 15:25:23 -0700 | [diff] [blame] | 511 | " 4. This output is intended for launch devices.\n" |
| 512 | " Upgrading devices should not use this tool to generate device\n" |
| 513 | " manifest and replace the existing manifest directly, but should\n" |
| 514 | " edit the existing manifest manually.\n" |
| 515 | " Specifically, devices which launched at Android O-MR1 or earlier\n" |
| 516 | " should not use the 'fqname' format for required HAL entries and\n" |
| 517 | " should instead use the legacy package, name, instance-name format\n" |
| 518 | " until they are updated.\n" |
Yifan Hong | f31aa05 | 2018-02-02 15:17:51 -0800 | [diff] [blame] | 519 | }; |
| 520 | |
Yifan Hong | 0ad64f5 | 2018-05-25 15:29:17 -0700 | [diff] [blame] | 521 | static vintf::Arch fromBaseArchitecture(::android::hidl::base::V1_0::DebugInfo::Architecture a) { |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 522 | switch (a) { |
| 523 | case ::android::hidl::base::V1_0::DebugInfo::Architecture::IS_64BIT: |
Yifan Hong | 0ad64f5 | 2018-05-25 15:29:17 -0700 | [diff] [blame] | 524 | return vintf::Arch::ARCH_64; |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 525 | case ::android::hidl::base::V1_0::DebugInfo::Architecture::IS_32BIT: |
Yifan Hong | 0ad64f5 | 2018-05-25 15:29:17 -0700 | [diff] [blame] | 526 | return vintf::Arch::ARCH_32; |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 527 | case ::android::hidl::base::V1_0::DebugInfo::Architecture::UNKNOWN: // fallthrough |
| 528 | default: |
Yifan Hong | 0ad64f5 | 2018-05-25 15:29:17 -0700 | [diff] [blame] | 529 | return vintf::Arch::ARCH_EMPTY; |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 530 | } |
| 531 | } |
| 532 | |
Yifan Hong | ca3b660 | 2017-09-07 16:44:27 -0700 | [diff] [blame] | 533 | void ListCommand::dumpTable(const NullableOStream<std::ostream>& out) const { |
Yifan Hong | 1bc1e9f | 2017-08-29 17:28:12 -0700 | [diff] [blame] | 534 | if (mNeat) { |
Yifan Hong | b72f19e | 2018-06-27 16:58:56 -0700 | [diff] [blame] | 535 | std::vector<const Table*> tables; |
| 536 | forEachTable([&tables](const Table &table) { |
| 537 | tables.push_back(&table); |
| 538 | }); |
| 539 | MergedTable(std::move(tables)).createTextTable().dump(out.buf()); |
Yifan Hong | 1bc1e9f | 2017-08-29 17:28:12 -0700 | [diff] [blame] | 540 | return; |
| 541 | } |
| 542 | |
Yifan Hong | ca3b660 | 2017-09-07 16:44:27 -0700 | [diff] [blame] | 543 | forEachTable([this, &out](const Table &table) { |
Yifan Hong | 1bc1e9f | 2017-08-29 17:28:12 -0700 | [diff] [blame] | 544 | |
Yifan Hong | d4a77e8 | 2017-09-06 19:40:24 -0700 | [diff] [blame] | 545 | // We're only interested in dumping debug info for already |
| 546 | // instantiated services. There's little value in dumping the |
| 547 | // debug info for a service we create on the fly, so we only operate |
| 548 | // on the "mServicesTable". |
| 549 | std::function<std::string(const std::string&)> emitDebugInfo = nullptr; |
| 550 | if (mEmitDebugInfo && &table == &mServicesTable) { |
| 551 | emitDebugInfo = [this](const auto& iName) { |
Yifan Hong | ca3b660 | 2017-09-07 16:44:27 -0700 | [diff] [blame] | 552 | std::stringstream ss; |
Yifan Hong | d4a77e8 | 2017-09-06 19:40:24 -0700 | [diff] [blame] | 553 | auto pair = splitFirst(iName, '/'); |
Steven Moreland | 5f32889 | 2018-01-18 14:38:07 -0800 | [diff] [blame] | 554 | mLshal.emitDebugInfo(pair.first, pair.second, {}, |
| 555 | false /* excludesParentInstances */, ss, |
Yifan Hong | 1bc1e9f | 2017-08-29 17:28:12 -0700 | [diff] [blame] | 556 | NullableOStream<std::ostream>(nullptr)); |
Yifan Hong | ca3b660 | 2017-09-07 16:44:27 -0700 | [diff] [blame] | 557 | return ss.str(); |
Yifan Hong | d4a77e8 | 2017-09-06 19:40:24 -0700 | [diff] [blame] | 558 | }; |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 559 | } |
Yifan Hong | ca3b660 | 2017-09-07 16:44:27 -0700 | [diff] [blame] | 560 | table.createTextTable(mNeat, emitDebugInfo).dump(out.buf()); |
| 561 | out << std::endl; |
Yifan Hong | 1bc1e9f | 2017-08-29 17:28:12 -0700 | [diff] [blame] | 562 | }); |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 563 | } |
| 564 | |
Yifan Hong | ca3b660 | 2017-09-07 16:44:27 -0700 | [diff] [blame] | 565 | Status ListCommand::dump() { |
| 566 | auto dump = mVintf ? &ListCommand::dumpVintf : &ListCommand::dumpTable; |
| 567 | |
| 568 | if (mFileOutputPath.empty()) { |
| 569 | (*this.*dump)(out()); |
| 570 | return OK; |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 571 | } |
Yifan Hong | ca3b660 | 2017-09-07 16:44:27 -0700 | [diff] [blame] | 572 | |
| 573 | std::ofstream fileOutput(mFileOutputPath); |
| 574 | if (!fileOutput.is_open()) { |
| 575 | err() << "Could not open file '" << mFileOutputPath << "'." << std::endl; |
| 576 | return IO_ERROR; |
| 577 | } |
| 578 | chown(mFileOutputPath.c_str(), AID_SHELL, AID_SHELL); |
| 579 | |
| 580 | (*this.*dump)(NullableOStream<std::ostream>(fileOutput)); |
| 581 | |
| 582 | fileOutput.flush(); |
| 583 | fileOutput.close(); |
| 584 | return OK; |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 585 | } |
| 586 | |
Yifan Hong | 20f4ee8 | 2018-06-25 16:21:29 -0700 | [diff] [blame] | 587 | void ListCommand::putEntry(HalType type, TableEntry &&entry) { |
Yifan Hong | db73053 | 2018-06-25 16:32:01 -0700 | [diff] [blame] | 588 | tableForType(type)->add(std::forward<TableEntry>(entry)); |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 589 | } |
| 590 | |
| 591 | Status ListCommand::fetchAllLibraries(const sp<IServiceManager> &manager) { |
Yifan Hong | 13ba0a9 | 2018-06-25 16:15:56 -0700 | [diff] [blame] | 592 | if (!shouldFetchHalType(HalType::PASSTHROUGH_LIBRARIES)) { return OK; } |
Nirav Atre | cce988d | 2018-05-16 11:14:46 -0700 | [diff] [blame] | 593 | |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 594 | using namespace ::android::hardware; |
| 595 | using namespace ::android::hidl::manager::V1_0; |
| 596 | using namespace ::android::hidl::base::V1_0; |
Yifan Hong | f2d557b | 2017-05-24 19:45:02 -0700 | [diff] [blame] | 597 | using std::literals::chrono_literals::operator""s; |
Yifan Hong | af58219 | 2018-04-10 17:45:06 -0700 | [diff] [blame] | 598 | auto ret = timeoutIPC(10s, manager, &IServiceManager::debugDump, [&] (const auto &infos) { |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 599 | std::map<std::string, TableEntry> entries; |
| 600 | for (const auto &info : infos) { |
| 601 | std::string interfaceName = std::string{info.interfaceName.c_str()} + "/" + |
| 602 | std::string{info.instanceName.c_str()}; |
| 603 | entries.emplace(interfaceName, TableEntry{ |
| 604 | .interfaceName = interfaceName, |
Yifan Hong | 8304e41 | 2018-05-25 15:05:36 -0700 | [diff] [blame] | 605 | .transport = vintf::Transport::PASSTHROUGH, |
Yifan Hong | f2d557b | 2017-05-24 19:45:02 -0700 | [diff] [blame] | 606 | .clientPids = info.clientPids, |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 607 | }).first->second.arch |= fromBaseArchitecture(info.arch); |
| 608 | } |
| 609 | for (auto &&pair : entries) { |
Yifan Hong | 20f4ee8 | 2018-06-25 16:21:29 -0700 | [diff] [blame] | 610 | putEntry(HalType::PASSTHROUGH_LIBRARIES, std::move(pair.second)); |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 611 | } |
| 612 | }); |
| 613 | if (!ret.isOk()) { |
Yifan Hong | 76ac14a | 2017-09-08 14:59:04 -0700 | [diff] [blame] | 614 | err() << "Error: Failed to call list on getPassthroughServiceManager(): " |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 615 | << ret.description() << std::endl; |
| 616 | return DUMP_ALL_LIBS_ERROR; |
| 617 | } |
| 618 | return OK; |
| 619 | } |
| 620 | |
| 621 | Status ListCommand::fetchPassthrough(const sp<IServiceManager> &manager) { |
Yifan Hong | 13ba0a9 | 2018-06-25 16:15:56 -0700 | [diff] [blame] | 622 | if (!shouldFetchHalType(HalType::PASSTHROUGH_CLIENTS)) { return OK; } |
Nirav Atre | cce988d | 2018-05-16 11:14:46 -0700 | [diff] [blame] | 623 | |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 624 | using namespace ::android::hardware; |
| 625 | using namespace ::android::hardware::details; |
| 626 | using namespace ::android::hidl::manager::V1_0; |
| 627 | using namespace ::android::hidl::base::V1_0; |
| 628 | auto ret = timeoutIPC(manager, &IServiceManager::debugDump, [&] (const auto &infos) { |
| 629 | for (const auto &info : infos) { |
| 630 | if (info.clientPids.size() <= 0) { |
| 631 | continue; |
| 632 | } |
Yifan Hong | 20f4ee8 | 2018-06-25 16:21:29 -0700 | [diff] [blame] | 633 | putEntry(HalType::PASSTHROUGH_CLIENTS, { |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 634 | .interfaceName = |
| 635 | std::string{info.interfaceName.c_str()} + "/" + |
| 636 | std::string{info.instanceName.c_str()}, |
Yifan Hong | 8304e41 | 2018-05-25 15:05:36 -0700 | [diff] [blame] | 637 | .transport = vintf::Transport::PASSTHROUGH, |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 638 | .serverPid = info.clientPids.size() == 1 ? info.clientPids[0] : NO_PID, |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 639 | .clientPids = info.clientPids, |
| 640 | .arch = fromBaseArchitecture(info.arch) |
| 641 | }); |
| 642 | } |
| 643 | }); |
| 644 | if (!ret.isOk()) { |
Yifan Hong | 76ac14a | 2017-09-08 14:59:04 -0700 | [diff] [blame] | 645 | err() << "Error: Failed to call debugDump on defaultServiceManager(): " |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 646 | << ret.description() << std::endl; |
| 647 | return DUMP_PASSTHROUGH_ERROR; |
| 648 | } |
| 649 | return OK; |
| 650 | } |
| 651 | |
| 652 | Status ListCommand::fetchBinderized(const sp<IServiceManager> &manager) { |
Yifan Hong | 8304e41 | 2018-05-25 15:05:36 -0700 | [diff] [blame] | 653 | using vintf::operator<<; |
| 654 | |
Yifan Hong | 13ba0a9 | 2018-06-25 16:15:56 -0700 | [diff] [blame] | 655 | if (!shouldFetchHalType(HalType::BINDERIZED_SERVICES)) { return OK; } |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 656 | |
Yifan Hong | 8304e41 | 2018-05-25 15:05:36 -0700 | [diff] [blame] | 657 | const vintf::Transport mode = vintf::Transport::HWBINDER; |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 658 | hidl_vec<hidl_string> fqInstanceNames; |
| 659 | // copying out for timeoutIPC |
| 660 | auto listRet = timeoutIPC(manager, &IServiceManager::list, [&] (const auto &names) { |
| 661 | fqInstanceNames = names; |
| 662 | }); |
| 663 | if (!listRet.isOk()) { |
Yifan Hong | 76ac14a | 2017-09-08 14:59:04 -0700 | [diff] [blame] | 664 | err() << "Error: Failed to list services for " << mode << ": " |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 665 | << listRet.description() << std::endl; |
| 666 | return DUMP_BINDERIZED_ERROR; |
| 667 | } |
| 668 | |
| 669 | Status status = OK; |
Yifan Hong | 22ea7b8 | 2017-09-14 18:07:43 -0700 | [diff] [blame] | 670 | std::map<std::string, TableEntry> allTableEntries; |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 671 | for (const auto &fqInstanceName : fqInstanceNames) { |
Yifan Hong | 22ea7b8 | 2017-09-14 18:07:43 -0700 | [diff] [blame] | 672 | // create entry and default assign all fields. |
| 673 | TableEntry& entry = allTableEntries[fqInstanceName]; |
| 674 | entry.interfaceName = fqInstanceName; |
| 675 | entry.transport = mode; |
Yifan Hong | 13ba0a9 | 2018-06-25 16:15:56 -0700 | [diff] [blame] | 676 | entry.serviceStatus = ServiceStatus::NON_RESPONSIVE; |
Yifan Hong | 22ea7b8 | 2017-09-14 18:07:43 -0700 | [diff] [blame] | 677 | |
| 678 | status |= fetchBinderizedEntry(manager, &entry); |
| 679 | } |
| 680 | |
| 681 | for (auto& pair : allTableEntries) { |
Yifan Hong | 20f4ee8 | 2018-06-25 16:21:29 -0700 | [diff] [blame] | 682 | putEntry(HalType::BINDERIZED_SERVICES, std::move(pair.second)); |
Yifan Hong | 22ea7b8 | 2017-09-14 18:07:43 -0700 | [diff] [blame] | 683 | } |
| 684 | return status; |
| 685 | } |
| 686 | |
| 687 | Status ListCommand::fetchBinderizedEntry(const sp<IServiceManager> &manager, |
| 688 | TableEntry *entry) { |
| 689 | Status status = OK; |
| 690 | const auto handleError = [&](Status additionalError, const std::string& msg) { |
| 691 | err() << "Warning: Skipping \"" << entry->interfaceName << "\": " << msg << std::endl; |
| 692 | status |= DUMP_BINDERIZED_ERROR | additionalError; |
| 693 | }; |
| 694 | |
| 695 | const auto pair = splitFirst(entry->interfaceName, '/'); |
| 696 | const auto &serviceName = pair.first; |
| 697 | const auto &instanceName = pair.second; |
| 698 | auto getRet = timeoutIPC(manager, &IServiceManager::get, serviceName, instanceName); |
| 699 | if (!getRet.isOk()) { |
| 700 | handleError(TRANSACTION_ERROR, |
| 701 | "cannot be fetched from service manager:" + getRet.description()); |
| 702 | return status; |
| 703 | } |
| 704 | sp<IBase> service = getRet; |
| 705 | if (service == nullptr) { |
| 706 | handleError(NO_INTERFACE, "cannot be fetched from service manager (null)"); |
| 707 | return status; |
| 708 | } |
| 709 | |
| 710 | // getDebugInfo |
| 711 | do { |
| 712 | DebugInfo debugInfo; |
| 713 | auto debugRet = timeoutIPC(service, &IBase::getDebugInfo, [&] (const auto &received) { |
| 714 | debugInfo = received; |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 715 | }); |
| 716 | if (!debugRet.isOk()) { |
Yifan Hong | 22ea7b8 | 2017-09-14 18:07:43 -0700 | [diff] [blame] | 717 | handleError(TRANSACTION_ERROR, |
| 718 | "debugging information cannot be retrieved: " + debugRet.description()); |
| 719 | break; // skip getPidInfo |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 720 | } |
Steven Moreland | d8e2019 | 2017-05-24 11:23:08 -0700 | [diff] [blame] | 721 | |
Yifan Hong | 22ea7b8 | 2017-09-14 18:07:43 -0700 | [diff] [blame] | 722 | entry->serverPid = debugInfo.pid; |
| 723 | entry->serverObjectAddress = debugInfo.ptr; |
| 724 | entry->arch = fromBaseArchitecture(debugInfo.arch); |
Steven Moreland | d8e2019 | 2017-05-24 11:23:08 -0700 | [diff] [blame] | 725 | |
Yifan Hong | 22ea7b8 | 2017-09-14 18:07:43 -0700 | [diff] [blame] | 726 | if (debugInfo.pid != NO_PID) { |
| 727 | const PidInfo* pidInfo = getPidInfoCached(debugInfo.pid); |
| 728 | if (pidInfo == nullptr) { |
| 729 | handleError(IO_ERROR, |
| 730 | "no information for PID " + std::to_string(debugInfo.pid) + |
| 731 | ", are you root?"); |
| 732 | break; |
| 733 | } |
| 734 | if (debugInfo.ptr != NO_PTR) { |
| 735 | auto it = pidInfo->refPids.find(debugInfo.ptr); |
| 736 | if (it != pidInfo->refPids.end()) { |
| 737 | entry->clientPids = it->second; |
| 738 | } |
| 739 | } |
| 740 | entry->threadUsage = pidInfo->threadUsage; |
| 741 | entry->threadCount = pidInfo->threadCount; |
| 742 | } |
| 743 | } while (0); |
Yifan Hong | fee209d | 2017-09-14 18:23:38 -0700 | [diff] [blame] | 744 | |
| 745 | // hash |
| 746 | do { |
| 747 | ssize_t hashIndex = -1; |
| 748 | auto ifaceChainRet = timeoutIPC(service, &IBase::interfaceChain, [&] (const auto& c) { |
| 749 | for (size_t i = 0; i < c.size(); ++i) { |
| 750 | if (serviceName == c[i]) { |
| 751 | hashIndex = static_cast<ssize_t>(i); |
| 752 | break; |
| 753 | } |
| 754 | } |
| 755 | }); |
| 756 | if (!ifaceChainRet.isOk()) { |
| 757 | handleError(TRANSACTION_ERROR, |
| 758 | "interfaceChain fails: " + ifaceChainRet.description()); |
| 759 | break; // skip getHashChain |
| 760 | } |
| 761 | if (hashIndex < 0) { |
| 762 | handleError(BAD_IMPL, "Interface name does not exist in interfaceChain."); |
| 763 | break; // skip getHashChain |
| 764 | } |
| 765 | auto hashRet = timeoutIPC(service, &IBase::getHashChain, [&] (const auto& hashChain) { |
| 766 | if (static_cast<size_t>(hashIndex) >= hashChain.size()) { |
| 767 | handleError(BAD_IMPL, |
| 768 | "interfaceChain indicates position " + std::to_string(hashIndex) + |
| 769 | " but getHashChain returns " + std::to_string(hashChain.size()) + |
| 770 | " hashes"); |
| 771 | return; |
| 772 | } |
| 773 | |
| 774 | auto&& hashArray = hashChain[hashIndex]; |
| 775 | std::vector<uint8_t> hashVec{hashArray.data(), hashArray.data() + hashArray.size()}; |
| 776 | entry->hash = Hash::hexString(hashVec); |
| 777 | }); |
| 778 | if (!hashRet.isOk()) { |
| 779 | handleError(TRANSACTION_ERROR, "getHashChain failed: " + hashRet.description()); |
| 780 | } |
| 781 | } while (0); |
Yifan Hong | 13ba0a9 | 2018-06-25 16:15:56 -0700 | [diff] [blame] | 782 | if (status == OK) { |
| 783 | entry->serviceStatus = ServiceStatus::ALIVE; |
| 784 | } |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 785 | return status; |
| 786 | } |
| 787 | |
Yifan Hong | 13ba0a9 | 2018-06-25 16:15:56 -0700 | [diff] [blame] | 788 | Status ListCommand::fetchManifestHals() { |
| 789 | if (!shouldFetchHalType(HalType::VINTF_MANIFEST)) { return OK; } |
| 790 | Status status = OK; |
| 791 | |
| 792 | for (auto manifest : {getDeviceManifest(), getFrameworkManifest()}) { |
| 793 | if (manifest == nullptr) { |
| 794 | status |= VINTF_ERROR; |
| 795 | continue; |
| 796 | } |
| 797 | |
| 798 | std::map<std::string, TableEntry> entries; |
| 799 | |
Yifan Hong | c346a16 | 2019-09-10 19:35:55 -0700 | [diff] [blame^] | 800 | manifest->forEachHidlInstance([&] (const vintf::ManifestInstance& manifestInstance) { |
Yifan Hong | 13ba0a9 | 2018-06-25 16:15:56 -0700 | [diff] [blame] | 801 | TableEntry entry{ |
Yifan Hong | c346a16 | 2019-09-10 19:35:55 -0700 | [diff] [blame^] | 802 | .interfaceName = manifestInstance.description(), |
Yifan Hong | 13ba0a9 | 2018-06-25 16:15:56 -0700 | [diff] [blame] | 803 | .transport = manifestInstance.transport(), |
| 804 | .arch = manifestInstance.arch(), |
| 805 | // TODO(b/71555570): Device manifest does not distinguish HALs from vendor or ODM. |
| 806 | .partition = toPartition(manifest->type()), |
| 807 | .serviceStatus = ServiceStatus::DECLARED}; |
| 808 | std::string key = entry.interfaceName; |
| 809 | entries.emplace(std::move(key), std::move(entry)); |
| 810 | return true; |
| 811 | }); |
| 812 | |
| 813 | for (auto&& pair : entries) |
| 814 | mManifestHalsTable.add(std::move(pair.second)); |
| 815 | } |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 816 | return status; |
| 817 | } |
| 818 | |
Yifan Hong | 3212f17 | 2018-06-28 12:39:50 -0700 | [diff] [blame] | 819 | Status ListCommand::fetchLazyHals() { |
| 820 | using vintf::operator<<; |
| 821 | |
| 822 | if (!shouldFetchHalType(HalType::LAZY_HALS)) { return OK; } |
| 823 | Status status = OK; |
| 824 | |
| 825 | for (const TableEntry& manifestEntry : mManifestHalsTable) { |
| 826 | if (manifestEntry.transport == vintf::Transport::HWBINDER) { |
| 827 | if (!hasHwbinderEntry(manifestEntry)) { |
| 828 | mLazyHalsTable.add(TableEntry(manifestEntry)); |
| 829 | } |
| 830 | continue; |
| 831 | } |
| 832 | if (manifestEntry.transport == vintf::Transport::PASSTHROUGH) { |
| 833 | if (!hasPassthroughEntry(manifestEntry)) { |
| 834 | mLazyHalsTable.add(TableEntry(manifestEntry)); |
| 835 | } |
| 836 | continue; |
| 837 | } |
| 838 | err() << "Warning: unrecognized transport in VINTF manifest: " |
| 839 | << manifestEntry.transport; |
| 840 | status |= VINTF_ERROR; |
| 841 | } |
| 842 | return status; |
| 843 | } |
| 844 | |
| 845 | bool ListCommand::hasHwbinderEntry(const TableEntry& entry) const { |
| 846 | for (const TableEntry& existing : mServicesTable) { |
| 847 | if (existing.interfaceName == entry.interfaceName) { |
| 848 | return true; |
| 849 | } |
| 850 | } |
| 851 | return false; |
| 852 | } |
| 853 | |
| 854 | bool ListCommand::hasPassthroughEntry(const TableEntry& entry) const { |
| 855 | FqInstance entryFqInstance; |
| 856 | if (!entryFqInstance.setTo(entry.interfaceName)) { |
| 857 | return false; // cannot parse, so add it anyway. |
| 858 | } |
| 859 | for (const TableEntry& existing : mImplementationsTable) { |
| 860 | FqInstance existingFqInstance; |
| 861 | if (!existingFqInstance.setTo(getPackageAndVersion(existing.interfaceName))) { |
| 862 | continue; |
| 863 | } |
| 864 | |
| 865 | // For example, manifest may say graphics.mapper@2.1 but passthroughServiceManager |
| 866 | // can only list graphics.mapper@2.0. |
| 867 | if (entryFqInstance.getPackage() == existingFqInstance.getPackage() && |
| 868 | vintf::Version{entryFqInstance.getVersion()} |
| 869 | .minorAtLeast(vintf::Version{existingFqInstance.getVersion()})) { |
| 870 | return true; |
| 871 | } |
| 872 | } |
| 873 | return false; |
| 874 | } |
| 875 | |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 876 | Status ListCommand::fetch() { |
| 877 | Status status = OK; |
Yifan Hong | 9881df9 | 2017-05-10 14:33:05 -0700 | [diff] [blame] | 878 | auto bManager = mLshal.serviceManager(); |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 879 | if (bManager == nullptr) { |
Yifan Hong | 76ac14a | 2017-09-08 14:59:04 -0700 | [diff] [blame] | 880 | err() << "Failed to get defaultServiceManager()!" << std::endl; |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 881 | status |= NO_BINDERIZED_MANAGER; |
| 882 | } else { |
| 883 | status |= fetchBinderized(bManager); |
| 884 | // Passthrough PIDs are registered to the binderized manager as well. |
| 885 | status |= fetchPassthrough(bManager); |
| 886 | } |
| 887 | |
Yifan Hong | 9881df9 | 2017-05-10 14:33:05 -0700 | [diff] [blame] | 888 | auto pManager = mLshal.passthroughManager(); |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 889 | if (pManager == nullptr) { |
Yifan Hong | 76ac14a | 2017-09-08 14:59:04 -0700 | [diff] [blame] | 890 | err() << "Failed to get getPassthroughServiceManager()!" << std::endl; |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 891 | status |= NO_PASSTHROUGH_MANAGER; |
| 892 | } else { |
| 893 | status |= fetchAllLibraries(pManager); |
| 894 | } |
Yifan Hong | 13ba0a9 | 2018-06-25 16:15:56 -0700 | [diff] [blame] | 895 | status |= fetchManifestHals(); |
Yifan Hong | 3212f17 | 2018-06-28 12:39:50 -0700 | [diff] [blame] | 896 | status |= fetchLazyHals(); |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 897 | return status; |
| 898 | } |
| 899 | |
Yifan Hong | 13ba0a9 | 2018-06-25 16:15:56 -0700 | [diff] [blame] | 900 | void ListCommand::initFetchTypes() { |
Yifan Hong | 3212f17 | 2018-06-28 12:39:50 -0700 | [diff] [blame] | 901 | // TODO: refactor to do polymorphism on each table (so that dependency graph is not hardcoded). |
| 902 | static const std::map<HalType, std::set<HalType>> kDependencyGraph{ |
| 903 | {HalType::LAZY_HALS, {HalType::BINDERIZED_SERVICES, |
| 904 | HalType::PASSTHROUGH_LIBRARIES, |
| 905 | HalType::VINTF_MANIFEST}}, |
| 906 | }; |
Yifan Hong | 13ba0a9 | 2018-06-25 16:15:56 -0700 | [diff] [blame] | 907 | mFetchTypes.insert(mListTypes.begin(), mListTypes.end()); |
Yifan Hong | 3212f17 | 2018-06-28 12:39:50 -0700 | [diff] [blame] | 908 | for (HalType listType : mListTypes) { |
| 909 | auto it = kDependencyGraph.find(listType); |
| 910 | if (it != kDependencyGraph.end()) { |
| 911 | mFetchTypes.insert(it->second.begin(), it->second.end()); |
| 912 | } |
| 913 | } |
Yifan Hong | 13ba0a9 | 2018-06-25 16:15:56 -0700 | [diff] [blame] | 914 | } |
| 915 | |
Yifan Hong | a6b93f0 | 2017-09-13 16:53:37 -0700 | [diff] [blame] | 916 | void ListCommand::registerAllOptions() { |
| 917 | int v = mOptions.size(); |
| 918 | // A list of acceptable command line options |
| 919 | // key: value returned by getopt_long |
| 920 | // long options with short alternatives |
| 921 | mOptions.push_back({'h', "help", no_argument, v++, [](ListCommand*, const char*) { |
| 922 | return USAGE; |
| 923 | }, ""}); |
| 924 | mOptions.push_back({'i', "interface", no_argument, v++, [](ListCommand* thiz, const char*) { |
| 925 | thiz->mSelectedColumns.push_back(TableColumnType::INTERFACE_NAME); |
| 926 | return OK; |
| 927 | }, "print the instance name column"}); |
Yifan Hong | fee209d | 2017-09-14 18:23:38 -0700 | [diff] [blame] | 928 | mOptions.push_back({'l', "released", no_argument, v++, [](ListCommand* thiz, const char*) { |
| 929 | thiz->mSelectedColumns.push_back(TableColumnType::RELEASED); |
| 930 | return OK; |
Yifan Hong | 430f898 | 2018-05-25 17:28:39 -0700 | [diff] [blame] | 931 | }, "print the 'is released?' column\n(Y=released, N=unreleased, ?=unknown)"}); |
Yifan Hong | a6b93f0 | 2017-09-13 16:53:37 -0700 | [diff] [blame] | 932 | mOptions.push_back({'t', "transport", no_argument, v++, [](ListCommand* thiz, const char*) { |
| 933 | thiz->mSelectedColumns.push_back(TableColumnType::TRANSPORT); |
| 934 | return OK; |
| 935 | }, "print the transport mode column"}); |
| 936 | mOptions.push_back({'r', "arch", no_argument, v++, [](ListCommand* thiz, const char*) { |
| 937 | thiz->mSelectedColumns.push_back(TableColumnType::ARCH); |
| 938 | return OK; |
| 939 | }, "print the bitness column"}); |
Yifan Hong | fee209d | 2017-09-14 18:23:38 -0700 | [diff] [blame] | 940 | mOptions.push_back({'s', "hash", no_argument, v++, [](ListCommand* thiz, const char*) { |
| 941 | thiz->mSelectedColumns.push_back(TableColumnType::HASH); |
| 942 | return OK; |
| 943 | }, "print hash of the interface"}); |
Yifan Hong | a6b93f0 | 2017-09-13 16:53:37 -0700 | [diff] [blame] | 944 | mOptions.push_back({'p', "pid", no_argument, v++, [](ListCommand* thiz, const char*) { |
| 945 | thiz->mSelectedColumns.push_back(TableColumnType::SERVER_PID); |
| 946 | return OK; |
| 947 | }, "print the server PID, or server cmdline if -m is set"}); |
| 948 | mOptions.push_back({'a', "address", no_argument, v++, [](ListCommand* thiz, const char*) { |
| 949 | thiz->mSelectedColumns.push_back(TableColumnType::SERVER_ADDR); |
| 950 | return OK; |
| 951 | }, "print the server object address column"}); |
| 952 | mOptions.push_back({'c', "clients", no_argument, v++, [](ListCommand* thiz, const char*) { |
| 953 | thiz->mSelectedColumns.push_back(TableColumnType::CLIENT_PIDS); |
| 954 | return OK; |
| 955 | }, "print the client PIDs, or client cmdlines if -m is set"}); |
| 956 | mOptions.push_back({'e', "threads", no_argument, v++, [](ListCommand* thiz, const char*) { |
| 957 | thiz->mSelectedColumns.push_back(TableColumnType::THREADS); |
| 958 | return OK; |
| 959 | }, "print currently used/available threads\n(note, available threads created lazily)"}); |
| 960 | mOptions.push_back({'m', "cmdline", no_argument, v++, [](ListCommand* thiz, const char*) { |
| 961 | thiz->mEnableCmdlines = true; |
| 962 | return OK; |
| 963 | }, "print cmdline instead of PIDs"}); |
| 964 | mOptions.push_back({'d', "debug", optional_argument, v++, [](ListCommand* thiz, const char* arg) { |
| 965 | thiz->mEmitDebugInfo = true; |
| 966 | if (arg) thiz->mFileOutputPath = arg; |
| 967 | return OK; |
| 968 | }, "Emit debug info from\nIBase::debug with empty options. Cannot be used with --neat.\n" |
| 969 | "Writes to specified file if 'arg' is provided, otherwise stdout."}); |
| 970 | |
Yifan Hong | bdf44f8 | 2018-05-25 14:20:00 -0700 | [diff] [blame] | 971 | mOptions.push_back({'V', "vintf", no_argument, v++, [](ListCommand* thiz, const char*) { |
| 972 | thiz->mSelectedColumns.push_back(TableColumnType::VINTF); |
| 973 | return OK; |
| 974 | }, "print VINTF info. This column contains a comma-separated list of:\n" |
Steven Moreland | 8e0f539 | 2018-12-12 14:27:24 -0800 | [diff] [blame] | 975 | " - DM: if the HAL is in the device manifest\n" |
| 976 | " - DC: if the HAL is in the device compatibility matrix\n" |
| 977 | " - FM: if the HAL is in the framework manifest\n" |
Steven Moreland | d64ba89 | 2019-07-12 15:59:31 -0700 | [diff] [blame] | 978 | " - FC: if the HAL is in the framework compatibility matrix\n" |
| 979 | " - X: if the HAL is in none of the above lists"}); |
Yifan Hong | 13ba0a9 | 2018-06-25 16:15:56 -0700 | [diff] [blame] | 980 | mOptions.push_back({'S', "service-status", no_argument, v++, [](ListCommand* thiz, const char*) { |
| 981 | thiz->mSelectedColumns.push_back(TableColumnType::SERVICE_STATUS); |
| 982 | return OK; |
| 983 | }, "print service status column. Possible values are:\n" |
| 984 | " - alive: alive and running hwbinder service;\n" |
| 985 | " - registered;dead: registered to hwservicemanager but is not responsive;\n" |
| 986 | " - declared: only declared in VINTF manifest but is not registered to hwservicemanager;\n" |
| 987 | " - N/A: no information for passthrough HALs."}); |
Yifan Hong | bdf44f8 | 2018-05-25 14:20:00 -0700 | [diff] [blame] | 988 | |
Yifan Hong | a6b93f0 | 2017-09-13 16:53:37 -0700 | [diff] [blame] | 989 | // long options without short alternatives |
| 990 | mOptions.push_back({'\0', "init-vintf", no_argument, v++, [](ListCommand* thiz, const char* arg) { |
| 991 | thiz->mVintf = true; |
Yifan Hong | f31aa05 | 2018-02-02 15:17:51 -0800 | [diff] [blame] | 992 | if (thiz->mVintfPartition == Partition::UNKNOWN) |
| 993 | thiz->mVintfPartition = Partition::VENDOR; |
Yifan Hong | a6b93f0 | 2017-09-13 16:53:37 -0700 | [diff] [blame] | 994 | if (arg) thiz->mFileOutputPath = arg; |
| 995 | return OK; |
| 996 | }, "form a skeleton HAL manifest to specified file,\nor stdout if no file specified."}); |
Yifan Hong | f31aa05 | 2018-02-02 15:17:51 -0800 | [diff] [blame] | 997 | mOptions.push_back({'\0', "init-vintf-partition", required_argument, v++, [](ListCommand* thiz, const char* arg) { |
| 998 | if (!arg) return USAGE; |
| 999 | thiz->mVintfPartition = android::procpartition::parsePartition(arg); |
| 1000 | if (thiz->mVintfPartition == Partition::UNKNOWN) return USAGE; |
| 1001 | return OK; |
| 1002 | }, "Specify the partition of the HAL manifest\ngenerated by --init-vintf.\n" |
| 1003 | "Valid values are 'system', 'vendor', and 'odm'. Default is 'vendor'."}); |
Yifan Hong | a6b93f0 | 2017-09-13 16:53:37 -0700 | [diff] [blame] | 1004 | mOptions.push_back({'\0', "sort", required_argument, v++, [](ListCommand* thiz, const char* arg) { |
| 1005 | if (strcmp(arg, "interface") == 0 || strcmp(arg, "i") == 0) { |
| 1006 | thiz->mSortColumn = TableEntry::sortByInterfaceName; |
| 1007 | } else if (strcmp(arg, "pid") == 0 || strcmp(arg, "p") == 0) { |
| 1008 | thiz->mSortColumn = TableEntry::sortByServerPid; |
| 1009 | } else { |
| 1010 | thiz->err() << "Unrecognized sorting column: " << arg << std::endl; |
| 1011 | return USAGE; |
| 1012 | } |
| 1013 | return OK; |
| 1014 | }, "sort by a column. 'arg' can be (i|interface) or (p|pid)."}); |
| 1015 | mOptions.push_back({'\0', "neat", no_argument, v++, [](ListCommand* thiz, const char*) { |
| 1016 | thiz->mNeat = true; |
| 1017 | return OK; |
| 1018 | }, "output is machine parsable (no explanatory text).\nCannot be used with --debug."}); |
Nirav Atre | cce988d | 2018-05-16 11:14:46 -0700 | [diff] [blame] | 1019 | mOptions.push_back({'\0', "types", required_argument, v++, [](ListCommand* thiz, const char* arg) { |
| 1020 | if (!arg) { return USAGE; } |
| 1021 | |
| 1022 | static const std::map<std::string, HalType> kHalTypeMap { |
| 1023 | {"binderized", HalType::BINDERIZED_SERVICES}, |
| 1024 | {"b", HalType::BINDERIZED_SERVICES}, |
| 1025 | {"passthrough_clients", HalType::PASSTHROUGH_CLIENTS}, |
| 1026 | {"c", HalType::PASSTHROUGH_CLIENTS}, |
| 1027 | {"passthrough_libs", HalType::PASSTHROUGH_LIBRARIES}, |
Yifan Hong | 13ba0a9 | 2018-06-25 16:15:56 -0700 | [diff] [blame] | 1028 | {"l", HalType::PASSTHROUGH_LIBRARIES}, |
| 1029 | {"vintf", HalType::VINTF_MANIFEST}, |
| 1030 | {"v", HalType::VINTF_MANIFEST}, |
Yifan Hong | 3212f17 | 2018-06-28 12:39:50 -0700 | [diff] [blame] | 1031 | {"lazy", HalType::LAZY_HALS}, |
| 1032 | {"z", HalType::LAZY_HALS}, |
Nirav Atre | cce988d | 2018-05-16 11:14:46 -0700 | [diff] [blame] | 1033 | }; |
| 1034 | |
| 1035 | std::vector<std::string> halTypesArgs = split(std::string(arg), ','); |
| 1036 | for (const auto& halTypeArg : halTypesArgs) { |
| 1037 | if (halTypeArg.empty()) continue; |
| 1038 | |
| 1039 | const auto& halTypeIter = kHalTypeMap.find(halTypeArg); |
| 1040 | if (halTypeIter == kHalTypeMap.end()) { |
| 1041 | |
| 1042 | thiz->err() << "Unrecognized HAL type: " << halTypeArg << std::endl; |
| 1043 | return USAGE; |
| 1044 | } |
| 1045 | |
| 1046 | // Append unique (non-repeated) HAL types to the reporting list |
| 1047 | HalType halType = halTypeIter->second; |
| 1048 | if (std::find(thiz->mListTypes.begin(), thiz->mListTypes.end(), halType) == |
| 1049 | thiz->mListTypes.end()) { |
| 1050 | thiz->mListTypes.push_back(halType); |
| 1051 | } |
| 1052 | } |
| 1053 | |
| 1054 | if (thiz->mListTypes.empty()) { return USAGE; } |
| 1055 | return OK; |
Yifan Hong | 13ba0a9 | 2018-06-25 16:15:56 -0700 | [diff] [blame] | 1056 | }, "comma-separated list of one or more sections.\nThe output is restricted to the selected " |
| 1057 | "section(s). Valid options\nare: (b|binderized), (c|passthrough_clients), (l|" |
Steven Moreland | 8e0f539 | 2018-12-12 14:27:24 -0800 | [diff] [blame] | 1058 | "passthrough_libs), (v|vintf), and (z|lazy).\nDefault is `bcl`."}); |
Yifan Hong | a6b93f0 | 2017-09-13 16:53:37 -0700 | [diff] [blame] | 1059 | } |
| 1060 | |
| 1061 | // Create 'longopts' argument to getopt_long. Caller is responsible for maintaining |
| 1062 | // the lifetime of "options" during the usage of the returned array. |
| 1063 | static std::unique_ptr<struct option[]> getLongOptions( |
| 1064 | const ListCommand::RegisteredOptions& options, |
| 1065 | int* longOptFlag) { |
| 1066 | std::unique_ptr<struct option[]> ret{new struct option[options.size() + 1]}; |
| 1067 | int i = 0; |
| 1068 | for (const auto& e : options) { |
| 1069 | ret[i].name = e.longOption.c_str(); |
| 1070 | ret[i].has_arg = e.hasArg; |
| 1071 | ret[i].flag = longOptFlag; |
| 1072 | ret[i].val = e.val; |
| 1073 | |
| 1074 | i++; |
| 1075 | } |
| 1076 | // getopt_long last option has all zeros |
Yi Kong | 19d5c00 | 2018-07-20 13:39:55 -0700 | [diff] [blame] | 1077 | ret[i].name = nullptr; |
Yifan Hong | a6b93f0 | 2017-09-13 16:53:37 -0700 | [diff] [blame] | 1078 | ret[i].has_arg = 0; |
Yi Kong | 19d5c00 | 2018-07-20 13:39:55 -0700 | [diff] [blame] | 1079 | ret[i].flag = nullptr; |
Yifan Hong | a6b93f0 | 2017-09-13 16:53:37 -0700 | [diff] [blame] | 1080 | ret[i].val = 0; |
| 1081 | |
| 1082 | return ret; |
| 1083 | } |
| 1084 | |
| 1085 | // Create 'optstring' argument to getopt_long. |
| 1086 | static std::string getShortOptions(const ListCommand::RegisteredOptions& options) { |
| 1087 | std::stringstream ss; |
| 1088 | for (const auto& e : options) { |
| 1089 | if (e.shortOption != '\0') { |
| 1090 | ss << e.shortOption; |
| 1091 | } |
| 1092 | } |
| 1093 | return ss.str(); |
| 1094 | } |
| 1095 | |
Yifan Hong | a8bedc6 | 2017-09-08 18:00:31 -0700 | [diff] [blame] | 1096 | Status ListCommand::parseArgs(const Arg &arg) { |
Nirav Atre | cce988d | 2018-05-16 11:14:46 -0700 | [diff] [blame] | 1097 | mListTypes.clear(); |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 1098 | |
Yifan Hong | a6b93f0 | 2017-09-13 16:53:37 -0700 | [diff] [blame] | 1099 | if (mOptions.empty()) { |
| 1100 | registerAllOptions(); |
| 1101 | } |
| 1102 | int longOptFlag; |
| 1103 | std::unique_ptr<struct option[]> longOptions = getLongOptions(mOptions, &longOptFlag); |
| 1104 | std::string shortOptions = getShortOptions(mOptions); |
Yifan Hong | d4a77e8 | 2017-09-06 19:40:24 -0700 | [diff] [blame] | 1105 | |
Yifan Hong | a8bedc6 | 2017-09-08 18:00:31 -0700 | [diff] [blame] | 1106 | // suppress output to std::err for unknown options |
| 1107 | opterr = 0; |
| 1108 | |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 1109 | int optionIndex; |
| 1110 | int c; |
| 1111 | // Lshal::parseArgs has set optind to the next option to parse |
| 1112 | for (;;) { |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 1113 | c = getopt_long(arg.argc, arg.argv, |
Yifan Hong | a6b93f0 | 2017-09-13 16:53:37 -0700 | [diff] [blame] | 1114 | shortOptions.c_str(), longOptions.get(), &optionIndex); |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 1115 | if (c == -1) { |
| 1116 | break; |
| 1117 | } |
Yifan Hong | a6b93f0 | 2017-09-13 16:53:37 -0700 | [diff] [blame] | 1118 | const RegisteredOption* found = nullptr; |
| 1119 | if (c == 0) { |
| 1120 | // see long option |
| 1121 | for (const auto& e : mOptions) { |
| 1122 | if (longOptFlag == e.val) found = &e; |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 1123 | } |
Yifan Hong | a6b93f0 | 2017-09-13 16:53:37 -0700 | [diff] [blame] | 1124 | } else { |
| 1125 | // see short option |
| 1126 | for (const auto& e : mOptions) { |
| 1127 | if (c == e.shortOption) found = &e; |
| 1128 | } |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 1129 | } |
Yifan Hong | a6b93f0 | 2017-09-13 16:53:37 -0700 | [diff] [blame] | 1130 | |
| 1131 | if (found == nullptr) { |
| 1132 | // see unrecognized options |
Yifan Hong | a8bedc6 | 2017-09-08 18:00:31 -0700 | [diff] [blame] | 1133 | err() << "unrecognized option `" << arg.argv[optind - 1] << "'" << std::endl; |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 1134 | return USAGE; |
| 1135 | } |
Yifan Hong | a6b93f0 | 2017-09-13 16:53:37 -0700 | [diff] [blame] | 1136 | |
| 1137 | Status status = found->op(this, optarg); |
| 1138 | if (status != OK) { |
| 1139 | return status; |
| 1140 | } |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 1141 | } |
| 1142 | if (optind < arg.argc) { |
| 1143 | // see non option |
Yifan Hong | a8bedc6 | 2017-09-08 18:00:31 -0700 | [diff] [blame] | 1144 | err() << "unrecognized option `" << arg.argv[optind] << "'" << std::endl; |
Yifan Hong | 1bc1e9f | 2017-08-29 17:28:12 -0700 | [diff] [blame] | 1145 | return USAGE; |
| 1146 | } |
| 1147 | |
| 1148 | if (mNeat && mEmitDebugInfo) { |
Yifan Hong | 76ac14a | 2017-09-08 14:59:04 -0700 | [diff] [blame] | 1149 | err() << "Error: --neat should not be used with --debug." << std::endl; |
Yifan Hong | 1bc1e9f | 2017-08-29 17:28:12 -0700 | [diff] [blame] | 1150 | return USAGE; |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 1151 | } |
| 1152 | |
Yifan Hong | a6b93f0 | 2017-09-13 16:53:37 -0700 | [diff] [blame] | 1153 | if (mSelectedColumns.empty()) { |
Steven Moreland | 8e0f539 | 2018-12-12 14:27:24 -0800 | [diff] [blame] | 1154 | mSelectedColumns = {TableColumnType::VINTF, TableColumnType::RELEASED, |
Yifan Hong | fee209d | 2017-09-14 18:23:38 -0700 | [diff] [blame] | 1155 | TableColumnType::INTERFACE_NAME, TableColumnType::THREADS, |
Yifan Hong | 05494a5 | 2017-08-29 18:50:00 -0700 | [diff] [blame] | 1156 | TableColumnType::SERVER_PID, TableColumnType::CLIENT_PIDS}; |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 1157 | } |
Yifan Hong | d4a77e8 | 2017-09-06 19:40:24 -0700 | [diff] [blame] | 1158 | |
Yifan Hong | a6b93f0 | 2017-09-13 16:53:37 -0700 | [diff] [blame] | 1159 | if (mEnableCmdlines) { |
| 1160 | for (size_t i = 0; i < mSelectedColumns.size(); ++i) { |
| 1161 | if (mSelectedColumns[i] == TableColumnType::SERVER_PID) { |
| 1162 | mSelectedColumns[i] = TableColumnType::SERVER_CMD; |
Yifan Hong | d4a77e8 | 2017-09-06 19:40:24 -0700 | [diff] [blame] | 1163 | } |
Yifan Hong | a6b93f0 | 2017-09-13 16:53:37 -0700 | [diff] [blame] | 1164 | if (mSelectedColumns[i] == TableColumnType::CLIENT_PIDS) { |
| 1165 | mSelectedColumns[i] = TableColumnType::CLIENT_CMDS; |
Yifan Hong | d4a77e8 | 2017-09-06 19:40:24 -0700 | [diff] [blame] | 1166 | } |
| 1167 | } |
| 1168 | } |
| 1169 | |
Nirav Atre | cce988d | 2018-05-16 11:14:46 -0700 | [diff] [blame] | 1170 | // By default, list all HAL types |
| 1171 | if (mListTypes.empty()) { |
| 1172 | mListTypes = {HalType::BINDERIZED_SERVICES, HalType::PASSTHROUGH_CLIENTS, |
| 1173 | HalType::PASSTHROUGH_LIBRARIES}; |
| 1174 | } |
Yifan Hong | 13ba0a9 | 2018-06-25 16:15:56 -0700 | [diff] [blame] | 1175 | initFetchTypes(); |
Nirav Atre | cce988d | 2018-05-16 11:14:46 -0700 | [diff] [blame] | 1176 | |
Yifan Hong | a6b93f0 | 2017-09-13 16:53:37 -0700 | [diff] [blame] | 1177 | forEachTable([this] (Table& table) { |
| 1178 | table.setSelectedColumns(this->mSelectedColumns); |
Yifan Hong | d4a77e8 | 2017-09-06 19:40:24 -0700 | [diff] [blame] | 1179 | }); |
| 1180 | |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 1181 | return OK; |
| 1182 | } |
| 1183 | |
Yifan Hong | a8bedc6 | 2017-09-08 18:00:31 -0700 | [diff] [blame] | 1184 | Status ListCommand::main(const Arg &arg) { |
| 1185 | Status status = parseArgs(arg); |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 1186 | if (status != OK) { |
| 1187 | return status; |
| 1188 | } |
| 1189 | status = fetch(); |
| 1190 | postprocess(); |
Yifan Hong | ca3b660 | 2017-09-07 16:44:27 -0700 | [diff] [blame] | 1191 | status |= dump(); |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 1192 | return status; |
| 1193 | } |
| 1194 | |
Yifan Hong | a6b93f0 | 2017-09-13 16:53:37 -0700 | [diff] [blame] | 1195 | const std::string& ListCommand::RegisteredOption::getHelpMessageForArgument() const { |
| 1196 | static const std::string empty{}; |
| 1197 | static const std::string optional{"[=<arg>]"}; |
| 1198 | static const std::string required{"=<arg>"}; |
| 1199 | |
| 1200 | if (hasArg == optional_argument) { |
| 1201 | return optional; |
| 1202 | } |
| 1203 | if (hasArg == required_argument) { |
| 1204 | return required; |
| 1205 | } |
| 1206 | return empty; |
| 1207 | } |
| 1208 | |
Yifan Hong | a8bedc6 | 2017-09-08 18:00:31 -0700 | [diff] [blame] | 1209 | void ListCommand::usage() const { |
| 1210 | |
Yifan Hong | a6b93f0 | 2017-09-13 16:53:37 -0700 | [diff] [blame] | 1211 | err() << "list:" << std::endl |
| 1212 | << " lshal" << std::endl |
| 1213 | << " lshal list" << std::endl |
Steven Moreland | 8e0f539 | 2018-12-12 14:27:24 -0800 | [diff] [blame] | 1214 | << " List all hals with default ordering and columns (`lshal list -Vliepc`)" << std::endl |
Yifan Hong | a6b93f0 | 2017-09-13 16:53:37 -0700 | [diff] [blame] | 1215 | << " lshal list [-h|--help]" << std::endl |
| 1216 | << " -h, --help: Print help message for list (`lshal help list`)" << std::endl |
| 1217 | << " lshal [list] [OPTIONS...]" << std::endl; |
| 1218 | for (const auto& e : mOptions) { |
| 1219 | if (e.help.empty()) { |
| 1220 | continue; |
| 1221 | } |
| 1222 | err() << " "; |
| 1223 | if (e.shortOption != '\0') |
| 1224 | err() << "-" << e.shortOption << e.getHelpMessageForArgument(); |
| 1225 | if (e.shortOption != '\0' && !e.longOption.empty()) |
| 1226 | err() << ", "; |
| 1227 | if (!e.longOption.empty()) |
| 1228 | err() << "--" << e.longOption << e.getHelpMessageForArgument(); |
| 1229 | err() << ": "; |
Nirav Atre | cce988d | 2018-05-16 11:14:46 -0700 | [diff] [blame] | 1230 | std::vector<std::string> lines = split(e.help, '\n'); |
Yifan Hong | a6b93f0 | 2017-09-13 16:53:37 -0700 | [diff] [blame] | 1231 | for (const auto& line : lines) { |
| 1232 | if (&line != &lines.front()) |
| 1233 | err() << " "; |
| 1234 | err() << line << std::endl; |
| 1235 | } |
| 1236 | } |
Yifan Hong | a8bedc6 | 2017-09-08 18:00:31 -0700 | [diff] [blame] | 1237 | } |
| 1238 | |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 1239 | } // namespace lshal |
| 1240 | } // namespace android |
Yifan Hong | 05494a5 | 2017-08-29 18:50:00 -0700 | [diff] [blame] | 1241 | |