phpdbg_frame.c 10 KB

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