1 //===-- SBBreakpointLocation.cpp --------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "lldb/API/SBBreakpointLocation.h"
11 #include "lldb/API/SBDefines.h"
12 #include "lldb/API/SBDebugger.h"
13 #include "lldb/API/SBStream.h"
14 
15 #include "lldb/lldb-types.h"
16 #include "lldb/lldb-defines.h"
17 #include "lldb/Breakpoint/Breakpoint.h"
18 #include "lldb/Breakpoint/BreakpointLocation.h"
19 #include "lldb/Target/ThreadSpec.h"
20 #include "lldb/Core/Log.h"
21 #include "lldb/Core/Stream.h"
22 #include "lldb/Core/StreamFile.h"
23 #include "lldb/Target/Target.h"
24 #include "lldb/Target/ThreadSpec.h"
25 
26 using namespace lldb;
27 using namespace lldb_private;
28 
29 
30 SBBreakpointLocation::SBBreakpointLocation () :
31     m_opaque_sp ()
32 {
33 }
34 
35 SBBreakpointLocation::SBBreakpointLocation (const lldb::BreakpointLocationSP &break_loc_sp) :
36     m_opaque_sp (break_loc_sp)
37 {
38     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
39 
40     if (log)
41     {
42         SBStream sstr;
43         GetDescription (lldb::eDescriptionLevelBrief, sstr);
44         log->Printf ("SBBreakpointLocation::SBBreakpointLocaiton (const lldb::BreakpointLocationsSP &break_loc_sp"
45                      "=%p)  => this.sp = %p (%s)", break_loc_sp.get(), m_opaque_sp.get(), sstr.GetData());
46     }
47 }
48 
49 SBBreakpointLocation::SBBreakpointLocation(const SBBreakpointLocation &rhs) :
50     m_opaque_sp (rhs.m_opaque_sp)
51 {
52 }
53 
54 const SBBreakpointLocation &
55 SBBreakpointLocation::operator = (const SBBreakpointLocation &rhs)
56 {
57     if (this != &rhs)
58         m_opaque_sp = rhs.m_opaque_sp;
59     return *this;
60 }
61 
62 
63 SBBreakpointLocation::~SBBreakpointLocation ()
64 {
65 }
66 
67 bool
68 SBBreakpointLocation::IsValid() const
69 {
70     return m_opaque_sp.get() != NULL;
71 }
72 
73 addr_t
74 SBBreakpointLocation::GetLoadAddress ()
75 {
76     addr_t ret_addr = LLDB_INVALID_ADDRESS;
77 
78     if (m_opaque_sp)
79     {
80         Mutex::Locker api_locker (m_opaque_sp->GetBreakpoint().GetTarget().GetAPIMutex());
81         ret_addr = m_opaque_sp->GetLoadAddress();
82     }
83 
84     return ret_addr;
85 }
86 
87 void
88 SBBreakpointLocation::SetEnabled (bool enabled)
89 {
90     if (m_opaque_sp)
91     {
92         Mutex::Locker api_locker (m_opaque_sp->GetBreakpoint().GetTarget().GetAPIMutex());
93         m_opaque_sp->SetEnabled (enabled);
94     }
95 }
96 
97 bool
98 SBBreakpointLocation::IsEnabled ()
99 {
100     if (m_opaque_sp)
101     {
102         Mutex::Locker api_locker (m_opaque_sp->GetBreakpoint().GetTarget().GetAPIMutex());
103         return m_opaque_sp->IsEnabled();
104     }
105     else
106         return false;
107 }
108 
109 uint32_t
110 SBBreakpointLocation::GetIgnoreCount ()
111 {
112     if (m_opaque_sp)
113     {
114         Mutex::Locker api_locker (m_opaque_sp->GetBreakpoint().GetTarget().GetAPIMutex());
115         return m_opaque_sp->GetIgnoreCount();
116     }
117     else
118         return 0;
119 }
120 
121 void
122 SBBreakpointLocation::SetIgnoreCount (uint32_t n)
123 {
124     if (m_opaque_sp)
125     {
126         Mutex::Locker api_locker (m_opaque_sp->GetBreakpoint().GetTarget().GetAPIMutex());
127         m_opaque_sp->SetIgnoreCount (n);
128     }
129 }
130 
131 void
132 SBBreakpointLocation::SetCondition (const char *condition)
133 {
134     if (m_opaque_sp)
135     {
136         Mutex::Locker api_locker (m_opaque_sp->GetBreakpoint().GetTarget().GetAPIMutex());
137         m_opaque_sp->SetCondition (condition);
138     }
139 }
140 
141 const char *
142 SBBreakpointLocation::GetCondition ()
143 {
144     if (m_opaque_sp)
145     {
146         Mutex::Locker api_locker (m_opaque_sp->GetBreakpoint().GetTarget().GetAPIMutex());
147         return m_opaque_sp->GetConditionText ();
148     }
149     return NULL;
150 }
151 
152 void
153 SBBreakpointLocation::SetThreadID (tid_t thread_id)
154 {
155     if (m_opaque_sp)
156     {
157         Mutex::Locker api_locker (m_opaque_sp->GetBreakpoint().GetTarget().GetAPIMutex());
158         m_opaque_sp->SetThreadID (thread_id);
159     }
160 }
161 
162 tid_t
163 SBBreakpointLocation::GetThreadID ()
164 {
165     tid_t tid = LLDB_INVALID_THREAD_ID;
166     if (m_opaque_sp)
167     {
168         Mutex::Locker api_locker (m_opaque_sp->GetBreakpoint().GetTarget().GetAPIMutex());
169         const ThreadSpec *thread_spec = m_opaque_sp->GetLocationOptions()->GetThreadSpecNoCreate();
170         if (thread_spec)
171             tid = thread_spec->GetTID();
172     }
173     return tid;
174 }
175 
176 void
177 SBBreakpointLocation::SetThreadIndex (uint32_t index)
178 {
179     if (m_opaque_sp)
180     {
181         Mutex::Locker api_locker (m_opaque_sp->GetBreakpoint().GetTarget().GetAPIMutex());
182         m_opaque_sp->GetLocationOptions()->GetThreadSpec()->SetIndex (index);
183     }
184 }
185 
186 uint32_t
187 SBBreakpointLocation::GetThreadIndex() const
188 {
189     uint32_t thread_idx = UINT32_MAX;
190     if (m_opaque_sp)
191     {
192         Mutex::Locker api_locker (m_opaque_sp->GetBreakpoint().GetTarget().GetAPIMutex());
193         const ThreadSpec *thread_spec = m_opaque_sp->GetOptionsNoCreate()->GetThreadSpecNoCreate();
194         if (thread_spec)
195             thread_idx = thread_spec->GetIndex();
196     }
197     return thread_idx;
198 }
199 
200 
201 void
202 SBBreakpointLocation::SetThreadName (const char *thread_name)
203 {
204     if (m_opaque_sp)
205     {
206         Mutex::Locker api_locker (m_opaque_sp->GetBreakpoint().GetTarget().GetAPIMutex());
207         m_opaque_sp->GetLocationOptions()->GetThreadSpec()->SetName (thread_name);
208     }
209 }
210 
211 const char *
212 SBBreakpointLocation::GetThreadName () const
213 {
214     if (m_opaque_sp)
215     {
216         Mutex::Locker api_locker (m_opaque_sp->GetBreakpoint().GetTarget().GetAPIMutex());
217         const ThreadSpec *thread_spec = m_opaque_sp->GetOptionsNoCreate()->GetThreadSpecNoCreate();
218         if (thread_spec)
219             return thread_spec->GetName();
220     }
221     return NULL;
222 }
223 
224 void
225 SBBreakpointLocation::SetQueueName (const char *queue_name)
226 {
227     if (m_opaque_sp)
228     {
229         Mutex::Locker api_locker (m_opaque_sp->GetBreakpoint().GetTarget().GetAPIMutex());
230         m_opaque_sp->GetLocationOptions()->GetThreadSpec()->SetQueueName (queue_name);
231     }
232 }
233 
234 const char *
235 SBBreakpointLocation::GetQueueName () const
236 {
237     if (m_opaque_sp)
238     {
239         Mutex::Locker api_locker (m_opaque_sp->GetBreakpoint().GetTarget().GetAPIMutex());
240         const ThreadSpec *thread_spec = m_opaque_sp->GetOptionsNoCreate()->GetThreadSpecNoCreate();
241         if (thread_spec)
242             return thread_spec->GetQueueName();
243     }
244     return NULL;
245 }
246 
247 bool
248 SBBreakpointLocation::IsResolved ()
249 {
250     if (m_opaque_sp)
251     {
252         Mutex::Locker api_locker (m_opaque_sp->GetBreakpoint().GetTarget().GetAPIMutex());
253         return m_opaque_sp->IsResolved();
254     }
255     return false;
256 }
257 
258 void
259 SBBreakpointLocation::SetLocation (const lldb::BreakpointLocationSP &break_loc_sp)
260 {
261     // Uninstall the callbacks?
262     m_opaque_sp = break_loc_sp;
263 }
264 
265 bool
266 SBBreakpointLocation::GetDescription (DescriptionLevel level, SBStream &description)
267 {
268     if (m_opaque_sp)
269     {
270         Mutex::Locker api_locker (m_opaque_sp->GetBreakpoint().GetTarget().GetAPIMutex());
271         description.ref();
272         m_opaque_sp->GetDescription (description.get(), level);
273         description.get()->EOL();
274     }
275     else
276         description.Printf ("No value");
277 
278     return true;
279 }
280 
281 SBBreakpoint
282 SBBreakpointLocation::GetBreakpoint ()
283 {
284     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
285 
286     //if (log)
287     //    log->Printf ("SBBreakpointLocation::GetBreakpoint ()");
288 
289     SBBreakpoint sb_bp;
290     if (m_opaque_sp)
291     {
292         Mutex::Locker api_locker (m_opaque_sp->GetBreakpoint().GetTarget().GetAPIMutex());
293         *sb_bp = m_opaque_sp->GetBreakpoint ().GetSP();
294     }
295 
296     if (log)
297     {
298         SBStream sstr;
299         sb_bp.GetDescription (sstr);
300         log->Printf ("SBBreakpointLocation(%p)::GetBreakpoint () => SBBreakpoint(%p) %s",
301                      m_opaque_sp.get(), sb_bp.get(), sstr.GetData());
302     }
303     return sb_bp;
304 }
305 
306