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