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 #ifndef PANDA_FILE_DEBUG_HELPERS_
17 #define PANDA_FILE_DEBUG_HELPERS_
18 
19 #include "debug_data_accessor-inl.h"
20 #include "file.h"
21 #include "file_items.h"
22 #include "method_data_accessor-inl.h"
23 #include "line_number_program.h"
24 #include "libpandabase/utils/span.h"
25 
26 namespace ark::panda_file::debug_helpers {
27 
28 class BytecodeOffsetResolver {
29 public:
BytecodeOffsetResolver(panda_file::LineProgramState *state, uint32_t bcOffset)30     BytecodeOffsetResolver(panda_file::LineProgramState *state, uint32_t bcOffset)
31         : state_(state), bcOffset_(bcOffset), prevLine_(state->GetLine())
32     {
33     }
34 
35     ~BytecodeOffsetResolver() = default;
36 
37     DEFAULT_MOVE_SEMANTIC(BytecodeOffsetResolver);
38     DEFAULT_COPY_SEMANTIC(BytecodeOffsetResolver);
39 
GetState() const40     panda_file::LineProgramState *GetState() const
41     {
42         return state_;
43     }
44 
GetLine() const45     uint32_t GetLine() const
46     {
47         return line_;
48     }
49 
ProcessBegin() const50     void ProcessBegin() const {}
51 
ProcessEnd()52     void ProcessEnd()
53     {
54         if (line_ == 0) {
55             line_ = state_->GetLine();
56         }
57     }
58 
HandleAdvanceLine(int32_t lineDiff) const59     bool HandleAdvanceLine(int32_t lineDiff) const
60     {
61         state_->AdvanceLine(lineDiff);
62         return true;
63     }
64 
HandleAdvancePc(uint32_t pcDiff) const65     bool HandleAdvancePc(uint32_t pcDiff) const
66     {
67         state_->AdvancePc(pcDiff);
68         return true;
69     }
70 
HandleSetFile([[maybe_unused]] uint32_t sourceFileId) const71     bool HandleSetFile([[maybe_unused]] uint32_t sourceFileId) const
72     {
73         return true;
74     }
75 
HandleSetSourceCode([[maybe_unused]] uint32_t sourceCodeId) const76     bool HandleSetSourceCode([[maybe_unused]] uint32_t sourceCodeId) const
77     {
78         return true;
79     }
80 
HandleSetPrologueEnd() const81     bool HandleSetPrologueEnd() const
82     {
83         return true;
84     }
85 
HandleSetEpilogueBegin() const86     bool HandleSetEpilogueBegin() const
87     {
88         return true;
89     }
90 
HandleStartLocal([[maybe_unused]] int32_t regNumber, [[maybe_unused]] uint32_t nameId, [[maybe_unused]] uint32_t typeId) const91     bool HandleStartLocal([[maybe_unused]] int32_t regNumber, [[maybe_unused]] uint32_t nameId,
92                           [[maybe_unused]] uint32_t typeId) const
93     {
94         return true;
95     }
96 
HandleStartLocalExtended([[maybe_unused]] int32_t regNumber, [[maybe_unused]] uint32_t nameId, [[maybe_unused]] uint32_t typeId, [[maybe_unused]] uint32_t typeSignatureId) const97     bool HandleStartLocalExtended([[maybe_unused]] int32_t regNumber, [[maybe_unused]] uint32_t nameId,
98                                   [[maybe_unused]] uint32_t typeId, [[maybe_unused]] uint32_t typeSignatureId) const
99     {
100         return true;
101     }
102 
HandleEndLocal([[maybe_unused]] int32_t regNumber) const103     bool HandleEndLocal([[maybe_unused]] int32_t regNumber) const
104     {
105         return true;
106     }
107 
HandleRestartLocal([[maybe_unused]] int32_t regNumber) const108     bool HandleRestartLocal([[maybe_unused]] int32_t regNumber) const
109     {
110         return true;
111     }
112 
HandleSetColumn([[maybe_unused]] int32_t columnNumber) const113     bool HandleSetColumn([[maybe_unused]] int32_t columnNumber) const
114     {
115         return true;
116     }
117 
HandleSpecialOpcode(uint32_t pcOffset, int32_t lineOffset)118     bool HandleSpecialOpcode(uint32_t pcOffset, int32_t lineOffset)
119     {
120         state_->AdvancePc(pcOffset);
121         state_->AdvanceLine(lineOffset);
122 
123         if (state_->GetAddress() == bcOffset_) {
124             line_ = state_->GetLine();
125             return false;
126         }
127 
128         if (state_->GetAddress() > bcOffset_) {
129             line_ = prevLine_;
130             return false;
131         }
132 
133         prevLine_ = state_->GetLine();
134 
135         return true;
136     }
137 
138 private:
139     panda_file::LineProgramState *state_;
140     uint32_t bcOffset_;
141     uint32_t prevLine_;
142     uint32_t line_ {0};
143 };
144 
145 size_t GetLineNumber(ark::panda_file::MethodDataAccessor mda, uint32_t bcOffset,
146                      const ark::panda_file::File *pandaDebugFile);
147 
148 const char *GetStringFromConstantPool(const File &pf, uint32_t offset);
149 
150 }  // namespace ark::panda_file::debug_helpers
151 
152 #endif  // PANDA_FILE_DEBUG_HELPERS_
153