You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

181 lines
5.4 KiB

12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2014 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 "zend_vm_opcodes.h"
  22. #include "zend_compile.h"
  23. #include "phpdbg_opcode.h"
  24. #include "phpdbg_utils.h"
  25. ZEND_EXTERN_MODULE_GLOBALS(phpdbg);
  26. static inline uint32_t phpdbg_decode_literal(zend_op_array *ops, zval *literal) /* {{{ */
  27. {
  28. int iter = 0;
  29. while (iter < ops->last_literal) {
  30. if (literal == &ops->literals[iter]) {
  31. return iter;
  32. }
  33. iter++;
  34. }
  35. return 0;
  36. } /* }}} */
  37. static inline char *phpdbg_decode_op(zend_op_array *ops, znode_op *op, uint32_t type, HashTable *vars) /* {{{ */
  38. {
  39. char *decode = NULL;
  40. switch (type &~ EXT_TYPE_UNUSED) {
  41. case IS_CV:
  42. asprintf(&decode, "$%s", ops->vars[EX_VAR_TO_NUM(op->var)]->val);
  43. break;
  44. case IS_VAR:
  45. case IS_TMP_VAR: {
  46. zend_ulong id = 0, *pid = NULL;
  47. if (vars != NULL) {
  48. if ((pid = zend_hash_index_find_ptr(vars, (zend_ulong) ops->vars - op->var))) {
  49. id = *pid;
  50. } else {
  51. id = zend_hash_num_elements(vars);
  52. zend_hash_index_update_mem(vars, (zend_ulong) ops->vars - op->var, &id, sizeof(zend_ulong));
  53. }
  54. }
  55. asprintf(&decode, "@%llu", id);
  56. } break;
  57. case IS_CONST:
  58. asprintf(&decode, "C%u", phpdbg_decode_literal(ops, RT_CONSTANT(ops, *op)));
  59. break;
  60. case IS_UNUSED:
  61. asprintf(&decode, "<unused>");
  62. break;
  63. }
  64. return decode;
  65. } /* }}} */
  66. char *phpdbg_decode_opline(zend_op_array *ops, zend_op *op, HashTable *vars) /*{{{ */
  67. {
  68. char *decode[4] = {NULL, NULL, NULL, NULL};
  69. switch (op->opcode) {
  70. case ZEND_JMP:
  71. #ifdef ZEND_GOTO
  72. case ZEND_GOTO:
  73. #endif
  74. #ifdef ZEND_FAST_CALL
  75. case ZEND_FAST_CALL:
  76. #endif
  77. asprintf(&decode[1], "J%ld", OP_JMP_ADDR(op, op->op1) - ops->opcodes);
  78. goto format;
  79. case ZEND_JMPZNZ:
  80. decode[1] = phpdbg_decode_op(ops, &op->op1, op->op1_type, vars);
  81. asprintf(&decode[2], "J%u or J%llu", op->op2.opline_num, op->extended_value);
  82. goto result;
  83. case ZEND_JMPZ:
  84. case ZEND_JMPNZ:
  85. case ZEND_JMPZ_EX:
  86. case ZEND_JMPNZ_EX:
  87. #ifdef ZEND_JMP_SET
  88. case ZEND_JMP_SET:
  89. #endif
  90. decode[1] = phpdbg_decode_op(ops, &op->op1, op->op1_type, vars);
  91. asprintf(&decode[2], "J%ld", OP_JMP_ADDR(op, op->op2) - ops->opcodes);
  92. goto result;
  93. case ZEND_RECV_INIT:
  94. goto result;
  95. default: {
  96. decode[1] = phpdbg_decode_op(ops, &op->op1, op->op1_type, vars);
  97. decode[2] = phpdbg_decode_op(ops, &op->op2, op->op2_type, vars);
  98. result:
  99. decode[3] = phpdbg_decode_op(ops, &op->result, op->result_type, vars);
  100. format:
  101. asprintf(&decode[0],
  102. "%-20s %-20s %-20s",
  103. decode[1] ? decode[1] : "",
  104. decode[2] ? decode[2] : "",
  105. decode[3] ? decode[3] : "");
  106. }
  107. }
  108. if (decode[1])
  109. free(decode[1]);
  110. if (decode[2])
  111. free(decode[2]);
  112. if (decode[3])
  113. free(decode[3]);
  114. return decode[0];
  115. } /* }}} */
  116. void phpdbg_print_opline_ex(zend_execute_data *execute_data, HashTable *vars, zend_bool ignore_flags) /* {{{ */
  117. {
  118. /* force out a line while stepping so the user knows what is happening */
  119. if (ignore_flags ||
  120. (!(PHPDBG_G(flags) & PHPDBG_IS_QUIET) ||
  121. (PHPDBG_G(flags) & PHPDBG_IS_STEPPING) ||
  122. (PHPDBG_G(oplog)))) {
  123. zend_op *opline = (zend_op *) execute_data->opline;
  124. char *decode = phpdbg_decode_opline(&execute_data->func->op_array, opline, vars);
  125. if (ignore_flags || (!(PHPDBG_G(flags) & PHPDBG_IS_QUIET) || (PHPDBG_G(flags) & PHPDBG_IS_STEPPING))) {
  126. /* output line info */
  127. phpdbg_notice("opline", "line=\"%u\" opline=\"%p\" opcode=\"%s\" op=\"%s\" file=\"%s\"", "L%-5u %16p %-30s %s %s",
  128. opline->lineno,
  129. opline,
  130. phpdbg_decode_opcode(opline->opcode),
  131. decode,
  132. execute_data->func->op_array.filename ? execute_data->func->op_array.filename->val : "unknown");
  133. }
  134. if (!ignore_flags && PHPDBG_G(oplog)) {
  135. phpdbg_log_ex(fileno(PHPDBG_G(oplog)), "L%-5u %16p %-30s %s %s",
  136. opline->lineno,
  137. opline,
  138. phpdbg_decode_opcode(opline->opcode),
  139. decode,
  140. execute_data->func->op_array.filename ? execute_data->func->op_array.filename->val : "unknown");
  141. }
  142. if (decode) {
  143. free(decode);
  144. }
  145. }
  146. } /* }}} */
  147. void phpdbg_print_opline(zend_execute_data *execute_data, zend_bool ignore_flags) /* {{{ */
  148. {
  149. phpdbg_print_opline_ex(execute_data, NULL, ignore_flags);
  150. } /* }}} */
  151. const char *phpdbg_decode_opcode(zend_uchar opcode) /* {{{ */
  152. {
  153. const char *ret = zend_get_opcode_name(opcode);
  154. return ret?ret:"UNKNOWN";
  155. } /* }}} */