xref: /freebsd/contrib/llvm-project/lldb/include/lldb/Breakpoint/WatchpointResource.h (revision 700637cbb5e582861067a11aaca4d053546871d2)
1 //===-- WatchpointResource.h ------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLDB_BREAKPOINT_WATCHPOINTRESOURCE_H
10 #define LLDB_BREAKPOINT_WATCHPOINTRESOURCE_H
11 
12 #include "lldb/Utility/Iterable.h"
13 #include "lldb/lldb-public.h"
14 
15 #include <mutex>
16 #include <vector>
17 
18 namespace lldb_private {
19 
20 class WatchpointResource
21     : public std::enable_shared_from_this<WatchpointResource> {
22 
23 public:
24   WatchpointResource(lldb::addr_t addr, size_t size, bool read, bool write);
25 
26   ~WatchpointResource();
27 
28   typedef lldb::wp_resource_id_t SiteID;
29   typedef lldb::watch_id_t ConstituentID;
30 
31   lldb::addr_t GetLoadAddress() const;
32 
33   size_t GetByteSize() const;
34 
35   bool WatchpointResourceRead() const;
36 
37   bool WatchpointResourceWrite() const;
38 
39   void SetType(bool read, bool write);
40 
41   typedef std::vector<lldb::WatchpointSP> WatchpointCollection;
42   typedef LockingAdaptedIterable<std::mutex, WatchpointCollection>
43       WatchpointIterable;
44 
45   /// Iterate over the watchpoint constituents for this resource
46   ///
47   /// \return
48   ///     An Iterable object which can be used to loop over the watchpoints
49   ///     that are constituents of this resource.
Constituents()50   WatchpointIterable Constituents() {
51     return WatchpointIterable(m_constituents, m_constituents_mutex);
52   }
53 
54   /// Enquires of the atchpoints that produced this watchpoint resource
55   /// whether we should stop at this location.
56   ///
57   /// \param[in] context
58   ///    This contains the information about this stop.
59   ///
60   /// \return
61   ///    \b true if we should stop, \b false otherwise.
62   bool ShouldStop(StoppointCallbackContext *context);
63 
64   /// Standard Dump method
65   void Dump(Stream *s) const;
66 
67   /// The "Constituents" are the watchpoints that share this resource.
68   /// The method adds the \a constituent to this resource's constituent list.
69   ///
70   /// \param[in] constituent
71   ///    \a constituent is the Wachpoint to add.
72   void AddConstituent(const lldb::WatchpointSP &constituent);
73 
74   /// The method removes the constituent at \a constituent from this watchpoint
75   /// resource.
76   void RemoveConstituent(lldb::WatchpointSP &constituent);
77 
78   /// This method returns the number of Watchpoints currently using
79   /// watchpoint resource.
80   ///
81   /// \return
82   ///    The number of constituents.
83   size_t GetNumberOfConstituents();
84 
85   /// This method returns the Watchpoint at index \a index using this
86   /// Resource.  The constituents are listed ordinally from 0 to
87   /// GetNumberOfConstituents() - 1 so you can use this method to iterate over
88   /// the constituents.
89   ///
90   /// \param[in] idx
91   ///     The index in the list of constituents for which you wish the
92   ///     constituent location.
93   ///
94   /// \return
95   ///    The Watchpoint at that index.
96   lldb::WatchpointSP GetConstituentAtIndex(size_t idx);
97 
98   /// Check if the constituents includes a watchpoint.
99   ///
100   /// \param[in] wp_sp
101   ///     The WatchpointSP to search for.
102   ///
103   /// \result
104   ///     true if this resource's constituents includes the watchpoint.
105   bool ConstituentsContains(const lldb::WatchpointSP &wp_sp);
106 
107   /// Check if the constituents includes a watchpoint.
108   ///
109   /// \param[in] wp
110   ///     The Watchpoint to search for.
111   ///
112   /// \result
113   ///     true if this resource's constituents includes the watchpoint.
114   bool ConstituentsContains(const lldb_private::Watchpoint *wp);
115 
116   /// This method copies the watchpoint resource's constituents into a new
117   /// collection. It does this while the constituents mutex is locked.
118   ///
119   /// \return
120   ///    A copy of the Watchpoints which own this resource.
121   WatchpointCollection CopyConstituentsList();
122 
123   lldb::wp_resource_id_t GetID() const;
124 
125   bool Contains(lldb::addr_t addr);
126 
127 protected:
128   // The StopInfoWatchpoint knows when it is processing a hit for a thread for
129   // a site, so let it be the one to manage setting the location hit count once
130   // and only once.
131   friend class StopInfoWatchpoint;
132 
133   void BumpHitCounts();
134 
135 private:
136   static lldb::wp_resource_id_t GetNextID();
137 
138   lldb::wp_resource_id_t m_id;
139 
140   // Start address & size aligned & expanded to be a valid watchpoint
141   // memory granule on this target.
142   lldb::addr_t m_addr;
143   size_t m_size;
144 
145   bool m_watch_read;
146   bool m_watch_write;
147 
148   /// The Watchpoints which own this resource.
149   WatchpointCollection m_constituents;
150 
151   /// This mutex protects the constituents collection.
152   std::mutex m_constituents_mutex;
153 
154   WatchpointResource(const WatchpointResource &) = delete;
155   const WatchpointResource &operator=(const WatchpointResource &) = delete;
156 };
157 
158 } // namespace lldb_private
159 
160 #endif // LLDB_BREAKPOINT_WATCHPOINTRESOURCE_H
161