Skip to content

Commit cd8e511

Browse files
committed
replace custom opcode dumper with O+ dump
1 parent 1f42777 commit cd8e511

17 files changed

+100
-365
lines changed

Zend/Optimizer/zend_dump.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,10 @@ ZEND_API void zend_dump_op(const zend_op_array *op_array, const zend_basic_block
410410
uint32_t flags = zend_get_opcode_flags(opline->opcode);
411411
uint32_t n = 0;
412412

413+
if (dump_flags & ZEND_DUMP_LINE_NUMBERS) {
414+
fprintf(stderr, "L%04u ", opline->lineno);
415+
}
416+
413417
if (!ssa_op || ssa_op->result_use < 0) {
414418
if (opline->result_type & (IS_CV|IS_VAR|IS_TMP_VAR)) {
415419
if (ssa_op && ssa_op->result_def >= 0) {
@@ -721,6 +725,10 @@ static void zend_dump_op_line(const zend_op_array *op_array, const zend_basic_bl
721725
int len = 0;
722726
const zend_ssa *ssa = NULL;
723727
zend_ssa_op *ssa_op = NULL;
728+
729+
if (dump_flags & ZEND_DUMP_LINE_NUMBERS) {
730+
fprintf(stderr, "L%04u ", opline->lineno);
731+
}
724732

725733
len = fprintf(stderr, "%04u", (uint32_t)(opline - op_array->opcodes));
726734
fprintf(stderr, "%*c", 5-len, ' ');
@@ -732,7 +740,7 @@ static void zend_dump_op_line(const zend_op_array *op_array, const zend_basic_bl
732740
}
733741
}
734742

735-
zend_dump_op(op_array, b, opline, dump_flags, ssa, ssa_op);
743+
zend_dump_op(op_array, b, opline, dump_flags &~ ZEND_DUMP_LINE_NUMBERS, ssa, ssa_op);
736744
fprintf(stderr, "\n");
737745
}
738746

Zend/Optimizer/zend_dump.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#define ZEND_DUMP_CFG (1<<2)
2828
#define ZEND_DUMP_SSA (1<<3)
2929
#define ZEND_DUMP_LIVE_RANGES (1<<4)
30+
#define ZEND_DUMP_LINE_NUMBERS (1<<5)
3031

3132
BEGIN_EXTERN_C()
3233

sapi/phpdbg/phpdbg.1.in

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,6 @@ Override .phpgdbinit location (implies -I)
4949
.B \-I
5050
Ignore .phpdbginit (default init file)
5151
.TP
52-
.B \-O \fIfile\fR
53-
Set oplog output to
54-
.IR file
55-
.TP
5652
.B \-r
5753
Jump straight to run
5854
.TP

sapi/phpdbg/phpdbg.c

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,6 @@ static inline void php_phpdbg_globals_ctor(zend_phpdbg_globals *pg) /* {{{ */
122122
pg->in_execution = 0;
123123
pg->bp_count = 0;
124124
pg->flags = PHPDBG_DEFAULT_FLAGS;
125-
pg->oplog = NULL;
126125
memset(pg->io, 0, sizeof(pg->io));
127126
pg->frame.num = 0;
128127
pg->sapi_name_ptr = NULL;
@@ -199,11 +198,6 @@ static PHP_MSHUTDOWN_FUNCTION(phpdbg) /* {{{ */
199198
PHPDBG_G(exec) = NULL;
200199
}
201200

202-
if (PHPDBG_G(oplog)) {
203-
fclose(PHPDBG_G(oplog));
204-
PHPDBG_G(oplog) = NULL;
205-
}
206-
207201
if (PHPDBG_G(oplog_list)) {
208202
phpdbg_oplog_list *cur = PHPDBG_G(oplog_list);
209203
do {
@@ -1134,8 +1128,6 @@ int main(int argc, char **argv) /* {{{ */
11341128
char *init_file;
11351129
size_t init_file_len;
11361130
bool init_file_default;
1137-
char *oplog_file;
1138-
size_t oplog_file_len;
11391131
uint64_t flags;
11401132
char *php_optarg;
11411133
int php_optind, opt, show_banner = 1;
@@ -1186,8 +1178,6 @@ int main(int argc, char **argv) /* {{{ */
11861178
init_file = NULL;
11871179
init_file_len = 0;
11881180
init_file_default = 1;
1189-
oplog_file = NULL;
1190-
oplog_file_len = 0;
11911181
flags = PHPDBG_DEFAULT_FLAGS;
11921182
is_exit = 0;
11931183
php_optarg = NULL;
@@ -1281,13 +1271,6 @@ int main(int argc, char **argv) /* {{{ */
12811271
}
12821272
} break;
12831273

1284-
case 'O': { /* set oplog output */
1285-
oplog_file_len = strlen(php_optarg);
1286-
if (oplog_file_len) {
1287-
oplog_file = strdup(php_optarg);
1288-
}
1289-
} break;
1290-
12911274
case 'v': /* set quietness off */
12921275
flags &= ~PHPDBG_IS_QUIET;
12931276
break;
@@ -1442,9 +1425,6 @@ int main(int argc, char **argv) /* {{{ */
14421425
if (exec) {
14431426
free(exec);
14441427
}
1445-
if (oplog_file) {
1446-
free(oplog_file);
1447-
}
14481428
if (init_file) {
14491429
free(init_file);
14501430
}
@@ -1534,15 +1514,6 @@ int main(int argc, char **argv) /* {{{ */
15341514
php_stream_stdio_ops.write = phpdbg_stdiop_write;
15351515
#endif
15361516

1537-
if (oplog_file) { /* open oplog */
1538-
PHPDBG_G(oplog) = fopen(oplog_file, "w+");
1539-
if (!PHPDBG_G(oplog)) {
1540-
phpdbg_error("Failed to open oplog %s", oplog_file);
1541-
}
1542-
free(oplog_file);
1543-
oplog_file = NULL;
1544-
}
1545-
15461517
{
15471518
zval *zv = zend_hash_str_find(php_stream_get_url_stream_wrappers_hash(), ZEND_STRL("php"));
15481519
php_stream_wrapper *tmp_wrapper = Z_PTR_P(zv);
@@ -1757,7 +1728,6 @@ int main(int argc, char **argv) /* {{{ */
17571728
settings->exec = zend_strndup(PHPDBG_G(exec), PHPDBG_G(exec_len));
17581729
settings->exec_len = PHPDBG_G(exec_len);
17591730
}
1760-
settings->oplog = PHPDBG_G(oplog);
17611731
settings->prompt[0] = PHPDBG_G(prompt)[0];
17621732
settings->prompt[1] = PHPDBG_G(prompt)[1];
17631733
memcpy(ZEND_VOIDP(settings->colors), PHPDBG_G(colors), sizeof(settings->colors));

sapi/phpdbg/phpdbg.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
#include "zend_ini_scanner.h"
4545
#include "zend_stream.h"
4646
#include "zend_signal.h"
47+
#include "Optimizer/zend_dump.h"
4748
#if !defined(_WIN32) && !defined(ZEND_SIGNALS)
4849
# include <signal.h>
4950
#elif defined(PHP_WIN32)
@@ -107,8 +108,6 @@
107108
#define memcpy(...) memcpy_tmp(__VA_ARGS__)
108109
#endif
109110

110-
#if !defined(PHPDBG_WEBDATA_TRANSFER_H) && !defined(PHPDBG_WEBHELPER_H)
111-
112111
#ifdef ZTS
113112
# define PHPDBG_G(v) ZEND_TSRMG(phpdbg_globals_id, zend_phpdbg_globals *, v)
114113
#else
@@ -266,7 +265,6 @@ ZEND_BEGIN_MODULE_GLOBALS(phpdbg)
266265
zend_op_array *(*compile_string)(zend_string *source_string, const char *filename);
267266
HashTable file_sources;
268267

269-
FILE *oplog; /* opline log */
270268
zend_arena *oplog_arena; /* arena for storing oplog */
271269
phpdbg_oplog_list *oplog_list; /* list of oplog starts */
272270
phpdbg_oplog_entry *oplog_cur; /* current oplog entry */
@@ -304,6 +302,4 @@ ZEND_BEGIN_MODULE_GLOBALS(phpdbg)
304302
zend_ulong lines; /* max number of lines to display */
305303
ZEND_END_MODULE_GLOBALS(phpdbg) /* }}} */
306304

307-
#endif
308-
309305
#endif /* PHPDBG_H */

sapi/phpdbg/phpdbg_help.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,6 @@ phpdbg_help_text_t phpdbg_help_text[] = {
391391
" **-b** Disable colour" CR
392392
" **-i** **-i**my.init Set .phpdbginit file" CR
393393
" **-I** Ignore default .phpdbginit" CR
394-
" **-O** **-O**my.oplog Sets oplog output file" CR
395394
" **-r** Run execution context" CR
396395
" **-rr** Run execution context and quit after execution (not respecting breakpoints)" CR
397396
" **-e** Generate extended information for debugger/profiler" CR
@@ -872,7 +871,6 @@ phpdbg_help_text_t phpdbg_help_text[] = {
872871
" **prompt** **p** set the prompt" CR
873872
" **color** **c** set color <element> <color>" CR
874873
" **colors** **C** set colors [<on|off>]" CR
875-
" **oplog** **O** set oplog [output]" CR
876874
" **break** **b** set break **id** <on|off>" CR
877875
" **breaks** **B** set breaks [<on|off>]" CR
878876
" **quiet** **q** set quiet [<on|off>]" CR

sapi/phpdbg/phpdbg_opcode.c

Lines changed: 6 additions & 145 deletions
Original file line numberDiff line numberDiff line change
@@ -25,157 +25,18 @@
2525

2626
ZEND_EXTERN_MODULE_GLOBALS(phpdbg)
2727

28-
static inline const char *phpdbg_decode_opcode(zend_uchar opcode) /* {{{ */
29-
{
30-
const char *ret = zend_get_opcode_name(opcode);
31-
if (ret) {
32-
return ret + 5; /* Skip ZEND_ prefix */
33-
}
34-
return "UNKNOWN";
35-
} /* }}} */
36-
37-
static inline char *phpdbg_decode_op(
38-
zend_op_array *ops, const zend_op *opline, const znode_op *op, uint32_t type) /* {{{ */
39-
{
40-
char *decode = NULL;
41-
42-
switch (type) {
43-
case IS_CV: {
44-
zend_string *var = ops->vars[EX_VAR_TO_NUM(op->var)];
45-
spprintf(&decode, 0, "$%.*s%c",
46-
ZSTR_LEN(var) <= 19 ? (int) ZSTR_LEN(var) : 18,
47-
ZSTR_VAL(var), ZSTR_LEN(var) <= 19 ? 0 : '+');
48-
} break;
49-
50-
case IS_VAR:
51-
spprintf(&decode, 0, "@%u", EX_VAR_TO_NUM(op->var) - ops->last_var);
52-
break;
53-
case IS_TMP_VAR:
54-
spprintf(&decode, 0, "~%u", EX_VAR_TO_NUM(op->var) - ops->last_var);
55-
break;
56-
case IS_CONST: {
57-
zval *literal = RT_CONSTANT(opline, *op);
58-
decode = phpdbg_short_zval_print(literal, 20);
59-
} break;
60-
}
61-
return decode;
62-
} /* }}} */
63-
64-
char *phpdbg_decode_input_op(
65-
zend_op_array *ops, const zend_op *opline, znode_op op, zend_uchar op_type,
66-
uint32_t flags) {
67-
char *result = NULL;
68-
if (op_type != IS_UNUSED) {
69-
result = phpdbg_decode_op(ops, opline, &op, op_type);
70-
} else if (ZEND_VM_OP_JMP_ADDR == (flags & ZEND_VM_OP_MASK)) {
71-
spprintf(&result, 0, "J%td", OP_JMP_ADDR(opline, op) - ops->opcodes);
72-
} else if (ZEND_VM_OP_NUM == (flags & ZEND_VM_OP_MASK)) {
73-
spprintf(&result, 0, "%" PRIu32, op.num);
74-
} else if (ZEND_VM_OP_TRY_CATCH == (flags & ZEND_VM_OP_MASK)) {
75-
if (op.num != (uint32_t)-1) {
76-
spprintf(&result, 0, "try-catch(%" PRIu32 ")", op.num);
77-
}
78-
} else if (ZEND_VM_OP_THIS == (flags & ZEND_VM_OP_MASK)) {
79-
result = estrdup("THIS");
80-
} else if (ZEND_VM_OP_NEXT == (flags & ZEND_VM_OP_MASK)) {
81-
result = estrdup("NEXT");
82-
} else if (ZEND_VM_OP_CLASS_FETCH == (flags & ZEND_VM_OP_MASK)) {
83-
//zend_dump_class_fetch_type(op.num);
84-
} else if (ZEND_VM_OP_CONSTRUCTOR == (flags & ZEND_VM_OP_MASK)) {
85-
result = estrdup("CONSTRUCTOR");
86-
}
87-
return result;
88-
}
89-
90-
char *phpdbg_decode_opline(zend_op_array *ops, zend_op *opline) /*{{{ */
91-
{
92-
const char *opcode_name = phpdbg_decode_opcode(opline->opcode);
93-
uint32_t flags = zend_get_opcode_flags(opline->opcode);
94-
char *result, *decode[4] = {NULL, NULL, NULL, NULL};
95-
96-
/* OpcodeName */
97-
if (opline->extended_value) {
98-
spprintf(&decode[0], 0, "%s<%" PRIi32 ">", opcode_name, opline->extended_value);
99-
}
100-
101-
/* OP1 */
102-
decode[1] = phpdbg_decode_input_op(
103-
ops, opline, opline->op1, opline->op1_type, ZEND_VM_OP1_FLAGS(flags));
104-
105-
/* OP2 */
106-
decode[2] = phpdbg_decode_input_op(
107-
ops, opline, opline->op2, opline->op2_type, ZEND_VM_OP2_FLAGS(flags));
108-
109-
/* RESULT */
110-
switch (opline->opcode) {
111-
case ZEND_CATCH:
112-
if (opline->extended_value & ZEND_LAST_CATCH) {
113-
if (decode[2]) {
114-
efree(decode[2]);
115-
decode[2] = NULL;
116-
}
117-
}
118-
decode[3] = phpdbg_decode_op(ops, opline, &opline->result, opline->result_type);
119-
break;
120-
default:
121-
decode[3] = phpdbg_decode_op(ops, opline, &opline->result, opline->result_type);
122-
break;
123-
}
124-
125-
spprintf(&result, 0,
126-
"%-23s %-20s %-20s %-20s",
127-
decode[0] ? decode[0] : opcode_name,
128-
decode[1] ? decode[1] : "",
129-
decode[2] ? decode[2] : "",
130-
decode[3] ? decode[3] : "");
131-
132-
if (decode[0])
133-
efree(decode[0]);
134-
if (decode[1])
135-
efree(decode[1]);
136-
if (decode[2])
137-
efree(decode[2]);
138-
if (decode[3])
139-
efree(decode[3]);
140-
141-
return result;
142-
} /* }}} */
143-
14428
void phpdbg_print_opline_ex(zend_execute_data *execute_data, bool ignore_flags) /* {{{ */
14529
{
146-
/* force out a line while stepping so the user knows what is happening */
147-
if (ignore_flags ||
148-
(!(PHPDBG_G(flags) & PHPDBG_IS_QUIET) ||
149-
(PHPDBG_G(flags) & PHPDBG_IS_STEPPING) ||
150-
(PHPDBG_G(oplog)))) {
151-
152-
zend_op *opline = (zend_op *) execute_data->opline;
153-
char *decode = phpdbg_decode_opline(&execute_data->func->op_array, opline);
154-
155-
if (ignore_flags || (!(PHPDBG_G(flags) & PHPDBG_IS_QUIET) || (PHPDBG_G(flags) & PHPDBG_IS_STEPPING))) {
156-
/* output line info */
157-
phpdbg_notice("L%-5u %16p %s %s",
158-
opline->lineno,
159-
opline,
160-
decode,
161-
execute_data->func->op_array.filename ? ZSTR_VAL(execute_data->func->op_array.filename) : "unknown");
162-
}
163-
164-
if (!ignore_flags && PHPDBG_G(oplog)) {
165-
phpdbg_log_internal(fileno(PHPDBG_G(oplog)), "L%-5u %16p %s %s\n",
166-
opline->lineno,
167-
opline,
168-
decode,
169-
execute_data->func->op_array.filename ? ZSTR_VAL(execute_data->func->op_array.filename) : "unknown");
170-
}
171-
172-
efree(decode);
30+
if (ignore_flags || (!(PHPDBG_G(flags) & PHPDBG_IS_QUIET) && (PHPDBG_G(flags) & PHPDBG_IS_STEPPING))) {
31+
fprintf(stderr, "[");
32+
zend_dump_op(&EX(func)->op_array, NULL, EX(opline), ZEND_DUMP_LINE_NUMBERS, NULL, NULL);
33+
fprintf(stderr, "]\n");
17334
}
17435

17536
if (PHPDBG_G(oplog_list)) {
17637
phpdbg_oplog_entry *cur = zend_arena_alloc(&PHPDBG_G(oplog_arena), sizeof(phpdbg_oplog_entry));
177-
zend_op_array *op_array = &execute_data->func->op_array;
178-
cur->op = (zend_op *) execute_data->opline;
38+
zend_op_array *op_array = &EX(func)->op_array;
39+
cur->op = (zend_op *) EX(opline);
17940
cur->opcodes = op_array->opcodes;
18041
cur->filename = op_array->filename;
18142
cur->scope = op_array->scope;

sapi/phpdbg/phpdbg_opcode.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
#include "zend_types.h"
2323

24-
char *phpdbg_decode_opline(zend_op_array *ops, zend_op *op);
2524
void phpdbg_print_opline(zend_execute_data *execute_data, bool ignore_flags);
2625
void phpdbg_print_opline_ex(zend_execute_data *execute_data, bool ignore_flags);
2726

0 commit comments

Comments
 (0)