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