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