106f6ba60Sopenharmony_ci/*
206f6ba60Sopenharmony_ci * Copyright (c) Huawei Technologies Co., Ltd. 2022. All rights reserved.
306f6ba60Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
406f6ba60Sopenharmony_ci * you may not use this file except in compliance with the License.
506f6ba60Sopenharmony_ci * You may obtain a copy of the License at
606f6ba60Sopenharmony_ci *
706f6ba60Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
806f6ba60Sopenharmony_ci *
906f6ba60Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1006f6ba60Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1106f6ba60Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1206f6ba60Sopenharmony_ci * See the License for the specific language governing permissions and
1306f6ba60Sopenharmony_ci * limitations under the License.
1406f6ba60Sopenharmony_ci */
1506f6ba60Sopenharmony_ci
1606f6ba60Sopenharmony_ci#ifndef COMMAND_HELPER_H
1706f6ba60Sopenharmony_ci#define COMMAND_HELPER_H
1806f6ba60Sopenharmony_ci
1906f6ba60Sopenharmony_ci#include <string>
2006f6ba60Sopenharmony_ci#include <array>
2106f6ba60Sopenharmony_ci
2206f6ba60Sopenharmony_ciclass CommandHelper {
2306f6ba60Sopenharmony_cipublic:
2406f6ba60Sopenharmony_ci    ~CommandHelper() = default;
2506f6ba60Sopenharmony_ci    static inline CommandHelper& GetInstance()
2606f6ba60Sopenharmony_ci    {
2706f6ba60Sopenharmony_ci        static CommandHelper instance {};
2806f6ba60Sopenharmony_ci        return instance;
2906f6ba60Sopenharmony_ci    }
3006f6ba60Sopenharmony_ci
3106f6ba60Sopenharmony_ci    int Start(int* argc, char*** argv, const std::string& help);
3206f6ba60Sopenharmony_ciprivate:
3306f6ba60Sopenharmony_ci    CommandHelper() = default;
3406f6ba60Sopenharmony_ci    void DoHelp(const std::string& help);
3506f6ba60Sopenharmony_ci
3606f6ba60Sopenharmony_ci    enum ArgNames:int {
3706f6ba60Sopenharmony_ci        ARG_NONG = 0,
3806f6ba60Sopenharmony_ci        ARG_HELP,
3906f6ba60Sopenharmony_ci        ARG_OUTPUT_FILE,
4006f6ba60Sopenharmony_ci        ARG_EXCLUDE_TRACER,
4106f6ba60Sopenharmony_ci        ARG_MAX_STACK_DEPTH,
4206f6ba60Sopenharmony_ci        ARG_DURATION,
4306f6ba60Sopenharmony_ci        ARG_EVENTS,
4406f6ba60Sopenharmony_ci        ARG_PIDS,
4506f6ba60Sopenharmony_ci        ARG_DUMP_EVENTS,
4606f6ba60Sopenharmony_ci        ARG_UNWIND_STACK,
4706f6ba60Sopenharmony_ci        ARG_BPF_LOG_LEVEL,
4806f6ba60Sopenharmony_ci        ARG_BPF_LOG_FILE,
4906f6ba60Sopenharmony_ci        ARG_LIBBPF_LOG_LEVEL,
5006f6ba60Sopenharmony_ci        ARG_LIBBPF_LOG_FILE,
5106f6ba60Sopenharmony_ci        ARG_HHLOG_LEVEL,
5206f6ba60Sopenharmony_ci        ARG_HHLOG_FILE,
5306f6ba60Sopenharmony_ci        ARG_SERVER_START,
5406f6ba60Sopenharmony_ci        ARG_SERVER_STOP,
5506f6ba60Sopenharmony_ci        NR_SUPPORTED_ARGS,
5606f6ba60Sopenharmony_ci    };
5706f6ba60Sopenharmony_ci
5806f6ba60Sopenharmony_ci    const std::array<const std::string, NR_SUPPORTED_ARGS> SUPPORTED_ARGS {
5906f6ba60Sopenharmony_ci        "none",
6006f6ba60Sopenharmony_ci        "help",
6106f6ba60Sopenharmony_ci        "output_file",
6206f6ba60Sopenharmony_ci        "exclude_tracer",
6306f6ba60Sopenharmony_ci        "max_stack_depth",
6406f6ba60Sopenharmony_ci        "duration",
6506f6ba60Sopenharmony_ci        "events",
6606f6ba60Sopenharmony_ci        "pids",
6706f6ba60Sopenharmony_ci        "dump_events",
6806f6ba60Sopenharmony_ci        "unwind_stack",
6906f6ba60Sopenharmony_ci        "bpf_log_level",
7006f6ba60Sopenharmony_ci        "bpf_log_file",
7106f6ba60Sopenharmony_ci        "libbpf_log_level",
7206f6ba60Sopenharmony_ci        "libbpf_log_file",
7306f6ba60Sopenharmony_ci        "hhlog_level",
7406f6ba60Sopenharmony_ci        "hhlog_file",
7506f6ba60Sopenharmony_ci        "start",
7606f6ba60Sopenharmony_ci        "stop"
7706f6ba60Sopenharmony_ci    };
7806f6ba60Sopenharmony_ci
7906f6ba60Sopenharmony_ci    const std::string tipHelp_ {
8006f6ba60Sopenharmony_ci        "Tip:\n"
8106f6ba60Sopenharmony_ci        "    I suppose you don't know the command usage, ignore this tip if you do,\n"
8206f6ba60Sopenharmony_ci        "    run 'hiebpf --help help' for help if you don't\n"};
8306f6ba60Sopenharmony_ci    const std::string helpHelp_ {
8406f6ba60Sopenharmony_ci        "command usage: hiebpf [--<arg> <val>]\n"
8506f6ba60Sopenharmony_ci        "supported arguments:\n"
8606f6ba60Sopenharmony_ci        "    --help <arg>:\n"
8706f6ba60Sopenharmony_ci        "        show help message of the specified argument, '--help help' shows the\n"
8806f6ba60Sopenharmony_ci        "        current message\n"
8906f6ba60Sopenharmony_ci        "    --output_file <filepath>:\n"
9006f6ba60Sopenharmony_ci        "        the file used to save hiebpf data. default: /data/local/tmp/hiebpf.data\n"
9106f6ba60Sopenharmony_ci        "    --exclude_tracer <true|false>:\n"
9206f6ba60Sopenharmony_ci        "        controlls exclude to trace the command itself. default: true\n"
9306f6ba60Sopenharmony_ci        "    --dump_events <val>:\n"
9406f6ba60Sopenharmony_ci        "        maxinum number of events to print to console. default:0\n"
9506f6ba60Sopenharmony_ci        "    --unwind_stack <true|false>:\n"
9606f6ba60Sopenharmony_ci        "        whether enable unwinding or not. default:true.\n"
9706f6ba60Sopenharmony_ci        "    --max_stack_depth <val>:\n"
9806f6ba60Sopenharmony_ci        "        max stack depth to unwind\n"
9906f6ba60Sopenharmony_ci        "    --duration <secs>:\n"
10006f6ba60Sopenharmony_ci        "        the trace will run for <sec> seconds. defalut: 0\n"
10106f6ba60Sopenharmony_ci        "    --events <event1[<,event2>]>:\n"
10206f6ba60Sopenharmony_ci        "        configure event category.\n"
10306f6ba60Sopenharmony_ci        "    --pids <pid1[<,pid2>]>:\n"
10406f6ba60Sopenharmony_ci        "        pids of target process to trace. if not be set, trace system.\n"
10506f6ba60Sopenharmony_ci        "    --bpf_log_level <level>:\n"
10606f6ba60Sopenharmony_ci        "        log level of BPF programs. default: NONE\n"
10706f6ba60Sopenharmony_ci        "    --bpf_log_file <filepath>:\n"
10806f6ba60Sopenharmony_ci        "        the file used to save bpf logs. default: /data/local/tmp/bpf.log\n"
10906f6ba60Sopenharmony_ci        "    --libbpf_log_level <level>:\n"
11006f6ba60Sopenharmony_ci        "        level of libbpf's internal log. default:NONE\n"
11106f6ba60Sopenharmony_ci        "    --libbpf_log_file <filepath>:\n"
11206f6ba60Sopenharmony_ci        "        the file used to save libbpf logs. default: /data/local/tmp/libbpf.log\n"
11306f6ba60Sopenharmony_ci        "    --hhlog_level <level>:\n"
11406f6ba60Sopenharmony_ci        "        lowest level of HHLog to print\n"
11506f6ba60Sopenharmony_ci        "    --hhlog_file <filepath>:\n"
11606f6ba60Sopenharmony_ci        "        the file used to save HHLog. default: /data/local/tmp/hhlog.txt\n"
11706f6ba60Sopenharmony_ci        "    --start <true|false>:\n"
11806f6ba60Sopenharmony_ci        "        whether start hiebpf server. default: false\n"
11906f6ba60Sopenharmony_ci        "    --stop <true|false>:\n"
12006f6ba60Sopenharmony_ci        "        whether stop hiebpf server. default: false\n"
12106f6ba60Sopenharmony_ci    };
12206f6ba60Sopenharmony_ci    const std::string outputFileHelp_ {
12306f6ba60Sopenharmony_ci        "argument usage:\n"
12406f6ba60Sopenharmony_ci        "    --output_file <filepath>: the file used to save hiebpf data. default:/data/local/tmp/hiebpf.data\n"};
12506f6ba60Sopenharmony_ci    const std::string excludeTracerHelp_ {
12606f6ba60Sopenharmony_ci        "argument usage:\n"
12706f6ba60Sopenharmony_ci        "    --exclude_tracer <true|false>: exclude to trace the command itself. default: true\n"};
12806f6ba60Sopenharmony_ci    const std::string maxStackDepthHelp_ {
12906f6ba60Sopenharmony_ci        "argument usage:\n"
13006f6ba60Sopenharmony_ci        "    --max_stack_depth <depth>: max unwinding stack depth. default:\n"};
13106f6ba60Sopenharmony_ci    const std::string durationHelp_ {
13206f6ba60Sopenharmony_ci        "argument usage:\n"
13306f6ba60Sopenharmony_ci        "    --duration <secs>: the tracer will run for <duration> seconds. default:0s, maxinum number:3600s"};
13406f6ba60Sopenharmony_ci    const std::string eventsHelp_ {
13506f6ba60Sopenharmony_ci        "argument usage:\n"
13606f6ba60Sopenharmony_ci        "    --events <EVENT>: target events to trace, multiple events are seprated by comma\n"
13706f6ba60Sopenharmony_ci        "supported <EVENT>:\n"
13806f6ba60Sopenharmony_ci        "    fs        - all events of file system\n"
13906f6ba60Sopenharmony_ci        "    fs:open   - open events of file system\n"
14006f6ba60Sopenharmony_ci        "    fs:close  - close events of file system\n"
14106f6ba60Sopenharmony_ci        "    fs:read   - read events of file system\n"
14206f6ba60Sopenharmony_ci        "    fs:write  - write events of file system\n"
14306f6ba60Sopenharmony_ci        "    ptrace    - page fault events\n"
14406f6ba60Sopenharmony_ci        "    bio       - all BIO events\n"};
14506f6ba60Sopenharmony_ci    const std::string pidsHelp_ {
14606f6ba60Sopenharmony_ci        "argument usage:\n"
14706f6ba60Sopenharmony_ci        "    --pids <pid1[<,pid2>]>: pids of target process to trace. if not be set, trace system.\n"};
14806f6ba60Sopenharmony_ci    const std::string dumpEventsHelp_ {
14906f6ba60Sopenharmony_ci        "argument usage:\n"
15006f6ba60Sopenharmony_ci        "    --dump_events <maxinum>: maxinum number of events to print to console. default:0\n"};
15106f6ba60Sopenharmony_ci    const std::string unwindStackHelp_ {
15206f6ba60Sopenharmony_ci        "argument usage:\n"
15306f6ba60Sopenharmony_ci        "    --unwind_stack <true|false>: whether enable unwinding or not. default: true\n"};
15406f6ba60Sopenharmony_ci    const std::string bpfLogLevelHelp_ {
15506f6ba60Sopenharmony_ci        "argument usage: \n"
15606f6ba60Sopenharmony_ci        "    --bpf_log_level <level>: log level of BPF programs. default:NONE\n"
15706f6ba60Sopenharmony_ci        "supported <level>:\n"
15806f6ba60Sopenharmony_ci        "    DEBUG: \n"
15906f6ba60Sopenharmony_ci        "    INFO: \n"
16006f6ba60Sopenharmony_ci        "    WARN: \n"
16106f6ba60Sopenharmony_ci        "    ERROR: \n"
16206f6ba60Sopenharmony_ci        "    FATAL: \n"};
16306f6ba60Sopenharmony_ci    const std::string bpfLogFileHelp_ {
16406f6ba60Sopenharmony_ci        "argument usage: \n"
16506f6ba60Sopenharmony_ci        "    --bpf_log_file <filepath>: the file used to save bpf logs. default:/data/local/tmp/bpf.log\n"};
16606f6ba60Sopenharmony_ci    const std::string libbpfLogLevelHelp_ {
16706f6ba60Sopenharmony_ci        "argument usage: \n"
16806f6ba60Sopenharmony_ci        "    --libbpf_log_level <level>: level of libbpf's internal log. default:NONE\n"
16906f6ba60Sopenharmony_ci        "supported <level>:\n"
17006f6ba60Sopenharmony_ci        "    DEBUG: \n"
17106f6ba60Sopenharmony_ci        "    INFO: \n"
17206f6ba60Sopenharmony_ci        "    WARN: \n"
17306f6ba60Sopenharmony_ci        "    ERROR: \n"
17406f6ba60Sopenharmony_ci        "    FATAL: \n"};
17506f6ba60Sopenharmony_ci    const std::string libbpfFileHelp_ {
17606f6ba60Sopenharmony_ci        "argument usage: \n"
17706f6ba60Sopenharmony_ci        "    --libbpf_log_file <filepath>: the file used to save bpf logs. default:/data/local/tmp/libbpf.log\n"};
17806f6ba60Sopenharmony_ci    const std::string hhLogLevelHelp_ {
17906f6ba60Sopenharmony_ci        "argument usage: \n"
18006f6ba60Sopenharmony_ci        "    --hhlog_level <level>: lowest level of HHLog to print. default:NONE\n"
18106f6ba60Sopenharmony_ci        "supported <level>:\n"
18206f6ba60Sopenharmony_ci        "    DEBUG: \n"
18306f6ba60Sopenharmony_ci        "    INFO: \n"
18406f6ba60Sopenharmony_ci        "    WARN: \n"
18506f6ba60Sopenharmony_ci        "    ERROR: \n"
18606f6ba60Sopenharmony_ci        "    FATAL: \n"};
18706f6ba60Sopenharmony_ci    const std::string hhLogFileHelp_ {
18806f6ba60Sopenharmony_ci        "argument usage: \n"
18906f6ba60Sopenharmony_ci        "    --hhlog_file <filepath>: the file used to save HHLog. default: /data/local/tmp/hhlog.txt\n"};
19006f6ba60Sopenharmony_ci    const std::string serverStartHelp_ {
19106f6ba60Sopenharmony_ci        "argument usage:\n"
19206f6ba60Sopenharmony_ci        "    --start <true|false>: whether start hiebpf server. default: false\n"};
19306f6ba60Sopenharmony_ci    const std::string serverStopHelp_ {
19406f6ba60Sopenharmony_ci        "argument usage:\n"
19506f6ba60Sopenharmony_ci        "    --stop <true|false>: whether stop hiebpf server. default: false\n"};
19606f6ba60Sopenharmony_ci};
19706f6ba60Sopenharmony_ci
19806f6ba60Sopenharmony_ci#endif
199