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; |
| 56 | void putEntry(TableEntry &&entry); |
| 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 | 4b86549 | 2017-02-28 19:38:24 -0800 | [diff] [blame^] | 62 | void dumpTable() const; |
| 63 | void dumpVintf() const; |
Yifan Hong | 38d53e0 | 2017-02-13 17:51:59 -0800 | [diff] [blame] | 64 | void printLine( |
| 65 | const std::string &interfaceName, |
| 66 | const std::string &transport, const std::string &server, |
Yifan Hong | ae09a3d | 2017-02-14 17:33:50 -0800 | [diff] [blame] | 67 | const std::string &serverCmdline, |
| 68 | const std::string &address, const std::string &clients, |
| 69 | const std::string &clientCmdlines) const ; |
| 70 | // Return /proc/{pid}/cmdline if it exists, else empty string. |
| 71 | const std::string &getCmdline(pid_t pid); |
| 72 | // Call getCmdline on all pid in pids. If it returns empty string, the process might |
| 73 | // have died, and the pid is removed from pids. |
| 74 | void removeDeadProcesses(Pids *pids); |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 75 | |
Yifan Hong | 38d53e0 | 2017-02-13 17:51:59 -0800 | [diff] [blame] | 76 | Table mTable{}; |
Yifan Hong | 4b86549 | 2017-02-28 19:38:24 -0800 | [diff] [blame^] | 77 | NullableOStream<std::ostream> mErr = std::cerr; |
| 78 | NullableOStream<std::ostream> mOut = std::cout; |
| 79 | NullableOStream<std::ofstream> mFileOutput = nullptr; |
Yifan Hong | 38d53e0 | 2017-02-13 17:51:59 -0800 | [diff] [blame] | 80 | TableEntryCompare mSortColumn = nullptr; |
| 81 | TableEntrySelect mSelectedColumns = 0; |
Yifan Hong | ae09a3d | 2017-02-14 17:33:50 -0800 | [diff] [blame] | 82 | // If true, cmdlines will be printed instead of pid. |
Yifan Hong | 4b86549 | 2017-02-28 19:38:24 -0800 | [diff] [blame^] | 83 | bool mEnableCmdlines = false; |
| 84 | bool mVintf = false; |
Yifan Hong | ae09a3d | 2017-02-14 17:33:50 -0800 | [diff] [blame] | 85 | // If an entry does not exist, need to ask /proc/{pid}/cmdline to get it. |
| 86 | // If an entry exist but is an empty string, process might have died. |
| 87 | // If an entry exist and not empty, it contains the cached content of /proc/{pid}/cmdline. |
| 88 | std::map<pid_t, std::string> mCmdlines; |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 89 | }; |
| 90 | |
| 91 | |
| 92 | } // namespace lshal |
| 93 | } // namespace android |
| 94 | |
| 95 | #endif // FRAMEWORK_NATIVE_CMDS_LSHAL_LSHAL_H_ |