phpdbg_print.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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 "phpdbg.h"
  19. #include "phpdbg_print.h"
  20. #include "phpdbg_utils.h"
  21. #include "phpdbg_prompt.h"
  22. #include "Optimizer/zend_dump.h"
  23. ZEND_EXTERN_MODULE_GLOBALS(phpdbg)
  24. #define PHPDBG_PRINT_COMMAND_D(f, h, a, m, l, s, flags) \
  25. PHPDBG_COMMAND_D_EXP(f, h, a, m, l, s, &phpdbg_prompt_commands[8], flags)
  26. const phpdbg_command_t phpdbg_print_commands[] = {
  27. PHPDBG_PRINT_COMMAND_D(exec, "print out the instructions in the main execution context", 'e', print_exec, NULL, 0, PHPDBG_ASYNC_SAFE),
  28. PHPDBG_PRINT_COMMAND_D(opline, "print out the instruction in the current opline", 'o', print_opline, NULL, 0, PHPDBG_ASYNC_SAFE),
  29. PHPDBG_PRINT_COMMAND_D(class, "print out the instructions in the specified class", 'c', print_class, NULL, "s", PHPDBG_ASYNC_SAFE),
  30. PHPDBG_PRINT_COMMAND_D(method, "print out the instructions in the specified method", 'm', print_method, NULL, "m", PHPDBG_ASYNC_SAFE),
  31. PHPDBG_PRINT_COMMAND_D(func, "print out the instructions in the specified function", 'f', print_func, NULL, "s", PHPDBG_ASYNC_SAFE),
  32. PHPDBG_PRINT_COMMAND_D(stack, "print out the instructions in the current stack", 's', print_stack, NULL, 0, PHPDBG_ASYNC_SAFE),
  33. PHPDBG_END_COMMAND
  34. };
  35. PHPDBG_PRINT(opline) /* {{{ */
  36. {
  37. if (PHPDBG_G(in_execution) && EG(current_execute_data)) {
  38. phpdbg_print_opline(phpdbg_user_execute_data(EG(current_execute_data)), 1);
  39. } else {
  40. phpdbg_error("Not Executing!");
  41. }
  42. return SUCCESS;
  43. } /* }}} */
  44. static inline void phpdbg_print_function_helper(zend_function *method) /* {{{ */
  45. {
  46. switch (method->type) {
  47. case ZEND_USER_FUNCTION: {
  48. zend_op_array* op_array = &(method->op_array);
  49. if (op_array) {
  50. zend_dump_op_array(op_array, ZEND_DUMP_LINE_NUMBERS, NULL, NULL);
  51. for (uint32_t i = 0; i < op_array->num_dynamic_func_defs; i++) {
  52. zend_op_array *def = op_array->dynamic_func_defs[i];
  53. phpdbg_out("\ndynamic def: %i, function name: %.*s\n",
  54. i, (int) ZSTR_LEN(def->function_name), ZSTR_VAL(def->function_name));
  55. zend_dump_op_array(def, ZEND_DUMP_LINE_NUMBERS, NULL, NULL);
  56. }
  57. }
  58. } break;
  59. default: {
  60. if (method->common.scope) {
  61. phpdbg_writeln("\tInternal %s::%s()", ZSTR_VAL(method->common.scope->name), ZSTR_VAL(method->common.function_name));
  62. } else {
  63. phpdbg_writeln("\tInternal %s()", ZSTR_VAL(method->common.function_name));
  64. }
  65. }
  66. }
  67. } /* }}} */
  68. PHPDBG_PRINT(exec) /* {{{ */
  69. {
  70. if (PHPDBG_G(exec)) {
  71. if (!PHPDBG_G(ops) && !(PHPDBG_G(flags) & PHPDBG_IN_SIGNAL_HANDLER)) {
  72. phpdbg_compile();
  73. }
  74. if (PHPDBG_G(ops)) {
  75. phpdbg_notice("Context %s (%d ops)", PHPDBG_G(exec), PHPDBG_G(ops)->last);
  76. phpdbg_print_function_helper((zend_function*) PHPDBG_G(ops));
  77. }
  78. } else {
  79. phpdbg_error("No execution context set");
  80. }
  81. return SUCCESS;
  82. } /* }}} */
  83. PHPDBG_PRINT(stack) /* {{{ */
  84. {
  85. if (PHPDBG_G(in_execution) && EG(current_execute_data)) {
  86. zend_op_array *ops = &phpdbg_user_execute_data(EG(current_execute_data))->func->op_array;
  87. if (ops->function_name) {
  88. if (ops->scope) {
  89. phpdbg_notice("Stack in %s::%s() (%d ops)", ZSTR_VAL(ops->scope->name), ZSTR_VAL(ops->function_name), ops->last);
  90. } else {
  91. phpdbg_notice("Stack in %s() (%d ops)", ZSTR_VAL(ops->function_name), ops->last);
  92. }
  93. } else {
  94. if (ops->filename) {
  95. phpdbg_notice("Stack in %s (%d ops)", ZSTR_VAL(ops->filename), ops->last);
  96. } else {
  97. phpdbg_notice("Stack @ %p (%d ops)", ops, ops->last);
  98. }
  99. }
  100. phpdbg_print_function_helper((zend_function*) ops);
  101. } else {
  102. phpdbg_error("Not Executing!");
  103. }
  104. return SUCCESS;
  105. } /* }}} */
  106. PHPDBG_PRINT(class) /* {{{ */
  107. {
  108. zend_class_entry *ce;
  109. if (phpdbg_safe_class_lookup(param->str, param->len, &ce) == SUCCESS) {
  110. phpdbg_notice("%s %s: %s (%d methods)",
  111. (ce->type == ZEND_USER_CLASS) ?
  112. "User" : "Internal",
  113. (ce->ce_flags & ZEND_ACC_INTERFACE) ?
  114. "Interface" :
  115. (ce->ce_flags & ZEND_ACC_ABSTRACT) ?
  116. "Abstract Class" :
  117. "Class",
  118. ZSTR_VAL(ce->name),
  119. zend_hash_num_elements(&ce->function_table));
  120. if (zend_hash_num_elements(&ce->function_table)) {
  121. zend_function *method;
  122. ZEND_HASH_FOREACH_PTR(&ce->function_table, method) {
  123. phpdbg_print_function_helper(method);
  124. } ZEND_HASH_FOREACH_END();
  125. }
  126. } else {
  127. phpdbg_error("The class %s could not be found", param->str);
  128. }
  129. return SUCCESS;
  130. } /* }}} */
  131. PHPDBG_PRINT(method) /* {{{ */
  132. {
  133. zend_class_entry *ce;
  134. if (phpdbg_safe_class_lookup(param->method.class, strlen(param->method.class), &ce) == SUCCESS) {
  135. zend_function *fbc;
  136. zend_string *lcname = zend_string_alloc(strlen(param->method.name), 0);
  137. zend_str_tolower_copy(ZSTR_VAL(lcname), param->method.name, ZSTR_LEN(lcname));
  138. if ((fbc = zend_hash_find_ptr(&ce->function_table, lcname))) {
  139. phpdbg_notice("%s Method %s (%d ops)",
  140. (fbc->type == ZEND_USER_FUNCTION) ? "User" : "Internal",
  141. ZSTR_VAL(fbc->common.function_name),
  142. (fbc->type == ZEND_USER_FUNCTION) ? fbc->op_array.last : 0);
  143. phpdbg_print_function_helper(fbc);
  144. } else {
  145. phpdbg_error("The method %s::%s could not be found", param->method.class, param->method.name);
  146. }
  147. zend_string_release(lcname);
  148. } else {
  149. phpdbg_error("The class %s could not be found", param->method.class);
  150. }
  151. return SUCCESS;
  152. } /* }}} */
  153. PHPDBG_PRINT(func) /* {{{ */
  154. {
  155. HashTable *func_table = EG(function_table);
  156. zend_function* fbc;
  157. const char *func_name = param->str;
  158. size_t func_name_len = param->len;
  159. zend_string *lcname;
  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 SUCCESS;
  170. }
  171. } else if (!EG(function_table)) {
  172. phpdbg_error("No function table loaded");
  173. return SUCCESS;
  174. } else {
  175. func_table = EG(function_table);
  176. }
  177. lcname = zend_string_alloc(func_name_len, 0);
  178. zend_str_tolower_copy(ZSTR_VAL(lcname), func_name, ZSTR_LEN(lcname));
  179. phpdbg_try_access {
  180. if ((fbc = zend_hash_find_ptr(func_table, lcname))) {
  181. phpdbg_notice("%s %s %s (%d ops)",
  182. (fbc->type == ZEND_USER_FUNCTION) ? "User" : "Internal",
  183. (fbc->common.scope) ? "Method" : "Function",
  184. ZSTR_VAL(fbc->common.function_name),
  185. (fbc->type == ZEND_USER_FUNCTION) ? fbc->op_array.last : 0);
  186. phpdbg_print_function_helper(fbc);
  187. } else {
  188. phpdbg_error("The function %s could not be found", func_name);
  189. }
  190. } phpdbg_catch_access {
  191. phpdbg_error("Couldn't fetch function %.*s, invalid data source", (int) func_name_len, func_name);
  192. } phpdbg_end_try_access();
  193. efree(lcname);
  194. return SUCCESS;
  195. } /* }}} */
  196. void phpdbg_print_opcodes_main(void) {
  197. phpdbg_print_function_helper((zend_function *) PHPDBG_G(ops));
  198. }
  199. void phpdbg_print_opcodes_function(const char *function, size_t len) {
  200. zend_function *func = zend_hash_str_find_ptr(EG(function_table), function, len);
  201. if (!func) {
  202. phpdbg_error("The function %s could not be found", function);
  203. return;
  204. }
  205. phpdbg_print_function_helper(func);
  206. }
  207. static void phpdbg_print_opcodes_method_ce(zend_class_entry *ce, const char *function) {
  208. zend_function *func;
  209. if (ce->type != ZEND_USER_CLASS) {
  210. phpdbg_out("function name: %s::%s (internal)\n", ce->name->val, function);
  211. return;
  212. }
  213. if (!(func = zend_hash_str_find_ptr(&ce->function_table, function, strlen(function)))) {
  214. phpdbg_error("The method %s::%s could not be found", ZSTR_VAL(ce->name), function);
  215. return;
  216. }
  217. phpdbg_print_function_helper(func);
  218. }
  219. void phpdbg_print_opcodes_method(const char *class, const char *function) {
  220. zend_class_entry *ce;
  221. if (phpdbg_safe_class_lookup(class, strlen(class), &ce) != SUCCESS) {
  222. phpdbg_error("The class %s could not be found", class);
  223. return;
  224. }
  225. phpdbg_print_opcodes_method_ce(ce, function);
  226. }
  227. static void phpdbg_print_opcodes_ce(zend_class_entry *ce) {
  228. zend_function *method;
  229. bool first = 1;
  230. phpdbg_out("%s %s: %s\n",
  231. (ce->type == ZEND_USER_CLASS) ?
  232. "user" : "internal",
  233. (ce->ce_flags & ZEND_ACC_INTERFACE) ?
  234. "interface" :
  235. (ce->ce_flags & ZEND_ACC_ABSTRACT) ?
  236. "abstract Class" :
  237. "class",
  238. ZSTR_VAL(ce->name));
  239. if (ce->type != ZEND_USER_CLASS) {
  240. return;
  241. }
  242. phpdbg_out("%d methods: ", zend_hash_num_elements(&ce->function_table));
  243. ZEND_HASH_FOREACH_PTR(&ce->function_table, method) {
  244. if (first) {
  245. first = 0;
  246. } else {
  247. phpdbg_out(", ");
  248. }
  249. phpdbg_out("%s", ZSTR_VAL(method->common.function_name));
  250. } ZEND_HASH_FOREACH_END();
  251. if (first) {
  252. phpdbg_out("-");
  253. }
  254. phpdbg_out("\n");
  255. ZEND_HASH_FOREACH_PTR(&ce->function_table, method) {
  256. phpdbg_print_function_helper(method);
  257. } ZEND_HASH_FOREACH_END();
  258. }
  259. void phpdbg_print_opcodes_class(const char *class) {
  260. zend_class_entry *ce;
  261. if (phpdbg_safe_class_lookup(class, strlen(class), &ce) != SUCCESS) {
  262. phpdbg_error("The class %s could not be found", class);
  263. return;
  264. }
  265. phpdbg_print_opcodes_ce(ce);
  266. }
  267. void phpdbg_print_opcodes(const char *function)
  268. {
  269. if (function == NULL) {
  270. phpdbg_print_opcodes_main();
  271. } else if (function[0] == '*' && function[1] == 0) {
  272. /* all */
  273. zend_string *name;
  274. zend_function *func;
  275. zend_class_entry *ce;
  276. phpdbg_print_opcodes_main();
  277. ZEND_HASH_FOREACH_STR_KEY_PTR(EG(function_table), name, func) {
  278. if (func->type == ZEND_USER_FUNCTION) {
  279. phpdbg_print_opcodes_function(ZSTR_VAL(name), ZSTR_LEN(name));
  280. }
  281. } ZEND_HASH_FOREACH_END();
  282. ZEND_HASH_FOREACH_PTR(EG(class_table), ce) {
  283. if (ce->type == ZEND_USER_CLASS) {
  284. phpdbg_out("\n");
  285. phpdbg_print_opcodes_ce(ce);
  286. }
  287. } ZEND_HASH_FOREACH_END();
  288. } else {
  289. char *function_lowercase = zend_str_tolower_dup(function, strlen(function));
  290. if (strstr(function_lowercase, "::") == NULL) {
  291. phpdbg_print_opcodes_function(function_lowercase, strlen(function_lowercase));
  292. } else {
  293. char *method_name, *class_name = strtok(function_lowercase, "::");
  294. if ((method_name = strtok(NULL, "::")) == NULL) {
  295. phpdbg_print_opcodes_class(class_name);
  296. } else {
  297. phpdbg_print_opcodes_method(class_name, method_name);
  298. }
  299. }
  300. efree(function_lowercase);
  301. }
  302. }
  303. void phpdbg_print_opline(zend_execute_data *execute_data, bool ignore_flags) /* {{{ */
  304. {
  305. if (ignore_flags || (!(PHPDBG_G(flags) & PHPDBG_IS_QUIET) && (PHPDBG_G(flags) & PHPDBG_IS_STEPPING))) {
  306. zend_dump_op_line(&EX(func)->op_array, NULL, EX(opline), ZEND_DUMP_LINE_NUMBERS, NULL);
  307. }
  308. if (PHPDBG_G(oplog_list)) {
  309. phpdbg_oplog_entry *cur = zend_arena_alloc(&PHPDBG_G(oplog_arena), sizeof(phpdbg_oplog_entry));
  310. zend_op_array *op_array = &EX(func)->op_array;
  311. cur->op = (zend_op *) EX(opline);
  312. cur->opcodes = op_array->opcodes;
  313. cur->filename = op_array->filename;
  314. cur->scope = op_array->scope;
  315. cur->function_name = op_array->function_name;
  316. cur->next = NULL;
  317. PHPDBG_G(oplog_cur)->next = cur;
  318. PHPDBG_G(oplog_cur) = cur;
  319. }
  320. } /* }}} */