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;
294 const bool unwind_on_error = true;
295 const bool ignore_breakpoints = true;
296 const bool try_all_threads = true;
300 StreamString execution_errors;
302 ClangExpressionVariableSP result_variable_sp;
304 ExecutionResults result_code =
305 m_user_expression_sp->Execute(execution_errors,
309 m_user_expression_sp,
312 ClangUserExpression::kDefaultTimeout);
316 if (result_code == eExecutionCompleted)
318 if (!result_variable_sp)
321 error.SetErrorString("Expression did not return a result");
325 result_value_sp = result_variable_sp->GetValueObject();
330 if (result_value_sp->ResolveValue (scalar_value))
332 if (scalar_value.ULongLong(1) == 0)
337 log->Printf("Condition successfully evaluated, result is %s.\n",
338 ret ? "true" : "false");
343 error.SetErrorString("Failed to get an integer result from the expression");
349 error.SetErrorString("Failed to get any result from the expression");
355 error.SetErrorStringWithFormat("Couldn't execute expression:\n%s", execution_errors.GetData());
362 BreakpointLocation::GetIgnoreCount ()
364 return GetOptionsNoCreate()->GetIgnoreCount();
368 BreakpointLocation::SetIgnoreCount (uint32_t n)
370 GetLocationOptions()->SetIgnoreCount(n);
371 SendBreakpointLocationChangedEvent (eBreakpointEventTypeIgnoreChanged);
375 BreakpointLocation::DecrementIgnoreCount()
377 if (m_options_ap.get() != NULL)
379 uint32_t loc_ignore = m_options_ap->GetIgnoreCount();
381 m_options_ap->SetIgnoreCount(loc_ignore - 1);
386 BreakpointLocation::IgnoreCountShouldStop()
388 if (m_options_ap.get() != NULL)
390 uint32_t loc_ignore = m_options_ap->GetIgnoreCount();
393 m_owner.DecrementIgnoreCount();
394 DecrementIgnoreCount(); // Have to decrement our owners' ignore count, since it won't get a
402 const BreakpointOptions *
403 BreakpointLocation::GetOptionsNoCreate () const
405 if (m_options_ap.get() != NULL)
406 return m_options_ap.get();
408 return m_owner.GetOptions ();
412 BreakpointLocation::GetLocationOptions ()
414 // If we make the copy we don't copy the callbacks because that is potentially
415 // expensive and we don't want to do that for the simple case where someone is
416 // just disabling the location.
417 if (m_options_ap.get() == NULL)
418 m_options_ap.reset(BreakpointOptions::CopyOptionsNoCallback(*m_owner.GetOptions ()));
420 return m_options_ap.get();
424 BreakpointLocation::ValidForThisThread (Thread *thread)
426 return thread->MatchesSpec(GetOptionsNoCreate()->GetThreadSpecNoCreate());
429 // RETURNS - true if we should stop at this breakpoint, false if we
430 // should continue. Note, we don't check the thread spec for the breakpoint
431 // here, since if the breakpoint is not for this thread, then the event won't
432 // even get reported, so the check is redundant.
435 BreakpointLocation::ShouldStop (StoppointCallbackContext *context)
437 bool should_stop = true;
438 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS);
445 if (!IgnoreCountShouldStop())
448 if (!m_owner.IgnoreCountShouldStop())
451 // We only run synchronous callbacks in ShouldStop:
452 context->is_synchronous = true;
453 should_stop = InvokeCallback (context);
458 GetDescription (&s, lldb::eDescriptionLevelVerbose);
459 log->Printf ("Hit breakpoint location: %s, %s.\n", s.GetData(), should_stop ? "stopping" : "continuing");
466 BreakpointLocation::IsResolved () const
468 return m_bp_site_sp.get() != NULL;
471 lldb::BreakpointSiteSP
472 BreakpointLocation::GetBreakpointSite() const
478 BreakpointLocation::ResolveBreakpointSite ()
483 Process *process = m_owner.GetTarget().GetProcessSP().get();
487 lldb::break_id_t new_id = process->CreateBreakpointSite (shared_from_this(), false);
489 if (new_id == LLDB_INVALID_BREAK_ID)
491 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS);
493 log->Warning ("Tried to add breakpoint site at 0x%" PRIx64 " but it was already present.\n",
494 m_address.GetOpcodeLoadAddress (&m_owner.GetTarget()));
502 BreakpointLocation::SetBreakpointSite (BreakpointSiteSP& bp_site_sp)
504 m_bp_site_sp = bp_site_sp;
509 BreakpointLocation::ClearBreakpointSite ()
511 if (m_bp_site_sp.get())
513 m_owner.GetTarget().GetProcessSP()->RemoveOwnerFromBreakpointSite (GetBreakpoint().GetID(),
514 GetID(), m_bp_site_sp);
515 m_bp_site_sp.reset();
522 BreakpointLocation::GetDescription (Stream *s, lldb::DescriptionLevel level)
526 // If the description level is "initial" then the breakpoint is printing out our initial state,
527 // and we should let it decide how it wants to print our label.
528 if (level != eDescriptionLevelInitial)
531 BreakpointID::GetCanonicalReference(s, m_owner.GetID(), GetID());
534 if (level == lldb::eDescriptionLevelBrief)
537 if (level != eDescriptionLevelInitial)
540 if (level == lldb::eDescriptionLevelVerbose)
543 if (m_address.IsSectionOffset())
545 m_address.CalculateSymbolContext(&sc);
547 if (level == lldb::eDescriptionLevelFull || level == eDescriptionLevelInitial)
549 s->PutCString("where = ");
550 sc.DumpStopContext (s, m_owner.GetTarget().GetProcessSP().get(), m_address, false, true, false);
557 s->Indent("module = ");
558 sc.module_sp->GetFileSpec().Dump (s);
561 if (sc.comp_unit != NULL)
564 s->Indent("compile unit = ");
565 static_cast<FileSpec*>(sc.comp_unit)->GetFilename().Dump (s);
567 if (sc.function != NULL)
570 s->Indent("function = ");
571 s->PutCString (sc.function->GetMangled().GetName().AsCString("<unknown>"));
574 if (sc.line_entry.line > 0)
577 s->Indent("location = ");
578 sc.line_entry.DumpStopContext (s, true);
584 // If we don't have a comp unit, see if we have a symbol we can print.
588 s->Indent("symbol = ");
589 s->PutCString(sc.symbol->GetMangled().GetName().AsCString("<unknown>"));
595 if (level == lldb::eDescriptionLevelVerbose)
601 if (m_address.IsSectionOffset() && (level == eDescriptionLevelFull || level == eDescriptionLevelInitial))
603 s->Printf ("address = ");
605 ExecutionContextScope *exe_scope = NULL;
606 Target *target = &m_owner.GetTarget();
608 exe_scope = target->GetProcessSP().get();
609 if (exe_scope == NULL)
612 if (eDescriptionLevelInitial)
613 m_address.Dump(s, exe_scope, Address::DumpStyleLoadAddress, Address::DumpStyleFileAddress);
615 m_address.Dump(s, exe_scope, Address::DumpStyleLoadAddress, Address::DumpStyleModuleWithFileAddress);
617 if (level == lldb::eDescriptionLevelVerbose)
621 s->Printf("resolved = %s\n", IsResolved() ? "true" : "false");
624 s->Printf ("hit count = %-4u\n", GetHitCount());
626 if (m_options_ap.get())
629 m_options_ap->GetDescription (s, level);
634 else if (level != eDescriptionLevelInitial)
636 s->Printf(", %sresolved, hit count = %u ",
637 (IsResolved() ? "" : "un"),
639 if (m_options_ap.get())
641 m_options_ap->GetDescription (s, level);
647 BreakpointLocation::Dump(Stream *s) const
652 s->Printf("BreakpointLocation %u: tid = %4.4" PRIx64 " load addr = 0x%8.8" PRIx64 " state = %s type = %s breakpoint "
653 "hw_index = %i hit_count = %-4u ignore_count = %-4u",
655 GetOptionsNoCreate()->GetThreadSpecNoCreate()->GetTID(),
656 (uint64_t) m_address.GetOpcodeLoadAddress (&m_owner.GetTarget()),
657 (m_options_ap.get() ? m_options_ap->IsEnabled() : m_owner.IsEnabled()) ? "enabled " : "disabled",
658 IsHardware() ? "hardware" : "software",
661 GetOptionsNoCreate()->GetIgnoreCount());
665 BreakpointLocation::SendBreakpointLocationChangedEvent (lldb::BreakpointEventType eventKind)
668 && !m_owner.IsInternal()
669 && m_owner.GetTarget().EventTypeHasListeners(Target::eBroadcastBitBreakpointChanged))
671 Breakpoint::BreakpointEventData *data = new Breakpoint::BreakpointEventData (eventKind,
672 m_owner.shared_from_this());
673 data->GetBreakpointLocationCollection().Add (shared_from_this());
674 m_owner.GetTarget().BroadcastEvent (Target::eBroadcastBitBreakpointChanged, data);