blob: 9a5ee92f37b8494f0bc8a35361a16ce388257e59 [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +00001//===-- BreakpointSiteList.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#include "lldb/Breakpoint/BreakpointSiteList.h"
11
12// C Includes
13// C++ Includes
14// Other libraries and framework includes
15// Project includes
16#include "lldb/Core/Stream.h"
Eli Friedman10ff00e2010-06-09 07:47:43 +000017#include <algorithm>
Chris Lattner24943d22010-06-08 16:52:24 +000018
19using namespace lldb;
20using namespace lldb_private;
21
22BreakpointSiteList::BreakpointSiteList() :
23 m_bp_site_list()
24{
25}
26
27BreakpointSiteList::~BreakpointSiteList()
28{
29}
30
31// Add breakpoint site to the list. However, if the element already exists in the
32// list, then we don't add it, and return LLDB_INVALID_BREAK_ID.
33
Greg Clayton54e7afa2010-07-09 20:39:50 +000034lldb::break_id_t
Chris Lattner24943d22010-06-08 16:52:24 +000035BreakpointSiteList::Add(const BreakpointSiteSP &bp)
36{
37 lldb::addr_t bp_site_load_addr = bp->GetLoadAddress();
38 collection::iterator iter = m_bp_site_list.find (bp_site_load_addr);
39
40 if (iter == m_bp_site_list.end())
41 {
42 m_bp_site_list.insert (iter, collection::value_type (bp_site_load_addr, bp));
43 return bp->GetID();
44 }
45 else
46 {
47 return LLDB_INVALID_BREAK_ID;
48 }
49}
50
51bool
Greg Clayton54e7afa2010-07-09 20:39:50 +000052BreakpointSiteList::ShouldStop (StoppointCallbackContext *context, lldb::break_id_t break_id)
Chris Lattner24943d22010-06-08 16:52:24 +000053{
54 BreakpointSiteSP bp = FindByID (break_id);
55 if (bp)
56 {
57 // Let the BreakpointSite decide if it should stop here (could not have
58 // reached it's target hit count yet, or it could have a callback
59 // that decided it shouldn't stop (shared library loads/unloads).
60 return bp->ShouldStop (context);
61 }
62 // We should stop here since this BreakpointSite isn't valid anymore or it
63 // doesn't exist.
64 return true;
65}
Greg Clayton54e7afa2010-07-09 20:39:50 +000066lldb::break_id_t
Chris Lattner24943d22010-06-08 16:52:24 +000067BreakpointSiteList::FindIDByAddress (lldb::addr_t addr)
68{
69 BreakpointSiteSP bp = FindByAddress (addr);
70 if (bp)
71 {
72 //DBLogIf(PD_LOG_BREAKPOINTS, "BreakpointSiteList::%s ( addr = 0x%8.8llx ) => %u", __FUNCTION__, (uint64_t)addr, bp->GetID());
73 return bp.get()->GetID();
74 }
75 //DBLogIf(PD_LOG_BREAKPOINTS, "BreakpointSiteList::%s ( addr = 0x%8.8llx ) => NONE", __FUNCTION__, (uint64_t)addr);
76 return LLDB_INVALID_BREAK_ID;
77}
78
79bool
Greg Clayton54e7afa2010-07-09 20:39:50 +000080BreakpointSiteList::Remove (lldb::break_id_t break_id)
Chris Lattner24943d22010-06-08 16:52:24 +000081{
82 collection::iterator pos = GetIDIterator(break_id); // Predicate
83 if (pos != m_bp_site_list.end())
84 {
85 m_bp_site_list.erase(pos);
86 return true;
87 }
88 return false;
89}
90
91bool
92BreakpointSiteList::RemoveByAddress (lldb::addr_t address)
93{
94 collection::iterator pos = m_bp_site_list.find(address);
95 if (pos != m_bp_site_list.end())
96 {
97 m_bp_site_list.erase(pos);
98 return true;
99 }
100 return false;
101}
102
103class BreakpointSiteIDMatches
104{
105public:
Greg Clayton54e7afa2010-07-09 20:39:50 +0000106 BreakpointSiteIDMatches (lldb::break_id_t break_id) :
Chris Lattner24943d22010-06-08 16:52:24 +0000107 m_break_id(break_id)
108 {
109 }
110
111 bool operator() (std::pair <lldb::addr_t, BreakpointSiteSP> val_pair) const
112 {
113 return m_break_id == val_pair.second.get()->GetID();
114 }
115
116private:
Greg Clayton54e7afa2010-07-09 20:39:50 +0000117 const lldb::break_id_t m_break_id;
Chris Lattner24943d22010-06-08 16:52:24 +0000118};
119
120BreakpointSiteList::collection::iterator
Greg Clayton54e7afa2010-07-09 20:39:50 +0000121BreakpointSiteList::GetIDIterator (lldb::break_id_t break_id)
Chris Lattner24943d22010-06-08 16:52:24 +0000122{
123 return std::find_if(m_bp_site_list.begin(), m_bp_site_list.end(), // Search full range
124 BreakpointSiteIDMatches(break_id)); // Predicate
125}
126
127BreakpointSiteList::collection::const_iterator
Greg Clayton54e7afa2010-07-09 20:39:50 +0000128BreakpointSiteList::GetIDConstIterator (lldb::break_id_t break_id) const
Chris Lattner24943d22010-06-08 16:52:24 +0000129{
130 return std::find_if(m_bp_site_list.begin(), m_bp_site_list.end(), // Search full range
131 BreakpointSiteIDMatches(break_id)); // Predicate
132}
133
134BreakpointSiteSP
Greg Clayton54e7afa2010-07-09 20:39:50 +0000135BreakpointSiteList::FindByID (lldb::break_id_t break_id)
Chris Lattner24943d22010-06-08 16:52:24 +0000136{
137 BreakpointSiteSP stop_sp;
138 collection::iterator pos = GetIDIterator(break_id);
139 if (pos != m_bp_site_list.end())
140 stop_sp = pos->second;
141
142 return stop_sp;
143}
144
145const BreakpointSiteSP
Greg Clayton54e7afa2010-07-09 20:39:50 +0000146BreakpointSiteList::FindByID (lldb::break_id_t break_id) const
Chris Lattner24943d22010-06-08 16:52:24 +0000147{
148 BreakpointSiteSP stop_sp;
149 collection::const_iterator pos = GetIDConstIterator(break_id);
150 if (pos != m_bp_site_list.end())
151 stop_sp = pos->second;
152
153 return stop_sp;
154}
155
156BreakpointSiteSP
157BreakpointSiteList::FindByAddress (lldb::addr_t addr)
158{
159 BreakpointSiteSP found_sp;
160
161 collection::iterator iter = m_bp_site_list.find(addr);
162 if (iter != m_bp_site_list.end())
163 found_sp = iter->second;
164 return found_sp;
165}
166
167void
168BreakpointSiteList::Dump (Stream *s) const
169{
170 s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
171 s->Indent();
172 s->Printf("BreakpointSiteList with %u BreakpointSites:\n", (uint32_t)m_bp_site_list.size());
173 s->IndentMore();
174 collection::const_iterator pos;
175 collection::const_iterator end = m_bp_site_list.end();
176 for (pos = m_bp_site_list.begin(); pos != end; ++pos)
177 pos->second.get()->Dump(s);
178 s->IndentLess();
179}
180
181
182BreakpointSiteSP
183BreakpointSiteList::GetByIndex (uint32_t i)
184{
185 BreakpointSiteSP stop_sp;
186 collection::iterator end = m_bp_site_list.end();
187 collection::iterator pos;
188 uint32_t curr_i = 0;
189 for (pos = m_bp_site_list.begin(), curr_i = 0; pos != end; ++pos, ++curr_i)
190 {
191 if (curr_i == i)
192 stop_sp = pos->second;
193 }
194 return stop_sp;
195}
196
197const BreakpointSiteSP
198BreakpointSiteList::GetByIndex (uint32_t i) const
199{
200 BreakpointSiteSP stop_sp;
201 collection::const_iterator end = m_bp_site_list.end();
202 collection::const_iterator pos;
203 uint32_t curr_i = 0;
204 for (pos = m_bp_site_list.begin(), curr_i = 0; pos != end; ++pos, ++curr_i)
205 {
206 if (curr_i == i)
207 stop_sp = pos->second;
208 }
209 return stop_sp;
210}
211
212void
Greg Clayton54e7afa2010-07-09 20:39:50 +0000213BreakpointSiteList::SetEnabledForAll (const bool enabled, const lldb::break_id_t except_id)
Chris Lattner24943d22010-06-08 16:52:24 +0000214{
215 collection::iterator end = m_bp_site_list.end();
216 collection::iterator pos;
217 for (pos = m_bp_site_list.begin(); pos != end; ++pos)
218 {
219 if (except_id != LLDB_INVALID_BREAK_ID && except_id != pos->second->GetID())
220 pos->second->SetEnabled (enabled);
221 else
222 pos->second->SetEnabled (!enabled);
223 }
224}
225
226const BreakpointSiteList::collection *
227BreakpointSiteList::GetMap ()
228{
229 return &m_bp_site_list;
230}