blob: 5cdcb23d1378f3fb5a6d7807f39eec8e86a9c309 [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>
Steven Moreland5f328892018-01-18 14:38:07 -080026#include <hidl/HidlTransportUtils.h>
Yifan Hong48dc9f82017-05-09 19:33:08 -070027
28#include "DebugCommand.h"
Steven Moreland552e4072019-06-25 09:33:32 -070029#include "HelpCommand.h"
Yifan Hong443df792017-05-09 18:49:45 -070030#include "ListCommand.h"
Steven Moreland552e4072019-06-25 09:33:32 -070031#include "WaitCommand.h"
Andreas Huber28d35912017-03-24 13:14:11 -070032#include "PipeRelay.h"
Yifan Hongb0dde932017-02-10 17:49:58 -080033
34namespace android {
35namespace lshal {
36
Yifan Hong443df792017-05-09 18:49:45 -070037using ::android::hidl::manager::V1_0::IServiceManager;
Yifan Hongb0dde932017-02-10 17:49:58 -080038
Yifan Hong9881df92017-05-10 14:33:05 -070039Lshal::Lshal()
Yifan Hong795b6ec2017-09-13 11:25:28 -070040 : Lshal(std::cout, std::cerr, ::android::hardware::defaultServiceManager(),
41 ::android::hardware::getPassthroughServiceManager()) {
Yifan Hong9881df92017-05-10 14:33:05 -070042}
43
44Lshal::Lshal(std::ostream &out, std::ostream &err,
45 sp<hidl::manager::V1_0::IServiceManager> serviceManager,
46 sp<hidl::manager::V1_0::IServiceManager> passthroughManager)
47 : mOut(out), mErr(err),
48 mServiceManager(serviceManager),
49 mPassthroughManager(passthroughManager) {
50
Yifan Hong795b6ec2017-09-13 11:25:28 -070051 mRegisteredCommands.push_back({std::make_unique<ListCommand>(*this)});
52 mRegisteredCommands.push_back({std::make_unique<DebugCommand>(*this)});
53 mRegisteredCommands.push_back({std::make_unique<HelpCommand>(*this)});
Steven Moreland552e4072019-06-25 09:33:32 -070054 mRegisteredCommands.push_back({std::make_unique<WaitCommand>(*this)});
Yifan Hong795b6ec2017-09-13 11:25:28 -070055}
56
57void Lshal::forEachCommand(const std::function<void(const Command* c)>& f) const {
58 for (const auto& e : mRegisteredCommands) f(e.get());
Yifan Hong443df792017-05-09 18:49:45 -070059}
60
Yifan Honga8bedc62017-09-08 18:00:31 -070061void Lshal::usage() {
Steven Morelanddbbbc652021-02-02 23:02:38 +000062 err() << "lshal: List and debug HIDL HALs." << std::endl
63 << " (for AIDL HALs, see `dumpsys`)" << std::endl << std::endl
Yifan Hong795b6ec2017-09-13 11:25:28 -070064 << "commands:" << std::endl;
Yifan Hong443df792017-05-09 18:49:45 -070065
Yifan Hong795b6ec2017-09-13 11:25:28 -070066 size_t nameMaxLength = 0;
67 forEachCommand([&](const Command* e) {
68 nameMaxLength = std::max(nameMaxLength, e->getName().length());
69 });
70 bool first = true;
71 forEachCommand([&](const Command* e) {
72 if (!first) err() << std::endl;
73 first = false;
74 err() << " " << std::left << std::setw(nameMaxLength + 8) << e->getName()
75 << e->getSimpleDescription();
76 });
77 err() << std::endl << "If no command is specified, `" << ListCommand::GetName()
78 << "` is the default." << std::endl << std::endl;
79
80 first = true;
81 forEachCommand([&](const Command* e) {
82 if (!first) err() << std::endl;
83 first = false;
84 e->usage();
85 });
Yifan Hongb4479022017-03-02 16:54:11 -080086}
87
Andreas Huber28d35912017-03-24 13:14:11 -070088// A unique_ptr type using a custom deleter function.
89template<typename T>
90using deleted_unique_ptr = std::unique_ptr<T, std::function<void(T *)> >;
91
Yifan Hong443df792017-05-09 18:49:45 -070092static hardware::hidl_vec<hardware::hidl_string> convert(const std::vector<std::string> &v) {
93 hardware::hidl_vec<hardware::hidl_string> hv;
94 hv.resize(v.size());
95 for (size_t i = 0; i < v.size(); ++i) {
96 hv[i].setToExternal(v[i].c_str(), v[i].size());
97 }
98 return hv;
99}
100
Yifan Hong48dc9f82017-05-09 19:33:08 -0700101Status Lshal::emitDebugInfo(
Andreas Huber28d35912017-03-24 13:14:11 -0700102 const std::string &interfaceName,
Yifan Hong443df792017-05-09 18:49:45 -0700103 const std::string &instanceName,
104 const std::vector<std::string> &options,
Yifan Hong6884b872020-07-09 16:38:18 -0700105 ParentDebugInfoLevel parentDebugInfoLevel,
Yifan Hong48dc9f82017-05-09 19:33:08 -0700106 std::ostream &out,
107 NullableOStream<std::ostream> err) const {
Andreas Huber28d35912017-03-24 13:14:11 -0700108 using android::hidl::base::V1_0::IBase;
Steven Moreland5f328892018-01-18 14:38:07 -0800109 using android::hardware::details::getDescriptor;
Andreas Huber28d35912017-03-24 13:14:11 -0700110
Yifan Hong9881df92017-05-10 14:33:05 -0700111 hardware::Return<sp<IBase>> retBase = serviceManager()->get(interfaceName, instanceName);
Andreas Huber28d35912017-03-24 13:14:11 -0700112
Yifan Hong48dc9f82017-05-09 19:33:08 -0700113 if (!retBase.isOk()) {
114 std::string msg = "Cannot get " + interfaceName + "/" + instanceName + ": "
115 + retBase.description();
116 err << msg << std::endl;
117 LOG(ERROR) << msg;
118 return TRANSACTION_ERROR;
119 }
120
121 sp<IBase> base = retBase;
122 if (base == nullptr) {
123 std::string msg = interfaceName + "/" + instanceName + " does not exist, or "
124 + "no permission to connect.";
125 err << msg << std::endl;
126 LOG(ERROR) << msg;
127 return NO_INTERFACE;
Andreas Huber28d35912017-03-24 13:14:11 -0700128 }
129
Yifan Hong6884b872020-07-09 16:38:18 -0700130 if (parentDebugInfoLevel != ParentDebugInfoLevel::FULL) {
Steven Moreland5f328892018-01-18 14:38:07 -0800131 const std::string descriptor = getDescriptor(base.get());
132 if (descriptor.empty()) {
133 std::string msg = interfaceName + "/" + instanceName + " getDescriptor failed";
134 err << msg << std::endl;
135 LOG(ERROR) << msg;
136 }
137 if (descriptor != interfaceName) {
Yifan Hong6884b872020-07-09 16:38:18 -0700138 if (parentDebugInfoLevel == ParentDebugInfoLevel::FQNAME_ONLY) {
139 out << "[See " << descriptor << "/" << instanceName << "]";
140 }
Steven Moreland5f328892018-01-18 14:38:07 -0800141 return OK;
142 }
143 }
144
Yifan Hong891989f2021-06-17 18:31:42 -0700145 auto relay = PipeRelay::create(out, err, interfaceName + "/" + instanceName);
146 if (!relay.ok()) {
147 err << "Unable to create PipeRelay: " << relay.error() << std::endl;
148 LOG(ERROR) << "Unable to create PipeRelay: " << relay.error();
Yifan Hong48dc9f82017-05-09 19:33:08 -0700149 return IO_ERROR;
Andreas Huber28d35912017-03-24 13:14:11 -0700150 }
151
152 deleted_unique_ptr<native_handle_t> fdHandle(
153 native_handle_create(1 /* numFds */, 0 /* numInts */),
154 native_handle_delete);
155
Yifan Hong891989f2021-06-17 18:31:42 -0700156 fdHandle->data[0] = relay.value()->fd().get();
Andreas Huber28d35912017-03-24 13:14:11 -0700157
Yifan Hong443df792017-05-09 18:49:45 -0700158 hardware::Return<void> ret = base->debug(fdHandle.get(), convert(options));
Andreas Huber28d35912017-03-24 13:14:11 -0700159
160 if (!ret.isOk()) {
Yifan Hong48dc9f82017-05-09 19:33:08 -0700161 std::string msg = "debug() FAILED on " + interfaceName + "/" + instanceName + ": "
162 + ret.description();
163 err << msg << std::endl;
164 LOG(ERROR) << msg;
165 return TRANSACTION_ERROR;
Andreas Huber28d35912017-03-24 13:14:11 -0700166 }
Yifan Hong48dc9f82017-05-09 19:33:08 -0700167 return OK;
Andreas Huber28d35912017-03-24 13:14:11 -0700168}
169
Yifan Hong443df792017-05-09 18:49:45 -0700170Status Lshal::parseArgs(const Arg &arg) {
Yifan Hongb0dde932017-02-10 17:49:58 -0800171 optind = 1;
Yifan Hong443df792017-05-09 18:49:45 -0700172 if (optind >= arg.argc) {
173 // no options at all.
174 return OK;
175 }
176 mCommand = arg.argv[optind];
Yifan Hong795b6ec2017-09-13 11:25:28 -0700177 if (selectCommand(mCommand) != nullptr) {
Yifan Hong443df792017-05-09 18:49:45 -0700178 ++optind;
179 return OK; // mCommand is set correctly
Yifan Hongb0dde932017-02-10 17:49:58 -0800180 }
Yifan Hong38d53e02017-02-13 17:51:59 -0800181
Yifan Hong443df792017-05-09 18:49:45 -0700182 if (mCommand.size() > 0 && mCommand[0] == '-') {
183 // first argument is an option, set command to "" (which is recognized as "list")
Yifan Hong795b6ec2017-09-13 11:25:28 -0700184 mCommand.clear();
Yifan Hong443df792017-05-09 18:49:45 -0700185 return OK;
Yifan Hong38d53e02017-02-13 17:51:59 -0800186 }
Yifan Hong443df792017-05-09 18:49:45 -0700187
Yifan Honga8bedc62017-09-08 18:00:31 -0700188 err() << arg.argv[0] << ": unrecognized option `" << arg.argv[optind] << "'" << std::endl;
Yifan Hong443df792017-05-09 18:49:45 -0700189 return USAGE;
Yifan Hongb0dde932017-02-10 17:49:58 -0800190}
191
Yifan Hong9881df92017-05-10 14:33:05 -0700192void signalHandler(int sig) {
193 if (sig == SIGINT) {
194 int retVal;
195 pthread_exit(&retVal);
196 }
197}
198
Yifan Hong795b6ec2017-09-13 11:25:28 -0700199Command* Lshal::selectCommand(const std::string& command) const {
200 if (command.empty()) {
201 return selectCommand(ListCommand::GetName());
Yifan Hongded398e2017-09-07 13:54:28 -0700202 }
Yifan Hong795b6ec2017-09-13 11:25:28 -0700203 for (const auto& e : mRegisteredCommands) {
204 if (e->getName() == command) {
205 return e.get();
206 }
Yifan Honga8bedc62017-09-08 18:00:31 -0700207 }
Yifan Hongded398e2017-09-07 13:54:28 -0700208 return nullptr;
209}
210
Yifan Hong443df792017-05-09 18:49:45 -0700211Status Lshal::main(const Arg &arg) {
Yifan Hong9881df92017-05-10 14:33:05 -0700212 // Allow SIGINT to terminate all threads.
213 signal(SIGINT, signalHandler);
214
Yifan Hong443df792017-05-09 18:49:45 -0700215 Status status = parseArgs(arg);
Yifan Hongb0dde932017-02-10 17:49:58 -0800216 if (status != OK) {
Yifan Honga8bedc62017-09-08 18:00:31 -0700217 usage();
Yifan Hongb0dde932017-02-10 17:49:58 -0800218 return status;
219 }
Yifan Honga8bedc62017-09-08 18:00:31 -0700220 auto c = selectCommand(mCommand);
221 if (c == nullptr) {
222 // unknown command, print global usage
223 usage();
Yifan Hong443df792017-05-09 18:49:45 -0700224 return USAGE;
225 }
Yifan Honga8bedc62017-09-08 18:00:31 -0700226 status = c->main(arg);
227 if (status == USAGE) {
228 // bad options. Run `lshal help ${mCommand}` instead.
229 // For example, `lshal --unknown-option` becomes `lshal help` (prints global help)
230 // and `lshal list --unknown-option` becomes `lshal help list`
Yifan Hong795b6ec2017-09-13 11:25:28 -0700231 auto&& help = selectCommand(HelpCommand::GetName());
232 return static_cast<HelpCommand*>(help)->usageOfCommand(mCommand);
Yifan Hong443df792017-05-09 18:49:45 -0700233 }
Yifan Honga8bedc62017-09-08 18:00:31 -0700234
Yifan Hong6b13e502024-02-01 16:54:36 -0800235 // After Lshal::main() finishes, caller may call _exit(), causing debug
236 // information to prematurely ends. Hence flush().
237 err().flush();
238 out().flush();
239
Yifan Honga8bedc62017-09-08 18:00:31 -0700240 return status;
Yifan Hong443df792017-05-09 18:49:45 -0700241}
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 Hong96bc7142023-12-18 21:57:56 -0800258void Lshal::setWaitTimeForTest(std::chrono::milliseconds ipcCallWait,
259 std::chrono::milliseconds debugDumpWait) {
260 mIpcCallWait = ipcCallWait;
261 mDebugDumpWait = debugDumpWait;
262}
263std::chrono::milliseconds Lshal::getIpcCallWait() const {
264 return mIpcCallWait;
265}
266std::chrono::milliseconds Lshal::getDebugDumpWait() const {
267 return mDebugDumpWait;
268}
269
Yifan Hongb0dde932017-02-10 17:49:58 -0800270} // namespace lshal
271} // namespace android