phpdbg_set.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 7 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2018 The PHP Group |
  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. | Authors: Felipe Pena <felipe@php.net> |
  16. | Authors: Joe Watkins <joe.watkins@live.co.uk> |
  17. | Authors: Bob Weinand <bwoebi@php.net> |
  18. +----------------------------------------------------------------------+
  19. */
  20. #include "phpdbg.h"
  21. #include "phpdbg_cmd.h"
  22. #include "phpdbg_set.h"
  23. #include "phpdbg_utils.h"
  24. #include "phpdbg_bp.h"
  25. #include "phpdbg_prompt.h"
  26. ZEND_EXTERN_MODULE_GLOBALS(phpdbg)
  27. #define PHPDBG_SET_COMMAND_D(f, h, a, m, l, s, flags) \
  28. PHPDBG_COMMAND_D_EXP(f, h, a, m, l, s, &phpdbg_prompt_commands[17], flags)
  29. const phpdbg_command_t phpdbg_set_commands[] = {
  30. PHPDBG_SET_COMMAND_D(prompt, "usage: set prompt [<string>]", 'p', set_prompt, NULL, "|s", 0),
  31. PHPDBG_SET_COMMAND_D(pagination, "usage: set pagination [<on|off>]", 'P', set_pagination, NULL, "|b", PHPDBG_ASYNC_SAFE),
  32. #ifndef _WIN32
  33. PHPDBG_SET_COMMAND_D(color, "usage: set color <element> <color>", 'c', set_color, NULL, "ss", PHPDBG_ASYNC_SAFE),
  34. PHPDBG_SET_COMMAND_D(colors, "usage: set colors [<on|off>]", 'C', set_colors, NULL, "|b", PHPDBG_ASYNC_SAFE),
  35. #endif
  36. PHPDBG_SET_COMMAND_D(oplog, "usage: set oplog [<output>]", 'O', set_oplog, NULL, "|s", 0),
  37. PHPDBG_SET_COMMAND_D(break, "usage: set break id [<on|off>]", 'b', set_break, NULL, "l|b", PHPDBG_ASYNC_SAFE),
  38. PHPDBG_SET_COMMAND_D(breaks, "usage: set breaks [<on|off>]", 'B', set_breaks, NULL, "|b", PHPDBG_ASYNC_SAFE),
  39. PHPDBG_SET_COMMAND_D(quiet, "usage: set quiet [<on|off>]", 'q', set_quiet, NULL, "|b", PHPDBG_ASYNC_SAFE),
  40. PHPDBG_SET_COMMAND_D(stepping, "usage: set stepping [<line|op>]", 's', set_stepping, NULL, "|s", PHPDBG_ASYNC_SAFE),
  41. PHPDBG_SET_COMMAND_D(refcount, "usage: set refcount [<on|off>]", 'r', set_refcount, NULL, "|b", PHPDBG_ASYNC_SAFE),
  42. PHPDBG_SET_COMMAND_D(lines, "usage: set lines [<number>]", 'l', set_lines, NULL, "|l", PHPDBG_ASYNC_SAFE),
  43. PHPDBG_END_COMMAND
  44. };
  45. PHPDBG_SET(prompt) /* {{{ */
  46. {
  47. if (!param || param->type == EMPTY_PARAM) {
  48. phpdbg_writeln("setprompt", "str=\"%s\"", "Current prompt: %s", phpdbg_get_prompt());
  49. } else {
  50. phpdbg_set_prompt(param->str);
  51. }
  52. return SUCCESS;
  53. } /* }}} */
  54. PHPDBG_SET(pagination) /* {{{ */
  55. {
  56. if (!param || param->type == EMPTY_PARAM) {
  57. phpdbg_writeln("setpagination", "active=\"%s\"", "Pagination %s", PHPDBG_G(flags) & PHPDBG_HAS_PAGINATION ? "on" : "off");
  58. } else switch (param->type) {
  59. case NUMERIC_PARAM: {
  60. if (param->num) {
  61. PHPDBG_G(flags) |= PHPDBG_HAS_PAGINATION;
  62. } else {
  63. PHPDBG_G(flags) &= ~PHPDBG_HAS_PAGINATION;
  64. }
  65. } break;
  66. default:
  67. phpdbg_error("setpagination", "type=\"wrongargs\"", "set pagination used incorrectly: set pagination <on|off>");
  68. }
  69. return SUCCESS;
  70. } /* }}} */
  71. PHPDBG_SET(lines) /* {{{ */
  72. {
  73. if (!param || param->type == EMPTY_PARAM) {
  74. phpdbg_writeln("setlines", "active=\"%s\"", "Lines %ld", PHPDBG_G(lines));
  75. } else switch (param->type) {
  76. case NUMERIC_PARAM: {
  77. PHPDBG_G(lines) = param->num;
  78. } break;
  79. default:
  80. phpdbg_error("setlines", "type=\"wrongargs\"", "set lines used incorrectly: set lines <number>");
  81. }
  82. return SUCCESS;
  83. } /* }}} */
  84. PHPDBG_SET(break) /* {{{ */
  85. {
  86. switch (param->type) {
  87. case NUMERIC_PARAM: {
  88. if (param->next) {
  89. if (param->next->num) {
  90. phpdbg_enable_breakpoint(param->num);
  91. } else {
  92. phpdbg_disable_breakpoint(param->num);
  93. }
  94. } else {
  95. phpdbg_breakbase_t *brake = phpdbg_find_breakbase(param->num);
  96. if (brake) {
  97. phpdbg_writeln("setbreak", "id=\"%ld\" active=\"%s\"", "Breakpoint #%ld %s", param->num, brake->disabled ? "off" : "on");
  98. } else {
  99. phpdbg_error("setbreak", "type=\"nobreak\" id=\"%ld\"", "Failed to find breakpoint #%ld", param->num);
  100. }
  101. }
  102. } break;
  103. default:
  104. phpdbg_error("setbreak", "type=\"wrongargs\"", "set break used incorrectly: set break [id] <on|off>");
  105. }
  106. return SUCCESS;
  107. } /* }}} */
  108. PHPDBG_SET(breaks) /* {{{ */
  109. {
  110. if (!param || param->type == EMPTY_PARAM) {
  111. phpdbg_writeln("setbreaks", "active=\"%s\"", "Breakpoints %s",PHPDBG_G(flags) & PHPDBG_IS_BP_ENABLED ? "on" : "off");
  112. } else switch (param->type) {
  113. case NUMERIC_PARAM: {
  114. if (param->num) {
  115. phpdbg_enable_breakpoints();
  116. } else {
  117. phpdbg_disable_breakpoints();
  118. }
  119. } break;
  120. default:
  121. phpdbg_error("setbreaks", "type=\"wrongargs\"", "set breaks used incorrectly: set breaks <on|off>");
  122. }
  123. return SUCCESS;
  124. } /* }}} */
  125. #ifndef _WIN32
  126. PHPDBG_SET(color) /* {{{ */
  127. {
  128. const phpdbg_color_t *color = phpdbg_get_color(param->next->str, param->next->len);
  129. if (!color) {
  130. phpdbg_error("setcolor", "type=\"nocolor\"", "Failed to find the requested color (%s)", param->next->str);
  131. return SUCCESS;
  132. }
  133. switch (phpdbg_get_element(param->str, param->len)) {
  134. case PHPDBG_COLOR_PROMPT:
  135. phpdbg_notice("setcolor", "type=\"prompt\" color=\"%s\" code=\"%s\"", "setting prompt color to %s (%s)", color->name, color->code);
  136. if (PHPDBG_G(prompt)[1]) {
  137. free(PHPDBG_G(prompt)[1]);
  138. PHPDBG_G(prompt)[1]=NULL;
  139. }
  140. phpdbg_set_color(PHPDBG_COLOR_PROMPT, color);
  141. break;
  142. case PHPDBG_COLOR_ERROR:
  143. phpdbg_notice("setcolor", "type=\"error\" color=\"%s\" code=\"%s\"", "setting error color to %s (%s)", color->name, color->code);
  144. phpdbg_set_color(PHPDBG_COLOR_ERROR, color);
  145. break;
  146. case PHPDBG_COLOR_NOTICE:
  147. phpdbg_notice("setcolor", "type=\"notice\" color=\"%s\" code=\"%s\"", "setting notice color to %s (%s)", color->name, color->code);
  148. phpdbg_set_color(PHPDBG_COLOR_NOTICE, color);
  149. break;
  150. default:
  151. phpdbg_error("setcolor", "type=\"invalidtype\"", "Failed to find the requested element (%s)", param->str);
  152. }
  153. return SUCCESS;
  154. } /* }}} */
  155. PHPDBG_SET(colors) /* {{{ */
  156. {
  157. if (!param || param->type == EMPTY_PARAM) {
  158. phpdbg_writeln("setcolors", "active=\"%s\"", "Colors %s", PHPDBG_G(flags) & PHPDBG_IS_COLOURED ? "on" : "off");
  159. } else switch (param->type) {
  160. case NUMERIC_PARAM: {
  161. if (param->num) {
  162. PHPDBG_G(flags) |= PHPDBG_IS_COLOURED;
  163. } else {
  164. PHPDBG_G(flags) &= ~PHPDBG_IS_COLOURED;
  165. }
  166. } break;
  167. default:
  168. phpdbg_error("setcolors", "type=\"wrongargs\"", "set colors used incorrectly: set colors <on|off>");
  169. }
  170. return SUCCESS;
  171. } /* }}} */
  172. #endif
  173. PHPDBG_SET(oplog) /* {{{ */
  174. {
  175. if (!param || param->type == EMPTY_PARAM) {
  176. phpdbg_notice("setoplog", "active=\"%s\"", "Oplog %s", PHPDBG_G(oplog) ? "on" : "off");
  177. } else switch (param->type) {
  178. case STR_PARAM: {
  179. /* open oplog */
  180. FILE *old = PHPDBG_G(oplog);
  181. PHPDBG_G(oplog) = fopen(param->str, "w+");
  182. if (!PHPDBG_G(oplog)) {
  183. phpdbg_error("setoplog", "type=\"openfailure\" file=\"%s\"", "Failed to open %s for oplog", param->str);
  184. PHPDBG_G(oplog) = old;
  185. } else {
  186. if (old) {
  187. phpdbg_notice("setoplog", "type=\"closingold\"", "Closing previously open oplog");
  188. fclose(old);
  189. }
  190. phpdbg_notice("setoplog", "file=\"%s\"", "Successfully opened oplog %s", param->str);
  191. }
  192. } break;
  193. phpdbg_default_switch_case();
  194. }
  195. return SUCCESS;
  196. } /* }}} */
  197. PHPDBG_SET(quiet) /* {{{ */
  198. {
  199. if (!param || param->type == EMPTY_PARAM) {
  200. phpdbg_writeln("setquiet", "active=\"%s\"", "Quietness %s", PHPDBG_G(flags) & PHPDBG_IS_QUIET ? "on" : "off");
  201. } else switch (param->type) {
  202. case NUMERIC_PARAM: {
  203. if (param->num) {
  204. PHPDBG_G(flags) |= PHPDBG_IS_QUIET;
  205. } else {
  206. PHPDBG_G(flags) &= ~PHPDBG_IS_QUIET;
  207. }
  208. } break;
  209. phpdbg_default_switch_case();
  210. }
  211. return SUCCESS;
  212. } /* }}} */
  213. PHPDBG_SET(stepping) /* {{{ */
  214. {
  215. if (!param || param->type == EMPTY_PARAM) {
  216. phpdbg_writeln("setstepping", "type=\"%s\"", "Stepping %s", PHPDBG_G(flags) & PHPDBG_STEP_OPCODE ? "opcode" : "line");
  217. } else switch (param->type) {
  218. case STR_PARAM: {
  219. if (param->len == sizeof("opcode") - 1 && !memcmp(param->str, "opcode", sizeof("opcode"))) {
  220. PHPDBG_G(flags) |= PHPDBG_STEP_OPCODE;
  221. } else if (param->len == sizeof("line") - 1 && !memcmp(param->str, "line", sizeof("line"))) {
  222. PHPDBG_G(flags) &= ~PHPDBG_STEP_OPCODE;
  223. } else {
  224. phpdbg_error("setstepping", "type=\"wrongargs\"", "usage set stepping [<opcode|line>]");
  225. }
  226. } break;
  227. phpdbg_default_switch_case();
  228. }
  229. return SUCCESS;
  230. } /* }}} */
  231. PHPDBG_SET(refcount) /* {{{ */
  232. {
  233. if (!param || param->type == EMPTY_PARAM) {
  234. phpdbg_writeln("setrefcount", "active=\"%s\"", "Showing refcounts %s", PHPDBG_G(flags) & PHPDBG_IS_QUIET ? "on" : "off");
  235. } else switch (param->type) {
  236. case NUMERIC_PARAM: {
  237. if (param->num) {
  238. PHPDBG_G(flags) |= PHPDBG_SHOW_REFCOUNTS;
  239. } else {
  240. PHPDBG_G(flags) &= ~PHPDBG_SHOW_REFCOUNTS;
  241. }
  242. } break;
  243. phpdbg_default_switch_case();
  244. }
  245. return SUCCESS;
  246. } /* }}} */