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 <iostream>
20 #include <securec.h>
21 #include <unistd.h>
22
23 #include "dfx_define.h"
24 #include "dfx_log.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 std::cout << DUMP_STACK_TAG_USAGE << std::endl;
38 std::cout << "please use dumpcatcher" << std::endl;
39 }
40
ParseParameters(int argc,char * argv[],bool & isSignalHdlr)41 static bool ParseParameters(int argc, char *argv[], bool &isSignalHdlr)
42 {
43 if (argc <= DUMP_ARG_ONE) {
44 return false;
45 }
46 DFXLOG_DEBUG("argc: %d, argv1: %s", argc, argv[1]);
47
48 if (!strcmp("-signalhandler", argv[DUMP_ARG_ONE])) {
49 isSignalHdlr = true;
50 return true;
51 }
52 return false;
53 }
54
main(int argc,char * argv[])55 int main(int argc, char *argv[])
56 {
57 DFXLOG_INFO("%s", "Start main function of processdump");
58 #if defined(DEBUG_CRASH_LOCAL_HANDLER)
59 DFX_InstallLocalSignalHandler();
60 #endif
61 if (signal(SIGCHLD, SIG_IGN) == SIG_ERR) {
62 DFXLOG_ERROR("%s", "Processdump ignore SIGCHLD failed.");
63 }
64
65 bool isSignalHdlr = false;
66
67 alarm(PROCESSDUMP_TIMEOUT);
68 DFXLOG_INFO("Start alarm %d seconds for processdump", PROCESSDUMP_TIMEOUT);
69 setsid();
70
71 if (!ParseParameters(argc, argv, isSignalHdlr)) {
72 PrintCommandHelp();
73 return 0;
74 }
75
76 if (isSignalHdlr) {
77 OHOS::HiviewDFX::ProcessDumper::GetInstance().Dump();
78 }
79 #ifndef CLANG_COVERAGE
80 _exit(0);
81 #endif
82 return 0;
83 }
84