blob: 412aadd7e437fa3de9fa2fa1f40efaab6ff07611 [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
Steven Moreland0b8e3872019-06-25 08:54:34 -070017#pragma once
Yifan Hong443df792017-05-09 18:49:45 -070018
Yifan Honga6b93f02017-09-13 16:53:37 -070019#include <getopt.h>
Yifan Hong443df792017-05-09 18:49:45 -070020#include <stdint.h>
21
22#include <fstream>
23#include <string>
24#include <vector>
25
26#include <android-base/macros.h>
27#include <android/hidl/manager/1.0/IServiceManager.h>
Yifan Hongb2d096a2018-05-01 15:25:23 -070028#include <hidl-util/FqInstance.h>
29#include <vintf/HalManifest.h>
Yifan Hongbdf44f82018-05-25 14:20:00 -070030#include <vintf/VintfObject.h>
Yifan Hong443df792017-05-09 18:49:45 -070031
Yifan Hongded398e2017-09-07 13:54:28 -070032#include "Command.h"
Yifan Hong443df792017-05-09 18:49:45 -070033#include "NullableOStream.h"
34#include "TableEntry.h"
Yifan Hong1bc1e9f2017-08-29 17:28:12 -070035#include "TextTable.h"
Yifan Hong443df792017-05-09 18:49:45 -070036#include "utils.h"
37
38namespace android {
39namespace lshal {
40
41class Lshal;
42
Yifan Hong8bf73162017-09-07 18:06:13 -070043struct PidInfo {
44 std::map<uint64_t, Pids> refPids; // pids that are referenced
45 uint32_t threadUsage; // number of threads in use
46 uint32_t threadCount; // number of threads total
47};
48
Nirav Atrecce988d2018-05-16 11:14:46 -070049enum class HalType {
50 BINDERIZED_SERVICES = 0,
51 PASSTHROUGH_CLIENTS,
Yifan Hong13ba0a92018-06-25 16:15:56 -070052 PASSTHROUGH_LIBRARIES,
53 VINTF_MANIFEST,
Yifan Hong3212f172018-06-28 12:39:50 -070054 LAZY_HALS,
Yifan Hong30528a22020-08-07 18:24:06 -070055
56 // Not a real HalType. Used to determine all HalTypes.
57 LAST,
Nirav Atrecce988d2018-05-16 11:14:46 -070058};
59
Yifan Hongded398e2017-09-07 13:54:28 -070060class ListCommand : public Command {
Yifan Hong443df792017-05-09 18:49:45 -070061public:
Chih-Hung Hsieh45e31c72018-12-20 15:47:01 -080062 explicit ListCommand(Lshal &lshal) : Command(lshal) {}
Yifan Hong8bf73162017-09-07 18:06:13 -070063 virtual ~ListCommand() = default;
Yifan Honga8bedc62017-09-08 18:00:31 -070064 Status main(const Arg &arg) override;
65 void usage() const override;
Yifan Hong795b6ec2017-09-13 11:25:28 -070066 std::string getSimpleDescription() const override;
67 std::string getName() const override { return GetName(); }
68
69 static std::string GetName();
Yifan Honga6b93f02017-09-13 16:53:37 -070070
71 struct RegisteredOption {
72 // short alternative, e.g. 'v'. If '\0', no short options is available.
73 char shortOption;
74 // long alternative, e.g. 'init-vintf'
75 std::string longOption;
76 // no_argument, required_argument or optional_argument
77 int hasArg;
78 // value written to 'flag' by getopt_long
79 int val;
80 // operation when the argument is present
81 std::function<Status(ListCommand* thiz, const char* arg)> op;
82 // help message
83 std::string help;
84
85 const std::string& getHelpMessageForArgument() const;
86 };
87 // A list of acceptable command line options
88 // key: value returned by getopt_long
89 using RegisteredOptions = std::vector<RegisteredOption>;
90
Yifan Hongf31aa052018-02-02 15:17:51 -080091 static std::string INIT_VINTF_NOTES;
92
Yifan Hongb2a2ecb2017-09-07 15:08:22 -070093protected:
Yifan Honga8bedc62017-09-08 18:00:31 -070094 Status parseArgs(const Arg &arg);
Yifan Hongbdf44f82018-05-25 14:20:00 -070095 // Retrieve first-hand information
Yifan Hong443df792017-05-09 18:49:45 -070096 Status fetch();
Yifan Hongbdf44f82018-05-25 14:20:00 -070097 // Retrieve derived information base on existing table
Yifan Hong93b8bff2017-09-14 16:02:52 -070098 virtual void postprocess();
Yifan Hongca3b6602017-09-07 16:44:27 -070099 Status dump();
Yifan Hong20f4ee82018-06-25 16:21:29 -0700100 void putEntry(HalType type, TableEntry &&entry);
Yifan Hong443df792017-05-09 18:49:45 -0700101 Status fetchPassthrough(const sp<::android::hidl::manager::V1_0::IServiceManager> &manager);
102 Status fetchBinderized(const sp<::android::hidl::manager::V1_0::IServiceManager> &manager);
103 Status fetchAllLibraries(const sp<::android::hidl::manager::V1_0::IServiceManager> &manager);
Yifan Hong13ba0a92018-06-25 16:15:56 -0700104 Status fetchManifestHals();
Yifan Hong3212f172018-06-28 12:39:50 -0700105 Status fetchLazyHals();
Steven Morelandd8e20192017-05-24 11:23:08 -0700106
Yifan Hong22ea7b82017-09-14 18:07:43 -0700107 Status fetchBinderizedEntry(const sp<::android::hidl::manager::V1_0::IServiceManager> &manager,
108 TableEntry *entry);
109
Hridya Valsarajub49b0b12020-02-25 12:27:14 -0800110 // Get relevant information for a PID by parsing files under
111 // /dev/binderfs/binder_logs or /d/binder.
Yifan Hong1243dde2017-09-14 17:49:30 -0700112 // It is a virtual member function so that it can be mocked.
Yifan Hong8bf73162017-09-07 18:06:13 -0700113 virtual bool getPidInfo(pid_t serverPid, PidInfo *info) const;
Yifan Hong1243dde2017-09-14 17:49:30 -0700114 // Retrieve from mCachedPidInfos and call getPidInfo if necessary.
115 const PidInfo* getPidInfoCached(pid_t serverPid);
Steven Morelandd8e20192017-05-24 11:23:08 -0700116
Yifan Hongca3b6602017-09-07 16:44:27 -0700117 void dumpTable(const NullableOStream<std::ostream>& out) const;
118 void dumpVintf(const NullableOStream<std::ostream>& out) const;
Yifan Hong1bc1e9f2017-08-29 17:28:12 -0700119 void addLine(TextTable *table, const std::string &interfaceName, const std::string &transport,
120 const std::string &arch, const std::string &threadUsage, const std::string &server,
121 const std::string &serverCmdline, const std::string &address,
122 const std::string &clients, const std::string &clientCmdlines) const;
123 void addLine(TextTable *table, const TableEntry &entry);
Yifan Hong8bf73162017-09-07 18:06:13 -0700124 // Read and return /proc/{pid}/cmdline.
125 virtual std::string parseCmdline(pid_t pid) const;
Yifan Hong443df792017-05-09 18:49:45 -0700126 // Return /proc/{pid}/cmdline if it exists, else empty string.
Yifan Hongf31aa052018-02-02 15:17:51 -0800127 const std::string& getCmdline(pid_t pid);
Yifan Hong443df792017-05-09 18:49:45 -0700128 // Call getCmdline on all pid in pids. If it returns empty string, the process might
129 // have died, and the pid is removed from pids.
130 void removeDeadProcesses(Pids *pids);
Yifan Hongf31aa052018-02-02 15:17:51 -0800131
132 virtual Partition getPartition(pid_t pid);
Yifan Hongb2d096a2018-05-01 15:25:23 -0700133 Partition resolvePartition(Partition processPartition, const FqInstance &fqInstance) const;
Yifan Hongf31aa052018-02-02 15:17:51 -0800134
Yifan Hongbdf44f82018-05-25 14:20:00 -0700135 VintfInfo getVintfInfo(const std::string &fqInstanceName, vintf::TransportArch ta) const;
136 // Allow to mock these functions for testing.
137 virtual std::shared_ptr<const vintf::HalManifest> getDeviceManifest() const;
138 virtual std::shared_ptr<const vintf::CompatibilityMatrix> getDeviceMatrix() const;
139 virtual std::shared_ptr<const vintf::HalManifest> getFrameworkManifest() const;
140 virtual std::shared_ptr<const vintf::CompatibilityMatrix> getFrameworkMatrix() const;
141
Yifan Hong443df792017-05-09 18:49:45 -0700142 void forEachTable(const std::function<void(Table &)> &f);
143 void forEachTable(const std::function<void(const Table &)> &f) const;
Yifan Hongdb730532018-06-25 16:32:01 -0700144 Table* tableForType(HalType type);
145 const Table* tableForType(HalType type) const;
Yifan Hong443df792017-05-09 18:49:45 -0700146
Yifan Hong76ac14a2017-09-08 14:59:04 -0700147 NullableOStream<std::ostream> err() const;
148 NullableOStream<std::ostream> out() const;
149
Yifan Honga6b93f02017-09-13 16:53:37 -0700150 void registerAllOptions();
151
Yifan Hongb2d096a2018-05-01 15:25:23 -0700152 // helper functions to dumpVintf.
153 bool addEntryWithInstance(const TableEntry &entry, vintf::HalManifest *manifest) const;
154 bool addEntryWithoutInstance(const TableEntry &entry, const vintf::HalManifest *manifest) const;
155
Yifan Hong13ba0a92018-06-25 16:15:56 -0700156 // Helper function. Whether to fetch entries corresponding to a given HAL type.
157 bool shouldFetchHalType(const HalType &type) const;
158
159 void initFetchTypes();
Nirav Atrecce988d2018-05-16 11:14:46 -0700160
Yifan Hong3212f172018-06-28 12:39:50 -0700161 // Helper functions ti add HALs that are listed in VINTF manifest to LAZY_HALS table.
162 bool hasHwbinderEntry(const TableEntry& entry) const;
163 bool hasPassthroughEntry(const TableEntry& entry) const;
164
Yifan Hong443df792017-05-09 18:49:45 -0700165 Table mServicesTable{};
166 Table mPassthroughRefTable{};
167 Table mImplementationsTable{};
Yifan Hong13ba0a92018-06-25 16:15:56 -0700168 Table mManifestHalsTable{};
Yifan Hong3212f172018-06-28 12:39:50 -0700169 Table mLazyHalsTable{};
Yifan Hong443df792017-05-09 18:49:45 -0700170
Yifan Hongca3b6602017-09-07 16:44:27 -0700171 std::string mFileOutputPath;
Yifan Hong443df792017-05-09 18:49:45 -0700172 TableEntryCompare mSortColumn = nullptr;
Yifan Hong443df792017-05-09 18:49:45 -0700173
Yifan Hong443df792017-05-09 18:49:45 -0700174 bool mEmitDebugInfo = false;
175
Yifan Hongf31aa052018-02-02 15:17:51 -0800176 // If true, output in VINTF format. Output only entries from the specified partition.
Yifan Hong443df792017-05-09 18:49:45 -0700177 bool mVintf = false;
Yifan Hongf31aa052018-02-02 15:17:51 -0800178 Partition mVintfPartition = Partition::UNKNOWN;
Yifan Hong6da06912017-05-12 16:56:43 -0700179
180 // If true, explanatory text are not emitted.
181 bool mNeat = false;
182
Yifan Hong13ba0a92018-06-25 16:15:56 -0700183 // Type(s) of HAL associations to list.
184 std::vector<HalType> mListTypes{};
185 // Type(s) of HAL associations to fetch.
186 std::set<HalType> mFetchTypes{};
Nirav Atrecce988d2018-05-16 11:14:46 -0700187
Yifan Hong443df792017-05-09 18:49:45 -0700188 // If an entry does not exist, need to ask /proc/{pid}/cmdline to get it.
189 // If an entry exist but is an empty string, process might have died.
190 // If an entry exist and not empty, it contains the cached content of /proc/{pid}/cmdline.
191 std::map<pid_t, std::string> mCmdlines;
192
Yifan Hong1243dde2017-09-14 17:49:30 -0700193 // Cache for getPidInfo.
194 std::map<pid_t, PidInfo> mCachedPidInfos;
195
Yifan Hongf31aa052018-02-02 15:17:51 -0800196 // Cache for getPartition.
197 std::map<pid_t, Partition> mPartitions;
198
Yifan Honga6b93f02017-09-13 16:53:37 -0700199 RegisteredOptions mOptions;
200 // All selected columns
201 std::vector<TableColumnType> mSelectedColumns;
202 // If true, emit cmdlines instead of PIDs
203 bool mEnableCmdlines = false;
204
Yifan Hong91e655d2017-09-13 15:44:56 -0700205private:
Yifan Hong443df792017-05-09 18:49:45 -0700206 DISALLOW_COPY_AND_ASSIGN(ListCommand);
207};
208
209
210} // namespace lshal
211} // namespace android