| Yifan Hong | 48dc9f8 | 2017-05-09 19:33:08 -0700 | [diff] [blame] | 1 | /* | 
 | 2 |  * Copyright (C) 2017 The Android Open Source Project | 
 | 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 "DebugCommand.h" | 
 | 18 |  | 
 | 19 | #include "Lshal.h" | 
 | 20 |  | 
| Steven Moreland | 3481c9a | 2018-01-04 17:14:17 -0800 | [diff] [blame] | 21 | #include <hidl-util/FQName.h> | 
 | 22 |  | 
| Yifan Hong | 48dc9f8 | 2017-05-09 19:33:08 -0700 | [diff] [blame] | 23 | namespace android { | 
 | 24 | namespace lshal { | 
 | 25 |  | 
| Yifan Hong | 795b6ec | 2017-09-13 11:25:28 -0700 | [diff] [blame] | 26 | std::string DebugCommand::getName() const { | 
 | 27 |     return "debug"; | 
 | 28 | } | 
 | 29 |  | 
 | 30 | std::string DebugCommand::getSimpleDescription() const { | 
 | 31 |     return "Debug a specified HAL."; | 
 | 32 | } | 
 | 33 |  | 
| Yifan Hong | a8bedc6 | 2017-09-08 18:00:31 -0700 | [diff] [blame] | 34 | Status DebugCommand::parseArgs(const Arg &arg) { | 
| Yifan Hong | 48dc9f8 | 2017-05-09 19:33:08 -0700 | [diff] [blame] | 35 |     if (optind >= arg.argc) { | 
| Yifan Hong | 48dc9f8 | 2017-05-09 19:33:08 -0700 | [diff] [blame] | 36 |         return USAGE; | 
 | 37 |     } | 
| Steven Moreland | 5f32889 | 2018-01-18 14:38:07 -0800 | [diff] [blame] | 38 |  | 
 | 39 |     // Optargs cannnot be used because the flag should not be considered set | 
 | 40 |     // if it should really be contained in mOptions. | 
 | 41 |     if (std::string(arg.argv[optind]) == "-E") { | 
 | 42 |         mExcludesParentInstances = true; | 
 | 43 |         optind++; | 
 | 44 |     } | 
 | 45 |  | 
| Yifan Hong | 48dc9f8 | 2017-05-09 19:33:08 -0700 | [diff] [blame] | 46 |     mInterfaceName = arg.argv[optind]; | 
 | 47 |     ++optind; | 
 | 48 |     for (; optind < arg.argc; ++optind) { | 
 | 49 |         mOptions.push_back(arg.argv[optind]); | 
 | 50 |     } | 
 | 51 |     return OK; | 
 | 52 | } | 
 | 53 |  | 
| Yifan Hong | a8bedc6 | 2017-09-08 18:00:31 -0700 | [diff] [blame] | 54 | Status DebugCommand::main(const Arg &arg) { | 
 | 55 |     Status status = parseArgs(arg); | 
| Yifan Hong | 48dc9f8 | 2017-05-09 19:33:08 -0700 | [diff] [blame] | 56 |     if (status != OK) { | 
 | 57 |         return status; | 
 | 58 |     } | 
| Steven Moreland | 3481c9a | 2018-01-04 17:14:17 -0800 | [diff] [blame] | 59 |  | 
| Yifan Hong | 48dc9f8 | 2017-05-09 19:33:08 -0700 | [diff] [blame] | 60 |     auto pair = splitFirst(mInterfaceName, '/'); | 
| Steven Moreland | 3481c9a | 2018-01-04 17:14:17 -0800 | [diff] [blame] | 61 |  | 
| Steven Moreland | d4f32b3 | 2018-03-06 14:47:58 -0800 | [diff] [blame] | 62 |     FQName fqName; | 
 | 63 |     if (!FQName::parse(pair.first, &fqName) || fqName.isIdentifier() || !fqName.isFullyQualified()) { | 
| Steven Moreland | 3481c9a | 2018-01-04 17:14:17 -0800 | [diff] [blame] | 64 |         mLshal.err() << "Invalid fully-qualified name '" << pair.first << "'\n\n"; | 
 | 65 |         return USAGE; | 
 | 66 |     } | 
 | 67 |  | 
| Yifan Hong | 48dc9f8 | 2017-05-09 19:33:08 -0700 | [diff] [blame] | 68 |     return mLshal.emitDebugInfo( | 
 | 69 |             pair.first, pair.second.empty() ? "default" : pair.second, mOptions, | 
| Steven Moreland | 5f32889 | 2018-01-18 14:38:07 -0800 | [diff] [blame] | 70 |             mExcludesParentInstances, | 
| Yifan Hong | 48dc9f8 | 2017-05-09 19:33:08 -0700 | [diff] [blame] | 71 |             mLshal.out().buf(), | 
 | 72 |             mLshal.err()); | 
 | 73 | } | 
 | 74 |  | 
| Yifan Hong | a8bedc6 | 2017-09-08 18:00:31 -0700 | [diff] [blame] | 75 | void DebugCommand::usage() const { | 
 | 76 |  | 
 | 77 |     static const std::string debug = | 
 | 78 |             "debug:\n" | 
| Steven Moreland | 5f32889 | 2018-01-18 14:38:07 -0800 | [diff] [blame] | 79 |             "    lshal debug [-E] <interface> [options [options [...]]] \n" | 
| Yifan Hong | a8bedc6 | 2017-09-08 18:00:31 -0700 | [diff] [blame] | 80 |             "        Print debug information of a specified interface.\n" | 
| Steven Moreland | 5f32889 | 2018-01-18 14:38:07 -0800 | [diff] [blame] | 81 |             "        -E: excludes debug output if HAL is actually a subclass.\n" | 
| Felipe Leme | 2382d31 | 2019-06-21 14:55:50 -0700 | [diff] [blame] | 82 |             "        <interface>: Format is `android.hardware.foo@1.0::IFoo/default`.\n" | 
| Yifan Hong | a8bedc6 | 2017-09-08 18:00:31 -0700 | [diff] [blame] | 83 |             "            If instance name is missing `default` is used.\n" | 
 | 84 |             "        options: space separated options to IBase::debug.\n"; | 
 | 85 |  | 
 | 86 |     mLshal.err() << debug; | 
 | 87 | } | 
 | 88 |  | 
| Yifan Hong | 48dc9f8 | 2017-05-09 19:33:08 -0700 | [diff] [blame] | 89 | }  // namespace lshal | 
 | 90 | }  // namespace android |