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