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