1800b99b8Sopenharmony_ci/* 2800b99b8Sopenharmony_ci * Copyright (c) 2021-2024 Huawei Device Co., Ltd. 3800b99b8Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4800b99b8Sopenharmony_ci * you may not use this file except in compliance with the License. 5800b99b8Sopenharmony_ci * You may obtain a copy of the License at 6800b99b8Sopenharmony_ci * 7800b99b8Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8800b99b8Sopenharmony_ci * 9800b99b8Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10800b99b8Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11800b99b8Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12800b99b8Sopenharmony_ci * See the License for the specific language governing permissions and 13800b99b8Sopenharmony_ci * limitations under the License. 14800b99b8Sopenharmony_ci */ 15800b99b8Sopenharmony_ci 16800b99b8Sopenharmony_ci#include <cstdio> 17800b99b8Sopenharmony_ci#include <cstdlib> 18800b99b8Sopenharmony_ci#include <cstring> 19800b99b8Sopenharmony_ci#include <securec.h> 20800b99b8Sopenharmony_ci#include <unistd.h> 21800b99b8Sopenharmony_ci 22800b99b8Sopenharmony_ci#include "dfx_define.h" 23800b99b8Sopenharmony_ci#include "dfx_log.h" 24800b99b8Sopenharmony_ci#include "process_dumper.h" 25800b99b8Sopenharmony_ci 26800b99b8Sopenharmony_ci#if defined(DEBUG_CRASH_LOCAL_HANDLER) 27800b99b8Sopenharmony_ci#include "dfx_signal_local_handler.h" 28800b99b8Sopenharmony_ci#endif 29800b99b8Sopenharmony_ci 30800b99b8Sopenharmony_cistatic const int DUMP_ARG_ONE = 1; 31800b99b8Sopenharmony_cistatic const std::string DUMP_STACK_TAG_USAGE = "usage:"; 32800b99b8Sopenharmony_cistatic const std::string DUMP_STACK_TAG_FAILED = "failed:"; 33800b99b8Sopenharmony_ci 34800b99b8Sopenharmony_cistatic void PrintCommandHelp() 35800b99b8Sopenharmony_ci{ 36800b99b8Sopenharmony_ci printf("%s\nplease use dumpcatcher\n", DUMP_STACK_TAG_USAGE.c_str()); 37800b99b8Sopenharmony_ci} 38800b99b8Sopenharmony_ci 39800b99b8Sopenharmony_cistatic bool ParseParameters(int argc, char *argv[], bool &isSignalHdlr) 40800b99b8Sopenharmony_ci{ 41800b99b8Sopenharmony_ci if (argc <= DUMP_ARG_ONE) { 42800b99b8Sopenharmony_ci return false; 43800b99b8Sopenharmony_ci } 44800b99b8Sopenharmony_ci DFXLOGD("[%{public}d]: argc: %{public}d, argv1: %{public}s", __LINE__, argc, argv[1]); 45800b99b8Sopenharmony_ci 46800b99b8Sopenharmony_ci if (!strcmp("-signalhandler", argv[DUMP_ARG_ONE])) { 47800b99b8Sopenharmony_ci isSignalHdlr = true; 48800b99b8Sopenharmony_ci return true; 49800b99b8Sopenharmony_ci } 50800b99b8Sopenharmony_ci return false; 51800b99b8Sopenharmony_ci} 52800b99b8Sopenharmony_ci 53800b99b8Sopenharmony_ciint main(int argc, char *argv[]) 54800b99b8Sopenharmony_ci{ 55800b99b8Sopenharmony_ci DFXLOGW("Start main function of processdump"); 56800b99b8Sopenharmony_ci#if defined(DEBUG_CRASH_LOCAL_HANDLER) 57800b99b8Sopenharmony_ci DFX_InstallLocalSignalHandler(); 58800b99b8Sopenharmony_ci#endif 59800b99b8Sopenharmony_ci if (signal(SIGCHLD, SIG_IGN) == SIG_ERR) { 60800b99b8Sopenharmony_ci DFXLOGE("Processdump ignore SIGCHLD failed."); 61800b99b8Sopenharmony_ci } 62800b99b8Sopenharmony_ci 63800b99b8Sopenharmony_ci bool isSignalHdlr = false; 64800b99b8Sopenharmony_ci 65800b99b8Sopenharmony_ci alarm(PROCESSDUMP_TIMEOUT); 66800b99b8Sopenharmony_ci setsid(); 67800b99b8Sopenharmony_ci 68800b99b8Sopenharmony_ci if (!ParseParameters(argc, argv, isSignalHdlr)) { 69800b99b8Sopenharmony_ci PrintCommandHelp(); 70800b99b8Sopenharmony_ci return 0; 71800b99b8Sopenharmony_ci } 72800b99b8Sopenharmony_ci 73800b99b8Sopenharmony_ci if (isSignalHdlr) { 74800b99b8Sopenharmony_ci OHOS::HiviewDFX::ProcessDumper::GetInstance().Dump(); 75800b99b8Sopenharmony_ci } 76800b99b8Sopenharmony_ci#ifndef CLANG_COVERAGE 77800b99b8Sopenharmony_ci _exit(0); 78800b99b8Sopenharmony_ci#endif 79800b99b8Sopenharmony_ci return 0; 80800b99b8Sopenharmony_ci} 81