Repository navigation
Expand file tree
/
Copy pathcmd_print.cpp
More file actions
368 lines (338 loc) · 11.8 KB
/
Copy pathcmd_print.cpp
File metadata and controls
368 lines (338 loc) · 11.8 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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/*
+----------------------------------------------------------------------+
| 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_print.h"
#include "hphp/runtime/base/datetime.h"
#include "hphp/runtime/base/string-util.h"
#include "hphp/runtime/vm/debugger-hook.h"
#include "hphp/runtime/base/array-init.h"
namespace HPHP { namespace Eval {
///////////////////////////////////////////////////////////////////////////////
TRACE_SET_MOD(debugger);
const char *CmdPrint::Formats[] = {
"r", "v", "x", "hex", "oct", "dec", "unsigned", "time", nullptr
};
std::string CmdPrint::FormatResult(const char *format, CVarRef ret) {
if (format == nullptr) {
String sret = DebuggerClient::FormatVariable(ret, -1);
return string(sret.data(), sret.size());
}
if (strcmp(format, "r") == 0) {
String sret = DebuggerClient::FormatVariable(ret, -1, 'r');
return string(sret.data(), sret.size());
}
if (strcmp(format, "v") == 0) {
String sret = DebuggerClient::FormatVariable(ret, -1, 'v');
return string(sret.data(), sret.size());
}
if (strcmp(format, "dec") == 0 ||
strcmp(format, "unsigned") == 0 ||
ret.isInteger()) {
int64_t nret = ret.toInt64();
char buf[64];
if (strcmp(format, "hex") == 0 || strcmp(format, "x") == 0) {
snprintf(buf, sizeof(buf), "%" PRIx64, nret);
return buf;
}
if (strcmp(format, "oct") == 0) {
snprintf(buf, sizeof(buf), "%" PRIo64, nret);
return buf;
}
if (strcmp(format, "dec") == 0) {
snprintf(buf, sizeof(buf), "%" PRId64, nret);
return buf;
}
if (strcmp(format, "unsigned") == 0) {
snprintf(buf, sizeof(buf), "%" PRIu64, nret);
return buf;
}
if (strcmp(format, "time") == 0) {
StringBuffer sb;
DateTime dt(nret);
sb.append("RFC822: ");
sb.append(dt.toString(DateTime::DateFormat::RFC822));
sb.append("\nRFC850: ");
sb.append(dt.toString(DateTime::DateFormat::RFC850));
sb.append("\nRFC1036: ");
sb.append(dt.toString(DateTime::DateFormat::RFC1036));
sb.append("\nRFC1123/RSS: ");
sb.append(dt.toString(DateTime::DateFormat::RFC1123));
sb.append("\nRFC2822: ");
sb.append(dt.toString(DateTime::DateFormat::RFC2822));
sb.append("\nRFC3339/ATOM/W3C: ");
sb.append(dt.toString(DateTime::DateFormat::RFC3339));
sb.append("\nISO8601: ");
sb.append(dt.toString(DateTime::DateFormat::ISO8601));
sb.append("\nCookie: ");
sb.append(dt.toString(DateTime::DateFormat::Cookie));
sb.append("\nHttpHeader: ");
sb.append(dt.toString(DateTime::DateFormat::HttpHeader));
return sb.data();
}
assert(false);
}
String sret = DebuggerClient::FormatVariable(ret, -1);
if (strcmp(format, "hex") == 0 || strcmp(format, "x") == 0 ||
strcmp(format, "oct") == 0) {
StringBuffer sb;
for (int i = 0; i < sret.size(); i++) {
char ch = sret[i];
if (isprint(ch)) {
sb.append(ch);
} else {
char buf[6];
if (strcmp(format, "oct") == 0) {
snprintf(buf, sizeof(buf), "\\%03o", ch);
} else {
snprintf(buf, sizeof(buf), "\\x%02x", ch);
}
sb.append(buf);
}
}
return sb.data() ? sb.data() : "";
}
if (strcmp(format, "time") == 0) {
DateTime dt;
int64_t ts = -1;
if (dt.fromString(ret.toString(), SmartResource<TimeZone>())) {
bool err;
ts = dt.toTimeStamp(err);
}
return String(ts).data();
}
assert(false);
return "";
}
///////////////////////////////////////////////////////////////////////////////
void CmdPrint::sendImpl(DebuggerThriftBuffer &thrift) {
DebuggerCommand::sendImpl(thrift);
if (m_printLevel > 0) {
g_context->setDebuggerPrintLevel(m_printLevel);
}
{
String sdata;
DebuggerWireHelpers::WireSerialize(m_ret, sdata);
thrift.write(sdata);
}
if (m_printLevel > 0) {
g_context->setDebuggerPrintLevel(-1);
}
thrift.write(m_output);
thrift.write(m_frame);
thrift.write(m_bypassAccessCheck);
thrift.write(m_printLevel);
thrift.write(m_noBreak);
}
void CmdPrint::recvImpl(DebuggerThriftBuffer &thrift) {
DebuggerCommand::recvImpl(thrift);
{
String sdata;
thrift.read(sdata);
int error = DebuggerWireHelpers::WireUnserialize(sdata, m_ret);
if (error) {
m_ret = uninit_null();
}
if (error == DebuggerWireHelpers::HitLimit) {
m_wireError = "Hit unserialization limit. "
"Try with smaller print level";
}
}
thrift.read(m_output);
thrift.read(m_frame);
thrift.read(m_bypassAccessCheck);
thrift.read(m_printLevel);
thrift.read(m_noBreak);
}
void CmdPrint::list(DebuggerClient &client) {
if (client.arg(1, "clear")) {
client.addCompletion("all");
return;
}
client.addCompletion(DebuggerClient::AutoCompleteCode);
if (client.argCount() == 0) {
client.addCompletion(Formats);
client.addCompletion("always");
client.addCompletion("list");
client.addCompletion("clear");
} else if (client.argCount() == 1 && client.arg(1, "always")) {
client.addCompletion(Formats);
}
}
void CmdPrint::help(DebuggerClient &client) {
client.helpTitle("Print Command");
client.helpCmds(
"[p]rint {php}", "prints result of PHP code",
"[p]rint r {php}", "prints result of PHP code, (print_r)",
"[p]rint v {php}", "prints result of PHP code, (var_dump)",
"[p]rint x {php}", "prints hex encoded string or number",
"[p]rint [h]ex {php}", "prints hex encoded string or number",
"[p]rint [o]ct {php}", "prints octal encoded string or number",
"[p]rint [d]ec {php}", "prints as signed integer",
"[p]rint [u]nsigned {php}", "prints as unsigned integer",
"[p]rint [t]ime {php}", "converts between time and timestamp",
"", "",
"[p]rint [a]lways {above}", "adds a watch expression at break",
"[p]rint [l]ist", "lists watch expressions",
"[p]rint [c]lear {index}", "clears a watch expression",
"[p]rint [c]lear [a]ll", "clears all watch expressions",
nullptr
);
client.helpBody(
"Prints result of an expression in certain format. If '[a]lways' is "
"specified, the expression will be added to a watch list. At every break, "
"either at a breakpoint or caused by step commands, these expressions "
"will be evaluated and printed out."
);
}
void CmdPrint::processList(DebuggerClient &client) {
DebuggerClient::WatchPtrVec &watches = client.getWatches();
for (int i = 0; i < (int)watches.size(); i++) {
client.print(" %d %s %s", i + 1,
StringUtil::Pad(watches[i]->first, 8, " ",
StringUtil::PadType::Left).data(),
watches[i]->second.c_str());
}
if (watches.empty()) {
client.tutorial(
"Use '[p]rint [a]lways ...' to set new watch expressions. "
"Use '[p]rint ?|[h]elp' to read how to set them. "
);
} else {
client.tutorial(
"Use '[p]rint [c]lear {index}|[a]ll' to remove watch expression(s). "
);
}
}
void CmdPrint::processClear(DebuggerClient &client) {
DebuggerClient::WatchPtrVec &watches = client.getWatches();
if (watches.empty()) {
client.error("There is no watch expression to clear.");
client.tutorial(
"Use '[p]rint [a]lways ...' to set new watch expressions. "
"Use '[p]rint ?|[h]elp' to read how to set them. "
);
return;
}
if (client.arg(2, "all")) {
watches.clear();
client.info("All watch expressions are cleared.");
return;
}
string snum = client.argValue(2);
if (!DebuggerClient::IsValidNumber(snum)) {
client.error("'[p]rint [c]lear' needs an {index} argument.");
client.tutorial(
"You will have to run '[p]rint [l]ist' first to see a list of valid "
"numbers or indices to specify."
);
return;
}
int num = atoi(snum.c_str()) - 1;
if (num < 0 || num >= (int)watches.size()) {
client.error("\"%s\" is not a valid index. Choose one from this list:",
snum.c_str());
processList(client);
return;
}
watches.erase(watches.begin() + num);
}
Variant CmdPrint::processWatch(DebuggerClient &client, const char *format,
const std::string &php) {
m_body = php;
m_frame = client.getFrame();
m_noBreak = true;
CmdPrintPtr res = client.xend<CmdPrint>(this);
if (!res->m_output.empty()) {
client.output(res->m_output);
}
return res->m_ret;
}
void CmdPrint::handleReply(DebuggerClient &client) {
if (!m_output.empty()) {
client.output(m_output);
}
client.output(m_ret.toString());
}
void CmdPrint::onClient(DebuggerClient &client) {
if (DebuggerCommand::displayedHelp(client)) return;
if (client.argCount() == 0) {
help(client);
return;
}
int index = 1;
if (client.arg(1, "always")) {
m_isForWatch = true;
if (client.argCount() == 1) {
client.error("'[p]rint [a]lways' needs an expression to watch.");
return;
}
index++;
} else if (client.arg(1, "list")) {
m_isForWatch = true;
processList(client);
return;
} else if (client.arg(1, "clear")) {
m_isForWatch = true;
processClear(client);
return;
}
const char *format = nullptr;
for (const char **fmt = Formats; *fmt; fmt++) {
if (client.arg(index, *fmt)) {
format = *fmt;
index++;
break;
}
}
m_body = client.lineRest(index);
if (m_isForWatch) {
client.addWatch(format, m_body);
return;
}
m_bypassAccessCheck = client.getDebuggerClientBypassCheck();
m_printLevel = client.getDebuggerClientPrintLevel();
assert(m_printLevel <= 0 || m_printLevel >= DebuggerClient::MinPrintLevel);
m_frame = client.getFrame();
CmdPrintPtr res = client.xendWithNestedExecution<CmdPrint>(this);
m_output = res->m_output;
m_ret = res->m_ret;
if (!m_output.empty()) {
client.output(m_output);
}
client.output(FormatResult(format, m_ret));
}
// NB: unlike most other commands, the client expects that more interrupts
// can occur while we're doing the server-side work for a print.
bool CmdPrint::onServer(DebuggerProxy &proxy) {
PCFilter* locSave = g_vmContext->m_lastLocFilter;
g_vmContext->m_lastLocFilter = new PCFilter();
g_vmContext->setDebuggerBypassCheck(m_bypassAccessCheck);
{
EvalBreakControl eval(m_noBreak);
bool failed;
m_ret =
proxy.ExecutePHP(DebuggerProxy::MakePHPReturn(m_body),
m_output, m_frame, failed,
DebuggerProxy::ExecutePHPFlagsAtInterrupt |
(!proxy.isLocal() ? DebuggerProxy::ExecutePHPFlagsLog :
DebuggerProxy::ExecutePHPFlagsNone));
}
g_vmContext->setDebuggerBypassCheck(false);
delete g_vmContext->m_lastLocFilter;
g_vmContext->m_lastLocFilter = locSave;
return proxy.sendToClient(this);
}
///////////////////////////////////////////////////////////////////////////////
}}