1 /*
2  * Copyright (c) 2021 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 <cstdio>
17 #include <iostream>
18 
19 #include "command.h"
20 #include "debug_logger.h"
21 #if defined(is_ohos) && is_ohos
22 #include "hiperf_hilog.h"
23 #endif
24 #include "option_debug.h"
25 #include "subcommand.h"
26 #include "subcommand_help.h"
27 #include "utilities.h"
28 #if SUPPORT_PERF_EVENT
29 #include "subcommand_list.h"
30 #include "subcommand_record.h"
31 #include "subcommand_stat.h"
32 #endif
33 #include "subcommand_dump.h"
34 #include "subcommand_report.h"
35 
36 using namespace std;
37 using namespace OHOS::Developtools::HiPerf;
38 
39 #ifdef FUZZER_TEST
40 #define main HiperfFuzzerMain
41 #endif
42 
main(const int argc, const char *argv[])43 int main(const int argc, const char *argv[])
44 {
45     if (!GetDeveloperMode() && !IsAllowProfilingUid()) {
46         printf("error: not in developermode, exit.\n");
47         return -1;
48     }
49 
50     if (argc < 0 || argc > 128) { // 128 : max input argument counts
51         printf("The number of input arguments exceeds the upper limit.\n");
52         return -1;
53     }
54     std::ios::sync_with_stdio(false);
55     cin.tie(nullptr);
56 
57 #if defined(is_ohos) && is_ohos
58     if (IsRoot() && setgid(2000) != 0) { // 2000 is shell group
59         printf("setgid failed errno: %d.\n", errno);
60     }
61     WriteStringToFile("/proc/self/oom_score_adj", "0");
62     if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) {
63         HIPERF_HILOGI(MODULE_DEFAULT, "ignore SIGPIPE failed.");
64     }
65     HIPERF_HILOGI(MODULE_DEFAULT, "hiperf start.");
66 #endif
67 
68     // pass the argv to next
69     vector<string> args;
70     for (int i = 1; i < argc; i++) {
71         args.push_back(argv[i]);
72     }
73 
74     if (args.empty()) {
75         printf("no command input.\n");
76         args.push_back("help"); // no cmd like help cmd
77     }
78 
79 // register all the main command
80 #ifdef HIPERF_DEBUG
81     RegisterMainCommandDebug();
82 #endif
83 
84     // register all the sub command
85     SubCommandHelp::RegisterSubCommandHelp();
86 
87 #if SUPPORT_PERF_EVENT
88     RegisterSubCommandStat();
89     SubCommandList::RegisterSubCommandList();
90     SubCommandRecord::RegisterSubCommandRecord();
91 #endif
92 
93     SubCommandDump::RegisterSubCommandDump();
94     SubCommandReport::RegisterSubCommandReport();
95 #ifdef FUZZER_TEST
96     bool isRecordCmd = false;
97     if (args[0] == "record") {
98         isRecordCmd = true;
99     }
100 #endif
101     // tell sub cmd to do the process
102     Command::DispatchCommands(args);
103 
104 #ifdef FUZZER_TEST
105     SubCommand::ClearSubCommands();
106     Option::ClearMainOptions();
107 
108     if (isRecordCmd) {
109         const uint64_t msWaitSysReleaseThread = 100;
110         std::this_thread::sleep_for(std::chrono::milliseconds(msWaitSysReleaseThread));
111     }
112 #endif
113 
114     HLOGD("normal exit.");
115     return 0;
116 }
117