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 | a3b8709 | 2017-03-02 19:19:29 -0800 | [diff] [blame] | 162 | void Lshal::forEachTable(const std::function<void(Table &)> &f) { |
Yifan Hong | 8ab3bee | 2017-03-08 14:01:11 -0800 | [diff] [blame] | 163 | f(mServicesTable); |
| 164 | f(mPassthroughRefTable); |
| 165 | f(mImplementationsTable); |
Yifan Hong | a3b8709 | 2017-03-02 19:19:29 -0800 | [diff] [blame] | 166 | } |
| 167 | void Lshal::forEachTable(const std::function<void(const Table &)> &f) const { |
Yifan Hong | 8ab3bee | 2017-03-08 14:01:11 -0800 | [diff] [blame] | 168 | f(mServicesTable); |
| 169 | f(mPassthroughRefTable); |
| 170 | f(mImplementationsTable); |
Yifan Hong | a3b8709 | 2017-03-02 19:19:29 -0800 | [diff] [blame] | 171 | } |
| 172 | |
Yifan Hong | 38d53e0 | 2017-02-13 17:51:59 -0800 | [diff] [blame] | 173 | void Lshal::postprocess() { |
Yifan Hong | a3b8709 | 2017-03-02 19:19:29 -0800 | [diff] [blame] | 174 | forEachTable([this](Table &table) { |
| 175 | if (mSortColumn) { |
| 176 | std::sort(table.begin(), table.end(), mSortColumn); |
Yifan Hong | ae09a3d | 2017-02-14 17:33:50 -0800 | [diff] [blame] | 177 | } |
Yifan Hong | a3b8709 | 2017-03-02 19:19:29 -0800 | [diff] [blame] | 178 | 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 Hong | 3fdbd9f | 2017-03-08 14:01:58 -0800 | [diff] [blame] | 186 | // 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 Hong | 38d53e0 | 2017-02-13 17:51:59 -0800 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | void Lshal::printLine( |
| 209 | const std::string &interfaceName, |
Yifan Hong | b447902 | 2017-03-02 16:54:11 -0800 | [diff] [blame] | 210 | const std::string &transport, |
| 211 | const std::string &arch, |
| 212 | const std::string &server, |
Yifan Hong | ae09a3d | 2017-02-14 17:33:50 -0800 | [diff] [blame] | 213 | const std::string &serverCmdline, |
| 214 | const std::string &address, const std::string &clients, |
| 215 | const std::string &clientCmdlines) const { |
Yifan Hong | 38d53e0 | 2017-02-13 17:51:59 -0800 | [diff] [blame] | 216 | 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 Hong | b447902 | 2017-03-02 16:54:11 -0800 | [diff] [blame] | 220 | if (mSelectedColumns & ENABLE_ARCH) |
| 221 | mOut << std::setw(5) << arch << "\t"; |
Yifan Hong | ae09a3d | 2017-02-14 17:33:50 -0800 | [diff] [blame] | 222 | 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 Hong | 38d53e0 | 2017-02-13 17:51:59 -0800 | [diff] [blame] | 229 | if (mSelectedColumns & ENABLE_SERVER_ADDR) |
| 230 | mOut << std::setw(16) << address << "\t"; |
Yifan Hong | ae09a3d | 2017-02-14 17:33:50 -0800 | [diff] [blame] | 231 | 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 Hong | 38d53e0 | 2017-02-13 17:51:59 -0800 | [diff] [blame] | 238 | mOut << std::endl; |
| 239 | } |
| 240 | |
Yifan Hong | 4b86549 | 2017-02-28 19:38:24 -0800 | [diff] [blame] | 241 | void Lshal::dumpVintf() const { |
| 242 | vintf::HalManifest manifest; |
Yifan Hong | a3b8709 | 2017-03-02 19:19:29 -0800 | [diff] [blame] | 243 | forEachTable([this, &manifest] (const Table &table) { |
| 244 | for (const TableEntry &entry : table) { |
Yifan Hong | 4b86549 | 2017-02-28 19:38:24 -0800 | [diff] [blame] | 245 | |
Yifan Hong | a3b8709 | 2017-03-02 19:19:29 -0800 | [diff] [blame] | 246 | std::string fqInstanceName = entry.interfaceName; |
Yifan Hong | 4b86549 | 2017-02-28 19:38:24 -0800 | [diff] [blame] | 247 | |
Yifan Hong | a3b8709 | 2017-03-02 19:19:29 -0800 | [diff] [blame] | 248 | 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 Hong | 4b86549 | 2017-02-28 19:38:24 -0800 | [diff] [blame] | 257 | continue; |
| 258 | } |
Yifan Hong | a3b8709 | 2017-03-02 19:19:29 -0800 | [diff] [blame] | 259 | // Strip out system libs. |
Steven Moreland | 9b6cd60 | 2017-03-22 14:22:55 -0700 | [diff] [blame^] | 260 | if (fqName.inPackage("android.hidl") || |
| 261 | fqName.inPackage("android.frameworks") || |
| 262 | fqName.inPackage("android.system")) { |
Yifan Hong | a3b8709 | 2017-03-02 19:19:29 -0800 | [diff] [blame] | 263 | continue; |
| 264 | } |
| 265 | std::string interfaceName = |
| 266 | &table == &mImplementationsTable ? "" : fqName.name(); |
| 267 | std::string instanceName = |
| 268 | &table == &mImplementationsTable ? "" : splittedFqInstanceName.second; |
| 269 | |
| 270 | vintf::Transport transport; |
Yifan Hong | 3fdbd9f | 2017-03-08 14:01:58 -0800 | [diff] [blame] | 271 | vintf::Arch arch; |
Yifan Hong | a3b8709 | 2017-03-02 19:19:29 -0800 | [diff] [blame] | 272 | if (entry.transport == "hwbinder") { |
| 273 | transport = vintf::Transport::HWBINDER; |
Yifan Hong | 3fdbd9f | 2017-03-08 14:01:58 -0800 | [diff] [blame] | 274 | arch = vintf::Arch::ARCH_EMPTY; |
Yifan Hong | a3b8709 | 2017-03-02 19:19:29 -0800 | [diff] [blame] | 275 | } else if (entry.transport == "passthrough") { |
| 276 | transport = vintf::Transport::PASSTHROUGH; |
Yifan Hong | 3fdbd9f | 2017-03-08 14:01:58 -0800 | [diff] [blame] | 277 | switch (entry.arch) { |
| 278 | case lshal::ARCH32: |
| 279 | arch = vintf::Arch::ARCH_32; break; |
| 280 | case lshal::ARCH64: |
| 281 | arch = vintf::Arch::ARCH_64; break; |
| 282 | case lshal::ARCH_BOTH: |
| 283 | arch = vintf::Arch::ARCH_32_64; break; |
| 284 | case lshal::ARCH_UNKNOWN: // fallthrough |
| 285 | default: |
| 286 | mErr << "Warning: '" << fqName.package() |
| 287 | << "' doesn't have bitness info, assuming 32+64." << std::endl; |
| 288 | arch = vintf::Arch::ARCH_32_64; |
| 289 | } |
Yifan Hong | 4b86549 | 2017-02-28 19:38:24 -0800 | [diff] [blame] | 290 | } else { |
Yifan Hong | a3b8709 | 2017-03-02 19:19:29 -0800 | [diff] [blame] | 291 | mErr << "Warning: '" << entry.transport << "' is not a valid transport." << std::endl; |
| 292 | continue; |
| 293 | } |
| 294 | |
| 295 | vintf::ManifestHal *hal = manifest.getHal(fqName.package()); |
| 296 | if (hal == nullptr) { |
| 297 | if (!manifest.add(vintf::ManifestHal{ |
| 298 | .format = vintf::HalFormat::HIDL, |
| 299 | .name = fqName.package(), |
| 300 | .impl = {.implLevel = vintf::ImplLevel::GENERIC, .impl = ""}, |
Yifan Hong | 3fdbd9f | 2017-03-08 14:01:58 -0800 | [diff] [blame] | 301 | .transportArch = {transport, arch} |
Yifan Hong | a3b8709 | 2017-03-02 19:19:29 -0800 | [diff] [blame] | 302 | })) { |
| 303 | mErr << "Warning: cannot add hal '" << fqInstanceName << "'" << std::endl; |
| 304 | continue; |
| 305 | } |
| 306 | hal = manifest.getHal(fqName.package()); |
| 307 | } |
| 308 | if (hal == nullptr) { |
| 309 | mErr << "Warning: cannot get hal '" << fqInstanceName |
| 310 | << "' after adding it" << std::endl; |
| 311 | continue; |
| 312 | } |
| 313 | vintf::Version version{fqName.getPackageMajorVersion(), fqName.getPackageMinorVersion()}; |
| 314 | if (std::find(hal->versions.begin(), hal->versions.end(), version) == hal->versions.end()) { |
| 315 | hal->versions.push_back(version); |
| 316 | } |
| 317 | if (&table != &mImplementationsTable) { |
| 318 | auto it = hal->interfaces.find(interfaceName); |
| 319 | if (it == hal->interfaces.end()) { |
| 320 | hal->interfaces.insert({interfaceName, {interfaceName, {{instanceName}}}}); |
| 321 | } else { |
| 322 | it->second.instances.insert(instanceName); |
| 323 | } |
Yifan Hong | 4b86549 | 2017-02-28 19:38:24 -0800 | [diff] [blame] | 324 | } |
| 325 | } |
Yifan Hong | a3b8709 | 2017-03-02 19:19:29 -0800 | [diff] [blame] | 326 | }); |
Yifan Hong | 4b86549 | 2017-02-28 19:38:24 -0800 | [diff] [blame] | 327 | mOut << vintf::gHalManifestConverter(manifest); |
| 328 | } |
| 329 | |
Yifan Hong | b447902 | 2017-03-02 16:54:11 -0800 | [diff] [blame] | 330 | static const std::string &getArchString(Architecture arch) { |
| 331 | static const std::string sStr64 = "64"; |
| 332 | static const std::string sStr32 = "32"; |
Yifan Hong | 1071c1b | 2017-03-03 10:57:43 -0800 | [diff] [blame] | 333 | static const std::string sStrBoth = "32+64"; |
Yifan Hong | b447902 | 2017-03-02 16:54:11 -0800 | [diff] [blame] | 334 | static const std::string sStrUnknown = ""; |
| 335 | switch (arch) { |
| 336 | case ARCH64: |
| 337 | return sStr64; |
| 338 | case ARCH32: |
| 339 | return sStr32; |
| 340 | case ARCH_BOTH: |
| 341 | return sStrBoth; |
| 342 | case ARCH_UNKNOWN: // fall through |
| 343 | default: |
| 344 | return sStrUnknown; |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | static Architecture fromBaseArchitecture(::android::hidl::base::V1_0::DebugInfo::Architecture a) { |
| 349 | switch (a) { |
| 350 | case ::android::hidl::base::V1_0::DebugInfo::Architecture::IS_64BIT: |
| 351 | return ARCH64; |
| 352 | case ::android::hidl::base::V1_0::DebugInfo::Architecture::IS_32BIT: |
| 353 | return ARCH32; |
| 354 | case ::android::hidl::base::V1_0::DebugInfo::Architecture::UNKNOWN: // fallthrough |
| 355 | default: |
| 356 | return ARCH_UNKNOWN; |
| 357 | } |
| 358 | } |
| 359 | |
Yifan Hong | a3b8709 | 2017-03-02 19:19:29 -0800 | [diff] [blame] | 360 | void Lshal::dumpTable() { |
| 361 | mServicesTable.description = |
| 362 | "All binderized services (registered services through hwservicemanager)"; |
| 363 | mPassthroughRefTable.description = |
Yifan Hong | 1071c1b | 2017-03-03 10:57:43 -0800 | [diff] [blame] | 364 | "All interfaces that getService() has ever return as a passthrough interface;\n" |
Yifan Hong | a3b8709 | 2017-03-02 19:19:29 -0800 | [diff] [blame] | 365 | "PIDs / processes shown below might be inaccurate because the process\n" |
Yifan Hong | 1071c1b | 2017-03-03 10:57:43 -0800 | [diff] [blame] | 366 | "might have relinquished the interface or might have died.\n" |
Yifan Hong | a3b8709 | 2017-03-02 19:19:29 -0800 | [diff] [blame] | 367 | "The Server / Server CMD column can be ignored.\n" |
Yifan Hong | 1071c1b | 2017-03-03 10:57:43 -0800 | [diff] [blame] | 368 | "The Clients / Clients CMD column shows all process that have ever dlopen'ed \n" |
| 369 | "the library and successfully fetched the passthrough implementation."; |
Yifan Hong | a3b8709 | 2017-03-02 19:19:29 -0800 | [diff] [blame] | 370 | mImplementationsTable.description = |
| 371 | "All available passthrough implementations (all -impl.so files)"; |
| 372 | forEachTable([this] (const Table &table) { |
| 373 | mOut << table.description << std::endl; |
| 374 | mOut << std::left; |
| 375 | printLine("Interface", "Transport", "Arch", "Server", "Server CMD", |
| 376 | "PTR", "Clients", "Clients CMD"); |
| 377 | for (const auto &entry : table) { |
| 378 | printLine(entry.interfaceName, |
| 379 | entry.transport, |
| 380 | getArchString(entry.arch), |
| 381 | entry.serverPid == NO_PID ? "N/A" : std::to_string(entry.serverPid), |
| 382 | entry.serverCmdline, |
| 383 | entry.serverObjectAddress == NO_PTR ? "N/A" : toHexString(entry.serverObjectAddress), |
| 384 | join(entry.clientPids, " "), |
| 385 | join(entry.clientCmdlines, ";")); |
| 386 | } |
| 387 | mOut << std::endl; |
| 388 | }); |
| 389 | |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 390 | } |
| 391 | |
Yifan Hong | 4b86549 | 2017-02-28 19:38:24 -0800 | [diff] [blame] | 392 | void Lshal::dump() { |
| 393 | if (mVintf) { |
| 394 | dumpVintf(); |
| 395 | if (!!mFileOutput) { |
| 396 | mFileOutput.buf().close(); |
| 397 | delete &mFileOutput.buf(); |
| 398 | mFileOutput = nullptr; |
| 399 | } |
| 400 | mOut = std::cout; |
| 401 | } else { |
| 402 | dumpTable(); |
| 403 | } |
| 404 | } |
| 405 | |
Yifan Hong | a3b8709 | 2017-03-02 19:19:29 -0800 | [diff] [blame] | 406 | void Lshal::putEntry(TableEntrySource source, TableEntry &&entry) { |
| 407 | Table *table = nullptr; |
| 408 | switch (source) { |
| 409 | case HWSERVICEMANAGER_LIST : |
| 410 | table = &mServicesTable; break; |
| 411 | case PTSERVICEMANAGER_REG_CLIENT : |
| 412 | table = &mPassthroughRefTable; break; |
| 413 | case LIST_DLLIB : |
| 414 | table = &mImplementationsTable; break; |
| 415 | default: |
| 416 | mErr << "Error: Unknown source of entry " << source << std::endl; |
| 417 | } |
| 418 | if (table) { |
| 419 | table->entries.push_back(std::forward<TableEntry>(entry)); |
| 420 | } |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | Status Lshal::fetchAllLibraries(const sp<IServiceManager> &manager) { |
| 424 | using namespace ::android::hardware; |
| 425 | using namespace ::android::hidl::manager::V1_0; |
| 426 | using namespace ::android::hidl::base::V1_0; |
Yifan Hong | b447902 | 2017-03-02 16:54:11 -0800 | [diff] [blame] | 427 | auto ret = timeoutIPC(manager, &IServiceManager::debugDump, [&] (const auto &infos) { |
| 428 | std::map<std::string, TableEntry> entries; |
| 429 | for (const auto &info : infos) { |
| 430 | std::string interfaceName = std::string{info.interfaceName.c_str()} + "/" + |
| 431 | std::string{info.instanceName.c_str()}; |
Yifan Hong | 1071c1b | 2017-03-03 10:57:43 -0800 | [diff] [blame] | 432 | entries.emplace(interfaceName, TableEntry{ |
Yifan Hong | b447902 | 2017-03-02 16:54:11 -0800 | [diff] [blame] | 433 | .interfaceName = interfaceName, |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 434 | .transport = "passthrough", |
| 435 | .serverPid = NO_PID, |
| 436 | .serverObjectAddress = NO_PTR, |
Yifan Hong | 4b86549 | 2017-02-28 19:38:24 -0800 | [diff] [blame] | 437 | .clientPids = {}, |
Yifan Hong | a3b8709 | 2017-03-02 19:19:29 -0800 | [diff] [blame] | 438 | .arch = ARCH_UNKNOWN |
Yifan Hong | b447902 | 2017-03-02 16:54:11 -0800 | [diff] [blame] | 439 | }).first->second.arch |= fromBaseArchitecture(info.arch); |
| 440 | } |
| 441 | for (auto &&pair : entries) { |
Yifan Hong | a3b8709 | 2017-03-02 19:19:29 -0800 | [diff] [blame] | 442 | putEntry(LIST_DLLIB, std::move(pair.second)); |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 443 | } |
| 444 | }); |
| 445 | if (!ret.isOk()) { |
| 446 | mErr << "Error: Failed to call list on getPassthroughServiceManager(): " |
| 447 | << ret.description() << std::endl; |
| 448 | return DUMP_ALL_LIBS_ERROR; |
| 449 | } |
| 450 | return OK; |
| 451 | } |
| 452 | |
| 453 | Status Lshal::fetchPassthrough(const sp<IServiceManager> &manager) { |
| 454 | using namespace ::android::hardware; |
Yifan Hong | b447902 | 2017-03-02 16:54:11 -0800 | [diff] [blame] | 455 | using namespace ::android::hardware::details; |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 456 | using namespace ::android::hidl::manager::V1_0; |
| 457 | using namespace ::android::hidl::base::V1_0; |
Yifan Hong | e2dadf0 | 2017-02-14 15:43:31 -0800 | [diff] [blame] | 458 | auto ret = timeoutIPC(manager, &IServiceManager::debugDump, [&] (const auto &infos) { |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 459 | for (const auto &info : infos) { |
Yifan Hong | a3b8709 | 2017-03-02 19:19:29 -0800 | [diff] [blame] | 460 | putEntry(PTSERVICEMANAGER_REG_CLIENT, { |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 461 | .interfaceName = |
| 462 | std::string{info.interfaceName.c_str()} + "/" + |
| 463 | std::string{info.instanceName.c_str()}, |
| 464 | .transport = "passthrough", |
| 465 | .serverPid = info.clientPids.size() == 1 ? info.clientPids[0] : NO_PID, |
| 466 | .serverObjectAddress = NO_PTR, |
Yifan Hong | 4b86549 | 2017-02-28 19:38:24 -0800 | [diff] [blame] | 467 | .clientPids = info.clientPids, |
Yifan Hong | a3b8709 | 2017-03-02 19:19:29 -0800 | [diff] [blame] | 468 | .arch = fromBaseArchitecture(info.arch) |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 469 | }); |
| 470 | } |
| 471 | }); |
| 472 | if (!ret.isOk()) { |
| 473 | mErr << "Error: Failed to call debugDump on defaultServiceManager(): " |
| 474 | << ret.description() << std::endl; |
| 475 | return DUMP_PASSTHROUGH_ERROR; |
| 476 | } |
| 477 | return OK; |
| 478 | } |
| 479 | |
| 480 | Status Lshal::fetchBinderized(const sp<IServiceManager> &manager) { |
| 481 | using namespace ::std; |
| 482 | using namespace ::android::hardware; |
| 483 | using namespace ::android::hidl::manager::V1_0; |
| 484 | using namespace ::android::hidl::base::V1_0; |
| 485 | const std::string mode = "hwbinder"; |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 486 | |
Steven Moreland | 9b5c15d | 2017-02-28 17:52:58 -0800 | [diff] [blame] | 487 | hidl_vec<hidl_string> fqInstanceNames; |
| 488 | // copying out for timeoutIPC |
| 489 | auto listRet = timeoutIPC(manager, &IServiceManager::list, [&] (const auto &names) { |
| 490 | fqInstanceNames = names; |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 491 | }); |
| 492 | if (!listRet.isOk()) { |
| 493 | mErr << "Error: Failed to list services for " << mode << ": " |
| 494 | << listRet.description() << std::endl; |
Steven Moreland | 9b5c15d | 2017-02-28 17:52:58 -0800 | [diff] [blame] | 495 | return DUMP_BINDERIZED_ERROR; |
| 496 | } |
| 497 | |
| 498 | Status status = OK; |
| 499 | // server pid, .ptr value of binder object, child pids |
| 500 | std::map<std::string, DebugInfo> allDebugInfos; |
| 501 | std::map<pid_t, std::map<uint64_t, Pids>> allPids; |
| 502 | for (const auto &fqInstanceName : fqInstanceNames) { |
Yifan Hong | 4b86549 | 2017-02-28 19:38:24 -0800 | [diff] [blame] | 503 | const auto pair = splitFirst(fqInstanceName, '/'); |
Steven Moreland | 9b5c15d | 2017-02-28 17:52:58 -0800 | [diff] [blame] | 504 | const auto &serviceName = pair.first; |
| 505 | const auto &instanceName = pair.second; |
| 506 | auto getRet = timeoutIPC(manager, &IServiceManager::get, serviceName, instanceName); |
| 507 | if (!getRet.isOk()) { |
| 508 | mErr << "Warning: Skipping \"" << fqInstanceName << "\": " |
| 509 | << "cannot be fetched from service manager:" |
| 510 | << getRet.description() << std::endl; |
| 511 | status |= DUMP_BINDERIZED_ERROR; |
| 512 | continue; |
| 513 | } |
| 514 | sp<IBase> service = getRet; |
| 515 | if (service == nullptr) { |
| 516 | mErr << "Warning: Skipping \"" << fqInstanceName << "\": " |
| 517 | << "cannot be fetched from service manager (null)"; |
| 518 | status |= DUMP_BINDERIZED_ERROR; |
| 519 | continue; |
| 520 | } |
| 521 | auto debugRet = timeoutIPC(service, &IBase::getDebugInfo, [&] (const auto &debugInfo) { |
| 522 | allDebugInfos[fqInstanceName] = debugInfo; |
| 523 | if (debugInfo.pid >= 0) { |
| 524 | allPids[static_cast<pid_t>(debugInfo.pid)].clear(); |
| 525 | } |
| 526 | }); |
| 527 | if (!debugRet.isOk()) { |
| 528 | mErr << "Warning: Skipping \"" << fqInstanceName << "\": " |
| 529 | << "debugging information cannot be retrieved:" |
| 530 | << debugRet.description() << std::endl; |
| 531 | status |= DUMP_BINDERIZED_ERROR; |
| 532 | } |
| 533 | } |
| 534 | for (auto &pair : allPids) { |
| 535 | pid_t serverPid = pair.first; |
| 536 | if (!getReferencedPids(serverPid, &allPids[serverPid])) { |
| 537 | mErr << "Warning: no information for PID " << serverPid |
| 538 | << ", are you root?" << std::endl; |
| 539 | status |= DUMP_BINDERIZED_ERROR; |
| 540 | } |
| 541 | } |
| 542 | for (const auto &fqInstanceName : fqInstanceNames) { |
| 543 | auto it = allDebugInfos.find(fqInstanceName); |
| 544 | if (it == allDebugInfos.end()) { |
Yifan Hong | a3b8709 | 2017-03-02 19:19:29 -0800 | [diff] [blame] | 545 | putEntry(HWSERVICEMANAGER_LIST, { |
Steven Moreland | 9b5c15d | 2017-02-28 17:52:58 -0800 | [diff] [blame] | 546 | .interfaceName = fqInstanceName, |
| 547 | .transport = mode, |
| 548 | .serverPid = NO_PID, |
| 549 | .serverObjectAddress = NO_PTR, |
Yifan Hong | 4b86549 | 2017-02-28 19:38:24 -0800 | [diff] [blame] | 550 | .clientPids = {}, |
Yifan Hong | a3b8709 | 2017-03-02 19:19:29 -0800 | [diff] [blame] | 551 | .arch = ARCH_UNKNOWN |
Steven Moreland | 9b5c15d | 2017-02-28 17:52:58 -0800 | [diff] [blame] | 552 | }); |
| 553 | continue; |
| 554 | } |
| 555 | const DebugInfo &info = it->second; |
Yifan Hong | a3b8709 | 2017-03-02 19:19:29 -0800 | [diff] [blame] | 556 | putEntry(HWSERVICEMANAGER_LIST, { |
Steven Moreland | 9b5c15d | 2017-02-28 17:52:58 -0800 | [diff] [blame] | 557 | .interfaceName = fqInstanceName, |
| 558 | .transport = mode, |
| 559 | .serverPid = info.pid, |
| 560 | .serverObjectAddress = info.ptr, |
| 561 | .clientPids = info.pid == NO_PID || info.ptr == NO_PTR |
Yifan Hong | 4b86549 | 2017-02-28 19:38:24 -0800 | [diff] [blame] | 562 | ? Pids{} : allPids[info.pid][info.ptr], |
Yifan Hong | b447902 | 2017-03-02 16:54:11 -0800 | [diff] [blame] | 563 | .arch = fromBaseArchitecture(info.arch), |
Steven Moreland | 9b5c15d | 2017-02-28 17:52:58 -0800 | [diff] [blame] | 564 | }); |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 565 | } |
| 566 | return status; |
| 567 | } |
| 568 | |
| 569 | Status Lshal::fetch() { |
| 570 | Status status = OK; |
| 571 | auto bManager = ::android::hardware::defaultServiceManager(); |
| 572 | if (bManager == nullptr) { |
| 573 | mErr << "Failed to get defaultServiceManager()!" << std::endl; |
| 574 | status |= NO_BINDERIZED_MANAGER; |
| 575 | } else { |
| 576 | status |= fetchBinderized(bManager); |
| 577 | // Passthrough PIDs are registered to the binderized manager as well. |
| 578 | status |= fetchPassthrough(bManager); |
| 579 | } |
| 580 | |
| 581 | auto pManager = ::android::hardware::getPassthroughServiceManager(); |
| 582 | if (pManager == nullptr) { |
| 583 | mErr << "Failed to get getPassthroughServiceManager()!" << std::endl; |
| 584 | status |= NO_PASSTHROUGH_MANAGER; |
| 585 | } else { |
| 586 | status |= fetchAllLibraries(pManager); |
| 587 | } |
| 588 | return status; |
| 589 | } |
| 590 | |
| 591 | void Lshal::usage() const { |
| 592 | mErr |
| 593 | << "usage: lshal" << std::endl |
Yifan Hong | a3b8709 | 2017-03-02 19:19:29 -0800 | [diff] [blame] | 594 | << " Dump all hals with default ordering and columns [-ipc]." << std::endl |
Yifan Hong | b447902 | 2017-03-02 16:54:11 -0800 | [diff] [blame] | 595 | << " lshal [--interface|-i] [--transport|-t] [-r|--arch]" << std::endl |
Yifan Hong | 38d53e0 | 2017-02-13 17:51:59 -0800 | [diff] [blame] | 596 | << " [--pid|-p] [--address|-a] [--clients|-c] [--cmdline|-m]" << std::endl |
Yifan Hong | 4b86549 | 2017-02-28 19:38:24 -0800 | [diff] [blame] | 597 | << " [--sort={interface|i|pid|p}] [--init-vintf[=path]]" << std::endl |
Yifan Hong | 38d53e0 | 2017-02-13 17:51:59 -0800 | [diff] [blame] | 598 | << " -i, --interface: print the interface name column" << std::endl |
| 599 | << " -n, --instance: print the instance name column" << std::endl |
| 600 | << " -t, --transport: print the transport mode column" << std::endl |
Yifan Hong | b447902 | 2017-03-02 16:54:11 -0800 | [diff] [blame] | 601 | << " -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] | 602 | << " -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] | 603 | << " -a, --address: print the server object address column" << std::endl |
Yifan Hong | ae09a3d | 2017-02-14 17:33:50 -0800 | [diff] [blame] | 604 | << " -c, --clients: print the client PIDs, or client cmdlines if -m is set" |
| 605 | << std::endl |
| 606 | << " -m, --cmdline: print cmdline instead of PIDs" << std::endl |
Yifan Hong | 38d53e0 | 2017-02-13 17:51:59 -0800 | [diff] [blame] | 607 | << " --sort=i, --sort=interface: sort by interface name" << std::endl |
| 608 | << " --sort=p, --sort=pid: sort by server pid" << std::endl |
Yifan Hong | 4b86549 | 2017-02-28 19:38:24 -0800 | [diff] [blame] | 609 | << " --init-vintf=path: form a skeleton HAL manifest to specified file " << std::endl |
| 610 | << " (stdout if no file specified)" << std::endl |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 611 | << " lshal [-h|--help]" << std::endl |
| 612 | << " -h, --help: show this help information." << std::endl; |
| 613 | } |
| 614 | |
| 615 | Status Lshal::parseArgs(int argc, char **argv) { |
| 616 | static struct option longOptions[] = { |
Yifan Hong | 38d53e0 | 2017-02-13 17:51:59 -0800 | [diff] [blame] | 617 | // long options with short alternatives |
| 618 | {"help", no_argument, 0, 'h' }, |
| 619 | {"interface", no_argument, 0, 'i' }, |
| 620 | {"transport", no_argument, 0, 't' }, |
Yifan Hong | b447902 | 2017-03-02 16:54:11 -0800 | [diff] [blame] | 621 | {"arch", no_argument, 0, 'r' }, |
Yifan Hong | 38d53e0 | 2017-02-13 17:51:59 -0800 | [diff] [blame] | 622 | {"pid", no_argument, 0, 'p' }, |
| 623 | {"address", no_argument, 0, 'a' }, |
| 624 | {"clients", no_argument, 0, 'c' }, |
Yifan Hong | ae09a3d | 2017-02-14 17:33:50 -0800 | [diff] [blame] | 625 | {"cmdline", no_argument, 0, 'm' }, |
Yifan Hong | 38d53e0 | 2017-02-13 17:51:59 -0800 | [diff] [blame] | 626 | |
| 627 | // long options without short alternatives |
| 628 | {"sort", required_argument, 0, 's' }, |
Yifan Hong | 4b86549 | 2017-02-28 19:38:24 -0800 | [diff] [blame] | 629 | {"init-vintf",optional_argument, 0, 'v' }, |
Yifan Hong | 38d53e0 | 2017-02-13 17:51:59 -0800 | [diff] [blame] | 630 | { 0, 0, 0, 0 } |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 631 | }; |
| 632 | |
| 633 | int optionIndex; |
| 634 | int c; |
| 635 | optind = 1; |
| 636 | for (;;) { |
| 637 | // 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] | 638 | c = getopt_long(argc, argv, "hitrpacm", longOptions, &optionIndex); |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 639 | if (c == -1) { |
| 640 | break; |
| 641 | } |
| 642 | switch (c) { |
Yifan Hong | 38d53e0 | 2017-02-13 17:51:59 -0800 | [diff] [blame] | 643 | case 's': { |
| 644 | if (strcmp(optarg, "interface") == 0 || strcmp(optarg, "i") == 0) { |
| 645 | mSortColumn = TableEntry::sortByInterfaceName; |
| 646 | } else if (strcmp(optarg, "pid") == 0 || strcmp(optarg, "p") == 0) { |
| 647 | mSortColumn = TableEntry::sortByServerPid; |
| 648 | } else { |
| 649 | mErr << "Unrecognized sorting column: " << optarg << std::endl; |
| 650 | usage(); |
| 651 | return USAGE; |
| 652 | } |
| 653 | break; |
| 654 | } |
Yifan Hong | 4b86549 | 2017-02-28 19:38:24 -0800 | [diff] [blame] | 655 | case 'v': { |
| 656 | if (optarg) { |
| 657 | mFileOutput = new std::ofstream{optarg}; |
| 658 | mOut = mFileOutput; |
| 659 | if (!mFileOutput.buf().is_open()) { |
| 660 | mErr << "Could not open file '" << optarg << "'." << std::endl; |
| 661 | return IO_ERROR; |
| 662 | } |
| 663 | } |
| 664 | mVintf = true; |
| 665 | } |
Yifan Hong | 38d53e0 | 2017-02-13 17:51:59 -0800 | [diff] [blame] | 666 | case 'i': { |
| 667 | mSelectedColumns |= ENABLE_INTERFACE_NAME; |
| 668 | break; |
| 669 | } |
| 670 | case 't': { |
| 671 | mSelectedColumns |= ENABLE_TRANSPORT; |
| 672 | break; |
| 673 | } |
Yifan Hong | b447902 | 2017-03-02 16:54:11 -0800 | [diff] [blame] | 674 | case 'r': { |
| 675 | mSelectedColumns |= ENABLE_ARCH; |
| 676 | break; |
| 677 | } |
Yifan Hong | 38d53e0 | 2017-02-13 17:51:59 -0800 | [diff] [blame] | 678 | case 'p': { |
| 679 | mSelectedColumns |= ENABLE_SERVER_PID; |
| 680 | break; |
| 681 | } |
| 682 | case 'a': { |
| 683 | mSelectedColumns |= ENABLE_SERVER_ADDR; |
| 684 | break; |
| 685 | } |
| 686 | case 'c': { |
| 687 | mSelectedColumns |= ENABLE_CLIENT_PIDS; |
| 688 | break; |
| 689 | } |
Yifan Hong | ae09a3d | 2017-02-14 17:33:50 -0800 | [diff] [blame] | 690 | case 'm': { |
| 691 | mEnableCmdlines = true; |
| 692 | break; |
| 693 | } |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 694 | case 'h': // falls through |
| 695 | default: // see unrecognized options |
| 696 | usage(); |
| 697 | return USAGE; |
| 698 | } |
| 699 | } |
Yifan Hong | 38d53e0 | 2017-02-13 17:51:59 -0800 | [diff] [blame] | 700 | |
| 701 | if (mSelectedColumns == 0) { |
Yifan Hong | a3b8709 | 2017-03-02 19:19:29 -0800 | [diff] [blame] | 702 | mSelectedColumns = ENABLE_INTERFACE_NAME | ENABLE_SERVER_PID | ENABLE_CLIENT_PIDS; |
Yifan Hong | 38d53e0 | 2017-02-13 17:51:59 -0800 | [diff] [blame] | 703 | } |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 704 | return OK; |
| 705 | } |
| 706 | |
| 707 | int Lshal::main(int argc, char **argv) { |
| 708 | Status status = parseArgs(argc, argv); |
| 709 | if (status != OK) { |
| 710 | return status; |
| 711 | } |
| 712 | status = fetch(); |
Yifan Hong | 38d53e0 | 2017-02-13 17:51:59 -0800 | [diff] [blame] | 713 | postprocess(); |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 714 | dump(); |
| 715 | return status; |
| 716 | } |
| 717 | |
Yifan Hong | a57dffb | 2017-02-21 14:59:00 -0800 | [diff] [blame] | 718 | void signalHandler(int sig) { |
| 719 | if (sig == SIGINT) { |
| 720 | int retVal; |
| 721 | pthread_exit(&retVal); |
| 722 | } |
| 723 | } |
| 724 | |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 725 | } // namespace lshal |
| 726 | } // namespace android |
| 727 | |
| 728 | int main(int argc, char **argv) { |
Yifan Hong | a57dffb | 2017-02-21 14:59:00 -0800 | [diff] [blame] | 729 | signal(SIGINT, ::android::lshal::signalHandler); |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 730 | return ::android::lshal::Lshal{}.main(argc, argv); |
| 731 | } |