Skip to content

Commit

Permalink
[hphpd] add print_r format to print command
Browse files Browse the repository at this point in the history
Summary:
We have a level limit on print command but, the '=' cmd could still
trigger a huge serialization since it could endup in print_r() with
no checking on serialization limit. But print_r() format is
generally prettier than var_dump format used by print command.

This diff adds print_r format to print command and uses it as default,
var_dump format can be used by: p v $toprint. So mostly people should
stick to print command, instead of '='. This diff also makes sure when
'=' is trying to serialize, it observe the serialization limit and fail
faster.

Test Plan:
make fast_tests
manually verified with a few cases including the error case in hphpi

Reviewers: myang, mwilliams

Reviewed By: myang

CC: ps, mwilliams, myang

Differential Revision: 342394

Task ID: 754526
  • Loading branch information
qigao authored and macvicar committed Oct 18, 2011
1 parent d5c2d1c commit f7457f5
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
10 changes: 8 additions & 2 deletions src/runtime/eval/debugger/cmd/cmd_print.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace HPHP { namespace Eval {
///////////////////////////////////////////////////////////////////////////////

const char *CmdPrint::Formats[] = {
"x", "hex", "oct", "dec", "unsigned", "time", NULL
"x", "v", "hex", "oct", "dec", "unsigned", "time", NULL
};

std::string CmdPrint::FormatResult(const char *format, CVarRef ret) {
Expand All @@ -33,6 +33,11 @@ std::string CmdPrint::FormatResult(const char *format, CVarRef ret) {
return string(sret.data(), sret.size());
}

if (strcmp(format, "v") == 0) {
String sret = DebuggerClient::FormatVariable(ret, -1, true);
return string(sret.data(), sret.size());
}

if (strcmp(format, "dec") == 0 ||
strcmp(format, "unsigned") == 0 ||
ret.isInteger()) {
Expand Down Expand Up @@ -161,7 +166,8 @@ void CmdPrint::list(DebuggerClient *client) {
bool CmdPrint::help(DebuggerClient *client) {
client->helpTitle("Print Command");
client->helpCmds(
"[p]rint {php}", "prints result of PHP code",
"[p]rint {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",
Expand Down
12 changes: 8 additions & 4 deletions src/runtime/eval/debugger/debugger_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -272,11 +272,15 @@ bool DebuggerClient::IsValidNumber(const std::string &arg) {
return true;
}

String DebuggerClient::FormatVariable(CVarRef v, int maxlen /* = 80 */) {
String DebuggerClient::FormatVariable(CVarRef v, int maxlen /* = 80 */,
bool vardump /* = false */) {
String value;
if (maxlen <= 0) {
try {
VariableSerializer vs(VariableSerializer::VarDump, 0, 2);
VariableSerializer::Type t = vardump ?
VariableSerializer::VarDump :
VariableSerializer::PrintR;
VariableSerializer vs(t, 0, 2);
value = vs.serialize(v, true);
} catch (StringBufferLimitException &e) {
value = "Serialization limit reached";
Expand Down Expand Up @@ -1786,8 +1790,8 @@ void DebuggerClient::loadConfig() {
}

m_printFunction = (boost::format(
"(function_exists(\"%s\") ? %s($_) : print_r($_));") % pprint % pprint)
.str();
"(function_exists(\"%s\") ? %s($_) : print_r(print_r($_, true)));")
% pprint % pprint).str();

m_config["Tutorial"]["Visited"].get(m_tutorialVisited);

Expand Down
3 changes: 2 additions & 1 deletion src/runtime/eval/debugger/debugger_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ class DebuggerClient {
static void AdjustScreenMetrics();
static bool Match(const char *input, const char *cmd);
static bool IsValidNumber(const std::string &arg);
static String FormatVariable(CVarRef v, int maxlen = 80);
static String FormatVariable(CVarRef v, int maxlen = 80,
bool vardump = false);
static String FormatInfoVec(const IDebuggable::InfoVec &info,
int *nameLen = NULL);
static String FormatTitle(const char *title);
Expand Down

0 comments on commit f7457f5

Please sign in to comment.