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