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