blob: 4249165df77669be5547630d92d9fbe32f55168f [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>
Yifan Hongb2d096a2018-05-01 15:25:23 -070022#include <functional>
Yifan Hong443df792017-05-09 18:49:45 -070023#include <iomanip>
24#include <iostream>
25#include <map>
Yifan Hong443df792017-05-09 18:49:45 -070026#include <regex>
Yifan Hongb2d096a2018-05-01 15:25:23 -070027#include <sstream>
Yifan Hong443df792017-05-09 18:49:45 -070028
Yifan Hongf31aa052018-02-02 15:17:51 -080029#include <android-base/file.h>
Yifan Hong443df792017-05-09 18:49:45 -070030#include <android-base/parseint.h>
31#include <android/hidl/manager/1.0/IServiceManager.h>
Yifan Hongfee209d2017-09-14 18:23:38 -070032#include <hidl-hash/Hash.h>
Yifan Hong443df792017-05-09 18:49:45 -070033#include <hidl-util/FQName.h>
34#include <private/android_filesystem_config.h>
35#include <sys/stat.h>
36#include <vintf/HalManifest.h>
Yifan Hongf31aa052018-02-02 15:17:51 -080037#include <vintf/parse_string.h>
Yifan Hong443df792017-05-09 18:49:45 -070038#include <vintf/parse_xml.h>
39
40#include "Lshal.h"
41#include "PipeRelay.h"
42#include "Timeout.h"
43#include "utils.h"
44
45using ::android::hardware::hidl_string;
Yifan Hong22ea7b82017-09-14 18:07:43 -070046using ::android::hardware::hidl_vec;
47using ::android::hidl::base::V1_0::DebugInfo;
48using ::android::hidl::base::V1_0::IBase;
Yifan Hong443df792017-05-09 18:49:45 -070049using ::android::hidl::manager::V1_0::IServiceManager;
50
51namespace android {
52namespace lshal {
53
Yifan Hongf31aa052018-02-02 15:17:51 -080054vintf::SchemaType toSchemaType(Partition p) {
55 return (p == Partition::SYSTEM) ? vintf::SchemaType::FRAMEWORK : vintf::SchemaType::DEVICE;
56}
57
Yifan Hong76ac14a2017-09-08 14:59:04 -070058NullableOStream<std::ostream> ListCommand::out() const {
59 return mLshal.out();
60}
61
62NullableOStream<std::ostream> ListCommand::err() const {
63 return mLshal.err();
Yifan Hong443df792017-05-09 18:49:45 -070064}
65
Yifan Hong795b6ec2017-09-13 11:25:28 -070066std::string ListCommand::GetName() {
67 return "list";
68}
69std::string ListCommand::getSimpleDescription() const {
70 return "List HALs.";
71}
72
Yifan Hong8bf73162017-09-07 18:06:13 -070073std::string ListCommand::parseCmdline(pid_t pid) const {
Yifan Hongf31aa052018-02-02 15:17:51 -080074 return android::procpartition::getCmdline(pid);
Yifan Hong443df792017-05-09 18:49:45 -070075}
76
77const std::string &ListCommand::getCmdline(pid_t pid) {
78 auto pair = mCmdlines.find(pid);
79 if (pair != mCmdlines.end()) {
80 return pair->second;
81 }
Yifan Hong8bf73162017-09-07 18:06:13 -070082 mCmdlines[pid] = parseCmdline(pid);
Yifan Hong443df792017-05-09 18:49:45 -070083 return mCmdlines[pid];
84}
85
86void ListCommand::removeDeadProcesses(Pids *pids) {
87 static const pid_t myPid = getpid();
Yifan Hong61fb7bc2017-05-12 16:33:57 -070088 pids->erase(std::remove_if(pids->begin(), pids->end(), [this](auto pid) {
Yifan Hong443df792017-05-09 18:49:45 -070089 return pid == myPid || this->getCmdline(pid).empty();
Yifan Hong61fb7bc2017-05-12 16:33:57 -070090 }), pids->end());
Yifan Hong443df792017-05-09 18:49:45 -070091}
92
Yifan Hongf31aa052018-02-02 15:17:51 -080093Partition ListCommand::getPartition(pid_t pid) {
94 auto it = mPartitions.find(pid);
95 if (it != mPartitions.end()) {
96 return it->second;
97 }
98 Partition partition = android::procpartition::getPartition(pid);
99 mPartitions.emplace(pid, partition);
100 return partition;
101}
102
103// Give sensible defaults when nothing can be inferred from runtime.
104// process: Partition inferred from executable location or cmdline.
Yifan Hongb2d096a2018-05-01 15:25:23 -0700105Partition ListCommand::resolvePartition(Partition process, const FqInstance& fqInstance) const {
106 if (fqInstance.inPackage("vendor") || fqInstance.inPackage("com")) {
Yifan Hongf31aa052018-02-02 15:17:51 -0800107 return Partition::VENDOR;
108 }
109
Yifan Hongb2d096a2018-05-01 15:25:23 -0700110 if (fqInstance.inPackage("android.frameworks") || fqInstance.inPackage("android.system") ||
111 fqInstance.inPackage("android.hidl")) {
Yifan Hongf31aa052018-02-02 15:17:51 -0800112 return Partition::SYSTEM;
113 }
114
115 // Some android.hardware HALs are served from system. Check the value from executable
116 // location / cmdline first.
Yifan Hongb2d096a2018-05-01 15:25:23 -0700117 if (fqInstance.inPackage("android.hardware")) {
Yifan Hongf31aa052018-02-02 15:17:51 -0800118 if (process != Partition::UNKNOWN) {
119 return process;
120 }
121 return Partition::VENDOR;
122 }
123
124 return process;
125}
126
Yifan Hong1243dde2017-09-14 17:49:30 -0700127static bool scanBinderContext(pid_t pid,
Steven Morelandd8e20192017-05-24 11:23:08 -0700128 const std::string &contextName,
129 std::function<void(const std::string&)> eachLine) {
130 std::ifstream ifs("/d/binder/proc/" + std::to_string(pid));
Yifan Hong443df792017-05-09 18:49:45 -0700131 if (!ifs.is_open()) {
132 return false;
133 }
134
Steven Morelandd8e20192017-05-24 11:23:08 -0700135 static const std::regex kContextLine("^context (\\w+)$");
Yifan Hong443df792017-05-09 18:49:45 -0700136
Steven Morelandd8e20192017-05-24 11:23:08 -0700137 bool isDesiredContext = false;
Yifan Hong443df792017-05-09 18:49:45 -0700138 std::string line;
139 std::smatch match;
140 while(getline(ifs, line)) {
Steven Morelandd8e20192017-05-24 11:23:08 -0700141 if (std::regex_search(line, match, kContextLine)) {
142 isDesiredContext = match.str(1) == contextName;
Yifan Hong443df792017-05-09 18:49:45 -0700143 continue;
144 }
Steven Morelandd8e20192017-05-24 11:23:08 -0700145
146 if (!isDesiredContext) {
Yifan Hong443df792017-05-09 18:49:45 -0700147 continue;
148 }
Steven Morelandd8e20192017-05-24 11:23:08 -0700149
150 eachLine(line);
Yifan Hong443df792017-05-09 18:49:45 -0700151 }
152 return true;
153}
154
Steven Morelandd8e20192017-05-24 11:23:08 -0700155bool ListCommand::getPidInfo(
156 pid_t serverPid, PidInfo *pidInfo) const {
157 static const std::regex kReferencePrefix("^\\s*node \\d+:\\s+u([0-9a-f]+)\\s+c([0-9a-f]+)\\s+");
158 static const std::regex kThreadPrefix("^\\s*thread \\d+:\\s+l\\s+(\\d)(\\d)");
159
160 std::smatch match;
161 return scanBinderContext(serverPid, "hwbinder", [&](const std::string& line) {
162 if (std::regex_search(line, match, kReferencePrefix)) {
163 const std::string &ptrString = "0x" + match.str(2); // use number after c
164 uint64_t ptr;
165 if (!::android::base::ParseUint(ptrString.c_str(), &ptr)) {
166 // Should not reach here, but just be tolerant.
Yifan Hong76ac14a2017-09-08 14:59:04 -0700167 err() << "Could not parse number " << ptrString << std::endl;
Steven Morelandd8e20192017-05-24 11:23:08 -0700168 return;
169 }
170 const std::string proc = " proc ";
171 auto pos = line.rfind(proc);
172 if (pos != std::string::npos) {
173 for (const std::string &pidStr : split(line.substr(pos + proc.size()), ' ')) {
174 int32_t pid;
175 if (!::android::base::ParseInt(pidStr, &pid)) {
Yifan Hong76ac14a2017-09-08 14:59:04 -0700176 err() << "Could not parse number " << pidStr << std::endl;
Steven Morelandd8e20192017-05-24 11:23:08 -0700177 return;
178 }
179 pidInfo->refPids[ptr].push_back(pid);
180 }
181 }
182
183 return;
184 }
185
186 if (std::regex_search(line, match, kThreadPrefix)) {
187 // "1" is waiting in binder driver
188 // "2" is poll. It's impossible to tell if these are in use.
189 // and HIDL default code doesn't use it.
190 bool isInUse = match.str(1) != "1";
191 // "0" is a thread that has called into binder
192 // "1" is looper thread
193 // "2" is main looper thread
194 bool isHwbinderThread = match.str(2) != "0";
195
196 if (!isHwbinderThread) {
197 return;
198 }
199
200 if (isInUse) {
201 pidInfo->threadUsage++;
202 }
203
204 pidInfo->threadCount++;
205 return;
206 }
207
208 // not reference or thread line
209 return;
210 });
211}
212
Yifan Hong1243dde2017-09-14 17:49:30 -0700213const PidInfo* ListCommand::getPidInfoCached(pid_t serverPid) {
214 auto pair = mCachedPidInfos.insert({serverPid, PidInfo{}});
215 if (pair.second /* did insertion take place? */) {
216 if (!getPidInfo(serverPid, &pair.first->second)) {
217 return nullptr;
218 }
219 }
220 return &pair.first->second;
221}
222
Yifan Hong443df792017-05-09 18:49:45 -0700223// Must process hwbinder services first, then passthrough services.
224void ListCommand::forEachTable(const std::function<void(Table &)> &f) {
225 f(mServicesTable);
226 f(mPassthroughRefTable);
227 f(mImplementationsTable);
228}
229void ListCommand::forEachTable(const std::function<void(const Table &)> &f) const {
230 f(mServicesTable);
231 f(mPassthroughRefTable);
232 f(mImplementationsTable);
233}
234
235void ListCommand::postprocess() {
236 forEachTable([this](Table &table) {
237 if (mSortColumn) {
238 std::sort(table.begin(), table.end(), mSortColumn);
239 }
240 for (TableEntry &entry : table) {
241 entry.serverCmdline = getCmdline(entry.serverPid);
242 removeDeadProcesses(&entry.clientPids);
243 for (auto pid : entry.clientPids) {
244 entry.clientCmdlines.push_back(this->getCmdline(pid));
245 }
246 }
Yifan Hongf31aa052018-02-02 15:17:51 -0800247 for (TableEntry& entry : table) {
248 entry.partition = getPartition(entry.serverPid);
249 }
Yifan Hong443df792017-05-09 18:49:45 -0700250 });
251 // use a double for loop here because lshal doesn't care about efficiency.
252 for (TableEntry &packageEntry : mImplementationsTable) {
253 std::string packageName = packageEntry.interfaceName;
Steven Morelandd4f32b32018-03-06 14:47:58 -0800254 FQName fqPackageName;
255 if (!FQName::parse(packageName.substr(0, packageName.find("::")), &fqPackageName)) {
Yifan Hong443df792017-05-09 18:49:45 -0700256 continue;
257 }
258 for (TableEntry &interfaceEntry : mPassthroughRefTable) {
259 if (interfaceEntry.arch != ARCH_UNKNOWN) {
260 continue;
261 }
Steven Morelandd4f32b32018-03-06 14:47:58 -0800262 FQName interfaceName;
263 if (!FQName::parse(splitFirst(interfaceEntry.interfaceName, '/').first, &interfaceName)) {
Yifan Hong443df792017-05-09 18:49:45 -0700264 continue;
265 }
266 if (interfaceName.getPackageAndVersion() == fqPackageName) {
267 interfaceEntry.arch = packageEntry.arch;
268 }
269 }
270 }
Yifan Hongca3b6602017-09-07 16:44:27 -0700271
272 mServicesTable.setDescription(
273 "All binderized services (registered services through hwservicemanager)");
274 mPassthroughRefTable.setDescription(
275 "All interfaces that getService() has ever return as a passthrough interface;\n"
276 "PIDs / processes shown below might be inaccurate because the process\n"
277 "might have relinquished the interface or might have died.\n"
278 "The Server / Server CMD column can be ignored.\n"
279 "The Clients / Clients CMD column shows all process that have ever dlopen'ed \n"
280 "the library and successfully fetched the passthrough implementation.");
281 mImplementationsTable.setDescription(
Steven Moreland3d2c1e12018-03-14 10:41:13 -0700282 "All available passthrough implementations (all -impl.so files).\n"
283 "These may return subclasses through their respective HIDL_FETCH_I* functions.");
Yifan Hong443df792017-05-09 18:49:45 -0700284}
285
Yifan Hongb2d096a2018-05-01 15:25:23 -0700286bool ListCommand::addEntryWithInstance(const TableEntry& entry,
287 vintf::HalManifest* manifest) const {
288 FqInstance fqInstance;
289 if (!fqInstance.setTo(entry.interfaceName)) {
290 err() << "Warning: '" << entry.interfaceName << "' is not a valid FqInstance." << std::endl;
291 return false;
Yifan Hong77c87822017-06-19 15:47:39 -0700292 }
Yifan Hongb2d096a2018-05-01 15:25:23 -0700293
294 if (fqInstance.getPackage() == gIBaseFqName.package()) {
295 return true; // always remove IBase from manifest
296 }
297
298 Partition partition = resolvePartition(entry.partition, fqInstance);
299
300 if (partition == Partition::UNKNOWN) {
301 err() << "Warning: Cannot guess the partition of FqInstance " << fqInstance.string()
302 << std::endl;
303 return false;
304 }
305
306 if (partition != mVintfPartition) {
307 return true; // strip out instances that is in a different partition.
308 }
309
310 vintf::Transport transport;
311 vintf::Arch arch;
312 if (entry.transport == "hwbinder") {
313 transport = vintf::Transport::HWBINDER;
314 arch = vintf::Arch::ARCH_EMPTY;
315 } else if (entry.transport == "passthrough") {
316 transport = vintf::Transport::PASSTHROUGH;
317 switch (entry.arch) {
318 case lshal::ARCH32:
319 arch = vintf::Arch::ARCH_32;
320 break;
321 case lshal::ARCH64:
322 arch = vintf::Arch::ARCH_64;
323 break;
324 case lshal::ARCH_BOTH:
325 arch = vintf::Arch::ARCH_32_64;
326 break;
327 case lshal::ARCH_UNKNOWN: // fallthrough
328 default:
329 err() << "Warning: '" << entry.interfaceName << "' doesn't have bitness info.";
330 return false;
331 }
332 } else {
333 err() << "Warning: '" << entry.transport << "' is not a valid transport." << std::endl;
334 return false;
335 }
336
337 std::string e;
338 if (!manifest->insertInstance(fqInstance, transport, arch, vintf::HalFormat::HIDL, &e)) {
339 err() << "Warning: Cannot insert '" << fqInstance.string() << ": " << e << std::endl;
340 return false;
341 }
342 return true;
343}
344
345bool ListCommand::addEntryWithoutInstance(const TableEntry& entry,
346 const vintf::HalManifest* manifest) const {
347 const auto& packageAndVersion = splitFirst(splitFirst(entry.interfaceName, ':').first, '@');
348 const auto& package = packageAndVersion.first;
349 vintf::Version version;
350 if (!vintf::parse(packageAndVersion.second, &version)) {
351 err() << "Warning: Cannot parse version '" << packageAndVersion.second << "' for entry '"
352 << entry.interfaceName << "'" << std::endl;
353 return false;
354 }
355
356 bool found = false;
357 (void)manifest->forEachInstanceOfVersion(package, version, [&found](const auto&) {
358 found = true;
359 return false; // break
360 });
361 return found;
Yifan Hong77c87822017-06-19 15:47:39 -0700362}
363
Yifan Hongca3b6602017-09-07 16:44:27 -0700364void ListCommand::dumpVintf(const NullableOStream<std::ostream>& out) const {
Yifan Hong236301c2017-06-19 12:27:08 -0700365 using vintf::operator|=;
Yifan Hongf31aa052018-02-02 15:17:51 -0800366 using vintf::operator<<;
Yifan Hongb2d096a2018-05-01 15:25:23 -0700367 using namespace std::placeholders;
Yifan Hong443df792017-05-09 18:49:45 -0700368
369 vintf::HalManifest manifest;
Yifan Hongf31aa052018-02-02 15:17:51 -0800370 manifest.setType(toSchemaType(mVintfPartition));
Yifan Hong443df792017-05-09 18:49:45 -0700371
Yifan Hongb2d096a2018-05-01 15:25:23 -0700372 std::vector<std::string> error;
373 for (const TableEntry& entry : mServicesTable)
374 if (!addEntryWithInstance(entry, &manifest)) error.push_back(entry.interfaceName);
375 for (const TableEntry& entry : mPassthroughRefTable)
376 if (!addEntryWithInstance(entry, &manifest)) error.push_back(entry.interfaceName);
Yifan Hong443df792017-05-09 18:49:45 -0700377
Yifan Hongb2d096a2018-05-01 15:25:23 -0700378 std::vector<std::string> passthrough;
379 for (const TableEntry& entry : mImplementationsTable)
380 if (!addEntryWithoutInstance(entry, &manifest)) passthrough.push_back(entry.interfaceName);
Yifan Hongf31aa052018-02-02 15:17:51 -0800381
Yifan Hongf31aa052018-02-02 15:17:51 -0800382 out << "<!-- " << std::endl
Yifan Hongb2d096a2018-05-01 15:25:23 -0700383 << " This is a skeleton " << manifest.type() << " manifest. Notes: " << std::endl
384 << INIT_VINTF_NOTES;
385 if (!error.empty()) {
386 out << std::endl << " The following HALs are not added; see warnings." << std::endl;
387 for (const auto& e : error) {
388 out << " " << e << std::endl;
389 }
390 }
391 if (!passthrough.empty()) {
392 out << std::endl
393 << " The following HALs are passthrough and no interface or instance " << std::endl
394 << " names can be inferred." << std::endl;
395 for (const auto& e : passthrough) {
396 out << " " << e << std::endl;
397 }
398 }
399 out << "-->" << std::endl;
400 out << vintf::gHalManifestConverter(manifest, vintf::SerializeFlag::HALS_ONLY);
Yifan Hong443df792017-05-09 18:49:45 -0700401}
402
Yifan Hongf31aa052018-02-02 15:17:51 -0800403std::string ListCommand::INIT_VINTF_NOTES{
Yifan Hongb2d096a2018-05-01 15:25:23 -0700404 " 1. If a HAL is supported in both hwbinder and passthrough transport,\n"
Yifan Hongf31aa052018-02-02 15:17:51 -0800405 " only hwbinder is shown.\n"
406 " 2. It is likely that HALs in passthrough transport does not have\n"
407 " <interface> declared; users will have to write them by hand.\n"
408 " 3. A HAL with lower minor version can be overridden by a HAL with\n"
409 " higher minor version if they have the same name and major version.\n"
Yifan Hongb2d096a2018-05-01 15:25:23 -0700410 " 4. This output is intended for launch devices.\n"
411 " Upgrading devices should not use this tool to generate device\n"
412 " manifest and replace the existing manifest directly, but should\n"
413 " edit the existing manifest manually.\n"
414 " Specifically, devices which launched at Android O-MR1 or earlier\n"
415 " should not use the 'fqname' format for required HAL entries and\n"
416 " should instead use the legacy package, name, instance-name format\n"
417 " until they are updated.\n"
Yifan Hongf31aa052018-02-02 15:17:51 -0800418};
419
Yifan Hong443df792017-05-09 18:49:45 -0700420static Architecture fromBaseArchitecture(::android::hidl::base::V1_0::DebugInfo::Architecture a) {
421 switch (a) {
422 case ::android::hidl::base::V1_0::DebugInfo::Architecture::IS_64BIT:
423 return ARCH64;
424 case ::android::hidl::base::V1_0::DebugInfo::Architecture::IS_32BIT:
425 return ARCH32;
426 case ::android::hidl::base::V1_0::DebugInfo::Architecture::UNKNOWN: // fallthrough
427 default:
428 return ARCH_UNKNOWN;
429 }
430}
431
Yifan Hongca3b6602017-09-07 16:44:27 -0700432void ListCommand::dumpTable(const NullableOStream<std::ostream>& out) const {
Yifan Hong1bc1e9f2017-08-29 17:28:12 -0700433 if (mNeat) {
Yifan Hongd4a77e82017-09-06 19:40:24 -0700434 MergedTable({&mServicesTable, &mPassthroughRefTable, &mImplementationsTable})
Yifan Hongca3b6602017-09-07 16:44:27 -0700435 .createTextTable().dump(out.buf());
Yifan Hong1bc1e9f2017-08-29 17:28:12 -0700436 return;
437 }
438
Yifan Hongca3b6602017-09-07 16:44:27 -0700439 forEachTable([this, &out](const Table &table) {
Yifan Hong1bc1e9f2017-08-29 17:28:12 -0700440
Yifan Hongd4a77e82017-09-06 19:40:24 -0700441 // We're only interested in dumping debug info for already
442 // instantiated services. There's little value in dumping the
443 // debug info for a service we create on the fly, so we only operate
444 // on the "mServicesTable".
445 std::function<std::string(const std::string&)> emitDebugInfo = nullptr;
446 if (mEmitDebugInfo && &table == &mServicesTable) {
447 emitDebugInfo = [this](const auto& iName) {
Yifan Hongca3b6602017-09-07 16:44:27 -0700448 std::stringstream ss;
Yifan Hongd4a77e82017-09-06 19:40:24 -0700449 auto pair = splitFirst(iName, '/');
Steven Moreland5f328892018-01-18 14:38:07 -0800450 mLshal.emitDebugInfo(pair.first, pair.second, {},
451 false /* excludesParentInstances */, ss,
Yifan Hong1bc1e9f2017-08-29 17:28:12 -0700452 NullableOStream<std::ostream>(nullptr));
Yifan Hongca3b6602017-09-07 16:44:27 -0700453 return ss.str();
Yifan Hongd4a77e82017-09-06 19:40:24 -0700454 };
Yifan Hong443df792017-05-09 18:49:45 -0700455 }
Yifan Hongca3b6602017-09-07 16:44:27 -0700456 table.createTextTable(mNeat, emitDebugInfo).dump(out.buf());
457 out << std::endl;
Yifan Hong1bc1e9f2017-08-29 17:28:12 -0700458 });
Yifan Hong443df792017-05-09 18:49:45 -0700459}
460
Yifan Hongca3b6602017-09-07 16:44:27 -0700461Status ListCommand::dump() {
462 auto dump = mVintf ? &ListCommand::dumpVintf : &ListCommand::dumpTable;
463
464 if (mFileOutputPath.empty()) {
465 (*this.*dump)(out());
466 return OK;
Yifan Hong443df792017-05-09 18:49:45 -0700467 }
Yifan Hongca3b6602017-09-07 16:44:27 -0700468
469 std::ofstream fileOutput(mFileOutputPath);
470 if (!fileOutput.is_open()) {
471 err() << "Could not open file '" << mFileOutputPath << "'." << std::endl;
472 return IO_ERROR;
473 }
474 chown(mFileOutputPath.c_str(), AID_SHELL, AID_SHELL);
475
476 (*this.*dump)(NullableOStream<std::ostream>(fileOutput));
477
478 fileOutput.flush();
479 fileOutput.close();
480 return OK;
Yifan Hong443df792017-05-09 18:49:45 -0700481}
482
483void ListCommand::putEntry(TableEntrySource source, TableEntry &&entry) {
484 Table *table = nullptr;
485 switch (source) {
486 case HWSERVICEMANAGER_LIST :
487 table = &mServicesTable; break;
488 case PTSERVICEMANAGER_REG_CLIENT :
489 table = &mPassthroughRefTable; break;
490 case LIST_DLLIB :
491 table = &mImplementationsTable; break;
492 default:
Yifan Hong76ac14a2017-09-08 14:59:04 -0700493 err() << "Error: Unknown source of entry " << source << std::endl;
Yifan Hong443df792017-05-09 18:49:45 -0700494 }
495 if (table) {
Yifan Hongd4a77e82017-09-06 19:40:24 -0700496 table->add(std::forward<TableEntry>(entry));
Yifan Hong443df792017-05-09 18:49:45 -0700497 }
498}
499
500Status ListCommand::fetchAllLibraries(const sp<IServiceManager> &manager) {
501 using namespace ::android::hardware;
502 using namespace ::android::hidl::manager::V1_0;
503 using namespace ::android::hidl::base::V1_0;
Yifan Hongf2d557b2017-05-24 19:45:02 -0700504 using std::literals::chrono_literals::operator""s;
505 auto ret = timeoutIPC(2s, manager, &IServiceManager::debugDump, [&] (const auto &infos) {
Yifan Hong443df792017-05-09 18:49:45 -0700506 std::map<std::string, TableEntry> entries;
507 for (const auto &info : infos) {
508 std::string interfaceName = std::string{info.interfaceName.c_str()} + "/" +
509 std::string{info.instanceName.c_str()};
510 entries.emplace(interfaceName, TableEntry{
511 .interfaceName = interfaceName,
512 .transport = "passthrough",
Yifan Hongf2d557b2017-05-24 19:45:02 -0700513 .clientPids = info.clientPids,
Yifan Hong443df792017-05-09 18:49:45 -0700514 }).first->second.arch |= fromBaseArchitecture(info.arch);
515 }
516 for (auto &&pair : entries) {
517 putEntry(LIST_DLLIB, std::move(pair.second));
518 }
519 });
520 if (!ret.isOk()) {
Yifan Hong76ac14a2017-09-08 14:59:04 -0700521 err() << "Error: Failed to call list on getPassthroughServiceManager(): "
Yifan Hong443df792017-05-09 18:49:45 -0700522 << ret.description() << std::endl;
523 return DUMP_ALL_LIBS_ERROR;
524 }
525 return OK;
526}
527
528Status ListCommand::fetchPassthrough(const sp<IServiceManager> &manager) {
529 using namespace ::android::hardware;
530 using namespace ::android::hardware::details;
531 using namespace ::android::hidl::manager::V1_0;
532 using namespace ::android::hidl::base::V1_0;
533 auto ret = timeoutIPC(manager, &IServiceManager::debugDump, [&] (const auto &infos) {
534 for (const auto &info : infos) {
535 if (info.clientPids.size() <= 0) {
536 continue;
537 }
538 putEntry(PTSERVICEMANAGER_REG_CLIENT, {
539 .interfaceName =
540 std::string{info.interfaceName.c_str()} + "/" +
541 std::string{info.instanceName.c_str()},
542 .transport = "passthrough",
543 .serverPid = info.clientPids.size() == 1 ? info.clientPids[0] : NO_PID,
Yifan Hong443df792017-05-09 18:49:45 -0700544 .clientPids = info.clientPids,
545 .arch = fromBaseArchitecture(info.arch)
546 });
547 }
548 });
549 if (!ret.isOk()) {
Yifan Hong76ac14a2017-09-08 14:59:04 -0700550 err() << "Error: Failed to call debugDump on defaultServiceManager(): "
Yifan Hong443df792017-05-09 18:49:45 -0700551 << ret.description() << std::endl;
552 return DUMP_PASSTHROUGH_ERROR;
553 }
554 return OK;
555}
556
557Status ListCommand::fetchBinderized(const sp<IServiceManager> &manager) {
Yifan Hong443df792017-05-09 18:49:45 -0700558 const std::string mode = "hwbinder";
559
560 hidl_vec<hidl_string> fqInstanceNames;
561 // copying out for timeoutIPC
562 auto listRet = timeoutIPC(manager, &IServiceManager::list, [&] (const auto &names) {
563 fqInstanceNames = names;
564 });
565 if (!listRet.isOk()) {
Yifan Hong76ac14a2017-09-08 14:59:04 -0700566 err() << "Error: Failed to list services for " << mode << ": "
Yifan Hong443df792017-05-09 18:49:45 -0700567 << listRet.description() << std::endl;
568 return DUMP_BINDERIZED_ERROR;
569 }
570
571 Status status = OK;
Yifan Hong22ea7b82017-09-14 18:07:43 -0700572 std::map<std::string, TableEntry> allTableEntries;
Yifan Hong443df792017-05-09 18:49:45 -0700573 for (const auto &fqInstanceName : fqInstanceNames) {
Yifan Hong22ea7b82017-09-14 18:07:43 -0700574 // create entry and default assign all fields.
575 TableEntry& entry = allTableEntries[fqInstanceName];
576 entry.interfaceName = fqInstanceName;
577 entry.transport = mode;
578
579 status |= fetchBinderizedEntry(manager, &entry);
580 }
581
582 for (auto& pair : allTableEntries) {
583 putEntry(HWSERVICEMANAGER_LIST, std::move(pair.second));
584 }
585 return status;
586}
587
588Status ListCommand::fetchBinderizedEntry(const sp<IServiceManager> &manager,
589 TableEntry *entry) {
590 Status status = OK;
591 const auto handleError = [&](Status additionalError, const std::string& msg) {
592 err() << "Warning: Skipping \"" << entry->interfaceName << "\": " << msg << std::endl;
593 status |= DUMP_BINDERIZED_ERROR | additionalError;
594 };
595
596 const auto pair = splitFirst(entry->interfaceName, '/');
597 const auto &serviceName = pair.first;
598 const auto &instanceName = pair.second;
599 auto getRet = timeoutIPC(manager, &IServiceManager::get, serviceName, instanceName);
600 if (!getRet.isOk()) {
601 handleError(TRANSACTION_ERROR,
602 "cannot be fetched from service manager:" + getRet.description());
603 return status;
604 }
605 sp<IBase> service = getRet;
606 if (service == nullptr) {
607 handleError(NO_INTERFACE, "cannot be fetched from service manager (null)");
608 return status;
609 }
610
611 // getDebugInfo
612 do {
613 DebugInfo debugInfo;
614 auto debugRet = timeoutIPC(service, &IBase::getDebugInfo, [&] (const auto &received) {
615 debugInfo = received;
Yifan Hong443df792017-05-09 18:49:45 -0700616 });
617 if (!debugRet.isOk()) {
Yifan Hong22ea7b82017-09-14 18:07:43 -0700618 handleError(TRANSACTION_ERROR,
619 "debugging information cannot be retrieved: " + debugRet.description());
620 break; // skip getPidInfo
Yifan Hong443df792017-05-09 18:49:45 -0700621 }
Steven Morelandd8e20192017-05-24 11:23:08 -0700622
Yifan Hong22ea7b82017-09-14 18:07:43 -0700623 entry->serverPid = debugInfo.pid;
624 entry->serverObjectAddress = debugInfo.ptr;
625 entry->arch = fromBaseArchitecture(debugInfo.arch);
Steven Morelandd8e20192017-05-24 11:23:08 -0700626
Yifan Hong22ea7b82017-09-14 18:07:43 -0700627 if (debugInfo.pid != NO_PID) {
628 const PidInfo* pidInfo = getPidInfoCached(debugInfo.pid);
629 if (pidInfo == nullptr) {
630 handleError(IO_ERROR,
631 "no information for PID " + std::to_string(debugInfo.pid) +
632 ", are you root?");
633 break;
634 }
635 if (debugInfo.ptr != NO_PTR) {
636 auto it = pidInfo->refPids.find(debugInfo.ptr);
637 if (it != pidInfo->refPids.end()) {
638 entry->clientPids = it->second;
639 }
640 }
641 entry->threadUsage = pidInfo->threadUsage;
642 entry->threadCount = pidInfo->threadCount;
643 }
644 } while (0);
Yifan Hongfee209d2017-09-14 18:23:38 -0700645
646 // hash
647 do {
648 ssize_t hashIndex = -1;
649 auto ifaceChainRet = timeoutIPC(service, &IBase::interfaceChain, [&] (const auto& c) {
650 for (size_t i = 0; i < c.size(); ++i) {
651 if (serviceName == c[i]) {
652 hashIndex = static_cast<ssize_t>(i);
653 break;
654 }
655 }
656 });
657 if (!ifaceChainRet.isOk()) {
658 handleError(TRANSACTION_ERROR,
659 "interfaceChain fails: " + ifaceChainRet.description());
660 break; // skip getHashChain
661 }
662 if (hashIndex < 0) {
663 handleError(BAD_IMPL, "Interface name does not exist in interfaceChain.");
664 break; // skip getHashChain
665 }
666 auto hashRet = timeoutIPC(service, &IBase::getHashChain, [&] (const auto& hashChain) {
667 if (static_cast<size_t>(hashIndex) >= hashChain.size()) {
668 handleError(BAD_IMPL,
669 "interfaceChain indicates position " + std::to_string(hashIndex) +
670 " but getHashChain returns " + std::to_string(hashChain.size()) +
671 " hashes");
672 return;
673 }
674
675 auto&& hashArray = hashChain[hashIndex];
676 std::vector<uint8_t> hashVec{hashArray.data(), hashArray.data() + hashArray.size()};
677 entry->hash = Hash::hexString(hashVec);
678 });
679 if (!hashRet.isOk()) {
680 handleError(TRANSACTION_ERROR, "getHashChain failed: " + hashRet.description());
681 }
682 } while (0);
Yifan Hong443df792017-05-09 18:49:45 -0700683 return status;
684}
685
686Status ListCommand::fetch() {
687 Status status = OK;
Yifan Hong9881df92017-05-10 14:33:05 -0700688 auto bManager = mLshal.serviceManager();
Yifan Hong443df792017-05-09 18:49:45 -0700689 if (bManager == nullptr) {
Yifan Hong76ac14a2017-09-08 14:59:04 -0700690 err() << "Failed to get defaultServiceManager()!" << std::endl;
Yifan Hong443df792017-05-09 18:49:45 -0700691 status |= NO_BINDERIZED_MANAGER;
692 } else {
693 status |= fetchBinderized(bManager);
694 // Passthrough PIDs are registered to the binderized manager as well.
695 status |= fetchPassthrough(bManager);
696 }
697
Yifan Hong9881df92017-05-10 14:33:05 -0700698 auto pManager = mLshal.passthroughManager();
Yifan Hong443df792017-05-09 18:49:45 -0700699 if (pManager == nullptr) {
Yifan Hong76ac14a2017-09-08 14:59:04 -0700700 err() << "Failed to get getPassthroughServiceManager()!" << std::endl;
Yifan Hong443df792017-05-09 18:49:45 -0700701 status |= NO_PASSTHROUGH_MANAGER;
702 } else {
703 status |= fetchAllLibraries(pManager);
704 }
705 return status;
706}
707
Yifan Honga6b93f02017-09-13 16:53:37 -0700708void ListCommand::registerAllOptions() {
709 int v = mOptions.size();
710 // A list of acceptable command line options
711 // key: value returned by getopt_long
712 // long options with short alternatives
713 mOptions.push_back({'h', "help", no_argument, v++, [](ListCommand*, const char*) {
714 return USAGE;
715 }, ""});
716 mOptions.push_back({'i', "interface", no_argument, v++, [](ListCommand* thiz, const char*) {
717 thiz->mSelectedColumns.push_back(TableColumnType::INTERFACE_NAME);
718 return OK;
719 }, "print the instance name column"});
Yifan Hongfee209d2017-09-14 18:23:38 -0700720 mOptions.push_back({'l', "released", no_argument, v++, [](ListCommand* thiz, const char*) {
721 thiz->mSelectedColumns.push_back(TableColumnType::RELEASED);
722 return OK;
723 }, "print the 'is released?' column\n(Y=released, empty=unreleased or unknown)"});
Yifan Honga6b93f02017-09-13 16:53:37 -0700724 mOptions.push_back({'t', "transport", no_argument, v++, [](ListCommand* thiz, const char*) {
725 thiz->mSelectedColumns.push_back(TableColumnType::TRANSPORT);
726 return OK;
727 }, "print the transport mode column"});
728 mOptions.push_back({'r', "arch", no_argument, v++, [](ListCommand* thiz, const char*) {
729 thiz->mSelectedColumns.push_back(TableColumnType::ARCH);
730 return OK;
731 }, "print the bitness column"});
Yifan Hongfee209d2017-09-14 18:23:38 -0700732 mOptions.push_back({'s', "hash", no_argument, v++, [](ListCommand* thiz, const char*) {
733 thiz->mSelectedColumns.push_back(TableColumnType::HASH);
734 return OK;
735 }, "print hash of the interface"});
Yifan Honga6b93f02017-09-13 16:53:37 -0700736 mOptions.push_back({'p', "pid", no_argument, v++, [](ListCommand* thiz, const char*) {
737 thiz->mSelectedColumns.push_back(TableColumnType::SERVER_PID);
738 return OK;
739 }, "print the server PID, or server cmdline if -m is set"});
740 mOptions.push_back({'a', "address", no_argument, v++, [](ListCommand* thiz, const char*) {
741 thiz->mSelectedColumns.push_back(TableColumnType::SERVER_ADDR);
742 return OK;
743 }, "print the server object address column"});
744 mOptions.push_back({'c', "clients", no_argument, v++, [](ListCommand* thiz, const char*) {
745 thiz->mSelectedColumns.push_back(TableColumnType::CLIENT_PIDS);
746 return OK;
747 }, "print the client PIDs, or client cmdlines if -m is set"});
748 mOptions.push_back({'e', "threads", no_argument, v++, [](ListCommand* thiz, const char*) {
749 thiz->mSelectedColumns.push_back(TableColumnType::THREADS);
750 return OK;
751 }, "print currently used/available threads\n(note, available threads created lazily)"});
752 mOptions.push_back({'m', "cmdline", no_argument, v++, [](ListCommand* thiz, const char*) {
753 thiz->mEnableCmdlines = true;
754 return OK;
755 }, "print cmdline instead of PIDs"});
756 mOptions.push_back({'d', "debug", optional_argument, v++, [](ListCommand* thiz, const char* arg) {
757 thiz->mEmitDebugInfo = true;
758 if (arg) thiz->mFileOutputPath = arg;
759 return OK;
760 }, "Emit debug info from\nIBase::debug with empty options. Cannot be used with --neat.\n"
761 "Writes to specified file if 'arg' is provided, otherwise stdout."});
762
763 // long options without short alternatives
764 mOptions.push_back({'\0', "init-vintf", no_argument, v++, [](ListCommand* thiz, const char* arg) {
765 thiz->mVintf = true;
Yifan Hongf31aa052018-02-02 15:17:51 -0800766 if (thiz->mVintfPartition == Partition::UNKNOWN)
767 thiz->mVintfPartition = Partition::VENDOR;
Yifan Honga6b93f02017-09-13 16:53:37 -0700768 if (arg) thiz->mFileOutputPath = arg;
769 return OK;
770 }, "form a skeleton HAL manifest to specified file,\nor stdout if no file specified."});
Yifan Hongf31aa052018-02-02 15:17:51 -0800771 mOptions.push_back({'\0', "init-vintf-partition", required_argument, v++, [](ListCommand* thiz, const char* arg) {
772 if (!arg) return USAGE;
773 thiz->mVintfPartition = android::procpartition::parsePartition(arg);
774 if (thiz->mVintfPartition == Partition::UNKNOWN) return USAGE;
775 return OK;
776 }, "Specify the partition of the HAL manifest\ngenerated by --init-vintf.\n"
777 "Valid values are 'system', 'vendor', and 'odm'. Default is 'vendor'."});
Yifan Honga6b93f02017-09-13 16:53:37 -0700778 mOptions.push_back({'\0', "sort", required_argument, v++, [](ListCommand* thiz, const char* arg) {
779 if (strcmp(arg, "interface") == 0 || strcmp(arg, "i") == 0) {
780 thiz->mSortColumn = TableEntry::sortByInterfaceName;
781 } else if (strcmp(arg, "pid") == 0 || strcmp(arg, "p") == 0) {
782 thiz->mSortColumn = TableEntry::sortByServerPid;
783 } else {
784 thiz->err() << "Unrecognized sorting column: " << arg << std::endl;
785 return USAGE;
786 }
787 return OK;
788 }, "sort by a column. 'arg' can be (i|interface) or (p|pid)."});
789 mOptions.push_back({'\0', "neat", no_argument, v++, [](ListCommand* thiz, const char*) {
790 thiz->mNeat = true;
791 return OK;
792 }, "output is machine parsable (no explanatory text).\nCannot be used with --debug."});
793}
794
795// Create 'longopts' argument to getopt_long. Caller is responsible for maintaining
796// the lifetime of "options" during the usage of the returned array.
797static std::unique_ptr<struct option[]> getLongOptions(
798 const ListCommand::RegisteredOptions& options,
799 int* longOptFlag) {
800 std::unique_ptr<struct option[]> ret{new struct option[options.size() + 1]};
801 int i = 0;
802 for (const auto& e : options) {
803 ret[i].name = e.longOption.c_str();
804 ret[i].has_arg = e.hasArg;
805 ret[i].flag = longOptFlag;
806 ret[i].val = e.val;
807
808 i++;
809 }
810 // getopt_long last option has all zeros
811 ret[i].name = NULL;
812 ret[i].has_arg = 0;
813 ret[i].flag = NULL;
814 ret[i].val = 0;
815
816 return ret;
817}
818
819// Create 'optstring' argument to getopt_long.
820static std::string getShortOptions(const ListCommand::RegisteredOptions& options) {
821 std::stringstream ss;
822 for (const auto& e : options) {
823 if (e.shortOption != '\0') {
824 ss << e.shortOption;
825 }
826 }
827 return ss.str();
828}
829
Yifan Honga8bedc62017-09-08 18:00:31 -0700830Status ListCommand::parseArgs(const Arg &arg) {
Yifan Hong443df792017-05-09 18:49:45 -0700831
Yifan Honga6b93f02017-09-13 16:53:37 -0700832 if (mOptions.empty()) {
833 registerAllOptions();
834 }
835 int longOptFlag;
836 std::unique_ptr<struct option[]> longOptions = getLongOptions(mOptions, &longOptFlag);
837 std::string shortOptions = getShortOptions(mOptions);
Yifan Hongd4a77e82017-09-06 19:40:24 -0700838
Yifan Honga8bedc62017-09-08 18:00:31 -0700839 // suppress output to std::err for unknown options
840 opterr = 0;
841
Yifan Hong443df792017-05-09 18:49:45 -0700842 int optionIndex;
843 int c;
844 // Lshal::parseArgs has set optind to the next option to parse
845 for (;;) {
Yifan Hong443df792017-05-09 18:49:45 -0700846 c = getopt_long(arg.argc, arg.argv,
Yifan Honga6b93f02017-09-13 16:53:37 -0700847 shortOptions.c_str(), longOptions.get(), &optionIndex);
Yifan Hong443df792017-05-09 18:49:45 -0700848 if (c == -1) {
849 break;
850 }
Yifan Honga6b93f02017-09-13 16:53:37 -0700851 const RegisteredOption* found = nullptr;
852 if (c == 0) {
853 // see long option
854 for (const auto& e : mOptions) {
855 if (longOptFlag == e.val) found = &e;
Yifan Hong443df792017-05-09 18:49:45 -0700856 }
Yifan Honga6b93f02017-09-13 16:53:37 -0700857 } else {
858 // see short option
859 for (const auto& e : mOptions) {
860 if (c == e.shortOption) found = &e;
861 }
Yifan Hong443df792017-05-09 18:49:45 -0700862 }
Yifan Honga6b93f02017-09-13 16:53:37 -0700863
864 if (found == nullptr) {
865 // see unrecognized options
Yifan Honga8bedc62017-09-08 18:00:31 -0700866 err() << "unrecognized option `" << arg.argv[optind - 1] << "'" << std::endl;
Yifan Hong443df792017-05-09 18:49:45 -0700867 return USAGE;
868 }
Yifan Honga6b93f02017-09-13 16:53:37 -0700869
870 Status status = found->op(this, optarg);
871 if (status != OK) {
872 return status;
873 }
Yifan Hong443df792017-05-09 18:49:45 -0700874 }
875 if (optind < arg.argc) {
876 // see non option
Yifan Honga8bedc62017-09-08 18:00:31 -0700877 err() << "unrecognized option `" << arg.argv[optind] << "'" << std::endl;
Yifan Hong1bc1e9f2017-08-29 17:28:12 -0700878 return USAGE;
879 }
880
881 if (mNeat && mEmitDebugInfo) {
Yifan Hong76ac14a2017-09-08 14:59:04 -0700882 err() << "Error: --neat should not be used with --debug." << std::endl;
Yifan Hong1bc1e9f2017-08-29 17:28:12 -0700883 return USAGE;
Yifan Hong443df792017-05-09 18:49:45 -0700884 }
885
Yifan Honga6b93f02017-09-13 16:53:37 -0700886 if (mSelectedColumns.empty()) {
Yifan Hongfee209d2017-09-14 18:23:38 -0700887 mSelectedColumns = {TableColumnType::RELEASED,
888 TableColumnType::INTERFACE_NAME, TableColumnType::THREADS,
Yifan Hong05494a52017-08-29 18:50:00 -0700889 TableColumnType::SERVER_PID, TableColumnType::CLIENT_PIDS};
Yifan Hong443df792017-05-09 18:49:45 -0700890 }
Yifan Hongd4a77e82017-09-06 19:40:24 -0700891
Yifan Honga6b93f02017-09-13 16:53:37 -0700892 if (mEnableCmdlines) {
893 for (size_t i = 0; i < mSelectedColumns.size(); ++i) {
894 if (mSelectedColumns[i] == TableColumnType::SERVER_PID) {
895 mSelectedColumns[i] = TableColumnType::SERVER_CMD;
Yifan Hongd4a77e82017-09-06 19:40:24 -0700896 }
Yifan Honga6b93f02017-09-13 16:53:37 -0700897 if (mSelectedColumns[i] == TableColumnType::CLIENT_PIDS) {
898 mSelectedColumns[i] = TableColumnType::CLIENT_CMDS;
Yifan Hongd4a77e82017-09-06 19:40:24 -0700899 }
900 }
901 }
902
Yifan Honga6b93f02017-09-13 16:53:37 -0700903 forEachTable([this] (Table& table) {
904 table.setSelectedColumns(this->mSelectedColumns);
Yifan Hongd4a77e82017-09-06 19:40:24 -0700905 });
906
Yifan Hong443df792017-05-09 18:49:45 -0700907 return OK;
908}
909
Yifan Honga8bedc62017-09-08 18:00:31 -0700910Status ListCommand::main(const Arg &arg) {
911 Status status = parseArgs(arg);
Yifan Hong443df792017-05-09 18:49:45 -0700912 if (status != OK) {
913 return status;
914 }
915 status = fetch();
916 postprocess();
Yifan Hongca3b6602017-09-07 16:44:27 -0700917 status |= dump();
Yifan Hong443df792017-05-09 18:49:45 -0700918 return status;
919}
920
Yifan Honga6b93f02017-09-13 16:53:37 -0700921static std::vector<std::string> splitString(const std::string &s, char c) {
922 std::vector<std::string> components;
923
924 size_t startPos = 0;
925 size_t matchPos;
926 while ((matchPos = s.find(c, startPos)) != std::string::npos) {
927 components.push_back(s.substr(startPos, matchPos - startPos));
928 startPos = matchPos + 1;
929 }
930
931 if (startPos <= s.length()) {
932 components.push_back(s.substr(startPos));
933 }
934 return components;
935}
936
937const std::string& ListCommand::RegisteredOption::getHelpMessageForArgument() const {
938 static const std::string empty{};
939 static const std::string optional{"[=<arg>]"};
940 static const std::string required{"=<arg>"};
941
942 if (hasArg == optional_argument) {
943 return optional;
944 }
945 if (hasArg == required_argument) {
946 return required;
947 }
948 return empty;
949}
950
Yifan Honga8bedc62017-09-08 18:00:31 -0700951void ListCommand::usage() const {
952
Yifan Honga6b93f02017-09-13 16:53:37 -0700953 err() << "list:" << std::endl
954 << " lshal" << std::endl
955 << " lshal list" << std::endl
Yifan Hongfee209d2017-09-14 18:23:38 -0700956 << " List all hals with default ordering and columns (`lshal list -riepc`)" << std::endl
Yifan Honga6b93f02017-09-13 16:53:37 -0700957 << " lshal list [-h|--help]" << std::endl
958 << " -h, --help: Print help message for list (`lshal help list`)" << std::endl
959 << " lshal [list] [OPTIONS...]" << std::endl;
960 for (const auto& e : mOptions) {
961 if (e.help.empty()) {
962 continue;
963 }
964 err() << " ";
965 if (e.shortOption != '\0')
966 err() << "-" << e.shortOption << e.getHelpMessageForArgument();
967 if (e.shortOption != '\0' && !e.longOption.empty())
968 err() << ", ";
969 if (!e.longOption.empty())
970 err() << "--" << e.longOption << e.getHelpMessageForArgument();
971 err() << ": ";
972 std::vector<std::string> lines = splitString(e.help, '\n');
973 for (const auto& line : lines) {
974 if (&line != &lines.front())
975 err() << " ";
976 err() << line << std::endl;
977 }
978 }
Yifan Honga8bedc62017-09-08 18:00:31 -0700979}
980
Yifan Hong443df792017-05-09 18:49:45 -0700981} // namespace lshal
982} // namespace android
Yifan Hong05494a52017-08-29 18:50:00 -0700983