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 HIEBPF_COMMAND_H
1706f6ba60Sopenharmony_ci#define HIEBPF_COMMAND_H
1806f6ba60Sopenharmony_ci
1906f6ba60Sopenharmony_ci#include <csignal>
2006f6ba60Sopenharmony_ci#include <fstream>
2106f6ba60Sopenharmony_ci#include <queue>
2206f6ba60Sopenharmony_ci#include <string>
2306f6ba60Sopenharmony_ci#include <vector>
2406f6ba60Sopenharmony_ci
2506f6ba60Sopenharmony_ci#include "bpf_controller.h"
2606f6ba60Sopenharmony_ci#include "elf_symbol_info.h"
2706f6ba60Sopenharmony_ci#include "maps_info.h"
2806f6ba60Sopenharmony_ci#include "ring_buffer.h"
2906f6ba60Sopenharmony_ci#include "hiebpf_common.h"
3006f6ba60Sopenharmony_ciclass Command {
3106f6ba60Sopenharmony_cipublic:
3206f6ba60Sopenharmony_ci    bool DispatchCommand(std::vector<std::string> args);
3306f6ba60Sopenharmony_ci    Command();
3406f6ba60Sopenharmony_ci    void DoWork();
3506f6ba60Sopenharmony_ci
3606f6ba60Sopenharmony_ciprivate:
3706f6ba60Sopenharmony_ci    bool ParseArgs(std::vector<std::string> arguments);
3806f6ba60Sopenharmony_ci    int GetClockId(const std::string &str)
3906f6ba60Sopenharmony_ci    {
4006f6ba60Sopenharmony_ci        if (str == "realtime") {
4106f6ba60Sopenharmony_ci            return CLOCK_TYPE::RealTime;
4206f6ba60Sopenharmony_ci        } else if (str == "boottime") {
4306f6ba60Sopenharmony_ci            return CLOCK_TYPE::BootTime;
4406f6ba60Sopenharmony_ci        } else if (str == "monotonic") {
4506f6ba60Sopenharmony_ci            return CLOCK_TYPE::Monotonic;
4606f6ba60Sopenharmony_ci        } else if (str == "monotonicraw") {
4706f6ba60Sopenharmony_ci            return CLOCK_TYPE::MonotonicRaw;
4806f6ba60Sopenharmony_ci        }
4906f6ba60Sopenharmony_ci        return -1;
5006f6ba60Sopenharmony_ci    }
5106f6ba60Sopenharmony_ci
5206f6ba60Sopenharmony_ci    void PrintHelp();
5306f6ba60Sopenharmony_ci    static int EventReceiver(BPFEvent* bpfEvent);
5406f6ba60Sopenharmony_ci
5506f6ba60Sopenharmony_ci    void WriteHeader();
5606f6ba60Sopenharmony_ci    void WriteEventMaps(uint32_t pid);
5706f6ba60Sopenharmony_ci    void WriteSymbolInfo(const std::string &fileName);
5806f6ba60Sopenharmony_ci    void WriteEventFs(uint8_t* data);
5906f6ba60Sopenharmony_ci    void WriteEventMem();
6006f6ba60Sopenharmony_ciprivate:
6106f6ba60Sopenharmony_ci    bool helpFlag_ = false;
6206f6ba60Sopenharmony_ci    BPFConfig bpfConfig;
6306f6ba60Sopenharmony_ci    std::unique_ptr<BPFController> bpfControllerPtr_;
6406f6ba60Sopenharmony_ci    // event type
6506f6ba60Sopenharmony_ci    std::vector<std::string> events_;
6606f6ba60Sopenharmony_ci    int clockId_ = CLOCK_TYPE::BootTime;
6706f6ba60Sopenharmony_ci    int duration_ = 1000;
6806f6ba60Sopenharmony_ci    std::string symbolFilePath_;
6906f6ba60Sopenharmony_ci    std::string symbolFileName_;
7006f6ba60Sopenharmony_ci    std::string fileName_ = "/data/local/tmp/ebpf.data";
7106f6ba60Sopenharmony_ci    std::vector<int> pids_;
7206f6ba60Sopenharmony_ci    std::string help_;
7306f6ba60Sopenharmony_ci    std::ofstream stream_ {};
7406f6ba60Sopenharmony_ci    std::string cmdLine_;
7506f6ba60Sopenharmony_ci    std::thread writeFileThread_;
7606f6ba60Sopenharmony_ci    bool ThreadRunningFlag_ = true;
7706f6ba60Sopenharmony_ci
7806f6ba60Sopenharmony_ci    OHOS::Developtools::Hiebpf::MapsInfo mapsInfo_;
7906f6ba60Sopenharmony_ci    OHOS::Developtools::Hiebpf::ElfSymbolInfo elfSymbolInfo_;
8006f6ba60Sopenharmony_ci
8106f6ba60Sopenharmony_ci    void GetArgsValues(const FSTraceEvent* bpfEvent, uint64_t* args, std::vector<ArgStr>& argStr);
8206f6ba60Sopenharmony_ci    void EventLoop();
8306f6ba60Sopenharmony_ci    bool CheckPids(std::vector<int>& pids);
8406f6ba60Sopenharmony_ci    bool SetTargetPids();
8506f6ba60Sopenharmony_ci    bool SetTargetEvents();
8606f6ba60Sopenharmony_ci};
8706f6ba60Sopenharmony_ci#endif
88