blob: 88faac149d6f47c69ec2b75fbfa501d0f93d68cd [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 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
Yifan Hongded398e2017-09-07 13:54:28 -070049class ListCommand : public Command {
Yifan Hong443df792017-05-09 18:49:45 -070050public:
Yifan Hongded398e2017-09-07 13:54:28 -070051 ListCommand(Lshal &lshal) : Command(lshal) {}
Yifan Hong8bf73162017-09-07 18:06:13 -070052 virtual ~ListCommand() = default;
Yifan Honga8bedc62017-09-08 18:00:31 -070053 Status main(const Arg &arg) override;
54 void usage() const override;
Yifan Hong795b6ec2017-09-13 11:25:28 -070055 std::string getSimpleDescription() const override;
56 std::string getName() const override { return GetName(); }
57
58 static std::string GetName();
Yifan Honga6b93f02017-09-13 16:53:37 -070059
60 struct RegisteredOption {
61 // short alternative, e.g. 'v'. If '\0', no short options is available.
62 char shortOption;
63 // long alternative, e.g. 'init-vintf'
64 std::string longOption;
65 // no_argument, required_argument or optional_argument
66 int hasArg;
67 // value written to 'flag' by getopt_long
68 int val;
69 // operation when the argument is present
70 std::function<Status(ListCommand* thiz, const char* arg)> op;
71 // help message
72 std::string help;
73
74 const std::string& getHelpMessageForArgument() const;
75 };
76 // A list of acceptable command line options
77 // key: value returned by getopt_long
78 using RegisteredOptions = std::vector<RegisteredOption>;
79
Yifan Hongf31aa052018-02-02 15:17:51 -080080 static std::string INIT_VINTF_NOTES;
81
Yifan Hongb2a2ecb2017-09-07 15:08:22 -070082protected:
Yifan Honga8bedc62017-09-08 18:00:31 -070083 Status parseArgs(const Arg &arg);
Yifan Hong443df792017-05-09 18:49:45 -070084 Status fetch();
Yifan Hong93b8bff2017-09-14 16:02:52 -070085 virtual void postprocess();
Yifan Hongca3b6602017-09-07 16:44:27 -070086 Status dump();
Yifan Hong443df792017-05-09 18:49:45 -070087 void putEntry(TableEntrySource source, TableEntry &&entry);
88 Status fetchPassthrough(const sp<::android::hidl::manager::V1_0::IServiceManager> &manager);
89 Status fetchBinderized(const sp<::android::hidl::manager::V1_0::IServiceManager> &manager);
90 Status fetchAllLibraries(const sp<::android::hidl::manager::V1_0::IServiceManager> &manager);
Steven Morelandd8e20192017-05-24 11:23:08 -070091
Yifan Hong22ea7b82017-09-14 18:07:43 -070092 Status fetchBinderizedEntry(const sp<::android::hidl::manager::V1_0::IServiceManager> &manager,
93 TableEntry *entry);
94
Yifan Hong1243dde2017-09-14 17:49:30 -070095 // Get relevant information for a PID by parsing files under /d/binder.
96 // It is a virtual member function so that it can be mocked.
Yifan Hong8bf73162017-09-07 18:06:13 -070097 virtual bool getPidInfo(pid_t serverPid, PidInfo *info) const;
Yifan Hong1243dde2017-09-14 17:49:30 -070098 // Retrieve from mCachedPidInfos and call getPidInfo if necessary.
99 const PidInfo* getPidInfoCached(pid_t serverPid);
Steven Morelandd8e20192017-05-24 11:23:08 -0700100
Yifan Hongca3b6602017-09-07 16:44:27 -0700101 void dumpTable(const NullableOStream<std::ostream>& out) const;
102 void dumpVintf(const NullableOStream<std::ostream>& out) const;
Yifan Hong1bc1e9f2017-08-29 17:28:12 -0700103 void addLine(TextTable *table, const std::string &interfaceName, const std::string &transport,
104 const std::string &arch, const std::string &threadUsage, const std::string &server,
105 const std::string &serverCmdline, const std::string &address,
106 const std::string &clients, const std::string &clientCmdlines) const;
107 void addLine(TextTable *table, const TableEntry &entry);
Yifan Hong8bf73162017-09-07 18:06:13 -0700108 // Read and return /proc/{pid}/cmdline.
109 virtual std::string parseCmdline(pid_t pid) const;
Yifan Hong443df792017-05-09 18:49:45 -0700110 // Return /proc/{pid}/cmdline if it exists, else empty string.
Yifan Hongf31aa052018-02-02 15:17:51 -0800111 const std::string& getCmdline(pid_t pid);
Yifan Hong443df792017-05-09 18:49:45 -0700112 // Call getCmdline on all pid in pids. If it returns empty string, the process might
113 // have died, and the pid is removed from pids.
114 void removeDeadProcesses(Pids *pids);
Yifan Hongf31aa052018-02-02 15:17:51 -0800115
116 virtual Partition getPartition(pid_t pid);
Yifan Hongb2d096a2018-05-01 15:25:23 -0700117 Partition resolvePartition(Partition processPartition, const FqInstance &fqInstance) const;
Yifan Hongf31aa052018-02-02 15:17:51 -0800118
Yifan Hong443df792017-05-09 18:49:45 -0700119 void forEachTable(const std::function<void(Table &)> &f);
120 void forEachTable(const std::function<void(const Table &)> &f) const;
121
Yifan Hong76ac14a2017-09-08 14:59:04 -0700122 NullableOStream<std::ostream> err() const;
123 NullableOStream<std::ostream> out() const;
124
Yifan Honga6b93f02017-09-13 16:53:37 -0700125 void registerAllOptions();
126
Yifan Hongb2d096a2018-05-01 15:25:23 -0700127 // helper functions to dumpVintf.
128 bool addEntryWithInstance(const TableEntry &entry, vintf::HalManifest *manifest) const;
129 bool addEntryWithoutInstance(const TableEntry &entry, const vintf::HalManifest *manifest) const;
130
Yifan Hong443df792017-05-09 18:49:45 -0700131 Table mServicesTable{};
132 Table mPassthroughRefTable{};
133 Table mImplementationsTable{};
134
Yifan Hongca3b6602017-09-07 16:44:27 -0700135 std::string mFileOutputPath;
Yifan Hong443df792017-05-09 18:49:45 -0700136 TableEntryCompare mSortColumn = nullptr;
Yifan Hong443df792017-05-09 18:49:45 -0700137
Yifan Hong443df792017-05-09 18:49:45 -0700138 bool mEmitDebugInfo = false;
139
Yifan Hongf31aa052018-02-02 15:17:51 -0800140 // If true, output in VINTF format. Output only entries from the specified partition.
Yifan Hong443df792017-05-09 18:49:45 -0700141 bool mVintf = false;
Yifan Hongf31aa052018-02-02 15:17:51 -0800142 Partition mVintfPartition = Partition::UNKNOWN;
Yifan Hong6da06912017-05-12 16:56:43 -0700143
144 // If true, explanatory text are not emitted.
145 bool mNeat = false;
146
Yifan Hong443df792017-05-09 18:49:45 -0700147 // If an entry does not exist, need to ask /proc/{pid}/cmdline to get it.
148 // If an entry exist but is an empty string, process might have died.
149 // If an entry exist and not empty, it contains the cached content of /proc/{pid}/cmdline.
150 std::map<pid_t, std::string> mCmdlines;
151
Yifan Hong1243dde2017-09-14 17:49:30 -0700152 // Cache for getPidInfo.
153 std::map<pid_t, PidInfo> mCachedPidInfos;
154
Yifan Hongf31aa052018-02-02 15:17:51 -0800155 // Cache for getPartition.
156 std::map<pid_t, Partition> mPartitions;
157
Yifan Honga6b93f02017-09-13 16:53:37 -0700158 RegisteredOptions mOptions;
159 // All selected columns
160 std::vector<TableColumnType> mSelectedColumns;
161 // If true, emit cmdlines instead of PIDs
162 bool mEnableCmdlines = false;
163
Yifan Hong91e655d2017-09-13 15:44:56 -0700164private:
Yifan Hong443df792017-05-09 18:49:45 -0700165 DISALLOW_COPY_AND_ASSIGN(ListCommand);
166};
167
168
169} // namespace lshal
170} // namespace android
171
172#endif // FRAMEWORK_NATIVE_CMDS_LSHAL_LIST_COMMAND_H_