| Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 1 | /* | 
 | 2 |  * Copyright (C) 2017 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_LIST_COMMAND_H_ | 
 | 18 | #define FRAMEWORK_NATIVE_CMDS_LSHAL_LIST_COMMAND_H_ | 
 | 19 |  | 
| Yifan Hong | a6b93f0 | 2017-09-13 16:53:37 -0700 | [diff] [blame] | 20 | #include <getopt.h> | 
| Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 21 | #include <stdint.h> | 
 | 22 |  | 
 | 23 | #include <fstream> | 
 | 24 | #include <string> | 
 | 25 | #include <vector> | 
 | 26 |  | 
 | 27 | #include <android-base/macros.h> | 
 | 28 | #include <android/hidl/manager/1.0/IServiceManager.h> | 
| Yifan Hong | b2d096a | 2018-05-01 15:25:23 -0700 | [diff] [blame] | 29 | #include <hidl-util/FqInstance.h> | 
 | 30 | #include <vintf/HalManifest.h> | 
| Yifan Hong | bdf44f8 | 2018-05-25 14:20:00 -0700 | [diff] [blame] | 31 | #include <vintf/VintfObject.h> | 
| Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 32 |  | 
| Yifan Hong | ded398e | 2017-09-07 13:54:28 -0700 | [diff] [blame] | 33 | #include "Command.h" | 
| Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 34 | #include "NullableOStream.h" | 
 | 35 | #include "TableEntry.h" | 
| Yifan Hong | 1bc1e9f | 2017-08-29 17:28:12 -0700 | [diff] [blame] | 36 | #include "TextTable.h" | 
| Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 37 | #include "utils.h" | 
 | 38 |  | 
 | 39 | namespace android { | 
 | 40 | namespace lshal { | 
 | 41 |  | 
 | 42 | class Lshal; | 
 | 43 |  | 
| Yifan Hong | 8bf7316 | 2017-09-07 18:06:13 -0700 | [diff] [blame] | 44 | struct PidInfo { | 
 | 45 |     std::map<uint64_t, Pids> refPids; // pids that are referenced | 
 | 46 |     uint32_t threadUsage; // number of threads in use | 
 | 47 |     uint32_t threadCount; // number of threads total | 
 | 48 | }; | 
 | 49 |  | 
| Nirav Atre | cce988d | 2018-05-16 11:14:46 -0700 | [diff] [blame] | 50 | enum class HalType { | 
 | 51 |     BINDERIZED_SERVICES = 0, | 
 | 52 |     PASSTHROUGH_CLIENTS, | 
| Yifan Hong | 13ba0a9 | 2018-06-25 16:15:56 -0700 | [diff] [blame] | 53 |     PASSTHROUGH_LIBRARIES, | 
 | 54 |     VINTF_MANIFEST, | 
| Yifan Hong | 3212f17 | 2018-06-28 12:39:50 -0700 | [diff] [blame] | 55 |     LAZY_HALS, | 
| Nirav Atre | cce988d | 2018-05-16 11:14:46 -0700 | [diff] [blame] | 56 | }; | 
 | 57 |  | 
| Yifan Hong | ded398e | 2017-09-07 13:54:28 -0700 | [diff] [blame] | 58 | class ListCommand : public Command { | 
| Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 59 | public: | 
| Chih-Hung Hsieh | 45e31c7 | 2018-12-20 15:47:01 -0800 | [diff] [blame] | 60 |     explicit ListCommand(Lshal &lshal) : Command(lshal) {} | 
| Yifan Hong | 8bf7316 | 2017-09-07 18:06:13 -0700 | [diff] [blame] | 61 |     virtual ~ListCommand() = default; | 
| Yifan Hong | a8bedc6 | 2017-09-08 18:00:31 -0700 | [diff] [blame] | 62 |     Status main(const Arg &arg) override; | 
 | 63 |     void usage() const override; | 
| Yifan Hong | 795b6ec | 2017-09-13 11:25:28 -0700 | [diff] [blame] | 64 |     std::string getSimpleDescription() const override; | 
 | 65 |     std::string getName() const override { return GetName(); } | 
 | 66 |  | 
 | 67 |     static std::string GetName(); | 
| Yifan Hong | a6b93f0 | 2017-09-13 16:53:37 -0700 | [diff] [blame] | 68 |  | 
 | 69 |     struct RegisteredOption { | 
 | 70 |         // short alternative, e.g. 'v'. If '\0', no short options is available. | 
 | 71 |         char shortOption; | 
 | 72 |         // long alternative, e.g. 'init-vintf' | 
 | 73 |         std::string longOption; | 
 | 74 |         // no_argument, required_argument or optional_argument | 
 | 75 |         int hasArg; | 
 | 76 |         // value written to 'flag' by getopt_long | 
 | 77 |         int val; | 
 | 78 |         // operation when the argument is present | 
 | 79 |         std::function<Status(ListCommand* thiz, const char* arg)> op; | 
 | 80 |         // help message | 
 | 81 |         std::string help; | 
 | 82 |  | 
 | 83 |         const std::string& getHelpMessageForArgument() const; | 
 | 84 |     }; | 
 | 85 |     // A list of acceptable command line options | 
 | 86 |     // key: value returned by getopt_long | 
 | 87 |     using RegisteredOptions = std::vector<RegisteredOption>; | 
 | 88 |  | 
| Yifan Hong | f31aa05 | 2018-02-02 15:17:51 -0800 | [diff] [blame] | 89 |     static std::string INIT_VINTF_NOTES; | 
 | 90 |  | 
| Yifan Hong | b2a2ecb | 2017-09-07 15:08:22 -0700 | [diff] [blame] | 91 | protected: | 
| Yifan Hong | a8bedc6 | 2017-09-08 18:00:31 -0700 | [diff] [blame] | 92 |     Status parseArgs(const Arg &arg); | 
| Yifan Hong | bdf44f8 | 2018-05-25 14:20:00 -0700 | [diff] [blame] | 93 |     // Retrieve first-hand information | 
| Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 94 |     Status fetch(); | 
| Yifan Hong | bdf44f8 | 2018-05-25 14:20:00 -0700 | [diff] [blame] | 95 |     // Retrieve derived information base on existing table | 
| Yifan Hong | 93b8bff | 2017-09-14 16:02:52 -0700 | [diff] [blame] | 96 |     virtual void postprocess(); | 
| Yifan Hong | ca3b660 | 2017-09-07 16:44:27 -0700 | [diff] [blame] | 97 |     Status dump(); | 
| Yifan Hong | 20f4ee8 | 2018-06-25 16:21:29 -0700 | [diff] [blame] | 98 |     void putEntry(HalType type, TableEntry &&entry); | 
| Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 99 |     Status fetchPassthrough(const sp<::android::hidl::manager::V1_0::IServiceManager> &manager); | 
 | 100 |     Status fetchBinderized(const sp<::android::hidl::manager::V1_0::IServiceManager> &manager); | 
 | 101 |     Status fetchAllLibraries(const sp<::android::hidl::manager::V1_0::IServiceManager> &manager); | 
| Yifan Hong | 13ba0a9 | 2018-06-25 16:15:56 -0700 | [diff] [blame] | 102 |     Status fetchManifestHals(); | 
| Yifan Hong | 3212f17 | 2018-06-28 12:39:50 -0700 | [diff] [blame] | 103 |     Status fetchLazyHals(); | 
| Steven Moreland | d8e2019 | 2017-05-24 11:23:08 -0700 | [diff] [blame] | 104 |  | 
| Yifan Hong | 22ea7b8 | 2017-09-14 18:07:43 -0700 | [diff] [blame] | 105 |     Status fetchBinderizedEntry(const sp<::android::hidl::manager::V1_0::IServiceManager> &manager, | 
 | 106 |                                 TableEntry *entry); | 
 | 107 |  | 
| Yifan Hong | 1243dde | 2017-09-14 17:49:30 -0700 | [diff] [blame] | 108 |     // Get relevant information for a PID by parsing files under /d/binder. | 
 | 109 |     // It is a virtual member function so that it can be mocked. | 
| Yifan Hong | 8bf7316 | 2017-09-07 18:06:13 -0700 | [diff] [blame] | 110 |     virtual bool getPidInfo(pid_t serverPid, PidInfo *info) const; | 
| Yifan Hong | 1243dde | 2017-09-14 17:49:30 -0700 | [diff] [blame] | 111 |     // Retrieve from mCachedPidInfos and call getPidInfo if necessary. | 
 | 112 |     const PidInfo* getPidInfoCached(pid_t serverPid); | 
| Steven Moreland | d8e2019 | 2017-05-24 11:23:08 -0700 | [diff] [blame] | 113 |  | 
| Yifan Hong | ca3b660 | 2017-09-07 16:44:27 -0700 | [diff] [blame] | 114 |     void dumpTable(const NullableOStream<std::ostream>& out) const; | 
 | 115 |     void dumpVintf(const NullableOStream<std::ostream>& out) const; | 
| Yifan Hong | 1bc1e9f | 2017-08-29 17:28:12 -0700 | [diff] [blame] | 116 |     void addLine(TextTable *table, const std::string &interfaceName, const std::string &transport, | 
 | 117 |                  const std::string &arch, const std::string &threadUsage, const std::string &server, | 
 | 118 |                  const std::string &serverCmdline, const std::string &address, | 
 | 119 |                  const std::string &clients, const std::string &clientCmdlines) const; | 
 | 120 |     void addLine(TextTable *table, const TableEntry &entry); | 
| Yifan Hong | 8bf7316 | 2017-09-07 18:06:13 -0700 | [diff] [blame] | 121 |     // Read and return /proc/{pid}/cmdline. | 
 | 122 |     virtual std::string parseCmdline(pid_t pid) const; | 
| Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 123 |     // Return /proc/{pid}/cmdline if it exists, else empty string. | 
| Yifan Hong | f31aa05 | 2018-02-02 15:17:51 -0800 | [diff] [blame] | 124 |     const std::string& getCmdline(pid_t pid); | 
| Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 125 |     // Call getCmdline on all pid in pids. If it returns empty string, the process might | 
 | 126 |     // have died, and the pid is removed from pids. | 
 | 127 |     void removeDeadProcesses(Pids *pids); | 
| Yifan Hong | f31aa05 | 2018-02-02 15:17:51 -0800 | [diff] [blame] | 128 |  | 
 | 129 |     virtual Partition getPartition(pid_t pid); | 
| Yifan Hong | b2d096a | 2018-05-01 15:25:23 -0700 | [diff] [blame] | 130 |     Partition resolvePartition(Partition processPartition, const FqInstance &fqInstance) const; | 
| Yifan Hong | f31aa05 | 2018-02-02 15:17:51 -0800 | [diff] [blame] | 131 |  | 
| Yifan Hong | bdf44f8 | 2018-05-25 14:20:00 -0700 | [diff] [blame] | 132 |     VintfInfo getVintfInfo(const std::string &fqInstanceName, vintf::TransportArch ta) const; | 
 | 133 |     // Allow to mock these functions for testing. | 
 | 134 |     virtual std::shared_ptr<const vintf::HalManifest> getDeviceManifest() const; | 
 | 135 |     virtual std::shared_ptr<const vintf::CompatibilityMatrix> getDeviceMatrix() const; | 
 | 136 |     virtual std::shared_ptr<const vintf::HalManifest> getFrameworkManifest() const; | 
 | 137 |     virtual std::shared_ptr<const vintf::CompatibilityMatrix> getFrameworkMatrix() const; | 
 | 138 |  | 
| Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 139 |     void forEachTable(const std::function<void(Table &)> &f); | 
 | 140 |     void forEachTable(const std::function<void(const Table &)> &f) const; | 
| Yifan Hong | db73053 | 2018-06-25 16:32:01 -0700 | [diff] [blame] | 141 |     Table* tableForType(HalType type); | 
 | 142 |     const Table* tableForType(HalType type) const; | 
| Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 143 |  | 
| Yifan Hong | 76ac14a | 2017-09-08 14:59:04 -0700 | [diff] [blame] | 144 |     NullableOStream<std::ostream> err() const; | 
 | 145 |     NullableOStream<std::ostream> out() const; | 
 | 146 |  | 
| Yifan Hong | a6b93f0 | 2017-09-13 16:53:37 -0700 | [diff] [blame] | 147 |     void registerAllOptions(); | 
 | 148 |  | 
| Yifan Hong | b2d096a | 2018-05-01 15:25:23 -0700 | [diff] [blame] | 149 |     // helper functions to dumpVintf. | 
 | 150 |     bool addEntryWithInstance(const TableEntry &entry, vintf::HalManifest *manifest) const; | 
 | 151 |     bool addEntryWithoutInstance(const TableEntry &entry, const vintf::HalManifest *manifest) const; | 
 | 152 |  | 
| Yifan Hong | 13ba0a9 | 2018-06-25 16:15:56 -0700 | [diff] [blame] | 153 |     // Helper function. Whether to fetch entries corresponding to a given HAL type. | 
 | 154 |     bool shouldFetchHalType(const HalType &type) const; | 
 | 155 |  | 
 | 156 |     void initFetchTypes(); | 
| Nirav Atre | cce988d | 2018-05-16 11:14:46 -0700 | [diff] [blame] | 157 |  | 
| Yifan Hong | 3212f17 | 2018-06-28 12:39:50 -0700 | [diff] [blame] | 158 |     // Helper functions ti add HALs that are listed in VINTF manifest to LAZY_HALS table. | 
 | 159 |     bool hasHwbinderEntry(const TableEntry& entry) const; | 
 | 160 |     bool hasPassthroughEntry(const TableEntry& entry) const; | 
 | 161 |  | 
| Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 162 |     Table mServicesTable{}; | 
 | 163 |     Table mPassthroughRefTable{}; | 
 | 164 |     Table mImplementationsTable{}; | 
| Yifan Hong | 13ba0a9 | 2018-06-25 16:15:56 -0700 | [diff] [blame] | 165 |     Table mManifestHalsTable{}; | 
| Yifan Hong | 3212f17 | 2018-06-28 12:39:50 -0700 | [diff] [blame] | 166 |     Table mLazyHalsTable{}; | 
| Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 167 |  | 
| Yifan Hong | ca3b660 | 2017-09-07 16:44:27 -0700 | [diff] [blame] | 168 |     std::string mFileOutputPath; | 
| Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 169 |     TableEntryCompare mSortColumn = nullptr; | 
| Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 170 |  | 
| Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 171 |     bool mEmitDebugInfo = false; | 
 | 172 |  | 
| Yifan Hong | f31aa05 | 2018-02-02 15:17:51 -0800 | [diff] [blame] | 173 |     // If true, output in VINTF format. Output only entries from the specified partition. | 
| Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 174 |     bool mVintf = false; | 
| Yifan Hong | f31aa05 | 2018-02-02 15:17:51 -0800 | [diff] [blame] | 175 |     Partition mVintfPartition = Partition::UNKNOWN; | 
| Yifan Hong | 6da0691 | 2017-05-12 16:56:43 -0700 | [diff] [blame] | 176 |  | 
 | 177 |     // If true, explanatory text are not emitted. | 
 | 178 |     bool mNeat = false; | 
 | 179 |  | 
| Yifan Hong | 13ba0a9 | 2018-06-25 16:15:56 -0700 | [diff] [blame] | 180 |     // Type(s) of HAL associations to list. | 
 | 181 |     std::vector<HalType> mListTypes{}; | 
 | 182 |     // Type(s) of HAL associations to fetch. | 
 | 183 |     std::set<HalType> mFetchTypes{}; | 
| Nirav Atre | cce988d | 2018-05-16 11:14:46 -0700 | [diff] [blame] | 184 |  | 
| Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 185 |     // If an entry does not exist, need to ask /proc/{pid}/cmdline to get it. | 
 | 186 |     // If an entry exist but is an empty string, process might have died. | 
 | 187 |     // If an entry exist and not empty, it contains the cached content of /proc/{pid}/cmdline. | 
 | 188 |     std::map<pid_t, std::string> mCmdlines; | 
 | 189 |  | 
| Yifan Hong | 1243dde | 2017-09-14 17:49:30 -0700 | [diff] [blame] | 190 |     // Cache for getPidInfo. | 
 | 191 |     std::map<pid_t, PidInfo> mCachedPidInfos; | 
 | 192 |  | 
| Yifan Hong | f31aa05 | 2018-02-02 15:17:51 -0800 | [diff] [blame] | 193 |     // Cache for getPartition. | 
 | 194 |     std::map<pid_t, Partition> mPartitions; | 
 | 195 |  | 
| Yifan Hong | a6b93f0 | 2017-09-13 16:53:37 -0700 | [diff] [blame] | 196 |     RegisteredOptions mOptions; | 
 | 197 |     // All selected columns | 
 | 198 |     std::vector<TableColumnType> mSelectedColumns; | 
 | 199 |     // If true, emit cmdlines instead of PIDs | 
 | 200 |     bool mEnableCmdlines = false; | 
 | 201 |  | 
| Yifan Hong | 91e655d | 2017-09-13 15:44:56 -0700 | [diff] [blame] | 202 | private: | 
| Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 203 |     DISALLOW_COPY_AND_ASSIGN(ListCommand); | 
 | 204 | }; | 
 | 205 |  | 
 | 206 |  | 
 | 207 | }  // namespace lshal | 
 | 208 | }  // namespace android | 
 | 209 |  | 
 | 210 | #endif  // FRAMEWORK_NATIVE_CMDS_LSHAL_LIST_COMMAND_H_ |