1 //===-- SBBreakpoint.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/API/SBBreakpoint.h"
11 #include "lldb/API/SBBreakpointLocation.h"
12 #include "lldb/API/SBDebugger.h"
13 #include "lldb/API/SBEvent.h"
14 #include "lldb/API/SBProcess.h"
15 #include "lldb/API/SBStream.h"
16 #include "lldb/API/SBThread.h"
18 #include "lldb/Breakpoint/Breakpoint.h"
19 #include "lldb/Breakpoint/BreakpointLocation.h"
20 #include "lldb/Breakpoint/StoppointCallbackContext.h"
21 #include "lldb/Core/Address.h"
22 #include "lldb/Core/Log.h"
23 #include "lldb/Core/Stream.h"
24 #include "lldb/Core/StreamFile.h"
25 #include "lldb/Target/Process.h"
26 #include "lldb/Target/SectionLoadList.h"
27 #include "lldb/Target/Target.h"
28 #include "lldb/Target/Thread.h"
29 #include "lldb/Target/ThreadSpec.h"
32 #include "lldb/lldb-enumerations.h"
35 using namespace lldb_private;
39 SBBreakpoint::BreakpointHitCallback callback;
43 class SBBreakpointCallbackBaton : public Baton
47 SBBreakpointCallbackBaton (SBBreakpoint::BreakpointHitCallback callback, void *baton) :
48 Baton (new CallbackData)
50 CallbackData *data = (CallbackData *)m_data;
51 data->callback = callback;
52 data->callback_baton = baton;
55 virtual ~SBBreakpointCallbackBaton()
57 CallbackData *data = (CallbackData *)m_data;
68 SBBreakpoint::SBBreakpoint () :
73 SBBreakpoint::SBBreakpoint (const SBBreakpoint& rhs) :
74 m_opaque_sp (rhs.m_opaque_sp)
79 SBBreakpoint::SBBreakpoint (const lldb::BreakpointSP &bp_sp) :
84 SBBreakpoint::~SBBreakpoint()
89 SBBreakpoint::operator = (const SBBreakpoint& rhs)
92 m_opaque_sp = rhs.m_opaque_sp;
97 SBBreakpoint::operator == (const lldb::SBBreakpoint& rhs)
99 if (m_opaque_sp && rhs.m_opaque_sp)
100 return m_opaque_sp.get() == rhs.m_opaque_sp.get();
105 SBBreakpoint::operator != (const lldb::SBBreakpoint& rhs)
107 if (m_opaque_sp && rhs.m_opaque_sp)
108 return m_opaque_sp.get() != rhs.m_opaque_sp.get();
109 return (m_opaque_sp && !rhs.m_opaque_sp) || (rhs.m_opaque_sp && !m_opaque_sp);
113 SBBreakpoint::GetID () const
115 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
117 break_id_t break_id = LLDB_INVALID_BREAK_ID;
119 break_id = m_opaque_sp->GetID();
123 if (break_id == LLDB_INVALID_BREAK_ID)
124 log->Printf ("SBBreakpoint(%p)::GetID () => LLDB_INVALID_BREAK_ID", m_opaque_sp.get());
126 log->Printf ("SBBreakpoint(%p)::GetID () => %u", m_opaque_sp.get(), break_id);
134 SBBreakpoint::IsValid() const
136 return (bool) m_opaque_sp;
140 SBBreakpoint::ClearAllBreakpointSites ()
144 Mutex::Locker api_locker (m_opaque_sp->GetTarget().GetAPIMutex());
145 m_opaque_sp->ClearAllBreakpointSites ();
150 SBBreakpoint::FindLocationByAddress (addr_t vm_addr)
152 SBBreakpointLocation sb_bp_location;
156 if (vm_addr != LLDB_INVALID_ADDRESS)
158 Mutex::Locker api_locker (m_opaque_sp->GetTarget().GetAPIMutex());
160 Target &target = m_opaque_sp->GetTarget();
161 if (target.GetSectionLoadList().ResolveLoadAddress (vm_addr, address) == false)
163 address.SetRawAddress (vm_addr);
165 sb_bp_location.SetLocation (m_opaque_sp->FindLocationByAddress (address));
168 return sb_bp_location;
172 SBBreakpoint::FindLocationIDByAddress (addr_t vm_addr)
174 break_id_t break_id = LLDB_INVALID_BREAK_ID;
176 if (m_opaque_sp && vm_addr != LLDB_INVALID_ADDRESS)
178 Mutex::Locker api_locker (m_opaque_sp->GetTarget().GetAPIMutex());
180 Target &target = m_opaque_sp->GetTarget();
181 if (target.GetSectionLoadList().ResolveLoadAddress (vm_addr, address) == false)
183 address.SetRawAddress (vm_addr);
185 break_id = m_opaque_sp->FindLocationIDByAddress (address);
192 SBBreakpoint::FindLocationByID (break_id_t bp_loc_id)
194 SBBreakpointLocation sb_bp_location;
198 Mutex::Locker api_locker (m_opaque_sp->GetTarget().GetAPIMutex());
199 sb_bp_location.SetLocation (m_opaque_sp->FindLocationByID (bp_loc_id));
202 return sb_bp_location;
206 SBBreakpoint::GetLocationAtIndex (uint32_t index)
208 SBBreakpointLocation sb_bp_location;
212 Mutex::Locker api_locker (m_opaque_sp->GetTarget().GetAPIMutex());
213 sb_bp_location.SetLocation (m_opaque_sp->GetLocationAtIndex (index));
216 return sb_bp_location;
220 SBBreakpoint::SetEnabled (bool enable)
222 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
225 log->Printf ("SBBreakpoint(%p)::SetEnabled (enabled=%i)", m_opaque_sp.get(), enable);
229 Mutex::Locker api_locker (m_opaque_sp->GetTarget().GetAPIMutex());
230 m_opaque_sp->SetEnabled (enable);
235 SBBreakpoint::IsEnabled ()
239 Mutex::Locker api_locker (m_opaque_sp->GetTarget().GetAPIMutex());
240 return m_opaque_sp->IsEnabled();
247 SBBreakpoint::SetOneShot (bool one_shot)
249 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
252 log->Printf ("SBBreakpoint(%p)::SetOneShot (one_shot=%i)", m_opaque_sp.get(), one_shot);
256 Mutex::Locker api_locker (m_opaque_sp->GetTarget().GetAPIMutex());
257 m_opaque_sp->SetOneShot (one_shot);
262 SBBreakpoint::IsOneShot () const
266 Mutex::Locker api_locker (m_opaque_sp->GetTarget().GetAPIMutex());
267 return m_opaque_sp->IsOneShot();
274 SBBreakpoint::IsInternal ()
278 Mutex::Locker api_locker (m_opaque_sp->GetTarget().GetAPIMutex());
279 return m_opaque_sp->IsInternal();
286 SBBreakpoint::SetIgnoreCount (uint32_t count)
288 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
291 log->Printf ("SBBreakpoint(%p)::SetIgnoreCount (count=%u)", m_opaque_sp.get(), count);
295 Mutex::Locker api_locker (m_opaque_sp->GetTarget().GetAPIMutex());
296 m_opaque_sp->SetIgnoreCount (count);
301 SBBreakpoint::SetCondition (const char *condition)
305 Mutex::Locker api_locker (m_opaque_sp->GetTarget().GetAPIMutex());
306 m_opaque_sp->SetCondition (condition);
311 SBBreakpoint::GetCondition ()
315 Mutex::Locker api_locker (m_opaque_sp->GetTarget().GetAPIMutex());
316 return m_opaque_sp->GetConditionText ();
322 SBBreakpoint::GetHitCount () const
327 Mutex::Locker api_locker (m_opaque_sp->GetTarget().GetAPIMutex());
328 count = m_opaque_sp->GetHitCount();
331 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
333 log->Printf ("SBBreakpoint(%p)::GetHitCount () => %u", m_opaque_sp.get(), count);
339 SBBreakpoint::GetIgnoreCount () const
344 Mutex::Locker api_locker (m_opaque_sp->GetTarget().GetAPIMutex());
345 count = m_opaque_sp->GetIgnoreCount();
348 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
350 log->Printf ("SBBreakpoint(%p)::GetIgnoreCount () => %u", m_opaque_sp.get(), count);
356 SBBreakpoint::SetThreadID (tid_t tid)
360 Mutex::Locker api_locker (m_opaque_sp->GetTarget().GetAPIMutex());
361 m_opaque_sp->SetThreadID (tid);
363 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
365 log->Printf ("SBBreakpoint(%p)::SetThreadID (tid=0x%4.4" PRIx64 ")", m_opaque_sp.get(), tid);
370 SBBreakpoint::GetThreadID ()
372 tid_t tid = LLDB_INVALID_THREAD_ID;
375 Mutex::Locker api_locker (m_opaque_sp->GetTarget().GetAPIMutex());
376 tid = m_opaque_sp->GetThreadID();
379 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
381 log->Printf ("SBBreakpoint(%p)::GetThreadID () => 0x%4.4" PRIx64, m_opaque_sp.get(), tid);
386 SBBreakpoint::SetThreadIndex (uint32_t index)
388 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
390 log->Printf ("SBBreakpoint(%p)::SetThreadIndex (%u)", m_opaque_sp.get(), index);
393 Mutex::Locker api_locker (m_opaque_sp->GetTarget().GetAPIMutex());
394 m_opaque_sp->GetOptions()->GetThreadSpec()->SetIndex (index);
399 SBBreakpoint::GetThreadIndex() const
401 uint32_t thread_idx = UINT32_MAX;
404 Mutex::Locker api_locker (m_opaque_sp->GetTarget().GetAPIMutex());
405 const ThreadSpec *thread_spec = m_opaque_sp->GetOptions()->GetThreadSpecNoCreate();
406 if (thread_spec != NULL)
407 thread_idx = thread_spec->GetIndex();
409 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
411 log->Printf ("SBBreakpoint(%p)::GetThreadIndex () => %u", m_opaque_sp.get(), thread_idx);
418 SBBreakpoint::SetThreadName (const char *thread_name)
420 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
422 log->Printf ("SBBreakpoint(%p)::SetThreadName (%s)", m_opaque_sp.get(), thread_name);
426 Mutex::Locker api_locker (m_opaque_sp->GetTarget().GetAPIMutex());
427 m_opaque_sp->GetOptions()->GetThreadSpec()->SetName (thread_name);
432 SBBreakpoint::GetThreadName () const
434 const char *name = NULL;
437 Mutex::Locker api_locker (m_opaque_sp->GetTarget().GetAPIMutex());
438 const ThreadSpec *thread_spec = m_opaque_sp->GetOptions()->GetThreadSpecNoCreate();
439 if (thread_spec != NULL)
440 name = thread_spec->GetName();
442 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
444 log->Printf ("SBBreakpoint(%p)::GetThreadName () => %s", m_opaque_sp.get(), name);
450 SBBreakpoint::SetQueueName (const char *queue_name)
452 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
454 log->Printf ("SBBreakpoint(%p)::SetQueueName (%s)", m_opaque_sp.get(), queue_name);
457 Mutex::Locker api_locker (m_opaque_sp->GetTarget().GetAPIMutex());
458 m_opaque_sp->GetOptions()->GetThreadSpec()->SetQueueName (queue_name);
463 SBBreakpoint::GetQueueName () const
465 const char *name = NULL;
468 Mutex::Locker api_locker (m_opaque_sp->GetTarget().GetAPIMutex());
469 const ThreadSpec *thread_spec = m_opaque_sp->GetOptions()->GetThreadSpecNoCreate();
471 name = thread_spec->GetQueueName();
473 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
475 log->Printf ("SBBreakpoint(%p)::GetQueueName () => %s", m_opaque_sp.get(), name);
481 SBBreakpoint::GetNumResolvedLocations() const
483 size_t num_resolved = 0;
486 Mutex::Locker api_locker (m_opaque_sp->GetTarget().GetAPIMutex());
487 num_resolved = m_opaque_sp->GetNumResolvedLocations();
489 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
491 log->Printf ("SBBreakpoint(%p)::GetNumResolvedLocations () => %" PRIu64, m_opaque_sp.get(), (uint64_t)num_resolved);
496 SBBreakpoint::GetNumLocations() const
501 Mutex::Locker api_locker (m_opaque_sp->GetTarget().GetAPIMutex());
502 num_locs = m_opaque_sp->GetNumLocations();
504 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
506 log->Printf ("SBBreakpoint(%p)::GetNumLocations () => %" PRIu64, m_opaque_sp.get(), (uint64_t)num_locs);
511 SBBreakpoint::GetDescription (SBStream &s)
515 Mutex::Locker api_locker (m_opaque_sp->GetTarget().GetAPIMutex());
516 s.Printf("SBBreakpoint: id = %i, ", m_opaque_sp->GetID());
517 m_opaque_sp->GetResolverDescription (s.get());
518 m_opaque_sp->GetFilterDescription (s.get());
519 const size_t num_locations = m_opaque_sp->GetNumLocations ();
520 s.Printf(", locations = %" PRIu64, (uint64_t)num_locations);
523 s.Printf ("No value");
528 SBBreakpoint::PrivateBreakpointHitCallback
531 StoppointCallbackContext *ctx,
532 lldb::user_id_t break_id,
533 lldb::user_id_t break_loc_id
536 ExecutionContext exe_ctx (ctx->exe_ctx_ref);
537 BreakpointSP bp_sp(exe_ctx.GetTargetRef().GetBreakpointList().FindBreakpointByID(break_id));
540 CallbackData *data = (CallbackData *)baton;
541 lldb_private::Breakpoint *bp = bp_sp.get();
542 if (bp && data->callback)
544 Process *process = exe_ctx.GetProcessPtr();
547 SBProcess sb_process (process->shared_from_this());
549 SBBreakpointLocation sb_location;
551 sb_location.SetLocation (bp_sp->FindLocationByID (break_loc_id));
552 Thread *thread = exe_ctx.GetThreadPtr();
554 sb_thread.SetThread(thread->shared_from_this());
556 return data->callback (data->callback_baton,
563 return true; // Return true if we should stop at this breakpoint
567 SBBreakpoint::SetCallback (BreakpointHitCallback callback, void *baton)
569 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
572 log->Printf ("SBBreakpoint(%p)::SetCallback (callback=%p, baton=%p)", m_opaque_sp.get(), callback, baton);
576 Mutex::Locker api_locker (m_opaque_sp->GetTarget().GetAPIMutex());
577 BatonSP baton_sp(new SBBreakpointCallbackBaton (callback, baton));
578 m_opaque_sp->SetCallback (SBBreakpoint::PrivateBreakpointHitCallback, baton_sp, false);
583 lldb_private::Breakpoint *
584 SBBreakpoint::operator->() const
586 return m_opaque_sp.get();
589 lldb_private::Breakpoint *
590 SBBreakpoint::get() const
592 return m_opaque_sp.get();
596 SBBreakpoint::operator *()
601 const lldb::BreakpointSP &
602 SBBreakpoint::operator *() const
608 SBBreakpoint::EventIsBreakpointEvent (const lldb::SBEvent &event)
610 return Breakpoint::BreakpointEventData::GetEventDataFromEvent(event.get()) != NULL;
615 SBBreakpoint::GetBreakpointEventTypeFromEvent (const SBEvent& event)
618 return Breakpoint::BreakpointEventData::GetBreakpointEventTypeFromEvent (event.GetSP());
619 return eBreakpointEventTypeInvalidType;
623 SBBreakpoint::GetBreakpointFromEvent (const lldb::SBEvent& event)
625 SBBreakpoint sb_breakpoint;
627 sb_breakpoint.m_opaque_sp = Breakpoint::BreakpointEventData::GetBreakpointFromEvent (event.GetSP());
628 return sb_breakpoint;
632 SBBreakpoint::GetBreakpointLocationAtIndexFromEvent (const lldb::SBEvent& event, uint32_t loc_idx)
634 SBBreakpointLocation sb_breakpoint_loc;
636 sb_breakpoint_loc.SetLocation (Breakpoint::BreakpointEventData::GetBreakpointLocationAtIndexFromEvent (event.GetSP(), loc_idx));
637 return sb_breakpoint_loc;
641 SBBreakpoint::GetNumBreakpointLocationsFromEvent (const lldb::SBEvent &event)
643 uint32_t num_locations = 0;
645 num_locations = (Breakpoint::BreakpointEventData::GetNumBreakpointLocationsFromEvent (event.GetSP()));
646 return num_locations;