1 /*
2 * Copyright (c) 2021-2023 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 #include <getopt.h>
23 #include "dfx_define.h"
24 #include "dfx_log.h"
25 #include "dump_catcher.h"
26
27 #if defined(DEBUG_CRASH_LOCAL_HANDLER)
28 #include "dfx_signal_local_handler.h"
29 #endif
30
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 std::cout << DUMP_STACK_TAG_USAGE << std::endl;
37 std::cout << "-p pid -t tid dump the stacktrace of the thread with given tid." << std::endl;
38 std::cout << "-p pid dump the stacktrace of all the threads with given pid." << std::endl;
39 std::cout << "[-c -m -k] optional parameter, -c(cpp) -m(mix) -k(kernel)." << std::endl;
40 }
41
PrintCommandFailed()42 static void PrintCommandFailed()
43 {
44 std::cout << DUMP_STACK_TAG_FAILED << std::endl;
45 std::cout << "pid and tid must > 0." << std::endl;
46 }
47
ParseParamters(int argc,char * argv[],int & type,int32_t & pid,int32_t & tid)48 static int ParseParamters(int argc, char *argv[], int &type, int32_t &pid, int32_t &tid)
49 {
50 int ret = 0;
51 if (argc <= 1) {
52 return ret;
53 }
54 DFXLOG_DEBUG("argc: %d, argv1: %s", argc, argv[1]);
55
56 int optRet;
57 const char *optString = "cmkp:t:";
58 while ((optRet = getopt(argc, argv, optString)) != -1) {
59 switch (optRet) {
60 case 'c':
61 if ((type != DUMP_TYPE_KERNEL) && (type != DUMP_TYPE_MIX)) {
62 type = DUMP_TYPE_NATIVE;
63 }
64 break;
65 case 'm':
66 if (type != DUMP_TYPE_KERNEL) {
67 type = DUMP_TYPE_MIX;
68 }
69 break;
70 case 'k':
71 type = DUMP_TYPE_KERNEL;
72 break;
73 case 'p':
74 ret = 0;
75 if (optarg != nullptr) {
76 if (atoi(optarg) > 0) {
77 ret = 1;
78 pid = atoi(optarg);
79 } else {
80 ret = -1;
81 PrintCommandFailed();
82 }
83 }
84 break;
85 case 't':
86 if (optarg != nullptr) {
87 if (atoi(optarg) > 0) {
88 tid = atoi(optarg);
89 } else {
90 ret = -1;
91 PrintCommandFailed();
92 }
93 }
94 break;
95 default:
96 ret = 0;
97 break;
98 }
99 }
100
101 if (ret == 0) {
102 PrintCommandHelp();
103 }
104 return ret;
105 }
106
main(int argc,char * argv[])107 int main(int argc, char *argv[])
108 {
109 #if defined(DEBUG_CRASH_LOCAL_HANDLER)
110 DFX_InstallLocalSignalHandler();
111 #endif
112
113 int32_t type = DUMP_TYPE_NATIVE;
114 int32_t pid = 0;
115 int32_t tid = 0;
116
117 alarm(DUMPCATCHER_TIMEOUT);
118 setsid();
119
120 if (ParseParamters(argc, argv, type, pid, tid) <= 0) {
121 return 0;
122 }
123
124 DFXLOG_DEBUG("type: %d, pid: %d, tid: %d", type, pid, tid);
125 OHOS::HiviewDFX::DumpCatcher::GetInstance().Dump(type, pid, tid);
126 return 0;
127 }
128