Repository navigation
Expand file tree
/
Copy pathdebugger_hook_handler.cpp
More file actions
196 lines (180 loc) · 7.1 KB
/
Copy pathdebugger_hook_handler.cpp
File metadata and controls
196 lines (180 loc) · 7.1 KB
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
/*
+----------------------------------------------------------------------+
| HipHop for PHP |
+----------------------------------------------------------------------+
| Copyright (c) 2010-present Facebook, Inc. (http://www.facebook.com) |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
*/
#include "hphp/runtime/debugger/debugger_hook_handler.h"
namespace HPHP { namespace Eval {
//////////////////////////////////////////////////////////////////////////
TRACE_SET_MOD(debuggerflow);
// See if the given name matches the function's name.
static bool matchFunctionName(std::string name, const Func* f) {
return name == f->name()->data();
}
static void addBreakPointInUnit(BreakPointInfoPtr bp, Unit* unit) {
OffsetRangeVec offsets;
if (!unit->getOffsetRanges(bp->m_line1, offsets) || offsets.size() == 0) {
bp->m_bindState = BreakPointInfo::KnownToBeInvalid;
return;
}
bp->m_bindState = BreakPointInfo::KnownToBeValid;
TRACE(3, "Add to breakpoint filter for %s:%d, unit %p:\n",
unit->filepath()->data(), bp->m_line1, unit);
assertx(offsets.size() > 0);
auto bpOffset = offsets[0].base;
phpAddBreakPoint(unit, bpOffset);
}
void proxySetBreakPoints(DebuggerProxy* proxy) {
std::vector<BreakPointInfoPtr> bps;
proxy->getBreakPoints(bps);
for (unsigned int i = 0; i < bps.size(); i++) {
BreakPointInfoPtr bp = bps[i];
bp->m_bindState = BreakPointInfo::Unknown;
auto className = bp->getClass();
if (!className.empty()) {
auto clsName = makeStaticString(className);
auto cls = Unit::lookupClass(clsName);
if (cls == nullptr) continue;
bp->m_bindState = BreakPointInfo::KnownToBeInvalid;
size_t numFuncs = cls->numMethods();
if (numFuncs == 0) continue;
auto methodName = bp->getFunction();
for (size_t i2 = 0; i2 < numFuncs; ++i2) {
auto f = cls->getMethod(i2);
if (!matchFunctionName(methodName, f)) continue;
bp->m_bindState = BreakPointInfo::KnownToBeValid;
phpAddBreakPointFuncEntry(f);
break;
}
// TODO(#2527229): what about superclass methods accessed via the derived
// class?
continue;
}
auto funcName = bp->getFuncName();
if (!funcName.empty()) {
auto fName = makeStaticString(funcName);
Func* f = Unit::lookupFunc(fName);
if (f == nullptr) continue;
bp->m_bindState = BreakPointInfo::KnownToBeValid;
phpAddBreakPointFuncEntry(f);
continue;
}
auto fileName = bp->m_file;
if (!fileName.empty()) {
for (auto& kv : g_context->m_evaledFiles) {
auto const unit = kv.second.unit;
if (!BreakPointInfo::MatchFile(fileName,
unit->filepath()->toCppString())) {
continue;
}
addBreakPointInUnit(bp, unit);
break;
}
continue;
}
auto exceptionClassName = bp->getExceptionClass();
if (exceptionClassName == "@") {
bp->m_bindState = BreakPointInfo::KnownToBeValid;
continue;
} else if (!exceptionClassName.empty()) {
auto expClsName = makeStaticString(exceptionClassName);
auto cls = Unit::lookupClass(expClsName);
if (cls != nullptr) {
auto baseClsName = makeStaticString("Exception");
auto baseCls = Unit::lookupClass(baseClsName);
if (baseCls != nullptr) {
if (cls->classof(baseCls)) {
bp->m_bindState = BreakPointInfo::KnownToBeValid;
} else {
bp->m_bindState = BreakPointInfo::KnownToBeInvalid;
}
}
}
continue;
} else {
continue;
}
// If we get here, the break point is of a type that does
// not need to be explicitly enabled in the VM. For example
// a break point that get's triggered when the server starts
// to process a page request.
bp->m_bindState = BreakPointInfo::KnownToBeValid;
}
}
DebuggerHook* HphpdHook::GetInstance() {
static DebuggerHook* instance = new HphpdHook();
return instance;
}
void HphpdHook::onFileLoad(Unit* unit) {
DebuggerProxy* proxy = Debugger::GetProxy().get();
if (proxy == nullptr) return;
// Look up the proxy's breakpoints and add needed breakpoints to the passed
// unit
std::vector<BreakPointInfoPtr> bps;
proxy->getBreakPoints(bps);
for (unsigned int i = 0; i < bps.size(); i++) {
BreakPointInfoPtr bp = bps[i];
if (BreakPointInfo::MatchFile(bp->m_file,
unit->filepath()->toCppString())) {
addBreakPointInUnit(bp, unit);
}
}
}
void HphpdHook::onDefClass(const Class* cls) {
// Make sure we have a proxy
DebuggerProxy* proxy = Debugger::GetProxy().get();
if (proxy == nullptr) return;
// If the proxy has enabled breakpoints that match entry into methods of
// the given class, arrange for the VM to stop execution and notify the
// debugger whenever execution enters one of these matched method.
// This function is called once, when a class is first loaded, so it is not
// performance critical.
size_t numFuncs = cls->numMethods();
if (numFuncs == 0) return;
auto clsName = cls->name();
std::vector<BreakPointInfoPtr> bps;
proxy->getBreakPoints(bps);
for (unsigned int i = 0; i < bps.size(); i++) {
BreakPointInfoPtr bp = bps[i];
if (bp->m_state == BreakPointInfo::Disabled) continue;
// TODO: check name space separately
if (bp->getClass() != clsName->data()) continue;
bp->m_bindState = BreakPointInfo::KnownToBeInvalid;
for (size_t i2 = 0; i2 < numFuncs; ++i2) {
auto f = cls->getMethod(i2);
if (!matchFunctionName(bp->getFunction(), f)) continue;
bp->m_bindState = BreakPointInfo::KnownToBeValid;
phpAddBreakPointFuncEntry(f);
}
}
}
void HphpdHook::onDefFunc(const Func* f) {
// Make sure we have a proxy
DebuggerProxyPtr proxy = Debugger::GetProxy();
if (proxy == nullptr) return;
// If the proxy has an enabled breakpoint that matches entry into the given
// function, arrange for the VM to stop execution and notify the debugger
// whenever execution enters the given function.
std::vector<BreakPointInfoPtr> bps;
proxy->getBreakPoints(bps);
for (unsigned int i = 0; i < bps.size(); i++) {
BreakPointInfoPtr bp = bps[i];
if (bp->m_state == BreakPointInfo::Disabled) continue;
if (!matchFunctionName(bp->getFuncName(), f)) continue;
bp->m_bindState = BreakPointInfo::KnownToBeValid;
phpAddBreakPointFuncEntry(f);
return;
}
}
//////////////////////////////////////////////////////////////////////////
}}