PageRenderTime 44ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/hphp/runtime/debugger/cmd/cmd_print.cpp

https://github.com/tstarling/hiphop-php
C++ | 368 lines | 318 code | 30 blank | 20 comment | 72 complexity | 65b60381b829cdd45a73b923b4729af8 MD5 | raw file
  1. /*
  2. +----------------------------------------------------------------------+
  3. | HipHop for PHP |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 2010-2014 Facebook, Inc. (http://www.facebook.com) |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 3.01 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.php.net/license/3_01.txt |
  11. | If you did not receive a copy of the PHP license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@php.net so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. */
  16. #include "hphp/runtime/debugger/cmd/cmd_print.h"
  17. #include "hphp/runtime/base/datetime.h"
  18. #include "hphp/runtime/base/string-util.h"
  19. #include "hphp/runtime/vm/debugger-hook.h"
  20. #include "hphp/runtime/base/array-init.h"
  21. namespace HPHP { namespace Eval {
  22. ///////////////////////////////////////////////////////////////////////////////
  23. TRACE_SET_MOD(debugger);
  24. const char *CmdPrint::Formats[] = {
  25. "r", "v", "x", "hex", "oct", "dec", "unsigned", "time", nullptr
  26. };
  27. std::string CmdPrint::FormatResult(const char *format, const Variant& ret) {
  28. if (format == nullptr) {
  29. String sret = DebuggerClient::FormatVariable(ret, -1);
  30. return std::string(sret.data(), sret.size());
  31. }
  32. if (strcmp(format, "r") == 0) {
  33. String sret = DebuggerClient::FormatVariable(ret, -1, 'r');
  34. return std::string(sret.data(), sret.size());
  35. }
  36. if (strcmp(format, "v") == 0) {
  37. String sret = DebuggerClient::FormatVariable(ret, -1, 'v');
  38. return std::string(sret.data(), sret.size());
  39. }
  40. if (strcmp(format, "dec") == 0 ||
  41. strcmp(format, "unsigned") == 0 ||
  42. ret.isInteger()) {
  43. int64_t nret = ret.toInt64();
  44. char buf[64];
  45. if (strcmp(format, "hex") == 0 || strcmp(format, "x") == 0) {
  46. snprintf(buf, sizeof(buf), "%" PRIx64, nret);
  47. return buf;
  48. }
  49. if (strcmp(format, "oct") == 0) {
  50. snprintf(buf, sizeof(buf), "%" PRIo64, nret);
  51. return buf;
  52. }
  53. if (strcmp(format, "dec") == 0) {
  54. snprintf(buf, sizeof(buf), "%" PRId64, nret);
  55. return buf;
  56. }
  57. if (strcmp(format, "unsigned") == 0) {
  58. snprintf(buf, sizeof(buf), "%" PRIu64, nret);
  59. return buf;
  60. }
  61. if (strcmp(format, "time") == 0) {
  62. StringBuffer sb;
  63. DateTime dt(nret);
  64. sb.append("RFC822: ");
  65. sb.append(dt.toString(DateTime::DateFormat::RFC822));
  66. sb.append("\nRFC850: ");
  67. sb.append(dt.toString(DateTime::DateFormat::RFC850));
  68. sb.append("\nRFC1036: ");
  69. sb.append(dt.toString(DateTime::DateFormat::RFC1036));
  70. sb.append("\nRFC1123/RSS: ");
  71. sb.append(dt.toString(DateTime::DateFormat::RFC1123));
  72. sb.append("\nRFC2822: ");
  73. sb.append(dt.toString(DateTime::DateFormat::RFC2822));
  74. sb.append("\nRFC3339/ATOM/W3C: ");
  75. sb.append(dt.toString(DateTime::DateFormat::RFC3339));
  76. sb.append("\nISO8601: ");
  77. sb.append(dt.toString(DateTime::DateFormat::ISO8601));
  78. sb.append("\nCookie: ");
  79. sb.append(dt.toString(DateTime::DateFormat::Cookie));
  80. sb.append("\nHttpHeader: ");
  81. sb.append(dt.toString(DateTime::DateFormat::HttpHeader));
  82. return sb.data();
  83. }
  84. assert(false);
  85. }
  86. String sret = DebuggerClient::FormatVariable(ret, -1);
  87. if (strcmp(format, "hex") == 0 || strcmp(format, "x") == 0 ||
  88. strcmp(format, "oct") == 0) {
  89. StringBuffer sb;
  90. for (int i = 0; i < sret.size(); i++) {
  91. char ch = sret[i];
  92. if (isprint(ch)) {
  93. sb.append(ch);
  94. } else {
  95. char buf[6];
  96. if (strcmp(format, "oct") == 0) {
  97. snprintf(buf, sizeof(buf), "\\%03o", ch);
  98. } else {
  99. snprintf(buf, sizeof(buf), "\\x%02x", ch);
  100. }
  101. sb.append(buf);
  102. }
  103. }
  104. return sb.data() ? sb.data() : "";
  105. }
  106. if (strcmp(format, "time") == 0) {
  107. DateTime dt;
  108. int64_t ts = -1;
  109. if (dt.fromString(ret.toString(), SmartResource<TimeZone>())) {
  110. bool err;
  111. ts = dt.toTimeStamp(err);
  112. }
  113. return String(ts).data();
  114. }
  115. assert(false);
  116. return "";
  117. }
  118. ///////////////////////////////////////////////////////////////////////////////
  119. void CmdPrint::sendImpl(DebuggerThriftBuffer &thrift) {
  120. DebuggerCommand::sendImpl(thrift);
  121. if (m_printLevel > 0) {
  122. g_context->debuggerSettings.printLevel = m_printLevel;
  123. }
  124. {
  125. String sdata;
  126. DebuggerWireHelpers::WireSerialize(m_ret, sdata);
  127. thrift.write(sdata);
  128. }
  129. if (m_printLevel > 0) {
  130. g_context->debuggerSettings.printLevel = -1;
  131. }
  132. thrift.write(m_output);
  133. thrift.write(m_frame);
  134. thrift.write(m_bypassAccessCheck);
  135. thrift.write(m_printLevel);
  136. thrift.write(m_noBreak);
  137. }
  138. void CmdPrint::recvImpl(DebuggerThriftBuffer &thrift) {
  139. DebuggerCommand::recvImpl(thrift);
  140. {
  141. String sdata;
  142. thrift.read(sdata);
  143. int error = DebuggerWireHelpers::WireUnserialize(sdata, m_ret);
  144. if (error) {
  145. m_ret = uninit_null();
  146. }
  147. if (error == DebuggerWireHelpers::HitLimit) {
  148. m_wireError = "Hit unserialization limit. "
  149. "Try with smaller print level";
  150. }
  151. }
  152. thrift.read(m_output);
  153. thrift.read(m_frame);
  154. thrift.read(m_bypassAccessCheck);
  155. thrift.read(m_printLevel);
  156. thrift.read(m_noBreak);
  157. }
  158. void CmdPrint::list(DebuggerClient &client) {
  159. if (client.arg(1, "clear")) {
  160. client.addCompletion("all");
  161. return;
  162. }
  163. client.addCompletion(DebuggerClient::AutoCompleteCode);
  164. if (client.argCount() == 0) {
  165. client.addCompletion(Formats);
  166. client.addCompletion("always");
  167. client.addCompletion("list");
  168. client.addCompletion("clear");
  169. } else if (client.argCount() == 1 && client.arg(1, "always")) {
  170. client.addCompletion(Formats);
  171. }
  172. }
  173. void CmdPrint::help(DebuggerClient &client) {
  174. client.helpTitle("Print Command");
  175. client.helpCmds(
  176. "[p]rint {php}", "prints result of PHP code",
  177. "[p]rint r {php}", "prints result of PHP code, (print_r)",
  178. "[p]rint v {php}", "prints result of PHP code, (var_dump)",
  179. "[p]rint x {php}", "prints hex encoded string or number",
  180. "[p]rint [h]ex {php}", "prints hex encoded string or number",
  181. "[p]rint [o]ct {php}", "prints octal encoded string or number",
  182. "[p]rint [d]ec {php}", "prints as signed integer",
  183. "[p]rint [u]nsigned {php}", "prints as unsigned integer",
  184. "[p]rint [t]ime {php}", "converts between time and timestamp",
  185. "", "",
  186. "[p]rint [a]lways {above}", "adds a watch expression at break",
  187. "[p]rint [l]ist", "lists watch expressions",
  188. "[p]rint [c]lear {index}", "clears a watch expression",
  189. "[p]rint [c]lear [a]ll", "clears all watch expressions",
  190. nullptr
  191. );
  192. client.helpBody(
  193. "Prints result of an expression in certain format. If '[a]lways' is "
  194. "specified, the expression will be added to a watch list. At every break, "
  195. "either at a breakpoint or caused by step commands, these expressions "
  196. "will be evaluated and printed out."
  197. );
  198. }
  199. void CmdPrint::processList(DebuggerClient &client) {
  200. DebuggerClient::WatchPtrVec &watches = client.getWatches();
  201. for (int i = 0; i < (int)watches.size(); i++) {
  202. client.print(" %d %s %s", i + 1,
  203. StringUtil::Pad(watches[i]->first, 8, " ",
  204. StringUtil::PadType::Left).data(),
  205. watches[i]->second.c_str());
  206. }
  207. if (watches.empty()) {
  208. client.tutorial(
  209. "Use '[p]rint [a]lways ...' to set new watch expressions. "
  210. "Use '[p]rint ?|[h]elp' to read how to set them. "
  211. );
  212. } else {
  213. client.tutorial(
  214. "Use '[p]rint [c]lear {index}|[a]ll' to remove watch expression(s). "
  215. );
  216. }
  217. }
  218. void CmdPrint::processClear(DebuggerClient &client) {
  219. DebuggerClient::WatchPtrVec &watches = client.getWatches();
  220. if (watches.empty()) {
  221. client.error("There is no watch expression to clear.");
  222. client.tutorial(
  223. "Use '[p]rint [a]lways ...' to set new watch expressions. "
  224. "Use '[p]rint ?|[h]elp' to read how to set them. "
  225. );
  226. return;
  227. }
  228. if (client.arg(2, "all")) {
  229. watches.clear();
  230. client.info("All watch expressions are cleared.");
  231. return;
  232. }
  233. std::string snum = client.argValue(2);
  234. if (!DebuggerClient::IsValidNumber(snum)) {
  235. client.error("'[p]rint [c]lear' needs an {index} argument.");
  236. client.tutorial(
  237. "You will have to run '[p]rint [l]ist' first to see a list of valid "
  238. "numbers or indices to specify."
  239. );
  240. return;
  241. }
  242. int num = atoi(snum.c_str()) - 1;
  243. if (num < 0 || num >= (int)watches.size()) {
  244. client.error("\"%s\" is not a valid index. Choose one from this list:",
  245. snum.c_str());
  246. processList(client);
  247. return;
  248. }
  249. watches.erase(watches.begin() + num);
  250. }
  251. Variant CmdPrint::processWatch(DebuggerClient &client, const char *format,
  252. const std::string &php) {
  253. m_body = php;
  254. m_frame = client.getFrame();
  255. m_noBreak = true;
  256. auto res = client.xend<CmdPrint>(this);
  257. if (!res->m_output.empty()) {
  258. client.output(res->m_output);
  259. }
  260. return res->m_ret;
  261. }
  262. void CmdPrint::handleReply(DebuggerClient &client) {
  263. if (!m_output.empty()) {
  264. client.output(m_output);
  265. }
  266. client.output(m_ret.toString());
  267. }
  268. void CmdPrint::onClient(DebuggerClient &client) {
  269. if (DebuggerCommand::displayedHelp(client)) return;
  270. if (client.argCount() == 0) {
  271. help(client);
  272. return;
  273. }
  274. int index = 1;
  275. if (client.arg(1, "always")) {
  276. m_isForWatch = true;
  277. if (client.argCount() == 1) {
  278. client.error("'[p]rint [a]lways' needs an expression to watch.");
  279. return;
  280. }
  281. index++;
  282. } else if (client.arg(1, "list")) {
  283. m_isForWatch = true;
  284. processList(client);
  285. return;
  286. } else if (client.arg(1, "clear")) {
  287. m_isForWatch = true;
  288. processClear(client);
  289. return;
  290. }
  291. const char *format = nullptr;
  292. for (const char **fmt = Formats; *fmt; fmt++) {
  293. if (client.arg(index, *fmt)) {
  294. format = *fmt;
  295. index++;
  296. break;
  297. }
  298. }
  299. m_body = client.lineRest(index);
  300. if (m_isForWatch) {
  301. client.addWatch(format, m_body);
  302. return;
  303. }
  304. m_bypassAccessCheck = client.getDebuggerClientBypassCheck();
  305. m_printLevel = client.getDebuggerClientPrintLevel();
  306. assert(m_printLevel <= 0 || m_printLevel >= DebuggerClient::MinPrintLevel);
  307. m_frame = client.getFrame();
  308. auto res = client.xendWithNestedExecution<CmdPrint>(this);
  309. m_output = res->m_output;
  310. m_ret = res->m_ret;
  311. if (!m_output.empty()) {
  312. client.output(m_output);
  313. }
  314. client.output(FormatResult(format, m_ret));
  315. }
  316. // NB: unlike most other commands, the client expects that more interrupts
  317. // can occur while we're doing the server-side work for a print.
  318. bool CmdPrint::onServer(DebuggerProxy &proxy) {
  319. PCFilter* locSave = g_context->m_lastLocFilter;
  320. g_context->m_lastLocFilter = new PCFilter();
  321. g_context->debuggerSettings.bypassCheck = m_bypassAccessCheck;
  322. {
  323. EvalBreakControl eval(m_noBreak);
  324. bool failed;
  325. m_ret =
  326. proxy.ExecutePHP(DebuggerProxy::MakePHPReturn(m_body),
  327. m_output, m_frame, failed,
  328. DebuggerProxy::ExecutePHPFlagsAtInterrupt |
  329. (!proxy.isLocal() ? DebuggerProxy::ExecutePHPFlagsLog :
  330. DebuggerProxy::ExecutePHPFlagsNone));
  331. }
  332. g_context->debuggerSettings.bypassCheck = false;
  333. delete g_context->m_lastLocFilter;
  334. g_context->m_lastLocFilter = locSave;
  335. return proxy.sendToClient(this);
  336. }
  337. ///////////////////////////////////////////////////////////////////////////////
  338. }}