blob: 420ec3cbf6df362e712e9f343ec155dc6908e9ed [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
299 vintf::ManifestHal *hal = manifest.getHal(fqName.package());
300 if (hal == nullptr) {
301 if (!manifest.add(vintf::ManifestHal{
302 .format = vintf::HalFormat::HIDL,
303 .name = fqName.package(),
304 .impl = {.implLevel = vintf::ImplLevel::GENERIC, .impl = ""},
Yifan Hong3fdbd9f2017-03-08 14:01:58 -0800305 .transportArch = {transport, arch}
Yifan Honga3b87092017-03-02 19:19:29 -0800306 })) {
307 mErr << "Warning: cannot add hal '" << fqInstanceName << "'" << std::endl;
308 continue;
309 }
310 hal = manifest.getHal(fqName.package());
311 }
312 if (hal == nullptr) {
313 mErr << "Warning: cannot get hal '" << fqInstanceName
314 << "' after adding it" << std::endl;
315 continue;
316 }
317 vintf::Version version{fqName.getPackageMajorVersion(), fqName.getPackageMinorVersion()};
318 if (std::find(hal->versions.begin(), hal->versions.end(), version) == hal->versions.end()) {
319 hal->versions.push_back(version);
320 }
321 if (&table != &mImplementationsTable) {
322 auto it = hal->interfaces.find(interfaceName);
323 if (it == hal->interfaces.end()) {
324 hal->interfaces.insert({interfaceName, {interfaceName, {{instanceName}}}});
325 } else {
326 it->second.instances.insert(instanceName);
327 }
Yifan Hong4b865492017-02-28 19:38:24 -0800328 }
329 }
Yifan Honga3b87092017-03-02 19:19:29 -0800330 });
Yifan Hong4b865492017-02-28 19:38:24 -0800331 mOut << vintf::gHalManifestConverter(manifest);
332}
333
Yifan Hongb4479022017-03-02 16:54:11 -0800334static const std::string &getArchString(Architecture arch) {
335 static const std::string sStr64 = "64";
336 static const std::string sStr32 = "32";
Yifan Hong1071c1b2017-03-03 10:57:43 -0800337 static const std::string sStrBoth = "32+64";
Yifan Hongb4479022017-03-02 16:54:11 -0800338 static const std::string sStrUnknown = "";
339 switch (arch) {
340 case ARCH64:
341 return sStr64;
342 case ARCH32:
343 return sStr32;
344 case ARCH_BOTH:
345 return sStrBoth;
346 case ARCH_UNKNOWN: // fall through
347 default:
348 return sStrUnknown;
349 }
350}
351
352static Architecture fromBaseArchitecture(::android::hidl::base::V1_0::DebugInfo::Architecture a) {
353 switch (a) {
354 case ::android::hidl::base::V1_0::DebugInfo::Architecture::IS_64BIT:
355 return ARCH64;
356 case ::android::hidl::base::V1_0::DebugInfo::Architecture::IS_32BIT:
357 return ARCH32;
358 case ::android::hidl::base::V1_0::DebugInfo::Architecture::UNKNOWN: // fallthrough
359 default:
360 return ARCH_UNKNOWN;
361 }
362}
363
Andreas Huber28d35912017-03-24 13:14:11 -0700364// A unique_ptr type using a custom deleter function.
365template<typename T>
366using deleted_unique_ptr = std::unique_ptr<T, std::function<void(T *)> >;
367
368void Lshal::emitDebugInfo(
369 const sp<IServiceManager> &serviceManager,
370 const std::string &interfaceName,
371 const std::string &instanceName) const {
372 using android::hidl::base::V1_0::IBase;
373
374 hardware::Return<sp<IBase>> retBase =
375 serviceManager->get(interfaceName, instanceName);
376
377 sp<IBase> base;
378 if (!retBase.isOk() || (base = retBase) == nullptr) {
379 // There's a small race, where a service instantiated while collecting
380 // the list of services has by now terminated, so this isn't anything
381 // to be concerned about.
382 return;
383 }
384
385 PipeRelay relay(mOut.buf());
386
387 if (relay.initCheck() != OK) {
388 LOG(ERROR) << "PipeRelay::initCheck() FAILED w/ " << relay.initCheck();
389 return;
390 }
391
392 deleted_unique_ptr<native_handle_t> fdHandle(
393 native_handle_create(1 /* numFds */, 0 /* numInts */),
394 native_handle_delete);
395
396 fdHandle->data[0] = relay.fd();
397
398 hardware::hidl_vec<hardware::hidl_string> options;
399 hardware::Return<void> ret = base->debug(fdHandle.get(), options);
400
401 if (!ret.isOk()) {
402 LOG(ERROR)
403 << interfaceName
404 << "::debug(...) FAILED. (instance "
405 << instanceName
406 << ")";
407 }
408}
409
Yifan Honga3b87092017-03-02 19:19:29 -0800410void Lshal::dumpTable() {
411 mServicesTable.description =
412 "All binderized services (registered services through hwservicemanager)";
413 mPassthroughRefTable.description =
Yifan Hong1071c1b2017-03-03 10:57:43 -0800414 "All interfaces that getService() has ever return as a passthrough interface;\n"
Yifan Honga3b87092017-03-02 19:19:29 -0800415 "PIDs / processes shown below might be inaccurate because the process\n"
Yifan Hong1071c1b2017-03-03 10:57:43 -0800416 "might have relinquished the interface or might have died.\n"
Yifan Honga3b87092017-03-02 19:19:29 -0800417 "The Server / Server CMD column can be ignored.\n"
Yifan Hong1071c1b2017-03-03 10:57:43 -0800418 "The Clients / Clients CMD column shows all process that have ever dlopen'ed \n"
419 "the library and successfully fetched the passthrough implementation.";
Yifan Honga3b87092017-03-02 19:19:29 -0800420 mImplementationsTable.description =
421 "All available passthrough implementations (all -impl.so files)";
422 forEachTable([this] (const Table &table) {
423 mOut << table.description << std::endl;
424 mOut << std::left;
425 printLine("Interface", "Transport", "Arch", "Server", "Server CMD",
426 "PTR", "Clients", "Clients CMD");
Andreas Huber28d35912017-03-24 13:14:11 -0700427
428 // We're only interested in dumping debug info for already
429 // instantiated services. There's little value in dumping the
430 // debug info for a service we create on the fly, so we only operate
431 // on the "mServicesTable".
432 sp<IServiceManager> serviceManager;
433 if (mEmitDebugInfo && &table == &mServicesTable) {
434 serviceManager = ::android::hardware::defaultServiceManager();
435 }
436
Yifan Honga3b87092017-03-02 19:19:29 -0800437 for (const auto &entry : table) {
438 printLine(entry.interfaceName,
439 entry.transport,
440 getArchString(entry.arch),
441 entry.serverPid == NO_PID ? "N/A" : std::to_string(entry.serverPid),
442 entry.serverCmdline,
443 entry.serverObjectAddress == NO_PTR ? "N/A" : toHexString(entry.serverObjectAddress),
444 join(entry.clientPids, " "),
445 join(entry.clientCmdlines, ";"));
Andreas Huber28d35912017-03-24 13:14:11 -0700446
447 if (serviceManager != nullptr) {
448 auto pair = splitFirst(entry.interfaceName, '/');
449 emitDebugInfo(serviceManager, pair.first, pair.second);
450 }
Yifan Honga3b87092017-03-02 19:19:29 -0800451 }
452 mOut << std::endl;
453 });
454
Yifan Hongb0dde932017-02-10 17:49:58 -0800455}
456
Yifan Hong4b865492017-02-28 19:38:24 -0800457void Lshal::dump() {
458 if (mVintf) {
459 dumpVintf();
460 if (!!mFileOutput) {
461 mFileOutput.buf().close();
462 delete &mFileOutput.buf();
463 mFileOutput = nullptr;
464 }
465 mOut = std::cout;
466 } else {
467 dumpTable();
468 }
469}
470
Yifan Honga3b87092017-03-02 19:19:29 -0800471void Lshal::putEntry(TableEntrySource source, TableEntry &&entry) {
472 Table *table = nullptr;
473 switch (source) {
474 case HWSERVICEMANAGER_LIST :
475 table = &mServicesTable; break;
476 case PTSERVICEMANAGER_REG_CLIENT :
477 table = &mPassthroughRefTable; break;
478 case LIST_DLLIB :
479 table = &mImplementationsTable; break;
480 default:
481 mErr << "Error: Unknown source of entry " << source << std::endl;
482 }
483 if (table) {
484 table->entries.push_back(std::forward<TableEntry>(entry));
485 }
Yifan Hongb0dde932017-02-10 17:49:58 -0800486}
487
488Status Lshal::fetchAllLibraries(const sp<IServiceManager> &manager) {
489 using namespace ::android::hardware;
490 using namespace ::android::hidl::manager::V1_0;
491 using namespace ::android::hidl::base::V1_0;
Yifan Hongb4479022017-03-02 16:54:11 -0800492 auto ret = timeoutIPC(manager, &IServiceManager::debugDump, [&] (const auto &infos) {
493 std::map<std::string, TableEntry> entries;
494 for (const auto &info : infos) {
495 std::string interfaceName = std::string{info.interfaceName.c_str()} + "/" +
496 std::string{info.instanceName.c_str()};
Yifan Hong1071c1b2017-03-03 10:57:43 -0800497 entries.emplace(interfaceName, TableEntry{
Yifan Hongb4479022017-03-02 16:54:11 -0800498 .interfaceName = interfaceName,
Yifan Hongb0dde932017-02-10 17:49:58 -0800499 .transport = "passthrough",
500 .serverPid = NO_PID,
501 .serverObjectAddress = NO_PTR,
Yifan Hong4b865492017-02-28 19:38:24 -0800502 .clientPids = {},
Yifan Honga3b87092017-03-02 19:19:29 -0800503 .arch = ARCH_UNKNOWN
Yifan Hongb4479022017-03-02 16:54:11 -0800504 }).first->second.arch |= fromBaseArchitecture(info.arch);
505 }
506 for (auto &&pair : entries) {
Yifan Honga3b87092017-03-02 19:19:29 -0800507 putEntry(LIST_DLLIB, std::move(pair.second));
Yifan Hongb0dde932017-02-10 17:49:58 -0800508 }
509 });
510 if (!ret.isOk()) {
511 mErr << "Error: Failed to call list on getPassthroughServiceManager(): "
512 << ret.description() << std::endl;
513 return DUMP_ALL_LIBS_ERROR;
514 }
515 return OK;
516}
517
518Status Lshal::fetchPassthrough(const sp<IServiceManager> &manager) {
519 using namespace ::android::hardware;
Yifan Hongb4479022017-03-02 16:54:11 -0800520 using namespace ::android::hardware::details;
Yifan Hongb0dde932017-02-10 17:49:58 -0800521 using namespace ::android::hidl::manager::V1_0;
522 using namespace ::android::hidl::base::V1_0;
Yifan Honge2dadf02017-02-14 15:43:31 -0800523 auto ret = timeoutIPC(manager, &IServiceManager::debugDump, [&] (const auto &infos) {
Yifan Hongb0dde932017-02-10 17:49:58 -0800524 for (const auto &info : infos) {
Steven Moreland56d2d512017-03-21 12:17:55 -0700525 if (info.clientPids.size() <= 0) {
526 continue;
527 }
Yifan Honga3b87092017-03-02 19:19:29 -0800528 putEntry(PTSERVICEMANAGER_REG_CLIENT, {
Yifan Hongb0dde932017-02-10 17:49:58 -0800529 .interfaceName =
530 std::string{info.interfaceName.c_str()} + "/" +
531 std::string{info.instanceName.c_str()},
532 .transport = "passthrough",
533 .serverPid = info.clientPids.size() == 1 ? info.clientPids[0] : NO_PID,
534 .serverObjectAddress = NO_PTR,
Yifan Hong4b865492017-02-28 19:38:24 -0800535 .clientPids = info.clientPids,
Yifan Honga3b87092017-03-02 19:19:29 -0800536 .arch = fromBaseArchitecture(info.arch)
Yifan Hongb0dde932017-02-10 17:49:58 -0800537 });
538 }
539 });
540 if (!ret.isOk()) {
541 mErr << "Error: Failed to call debugDump on defaultServiceManager(): "
542 << ret.description() << std::endl;
543 return DUMP_PASSTHROUGH_ERROR;
544 }
545 return OK;
546}
547
548Status Lshal::fetchBinderized(const sp<IServiceManager> &manager) {
549 using namespace ::std;
550 using namespace ::android::hardware;
551 using namespace ::android::hidl::manager::V1_0;
552 using namespace ::android::hidl::base::V1_0;
553 const std::string mode = "hwbinder";
Yifan Hongb0dde932017-02-10 17:49:58 -0800554
Steven Moreland9b5c15d2017-02-28 17:52:58 -0800555 hidl_vec<hidl_string> fqInstanceNames;
556 // copying out for timeoutIPC
557 auto listRet = timeoutIPC(manager, &IServiceManager::list, [&] (const auto &names) {
558 fqInstanceNames = names;
Yifan Hongb0dde932017-02-10 17:49:58 -0800559 });
560 if (!listRet.isOk()) {
561 mErr << "Error: Failed to list services for " << mode << ": "
562 << listRet.description() << std::endl;
Steven Moreland9b5c15d2017-02-28 17:52:58 -0800563 return DUMP_BINDERIZED_ERROR;
564 }
565
566 Status status = OK;
567 // server pid, .ptr value of binder object, child pids
568 std::map<std::string, DebugInfo> allDebugInfos;
569 std::map<pid_t, std::map<uint64_t, Pids>> allPids;
570 for (const auto &fqInstanceName : fqInstanceNames) {
Yifan Hong4b865492017-02-28 19:38:24 -0800571 const auto pair = splitFirst(fqInstanceName, '/');
Steven Moreland9b5c15d2017-02-28 17:52:58 -0800572 const auto &serviceName = pair.first;
573 const auto &instanceName = pair.second;
574 auto getRet = timeoutIPC(manager, &IServiceManager::get, serviceName, instanceName);
575 if (!getRet.isOk()) {
576 mErr << "Warning: Skipping \"" << fqInstanceName << "\": "
577 << "cannot be fetched from service manager:"
578 << getRet.description() << std::endl;
579 status |= DUMP_BINDERIZED_ERROR;
580 continue;
581 }
582 sp<IBase> service = getRet;
583 if (service == nullptr) {
584 mErr << "Warning: Skipping \"" << fqInstanceName << "\": "
585 << "cannot be fetched from service manager (null)";
586 status |= DUMP_BINDERIZED_ERROR;
587 continue;
588 }
589 auto debugRet = timeoutIPC(service, &IBase::getDebugInfo, [&] (const auto &debugInfo) {
590 allDebugInfos[fqInstanceName] = debugInfo;
591 if (debugInfo.pid >= 0) {
592 allPids[static_cast<pid_t>(debugInfo.pid)].clear();
593 }
594 });
595 if (!debugRet.isOk()) {
596 mErr << "Warning: Skipping \"" << fqInstanceName << "\": "
597 << "debugging information cannot be retrieved:"
598 << debugRet.description() << std::endl;
599 status |= DUMP_BINDERIZED_ERROR;
600 }
601 }
602 for (auto &pair : allPids) {
603 pid_t serverPid = pair.first;
604 if (!getReferencedPids(serverPid, &allPids[serverPid])) {
605 mErr << "Warning: no information for PID " << serverPid
606 << ", are you root?" << std::endl;
607 status |= DUMP_BINDERIZED_ERROR;
608 }
609 }
610 for (const auto &fqInstanceName : fqInstanceNames) {
611 auto it = allDebugInfos.find(fqInstanceName);
612 if (it == allDebugInfos.end()) {
Yifan Honga3b87092017-03-02 19:19:29 -0800613 putEntry(HWSERVICEMANAGER_LIST, {
Steven Moreland9b5c15d2017-02-28 17:52:58 -0800614 .interfaceName = fqInstanceName,
615 .transport = mode,
616 .serverPid = NO_PID,
617 .serverObjectAddress = NO_PTR,
Yifan Hong4b865492017-02-28 19:38:24 -0800618 .clientPids = {},
Yifan Honga3b87092017-03-02 19:19:29 -0800619 .arch = ARCH_UNKNOWN
Steven Moreland9b5c15d2017-02-28 17:52:58 -0800620 });
621 continue;
622 }
623 const DebugInfo &info = it->second;
Yifan Honga3b87092017-03-02 19:19:29 -0800624 putEntry(HWSERVICEMANAGER_LIST, {
Steven Moreland9b5c15d2017-02-28 17:52:58 -0800625 .interfaceName = fqInstanceName,
626 .transport = mode,
627 .serverPid = info.pid,
628 .serverObjectAddress = info.ptr,
629 .clientPids = info.pid == NO_PID || info.ptr == NO_PTR
Yifan Hong4b865492017-02-28 19:38:24 -0800630 ? Pids{} : allPids[info.pid][info.ptr],
Yifan Hongb4479022017-03-02 16:54:11 -0800631 .arch = fromBaseArchitecture(info.arch),
Steven Moreland9b5c15d2017-02-28 17:52:58 -0800632 });
Yifan Hongb0dde932017-02-10 17:49:58 -0800633 }
634 return status;
635}
636
637Status Lshal::fetch() {
638 Status status = OK;
639 auto bManager = ::android::hardware::defaultServiceManager();
640 if (bManager == nullptr) {
641 mErr << "Failed to get defaultServiceManager()!" << std::endl;
642 status |= NO_BINDERIZED_MANAGER;
643 } else {
644 status |= fetchBinderized(bManager);
645 // Passthrough PIDs are registered to the binderized manager as well.
646 status |= fetchPassthrough(bManager);
647 }
648
649 auto pManager = ::android::hardware::getPassthroughServiceManager();
650 if (pManager == nullptr) {
651 mErr << "Failed to get getPassthroughServiceManager()!" << std::endl;
652 status |= NO_PASSTHROUGH_MANAGER;
653 } else {
654 status |= fetchAllLibraries(pManager);
655 }
656 return status;
657}
658
659void Lshal::usage() const {
660 mErr
661 << "usage: lshal" << std::endl
Yifan Honga3b87092017-03-02 19:19:29 -0800662 << " Dump all hals with default ordering and columns [-ipc]." << std::endl
Yifan Hongb4479022017-03-02 16:54:11 -0800663 << " lshal [--interface|-i] [--transport|-t] [-r|--arch]" << std::endl
Yifan Hong38d53e02017-02-13 17:51:59 -0800664 << " [--pid|-p] [--address|-a] [--clients|-c] [--cmdline|-m]" << std::endl
Yifan Hong4b865492017-02-28 19:38:24 -0800665 << " [--sort={interface|i|pid|p}] [--init-vintf[=path]]" << std::endl
Yifan Hong38d53e02017-02-13 17:51:59 -0800666 << " -i, --interface: print the interface name column" << std::endl
667 << " -n, --instance: print the instance name column" << std::endl
668 << " -t, --transport: print the transport mode column" << std::endl
Yifan Hongb4479022017-03-02 16:54:11 -0800669 << " -r, --arch: print if the HAL is in 64-bit or 32-bit" << std::endl
Yifan Hongae09a3d2017-02-14 17:33:50 -0800670 << " -p, --pid: print the server PID, or server cmdline if -m is set" << std::endl
Yifan Hong38d53e02017-02-13 17:51:59 -0800671 << " -a, --address: print the server object address column" << std::endl
Yifan Hongae09a3d2017-02-14 17:33:50 -0800672 << " -c, --clients: print the client PIDs, or client cmdlines if -m is set"
673 << std::endl
674 << " -m, --cmdline: print cmdline instead of PIDs" << std::endl
Yifan Hong38d53e02017-02-13 17:51:59 -0800675 << " --sort=i, --sort=interface: sort by interface name" << std::endl
676 << " --sort=p, --sort=pid: sort by server pid" << std::endl
Yifan Hong4b865492017-02-28 19:38:24 -0800677 << " --init-vintf=path: form a skeleton HAL manifest to specified file " << std::endl
678 << " (stdout if no file specified)" << std::endl
Yifan Hongb0dde932017-02-10 17:49:58 -0800679 << " lshal [-h|--help]" << std::endl
680 << " -h, --help: show this help information." << std::endl;
681}
682
683Status Lshal::parseArgs(int argc, char **argv) {
684 static struct option longOptions[] = {
Yifan Hong38d53e02017-02-13 17:51:59 -0800685 // long options with short alternatives
686 {"help", no_argument, 0, 'h' },
687 {"interface", no_argument, 0, 'i' },
688 {"transport", no_argument, 0, 't' },
Yifan Hongb4479022017-03-02 16:54:11 -0800689 {"arch", no_argument, 0, 'r' },
Yifan Hong38d53e02017-02-13 17:51:59 -0800690 {"pid", no_argument, 0, 'p' },
691 {"address", no_argument, 0, 'a' },
692 {"clients", no_argument, 0, 'c' },
Yifan Hongae09a3d2017-02-14 17:33:50 -0800693 {"cmdline", no_argument, 0, 'm' },
Andreas Huber28d35912017-03-24 13:14:11 -0700694 {"debug", optional_argument, 0, 'd' },
Yifan Hong38d53e02017-02-13 17:51:59 -0800695
696 // long options without short alternatives
697 {"sort", required_argument, 0, 's' },
Yifan Hong4b865492017-02-28 19:38:24 -0800698 {"init-vintf",optional_argument, 0, 'v' },
Yifan Hong38d53e02017-02-13 17:51:59 -0800699 { 0, 0, 0, 0 }
Yifan Hongb0dde932017-02-10 17:49:58 -0800700 };
701
702 int optionIndex;
703 int c;
704 optind = 1;
705 for (;;) {
706 // using getopt_long in case we want to add other options in the future
Andreas Huber28d35912017-03-24 13:14:11 -0700707 c = getopt_long(argc, argv, "hitrpacmd", longOptions, &optionIndex);
Yifan Hongb0dde932017-02-10 17:49:58 -0800708 if (c == -1) {
709 break;
710 }
711 switch (c) {
Yifan Hong38d53e02017-02-13 17:51:59 -0800712 case 's': {
713 if (strcmp(optarg, "interface") == 0 || strcmp(optarg, "i") == 0) {
714 mSortColumn = TableEntry::sortByInterfaceName;
715 } else if (strcmp(optarg, "pid") == 0 || strcmp(optarg, "p") == 0) {
716 mSortColumn = TableEntry::sortByServerPid;
717 } else {
718 mErr << "Unrecognized sorting column: " << optarg << std::endl;
719 usage();
720 return USAGE;
721 }
722 break;
723 }
Yifan Hong4b865492017-02-28 19:38:24 -0800724 case 'v': {
725 if (optarg) {
726 mFileOutput = new std::ofstream{optarg};
727 mOut = mFileOutput;
728 if (!mFileOutput.buf().is_open()) {
729 mErr << "Could not open file '" << optarg << "'." << std::endl;
730 return IO_ERROR;
731 }
732 }
733 mVintf = true;
734 }
Yifan Hong38d53e02017-02-13 17:51:59 -0800735 case 'i': {
736 mSelectedColumns |= ENABLE_INTERFACE_NAME;
737 break;
738 }
739 case 't': {
740 mSelectedColumns |= ENABLE_TRANSPORT;
741 break;
742 }
Yifan Hongb4479022017-03-02 16:54:11 -0800743 case 'r': {
744 mSelectedColumns |= ENABLE_ARCH;
745 break;
746 }
Yifan Hong38d53e02017-02-13 17:51:59 -0800747 case 'p': {
748 mSelectedColumns |= ENABLE_SERVER_PID;
749 break;
750 }
751 case 'a': {
752 mSelectedColumns |= ENABLE_SERVER_ADDR;
753 break;
754 }
755 case 'c': {
756 mSelectedColumns |= ENABLE_CLIENT_PIDS;
757 break;
758 }
Yifan Hongae09a3d2017-02-14 17:33:50 -0800759 case 'm': {
760 mEnableCmdlines = true;
761 break;
762 }
Andreas Huber28d35912017-03-24 13:14:11 -0700763 case 'd': {
764 mEmitDebugInfo = true;
765
766 if (optarg) {
767 mFileOutput = new std::ofstream{optarg};
768 mOut = mFileOutput;
769 if (!mFileOutput.buf().is_open()) {
770 mErr << "Could not open file '" << optarg << "'." << std::endl;
771 return IO_ERROR;
772 }
773 chown(optarg, AID_SHELL, AID_SHELL);
774 }
775 break;
776 }
Yifan Hongb0dde932017-02-10 17:49:58 -0800777 case 'h': // falls through
778 default: // see unrecognized options
779 usage();
780 return USAGE;
781 }
782 }
Yifan Hong38d53e02017-02-13 17:51:59 -0800783
784 if (mSelectedColumns == 0) {
Yifan Honga3b87092017-03-02 19:19:29 -0800785 mSelectedColumns = ENABLE_INTERFACE_NAME | ENABLE_SERVER_PID | ENABLE_CLIENT_PIDS;
Yifan Hong38d53e02017-02-13 17:51:59 -0800786 }
Yifan Hongb0dde932017-02-10 17:49:58 -0800787 return OK;
788}
789
790int Lshal::main(int argc, char **argv) {
791 Status status = parseArgs(argc, argv);
792 if (status != OK) {
793 return status;
794 }
795 status = fetch();
Yifan Hong38d53e02017-02-13 17:51:59 -0800796 postprocess();
Yifan Hongb0dde932017-02-10 17:49:58 -0800797 dump();
798 return status;
799}
800
Yifan Honga57dffb2017-02-21 14:59:00 -0800801void signalHandler(int sig) {
802 if (sig == SIGINT) {
803 int retVal;
804 pthread_exit(&retVal);
805 }
806}
807
Yifan Hongb0dde932017-02-10 17:49:58 -0800808} // namespace lshal
809} // namespace android
810
811int main(int argc, char **argv) {
Yifan Honga57dffb2017-02-21 14:59:00 -0800812 signal(SIGINT, ::android::lshal::signalHandler);
Yifan Hongb0dde932017-02-10 17:49:58 -0800813 return ::android::lshal::Lshal{}.main(argc, argv);
814}