blob: fb123de56e6dce32830b10780961ebb64e485133 [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{
Jim Inghamd1686902010-10-14 23:45:03 +0000152 if (m_options_ap.get())
Greg Clayton13d24fb2012-01-29 20:56:30 +0000153 return m_options_ap->GetThreadPlanToTestCondition (exe_ctx, shared_from_this(), error);
Jim Inghamd1686902010-10-14 23:45:03 +0000154 else
Greg Clayton13d24fb2012-01-29 20:56:30 +0000155 return m_owner.GetThreadPlanToTestCondition (exe_ctx, shared_from_this(), error);
Jim Inghamd1686902010-10-14 23:45:03 +0000156}
157
158const char *
Jim Inghamac354422011-06-15 21:16:00 +0000159BreakpointLocation::GetConditionText () const
Jim Inghamd1686902010-10-14 23:45:03 +0000160{
Jim Inghamac354422011-06-15 21:16:00 +0000161 return GetOptionsNoCreate()->GetConditionText();
Jim Inghamd1686902010-10-14 23:45:03 +0000162}
163
Greg Clayton54e7afa2010-07-09 20:39:50 +0000164uint32_t
Chris Lattner24943d22010-06-08 16:52:24 +0000165BreakpointLocation::GetIgnoreCount ()
166{
Jim Ingham9c6898b2010-06-22 21:12:54 +0000167 return GetOptionsNoCreate()->GetIgnoreCount();
Chris Lattner24943d22010-06-08 16:52:24 +0000168}
169
170void
Greg Clayton54e7afa2010-07-09 20:39:50 +0000171BreakpointLocation::SetIgnoreCount (uint32_t n)
Chris Lattner24943d22010-06-08 16:52:24 +0000172{
173 GetLocationOptions()->SetIgnoreCount(n);
174}
175
Jim Ingham3c7b5b92010-06-16 02:00:15 +0000176const BreakpointOptions *
Jim Ingham9c6898b2010-06-22 21:12:54 +0000177BreakpointLocation::GetOptionsNoCreate () const
Chris Lattner24943d22010-06-08 16:52:24 +0000178{
179 if (m_options_ap.get() != NULL)
180 return m_options_ap.get();
181 else
182 return m_owner.GetOptions ();
183}
184
185BreakpointOptions *
186BreakpointLocation::GetLocationOptions ()
187{
Jim Ingham649492b2010-06-18 01:00:58 +0000188 // If we make the copy we don't copy the callbacks because that is potentially
189 // expensive and we don't want to do that for the simple case where someone is
190 // just disabling the location.
Chris Lattner24943d22010-06-08 16:52:24 +0000191 if (m_options_ap.get() == NULL)
Jim Ingham649492b2010-06-18 01:00:58 +0000192 m_options_ap.reset(BreakpointOptions::CopyOptionsNoCallback(*m_owner.GetOptions ()));
193
Chris Lattner24943d22010-06-08 16:52:24 +0000194 return m_options_ap.get();
195}
196
Jim Ingham3c7b5b92010-06-16 02:00:15 +0000197bool
198BreakpointLocation::ValidForThisThread (Thread *thread)
199{
Jim Ingham9c6898b2010-06-22 21:12:54 +0000200 return thread->MatchesSpec(GetOptionsNoCreate()->GetThreadSpecNoCreate());
Jim Ingham3c7b5b92010-06-16 02:00:15 +0000201}
202
Chris Lattner24943d22010-06-08 16:52:24 +0000203// RETURNS - true if we should stop at this breakpoint, false if we
Jim Ingham3c7b5b92010-06-16 02:00:15 +0000204// should continue. Note, we don't check the thread spec for the breakpoint
205// here, since if the breakpoint is not for this thread, then the event won't
206// even get reported, so the check is redundant.
Chris Lattner24943d22010-06-08 16:52:24 +0000207
208bool
209BreakpointLocation::ShouldStop (StoppointCallbackContext *context)
210{
211 bool should_stop = true;
Greg Claytone005f2c2010-11-06 01:53:30 +0000212 LogSP log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS);
Chris Lattner24943d22010-06-08 16:52:24 +0000213
Johnny Chen51b7c5f2012-01-23 23:03:59 +0000214 IncrementHitCount();
Chris Lattner24943d22010-06-08 16:52:24 +0000215
216 if (!IsEnabled())
217 return false;
218
Johnny Chen51b7c5f2012-01-23 23:03:59 +0000219 if (GetHitCount() <= GetIgnoreCount())
Chris Lattner24943d22010-06-08 16:52:24 +0000220 return false;
221
Jim Inghamd1686902010-10-14 23:45:03 +0000222 // We only run synchronous callbacks in ShouldStop:
Chris Lattner24943d22010-06-08 16:52:24 +0000223 context->is_synchronous = true;
224 should_stop = InvokeCallback (context);
Jim Inghamd1686902010-10-14 23:45:03 +0000225
Jim Inghame5ed8e92011-06-02 23:58:26 +0000226 if (log)
Jim Inghamd1686902010-10-14 23:45:03 +0000227 {
Jim Inghame5ed8e92011-06-02 23:58:26 +0000228 StreamString s;
229 GetDescription (&s, lldb::eDescriptionLevelVerbose);
230 log->Printf ("Hit breakpoint location: %s, %s.\n", s.GetData(), should_stop ? "stopping" : "continuing");
Chris Lattner24943d22010-06-08 16:52:24 +0000231 }
Jim Inghame5ed8e92011-06-02 23:58:26 +0000232
Chris Lattner24943d22010-06-08 16:52:24 +0000233 return should_stop;
234}
235
236bool
237BreakpointLocation::IsResolved () const
238{
239 return m_bp_site_sp.get() != NULL;
240}
241
Jim Inghamd1686902010-10-14 23:45:03 +0000242lldb::BreakpointSiteSP
243BreakpointLocation::GetBreakpointSite() const
244{
245 return m_bp_site_sp;
246}
247
Chris Lattner24943d22010-06-08 16:52:24 +0000248bool
249BreakpointLocation::ResolveBreakpointSite ()
250{
251 if (m_bp_site_sp)
252 return true;
253
Greg Claytoneea26402010-09-14 23:36:40 +0000254 Process *process = m_owner.GetTarget().GetProcessSP().get();
Chris Lattner24943d22010-06-08 16:52:24 +0000255 if (process == NULL)
256 return false;
257
Greg Claytoneea26402010-09-14 23:36:40 +0000258 if (m_owner.GetTarget().GetSectionLoadList().IsEmpty())
259 return false;
260
Greg Clayton13d24fb2012-01-29 20:56:30 +0000261 lldb::break_id_t new_id = process->CreateBreakpointSite (shared_from_this(), false);
Chris Lattner24943d22010-06-08 16:52:24 +0000262
Stephen Wilson3fd1f362010-07-17 00:56:13 +0000263 if (new_id == LLDB_INVALID_BREAK_ID)
Chris Lattner24943d22010-06-08 16:52:24 +0000264 {
Greg Claytone005f2c2010-11-06 01:53:30 +0000265 LogSP log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS);
Chris Lattner24943d22010-06-08 16:52:24 +0000266 if (log)
267 log->Warning ("Tried to add breakpoint site at 0x%llx but it was already present.\n",
Greg Claytonc0fa5332011-05-22 22:46:53 +0000268 m_address.GetOpcodeLoadAddress (&m_owner.GetTarget()));
Chris Lattner24943d22010-06-08 16:52:24 +0000269 return false;
270 }
271
272 return true;
273}
274
275bool
276BreakpointLocation::SetBreakpointSite (BreakpointSiteSP& bp_site_sp)
277{
278 m_bp_site_sp = bp_site_sp;
279 return true;
280}
281
282bool
283BreakpointLocation::ClearBreakpointSite ()
284{
285 if (m_bp_site_sp.get())
286 {
Jim Ingham649492b2010-06-18 01:00:58 +0000287 m_owner.GetTarget().GetProcessSP()->RemoveOwnerFromBreakpointSite (GetBreakpoint().GetID(),
288 GetID(), m_bp_site_sp);
Chris Lattner24943d22010-06-08 16:52:24 +0000289 m_bp_site_sp.reset();
290 return true;
291 }
292 return false;
293}
294
295void
296BreakpointLocation::GetDescription (Stream *s, lldb::DescriptionLevel level)
297{
298 SymbolContext sc;
299 s->Indent();
300 BreakpointID::GetCanonicalReference(s, m_owner.GetID(), GetID());
301
302 if (level == lldb::eDescriptionLevelBrief)
303 return;
304
305 s->PutCString(": ");
306
307 if (level == lldb::eDescriptionLevelVerbose)
308 s->IndentMore();
309
310 if (m_address.IsSectionOffset())
311 {
312 m_address.CalculateSymbolContext(&sc);
313
314 if (level == lldb::eDescriptionLevelFull)
315 {
316 s->PutCString("where = ");
Greg Clayton5205f0b2010-09-03 17:10:42 +0000317 sc.DumpStopContext (s, m_owner.GetTarget().GetProcessSP().get(), m_address, false, true, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000318 }
319 else
320 {
321 if (sc.module_sp)
322 {
323 s->EOL();
324 s->Indent("module = ");
325 sc.module_sp->GetFileSpec().Dump (s);
326 }
327
328 if (sc.comp_unit != NULL)
329 {
330 s->EOL();
331 s->Indent("compile unit = ");
Jim Ingham7ea35232010-10-27 22:58:34 +0000332 static_cast<FileSpec*>(sc.comp_unit)->GetFilename().Dump (s);
Chris Lattner24943d22010-06-08 16:52:24 +0000333
334 if (sc.function != NULL)
335 {
336 s->EOL();
337 s->Indent("function = ");
338 s->PutCString (sc.function->GetMangled().GetName().AsCString("<unknown>"));
339 }
340
341 if (sc.line_entry.line > 0)
342 {
343 s->EOL();
344 s->Indent("location = ");
Greg Clayton72b71582010-09-02 21:44:10 +0000345 sc.line_entry.DumpStopContext (s, true);
Chris Lattner24943d22010-06-08 16:52:24 +0000346 }
347
348 }
349 else
350 {
351 // If we don't have a comp unit, see if we have a symbol we can print.
352 if (sc.symbol)
353 {
354 s->EOL();
355 s->Indent("symbol = ");
356 s->PutCString(sc.symbol->GetMangled().GetName().AsCString("<unknown>"));
357 }
358 }
359 }
360 }
361
362 if (level == lldb::eDescriptionLevelVerbose)
363 {
364 s->EOL();
365 s->Indent();
366 }
367 s->Printf ("%saddress = ", (level == lldb::eDescriptionLevelFull && m_address.IsSectionOffset()) ? ", " : "");
368 ExecutionContextScope *exe_scope = NULL;
369 Target *target = &m_owner.GetTarget();
370 if (target)
371 exe_scope = target->GetProcessSP().get();
372 if (exe_scope == NULL)
373 exe_scope = target;
374
375 m_address.Dump(s, exe_scope, Address::DumpStyleLoadAddress, Address::DumpStyleModuleWithFileAddress);
376
377 if (level == lldb::eDescriptionLevelVerbose)
378 {
379 s->EOL();
380 s->Indent();
381 s->Printf("resolved = %s\n", IsResolved() ? "true" : "false");
382
383 s->Indent();
Chris Lattner24943d22010-06-08 16:52:24 +0000384 s->Printf ("hit count = %-4u\n", GetHitCount());
385
386 if (m_options_ap.get())
387 {
Jim Ingham649492b2010-06-18 01:00:58 +0000388 s->Indent();
389 m_options_ap->GetDescription (s, level);
390 s->EOL();
Chris Lattner24943d22010-06-08 16:52:24 +0000391 }
392 s->IndentLess();
393 }
394 else
395 {
Jim Ingham649492b2010-06-18 01:00:58 +0000396 s->Printf(", %sresolved, hit count = %u ",
Chris Lattner24943d22010-06-08 16:52:24 +0000397 (IsResolved() ? "" : "un"),
Chris Lattner24943d22010-06-08 16:52:24 +0000398 GetHitCount());
Jim Ingham649492b2010-06-18 01:00:58 +0000399 if (m_options_ap.get())
400 {
401 m_options_ap->GetDescription (s, level);
402 }
Chris Lattner24943d22010-06-08 16:52:24 +0000403 }
404}
405
406void
407BreakpointLocation::Dump(Stream *s) const
408{
409 if (s == NULL)
410 return;
411
Greg Claytond9919d32011-12-01 23:28:38 +0000412 s->Printf("BreakpointLocation %u: tid = %4.4llx load addr = 0x%8.8llx state = %s type = %s breakpoint "
Jim Ingham649492b2010-06-18 01:00:58 +0000413 "hw_index = %i hit_count = %-4u ignore_count = %-4u",
Johnny Chenf8c0fc52012-01-26 00:08:14 +0000414 GetID(),
415 GetOptionsNoCreate()->GetThreadSpecNoCreate()->GetTID(),
416 (uint64_t) m_address.GetOpcodeLoadAddress (&m_owner.GetTarget()),
417 (m_options_ap.get() ? m_options_ap->IsEnabled() : m_owner.IsEnabled()) ? "enabled " : "disabled",
418 IsHardware() ? "hardware" : "software",
419 GetHardwareIndex(),
420 GetHitCount(),
421 GetOptionsNoCreate()->GetIgnoreCount());
Chris Lattner24943d22010-06-08 16:52:24 +0000422}