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/Target.h"
27 #include "lldb/Target/Thread.h"
28 #include "lldb/Target/ThreadSpec.h"
31 #include "lldb/lldb-enumerations.h"
34 using namespace lldb_private;
38 SBBreakpoint::BreakpointHitCallback callback;
42 class SBBreakpointCallbackBaton : public Baton
46 SBBreakpointCallbackBaton (SBBreakpoint::BreakpointHitCallback callback, void *baton) :
47 Baton (new CallbackData)
49 CallbackData *data = (CallbackData *)m_data;
50 data->callback = callback;
51 data->callback_baton = baton;
54 virtual ~SBBreakpointCallbackBaton()
56 CallbackData *data = (CallbackData *)m_data;
67 SBBreakpoint::SBBreakpoint () :
72 SBBreakpoint::SBBreakpoint (const SBBreakpoint& rhs) :
73 m_opaque_sp (rhs.m_opaque_sp)
78 SBBreakpoint::SBBreakpoint (const lldb::BreakpointSP &bp_sp) :
83 SBBreakpoint::~SBBreakpoint()
88 SBBreakpoint::operator = (const SBBreakpoint& rhs)
91 m_opaque_sp = rhs.m_opaque_sp;
96 SBBreakpoint::operator == (const lldb::SBBreakpoint& rhs)
98 if (m_opaque_sp && rhs.m_opaque_sp)
99 return m_opaque_sp.get() == rhs.m_opaque_sp.get();
104 SBBreakpoint::operator != (const lldb::SBBreakpoint& rhs)
106 if (m_opaque_sp && rhs.m_opaque_sp)
107 return m_opaque_sp.get() != rhs.m_opaque_sp.get();
108 return (m_opaque_sp && !rhs.m_opaque_sp) || (rhs.m_opaque_sp && !m_opaque_sp);
112 SBBreakpoint::GetID () const
114 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
116 break_id_t break_id = LLDB_INVALID_BREAK_ID;
118 break_id = m_opaque_sp->GetID();
122 if (break_id == LLDB_INVALID_BREAK_ID)
123 log->Printf ("SBBreakpoint(%p)::GetID () => LLDB_INVALID_BREAK_ID", m_opaque_sp.get());
125 log->Printf ("SBBreakpoint(%p)::GetID () => %u", m_opaque_sp.get(), break_id);
133 SBBreakpoint::IsValid() const
135 return (bool) m_opaque_sp;
139 SBBreakpoint::ClearAllBreakpointSites ()
143 Mutex::Locker api_locker (m_opaque_sp->GetTarget().GetAPIMutex());
144 m_opaque_sp->ClearAllBreakpointSites ();
149 SBBreakpoint::FindLocationByAddress (addr_t vm_addr)
151 SBBreakpointLocation sb_bp_location;
155 if (vm_addr != LLDB_INVALID_ADDRESS)
157 Mutex::Locker api_locker (m_opaque_sp->GetTarget().GetAPIMutex());
159 Target &target = m_opaque_sp->GetTarget();
160 if (target.GetSectionLoadList().ResolveLoadAddress (vm_addr, address) == false)
162 address.SetRawAddress (vm_addr);
164 sb_bp_location.SetLocation (m_opaque_sp->FindLocationByAddress (address));
167 return sb_bp_location;
171 SBBreakpoint::FindLocationIDByAddress (addr_t vm_addr)
173 break_id_t break_id = LLDB_INVALID_BREAK_ID;
175 if (m_opaque_sp && vm_addr != LLDB_INVALID_ADDRESS)
177 Mutex::Locker api_locker (m_opaque_sp->GetTarget().GetAPIMutex());
179 Target &target = m_opaque_sp->GetTarget();
180 if (target.GetSectionLoadList().ResolveLoadAddress (vm_addr, address) == false)
182 address.SetRawAddress (vm_addr);
184 break_id = m_opaque_sp->FindLocationIDByAddress (address);
191 SBBreakpoint::FindLocationByID (break_id_t bp_loc_id)
193 SBBreakpointLocation sb_bp_location;
197 Mutex::Locker api_locker (m_opaque_sp->GetTarget().GetAPIMutex());
198 sb_bp_location.SetLocation (m_opaque_sp->FindLocationByID (bp_loc_id));
201 return sb_bp_location;
205 SBBreakpoint::GetLocationAtIndex (uint32_t index)
207 SBBreakpointLocation sb_bp_location;
211 Mutex::Locker api_locker (m_opaque_sp->GetTarget().GetAPIMutex());
212 sb_bp_location.SetLocation (m_opaque_sp->GetLocationAtIndex (index));
215 return sb_bp_location;
219 SBBreakpoint::SetEnabled (bool enable)
221 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
224 log->Printf ("SBBreakpoint(%p)::SetEnabled (enabled=%i)", m_opaque_sp.get(), enable);
228 Mutex::Locker api_locker (m_opaque_sp->GetTarget().GetAPIMutex());
229 m_opaque_sp->SetEnabled (enable);
234 SBBreakpoint::IsEnabled ()
238 Mutex::Locker api_locker (m_opaque_sp->GetTarget().GetAPIMutex());
239 return m_opaque_sp->IsEnabled();
246 SBBreakpoint::SetOneShot (bool one_shot)
248 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
251 log->Printf ("SBBreakpoint(%p)::SetOneShot (one_shot=%i)", m_opaque_sp.get(), one_shot);
255 Mutex::Locker api_locker (m_opaque_sp->GetTarget().GetAPIMutex());
256 m_opaque_sp->SetOneShot (one_shot);
261 SBBreakpoint::IsOneShot () const
265 Mutex::Locker api_locker (m_opaque_sp->GetTarget().GetAPIMutex());
266 return m_opaque_sp->IsOneShot();
273 SBBreakpoint::IsInternal ()
277 Mutex::Locker api_locker (m_opaque_sp->GetTarget().GetAPIMutex());
278 return m_opaque_sp->IsInternal();
285 SBBreakpoint::SetIgnoreCount (uint32_t count)
287 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
290 log->Printf ("SBBreakpoint(%p)::SetIgnoreCount (count=%u)", m_opaque_sp.get(), count);
294 Mutex::Locker api_locker (m_opaque_sp->GetTarget().GetAPIMutex());
295 m_opaque_sp->SetIgnoreCount (count);
300 SBBreakpoint::SetCondition (const char *condition)
304 Mutex::Locker api_locker (m_opaque_sp->GetTarget().GetAPIMutex());
305 m_opaque_sp->SetCondition (condition);
310 SBBreakpoint::GetCondition ()
314 Mutex::Locker api_locker (m_opaque_sp->GetTarget().GetAPIMutex());
315 return m_opaque_sp->GetConditionText ();
321 SBBreakpoint::GetHitCount () const
326 Mutex::Locker api_locker (m_opaque_sp->GetTarget().GetAPIMutex());
327 count = m_opaque_sp->GetHitCount();
330 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
332 log->Printf ("SBBreakpoint(%p)::GetHitCount () => %u", m_opaque_sp.get(), count);
338 SBBreakpoint::GetIgnoreCount () const
343 Mutex::Locker api_locker (m_opaque_sp->GetTarget().GetAPIMutex());
344 count = m_opaque_sp->GetIgnoreCount();
347 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
349 log->Printf ("SBBreakpoint(%p)::GetIgnoreCount () => %u", m_opaque_sp.get(), count);
355 SBBreakpoint::SetThreadID (tid_t tid)
359 Mutex::Locker api_locker (m_opaque_sp->GetTarget().GetAPIMutex());
360 m_opaque_sp->SetThreadID (tid);
362 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
364 log->Printf ("SBBreakpoint(%p)::SetThreadID (tid=0x%4.4" PRIx64 ")", m_opaque_sp.get(), tid);
369 SBBreakpoint::GetThreadID ()
371 tid_t tid = LLDB_INVALID_THREAD_ID;
374 Mutex::Locker api_locker (m_opaque_sp->GetTarget().GetAPIMutex());
375 tid = m_opaque_sp->GetThreadID();
378 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
380 log->Printf ("SBBreakpoint(%p)::GetThreadID () => 0x%4.4" PRIx64, m_opaque_sp.get(), tid);
385 SBBreakpoint::SetThreadIndex (uint32_t index)
387 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
389 log->Printf ("SBBreakpoint(%p)::SetThreadIndex (%u)", m_opaque_sp.get(), index);
392 Mutex::Locker api_locker (m_opaque_sp->GetTarget().GetAPIMutex());
393 m_opaque_sp->GetOptions()->GetThreadSpec()->SetIndex (index);
398 SBBreakpoint::GetThreadIndex() const
400 uint32_t thread_idx = UINT32_MAX;
403 Mutex::Locker api_locker (m_opaque_sp->GetTarget().GetAPIMutex());
404 const ThreadSpec *thread_spec = m_opaque_sp->GetOptions()->GetThreadSpecNoCreate();
405 if (thread_spec != NULL)
406 thread_idx = thread_spec->GetIndex();
408 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
410 log->Printf ("SBBreakpoint(%p)::GetThreadIndex () => %u", m_opaque_sp.get(), thread_idx);
417 SBBreakpoint::SetThreadName (const char *thread_name)
419 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
421 log->Printf ("SBBreakpoint(%p)::SetThreadName (%s)", m_opaque_sp.get(), thread_name);
425 Mutex::Locker api_locker (m_opaque_sp->GetTarget().GetAPIMutex());
426 m_opaque_sp->GetOptions()->GetThreadSpec()->SetName (thread_name);
431 SBBreakpoint::GetThreadName () const
433 const char *name = NULL;
436 Mutex::Locker api_locker (m_opaque_sp->GetTarget().GetAPIMutex());
437 const ThreadSpec *thread_spec = m_opaque_sp->GetOptions()->GetThreadSpecNoCreate();
438 if (thread_spec != NULL)
439 name = thread_spec->GetName();
441 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
443 log->Printf ("SBBreakpoint(%p)::GetThreadName () => %s", m_opaque_sp.get(), name);
449 SBBreakpoint::SetQueueName (const char *queue_name)
451 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
453 log->Printf ("SBBreakpoint(%p)::SetQueueName (%s)", m_opaque_sp.get(), queue_name);
456 Mutex::Locker api_locker (m_opaque_sp->GetTarget().GetAPIMutex());
457 m_opaque_sp->GetOptions()->GetThreadSpec()->SetQueueName (queue_name);
462 SBBreakpoint::GetQueueName () const
464 const char *name = NULL;
467 Mutex::Locker api_locker (m_opaque_sp->GetTarget().GetAPIMutex());
468 const ThreadSpec *thread_spec = m_opaque_sp->GetOptions()->GetThreadSpecNoCreate();
470 name = thread_spec->GetQueueName();
472 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
474 log->Printf ("SBBreakpoint(%p)::GetQueueName () => %s", m_opaque_sp.get(), name);
480 SBBreakpoint::GetNumResolvedLocations() const
482 size_t num_resolved = 0;
485 Mutex::Locker api_locker (m_opaque_sp->GetTarget().GetAPIMutex());
486 num_resolved = m_opaque_sp->GetNumResolvedLocations();
488 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
490 log->Printf ("SBBreakpoint(%p)::GetNumResolvedLocations () => %" PRIu64, m_opaque_sp.get(), (uint64_t)num_resolved);
495 SBBreakpoint::GetNumLocations() const
500 Mutex::Locker api_locker (m_opaque_sp->GetTarget().GetAPIMutex());
501 num_locs = m_opaque_sp->GetNumLocations();
503 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
505 log->Printf ("SBBreakpoint(%p)::GetNumLocations () => %" PRIu64, m_opaque_sp.get(), (uint64_t)num_locs);
510 SBBreakpoint::GetDescription (SBStream &s)
514 Mutex::Locker api_locker (m_opaque_sp->GetTarget().GetAPIMutex());
515 s.Printf("SBBreakpoint: id = %i, ", m_opaque_sp->GetID());
516 m_opaque_sp->GetResolverDescription (s.get());
517 m_opaque_sp->GetFilterDescription (s.get());
518 const size_t num_locations = m_opaque_sp->GetNumLocations ();
519 s.Printf(", locations = %" PRIu64, (uint64_t)num_locations);
522 s.Printf ("No value");
527 SBBreakpoint::PrivateBreakpointHitCallback
530 StoppointCallbackContext *ctx,
531 lldb::user_id_t break_id,
532 lldb::user_id_t break_loc_id
535 ExecutionContext exe_ctx (ctx->exe_ctx_ref);
536 BreakpointSP bp_sp(exe_ctx.GetTargetRef().GetBreakpointList().FindBreakpointByID(break_id));
539 CallbackData *data = (CallbackData *)baton;
540 lldb_private::Breakpoint *bp = bp_sp.get();
541 if (bp && data->callback)
543 Process *process = exe_ctx.GetProcessPtr();
546 SBProcess sb_process (process->shared_from_this());
548 SBBreakpointLocation sb_location;
550 sb_location.SetLocation (bp_sp->FindLocationByID (break_loc_id));
551 Thread *thread = exe_ctx.GetThreadPtr();
553 sb_thread.SetThread(thread->shared_from_this());
555 return data->callback (data->callback_baton,
562 return true; // Return true if we should stop at this breakpoint
566 SBBreakpoint::SetCallback (BreakpointHitCallback callback, void *baton)
568 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
571 log->Printf ("SBBreakpoint(%p)::SetCallback (callback=%p, baton=%p)", m_opaque_sp.get(), callback, baton);
575 Mutex::Locker api_locker (m_opaque_sp->GetTarget().GetAPIMutex());
576 BatonSP baton_sp(new SBBreakpointCallbackBaton (callback, baton));
577 m_opaque_sp->SetCallback (SBBreakpoint::PrivateBreakpointHitCallback, baton_sp, false);
582 lldb_private::Breakpoint *
583 SBBreakpoint::operator->() const
585 return m_opaque_sp.get();
588 lldb_private::Breakpoint *
589 SBBreakpoint::get() const
591 return m_opaque_sp.get();
595 SBBreakpoint::operator *()
600 const lldb::BreakpointSP &
601 SBBreakpoint::operator *() const
607 SBBreakpoint::EventIsBreakpointEvent (const lldb::SBEvent &event)
609 return Breakpoint::BreakpointEventData::GetEventDataFromEvent(event.get()) != NULL;
614 SBBreakpoint::GetBreakpointEventTypeFromEvent (const SBEvent& event)
617 return Breakpoint::BreakpointEventData::GetBreakpointEventTypeFromEvent (event.GetSP());
618 return eBreakpointEventTypeInvalidType;
622 SBBreakpoint::GetBreakpointFromEvent (const lldb::SBEvent& event)
624 SBBreakpoint sb_breakpoint;
626 sb_breakpoint.m_opaque_sp = Breakpoint::BreakpointEventData::GetBreakpointFromEvent (event.GetSP());
627 return sb_breakpoint;
631 SBBreakpoint::GetBreakpointLocationAtIndexFromEvent (const lldb::SBEvent& event, uint32_t loc_idx)
633 SBBreakpointLocation sb_breakpoint_loc;
635 sb_breakpoint_loc.SetLocation (Breakpoint::BreakpointEventData::GetBreakpointLocationAtIndexFromEvent (event.GetSP(), loc_idx));
636 return sb_breakpoint_loc;
640 SBBreakpoint::GetNumBreakpointLocationsFromEvent (const lldb::SBEvent &event)
642 uint32_t num_locations = 0;
644 num_locations = (Breakpoint::BreakpointEventData::GetNumBreakpointLocationsFromEvent (event.GetSP()));
645 return num_locations;