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