blob: 9db42f10ca59876eca9ed72dd71106a33df53eb1 [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"
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;
Yifan Hongb0dde932017-02-10 17:49:58 -0800110 }
Yifan Hong443df792017-05-09 18:49:45 -0700111 if (command == "debug") {
112 mErr << debug;
113 return;
Yifan Hongb0dde932017-02-10 17:49:58 -0800114 }
115
Yifan Hong443df792017-05-09 18:49:45 -0700116 mErr << helpSummary << "\n" << list << "\n" << debug << "\n" << help;
Yifan Hongb4479022017-03-02 16:54:11 -0800117}
118
Andreas Huber28d35912017-03-24 13:14:11 -0700119// A unique_ptr type using a custom deleter function.
120template<typename T>
121using deleted_unique_ptr = std::unique_ptr<T, std::function<void(T *)> >;
122
Yifan Hong443df792017-05-09 18:49:45 -0700123static 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
Yifan Hong48dc9f82017-05-09 19:33:08 -0700132Status Lshal::emitDebugInfo(
Andreas Huber28d35912017-03-24 13:14:11 -0700133 const std::string &interfaceName,
Yifan Hong443df792017-05-09 18:49:45 -0700134 const std::string &instanceName,
135 const std::vector<std::string> &options,
Yifan Hong48dc9f82017-05-09 19:33:08 -0700136 std::ostream &out,
137 NullableOStream<std::ostream> err) const {
Andreas Huber28d35912017-03-24 13:14:11 -0700138 using android::hidl::base::V1_0::IBase;
139
Yifan Hong9881df92017-05-10 14:33:05 -0700140 hardware::Return<sp<IBase>> retBase = serviceManager()->get(interfaceName, instanceName);
Andreas Huber28d35912017-03-24 13:14:11 -0700141
Yifan Hong48dc9f82017-05-09 19:33:08 -0700142 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;
Andreas Huber28d35912017-03-24 13:14:11 -0700157 }
158
Yifan Hong443df792017-05-09 18:49:45 -0700159 PipeRelay relay(out);
Andreas Huber28d35912017-03-24 13:14:11 -0700160
161 if (relay.initCheck() != OK) {
Yifan Hong48dc9f82017-05-09 19:33:08 -0700162 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;
Andreas Huber28d35912017-03-24 13:14:11 -0700166 }
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
Yifan Hong443df792017-05-09 18:49:45 -0700174 hardware::Return<void> ret = base->debug(fdHandle.get(), convert(options));
Andreas Huber28d35912017-03-24 13:14:11 -0700175
176 if (!ret.isOk()) {
Yifan Hong48dc9f82017-05-09 19:33:08 -0700177 std::string msg = "debug() FAILED on " + interfaceName + "/" + instanceName + ": "
178 + ret.description();
179 err << msg << std::endl;
180 LOG(ERROR) << msg;
181 return TRANSACTION_ERROR;
Andreas Huber28d35912017-03-24 13:14:11 -0700182 }
Yifan Hong48dc9f82017-05-09 19:33:08 -0700183 return OK;
Andreas Huber28d35912017-03-24 13:14:11 -0700184}
185
Yifan Hong443df792017-05-09 18:49:45 -0700186Status Lshal::parseArgs(const Arg &arg) {
187 static std::set<std::string> sAllCommands{"list", "debug", "help"};
Yifan Hongb0dde932017-02-10 17:49:58 -0800188 optind = 1;
Yifan Hong443df792017-05-09 18:49:45 -0700189 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
Yifan Hongb0dde932017-02-10 17:49:58 -0800197 }
Yifan Hong38d53e02017-02-13 17:51:59 -0800198
Yifan Hong443df792017-05-09 18:49:45 -0700199 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;
Yifan Hong38d53e02017-02-13 17:51:59 -0800203 }
Yifan Hong443df792017-05-09 18:49:45 -0700204
205 mErr << arg.argv[0] << ": unrecognized option `" << arg.argv[optind] << "`" << std::endl;
206 usage();
207 return USAGE;
Yifan Hongb0dde932017-02-10 17:49:58 -0800208}
209
Yifan Hong9881df92017-05-10 14:33:05 -0700210void signalHandler(int sig) {
211 if (sig == SIGINT) {
212 int retVal;
213 pthread_exit(&retVal);
214 }
215}
216
Yifan Hong443df792017-05-09 18:49:45 -0700217Status Lshal::main(const Arg &arg) {
Yifan Hong9881df92017-05-10 14:33:05 -0700218 // Allow SIGINT to terminate all threads.
219 signal(SIGINT, signalHandler);
220
Yifan Hong443df792017-05-09 18:49:45 -0700221 Status status = parseArgs(arg);
Yifan Hongb0dde932017-02-10 17:49:58 -0800222 if (status != OK) {
223 return status;
224 }
Yifan Hong443df792017-05-09 18:49:45 -0700225 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") {
Yifan Hong48dc9f82017-05-09 19:33:08 -0700234 return DebugCommand{*this}.main(mCommand, arg);
Yifan Hong443df792017-05-09 18:49:45 -0700235 }
236 usage();
237 return USAGE;
238}
239
240NullableOStream<std::ostream> Lshal::err() const {
241 return mErr;
242}
243NullableOStream<std::ostream> Lshal::out() const {
244 return mOut;
Yifan Hongb0dde932017-02-10 17:49:58 -0800245}
246
Yifan Hong9881df92017-05-10 14:33:05 -0700247const sp<IServiceManager> &Lshal::serviceManager() const {
248 return mServiceManager;
249}
250
251const sp<IServiceManager> &Lshal::passthroughManager() const {
252 return mPassthroughManager;
Yifan Honga57dffb2017-02-21 14:59:00 -0800253}
254
Yifan Hongb0dde932017-02-10 17:49:58 -0800255} // namespace lshal
256} // namespace android