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 | |
| 17 | #include "Lshal.h" |
| 18 | |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame^] | 19 | #include <set> |
| 20 | #include <string> |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 21 | |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame^] | 22 | #include "ListCommand.h" |
Andreas Huber | 28d3591 | 2017-03-24 13:14:11 -0700 | [diff] [blame] | 23 | #include "PipeRelay.h" |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 24 | |
| 25 | namespace android { |
| 26 | namespace lshal { |
| 27 | |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame^] | 28 | using ::android::hidl::manager::V1_0::IServiceManager; |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 29 | |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame^] | 30 | Lshal::Lshal() { |
| 31 | } |
| 32 | |
| 33 | void 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 Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 92 | } |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame^] | 93 | if (command == "debug") { |
| 94 | mErr << debug; |
| 95 | return; |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 96 | } |
| 97 | |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame^] | 98 | mErr << helpSummary << "\n" << list << "\n" << debug << "\n" << help; |
Yifan Hong | b447902 | 2017-03-02 16:54:11 -0800 | [diff] [blame] | 99 | } |
| 100 | |
Andreas Huber | 28d3591 | 2017-03-24 13:14:11 -0700 | [diff] [blame] | 101 | // A unique_ptr type using a custom deleter function. |
| 102 | template<typename T> |
| 103 | using deleted_unique_ptr = std::unique_ptr<T, std::function<void(T *)> >; |
| 104 | |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame^] | 105 | static 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 Huber | 28d3591 | 2017-03-24 13:14:11 -0700 | [diff] [blame] | 115 | void Lshal::emitDebugInfo( |
| 116 | const sp<IServiceManager> &serviceManager, |
| 117 | const std::string &interfaceName, |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame^] | 118 | const std::string &instanceName, |
| 119 | const std::vector<std::string> &options, |
| 120 | std::ostream &out) { |
Andreas Huber | 28d3591 | 2017-03-24 13:14:11 -0700 | [diff] [blame] | 121 | 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 Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame^] | 128 | mErr << interfaceName << "/" << instanceName << " does not exist." << std::endl; |
Andreas Huber | 28d3591 | 2017-03-24 13:14:11 -0700 | [diff] [blame] | 129 | return; |
| 130 | } |
| 131 | |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame^] | 132 | PipeRelay relay(out); |
Andreas Huber | 28d3591 | 2017-03-24 13:14:11 -0700 | [diff] [blame] | 133 | |
| 134 | if (relay.initCheck() != OK) { |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame^] | 135 | mErr << "PipeRelay::initCheck() FAILED w/ " << relay.initCheck() << std::endl; |
Andreas Huber | 28d3591 | 2017-03-24 13:14:11 -0700 | [diff] [blame] | 136 | 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 Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame^] | 145 | hardware::Return<void> ret = base->debug(fdHandle.get(), convert(options)); |
Andreas Huber | 28d3591 | 2017-03-24 13:14:11 -0700 | [diff] [blame] | 146 | |
| 147 | if (!ret.isOk()) { |
| 148 | LOG(ERROR) |
| 149 | << interfaceName |
| 150 | << "::debug(...) FAILED. (instance " |
| 151 | << instanceName |
| 152 | << ")"; |
| 153 | } |
| 154 | } |
| 155 | |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame^] | 156 | Status Lshal::parseArgs(const Arg &arg) { |
| 157 | static std::set<std::string> sAllCommands{"list", "debug", "help"}; |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 158 | optind = 1; |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame^] | 159 | 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 Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 167 | } |
Yifan Hong | 38d53e0 | 2017-02-13 17:51:59 -0800 | [diff] [blame] | 168 | |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame^] | 169 | 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 Hong | 38d53e0 | 2017-02-13 17:51:59 -0800 | [diff] [blame] | 173 | } |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame^] | 174 | |
| 175 | mErr << arg.argv[0] << ": unrecognized option `" << arg.argv[optind] << "`" << std::endl; |
| 176 | usage(); |
| 177 | return USAGE; |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 178 | } |
| 179 | |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame^] | 180 | Status Lshal::main(const Arg &arg) { |
| 181 | Status status = parseArgs(arg); |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 182 | if (status != OK) { |
| 183 | return status; |
| 184 | } |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame^] | 185 | 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 | |
| 201 | NullableOStream<std::ostream> Lshal::err() const { |
| 202 | return mErr; |
| 203 | } |
| 204 | NullableOStream<std::ostream> Lshal::out() const { |
| 205 | return mOut; |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 206 | } |
| 207 | |
Yifan Hong | a57dffb | 2017-02-21 14:59:00 -0800 | [diff] [blame] | 208 | void signalHandler(int sig) { |
| 209 | if (sig == SIGINT) { |
| 210 | int retVal; |
| 211 | pthread_exit(&retVal); |
| 212 | } |
| 213 | } |
| 214 | |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 215 | } // namespace lshal |
| 216 | } // namespace android |
| 217 | |
| 218 | int main(int argc, char **argv) { |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame^] | 219 | using namespace ::android::lshal; |
| 220 | signal(SIGINT, signalHandler); |
| 221 | return Lshal{}.main(Arg{argc, argv}); |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 222 | } |