This repository has been archived by the owner on Feb 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 74
/
HardwareBreakpointManager.cpp
162 lines (130 loc) · 4.53 KB
/
HardwareBreakpointManager.cpp
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
//
// Copyright (c) 2014-present, Facebook, Inc.
// All rights reserved.
//
// This source code is licensed under the University of Illinois/NCSA Open
// Source License found in the LICENSE file in the root directory of this
// source tree. An additional grant of patent rights can be found in the
// PATENTS file in the same directory.
//
#include "DebugServer2/Core/HardwareBreakpointManager.h"
#include "DebugServer2/Target/Process.h"
#include "DebugServer2/Target/Thread.h"
#include <algorithm>
#define super ds2::BreakpointManager
namespace ds2 {
HardwareBreakpointManager::HardwareBreakpointManager(
Target::ProcessBase *process)
: super(process), _locations(maxWatchpoints()) {}
HardwareBreakpointManager::~HardwareBreakpointManager() {}
ErrorCode HardwareBreakpointManager::add(Address const &address, Type type,
size_t size, Mode mode) {
if (_sites.size() >= maxWatchpoints()) {
return kErrorInvalidArgument;
}
if (mode == kModeRead) {
DS2LOG(Warning,
"read-only watchpoints are unsupported, setting as read-write");
mode = static_cast<Mode>(mode | kModeWrite);
}
return super::add(address, type, size, mode);
}
ErrorCode HardwareBreakpointManager::remove(Address const &address) {
auto loc = std::find_if(_locations.begin(), _locations.end(),
[address](const Location &l){
return l.address == address;
});
if (loc != _locations.end()) {
_locations[loc - _locations.begin()].address = 0;
}
return super::remove(address);
}
ErrorCode HardwareBreakpointManager::enableLocation(Site const &site,
Target::Thread *thread) {
ErrorCode error;
int idx;
auto loc = std::find_if(_locations.begin(), _locations.end(),
[site](const Location &l){
return l.address == site.address;
});
if (loc == _locations.end()) {
idx = getAvailableLocation();
if (idx < 0) {
return kErrorInvalidArgument;
}
} else {
idx = loc - _locations.begin();
}
enumerateThreads(thread, [&](Target::Thread *t) {
if (t->state() == Target::Thread::kStopped && !enabled(t)) {
error = enableLocation(site, idx, t);
}
});
if (error == kSuccess) {
_locations[idx].address = site.address;
}
return error;
}
ErrorCode HardwareBreakpointManager::disableLocation(Site const &site,
Target::Thread *thread) {
ErrorCode error;
auto loc = std::find_if(_locations.begin(), _locations.end(),
[site](const Location &l){
return l.address == site.address;
});
if (loc == _locations.end()) {
return kErrorInvalidArgument;
}
int idx = loc - _locations.begin();
enumerateThreads(thread, [&](Target::Thread *t) {
if (t->state() == Target::Thread::kStopped && enabled(t)) {
error = disableLocation(idx, t);
}
});
return error;
}
int HardwareBreakpointManager::getAvailableLocation() {
DS2ASSERT(_locations.size() == maxWatchpoints());
if (_sites.size() == maxWatchpoints()) {
return -1;
}
auto loc = std::find_if(_locations.begin(), _locations.end(),
[](const Location &l){
return l.address == 0;
});
DS2ASSERT(it != _locations.end());
return (it - _locations.begin());
}
void HardwareBreakpointManager::enumerateThreads(
Target::Thread *thread,
std::function<void(Target::Thread *)> const &cb) const {
std::set<Target::Thread *> threads;
if (thread != nullptr) {
threads.insert(thread);
} else {
_process->enumerateThreads([&](Target::Thread *t) { threads.insert(t); });
}
for (auto t : threads) {
cb(t);
}
}
void HardwareBreakpointManager::enable(Target::Thread *thread) {
super::enable(thread);
enumerateThreads(thread,
[this](Target::Thread *t) { _enabled.insert(t->tid()); });
}
void HardwareBreakpointManager::disable(Target::Thread *thread) {
super::disable(thread);
enumerateThreads(thread,
[this](Target::Thread *t) { _enabled.erase(t->tid()); });
}
bool HardwareBreakpointManager::enabled(Target::Thread *thread) const {
bool isEnabled = true;
enumerateThreads(thread, [this, &isEnabled](Target::Thread *t) {
if (_enabled.count(t->tid()) == 0) {
isEnabled = false;
}
});
return isEnabled;
}
} // namespace ds2