blob: f64577b3743238d3fb71640064598b5cced32dc9 [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 {
Jim Ingham98e6daf2015-10-31 00:02:18 +0000359 ret = result_value_sp->IsLogicalTrue(error);
360 if (log)
Sean Callanan3dbf3462013-04-19 07:09:15 +0000361 {
Jim Ingham98e6daf2015-10-31 00:02:18 +0000362 if (error.Success())
363 {
Sean Callanan3dbf3462013-04-19 07:09:15 +0000364 log->Printf("Condition successfully evaluated, result is %s.\n",
365 ret ? "true" : "false");
Jim Ingham98e6daf2015-10-31 00:02:18 +0000366 }
367 else
368 {
369 error.SetErrorString("Failed to get an integer result from the expression");
370 ret = false;
371 }
372
Sean Callanan3dbf3462013-04-19 07:09:15 +0000373 }
374 }
375 else
376 {
377 ret = false;
378 error.SetErrorString("Failed to get any result from the expression");
379 }
380 }
381 else
382 {
383 ret = false;
384 error.SetErrorStringWithFormat("Couldn't execute expression:\n%s", execution_errors.GetData());
385 }
386
387 return ret;
Jim Ingham36f3b362010-10-14 23:45:03 +0000388}
389
Greg Claytonc982c762010-07-09 20:39:50 +0000390uint32_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000391BreakpointLocation::GetIgnoreCount ()
392{
Jim Ingham05407f62010-06-22 21:12:54 +0000393 return GetOptionsNoCreate()->GetIgnoreCount();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000394}
395
396void
Greg Claytonc982c762010-07-09 20:39:50 +0000397BreakpointLocation::SetIgnoreCount (uint32_t n)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000398{
399 GetLocationOptions()->SetIgnoreCount(n);
Jim Inghame6bc6cb2012-02-08 05:23:15 +0000400 SendBreakpointLocationChangedEvent (eBreakpointEventTypeIgnoreChanged);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000401}
402
Jim Ingham0fd1b752012-06-26 22:27:55 +0000403void
404BreakpointLocation::DecrementIgnoreCount()
405{
Eugene Zelenko16fd7512015-10-30 18:50:12 +0000406 if (m_options_ap.get() != nullptr)
Jim Ingham0fd1b752012-06-26 22:27:55 +0000407 {
408 uint32_t loc_ignore = m_options_ap->GetIgnoreCount();
409 if (loc_ignore != 0)
410 m_options_ap->SetIgnoreCount(loc_ignore - 1);
411 }
412}
413
414bool
415BreakpointLocation::IgnoreCountShouldStop()
416{
Eugene Zelenko16fd7512015-10-30 18:50:12 +0000417 if (m_options_ap.get() != nullptr)
Jim Ingham0fd1b752012-06-26 22:27:55 +0000418 {
419 uint32_t loc_ignore = m_options_ap->GetIgnoreCount();
420 if (loc_ignore != 0)
421 {
422 m_owner.DecrementIgnoreCount();
423 DecrementIgnoreCount(); // Have to decrement our owners' ignore count, since it won't get a
424 // chance to.
425 return false;
426 }
427 }
428 return true;
429}
430
Jim Ingham1b54c882010-06-16 02:00:15 +0000431const BreakpointOptions *
Jim Ingham05407f62010-06-22 21:12:54 +0000432BreakpointLocation::GetOptionsNoCreate () const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000433{
Eugene Zelenko16fd7512015-10-30 18:50:12 +0000434 if (m_options_ap.get() != nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000435 return m_options_ap.get();
436 else
437 return m_owner.GetOptions ();
438}
439
440BreakpointOptions *
441BreakpointLocation::GetLocationOptions ()
442{
Jim Ingham01363092010-06-18 01:00:58 +0000443 // If we make the copy we don't copy the callbacks because that is potentially
444 // expensive and we don't want to do that for the simple case where someone is
445 // just disabling the location.
Eugene Zelenko16fd7512015-10-30 18:50:12 +0000446 if (m_options_ap.get() == nullptr)
Jim Ingham01363092010-06-18 01:00:58 +0000447 m_options_ap.reset(BreakpointOptions::CopyOptionsNoCallback(*m_owner.GetOptions ()));
448
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000449 return m_options_ap.get();
450}
451
Jim Ingham1b54c882010-06-16 02:00:15 +0000452bool
453BreakpointLocation::ValidForThisThread (Thread *thread)
454{
Jim Ingham05407f62010-06-22 21:12:54 +0000455 return thread->MatchesSpec(GetOptionsNoCreate()->GetThreadSpecNoCreate());
Jim Ingham1b54c882010-06-16 02:00:15 +0000456}
457
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000458// RETURNS - true if we should stop at this breakpoint, false if we
Jim Ingham1b54c882010-06-16 02:00:15 +0000459// should continue. Note, we don't check the thread spec for the breakpoint
460// here, since if the breakpoint is not for this thread, then the event won't
461// even get reported, so the check is redundant.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000462
463bool
464BreakpointLocation::ShouldStop (StoppointCallbackContext *context)
465{
466 bool should_stop = true;
Greg Clayton5160ce52013-03-27 23:08:40 +0000467 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000468
Jim Inghama672ece2014-10-22 01:54:17 +0000469 // Do this first, if a location is disabled, it shouldn't increment its hit count.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000470 if (!IsEnabled())
471 return false;
472
Jim Ingham0fd1b752012-06-26 22:27:55 +0000473 if (!IgnoreCountShouldStop())
474 return false;
475
476 if (!m_owner.IgnoreCountShouldStop())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000477 return false;
478
Jim Ingham36f3b362010-10-14 23:45:03 +0000479 // We only run synchronous callbacks in ShouldStop:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000480 context->is_synchronous = true;
481 should_stop = InvokeCallback (context);
Jim Ingham36f3b362010-10-14 23:45:03 +0000482
Jim Ingham5b52f0c2011-06-02 23:58:26 +0000483 if (log)
Jim Ingham36f3b362010-10-14 23:45:03 +0000484 {
Jim Ingham5b52f0c2011-06-02 23:58:26 +0000485 StreamString s;
486 GetDescription (&s, lldb::eDescriptionLevelVerbose);
487 log->Printf ("Hit breakpoint location: %s, %s.\n", s.GetData(), should_stop ? "stopping" : "continuing");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000488 }
Jim Ingham5b52f0c2011-06-02 23:58:26 +0000489
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000490 return should_stop;
491}
492
Jim Inghama672ece2014-10-22 01:54:17 +0000493void
494BreakpointLocation::BumpHitCount()
495{
496 if (IsEnabled())
Jim Inghamd762df82015-01-15 01:41:04 +0000497 {
498 // Step our hit count, and also step the hit count of the owner.
Jim Inghama672ece2014-10-22 01:54:17 +0000499 IncrementHitCount();
Jim Inghamd762df82015-01-15 01:41:04 +0000500 m_owner.IncrementHitCount();
501 }
502}
503
504void
505BreakpointLocation::UndoBumpHitCount()
506{
507 if (IsEnabled())
508 {
509 // Step our hit count, and also step the hit count of the owner.
510 DecrementHitCount();
511 m_owner.DecrementHitCount();
512 }
Jim Inghama672ece2014-10-22 01:54:17 +0000513}
514
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000515bool
516BreakpointLocation::IsResolved () const
517{
Eugene Zelenko16fd7512015-10-30 18:50:12 +0000518 return m_bp_site_sp.get() != nullptr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000519}
520
Jim Ingham36f3b362010-10-14 23:45:03 +0000521lldb::BreakpointSiteSP
522BreakpointLocation::GetBreakpointSite() const
523{
524 return m_bp_site_sp;
525}
526
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000527bool
528BreakpointLocation::ResolveBreakpointSite ()
529{
530 if (m_bp_site_sp)
531 return true;
532
Greg Claytonf5e56de2010-09-14 23:36:40 +0000533 Process *process = m_owner.GetTarget().GetProcessSP().get();
Eugene Zelenko16fd7512015-10-30 18:50:12 +0000534 if (process == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000535 return false;
536
Greg Claytoneb023e72013-10-11 19:48:25 +0000537 lldb::break_id_t new_id = process->CreateBreakpointSite (shared_from_this(), m_owner.IsHardware());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000538
Stephen Wilson50bd94f2010-07-17 00:56:13 +0000539 if (new_id == LLDB_INVALID_BREAK_ID)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000540 {
Greg Clayton5160ce52013-03-27 23:08:40 +0000541 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000542 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000543 log->Warning ("Tried to add breakpoint site at 0x%" PRIx64 " but it was already present.\n",
Greg Claytonf3ef3d22011-05-22 22:46:53 +0000544 m_address.GetOpcodeLoadAddress (&m_owner.GetTarget()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000545 return false;
546 }
547
548 return true;
549}
550
551bool
552BreakpointLocation::SetBreakpointSite (BreakpointSiteSP& bp_site_sp)
553{
554 m_bp_site_sp = bp_site_sp;
Ilia K9b618d22015-04-09 12:55:13 +0000555 SendBreakpointLocationChangedEvent (eBreakpointEventTypeLocationsResolved);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000556 return true;
557}
558
559bool
560BreakpointLocation::ClearBreakpointSite ()
561{
562 if (m_bp_site_sp.get())
563 {
Jim Ingham15783132014-03-12 22:03:13 +0000564 ProcessSP process_sp(m_owner.GetTarget().GetProcessSP());
565 // If the process exists, get it to remove the owner, it will remove the physical implementation
566 // of the breakpoint as well if there are no more owners. Otherwise just remove this owner.
567 if (process_sp)
568 process_sp->RemoveOwnerFromBreakpointSite (GetBreakpoint().GetID(),
Jim Ingham01363092010-06-18 01:00:58 +0000569 GetID(), m_bp_site_sp);
Jim Ingham15783132014-03-12 22:03:13 +0000570 else
571 m_bp_site_sp->RemoveOwner(GetBreakpoint().GetID(), GetID());
572
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000573 m_bp_site_sp.reset();
574 return true;
575 }
576 return false;
577}
578
579void
580BreakpointLocation::GetDescription (Stream *s, lldb::DescriptionLevel level)
581{
582 SymbolContext sc;
Jim Ingham1391cc72012-09-22 00:04:04 +0000583
584 // If the description level is "initial" then the breakpoint is printing out our initial state,
585 // and we should let it decide how it wants to print our label.
586 if (level != eDescriptionLevelInitial)
587 {
588 s->Indent();
589 BreakpointID::GetCanonicalReference(s, m_owner.GetID(), GetID());
590 }
591
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000592 if (level == lldb::eDescriptionLevelBrief)
593 return;
594
Jim Ingham1391cc72012-09-22 00:04:04 +0000595 if (level != eDescriptionLevelInitial)
596 s->PutCString(": ");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000597
598 if (level == lldb::eDescriptionLevelVerbose)
599 s->IndentMore();
600
601 if (m_address.IsSectionOffset())
602 {
603 m_address.CalculateSymbolContext(&sc);
604
Jim Ingham1391cc72012-09-22 00:04:04 +0000605 if (level == lldb::eDescriptionLevelFull || level == eDescriptionLevelInitial)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000606 {
Jim Ingham1460e4b2014-01-10 23:46:59 +0000607 if (IsReExported())
608 s->PutCString ("re-exported target = ");
609 else
610 s->PutCString("where = ");
Jason Molendac980fa92015-02-13 23:24:21 +0000611 sc.DumpStopContext (s, m_owner.GetTarget().GetProcessSP().get(), m_address, false, true, false, true, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000612 }
613 else
614 {
615 if (sc.module_sp)
616 {
617 s->EOL();
618 s->Indent("module = ");
619 sc.module_sp->GetFileSpec().Dump (s);
620 }
621
Eugene Zelenko16fd7512015-10-30 18:50:12 +0000622 if (sc.comp_unit != nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000623 {
624 s->EOL();
625 s->Indent("compile unit = ");
Jim Ingham517b3b22010-10-27 22:58:34 +0000626 static_cast<FileSpec*>(sc.comp_unit)->GetFilename().Dump (s);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000627
Eugene Zelenko16fd7512015-10-30 18:50:12 +0000628 if (sc.function != nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000629 {
630 s->EOL();
631 s->Indent("function = ");
Greg Claytonddaf6a72015-07-08 22:32:23 +0000632 s->PutCString (sc.function->GetName().AsCString("<unknown>"));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000633 }
634
635 if (sc.line_entry.line > 0)
636 {
637 s->EOL();
638 s->Indent("location = ");
Greg Clayton6dadd502010-09-02 21:44:10 +0000639 sc.line_entry.DumpStopContext (s, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000640 }
641
642 }
643 else
644 {
645 // If we don't have a comp unit, see if we have a symbol we can print.
646 if (sc.symbol)
647 {
648 s->EOL();
Jim Ingham1460e4b2014-01-10 23:46:59 +0000649 if (IsReExported())
650 s->Indent ("re-exported target = ");
651 else
652 s->Indent("symbol = ");
Greg Claytonddaf6a72015-07-08 22:32:23 +0000653 s->PutCString(sc.symbol->GetName().AsCString("<unknown>"));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000654 }
655 }
656 }
657 }
658
659 if (level == lldb::eDescriptionLevelVerbose)
660 {
661 s->EOL();
662 s->Indent();
663 }
Jim Ingham1391cc72012-09-22 00:04:04 +0000664
665 if (m_address.IsSectionOffset() && (level == eDescriptionLevelFull || level == eDescriptionLevelInitial))
666 s->Printf (", ");
667 s->Printf ("address = ");
668
Eugene Zelenko16fd7512015-10-30 18:50:12 +0000669 ExecutionContextScope *exe_scope = nullptr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000670 Target *target = &m_owner.GetTarget();
671 if (target)
672 exe_scope = target->GetProcessSP().get();
Eugene Zelenko16fd7512015-10-30 18:50:12 +0000673 if (exe_scope == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000674 exe_scope = target;
675
Jim Ingham8f632662014-03-04 03:09:00 +0000676 if (level == eDescriptionLevelInitial)
Jim Ingham1391cc72012-09-22 00:04:04 +0000677 m_address.Dump(s, exe_scope, Address::DumpStyleLoadAddress, Address::DumpStyleFileAddress);
678 else
679 m_address.Dump(s, exe_scope, Address::DumpStyleLoadAddress, Address::DumpStyleModuleWithFileAddress);
Jim Ingham1460e4b2014-01-10 23:46:59 +0000680
681 if (IsIndirect() && m_bp_site_sp)
682 {
683 Address resolved_address;
684 resolved_address.SetLoadAddress(m_bp_site_sp->GetLoadAddress(), target);
685 Symbol *resolved_symbol = resolved_address.CalculateSymbolContextSymbol();
686 if (resolved_symbol)
687 {
688 if (level == eDescriptionLevelFull || level == eDescriptionLevelInitial)
689 s->Printf (", ");
690 else if (level == lldb::eDescriptionLevelVerbose)
691 {
692 s->EOL();
693 s->Indent();
694 }
695 s->Printf ("indirect target = %s", resolved_symbol->GetName().GetCString());
696 }
697 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000698
699 if (level == lldb::eDescriptionLevelVerbose)
700 {
701 s->EOL();
702 s->Indent();
703 s->Printf("resolved = %s\n", IsResolved() ? "true" : "false");
704
705 s->Indent();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000706 s->Printf ("hit count = %-4u\n", GetHitCount());
707
708 if (m_options_ap.get())
709 {
Jim Ingham01363092010-06-18 01:00:58 +0000710 s->Indent();
711 m_options_ap->GetDescription (s, level);
712 s->EOL();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000713 }
714 s->IndentLess();
715 }
Jim Ingham1391cc72012-09-22 00:04:04 +0000716 else if (level != eDescriptionLevelInitial)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000717 {
Jim Ingham01363092010-06-18 01:00:58 +0000718 s->Printf(", %sresolved, hit count = %u ",
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000719 (IsResolved() ? "" : "un"),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000720 GetHitCount());
Jim Ingham01363092010-06-18 01:00:58 +0000721 if (m_options_ap.get())
722 {
723 m_options_ap->GetDescription (s, level);
724 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000725 }
726}
727
728void
729BreakpointLocation::Dump(Stream *s) const
730{
Eugene Zelenko16fd7512015-10-30 18:50:12 +0000731 if (s == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000732 return;
733
Daniel Malead01b2952012-11-29 21:49:15 +0000734 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 +0000735 "hw_index = %i hit_count = %-4u ignore_count = %-4u",
Johnny Chen9ec3c4f2012-01-26 00:08:14 +0000736 GetID(),
737 GetOptionsNoCreate()->GetThreadSpecNoCreate()->GetTID(),
738 (uint64_t) m_address.GetOpcodeLoadAddress (&m_owner.GetTarget()),
Johnny Chen50df1f92012-01-30 22:48:10 +0000739 (m_options_ap.get() ? m_options_ap->IsEnabled() : m_owner.IsEnabled()) ? "enabled " : "disabled",
Johnny Chen9ec3c4f2012-01-26 00:08:14 +0000740 IsHardware() ? "hardware" : "software",
741 GetHardwareIndex(),
742 GetHitCount(),
743 GetOptionsNoCreate()->GetIgnoreCount());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000744}
Jim Inghame6bc6cb2012-02-08 05:23:15 +0000745
746void
747BreakpointLocation::SendBreakpointLocationChangedEvent (lldb::BreakpointEventType eventKind)
748{
749 if (!m_being_created
750 && !m_owner.IsInternal()
751 && m_owner.GetTarget().EventTypeHasListeners(Target::eBroadcastBitBreakpointChanged))
752 {
753 Breakpoint::BreakpointEventData *data = new Breakpoint::BreakpointEventData (eventKind,
754 m_owner.shared_from_this());
755 data->GetBreakpointLocationCollection().Add (shared_from_this());
756 m_owner.GetTarget().BroadcastEvent (Target::eBroadcastBitBreakpointChanged, data);
757 }
758}
Jim Ingham77fd7382014-09-10 21:40:47 +0000759
760void
761BreakpointLocation::SwapLocation (BreakpointLocationSP swap_from)
762{
763 m_address = swap_from->m_address;
764 m_should_resolve_indirect_functions = swap_from->m_should_resolve_indirect_functions;
765 m_is_reexported = swap_from->m_is_reexported;
766 m_is_indirect = swap_from->m_is_indirect;
767 m_user_expression_sp.reset();
768}