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 #ifndef HIPERF_CALLSTACK_TEST_H
16 #define HIPERF_CALLSTACK_TEST_H
17
18 #include <gmock/gmock.h>
19 #include <gtest/gtest.h>
20 #include <iterator>
21 #include <link.h>
22 #include <random>
23 #include <sys/mman.h>
24
25 #include <hilog/log.h>
26
27 #include "callstack.h"
28 #include "debug_logger.h"
29 #include "utilities.h"
30
31 namespace OHOS {
32 namespace Developtools {
33 namespace HiPerf {
34 static const std::string PATH_RESOURCE_TEST_DWARF_DATA = "resource/testdata/dwarf/";
35 /*
36 create from
37 ./hiperf_example_cmd --thread 1 --time 60 --stack 10
38 ./hiperf_host dump --userdata > dump.txt
39 */
40 static const std::string TEST_DWARF_ELF = "hiperf_example_cmd";
41 static const std::string TEST_DWARF_USER_REGS_0 = "user_regs.dump";
42 static const std::string TEST_DWARF_USER_DATA_0 = "user_data.dump";
43
44 struct mmapDumpInfo {
45 std::string fileName;
46 uint64_t begin = 0;
47 uint64_t len = 0;
48 uint64_t pgoff = 0;
49 };
50 /*
51 record mmap2: type 10, misc 2, size 107
52 pid 643, tid 643, addr 0x454000, len 0x3000
53 pgoff 0x0, maj 179, min 7, ino 4868, ino_generation 6874019636378171493
54 prot 1, flags 2, filename /data/local/tmp/hiperf_example_cmd
55
56 record mmap2: type 10, misc 2, size 107
57 pid 643, tid 643, addr 0x457000, len 0x5000
58 pgoff 0x2000, maj 179, min 7, ino 4868, ino_generation 7237954708011182952
59 prot 5, flags 2, filename /data/local/tmp/hiperf_example_cmd
60
61 record mmap2: type 10, misc 2, size 107
62 pid 643, tid 643, addr 0x45c000, len 0x1000
63 pgoff 0x6000, maj 179, min 7, ino 4868, ino_generation 7237954708011182952
64 prot 1, flags 2, filename /data/local/tmp/hiperf_example_cmd
65
66 record mmap2: type 10, misc 2, size 107
67 pid 643, tid 643, addr 0x45d000, len 0x1000
68 pgoff 0x6000, maj 179, min 7, ino 4868, ino_generation 7237954708011182952
69 prot 3, flags 2, filename /data/local/tmp/hiperf_example_cmd
70 */
71 static const std::vector<mmapDumpInfo> TEST_DWARF_MMAP = {
72 {"/data/local/tmp/hiperf_example_cmd", 0x454000, 0x3000, 0x0},
73 {"/data/local/tmp/hiperf_example_cmd", 0x457000, 0x5000, 0x2000},
74 {"/data/local/tmp/hiperf_example_cmd", 0x45c000, 0x1000, 0x6000},
75 {"/data/local/tmp/hiperf_example_cmd", 0x45d000, 0x1000, 0x6000},
76
77 };
78
79 /*
80 UnwindStep:unwind:0: ip 0x45765e sp 0xb6ca1c68
81 UnwindStep:unwind:1: ip 0x45768a sp 0xb6ca1c78
82 UnwindStep:unwind:2: ip 0x4576ce sp 0xb6ca1c88
83 UnwindStep:unwind:3: ip 0x457712 sp 0xb6ca1c98
84 UnwindStep:unwind:4: ip 0x457756 sp 0xb6ca1ca8
85 UnwindStep:unwind:5: ip 0x45779a sp 0xb6ca1cb8
86 UnwindStep:unwind:6: ip 0x4577de sp 0xb6ca1cc8
87 UnwindStep:unwind:7: ip 0x457822 sp 0xb6ca1cd8
88 UnwindStep:unwind:8: ip 0x457866 sp 0xb6ca1ce8
89 UnwindStep:unwind:9: ip 0x4578aa sp 0xb6ca1cf8
90 UnwindStep:unwind:10: ip 0x4578ee sp 0xb6ca1d08
91 UnwindStep:unwind:11: ip 0x45793c sp 0xb6ca1d18
92 UnwindStep:unwind:12: ip 0x457ffe sp 0xb6ca1d28
93 UnwindStep:unwind:13: ip 0xb6f01f73 sp 0xb6ca1d38
94 */
95 struct frame {
96 uint64_t ip = 0;
97 uint64_t sp = 0;
98 };
99 static const std::vector<frame> TEST_DWARF_FRAMES = {
100 {0x4575BC, 0xB6CA1C18}, // base ip sp
101 {0x45765e, 0xb6ca1c68}, {0x45768a, 0xb6ca1c78}, {0x4576ce, 0xb6ca1c88},
102 {0x457712, 0xb6ca1c98}, {0x457756, 0xb6ca1ca8}, {0x45779a, 0xb6ca1cb8},
103 {0x4577de, 0xb6ca1cc8}, {0x457822, 0xb6ca1cd8}, {0x457866, 0xb6ca1ce8},
104 {0x4578aa, 0xb6ca1cf8}, {0x4578ee, 0xb6ca1d08}, {0x45793c, 0xb6ca1d18},
105 {0x457ffe, 0xb6ca1d28}, {0xb6f01f73, 0xb6ca1d38},
106 };
107
108 // data convert funcion
109 template<class T>
LoadFromFile(const std::string & fileName,std::vector<T> & data)110 void LoadFromFile(const std::string &fileName, std::vector<T> &data)
111 {
112 FILE* fp_ = fopen(fileName.c_str(), "r");
113 if (fp_ == nullptr) {
114 return;
115 }
116 std::unique_ptr<FILE, decltype(&fclose)> fp(fp_, fclose);
117 if (fp) {
118 struct stat sb = {};
119 ASSERT_NE(fstat(fileno(fp.get()), &sb), -1);
120 ASSERT_NE(sb.st_size, 0);
121 ASSERT_EQ(sb.st_size % sizeof(T), 0u);
122 data.resize(sb.st_size / sizeof(T));
123 ASSERT_EQ(fread(data.data(), sizeof(T), data.size(), fp.get()), data.size());
124 }
125 }
126
MakeMaps(VirtualThread & thread)127 static void MakeMaps(VirtualThread &thread)
128 {
129 for (const mmapDumpInfo &mmap : TEST_DWARF_MMAP) {
130 thread.CreateMapItem(mmap.fileName, mmap.begin, mmap.len, mmap.pgoff);
131 }
132 }
133 } // namespace HiPerf
134 } // namespace Developtools
135 } // namespace OHOS
136 #endif