blob: 43108b7925adc9789b2b8ae04bd84c737ea7a3f6 [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
17#include "Lshal.h"
18
Yifan Hong443df792017-05-09 18:49:45 -070019#include <set>
20#include <string>
Yifan Hongb0dde932017-02-10 17:49:58 -080021
Yifan Hong443df792017-05-09 18:49:45 -070022#include "ListCommand.h"
Andreas Huber28d35912017-03-24 13:14:11 -070023#include "PipeRelay.h"
Yifan Hongb0dde932017-02-10 17:49:58 -080024
25namespace android {
26namespace lshal {
27
Yifan Hong443df792017-05-09 18:49:45 -070028using ::android::hidl::manager::V1_0::IServiceManager;
Yifan Hongb0dde932017-02-10 17:49:58 -080029
Yifan Hong443df792017-05-09 18:49:45 -070030Lshal::Lshal() {
31}
32
33void Lshal::usage(const std::string &command) const {
34 static const std::string helpSummary =
35 "lshal: List and debug HALs.\n"
36 "\n"
37 "commands:\n"
38 " help Print help message\n"
39 " list list HALs\n"
40 " debug debug a specified HAL\n"
41 "\n"
42 "If no command is specified, `list` is the default.\n";
43
44 static const std::string list =
45 "list:\n"
46 " lshal\n"
47 " lshal list\n"
48 " List all hals with default ordering and columns (`lshal list -ipc`)\n"
49 " lshal list [-h|--help]\n"
50 " -h, --help: Print help message for list (`lshal help list`)\n"
51 " lshal [list] [--interface|-i] [--transport|-t] [-r|--arch]\n"
52 " [--pid|-p] [--address|-a] [--clients|-c] [--cmdline|-m]\n"
53 " [--sort={interface|i|pid|p}] [--init-vintf[=<output file>]]\n"
54 " [--debug|-d[=<output file>]]\n"
55 " -i, --interface: print the interface name column\n"
56 " -n, --instance: print the instance name column\n"
57 " -t, --transport: print the transport mode column\n"
58 " -r, --arch: print if the HAL is in 64-bit or 32-bit\n"
59 " -p, --pid: print the server PID, or server cmdline if -m is set\n"
60 " -a, --address: print the server object address column\n"
61 " -c, --clients: print the client PIDs, or client cmdlines if -m is set\n"
62 " -m, --cmdline: print cmdline instead of PIDs\n"
63 " -d[=<output file>], --debug[=<output file>]: emit debug info from \n"
64 " IBase::debug with empty options\n"
65 " --sort=i, --sort=interface: sort by interface name\n"
66 " --sort=p, --sort=pid: sort by server pid\n"
67 " --init-vintf=<output file>: form a skeleton HAL manifest to specified\n"
68 " file, or stdout if no file specified.\n";
69
70 static const std::string debug =
71 "debug:\n"
72 " lshal debug <interface> [options [options [...]]] \n"
73 " Print debug information of a specified interface.\n"
74 " <inteface>: Format is `android.hardware.foo@1.0::IFoo/default`.\n"
75 " If instance name is missing `default` is used.\n"
76 " options: space separated options to IBase::debug.\n";
77
78 static const std::string help =
79 "help:\n"
80 " lshal -h\n"
81 " lshal --help\n"
82 " lshal help\n"
83 " Print this help message\n"
84 " lshal help list\n"
85 " Print help message for list\n"
86 " lshal help debug\n"
87 " Print help message for debug\n";
88
89 if (command == "list") {
90 mErr << list;
91 return;
Yifan Hongb0dde932017-02-10 17:49:58 -080092 }
Yifan Hong443df792017-05-09 18:49:45 -070093 if (command == "debug") {
94 mErr << debug;
95 return;
Yifan Hongb0dde932017-02-10 17:49:58 -080096 }
97
Yifan Hong443df792017-05-09 18:49:45 -070098 mErr << helpSummary << "\n" << list << "\n" << debug << "\n" << help;
Yifan Hongb4479022017-03-02 16:54:11 -080099}
100
Andreas Huber28d35912017-03-24 13:14:11 -0700101// A unique_ptr type using a custom deleter function.
102template<typename T>
103using deleted_unique_ptr = std::unique_ptr<T, std::function<void(T *)> >;
104
Yifan Hong443df792017-05-09 18:49:45 -0700105static hardware::hidl_vec<hardware::hidl_string> convert(const std::vector<std::string> &v) {
106 hardware::hidl_vec<hardware::hidl_string> hv;
107 hv.resize(v.size());
108 for (size_t i = 0; i < v.size(); ++i) {
109 hv[i].setToExternal(v[i].c_str(), v[i].size());
110 }
111 return hv;
112}
113
114// static
Andreas Huber28d35912017-03-24 13:14:11 -0700115void Lshal::emitDebugInfo(
116 const sp<IServiceManager> &serviceManager,
117 const std::string &interfaceName,
Yifan Hong443df792017-05-09 18:49:45 -0700118 const std::string &instanceName,
119 const std::vector<std::string> &options,
120 std::ostream &out) {
Andreas Huber28d35912017-03-24 13:14:11 -0700121 using android::hidl::base::V1_0::IBase;
122
123 hardware::Return<sp<IBase>> retBase =
124 serviceManager->get(interfaceName, instanceName);
125
126 sp<IBase> base;
127 if (!retBase.isOk() || (base = retBase) == nullptr) {
Yifan Hong443df792017-05-09 18:49:45 -0700128 mErr << interfaceName << "/" << instanceName << " does not exist." << std::endl;
Andreas Huber28d35912017-03-24 13:14:11 -0700129 return;
130 }
131
Yifan Hong443df792017-05-09 18:49:45 -0700132 PipeRelay relay(out);
Andreas Huber28d35912017-03-24 13:14:11 -0700133
134 if (relay.initCheck() != OK) {
Yifan Hong443df792017-05-09 18:49:45 -0700135 mErr << "PipeRelay::initCheck() FAILED w/ " << relay.initCheck() << std::endl;
Andreas Huber28d35912017-03-24 13:14:11 -0700136 return;
137 }
138
139 deleted_unique_ptr<native_handle_t> fdHandle(
140 native_handle_create(1 /* numFds */, 0 /* numInts */),
141 native_handle_delete);
142
143 fdHandle->data[0] = relay.fd();
144
Yifan Hong443df792017-05-09 18:49:45 -0700145 hardware::Return<void> ret = base->debug(fdHandle.get(), convert(options));
Andreas Huber28d35912017-03-24 13:14:11 -0700146
147 if (!ret.isOk()) {
148 LOG(ERROR)
149 << interfaceName
150 << "::debug(...) FAILED. (instance "
151 << instanceName
152 << ")";
153 }
154}
155
Yifan Hong443df792017-05-09 18:49:45 -0700156Status Lshal::parseArgs(const Arg &arg) {
157 static std::set<std::string> sAllCommands{"list", "debug", "help"};
Yifan Hongb0dde932017-02-10 17:49:58 -0800158 optind = 1;
Yifan Hong443df792017-05-09 18:49:45 -0700159 if (optind >= arg.argc) {
160 // no options at all.
161 return OK;
162 }
163 mCommand = arg.argv[optind];
164 if (sAllCommands.find(mCommand) != sAllCommands.end()) {
165 ++optind;
166 return OK; // mCommand is set correctly
Yifan Hongb0dde932017-02-10 17:49:58 -0800167 }
Yifan Hong38d53e02017-02-13 17:51:59 -0800168
Yifan Hong443df792017-05-09 18:49:45 -0700169 if (mCommand.size() > 0 && mCommand[0] == '-') {
170 // first argument is an option, set command to "" (which is recognized as "list")
171 mCommand = "";
172 return OK;
Yifan Hong38d53e02017-02-13 17:51:59 -0800173 }
Yifan Hong443df792017-05-09 18:49:45 -0700174
175 mErr << arg.argv[0] << ": unrecognized option `" << arg.argv[optind] << "`" << std::endl;
176 usage();
177 return USAGE;
Yifan Hongb0dde932017-02-10 17:49:58 -0800178}
179
Yifan Hong443df792017-05-09 18:49:45 -0700180Status Lshal::main(const Arg &arg) {
181 Status status = parseArgs(arg);
Yifan Hongb0dde932017-02-10 17:49:58 -0800182 if (status != OK) {
183 return status;
184 }
Yifan Hong443df792017-05-09 18:49:45 -0700185 if (mCommand == "help") {
186 usage(optind < arg.argc ? arg.argv[optind] : "");
187 return USAGE;
188 }
189 // Default command is list
190 if (mCommand == "list" || mCommand == "") {
191 return ListCommand{*this}.main(mCommand, arg);
192 }
193 if (mCommand == "debug") {
194 // TODO(b/37725279) implement this
195 return OK;
196 }
197 usage();
198 return USAGE;
199}
200
201NullableOStream<std::ostream> Lshal::err() const {
202 return mErr;
203}
204NullableOStream<std::ostream> Lshal::out() const {
205 return mOut;
Yifan Hongb0dde932017-02-10 17:49:58 -0800206}
207
Yifan Honga57dffb2017-02-21 14:59:00 -0800208void signalHandler(int sig) {
209 if (sig == SIGINT) {
210 int retVal;
211 pthread_exit(&retVal);
212 }
213}
214
Yifan Hongb0dde932017-02-10 17:49:58 -0800215} // namespace lshal
216} // namespace android
217
218int main(int argc, char **argv) {
Yifan Hong443df792017-05-09 18:49:45 -0700219 using namespace ::android::lshal;
220 signal(SIGINT, signalHandler);
221 return Lshal{}.main(Arg{argc, argv});
Yifan Hongb0dde932017-02-10 17:49:58 -0800222}