forked from krakjoe/explain
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexplain.c
572 lines (468 loc) · 19.1 KB
/
explain.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
/*
+----------------------------------------------------------------------+
| PHP Version 7 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2015 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: |
+----------------------------------------------------------------------+
*/
/* $Id$ */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "php_ini.h"
#include "php_main.h"
#include "ext/standard/info.h"
#include "php_explain.h"
typedef struct _explain_opcode_t {
const char *name;
size_t name_len;
zend_uchar opcode;
} explain_opcode_t;
#define EXPLAIN_FILE 0x00000001
#define EXPLAIN_STRING 0x00000010
#define EXPLAIN_OPLINE 0x00000011
#define EXPLAIN_OPCODE_NAME(c) \
{#c, sizeof(#c)-1, c}
#include "explain_opcodes.h"
#if ZEND_USE_ABS_JMP_ADDR
# define JMP_LINE(node, base_address) (int32_t)(((long)((node).jmp_addr) - (long)(base_address)) / sizeof(zend_op))
#else
# define JMP_LINE(node, opline) (int32_t)(((int32_t)((node).jmp_offset) / sizeof(zend_op)) + (opline))
#endif
ZEND_DECLARE_MODULE_GLOBALS(explain);
static inline void explain_opcode(long opcode, zval *return_value) { /* {{{ */
if (opcode >= sizeof(opcodes)) {
ZVAL_STRINGL(return_value, "unknown", strlen("unknown"));
}
explain_opcode_t decode = opcodes[opcode];
if (decode.opcode == opcode) {
ZVAL_STRINGL(return_value, decode.name, decode.name_len);
} else {
ZVAL_STRINGL(return_value, "unknown", strlen("unknown"));
}
} /* }}} */
/* True global resources - no need for thread safety here */
static int le_explain;
/* {{{ PHP_INI
*/
/* Remove comments and fill if you need to have entries in php.ini
PHP_INI_BEGIN()
STD_PHP_INI_ENTRY("explain.global_value", "42", PHP_INI_ALL, OnUpdateLong, global_value, zend_explain_globals, explain_globals)
STD_PHP_INI_ENTRY("explain.global_string", "foobar", PHP_INI_ALL, OnUpdateString, global_string, zend_explain_globals, explain_globals)
PHP_INI_END()
*/
/* }}} */
static int explain_variable(zend_ulong var, zend_llist *vars) { /* {{{ */
zend_llist_position position;
zend_ulong *cache = zend_llist_get_first_ex(vars, &position);
zend_ulong id = 0;
while (cache) {
if (*cache == var) {
return id;
}
cache = zend_llist_get_next_ex(vars, &position);
id++;
}
zend_llist_add_element(vars, &var);
return id;
} /* }}} */
static inline void explain_zend_op(zend_op_array *ops, znode_op *op, zend_ulong type, const char *name, size_t name_len, zend_llist *vars, zval *return_value_ptr) { /* {{{ */
if (!op || type == IS_UNUSED)
return;
switch (type) {
case IS_CV : {
add_assoc_str_ex(return_value_ptr, name, name_len, ops->vars[(op->var - sizeof(zend_execute_data)) / sizeof(zval)]);
break;
}
case IS_VAR:
case IS_TMP_VAR: {
/* convert this to a human friendly number */
add_assoc_long_ex(return_value_ptr, name, name_len, explain_variable((zend_ulong) ops->vars - op->var, vars));
break;
}
case IS_CONST : {
zval pzval = *RT_CONSTANT_EX(ops->literals, *op);
add_assoc_zval_ex(return_value_ptr, name, name_len, &pzval);
break;
}
}
} /* }}} */
static inline void explain_optype(uint32_t type, zval *return_value_ptr) { /* {{{ */
switch (type) {
case IS_CV:
ZVAL_STRINGL(return_value_ptr, "IS_CV", strlen("IS_CV"));
break;
case IS_TMP_VAR:
ZVAL_STRINGL(return_value_ptr, "IS_TMP_VAR", strlen("IS_TMP_VAR"));
break;
case IS_VAR:
ZVAL_STRINGL(return_value_ptr, "IS_VAR", strlen("IS_VAR"));
break;
case IS_CONST:
ZVAL_STRINGL(return_value_ptr, "IS_CONST", strlen("IS_CONST"));
break;
case IS_UNUSED:
ZVAL_STRINGL(return_value_ptr, "IS_UNUSED", strlen("IS_UNUSED"));
break;
/* special case for jmp's */
case EXPLAIN_OPLINE:
ZVAL_STRINGL(return_value_ptr, "IS_OPLINE", strlen("IS_OPLINE"));
break;
default: if (type & EXT_TYPE_UNUSED) {
switch (type &~ EXT_TYPE_UNUSED) {
case IS_CV:
ZVAL_STRINGL(return_value_ptr, "IS_CV|EXT_TYPE_UNUSED", strlen("IS_CV|EXT_TYPE_UNUSED"));
break;
case IS_TMP_VAR:
ZVAL_STRINGL(return_value_ptr, "IS_TMP_VAR|EXT_TYPE_UNUSED", strlen("IS_TMP_VAR|EXT_TYPE_UNUSED"));
break;
case IS_VAR:
ZVAL_STRINGL(return_value_ptr, "IS_VAR|EXT_TYPE_UNUSED", strlen("IS_VAR|EXT_TYPE_UNUSED"));
break;
case IS_CONST:
ZVAL_STRINGL(return_value_ptr, "IS_CONST|EXT_TYPE_UNUSED", strlen("IS_CONST|EXT_TYPE_UNUSED"));
break;
case IS_UNUSED:
ZVAL_STRINGL(return_value_ptr, "IS_UNUSED|EXT_TYPE_UNUSED", strlen("IS_UNUSED|EXT_TYPE_UNUSED"));
break;
}
} else {
ZVAL_STRINGL(return_value_ptr, "unknown", strlen("unknown"));
}
}
} /* }}} */
static inline void explain_op_array(zend_op_array *ops, zval *result) {
if (ops) {
zend_ulong next = 0;
zend_llist vars;
zend_llist_init(&vars, sizeof(size_t), NULL, 0);
array_init(result);
do {
zval zopline;
array_init(&zopline);
zend_op *opline = &ops->opcodes[next];
add_assoc_long_ex(&zopline, "opline", sizeof("opline") - 1, next);
add_assoc_long_ex(&zopline, "opcode", sizeof("opcode") - 1, opline->opcode);
switch (opline->opcode) {
case ZEND_JMP:
#ifdef ZEND_GOTO
case ZEND_GOTO:
#endif
#ifdef ZEND_FAST_CALL
case ZEND_FAST_CALL:
#endif
add_assoc_long_ex(&zopline, "op1_type", sizeof("op1_type") - 1, EXPLAIN_OPLINE);
#if ZEND_USE_ABS_JMP_ADDR
zend_op *base_address = &(opa->opcodes[0]);
add_assoc_long_ex(&zopline, "op1", sizeof("op1") - 1, JMP_LINE(opline->op1, base_address));
#else
add_assoc_long_ex(&zopline, "op1", sizeof("op1") - 1, JMP_LINE(opline->op1, next));
#endif
break;
case ZEND_JMPZNZ:
add_assoc_long_ex(&zopline, "op1_type", sizeof("op1_type") - 1, opline->op1_type);
explain_zend_op(ops, &opline->op1, opline->op1_type, "op1", sizeof("op1") - 1, &vars, &zopline);
/* TODO(krakjoe) needs opline->extended_value on true, opline_num on false */
add_assoc_long_ex(&zopline, "op2_type", sizeof("op2_type") - 1, EXPLAIN_OPLINE);
add_assoc_long_ex(&zopline, "op2", sizeof("op2") - 1, opline->op2.opline_num);
add_assoc_long_ex(&zopline, "result_type", sizeof("result_type") - 1, opline->result_type);
explain_zend_op(ops, &opline->result, opline->result_type, "result", sizeof("result") - 1, &vars, &zopline);
break;
case ZEND_JMPZ:
case ZEND_JMPNZ:
case ZEND_JMPZ_EX:
case ZEND_JMPNZ_EX:
#ifdef ZEND_JMP_SET
case ZEND_JMP_SET:
#endif
#ifdef ZEND_JMP_SET_VAR
case ZEND_JMP_SET_VAR:
#endif
add_assoc_long_ex(&zopline, "op1_type", sizeof("op1_type") - 1, opline->op1_type);
explain_zend_op(ops, &opline->op1, opline->op1_type, "op1", sizeof("op1") - 1, &vars, &zopline);
add_assoc_long_ex(&zopline, "op2_type", sizeof("op2_type") - 1, EXPLAIN_OPLINE);
#if ZEND_USE_ABS_JMP_ADDR
zend_op *base_address = &(opa->opcodes[0]);
add_assoc_long_ex(&zopline, "op2", sizeof("op2") - 1, JMP_LINE(opline->op2, base_address));
#else
add_assoc_long_ex(&zopline, "op2", sizeof("op2") - 1, JMP_LINE(opline->op2, next));
#endif
add_assoc_long_ex(&zopline, "result_type", sizeof("result_type") - 1, opline->result_type);
explain_zend_op(ops, &opline->result, opline->result_type, "result", sizeof("result") - 1, &vars, &zopline);
break;
case ZEND_RECV_INIT:
add_assoc_long_ex(&zopline, "result_type", sizeof("result_type") - 1, opline->result_type);
explain_zend_op(ops, &opline->result, opline->result_type, "result", sizeof("result") - 1, &vars, &zopline);
break;
default: {
add_assoc_long_ex(&zopline, "op1_type", sizeof("op1_type") - 1, opline->op1_type);
explain_zend_op(ops, &opline->op1, opline->op1_type, "op1", sizeof("op1") - 1, &vars, &zopline);
add_assoc_long_ex(&zopline, "op2_type", sizeof("op2_type") - 1, opline->op2_type);
explain_zend_op(ops, &opline->op2, opline->op2_type, "op2", sizeof("op2") - 1, &vars, &zopline);
add_assoc_long_ex(&zopline, "result_type", sizeof("result_type") - 1, opline->result_type);
explain_zend_op(ops, &opline->result, opline->result_type, "result", sizeof("result") - 1, &vars, &zopline);
}
}
if (opline->extended_value) {
add_assoc_long_ex(&zopline, "extended_value", sizeof("extended_value") - 1, opline->extended_value);
}
add_assoc_long_ex(&zopline, "lineno", sizeof("lineno") - 1, opline->lineno);
add_next_index_zval(result, &zopline);
} while (++next < ops->last);
zend_llist_destroy(&vars);
} else {
ZVAL_NULL(result);
}
}
static inline void explain_create_caches(HashTable *classes, HashTable *functions) { /* {{{ */
zend_function tf;
zend_class_entry *te;
zend_hash_init(classes, zend_hash_num_elements(classes), NULL, NULL, 0);
zend_hash_copy(classes, CG(class_table), NULL);
zend_hash_init(functions, zend_hash_num_elements(functions), NULL, NULL, 0);
zend_hash_copy(functions, CG(function_table), NULL);
} /* }}} */
static inline void explain_destroy_caches(HashTable *classes, HashTable *functions) { /* {{{ */
zend_hash_destroy(classes);
zend_hash_destroy(functions);
} /* }}} */
/* Every user-visible function in PHP should document itself in the source */
/* {{{ proto string confirm_explain_compiled(string arg)
Return a string to confirm that the module is compiled in */
PHP_FUNCTION(explain)
{
zval *code, *classes, *functions, res;
zend_ulong options = EXPLAIN_FILE;
HashTable caches[2] = {0, 0};
if (zend_parse_parameters(ZEND_NUM_ARGS(), "z|lzz", &code, &options, &classes, &functions) == FAILURE) {
return;
}
zend_file_handle fh;
zend_op_array *ops = NULL;
if (options & EXPLAIN_FILE) {
if (php_stream_open_for_zend_ex(Z_STRVAL_P(code), &fh, USE_PATH|STREAM_OPEN_FOR_INCLUDE) == SUCCESS) {
explain_create_caches(&caches[0], &caches[1]);
ops = zend_compile_file(&fh, ZEND_INCLUDE);
zend_destroy_file_handle(&fh);
} else {
zend_error(E_WARNING, "file %s couldn't be opened", Z_STRVAL_P(code));
RETURN_FALSE;
}
} else if (options & EXPLAIN_STRING) {
explain_create_caches(&caches[0], &caches[1]);
ops = zend_compile_string(code, "explained");
if (!ops) {
explain_destroy_caches(&caches[0], &caches[1]);
}
} else {
zend_error(E_WARNING, "invalid options passed to explain (%lu), please see documentation", options);
RETURN_FALSE;
}
if (!ops) {
explain_destroy_caches(&caches[0], &caches[1]);
zend_error(E_WARNING, "explain was unable to compile code");
RETURN_FALSE;
}
explain_op_array(ops, &res);
if (ZVAL_IS_NULL(&res)) {
explain_destroy_caches(&caches[0], &caches[1]);
zend_error(E_WARNING, "explain was unable to compile code");
RETURN_FALSE;
}
if (classes) {
zend_ulong pce_num_key;
zend_string *pce_name;
zend_class_entry *pce;
if (Z_TYPE_P(classes) != IS_ARRAY) {
array_init(classes);
}
ZEND_HASH_FOREACH_KEY_VAL(CG(class_table), pce_num_key, pce_name, pce)
{
if (pce->type == ZEND_USER_CLASS && !zend_hash_exists(&caches[0], pce_name)) {
zval zce;
zend_function *pfe;
array_init(&zce);
ZEND_HASH_FOREACH_PTR(&pce->function_table, pfe) {
if (pfe->common.type == ZEND_USER_FUNCTION) {
zval zfe;
array_init(&zfe);
explain_op_array(&pfe->op_array, &zfe);
add_assoc_zval_ex(&zce, pfe->common.function_name, strlen(pfe->common.function_name), &zfe);
}
} ZEND_HASH_FOREACH_END();
add_assoc_zval_ex(classes, pce->name, strlen(pce->name), &zce);
}
}ZEND_HASH_FOREACH_END();
}
if (functions) {
zend_ulong pfe_num_key;
zend_function *pfe;
zend_string *fe_name;
if (Z_TYPE_P(functions) != IS_ARRAY) {
array_init(functions);
}
ZEND_HASH_FOREACH_KEY_VAL(CG(function_table), pfe_num_key, fe_name, pfe) {
if (pfe->common.type == ZEND_USER_FUNCTION && !zend_hash_exists(&caches[1], fe_name)) {
zval zfe;
array_init(&zfe);
explain_op_array(&pfe->op_array, &zfe);
add_assoc_zval_ex(functions, fe_name, strlen(fe_name), &zfe);
}
} ZEND_HASH_FOREACH_END();
}
explain_destroy_caches(&caches[0], &caches[1]);
RETURN_ZVAL(&res, 0, 0);
}
/* }}} */
/* {{{ proto string explain_opcode(integer opcode)
get the friendly name for an opcode */
PHP_FUNCTION(explain_opcode) {
long opcode;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &opcode) == FAILURE) {
return;
}
explain_opcode(opcode, return_value);
} /* }}} */
/* {{{ proto string explain_optype(integer optype)
get the friendly name for an optype */
PHP_FUNCTION(explain_optype) {
long optype;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &optype) == FAILURE) {
return;
}
explain_optype((zend_uchar)optype, return_value);
} /* }}} */
/* {{{ */
static inline void php_explain_globals_ctor(zend_explain_globals *eg) {} /* }}} */
static inline void php_explain_destroy_ops(zend_op_array **ops) { /* {{{ */
if ((*ops)) {
destroy_op_array(*ops);
efree(*ops);
}
} /* }}} */
/* {{{ PHP_MINIT_FUNCTION
*/
PHP_MINIT_FUNCTION(explain)
{
ZEND_INIT_MODULE_GLOBALS(explain, php_explain_globals_ctor, NULL);
REGISTER_LONG_CONSTANT("EXPLAIN_STRING", EXPLAIN_STRING, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("EXPLAIN_FILE", EXPLAIN_FILE, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("EXPLAIN_OPLINE", EXPLAIN_OPLINE, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("EXPLAIN_IS_UNUSED", IS_UNUSED, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("EXPLAIN_IS_VAR", IS_VAR, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("EXPLAIN_IS_TMP_VAR", IS_TMP_VAR, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("EXPLAIN_IS_CV", IS_CV, CONST_CS | CONST_PERSISTENT);
#ifdef EXT_TYPE_UNUSED
REGISTER_LONG_CONSTANT("EXPLAIN_EXT_TYPE_UNUSED", EXT_TYPE_UNUSED, CONST_CS | CONST_PERSISTENT);
#endif
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MSHUTDOWN_FUNCTION
*/
PHP_MSHUTDOWN_FUNCTION(explain)
{
/* uncomment this line if you have INI entries
UNREGISTER_INI_ENTRIES();
*/
return SUCCESS;
}
/* }}} */
/* Remove if there's nothing to do at request start */
/* {{{ PHP_RINIT_FUNCTION
*/
PHP_RINIT_FUNCTION(explain)
{
#if defined(COMPILE_DL_EXPLAIN) && defined(ZTS)
ZEND_TSRMLS_CACHE_UPDATE();
#endif
zend_hash_init(&EX_G(explained), 8, NULL, (dtor_func_t) php_explain_destroy_ops, 0);
zend_hash_init(&EX_G(zval_cache), 8, NULL, (dtor_func_t) ZVAL_PTR_DTOR, 0);
return SUCCESS;
}
/* }}} */
/* Remove if there's nothing to do at request end */
/* {{{ PHP_RSHUTDOWN_FUNCTION
*/
PHP_RSHUTDOWN_FUNCTION(explain)
{
zend_hash_destroy(&EX_G(explained));
zend_hash_destroy(&EX_G(zval_cache));
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MINFO_FUNCTION
*/
PHP_MINFO_FUNCTION(explain)
{
php_info_print_table_start();
php_info_print_table_header(2, "explain support", "enabled");
php_info_print_table_end();
/* Remove comments if you have entries in php.ini
DISPLAY_INI_ENTRIES();
*/
}
/* }}} */
ZEND_BEGIN_ARG_INFO_EX(arginfo_explain, 0, 0, 1)
ZEND_ARG_INFO(0, code)
ZEND_ARG_INFO(0, options)
ZEND_ARG_INFO(1, classes)
ZEND_ARG_INFO(1, functions)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_explain_opcode, 0, 0, 1)
ZEND_ARG_INFO(0, opcode)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_explain_optype, 0, 0, 1)
ZEND_ARG_INFO(0, optype)
ZEND_END_ARG_INFO()
/* {{{ explain_functions[]
*
* Every user visible function must have an entry in explain_functions[].
*/
const zend_function_entry explain_functions[] = {
PHP_FE(explain, NULL)
PHP_FE(explain_opcode, arginfo_explain_opcode)
PHP_FE(explain_optype, arginfo_explain_optype)
PHP_FE_END /* Must be the last line in explain_functions[] */
};
/* }}} */
/* {{{ explain_module_entry
*/
zend_module_entry explain_module_entry = {
STANDARD_MODULE_HEADER,
"explain",
explain_functions,
PHP_MINIT(explain),
PHP_MSHUTDOWN(explain),
PHP_RINIT(explain), /* Replace with NULL if there's nothing to do at request start */
PHP_RSHUTDOWN(explain), /* Replace with NULL if there's nothing to do at request end */
PHP_MINFO(explain),
PHP_EXPLAIN_VERSION,
STANDARD_MODULE_PROPERTIES
};
/* }}} */
#ifdef COMPILE_DL_EXPLAIN
#ifdef ZTS
ZEND_TSRMLS_CACHE_DEFINE();
#endif
ZEND_GET_MODULE(explain)
#endif
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: noet sw=4 ts=4 fdm=marker
* vim<600: noet sw=4 ts=4
*/