• 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 #include "dump_catcher_demo.h"
17 
18 #include <cstdint>
19 #include <cstdlib>
20 #include <cstring>
21 #include <iostream>
22 #include <string>
23 #include <unistd.h>
24 #include "dfx_dump_catcher.h"
25 #include "iosfwd"
26 #include "ostream"
27 
28 using namespace std;
29 
TestFunc10(void)30 static NOINLINE int TestFunc10(void)
31 {
32     OHOS::HiviewDFX::DfxDumpCatcher dumplog;
33     string msg = "";
34     bool ret = dumplog.DumpCatch(getpid(), gettid(), msg);
35     if (ret) {
36         cout << msg << endl;
37     }
38     return 0;
39 }
40 
TestFuncRemote(int32_t pid,int32_t tid)41 static NOINLINE int TestFuncRemote(int32_t pid, int32_t tid)
42 {
43     OHOS::HiviewDFX::DfxDumpCatcher dumplog;
44     string msg = "";
45     bool ret = dumplog.DumpCatch(pid, tid, msg);
46     if (ret) {
47         cout << msg << endl;
48     }
49     return ret;
50 }
51 
52 // auto gen function
53 GEN_TEST_FUNCTION(0, 1)
54 GEN_TEST_FUNCTION(1, 2)
55 GEN_TEST_FUNCTION(2, 3)
56 GEN_TEST_FUNCTION(3, 4)
57 GEN_TEST_FUNCTION(4, 5)
58 GEN_TEST_FUNCTION(5, 6)
59 GEN_TEST_FUNCTION(6, 7)
60 GEN_TEST_FUNCTION(7, 8)
61 GEN_TEST_FUNCTION(8, 9)
62 GEN_TEST_FUNCTION(9, 10)
63 
ParseParameters(int argc,char * argv[],int32_t & pid,int32_t & tid)64 static bool ParseParameters(int argc, char *argv[], int32_t &pid, int32_t &tid)
65 {
66     switch (argc) {
67         case 3:
68             if (!strcmp("-p", argv[1])) {
69                 pid = atoi(argv[2]);
70                 return true;
71             }
72             break;
73         case 5:
74             if (!strcmp("-p", argv[1])) {
75                 pid = atoi(argv[2]);
76 
77                 if (!strcmp("-t", argv[3])) {
78                     tid = atoi(argv[4]);
79                     return true;
80                 }
81             } else if (!strcmp("-t", argv[1])) {
82                 tid = atoi(argv[2]);
83 
84                 if (!strcmp("-p", argv[3])) {
85                     pid = atoi(argv[4]);
86                     return true;
87                 }
88             }
89             break;
90         default:
91             break;
92     }
93     return false;
94 }
95 
main(int argc,char * argv[])96 int main(int argc, char *argv[])
97 {
98     int32_t pid = 0;
99     int32_t tid = 0;
100     if (ParseParameters(argc, argv, pid, tid)) {
101         TestFuncRemote(pid, tid);
102     } else {
103         TestFunc0();
104     }
105 
106     return 0;
107 }
108