blob: 2140471488a67e49254b2ede5cb7582e4ef52cc1 [file] [log] [blame]
Yifan Hongb0dde932017-02-10 17:49:58 -08001/*
2 * Copyright (C) 2016 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 "Lshal.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>
30#include <hidl/ServiceManagement.h>
Yifan Hong4b865492017-02-28 19:38:24 -080031#include <hidl-util/FQName.h>
32#include <vintf/HalManifest.h>
33#include <vintf/parse_xml.h>
Yifan Hongb0dde932017-02-10 17:49:58 -080034
Yifan Honge2dadf02017-02-14 15:43:31 -080035#include "Timeout.h"
36
Yifan Hongb0dde932017-02-10 17:49:58 -080037using ::android::hardware::hidl_string;
38using ::android::hidl::manager::V1_0::IServiceManager;
39
40namespace android {
41namespace lshal {
42
Yifan Hongb0dde932017-02-10 17:49:58 -080043template <typename A>
44std::string join(const A &components, const std::string &separator) {
45 std::stringstream out;
46 bool first = true;
47 for (const auto &component : components) {
48 if (!first) {
49 out << separator;
50 }
51 out << component;
52
53 first = false;
54 }
55 return out.str();
56}
57
58static std::string toHexString(uint64_t t) {
59 std::ostringstream os;
60 os << std::hex << std::setfill('0') << std::setw(16) << t;
61 return os.str();
62}
63
Yifan Hong4b865492017-02-28 19:38:24 -080064template<typename String>
65static std::pair<String, String> splitFirst(const String &s, char c) {
Yifan Hongb0dde932017-02-10 17:49:58 -080066 const char *pos = strchr(s.c_str(), c);
67 if (pos == nullptr) {
68 return {s, {}};
69 }
Yifan Hong4b865492017-02-28 19:38:24 -080070 return {String(s.c_str(), pos - s.c_str()), String(pos + 1)};
Yifan Hongb0dde932017-02-10 17:49:58 -080071}
72
73static std::vector<std::string> split(const std::string &s, char c) {
74 std::vector<std::string> components{};
75 size_t startPos = 0;
76 size_t matchPos;
77 while ((matchPos = s.find(c, startPos)) != std::string::npos) {
78 components.push_back(s.substr(startPos, matchPos - startPos));
79 startPos = matchPos + 1;
80 }
81
82 if (startPos <= s.length()) {
83 components.push_back(s.substr(startPos));
84 }
85 return components;
86}
87
Yifan Hong4b865492017-02-28 19:38:24 -080088static void replaceAll(std::string *s, char from, char to) {
89 for (size_t i = 0; i < s->size(); ++i) {
90 if (s->at(i) == from) {
91 s->at(i) = to;
92 }
93 }
94}
95
Yifan Hongae09a3d2017-02-14 17:33:50 -080096std::string getCmdline(pid_t pid) {
97 std::ifstream ifs("/proc/" + std::to_string(pid) + "/cmdline");
98 std::string cmdline;
99 if (!ifs.is_open()) {
100 return "";
101 }
102 ifs >> cmdline;
103 return cmdline;
104}
105
106const std::string &Lshal::getCmdline(pid_t pid) {
107 auto pair = mCmdlines.find(pid);
108 if (pair != mCmdlines.end()) {
109 return pair->second;
110 }
111 mCmdlines[pid] = ::android::lshal::getCmdline(pid);
112 return mCmdlines[pid];
113}
114
115void Lshal::removeDeadProcesses(Pids *pids) {
116 static const pid_t myPid = getpid();
117 std::remove_if(pids->begin(), pids->end(), [this](auto pid) {
118 return pid == myPid || this->getCmdline(pid).empty();
119 });
120}
121
Yifan Hongb0dde932017-02-10 17:49:58 -0800122bool Lshal::getReferencedPids(
123 pid_t serverPid, std::map<uint64_t, Pids> *objects) const {
124
125 std::ifstream ifs("/d/binder/proc/" + std::to_string(serverPid));
126 if (!ifs.is_open()) {
127 return false;
128 }
129
130 static const std::regex prefix("^\\s*node \\d+:\\s+u([0-9a-f]+)\\s+c([0-9a-f]+)\\s+");
131
132 std::string line;
133 std::smatch match;
134 while(getline(ifs, line)) {
135 if (!std::regex_search(line, match, prefix)) {
136 // the line doesn't start with the correct prefix
137 continue;
138 }
139 std::string ptrString = "0x" + match.str(2); // use number after c
140 uint64_t ptr;
141 if (!::android::base::ParseUint(ptrString.c_str(), &ptr)) {
142 // Should not reach here, but just be tolerant.
143 mErr << "Could not parse number " << ptrString << std::endl;
144 continue;
145 }
146 const std::string proc = " proc ";
147 auto pos = line.rfind(proc);
148 if (pos != std::string::npos) {
149 for (const std::string &pidStr : split(line.substr(pos + proc.size()), ' ')) {
150 int32_t pid;
151 if (!::android::base::ParseInt(pidStr, &pid)) {
152 mErr << "Could not parse number " << pidStr << std::endl;
153 continue;
154 }
155 (*objects)[ptr].push_back(pid);
156 }
157 }
158 }
159 return true;
160}
161
Yifan Hong38d53e02017-02-13 17:51:59 -0800162void Lshal::postprocess() {
163 if (mSortColumn) {
164 std::sort(mTable.begin(), mTable.end(), mSortColumn);
165 }
Yifan Hongae09a3d2017-02-14 17:33:50 -0800166 for (TableEntry &entry : mTable) {
167 entry.serverCmdline = getCmdline(entry.serverPid);
168 removeDeadProcesses(&entry.clientPids);
169 for (auto pid : entry.clientPids) {
170 entry.clientCmdlines.push_back(this->getCmdline(pid));
171 }
172 }
Yifan Hong38d53e02017-02-13 17:51:59 -0800173}
174
175void Lshal::printLine(
176 const std::string &interfaceName,
Yifan Hongb4479022017-03-02 16:54:11 -0800177 const std::string &transport,
178 const std::string &arch,
179 const std::string &server,
Yifan Hongae09a3d2017-02-14 17:33:50 -0800180 const std::string &serverCmdline,
181 const std::string &address, const std::string &clients,
182 const std::string &clientCmdlines) const {
Yifan Hong38d53e02017-02-13 17:51:59 -0800183 if (mSelectedColumns & ENABLE_INTERFACE_NAME)
184 mOut << std::setw(80) << interfaceName << "\t";
185 if (mSelectedColumns & ENABLE_TRANSPORT)
186 mOut << std::setw(10) << transport << "\t";
Yifan Hongb4479022017-03-02 16:54:11 -0800187 if (mSelectedColumns & ENABLE_ARCH)
188 mOut << std::setw(5) << arch << "\t";
Yifan Hongae09a3d2017-02-14 17:33:50 -0800189 if (mSelectedColumns & ENABLE_SERVER_PID) {
190 if (mEnableCmdlines) {
191 mOut << std::setw(15) << serverCmdline << "\t";
192 } else {
193 mOut << std::setw(5) << server << "\t";
194 }
195 }
Yifan Hong38d53e02017-02-13 17:51:59 -0800196 if (mSelectedColumns & ENABLE_SERVER_ADDR)
197 mOut << std::setw(16) << address << "\t";
Yifan Hongae09a3d2017-02-14 17:33:50 -0800198 if (mSelectedColumns & ENABLE_CLIENT_PIDS) {
199 if (mEnableCmdlines) {
200 mOut << std::setw(0) << clientCmdlines;
201 } else {
202 mOut << std::setw(0) << clients;
203 }
204 }
Yifan Hong38d53e02017-02-13 17:51:59 -0800205 mOut << std::endl;
206}
207
Yifan Hong4b865492017-02-28 19:38:24 -0800208void Lshal::dumpVintf() const {
209 vintf::HalManifest manifest;
210 for (const TableEntry &entry : mTable) {
211
212 std::string fqInstanceName = entry.interfaceName;
213
214 if (entry.source == LIST_DLLIB) {
215 // Quick hack to work around *'s
216 replaceAll(&fqInstanceName, '*', 'D');
217 }
218 auto splittedFqInstanceName = splitFirst(fqInstanceName, '/');
219 FQName fqName(splittedFqInstanceName.first);
220 if (!fqName.isValid()) {
221 mErr << "Warning: '" << splittedFqInstanceName.first
222 << "' is not a valid FQName." << std::endl;
223 continue;
224 }
225 // Strip out system libs.
226 // TODO(b/34772739): might want to add other framework HAL packages
227 if (fqName.inPackage("android.hidl")) {
228 continue;
229 }
230 std::string interfaceName =
231 entry.source == LIST_DLLIB ? "" : fqName.name();
232 std::string instanceName =
233 entry.source == LIST_DLLIB ? "" : splittedFqInstanceName.second;
234
235 vintf::Transport transport;
236 if (entry.transport == "hwbinder") {
237 transport = vintf::Transport::HWBINDER;
238 } else if (entry.transport == "passthrough") {
239 transport = vintf::Transport::PASSTHROUGH;
240 } else {
241 mErr << "Warning: '" << entry.transport << "' is not a valid transport." << std::endl;
242 continue;
243 }
244
245 vintf::ManifestHal *hal = manifest.getHal(fqName.package());
246 if (hal == nullptr) {
247 if (!manifest.add(vintf::ManifestHal{
248 .format = vintf::HalFormat::HIDL,
249 .name = fqName.package(),
250 .impl = {.implLevel = vintf::ImplLevel::GENERIC, .impl = ""},
251 .transport = transport
252 })) {
253 mErr << "Warning: cannot add hal '" << fqInstanceName << "'" << std::endl;
254 continue;
255 }
256 hal = manifest.getHal(fqName.package());
257 }
258 if (hal == nullptr) {
259 mErr << "Warning: cannot get hal '" << fqInstanceName
260 << "' after adding it" << std::endl;
261 continue;
262 }
263 vintf::Version version{fqName.getPackageMajorVersion(), fqName.getPackageMinorVersion()};
264 if (std::find(hal->versions.begin(), hal->versions.end(), version) == hal->versions.end()) {
265 hal->versions.push_back(version);
266 }
267 if (entry.source != LIST_DLLIB) {
268 auto it = hal->interfaces.find(interfaceName);
269 if (it == hal->interfaces.end()) {
270 hal->interfaces.insert({interfaceName, {interfaceName, {{instanceName}}}});
271 } else {
272 it->second.instances.insert(instanceName);
273 }
274 }
275 }
276 mOut << vintf::gHalManifestConverter(manifest);
277}
278
Yifan Hongb4479022017-03-02 16:54:11 -0800279static const std::string &getArchString(Architecture arch) {
280 static const std::string sStr64 = "64";
281 static const std::string sStr32 = "32";
282 static const std::string sStrBoth = "64&32";
283 static const std::string sStrUnknown = "";
284 switch (arch) {
285 case ARCH64:
286 return sStr64;
287 case ARCH32:
288 return sStr32;
289 case ARCH_BOTH:
290 return sStrBoth;
291 case ARCH_UNKNOWN: // fall through
292 default:
293 return sStrUnknown;
294 }
295}
296
297static Architecture fromBaseArchitecture(::android::hidl::base::V1_0::DebugInfo::Architecture a) {
298 switch (a) {
299 case ::android::hidl::base::V1_0::DebugInfo::Architecture::IS_64BIT:
300 return ARCH64;
301 case ::android::hidl::base::V1_0::DebugInfo::Architecture::IS_32BIT:
302 return ARCH32;
303 case ::android::hidl::base::V1_0::DebugInfo::Architecture::UNKNOWN: // fallthrough
304 default:
305 return ARCH_UNKNOWN;
306 }
307}
308
Yifan Hong4b865492017-02-28 19:38:24 -0800309void Lshal::dumpTable() const {
Yifan Hongb0dde932017-02-10 17:49:58 -0800310 mOut << "All services:" << std::endl;
Yifan Hong38d53e02017-02-13 17:51:59 -0800311 mOut << std::left;
Yifan Hongb4479022017-03-02 16:54:11 -0800312 printLine("Interface", "Transport", "Arch", "Server", "Server CMD", "PTR", "Clients", "Clients CMD");
Yifan Hongb0dde932017-02-10 17:49:58 -0800313 for (const auto &entry : mTable) {
Yifan Hong38d53e02017-02-13 17:51:59 -0800314 printLine(entry.interfaceName,
Yifan Hongb0dde932017-02-10 17:49:58 -0800315 entry.transport,
Yifan Hongb4479022017-03-02 16:54:11 -0800316 getArchString(entry.arch),
Yifan Hongb0dde932017-02-10 17:49:58 -0800317 entry.serverPid == NO_PID ? "N/A" : std::to_string(entry.serverPid),
Yifan Hongae09a3d2017-02-14 17:33:50 -0800318 entry.serverCmdline,
Yifan Hongb0dde932017-02-10 17:49:58 -0800319 entry.serverObjectAddress == NO_PTR ? "N/A" : toHexString(entry.serverObjectAddress),
Yifan Hongae09a3d2017-02-14 17:33:50 -0800320 join(entry.clientPids, " "),
321 join(entry.clientCmdlines, ";"));
Yifan Hongb0dde932017-02-10 17:49:58 -0800322 }
323}
324
Yifan Hong4b865492017-02-28 19:38:24 -0800325void Lshal::dump() {
326 if (mVintf) {
327 dumpVintf();
328 if (!!mFileOutput) {
329 mFileOutput.buf().close();
330 delete &mFileOutput.buf();
331 mFileOutput = nullptr;
332 }
333 mOut = std::cout;
334 } else {
335 dumpTable();
336 }
337}
338
Yifan Hongb0dde932017-02-10 17:49:58 -0800339void Lshal::putEntry(TableEntry &&entry) {
340 mTable.push_back(std::forward<TableEntry>(entry));
341}
342
343Status Lshal::fetchAllLibraries(const sp<IServiceManager> &manager) {
344 using namespace ::android::hardware;
345 using namespace ::android::hidl::manager::V1_0;
346 using namespace ::android::hidl::base::V1_0;
Yifan Hongb4479022017-03-02 16:54:11 -0800347 auto ret = timeoutIPC(manager, &IServiceManager::debugDump, [&] (const auto &infos) {
348 std::map<std::string, TableEntry> entries;
349 for (const auto &info : infos) {
350 std::string interfaceName = std::string{info.interfaceName.c_str()} + "/" +
351 std::string{info.instanceName.c_str()};
352 entries.emplace(std::string{interfaceName}, TableEntry{
353 .interfaceName = interfaceName,
Yifan Hongb0dde932017-02-10 17:49:58 -0800354 .transport = "passthrough",
355 .serverPid = NO_PID,
356 .serverObjectAddress = NO_PTR,
Yifan Hong4b865492017-02-28 19:38:24 -0800357 .clientPids = {},
Yifan Hongb4479022017-03-02 16:54:11 -0800358 .arch = ARCH_UNKNOWN,
Yifan Hong4b865492017-02-28 19:38:24 -0800359 .source = LIST_DLLIB
Yifan Hongb4479022017-03-02 16:54:11 -0800360 }).first->second.arch |= fromBaseArchitecture(info.arch);
361 }
362 for (auto &&pair : entries) {
363 putEntry(std::move(pair.second));
Yifan Hongb0dde932017-02-10 17:49:58 -0800364 }
365 });
366 if (!ret.isOk()) {
367 mErr << "Error: Failed to call list on getPassthroughServiceManager(): "
368 << ret.description() << std::endl;
369 return DUMP_ALL_LIBS_ERROR;
370 }
371 return OK;
372}
373
374Status Lshal::fetchPassthrough(const sp<IServiceManager> &manager) {
375 using namespace ::android::hardware;
Yifan Hongb4479022017-03-02 16:54:11 -0800376 using namespace ::android::hardware::details;
Yifan Hongb0dde932017-02-10 17:49:58 -0800377 using namespace ::android::hidl::manager::V1_0;
378 using namespace ::android::hidl::base::V1_0;
Yifan Honge2dadf02017-02-14 15:43:31 -0800379 auto ret = timeoutIPC(manager, &IServiceManager::debugDump, [&] (const auto &infos) {
Yifan Hongb0dde932017-02-10 17:49:58 -0800380 for (const auto &info : infos) {
381 putEntry({
382 .interfaceName =
383 std::string{info.interfaceName.c_str()} + "/" +
384 std::string{info.instanceName.c_str()},
385 .transport = "passthrough",
386 .serverPid = info.clientPids.size() == 1 ? info.clientPids[0] : NO_PID,
387 .serverObjectAddress = NO_PTR,
Yifan Hong4b865492017-02-28 19:38:24 -0800388 .clientPids = info.clientPids,
Yifan Hongb4479022017-03-02 16:54:11 -0800389 .arch = fromBaseArchitecture(info.arch),
Yifan Hong4b865492017-02-28 19:38:24 -0800390 .source = PTSERVICEMANAGER_REG_CLIENT
Yifan Hongb0dde932017-02-10 17:49:58 -0800391 });
392 }
393 });
394 if (!ret.isOk()) {
395 mErr << "Error: Failed to call debugDump on defaultServiceManager(): "
396 << ret.description() << std::endl;
397 return DUMP_PASSTHROUGH_ERROR;
398 }
399 return OK;
400}
401
402Status Lshal::fetchBinderized(const sp<IServiceManager> &manager) {
403 using namespace ::std;
404 using namespace ::android::hardware;
405 using namespace ::android::hidl::manager::V1_0;
406 using namespace ::android::hidl::base::V1_0;
407 const std::string mode = "hwbinder";
Yifan Hongb0dde932017-02-10 17:49:58 -0800408
Steven Moreland9b5c15d2017-02-28 17:52:58 -0800409 hidl_vec<hidl_string> fqInstanceNames;
410 // copying out for timeoutIPC
411 auto listRet = timeoutIPC(manager, &IServiceManager::list, [&] (const auto &names) {
412 fqInstanceNames = names;
Yifan Hongb0dde932017-02-10 17:49:58 -0800413 });
414 if (!listRet.isOk()) {
415 mErr << "Error: Failed to list services for " << mode << ": "
416 << listRet.description() << std::endl;
Steven Moreland9b5c15d2017-02-28 17:52:58 -0800417 return DUMP_BINDERIZED_ERROR;
418 }
419
420 Status status = OK;
421 // server pid, .ptr value of binder object, child pids
422 std::map<std::string, DebugInfo> allDebugInfos;
423 std::map<pid_t, std::map<uint64_t, Pids>> allPids;
424 for (const auto &fqInstanceName : fqInstanceNames) {
Yifan Hong4b865492017-02-28 19:38:24 -0800425 const auto pair = splitFirst(fqInstanceName, '/');
Steven Moreland9b5c15d2017-02-28 17:52:58 -0800426 const auto &serviceName = pair.first;
427 const auto &instanceName = pair.second;
428 auto getRet = timeoutIPC(manager, &IServiceManager::get, serviceName, instanceName);
429 if (!getRet.isOk()) {
430 mErr << "Warning: Skipping \"" << fqInstanceName << "\": "
431 << "cannot be fetched from service manager:"
432 << getRet.description() << std::endl;
433 status |= DUMP_BINDERIZED_ERROR;
434 continue;
435 }
436 sp<IBase> service = getRet;
437 if (service == nullptr) {
438 mErr << "Warning: Skipping \"" << fqInstanceName << "\": "
439 << "cannot be fetched from service manager (null)";
440 status |= DUMP_BINDERIZED_ERROR;
441 continue;
442 }
443 auto debugRet = timeoutIPC(service, &IBase::getDebugInfo, [&] (const auto &debugInfo) {
444 allDebugInfos[fqInstanceName] = debugInfo;
445 if (debugInfo.pid >= 0) {
446 allPids[static_cast<pid_t>(debugInfo.pid)].clear();
447 }
448 });
449 if (!debugRet.isOk()) {
450 mErr << "Warning: Skipping \"" << fqInstanceName << "\": "
451 << "debugging information cannot be retrieved:"
452 << debugRet.description() << std::endl;
453 status |= DUMP_BINDERIZED_ERROR;
454 }
455 }
456 for (auto &pair : allPids) {
457 pid_t serverPid = pair.first;
458 if (!getReferencedPids(serverPid, &allPids[serverPid])) {
459 mErr << "Warning: no information for PID " << serverPid
460 << ", are you root?" << std::endl;
461 status |= DUMP_BINDERIZED_ERROR;
462 }
463 }
464 for (const auto &fqInstanceName : fqInstanceNames) {
465 auto it = allDebugInfos.find(fqInstanceName);
466 if (it == allDebugInfos.end()) {
467 putEntry({
468 .interfaceName = fqInstanceName,
469 .transport = mode,
470 .serverPid = NO_PID,
471 .serverObjectAddress = NO_PTR,
Yifan Hong4b865492017-02-28 19:38:24 -0800472 .clientPids = {},
Yifan Hongb4479022017-03-02 16:54:11 -0800473 .arch = ARCH_UNKNOWN,
Yifan Hong4b865492017-02-28 19:38:24 -0800474 .source = HWSERVICEMANAGER_LIST
Steven Moreland9b5c15d2017-02-28 17:52:58 -0800475 });
476 continue;
477 }
478 const DebugInfo &info = it->second;
479 putEntry({
480 .interfaceName = fqInstanceName,
481 .transport = mode,
482 .serverPid = info.pid,
483 .serverObjectAddress = info.ptr,
484 .clientPids = info.pid == NO_PID || info.ptr == NO_PTR
Yifan Hong4b865492017-02-28 19:38:24 -0800485 ? Pids{} : allPids[info.pid][info.ptr],
Yifan Hongb4479022017-03-02 16:54:11 -0800486 .arch = fromBaseArchitecture(info.arch),
Yifan Hong4b865492017-02-28 19:38:24 -0800487 .source = HWSERVICEMANAGER_LIST
Steven Moreland9b5c15d2017-02-28 17:52:58 -0800488 });
Yifan Hongb0dde932017-02-10 17:49:58 -0800489 }
490 return status;
491}
492
493Status Lshal::fetch() {
494 Status status = OK;
495 auto bManager = ::android::hardware::defaultServiceManager();
496 if (bManager == nullptr) {
497 mErr << "Failed to get defaultServiceManager()!" << std::endl;
498 status |= NO_BINDERIZED_MANAGER;
499 } else {
500 status |= fetchBinderized(bManager);
501 // Passthrough PIDs are registered to the binderized manager as well.
502 status |= fetchPassthrough(bManager);
503 }
504
505 auto pManager = ::android::hardware::getPassthroughServiceManager();
506 if (pManager == nullptr) {
507 mErr << "Failed to get getPassthroughServiceManager()!" << std::endl;
508 status |= NO_PASSTHROUGH_MANAGER;
509 } else {
510 status |= fetchAllLibraries(pManager);
511 }
512 return status;
513}
514
515void Lshal::usage() const {
516 mErr
517 << "usage: lshal" << std::endl
Yifan Hong38d53e02017-02-13 17:51:59 -0800518 << " Dump all hals with default ordering and columns [-itpc]." << std::endl
Yifan Hongb4479022017-03-02 16:54:11 -0800519 << " lshal [--interface|-i] [--transport|-t] [-r|--arch]" << std::endl
Yifan Hong38d53e02017-02-13 17:51:59 -0800520 << " [--pid|-p] [--address|-a] [--clients|-c] [--cmdline|-m]" << std::endl
Yifan Hong4b865492017-02-28 19:38:24 -0800521 << " [--sort={interface|i|pid|p}] [--init-vintf[=path]]" << std::endl
Yifan Hong38d53e02017-02-13 17:51:59 -0800522 << " -i, --interface: print the interface name column" << std::endl
523 << " -n, --instance: print the instance name column" << std::endl
524 << " -t, --transport: print the transport mode column" << std::endl
Yifan Hongb4479022017-03-02 16:54:11 -0800525 << " -r, --arch: print if the HAL is in 64-bit or 32-bit" << std::endl
Yifan Hongae09a3d2017-02-14 17:33:50 -0800526 << " -p, --pid: print the server PID, or server cmdline if -m is set" << std::endl
Yifan Hong38d53e02017-02-13 17:51:59 -0800527 << " -a, --address: print the server object address column" << std::endl
Yifan Hongae09a3d2017-02-14 17:33:50 -0800528 << " -c, --clients: print the client PIDs, or client cmdlines if -m is set"
529 << std::endl
530 << " -m, --cmdline: print cmdline instead of PIDs" << std::endl
Yifan Hong38d53e02017-02-13 17:51:59 -0800531 << " --sort=i, --sort=interface: sort by interface name" << std::endl
532 << " --sort=p, --sort=pid: sort by server pid" << std::endl
Yifan Hong4b865492017-02-28 19:38:24 -0800533 << " --init-vintf=path: form a skeleton HAL manifest to specified file " << std::endl
534 << " (stdout if no file specified)" << std::endl
Yifan Hongb0dde932017-02-10 17:49:58 -0800535 << " lshal [-h|--help]" << std::endl
536 << " -h, --help: show this help information." << std::endl;
537}
538
539Status Lshal::parseArgs(int argc, char **argv) {
540 static struct option longOptions[] = {
Yifan Hong38d53e02017-02-13 17:51:59 -0800541 // long options with short alternatives
542 {"help", no_argument, 0, 'h' },
543 {"interface", no_argument, 0, 'i' },
544 {"transport", no_argument, 0, 't' },
Yifan Hongb4479022017-03-02 16:54:11 -0800545 {"arch", no_argument, 0, 'r' },
Yifan Hong38d53e02017-02-13 17:51:59 -0800546 {"pid", no_argument, 0, 'p' },
547 {"address", no_argument, 0, 'a' },
548 {"clients", no_argument, 0, 'c' },
Yifan Hongae09a3d2017-02-14 17:33:50 -0800549 {"cmdline", no_argument, 0, 'm' },
Yifan Hong38d53e02017-02-13 17:51:59 -0800550
551 // long options without short alternatives
552 {"sort", required_argument, 0, 's' },
Yifan Hong4b865492017-02-28 19:38:24 -0800553 {"init-vintf",optional_argument, 0, 'v' },
Yifan Hong38d53e02017-02-13 17:51:59 -0800554 { 0, 0, 0, 0 }
Yifan Hongb0dde932017-02-10 17:49:58 -0800555 };
556
557 int optionIndex;
558 int c;
559 optind = 1;
560 for (;;) {
561 // using getopt_long in case we want to add other options in the future
Yifan Hongb4479022017-03-02 16:54:11 -0800562 c = getopt_long(argc, argv, "hitrpacm", longOptions, &optionIndex);
Yifan Hongb0dde932017-02-10 17:49:58 -0800563 if (c == -1) {
564 break;
565 }
566 switch (c) {
Yifan Hong38d53e02017-02-13 17:51:59 -0800567 case 's': {
568 if (strcmp(optarg, "interface") == 0 || strcmp(optarg, "i") == 0) {
569 mSortColumn = TableEntry::sortByInterfaceName;
570 } else if (strcmp(optarg, "pid") == 0 || strcmp(optarg, "p") == 0) {
571 mSortColumn = TableEntry::sortByServerPid;
572 } else {
573 mErr << "Unrecognized sorting column: " << optarg << std::endl;
574 usage();
575 return USAGE;
576 }
577 break;
578 }
Yifan Hong4b865492017-02-28 19:38:24 -0800579 case 'v': {
580 if (optarg) {
581 mFileOutput = new std::ofstream{optarg};
582 mOut = mFileOutput;
583 if (!mFileOutput.buf().is_open()) {
584 mErr << "Could not open file '" << optarg << "'." << std::endl;
585 return IO_ERROR;
586 }
587 }
588 mVintf = true;
589 }
Yifan Hong38d53e02017-02-13 17:51:59 -0800590 case 'i': {
591 mSelectedColumns |= ENABLE_INTERFACE_NAME;
592 break;
593 }
594 case 't': {
595 mSelectedColumns |= ENABLE_TRANSPORT;
596 break;
597 }
Yifan Hongb4479022017-03-02 16:54:11 -0800598 case 'r': {
599 mSelectedColumns |= ENABLE_ARCH;
600 break;
601 }
Yifan Hong38d53e02017-02-13 17:51:59 -0800602 case 'p': {
603 mSelectedColumns |= ENABLE_SERVER_PID;
604 break;
605 }
606 case 'a': {
607 mSelectedColumns |= ENABLE_SERVER_ADDR;
608 break;
609 }
610 case 'c': {
611 mSelectedColumns |= ENABLE_CLIENT_PIDS;
612 break;
613 }
Yifan Hongae09a3d2017-02-14 17:33:50 -0800614 case 'm': {
615 mEnableCmdlines = true;
616 break;
617 }
Yifan Hongb0dde932017-02-10 17:49:58 -0800618 case 'h': // falls through
619 default: // see unrecognized options
620 usage();
621 return USAGE;
622 }
623 }
Yifan Hong38d53e02017-02-13 17:51:59 -0800624
625 if (mSelectedColumns == 0) {
626 mSelectedColumns = ENABLE_INTERFACE_NAME
627 | ENABLE_TRANSPORT | ENABLE_SERVER_PID | ENABLE_CLIENT_PIDS;
628 }
Yifan Hongb0dde932017-02-10 17:49:58 -0800629 return OK;
630}
631
632int Lshal::main(int argc, char **argv) {
633 Status status = parseArgs(argc, argv);
634 if (status != OK) {
635 return status;
636 }
637 status = fetch();
Yifan Hong38d53e02017-02-13 17:51:59 -0800638 postprocess();
Yifan Hongb0dde932017-02-10 17:49:58 -0800639 dump();
640 return status;
641}
642
Yifan Honga57dffb2017-02-21 14:59:00 -0800643void signalHandler(int sig) {
644 if (sig == SIGINT) {
645 int retVal;
646 pthread_exit(&retVal);
647 }
648}
649
Yifan Hongb0dde932017-02-10 17:49:58 -0800650} // namespace lshal
651} // namespace android
652
653int main(int argc, char **argv) {
Yifan Honga57dffb2017-02-21 14:59:00 -0800654 signal(SIGINT, ::android::lshal::signalHandler);
Yifan Hongb0dde932017-02-10 17:49:58 -0800655 return ::android::lshal::Lshal{}.main(argc, argv);
656}