blob: c79b1a89afe38d3d195d0b55a395fb93d2f8f7ec [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"
19#include "lldb/Breakpoint/BreakpointResolver.h"
20#include "lldb/Core/Log.h"
21#include "lldb/Core/ModuleList.h"
22#include "lldb/Core/SearchFilter.h"
23#include "lldb/Core/Stream.h"
24#include "lldb/Core/StreamString.h"
25#include "lldb/Symbol/SymbolContext.h"
26#include "lldb/Target/Target.h"
Jim Ingham3c7b5b92010-06-16 02:00:15 +000027#include "lldb/Target/ThreadSpec.h"
Chris Lattner24943d22010-06-08 16:52:24 +000028#include "lldb/lldb-private-log.h"
29
30using namespace lldb;
31using namespace lldb_private;
32
33const ConstString &
34Breakpoint::GetEventIdentifier ()
35{
36 static ConstString g_identifier("event-identifier.breakpoint.changed");
37 return g_identifier;
38}
39
40//----------------------------------------------------------------------
41// Breakpoint constructor
42//----------------------------------------------------------------------
43Breakpoint::Breakpoint(Target &target, SearchFilterSP &filter_sp, BreakpointResolverSP &resolver_sp) :
44 m_target (target),
45 m_filter_sp (filter_sp),
46 m_resolver_sp (resolver_sp),
47 m_options (),
48 m_locations ()
49{
50}
51
52//----------------------------------------------------------------------
53// Destructor
54//----------------------------------------------------------------------
55Breakpoint::~Breakpoint()
56{
57}
58
59bool
60Breakpoint::IsInternal () const
61{
62 return LLDB_BREAK_ID_IS_INTERNAL(m_bid);
63}
64
65
66
67Target&
68Breakpoint::GetTarget ()
69{
70 return m_target;
71}
72
73const Target&
74Breakpoint::GetTarget () const
75{
76 return m_target;
77}
78
79BreakpointLocationSP
80Breakpoint::AddLocation (Address &addr, bool *new_location)
81{
82 BreakpointLocationSP bp_loc_sp (m_locations.FindByAddress(addr));
83 if (bp_loc_sp)
84 {
85 if (new_location)
86 *new_location = false;
87 return bp_loc_sp;
88 }
89
90 bp_loc_sp.reset (new BreakpointLocation (m_locations.GetNextID(), *this, addr));
91 m_locations.Add (bp_loc_sp);
92 bp_loc_sp->ResolveBreakpointSite();
93
94 if (new_location)
95 *new_location = true;
96 return bp_loc_sp;
97}
98
99BreakpointLocationSP
100Breakpoint::FindLocationByAddress (Address &addr)
101{
102 return m_locations.FindByAddress(addr);
103}
104
105break_id_t
106Breakpoint::FindLocationIDByAddress (Address &addr)
107{
108 return m_locations.FindIDByAddress(addr);
109}
110
111BreakpointLocationSP
112Breakpoint::FindLocationByID (break_id_t bp_loc_id)
113{
114 return m_locations.FindByID(bp_loc_id);
115}
116
117BreakpointLocationSP
118Breakpoint::GetLocationAtIndex (uint32_t index)
119{
120 return m_locations.GetByIndex(index);
121}
122
123BreakpointLocationSP
124Breakpoint::GetLocationSP (BreakpointLocation *bp_loc_ptr)
125{
126 assert (bp_loc_ptr->GetBreakpoint().GetID() == GetID());
127 return m_locations.FindByID(bp_loc_ptr->GetID());
128}
129
130
131// For each of the overall options we need to decide how they propagate to
132// the location options. This will determine the precedence of options on
133// the breakpoint vrs. its locations.
134
135// Disable at the breakpoint level should override the location settings.
136// That way you can conveniently turn off a whole breakpoint without messing
137// up the individual settings.
138
139void
140Breakpoint::SetEnabled (bool enable)
141{
142 m_options.SetEnabled(enable);
143 if (enable)
144 m_locations.ResolveAllBreakpointSites();
145 else
146 m_locations.ClearAllBreakpointSites();
147}
148
149bool
150Breakpoint::IsEnabled ()
151{
152 return m_options.IsEnabled();
153}
154
155void
Greg Clayton54e7afa2010-07-09 20:39:50 +0000156Breakpoint::SetIgnoreCount (uint32_t n)
Chris Lattner24943d22010-06-08 16:52:24 +0000157{
158 m_options.SetIgnoreCount(n);
159}
160
Greg Clayton54e7afa2010-07-09 20:39:50 +0000161uint32_t
Chris Lattner24943d22010-06-08 16:52:24 +0000162Breakpoint::GetIgnoreCount () const
163{
164 return m_options.GetIgnoreCount();
165}
166
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000167uint32_t
168Breakpoint::GetHitCount () const
169{
170 return m_locations.GetHitCount();
171}
172
Chris Lattner24943d22010-06-08 16:52:24 +0000173void
174Breakpoint::SetThreadID (lldb::tid_t thread_id)
175{
Jim Ingham3c7b5b92010-06-16 02:00:15 +0000176 m_options.GetThreadSpec()->SetTID(thread_id);
Chris Lattner24943d22010-06-08 16:52:24 +0000177}
178
179lldb::tid_t
180Breakpoint::GetThreadID ()
181{
Jim Ingham3c7b5b92010-06-16 02:00:15 +0000182 if (m_options.GetThreadSpec() == NULL)
183 return LLDB_INVALID_THREAD_ID;
184 else
185 return m_options.GetThreadSpec()->GetTID();
Chris Lattner24943d22010-06-08 16:52:24 +0000186}
187
188// This function is used when "baton" doesn't need to be freed
189void
190Breakpoint::SetCallback (BreakpointHitCallback callback, void *baton, bool is_synchronous)
191{
192 // The default "Baton" class will keep a copy of "baton" and won't free
193 // or delete it when it goes goes out of scope.
194 m_options.SetCallback(callback, BatonSP (new Baton(baton)), is_synchronous);
195}
196
197// This function is used when a baton needs to be freed and therefore is
198// contained in a "Baton" subclass.
199void
200Breakpoint::SetCallback (BreakpointHitCallback callback, const BatonSP &callback_baton_sp, bool is_synchronous)
201{
202 m_options.SetCallback(callback, callback_baton_sp, is_synchronous);
203}
204
205void
206Breakpoint::ClearCallback ()
207{
208 m_options.ClearCallback ();
209}
210
211bool
212Breakpoint::InvokeCallback (StoppointCallbackContext *context, break_id_t bp_loc_id)
213{
214 return m_options.InvokeCallback (context, GetID(), bp_loc_id);
215}
216
217BreakpointOptions *
218Breakpoint::GetOptions ()
219{
220 return &m_options;
221}
222
223void
224Breakpoint::ResolveBreakpoint ()
225{
226 if (m_resolver_sp)
227 m_resolver_sp->ResolveBreakpoint(*m_filter_sp);
228}
229
230void
231Breakpoint::ResolveBreakpointInModules (ModuleList &module_list)
232{
233 if (m_resolver_sp)
234 m_resolver_sp->ResolveBreakpointInModules(*m_filter_sp, module_list);
235}
236
237void
238Breakpoint::ClearAllBreakpointSites ()
239{
240 m_locations.ClearAllBreakpointSites();
241}
242
243//----------------------------------------------------------------------
244// ModulesChanged: Pass in a list of new modules, and
245//----------------------------------------------------------------------
246
247void
248Breakpoint::ModulesChanged (ModuleList &module_list, bool load)
249{
250 if (load)
251 {
252 // The logic for handling new modules is:
253 // 1) If the filter rejects this module, then skip it.
254 // 2) Run through the current location list and if there are any locations
255 // for that module, we mark the module as "seen" and we don't try to re-resolve
256 // breakpoint locations for that module.
257 // However, we do add breakpoint sites to these locations if needed.
258 // 3) If we don't see this module in our breakpoint location list, call ResolveInModules.
259
260 ModuleList new_modules; // We'll stuff the "unseen" modules in this list, and then resolve
261 // them after the locations pass. Have to do it this way because
262 // resolving breakpoints will add new locations potentially.
263
Greg Clayton54e7afa2010-07-09 20:39:50 +0000264 for (size_t i = 0; i < module_list.GetSize(); i++)
Chris Lattner24943d22010-06-08 16:52:24 +0000265 {
266 bool seen = false;
267 ModuleSP module_sp (module_list.GetModuleAtIndex (i));
268 Module *module = module_sp.get();
269 if (!m_filter_sp->ModulePasses (module_sp))
270 continue;
271
Greg Clayton54e7afa2010-07-09 20:39:50 +0000272 for (size_t j = 0; j < m_locations.GetSize(); j++)
Chris Lattner24943d22010-06-08 16:52:24 +0000273 {
Greg Clayton54e7afa2010-07-09 20:39:50 +0000274 BreakpointLocationSP break_loc = m_locations.GetByIndex(j);
Chris Lattner24943d22010-06-08 16:52:24 +0000275 const Section *section = break_loc->GetAddress().GetSection();
276 if (section == NULL || section->GetModule() == module)
277 {
278 if (!seen)
279 seen = true;
280
281 if (!break_loc->ResolveBreakpointSite())
282 {
283 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS);
284 if (log)
285 log->Printf ("Warning: could not set breakpoint site for breakpoint location %d of breakpoint %d.\n",
286 break_loc->GetID(), GetID());
287 }
288 }
289 }
290
291 if (!seen)
292 new_modules.AppendInNeeded (module_sp);
293
294 }
295 if (new_modules.GetSize() > 0)
296 {
297 ResolveBreakpointInModules(new_modules);
298 }
299 }
300 else
301 {
302 // Go through the currently set locations and if any have breakpoints in
303 // the module list, then remove their breakpoint sites.
304 // FIXME: Think about this... Maybe it's better to delete the locations?
305 // Are we sure that on load-unload-reload the module pointer will remain
306 // the same? Or do we need to do an equality on modules that is an
307 // "equivalence"???
308
Greg Clayton54e7afa2010-07-09 20:39:50 +0000309 for (size_t i = 0; i < module_list.GetSize(); i++)
Chris Lattner24943d22010-06-08 16:52:24 +0000310 {
311 ModuleSP module_sp (module_list.GetModuleAtIndex (i));
312 if (!m_filter_sp->ModulePasses (module_sp))
313 continue;
314
Greg Clayton54e7afa2010-07-09 20:39:50 +0000315 for (size_t j = 0; j < m_locations.GetSize(); j++)
Chris Lattner24943d22010-06-08 16:52:24 +0000316 {
Greg Clayton54e7afa2010-07-09 20:39:50 +0000317 BreakpointLocationSP break_loc = m_locations.GetByIndex(j);
Chris Lattner24943d22010-06-08 16:52:24 +0000318 const Section *section = break_loc->GetAddress().GetSection();
319 if (section)
320 {
321 if (section->GetModule() == module_sp.get())
322 break_loc->ClearBreakpointSite();
323 }
324// else
325// {
326// Address temp_addr;
327// if (module->ResolveLoadAddress(break_loc->GetLoadAddress(), m_target->GetProcess(), temp_addr))
328// break_loc->ClearBreakpointSite();
329// }
330 }
331 }
332 }
333}
334
335void
336Breakpoint::Dump (Stream *)
337{
338}
339
340size_t
341Breakpoint::GetNumResolvedLocations() const
342{
343 // Return the number of breakpoints that are actually resolved and set
344 // down in the inferior process.
345 return m_locations.GetNumResolvedLocations();
346}
347
348size_t
349Breakpoint::GetNumLocations() const
350{
351 return m_locations.GetSize();
352}
353
354void
355Breakpoint::GetDescription (Stream *s, lldb::DescriptionLevel level, bool show_locations)
356{
357 assert (s != NULL);
Greg Clayton12bec712010-06-28 21:30:43 +0000358 s->Printf("%i: ", GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000359 GetResolverDescription (s);
Greg Clayton12bec712010-06-28 21:30:43 +0000360 GetFilterDescription (s);
Chris Lattner24943d22010-06-08 16:52:24 +0000361
Greg Clayton54e7afa2010-07-09 20:39:50 +0000362 const size_t num_locations = GetNumLocations ();
363 const size_t num_resolved_locations = GetNumResolvedLocations ();
Chris Lattner24943d22010-06-08 16:52:24 +0000364
365 switch (level)
366 {
367 case lldb::eDescriptionLevelBrief:
368 case lldb::eDescriptionLevelFull:
369 if (num_locations > 0)
370 {
Greg Clayton54e7afa2010-07-09 20:39:50 +0000371 s->Printf(", locations = %zu", num_locations);
Chris Lattner24943d22010-06-08 16:52:24 +0000372 if (num_resolved_locations > 0)
Greg Clayton54e7afa2010-07-09 20:39:50 +0000373 s->Printf(", resolved = %zu", num_resolved_locations);
Chris Lattner24943d22010-06-08 16:52:24 +0000374 }
375 else
376 {
Greg Clayton12bec712010-06-28 21:30:43 +0000377 s->Printf(", locations = 0 (pending)");
Chris Lattner24943d22010-06-08 16:52:24 +0000378 }
379
Jim Ingham649492b2010-06-18 01:00:58 +0000380 GetOptions()->GetDescription(s, level);
381
Chris Lattner24943d22010-06-08 16:52:24 +0000382 if (level == lldb::eDescriptionLevelFull)
383 {
Jim Ingham649492b2010-06-18 01:00:58 +0000384 s->IndentLess();
385 s->EOL();
Chris Lattner24943d22010-06-08 16:52:24 +0000386 }
387 break;
388
389 case lldb::eDescriptionLevelVerbose:
390 // Verbose mode does a debug dump of the breakpoint
391 Dump (s);
Jim Ingham649492b2010-06-18 01:00:58 +0000392 s->EOL ();
393 s->Indent();
394 GetOptions()->GetDescription(s, level);
Chris Lattner24943d22010-06-08 16:52:24 +0000395 break;
Greg Clayton54e7afa2010-07-09 20:39:50 +0000396
397 default:
398 break;
Chris Lattner24943d22010-06-08 16:52:24 +0000399 }
400
401 if (show_locations)
402 {
Chris Lattner24943d22010-06-08 16:52:24 +0000403 s->IndentMore();
Greg Clayton54e7afa2010-07-09 20:39:50 +0000404 for (size_t i = 0; i < num_locations; ++i)
Chris Lattner24943d22010-06-08 16:52:24 +0000405 {
406 BreakpointLocation *loc = GetLocationAtIndex(i).get();
407 loc->GetDescription(s, level);
408 s->EOL();
409 }
410 s->IndentLess();
Chris Lattner24943d22010-06-08 16:52:24 +0000411 }
412}
413
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000414Breakpoint::BreakpointEventData::BreakpointEventData (BreakpointEventType sub_type,
Jim Ingham649492b2010-06-18 01:00:58 +0000415 BreakpointSP &new_breakpoint_sp) :
Chris Lattner24943d22010-06-08 16:52:24 +0000416 EventData (),
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000417 m_breakpoint_event (sub_type),
Chris Lattner24943d22010-06-08 16:52:24 +0000418 m_new_breakpoint_sp (new_breakpoint_sp)
419{
420}
421
422Breakpoint::BreakpointEventData::~BreakpointEventData ()
423{
424}
425
426const ConstString &
427Breakpoint::BreakpointEventData::GetFlavorString ()
428{
429 static ConstString g_flavor ("Breakpoint::BreakpointEventData");
430 return g_flavor;
431}
432
433const ConstString &
434Breakpoint::BreakpointEventData::GetFlavor () const
435{
436 return BreakpointEventData::GetFlavorString ();
437}
438
439
440BreakpointSP &
441Breakpoint::BreakpointEventData::GetBreakpoint ()
442{
443 return m_new_breakpoint_sp;
444}
445
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000446BreakpointEventType
447Breakpoint::BreakpointEventData::GetBreakpointEventType () const
Chris Lattner24943d22010-06-08 16:52:24 +0000448{
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000449 return m_breakpoint_event;
Chris Lattner24943d22010-06-08 16:52:24 +0000450}
451
452void
453Breakpoint::BreakpointEventData::Dump (Stream *s) const
454{
455}
456
457Breakpoint::BreakpointEventData *
458Breakpoint::BreakpointEventData::GetEventDataFromEvent (const EventSP &event_sp)
459{
460 if (event_sp)
461 {
462 EventData *event_data = event_sp->GetData();
463 if (event_data && event_data->GetFlavor() == BreakpointEventData::GetFlavorString())
464 return static_cast <BreakpointEventData *> (event_sp->GetData());
465 }
466 return NULL;
467}
468
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000469BreakpointEventType
470Breakpoint::BreakpointEventData::GetBreakpointEventTypeFromEvent (const EventSP &event_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000471{
472 BreakpointEventData *data = GetEventDataFromEvent (event_sp);
473
474 if (data == NULL)
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000475 return eBreakpointEventTypeInvalidType;
Chris Lattner24943d22010-06-08 16:52:24 +0000476 else
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000477 return data->GetBreakpointEventType();
Chris Lattner24943d22010-06-08 16:52:24 +0000478}
479
480BreakpointSP
481Breakpoint::BreakpointEventData::GetBreakpointFromEvent (const EventSP &event_sp)
482{
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000483 BreakpointSP bp_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000484
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000485 BreakpointEventData *data = GetEventDataFromEvent (event_sp);
486 if (data)
487 bp_sp = data->GetBreakpoint();
488
489 return bp_sp;
490}
491
492lldb::BreakpointLocationSP
493Breakpoint::BreakpointEventData::GetBreakpointLocationAtIndexFromEvent (const lldb::EventSP &event_sp, uint32_t bp_loc_idx)
494{
495 lldb::BreakpointLocationSP bp_loc_sp;
496
497 BreakpointEventData *data = GetEventDataFromEvent (event_sp);
498 if (data)
Chris Lattner24943d22010-06-08 16:52:24 +0000499 {
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000500 Breakpoint *bp = data->GetBreakpoint().get();
501 if (bp)
502 bp_loc_sp = bp->GetLocationAtIndex(bp_loc_idx);
Chris Lattner24943d22010-06-08 16:52:24 +0000503 }
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000504
505 return bp_loc_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000506}
507
508
509void
510Breakpoint::GetResolverDescription (Stream *s)
511{
512 if (m_resolver_sp)
513 m_resolver_sp->GetDescription (s);
514}
515
516void
517Breakpoint::GetFilterDescription (Stream *s)
518{
519 m_filter_sp->GetDescription (s);
520}
521
522const BreakpointSP
523Breakpoint::GetSP ()
524{
525 return m_target.GetBreakpointList().FindBreakpointByID (GetID());
526}