1 //===-- BreakpointLocation.cpp ----------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/lldb-python.h"
16 // Other libraries and framework includes
18 #include "lldb/lldb-private-log.h"
19 #include "lldb/Breakpoint/BreakpointLocation.h"
20 #include "lldb/Breakpoint/BreakpointID.h"
21 #include "lldb/Breakpoint/StoppointCallbackContext.h"
22 #include "lldb/Core/Debugger.h"
23 #include "lldb/Core/Log.h"
24 #include "lldb/Core/Module.h"
25 #include "lldb/Core/StreamString.h"
26 #include "lldb/Symbol/CompileUnit.h"
27 #include "lldb/Symbol/Symbol.h"
28 #include "lldb/Target/Target.h"
29 #include "lldb/Target/Process.h"
30 #include "lldb/Target/Thread.h"
31 #include "lldb/Target/ThreadSpec.h"
34 using namespace lldb_private;
36 BreakpointLocation::BreakpointLocation
44 StoppointLocation (loc_id, addr.GetOpcodeLoadAddress(&owner.GetTarget()), hardware),
45 m_being_created(true),
53 m_being_created = false;
56 BreakpointLocation::~BreakpointLocation()
58 ClearBreakpointSite();
62 BreakpointLocation::GetLoadAddress () const
64 return m_address.GetOpcodeLoadAddress (&m_owner.GetTarget());
68 BreakpointLocation::GetAddress ()
74 BreakpointLocation::GetBreakpoint ()
80 BreakpointLocation::IsEnabled () const
82 if (!m_owner.IsEnabled())
84 else if (m_options_ap.get() != NULL)
85 return m_options_ap->IsEnabled();
91 BreakpointLocation::SetEnabled (bool enabled)
93 GetLocationOptions()->SetEnabled(enabled);
96 ResolveBreakpointSite();
100 ClearBreakpointSite();
102 SendBreakpointLocationChangedEvent (enabled ? eBreakpointEventTypeEnabled : eBreakpointEventTypeDisabled);
106 BreakpointLocation::SetThreadID (lldb::tid_t thread_id)
108 if (thread_id != LLDB_INVALID_THREAD_ID)
109 GetLocationOptions()->SetThreadID(thread_id);
112 // If we're resetting this to an invalid thread id, then
113 // don't make an options pointer just to do that.
114 if (m_options_ap.get() != NULL)
115 m_options_ap->SetThreadID (thread_id);
117 SendBreakpointLocationChangedEvent (eBreakpointEventTypeThreadChanged);
121 BreakpointLocation::GetThreadID ()
123 if (GetOptionsNoCreate()->GetThreadSpecNoCreate())
124 return GetOptionsNoCreate()->GetThreadSpecNoCreate()->GetTID();
126 return LLDB_INVALID_THREAD_ID;
130 BreakpointLocation::SetThreadIndex (uint32_t index)
133 GetLocationOptions()->GetThreadSpec()->SetIndex(index);
136 // If we're resetting this to an invalid thread id, then
137 // don't make an options pointer just to do that.
138 if (m_options_ap.get() != NULL)
139 m_options_ap->GetThreadSpec()->SetIndex(index);
141 SendBreakpointLocationChangedEvent (eBreakpointEventTypeThreadChanged);
146 BreakpointLocation::GetThreadIndex() const
148 if (GetOptionsNoCreate()->GetThreadSpecNoCreate())
149 return GetOptionsNoCreate()->GetThreadSpecNoCreate()->GetIndex();
155 BreakpointLocation::SetThreadName (const char *thread_name)
157 if (thread_name != NULL)
158 GetLocationOptions()->GetThreadSpec()->SetName(thread_name);
161 // If we're resetting this to an invalid thread id, then
162 // don't make an options pointer just to do that.
163 if (m_options_ap.get() != NULL)
164 m_options_ap->GetThreadSpec()->SetName(thread_name);
166 SendBreakpointLocationChangedEvent (eBreakpointEventTypeThreadChanged);
170 BreakpointLocation::GetThreadName () const
172 if (GetOptionsNoCreate()->GetThreadSpecNoCreate())
173 return GetOptionsNoCreate()->GetThreadSpecNoCreate()->GetName();
179 BreakpointLocation::SetQueueName (const char *queue_name)
181 if (queue_name != NULL)
182 GetLocationOptions()->GetThreadSpec()->SetQueueName(queue_name);
185 // If we're resetting this to an invalid thread id, then
186 // don't make an options pointer just to do that.
187 if (m_options_ap.get() != NULL)
188 m_options_ap->GetThreadSpec()->SetQueueName(queue_name);
190 SendBreakpointLocationChangedEvent (eBreakpointEventTypeThreadChanged);
194 BreakpointLocation::GetQueueName () const
196 if (GetOptionsNoCreate()->GetThreadSpecNoCreate())
197 return GetOptionsNoCreate()->GetThreadSpecNoCreate()->GetQueueName();
203 BreakpointLocation::InvokeCallback (StoppointCallbackContext *context)
205 if (m_options_ap.get() != NULL && m_options_ap->HasCallback())
206 return m_options_ap->InvokeCallback (context, m_owner.GetID(), GetID());
208 return m_owner.InvokeCallback (context, GetID());
212 BreakpointLocation::SetCallback (BreakpointHitCallback callback, void *baton,
215 // The default "Baton" class will keep a copy of "baton" and won't free
216 // or delete it when it goes goes out of scope.
217 GetLocationOptions()->SetCallback(callback, BatonSP (new Baton(baton)), is_synchronous);
218 SendBreakpointLocationChangedEvent (eBreakpointEventTypeCommandChanged);
222 BreakpointLocation::SetCallback (BreakpointHitCallback callback, const BatonSP &baton_sp,
225 GetLocationOptions()->SetCallback (callback, baton_sp, is_synchronous);
226 SendBreakpointLocationChangedEvent (eBreakpointEventTypeCommandChanged);
231 BreakpointLocation::ClearCallback ()
233 GetLocationOptions()->ClearCallback();
237 BreakpointLocation::SetCondition (const char *condition)
239 GetLocationOptions()->SetCondition (condition);
240 SendBreakpointLocationChangedEvent (eBreakpointEventTypeConditionChanged);
244 BreakpointLocation::GetConditionText (size_t *hash) const
246 return GetOptionsNoCreate()->GetConditionText(hash);
250 BreakpointLocation::ConditionSaysStop (ExecutionContext &exe_ctx, Error &error)
252 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS);
254 Mutex::Locker evaluation_locker(m_condition_mutex);
256 size_t condition_hash;
257 const char *condition_text = GetConditionText(&condition_hash);
261 m_user_expression_sp.reset();
265 if (condition_hash != m_condition_hash ||
266 !m_user_expression_sp ||
267 !m_user_expression_sp->MatchesContext(exe_ctx))
269 m_user_expression_sp.reset(new ClangUserExpression(condition_text,
271 lldb::eLanguageTypeUnknown,
272 ClangUserExpression::eResultTypeAny));
276 if (!m_user_expression_sp->Parse(errors,
278 eExecutionPolicyOnlyWhenNeeded,
281 error.SetErrorStringWithFormat("Couldn't parse conditional expression:\n%s",
283 m_user_expression_sp.reset();
287 m_condition_hash = condition_hash;
290 // We need to make sure the user sees any parse errors in their condition, so we'll hook the
291 // constructor errors up to the debugger's Async I/O.
293 ValueObjectSP result_value_sp;
295 EvaluateExpressionOptions options;
296 options.SetUnwindOnError(true);
297 options.SetIgnoreBreakpoints(true);
298 options.SetRunOthers(true);
302 StreamString execution_errors;
304 ClangExpressionVariableSP result_variable_sp;
306 ExecutionResults result_code =
307 m_user_expression_sp->Execute(execution_errors,
310 m_user_expression_sp,
315 if (result_code == eExecutionCompleted)
317 if (!result_variable_sp)
320 error.SetErrorString("Expression did not return a result");
324 result_value_sp = result_variable_sp->GetValueObject();
329 if (result_value_sp->ResolveValue (scalar_value))
331 if (scalar_value.ULongLong(1) == 0)
336 log->Printf("Condition successfully evaluated, result is %s.\n",
337 ret ? "true" : "false");
342 error.SetErrorString("Failed to get an integer result from the expression");
348 error.SetErrorString("Failed to get any result from the expression");
354 error.SetErrorStringWithFormat("Couldn't execute expression:\n%s", execution_errors.GetData());
361 BreakpointLocation::GetIgnoreCount ()
363 return GetOptionsNoCreate()->GetIgnoreCount();
367 BreakpointLocation::SetIgnoreCount (uint32_t n)
369 GetLocationOptions()->SetIgnoreCount(n);
370 SendBreakpointLocationChangedEvent (eBreakpointEventTypeIgnoreChanged);
374 BreakpointLocation::DecrementIgnoreCount()
376 if (m_options_ap.get() != NULL)
378 uint32_t loc_ignore = m_options_ap->GetIgnoreCount();
380 m_options_ap->SetIgnoreCount(loc_ignore - 1);
385 BreakpointLocation::IgnoreCountShouldStop()
387 if (m_options_ap.get() != NULL)
389 uint32_t loc_ignore = m_options_ap->GetIgnoreCount();
392 m_owner.DecrementIgnoreCount();
393 DecrementIgnoreCount(); // Have to decrement our owners' ignore count, since it won't get a
401 const BreakpointOptions *
402 BreakpointLocation::GetOptionsNoCreate () const
404 if (m_options_ap.get() != NULL)
405 return m_options_ap.get();
407 return m_owner.GetOptions ();
411 BreakpointLocation::GetLocationOptions ()
413 // If we make the copy we don't copy the callbacks because that is potentially
414 // expensive and we don't want to do that for the simple case where someone is
415 // just disabling the location.
416 if (m_options_ap.get() == NULL)
417 m_options_ap.reset(BreakpointOptions::CopyOptionsNoCallback(*m_owner.GetOptions ()));
419 return m_options_ap.get();
423 BreakpointLocation::ValidForThisThread (Thread *thread)
425 return thread->MatchesSpec(GetOptionsNoCreate()->GetThreadSpecNoCreate());
428 // RETURNS - true if we should stop at this breakpoint, false if we
429 // should continue. Note, we don't check the thread spec for the breakpoint
430 // here, since if the breakpoint is not for this thread, then the event won't
431 // even get reported, so the check is redundant.
434 BreakpointLocation::ShouldStop (StoppointCallbackContext *context)
436 bool should_stop = true;
437 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS);
444 if (!IgnoreCountShouldStop())
447 if (!m_owner.IgnoreCountShouldStop())
450 // We only run synchronous callbacks in ShouldStop:
451 context->is_synchronous = true;
452 should_stop = InvokeCallback (context);
457 GetDescription (&s, lldb::eDescriptionLevelVerbose);
458 log->Printf ("Hit breakpoint location: %s, %s.\n", s.GetData(), should_stop ? "stopping" : "continuing");
465 BreakpointLocation::IsResolved () const
467 return m_bp_site_sp.get() != NULL;
470 lldb::BreakpointSiteSP
471 BreakpointLocation::GetBreakpointSite() const
477 BreakpointLocation::ResolveBreakpointSite ()
482 Process *process = m_owner.GetTarget().GetProcessSP().get();
486 lldb::break_id_t new_id = process->CreateBreakpointSite (shared_from_this(), m_owner.IsHardware());
488 if (new_id == LLDB_INVALID_BREAK_ID)
490 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS);
492 log->Warning ("Tried to add breakpoint site at 0x%" PRIx64 " but it was already present.\n",
493 m_address.GetOpcodeLoadAddress (&m_owner.GetTarget()));
501 BreakpointLocation::SetBreakpointSite (BreakpointSiteSP& bp_site_sp)
503 m_bp_site_sp = bp_site_sp;
508 BreakpointLocation::ClearBreakpointSite ()
510 if (m_bp_site_sp.get())
512 m_owner.GetTarget().GetProcessSP()->RemoveOwnerFromBreakpointSite (GetBreakpoint().GetID(),
513 GetID(), m_bp_site_sp);
514 m_bp_site_sp.reset();
521 BreakpointLocation::GetDescription (Stream *s, lldb::DescriptionLevel level)
525 // If the description level is "initial" then the breakpoint is printing out our initial state,
526 // and we should let it decide how it wants to print our label.
527 if (level != eDescriptionLevelInitial)
530 BreakpointID::GetCanonicalReference(s, m_owner.GetID(), GetID());
533 if (level == lldb::eDescriptionLevelBrief)
536 if (level != eDescriptionLevelInitial)
539 if (level == lldb::eDescriptionLevelVerbose)
542 if (m_address.IsSectionOffset())
544 m_address.CalculateSymbolContext(&sc);
546 if (level == lldb::eDescriptionLevelFull || level == eDescriptionLevelInitial)
548 s->PutCString("where = ");
549 sc.DumpStopContext (s, m_owner.GetTarget().GetProcessSP().get(), m_address, false, true, false);
556 s->Indent("module = ");
557 sc.module_sp->GetFileSpec().Dump (s);
560 if (sc.comp_unit != NULL)
563 s->Indent("compile unit = ");
564 static_cast<FileSpec*>(sc.comp_unit)->GetFilename().Dump (s);
566 if (sc.function != NULL)
569 s->Indent("function = ");
570 s->PutCString (sc.function->GetMangled().GetName().AsCString("<unknown>"));
573 if (sc.line_entry.line > 0)
576 s->Indent("location = ");
577 sc.line_entry.DumpStopContext (s, true);
583 // If we don't have a comp unit, see if we have a symbol we can print.
587 s->Indent("symbol = ");
588 s->PutCString(sc.symbol->GetMangled().GetName().AsCString("<unknown>"));
594 if (level == lldb::eDescriptionLevelVerbose)
600 if (m_address.IsSectionOffset() && (level == eDescriptionLevelFull || level == eDescriptionLevelInitial))
602 s->Printf ("address = ");
604 ExecutionContextScope *exe_scope = NULL;
605 Target *target = &m_owner.GetTarget();
607 exe_scope = target->GetProcessSP().get();
608 if (exe_scope == NULL)
611 if (eDescriptionLevelInitial)
612 m_address.Dump(s, exe_scope, Address::DumpStyleLoadAddress, Address::DumpStyleFileAddress);
614 m_address.Dump(s, exe_scope, Address::DumpStyleLoadAddress, Address::DumpStyleModuleWithFileAddress);
616 if (level == lldb::eDescriptionLevelVerbose)
620 s->Printf("resolved = %s\n", IsResolved() ? "true" : "false");
623 s->Printf ("hit count = %-4u\n", GetHitCount());
625 if (m_options_ap.get())
628 m_options_ap->GetDescription (s, level);
633 else if (level != eDescriptionLevelInitial)
635 s->Printf(", %sresolved, hit count = %u ",
636 (IsResolved() ? "" : "un"),
638 if (m_options_ap.get())
640 m_options_ap->GetDescription (s, level);
646 BreakpointLocation::Dump(Stream *s) const
651 s->Printf("BreakpointLocation %u: tid = %4.4" PRIx64 " load addr = 0x%8.8" PRIx64 " state = %s type = %s breakpoint "
652 "hw_index = %i hit_count = %-4u ignore_count = %-4u",
654 GetOptionsNoCreate()->GetThreadSpecNoCreate()->GetTID(),
655 (uint64_t) m_address.GetOpcodeLoadAddress (&m_owner.GetTarget()),
656 (m_options_ap.get() ? m_options_ap->IsEnabled() : m_owner.IsEnabled()) ? "enabled " : "disabled",
657 IsHardware() ? "hardware" : "software",
660 GetOptionsNoCreate()->GetIgnoreCount());
664 BreakpointLocation::SendBreakpointLocationChangedEvent (lldb::BreakpointEventType eventKind)
667 && !m_owner.IsInternal()
668 && m_owner.GetTarget().EventTypeHasListeners(Target::eBroadcastBitBreakpointChanged))
670 Breakpoint::BreakpointEventData *data = new Breakpoint::BreakpointEventData (eventKind,
671 m_owner.shared_from_this());
672 data->GetBreakpointLocationCollection().Add (shared_from_this());
673 m_owner.GetTarget().BroadcastEvent (Target::eBroadcastBitBreakpointChanged, data);