blob: 6ee162b017e9c5024dc22869dc95382173707635 [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
Yifan Hong77c87822017-06-19 15:47:39 -0700209static inline bool findAndBumpVersion(vintf::ManifestHal* hal, const vintf::Version& version) {
210 for (vintf::Version& v : hal->versions) {
211 if (v.majorVer == version.majorVer) {
212 v.minorVer = std::max(v.minorVer, version.minorVer);
213 return true;
214 }
215 }
216 return false;
217}
218
Yifan Hong443df792017-05-09 18:49:45 -0700219void ListCommand::dumpVintf() const {
Yifan Hong236301c2017-06-19 12:27:08 -0700220 using vintf::operator|=;
Yifan Hong443df792017-05-09 18:49:45 -0700221 mOut << "<!-- " << std::endl
222 << " This is a skeleton device manifest. Notes: " << std::endl
223 << " 1. android.hidl.*, android.frameworks.*, android.system.* are not included." << std::endl
224 << " 2. If a HAL is supported in both hwbinder and passthrough transport, " << std::endl
225 << " only hwbinder is shown." << std::endl
226 << " 3. It is likely that HALs in passthrough transport does not have" << std::endl
227 << " <interface> declared; users will have to write them by hand." << std::endl
Yifan Hong77c87822017-06-19 15:47:39 -0700228 << " 4. A HAL with lower minor version can be overridden by a HAL with" << std::endl
229 << " higher minor version if they have the same name and major version." << std::endl
230 << " 5. sepolicy version is set to 0.0. It is recommended that the entry" << std::endl
Yifan Hong443df792017-05-09 18:49:45 -0700231 << " is removed from the manifest file and written by assemble_vintf" << std::endl
232 << " at build time." << std::endl
233 << "-->" << std::endl;
234
235 vintf::HalManifest manifest;
236 forEachTable([this, &manifest] (const Table &table) {
237 for (const TableEntry &entry : table) {
238
239 std::string fqInstanceName = entry.interfaceName;
240
241 if (&table == &mImplementationsTable) {
242 // Quick hack to work around *'s
243 replaceAll(&fqInstanceName, '*', 'D');
244 }
245 auto splittedFqInstanceName = splitFirst(fqInstanceName, '/');
246 FQName fqName(splittedFqInstanceName.first);
247 if (!fqName.isValid()) {
248 mErr << "Warning: '" << splittedFqInstanceName.first
249 << "' is not a valid FQName." << std::endl;
250 continue;
251 }
252 // Strip out system libs.
253 if (fqName.inPackage("android.hidl") ||
254 fqName.inPackage("android.frameworks") ||
255 fqName.inPackage("android.system")) {
256 continue;
257 }
258 std::string interfaceName =
259 &table == &mImplementationsTable ? "" : fqName.name();
260 std::string instanceName =
261 &table == &mImplementationsTable ? "" : splittedFqInstanceName.second;
262
263 vintf::Version version{fqName.getPackageMajorVersion(),
264 fqName.getPackageMinorVersion()};
265 vintf::Transport transport;
266 vintf::Arch arch;
267 if (entry.transport == "hwbinder") {
268 transport = vintf::Transport::HWBINDER;
269 arch = vintf::Arch::ARCH_EMPTY;
270 } else if (entry.transport == "passthrough") {
271 transport = vintf::Transport::PASSTHROUGH;
272 switch (entry.arch) {
273 case lshal::ARCH32:
274 arch = vintf::Arch::ARCH_32; break;
275 case lshal::ARCH64:
276 arch = vintf::Arch::ARCH_64; break;
277 case lshal::ARCH_BOTH:
278 arch = vintf::Arch::ARCH_32_64; break;
279 case lshal::ARCH_UNKNOWN: // fallthrough
280 default:
281 mErr << "Warning: '" << fqName.package()
282 << "' doesn't have bitness info, assuming 32+64." << std::endl;
283 arch = vintf::Arch::ARCH_32_64;
284 }
285 } else {
286 mErr << "Warning: '" << entry.transport << "' is not a valid transport." << std::endl;
287 continue;
288 }
289
290 bool done = false;
291 for (vintf::ManifestHal *hal : manifest.getHals(fqName.package())) {
292 if (hal->transport() != transport) {
293 if (transport != vintf::Transport::PASSTHROUGH) {
Yifan Hong236301c2017-06-19 12:27:08 -0700294 mErr << "Fatal: should not reach here. Generated result may be wrong for '"
295 << hal->name << "'."
Yifan Hong443df792017-05-09 18:49:45 -0700296 << std::endl;
297 }
298 done = true;
299 break;
300 }
Yifan Hong77c87822017-06-19 15:47:39 -0700301 if (findAndBumpVersion(hal, version)) {
Yifan Hong443df792017-05-09 18:49:45 -0700302 if (&table != &mImplementationsTable) {
303 hal->interfaces[interfaceName].name = interfaceName;
304 hal->interfaces[interfaceName].instances.insert(instanceName);
305 }
Yifan Hong236301c2017-06-19 12:27:08 -0700306 hal->transportArch.arch |= arch;
Yifan Hong443df792017-05-09 18:49:45 -0700307 done = true;
308 break;
309 }
310 }
311 if (done) {
312 continue; // to next TableEntry
313 }
314 decltype(vintf::ManifestHal::interfaces) interfaces;
315 if (&table != &mImplementationsTable) {
316 interfaces[interfaceName].name = interfaceName;
317 interfaces[interfaceName].instances.insert(instanceName);
318 }
319 if (!manifest.add(vintf::ManifestHal{
320 .format = vintf::HalFormat::HIDL,
321 .name = fqName.package(),
322 .versions = {version},
323 .transportArch = {transport, arch},
324 .interfaces = interfaces})) {
325 mErr << "Warning: cannot add hal '" << fqInstanceName << "'" << std::endl;
326 }
327 }
328 });
329 mOut << vintf::gHalManifestConverter(manifest);
330}
331
Yifan Hong443df792017-05-09 18:49:45 -0700332static Architecture fromBaseArchitecture(::android::hidl::base::V1_0::DebugInfo::Architecture a) {
333 switch (a) {
334 case ::android::hidl::base::V1_0::DebugInfo::Architecture::IS_64BIT:
335 return ARCH64;
336 case ::android::hidl::base::V1_0::DebugInfo::Architecture::IS_32BIT:
337 return ARCH32;
338 case ::android::hidl::base::V1_0::DebugInfo::Architecture::UNKNOWN: // fallthrough
339 default:
340 return ARCH_UNKNOWN;
341 }
342}
343
344void ListCommand::dumpTable() {
Yifan Hong1bc1e9f2017-08-29 17:28:12 -0700345 if (mNeat) {
Yifan Hongd4a77e82017-09-06 19:40:24 -0700346 MergedTable({&mServicesTable, &mPassthroughRefTable, &mImplementationsTable})
347 .createTextTable().dump(mOut.buf());
Yifan Hong1bc1e9f2017-08-29 17:28:12 -0700348 return;
349 }
350
Yifan Hongd4a77e82017-09-06 19:40:24 -0700351 mServicesTable.setDescription(
352 "All binderized services (registered services through hwservicemanager)");
353 mPassthroughRefTable.setDescription(
Yifan Hong443df792017-05-09 18:49:45 -0700354 "All interfaces that getService() has ever return as a passthrough interface;\n"
355 "PIDs / processes shown below might be inaccurate because the process\n"
356 "might have relinquished the interface or might have died.\n"
357 "The Server / Server CMD column can be ignored.\n"
358 "The Clients / Clients CMD column shows all process that have ever dlopen'ed \n"
Yifan Hongd4a77e82017-09-06 19:40:24 -0700359 "the library and successfully fetched the passthrough implementation.");
360 mImplementationsTable.setDescription(
361 "All available passthrough implementations (all -impl.so files)");
Yifan Hong1bc1e9f2017-08-29 17:28:12 -0700362
363 forEachTable([this](const Table &table) {
Yifan Hong1bc1e9f2017-08-29 17:28:12 -0700364
Yifan Hongd4a77e82017-09-06 19:40:24 -0700365 // We're only interested in dumping debug info for already
366 // instantiated services. There's little value in dumping the
367 // debug info for a service we create on the fly, so we only operate
368 // on the "mServicesTable".
369 std::function<std::string(const std::string&)> emitDebugInfo = nullptr;
370 if (mEmitDebugInfo && &table == &mServicesTable) {
371 emitDebugInfo = [this](const auto& iName) {
Yifan Hong1bc1e9f2017-08-29 17:28:12 -0700372 std::stringstream out;
Yifan Hongd4a77e82017-09-06 19:40:24 -0700373 auto pair = splitFirst(iName, '/');
Yifan Hong1bc1e9f2017-08-29 17:28:12 -0700374 mLshal.emitDebugInfo(pair.first, pair.second, {}, out,
375 NullableOStream<std::ostream>(nullptr));
Yifan Hongd4a77e82017-09-06 19:40:24 -0700376 return out.str();
377 };
Yifan Hong443df792017-05-09 18:49:45 -0700378 }
Yifan Hongd4a77e82017-09-06 19:40:24 -0700379 table.createTextTable(mNeat, emitDebugInfo).dump(mOut.buf());
380 mOut << std::endl;
Yifan Hong1bc1e9f2017-08-29 17:28:12 -0700381 });
Yifan Hong443df792017-05-09 18:49:45 -0700382}
383
384void ListCommand::dump() {
385 if (mVintf) {
386 dumpVintf();
387 if (!!mFileOutput) {
388 mFileOutput.buf().close();
389 delete &mFileOutput.buf();
390 mFileOutput = nullptr;
391 }
392 mOut = std::cout;
393 } else {
394 dumpTable();
395 }
396}
397
398void ListCommand::putEntry(TableEntrySource source, TableEntry &&entry) {
399 Table *table = nullptr;
400 switch (source) {
401 case HWSERVICEMANAGER_LIST :
402 table = &mServicesTable; break;
403 case PTSERVICEMANAGER_REG_CLIENT :
404 table = &mPassthroughRefTable; break;
405 case LIST_DLLIB :
406 table = &mImplementationsTable; break;
407 default:
408 mErr << "Error: Unknown source of entry " << source << std::endl;
409 }
410 if (table) {
Yifan Hongd4a77e82017-09-06 19:40:24 -0700411 table->add(std::forward<TableEntry>(entry));
Yifan Hong443df792017-05-09 18:49:45 -0700412 }
413}
414
415Status ListCommand::fetchAllLibraries(const sp<IServiceManager> &manager) {
416 using namespace ::android::hardware;
417 using namespace ::android::hidl::manager::V1_0;
418 using namespace ::android::hidl::base::V1_0;
Yifan Hongf2d557b2017-05-24 19:45:02 -0700419 using std::literals::chrono_literals::operator""s;
420 auto ret = timeoutIPC(2s, manager, &IServiceManager::debugDump, [&] (const auto &infos) {
Yifan Hong443df792017-05-09 18:49:45 -0700421 std::map<std::string, TableEntry> entries;
422 for (const auto &info : infos) {
423 std::string interfaceName = std::string{info.interfaceName.c_str()} + "/" +
424 std::string{info.instanceName.c_str()};
425 entries.emplace(interfaceName, TableEntry{
426 .interfaceName = interfaceName,
427 .transport = "passthrough",
428 .serverPid = NO_PID,
429 .serverObjectAddress = NO_PTR,
Yifan Hongf2d557b2017-05-24 19:45:02 -0700430 .clientPids = info.clientPids,
Yifan Hong443df792017-05-09 18:49:45 -0700431 .arch = ARCH_UNKNOWN
432 }).first->second.arch |= fromBaseArchitecture(info.arch);
433 }
434 for (auto &&pair : entries) {
435 putEntry(LIST_DLLIB, std::move(pair.second));
436 }
437 });
438 if (!ret.isOk()) {
439 mErr << "Error: Failed to call list on getPassthroughServiceManager(): "
440 << ret.description() << std::endl;
441 return DUMP_ALL_LIBS_ERROR;
442 }
443 return OK;
444}
445
446Status ListCommand::fetchPassthrough(const sp<IServiceManager> &manager) {
447 using namespace ::android::hardware;
448 using namespace ::android::hardware::details;
449 using namespace ::android::hidl::manager::V1_0;
450 using namespace ::android::hidl::base::V1_0;
451 auto ret = timeoutIPC(manager, &IServiceManager::debugDump, [&] (const auto &infos) {
452 for (const auto &info : infos) {
453 if (info.clientPids.size() <= 0) {
454 continue;
455 }
456 putEntry(PTSERVICEMANAGER_REG_CLIENT, {
457 .interfaceName =
458 std::string{info.interfaceName.c_str()} + "/" +
459 std::string{info.instanceName.c_str()},
460 .transport = "passthrough",
461 .serverPid = info.clientPids.size() == 1 ? info.clientPids[0] : NO_PID,
462 .serverObjectAddress = NO_PTR,
463 .clientPids = info.clientPids,
464 .arch = fromBaseArchitecture(info.arch)
465 });
466 }
467 });
468 if (!ret.isOk()) {
469 mErr << "Error: Failed to call debugDump on defaultServiceManager(): "
470 << ret.description() << std::endl;
471 return DUMP_PASSTHROUGH_ERROR;
472 }
473 return OK;
474}
475
476Status ListCommand::fetchBinderized(const sp<IServiceManager> &manager) {
477 using namespace ::std;
478 using namespace ::android::hardware;
479 using namespace ::android::hidl::manager::V1_0;
480 using namespace ::android::hidl::base::V1_0;
481 const std::string mode = "hwbinder";
482
483 hidl_vec<hidl_string> fqInstanceNames;
484 // copying out for timeoutIPC
485 auto listRet = timeoutIPC(manager, &IServiceManager::list, [&] (const auto &names) {
486 fqInstanceNames = names;
487 });
488 if (!listRet.isOk()) {
489 mErr << "Error: Failed to list services for " << mode << ": "
490 << listRet.description() << std::endl;
491 return DUMP_BINDERIZED_ERROR;
492 }
493
494 Status status = OK;
495 // server pid, .ptr value of binder object, child pids
496 std::map<std::string, DebugInfo> allDebugInfos;
Steven Morelandd8e20192017-05-24 11:23:08 -0700497 std::map<pid_t, PidInfo> allPids;
Yifan Hong443df792017-05-09 18:49:45 -0700498 for (const auto &fqInstanceName : fqInstanceNames) {
499 const auto pair = splitFirst(fqInstanceName, '/');
500 const auto &serviceName = pair.first;
501 const auto &instanceName = pair.second;
502 auto getRet = timeoutIPC(manager, &IServiceManager::get, serviceName, instanceName);
503 if (!getRet.isOk()) {
504 mErr << "Warning: Skipping \"" << fqInstanceName << "\": "
505 << "cannot be fetched from service manager:"
506 << getRet.description() << std::endl;
507 status |= DUMP_BINDERIZED_ERROR;
508 continue;
509 }
510 sp<IBase> service = getRet;
511 if (service == nullptr) {
512 mErr << "Warning: Skipping \"" << fqInstanceName << "\": "
513 << "cannot be fetched from service manager (null)"
514 << std::endl;
515 status |= DUMP_BINDERIZED_ERROR;
516 continue;
517 }
518 auto debugRet = timeoutIPC(service, &IBase::getDebugInfo, [&] (const auto &debugInfo) {
519 allDebugInfos[fqInstanceName] = debugInfo;
520 if (debugInfo.pid >= 0) {
Steven Morelandd8e20192017-05-24 11:23:08 -0700521 allPids[static_cast<pid_t>(debugInfo.pid)] = PidInfo();
Yifan Hong443df792017-05-09 18:49:45 -0700522 }
523 });
524 if (!debugRet.isOk()) {
525 mErr << "Warning: Skipping \"" << fqInstanceName << "\": "
526 << "debugging information cannot be retrieved:"
527 << debugRet.description() << std::endl;
528 status |= DUMP_BINDERIZED_ERROR;
529 }
530 }
Steven Morelandd8e20192017-05-24 11:23:08 -0700531
Yifan Hong443df792017-05-09 18:49:45 -0700532 for (auto &pair : allPids) {
533 pid_t serverPid = pair.first;
Steven Morelandd8e20192017-05-24 11:23:08 -0700534 if (!getPidInfo(serverPid, &allPids[serverPid])) {
Yifan Hong443df792017-05-09 18:49:45 -0700535 mErr << "Warning: no information for PID " << serverPid
536 << ", are you root?" << std::endl;
537 status |= DUMP_BINDERIZED_ERROR;
538 }
539 }
540 for (const auto &fqInstanceName : fqInstanceNames) {
541 auto it = allDebugInfos.find(fqInstanceName);
542 if (it == allDebugInfos.end()) {
543 putEntry(HWSERVICEMANAGER_LIST, {
544 .interfaceName = fqInstanceName,
545 .transport = mode,
546 .serverPid = NO_PID,
547 .serverObjectAddress = NO_PTR,
548 .clientPids = {},
Steven Morelandd8e20192017-05-24 11:23:08 -0700549 .threadUsage = 0,
550 .threadCount = 0,
Yifan Hong443df792017-05-09 18:49:45 -0700551 .arch = ARCH_UNKNOWN
552 });
553 continue;
554 }
555 const DebugInfo &info = it->second;
Steven Morelandd8e20192017-05-24 11:23:08 -0700556 bool writePidInfo = info.pid != NO_PID && info.ptr != NO_PTR;
557
Yifan Hong443df792017-05-09 18:49:45 -0700558 putEntry(HWSERVICEMANAGER_LIST, {
559 .interfaceName = fqInstanceName,
560 .transport = mode,
561 .serverPid = info.pid,
562 .serverObjectAddress = info.ptr,
Steven Morelandd8e20192017-05-24 11:23:08 -0700563 .clientPids = writePidInfo ? allPids[info.pid].refPids[info.ptr] : Pids{},
564 .threadUsage = writePidInfo ? allPids[info.pid].threadUsage : 0,
565 .threadCount = writePidInfo ? allPids[info.pid].threadCount : 0,
Yifan Hong443df792017-05-09 18:49:45 -0700566 .arch = fromBaseArchitecture(info.arch),
567 });
568 }
569 return status;
570}
571
572Status ListCommand::fetch() {
573 Status status = OK;
Yifan Hong9881df92017-05-10 14:33:05 -0700574 auto bManager = mLshal.serviceManager();
Yifan Hong443df792017-05-09 18:49:45 -0700575 if (bManager == nullptr) {
576 mErr << "Failed to get defaultServiceManager()!" << std::endl;
577 status |= NO_BINDERIZED_MANAGER;
578 } else {
579 status |= fetchBinderized(bManager);
580 // Passthrough PIDs are registered to the binderized manager as well.
581 status |= fetchPassthrough(bManager);
582 }
583
Yifan Hong9881df92017-05-10 14:33:05 -0700584 auto pManager = mLshal.passthroughManager();
Yifan Hong443df792017-05-09 18:49:45 -0700585 if (pManager == nullptr) {
586 mErr << "Failed to get getPassthroughServiceManager()!" << std::endl;
587 status |= NO_PASSTHROUGH_MANAGER;
588 } else {
589 status |= fetchAllLibraries(pManager);
590 }
591 return status;
592}
593
594Status ListCommand::parseArgs(const std::string &command, const Arg &arg) {
595 static struct option longOptions[] = {
596 // long options with short alternatives
597 {"help", no_argument, 0, 'h' },
598 {"interface", no_argument, 0, 'i' },
599 {"transport", no_argument, 0, 't' },
600 {"arch", no_argument, 0, 'r' },
601 {"pid", no_argument, 0, 'p' },
602 {"address", no_argument, 0, 'a' },
603 {"clients", no_argument, 0, 'c' },
Steven Morelandd8e20192017-05-24 11:23:08 -0700604 {"threads", no_argument, 0, 'e' },
Yifan Hong443df792017-05-09 18:49:45 -0700605 {"cmdline", no_argument, 0, 'm' },
606 {"debug", optional_argument, 0, 'd' },
607
608 // long options without short alternatives
609 {"sort", required_argument, 0, 's' },
610 {"init-vintf",optional_argument, 0, 'v' },
Yifan Hong6da06912017-05-12 16:56:43 -0700611 {"neat", no_argument, 0, 'n' },
Yifan Hong443df792017-05-09 18:49:45 -0700612 { 0, 0, 0, 0 }
613 };
614
Yifan Hongd4a77e82017-09-06 19:40:24 -0700615 std::vector<TableColumnType> selectedColumns;
616 bool enableCmdlines = false;
617
Yifan Hong443df792017-05-09 18:49:45 -0700618 int optionIndex;
619 int c;
620 // Lshal::parseArgs has set optind to the next option to parse
621 for (;;) {
622 // using getopt_long in case we want to add other options in the future
623 c = getopt_long(arg.argc, arg.argv,
Steven Morelandd8e20192017-05-24 11:23:08 -0700624 "hitrpacmde", longOptions, &optionIndex);
Yifan Hong443df792017-05-09 18:49:45 -0700625 if (c == -1) {
626 break;
627 }
628 switch (c) {
629 case 's': {
630 if (strcmp(optarg, "interface") == 0 || strcmp(optarg, "i") == 0) {
631 mSortColumn = TableEntry::sortByInterfaceName;
632 } else if (strcmp(optarg, "pid") == 0 || strcmp(optarg, "p") == 0) {
633 mSortColumn = TableEntry::sortByServerPid;
634 } else {
635 mErr << "Unrecognized sorting column: " << optarg << std::endl;
636 mLshal.usage(command);
637 return USAGE;
638 }
639 break;
640 }
641 case 'v': {
642 if (optarg) {
643 mFileOutput = new std::ofstream{optarg};
644 mOut = mFileOutput;
645 if (!mFileOutput.buf().is_open()) {
646 mErr << "Could not open file '" << optarg << "'." << std::endl;
647 return IO_ERROR;
648 }
649 }
650 mVintf = true;
651 }
652 case 'i': {
Yifan Hongd4a77e82017-09-06 19:40:24 -0700653 selectedColumns.push_back(TableColumnType::INTERFACE_NAME);
Yifan Hong443df792017-05-09 18:49:45 -0700654 break;
655 }
656 case 't': {
Yifan Hongd4a77e82017-09-06 19:40:24 -0700657 selectedColumns.push_back(TableColumnType::TRANSPORT);
Yifan Hong443df792017-05-09 18:49:45 -0700658 break;
659 }
660 case 'r': {
Yifan Hongd4a77e82017-09-06 19:40:24 -0700661 selectedColumns.push_back(TableColumnType::ARCH);
Yifan Hong443df792017-05-09 18:49:45 -0700662 break;
663 }
664 case 'p': {
Yifan Hongd4a77e82017-09-06 19:40:24 -0700665 selectedColumns.push_back(TableColumnType::SERVER_PID);
Yifan Hong443df792017-05-09 18:49:45 -0700666 break;
667 }
668 case 'a': {
Yifan Hongd4a77e82017-09-06 19:40:24 -0700669 selectedColumns.push_back(TableColumnType::SERVER_ADDR);
Yifan Hong443df792017-05-09 18:49:45 -0700670 break;
671 }
672 case 'c': {
Yifan Hongd4a77e82017-09-06 19:40:24 -0700673 selectedColumns.push_back(TableColumnType::CLIENT_PIDS);
Yifan Hong443df792017-05-09 18:49:45 -0700674 break;
675 }
Steven Morelandd8e20192017-05-24 11:23:08 -0700676 case 'e': {
Yifan Hongd4a77e82017-09-06 19:40:24 -0700677 selectedColumns.push_back(TableColumnType::THREADS);
Steven Morelandd8e20192017-05-24 11:23:08 -0700678 break;
679 }
Yifan Hong443df792017-05-09 18:49:45 -0700680 case 'm': {
Yifan Hongd4a77e82017-09-06 19:40:24 -0700681 enableCmdlines = true;
Yifan Hong443df792017-05-09 18:49:45 -0700682 break;
683 }
684 case 'd': {
685 mEmitDebugInfo = true;
686
687 if (optarg) {
688 mFileOutput = new std::ofstream{optarg};
689 mOut = mFileOutput;
690 if (!mFileOutput.buf().is_open()) {
691 mErr << "Could not open file '" << optarg << "'." << std::endl;
692 return IO_ERROR;
693 }
694 chown(optarg, AID_SHELL, AID_SHELL);
695 }
696 break;
697 }
Yifan Hong6da06912017-05-12 16:56:43 -0700698 case 'n': {
699 mNeat = true;
700 break;
701 }
Yifan Hong443df792017-05-09 18:49:45 -0700702 case 'h': // falls through
703 default: // see unrecognized options
704 mLshal.usage(command);
705 return USAGE;
706 }
707 }
708 if (optind < arg.argc) {
709 // see non option
710 mErr << "Unrecognized option `" << arg.argv[optind] << "`" << std::endl;
Yifan Hong1bc1e9f2017-08-29 17:28:12 -0700711 mLshal.usage(command);
712 return USAGE;
713 }
714
715 if (mNeat && mEmitDebugInfo) {
716 mErr << "Error: --neat should not be used with --debug." << std::endl;
717 mLshal.usage(command);
718 return USAGE;
Yifan Hong443df792017-05-09 18:49:45 -0700719 }
720
Yifan Hongd4a77e82017-09-06 19:40:24 -0700721 if (selectedColumns.empty()) {
722 selectedColumns = {TableColumnType::INTERFACE_NAME, TableColumnType::THREADS,
Yifan Hong05494a52017-08-29 18:50:00 -0700723 TableColumnType::SERVER_PID, TableColumnType::CLIENT_PIDS};
Yifan Hong443df792017-05-09 18:49:45 -0700724 }
Yifan Hongd4a77e82017-09-06 19:40:24 -0700725
726 if (enableCmdlines) {
727 for (size_t i = 0; i < selectedColumns.size(); ++i) {
728 if (selectedColumns[i] == TableColumnType::SERVER_PID) {
729 selectedColumns[i] = TableColumnType::SERVER_CMD;
730 }
731 if (selectedColumns[i] == TableColumnType::CLIENT_PIDS) {
732 selectedColumns[i] = TableColumnType::CLIENT_CMDS;
733 }
734 }
735 }
736
737 forEachTable([&selectedColumns] (Table& table) {
738 table.setSelectedColumns(selectedColumns);
739 });
740
Yifan Hong443df792017-05-09 18:49:45 -0700741 return OK;
742}
743
744Status ListCommand::main(const std::string &command, const Arg &arg) {
745 Status status = parseArgs(command, arg);
746 if (status != OK) {
747 return status;
748 }
749 status = fetch();
750 postprocess();
751 dump();
752 return status;
753}
754
755} // namespace lshal
756} // namespace android
Yifan Hong05494a52017-08-29 18:50:00 -0700757