phpdbg_frame.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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 "zend.h"
  19. #include "phpdbg.h"
  20. #include "phpdbg_utils.h"
  21. #include "phpdbg_frame.h"
  22. #include "phpdbg_list.h"
  23. #include "zend_smart_str.h"
  24. ZEND_EXTERN_MODULE_GLOBALS(phpdbg)
  25. static inline void phpdbg_append_individual_arg(smart_str *s, uint32_t i, zend_function *func, zval *arg) {
  26. const zend_arg_info *arginfo = func->common.arg_info;
  27. char *arg_name = NULL;
  28. if (i) {
  29. smart_str_appends(s, ", ");
  30. }
  31. if (i < func->common.num_args) {
  32. if (arginfo) {
  33. if (func->type == ZEND_INTERNAL_FUNCTION) {
  34. arg_name = (char *) ((zend_internal_arg_info *) &arginfo[i])->name;
  35. } else {
  36. arg_name = ZSTR_VAL(arginfo[i].name);
  37. }
  38. }
  39. smart_str_appends(s, arg_name ? arg_name : "?");
  40. smart_str_appendc(s, '=');
  41. }
  42. {
  43. char *arg_print = phpdbg_short_zval_print(arg, 40);
  44. smart_str_appends(s, arg_print);
  45. efree(arg_print);
  46. }
  47. }
  48. zend_string *phpdbg_compile_stackframe(zend_execute_data *ex) {
  49. smart_str s = {0};
  50. zend_op_array *op_array = &ex->func->op_array;
  51. uint32_t i = 0, first_extra_arg = op_array->num_args, num_args = ZEND_CALL_NUM_ARGS(ex);
  52. zval *p = ZEND_CALL_ARG(ex, 1);
  53. if (op_array->scope) {
  54. smart_str_append(&s, op_array->scope->name);
  55. smart_str_appends(&s, "::");
  56. }
  57. smart_str_append(&s, op_array->function_name);
  58. smart_str_appendc(&s, '(');
  59. if (ZEND_CALL_NUM_ARGS(ex) > first_extra_arg) {
  60. while (i < first_extra_arg) {
  61. phpdbg_append_individual_arg(&s, i, ex->func, p);
  62. p++;
  63. i++;
  64. }
  65. p = ZEND_CALL_VAR_NUM(ex, op_array->last_var + op_array->T);
  66. }
  67. while (i < num_args) {
  68. phpdbg_append_individual_arg(&s, i, ex->func, p);
  69. p++;
  70. i++;
  71. }
  72. smart_str_appendc(&s, ')');
  73. if (ex->func->type == ZEND_USER_FUNCTION) {
  74. smart_str_appends(&s, " at ");
  75. smart_str_append(&s, op_array->filename);
  76. smart_str_appendc(&s, ':');
  77. smart_str_append_unsigned(&s, ex->opline->lineno);
  78. } else {
  79. smart_str_appends(&s, " [internal function]");
  80. }
  81. return s.s;
  82. }
  83. void phpdbg_print_cur_frame_info(void) {
  84. const char *file_chr = zend_get_executed_filename();
  85. zend_string *file = zend_string_init(file_chr, strlen(file_chr), 0);
  86. phpdbg_list_file(file, 3, zend_get_executed_lineno() - 1, zend_get_executed_lineno());
  87. efree(file);
  88. }
  89. void phpdbg_restore_frame(void) /* {{{ */
  90. {
  91. if (PHPDBG_FRAME(num) == 0) {
  92. return;
  93. }
  94. if (PHPDBG_FRAME(generator)) {
  95. if (PHPDBG_FRAME(generator)->execute_data->call) {
  96. PHPDBG_FRAME(generator)->frozen_call_stack = zend_generator_freeze_call_stack(PHPDBG_FRAME(generator)->execute_data);
  97. }
  98. PHPDBG_FRAME(generator) = NULL;
  99. }
  100. PHPDBG_FRAME(num) = 0;
  101. /* move things back */
  102. EG(current_execute_data) = PHPDBG_FRAME(execute_data);
  103. } /* }}} */
  104. void phpdbg_switch_frame(int frame) /* {{{ */
  105. {
  106. zend_execute_data *execute_data = PHPDBG_FRAME(num) ? PHPDBG_FRAME(execute_data) : EG(current_execute_data);
  107. int i = 0;
  108. if (PHPDBG_FRAME(num) == frame) {
  109. phpdbg_notice("Already in frame #%d", frame);
  110. return;
  111. }
  112. phpdbg_try_access {
  113. while (execute_data) {
  114. if (i++ == frame) {
  115. break;
  116. }
  117. do {
  118. execute_data = execute_data->prev_execute_data;
  119. } while (execute_data && execute_data->opline == NULL);
  120. }
  121. } phpdbg_catch_access {
  122. phpdbg_error("Couldn't switch frames, invalid data source");
  123. return;
  124. } phpdbg_end_try_access();
  125. if (execute_data == NULL) {
  126. phpdbg_error("No frame #%d", frame);
  127. return;
  128. }
  129. phpdbg_restore_frame();
  130. if (frame > 0) {
  131. PHPDBG_FRAME(num) = frame;
  132. /* backup things and jump back */
  133. PHPDBG_FRAME(execute_data) = EG(current_execute_data);
  134. EG(current_execute_data) = execute_data;
  135. }
  136. phpdbg_try_access {
  137. zend_string *s = phpdbg_compile_stackframe(EG(current_execute_data));
  138. phpdbg_notice("Switched to frame #%d: %.*s", frame, (int) ZSTR_LEN(s), ZSTR_VAL(s));
  139. zend_string_release(s);
  140. } phpdbg_catch_access {
  141. phpdbg_notice("Switched to frame #%d", frame);
  142. } phpdbg_end_try_access();
  143. phpdbg_print_cur_frame_info();
  144. } /* }}} */
  145. static void phpdbg_dump_prototype(zval *tmp) /* {{{ */
  146. {
  147. zval *funcname, *class, class_zv, *args, *argstmp;
  148. funcname = zend_hash_str_find(Z_ARRVAL_P(tmp), ZEND_STRL("function"));
  149. if ((class = zend_hash_str_find(Z_ARRVAL_P(tmp), ZEND_STRL("object")))) {
  150. ZVAL_NEW_STR(&class_zv, Z_OBJCE_P(class)->name);
  151. class = &class_zv;
  152. } else {
  153. class = zend_hash_str_find(Z_ARRVAL_P(tmp), ZEND_STRL("class"));
  154. }
  155. if (class) {
  156. zval *type = zend_hash_str_find(Z_ARRVAL_P(tmp), ZEND_STRL("type"));
  157. phpdbg_out("%s%s%s(", Z_STRVAL_P(class), Z_STRVAL_P(type), Z_STRVAL_P(funcname));
  158. } else {
  159. phpdbg_out("%s(", Z_STRVAL_P(funcname));
  160. }
  161. args = zend_hash_str_find(Z_ARRVAL_P(tmp), ZEND_STRL("args"));
  162. if (args) {
  163. const zend_function *func = NULL;
  164. const zend_arg_info *arginfo = NULL;
  165. bool is_variadic = 0;
  166. int j = 0, m;
  167. phpdbg_try_access {
  168. /* assuming no autoloader call is necessary, class should have been loaded if it's in backtrace ... */
  169. if ((func = phpdbg_get_function(Z_STRVAL_P(funcname), class ? Z_STRVAL_P(class) : NULL))) {
  170. arginfo = func->common.arg_info;
  171. }
  172. } phpdbg_end_try_access();
  173. m = func ? func->common.num_args : 0;
  174. ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(args), argstmp) {
  175. if (j) {
  176. phpdbg_out(", ");
  177. }
  178. if (m && j < m) {
  179. char *arg_name = NULL;
  180. if (arginfo) {
  181. if (func->type == ZEND_INTERNAL_FUNCTION) {
  182. arg_name = (char *)((zend_internal_arg_info *)&arginfo[j])->name;
  183. } else {
  184. arg_name = ZSTR_VAL(arginfo[j].name);
  185. }
  186. }
  187. if (!is_variadic) {
  188. is_variadic = arginfo ? ZEND_ARG_IS_VARIADIC(&arginfo[j]) : 0;
  189. }
  190. phpdbg_out("%s=%s", arg_name ? arg_name : "?", is_variadic ? "[": "");
  191. }
  192. ++j;
  193. {
  194. char *arg_print = phpdbg_short_zval_print(argstmp, 40);
  195. php_printf("%s", arg_print);
  196. efree(arg_print);
  197. }
  198. } ZEND_HASH_FOREACH_END();
  199. if (is_variadic) {
  200. phpdbg_out("]");
  201. }
  202. }
  203. phpdbg_out(")");
  204. }
  205. void phpdbg_dump_backtrace(size_t num) /* {{{ */
  206. {
  207. HashPosition position;
  208. zval zbacktrace;
  209. zval *tmp;
  210. zval startline, startfile;
  211. const char *startfilename;
  212. zval *file = &startfile, *line = &startline;
  213. int i = 0, limit = num;
  214. PHPDBG_OUTPUT_BACKUP();
  215. if (limit < 0) {
  216. phpdbg_error("Invalid backtrace size %d", limit);
  217. PHPDBG_OUTPUT_BACKUP_RESTORE();
  218. return;
  219. }
  220. phpdbg_try_access {
  221. zend_fetch_debug_backtrace(&zbacktrace, 0, 0, limit);
  222. } phpdbg_catch_access {
  223. phpdbg_error("Couldn't fetch backtrace, invalid data source");
  224. return;
  225. } phpdbg_end_try_access();
  226. Z_LVAL(startline) = zend_get_executed_lineno();
  227. startfilename = zend_get_executed_filename();
  228. Z_STR(startfile) = zend_string_init(startfilename, strlen(startfilename), 0);
  229. zend_hash_internal_pointer_reset_ex(Z_ARRVAL(zbacktrace), &position);
  230. tmp = zend_hash_get_current_data_ex(Z_ARRVAL(zbacktrace), &position);
  231. while ((tmp = zend_hash_get_current_data_ex(Z_ARRVAL(zbacktrace), &position))) {
  232. if (file) { /* userland */
  233. phpdbg_out("frame #%d: ", i);
  234. phpdbg_dump_prototype(tmp);
  235. phpdbg_out(" at %s:"ZEND_LONG_FMT"\n", Z_STRVAL_P(file), Z_LVAL_P(line));
  236. i++;
  237. } else {
  238. phpdbg_out(" => ");
  239. phpdbg_dump_prototype(tmp);
  240. phpdbg_out(" (internal function)\n");
  241. }
  242. file = zend_hash_str_find(Z_ARRVAL_P(tmp), ZEND_STRL("file"));
  243. line = zend_hash_str_find(Z_ARRVAL_P(tmp), ZEND_STRL("line"));
  244. zend_hash_move_forward_ex(Z_ARRVAL(zbacktrace), &position);
  245. }
  246. phpdbg_writeln("frame #%d: {main} at %s:"ZEND_LONG_FMT, i, Z_STRVAL_P(file), Z_LVAL_P(line));
  247. zval_ptr_dtor_nogc(&zbacktrace);
  248. zend_string_release(Z_STR(startfile));
  249. PHPDBG_OUTPUT_BACKUP_RESTORE();
  250. } /* }}} */
  251. void phpdbg_open_generator_frame(zend_generator *gen) {
  252. zend_string *s;
  253. if (EG(current_execute_data) == gen->execute_data) {
  254. return;
  255. }
  256. phpdbg_restore_frame();
  257. PHPDBG_FRAME(num) = -1;
  258. PHPDBG_FRAME(generator) = gen;
  259. EG(current_execute_data) = gen->execute_data;
  260. if (gen->frozen_call_stack) {
  261. zend_generator_restore_call_stack(gen);
  262. }
  263. gen->execute_data->prev_execute_data = NULL;
  264. s = phpdbg_compile_stackframe(EG(current_execute_data));
  265. phpdbg_notice("Switched to generator with handle #%d: %.*s", gen->std.handle, (int) ZSTR_LEN(s), ZSTR_VAL(s));
  266. zend_string_release(s);
  267. phpdbg_print_cur_frame_info();
  268. }