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" |
Yifan Hong | 1bc1e9f | 2017-08-29 17:28:12 -0700 | [diff] [blame] | 66 | " List all hals with default ordering and columns (`lshal list -iepc`)\n" |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 67 | " lshal list [-h|--help]\n" |
| 68 | " -h, --help: Print help message for list (`lshal help list`)\n" |
Steven Moreland | d8e2019 | 2017-05-24 11:23:08 -0700 | [diff] [blame] | 69 | " lshal [list] [--interface|-i] [--transport|-t] [-r|--arch] [-e|--threads]\n" |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 70 | " [--pid|-p] [--address|-a] [--clients|-c] [--cmdline|-m]\n" |
| 71 | " [--sort={interface|i|pid|p}] [--init-vintf[=<output file>]]\n" |
Yifan Hong | 45a0ead | 2017-08-15 10:01:02 -0700 | [diff] [blame] | 72 | " [--debug|-d[=<output file>]] [--neat]\n" |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 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 Moreland | d8e2019 | 2017-05-24 11:23:08 -0700 | [diff] [blame] | 77 | " -e, --threads: print currently used/available threads\n" |
| 78 | " (note, available threads created lazily)\n" |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 79 | " -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" |
Yifan Hong | 1bc1e9f | 2017-08-29 17:28:12 -0700 | [diff] [blame] | 84 | " IBase::debug with empty options. Cannot be used with --neat.\n" |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 85 | " --sort=i, --sort=interface: sort by interface name\n" |
| 86 | " --sort=p, --sort=pid: sort by server pid\n" |
Yifan Hong | 45a0ead | 2017-08-15 10:01:02 -0700 | [diff] [blame] | 87 | " --neat: output is machine parsable (no explanatory text)\n" |
Yifan Hong | 1bc1e9f | 2017-08-29 17:28:12 -0700 | [diff] [blame] | 88 | " Cannot be used with --debug.\n" |
Yifan Hong | 45a0ead | 2017-08-15 10:01:02 -0700 | [diff] [blame] | 89 | " --init-vintf[=<output file>]: form a skeleton HAL manifest to specified\n" |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 90 | " file, or stdout if no file specified.\n"; |
| 91 | |
| 92 | static const std::string debug = |
| 93 | "debug:\n" |
| 94 | " lshal debug <interface> [options [options [...]]] \n" |
| 95 | " Print debug information of a specified interface.\n" |
| 96 | " <inteface>: Format is `android.hardware.foo@1.0::IFoo/default`.\n" |
| 97 | " If instance name is missing `default` is used.\n" |
| 98 | " options: space separated options to IBase::debug.\n"; |
| 99 | |
| 100 | static const std::string help = |
| 101 | "help:\n" |
| 102 | " lshal -h\n" |
| 103 | " lshal --help\n" |
| 104 | " lshal help\n" |
| 105 | " Print this help message\n" |
| 106 | " lshal help list\n" |
| 107 | " Print help message for list\n" |
| 108 | " lshal help debug\n" |
| 109 | " Print help message for debug\n"; |
| 110 | |
| 111 | if (command == "list") { |
Yifan Hong | 76ac14a | 2017-09-08 14:59:04 -0700 | [diff] [blame] | 112 | err() << list; |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 113 | return; |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 114 | } |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 115 | if (command == "debug") { |
Yifan Hong | 76ac14a | 2017-09-08 14:59:04 -0700 | [diff] [blame] | 116 | err() << debug; |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 117 | return; |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 118 | } |
| 119 | |
Yifan Hong | 76ac14a | 2017-09-08 14:59:04 -0700 | [diff] [blame] | 120 | err() << helpSummary << "\n" << list << "\n" << debug << "\n" << help; |
Yifan Hong | b447902 | 2017-03-02 16:54:11 -0800 | [diff] [blame] | 121 | } |
| 122 | |
Andreas Huber | 28d3591 | 2017-03-24 13:14:11 -0700 | [diff] [blame] | 123 | // A unique_ptr type using a custom deleter function. |
| 124 | template<typename T> |
| 125 | using deleted_unique_ptr = std::unique_ptr<T, std::function<void(T *)> >; |
| 126 | |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 127 | static hardware::hidl_vec<hardware::hidl_string> convert(const std::vector<std::string> &v) { |
| 128 | hardware::hidl_vec<hardware::hidl_string> hv; |
| 129 | hv.resize(v.size()); |
| 130 | for (size_t i = 0; i < v.size(); ++i) { |
| 131 | hv[i].setToExternal(v[i].c_str(), v[i].size()); |
| 132 | } |
| 133 | return hv; |
| 134 | } |
| 135 | |
Yifan Hong | 48dc9f8 | 2017-05-09 19:33:08 -0700 | [diff] [blame] | 136 | Status Lshal::emitDebugInfo( |
Andreas Huber | 28d3591 | 2017-03-24 13:14:11 -0700 | [diff] [blame] | 137 | const std::string &interfaceName, |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 138 | const std::string &instanceName, |
| 139 | const std::vector<std::string> &options, |
Yifan Hong | 48dc9f8 | 2017-05-09 19:33:08 -0700 | [diff] [blame] | 140 | std::ostream &out, |
| 141 | NullableOStream<std::ostream> err) const { |
Andreas Huber | 28d3591 | 2017-03-24 13:14:11 -0700 | [diff] [blame] | 142 | using android::hidl::base::V1_0::IBase; |
| 143 | |
Yifan Hong | 9881df9 | 2017-05-10 14:33:05 -0700 | [diff] [blame] | 144 | hardware::Return<sp<IBase>> retBase = serviceManager()->get(interfaceName, instanceName); |
Andreas Huber | 28d3591 | 2017-03-24 13:14:11 -0700 | [diff] [blame] | 145 | |
Yifan Hong | 48dc9f8 | 2017-05-09 19:33:08 -0700 | [diff] [blame] | 146 | if (!retBase.isOk()) { |
| 147 | std::string msg = "Cannot get " + interfaceName + "/" + instanceName + ": " |
| 148 | + retBase.description(); |
| 149 | err << msg << std::endl; |
| 150 | LOG(ERROR) << msg; |
| 151 | return TRANSACTION_ERROR; |
| 152 | } |
| 153 | |
| 154 | sp<IBase> base = retBase; |
| 155 | if (base == nullptr) { |
| 156 | std::string msg = interfaceName + "/" + instanceName + " does not exist, or " |
| 157 | + "no permission to connect."; |
| 158 | err << msg << std::endl; |
| 159 | LOG(ERROR) << msg; |
| 160 | return NO_INTERFACE; |
Andreas Huber | 28d3591 | 2017-03-24 13:14:11 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 163 | PipeRelay relay(out); |
Andreas Huber | 28d3591 | 2017-03-24 13:14:11 -0700 | [diff] [blame] | 164 | |
| 165 | if (relay.initCheck() != OK) { |
Yifan Hong | 48dc9f8 | 2017-05-09 19:33:08 -0700 | [diff] [blame] | 166 | std::string msg = "PipeRelay::initCheck() FAILED w/ " + std::to_string(relay.initCheck()); |
| 167 | err << msg << std::endl; |
| 168 | LOG(ERROR) << msg; |
| 169 | return IO_ERROR; |
Andreas Huber | 28d3591 | 2017-03-24 13:14:11 -0700 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | deleted_unique_ptr<native_handle_t> fdHandle( |
| 173 | native_handle_create(1 /* numFds */, 0 /* numInts */), |
| 174 | native_handle_delete); |
| 175 | |
| 176 | fdHandle->data[0] = relay.fd(); |
| 177 | |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 178 | hardware::Return<void> ret = base->debug(fdHandle.get(), convert(options)); |
Andreas Huber | 28d3591 | 2017-03-24 13:14:11 -0700 | [diff] [blame] | 179 | |
| 180 | if (!ret.isOk()) { |
Yifan Hong | 48dc9f8 | 2017-05-09 19:33:08 -0700 | [diff] [blame] | 181 | std::string msg = "debug() FAILED on " + interfaceName + "/" + instanceName + ": " |
| 182 | + ret.description(); |
| 183 | err << msg << std::endl; |
| 184 | LOG(ERROR) << msg; |
| 185 | return TRANSACTION_ERROR; |
Andreas Huber | 28d3591 | 2017-03-24 13:14:11 -0700 | [diff] [blame] | 186 | } |
Yifan Hong | 48dc9f8 | 2017-05-09 19:33:08 -0700 | [diff] [blame] | 187 | return OK; |
Andreas Huber | 28d3591 | 2017-03-24 13:14:11 -0700 | [diff] [blame] | 188 | } |
| 189 | |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 190 | Status Lshal::parseArgs(const Arg &arg) { |
| 191 | static std::set<std::string> sAllCommands{"list", "debug", "help"}; |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 192 | optind = 1; |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 193 | if (optind >= arg.argc) { |
| 194 | // no options at all. |
| 195 | return OK; |
| 196 | } |
| 197 | mCommand = arg.argv[optind]; |
| 198 | if (sAllCommands.find(mCommand) != sAllCommands.end()) { |
| 199 | ++optind; |
| 200 | return OK; // mCommand is set correctly |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 201 | } |
Yifan Hong | 38d53e0 | 2017-02-13 17:51:59 -0800 | [diff] [blame] | 202 | |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 203 | if (mCommand.size() > 0 && mCommand[0] == '-') { |
| 204 | // first argument is an option, set command to "" (which is recognized as "list") |
| 205 | mCommand = ""; |
| 206 | return OK; |
Yifan Hong | 38d53e0 | 2017-02-13 17:51:59 -0800 | [diff] [blame] | 207 | } |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 208 | |
Yifan Hong | 76ac14a | 2017-09-08 14:59:04 -0700 | [diff] [blame] | 209 | err() << arg.argv[0] << ": unrecognized option `" << arg.argv[optind] << "`" << std::endl; |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 210 | usage(); |
| 211 | return USAGE; |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 212 | } |
| 213 | |
Yifan Hong | 9881df9 | 2017-05-10 14:33:05 -0700 | [diff] [blame] | 214 | void signalHandler(int sig) { |
| 215 | if (sig == SIGINT) { |
| 216 | int retVal; |
| 217 | pthread_exit(&retVal); |
| 218 | } |
| 219 | } |
| 220 | |
Yifan Hong | ded398e | 2017-09-07 13:54:28 -0700 | [diff] [blame^] | 221 | std::unique_ptr<Command> Lshal::selectCommand(const std::string& command) { |
| 222 | // Default command is list |
| 223 | if (command == "list" || command == "") { |
| 224 | return std::make_unique<ListCommand>(*this); |
| 225 | } |
| 226 | if (command == "debug") { |
| 227 | return std::make_unique<DebugCommand>(*this); |
| 228 | } |
| 229 | return nullptr; |
| 230 | } |
| 231 | |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 232 | Status Lshal::main(const Arg &arg) { |
Yifan Hong | 9881df9 | 2017-05-10 14:33:05 -0700 | [diff] [blame] | 233 | // Allow SIGINT to terminate all threads. |
| 234 | signal(SIGINT, signalHandler); |
| 235 | |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 236 | Status status = parseArgs(arg); |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 237 | if (status != OK) { |
| 238 | return status; |
| 239 | } |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 240 | if (mCommand == "help") { |
| 241 | usage(optind < arg.argc ? arg.argv[optind] : ""); |
| 242 | return USAGE; |
| 243 | } |
Yifan Hong | ded398e | 2017-09-07 13:54:28 -0700 | [diff] [blame^] | 244 | auto c = selectCommand(mCommand); |
| 245 | if (c != nullptr) { |
| 246 | return c->main(mCommand, arg); |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 247 | } |
| 248 | usage(); |
| 249 | return USAGE; |
| 250 | } |
| 251 | |
| 252 | NullableOStream<std::ostream> Lshal::err() const { |
| 253 | return mErr; |
| 254 | } |
| 255 | NullableOStream<std::ostream> Lshal::out() const { |
| 256 | return mOut; |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 257 | } |
| 258 | |
Yifan Hong | 9881df9 | 2017-05-10 14:33:05 -0700 | [diff] [blame] | 259 | const sp<IServiceManager> &Lshal::serviceManager() const { |
| 260 | return mServiceManager; |
| 261 | } |
| 262 | |
| 263 | const sp<IServiceManager> &Lshal::passthroughManager() const { |
| 264 | return mPassthroughManager; |
Yifan Hong | a57dffb | 2017-02-21 14:59:00 -0800 | [diff] [blame] | 265 | } |
| 266 | |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 267 | } // namespace lshal |
| 268 | } // namespace android |