blob: 87b910419649eed663db99bd50dad29f2f31c976 [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 {
248 mOut << "<!-- " << std::endl
249 << " This is a skeleton device manifest. Notes: " << std::endl
250 << " 1. android.hidl.*, android.frameworks.*, android.system.* are not included." << std::endl
251 << " 2. If a HAL is supported in both hwbinder and passthrough transport, " << std::endl
252 << " only hwbinder is shown." << std::endl
253 << " 3. It is likely that HALs in passthrough transport does not have" << std::endl
254 << " <interface> declared; users will have to write them by hand." << std::endl
255 << " 4. sepolicy version is set to 0.0. It is recommended that the entry" << std::endl
256 << " is removed from the manifest file and written by assemble_vintf" << std::endl
257 << " at build time." << std::endl
258 << "-->" << std::endl;
259
260 vintf::HalManifest manifest;
261 forEachTable([this, &manifest] (const Table &table) {
262 for (const TableEntry &entry : table) {
263
264 std::string fqInstanceName = entry.interfaceName;
265
266 if (&table == &mImplementationsTable) {
267 // Quick hack to work around *'s
268 replaceAll(&fqInstanceName, '*', 'D');
269 }
270 auto splittedFqInstanceName = splitFirst(fqInstanceName, '/');
271 FQName fqName(splittedFqInstanceName.first);
272 if (!fqName.isValid()) {
273 mErr << "Warning: '" << splittedFqInstanceName.first
274 << "' is not a valid FQName." << std::endl;
275 continue;
276 }
277 // Strip out system libs.
278 if (fqName.inPackage("android.hidl") ||
279 fqName.inPackage("android.frameworks") ||
280 fqName.inPackage("android.system")) {
281 continue;
282 }
283 std::string interfaceName =
284 &table == &mImplementationsTable ? "" : fqName.name();
285 std::string instanceName =
286 &table == &mImplementationsTable ? "" : splittedFqInstanceName.second;
287
288 vintf::Version version{fqName.getPackageMajorVersion(),
289 fqName.getPackageMinorVersion()};
290 vintf::Transport transport;
291 vintf::Arch arch;
292 if (entry.transport == "hwbinder") {
293 transport = vintf::Transport::HWBINDER;
294 arch = vintf::Arch::ARCH_EMPTY;
295 } else if (entry.transport == "passthrough") {
296 transport = vintf::Transport::PASSTHROUGH;
297 switch (entry.arch) {
298 case lshal::ARCH32:
299 arch = vintf::Arch::ARCH_32; break;
300 case lshal::ARCH64:
301 arch = vintf::Arch::ARCH_64; break;
302 case lshal::ARCH_BOTH:
303 arch = vintf::Arch::ARCH_32_64; break;
304 case lshal::ARCH_UNKNOWN: // fallthrough
305 default:
306 mErr << "Warning: '" << fqName.package()
307 << "' doesn't have bitness info, assuming 32+64." << std::endl;
308 arch = vintf::Arch::ARCH_32_64;
309 }
310 } else {
311 mErr << "Warning: '" << entry.transport << "' is not a valid transport." << std::endl;
312 continue;
313 }
314
315 bool done = false;
316 for (vintf::ManifestHal *hal : manifest.getHals(fqName.package())) {
317 if (hal->transport() != transport) {
318 if (transport != vintf::Transport::PASSTHROUGH) {
319 mErr << "Fatal: should not reach here. Generated result may be wrong."
320 << std::endl;
321 }
322 done = true;
323 break;
324 }
325 if (hal->hasVersion(version)) {
326 if (&table != &mImplementationsTable) {
327 hal->interfaces[interfaceName].name = interfaceName;
328 hal->interfaces[interfaceName].instances.insert(instanceName);
329 }
330 done = true;
331 break;
332 }
333 }
334 if (done) {
335 continue; // to next TableEntry
336 }
337 decltype(vintf::ManifestHal::interfaces) interfaces;
338 if (&table != &mImplementationsTable) {
339 interfaces[interfaceName].name = interfaceName;
340 interfaces[interfaceName].instances.insert(instanceName);
341 }
342 if (!manifest.add(vintf::ManifestHal{
343 .format = vintf::HalFormat::HIDL,
344 .name = fqName.package(),
345 .versions = {version},
346 .transportArch = {transport, arch},
347 .interfaces = interfaces})) {
348 mErr << "Warning: cannot add hal '" << fqInstanceName << "'" << std::endl;
349 }
350 }
351 });
352 mOut << vintf::gHalManifestConverter(manifest);
353}
354
355static const std::string &getArchString(Architecture arch) {
356 static const std::string sStr64 = "64";
357 static const std::string sStr32 = "32";
358 static const std::string sStrBoth = "32+64";
359 static const std::string sStrUnknown = "";
360 switch (arch) {
361 case ARCH64:
362 return sStr64;
363 case ARCH32:
364 return sStr32;
365 case ARCH_BOTH:
366 return sStrBoth;
367 case ARCH_UNKNOWN: // fall through
368 default:
369 return sStrUnknown;
370 }
371}
372
373static Architecture fromBaseArchitecture(::android::hidl::base::V1_0::DebugInfo::Architecture a) {
374 switch (a) {
375 case ::android::hidl::base::V1_0::DebugInfo::Architecture::IS_64BIT:
376 return ARCH64;
377 case ::android::hidl::base::V1_0::DebugInfo::Architecture::IS_32BIT:
378 return ARCH32;
379 case ::android::hidl::base::V1_0::DebugInfo::Architecture::UNKNOWN: // fallthrough
380 default:
381 return ARCH_UNKNOWN;
382 }
383}
384
385void ListCommand::dumpTable() {
386 mServicesTable.description =
387 "All binderized services (registered services through hwservicemanager)";
388 mPassthroughRefTable.description =
389 "All interfaces that getService() has ever return as a passthrough interface;\n"
390 "PIDs / processes shown below might be inaccurate because the process\n"
391 "might have relinquished the interface or might have died.\n"
392 "The Server / Server CMD column can be ignored.\n"
393 "The Clients / Clients CMD column shows all process that have ever dlopen'ed \n"
394 "the library and successfully fetched the passthrough implementation.";
395 mImplementationsTable.description =
396 "All available passthrough implementations (all -impl.so files)";
397 forEachTable([this] (const Table &table) {
Yifan Hong6da06912017-05-12 16:56:43 -0700398 if (!mNeat) {
399 mOut << table.description << std::endl;
400 }
Yifan Hong443df792017-05-09 18:49:45 -0700401 mOut << std::left;
Yifan Hong6da06912017-05-12 16:56:43 -0700402 if (!mNeat) {
Steven Morelandd8e20192017-05-24 11:23:08 -0700403 printLine("Interface", "Transport", "Arch", "Thread Use", "Server",
404 "Server CMD", "PTR", "Clients", "Clients CMD");
Yifan Hong6da06912017-05-12 16:56:43 -0700405 }
Yifan Hong443df792017-05-09 18:49:45 -0700406
Yifan Hong443df792017-05-09 18:49:45 -0700407 for (const auto &entry : table) {
408 printLine(entry.interfaceName,
409 entry.transport,
410 getArchString(entry.arch),
Steven Morelandd8e20192017-05-24 11:23:08 -0700411 entry.getThreadUsage(),
Yifan Hong443df792017-05-09 18:49:45 -0700412 entry.serverPid == NO_PID ? "N/A" : std::to_string(entry.serverPid),
413 entry.serverCmdline,
414 entry.serverObjectAddress == NO_PTR ? "N/A" : toHexString(entry.serverObjectAddress),
415 join(entry.clientPids, " "),
416 join(entry.clientCmdlines, ";"));
417
Yifan Hong48dc9f82017-05-09 19:33:08 -0700418 // We're only interested in dumping debug info for already
419 // instantiated services. There's little value in dumping the
420 // debug info for a service we create on the fly, so we only operate
421 // on the "mServicesTable".
422 if (mEmitDebugInfo && &table == &mServicesTable) {
Yifan Hong443df792017-05-09 18:49:45 -0700423 auto pair = splitFirst(entry.interfaceName, '/');
Yifan Hong48dc9f82017-05-09 19:33:08 -0700424 mLshal.emitDebugInfo(pair.first, pair.second, {}, mOut.buf(),
425 NullableOStream<std::ostream>(nullptr));
Yifan Hong443df792017-05-09 18:49:45 -0700426 }
427 }
Yifan Hong6da06912017-05-12 16:56:43 -0700428 if (!mNeat) {
429 mOut << std::endl;
430 }
Yifan Hong443df792017-05-09 18:49:45 -0700431 });
432
433}
434
435void ListCommand::dump() {
436 if (mVintf) {
437 dumpVintf();
438 if (!!mFileOutput) {
439 mFileOutput.buf().close();
440 delete &mFileOutput.buf();
441 mFileOutput = nullptr;
442 }
443 mOut = std::cout;
444 } else {
445 dumpTable();
446 }
447}
448
449void ListCommand::putEntry(TableEntrySource source, TableEntry &&entry) {
450 Table *table = nullptr;
451 switch (source) {
452 case HWSERVICEMANAGER_LIST :
453 table = &mServicesTable; break;
454 case PTSERVICEMANAGER_REG_CLIENT :
455 table = &mPassthroughRefTable; break;
456 case LIST_DLLIB :
457 table = &mImplementationsTable; break;
458 default:
459 mErr << "Error: Unknown source of entry " << source << std::endl;
460 }
461 if (table) {
462 table->entries.push_back(std::forward<TableEntry>(entry));
463 }
464}
465
466Status ListCommand::fetchAllLibraries(const sp<IServiceManager> &manager) {
467 using namespace ::android::hardware;
468 using namespace ::android::hidl::manager::V1_0;
469 using namespace ::android::hidl::base::V1_0;
Yifan Hongf2d557b2017-05-24 19:45:02 -0700470 using std::literals::chrono_literals::operator""s;
471 auto ret = timeoutIPC(2s, manager, &IServiceManager::debugDump, [&] (const auto &infos) {
Yifan Hong443df792017-05-09 18:49:45 -0700472 std::map<std::string, TableEntry> entries;
473 for (const auto &info : infos) {
474 std::string interfaceName = std::string{info.interfaceName.c_str()} + "/" +
475 std::string{info.instanceName.c_str()};
476 entries.emplace(interfaceName, TableEntry{
477 .interfaceName = interfaceName,
478 .transport = "passthrough",
479 .serverPid = NO_PID,
480 .serverObjectAddress = NO_PTR,
Yifan Hongf2d557b2017-05-24 19:45:02 -0700481 .clientPids = info.clientPids,
Yifan Hong443df792017-05-09 18:49:45 -0700482 .arch = ARCH_UNKNOWN
483 }).first->second.arch |= fromBaseArchitecture(info.arch);
484 }
485 for (auto &&pair : entries) {
486 putEntry(LIST_DLLIB, std::move(pair.second));
487 }
488 });
489 if (!ret.isOk()) {
490 mErr << "Error: Failed to call list on getPassthroughServiceManager(): "
491 << ret.description() << std::endl;
492 return DUMP_ALL_LIBS_ERROR;
493 }
494 return OK;
495}
496
497Status ListCommand::fetchPassthrough(const sp<IServiceManager> &manager) {
498 using namespace ::android::hardware;
499 using namespace ::android::hardware::details;
500 using namespace ::android::hidl::manager::V1_0;
501 using namespace ::android::hidl::base::V1_0;
502 auto ret = timeoutIPC(manager, &IServiceManager::debugDump, [&] (const auto &infos) {
503 for (const auto &info : infos) {
504 if (info.clientPids.size() <= 0) {
505 continue;
506 }
507 putEntry(PTSERVICEMANAGER_REG_CLIENT, {
508 .interfaceName =
509 std::string{info.interfaceName.c_str()} + "/" +
510 std::string{info.instanceName.c_str()},
511 .transport = "passthrough",
512 .serverPid = info.clientPids.size() == 1 ? info.clientPids[0] : NO_PID,
513 .serverObjectAddress = NO_PTR,
514 .clientPids = info.clientPids,
515 .arch = fromBaseArchitecture(info.arch)
516 });
517 }
518 });
519 if (!ret.isOk()) {
520 mErr << "Error: Failed to call debugDump on defaultServiceManager(): "
521 << ret.description() << std::endl;
522 return DUMP_PASSTHROUGH_ERROR;
523 }
524 return OK;
525}
526
527Status ListCommand::fetchBinderized(const sp<IServiceManager> &manager) {
528 using namespace ::std;
529 using namespace ::android::hardware;
530 using namespace ::android::hidl::manager::V1_0;
531 using namespace ::android::hidl::base::V1_0;
532 const std::string mode = "hwbinder";
533
534 hidl_vec<hidl_string> fqInstanceNames;
535 // copying out for timeoutIPC
536 auto listRet = timeoutIPC(manager, &IServiceManager::list, [&] (const auto &names) {
537 fqInstanceNames = names;
538 });
539 if (!listRet.isOk()) {
540 mErr << "Error: Failed to list services for " << mode << ": "
541 << listRet.description() << std::endl;
542 return DUMP_BINDERIZED_ERROR;
543 }
544
545 Status status = OK;
546 // server pid, .ptr value of binder object, child pids
547 std::map<std::string, DebugInfo> allDebugInfos;
Steven Morelandd8e20192017-05-24 11:23:08 -0700548 std::map<pid_t, PidInfo> allPids;
Yifan Hong443df792017-05-09 18:49:45 -0700549 for (const auto &fqInstanceName : fqInstanceNames) {
550 const auto pair = splitFirst(fqInstanceName, '/');
551 const auto &serviceName = pair.first;
552 const auto &instanceName = pair.second;
553 auto getRet = timeoutIPC(manager, &IServiceManager::get, serviceName, instanceName);
554 if (!getRet.isOk()) {
555 mErr << "Warning: Skipping \"" << fqInstanceName << "\": "
556 << "cannot be fetched from service manager:"
557 << getRet.description() << std::endl;
558 status |= DUMP_BINDERIZED_ERROR;
559 continue;
560 }
561 sp<IBase> service = getRet;
562 if (service == nullptr) {
563 mErr << "Warning: Skipping \"" << fqInstanceName << "\": "
564 << "cannot be fetched from service manager (null)"
565 << std::endl;
566 status |= DUMP_BINDERIZED_ERROR;
567 continue;
568 }
569 auto debugRet = timeoutIPC(service, &IBase::getDebugInfo, [&] (const auto &debugInfo) {
570 allDebugInfos[fqInstanceName] = debugInfo;
571 if (debugInfo.pid >= 0) {
Steven Morelandd8e20192017-05-24 11:23:08 -0700572 allPids[static_cast<pid_t>(debugInfo.pid)] = PidInfo();
Yifan Hong443df792017-05-09 18:49:45 -0700573 }
574 });
575 if (!debugRet.isOk()) {
576 mErr << "Warning: Skipping \"" << fqInstanceName << "\": "
577 << "debugging information cannot be retrieved:"
578 << debugRet.description() << std::endl;
579 status |= DUMP_BINDERIZED_ERROR;
580 }
581 }
Steven Morelandd8e20192017-05-24 11:23:08 -0700582
Yifan Hong443df792017-05-09 18:49:45 -0700583 for (auto &pair : allPids) {
584 pid_t serverPid = pair.first;
Steven Morelandd8e20192017-05-24 11:23:08 -0700585 if (!getPidInfo(serverPid, &allPids[serverPid])) {
Yifan Hong443df792017-05-09 18:49:45 -0700586 mErr << "Warning: no information for PID " << serverPid
587 << ", are you root?" << std::endl;
588 status |= DUMP_BINDERIZED_ERROR;
589 }
590 }
591 for (const auto &fqInstanceName : fqInstanceNames) {
592 auto it = allDebugInfos.find(fqInstanceName);
593 if (it == allDebugInfos.end()) {
594 putEntry(HWSERVICEMANAGER_LIST, {
595 .interfaceName = fqInstanceName,
596 .transport = mode,
597 .serverPid = NO_PID,
598 .serverObjectAddress = NO_PTR,
599 .clientPids = {},
Steven Morelandd8e20192017-05-24 11:23:08 -0700600 .threadUsage = 0,
601 .threadCount = 0,
Yifan Hong443df792017-05-09 18:49:45 -0700602 .arch = ARCH_UNKNOWN
603 });
604 continue;
605 }
606 const DebugInfo &info = it->second;
Steven Morelandd8e20192017-05-24 11:23:08 -0700607 bool writePidInfo = info.pid != NO_PID && info.ptr != NO_PTR;
608
Yifan Hong443df792017-05-09 18:49:45 -0700609 putEntry(HWSERVICEMANAGER_LIST, {
610 .interfaceName = fqInstanceName,
611 .transport = mode,
612 .serverPid = info.pid,
613 .serverObjectAddress = info.ptr,
Steven Morelandd8e20192017-05-24 11:23:08 -0700614 .clientPids = writePidInfo ? allPids[info.pid].refPids[info.ptr] : Pids{},
615 .threadUsage = writePidInfo ? allPids[info.pid].threadUsage : 0,
616 .threadCount = writePidInfo ? allPids[info.pid].threadCount : 0,
Yifan Hong443df792017-05-09 18:49:45 -0700617 .arch = fromBaseArchitecture(info.arch),
618 });
619 }
620 return status;
621}
622
623Status ListCommand::fetch() {
624 Status status = OK;
Yifan Hong9881df92017-05-10 14:33:05 -0700625 auto bManager = mLshal.serviceManager();
Yifan Hong443df792017-05-09 18:49:45 -0700626 if (bManager == nullptr) {
627 mErr << "Failed to get defaultServiceManager()!" << std::endl;
628 status |= NO_BINDERIZED_MANAGER;
629 } else {
630 status |= fetchBinderized(bManager);
631 // Passthrough PIDs are registered to the binderized manager as well.
632 status |= fetchPassthrough(bManager);
633 }
634
Yifan Hong9881df92017-05-10 14:33:05 -0700635 auto pManager = mLshal.passthroughManager();
Yifan Hong443df792017-05-09 18:49:45 -0700636 if (pManager == nullptr) {
637 mErr << "Failed to get getPassthroughServiceManager()!" << std::endl;
638 status |= NO_PASSTHROUGH_MANAGER;
639 } else {
640 status |= fetchAllLibraries(pManager);
641 }
642 return status;
643}
644
645Status ListCommand::parseArgs(const std::string &command, const Arg &arg) {
646 static struct option longOptions[] = {
647 // long options with short alternatives
648 {"help", no_argument, 0, 'h' },
649 {"interface", no_argument, 0, 'i' },
650 {"transport", no_argument, 0, 't' },
651 {"arch", no_argument, 0, 'r' },
652 {"pid", no_argument, 0, 'p' },
653 {"address", no_argument, 0, 'a' },
654 {"clients", no_argument, 0, 'c' },
Steven Morelandd8e20192017-05-24 11:23:08 -0700655 {"threads", no_argument, 0, 'e' },
Yifan Hong443df792017-05-09 18:49:45 -0700656 {"cmdline", no_argument, 0, 'm' },
657 {"debug", optional_argument, 0, 'd' },
658
659 // long options without short alternatives
660 {"sort", required_argument, 0, 's' },
661 {"init-vintf",optional_argument, 0, 'v' },
Yifan Hong6da06912017-05-12 16:56:43 -0700662 {"neat", no_argument, 0, 'n' },
Yifan Hong443df792017-05-09 18:49:45 -0700663 { 0, 0, 0, 0 }
664 };
665
666 int optionIndex;
667 int c;
668 // Lshal::parseArgs has set optind to the next option to parse
669 for (;;) {
670 // using getopt_long in case we want to add other options in the future
671 c = getopt_long(arg.argc, arg.argv,
Steven Morelandd8e20192017-05-24 11:23:08 -0700672 "hitrpacmde", longOptions, &optionIndex);
Yifan Hong443df792017-05-09 18:49:45 -0700673 if (c == -1) {
674 break;
675 }
676 switch (c) {
677 case 's': {
678 if (strcmp(optarg, "interface") == 0 || strcmp(optarg, "i") == 0) {
679 mSortColumn = TableEntry::sortByInterfaceName;
680 } else if (strcmp(optarg, "pid") == 0 || strcmp(optarg, "p") == 0) {
681 mSortColumn = TableEntry::sortByServerPid;
682 } else {
683 mErr << "Unrecognized sorting column: " << optarg << std::endl;
684 mLshal.usage(command);
685 return USAGE;
686 }
687 break;
688 }
689 case 'v': {
690 if (optarg) {
691 mFileOutput = new std::ofstream{optarg};
692 mOut = mFileOutput;
693 if (!mFileOutput.buf().is_open()) {
694 mErr << "Could not open file '" << optarg << "'." << std::endl;
695 return IO_ERROR;
696 }
697 }
698 mVintf = true;
699 }
700 case 'i': {
701 mSelectedColumns |= ENABLE_INTERFACE_NAME;
702 break;
703 }
704 case 't': {
705 mSelectedColumns |= ENABLE_TRANSPORT;
706 break;
707 }
708 case 'r': {
709 mSelectedColumns |= ENABLE_ARCH;
710 break;
711 }
712 case 'p': {
713 mSelectedColumns |= ENABLE_SERVER_PID;
714 break;
715 }
716 case 'a': {
717 mSelectedColumns |= ENABLE_SERVER_ADDR;
718 break;
719 }
720 case 'c': {
721 mSelectedColumns |= ENABLE_CLIENT_PIDS;
722 break;
723 }
Steven Morelandd8e20192017-05-24 11:23:08 -0700724 case 'e': {
725 mSelectedColumns |= ENABLE_THREADS;
726 break;
727 }
Yifan Hong443df792017-05-09 18:49:45 -0700728 case 'm': {
729 mEnableCmdlines = true;
730 break;
731 }
732 case 'd': {
733 mEmitDebugInfo = true;
734
735 if (optarg) {
736 mFileOutput = new std::ofstream{optarg};
737 mOut = mFileOutput;
738 if (!mFileOutput.buf().is_open()) {
739 mErr << "Could not open file '" << optarg << "'." << std::endl;
740 return IO_ERROR;
741 }
742 chown(optarg, AID_SHELL, AID_SHELL);
743 }
744 break;
745 }
Yifan Hong6da06912017-05-12 16:56:43 -0700746 case 'n': {
747 mNeat = true;
748 break;
749 }
Yifan Hong443df792017-05-09 18:49:45 -0700750 case 'h': // falls through
751 default: // see unrecognized options
752 mLshal.usage(command);
753 return USAGE;
754 }
755 }
756 if (optind < arg.argc) {
757 // see non option
758 mErr << "Unrecognized option `" << arg.argv[optind] << "`" << std::endl;
759 }
760
761 if (mSelectedColumns == 0) {
Steven Morelandd8e20192017-05-24 11:23:08 -0700762 mSelectedColumns = ENABLE_INTERFACE_NAME | ENABLE_SERVER_PID | ENABLE_CLIENT_PIDS | ENABLE_THREADS;
Yifan Hong443df792017-05-09 18:49:45 -0700763 }
764 return OK;
765}
766
767Status ListCommand::main(const std::string &command, const Arg &arg) {
768 Status status = parseArgs(command, arg);
769 if (status != OK) {
770 return status;
771 }
772 status = fetch();
773 postprocess();
774 dump();
775 return status;
776}
777
778} // namespace lshal
779} // namespace android
780