2 * This file is part of the Symfony package.
4 * (c) Fabien Potencier <fabien@symfony.com>
6 * For the full copyright and license information, please view the LICENSE
7 * file that was distributed with this source code.
19 #include "ext/standard/info.h"
20 #include "php_symfony_debug.h"
21 #include "ext/standard/php_rand.h"
22 #include "ext/standard/php_lcg.h"
23 #include "ext/spl/php_spl.h"
24 #include "Zend/zend_gc.h"
25 #include "Zend/zend_builtin_functions.h"
26 #include "Zend/zend_extensions.h" /* for ZEND_EXTENSION_API_NO */
27 #include "ext/standard/php_array.h"
28 #include "Zend/zend_interfaces.h"
31 #define IS_PHP_53 ZEND_EXTENSION_API_NO == 220090626
33 ZEND_DECLARE_MODULE_GLOBALS(symfony_debug)
35 ZEND_BEGIN_ARG_INFO_EX(symfony_zval_arginfo, 0, 0, 2)
37 ZEND_ARG_ARRAY_INFO(0, array, 0)
38 ZEND_ARG_INFO(0, options)
41 const zend_function_entry symfony_debug_functions[] = {
42 PHP_FE(symfony_zval_info, symfony_zval_arginfo)
43 PHP_FE(symfony_debug_backtrace, NULL)
47 PHP_FUNCTION(symfony_debug_backtrace)
49 if (zend_parse_parameters_none() == FAILURE) {
53 zend_fetch_debug_backtrace(return_value, 1, 0 TSRMLS_CC);
55 zend_fetch_debug_backtrace(return_value, 1, 0, 0 TSRMLS_CC);
58 if (!SYMFONY_DEBUG_G(debug_bt)) {
62 php_array_merge(Z_ARRVAL_P(return_value), Z_ARRVAL_P(SYMFONY_DEBUG_G(debug_bt)), 0 TSRMLS_CC);
65 PHP_FUNCTION(symfony_zval_info)
67 zval *key = NULL, *arg = NULL;
69 HashTable *array = NULL;
72 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zh|l", &key, &array, &options) == FAILURE) {
76 switch (Z_TYPE_P(key)) {
78 if (zend_symtable_find(array, Z_STRVAL_P(key), Z_STRLEN_P(key) + 1, (void **)&data) == FAILURE) {
83 if (zend_hash_index_find(array, Z_LVAL_P(key), (void **)&data)) {
91 array_init(return_value);
93 add_assoc_string(return_value, "type", (char *)_symfony_debug_zval_type(arg), 1);
94 add_assoc_stringl(return_value, "zval_hash", _symfony_debug_memory_address_hash((void *)arg TSRMLS_CC), 16, 0);
95 add_assoc_long(return_value, "zval_refcount", Z_REFCOUNT_P(arg));
96 add_assoc_bool(return_value, "zval_isref", (zend_bool)Z_ISREF_P(arg));
98 if (Z_TYPE_P(arg) == IS_OBJECT) {
101 php_spl_object_hash(arg, (char *)hash TSRMLS_CC);
102 add_assoc_stringl(return_value, "object_class", (char *)Z_OBJCE_P(arg)->name, Z_OBJCE_P(arg)->name_length, 1);
103 add_assoc_long(return_value, "object_refcount", EG(objects_store).object_buckets[Z_OBJ_HANDLE_P(arg)].bucket.obj.refcount);
104 add_assoc_string(return_value, "object_hash", hash, 1);
105 add_assoc_long(return_value, "object_handle", Z_OBJ_HANDLE_P(arg));
106 } else if (Z_TYPE_P(arg) == IS_ARRAY) {
107 add_assoc_long(return_value, "array_count", zend_hash_num_elements(Z_ARRVAL_P(arg)));
108 } else if(Z_TYPE_P(arg) == IS_RESOURCE) {
109 add_assoc_long(return_value, "resource_handle", Z_LVAL_P(arg));
110 add_assoc_string(return_value, "resource_type", (char *)_symfony_debug_get_resource_type(Z_LVAL_P(arg) TSRMLS_CC), 1);
111 add_assoc_long(return_value, "resource_refcount", _symfony_debug_get_resource_refcount(Z_LVAL_P(arg) TSRMLS_CC));
112 } else if (Z_TYPE_P(arg) == IS_STRING) {
113 add_assoc_long(return_value, "strlen", Z_STRLEN_P(arg));
117 void symfony_debug_error_cb(int type, const char *error_filename, const uint error_lineno, const char *format, va_list args)
127 case E_COMPILE_ERROR:
128 case E_COMPILE_WARNING:
129 ALLOC_INIT_ZVAL(retval);
131 zend_fetch_debug_backtrace(retval, 1, 0 TSRMLS_CC);
133 zend_fetch_debug_backtrace(retval, 1, 0, 0 TSRMLS_CC);
135 SYMFONY_DEBUG_G(debug_bt) = retval;
138 SYMFONY_DEBUG_G(old_error_cb)(type, error_filename, error_lineno, format, args);
141 static const char* _symfony_debug_get_resource_type(long rsid TSRMLS_DC)
143 const char *res_type;
144 res_type = zend_rsrc_list_get_rsrc_type(rsid TSRMLS_CC);
153 static int _symfony_debug_get_resource_refcount(long rsid TSRMLS_DC)
155 zend_rsrc_list_entry *le;
157 if (zend_hash_index_find(&EG(regular_list), rsid, (void **) &le)==SUCCESS) {
164 static char *_symfony_debug_memory_address_hash(void *address TSRMLS_DC)
167 intptr_t address_rand;
169 if (!SYMFONY_DEBUG_G(req_rand_init)) {
170 if (!BG(mt_rand_is_seeded)) {
171 php_mt_srand(GENERATE_SEED() TSRMLS_CC);
173 SYMFONY_DEBUG_G(req_rand_init) = (intptr_t)php_mt_rand(TSRMLS_C);
176 address_rand = (intptr_t)address ^ SYMFONY_DEBUG_G(req_rand_init);
178 spprintf(&result, 17, "%016zx", address_rand);
183 static const char *_symfony_debug_zval_type(zval *zv)
185 switch (Z_TYPE_P(zv)) {
217 return "unknown type";
221 zend_module_entry symfony_debug_module_entry = {
222 STANDARD_MODULE_HEADER,
224 symfony_debug_functions,
225 PHP_MINIT(symfony_debug),
226 PHP_MSHUTDOWN(symfony_debug),
227 PHP_RINIT(symfony_debug),
228 PHP_RSHUTDOWN(symfony_debug),
229 PHP_MINFO(symfony_debug),
230 PHP_SYMFONY_DEBUG_VERSION,
231 PHP_MODULE_GLOBALS(symfony_debug),
232 PHP_GINIT(symfony_debug),
233 PHP_GSHUTDOWN(symfony_debug),
235 STANDARD_MODULE_PROPERTIES_EX
238 #ifdef COMPILE_DL_SYMFONY_DEBUG
239 ZEND_GET_MODULE(symfony_debug)
242 PHP_GINIT_FUNCTION(symfony_debug)
244 memset(symfony_debug_globals, 0 , sizeof(*symfony_debug_globals));
247 PHP_GSHUTDOWN_FUNCTION(symfony_debug)
252 PHP_MINIT_FUNCTION(symfony_debug)
254 SYMFONY_DEBUG_G(old_error_cb) = zend_error_cb;
255 zend_error_cb = symfony_debug_error_cb;
260 PHP_MSHUTDOWN_FUNCTION(symfony_debug)
262 zend_error_cb = SYMFONY_DEBUG_G(old_error_cb);
267 PHP_RINIT_FUNCTION(symfony_debug)
272 PHP_RSHUTDOWN_FUNCTION(symfony_debug)
277 PHP_MINFO_FUNCTION(symfony_debug)
279 php_info_print_table_start();
280 php_info_print_table_header(2, "Symfony Debug support", "enabled");
281 php_info_print_table_header(2, "Symfony Debug version", PHP_SYMFONY_DEBUG_VERSION);
282 php_info_print_table_end();