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