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 #ifdef ZTS
16 #include "TSRM.h"
17 #endif
18 #include "php_ini.h"
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"
29 #include "SAPI.h"
30
31 #define IS_PHP_53 ZEND_EXTENSION_API_NO == 220090626
32
33 ZEND_DECLARE_MODULE_GLOBALS(symfony_debug)
34
35 ZEND_BEGIN_ARG_INFO_EX(symfony_zval_arginfo, 0, 0, 2)
36 ZEND_ARG_INFO(0, key)
37 ZEND_ARG_ARRAY_INFO(0, array, 0)
38 ZEND_ARG_INFO(0, options)
39 ZEND_END_ARG_INFO()
40
41 const zend_function_entry symfony_debug_functions[] = {
42 PHP_FE(symfony_zval_info, symfony_zval_arginfo)
43 PHP_FE(symfony_debug_backtrace, NULL)
44 PHP_FE_END
45 };
46
PHP_FUNCTION(symfony_debug_backtrace)47 PHP_FUNCTION(symfony_debug_backtrace)
48 {
49 if (zend_parse_parameters_none() == FAILURE) {
50 return;
51 }
52 #if IS_PHP_53
53 zend_fetch_debug_backtrace(return_value, 1, 0 TSRMLS_CC);
54 #else
55 zend_fetch_debug_backtrace(return_value, 1, 0, 0 TSRMLS_CC);
56 #endif
57
58 if (!SYMFONY_DEBUG_G(debug_bt)) {
59 return;
60 }
61
62 php_array_merge(Z_ARRVAL_P(return_value), Z_ARRVAL_P(SYMFONY_DEBUG_G(debug_bt)), 0 TSRMLS_CC);
63 }
64
PHP_FUNCTION(symfony_zval_info)65 PHP_FUNCTION(symfony_zval_info)
66 {
67 zval *key = NULL, *arg = NULL;
68 zval **data = NULL;
69 HashTable *array = NULL;
70 long options = 0;
71
72 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zh|l", &key, &array, &options) == FAILURE) {
73 return;
74 }
75
76 switch (Z_TYPE_P(key)) {
77 case IS_STRING:
78 if (zend_symtable_find(array, Z_STRVAL_P(key), Z_STRLEN_P(key) + 1, (void **)&data) == FAILURE) {
79 return;
80 }
81 break;
82 case IS_LONG:
83 if (zend_hash_index_find(array, Z_LVAL_P(key), (void **)&data)) {
84 return;
85 }
86 break;
87 }
88
89 arg = *data;
90
91 array_init(return_value);
92
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));
97
98 if (Z_TYPE_P(arg) == IS_OBJECT) {
99 char hash[33] = {0};
100
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));
114 }
115 }
116
symfony_debug_error_cb(int type,const char * error_filename,const uint error_lineno,const char * format,va_list args)117 void symfony_debug_error_cb(int type, const char *error_filename, const uint error_lineno, const char *format, va_list args)
118 {
119 TSRMLS_FETCH();
120 zval *retval;
121
122 switch (type) {
123 case E_ERROR:
124 case E_PARSE:
125 case E_CORE_ERROR:
126 case E_CORE_WARNING:
127 case E_COMPILE_ERROR:
128 case E_COMPILE_WARNING:
129 ALLOC_INIT_ZVAL(retval);
130 #if IS_PHP_53
131 zend_fetch_debug_backtrace(retval, 1, 0 TSRMLS_CC);
132 #else
133 zend_fetch_debug_backtrace(retval, 1, 0, 0 TSRMLS_CC);
134 #endif
135 SYMFONY_DEBUG_G(debug_bt) = retval;
136 }
137
138 SYMFONY_DEBUG_G(old_error_cb)(type, error_filename, error_lineno, format, args);
139 }
140
_symfony_debug_get_resource_type(long rsid TSRMLS_DC)141 static const char* _symfony_debug_get_resource_type(long rsid TSRMLS_DC)
142 {
143 const char *res_type;
144 res_type = zend_rsrc_list_get_rsrc_type(rsid TSRMLS_CC);
145
146 if (!res_type) {
147 return "Unknown";
148 }
149
150 return res_type;
151 }
152
_symfony_debug_get_resource_refcount(long rsid TSRMLS_DC)153 static int _symfony_debug_get_resource_refcount(long rsid TSRMLS_DC)
154 {
155 zend_rsrc_list_entry *le;
156
157 if (zend_hash_index_find(&EG(regular_list), rsid, (void **) &le)==SUCCESS) {
158 return le->refcount;
159 }
160
161 return 0;
162 }
163
_symfony_debug_memory_address_hash(void * address TSRMLS_DC)164 static char *_symfony_debug_memory_address_hash(void *address TSRMLS_DC)
165 {
166 char *result = NULL;
167 intptr_t address_rand;
168
169 if (!SYMFONY_DEBUG_G(req_rand_init)) {
170 if (!BG(mt_rand_is_seeded)) {
171 php_mt_srand(GENERATE_SEED() TSRMLS_CC);
172 }
173 SYMFONY_DEBUG_G(req_rand_init) = (intptr_t)php_mt_rand(TSRMLS_C);
174 }
175
176 address_rand = (intptr_t)address ^ SYMFONY_DEBUG_G(req_rand_init);
177
178 spprintf(&result, 17, "%016zx", address_rand);
179
180 return result;
181 }
182
_symfony_debug_zval_type(zval * zv)183 static const char *_symfony_debug_zval_type(zval *zv)
184 {
185 switch (Z_TYPE_P(zv)) {
186 case IS_NULL:
187 return "NULL";
188 break;
189
190 case IS_BOOL:
191 return "boolean";
192 break;
193
194 case IS_LONG:
195 return "integer";
196 break;
197
198 case IS_DOUBLE:
199 return "double";
200 break;
201
202 case IS_STRING:
203 return "string";
204 break;
205
206 case IS_ARRAY:
207 return "array";
208 break;
209
210 case IS_OBJECT:
211 return "object";
212
213 case IS_RESOURCE:
214 return "resource";
215
216 default:
217 return "unknown type";
218 }
219 }
220
221 zend_module_entry symfony_debug_module_entry = {
222 STANDARD_MODULE_HEADER,
223 "symfony_debug",
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),
234 NULL,
235 STANDARD_MODULE_PROPERTIES_EX
236 };
237
238 #ifdef COMPILE_DL_SYMFONY_DEBUG
239 ZEND_GET_MODULE(symfony_debug)
240 #endif
241
PHP_GINIT_FUNCTION(symfony_debug)242 PHP_GINIT_FUNCTION(symfony_debug)
243 {
244 memset(symfony_debug_globals, 0 , sizeof(*symfony_debug_globals));
245 }
246
PHP_GSHUTDOWN_FUNCTION(symfony_debug)247 PHP_GSHUTDOWN_FUNCTION(symfony_debug)
248 {
249
250 }
251
PHP_MINIT_FUNCTION(symfony_debug)252 PHP_MINIT_FUNCTION(symfony_debug)
253 {
254 SYMFONY_DEBUG_G(old_error_cb) = zend_error_cb;
255 zend_error_cb = symfony_debug_error_cb;
256
257 return SUCCESS;
258 }
259
PHP_MSHUTDOWN_FUNCTION(symfony_debug)260 PHP_MSHUTDOWN_FUNCTION(symfony_debug)
261 {
262 zend_error_cb = SYMFONY_DEBUG_G(old_error_cb);
263
264 return SUCCESS;
265 }
266
PHP_RINIT_FUNCTION(symfony_debug)267 PHP_RINIT_FUNCTION(symfony_debug)
268 {
269 return SUCCESS;
270 }
271
PHP_RSHUTDOWN_FUNCTION(symfony_debug)272 PHP_RSHUTDOWN_FUNCTION(symfony_debug)
273 {
274 return SUCCESS;
275 }
276
PHP_MINFO_FUNCTION(symfony_debug)277 PHP_MINFO_FUNCTION(symfony_debug)
278 {
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();
283 }
284