blob: 2ec8dc8005b48089cbaa6de66d1c89345d6f42b7 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- BreakpointLocation.cpp ----------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10// C Includes
11// C++ Includes
Chris Lattner30fdc8d2010-06-08 16:52:24 +000012// Other libraries and framework includes
13// Project includes
14#include "lldb/Breakpoint/BreakpointLocation.h"
15#include "lldb/Breakpoint/BreakpointID.h"
16#include "lldb/Breakpoint/StoppointCallbackContext.h"
Jim Ingham5b52f0c2011-06-02 23:58:26 +000017#include "lldb/Core/Debugger.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000018#include "lldb/Core/Log.h"
Greg Clayton1f746072012-08-29 21:13:06 +000019#include "lldb/Core/Module.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000020#include "lldb/Core/StreamString.h"
Zachary Turnera78bd7f2015-03-03 23:11:11 +000021#include "lldb/Core/ValueObject.h"
Bruce Mitchener937e3962015-09-21 16:56:08 +000022#include "lldb/Expression/ExpressionVariable.h"
Jim Ingham151c0322015-09-15 21:13:50 +000023#include "lldb/Expression/UserExpression.h"
Greg Clayton1f746072012-08-29 21:13:06 +000024#include "lldb/Symbol/CompileUnit.h"
25#include "lldb/Symbol/Symbol.h"
Jim Ingham151c0322015-09-15 21:13:50 +000026#include "lldb/Symbol/TypeSystem.h"
Greg Clayton1f746072012-08-29 21:13:06 +000027#include "lldb/Target/Target.h"
28#include "lldb/Target/Process.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000029#include "lldb/Target/Thread.h"
Jim Ingham1b54c882010-06-16 02:00:15 +000030#include "lldb/Target/ThreadSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000031
32using namespace lldb;
33using namespace lldb_private;
34
35BreakpointLocation::BreakpointLocation
36(
37 break_id_t loc_id,
38 Breakpoint &owner,
Greg Claytonc0d34462011-02-05 00:38:04 +000039 const Address &addr,
Chris Lattner30fdc8d2010-06-08 16:52:24 +000040 lldb::tid_t tid,
Jim Ingham1460e4b2014-01-10 23:46:59 +000041 bool hardware,
42 bool check_for_resolver
Chris Lattner30fdc8d2010-06-08 16:52:24 +000043) :
Greg Claytonf3ef3d22011-05-22 22:46:53 +000044 StoppointLocation (loc_id, addr.GetOpcodeLoadAddress(&owner.GetTarget()), hardware),
Jim Inghame6bc6cb2012-02-08 05:23:15 +000045 m_being_created(true),
Jim Ingham1460e4b2014-01-10 23:46:59 +000046 m_should_resolve_indirect_functions (false),
47 m_is_reexported (false),
48 m_is_indirect (false),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000049 m_address (addr),
50 m_owner (owner),
51 m_options_ap (),
Sean Callananb4987e32013-06-06 20:18:50 +000052 m_bp_site_sp (),
53 m_condition_mutex ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +000054{
Jim Ingham1460e4b2014-01-10 23:46:59 +000055 if (check_for_resolver)
56 {
57 Symbol *symbol = m_address.CalculateSymbolContextSymbol();
58 if (symbol && symbol->IsIndirect())
59 {
60 SetShouldResolveIndirectFunctions (true);
61 }
62 }
63
Jim Ingham1b54c882010-06-16 02:00:15 +000064 SetThreadID (tid);
Jim Inghame6bc6cb2012-02-08 05:23:15 +000065 m_being_created = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000066}
67
68BreakpointLocation::~BreakpointLocation()
69{
70 ClearBreakpointSite();
71}
72
73lldb::addr_t
Greg Clayton13238c42010-06-14 04:18:27 +000074BreakpointLocation::GetLoadAddress () const
Chris Lattner30fdc8d2010-06-08 16:52:24 +000075{
Greg Claytonf3ef3d22011-05-22 22:46:53 +000076 return m_address.GetOpcodeLoadAddress (&m_owner.GetTarget());
Chris Lattner30fdc8d2010-06-08 16:52:24 +000077}
78
79Address &
80BreakpointLocation::GetAddress ()
81{
82 return m_address;
83}
84
85Breakpoint &
86BreakpointLocation::GetBreakpoint ()
87{
88 return m_owner;
89}
90
Jim Ingham151c0322015-09-15 21:13:50 +000091Target &
92BreakpointLocation::GetTarget()
93{
94 return m_owner.GetTarget();
95}
96
Chris Lattner30fdc8d2010-06-08 16:52:24 +000097bool
Johnny Chenfdad6792012-02-01 19:05:20 +000098BreakpointLocation::IsEnabled () const
Chris Lattner30fdc8d2010-06-08 16:52:24 +000099{
Johnny Chen50df1f92012-01-30 22:48:10 +0000100 if (!m_owner.IsEnabled())
101 return false;
Eugene Zelenko16fd7512015-10-30 18:50:12 +0000102 else if (m_options_ap.get() != nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000103 return m_options_ap->IsEnabled();
104 else
Johnny Chen50df1f92012-01-30 22:48:10 +0000105 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000106}
107
108void
109BreakpointLocation::SetEnabled (bool enabled)
110{
111 GetLocationOptions()->SetEnabled(enabled);
112 if (enabled)
113 {
114 ResolveBreakpointSite();
115 }
116 else
117 {
118 ClearBreakpointSite();
119 }
Jim Inghame6bc6cb2012-02-08 05:23:15 +0000120 SendBreakpointLocationChangedEvent (enabled ? eBreakpointEventTypeEnabled : eBreakpointEventTypeDisabled);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000121}
122
123void
124BreakpointLocation::SetThreadID (lldb::tid_t thread_id)
125{
Jim Ingham1b54c882010-06-16 02:00:15 +0000126 if (thread_id != LLDB_INVALID_THREAD_ID)
127 GetLocationOptions()->SetThreadID(thread_id);
128 else
129 {
130 // If we're resetting this to an invalid thread id, then
131 // don't make an options pointer just to do that.
Eugene Zelenko16fd7512015-10-30 18:50:12 +0000132 if (m_options_ap.get() != nullptr)
Jim Ingham1b54c882010-06-16 02:00:15 +0000133 m_options_ap->SetThreadID (thread_id);
134 }
Jim Inghame6bc6cb2012-02-08 05:23:15 +0000135 SendBreakpointLocationChangedEvent (eBreakpointEventTypeThreadChanged);
136}
137
138lldb::tid_t
139BreakpointLocation::GetThreadID ()
140{
141 if (GetOptionsNoCreate()->GetThreadSpecNoCreate())
142 return GetOptionsNoCreate()->GetThreadSpecNoCreate()->GetTID();
143 else
144 return LLDB_INVALID_THREAD_ID;
145}
146
147void
148BreakpointLocation::SetThreadIndex (uint32_t index)
149{
150 if (index != 0)
151 GetLocationOptions()->GetThreadSpec()->SetIndex(index);
152 else
153 {
154 // If we're resetting this to an invalid thread id, then
155 // don't make an options pointer just to do that.
Eugene Zelenko16fd7512015-10-30 18:50:12 +0000156 if (m_options_ap.get() != nullptr)
Jim Inghame6bc6cb2012-02-08 05:23:15 +0000157 m_options_ap->GetThreadSpec()->SetIndex(index);
158 }
159 SendBreakpointLocationChangedEvent (eBreakpointEventTypeThreadChanged);
Jim Inghame6bc6cb2012-02-08 05:23:15 +0000160}
161
162uint32_t
163BreakpointLocation::GetThreadIndex() const
164{
165 if (GetOptionsNoCreate()->GetThreadSpecNoCreate())
166 return GetOptionsNoCreate()->GetThreadSpecNoCreate()->GetIndex();
167 else
168 return 0;
169}
170
171void
172BreakpointLocation::SetThreadName (const char *thread_name)
173{
Eugene Zelenko16fd7512015-10-30 18:50:12 +0000174 if (thread_name != nullptr)
Jim Inghame6bc6cb2012-02-08 05:23:15 +0000175 GetLocationOptions()->GetThreadSpec()->SetName(thread_name);
176 else
177 {
178 // If we're resetting this to an invalid thread id, then
179 // don't make an options pointer just to do that.
Eugene Zelenko16fd7512015-10-30 18:50:12 +0000180 if (m_options_ap.get() != nullptr)
Jim Inghame6bc6cb2012-02-08 05:23:15 +0000181 m_options_ap->GetThreadSpec()->SetName(thread_name);
182 }
183 SendBreakpointLocationChangedEvent (eBreakpointEventTypeThreadChanged);
184}
185
186const char *
187BreakpointLocation::GetThreadName () const
188{
189 if (GetOptionsNoCreate()->GetThreadSpecNoCreate())
190 return GetOptionsNoCreate()->GetThreadSpecNoCreate()->GetName();
191 else
Eugene Zelenko16fd7512015-10-30 18:50:12 +0000192 return nullptr;
Jim Inghame6bc6cb2012-02-08 05:23:15 +0000193}
194
195void
196BreakpointLocation::SetQueueName (const char *queue_name)
197{
Eugene Zelenko16fd7512015-10-30 18:50:12 +0000198 if (queue_name != nullptr)
Jim Inghame6bc6cb2012-02-08 05:23:15 +0000199 GetLocationOptions()->GetThreadSpec()->SetQueueName(queue_name);
200 else
201 {
202 // If we're resetting this to an invalid thread id, then
203 // don't make an options pointer just to do that.
Eugene Zelenko16fd7512015-10-30 18:50:12 +0000204 if (m_options_ap.get() != nullptr)
Jim Inghame6bc6cb2012-02-08 05:23:15 +0000205 m_options_ap->GetThreadSpec()->SetQueueName(queue_name);
206 }
207 SendBreakpointLocationChangedEvent (eBreakpointEventTypeThreadChanged);
208}
209
210const char *
211BreakpointLocation::GetQueueName () const
212{
213 if (GetOptionsNoCreate()->GetThreadSpecNoCreate())
214 return GetOptionsNoCreate()->GetThreadSpecNoCreate()->GetQueueName();
215 else
Eugene Zelenko16fd7512015-10-30 18:50:12 +0000216 return nullptr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000217}
218
219bool
220BreakpointLocation::InvokeCallback (StoppointCallbackContext *context)
221{
Eugene Zelenko16fd7512015-10-30 18:50:12 +0000222 if (m_options_ap.get() != nullptr && m_options_ap->HasCallback())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000223 return m_options_ap->InvokeCallback (context, m_owner.GetID(), GetID());
Jim Ingham01363092010-06-18 01:00:58 +0000224 else
225 return m_owner.InvokeCallback (context, GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000226}
227
228void
229BreakpointLocation::SetCallback (BreakpointHitCallback callback, void *baton,
230 bool is_synchronous)
231{
232 // The default "Baton" class will keep a copy of "baton" and won't free
233 // or delete it when it goes goes out of scope.
234 GetLocationOptions()->SetCallback(callback, BatonSP (new Baton(baton)), is_synchronous);
Jim Inghame6bc6cb2012-02-08 05:23:15 +0000235 SendBreakpointLocationChangedEvent (eBreakpointEventTypeCommandChanged);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000236}
237
238void
239BreakpointLocation::SetCallback (BreakpointHitCallback callback, const BatonSP &baton_sp,
240 bool is_synchronous)
241{
242 GetLocationOptions()->SetCallback (callback, baton_sp, is_synchronous);
Jim Inghame6bc6cb2012-02-08 05:23:15 +0000243 SendBreakpointLocationChangedEvent (eBreakpointEventTypeCommandChanged);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000244}
245
246void
247BreakpointLocation::ClearCallback ()
248{
249 GetLocationOptions()->ClearCallback();
250}
251
Jim Ingham36f3b362010-10-14 23:45:03 +0000252void
253BreakpointLocation::SetCondition (const char *condition)
254{
255 GetLocationOptions()->SetCondition (condition);
Jim Inghame6bc6cb2012-02-08 05:23:15 +0000256 SendBreakpointLocationChangedEvent (eBreakpointEventTypeConditionChanged);
Jim Ingham36f3b362010-10-14 23:45:03 +0000257}
258
Jim Ingham36f3b362010-10-14 23:45:03 +0000259const char *
Sean Callanan3dbf3462013-04-19 07:09:15 +0000260BreakpointLocation::GetConditionText (size_t *hash) const
Jim Ingham36f3b362010-10-14 23:45:03 +0000261{
Sean Callanan3dbf3462013-04-19 07:09:15 +0000262 return GetOptionsNoCreate()->GetConditionText(hash);
263}
264
265bool
266BreakpointLocation::ConditionSaysStop (ExecutionContext &exe_ctx, Error &error)
267{
268 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS);
Sean Callananb4987e32013-06-06 20:18:50 +0000269
270 Mutex::Locker evaluation_locker(m_condition_mutex);
Sean Callanan3dbf3462013-04-19 07:09:15 +0000271
272 size_t condition_hash;
273 const char *condition_text = GetConditionText(&condition_hash);
274
275 if (!condition_text)
Sean Callananec537a22013-05-10 21:58:45 +0000276 {
277 m_user_expression_sp.reset();
Sean Callanan3dbf3462013-04-19 07:09:15 +0000278 return false;
Sean Callananec537a22013-05-10 21:58:45 +0000279 }
Sean Callanan3dbf3462013-04-19 07:09:15 +0000280
281 if (condition_hash != m_condition_hash ||
282 !m_user_expression_sp ||
283 !m_user_expression_sp->MatchesContext(exe_ctx))
284 {
Jim Ingham151c0322015-09-15 21:13:50 +0000285 LanguageType language = eLanguageTypeUnknown;
286 // See if we can figure out the language from the frame, otherwise use the default language:
287 CompileUnit *comp_unit = m_address.CalculateSymbolContextCompileUnit();
288 if (comp_unit)
289 language = comp_unit->GetLanguage();
290
291 Error error;
292 m_user_expression_sp.reset(GetTarget().GetUserExpressionForLanguage(condition_text,
Eugene Zelenko16fd7512015-10-30 18:50:12 +0000293 nullptr,
294 language,
295 Expression::eResultTypeAny,
296 error));
Jim Ingham151c0322015-09-15 21:13:50 +0000297 if (error.Fail())
298 {
299 if (log)
300 log->Printf("Error getting condition expression: %s.", error.AsCString());
301 m_user_expression_sp.reset();
302 return true;
303 }
Eugene Zelenko16fd7512015-10-30 18:50:12 +0000304
Sean Callanan3dbf3462013-04-19 07:09:15 +0000305 StreamString errors;
306
307 if (!m_user_expression_sp->Parse(errors,
308 exe_ctx,
309 eExecutionPolicyOnlyWhenNeeded,
Greg Clayton23f8c952014-03-24 23:10:19 +0000310 true,
311 false))
Sean Callanan3dbf3462013-04-19 07:09:15 +0000312 {
313 error.SetErrorStringWithFormat("Couldn't parse conditional expression:\n%s",
314 errors.GetData());
315 m_user_expression_sp.reset();
316 return false;
317 }
318
319 m_condition_hash = condition_hash;
320 }
321
322 // We need to make sure the user sees any parse errors in their condition, so we'll hook the
323 // constructor errors up to the debugger's Async I/O.
324
325 ValueObjectSP result_value_sp;
Greg Clayton62afb9f2013-11-04 19:35:17 +0000326
327 EvaluateExpressionOptions options;
328 options.SetUnwindOnError(true);
329 options.SetIgnoreBreakpoints(true);
Jim Ingham6fbc48b2013-11-07 00:11:47 +0000330 options.SetTryAllThreads(true);
Sean Callanan3dbf3462013-04-19 07:09:15 +0000331
332 Error expr_error;
333
334 StreamString execution_errors;
335
Sean Callananbc8ac342015-09-04 20:49:51 +0000336 ExpressionVariableSP result_variable_sp;
Sean Callanan3dbf3462013-04-19 07:09:15 +0000337
Jim Ingham1624a2d2014-05-05 02:26:40 +0000338 ExpressionResults result_code =
Sean Callanan3dbf3462013-04-19 07:09:15 +0000339 m_user_expression_sp->Execute(execution_errors,
340 exe_ctx,
Greg Clayton62afb9f2013-11-04 19:35:17 +0000341 options,
Sean Callanan3dbf3462013-04-19 07:09:15 +0000342 m_user_expression_sp,
Greg Clayton62afb9f2013-11-04 19:35:17 +0000343 result_variable_sp);
Sean Callanan3dbf3462013-04-19 07:09:15 +0000344
345 bool ret;
346
Jim Ingham8646d3c2014-05-05 02:47:44 +0000347 if (result_code == eExpressionCompleted)
Sean Callanan3dbf3462013-04-19 07:09:15 +0000348 {
Sean Callanan467441d52013-05-29 20:22:18 +0000349 if (!result_variable_sp)
350 {
Sean Callanan467441d52013-05-29 20:22:18 +0000351 error.SetErrorString("Expression did not return a result");
Sean Callanan879425f2013-06-24 17:58:46 +0000352 return false;
Sean Callanan467441d52013-05-29 20:22:18 +0000353 }
354
Sean Callanan3dbf3462013-04-19 07:09:15 +0000355 result_value_sp = result_variable_sp->GetValueObject();
356
357 if (result_value_sp)
358 {
359 Scalar scalar_value;
360 if (result_value_sp->ResolveValue (scalar_value))
361 {
Eugene Zelenko16fd7512015-10-30 18:50:12 +0000362 ret = (scalar_value.ULongLong(1) != 0);
Sean Callanan3dbf3462013-04-19 07:09:15 +0000363 if (log)
364 log->Printf("Condition successfully evaluated, result is %s.\n",
365 ret ? "true" : "false");
366 }
367 else
368 {
369 ret = false;
370 error.SetErrorString("Failed to get an integer result from the expression");
371 }
372 }
373 else
374 {
375 ret = false;
376 error.SetErrorString("Failed to get any result from the expression");
377 }
378 }
379 else
380 {
381 ret = false;
382 error.SetErrorStringWithFormat("Couldn't execute expression:\n%s", execution_errors.GetData());
383 }
384
385 return ret;
Jim Ingham36f3b362010-10-14 23:45:03 +0000386}
387
Greg Claytonc982c762010-07-09 20:39:50 +0000388uint32_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000389BreakpointLocation::GetIgnoreCount ()
390{
Jim Ingham05407f62010-06-22 21:12:54 +0000391 return GetOptionsNoCreate()->GetIgnoreCount();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000392}
393
394void
Greg Claytonc982c762010-07-09 20:39:50 +0000395BreakpointLocation::SetIgnoreCount (uint32_t n)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000396{
397 GetLocationOptions()->SetIgnoreCount(n);
Jim Inghame6bc6cb2012-02-08 05:23:15 +0000398 SendBreakpointLocationChangedEvent (eBreakpointEventTypeIgnoreChanged);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000399}
400
Jim Ingham0fd1b752012-06-26 22:27:55 +0000401void
402BreakpointLocation::DecrementIgnoreCount()
403{
Eugene Zelenko16fd7512015-10-30 18:50:12 +0000404 if (m_options_ap.get() != nullptr)
Jim Ingham0fd1b752012-06-26 22:27:55 +0000405 {
406 uint32_t loc_ignore = m_options_ap->GetIgnoreCount();
407 if (loc_ignore != 0)
408 m_options_ap->SetIgnoreCount(loc_ignore - 1);
409 }
410}
411
412bool
413BreakpointLocation::IgnoreCountShouldStop()
414{
Eugene Zelenko16fd7512015-10-30 18:50:12 +0000415 if (m_options_ap.get() != nullptr)
Jim Ingham0fd1b752012-06-26 22:27:55 +0000416 {
417 uint32_t loc_ignore = m_options_ap->GetIgnoreCount();
418 if (loc_ignore != 0)
419 {
420 m_owner.DecrementIgnoreCount();
421 DecrementIgnoreCount(); // Have to decrement our owners' ignore count, since it won't get a
422 // chance to.
423 return false;
424 }
425 }
426 return true;
427}
428
Jim Ingham1b54c882010-06-16 02:00:15 +0000429const BreakpointOptions *
Jim Ingham05407f62010-06-22 21:12:54 +0000430BreakpointLocation::GetOptionsNoCreate () const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000431{
Eugene Zelenko16fd7512015-10-30 18:50:12 +0000432 if (m_options_ap.get() != nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000433 return m_options_ap.get();
434 else
435 return m_owner.GetOptions ();
436}
437
438BreakpointOptions *
439BreakpointLocation::GetLocationOptions ()
440{
Jim Ingham01363092010-06-18 01:00:58 +0000441 // If we make the copy we don't copy the callbacks because that is potentially
442 // expensive and we don't want to do that for the simple case where someone is
443 // just disabling the location.
Eugene Zelenko16fd7512015-10-30 18:50:12 +0000444 if (m_options_ap.get() == nullptr)
Jim Ingham01363092010-06-18 01:00:58 +0000445 m_options_ap.reset(BreakpointOptions::CopyOptionsNoCallback(*m_owner.GetOptions ()));
446
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000447 return m_options_ap.get();
448}
449
Jim Ingham1b54c882010-06-16 02:00:15 +0000450bool
451BreakpointLocation::ValidForThisThread (Thread *thread)
452{
Jim Ingham05407f62010-06-22 21:12:54 +0000453 return thread->MatchesSpec(GetOptionsNoCreate()->GetThreadSpecNoCreate());
Jim Ingham1b54c882010-06-16 02:00:15 +0000454}
455
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000456// RETURNS - true if we should stop at this breakpoint, false if we
Jim Ingham1b54c882010-06-16 02:00:15 +0000457// should continue. Note, we don't check the thread spec for the breakpoint
458// here, since if the breakpoint is not for this thread, then the event won't
459// even get reported, so the check is redundant.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000460
461bool
462BreakpointLocation::ShouldStop (StoppointCallbackContext *context)
463{
464 bool should_stop = true;
Greg Clayton5160ce52013-03-27 23:08:40 +0000465 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000466
Jim Inghama672ece2014-10-22 01:54:17 +0000467 // Do this first, if a location is disabled, it shouldn't increment its hit count.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000468 if (!IsEnabled())
469 return false;
470
Jim Ingham0fd1b752012-06-26 22:27:55 +0000471 if (!IgnoreCountShouldStop())
472 return false;
473
474 if (!m_owner.IgnoreCountShouldStop())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000475 return false;
476
Jim Ingham36f3b362010-10-14 23:45:03 +0000477 // We only run synchronous callbacks in ShouldStop:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000478 context->is_synchronous = true;
479 should_stop = InvokeCallback (context);
Jim Ingham36f3b362010-10-14 23:45:03 +0000480
Jim Ingham5b52f0c2011-06-02 23:58:26 +0000481 if (log)
Jim Ingham36f3b362010-10-14 23:45:03 +0000482 {
Jim Ingham5b52f0c2011-06-02 23:58:26 +0000483 StreamString s;
484 GetDescription (&s, lldb::eDescriptionLevelVerbose);
485 log->Printf ("Hit breakpoint location: %s, %s.\n", s.GetData(), should_stop ? "stopping" : "continuing");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000486 }
Jim Ingham5b52f0c2011-06-02 23:58:26 +0000487
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000488 return should_stop;
489}
490
Jim Inghama672ece2014-10-22 01:54:17 +0000491void
492BreakpointLocation::BumpHitCount()
493{
494 if (IsEnabled())
Jim Inghamd762df82015-01-15 01:41:04 +0000495 {
496 // Step our hit count, and also step the hit count of the owner.
Jim Inghama672ece2014-10-22 01:54:17 +0000497 IncrementHitCount();
Jim Inghamd762df82015-01-15 01:41:04 +0000498 m_owner.IncrementHitCount();
499 }
500}
501
502void
503BreakpointLocation::UndoBumpHitCount()
504{
505 if (IsEnabled())
506 {
507 // Step our hit count, and also step the hit count of the owner.
508 DecrementHitCount();
509 m_owner.DecrementHitCount();
510 }
Jim Inghama672ece2014-10-22 01:54:17 +0000511}
512
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000513bool
514BreakpointLocation::IsResolved () const
515{
Eugene Zelenko16fd7512015-10-30 18:50:12 +0000516 return m_bp_site_sp.get() != nullptr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000517}
518
Jim Ingham36f3b362010-10-14 23:45:03 +0000519lldb::BreakpointSiteSP
520BreakpointLocation::GetBreakpointSite() const
521{
522 return m_bp_site_sp;
523}
524
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000525bool
526BreakpointLocation::ResolveBreakpointSite ()
527{
528 if (m_bp_site_sp)
529 return true;
530
Greg Claytonf5e56de2010-09-14 23:36:40 +0000531 Process *process = m_owner.GetTarget().GetProcessSP().get();
Eugene Zelenko16fd7512015-10-30 18:50:12 +0000532 if (process == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000533 return false;
534
Greg Claytoneb023e72013-10-11 19:48:25 +0000535 lldb::break_id_t new_id = process->CreateBreakpointSite (shared_from_this(), m_owner.IsHardware());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000536
Stephen Wilson50bd94f2010-07-17 00:56:13 +0000537 if (new_id == LLDB_INVALID_BREAK_ID)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000538 {
Greg Clayton5160ce52013-03-27 23:08:40 +0000539 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000540 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000541 log->Warning ("Tried to add breakpoint site at 0x%" PRIx64 " but it was already present.\n",
Greg Claytonf3ef3d22011-05-22 22:46:53 +0000542 m_address.GetOpcodeLoadAddress (&m_owner.GetTarget()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000543 return false;
544 }
545
546 return true;
547}
548
549bool
550BreakpointLocation::SetBreakpointSite (BreakpointSiteSP& bp_site_sp)
551{
552 m_bp_site_sp = bp_site_sp;
Ilia K9b618d22015-04-09 12:55:13 +0000553 SendBreakpointLocationChangedEvent (eBreakpointEventTypeLocationsResolved);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000554 return true;
555}
556
557bool
558BreakpointLocation::ClearBreakpointSite ()
559{
560 if (m_bp_site_sp.get())
561 {
Jim Ingham15783132014-03-12 22:03:13 +0000562 ProcessSP process_sp(m_owner.GetTarget().GetProcessSP());
563 // If the process exists, get it to remove the owner, it will remove the physical implementation
564 // of the breakpoint as well if there are no more owners. Otherwise just remove this owner.
565 if (process_sp)
566 process_sp->RemoveOwnerFromBreakpointSite (GetBreakpoint().GetID(),
Jim Ingham01363092010-06-18 01:00:58 +0000567 GetID(), m_bp_site_sp);
Jim Ingham15783132014-03-12 22:03:13 +0000568 else
569 m_bp_site_sp->RemoveOwner(GetBreakpoint().GetID(), GetID());
570
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000571 m_bp_site_sp.reset();
572 return true;
573 }
574 return false;
575}
576
577void
578BreakpointLocation::GetDescription (Stream *s, lldb::DescriptionLevel level)
579{
580 SymbolContext sc;
Jim Ingham1391cc72012-09-22 00:04:04 +0000581
582 // If the description level is "initial" then the breakpoint is printing out our initial state,
583 // and we should let it decide how it wants to print our label.
584 if (level != eDescriptionLevelInitial)
585 {
586 s->Indent();
587 BreakpointID::GetCanonicalReference(s, m_owner.GetID(), GetID());
588 }
589
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000590 if (level == lldb::eDescriptionLevelBrief)
591 return;
592
Jim Ingham1391cc72012-09-22 00:04:04 +0000593 if (level != eDescriptionLevelInitial)
594 s->PutCString(": ");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000595
596 if (level == lldb::eDescriptionLevelVerbose)
597 s->IndentMore();
598
599 if (m_address.IsSectionOffset())
600 {
601 m_address.CalculateSymbolContext(&sc);
602
Jim Ingham1391cc72012-09-22 00:04:04 +0000603 if (level == lldb::eDescriptionLevelFull || level == eDescriptionLevelInitial)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000604 {
Jim Ingham1460e4b2014-01-10 23:46:59 +0000605 if (IsReExported())
606 s->PutCString ("re-exported target = ");
607 else
608 s->PutCString("where = ");
Jason Molendac980fa92015-02-13 23:24:21 +0000609 sc.DumpStopContext (s, m_owner.GetTarget().GetProcessSP().get(), m_address, false, true, false, true, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000610 }
611 else
612 {
613 if (sc.module_sp)
614 {
615 s->EOL();
616 s->Indent("module = ");
617 sc.module_sp->GetFileSpec().Dump (s);
618 }
619
Eugene Zelenko16fd7512015-10-30 18:50:12 +0000620 if (sc.comp_unit != nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000621 {
622 s->EOL();
623 s->Indent("compile unit = ");
Jim Ingham517b3b22010-10-27 22:58:34 +0000624 static_cast<FileSpec*>(sc.comp_unit)->GetFilename().Dump (s);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000625
Eugene Zelenko16fd7512015-10-30 18:50:12 +0000626 if (sc.function != nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000627 {
628 s->EOL();
629 s->Indent("function = ");
Greg Claytonddaf6a72015-07-08 22:32:23 +0000630 s->PutCString (sc.function->GetName().AsCString("<unknown>"));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000631 }
632
633 if (sc.line_entry.line > 0)
634 {
635 s->EOL();
636 s->Indent("location = ");
Greg Clayton6dadd502010-09-02 21:44:10 +0000637 sc.line_entry.DumpStopContext (s, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000638 }
639
640 }
641 else
642 {
643 // If we don't have a comp unit, see if we have a symbol we can print.
644 if (sc.symbol)
645 {
646 s->EOL();
Jim Ingham1460e4b2014-01-10 23:46:59 +0000647 if (IsReExported())
648 s->Indent ("re-exported target = ");
649 else
650 s->Indent("symbol = ");
Greg Claytonddaf6a72015-07-08 22:32:23 +0000651 s->PutCString(sc.symbol->GetName().AsCString("<unknown>"));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000652 }
653 }
654 }
655 }
656
657 if (level == lldb::eDescriptionLevelVerbose)
658 {
659 s->EOL();
660 s->Indent();
661 }
Jim Ingham1391cc72012-09-22 00:04:04 +0000662
663 if (m_address.IsSectionOffset() && (level == eDescriptionLevelFull || level == eDescriptionLevelInitial))
664 s->Printf (", ");
665 s->Printf ("address = ");
666
Eugene Zelenko16fd7512015-10-30 18:50:12 +0000667 ExecutionContextScope *exe_scope = nullptr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000668 Target *target = &m_owner.GetTarget();
669 if (target)
670 exe_scope = target->GetProcessSP().get();
Eugene Zelenko16fd7512015-10-30 18:50:12 +0000671 if (exe_scope == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000672 exe_scope = target;
673
Jim Ingham8f632662014-03-04 03:09:00 +0000674 if (level == eDescriptionLevelInitial)
Jim Ingham1391cc72012-09-22 00:04:04 +0000675 m_address.Dump(s, exe_scope, Address::DumpStyleLoadAddress, Address::DumpStyleFileAddress);
676 else
677 m_address.Dump(s, exe_scope, Address::DumpStyleLoadAddress, Address::DumpStyleModuleWithFileAddress);
Jim Ingham1460e4b2014-01-10 23:46:59 +0000678
679 if (IsIndirect() && m_bp_site_sp)
680 {
681 Address resolved_address;
682 resolved_address.SetLoadAddress(m_bp_site_sp->GetLoadAddress(), target);
683 Symbol *resolved_symbol = resolved_address.CalculateSymbolContextSymbol();
684 if (resolved_symbol)
685 {
686 if (level == eDescriptionLevelFull || level == eDescriptionLevelInitial)
687 s->Printf (", ");
688 else if (level == lldb::eDescriptionLevelVerbose)
689 {
690 s->EOL();
691 s->Indent();
692 }
693 s->Printf ("indirect target = %s", resolved_symbol->GetName().GetCString());
694 }
695 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000696
697 if (level == lldb::eDescriptionLevelVerbose)
698 {
699 s->EOL();
700 s->Indent();
701 s->Printf("resolved = %s\n", IsResolved() ? "true" : "false");
702
703 s->Indent();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000704 s->Printf ("hit count = %-4u\n", GetHitCount());
705
706 if (m_options_ap.get())
707 {
Jim Ingham01363092010-06-18 01:00:58 +0000708 s->Indent();
709 m_options_ap->GetDescription (s, level);
710 s->EOL();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000711 }
712 s->IndentLess();
713 }
Jim Ingham1391cc72012-09-22 00:04:04 +0000714 else if (level != eDescriptionLevelInitial)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000715 {
Jim Ingham01363092010-06-18 01:00:58 +0000716 s->Printf(", %sresolved, hit count = %u ",
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000717 (IsResolved() ? "" : "un"),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000718 GetHitCount());
Jim Ingham01363092010-06-18 01:00:58 +0000719 if (m_options_ap.get())
720 {
721 m_options_ap->GetDescription (s, level);
722 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000723 }
724}
725
726void
727BreakpointLocation::Dump(Stream *s) const
728{
Eugene Zelenko16fd7512015-10-30 18:50:12 +0000729 if (s == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000730 return;
731
Daniel Malead01b2952012-11-29 21:49:15 +0000732 s->Printf("BreakpointLocation %u: tid = %4.4" PRIx64 " load addr = 0x%8.8" PRIx64 " state = %s type = %s breakpoint "
Jim Ingham01363092010-06-18 01:00:58 +0000733 "hw_index = %i hit_count = %-4u ignore_count = %-4u",
Johnny Chen9ec3c4f2012-01-26 00:08:14 +0000734 GetID(),
735 GetOptionsNoCreate()->GetThreadSpecNoCreate()->GetTID(),
736 (uint64_t) m_address.GetOpcodeLoadAddress (&m_owner.GetTarget()),
Johnny Chen50df1f92012-01-30 22:48:10 +0000737 (m_options_ap.get() ? m_options_ap->IsEnabled() : m_owner.IsEnabled()) ? "enabled " : "disabled",
Johnny Chen9ec3c4f2012-01-26 00:08:14 +0000738 IsHardware() ? "hardware" : "software",
739 GetHardwareIndex(),
740 GetHitCount(),
741 GetOptionsNoCreate()->GetIgnoreCount());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000742}
Jim Inghame6bc6cb2012-02-08 05:23:15 +0000743
744void
745BreakpointLocation::SendBreakpointLocationChangedEvent (lldb::BreakpointEventType eventKind)
746{
747 if (!m_being_created
748 && !m_owner.IsInternal()
749 && m_owner.GetTarget().EventTypeHasListeners(Target::eBroadcastBitBreakpointChanged))
750 {
751 Breakpoint::BreakpointEventData *data = new Breakpoint::BreakpointEventData (eventKind,
752 m_owner.shared_from_this());
753 data->GetBreakpointLocationCollection().Add (shared_from_this());
754 m_owner.GetTarget().BroadcastEvent (Target::eBroadcastBitBreakpointChanged, data);
755 }
756}
Jim Ingham77fd7382014-09-10 21:40:47 +0000757
758void
759BreakpointLocation::SwapLocation (BreakpointLocationSP swap_from)
760{
761 m_address = swap_from->m_address;
762 m_should_resolve_indirect_functions = swap_from->m_should_resolve_indirect_functions;
763 m_is_reexported = swap_from->m_is_reexported;
764 m_is_indirect = swap_from->m_is_indirect;
765 m_user_expression_sp.reset();
766}