blob: 9c5c234f1612b5bc2ada5b02175fb059005dbc81 [file] [log] [blame]
Yifan Hongb0dde932017-02-10 17:49:58 -08001/*
Yifan Hong443df792017-05-09 18:49:45 -07002 * Copyright (C) 2017 The Android Open Source Project
Yifan Hongb0dde932017-02-10 17:49:58 -08003 *
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
Yifan Hong48dc9f82017-05-09 19:33:08 -070017#define LOG_TAG "lshal"
18#include <android-base/logging.h>
19
Yifan Hongb0dde932017-02-10 17:49:58 -080020#include "Lshal.h"
21
Yifan Hong443df792017-05-09 18:49:45 -070022#include <set>
23#include <string>
Yifan Hongb0dde932017-02-10 17:49:58 -080024
Yifan Hong48dc9f82017-05-09 19:33:08 -070025#include <hidl/ServiceManagement.h>
26
27#include "DebugCommand.h"
Yifan Hong443df792017-05-09 18:49:45 -070028#include "ListCommand.h"
Andreas Huber28d35912017-03-24 13:14:11 -070029#include "PipeRelay.h"
Yifan Hongb0dde932017-02-10 17:49:58 -080030
31namespace android {
32namespace lshal {
33
Yifan Hong443df792017-05-09 18:49:45 -070034using ::android::hidl::manager::V1_0::IServiceManager;
Yifan Hongb0dde932017-02-10 17:49:58 -080035
Yifan Hong9881df92017-05-10 14:33:05 -070036Lshal::Lshal()
37 : mOut(std::cout), mErr(std::cerr),
38 mServiceManager(::android::hardware::defaultServiceManager()),
39 mPassthroughManager(::android::hardware::getPassthroughServiceManager()) {
40}
41
42Lshal::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
Yifan Hong443df792017-05-09 18:49:45 -070049}
50
51void 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"
Yifan Hong1bc1e9f2017-08-29 17:28:12 -070066 " List all hals with default ordering and columns (`lshal list -iepc`)\n"
Yifan Hong443df792017-05-09 18:49:45 -070067 " lshal list [-h|--help]\n"
68 " -h, --help: Print help message for list (`lshal help list`)\n"
Steven Morelandd8e20192017-05-24 11:23:08 -070069 " lshal [list] [--interface|-i] [--transport|-t] [-r|--arch] [-e|--threads]\n"
Yifan Hong443df792017-05-09 18:49:45 -070070 " [--pid|-p] [--address|-a] [--clients|-c] [--cmdline|-m]\n"
71 " [--sort={interface|i|pid|p}] [--init-vintf[=<output file>]]\n"
Yifan Hong45a0ead2017-08-15 10:01:02 -070072 " [--debug|-d[=<output file>]] [--neat]\n"
Yifan Hong443df792017-05-09 18:49:45 -070073 " -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"
Steven Morelandd8e20192017-05-24 11:23:08 -070077 " -e, --threads: print currently used/available threads\n"
78 " (note, available threads created lazily)\n"
Yifan Hong443df792017-05-09 18:49:45 -070079 " -p, --pid: print the server PID, or server cmdline if -m is set\n"
80 " -a, --address: print the server object address column\n"
81 " -c, --clients: print the client PIDs, or client cmdlines if -m is set\n"
82 " -m, --cmdline: print cmdline instead of PIDs\n"
83 " -d[=<output file>], --debug[=<output file>]: emit debug info from \n"
Yifan Hong1bc1e9f2017-08-29 17:28:12 -070084 " IBase::debug with empty options. Cannot be used with --neat.\n"
Yifan Hong443df792017-05-09 18:49:45 -070085 " --sort=i, --sort=interface: sort by interface name\n"
86 " --sort=p, --sort=pid: sort by server pid\n"
Yifan Hong45a0ead2017-08-15 10:01:02 -070087 " --neat: output is machine parsable (no explanatory text)\n"
Yifan Hong1bc1e9f2017-08-29 17:28:12 -070088 " Cannot be used with --debug.\n"
Yifan Hong45a0ead2017-08-15 10:01:02 -070089 " --init-vintf[=<output file>]: form a skeleton HAL manifest to specified\n"
Yifan Hong443df792017-05-09 18:49:45 -070090 " file, or stdout if no file specified.\n";
91
92 static const std::string debug =
93 "debug:\n"
94 " lshal debug <interface> [options [options [...]]] \n"
95 " Print debug information of a specified interface.\n"
96 " <inteface>: Format is `android.hardware.foo@1.0::IFoo/default`.\n"
97 " If instance name is missing `default` is used.\n"
98 " options: space separated options to IBase::debug.\n";
99
100 static const std::string help =
101 "help:\n"
102 " lshal -h\n"
103 " lshal --help\n"
104 " lshal help\n"
105 " Print this help message\n"
106 " lshal help list\n"
107 " Print help message for list\n"
108 " lshal help debug\n"
109 " Print help message for debug\n";
110
111 if (command == "list") {
Yifan Hong76ac14a2017-09-08 14:59:04 -0700112 err() << list;
Yifan Hong443df792017-05-09 18:49:45 -0700113 return;
Yifan Hongb0dde932017-02-10 17:49:58 -0800114 }
Yifan Hong443df792017-05-09 18:49:45 -0700115 if (command == "debug") {
Yifan Hong76ac14a2017-09-08 14:59:04 -0700116 err() << debug;
Yifan Hong443df792017-05-09 18:49:45 -0700117 return;
Yifan Hongb0dde932017-02-10 17:49:58 -0800118 }
119
Yifan Hong76ac14a2017-09-08 14:59:04 -0700120 err() << helpSummary << "\n" << list << "\n" << debug << "\n" << help;
Yifan Hongb4479022017-03-02 16:54:11 -0800121}
122
Andreas Huber28d35912017-03-24 13:14:11 -0700123// A unique_ptr type using a custom deleter function.
124template<typename T>
125using deleted_unique_ptr = std::unique_ptr<T, std::function<void(T *)> >;
126
Yifan Hong443df792017-05-09 18:49:45 -0700127static hardware::hidl_vec<hardware::hidl_string> convert(const std::vector<std::string> &v) {
128 hardware::hidl_vec<hardware::hidl_string> hv;
129 hv.resize(v.size());
130 for (size_t i = 0; i < v.size(); ++i) {
131 hv[i].setToExternal(v[i].c_str(), v[i].size());
132 }
133 return hv;
134}
135
Yifan Hong48dc9f82017-05-09 19:33:08 -0700136Status Lshal::emitDebugInfo(
Andreas Huber28d35912017-03-24 13:14:11 -0700137 const std::string &interfaceName,
Yifan Hong443df792017-05-09 18:49:45 -0700138 const std::string &instanceName,
139 const std::vector<std::string> &options,
Yifan Hong48dc9f82017-05-09 19:33:08 -0700140 std::ostream &out,
141 NullableOStream<std::ostream> err) const {
Andreas Huber28d35912017-03-24 13:14:11 -0700142 using android::hidl::base::V1_0::IBase;
143
Yifan Hong9881df92017-05-10 14:33:05 -0700144 hardware::Return<sp<IBase>> retBase = serviceManager()->get(interfaceName, instanceName);
Andreas Huber28d35912017-03-24 13:14:11 -0700145
Yifan Hong48dc9f82017-05-09 19:33:08 -0700146 if (!retBase.isOk()) {
147 std::string msg = "Cannot get " + interfaceName + "/" + instanceName + ": "
148 + retBase.description();
149 err << msg << std::endl;
150 LOG(ERROR) << msg;
151 return TRANSACTION_ERROR;
152 }
153
154 sp<IBase> base = retBase;
155 if (base == nullptr) {
156 std::string msg = interfaceName + "/" + instanceName + " does not exist, or "
157 + "no permission to connect.";
158 err << msg << std::endl;
159 LOG(ERROR) << msg;
160 return NO_INTERFACE;
Andreas Huber28d35912017-03-24 13:14:11 -0700161 }
162
Yifan Hong443df792017-05-09 18:49:45 -0700163 PipeRelay relay(out);
Andreas Huber28d35912017-03-24 13:14:11 -0700164
165 if (relay.initCheck() != OK) {
Yifan Hong48dc9f82017-05-09 19:33:08 -0700166 std::string msg = "PipeRelay::initCheck() FAILED w/ " + std::to_string(relay.initCheck());
167 err << msg << std::endl;
168 LOG(ERROR) << msg;
169 return IO_ERROR;
Andreas Huber28d35912017-03-24 13:14:11 -0700170 }
171
172 deleted_unique_ptr<native_handle_t> fdHandle(
173 native_handle_create(1 /* numFds */, 0 /* numInts */),
174 native_handle_delete);
175
176 fdHandle->data[0] = relay.fd();
177
Yifan Hong443df792017-05-09 18:49:45 -0700178 hardware::Return<void> ret = base->debug(fdHandle.get(), convert(options));
Andreas Huber28d35912017-03-24 13:14:11 -0700179
180 if (!ret.isOk()) {
Yifan Hong48dc9f82017-05-09 19:33:08 -0700181 std::string msg = "debug() FAILED on " + interfaceName + "/" + instanceName + ": "
182 + ret.description();
183 err << msg << std::endl;
184 LOG(ERROR) << msg;
185 return TRANSACTION_ERROR;
Andreas Huber28d35912017-03-24 13:14:11 -0700186 }
Yifan Hong48dc9f82017-05-09 19:33:08 -0700187 return OK;
Andreas Huber28d35912017-03-24 13:14:11 -0700188}
189
Yifan Hong443df792017-05-09 18:49:45 -0700190Status Lshal::parseArgs(const Arg &arg) {
191 static std::set<std::string> sAllCommands{"list", "debug", "help"};
Yifan Hongb0dde932017-02-10 17:49:58 -0800192 optind = 1;
Yifan Hong443df792017-05-09 18:49:45 -0700193 if (optind >= arg.argc) {
194 // no options at all.
195 return OK;
196 }
197 mCommand = arg.argv[optind];
198 if (sAllCommands.find(mCommand) != sAllCommands.end()) {
199 ++optind;
200 return OK; // mCommand is set correctly
Yifan Hongb0dde932017-02-10 17:49:58 -0800201 }
Yifan Hong38d53e02017-02-13 17:51:59 -0800202
Yifan Hong443df792017-05-09 18:49:45 -0700203 if (mCommand.size() > 0 && mCommand[0] == '-') {
204 // first argument is an option, set command to "" (which is recognized as "list")
205 mCommand = "";
206 return OK;
Yifan Hong38d53e02017-02-13 17:51:59 -0800207 }
Yifan Hong443df792017-05-09 18:49:45 -0700208
Yifan Hong76ac14a2017-09-08 14:59:04 -0700209 err() << arg.argv[0] << ": unrecognized option `" << arg.argv[optind] << "`" << std::endl;
Yifan Hong443df792017-05-09 18:49:45 -0700210 usage();
211 return USAGE;
Yifan Hongb0dde932017-02-10 17:49:58 -0800212}
213
Yifan Hong9881df92017-05-10 14:33:05 -0700214void signalHandler(int sig) {
215 if (sig == SIGINT) {
216 int retVal;
217 pthread_exit(&retVal);
218 }
219}
220
Yifan Hongded398e2017-09-07 13:54:28 -0700221std::unique_ptr<Command> Lshal::selectCommand(const std::string& command) {
222 // Default command is list
223 if (command == "list" || command == "") {
224 return std::make_unique<ListCommand>(*this);
225 }
226 if (command == "debug") {
227 return std::make_unique<DebugCommand>(*this);
228 }
229 return nullptr;
230}
231
Yifan Hong443df792017-05-09 18:49:45 -0700232Status Lshal::main(const Arg &arg) {
Yifan Hong9881df92017-05-10 14:33:05 -0700233 // Allow SIGINT to terminate all threads.
234 signal(SIGINT, signalHandler);
235
Yifan Hong443df792017-05-09 18:49:45 -0700236 Status status = parseArgs(arg);
Yifan Hongb0dde932017-02-10 17:49:58 -0800237 if (status != OK) {
238 return status;
239 }
Yifan Hong443df792017-05-09 18:49:45 -0700240 if (mCommand == "help") {
241 usage(optind < arg.argc ? arg.argv[optind] : "");
242 return USAGE;
243 }
Yifan Hongded398e2017-09-07 13:54:28 -0700244 auto c = selectCommand(mCommand);
245 if (c != nullptr) {
246 return c->main(mCommand, arg);
Yifan Hong443df792017-05-09 18:49:45 -0700247 }
248 usage();
249 return USAGE;
250}
251
252NullableOStream<std::ostream> Lshal::err() const {
253 return mErr;
254}
255NullableOStream<std::ostream> Lshal::out() const {
256 return mOut;
Yifan Hongb0dde932017-02-10 17:49:58 -0800257}
258
Yifan Hong9881df92017-05-10 14:33:05 -0700259const sp<IServiceManager> &Lshal::serviceManager() const {
260 return mServiceManager;
261}
262
263const sp<IServiceManager> &Lshal::passthroughManager() const {
264 return mPassthroughManager;
Yifan Honga57dffb2017-02-21 14:59:00 -0800265}
266
Yifan Hongb0dde932017-02-10 17:49:58 -0800267} // namespace lshal
268} // namespace android