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