1254721Semaste//===-- BreakpointLocationList.h --------------------------------*- C++ -*-===//
2254721Semaste//
3353358Sdim// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4353358Sdim// See https://llvm.org/LICENSE.txt for license information.
5353358Sdim// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6254721Semaste//
7254721Semaste//===----------------------------------------------------------------------===//
8254721Semaste
9254721Semaste#ifndef liblldb_BreakpointLocationList_h_
10254721Semaste#define liblldb_BreakpointLocationList_h_
11254721Semaste
12296417Sdim#include <map>
13309124Sdim#include <mutex>
14254721Semaste#include <vector>
15296417Sdim
16254721Semaste#include "lldb/Core/Address.h"
17280031Sdim#include "lldb/Utility/Iterable.h"
18314564Sdim#include "lldb/lldb-private.h"
19254721Semaste
20254721Semastenamespace lldb_private {
21254721Semaste
22353358Sdim/// \class BreakpointLocationList BreakpointLocationList.h
23341825Sdim/// "lldb/Breakpoint/BreakpointLocationList.h" This class is used by
24341825Sdim/// Breakpoint to manage a list of breakpoint locations, each breakpoint
25341825Sdim/// location in the list has a unique ID, and is unique by Address as well.
26314564Sdimclass BreakpointLocationList {
27341825Sdim  // Only Breakpoints can make the location list, or add elements to it. This
28341825Sdim  // is not just some random collection of locations.  Rather, the act of
29341825Sdim  // adding the location to this list sets its ID, and implicitly all the
30341825Sdim  // locations have the same breakpoint ID as well.  If you need a generic
31341825Sdim  // container for breakpoint locations, use BreakpointLocationCollection.
32314564Sdim  friend class Breakpoint;
33254721Semaste
34254721Semastepublic:
35314564Sdim  virtual ~BreakpointLocationList();
36254721Semaste
37314564Sdim  /// Standard "Dump" method.  At present it does nothing.
38314564Sdim  void Dump(Stream *s) const;
39254721Semaste
40341825Sdim  /// Returns a shared pointer to the breakpoint location at address \a addr -
41341825Sdim  /// const version.
42314564Sdim  ///
43353358Sdim  /// \param[in] addr
44314564Sdim  ///     The address to look for.
45314564Sdim  ///
46353358Sdim  /// \result
47314564Sdim  ///     A shared pointer to the breakpoint. May contain a nullptr
48314564Sdim  ///     pointer if the breakpoint doesn't exist.
49314564Sdim  const lldb::BreakpointLocationSP FindByAddress(const Address &addr) const;
50254721Semaste
51341825Sdim  /// Returns a shared pointer to the breakpoint location with id \a breakID,
52341825Sdim  /// const version.
53314564Sdim  ///
54353358Sdim  /// \param[in] breakID
55314564Sdim  ///     The breakpoint location ID to seek for.
56314564Sdim  ///
57353358Sdim  /// \result
58314564Sdim  ///     A shared pointer to the breakpoint. May contain a nullptr
59314564Sdim  ///     pointer if the breakpoint doesn't exist.
60314564Sdim  lldb::BreakpointLocationSP FindByID(lldb::break_id_t breakID) const;
61254721Semaste
62341825Sdim  /// Returns the breakpoint location id to the breakpoint location at address
63341825Sdim  /// \a addr.
64314564Sdim  ///
65353358Sdim  /// \param[in] addr
66314564Sdim  ///     The address to match.
67314564Sdim  ///
68353358Sdim  /// \result
69314564Sdim  ///     The ID of the breakpoint location, or LLDB_INVALID_BREAK_ID.
70314564Sdim  lldb::break_id_t FindIDByAddress(const Address &addr);
71254721Semaste
72341825Sdim  /// Returns a breakpoint location list of the breakpoint locations in the
73341825Sdim  /// module \a module.  This list is allocated, and owned by the caller.
74314564Sdim  ///
75353358Sdim  /// \param[in] module
76314564Sdim  ///     The module to seek in.
77314564Sdim  ///
78360784Sdim  /// \param[in] bp_loc_list
79314564Sdim  ///     A breakpoint collection that gets any breakpoint locations
80314564Sdim  ///     that match \a module appended to.
81314564Sdim  ///
82353358Sdim  /// \result
83314564Sdim  ///     The number of matches
84314564Sdim  size_t FindInModule(Module *module,
85314564Sdim                      BreakpointLocationCollection &bp_loc_list);
86254721Semaste
87341825Sdim  /// Returns a shared pointer to the breakpoint location with index \a i.
88314564Sdim  ///
89353358Sdim  /// \param[in] i
90314564Sdim  ///     The breakpoint location index to seek for.
91314564Sdim  ///
92353358Sdim  /// \result
93314564Sdim  ///     A shared pointer to the breakpoint. May contain a nullptr
94314564Sdim  ///     pointer if the breakpoint doesn't exist.
95314564Sdim  lldb::BreakpointLocationSP GetByIndex(size_t i);
96254721Semaste
97341825Sdim  /// Returns a shared pointer to the breakpoint location with index \a i,
98341825Sdim  /// const version.
99314564Sdim  ///
100353358Sdim  /// \param[in] i
101314564Sdim  ///     The breakpoint location index to seek for.
102314564Sdim  ///
103353358Sdim  /// \result
104314564Sdim  ///     A shared pointer to the breakpoint. May contain a nullptr
105314564Sdim  ///     pointer if the breakpoint doesn't exist.
106314564Sdim  const lldb::BreakpointLocationSP GetByIndex(size_t i) const;
107254721Semaste
108341825Sdim  /// Removes all the locations in this list from their breakpoint site owners
109341825Sdim  /// list.
110314564Sdim  void ClearAllBreakpointSites();
111254721Semaste
112341825Sdim  /// Tells all the breakpoint locations in this list to attempt to resolve
113341825Sdim  /// any possible breakpoint sites.
114314564Sdim  void ResolveAllBreakpointSites();
115254721Semaste
116341825Sdim  /// Returns the number of breakpoint locations in this list with resolved
117341825Sdim  /// breakpoints.
118314564Sdim  ///
119353358Sdim  /// \result
120314564Sdim  ///     Number of qualifying breakpoint locations.
121314564Sdim  size_t GetNumResolvedLocations() const;
122254721Semaste
123314564Sdim  /// Returns the number hit count of all locations in this list.
124314564Sdim  ///
125353358Sdim  /// \result
126314564Sdim  ///     Hit count of all locations in this list.
127314564Sdim  uint32_t GetHitCount() const;
128254721Semaste
129341825Sdim  /// Enquires of the breakpoint location in this list with ID \a breakID
130341825Sdim  /// whether we should stop.
131314564Sdim  ///
132353358Sdim  /// \param[in] context
133314564Sdim  ///     This contains the information about this stop.
134314564Sdim  ///
135353358Sdim  /// \param[in] breakID
136314564Sdim  ///     This break ID that we hit.
137314564Sdim  ///
138353358Sdim  /// \return
139314564Sdim  ///     \b true if we should stop, \b false otherwise.
140314564Sdim  bool ShouldStop(StoppointCallbackContext *context, lldb::break_id_t breakID);
141254721Semaste
142314564Sdim  /// Returns the number of elements in this breakpoint location list.
143314564Sdim  ///
144353358Sdim  /// \result
145314564Sdim  ///     The number of elements.
146314564Sdim  size_t GetSize() const { return m_locations.size(); }
147254721Semaste
148341825Sdim  /// Print a description of the breakpoint locations in this list to the
149341825Sdim  /// stream \a s.
150314564Sdim  ///
151353358Sdim  /// \param[in] s
152314564Sdim  ///     The stream to which to print the description.
153314564Sdim  ///
154353358Sdim  /// \param[in] level
155314564Sdim  ///     The description level that indicates the detail level to
156314564Sdim  ///     provide.
157314564Sdim  ///
158353358Sdim  /// \see lldb::DescriptionLevel
159314564Sdim  void GetDescription(Stream *s, lldb::DescriptionLevel level);
160254721Semaste
161254721Semasteprotected:
162314564Sdim  /// This is the standard constructor.
163314564Sdim  ///
164341825Sdim  /// It creates an empty breakpoint location list. It is protected here
165341825Sdim  /// because only Breakpoints are allowed to create the breakpoint location
166341825Sdim  /// list.
167314564Sdim  BreakpointLocationList(Breakpoint &owner);
168254721Semaste
169314564Sdim  lldb::BreakpointLocationSP Create(const Address &addr,
170314564Sdim                                    bool resolve_indirect_symbols);
171254721Semaste
172314564Sdim  void StartRecordingNewLocations(BreakpointLocationCollection &new_locations);
173254721Semaste
174314564Sdim  void StopRecordingNewLocations();
175254721Semaste
176314564Sdim  lldb::BreakpointLocationSP AddLocation(const Address &addr,
177314564Sdim                                         bool resolve_indirect_symbols,
178314564Sdim                                         bool *new_location = nullptr);
179296417Sdim
180314564Sdim  void SwapLocation(lldb::BreakpointLocationSP to_location_sp,
181314564Sdim                    lldb::BreakpointLocationSP from_location_sp);
182314564Sdim
183314564Sdim  bool RemoveLocation(const lldb::BreakpointLocationSP &bp_loc_sp);
184360784Sdim
185341825Sdim  void RemoveLocationByIndex(size_t idx);
186314564Sdim
187314564Sdim  void RemoveInvalidLocations(const ArchSpec &arch);
188314564Sdim
189314564Sdim  void Compact();
190314564Sdim
191314564Sdim  typedef std::vector<lldb::BreakpointLocationSP> collection;
192314564Sdim  typedef std::map<lldb_private::Address, lldb::BreakpointLocationSP,
193314564Sdim                   Address::ModulePointerAndOffsetLessThanFunctionObject>
194314564Sdim      addr_map;
195314564Sdim
196314564Sdim  Breakpoint &m_owner;
197314564Sdim  collection m_locations; // Vector of locations, sorted by ID
198314564Sdim  addr_map m_address_to_location;
199314564Sdim  mutable std::recursive_mutex m_mutex;
200314564Sdim  lldb::break_id_t m_next_id;
201314564Sdim  BreakpointLocationCollection *m_new_location_recorder;
202314564Sdim
203280031Sdimpublic:
204314564Sdim  typedef AdaptedIterable<collection, lldb::BreakpointLocationSP,
205314564Sdim                          vector_adapter>
206314564Sdim      BreakpointLocationIterable;
207296417Sdim
208314564Sdim  BreakpointLocationIterable BreakpointLocations() {
209314564Sdim    return BreakpointLocationIterable(m_locations);
210314564Sdim  }
211254721Semaste};
212254721Semaste
213254721Semaste} // namespace lldb_private
214254721Semaste
215296417Sdim#endif // liblldb_BreakpointLocationList_h_
216