aboutsummaryrefslogtreecommitdiff
path: root/source/Host/common/NativeBreakpointList.cpp
blob: 94d0b3756da4d2bd45f293f10b4178b664615862 (plain) (blame)
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
//===-- NativeBreakpointList.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/Host/common/NativeBreakpointList.h"

#include "lldb/Core/Log.h"

#include "lldb/Host/common/NativeBreakpoint.h"

using namespace lldb;
using namespace lldb_private;

NativeBreakpointList::NativeBreakpointList () :
    m_mutex (Mutex::eMutexTypeRecursive)
{
}

Error
NativeBreakpointList::AddRef (lldb::addr_t addr, size_t size_hint, bool hardware, CreateBreakpointFunc create_func)
{
    Log *log (GetLogIfAnyCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
    if (log)
        log->Printf ("NativeBreakpointList::%s addr = 0x%" PRIx64 ", size_hint = %lu, hardware = %s", __FUNCTION__, addr, size_hint, hardware ? "true" : "false");

    Mutex::Locker locker (m_mutex);

    // Check if the breakpoint is already set.
    auto iter = m_breakpoints.find (addr);
    if (iter != m_breakpoints.end ())
    {
        // Yes - bump up ref count.
        if (log)
            log->Printf ("NativeBreakpointList::%s addr = 0x%" PRIx64 " -- already enabled, upping ref count", __FUNCTION__, addr);

        iter->second->AddRef ();
        return Error ();
    }

    // Create a new breakpoint using the given create func.
    if (log)
        log->Printf ("NativeBreakpointList::%s creating breakpoint for addr = 0x%" PRIx64 ", size_hint = %lu, hardware = %s", __FUNCTION__, addr, size_hint, hardware ? "true" : "false");

    NativeBreakpointSP breakpoint_sp;
    Error error = create_func (addr, size_hint, hardware, breakpoint_sp);
    if (error.Fail ())
    {
        if (log)
            log->Printf ("NativeBreakpointList::%s creating breakpoint for addr = 0x%" PRIx64 ", size_hint = %lu, hardware = %s -- FAILED: %s", __FUNCTION__, addr, size_hint, hardware ? "true" : "false", error.AsCString ());
        return error;
    }

    // Remember the breakpoint.
    assert (breakpoint_sp && "NativeBreakpoint create function succeeded but returned NULL breakpoint");
    m_breakpoints.insert (BreakpointMap::value_type (addr, breakpoint_sp));

    return error;
}

Error
NativeBreakpointList::DecRef (lldb::addr_t addr)
{
    Error error;

    Log *log (GetLogIfAnyCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
    if (log)
        log->Printf ("NativeBreakpointList::%s addr = 0x%" PRIx64, __FUNCTION__, addr);

    Mutex::Locker locker (m_mutex);

    // Check if the breakpoint is already set.
    auto iter = m_breakpoints.find (addr);
    if (iter == m_breakpoints.end ())
    {
        // Not found!
        if (log)
            log->Printf ("NativeBreakpointList::%s addr = 0x%" PRIx64 " -- NOT FOUND", __FUNCTION__, addr);
        error.SetErrorString ("breakpoint not found");
        return error;
    }

    // Decrement ref count.
    const int32_t new_ref_count = iter->second->DecRef ();
    assert (new_ref_count >= 0 && "NativeBreakpoint ref count went negative");

    if (new_ref_count > 0)
    {
        // Still references to this breakpoint.  Leave it alone.
        if (log)
            log->Printf ("NativeBreakpointList::%s addr = 0x%" PRIx64 " -- new breakpoint ref count %" PRIu32, __FUNCTION__, addr, new_ref_count);
        return error;
    }

    // Breakpoint has no more references.  Disable it if it's not
    // already disabled.
    if (log)
        log->Printf ("NativeBreakpointList::%s addr = 0x%" PRIx64 " -- removing due to no remaining references", __FUNCTION__, addr);

    // If it's enabled, we need to disable it.
    if (iter->second->IsEnabled ())
    {
        if (log)
            log->Printf ("NativeBreakpointList::%s addr = 0x%" PRIx64 " -- currently enabled, now disabling", __FUNCTION__, addr);
        error = iter->second->Disable ();
        if (error.Fail ())
        {
            if (log)
                log->Printf ("NativeBreakpointList::%s addr = 0x%" PRIx64 " -- removal FAILED: %s", __FUNCTION__, addr, error.AsCString ());
            // Continue since we still want to take it out of the breakpoint list.
        }
    }
    else
    {
        if (log)
            log->Printf ("NativeBreakpointList::%s addr = 0x%" PRIx64 " -- already disabled, nothing to do", __FUNCTION__, addr);
    }

    // Take the breakpoint out of the list.
    if (log)
        log->Printf ("NativeBreakpointList::%s addr = 0x%" PRIx64 " -- removed from breakpoint map", __FUNCTION__, addr);

    m_breakpoints.erase (iter);
    return error;
}

Error
NativeBreakpointList::EnableBreakpoint (lldb::addr_t addr)
{
    Log *log (GetLogIfAnyCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
    if (log)
        log->Printf ("NativeBreakpointList::%s addr = 0x%" PRIx64, __FUNCTION__, addr);

    Mutex::Locker locker (m_mutex);

    // Ensure we have said breakpoint.
    auto iter = m_breakpoints.find (addr);
    if (iter == m_breakpoints.end ())
    {
        // Not found!
        if (log)
            log->Printf ("NativeBreakpointList::%s addr = 0x%" PRIx64 " -- NOT FOUND", __FUNCTION__, addr);
        return Error ("breakpoint not found");
    }

    // Enable it.
    return iter->second->Enable ();
}

Error
NativeBreakpointList::DisableBreakpoint (lldb::addr_t addr)
{
    Log *log (GetLogIfAnyCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
    if (log)
        log->Printf ("NativeBreakpointList::%s addr = 0x%" PRIx64, __FUNCTION__, addr);

    Mutex::Locker locker (m_mutex);

    // Ensure we have said breakpoint.
    auto iter = m_breakpoints.find (addr);
    if (iter == m_breakpoints.end ())
    {
        // Not found!
        if (log)
            log->Printf ("NativeBreakpointList::%s addr = 0x%" PRIx64 " -- NOT FOUND", __FUNCTION__, addr);
        return Error ("breakpoint not found");
    }

    // Disable it.
    return iter->second->Disable ();
}

Error
NativeBreakpointList::GetBreakpoint (lldb::addr_t addr, NativeBreakpointSP &breakpoint_sp)
{
    Log *log (GetLogIfAnyCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
    if (log)
        log->Printf ("NativeBreakpointList::%s addr = 0x%" PRIx64, __FUNCTION__, addr);

    Mutex::Locker locker (m_mutex);

    // Ensure we have said breakpoint.
    auto iter = m_breakpoints.find (addr);
    if (iter == m_breakpoints.end ())
    {
        // Not found!
        breakpoint_sp.reset ();
        return Error ("breakpoint not found");
    }

    // Disable it.
    breakpoint_sp = iter->second;
    return Error ();
}