phpdbg_list.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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 <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. ZEND_EXTERN_MODULE_GLOBALS(phpdbg);
  34. #define PHPDBG_LIST_COMMAND_D(f, h, a, m, l, s) \
  35. PHPDBG_COMMAND_D_EXP(f, h, a, m, l, s, &phpdbg_prompt_commands[13])
  36. const phpdbg_command_t phpdbg_list_commands[] = {
  37. PHPDBG_LIST_COMMAND_D(lines, "lists the specified lines", 'l', list_lines, NULL, "l"),
  38. PHPDBG_LIST_COMMAND_D(class, "lists the specified class", 'c', list_class, NULL, "s"),
  39. PHPDBG_LIST_COMMAND_D(method, "lists the specified method", 'm', list_method, NULL, "m"),
  40. PHPDBG_LIST_COMMAND_D(func, "lists the specified function", 'f', list_func, NULL, "s"),
  41. PHPDBG_END_COMMAND
  42. };
  43. PHPDBG_LIST(lines) /* {{{ */
  44. {
  45. if (!PHPDBG_G(exec) && !zend_is_executing(TSRMLS_C)) {
  46. phpdbg_error("Not executing, and execution context not set");
  47. return SUCCESS;
  48. }
  49. switch (param->type) {
  50. case NUMERIC_PARAM:
  51. phpdbg_list_file(phpdbg_current_file(TSRMLS_C),
  52. (param->num < 0 ? 1 - param->num : param->num),
  53. (param->num < 0 ? param->num : 0) + zend_get_executed_lineno(TSRMLS_C),
  54. 0 TSRMLS_CC);
  55. break;
  56. case FILE_PARAM:
  57. phpdbg_list_file(param->file.name, param->file.line, 0, 0 TSRMLS_CC);
  58. break;
  59. phpdbg_default_switch_case();
  60. }
  61. return SUCCESS;
  62. } /* }}} */
  63. PHPDBG_LIST(func) /* {{{ */
  64. {
  65. phpdbg_list_function_byname(param->str, param->len TSRMLS_CC);
  66. return SUCCESS;
  67. } /* }}} */
  68. PHPDBG_LIST(method) /* {{{ */
  69. {
  70. zend_class_entry **ce;
  71. if (zend_lookup_class(param->method.class, strlen(param->method.class), &ce TSRMLS_CC) == SUCCESS) {
  72. zend_function *function;
  73. char *lcname = zend_str_tolower_dup(param->method.name, strlen(param->method.name));
  74. if (zend_hash_find(&(*ce)->function_table, lcname, strlen(lcname)+1, (void**) &function) == SUCCESS) {
  75. phpdbg_list_function(function TSRMLS_CC);
  76. } else {
  77. phpdbg_error("Could not find %s::%s", param->method.class, param->method.name);
  78. }
  79. efree(lcname);
  80. } else {
  81. phpdbg_error("Could not find the class %s", param->method.class);
  82. }
  83. return SUCCESS;
  84. } /* }}} */
  85. PHPDBG_LIST(class) /* {{{ */
  86. {
  87. zend_class_entry **ce;
  88. if (zend_lookup_class(param->str, param->len, &ce TSRMLS_CC) == SUCCESS) {
  89. if ((*ce)->type == ZEND_USER_CLASS) {
  90. if ((*ce)->info.user.filename) {
  91. phpdbg_list_file(
  92. (*ce)->info.user.filename,
  93. (*ce)->info.user.line_end - (*ce)->info.user.line_start + 1,
  94. (*ce)->info.user.line_start, 0 TSRMLS_CC
  95. );
  96. } else {
  97. phpdbg_error("The source of the requested class (%s) cannot be found", (*ce)->name);
  98. }
  99. } else {
  100. phpdbg_error("The class requested (%s) is not user defined", (*ce)->name);
  101. }
  102. } else {
  103. phpdbg_error("The requested class (%s) could not be found", param->str);
  104. }
  105. return SUCCESS;
  106. } /* }}} */
  107. void phpdbg_list_file(const char *filename, long count, long offset, int highlight TSRMLS_DC) /* {{{ */
  108. {
  109. struct stat st;
  110. char *opened = NULL;
  111. char buffer[8096] = {0,};
  112. long line = 0;
  113. php_stream *stream = NULL;
  114. if (VCWD_STAT(filename, &st) == FAILURE) {
  115. phpdbg_error("Failed to stat file %s", filename);
  116. return;
  117. }
  118. stream = php_stream_open_wrapper(filename, "rb", USE_PATH, &opened);
  119. if (!stream) {
  120. phpdbg_error("Failed to open file %s to list", filename);
  121. return;
  122. }
  123. if (offset < 0) {
  124. count += offset;
  125. offset = 0;
  126. }
  127. while (php_stream_gets(stream, buffer, sizeof(buffer)) != NULL) {
  128. long linelen = strlen(buffer);
  129. ++line;
  130. if (offset <= line) {
  131. if (!highlight) {
  132. phpdbg_write("%05ld: %s", line, buffer);
  133. } else {
  134. if (highlight != line) {
  135. phpdbg_write(" %05ld: %s", line, buffer);
  136. } else {
  137. phpdbg_write(">%05ld: %s", line, buffer);
  138. }
  139. }
  140. if (buffer[linelen - 1] != '\n') {
  141. phpdbg_write("\n");
  142. }
  143. }
  144. if (count > 0 && count + offset - 1 < line) {
  145. break;
  146. }
  147. }
  148. php_stream_close(stream);
  149. } /* }}} */
  150. void phpdbg_list_function(const zend_function *fbc TSRMLS_DC) /* {{{ */
  151. {
  152. const zend_op_array *ops;
  153. if (fbc->type != ZEND_USER_FUNCTION) {
  154. phpdbg_error("The function requested (%s) is not user defined", fbc->common.function_name);
  155. return;
  156. }
  157. ops = (zend_op_array*)fbc;
  158. phpdbg_list_file(ops->filename,
  159. ops->line_end - ops->line_start + 1, ops->line_start, 0 TSRMLS_CC);
  160. } /* }}} */
  161. void phpdbg_list_function_byname(const char *str, size_t len TSRMLS_DC) /* {{{ */
  162. {
  163. HashTable *func_table = EG(function_table);
  164. zend_function* fbc;
  165. char *func_name = (char*) str;
  166. size_t func_name_len = len;
  167. /* search active scope if begins with period */
  168. if (func_name[0] == '.') {
  169. if (EG(scope)) {
  170. func_name++;
  171. func_name_len--;
  172. func_table = &EG(scope)->function_table;
  173. } else {
  174. phpdbg_error("No active class");
  175. return;
  176. }
  177. } else if (!EG(function_table)) {
  178. phpdbg_error("No function table loaded");
  179. return;
  180. } else {
  181. func_table = EG(function_table);
  182. }
  183. /* use lowercase names, case insensitive */
  184. func_name = zend_str_tolower_dup(func_name, func_name_len);
  185. if (zend_hash_find(func_table, func_name, func_name_len+1, (void**)&fbc) == SUCCESS) {
  186. phpdbg_list_function(fbc TSRMLS_CC);
  187. } else {
  188. phpdbg_error("Function %s not found", func_name);
  189. }
  190. efree(func_name);
  191. } /* }}} */