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 constexpr size_t defaultMaxFrameNum = 256;
38 bool ret = dumplog.DumpCatch(pid, tid, msg, defaultMaxFrameNum, isJson);
39 time_t elapsed1 = counter.Elapsed();
40 if (ret) {
41 std::cout << msg << std::endl;
42 if (isJson) {
43 std::string outStr = "";
44 OHOS::HiviewDFX::DfxJsonFormatter::FormatJsonStack(msg, outStr);
45 std::cout << outStr << std::endl;
46 }
47 }
48 time_t elapsed2 = counter.Elapsed();
49 std::cout << "elapsed1: " << elapsed1 << " ,elapsed2: " << elapsed2 << std::endl;
50 return ret;
51 }
52
TestFunc10(void)53 static NOINLINE int TestFunc10(void)
54 {
55 return TestFuncDump(getpid(), gettid(), false);
56 }
57
58 // auto gen function
59 GEN_TEST_FUNCTION(9, 10)
60
ParseParameters(int argc,char * argv[],int32_t & pid,int32_t & tid)61 static bool ParseParameters(int argc, char *argv[], int32_t &pid, int32_t &tid)
62 {
63 switch (argc) {
64 case 3:
65 if (!strcmp("-p", argv[1])) {
66 pid = atoi(argv[2]);
67 return true;
68 }
69 if (!strcmp("-t", argv[1])) {
70 pid = getpid();
71 tid = atoi(argv[2]);
72 return true;
73 }
74 break;
75 case 5:
76 if (!strcmp("-p", argv[1])) {
77 pid = atoi(argv[2]);
78
79 if (!strcmp("-t", argv[3])) {
80 tid = atoi(argv[4]);
81 return true;
82 }
83 } else if (!strcmp("-t", argv[1])) {
84 tid = atoi(argv[2]);
85
86 if (!strcmp("-p", argv[3])) {
87 pid = atoi(argv[4]);
88 return true;
89 }
90 }
91 break;
92 default:
93 break;
94 }
95 return false;
96 }
97
main(int argc,char * argv[])98 int main(int argc, char *argv[])
99 {
100 int32_t pid = 0;
101 int32_t tid = 0;
102 if (ParseParameters(argc, argv, pid, tid)) {
103 TestFuncDump(pid, tid, true);
104 } else {
105 TestFunc0();
106 }
107
108 return 0;
109 }