1 /*
2 * Copyright (c) 2021-2022 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 /* This files contains process dump entry function. */
17
18 #include <cstdio>
19 #include <cstdlib>
20 #include <cstring>
21 #include <iostream>
22 #include <securec.h>
23 #include <sys/socket.h>
24 #include <sys/un.h>
25 #include <unistd.h>
26 #include "dfx_config.h"
27 #include "dfx_define.h"
28 #include "dfx_dump_writer.h"
29 #include "dfx_logger.h"
30 #include "directory_ex.h"
31 #include "faultloggerd_client.h"
32 #include "file_ex.h"
33 #include "process_dumper.h"
34
35 #if defined(DEBUG_CRASH_LOCAL_HANDLER)
36 #include "dfx_signal_local_handler.h"
37 #include "dfx_cutil.h"
38 #endif
39
40 static const int DUMP_ARG_ONE = 1;
41 static const std::string DUMP_STACK_TAG_USAGE = "usage:";
42 static const std::string DUMP_STACK_TAG_FAILED = "failed:";
43
PrintCommandHelp()44 static void PrintCommandHelp()
45 {
46 std::cout << DUMP_STACK_TAG_USAGE << std::endl;
47 std::cout << "please use dumpcatcher" << std::endl;
48 }
49
ParseParameters(int argc,char * argv[],bool & isSignalHdlr)50 static bool ParseParameters(int argc, char *argv[], bool &isSignalHdlr)
51 {
52 if (argc <= DUMP_ARG_ONE) {
53 return false;
54 }
55 DfxLogDebug("argc: %d, argv1: %s", argc, argv[1]);
56
57 if (!strcmp("-signalhandler", argv[DUMP_ARG_ONE])) {
58 isSignalHdlr = true;
59 return true;
60 }
61 return false;
62 }
63
main(int argc,char * argv[])64 int main(int argc, char *argv[])
65 {
66 #if defined(DEBUG_CRASH_LOCAL_HANDLER)
67 DFX_InstallLocalSignalHandler();
68 #endif
69 if (signal(SIGCHLD, SIG_IGN) == SIG_ERR) {
70 DfxLogError("Processdump ignore SIGCHLD failed.");
71 }
72
73 bool isSignalHdlr = false;
74
75 alarm(PROCESSDUMP_TIMEOUT); // wait 30s for process dump done
76 setsid();
77
78 if (!ParseParameters(argc, argv, isSignalHdlr)) {
79 PrintCommandHelp();
80 return 0;
81 }
82
83 if (isSignalHdlr) {
84 OHOS::HiviewDFX::DfxConfig::GetInstance().ReadConfig();
85 OHOS::HiviewDFX::ProcessDumper::GetInstance().Dump();
86 }
87 return 0;
88 }
89