blob: 87501472e26bc536c3187a7483a6f77532fa328e [file] [log] [blame]
Yifan Hongb0dde932017-02-10 17:49:58 -08001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "Lshal.h"
18
19#include <getopt.h>
20
21#include <fstream>
22#include <iomanip>
23#include <iostream>
24#include <map>
25#include <sstream>
26#include <regex>
27
28#include <android-base/parseint.h>
29#include <android/hidl/manager/1.0/IServiceManager.h>
30#include <hidl/ServiceManagement.h>
Yifan Hong4b865492017-02-28 19:38:24 -080031#include <hidl-util/FQName.h>
32#include <vintf/HalManifest.h>
33#include <vintf/parse_xml.h>
Yifan Hongb0dde932017-02-10 17:49:58 -080034
Yifan Honge2dadf02017-02-14 15:43:31 -080035#include "Timeout.h"
36
Yifan Hongb0dde932017-02-10 17:49:58 -080037using ::android::hardware::hidl_string;
38using ::android::hidl::manager::V1_0::IServiceManager;
39
40namespace android {
41namespace lshal {
42
Yifan Hongb0dde932017-02-10 17:49:58 -080043template <typename A>
44std::string join(const A &components, const std::string &separator) {
45 std::stringstream out;
46 bool first = true;
47 for (const auto &component : components) {
48 if (!first) {
49 out << separator;
50 }
51 out << component;
52
53 first = false;
54 }
55 return out.str();
56}
57
58static std::string toHexString(uint64_t t) {
59 std::ostringstream os;
60 os << std::hex << std::setfill('0') << std::setw(16) << t;
61 return os.str();
62}
63
Yifan Hong4b865492017-02-28 19:38:24 -080064template<typename String>
65static std::pair<String, String> splitFirst(const String &s, char c) {
Yifan Hongb0dde932017-02-10 17:49:58 -080066 const char *pos = strchr(s.c_str(), c);
67 if (pos == nullptr) {
68 return {s, {}};
69 }
Yifan Hong4b865492017-02-28 19:38:24 -080070 return {String(s.c_str(), pos - s.c_str()), String(pos + 1)};
Yifan Hongb0dde932017-02-10 17:49:58 -080071}
72
73static std::vector<std::string> split(const std::string &s, char c) {
74 std::vector<std::string> components{};
75 size_t startPos = 0;
76 size_t matchPos;
77 while ((matchPos = s.find(c, startPos)) != std::string::npos) {
78 components.push_back(s.substr(startPos, matchPos - startPos));
79 startPos = matchPos + 1;
80 }
81
82 if (startPos <= s.length()) {
83 components.push_back(s.substr(startPos));
84 }
85 return components;
86}
87
Yifan Hong4b865492017-02-28 19:38:24 -080088static void replaceAll(std::string *s, char from, char to) {
89 for (size_t i = 0; i < s->size(); ++i) {
90 if (s->at(i) == from) {
91 s->at(i) = to;
92 }
93 }
94}
95
Yifan Hongae09a3d2017-02-14 17:33:50 -080096std::string getCmdline(pid_t pid) {
97 std::ifstream ifs("/proc/" + std::to_string(pid) + "/cmdline");
98 std::string cmdline;
99 if (!ifs.is_open()) {
100 return "";
101 }
102 ifs >> cmdline;
103 return cmdline;
104}
105
106const std::string &Lshal::getCmdline(pid_t pid) {
107 auto pair = mCmdlines.find(pid);
108 if (pair != mCmdlines.end()) {
109 return pair->second;
110 }
111 mCmdlines[pid] = ::android::lshal::getCmdline(pid);
112 return mCmdlines[pid];
113}
114
115void Lshal::removeDeadProcesses(Pids *pids) {
116 static const pid_t myPid = getpid();
117 std::remove_if(pids->begin(), pids->end(), [this](auto pid) {
118 return pid == myPid || this->getCmdline(pid).empty();
119 });
120}
121
Yifan Hongb0dde932017-02-10 17:49:58 -0800122bool Lshal::getReferencedPids(
123 pid_t serverPid, std::map<uint64_t, Pids> *objects) const {
124
125 std::ifstream ifs("/d/binder/proc/" + std::to_string(serverPid));
126 if (!ifs.is_open()) {
127 return false;
128 }
129
130 static const std::regex prefix("^\\s*node \\d+:\\s+u([0-9a-f]+)\\s+c([0-9a-f]+)\\s+");
131
132 std::string line;
133 std::smatch match;
134 while(getline(ifs, line)) {
135 if (!std::regex_search(line, match, prefix)) {
136 // the line doesn't start with the correct prefix
137 continue;
138 }
139 std::string ptrString = "0x" + match.str(2); // use number after c
140 uint64_t ptr;
141 if (!::android::base::ParseUint(ptrString.c_str(), &ptr)) {
142 // Should not reach here, but just be tolerant.
143 mErr << "Could not parse number " << ptrString << std::endl;
144 continue;
145 }
146 const std::string proc = " proc ";
147 auto pos = line.rfind(proc);
148 if (pos != std::string::npos) {
149 for (const std::string &pidStr : split(line.substr(pos + proc.size()), ' ')) {
150 int32_t pid;
151 if (!::android::base::ParseInt(pidStr, &pid)) {
152 mErr << "Could not parse number " << pidStr << std::endl;
153 continue;
154 }
155 (*objects)[ptr].push_back(pid);
156 }
157 }
158 }
159 return true;
160}
161
Yifan Honga3b87092017-03-02 19:19:29 -0800162void Lshal::forEachTable(const std::function<void(Table &)> &f) {
Yifan Hong8ab3bee2017-03-08 14:01:11 -0800163 f(mServicesTable);
164 f(mPassthroughRefTable);
165 f(mImplementationsTable);
Yifan Honga3b87092017-03-02 19:19:29 -0800166}
167void Lshal::forEachTable(const std::function<void(const Table &)> &f) const {
Yifan Hong8ab3bee2017-03-08 14:01:11 -0800168 f(mServicesTable);
169 f(mPassthroughRefTable);
170 f(mImplementationsTable);
Yifan Honga3b87092017-03-02 19:19:29 -0800171}
172
Yifan Hong38d53e02017-02-13 17:51:59 -0800173void Lshal::postprocess() {
Yifan Honga3b87092017-03-02 19:19:29 -0800174 forEachTable([this](Table &table) {
175 if (mSortColumn) {
176 std::sort(table.begin(), table.end(), mSortColumn);
Yifan Hongae09a3d2017-02-14 17:33:50 -0800177 }
Yifan Honga3b87092017-03-02 19:19:29 -0800178 for (TableEntry &entry : table) {
179 entry.serverCmdline = getCmdline(entry.serverPid);
180 removeDeadProcesses(&entry.clientPids);
181 for (auto pid : entry.clientPids) {
182 entry.clientCmdlines.push_back(this->getCmdline(pid));
183 }
184 }
185 });
Yifan Hong3fdbd9f2017-03-08 14:01:58 -0800186 // use a double for loop here because lshal doesn't care about efficiency.
187 for (TableEntry &packageEntry : mImplementationsTable) {
188 std::string packageName = packageEntry.interfaceName;
189 FQName fqPackageName{packageName.substr(0, packageName.find("::"))};
190 if (!fqPackageName.isValid()) {
191 continue;
192 }
193 for (TableEntry &interfaceEntry : mPassthroughRefTable) {
194 if (interfaceEntry.arch != ARCH_UNKNOWN) {
195 continue;
196 }
197 FQName interfaceName{splitFirst(interfaceEntry.interfaceName, '/').first};
198 if (!interfaceName.isValid()) {
199 continue;
200 }
201 if (interfaceName.getPackageAndVersion() == fqPackageName) {
202 interfaceEntry.arch = packageEntry.arch;
203 }
204 }
205 }
Yifan Hong38d53e02017-02-13 17:51:59 -0800206}
207
208void Lshal::printLine(
209 const std::string &interfaceName,
Yifan Hongb4479022017-03-02 16:54:11 -0800210 const std::string &transport,
211 const std::string &arch,
212 const std::string &server,
Yifan Hongae09a3d2017-02-14 17:33:50 -0800213 const std::string &serverCmdline,
214 const std::string &address, const std::string &clients,
215 const std::string &clientCmdlines) const {
Yifan Hong38d53e02017-02-13 17:51:59 -0800216 if (mSelectedColumns & ENABLE_INTERFACE_NAME)
217 mOut << std::setw(80) << interfaceName << "\t";
218 if (mSelectedColumns & ENABLE_TRANSPORT)
219 mOut << std::setw(10) << transport << "\t";
Yifan Hongb4479022017-03-02 16:54:11 -0800220 if (mSelectedColumns & ENABLE_ARCH)
221 mOut << std::setw(5) << arch << "\t";
Yifan Hongae09a3d2017-02-14 17:33:50 -0800222 if (mSelectedColumns & ENABLE_SERVER_PID) {
223 if (mEnableCmdlines) {
224 mOut << std::setw(15) << serverCmdline << "\t";
225 } else {
226 mOut << std::setw(5) << server << "\t";
227 }
228 }
Yifan Hong38d53e02017-02-13 17:51:59 -0800229 if (mSelectedColumns & ENABLE_SERVER_ADDR)
230 mOut << std::setw(16) << address << "\t";
Yifan Hongae09a3d2017-02-14 17:33:50 -0800231 if (mSelectedColumns & ENABLE_CLIENT_PIDS) {
232 if (mEnableCmdlines) {
233 mOut << std::setw(0) << clientCmdlines;
234 } else {
235 mOut << std::setw(0) << clients;
236 }
237 }
Yifan Hong38d53e02017-02-13 17:51:59 -0800238 mOut << std::endl;
239}
240
Yifan Hong4b865492017-02-28 19:38:24 -0800241void Lshal::dumpVintf() const {
242 vintf::HalManifest manifest;
Yifan Honga3b87092017-03-02 19:19:29 -0800243 forEachTable([this, &manifest] (const Table &table) {
244 for (const TableEntry &entry : table) {
Yifan Hong4b865492017-02-28 19:38:24 -0800245
Yifan Honga3b87092017-03-02 19:19:29 -0800246 std::string fqInstanceName = entry.interfaceName;
Yifan Hong4b865492017-02-28 19:38:24 -0800247
Yifan Honga3b87092017-03-02 19:19:29 -0800248 if (&table == &mImplementationsTable) {
249 // Quick hack to work around *'s
250 replaceAll(&fqInstanceName, '*', 'D');
251 }
252 auto splittedFqInstanceName = splitFirst(fqInstanceName, '/');
253 FQName fqName(splittedFqInstanceName.first);
254 if (!fqName.isValid()) {
255 mErr << "Warning: '" << splittedFqInstanceName.first
256 << "' is not a valid FQName." << std::endl;
Yifan Hong4b865492017-02-28 19:38:24 -0800257 continue;
258 }
Yifan Honga3b87092017-03-02 19:19:29 -0800259 // Strip out system libs.
260 // TODO(b/34772739): might want to add other framework HAL packages
261 if (fqName.inPackage("android.hidl")) {
262 continue;
263 }
264 std::string interfaceName =
265 &table == &mImplementationsTable ? "" : fqName.name();
266 std::string instanceName =
267 &table == &mImplementationsTable ? "" : splittedFqInstanceName.second;
268
269 vintf::Transport transport;
Yifan Hong3fdbd9f2017-03-08 14:01:58 -0800270 vintf::Arch arch;
Yifan Honga3b87092017-03-02 19:19:29 -0800271 if (entry.transport == "hwbinder") {
272 transport = vintf::Transport::HWBINDER;
Yifan Hong3fdbd9f2017-03-08 14:01:58 -0800273 arch = vintf::Arch::ARCH_EMPTY;
Yifan Honga3b87092017-03-02 19:19:29 -0800274 } else if (entry.transport == "passthrough") {
275 transport = vintf::Transport::PASSTHROUGH;
Yifan Hong3fdbd9f2017-03-08 14:01:58 -0800276 switch (entry.arch) {
277 case lshal::ARCH32:
278 arch = vintf::Arch::ARCH_32; break;
279 case lshal::ARCH64:
280 arch = vintf::Arch::ARCH_64; break;
281 case lshal::ARCH_BOTH:
282 arch = vintf::Arch::ARCH_32_64; break;
283 case lshal::ARCH_UNKNOWN: // fallthrough
284 default:
285 mErr << "Warning: '" << fqName.package()
286 << "' doesn't have bitness info, assuming 32+64." << std::endl;
287 arch = vintf::Arch::ARCH_32_64;
288 }
Yifan Hong4b865492017-02-28 19:38:24 -0800289 } else {
Yifan Honga3b87092017-03-02 19:19:29 -0800290 mErr << "Warning: '" << entry.transport << "' is not a valid transport." << std::endl;
291 continue;
292 }
293
294 vintf::ManifestHal *hal = manifest.getHal(fqName.package());
295 if (hal == nullptr) {
296 if (!manifest.add(vintf::ManifestHal{
297 .format = vintf::HalFormat::HIDL,
298 .name = fqName.package(),
299 .impl = {.implLevel = vintf::ImplLevel::GENERIC, .impl = ""},
Yifan Hong3fdbd9f2017-03-08 14:01:58 -0800300 .transportArch = {transport, arch}
Yifan Honga3b87092017-03-02 19:19:29 -0800301 })) {
302 mErr << "Warning: cannot add hal '" << fqInstanceName << "'" << std::endl;
303 continue;
304 }
305 hal = manifest.getHal(fqName.package());
306 }
307 if (hal == nullptr) {
308 mErr << "Warning: cannot get hal '" << fqInstanceName
309 << "' after adding it" << std::endl;
310 continue;
311 }
312 vintf::Version version{fqName.getPackageMajorVersion(), fqName.getPackageMinorVersion()};
313 if (std::find(hal->versions.begin(), hal->versions.end(), version) == hal->versions.end()) {
314 hal->versions.push_back(version);
315 }
316 if (&table != &mImplementationsTable) {
317 auto it = hal->interfaces.find(interfaceName);
318 if (it == hal->interfaces.end()) {
319 hal->interfaces.insert({interfaceName, {interfaceName, {{instanceName}}}});
320 } else {
321 it->second.instances.insert(instanceName);
322 }
Yifan Hong4b865492017-02-28 19:38:24 -0800323 }
324 }
Yifan Honga3b87092017-03-02 19:19:29 -0800325 });
Yifan Hong4b865492017-02-28 19:38:24 -0800326 mOut << vintf::gHalManifestConverter(manifest);
327}
328
Yifan Hongb4479022017-03-02 16:54:11 -0800329static const std::string &getArchString(Architecture arch) {
330 static const std::string sStr64 = "64";
331 static const std::string sStr32 = "32";
Yifan Hong1071c1b2017-03-03 10:57:43 -0800332 static const std::string sStrBoth = "32+64";
Yifan Hongb4479022017-03-02 16:54:11 -0800333 static const std::string sStrUnknown = "";
334 switch (arch) {
335 case ARCH64:
336 return sStr64;
337 case ARCH32:
338 return sStr32;
339 case ARCH_BOTH:
340 return sStrBoth;
341 case ARCH_UNKNOWN: // fall through
342 default:
343 return sStrUnknown;
344 }
345}
346
347static Architecture fromBaseArchitecture(::android::hidl::base::V1_0::DebugInfo::Architecture a) {
348 switch (a) {
349 case ::android::hidl::base::V1_0::DebugInfo::Architecture::IS_64BIT:
350 return ARCH64;
351 case ::android::hidl::base::V1_0::DebugInfo::Architecture::IS_32BIT:
352 return ARCH32;
353 case ::android::hidl::base::V1_0::DebugInfo::Architecture::UNKNOWN: // fallthrough
354 default:
355 return ARCH_UNKNOWN;
356 }
357}
358
Yifan Honga3b87092017-03-02 19:19:29 -0800359void Lshal::dumpTable() {
360 mServicesTable.description =
361 "All binderized services (registered services through hwservicemanager)";
362 mPassthroughRefTable.description =
Yifan Hong1071c1b2017-03-03 10:57:43 -0800363 "All interfaces that getService() has ever return as a passthrough interface;\n"
Yifan Honga3b87092017-03-02 19:19:29 -0800364 "PIDs / processes shown below might be inaccurate because the process\n"
Yifan Hong1071c1b2017-03-03 10:57:43 -0800365 "might have relinquished the interface or might have died.\n"
Yifan Honga3b87092017-03-02 19:19:29 -0800366 "The Server / Server CMD column can be ignored.\n"
Yifan Hong1071c1b2017-03-03 10:57:43 -0800367 "The Clients / Clients CMD column shows all process that have ever dlopen'ed \n"
368 "the library and successfully fetched the passthrough implementation.";
Yifan Honga3b87092017-03-02 19:19:29 -0800369 mImplementationsTable.description =
370 "All available passthrough implementations (all -impl.so files)";
371 forEachTable([this] (const Table &table) {
372 mOut << table.description << std::endl;
373 mOut << std::left;
374 printLine("Interface", "Transport", "Arch", "Server", "Server CMD",
375 "PTR", "Clients", "Clients CMD");
376 for (const auto &entry : table) {
377 printLine(entry.interfaceName,
378 entry.transport,
379 getArchString(entry.arch),
380 entry.serverPid == NO_PID ? "N/A" : std::to_string(entry.serverPid),
381 entry.serverCmdline,
382 entry.serverObjectAddress == NO_PTR ? "N/A" : toHexString(entry.serverObjectAddress),
383 join(entry.clientPids, " "),
384 join(entry.clientCmdlines, ";"));
385 }
386 mOut << std::endl;
387 });
388
Yifan Hongb0dde932017-02-10 17:49:58 -0800389}
390
Yifan Hong4b865492017-02-28 19:38:24 -0800391void Lshal::dump() {
392 if (mVintf) {
393 dumpVintf();
394 if (!!mFileOutput) {
395 mFileOutput.buf().close();
396 delete &mFileOutput.buf();
397 mFileOutput = nullptr;
398 }
399 mOut = std::cout;
400 } else {
401 dumpTable();
402 }
403}
404
Yifan Honga3b87092017-03-02 19:19:29 -0800405void Lshal::putEntry(TableEntrySource source, TableEntry &&entry) {
406 Table *table = nullptr;
407 switch (source) {
408 case HWSERVICEMANAGER_LIST :
409 table = &mServicesTable; break;
410 case PTSERVICEMANAGER_REG_CLIENT :
411 table = &mPassthroughRefTable; break;
412 case LIST_DLLIB :
413 table = &mImplementationsTable; break;
414 default:
415 mErr << "Error: Unknown source of entry " << source << std::endl;
416 }
417 if (table) {
418 table->entries.push_back(std::forward<TableEntry>(entry));
419 }
Yifan Hongb0dde932017-02-10 17:49:58 -0800420}
421
422Status Lshal::fetchAllLibraries(const sp<IServiceManager> &manager) {
423 using namespace ::android::hardware;
424 using namespace ::android::hidl::manager::V1_0;
425 using namespace ::android::hidl::base::V1_0;
Yifan Hongb4479022017-03-02 16:54:11 -0800426 auto ret = timeoutIPC(manager, &IServiceManager::debugDump, [&] (const auto &infos) {
427 std::map<std::string, TableEntry> entries;
428 for (const auto &info : infos) {
429 std::string interfaceName = std::string{info.interfaceName.c_str()} + "/" +
430 std::string{info.instanceName.c_str()};
Yifan Hong1071c1b2017-03-03 10:57:43 -0800431 entries.emplace(interfaceName, TableEntry{
Yifan Hongb4479022017-03-02 16:54:11 -0800432 .interfaceName = interfaceName,
Yifan Hongb0dde932017-02-10 17:49:58 -0800433 .transport = "passthrough",
434 .serverPid = NO_PID,
435 .serverObjectAddress = NO_PTR,
Yifan Hong4b865492017-02-28 19:38:24 -0800436 .clientPids = {},
Yifan Honga3b87092017-03-02 19:19:29 -0800437 .arch = ARCH_UNKNOWN
Yifan Hongb4479022017-03-02 16:54:11 -0800438 }).first->second.arch |= fromBaseArchitecture(info.arch);
439 }
440 for (auto &&pair : entries) {
Yifan Honga3b87092017-03-02 19:19:29 -0800441 putEntry(LIST_DLLIB, std::move(pair.second));
Yifan Hongb0dde932017-02-10 17:49:58 -0800442 }
443 });
444 if (!ret.isOk()) {
445 mErr << "Error: Failed to call list on getPassthroughServiceManager(): "
446 << ret.description() << std::endl;
447 return DUMP_ALL_LIBS_ERROR;
448 }
449 return OK;
450}
451
452Status Lshal::fetchPassthrough(const sp<IServiceManager> &manager) {
453 using namespace ::android::hardware;
Yifan Hongb4479022017-03-02 16:54:11 -0800454 using namespace ::android::hardware::details;
Yifan Hongb0dde932017-02-10 17:49:58 -0800455 using namespace ::android::hidl::manager::V1_0;
456 using namespace ::android::hidl::base::V1_0;
Yifan Honge2dadf02017-02-14 15:43:31 -0800457 auto ret = timeoutIPC(manager, &IServiceManager::debugDump, [&] (const auto &infos) {
Yifan Hongb0dde932017-02-10 17:49:58 -0800458 for (const auto &info : infos) {
Yifan Honga3b87092017-03-02 19:19:29 -0800459 putEntry(PTSERVICEMANAGER_REG_CLIENT, {
Yifan Hongb0dde932017-02-10 17:49:58 -0800460 .interfaceName =
461 std::string{info.interfaceName.c_str()} + "/" +
462 std::string{info.instanceName.c_str()},
463 .transport = "passthrough",
464 .serverPid = info.clientPids.size() == 1 ? info.clientPids[0] : NO_PID,
465 .serverObjectAddress = NO_PTR,
Yifan Hong4b865492017-02-28 19:38:24 -0800466 .clientPids = info.clientPids,
Yifan Honga3b87092017-03-02 19:19:29 -0800467 .arch = fromBaseArchitecture(info.arch)
Yifan Hongb0dde932017-02-10 17:49:58 -0800468 });
469 }
470 });
471 if (!ret.isOk()) {
472 mErr << "Error: Failed to call debugDump on defaultServiceManager(): "
473 << ret.description() << std::endl;
474 return DUMP_PASSTHROUGH_ERROR;
475 }
476 return OK;
477}
478
479Status Lshal::fetchBinderized(const sp<IServiceManager> &manager) {
480 using namespace ::std;
481 using namespace ::android::hardware;
482 using namespace ::android::hidl::manager::V1_0;
483 using namespace ::android::hidl::base::V1_0;
484 const std::string mode = "hwbinder";
Yifan Hongb0dde932017-02-10 17:49:58 -0800485
Steven Moreland9b5c15d2017-02-28 17:52:58 -0800486 hidl_vec<hidl_string> fqInstanceNames;
487 // copying out for timeoutIPC
488 auto listRet = timeoutIPC(manager, &IServiceManager::list, [&] (const auto &names) {
489 fqInstanceNames = names;
Yifan Hongb0dde932017-02-10 17:49:58 -0800490 });
491 if (!listRet.isOk()) {
492 mErr << "Error: Failed to list services for " << mode << ": "
493 << listRet.description() << std::endl;
Steven Moreland9b5c15d2017-02-28 17:52:58 -0800494 return DUMP_BINDERIZED_ERROR;
495 }
496
497 Status status = OK;
498 // server pid, .ptr value of binder object, child pids
499 std::map<std::string, DebugInfo> allDebugInfos;
500 std::map<pid_t, std::map<uint64_t, Pids>> allPids;
501 for (const auto &fqInstanceName : fqInstanceNames) {
Yifan Hong4b865492017-02-28 19:38:24 -0800502 const auto pair = splitFirst(fqInstanceName, '/');
Steven Moreland9b5c15d2017-02-28 17:52:58 -0800503 const auto &serviceName = pair.first;
504 const auto &instanceName = pair.second;
505 auto getRet = timeoutIPC(manager, &IServiceManager::get, serviceName, instanceName);
506 if (!getRet.isOk()) {
507 mErr << "Warning: Skipping \"" << fqInstanceName << "\": "
508 << "cannot be fetched from service manager:"
509 << getRet.description() << std::endl;
510 status |= DUMP_BINDERIZED_ERROR;
511 continue;
512 }
513 sp<IBase> service = getRet;
514 if (service == nullptr) {
515 mErr << "Warning: Skipping \"" << fqInstanceName << "\": "
516 << "cannot be fetched from service manager (null)";
517 status |= DUMP_BINDERIZED_ERROR;
518 continue;
519 }
520 auto debugRet = timeoutIPC(service, &IBase::getDebugInfo, [&] (const auto &debugInfo) {
521 allDebugInfos[fqInstanceName] = debugInfo;
522 if (debugInfo.pid >= 0) {
523 allPids[static_cast<pid_t>(debugInfo.pid)].clear();
524 }
525 });
526 if (!debugRet.isOk()) {
527 mErr << "Warning: Skipping \"" << fqInstanceName << "\": "
528 << "debugging information cannot be retrieved:"
529 << debugRet.description() << std::endl;
530 status |= DUMP_BINDERIZED_ERROR;
531 }
532 }
533 for (auto &pair : allPids) {
534 pid_t serverPid = pair.first;
535 if (!getReferencedPids(serverPid, &allPids[serverPid])) {
536 mErr << "Warning: no information for PID " << serverPid
537 << ", are you root?" << std::endl;
538 status |= DUMP_BINDERIZED_ERROR;
539 }
540 }
541 for (const auto &fqInstanceName : fqInstanceNames) {
542 auto it = allDebugInfos.find(fqInstanceName);
543 if (it == allDebugInfos.end()) {
Yifan Honga3b87092017-03-02 19:19:29 -0800544 putEntry(HWSERVICEMANAGER_LIST, {
Steven Moreland9b5c15d2017-02-28 17:52:58 -0800545 .interfaceName = fqInstanceName,
546 .transport = mode,
547 .serverPid = NO_PID,
548 .serverObjectAddress = NO_PTR,
Yifan Hong4b865492017-02-28 19:38:24 -0800549 .clientPids = {},
Yifan Honga3b87092017-03-02 19:19:29 -0800550 .arch = ARCH_UNKNOWN
Steven Moreland9b5c15d2017-02-28 17:52:58 -0800551 });
552 continue;
553 }
554 const DebugInfo &info = it->second;
Yifan Honga3b87092017-03-02 19:19:29 -0800555 putEntry(HWSERVICEMANAGER_LIST, {
Steven Moreland9b5c15d2017-02-28 17:52:58 -0800556 .interfaceName = fqInstanceName,
557 .transport = mode,
558 .serverPid = info.pid,
559 .serverObjectAddress = info.ptr,
560 .clientPids = info.pid == NO_PID || info.ptr == NO_PTR
Yifan Hong4b865492017-02-28 19:38:24 -0800561 ? Pids{} : allPids[info.pid][info.ptr],
Yifan Hongb4479022017-03-02 16:54:11 -0800562 .arch = fromBaseArchitecture(info.arch),
Steven Moreland9b5c15d2017-02-28 17:52:58 -0800563 });
Yifan Hongb0dde932017-02-10 17:49:58 -0800564 }
565 return status;
566}
567
568Status Lshal::fetch() {
569 Status status = OK;
570 auto bManager = ::android::hardware::defaultServiceManager();
571 if (bManager == nullptr) {
572 mErr << "Failed to get defaultServiceManager()!" << std::endl;
573 status |= NO_BINDERIZED_MANAGER;
574 } else {
575 status |= fetchBinderized(bManager);
576 // Passthrough PIDs are registered to the binderized manager as well.
577 status |= fetchPassthrough(bManager);
578 }
579
580 auto pManager = ::android::hardware::getPassthroughServiceManager();
581 if (pManager == nullptr) {
582 mErr << "Failed to get getPassthroughServiceManager()!" << std::endl;
583 status |= NO_PASSTHROUGH_MANAGER;
584 } else {
585 status |= fetchAllLibraries(pManager);
586 }
587 return status;
588}
589
590void Lshal::usage() const {
591 mErr
592 << "usage: lshal" << std::endl
Yifan Honga3b87092017-03-02 19:19:29 -0800593 << " Dump all hals with default ordering and columns [-ipc]." << std::endl
Yifan Hongb4479022017-03-02 16:54:11 -0800594 << " lshal [--interface|-i] [--transport|-t] [-r|--arch]" << std::endl
Yifan Hong38d53e02017-02-13 17:51:59 -0800595 << " [--pid|-p] [--address|-a] [--clients|-c] [--cmdline|-m]" << std::endl
Yifan Hong4b865492017-02-28 19:38:24 -0800596 << " [--sort={interface|i|pid|p}] [--init-vintf[=path]]" << std::endl
Yifan Hong38d53e02017-02-13 17:51:59 -0800597 << " -i, --interface: print the interface name column" << std::endl
598 << " -n, --instance: print the instance name column" << std::endl
599 << " -t, --transport: print the transport mode column" << std::endl
Yifan Hongb4479022017-03-02 16:54:11 -0800600 << " -r, --arch: print if the HAL is in 64-bit or 32-bit" << std::endl
Yifan Hongae09a3d2017-02-14 17:33:50 -0800601 << " -p, --pid: print the server PID, or server cmdline if -m is set" << std::endl
Yifan Hong38d53e02017-02-13 17:51:59 -0800602 << " -a, --address: print the server object address column" << std::endl
Yifan Hongae09a3d2017-02-14 17:33:50 -0800603 << " -c, --clients: print the client PIDs, or client cmdlines if -m is set"
604 << std::endl
605 << " -m, --cmdline: print cmdline instead of PIDs" << std::endl
Yifan Hong38d53e02017-02-13 17:51:59 -0800606 << " --sort=i, --sort=interface: sort by interface name" << std::endl
607 << " --sort=p, --sort=pid: sort by server pid" << std::endl
Yifan Hong4b865492017-02-28 19:38:24 -0800608 << " --init-vintf=path: form a skeleton HAL manifest to specified file " << std::endl
609 << " (stdout if no file specified)" << std::endl
Yifan Hongb0dde932017-02-10 17:49:58 -0800610 << " lshal [-h|--help]" << std::endl
611 << " -h, --help: show this help information." << std::endl;
612}
613
614Status Lshal::parseArgs(int argc, char **argv) {
615 static struct option longOptions[] = {
Yifan Hong38d53e02017-02-13 17:51:59 -0800616 // long options with short alternatives
617 {"help", no_argument, 0, 'h' },
618 {"interface", no_argument, 0, 'i' },
619 {"transport", no_argument, 0, 't' },
Yifan Hongb4479022017-03-02 16:54:11 -0800620 {"arch", no_argument, 0, 'r' },
Yifan Hong38d53e02017-02-13 17:51:59 -0800621 {"pid", no_argument, 0, 'p' },
622 {"address", no_argument, 0, 'a' },
623 {"clients", no_argument, 0, 'c' },
Yifan Hongae09a3d2017-02-14 17:33:50 -0800624 {"cmdline", no_argument, 0, 'm' },
Yifan Hong38d53e02017-02-13 17:51:59 -0800625
626 // long options without short alternatives
627 {"sort", required_argument, 0, 's' },
Yifan Hong4b865492017-02-28 19:38:24 -0800628 {"init-vintf",optional_argument, 0, 'v' },
Yifan Hong38d53e02017-02-13 17:51:59 -0800629 { 0, 0, 0, 0 }
Yifan Hongb0dde932017-02-10 17:49:58 -0800630 };
631
632 int optionIndex;
633 int c;
634 optind = 1;
635 for (;;) {
636 // using getopt_long in case we want to add other options in the future
Yifan Hongb4479022017-03-02 16:54:11 -0800637 c = getopt_long(argc, argv, "hitrpacm", longOptions, &optionIndex);
Yifan Hongb0dde932017-02-10 17:49:58 -0800638 if (c == -1) {
639 break;
640 }
641 switch (c) {
Yifan Hong38d53e02017-02-13 17:51:59 -0800642 case 's': {
643 if (strcmp(optarg, "interface") == 0 || strcmp(optarg, "i") == 0) {
644 mSortColumn = TableEntry::sortByInterfaceName;
645 } else if (strcmp(optarg, "pid") == 0 || strcmp(optarg, "p") == 0) {
646 mSortColumn = TableEntry::sortByServerPid;
647 } else {
648 mErr << "Unrecognized sorting column: " << optarg << std::endl;
649 usage();
650 return USAGE;
651 }
652 break;
653 }
Yifan Hong4b865492017-02-28 19:38:24 -0800654 case 'v': {
655 if (optarg) {
656 mFileOutput = new std::ofstream{optarg};
657 mOut = mFileOutput;
658 if (!mFileOutput.buf().is_open()) {
659 mErr << "Could not open file '" << optarg << "'." << std::endl;
660 return IO_ERROR;
661 }
662 }
663 mVintf = true;
664 }
Yifan Hong38d53e02017-02-13 17:51:59 -0800665 case 'i': {
666 mSelectedColumns |= ENABLE_INTERFACE_NAME;
667 break;
668 }
669 case 't': {
670 mSelectedColumns |= ENABLE_TRANSPORT;
671 break;
672 }
Yifan Hongb4479022017-03-02 16:54:11 -0800673 case 'r': {
674 mSelectedColumns |= ENABLE_ARCH;
675 break;
676 }
Yifan Hong38d53e02017-02-13 17:51:59 -0800677 case 'p': {
678 mSelectedColumns |= ENABLE_SERVER_PID;
679 break;
680 }
681 case 'a': {
682 mSelectedColumns |= ENABLE_SERVER_ADDR;
683 break;
684 }
685 case 'c': {
686 mSelectedColumns |= ENABLE_CLIENT_PIDS;
687 break;
688 }
Yifan Hongae09a3d2017-02-14 17:33:50 -0800689 case 'm': {
690 mEnableCmdlines = true;
691 break;
692 }
Yifan Hongb0dde932017-02-10 17:49:58 -0800693 case 'h': // falls through
694 default: // see unrecognized options
695 usage();
696 return USAGE;
697 }
698 }
Yifan Hong38d53e02017-02-13 17:51:59 -0800699
700 if (mSelectedColumns == 0) {
Yifan Honga3b87092017-03-02 19:19:29 -0800701 mSelectedColumns = ENABLE_INTERFACE_NAME | ENABLE_SERVER_PID | ENABLE_CLIENT_PIDS;
Yifan Hong38d53e02017-02-13 17:51:59 -0800702 }
Yifan Hongb0dde932017-02-10 17:49:58 -0800703 return OK;
704}
705
706int Lshal::main(int argc, char **argv) {
707 Status status = parseArgs(argc, argv);
708 if (status != OK) {
709 return status;
710 }
711 status = fetch();
Yifan Hong38d53e02017-02-13 17:51:59 -0800712 postprocess();
Yifan Hongb0dde932017-02-10 17:49:58 -0800713 dump();
714 return status;
715}
716
Yifan Honga57dffb2017-02-21 14:59:00 -0800717void signalHandler(int sig) {
718 if (sig == SIGINT) {
719 int retVal;
720 pthread_exit(&retVal);
721 }
722}
723
Yifan Hongb0dde932017-02-10 17:49:58 -0800724} // namespace lshal
725} // namespace android
726
727int main(int argc, char **argv) {
Yifan Honga57dffb2017-02-21 14:59:00 -0800728 signal(SIGINT, ::android::lshal::signalHandler);
Yifan Hongb0dde932017-02-10 17:49:58 -0800729 return ::android::lshal::Lshal{}.main(argc, argv);
730}