blob: 1cdd20266f0834b35dac4ea3742d1a6369fe26ff [file] [log] [blame]
Yifan Hong443df792017-05-09 18:49:45 -07001/*
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 Hong443df792017-05-09 18:49:45 -070030#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
41using ::android::hardware::hidl_string;
42using ::android::hidl::manager::V1_0::IServiceManager;
43
44namespace android {
45namespace lshal {
46
47ListCommand::ListCommand(Lshal &lshal) : mLshal(lshal), mErr(lshal.err()), mOut(lshal.out()) {
48}
49
50std::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
60const 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
69void ListCommand::removeDeadProcesses(Pids *pids) {
70 static const pid_t myPid = getpid();
Yifan Hong61fb7bc2017-05-12 16:33:57 -070071 pids->erase(std::remove_if(pids->begin(), pids->end(), [this](auto pid) {
Yifan Hong443df792017-05-09 18:49:45 -070072 return pid == myPid || this->getCmdline(pid).empty();
Yifan Hong61fb7bc2017-05-12 16:33:57 -070073 }), pids->end());
Yifan Hong443df792017-05-09 18:49:45 -070074}
75
Steven Morelandd8e20192017-05-24 11:23:08 -070076bool scanBinderContext(pid_t pid,
77 const std::string &contextName,
78 std::function<void(const std::string&)> eachLine) {
79 std::ifstream ifs("/d/binder/proc/" + std::to_string(pid));
Yifan Hong443df792017-05-09 18:49:45 -070080 if (!ifs.is_open()) {
81 return false;
82 }
83
Steven Morelandd8e20192017-05-24 11:23:08 -070084 static const std::regex kContextLine("^context (\\w+)$");
Yifan Hong443df792017-05-09 18:49:45 -070085
Steven Morelandd8e20192017-05-24 11:23:08 -070086 bool isDesiredContext = false;
Yifan Hong443df792017-05-09 18:49:45 -070087 std::string line;
88 std::smatch match;
89 while(getline(ifs, line)) {
Steven Morelandd8e20192017-05-24 11:23:08 -070090 if (std::regex_search(line, match, kContextLine)) {
91 isDesiredContext = match.str(1) == contextName;
Yifan Hong443df792017-05-09 18:49:45 -070092 continue;
93 }
Steven Morelandd8e20192017-05-24 11:23:08 -070094
95 if (!isDesiredContext) {
Yifan Hong443df792017-05-09 18:49:45 -070096 continue;
97 }
Steven Morelandd8e20192017-05-24 11:23:08 -070098
99 eachLine(line);
Yifan Hong443df792017-05-09 18:49:45 -0700100 }
101 return true;
102}
103
Steven Morelandd8e20192017-05-24 11:23:08 -0700104bool ListCommand::getPidInfo(
105 pid_t serverPid, PidInfo *pidInfo) const {
106 static const std::regex kReferencePrefix("^\\s*node \\d+:\\s+u([0-9a-f]+)\\s+c([0-9a-f]+)\\s+");
107 static const std::regex kThreadPrefix("^\\s*thread \\d+:\\s+l\\s+(\\d)(\\d)");
108
109 std::smatch match;
110 return scanBinderContext(serverPid, "hwbinder", [&](const std::string& line) {
111 if (std::regex_search(line, match, kReferencePrefix)) {
112 const std::string &ptrString = "0x" + match.str(2); // use number after c
113 uint64_t ptr;
114 if (!::android::base::ParseUint(ptrString.c_str(), &ptr)) {
115 // Should not reach here, but just be tolerant.
116 mErr << "Could not parse number " << ptrString << std::endl;
117 return;
118 }
119 const std::string proc = " proc ";
120 auto pos = line.rfind(proc);
121 if (pos != std::string::npos) {
122 for (const std::string &pidStr : split(line.substr(pos + proc.size()), ' ')) {
123 int32_t pid;
124 if (!::android::base::ParseInt(pidStr, &pid)) {
125 mErr << "Could not parse number " << pidStr << std::endl;
126 return;
127 }
128 pidInfo->refPids[ptr].push_back(pid);
129 }
130 }
131
132 return;
133 }
134
135 if (std::regex_search(line, match, kThreadPrefix)) {
136 // "1" is waiting in binder driver
137 // "2" is poll. It's impossible to tell if these are in use.
138 // and HIDL default code doesn't use it.
139 bool isInUse = match.str(1) != "1";
140 // "0" is a thread that has called into binder
141 // "1" is looper thread
142 // "2" is main looper thread
143 bool isHwbinderThread = match.str(2) != "0";
144
145 if (!isHwbinderThread) {
146 return;
147 }
148
149 if (isInUse) {
150 pidInfo->threadUsage++;
151 }
152
153 pidInfo->threadCount++;
154 return;
155 }
156
157 // not reference or thread line
158 return;
159 });
160}
161
Yifan Hong443df792017-05-09 18:49:45 -0700162// Must process hwbinder services first, then passthrough services.
163void ListCommand::forEachTable(const std::function<void(Table &)> &f) {
164 f(mServicesTable);
165 f(mPassthroughRefTable);
166 f(mImplementationsTable);
167}
168void ListCommand::forEachTable(const std::function<void(const Table &)> &f) const {
169 f(mServicesTable);
170 f(mPassthroughRefTable);
171 f(mImplementationsTable);
172}
173
174void ListCommand::postprocess() {
175 forEachTable([this](Table &table) {
176 if (mSortColumn) {
177 std::sort(table.begin(), table.end(), mSortColumn);
178 }
179 for (TableEntry &entry : table) {
180 entry.serverCmdline = getCmdline(entry.serverPid);
181 removeDeadProcesses(&entry.clientPids);
182 for (auto pid : entry.clientPids) {
183 entry.clientCmdlines.push_back(this->getCmdline(pid));
184 }
185 }
186 });
187 // use a double for loop here because lshal doesn't care about efficiency.
188 for (TableEntry &packageEntry : mImplementationsTable) {
189 std::string packageName = packageEntry.interfaceName;
190 FQName fqPackageName{packageName.substr(0, packageName.find("::"))};
191 if (!fqPackageName.isValid()) {
192 continue;
193 }
194 for (TableEntry &interfaceEntry : mPassthroughRefTable) {
195 if (interfaceEntry.arch != ARCH_UNKNOWN) {
196 continue;
197 }
198 FQName interfaceName{splitFirst(interfaceEntry.interfaceName, '/').first};
199 if (!interfaceName.isValid()) {
200 continue;
201 }
202 if (interfaceName.getPackageAndVersion() == fqPackageName) {
203 interfaceEntry.arch = packageEntry.arch;
204 }
205 }
206 }
207}
208
209void ListCommand::printLine(
210 const std::string &interfaceName,
211 const std::string &transport,
212 const std::string &arch,
Steven Morelandd8e20192017-05-24 11:23:08 -0700213 const std::string &threadUsage,
Yifan Hong443df792017-05-09 18:49:45 -0700214 const std::string &server,
215 const std::string &serverCmdline,
Steven Morelandd8e20192017-05-24 11:23:08 -0700216 const std::string &address,
217 const std::string &clients,
Yifan Hong443df792017-05-09 18:49:45 -0700218 const std::string &clientCmdlines) const {
219 if (mSelectedColumns & ENABLE_INTERFACE_NAME)
220 mOut << std::setw(80) << interfaceName << "\t";
221 if (mSelectedColumns & ENABLE_TRANSPORT)
222 mOut << std::setw(10) << transport << "\t";
223 if (mSelectedColumns & ENABLE_ARCH)
224 mOut << std::setw(5) << arch << "\t";
Steven Morelandd8e20192017-05-24 11:23:08 -0700225 if (mSelectedColumns & ENABLE_THREADS) {
226 mOut << std::setw(8) << threadUsage << "\t";
227 }
Yifan Hong443df792017-05-09 18:49:45 -0700228 if (mSelectedColumns & ENABLE_SERVER_PID) {
229 if (mEnableCmdlines) {
230 mOut << std::setw(15) << serverCmdline << "\t";
231 } else {
232 mOut << std::setw(5) << server << "\t";
233 }
234 }
235 if (mSelectedColumns & ENABLE_SERVER_ADDR)
236 mOut << std::setw(16) << address << "\t";
237 if (mSelectedColumns & ENABLE_CLIENT_PIDS) {
238 if (mEnableCmdlines) {
239 mOut << std::setw(0) << clientCmdlines;
240 } else {
241 mOut << std::setw(0) << clients;
242 }
243 }
244 mOut << std::endl;
245}
246
247void ListCommand::dumpVintf() const {
Yifan Hong236301c2017-06-19 12:27:08 -0700248 using vintf::operator|=;
Yifan Hong443df792017-05-09 18:49:45 -0700249 mOut << "<!-- " << std::endl
250 << " This is a skeleton device manifest. Notes: " << std::endl
251 << " 1. android.hidl.*, android.frameworks.*, android.system.* are not included." << std::endl
252 << " 2. If a HAL is supported in both hwbinder and passthrough transport, " << std::endl
253 << " only hwbinder is shown." << std::endl
254 << " 3. It is likely that HALs in passthrough transport does not have" << std::endl
255 << " <interface> declared; users will have to write them by hand." << std::endl
256 << " 4. sepolicy version is set to 0.0. It is recommended that the entry" << std::endl
257 << " is removed from the manifest file and written by assemble_vintf" << std::endl
258 << " at build time." << std::endl
259 << "-->" << std::endl;
260
261 vintf::HalManifest manifest;
262 forEachTable([this, &manifest] (const Table &table) {
263 for (const TableEntry &entry : table) {
264
265 std::string fqInstanceName = entry.interfaceName;
266
267 if (&table == &mImplementationsTable) {
268 // Quick hack to work around *'s
269 replaceAll(&fqInstanceName, '*', 'D');
270 }
271 auto splittedFqInstanceName = splitFirst(fqInstanceName, '/');
272 FQName fqName(splittedFqInstanceName.first);
273 if (!fqName.isValid()) {
274 mErr << "Warning: '" << splittedFqInstanceName.first
275 << "' is not a valid FQName." << std::endl;
276 continue;
277 }
278 // Strip out system libs.
279 if (fqName.inPackage("android.hidl") ||
280 fqName.inPackage("android.frameworks") ||
281 fqName.inPackage("android.system")) {
282 continue;
283 }
284 std::string interfaceName =
285 &table == &mImplementationsTable ? "" : fqName.name();
286 std::string instanceName =
287 &table == &mImplementationsTable ? "" : splittedFqInstanceName.second;
288
289 vintf::Version version{fqName.getPackageMajorVersion(),
290 fqName.getPackageMinorVersion()};
291 vintf::Transport transport;
292 vintf::Arch arch;
293 if (entry.transport == "hwbinder") {
294 transport = vintf::Transport::HWBINDER;
295 arch = vintf::Arch::ARCH_EMPTY;
296 } else if (entry.transport == "passthrough") {
297 transport = vintf::Transport::PASSTHROUGH;
298 switch (entry.arch) {
299 case lshal::ARCH32:
300 arch = vintf::Arch::ARCH_32; break;
301 case lshal::ARCH64:
302 arch = vintf::Arch::ARCH_64; break;
303 case lshal::ARCH_BOTH:
304 arch = vintf::Arch::ARCH_32_64; break;
305 case lshal::ARCH_UNKNOWN: // fallthrough
306 default:
307 mErr << "Warning: '" << fqName.package()
308 << "' doesn't have bitness info, assuming 32+64." << std::endl;
309 arch = vintf::Arch::ARCH_32_64;
310 }
311 } else {
312 mErr << "Warning: '" << entry.transport << "' is not a valid transport." << std::endl;
313 continue;
314 }
315
316 bool done = false;
317 for (vintf::ManifestHal *hal : manifest.getHals(fqName.package())) {
318 if (hal->transport() != transport) {
319 if (transport != vintf::Transport::PASSTHROUGH) {
Yifan Hong236301c2017-06-19 12:27:08 -0700320 mErr << "Fatal: should not reach here. Generated result may be wrong for '"
321 << hal->name << "'."
Yifan Hong443df792017-05-09 18:49:45 -0700322 << std::endl;
323 }
324 done = true;
325 break;
326 }
327 if (hal->hasVersion(version)) {
328 if (&table != &mImplementationsTable) {
329 hal->interfaces[interfaceName].name = interfaceName;
330 hal->interfaces[interfaceName].instances.insert(instanceName);
331 }
Yifan Hong236301c2017-06-19 12:27:08 -0700332 hal->transportArch.arch |= arch;
Yifan Hong443df792017-05-09 18:49:45 -0700333 done = true;
334 break;
335 }
336 }
337 if (done) {
338 continue; // to next TableEntry
339 }
340 decltype(vintf::ManifestHal::interfaces) interfaces;
341 if (&table != &mImplementationsTable) {
342 interfaces[interfaceName].name = interfaceName;
343 interfaces[interfaceName].instances.insert(instanceName);
344 }
345 if (!manifest.add(vintf::ManifestHal{
346 .format = vintf::HalFormat::HIDL,
347 .name = fqName.package(),
348 .versions = {version},
349 .transportArch = {transport, arch},
350 .interfaces = interfaces})) {
351 mErr << "Warning: cannot add hal '" << fqInstanceName << "'" << std::endl;
352 }
353 }
354 });
355 mOut << vintf::gHalManifestConverter(manifest);
356}
357
358static const std::string &getArchString(Architecture arch) {
359 static const std::string sStr64 = "64";
360 static const std::string sStr32 = "32";
361 static const std::string sStrBoth = "32+64";
362 static const std::string sStrUnknown = "";
363 switch (arch) {
364 case ARCH64:
365 return sStr64;
366 case ARCH32:
367 return sStr32;
368 case ARCH_BOTH:
369 return sStrBoth;
370 case ARCH_UNKNOWN: // fall through
371 default:
372 return sStrUnknown;
373 }
374}
375
376static Architecture fromBaseArchitecture(::android::hidl::base::V1_0::DebugInfo::Architecture a) {
377 switch (a) {
378 case ::android::hidl::base::V1_0::DebugInfo::Architecture::IS_64BIT:
379 return ARCH64;
380 case ::android::hidl::base::V1_0::DebugInfo::Architecture::IS_32BIT:
381 return ARCH32;
382 case ::android::hidl::base::V1_0::DebugInfo::Architecture::UNKNOWN: // fallthrough
383 default:
384 return ARCH_UNKNOWN;
385 }
386}
387
388void ListCommand::dumpTable() {
389 mServicesTable.description =
390 "All binderized services (registered services through hwservicemanager)";
391 mPassthroughRefTable.description =
392 "All interfaces that getService() has ever return as a passthrough interface;\n"
393 "PIDs / processes shown below might be inaccurate because the process\n"
394 "might have relinquished the interface or might have died.\n"
395 "The Server / Server CMD column can be ignored.\n"
396 "The Clients / Clients CMD column shows all process that have ever dlopen'ed \n"
397 "the library and successfully fetched the passthrough implementation.";
398 mImplementationsTable.description =
399 "All available passthrough implementations (all -impl.so files)";
400 forEachTable([this] (const Table &table) {
Yifan Hong6da06912017-05-12 16:56:43 -0700401 if (!mNeat) {
402 mOut << table.description << std::endl;
403 }
Yifan Hong443df792017-05-09 18:49:45 -0700404 mOut << std::left;
Yifan Hong6da06912017-05-12 16:56:43 -0700405 if (!mNeat) {
Steven Morelandd8e20192017-05-24 11:23:08 -0700406 printLine("Interface", "Transport", "Arch", "Thread Use", "Server",
407 "Server CMD", "PTR", "Clients", "Clients CMD");
Yifan Hong6da06912017-05-12 16:56:43 -0700408 }
Yifan Hong443df792017-05-09 18:49:45 -0700409
Yifan Hong443df792017-05-09 18:49:45 -0700410 for (const auto &entry : table) {
411 printLine(entry.interfaceName,
412 entry.transport,
413 getArchString(entry.arch),
Steven Morelandd8e20192017-05-24 11:23:08 -0700414 entry.getThreadUsage(),
Yifan Hong443df792017-05-09 18:49:45 -0700415 entry.serverPid == NO_PID ? "N/A" : std::to_string(entry.serverPid),
416 entry.serverCmdline,
417 entry.serverObjectAddress == NO_PTR ? "N/A" : toHexString(entry.serverObjectAddress),
418 join(entry.clientPids, " "),
419 join(entry.clientCmdlines, ";"));
420
Yifan Hong48dc9f82017-05-09 19:33:08 -0700421 // We're only interested in dumping debug info for already
422 // instantiated services. There's little value in dumping the
423 // debug info for a service we create on the fly, so we only operate
424 // on the "mServicesTable".
425 if (mEmitDebugInfo && &table == &mServicesTable) {
Yifan Hong443df792017-05-09 18:49:45 -0700426 auto pair = splitFirst(entry.interfaceName, '/');
Yifan Hong48dc9f82017-05-09 19:33:08 -0700427 mLshal.emitDebugInfo(pair.first, pair.second, {}, mOut.buf(),
428 NullableOStream<std::ostream>(nullptr));
Yifan Hong443df792017-05-09 18:49:45 -0700429 }
430 }
Yifan Hong6da06912017-05-12 16:56:43 -0700431 if (!mNeat) {
432 mOut << std::endl;
433 }
Yifan Hong443df792017-05-09 18:49:45 -0700434 });
435
436}
437
438void ListCommand::dump() {
439 if (mVintf) {
440 dumpVintf();
441 if (!!mFileOutput) {
442 mFileOutput.buf().close();
443 delete &mFileOutput.buf();
444 mFileOutput = nullptr;
445 }
446 mOut = std::cout;
447 } else {
448 dumpTable();
449 }
450}
451
452void ListCommand::putEntry(TableEntrySource source, TableEntry &&entry) {
453 Table *table = nullptr;
454 switch (source) {
455 case HWSERVICEMANAGER_LIST :
456 table = &mServicesTable; break;
457 case PTSERVICEMANAGER_REG_CLIENT :
458 table = &mPassthroughRefTable; break;
459 case LIST_DLLIB :
460 table = &mImplementationsTable; break;
461 default:
462 mErr << "Error: Unknown source of entry " << source << std::endl;
463 }
464 if (table) {
465 table->entries.push_back(std::forward<TableEntry>(entry));
466 }
467}
468
469Status ListCommand::fetchAllLibraries(const sp<IServiceManager> &manager) {
470 using namespace ::android::hardware;
471 using namespace ::android::hidl::manager::V1_0;
472 using namespace ::android::hidl::base::V1_0;
Yifan Hongf2d557b2017-05-24 19:45:02 -0700473 using std::literals::chrono_literals::operator""s;
474 auto ret = timeoutIPC(2s, manager, &IServiceManager::debugDump, [&] (const auto &infos) {
Yifan Hong443df792017-05-09 18:49:45 -0700475 std::map<std::string, TableEntry> entries;
476 for (const auto &info : infos) {
477 std::string interfaceName = std::string{info.interfaceName.c_str()} + "/" +
478 std::string{info.instanceName.c_str()};
479 entries.emplace(interfaceName, TableEntry{
480 .interfaceName = interfaceName,
481 .transport = "passthrough",
482 .serverPid = NO_PID,
483 .serverObjectAddress = NO_PTR,
Yifan Hongf2d557b2017-05-24 19:45:02 -0700484 .clientPids = info.clientPids,
Yifan Hong443df792017-05-09 18:49:45 -0700485 .arch = ARCH_UNKNOWN
486 }).first->second.arch |= fromBaseArchitecture(info.arch);
487 }
488 for (auto &&pair : entries) {
489 putEntry(LIST_DLLIB, std::move(pair.second));
490 }
491 });
492 if (!ret.isOk()) {
493 mErr << "Error: Failed to call list on getPassthroughServiceManager(): "
494 << ret.description() << std::endl;
495 return DUMP_ALL_LIBS_ERROR;
496 }
497 return OK;
498}
499
500Status ListCommand::fetchPassthrough(const sp<IServiceManager> &manager) {
501 using namespace ::android::hardware;
502 using namespace ::android::hardware::details;
503 using namespace ::android::hidl::manager::V1_0;
504 using namespace ::android::hidl::base::V1_0;
505 auto ret = timeoutIPC(manager, &IServiceManager::debugDump, [&] (const auto &infos) {
506 for (const auto &info : infos) {
507 if (info.clientPids.size() <= 0) {
508 continue;
509 }
510 putEntry(PTSERVICEMANAGER_REG_CLIENT, {
511 .interfaceName =
512 std::string{info.interfaceName.c_str()} + "/" +
513 std::string{info.instanceName.c_str()},
514 .transport = "passthrough",
515 .serverPid = info.clientPids.size() == 1 ? info.clientPids[0] : NO_PID,
516 .serverObjectAddress = NO_PTR,
517 .clientPids = info.clientPids,
518 .arch = fromBaseArchitecture(info.arch)
519 });
520 }
521 });
522 if (!ret.isOk()) {
523 mErr << "Error: Failed to call debugDump on defaultServiceManager(): "
524 << ret.description() << std::endl;
525 return DUMP_PASSTHROUGH_ERROR;
526 }
527 return OK;
528}
529
530Status ListCommand::fetchBinderized(const sp<IServiceManager> &manager) {
531 using namespace ::std;
532 using namespace ::android::hardware;
533 using namespace ::android::hidl::manager::V1_0;
534 using namespace ::android::hidl::base::V1_0;
535 const std::string mode = "hwbinder";
536
537 hidl_vec<hidl_string> fqInstanceNames;
538 // copying out for timeoutIPC
539 auto listRet = timeoutIPC(manager, &IServiceManager::list, [&] (const auto &names) {
540 fqInstanceNames = names;
541 });
542 if (!listRet.isOk()) {
543 mErr << "Error: Failed to list services for " << mode << ": "
544 << listRet.description() << std::endl;
545 return DUMP_BINDERIZED_ERROR;
546 }
547
548 Status status = OK;
549 // server pid, .ptr value of binder object, child pids
550 std::map<std::string, DebugInfo> allDebugInfos;
Steven Morelandd8e20192017-05-24 11:23:08 -0700551 std::map<pid_t, PidInfo> allPids;
Yifan Hong443df792017-05-09 18:49:45 -0700552 for (const auto &fqInstanceName : fqInstanceNames) {
553 const auto pair = splitFirst(fqInstanceName, '/');
554 const auto &serviceName = pair.first;
555 const auto &instanceName = pair.second;
556 auto getRet = timeoutIPC(manager, &IServiceManager::get, serviceName, instanceName);
557 if (!getRet.isOk()) {
558 mErr << "Warning: Skipping \"" << fqInstanceName << "\": "
559 << "cannot be fetched from service manager:"
560 << getRet.description() << std::endl;
561 status |= DUMP_BINDERIZED_ERROR;
562 continue;
563 }
564 sp<IBase> service = getRet;
565 if (service == nullptr) {
566 mErr << "Warning: Skipping \"" << fqInstanceName << "\": "
567 << "cannot be fetched from service manager (null)"
568 << std::endl;
569 status |= DUMP_BINDERIZED_ERROR;
570 continue;
571 }
572 auto debugRet = timeoutIPC(service, &IBase::getDebugInfo, [&] (const auto &debugInfo) {
573 allDebugInfos[fqInstanceName] = debugInfo;
574 if (debugInfo.pid >= 0) {
Steven Morelandd8e20192017-05-24 11:23:08 -0700575 allPids[static_cast<pid_t>(debugInfo.pid)] = PidInfo();
Yifan Hong443df792017-05-09 18:49:45 -0700576 }
577 });
578 if (!debugRet.isOk()) {
579 mErr << "Warning: Skipping \"" << fqInstanceName << "\": "
580 << "debugging information cannot be retrieved:"
581 << debugRet.description() << std::endl;
582 status |= DUMP_BINDERIZED_ERROR;
583 }
584 }
Steven Morelandd8e20192017-05-24 11:23:08 -0700585
Yifan Hong443df792017-05-09 18:49:45 -0700586 for (auto &pair : allPids) {
587 pid_t serverPid = pair.first;
Steven Morelandd8e20192017-05-24 11:23:08 -0700588 if (!getPidInfo(serverPid, &allPids[serverPid])) {
Yifan Hong443df792017-05-09 18:49:45 -0700589 mErr << "Warning: no information for PID " << serverPid
590 << ", are you root?" << std::endl;
591 status |= DUMP_BINDERIZED_ERROR;
592 }
593 }
594 for (const auto &fqInstanceName : fqInstanceNames) {
595 auto it = allDebugInfos.find(fqInstanceName);
596 if (it == allDebugInfos.end()) {
597 putEntry(HWSERVICEMANAGER_LIST, {
598 .interfaceName = fqInstanceName,
599 .transport = mode,
600 .serverPid = NO_PID,
601 .serverObjectAddress = NO_PTR,
602 .clientPids = {},
Steven Morelandd8e20192017-05-24 11:23:08 -0700603 .threadUsage = 0,
604 .threadCount = 0,
Yifan Hong443df792017-05-09 18:49:45 -0700605 .arch = ARCH_UNKNOWN
606 });
607 continue;
608 }
609 const DebugInfo &info = it->second;
Steven Morelandd8e20192017-05-24 11:23:08 -0700610 bool writePidInfo = info.pid != NO_PID && info.ptr != NO_PTR;
611
Yifan Hong443df792017-05-09 18:49:45 -0700612 putEntry(HWSERVICEMANAGER_LIST, {
613 .interfaceName = fqInstanceName,
614 .transport = mode,
615 .serverPid = info.pid,
616 .serverObjectAddress = info.ptr,
Steven Morelandd8e20192017-05-24 11:23:08 -0700617 .clientPids = writePidInfo ? allPids[info.pid].refPids[info.ptr] : Pids{},
618 .threadUsage = writePidInfo ? allPids[info.pid].threadUsage : 0,
619 .threadCount = writePidInfo ? allPids[info.pid].threadCount : 0,
Yifan Hong443df792017-05-09 18:49:45 -0700620 .arch = fromBaseArchitecture(info.arch),
621 });
622 }
623 return status;
624}
625
626Status ListCommand::fetch() {
627 Status status = OK;
Yifan Hong9881df92017-05-10 14:33:05 -0700628 auto bManager = mLshal.serviceManager();
Yifan Hong443df792017-05-09 18:49:45 -0700629 if (bManager == nullptr) {
630 mErr << "Failed to get defaultServiceManager()!" << std::endl;
631 status |= NO_BINDERIZED_MANAGER;
632 } else {
633 status |= fetchBinderized(bManager);
634 // Passthrough PIDs are registered to the binderized manager as well.
635 status |= fetchPassthrough(bManager);
636 }
637
Yifan Hong9881df92017-05-10 14:33:05 -0700638 auto pManager = mLshal.passthroughManager();
Yifan Hong443df792017-05-09 18:49:45 -0700639 if (pManager == nullptr) {
640 mErr << "Failed to get getPassthroughServiceManager()!" << std::endl;
641 status |= NO_PASSTHROUGH_MANAGER;
642 } else {
643 status |= fetchAllLibraries(pManager);
644 }
645 return status;
646}
647
648Status ListCommand::parseArgs(const std::string &command, const Arg &arg) {
649 static struct option longOptions[] = {
650 // long options with short alternatives
651 {"help", no_argument, 0, 'h' },
652 {"interface", no_argument, 0, 'i' },
653 {"transport", no_argument, 0, 't' },
654 {"arch", no_argument, 0, 'r' },
655 {"pid", no_argument, 0, 'p' },
656 {"address", no_argument, 0, 'a' },
657 {"clients", no_argument, 0, 'c' },
Steven Morelandd8e20192017-05-24 11:23:08 -0700658 {"threads", no_argument, 0, 'e' },
Yifan Hong443df792017-05-09 18:49:45 -0700659 {"cmdline", no_argument, 0, 'm' },
660 {"debug", optional_argument, 0, 'd' },
661
662 // long options without short alternatives
663 {"sort", required_argument, 0, 's' },
664 {"init-vintf",optional_argument, 0, 'v' },
Yifan Hong6da06912017-05-12 16:56:43 -0700665 {"neat", no_argument, 0, 'n' },
Yifan Hong443df792017-05-09 18:49:45 -0700666 { 0, 0, 0, 0 }
667 };
668
669 int optionIndex;
670 int c;
671 // Lshal::parseArgs has set optind to the next option to parse
672 for (;;) {
673 // using getopt_long in case we want to add other options in the future
674 c = getopt_long(arg.argc, arg.argv,
Steven Morelandd8e20192017-05-24 11:23:08 -0700675 "hitrpacmde", longOptions, &optionIndex);
Yifan Hong443df792017-05-09 18:49:45 -0700676 if (c == -1) {
677 break;
678 }
679 switch (c) {
680 case 's': {
681 if (strcmp(optarg, "interface") == 0 || strcmp(optarg, "i") == 0) {
682 mSortColumn = TableEntry::sortByInterfaceName;
683 } else if (strcmp(optarg, "pid") == 0 || strcmp(optarg, "p") == 0) {
684 mSortColumn = TableEntry::sortByServerPid;
685 } else {
686 mErr << "Unrecognized sorting column: " << optarg << std::endl;
687 mLshal.usage(command);
688 return USAGE;
689 }
690 break;
691 }
692 case 'v': {
693 if (optarg) {
694 mFileOutput = new std::ofstream{optarg};
695 mOut = mFileOutput;
696 if (!mFileOutput.buf().is_open()) {
697 mErr << "Could not open file '" << optarg << "'." << std::endl;
698 return IO_ERROR;
699 }
700 }
701 mVintf = true;
702 }
703 case 'i': {
704 mSelectedColumns |= ENABLE_INTERFACE_NAME;
705 break;
706 }
707 case 't': {
708 mSelectedColumns |= ENABLE_TRANSPORT;
709 break;
710 }
711 case 'r': {
712 mSelectedColumns |= ENABLE_ARCH;
713 break;
714 }
715 case 'p': {
716 mSelectedColumns |= ENABLE_SERVER_PID;
717 break;
718 }
719 case 'a': {
720 mSelectedColumns |= ENABLE_SERVER_ADDR;
721 break;
722 }
723 case 'c': {
724 mSelectedColumns |= ENABLE_CLIENT_PIDS;
725 break;
726 }
Steven Morelandd8e20192017-05-24 11:23:08 -0700727 case 'e': {
728 mSelectedColumns |= ENABLE_THREADS;
729 break;
730 }
Yifan Hong443df792017-05-09 18:49:45 -0700731 case 'm': {
732 mEnableCmdlines = true;
733 break;
734 }
735 case 'd': {
736 mEmitDebugInfo = true;
737
738 if (optarg) {
739 mFileOutput = new std::ofstream{optarg};
740 mOut = mFileOutput;
741 if (!mFileOutput.buf().is_open()) {
742 mErr << "Could not open file '" << optarg << "'." << std::endl;
743 return IO_ERROR;
744 }
745 chown(optarg, AID_SHELL, AID_SHELL);
746 }
747 break;
748 }
Yifan Hong6da06912017-05-12 16:56:43 -0700749 case 'n': {
750 mNeat = true;
751 break;
752 }
Yifan Hong443df792017-05-09 18:49:45 -0700753 case 'h': // falls through
754 default: // see unrecognized options
755 mLshal.usage(command);
756 return USAGE;
757 }
758 }
759 if (optind < arg.argc) {
760 // see non option
761 mErr << "Unrecognized option `" << arg.argv[optind] << "`" << std::endl;
762 }
763
764 if (mSelectedColumns == 0) {
Steven Morelandd8e20192017-05-24 11:23:08 -0700765 mSelectedColumns = ENABLE_INTERFACE_NAME | ENABLE_SERVER_PID | ENABLE_CLIENT_PIDS | ENABLE_THREADS;
Yifan Hong443df792017-05-09 18:49:45 -0700766 }
767 return OK;
768}
769
770Status ListCommand::main(const std::string &command, const Arg &arg) {
771 Status status = parseArgs(command, arg);
772 if (status != OK) {
773 return status;
774 }
775 status = fetch();
776 postprocess();
777 dump();
778 return status;
779}
780
781} // namespace lshal
782} // namespace android
783