blob: d6caf5f6fff8f86448694e946b56a82de13c285e [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
Andreas Huber28d35912017-03-24 13:14:11 -070028#include <android-base/logging.h>
Yifan Hongb0dde932017-02-10 17:49:58 -080029#include <android-base/parseint.h>
30#include <android/hidl/manager/1.0/IServiceManager.h>
31#include <hidl/ServiceManagement.h>
Yifan Hong4b865492017-02-28 19:38:24 -080032#include <hidl-util/FQName.h>
Andreas Huber28d35912017-03-24 13:14:11 -070033#include <private/android_filesystem_config.h>
34#include <sys/stat.h>
Yifan Hong4b865492017-02-28 19:38:24 -080035#include <vintf/HalManifest.h>
36#include <vintf/parse_xml.h>
Yifan Hongb0dde932017-02-10 17:49:58 -080037
Andreas Huber28d35912017-03-24 13:14:11 -070038#include "PipeRelay.h"
Yifan Honge2dadf02017-02-14 15:43:31 -080039#include "Timeout.h"
40
Yifan Hongb0dde932017-02-10 17:49:58 -080041using ::android::hardware::hidl_string;
42using ::android::hidl::manager::V1_0::IServiceManager;
43
44namespace android {
45namespace lshal {
46
Yifan Hongb0dde932017-02-10 17:49:58 -080047template <typename A>
48std::string join(const A &components, const std::string &separator) {
49 std::stringstream out;
50 bool first = true;
51 for (const auto &component : components) {
52 if (!first) {
53 out << separator;
54 }
55 out << component;
56
57 first = false;
58 }
59 return out.str();
60}
61
62static std::string toHexString(uint64_t t) {
63 std::ostringstream os;
64 os << std::hex << std::setfill('0') << std::setw(16) << t;
65 return os.str();
66}
67
Yifan Hong4b865492017-02-28 19:38:24 -080068template<typename String>
69static std::pair<String, String> splitFirst(const String &s, char c) {
Yifan Hongb0dde932017-02-10 17:49:58 -080070 const char *pos = strchr(s.c_str(), c);
71 if (pos == nullptr) {
72 return {s, {}};
73 }
Yifan Hong4b865492017-02-28 19:38:24 -080074 return {String(s.c_str(), pos - s.c_str()), String(pos + 1)};
Yifan Hongb0dde932017-02-10 17:49:58 -080075}
76
77static std::vector<std::string> split(const std::string &s, char c) {
78 std::vector<std::string> components{};
79 size_t startPos = 0;
80 size_t matchPos;
81 while ((matchPos = s.find(c, startPos)) != std::string::npos) {
82 components.push_back(s.substr(startPos, matchPos - startPos));
83 startPos = matchPos + 1;
84 }
85
86 if (startPos <= s.length()) {
87 components.push_back(s.substr(startPos));
88 }
89 return components;
90}
91
Yifan Hong4b865492017-02-28 19:38:24 -080092static void replaceAll(std::string *s, char from, char to) {
93 for (size_t i = 0; i < s->size(); ++i) {
94 if (s->at(i) == from) {
95 s->at(i) = to;
96 }
97 }
98}
99
Yifan Hongae09a3d2017-02-14 17:33:50 -0800100std::string getCmdline(pid_t pid) {
101 std::ifstream ifs("/proc/" + std::to_string(pid) + "/cmdline");
102 std::string cmdline;
103 if (!ifs.is_open()) {
104 return "";
105 }
106 ifs >> cmdline;
107 return cmdline;
108}
109
110const std::string &Lshal::getCmdline(pid_t pid) {
111 auto pair = mCmdlines.find(pid);
112 if (pair != mCmdlines.end()) {
113 return pair->second;
114 }
115 mCmdlines[pid] = ::android::lshal::getCmdline(pid);
116 return mCmdlines[pid];
117}
118
119void Lshal::removeDeadProcesses(Pids *pids) {
120 static const pid_t myPid = getpid();
121 std::remove_if(pids->begin(), pids->end(), [this](auto pid) {
122 return pid == myPid || this->getCmdline(pid).empty();
123 });
124}
125
Yifan Hongb0dde932017-02-10 17:49:58 -0800126bool Lshal::getReferencedPids(
127 pid_t serverPid, std::map<uint64_t, Pids> *objects) const {
128
129 std::ifstream ifs("/d/binder/proc/" + std::to_string(serverPid));
130 if (!ifs.is_open()) {
131 return false;
132 }
133
134 static const std::regex prefix("^\\s*node \\d+:\\s+u([0-9a-f]+)\\s+c([0-9a-f]+)\\s+");
135
136 std::string line;
137 std::smatch match;
138 while(getline(ifs, line)) {
139 if (!std::regex_search(line, match, prefix)) {
140 // the line doesn't start with the correct prefix
141 continue;
142 }
143 std::string ptrString = "0x" + match.str(2); // use number after c
144 uint64_t ptr;
145 if (!::android::base::ParseUint(ptrString.c_str(), &ptr)) {
146 // Should not reach here, but just be tolerant.
147 mErr << "Could not parse number " << ptrString << std::endl;
148 continue;
149 }
150 const std::string proc = " proc ";
151 auto pos = line.rfind(proc);
152 if (pos != std::string::npos) {
153 for (const std::string &pidStr : split(line.substr(pos + proc.size()), ' ')) {
154 int32_t pid;
155 if (!::android::base::ParseInt(pidStr, &pid)) {
156 mErr << "Could not parse number " << pidStr << std::endl;
157 continue;
158 }
159 (*objects)[ptr].push_back(pid);
160 }
161 }
162 }
163 return true;
164}
165
Yifan Hong9ee841f2017-04-18 14:19:40 -0700166// Must process hwbinder services first, then passthrough services.
Yifan Honga3b87092017-03-02 19:19:29 -0800167void Lshal::forEachTable(const std::function<void(Table &)> &f) {
Yifan Hong8ab3bee2017-03-08 14:01:11 -0800168 f(mServicesTable);
169 f(mPassthroughRefTable);
170 f(mImplementationsTable);
Yifan Honga3b87092017-03-02 19:19:29 -0800171}
172void Lshal::forEachTable(const std::function<void(const Table &)> &f) const {
Yifan Hong8ab3bee2017-03-08 14:01:11 -0800173 f(mServicesTable);
174 f(mPassthroughRefTable);
175 f(mImplementationsTable);
Yifan Honga3b87092017-03-02 19:19:29 -0800176}
177
Yifan Hong38d53e02017-02-13 17:51:59 -0800178void Lshal::postprocess() {
Yifan Honga3b87092017-03-02 19:19:29 -0800179 forEachTable([this](Table &table) {
180 if (mSortColumn) {
181 std::sort(table.begin(), table.end(), mSortColumn);
Yifan Hongae09a3d2017-02-14 17:33:50 -0800182 }
Yifan Honga3b87092017-03-02 19:19:29 -0800183 for (TableEntry &entry : table) {
184 entry.serverCmdline = getCmdline(entry.serverPid);
185 removeDeadProcesses(&entry.clientPids);
186 for (auto pid : entry.clientPids) {
187 entry.clientCmdlines.push_back(this->getCmdline(pid));
188 }
189 }
190 });
Yifan Hong3fdbd9f2017-03-08 14:01:58 -0800191 // use a double for loop here because lshal doesn't care about efficiency.
192 for (TableEntry &packageEntry : mImplementationsTable) {
193 std::string packageName = packageEntry.interfaceName;
194 FQName fqPackageName{packageName.substr(0, packageName.find("::"))};
195 if (!fqPackageName.isValid()) {
196 continue;
197 }
198 for (TableEntry &interfaceEntry : mPassthroughRefTable) {
199 if (interfaceEntry.arch != ARCH_UNKNOWN) {
200 continue;
201 }
202 FQName interfaceName{splitFirst(interfaceEntry.interfaceName, '/').first};
203 if (!interfaceName.isValid()) {
204 continue;
205 }
206 if (interfaceName.getPackageAndVersion() == fqPackageName) {
207 interfaceEntry.arch = packageEntry.arch;
208 }
209 }
210 }
Yifan Hong38d53e02017-02-13 17:51:59 -0800211}
212
213void Lshal::printLine(
214 const std::string &interfaceName,
Yifan Hongb4479022017-03-02 16:54:11 -0800215 const std::string &transport,
216 const std::string &arch,
217 const std::string &server,
Yifan Hongae09a3d2017-02-14 17:33:50 -0800218 const std::string &serverCmdline,
219 const std::string &address, const std::string &clients,
220 const std::string &clientCmdlines) const {
Yifan Hong38d53e02017-02-13 17:51:59 -0800221 if (mSelectedColumns & ENABLE_INTERFACE_NAME)
222 mOut << std::setw(80) << interfaceName << "\t";
223 if (mSelectedColumns & ENABLE_TRANSPORT)
224 mOut << std::setw(10) << transport << "\t";
Yifan Hongb4479022017-03-02 16:54:11 -0800225 if (mSelectedColumns & ENABLE_ARCH)
226 mOut << std::setw(5) << arch << "\t";
Yifan Hongae09a3d2017-02-14 17:33:50 -0800227 if (mSelectedColumns & ENABLE_SERVER_PID) {
228 if (mEnableCmdlines) {
229 mOut << std::setw(15) << serverCmdline << "\t";
230 } else {
231 mOut << std::setw(5) << server << "\t";
232 }
233 }
Yifan Hong38d53e02017-02-13 17:51:59 -0800234 if (mSelectedColumns & ENABLE_SERVER_ADDR)
235 mOut << std::setw(16) << address << "\t";
Yifan Hongae09a3d2017-02-14 17:33:50 -0800236 if (mSelectedColumns & ENABLE_CLIENT_PIDS) {
237 if (mEnableCmdlines) {
238 mOut << std::setw(0) << clientCmdlines;
239 } else {
240 mOut << std::setw(0) << clients;
241 }
242 }
Yifan Hong38d53e02017-02-13 17:51:59 -0800243 mOut << std::endl;
244}
245
Yifan Hong4b865492017-02-28 19:38:24 -0800246void Lshal::dumpVintf() const {
Yifan Hong9ee841f2017-04-18 14:19:40 -0700247 mOut << "<!-- " << std::endl
248 << " This is a skeleton device manifest. Notes: " << std::endl
249 << " 1. android.hidl.*, android.frameworks.*, android.system.* are not included." << std::endl
250 << " 2. If a HAL is supported in both hwbinder and passthrough transport, " << std::endl
251 << " only hwbinder is shown." << std::endl
252 << " 3. It is likely that HALs in passthrough transport does not have" << std::endl
253 << " <interface> declared; users will have to write them by hand." << std::endl
254 << " 4. sepolicy version is set to 0.0. It is recommended that the entry" << std::endl
255 << " is removed from the manifest file and written by assemble_vintf" << std::endl
256 << " at build time." << std::endl
257 << "-->" << std::endl;
258
Yifan Hong4b865492017-02-28 19:38:24 -0800259 vintf::HalManifest manifest;
Yifan Honga3b87092017-03-02 19:19:29 -0800260 forEachTable([this, &manifest] (const Table &table) {
261 for (const TableEntry &entry : table) {
Yifan Hong4b865492017-02-28 19:38:24 -0800262
Yifan Honga3b87092017-03-02 19:19:29 -0800263 std::string fqInstanceName = entry.interfaceName;
Yifan Hong4b865492017-02-28 19:38:24 -0800264
Yifan Honga3b87092017-03-02 19:19:29 -0800265 if (&table == &mImplementationsTable) {
266 // Quick hack to work around *'s
267 replaceAll(&fqInstanceName, '*', 'D');
268 }
269 auto splittedFqInstanceName = splitFirst(fqInstanceName, '/');
270 FQName fqName(splittedFqInstanceName.first);
271 if (!fqName.isValid()) {
272 mErr << "Warning: '" << splittedFqInstanceName.first
273 << "' is not a valid FQName." << std::endl;
Yifan Hong4b865492017-02-28 19:38:24 -0800274 continue;
275 }
Yifan Honga3b87092017-03-02 19:19:29 -0800276 // Strip out system libs.
Steven Moreland9b6cd602017-03-22 14:22:55 -0700277 if (fqName.inPackage("android.hidl") ||
278 fqName.inPackage("android.frameworks") ||
279 fqName.inPackage("android.system")) {
Yifan Honga3b87092017-03-02 19:19:29 -0800280 continue;
281 }
282 std::string interfaceName =
283 &table == &mImplementationsTable ? "" : fqName.name();
284 std::string instanceName =
285 &table == &mImplementationsTable ? "" : splittedFqInstanceName.second;
286
Yifan Hong9ee841f2017-04-18 14:19:40 -0700287 vintf::Version version{fqName.getPackageMajorVersion(),
288 fqName.getPackageMinorVersion()};
Yifan Honga3b87092017-03-02 19:19:29 -0800289 vintf::Transport transport;
Yifan Hong3fdbd9f2017-03-08 14:01:58 -0800290 vintf::Arch arch;
Yifan Honga3b87092017-03-02 19:19:29 -0800291 if (entry.transport == "hwbinder") {
292 transport = vintf::Transport::HWBINDER;
Yifan Hong3fdbd9f2017-03-08 14:01:58 -0800293 arch = vintf::Arch::ARCH_EMPTY;
Yifan Honga3b87092017-03-02 19:19:29 -0800294 } else if (entry.transport == "passthrough") {
295 transport = vintf::Transport::PASSTHROUGH;
Yifan Hong3fdbd9f2017-03-08 14:01:58 -0800296 switch (entry.arch) {
297 case lshal::ARCH32:
298 arch = vintf::Arch::ARCH_32; break;
299 case lshal::ARCH64:
300 arch = vintf::Arch::ARCH_64; break;
301 case lshal::ARCH_BOTH:
302 arch = vintf::Arch::ARCH_32_64; break;
303 case lshal::ARCH_UNKNOWN: // fallthrough
304 default:
305 mErr << "Warning: '" << fqName.package()
306 << "' doesn't have bitness info, assuming 32+64." << std::endl;
307 arch = vintf::Arch::ARCH_32_64;
308 }
Yifan Hong4b865492017-02-28 19:38:24 -0800309 } else {
Yifan Honga3b87092017-03-02 19:19:29 -0800310 mErr << "Warning: '" << entry.transport << "' is not a valid transport." << std::endl;
311 continue;
312 }
313
Yifan Hong9ee841f2017-04-18 14:19:40 -0700314 bool done = false;
315 for (vintf::ManifestHal *hal : manifest.getHals(fqName.package())) {
316 if (hal->transport() != transport) {
317 if (transport != vintf::Transport::PASSTHROUGH) {
318 mErr << "Fatal: should not reach here. Generated result may be wrong."
319 << std::endl;
320 }
321 done = true;
322 break;
323 }
324 if (hal->hasVersion(version)) {
325 hal->interfaces[interfaceName].name = interfaceName;
326 hal->interfaces[interfaceName].instances.insert(instanceName);
327 done = true;
328 break;
329 }
330 }
331 if (done) {
332 continue; // to next TableEntry
333 }
334 if (!manifest.add(vintf::ManifestHal{
Yifan Honga3b87092017-03-02 19:19:29 -0800335 .format = vintf::HalFormat::HIDL,
336 .name = fqName.package(),
Yifan Hong9ee841f2017-04-18 14:19:40 -0700337 .versions = {version},
338 .transportArch = {transport, arch},
339 .interfaces = {{interfaceName, {interfaceName, {{instanceName}}}}}})) {
340 mErr << "Warning: cannot add hal '" << fqInstanceName << "'" << std::endl;
Yifan Hong4b865492017-02-28 19:38:24 -0800341 }
342 }
Yifan Honga3b87092017-03-02 19:19:29 -0800343 });
Yifan Hong4b865492017-02-28 19:38:24 -0800344 mOut << vintf::gHalManifestConverter(manifest);
345}
346
Yifan Hongb4479022017-03-02 16:54:11 -0800347static const std::string &getArchString(Architecture arch) {
348 static const std::string sStr64 = "64";
349 static const std::string sStr32 = "32";
Yifan Hong1071c1b2017-03-03 10:57:43 -0800350 static const std::string sStrBoth = "32+64";
Yifan Hongb4479022017-03-02 16:54:11 -0800351 static const std::string sStrUnknown = "";
352 switch (arch) {
353 case ARCH64:
354 return sStr64;
355 case ARCH32:
356 return sStr32;
357 case ARCH_BOTH:
358 return sStrBoth;
359 case ARCH_UNKNOWN: // fall through
360 default:
361 return sStrUnknown;
362 }
363}
364
365static Architecture fromBaseArchitecture(::android::hidl::base::V1_0::DebugInfo::Architecture a) {
366 switch (a) {
367 case ::android::hidl::base::V1_0::DebugInfo::Architecture::IS_64BIT:
368 return ARCH64;
369 case ::android::hidl::base::V1_0::DebugInfo::Architecture::IS_32BIT:
370 return ARCH32;
371 case ::android::hidl::base::V1_0::DebugInfo::Architecture::UNKNOWN: // fallthrough
372 default:
373 return ARCH_UNKNOWN;
374 }
375}
376
Andreas Huber28d35912017-03-24 13:14:11 -0700377// A unique_ptr type using a custom deleter function.
378template<typename T>
379using deleted_unique_ptr = std::unique_ptr<T, std::function<void(T *)> >;
380
381void Lshal::emitDebugInfo(
382 const sp<IServiceManager> &serviceManager,
383 const std::string &interfaceName,
384 const std::string &instanceName) const {
385 using android::hidl::base::V1_0::IBase;
386
387 hardware::Return<sp<IBase>> retBase =
388 serviceManager->get(interfaceName, instanceName);
389
390 sp<IBase> base;
391 if (!retBase.isOk() || (base = retBase) == nullptr) {
392 // There's a small race, where a service instantiated while collecting
393 // the list of services has by now terminated, so this isn't anything
394 // to be concerned about.
395 return;
396 }
397
398 PipeRelay relay(mOut.buf());
399
400 if (relay.initCheck() != OK) {
401 LOG(ERROR) << "PipeRelay::initCheck() FAILED w/ " << relay.initCheck();
402 return;
403 }
404
405 deleted_unique_ptr<native_handle_t> fdHandle(
406 native_handle_create(1 /* numFds */, 0 /* numInts */),
407 native_handle_delete);
408
409 fdHandle->data[0] = relay.fd();
410
411 hardware::hidl_vec<hardware::hidl_string> options;
412 hardware::Return<void> ret = base->debug(fdHandle.get(), options);
413
414 if (!ret.isOk()) {
415 LOG(ERROR)
416 << interfaceName
417 << "::debug(...) FAILED. (instance "
418 << instanceName
419 << ")";
420 }
421}
422
Yifan Honga3b87092017-03-02 19:19:29 -0800423void Lshal::dumpTable() {
424 mServicesTable.description =
425 "All binderized services (registered services through hwservicemanager)";
426 mPassthroughRefTable.description =
Yifan Hong1071c1b2017-03-03 10:57:43 -0800427 "All interfaces that getService() has ever return as a passthrough interface;\n"
Yifan Honga3b87092017-03-02 19:19:29 -0800428 "PIDs / processes shown below might be inaccurate because the process\n"
Yifan Hong1071c1b2017-03-03 10:57:43 -0800429 "might have relinquished the interface or might have died.\n"
Yifan Honga3b87092017-03-02 19:19:29 -0800430 "The Server / Server CMD column can be ignored.\n"
Yifan Hong1071c1b2017-03-03 10:57:43 -0800431 "The Clients / Clients CMD column shows all process that have ever dlopen'ed \n"
432 "the library and successfully fetched the passthrough implementation.";
Yifan Honga3b87092017-03-02 19:19:29 -0800433 mImplementationsTable.description =
434 "All available passthrough implementations (all -impl.so files)";
435 forEachTable([this] (const Table &table) {
436 mOut << table.description << std::endl;
437 mOut << std::left;
438 printLine("Interface", "Transport", "Arch", "Server", "Server CMD",
439 "PTR", "Clients", "Clients CMD");
Andreas Huber28d35912017-03-24 13:14:11 -0700440
441 // 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 sp<IServiceManager> serviceManager;
446 if (mEmitDebugInfo && &table == &mServicesTable) {
447 serviceManager = ::android::hardware::defaultServiceManager();
448 }
449
Yifan Honga3b87092017-03-02 19:19:29 -0800450 for (const auto &entry : table) {
451 printLine(entry.interfaceName,
452 entry.transport,
453 getArchString(entry.arch),
454 entry.serverPid == NO_PID ? "N/A" : std::to_string(entry.serverPid),
455 entry.serverCmdline,
456 entry.serverObjectAddress == NO_PTR ? "N/A" : toHexString(entry.serverObjectAddress),
457 join(entry.clientPids, " "),
458 join(entry.clientCmdlines, ";"));
Andreas Huber28d35912017-03-24 13:14:11 -0700459
460 if (serviceManager != nullptr) {
461 auto pair = splitFirst(entry.interfaceName, '/');
462 emitDebugInfo(serviceManager, pair.first, pair.second);
463 }
Yifan Honga3b87092017-03-02 19:19:29 -0800464 }
465 mOut << std::endl;
466 });
467
Yifan Hongb0dde932017-02-10 17:49:58 -0800468}
469
Yifan Hong4b865492017-02-28 19:38:24 -0800470void Lshal::dump() {
471 if (mVintf) {
472 dumpVintf();
473 if (!!mFileOutput) {
474 mFileOutput.buf().close();
475 delete &mFileOutput.buf();
476 mFileOutput = nullptr;
477 }
478 mOut = std::cout;
479 } else {
480 dumpTable();
481 }
482}
483
Yifan Honga3b87092017-03-02 19:19:29 -0800484void Lshal::putEntry(TableEntrySource source, TableEntry &&entry) {
485 Table *table = nullptr;
486 switch (source) {
487 case HWSERVICEMANAGER_LIST :
488 table = &mServicesTable; break;
489 case PTSERVICEMANAGER_REG_CLIENT :
490 table = &mPassthroughRefTable; break;
491 case LIST_DLLIB :
492 table = &mImplementationsTable; break;
493 default:
494 mErr << "Error: Unknown source of entry " << source << std::endl;
495 }
496 if (table) {
497 table->entries.push_back(std::forward<TableEntry>(entry));
498 }
Yifan Hongb0dde932017-02-10 17:49:58 -0800499}
500
501Status Lshal::fetchAllLibraries(const sp<IServiceManager> &manager) {
502 using namespace ::android::hardware;
503 using namespace ::android::hidl::manager::V1_0;
504 using namespace ::android::hidl::base::V1_0;
Yifan Hongb4479022017-03-02 16:54:11 -0800505 auto ret = timeoutIPC(manager, &IServiceManager::debugDump, [&] (const auto &infos) {
506 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()};
Yifan Hong1071c1b2017-03-03 10:57:43 -0800510 entries.emplace(interfaceName, TableEntry{
Yifan Hongb4479022017-03-02 16:54:11 -0800511 .interfaceName = interfaceName,
Yifan Hongb0dde932017-02-10 17:49:58 -0800512 .transport = "passthrough",
513 .serverPid = NO_PID,
514 .serverObjectAddress = NO_PTR,
Yifan Hong4b865492017-02-28 19:38:24 -0800515 .clientPids = {},
Yifan Honga3b87092017-03-02 19:19:29 -0800516 .arch = ARCH_UNKNOWN
Yifan Hongb4479022017-03-02 16:54:11 -0800517 }).first->second.arch |= fromBaseArchitecture(info.arch);
518 }
519 for (auto &&pair : entries) {
Yifan Honga3b87092017-03-02 19:19:29 -0800520 putEntry(LIST_DLLIB, std::move(pair.second));
Yifan Hongb0dde932017-02-10 17:49:58 -0800521 }
522 });
523 if (!ret.isOk()) {
524 mErr << "Error: Failed to call list on getPassthroughServiceManager(): "
525 << ret.description() << std::endl;
526 return DUMP_ALL_LIBS_ERROR;
527 }
528 return OK;
529}
530
531Status Lshal::fetchPassthrough(const sp<IServiceManager> &manager) {
532 using namespace ::android::hardware;
Yifan Hongb4479022017-03-02 16:54:11 -0800533 using namespace ::android::hardware::details;
Yifan Hongb0dde932017-02-10 17:49:58 -0800534 using namespace ::android::hidl::manager::V1_0;
535 using namespace ::android::hidl::base::V1_0;
Yifan Honge2dadf02017-02-14 15:43:31 -0800536 auto ret = timeoutIPC(manager, &IServiceManager::debugDump, [&] (const auto &infos) {
Yifan Hongb0dde932017-02-10 17:49:58 -0800537 for (const auto &info : infos) {
Steven Moreland56d2d512017-03-21 12:17:55 -0700538 if (info.clientPids.size() <= 0) {
539 continue;
540 }
Yifan Honga3b87092017-03-02 19:19:29 -0800541 putEntry(PTSERVICEMANAGER_REG_CLIENT, {
Yifan Hongb0dde932017-02-10 17:49:58 -0800542 .interfaceName =
543 std::string{info.interfaceName.c_str()} + "/" +
544 std::string{info.instanceName.c_str()},
545 .transport = "passthrough",
546 .serverPid = info.clientPids.size() == 1 ? info.clientPids[0] : NO_PID,
547 .serverObjectAddress = NO_PTR,
Yifan Hong4b865492017-02-28 19:38:24 -0800548 .clientPids = info.clientPids,
Yifan Honga3b87092017-03-02 19:19:29 -0800549 .arch = fromBaseArchitecture(info.arch)
Yifan Hongb0dde932017-02-10 17:49:58 -0800550 });
551 }
552 });
553 if (!ret.isOk()) {
554 mErr << "Error: Failed to call debugDump on defaultServiceManager(): "
555 << ret.description() << std::endl;
556 return DUMP_PASSTHROUGH_ERROR;
557 }
558 return OK;
559}
560
561Status Lshal::fetchBinderized(const sp<IServiceManager> &manager) {
562 using namespace ::std;
563 using namespace ::android::hardware;
564 using namespace ::android::hidl::manager::V1_0;
565 using namespace ::android::hidl::base::V1_0;
566 const std::string mode = "hwbinder";
Yifan Hongb0dde932017-02-10 17:49:58 -0800567
Steven Moreland9b5c15d2017-02-28 17:52:58 -0800568 hidl_vec<hidl_string> fqInstanceNames;
569 // copying out for timeoutIPC
570 auto listRet = timeoutIPC(manager, &IServiceManager::list, [&] (const auto &names) {
571 fqInstanceNames = names;
Yifan Hongb0dde932017-02-10 17:49:58 -0800572 });
573 if (!listRet.isOk()) {
574 mErr << "Error: Failed to list services for " << mode << ": "
575 << listRet.description() << std::endl;
Steven Moreland9b5c15d2017-02-28 17:52:58 -0800576 return DUMP_BINDERIZED_ERROR;
577 }
578
579 Status status = OK;
580 // server pid, .ptr value of binder object, child pids
581 std::map<std::string, DebugInfo> allDebugInfos;
582 std::map<pid_t, std::map<uint64_t, Pids>> allPids;
583 for (const auto &fqInstanceName : fqInstanceNames) {
Yifan Hong4b865492017-02-28 19:38:24 -0800584 const auto pair = splitFirst(fqInstanceName, '/');
Steven Moreland9b5c15d2017-02-28 17:52:58 -0800585 const auto &serviceName = pair.first;
586 const auto &instanceName = pair.second;
587 auto getRet = timeoutIPC(manager, &IServiceManager::get, serviceName, instanceName);
588 if (!getRet.isOk()) {
589 mErr << "Warning: Skipping \"" << fqInstanceName << "\": "
590 << "cannot be fetched from service manager:"
591 << getRet.description() << std::endl;
592 status |= DUMP_BINDERIZED_ERROR;
593 continue;
594 }
595 sp<IBase> service = getRet;
596 if (service == nullptr) {
597 mErr << "Warning: Skipping \"" << fqInstanceName << "\": "
598 << "cannot be fetched from service manager (null)";
599 status |= DUMP_BINDERIZED_ERROR;
600 continue;
601 }
602 auto debugRet = timeoutIPC(service, &IBase::getDebugInfo, [&] (const auto &debugInfo) {
603 allDebugInfos[fqInstanceName] = debugInfo;
604 if (debugInfo.pid >= 0) {
605 allPids[static_cast<pid_t>(debugInfo.pid)].clear();
606 }
607 });
608 if (!debugRet.isOk()) {
609 mErr << "Warning: Skipping \"" << fqInstanceName << "\": "
610 << "debugging information cannot be retrieved:"
611 << debugRet.description() << std::endl;
612 status |= DUMP_BINDERIZED_ERROR;
613 }
614 }
615 for (auto &pair : allPids) {
616 pid_t serverPid = pair.first;
617 if (!getReferencedPids(serverPid, &allPids[serverPid])) {
618 mErr << "Warning: no information for PID " << serverPid
619 << ", are you root?" << std::endl;
620 status |= DUMP_BINDERIZED_ERROR;
621 }
622 }
623 for (const auto &fqInstanceName : fqInstanceNames) {
624 auto it = allDebugInfos.find(fqInstanceName);
625 if (it == allDebugInfos.end()) {
Yifan Honga3b87092017-03-02 19:19:29 -0800626 putEntry(HWSERVICEMANAGER_LIST, {
Steven Moreland9b5c15d2017-02-28 17:52:58 -0800627 .interfaceName = fqInstanceName,
628 .transport = mode,
629 .serverPid = NO_PID,
630 .serverObjectAddress = NO_PTR,
Yifan Hong4b865492017-02-28 19:38:24 -0800631 .clientPids = {},
Yifan Honga3b87092017-03-02 19:19:29 -0800632 .arch = ARCH_UNKNOWN
Steven Moreland9b5c15d2017-02-28 17:52:58 -0800633 });
634 continue;
635 }
636 const DebugInfo &info = it->second;
Yifan Honga3b87092017-03-02 19:19:29 -0800637 putEntry(HWSERVICEMANAGER_LIST, {
Steven Moreland9b5c15d2017-02-28 17:52:58 -0800638 .interfaceName = fqInstanceName,
639 .transport = mode,
640 .serverPid = info.pid,
641 .serverObjectAddress = info.ptr,
642 .clientPids = info.pid == NO_PID || info.ptr == NO_PTR
Yifan Hong4b865492017-02-28 19:38:24 -0800643 ? Pids{} : allPids[info.pid][info.ptr],
Yifan Hongb4479022017-03-02 16:54:11 -0800644 .arch = fromBaseArchitecture(info.arch),
Steven Moreland9b5c15d2017-02-28 17:52:58 -0800645 });
Yifan Hongb0dde932017-02-10 17:49:58 -0800646 }
647 return status;
648}
649
650Status Lshal::fetch() {
651 Status status = OK;
652 auto bManager = ::android::hardware::defaultServiceManager();
653 if (bManager == nullptr) {
654 mErr << "Failed to get defaultServiceManager()!" << std::endl;
655 status |= NO_BINDERIZED_MANAGER;
656 } else {
657 status |= fetchBinderized(bManager);
658 // Passthrough PIDs are registered to the binderized manager as well.
659 status |= fetchPassthrough(bManager);
660 }
661
662 auto pManager = ::android::hardware::getPassthroughServiceManager();
663 if (pManager == nullptr) {
664 mErr << "Failed to get getPassthroughServiceManager()!" << std::endl;
665 status |= NO_PASSTHROUGH_MANAGER;
666 } else {
667 status |= fetchAllLibraries(pManager);
668 }
669 return status;
670}
671
672void Lshal::usage() const {
673 mErr
674 << "usage: lshal" << std::endl
Yifan Honga3b87092017-03-02 19:19:29 -0800675 << " Dump all hals with default ordering and columns [-ipc]." << std::endl
Yifan Hongb4479022017-03-02 16:54:11 -0800676 << " lshal [--interface|-i] [--transport|-t] [-r|--arch]" << std::endl
Yifan Hong38d53e02017-02-13 17:51:59 -0800677 << " [--pid|-p] [--address|-a] [--clients|-c] [--cmdline|-m]" << std::endl
Yifan Hong4b865492017-02-28 19:38:24 -0800678 << " [--sort={interface|i|pid|p}] [--init-vintf[=path]]" << std::endl
Yifan Hong38d53e02017-02-13 17:51:59 -0800679 << " -i, --interface: print the interface name column" << std::endl
680 << " -n, --instance: print the instance name column" << std::endl
681 << " -t, --transport: print the transport mode column" << std::endl
Yifan Hongb4479022017-03-02 16:54:11 -0800682 << " -r, --arch: print if the HAL is in 64-bit or 32-bit" << std::endl
Yifan Hongae09a3d2017-02-14 17:33:50 -0800683 << " -p, --pid: print the server PID, or server cmdline if -m is set" << std::endl
Yifan Hong38d53e02017-02-13 17:51:59 -0800684 << " -a, --address: print the server object address column" << std::endl
Yifan Hongae09a3d2017-02-14 17:33:50 -0800685 << " -c, --clients: print the client PIDs, or client cmdlines if -m is set"
686 << std::endl
687 << " -m, --cmdline: print cmdline instead of PIDs" << std::endl
Steven Moreland6fb35102017-05-02 17:27:52 -0700688 << " -d, --debug: emit debug info from IBase::debug" << std::endl
Yifan Hong38d53e02017-02-13 17:51:59 -0800689 << " --sort=i, --sort=interface: sort by interface name" << std::endl
690 << " --sort=p, --sort=pid: sort by server pid" << std::endl
Yifan Hong4b865492017-02-28 19:38:24 -0800691 << " --init-vintf=path: form a skeleton HAL manifest to specified file " << std::endl
692 << " (stdout if no file specified)" << std::endl
Yifan Hongb0dde932017-02-10 17:49:58 -0800693 << " lshal [-h|--help]" << std::endl
694 << " -h, --help: show this help information." << std::endl;
695}
696
697Status Lshal::parseArgs(int argc, char **argv) {
698 static struct option longOptions[] = {
Yifan Hong38d53e02017-02-13 17:51:59 -0800699 // long options with short alternatives
700 {"help", no_argument, 0, 'h' },
701 {"interface", no_argument, 0, 'i' },
702 {"transport", no_argument, 0, 't' },
Yifan Hongb4479022017-03-02 16:54:11 -0800703 {"arch", no_argument, 0, 'r' },
Yifan Hong38d53e02017-02-13 17:51:59 -0800704 {"pid", no_argument, 0, 'p' },
705 {"address", no_argument, 0, 'a' },
706 {"clients", no_argument, 0, 'c' },
Yifan Hongae09a3d2017-02-14 17:33:50 -0800707 {"cmdline", no_argument, 0, 'm' },
Andreas Huber28d35912017-03-24 13:14:11 -0700708 {"debug", optional_argument, 0, 'd' },
Yifan Hong38d53e02017-02-13 17:51:59 -0800709
710 // long options without short alternatives
711 {"sort", required_argument, 0, 's' },
Yifan Hong4b865492017-02-28 19:38:24 -0800712 {"init-vintf",optional_argument, 0, 'v' },
Yifan Hong38d53e02017-02-13 17:51:59 -0800713 { 0, 0, 0, 0 }
Yifan Hongb0dde932017-02-10 17:49:58 -0800714 };
715
716 int optionIndex;
717 int c;
718 optind = 1;
719 for (;;) {
720 // using getopt_long in case we want to add other options in the future
Andreas Huber28d35912017-03-24 13:14:11 -0700721 c = getopt_long(argc, argv, "hitrpacmd", longOptions, &optionIndex);
Yifan Hongb0dde932017-02-10 17:49:58 -0800722 if (c == -1) {
723 break;
724 }
725 switch (c) {
Yifan Hong38d53e02017-02-13 17:51:59 -0800726 case 's': {
727 if (strcmp(optarg, "interface") == 0 || strcmp(optarg, "i") == 0) {
728 mSortColumn = TableEntry::sortByInterfaceName;
729 } else if (strcmp(optarg, "pid") == 0 || strcmp(optarg, "p") == 0) {
730 mSortColumn = TableEntry::sortByServerPid;
731 } else {
732 mErr << "Unrecognized sorting column: " << optarg << std::endl;
733 usage();
734 return USAGE;
735 }
736 break;
737 }
Yifan Hong4b865492017-02-28 19:38:24 -0800738 case 'v': {
739 if (optarg) {
740 mFileOutput = new std::ofstream{optarg};
741 mOut = mFileOutput;
742 if (!mFileOutput.buf().is_open()) {
743 mErr << "Could not open file '" << optarg << "'." << std::endl;
744 return IO_ERROR;
745 }
746 }
747 mVintf = true;
748 }
Yifan Hong38d53e02017-02-13 17:51:59 -0800749 case 'i': {
750 mSelectedColumns |= ENABLE_INTERFACE_NAME;
751 break;
752 }
753 case 't': {
754 mSelectedColumns |= ENABLE_TRANSPORT;
755 break;
756 }
Yifan Hongb4479022017-03-02 16:54:11 -0800757 case 'r': {
758 mSelectedColumns |= ENABLE_ARCH;
759 break;
760 }
Yifan Hong38d53e02017-02-13 17:51:59 -0800761 case 'p': {
762 mSelectedColumns |= ENABLE_SERVER_PID;
763 break;
764 }
765 case 'a': {
766 mSelectedColumns |= ENABLE_SERVER_ADDR;
767 break;
768 }
769 case 'c': {
770 mSelectedColumns |= ENABLE_CLIENT_PIDS;
771 break;
772 }
Yifan Hongae09a3d2017-02-14 17:33:50 -0800773 case 'm': {
774 mEnableCmdlines = true;
775 break;
776 }
Andreas Huber28d35912017-03-24 13:14:11 -0700777 case 'd': {
778 mEmitDebugInfo = true;
779
780 if (optarg) {
781 mFileOutput = new std::ofstream{optarg};
782 mOut = mFileOutput;
783 if (!mFileOutput.buf().is_open()) {
784 mErr << "Could not open file '" << optarg << "'." << std::endl;
785 return IO_ERROR;
786 }
787 chown(optarg, AID_SHELL, AID_SHELL);
788 }
789 break;
790 }
Yifan Hongb0dde932017-02-10 17:49:58 -0800791 case 'h': // falls through
792 default: // see unrecognized options
793 usage();
794 return USAGE;
795 }
796 }
Yifan Hong38d53e02017-02-13 17:51:59 -0800797
798 if (mSelectedColumns == 0) {
Yifan Honga3b87092017-03-02 19:19:29 -0800799 mSelectedColumns = ENABLE_INTERFACE_NAME | ENABLE_SERVER_PID | ENABLE_CLIENT_PIDS;
Yifan Hong38d53e02017-02-13 17:51:59 -0800800 }
Yifan Hongb0dde932017-02-10 17:49:58 -0800801 return OK;
802}
803
804int Lshal::main(int argc, char **argv) {
805 Status status = parseArgs(argc, argv);
806 if (status != OK) {
807 return status;
808 }
809 status = fetch();
Yifan Hong38d53e02017-02-13 17:51:59 -0800810 postprocess();
Yifan Hongb0dde932017-02-10 17:49:58 -0800811 dump();
812 return status;
813}
814
Yifan Honga57dffb2017-02-21 14:59:00 -0800815void signalHandler(int sig) {
816 if (sig == SIGINT) {
817 int retVal;
818 pthread_exit(&retVal);
819 }
820}
821
Yifan Hongb0dde932017-02-10 17:49:58 -0800822} // namespace lshal
823} // namespace android
824
825int main(int argc, char **argv) {
Yifan Honga57dffb2017-02-21 14:59:00 -0800826 signal(SIGINT, ::android::lshal::signalHandler);
Yifan Hongb0dde932017-02-10 17:49:58 -0800827 return ::android::lshal::Lshal{}.main(argc, argv);
828}