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 |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 268 | << "-->" << std::endl; |
| 269 | |
| 270 | vintf::HalManifest manifest; |
| 271 | forEachTable([this, &manifest] (const Table &table) { |
| 272 | for (const TableEntry &entry : table) { |
| 273 | |
| 274 | std::string fqInstanceName = entry.interfaceName; |
| 275 | |
| 276 | if (&table == &mImplementationsTable) { |
| 277 | // Quick hack to work around *'s |
| 278 | replaceAll(&fqInstanceName, '*', 'D'); |
| 279 | } |
| 280 | auto splittedFqInstanceName = splitFirst(fqInstanceName, '/'); |
| 281 | FQName fqName(splittedFqInstanceName.first); |
| 282 | if (!fqName.isValid()) { |
Yifan Hong | 76ac14a | 2017-09-08 14:59:04 -0700 | [diff] [blame] | 283 | err() << "Warning: '" << splittedFqInstanceName.first |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 284 | << "' is not a valid FQName." << std::endl; |
| 285 | continue; |
| 286 | } |
| 287 | // Strip out system libs. |
| 288 | if (fqName.inPackage("android.hidl") || |
| 289 | fqName.inPackage("android.frameworks") || |
| 290 | fqName.inPackage("android.system")) { |
| 291 | continue; |
| 292 | } |
| 293 | std::string interfaceName = |
| 294 | &table == &mImplementationsTable ? "" : fqName.name(); |
| 295 | std::string instanceName = |
| 296 | &table == &mImplementationsTable ? "" : splittedFqInstanceName.second; |
| 297 | |
| 298 | vintf::Version version{fqName.getPackageMajorVersion(), |
| 299 | fqName.getPackageMinorVersion()}; |
| 300 | vintf::Transport transport; |
| 301 | vintf::Arch arch; |
| 302 | if (entry.transport == "hwbinder") { |
| 303 | transport = vintf::Transport::HWBINDER; |
| 304 | arch = vintf::Arch::ARCH_EMPTY; |
| 305 | } else if (entry.transport == "passthrough") { |
| 306 | transport = vintf::Transport::PASSTHROUGH; |
| 307 | switch (entry.arch) { |
| 308 | case lshal::ARCH32: |
| 309 | arch = vintf::Arch::ARCH_32; break; |
| 310 | case lshal::ARCH64: |
| 311 | arch = vintf::Arch::ARCH_64; break; |
| 312 | case lshal::ARCH_BOTH: |
| 313 | arch = vintf::Arch::ARCH_32_64; break; |
| 314 | case lshal::ARCH_UNKNOWN: // fallthrough |
| 315 | default: |
Yifan Hong | 76ac14a | 2017-09-08 14:59:04 -0700 | [diff] [blame] | 316 | err() << "Warning: '" << fqName.package() |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 317 | << "' doesn't have bitness info, assuming 32+64." << std::endl; |
| 318 | arch = vintf::Arch::ARCH_32_64; |
| 319 | } |
| 320 | } else { |
Yifan Hong | 76ac14a | 2017-09-08 14:59:04 -0700 | [diff] [blame] | 321 | err() << "Warning: '" << entry.transport << "' is not a valid transport." << std::endl; |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 322 | continue; |
| 323 | } |
| 324 | |
| 325 | bool done = false; |
| 326 | for (vintf::ManifestHal *hal : manifest.getHals(fqName.package())) { |
| 327 | if (hal->transport() != transport) { |
| 328 | if (transport != vintf::Transport::PASSTHROUGH) { |
Yifan Hong | 76ac14a | 2017-09-08 14:59:04 -0700 | [diff] [blame] | 329 | err() << "Fatal: should not reach here. Generated result may be wrong for '" |
Yifan Hong | 236301c | 2017-06-19 12:27:08 -0700 | [diff] [blame] | 330 | << hal->name << "'." |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 331 | << std::endl; |
| 332 | } |
| 333 | done = true; |
| 334 | break; |
| 335 | } |
Yifan Hong | 77c8782 | 2017-06-19 15:47:39 -0700 | [diff] [blame] | 336 | if (findAndBumpVersion(hal, version)) { |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 337 | if (&table != &mImplementationsTable) { |
| 338 | hal->interfaces[interfaceName].name = interfaceName; |
| 339 | hal->interfaces[interfaceName].instances.insert(instanceName); |
| 340 | } |
Yifan Hong | 236301c | 2017-06-19 12:27:08 -0700 | [diff] [blame] | 341 | hal->transportArch.arch |= arch; |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 342 | done = true; |
| 343 | break; |
| 344 | } |
| 345 | } |
| 346 | if (done) { |
| 347 | continue; // to next TableEntry |
| 348 | } |
| 349 | decltype(vintf::ManifestHal::interfaces) interfaces; |
| 350 | if (&table != &mImplementationsTable) { |
| 351 | interfaces[interfaceName].name = interfaceName; |
| 352 | interfaces[interfaceName].instances.insert(instanceName); |
| 353 | } |
| 354 | if (!manifest.add(vintf::ManifestHal{ |
| 355 | .format = vintf::HalFormat::HIDL, |
| 356 | .name = fqName.package(), |
| 357 | .versions = {version}, |
| 358 | .transportArch = {transport, arch}, |
| 359 | .interfaces = interfaces})) { |
Yifan Hong | 76ac14a | 2017-09-08 14:59:04 -0700 | [diff] [blame] | 360 | err() << "Warning: cannot add hal '" << fqInstanceName << "'" << std::endl; |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 361 | } |
| 362 | } |
| 363 | }); |
Yifan Hong | acc1212 | 2018-01-25 10:49:39 -0800 | [diff] [blame^] | 364 | out << vintf::gHalManifestConverter(manifest, vintf::SerializeFlag::HALS_ONLY); |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 365 | } |
| 366 | |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 367 | static Architecture fromBaseArchitecture(::android::hidl::base::V1_0::DebugInfo::Architecture a) { |
| 368 | switch (a) { |
| 369 | case ::android::hidl::base::V1_0::DebugInfo::Architecture::IS_64BIT: |
| 370 | return ARCH64; |
| 371 | case ::android::hidl::base::V1_0::DebugInfo::Architecture::IS_32BIT: |
| 372 | return ARCH32; |
| 373 | case ::android::hidl::base::V1_0::DebugInfo::Architecture::UNKNOWN: // fallthrough |
| 374 | default: |
| 375 | return ARCH_UNKNOWN; |
| 376 | } |
| 377 | } |
| 378 | |
Yifan Hong | ca3b660 | 2017-09-07 16:44:27 -0700 | [diff] [blame] | 379 | void ListCommand::dumpTable(const NullableOStream<std::ostream>& out) const { |
Yifan Hong | 1bc1e9f | 2017-08-29 17:28:12 -0700 | [diff] [blame] | 380 | if (mNeat) { |
Yifan Hong | d4a77e8 | 2017-09-06 19:40:24 -0700 | [diff] [blame] | 381 | MergedTable({&mServicesTable, &mPassthroughRefTable, &mImplementationsTable}) |
Yifan Hong | ca3b660 | 2017-09-07 16:44:27 -0700 | [diff] [blame] | 382 | .createTextTable().dump(out.buf()); |
Yifan Hong | 1bc1e9f | 2017-08-29 17:28:12 -0700 | [diff] [blame] | 383 | return; |
| 384 | } |
| 385 | |
Yifan Hong | ca3b660 | 2017-09-07 16:44:27 -0700 | [diff] [blame] | 386 | forEachTable([this, &out](const Table &table) { |
Yifan Hong | 1bc1e9f | 2017-08-29 17:28:12 -0700 | [diff] [blame] | 387 | |
Yifan Hong | d4a77e8 | 2017-09-06 19:40:24 -0700 | [diff] [blame] | 388 | // We're only interested in dumping debug info for already |
| 389 | // instantiated services. There's little value in dumping the |
| 390 | // debug info for a service we create on the fly, so we only operate |
| 391 | // on the "mServicesTable". |
| 392 | std::function<std::string(const std::string&)> emitDebugInfo = nullptr; |
| 393 | if (mEmitDebugInfo && &table == &mServicesTable) { |
| 394 | emitDebugInfo = [this](const auto& iName) { |
Yifan Hong | ca3b660 | 2017-09-07 16:44:27 -0700 | [diff] [blame] | 395 | std::stringstream ss; |
Yifan Hong | d4a77e8 | 2017-09-06 19:40:24 -0700 | [diff] [blame] | 396 | auto pair = splitFirst(iName, '/'); |
Yifan Hong | ca3b660 | 2017-09-07 16:44:27 -0700 | [diff] [blame] | 397 | mLshal.emitDebugInfo(pair.first, pair.second, {}, ss, |
Yifan Hong | 1bc1e9f | 2017-08-29 17:28:12 -0700 | [diff] [blame] | 398 | NullableOStream<std::ostream>(nullptr)); |
Yifan Hong | ca3b660 | 2017-09-07 16:44:27 -0700 | [diff] [blame] | 399 | return ss.str(); |
Yifan Hong | d4a77e8 | 2017-09-06 19:40:24 -0700 | [diff] [blame] | 400 | }; |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 401 | } |
Yifan Hong | ca3b660 | 2017-09-07 16:44:27 -0700 | [diff] [blame] | 402 | table.createTextTable(mNeat, emitDebugInfo).dump(out.buf()); |
| 403 | out << std::endl; |
Yifan Hong | 1bc1e9f | 2017-08-29 17:28:12 -0700 | [diff] [blame] | 404 | }); |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 405 | } |
| 406 | |
Yifan Hong | ca3b660 | 2017-09-07 16:44:27 -0700 | [diff] [blame] | 407 | Status ListCommand::dump() { |
| 408 | auto dump = mVintf ? &ListCommand::dumpVintf : &ListCommand::dumpTable; |
| 409 | |
| 410 | if (mFileOutputPath.empty()) { |
| 411 | (*this.*dump)(out()); |
| 412 | return OK; |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 413 | } |
Yifan Hong | ca3b660 | 2017-09-07 16:44:27 -0700 | [diff] [blame] | 414 | |
| 415 | std::ofstream fileOutput(mFileOutputPath); |
| 416 | if (!fileOutput.is_open()) { |
| 417 | err() << "Could not open file '" << mFileOutputPath << "'." << std::endl; |
| 418 | return IO_ERROR; |
| 419 | } |
| 420 | chown(mFileOutputPath.c_str(), AID_SHELL, AID_SHELL); |
| 421 | |
| 422 | (*this.*dump)(NullableOStream<std::ostream>(fileOutput)); |
| 423 | |
| 424 | fileOutput.flush(); |
| 425 | fileOutput.close(); |
| 426 | return OK; |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | void ListCommand::putEntry(TableEntrySource source, TableEntry &&entry) { |
| 430 | Table *table = nullptr; |
| 431 | switch (source) { |
| 432 | case HWSERVICEMANAGER_LIST : |
| 433 | table = &mServicesTable; break; |
| 434 | case PTSERVICEMANAGER_REG_CLIENT : |
| 435 | table = &mPassthroughRefTable; break; |
| 436 | case LIST_DLLIB : |
| 437 | table = &mImplementationsTable; break; |
| 438 | default: |
Yifan Hong | 76ac14a | 2017-09-08 14:59:04 -0700 | [diff] [blame] | 439 | err() << "Error: Unknown source of entry " << source << std::endl; |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 440 | } |
| 441 | if (table) { |
Yifan Hong | d4a77e8 | 2017-09-06 19:40:24 -0700 | [diff] [blame] | 442 | table->add(std::forward<TableEntry>(entry)); |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 443 | } |
| 444 | } |
| 445 | |
| 446 | Status ListCommand::fetchAllLibraries(const sp<IServiceManager> &manager) { |
| 447 | using namespace ::android::hardware; |
| 448 | using namespace ::android::hidl::manager::V1_0; |
| 449 | using namespace ::android::hidl::base::V1_0; |
Yifan Hong | f2d557b | 2017-05-24 19:45:02 -0700 | [diff] [blame] | 450 | using std::literals::chrono_literals::operator""s; |
| 451 | auto ret = timeoutIPC(2s, manager, &IServiceManager::debugDump, [&] (const auto &infos) { |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 452 | std::map<std::string, TableEntry> entries; |
| 453 | for (const auto &info : infos) { |
| 454 | std::string interfaceName = std::string{info.interfaceName.c_str()} + "/" + |
| 455 | std::string{info.instanceName.c_str()}; |
| 456 | entries.emplace(interfaceName, TableEntry{ |
| 457 | .interfaceName = interfaceName, |
| 458 | .transport = "passthrough", |
Yifan Hong | f2d557b | 2017-05-24 19:45:02 -0700 | [diff] [blame] | 459 | .clientPids = info.clientPids, |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 460 | }).first->second.arch |= fromBaseArchitecture(info.arch); |
| 461 | } |
| 462 | for (auto &&pair : entries) { |
| 463 | putEntry(LIST_DLLIB, std::move(pair.second)); |
| 464 | } |
| 465 | }); |
| 466 | if (!ret.isOk()) { |
Yifan Hong | 76ac14a | 2017-09-08 14:59:04 -0700 | [diff] [blame] | 467 | err() << "Error: Failed to call list on getPassthroughServiceManager(): " |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 468 | << ret.description() << std::endl; |
| 469 | return DUMP_ALL_LIBS_ERROR; |
| 470 | } |
| 471 | return OK; |
| 472 | } |
| 473 | |
| 474 | Status ListCommand::fetchPassthrough(const sp<IServiceManager> &manager) { |
| 475 | using namespace ::android::hardware; |
| 476 | using namespace ::android::hardware::details; |
| 477 | using namespace ::android::hidl::manager::V1_0; |
| 478 | using namespace ::android::hidl::base::V1_0; |
| 479 | auto ret = timeoutIPC(manager, &IServiceManager::debugDump, [&] (const auto &infos) { |
| 480 | for (const auto &info : infos) { |
| 481 | if (info.clientPids.size() <= 0) { |
| 482 | continue; |
| 483 | } |
| 484 | putEntry(PTSERVICEMANAGER_REG_CLIENT, { |
| 485 | .interfaceName = |
| 486 | std::string{info.interfaceName.c_str()} + "/" + |
| 487 | std::string{info.instanceName.c_str()}, |
| 488 | .transport = "passthrough", |
| 489 | .serverPid = info.clientPids.size() == 1 ? info.clientPids[0] : NO_PID, |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 490 | .clientPids = info.clientPids, |
| 491 | .arch = fromBaseArchitecture(info.arch) |
| 492 | }); |
| 493 | } |
| 494 | }); |
| 495 | if (!ret.isOk()) { |
Yifan Hong | 76ac14a | 2017-09-08 14:59:04 -0700 | [diff] [blame] | 496 | err() << "Error: Failed to call debugDump on defaultServiceManager(): " |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 497 | << ret.description() << std::endl; |
| 498 | return DUMP_PASSTHROUGH_ERROR; |
| 499 | } |
| 500 | return OK; |
| 501 | } |
| 502 | |
| 503 | Status ListCommand::fetchBinderized(const sp<IServiceManager> &manager) { |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 504 | const std::string mode = "hwbinder"; |
| 505 | |
| 506 | hidl_vec<hidl_string> fqInstanceNames; |
| 507 | // copying out for timeoutIPC |
| 508 | auto listRet = timeoutIPC(manager, &IServiceManager::list, [&] (const auto &names) { |
| 509 | fqInstanceNames = names; |
| 510 | }); |
| 511 | if (!listRet.isOk()) { |
Yifan Hong | 76ac14a | 2017-09-08 14:59:04 -0700 | [diff] [blame] | 512 | err() << "Error: Failed to list services for " << mode << ": " |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 513 | << listRet.description() << std::endl; |
| 514 | return DUMP_BINDERIZED_ERROR; |
| 515 | } |
| 516 | |
| 517 | Status status = OK; |
Yifan Hong | 22ea7b8 | 2017-09-14 18:07:43 -0700 | [diff] [blame] | 518 | std::map<std::string, TableEntry> allTableEntries; |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 519 | for (const auto &fqInstanceName : fqInstanceNames) { |
Yifan Hong | 22ea7b8 | 2017-09-14 18:07:43 -0700 | [diff] [blame] | 520 | // create entry and default assign all fields. |
| 521 | TableEntry& entry = allTableEntries[fqInstanceName]; |
| 522 | entry.interfaceName = fqInstanceName; |
| 523 | entry.transport = mode; |
| 524 | |
| 525 | status |= fetchBinderizedEntry(manager, &entry); |
| 526 | } |
| 527 | |
| 528 | for (auto& pair : allTableEntries) { |
| 529 | putEntry(HWSERVICEMANAGER_LIST, std::move(pair.second)); |
| 530 | } |
| 531 | return status; |
| 532 | } |
| 533 | |
| 534 | Status ListCommand::fetchBinderizedEntry(const sp<IServiceManager> &manager, |
| 535 | TableEntry *entry) { |
| 536 | Status status = OK; |
| 537 | const auto handleError = [&](Status additionalError, const std::string& msg) { |
| 538 | err() << "Warning: Skipping \"" << entry->interfaceName << "\": " << msg << std::endl; |
| 539 | status |= DUMP_BINDERIZED_ERROR | additionalError; |
| 540 | }; |
| 541 | |
| 542 | const auto pair = splitFirst(entry->interfaceName, '/'); |
| 543 | const auto &serviceName = pair.first; |
| 544 | const auto &instanceName = pair.second; |
| 545 | auto getRet = timeoutIPC(manager, &IServiceManager::get, serviceName, instanceName); |
| 546 | if (!getRet.isOk()) { |
| 547 | handleError(TRANSACTION_ERROR, |
| 548 | "cannot be fetched from service manager:" + getRet.description()); |
| 549 | return status; |
| 550 | } |
| 551 | sp<IBase> service = getRet; |
| 552 | if (service == nullptr) { |
| 553 | handleError(NO_INTERFACE, "cannot be fetched from service manager (null)"); |
| 554 | return status; |
| 555 | } |
| 556 | |
| 557 | // getDebugInfo |
| 558 | do { |
| 559 | DebugInfo debugInfo; |
| 560 | auto debugRet = timeoutIPC(service, &IBase::getDebugInfo, [&] (const auto &received) { |
| 561 | debugInfo = received; |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 562 | }); |
| 563 | if (!debugRet.isOk()) { |
Yifan Hong | 22ea7b8 | 2017-09-14 18:07:43 -0700 | [diff] [blame] | 564 | handleError(TRANSACTION_ERROR, |
| 565 | "debugging information cannot be retrieved: " + debugRet.description()); |
| 566 | break; // skip getPidInfo |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 567 | } |
Steven Moreland | d8e2019 | 2017-05-24 11:23:08 -0700 | [diff] [blame] | 568 | |
Yifan Hong | 22ea7b8 | 2017-09-14 18:07:43 -0700 | [diff] [blame] | 569 | entry->serverPid = debugInfo.pid; |
| 570 | entry->serverObjectAddress = debugInfo.ptr; |
| 571 | entry->arch = fromBaseArchitecture(debugInfo.arch); |
Steven Moreland | d8e2019 | 2017-05-24 11:23:08 -0700 | [diff] [blame] | 572 | |
Yifan Hong | 22ea7b8 | 2017-09-14 18:07:43 -0700 | [diff] [blame] | 573 | if (debugInfo.pid != NO_PID) { |
| 574 | const PidInfo* pidInfo = getPidInfoCached(debugInfo.pid); |
| 575 | if (pidInfo == nullptr) { |
| 576 | handleError(IO_ERROR, |
| 577 | "no information for PID " + std::to_string(debugInfo.pid) + |
| 578 | ", are you root?"); |
| 579 | break; |
| 580 | } |
| 581 | if (debugInfo.ptr != NO_PTR) { |
| 582 | auto it = pidInfo->refPids.find(debugInfo.ptr); |
| 583 | if (it != pidInfo->refPids.end()) { |
| 584 | entry->clientPids = it->second; |
| 585 | } |
| 586 | } |
| 587 | entry->threadUsage = pidInfo->threadUsage; |
| 588 | entry->threadCount = pidInfo->threadCount; |
| 589 | } |
| 590 | } while (0); |
Yifan Hong | fee209d | 2017-09-14 18:23:38 -0700 | [diff] [blame] | 591 | |
| 592 | // hash |
| 593 | do { |
| 594 | ssize_t hashIndex = -1; |
| 595 | auto ifaceChainRet = timeoutIPC(service, &IBase::interfaceChain, [&] (const auto& c) { |
| 596 | for (size_t i = 0; i < c.size(); ++i) { |
| 597 | if (serviceName == c[i]) { |
| 598 | hashIndex = static_cast<ssize_t>(i); |
| 599 | break; |
| 600 | } |
| 601 | } |
| 602 | }); |
| 603 | if (!ifaceChainRet.isOk()) { |
| 604 | handleError(TRANSACTION_ERROR, |
| 605 | "interfaceChain fails: " + ifaceChainRet.description()); |
| 606 | break; // skip getHashChain |
| 607 | } |
| 608 | if (hashIndex < 0) { |
| 609 | handleError(BAD_IMPL, "Interface name does not exist in interfaceChain."); |
| 610 | break; // skip getHashChain |
| 611 | } |
| 612 | auto hashRet = timeoutIPC(service, &IBase::getHashChain, [&] (const auto& hashChain) { |
| 613 | if (static_cast<size_t>(hashIndex) >= hashChain.size()) { |
| 614 | handleError(BAD_IMPL, |
| 615 | "interfaceChain indicates position " + std::to_string(hashIndex) + |
| 616 | " but getHashChain returns " + std::to_string(hashChain.size()) + |
| 617 | " hashes"); |
| 618 | return; |
| 619 | } |
| 620 | |
| 621 | auto&& hashArray = hashChain[hashIndex]; |
| 622 | std::vector<uint8_t> hashVec{hashArray.data(), hashArray.data() + hashArray.size()}; |
| 623 | entry->hash = Hash::hexString(hashVec); |
| 624 | }); |
| 625 | if (!hashRet.isOk()) { |
| 626 | handleError(TRANSACTION_ERROR, "getHashChain failed: " + hashRet.description()); |
| 627 | } |
| 628 | } while (0); |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 629 | return status; |
| 630 | } |
| 631 | |
| 632 | Status ListCommand::fetch() { |
| 633 | Status status = OK; |
Yifan Hong | 9881df9 | 2017-05-10 14:33:05 -0700 | [diff] [blame] | 634 | auto bManager = mLshal.serviceManager(); |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 635 | if (bManager == nullptr) { |
Yifan Hong | 76ac14a | 2017-09-08 14:59:04 -0700 | [diff] [blame] | 636 | err() << "Failed to get defaultServiceManager()!" << std::endl; |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 637 | status |= NO_BINDERIZED_MANAGER; |
| 638 | } else { |
| 639 | status |= fetchBinderized(bManager); |
| 640 | // Passthrough PIDs are registered to the binderized manager as well. |
| 641 | status |= fetchPassthrough(bManager); |
| 642 | } |
| 643 | |
Yifan Hong | 9881df9 | 2017-05-10 14:33:05 -0700 | [diff] [blame] | 644 | auto pManager = mLshal.passthroughManager(); |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 645 | if (pManager == nullptr) { |
Yifan Hong | 76ac14a | 2017-09-08 14:59:04 -0700 | [diff] [blame] | 646 | err() << "Failed to get getPassthroughServiceManager()!" << std::endl; |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 647 | status |= NO_PASSTHROUGH_MANAGER; |
| 648 | } else { |
| 649 | status |= fetchAllLibraries(pManager); |
| 650 | } |
| 651 | return status; |
| 652 | } |
| 653 | |
Yifan Hong | a6b93f0 | 2017-09-13 16:53:37 -0700 | [diff] [blame] | 654 | void ListCommand::registerAllOptions() { |
| 655 | int v = mOptions.size(); |
| 656 | // A list of acceptable command line options |
| 657 | // key: value returned by getopt_long |
| 658 | // long options with short alternatives |
| 659 | mOptions.push_back({'h', "help", no_argument, v++, [](ListCommand*, const char*) { |
| 660 | return USAGE; |
| 661 | }, ""}); |
| 662 | mOptions.push_back({'i', "interface", no_argument, v++, [](ListCommand* thiz, const char*) { |
| 663 | thiz->mSelectedColumns.push_back(TableColumnType::INTERFACE_NAME); |
| 664 | return OK; |
| 665 | }, "print the instance name column"}); |
Yifan Hong | fee209d | 2017-09-14 18:23:38 -0700 | [diff] [blame] | 666 | mOptions.push_back({'l', "released", no_argument, v++, [](ListCommand* thiz, const char*) { |
| 667 | thiz->mSelectedColumns.push_back(TableColumnType::RELEASED); |
| 668 | return OK; |
| 669 | }, "print the 'is released?' column\n(Y=released, empty=unreleased or unknown)"}); |
Yifan Hong | a6b93f0 | 2017-09-13 16:53:37 -0700 | [diff] [blame] | 670 | mOptions.push_back({'t', "transport", no_argument, v++, [](ListCommand* thiz, const char*) { |
| 671 | thiz->mSelectedColumns.push_back(TableColumnType::TRANSPORT); |
| 672 | return OK; |
| 673 | }, "print the transport mode column"}); |
| 674 | mOptions.push_back({'r', "arch", no_argument, v++, [](ListCommand* thiz, const char*) { |
| 675 | thiz->mSelectedColumns.push_back(TableColumnType::ARCH); |
| 676 | return OK; |
| 677 | }, "print the bitness column"}); |
Yifan Hong | fee209d | 2017-09-14 18:23:38 -0700 | [diff] [blame] | 678 | mOptions.push_back({'s', "hash", no_argument, v++, [](ListCommand* thiz, const char*) { |
| 679 | thiz->mSelectedColumns.push_back(TableColumnType::HASH); |
| 680 | return OK; |
| 681 | }, "print hash of the interface"}); |
Yifan Hong | a6b93f0 | 2017-09-13 16:53:37 -0700 | [diff] [blame] | 682 | mOptions.push_back({'p', "pid", no_argument, v++, [](ListCommand* thiz, const char*) { |
| 683 | thiz->mSelectedColumns.push_back(TableColumnType::SERVER_PID); |
| 684 | return OK; |
| 685 | }, "print the server PID, or server cmdline if -m is set"}); |
| 686 | mOptions.push_back({'a', "address", no_argument, v++, [](ListCommand* thiz, const char*) { |
| 687 | thiz->mSelectedColumns.push_back(TableColumnType::SERVER_ADDR); |
| 688 | return OK; |
| 689 | }, "print the server object address column"}); |
| 690 | mOptions.push_back({'c', "clients", no_argument, v++, [](ListCommand* thiz, const char*) { |
| 691 | thiz->mSelectedColumns.push_back(TableColumnType::CLIENT_PIDS); |
| 692 | return OK; |
| 693 | }, "print the client PIDs, or client cmdlines if -m is set"}); |
| 694 | mOptions.push_back({'e', "threads", no_argument, v++, [](ListCommand* thiz, const char*) { |
| 695 | thiz->mSelectedColumns.push_back(TableColumnType::THREADS); |
| 696 | return OK; |
| 697 | }, "print currently used/available threads\n(note, available threads created lazily)"}); |
| 698 | mOptions.push_back({'m', "cmdline", no_argument, v++, [](ListCommand* thiz, const char*) { |
| 699 | thiz->mEnableCmdlines = true; |
| 700 | return OK; |
| 701 | }, "print cmdline instead of PIDs"}); |
| 702 | mOptions.push_back({'d', "debug", optional_argument, v++, [](ListCommand* thiz, const char* arg) { |
| 703 | thiz->mEmitDebugInfo = true; |
| 704 | if (arg) thiz->mFileOutputPath = arg; |
| 705 | return OK; |
| 706 | }, "Emit debug info from\nIBase::debug with empty options. Cannot be used with --neat.\n" |
| 707 | "Writes to specified file if 'arg' is provided, otherwise stdout."}); |
| 708 | |
| 709 | // long options without short alternatives |
| 710 | mOptions.push_back({'\0', "init-vintf", no_argument, v++, [](ListCommand* thiz, const char* arg) { |
| 711 | thiz->mVintf = true; |
| 712 | if (arg) thiz->mFileOutputPath = arg; |
| 713 | return OK; |
| 714 | }, "form a skeleton HAL manifest to specified file,\nor stdout if no file specified."}); |
| 715 | mOptions.push_back({'\0', "sort", required_argument, v++, [](ListCommand* thiz, const char* arg) { |
| 716 | if (strcmp(arg, "interface") == 0 || strcmp(arg, "i") == 0) { |
| 717 | thiz->mSortColumn = TableEntry::sortByInterfaceName; |
| 718 | } else if (strcmp(arg, "pid") == 0 || strcmp(arg, "p") == 0) { |
| 719 | thiz->mSortColumn = TableEntry::sortByServerPid; |
| 720 | } else { |
| 721 | thiz->err() << "Unrecognized sorting column: " << arg << std::endl; |
| 722 | return USAGE; |
| 723 | } |
| 724 | return OK; |
| 725 | }, "sort by a column. 'arg' can be (i|interface) or (p|pid)."}); |
| 726 | mOptions.push_back({'\0', "neat", no_argument, v++, [](ListCommand* thiz, const char*) { |
| 727 | thiz->mNeat = true; |
| 728 | return OK; |
| 729 | }, "output is machine parsable (no explanatory text).\nCannot be used with --debug."}); |
| 730 | } |
| 731 | |
| 732 | // Create 'longopts' argument to getopt_long. Caller is responsible for maintaining |
| 733 | // the lifetime of "options" during the usage of the returned array. |
| 734 | static std::unique_ptr<struct option[]> getLongOptions( |
| 735 | const ListCommand::RegisteredOptions& options, |
| 736 | int* longOptFlag) { |
| 737 | std::unique_ptr<struct option[]> ret{new struct option[options.size() + 1]}; |
| 738 | int i = 0; |
| 739 | for (const auto& e : options) { |
| 740 | ret[i].name = e.longOption.c_str(); |
| 741 | ret[i].has_arg = e.hasArg; |
| 742 | ret[i].flag = longOptFlag; |
| 743 | ret[i].val = e.val; |
| 744 | |
| 745 | i++; |
| 746 | } |
| 747 | // getopt_long last option has all zeros |
| 748 | ret[i].name = NULL; |
| 749 | ret[i].has_arg = 0; |
| 750 | ret[i].flag = NULL; |
| 751 | ret[i].val = 0; |
| 752 | |
| 753 | return ret; |
| 754 | } |
| 755 | |
| 756 | // Create 'optstring' argument to getopt_long. |
| 757 | static std::string getShortOptions(const ListCommand::RegisteredOptions& options) { |
| 758 | std::stringstream ss; |
| 759 | for (const auto& e : options) { |
| 760 | if (e.shortOption != '\0') { |
| 761 | ss << e.shortOption; |
| 762 | } |
| 763 | } |
| 764 | return ss.str(); |
| 765 | } |
| 766 | |
Yifan Hong | a8bedc6 | 2017-09-08 18:00:31 -0700 | [diff] [blame] | 767 | Status ListCommand::parseArgs(const Arg &arg) { |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 768 | |
Yifan Hong | a6b93f0 | 2017-09-13 16:53:37 -0700 | [diff] [blame] | 769 | if (mOptions.empty()) { |
| 770 | registerAllOptions(); |
| 771 | } |
| 772 | int longOptFlag; |
| 773 | std::unique_ptr<struct option[]> longOptions = getLongOptions(mOptions, &longOptFlag); |
| 774 | std::string shortOptions = getShortOptions(mOptions); |
Yifan Hong | d4a77e8 | 2017-09-06 19:40:24 -0700 | [diff] [blame] | 775 | |
Yifan Hong | a8bedc6 | 2017-09-08 18:00:31 -0700 | [diff] [blame] | 776 | // suppress output to std::err for unknown options |
| 777 | opterr = 0; |
| 778 | |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 779 | int optionIndex; |
| 780 | int c; |
| 781 | // Lshal::parseArgs has set optind to the next option to parse |
| 782 | for (;;) { |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 783 | c = getopt_long(arg.argc, arg.argv, |
Yifan Hong | a6b93f0 | 2017-09-13 16:53:37 -0700 | [diff] [blame] | 784 | shortOptions.c_str(), longOptions.get(), &optionIndex); |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 785 | if (c == -1) { |
| 786 | break; |
| 787 | } |
Yifan Hong | a6b93f0 | 2017-09-13 16:53:37 -0700 | [diff] [blame] | 788 | const RegisteredOption* found = nullptr; |
| 789 | if (c == 0) { |
| 790 | // see long option |
| 791 | for (const auto& e : mOptions) { |
| 792 | if (longOptFlag == e.val) found = &e; |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 793 | } |
Yifan Hong | a6b93f0 | 2017-09-13 16:53:37 -0700 | [diff] [blame] | 794 | } else { |
| 795 | // see short option |
| 796 | for (const auto& e : mOptions) { |
| 797 | if (c == e.shortOption) found = &e; |
| 798 | } |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 799 | } |
Yifan Hong | a6b93f0 | 2017-09-13 16:53:37 -0700 | [diff] [blame] | 800 | |
| 801 | if (found == nullptr) { |
| 802 | // see unrecognized options |
Yifan Hong | a8bedc6 | 2017-09-08 18:00:31 -0700 | [diff] [blame] | 803 | err() << "unrecognized option `" << arg.argv[optind - 1] << "'" << std::endl; |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 804 | return USAGE; |
| 805 | } |
Yifan Hong | a6b93f0 | 2017-09-13 16:53:37 -0700 | [diff] [blame] | 806 | |
| 807 | Status status = found->op(this, optarg); |
| 808 | if (status != OK) { |
| 809 | return status; |
| 810 | } |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 811 | } |
| 812 | if (optind < arg.argc) { |
| 813 | // see non option |
Yifan Hong | a8bedc6 | 2017-09-08 18:00:31 -0700 | [diff] [blame] | 814 | err() << "unrecognized option `" << arg.argv[optind] << "'" << std::endl; |
Yifan Hong | 1bc1e9f | 2017-08-29 17:28:12 -0700 | [diff] [blame] | 815 | return USAGE; |
| 816 | } |
| 817 | |
| 818 | if (mNeat && mEmitDebugInfo) { |
Yifan Hong | 76ac14a | 2017-09-08 14:59:04 -0700 | [diff] [blame] | 819 | err() << "Error: --neat should not be used with --debug." << std::endl; |
Yifan Hong | 1bc1e9f | 2017-08-29 17:28:12 -0700 | [diff] [blame] | 820 | return USAGE; |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 821 | } |
| 822 | |
Yifan Hong | a6b93f0 | 2017-09-13 16:53:37 -0700 | [diff] [blame] | 823 | if (mSelectedColumns.empty()) { |
Yifan Hong | fee209d | 2017-09-14 18:23:38 -0700 | [diff] [blame] | 824 | mSelectedColumns = {TableColumnType::RELEASED, |
| 825 | TableColumnType::INTERFACE_NAME, TableColumnType::THREADS, |
Yifan Hong | 05494a5 | 2017-08-29 18:50:00 -0700 | [diff] [blame] | 826 | TableColumnType::SERVER_PID, TableColumnType::CLIENT_PIDS}; |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 827 | } |
Yifan Hong | d4a77e8 | 2017-09-06 19:40:24 -0700 | [diff] [blame] | 828 | |
Yifan Hong | a6b93f0 | 2017-09-13 16:53:37 -0700 | [diff] [blame] | 829 | if (mEnableCmdlines) { |
| 830 | for (size_t i = 0; i < mSelectedColumns.size(); ++i) { |
| 831 | if (mSelectedColumns[i] == TableColumnType::SERVER_PID) { |
| 832 | mSelectedColumns[i] = TableColumnType::SERVER_CMD; |
Yifan Hong | d4a77e8 | 2017-09-06 19:40:24 -0700 | [diff] [blame] | 833 | } |
Yifan Hong | a6b93f0 | 2017-09-13 16:53:37 -0700 | [diff] [blame] | 834 | if (mSelectedColumns[i] == TableColumnType::CLIENT_PIDS) { |
| 835 | mSelectedColumns[i] = TableColumnType::CLIENT_CMDS; |
Yifan Hong | d4a77e8 | 2017-09-06 19:40:24 -0700 | [diff] [blame] | 836 | } |
| 837 | } |
| 838 | } |
| 839 | |
Yifan Hong | a6b93f0 | 2017-09-13 16:53:37 -0700 | [diff] [blame] | 840 | forEachTable([this] (Table& table) { |
| 841 | table.setSelectedColumns(this->mSelectedColumns); |
Yifan Hong | d4a77e8 | 2017-09-06 19:40:24 -0700 | [diff] [blame] | 842 | }); |
| 843 | |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 844 | return OK; |
| 845 | } |
| 846 | |
Yifan Hong | a8bedc6 | 2017-09-08 18:00:31 -0700 | [diff] [blame] | 847 | Status ListCommand::main(const Arg &arg) { |
| 848 | Status status = parseArgs(arg); |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 849 | if (status != OK) { |
| 850 | return status; |
| 851 | } |
| 852 | status = fetch(); |
| 853 | postprocess(); |
Yifan Hong | ca3b660 | 2017-09-07 16:44:27 -0700 | [diff] [blame] | 854 | status |= dump(); |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 855 | return status; |
| 856 | } |
| 857 | |
Yifan Hong | a6b93f0 | 2017-09-13 16:53:37 -0700 | [diff] [blame] | 858 | static std::vector<std::string> splitString(const std::string &s, char c) { |
| 859 | std::vector<std::string> components; |
| 860 | |
| 861 | size_t startPos = 0; |
| 862 | size_t matchPos; |
| 863 | while ((matchPos = s.find(c, startPos)) != std::string::npos) { |
| 864 | components.push_back(s.substr(startPos, matchPos - startPos)); |
| 865 | startPos = matchPos + 1; |
| 866 | } |
| 867 | |
| 868 | if (startPos <= s.length()) { |
| 869 | components.push_back(s.substr(startPos)); |
| 870 | } |
| 871 | return components; |
| 872 | } |
| 873 | |
| 874 | const std::string& ListCommand::RegisteredOption::getHelpMessageForArgument() const { |
| 875 | static const std::string empty{}; |
| 876 | static const std::string optional{"[=<arg>]"}; |
| 877 | static const std::string required{"=<arg>"}; |
| 878 | |
| 879 | if (hasArg == optional_argument) { |
| 880 | return optional; |
| 881 | } |
| 882 | if (hasArg == required_argument) { |
| 883 | return required; |
| 884 | } |
| 885 | return empty; |
| 886 | } |
| 887 | |
Yifan Hong | a8bedc6 | 2017-09-08 18:00:31 -0700 | [diff] [blame] | 888 | void ListCommand::usage() const { |
| 889 | |
Yifan Hong | a6b93f0 | 2017-09-13 16:53:37 -0700 | [diff] [blame] | 890 | err() << "list:" << std::endl |
| 891 | << " lshal" << std::endl |
| 892 | << " lshal list" << std::endl |
Yifan Hong | fee209d | 2017-09-14 18:23:38 -0700 | [diff] [blame] | 893 | << " 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] | 894 | << " lshal list [-h|--help]" << std::endl |
| 895 | << " -h, --help: Print help message for list (`lshal help list`)" << std::endl |
| 896 | << " lshal [list] [OPTIONS...]" << std::endl; |
| 897 | for (const auto& e : mOptions) { |
| 898 | if (e.help.empty()) { |
| 899 | continue; |
| 900 | } |
| 901 | err() << " "; |
| 902 | if (e.shortOption != '\0') |
| 903 | err() << "-" << e.shortOption << e.getHelpMessageForArgument(); |
| 904 | if (e.shortOption != '\0' && !e.longOption.empty()) |
| 905 | err() << ", "; |
| 906 | if (!e.longOption.empty()) |
| 907 | err() << "--" << e.longOption << e.getHelpMessageForArgument(); |
| 908 | err() << ": "; |
| 909 | std::vector<std::string> lines = splitString(e.help, '\n'); |
| 910 | for (const auto& line : lines) { |
| 911 | if (&line != &lines.front()) |
| 912 | err() << " "; |
| 913 | err() << line << std::endl; |
| 914 | } |
| 915 | } |
Yifan Hong | a8bedc6 | 2017-09-08 18:00:31 -0700 | [diff] [blame] | 916 | } |
| 917 | |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 918 | } // namespace lshal |
| 919 | } // namespace android |
Yifan Hong | 05494a5 | 2017-08-29 18:50:00 -0700 | [diff] [blame] | 920 | |