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