blob: 51a868296da56a3e44da54243d68285b491d78cc [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) {
Steven Moreland56d2d512017-03-21 12:17:55 -0700459 if (info.clientPids.size() <= 0) {
460 continue;
461 }
Yifan Honga3b87092017-03-02 19:19:29 -0800462 putEntry(PTSERVICEMANAGER_REG_CLIENT, {
Yifan Hongb0dde932017-02-10 17:49:58 -0800463 .interfaceName =
464 std::string{info.interfaceName.c_str()} + "/" +
465 std::string{info.instanceName.c_str()},
466 .transport = "passthrough",
467 .serverPid = info.clientPids.size() == 1 ? info.clientPids[0] : NO_PID,
468 .serverObjectAddress = NO_PTR,
Yifan Hong4b865492017-02-28 19:38:24 -0800469 .clientPids = info.clientPids,
Yifan Honga3b87092017-03-02 19:19:29 -0800470 .arch = fromBaseArchitecture(info.arch)
Yifan Hongb0dde932017-02-10 17:49:58 -0800471 });
472 }
473 });
474 if (!ret.isOk()) {
475 mErr << "Error: Failed to call debugDump on defaultServiceManager(): "
476 << ret.description() << std::endl;
477 return DUMP_PASSTHROUGH_ERROR;
478 }
479 return OK;
480}
481
482Status Lshal::fetchBinderized(const sp<IServiceManager> &manager) {
483 using namespace ::std;
484 using namespace ::android::hardware;
485 using namespace ::android::hidl::manager::V1_0;
486 using namespace ::android::hidl::base::V1_0;
487 const std::string mode = "hwbinder";
Yifan Hongb0dde932017-02-10 17:49:58 -0800488
Steven Moreland9b5c15d2017-02-28 17:52:58 -0800489 hidl_vec<hidl_string> fqInstanceNames;
490 // copying out for timeoutIPC
491 auto listRet = timeoutIPC(manager, &IServiceManager::list, [&] (const auto &names) {
492 fqInstanceNames = names;
Yifan Hongb0dde932017-02-10 17:49:58 -0800493 });
494 if (!listRet.isOk()) {
495 mErr << "Error: Failed to list services for " << mode << ": "
496 << listRet.description() << std::endl;
Steven Moreland9b5c15d2017-02-28 17:52:58 -0800497 return DUMP_BINDERIZED_ERROR;
498 }
499
500 Status status = OK;
501 // server pid, .ptr value of binder object, child pids
502 std::map<std::string, DebugInfo> allDebugInfos;
503 std::map<pid_t, std::map<uint64_t, Pids>> allPids;
504 for (const auto &fqInstanceName : fqInstanceNames) {
Yifan Hong4b865492017-02-28 19:38:24 -0800505 const auto pair = splitFirst(fqInstanceName, '/');
Steven Moreland9b5c15d2017-02-28 17:52:58 -0800506 const auto &serviceName = pair.first;
507 const auto &instanceName = pair.second;
508 auto getRet = timeoutIPC(manager, &IServiceManager::get, serviceName, instanceName);
509 if (!getRet.isOk()) {
510 mErr << "Warning: Skipping \"" << fqInstanceName << "\": "
511 << "cannot be fetched from service manager:"
512 << getRet.description() << std::endl;
513 status |= DUMP_BINDERIZED_ERROR;
514 continue;
515 }
516 sp<IBase> service = getRet;
517 if (service == nullptr) {
518 mErr << "Warning: Skipping \"" << fqInstanceName << "\": "
519 << "cannot be fetched from service manager (null)";
520 status |= DUMP_BINDERIZED_ERROR;
521 continue;
522 }
523 auto debugRet = timeoutIPC(service, &IBase::getDebugInfo, [&] (const auto &debugInfo) {
524 allDebugInfos[fqInstanceName] = debugInfo;
525 if (debugInfo.pid >= 0) {
526 allPids[static_cast<pid_t>(debugInfo.pid)].clear();
527 }
528 });
529 if (!debugRet.isOk()) {
530 mErr << "Warning: Skipping \"" << fqInstanceName << "\": "
531 << "debugging information cannot be retrieved:"
532 << debugRet.description() << std::endl;
533 status |= DUMP_BINDERIZED_ERROR;
534 }
535 }
536 for (auto &pair : allPids) {
537 pid_t serverPid = pair.first;
538 if (!getReferencedPids(serverPid, &allPids[serverPid])) {
539 mErr << "Warning: no information for PID " << serverPid
540 << ", are you root?" << std::endl;
541 status |= DUMP_BINDERIZED_ERROR;
542 }
543 }
544 for (const auto &fqInstanceName : fqInstanceNames) {
545 auto it = allDebugInfos.find(fqInstanceName);
546 if (it == allDebugInfos.end()) {
Yifan Honga3b87092017-03-02 19:19:29 -0800547 putEntry(HWSERVICEMANAGER_LIST, {
Steven Moreland9b5c15d2017-02-28 17:52:58 -0800548 .interfaceName = fqInstanceName,
549 .transport = mode,
550 .serverPid = NO_PID,
551 .serverObjectAddress = NO_PTR,
Yifan Hong4b865492017-02-28 19:38:24 -0800552 .clientPids = {},
Yifan Honga3b87092017-03-02 19:19:29 -0800553 .arch = ARCH_UNKNOWN
Steven Moreland9b5c15d2017-02-28 17:52:58 -0800554 });
555 continue;
556 }
557 const DebugInfo &info = it->second;
Yifan Honga3b87092017-03-02 19:19:29 -0800558 putEntry(HWSERVICEMANAGER_LIST, {
Steven Moreland9b5c15d2017-02-28 17:52:58 -0800559 .interfaceName = fqInstanceName,
560 .transport = mode,
561 .serverPid = info.pid,
562 .serverObjectAddress = info.ptr,
563 .clientPids = info.pid == NO_PID || info.ptr == NO_PTR
Yifan Hong4b865492017-02-28 19:38:24 -0800564 ? Pids{} : allPids[info.pid][info.ptr],
Yifan Hongb4479022017-03-02 16:54:11 -0800565 .arch = fromBaseArchitecture(info.arch),
Steven Moreland9b5c15d2017-02-28 17:52:58 -0800566 });
Yifan Hongb0dde932017-02-10 17:49:58 -0800567 }
568 return status;
569}
570
571Status Lshal::fetch() {
572 Status status = OK;
573 auto bManager = ::android::hardware::defaultServiceManager();
574 if (bManager == nullptr) {
575 mErr << "Failed to get defaultServiceManager()!" << std::endl;
576 status |= NO_BINDERIZED_MANAGER;
577 } else {
578 status |= fetchBinderized(bManager);
579 // Passthrough PIDs are registered to the binderized manager as well.
580 status |= fetchPassthrough(bManager);
581 }
582
583 auto pManager = ::android::hardware::getPassthroughServiceManager();
584 if (pManager == nullptr) {
585 mErr << "Failed to get getPassthroughServiceManager()!" << std::endl;
586 status |= NO_PASSTHROUGH_MANAGER;
587 } else {
588 status |= fetchAllLibraries(pManager);
589 }
590 return status;
591}
592
593void Lshal::usage() const {
594 mErr
595 << "usage: lshal" << std::endl
Yifan Honga3b87092017-03-02 19:19:29 -0800596 << " Dump all hals with default ordering and columns [-ipc]." << std::endl
Yifan Hongb4479022017-03-02 16:54:11 -0800597 << " lshal [--interface|-i] [--transport|-t] [-r|--arch]" << std::endl
Yifan Hong38d53e02017-02-13 17:51:59 -0800598 << " [--pid|-p] [--address|-a] [--clients|-c] [--cmdline|-m]" << std::endl
Yifan Hong4b865492017-02-28 19:38:24 -0800599 << " [--sort={interface|i|pid|p}] [--init-vintf[=path]]" << std::endl
Yifan Hong38d53e02017-02-13 17:51:59 -0800600 << " -i, --interface: print the interface name column" << std::endl
601 << " -n, --instance: print the instance name column" << std::endl
602 << " -t, --transport: print the transport mode column" << std::endl
Yifan Hongb4479022017-03-02 16:54:11 -0800603 << " -r, --arch: print if the HAL is in 64-bit or 32-bit" << std::endl
Yifan Hongae09a3d2017-02-14 17:33:50 -0800604 << " -p, --pid: print the server PID, or server cmdline if -m is set" << std::endl
Yifan Hong38d53e02017-02-13 17:51:59 -0800605 << " -a, --address: print the server object address column" << std::endl
Yifan Hongae09a3d2017-02-14 17:33:50 -0800606 << " -c, --clients: print the client PIDs, or client cmdlines if -m is set"
607 << std::endl
608 << " -m, --cmdline: print cmdline instead of PIDs" << std::endl
Yifan Hong38d53e02017-02-13 17:51:59 -0800609 << " --sort=i, --sort=interface: sort by interface name" << std::endl
610 << " --sort=p, --sort=pid: sort by server pid" << std::endl
Yifan Hong4b865492017-02-28 19:38:24 -0800611 << " --init-vintf=path: form a skeleton HAL manifest to specified file " << std::endl
612 << " (stdout if no file specified)" << std::endl
Yifan Hongb0dde932017-02-10 17:49:58 -0800613 << " lshal [-h|--help]" << std::endl
614 << " -h, --help: show this help information." << std::endl;
615}
616
617Status Lshal::parseArgs(int argc, char **argv) {
618 static struct option longOptions[] = {
Yifan Hong38d53e02017-02-13 17:51:59 -0800619 // long options with short alternatives
620 {"help", no_argument, 0, 'h' },
621 {"interface", no_argument, 0, 'i' },
622 {"transport", no_argument, 0, 't' },
Yifan Hongb4479022017-03-02 16:54:11 -0800623 {"arch", no_argument, 0, 'r' },
Yifan Hong38d53e02017-02-13 17:51:59 -0800624 {"pid", no_argument, 0, 'p' },
625 {"address", no_argument, 0, 'a' },
626 {"clients", no_argument, 0, 'c' },
Yifan Hongae09a3d2017-02-14 17:33:50 -0800627 {"cmdline", no_argument, 0, 'm' },
Yifan Hong38d53e02017-02-13 17:51:59 -0800628
629 // long options without short alternatives
630 {"sort", required_argument, 0, 's' },
Yifan Hong4b865492017-02-28 19:38:24 -0800631 {"init-vintf",optional_argument, 0, 'v' },
Yifan Hong38d53e02017-02-13 17:51:59 -0800632 { 0, 0, 0, 0 }
Yifan Hongb0dde932017-02-10 17:49:58 -0800633 };
634
635 int optionIndex;
636 int c;
637 optind = 1;
638 for (;;) {
639 // using getopt_long in case we want to add other options in the future
Yifan Hongb4479022017-03-02 16:54:11 -0800640 c = getopt_long(argc, argv, "hitrpacm", longOptions, &optionIndex);
Yifan Hongb0dde932017-02-10 17:49:58 -0800641 if (c == -1) {
642 break;
643 }
644 switch (c) {
Yifan Hong38d53e02017-02-13 17:51:59 -0800645 case 's': {
646 if (strcmp(optarg, "interface") == 0 || strcmp(optarg, "i") == 0) {
647 mSortColumn = TableEntry::sortByInterfaceName;
648 } else if (strcmp(optarg, "pid") == 0 || strcmp(optarg, "p") == 0) {
649 mSortColumn = TableEntry::sortByServerPid;
650 } else {
651 mErr << "Unrecognized sorting column: " << optarg << std::endl;
652 usage();
653 return USAGE;
654 }
655 break;
656 }
Yifan Hong4b865492017-02-28 19:38:24 -0800657 case 'v': {
658 if (optarg) {
659 mFileOutput = new std::ofstream{optarg};
660 mOut = mFileOutput;
661 if (!mFileOutput.buf().is_open()) {
662 mErr << "Could not open file '" << optarg << "'." << std::endl;
663 return IO_ERROR;
664 }
665 }
666 mVintf = true;
667 }
Yifan Hong38d53e02017-02-13 17:51:59 -0800668 case 'i': {
669 mSelectedColumns |= ENABLE_INTERFACE_NAME;
670 break;
671 }
672 case 't': {
673 mSelectedColumns |= ENABLE_TRANSPORT;
674 break;
675 }
Yifan Hongb4479022017-03-02 16:54:11 -0800676 case 'r': {
677 mSelectedColumns |= ENABLE_ARCH;
678 break;
679 }
Yifan Hong38d53e02017-02-13 17:51:59 -0800680 case 'p': {
681 mSelectedColumns |= ENABLE_SERVER_PID;
682 break;
683 }
684 case 'a': {
685 mSelectedColumns |= ENABLE_SERVER_ADDR;
686 break;
687 }
688 case 'c': {
689 mSelectedColumns |= ENABLE_CLIENT_PIDS;
690 break;
691 }
Yifan Hongae09a3d2017-02-14 17:33:50 -0800692 case 'm': {
693 mEnableCmdlines = true;
694 break;
695 }
Yifan Hongb0dde932017-02-10 17:49:58 -0800696 case 'h': // falls through
697 default: // see unrecognized options
698 usage();
699 return USAGE;
700 }
701 }
Yifan Hong38d53e02017-02-13 17:51:59 -0800702
703 if (mSelectedColumns == 0) {
Yifan Honga3b87092017-03-02 19:19:29 -0800704 mSelectedColumns = ENABLE_INTERFACE_NAME | ENABLE_SERVER_PID | ENABLE_CLIENT_PIDS;
Yifan Hong38d53e02017-02-13 17:51:59 -0800705 }
Yifan Hongb0dde932017-02-10 17:49:58 -0800706 return OK;
707}
708
709int Lshal::main(int argc, char **argv) {
710 Status status = parseArgs(argc, argv);
711 if (status != OK) {
712 return status;
713 }
714 status = fetch();
Yifan Hong38d53e02017-02-13 17:51:59 -0800715 postprocess();
Yifan Hongb0dde932017-02-10 17:49:58 -0800716 dump();
717 return status;
718}
719
Yifan Honga57dffb2017-02-21 14:59:00 -0800720void signalHandler(int sig) {
721 if (sig == SIGINT) {
722 int retVal;
723 pthread_exit(&retVal);
724 }
725}
726
Yifan Hongb0dde932017-02-10 17:49:58 -0800727} // namespace lshal
728} // namespace android
729
730int main(int argc, char **argv) {
Yifan Honga57dffb2017-02-21 14:59:00 -0800731 signal(SIGINT, ::android::lshal::signalHandler);
Yifan Hongb0dde932017-02-10 17:49:58 -0800732 return ::android::lshal::Lshal{}.main(argc, argv);
733}