phpdbg_list.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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 <stdio.h>
  21. #include <string.h>
  22. #include <sys/stat.h>
  23. #ifndef _WIN32
  24. # include <sys/mman.h>
  25. # include <unistd.h>
  26. #endif
  27. #include <fcntl.h>
  28. #include "phpdbg.h"
  29. #include "phpdbg_list.h"
  30. #include "phpdbg_utils.h"
  31. #include "phpdbg_prompt.h"
  32. #include "php_streams.h"
  33. #include "zend_exceptions.h"
  34. ZEND_EXTERN_MODULE_GLOBALS(phpdbg)
  35. #define PHPDBG_LIST_COMMAND_D(f, h, a, m, l, s, flags) \
  36. PHPDBG_COMMAND_D_EXP(f, h, a, m, l, s, &phpdbg_prompt_commands[12], flags)
  37. const phpdbg_command_t phpdbg_list_commands[] = {
  38. PHPDBG_LIST_COMMAND_D(lines, "lists the specified lines", 'l', list_lines, NULL, "l", PHPDBG_ASYNC_SAFE),
  39. PHPDBG_LIST_COMMAND_D(class, "lists the specified class", 'c', list_class, NULL, "s", PHPDBG_ASYNC_SAFE),
  40. PHPDBG_LIST_COMMAND_D(method, "lists the specified method", 'm', list_method, NULL, "m", PHPDBG_ASYNC_SAFE),
  41. PHPDBG_LIST_COMMAND_D(func, "lists the specified function", 'f', list_func, NULL, "s", PHPDBG_ASYNC_SAFE),
  42. PHPDBG_END_COMMAND
  43. };
  44. PHPDBG_LIST(lines) /* {{{ */
  45. {
  46. if (!PHPDBG_G(exec) && !zend_is_executing()) {
  47. phpdbg_error("inactive", "type=\"execution\"", "Not executing, and execution context not set");
  48. return SUCCESS;
  49. }
  50. switch (param->type) {
  51. case NUMERIC_PARAM: {
  52. const char *char_file = phpdbg_current_file();
  53. zend_string *file = zend_string_init(char_file, strlen(char_file), 0);
  54. phpdbg_list_file(file, param->num < 0 ? 1 - param->num : param->num, (param->num < 0 ? param->num : 0) + zend_get_executed_lineno(), 0);
  55. efree(file);
  56. } break;
  57. case FILE_PARAM: {
  58. zend_string *file;
  59. char resolved_path_buf[MAXPATHLEN];
  60. const char *abspath = param->file.name;
  61. if (VCWD_REALPATH(abspath, resolved_path_buf)) {
  62. abspath = resolved_path_buf;
  63. }
  64. file = zend_string_init(abspath, strlen(abspath), 0);
  65. phpdbg_list_file(file, param->file.line, 0, 0);
  66. zend_string_release(file);
  67. } break;
  68. phpdbg_default_switch_case();
  69. }
  70. return SUCCESS;
  71. } /* }}} */
  72. PHPDBG_LIST(func) /* {{{ */
  73. {
  74. phpdbg_list_function_byname(param->str, param->len);
  75. return SUCCESS;
  76. } /* }}} */
  77. PHPDBG_LIST(method) /* {{{ */
  78. {
  79. zend_class_entry *ce;
  80. if (phpdbg_safe_class_lookup(param->method.class, strlen(param->method.class), &ce) == SUCCESS) {
  81. zend_function *function;
  82. char *lcname = zend_str_tolower_dup(param->method.name, strlen(param->method.name));
  83. if ((function = zend_hash_str_find_ptr(&ce->function_table, lcname, strlen(lcname)))) {
  84. phpdbg_list_function(function);
  85. } else {
  86. phpdbg_error("list", "type=\"notfound\" method=\"%s::%s\"", "Could not find %s::%s", param->method.class, param->method.name);
  87. }
  88. efree(lcname);
  89. } else {
  90. phpdbg_error("list", "type=\"notfound\" class=\"%s\"", "Could not find the class %s", param->method.class);
  91. }
  92. return SUCCESS;
  93. } /* }}} */
  94. PHPDBG_LIST(class) /* {{{ */
  95. {
  96. zend_class_entry *ce;
  97. if (phpdbg_safe_class_lookup(param->str, param->len, &ce) == SUCCESS) {
  98. if (ce->type == ZEND_USER_CLASS) {
  99. if (ce->info.user.filename) {
  100. phpdbg_list_file(ce->info.user.filename, ce->info.user.line_end - ce->info.user.line_start + 1, ce->info.user.line_start, 0);
  101. } else {
  102. phpdbg_error("list", "type=\"nosource\" class=\"%s\"", "The source of the requested class (%s) cannot be found", ZSTR_VAL(ce->name));
  103. }
  104. } else {
  105. phpdbg_error("list", "type=\"internalclass\" class=\"%s\"", "The class requested (%s) is not user defined", ZSTR_VAL(ce->name));
  106. }
  107. } else {
  108. phpdbg_error("list", "type=\"notfound\" class=\"%s\"", "The requested class (%s) could not be found", param->str);
  109. }
  110. return SUCCESS;
  111. } /* }}} */
  112. void phpdbg_list_file(zend_string *filename, uint32_t count, int offset, uint32_t highlight) /* {{{ */
  113. {
  114. uint32_t line, lastline;
  115. phpdbg_file_source *data;
  116. if (!(data = zend_hash_find_ptr(&PHPDBG_G(file_sources), filename))) {
  117. phpdbg_error("list", "type=\"unknownfile\"", "Could not find information about included file...");
  118. return;
  119. }
  120. if (offset < 0) {
  121. count += offset;
  122. offset = 0;
  123. }
  124. lastline = offset + count;
  125. if (lastline > data->lines) {
  126. lastline = data->lines;
  127. }
  128. phpdbg_xml("<list %r file=\"%s\">", ZSTR_VAL(filename));
  129. for (line = offset; line < lastline;) {
  130. uint32_t linestart = data->line[line++];
  131. uint32_t linelen = data->line[line] - linestart;
  132. char *buffer = data->buf + linestart;
  133. if (!highlight) {
  134. phpdbg_write("line", "line=\"%u\" code=\"%.*s\"", " %05u: %.*s", line, linelen, buffer);
  135. } else {
  136. if (highlight != line) {
  137. phpdbg_write("line", "line=\"%u\" code=\"%.*s\"", " %05u: %.*s", line, linelen, buffer);
  138. } else {
  139. phpdbg_write("line", "line=\"%u\" code=\"%.*s\" current=\"current\"", ">%05u: %.*s", line, linelen, buffer);
  140. }
  141. }
  142. if (*(buffer + linelen - 1) != '\n' || !linelen) {
  143. phpdbg_out("\n");
  144. }
  145. }
  146. phpdbg_xml("</list>");
  147. } /* }}} */
  148. void phpdbg_list_function(const zend_function *fbc) /* {{{ */
  149. {
  150. const zend_op_array *ops;
  151. if (fbc->type != ZEND_USER_FUNCTION) {
  152. phpdbg_error("list", "type=\"internalfunction\" function=\"%s\"", "The function requested (%s) is not user defined", ZSTR_VAL(fbc->common.function_name));
  153. return;
  154. }
  155. ops = (zend_op_array *) fbc;
  156. phpdbg_list_file(ops->filename, ops->line_end - ops->line_start + 1, ops->line_start, 0);
  157. } /* }}} */
  158. void phpdbg_list_function_byname(const char *str, size_t len) /* {{{ */
  159. {
  160. HashTable *func_table = EG(function_table);
  161. zend_function* fbc;
  162. char *func_name = (char*) str;
  163. size_t func_name_len = len;
  164. /* search active scope if begins with period */
  165. if (func_name[0] == '.') {
  166. zend_class_entry *scope = zend_get_executed_scope();
  167. if (scope) {
  168. func_name++;
  169. func_name_len--;
  170. func_table = &scope->function_table;
  171. } else {
  172. phpdbg_error("inactive", "type=\"noclasses\"", "No active class");
  173. return;
  174. }
  175. } else if (!EG(function_table)) {
  176. phpdbg_error("inactive", "type=\"function_table\"", "No function table loaded");
  177. return;
  178. } else {
  179. func_table = EG(function_table);
  180. }
  181. /* use lowercase names, case insensitive */
  182. func_name = zend_str_tolower_dup(func_name, func_name_len);
  183. phpdbg_try_access {
  184. if ((fbc = zend_hash_str_find_ptr(func_table, func_name, func_name_len))) {
  185. phpdbg_list_function(fbc);
  186. } else {
  187. phpdbg_error("list", "type=\"nofunction\" function=\"%s\"", "Function %s not found", func_name);
  188. }
  189. } phpdbg_catch_access {
  190. phpdbg_error("signalsegv", "function=\"%s\"", "Could not list function %s, invalid data source", func_name);
  191. } phpdbg_end_try_access();
  192. efree(func_name);
  193. } /* }}} */
  194. /* Note: do not free the original file handler, let original compile_file() or caller do that. Caller may rely on its value to check success */
  195. zend_op_array *phpdbg_compile_file(zend_file_handle *file, int type) {
  196. phpdbg_file_source data, *dataptr;
  197. zend_op_array *ret;
  198. uint32_t line;
  199. char *bufptr, *endptr;
  200. size_t len;
  201. /* Copy file contents before calling original compile_file,
  202. * as it may invalidate the file handle. */
  203. if (zend_stream_fixup(file, &bufptr, &len) == FAILURE) {
  204. if (type == ZEND_REQUIRE) {
  205. zend_message_dispatcher(ZMSG_FAILED_REQUIRE_FOPEN, file->filename);
  206. zend_bailout();
  207. } else {
  208. zend_message_dispatcher(ZMSG_FAILED_INCLUDE_FOPEN, file->filename);
  209. }
  210. return NULL;
  211. }
  212. data.buf = estrndup(bufptr, len);
  213. data.len = len;
  214. ret = PHPDBG_G(compile_file)(file, type);
  215. if (ret == NULL) {
  216. efree(data.buf);
  217. return ret;
  218. }
  219. data.buf[data.len] = '\0';
  220. data.line[0] = 0;
  221. *(dataptr = emalloc(sizeof(phpdbg_file_source) + sizeof(uint32_t) * data.len)) = data;
  222. for (line = 0, bufptr = data.buf - 1, endptr = data.buf + data.len; ++bufptr < endptr;) {
  223. if (*bufptr == '\n') {
  224. dataptr->line[++line] = (uint32_t)(bufptr - data.buf) + 1;
  225. }
  226. }
  227. dataptr->lines = ++line;
  228. dataptr = erealloc(dataptr, sizeof(phpdbg_file_source) + sizeof(uint32_t) * line);
  229. dataptr->line[line] = endptr - data.buf;
  230. zend_hash_del(&PHPDBG_G(file_sources), ret->filename);
  231. zend_hash_add_ptr(&PHPDBG_G(file_sources), ret->filename, dataptr);
  232. phpdbg_resolve_pending_file_break(ZSTR_VAL(ret->filename));
  233. return ret;
  234. }
  235. zend_op_array *phpdbg_init_compile_file(zend_file_handle *file, int type) {
  236. char *filename = (char *)(file->opened_path ? ZSTR_VAL(file->opened_path) : file->filename);
  237. char resolved_path_buf[MAXPATHLEN];
  238. zend_op_array *op_array;
  239. phpdbg_file_source *dataptr;
  240. if (VCWD_REALPATH(filename, resolved_path_buf)) {
  241. filename = resolved_path_buf;
  242. if (file->opened_path) {
  243. zend_string_release(file->opened_path);
  244. file->opened_path = zend_string_init(filename, strlen(filename), 0);
  245. } else {
  246. if (file->free_filename) {
  247. efree((char *) file->filename);
  248. }
  249. file->free_filename = 0;
  250. file->filename = filename;
  251. }
  252. }
  253. op_array = PHPDBG_G(init_compile_file)(file, type);
  254. if (op_array == NULL) {
  255. return NULL;
  256. }
  257. dataptr = zend_hash_find_ptr(&PHPDBG_G(file_sources), op_array->filename);
  258. ZEND_ASSERT(dataptr != NULL);
  259. dataptr->op_array = *op_array;
  260. if (dataptr->op_array.refcount) {
  261. ++*dataptr->op_array.refcount;
  262. }
  263. return op_array;
  264. }
  265. zend_op_array *phpdbg_compile_string(zval *source_string, char *filename) {
  266. zend_string *fake_name;
  267. zend_op_array *op_array;
  268. phpdbg_file_source *dataptr;
  269. uint32_t line;
  270. char *bufptr, *endptr;
  271. if (PHPDBG_G(flags) & PHPDBG_IN_EVAL) {
  272. return PHPDBG_G(compile_string)(source_string, filename);
  273. }
  274. dataptr = emalloc(sizeof(phpdbg_file_source) + sizeof(uint32_t) * Z_STRLEN_P(source_string));
  275. dataptr->buf = estrndup(Z_STRVAL_P(source_string), Z_STRLEN_P(source_string));
  276. dataptr->len = Z_STRLEN_P(source_string);
  277. dataptr->line[0] = 0;
  278. for (line = 0, bufptr = dataptr->buf - 1, endptr = dataptr->buf + dataptr->len; ++bufptr < endptr;) {
  279. if (*bufptr == '\n') {
  280. dataptr->line[++line] = (uint32_t)(bufptr - dataptr->buf) + 1;
  281. }
  282. }
  283. dataptr->lines = ++line;
  284. dataptr->line[line] = endptr - dataptr->buf;
  285. op_array = PHPDBG_G(compile_string)(source_string, filename);
  286. if (op_array == NULL) {
  287. efree(dataptr->buf);
  288. efree(dataptr);
  289. return NULL;
  290. }
  291. fake_name = strpprintf(0, "%s%c%p", filename, 0, op_array->opcodes);
  292. dataptr = erealloc(dataptr, sizeof(phpdbg_file_source) + sizeof(uint32_t) * line);
  293. zend_hash_add_ptr(&PHPDBG_G(file_sources), fake_name, dataptr);
  294. zend_string_release(fake_name);
  295. dataptr->op_array = *op_array;
  296. if (dataptr->op_array.refcount) {
  297. ++*dataptr->op_array.refcount;
  298. }
  299. return op_array;
  300. }
  301. void phpdbg_init_list(void) {
  302. PHPDBG_G(compile_file) = zend_compile_file;
  303. PHPDBG_G(compile_string) = zend_compile_string;
  304. zend_compile_file = phpdbg_compile_file;
  305. zend_compile_string = phpdbg_compile_string;
  306. }
  307. void phpdbg_list_update(void) {
  308. PHPDBG_G(init_compile_file) = zend_compile_file;
  309. zend_compile_file = phpdbg_init_compile_file;
  310. }