blob: f4d3fdf67c105e79d23c857ac998bd78c31d59f5 [file] [log] [blame]
Yifan Hong443df792017-05-09 18:49:45 -07001/*
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 Honga6b93f02017-09-13 16:53:37 -070020#include <getopt.h>
Yifan Hong443df792017-05-09 18:49:45 -070021#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 Hongb2d096a2018-05-01 15:25:23 -070029#include <hidl-util/FqInstance.h>
30#include <vintf/HalManifest.h>
Yifan Hongbdf44f82018-05-25 14:20:00 -070031#include <vintf/VintfObject.h>
Yifan Hong443df792017-05-09 18:49:45 -070032
Yifan Hongded398e2017-09-07 13:54:28 -070033#include "Command.h"
Yifan Hong443df792017-05-09 18:49:45 -070034#include "NullableOStream.h"
35#include "TableEntry.h"
Yifan Hong1bc1e9f2017-08-29 17:28:12 -070036#include "TextTable.h"
Yifan Hong443df792017-05-09 18:49:45 -070037#include "utils.h"
38
39namespace android {
40namespace lshal {
41
42class Lshal;
43
Yifan Hong8bf73162017-09-07 18:06:13 -070044struct 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 Atrecce988d2018-05-16 11:14:46 -070050enum class HalType {
51 BINDERIZED_SERVICES = 0,
52 PASSTHROUGH_CLIENTS,
Yifan Hong13ba0a92018-06-25 16:15:56 -070053 PASSTHROUGH_LIBRARIES,
54 VINTF_MANIFEST,
Nirav Atrecce988d2018-05-16 11:14:46 -070055};
56
Yifan Hongded398e2017-09-07 13:54:28 -070057class ListCommand : public Command {
Yifan Hong443df792017-05-09 18:49:45 -070058public:
Yifan Hongded398e2017-09-07 13:54:28 -070059 ListCommand(Lshal &lshal) : Command(lshal) {}
Yifan Hong8bf73162017-09-07 18:06:13 -070060 virtual ~ListCommand() = default;
Yifan Honga8bedc62017-09-08 18:00:31 -070061 Status main(const Arg &arg) override;
62 void usage() const override;
Yifan Hong795b6ec2017-09-13 11:25:28 -070063 std::string getSimpleDescription() const override;
64 std::string getName() const override { return GetName(); }
65
66 static std::string GetName();
Yifan Honga6b93f02017-09-13 16:53:37 -070067
68 struct RegisteredOption {
69 // short alternative, e.g. 'v'. If '\0', no short options is available.
70 char shortOption;
71 // long alternative, e.g. 'init-vintf'
72 std::string longOption;
73 // no_argument, required_argument or optional_argument
74 int hasArg;
75 // value written to 'flag' by getopt_long
76 int val;
77 // operation when the argument is present
78 std::function<Status(ListCommand* thiz, const char* arg)> op;
79 // help message
80 std::string help;
81
82 const std::string& getHelpMessageForArgument() const;
83 };
84 // A list of acceptable command line options
85 // key: value returned by getopt_long
86 using RegisteredOptions = std::vector<RegisteredOption>;
87
Yifan Hongf31aa052018-02-02 15:17:51 -080088 static std::string INIT_VINTF_NOTES;
89
Yifan Hongb2a2ecb2017-09-07 15:08:22 -070090protected:
Yifan Honga8bedc62017-09-08 18:00:31 -070091 Status parseArgs(const Arg &arg);
Yifan Hongbdf44f82018-05-25 14:20:00 -070092 // Retrieve first-hand information
Yifan Hong443df792017-05-09 18:49:45 -070093 Status fetch();
Yifan Hongbdf44f82018-05-25 14:20:00 -070094 // Retrieve derived information base on existing table
Yifan Hong93b8bff2017-09-14 16:02:52 -070095 virtual void postprocess();
Yifan Hongca3b6602017-09-07 16:44:27 -070096 Status dump();
Yifan Hong20f4ee82018-06-25 16:21:29 -070097 void putEntry(HalType type, TableEntry &&entry);
Yifan Hong443df792017-05-09 18:49:45 -070098 Status fetchPassthrough(const sp<::android::hidl::manager::V1_0::IServiceManager> &manager);
99 Status fetchBinderized(const sp<::android::hidl::manager::V1_0::IServiceManager> &manager);
100 Status fetchAllLibraries(const sp<::android::hidl::manager::V1_0::IServiceManager> &manager);
Yifan Hong13ba0a92018-06-25 16:15:56 -0700101 Status fetchManifestHals();
Steven Morelandd8e20192017-05-24 11:23:08 -0700102
Yifan Hong22ea7b82017-09-14 18:07:43 -0700103 Status fetchBinderizedEntry(const sp<::android::hidl::manager::V1_0::IServiceManager> &manager,
104 TableEntry *entry);
105
Yifan Hong1243dde2017-09-14 17:49:30 -0700106 // Get relevant information for a PID by parsing files under /d/binder.
107 // It is a virtual member function so that it can be mocked.
Yifan Hong8bf73162017-09-07 18:06:13 -0700108 virtual bool getPidInfo(pid_t serverPid, PidInfo *info) const;
Yifan Hong1243dde2017-09-14 17:49:30 -0700109 // Retrieve from mCachedPidInfos and call getPidInfo if necessary.
110 const PidInfo* getPidInfoCached(pid_t serverPid);
Steven Morelandd8e20192017-05-24 11:23:08 -0700111
Yifan Hongca3b6602017-09-07 16:44:27 -0700112 void dumpTable(const NullableOStream<std::ostream>& out) const;
113 void dumpVintf(const NullableOStream<std::ostream>& out) const;
Yifan Hong1bc1e9f2017-08-29 17:28:12 -0700114 void addLine(TextTable *table, const std::string &interfaceName, const std::string &transport,
115 const std::string &arch, const std::string &threadUsage, const std::string &server,
116 const std::string &serverCmdline, const std::string &address,
117 const std::string &clients, const std::string &clientCmdlines) const;
118 void addLine(TextTable *table, const TableEntry &entry);
Yifan Hong8bf73162017-09-07 18:06:13 -0700119 // Read and return /proc/{pid}/cmdline.
120 virtual std::string parseCmdline(pid_t pid) const;
Yifan Hong443df792017-05-09 18:49:45 -0700121 // Return /proc/{pid}/cmdline if it exists, else empty string.
Yifan Hongf31aa052018-02-02 15:17:51 -0800122 const std::string& getCmdline(pid_t pid);
Yifan Hong443df792017-05-09 18:49:45 -0700123 // Call getCmdline on all pid in pids. If it returns empty string, the process might
124 // have died, and the pid is removed from pids.
125 void removeDeadProcesses(Pids *pids);
Yifan Hongf31aa052018-02-02 15:17:51 -0800126
127 virtual Partition getPartition(pid_t pid);
Yifan Hongb2d096a2018-05-01 15:25:23 -0700128 Partition resolvePartition(Partition processPartition, const FqInstance &fqInstance) const;
Yifan Hongf31aa052018-02-02 15:17:51 -0800129
Yifan Hongbdf44f82018-05-25 14:20:00 -0700130 VintfInfo getVintfInfo(const std::string &fqInstanceName, vintf::TransportArch ta) const;
131 // Allow to mock these functions for testing.
132 virtual std::shared_ptr<const vintf::HalManifest> getDeviceManifest() const;
133 virtual std::shared_ptr<const vintf::CompatibilityMatrix> getDeviceMatrix() const;
134 virtual std::shared_ptr<const vintf::HalManifest> getFrameworkManifest() const;
135 virtual std::shared_ptr<const vintf::CompatibilityMatrix> getFrameworkMatrix() const;
136
Yifan Hong443df792017-05-09 18:49:45 -0700137 void forEachTable(const std::function<void(Table &)> &f);
138 void forEachTable(const std::function<void(const Table &)> &f) const;
Yifan Hongdb730532018-06-25 16:32:01 -0700139 Table* tableForType(HalType type);
140 const Table* tableForType(HalType type) const;
Yifan Hong443df792017-05-09 18:49:45 -0700141
Yifan Hong76ac14a2017-09-08 14:59:04 -0700142 NullableOStream<std::ostream> err() const;
143 NullableOStream<std::ostream> out() const;
144
Yifan Honga6b93f02017-09-13 16:53:37 -0700145 void registerAllOptions();
146
Yifan Hongb2d096a2018-05-01 15:25:23 -0700147 // helper functions to dumpVintf.
148 bool addEntryWithInstance(const TableEntry &entry, vintf::HalManifest *manifest) const;
149 bool addEntryWithoutInstance(const TableEntry &entry, const vintf::HalManifest *manifest) const;
150
Yifan Hong13ba0a92018-06-25 16:15:56 -0700151 // Helper function. Whether to fetch entries corresponding to a given HAL type.
152 bool shouldFetchHalType(const HalType &type) const;
153
154 void initFetchTypes();
Nirav Atrecce988d2018-05-16 11:14:46 -0700155
Yifan Hong443df792017-05-09 18:49:45 -0700156 Table mServicesTable{};
157 Table mPassthroughRefTable{};
158 Table mImplementationsTable{};
Yifan Hong13ba0a92018-06-25 16:15:56 -0700159 Table mManifestHalsTable{};
Yifan Hong443df792017-05-09 18:49:45 -0700160
Yifan Hongca3b6602017-09-07 16:44:27 -0700161 std::string mFileOutputPath;
Yifan Hong443df792017-05-09 18:49:45 -0700162 TableEntryCompare mSortColumn = nullptr;
Yifan Hong443df792017-05-09 18:49:45 -0700163
Yifan Hong443df792017-05-09 18:49:45 -0700164 bool mEmitDebugInfo = false;
165
Yifan Hongf31aa052018-02-02 15:17:51 -0800166 // If true, output in VINTF format. Output only entries from the specified partition.
Yifan Hong443df792017-05-09 18:49:45 -0700167 bool mVintf = false;
Yifan Hongf31aa052018-02-02 15:17:51 -0800168 Partition mVintfPartition = Partition::UNKNOWN;
Yifan Hong6da06912017-05-12 16:56:43 -0700169
170 // If true, explanatory text are not emitted.
171 bool mNeat = false;
172
Yifan Hong13ba0a92018-06-25 16:15:56 -0700173 // Type(s) of HAL associations to list.
174 std::vector<HalType> mListTypes{};
175 // Type(s) of HAL associations to fetch.
176 std::set<HalType> mFetchTypes{};
Nirav Atrecce988d2018-05-16 11:14:46 -0700177
Yifan Hong443df792017-05-09 18:49:45 -0700178 // If an entry does not exist, need to ask /proc/{pid}/cmdline to get it.
179 // If an entry exist but is an empty string, process might have died.
180 // If an entry exist and not empty, it contains the cached content of /proc/{pid}/cmdline.
181 std::map<pid_t, std::string> mCmdlines;
182
Yifan Hong1243dde2017-09-14 17:49:30 -0700183 // Cache for getPidInfo.
184 std::map<pid_t, PidInfo> mCachedPidInfos;
185
Yifan Hongf31aa052018-02-02 15:17:51 -0800186 // Cache for getPartition.
187 std::map<pid_t, Partition> mPartitions;
188
Yifan Honga6b93f02017-09-13 16:53:37 -0700189 RegisteredOptions mOptions;
190 // All selected columns
191 std::vector<TableColumnType> mSelectedColumns;
192 // If true, emit cmdlines instead of PIDs
193 bool mEnableCmdlines = false;
194
Yifan Hong91e655d2017-09-13 15:44:56 -0700195private:
Yifan Hong443df792017-05-09 18:49:45 -0700196 DISALLOW_COPY_AND_ASSIGN(ListCommand);
197};
198
199
200} // namespace lshal
201} // namespace android
202
203#endif // FRAMEWORK_NATIVE_CMDS_LSHAL_LIST_COMMAND_H_