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