blob: 7646febd5194194c585a34ce33bd611da8308d82 [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 Honga3b87092017-03-02 19:19:29 -0800162void Lshal::forEachTable(const std::function<void(Table &)> &f) {
163 for (const Table &table : {mServicesTable, mPassthroughRefTable, mImplementationsTable}) {
164 f(const_cast<Table &>(table));
165 }
166}
167void Lshal::forEachTable(const std::function<void(const Table &)> &f) const {
168 for (const Table &table : {mServicesTable, mPassthroughRefTable, mImplementationsTable}) {
169 f(table);
170 }
171}
172
Yifan Hong38d53e02017-02-13 17:51:59 -0800173void Lshal::postprocess() {
Yifan Honga3b87092017-03-02 19:19:29 -0800174 forEachTable([this](Table &table) {
175 if (mSortColumn) {
176 std::sort(table.begin(), table.end(), mSortColumn);
Yifan Hongae09a3d2017-02-14 17:33:50 -0800177 }
Yifan Honga3b87092017-03-02 19:19:29 -0800178 for (TableEntry &entry : table) {
179 entry.serverCmdline = getCmdline(entry.serverPid);
180 removeDeadProcesses(&entry.clientPids);
181 for (auto pid : entry.clientPids) {
182 entry.clientCmdlines.push_back(this->getCmdline(pid));
183 }
184 }
185 });
Yifan Hong38d53e02017-02-13 17:51:59 -0800186}
187
188void Lshal::printLine(
189 const std::string &interfaceName,
Yifan Hongb4479022017-03-02 16:54:11 -0800190 const std::string &transport,
191 const std::string &arch,
192 const std::string &server,
Yifan Hongae09a3d2017-02-14 17:33:50 -0800193 const std::string &serverCmdline,
194 const std::string &address, const std::string &clients,
195 const std::string &clientCmdlines) const {
Yifan Hong38d53e02017-02-13 17:51:59 -0800196 if (mSelectedColumns & ENABLE_INTERFACE_NAME)
197 mOut << std::setw(80) << interfaceName << "\t";
198 if (mSelectedColumns & ENABLE_TRANSPORT)
199 mOut << std::setw(10) << transport << "\t";
Yifan Hongb4479022017-03-02 16:54:11 -0800200 if (mSelectedColumns & ENABLE_ARCH)
201 mOut << std::setw(5) << arch << "\t";
Yifan Hongae09a3d2017-02-14 17:33:50 -0800202 if (mSelectedColumns & ENABLE_SERVER_PID) {
203 if (mEnableCmdlines) {
204 mOut << std::setw(15) << serverCmdline << "\t";
205 } else {
206 mOut << std::setw(5) << server << "\t";
207 }
208 }
Yifan Hong38d53e02017-02-13 17:51:59 -0800209 if (mSelectedColumns & ENABLE_SERVER_ADDR)
210 mOut << std::setw(16) << address << "\t";
Yifan Hongae09a3d2017-02-14 17:33:50 -0800211 if (mSelectedColumns & ENABLE_CLIENT_PIDS) {
212 if (mEnableCmdlines) {
213 mOut << std::setw(0) << clientCmdlines;
214 } else {
215 mOut << std::setw(0) << clients;
216 }
217 }
Yifan Hong38d53e02017-02-13 17:51:59 -0800218 mOut << std::endl;
219}
220
Yifan Hong4b865492017-02-28 19:38:24 -0800221void Lshal::dumpVintf() const {
222 vintf::HalManifest manifest;
Yifan Honga3b87092017-03-02 19:19:29 -0800223 forEachTable([this, &manifest] (const Table &table) {
224 for (const TableEntry &entry : table) {
Yifan Hong4b865492017-02-28 19:38:24 -0800225
Yifan Honga3b87092017-03-02 19:19:29 -0800226 std::string fqInstanceName = entry.interfaceName;
Yifan Hong4b865492017-02-28 19:38:24 -0800227
Yifan Honga3b87092017-03-02 19:19:29 -0800228 if (&table == &mImplementationsTable) {
229 // Quick hack to work around *'s
230 replaceAll(&fqInstanceName, '*', 'D');
231 }
232 auto splittedFqInstanceName = splitFirst(fqInstanceName, '/');
233 FQName fqName(splittedFqInstanceName.first);
234 if (!fqName.isValid()) {
235 mErr << "Warning: '" << splittedFqInstanceName.first
236 << "' is not a valid FQName." << std::endl;
Yifan Hong4b865492017-02-28 19:38:24 -0800237 continue;
238 }
Yifan Honga3b87092017-03-02 19:19:29 -0800239 // Strip out system libs.
240 // TODO(b/34772739): might want to add other framework HAL packages
241 if (fqName.inPackage("android.hidl")) {
242 continue;
243 }
244 std::string interfaceName =
245 &table == &mImplementationsTable ? "" : fqName.name();
246 std::string instanceName =
247 &table == &mImplementationsTable ? "" : splittedFqInstanceName.second;
248
249 vintf::Transport transport;
250 if (entry.transport == "hwbinder") {
251 transport = vintf::Transport::HWBINDER;
252 } else if (entry.transport == "passthrough") {
253 transport = vintf::Transport::PASSTHROUGH;
Yifan Hong4b865492017-02-28 19:38:24 -0800254 } else {
Yifan Honga3b87092017-03-02 19:19:29 -0800255 mErr << "Warning: '" << entry.transport << "' is not a valid transport." << std::endl;
256 continue;
257 }
258
259 vintf::ManifestHal *hal = manifest.getHal(fqName.package());
260 if (hal == nullptr) {
261 if (!manifest.add(vintf::ManifestHal{
262 .format = vintf::HalFormat::HIDL,
263 .name = fqName.package(),
264 .impl = {.implLevel = vintf::ImplLevel::GENERIC, .impl = ""},
265 .transport = transport
266 })) {
267 mErr << "Warning: cannot add hal '" << fqInstanceName << "'" << std::endl;
268 continue;
269 }
270 hal = manifest.getHal(fqName.package());
271 }
272 if (hal == nullptr) {
273 mErr << "Warning: cannot get hal '" << fqInstanceName
274 << "' after adding it" << std::endl;
275 continue;
276 }
277 vintf::Version version{fqName.getPackageMajorVersion(), fqName.getPackageMinorVersion()};
278 if (std::find(hal->versions.begin(), hal->versions.end(), version) == hal->versions.end()) {
279 hal->versions.push_back(version);
280 }
281 if (&table != &mImplementationsTable) {
282 auto it = hal->interfaces.find(interfaceName);
283 if (it == hal->interfaces.end()) {
284 hal->interfaces.insert({interfaceName, {interfaceName, {{instanceName}}}});
285 } else {
286 it->second.instances.insert(instanceName);
287 }
Yifan Hong4b865492017-02-28 19:38:24 -0800288 }
289 }
Yifan Honga3b87092017-03-02 19:19:29 -0800290 });
Yifan Hong4b865492017-02-28 19:38:24 -0800291 mOut << vintf::gHalManifestConverter(manifest);
292}
293
Yifan Hongb4479022017-03-02 16:54:11 -0800294static const std::string &getArchString(Architecture arch) {
295 static const std::string sStr64 = "64";
296 static const std::string sStr32 = "32";
297 static const std::string sStrBoth = "64&32";
298 static const std::string sStrUnknown = "";
299 switch (arch) {
300 case ARCH64:
301 return sStr64;
302 case ARCH32:
303 return sStr32;
304 case ARCH_BOTH:
305 return sStrBoth;
306 case ARCH_UNKNOWN: // fall through
307 default:
308 return sStrUnknown;
309 }
310}
311
312static Architecture fromBaseArchitecture(::android::hidl::base::V1_0::DebugInfo::Architecture a) {
313 switch (a) {
314 case ::android::hidl::base::V1_0::DebugInfo::Architecture::IS_64BIT:
315 return ARCH64;
316 case ::android::hidl::base::V1_0::DebugInfo::Architecture::IS_32BIT:
317 return ARCH32;
318 case ::android::hidl::base::V1_0::DebugInfo::Architecture::UNKNOWN: // fallthrough
319 default:
320 return ARCH_UNKNOWN;
321 }
322}
323
Yifan Honga3b87092017-03-02 19:19:29 -0800324void Lshal::dumpTable() {
325 mServicesTable.description =
326 "All binderized services (registered services through hwservicemanager)";
327 mPassthroughRefTable.description =
328 "All interfaces that getService() has ever return a passthrough interface;\n"
329 "PIDs / processes shown below might be inaccurate because the process\n"
330 "might have relinquish the interface or might have died.\n"
331 "The Server / Server CMD column can be ignored.\n"
332 "The Clients / Clients CMD column shows all process that have ever dlopen the library\n"
333 "and successfully fetch the passthrough implementation.";
334 mImplementationsTable.description =
335 "All available passthrough implementations (all -impl.so files)";
336 forEachTable([this] (const Table &table) {
337 mOut << table.description << std::endl;
338 mOut << std::left;
339 printLine("Interface", "Transport", "Arch", "Server", "Server CMD",
340 "PTR", "Clients", "Clients CMD");
341 for (const auto &entry : table) {
342 printLine(entry.interfaceName,
343 entry.transport,
344 getArchString(entry.arch),
345 entry.serverPid == NO_PID ? "N/A" : std::to_string(entry.serverPid),
346 entry.serverCmdline,
347 entry.serverObjectAddress == NO_PTR ? "N/A" : toHexString(entry.serverObjectAddress),
348 join(entry.clientPids, " "),
349 join(entry.clientCmdlines, ";"));
350 }
351 mOut << std::endl;
352 });
353
Yifan Hongb0dde932017-02-10 17:49:58 -0800354}
355
Yifan Hong4b865492017-02-28 19:38:24 -0800356void Lshal::dump() {
357 if (mVintf) {
358 dumpVintf();
359 if (!!mFileOutput) {
360 mFileOutput.buf().close();
361 delete &mFileOutput.buf();
362 mFileOutput = nullptr;
363 }
364 mOut = std::cout;
365 } else {
366 dumpTable();
367 }
368}
369
Yifan Honga3b87092017-03-02 19:19:29 -0800370void Lshal::putEntry(TableEntrySource source, TableEntry &&entry) {
371 Table *table = nullptr;
372 switch (source) {
373 case HWSERVICEMANAGER_LIST :
374 table = &mServicesTable; break;
375 case PTSERVICEMANAGER_REG_CLIENT :
376 table = &mPassthroughRefTable; break;
377 case LIST_DLLIB :
378 table = &mImplementationsTable; break;
379 default:
380 mErr << "Error: Unknown source of entry " << source << std::endl;
381 }
382 if (table) {
383 table->entries.push_back(std::forward<TableEntry>(entry));
384 }
Yifan Hongb0dde932017-02-10 17:49:58 -0800385}
386
387Status Lshal::fetchAllLibraries(const sp<IServiceManager> &manager) {
388 using namespace ::android::hardware;
389 using namespace ::android::hidl::manager::V1_0;
390 using namespace ::android::hidl::base::V1_0;
Yifan Hongb4479022017-03-02 16:54:11 -0800391 auto ret = timeoutIPC(manager, &IServiceManager::debugDump, [&] (const auto &infos) {
392 std::map<std::string, TableEntry> entries;
393 for (const auto &info : infos) {
394 std::string interfaceName = std::string{info.interfaceName.c_str()} + "/" +
395 std::string{info.instanceName.c_str()};
396 entries.emplace(std::string{interfaceName}, TableEntry{
397 .interfaceName = interfaceName,
Yifan Hongb0dde932017-02-10 17:49:58 -0800398 .transport = "passthrough",
399 .serverPid = NO_PID,
400 .serverObjectAddress = NO_PTR,
Yifan Hong4b865492017-02-28 19:38:24 -0800401 .clientPids = {},
Yifan Honga3b87092017-03-02 19:19:29 -0800402 .arch = ARCH_UNKNOWN
Yifan Hongb4479022017-03-02 16:54:11 -0800403 }).first->second.arch |= fromBaseArchitecture(info.arch);
404 }
405 for (auto &&pair : entries) {
Yifan Honga3b87092017-03-02 19:19:29 -0800406 putEntry(LIST_DLLIB, std::move(pair.second));
Yifan Hongb0dde932017-02-10 17:49:58 -0800407 }
408 });
409 if (!ret.isOk()) {
410 mErr << "Error: Failed to call list on getPassthroughServiceManager(): "
411 << ret.description() << std::endl;
412 return DUMP_ALL_LIBS_ERROR;
413 }
414 return OK;
415}
416
417Status Lshal::fetchPassthrough(const sp<IServiceManager> &manager) {
418 using namespace ::android::hardware;
Yifan Hongb4479022017-03-02 16:54:11 -0800419 using namespace ::android::hardware::details;
Yifan Hongb0dde932017-02-10 17:49:58 -0800420 using namespace ::android::hidl::manager::V1_0;
421 using namespace ::android::hidl::base::V1_0;
Yifan Honge2dadf02017-02-14 15:43:31 -0800422 auto ret = timeoutIPC(manager, &IServiceManager::debugDump, [&] (const auto &infos) {
Yifan Hongb0dde932017-02-10 17:49:58 -0800423 for (const auto &info : infos) {
Yifan Honga3b87092017-03-02 19:19:29 -0800424 putEntry(PTSERVICEMANAGER_REG_CLIENT, {
Yifan Hongb0dde932017-02-10 17:49:58 -0800425 .interfaceName =
426 std::string{info.interfaceName.c_str()} + "/" +
427 std::string{info.instanceName.c_str()},
428 .transport = "passthrough",
429 .serverPid = info.clientPids.size() == 1 ? info.clientPids[0] : NO_PID,
430 .serverObjectAddress = NO_PTR,
Yifan Hong4b865492017-02-28 19:38:24 -0800431 .clientPids = info.clientPids,
Yifan Honga3b87092017-03-02 19:19:29 -0800432 .arch = fromBaseArchitecture(info.arch)
Yifan Hongb0dde932017-02-10 17:49:58 -0800433 });
434 }
435 });
436 if (!ret.isOk()) {
437 mErr << "Error: Failed to call debugDump on defaultServiceManager(): "
438 << ret.description() << std::endl;
439 return DUMP_PASSTHROUGH_ERROR;
440 }
441 return OK;
442}
443
444Status Lshal::fetchBinderized(const sp<IServiceManager> &manager) {
445 using namespace ::std;
446 using namespace ::android::hardware;
447 using namespace ::android::hidl::manager::V1_0;
448 using namespace ::android::hidl::base::V1_0;
449 const std::string mode = "hwbinder";
Yifan Hongb0dde932017-02-10 17:49:58 -0800450
Steven Moreland9b5c15d2017-02-28 17:52:58 -0800451 hidl_vec<hidl_string> fqInstanceNames;
452 // copying out for timeoutIPC
453 auto listRet = timeoutIPC(manager, &IServiceManager::list, [&] (const auto &names) {
454 fqInstanceNames = names;
Yifan Hongb0dde932017-02-10 17:49:58 -0800455 });
456 if (!listRet.isOk()) {
457 mErr << "Error: Failed to list services for " << mode << ": "
458 << listRet.description() << std::endl;
Steven Moreland9b5c15d2017-02-28 17:52:58 -0800459 return DUMP_BINDERIZED_ERROR;
460 }
461
462 Status status = OK;
463 // server pid, .ptr value of binder object, child pids
464 std::map<std::string, DebugInfo> allDebugInfos;
465 std::map<pid_t, std::map<uint64_t, Pids>> allPids;
466 for (const auto &fqInstanceName : fqInstanceNames) {
Yifan Hong4b865492017-02-28 19:38:24 -0800467 const auto pair = splitFirst(fqInstanceName, '/');
Steven Moreland9b5c15d2017-02-28 17:52:58 -0800468 const auto &serviceName = pair.first;
469 const auto &instanceName = pair.second;
470 auto getRet = timeoutIPC(manager, &IServiceManager::get, serviceName, instanceName);
471 if (!getRet.isOk()) {
472 mErr << "Warning: Skipping \"" << fqInstanceName << "\": "
473 << "cannot be fetched from service manager:"
474 << getRet.description() << std::endl;
475 status |= DUMP_BINDERIZED_ERROR;
476 continue;
477 }
478 sp<IBase> service = getRet;
479 if (service == nullptr) {
480 mErr << "Warning: Skipping \"" << fqInstanceName << "\": "
481 << "cannot be fetched from service manager (null)";
482 status |= DUMP_BINDERIZED_ERROR;
483 continue;
484 }
485 auto debugRet = timeoutIPC(service, &IBase::getDebugInfo, [&] (const auto &debugInfo) {
486 allDebugInfos[fqInstanceName] = debugInfo;
487 if (debugInfo.pid >= 0) {
488 allPids[static_cast<pid_t>(debugInfo.pid)].clear();
489 }
490 });
491 if (!debugRet.isOk()) {
492 mErr << "Warning: Skipping \"" << fqInstanceName << "\": "
493 << "debugging information cannot be retrieved:"
494 << debugRet.description() << std::endl;
495 status |= DUMP_BINDERIZED_ERROR;
496 }
497 }
498 for (auto &pair : allPids) {
499 pid_t serverPid = pair.first;
500 if (!getReferencedPids(serverPid, &allPids[serverPid])) {
501 mErr << "Warning: no information for PID " << serverPid
502 << ", are you root?" << std::endl;
503 status |= DUMP_BINDERIZED_ERROR;
504 }
505 }
506 for (const auto &fqInstanceName : fqInstanceNames) {
507 auto it = allDebugInfos.find(fqInstanceName);
508 if (it == allDebugInfos.end()) {
Yifan Honga3b87092017-03-02 19:19:29 -0800509 putEntry(HWSERVICEMANAGER_LIST, {
Steven Moreland9b5c15d2017-02-28 17:52:58 -0800510 .interfaceName = fqInstanceName,
511 .transport = mode,
512 .serverPid = NO_PID,
513 .serverObjectAddress = NO_PTR,
Yifan Hong4b865492017-02-28 19:38:24 -0800514 .clientPids = {},
Yifan Honga3b87092017-03-02 19:19:29 -0800515 .arch = ARCH_UNKNOWN
Steven Moreland9b5c15d2017-02-28 17:52:58 -0800516 });
517 continue;
518 }
519 const DebugInfo &info = it->second;
Yifan Honga3b87092017-03-02 19:19:29 -0800520 putEntry(HWSERVICEMANAGER_LIST, {
Steven Moreland9b5c15d2017-02-28 17:52:58 -0800521 .interfaceName = fqInstanceName,
522 .transport = mode,
523 .serverPid = info.pid,
524 .serverObjectAddress = info.ptr,
525 .clientPids = info.pid == NO_PID || info.ptr == NO_PTR
Yifan Hong4b865492017-02-28 19:38:24 -0800526 ? Pids{} : allPids[info.pid][info.ptr],
Yifan Hongb4479022017-03-02 16:54:11 -0800527 .arch = fromBaseArchitecture(info.arch),
Steven Moreland9b5c15d2017-02-28 17:52:58 -0800528 });
Yifan Hongb0dde932017-02-10 17:49:58 -0800529 }
530 return status;
531}
532
533Status Lshal::fetch() {
534 Status status = OK;
535 auto bManager = ::android::hardware::defaultServiceManager();
536 if (bManager == nullptr) {
537 mErr << "Failed to get defaultServiceManager()!" << std::endl;
538 status |= NO_BINDERIZED_MANAGER;
539 } else {
540 status |= fetchBinderized(bManager);
541 // Passthrough PIDs are registered to the binderized manager as well.
542 status |= fetchPassthrough(bManager);
543 }
544
545 auto pManager = ::android::hardware::getPassthroughServiceManager();
546 if (pManager == nullptr) {
547 mErr << "Failed to get getPassthroughServiceManager()!" << std::endl;
548 status |= NO_PASSTHROUGH_MANAGER;
549 } else {
550 status |= fetchAllLibraries(pManager);
551 }
552 return status;
553}
554
555void Lshal::usage() const {
556 mErr
557 << "usage: lshal" << std::endl
Yifan Honga3b87092017-03-02 19:19:29 -0800558 << " Dump all hals with default ordering and columns [-ipc]." << std::endl
Yifan Hongb4479022017-03-02 16:54:11 -0800559 << " lshal [--interface|-i] [--transport|-t] [-r|--arch]" << std::endl
Yifan Hong38d53e02017-02-13 17:51:59 -0800560 << " [--pid|-p] [--address|-a] [--clients|-c] [--cmdline|-m]" << std::endl
Yifan Hong4b865492017-02-28 19:38:24 -0800561 << " [--sort={interface|i|pid|p}] [--init-vintf[=path]]" << std::endl
Yifan Hong38d53e02017-02-13 17:51:59 -0800562 << " -i, --interface: print the interface name column" << std::endl
563 << " -n, --instance: print the instance name column" << std::endl
564 << " -t, --transport: print the transport mode column" << std::endl
Yifan Hongb4479022017-03-02 16:54:11 -0800565 << " -r, --arch: print if the HAL is in 64-bit or 32-bit" << std::endl
Yifan Hongae09a3d2017-02-14 17:33:50 -0800566 << " -p, --pid: print the server PID, or server cmdline if -m is set" << std::endl
Yifan Hong38d53e02017-02-13 17:51:59 -0800567 << " -a, --address: print the server object address column" << std::endl
Yifan Hongae09a3d2017-02-14 17:33:50 -0800568 << " -c, --clients: print the client PIDs, or client cmdlines if -m is set"
569 << std::endl
570 << " -m, --cmdline: print cmdline instead of PIDs" << std::endl
Yifan Hong38d53e02017-02-13 17:51:59 -0800571 << " --sort=i, --sort=interface: sort by interface name" << std::endl
572 << " --sort=p, --sort=pid: sort by server pid" << std::endl
Yifan Hong4b865492017-02-28 19:38:24 -0800573 << " --init-vintf=path: form a skeleton HAL manifest to specified file " << std::endl
574 << " (stdout if no file specified)" << std::endl
Yifan Hongb0dde932017-02-10 17:49:58 -0800575 << " lshal [-h|--help]" << std::endl
576 << " -h, --help: show this help information." << std::endl;
577}
578
579Status Lshal::parseArgs(int argc, char **argv) {
580 static struct option longOptions[] = {
Yifan Hong38d53e02017-02-13 17:51:59 -0800581 // long options with short alternatives
582 {"help", no_argument, 0, 'h' },
583 {"interface", no_argument, 0, 'i' },
584 {"transport", no_argument, 0, 't' },
Yifan Hongb4479022017-03-02 16:54:11 -0800585 {"arch", no_argument, 0, 'r' },
Yifan Hong38d53e02017-02-13 17:51:59 -0800586 {"pid", no_argument, 0, 'p' },
587 {"address", no_argument, 0, 'a' },
588 {"clients", no_argument, 0, 'c' },
Yifan Hongae09a3d2017-02-14 17:33:50 -0800589 {"cmdline", no_argument, 0, 'm' },
Yifan Hong38d53e02017-02-13 17:51:59 -0800590
591 // long options without short alternatives
592 {"sort", required_argument, 0, 's' },
Yifan Hong4b865492017-02-28 19:38:24 -0800593 {"init-vintf",optional_argument, 0, 'v' },
Yifan Hong38d53e02017-02-13 17:51:59 -0800594 { 0, 0, 0, 0 }
Yifan Hongb0dde932017-02-10 17:49:58 -0800595 };
596
597 int optionIndex;
598 int c;
599 optind = 1;
600 for (;;) {
601 // using getopt_long in case we want to add other options in the future
Yifan Hongb4479022017-03-02 16:54:11 -0800602 c = getopt_long(argc, argv, "hitrpacm", longOptions, &optionIndex);
Yifan Hongb0dde932017-02-10 17:49:58 -0800603 if (c == -1) {
604 break;
605 }
606 switch (c) {
Yifan Hong38d53e02017-02-13 17:51:59 -0800607 case 's': {
608 if (strcmp(optarg, "interface") == 0 || strcmp(optarg, "i") == 0) {
609 mSortColumn = TableEntry::sortByInterfaceName;
610 } else if (strcmp(optarg, "pid") == 0 || strcmp(optarg, "p") == 0) {
611 mSortColumn = TableEntry::sortByServerPid;
612 } else {
613 mErr << "Unrecognized sorting column: " << optarg << std::endl;
614 usage();
615 return USAGE;
616 }
617 break;
618 }
Yifan Hong4b865492017-02-28 19:38:24 -0800619 case 'v': {
620 if (optarg) {
621 mFileOutput = new std::ofstream{optarg};
622 mOut = mFileOutput;
623 if (!mFileOutput.buf().is_open()) {
624 mErr << "Could not open file '" << optarg << "'." << std::endl;
625 return IO_ERROR;
626 }
627 }
628 mVintf = true;
629 }
Yifan Hong38d53e02017-02-13 17:51:59 -0800630 case 'i': {
631 mSelectedColumns |= ENABLE_INTERFACE_NAME;
632 break;
633 }
634 case 't': {
635 mSelectedColumns |= ENABLE_TRANSPORT;
636 break;
637 }
Yifan Hongb4479022017-03-02 16:54:11 -0800638 case 'r': {
639 mSelectedColumns |= ENABLE_ARCH;
640 break;
641 }
Yifan Hong38d53e02017-02-13 17:51:59 -0800642 case 'p': {
643 mSelectedColumns |= ENABLE_SERVER_PID;
644 break;
645 }
646 case 'a': {
647 mSelectedColumns |= ENABLE_SERVER_ADDR;
648 break;
649 }
650 case 'c': {
651 mSelectedColumns |= ENABLE_CLIENT_PIDS;
652 break;
653 }
Yifan Hongae09a3d2017-02-14 17:33:50 -0800654 case 'm': {
655 mEnableCmdlines = true;
656 break;
657 }
Yifan Hongb0dde932017-02-10 17:49:58 -0800658 case 'h': // falls through
659 default: // see unrecognized options
660 usage();
661 return USAGE;
662 }
663 }
Yifan Hong38d53e02017-02-13 17:51:59 -0800664
665 if (mSelectedColumns == 0) {
Yifan Honga3b87092017-03-02 19:19:29 -0800666 mSelectedColumns = ENABLE_INTERFACE_NAME | ENABLE_SERVER_PID | ENABLE_CLIENT_PIDS;
Yifan Hong38d53e02017-02-13 17:51:59 -0800667 }
Yifan Hongb0dde932017-02-10 17:49:58 -0800668 return OK;
669}
670
671int Lshal::main(int argc, char **argv) {
672 Status status = parseArgs(argc, argv);
673 if (status != OK) {
674 return status;
675 }
676 status = fetch();
Yifan Hong38d53e02017-02-13 17:51:59 -0800677 postprocess();
Yifan Hongb0dde932017-02-10 17:49:58 -0800678 dump();
679 return status;
680}
681
Yifan Honga57dffb2017-02-21 14:59:00 -0800682void signalHandler(int sig) {
683 if (sig == SIGINT) {
684 int retVal;
685 pthread_exit(&retVal);
686 }
687}
688
Yifan Hongb0dde932017-02-10 17:49:58 -0800689} // namespace lshal
690} // namespace android
691
692int main(int argc, char **argv) {
Yifan Honga57dffb2017-02-21 14:59:00 -0800693 signal(SIGINT, ::android::lshal::signalHandler);
Yifan Hongb0dde932017-02-10 17:49:58 -0800694 return ::android::lshal::Lshal{}.main(argc, argv);
695}