phpdbg_print.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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 "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, flags) \
  27. PHPDBG_COMMAND_D_EXP(f, h, a, m, l, s, &phpdbg_prompt_commands[8], flags)
  28. const phpdbg_command_t phpdbg_print_commands[] = {
  29. PHPDBG_PRINT_COMMAND_D(exec, "print out the instructions in the main execution context", 'e', print_exec, NULL, 0, PHPDBG_ASYNC_SAFE),
  30. PHPDBG_PRINT_COMMAND_D(opline, "print out the instruction in the current opline", 'o', print_opline, NULL, 0, PHPDBG_ASYNC_SAFE),
  31. PHPDBG_PRINT_COMMAND_D(class, "print out the instructions in the specified class", 'c', print_class, NULL, "s", PHPDBG_ASYNC_SAFE),
  32. PHPDBG_PRINT_COMMAND_D(method, "print out the instructions in the specified method", 'm', print_method, NULL, "m", PHPDBG_ASYNC_SAFE),
  33. PHPDBG_PRINT_COMMAND_D(func, "print out the instructions in the specified function", 'f', print_func, NULL, "s", PHPDBG_ASYNC_SAFE),
  34. PHPDBG_PRINT_COMMAND_D(stack, "print out the instructions in the current stack", 's', print_stack, NULL, 0, PHPDBG_ASYNC_SAFE),
  35. PHPDBG_END_COMMAND
  36. };
  37. PHPDBG_PRINT(opline) /* {{{ */
  38. {
  39. if (PHPDBG_G(in_execution) && EG(current_execute_data)) {
  40. phpdbg_print_opline(phpdbg_user_execute_data(EG(current_execute_data)), 1);
  41. } else {
  42. phpdbg_error("inactive", "type=\"execution\"", "Not Executing!");
  43. }
  44. return SUCCESS;
  45. } /* }}} */
  46. static inline void phpdbg_print_function_helper(zend_function *method) /* {{{ */
  47. {
  48. switch (method->type) {
  49. case ZEND_USER_FUNCTION: {
  50. zend_op_array* op_array = &(method->op_array);
  51. if (op_array) {
  52. zend_op *opline = &(op_array->opcodes[0]);
  53. uint32_t opcode = 0,
  54. end = op_array->last-1;
  55. if (method->common.scope) {
  56. phpdbg_writeln("printoplineinfo", "type=\"User\" startline=\"%d\" endline=\"%d\" method=\"%s::%s\" file=\"%s\" opline=\"%p\"", "L%d-%d %s::%s() %s - %p + %d ops",
  57. op_array->line_start,
  58. op_array->line_end,
  59. ZSTR_VAL(method->common.scope->name),
  60. ZSTR_VAL(method->common.function_name),
  61. op_array->filename ? ZSTR_VAL(op_array->filename) : "unknown",
  62. opline,
  63. op_array->last);
  64. } else {
  65. phpdbg_writeln("printoplineinfo", "type=\"User\" startline=\"%d\" endline=\"%d\" function=\"%s\" file=\"%s\" opline=\"%p\"", "L%d-%d %s() %s - %p + %d ops",
  66. op_array->line_start,
  67. op_array->line_end,
  68. method->common.function_name ? ZSTR_VAL(method->common.function_name) : "{main}",
  69. op_array->filename ? ZSTR_VAL(op_array->filename) : "unknown",
  70. opline,
  71. op_array->last);
  72. }
  73. do {
  74. char *decode = phpdbg_decode_opline(op_array, opline);
  75. phpdbg_writeln("print", "line=\"%u\" opnum=\"%u\" op=\"%s\"", " L%-4u #%-5u %s",
  76. opline->lineno,
  77. opcode,
  78. decode);
  79. efree(decode);
  80. opline++;
  81. } while (opcode++ < end);
  82. }
  83. } break;
  84. default: {
  85. if (method->common.scope) {
  86. phpdbg_writeln("printoplineinfo", "type=\"Internal\" method=\"%s::%s\"", "\tInternal %s::%s()", ZSTR_VAL(method->common.scope->name), ZSTR_VAL(method->common.function_name));
  87. } else {
  88. phpdbg_writeln("printoplineinfo", "type=\"Internal\" function=\"%s\"", "\tInternal %s()", ZSTR_VAL(method->common.function_name));
  89. }
  90. }
  91. }
  92. } /* }}} */
  93. PHPDBG_PRINT(exec) /* {{{ */
  94. {
  95. if (PHPDBG_G(exec)) {
  96. if (!PHPDBG_G(ops) && !(PHPDBG_G(flags) & PHPDBG_IN_SIGNAL_HANDLER)) {
  97. phpdbg_compile();
  98. }
  99. if (PHPDBG_G(ops)) {
  100. phpdbg_notice("printinfo", "file=\"%s\" num=\"%d\"", "Context %s (%d ops)", PHPDBG_G(exec), PHPDBG_G(ops)->last);
  101. phpdbg_print_function_helper((zend_function*) PHPDBG_G(ops));
  102. }
  103. } else {
  104. phpdbg_error("inactive", "type=\"nocontext\"", "No execution context set");
  105. }
  106. return SUCCESS;
  107. } /* }}} */
  108. PHPDBG_PRINT(stack) /* {{{ */
  109. {
  110. if (PHPDBG_G(in_execution) && EG(current_execute_data)) {
  111. zend_op_array *ops = &phpdbg_user_execute_data(EG(current_execute_data))->func->op_array;
  112. if (ops->function_name) {
  113. if (ops->scope) {
  114. phpdbg_notice("printinfo", "method=\"%s::%s\" num=\"%d\"", "Stack in %s::%s() (%d ops)", ZSTR_VAL(ops->scope->name), ZSTR_VAL(ops->function_name), ops->last);
  115. } else {
  116. phpdbg_notice("printinfo", "function=\"%s\" num=\"%d\"", "Stack in %s() (%d ops)", ZSTR_VAL(ops->function_name), ops->last);
  117. }
  118. } else {
  119. if (ops->filename) {
  120. phpdbg_notice("printinfo", "file=\"%s\" num=\"%d\"", "Stack in %s (%d ops)", ZSTR_VAL(ops->filename), ops->last);
  121. } else {
  122. phpdbg_notice("printinfo", "opline=\"%p\" num=\"%d\"", "Stack @ %p (%d ops)", ops, ops->last);
  123. }
  124. }
  125. phpdbg_print_function_helper((zend_function*) ops);
  126. } else {
  127. phpdbg_error("inactive", "type=\"execution\"", "Not Executing!");
  128. }
  129. return SUCCESS;
  130. } /* }}} */
  131. PHPDBG_PRINT(class) /* {{{ */
  132. {
  133. zend_class_entry *ce;
  134. if (phpdbg_safe_class_lookup(param->str, param->len, &ce) == SUCCESS) {
  135. phpdbg_notice("printinfo", "type=\"%s\" flag=\"%s\" class=\"%s\" num=\"%d\"", "%s %s: %s (%d methods)",
  136. (ce->type == ZEND_USER_CLASS) ?
  137. "User" : "Internal",
  138. (ce->ce_flags & ZEND_ACC_INTERFACE) ?
  139. "Interface" :
  140. (ce->ce_flags & ZEND_ACC_ABSTRACT) ?
  141. "Abstract Class" :
  142. "Class",
  143. ZSTR_VAL(ce->name),
  144. zend_hash_num_elements(&ce->function_table));
  145. phpdbg_xml("<printmethods %r>");
  146. if (zend_hash_num_elements(&ce->function_table)) {
  147. zend_function *method;
  148. ZEND_HASH_FOREACH_PTR(&ce->function_table, method) {
  149. phpdbg_print_function_helper(method);
  150. } ZEND_HASH_FOREACH_END();
  151. }
  152. phpdbg_xml("</printmethods>");
  153. } else {
  154. phpdbg_error("print", "type=\"noclass\" class=\"%s\"", "The class %s could not be found", param->str);
  155. }
  156. return SUCCESS;
  157. } /* }}} */
  158. PHPDBG_PRINT(method) /* {{{ */
  159. {
  160. zend_class_entry *ce;
  161. if (phpdbg_safe_class_lookup(param->method.class, strlen(param->method.class), &ce) == SUCCESS) {
  162. zend_function *fbc;
  163. zend_string *lcname = zend_string_alloc(strlen(param->method.name), 0);
  164. zend_str_tolower_copy(ZSTR_VAL(lcname), param->method.name, ZSTR_LEN(lcname));
  165. if ((fbc = zend_hash_find_ptr(&ce->function_table, lcname))) {
  166. phpdbg_notice("printinfo", "type=\"%s\" flags=\"Method\" symbol=\"%s\" num=\"%d\"", "%s Method %s (%d ops)",
  167. (fbc->type == ZEND_USER_FUNCTION) ? "User" : "Internal",
  168. ZSTR_VAL(fbc->common.function_name),
  169. (fbc->type == ZEND_USER_FUNCTION) ? fbc->op_array.last : 0);
  170. phpdbg_print_function_helper(fbc);
  171. } else {
  172. phpdbg_error("print", "type=\"nomethod\" method=\"%s::%s\"", "The method %s::%s could not be found", param->method.class, param->method.name);
  173. }
  174. zend_string_release(lcname);
  175. } else {
  176. phpdbg_error("print", "type=\"noclass\" class=\"%s\"", "The class %s could not be found", param->method.class);
  177. }
  178. return SUCCESS;
  179. } /* }}} */
  180. PHPDBG_PRINT(func) /* {{{ */
  181. {
  182. HashTable *func_table = EG(function_table);
  183. zend_function* fbc;
  184. const char *func_name = param->str;
  185. size_t func_name_len = param->len;
  186. zend_string *lcname;
  187. /* search active scope if begins with period */
  188. if (func_name[0] == '.') {
  189. zend_class_entry *scope = zend_get_executed_scope();
  190. if (scope) {
  191. func_name++;
  192. func_name_len--;
  193. func_table = &scope->function_table;
  194. } else {
  195. phpdbg_error("inactive", "type=\"noclasses\"", "No active class");
  196. return SUCCESS;
  197. }
  198. } else if (!EG(function_table)) {
  199. phpdbg_error("inactive", "type=\"function_table\"", "No function table loaded");
  200. return SUCCESS;
  201. } else {
  202. func_table = EG(function_table);
  203. }
  204. lcname = zend_string_alloc(func_name_len, 0);
  205. zend_str_tolower_copy(ZSTR_VAL(lcname), func_name, ZSTR_LEN(lcname));
  206. phpdbg_try_access {
  207. if ((fbc = zend_hash_find_ptr(func_table, lcname))) {
  208. phpdbg_notice("printinfo", "type=\"%s\" flags=\"%s\" symbol=\"%s\" num=\"%d\"", "%s %s %s (%d ops)",
  209. (fbc->type == ZEND_USER_FUNCTION) ? "User" : "Internal",
  210. (fbc->common.scope) ? "Method" : "Function",
  211. ZSTR_VAL(fbc->common.function_name),
  212. (fbc->type == ZEND_USER_FUNCTION) ? fbc->op_array.last : 0);
  213. phpdbg_print_function_helper(fbc);
  214. } else {
  215. phpdbg_error("print", "type=\"nofunction\" function=\"%s\"", "The function %s could not be found", func_name);
  216. }
  217. } phpdbg_catch_access {
  218. phpdbg_error("signalsegv", "function=\"%.*s\"", "Couldn't fetch function %.*s, invalid data source", (int) func_name_len, func_name);
  219. } phpdbg_end_try_access();
  220. efree(lcname);
  221. return SUCCESS;
  222. } /* }}} */
  223. void phpdbg_print_opcodes_main() {
  224. phpdbg_out("function name: (null)\n");
  225. phpdbg_print_function_helper((zend_function *) PHPDBG_G(ops));
  226. }
  227. void phpdbg_print_opcodes_function(const char *function, size_t len) {
  228. zend_function *func = zend_hash_str_find_ptr(EG(function_table), function, len);
  229. if (!func) {
  230. zend_string *rt_name;
  231. ZEND_HASH_FOREACH_STR_KEY_PTR(EG(class_table), rt_name, func) {
  232. if (func->type == ZEND_USER_FUNCTION && *rt_name->val == '\0') {
  233. if (func->op_array.function_name->len == len && !zend_binary_strcasecmp(function, len, func->op_array.function_name->val, func->op_array.function_name->len)) {
  234. phpdbg_print_opcodes_function(rt_name->val, rt_name->len);
  235. }
  236. }
  237. } ZEND_HASH_FOREACH_END();
  238. return;
  239. }
  240. phpdbg_out("function name: %.*s\n", (int) ZSTR_LEN(func->op_array.function_name), ZSTR_VAL(func->op_array.function_name));
  241. phpdbg_print_function_helper(func);
  242. }
  243. static void phpdbg_print_opcodes_method_ce(zend_class_entry *ce, const char *function) {
  244. zend_function *func;
  245. if (ce->type != ZEND_USER_CLASS) {
  246. phpdbg_out("function name: %s::%s (internal)\n", ce->name->val, function);
  247. return;
  248. }
  249. if (!(func = zend_hash_str_find_ptr(&ce->function_table, function, strlen(function)))) {
  250. return;
  251. }
  252. phpdbg_out("function name: %s::%s\n", ce->name->val, function);
  253. phpdbg_print_function_helper(func);
  254. }
  255. void phpdbg_print_opcodes_method(const char *class, const char *function) {
  256. zend_class_entry *ce;
  257. if (phpdbg_safe_class_lookup(class, strlen(class), &ce) != SUCCESS) {
  258. zend_string *rt_name;
  259. ZEND_HASH_FOREACH_STR_KEY_PTR(EG(class_table), rt_name, ce) {
  260. if (ce->type == ZEND_USER_CLASS && *rt_name->val == '\0') {
  261. if (ce->name->len == strlen(class) && !zend_binary_strcasecmp(class, strlen(class), ce->name->val, ce->name->len)) {
  262. phpdbg_print_opcodes_method_ce(ce, function);
  263. }
  264. }
  265. } ZEND_HASH_FOREACH_END();
  266. return;
  267. }
  268. phpdbg_print_opcodes_method_ce(ce, function);
  269. }
  270. static void phpdbg_print_opcodes_ce(zend_class_entry *ce) {
  271. zend_function *method;
  272. zend_string *method_name;
  273. zend_bool first = 1;
  274. phpdbg_out("%s %s: %s\n",
  275. (ce->type == ZEND_USER_CLASS) ?
  276. "user" : "internal",
  277. (ce->ce_flags & ZEND_ACC_INTERFACE) ?
  278. "interface" :
  279. (ce->ce_flags & ZEND_ACC_ABSTRACT) ?
  280. "abstract Class" :
  281. "class",
  282. ZSTR_VAL(ce->name));
  283. if (ce->type != ZEND_USER_CLASS) {
  284. return;
  285. }
  286. phpdbg_out("%d methods: ", zend_hash_num_elements(&ce->function_table));
  287. ZEND_HASH_FOREACH_PTR(&ce->function_table, method) {
  288. if (first) {
  289. first = 0;
  290. } else {
  291. phpdbg_out(", ");
  292. }
  293. phpdbg_out("%s", ZSTR_VAL(method->common.function_name));
  294. } ZEND_HASH_FOREACH_END();
  295. if (first) {
  296. phpdbg_out("-");
  297. }
  298. phpdbg_out("\n");
  299. ZEND_HASH_FOREACH_STR_KEY_PTR(&ce->function_table, method_name, method) {
  300. phpdbg_out("\nfunction name: %s\n", ZSTR_VAL(method_name));
  301. phpdbg_print_function_helper(method);
  302. } ZEND_HASH_FOREACH_END();
  303. }
  304. void phpdbg_print_opcodes_class(const char *class) {
  305. zend_class_entry *ce;
  306. if (phpdbg_safe_class_lookup(class, strlen(class), &ce) != SUCCESS) {
  307. zend_string *rt_name;
  308. ZEND_HASH_FOREACH_STR_KEY_PTR(EG(class_table), rt_name, ce) {
  309. if (ce->type == ZEND_USER_CLASS && *rt_name->val == '\0') {
  310. if (ce->name->len == strlen(class) && !zend_binary_strcasecmp(class, strlen(class), ce->name->val, ce->name->len)) {
  311. phpdbg_print_opcodes_ce(ce);
  312. }
  313. }
  314. } ZEND_HASH_FOREACH_END();
  315. return;
  316. }
  317. phpdbg_print_opcodes_ce(ce);
  318. }
  319. PHPDBG_API void phpdbg_print_opcodes(char *function)
  320. {
  321. if (function == NULL) {
  322. phpdbg_print_opcodes_main();
  323. } else if (function[0] == '*' && function[1] == 0) {
  324. /* all */
  325. zend_string *name;
  326. zend_function *func;
  327. zend_class_entry *ce;
  328. phpdbg_print_opcodes_main();
  329. ZEND_HASH_FOREACH_STR_KEY_PTR(EG(function_table), name, func) {
  330. if (func->type == ZEND_USER_FUNCTION) {
  331. phpdbg_out("\n");
  332. phpdbg_print_opcodes_function(ZSTR_VAL(name), ZSTR_LEN(name));
  333. }
  334. } ZEND_HASH_FOREACH_END();
  335. ZEND_HASH_FOREACH_PTR(EG(class_table), ce) {
  336. if (ce->type == ZEND_USER_CLASS) {
  337. phpdbg_out("\n\n");
  338. phpdbg_print_opcodes_ce(ce);
  339. }
  340. } ZEND_HASH_FOREACH_END();
  341. } else {
  342. function = zend_str_tolower_dup(function, strlen(function));
  343. if (strstr(function, "::") == NULL) {
  344. phpdbg_print_opcodes_function(function, strlen(function));
  345. } else {
  346. char *method_name, *class_name = strtok(function, "::");
  347. if ((method_name = strtok(NULL, "::")) == NULL) {
  348. phpdbg_print_opcodes_class(class_name);
  349. } else {
  350. phpdbg_print_opcodes_method(class_name, method_name);
  351. }
  352. }
  353. efree(function);
  354. }
  355. }