blob: ef9c43152b415308475a078a2b72efde5ad829ad [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +00001//===-- BreakpointLocation.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// C Includes
11// C++ Includes
12#include <string>
13
14// Other libraries and framework includes
15// Project includes
16#include "lldb/Breakpoint/BreakpointLocation.h"
17#include "lldb/Breakpoint/BreakpointID.h"
18#include "lldb/Breakpoint/StoppointCallbackContext.h"
19#include "lldb/Core/Log.h"
20#include "lldb/Target/Target.h"
21#include "lldb/Target/Process.h"
22#include "lldb/Core/StreamString.h"
23#include "lldb/lldb-private-log.h"
24#include "lldb/Target/Thread.h"
Jim Ingham3c7b5b92010-06-16 02:00:15 +000025#include "lldb/Target/ThreadSpec.h"
Chris Lattner24943d22010-06-08 16:52:24 +000026
27using namespace lldb;
28using namespace lldb_private;
29
30BreakpointLocation::BreakpointLocation
31(
32 break_id_t loc_id,
33 Breakpoint &owner,
34 Address &addr,
35 lldb::tid_t tid,
36 bool hardware
37) :
Jim Ingham3c7b5b92010-06-16 02:00:15 +000038 StoppointLocation (loc_id, addr.GetLoadAddress(owner.GetTarget().GetProcessSP().get()), hardware),
Chris Lattner24943d22010-06-08 16:52:24 +000039 m_address (addr),
40 m_owner (owner),
41 m_options_ap (),
42 m_bp_site_sp ()
43{
Jim Ingham3c7b5b92010-06-16 02:00:15 +000044 SetThreadID (tid);
Chris Lattner24943d22010-06-08 16:52:24 +000045}
46
47BreakpointLocation::~BreakpointLocation()
48{
49 ClearBreakpointSite();
50}
51
52lldb::addr_t
Greg Clayton273a8e52010-06-14 04:18:27 +000053BreakpointLocation::GetLoadAddress () const
Chris Lattner24943d22010-06-08 16:52:24 +000054{
55 return m_address.GetLoadAddress(m_owner.GetTarget().GetProcessSP().get());
56}
57
58Address &
59BreakpointLocation::GetAddress ()
60{
61 return m_address;
62}
63
64Breakpoint &
65BreakpointLocation::GetBreakpoint ()
66{
67 return m_owner;
68}
69
70bool
71BreakpointLocation::IsEnabled ()
72{
73 if (!m_owner.IsEnabled())
74 return false;
75 else if (m_options_ap.get() != NULL)
76 return m_options_ap->IsEnabled();
77 else
78 return true;
79}
80
81void
82BreakpointLocation::SetEnabled (bool enabled)
83{
84 GetLocationOptions()->SetEnabled(enabled);
85 if (enabled)
86 {
87 ResolveBreakpointSite();
88 }
89 else
90 {
91 ClearBreakpointSite();
92 }
93}
94
95void
96BreakpointLocation::SetThreadID (lldb::tid_t thread_id)
97{
Jim Ingham3c7b5b92010-06-16 02:00:15 +000098 if (thread_id != LLDB_INVALID_THREAD_ID)
99 GetLocationOptions()->SetThreadID(thread_id);
100 else
101 {
102 // If we're resetting this to an invalid thread id, then
103 // don't make an options pointer just to do that.
104 if (m_options_ap.get() != NULL)
105 m_options_ap->SetThreadID (thread_id);
106 }
Chris Lattner24943d22010-06-08 16:52:24 +0000107}
108
109bool
110BreakpointLocation::InvokeCallback (StoppointCallbackContext *context)
111{
Jim Ingham649492b2010-06-18 01:00:58 +0000112 if (m_options_ap.get() != NULL && m_options_ap->HasCallback())
Chris Lattner24943d22010-06-08 16:52:24 +0000113 return m_options_ap->InvokeCallback (context, m_owner.GetID(), GetID());
Jim Ingham649492b2010-06-18 01:00:58 +0000114 else
115 return m_owner.InvokeCallback (context, GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000116}
117
118void
119BreakpointLocation::SetCallback (BreakpointHitCallback callback, void *baton,
120 bool is_synchronous)
121{
122 // The default "Baton" class will keep a copy of "baton" and won't free
123 // or delete it when it goes goes out of scope.
124 GetLocationOptions()->SetCallback(callback, BatonSP (new Baton(baton)), is_synchronous);
125}
126
127void
128BreakpointLocation::SetCallback (BreakpointHitCallback callback, const BatonSP &baton_sp,
129 bool is_synchronous)
130{
131 GetLocationOptions()->SetCallback (callback, baton_sp, is_synchronous);
132}
133
134void
135BreakpointLocation::ClearCallback ()
136{
137 GetLocationOptions()->ClearCallback();
138}
139
140int32_t
141BreakpointLocation::GetIgnoreCount ()
142{
143 return GetOptionsNoCopy()->GetIgnoreCount();
144}
145
146void
147BreakpointLocation::SetIgnoreCount (int32_t n)
148{
149 GetLocationOptions()->SetIgnoreCount(n);
150}
151
Jim Ingham3c7b5b92010-06-16 02:00:15 +0000152const BreakpointOptions *
153BreakpointLocation::GetOptionsNoCopy () const
Chris Lattner24943d22010-06-08 16:52:24 +0000154{
155 if (m_options_ap.get() != NULL)
156 return m_options_ap.get();
157 else
158 return m_owner.GetOptions ();
159}
160
161BreakpointOptions *
162BreakpointLocation::GetLocationOptions ()
163{
Jim Ingham649492b2010-06-18 01:00:58 +0000164 // If we make the copy we don't copy the callbacks because that is potentially
165 // expensive and we don't want to do that for the simple case where someone is
166 // just disabling the location.
Chris Lattner24943d22010-06-08 16:52:24 +0000167 if (m_options_ap.get() == NULL)
Jim Ingham649492b2010-06-18 01:00:58 +0000168 m_options_ap.reset(BreakpointOptions::CopyOptionsNoCallback(*m_owner.GetOptions ()));
169
Chris Lattner24943d22010-06-08 16:52:24 +0000170 return m_options_ap.get();
171}
172
Jim Ingham3c7b5b92010-06-16 02:00:15 +0000173bool
174BreakpointLocation::ValidForThisThread (Thread *thread)
175{
176 return thread->MatchesSpec(GetOptionsNoCopy()->GetThreadSpec());
177}
178
Chris Lattner24943d22010-06-08 16:52:24 +0000179// RETURNS - true if we should stop at this breakpoint, false if we
Jim Ingham3c7b5b92010-06-16 02:00:15 +0000180// should continue. Note, we don't check the thread spec for the breakpoint
181// here, since if the breakpoint is not for this thread, then the event won't
182// even get reported, so the check is redundant.
Chris Lattner24943d22010-06-08 16:52:24 +0000183
184bool
185BreakpointLocation::ShouldStop (StoppointCallbackContext *context)
186{
187 bool should_stop = true;
188
189 m_hit_count++;
190
191 if (!IsEnabled())
192 return false;
193
Chris Lattner24943d22010-06-08 16:52:24 +0000194 if (m_hit_count <= GetIgnoreCount())
195 return false;
196
197 // Tell if the callback is synchronous here.
198 context->is_synchronous = true;
199 should_stop = InvokeCallback (context);
200
201 if (should_stop)
202 {
203 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS);
204 if (log)
205 {
206 StreamString s;
207 GetDescription (&s, lldb::eDescriptionLevelVerbose);
208 log->Printf ("Hit breakpoint location: %s\n", s.GetData());
209 }
210 }
211 return should_stop;
212}
213
214bool
215BreakpointLocation::IsResolved () const
216{
217 return m_bp_site_sp.get() != NULL;
218}
219
220bool
221BreakpointLocation::ResolveBreakpointSite ()
222{
223 if (m_bp_site_sp)
224 return true;
225
226 Process* process = m_owner.GetTarget().GetProcessSP().get();
227 if (process == NULL)
228 return false;
229
230 BreakpointLocationSP myself_sp(m_owner.GetLocationSP (this));
231
232 lldb::user_id_t new_id = process->CreateBreakpointSite (myself_sp, false);
233
234 if (new_id == LLDB_INVALID_UID)
235 {
236 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS);
237 if (log)
238 log->Warning ("Tried to add breakpoint site at 0x%llx but it was already present.\n",
239 m_address.GetLoadAddress(process));
240 return false;
241 }
242
243 return true;
244}
245
246bool
247BreakpointLocation::SetBreakpointSite (BreakpointSiteSP& bp_site_sp)
248{
249 m_bp_site_sp = bp_site_sp;
250 return true;
251}
252
253bool
254BreakpointLocation::ClearBreakpointSite ()
255{
256 if (m_bp_site_sp.get())
257 {
Jim Ingham649492b2010-06-18 01:00:58 +0000258 m_owner.GetTarget().GetProcessSP()->RemoveOwnerFromBreakpointSite (GetBreakpoint().GetID(),
259 GetID(), m_bp_site_sp);
Chris Lattner24943d22010-06-08 16:52:24 +0000260 m_bp_site_sp.reset();
261 return true;
262 }
263 return false;
264}
265
266void
267BreakpointLocation::GetDescription (Stream *s, lldb::DescriptionLevel level)
268{
269 SymbolContext sc;
270 s->Indent();
271 BreakpointID::GetCanonicalReference(s, m_owner.GetID(), GetID());
272
273 if (level == lldb::eDescriptionLevelBrief)
274 return;
275
276 s->PutCString(": ");
277
278 if (level == lldb::eDescriptionLevelVerbose)
279 s->IndentMore();
280
281 if (m_address.IsSectionOffset())
282 {
283 m_address.CalculateSymbolContext(&sc);
284
285 if (level == lldb::eDescriptionLevelFull)
286 {
287 s->PutCString("where = ");
288 sc.DumpStopContext (s, m_owner.GetTarget().GetProcessSP().get(), m_address);
289 }
290 else
291 {
292 if (sc.module_sp)
293 {
294 s->EOL();
295 s->Indent("module = ");
296 sc.module_sp->GetFileSpec().Dump (s);
297 }
298
299 if (sc.comp_unit != NULL)
300 {
301 s->EOL();
302 s->Indent("compile unit = ");
303 dynamic_cast<FileSpec*>(sc.comp_unit)->GetFilename().Dump (s);
304
305 if (sc.function != NULL)
306 {
307 s->EOL();
308 s->Indent("function = ");
309 s->PutCString (sc.function->GetMangled().GetName().AsCString("<unknown>"));
310 }
311
312 if (sc.line_entry.line > 0)
313 {
314 s->EOL();
315 s->Indent("location = ");
316 sc.line_entry.DumpStopContext (s);
317 }
318
319 }
320 else
321 {
322 // If we don't have a comp unit, see if we have a symbol we can print.
323 if (sc.symbol)
324 {
325 s->EOL();
326 s->Indent("symbol = ");
327 s->PutCString(sc.symbol->GetMangled().GetName().AsCString("<unknown>"));
328 }
329 }
330 }
331 }
332
333 if (level == lldb::eDescriptionLevelVerbose)
334 {
335 s->EOL();
336 s->Indent();
337 }
338 s->Printf ("%saddress = ", (level == lldb::eDescriptionLevelFull && m_address.IsSectionOffset()) ? ", " : "");
339 ExecutionContextScope *exe_scope = NULL;
340 Target *target = &m_owner.GetTarget();
341 if (target)
342 exe_scope = target->GetProcessSP().get();
343 if (exe_scope == NULL)
344 exe_scope = target;
345
346 m_address.Dump(s, exe_scope, Address::DumpStyleLoadAddress, Address::DumpStyleModuleWithFileAddress);
347
348 if (level == lldb::eDescriptionLevelVerbose)
349 {
350 s->EOL();
351 s->Indent();
352 s->Printf("resolved = %s\n", IsResolved() ? "true" : "false");
353
354 s->Indent();
Chris Lattner24943d22010-06-08 16:52:24 +0000355 s->Printf ("hit count = %-4u\n", GetHitCount());
356
357 if (m_options_ap.get())
358 {
Jim Ingham649492b2010-06-18 01:00:58 +0000359 s->Indent();
360 m_options_ap->GetDescription (s, level);
361 s->EOL();
Chris Lattner24943d22010-06-08 16:52:24 +0000362 }
363 s->IndentLess();
364 }
365 else
366 {
Jim Ingham649492b2010-06-18 01:00:58 +0000367 s->Printf(", %sresolved, hit count = %u ",
Chris Lattner24943d22010-06-08 16:52:24 +0000368 (IsResolved() ? "" : "un"),
Chris Lattner24943d22010-06-08 16:52:24 +0000369 GetHitCount());
Jim Ingham649492b2010-06-18 01:00:58 +0000370 if (m_options_ap.get())
371 {
372 m_options_ap->GetDescription (s, level);
373 }
Chris Lattner24943d22010-06-08 16:52:24 +0000374 }
375}
376
377void
378BreakpointLocation::Dump(Stream *s) const
379{
380 if (s == NULL)
381 return;
382
Jim Ingham649492b2010-06-18 01:00:58 +0000383 s->Printf("BreakpointLocation %u: tid = %4.4x load addr = 0x%8.8llx state = %s type = %s breakpoint "
384 "hw_index = %i hit_count = %-4u ignore_count = %-4u",
Chris Lattner24943d22010-06-08 16:52:24 +0000385 GetID(),
Jim Ingham3c7b5b92010-06-16 02:00:15 +0000386 GetOptionsNoCopy()->GetThreadSpec()->GetTID(),
Chris Lattner24943d22010-06-08 16:52:24 +0000387 (uint64_t) m_address.GetLoadAddress(m_owner.GetTarget().GetProcessSP().get()),
388 (m_options_ap.get() ? m_options_ap->IsEnabled() : m_owner.IsEnabled()) ? "enabled " : "disabled",
389 IsHardware() ? "hardware" : "software",
390 GetHardwareIndex(),
391 GetHitCount(),
392 m_options_ap.get() ? m_options_ap->GetIgnoreCount() : m_owner.GetIgnoreCount());
393}