• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2022-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_TOOLING_INSPECTOR_DEBUG_INFO_CACHE_H
17 #define PANDA_TOOLING_INSPECTOR_DEBUG_INFO_CACHE_H
18 
19 #include <memory>
20 #include <set>
21 #include <unordered_map>
22 #include <unordered_set>
23 #include <vector>
24 
25 #include "disassembler/disasm_backed_debug_info_extractor.h"
26 #include "include/method.h"
27 #include "include/typed_value.h"
28 #include "runtime/tooling/debugger.h"
29 
30 #include "types/location.h"
31 
32 namespace ark::tooling::inspector {
33 class DebugInfoCache final {
34 public:
35     DebugInfoCache() = default;
36     ~DebugInfoCache() = default;
37 
38     NO_COPY_SEMANTIC(DebugInfoCache);
39     NO_MOVE_SEMANTIC(DebugInfoCache);
40 
41     void AddPandaFile(const panda_file::File &file);
42     void GetSourceLocation(const PtFrame &frame, std::string_view &sourceFile, std::string_view &methodName,
43                            size_t &lineNumber);
44     std::unordered_set<PtLocation, HashLocation> GetCurrentLineLocations(const PtFrame &frame);
45     std::unordered_set<PtLocation, HashLocation> GetContinueToLocations(std::string_view sourceFile, size_t lineNumber);
46     std::vector<PtLocation> GetBreakpointLocations(const std::function<bool(std::string_view)> &sourceFileFilter,
47                                                    size_t lineNumber, std::set<std::string_view> &sourceFiles);
48     std::set<size_t> GetValidLineNumbers(std::string_view sourceFile, size_t startLine, size_t endLine,
49                                          bool restrictToFunction);
50 
51     std::map<std::string, TypedValue> GetLocals(const PtFrame &frame);
52 
53     std::string GetSourceCode(std::string_view sourceFile);
54 
55 private:
56     const panda_file::DebugInfoExtractor &GetDebugInfo(const panda_file::File *file);
57 
58     template <typename PFF, typename MF, typename H>
EnumerateLineEntries(PFF && pandaFileFilter,MF && methodFilter,H && handler)59     void EnumerateLineEntries(PFF &&pandaFileFilter, MF &&methodFilter, H &&handler)
60     {
61         os::memory::LockHolder lock(debugInfosMutex_);
62 
63         for (auto &[file, debugInfo] : debugInfos_) {
64             if (!pandaFileFilter(file, debugInfo)) {
65                 continue;
66             }
67 
68             for (auto methodId : debugInfo.GetMethodIdList()) {
69                 if (!methodFilter(file, debugInfo, methodId)) {
70                     continue;
71                 }
72 
73                 EnumerateLineEntries(file, debugInfo, methodId, std::forward<H>(handler));
74             }
75         }
76     }
77 
78     template <typename H>
EnumerateLineEntries(const panda_file::File * file,const disasm::DisasmBackedDebugInfoExtractor & debugInfo,panda_file::File::EntityId methodId,H && handler)79     void EnumerateLineEntries(const panda_file::File *file, const disasm::DisasmBackedDebugInfoExtractor &debugInfo,
80                               panda_file::File::EntityId methodId, H &&handler) REQUIRES(debugInfosMutex_)
81     {
82         auto &table = debugInfo.GetLineNumberTable(methodId);
83         for (auto it = table.begin(); it != table.end(); ++it) {
84             auto next = it + 1;
85             if (!handler(file, debugInfo, methodId, *it, next != table.end() ? &*next : nullptr)) {
86                 break;
87             }
88         }
89     }
90 
91     os::memory::Mutex debugInfosMutex_;
92     std::unordered_map<const panda_file::File *, disasm::DisasmBackedDebugInfoExtractor> debugInfos_
93         GUARDED_BY(debugInfosMutex_);
94 
95     os::memory::Mutex disassembliesMutex_;
96     std::unordered_map<std::string_view, std::pair<const panda_file::File &, panda_file::File::EntityId>> disassemblies_
97         GUARDED_BY(disassembliesMutex_);
98 };
99 }  // namespace ark::tooling::inspector
100 
101 #endif  // PANDA_TOOLING_INSPECTOR_DEBUG_INFO_CACHE_H
102