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