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 #define LOG_TAG "lshal"
18 #include <android-base/logging.h>
19
20 #include "Lshal.h"
21
22 #include <set>
23 #include <string>
24
25 #include <hidl/ServiceManagement.h>
26
27 #include "DebugCommand.h"
28 #include "ListCommand.h"
29 #include "PipeRelay.h"
30
31 namespace android {
32 namespace lshal {
33
34 using ::android::hidl::manager::V1_0::IServiceManager;
35
Lshal()36 Lshal::Lshal()
37 : mOut(std::cout), mErr(std::cerr),
38 mServiceManager(::android::hardware::defaultServiceManager()),
39 mPassthroughManager(::android::hardware::getPassthroughServiceManager()) {
40 }
41
Lshal(std::ostream & out,std::ostream & err,sp<hidl::manager::V1_0::IServiceManager> serviceManager,sp<hidl::manager::V1_0::IServiceManager> passthroughManager)42 Lshal::Lshal(std::ostream &out, std::ostream &err,
43 sp<hidl::manager::V1_0::IServiceManager> serviceManager,
44 sp<hidl::manager::V1_0::IServiceManager> passthroughManager)
45 : mOut(out), mErr(err),
46 mServiceManager(serviceManager),
47 mPassthroughManager(passthroughManager) {
48
49 }
50
usage(const std::string & command) const51 void Lshal::usage(const std::string &command) const {
52 static const std::string helpSummary =
53 "lshal: List and debug HALs.\n"
54 "\n"
55 "commands:\n"
56 " help Print help message\n"
57 " list list HALs\n"
58 " debug debug a specified HAL\n"
59 "\n"
60 "If no command is specified, `list` is the default.\n";
61
62 static const std::string list =
63 "list:\n"
64 " lshal\n"
65 " lshal list\n"
66 " List all hals with default ordering and columns (`lshal list -ipc`)\n"
67 " lshal list [-h|--help]\n"
68 " -h, --help: Print help message for list (`lshal help list`)\n"
69 " lshal [list] [--interface|-i] [--transport|-t] [-r|--arch]\n"
70 " [--pid|-p] [--address|-a] [--clients|-c] [--cmdline|-m]\n"
71 " [--sort={interface|i|pid|p}] [--init-vintf[=<output file>]]\n"
72 " [--debug|-d[=<output file>]]\n"
73 " -i, --interface: print the interface name column\n"
74 " -n, --instance: print the instance name column\n"
75 " -t, --transport: print the transport mode column\n"
76 " -r, --arch: print if the HAL is in 64-bit or 32-bit\n"
77 " -p, --pid: print the server PID, or server cmdline if -m is set\n"
78 " -a, --address: print the server object address column\n"
79 " -c, --clients: print the client PIDs, or client cmdlines if -m is set\n"
80 " -m, --cmdline: print cmdline instead of PIDs\n"
81 " -d[=<output file>], --debug[=<output file>]: emit debug info from \n"
82 " IBase::debug with empty options\n"
83 " --sort=i, --sort=interface: sort by interface name\n"
84 " --sort=p, --sort=pid: sort by server pid\n"
85 " --init-vintf=<output file>: form a skeleton HAL manifest to specified\n"
86 " file, or stdout if no file specified.\n";
87
88 static const std::string debug =
89 "debug:\n"
90 " lshal debug <interface> [options [options [...]]] \n"
91 " Print debug information of a specified interface.\n"
92 " <inteface>: Format is `android.hardware.foo@1.0::IFoo/default`.\n"
93 " If instance name is missing `default` is used.\n"
94 " options: space separated options to IBase::debug.\n";
95
96 static const std::string help =
97 "help:\n"
98 " lshal -h\n"
99 " lshal --help\n"
100 " lshal help\n"
101 " Print this help message\n"
102 " lshal help list\n"
103 " Print help message for list\n"
104 " lshal help debug\n"
105 " Print help message for debug\n";
106
107 if (command == "list") {
108 mErr << list;
109 return;
110 }
111 if (command == "debug") {
112 mErr << debug;
113 return;
114 }
115
116 mErr << helpSummary << "\n" << list << "\n" << debug << "\n" << help;
117 }
118
119 // A unique_ptr type using a custom deleter function.
120 template<typename T>
121 using deleted_unique_ptr = std::unique_ptr<T, std::function<void(T *)> >;
122
convert(const std::vector<std::string> & v)123 static hardware::hidl_vec<hardware::hidl_string> convert(const std::vector<std::string> &v) {
124 hardware::hidl_vec<hardware::hidl_string> hv;
125 hv.resize(v.size());
126 for (size_t i = 0; i < v.size(); ++i) {
127 hv[i].setToExternal(v[i].c_str(), v[i].size());
128 }
129 return hv;
130 }
131
emitDebugInfo(const std::string & interfaceName,const std::string & instanceName,const std::vector<std::string> & options,std::ostream & out,NullableOStream<std::ostream> err) const132 Status Lshal::emitDebugInfo(
133 const std::string &interfaceName,
134 const std::string &instanceName,
135 const std::vector<std::string> &options,
136 std::ostream &out,
137 NullableOStream<std::ostream> err) const {
138 using android::hidl::base::V1_0::IBase;
139
140 hardware::Return<sp<IBase>> retBase = serviceManager()->get(interfaceName, instanceName);
141
142 if (!retBase.isOk()) {
143 std::string msg = "Cannot get " + interfaceName + "/" + instanceName + ": "
144 + retBase.description();
145 err << msg << std::endl;
146 LOG(ERROR) << msg;
147 return TRANSACTION_ERROR;
148 }
149
150 sp<IBase> base = retBase;
151 if (base == nullptr) {
152 std::string msg = interfaceName + "/" + instanceName + " does not exist, or "
153 + "no permission to connect.";
154 err << msg << std::endl;
155 LOG(ERROR) << msg;
156 return NO_INTERFACE;
157 }
158
159 PipeRelay relay(out);
160
161 if (relay.initCheck() != OK) {
162 std::string msg = "PipeRelay::initCheck() FAILED w/ " + std::to_string(relay.initCheck());
163 err << msg << std::endl;
164 LOG(ERROR) << msg;
165 return IO_ERROR;
166 }
167
168 deleted_unique_ptr<native_handle_t> fdHandle(
169 native_handle_create(1 /* numFds */, 0 /* numInts */),
170 native_handle_delete);
171
172 fdHandle->data[0] = relay.fd();
173
174 hardware::Return<void> ret = base->debug(fdHandle.get(), convert(options));
175
176 if (!ret.isOk()) {
177 std::string msg = "debug() FAILED on " + interfaceName + "/" + instanceName + ": "
178 + ret.description();
179 err << msg << std::endl;
180 LOG(ERROR) << msg;
181 return TRANSACTION_ERROR;
182 }
183 return OK;
184 }
185
parseArgs(const Arg & arg)186 Status Lshal::parseArgs(const Arg &arg) {
187 static std::set<std::string> sAllCommands{"list", "debug", "help"};
188 optind = 1;
189 if (optind >= arg.argc) {
190 // no options at all.
191 return OK;
192 }
193 mCommand = arg.argv[optind];
194 if (sAllCommands.find(mCommand) != sAllCommands.end()) {
195 ++optind;
196 return OK; // mCommand is set correctly
197 }
198
199 if (mCommand.size() > 0 && mCommand[0] == '-') {
200 // first argument is an option, set command to "" (which is recognized as "list")
201 mCommand = "";
202 return OK;
203 }
204
205 mErr << arg.argv[0] << ": unrecognized option `" << arg.argv[optind] << "`" << std::endl;
206 usage();
207 return USAGE;
208 }
209
signalHandler(int sig)210 void signalHandler(int sig) {
211 if (sig == SIGINT) {
212 int retVal;
213 pthread_exit(&retVal);
214 }
215 }
216
main(const Arg & arg)217 Status Lshal::main(const Arg &arg) {
218 // Allow SIGINT to terminate all threads.
219 signal(SIGINT, signalHandler);
220
221 Status status = parseArgs(arg);
222 if (status != OK) {
223 return status;
224 }
225 if (mCommand == "help") {
226 usage(optind < arg.argc ? arg.argv[optind] : "");
227 return USAGE;
228 }
229 // Default command is list
230 if (mCommand == "list" || mCommand == "") {
231 return ListCommand{*this}.main(mCommand, arg);
232 }
233 if (mCommand == "debug") {
234 return DebugCommand{*this}.main(mCommand, arg);
235 }
236 usage();
237 return USAGE;
238 }
239
err() const240 NullableOStream<std::ostream> Lshal::err() const {
241 return mErr;
242 }
out() const243 NullableOStream<std::ostream> Lshal::out() const {
244 return mOut;
245 }
246
serviceManager() const247 const sp<IServiceManager> &Lshal::serviceManager() const {
248 return mServiceManager;
249 }
250
passthroughManager() const251 const sp<IServiceManager> &Lshal::passthroughManager() const {
252 return mPassthroughManager;
253 }
254
255 } // namespace lshal
256 } // namespace android
257