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 "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 ark::tooling::test {
31
TestExtractor(const panda_file::File * pf)32 TestExtractor::TestExtractor(const panda_file::File *pf)
33 : langExtractor_(MakePandaUnique<panda_file::DebugInfoExtractor>(pf))
34 {
35 }
36
GetBreakpointAddress(const SourceLocation & sourceLocation)37 std::pair<EntityId, uint32_t> TestExtractor::GetBreakpointAddress(const SourceLocation &sourceLocation)
38 {
39 auto pos = sourceLocation.path.find_last_of("/\\");
40 auto name = sourceLocation.path;
41
42 if (pos != PandaString::npos) {
43 name = name.substr(pos + 1);
44 }
45
46 std::vector<panda_file::File::EntityId> methods = langExtractor_->GetMethodIdList();
47 for (const auto &method : methods) {
48 auto srcName = PandaString(langExtractor_->GetSourceFile(method));
49 auto posSf = srcName.find_last_of("/\\");
50 if (posSf != PandaString::npos) {
51 srcName = srcName.substr(posSf + 1);
52 }
53 if (srcName == name) {
54 const panda_file::LineNumberTable &lineTable = langExtractor_->GetLineNumberTable(method);
55 if (lineTable.empty()) {
56 continue;
57 }
58
59 std::optional<size_t> offset = GetOffsetByTableLineNumber(lineTable, sourceLocation.line);
60 if (offset == std::nullopt) {
61 continue;
62 }
63 return {method, offset.value()};
64 }
65 }
66 return {EntityId(), 0};
67 }
68
GetStepRanges(EntityId methodId,uint32_t currentOffset)69 PandaList<PtStepRange> TestExtractor::GetStepRanges(EntityId methodId, uint32_t currentOffset)
70 {
71 const panda_file::LineNumberTable &lineTable = langExtractor_->GetLineNumberTable(methodId);
72 if (lineTable.empty()) {
73 return {};
74 }
75
76 std::optional<size_t> line = GetLineNumberByTableOffset(lineTable, currentOffset);
77 if (line == std::nullopt) {
78 return {};
79 }
80
81 PandaList<PtStepRange> res;
82 for (auto it = lineTable.begin(); it != lineTable.end(); ++it) {
83 if (it->line == line) {
84 size_t idx = it - lineTable.begin();
85 if (it + 1 != lineTable.end()) {
86 res.push_back({lineTable[idx].offset, lineTable[idx + 1].offset});
87 } else {
88 res.push_back({lineTable[idx].offset, std::numeric_limits<uint32_t>::max()});
89 }
90 }
91 }
92 return res;
93 }
94
GetLocalVariableInfo(EntityId methodId,size_t offset)95 std::vector<panda_file::LocalVariableInfo> TestExtractor::GetLocalVariableInfo(EntityId methodId, size_t offset)
96 {
97 const std::vector<panda_file::LocalVariableInfo> &variables = langExtractor_->GetLocalVariableTable(methodId);
98 std::vector<panda_file::LocalVariableInfo> result;
99
100 for (const auto &variable : variables) {
101 if (variable.startOffset <= offset && offset <= variable.endOffset) {
102 result.push_back(variable);
103 }
104 }
105 return result;
106 }
107
GetParameterInfo(EntityId methodId)108 const std::vector<panda_file::DebugInfoExtractor::ParamInfo> &TestExtractor::GetParameterInfo(EntityId methodId)
109 {
110 return langExtractor_->GetParameterInfo(methodId);
111 }
112
GetLineNumberByTableOffset(const panda_file::LineNumberTable & table,uint32_t offset)113 std::optional<size_t> TestExtractor::GetLineNumberByTableOffset(const panda_file::LineNumberTable &table,
114 uint32_t offset)
115 {
116 for (const auto &value : table) {
117 if (value.offset == offset) {
118 return value.line;
119 }
120 }
121 return std::nullopt;
122 }
123
GetOffsetByTableLineNumber(const panda_file::LineNumberTable & table,size_t line)124 std::optional<uint32_t> TestExtractor::GetOffsetByTableLineNumber(const panda_file::LineNumberTable &table, size_t line)
125 {
126 for (const auto &value : table) {
127 if (value.line == line) {
128 return value.offset;
129 }
130 }
131 return std::nullopt;
132 }
133
GetSourceLocation(EntityId methodId,uint32_t bytecodeOffset)134 SourceLocation TestExtractor::GetSourceLocation(EntityId methodId, uint32_t bytecodeOffset)
135 {
136 const panda_file::LineNumberTable &lineTable = langExtractor_->GetLineNumberTable(methodId);
137 if (lineTable.empty()) {
138 return SourceLocation();
139 }
140
141 std::optional<size_t> line = GetLineNumberByTableOffset(lineTable, bytecodeOffset);
142 if (line == std::nullopt) {
143 return SourceLocation();
144 }
145
146 return SourceLocation {langExtractor_->GetSourceFile(methodId), line.value()};
147 }
148 } // namespace ark::tooling::test
149