blob: 6693c469cc64ee84b44e401540121deaebb22127 [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"
Jim Inghame5ed8e92011-06-02 23:58:26 +000019#include "lldb/Core/Debugger.h"
Chris Lattner24943d22010-06-08 16:52:24 +000020#include "lldb/Core/Log.h"
21#include "lldb/Target/Target.h"
Jim Inghamd1686902010-10-14 23:45:03 +000022#include "lldb/Target/ThreadPlan.h"
Chris Lattner24943d22010-06-08 16:52:24 +000023#include "lldb/Target/Process.h"
24#include "lldb/Core/StreamString.h"
25#include "lldb/lldb-private-log.h"
26#include "lldb/Target/Thread.h"
Jim Ingham3c7b5b92010-06-16 02:00:15 +000027#include "lldb/Target/ThreadSpec.h"
Chris Lattner24943d22010-06-08 16:52:24 +000028
29using namespace lldb;
30using namespace lldb_private;
31
32BreakpointLocation::BreakpointLocation
33(
34 break_id_t loc_id,
35 Breakpoint &owner,
Greg Clayton19a1ab82011-02-05 00:38:04 +000036 const Address &addr,
Chris Lattner24943d22010-06-08 16:52:24 +000037 lldb::tid_t tid,
38 bool hardware
39) :
Greg Claytonc0fa5332011-05-22 22:46:53 +000040 StoppointLocation (loc_id, addr.GetOpcodeLoadAddress(&owner.GetTarget()), hardware),
Chris Lattner24943d22010-06-08 16:52:24 +000041 m_address (addr),
42 m_owner (owner),
43 m_options_ap (),
44 m_bp_site_sp ()
45{
Jim Ingham3c7b5b92010-06-16 02:00:15 +000046 SetThreadID (tid);
Chris Lattner24943d22010-06-08 16:52:24 +000047}
48
49BreakpointLocation::~BreakpointLocation()
50{
51 ClearBreakpointSite();
52}
53
54lldb::addr_t
Greg Clayton273a8e52010-06-14 04:18:27 +000055BreakpointLocation::GetLoadAddress () const
Chris Lattner24943d22010-06-08 16:52:24 +000056{
Greg Claytonc0fa5332011-05-22 22:46:53 +000057 return m_address.GetOpcodeLoadAddress (&m_owner.GetTarget());
Chris Lattner24943d22010-06-08 16:52:24 +000058}
59
60Address &
61BreakpointLocation::GetAddress ()
62{
63 return m_address;
64}
65
66Breakpoint &
67BreakpointLocation::GetBreakpoint ()
68{
69 return m_owner;
70}
71
72bool
73BreakpointLocation::IsEnabled ()
74{
75 if (!m_owner.IsEnabled())
76 return false;
77 else if (m_options_ap.get() != NULL)
78 return m_options_ap->IsEnabled();
79 else
80 return true;
81}
82
83void
84BreakpointLocation::SetEnabled (bool enabled)
85{
86 GetLocationOptions()->SetEnabled(enabled);
87 if (enabled)
88 {
89 ResolveBreakpointSite();
90 }
91 else
92 {
93 ClearBreakpointSite();
94 }
95}
96
97void
98BreakpointLocation::SetThreadID (lldb::tid_t thread_id)
99{
Jim Ingham3c7b5b92010-06-16 02:00:15 +0000100 if (thread_id != LLDB_INVALID_THREAD_ID)
101 GetLocationOptions()->SetThreadID(thread_id);
102 else
103 {
104 // If we're resetting this to an invalid thread id, then
105 // don't make an options pointer just to do that.
106 if (m_options_ap.get() != NULL)
107 m_options_ap->SetThreadID (thread_id);
108 }
Chris Lattner24943d22010-06-08 16:52:24 +0000109}
110
111bool
112BreakpointLocation::InvokeCallback (StoppointCallbackContext *context)
113{
Jim Ingham649492b2010-06-18 01:00:58 +0000114 if (m_options_ap.get() != NULL && m_options_ap->HasCallback())
Chris Lattner24943d22010-06-08 16:52:24 +0000115 return m_options_ap->InvokeCallback (context, m_owner.GetID(), GetID());
Jim Ingham649492b2010-06-18 01:00:58 +0000116 else
117 return m_owner.InvokeCallback (context, GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000118}
119
120void
121BreakpointLocation::SetCallback (BreakpointHitCallback callback, void *baton,
122 bool is_synchronous)
123{
124 // The default "Baton" class will keep a copy of "baton" and won't free
125 // or delete it when it goes goes out of scope.
126 GetLocationOptions()->SetCallback(callback, BatonSP (new Baton(baton)), is_synchronous);
127}
128
129void
130BreakpointLocation::SetCallback (BreakpointHitCallback callback, const BatonSP &baton_sp,
131 bool is_synchronous)
132{
133 GetLocationOptions()->SetCallback (callback, baton_sp, is_synchronous);
134}
135
Jim Inghamd1686902010-10-14 23:45:03 +0000136
Chris Lattner24943d22010-06-08 16:52:24 +0000137void
138BreakpointLocation::ClearCallback ()
139{
140 GetLocationOptions()->ClearCallback();
141}
142
Jim Inghamd1686902010-10-14 23:45:03 +0000143void
144BreakpointLocation::SetCondition (const char *condition)
145{
146 GetLocationOptions()->SetCondition (condition);
147}
148
149ThreadPlan *
150BreakpointLocation::GetThreadPlanToTestCondition (ExecutionContext &exe_ctx, Stream &error)
151{
Greg Clayton987c7eb2011-09-17 08:33:22 +0000152 lldb::BreakpointLocationSP this_sp(this);
Jim Inghamd1686902010-10-14 23:45:03 +0000153 if (m_options_ap.get())
Greg Clayton987c7eb2011-09-17 08:33:22 +0000154 return m_options_ap->GetThreadPlanToTestCondition (exe_ctx, this_sp, error);
Jim Inghamd1686902010-10-14 23:45:03 +0000155 else
Greg Clayton987c7eb2011-09-17 08:33:22 +0000156 return m_owner.GetThreadPlanToTestCondition (exe_ctx, this_sp, error);
Jim Inghamd1686902010-10-14 23:45:03 +0000157}
158
159const char *
Jim Inghamac354422011-06-15 21:16:00 +0000160BreakpointLocation::GetConditionText () const
Jim Inghamd1686902010-10-14 23:45:03 +0000161{
Jim Inghamac354422011-06-15 21:16:00 +0000162 return GetOptionsNoCreate()->GetConditionText();
Jim Inghamd1686902010-10-14 23:45:03 +0000163}
164
Greg Clayton54e7afa2010-07-09 20:39:50 +0000165uint32_t
Chris Lattner24943d22010-06-08 16:52:24 +0000166BreakpointLocation::GetIgnoreCount ()
167{
Jim Ingham9c6898b2010-06-22 21:12:54 +0000168 return GetOptionsNoCreate()->GetIgnoreCount();
Chris Lattner24943d22010-06-08 16:52:24 +0000169}
170
171void
Greg Clayton54e7afa2010-07-09 20:39:50 +0000172BreakpointLocation::SetIgnoreCount (uint32_t n)
Chris Lattner24943d22010-06-08 16:52:24 +0000173{
174 GetLocationOptions()->SetIgnoreCount(n);
175}
176
Jim Ingham3c7b5b92010-06-16 02:00:15 +0000177const BreakpointOptions *
Jim Ingham9c6898b2010-06-22 21:12:54 +0000178BreakpointLocation::GetOptionsNoCreate () const
Chris Lattner24943d22010-06-08 16:52:24 +0000179{
180 if (m_options_ap.get() != NULL)
181 return m_options_ap.get();
182 else
183 return m_owner.GetOptions ();
184}
185
186BreakpointOptions *
187BreakpointLocation::GetLocationOptions ()
188{
Jim Ingham649492b2010-06-18 01:00:58 +0000189 // If we make the copy we don't copy the callbacks because that is potentially
190 // expensive and we don't want to do that for the simple case where someone is
191 // just disabling the location.
Chris Lattner24943d22010-06-08 16:52:24 +0000192 if (m_options_ap.get() == NULL)
Jim Ingham649492b2010-06-18 01:00:58 +0000193 m_options_ap.reset(BreakpointOptions::CopyOptionsNoCallback(*m_owner.GetOptions ()));
194
Chris Lattner24943d22010-06-08 16:52:24 +0000195 return m_options_ap.get();
196}
197
Jim Ingham3c7b5b92010-06-16 02:00:15 +0000198bool
199BreakpointLocation::ValidForThisThread (Thread *thread)
200{
Jim Ingham9c6898b2010-06-22 21:12:54 +0000201 return thread->MatchesSpec(GetOptionsNoCreate()->GetThreadSpecNoCreate());
Jim Ingham3c7b5b92010-06-16 02:00:15 +0000202}
203
Chris Lattner24943d22010-06-08 16:52:24 +0000204// RETURNS - true if we should stop at this breakpoint, false if we
Jim Ingham3c7b5b92010-06-16 02:00:15 +0000205// should continue. Note, we don't check the thread spec for the breakpoint
206// here, since if the breakpoint is not for this thread, then the event won't
207// even get reported, so the check is redundant.
Chris Lattner24943d22010-06-08 16:52:24 +0000208
209bool
210BreakpointLocation::ShouldStop (StoppointCallbackContext *context)
211{
212 bool should_stop = true;
Greg Claytone005f2c2010-11-06 01:53:30 +0000213 LogSP log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS);
Chris Lattner24943d22010-06-08 16:52:24 +0000214
215 m_hit_count++;
216
217 if (!IsEnabled())
218 return false;
219
Chris Lattner24943d22010-06-08 16:52:24 +0000220 if (m_hit_count <= GetIgnoreCount())
221 return false;
222
Jim Inghamd1686902010-10-14 23:45:03 +0000223 // We only run synchronous callbacks in ShouldStop:
Chris Lattner24943d22010-06-08 16:52:24 +0000224 context->is_synchronous = true;
225 should_stop = InvokeCallback (context);
Jim Inghamd1686902010-10-14 23:45:03 +0000226
Jim Inghame5ed8e92011-06-02 23:58:26 +0000227 if (log)
Jim Inghamd1686902010-10-14 23:45:03 +0000228 {
Jim Inghame5ed8e92011-06-02 23:58:26 +0000229 StreamString s;
230 GetDescription (&s, lldb::eDescriptionLevelVerbose);
231 log->Printf ("Hit breakpoint location: %s, %s.\n", s.GetData(), should_stop ? "stopping" : "continuing");
Chris Lattner24943d22010-06-08 16:52:24 +0000232 }
Jim Inghame5ed8e92011-06-02 23:58:26 +0000233
Chris Lattner24943d22010-06-08 16:52:24 +0000234 return should_stop;
235}
236
237bool
238BreakpointLocation::IsResolved () const
239{
240 return m_bp_site_sp.get() != NULL;
241}
242
Jim Inghamd1686902010-10-14 23:45:03 +0000243lldb::BreakpointSiteSP
244BreakpointLocation::GetBreakpointSite() const
245{
246 return m_bp_site_sp;
247}
248
Chris Lattner24943d22010-06-08 16:52:24 +0000249bool
250BreakpointLocation::ResolveBreakpointSite ()
251{
252 if (m_bp_site_sp)
253 return true;
254
Greg Claytoneea26402010-09-14 23:36:40 +0000255 Process *process = m_owner.GetTarget().GetProcessSP().get();
Chris Lattner24943d22010-06-08 16:52:24 +0000256 if (process == NULL)
257 return false;
258
Greg Claytoneea26402010-09-14 23:36:40 +0000259 if (m_owner.GetTarget().GetSectionLoadList().IsEmpty())
260 return false;
261
Greg Clayton987c7eb2011-09-17 08:33:22 +0000262 BreakpointLocationSP this_sp(this);
Chris Lattner24943d22010-06-08 16:52:24 +0000263
Greg Clayton987c7eb2011-09-17 08:33:22 +0000264 lldb::break_id_t new_id = process->CreateBreakpointSite (this_sp, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000265
Stephen Wilson3fd1f362010-07-17 00:56:13 +0000266 if (new_id == LLDB_INVALID_BREAK_ID)
Chris Lattner24943d22010-06-08 16:52:24 +0000267 {
Greg Claytone005f2c2010-11-06 01:53:30 +0000268 LogSP log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS);
Chris Lattner24943d22010-06-08 16:52:24 +0000269 if (log)
270 log->Warning ("Tried to add breakpoint site at 0x%llx but it was already present.\n",
Greg Claytonc0fa5332011-05-22 22:46:53 +0000271 m_address.GetOpcodeLoadAddress (&m_owner.GetTarget()));
Chris Lattner24943d22010-06-08 16:52:24 +0000272 return false;
273 }
274
275 return true;
276}
277
278bool
279BreakpointLocation::SetBreakpointSite (BreakpointSiteSP& bp_site_sp)
280{
281 m_bp_site_sp = bp_site_sp;
282 return true;
283}
284
285bool
286BreakpointLocation::ClearBreakpointSite ()
287{
288 if (m_bp_site_sp.get())
289 {
Jim Ingham649492b2010-06-18 01:00:58 +0000290 m_owner.GetTarget().GetProcessSP()->RemoveOwnerFromBreakpointSite (GetBreakpoint().GetID(),
291 GetID(), m_bp_site_sp);
Chris Lattner24943d22010-06-08 16:52:24 +0000292 m_bp_site_sp.reset();
293 return true;
294 }
295 return false;
296}
297
298void
299BreakpointLocation::GetDescription (Stream *s, lldb::DescriptionLevel level)
300{
301 SymbolContext sc;
302 s->Indent();
303 BreakpointID::GetCanonicalReference(s, m_owner.GetID(), GetID());
304
305 if (level == lldb::eDescriptionLevelBrief)
306 return;
307
308 s->PutCString(": ");
309
310 if (level == lldb::eDescriptionLevelVerbose)
311 s->IndentMore();
312
313 if (m_address.IsSectionOffset())
314 {
315 m_address.CalculateSymbolContext(&sc);
316
317 if (level == lldb::eDescriptionLevelFull)
318 {
319 s->PutCString("where = ");
Greg Clayton5205f0b2010-09-03 17:10:42 +0000320 sc.DumpStopContext (s, m_owner.GetTarget().GetProcessSP().get(), m_address, false, true, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000321 }
322 else
323 {
324 if (sc.module_sp)
325 {
326 s->EOL();
327 s->Indent("module = ");
328 sc.module_sp->GetFileSpec().Dump (s);
329 }
330
331 if (sc.comp_unit != NULL)
332 {
333 s->EOL();
334 s->Indent("compile unit = ");
Jim Ingham7ea35232010-10-27 22:58:34 +0000335 static_cast<FileSpec*>(sc.comp_unit)->GetFilename().Dump (s);
Chris Lattner24943d22010-06-08 16:52:24 +0000336
337 if (sc.function != NULL)
338 {
339 s->EOL();
340 s->Indent("function = ");
341 s->PutCString (sc.function->GetMangled().GetName().AsCString("<unknown>"));
342 }
343
344 if (sc.line_entry.line > 0)
345 {
346 s->EOL();
347 s->Indent("location = ");
Greg Clayton72b71582010-09-02 21:44:10 +0000348 sc.line_entry.DumpStopContext (s, true);
Chris Lattner24943d22010-06-08 16:52:24 +0000349 }
350
351 }
352 else
353 {
354 // If we don't have a comp unit, see if we have a symbol we can print.
355 if (sc.symbol)
356 {
357 s->EOL();
358 s->Indent("symbol = ");
359 s->PutCString(sc.symbol->GetMangled().GetName().AsCString("<unknown>"));
360 }
361 }
362 }
363 }
364
365 if (level == lldb::eDescriptionLevelVerbose)
366 {
367 s->EOL();
368 s->Indent();
369 }
370 s->Printf ("%saddress = ", (level == lldb::eDescriptionLevelFull && m_address.IsSectionOffset()) ? ", " : "");
371 ExecutionContextScope *exe_scope = NULL;
372 Target *target = &m_owner.GetTarget();
373 if (target)
374 exe_scope = target->GetProcessSP().get();
375 if (exe_scope == NULL)
376 exe_scope = target;
377
378 m_address.Dump(s, exe_scope, Address::DumpStyleLoadAddress, Address::DumpStyleModuleWithFileAddress);
379
380 if (level == lldb::eDescriptionLevelVerbose)
381 {
382 s->EOL();
383 s->Indent();
384 s->Printf("resolved = %s\n", IsResolved() ? "true" : "false");
385
386 s->Indent();
Chris Lattner24943d22010-06-08 16:52:24 +0000387 s->Printf ("hit count = %-4u\n", GetHitCount());
388
389 if (m_options_ap.get())
390 {
Jim Ingham649492b2010-06-18 01:00:58 +0000391 s->Indent();
392 m_options_ap->GetDescription (s, level);
393 s->EOL();
Chris Lattner24943d22010-06-08 16:52:24 +0000394 }
395 s->IndentLess();
396 }
397 else
398 {
Jim Ingham649492b2010-06-18 01:00:58 +0000399 s->Printf(", %sresolved, hit count = %u ",
Chris Lattner24943d22010-06-08 16:52:24 +0000400 (IsResolved() ? "" : "un"),
Chris Lattner24943d22010-06-08 16:52:24 +0000401 GetHitCount());
Jim Ingham649492b2010-06-18 01:00:58 +0000402 if (m_options_ap.get())
403 {
404 m_options_ap->GetDescription (s, level);
405 }
Chris Lattner24943d22010-06-08 16:52:24 +0000406 }
407}
408
409void
410BreakpointLocation::Dump(Stream *s) const
411{
412 if (s == NULL)
413 return;
414
Greg Claytond9919d32011-12-01 23:28:38 +0000415 s->Printf("BreakpointLocation %u: tid = %4.4llx load addr = 0x%8.8llx state = %s type = %s breakpoint "
Jim Ingham649492b2010-06-18 01:00:58 +0000416 "hw_index = %i hit_count = %-4u ignore_count = %-4u",
Chris Lattner24943d22010-06-08 16:52:24 +0000417 GetID(),
Jim Ingham9c6898b2010-06-22 21:12:54 +0000418 GetOptionsNoCreate()->GetThreadSpecNoCreate()->GetTID(),
Greg Claytonc0fa5332011-05-22 22:46:53 +0000419 (uint64_t) m_address.GetOpcodeLoadAddress (&m_owner.GetTarget()),
Chris Lattner24943d22010-06-08 16:52:24 +0000420 (m_options_ap.get() ? m_options_ap->IsEnabled() : m_owner.IsEnabled()) ? "enabled " : "disabled",
421 IsHardware() ? "hardware" : "software",
422 GetHardwareIndex(),
423 GetHitCount(),
424 m_options_ap.get() ? m_options_ap->GetIgnoreCount() : m_owner.GetIgnoreCount());
425}