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 #include "option_debug.h" 22 #include "subcommand.h" 23 #include "subcommand_help.h" 24 #include "utilities.h" 25 #if SUPPORT_PERF_EVENT 26 #include "subcommand_list.h" 27 #include "subcommand_record.h" 28 #include "subcommand_stat.h" 29 #endif 30 #include "subcommand_dump.h" 31 #include "subcommand_report.h" 32 33 using namespace std; 34 using namespace OHOS::Developtools::HiPerf; 35 36 #ifdef FUZZER_TEST 37 #define main HiperfFuzzerMain 38 #endif 39 main(const int argc,const char * argv[])40int main(const int argc, const char *argv[]) 41 { 42 std::ios::sync_with_stdio(false); 43 cin.tie(nullptr); 44 45 #if defined(is_ohos) && is_ohos 46 if (IsRoot() && setgid(2000) != 0) { // 2000 is shell group 47 printf("setgid failed errno: %d.\n", errno); 48 } 49 #endif 50 51 // pass the argv to next 52 vector<string> args; 53 for (int i = 1; i < argc; i++) { 54 args.push_back(argv[i]); 55 } 56 57 if (args.empty()) { 58 printf("no command input.\n"); 59 args.push_back("help"); // no cmd like help cmd 60 } 61 62 // register all the main command 63 #ifdef HIPERF_DEBUG 64 RegisterMainCommandDebug(); 65 #endif 66 67 // register all the sub command 68 SubCommandHelp::RegisterSubCommandHelp(); 69 70 #if SUPPORT_PERF_EVENT 71 RegisterSubCommandStat(); 72 SubCommandList::RegisterSubCommandList(); 73 SubCommandRecord::RegisterSubCommandRecord(); 74 #endif 75 76 SubCommandDump::RegisterSubCommandDump(); 77 SubCommandReport::RegisterSubCommandReport(); 78 #ifdef FUZZER_TEST 79 bool isRecordCmd = false; 80 if (args[0] == "record") { 81 isRecordCmd = true; 82 } 83 #endif 84 // tell sub cmd to do the process 85 Command::DispatchCommands(args); 86 87 #ifdef FUZZER_TEST 88 SubCommand::ClearSubCommands(); 89 Option::ClearMainOptions(); 90 91 if (isRecordCmd) { 92 const uint64_t msWaitSysReleaseThread = 100; 93 std::this_thread::sleep_for(std::chrono::milliseconds(msWaitSysReleaseThread)); 94 } 95 #endif 96 97 HLOGD("normal exit."); 98 return 0; 99 } 100