BreakpointList.cpp revision 258884
1254721Semaste//===-- BreakpointList.cpp --------------------------------------*- C++ -*-===//
2254721Semaste//
3254721Semaste//                     The LLVM Compiler Infrastructure
4254721Semaste//
5254721Semaste// This file is distributed under the University of Illinois Open Source
6254721Semaste// License. See LICENSE.TXT for details.
7254721Semaste//
8254721Semaste//===----------------------------------------------------------------------===//
9254721Semaste
10254721Semaste#include "lldb/Breakpoint/BreakpointList.h"
11254721Semaste
12254721Semaste// C Includes
13254721Semaste// C++ Includes
14254721Semaste// Other libraries and framework includes
15254721Semaste// Project includes
16254721Semaste#include "lldb/Target/Target.h"
17254721Semaste
18254721Semasteusing namespace lldb;
19254721Semasteusing namespace lldb_private;
20254721Semaste
21254721SemasteBreakpointList::BreakpointList (bool is_internal) :
22254721Semaste    m_mutex (Mutex::eMutexTypeRecursive),
23254721Semaste    m_breakpoints(),
24254721Semaste    m_next_break_id (0),
25254721Semaste    m_is_internal (is_internal)
26254721Semaste{
27254721Semaste}
28254721Semaste
29254721SemasteBreakpointList::~BreakpointList()
30254721Semaste{
31254721Semaste}
32254721Semaste
33254721Semaste
34254721Semastebreak_id_t
35254721SemasteBreakpointList::Add (BreakpointSP &bp_sp, bool notify)
36254721Semaste{
37254721Semaste    Mutex::Locker locker(m_mutex);
38254721Semaste    // Internal breakpoint IDs are negative, normal ones are positive
39254721Semaste    bp_sp->SetID (m_is_internal ? --m_next_break_id : ++m_next_break_id);
40254721Semaste
41254721Semaste    m_breakpoints.push_back(bp_sp);
42254721Semaste    if (notify)
43254721Semaste    {
44254721Semaste        if (bp_sp->GetTarget().EventTypeHasListeners(Target::eBroadcastBitBreakpointChanged))
45254721Semaste            bp_sp->GetTarget().BroadcastEvent (Target::eBroadcastBitBreakpointChanged,
46254721Semaste                                               new Breakpoint::BreakpointEventData (eBreakpointEventTypeAdded, bp_sp));
47254721Semaste    }
48254721Semaste    return bp_sp->GetID();
49254721Semaste}
50254721Semaste
51254721Semastebool
52254721SemasteBreakpointList::Remove (break_id_t break_id, bool notify)
53254721Semaste{
54254721Semaste    Mutex::Locker locker(m_mutex);
55254721Semaste    bp_collection::iterator pos = GetBreakpointIDIterator(break_id);    // Predicate
56254721Semaste    if (pos != m_breakpoints.end())
57254721Semaste    {
58254721Semaste        BreakpointSP bp_sp (*pos);
59254721Semaste        m_breakpoints.erase(pos);
60254721Semaste        if (notify)
61254721Semaste        {
62254721Semaste            if (bp_sp->GetTarget().EventTypeHasListeners(Target::eBroadcastBitBreakpointChanged))
63254721Semaste                bp_sp->GetTarget().BroadcastEvent (Target::eBroadcastBitBreakpointChanged,
64254721Semaste                                                   new Breakpoint::BreakpointEventData (eBreakpointEventTypeRemoved, bp_sp));
65254721Semaste        }
66254721Semaste        return true;
67254721Semaste    }
68254721Semaste    return false;
69254721Semaste}
70254721Semaste
71254721Semastevoid
72258884SemasteBreakpointList::RemoveInvalidLocations (const ArchSpec &arch)
73258884Semaste{
74258884Semaste    Mutex::Locker locker(m_mutex);
75258884Semaste    for (const auto &bp_sp : m_breakpoints)
76258884Semaste        bp_sp->RemoveInvalidLocations(arch);
77258884Semaste}
78258884Semaste
79258884Semaste
80258884Semastevoid
81254721SemasteBreakpointList::SetEnabledAll (bool enabled)
82254721Semaste{
83254721Semaste    Mutex::Locker locker(m_mutex);
84258884Semaste    for (const auto &bp_sp : m_breakpoints)
85258884Semaste        bp_sp->SetEnabled (enabled);
86254721Semaste}
87254721Semaste
88254721Semaste
89254721Semastevoid
90254721SemasteBreakpointList::RemoveAll (bool notify)
91254721Semaste{
92254721Semaste    Mutex::Locker locker(m_mutex);
93254721Semaste    ClearAllBreakpointSites ();
94254721Semaste
95254721Semaste    if (notify)
96254721Semaste    {
97254721Semaste        bp_collection::iterator pos, end = m_breakpoints.end();
98254721Semaste        for (pos = m_breakpoints.begin(); pos != end; ++pos)
99254721Semaste        {
100254721Semaste            if ((*pos)->GetTarget().EventTypeHasListeners(Target::eBroadcastBitBreakpointChanged))
101254721Semaste            {
102254721Semaste                (*pos)->GetTarget().BroadcastEvent (Target::eBroadcastBitBreakpointChanged,
103254721Semaste                                                    new Breakpoint::BreakpointEventData (eBreakpointEventTypeRemoved,
104254721Semaste                                                                                         *pos));
105254721Semaste            }
106254721Semaste        }
107254721Semaste    }
108254721Semaste    m_breakpoints.erase (m_breakpoints.begin(), m_breakpoints.end());
109254721Semaste}
110254721Semaste
111254721Semasteclass BreakpointIDMatches
112254721Semaste{
113254721Semastepublic:
114254721Semaste    BreakpointIDMatches (break_id_t break_id) :
115254721Semaste        m_break_id(break_id)
116254721Semaste    {
117254721Semaste    }
118254721Semaste
119254721Semaste    bool operator() (const BreakpointSP &bp) const
120254721Semaste    {
121254721Semaste        return m_break_id == bp->GetID();
122254721Semaste    }
123254721Semaste
124254721Semasteprivate:
125254721Semaste   const break_id_t m_break_id;
126254721Semaste};
127254721Semaste
128254721SemasteBreakpointList::bp_collection::iterator
129254721SemasteBreakpointList::GetBreakpointIDIterator (break_id_t break_id)
130254721Semaste{
131254721Semaste    return std::find_if(m_breakpoints.begin(), m_breakpoints.end(), // Search full range
132254721Semaste                        BreakpointIDMatches(break_id));             // Predicate
133254721Semaste}
134254721Semaste
135254721SemasteBreakpointList::bp_collection::const_iterator
136254721SemasteBreakpointList::GetBreakpointIDConstIterator (break_id_t break_id) const
137254721Semaste{
138254721Semaste    return std::find_if(m_breakpoints.begin(), m_breakpoints.end(), // Search full range
139254721Semaste                        BreakpointIDMatches(break_id));             // Predicate
140254721Semaste}
141254721Semaste
142254721SemasteBreakpointSP
143254721SemasteBreakpointList::FindBreakpointByID (break_id_t break_id)
144254721Semaste{
145254721Semaste    Mutex::Locker locker(m_mutex);
146254721Semaste    BreakpointSP stop_sp;
147254721Semaste    bp_collection::iterator pos = GetBreakpointIDIterator(break_id);
148254721Semaste    if (pos != m_breakpoints.end())
149254721Semaste        stop_sp = *pos;
150254721Semaste
151254721Semaste    return stop_sp;
152254721Semaste}
153254721Semaste
154254721Semasteconst BreakpointSP
155254721SemasteBreakpointList::FindBreakpointByID (break_id_t break_id) const
156254721Semaste{
157254721Semaste    Mutex::Locker locker(m_mutex);
158254721Semaste    BreakpointSP stop_sp;
159254721Semaste    bp_collection::const_iterator pos = GetBreakpointIDConstIterator(break_id);
160254721Semaste    if (pos != m_breakpoints.end())
161254721Semaste        stop_sp = *pos;
162254721Semaste
163254721Semaste    return stop_sp;
164254721Semaste}
165254721Semaste
166254721Semastevoid
167254721SemasteBreakpointList::Dump (Stream *s) const
168254721Semaste{
169254721Semaste    Mutex::Locker locker(m_mutex);
170254721Semaste    s->Printf("%p: ", this);
171254721Semaste    s->Indent();
172254721Semaste    s->Printf("BreakpointList with %u Breakpoints:\n", (uint32_t)m_breakpoints.size());
173254721Semaste    s->IndentMore();
174258884Semaste    for (const auto &bp_sp : m_breakpoints)
175258884Semaste        bp_sp->Dump(s);
176254721Semaste    s->IndentLess();
177254721Semaste}
178254721Semaste
179254721Semaste
180254721SemasteBreakpointSP
181254721SemasteBreakpointList::GetBreakpointAtIndex (size_t i)
182254721Semaste{
183254721Semaste    Mutex::Locker locker(m_mutex);
184254721Semaste    BreakpointSP stop_sp;
185254721Semaste    bp_collection::iterator end = m_breakpoints.end();
186254721Semaste    bp_collection::iterator pos;
187254721Semaste    size_t curr_i = 0;
188254721Semaste    for (pos = m_breakpoints.begin(), curr_i = 0; pos != end; ++pos, ++curr_i)
189254721Semaste    {
190254721Semaste        if (curr_i == i)
191254721Semaste            stop_sp = *pos;
192254721Semaste    }
193254721Semaste    return stop_sp;
194254721Semaste}
195254721Semaste
196254721Semasteconst BreakpointSP
197254721SemasteBreakpointList::GetBreakpointAtIndex (size_t i) const
198254721Semaste{
199254721Semaste    Mutex::Locker locker(m_mutex);
200254721Semaste    BreakpointSP stop_sp;
201254721Semaste    bp_collection::const_iterator end = m_breakpoints.end();
202254721Semaste    bp_collection::const_iterator pos;
203254721Semaste    size_t curr_i = 0;
204254721Semaste    for (pos = m_breakpoints.begin(), curr_i = 0; pos != end; ++pos, ++curr_i)
205254721Semaste    {
206254721Semaste        if (curr_i == i)
207254721Semaste            stop_sp = *pos;
208254721Semaste    }
209254721Semaste    return stop_sp;
210254721Semaste}
211254721Semaste
212254721Semastevoid
213258054SemasteBreakpointList::UpdateBreakpoints (ModuleList& module_list, bool added, bool delete_locations)
214254721Semaste{
215254721Semaste    Mutex::Locker locker(m_mutex);
216258884Semaste    for (const auto &bp_sp : m_breakpoints)
217258884Semaste        bp_sp->ModulesChanged (module_list, added, delete_locations);
218254721Semaste
219254721Semaste}
220254721Semaste
221254721Semastevoid
222254721SemasteBreakpointList::UpdateBreakpointsWhenModuleIsReplaced (ModuleSP old_module_sp, ModuleSP new_module_sp)
223254721Semaste{
224254721Semaste    Mutex::Locker locker(m_mutex);
225258884Semaste    for (const auto &bp_sp : m_breakpoints)
226258884Semaste        bp_sp->ModuleReplaced (old_module_sp, new_module_sp);
227254721Semaste
228254721Semaste}
229254721Semaste
230254721Semastevoid
231254721SemasteBreakpointList::ClearAllBreakpointSites ()
232254721Semaste{
233254721Semaste    Mutex::Locker locker(m_mutex);
234258884Semaste    for (const auto &bp_sp : m_breakpoints)
235258884Semaste        bp_sp->ClearAllBreakpointSites ();
236254721Semaste
237254721Semaste}
238254721Semaste
239254721Semastevoid
240254721SemasteBreakpointList::GetListMutex (Mutex::Locker &locker)
241254721Semaste{
242254721Semaste    return locker.Lock (m_mutex);
243254721Semaste}
244