blob: bf4ba2c2d674909d84a1b5c9c0dd9d4c8479b9c5 [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 Hong443df792017-05-09 18:49:45 -070036Lshal::Lshal() {
37}
38
39void Lshal::usage(const std::string &command) const {
40 static const std::string helpSummary =
41 "lshal: List and debug HALs.\n"
42 "\n"
43 "commands:\n"
44 " help Print help message\n"
45 " list list HALs\n"
46 " debug debug a specified HAL\n"
47 "\n"
48 "If no command is specified, `list` is the default.\n";
49
50 static const std::string list =
51 "list:\n"
52 " lshal\n"
53 " lshal list\n"
54 " List all hals with default ordering and columns (`lshal list -ipc`)\n"
55 " lshal list [-h|--help]\n"
56 " -h, --help: Print help message for list (`lshal help list`)\n"
57 " lshal [list] [--interface|-i] [--transport|-t] [-r|--arch]\n"
58 " [--pid|-p] [--address|-a] [--clients|-c] [--cmdline|-m]\n"
59 " [--sort={interface|i|pid|p}] [--init-vintf[=<output file>]]\n"
60 " [--debug|-d[=<output file>]]\n"
61 " -i, --interface: print the interface name column\n"
62 " -n, --instance: print the instance name column\n"
63 " -t, --transport: print the transport mode column\n"
64 " -r, --arch: print if the HAL is in 64-bit or 32-bit\n"
65 " -p, --pid: print the server PID, or server cmdline if -m is set\n"
66 " -a, --address: print the server object address column\n"
67 " -c, --clients: print the client PIDs, or client cmdlines if -m is set\n"
68 " -m, --cmdline: print cmdline instead of PIDs\n"
69 " -d[=<output file>], --debug[=<output file>]: emit debug info from \n"
70 " IBase::debug with empty options\n"
71 " --sort=i, --sort=interface: sort by interface name\n"
72 " --sort=p, --sort=pid: sort by server pid\n"
73 " --init-vintf=<output file>: form a skeleton HAL manifest to specified\n"
74 " file, or stdout if no file specified.\n";
75
76 static const std::string debug =
77 "debug:\n"
78 " lshal debug <interface> [options [options [...]]] \n"
79 " Print debug information of a specified interface.\n"
80 " <inteface>: Format is `android.hardware.foo@1.0::IFoo/default`.\n"
81 " If instance name is missing `default` is used.\n"
82 " options: space separated options to IBase::debug.\n";
83
84 static const std::string help =
85 "help:\n"
86 " lshal -h\n"
87 " lshal --help\n"
88 " lshal help\n"
89 " Print this help message\n"
90 " lshal help list\n"
91 " Print help message for list\n"
92 " lshal help debug\n"
93 " Print help message for debug\n";
94
95 if (command == "list") {
96 mErr << list;
97 return;
Yifan Hongb0dde932017-02-10 17:49:58 -080098 }
Yifan Hong443df792017-05-09 18:49:45 -070099 if (command == "debug") {
100 mErr << debug;
101 return;
Yifan Hongb0dde932017-02-10 17:49:58 -0800102 }
103
Yifan Hong443df792017-05-09 18:49:45 -0700104 mErr << helpSummary << "\n" << list << "\n" << debug << "\n" << help;
Yifan Hongb4479022017-03-02 16:54:11 -0800105}
106
Andreas Huber28d35912017-03-24 13:14:11 -0700107// A unique_ptr type using a custom deleter function.
108template<typename T>
109using deleted_unique_ptr = std::unique_ptr<T, std::function<void(T *)> >;
110
Yifan Hong443df792017-05-09 18:49:45 -0700111static hardware::hidl_vec<hardware::hidl_string> convert(const std::vector<std::string> &v) {
112 hardware::hidl_vec<hardware::hidl_string> hv;
113 hv.resize(v.size());
114 for (size_t i = 0; i < v.size(); ++i) {
115 hv[i].setToExternal(v[i].c_str(), v[i].size());
116 }
117 return hv;
118}
119
Yifan Hong48dc9f82017-05-09 19:33:08 -0700120Status Lshal::emitDebugInfo(
Andreas Huber28d35912017-03-24 13:14:11 -0700121 const std::string &interfaceName,
Yifan Hong443df792017-05-09 18:49:45 -0700122 const std::string &instanceName,
123 const std::vector<std::string> &options,
Yifan Hong48dc9f82017-05-09 19:33:08 -0700124 std::ostream &out,
125 NullableOStream<std::ostream> err) const {
Andreas Huber28d35912017-03-24 13:14:11 -0700126 using android::hidl::base::V1_0::IBase;
127
128 hardware::Return<sp<IBase>> retBase =
Yifan Hong48dc9f82017-05-09 19:33:08 -0700129 ::android::hardware::defaultServiceManager()->get(interfaceName, instanceName);
Andreas Huber28d35912017-03-24 13:14:11 -0700130
Yifan Hong48dc9f82017-05-09 19:33:08 -0700131 if (!retBase.isOk()) {
132 std::string msg = "Cannot get " + interfaceName + "/" + instanceName + ": "
133 + retBase.description();
134 err << msg << std::endl;
135 LOG(ERROR) << msg;
136 return TRANSACTION_ERROR;
137 }
138
139 sp<IBase> base = retBase;
140 if (base == nullptr) {
141 std::string msg = interfaceName + "/" + instanceName + " does not exist, or "
142 + "no permission to connect.";
143 err << msg << std::endl;
144 LOG(ERROR) << msg;
145 return NO_INTERFACE;
Andreas Huber28d35912017-03-24 13:14:11 -0700146 }
147
Yifan Hong443df792017-05-09 18:49:45 -0700148 PipeRelay relay(out);
Andreas Huber28d35912017-03-24 13:14:11 -0700149
150 if (relay.initCheck() != OK) {
Yifan Hong48dc9f82017-05-09 19:33:08 -0700151 std::string msg = "PipeRelay::initCheck() FAILED w/ " + std::to_string(relay.initCheck());
152 err << msg << std::endl;
153 LOG(ERROR) << msg;
154 return IO_ERROR;
Andreas Huber28d35912017-03-24 13:14:11 -0700155 }
156
157 deleted_unique_ptr<native_handle_t> fdHandle(
158 native_handle_create(1 /* numFds */, 0 /* numInts */),
159 native_handle_delete);
160
161 fdHandle->data[0] = relay.fd();
162
Yifan Hong443df792017-05-09 18:49:45 -0700163 hardware::Return<void> ret = base->debug(fdHandle.get(), convert(options));
Andreas Huber28d35912017-03-24 13:14:11 -0700164
165 if (!ret.isOk()) {
Yifan Hong48dc9f82017-05-09 19:33:08 -0700166 std::string msg = "debug() FAILED on " + interfaceName + "/" + instanceName + ": "
167 + ret.description();
168 err << msg << std::endl;
169 LOG(ERROR) << msg;
170 return TRANSACTION_ERROR;
Andreas Huber28d35912017-03-24 13:14:11 -0700171 }
Yifan Hong48dc9f82017-05-09 19:33:08 -0700172 return OK;
Andreas Huber28d35912017-03-24 13:14:11 -0700173}
174
Yifan Hong443df792017-05-09 18:49:45 -0700175Status Lshal::parseArgs(const Arg &arg) {
176 static std::set<std::string> sAllCommands{"list", "debug", "help"};
Yifan Hongb0dde932017-02-10 17:49:58 -0800177 optind = 1;
Yifan Hong443df792017-05-09 18:49:45 -0700178 if (optind >= arg.argc) {
179 // no options at all.
180 return OK;
181 }
182 mCommand = arg.argv[optind];
183 if (sAllCommands.find(mCommand) != sAllCommands.end()) {
184 ++optind;
185 return OK; // mCommand is set correctly
Yifan Hongb0dde932017-02-10 17:49:58 -0800186 }
Yifan Hong38d53e02017-02-13 17:51:59 -0800187
Yifan Hong443df792017-05-09 18:49:45 -0700188 if (mCommand.size() > 0 && mCommand[0] == '-') {
189 // first argument is an option, set command to "" (which is recognized as "list")
190 mCommand = "";
191 return OK;
Yifan Hong38d53e02017-02-13 17:51:59 -0800192 }
Yifan Hong443df792017-05-09 18:49:45 -0700193
194 mErr << arg.argv[0] << ": unrecognized option `" << arg.argv[optind] << "`" << std::endl;
195 usage();
196 return USAGE;
Yifan Hongb0dde932017-02-10 17:49:58 -0800197}
198
Yifan Hong443df792017-05-09 18:49:45 -0700199Status Lshal::main(const Arg &arg) {
200 Status status = parseArgs(arg);
Yifan Hongb0dde932017-02-10 17:49:58 -0800201 if (status != OK) {
202 return status;
203 }
Yifan Hong443df792017-05-09 18:49:45 -0700204 if (mCommand == "help") {
205 usage(optind < arg.argc ? arg.argv[optind] : "");
206 return USAGE;
207 }
208 // Default command is list
209 if (mCommand == "list" || mCommand == "") {
210 return ListCommand{*this}.main(mCommand, arg);
211 }
212 if (mCommand == "debug") {
Yifan Hong48dc9f82017-05-09 19:33:08 -0700213 return DebugCommand{*this}.main(mCommand, arg);
Yifan Hong443df792017-05-09 18:49:45 -0700214 }
215 usage();
216 return USAGE;
217}
218
219NullableOStream<std::ostream> Lshal::err() const {
220 return mErr;
221}
222NullableOStream<std::ostream> Lshal::out() const {
223 return mOut;
Yifan Hongb0dde932017-02-10 17:49:58 -0800224}
225
Yifan Honga57dffb2017-02-21 14:59:00 -0800226void signalHandler(int sig) {
227 if (sig == SIGINT) {
228 int retVal;
229 pthread_exit(&retVal);
230 }
231}
232
Yifan Hongb0dde932017-02-10 17:49:58 -0800233} // namespace lshal
234} // namespace android
235
236int main(int argc, char **argv) {
Yifan Hong443df792017-05-09 18:49:45 -0700237 using namespace ::android::lshal;
238 signal(SIGINT, signalHandler);
239 return Lshal{}.main(Arg{argc, argv});
Yifan Hongb0dde932017-02-10 17:49:58 -0800240}