Repository navigation
Expand file tree
/
Copy pathcmd_machine.cpp
More file actions
339 lines (312 loc) · 11.7 KB
/
Copy pathcmd_machine.cpp
File metadata and controls
339 lines (312 loc) · 11.7 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
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/*
+----------------------------------------------------------------------+
| HipHop for PHP |
+----------------------------------------------------------------------+
| Copyright (c) 2010-2013 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/cmd/cmd_machine.h"
#include "hphp/runtime/debugger/cmd/cmd_signal.h"
#include "hphp/runtime/base/runtime-option.h"
#include "hphp/runtime/base/intercept.h"
#include "hphp/runtime/base/array-init.h"
#include "hphp/runtime/base/libevent-http-client.h"
#include "hphp/util/process.h"
namespace HPHP { namespace Eval {
///////////////////////////////////////////////////////////////////////////////
TRACE_SET_MOD(debugger);
void CmdMachine::sendImpl(DebuggerThriftBuffer &thrift) {
DebuggerCommand::sendImpl(thrift);
thrift.write(m_sandboxes);
thrift.write(m_rpcConfig);
thrift.write(m_force);
thrift.write(m_succeed);
}
void CmdMachine::recvImpl(DebuggerThriftBuffer &thrift) {
DebuggerCommand::recvImpl(thrift);
thrift.read(m_sandboxes);
thrift.read(m_rpcConfig);
thrift.read(m_force);
thrift.read(m_succeed);
}
void CmdMachine::list(DebuggerClient &client) {
if (client.argCount() == 0) {
static const char *keywords[] =
{ "disconnect", "connect", "rpc", "list", "attach", nullptr };
client.addCompletion(keywords);
}
}
void CmdMachine::help(DebuggerClient &client) {
client.helpTitle("Machine Command");
client.helpCmds(
"[m]achine [c]onnect {host}", "debugging remote server natively",
"[m]achine [c]onnect {host}:{port}", "debugging remote server natively",
"[m]achine [r]pc {host}", "debugging remote server with RPC",
"[m]achine [r]pc {host}:{port}", "debugging remote server with RPC",
"[m]achine [d]isconnect", "disconnect, debugging local script",
"[m]achine [l]ist", "list all sandboxes",
"[m]achine [a]ttach {index}", "attach to a sandbox",
"[m]achine [a]ttach {sandbox}", "attach to my sandbox by name",
"[m]achine [a]ttach {user} {sandbox}",
"attach to a sandbox by user and name",
"[m]achine [a]ttach [f]orce {index|sandbox|user sandbox}",
"force attach to a sandbox (see below)",
nullptr
);
client.helpBody(
"Use this command to switch between different machines or "
"sandboxes.\n"
"\n"
"If command prompt says \"hphpd\", all evaluation of PHP code happens "
"locally within the debugger. This is the mode when debugger is started "
"without a remote server name. No user libraries are pre-loaded in this "
"mode.\n"
"\n"
"When connecting to a remote server, it will automatically attach "
"to \"default\" sandbox under current user. If \"default\" sandbox "
"does not exist, it will attach to a random sandbox under current "
"user. In sandbox mode, a file specified in server's configuration "
"of \"Eval.Debugger.StartupDocument\" is pre-loaded.\n"
"\n"
"If there is no sandbox available, it will create a \"dummy\" "
"sandbox and attach to it.\n"
"\n"
"When your sandbox is not available, please hit it at least once "
"from your browser. Then run '[m]achine [l]ist' command again.\n"
"\n"
"If another debugger client is already attached to your sandbox you can "
"use the '[f]orce' option to '[m]achine [a]ttach'. This will disconnect "
"the other client and force your client to connect.\n"
"\n"
"If a HipHop server has RPC port open, one can also debug the server in "
"a very special RPC mode. In this mode, one can type in PHP scripts to "
"run, but all functions will be executed on server through RPC. Because "
"states are still maintained locally and only functions are executed "
"remotely, it may not work with functions or scripts that depend on "
"global variables or low-level raw resource pointers. As a simple rule, "
"stateless functions will work just fine. This is true to objects and "
"method calls as well, except classes will have to be loaded on client "
"side by '=include(\"file-containing-the-class.php\")'."
);
}
bool CmdMachine::processList(DebuggerClient &client,
bool output /* = true */) {
m_body = "list";
CmdMachinePtr res = client.xend<CmdMachine>(this);
client.updateSandboxes(res->m_sandboxes);
if (!output) return true;
if (res->m_sandboxes.empty()) {
client.info("(no sandbox was found)");
client.tutorial(
"Please hit the sandbox from browser at least once. Then run "
"'[m]achine [l]ist' again."
);
} else {
for (int i = 0; i < (int)res->m_sandboxes.size(); i++) {
client.print(" %d\t%s", i + 1,
res->m_sandboxes[i]->desc().c_str());
}
client.tutorial(
"Use '[m]achine [a]ttach {index}' to attach to one sandbox. For "
"example, 'm a 1'. If desired sandbox is not on the list, please "
"hit the sandbox from browser once. Then run '[m]achine [l]ist' "
"again."
);
}
return true;
}
bool CmdMachine::AttachSandbox(DebuggerClient &client,
const char *user /* = NULL */,
const char *name /* = NULL */,
bool force /* = false */) {
string login;
if (user == nullptr) {
user = client.getCurrentUser().c_str();
}
DSandboxInfoPtr sandbox(new DSandboxInfo());
sandbox->m_user = user ? user : "";
sandbox->m_name = (name && *name) ? name : "default";
return AttachSandbox(client, sandbox, force);
}
bool CmdMachine::AttachSandbox(DebuggerClient &client,
DSandboxInfoPtr sandbox,
bool force /* = false */) {
if (client.isLocal()) {
client.error("Local script doesn't have sandbox to attach to.");
return false;
}
CmdMachine cmd;
cmd.m_body = "attach";
cmd.m_sandboxes.push_back(sandbox);
cmd.m_force = force;
client.info("Attaching to %s and pre-loading, please wait...",
sandbox->desc().c_str());
CmdMachinePtr cmdMachine = client.xend<CmdMachine>(&cmd);
if (cmdMachine->m_succeed) {
client.setSandbox(sandbox);
client.playMacro("startup");
} else {
// Note: it would be nice to give them more info about the process we think
// is debugging this sandbox: what machine it's on, what it's pid is, etc.
// Unfortunately, we don't have any of that data. We'd need a protocol
// change to have the client give us more info when it attaches.
client.error(
"Failed to attach to the sandbox. Maybe another client is debugging, \n"
"or a client failed to detach cleanly.\n"
"You can attach to another sandbox, or exit the other attached client, \n"
"or force this client to take over the sandbox with: \n"
"\n"
"\t[m]achine [a]ttach [f]orce %s %s"
"\n",
sandbox->m_user.c_str(), sandbox->m_name.c_str());
}
return cmdMachine->m_succeed;
}
const StaticString
s_host("host"),
s_port("port"),
s_auth("auth"),
s_timeout("timeout");
void CmdMachine::UpdateIntercept(DebuggerClient &client,
const std::string &host, int port) {
CmdMachine cmd;
cmd.m_body = "rpc";
cmd.m_rpcConfig = make_map_array
(s_host, String(host),
s_port, port ? port : RuntimeOption::DebuggerDefaultRpcPort,
s_auth, String(RuntimeOption::DebuggerDefaultRpcAuth),
s_timeout, RuntimeOption::DebuggerDefaultRpcTimeout);
client.xend<CmdMachine>(&cmd);
}
void CmdMachine::onClient(DebuggerClient &client) {
if (DebuggerCommand::displayedHelp(client)) return;
if (client.argCount() == 0) {
help(client);
return;
}
bool rpc = client.arg(1, "rpc");
if (rpc || client.arg(1, "connect")) {
if (client.argCount() != 2) {
help(client);
return;
}
string host = client.argValue(2);
int port = 0;
size_t pos = host.find(":");
if (pos != string::npos) {
if (!DebuggerClient::IsValidNumber(host.substr(pos + 1))) {
client.error("Port needs to be a number");
help(client);
return;
}
port = atoi(host.substr(pos + 1).c_str());
host = host.substr(0, pos);
}
if (rpc) {
if (client.connectRPC(host, port)) {
throw DebuggerConsoleExitException();
}
} else {
if (client.connect(host, port)) {
throw DebuggerConsoleExitException();
}
}
if (!client.initializeMachine()) {
throw DebuggerConsoleExitException();
}
return;
}
if (client.arg(1, "disconnect")) {
if (client.disconnect()) {
throw DebuggerConsoleExitException();
}
if (!client.initializeMachine()) {
throw DebuggerConsoleExitException();
}
return;
}
if (client.arg(1, "list")) {
processList(client);
return;
}
if (client.arg(1, "attach")) {
DSandboxInfoPtr sandbox;
string snum = client.argValue(2);
if (DebuggerClient::IsValidNumber(snum)) {
int num = atoi(snum.c_str());
sandbox = client.getSandbox(num);
if (!sandbox) {
processList(client, false);
sandbox = client.getSandbox(num);
if (!sandbox) {
client.error("\"%s\" is not a valid sandbox index. Choose one from "
"this list:", snum.c_str());
processList(client);
return;
}
}
} else {
int argBase = 2;
if (client.argCount() >= 2 && client.arg(2, "force")) {
m_force = true;
argBase++;
}
sandbox = DSandboxInfoPtr(new DSandboxInfo());
if (client.argCount() < argBase) {
sandbox->m_user = client.getCurrentUser();
sandbox->m_name = "default";
} else if (client.argCount() == argBase) {
sandbox->m_user = client.getCurrentUser();
sandbox->m_name = client.argValue(argBase);
} else if (client.argCount() == argBase + 1) {
sandbox->m_user = client.argValue(argBase);
sandbox->m_name = client.argValue(argBase + 1);
} else {
help(client);
return;
}
}
if (AttachSandbox(client, sandbox, m_force)) {
// Attach succeed, wait for next interrupt
throw DebuggerConsoleExitException();
}
return;
}
help(client);
}
bool CmdMachine::onServer(DebuggerProxy &proxy) {
if (m_body == "rpc") {
String host = m_rpcConfig[s_host].toString();
if (host.empty()) {
register_intercept("", false, uninit_null());
} else {
int port = m_rpcConfig[s_port].toInt32();
LibEventHttpClient::SetCache(host.data(), port, 1);
register_intercept("", "fb_rpc_intercept_handler", m_rpcConfig);
}
return proxy.sendToClient(this);
}
if (m_body == "list") {
Debugger::GetRegisteredSandboxes(m_sandboxes);
return proxy.sendToClient(this);
}
if (m_body == "attach" && !m_sandboxes.empty()) {
m_succeed = proxy.switchSandbox(m_sandboxes[0]->id(), m_force);
if (m_succeed) {
proxy.notifyDummySandbox();
m_exitInterrupt = true;
}
return proxy.sendToClient(this);
}
return false;
}
///////////////////////////////////////////////////////////////////////////////
}}