• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 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 #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 panda::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, debug_info] : debugInfos_) {
63             if (!pandaFileFilter(file, debug_info)) {
64                 continue;
65             }
66 
67             for (auto methodId : debug_info.GetMethodIdList()) {
68                 if (!methodFilter(file, debug_info, methodId)) {
69                     continue;
70                 }
71 
72                 auto &table = debug_info.GetLineNumberTable(methodId);
73                 for (auto it = table.begin(); it != table.end(); ++it) {
74                     auto next = it + 1;
75                     if (!handler(file, debug_info, methodId, *it, next != table.end() ? &*next : nullptr)) {
76                         break;
77                     }
78                 }
79             }
80         }
81     }
82 
83     os::memory::Mutex debugInfosMutex_;
84     std::unordered_map<const panda_file::File *, disasm::DisasmBackedDebugInfoExtractor> debugInfos_
85         GUARDED_BY(debugInfosMutex_);
86 
87     os::memory::Mutex disassembliesMutex_;
88     std::unordered_map<std::string_view, std::pair<const panda_file::File &, panda_file::File::EntityId>> disassemblies_
89         GUARDED_BY(disassembliesMutex_);
90 };
91 }  // namespace panda::tooling::inspector
92 
93 #endif  // PANDA_TOOLING_INSPECTOR_DEBUG_INFO_CACHE_H
94