blob: 5bc834c320c46c898449d4adf5872174658a69a5 [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>
29
Yifan Hongded398e2017-09-07 13:54:28 -070030#include "Command.h"
Yifan Hong443df792017-05-09 18:49:45 -070031#include "NullableOStream.h"
32#include "TableEntry.h"
Yifan Hong1bc1e9f2017-08-29 17:28:12 -070033#include "TextTable.h"
Yifan Hong443df792017-05-09 18:49:45 -070034#include "utils.h"
35
36namespace android {
37namespace lshal {
38
39class Lshal;
40
Yifan Hong8bf73162017-09-07 18:06:13 -070041struct PidInfo {
42 std::map<uint64_t, Pids> refPids; // pids that are referenced
43 uint32_t threadUsage; // number of threads in use
44 uint32_t threadCount; // number of threads total
45};
46
Yifan Hongded398e2017-09-07 13:54:28 -070047class ListCommand : public Command {
Yifan Hong443df792017-05-09 18:49:45 -070048public:
Yifan Hongded398e2017-09-07 13:54:28 -070049 ListCommand(Lshal &lshal) : Command(lshal) {}
Yifan Hong8bf73162017-09-07 18:06:13 -070050 virtual ~ListCommand() = default;
Yifan Honga8bedc62017-09-08 18:00:31 -070051 Status main(const Arg &arg) override;
52 void usage() const override;
Yifan Hong795b6ec2017-09-13 11:25:28 -070053 std::string getSimpleDescription() const override;
54 std::string getName() const override { return GetName(); }
55
56 static std::string GetName();
Yifan Honga6b93f02017-09-13 16:53:37 -070057
58 struct RegisteredOption {
59 // short alternative, e.g. 'v'. If '\0', no short options is available.
60 char shortOption;
61 // long alternative, e.g. 'init-vintf'
62 std::string longOption;
63 // no_argument, required_argument or optional_argument
64 int hasArg;
65 // value written to 'flag' by getopt_long
66 int val;
67 // operation when the argument is present
68 std::function<Status(ListCommand* thiz, const char* arg)> op;
69 // help message
70 std::string help;
71
72 const std::string& getHelpMessageForArgument() const;
73 };
74 // A list of acceptable command line options
75 // key: value returned by getopt_long
76 using RegisteredOptions = std::vector<RegisteredOption>;
77
Yifan Hongb2a2ecb2017-09-07 15:08:22 -070078protected:
Yifan Honga8bedc62017-09-08 18:00:31 -070079 Status parseArgs(const Arg &arg);
Yifan Hong443df792017-05-09 18:49:45 -070080 Status fetch();
81 void postprocess();
Yifan Hongca3b6602017-09-07 16:44:27 -070082 Status dump();
Yifan Hong443df792017-05-09 18:49:45 -070083 void putEntry(TableEntrySource source, TableEntry &&entry);
84 Status fetchPassthrough(const sp<::android::hidl::manager::V1_0::IServiceManager> &manager);
85 Status fetchBinderized(const sp<::android::hidl::manager::V1_0::IServiceManager> &manager);
86 Status fetchAllLibraries(const sp<::android::hidl::manager::V1_0::IServiceManager> &manager);
Steven Morelandd8e20192017-05-24 11:23:08 -070087
Yifan Hong8bf73162017-09-07 18:06:13 -070088 virtual bool getPidInfo(pid_t serverPid, PidInfo *info) const;
Steven Morelandd8e20192017-05-24 11:23:08 -070089
Yifan Hongca3b6602017-09-07 16:44:27 -070090 void dumpTable(const NullableOStream<std::ostream>& out) const;
91 void dumpVintf(const NullableOStream<std::ostream>& out) const;
Yifan Hong1bc1e9f2017-08-29 17:28:12 -070092 void addLine(TextTable *table, const std::string &interfaceName, const std::string &transport,
93 const std::string &arch, const std::string &threadUsage, const std::string &server,
94 const std::string &serverCmdline, const std::string &address,
95 const std::string &clients, const std::string &clientCmdlines) const;
96 void addLine(TextTable *table, const TableEntry &entry);
Yifan Hong8bf73162017-09-07 18:06:13 -070097 // Read and return /proc/{pid}/cmdline.
98 virtual std::string parseCmdline(pid_t pid) const;
Yifan Hong443df792017-05-09 18:49:45 -070099 // Return /proc/{pid}/cmdline if it exists, else empty string.
100 const std::string &getCmdline(pid_t pid);
101 // Call getCmdline on all pid in pids. If it returns empty string, the process might
102 // have died, and the pid is removed from pids.
103 void removeDeadProcesses(Pids *pids);
104 void forEachTable(const std::function<void(Table &)> &f);
105 void forEachTable(const std::function<void(const Table &)> &f) const;
106
Yifan Hong76ac14a2017-09-08 14:59:04 -0700107 NullableOStream<std::ostream> err() const;
108 NullableOStream<std::ostream> out() const;
109
Yifan Honga6b93f02017-09-13 16:53:37 -0700110 void registerAllOptions();
111
Yifan Hong443df792017-05-09 18:49:45 -0700112 Table mServicesTable{};
113 Table mPassthroughRefTable{};
114 Table mImplementationsTable{};
115
Yifan Hongca3b6602017-09-07 16:44:27 -0700116 std::string mFileOutputPath;
Yifan Hong443df792017-05-09 18:49:45 -0700117 TableEntryCompare mSortColumn = nullptr;
Yifan Hong443df792017-05-09 18:49:45 -0700118
Yifan Hong443df792017-05-09 18:49:45 -0700119 bool mEmitDebugInfo = false;
120
Yifan Hong6da06912017-05-12 16:56:43 -0700121 // If true, output in VINTF format.
Yifan Hong443df792017-05-09 18:49:45 -0700122 bool mVintf = false;
Yifan Hong6da06912017-05-12 16:56:43 -0700123
124 // If true, explanatory text are not emitted.
125 bool mNeat = false;
126
Yifan Hong443df792017-05-09 18:49:45 -0700127 // If an entry does not exist, need to ask /proc/{pid}/cmdline to get it.
128 // If an entry exist but is an empty string, process might have died.
129 // If an entry exist and not empty, it contains the cached content of /proc/{pid}/cmdline.
130 std::map<pid_t, std::string> mCmdlines;
131
Yifan Honga6b93f02017-09-13 16:53:37 -0700132 RegisteredOptions mOptions;
133 // All selected columns
134 std::vector<TableColumnType> mSelectedColumns;
135 // If true, emit cmdlines instead of PIDs
136 bool mEnableCmdlines = false;
137
Yifan Hong91e655d2017-09-13 15:44:56 -0700138private:
Yifan Hong443df792017-05-09 18:49:45 -0700139 DISALLOW_COPY_AND_ASSIGN(ListCommand);
140};
141
142
143} // namespace lshal
144} // namespace android
145
146#endif // FRAMEWORK_NATIVE_CMDS_LSHAL_LIST_COMMAND_H_