phpdbg_list.c 11 KB

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