• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2024 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 <cstdlib>
18 #include <cstring>
19 #include <securec.h>
20 #include <unistd.h>
21 
22 #include "dfx_define.h"
23 #include "dfx_log.h"
24 #include "lite_perf_dumper.h"
25 #include "process_dumper.h"
26 
27 #if defined(DEBUG_CRASH_LOCAL_HANDLER)
28 #include "dfx_signal_local_handler.h"
29 #endif
30 
31 static const int DUMP_ARG_ONE = 1;
32 static const std::string DUMP_STACK_TAG_USAGE = "usage:";
33 static const std::string DUMP_STACK_TAG_FAILED = "failed:";
34 
PrintCommandHelp()35 static void PrintCommandHelp()
36 {
37     printf("%s\nplease use dumpcatcher\n", DUMP_STACK_TAG_USAGE.c_str());
38 }
39 
ParseParameters(int argc,char * argv[],bool & isSignalHdlr,bool & isLitePerf)40 static bool ParseParameters(int argc, char *argv[], bool &isSignalHdlr, bool &isLitePerf)
41 {
42     if (argc <= DUMP_ARG_ONE) {
43         return false;
44     }
45     DFXLOGD("[%{public}d]: argc: %{public}d, argv1: %{public}s", __LINE__, argc, argv[1]);
46 
47     if (!strcmp("-signalhandler", argv[DUMP_ARG_ONE])) {
48         isSignalHdlr = true;
49         return true;
50     } else if (!strcmp("-liteperf", argv[DUMP_ARG_ONE])) {
51         isLitePerf = true;
52         return true;
53     }
54     return false;
55 }
56 
main(int argc,char * argv[])57 int main(int argc, char *argv[])
58 {
59     DFXLOGI("Start main function of processdump");
60 #if defined(DEBUG_CRASH_LOCAL_HANDLER) && !defined(DFX_ALLOCATE_ASAN)
61     DFX_InstallLocalSignalHandler();
62 #endif
63     if (signal(SIGCHLD, SIG_IGN) == SIG_ERR) {
64         DFXLOGE("Processdump ignore SIGCHLD failed.");
65     }
66 
67     bool isSignalHdlr = false;
68     bool isLitePerf = false;
69 
70     setsid();
71 
72     if (!ParseParameters(argc, argv, isSignalHdlr, isLitePerf)) {
73         PrintCommandHelp();
74         return 0;
75     }
76 
77     if (isSignalHdlr) {
78         alarm(PROCESSDUMP_TIMEOUT);
79         OHOS::HiviewDFX::ProcessDumper::GetInstance().Dump();
80     } else if (isLitePerf) {
81 #ifdef DFX_ENABLE_LPERF
82         OHOS::HiviewDFX::LitePerfDumper::GetInstance().Perf();
83 #endif
84     }
85 #ifndef CLANG_COVERAGE
86     _exit(0);
87 #endif
88     return 0;
89 }
90