1254721Semaste//===-- BreakpointSite.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_BreakpointSite_h_
10254721Semaste#define liblldb_BreakpointSite_h_
11254721Semaste
12254721Semaste
13254721Semaste#include <list>
14309124Sdim#include <mutex>
15254721Semaste
16254721Semaste
17314564Sdim#include "lldb/Breakpoint/BreakpointLocationCollection.h"
18314564Sdim#include "lldb/Breakpoint/StoppointLocation.h"
19321369Sdim#include "lldb/Utility/UserID.h"
20288943Sdim#include "lldb/lldb-forward.h"
21254721Semaste
22254721Semastenamespace lldb_private {
23254721Semaste
24353358Sdim/// \class BreakpointSite BreakpointSite.h "lldb/Breakpoint/BreakpointSite.h"
25341825Sdim/// Class that manages the actual breakpoint that will be inserted into the
26341825Sdim/// running program.
27254721Semaste///
28341825Sdim/// The BreakpointSite class handles the physical breakpoint that is actually
29341825Sdim/// inserted in the target program.  As such, it is also the one that  gets
30341825Sdim/// hit, when the program stops. It keeps a list of all BreakpointLocations
31341825Sdim/// that share this physical site. When the breakpoint is hit, all the
32341825Sdim/// locations are informed by the breakpoint site. Breakpoint sites are owned
33341825Sdim/// by the process.
34254721Semaste
35314564Sdimclass BreakpointSite : public std::enable_shared_from_this<BreakpointSite>,
36314564Sdim                       public StoppointLocation {
37254721Semastepublic:
38314564Sdim  enum Type {
39314564Sdim    eSoftware, // Breakpoint opcode has been written to memory and
40314564Sdim               // m_saved_opcode
41314564Sdim               // and m_trap_opcode contain the saved and written opcode.
42314564Sdim    eHardware, // Breakpoint site is set as a hardware breakpoint
43314564Sdim    eExternal  // Breakpoint site is managed by an external debug nub or
44314564Sdim               // debug interface where memory reads transparently will not
45314564Sdim               // display any breakpoint opcodes.
46314564Sdim  };
47254721Semaste
48314564Sdim  ~BreakpointSite() override;
49254721Semaste
50314564Sdim  // This section manages the breakpoint traps
51254721Semaste
52314564Sdim  /// Returns the Opcode Bytes for this breakpoint
53314564Sdim  uint8_t *GetTrapOpcodeBytes();
54254721Semaste
55314564Sdim  /// Returns the Opcode Bytes for this breakpoint - const version
56314564Sdim  const uint8_t *GetTrapOpcodeBytes() const;
57254721Semaste
58314564Sdim  /// Get the size of the trap opcode for this address
59314564Sdim  size_t GetTrapOpcodeMaxByteSize() const;
60254721Semaste
61314564Sdim  /// Sets the trap opcode
62314564Sdim  bool SetTrapOpcode(const uint8_t *trap_opcode, uint32_t trap_opcode_size);
63254721Semaste
64314564Sdim  /// Gets the original instruction bytes that were overwritten by the trap
65314564Sdim  uint8_t *GetSavedOpcodeBytes();
66254721Semaste
67314564Sdim  /// Gets the original instruction bytes that were overwritten by the trap
68314564Sdim  /// const version
69314564Sdim  const uint8_t *GetSavedOpcodeBytes() const;
70254721Semaste
71314564Sdim  /// Says whether \a addr and size \a size intersects with the address \a
72314564Sdim  /// intersect_addr
73314564Sdim  bool IntersectsRange(lldb::addr_t addr, size_t size,
74314564Sdim                       lldb::addr_t *intersect_addr, size_t *intersect_size,
75314564Sdim                       size_t *opcode_offset) const;
76254721Semaste
77314564Sdim  /// Tells whether the current breakpoint site is enabled or not
78314564Sdim  ///
79314564Sdim  /// This is a low-level enable bit for the breakpoint sites.  If a
80341825Sdim  /// breakpoint site has no enabled owners, it should just get removed.  This
81341825Sdim  /// enable/disable is for the low-level target code to enable and disable
82341825Sdim  /// breakpoint sites when single stepping, etc.
83314564Sdim  bool IsEnabled() const;
84254721Semaste
85314564Sdim  /// Sets whether the current breakpoint site is enabled or not
86314564Sdim  ///
87353358Sdim  /// \param[in] enabled
88314564Sdim  ///    \b true if the breakpoint is enabled, \b false otherwise.
89314564Sdim  void SetEnabled(bool enabled);
90254721Semaste
91314564Sdim  /// Enquires of the breakpoint locations that produced this breakpoint site
92341825Sdim  /// whether we should stop at this location.
93314564Sdim  ///
94353358Sdim  /// \param[in] context
95314564Sdim  ///    This contains the information about this stop.
96314564Sdim  ///
97353358Sdim  /// \return
98314564Sdim  ///    \b true if we should stop, \b false otherwise.
99314564Sdim  bool ShouldStop(StoppointCallbackContext *context) override;
100254721Semaste
101314564Sdim  /// Standard Dump method
102314564Sdim  void Dump(Stream *s) const override;
103254721Semaste
104341825Sdim  /// The "Owners" are the breakpoint locations that share this breakpoint
105341825Sdim  /// site. The method adds the \a owner to this breakpoint site's owner list.
106314564Sdim  ///
107360784Sdim  /// \param[in] owner
108314564Sdim  ///    \a owner is the Breakpoint Location to add.
109314564Sdim  void AddOwner(const lldb::BreakpointLocationSP &owner);
110254721Semaste
111341825Sdim  /// This method returns the number of breakpoint locations currently located
112341825Sdim  /// at this breakpoint site.
113314564Sdim  ///
114353358Sdim  /// \return
115314564Sdim  ///    The number of owners.
116314564Sdim  size_t GetNumberOfOwners();
117254721Semaste
118341825Sdim  /// This method returns the breakpoint location at index \a index located at
119341825Sdim  /// this breakpoint site.  The owners are listed ordinally from 0 to
120341825Sdim  /// GetNumberOfOwners() - 1 so you can use this method to iterate over the
121341825Sdim  /// owners
122314564Sdim  ///
123360784Sdim  /// \param[in] idx
124314564Sdim  ///     The index in the list of owners for which you wish the owner location.
125360784Sdim  ///
126353358Sdim  /// \return
127314564Sdim  ///    A shared pointer to the breakpoint location at that index.
128314564Sdim  lldb::BreakpointLocationSP GetOwnerAtIndex(size_t idx);
129254721Semaste
130314564Sdim  /// This method copies the breakpoint site's owners into a new collection.
131314564Sdim  /// It does this while the owners mutex is locked.
132314564Sdim  ///
133353358Sdim  /// \param[out] out_collection
134314564Sdim  ///    The BreakpointLocationCollection into which to put the owners
135314564Sdim  ///    of this breakpoint site.
136314564Sdim  ///
137353358Sdim  /// \return
138314564Sdim  ///    The number of elements copied into out_collection.
139314564Sdim  size_t CopyOwnersList(BreakpointLocationCollection &out_collection);
140254721Semaste
141341825Sdim  /// Check whether the owners of this breakpoint site have any thread
142341825Sdim  /// specifiers, and if yes, is \a thread contained in any of these
143341825Sdim  /// specifiers.
144314564Sdim  ///
145353358Sdim  /// \param[in] thread
146314564Sdim  ///     The thread against which to test.
147314564Sdim  ///
148314564Sdim  /// return
149314564Sdim  ///     \b true if the collection contains at least one location that
150314564Sdim  ///     would be valid for this thread, false otherwise.
151314564Sdim  bool ValidForThisThread(Thread *thread);
152254721Semaste
153314564Sdim  /// Print a description of this breakpoint site to the stream \a s.
154341825Sdim  /// GetDescription tells you about the breakpoint site's owners. Use
155341825Sdim  /// BreakpointSite::Dump(Stream *) to get information about the breakpoint
156341825Sdim  /// site itself.
157314564Sdim  ///
158353358Sdim  /// \param[in] s
159314564Sdim  ///     The stream to which to print the description.
160314564Sdim  ///
161353358Sdim  /// \param[in] level
162314564Sdim  ///     The description level that indicates the detail level to
163314564Sdim  ///     provide.
164314564Sdim  ///
165353358Sdim  /// \see lldb::DescriptionLevel
166314564Sdim  void GetDescription(Stream *s, lldb::DescriptionLevel level);
167254721Semaste
168314564Sdim  /// Tell whether a breakpoint has a location at this site.
169314564Sdim  ///
170353358Sdim  /// \param[in] bp_id
171314564Sdim  ///     The breakpoint id to query.
172314564Sdim  ///
173353358Sdim  /// \result
174314564Sdim  ///     \b true if bp_id has a location that is at this site,
175314564Sdim  ///     \b false otherwise.
176314564Sdim  bool IsBreakpointAtThisSite(lldb::break_id_t bp_id);
177254721Semaste
178341825Sdim  /// Tell whether ALL the breakpoints in the location collection are
179341825Sdim  /// internal.
180314564Sdim  ///
181353358Sdim  /// \result
182314564Sdim  ///     \b true if all breakpoint locations are owned by internal breakpoints,
183314564Sdim  ///     \b false otherwise.
184314564Sdim  bool IsInternal() const;
185254721Semaste
186314564Sdim  BreakpointSite::Type GetType() const { return m_type; }
187314564Sdim
188314564Sdim  void SetType(BreakpointSite::Type type) { m_type = type; }
189314564Sdim
190254721Semasteprivate:
191314564Sdim  friend class Process;
192314564Sdim  friend class BreakpointLocation;
193341825Sdim  // The StopInfoBreakpoint knows when it is processing a hit for a thread for
194341825Sdim  // a site, so let it be the one to manage setting the location hit count once
195341825Sdim  // and only once.
196314564Sdim  friend class StopInfoBreakpoint;
197254721Semaste
198314564Sdim  void BumpHitCounts();
199280031Sdim
200341825Sdim  /// The method removes the owner at \a break_loc_id from this breakpoint
201341825Sdim  /// list.
202314564Sdim  size_t RemoveOwner(lldb::break_id_t break_id, lldb::break_id_t break_loc_id);
203254721Semaste
204314564Sdim  BreakpointSite::Type m_type; ///< The type of this breakpoint site.
205314564Sdim  uint8_t m_saved_opcode[8]; ///< The saved opcode bytes if this breakpoint site
206314564Sdim                             ///uses trap opcodes.
207314564Sdim  uint8_t m_trap_opcode[8];  ///< The opcode that was used to create the
208314564Sdim                             ///breakpoint if it is a software breakpoint site.
209314564Sdim  bool
210314564Sdim      m_enabled; ///< Boolean indicating if this breakpoint site enabled or not.
211254721Semaste
212341825Sdim  // Consider adding an optimization where if there is only one owner, we don't
213341825Sdim  // store a list.  The usual case will be only one owner...
214314564Sdim  BreakpointLocationCollection m_owners; ///< This has the BreakpointLocations
215314564Sdim                                         ///that share this breakpoint site.
216314564Sdim  std::recursive_mutex
217314564Sdim      m_owners_mutex; ///< This mutex protects the owners collection.
218254721Semaste
219314564Sdim  static lldb::break_id_t GetNextID();
220254721Semaste
221314564Sdim  // Only the Process can create breakpoint sites in
222314564Sdim  // Process::CreateBreakpointSite (lldb::BreakpointLocationSP &, bool).
223314564Sdim  BreakpointSite(BreakpointSiteList *list,
224314564Sdim                 const lldb::BreakpointLocationSP &owner, lldb::addr_t m_addr,
225314564Sdim                 bool use_hardware);
226254721Semaste
227314564Sdim  DISALLOW_COPY_AND_ASSIGN(BreakpointSite);
228254721Semaste};
229254721Semaste
230254721Semaste} // namespace lldb_private
231254721Semaste
232296417Sdim#endif // liblldb_BreakpointSite_h_
233