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 
21 #include <hidl-util/FQName.h>
22 
23 namespace android {
24 namespace lshal {
25 
getName() const26 std::string DebugCommand::getName() const {
27     return "debug";
28 }
29 
getSimpleDescription() const30 std::string DebugCommand::getSimpleDescription() const {
31     return "Debug a specified HAL.";
32 }
33 
parseArgs(const Arg & arg)34 Status DebugCommand::parseArgs(const Arg &arg) {
35     if (optind >= arg.argc) {
36         return USAGE;
37     }
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 
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 
main(const Arg & arg)54 Status DebugCommand::main(const Arg &arg) {
55     Status status = parseArgs(arg);
56     if (status != OK) {
57         return status;
58     }
59 
60     auto pair = splitFirst(mInterfaceName, '/');
61 
62     FQName fqName;
63     if (!FQName::parse(pair.first, &fqName) || fqName.isIdentifier() || !fqName.isFullyQualified()) {
64         mLshal.err() << "Invalid fully-qualified name '" << pair.first << "'\n\n";
65         return USAGE;
66     }
67 
68     return mLshal.emitDebugInfo(
69             pair.first, pair.second.empty() ? "default" : pair.second, mOptions,
70             mExcludesParentInstances,
71             mLshal.out().buf(),
72             mLshal.err());
73 }
74 
usage() const75 void DebugCommand::usage() const {
76 
77     static const std::string debug =
78             "debug:\n"
79             "    lshal debug [-E] <interface> [options [options [...]]] \n"
80             "        Print debug information of a specified interface.\n"
81             "        -E: excludes debug output if HAL is actually a subclass.\n"
82             "        <interface>: Format is `android.hardware.foo@1.0::IFoo/default`.\n"
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 
89 }  // namespace lshal
90 }  // namespace android
91