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 | |
| 21 | #include <fstream> |
Yifan Hong | b2d096a | 2018-05-01 15:25:23 -0700 | [diff] [blame^] | 22 | #include <functional> |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 23 | #include <iomanip> |
| 24 | #include <iostream> |
| 25 | #include <map> |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 26 | #include <regex> |
Yifan Hong | b2d096a | 2018-05-01 15:25:23 -0700 | [diff] [blame^] | 27 | #include <sstream> |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 28 | |
Yifan Hong | f31aa05 | 2018-02-02 15:17:51 -0800 | [diff] [blame] | 29 | #include <android-base/file.h> |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 30 | #include <android-base/parseint.h> |
| 31 | #include <android/hidl/manager/1.0/IServiceManager.h> |
Yifan Hong | fee209d | 2017-09-14 18:23:38 -0700 | [diff] [blame] | 32 | #include <hidl-hash/Hash.h> |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 33 | #include <hidl-util/FQName.h> |
| 34 | #include <private/android_filesystem_config.h> |
| 35 | #include <sys/stat.h> |
| 36 | #include <vintf/HalManifest.h> |
Yifan Hong | f31aa05 | 2018-02-02 15:17:51 -0800 | [diff] [blame] | 37 | #include <vintf/parse_string.h> |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 38 | #include <vintf/parse_xml.h> |
| 39 | |
| 40 | #include "Lshal.h" |
| 41 | #include "PipeRelay.h" |
| 42 | #include "Timeout.h" |
| 43 | #include "utils.h" |
| 44 | |
| 45 | using ::android::hardware::hidl_string; |
Yifan Hong | 22ea7b8 | 2017-09-14 18:07:43 -0700 | [diff] [blame] | 46 | using ::android::hardware::hidl_vec; |
| 47 | using ::android::hidl::base::V1_0::DebugInfo; |
| 48 | using ::android::hidl::base::V1_0::IBase; |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 49 | using ::android::hidl::manager::V1_0::IServiceManager; |
| 50 | |
| 51 | namespace android { |
| 52 | namespace lshal { |
| 53 | |
Yifan Hong | f31aa05 | 2018-02-02 15:17:51 -0800 | [diff] [blame] | 54 | vintf::SchemaType toSchemaType(Partition p) { |
| 55 | return (p == Partition::SYSTEM) ? vintf::SchemaType::FRAMEWORK : vintf::SchemaType::DEVICE; |
| 56 | } |
| 57 | |
Yifan Hong | 76ac14a | 2017-09-08 14:59:04 -0700 | [diff] [blame] | 58 | NullableOStream<std::ostream> ListCommand::out() const { |
| 59 | return mLshal.out(); |
| 60 | } |
| 61 | |
| 62 | NullableOStream<std::ostream> ListCommand::err() const { |
| 63 | return mLshal.err(); |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 64 | } |
| 65 | |
Yifan Hong | 795b6ec | 2017-09-13 11:25:28 -0700 | [diff] [blame] | 66 | std::string ListCommand::GetName() { |
| 67 | return "list"; |
| 68 | } |
| 69 | std::string ListCommand::getSimpleDescription() const { |
| 70 | return "List HALs."; |
| 71 | } |
| 72 | |
Yifan Hong | 8bf7316 | 2017-09-07 18:06:13 -0700 | [diff] [blame] | 73 | std::string ListCommand::parseCmdline(pid_t pid) const { |
Yifan Hong | f31aa05 | 2018-02-02 15:17:51 -0800 | [diff] [blame] | 74 | return android::procpartition::getCmdline(pid); |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | const std::string &ListCommand::getCmdline(pid_t pid) { |
| 78 | auto pair = mCmdlines.find(pid); |
| 79 | if (pair != mCmdlines.end()) { |
| 80 | return pair->second; |
| 81 | } |
Yifan Hong | 8bf7316 | 2017-09-07 18:06:13 -0700 | [diff] [blame] | 82 | mCmdlines[pid] = parseCmdline(pid); |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 83 | return mCmdlines[pid]; |
| 84 | } |
| 85 | |
| 86 | void ListCommand::removeDeadProcesses(Pids *pids) { |
| 87 | static const pid_t myPid = getpid(); |
Yifan Hong | 61fb7bc | 2017-05-12 16:33:57 -0700 | [diff] [blame] | 88 | pids->erase(std::remove_if(pids->begin(), pids->end(), [this](auto pid) { |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 89 | return pid == myPid || this->getCmdline(pid).empty(); |
Yifan Hong | 61fb7bc | 2017-05-12 16:33:57 -0700 | [diff] [blame] | 90 | }), pids->end()); |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 91 | } |
| 92 | |
Yifan Hong | f31aa05 | 2018-02-02 15:17:51 -0800 | [diff] [blame] | 93 | Partition ListCommand::getPartition(pid_t pid) { |
| 94 | auto it = mPartitions.find(pid); |
| 95 | if (it != mPartitions.end()) { |
| 96 | return it->second; |
| 97 | } |
| 98 | Partition partition = android::procpartition::getPartition(pid); |
| 99 | mPartitions.emplace(pid, partition); |
| 100 | return partition; |
| 101 | } |
| 102 | |
| 103 | // Give sensible defaults when nothing can be inferred from runtime. |
| 104 | // process: Partition inferred from executable location or cmdline. |
Yifan Hong | b2d096a | 2018-05-01 15:25:23 -0700 | [diff] [blame^] | 105 | Partition ListCommand::resolvePartition(Partition process, const FqInstance& fqInstance) const { |
| 106 | if (fqInstance.inPackage("vendor") || fqInstance.inPackage("com")) { |
Yifan Hong | f31aa05 | 2018-02-02 15:17:51 -0800 | [diff] [blame] | 107 | return Partition::VENDOR; |
| 108 | } |
| 109 | |
Yifan Hong | b2d096a | 2018-05-01 15:25:23 -0700 | [diff] [blame^] | 110 | if (fqInstance.inPackage("android.frameworks") || fqInstance.inPackage("android.system") || |
| 111 | fqInstance.inPackage("android.hidl")) { |
Yifan Hong | f31aa05 | 2018-02-02 15:17:51 -0800 | [diff] [blame] | 112 | return Partition::SYSTEM; |
| 113 | } |
| 114 | |
| 115 | // Some android.hardware HALs are served from system. Check the value from executable |
| 116 | // location / cmdline first. |
Yifan Hong | b2d096a | 2018-05-01 15:25:23 -0700 | [diff] [blame^] | 117 | if (fqInstance.inPackage("android.hardware")) { |
Yifan Hong | f31aa05 | 2018-02-02 15:17:51 -0800 | [diff] [blame] | 118 | if (process != Partition::UNKNOWN) { |
| 119 | return process; |
| 120 | } |
| 121 | return Partition::VENDOR; |
| 122 | } |
| 123 | |
| 124 | return process; |
| 125 | } |
| 126 | |
Yifan Hong | 1243dde | 2017-09-14 17:49:30 -0700 | [diff] [blame] | 127 | static bool scanBinderContext(pid_t pid, |
Steven Moreland | d8e2019 | 2017-05-24 11:23:08 -0700 | [diff] [blame] | 128 | const std::string &contextName, |
| 129 | std::function<void(const std::string&)> eachLine) { |
| 130 | std::ifstream ifs("/d/binder/proc/" + std::to_string(pid)); |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 131 | if (!ifs.is_open()) { |
| 132 | return false; |
| 133 | } |
| 134 | |
Steven Moreland | d8e2019 | 2017-05-24 11:23:08 -0700 | [diff] [blame] | 135 | static const std::regex kContextLine("^context (\\w+)$"); |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 136 | |
Steven Moreland | d8e2019 | 2017-05-24 11:23:08 -0700 | [diff] [blame] | 137 | bool isDesiredContext = false; |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 138 | std::string line; |
| 139 | std::smatch match; |
| 140 | while(getline(ifs, line)) { |
Steven Moreland | d8e2019 | 2017-05-24 11:23:08 -0700 | [diff] [blame] | 141 | if (std::regex_search(line, match, kContextLine)) { |
| 142 | isDesiredContext = match.str(1) == contextName; |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 143 | continue; |
| 144 | } |
Steven Moreland | d8e2019 | 2017-05-24 11:23:08 -0700 | [diff] [blame] | 145 | |
| 146 | if (!isDesiredContext) { |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 147 | continue; |
| 148 | } |
Steven Moreland | d8e2019 | 2017-05-24 11:23:08 -0700 | [diff] [blame] | 149 | |
| 150 | eachLine(line); |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 151 | } |
| 152 | return true; |
| 153 | } |
| 154 | |
Steven Moreland | d8e2019 | 2017-05-24 11:23:08 -0700 | [diff] [blame] | 155 | bool ListCommand::getPidInfo( |
| 156 | pid_t serverPid, PidInfo *pidInfo) const { |
| 157 | static const std::regex kReferencePrefix("^\\s*node \\d+:\\s+u([0-9a-f]+)\\s+c([0-9a-f]+)\\s+"); |
| 158 | static const std::regex kThreadPrefix("^\\s*thread \\d+:\\s+l\\s+(\\d)(\\d)"); |
| 159 | |
| 160 | std::smatch match; |
| 161 | return scanBinderContext(serverPid, "hwbinder", [&](const std::string& line) { |
| 162 | if (std::regex_search(line, match, kReferencePrefix)) { |
| 163 | const std::string &ptrString = "0x" + match.str(2); // use number after c |
| 164 | uint64_t ptr; |
| 165 | if (!::android::base::ParseUint(ptrString.c_str(), &ptr)) { |
| 166 | // Should not reach here, but just be tolerant. |
Yifan Hong | 76ac14a | 2017-09-08 14:59:04 -0700 | [diff] [blame] | 167 | err() << "Could not parse number " << ptrString << std::endl; |
Steven Moreland | d8e2019 | 2017-05-24 11:23:08 -0700 | [diff] [blame] | 168 | return; |
| 169 | } |
| 170 | const std::string proc = " proc "; |
| 171 | auto pos = line.rfind(proc); |
| 172 | if (pos != std::string::npos) { |
| 173 | for (const std::string &pidStr : split(line.substr(pos + proc.size()), ' ')) { |
| 174 | int32_t pid; |
| 175 | if (!::android::base::ParseInt(pidStr, &pid)) { |
Yifan Hong | 76ac14a | 2017-09-08 14:59:04 -0700 | [diff] [blame] | 176 | err() << "Could not parse number " << pidStr << std::endl; |
Steven Moreland | d8e2019 | 2017-05-24 11:23:08 -0700 | [diff] [blame] | 177 | return; |
| 178 | } |
| 179 | pidInfo->refPids[ptr].push_back(pid); |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | return; |
| 184 | } |
| 185 | |
| 186 | if (std::regex_search(line, match, kThreadPrefix)) { |
| 187 | // "1" is waiting in binder driver |
| 188 | // "2" is poll. It's impossible to tell if these are in use. |
| 189 | // and HIDL default code doesn't use it. |
| 190 | bool isInUse = match.str(1) != "1"; |
| 191 | // "0" is a thread that has called into binder |
| 192 | // "1" is looper thread |
| 193 | // "2" is main looper thread |
| 194 | bool isHwbinderThread = match.str(2) != "0"; |
| 195 | |
| 196 | if (!isHwbinderThread) { |
| 197 | return; |
| 198 | } |
| 199 | |
| 200 | if (isInUse) { |
| 201 | pidInfo->threadUsage++; |
| 202 | } |
| 203 | |
| 204 | pidInfo->threadCount++; |
| 205 | return; |
| 206 | } |
| 207 | |
| 208 | // not reference or thread line |
| 209 | return; |
| 210 | }); |
| 211 | } |
| 212 | |
Yifan Hong | 1243dde | 2017-09-14 17:49:30 -0700 | [diff] [blame] | 213 | const PidInfo* ListCommand::getPidInfoCached(pid_t serverPid) { |
| 214 | auto pair = mCachedPidInfos.insert({serverPid, PidInfo{}}); |
| 215 | if (pair.second /* did insertion take place? */) { |
| 216 | if (!getPidInfo(serverPid, &pair.first->second)) { |
| 217 | return nullptr; |
| 218 | } |
| 219 | } |
| 220 | return &pair.first->second; |
| 221 | } |
| 222 | |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 223 | // Must process hwbinder services first, then passthrough services. |
| 224 | void ListCommand::forEachTable(const std::function<void(Table &)> &f) { |
| 225 | f(mServicesTable); |
| 226 | f(mPassthroughRefTable); |
| 227 | f(mImplementationsTable); |
| 228 | } |
| 229 | void ListCommand::forEachTable(const std::function<void(const Table &)> &f) const { |
| 230 | f(mServicesTable); |
| 231 | f(mPassthroughRefTable); |
| 232 | f(mImplementationsTable); |
| 233 | } |
| 234 | |
| 235 | void ListCommand::postprocess() { |
| 236 | forEachTable([this](Table &table) { |
| 237 | if (mSortColumn) { |
| 238 | std::sort(table.begin(), table.end(), mSortColumn); |
| 239 | } |
| 240 | for (TableEntry &entry : table) { |
| 241 | entry.serverCmdline = getCmdline(entry.serverPid); |
| 242 | removeDeadProcesses(&entry.clientPids); |
| 243 | for (auto pid : entry.clientPids) { |
| 244 | entry.clientCmdlines.push_back(this->getCmdline(pid)); |
| 245 | } |
| 246 | } |
Yifan Hong | f31aa05 | 2018-02-02 15:17:51 -0800 | [diff] [blame] | 247 | for (TableEntry& entry : table) { |
| 248 | entry.partition = getPartition(entry.serverPid); |
| 249 | } |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 250 | }); |
| 251 | // use a double for loop here because lshal doesn't care about efficiency. |
| 252 | for (TableEntry &packageEntry : mImplementationsTable) { |
| 253 | std::string packageName = packageEntry.interfaceName; |
Steven Moreland | d4f32b3 | 2018-03-06 14:47:58 -0800 | [diff] [blame] | 254 | FQName fqPackageName; |
| 255 | if (!FQName::parse(packageName.substr(0, packageName.find("::")), &fqPackageName)) { |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 256 | continue; |
| 257 | } |
| 258 | for (TableEntry &interfaceEntry : mPassthroughRefTable) { |
| 259 | if (interfaceEntry.arch != ARCH_UNKNOWN) { |
| 260 | continue; |
| 261 | } |
Steven Moreland | d4f32b3 | 2018-03-06 14:47:58 -0800 | [diff] [blame] | 262 | FQName interfaceName; |
| 263 | if (!FQName::parse(splitFirst(interfaceEntry.interfaceName, '/').first, &interfaceName)) { |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 264 | continue; |
| 265 | } |
| 266 | if (interfaceName.getPackageAndVersion() == fqPackageName) { |
| 267 | interfaceEntry.arch = packageEntry.arch; |
| 268 | } |
| 269 | } |
| 270 | } |
Yifan Hong | ca3b660 | 2017-09-07 16:44:27 -0700 | [diff] [blame] | 271 | |
| 272 | mServicesTable.setDescription( |
| 273 | "All binderized services (registered services through hwservicemanager)"); |
| 274 | mPassthroughRefTable.setDescription( |
| 275 | "All interfaces that getService() has ever return as a passthrough interface;\n" |
| 276 | "PIDs / processes shown below might be inaccurate because the process\n" |
| 277 | "might have relinquished the interface or might have died.\n" |
| 278 | "The Server / Server CMD column can be ignored.\n" |
| 279 | "The Clients / Clients CMD column shows all process that have ever dlopen'ed \n" |
| 280 | "the library and successfully fetched the passthrough implementation."); |
| 281 | mImplementationsTable.setDescription( |
Steven Moreland | 3d2c1e1 | 2018-03-14 10:41:13 -0700 | [diff] [blame] | 282 | "All available passthrough implementations (all -impl.so files).\n" |
| 283 | "These may return subclasses through their respective HIDL_FETCH_I* functions."); |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 284 | } |
| 285 | |
Yifan Hong | b2d096a | 2018-05-01 15:25:23 -0700 | [diff] [blame^] | 286 | bool ListCommand::addEntryWithInstance(const TableEntry& entry, |
| 287 | vintf::HalManifest* manifest) const { |
| 288 | FqInstance fqInstance; |
| 289 | if (!fqInstance.setTo(entry.interfaceName)) { |
| 290 | err() << "Warning: '" << entry.interfaceName << "' is not a valid FqInstance." << std::endl; |
| 291 | return false; |
Yifan Hong | 77c8782 | 2017-06-19 15:47:39 -0700 | [diff] [blame] | 292 | } |
Yifan Hong | b2d096a | 2018-05-01 15:25:23 -0700 | [diff] [blame^] | 293 | |
| 294 | if (fqInstance.getPackage() == gIBaseFqName.package()) { |
| 295 | return true; // always remove IBase from manifest |
| 296 | } |
| 297 | |
| 298 | Partition partition = resolvePartition(entry.partition, fqInstance); |
| 299 | |
| 300 | if (partition == Partition::UNKNOWN) { |
| 301 | err() << "Warning: Cannot guess the partition of FqInstance " << fqInstance.string() |
| 302 | << std::endl; |
| 303 | return false; |
| 304 | } |
| 305 | |
| 306 | if (partition != mVintfPartition) { |
| 307 | return true; // strip out instances that is in a different partition. |
| 308 | } |
| 309 | |
| 310 | vintf::Transport transport; |
| 311 | vintf::Arch arch; |
| 312 | if (entry.transport == "hwbinder") { |
| 313 | transport = vintf::Transport::HWBINDER; |
| 314 | arch = vintf::Arch::ARCH_EMPTY; |
| 315 | } else if (entry.transport == "passthrough") { |
| 316 | transport = vintf::Transport::PASSTHROUGH; |
| 317 | switch (entry.arch) { |
| 318 | case lshal::ARCH32: |
| 319 | arch = vintf::Arch::ARCH_32; |
| 320 | break; |
| 321 | case lshal::ARCH64: |
| 322 | arch = vintf::Arch::ARCH_64; |
| 323 | break; |
| 324 | case lshal::ARCH_BOTH: |
| 325 | arch = vintf::Arch::ARCH_32_64; |
| 326 | break; |
| 327 | case lshal::ARCH_UNKNOWN: // fallthrough |
| 328 | default: |
| 329 | err() << "Warning: '" << entry.interfaceName << "' doesn't have bitness info."; |
| 330 | return false; |
| 331 | } |
| 332 | } else { |
| 333 | err() << "Warning: '" << entry.transport << "' is not a valid transport." << std::endl; |
| 334 | return false; |
| 335 | } |
| 336 | |
| 337 | std::string e; |
| 338 | if (!manifest->insertInstance(fqInstance, transport, arch, vintf::HalFormat::HIDL, &e)) { |
| 339 | err() << "Warning: Cannot insert '" << fqInstance.string() << ": " << e << std::endl; |
| 340 | return false; |
| 341 | } |
| 342 | return true; |
| 343 | } |
| 344 | |
| 345 | bool ListCommand::addEntryWithoutInstance(const TableEntry& entry, |
| 346 | const vintf::HalManifest* manifest) const { |
| 347 | const auto& packageAndVersion = splitFirst(splitFirst(entry.interfaceName, ':').first, '@'); |
| 348 | const auto& package = packageAndVersion.first; |
| 349 | vintf::Version version; |
| 350 | if (!vintf::parse(packageAndVersion.second, &version)) { |
| 351 | err() << "Warning: Cannot parse version '" << packageAndVersion.second << "' for entry '" |
| 352 | << entry.interfaceName << "'" << std::endl; |
| 353 | return false; |
| 354 | } |
| 355 | |
| 356 | bool found = false; |
| 357 | (void)manifest->forEachInstanceOfVersion(package, version, [&found](const auto&) { |
| 358 | found = true; |
| 359 | return false; // break |
| 360 | }); |
| 361 | return found; |
Yifan Hong | 77c8782 | 2017-06-19 15:47:39 -0700 | [diff] [blame] | 362 | } |
| 363 | |
Yifan Hong | ca3b660 | 2017-09-07 16:44:27 -0700 | [diff] [blame] | 364 | void ListCommand::dumpVintf(const NullableOStream<std::ostream>& out) const { |
Yifan Hong | 236301c | 2017-06-19 12:27:08 -0700 | [diff] [blame] | 365 | using vintf::operator|=; |
Yifan Hong | f31aa05 | 2018-02-02 15:17:51 -0800 | [diff] [blame] | 366 | using vintf::operator<<; |
Yifan Hong | b2d096a | 2018-05-01 15:25:23 -0700 | [diff] [blame^] | 367 | using namespace std::placeholders; |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 368 | |
| 369 | vintf::HalManifest manifest; |
Yifan Hong | f31aa05 | 2018-02-02 15:17:51 -0800 | [diff] [blame] | 370 | manifest.setType(toSchemaType(mVintfPartition)); |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 371 | |
Yifan Hong | b2d096a | 2018-05-01 15:25:23 -0700 | [diff] [blame^] | 372 | std::vector<std::string> error; |
| 373 | for (const TableEntry& entry : mServicesTable) |
| 374 | if (!addEntryWithInstance(entry, &manifest)) error.push_back(entry.interfaceName); |
| 375 | for (const TableEntry& entry : mPassthroughRefTable) |
| 376 | if (!addEntryWithInstance(entry, &manifest)) error.push_back(entry.interfaceName); |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 377 | |
Yifan Hong | b2d096a | 2018-05-01 15:25:23 -0700 | [diff] [blame^] | 378 | std::vector<std::string> passthrough; |
| 379 | for (const TableEntry& entry : mImplementationsTable) |
| 380 | if (!addEntryWithoutInstance(entry, &manifest)) passthrough.push_back(entry.interfaceName); |
Yifan Hong | f31aa05 | 2018-02-02 15:17:51 -0800 | [diff] [blame] | 381 | |
Yifan Hong | f31aa05 | 2018-02-02 15:17:51 -0800 | [diff] [blame] | 382 | out << "<!-- " << std::endl |
Yifan Hong | b2d096a | 2018-05-01 15:25:23 -0700 | [diff] [blame^] | 383 | << " This is a skeleton " << manifest.type() << " manifest. Notes: " << std::endl |
| 384 | << INIT_VINTF_NOTES; |
| 385 | if (!error.empty()) { |
| 386 | out << std::endl << " The following HALs are not added; see warnings." << std::endl; |
| 387 | for (const auto& e : error) { |
| 388 | out << " " << e << std::endl; |
| 389 | } |
| 390 | } |
| 391 | if (!passthrough.empty()) { |
| 392 | out << std::endl |
| 393 | << " The following HALs are passthrough and no interface or instance " << std::endl |
| 394 | << " names can be inferred." << std::endl; |
| 395 | for (const auto& e : passthrough) { |
| 396 | out << " " << e << std::endl; |
| 397 | } |
| 398 | } |
| 399 | out << "-->" << std::endl; |
| 400 | out << vintf::gHalManifestConverter(manifest, vintf::SerializeFlag::HALS_ONLY); |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 401 | } |
| 402 | |
Yifan Hong | f31aa05 | 2018-02-02 15:17:51 -0800 | [diff] [blame] | 403 | std::string ListCommand::INIT_VINTF_NOTES{ |
Yifan Hong | b2d096a | 2018-05-01 15:25:23 -0700 | [diff] [blame^] | 404 | " 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] | 405 | " only hwbinder is shown.\n" |
| 406 | " 2. It is likely that HALs in passthrough transport does not have\n" |
| 407 | " <interface> declared; users will have to write them by hand.\n" |
| 408 | " 3. A HAL with lower minor version can be overridden by a HAL with\n" |
| 409 | " 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^] | 410 | " 4. This output is intended for launch devices.\n" |
| 411 | " Upgrading devices should not use this tool to generate device\n" |
| 412 | " manifest and replace the existing manifest directly, but should\n" |
| 413 | " edit the existing manifest manually.\n" |
| 414 | " Specifically, devices which launched at Android O-MR1 or earlier\n" |
| 415 | " should not use the 'fqname' format for required HAL entries and\n" |
| 416 | " should instead use the legacy package, name, instance-name format\n" |
| 417 | " until they are updated.\n" |
Yifan Hong | f31aa05 | 2018-02-02 15:17:51 -0800 | [diff] [blame] | 418 | }; |
| 419 | |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 420 | static Architecture fromBaseArchitecture(::android::hidl::base::V1_0::DebugInfo::Architecture a) { |
| 421 | switch (a) { |
| 422 | case ::android::hidl::base::V1_0::DebugInfo::Architecture::IS_64BIT: |
| 423 | return ARCH64; |
| 424 | case ::android::hidl::base::V1_0::DebugInfo::Architecture::IS_32BIT: |
| 425 | return ARCH32; |
| 426 | case ::android::hidl::base::V1_0::DebugInfo::Architecture::UNKNOWN: // fallthrough |
| 427 | default: |
| 428 | return ARCH_UNKNOWN; |
| 429 | } |
| 430 | } |
| 431 | |
Yifan Hong | ca3b660 | 2017-09-07 16:44:27 -0700 | [diff] [blame] | 432 | void ListCommand::dumpTable(const NullableOStream<std::ostream>& out) const { |
Yifan Hong | 1bc1e9f | 2017-08-29 17:28:12 -0700 | [diff] [blame] | 433 | if (mNeat) { |
Yifan Hong | d4a77e8 | 2017-09-06 19:40:24 -0700 | [diff] [blame] | 434 | MergedTable({&mServicesTable, &mPassthroughRefTable, &mImplementationsTable}) |
Yifan Hong | ca3b660 | 2017-09-07 16:44:27 -0700 | [diff] [blame] | 435 | .createTextTable().dump(out.buf()); |
Yifan Hong | 1bc1e9f | 2017-08-29 17:28:12 -0700 | [diff] [blame] | 436 | return; |
| 437 | } |
| 438 | |
Yifan Hong | ca3b660 | 2017-09-07 16:44:27 -0700 | [diff] [blame] | 439 | forEachTable([this, &out](const Table &table) { |
Yifan Hong | 1bc1e9f | 2017-08-29 17:28:12 -0700 | [diff] [blame] | 440 | |
Yifan Hong | d4a77e8 | 2017-09-06 19:40:24 -0700 | [diff] [blame] | 441 | // We're only interested in dumping debug info for already |
| 442 | // instantiated services. There's little value in dumping the |
| 443 | // debug info for a service we create on the fly, so we only operate |
| 444 | // on the "mServicesTable". |
| 445 | std::function<std::string(const std::string&)> emitDebugInfo = nullptr; |
| 446 | if (mEmitDebugInfo && &table == &mServicesTable) { |
| 447 | emitDebugInfo = [this](const auto& iName) { |
Yifan Hong | ca3b660 | 2017-09-07 16:44:27 -0700 | [diff] [blame] | 448 | std::stringstream ss; |
Yifan Hong | d4a77e8 | 2017-09-06 19:40:24 -0700 | [diff] [blame] | 449 | auto pair = splitFirst(iName, '/'); |
Steven Moreland | 5f32889 | 2018-01-18 14:38:07 -0800 | [diff] [blame] | 450 | mLshal.emitDebugInfo(pair.first, pair.second, {}, |
| 451 | false /* excludesParentInstances */, ss, |
Yifan Hong | 1bc1e9f | 2017-08-29 17:28:12 -0700 | [diff] [blame] | 452 | NullableOStream<std::ostream>(nullptr)); |
Yifan Hong | ca3b660 | 2017-09-07 16:44:27 -0700 | [diff] [blame] | 453 | return ss.str(); |
Yifan Hong | d4a77e8 | 2017-09-06 19:40:24 -0700 | [diff] [blame] | 454 | }; |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 455 | } |
Yifan Hong | ca3b660 | 2017-09-07 16:44:27 -0700 | [diff] [blame] | 456 | table.createTextTable(mNeat, emitDebugInfo).dump(out.buf()); |
| 457 | out << std::endl; |
Yifan Hong | 1bc1e9f | 2017-08-29 17:28:12 -0700 | [diff] [blame] | 458 | }); |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 459 | } |
| 460 | |
Yifan Hong | ca3b660 | 2017-09-07 16:44:27 -0700 | [diff] [blame] | 461 | Status ListCommand::dump() { |
| 462 | auto dump = mVintf ? &ListCommand::dumpVintf : &ListCommand::dumpTable; |
| 463 | |
| 464 | if (mFileOutputPath.empty()) { |
| 465 | (*this.*dump)(out()); |
| 466 | return OK; |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 467 | } |
Yifan Hong | ca3b660 | 2017-09-07 16:44:27 -0700 | [diff] [blame] | 468 | |
| 469 | std::ofstream fileOutput(mFileOutputPath); |
| 470 | if (!fileOutput.is_open()) { |
| 471 | err() << "Could not open file '" << mFileOutputPath << "'." << std::endl; |
| 472 | return IO_ERROR; |
| 473 | } |
| 474 | chown(mFileOutputPath.c_str(), AID_SHELL, AID_SHELL); |
| 475 | |
| 476 | (*this.*dump)(NullableOStream<std::ostream>(fileOutput)); |
| 477 | |
| 478 | fileOutput.flush(); |
| 479 | fileOutput.close(); |
| 480 | return OK; |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | void ListCommand::putEntry(TableEntrySource source, TableEntry &&entry) { |
| 484 | Table *table = nullptr; |
| 485 | switch (source) { |
| 486 | case HWSERVICEMANAGER_LIST : |
| 487 | table = &mServicesTable; break; |
| 488 | case PTSERVICEMANAGER_REG_CLIENT : |
| 489 | table = &mPassthroughRefTable; break; |
| 490 | case LIST_DLLIB : |
| 491 | table = &mImplementationsTable; break; |
| 492 | default: |
Yifan Hong | 76ac14a | 2017-09-08 14:59:04 -0700 | [diff] [blame] | 493 | err() << "Error: Unknown source of entry " << source << std::endl; |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 494 | } |
| 495 | if (table) { |
Yifan Hong | d4a77e8 | 2017-09-06 19:40:24 -0700 | [diff] [blame] | 496 | table->add(std::forward<TableEntry>(entry)); |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 497 | } |
| 498 | } |
| 499 | |
| 500 | Status ListCommand::fetchAllLibraries(const sp<IServiceManager> &manager) { |
| 501 | using namespace ::android::hardware; |
| 502 | using namespace ::android::hidl::manager::V1_0; |
| 503 | using namespace ::android::hidl::base::V1_0; |
Yifan Hong | f2d557b | 2017-05-24 19:45:02 -0700 | [diff] [blame] | 504 | using std::literals::chrono_literals::operator""s; |
| 505 | auto ret = timeoutIPC(2s, manager, &IServiceManager::debugDump, [&] (const auto &infos) { |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 506 | std::map<std::string, TableEntry> entries; |
| 507 | for (const auto &info : infos) { |
| 508 | std::string interfaceName = std::string{info.interfaceName.c_str()} + "/" + |
| 509 | std::string{info.instanceName.c_str()}; |
| 510 | entries.emplace(interfaceName, TableEntry{ |
| 511 | .interfaceName = interfaceName, |
| 512 | .transport = "passthrough", |
Yifan Hong | f2d557b | 2017-05-24 19:45:02 -0700 | [diff] [blame] | 513 | .clientPids = info.clientPids, |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 514 | }).first->second.arch |= fromBaseArchitecture(info.arch); |
| 515 | } |
| 516 | for (auto &&pair : entries) { |
| 517 | putEntry(LIST_DLLIB, std::move(pair.second)); |
| 518 | } |
| 519 | }); |
| 520 | if (!ret.isOk()) { |
Yifan Hong | 76ac14a | 2017-09-08 14:59:04 -0700 | [diff] [blame] | 521 | err() << "Error: Failed to call list on getPassthroughServiceManager(): " |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 522 | << ret.description() << std::endl; |
| 523 | return DUMP_ALL_LIBS_ERROR; |
| 524 | } |
| 525 | return OK; |
| 526 | } |
| 527 | |
| 528 | Status ListCommand::fetchPassthrough(const sp<IServiceManager> &manager) { |
| 529 | using namespace ::android::hardware; |
| 530 | using namespace ::android::hardware::details; |
| 531 | using namespace ::android::hidl::manager::V1_0; |
| 532 | using namespace ::android::hidl::base::V1_0; |
| 533 | auto ret = timeoutIPC(manager, &IServiceManager::debugDump, [&] (const auto &infos) { |
| 534 | for (const auto &info : infos) { |
| 535 | if (info.clientPids.size() <= 0) { |
| 536 | continue; |
| 537 | } |
| 538 | putEntry(PTSERVICEMANAGER_REG_CLIENT, { |
| 539 | .interfaceName = |
| 540 | std::string{info.interfaceName.c_str()} + "/" + |
| 541 | std::string{info.instanceName.c_str()}, |
| 542 | .transport = "passthrough", |
| 543 | .serverPid = info.clientPids.size() == 1 ? info.clientPids[0] : NO_PID, |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 544 | .clientPids = info.clientPids, |
| 545 | .arch = fromBaseArchitecture(info.arch) |
| 546 | }); |
| 547 | } |
| 548 | }); |
| 549 | if (!ret.isOk()) { |
Yifan Hong | 76ac14a | 2017-09-08 14:59:04 -0700 | [diff] [blame] | 550 | err() << "Error: Failed to call debugDump on defaultServiceManager(): " |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 551 | << ret.description() << std::endl; |
| 552 | return DUMP_PASSTHROUGH_ERROR; |
| 553 | } |
| 554 | return OK; |
| 555 | } |
| 556 | |
| 557 | Status ListCommand::fetchBinderized(const sp<IServiceManager> &manager) { |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 558 | const std::string mode = "hwbinder"; |
| 559 | |
| 560 | hidl_vec<hidl_string> fqInstanceNames; |
| 561 | // copying out for timeoutIPC |
| 562 | auto listRet = timeoutIPC(manager, &IServiceManager::list, [&] (const auto &names) { |
| 563 | fqInstanceNames = names; |
| 564 | }); |
| 565 | if (!listRet.isOk()) { |
Yifan Hong | 76ac14a | 2017-09-08 14:59:04 -0700 | [diff] [blame] | 566 | err() << "Error: Failed to list services for " << mode << ": " |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 567 | << listRet.description() << std::endl; |
| 568 | return DUMP_BINDERIZED_ERROR; |
| 569 | } |
| 570 | |
| 571 | Status status = OK; |
Yifan Hong | 22ea7b8 | 2017-09-14 18:07:43 -0700 | [diff] [blame] | 572 | std::map<std::string, TableEntry> allTableEntries; |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 573 | for (const auto &fqInstanceName : fqInstanceNames) { |
Yifan Hong | 22ea7b8 | 2017-09-14 18:07:43 -0700 | [diff] [blame] | 574 | // create entry and default assign all fields. |
| 575 | TableEntry& entry = allTableEntries[fqInstanceName]; |
| 576 | entry.interfaceName = fqInstanceName; |
| 577 | entry.transport = mode; |
| 578 | |
| 579 | status |= fetchBinderizedEntry(manager, &entry); |
| 580 | } |
| 581 | |
| 582 | for (auto& pair : allTableEntries) { |
| 583 | putEntry(HWSERVICEMANAGER_LIST, std::move(pair.second)); |
| 584 | } |
| 585 | return status; |
| 586 | } |
| 587 | |
| 588 | Status ListCommand::fetchBinderizedEntry(const sp<IServiceManager> &manager, |
| 589 | TableEntry *entry) { |
| 590 | Status status = OK; |
| 591 | const auto handleError = [&](Status additionalError, const std::string& msg) { |
| 592 | err() << "Warning: Skipping \"" << entry->interfaceName << "\": " << msg << std::endl; |
| 593 | status |= DUMP_BINDERIZED_ERROR | additionalError; |
| 594 | }; |
| 595 | |
| 596 | const auto pair = splitFirst(entry->interfaceName, '/'); |
| 597 | const auto &serviceName = pair.first; |
| 598 | const auto &instanceName = pair.second; |
| 599 | auto getRet = timeoutIPC(manager, &IServiceManager::get, serviceName, instanceName); |
| 600 | if (!getRet.isOk()) { |
| 601 | handleError(TRANSACTION_ERROR, |
| 602 | "cannot be fetched from service manager:" + getRet.description()); |
| 603 | return status; |
| 604 | } |
| 605 | sp<IBase> service = getRet; |
| 606 | if (service == nullptr) { |
| 607 | handleError(NO_INTERFACE, "cannot be fetched from service manager (null)"); |
| 608 | return status; |
| 609 | } |
| 610 | |
| 611 | // getDebugInfo |
| 612 | do { |
| 613 | DebugInfo debugInfo; |
| 614 | auto debugRet = timeoutIPC(service, &IBase::getDebugInfo, [&] (const auto &received) { |
| 615 | debugInfo = received; |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 616 | }); |
| 617 | if (!debugRet.isOk()) { |
Yifan Hong | 22ea7b8 | 2017-09-14 18:07:43 -0700 | [diff] [blame] | 618 | handleError(TRANSACTION_ERROR, |
| 619 | "debugging information cannot be retrieved: " + debugRet.description()); |
| 620 | break; // skip getPidInfo |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 621 | } |
Steven Moreland | d8e2019 | 2017-05-24 11:23:08 -0700 | [diff] [blame] | 622 | |
Yifan Hong | 22ea7b8 | 2017-09-14 18:07:43 -0700 | [diff] [blame] | 623 | entry->serverPid = debugInfo.pid; |
| 624 | entry->serverObjectAddress = debugInfo.ptr; |
| 625 | entry->arch = fromBaseArchitecture(debugInfo.arch); |
Steven Moreland | d8e2019 | 2017-05-24 11:23:08 -0700 | [diff] [blame] | 626 | |
Yifan Hong | 22ea7b8 | 2017-09-14 18:07:43 -0700 | [diff] [blame] | 627 | if (debugInfo.pid != NO_PID) { |
| 628 | const PidInfo* pidInfo = getPidInfoCached(debugInfo.pid); |
| 629 | if (pidInfo == nullptr) { |
| 630 | handleError(IO_ERROR, |
| 631 | "no information for PID " + std::to_string(debugInfo.pid) + |
| 632 | ", are you root?"); |
| 633 | break; |
| 634 | } |
| 635 | if (debugInfo.ptr != NO_PTR) { |
| 636 | auto it = pidInfo->refPids.find(debugInfo.ptr); |
| 637 | if (it != pidInfo->refPids.end()) { |
| 638 | entry->clientPids = it->second; |
| 639 | } |
| 640 | } |
| 641 | entry->threadUsage = pidInfo->threadUsage; |
| 642 | entry->threadCount = pidInfo->threadCount; |
| 643 | } |
| 644 | } while (0); |
Yifan Hong | fee209d | 2017-09-14 18:23:38 -0700 | [diff] [blame] | 645 | |
| 646 | // hash |
| 647 | do { |
| 648 | ssize_t hashIndex = -1; |
| 649 | auto ifaceChainRet = timeoutIPC(service, &IBase::interfaceChain, [&] (const auto& c) { |
| 650 | for (size_t i = 0; i < c.size(); ++i) { |
| 651 | if (serviceName == c[i]) { |
| 652 | hashIndex = static_cast<ssize_t>(i); |
| 653 | break; |
| 654 | } |
| 655 | } |
| 656 | }); |
| 657 | if (!ifaceChainRet.isOk()) { |
| 658 | handleError(TRANSACTION_ERROR, |
| 659 | "interfaceChain fails: " + ifaceChainRet.description()); |
| 660 | break; // skip getHashChain |
| 661 | } |
| 662 | if (hashIndex < 0) { |
| 663 | handleError(BAD_IMPL, "Interface name does not exist in interfaceChain."); |
| 664 | break; // skip getHashChain |
| 665 | } |
| 666 | auto hashRet = timeoutIPC(service, &IBase::getHashChain, [&] (const auto& hashChain) { |
| 667 | if (static_cast<size_t>(hashIndex) >= hashChain.size()) { |
| 668 | handleError(BAD_IMPL, |
| 669 | "interfaceChain indicates position " + std::to_string(hashIndex) + |
| 670 | " but getHashChain returns " + std::to_string(hashChain.size()) + |
| 671 | " hashes"); |
| 672 | return; |
| 673 | } |
| 674 | |
| 675 | auto&& hashArray = hashChain[hashIndex]; |
| 676 | std::vector<uint8_t> hashVec{hashArray.data(), hashArray.data() + hashArray.size()}; |
| 677 | entry->hash = Hash::hexString(hashVec); |
| 678 | }); |
| 679 | if (!hashRet.isOk()) { |
| 680 | handleError(TRANSACTION_ERROR, "getHashChain failed: " + hashRet.description()); |
| 681 | } |
| 682 | } while (0); |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 683 | return status; |
| 684 | } |
| 685 | |
| 686 | Status ListCommand::fetch() { |
| 687 | Status status = OK; |
Yifan Hong | 9881df9 | 2017-05-10 14:33:05 -0700 | [diff] [blame] | 688 | auto bManager = mLshal.serviceManager(); |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 689 | if (bManager == nullptr) { |
Yifan Hong | 76ac14a | 2017-09-08 14:59:04 -0700 | [diff] [blame] | 690 | err() << "Failed to get defaultServiceManager()!" << std::endl; |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 691 | status |= NO_BINDERIZED_MANAGER; |
| 692 | } else { |
| 693 | status |= fetchBinderized(bManager); |
| 694 | // Passthrough PIDs are registered to the binderized manager as well. |
| 695 | status |= fetchPassthrough(bManager); |
| 696 | } |
| 697 | |
Yifan Hong | 9881df9 | 2017-05-10 14:33:05 -0700 | [diff] [blame] | 698 | auto pManager = mLshal.passthroughManager(); |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 699 | if (pManager == nullptr) { |
Yifan Hong | 76ac14a | 2017-09-08 14:59:04 -0700 | [diff] [blame] | 700 | err() << "Failed to get getPassthroughServiceManager()!" << std::endl; |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 701 | status |= NO_PASSTHROUGH_MANAGER; |
| 702 | } else { |
| 703 | status |= fetchAllLibraries(pManager); |
| 704 | } |
| 705 | return status; |
| 706 | } |
| 707 | |
Yifan Hong | a6b93f0 | 2017-09-13 16:53:37 -0700 | [diff] [blame] | 708 | void ListCommand::registerAllOptions() { |
| 709 | int v = mOptions.size(); |
| 710 | // A list of acceptable command line options |
| 711 | // key: value returned by getopt_long |
| 712 | // long options with short alternatives |
| 713 | mOptions.push_back({'h', "help", no_argument, v++, [](ListCommand*, const char*) { |
| 714 | return USAGE; |
| 715 | }, ""}); |
| 716 | mOptions.push_back({'i', "interface", no_argument, v++, [](ListCommand* thiz, const char*) { |
| 717 | thiz->mSelectedColumns.push_back(TableColumnType::INTERFACE_NAME); |
| 718 | return OK; |
| 719 | }, "print the instance name column"}); |
Yifan Hong | fee209d | 2017-09-14 18:23:38 -0700 | [diff] [blame] | 720 | mOptions.push_back({'l', "released", no_argument, v++, [](ListCommand* thiz, const char*) { |
| 721 | thiz->mSelectedColumns.push_back(TableColumnType::RELEASED); |
| 722 | return OK; |
| 723 | }, "print the 'is released?' column\n(Y=released, empty=unreleased or unknown)"}); |
Yifan Hong | a6b93f0 | 2017-09-13 16:53:37 -0700 | [diff] [blame] | 724 | mOptions.push_back({'t', "transport", no_argument, v++, [](ListCommand* thiz, const char*) { |
| 725 | thiz->mSelectedColumns.push_back(TableColumnType::TRANSPORT); |
| 726 | return OK; |
| 727 | }, "print the transport mode column"}); |
| 728 | mOptions.push_back({'r', "arch", no_argument, v++, [](ListCommand* thiz, const char*) { |
| 729 | thiz->mSelectedColumns.push_back(TableColumnType::ARCH); |
| 730 | return OK; |
| 731 | }, "print the bitness column"}); |
Yifan Hong | fee209d | 2017-09-14 18:23:38 -0700 | [diff] [blame] | 732 | mOptions.push_back({'s', "hash", no_argument, v++, [](ListCommand* thiz, const char*) { |
| 733 | thiz->mSelectedColumns.push_back(TableColumnType::HASH); |
| 734 | return OK; |
| 735 | }, "print hash of the interface"}); |
Yifan Hong | a6b93f0 | 2017-09-13 16:53:37 -0700 | [diff] [blame] | 736 | mOptions.push_back({'p', "pid", no_argument, v++, [](ListCommand* thiz, const char*) { |
| 737 | thiz->mSelectedColumns.push_back(TableColumnType::SERVER_PID); |
| 738 | return OK; |
| 739 | }, "print the server PID, or server cmdline if -m is set"}); |
| 740 | mOptions.push_back({'a', "address", no_argument, v++, [](ListCommand* thiz, const char*) { |
| 741 | thiz->mSelectedColumns.push_back(TableColumnType::SERVER_ADDR); |
| 742 | return OK; |
| 743 | }, "print the server object address column"}); |
| 744 | mOptions.push_back({'c', "clients", no_argument, v++, [](ListCommand* thiz, const char*) { |
| 745 | thiz->mSelectedColumns.push_back(TableColumnType::CLIENT_PIDS); |
| 746 | return OK; |
| 747 | }, "print the client PIDs, or client cmdlines if -m is set"}); |
| 748 | mOptions.push_back({'e', "threads", no_argument, v++, [](ListCommand* thiz, const char*) { |
| 749 | thiz->mSelectedColumns.push_back(TableColumnType::THREADS); |
| 750 | return OK; |
| 751 | }, "print currently used/available threads\n(note, available threads created lazily)"}); |
| 752 | mOptions.push_back({'m', "cmdline", no_argument, v++, [](ListCommand* thiz, const char*) { |
| 753 | thiz->mEnableCmdlines = true; |
| 754 | return OK; |
| 755 | }, "print cmdline instead of PIDs"}); |
| 756 | mOptions.push_back({'d', "debug", optional_argument, v++, [](ListCommand* thiz, const char* arg) { |
| 757 | thiz->mEmitDebugInfo = true; |
| 758 | if (arg) thiz->mFileOutputPath = arg; |
| 759 | return OK; |
| 760 | }, "Emit debug info from\nIBase::debug with empty options. Cannot be used with --neat.\n" |
| 761 | "Writes to specified file if 'arg' is provided, otherwise stdout."}); |
| 762 | |
| 763 | // long options without short alternatives |
| 764 | mOptions.push_back({'\0', "init-vintf", no_argument, v++, [](ListCommand* thiz, const char* arg) { |
| 765 | thiz->mVintf = true; |
Yifan Hong | f31aa05 | 2018-02-02 15:17:51 -0800 | [diff] [blame] | 766 | if (thiz->mVintfPartition == Partition::UNKNOWN) |
| 767 | thiz->mVintfPartition = Partition::VENDOR; |
Yifan Hong | a6b93f0 | 2017-09-13 16:53:37 -0700 | [diff] [blame] | 768 | if (arg) thiz->mFileOutputPath = arg; |
| 769 | return OK; |
| 770 | }, "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] | 771 | mOptions.push_back({'\0', "init-vintf-partition", required_argument, v++, [](ListCommand* thiz, const char* arg) { |
| 772 | if (!arg) return USAGE; |
| 773 | thiz->mVintfPartition = android::procpartition::parsePartition(arg); |
| 774 | if (thiz->mVintfPartition == Partition::UNKNOWN) return USAGE; |
| 775 | return OK; |
| 776 | }, "Specify the partition of the HAL manifest\ngenerated by --init-vintf.\n" |
| 777 | "Valid values are 'system', 'vendor', and 'odm'. Default is 'vendor'."}); |
Yifan Hong | a6b93f0 | 2017-09-13 16:53:37 -0700 | [diff] [blame] | 778 | mOptions.push_back({'\0', "sort", required_argument, v++, [](ListCommand* thiz, const char* arg) { |
| 779 | if (strcmp(arg, "interface") == 0 || strcmp(arg, "i") == 0) { |
| 780 | thiz->mSortColumn = TableEntry::sortByInterfaceName; |
| 781 | } else if (strcmp(arg, "pid") == 0 || strcmp(arg, "p") == 0) { |
| 782 | thiz->mSortColumn = TableEntry::sortByServerPid; |
| 783 | } else { |
| 784 | thiz->err() << "Unrecognized sorting column: " << arg << std::endl; |
| 785 | return USAGE; |
| 786 | } |
| 787 | return OK; |
| 788 | }, "sort by a column. 'arg' can be (i|interface) or (p|pid)."}); |
| 789 | mOptions.push_back({'\0', "neat", no_argument, v++, [](ListCommand* thiz, const char*) { |
| 790 | thiz->mNeat = true; |
| 791 | return OK; |
| 792 | }, "output is machine parsable (no explanatory text).\nCannot be used with --debug."}); |
| 793 | } |
| 794 | |
| 795 | // Create 'longopts' argument to getopt_long. Caller is responsible for maintaining |
| 796 | // the lifetime of "options" during the usage of the returned array. |
| 797 | static std::unique_ptr<struct option[]> getLongOptions( |
| 798 | const ListCommand::RegisteredOptions& options, |
| 799 | int* longOptFlag) { |
| 800 | std::unique_ptr<struct option[]> ret{new struct option[options.size() + 1]}; |
| 801 | int i = 0; |
| 802 | for (const auto& e : options) { |
| 803 | ret[i].name = e.longOption.c_str(); |
| 804 | ret[i].has_arg = e.hasArg; |
| 805 | ret[i].flag = longOptFlag; |
| 806 | ret[i].val = e.val; |
| 807 | |
| 808 | i++; |
| 809 | } |
| 810 | // getopt_long last option has all zeros |
| 811 | ret[i].name = NULL; |
| 812 | ret[i].has_arg = 0; |
| 813 | ret[i].flag = NULL; |
| 814 | ret[i].val = 0; |
| 815 | |
| 816 | return ret; |
| 817 | } |
| 818 | |
| 819 | // Create 'optstring' argument to getopt_long. |
| 820 | static std::string getShortOptions(const ListCommand::RegisteredOptions& options) { |
| 821 | std::stringstream ss; |
| 822 | for (const auto& e : options) { |
| 823 | if (e.shortOption != '\0') { |
| 824 | ss << e.shortOption; |
| 825 | } |
| 826 | } |
| 827 | return ss.str(); |
| 828 | } |
| 829 | |
Yifan Hong | a8bedc6 | 2017-09-08 18:00:31 -0700 | [diff] [blame] | 830 | Status ListCommand::parseArgs(const Arg &arg) { |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 831 | |
Yifan Hong | a6b93f0 | 2017-09-13 16:53:37 -0700 | [diff] [blame] | 832 | if (mOptions.empty()) { |
| 833 | registerAllOptions(); |
| 834 | } |
| 835 | int longOptFlag; |
| 836 | std::unique_ptr<struct option[]> longOptions = getLongOptions(mOptions, &longOptFlag); |
| 837 | std::string shortOptions = getShortOptions(mOptions); |
Yifan Hong | d4a77e8 | 2017-09-06 19:40:24 -0700 | [diff] [blame] | 838 | |
Yifan Hong | a8bedc6 | 2017-09-08 18:00:31 -0700 | [diff] [blame] | 839 | // suppress output to std::err for unknown options |
| 840 | opterr = 0; |
| 841 | |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 842 | int optionIndex; |
| 843 | int c; |
| 844 | // Lshal::parseArgs has set optind to the next option to parse |
| 845 | for (;;) { |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 846 | c = getopt_long(arg.argc, arg.argv, |
Yifan Hong | a6b93f0 | 2017-09-13 16:53:37 -0700 | [diff] [blame] | 847 | shortOptions.c_str(), longOptions.get(), &optionIndex); |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 848 | if (c == -1) { |
| 849 | break; |
| 850 | } |
Yifan Hong | a6b93f0 | 2017-09-13 16:53:37 -0700 | [diff] [blame] | 851 | const RegisteredOption* found = nullptr; |
| 852 | if (c == 0) { |
| 853 | // see long option |
| 854 | for (const auto& e : mOptions) { |
| 855 | if (longOptFlag == e.val) found = &e; |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 856 | } |
Yifan Hong | a6b93f0 | 2017-09-13 16:53:37 -0700 | [diff] [blame] | 857 | } else { |
| 858 | // see short option |
| 859 | for (const auto& e : mOptions) { |
| 860 | if (c == e.shortOption) found = &e; |
| 861 | } |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 862 | } |
Yifan Hong | a6b93f0 | 2017-09-13 16:53:37 -0700 | [diff] [blame] | 863 | |
| 864 | if (found == nullptr) { |
| 865 | // see unrecognized options |
Yifan Hong | a8bedc6 | 2017-09-08 18:00:31 -0700 | [diff] [blame] | 866 | err() << "unrecognized option `" << arg.argv[optind - 1] << "'" << std::endl; |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 867 | return USAGE; |
| 868 | } |
Yifan Hong | a6b93f0 | 2017-09-13 16:53:37 -0700 | [diff] [blame] | 869 | |
| 870 | Status status = found->op(this, optarg); |
| 871 | if (status != OK) { |
| 872 | return status; |
| 873 | } |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 874 | } |
| 875 | if (optind < arg.argc) { |
| 876 | // see non option |
Yifan Hong | a8bedc6 | 2017-09-08 18:00:31 -0700 | [diff] [blame] | 877 | err() << "unrecognized option `" << arg.argv[optind] << "'" << std::endl; |
Yifan Hong | 1bc1e9f | 2017-08-29 17:28:12 -0700 | [diff] [blame] | 878 | return USAGE; |
| 879 | } |
| 880 | |
| 881 | if (mNeat && mEmitDebugInfo) { |
Yifan Hong | 76ac14a | 2017-09-08 14:59:04 -0700 | [diff] [blame] | 882 | err() << "Error: --neat should not be used with --debug." << std::endl; |
Yifan Hong | 1bc1e9f | 2017-08-29 17:28:12 -0700 | [diff] [blame] | 883 | return USAGE; |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 884 | } |
| 885 | |
Yifan Hong | a6b93f0 | 2017-09-13 16:53:37 -0700 | [diff] [blame] | 886 | if (mSelectedColumns.empty()) { |
Yifan Hong | fee209d | 2017-09-14 18:23:38 -0700 | [diff] [blame] | 887 | mSelectedColumns = {TableColumnType::RELEASED, |
| 888 | TableColumnType::INTERFACE_NAME, TableColumnType::THREADS, |
Yifan Hong | 05494a5 | 2017-08-29 18:50:00 -0700 | [diff] [blame] | 889 | TableColumnType::SERVER_PID, TableColumnType::CLIENT_PIDS}; |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 890 | } |
Yifan Hong | d4a77e8 | 2017-09-06 19:40:24 -0700 | [diff] [blame] | 891 | |
Yifan Hong | a6b93f0 | 2017-09-13 16:53:37 -0700 | [diff] [blame] | 892 | if (mEnableCmdlines) { |
| 893 | for (size_t i = 0; i < mSelectedColumns.size(); ++i) { |
| 894 | if (mSelectedColumns[i] == TableColumnType::SERVER_PID) { |
| 895 | mSelectedColumns[i] = TableColumnType::SERVER_CMD; |
Yifan Hong | d4a77e8 | 2017-09-06 19:40:24 -0700 | [diff] [blame] | 896 | } |
Yifan Hong | a6b93f0 | 2017-09-13 16:53:37 -0700 | [diff] [blame] | 897 | if (mSelectedColumns[i] == TableColumnType::CLIENT_PIDS) { |
| 898 | mSelectedColumns[i] = TableColumnType::CLIENT_CMDS; |
Yifan Hong | d4a77e8 | 2017-09-06 19:40:24 -0700 | [diff] [blame] | 899 | } |
| 900 | } |
| 901 | } |
| 902 | |
Yifan Hong | a6b93f0 | 2017-09-13 16:53:37 -0700 | [diff] [blame] | 903 | forEachTable([this] (Table& table) { |
| 904 | table.setSelectedColumns(this->mSelectedColumns); |
Yifan Hong | d4a77e8 | 2017-09-06 19:40:24 -0700 | [diff] [blame] | 905 | }); |
| 906 | |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 907 | return OK; |
| 908 | } |
| 909 | |
Yifan Hong | a8bedc6 | 2017-09-08 18:00:31 -0700 | [diff] [blame] | 910 | Status ListCommand::main(const Arg &arg) { |
| 911 | Status status = parseArgs(arg); |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 912 | if (status != OK) { |
| 913 | return status; |
| 914 | } |
| 915 | status = fetch(); |
| 916 | postprocess(); |
Yifan Hong | ca3b660 | 2017-09-07 16:44:27 -0700 | [diff] [blame] | 917 | status |= dump(); |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 918 | return status; |
| 919 | } |
| 920 | |
Yifan Hong | a6b93f0 | 2017-09-13 16:53:37 -0700 | [diff] [blame] | 921 | static std::vector<std::string> splitString(const std::string &s, char c) { |
| 922 | std::vector<std::string> components; |
| 923 | |
| 924 | size_t startPos = 0; |
| 925 | size_t matchPos; |
| 926 | while ((matchPos = s.find(c, startPos)) != std::string::npos) { |
| 927 | components.push_back(s.substr(startPos, matchPos - startPos)); |
| 928 | startPos = matchPos + 1; |
| 929 | } |
| 930 | |
| 931 | if (startPos <= s.length()) { |
| 932 | components.push_back(s.substr(startPos)); |
| 933 | } |
| 934 | return components; |
| 935 | } |
| 936 | |
| 937 | const std::string& ListCommand::RegisteredOption::getHelpMessageForArgument() const { |
| 938 | static const std::string empty{}; |
| 939 | static const std::string optional{"[=<arg>]"}; |
| 940 | static const std::string required{"=<arg>"}; |
| 941 | |
| 942 | if (hasArg == optional_argument) { |
| 943 | return optional; |
| 944 | } |
| 945 | if (hasArg == required_argument) { |
| 946 | return required; |
| 947 | } |
| 948 | return empty; |
| 949 | } |
| 950 | |
Yifan Hong | a8bedc6 | 2017-09-08 18:00:31 -0700 | [diff] [blame] | 951 | void ListCommand::usage() const { |
| 952 | |
Yifan Hong | a6b93f0 | 2017-09-13 16:53:37 -0700 | [diff] [blame] | 953 | err() << "list:" << std::endl |
| 954 | << " lshal" << std::endl |
| 955 | << " lshal list" << std::endl |
Yifan Hong | fee209d | 2017-09-14 18:23:38 -0700 | [diff] [blame] | 956 | << " List all hals with default ordering and columns (`lshal list -riepc`)" << std::endl |
Yifan Hong | a6b93f0 | 2017-09-13 16:53:37 -0700 | [diff] [blame] | 957 | << " lshal list [-h|--help]" << std::endl |
| 958 | << " -h, --help: Print help message for list (`lshal help list`)" << std::endl |
| 959 | << " lshal [list] [OPTIONS...]" << std::endl; |
| 960 | for (const auto& e : mOptions) { |
| 961 | if (e.help.empty()) { |
| 962 | continue; |
| 963 | } |
| 964 | err() << " "; |
| 965 | if (e.shortOption != '\0') |
| 966 | err() << "-" << e.shortOption << e.getHelpMessageForArgument(); |
| 967 | if (e.shortOption != '\0' && !e.longOption.empty()) |
| 968 | err() << ", "; |
| 969 | if (!e.longOption.empty()) |
| 970 | err() << "--" << e.longOption << e.getHelpMessageForArgument(); |
| 971 | err() << ": "; |
| 972 | std::vector<std::string> lines = splitString(e.help, '\n'); |
| 973 | for (const auto& line : lines) { |
| 974 | if (&line != &lines.front()) |
| 975 | err() << " "; |
| 976 | err() << line << std::endl; |
| 977 | } |
| 978 | } |
Yifan Hong | a8bedc6 | 2017-09-08 18:00:31 -0700 | [diff] [blame] | 979 | } |
| 980 | |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 981 | } // namespace lshal |
| 982 | } // namespace android |
Yifan Hong | 05494a5 | 2017-08-29 18:50:00 -0700 | [diff] [blame] | 983 | |