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 "test_extractor.h"
17
18 #include <algorithm>
19 #include <limits>
20
21 #include "libpandabase/utils/leb128.h"
22 #include "libpandabase/utils/utf.h"
23 #include "libpandafile/class_data_accessor-inl.h"
24 #include "libpandafile/code_data_accessor-inl.h"
25 #include "libpandafile/debug_data_accessor-inl.h"
26 #include "libpandafile/helpers.h"
27 #include "libpandafile/method_data_accessor-inl.h"
28 #include "libpandafile/proto_data_accessor-inl.h"
29
30 namespace panda::tooling::test {
31
TestExtractor(const panda_file::File * pf)32 TestExtractor::TestExtractor(const panda_file::File *pf)
33 {
34 lang_extractor_ = MakePandaUnique<panda_file::DebugInfoExtractor>(pf);
35 }
36
GetBreakpointAddress(const SourceLocation & source_location)37 std::pair<EntityId, uint32_t> TestExtractor::GetBreakpointAddress(const SourceLocation &source_location)
38 {
39 auto pos = source_location.path.find_last_of("/\\");
40 auto name = source_location.path;
41
42 if (pos != PandaString::npos) {
43 name = name.substr(pos + 1);
44 }
45
46 std::vector<panda_file::File::EntityId> methods = lang_extractor_->GetMethodIdList();
47 for (const auto &method : methods) {
48 auto srcName = PandaString(lang_extractor_->GetSourceFile(method));
49 auto pos_sf = srcName.find_last_of("/\\");
50 if (pos_sf != PandaString::npos) {
51 srcName = srcName.substr(pos_sf + 1);
52 }
53 if (srcName == name) {
54 const panda_file::LineNumberTable &line_table = lang_extractor_->GetLineNumberTable(method);
55 if (line_table.empty()) {
56 continue;
57 }
58
59 std::optional<size_t> offset = GetOffsetByTableLineNumber(line_table, source_location.line);
60 if (offset == std::nullopt) {
61 continue;
62 }
63 return {method, offset.value()};
64 }
65 }
66 return {EntityId(), 0};
67 }
68
GetStepRanges(EntityId method_id,uint32_t current_offset)69 PandaList<PtStepRange> TestExtractor::GetStepRanges(EntityId method_id, uint32_t current_offset)
70 {
71 const panda_file::LineNumberTable &line_table = lang_extractor_->GetLineNumberTable(method_id);
72 if (line_table.empty()) {
73 return {};
74 }
75
76 std::optional<size_t> line = GetLineNumberByTableOffset(line_table, current_offset);
77 if (line == std::nullopt) {
78 return {};
79 }
80
81 PandaList<PtStepRange> res;
82 for (auto it = line_table.begin(); it != line_table.end(); ++it) {
83 if (it->line == line) {
84 size_t idx = it - line_table.begin();
85 if (it + 1 != line_table.end()) {
86 res.push_back({line_table[idx].offset, line_table[idx + 1].offset});
87 } else {
88 res.push_back({line_table[idx].offset, std::numeric_limits<uint32_t>::max()});
89 }
90 }
91 }
92 return res;
93 }
94
GetLocalVariableInfo(EntityId method_id,size_t offset)95 std::vector<panda_file::LocalVariableInfo> TestExtractor::GetLocalVariableInfo(EntityId method_id, size_t offset)
96 {
97 const std::vector<panda_file::LocalVariableInfo> &variables = lang_extractor_->GetLocalVariableTable(method_id);
98 std::vector<panda_file::LocalVariableInfo> result;
99
100 for (const auto &variable : variables) {
101 if (variable.start_offset <= offset && offset <= variable.end_offset) {
102 result.push_back(variable);
103 }
104 }
105 return result;
106 }
107
GetLineNumberByTableOffset(const panda_file::LineNumberTable & table,uint32_t offset)108 std::optional<size_t> TestExtractor::GetLineNumberByTableOffset(const panda_file::LineNumberTable &table,
109 uint32_t offset)
110 {
111 for (const auto &value : table) {
112 if (value.offset == offset) {
113 return value.line;
114 }
115 }
116 return std::nullopt;
117 }
118
GetOffsetByTableLineNumber(const panda_file::LineNumberTable & table,size_t line)119 std::optional<uint32_t> TestExtractor::GetOffsetByTableLineNumber(const panda_file::LineNumberTable &table, size_t line)
120 {
121 for (const auto &value : table) {
122 if (value.line == line) {
123 return value.offset;
124 }
125 }
126 return std::nullopt;
127 }
128
GetSourceLocation(EntityId method_id,uint32_t bytecode_offset)129 SourceLocation TestExtractor::GetSourceLocation(EntityId method_id, uint32_t bytecode_offset)
130 {
131 const panda_file::LineNumberTable &line_table = lang_extractor_->GetLineNumberTable(method_id);
132 if (line_table.empty()) {
133 return SourceLocation();
134 }
135
136 std::optional<size_t> line = GetLineNumberByTableOffset(line_table, bytecode_offset);
137 if (line == std::nullopt) {
138 return SourceLocation();
139 }
140
141 return SourceLocation {lang_extractor_->GetSourceFile(method_id), line.value()};
142 }
143 } // namespace panda::tooling::test
144