Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 1 | /* |
| 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 Hong | 4b86549 | 2017-02-28 19:38:24 -0800 | [diff] [blame] | 31 | #include <hidl-util/FQName.h> |
| 32 | #include <vintf/HalManifest.h> |
| 33 | #include <vintf/parse_xml.h> |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 34 | |
Yifan Hong | e2dadf0 | 2017-02-14 15:43:31 -0800 | [diff] [blame] | 35 | #include "Timeout.h" |
| 36 | |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 37 | using ::android::hardware::hidl_string; |
| 38 | using ::android::hidl::manager::V1_0::IServiceManager; |
| 39 | |
| 40 | namespace android { |
| 41 | namespace lshal { |
| 42 | |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 43 | template <typename A> |
| 44 | std::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 | |
| 58 | static 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 Hong | 4b86549 | 2017-02-28 19:38:24 -0800 | [diff] [blame] | 64 | template<typename String> |
| 65 | static std::pair<String, String> splitFirst(const String &s, char c) { |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 66 | const char *pos = strchr(s.c_str(), c); |
| 67 | if (pos == nullptr) { |
| 68 | return {s, {}}; |
| 69 | } |
Yifan Hong | 4b86549 | 2017-02-28 19:38:24 -0800 | [diff] [blame] | 70 | return {String(s.c_str(), pos - s.c_str()), String(pos + 1)}; |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | static 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 Hong | 4b86549 | 2017-02-28 19:38:24 -0800 | [diff] [blame] | 88 | static 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 Hong | ae09a3d | 2017-02-14 17:33:50 -0800 | [diff] [blame] | 96 | std::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 | |
| 106 | const 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 | |
| 115 | void 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 Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 122 | bool 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 Hong | 38d53e0 | 2017-02-13 17:51:59 -0800 | [diff] [blame] | 162 | void Lshal::postprocess() { |
| 163 | if (mSortColumn) { |
| 164 | std::sort(mTable.begin(), mTable.end(), mSortColumn); |
| 165 | } |
Yifan Hong | ae09a3d | 2017-02-14 17:33:50 -0800 | [diff] [blame] | 166 | for (TableEntry &entry : mTable) { |
| 167 | entry.serverCmdline = getCmdline(entry.serverPid); |
| 168 | removeDeadProcesses(&entry.clientPids); |
| 169 | for (auto pid : entry.clientPids) { |
| 170 | entry.clientCmdlines.push_back(this->getCmdline(pid)); |
| 171 | } |
| 172 | } |
Yifan Hong | 38d53e0 | 2017-02-13 17:51:59 -0800 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | void Lshal::printLine( |
| 176 | const std::string &interfaceName, |
Yifan Hong | b447902 | 2017-03-02 16:54:11 -0800 | [diff] [blame^] | 177 | const std::string &transport, |
| 178 | const std::string &arch, |
| 179 | const std::string &server, |
Yifan Hong | ae09a3d | 2017-02-14 17:33:50 -0800 | [diff] [blame] | 180 | const std::string &serverCmdline, |
| 181 | const std::string &address, const std::string &clients, |
| 182 | const std::string &clientCmdlines) const { |
Yifan Hong | 38d53e0 | 2017-02-13 17:51:59 -0800 | [diff] [blame] | 183 | if (mSelectedColumns & ENABLE_INTERFACE_NAME) |
| 184 | mOut << std::setw(80) << interfaceName << "\t"; |
| 185 | if (mSelectedColumns & ENABLE_TRANSPORT) |
| 186 | mOut << std::setw(10) << transport << "\t"; |
Yifan Hong | b447902 | 2017-03-02 16:54:11 -0800 | [diff] [blame^] | 187 | if (mSelectedColumns & ENABLE_ARCH) |
| 188 | mOut << std::setw(5) << arch << "\t"; |
Yifan Hong | ae09a3d | 2017-02-14 17:33:50 -0800 | [diff] [blame] | 189 | if (mSelectedColumns & ENABLE_SERVER_PID) { |
| 190 | if (mEnableCmdlines) { |
| 191 | mOut << std::setw(15) << serverCmdline << "\t"; |
| 192 | } else { |
| 193 | mOut << std::setw(5) << server << "\t"; |
| 194 | } |
| 195 | } |
Yifan Hong | 38d53e0 | 2017-02-13 17:51:59 -0800 | [diff] [blame] | 196 | if (mSelectedColumns & ENABLE_SERVER_ADDR) |
| 197 | mOut << std::setw(16) << address << "\t"; |
Yifan Hong | ae09a3d | 2017-02-14 17:33:50 -0800 | [diff] [blame] | 198 | if (mSelectedColumns & ENABLE_CLIENT_PIDS) { |
| 199 | if (mEnableCmdlines) { |
| 200 | mOut << std::setw(0) << clientCmdlines; |
| 201 | } else { |
| 202 | mOut << std::setw(0) << clients; |
| 203 | } |
| 204 | } |
Yifan Hong | 38d53e0 | 2017-02-13 17:51:59 -0800 | [diff] [blame] | 205 | mOut << std::endl; |
| 206 | } |
| 207 | |
Yifan Hong | 4b86549 | 2017-02-28 19:38:24 -0800 | [diff] [blame] | 208 | void Lshal::dumpVintf() const { |
| 209 | vintf::HalManifest manifest; |
| 210 | for (const TableEntry &entry : mTable) { |
| 211 | |
| 212 | std::string fqInstanceName = entry.interfaceName; |
| 213 | |
| 214 | if (entry.source == LIST_DLLIB) { |
| 215 | // Quick hack to work around *'s |
| 216 | replaceAll(&fqInstanceName, '*', 'D'); |
| 217 | } |
| 218 | auto splittedFqInstanceName = splitFirst(fqInstanceName, '/'); |
| 219 | FQName fqName(splittedFqInstanceName.first); |
| 220 | if (!fqName.isValid()) { |
| 221 | mErr << "Warning: '" << splittedFqInstanceName.first |
| 222 | << "' is not a valid FQName." << std::endl; |
| 223 | continue; |
| 224 | } |
| 225 | // Strip out system libs. |
| 226 | // TODO(b/34772739): might want to add other framework HAL packages |
| 227 | if (fqName.inPackage("android.hidl")) { |
| 228 | continue; |
| 229 | } |
| 230 | std::string interfaceName = |
| 231 | entry.source == LIST_DLLIB ? "" : fqName.name(); |
| 232 | std::string instanceName = |
| 233 | entry.source == LIST_DLLIB ? "" : splittedFqInstanceName.second; |
| 234 | |
| 235 | vintf::Transport transport; |
| 236 | if (entry.transport == "hwbinder") { |
| 237 | transport = vintf::Transport::HWBINDER; |
| 238 | } else if (entry.transport == "passthrough") { |
| 239 | transport = vintf::Transport::PASSTHROUGH; |
| 240 | } else { |
| 241 | mErr << "Warning: '" << entry.transport << "' is not a valid transport." << std::endl; |
| 242 | continue; |
| 243 | } |
| 244 | |
| 245 | vintf::ManifestHal *hal = manifest.getHal(fqName.package()); |
| 246 | if (hal == nullptr) { |
| 247 | if (!manifest.add(vintf::ManifestHal{ |
| 248 | .format = vintf::HalFormat::HIDL, |
| 249 | .name = fqName.package(), |
| 250 | .impl = {.implLevel = vintf::ImplLevel::GENERIC, .impl = ""}, |
| 251 | .transport = transport |
| 252 | })) { |
| 253 | mErr << "Warning: cannot add hal '" << fqInstanceName << "'" << std::endl; |
| 254 | continue; |
| 255 | } |
| 256 | hal = manifest.getHal(fqName.package()); |
| 257 | } |
| 258 | if (hal == nullptr) { |
| 259 | mErr << "Warning: cannot get hal '" << fqInstanceName |
| 260 | << "' after adding it" << std::endl; |
| 261 | continue; |
| 262 | } |
| 263 | vintf::Version version{fqName.getPackageMajorVersion(), fqName.getPackageMinorVersion()}; |
| 264 | if (std::find(hal->versions.begin(), hal->versions.end(), version) == hal->versions.end()) { |
| 265 | hal->versions.push_back(version); |
| 266 | } |
| 267 | if (entry.source != LIST_DLLIB) { |
| 268 | auto it = hal->interfaces.find(interfaceName); |
| 269 | if (it == hal->interfaces.end()) { |
| 270 | hal->interfaces.insert({interfaceName, {interfaceName, {{instanceName}}}}); |
| 271 | } else { |
| 272 | it->second.instances.insert(instanceName); |
| 273 | } |
| 274 | } |
| 275 | } |
| 276 | mOut << vintf::gHalManifestConverter(manifest); |
| 277 | } |
| 278 | |
Yifan Hong | b447902 | 2017-03-02 16:54:11 -0800 | [diff] [blame^] | 279 | static const std::string &getArchString(Architecture arch) { |
| 280 | static const std::string sStr64 = "64"; |
| 281 | static const std::string sStr32 = "32"; |
| 282 | static const std::string sStrBoth = "64&32"; |
| 283 | static const std::string sStrUnknown = ""; |
| 284 | switch (arch) { |
| 285 | case ARCH64: |
| 286 | return sStr64; |
| 287 | case ARCH32: |
| 288 | return sStr32; |
| 289 | case ARCH_BOTH: |
| 290 | return sStrBoth; |
| 291 | case ARCH_UNKNOWN: // fall through |
| 292 | default: |
| 293 | return sStrUnknown; |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | static Architecture fromBaseArchitecture(::android::hidl::base::V1_0::DebugInfo::Architecture a) { |
| 298 | switch (a) { |
| 299 | case ::android::hidl::base::V1_0::DebugInfo::Architecture::IS_64BIT: |
| 300 | return ARCH64; |
| 301 | case ::android::hidl::base::V1_0::DebugInfo::Architecture::IS_32BIT: |
| 302 | return ARCH32; |
| 303 | case ::android::hidl::base::V1_0::DebugInfo::Architecture::UNKNOWN: // fallthrough |
| 304 | default: |
| 305 | return ARCH_UNKNOWN; |
| 306 | } |
| 307 | } |
| 308 | |
Yifan Hong | 4b86549 | 2017-02-28 19:38:24 -0800 | [diff] [blame] | 309 | void Lshal::dumpTable() const { |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 310 | mOut << "All services:" << std::endl; |
Yifan Hong | 38d53e0 | 2017-02-13 17:51:59 -0800 | [diff] [blame] | 311 | mOut << std::left; |
Yifan Hong | b447902 | 2017-03-02 16:54:11 -0800 | [diff] [blame^] | 312 | printLine("Interface", "Transport", "Arch", "Server", "Server CMD", "PTR", "Clients", "Clients CMD"); |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 313 | for (const auto &entry : mTable) { |
Yifan Hong | 38d53e0 | 2017-02-13 17:51:59 -0800 | [diff] [blame] | 314 | printLine(entry.interfaceName, |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 315 | entry.transport, |
Yifan Hong | b447902 | 2017-03-02 16:54:11 -0800 | [diff] [blame^] | 316 | getArchString(entry.arch), |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 317 | entry.serverPid == NO_PID ? "N/A" : std::to_string(entry.serverPid), |
Yifan Hong | ae09a3d | 2017-02-14 17:33:50 -0800 | [diff] [blame] | 318 | entry.serverCmdline, |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 319 | entry.serverObjectAddress == NO_PTR ? "N/A" : toHexString(entry.serverObjectAddress), |
Yifan Hong | ae09a3d | 2017-02-14 17:33:50 -0800 | [diff] [blame] | 320 | join(entry.clientPids, " "), |
| 321 | join(entry.clientCmdlines, ";")); |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 322 | } |
| 323 | } |
| 324 | |
Yifan Hong | 4b86549 | 2017-02-28 19:38:24 -0800 | [diff] [blame] | 325 | void Lshal::dump() { |
| 326 | if (mVintf) { |
| 327 | dumpVintf(); |
| 328 | if (!!mFileOutput) { |
| 329 | mFileOutput.buf().close(); |
| 330 | delete &mFileOutput.buf(); |
| 331 | mFileOutput = nullptr; |
| 332 | } |
| 333 | mOut = std::cout; |
| 334 | } else { |
| 335 | dumpTable(); |
| 336 | } |
| 337 | } |
| 338 | |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 339 | void Lshal::putEntry(TableEntry &&entry) { |
| 340 | mTable.push_back(std::forward<TableEntry>(entry)); |
| 341 | } |
| 342 | |
| 343 | Status Lshal::fetchAllLibraries(const sp<IServiceManager> &manager) { |
| 344 | using namespace ::android::hardware; |
| 345 | using namespace ::android::hidl::manager::V1_0; |
| 346 | using namespace ::android::hidl::base::V1_0; |
Yifan Hong | b447902 | 2017-03-02 16:54:11 -0800 | [diff] [blame^] | 347 | auto ret = timeoutIPC(manager, &IServiceManager::debugDump, [&] (const auto &infos) { |
| 348 | std::map<std::string, TableEntry> entries; |
| 349 | for (const auto &info : infos) { |
| 350 | std::string interfaceName = std::string{info.interfaceName.c_str()} + "/" + |
| 351 | std::string{info.instanceName.c_str()}; |
| 352 | entries.emplace(std::string{interfaceName}, TableEntry{ |
| 353 | .interfaceName = interfaceName, |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 354 | .transport = "passthrough", |
| 355 | .serverPid = NO_PID, |
| 356 | .serverObjectAddress = NO_PTR, |
Yifan Hong | 4b86549 | 2017-02-28 19:38:24 -0800 | [diff] [blame] | 357 | .clientPids = {}, |
Yifan Hong | b447902 | 2017-03-02 16:54:11 -0800 | [diff] [blame^] | 358 | .arch = ARCH_UNKNOWN, |
Yifan Hong | 4b86549 | 2017-02-28 19:38:24 -0800 | [diff] [blame] | 359 | .source = LIST_DLLIB |
Yifan Hong | b447902 | 2017-03-02 16:54:11 -0800 | [diff] [blame^] | 360 | }).first->second.arch |= fromBaseArchitecture(info.arch); |
| 361 | } |
| 362 | for (auto &&pair : entries) { |
| 363 | putEntry(std::move(pair.second)); |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 364 | } |
| 365 | }); |
| 366 | if (!ret.isOk()) { |
| 367 | mErr << "Error: Failed to call list on getPassthroughServiceManager(): " |
| 368 | << ret.description() << std::endl; |
| 369 | return DUMP_ALL_LIBS_ERROR; |
| 370 | } |
| 371 | return OK; |
| 372 | } |
| 373 | |
| 374 | Status Lshal::fetchPassthrough(const sp<IServiceManager> &manager) { |
| 375 | using namespace ::android::hardware; |
Yifan Hong | b447902 | 2017-03-02 16:54:11 -0800 | [diff] [blame^] | 376 | using namespace ::android::hardware::details; |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 377 | using namespace ::android::hidl::manager::V1_0; |
| 378 | using namespace ::android::hidl::base::V1_0; |
Yifan Hong | e2dadf0 | 2017-02-14 15:43:31 -0800 | [diff] [blame] | 379 | auto ret = timeoutIPC(manager, &IServiceManager::debugDump, [&] (const auto &infos) { |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 380 | for (const auto &info : infos) { |
| 381 | putEntry({ |
| 382 | .interfaceName = |
| 383 | std::string{info.interfaceName.c_str()} + "/" + |
| 384 | std::string{info.instanceName.c_str()}, |
| 385 | .transport = "passthrough", |
| 386 | .serverPid = info.clientPids.size() == 1 ? info.clientPids[0] : NO_PID, |
| 387 | .serverObjectAddress = NO_PTR, |
Yifan Hong | 4b86549 | 2017-02-28 19:38:24 -0800 | [diff] [blame] | 388 | .clientPids = info.clientPids, |
Yifan Hong | b447902 | 2017-03-02 16:54:11 -0800 | [diff] [blame^] | 389 | .arch = fromBaseArchitecture(info.arch), |
Yifan Hong | 4b86549 | 2017-02-28 19:38:24 -0800 | [diff] [blame] | 390 | .source = PTSERVICEMANAGER_REG_CLIENT |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 391 | }); |
| 392 | } |
| 393 | }); |
| 394 | if (!ret.isOk()) { |
| 395 | mErr << "Error: Failed to call debugDump on defaultServiceManager(): " |
| 396 | << ret.description() << std::endl; |
| 397 | return DUMP_PASSTHROUGH_ERROR; |
| 398 | } |
| 399 | return OK; |
| 400 | } |
| 401 | |
| 402 | Status Lshal::fetchBinderized(const sp<IServiceManager> &manager) { |
| 403 | using namespace ::std; |
| 404 | using namespace ::android::hardware; |
| 405 | using namespace ::android::hidl::manager::V1_0; |
| 406 | using namespace ::android::hidl::base::V1_0; |
| 407 | const std::string mode = "hwbinder"; |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 408 | |
Steven Moreland | 9b5c15d | 2017-02-28 17:52:58 -0800 | [diff] [blame] | 409 | hidl_vec<hidl_string> fqInstanceNames; |
| 410 | // copying out for timeoutIPC |
| 411 | auto listRet = timeoutIPC(manager, &IServiceManager::list, [&] (const auto &names) { |
| 412 | fqInstanceNames = names; |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 413 | }); |
| 414 | if (!listRet.isOk()) { |
| 415 | mErr << "Error: Failed to list services for " << mode << ": " |
| 416 | << listRet.description() << std::endl; |
Steven Moreland | 9b5c15d | 2017-02-28 17:52:58 -0800 | [diff] [blame] | 417 | return DUMP_BINDERIZED_ERROR; |
| 418 | } |
| 419 | |
| 420 | Status status = OK; |
| 421 | // server pid, .ptr value of binder object, child pids |
| 422 | std::map<std::string, DebugInfo> allDebugInfos; |
| 423 | std::map<pid_t, std::map<uint64_t, Pids>> allPids; |
| 424 | for (const auto &fqInstanceName : fqInstanceNames) { |
Yifan Hong | 4b86549 | 2017-02-28 19:38:24 -0800 | [diff] [blame] | 425 | const auto pair = splitFirst(fqInstanceName, '/'); |
Steven Moreland | 9b5c15d | 2017-02-28 17:52:58 -0800 | [diff] [blame] | 426 | const auto &serviceName = pair.first; |
| 427 | const auto &instanceName = pair.second; |
| 428 | auto getRet = timeoutIPC(manager, &IServiceManager::get, serviceName, instanceName); |
| 429 | if (!getRet.isOk()) { |
| 430 | mErr << "Warning: Skipping \"" << fqInstanceName << "\": " |
| 431 | << "cannot be fetched from service manager:" |
| 432 | << getRet.description() << std::endl; |
| 433 | status |= DUMP_BINDERIZED_ERROR; |
| 434 | continue; |
| 435 | } |
| 436 | sp<IBase> service = getRet; |
| 437 | if (service == nullptr) { |
| 438 | mErr << "Warning: Skipping \"" << fqInstanceName << "\": " |
| 439 | << "cannot be fetched from service manager (null)"; |
| 440 | status |= DUMP_BINDERIZED_ERROR; |
| 441 | continue; |
| 442 | } |
| 443 | auto debugRet = timeoutIPC(service, &IBase::getDebugInfo, [&] (const auto &debugInfo) { |
| 444 | allDebugInfos[fqInstanceName] = debugInfo; |
| 445 | if (debugInfo.pid >= 0) { |
| 446 | allPids[static_cast<pid_t>(debugInfo.pid)].clear(); |
| 447 | } |
| 448 | }); |
| 449 | if (!debugRet.isOk()) { |
| 450 | mErr << "Warning: Skipping \"" << fqInstanceName << "\": " |
| 451 | << "debugging information cannot be retrieved:" |
| 452 | << debugRet.description() << std::endl; |
| 453 | status |= DUMP_BINDERIZED_ERROR; |
| 454 | } |
| 455 | } |
| 456 | for (auto &pair : allPids) { |
| 457 | pid_t serverPid = pair.first; |
| 458 | if (!getReferencedPids(serverPid, &allPids[serverPid])) { |
| 459 | mErr << "Warning: no information for PID " << serverPid |
| 460 | << ", are you root?" << std::endl; |
| 461 | status |= DUMP_BINDERIZED_ERROR; |
| 462 | } |
| 463 | } |
| 464 | for (const auto &fqInstanceName : fqInstanceNames) { |
| 465 | auto it = allDebugInfos.find(fqInstanceName); |
| 466 | if (it == allDebugInfos.end()) { |
| 467 | putEntry({ |
| 468 | .interfaceName = fqInstanceName, |
| 469 | .transport = mode, |
| 470 | .serverPid = NO_PID, |
| 471 | .serverObjectAddress = NO_PTR, |
Yifan Hong | 4b86549 | 2017-02-28 19:38:24 -0800 | [diff] [blame] | 472 | .clientPids = {}, |
Yifan Hong | b447902 | 2017-03-02 16:54:11 -0800 | [diff] [blame^] | 473 | .arch = ARCH_UNKNOWN, |
Yifan Hong | 4b86549 | 2017-02-28 19:38:24 -0800 | [diff] [blame] | 474 | .source = HWSERVICEMANAGER_LIST |
Steven Moreland | 9b5c15d | 2017-02-28 17:52:58 -0800 | [diff] [blame] | 475 | }); |
| 476 | continue; |
| 477 | } |
| 478 | const DebugInfo &info = it->second; |
| 479 | putEntry({ |
| 480 | .interfaceName = fqInstanceName, |
| 481 | .transport = mode, |
| 482 | .serverPid = info.pid, |
| 483 | .serverObjectAddress = info.ptr, |
| 484 | .clientPids = info.pid == NO_PID || info.ptr == NO_PTR |
Yifan Hong | 4b86549 | 2017-02-28 19:38:24 -0800 | [diff] [blame] | 485 | ? Pids{} : allPids[info.pid][info.ptr], |
Yifan Hong | b447902 | 2017-03-02 16:54:11 -0800 | [diff] [blame^] | 486 | .arch = fromBaseArchitecture(info.arch), |
Yifan Hong | 4b86549 | 2017-02-28 19:38:24 -0800 | [diff] [blame] | 487 | .source = HWSERVICEMANAGER_LIST |
Steven Moreland | 9b5c15d | 2017-02-28 17:52:58 -0800 | [diff] [blame] | 488 | }); |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 489 | } |
| 490 | return status; |
| 491 | } |
| 492 | |
| 493 | Status Lshal::fetch() { |
| 494 | Status status = OK; |
| 495 | auto bManager = ::android::hardware::defaultServiceManager(); |
| 496 | if (bManager == nullptr) { |
| 497 | mErr << "Failed to get defaultServiceManager()!" << std::endl; |
| 498 | status |= NO_BINDERIZED_MANAGER; |
| 499 | } else { |
| 500 | status |= fetchBinderized(bManager); |
| 501 | // Passthrough PIDs are registered to the binderized manager as well. |
| 502 | status |= fetchPassthrough(bManager); |
| 503 | } |
| 504 | |
| 505 | auto pManager = ::android::hardware::getPassthroughServiceManager(); |
| 506 | if (pManager == nullptr) { |
| 507 | mErr << "Failed to get getPassthroughServiceManager()!" << std::endl; |
| 508 | status |= NO_PASSTHROUGH_MANAGER; |
| 509 | } else { |
| 510 | status |= fetchAllLibraries(pManager); |
| 511 | } |
| 512 | return status; |
| 513 | } |
| 514 | |
| 515 | void Lshal::usage() const { |
| 516 | mErr |
| 517 | << "usage: lshal" << std::endl |
Yifan Hong | 38d53e0 | 2017-02-13 17:51:59 -0800 | [diff] [blame] | 518 | << " Dump all hals with default ordering and columns [-itpc]." << std::endl |
Yifan Hong | b447902 | 2017-03-02 16:54:11 -0800 | [diff] [blame^] | 519 | << " lshal [--interface|-i] [--transport|-t] [-r|--arch]" << std::endl |
Yifan Hong | 38d53e0 | 2017-02-13 17:51:59 -0800 | [diff] [blame] | 520 | << " [--pid|-p] [--address|-a] [--clients|-c] [--cmdline|-m]" << std::endl |
Yifan Hong | 4b86549 | 2017-02-28 19:38:24 -0800 | [diff] [blame] | 521 | << " [--sort={interface|i|pid|p}] [--init-vintf[=path]]" << std::endl |
Yifan Hong | 38d53e0 | 2017-02-13 17:51:59 -0800 | [diff] [blame] | 522 | << " -i, --interface: print the interface name column" << std::endl |
| 523 | << " -n, --instance: print the instance name column" << std::endl |
| 524 | << " -t, --transport: print the transport mode column" << std::endl |
Yifan Hong | b447902 | 2017-03-02 16:54:11 -0800 | [diff] [blame^] | 525 | << " -r, --arch: print if the HAL is in 64-bit or 32-bit" << std::endl |
Yifan Hong | ae09a3d | 2017-02-14 17:33:50 -0800 | [diff] [blame] | 526 | << " -p, --pid: print the server PID, or server cmdline if -m is set" << std::endl |
Yifan Hong | 38d53e0 | 2017-02-13 17:51:59 -0800 | [diff] [blame] | 527 | << " -a, --address: print the server object address column" << std::endl |
Yifan Hong | ae09a3d | 2017-02-14 17:33:50 -0800 | [diff] [blame] | 528 | << " -c, --clients: print the client PIDs, or client cmdlines if -m is set" |
| 529 | << std::endl |
| 530 | << " -m, --cmdline: print cmdline instead of PIDs" << std::endl |
Yifan Hong | 38d53e0 | 2017-02-13 17:51:59 -0800 | [diff] [blame] | 531 | << " --sort=i, --sort=interface: sort by interface name" << std::endl |
| 532 | << " --sort=p, --sort=pid: sort by server pid" << std::endl |
Yifan Hong | 4b86549 | 2017-02-28 19:38:24 -0800 | [diff] [blame] | 533 | << " --init-vintf=path: form a skeleton HAL manifest to specified file " << std::endl |
| 534 | << " (stdout if no file specified)" << std::endl |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 535 | << " lshal [-h|--help]" << std::endl |
| 536 | << " -h, --help: show this help information." << std::endl; |
| 537 | } |
| 538 | |
| 539 | Status Lshal::parseArgs(int argc, char **argv) { |
| 540 | static struct option longOptions[] = { |
Yifan Hong | 38d53e0 | 2017-02-13 17:51:59 -0800 | [diff] [blame] | 541 | // long options with short alternatives |
| 542 | {"help", no_argument, 0, 'h' }, |
| 543 | {"interface", no_argument, 0, 'i' }, |
| 544 | {"transport", no_argument, 0, 't' }, |
Yifan Hong | b447902 | 2017-03-02 16:54:11 -0800 | [diff] [blame^] | 545 | {"arch", no_argument, 0, 'r' }, |
Yifan Hong | 38d53e0 | 2017-02-13 17:51:59 -0800 | [diff] [blame] | 546 | {"pid", no_argument, 0, 'p' }, |
| 547 | {"address", no_argument, 0, 'a' }, |
| 548 | {"clients", no_argument, 0, 'c' }, |
Yifan Hong | ae09a3d | 2017-02-14 17:33:50 -0800 | [diff] [blame] | 549 | {"cmdline", no_argument, 0, 'm' }, |
Yifan Hong | 38d53e0 | 2017-02-13 17:51:59 -0800 | [diff] [blame] | 550 | |
| 551 | // long options without short alternatives |
| 552 | {"sort", required_argument, 0, 's' }, |
Yifan Hong | 4b86549 | 2017-02-28 19:38:24 -0800 | [diff] [blame] | 553 | {"init-vintf",optional_argument, 0, 'v' }, |
Yifan Hong | 38d53e0 | 2017-02-13 17:51:59 -0800 | [diff] [blame] | 554 | { 0, 0, 0, 0 } |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 555 | }; |
| 556 | |
| 557 | int optionIndex; |
| 558 | int c; |
| 559 | optind = 1; |
| 560 | for (;;) { |
| 561 | // using getopt_long in case we want to add other options in the future |
Yifan Hong | b447902 | 2017-03-02 16:54:11 -0800 | [diff] [blame^] | 562 | c = getopt_long(argc, argv, "hitrpacm", longOptions, &optionIndex); |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 563 | if (c == -1) { |
| 564 | break; |
| 565 | } |
| 566 | switch (c) { |
Yifan Hong | 38d53e0 | 2017-02-13 17:51:59 -0800 | [diff] [blame] | 567 | case 's': { |
| 568 | if (strcmp(optarg, "interface") == 0 || strcmp(optarg, "i") == 0) { |
| 569 | mSortColumn = TableEntry::sortByInterfaceName; |
| 570 | } else if (strcmp(optarg, "pid") == 0 || strcmp(optarg, "p") == 0) { |
| 571 | mSortColumn = TableEntry::sortByServerPid; |
| 572 | } else { |
| 573 | mErr << "Unrecognized sorting column: " << optarg << std::endl; |
| 574 | usage(); |
| 575 | return USAGE; |
| 576 | } |
| 577 | break; |
| 578 | } |
Yifan Hong | 4b86549 | 2017-02-28 19:38:24 -0800 | [diff] [blame] | 579 | case 'v': { |
| 580 | if (optarg) { |
| 581 | mFileOutput = new std::ofstream{optarg}; |
| 582 | mOut = mFileOutput; |
| 583 | if (!mFileOutput.buf().is_open()) { |
| 584 | mErr << "Could not open file '" << optarg << "'." << std::endl; |
| 585 | return IO_ERROR; |
| 586 | } |
| 587 | } |
| 588 | mVintf = true; |
| 589 | } |
Yifan Hong | 38d53e0 | 2017-02-13 17:51:59 -0800 | [diff] [blame] | 590 | case 'i': { |
| 591 | mSelectedColumns |= ENABLE_INTERFACE_NAME; |
| 592 | break; |
| 593 | } |
| 594 | case 't': { |
| 595 | mSelectedColumns |= ENABLE_TRANSPORT; |
| 596 | break; |
| 597 | } |
Yifan Hong | b447902 | 2017-03-02 16:54:11 -0800 | [diff] [blame^] | 598 | case 'r': { |
| 599 | mSelectedColumns |= ENABLE_ARCH; |
| 600 | break; |
| 601 | } |
Yifan Hong | 38d53e0 | 2017-02-13 17:51:59 -0800 | [diff] [blame] | 602 | case 'p': { |
| 603 | mSelectedColumns |= ENABLE_SERVER_PID; |
| 604 | break; |
| 605 | } |
| 606 | case 'a': { |
| 607 | mSelectedColumns |= ENABLE_SERVER_ADDR; |
| 608 | break; |
| 609 | } |
| 610 | case 'c': { |
| 611 | mSelectedColumns |= ENABLE_CLIENT_PIDS; |
| 612 | break; |
| 613 | } |
Yifan Hong | ae09a3d | 2017-02-14 17:33:50 -0800 | [diff] [blame] | 614 | case 'm': { |
| 615 | mEnableCmdlines = true; |
| 616 | break; |
| 617 | } |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 618 | case 'h': // falls through |
| 619 | default: // see unrecognized options |
| 620 | usage(); |
| 621 | return USAGE; |
| 622 | } |
| 623 | } |
Yifan Hong | 38d53e0 | 2017-02-13 17:51:59 -0800 | [diff] [blame] | 624 | |
| 625 | if (mSelectedColumns == 0) { |
| 626 | mSelectedColumns = ENABLE_INTERFACE_NAME |
| 627 | | ENABLE_TRANSPORT | ENABLE_SERVER_PID | ENABLE_CLIENT_PIDS; |
| 628 | } |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 629 | return OK; |
| 630 | } |
| 631 | |
| 632 | int Lshal::main(int argc, char **argv) { |
| 633 | Status status = parseArgs(argc, argv); |
| 634 | if (status != OK) { |
| 635 | return status; |
| 636 | } |
| 637 | status = fetch(); |
Yifan Hong | 38d53e0 | 2017-02-13 17:51:59 -0800 | [diff] [blame] | 638 | postprocess(); |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 639 | dump(); |
| 640 | return status; |
| 641 | } |
| 642 | |
Yifan Hong | a57dffb | 2017-02-21 14:59:00 -0800 | [diff] [blame] | 643 | void signalHandler(int sig) { |
| 644 | if (sig == SIGINT) { |
| 645 | int retVal; |
| 646 | pthread_exit(&retVal); |
| 647 | } |
| 648 | } |
| 649 | |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 650 | } // namespace lshal |
| 651 | } // namespace android |
| 652 | |
| 653 | int main(int argc, char **argv) { |
Yifan Hong | a57dffb | 2017-02-21 14:59:00 -0800 | [diff] [blame] | 654 | signal(SIGINT, ::android::lshal::signalHandler); |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 655 | return ::android::lshal::Lshal{}.main(argc, argv); |
| 656 | } |