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 "process_dumper.h"
25 
26 #if defined(DEBUG_CRASH_LOCAL_HANDLER)
27 #include "dfx_signal_local_handler.h"
28 #endif
29 
30 static const int DUMP_ARG_ONE = 1;
31 static const std::string DUMP_STACK_TAG_USAGE = "usage:";
32 static const std::string DUMP_STACK_TAG_FAILED = "failed:";
33 
PrintCommandHelp()34 static void PrintCommandHelp()
35 {
36     printf("%s\nplease use dumpcatcher\n", DUMP_STACK_TAG_USAGE.c_str());
37 }
38 
ParseParameters(int argc, char *argv[], bool &isSignalHdlr)39 static bool ParseParameters(int argc, char *argv[], bool &isSignalHdlr)
40 {
41     if (argc <= DUMP_ARG_ONE) {
42         return false;
43     }
44     DFXLOGD("[%{public}d]: argc: %{public}d, argv1: %{public}s", __LINE__, argc, argv[1]);
45 
46     if (!strcmp("-signalhandler", argv[DUMP_ARG_ONE])) {
47         isSignalHdlr = true;
48         return true;
49     }
50     return false;
51 }
52 
main(int argc, char *argv[])53 int main(int argc, char *argv[])
54 {
55     DFXLOGW("Start main function of processdump");
56 #if defined(DEBUG_CRASH_LOCAL_HANDLER)
57     DFX_InstallLocalSignalHandler();
58 #endif
59     if (signal(SIGCHLD, SIG_IGN) == SIG_ERR) {
60         DFXLOGE("Processdump ignore SIGCHLD failed.");
61     }
62 
63     bool isSignalHdlr = false;
64 
65     alarm(PROCESSDUMP_TIMEOUT);
66     setsid();
67 
68     if (!ParseParameters(argc, argv, isSignalHdlr)) {
69         PrintCommandHelp();
70         return 0;
71     }
72 
73     if (isSignalHdlr) {
74         OHOS::HiviewDFX::ProcessDumper::GetInstance().Dump();
75     }
76 #ifndef CLANG_COVERAGE
77     _exit(0);
78 #endif
79     return 0;
80 }
81