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