summaryrefslogtreecommitdiffstats
path: root/lldb/source/Breakpoint/BreakpointSiteList.cpp
blob: 76ea5b4c7acbd6c213e927c5909cac6fbdf005d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
//===-- BreakpointSiteList.cpp ----------------------------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include "lldb/Breakpoint/BreakpointSiteList.h"

// C Includes
// C++ Includes
// Other libraries and framework includes
// Project includes
#include "lldb/Core/Stream.h"
#include <algorithm>

using namespace lldb;
using namespace lldb_private;

BreakpointSiteList::BreakpointSiteList() :
    m_bp_site_list()
{
}

BreakpointSiteList::~BreakpointSiteList()
{
}

// Add breakpoint site to the list.  However, if the element already exists in the
// list, then we don't add it, and return LLDB_INVALID_BREAK_ID.

lldb::break_id_t
BreakpointSiteList::Add(const BreakpointSiteSP &bp)
{
    lldb::addr_t bp_site_load_addr = bp->GetLoadAddress();
    collection::iterator iter = m_bp_site_list.find (bp_site_load_addr);

    if (iter == m_bp_site_list.end())
    {
        m_bp_site_list.insert (iter, collection::value_type (bp_site_load_addr, bp));
        return bp->GetID();
    }
    else
    {
        return LLDB_INVALID_BREAK_ID;
    }
}

bool
BreakpointSiteList::ShouldStop (StoppointCallbackContext *context, lldb::break_id_t site_id)
{
    BreakpointSiteSP site_sp (FindByID (site_id));
    if (site_sp)
    {
        // Let the BreakpointSite decide if it should stop here (could not have
        // reached it's target hit count yet, or it could have a callback
        // that decided it shouldn't stop (shared library loads/unloads).
        return site_sp->ShouldStop (context);
    }
    // We should stop here since this BreakpointSite isn't valid anymore or it
    // doesn't exist.
    return true;
}
lldb::break_id_t
BreakpointSiteList::FindIDByAddress (lldb::addr_t addr)
{
    BreakpointSiteSP bp = FindByAddress (addr);
    if (bp)
    {
        //DBLogIf(PD_LOG_BREAKPOINTS, "BreakpointSiteList::%s ( addr = 0x%8.8" PRIx64 " ) => %u", __FUNCTION__, (uint64_t)addr, bp->GetID());
        return bp.get()->GetID();
    }
    //DBLogIf(PD_LOG_BREAKPOINTS, "BreakpointSiteList::%s ( addr = 0x%8.8" PRIx64 " ) => NONE", __FUNCTION__, (uint64_t)addr);
    return LLDB_INVALID_BREAK_ID;
}

bool
BreakpointSiteList::Remove (lldb::break_id_t break_id)
{
    collection::iterator pos = GetIDIterator(break_id);    // Predicate
    if (pos != m_bp_site_list.end())
    {
        m_bp_site_list.erase(pos);
        return true;
    }
    return false;
}

bool
BreakpointSiteList::RemoveByAddress (lldb::addr_t address)
{
    collection::iterator pos =  m_bp_site_list.find(address);
    if (pos != m_bp_site_list.end())
    {
        m_bp_site_list.erase(pos);
        return true;
    }
    return false;
}

class BreakpointSiteIDMatches
{
public:
    BreakpointSiteIDMatches (lldb::break_id_t break_id) :
        m_break_id(break_id)
    {
    }

    bool operator() (std::pair <lldb::addr_t, BreakpointSiteSP> val_pair) const
    {
        return m_break_id == val_pair.second.get()->GetID();
    }

private:
   const lldb::break_id_t m_break_id;
};

BreakpointSiteList::collection::iterator
BreakpointSiteList::GetIDIterator (lldb::break_id_t break_id)
{
    return std::find_if(m_bp_site_list.begin(), m_bp_site_list.end(),   // Search full range
                        BreakpointSiteIDMatches(break_id));             // Predicate
}

BreakpointSiteList::collection::const_iterator
BreakpointSiteList::GetIDConstIterator (lldb::break_id_t break_id) const
{
    return std::find_if(m_bp_site_list.begin(), m_bp_site_list.end(),   // Search full range
                        BreakpointSiteIDMatches(break_id));             // Predicate
}

BreakpointSiteSP
BreakpointSiteList::FindByID (lldb::break_id_t break_id)
{
    BreakpointSiteSP stop_sp;
    collection::iterator pos = GetIDIterator(break_id);
    if (pos != m_bp_site_list.end())
        stop_sp = pos->second;

    return stop_sp;
}

const BreakpointSiteSP
BreakpointSiteList::FindByID (lldb::break_id_t break_id) const
{
    BreakpointSiteSP stop_sp;
    collection::const_iterator pos = GetIDConstIterator(break_id);
    if (pos != m_bp_site_list.end())
        stop_sp = pos->second;

    return stop_sp;
}

BreakpointSiteSP
BreakpointSiteList::FindByAddress (lldb::addr_t addr)
{
    BreakpointSiteSP found_sp;

    collection::iterator iter =  m_bp_site_list.find(addr);
    if (iter != m_bp_site_list.end())
        found_sp = iter->second;
    return found_sp;
}

bool
BreakpointSiteList::BreakpointSiteContainsBreakpoint (lldb::break_id_t bp_site_id, lldb::break_id_t bp_id)
{
    collection::const_iterator pos = GetIDConstIterator(bp_site_id);
    if (pos != m_bp_site_list.end())
        pos->second->IsBreakpointAtThisSite (bp_id);

    return false;
}

void
BreakpointSiteList::Dump (Stream *s) const
{
    s->Printf("%p: ", this);
    //s->Indent();
    s->Printf("BreakpointSiteList with %u BreakpointSites:\n", (uint32_t)m_bp_site_list.size());
    s->IndentMore();
    collection::const_iterator pos;
    collection::const_iterator end = m_bp_site_list.end();
    for (pos = m_bp_site_list.begin(); pos != end; ++pos)
        pos->second.get()->Dump(s);
    s->IndentLess();
}


BreakpointSiteSP
BreakpointSiteList::GetByIndex (uint32_t i)
{
    BreakpointSiteSP stop_sp;
    collection::iterator end = m_bp_site_list.end();
    collection::iterator pos;
    uint32_t curr_i = 0;
    for (pos = m_bp_site_list.begin(), curr_i = 0; pos != end; ++pos, ++curr_i)
    {
        if (curr_i == i)
            stop_sp = pos->second;
    }
    return stop_sp;
}

const BreakpointSiteSP
BreakpointSiteList::GetByIndex (uint32_t i) const
{
    BreakpointSiteSP stop_sp;
    collection::const_iterator end = m_bp_site_list.end();
    collection::const_iterator pos;
    uint32_t curr_i = 0;
    for (pos = m_bp_site_list.begin(), curr_i = 0; pos != end; ++pos, ++curr_i)
    {
        if (curr_i == i)
            stop_sp = pos->second;
    }
    return stop_sp;
}

bool
BreakpointSiteList::FindInRange (lldb::addr_t lower_bound, lldb::addr_t upper_bound, BreakpointSiteList &bp_site_list) const
{
        
    if (lower_bound > upper_bound)
        return false;
    
    collection::const_iterator lower, upper, pos;
    lower = m_bp_site_list.lower_bound(lower_bound);
    if (lower == m_bp_site_list.end()
            || (*lower).first >= upper_bound)
        return false;
    
    // This is one tricky bit.  The breakpoint might overlap the bottom end of the range.  So we grab the
    // breakpoint prior to the lower bound, and check that that + its byte size isn't in our range.
    if (lower != m_bp_site_list.begin())
    {
        collection::const_iterator prev_pos = lower;
        prev_pos--;
        const BreakpointSiteSP &prev_bp = (*prev_pos).second;
        if (prev_bp->GetLoadAddress() + prev_bp->GetByteSize() > lower_bound)
            bp_site_list.Add (prev_bp);
        
    }
    
    upper = m_bp_site_list.upper_bound(upper_bound);
        
    for (pos = lower; pos != upper; pos++)
    {
        bp_site_list.Add ((*pos).second);
    }
    return true;
}


void
BreakpointSiteList::SetEnabledForAll (const bool enabled, const lldb::break_id_t except_id)
{
    collection::iterator end = m_bp_site_list.end();
    collection::iterator pos;
    for (pos = m_bp_site_list.begin(); pos != end; ++pos)
    {
        if (except_id != LLDB_INVALID_BREAK_ID && except_id != pos->second->GetID())
            pos->second->SetEnabled (enabled);
        else
            pos->second->SetEnabled (!enabled);
    }
}

const BreakpointSiteList::collection *
BreakpointSiteList::GetMap ()
{
    return &m_bp_site_list;
}
OpenPOWER on IntegriCloud