Skip to content

Commit af255c6

Browse files
committed
Make op1/op2 decoding in phpdbg more complete
1 parent b1e4883 commit af255c6

File tree

4 files changed

+49
-67
lines changed

4 files changed

+49
-67
lines changed

sapi/phpdbg/phpdbg_opcode.c

Lines changed: 46 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ static inline const char *phpdbg_decode_opcode(zend_uchar opcode) /* {{{ */
3636
return "UNKNOWN";
3737
} /* }}} */
3838

39-
static inline char *phpdbg_decode_op(zend_op_array *ops, znode_op *op, uint32_t type) /* {{{ */
39+
static inline char *phpdbg_decode_op(
40+
zend_op_array *ops, const znode_op *op, uint32_t type) /* {{{ */
4041
{
4142
char *decode = NULL;
4243

@@ -62,91 +63,72 @@ static inline char *phpdbg_decode_op(zend_op_array *ops, znode_op *op, uint32_t
6263
return decode;
6364
} /* }}} */
6465

65-
char *phpdbg_decode_opline(zend_op_array *ops, zend_op *op) /*{{{ */
66+
char *phpdbg_decode_input_op(
67+
zend_op_array *ops, const zend_op *opline, znode_op op, zend_uchar op_type,
68+
uint32_t flags) {
69+
char *result = NULL;
70+
if (op_type != IS_UNUSED) {
71+
result = phpdbg_decode_op(ops, &op, op_type);
72+
} else if (ZEND_VM_OP_JMP_ADDR == (flags & ZEND_VM_OP_MASK)) {
73+
spprintf(&result, 0, "J%td", OP_JMP_ADDR(opline, op) - ops->opcodes);
74+
} else if (ZEND_VM_OP_NUM == (flags & ZEND_VM_OP_MASK)) {
75+
spprintf(&result, 0, "%" PRIu32, op.num);
76+
} else if (ZEND_VM_OP_TRY_CATCH == (flags & ZEND_VM_OP_MASK)) {
77+
if (opline->opcode != ZEND_FAST_RET || opline->extended_value) {
78+
spprintf(&result, 0, "try-catch(%" PRIu32 ")", op.num);
79+
}
80+
} else if (ZEND_VM_OP_LIVE_RANGE == (flags & ZEND_VM_OP_MASK)) {
81+
if (opline->extended_value & ZEND_FREE_ON_RETURN) {
82+
spprintf(&result, 0, "live-range(%" PRIu32 ")", op.num);
83+
}
84+
} else if (ZEND_VM_OP_THIS == (flags & ZEND_VM_OP_MASK)) {
85+
result = estrdup("THIS");
86+
} else if (ZEND_VM_OP_NEXT == (flags & ZEND_VM_OP_MASK)) {
87+
result = estrdup("NEXT");
88+
} else if (ZEND_VM_OP_CLASS_FETCH == (flags & ZEND_VM_OP_MASK)) {
89+
//zend_dump_class_fetch_type(op.num);
90+
} else if (ZEND_VM_OP_CONSTRUCTOR == (flags & ZEND_VM_OP_MASK)) {
91+
result = estrdup("CONSTRUCTOR");
92+
}
93+
return result;
94+
}
95+
96+
char *phpdbg_decode_opline(zend_op_array *ops, zend_op *opline) /*{{{ */
6697
{
67-
const char *opcode_name = phpdbg_decode_opcode(op->opcode);
98+
const char *opcode_name = phpdbg_decode_opcode(opline->opcode);
99+
uint32_t flags = zend_get_opcode_flags(opline->opcode);
68100
char *result, *decode[4] = {NULL, NULL, NULL, NULL};
69101

70102
/* EX */
71-
switch (op->opcode) {
103+
switch (opline->opcode) {
72104
case ZEND_FAST_CALL:
73-
if (op->extended_value == ZEND_FAST_CALL_FROM_FINALLY) {
105+
if (opline->extended_value == ZEND_FAST_CALL_FROM_FINALLY) {
74106
decode[0] = estrdup("FAST_CALL<FROM_FINALLY>");
75107
}
76108
break;
77109
case ZEND_FAST_RET:
78-
if (op->extended_value != 0) {
110+
if (opline->extended_value != 0) {
79111
spprintf(&decode[0], 0, "FAST_RET<%s>",
80-
op->extended_value == ZEND_FAST_RET_TO_CATCH ? "TO_CATCH" : "TO_FINALLY");
112+
opline->extended_value == ZEND_FAST_RET_TO_CATCH ? "TO_CATCH" : "TO_FINALLY");
81113
}
82114
break;
83115
}
84116

85117
/* OP1 */
86-
switch (op->opcode) {
87-
case ZEND_JMP:
88-
case ZEND_FAST_CALL:
89-
spprintf(&decode[1], 0, "J%td", OP_JMP_ADDR(op, op->op1) - ops->opcodes);
90-
break;
91-
92-
case ZEND_INIT_FCALL:
93-
case ZEND_RECV:
94-
case ZEND_RECV_INIT:
95-
case ZEND_RECV_VARIADIC:
96-
spprintf(&decode[1], 0, "%" PRIu32, op->op1.num);
97-
break;
98-
99-
default:
100-
decode[1] = phpdbg_decode_op(ops, &op->op1, op->op1_type);
101-
break;
102-
}
118+
decode[1] = phpdbg_decode_input_op(
119+
ops, opline, opline->op1, opline->op1_type, ZEND_VM_OP1_FLAGS(flags));
103120

104121
/* OP2 */
105-
switch (op->opcode) {
106-
case ZEND_JMPZNZ:
107-
spprintf(&decode[2], 0, "J%td or J%td",
108-
OP_JMP_ADDR(op, op->op2) - ops->opcodes,
109-
ZEND_OFFSET_TO_OPLINE(op, op->extended_value) - ops->opcodes);
110-
break;
111-
112-
case ZEND_JMPZ:
113-
case ZEND_JMPNZ:
114-
case ZEND_JMPZ_EX:
115-
case ZEND_JMPNZ_EX:
116-
case ZEND_JMP_SET:
117-
case ZEND_ASSERT_CHECK:
118-
spprintf(&decode[2], 0, "J%td", OP_JMP_ADDR(op, op->op2) - ops->opcodes);
119-
break;
120-
121-
case ZEND_FAST_CALL:
122-
case ZEND_FAST_RET:
123-
if (op->extended_value != 0) {
124-
spprintf(&decode[2], 0, "%" PRIu32, op->op2.num);
125-
}
126-
break;
127-
128-
case ZEND_SEND_VAL:
129-
case ZEND_SEND_VAL_EX:
130-
case ZEND_SEND_VAR:
131-
case ZEND_SEND_VAR_NO_REF:
132-
case ZEND_SEND_REF:
133-
case ZEND_SEND_VAR_EX:
134-
case ZEND_SEND_USER:
135-
spprintf(&decode[2], 0, "%" PRIu32, op->op2.num);
136-
break;
137-
138-
default:
139-
decode[2] = phpdbg_decode_op(ops, &op->op2, op->op2_type);
140-
break;
141-
}
122+
decode[2] = phpdbg_decode_input_op(
123+
ops, opline, opline->op2, opline->op2_type, ZEND_VM_OP2_FLAGS(flags));
142124

143125
/* RESULT */
144-
switch (op->opcode) {
126+
switch (opline->opcode) {
145127
case ZEND_CATCH:
146-
spprintf(&decode[3], 0, "%" PRIu32, op->result.num);
128+
spprintf(&decode[3], 0, "%" PRIu32, opline->result.num);
147129
break;
148130
default:
149-
decode[3] = phpdbg_decode_op(ops, &op->result, op->result_type);
131+
decode[3] = phpdbg_decode_op(ops, &opline->result, opline->result_type);
150132
break;
151133
}
152134

sapi/phpdbg/tests/exceptions_003.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ prompt> [L7 %s ECHO "ok "
2525
00008: }
2626
00009: } catch (Error $e) {
2727
prompt> ok
28-
[L7 %s FAST_RET<TO_CATCH> ~%d 0 %s]
28+
[L7 %s FAST_RET<TO_CATCH> ~%d try-catch(0) %s]
2929
[L9 %s CATCH "Error" $e 1 %s]
3030
>00005: x();
3131
00006: } finally {

sapi/phpdbg/tests/print_001.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ prompt> [Context %s (11 ops)]
3434
L1-19 {main}() %s - %s + 11 ops
3535
L4 #0 NOP
3636
L14 #1 NOP
37-
L18 #2 NEW "Foo\\Bar" @1
37+
L18 #2 NEW "Foo\\Bar" J4 @1
3838
L18 #3 DO_FCALL
3939
L18 #4 INIT_METHOD_CALL @1 "Foo"
4040
L18 #5 SEND_VAL_EX "test" 1

sapi/phpdbg/tests/stepping_001.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ prompt> [L10 %s ECHO "ok"
3434
00011: } finally {
3535
00012: echo " ... ok";
3636
prompt> ok
37-
[L11 %s FAST_CALL J8 ~%d %s]
37+
[L11 %s FAST_CALL J8 try-catch(0) ~%d %s]
3838
>00011: } finally {
3939
00012: echo " ... ok";
4040
00013: }

0 commit comments

Comments
 (0)