symfony_debug.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*
  2. * This file is part of the Symfony package.
  3. *
  4. * (c) Fabien Potencier <fabien@symfony.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. #ifdef HAVE_CONFIG_H
  10. #include "config.h"
  11. #endif
  12. #include "php.h"
  13. #ifdef ZTS
  14. #include "TSRM.h"
  15. #endif
  16. #include "php_ini.h"
  17. #include "ext/standard/info.h"
  18. #include "php_symfony_debug.h"
  19. #include "ext/standard/php_rand.h"
  20. #include "ext/standard/php_lcg.h"
  21. #include "ext/spl/php_spl.h"
  22. #include "Zend/zend_gc.h"
  23. #include "Zend/zend_builtin_functions.h"
  24. #include "Zend/zend_extensions.h" /* for ZEND_EXTENSION_API_NO */
  25. #include "ext/standard/php_array.h"
  26. #include "Zend/zend_interfaces.h"
  27. #include "SAPI.h"
  28. #define IS_PHP_53 ZEND_EXTENSION_API_NO == 220090626
  29. ZEND_DECLARE_MODULE_GLOBALS(symfony_debug)
  30. ZEND_BEGIN_ARG_INFO_EX(symfony_zval_arginfo, 0, 0, 2)
  31. ZEND_ARG_INFO(0, key)
  32. ZEND_ARG_ARRAY_INFO(0, array, 0)
  33. ZEND_ARG_INFO(0, options)
  34. ZEND_END_ARG_INFO()
  35. const zend_function_entry symfony_debug_functions[] = {
  36. PHP_FE(symfony_zval_info, symfony_zval_arginfo)
  37. PHP_FE(symfony_debug_backtrace, NULL)
  38. PHP_FE_END
  39. };
  40. PHP_FUNCTION(symfony_debug_backtrace)
  41. {
  42. if (zend_parse_parameters_none() == FAILURE) {
  43. return;
  44. }
  45. #if IS_PHP_53
  46. zend_fetch_debug_backtrace(return_value, 1, 0 TSRMLS_CC);
  47. #else
  48. zend_fetch_debug_backtrace(return_value, 1, 0, 0 TSRMLS_CC);
  49. #endif
  50. if (!SYMFONY_DEBUG_G(debug_bt)) {
  51. return;
  52. }
  53. php_array_merge(Z_ARRVAL_P(return_value), Z_ARRVAL_P(SYMFONY_DEBUG_G(debug_bt)), 0 TSRMLS_CC);
  54. }
  55. PHP_FUNCTION(symfony_zval_info)
  56. {
  57. zval *key = NULL, *arg = NULL;
  58. zval **data = NULL;
  59. HashTable *array = NULL;
  60. long options = 0;
  61. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zh|l", &key, &array, &options) == FAILURE) {
  62. return;
  63. }
  64. switch (Z_TYPE_P(key)) {
  65. case IS_STRING:
  66. if (zend_symtable_find(array, Z_STRVAL_P(key), Z_STRLEN_P(key) + 1, (void **)&data) == FAILURE) {
  67. return;
  68. }
  69. break;
  70. case IS_LONG:
  71. if (zend_hash_index_find(array, Z_LVAL_P(key), (void **)&data)) {
  72. return;
  73. }
  74. break;
  75. }
  76. arg = *data;
  77. array_init(return_value);
  78. add_assoc_string(return_value, "type", (char *)_symfony_debug_zval_type(arg), 1);
  79. add_assoc_stringl(return_value, "zval_hash", _symfony_debug_memory_address_hash((void *)arg TSRMLS_CC), 16, 0);
  80. add_assoc_long(return_value, "zval_refcount", Z_REFCOUNT_P(arg));
  81. add_assoc_bool(return_value, "zval_isref", (zend_bool)Z_ISREF_P(arg));
  82. if (Z_TYPE_P(arg) == IS_OBJECT) {
  83. char hash[33] = {0};
  84. php_spl_object_hash(arg, (char *)hash TSRMLS_CC);
  85. add_assoc_stringl(return_value, "object_class", (char *)Z_OBJCE_P(arg)->name, Z_OBJCE_P(arg)->name_length, 1);
  86. add_assoc_long(return_value, "object_refcount", EG(objects_store).object_buckets[Z_OBJ_HANDLE_P(arg)].bucket.obj.refcount);
  87. add_assoc_string(return_value, "object_hash", hash, 1);
  88. add_assoc_long(return_value, "object_handle", Z_OBJ_HANDLE_P(arg));
  89. } else if (Z_TYPE_P(arg) == IS_ARRAY) {
  90. add_assoc_long(return_value, "array_count", zend_hash_num_elements(Z_ARRVAL_P(arg)));
  91. } else if(Z_TYPE_P(arg) == IS_RESOURCE) {
  92. add_assoc_long(return_value, "resource_handle", Z_LVAL_P(arg));
  93. add_assoc_string(return_value, "resource_type", (char *)_symfony_debug_get_resource_type(Z_LVAL_P(arg) TSRMLS_CC), 1);
  94. add_assoc_long(return_value, "resource_refcount", _symfony_debug_get_resource_refcount(Z_LVAL_P(arg) TSRMLS_CC));
  95. } else if (Z_TYPE_P(arg) == IS_STRING) {
  96. add_assoc_long(return_value, "strlen", Z_STRLEN_P(arg));
  97. }
  98. }
  99. void symfony_debug_error_cb(int type, const char *error_filename, const uint error_lineno, const char *format, va_list args)
  100. {
  101. TSRMLS_FETCH();
  102. zval *retval;
  103. switch (type) {
  104. case E_ERROR:
  105. case E_PARSE:
  106. case E_CORE_ERROR:
  107. case E_CORE_WARNING:
  108. case E_COMPILE_ERROR:
  109. case E_COMPILE_WARNING:
  110. ALLOC_INIT_ZVAL(retval);
  111. #if IS_PHP_53
  112. zend_fetch_debug_backtrace(retval, 1, 0 TSRMLS_CC);
  113. #else
  114. zend_fetch_debug_backtrace(retval, 1, 0, 0 TSRMLS_CC);
  115. #endif
  116. SYMFONY_DEBUG_G(debug_bt) = retval;
  117. }
  118. SYMFONY_DEBUG_G(old_error_cb)(type, error_filename, error_lineno, format, args);
  119. }
  120. static const char* _symfony_debug_get_resource_type(long rsid TSRMLS_DC)
  121. {
  122. const char *res_type;
  123. res_type = zend_rsrc_list_get_rsrc_type(rsid TSRMLS_CC);
  124. if (!res_type) {
  125. return "Unknown";
  126. }
  127. return res_type;
  128. }
  129. static int _symfony_debug_get_resource_refcount(long rsid TSRMLS_DC)
  130. {
  131. zend_rsrc_list_entry *le;
  132. if (zend_hash_index_find(&EG(regular_list), rsid, (void **) &le)==SUCCESS) {
  133. return le->refcount;
  134. }
  135. return 0;
  136. }
  137. static char *_symfony_debug_memory_address_hash(void *address TSRMLS_DC)
  138. {
  139. char *result = NULL;
  140. intptr_t address_rand;
  141. if (!SYMFONY_DEBUG_G(req_rand_init)) {
  142. if (!BG(mt_rand_is_seeded)) {
  143. php_mt_srand(GENERATE_SEED() TSRMLS_CC);
  144. }
  145. SYMFONY_DEBUG_G(req_rand_init) = (intptr_t)php_mt_rand(TSRMLS_C);
  146. }
  147. address_rand = (intptr_t)address ^ SYMFONY_DEBUG_G(req_rand_init);
  148. spprintf(&result, 17, "%016zx", address_rand);
  149. return result;
  150. }
  151. static const char *_symfony_debug_zval_type(zval *zv)
  152. {
  153. switch (Z_TYPE_P(zv)) {
  154. case IS_NULL:
  155. return "NULL";
  156. break;
  157. case IS_BOOL:
  158. return "boolean";
  159. break;
  160. case IS_LONG:
  161. return "integer";
  162. break;
  163. case IS_DOUBLE:
  164. return "double";
  165. break;
  166. case IS_STRING:
  167. return "string";
  168. break;
  169. case IS_ARRAY:
  170. return "array";
  171. break;
  172. case IS_OBJECT:
  173. return "object";
  174. case IS_RESOURCE:
  175. return "resource";
  176. default:
  177. return "unknown type";
  178. }
  179. }
  180. zend_module_entry symfony_debug_module_entry = {
  181. STANDARD_MODULE_HEADER,
  182. "symfony_debug",
  183. symfony_debug_functions,
  184. PHP_MINIT(symfony_debug),
  185. PHP_MSHUTDOWN(symfony_debug),
  186. PHP_RINIT(symfony_debug),
  187. PHP_RSHUTDOWN(symfony_debug),
  188. PHP_MINFO(symfony_debug),
  189. PHP_SYMFONY_DEBUG_VERSION,
  190. PHP_MODULE_GLOBALS(symfony_debug),
  191. PHP_GINIT(symfony_debug),
  192. PHP_GSHUTDOWN(symfony_debug),
  193. NULL,
  194. STANDARD_MODULE_PROPERTIES_EX
  195. };
  196. #ifdef COMPILE_DL_SYMFONY_DEBUG
  197. ZEND_GET_MODULE(symfony_debug)
  198. #endif
  199. PHP_GINIT_FUNCTION(symfony_debug)
  200. {
  201. memset(symfony_debug_globals, 0 , sizeof(*symfony_debug_globals));
  202. }
  203. PHP_GSHUTDOWN_FUNCTION(symfony_debug)
  204. {
  205. }
  206. PHP_MINIT_FUNCTION(symfony_debug)
  207. {
  208. SYMFONY_DEBUG_G(old_error_cb) = zend_error_cb;
  209. zend_error_cb = symfony_debug_error_cb;
  210. return SUCCESS;
  211. }
  212. PHP_MSHUTDOWN_FUNCTION(symfony_debug)
  213. {
  214. zend_error_cb = SYMFONY_DEBUG_G(old_error_cb);
  215. return SUCCESS;
  216. }
  217. PHP_RINIT_FUNCTION(symfony_debug)
  218. {
  219. return SUCCESS;
  220. }
  221. PHP_RSHUTDOWN_FUNCTION(symfony_debug)
  222. {
  223. return SUCCESS;
  224. }
  225. PHP_MINFO_FUNCTION(symfony_debug)
  226. {
  227. php_info_print_table_start();
  228. php_info_print_table_header(2, "Symfony Debug support", "enabled");
  229. php_info_print_table_header(2, "Symfony Debug version", PHP_SYMFONY_DEBUG_VERSION);
  230. php_info_print_table_end();
  231. }