blob: c99b8637f31f0129bb162f4af62638f9dbd295d3 [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
Nirav Atrecce988d2018-05-16 11:14:46 -070021#include <algorithm>
Yifan Hong443df792017-05-09 18:49:45 -070022#include <fstream>
Yifan Hongb2d096a2018-05-01 15:25:23 -070023#include <functional>
Yifan Hong443df792017-05-09 18:49:45 -070024#include <iomanip>
25#include <iostream>
26#include <map>
Yifan Hong443df792017-05-09 18:49:45 -070027#include <regex>
Yifan Hongb2d096a2018-05-01 15:25:23 -070028#include <sstream>
Yifan Hong443df792017-05-09 18:49:45 -070029
Yifan Hongf31aa052018-02-02 15:17:51 -080030#include <android-base/file.h>
Nirav Atrecce988d2018-05-16 11:14:46 -070031#include <android-base/logging.h>
Yifan Hong443df792017-05-09 18:49:45 -070032#include <android-base/parseint.h>
33#include <android/hidl/manager/1.0/IServiceManager.h>
Yifan Hongfee209d2017-09-14 18:23:38 -070034#include <hidl-hash/Hash.h>
Yifan Hong443df792017-05-09 18:49:45 -070035#include <hidl-util/FQName.h>
36#include <private/android_filesystem_config.h>
37#include <sys/stat.h>
38#include <vintf/HalManifest.h>
Yifan Hongf31aa052018-02-02 15:17:51 -080039#include <vintf/parse_string.h>
Yifan Hong443df792017-05-09 18:49:45 -070040#include <vintf/parse_xml.h>
41
42#include "Lshal.h"
43#include "PipeRelay.h"
44#include "Timeout.h"
45#include "utils.h"
46
47using ::android::hardware::hidl_string;
Yifan Hong22ea7b82017-09-14 18:07:43 -070048using ::android::hardware::hidl_vec;
49using ::android::hidl::base::V1_0::DebugInfo;
50using ::android::hidl::base::V1_0::IBase;
Yifan Hong443df792017-05-09 18:49:45 -070051using ::android::hidl::manager::V1_0::IServiceManager;
52
53namespace android {
54namespace lshal {
55
Yifan Hongf31aa052018-02-02 15:17:51 -080056vintf::SchemaType toSchemaType(Partition p) {
57 return (p == Partition::SYSTEM) ? vintf::SchemaType::FRAMEWORK : vintf::SchemaType::DEVICE;
58}
59
Yifan Hong76ac14a2017-09-08 14:59:04 -070060NullableOStream<std::ostream> ListCommand::out() const {
61 return mLshal.out();
62}
63
64NullableOStream<std::ostream> ListCommand::err() const {
65 return mLshal.err();
Yifan Hong443df792017-05-09 18:49:45 -070066}
67
Yifan Hong795b6ec2017-09-13 11:25:28 -070068std::string ListCommand::GetName() {
69 return "list";
70}
71std::string ListCommand::getSimpleDescription() const {
72 return "List HALs.";
73}
74
Yifan Hong8bf73162017-09-07 18:06:13 -070075std::string ListCommand::parseCmdline(pid_t pid) const {
Yifan Hongf31aa052018-02-02 15:17:51 -080076 return android::procpartition::getCmdline(pid);
Yifan Hong443df792017-05-09 18:49:45 -070077}
78
79const std::string &ListCommand::getCmdline(pid_t pid) {
80 auto pair = mCmdlines.find(pid);
81 if (pair != mCmdlines.end()) {
82 return pair->second;
83 }
Yifan Hong8bf73162017-09-07 18:06:13 -070084 mCmdlines[pid] = parseCmdline(pid);
Yifan Hong443df792017-05-09 18:49:45 -070085 return mCmdlines[pid];
86}
87
88void ListCommand::removeDeadProcesses(Pids *pids) {
89 static const pid_t myPid = getpid();
Yifan Hong61fb7bc2017-05-12 16:33:57 -070090 pids->erase(std::remove_if(pids->begin(), pids->end(), [this](auto pid) {
Yifan Hong443df792017-05-09 18:49:45 -070091 return pid == myPid || this->getCmdline(pid).empty();
Yifan Hong61fb7bc2017-05-12 16:33:57 -070092 }), pids->end());
Yifan Hong443df792017-05-09 18:49:45 -070093}
94
Yifan Hongf31aa052018-02-02 15:17:51 -080095Partition ListCommand::getPartition(pid_t pid) {
96 auto it = mPartitions.find(pid);
97 if (it != mPartitions.end()) {
98 return it->second;
99 }
100 Partition partition = android::procpartition::getPartition(pid);
101 mPartitions.emplace(pid, partition);
102 return partition;
103}
104
105// Give sensible defaults when nothing can be inferred from runtime.
106// process: Partition inferred from executable location or cmdline.
Yifan Hongb2d096a2018-05-01 15:25:23 -0700107Partition ListCommand::resolvePartition(Partition process, const FqInstance& fqInstance) const {
108 if (fqInstance.inPackage("vendor") || fqInstance.inPackage("com")) {
Yifan Hongf31aa052018-02-02 15:17:51 -0800109 return Partition::VENDOR;
110 }
111
Yifan Hongb2d096a2018-05-01 15:25:23 -0700112 if (fqInstance.inPackage("android.frameworks") || fqInstance.inPackage("android.system") ||
113 fqInstance.inPackage("android.hidl")) {
Yifan Hongf31aa052018-02-02 15:17:51 -0800114 return Partition::SYSTEM;
115 }
116
117 // Some android.hardware HALs are served from system. Check the value from executable
118 // location / cmdline first.
Yifan Hongb2d096a2018-05-01 15:25:23 -0700119 if (fqInstance.inPackage("android.hardware")) {
Yifan Hongf31aa052018-02-02 15:17:51 -0800120 if (process != Partition::UNKNOWN) {
121 return process;
122 }
123 return Partition::VENDOR;
124 }
125
126 return process;
127}
128
Yifan Hong1243dde2017-09-14 17:49:30 -0700129static bool scanBinderContext(pid_t pid,
Steven Morelandd8e20192017-05-24 11:23:08 -0700130 const std::string &contextName,
131 std::function<void(const std::string&)> eachLine) {
132 std::ifstream ifs("/d/binder/proc/" + std::to_string(pid));
Yifan Hong443df792017-05-09 18:49:45 -0700133 if (!ifs.is_open()) {
134 return false;
135 }
136
Steven Morelandd8e20192017-05-24 11:23:08 -0700137 static const std::regex kContextLine("^context (\\w+)$");
Yifan Hong443df792017-05-09 18:49:45 -0700138
Steven Morelandd8e20192017-05-24 11:23:08 -0700139 bool isDesiredContext = false;
Yifan Hong443df792017-05-09 18:49:45 -0700140 std::string line;
141 std::smatch match;
142 while(getline(ifs, line)) {
Steven Morelandd8e20192017-05-24 11:23:08 -0700143 if (std::regex_search(line, match, kContextLine)) {
144 isDesiredContext = match.str(1) == contextName;
Yifan Hong443df792017-05-09 18:49:45 -0700145 continue;
146 }
Steven Morelandd8e20192017-05-24 11:23:08 -0700147
148 if (!isDesiredContext) {
Yifan Hong443df792017-05-09 18:49:45 -0700149 continue;
150 }
Steven Morelandd8e20192017-05-24 11:23:08 -0700151
152 eachLine(line);
Yifan Hong443df792017-05-09 18:49:45 -0700153 }
154 return true;
155}
156
Steven Morelandd8e20192017-05-24 11:23:08 -0700157bool ListCommand::getPidInfo(
158 pid_t serverPid, PidInfo *pidInfo) const {
159 static const std::regex kReferencePrefix("^\\s*node \\d+:\\s+u([0-9a-f]+)\\s+c([0-9a-f]+)\\s+");
160 static const std::regex kThreadPrefix("^\\s*thread \\d+:\\s+l\\s+(\\d)(\\d)");
161
162 std::smatch match;
163 return scanBinderContext(serverPid, "hwbinder", [&](const std::string& line) {
164 if (std::regex_search(line, match, kReferencePrefix)) {
165 const std::string &ptrString = "0x" + match.str(2); // use number after c
166 uint64_t ptr;
167 if (!::android::base::ParseUint(ptrString.c_str(), &ptr)) {
168 // Should not reach here, but just be tolerant.
Yifan Hong76ac14a2017-09-08 14:59:04 -0700169 err() << "Could not parse number " << ptrString << std::endl;
Steven Morelandd8e20192017-05-24 11:23:08 -0700170 return;
171 }
172 const std::string proc = " proc ";
173 auto pos = line.rfind(proc);
174 if (pos != std::string::npos) {
175 for (const std::string &pidStr : split(line.substr(pos + proc.size()), ' ')) {
176 int32_t pid;
177 if (!::android::base::ParseInt(pidStr, &pid)) {
Yifan Hong76ac14a2017-09-08 14:59:04 -0700178 err() << "Could not parse number " << pidStr << std::endl;
Steven Morelandd8e20192017-05-24 11:23:08 -0700179 return;
180 }
181 pidInfo->refPids[ptr].push_back(pid);
182 }
183 }
184
185 return;
186 }
187
188 if (std::regex_search(line, match, kThreadPrefix)) {
189 // "1" is waiting in binder driver
190 // "2" is poll. It's impossible to tell if these are in use.
191 // and HIDL default code doesn't use it.
192 bool isInUse = match.str(1) != "1";
193 // "0" is a thread that has called into binder
194 // "1" is looper thread
195 // "2" is main looper thread
196 bool isHwbinderThread = match.str(2) != "0";
197
198 if (!isHwbinderThread) {
199 return;
200 }
201
202 if (isInUse) {
203 pidInfo->threadUsage++;
204 }
205
206 pidInfo->threadCount++;
207 return;
208 }
209
210 // not reference or thread line
211 return;
212 });
213}
214
Yifan Hong1243dde2017-09-14 17:49:30 -0700215const PidInfo* ListCommand::getPidInfoCached(pid_t serverPid) {
216 auto pair = mCachedPidInfos.insert({serverPid, PidInfo{}});
217 if (pair.second /* did insertion take place? */) {
218 if (!getPidInfo(serverPid, &pair.first->second)) {
219 return nullptr;
220 }
221 }
222 return &pair.first->second;
223}
224
Nirav Atrecce988d2018-05-16 11:14:46 -0700225bool ListCommand::shouldReportHalType(const HalType &type) const {
226 return (std::find(mListTypes.begin(), mListTypes.end(), type) != mListTypes.end());
227}
228
Yifan Hong443df792017-05-09 18:49:45 -0700229void ListCommand::forEachTable(const std::function<void(Table &)> &f) {
Nirav Atrecce988d2018-05-16 11:14:46 -0700230 for (const auto& type : mListTypes) {
231 switch (type) {
232 case HalType::BINDERIZED_SERVICES:
233 f(mServicesTable); break;
234 case HalType::PASSTHROUGH_CLIENTS:
235 f(mPassthroughRefTable); break;
236 case HalType::PASSTHROUGH_LIBRARIES:
237 f(mImplementationsTable); break;
238 default:
239 LOG(FATAL) << __func__ << "Unknown HAL type.";
240 }
241 }
Yifan Hong443df792017-05-09 18:49:45 -0700242}
243void ListCommand::forEachTable(const std::function<void(const Table &)> &f) const {
Nirav Atrecce988d2018-05-16 11:14:46 -0700244 for (const auto& type : mListTypes) {
245 switch (type) {
246 case HalType::BINDERIZED_SERVICES:
247 f(mServicesTable); break;
248 case HalType::PASSTHROUGH_CLIENTS:
249 f(mPassthroughRefTable); break;
250 case HalType::PASSTHROUGH_LIBRARIES:
251 f(mImplementationsTable); break;
252 default:
253 LOG(FATAL) << __func__ << "Unknown HAL type.";
254 }
255 }
Yifan Hong443df792017-05-09 18:49:45 -0700256}
257
258void ListCommand::postprocess() {
259 forEachTable([this](Table &table) {
260 if (mSortColumn) {
261 std::sort(table.begin(), table.end(), mSortColumn);
262 }
263 for (TableEntry &entry : table) {
264 entry.serverCmdline = getCmdline(entry.serverPid);
265 removeDeadProcesses(&entry.clientPids);
266 for (auto pid : entry.clientPids) {
267 entry.clientCmdlines.push_back(this->getCmdline(pid));
268 }
269 }
Yifan Hongf31aa052018-02-02 15:17:51 -0800270 for (TableEntry& entry : table) {
271 entry.partition = getPartition(entry.serverPid);
272 }
Yifan Hong443df792017-05-09 18:49:45 -0700273 });
274 // use a double for loop here because lshal doesn't care about efficiency.
275 for (TableEntry &packageEntry : mImplementationsTable) {
276 std::string packageName = packageEntry.interfaceName;
Steven Morelandd4f32b32018-03-06 14:47:58 -0800277 FQName fqPackageName;
278 if (!FQName::parse(packageName.substr(0, packageName.find("::")), &fqPackageName)) {
Yifan Hong443df792017-05-09 18:49:45 -0700279 continue;
280 }
281 for (TableEntry &interfaceEntry : mPassthroughRefTable) {
282 if (interfaceEntry.arch != ARCH_UNKNOWN) {
283 continue;
284 }
Steven Morelandd4f32b32018-03-06 14:47:58 -0800285 FQName interfaceName;
286 if (!FQName::parse(splitFirst(interfaceEntry.interfaceName, '/').first, &interfaceName)) {
Yifan Hong443df792017-05-09 18:49:45 -0700287 continue;
288 }
289 if (interfaceName.getPackageAndVersion() == fqPackageName) {
290 interfaceEntry.arch = packageEntry.arch;
291 }
292 }
293 }
Yifan Hongca3b6602017-09-07 16:44:27 -0700294
295 mServicesTable.setDescription(
296 "All binderized services (registered services through hwservicemanager)");
297 mPassthroughRefTable.setDescription(
298 "All interfaces that getService() has ever return as a passthrough interface;\n"
299 "PIDs / processes shown below might be inaccurate because the process\n"
300 "might have relinquished the interface or might have died.\n"
301 "The Server / Server CMD column can be ignored.\n"
302 "The Clients / Clients CMD column shows all process that have ever dlopen'ed \n"
303 "the library and successfully fetched the passthrough implementation.");
304 mImplementationsTable.setDescription(
Steven Moreland3d2c1e12018-03-14 10:41:13 -0700305 "All available passthrough implementations (all -impl.so files).\n"
306 "These may return subclasses through their respective HIDL_FETCH_I* functions.");
Yifan Hong443df792017-05-09 18:49:45 -0700307}
308
Yifan Hongb2d096a2018-05-01 15:25:23 -0700309bool ListCommand::addEntryWithInstance(const TableEntry& entry,
310 vintf::HalManifest* manifest) const {
311 FqInstance fqInstance;
312 if (!fqInstance.setTo(entry.interfaceName)) {
313 err() << "Warning: '" << entry.interfaceName << "' is not a valid FqInstance." << std::endl;
314 return false;
Yifan Hong77c87822017-06-19 15:47:39 -0700315 }
Yifan Hongb2d096a2018-05-01 15:25:23 -0700316
317 if (fqInstance.getPackage() == gIBaseFqName.package()) {
318 return true; // always remove IBase from manifest
319 }
320
321 Partition partition = resolvePartition(entry.partition, fqInstance);
322
323 if (partition == Partition::UNKNOWN) {
324 err() << "Warning: Cannot guess the partition of FqInstance " << fqInstance.string()
325 << std::endl;
326 return false;
327 }
328
329 if (partition != mVintfPartition) {
330 return true; // strip out instances that is in a different partition.
331 }
332
333 vintf::Transport transport;
334 vintf::Arch arch;
335 if (entry.transport == "hwbinder") {
336 transport = vintf::Transport::HWBINDER;
337 arch = vintf::Arch::ARCH_EMPTY;
338 } else if (entry.transport == "passthrough") {
339 transport = vintf::Transport::PASSTHROUGH;
340 switch (entry.arch) {
341 case lshal::ARCH32:
342 arch = vintf::Arch::ARCH_32;
343 break;
344 case lshal::ARCH64:
345 arch = vintf::Arch::ARCH_64;
346 break;
347 case lshal::ARCH_BOTH:
348 arch = vintf::Arch::ARCH_32_64;
349 break;
350 case lshal::ARCH_UNKNOWN: // fallthrough
351 default:
352 err() << "Warning: '" << entry.interfaceName << "' doesn't have bitness info.";
353 return false;
354 }
355 } else {
356 err() << "Warning: '" << entry.transport << "' is not a valid transport." << std::endl;
357 return false;
358 }
359
360 std::string e;
361 if (!manifest->insertInstance(fqInstance, transport, arch, vintf::HalFormat::HIDL, &e)) {
362 err() << "Warning: Cannot insert '" << fqInstance.string() << ": " << e << std::endl;
363 return false;
364 }
365 return true;
366}
367
368bool ListCommand::addEntryWithoutInstance(const TableEntry& entry,
369 const vintf::HalManifest* manifest) const {
370 const auto& packageAndVersion = splitFirst(splitFirst(entry.interfaceName, ':').first, '@');
371 const auto& package = packageAndVersion.first;
372 vintf::Version version;
373 if (!vintf::parse(packageAndVersion.second, &version)) {
374 err() << "Warning: Cannot parse version '" << packageAndVersion.second << "' for entry '"
375 << entry.interfaceName << "'" << std::endl;
376 return false;
377 }
378
379 bool found = false;
380 (void)manifest->forEachInstanceOfVersion(package, version, [&found](const auto&) {
381 found = true;
382 return false; // break
383 });
384 return found;
Yifan Hong77c87822017-06-19 15:47:39 -0700385}
386
Yifan Hongca3b6602017-09-07 16:44:27 -0700387void ListCommand::dumpVintf(const NullableOStream<std::ostream>& out) const {
Yifan Hong236301c2017-06-19 12:27:08 -0700388 using vintf::operator|=;
Yifan Hongf31aa052018-02-02 15:17:51 -0800389 using vintf::operator<<;
Yifan Hongb2d096a2018-05-01 15:25:23 -0700390 using namespace std::placeholders;
Yifan Hong443df792017-05-09 18:49:45 -0700391
392 vintf::HalManifest manifest;
Yifan Hongf31aa052018-02-02 15:17:51 -0800393 manifest.setType(toSchemaType(mVintfPartition));
Yifan Hong443df792017-05-09 18:49:45 -0700394
Yifan Hongb2d096a2018-05-01 15:25:23 -0700395 std::vector<std::string> error;
396 for (const TableEntry& entry : mServicesTable)
397 if (!addEntryWithInstance(entry, &manifest)) error.push_back(entry.interfaceName);
398 for (const TableEntry& entry : mPassthroughRefTable)
399 if (!addEntryWithInstance(entry, &manifest)) error.push_back(entry.interfaceName);
Yifan Hong443df792017-05-09 18:49:45 -0700400
Yifan Hongb2d096a2018-05-01 15:25:23 -0700401 std::vector<std::string> passthrough;
402 for (const TableEntry& entry : mImplementationsTable)
403 if (!addEntryWithoutInstance(entry, &manifest)) passthrough.push_back(entry.interfaceName);
Yifan Hongf31aa052018-02-02 15:17:51 -0800404
Yifan Hongf31aa052018-02-02 15:17:51 -0800405 out << "<!-- " << std::endl
Yifan Hongb2d096a2018-05-01 15:25:23 -0700406 << " This is a skeleton " << manifest.type() << " manifest. Notes: " << std::endl
407 << INIT_VINTF_NOTES;
408 if (!error.empty()) {
409 out << std::endl << " The following HALs are not added; see warnings." << std::endl;
410 for (const auto& e : error) {
411 out << " " << e << std::endl;
412 }
413 }
414 if (!passthrough.empty()) {
415 out << std::endl
416 << " The following HALs are passthrough and no interface or instance " << std::endl
417 << " names can be inferred." << std::endl;
418 for (const auto& e : passthrough) {
419 out << " " << e << std::endl;
420 }
421 }
422 out << "-->" << std::endl;
423 out << vintf::gHalManifestConverter(manifest, vintf::SerializeFlag::HALS_ONLY);
Yifan Hong443df792017-05-09 18:49:45 -0700424}
425
Yifan Hongf31aa052018-02-02 15:17:51 -0800426std::string ListCommand::INIT_VINTF_NOTES{
Yifan Hongb2d096a2018-05-01 15:25:23 -0700427 " 1. If a HAL is supported in both hwbinder and passthrough transport,\n"
Yifan Hongf31aa052018-02-02 15:17:51 -0800428 " only hwbinder is shown.\n"
429 " 2. It is likely that HALs in passthrough transport does not have\n"
430 " <interface> declared; users will have to write them by hand.\n"
431 " 3. A HAL with lower minor version can be overridden by a HAL with\n"
432 " higher minor version if they have the same name and major version.\n"
Yifan Hongb2d096a2018-05-01 15:25:23 -0700433 " 4. This output is intended for launch devices.\n"
434 " Upgrading devices should not use this tool to generate device\n"
435 " manifest and replace the existing manifest directly, but should\n"
436 " edit the existing manifest manually.\n"
437 " Specifically, devices which launched at Android O-MR1 or earlier\n"
438 " should not use the 'fqname' format for required HAL entries and\n"
439 " should instead use the legacy package, name, instance-name format\n"
440 " until they are updated.\n"
Yifan Hongf31aa052018-02-02 15:17:51 -0800441};
442
Yifan Hong443df792017-05-09 18:49:45 -0700443static Architecture fromBaseArchitecture(::android::hidl::base::V1_0::DebugInfo::Architecture a) {
444 switch (a) {
445 case ::android::hidl::base::V1_0::DebugInfo::Architecture::IS_64BIT:
446 return ARCH64;
447 case ::android::hidl::base::V1_0::DebugInfo::Architecture::IS_32BIT:
448 return ARCH32;
449 case ::android::hidl::base::V1_0::DebugInfo::Architecture::UNKNOWN: // fallthrough
450 default:
451 return ARCH_UNKNOWN;
452 }
453}
454
Yifan Hongca3b6602017-09-07 16:44:27 -0700455void ListCommand::dumpTable(const NullableOStream<std::ostream>& out) const {
Yifan Hong1bc1e9f2017-08-29 17:28:12 -0700456 if (mNeat) {
Yifan Hongd4a77e82017-09-06 19:40:24 -0700457 MergedTable({&mServicesTable, &mPassthroughRefTable, &mImplementationsTable})
Yifan Hongca3b6602017-09-07 16:44:27 -0700458 .createTextTable().dump(out.buf());
Yifan Hong1bc1e9f2017-08-29 17:28:12 -0700459 return;
460 }
461
Yifan Hongca3b6602017-09-07 16:44:27 -0700462 forEachTable([this, &out](const Table &table) {
Yifan Hong1bc1e9f2017-08-29 17:28:12 -0700463
Yifan Hongd4a77e82017-09-06 19:40:24 -0700464 // We're only interested in dumping debug info for already
465 // instantiated services. There's little value in dumping the
466 // debug info for a service we create on the fly, so we only operate
467 // on the "mServicesTable".
468 std::function<std::string(const std::string&)> emitDebugInfo = nullptr;
469 if (mEmitDebugInfo && &table == &mServicesTable) {
470 emitDebugInfo = [this](const auto& iName) {
Yifan Hongca3b6602017-09-07 16:44:27 -0700471 std::stringstream ss;
Yifan Hongd4a77e82017-09-06 19:40:24 -0700472 auto pair = splitFirst(iName, '/');
Steven Moreland5f328892018-01-18 14:38:07 -0800473 mLshal.emitDebugInfo(pair.first, pair.second, {},
474 false /* excludesParentInstances */, ss,
Yifan Hong1bc1e9f2017-08-29 17:28:12 -0700475 NullableOStream<std::ostream>(nullptr));
Yifan Hongca3b6602017-09-07 16:44:27 -0700476 return ss.str();
Yifan Hongd4a77e82017-09-06 19:40:24 -0700477 };
Yifan Hong443df792017-05-09 18:49:45 -0700478 }
Yifan Hongca3b6602017-09-07 16:44:27 -0700479 table.createTextTable(mNeat, emitDebugInfo).dump(out.buf());
480 out << std::endl;
Yifan Hong1bc1e9f2017-08-29 17:28:12 -0700481 });
Yifan Hong443df792017-05-09 18:49:45 -0700482}
483
Yifan Hongca3b6602017-09-07 16:44:27 -0700484Status ListCommand::dump() {
485 auto dump = mVintf ? &ListCommand::dumpVintf : &ListCommand::dumpTable;
486
487 if (mFileOutputPath.empty()) {
488 (*this.*dump)(out());
489 return OK;
Yifan Hong443df792017-05-09 18:49:45 -0700490 }
Yifan Hongca3b6602017-09-07 16:44:27 -0700491
492 std::ofstream fileOutput(mFileOutputPath);
493 if (!fileOutput.is_open()) {
494 err() << "Could not open file '" << mFileOutputPath << "'." << std::endl;
495 return IO_ERROR;
496 }
497 chown(mFileOutputPath.c_str(), AID_SHELL, AID_SHELL);
498
499 (*this.*dump)(NullableOStream<std::ostream>(fileOutput));
500
501 fileOutput.flush();
502 fileOutput.close();
503 return OK;
Yifan Hong443df792017-05-09 18:49:45 -0700504}
505
506void ListCommand::putEntry(TableEntrySource source, TableEntry &&entry) {
507 Table *table = nullptr;
508 switch (source) {
509 case HWSERVICEMANAGER_LIST :
510 table = &mServicesTable; break;
511 case PTSERVICEMANAGER_REG_CLIENT :
512 table = &mPassthroughRefTable; break;
513 case LIST_DLLIB :
514 table = &mImplementationsTable; break;
515 default:
Yifan Hong76ac14a2017-09-08 14:59:04 -0700516 err() << "Error: Unknown source of entry " << source << std::endl;
Yifan Hong443df792017-05-09 18:49:45 -0700517 }
518 if (table) {
Yifan Hongd4a77e82017-09-06 19:40:24 -0700519 table->add(std::forward<TableEntry>(entry));
Yifan Hong443df792017-05-09 18:49:45 -0700520 }
521}
522
523Status ListCommand::fetchAllLibraries(const sp<IServiceManager> &manager) {
Nirav Atrecce988d2018-05-16 11:14:46 -0700524 if (!shouldReportHalType(HalType::PASSTHROUGH_LIBRARIES)) { return OK; }
525
Yifan Hong443df792017-05-09 18:49:45 -0700526 using namespace ::android::hardware;
527 using namespace ::android::hidl::manager::V1_0;
528 using namespace ::android::hidl::base::V1_0;
Yifan Hongf2d557b2017-05-24 19:45:02 -0700529 using std::literals::chrono_literals::operator""s;
530 auto ret = timeoutIPC(2s, manager, &IServiceManager::debugDump, [&] (const auto &infos) {
Yifan Hong443df792017-05-09 18:49:45 -0700531 std::map<std::string, TableEntry> entries;
532 for (const auto &info : infos) {
533 std::string interfaceName = std::string{info.interfaceName.c_str()} + "/" +
534 std::string{info.instanceName.c_str()};
535 entries.emplace(interfaceName, TableEntry{
536 .interfaceName = interfaceName,
537 .transport = "passthrough",
Yifan Hongf2d557b2017-05-24 19:45:02 -0700538 .clientPids = info.clientPids,
Yifan Hong443df792017-05-09 18:49:45 -0700539 }).first->second.arch |= fromBaseArchitecture(info.arch);
540 }
541 for (auto &&pair : entries) {
542 putEntry(LIST_DLLIB, std::move(pair.second));
543 }
544 });
545 if (!ret.isOk()) {
Yifan Hong76ac14a2017-09-08 14:59:04 -0700546 err() << "Error: Failed to call list on getPassthroughServiceManager(): "
Yifan Hong443df792017-05-09 18:49:45 -0700547 << ret.description() << std::endl;
548 return DUMP_ALL_LIBS_ERROR;
549 }
550 return OK;
551}
552
553Status ListCommand::fetchPassthrough(const sp<IServiceManager> &manager) {
Nirav Atrecce988d2018-05-16 11:14:46 -0700554 if (!shouldReportHalType(HalType::PASSTHROUGH_CLIENTS)) { return OK; }
555
Yifan Hong443df792017-05-09 18:49:45 -0700556 using namespace ::android::hardware;
557 using namespace ::android::hardware::details;
558 using namespace ::android::hidl::manager::V1_0;
559 using namespace ::android::hidl::base::V1_0;
560 auto ret = timeoutIPC(manager, &IServiceManager::debugDump, [&] (const auto &infos) {
561 for (const auto &info : infos) {
562 if (info.clientPids.size() <= 0) {
563 continue;
564 }
565 putEntry(PTSERVICEMANAGER_REG_CLIENT, {
566 .interfaceName =
567 std::string{info.interfaceName.c_str()} + "/" +
568 std::string{info.instanceName.c_str()},
569 .transport = "passthrough",
570 .serverPid = info.clientPids.size() == 1 ? info.clientPids[0] : NO_PID,
Yifan Hong443df792017-05-09 18:49:45 -0700571 .clientPids = info.clientPids,
572 .arch = fromBaseArchitecture(info.arch)
573 });
574 }
575 });
576 if (!ret.isOk()) {
Yifan Hong76ac14a2017-09-08 14:59:04 -0700577 err() << "Error: Failed to call debugDump on defaultServiceManager(): "
Yifan Hong443df792017-05-09 18:49:45 -0700578 << ret.description() << std::endl;
579 return DUMP_PASSTHROUGH_ERROR;
580 }
581 return OK;
582}
583
584Status ListCommand::fetchBinderized(const sp<IServiceManager> &manager) {
Nirav Atrecce988d2018-05-16 11:14:46 -0700585 if (!shouldReportHalType(HalType::BINDERIZED_SERVICES)) { return OK; }
Yifan Hong443df792017-05-09 18:49:45 -0700586
Nirav Atrecce988d2018-05-16 11:14:46 -0700587 const std::string mode = "hwbinder";
Yifan Hong443df792017-05-09 18:49:45 -0700588 hidl_vec<hidl_string> fqInstanceNames;
589 // copying out for timeoutIPC
590 auto listRet = timeoutIPC(manager, &IServiceManager::list, [&] (const auto &names) {
591 fqInstanceNames = names;
592 });
593 if (!listRet.isOk()) {
Yifan Hong76ac14a2017-09-08 14:59:04 -0700594 err() << "Error: Failed to list services for " << mode << ": "
Yifan Hong443df792017-05-09 18:49:45 -0700595 << listRet.description() << std::endl;
596 return DUMP_BINDERIZED_ERROR;
597 }
598
599 Status status = OK;
Yifan Hong22ea7b82017-09-14 18:07:43 -0700600 std::map<std::string, TableEntry> allTableEntries;
Yifan Hong443df792017-05-09 18:49:45 -0700601 for (const auto &fqInstanceName : fqInstanceNames) {
Yifan Hong22ea7b82017-09-14 18:07:43 -0700602 // create entry and default assign all fields.
603 TableEntry& entry = allTableEntries[fqInstanceName];
604 entry.interfaceName = fqInstanceName;
605 entry.transport = mode;
606
607 status |= fetchBinderizedEntry(manager, &entry);
608 }
609
610 for (auto& pair : allTableEntries) {
611 putEntry(HWSERVICEMANAGER_LIST, std::move(pair.second));
612 }
613 return status;
614}
615
616Status ListCommand::fetchBinderizedEntry(const sp<IServiceManager> &manager,
617 TableEntry *entry) {
618 Status status = OK;
619 const auto handleError = [&](Status additionalError, const std::string& msg) {
620 err() << "Warning: Skipping \"" << entry->interfaceName << "\": " << msg << std::endl;
621 status |= DUMP_BINDERIZED_ERROR | additionalError;
622 };
623
624 const auto pair = splitFirst(entry->interfaceName, '/');
625 const auto &serviceName = pair.first;
626 const auto &instanceName = pair.second;
627 auto getRet = timeoutIPC(manager, &IServiceManager::get, serviceName, instanceName);
628 if (!getRet.isOk()) {
629 handleError(TRANSACTION_ERROR,
630 "cannot be fetched from service manager:" + getRet.description());
631 return status;
632 }
633 sp<IBase> service = getRet;
634 if (service == nullptr) {
635 handleError(NO_INTERFACE, "cannot be fetched from service manager (null)");
636 return status;
637 }
638
639 // getDebugInfo
640 do {
641 DebugInfo debugInfo;
642 auto debugRet = timeoutIPC(service, &IBase::getDebugInfo, [&] (const auto &received) {
643 debugInfo = received;
Yifan Hong443df792017-05-09 18:49:45 -0700644 });
645 if (!debugRet.isOk()) {
Yifan Hong22ea7b82017-09-14 18:07:43 -0700646 handleError(TRANSACTION_ERROR,
647 "debugging information cannot be retrieved: " + debugRet.description());
648 break; // skip getPidInfo
Yifan Hong443df792017-05-09 18:49:45 -0700649 }
Steven Morelandd8e20192017-05-24 11:23:08 -0700650
Yifan Hong22ea7b82017-09-14 18:07:43 -0700651 entry->serverPid = debugInfo.pid;
652 entry->serverObjectAddress = debugInfo.ptr;
653 entry->arch = fromBaseArchitecture(debugInfo.arch);
Steven Morelandd8e20192017-05-24 11:23:08 -0700654
Yifan Hong22ea7b82017-09-14 18:07:43 -0700655 if (debugInfo.pid != NO_PID) {
656 const PidInfo* pidInfo = getPidInfoCached(debugInfo.pid);
657 if (pidInfo == nullptr) {
658 handleError(IO_ERROR,
659 "no information for PID " + std::to_string(debugInfo.pid) +
660 ", are you root?");
661 break;
662 }
663 if (debugInfo.ptr != NO_PTR) {
664 auto it = pidInfo->refPids.find(debugInfo.ptr);
665 if (it != pidInfo->refPids.end()) {
666 entry->clientPids = it->second;
667 }
668 }
669 entry->threadUsage = pidInfo->threadUsage;
670 entry->threadCount = pidInfo->threadCount;
671 }
672 } while (0);
Yifan Hongfee209d2017-09-14 18:23:38 -0700673
674 // hash
675 do {
676 ssize_t hashIndex = -1;
677 auto ifaceChainRet = timeoutIPC(service, &IBase::interfaceChain, [&] (const auto& c) {
678 for (size_t i = 0; i < c.size(); ++i) {
679 if (serviceName == c[i]) {
680 hashIndex = static_cast<ssize_t>(i);
681 break;
682 }
683 }
684 });
685 if (!ifaceChainRet.isOk()) {
686 handleError(TRANSACTION_ERROR,
687 "interfaceChain fails: " + ifaceChainRet.description());
688 break; // skip getHashChain
689 }
690 if (hashIndex < 0) {
691 handleError(BAD_IMPL, "Interface name does not exist in interfaceChain.");
692 break; // skip getHashChain
693 }
694 auto hashRet = timeoutIPC(service, &IBase::getHashChain, [&] (const auto& hashChain) {
695 if (static_cast<size_t>(hashIndex) >= hashChain.size()) {
696 handleError(BAD_IMPL,
697 "interfaceChain indicates position " + std::to_string(hashIndex) +
698 " but getHashChain returns " + std::to_string(hashChain.size()) +
699 " hashes");
700 return;
701 }
702
703 auto&& hashArray = hashChain[hashIndex];
704 std::vector<uint8_t> hashVec{hashArray.data(), hashArray.data() + hashArray.size()};
705 entry->hash = Hash::hexString(hashVec);
706 });
707 if (!hashRet.isOk()) {
708 handleError(TRANSACTION_ERROR, "getHashChain failed: " + hashRet.description());
709 }
710 } while (0);
Yifan Hong443df792017-05-09 18:49:45 -0700711 return status;
712}
713
714Status ListCommand::fetch() {
715 Status status = OK;
Yifan Hong9881df92017-05-10 14:33:05 -0700716 auto bManager = mLshal.serviceManager();
Yifan Hong443df792017-05-09 18:49:45 -0700717 if (bManager == nullptr) {
Yifan Hong76ac14a2017-09-08 14:59:04 -0700718 err() << "Failed to get defaultServiceManager()!" << std::endl;
Yifan Hong443df792017-05-09 18:49:45 -0700719 status |= NO_BINDERIZED_MANAGER;
720 } else {
721 status |= fetchBinderized(bManager);
722 // Passthrough PIDs are registered to the binderized manager as well.
723 status |= fetchPassthrough(bManager);
724 }
725
Yifan Hong9881df92017-05-10 14:33:05 -0700726 auto pManager = mLshal.passthroughManager();
Yifan Hong443df792017-05-09 18:49:45 -0700727 if (pManager == nullptr) {
Yifan Hong76ac14a2017-09-08 14:59:04 -0700728 err() << "Failed to get getPassthroughServiceManager()!" << std::endl;
Yifan Hong443df792017-05-09 18:49:45 -0700729 status |= NO_PASSTHROUGH_MANAGER;
730 } else {
731 status |= fetchAllLibraries(pManager);
732 }
733 return status;
734}
735
Yifan Honga6b93f02017-09-13 16:53:37 -0700736void ListCommand::registerAllOptions() {
737 int v = mOptions.size();
738 // A list of acceptable command line options
739 // key: value returned by getopt_long
740 // long options with short alternatives
741 mOptions.push_back({'h', "help", no_argument, v++, [](ListCommand*, const char*) {
742 return USAGE;
743 }, ""});
744 mOptions.push_back({'i', "interface", no_argument, v++, [](ListCommand* thiz, const char*) {
745 thiz->mSelectedColumns.push_back(TableColumnType::INTERFACE_NAME);
746 return OK;
747 }, "print the instance name column"});
Yifan Hongfee209d2017-09-14 18:23:38 -0700748 mOptions.push_back({'l', "released", no_argument, v++, [](ListCommand* thiz, const char*) {
749 thiz->mSelectedColumns.push_back(TableColumnType::RELEASED);
750 return OK;
751 }, "print the 'is released?' column\n(Y=released, empty=unreleased or unknown)"});
Yifan Honga6b93f02017-09-13 16:53:37 -0700752 mOptions.push_back({'t', "transport", no_argument, v++, [](ListCommand* thiz, const char*) {
753 thiz->mSelectedColumns.push_back(TableColumnType::TRANSPORT);
754 return OK;
755 }, "print the transport mode column"});
756 mOptions.push_back({'r', "arch", no_argument, v++, [](ListCommand* thiz, const char*) {
757 thiz->mSelectedColumns.push_back(TableColumnType::ARCH);
758 return OK;
759 }, "print the bitness column"});
Yifan Hongfee209d2017-09-14 18:23:38 -0700760 mOptions.push_back({'s', "hash", no_argument, v++, [](ListCommand* thiz, const char*) {
761 thiz->mSelectedColumns.push_back(TableColumnType::HASH);
762 return OK;
763 }, "print hash of the interface"});
Yifan Honga6b93f02017-09-13 16:53:37 -0700764 mOptions.push_back({'p', "pid", no_argument, v++, [](ListCommand* thiz, const char*) {
765 thiz->mSelectedColumns.push_back(TableColumnType::SERVER_PID);
766 return OK;
767 }, "print the server PID, or server cmdline if -m is set"});
768 mOptions.push_back({'a', "address", no_argument, v++, [](ListCommand* thiz, const char*) {
769 thiz->mSelectedColumns.push_back(TableColumnType::SERVER_ADDR);
770 return OK;
771 }, "print the server object address column"});
772 mOptions.push_back({'c', "clients", no_argument, v++, [](ListCommand* thiz, const char*) {
773 thiz->mSelectedColumns.push_back(TableColumnType::CLIENT_PIDS);
774 return OK;
775 }, "print the client PIDs, or client cmdlines if -m is set"});
776 mOptions.push_back({'e', "threads", no_argument, v++, [](ListCommand* thiz, const char*) {
777 thiz->mSelectedColumns.push_back(TableColumnType::THREADS);
778 return OK;
779 }, "print currently used/available threads\n(note, available threads created lazily)"});
780 mOptions.push_back({'m', "cmdline", no_argument, v++, [](ListCommand* thiz, const char*) {
781 thiz->mEnableCmdlines = true;
782 return OK;
783 }, "print cmdline instead of PIDs"});
784 mOptions.push_back({'d', "debug", optional_argument, v++, [](ListCommand* thiz, const char* arg) {
785 thiz->mEmitDebugInfo = true;
786 if (arg) thiz->mFileOutputPath = arg;
787 return OK;
788 }, "Emit debug info from\nIBase::debug with empty options. Cannot be used with --neat.\n"
789 "Writes to specified file if 'arg' is provided, otherwise stdout."});
790
791 // long options without short alternatives
792 mOptions.push_back({'\0', "init-vintf", no_argument, v++, [](ListCommand* thiz, const char* arg) {
793 thiz->mVintf = true;
Yifan Hongf31aa052018-02-02 15:17:51 -0800794 if (thiz->mVintfPartition == Partition::UNKNOWN)
795 thiz->mVintfPartition = Partition::VENDOR;
Yifan Honga6b93f02017-09-13 16:53:37 -0700796 if (arg) thiz->mFileOutputPath = arg;
797 return OK;
798 }, "form a skeleton HAL manifest to specified file,\nor stdout if no file specified."});
Yifan Hongf31aa052018-02-02 15:17:51 -0800799 mOptions.push_back({'\0', "init-vintf-partition", required_argument, v++, [](ListCommand* thiz, const char* arg) {
800 if (!arg) return USAGE;
801 thiz->mVintfPartition = android::procpartition::parsePartition(arg);
802 if (thiz->mVintfPartition == Partition::UNKNOWN) return USAGE;
803 return OK;
804 }, "Specify the partition of the HAL manifest\ngenerated by --init-vintf.\n"
805 "Valid values are 'system', 'vendor', and 'odm'. Default is 'vendor'."});
Yifan Honga6b93f02017-09-13 16:53:37 -0700806 mOptions.push_back({'\0', "sort", required_argument, v++, [](ListCommand* thiz, const char* arg) {
807 if (strcmp(arg, "interface") == 0 || strcmp(arg, "i") == 0) {
808 thiz->mSortColumn = TableEntry::sortByInterfaceName;
809 } else if (strcmp(arg, "pid") == 0 || strcmp(arg, "p") == 0) {
810 thiz->mSortColumn = TableEntry::sortByServerPid;
811 } else {
812 thiz->err() << "Unrecognized sorting column: " << arg << std::endl;
813 return USAGE;
814 }
815 return OK;
816 }, "sort by a column. 'arg' can be (i|interface) or (p|pid)."});
817 mOptions.push_back({'\0', "neat", no_argument, v++, [](ListCommand* thiz, const char*) {
818 thiz->mNeat = true;
819 return OK;
820 }, "output is machine parsable (no explanatory text).\nCannot be used with --debug."});
Nirav Atrecce988d2018-05-16 11:14:46 -0700821 mOptions.push_back({'\0', "types", required_argument, v++, [](ListCommand* thiz, const char* arg) {
822 if (!arg) { return USAGE; }
823
824 static const std::map<std::string, HalType> kHalTypeMap {
825 {"binderized", HalType::BINDERIZED_SERVICES},
826 {"b", HalType::BINDERIZED_SERVICES},
827 {"passthrough_clients", HalType::PASSTHROUGH_CLIENTS},
828 {"c", HalType::PASSTHROUGH_CLIENTS},
829 {"passthrough_libs", HalType::PASSTHROUGH_LIBRARIES},
830 {"l", HalType::PASSTHROUGH_LIBRARIES}
831 };
832
833 std::vector<std::string> halTypesArgs = split(std::string(arg), ',');
834 for (const auto& halTypeArg : halTypesArgs) {
835 if (halTypeArg.empty()) continue;
836
837 const auto& halTypeIter = kHalTypeMap.find(halTypeArg);
838 if (halTypeIter == kHalTypeMap.end()) {
839
840 thiz->err() << "Unrecognized HAL type: " << halTypeArg << std::endl;
841 return USAGE;
842 }
843
844 // Append unique (non-repeated) HAL types to the reporting list
845 HalType halType = halTypeIter->second;
846 if (std::find(thiz->mListTypes.begin(), thiz->mListTypes.end(), halType) ==
847 thiz->mListTypes.end()) {
848 thiz->mListTypes.push_back(halType);
849 }
850 }
851
852 if (thiz->mListTypes.empty()) { return USAGE; }
853 return OK;
854 }, "comma-separated list of one or more HAL types.\nThe output is restricted to the selected "
855 "association(s). Valid options\nare: (b|binderized), (c|passthrough_clients), and (l|"
856 "passthrough_libs).\nBy default, lists all available HALs."});
Yifan Honga6b93f02017-09-13 16:53:37 -0700857}
858
859// Create 'longopts' argument to getopt_long. Caller is responsible for maintaining
860// the lifetime of "options" during the usage of the returned array.
861static std::unique_ptr<struct option[]> getLongOptions(
862 const ListCommand::RegisteredOptions& options,
863 int* longOptFlag) {
864 std::unique_ptr<struct option[]> ret{new struct option[options.size() + 1]};
865 int i = 0;
866 for (const auto& e : options) {
867 ret[i].name = e.longOption.c_str();
868 ret[i].has_arg = e.hasArg;
869 ret[i].flag = longOptFlag;
870 ret[i].val = e.val;
871
872 i++;
873 }
874 // getopt_long last option has all zeros
875 ret[i].name = NULL;
876 ret[i].has_arg = 0;
877 ret[i].flag = NULL;
878 ret[i].val = 0;
879
880 return ret;
881}
882
883// Create 'optstring' argument to getopt_long.
884static std::string getShortOptions(const ListCommand::RegisteredOptions& options) {
885 std::stringstream ss;
886 for (const auto& e : options) {
887 if (e.shortOption != '\0') {
888 ss << e.shortOption;
889 }
890 }
891 return ss.str();
892}
893
Yifan Honga8bedc62017-09-08 18:00:31 -0700894Status ListCommand::parseArgs(const Arg &arg) {
Nirav Atrecce988d2018-05-16 11:14:46 -0700895 mListTypes.clear();
Yifan Hong443df792017-05-09 18:49:45 -0700896
Yifan Honga6b93f02017-09-13 16:53:37 -0700897 if (mOptions.empty()) {
898 registerAllOptions();
899 }
900 int longOptFlag;
901 std::unique_ptr<struct option[]> longOptions = getLongOptions(mOptions, &longOptFlag);
902 std::string shortOptions = getShortOptions(mOptions);
Yifan Hongd4a77e82017-09-06 19:40:24 -0700903
Yifan Honga8bedc62017-09-08 18:00:31 -0700904 // suppress output to std::err for unknown options
905 opterr = 0;
906
Yifan Hong443df792017-05-09 18:49:45 -0700907 int optionIndex;
908 int c;
909 // Lshal::parseArgs has set optind to the next option to parse
910 for (;;) {
Yifan Hong443df792017-05-09 18:49:45 -0700911 c = getopt_long(arg.argc, arg.argv,
Yifan Honga6b93f02017-09-13 16:53:37 -0700912 shortOptions.c_str(), longOptions.get(), &optionIndex);
Yifan Hong443df792017-05-09 18:49:45 -0700913 if (c == -1) {
914 break;
915 }
Yifan Honga6b93f02017-09-13 16:53:37 -0700916 const RegisteredOption* found = nullptr;
917 if (c == 0) {
918 // see long option
919 for (const auto& e : mOptions) {
920 if (longOptFlag == e.val) found = &e;
Yifan Hong443df792017-05-09 18:49:45 -0700921 }
Yifan Honga6b93f02017-09-13 16:53:37 -0700922 } else {
923 // see short option
924 for (const auto& e : mOptions) {
925 if (c == e.shortOption) found = &e;
926 }
Yifan Hong443df792017-05-09 18:49:45 -0700927 }
Yifan Honga6b93f02017-09-13 16:53:37 -0700928
929 if (found == nullptr) {
930 // see unrecognized options
Yifan Honga8bedc62017-09-08 18:00:31 -0700931 err() << "unrecognized option `" << arg.argv[optind - 1] << "'" << std::endl;
Yifan Hong443df792017-05-09 18:49:45 -0700932 return USAGE;
933 }
Yifan Honga6b93f02017-09-13 16:53:37 -0700934
935 Status status = found->op(this, optarg);
936 if (status != OK) {
937 return status;
938 }
Yifan Hong443df792017-05-09 18:49:45 -0700939 }
940 if (optind < arg.argc) {
941 // see non option
Yifan Honga8bedc62017-09-08 18:00:31 -0700942 err() << "unrecognized option `" << arg.argv[optind] << "'" << std::endl;
Yifan Hong1bc1e9f2017-08-29 17:28:12 -0700943 return USAGE;
944 }
945
946 if (mNeat && mEmitDebugInfo) {
Yifan Hong76ac14a2017-09-08 14:59:04 -0700947 err() << "Error: --neat should not be used with --debug." << std::endl;
Yifan Hong1bc1e9f2017-08-29 17:28:12 -0700948 return USAGE;
Yifan Hong443df792017-05-09 18:49:45 -0700949 }
950
Yifan Honga6b93f02017-09-13 16:53:37 -0700951 if (mSelectedColumns.empty()) {
Yifan Hongfee209d2017-09-14 18:23:38 -0700952 mSelectedColumns = {TableColumnType::RELEASED,
953 TableColumnType::INTERFACE_NAME, TableColumnType::THREADS,
Yifan Hong05494a52017-08-29 18:50:00 -0700954 TableColumnType::SERVER_PID, TableColumnType::CLIENT_PIDS};
Yifan Hong443df792017-05-09 18:49:45 -0700955 }
Yifan Hongd4a77e82017-09-06 19:40:24 -0700956
Yifan Honga6b93f02017-09-13 16:53:37 -0700957 if (mEnableCmdlines) {
958 for (size_t i = 0; i < mSelectedColumns.size(); ++i) {
959 if (mSelectedColumns[i] == TableColumnType::SERVER_PID) {
960 mSelectedColumns[i] = TableColumnType::SERVER_CMD;
Yifan Hongd4a77e82017-09-06 19:40:24 -0700961 }
Yifan Honga6b93f02017-09-13 16:53:37 -0700962 if (mSelectedColumns[i] == TableColumnType::CLIENT_PIDS) {
963 mSelectedColumns[i] = TableColumnType::CLIENT_CMDS;
Yifan Hongd4a77e82017-09-06 19:40:24 -0700964 }
965 }
966 }
967
Nirav Atrecce988d2018-05-16 11:14:46 -0700968 // By default, list all HAL types
969 if (mListTypes.empty()) {
970 mListTypes = {HalType::BINDERIZED_SERVICES, HalType::PASSTHROUGH_CLIENTS,
971 HalType::PASSTHROUGH_LIBRARIES};
972 }
973
Yifan Honga6b93f02017-09-13 16:53:37 -0700974 forEachTable([this] (Table& table) {
975 table.setSelectedColumns(this->mSelectedColumns);
Yifan Hongd4a77e82017-09-06 19:40:24 -0700976 });
977
Yifan Hong443df792017-05-09 18:49:45 -0700978 return OK;
979}
980
Yifan Honga8bedc62017-09-08 18:00:31 -0700981Status ListCommand::main(const Arg &arg) {
982 Status status = parseArgs(arg);
Yifan Hong443df792017-05-09 18:49:45 -0700983 if (status != OK) {
984 return status;
985 }
986 status = fetch();
987 postprocess();
Yifan Hongca3b6602017-09-07 16:44:27 -0700988 status |= dump();
Yifan Hong443df792017-05-09 18:49:45 -0700989 return status;
990}
991
Yifan Honga6b93f02017-09-13 16:53:37 -0700992const std::string& ListCommand::RegisteredOption::getHelpMessageForArgument() const {
993 static const std::string empty{};
994 static const std::string optional{"[=<arg>]"};
995 static const std::string required{"=<arg>"};
996
997 if (hasArg == optional_argument) {
998 return optional;
999 }
1000 if (hasArg == required_argument) {
1001 return required;
1002 }
1003 return empty;
1004}
1005
Yifan Honga8bedc62017-09-08 18:00:31 -07001006void ListCommand::usage() const {
1007
Yifan Honga6b93f02017-09-13 16:53:37 -07001008 err() << "list:" << std::endl
1009 << " lshal" << std::endl
1010 << " lshal list" << std::endl
Yifan Hongfee209d2017-09-14 18:23:38 -07001011 << " List all hals with default ordering and columns (`lshal list -riepc`)" << std::endl
Yifan Honga6b93f02017-09-13 16:53:37 -07001012 << " lshal list [-h|--help]" << std::endl
1013 << " -h, --help: Print help message for list (`lshal help list`)" << std::endl
1014 << " lshal [list] [OPTIONS...]" << std::endl;
1015 for (const auto& e : mOptions) {
1016 if (e.help.empty()) {
1017 continue;
1018 }
1019 err() << " ";
1020 if (e.shortOption != '\0')
1021 err() << "-" << e.shortOption << e.getHelpMessageForArgument();
1022 if (e.shortOption != '\0' && !e.longOption.empty())
1023 err() << ", ";
1024 if (!e.longOption.empty())
1025 err() << "--" << e.longOption << e.getHelpMessageForArgument();
1026 err() << ": ";
Nirav Atrecce988d2018-05-16 11:14:46 -07001027 std::vector<std::string> lines = split(e.help, '\n');
Yifan Honga6b93f02017-09-13 16:53:37 -07001028 for (const auto& line : lines) {
1029 if (&line != &lines.front())
1030 err() << " ";
1031 err() << line << std::endl;
1032 }
1033 }
Yifan Honga8bedc62017-09-08 18:00:31 -07001034}
1035
Yifan Hong443df792017-05-09 18:49:45 -07001036} // namespace lshal
1037} // namespace android
Yifan Hong05494a52017-08-29 18:50:00 -07001038