blob: 71ac25bdba40ede428599dfaea838b4f6f368633 [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
Yifan Hong76ac14a2017-09-08 14:59:04 -070047NullableOStream<std::ostream> ListCommand::out() const {
48 return mLshal.out();
49}
50
51NullableOStream<std::ostream> ListCommand::err() const {
52 return mLshal.err();
Yifan Hong443df792017-05-09 18:49:45 -070053}
54
Yifan Hong8bf73162017-09-07 18:06:13 -070055std::string ListCommand::parseCmdline(pid_t pid) const {
Yifan Hong443df792017-05-09 18:49:45 -070056 std::ifstream ifs("/proc/" + std::to_string(pid) + "/cmdline");
57 std::string cmdline;
58 if (!ifs.is_open()) {
59 return "";
60 }
61 ifs >> cmdline;
62 return cmdline;
63}
64
65const std::string &ListCommand::getCmdline(pid_t pid) {
66 auto pair = mCmdlines.find(pid);
67 if (pair != mCmdlines.end()) {
68 return pair->second;
69 }
Yifan Hong8bf73162017-09-07 18:06:13 -070070 mCmdlines[pid] = parseCmdline(pid);
Yifan Hong443df792017-05-09 18:49:45 -070071 return mCmdlines[pid];
72}
73
74void ListCommand::removeDeadProcesses(Pids *pids) {
75 static const pid_t myPid = getpid();
Yifan Hong61fb7bc2017-05-12 16:33:57 -070076 pids->erase(std::remove_if(pids->begin(), pids->end(), [this](auto pid) {
Yifan Hong443df792017-05-09 18:49:45 -070077 return pid == myPid || this->getCmdline(pid).empty();
Yifan Hong61fb7bc2017-05-12 16:33:57 -070078 }), pids->end());
Yifan Hong443df792017-05-09 18:49:45 -070079}
80
Steven Morelandd8e20192017-05-24 11:23:08 -070081bool scanBinderContext(pid_t pid,
82 const std::string &contextName,
83 std::function<void(const std::string&)> eachLine) {
84 std::ifstream ifs("/d/binder/proc/" + std::to_string(pid));
Yifan Hong443df792017-05-09 18:49:45 -070085 if (!ifs.is_open()) {
86 return false;
87 }
88
Steven Morelandd8e20192017-05-24 11:23:08 -070089 static const std::regex kContextLine("^context (\\w+)$");
Yifan Hong443df792017-05-09 18:49:45 -070090
Steven Morelandd8e20192017-05-24 11:23:08 -070091 bool isDesiredContext = false;
Yifan Hong443df792017-05-09 18:49:45 -070092 std::string line;
93 std::smatch match;
94 while(getline(ifs, line)) {
Steven Morelandd8e20192017-05-24 11:23:08 -070095 if (std::regex_search(line, match, kContextLine)) {
96 isDesiredContext = match.str(1) == contextName;
Yifan Hong443df792017-05-09 18:49:45 -070097 continue;
98 }
Steven Morelandd8e20192017-05-24 11:23:08 -070099
100 if (!isDesiredContext) {
Yifan Hong443df792017-05-09 18:49:45 -0700101 continue;
102 }
Steven Morelandd8e20192017-05-24 11:23:08 -0700103
104 eachLine(line);
Yifan Hong443df792017-05-09 18:49:45 -0700105 }
106 return true;
107}
108
Steven Morelandd8e20192017-05-24 11:23:08 -0700109bool ListCommand::getPidInfo(
110 pid_t serverPid, PidInfo *pidInfo) const {
111 static const std::regex kReferencePrefix("^\\s*node \\d+:\\s+u([0-9a-f]+)\\s+c([0-9a-f]+)\\s+");
112 static const std::regex kThreadPrefix("^\\s*thread \\d+:\\s+l\\s+(\\d)(\\d)");
113
114 std::smatch match;
115 return scanBinderContext(serverPid, "hwbinder", [&](const std::string& line) {
116 if (std::regex_search(line, match, kReferencePrefix)) {
117 const std::string &ptrString = "0x" + match.str(2); // use number after c
118 uint64_t ptr;
119 if (!::android::base::ParseUint(ptrString.c_str(), &ptr)) {
120 // Should not reach here, but just be tolerant.
Yifan Hong76ac14a2017-09-08 14:59:04 -0700121 err() << "Could not parse number " << ptrString << std::endl;
Steven Morelandd8e20192017-05-24 11:23:08 -0700122 return;
123 }
124 const std::string proc = " proc ";
125 auto pos = line.rfind(proc);
126 if (pos != std::string::npos) {
127 for (const std::string &pidStr : split(line.substr(pos + proc.size()), ' ')) {
128 int32_t pid;
129 if (!::android::base::ParseInt(pidStr, &pid)) {
Yifan Hong76ac14a2017-09-08 14:59:04 -0700130 err() << "Could not parse number " << pidStr << std::endl;
Steven Morelandd8e20192017-05-24 11:23:08 -0700131 return;
132 }
133 pidInfo->refPids[ptr].push_back(pid);
134 }
135 }
136
137 return;
138 }
139
140 if (std::regex_search(line, match, kThreadPrefix)) {
141 // "1" is waiting in binder driver
142 // "2" is poll. It's impossible to tell if these are in use.
143 // and HIDL default code doesn't use it.
144 bool isInUse = match.str(1) != "1";
145 // "0" is a thread that has called into binder
146 // "1" is looper thread
147 // "2" is main looper thread
148 bool isHwbinderThread = match.str(2) != "0";
149
150 if (!isHwbinderThread) {
151 return;
152 }
153
154 if (isInUse) {
155 pidInfo->threadUsage++;
156 }
157
158 pidInfo->threadCount++;
159 return;
160 }
161
162 // not reference or thread line
163 return;
164 });
165}
166
Yifan Hong443df792017-05-09 18:49:45 -0700167// Must process hwbinder services first, then passthrough services.
168void ListCommand::forEachTable(const std::function<void(Table &)> &f) {
169 f(mServicesTable);
170 f(mPassthroughRefTable);
171 f(mImplementationsTable);
172}
173void ListCommand::forEachTable(const std::function<void(const Table &)> &f) const {
174 f(mServicesTable);
175 f(mPassthroughRefTable);
176 f(mImplementationsTable);
177}
178
179void ListCommand::postprocess() {
180 forEachTable([this](Table &table) {
181 if (mSortColumn) {
182 std::sort(table.begin(), table.end(), mSortColumn);
183 }
184 for (TableEntry &entry : table) {
185 entry.serverCmdline = getCmdline(entry.serverPid);
186 removeDeadProcesses(&entry.clientPids);
187 for (auto pid : entry.clientPids) {
188 entry.clientCmdlines.push_back(this->getCmdline(pid));
189 }
190 }
191 });
192 // use a double for loop here because lshal doesn't care about efficiency.
193 for (TableEntry &packageEntry : mImplementationsTable) {
194 std::string packageName = packageEntry.interfaceName;
195 FQName fqPackageName{packageName.substr(0, packageName.find("::"))};
196 if (!fqPackageName.isValid()) {
197 continue;
198 }
199 for (TableEntry &interfaceEntry : mPassthroughRefTable) {
200 if (interfaceEntry.arch != ARCH_UNKNOWN) {
201 continue;
202 }
203 FQName interfaceName{splitFirst(interfaceEntry.interfaceName, '/').first};
204 if (!interfaceName.isValid()) {
205 continue;
206 }
207 if (interfaceName.getPackageAndVersion() == fqPackageName) {
208 interfaceEntry.arch = packageEntry.arch;
209 }
210 }
211 }
Yifan Hongca3b6602017-09-07 16:44:27 -0700212
213 mServicesTable.setDescription(
214 "All binderized services (registered services through hwservicemanager)");
215 mPassthroughRefTable.setDescription(
216 "All interfaces that getService() has ever return as a passthrough interface;\n"
217 "PIDs / processes shown below might be inaccurate because the process\n"
218 "might have relinquished the interface or might have died.\n"
219 "The Server / Server CMD column can be ignored.\n"
220 "The Clients / Clients CMD column shows all process that have ever dlopen'ed \n"
221 "the library and successfully fetched the passthrough implementation.");
222 mImplementationsTable.setDescription(
223 "All available passthrough implementations (all -impl.so files)");
Yifan Hong443df792017-05-09 18:49:45 -0700224}
225
Yifan Hong77c87822017-06-19 15:47:39 -0700226static inline bool findAndBumpVersion(vintf::ManifestHal* hal, const vintf::Version& version) {
227 for (vintf::Version& v : hal->versions) {
228 if (v.majorVer == version.majorVer) {
229 v.minorVer = std::max(v.minorVer, version.minorVer);
230 return true;
231 }
232 }
233 return false;
234}
235
Yifan Hongca3b6602017-09-07 16:44:27 -0700236void ListCommand::dumpVintf(const NullableOStream<std::ostream>& out) const {
Yifan Hong236301c2017-06-19 12:27:08 -0700237 using vintf::operator|=;
Yifan Hongca3b6602017-09-07 16:44:27 -0700238 out << "<!-- " << std::endl
Yifan Hong443df792017-05-09 18:49:45 -0700239 << " This is a skeleton device manifest. Notes: " << std::endl
240 << " 1. android.hidl.*, android.frameworks.*, android.system.* are not included." << std::endl
241 << " 2. If a HAL is supported in both hwbinder and passthrough transport, " << std::endl
242 << " only hwbinder is shown." << std::endl
243 << " 3. It is likely that HALs in passthrough transport does not have" << std::endl
244 << " <interface> declared; users will have to write them by hand." << std::endl
Yifan Hong77c87822017-06-19 15:47:39 -0700245 << " 4. A HAL with lower minor version can be overridden by a HAL with" << std::endl
246 << " higher minor version if they have the same name and major version." << std::endl
247 << " 5. sepolicy version is set to 0.0. It is recommended that the entry" << std::endl
Yifan Hong443df792017-05-09 18:49:45 -0700248 << " is removed from the manifest file and written by assemble_vintf" << std::endl
249 << " at build time." << std::endl
250 << "-->" << std::endl;
251
252 vintf::HalManifest manifest;
253 forEachTable([this, &manifest] (const Table &table) {
254 for (const TableEntry &entry : table) {
255
256 std::string fqInstanceName = entry.interfaceName;
257
258 if (&table == &mImplementationsTable) {
259 // Quick hack to work around *'s
260 replaceAll(&fqInstanceName, '*', 'D');
261 }
262 auto splittedFqInstanceName = splitFirst(fqInstanceName, '/');
263 FQName fqName(splittedFqInstanceName.first);
264 if (!fqName.isValid()) {
Yifan Hong76ac14a2017-09-08 14:59:04 -0700265 err() << "Warning: '" << splittedFqInstanceName.first
Yifan Hong443df792017-05-09 18:49:45 -0700266 << "' is not a valid FQName." << std::endl;
267 continue;
268 }
269 // Strip out system libs.
270 if (fqName.inPackage("android.hidl") ||
271 fqName.inPackage("android.frameworks") ||
272 fqName.inPackage("android.system")) {
273 continue;
274 }
275 std::string interfaceName =
276 &table == &mImplementationsTable ? "" : fqName.name();
277 std::string instanceName =
278 &table == &mImplementationsTable ? "" : splittedFqInstanceName.second;
279
280 vintf::Version version{fqName.getPackageMajorVersion(),
281 fqName.getPackageMinorVersion()};
282 vintf::Transport transport;
283 vintf::Arch arch;
284 if (entry.transport == "hwbinder") {
285 transport = vintf::Transport::HWBINDER;
286 arch = vintf::Arch::ARCH_EMPTY;
287 } else if (entry.transport == "passthrough") {
288 transport = vintf::Transport::PASSTHROUGH;
289 switch (entry.arch) {
290 case lshal::ARCH32:
291 arch = vintf::Arch::ARCH_32; break;
292 case lshal::ARCH64:
293 arch = vintf::Arch::ARCH_64; break;
294 case lshal::ARCH_BOTH:
295 arch = vintf::Arch::ARCH_32_64; break;
296 case lshal::ARCH_UNKNOWN: // fallthrough
297 default:
Yifan Hong76ac14a2017-09-08 14:59:04 -0700298 err() << "Warning: '" << fqName.package()
Yifan Hong443df792017-05-09 18:49:45 -0700299 << "' doesn't have bitness info, assuming 32+64." << std::endl;
300 arch = vintf::Arch::ARCH_32_64;
301 }
302 } else {
Yifan Hong76ac14a2017-09-08 14:59:04 -0700303 err() << "Warning: '" << entry.transport << "' is not a valid transport." << std::endl;
Yifan Hong443df792017-05-09 18:49:45 -0700304 continue;
305 }
306
307 bool done = false;
308 for (vintf::ManifestHal *hal : manifest.getHals(fqName.package())) {
309 if (hal->transport() != transport) {
310 if (transport != vintf::Transport::PASSTHROUGH) {
Yifan Hong76ac14a2017-09-08 14:59:04 -0700311 err() << "Fatal: should not reach here. Generated result may be wrong for '"
Yifan Hong236301c2017-06-19 12:27:08 -0700312 << hal->name << "'."
Yifan Hong443df792017-05-09 18:49:45 -0700313 << std::endl;
314 }
315 done = true;
316 break;
317 }
Yifan Hong77c87822017-06-19 15:47:39 -0700318 if (findAndBumpVersion(hal, version)) {
Yifan Hong443df792017-05-09 18:49:45 -0700319 if (&table != &mImplementationsTable) {
320 hal->interfaces[interfaceName].name = interfaceName;
321 hal->interfaces[interfaceName].instances.insert(instanceName);
322 }
Yifan Hong236301c2017-06-19 12:27:08 -0700323 hal->transportArch.arch |= arch;
Yifan Hong443df792017-05-09 18:49:45 -0700324 done = true;
325 break;
326 }
327 }
328 if (done) {
329 continue; // to next TableEntry
330 }
331 decltype(vintf::ManifestHal::interfaces) interfaces;
332 if (&table != &mImplementationsTable) {
333 interfaces[interfaceName].name = interfaceName;
334 interfaces[interfaceName].instances.insert(instanceName);
335 }
336 if (!manifest.add(vintf::ManifestHal{
337 .format = vintf::HalFormat::HIDL,
338 .name = fqName.package(),
339 .versions = {version},
340 .transportArch = {transport, arch},
341 .interfaces = interfaces})) {
Yifan Hong76ac14a2017-09-08 14:59:04 -0700342 err() << "Warning: cannot add hal '" << fqInstanceName << "'" << std::endl;
Yifan Hong443df792017-05-09 18:49:45 -0700343 }
344 }
345 });
Yifan Hongca3b6602017-09-07 16:44:27 -0700346 out << vintf::gHalManifestConverter(manifest);
Yifan Hong443df792017-05-09 18:49:45 -0700347}
348
Yifan Hong443df792017-05-09 18:49:45 -0700349static Architecture fromBaseArchitecture(::android::hidl::base::V1_0::DebugInfo::Architecture a) {
350 switch (a) {
351 case ::android::hidl::base::V1_0::DebugInfo::Architecture::IS_64BIT:
352 return ARCH64;
353 case ::android::hidl::base::V1_0::DebugInfo::Architecture::IS_32BIT:
354 return ARCH32;
355 case ::android::hidl::base::V1_0::DebugInfo::Architecture::UNKNOWN: // fallthrough
356 default:
357 return ARCH_UNKNOWN;
358 }
359}
360
Yifan Hongca3b6602017-09-07 16:44:27 -0700361void ListCommand::dumpTable(const NullableOStream<std::ostream>& out) const {
Yifan Hong1bc1e9f2017-08-29 17:28:12 -0700362 if (mNeat) {
Yifan Hongd4a77e82017-09-06 19:40:24 -0700363 MergedTable({&mServicesTable, &mPassthroughRefTable, &mImplementationsTable})
Yifan Hongca3b6602017-09-07 16:44:27 -0700364 .createTextTable().dump(out.buf());
Yifan Hong1bc1e9f2017-08-29 17:28:12 -0700365 return;
366 }
367
Yifan Hongca3b6602017-09-07 16:44:27 -0700368 forEachTable([this, &out](const Table &table) {
Yifan Hong1bc1e9f2017-08-29 17:28:12 -0700369
Yifan Hongd4a77e82017-09-06 19:40:24 -0700370 // We're only interested in dumping debug info for already
371 // instantiated services. There's little value in dumping the
372 // debug info for a service we create on the fly, so we only operate
373 // on the "mServicesTable".
374 std::function<std::string(const std::string&)> emitDebugInfo = nullptr;
375 if (mEmitDebugInfo && &table == &mServicesTable) {
376 emitDebugInfo = [this](const auto& iName) {
Yifan Hongca3b6602017-09-07 16:44:27 -0700377 std::stringstream ss;
Yifan Hongd4a77e82017-09-06 19:40:24 -0700378 auto pair = splitFirst(iName, '/');
Yifan Hongca3b6602017-09-07 16:44:27 -0700379 mLshal.emitDebugInfo(pair.first, pair.second, {}, ss,
Yifan Hong1bc1e9f2017-08-29 17:28:12 -0700380 NullableOStream<std::ostream>(nullptr));
Yifan Hongca3b6602017-09-07 16:44:27 -0700381 return ss.str();
Yifan Hongd4a77e82017-09-06 19:40:24 -0700382 };
Yifan Hong443df792017-05-09 18:49:45 -0700383 }
Yifan Hongca3b6602017-09-07 16:44:27 -0700384 table.createTextTable(mNeat, emitDebugInfo).dump(out.buf());
385 out << std::endl;
Yifan Hong1bc1e9f2017-08-29 17:28:12 -0700386 });
Yifan Hong443df792017-05-09 18:49:45 -0700387}
388
Yifan Hongca3b6602017-09-07 16:44:27 -0700389Status ListCommand::dump() {
390 auto dump = mVintf ? &ListCommand::dumpVintf : &ListCommand::dumpTable;
391
392 if (mFileOutputPath.empty()) {
393 (*this.*dump)(out());
394 return OK;
Yifan Hong443df792017-05-09 18:49:45 -0700395 }
Yifan Hongca3b6602017-09-07 16:44:27 -0700396
397 std::ofstream fileOutput(mFileOutputPath);
398 if (!fileOutput.is_open()) {
399 err() << "Could not open file '" << mFileOutputPath << "'." << std::endl;
400 return IO_ERROR;
401 }
402 chown(mFileOutputPath.c_str(), AID_SHELL, AID_SHELL);
403
404 (*this.*dump)(NullableOStream<std::ostream>(fileOutput));
405
406 fileOutput.flush();
407 fileOutput.close();
408 return OK;
Yifan Hong443df792017-05-09 18:49:45 -0700409}
410
411void ListCommand::putEntry(TableEntrySource source, TableEntry &&entry) {
412 Table *table = nullptr;
413 switch (source) {
414 case HWSERVICEMANAGER_LIST :
415 table = &mServicesTable; break;
416 case PTSERVICEMANAGER_REG_CLIENT :
417 table = &mPassthroughRefTable; break;
418 case LIST_DLLIB :
419 table = &mImplementationsTable; break;
420 default:
Yifan Hong76ac14a2017-09-08 14:59:04 -0700421 err() << "Error: Unknown source of entry " << source << std::endl;
Yifan Hong443df792017-05-09 18:49:45 -0700422 }
423 if (table) {
Yifan Hongd4a77e82017-09-06 19:40:24 -0700424 table->add(std::forward<TableEntry>(entry));
Yifan Hong443df792017-05-09 18:49:45 -0700425 }
426}
427
428Status ListCommand::fetchAllLibraries(const sp<IServiceManager> &manager) {
429 using namespace ::android::hardware;
430 using namespace ::android::hidl::manager::V1_0;
431 using namespace ::android::hidl::base::V1_0;
Yifan Hongf2d557b2017-05-24 19:45:02 -0700432 using std::literals::chrono_literals::operator""s;
433 auto ret = timeoutIPC(2s, manager, &IServiceManager::debugDump, [&] (const auto &infos) {
Yifan Hong443df792017-05-09 18:49:45 -0700434 std::map<std::string, TableEntry> entries;
435 for (const auto &info : infos) {
436 std::string interfaceName = std::string{info.interfaceName.c_str()} + "/" +
437 std::string{info.instanceName.c_str()};
438 entries.emplace(interfaceName, TableEntry{
439 .interfaceName = interfaceName,
440 .transport = "passthrough",
441 .serverPid = NO_PID,
442 .serverObjectAddress = NO_PTR,
Yifan Hongf2d557b2017-05-24 19:45:02 -0700443 .clientPids = info.clientPids,
Yifan Hong443df792017-05-09 18:49:45 -0700444 .arch = ARCH_UNKNOWN
445 }).first->second.arch |= fromBaseArchitecture(info.arch);
446 }
447 for (auto &&pair : entries) {
448 putEntry(LIST_DLLIB, std::move(pair.second));
449 }
450 });
451 if (!ret.isOk()) {
Yifan Hong76ac14a2017-09-08 14:59:04 -0700452 err() << "Error: Failed to call list on getPassthroughServiceManager(): "
Yifan Hong443df792017-05-09 18:49:45 -0700453 << ret.description() << std::endl;
454 return DUMP_ALL_LIBS_ERROR;
455 }
456 return OK;
457}
458
459Status ListCommand::fetchPassthrough(const sp<IServiceManager> &manager) {
460 using namespace ::android::hardware;
461 using namespace ::android::hardware::details;
462 using namespace ::android::hidl::manager::V1_0;
463 using namespace ::android::hidl::base::V1_0;
464 auto ret = timeoutIPC(manager, &IServiceManager::debugDump, [&] (const auto &infos) {
465 for (const auto &info : infos) {
466 if (info.clientPids.size() <= 0) {
467 continue;
468 }
469 putEntry(PTSERVICEMANAGER_REG_CLIENT, {
470 .interfaceName =
471 std::string{info.interfaceName.c_str()} + "/" +
472 std::string{info.instanceName.c_str()},
473 .transport = "passthrough",
474 .serverPid = info.clientPids.size() == 1 ? info.clientPids[0] : NO_PID,
475 .serverObjectAddress = NO_PTR,
476 .clientPids = info.clientPids,
477 .arch = fromBaseArchitecture(info.arch)
478 });
479 }
480 });
481 if (!ret.isOk()) {
Yifan Hong76ac14a2017-09-08 14:59:04 -0700482 err() << "Error: Failed to call debugDump on defaultServiceManager(): "
Yifan Hong443df792017-05-09 18:49:45 -0700483 << ret.description() << std::endl;
484 return DUMP_PASSTHROUGH_ERROR;
485 }
486 return OK;
487}
488
489Status ListCommand::fetchBinderized(const sp<IServiceManager> &manager) {
490 using namespace ::std;
491 using namespace ::android::hardware;
492 using namespace ::android::hidl::manager::V1_0;
493 using namespace ::android::hidl::base::V1_0;
494 const std::string mode = "hwbinder";
495
496 hidl_vec<hidl_string> fqInstanceNames;
497 // copying out for timeoutIPC
498 auto listRet = timeoutIPC(manager, &IServiceManager::list, [&] (const auto &names) {
499 fqInstanceNames = names;
500 });
501 if (!listRet.isOk()) {
Yifan Hong76ac14a2017-09-08 14:59:04 -0700502 err() << "Error: Failed to list services for " << mode << ": "
Yifan Hong443df792017-05-09 18:49:45 -0700503 << listRet.description() << std::endl;
504 return DUMP_BINDERIZED_ERROR;
505 }
506
507 Status status = OK;
508 // server pid, .ptr value of binder object, child pids
509 std::map<std::string, DebugInfo> allDebugInfos;
Steven Morelandd8e20192017-05-24 11:23:08 -0700510 std::map<pid_t, PidInfo> allPids;
Yifan Hong443df792017-05-09 18:49:45 -0700511 for (const auto &fqInstanceName : fqInstanceNames) {
512 const auto pair = splitFirst(fqInstanceName, '/');
513 const auto &serviceName = pair.first;
514 const auto &instanceName = pair.second;
515 auto getRet = timeoutIPC(manager, &IServiceManager::get, serviceName, instanceName);
516 if (!getRet.isOk()) {
Yifan Hong76ac14a2017-09-08 14:59:04 -0700517 err() << "Warning: Skipping \"" << fqInstanceName << "\": "
Yifan Hong443df792017-05-09 18:49:45 -0700518 << "cannot be fetched from service manager:"
519 << getRet.description() << std::endl;
520 status |= DUMP_BINDERIZED_ERROR;
521 continue;
522 }
523 sp<IBase> service = getRet;
524 if (service == nullptr) {
Yifan Hong76ac14a2017-09-08 14:59:04 -0700525 err() << "Warning: Skipping \"" << fqInstanceName << "\": "
Yifan Hong443df792017-05-09 18:49:45 -0700526 << "cannot be fetched from service manager (null)"
527 << std::endl;
528 status |= DUMP_BINDERIZED_ERROR;
529 continue;
530 }
531 auto debugRet = timeoutIPC(service, &IBase::getDebugInfo, [&] (const auto &debugInfo) {
532 allDebugInfos[fqInstanceName] = debugInfo;
533 if (debugInfo.pid >= 0) {
Steven Morelandd8e20192017-05-24 11:23:08 -0700534 allPids[static_cast<pid_t>(debugInfo.pid)] = PidInfo();
Yifan Hong443df792017-05-09 18:49:45 -0700535 }
536 });
537 if (!debugRet.isOk()) {
Yifan Hong76ac14a2017-09-08 14:59:04 -0700538 err() << "Warning: Skipping \"" << fqInstanceName << "\": "
Yifan Hong443df792017-05-09 18:49:45 -0700539 << "debugging information cannot be retrieved:"
540 << debugRet.description() << std::endl;
541 status |= DUMP_BINDERIZED_ERROR;
542 }
543 }
Steven Morelandd8e20192017-05-24 11:23:08 -0700544
Yifan Hong443df792017-05-09 18:49:45 -0700545 for (auto &pair : allPids) {
546 pid_t serverPid = pair.first;
Steven Morelandd8e20192017-05-24 11:23:08 -0700547 if (!getPidInfo(serverPid, &allPids[serverPid])) {
Yifan Hong76ac14a2017-09-08 14:59:04 -0700548 err() << "Warning: no information for PID " << serverPid
Yifan Hong443df792017-05-09 18:49:45 -0700549 << ", are you root?" << std::endl;
550 status |= DUMP_BINDERIZED_ERROR;
551 }
552 }
553 for (const auto &fqInstanceName : fqInstanceNames) {
554 auto it = allDebugInfos.find(fqInstanceName);
555 if (it == allDebugInfos.end()) {
556 putEntry(HWSERVICEMANAGER_LIST, {
557 .interfaceName = fqInstanceName,
558 .transport = mode,
559 .serverPid = NO_PID,
560 .serverObjectAddress = NO_PTR,
561 .clientPids = {},
Steven Morelandd8e20192017-05-24 11:23:08 -0700562 .threadUsage = 0,
563 .threadCount = 0,
Yifan Hong443df792017-05-09 18:49:45 -0700564 .arch = ARCH_UNKNOWN
565 });
566 continue;
567 }
568 const DebugInfo &info = it->second;
Steven Morelandd8e20192017-05-24 11:23:08 -0700569 bool writePidInfo = info.pid != NO_PID && info.ptr != NO_PTR;
570
Yifan Hong443df792017-05-09 18:49:45 -0700571 putEntry(HWSERVICEMANAGER_LIST, {
572 .interfaceName = fqInstanceName,
573 .transport = mode,
574 .serverPid = info.pid,
575 .serverObjectAddress = info.ptr,
Steven Morelandd8e20192017-05-24 11:23:08 -0700576 .clientPids = writePidInfo ? allPids[info.pid].refPids[info.ptr] : Pids{},
577 .threadUsage = writePidInfo ? allPids[info.pid].threadUsage : 0,
578 .threadCount = writePidInfo ? allPids[info.pid].threadCount : 0,
Yifan Hong443df792017-05-09 18:49:45 -0700579 .arch = fromBaseArchitecture(info.arch),
580 });
581 }
582 return status;
583}
584
585Status ListCommand::fetch() {
586 Status status = OK;
Yifan Hong9881df92017-05-10 14:33:05 -0700587 auto bManager = mLshal.serviceManager();
Yifan Hong443df792017-05-09 18:49:45 -0700588 if (bManager == nullptr) {
Yifan Hong76ac14a2017-09-08 14:59:04 -0700589 err() << "Failed to get defaultServiceManager()!" << std::endl;
Yifan Hong443df792017-05-09 18:49:45 -0700590 status |= NO_BINDERIZED_MANAGER;
591 } else {
592 status |= fetchBinderized(bManager);
593 // Passthrough PIDs are registered to the binderized manager as well.
594 status |= fetchPassthrough(bManager);
595 }
596
Yifan Hong9881df92017-05-10 14:33:05 -0700597 auto pManager = mLshal.passthroughManager();
Yifan Hong443df792017-05-09 18:49:45 -0700598 if (pManager == nullptr) {
Yifan Hong76ac14a2017-09-08 14:59:04 -0700599 err() << "Failed to get getPassthroughServiceManager()!" << std::endl;
Yifan Hong443df792017-05-09 18:49:45 -0700600 status |= NO_PASSTHROUGH_MANAGER;
601 } else {
602 status |= fetchAllLibraries(pManager);
603 }
604 return status;
605}
606
Yifan Honga8bedc62017-09-08 18:00:31 -0700607Status ListCommand::parseArgs(const Arg &arg) {
Yifan Hong443df792017-05-09 18:49:45 -0700608 static struct option longOptions[] = {
609 // long options with short alternatives
610 {"help", no_argument, 0, 'h' },
611 {"interface", no_argument, 0, 'i' },
612 {"transport", no_argument, 0, 't' },
613 {"arch", no_argument, 0, 'r' },
614 {"pid", no_argument, 0, 'p' },
615 {"address", no_argument, 0, 'a' },
616 {"clients", no_argument, 0, 'c' },
Steven Morelandd8e20192017-05-24 11:23:08 -0700617 {"threads", no_argument, 0, 'e' },
Yifan Hong443df792017-05-09 18:49:45 -0700618 {"cmdline", no_argument, 0, 'm' },
619 {"debug", optional_argument, 0, 'd' },
620
621 // long options without short alternatives
622 {"sort", required_argument, 0, 's' },
623 {"init-vintf",optional_argument, 0, 'v' },
Yifan Hong6da06912017-05-12 16:56:43 -0700624 {"neat", no_argument, 0, 'n' },
Yifan Hong443df792017-05-09 18:49:45 -0700625 { 0, 0, 0, 0 }
626 };
627
Yifan Hongd4a77e82017-09-06 19:40:24 -0700628 std::vector<TableColumnType> selectedColumns;
629 bool enableCmdlines = false;
630
Yifan Honga8bedc62017-09-08 18:00:31 -0700631 // suppress output to std::err for unknown options
632 opterr = 0;
633
Yifan Hong443df792017-05-09 18:49:45 -0700634 int optionIndex;
635 int c;
636 // Lshal::parseArgs has set optind to the next option to parse
637 for (;;) {
638 // using getopt_long in case we want to add other options in the future
639 c = getopt_long(arg.argc, arg.argv,
Steven Morelandd8e20192017-05-24 11:23:08 -0700640 "hitrpacmde", longOptions, &optionIndex);
Yifan Hong443df792017-05-09 18:49:45 -0700641 if (c == -1) {
642 break;
643 }
644 switch (c) {
645 case 's': {
646 if (strcmp(optarg, "interface") == 0 || strcmp(optarg, "i") == 0) {
647 mSortColumn = TableEntry::sortByInterfaceName;
648 } else if (strcmp(optarg, "pid") == 0 || strcmp(optarg, "p") == 0) {
649 mSortColumn = TableEntry::sortByServerPid;
650 } else {
Yifan Hong76ac14a2017-09-08 14:59:04 -0700651 err() << "Unrecognized sorting column: " << optarg << std::endl;
Yifan Hong443df792017-05-09 18:49:45 -0700652 return USAGE;
653 }
654 break;
655 }
656 case 'v': {
Yifan Hong443df792017-05-09 18:49:45 -0700657 mVintf = true;
Yifan Hongca3b6602017-09-07 16:44:27 -0700658 if (optarg) mFileOutputPath = optarg;
659 break;
Yifan Hong443df792017-05-09 18:49:45 -0700660 }
661 case 'i': {
Yifan Hongd4a77e82017-09-06 19:40:24 -0700662 selectedColumns.push_back(TableColumnType::INTERFACE_NAME);
Yifan Hong443df792017-05-09 18:49:45 -0700663 break;
664 }
665 case 't': {
Yifan Hongd4a77e82017-09-06 19:40:24 -0700666 selectedColumns.push_back(TableColumnType::TRANSPORT);
Yifan Hong443df792017-05-09 18:49:45 -0700667 break;
668 }
669 case 'r': {
Yifan Hongd4a77e82017-09-06 19:40:24 -0700670 selectedColumns.push_back(TableColumnType::ARCH);
Yifan Hong443df792017-05-09 18:49:45 -0700671 break;
672 }
673 case 'p': {
Yifan Hongd4a77e82017-09-06 19:40:24 -0700674 selectedColumns.push_back(TableColumnType::SERVER_PID);
Yifan Hong443df792017-05-09 18:49:45 -0700675 break;
676 }
677 case 'a': {
Yifan Hongd4a77e82017-09-06 19:40:24 -0700678 selectedColumns.push_back(TableColumnType::SERVER_ADDR);
Yifan Hong443df792017-05-09 18:49:45 -0700679 break;
680 }
681 case 'c': {
Yifan Hongd4a77e82017-09-06 19:40:24 -0700682 selectedColumns.push_back(TableColumnType::CLIENT_PIDS);
Yifan Hong443df792017-05-09 18:49:45 -0700683 break;
684 }
Steven Morelandd8e20192017-05-24 11:23:08 -0700685 case 'e': {
Yifan Hongd4a77e82017-09-06 19:40:24 -0700686 selectedColumns.push_back(TableColumnType::THREADS);
Steven Morelandd8e20192017-05-24 11:23:08 -0700687 break;
688 }
Yifan Hong443df792017-05-09 18:49:45 -0700689 case 'm': {
Yifan Hongd4a77e82017-09-06 19:40:24 -0700690 enableCmdlines = true;
Yifan Hong443df792017-05-09 18:49:45 -0700691 break;
692 }
693 case 'd': {
694 mEmitDebugInfo = true;
Yifan Hongca3b6602017-09-07 16:44:27 -0700695 if (optarg) mFileOutputPath = optarg;
Yifan Hong443df792017-05-09 18:49:45 -0700696 break;
697 }
Yifan Hong6da06912017-05-12 16:56:43 -0700698 case 'n': {
699 mNeat = true;
700 break;
701 }
Yifan Honga8bedc62017-09-08 18:00:31 -0700702 case 'h': {
703 return USAGE;
704 }
Yifan Hong443df792017-05-09 18:49:45 -0700705 default: // see unrecognized options
Yifan Honga8bedc62017-09-08 18:00:31 -0700706 err() << "unrecognized option `" << arg.argv[optind - 1] << "'" << std::endl;
Yifan Hong443df792017-05-09 18:49:45 -0700707 return USAGE;
708 }
709 }
710 if (optind < arg.argc) {
711 // see non option
Yifan Honga8bedc62017-09-08 18:00:31 -0700712 err() << "unrecognized option `" << arg.argv[optind] << "'" << std::endl;
Yifan Hong1bc1e9f2017-08-29 17:28:12 -0700713 return USAGE;
714 }
715
716 if (mNeat && mEmitDebugInfo) {
Yifan Hong76ac14a2017-09-08 14:59:04 -0700717 err() << "Error: --neat should not be used with --debug." << std::endl;
Yifan Hong1bc1e9f2017-08-29 17:28:12 -0700718 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
Yifan Honga8bedc62017-09-08 18:00:31 -0700744Status ListCommand::main(const Arg &arg) {
745 Status status = parseArgs(arg);
Yifan Hong443df792017-05-09 18:49:45 -0700746 if (status != OK) {
747 return status;
748 }
749 status = fetch();
750 postprocess();
Yifan Hongca3b6602017-09-07 16:44:27 -0700751 status |= dump();
Yifan Hong443df792017-05-09 18:49:45 -0700752 return status;
753}
754
Yifan Honga8bedc62017-09-08 18:00:31 -0700755void ListCommand::usage() const {
756
757 static const std::string list =
758 "list:\n"
759 " lshal\n"
760 " lshal list\n"
761 " List all hals with default ordering and columns (`lshal list -iepc`)\n"
762 " lshal list [-h|--help]\n"
763 " -h, --help: Print help message for list (`lshal help list`)\n"
764 " lshal [list] [--interface|-i] [--transport|-t] [-r|--arch] [-e|--threads]\n"
765 " [--pid|-p] [--address|-a] [--clients|-c] [--cmdline|-m]\n"
766 " [--sort={interface|i|pid|p}] [--init-vintf[=<output file>]]\n"
767 " [--debug|-d[=<output file>]] [--neat]\n"
768 " -i, --interface: print the interface name column\n"
769 " -n, --instance: print the instance name column\n"
770 " -t, --transport: print the transport mode column\n"
771 " -r, --arch: print if the HAL is in 64-bit or 32-bit\n"
772 " -e, --threads: print currently used/available threads\n"
773 " (note, available threads created lazily)\n"
774 " -p, --pid: print the server PID, or server cmdline if -m is set\n"
775 " -a, --address: print the server object address column\n"
776 " -c, --clients: print the client PIDs, or client cmdlines if -m is set\n"
777 " -m, --cmdline: print cmdline instead of PIDs\n"
778 " -d[=<output file>], --debug[=<output file>]: emit debug info from \n"
779 " IBase::debug with empty options. Cannot be used with --neat.\n"
780 " --sort=i, --sort=interface: sort by interface name\n"
781 " --sort=p, --sort=pid: sort by server pid\n"
782 " --neat: output is machine parsable (no explanatory text)\n"
783 " Cannot be used with --debug.\n"
784 " --init-vintf[=<output file>]: form a skeleton HAL manifest to specified\n"
785 " file, or stdout if no file specified.\n";
786
787 err() << list;
788}
789
Yifan Hong443df792017-05-09 18:49:45 -0700790} // namespace lshal
791} // namespace android
Yifan Hong05494a52017-08-29 18:50:00 -0700792