blob: e2d5f6df0afa1f6b2c2f9bacfcbbc5f4ff41323c [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"
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"
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"
87 " --init-vintf=<output file>: form a skeleton HAL manifest to specified\n"
88 " file, or stdout if no file specified.\n";
89
90 static const std::string debug =
91 "debug:\n"
92 " lshal debug <interface> [options [options [...]]] \n"
93 " Print debug information of a specified interface.\n"
94 " <inteface>: Format is `android.hardware.foo@1.0::IFoo/default`.\n"
95 " If instance name is missing `default` is used.\n"
96 " options: space separated options to IBase::debug.\n";
97
98 static const std::string help =
99 "help:\n"
100 " lshal -h\n"
101 " lshal --help\n"
102 " lshal help\n"
103 " Print this help message\n"
104 " lshal help list\n"
105 " Print help message for list\n"
106 " lshal help debug\n"
107 " Print help message for debug\n";
108
109 if (command == "list") {
110 mErr << list;
111 return;
Yifan Hongb0dde932017-02-10 17:49:58 -0800112 }
Yifan Hong443df792017-05-09 18:49:45 -0700113 if (command == "debug") {
114 mErr << debug;
115 return;
Yifan Hongb0dde932017-02-10 17:49:58 -0800116 }
117
Yifan Hong443df792017-05-09 18:49:45 -0700118 mErr << helpSummary << "\n" << list << "\n" << debug << "\n" << help;
Yifan Hongb4479022017-03-02 16:54:11 -0800119}
120
Andreas Huber28d35912017-03-24 13:14:11 -0700121// A unique_ptr type using a custom deleter function.
122template<typename T>
123using deleted_unique_ptr = std::unique_ptr<T, std::function<void(T *)> >;
124
Yifan Hong443df792017-05-09 18:49:45 -0700125static hardware::hidl_vec<hardware::hidl_string> convert(const std::vector<std::string> &v) {
126 hardware::hidl_vec<hardware::hidl_string> hv;
127 hv.resize(v.size());
128 for (size_t i = 0; i < v.size(); ++i) {
129 hv[i].setToExternal(v[i].c_str(), v[i].size());
130 }
131 return hv;
132}
133
Yifan Hong48dc9f82017-05-09 19:33:08 -0700134Status Lshal::emitDebugInfo(
Andreas Huber28d35912017-03-24 13:14:11 -0700135 const std::string &interfaceName,
Yifan Hong443df792017-05-09 18:49:45 -0700136 const std::string &instanceName,
137 const std::vector<std::string> &options,
Yifan Hong48dc9f82017-05-09 19:33:08 -0700138 std::ostream &out,
139 NullableOStream<std::ostream> err) const {
Andreas Huber28d35912017-03-24 13:14:11 -0700140 using android::hidl::base::V1_0::IBase;
141
Yifan Hong9881df92017-05-10 14:33:05 -0700142 hardware::Return<sp<IBase>> retBase = serviceManager()->get(interfaceName, instanceName);
Andreas Huber28d35912017-03-24 13:14:11 -0700143
Yifan Hong48dc9f82017-05-09 19:33:08 -0700144 if (!retBase.isOk()) {
145 std::string msg = "Cannot get " + interfaceName + "/" + instanceName + ": "
146 + retBase.description();
147 err << msg << std::endl;
148 LOG(ERROR) << msg;
149 return TRANSACTION_ERROR;
150 }
151
152 sp<IBase> base = retBase;
153 if (base == nullptr) {
154 std::string msg = interfaceName + "/" + instanceName + " does not exist, or "
155 + "no permission to connect.";
156 err << msg << std::endl;
157 LOG(ERROR) << msg;
158 return NO_INTERFACE;
Andreas Huber28d35912017-03-24 13:14:11 -0700159 }
160
Yifan Hong443df792017-05-09 18:49:45 -0700161 PipeRelay relay(out);
Andreas Huber28d35912017-03-24 13:14:11 -0700162
163 if (relay.initCheck() != OK) {
Yifan Hong48dc9f82017-05-09 19:33:08 -0700164 std::string msg = "PipeRelay::initCheck() FAILED w/ " + std::to_string(relay.initCheck());
165 err << msg << std::endl;
166 LOG(ERROR) << msg;
167 return IO_ERROR;
Andreas Huber28d35912017-03-24 13:14:11 -0700168 }
169
170 deleted_unique_ptr<native_handle_t> fdHandle(
171 native_handle_create(1 /* numFds */, 0 /* numInts */),
172 native_handle_delete);
173
174 fdHandle->data[0] = relay.fd();
175
Yifan Hong443df792017-05-09 18:49:45 -0700176 hardware::Return<void> ret = base->debug(fdHandle.get(), convert(options));
Andreas Huber28d35912017-03-24 13:14:11 -0700177
178 if (!ret.isOk()) {
Yifan Hong48dc9f82017-05-09 19:33:08 -0700179 std::string msg = "debug() FAILED on " + interfaceName + "/" + instanceName + ": "
180 + ret.description();
181 err << msg << std::endl;
182 LOG(ERROR) << msg;
183 return TRANSACTION_ERROR;
Andreas Huber28d35912017-03-24 13:14:11 -0700184 }
Yifan Hong48dc9f82017-05-09 19:33:08 -0700185 return OK;
Andreas Huber28d35912017-03-24 13:14:11 -0700186}
187
Yifan Hong443df792017-05-09 18:49:45 -0700188Status Lshal::parseArgs(const Arg &arg) {
189 static std::set<std::string> sAllCommands{"list", "debug", "help"};
Yifan Hongb0dde932017-02-10 17:49:58 -0800190 optind = 1;
Yifan Hong443df792017-05-09 18:49:45 -0700191 if (optind >= arg.argc) {
192 // no options at all.
193 return OK;
194 }
195 mCommand = arg.argv[optind];
196 if (sAllCommands.find(mCommand) != sAllCommands.end()) {
197 ++optind;
198 return OK; // mCommand is set correctly
Yifan Hongb0dde932017-02-10 17:49:58 -0800199 }
Yifan Hong38d53e02017-02-13 17:51:59 -0800200
Yifan Hong443df792017-05-09 18:49:45 -0700201 if (mCommand.size() > 0 && mCommand[0] == '-') {
202 // first argument is an option, set command to "" (which is recognized as "list")
203 mCommand = "";
204 return OK;
Yifan Hong38d53e02017-02-13 17:51:59 -0800205 }
Yifan Hong443df792017-05-09 18:49:45 -0700206
207 mErr << arg.argv[0] << ": unrecognized option `" << arg.argv[optind] << "`" << std::endl;
208 usage();
209 return USAGE;
Yifan Hongb0dde932017-02-10 17:49:58 -0800210}
211
Yifan Hong9881df92017-05-10 14:33:05 -0700212void signalHandler(int sig) {
213 if (sig == SIGINT) {
214 int retVal;
215 pthread_exit(&retVal);
216 }
217}
218
Yifan Hong443df792017-05-09 18:49:45 -0700219Status Lshal::main(const Arg &arg) {
Yifan Hong9881df92017-05-10 14:33:05 -0700220 // Allow SIGINT to terminate all threads.
221 signal(SIGINT, signalHandler);
222
Yifan Hong443df792017-05-09 18:49:45 -0700223 Status status = parseArgs(arg);
Yifan Hongb0dde932017-02-10 17:49:58 -0800224 if (status != OK) {
225 return status;
226 }
Yifan Hong443df792017-05-09 18:49:45 -0700227 if (mCommand == "help") {
228 usage(optind < arg.argc ? arg.argv[optind] : "");
229 return USAGE;
230 }
231 // Default command is list
232 if (mCommand == "list" || mCommand == "") {
233 return ListCommand{*this}.main(mCommand, arg);
234 }
235 if (mCommand == "debug") {
Yifan Hong48dc9f82017-05-09 19:33:08 -0700236 return DebugCommand{*this}.main(mCommand, arg);
Yifan Hong443df792017-05-09 18:49:45 -0700237 }
238 usage();
239 return USAGE;
240}
241
242NullableOStream<std::ostream> Lshal::err() const {
243 return mErr;
244}
245NullableOStream<std::ostream> Lshal::out() const {
246 return mOut;
Yifan Hongb0dde932017-02-10 17:49:58 -0800247}
248
Yifan Hong9881df92017-05-10 14:33:05 -0700249const sp<IServiceManager> &Lshal::serviceManager() const {
250 return mServiceManager;
251}
252
253const sp<IServiceManager> &Lshal::passthroughManager() const {
254 return mPassthroughManager;
Yifan Honga57dffb2017-02-21 14:59:00 -0800255}
256
Yifan Hongb0dde932017-02-10 17:49:58 -0800257} // namespace lshal
258} // namespace android