LLDB mainline
BreakpointLocation.h
Go to the documentation of this file.
1//===-- BreakpointLocation.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_BREAKPOINTLOCATION_H
10#define LLDB_BREAKPOINT_BREAKPOINTLOCATION_H
11
12#include <memory>
13#include <mutex>
14#include <optional>
15
18#include "lldb/Core/Address.h"
20#include "lldb/Utility/UserID.h"
21#include "lldb/lldb-private.h"
22
23namespace lldb_private {
24
25/// \class BreakpointLocation BreakpointLocation.h
26/// "lldb/Breakpoint/BreakpointLocation.h" Class that manages one unique (by
27/// address) instance of a logical breakpoint.
28
29/// General Outline:
30/// A breakpoint location is defined by the breakpoint that produces it,
31/// and the address that resulted in this particular instantiation. Each
32/// breakpoint location also may have a breakpoint site if its address has
33/// been loaded into the program. Finally it has a settable options object.
34///
35/// FIXME: Should we also store some fingerprint for the location, so
36/// we can map one location to the "equivalent location" on rerun? This would
37/// be useful if you've set options on the locations.
38
40 : public std::enable_shared_from_this<BreakpointLocation> {
41 friend class BreakpointSite;
43 friend class Breakpoint;
44 friend class Process;
45 friend class StopInfoBreakpoint;
46
47public:
49
50 /// Gets the load address for this breakpoint location \return
51 /// Returns breakpoint location load address, \b
52 /// LLDB_INVALID_ADDRESS if not yet set.
54
55 /// Gets the Address for this breakpoint location \return
56 /// Returns breakpoint location Address.
58 /// Gets the Breakpoint that created this breakpoint location \return
59 /// Returns the owning breakpoint.
61
63
64 /// This is a programmatic version of a breakpoint "condition". When a
65 /// breakpoint is hit, WasHit will get called before the synchronous
66 /// ShouldStop callback is run, and if it returns an empty
67 /// BreakpointLocationSP, lldb will act as if that breakpoint wasn't hit.
68 ///
69 /// \param[in] context
70 /// The context at the stop point
71 ///
72 /// \return
73 /// This will return the breakpoint location that was hit on this stop.
74 /// If there was no facade location this will be the original location.
75 /// If the shared pointer is empty, then we'll treat it as if the
76 /// breakpoint was not hit.
78
79 /// Determines whether we should stop due to a hit at this breakpoint
80 /// location.
81 ///
82 /// Side Effects: This may evaluate the breakpoint condition, and run the
83 /// callback. So this command may do a considerable amount of work.
84 ///
85 /// \param[in] context
86 /// The context at the stop point
87 ///
88 /// \param[out] facade_loc_sp
89 /// If this stop should be attributed not to the location that was hit, but
90 /// to a facade location, it will be returned in this facade_loc_sp.
91 ///
92 /// \return
93 /// \b true if this breakpoint location thinks we should stop,
94 /// \b false otherwise.
96 lldb::BreakpointLocationSP &facade_loc_sp);
97
98 // The next section deals with various breakpoint options.
99
100 /// If \a enabled is \b true, enable the breakpoint, if \b false disable it.
101 llvm::Error SetEnabled(bool enabled);
102
103 /// Check the Enable/Disable state.
104 ///
105 /// \return
106 /// \b true if the breakpoint is enabled, \b false if disabled.
107 bool IsEnabled() const;
108
109 /// If \a auto_continue is \b true, set the breakpoint to continue when hit.
110 void SetAutoContinue(bool auto_continue);
111
112 /// Check the AutoContinue state.
113 ///
114 /// \return
115 /// \b true if the breakpoint is set to auto-continue, \b false if not.
116 bool IsAutoContinue() const;
117
118 /// Return the current Hit Count.
119 uint32_t GetHitCount() const { return m_hit_counter.GetValue(); }
120
121 /// Resets the current Hit Count.
122 void ResetHitCount() { m_hit_counter.Reset(); }
123
124 /// Return the current Ignore Count.
125 ///
126 /// \return
127 /// The number of breakpoint hits to be ignored.
128 uint32_t GetIgnoreCount() const;
129
130 /// Set the breakpoint to ignore the next \a count breakpoint hits.
131 ///
132 /// \param[in] n
133 /// The number of breakpoint hits to ignore.
134 void SetIgnoreCount(uint32_t n);
135
136 /// Set the callback action invoked when the breakpoint is hit.
137 ///
138 /// The callback will return a bool indicating whether the target should
139 /// stop at this breakpoint or not.
140 ///
141 /// \param[in] callback
142 /// The method that will get called when the breakpoint is hit.
143 ///
144 /// \param[in] callback_baton_sp
145 /// A shared pointer to a Baton that provides the void * needed
146 /// for the callback.
147 ///
148 /// \see lldb_private::Baton
150 const lldb::BatonSP &callback_baton_sp, bool is_synchronous);
151
152 void SetCallback(BreakpointHitCallback callback, void *baton,
153 bool is_synchronous);
154
155 void ClearCallback();
156
157 /// Set the breakpoint location's condition.
158 ///
159 /// \param[in] condition
160 /// The condition to evaluate when the breakpoint is hit.
161 void SetCondition(StopCondition condition);
162
163 /// Return the breakpoint condition.
164 const StopCondition &GetCondition() const;
165
167
168 /// Set the valid thread to be checked when the breakpoint is hit.
169 ///
170 /// \param[in] thread_id
171 /// If this thread hits the breakpoint, we stop, otherwise not.
172 void SetThreadID(lldb::tid_t thread_id);
173
175
176 void SetThreadIndex(uint32_t index);
177
178 uint32_t GetThreadIndex() const;
179
180 void SetThreadName(const char *thread_name);
181
182 const char *GetThreadName() const;
183
184 void SetQueueName(const char *queue_name);
185
186 const char *GetQueueName() const;
187
188 // The next section deals with this location's breakpoint sites.
189
190 /// Try to resolve the breakpoint site for this location.
191 llvm::Error ResolveBreakpointSite();
192
193 /// Clear this breakpoint location's breakpoint site - for instance when
194 /// disabling the breakpoint.
195 llvm::Error ClearBreakpointSite();
196
197 /// Return whether this breakpoint location has a breakpoint site. \return
198 /// \b true if there was a breakpoint site for this breakpoint
199 /// location, \b false otherwise.
200 bool IsResolved() const;
201
203
204 // The next section are generic report functions.
205
206 /// Print a description of this breakpoint location to the stream \a s.
207 ///
208 /// \param[in] s
209 /// The stream to which to print the description.
210 ///
211 /// \param[in] level
212 /// The description level that indicates the detail level to
213 /// provide.
214 ///
215 /// \see lldb::DescriptionLevel
217
218 /// Standard "Dump" method. At present it does nothing.
219 void Dump(Stream *s) const;
220
221 /// Use this to set location specific breakpoint options.
222 ///
223 /// It will create a copy of the containing breakpoint's options if that
224 /// hasn't been done already
225 ///
226 /// \return
227 /// A reference to the breakpoint options.
229
230 /// Use this to access breakpoint options from this breakpoint location.
231 /// This will return the options that have a setting for the specified
232 /// BreakpointOptions kind.
233 ///
234 /// \param[in] kind
235 /// The particular option you are looking up.
236 /// \return
237 /// A pointer to the containing breakpoint's options if this
238 /// location doesn't have its own copy.
239 const BreakpointOptions &
241
242 bool ValidForThisThread(Thread &thread);
243
244 /// Invoke the callback action when the breakpoint is hit.
245 ///
246 /// Meant to be used by the BreakpointLocation class.
247 ///
248 /// \param[in] context
249 /// Described the breakpoint event.
250 ///
251 /// \return
252 /// \b true if the target should stop at this breakpoint and \b
253 /// false not.
255
256 /// Report whether the callback for this location is synchronous or not.
257 ///
258 /// \return
259 /// \b true if the callback is synchronous and \b false if not.
261
262 /// Returns whether we should resolve Indirect functions in setting the
263 /// breakpoint site for this location.
264 ///
265 /// \return
266 /// \b true if the breakpoint SITE for this location should be set on the
267 /// resolved location for Indirect functions.
271
272 /// Returns whether the address set in the breakpoint site for this location
273 /// was found by resolving an indirect symbol.
274 ///
275 /// \return
276 /// \b true or \b false as given in the description above.
277 bool IsIndirect() { return m_is_indirect; }
278
279 void SetIsIndirect(bool is_indirect) { m_is_indirect = is_indirect; }
280
281 /// Returns whether the address set in the breakpoint location was re-routed
282 /// to the target of a re-exported symbol.
283 ///
284 /// \return
285 /// \b true or \b false as given in the description above.
286 bool IsReExported() { return m_is_reexported; }
287
288 void SetIsReExported(bool is_reexported) { m_is_reexported = is_reexported; }
289
290 /// Returns whether the two breakpoint locations might represent "equivalent
291 /// locations". This is used when modules changed to determine if a Location
292 /// in the old module might be the "same as" the input location.
293 ///
294 /// \param[in] location
295 /// The location to compare against.
296 ///
297 /// \return
298 /// \b true or \b false as given in the description above.
300
301 /// Returns the breakpoint location ID.
302 lldb::break_id_t GetID() const { return m_loc_id; }
303
304 /// Set the line entry that should be shown to users for this location.
305 /// It is up to the caller to verify that this is a valid entry to show.
306 /// The current use of this is to distinguish among line entries from a
307 /// virtual inlined call stack that all share the same address.
308 /// The line entry must have the same start address as the address for this
309 /// location.
310 bool SetPreferredLineEntry(const LineEntry &line_entry) {
311 if (m_address == line_entry.range.GetBaseAddress()) {
312 m_preferred_line_entry = line_entry;
313 return true;
314 }
315 assert(0 && "Tried to set a preferred line entry with a different address");
316 return false;
317 }
318
319 const std::optional<LineEntry> GetPreferredLineEntry() {
321 }
322
323protected:
324 /// Set the breakpoint site for this location to \a bp_site_sp.
325 ///
326 /// \param[in] bp_site_sp
327 /// The breakpoint site we are setting for this location.
328 ///
329 /// \return
330 /// \b true if we were successful at setting the breakpoint site,
331 /// \b false otherwise.
333
335
336 /// BreakpointLocation::IgnoreCountShouldStop can only be called once
337 /// per stop. This method checks first against the loc and then the owner.
338 /// It also takes care of decrementing the ignore counters.
339 /// If it returns false we should continue, otherwise stop.
341
342 /// If this location knows that the virtual stack frame it represents is
343 /// not frame 0, return the suggested stack frame instead. This will happen
344 /// when the location's address contains a "virtual inlined call stack" and
345 /// the breakpoint was set on a file & line that are not at the bottom of that
346 /// stack. For now we key off the "preferred line entry" - looking for that
347 /// in the blocks that start with the stop PC.
348 /// This version of the API doesn't take an "inlined" parameter because it
349 /// only changes frames in the inline stack.
350 std::optional<uint32_t> GetSuggestedStackFrameIndex();
351
352private:
354
355 void BumpHitCount();
356
357 void UndoBumpHitCount();
358
359 /// Updates the thread ID internally.
360 ///
361 /// This method was created to handle actually mutating the thread ID
362 /// internally because SetThreadID broadcasts an event in addition to mutating
363 /// state. The constructor calls this instead of SetThreadID to avoid the
364 /// broadcast.
365 ///
366 /// \param[in] thread_id
367 /// The new thread ID.
368 void SetThreadIDInternal(lldb::tid_t thread_id);
369
370 // Constructors and Destructors
371 //
372 // Only the Breakpoint can make breakpoint locations, and it owns them.
373 /// Constructor.
374 ///
375 /// \param[in] loc_id
376 /// The location id of the new location.
377 ///
378 /// \param[in] owner
379 /// A back pointer to the breakpoint that owns this location.
380 ///
381 /// \param[in] addr
382 /// The Address defining this location.
383 ///
384 /// \param[in] tid
385 /// The thread for which this breakpoint location is valid, or
386 /// LLDB_INVALID_THREAD_ID if it is valid for all threads.
387 ///
389 const Address &addr, lldb::tid_t tid,
390 bool check_for_resolver = true);
391
392 /// This is the constructor for locations with no address. Currently this is
393 /// just used for Facade locations.
394 ///
395 /// \param[in] loc_id
396 /// The location id of the new location.
397 ///
398 /// \param[in] owner
399 /// A back pointer to the breakpoint that owns this location.
400 ///
401 ///
402public:
404 bool IsValid() const { return m_is_valid; }
405 bool IsFacade() const { return m_is_facade; }
406
407private:
408 // Data members:
412 ///< The address defining this location.
414 ///< The breakpoint that produced this object.
416 ///< Breakpoint options pointer, nullptr if we're using our breakpoint's
417 /// options.
418 std::unique_ptr<BreakpointOptions> m_options_up;
419 ///< Our breakpoint site (it may be shared by more than one location.)
421 ///< The compiled expression to use in testing our condition.
423 ///< Guards parsing and evaluation of the condition, which could be evaluated
424 /// by multiple processes.
426 ///< For testing whether the condition source code changed.
428 ///< Breakpoint location ID.
430 ///< Number of times this breakpoint location has been hit.
432 /// If this exists, use it to print the stop description rather than the
433 /// LineEntry m_address resolves to directly. Use this for instance when the
434 /// location was given somewhere in the virtual inlined call stack since the
435 /// Address always resolves to the lowest entry in the stack.
436 std::optional<LineEntry> m_preferred_line_entry;
437 /// Because Facade locations don't have sites we can't use the presence of
438 /// the site to mean this breakpoint is valid, but must manage the state
439 /// directly.
440 bool m_is_valid = true;
441 /// Facade locations aren't directly triggered and don't have a breakpoint
442 /// site. They are a useful fiction when you want to represent the stop
443 /// location as something lldb can't naturally stop at.
444 bool m_is_facade = false;
445
446 void SetInvalid() { m_is_valid = false; }
447
448 void SetShouldResolveIndirectFunctions(bool do_resolve) {
450 }
451
452 void SendBreakpointLocationChangedEvent(lldb::BreakpointEventType eventKind);
453
456};
457
458} // namespace lldb_private
459
460#endif // LLDB_BREAKPOINT_BREAKPOINTLOCATION_H
static llvm::raw_ostream & error(Stream &strm)
Address & GetBaseAddress()
Get accessor for the base address of the range.
A section + offset based address class.
Definition Address.h:62
std::optional< LineEntry > m_preferred_line_entry
If this exists, use it to print the stop description rather than the LineEntry m_address resolves to ...
size_t m_condition_hash
Breakpoint location ID.
void SetCondition(StopCondition condition)
Set the breakpoint location's condition.
const std::optional< LineEntry > GetPreferredLineEntry()
void SetShouldResolveIndirectFunctions(bool do_resolve)
void SwapLocation(lldb::BreakpointLocationSP swap_from)
BreakpointLocation(lldb::break_id_t loc_id, Breakpoint &owner, const Address &addr, lldb::tid_t tid, bool check_for_resolver=true)
Constructor.
bool IsIndirect()
Returns whether the address set in the breakpoint site for this location was found by resolving an in...
uint32_t GetHitCount() const
Return the current Hit Count.
std::mutex m_condition_mutex
For testing whether the condition source code changed.
bool ShouldResolveIndirectFunctions()
Returns whether we should resolve Indirect functions in setting the breakpoint site for this location...
lldb::BreakpointSiteSP m_bp_site_sp
The compiled expression to use in testing our condition.
llvm::Error ClearBreakpointSite()
Clear this breakpoint location's breakpoint site - for instance when disabling the breakpoint.
Breakpoint & m_owner
Breakpoint options pointer, nullptr if we're using our breakpoint's options.
bool SetBreakpointSite(lldb::BreakpointSiteSP &bp_site_sp)
Set the breakpoint site for this location to bp_site_sp.
void SetIgnoreCount(uint32_t n)
Set the breakpoint to ignore the next count breakpoint hits.
void SetThreadIDInternal(lldb::tid_t thread_id)
Updates the thread ID internally.
void SetQueueName(const char *queue_name)
BreakpointLocation(const BreakpointLocation &)=delete
llvm::Error SetEnabled(bool enabled)
If enabled is true, enable the breakpoint, if false disable it.
bool IsReExported()
Returns whether the address set in the breakpoint location was re-routed to the target of a re-export...
BreakpointLocation(lldb::break_id_t loc_id, Breakpoint &owner)
This is the constructor for locations with no address.
lldb::addr_t GetLoadAddress() const
Gets the load address for this breakpoint location.
BreakpointOptions & GetLocationOptions()
Use this to set location specific breakpoint options.
bool InvokeCallback(StoppointCallbackContext *context)
Invoke the callback action when the breakpoint is hit.
void GetDescription(Stream *s, lldb::DescriptionLevel level)
Print a description of this breakpoint location to the stream s.
Address m_address
The breakpoint that produced this object.
bool IsEnabled() const
Check the Enable/Disable state.
bool IgnoreCountShouldStop()
BreakpointLocation::IgnoreCountShouldStop can only be called once per stop.
const BreakpointLocation & operator=(const BreakpointLocation &)=delete
const StopCondition & GetCondition() const
Return the breakpoint condition.
bool IsResolved() const
Return whether this breakpoint location has a breakpoint site.
lldb::break_id_t GetID() const
Returns the breakpoint location ID.
void Dump(Stream *s) const
Standard "Dump" method. At present it does nothing.
bool IsCallbackSynchronous()
Report whether the callback for this location is synchronous or not.
void SendBreakpointLocationChangedEvent(lldb::BreakpointEventType eventKind)
void SetIsReExported(bool is_reexported)
void SetAutoContinue(bool auto_continue)
If auto_continue is true, set the breakpoint to continue when hit.
bool m_is_facade
Facade locations aren't directly triggered and don't have a breakpoint site.
void SetThreadID(lldb::tid_t thread_id)
Set the valid thread to be checked when the breakpoint is hit.
lldb::UserExpressionSP m_user_expression_sp
Guards parsing and evaluation of the condition, which could be evaluated by multiple processes.
void SetCallback(BreakpointHitCallback callback, const lldb::BatonSP &callback_baton_sp, bool is_synchronous)
Set the callback action invoked when the breakpoint is hit.
uint32_t GetIgnoreCount() const
Return the current Ignore Count.
bool m_is_indirect
The address defining this location.
bool ConditionSaysStop(ExecutionContext &exe_ctx, Status &error)
bool EquivalentToLocation(BreakpointLocation &location)
Returns whether the two breakpoint locations might represent "equivalentlocations".
std::unique_ptr< BreakpointOptions > m_options_up
Our breakpoint site (it may be shared by more than one location.)
Address & GetAddress()
Gets the Address for this breakpoint location.
bool IsAutoContinue() const
Check the AutoContinue state.
std::optional< uint32_t > GetSuggestedStackFrameIndex()
If this location knows that the virtual stack frame it represents is not frame 0, return the suggeste...
void ResetHitCount()
Resets the current Hit Count.
bool m_is_valid
Because Facade locations don't have sites we can't use the presence of the site to mean this breakpoi...
void SetThreadName(const char *thread_name)
Breakpoint & GetBreakpoint()
Gets the Breakpoint that created this breakpoint location.
const BreakpointOptions & GetOptionsSpecifyingKind(BreakpointOptions::OptionKind kind) const
Use this to access breakpoint options from this breakpoint location.
llvm::Error ResolveBreakpointSite()
Try to resolve the breakpoint site for this location.
lldb::BreakpointLocationSP WasHit(StoppointCallbackContext *context)
This is a programmatic version of a breakpoint "condition".
lldb::BreakpointSiteSP GetBreakpointSite() const
lldb::break_id_t m_loc_id
Number of times this breakpoint location has been hit.
bool SetPreferredLineEntry(const LineEntry &line_entry)
Set the line entry that should be shown to users for this location.
bool ShouldStop(StoppointCallbackContext *context, lldb::BreakpointLocationSP &facade_loc_sp)
Determines whether we should stop due to a hit at this breakpoint location.
"lldb/Breakpoint/BreakpointOptions.h" Class that manages the options on a breakpoint or breakpoint lo...
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
An error handling class.
Definition Status.h:118
General Outline: When we hit a breakpoint we need to package up whatever information is needed to eva...
A stream class that can stream formatted output to a file.
Definition Stream.h:28
A class that represents a running process on the host machine.
std::function< bool(void *baton, StoppointCallbackContext *context, lldb::user_id_t break_id, lldb::user_id_t break_loc_id)> BreakpointHitCallback
std::shared_ptr< lldb_private::BreakpointSite > BreakpointSiteSP
std::shared_ptr< lldb_private::BreakpointLocation > BreakpointLocationSP
DescriptionLevel
Description levels for "void GetDescription(Stream *, DescriptionLevel)" calls.
std::shared_ptr< lldb_private::UserExpression > UserExpressionSP
int32_t break_id_t
Definition lldb-types.h:86
std::shared_ptr< lldb_private::Baton > BatonSP
uint64_t addr_t
Definition lldb-types.h:80
uint64_t tid_t
Definition lldb-types.h:84
A line table entry class.
Definition LineEntry.h:21
AddressRange range
The section offset address range for this line entry.
Definition LineEntry.h:137