blob: 0d4a1d193adfa62015843ba230fa5cf7ab791074 [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"
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"
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"
84 " IBase::debug with empty options\n"
85 " --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"
88 " --init-vintf[=<output file>]: form a skeleton HAL manifest to specified\n"
Yifan Hong443df792017-05-09 18:49:45 -070089 " file, or stdout if no file specified.\n";
90
91 static const std::string debug =
92 "debug:\n"
93 " lshal debug <interface> [options [options [...]]] \n"
94 " Print debug information of a specified interface.\n"
95 " <inteface>: Format is `android.hardware.foo@1.0::IFoo/default`.\n"
96 " If instance name is missing `default` is used.\n"
97 " options: space separated options to IBase::debug.\n";
98
99 static const std::string help =
100 "help:\n"
101 " lshal -h\n"
102 " lshal --help\n"
103 " lshal help\n"
104 " Print this help message\n"
105 " lshal help list\n"
106 " Print help message for list\n"
107 " lshal help debug\n"
108 " Print help message for debug\n";
109
110 if (command == "list") {
111 mErr << list;
112 return;
Yifan Hongb0dde932017-02-10 17:49:58 -0800113 }
Yifan Hong443df792017-05-09 18:49:45 -0700114 if (command == "debug") {
115 mErr << debug;
116 return;
Yifan Hongb0dde932017-02-10 17:49:58 -0800117 }
118
Yifan Hong443df792017-05-09 18:49:45 -0700119 mErr << helpSummary << "\n" << list << "\n" << debug << "\n" << help;
Yifan Hongb4479022017-03-02 16:54:11 -0800120}
121
Andreas Huber28d35912017-03-24 13:14:11 -0700122// A unique_ptr type using a custom deleter function.
123template<typename T>
124using deleted_unique_ptr = std::unique_ptr<T, std::function<void(T *)> >;
125
Yifan Hong443df792017-05-09 18:49:45 -0700126static hardware::hidl_vec<hardware::hidl_string> convert(const std::vector<std::string> &v) {
127 hardware::hidl_vec<hardware::hidl_string> hv;
128 hv.resize(v.size());
129 for (size_t i = 0; i < v.size(); ++i) {
130 hv[i].setToExternal(v[i].c_str(), v[i].size());
131 }
132 return hv;
133}
134
Yifan Hong48dc9f82017-05-09 19:33:08 -0700135Status Lshal::emitDebugInfo(
Andreas Huber28d35912017-03-24 13:14:11 -0700136 const std::string &interfaceName,
Yifan Hong443df792017-05-09 18:49:45 -0700137 const std::string &instanceName,
138 const std::vector<std::string> &options,
Yifan Hong48dc9f82017-05-09 19:33:08 -0700139 std::ostream &out,
140 NullableOStream<std::ostream> err) const {
Andreas Huber28d35912017-03-24 13:14:11 -0700141 using android::hidl::base::V1_0::IBase;
142
Yifan Hong9881df92017-05-10 14:33:05 -0700143 hardware::Return<sp<IBase>> retBase = serviceManager()->get(interfaceName, instanceName);
Andreas Huber28d35912017-03-24 13:14:11 -0700144
Yifan Hong48dc9f82017-05-09 19:33:08 -0700145 if (!retBase.isOk()) {
146 std::string msg = "Cannot get " + interfaceName + "/" + instanceName + ": "
147 + retBase.description();
148 err << msg << std::endl;
149 LOG(ERROR) << msg;
150 return TRANSACTION_ERROR;
151 }
152
153 sp<IBase> base = retBase;
154 if (base == nullptr) {
155 std::string msg = interfaceName + "/" + instanceName + " does not exist, or "
156 + "no permission to connect.";
157 err << msg << std::endl;
158 LOG(ERROR) << msg;
159 return NO_INTERFACE;
Andreas Huber28d35912017-03-24 13:14:11 -0700160 }
161
Yifan Hong443df792017-05-09 18:49:45 -0700162 PipeRelay relay(out);
Andreas Huber28d35912017-03-24 13:14:11 -0700163
164 if (relay.initCheck() != OK) {
Yifan Hong48dc9f82017-05-09 19:33:08 -0700165 std::string msg = "PipeRelay::initCheck() FAILED w/ " + std::to_string(relay.initCheck());
166 err << msg << std::endl;
167 LOG(ERROR) << msg;
168 return IO_ERROR;
Andreas Huber28d35912017-03-24 13:14:11 -0700169 }
170
171 deleted_unique_ptr<native_handle_t> fdHandle(
172 native_handle_create(1 /* numFds */, 0 /* numInts */),
173 native_handle_delete);
174
175 fdHandle->data[0] = relay.fd();
176
Yifan Hong443df792017-05-09 18:49:45 -0700177 hardware::Return<void> ret = base->debug(fdHandle.get(), convert(options));
Andreas Huber28d35912017-03-24 13:14:11 -0700178
179 if (!ret.isOk()) {
Yifan Hong48dc9f82017-05-09 19:33:08 -0700180 std::string msg = "debug() FAILED on " + interfaceName + "/" + instanceName + ": "
181 + ret.description();
182 err << msg << std::endl;
183 LOG(ERROR) << msg;
184 return TRANSACTION_ERROR;
Andreas Huber28d35912017-03-24 13:14:11 -0700185 }
Yifan Hong48dc9f82017-05-09 19:33:08 -0700186 return OK;
Andreas Huber28d35912017-03-24 13:14:11 -0700187}
188
Yifan Hong443df792017-05-09 18:49:45 -0700189Status Lshal::parseArgs(const Arg &arg) {
190 static std::set<std::string> sAllCommands{"list", "debug", "help"};
Yifan Hongb0dde932017-02-10 17:49:58 -0800191 optind = 1;
Yifan Hong443df792017-05-09 18:49:45 -0700192 if (optind >= arg.argc) {
193 // no options at all.
194 return OK;
195 }
196 mCommand = arg.argv[optind];
197 if (sAllCommands.find(mCommand) != sAllCommands.end()) {
198 ++optind;
199 return OK; // mCommand is set correctly
Yifan Hongb0dde932017-02-10 17:49:58 -0800200 }
Yifan Hong38d53e02017-02-13 17:51:59 -0800201
Yifan Hong443df792017-05-09 18:49:45 -0700202 if (mCommand.size() > 0 && mCommand[0] == '-') {
203 // first argument is an option, set command to "" (which is recognized as "list")
204 mCommand = "";
205 return OK;
Yifan Hong38d53e02017-02-13 17:51:59 -0800206 }
Yifan Hong443df792017-05-09 18:49:45 -0700207
208 mErr << arg.argv[0] << ": unrecognized option `" << arg.argv[optind] << "`" << std::endl;
209 usage();
210 return USAGE;
Yifan Hongb0dde932017-02-10 17:49:58 -0800211}
212
Yifan Hong9881df92017-05-10 14:33:05 -0700213void signalHandler(int sig) {
214 if (sig == SIGINT) {
215 int retVal;
216 pthread_exit(&retVal);
217 }
218}
219
Yifan Hong443df792017-05-09 18:49:45 -0700220Status Lshal::main(const Arg &arg) {
Yifan Hong9881df92017-05-10 14:33:05 -0700221 // Allow SIGINT to terminate all threads.
222 signal(SIGINT, signalHandler);
223
Yifan Hong443df792017-05-09 18:49:45 -0700224 Status status = parseArgs(arg);
Yifan Hongb0dde932017-02-10 17:49:58 -0800225 if (status != OK) {
226 return status;
227 }
Yifan Hong443df792017-05-09 18:49:45 -0700228 if (mCommand == "help") {
229 usage(optind < arg.argc ? arg.argv[optind] : "");
230 return USAGE;
231 }
232 // Default command is list
233 if (mCommand == "list" || mCommand == "") {
234 return ListCommand{*this}.main(mCommand, arg);
235 }
236 if (mCommand == "debug") {
Yifan Hong48dc9f82017-05-09 19:33:08 -0700237 return DebugCommand{*this}.main(mCommand, arg);
Yifan Hong443df792017-05-09 18:49:45 -0700238 }
239 usage();
240 return USAGE;
241}
242
243NullableOStream<std::ostream> Lshal::err() const {
244 return mErr;
245}
246NullableOStream<std::ostream> Lshal::out() const {
247 return mOut;
Yifan Hongb0dde932017-02-10 17:49:58 -0800248}
249
Yifan Hong9881df92017-05-10 14:33:05 -0700250const sp<IServiceManager> &Lshal::serviceManager() const {
251 return mServiceManager;
252}
253
254const sp<IServiceManager> &Lshal::passthroughManager() const {
255 return mPassthroughManager;
Yifan Honga57dffb2017-02-21 14:59:00 -0800256}
257
Yifan Hongb0dde932017-02-10 17:49:58 -0800258} // namespace lshal
259} // namespace android