blob: 0228a37e8465c428dfc10cb36fb1d8e006633c74 [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) {
Yifan Hong8ab3bee2017-03-08 14:01:11 -0800163 f(mServicesTable);
164 f(mPassthroughRefTable);
165 f(mImplementationsTable);
Yifan Honga3b87092017-03-02 19:19:29 -0800166}
167void Lshal::forEachTable(const std::function<void(const Table &)> &f) const {
Yifan Hong8ab3bee2017-03-08 14:01:11 -0800168 f(mServicesTable);
169 f(mPassthroughRefTable);
170 f(mImplementationsTable);
Yifan Honga3b87092017-03-02 19:19:29 -0800171}
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 Hong3fdbd9f2017-03-08 14:01:58 -0800186 // use a double for loop here because lshal doesn't care about efficiency.
187 for (TableEntry &packageEntry : mImplementationsTable) {
188 std::string packageName = packageEntry.interfaceName;
189 FQName fqPackageName{packageName.substr(0, packageName.find("::"))};
190 if (!fqPackageName.isValid()) {
191 continue;
192 }
193 for (TableEntry &interfaceEntry : mPassthroughRefTable) {
194 if (interfaceEntry.arch != ARCH_UNKNOWN) {
195 continue;
196 }
197 FQName interfaceName{splitFirst(interfaceEntry.interfaceName, '/').first};
198 if (!interfaceName.isValid()) {
199 continue;
200 }
201 if (interfaceName.getPackageAndVersion() == fqPackageName) {
202 interfaceEntry.arch = packageEntry.arch;
203 }
204 }
205 }
Yifan Hong38d53e02017-02-13 17:51:59 -0800206}
207
208void Lshal::printLine(
209 const std::string &interfaceName,
Yifan Hongb4479022017-03-02 16:54:11 -0800210 const std::string &transport,
211 const std::string &arch,
212 const std::string &server,
Yifan Hongae09a3d2017-02-14 17:33:50 -0800213 const std::string &serverCmdline,
214 const std::string &address, const std::string &clients,
215 const std::string &clientCmdlines) const {
Yifan Hong38d53e02017-02-13 17:51:59 -0800216 if (mSelectedColumns & ENABLE_INTERFACE_NAME)
217 mOut << std::setw(80) << interfaceName << "\t";
218 if (mSelectedColumns & ENABLE_TRANSPORT)
219 mOut << std::setw(10) << transport << "\t";
Yifan Hongb4479022017-03-02 16:54:11 -0800220 if (mSelectedColumns & ENABLE_ARCH)
221 mOut << std::setw(5) << arch << "\t";
Yifan Hongae09a3d2017-02-14 17:33:50 -0800222 if (mSelectedColumns & ENABLE_SERVER_PID) {
223 if (mEnableCmdlines) {
224 mOut << std::setw(15) << serverCmdline << "\t";
225 } else {
226 mOut << std::setw(5) << server << "\t";
227 }
228 }
Yifan Hong38d53e02017-02-13 17:51:59 -0800229 if (mSelectedColumns & ENABLE_SERVER_ADDR)
230 mOut << std::setw(16) << address << "\t";
Yifan Hongae09a3d2017-02-14 17:33:50 -0800231 if (mSelectedColumns & ENABLE_CLIENT_PIDS) {
232 if (mEnableCmdlines) {
233 mOut << std::setw(0) << clientCmdlines;
234 } else {
235 mOut << std::setw(0) << clients;
236 }
237 }
Yifan Hong38d53e02017-02-13 17:51:59 -0800238 mOut << std::endl;
239}
240
Yifan Hong4b865492017-02-28 19:38:24 -0800241void Lshal::dumpVintf() const {
242 vintf::HalManifest manifest;
Yifan Honga3b87092017-03-02 19:19:29 -0800243 forEachTable([this, &manifest] (const Table &table) {
244 for (const TableEntry &entry : table) {
Yifan Hong4b865492017-02-28 19:38:24 -0800245
Yifan Honga3b87092017-03-02 19:19:29 -0800246 std::string fqInstanceName = entry.interfaceName;
Yifan Hong4b865492017-02-28 19:38:24 -0800247
Yifan Honga3b87092017-03-02 19:19:29 -0800248 if (&table == &mImplementationsTable) {
249 // Quick hack to work around *'s
250 replaceAll(&fqInstanceName, '*', 'D');
251 }
252 auto splittedFqInstanceName = splitFirst(fqInstanceName, '/');
253 FQName fqName(splittedFqInstanceName.first);
254 if (!fqName.isValid()) {
255 mErr << "Warning: '" << splittedFqInstanceName.first
256 << "' is not a valid FQName." << std::endl;
Yifan Hong4b865492017-02-28 19:38:24 -0800257 continue;
258 }
Yifan Honga3b87092017-03-02 19:19:29 -0800259 // Strip out system libs.
Steven Moreland9b6cd602017-03-22 14:22:55 -0700260 if (fqName.inPackage("android.hidl") ||
261 fqName.inPackage("android.frameworks") ||
262 fqName.inPackage("android.system")) {
Yifan Honga3b87092017-03-02 19:19:29 -0800263 continue;
264 }
265 std::string interfaceName =
266 &table == &mImplementationsTable ? "" : fqName.name();
267 std::string instanceName =
268 &table == &mImplementationsTable ? "" : splittedFqInstanceName.second;
269
270 vintf::Transport transport;
Yifan Hong3fdbd9f2017-03-08 14:01:58 -0800271 vintf::Arch arch;
Yifan Honga3b87092017-03-02 19:19:29 -0800272 if (entry.transport == "hwbinder") {
273 transport = vintf::Transport::HWBINDER;
Yifan Hong3fdbd9f2017-03-08 14:01:58 -0800274 arch = vintf::Arch::ARCH_EMPTY;
Yifan Honga3b87092017-03-02 19:19:29 -0800275 } else if (entry.transport == "passthrough") {
276 transport = vintf::Transport::PASSTHROUGH;
Yifan Hong3fdbd9f2017-03-08 14:01:58 -0800277 switch (entry.arch) {
278 case lshal::ARCH32:
279 arch = vintf::Arch::ARCH_32; break;
280 case lshal::ARCH64:
281 arch = vintf::Arch::ARCH_64; break;
282 case lshal::ARCH_BOTH:
283 arch = vintf::Arch::ARCH_32_64; break;
284 case lshal::ARCH_UNKNOWN: // fallthrough
285 default:
286 mErr << "Warning: '" << fqName.package()
287 << "' doesn't have bitness info, assuming 32+64." << std::endl;
288 arch = vintf::Arch::ARCH_32_64;
289 }
Yifan Hong4b865492017-02-28 19:38:24 -0800290 } else {
Yifan Honga3b87092017-03-02 19:19:29 -0800291 mErr << "Warning: '" << entry.transport << "' is not a valid transport." << std::endl;
292 continue;
293 }
294
295 vintf::ManifestHal *hal = manifest.getHal(fqName.package());
296 if (hal == nullptr) {
297 if (!manifest.add(vintf::ManifestHal{
298 .format = vintf::HalFormat::HIDL,
299 .name = fqName.package(),
300 .impl = {.implLevel = vintf::ImplLevel::GENERIC, .impl = ""},
Yifan Hong3fdbd9f2017-03-08 14:01:58 -0800301 .transportArch = {transport, arch}
Yifan Honga3b87092017-03-02 19:19:29 -0800302 })) {
303 mErr << "Warning: cannot add hal '" << fqInstanceName << "'" << std::endl;
304 continue;
305 }
306 hal = manifest.getHal(fqName.package());
307 }
308 if (hal == nullptr) {
309 mErr << "Warning: cannot get hal '" << fqInstanceName
310 << "' after adding it" << std::endl;
311 continue;
312 }
313 vintf::Version version{fqName.getPackageMajorVersion(), fqName.getPackageMinorVersion()};
314 if (std::find(hal->versions.begin(), hal->versions.end(), version) == hal->versions.end()) {
315 hal->versions.push_back(version);
316 }
317 if (&table != &mImplementationsTable) {
318 auto it = hal->interfaces.find(interfaceName);
319 if (it == hal->interfaces.end()) {
320 hal->interfaces.insert({interfaceName, {interfaceName, {{instanceName}}}});
321 } else {
322 it->second.instances.insert(instanceName);
323 }
Yifan Hong4b865492017-02-28 19:38:24 -0800324 }
325 }
Yifan Honga3b87092017-03-02 19:19:29 -0800326 });
Yifan Hong4b865492017-02-28 19:38:24 -0800327 mOut << vintf::gHalManifestConverter(manifest);
328}
329
Yifan Hongb4479022017-03-02 16:54:11 -0800330static const std::string &getArchString(Architecture arch) {
331 static const std::string sStr64 = "64";
332 static const std::string sStr32 = "32";
Yifan Hong1071c1b2017-03-03 10:57:43 -0800333 static const std::string sStrBoth = "32+64";
Yifan Hongb4479022017-03-02 16:54:11 -0800334 static const std::string sStrUnknown = "";
335 switch (arch) {
336 case ARCH64:
337 return sStr64;
338 case ARCH32:
339 return sStr32;
340 case ARCH_BOTH:
341 return sStrBoth;
342 case ARCH_UNKNOWN: // fall through
343 default:
344 return sStrUnknown;
345 }
346}
347
348static Architecture fromBaseArchitecture(::android::hidl::base::V1_0::DebugInfo::Architecture a) {
349 switch (a) {
350 case ::android::hidl::base::V1_0::DebugInfo::Architecture::IS_64BIT:
351 return ARCH64;
352 case ::android::hidl::base::V1_0::DebugInfo::Architecture::IS_32BIT:
353 return ARCH32;
354 case ::android::hidl::base::V1_0::DebugInfo::Architecture::UNKNOWN: // fallthrough
355 default:
356 return ARCH_UNKNOWN;
357 }
358}
359
Yifan Honga3b87092017-03-02 19:19:29 -0800360void Lshal::dumpTable() {
361 mServicesTable.description =
362 "All binderized services (registered services through hwservicemanager)";
363 mPassthroughRefTable.description =
Yifan Hong1071c1b2017-03-03 10:57:43 -0800364 "All interfaces that getService() has ever return as a passthrough interface;\n"
Yifan Honga3b87092017-03-02 19:19:29 -0800365 "PIDs / processes shown below might be inaccurate because the process\n"
Yifan Hong1071c1b2017-03-03 10:57:43 -0800366 "might have relinquished the interface or might have died.\n"
Yifan Honga3b87092017-03-02 19:19:29 -0800367 "The Server / Server CMD column can be ignored.\n"
Yifan Hong1071c1b2017-03-03 10:57:43 -0800368 "The Clients / Clients CMD column shows all process that have ever dlopen'ed \n"
369 "the library and successfully fetched the passthrough implementation.";
Yifan Honga3b87092017-03-02 19:19:29 -0800370 mImplementationsTable.description =
371 "All available passthrough implementations (all -impl.so files)";
372 forEachTable([this] (const Table &table) {
373 mOut << table.description << std::endl;
374 mOut << std::left;
375 printLine("Interface", "Transport", "Arch", "Server", "Server CMD",
376 "PTR", "Clients", "Clients CMD");
377 for (const auto &entry : table) {
378 printLine(entry.interfaceName,
379 entry.transport,
380 getArchString(entry.arch),
381 entry.serverPid == NO_PID ? "N/A" : std::to_string(entry.serverPid),
382 entry.serverCmdline,
383 entry.serverObjectAddress == NO_PTR ? "N/A" : toHexString(entry.serverObjectAddress),
384 join(entry.clientPids, " "),
385 join(entry.clientCmdlines, ";"));
386 }
387 mOut << std::endl;
388 });
389
Yifan Hongb0dde932017-02-10 17:49:58 -0800390}
391
Yifan Hong4b865492017-02-28 19:38:24 -0800392void Lshal::dump() {
393 if (mVintf) {
394 dumpVintf();
395 if (!!mFileOutput) {
396 mFileOutput.buf().close();
397 delete &mFileOutput.buf();
398 mFileOutput = nullptr;
399 }
400 mOut = std::cout;
401 } else {
402 dumpTable();
403 }
404}
405
Yifan Honga3b87092017-03-02 19:19:29 -0800406void Lshal::putEntry(TableEntrySource source, TableEntry &&entry) {
407 Table *table = nullptr;
408 switch (source) {
409 case HWSERVICEMANAGER_LIST :
410 table = &mServicesTable; break;
411 case PTSERVICEMANAGER_REG_CLIENT :
412 table = &mPassthroughRefTable; break;
413 case LIST_DLLIB :
414 table = &mImplementationsTable; break;
415 default:
416 mErr << "Error: Unknown source of entry " << source << std::endl;
417 }
418 if (table) {
419 table->entries.push_back(std::forward<TableEntry>(entry));
420 }
Yifan Hongb0dde932017-02-10 17:49:58 -0800421}
422
423Status Lshal::fetchAllLibraries(const sp<IServiceManager> &manager) {
424 using namespace ::android::hardware;
425 using namespace ::android::hidl::manager::V1_0;
426 using namespace ::android::hidl::base::V1_0;
Yifan Hongb4479022017-03-02 16:54:11 -0800427 auto ret = timeoutIPC(manager, &IServiceManager::debugDump, [&] (const auto &infos) {
428 std::map<std::string, TableEntry> entries;
429 for (const auto &info : infos) {
430 std::string interfaceName = std::string{info.interfaceName.c_str()} + "/" +
431 std::string{info.instanceName.c_str()};
Yifan Hong1071c1b2017-03-03 10:57:43 -0800432 entries.emplace(interfaceName, TableEntry{
Yifan Hongb4479022017-03-02 16:54:11 -0800433 .interfaceName = interfaceName,
Yifan Hongb0dde932017-02-10 17:49:58 -0800434 .transport = "passthrough",
435 .serverPid = NO_PID,
436 .serverObjectAddress = NO_PTR,
Yifan Hong4b865492017-02-28 19:38:24 -0800437 .clientPids = {},
Yifan Honga3b87092017-03-02 19:19:29 -0800438 .arch = ARCH_UNKNOWN
Yifan Hongb4479022017-03-02 16:54:11 -0800439 }).first->second.arch |= fromBaseArchitecture(info.arch);
440 }
441 for (auto &&pair : entries) {
Yifan Honga3b87092017-03-02 19:19:29 -0800442 putEntry(LIST_DLLIB, std::move(pair.second));
Yifan Hongb0dde932017-02-10 17:49:58 -0800443 }
444 });
445 if (!ret.isOk()) {
446 mErr << "Error: Failed to call list on getPassthroughServiceManager(): "
447 << ret.description() << std::endl;
448 return DUMP_ALL_LIBS_ERROR;
449 }
450 return OK;
451}
452
453Status Lshal::fetchPassthrough(const sp<IServiceManager> &manager) {
454 using namespace ::android::hardware;
Yifan Hongb4479022017-03-02 16:54:11 -0800455 using namespace ::android::hardware::details;
Yifan Hongb0dde932017-02-10 17:49:58 -0800456 using namespace ::android::hidl::manager::V1_0;
457 using namespace ::android::hidl::base::V1_0;
Yifan Honge2dadf02017-02-14 15:43:31 -0800458 auto ret = timeoutIPC(manager, &IServiceManager::debugDump, [&] (const auto &infos) {
Yifan Hongb0dde932017-02-10 17:49:58 -0800459 for (const auto &info : infos) {
Yifan Honga3b87092017-03-02 19:19:29 -0800460 putEntry(PTSERVICEMANAGER_REG_CLIENT, {
Yifan Hongb0dde932017-02-10 17:49:58 -0800461 .interfaceName =
462 std::string{info.interfaceName.c_str()} + "/" +
463 std::string{info.instanceName.c_str()},
464 .transport = "passthrough",
465 .serverPid = info.clientPids.size() == 1 ? info.clientPids[0] : NO_PID,
466 .serverObjectAddress = NO_PTR,
Yifan Hong4b865492017-02-28 19:38:24 -0800467 .clientPids = info.clientPids,
Yifan Honga3b87092017-03-02 19:19:29 -0800468 .arch = fromBaseArchitecture(info.arch)
Yifan Hongb0dde932017-02-10 17:49:58 -0800469 });
470 }
471 });
472 if (!ret.isOk()) {
473 mErr << "Error: Failed to call debugDump on defaultServiceManager(): "
474 << ret.description() << std::endl;
475 return DUMP_PASSTHROUGH_ERROR;
476 }
477 return OK;
478}
479
480Status Lshal::fetchBinderized(const sp<IServiceManager> &manager) {
481 using namespace ::std;
482 using namespace ::android::hardware;
483 using namespace ::android::hidl::manager::V1_0;
484 using namespace ::android::hidl::base::V1_0;
485 const std::string mode = "hwbinder";
Yifan Hongb0dde932017-02-10 17:49:58 -0800486
Steven Moreland9b5c15d2017-02-28 17:52:58 -0800487 hidl_vec<hidl_string> fqInstanceNames;
488 // copying out for timeoutIPC
489 auto listRet = timeoutIPC(manager, &IServiceManager::list, [&] (const auto &names) {
490 fqInstanceNames = names;
Yifan Hongb0dde932017-02-10 17:49:58 -0800491 });
492 if (!listRet.isOk()) {
493 mErr << "Error: Failed to list services for " << mode << ": "
494 << listRet.description() << std::endl;
Steven Moreland9b5c15d2017-02-28 17:52:58 -0800495 return DUMP_BINDERIZED_ERROR;
496 }
497
498 Status status = OK;
499 // server pid, .ptr value of binder object, child pids
500 std::map<std::string, DebugInfo> allDebugInfos;
501 std::map<pid_t, std::map<uint64_t, Pids>> allPids;
502 for (const auto &fqInstanceName : fqInstanceNames) {
Yifan Hong4b865492017-02-28 19:38:24 -0800503 const auto pair = splitFirst(fqInstanceName, '/');
Steven Moreland9b5c15d2017-02-28 17:52:58 -0800504 const auto &serviceName = pair.first;
505 const auto &instanceName = pair.second;
506 auto getRet = timeoutIPC(manager, &IServiceManager::get, serviceName, instanceName);
507 if (!getRet.isOk()) {
508 mErr << "Warning: Skipping \"" << fqInstanceName << "\": "
509 << "cannot be fetched from service manager:"
510 << getRet.description() << std::endl;
511 status |= DUMP_BINDERIZED_ERROR;
512 continue;
513 }
514 sp<IBase> service = getRet;
515 if (service == nullptr) {
516 mErr << "Warning: Skipping \"" << fqInstanceName << "\": "
517 << "cannot be fetched from service manager (null)";
518 status |= DUMP_BINDERIZED_ERROR;
519 continue;
520 }
521 auto debugRet = timeoutIPC(service, &IBase::getDebugInfo, [&] (const auto &debugInfo) {
522 allDebugInfos[fqInstanceName] = debugInfo;
523 if (debugInfo.pid >= 0) {
524 allPids[static_cast<pid_t>(debugInfo.pid)].clear();
525 }
526 });
527 if (!debugRet.isOk()) {
528 mErr << "Warning: Skipping \"" << fqInstanceName << "\": "
529 << "debugging information cannot be retrieved:"
530 << debugRet.description() << std::endl;
531 status |= DUMP_BINDERIZED_ERROR;
532 }
533 }
534 for (auto &pair : allPids) {
535 pid_t serverPid = pair.first;
536 if (!getReferencedPids(serverPid, &allPids[serverPid])) {
537 mErr << "Warning: no information for PID " << serverPid
538 << ", are you root?" << std::endl;
539 status |= DUMP_BINDERIZED_ERROR;
540 }
541 }
542 for (const auto &fqInstanceName : fqInstanceNames) {
543 auto it = allDebugInfos.find(fqInstanceName);
544 if (it == allDebugInfos.end()) {
Yifan Honga3b87092017-03-02 19:19:29 -0800545 putEntry(HWSERVICEMANAGER_LIST, {
Steven Moreland9b5c15d2017-02-28 17:52:58 -0800546 .interfaceName = fqInstanceName,
547 .transport = mode,
548 .serverPid = NO_PID,
549 .serverObjectAddress = NO_PTR,
Yifan Hong4b865492017-02-28 19:38:24 -0800550 .clientPids = {},
Yifan Honga3b87092017-03-02 19:19:29 -0800551 .arch = ARCH_UNKNOWN
Steven Moreland9b5c15d2017-02-28 17:52:58 -0800552 });
553 continue;
554 }
555 const DebugInfo &info = it->second;
Yifan Honga3b87092017-03-02 19:19:29 -0800556 putEntry(HWSERVICEMANAGER_LIST, {
Steven Moreland9b5c15d2017-02-28 17:52:58 -0800557 .interfaceName = fqInstanceName,
558 .transport = mode,
559 .serverPid = info.pid,
560 .serverObjectAddress = info.ptr,
561 .clientPids = info.pid == NO_PID || info.ptr == NO_PTR
Yifan Hong4b865492017-02-28 19:38:24 -0800562 ? Pids{} : allPids[info.pid][info.ptr],
Yifan Hongb4479022017-03-02 16:54:11 -0800563 .arch = fromBaseArchitecture(info.arch),
Steven Moreland9b5c15d2017-02-28 17:52:58 -0800564 });
Yifan Hongb0dde932017-02-10 17:49:58 -0800565 }
566 return status;
567}
568
569Status Lshal::fetch() {
570 Status status = OK;
571 auto bManager = ::android::hardware::defaultServiceManager();
572 if (bManager == nullptr) {
573 mErr << "Failed to get defaultServiceManager()!" << std::endl;
574 status |= NO_BINDERIZED_MANAGER;
575 } else {
576 status |= fetchBinderized(bManager);
577 // Passthrough PIDs are registered to the binderized manager as well.
578 status |= fetchPassthrough(bManager);
579 }
580
581 auto pManager = ::android::hardware::getPassthroughServiceManager();
582 if (pManager == nullptr) {
583 mErr << "Failed to get getPassthroughServiceManager()!" << std::endl;
584 status |= NO_PASSTHROUGH_MANAGER;
585 } else {
586 status |= fetchAllLibraries(pManager);
587 }
588 return status;
589}
590
591void Lshal::usage() const {
592 mErr
593 << "usage: lshal" << std::endl
Yifan Honga3b87092017-03-02 19:19:29 -0800594 << " Dump all hals with default ordering and columns [-ipc]." << std::endl
Yifan Hongb4479022017-03-02 16:54:11 -0800595 << " lshal [--interface|-i] [--transport|-t] [-r|--arch]" << std::endl
Yifan Hong38d53e02017-02-13 17:51:59 -0800596 << " [--pid|-p] [--address|-a] [--clients|-c] [--cmdline|-m]" << std::endl
Yifan Hong4b865492017-02-28 19:38:24 -0800597 << " [--sort={interface|i|pid|p}] [--init-vintf[=path]]" << std::endl
Yifan Hong38d53e02017-02-13 17:51:59 -0800598 << " -i, --interface: print the interface name column" << std::endl
599 << " -n, --instance: print the instance name column" << std::endl
600 << " -t, --transport: print the transport mode column" << std::endl
Yifan Hongb4479022017-03-02 16:54:11 -0800601 << " -r, --arch: print if the HAL is in 64-bit or 32-bit" << std::endl
Yifan Hongae09a3d2017-02-14 17:33:50 -0800602 << " -p, --pid: print the server PID, or server cmdline if -m is set" << std::endl
Yifan Hong38d53e02017-02-13 17:51:59 -0800603 << " -a, --address: print the server object address column" << std::endl
Yifan Hongae09a3d2017-02-14 17:33:50 -0800604 << " -c, --clients: print the client PIDs, or client cmdlines if -m is set"
605 << std::endl
606 << " -m, --cmdline: print cmdline instead of PIDs" << std::endl
Yifan Hong38d53e02017-02-13 17:51:59 -0800607 << " --sort=i, --sort=interface: sort by interface name" << std::endl
608 << " --sort=p, --sort=pid: sort by server pid" << std::endl
Yifan Hong4b865492017-02-28 19:38:24 -0800609 << " --init-vintf=path: form a skeleton HAL manifest to specified file " << std::endl
610 << " (stdout if no file specified)" << std::endl
Yifan Hongb0dde932017-02-10 17:49:58 -0800611 << " lshal [-h|--help]" << std::endl
612 << " -h, --help: show this help information." << std::endl;
613}
614
615Status Lshal::parseArgs(int argc, char **argv) {
616 static struct option longOptions[] = {
Yifan Hong38d53e02017-02-13 17:51:59 -0800617 // long options with short alternatives
618 {"help", no_argument, 0, 'h' },
619 {"interface", no_argument, 0, 'i' },
620 {"transport", no_argument, 0, 't' },
Yifan Hongb4479022017-03-02 16:54:11 -0800621 {"arch", no_argument, 0, 'r' },
Yifan Hong38d53e02017-02-13 17:51:59 -0800622 {"pid", no_argument, 0, 'p' },
623 {"address", no_argument, 0, 'a' },
624 {"clients", no_argument, 0, 'c' },
Yifan Hongae09a3d2017-02-14 17:33:50 -0800625 {"cmdline", no_argument, 0, 'm' },
Yifan Hong38d53e02017-02-13 17:51:59 -0800626
627 // long options without short alternatives
628 {"sort", required_argument, 0, 's' },
Yifan Hong4b865492017-02-28 19:38:24 -0800629 {"init-vintf",optional_argument, 0, 'v' },
Yifan Hong38d53e02017-02-13 17:51:59 -0800630 { 0, 0, 0, 0 }
Yifan Hongb0dde932017-02-10 17:49:58 -0800631 };
632
633 int optionIndex;
634 int c;
635 optind = 1;
636 for (;;) {
637 // using getopt_long in case we want to add other options in the future
Yifan Hongb4479022017-03-02 16:54:11 -0800638 c = getopt_long(argc, argv, "hitrpacm", longOptions, &optionIndex);
Yifan Hongb0dde932017-02-10 17:49:58 -0800639 if (c == -1) {
640 break;
641 }
642 switch (c) {
Yifan Hong38d53e02017-02-13 17:51:59 -0800643 case 's': {
644 if (strcmp(optarg, "interface") == 0 || strcmp(optarg, "i") == 0) {
645 mSortColumn = TableEntry::sortByInterfaceName;
646 } else if (strcmp(optarg, "pid") == 0 || strcmp(optarg, "p") == 0) {
647 mSortColumn = TableEntry::sortByServerPid;
648 } else {
649 mErr << "Unrecognized sorting column: " << optarg << std::endl;
650 usage();
651 return USAGE;
652 }
653 break;
654 }
Yifan Hong4b865492017-02-28 19:38:24 -0800655 case 'v': {
656 if (optarg) {
657 mFileOutput = new std::ofstream{optarg};
658 mOut = mFileOutput;
659 if (!mFileOutput.buf().is_open()) {
660 mErr << "Could not open file '" << optarg << "'." << std::endl;
661 return IO_ERROR;
662 }
663 }
664 mVintf = true;
665 }
Yifan Hong38d53e02017-02-13 17:51:59 -0800666 case 'i': {
667 mSelectedColumns |= ENABLE_INTERFACE_NAME;
668 break;
669 }
670 case 't': {
671 mSelectedColumns |= ENABLE_TRANSPORT;
672 break;
673 }
Yifan Hongb4479022017-03-02 16:54:11 -0800674 case 'r': {
675 mSelectedColumns |= ENABLE_ARCH;
676 break;
677 }
Yifan Hong38d53e02017-02-13 17:51:59 -0800678 case 'p': {
679 mSelectedColumns |= ENABLE_SERVER_PID;
680 break;
681 }
682 case 'a': {
683 mSelectedColumns |= ENABLE_SERVER_ADDR;
684 break;
685 }
686 case 'c': {
687 mSelectedColumns |= ENABLE_CLIENT_PIDS;
688 break;
689 }
Yifan Hongae09a3d2017-02-14 17:33:50 -0800690 case 'm': {
691 mEnableCmdlines = true;
692 break;
693 }
Yifan Hongb0dde932017-02-10 17:49:58 -0800694 case 'h': // falls through
695 default: // see unrecognized options
696 usage();
697 return USAGE;
698 }
699 }
Yifan Hong38d53e02017-02-13 17:51:59 -0800700
701 if (mSelectedColumns == 0) {
Yifan Honga3b87092017-03-02 19:19:29 -0800702 mSelectedColumns = ENABLE_INTERFACE_NAME | ENABLE_SERVER_PID | ENABLE_CLIENT_PIDS;
Yifan Hong38d53e02017-02-13 17:51:59 -0800703 }
Yifan Hongb0dde932017-02-10 17:49:58 -0800704 return OK;
705}
706
707int Lshal::main(int argc, char **argv) {
708 Status status = parseArgs(argc, argv);
709 if (status != OK) {
710 return status;
711 }
712 status = fetch();
Yifan Hong38d53e02017-02-13 17:51:59 -0800713 postprocess();
Yifan Hongb0dde932017-02-10 17:49:58 -0800714 dump();
715 return status;
716}
717
Yifan Honga57dffb2017-02-21 14:59:00 -0800718void signalHandler(int sig) {
719 if (sig == SIGINT) {
720 int retVal;
721 pthread_exit(&retVal);
722 }
723}
724
Yifan Hongb0dde932017-02-10 17:49:58 -0800725} // namespace lshal
726} // namespace android
727
728int main(int argc, char **argv) {
Yifan Honga57dffb2017-02-21 14:59:00 -0800729 signal(SIGINT, ::android::lshal::signalHandler);
Yifan Hongb0dde932017-02-10 17:49:58 -0800730 return ::android::lshal::Lshal{}.main(argc, argv);
731}