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