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
215 lines (176 loc) · 6.03 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
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
// 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;
}
// Readonly hardware watchpoints aren't natively implemented, so they
// are implemened in software instead. See #510
if (mode == kModeRead) mode = static_cast<Mode>(mode | kModeWrite);
return super::add(address, type, size, mode);
}
ErrorCode HardwareBreakpointManager::remove(Address const &address) {
auto loc = std::find(_locations.begin(), _locations.end(), address);
if (loc != _locations.end()) {
_locations.erase(loc);
}
return super::remove(address);
}
ErrorCode HardwareBreakpointManager::enableLocation(Site const &site,
Target::Thread *thread) {
ErrorCode error;
int idx;
auto loc = std::find(_locations.begin(), _locations.end(), 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] = site.address;
CHK(storeNewValueAtAddress(site.address));
}
return error;
}
ErrorCode HardwareBreakpointManager::disableLocation(Site const &site,
Target::Thread *thread) {
ErrorCode error;
auto loc = std::find(_locations.begin(), _locations.end(), site.address);
if (loc == _locations.end()) {
return kErrorInvalidArgument;
} else {
int idx = loc - _locations.begin();
enumerateThreads(thread, [&](Target::Thread *t) {
if (t->state() == Target::Thread::kStopped && enabled(t)) {
// TODO(asb) make sure that we're reducing the error correctly and it
// isn't getting overwritten by multiple calls
error = disableLocation(idx, t);
}
});
return error;
}
}
inline bool
HardwareBreakpointManager::softwareImplementationOfReadonlyWatchpoints(
Address const &address, Site &site) {
switch (site.mode) {
// if this is a readonly hardware watchpoint
case kModeRead:
// if written to, return false. if not, invoke super.
return wasWritten(address) ? false : super::hit(address, site);
break;
default:
return super::hit(address, site);
}
}
ErrorCode
HardwareBreakpointManager::storeNewValueAtAddress(Address const &address) {
uint64_t value;
CHK(_process->readMemory(address, &value, sizeof(value)));
try {
// store the new value. this will throw if _sites[address.value()] == null
_sites.at(address.value()).value = value;
return kSuccess;
} catch (const std::out_of_range &) {
return kErrorInvalidAddress;
}
}
bool HardwareBreakpointManager::hit(Address const &address, Site &site) {
auto ret = softwareImplementationOfReadonlyWatchpoints(address, site);
DS2ASSERT(storeNewValueAtAddress(address) == kSuccess);
return ret;
}
int HardwareBreakpointManager::getAvailableLocation() {
DS2ASSERT(_locations.size() == maxWatchpoints());
if (_sites.size() == maxWatchpoints()) {
return -1;
}
auto it = std::find(_locations.begin(), _locations.end(), 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;
}
bool HardwareBreakpointManager::fillStopInfo(Target::Thread *thread,
StopInfo &stopInfo) {
BreakpointManager::Site site;
int bpIdx = hit(thread, site);
if (bpIdx < 0) {
return false;
}
stopInfo.watchpointIndex = bpIdx;
stopInfo.watchpointAddress = site.address;
switch (static_cast<int>(site.mode)) {
case BreakpointManager::kModeExec:
stopInfo.reason = StopInfo::kReasonBreakpoint;
break;
case BreakpointManager::kModeWrite:
stopInfo.reason = StopInfo::kReasonWriteWatchpoint;
break;
case BreakpointManager::kModeRead:
stopInfo.reason = StopInfo::kReasonReadWatchpoint;
break;
case BreakpointManager::kModeRead | BreakpointManager::kModeWrite:
stopInfo.reason = StopInfo::kReasonAccessWatchpoint;
break;
default:
DS2BUG("invalid mode");
}
return true;
}
} // namespace ds2