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 | |
Andreas Huber | 28d3591 | 2017-03-24 13:14:11 -0700 | [diff] [blame^] | 28 | #include <android-base/logging.h> |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 29 | #include <android-base/parseint.h> |
| 30 | #include <android/hidl/manager/1.0/IServiceManager.h> |
| 31 | #include <hidl/ServiceManagement.h> |
Yifan Hong | 4b86549 | 2017-02-28 19:38:24 -0800 | [diff] [blame] | 32 | #include <hidl-util/FQName.h> |
Andreas Huber | 28d3591 | 2017-03-24 13:14:11 -0700 | [diff] [blame^] | 33 | #include <private/android_filesystem_config.h> |
| 34 | #include <sys/stat.h> |
Yifan Hong | 4b86549 | 2017-02-28 19:38:24 -0800 | [diff] [blame] | 35 | #include <vintf/HalManifest.h> |
| 36 | #include <vintf/parse_xml.h> |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 37 | |
Andreas Huber | 28d3591 | 2017-03-24 13:14:11 -0700 | [diff] [blame^] | 38 | #include "PipeRelay.h" |
Yifan Hong | e2dadf0 | 2017-02-14 15:43:31 -0800 | [diff] [blame] | 39 | #include "Timeout.h" |
| 40 | |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 41 | using ::android::hardware::hidl_string; |
| 42 | using ::android::hidl::manager::V1_0::IServiceManager; |
| 43 | |
| 44 | namespace android { |
| 45 | namespace lshal { |
| 46 | |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 47 | template <typename A> |
| 48 | std::string join(const A &components, const std::string &separator) { |
| 49 | std::stringstream out; |
| 50 | bool first = true; |
| 51 | for (const auto &component : components) { |
| 52 | if (!first) { |
| 53 | out << separator; |
| 54 | } |
| 55 | out << component; |
| 56 | |
| 57 | first = false; |
| 58 | } |
| 59 | return out.str(); |
| 60 | } |
| 61 | |
| 62 | static std::string toHexString(uint64_t t) { |
| 63 | std::ostringstream os; |
| 64 | os << std::hex << std::setfill('0') << std::setw(16) << t; |
| 65 | return os.str(); |
| 66 | } |
| 67 | |
Yifan Hong | 4b86549 | 2017-02-28 19:38:24 -0800 | [diff] [blame] | 68 | template<typename String> |
| 69 | static std::pair<String, String> splitFirst(const String &s, char c) { |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 70 | const char *pos = strchr(s.c_str(), c); |
| 71 | if (pos == nullptr) { |
| 72 | return {s, {}}; |
| 73 | } |
Yifan Hong | 4b86549 | 2017-02-28 19:38:24 -0800 | [diff] [blame] | 74 | return {String(s.c_str(), pos - s.c_str()), String(pos + 1)}; |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | static std::vector<std::string> split(const std::string &s, char c) { |
| 78 | std::vector<std::string> components{}; |
| 79 | size_t startPos = 0; |
| 80 | size_t matchPos; |
| 81 | while ((matchPos = s.find(c, startPos)) != std::string::npos) { |
| 82 | components.push_back(s.substr(startPos, matchPos - startPos)); |
| 83 | startPos = matchPos + 1; |
| 84 | } |
| 85 | |
| 86 | if (startPos <= s.length()) { |
| 87 | components.push_back(s.substr(startPos)); |
| 88 | } |
| 89 | return components; |
| 90 | } |
| 91 | |
Yifan Hong | 4b86549 | 2017-02-28 19:38:24 -0800 | [diff] [blame] | 92 | static void replaceAll(std::string *s, char from, char to) { |
| 93 | for (size_t i = 0; i < s->size(); ++i) { |
| 94 | if (s->at(i) == from) { |
| 95 | s->at(i) = to; |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | |
Yifan Hong | ae09a3d | 2017-02-14 17:33:50 -0800 | [diff] [blame] | 100 | std::string getCmdline(pid_t pid) { |
| 101 | std::ifstream ifs("/proc/" + std::to_string(pid) + "/cmdline"); |
| 102 | std::string cmdline; |
| 103 | if (!ifs.is_open()) { |
| 104 | return ""; |
| 105 | } |
| 106 | ifs >> cmdline; |
| 107 | return cmdline; |
| 108 | } |
| 109 | |
| 110 | const std::string &Lshal::getCmdline(pid_t pid) { |
| 111 | auto pair = mCmdlines.find(pid); |
| 112 | if (pair != mCmdlines.end()) { |
| 113 | return pair->second; |
| 114 | } |
| 115 | mCmdlines[pid] = ::android::lshal::getCmdline(pid); |
| 116 | return mCmdlines[pid]; |
| 117 | } |
| 118 | |
| 119 | void Lshal::removeDeadProcesses(Pids *pids) { |
| 120 | static const pid_t myPid = getpid(); |
| 121 | std::remove_if(pids->begin(), pids->end(), [this](auto pid) { |
| 122 | return pid == myPid || this->getCmdline(pid).empty(); |
| 123 | }); |
| 124 | } |
| 125 | |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 126 | bool Lshal::getReferencedPids( |
| 127 | pid_t serverPid, std::map<uint64_t, Pids> *objects) const { |
| 128 | |
| 129 | std::ifstream ifs("/d/binder/proc/" + std::to_string(serverPid)); |
| 130 | if (!ifs.is_open()) { |
| 131 | return false; |
| 132 | } |
| 133 | |
| 134 | static const std::regex prefix("^\\s*node \\d+:\\s+u([0-9a-f]+)\\s+c([0-9a-f]+)\\s+"); |
| 135 | |
| 136 | std::string line; |
| 137 | std::smatch match; |
| 138 | while(getline(ifs, line)) { |
| 139 | if (!std::regex_search(line, match, prefix)) { |
| 140 | // the line doesn't start with the correct prefix |
| 141 | continue; |
| 142 | } |
| 143 | std::string ptrString = "0x" + match.str(2); // use number after c |
| 144 | uint64_t ptr; |
| 145 | if (!::android::base::ParseUint(ptrString.c_str(), &ptr)) { |
| 146 | // Should not reach here, but just be tolerant. |
| 147 | mErr << "Could not parse number " << ptrString << std::endl; |
| 148 | continue; |
| 149 | } |
| 150 | const std::string proc = " proc "; |
| 151 | auto pos = line.rfind(proc); |
| 152 | if (pos != std::string::npos) { |
| 153 | for (const std::string &pidStr : split(line.substr(pos + proc.size()), ' ')) { |
| 154 | int32_t pid; |
| 155 | if (!::android::base::ParseInt(pidStr, &pid)) { |
| 156 | mErr << "Could not parse number " << pidStr << std::endl; |
| 157 | continue; |
| 158 | } |
| 159 | (*objects)[ptr].push_back(pid); |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | return true; |
| 164 | } |
| 165 | |
Yifan Hong | a3b8709 | 2017-03-02 19:19:29 -0800 | [diff] [blame] | 166 | void Lshal::forEachTable(const std::function<void(Table &)> &f) { |
Yifan Hong | 8ab3bee | 2017-03-08 14:01:11 -0800 | [diff] [blame] | 167 | f(mServicesTable); |
| 168 | f(mPassthroughRefTable); |
| 169 | f(mImplementationsTable); |
Yifan Hong | a3b8709 | 2017-03-02 19:19:29 -0800 | [diff] [blame] | 170 | } |
| 171 | void Lshal::forEachTable(const std::function<void(const Table &)> &f) const { |
Yifan Hong | 8ab3bee | 2017-03-08 14:01:11 -0800 | [diff] [blame] | 172 | f(mServicesTable); |
| 173 | f(mPassthroughRefTable); |
| 174 | f(mImplementationsTable); |
Yifan Hong | a3b8709 | 2017-03-02 19:19:29 -0800 | [diff] [blame] | 175 | } |
| 176 | |
Yifan Hong | 38d53e0 | 2017-02-13 17:51:59 -0800 | [diff] [blame] | 177 | void Lshal::postprocess() { |
Yifan Hong | a3b8709 | 2017-03-02 19:19:29 -0800 | [diff] [blame] | 178 | forEachTable([this](Table &table) { |
| 179 | if (mSortColumn) { |
| 180 | std::sort(table.begin(), table.end(), mSortColumn); |
Yifan Hong | ae09a3d | 2017-02-14 17:33:50 -0800 | [diff] [blame] | 181 | } |
Yifan Hong | a3b8709 | 2017-03-02 19:19:29 -0800 | [diff] [blame] | 182 | for (TableEntry &entry : table) { |
| 183 | entry.serverCmdline = getCmdline(entry.serverPid); |
| 184 | removeDeadProcesses(&entry.clientPids); |
| 185 | for (auto pid : entry.clientPids) { |
| 186 | entry.clientCmdlines.push_back(this->getCmdline(pid)); |
| 187 | } |
| 188 | } |
| 189 | }); |
Yifan Hong | 3fdbd9f | 2017-03-08 14:01:58 -0800 | [diff] [blame] | 190 | // use a double for loop here because lshal doesn't care about efficiency. |
| 191 | for (TableEntry &packageEntry : mImplementationsTable) { |
| 192 | std::string packageName = packageEntry.interfaceName; |
| 193 | FQName fqPackageName{packageName.substr(0, packageName.find("::"))}; |
| 194 | if (!fqPackageName.isValid()) { |
| 195 | continue; |
| 196 | } |
| 197 | for (TableEntry &interfaceEntry : mPassthroughRefTable) { |
| 198 | if (interfaceEntry.arch != ARCH_UNKNOWN) { |
| 199 | continue; |
| 200 | } |
| 201 | FQName interfaceName{splitFirst(interfaceEntry.interfaceName, '/').first}; |
| 202 | if (!interfaceName.isValid()) { |
| 203 | continue; |
| 204 | } |
| 205 | if (interfaceName.getPackageAndVersion() == fqPackageName) { |
| 206 | interfaceEntry.arch = packageEntry.arch; |
| 207 | } |
| 208 | } |
| 209 | } |
Yifan Hong | 38d53e0 | 2017-02-13 17:51:59 -0800 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | void Lshal::printLine( |
| 213 | const std::string &interfaceName, |
Yifan Hong | b447902 | 2017-03-02 16:54:11 -0800 | [diff] [blame] | 214 | const std::string &transport, |
| 215 | const std::string &arch, |
| 216 | const std::string &server, |
Yifan Hong | ae09a3d | 2017-02-14 17:33:50 -0800 | [diff] [blame] | 217 | const std::string &serverCmdline, |
| 218 | const std::string &address, const std::string &clients, |
| 219 | const std::string &clientCmdlines) const { |
Yifan Hong | 38d53e0 | 2017-02-13 17:51:59 -0800 | [diff] [blame] | 220 | if (mSelectedColumns & ENABLE_INTERFACE_NAME) |
| 221 | mOut << std::setw(80) << interfaceName << "\t"; |
| 222 | if (mSelectedColumns & ENABLE_TRANSPORT) |
| 223 | mOut << std::setw(10) << transport << "\t"; |
Yifan Hong | b447902 | 2017-03-02 16:54:11 -0800 | [diff] [blame] | 224 | if (mSelectedColumns & ENABLE_ARCH) |
| 225 | mOut << std::setw(5) << arch << "\t"; |
Yifan Hong | ae09a3d | 2017-02-14 17:33:50 -0800 | [diff] [blame] | 226 | if (mSelectedColumns & ENABLE_SERVER_PID) { |
| 227 | if (mEnableCmdlines) { |
| 228 | mOut << std::setw(15) << serverCmdline << "\t"; |
| 229 | } else { |
| 230 | mOut << std::setw(5) << server << "\t"; |
| 231 | } |
| 232 | } |
Yifan Hong | 38d53e0 | 2017-02-13 17:51:59 -0800 | [diff] [blame] | 233 | if (mSelectedColumns & ENABLE_SERVER_ADDR) |
| 234 | mOut << std::setw(16) << address << "\t"; |
Yifan Hong | ae09a3d | 2017-02-14 17:33:50 -0800 | [diff] [blame] | 235 | if (mSelectedColumns & ENABLE_CLIENT_PIDS) { |
| 236 | if (mEnableCmdlines) { |
| 237 | mOut << std::setw(0) << clientCmdlines; |
| 238 | } else { |
| 239 | mOut << std::setw(0) << clients; |
| 240 | } |
| 241 | } |
Yifan Hong | 38d53e0 | 2017-02-13 17:51:59 -0800 | [diff] [blame] | 242 | mOut << std::endl; |
| 243 | } |
| 244 | |
Yifan Hong | 4b86549 | 2017-02-28 19:38:24 -0800 | [diff] [blame] | 245 | void Lshal::dumpVintf() const { |
| 246 | vintf::HalManifest manifest; |
Yifan Hong | a3b8709 | 2017-03-02 19:19:29 -0800 | [diff] [blame] | 247 | forEachTable([this, &manifest] (const Table &table) { |
| 248 | for (const TableEntry &entry : table) { |
Yifan Hong | 4b86549 | 2017-02-28 19:38:24 -0800 | [diff] [blame] | 249 | |
Yifan Hong | a3b8709 | 2017-03-02 19:19:29 -0800 | [diff] [blame] | 250 | std::string fqInstanceName = entry.interfaceName; |
Yifan Hong | 4b86549 | 2017-02-28 19:38:24 -0800 | [diff] [blame] | 251 | |
Yifan Hong | a3b8709 | 2017-03-02 19:19:29 -0800 | [diff] [blame] | 252 | if (&table == &mImplementationsTable) { |
| 253 | // Quick hack to work around *'s |
| 254 | replaceAll(&fqInstanceName, '*', 'D'); |
| 255 | } |
| 256 | auto splittedFqInstanceName = splitFirst(fqInstanceName, '/'); |
| 257 | FQName fqName(splittedFqInstanceName.first); |
| 258 | if (!fqName.isValid()) { |
| 259 | mErr << "Warning: '" << splittedFqInstanceName.first |
| 260 | << "' is not a valid FQName." << std::endl; |
Yifan Hong | 4b86549 | 2017-02-28 19:38:24 -0800 | [diff] [blame] | 261 | continue; |
| 262 | } |
Yifan Hong | a3b8709 | 2017-03-02 19:19:29 -0800 | [diff] [blame] | 263 | // Strip out system libs. |
Steven Moreland | 9b6cd60 | 2017-03-22 14:22:55 -0700 | [diff] [blame] | 264 | if (fqName.inPackage("android.hidl") || |
| 265 | fqName.inPackage("android.frameworks") || |
| 266 | fqName.inPackage("android.system")) { |
Yifan Hong | a3b8709 | 2017-03-02 19:19:29 -0800 | [diff] [blame] | 267 | continue; |
| 268 | } |
| 269 | std::string interfaceName = |
| 270 | &table == &mImplementationsTable ? "" : fqName.name(); |
| 271 | std::string instanceName = |
| 272 | &table == &mImplementationsTable ? "" : splittedFqInstanceName.second; |
| 273 | |
| 274 | vintf::Transport transport; |
Yifan Hong | 3fdbd9f | 2017-03-08 14:01:58 -0800 | [diff] [blame] | 275 | vintf::Arch arch; |
Yifan Hong | a3b8709 | 2017-03-02 19:19:29 -0800 | [diff] [blame] | 276 | if (entry.transport == "hwbinder") { |
| 277 | transport = vintf::Transport::HWBINDER; |
Yifan Hong | 3fdbd9f | 2017-03-08 14:01:58 -0800 | [diff] [blame] | 278 | arch = vintf::Arch::ARCH_EMPTY; |
Yifan Hong | a3b8709 | 2017-03-02 19:19:29 -0800 | [diff] [blame] | 279 | } else if (entry.transport == "passthrough") { |
| 280 | transport = vintf::Transport::PASSTHROUGH; |
Yifan Hong | 3fdbd9f | 2017-03-08 14:01:58 -0800 | [diff] [blame] | 281 | switch (entry.arch) { |
| 282 | case lshal::ARCH32: |
| 283 | arch = vintf::Arch::ARCH_32; break; |
| 284 | case lshal::ARCH64: |
| 285 | arch = vintf::Arch::ARCH_64; break; |
| 286 | case lshal::ARCH_BOTH: |
| 287 | arch = vintf::Arch::ARCH_32_64; break; |
| 288 | case lshal::ARCH_UNKNOWN: // fallthrough |
| 289 | default: |
| 290 | mErr << "Warning: '" << fqName.package() |
| 291 | << "' doesn't have bitness info, assuming 32+64." << std::endl; |
| 292 | arch = vintf::Arch::ARCH_32_64; |
| 293 | } |
Yifan Hong | 4b86549 | 2017-02-28 19:38:24 -0800 | [diff] [blame] | 294 | } else { |
Yifan Hong | a3b8709 | 2017-03-02 19:19:29 -0800 | [diff] [blame] | 295 | mErr << "Warning: '" << entry.transport << "' is not a valid transport." << std::endl; |
| 296 | continue; |
| 297 | } |
| 298 | |
| 299 | vintf::ManifestHal *hal = manifest.getHal(fqName.package()); |
| 300 | if (hal == nullptr) { |
| 301 | if (!manifest.add(vintf::ManifestHal{ |
| 302 | .format = vintf::HalFormat::HIDL, |
| 303 | .name = fqName.package(), |
| 304 | .impl = {.implLevel = vintf::ImplLevel::GENERIC, .impl = ""}, |
Yifan Hong | 3fdbd9f | 2017-03-08 14:01:58 -0800 | [diff] [blame] | 305 | .transportArch = {transport, arch} |
Yifan Hong | a3b8709 | 2017-03-02 19:19:29 -0800 | [diff] [blame] | 306 | })) { |
| 307 | mErr << "Warning: cannot add hal '" << fqInstanceName << "'" << std::endl; |
| 308 | continue; |
| 309 | } |
| 310 | hal = manifest.getHal(fqName.package()); |
| 311 | } |
| 312 | if (hal == nullptr) { |
| 313 | mErr << "Warning: cannot get hal '" << fqInstanceName |
| 314 | << "' after adding it" << std::endl; |
| 315 | continue; |
| 316 | } |
| 317 | vintf::Version version{fqName.getPackageMajorVersion(), fqName.getPackageMinorVersion()}; |
| 318 | if (std::find(hal->versions.begin(), hal->versions.end(), version) == hal->versions.end()) { |
| 319 | hal->versions.push_back(version); |
| 320 | } |
| 321 | if (&table != &mImplementationsTable) { |
| 322 | auto it = hal->interfaces.find(interfaceName); |
| 323 | if (it == hal->interfaces.end()) { |
| 324 | hal->interfaces.insert({interfaceName, {interfaceName, {{instanceName}}}}); |
| 325 | } else { |
| 326 | it->second.instances.insert(instanceName); |
| 327 | } |
Yifan Hong | 4b86549 | 2017-02-28 19:38:24 -0800 | [diff] [blame] | 328 | } |
| 329 | } |
Yifan Hong | a3b8709 | 2017-03-02 19:19:29 -0800 | [diff] [blame] | 330 | }); |
Yifan Hong | 4b86549 | 2017-02-28 19:38:24 -0800 | [diff] [blame] | 331 | mOut << vintf::gHalManifestConverter(manifest); |
| 332 | } |
| 333 | |
Yifan Hong | b447902 | 2017-03-02 16:54:11 -0800 | [diff] [blame] | 334 | static const std::string &getArchString(Architecture arch) { |
| 335 | static const std::string sStr64 = "64"; |
| 336 | static const std::string sStr32 = "32"; |
Yifan Hong | 1071c1b | 2017-03-03 10:57:43 -0800 | [diff] [blame] | 337 | static const std::string sStrBoth = "32+64"; |
Yifan Hong | b447902 | 2017-03-02 16:54:11 -0800 | [diff] [blame] | 338 | static const std::string sStrUnknown = ""; |
| 339 | switch (arch) { |
| 340 | case ARCH64: |
| 341 | return sStr64; |
| 342 | case ARCH32: |
| 343 | return sStr32; |
| 344 | case ARCH_BOTH: |
| 345 | return sStrBoth; |
| 346 | case ARCH_UNKNOWN: // fall through |
| 347 | default: |
| 348 | return sStrUnknown; |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | static Architecture fromBaseArchitecture(::android::hidl::base::V1_0::DebugInfo::Architecture a) { |
| 353 | switch (a) { |
| 354 | case ::android::hidl::base::V1_0::DebugInfo::Architecture::IS_64BIT: |
| 355 | return ARCH64; |
| 356 | case ::android::hidl::base::V1_0::DebugInfo::Architecture::IS_32BIT: |
| 357 | return ARCH32; |
| 358 | case ::android::hidl::base::V1_0::DebugInfo::Architecture::UNKNOWN: // fallthrough |
| 359 | default: |
| 360 | return ARCH_UNKNOWN; |
| 361 | } |
| 362 | } |
| 363 | |
Andreas Huber | 28d3591 | 2017-03-24 13:14:11 -0700 | [diff] [blame^] | 364 | // A unique_ptr type using a custom deleter function. |
| 365 | template<typename T> |
| 366 | using deleted_unique_ptr = std::unique_ptr<T, std::function<void(T *)> >; |
| 367 | |
| 368 | void Lshal::emitDebugInfo( |
| 369 | const sp<IServiceManager> &serviceManager, |
| 370 | const std::string &interfaceName, |
| 371 | const std::string &instanceName) const { |
| 372 | using android::hidl::base::V1_0::IBase; |
| 373 | |
| 374 | hardware::Return<sp<IBase>> retBase = |
| 375 | serviceManager->get(interfaceName, instanceName); |
| 376 | |
| 377 | sp<IBase> base; |
| 378 | if (!retBase.isOk() || (base = retBase) == nullptr) { |
| 379 | // There's a small race, where a service instantiated while collecting |
| 380 | // the list of services has by now terminated, so this isn't anything |
| 381 | // to be concerned about. |
| 382 | return; |
| 383 | } |
| 384 | |
| 385 | PipeRelay relay(mOut.buf()); |
| 386 | |
| 387 | if (relay.initCheck() != OK) { |
| 388 | LOG(ERROR) << "PipeRelay::initCheck() FAILED w/ " << relay.initCheck(); |
| 389 | return; |
| 390 | } |
| 391 | |
| 392 | deleted_unique_ptr<native_handle_t> fdHandle( |
| 393 | native_handle_create(1 /* numFds */, 0 /* numInts */), |
| 394 | native_handle_delete); |
| 395 | |
| 396 | fdHandle->data[0] = relay.fd(); |
| 397 | |
| 398 | hardware::hidl_vec<hardware::hidl_string> options; |
| 399 | hardware::Return<void> ret = base->debug(fdHandle.get(), options); |
| 400 | |
| 401 | if (!ret.isOk()) { |
| 402 | LOG(ERROR) |
| 403 | << interfaceName |
| 404 | << "::debug(...) FAILED. (instance " |
| 405 | << instanceName |
| 406 | << ")"; |
| 407 | } |
| 408 | } |
| 409 | |
Yifan Hong | a3b8709 | 2017-03-02 19:19:29 -0800 | [diff] [blame] | 410 | void Lshal::dumpTable() { |
| 411 | mServicesTable.description = |
| 412 | "All binderized services (registered services through hwservicemanager)"; |
| 413 | mPassthroughRefTable.description = |
Yifan Hong | 1071c1b | 2017-03-03 10:57:43 -0800 | [diff] [blame] | 414 | "All interfaces that getService() has ever return as a passthrough interface;\n" |
Yifan Hong | a3b8709 | 2017-03-02 19:19:29 -0800 | [diff] [blame] | 415 | "PIDs / processes shown below might be inaccurate because the process\n" |
Yifan Hong | 1071c1b | 2017-03-03 10:57:43 -0800 | [diff] [blame] | 416 | "might have relinquished the interface or might have died.\n" |
Yifan Hong | a3b8709 | 2017-03-02 19:19:29 -0800 | [diff] [blame] | 417 | "The Server / Server CMD column can be ignored.\n" |
Yifan Hong | 1071c1b | 2017-03-03 10:57:43 -0800 | [diff] [blame] | 418 | "The Clients / Clients CMD column shows all process that have ever dlopen'ed \n" |
| 419 | "the library and successfully fetched the passthrough implementation."; |
Yifan Hong | a3b8709 | 2017-03-02 19:19:29 -0800 | [diff] [blame] | 420 | mImplementationsTable.description = |
| 421 | "All available passthrough implementations (all -impl.so files)"; |
| 422 | forEachTable([this] (const Table &table) { |
| 423 | mOut << table.description << std::endl; |
| 424 | mOut << std::left; |
| 425 | printLine("Interface", "Transport", "Arch", "Server", "Server CMD", |
| 426 | "PTR", "Clients", "Clients CMD"); |
Andreas Huber | 28d3591 | 2017-03-24 13:14:11 -0700 | [diff] [blame^] | 427 | |
| 428 | // We're only interested in dumping debug info for already |
| 429 | // instantiated services. There's little value in dumping the |
| 430 | // debug info for a service we create on the fly, so we only operate |
| 431 | // on the "mServicesTable". |
| 432 | sp<IServiceManager> serviceManager; |
| 433 | if (mEmitDebugInfo && &table == &mServicesTable) { |
| 434 | serviceManager = ::android::hardware::defaultServiceManager(); |
| 435 | } |
| 436 | |
Yifan Hong | a3b8709 | 2017-03-02 19:19:29 -0800 | [diff] [blame] | 437 | for (const auto &entry : table) { |
| 438 | printLine(entry.interfaceName, |
| 439 | entry.transport, |
| 440 | getArchString(entry.arch), |
| 441 | entry.serverPid == NO_PID ? "N/A" : std::to_string(entry.serverPid), |
| 442 | entry.serverCmdline, |
| 443 | entry.serverObjectAddress == NO_PTR ? "N/A" : toHexString(entry.serverObjectAddress), |
| 444 | join(entry.clientPids, " "), |
| 445 | join(entry.clientCmdlines, ";")); |
Andreas Huber | 28d3591 | 2017-03-24 13:14:11 -0700 | [diff] [blame^] | 446 | |
| 447 | if (serviceManager != nullptr) { |
| 448 | auto pair = splitFirst(entry.interfaceName, '/'); |
| 449 | emitDebugInfo(serviceManager, pair.first, pair.second); |
| 450 | } |
Yifan Hong | a3b8709 | 2017-03-02 19:19:29 -0800 | [diff] [blame] | 451 | } |
| 452 | mOut << std::endl; |
| 453 | }); |
| 454 | |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 455 | } |
| 456 | |
Yifan Hong | 4b86549 | 2017-02-28 19:38:24 -0800 | [diff] [blame] | 457 | void Lshal::dump() { |
| 458 | if (mVintf) { |
| 459 | dumpVintf(); |
| 460 | if (!!mFileOutput) { |
| 461 | mFileOutput.buf().close(); |
| 462 | delete &mFileOutput.buf(); |
| 463 | mFileOutput = nullptr; |
| 464 | } |
| 465 | mOut = std::cout; |
| 466 | } else { |
| 467 | dumpTable(); |
| 468 | } |
| 469 | } |
| 470 | |
Yifan Hong | a3b8709 | 2017-03-02 19:19:29 -0800 | [diff] [blame] | 471 | void Lshal::putEntry(TableEntrySource source, TableEntry &&entry) { |
| 472 | Table *table = nullptr; |
| 473 | switch (source) { |
| 474 | case HWSERVICEMANAGER_LIST : |
| 475 | table = &mServicesTable; break; |
| 476 | case PTSERVICEMANAGER_REG_CLIENT : |
| 477 | table = &mPassthroughRefTable; break; |
| 478 | case LIST_DLLIB : |
| 479 | table = &mImplementationsTable; break; |
| 480 | default: |
| 481 | mErr << "Error: Unknown source of entry " << source << std::endl; |
| 482 | } |
| 483 | if (table) { |
| 484 | table->entries.push_back(std::forward<TableEntry>(entry)); |
| 485 | } |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | Status Lshal::fetchAllLibraries(const sp<IServiceManager> &manager) { |
| 489 | using namespace ::android::hardware; |
| 490 | using namespace ::android::hidl::manager::V1_0; |
| 491 | using namespace ::android::hidl::base::V1_0; |
Yifan Hong | b447902 | 2017-03-02 16:54:11 -0800 | [diff] [blame] | 492 | auto ret = timeoutIPC(manager, &IServiceManager::debugDump, [&] (const auto &infos) { |
| 493 | std::map<std::string, TableEntry> entries; |
| 494 | for (const auto &info : infos) { |
| 495 | std::string interfaceName = std::string{info.interfaceName.c_str()} + "/" + |
| 496 | std::string{info.instanceName.c_str()}; |
Yifan Hong | 1071c1b | 2017-03-03 10:57:43 -0800 | [diff] [blame] | 497 | entries.emplace(interfaceName, TableEntry{ |
Yifan Hong | b447902 | 2017-03-02 16:54:11 -0800 | [diff] [blame] | 498 | .interfaceName = interfaceName, |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 499 | .transport = "passthrough", |
| 500 | .serverPid = NO_PID, |
| 501 | .serverObjectAddress = NO_PTR, |
Yifan Hong | 4b86549 | 2017-02-28 19:38:24 -0800 | [diff] [blame] | 502 | .clientPids = {}, |
Yifan Hong | a3b8709 | 2017-03-02 19:19:29 -0800 | [diff] [blame] | 503 | .arch = ARCH_UNKNOWN |
Yifan Hong | b447902 | 2017-03-02 16:54:11 -0800 | [diff] [blame] | 504 | }).first->second.arch |= fromBaseArchitecture(info.arch); |
| 505 | } |
| 506 | for (auto &&pair : entries) { |
Yifan Hong | a3b8709 | 2017-03-02 19:19:29 -0800 | [diff] [blame] | 507 | putEntry(LIST_DLLIB, std::move(pair.second)); |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 508 | } |
| 509 | }); |
| 510 | if (!ret.isOk()) { |
| 511 | mErr << "Error: Failed to call list on getPassthroughServiceManager(): " |
| 512 | << ret.description() << std::endl; |
| 513 | return DUMP_ALL_LIBS_ERROR; |
| 514 | } |
| 515 | return OK; |
| 516 | } |
| 517 | |
| 518 | Status Lshal::fetchPassthrough(const sp<IServiceManager> &manager) { |
| 519 | using namespace ::android::hardware; |
Yifan Hong | b447902 | 2017-03-02 16:54:11 -0800 | [diff] [blame] | 520 | using namespace ::android::hardware::details; |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 521 | using namespace ::android::hidl::manager::V1_0; |
| 522 | using namespace ::android::hidl::base::V1_0; |
Yifan Hong | e2dadf0 | 2017-02-14 15:43:31 -0800 | [diff] [blame] | 523 | auto ret = timeoutIPC(manager, &IServiceManager::debugDump, [&] (const auto &infos) { |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 524 | for (const auto &info : infos) { |
Steven Moreland | 56d2d51 | 2017-03-21 12:17:55 -0700 | [diff] [blame] | 525 | if (info.clientPids.size() <= 0) { |
| 526 | continue; |
| 527 | } |
Yifan Hong | a3b8709 | 2017-03-02 19:19:29 -0800 | [diff] [blame] | 528 | putEntry(PTSERVICEMANAGER_REG_CLIENT, { |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 529 | .interfaceName = |
| 530 | std::string{info.interfaceName.c_str()} + "/" + |
| 531 | std::string{info.instanceName.c_str()}, |
| 532 | .transport = "passthrough", |
| 533 | .serverPid = info.clientPids.size() == 1 ? info.clientPids[0] : NO_PID, |
| 534 | .serverObjectAddress = NO_PTR, |
Yifan Hong | 4b86549 | 2017-02-28 19:38:24 -0800 | [diff] [blame] | 535 | .clientPids = info.clientPids, |
Yifan Hong | a3b8709 | 2017-03-02 19:19:29 -0800 | [diff] [blame] | 536 | .arch = fromBaseArchitecture(info.arch) |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 537 | }); |
| 538 | } |
| 539 | }); |
| 540 | if (!ret.isOk()) { |
| 541 | mErr << "Error: Failed to call debugDump on defaultServiceManager(): " |
| 542 | << ret.description() << std::endl; |
| 543 | return DUMP_PASSTHROUGH_ERROR; |
| 544 | } |
| 545 | return OK; |
| 546 | } |
| 547 | |
| 548 | Status Lshal::fetchBinderized(const sp<IServiceManager> &manager) { |
| 549 | using namespace ::std; |
| 550 | using namespace ::android::hardware; |
| 551 | using namespace ::android::hidl::manager::V1_0; |
| 552 | using namespace ::android::hidl::base::V1_0; |
| 553 | const std::string mode = "hwbinder"; |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 554 | |
Steven Moreland | 9b5c15d | 2017-02-28 17:52:58 -0800 | [diff] [blame] | 555 | hidl_vec<hidl_string> fqInstanceNames; |
| 556 | // copying out for timeoutIPC |
| 557 | auto listRet = timeoutIPC(manager, &IServiceManager::list, [&] (const auto &names) { |
| 558 | fqInstanceNames = names; |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 559 | }); |
| 560 | if (!listRet.isOk()) { |
| 561 | mErr << "Error: Failed to list services for " << mode << ": " |
| 562 | << listRet.description() << std::endl; |
Steven Moreland | 9b5c15d | 2017-02-28 17:52:58 -0800 | [diff] [blame] | 563 | return DUMP_BINDERIZED_ERROR; |
| 564 | } |
| 565 | |
| 566 | Status status = OK; |
| 567 | // server pid, .ptr value of binder object, child pids |
| 568 | std::map<std::string, DebugInfo> allDebugInfos; |
| 569 | std::map<pid_t, std::map<uint64_t, Pids>> allPids; |
| 570 | for (const auto &fqInstanceName : fqInstanceNames) { |
Yifan Hong | 4b86549 | 2017-02-28 19:38:24 -0800 | [diff] [blame] | 571 | const auto pair = splitFirst(fqInstanceName, '/'); |
Steven Moreland | 9b5c15d | 2017-02-28 17:52:58 -0800 | [diff] [blame] | 572 | const auto &serviceName = pair.first; |
| 573 | const auto &instanceName = pair.second; |
| 574 | auto getRet = timeoutIPC(manager, &IServiceManager::get, serviceName, instanceName); |
| 575 | if (!getRet.isOk()) { |
| 576 | mErr << "Warning: Skipping \"" << fqInstanceName << "\": " |
| 577 | << "cannot be fetched from service manager:" |
| 578 | << getRet.description() << std::endl; |
| 579 | status |= DUMP_BINDERIZED_ERROR; |
| 580 | continue; |
| 581 | } |
| 582 | sp<IBase> service = getRet; |
| 583 | if (service == nullptr) { |
| 584 | mErr << "Warning: Skipping \"" << fqInstanceName << "\": " |
| 585 | << "cannot be fetched from service manager (null)"; |
| 586 | status |= DUMP_BINDERIZED_ERROR; |
| 587 | continue; |
| 588 | } |
| 589 | auto debugRet = timeoutIPC(service, &IBase::getDebugInfo, [&] (const auto &debugInfo) { |
| 590 | allDebugInfos[fqInstanceName] = debugInfo; |
| 591 | if (debugInfo.pid >= 0) { |
| 592 | allPids[static_cast<pid_t>(debugInfo.pid)].clear(); |
| 593 | } |
| 594 | }); |
| 595 | if (!debugRet.isOk()) { |
| 596 | mErr << "Warning: Skipping \"" << fqInstanceName << "\": " |
| 597 | << "debugging information cannot be retrieved:" |
| 598 | << debugRet.description() << std::endl; |
| 599 | status |= DUMP_BINDERIZED_ERROR; |
| 600 | } |
| 601 | } |
| 602 | for (auto &pair : allPids) { |
| 603 | pid_t serverPid = pair.first; |
| 604 | if (!getReferencedPids(serverPid, &allPids[serverPid])) { |
| 605 | mErr << "Warning: no information for PID " << serverPid |
| 606 | << ", are you root?" << std::endl; |
| 607 | status |= DUMP_BINDERIZED_ERROR; |
| 608 | } |
| 609 | } |
| 610 | for (const auto &fqInstanceName : fqInstanceNames) { |
| 611 | auto it = allDebugInfos.find(fqInstanceName); |
| 612 | if (it == allDebugInfos.end()) { |
Yifan Hong | a3b8709 | 2017-03-02 19:19:29 -0800 | [diff] [blame] | 613 | putEntry(HWSERVICEMANAGER_LIST, { |
Steven Moreland | 9b5c15d | 2017-02-28 17:52:58 -0800 | [diff] [blame] | 614 | .interfaceName = fqInstanceName, |
| 615 | .transport = mode, |
| 616 | .serverPid = NO_PID, |
| 617 | .serverObjectAddress = NO_PTR, |
Yifan Hong | 4b86549 | 2017-02-28 19:38:24 -0800 | [diff] [blame] | 618 | .clientPids = {}, |
Yifan Hong | a3b8709 | 2017-03-02 19:19:29 -0800 | [diff] [blame] | 619 | .arch = ARCH_UNKNOWN |
Steven Moreland | 9b5c15d | 2017-02-28 17:52:58 -0800 | [diff] [blame] | 620 | }); |
| 621 | continue; |
| 622 | } |
| 623 | const DebugInfo &info = it->second; |
Yifan Hong | a3b8709 | 2017-03-02 19:19:29 -0800 | [diff] [blame] | 624 | putEntry(HWSERVICEMANAGER_LIST, { |
Steven Moreland | 9b5c15d | 2017-02-28 17:52:58 -0800 | [diff] [blame] | 625 | .interfaceName = fqInstanceName, |
| 626 | .transport = mode, |
| 627 | .serverPid = info.pid, |
| 628 | .serverObjectAddress = info.ptr, |
| 629 | .clientPids = info.pid == NO_PID || info.ptr == NO_PTR |
Yifan Hong | 4b86549 | 2017-02-28 19:38:24 -0800 | [diff] [blame] | 630 | ? Pids{} : allPids[info.pid][info.ptr], |
Yifan Hong | b447902 | 2017-03-02 16:54:11 -0800 | [diff] [blame] | 631 | .arch = fromBaseArchitecture(info.arch), |
Steven Moreland | 9b5c15d | 2017-02-28 17:52:58 -0800 | [diff] [blame] | 632 | }); |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 633 | } |
| 634 | return status; |
| 635 | } |
| 636 | |
| 637 | Status Lshal::fetch() { |
| 638 | Status status = OK; |
| 639 | auto bManager = ::android::hardware::defaultServiceManager(); |
| 640 | if (bManager == nullptr) { |
| 641 | mErr << "Failed to get defaultServiceManager()!" << std::endl; |
| 642 | status |= NO_BINDERIZED_MANAGER; |
| 643 | } else { |
| 644 | status |= fetchBinderized(bManager); |
| 645 | // Passthrough PIDs are registered to the binderized manager as well. |
| 646 | status |= fetchPassthrough(bManager); |
| 647 | } |
| 648 | |
| 649 | auto pManager = ::android::hardware::getPassthroughServiceManager(); |
| 650 | if (pManager == nullptr) { |
| 651 | mErr << "Failed to get getPassthroughServiceManager()!" << std::endl; |
| 652 | status |= NO_PASSTHROUGH_MANAGER; |
| 653 | } else { |
| 654 | status |= fetchAllLibraries(pManager); |
| 655 | } |
| 656 | return status; |
| 657 | } |
| 658 | |
| 659 | void Lshal::usage() const { |
| 660 | mErr |
| 661 | << "usage: lshal" << std::endl |
Yifan Hong | a3b8709 | 2017-03-02 19:19:29 -0800 | [diff] [blame] | 662 | << " Dump all hals with default ordering and columns [-ipc]." << std::endl |
Yifan Hong | b447902 | 2017-03-02 16:54:11 -0800 | [diff] [blame] | 663 | << " lshal [--interface|-i] [--transport|-t] [-r|--arch]" << std::endl |
Yifan Hong | 38d53e0 | 2017-02-13 17:51:59 -0800 | [diff] [blame] | 664 | << " [--pid|-p] [--address|-a] [--clients|-c] [--cmdline|-m]" << std::endl |
Yifan Hong | 4b86549 | 2017-02-28 19:38:24 -0800 | [diff] [blame] | 665 | << " [--sort={interface|i|pid|p}] [--init-vintf[=path]]" << std::endl |
Yifan Hong | 38d53e0 | 2017-02-13 17:51:59 -0800 | [diff] [blame] | 666 | << " -i, --interface: print the interface name column" << std::endl |
| 667 | << " -n, --instance: print the instance name column" << std::endl |
| 668 | << " -t, --transport: print the transport mode column" << std::endl |
Yifan Hong | b447902 | 2017-03-02 16:54:11 -0800 | [diff] [blame] | 669 | << " -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] | 670 | << " -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] | 671 | << " -a, --address: print the server object address column" << std::endl |
Yifan Hong | ae09a3d | 2017-02-14 17:33:50 -0800 | [diff] [blame] | 672 | << " -c, --clients: print the client PIDs, or client cmdlines if -m is set" |
| 673 | << std::endl |
| 674 | << " -m, --cmdline: print cmdline instead of PIDs" << std::endl |
Yifan Hong | 38d53e0 | 2017-02-13 17:51:59 -0800 | [diff] [blame] | 675 | << " --sort=i, --sort=interface: sort by interface name" << std::endl |
| 676 | << " --sort=p, --sort=pid: sort by server pid" << std::endl |
Yifan Hong | 4b86549 | 2017-02-28 19:38:24 -0800 | [diff] [blame] | 677 | << " --init-vintf=path: form a skeleton HAL manifest to specified file " << std::endl |
| 678 | << " (stdout if no file specified)" << std::endl |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 679 | << " lshal [-h|--help]" << std::endl |
| 680 | << " -h, --help: show this help information." << std::endl; |
| 681 | } |
| 682 | |
| 683 | Status Lshal::parseArgs(int argc, char **argv) { |
| 684 | static struct option longOptions[] = { |
Yifan Hong | 38d53e0 | 2017-02-13 17:51:59 -0800 | [diff] [blame] | 685 | // long options with short alternatives |
| 686 | {"help", no_argument, 0, 'h' }, |
| 687 | {"interface", no_argument, 0, 'i' }, |
| 688 | {"transport", no_argument, 0, 't' }, |
Yifan Hong | b447902 | 2017-03-02 16:54:11 -0800 | [diff] [blame] | 689 | {"arch", no_argument, 0, 'r' }, |
Yifan Hong | 38d53e0 | 2017-02-13 17:51:59 -0800 | [diff] [blame] | 690 | {"pid", no_argument, 0, 'p' }, |
| 691 | {"address", no_argument, 0, 'a' }, |
| 692 | {"clients", no_argument, 0, 'c' }, |
Yifan Hong | ae09a3d | 2017-02-14 17:33:50 -0800 | [diff] [blame] | 693 | {"cmdline", no_argument, 0, 'm' }, |
Andreas Huber | 28d3591 | 2017-03-24 13:14:11 -0700 | [diff] [blame^] | 694 | {"debug", optional_argument, 0, 'd' }, |
Yifan Hong | 38d53e0 | 2017-02-13 17:51:59 -0800 | [diff] [blame] | 695 | |
| 696 | // long options without short alternatives |
| 697 | {"sort", required_argument, 0, 's' }, |
Yifan Hong | 4b86549 | 2017-02-28 19:38:24 -0800 | [diff] [blame] | 698 | {"init-vintf",optional_argument, 0, 'v' }, |
Yifan Hong | 38d53e0 | 2017-02-13 17:51:59 -0800 | [diff] [blame] | 699 | { 0, 0, 0, 0 } |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 700 | }; |
| 701 | |
| 702 | int optionIndex; |
| 703 | int c; |
| 704 | optind = 1; |
| 705 | for (;;) { |
| 706 | // using getopt_long in case we want to add other options in the future |
Andreas Huber | 28d3591 | 2017-03-24 13:14:11 -0700 | [diff] [blame^] | 707 | c = getopt_long(argc, argv, "hitrpacmd", longOptions, &optionIndex); |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 708 | if (c == -1) { |
| 709 | break; |
| 710 | } |
| 711 | switch (c) { |
Yifan Hong | 38d53e0 | 2017-02-13 17:51:59 -0800 | [diff] [blame] | 712 | case 's': { |
| 713 | if (strcmp(optarg, "interface") == 0 || strcmp(optarg, "i") == 0) { |
| 714 | mSortColumn = TableEntry::sortByInterfaceName; |
| 715 | } else if (strcmp(optarg, "pid") == 0 || strcmp(optarg, "p") == 0) { |
| 716 | mSortColumn = TableEntry::sortByServerPid; |
| 717 | } else { |
| 718 | mErr << "Unrecognized sorting column: " << optarg << std::endl; |
| 719 | usage(); |
| 720 | return USAGE; |
| 721 | } |
| 722 | break; |
| 723 | } |
Yifan Hong | 4b86549 | 2017-02-28 19:38:24 -0800 | [diff] [blame] | 724 | case 'v': { |
| 725 | if (optarg) { |
| 726 | mFileOutput = new std::ofstream{optarg}; |
| 727 | mOut = mFileOutput; |
| 728 | if (!mFileOutput.buf().is_open()) { |
| 729 | mErr << "Could not open file '" << optarg << "'." << std::endl; |
| 730 | return IO_ERROR; |
| 731 | } |
| 732 | } |
| 733 | mVintf = true; |
| 734 | } |
Yifan Hong | 38d53e0 | 2017-02-13 17:51:59 -0800 | [diff] [blame] | 735 | case 'i': { |
| 736 | mSelectedColumns |= ENABLE_INTERFACE_NAME; |
| 737 | break; |
| 738 | } |
| 739 | case 't': { |
| 740 | mSelectedColumns |= ENABLE_TRANSPORT; |
| 741 | break; |
| 742 | } |
Yifan Hong | b447902 | 2017-03-02 16:54:11 -0800 | [diff] [blame] | 743 | case 'r': { |
| 744 | mSelectedColumns |= ENABLE_ARCH; |
| 745 | break; |
| 746 | } |
Yifan Hong | 38d53e0 | 2017-02-13 17:51:59 -0800 | [diff] [blame] | 747 | case 'p': { |
| 748 | mSelectedColumns |= ENABLE_SERVER_PID; |
| 749 | break; |
| 750 | } |
| 751 | case 'a': { |
| 752 | mSelectedColumns |= ENABLE_SERVER_ADDR; |
| 753 | break; |
| 754 | } |
| 755 | case 'c': { |
| 756 | mSelectedColumns |= ENABLE_CLIENT_PIDS; |
| 757 | break; |
| 758 | } |
Yifan Hong | ae09a3d | 2017-02-14 17:33:50 -0800 | [diff] [blame] | 759 | case 'm': { |
| 760 | mEnableCmdlines = true; |
| 761 | break; |
| 762 | } |
Andreas Huber | 28d3591 | 2017-03-24 13:14:11 -0700 | [diff] [blame^] | 763 | case 'd': { |
| 764 | mEmitDebugInfo = true; |
| 765 | |
| 766 | if (optarg) { |
| 767 | mFileOutput = new std::ofstream{optarg}; |
| 768 | mOut = mFileOutput; |
| 769 | if (!mFileOutput.buf().is_open()) { |
| 770 | mErr << "Could not open file '" << optarg << "'." << std::endl; |
| 771 | return IO_ERROR; |
| 772 | } |
| 773 | chown(optarg, AID_SHELL, AID_SHELL); |
| 774 | } |
| 775 | break; |
| 776 | } |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 777 | case 'h': // falls through |
| 778 | default: // see unrecognized options |
| 779 | usage(); |
| 780 | return USAGE; |
| 781 | } |
| 782 | } |
Yifan Hong | 38d53e0 | 2017-02-13 17:51:59 -0800 | [diff] [blame] | 783 | |
| 784 | if (mSelectedColumns == 0) { |
Yifan Hong | a3b8709 | 2017-03-02 19:19:29 -0800 | [diff] [blame] | 785 | mSelectedColumns = ENABLE_INTERFACE_NAME | ENABLE_SERVER_PID | ENABLE_CLIENT_PIDS; |
Yifan Hong | 38d53e0 | 2017-02-13 17:51:59 -0800 | [diff] [blame] | 786 | } |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 787 | return OK; |
| 788 | } |
| 789 | |
| 790 | int Lshal::main(int argc, char **argv) { |
| 791 | Status status = parseArgs(argc, argv); |
| 792 | if (status != OK) { |
| 793 | return status; |
| 794 | } |
| 795 | status = fetch(); |
Yifan Hong | 38d53e0 | 2017-02-13 17:51:59 -0800 | [diff] [blame] | 796 | postprocess(); |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 797 | dump(); |
| 798 | return status; |
| 799 | } |
| 800 | |
Yifan Hong | a57dffb | 2017-02-21 14:59:00 -0800 | [diff] [blame] | 801 | void signalHandler(int sig) { |
| 802 | if (sig == SIGINT) { |
| 803 | int retVal; |
| 804 | pthread_exit(&retVal); |
| 805 | } |
| 806 | } |
| 807 | |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 808 | } // namespace lshal |
| 809 | } // namespace android |
| 810 | |
| 811 | int main(int argc, char **argv) { |
Yifan Hong | a57dffb | 2017-02-21 14:59:00 -0800 | [diff] [blame] | 812 | signal(SIGINT, ::android::lshal::signalHandler); |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 813 | return ::android::lshal::Lshal{}.main(argc, argv); |
| 814 | } |