annotate vendor/symfony/debug/Resources/ext/symfony_debug.c @ 19:fa3358dc1485 tip

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