Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 1 | /* |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 2 | * Copyright (C) 2017 The Android Open Source Project |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 3 | * |
| 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 Hong | 48dc9f8 | 2017-05-09 19:33:08 -0700 | [diff] [blame] | 17 | #define LOG_TAG "lshal" |
| 18 | #include <android-base/logging.h> |
| 19 | |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 20 | #include "Lshal.h" |
| 21 | |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 22 | #include <set> |
| 23 | #include <string> |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 24 | |
Yifan Hong | 48dc9f8 | 2017-05-09 19:33:08 -0700 | [diff] [blame] | 25 | #include <hidl/ServiceManagement.h> |
| 26 | |
| 27 | #include "DebugCommand.h" |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 28 | #include "ListCommand.h" |
Andreas Huber | 28d3591 | 2017-03-24 13:14:11 -0700 | [diff] [blame] | 29 | #include "PipeRelay.h" |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 30 | |
| 31 | namespace android { |
| 32 | namespace lshal { |
| 33 | |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 34 | using ::android::hidl::manager::V1_0::IServiceManager; |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 35 | |
Yifan Hong | 9881df9 | 2017-05-10 14:33:05 -0700 | [diff] [blame^] | 36 | Lshal::Lshal() |
| 37 | : mOut(std::cout), mErr(std::cerr), |
| 38 | mServiceManager(::android::hardware::defaultServiceManager()), |
| 39 | mPassthroughManager(::android::hardware::getPassthroughServiceManager()) { |
| 40 | } |
| 41 | |
| 42 | Lshal::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 Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | void 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 Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 110 | } |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 111 | if (command == "debug") { |
| 112 | mErr << debug; |
| 113 | return; |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 114 | } |
| 115 | |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 116 | mErr << helpSummary << "\n" << list << "\n" << debug << "\n" << help; |
Yifan Hong | b447902 | 2017-03-02 16:54:11 -0800 | [diff] [blame] | 117 | } |
| 118 | |
Andreas Huber | 28d3591 | 2017-03-24 13:14:11 -0700 | [diff] [blame] | 119 | // A unique_ptr type using a custom deleter function. |
| 120 | template<typename T> |
| 121 | using deleted_unique_ptr = std::unique_ptr<T, std::function<void(T *)> >; |
| 122 | |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 123 | static 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 Hong | 48dc9f8 | 2017-05-09 19:33:08 -0700 | [diff] [blame] | 132 | Status Lshal::emitDebugInfo( |
Andreas Huber | 28d3591 | 2017-03-24 13:14:11 -0700 | [diff] [blame] | 133 | const std::string &interfaceName, |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 134 | const std::string &instanceName, |
| 135 | const std::vector<std::string> &options, |
Yifan Hong | 48dc9f8 | 2017-05-09 19:33:08 -0700 | [diff] [blame] | 136 | std::ostream &out, |
| 137 | NullableOStream<std::ostream> err) const { |
Andreas Huber | 28d3591 | 2017-03-24 13:14:11 -0700 | [diff] [blame] | 138 | using android::hidl::base::V1_0::IBase; |
| 139 | |
Yifan Hong | 9881df9 | 2017-05-10 14:33:05 -0700 | [diff] [blame^] | 140 | hardware::Return<sp<IBase>> retBase = serviceManager()->get(interfaceName, instanceName); |
Andreas Huber | 28d3591 | 2017-03-24 13:14:11 -0700 | [diff] [blame] | 141 | |
Yifan Hong | 48dc9f8 | 2017-05-09 19:33:08 -0700 | [diff] [blame] | 142 | 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 Huber | 28d3591 | 2017-03-24 13:14:11 -0700 | [diff] [blame] | 157 | } |
| 158 | |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 159 | PipeRelay relay(out); |
Andreas Huber | 28d3591 | 2017-03-24 13:14:11 -0700 | [diff] [blame] | 160 | |
| 161 | if (relay.initCheck() != OK) { |
Yifan Hong | 48dc9f8 | 2017-05-09 19:33:08 -0700 | [diff] [blame] | 162 | 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 Huber | 28d3591 | 2017-03-24 13:14:11 -0700 | [diff] [blame] | 166 | } |
| 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 Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 174 | hardware::Return<void> ret = base->debug(fdHandle.get(), convert(options)); |
Andreas Huber | 28d3591 | 2017-03-24 13:14:11 -0700 | [diff] [blame] | 175 | |
| 176 | if (!ret.isOk()) { |
Yifan Hong | 48dc9f8 | 2017-05-09 19:33:08 -0700 | [diff] [blame] | 177 | 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 Huber | 28d3591 | 2017-03-24 13:14:11 -0700 | [diff] [blame] | 182 | } |
Yifan Hong | 48dc9f8 | 2017-05-09 19:33:08 -0700 | [diff] [blame] | 183 | return OK; |
Andreas Huber | 28d3591 | 2017-03-24 13:14:11 -0700 | [diff] [blame] | 184 | } |
| 185 | |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 186 | Status Lshal::parseArgs(const Arg &arg) { |
| 187 | static std::set<std::string> sAllCommands{"list", "debug", "help"}; |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 188 | optind = 1; |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 189 | 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 Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 197 | } |
Yifan Hong | 38d53e0 | 2017-02-13 17:51:59 -0800 | [diff] [blame] | 198 | |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 199 | 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 Hong | 38d53e0 | 2017-02-13 17:51:59 -0800 | [diff] [blame] | 203 | } |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 204 | |
| 205 | mErr << arg.argv[0] << ": unrecognized option `" << arg.argv[optind] << "`" << std::endl; |
| 206 | usage(); |
| 207 | return USAGE; |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 208 | } |
| 209 | |
Yifan Hong | 9881df9 | 2017-05-10 14:33:05 -0700 | [diff] [blame^] | 210 | void signalHandler(int sig) { |
| 211 | if (sig == SIGINT) { |
| 212 | int retVal; |
| 213 | pthread_exit(&retVal); |
| 214 | } |
| 215 | } |
| 216 | |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 217 | Status Lshal::main(const Arg &arg) { |
Yifan Hong | 9881df9 | 2017-05-10 14:33:05 -0700 | [diff] [blame^] | 218 | // Allow SIGINT to terminate all threads. |
| 219 | signal(SIGINT, signalHandler); |
| 220 | |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 221 | Status status = parseArgs(arg); |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 222 | if (status != OK) { |
| 223 | return status; |
| 224 | } |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 225 | 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 Hong | 48dc9f8 | 2017-05-09 19:33:08 -0700 | [diff] [blame] | 234 | return DebugCommand{*this}.main(mCommand, arg); |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 235 | } |
| 236 | usage(); |
| 237 | return USAGE; |
| 238 | } |
| 239 | |
| 240 | NullableOStream<std::ostream> Lshal::err() const { |
| 241 | return mErr; |
| 242 | } |
| 243 | NullableOStream<std::ostream> Lshal::out() const { |
| 244 | return mOut; |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 245 | } |
| 246 | |
Yifan Hong | 9881df9 | 2017-05-10 14:33:05 -0700 | [diff] [blame^] | 247 | const sp<IServiceManager> &Lshal::serviceManager() const { |
| 248 | return mServiceManager; |
| 249 | } |
| 250 | |
| 251 | const sp<IServiceManager> &Lshal::passthroughManager() const { |
| 252 | return mPassthroughManager; |
Yifan Hong | a57dffb | 2017-02-21 14:59:00 -0800 | [diff] [blame] | 253 | } |
| 254 | |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 255 | } // namespace lshal |
| 256 | } // namespace android |