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
43 bool check_for_resolver
45 StoppointLocation (loc_id, addr.GetOpcodeLoadAddress(&owner.GetTarget()), hardware),
46 m_being_created(true),
47 m_should_resolve_indirect_functions (false),
48 m_is_reexported (false),
49 m_is_indirect (false),
56 if (check_for_resolver)
58 Symbol *symbol = m_address.CalculateSymbolContextSymbol();
59 if (symbol && symbol->IsIndirect())
61 SetShouldResolveIndirectFunctions (true);
66 m_being_created = false;
69 BreakpointLocation::~BreakpointLocation()
71 ClearBreakpointSite();
75 BreakpointLocation::GetLoadAddress () const
77 return m_address.GetOpcodeLoadAddress (&m_owner.GetTarget());
81 BreakpointLocation::GetAddress ()
87 BreakpointLocation::GetBreakpoint ()
93 BreakpointLocation::IsEnabled () const
95 if (!m_owner.IsEnabled())
97 else if (m_options_ap.get() != NULL)
98 return m_options_ap->IsEnabled();
104 BreakpointLocation::SetEnabled (bool enabled)
106 GetLocationOptions()->SetEnabled(enabled);
109 ResolveBreakpointSite();
113 ClearBreakpointSite();
115 SendBreakpointLocationChangedEvent (enabled ? eBreakpointEventTypeEnabled : eBreakpointEventTypeDisabled);
119 BreakpointLocation::SetThreadID (lldb::tid_t thread_id)
121 if (thread_id != LLDB_INVALID_THREAD_ID)
122 GetLocationOptions()->SetThreadID(thread_id);
125 // If we're resetting this to an invalid thread id, then
126 // don't make an options pointer just to do that.
127 if (m_options_ap.get() != NULL)
128 m_options_ap->SetThreadID (thread_id);
130 SendBreakpointLocationChangedEvent (eBreakpointEventTypeThreadChanged);
134 BreakpointLocation::GetThreadID ()
136 if (GetOptionsNoCreate()->GetThreadSpecNoCreate())
137 return GetOptionsNoCreate()->GetThreadSpecNoCreate()->GetTID();
139 return LLDB_INVALID_THREAD_ID;
143 BreakpointLocation::SetThreadIndex (uint32_t index)
146 GetLocationOptions()->GetThreadSpec()->SetIndex(index);
149 // If we're resetting this to an invalid thread id, then
150 // don't make an options pointer just to do that.
151 if (m_options_ap.get() != NULL)
152 m_options_ap->GetThreadSpec()->SetIndex(index);
154 SendBreakpointLocationChangedEvent (eBreakpointEventTypeThreadChanged);
159 BreakpointLocation::GetThreadIndex() const
161 if (GetOptionsNoCreate()->GetThreadSpecNoCreate())
162 return GetOptionsNoCreate()->GetThreadSpecNoCreate()->GetIndex();
168 BreakpointLocation::SetThreadName (const char *thread_name)
170 if (thread_name != NULL)
171 GetLocationOptions()->GetThreadSpec()->SetName(thread_name);
174 // If we're resetting this to an invalid thread id, then
175 // don't make an options pointer just to do that.
176 if (m_options_ap.get() != NULL)
177 m_options_ap->GetThreadSpec()->SetName(thread_name);
179 SendBreakpointLocationChangedEvent (eBreakpointEventTypeThreadChanged);
183 BreakpointLocation::GetThreadName () const
185 if (GetOptionsNoCreate()->GetThreadSpecNoCreate())
186 return GetOptionsNoCreate()->GetThreadSpecNoCreate()->GetName();
192 BreakpointLocation::SetQueueName (const char *queue_name)
194 if (queue_name != NULL)
195 GetLocationOptions()->GetThreadSpec()->SetQueueName(queue_name);
198 // If we're resetting this to an invalid thread id, then
199 // don't make an options pointer just to do that.
200 if (m_options_ap.get() != NULL)
201 m_options_ap->GetThreadSpec()->SetQueueName(queue_name);
203 SendBreakpointLocationChangedEvent (eBreakpointEventTypeThreadChanged);
207 BreakpointLocation::GetQueueName () const
209 if (GetOptionsNoCreate()->GetThreadSpecNoCreate())
210 return GetOptionsNoCreate()->GetThreadSpecNoCreate()->GetQueueName();
216 BreakpointLocation::InvokeCallback (StoppointCallbackContext *context)
218 if (m_options_ap.get() != NULL && m_options_ap->HasCallback())
219 return m_options_ap->InvokeCallback (context, m_owner.GetID(), GetID());
221 return m_owner.InvokeCallback (context, GetID());
225 BreakpointLocation::SetCallback (BreakpointHitCallback callback, void *baton,
228 // The default "Baton" class will keep a copy of "baton" and won't free
229 // or delete it when it goes goes out of scope.
230 GetLocationOptions()->SetCallback(callback, BatonSP (new Baton(baton)), is_synchronous);
231 SendBreakpointLocationChangedEvent (eBreakpointEventTypeCommandChanged);
235 BreakpointLocation::SetCallback (BreakpointHitCallback callback, const BatonSP &baton_sp,
238 GetLocationOptions()->SetCallback (callback, baton_sp, is_synchronous);
239 SendBreakpointLocationChangedEvent (eBreakpointEventTypeCommandChanged);
244 BreakpointLocation::ClearCallback ()
246 GetLocationOptions()->ClearCallback();
250 BreakpointLocation::SetCondition (const char *condition)
252 GetLocationOptions()->SetCondition (condition);
253 SendBreakpointLocationChangedEvent (eBreakpointEventTypeConditionChanged);
257 BreakpointLocation::GetConditionText (size_t *hash) const
259 return GetOptionsNoCreate()->GetConditionText(hash);
263 BreakpointLocation::ConditionSaysStop (ExecutionContext &exe_ctx, Error &error)
265 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS);
267 Mutex::Locker evaluation_locker(m_condition_mutex);
269 size_t condition_hash;
270 const char *condition_text = GetConditionText(&condition_hash);
274 m_user_expression_sp.reset();
278 if (condition_hash != m_condition_hash ||
279 !m_user_expression_sp ||
280 !m_user_expression_sp->MatchesContext(exe_ctx))
282 m_user_expression_sp.reset(new ClangUserExpression(condition_text,
284 lldb::eLanguageTypeUnknown,
285 ClangUserExpression::eResultTypeAny));
289 if (!m_user_expression_sp->Parse(errors,
291 eExecutionPolicyOnlyWhenNeeded,
294 error.SetErrorStringWithFormat("Couldn't parse conditional expression:\n%s",
296 m_user_expression_sp.reset();
300 m_condition_hash = condition_hash;
303 // We need to make sure the user sees any parse errors in their condition, so we'll hook the
304 // constructor errors up to the debugger's Async I/O.
306 ValueObjectSP result_value_sp;
308 EvaluateExpressionOptions options;
309 options.SetUnwindOnError(true);
310 options.SetIgnoreBreakpoints(true);
311 options.SetTryAllThreads(true);
315 StreamString execution_errors;
317 ClangExpressionVariableSP result_variable_sp;
319 ExecutionResults result_code =
320 m_user_expression_sp->Execute(execution_errors,
323 m_user_expression_sp,
328 if (result_code == eExecutionCompleted)
330 if (!result_variable_sp)
333 error.SetErrorString("Expression did not return a result");
337 result_value_sp = result_variable_sp->GetValueObject();
342 if (result_value_sp->ResolveValue (scalar_value))
344 if (scalar_value.ULongLong(1) == 0)
349 log->Printf("Condition successfully evaluated, result is %s.\n",
350 ret ? "true" : "false");
355 error.SetErrorString("Failed to get an integer result from the expression");
361 error.SetErrorString("Failed to get any result from the expression");
367 error.SetErrorStringWithFormat("Couldn't execute expression:\n%s", execution_errors.GetData());
374 BreakpointLocation::GetIgnoreCount ()
376 return GetOptionsNoCreate()->GetIgnoreCount();
380 BreakpointLocation::SetIgnoreCount (uint32_t n)
382 GetLocationOptions()->SetIgnoreCount(n);
383 SendBreakpointLocationChangedEvent (eBreakpointEventTypeIgnoreChanged);
387 BreakpointLocation::DecrementIgnoreCount()
389 if (m_options_ap.get() != NULL)
391 uint32_t loc_ignore = m_options_ap->GetIgnoreCount();
393 m_options_ap->SetIgnoreCount(loc_ignore - 1);
398 BreakpointLocation::IgnoreCountShouldStop()
400 if (m_options_ap.get() != NULL)
402 uint32_t loc_ignore = m_options_ap->GetIgnoreCount();
405 m_owner.DecrementIgnoreCount();
406 DecrementIgnoreCount(); // Have to decrement our owners' ignore count, since it won't get a
414 const BreakpointOptions *
415 BreakpointLocation::GetOptionsNoCreate () const
417 if (m_options_ap.get() != NULL)
418 return m_options_ap.get();
420 return m_owner.GetOptions ();
424 BreakpointLocation::GetLocationOptions ()
426 // If we make the copy we don't copy the callbacks because that is potentially
427 // expensive and we don't want to do that for the simple case where someone is
428 // just disabling the location.
429 if (m_options_ap.get() == NULL)
430 m_options_ap.reset(BreakpointOptions::CopyOptionsNoCallback(*m_owner.GetOptions ()));
432 return m_options_ap.get();
436 BreakpointLocation::ValidForThisThread (Thread *thread)
438 return thread->MatchesSpec(GetOptionsNoCreate()->GetThreadSpecNoCreate());
441 // RETURNS - true if we should stop at this breakpoint, false if we
442 // should continue. Note, we don't check the thread spec for the breakpoint
443 // here, since if the breakpoint is not for this thread, then the event won't
444 // even get reported, so the check is redundant.
447 BreakpointLocation::ShouldStop (StoppointCallbackContext *context)
449 bool should_stop = true;
450 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS);
457 if (!IgnoreCountShouldStop())
460 if (!m_owner.IgnoreCountShouldStop())
463 // We only run synchronous callbacks in ShouldStop:
464 context->is_synchronous = true;
465 should_stop = InvokeCallback (context);
470 GetDescription (&s, lldb::eDescriptionLevelVerbose);
471 log->Printf ("Hit breakpoint location: %s, %s.\n", s.GetData(), should_stop ? "stopping" : "continuing");
478 BreakpointLocation::IsResolved () const
480 return m_bp_site_sp.get() != NULL;
483 lldb::BreakpointSiteSP
484 BreakpointLocation::GetBreakpointSite() const
490 BreakpointLocation::ResolveBreakpointSite ()
495 Process *process = m_owner.GetTarget().GetProcessSP().get();
499 lldb::break_id_t new_id = process->CreateBreakpointSite (shared_from_this(), m_owner.IsHardware());
501 if (new_id == LLDB_INVALID_BREAK_ID)
503 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS);
505 log->Warning ("Tried to add breakpoint site at 0x%" PRIx64 " but it was already present.\n",
506 m_address.GetOpcodeLoadAddress (&m_owner.GetTarget()));
514 BreakpointLocation::SetBreakpointSite (BreakpointSiteSP& bp_site_sp)
516 m_bp_site_sp = bp_site_sp;
521 BreakpointLocation::ClearBreakpointSite ()
523 if (m_bp_site_sp.get())
525 m_owner.GetTarget().GetProcessSP()->RemoveOwnerFromBreakpointSite (GetBreakpoint().GetID(),
526 GetID(), m_bp_site_sp);
527 m_bp_site_sp.reset();
534 BreakpointLocation::GetDescription (Stream *s, lldb::DescriptionLevel level)
538 // If the description level is "initial" then the breakpoint is printing out our initial state,
539 // and we should let it decide how it wants to print our label.
540 if (level != eDescriptionLevelInitial)
543 BreakpointID::GetCanonicalReference(s, m_owner.GetID(), GetID());
546 if (level == lldb::eDescriptionLevelBrief)
549 if (level != eDescriptionLevelInitial)
552 if (level == lldb::eDescriptionLevelVerbose)
555 if (m_address.IsSectionOffset())
557 m_address.CalculateSymbolContext(&sc);
559 if (level == lldb::eDescriptionLevelFull || level == eDescriptionLevelInitial)
562 s->PutCString ("re-exported target = ");
564 s->PutCString("where = ");
565 sc.DumpStopContext (s, m_owner.GetTarget().GetProcessSP().get(), m_address, false, true, false);
572 s->Indent("module = ");
573 sc.module_sp->GetFileSpec().Dump (s);
576 if (sc.comp_unit != NULL)
579 s->Indent("compile unit = ");
580 static_cast<FileSpec*>(sc.comp_unit)->GetFilename().Dump (s);
582 if (sc.function != NULL)
585 s->Indent("function = ");
586 s->PutCString (sc.function->GetMangled().GetName().AsCString("<unknown>"));
589 if (sc.line_entry.line > 0)
592 s->Indent("location = ");
593 sc.line_entry.DumpStopContext (s, true);
599 // If we don't have a comp unit, see if we have a symbol we can print.
604 s->Indent ("re-exported target = ");
606 s->Indent("symbol = ");
607 s->PutCString(sc.symbol->GetMangled().GetName().AsCString("<unknown>"));
613 if (level == lldb::eDescriptionLevelVerbose)
619 if (m_address.IsSectionOffset() && (level == eDescriptionLevelFull || level == eDescriptionLevelInitial))
621 s->Printf ("address = ");
623 ExecutionContextScope *exe_scope = NULL;
624 Target *target = &m_owner.GetTarget();
626 exe_scope = target->GetProcessSP().get();
627 if (exe_scope == NULL)
630 if (eDescriptionLevelInitial)
631 m_address.Dump(s, exe_scope, Address::DumpStyleLoadAddress, Address::DumpStyleFileAddress);
633 m_address.Dump(s, exe_scope, Address::DumpStyleLoadAddress, Address::DumpStyleModuleWithFileAddress);
635 if (IsIndirect() && m_bp_site_sp)
637 Address resolved_address;
638 resolved_address.SetLoadAddress(m_bp_site_sp->GetLoadAddress(), target);
639 Symbol *resolved_symbol = resolved_address.CalculateSymbolContextSymbol();
642 if (level == eDescriptionLevelFull || level == eDescriptionLevelInitial)
644 else if (level == lldb::eDescriptionLevelVerbose)
649 s->Printf ("indirect target = %s", resolved_symbol->GetName().GetCString());
653 if (level == lldb::eDescriptionLevelVerbose)
657 s->Printf("resolved = %s\n", IsResolved() ? "true" : "false");
660 s->Printf ("hit count = %-4u\n", GetHitCount());
662 if (m_options_ap.get())
665 m_options_ap->GetDescription (s, level);
670 else if (level != eDescriptionLevelInitial)
672 s->Printf(", %sresolved, hit count = %u ",
673 (IsResolved() ? "" : "un"),
675 if (m_options_ap.get())
677 m_options_ap->GetDescription (s, level);
683 BreakpointLocation::Dump(Stream *s) const
688 s->Printf("BreakpointLocation %u: tid = %4.4" PRIx64 " load addr = 0x%8.8" PRIx64 " state = %s type = %s breakpoint "
689 "hw_index = %i hit_count = %-4u ignore_count = %-4u",
691 GetOptionsNoCreate()->GetThreadSpecNoCreate()->GetTID(),
692 (uint64_t) m_address.GetOpcodeLoadAddress (&m_owner.GetTarget()),
693 (m_options_ap.get() ? m_options_ap->IsEnabled() : m_owner.IsEnabled()) ? "enabled " : "disabled",
694 IsHardware() ? "hardware" : "software",
697 GetOptionsNoCreate()->GetIgnoreCount());
701 BreakpointLocation::SendBreakpointLocationChangedEvent (lldb::BreakpointEventType eventKind)
704 && !m_owner.IsInternal()
705 && m_owner.GetTarget().EventTypeHasListeners(Target::eBroadcastBitBreakpointChanged))
707 Breakpoint::BreakpointEventData *data = new Breakpoint::BreakpointEventData (eventKind,
708 m_owner.shared_from_this());
709 data->GetBreakpointLocationCollection().Add (shared_from_this());
710 m_owner.GetTarget().BroadcastEvent (Target::eBroadcastBitBreakpointChanged, data);