blob: 33bc43c397851fb82121fb3c0cc915c6a1eb7114 [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 Honga3b87092017-03-02 19:19:29 -0800166void Lshal::forEachTable(const std::function<void(Table &)> &f) {
Yifan Hong8ab3bee2017-03-08 14:01:11 -0800167 f(mServicesTable);
168 f(mPassthroughRefTable);
169 f(mImplementationsTable);
Yifan Honga3b87092017-03-02 19:19:29 -0800170}
171void Lshal::forEachTable(const std::function<void(const Table &)> &f) const {
Yifan Hong8ab3bee2017-03-08 14:01:11 -0800172 f(mServicesTable);
173 f(mPassthroughRefTable);
174 f(mImplementationsTable);
Yifan Honga3b87092017-03-02 19:19:29 -0800175}
176
Yifan Hong38d53e02017-02-13 17:51:59 -0800177void Lshal::postprocess() {
Yifan Honga3b87092017-03-02 19:19:29 -0800178 forEachTable([this](Table &table) {
179 if (mSortColumn) {
180 std::sort(table.begin(), table.end(), mSortColumn);
Yifan Hongae09a3d2017-02-14 17:33:50 -0800181 }
Yifan Honga3b87092017-03-02 19:19:29 -0800182 for (TableEntry &entry : table) {
183 entry.serverCmdline = getCmdline(entry.serverPid);
184 removeDeadProcesses(&entry.clientPids);
185 for (auto pid : entry.clientPids) {
186 entry.clientCmdlines.push_back(this->getCmdline(pid));
187 }
188 }
189 });
Yifan Hong3fdbd9f2017-03-08 14:01:58 -0800190 // use a double for loop here because lshal doesn't care about efficiency.
191 for (TableEntry &packageEntry : mImplementationsTable) {
192 std::string packageName = packageEntry.interfaceName;
193 FQName fqPackageName{packageName.substr(0, packageName.find("::"))};
194 if (!fqPackageName.isValid()) {
195 continue;
196 }
197 for (TableEntry &interfaceEntry : mPassthroughRefTable) {
198 if (interfaceEntry.arch != ARCH_UNKNOWN) {
199 continue;
200 }
201 FQName interfaceName{splitFirst(interfaceEntry.interfaceName, '/').first};
202 if (!interfaceName.isValid()) {
203 continue;
204 }
205 if (interfaceName.getPackageAndVersion() == fqPackageName) {
206 interfaceEntry.arch = packageEntry.arch;
207 }
208 }
209 }
Yifan Hong38d53e02017-02-13 17:51:59 -0800210}
211
212void Lshal::printLine(
213 const std::string &interfaceName,
Yifan Hongb4479022017-03-02 16:54:11 -0800214 const std::string &transport,
215 const std::string &arch,
216 const std::string &server,
Yifan Hongae09a3d2017-02-14 17:33:50 -0800217 const std::string &serverCmdline,
218 const std::string &address, const std::string &clients,
219 const std::string &clientCmdlines) const {
Yifan Hong38d53e02017-02-13 17:51:59 -0800220 if (mSelectedColumns & ENABLE_INTERFACE_NAME)
221 mOut << std::setw(80) << interfaceName << "\t";
222 if (mSelectedColumns & ENABLE_TRANSPORT)
223 mOut << std::setw(10) << transport << "\t";
Yifan Hongb4479022017-03-02 16:54:11 -0800224 if (mSelectedColumns & ENABLE_ARCH)
225 mOut << std::setw(5) << arch << "\t";
Yifan Hongae09a3d2017-02-14 17:33:50 -0800226 if (mSelectedColumns & ENABLE_SERVER_PID) {
227 if (mEnableCmdlines) {
228 mOut << std::setw(15) << serverCmdline << "\t";
229 } else {
230 mOut << std::setw(5) << server << "\t";
231 }
232 }
Yifan Hong38d53e02017-02-13 17:51:59 -0800233 if (mSelectedColumns & ENABLE_SERVER_ADDR)
234 mOut << std::setw(16) << address << "\t";
Yifan Hongae09a3d2017-02-14 17:33:50 -0800235 if (mSelectedColumns & ENABLE_CLIENT_PIDS) {
236 if (mEnableCmdlines) {
237 mOut << std::setw(0) << clientCmdlines;
238 } else {
239 mOut << std::setw(0) << clients;
240 }
241 }
Yifan Hong38d53e02017-02-13 17:51:59 -0800242 mOut << std::endl;
243}
244
Yifan Hong4b865492017-02-28 19:38:24 -0800245void Lshal::dumpVintf() const {
246 vintf::HalManifest manifest;
Yifan Honga3b87092017-03-02 19:19:29 -0800247 forEachTable([this, &manifest] (const Table &table) {
248 for (const TableEntry &entry : table) {
Yifan Hong4b865492017-02-28 19:38:24 -0800249
Yifan Honga3b87092017-03-02 19:19:29 -0800250 std::string fqInstanceName = entry.interfaceName;
Yifan Hong4b865492017-02-28 19:38:24 -0800251
Yifan Honga3b87092017-03-02 19:19:29 -0800252 if (&table == &mImplementationsTable) {
253 // Quick hack to work around *'s
254 replaceAll(&fqInstanceName, '*', 'D');
255 }
256 auto splittedFqInstanceName = splitFirst(fqInstanceName, '/');
257 FQName fqName(splittedFqInstanceName.first);
258 if (!fqName.isValid()) {
259 mErr << "Warning: '" << splittedFqInstanceName.first
260 << "' is not a valid FQName." << std::endl;
Yifan Hong4b865492017-02-28 19:38:24 -0800261 continue;
262 }
Yifan Honga3b87092017-03-02 19:19:29 -0800263 // Strip out system libs.
Steven Moreland9b6cd602017-03-22 14:22:55 -0700264 if (fqName.inPackage("android.hidl") ||
265 fqName.inPackage("android.frameworks") ||
266 fqName.inPackage("android.system")) {
Yifan Honga3b87092017-03-02 19:19:29 -0800267 continue;
268 }
269 std::string interfaceName =
270 &table == &mImplementationsTable ? "" : fqName.name();
271 std::string instanceName =
272 &table == &mImplementationsTable ? "" : splittedFqInstanceName.second;
273
274 vintf::Transport transport;
Yifan Hong3fdbd9f2017-03-08 14:01:58 -0800275 vintf::Arch arch;
Yifan Honga3b87092017-03-02 19:19:29 -0800276 if (entry.transport == "hwbinder") {
277 transport = vintf::Transport::HWBINDER;
Yifan Hong3fdbd9f2017-03-08 14:01:58 -0800278 arch = vintf::Arch::ARCH_EMPTY;
Yifan Honga3b87092017-03-02 19:19:29 -0800279 } else if (entry.transport == "passthrough") {
280 transport = vintf::Transport::PASSTHROUGH;
Yifan Hong3fdbd9f2017-03-08 14:01:58 -0800281 switch (entry.arch) {
282 case lshal::ARCH32:
283 arch = vintf::Arch::ARCH_32; break;
284 case lshal::ARCH64:
285 arch = vintf::Arch::ARCH_64; break;
286 case lshal::ARCH_BOTH:
287 arch = vintf::Arch::ARCH_32_64; break;
288 case lshal::ARCH_UNKNOWN: // fallthrough
289 default:
290 mErr << "Warning: '" << fqName.package()
291 << "' doesn't have bitness info, assuming 32+64." << std::endl;
292 arch = vintf::Arch::ARCH_32_64;
293 }
Yifan Hong4b865492017-02-28 19:38:24 -0800294 } else {
Yifan Honga3b87092017-03-02 19:19:29 -0800295 mErr << "Warning: '" << entry.transport << "' is not a valid transport." << std::endl;
296 continue;
297 }
298
Yifan Honga46e2f82017-04-13 01:00:12 -0700299 vintf::ManifestHal *hal = manifest.getAnyHal(fqName.package());
Yifan Honga3b87092017-03-02 19:19:29 -0800300 if (hal == nullptr) {
301 if (!manifest.add(vintf::ManifestHal{
302 .format = vintf::HalFormat::HIDL,
303 .name = fqName.package(),
Yifan Hong3fdbd9f2017-03-08 14:01:58 -0800304 .transportArch = {transport, arch}
Yifan Honga3b87092017-03-02 19:19:29 -0800305 })) {
306 mErr << "Warning: cannot add hal '" << fqInstanceName << "'" << std::endl;
307 continue;
308 }
Yifan Honga46e2f82017-04-13 01:00:12 -0700309 hal = manifest.getAnyHal(fqName.package());
Yifan Honga3b87092017-03-02 19:19:29 -0800310 }
311 if (hal == nullptr) {
312 mErr << "Warning: cannot get hal '" << fqInstanceName
313 << "' after adding it" << std::endl;
314 continue;
315 }
316 vintf::Version version{fqName.getPackageMajorVersion(), fqName.getPackageMinorVersion()};
317 if (std::find(hal->versions.begin(), hal->versions.end(), version) == hal->versions.end()) {
318 hal->versions.push_back(version);
319 }
320 if (&table != &mImplementationsTable) {
321 auto it = hal->interfaces.find(interfaceName);
322 if (it == hal->interfaces.end()) {
323 hal->interfaces.insert({interfaceName, {interfaceName, {{instanceName}}}});
324 } else {
325 it->second.instances.insert(instanceName);
326 }
Yifan Hong4b865492017-02-28 19:38:24 -0800327 }
328 }
Yifan Honga3b87092017-03-02 19:19:29 -0800329 });
Yifan Hong4b865492017-02-28 19:38:24 -0800330 mOut << vintf::gHalManifestConverter(manifest);
331}
332
Yifan Hongb4479022017-03-02 16:54:11 -0800333static const std::string &getArchString(Architecture arch) {
334 static const std::string sStr64 = "64";
335 static const std::string sStr32 = "32";
Yifan Hong1071c1b2017-03-03 10:57:43 -0800336 static const std::string sStrBoth = "32+64";
Yifan Hongb4479022017-03-02 16:54:11 -0800337 static const std::string sStrUnknown = "";
338 switch (arch) {
339 case ARCH64:
340 return sStr64;
341 case ARCH32:
342 return sStr32;
343 case ARCH_BOTH:
344 return sStrBoth;
345 case ARCH_UNKNOWN: // fall through
346 default:
347 return sStrUnknown;
348 }
349}
350
351static Architecture fromBaseArchitecture(::android::hidl::base::V1_0::DebugInfo::Architecture a) {
352 switch (a) {
353 case ::android::hidl::base::V1_0::DebugInfo::Architecture::IS_64BIT:
354 return ARCH64;
355 case ::android::hidl::base::V1_0::DebugInfo::Architecture::IS_32BIT:
356 return ARCH32;
357 case ::android::hidl::base::V1_0::DebugInfo::Architecture::UNKNOWN: // fallthrough
358 default:
359 return ARCH_UNKNOWN;
360 }
361}
362
Andreas Huber28d35912017-03-24 13:14:11 -0700363// A unique_ptr type using a custom deleter function.
364template<typename T>
365using deleted_unique_ptr = std::unique_ptr<T, std::function<void(T *)> >;
366
367void Lshal::emitDebugInfo(
368 const sp<IServiceManager> &serviceManager,
369 const std::string &interfaceName,
370 const std::string &instanceName) const {
371 using android::hidl::base::V1_0::IBase;
372
373 hardware::Return<sp<IBase>> retBase =
374 serviceManager->get(interfaceName, instanceName);
375
376 sp<IBase> base;
377 if (!retBase.isOk() || (base = retBase) == nullptr) {
378 // There's a small race, where a service instantiated while collecting
379 // the list of services has by now terminated, so this isn't anything
380 // to be concerned about.
381 return;
382 }
383
384 PipeRelay relay(mOut.buf());
385
386 if (relay.initCheck() != OK) {
387 LOG(ERROR) << "PipeRelay::initCheck() FAILED w/ " << relay.initCheck();
388 return;
389 }
390
391 deleted_unique_ptr<native_handle_t> fdHandle(
392 native_handle_create(1 /* numFds */, 0 /* numInts */),
393 native_handle_delete);
394
395 fdHandle->data[0] = relay.fd();
396
397 hardware::hidl_vec<hardware::hidl_string> options;
398 hardware::Return<void> ret = base->debug(fdHandle.get(), options);
399
400 if (!ret.isOk()) {
401 LOG(ERROR)
402 << interfaceName
403 << "::debug(...) FAILED. (instance "
404 << instanceName
405 << ")";
406 }
407}
408
Yifan Honga3b87092017-03-02 19:19:29 -0800409void Lshal::dumpTable() {
410 mServicesTable.description =
411 "All binderized services (registered services through hwservicemanager)";
412 mPassthroughRefTable.description =
Yifan Hong1071c1b2017-03-03 10:57:43 -0800413 "All interfaces that getService() has ever return as a passthrough interface;\n"
Yifan Honga3b87092017-03-02 19:19:29 -0800414 "PIDs / processes shown below might be inaccurate because the process\n"
Yifan Hong1071c1b2017-03-03 10:57:43 -0800415 "might have relinquished the interface or might have died.\n"
Yifan Honga3b87092017-03-02 19:19:29 -0800416 "The Server / Server CMD column can be ignored.\n"
Yifan Hong1071c1b2017-03-03 10:57:43 -0800417 "The Clients / Clients CMD column shows all process that have ever dlopen'ed \n"
418 "the library and successfully fetched the passthrough implementation.";
Yifan Honga3b87092017-03-02 19:19:29 -0800419 mImplementationsTable.description =
420 "All available passthrough implementations (all -impl.so files)";
421 forEachTable([this] (const Table &table) {
422 mOut << table.description << std::endl;
423 mOut << std::left;
424 printLine("Interface", "Transport", "Arch", "Server", "Server CMD",
425 "PTR", "Clients", "Clients CMD");
Andreas Huber28d35912017-03-24 13:14:11 -0700426
427 // We're only interested in dumping debug info for already
428 // instantiated services. There's little value in dumping the
429 // debug info for a service we create on the fly, so we only operate
430 // on the "mServicesTable".
431 sp<IServiceManager> serviceManager;
432 if (mEmitDebugInfo && &table == &mServicesTable) {
433 serviceManager = ::android::hardware::defaultServiceManager();
434 }
435
Yifan Honga3b87092017-03-02 19:19:29 -0800436 for (const auto &entry : table) {
437 printLine(entry.interfaceName,
438 entry.transport,
439 getArchString(entry.arch),
440 entry.serverPid == NO_PID ? "N/A" : std::to_string(entry.serverPid),
441 entry.serverCmdline,
442 entry.serverObjectAddress == NO_PTR ? "N/A" : toHexString(entry.serverObjectAddress),
443 join(entry.clientPids, " "),
444 join(entry.clientCmdlines, ";"));
Andreas Huber28d35912017-03-24 13:14:11 -0700445
446 if (serviceManager != nullptr) {
447 auto pair = splitFirst(entry.interfaceName, '/');
448 emitDebugInfo(serviceManager, pair.first, pair.second);
449 }
Yifan Honga3b87092017-03-02 19:19:29 -0800450 }
451 mOut << std::endl;
452 });
453
Yifan Hongb0dde932017-02-10 17:49:58 -0800454}
455
Yifan Hong4b865492017-02-28 19:38:24 -0800456void Lshal::dump() {
457 if (mVintf) {
458 dumpVintf();
459 if (!!mFileOutput) {
460 mFileOutput.buf().close();
461 delete &mFileOutput.buf();
462 mFileOutput = nullptr;
463 }
464 mOut = std::cout;
465 } else {
466 dumpTable();
467 }
468}
469
Yifan Honga3b87092017-03-02 19:19:29 -0800470void Lshal::putEntry(TableEntrySource source, TableEntry &&entry) {
471 Table *table = nullptr;
472 switch (source) {
473 case HWSERVICEMANAGER_LIST :
474 table = &mServicesTable; break;
475 case PTSERVICEMANAGER_REG_CLIENT :
476 table = &mPassthroughRefTable; break;
477 case LIST_DLLIB :
478 table = &mImplementationsTable; break;
479 default:
480 mErr << "Error: Unknown source of entry " << source << std::endl;
481 }
482 if (table) {
483 table->entries.push_back(std::forward<TableEntry>(entry));
484 }
Yifan Hongb0dde932017-02-10 17:49:58 -0800485}
486
487Status Lshal::fetchAllLibraries(const sp<IServiceManager> &manager) {
488 using namespace ::android::hardware;
489 using namespace ::android::hidl::manager::V1_0;
490 using namespace ::android::hidl::base::V1_0;
Yifan Hongb4479022017-03-02 16:54:11 -0800491 auto ret = timeoutIPC(manager, &IServiceManager::debugDump, [&] (const auto &infos) {
492 std::map<std::string, TableEntry> entries;
493 for (const auto &info : infos) {
494 std::string interfaceName = std::string{info.interfaceName.c_str()} + "/" +
495 std::string{info.instanceName.c_str()};
Yifan Hong1071c1b2017-03-03 10:57:43 -0800496 entries.emplace(interfaceName, TableEntry{
Yifan Hongb4479022017-03-02 16:54:11 -0800497 .interfaceName = interfaceName,
Yifan Hongb0dde932017-02-10 17:49:58 -0800498 .transport = "passthrough",
499 .serverPid = NO_PID,
500 .serverObjectAddress = NO_PTR,
Yifan Hong4b865492017-02-28 19:38:24 -0800501 .clientPids = {},
Yifan Honga3b87092017-03-02 19:19:29 -0800502 .arch = ARCH_UNKNOWN
Yifan Hongb4479022017-03-02 16:54:11 -0800503 }).first->second.arch |= fromBaseArchitecture(info.arch);
504 }
505 for (auto &&pair : entries) {
Yifan Honga3b87092017-03-02 19:19:29 -0800506 putEntry(LIST_DLLIB, std::move(pair.second));
Yifan Hongb0dde932017-02-10 17:49:58 -0800507 }
508 });
509 if (!ret.isOk()) {
510 mErr << "Error: Failed to call list on getPassthroughServiceManager(): "
511 << ret.description() << std::endl;
512 return DUMP_ALL_LIBS_ERROR;
513 }
514 return OK;
515}
516
517Status Lshal::fetchPassthrough(const sp<IServiceManager> &manager) {
518 using namespace ::android::hardware;
Yifan Hongb4479022017-03-02 16:54:11 -0800519 using namespace ::android::hardware::details;
Yifan Hongb0dde932017-02-10 17:49:58 -0800520 using namespace ::android::hidl::manager::V1_0;
521 using namespace ::android::hidl::base::V1_0;
Yifan Honge2dadf02017-02-14 15:43:31 -0800522 auto ret = timeoutIPC(manager, &IServiceManager::debugDump, [&] (const auto &infos) {
Yifan Hongb0dde932017-02-10 17:49:58 -0800523 for (const auto &info : infos) {
Steven Moreland56d2d512017-03-21 12:17:55 -0700524 if (info.clientPids.size() <= 0) {
525 continue;
526 }
Yifan Honga3b87092017-03-02 19:19:29 -0800527 putEntry(PTSERVICEMANAGER_REG_CLIENT, {
Yifan Hongb0dde932017-02-10 17:49:58 -0800528 .interfaceName =
529 std::string{info.interfaceName.c_str()} + "/" +
530 std::string{info.instanceName.c_str()},
531 .transport = "passthrough",
532 .serverPid = info.clientPids.size() == 1 ? info.clientPids[0] : NO_PID,
533 .serverObjectAddress = NO_PTR,
Yifan Hong4b865492017-02-28 19:38:24 -0800534 .clientPids = info.clientPids,
Yifan Honga3b87092017-03-02 19:19:29 -0800535 .arch = fromBaseArchitecture(info.arch)
Yifan Hongb0dde932017-02-10 17:49:58 -0800536 });
537 }
538 });
539 if (!ret.isOk()) {
540 mErr << "Error: Failed to call debugDump on defaultServiceManager(): "
541 << ret.description() << std::endl;
542 return DUMP_PASSTHROUGH_ERROR;
543 }
544 return OK;
545}
546
547Status Lshal::fetchBinderized(const sp<IServiceManager> &manager) {
548 using namespace ::std;
549 using namespace ::android::hardware;
550 using namespace ::android::hidl::manager::V1_0;
551 using namespace ::android::hidl::base::V1_0;
552 const std::string mode = "hwbinder";
Yifan Hongb0dde932017-02-10 17:49:58 -0800553
Steven Moreland9b5c15d2017-02-28 17:52:58 -0800554 hidl_vec<hidl_string> fqInstanceNames;
555 // copying out for timeoutIPC
556 auto listRet = timeoutIPC(manager, &IServiceManager::list, [&] (const auto &names) {
557 fqInstanceNames = names;
Yifan Hongb0dde932017-02-10 17:49:58 -0800558 });
559 if (!listRet.isOk()) {
560 mErr << "Error: Failed to list services for " << mode << ": "
561 << listRet.description() << std::endl;
Steven Moreland9b5c15d2017-02-28 17:52:58 -0800562 return DUMP_BINDERIZED_ERROR;
563 }
564
565 Status status = OK;
566 // server pid, .ptr value of binder object, child pids
567 std::map<std::string, DebugInfo> allDebugInfos;
568 std::map<pid_t, std::map<uint64_t, Pids>> allPids;
569 for (const auto &fqInstanceName : fqInstanceNames) {
Yifan Hong4b865492017-02-28 19:38:24 -0800570 const auto pair = splitFirst(fqInstanceName, '/');
Steven Moreland9b5c15d2017-02-28 17:52:58 -0800571 const auto &serviceName = pair.first;
572 const auto &instanceName = pair.second;
573 auto getRet = timeoutIPC(manager, &IServiceManager::get, serviceName, instanceName);
574 if (!getRet.isOk()) {
575 mErr << "Warning: Skipping \"" << fqInstanceName << "\": "
576 << "cannot be fetched from service manager:"
577 << getRet.description() << std::endl;
578 status |= DUMP_BINDERIZED_ERROR;
579 continue;
580 }
581 sp<IBase> service = getRet;
582 if (service == nullptr) {
583 mErr << "Warning: Skipping \"" << fqInstanceName << "\": "
584 << "cannot be fetched from service manager (null)";
585 status |= DUMP_BINDERIZED_ERROR;
586 continue;
587 }
588 auto debugRet = timeoutIPC(service, &IBase::getDebugInfo, [&] (const auto &debugInfo) {
589 allDebugInfos[fqInstanceName] = debugInfo;
590 if (debugInfo.pid >= 0) {
591 allPids[static_cast<pid_t>(debugInfo.pid)].clear();
592 }
593 });
594 if (!debugRet.isOk()) {
595 mErr << "Warning: Skipping \"" << fqInstanceName << "\": "
596 << "debugging information cannot be retrieved:"
597 << debugRet.description() << std::endl;
598 status |= DUMP_BINDERIZED_ERROR;
599 }
600 }
601 for (auto &pair : allPids) {
602 pid_t serverPid = pair.first;
603 if (!getReferencedPids(serverPid, &allPids[serverPid])) {
604 mErr << "Warning: no information for PID " << serverPid
605 << ", are you root?" << std::endl;
606 status |= DUMP_BINDERIZED_ERROR;
607 }
608 }
609 for (const auto &fqInstanceName : fqInstanceNames) {
610 auto it = allDebugInfos.find(fqInstanceName);
611 if (it == allDebugInfos.end()) {
Yifan Honga3b87092017-03-02 19:19:29 -0800612 putEntry(HWSERVICEMANAGER_LIST, {
Steven Moreland9b5c15d2017-02-28 17:52:58 -0800613 .interfaceName = fqInstanceName,
614 .transport = mode,
615 .serverPid = NO_PID,
616 .serverObjectAddress = NO_PTR,
Yifan Hong4b865492017-02-28 19:38:24 -0800617 .clientPids = {},
Yifan Honga3b87092017-03-02 19:19:29 -0800618 .arch = ARCH_UNKNOWN
Steven Moreland9b5c15d2017-02-28 17:52:58 -0800619 });
620 continue;
621 }
622 const DebugInfo &info = it->second;
Yifan Honga3b87092017-03-02 19:19:29 -0800623 putEntry(HWSERVICEMANAGER_LIST, {
Steven Moreland9b5c15d2017-02-28 17:52:58 -0800624 .interfaceName = fqInstanceName,
625 .transport = mode,
626 .serverPid = info.pid,
627 .serverObjectAddress = info.ptr,
628 .clientPids = info.pid == NO_PID || info.ptr == NO_PTR
Yifan Hong4b865492017-02-28 19:38:24 -0800629 ? Pids{} : allPids[info.pid][info.ptr],
Yifan Hongb4479022017-03-02 16:54:11 -0800630 .arch = fromBaseArchitecture(info.arch),
Steven Moreland9b5c15d2017-02-28 17:52:58 -0800631 });
Yifan Hongb0dde932017-02-10 17:49:58 -0800632 }
633 return status;
634}
635
636Status Lshal::fetch() {
637 Status status = OK;
638 auto bManager = ::android::hardware::defaultServiceManager();
639 if (bManager == nullptr) {
640 mErr << "Failed to get defaultServiceManager()!" << std::endl;
641 status |= NO_BINDERIZED_MANAGER;
642 } else {
643 status |= fetchBinderized(bManager);
644 // Passthrough PIDs are registered to the binderized manager as well.
645 status |= fetchPassthrough(bManager);
646 }
647
648 auto pManager = ::android::hardware::getPassthroughServiceManager();
649 if (pManager == nullptr) {
650 mErr << "Failed to get getPassthroughServiceManager()!" << std::endl;
651 status |= NO_PASSTHROUGH_MANAGER;
652 } else {
653 status |= fetchAllLibraries(pManager);
654 }
655 return status;
656}
657
658void Lshal::usage() const {
659 mErr
660 << "usage: lshal" << std::endl
Yifan Honga3b87092017-03-02 19:19:29 -0800661 << " Dump all hals with default ordering and columns [-ipc]." << std::endl
Yifan Hongb4479022017-03-02 16:54:11 -0800662 << " lshal [--interface|-i] [--transport|-t] [-r|--arch]" << std::endl
Yifan Hong38d53e02017-02-13 17:51:59 -0800663 << " [--pid|-p] [--address|-a] [--clients|-c] [--cmdline|-m]" << std::endl
Yifan Hong4b865492017-02-28 19:38:24 -0800664 << " [--sort={interface|i|pid|p}] [--init-vintf[=path]]" << std::endl
Yifan Hong38d53e02017-02-13 17:51:59 -0800665 << " -i, --interface: print the interface name column" << std::endl
666 << " -n, --instance: print the instance name column" << std::endl
667 << " -t, --transport: print the transport mode column" << std::endl
Yifan Hongb4479022017-03-02 16:54:11 -0800668 << " -r, --arch: print if the HAL is in 64-bit or 32-bit" << std::endl
Yifan Hongae09a3d2017-02-14 17:33:50 -0800669 << " -p, --pid: print the server PID, or server cmdline if -m is set" << std::endl
Yifan Hong38d53e02017-02-13 17:51:59 -0800670 << " -a, --address: print the server object address column" << std::endl
Yifan Hongae09a3d2017-02-14 17:33:50 -0800671 << " -c, --clients: print the client PIDs, or client cmdlines if -m is set"
672 << std::endl
673 << " -m, --cmdline: print cmdline instead of PIDs" << std::endl
Yifan Hong38d53e02017-02-13 17:51:59 -0800674 << " --sort=i, --sort=interface: sort by interface name" << std::endl
675 << " --sort=p, --sort=pid: sort by server pid" << std::endl
Yifan Hong4b865492017-02-28 19:38:24 -0800676 << " --init-vintf=path: form a skeleton HAL manifest to specified file " << std::endl
677 << " (stdout if no file specified)" << std::endl
Yifan Hongb0dde932017-02-10 17:49:58 -0800678 << " lshal [-h|--help]" << std::endl
679 << " -h, --help: show this help information." << std::endl;
680}
681
682Status Lshal::parseArgs(int argc, char **argv) {
683 static struct option longOptions[] = {
Yifan Hong38d53e02017-02-13 17:51:59 -0800684 // long options with short alternatives
685 {"help", no_argument, 0, 'h' },
686 {"interface", no_argument, 0, 'i' },
687 {"transport", no_argument, 0, 't' },
Yifan Hongb4479022017-03-02 16:54:11 -0800688 {"arch", no_argument, 0, 'r' },
Yifan Hong38d53e02017-02-13 17:51:59 -0800689 {"pid", no_argument, 0, 'p' },
690 {"address", no_argument, 0, 'a' },
691 {"clients", no_argument, 0, 'c' },
Yifan Hongae09a3d2017-02-14 17:33:50 -0800692 {"cmdline", no_argument, 0, 'm' },
Andreas Huber28d35912017-03-24 13:14:11 -0700693 {"debug", optional_argument, 0, 'd' },
Yifan Hong38d53e02017-02-13 17:51:59 -0800694
695 // long options without short alternatives
696 {"sort", required_argument, 0, 's' },
Yifan Hong4b865492017-02-28 19:38:24 -0800697 {"init-vintf",optional_argument, 0, 'v' },
Yifan Hong38d53e02017-02-13 17:51:59 -0800698 { 0, 0, 0, 0 }
Yifan Hongb0dde932017-02-10 17:49:58 -0800699 };
700
701 int optionIndex;
702 int c;
703 optind = 1;
704 for (;;) {
705 // using getopt_long in case we want to add other options in the future
Andreas Huber28d35912017-03-24 13:14:11 -0700706 c = getopt_long(argc, argv, "hitrpacmd", longOptions, &optionIndex);
Yifan Hongb0dde932017-02-10 17:49:58 -0800707 if (c == -1) {
708 break;
709 }
710 switch (c) {
Yifan Hong38d53e02017-02-13 17:51:59 -0800711 case 's': {
712 if (strcmp(optarg, "interface") == 0 || strcmp(optarg, "i") == 0) {
713 mSortColumn = TableEntry::sortByInterfaceName;
714 } else if (strcmp(optarg, "pid") == 0 || strcmp(optarg, "p") == 0) {
715 mSortColumn = TableEntry::sortByServerPid;
716 } else {
717 mErr << "Unrecognized sorting column: " << optarg << std::endl;
718 usage();
719 return USAGE;
720 }
721 break;
722 }
Yifan Hong4b865492017-02-28 19:38:24 -0800723 case 'v': {
724 if (optarg) {
725 mFileOutput = new std::ofstream{optarg};
726 mOut = mFileOutput;
727 if (!mFileOutput.buf().is_open()) {
728 mErr << "Could not open file '" << optarg << "'." << std::endl;
729 return IO_ERROR;
730 }
731 }
732 mVintf = true;
733 }
Yifan Hong38d53e02017-02-13 17:51:59 -0800734 case 'i': {
735 mSelectedColumns |= ENABLE_INTERFACE_NAME;
736 break;
737 }
738 case 't': {
739 mSelectedColumns |= ENABLE_TRANSPORT;
740 break;
741 }
Yifan Hongb4479022017-03-02 16:54:11 -0800742 case 'r': {
743 mSelectedColumns |= ENABLE_ARCH;
744 break;
745 }
Yifan Hong38d53e02017-02-13 17:51:59 -0800746 case 'p': {
747 mSelectedColumns |= ENABLE_SERVER_PID;
748 break;
749 }
750 case 'a': {
751 mSelectedColumns |= ENABLE_SERVER_ADDR;
752 break;
753 }
754 case 'c': {
755 mSelectedColumns |= ENABLE_CLIENT_PIDS;
756 break;
757 }
Yifan Hongae09a3d2017-02-14 17:33:50 -0800758 case 'm': {
759 mEnableCmdlines = true;
760 break;
761 }
Andreas Huber28d35912017-03-24 13:14:11 -0700762 case 'd': {
763 mEmitDebugInfo = true;
764
765 if (optarg) {
766 mFileOutput = new std::ofstream{optarg};
767 mOut = mFileOutput;
768 if (!mFileOutput.buf().is_open()) {
769 mErr << "Could not open file '" << optarg << "'." << std::endl;
770 return IO_ERROR;
771 }
772 chown(optarg, AID_SHELL, AID_SHELL);
773 }
774 break;
775 }
Yifan Hongb0dde932017-02-10 17:49:58 -0800776 case 'h': // falls through
777 default: // see unrecognized options
778 usage();
779 return USAGE;
780 }
781 }
Yifan Hong38d53e02017-02-13 17:51:59 -0800782
783 if (mSelectedColumns == 0) {
Yifan Honga3b87092017-03-02 19:19:29 -0800784 mSelectedColumns = ENABLE_INTERFACE_NAME | ENABLE_SERVER_PID | ENABLE_CLIENT_PIDS;
Yifan Hong38d53e02017-02-13 17:51:59 -0800785 }
Yifan Hongb0dde932017-02-10 17:49:58 -0800786 return OK;
787}
788
789int Lshal::main(int argc, char **argv) {
790 Status status = parseArgs(argc, argv);
791 if (status != OK) {
792 return status;
793 }
794 status = fetch();
Yifan Hong38d53e02017-02-13 17:51:59 -0800795 postprocess();
Yifan Hongb0dde932017-02-10 17:49:58 -0800796 dump();
797 return status;
798}
799
Yifan Honga57dffb2017-02-21 14:59:00 -0800800void signalHandler(int sig) {
801 if (sig == SIGINT) {
802 int retVal;
803 pthread_exit(&retVal);
804 }
805}
806
Yifan Hongb0dde932017-02-10 17:49:58 -0800807} // namespace lshal
808} // namespace android
809
810int main(int argc, char **argv) {
Yifan Honga57dffb2017-02-21 14:59:00 -0800811 signal(SIGINT, ::android::lshal::signalHandler);
Yifan Hongb0dde932017-02-10 17:49:58 -0800812 return ::android::lshal::Lshal{}.main(argc, argv);
813}