phpdbg_print.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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 "phpdbg.h"
  21. #include "phpdbg_print.h"
  22. #include "phpdbg_utils.h"
  23. #include "phpdbg_opcode.h"
  24. #include "phpdbg_prompt.h"
  25. ZEND_EXTERN_MODULE_GLOBALS(phpdbg);
  26. #define PHPDBG_PRINT_COMMAND_D(f, h, a, m, l, s) \
  27. PHPDBG_COMMAND_D_EXP(f, h, a, m, l, s, &phpdbg_prompt_commands[9])
  28. const phpdbg_command_t phpdbg_print_commands[] = {
  29. PHPDBG_PRINT_COMMAND_D(exec, "print out the instructions in the execution context", 'e', print_exec, NULL, 0),
  30. PHPDBG_PRINT_COMMAND_D(opline, "print out the instruction in the current opline", 'o', print_opline, NULL, 0),
  31. PHPDBG_PRINT_COMMAND_D(class, "print out the instructions in the specified class", 'c', print_class, NULL, "s"),
  32. PHPDBG_PRINT_COMMAND_D(method, "print out the instructions in the specified method", 'm', print_method, NULL, "m"),
  33. PHPDBG_PRINT_COMMAND_D(func, "print out the instructions in the specified function", 'f', print_func, NULL, "s"),
  34. PHPDBG_PRINT_COMMAND_D(stack, "print out the instructions in the current stack", 's', print_stack, NULL, 0),
  35. PHPDBG_END_COMMAND
  36. };
  37. PHPDBG_PRINT(opline) /* {{{ */
  38. {
  39. if (EG(in_execution) && EG(current_execute_data)) {
  40. phpdbg_print_opline(EG(current_execute_data), 1 TSRMLS_CC);
  41. } else {
  42. phpdbg_error("Not Executing!");
  43. }
  44. return SUCCESS;
  45. } /* }}} */
  46. static inline void phpdbg_print_function_helper(zend_function *method TSRMLS_DC) /* {{{ */
  47. {
  48. switch (method->type) {
  49. case ZEND_USER_FUNCTION: {
  50. zend_op_array* op_array = &(method->op_array);
  51. HashTable vars;
  52. if (op_array) {
  53. zend_op *opline = &(op_array->opcodes[0]);
  54. zend_uint opcode = 0,
  55. end = op_array->last-1;
  56. if (method->common.scope) {
  57. phpdbg_writeln("\tL%d-%d %s::%s() %s",
  58. op_array->line_start, op_array->line_end,
  59. method->common.scope->name,
  60. method->common.function_name,
  61. op_array->filename ? op_array->filename : "unknown");
  62. } else {
  63. phpdbg_writeln("\tL%d-%d %s() %s",
  64. method->common.function_name ? op_array->line_start : 0,
  65. method->common.function_name ? op_array->line_end : 0,
  66. method->common.function_name ? method->common.function_name : "{main}",
  67. op_array->filename ? op_array->filename : "unknown");
  68. }
  69. zend_hash_init(&vars, op_array->last, NULL, NULL, 0);
  70. do {
  71. char *decode = phpdbg_decode_opline(op_array, opline, &vars TSRMLS_CC);
  72. if (decode != NULL) {
  73. phpdbg_writeln("\t\tL%u\t%p %-30s %s",
  74. opline->lineno,
  75. opline,
  76. phpdbg_decode_opcode(opline->opcode),
  77. decode);
  78. free(decode);
  79. } else {
  80. phpdbg_error("\tFailed to decode opline %16p", opline);
  81. }
  82. opline++;
  83. } while (opcode++ < end);
  84. zend_hash_destroy(&vars);
  85. }
  86. } break;
  87. default: {
  88. if (method->common.scope) {
  89. phpdbg_writeln("\tInternal %s::%s()", method->common.scope->name, method->common.function_name);
  90. } else {
  91. phpdbg_writeln("\tInternal %s()", method->common.function_name);
  92. }
  93. }
  94. }
  95. } /* }}} */
  96. PHPDBG_PRINT(exec) /* {{{ */
  97. {
  98. if (PHPDBG_G(exec)) {
  99. if (!PHPDBG_G(ops)) {
  100. phpdbg_compile(TSRMLS_C);
  101. }
  102. if (PHPDBG_G(ops)) {
  103. phpdbg_notice("Context %s", PHPDBG_G(exec));
  104. phpdbg_print_function_helper((zend_function*) PHPDBG_G(ops) TSRMLS_CC);
  105. }
  106. } else {
  107. phpdbg_error("No execution context set");
  108. }
  109. return SUCCESS;
  110. } /* }}} */
  111. PHPDBG_PRINT(stack) /* {{{ */
  112. {
  113. zend_op_array *ops = EG(active_op_array);
  114. if (EG(in_execution) && ops) {
  115. if (ops->function_name) {
  116. if (ops->scope) {
  117. phpdbg_notice("Stack in %s::%s()", ops->scope->name, ops->function_name);
  118. } else {
  119. phpdbg_notice("Stack in %s()", ops->function_name);
  120. }
  121. } else {
  122. if (ops->filename) {
  123. phpdbg_notice("Stack in %s", ops->filename);
  124. } else {
  125. phpdbg_notice("Stack @ %p", ops);
  126. }
  127. }
  128. phpdbg_print_function_helper((zend_function*) ops TSRMLS_CC);
  129. } else {
  130. phpdbg_error("Not Executing!");
  131. }
  132. return SUCCESS;
  133. } /* }}} */
  134. PHPDBG_PRINT(class) /* {{{ */
  135. {
  136. zend_class_entry **ce;
  137. if (zend_lookup_class(param->str, param->len, &ce TSRMLS_CC) == SUCCESS) {
  138. phpdbg_notice("%s %s: %s",
  139. ((*ce)->type == ZEND_USER_CLASS) ?
  140. "User" : "Internal",
  141. ((*ce)->ce_flags & ZEND_ACC_INTERFACE) ?
  142. "Interface" :
  143. ((*ce)->ce_flags & ZEND_ACC_ABSTRACT) ?
  144. "Abstract Class" :
  145. "Class",
  146. (*ce)->name);
  147. phpdbg_writeln("Methods (%d):", zend_hash_num_elements(&(*ce)->function_table));
  148. if (zend_hash_num_elements(&(*ce)->function_table)) {
  149. HashPosition position;
  150. zend_function *method;
  151. for (zend_hash_internal_pointer_reset_ex(&(*ce)->function_table, &position);
  152. zend_hash_get_current_data_ex(&(*ce)->function_table, (void**) &method, &position) == SUCCESS;
  153. zend_hash_move_forward_ex(&(*ce)->function_table, &position)) {
  154. phpdbg_print_function_helper(method TSRMLS_CC);
  155. }
  156. }
  157. } else {
  158. phpdbg_error("The class %s could not be found", param->str);
  159. }
  160. return SUCCESS;
  161. } /* }}} */
  162. PHPDBG_PRINT(method) /* {{{ */
  163. {
  164. zend_class_entry **ce;
  165. if (zend_lookup_class(param->method.class, strlen(param->method.class), &ce TSRMLS_CC) == SUCCESS) {
  166. zend_function *fbc;
  167. char *lcname = zend_str_tolower_dup(param->method.name, strlen(param->method.name));
  168. if (zend_hash_find(&(*ce)->function_table, lcname, strlen(lcname)+1, (void**)&fbc) == SUCCESS) {
  169. phpdbg_notice("%s Method %s",
  170. (fbc->type == ZEND_USER_FUNCTION) ? "User" : "Internal",
  171. fbc->common.function_name);
  172. phpdbg_print_function_helper(fbc TSRMLS_CC);
  173. } else {
  174. phpdbg_error("The method %s could not be found", param->method.name);
  175. }
  176. efree(lcname);
  177. } else {
  178. phpdbg_error("The class %s could not be found", param->method.class);
  179. }
  180. return SUCCESS;
  181. } /* }}} */
  182. PHPDBG_PRINT(func) /* {{{ */
  183. {
  184. HashTable *func_table = EG(function_table);
  185. zend_function* fbc;
  186. const char *func_name = param->str;
  187. size_t func_name_len = param->len;
  188. char *lcname;
  189. /* search active scope if begins with period */
  190. if (func_name[0] == '.') {
  191. if (EG(scope)) {
  192. func_name++;
  193. func_name_len--;
  194. func_table = &EG(scope)->function_table;
  195. } else {
  196. phpdbg_error("No active class");
  197. return SUCCESS;
  198. }
  199. } else if (!EG(function_table)) {
  200. phpdbg_error("No function table loaded");
  201. return SUCCESS;
  202. } else {
  203. func_table = EG(function_table);
  204. }
  205. lcname = zend_str_tolower_dup(func_name, func_name_len);
  206. if (zend_hash_find(func_table, lcname, strlen(lcname)+1, (void**)&fbc) == SUCCESS) {
  207. phpdbg_notice("%s %s %s",
  208. (fbc->type == ZEND_USER_FUNCTION) ? "User" : "Internal",
  209. (fbc->common.scope) ? "Method" : "Function",
  210. fbc->common.function_name);
  211. phpdbg_print_function_helper(fbc TSRMLS_CC);
  212. } else {
  213. phpdbg_error("The function %s could not be found", func_name);
  214. }
  215. efree(lcname);
  216. return SUCCESS;
  217. } /* }}} */