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 | #ifndef FRAMEWORK_NATIVE_CMDS_LSHAL_LSHAL_H_ |
| 18 | #define FRAMEWORK_NATIVE_CMDS_LSHAL_LSHAL_H_ |
| 19 | |
| 20 | #include <stdint.h> |
| 21 | |
Yifan Hong | 4b86549 | 2017-02-28 19:38:24 -0800 | [diff] [blame] | 22 | #include <fstream> |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 23 | #include <string> |
| 24 | #include <vector> |
| 25 | |
| 26 | #include <android/hidl/manager/1.0/IServiceManager.h> |
| 27 | |
Yifan Hong | 4b86549 | 2017-02-28 19:38:24 -0800 | [diff] [blame] | 28 | #include "NullableOStream.h" |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 29 | #include "TableEntry.h" |
| 30 | |
| 31 | namespace android { |
| 32 | namespace lshal { |
| 33 | |
| 34 | enum : unsigned int { |
| 35 | OK = 0, |
| 36 | USAGE = 1 << 0, |
| 37 | NO_BINDERIZED_MANAGER = 1 << 1, |
| 38 | NO_PASSTHROUGH_MANAGER = 1 << 2, |
| 39 | DUMP_BINDERIZED_ERROR = 1 << 3, |
| 40 | DUMP_PASSTHROUGH_ERROR = 1 << 4, |
| 41 | DUMP_ALL_LIBS_ERROR = 1 << 5, |
Yifan Hong | 4b86549 | 2017-02-28 19:38:24 -0800 | [diff] [blame] | 42 | IO_ERROR = 1 << 6, |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 43 | }; |
| 44 | using Status = unsigned int; |
| 45 | |
| 46 | class Lshal { |
| 47 | public: |
| 48 | int main(int argc, char **argv); |
| 49 | |
| 50 | private: |
| 51 | Status parseArgs(int argc, char **argv); |
| 52 | Status fetch(); |
Yifan Hong | 38d53e0 | 2017-02-13 17:51:59 -0800 | [diff] [blame] | 53 | void postprocess(); |
Yifan Hong | 4b86549 | 2017-02-28 19:38:24 -0800 | [diff] [blame] | 54 | void dump(); |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 55 | void usage() const; |
Yifan Hong | a3b8709 | 2017-03-02 19:19:29 -0800 | [diff] [blame] | 56 | void putEntry(TableEntrySource source, TableEntry &&entry); |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 57 | Status fetchPassthrough(const sp<::android::hidl::manager::V1_0::IServiceManager> &manager); |
| 58 | Status fetchBinderized(const sp<::android::hidl::manager::V1_0::IServiceManager> &manager); |
| 59 | Status fetchAllLibraries(const sp<::android::hidl::manager::V1_0::IServiceManager> &manager); |
| 60 | bool getReferencedPids( |
| 61 | pid_t serverPid, std::map<uint64_t, Pids> *objects) const; |
Yifan Hong | a3b8709 | 2017-03-02 19:19:29 -0800 | [diff] [blame] | 62 | void dumpTable(); |
Yifan Hong | 4b86549 | 2017-02-28 19:38:24 -0800 | [diff] [blame] | 63 | void dumpVintf() const; |
Yifan Hong | 38d53e0 | 2017-02-13 17:51:59 -0800 | [diff] [blame] | 64 | void printLine( |
| 65 | const std::string &interfaceName, |
Yifan Hong | b447902 | 2017-03-02 16:54:11 -0800 | [diff] [blame] | 66 | const std::string &transport, |
| 67 | const std::string &arch, |
| 68 | const std::string &server, |
Yifan Hong | ae09a3d | 2017-02-14 17:33:50 -0800 | [diff] [blame] | 69 | const std::string &serverCmdline, |
| 70 | const std::string &address, const std::string &clients, |
| 71 | const std::string &clientCmdlines) const ; |
| 72 | // Return /proc/{pid}/cmdline if it exists, else empty string. |
| 73 | const std::string &getCmdline(pid_t pid); |
| 74 | // Call getCmdline on all pid in pids. If it returns empty string, the process might |
| 75 | // have died, and the pid is removed from pids. |
| 76 | void removeDeadProcesses(Pids *pids); |
Yifan Hong | a3b8709 | 2017-03-02 19:19:29 -0800 | [diff] [blame] | 77 | void forEachTable(const std::function<void(Table &)> &f); |
| 78 | void forEachTable(const std::function<void(const Table &)> &f) const; |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 79 | |
Andreas Huber | 28d3591 | 2017-03-24 13:14:11 -0700 | [diff] [blame^] | 80 | void emitDebugInfo( |
| 81 | const sp<hidl::manager::V1_0::IServiceManager> &serviceManager, |
| 82 | const std::string &interfaceName, |
| 83 | const std::string &instanceName) const; |
| 84 | |
Yifan Hong | a3b8709 | 2017-03-02 19:19:29 -0800 | [diff] [blame] | 85 | Table mServicesTable{}; |
| 86 | Table mPassthroughRefTable{}; |
| 87 | Table mImplementationsTable{}; |
| 88 | |
Yifan Hong | 4b86549 | 2017-02-28 19:38:24 -0800 | [diff] [blame] | 89 | NullableOStream<std::ostream> mErr = std::cerr; |
| 90 | NullableOStream<std::ostream> mOut = std::cout; |
| 91 | NullableOStream<std::ofstream> mFileOutput = nullptr; |
Yifan Hong | 38d53e0 | 2017-02-13 17:51:59 -0800 | [diff] [blame] | 92 | TableEntryCompare mSortColumn = nullptr; |
| 93 | TableEntrySelect mSelectedColumns = 0; |
Yifan Hong | ae09a3d | 2017-02-14 17:33:50 -0800 | [diff] [blame] | 94 | // If true, cmdlines will be printed instead of pid. |
Yifan Hong | 4b86549 | 2017-02-28 19:38:24 -0800 | [diff] [blame] | 95 | bool mEnableCmdlines = false; |
Andreas Huber | 28d3591 | 2017-03-24 13:14:11 -0700 | [diff] [blame^] | 96 | |
| 97 | // If true, calls IBase::debug(...) on each service. |
| 98 | bool mEmitDebugInfo = false; |
| 99 | |
Yifan Hong | 4b86549 | 2017-02-28 19:38:24 -0800 | [diff] [blame] | 100 | bool mVintf = false; |
Yifan Hong | ae09a3d | 2017-02-14 17:33:50 -0800 | [diff] [blame] | 101 | // If an entry does not exist, need to ask /proc/{pid}/cmdline to get it. |
| 102 | // If an entry exist but is an empty string, process might have died. |
| 103 | // If an entry exist and not empty, it contains the cached content of /proc/{pid}/cmdline. |
| 104 | std::map<pid_t, std::string> mCmdlines; |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 105 | }; |
| 106 | |
| 107 | |
| 108 | } // namespace lshal |
| 109 | } // namespace android |
| 110 | |
| 111 | #endif // FRAMEWORK_NATIVE_CMDS_LSHAL_LSHAL_H_ |