148f512ceSopenharmony_ci/*
248f512ceSopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
348f512ceSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
448f512ceSopenharmony_ci * you may not use this file except in compliance with the License.
548f512ceSopenharmony_ci * You may obtain a copy of the License at
648f512ceSopenharmony_ci *
748f512ceSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
848f512ceSopenharmony_ci *
948f512ceSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1048f512ceSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1148f512ceSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1248f512ceSopenharmony_ci * See the License for the specific language governing permissions and
1348f512ceSopenharmony_ci * limitations under the License.
1448f512ceSopenharmony_ci */
1548f512ceSopenharmony_ci
1648f512ceSopenharmony_ci#include <cstdio>
1748f512ceSopenharmony_ci#include <iostream>
1848f512ceSopenharmony_ci
1948f512ceSopenharmony_ci#include "command.h"
2048f512ceSopenharmony_ci#include "debug_logger.h"
2148f512ceSopenharmony_ci#if defined(is_ohos) && is_ohos
2248f512ceSopenharmony_ci#include "hiperf_hilog.h"
2348f512ceSopenharmony_ci#endif
2448f512ceSopenharmony_ci#include "option_debug.h"
2548f512ceSopenharmony_ci#include "subcommand.h"
2648f512ceSopenharmony_ci#include "subcommand_help.h"
2748f512ceSopenharmony_ci#include "utilities.h"
2848f512ceSopenharmony_ci#if SUPPORT_PERF_EVENT
2948f512ceSopenharmony_ci#include "subcommand_list.h"
3048f512ceSopenharmony_ci#include "subcommand_record.h"
3148f512ceSopenharmony_ci#include "subcommand_stat.h"
3248f512ceSopenharmony_ci#endif
3348f512ceSopenharmony_ci#include "subcommand_dump.h"
3448f512ceSopenharmony_ci#include "subcommand_report.h"
3548f512ceSopenharmony_ci
3648f512ceSopenharmony_ciusing namespace std;
3748f512ceSopenharmony_ciusing namespace OHOS::Developtools::HiPerf;
3848f512ceSopenharmony_ci
3948f512ceSopenharmony_ci#ifdef FUZZER_TEST
4048f512ceSopenharmony_ci#define main HiperfFuzzerMain
4148f512ceSopenharmony_ci#endif
4248f512ceSopenharmony_ci
4348f512ceSopenharmony_ciint main(const int argc, const char *argv[])
4448f512ceSopenharmony_ci{
4548f512ceSopenharmony_ci    if (!GetDeveloperMode() && !IsAllowProfilingUid()) {
4648f512ceSopenharmony_ci        printf("error: not in developermode, exit.\n");
4748f512ceSopenharmony_ci        return -1;
4848f512ceSopenharmony_ci    }
4948f512ceSopenharmony_ci
5048f512ceSopenharmony_ci    if (argc < 0 || argc > 128) { // 128 : max input argument counts
5148f512ceSopenharmony_ci        printf("The number of input arguments exceeds the upper limit.\n");
5248f512ceSopenharmony_ci        return -1;
5348f512ceSopenharmony_ci    }
5448f512ceSopenharmony_ci    std::ios::sync_with_stdio(false);
5548f512ceSopenharmony_ci    cin.tie(nullptr);
5648f512ceSopenharmony_ci
5748f512ceSopenharmony_ci#if defined(is_ohos) && is_ohos
5848f512ceSopenharmony_ci    if (IsRoot() && setgid(2000) != 0) { // 2000 is shell group
5948f512ceSopenharmony_ci        printf("setgid failed errno: %d.\n", errno);
6048f512ceSopenharmony_ci    }
6148f512ceSopenharmony_ci    WriteStringToFile("/proc/self/oom_score_adj", "0");
6248f512ceSopenharmony_ci    if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) {
6348f512ceSopenharmony_ci        HIPERF_HILOGI(MODULE_DEFAULT, "ignore SIGPIPE failed.");
6448f512ceSopenharmony_ci    }
6548f512ceSopenharmony_ci    HIPERF_HILOGI(MODULE_DEFAULT, "hiperf start.");
6648f512ceSopenharmony_ci#endif
6748f512ceSopenharmony_ci
6848f512ceSopenharmony_ci    // pass the argv to next
6948f512ceSopenharmony_ci    vector<string> args;
7048f512ceSopenharmony_ci    for (int i = 1; i < argc; i++) {
7148f512ceSopenharmony_ci        args.push_back(argv[i]);
7248f512ceSopenharmony_ci    }
7348f512ceSopenharmony_ci
7448f512ceSopenharmony_ci    if (args.empty()) {
7548f512ceSopenharmony_ci        printf("no command input.\n");
7648f512ceSopenharmony_ci        args.push_back("help"); // no cmd like help cmd
7748f512ceSopenharmony_ci    }
7848f512ceSopenharmony_ci
7948f512ceSopenharmony_ci// register all the main command
8048f512ceSopenharmony_ci#ifdef HIPERF_DEBUG
8148f512ceSopenharmony_ci    RegisterMainCommandDebug();
8248f512ceSopenharmony_ci#endif
8348f512ceSopenharmony_ci
8448f512ceSopenharmony_ci    // register all the sub command
8548f512ceSopenharmony_ci    SubCommandHelp::RegisterSubCommandHelp();
8648f512ceSopenharmony_ci
8748f512ceSopenharmony_ci#if SUPPORT_PERF_EVENT
8848f512ceSopenharmony_ci    RegisterSubCommandStat();
8948f512ceSopenharmony_ci    SubCommandList::RegisterSubCommandList();
9048f512ceSopenharmony_ci    SubCommandRecord::RegisterSubCommandRecord();
9148f512ceSopenharmony_ci#endif
9248f512ceSopenharmony_ci
9348f512ceSopenharmony_ci    SubCommandDump::RegisterSubCommandDump();
9448f512ceSopenharmony_ci    SubCommandReport::RegisterSubCommandReport();
9548f512ceSopenharmony_ci#ifdef FUZZER_TEST
9648f512ceSopenharmony_ci    bool isRecordCmd = false;
9748f512ceSopenharmony_ci    if (args[0] == "record") {
9848f512ceSopenharmony_ci        isRecordCmd = true;
9948f512ceSopenharmony_ci    }
10048f512ceSopenharmony_ci#endif
10148f512ceSopenharmony_ci    // tell sub cmd to do the process
10248f512ceSopenharmony_ci    Command::DispatchCommands(args);
10348f512ceSopenharmony_ci
10448f512ceSopenharmony_ci#ifdef FUZZER_TEST
10548f512ceSopenharmony_ci    SubCommand::ClearSubCommands();
10648f512ceSopenharmony_ci    Option::ClearMainOptions();
10748f512ceSopenharmony_ci
10848f512ceSopenharmony_ci    if (isRecordCmd) {
10948f512ceSopenharmony_ci        const uint64_t msWaitSysReleaseThread = 100;
11048f512ceSopenharmony_ci        std::this_thread::sleep_for(std::chrono::milliseconds(msWaitSysReleaseThread));
11148f512ceSopenharmony_ci    }
11248f512ceSopenharmony_ci#endif
11348f512ceSopenharmony_ci
11448f512ceSopenharmony_ci    HLOGD("normal exit.");
11548f512ceSopenharmony_ci    return 0;
11648f512ceSopenharmony_ci}
117