blob: 346c7747b5c62668c098cd2c0a3dd2ceb8619998 [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +00001//===-- Breakpoint.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
11// C Includes
12// C++ Includes
13// Other libraries and framework includes
14// Project includes
15
16#include "lldb/Core/Address.h"
17#include "lldb/Breakpoint/Breakpoint.h"
18#include "lldb/Breakpoint/BreakpointLocation.h"
Johnny Chena62ad7c2010-10-28 17:27:46 +000019#include "lldb/Breakpoint/BreakpointLocationCollection.h"
Chris Lattner24943d22010-06-08 16:52:24 +000020#include "lldb/Breakpoint/BreakpointResolver.h"
Johnny Chena62ad7c2010-10-28 17:27:46 +000021#include "lldb/Breakpoint/BreakpointResolverFileLine.h"
Chris Lattner24943d22010-06-08 16:52:24 +000022#include "lldb/Core/Log.h"
23#include "lldb/Core/ModuleList.h"
24#include "lldb/Core/SearchFilter.h"
25#include "lldb/Core/Stream.h"
26#include "lldb/Core/StreamString.h"
27#include "lldb/Symbol/SymbolContext.h"
28#include "lldb/Target/Target.h"
Jim Ingham3c7b5b92010-06-16 02:00:15 +000029#include "lldb/Target/ThreadSpec.h"
Chris Lattner24943d22010-06-08 16:52:24 +000030#include "lldb/lldb-private-log.h"
Johnny Chena62ad7c2010-10-28 17:27:46 +000031#include "llvm/Support/Casting.h"
Chris Lattner24943d22010-06-08 16:52:24 +000032
33using namespace lldb;
34using namespace lldb_private;
Johnny Chena62ad7c2010-10-28 17:27:46 +000035using namespace llvm;
Chris Lattner24943d22010-06-08 16:52:24 +000036
37const ConstString &
38Breakpoint::GetEventIdentifier ()
39{
40 static ConstString g_identifier("event-identifier.breakpoint.changed");
41 return g_identifier;
42}
43
44//----------------------------------------------------------------------
45// Breakpoint constructor
46//----------------------------------------------------------------------
47Breakpoint::Breakpoint(Target &target, SearchFilterSP &filter_sp, BreakpointResolverSP &resolver_sp) :
48 m_target (target),
49 m_filter_sp (filter_sp),
50 m_resolver_sp (resolver_sp),
51 m_options (),
52 m_locations ()
53{
54}
55
56//----------------------------------------------------------------------
57// Destructor
58//----------------------------------------------------------------------
59Breakpoint::~Breakpoint()
60{
61}
62
63bool
64Breakpoint::IsInternal () const
65{
66 return LLDB_BREAK_ID_IS_INTERNAL(m_bid);
67}
68
69
70
71Target&
72Breakpoint::GetTarget ()
73{
74 return m_target;
75}
76
77const Target&
78Breakpoint::GetTarget () const
79{
80 return m_target;
81}
82
83BreakpointLocationSP
84Breakpoint::AddLocation (Address &addr, bool *new_location)
85{
86 BreakpointLocationSP bp_loc_sp (m_locations.FindByAddress(addr));
87 if (bp_loc_sp)
88 {
89 if (new_location)
90 *new_location = false;
91 return bp_loc_sp;
92 }
93
94 bp_loc_sp.reset (new BreakpointLocation (m_locations.GetNextID(), *this, addr));
95 m_locations.Add (bp_loc_sp);
96 bp_loc_sp->ResolveBreakpointSite();
97
98 if (new_location)
99 *new_location = true;
100 return bp_loc_sp;
101}
102
103BreakpointLocationSP
104Breakpoint::FindLocationByAddress (Address &addr)
105{
106 return m_locations.FindByAddress(addr);
107}
108
109break_id_t
110Breakpoint::FindLocationIDByAddress (Address &addr)
111{
112 return m_locations.FindIDByAddress(addr);
113}
114
115BreakpointLocationSP
116Breakpoint::FindLocationByID (break_id_t bp_loc_id)
117{
118 return m_locations.FindByID(bp_loc_id);
119}
120
121BreakpointLocationSP
122Breakpoint::GetLocationAtIndex (uint32_t index)
123{
124 return m_locations.GetByIndex(index);
125}
126
127BreakpointLocationSP
128Breakpoint::GetLocationSP (BreakpointLocation *bp_loc_ptr)
129{
130 assert (bp_loc_ptr->GetBreakpoint().GetID() == GetID());
131 return m_locations.FindByID(bp_loc_ptr->GetID());
132}
133
134
135// For each of the overall options we need to decide how they propagate to
136// the location options. This will determine the precedence of options on
137// the breakpoint vrs. its locations.
138
139// Disable at the breakpoint level should override the location settings.
140// That way you can conveniently turn off a whole breakpoint without messing
141// up the individual settings.
142
143void
144Breakpoint::SetEnabled (bool enable)
145{
146 m_options.SetEnabled(enable);
147 if (enable)
148 m_locations.ResolveAllBreakpointSites();
149 else
150 m_locations.ClearAllBreakpointSites();
151}
152
153bool
154Breakpoint::IsEnabled ()
155{
156 return m_options.IsEnabled();
157}
158
159void
Greg Clayton54e7afa2010-07-09 20:39:50 +0000160Breakpoint::SetIgnoreCount (uint32_t n)
Chris Lattner24943d22010-06-08 16:52:24 +0000161{
162 m_options.SetIgnoreCount(n);
163}
164
Greg Clayton54e7afa2010-07-09 20:39:50 +0000165uint32_t
Chris Lattner24943d22010-06-08 16:52:24 +0000166Breakpoint::GetIgnoreCount () const
167{
168 return m_options.GetIgnoreCount();
169}
170
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000171uint32_t
172Breakpoint::GetHitCount () const
173{
174 return m_locations.GetHitCount();
175}
176
Chris Lattner24943d22010-06-08 16:52:24 +0000177void
178Breakpoint::SetThreadID (lldb::tid_t thread_id)
179{
Jim Ingham3c7b5b92010-06-16 02:00:15 +0000180 m_options.GetThreadSpec()->SetTID(thread_id);
Chris Lattner24943d22010-06-08 16:52:24 +0000181}
182
183lldb::tid_t
184Breakpoint::GetThreadID ()
185{
Jim Ingham3c7b5b92010-06-16 02:00:15 +0000186 if (m_options.GetThreadSpec() == NULL)
187 return LLDB_INVALID_THREAD_ID;
188 else
189 return m_options.GetThreadSpec()->GetTID();
Chris Lattner24943d22010-06-08 16:52:24 +0000190}
191
Jim Inghamd1686902010-10-14 23:45:03 +0000192void
193Breakpoint::SetCondition (const char *condition)
194{
195 m_options.SetCondition (condition);
196}
197
198ThreadPlan *
199Breakpoint::GetThreadPlanToTestCondition (ExecutionContext &exe_ctx, lldb::BreakpointLocationSP loc_sp, Stream &error)
200{
201 return m_options.GetThreadPlanToTestCondition (exe_ctx, loc_sp, error);
202}
203
204const char *
205Breakpoint::GetConditionText ()
206{
207 return m_options.GetConditionText();
208}
209
Chris Lattner24943d22010-06-08 16:52:24 +0000210// This function is used when "baton" doesn't need to be freed
211void
212Breakpoint::SetCallback (BreakpointHitCallback callback, void *baton, bool is_synchronous)
213{
214 // The default "Baton" class will keep a copy of "baton" and won't free
215 // or delete it when it goes goes out of scope.
216 m_options.SetCallback(callback, BatonSP (new Baton(baton)), is_synchronous);
217}
218
219// This function is used when a baton needs to be freed and therefore is
220// contained in a "Baton" subclass.
221void
222Breakpoint::SetCallback (BreakpointHitCallback callback, const BatonSP &callback_baton_sp, bool is_synchronous)
223{
224 m_options.SetCallback(callback, callback_baton_sp, is_synchronous);
225}
226
227void
228Breakpoint::ClearCallback ()
229{
230 m_options.ClearCallback ();
231}
232
233bool
234Breakpoint::InvokeCallback (StoppointCallbackContext *context, break_id_t bp_loc_id)
235{
236 return m_options.InvokeCallback (context, GetID(), bp_loc_id);
237}
238
239BreakpointOptions *
240Breakpoint::GetOptions ()
241{
242 return &m_options;
243}
244
245void
246Breakpoint::ResolveBreakpoint ()
247{
248 if (m_resolver_sp)
249 m_resolver_sp->ResolveBreakpoint(*m_filter_sp);
250}
251
252void
253Breakpoint::ResolveBreakpointInModules (ModuleList &module_list)
254{
255 if (m_resolver_sp)
256 m_resolver_sp->ResolveBreakpointInModules(*m_filter_sp, module_list);
257}
258
259void
260Breakpoint::ClearAllBreakpointSites ()
261{
262 m_locations.ClearAllBreakpointSites();
263}
264
265//----------------------------------------------------------------------
266// ModulesChanged: Pass in a list of new modules, and
267//----------------------------------------------------------------------
268
269void
270Breakpoint::ModulesChanged (ModuleList &module_list, bool load)
271{
272 if (load)
273 {
274 // The logic for handling new modules is:
275 // 1) If the filter rejects this module, then skip it.
276 // 2) Run through the current location list and if there are any locations
277 // for that module, we mark the module as "seen" and we don't try to re-resolve
278 // breakpoint locations for that module.
279 // However, we do add breakpoint sites to these locations if needed.
280 // 3) If we don't see this module in our breakpoint location list, call ResolveInModules.
281
282 ModuleList new_modules; // We'll stuff the "unseen" modules in this list, and then resolve
283 // them after the locations pass. Have to do it this way because
284 // resolving breakpoints will add new locations potentially.
285
Greg Clayton54e7afa2010-07-09 20:39:50 +0000286 for (size_t i = 0; i < module_list.GetSize(); i++)
Chris Lattner24943d22010-06-08 16:52:24 +0000287 {
288 bool seen = false;
289 ModuleSP module_sp (module_list.GetModuleAtIndex (i));
290 Module *module = module_sp.get();
291 if (!m_filter_sp->ModulePasses (module_sp))
292 continue;
293
Greg Clayton7b9fcc02010-12-06 23:51:26 +0000294 for (size_t loc_idx = 0; loc_idx < m_locations.GetSize(); loc_idx++)
Chris Lattner24943d22010-06-08 16:52:24 +0000295 {
Greg Clayton7b9fcc02010-12-06 23:51:26 +0000296 BreakpointLocationSP break_loc = m_locations.GetByIndex(loc_idx);
Jim Ingham1de036b2010-10-20 03:36:33 +0000297 if (!break_loc->IsEnabled())
298 continue;
Chris Lattner24943d22010-06-08 16:52:24 +0000299 const Section *section = break_loc->GetAddress().GetSection();
300 if (section == NULL || section->GetModule() == module)
301 {
302 if (!seen)
303 seen = true;
304
305 if (!break_loc->ResolveBreakpointSite())
306 {
Greg Claytone005f2c2010-11-06 01:53:30 +0000307 LogSP log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000308 if (log)
309 log->Printf ("Warning: could not set breakpoint site for breakpoint location %d of breakpoint %d.\n",
310 break_loc->GetID(), GetID());
311 }
312 }
313 }
314
315 if (!seen)
Greg Claytonab429022010-12-12 21:03:32 +0000316 new_modules.AppendIfNeeded (module_sp);
Chris Lattner24943d22010-06-08 16:52:24 +0000317
318 }
319 if (new_modules.GetSize() > 0)
320 {
321 ResolveBreakpointInModules(new_modules);
322 }
323 }
324 else
325 {
326 // Go through the currently set locations and if any have breakpoints in
327 // the module list, then remove their breakpoint sites.
328 // FIXME: Think about this... Maybe it's better to delete the locations?
329 // Are we sure that on load-unload-reload the module pointer will remain
330 // the same? Or do we need to do an equality on modules that is an
331 // "equivalence"???
332
Greg Clayton54e7afa2010-07-09 20:39:50 +0000333 for (size_t i = 0; i < module_list.GetSize(); i++)
Chris Lattner24943d22010-06-08 16:52:24 +0000334 {
335 ModuleSP module_sp (module_list.GetModuleAtIndex (i));
Greg Clayton7b9fcc02010-12-06 23:51:26 +0000336 if (m_filter_sp->ModulePasses (module_sp))
Chris Lattner24943d22010-06-08 16:52:24 +0000337 {
Greg Clayton7b9fcc02010-12-06 23:51:26 +0000338 const size_t num_locs = m_locations.GetSize();
339 for (size_t loc_idx = 0; loc_idx < num_locs; ++loc_idx)
Chris Lattner24943d22010-06-08 16:52:24 +0000340 {
Greg Clayton7b9fcc02010-12-06 23:51:26 +0000341 BreakpointLocationSP break_loc = m_locations.GetByIndex(loc_idx);
342 const Section *section = break_loc->GetAddress().GetSection();
343 if (section && section->GetModule() == module_sp.get())
344 {
345 // Remove this breakpoint since the shared library is
346 // unloaded, but keep the breakpoint location around
347 // so we always get complete hit count and breakpoint
348 // lifetime info
Chris Lattner24943d22010-06-08 16:52:24 +0000349 break_loc->ClearBreakpointSite();
Greg Clayton7b9fcc02010-12-06 23:51:26 +0000350 }
Chris Lattner24943d22010-06-08 16:52:24 +0000351 }
Chris Lattner24943d22010-06-08 16:52:24 +0000352 }
353 }
354 }
355}
356
357void
358Breakpoint::Dump (Stream *)
359{
360}
361
362size_t
363Breakpoint::GetNumResolvedLocations() const
364{
365 // Return the number of breakpoints that are actually resolved and set
366 // down in the inferior process.
367 return m_locations.GetNumResolvedLocations();
368}
369
370size_t
371Breakpoint::GetNumLocations() const
372{
373 return m_locations.GetSize();
374}
375
376void
377Breakpoint::GetDescription (Stream *s, lldb::DescriptionLevel level, bool show_locations)
378{
379 assert (s != NULL);
Greg Clayton12bec712010-06-28 21:30:43 +0000380 s->Printf("%i: ", GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000381 GetResolverDescription (s);
Greg Clayton12bec712010-06-28 21:30:43 +0000382 GetFilterDescription (s);
Chris Lattner24943d22010-06-08 16:52:24 +0000383
Greg Clayton54e7afa2010-07-09 20:39:50 +0000384 const size_t num_locations = GetNumLocations ();
385 const size_t num_resolved_locations = GetNumResolvedLocations ();
Chris Lattner24943d22010-06-08 16:52:24 +0000386
387 switch (level)
388 {
389 case lldb::eDescriptionLevelBrief:
390 case lldb::eDescriptionLevelFull:
391 if (num_locations > 0)
392 {
Greg Clayton54e7afa2010-07-09 20:39:50 +0000393 s->Printf(", locations = %zu", num_locations);
Chris Lattner24943d22010-06-08 16:52:24 +0000394 if (num_resolved_locations > 0)
Greg Clayton54e7afa2010-07-09 20:39:50 +0000395 s->Printf(", resolved = %zu", num_resolved_locations);
Chris Lattner24943d22010-06-08 16:52:24 +0000396 }
397 else
398 {
Greg Clayton12bec712010-06-28 21:30:43 +0000399 s->Printf(", locations = 0 (pending)");
Chris Lattner24943d22010-06-08 16:52:24 +0000400 }
401
Jim Ingham649492b2010-06-18 01:00:58 +0000402 GetOptions()->GetDescription(s, level);
403
Chris Lattner24943d22010-06-08 16:52:24 +0000404 if (level == lldb::eDescriptionLevelFull)
405 {
Jim Ingham649492b2010-06-18 01:00:58 +0000406 s->IndentLess();
407 s->EOL();
Chris Lattner24943d22010-06-08 16:52:24 +0000408 }
409 break;
410
411 case lldb::eDescriptionLevelVerbose:
412 // Verbose mode does a debug dump of the breakpoint
413 Dump (s);
Jim Ingham649492b2010-06-18 01:00:58 +0000414 s->EOL ();
415 s->Indent();
416 GetOptions()->GetDescription(s, level);
Chris Lattner24943d22010-06-08 16:52:24 +0000417 break;
Greg Clayton54e7afa2010-07-09 20:39:50 +0000418
419 default:
420 break;
Chris Lattner24943d22010-06-08 16:52:24 +0000421 }
422
423 if (show_locations)
424 {
Chris Lattner24943d22010-06-08 16:52:24 +0000425 s->IndentMore();
Greg Clayton54e7afa2010-07-09 20:39:50 +0000426 for (size_t i = 0; i < num_locations; ++i)
Chris Lattner24943d22010-06-08 16:52:24 +0000427 {
428 BreakpointLocation *loc = GetLocationAtIndex(i).get();
429 loc->GetDescription(s, level);
430 s->EOL();
431 }
432 s->IndentLess();
Chris Lattner24943d22010-06-08 16:52:24 +0000433 }
434}
435
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000436Breakpoint::BreakpointEventData::BreakpointEventData (BreakpointEventType sub_type,
Jim Ingham649492b2010-06-18 01:00:58 +0000437 BreakpointSP &new_breakpoint_sp) :
Chris Lattner24943d22010-06-08 16:52:24 +0000438 EventData (),
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000439 m_breakpoint_event (sub_type),
Chris Lattner24943d22010-06-08 16:52:24 +0000440 m_new_breakpoint_sp (new_breakpoint_sp)
441{
442}
443
444Breakpoint::BreakpointEventData::~BreakpointEventData ()
445{
446}
447
448const ConstString &
449Breakpoint::BreakpointEventData::GetFlavorString ()
450{
451 static ConstString g_flavor ("Breakpoint::BreakpointEventData");
452 return g_flavor;
453}
454
455const ConstString &
456Breakpoint::BreakpointEventData::GetFlavor () const
457{
458 return BreakpointEventData::GetFlavorString ();
459}
460
461
462BreakpointSP &
463Breakpoint::BreakpointEventData::GetBreakpoint ()
464{
465 return m_new_breakpoint_sp;
466}
467
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000468BreakpointEventType
469Breakpoint::BreakpointEventData::GetBreakpointEventType () const
Chris Lattner24943d22010-06-08 16:52:24 +0000470{
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000471 return m_breakpoint_event;
Chris Lattner24943d22010-06-08 16:52:24 +0000472}
473
474void
475Breakpoint::BreakpointEventData::Dump (Stream *s) const
476{
477}
478
479Breakpoint::BreakpointEventData *
480Breakpoint::BreakpointEventData::GetEventDataFromEvent (const EventSP &event_sp)
481{
482 if (event_sp)
483 {
484 EventData *event_data = event_sp->GetData();
485 if (event_data && event_data->GetFlavor() == BreakpointEventData::GetFlavorString())
486 return static_cast <BreakpointEventData *> (event_sp->GetData());
487 }
488 return NULL;
489}
490
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000491BreakpointEventType
492Breakpoint::BreakpointEventData::GetBreakpointEventTypeFromEvent (const EventSP &event_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000493{
494 BreakpointEventData *data = GetEventDataFromEvent (event_sp);
495
496 if (data == NULL)
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000497 return eBreakpointEventTypeInvalidType;
Chris Lattner24943d22010-06-08 16:52:24 +0000498 else
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000499 return data->GetBreakpointEventType();
Chris Lattner24943d22010-06-08 16:52:24 +0000500}
501
502BreakpointSP
503Breakpoint::BreakpointEventData::GetBreakpointFromEvent (const EventSP &event_sp)
504{
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000505 BreakpointSP bp_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000506
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000507 BreakpointEventData *data = GetEventDataFromEvent (event_sp);
508 if (data)
509 bp_sp = data->GetBreakpoint();
510
511 return bp_sp;
512}
513
514lldb::BreakpointLocationSP
515Breakpoint::BreakpointEventData::GetBreakpointLocationAtIndexFromEvent (const lldb::EventSP &event_sp, uint32_t bp_loc_idx)
516{
517 lldb::BreakpointLocationSP bp_loc_sp;
518
519 BreakpointEventData *data = GetEventDataFromEvent (event_sp);
520 if (data)
Chris Lattner24943d22010-06-08 16:52:24 +0000521 {
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000522 Breakpoint *bp = data->GetBreakpoint().get();
523 if (bp)
524 bp_loc_sp = bp->GetLocationAtIndex(bp_loc_idx);
Chris Lattner24943d22010-06-08 16:52:24 +0000525 }
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000526
527 return bp_loc_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000528}
529
530
531void
532Breakpoint::GetResolverDescription (Stream *s)
533{
534 if (m_resolver_sp)
535 m_resolver_sp->GetDescription (s);
536}
537
Johnny Chena62ad7c2010-10-28 17:27:46 +0000538
539bool
540Breakpoint::GetMatchingFileLine (const ConstString &filename, uint32_t line_number, BreakpointLocationCollection &loc_coll)
541{
542 // TODO: To be correct, this method needs to fill the breakpoint location collection
543 // with the location IDs which match the filename and line_number.
544 //
545
546 if (m_resolver_sp)
547 {
548 BreakpointResolverFileLine *resolverFileLine = dyn_cast<BreakpointResolverFileLine>(m_resolver_sp.get());
549 if (resolverFileLine &&
550 resolverFileLine->m_file_spec.GetFilename() == filename &&
551 resolverFileLine->m_line_number == line_number)
552 {
553 return true;
554 }
555 }
556 return false;
557}
558
Chris Lattner24943d22010-06-08 16:52:24 +0000559void
560Breakpoint::GetFilterDescription (Stream *s)
561{
562 m_filter_sp->GetDescription (s);
563}
564
565const BreakpointSP
566Breakpoint::GetSP ()
567{
568 return m_target.GetBreakpointList().FindBreakpointByID (GetID());
569}