phpdbg_utils.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2016 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 <stdio.h>
  21. #include <ctype.h>
  22. #include <string.h>
  23. #include "zend.h"
  24. #include "php.h"
  25. #include "spprintf.h"
  26. #include "phpdbg.h"
  27. #include "phpdbg_opcode.h"
  28. #include "phpdbg_utils.h"
  29. #ifdef _WIN32
  30. # include "win32/time.h"
  31. #elif defined(HAVE_SYS_IOCTL_H)
  32. # include "sys/ioctl.h"
  33. # ifndef GWINSZ_IN_SYS_IOCTL
  34. # include <termios.h>
  35. # endif
  36. #endif
  37. ZEND_EXTERN_MODULE_GLOBALS(phpdbg);
  38. /* {{{ color structures */
  39. const static phpdbg_color_t colors[] = {
  40. PHPDBG_COLOR_D("none", "0;0"),
  41. PHPDBG_COLOR_D("white", "0;64"),
  42. PHPDBG_COLOR_D("white-bold", "1;64"),
  43. PHPDBG_COLOR_D("white-underline", "4;64"),
  44. PHPDBG_COLOR_D("red", "0;31"),
  45. PHPDBG_COLOR_D("red-bold", "1;31"),
  46. PHPDBG_COLOR_D("red-underline", "4;31"),
  47. PHPDBG_COLOR_D("green", "0;32"),
  48. PHPDBG_COLOR_D("green-bold", "1;32"),
  49. PHPDBG_COLOR_D("green-underline", "4;32"),
  50. PHPDBG_COLOR_D("yellow", "0;33"),
  51. PHPDBG_COLOR_D("yellow-bold", "1;33"),
  52. PHPDBG_COLOR_D("yellow-underline", "4;33"),
  53. PHPDBG_COLOR_D("blue", "0;34"),
  54. PHPDBG_COLOR_D("blue-bold", "1;34"),
  55. PHPDBG_COLOR_D("blue-underline", "4;34"),
  56. PHPDBG_COLOR_D("purple", "0;35"),
  57. PHPDBG_COLOR_D("purple-bold", "1;35"),
  58. PHPDBG_COLOR_D("purple-underline", "4;35"),
  59. PHPDBG_COLOR_D("cyan", "0;36"),
  60. PHPDBG_COLOR_D("cyan-bold", "1;36"),
  61. PHPDBG_COLOR_D("cyan-underline", "4;36"),
  62. PHPDBG_COLOR_D("black", "0;30"),
  63. PHPDBG_COLOR_D("black-bold", "1;30"),
  64. PHPDBG_COLOR_D("black-underline", "4;30"),
  65. PHPDBG_COLOR_END
  66. }; /* }}} */
  67. /* {{{ */
  68. const static phpdbg_element_t elements[] = {
  69. PHPDBG_ELEMENT_D("prompt", PHPDBG_COLOR_PROMPT),
  70. PHPDBG_ELEMENT_D("error", PHPDBG_COLOR_ERROR),
  71. PHPDBG_ELEMENT_D("notice", PHPDBG_COLOR_NOTICE),
  72. PHPDBG_ELEMENT_END
  73. }; /* }}} */
  74. PHPDBG_API int phpdbg_is_numeric(const char *str) /* {{{ */
  75. {
  76. if (!str)
  77. return 0;
  78. for (; *str; str++) {
  79. if (isspace(*str) || *str == '-') {
  80. continue;
  81. }
  82. return isdigit(*str);
  83. }
  84. return 0;
  85. } /* }}} */
  86. PHPDBG_API int phpdbg_is_empty(const char *str) /* {{{ */
  87. {
  88. if (!str)
  89. return 1;
  90. for (; *str; str++) {
  91. if (isspace(*str)) {
  92. continue;
  93. }
  94. return 0;
  95. }
  96. return 1;
  97. } /* }}} */
  98. PHPDBG_API int phpdbg_is_addr(const char *str) /* {{{ */
  99. {
  100. return str[0] && str[1] && memcmp(str, "0x", 2) == 0;
  101. } /* }}} */
  102. PHPDBG_API int phpdbg_is_class_method(const char *str, size_t len, char **class, char **method) /* {{{ */
  103. {
  104. char *sep = NULL;
  105. if (strstr(str, "#") != NULL)
  106. return 0;
  107. if (strstr(str, " ") != NULL)
  108. return 0;
  109. sep = strstr(str, "::");
  110. if (!sep || sep == str || sep+2 == str+len-1) {
  111. return 0;
  112. }
  113. if (class != NULL) {
  114. if (str[0] == '\\') {
  115. str++;
  116. len--;
  117. }
  118. *class = estrndup(str, sep - str);
  119. (*class)[sep - str] = 0;
  120. }
  121. if (method != NULL) {
  122. *method = estrndup(sep+2, str + len - (sep + 2));
  123. }
  124. return 1;
  125. } /* }}} */
  126. PHPDBG_API char *phpdbg_resolve_path(const char *path TSRMLS_DC) /* {{{ */
  127. {
  128. char resolved_name[MAXPATHLEN];
  129. if (expand_filepath(path, resolved_name TSRMLS_CC) == NULL) {
  130. return NULL;
  131. }
  132. return estrdup(resolved_name);
  133. } /* }}} */
  134. PHPDBG_API const char *phpdbg_current_file(TSRMLS_D) /* {{{ */
  135. {
  136. const char *file = zend_get_executed_filename(TSRMLS_C);
  137. if (memcmp(file, "[no active file]", sizeof("[no active file]")) == 0) {
  138. return PHPDBG_G(exec);
  139. }
  140. return file;
  141. } /* }}} */
  142. PHPDBG_API const zend_function *phpdbg_get_function(const char *fname, const char *cname TSRMLS_DC) /* {{{ */
  143. {
  144. zend_function *func = NULL;
  145. size_t fname_len = strlen(fname);
  146. char *lcname = zend_str_tolower_dup(fname, fname_len);
  147. if (cname) {
  148. zend_class_entry **ce;
  149. size_t cname_len = strlen(cname);
  150. char *lc_cname = zend_str_tolower_dup(cname, cname_len);
  151. int ret = zend_lookup_class(lc_cname, cname_len, &ce TSRMLS_CC);
  152. efree(lc_cname);
  153. if (ret == SUCCESS) {
  154. zend_hash_find(&(*ce)->function_table, lcname, fname_len+1,
  155. (void**)&func);
  156. }
  157. } else {
  158. zend_hash_find(EG(function_table), lcname, fname_len+1,
  159. (void**)&func);
  160. }
  161. efree(lcname);
  162. return func;
  163. } /* }}} */
  164. PHPDBG_API char *phpdbg_trim(const char *str, size_t len, size_t *new_len) /* {{{ */
  165. {
  166. const char *p = str;
  167. char *new = NULL;
  168. while (p && isspace(*p)) {
  169. ++p;
  170. --len;
  171. }
  172. while (*p && isspace(*(p + len -1))) {
  173. --len;
  174. }
  175. if (len == 0) {
  176. new = estrndup("", sizeof(""));
  177. *new_len = 0;
  178. } else {
  179. new = estrndup(p, len);
  180. *(new + len) = '\0';
  181. if (new_len) {
  182. *new_len = len;
  183. }
  184. }
  185. return new;
  186. } /* }}} */
  187. PHPDBG_API int phpdbg_print(int type TSRMLS_DC, FILE *fp, const char *format, ...) /* {{{ */
  188. {
  189. int rc = 0;
  190. char *buffer = NULL;
  191. va_list args;
  192. if (format != NULL && strlen(format) > 0L) {
  193. va_start(args, format);
  194. vspprintf(&buffer, 0, format, args);
  195. va_end(args);
  196. }
  197. /* TODO(anyone) colours */
  198. switch (type) {
  199. case P_ERROR:
  200. if (PHPDBG_G(flags) & PHPDBG_IS_COLOURED) {
  201. rc = fprintf(fp,
  202. "\033[%sm[%s]\033[0m\n",
  203. PHPDBG_G(colors)[PHPDBG_COLOR_ERROR]->code, buffer);
  204. } else {
  205. rc = fprintf(fp, "[%s]\n", buffer);
  206. }
  207. break;
  208. case P_NOTICE:
  209. if (PHPDBG_G(flags) & PHPDBG_IS_COLOURED) {
  210. rc = fprintf(fp,
  211. "\033[%sm[%s]\033[0m\n",
  212. PHPDBG_G(colors)[PHPDBG_COLOR_NOTICE]->code, buffer);
  213. } else {
  214. rc = fprintf(fp, "[%s]\n", buffer);
  215. }
  216. break;
  217. case P_WRITELN: {
  218. if (buffer) {
  219. rc = fprintf(fp, "%s\n", buffer);
  220. } else {
  221. rc = fprintf(fp, "\n");
  222. }
  223. } break;
  224. case P_WRITE:
  225. if (buffer) {
  226. rc = fprintf(fp, "%s", buffer);
  227. }
  228. break;
  229. /* no formatting on logging output */
  230. case P_LOG:
  231. if (buffer) {
  232. struct timeval tp;
  233. if (gettimeofday(&tp, NULL) == SUCCESS) {
  234. rc = fprintf(fp, "[%ld %.8F]: %s\n", tp.tv_sec, tp.tv_usec / 1000000.00, buffer);
  235. } else {
  236. rc = FAILURE;
  237. }
  238. }
  239. break;
  240. }
  241. if (buffer) {
  242. efree(buffer);
  243. }
  244. return rc;
  245. } /* }}} */
  246. PHPDBG_API int phpdbg_rlog(FILE *fp, const char *fmt, ...) { /* {{{ */
  247. int rc = 0;
  248. va_list args;
  249. struct timeval tp;
  250. va_start(args, fmt);
  251. if (gettimeofday(&tp, NULL) == SUCCESS) {
  252. char friendly[100];
  253. char *format = NULL, *buffer = NULL;
  254. const time_t tt = tp.tv_sec;
  255. strftime(friendly, 100, "%a %b %d %T.%%04d %Y", localtime(&tt));
  256. asprintf(
  257. &buffer, friendly, tp.tv_usec/1000);
  258. asprintf(
  259. &format, "[%s]: %s\n", buffer, fmt);
  260. rc = vfprintf(
  261. fp, format, args);
  262. free(format);
  263. free(buffer);
  264. }
  265. va_end(args);
  266. return rc;
  267. } /* }}} */
  268. PHPDBG_API const phpdbg_color_t *phpdbg_get_color(const char *name, size_t name_length TSRMLS_DC) /* {{{ */
  269. {
  270. const phpdbg_color_t *color = colors;
  271. while (color && color->name) {
  272. if (name_length == color->name_length &&
  273. memcmp(name, color->name, name_length) == SUCCESS) {
  274. phpdbg_debug(
  275. "phpdbg_get_color(%s, %lu): %s", name, name_length, color->code);
  276. return color;
  277. }
  278. ++color;
  279. }
  280. phpdbg_debug(
  281. "phpdbg_get_color(%s, %lu): failed", name, name_length);
  282. return NULL;
  283. } /* }}} */
  284. PHPDBG_API void phpdbg_set_color(int element, const phpdbg_color_t *color TSRMLS_DC) /* {{{ */
  285. {
  286. PHPDBG_G(colors)[element] = color;
  287. } /* }}} */
  288. PHPDBG_API void phpdbg_set_color_ex(int element, const char *name, size_t name_length TSRMLS_DC) /* {{{ */
  289. {
  290. const phpdbg_color_t *color = phpdbg_get_color(name, name_length TSRMLS_CC);
  291. if (color) {
  292. phpdbg_set_color(element, color TSRMLS_CC);
  293. } else PHPDBG_G(colors)[element] = colors;
  294. } /* }}} */
  295. PHPDBG_API const phpdbg_color_t* phpdbg_get_colors(TSRMLS_D) /* {{{ */
  296. {
  297. return colors;
  298. } /* }}} */
  299. PHPDBG_API int phpdbg_get_element(const char *name, size_t len TSRMLS_DC) {
  300. const phpdbg_element_t *element = elements;
  301. while (element && element->name) {
  302. if (len == element->name_length) {
  303. if (strncasecmp(name, element->name, len) == SUCCESS) {
  304. return element->id;
  305. }
  306. }
  307. element++;
  308. }
  309. return PHPDBG_COLOR_INVALID;
  310. }
  311. PHPDBG_API void phpdbg_set_prompt(const char *prompt TSRMLS_DC) /* {{{ */
  312. {
  313. /* free formatted prompt */
  314. if (PHPDBG_G(prompt)[1]) {
  315. free(PHPDBG_G(prompt)[1]);
  316. PHPDBG_G(prompt)[1] = NULL;
  317. }
  318. /* free old prompt */
  319. if (PHPDBG_G(prompt)[0]) {
  320. free(PHPDBG_G(prompt)[0]);
  321. PHPDBG_G(prompt)[0] = NULL;
  322. }
  323. /* copy new prompt */
  324. PHPDBG_G(prompt)[0] = strdup(prompt);
  325. } /* }}} */
  326. PHPDBG_API const char *phpdbg_get_prompt(TSRMLS_D) /* {{{ */
  327. {
  328. /* find cached prompt */
  329. if (PHPDBG_G(prompt)[1]) {
  330. return PHPDBG_G(prompt)[1];
  331. }
  332. /* create cached prompt */
  333. #ifndef HAVE_LIBEDIT
  334. /* TODO: libedit doesn't seems to support coloured prompt */
  335. if ((PHPDBG_G(flags) & PHPDBG_IS_COLOURED)) {
  336. asprintf(
  337. &PHPDBG_G(prompt)[1], "\033[%sm%s\033[0m ",
  338. PHPDBG_G(colors)[PHPDBG_COLOR_PROMPT]->code,
  339. PHPDBG_G(prompt)[0]);
  340. } else
  341. #endif
  342. {
  343. asprintf(
  344. &PHPDBG_G(prompt)[1], "%s ",
  345. PHPDBG_G(prompt)[0]);
  346. }
  347. return PHPDBG_G(prompt)[1];
  348. } /* }}} */
  349. int phpdbg_rebuild_symtable(TSRMLS_D) {
  350. if (!EG(active_op_array)) {
  351. phpdbg_error("No active op array!");
  352. return FAILURE;
  353. }
  354. if (!EG(active_symbol_table)) {
  355. zend_rebuild_symbol_table(TSRMLS_C);
  356. if (!EG(active_symbol_table)) {
  357. phpdbg_error("No active symbol table!");
  358. return FAILURE;
  359. }
  360. }
  361. return SUCCESS;
  362. }
  363. PHPDBG_API int phpdbg_get_terminal_width(TSRMLS_D) /* {{{ */
  364. {
  365. int columns;
  366. #ifdef _WIN32
  367. CONSOLE_SCREEN_BUFFER_INFO csbi;
  368. GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
  369. columns = csbi.srWindow.Right - csbi.srWindow.Left + 1;
  370. #elif defined(HAVE_SYS_IOCTL_H) && defined (TIOCGWINSZ)
  371. struct winsize w;
  372. columns = ioctl(fileno(stdout), TIOCGWINSZ, &w) == 0 ? w.ws_col : 80;
  373. #else
  374. columns = 80;
  375. #endif
  376. return columns;
  377. } /* }}} */