1 /** 2 * Copyright (c) 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_BREAKPOINT_STORAGE_H 17 #define PANDA_TOOLING_INSPECTOR_BREAKPOINT_STORAGE_H 18 19 #include <algorithm> 20 #include <functional> 21 #include <unordered_set> 22 23 #include "breakpoint_base.h" 24 #include "types/numeric_id.h" 25 26 namespace ark::tooling::inspector { 27 28 /** 29 * @brief Blocking breakpoint storage class, adds functionality to add/remove/enable/disable breakpoints, 30 * Allows threads to check if it should stop at location 31 */ 32 class BreakpointStorage final { 33 public: 34 BreakpointStorage() = default; 35 36 NO_COPY_SEMANTIC(BreakpointStorage); 37 NO_MOVE_SEMANTIC(BreakpointStorage); 38 39 ~BreakpointStorage() = default; 40 41 /** 42 * @brief Enable/disable breakpoints 43 * @param[in] active bool arg 44 */ 45 void SetBreakpointsActive(bool active); 46 47 /** 48 * @brief Get breakpoint id vector by location 49 * @param[in] location 50 * @returns std::vector of ids 51 */ 52 std::vector<BreakpointId> GetBreakpointsByLocation(const PtLocation &location) const; 53 54 /** 55 * @brief Set a breakpoint with optional condition. 56 * @param[in] sourceFilesFilter handler to filter src files. 57 * @param[in] lineNumber line in filtered src files. 58 * @param[out] sourceFiles returns sourceFiles where breakpoint was set 59 * @param[in] condition code fragment to compiled and evaluated for conditional bp 60 * @param[in] debugCache debug cache to link src files with bytecode executable 61 * @returns BreakpointId of set breakpoint. 62 */ 63 std::optional<BreakpointId> SetBreakpoint(SourceFileFilter &&sourceFilesFilter, size_t lineNumber, 64 std::set<std::string_view> &sourceFiles, const std::string *condition, 65 const DebugInfoCache &debugCache); 66 67 /** 68 * @brief Removes breakpoint by id 69 * @param[in] id 70 */ 71 void RemoveBreakpoint(BreakpointId id); 72 73 /** 74 * @brief Check if should stop at location 75 * @param[in] location 76 * @param[in] engine to evaluate conditions for conditional breakpoint 77 * @returns true/false 78 */ 79 bool ShouldStopAtBreakpoint(const PtLocation &location, EvaluationEngine &engine); 80 81 /** 82 * @brief Resolves pending breakpoints on new pandafile load 83 * @param[in] file loaded file 84 * @param[in] debugInfoCache debugcache with already added file 85 */ 86 void ResolveBreakpoints(const panda_file::File &file, const panda_file::DebugInfoExtractor *debugInfoCache); 87 88 /// @brief Resets storage to empty state 89 void Reset(); 90 91 /** 92 * @brief Removes breakpoint by filter function 93 * @param[in] filter function to apply to every breakpoint 94 */ 95 void RemoveBreakpoints(const std::function<bool(const PtLocation &loc)> &filter); 96 97 private: 98 mutable os::memory::RWLock lock_; GUARDED_BY(lock_)99 bool breakpointsActive_ GUARDED_BY(lock_) {true}; 100 BreakpointId nextBreakpointId_ GUARDED_BY(lock_) = 0; 101 std::unordered_multimap<PtLocation, BreakpointId, HashLocation> breakpointLocations_ GUARDED_BY(lock_); 102 std::unordered_map<BreakpointId, std::unique_ptr<BreakpointBase>> breakpointStorage_ GUARDED_BY(lock_); 103 }; 104 105 } // namespace ark::tooling::inspector 106 107 #endif // PANDA_TOOLING_INSPECTOR_BREAKPOINT_STORAGE_H 108