1 //===-- BreakpointLocationCollection.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 #include "lldb/Breakpoint/BreakpointLocationCollection.h"
16 #include "lldb/Core/ModuleList.h"
17 #include "lldb/Breakpoint/BreakpointLocation.h"
18 #include "lldb/Breakpoint/BreakpointLocationList.h"
19 
20 using namespace lldb;
21 using namespace lldb_private;
22 
23 //----------------------------------------------------------------------
24 // BreakpointLocationCollection constructor
25 //----------------------------------------------------------------------
26 BreakpointLocationCollection::BreakpointLocationCollection() :
27     m_break_loc_collection()
28 {
29 }
30 
31 //----------------------------------------------------------------------
32 // Destructor
33 //----------------------------------------------------------------------
34 BreakpointLocationCollection::~BreakpointLocationCollection()
35 {
36 }
37 
38 void
39 BreakpointLocationCollection::Add(const BreakpointLocationSP &bp_loc)
40 {
41     BreakpointLocationSP old_bp_loc = FindByIDPair (bp_loc->GetBreakpoint().GetID(), bp_loc->GetID());
42     if (!old_bp_loc.get())
43         m_break_loc_collection.push_back(bp_loc);
44 }
45 
46 bool
47 BreakpointLocationCollection::Remove (lldb::user_id_t bp_id, lldb::user_id_t bp_loc_id)
48 {
49     collection::iterator pos = GetIDPairIterator(bp_id, bp_loc_id);    // Predicate
50     if (pos != m_break_loc_collection.end())
51     {
52         m_break_loc_collection.erase(pos);
53         return true;
54     }
55     return false;
56 
57 }
58 
59 class BreakpointIDPairMatches
60 {
61 public:
62     BreakpointIDPairMatches (lldb::user_id_t break_id, lldb::user_id_t break_loc_id) :
63         m_break_id(break_id),
64         m_break_loc_id (break_loc_id)
65     {
66     }
67 
68     bool operator() (const BreakpointLocationSP &bp_loc) const
69     {
70         return m_break_id == bp_loc->GetBreakpoint().GetID()
71             && m_break_loc_id == bp_loc->GetID();
72     }
73 
74 private:
75    const lldb::user_id_t m_break_id;
76    const lldb::user_id_t m_break_loc_id;
77 };
78 
79 BreakpointLocationCollection::collection::iterator
80 BreakpointLocationCollection::GetIDPairIterator (lldb::user_id_t break_id, lldb::user_id_t break_loc_id)
81 {
82     return std::find_if(m_break_loc_collection.begin(), m_break_loc_collection.end(),   // Search full range
83                         BreakpointIDPairMatches(break_id, break_loc_id));               // Predicate
84 }
85 
86 BreakpointLocationCollection::collection::const_iterator
87 BreakpointLocationCollection::GetIDPairConstIterator (lldb::user_id_t break_id, lldb::user_id_t break_loc_id) const
88 {
89     return std::find_if(m_break_loc_collection.begin(), m_break_loc_collection.end(),   // Search full range
90                         BreakpointIDPairMatches(break_id, break_loc_id));               // Predicate
91 }
92 
93 BreakpointLocationSP
94 BreakpointLocationCollection::FindByIDPair (lldb::user_id_t break_id, lldb::user_id_t break_loc_id)
95 {
96     BreakpointLocationSP stop_sp;
97     collection::iterator pos = GetIDPairIterator(break_id, break_loc_id);
98     if (pos != m_break_loc_collection.end())
99         stop_sp = *pos;
100 
101     return stop_sp;
102 }
103 
104 const BreakpointLocationSP
105 BreakpointLocationCollection::FindByIDPair (lldb::user_id_t break_id, lldb::user_id_t break_loc_id) const
106 {
107     BreakpointLocationSP stop_sp;
108     collection::const_iterator pos = GetIDPairConstIterator(break_id, break_loc_id);
109     if (pos != m_break_loc_collection.end())
110         stop_sp = *pos;
111 
112     return stop_sp;
113 }
114 
115 BreakpointLocationSP
116 BreakpointLocationCollection::GetByIndex (uint32_t i)
117 {
118     BreakpointLocationSP stop_sp;
119     if (i >= 0 && i < m_break_loc_collection.size())
120         stop_sp = m_break_loc_collection[i];
121 
122     return stop_sp;
123 }
124 
125 const BreakpointLocationSP
126 BreakpointLocationCollection::GetByIndex (uint32_t i) const
127 {
128     BreakpointLocationSP stop_sp;
129     if (i >= 0 && i < m_break_loc_collection.size())
130         stop_sp = m_break_loc_collection[i];
131 
132     return stop_sp;
133 }
134 
135 bool
136 BreakpointLocationCollection::ShouldStop (StoppointCallbackContext *context)
137 {
138     bool shouldStop = false;
139 
140     for (int i = 0; i < GetSize(); i++) {
141         bool one_result = GetByIndex(i)->ShouldStop(context);
142         if (one_result)
143             shouldStop = true;
144     }
145     return shouldStop;
146 }
147 
148 void
149 BreakpointLocationCollection::GetDescription (Stream *s, lldb::DescriptionLevel level)
150 {
151     collection::iterator pos,
152         begin = m_break_loc_collection.begin(),
153         end = m_break_loc_collection.end();
154 
155     for (pos = begin; pos != end; ++pos)
156     {
157         if (pos != begin)
158             s->PutChar(' ');
159         (*pos)->GetDescription(s, level);
160     }
161 }
162