1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <iostream>
17
18 #include "command_helper.h"
19
Start(int * argc,char *** argv,const std::string & help)20 int CommandHelper::Start(int* argc, char*** argv, const std::string& help)
21 {
22 if ((*argc) == 1) {
23 // no argument specified, we speculate the user may do not know the command usage.
24 // Hence print the usage help and continue
25 std::cout << tipHelp_ << std::endl;
26 return 0;
27 }
28 if (help.compare(SUPPORTED_ARGS[ARG_NONG]) == 0) {
29 // user does not issue help
30 bool printTip {false};
31 for (int count = 1; count < (*argc); ++count) {
32 if ((*argv)[count] and strlen((*argv)[count]) != 0) {
33 // undefined commandline argument specified, we speculate the user may do
34 // not know the command usage. Hence print the usage help and continue
35 printTip = true;
36 std::cout << "CommandHelper WARN: argument " << (*argv)[count]
37 << " specified but unused" << std::endl;
38 }
39 }
40 if (printTip) {
41 std::cout << tipHelp_ << std::endl;
42 }
43 return 0;
44 }
45 DoHelp(help);
46 return -1;
47 }
48
DoHelp(const std::string & help)49 void CommandHelper::DoHelp(const std::string& help)
50 {
51 if (help.compare(SUPPORTED_ARGS[ARG_HELP]) == 0) {
52 std::cout << helpHelp_ << std::endl;
53 } else if (help.compare(SUPPORTED_ARGS[ARG_OUTPUT_FILE]) == 0) {
54 std::cout << outputFileHelp_ << std::endl;
55 } else if (help.compare(SUPPORTED_ARGS[ARG_EXCLUDE_TRACER]) == 0) {
56 std::cout << excludeTracerHelp_ << std::endl;
57 } else if (help.compare(SUPPORTED_ARGS[ARG_MAX_STACK_DEPTH]) == 0) {
58 std::cout << maxStackDepthHelp_ << std::endl;
59 } else if (help.compare(SUPPORTED_ARGS[ARG_DURATION]) == 0) {
60 std::cout << durationHelp_ << std::endl;
61 } else if (help.compare(SUPPORTED_ARGS[ARG_EVENTS]) == 0) {
62 std::cout << eventsHelp_ << std::endl;
63 } else if (help.compare(SUPPORTED_ARGS[ARG_PIDS]) == 0) {
64 std::cout << pidsHelp_ << std::endl;
65 } else if (help.compare(SUPPORTED_ARGS[ARG_DUMP_EVENTS]) == 0) {
66 std::cout << dumpEventsHelp_ << std::endl;
67 } else if (help.compare(SUPPORTED_ARGS[ARG_UNWIND_STACK]) == 0) {
68 std::cout << unwindStackHelp_ << std::endl;
69 } else if (help.compare(SUPPORTED_ARGS[ARG_BPF_LOG_LEVEL]) == 0) {
70 std::cout << bpfLogLevelHelp_ << std::endl;
71 } else if (help.compare(SUPPORTED_ARGS[ARG_BPF_LOG_FILE]) == 0) {
72 std::cout << bpfLogFileHelp_ << std::endl;
73 } else if (help.compare(SUPPORTED_ARGS[ARG_LIBBPF_LOG_LEVEL]) == 0) {
74 std::cout << libbpfLogLevelHelp_ << std::endl;
75 } else if (help.compare(SUPPORTED_ARGS[ARG_LIBBPF_LOG_FILE]) == 0) {
76 std::cout << libbpfFileHelp_ << std::endl;
77 } else if (help.compare(SUPPORTED_ARGS[ARG_SERVER_START]) == 0) {
78 std::cout << serverStartHelp_ << std::endl;
79 } else if (help.compare(SUPPORTED_ARGS[ARG_SERVER_STOP]) == 0) {
80 std::cout << serverStopHelp_ << std::endl;
81 } else if (help.compare(SUPPORTED_ARGS[ARG_HHLOG_LEVEL]) == 0) {
82 std::cout << hhLogLevelHelp_ << std::endl;
83 } else if (help.compare(SUPPORTED_ARGS[ARG_HHLOG_FILE]) == 0) {
84 std::cout << hhLogFileHelp_ << std::endl;
85 } else {
86 std::cout << tipHelp_ << std::endl;
87 }
88 return;
89 }