1 /*
2  * Copyright (c) Huawei Technologies Co., Ltd. 2022. All rights reserved.
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 #ifndef COMMAND_HELPER_H
17 #define COMMAND_HELPER_H
18 
19 #include <string>
20 #include <array>
21 
22 class CommandHelper {
23 public:
24     ~CommandHelper() = default;
GetInstance()25     static inline CommandHelper& GetInstance()
26     {
27         static CommandHelper instance {};
28         return instance;
29     }
30 
31     int Start(int* argc, char*** argv, const std::string& help);
32 private:
33     CommandHelper() = default;
34     void DoHelp(const std::string& help);
35 
36     enum ArgNames:int {
37         ARG_NONG = 0,
38         ARG_HELP,
39         ARG_OUTPUT_FILE,
40         ARG_EXCLUDE_TRACER,
41         ARG_MAX_STACK_DEPTH,
42         ARG_DURATION,
43         ARG_EVENTS,
44         ARG_PIDS,
45         ARG_DUMP_EVENTS,
46         ARG_UNWIND_STACK,
47         ARG_BPF_LOG_LEVEL,
48         ARG_BPF_LOG_FILE,
49         ARG_LIBBPF_LOG_LEVEL,
50         ARG_LIBBPF_LOG_FILE,
51         ARG_HHLOG_LEVEL,
52         ARG_HHLOG_FILE,
53         ARG_SERVER_START,
54         ARG_SERVER_STOP,
55         NR_SUPPORTED_ARGS,
56     };
57 
58     const std::array<const std::string, NR_SUPPORTED_ARGS> SUPPORTED_ARGS {
59         "none",
60         "help",
61         "output_file",
62         "exclude_tracer",
63         "max_stack_depth",
64         "duration",
65         "events",
66         "pids",
67         "dump_events",
68         "unwind_stack",
69         "bpf_log_level",
70         "bpf_log_file",
71         "libbpf_log_level",
72         "libbpf_log_file",
73         "hhlog_level",
74         "hhlog_file",
75         "start",
76         "stop"
77     };
78 
79     const std::string tipHelp_ {
80         "Tip:\n"
81         "    I suppose you don't know the command usage, ignore this tip if you do,\n"
82         "    run 'hiebpf --help help' for help if you don't\n"};
83     const std::string helpHelp_ {
84         "command usage: hiebpf [--<arg> <val>]\n"
85         "supported arguments:\n"
86         "    --help <arg>:\n"
87         "        show help message of the specified argument, '--help help' shows the\n"
88         "        current message\n"
89         "    --output_file <filepath>:\n"
90         "        the file used to save hiebpf data. default: /data/local/tmp/hiebpf.data\n"
91         "    --exclude_tracer <true|false>:\n"
92         "        controlls exclude to trace the command itself. default: true\n"
93         "    --dump_events <val>:\n"
94         "        maxinum number of events to print to console. default:0\n"
95         "    --unwind_stack <true|false>:\n"
96         "        whether enable unwinding or not. default:true.\n"
97         "    --max_stack_depth <val>:\n"
98         "        max stack depth to unwind\n"
99         "    --duration <secs>:\n"
100         "        the trace will run for <sec> seconds. defalut: 0\n"
101         "    --events <event1[<,event2>]>:\n"
102         "        configure event category.\n"
103         "    --pids <pid1[<,pid2>]>:\n"
104         "        pids of target process to trace. if not be set, trace system.\n"
105         "    --bpf_log_level <level>:\n"
106         "        log level of BPF programs. default: NONE\n"
107         "    --bpf_log_file <filepath>:\n"
108         "        the file used to save bpf logs. default: /data/local/tmp/bpf.log\n"
109         "    --libbpf_log_level <level>:\n"
110         "        level of libbpf's internal log. default:NONE\n"
111         "    --libbpf_log_file <filepath>:\n"
112         "        the file used to save libbpf logs. default: /data/local/tmp/libbpf.log\n"
113         "    --hhlog_level <level>:\n"
114         "        lowest level of HHLog to print\n"
115         "    --hhlog_file <filepath>:\n"
116         "        the file used to save HHLog. default: /data/local/tmp/hhlog.txt\n"
117         "    --start <true|false>:\n"
118         "        whether start hiebpf server. default: false\n"
119         "    --stop <true|false>:\n"
120         "        whether stop hiebpf server. default: false\n"
121     };
122     const std::string outputFileHelp_ {
123         "argument usage:\n"
124         "    --output_file <filepath>: the file used to save hiebpf data. default:/data/local/tmp/hiebpf.data\n"};
125     const std::string excludeTracerHelp_ {
126         "argument usage:\n"
127         "    --exclude_tracer <true|false>: exclude to trace the command itself. default: true\n"};
128     const std::string maxStackDepthHelp_ {
129         "argument usage:\n"
130         "    --max_stack_depth <depth>: max unwinding stack depth. default:\n"};
131     const std::string durationHelp_ {
132         "argument usage:\n"
133         "    --duration <secs>: the tracer will run for <duration> seconds. default:0s, maxinum number:3600s"};
134     const std::string eventsHelp_ {
135         "argument usage:\n"
136         "    --events <EVENT>: target events to trace, multiple events are seprated by comma\n"
137         "supported <EVENT>:\n"
138         "    fs        - all events of file system\n"
139         "    fs:open   - open events of file system\n"
140         "    fs:close  - close events of file system\n"
141         "    fs:read   - read events of file system\n"
142         "    fs:write  - write events of file system\n"
143         "    ptrace    - page fault events\n"
144         "    bio       - all BIO events\n"};
145     const std::string pidsHelp_ {
146         "argument usage:\n"
147         "    --pids <pid1[<,pid2>]>: pids of target process to trace. if not be set, trace system.\n"};
148     const std::string dumpEventsHelp_ {
149         "argument usage:\n"
150         "    --dump_events <maxinum>: maxinum number of events to print to console. default:0\n"};
151     const std::string unwindStackHelp_ {
152         "argument usage:\n"
153         "    --unwind_stack <true|false>: whether enable unwinding or not. default: true\n"};
154     const std::string bpfLogLevelHelp_ {
155         "argument usage: \n"
156         "    --bpf_log_level <level>: log level of BPF programs. default:NONE\n"
157         "supported <level>:\n"
158         "    DEBUG: \n"
159         "    INFO: \n"
160         "    WARN: \n"
161         "    ERROR: \n"
162         "    FATAL: \n"};
163     const std::string bpfLogFileHelp_ {
164         "argument usage: \n"
165         "    --bpf_log_file <filepath>: the file used to save bpf logs. default:/data/local/tmp/bpf.log\n"};
166     const std::string libbpfLogLevelHelp_ {
167         "argument usage: \n"
168         "    --libbpf_log_level <level>: level of libbpf's internal log. default:NONE\n"
169         "supported <level>:\n"
170         "    DEBUG: \n"
171         "    INFO: \n"
172         "    WARN: \n"
173         "    ERROR: \n"
174         "    FATAL: \n"};
175     const std::string libbpfFileHelp_ {
176         "argument usage: \n"
177         "    --libbpf_log_file <filepath>: the file used to save bpf logs. default:/data/local/tmp/libbpf.log\n"};
178     const std::string hhLogLevelHelp_ {
179         "argument usage: \n"
180         "    --hhlog_level <level>: lowest level of HHLog to print. default:NONE\n"
181         "supported <level>:\n"
182         "    DEBUG: \n"
183         "    INFO: \n"
184         "    WARN: \n"
185         "    ERROR: \n"
186         "    FATAL: \n"};
187     const std::string hhLogFileHelp_ {
188         "argument usage: \n"
189         "    --hhlog_file <filepath>: the file used to save HHLog. default: /data/local/tmp/hhlog.txt\n"};
190     const std::string serverStartHelp_ {
191         "argument usage:\n"
192         "    --start <true|false>: whether start hiebpf server. default: false\n"};
193     const std::string serverStopHelp_ {
194         "argument usage:\n"
195         "    --stop <true|false>: whether stop hiebpf server. default: false\n"};
196 };
197 
198 #endif
199