Skip to content

Commit 1a249bd

Browse files
committed
Shorten opline dump lines and show literals
1 parent 1c3b99c commit 1a249bd

File tree

5 files changed

+92
-35
lines changed

5 files changed

+92
-35
lines changed

sapi/phpdbg/phpdbg.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -763,6 +763,7 @@ const opt_struct OPTIONS[] = { /* {{{ */
763763
#endif
764764
{'x', 0, "xml output"},
765765
{'p', 2, "show opcodes"},
766+
{'o', 0, "display opline addresses"},
766767
{'h', 0, "help"},
767768
{'V', 0, "version"},
768769
{'-', 0, NULL}
@@ -1192,7 +1193,7 @@ int main(int argc, char **argv) /* {{{ */
11921193
if (sscanf(php_optarg, "%d", &listen) != 1) {
11931194
listen = 8000;
11941195
}
1195-
break;
1196+
break;
11961197

11971198
case 'a': { /* set bind address */
11981199
free(address);
@@ -1213,6 +1214,10 @@ int main(int argc, char **argv) /* {{{ */
12131214
settings = (void *) 0x1;
12141215
} break;
12151216

1217+
case 'o':
1218+
flags |= PHPDBG_PRINT_OPLINE_ADDR;
1219+
break;
1220+
12161221
case 'h': {
12171222
sapi_startup(phpdbg);
12181223
phpdbg->startup(phpdbg);

sapi/phpdbg/phpdbg.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,10 +187,11 @@ int phpdbg_do_parse(phpdbg_param_t *stack, char *input);
187187
#define PHPDBG_WRITE_XML (1ULL<<31)
188188

189189
#define PHPDBG_SHOW_REFCOUNTS (1ULL<<32)
190+
#define PHPDBG_PRINT_OPLINE_ADDR (1ULL<<33)
190191

191-
#define PHPDBG_IN_SIGNAL_HANDLER (1ULL<<33)
192+
#define PHPDBG_IN_SIGNAL_HANDLER (1ULL<<34)
192193

193-
#define PHPDBG_DISCARD_OUTPUT (1ULL<<34)
194+
#define PHPDBG_DISCARD_OUTPUT (1ULL<<35)
194195

195196
#define PHPDBG_SEEK_MASK (PHPDBG_IN_UNTIL | PHPDBG_IN_FINISH | PHPDBG_IN_LEAVE)
196197
#define PHPDBG_BP_RESOLVE_MASK (PHPDBG_HAS_FUNCTION_OPLINE_BP | PHPDBG_HAS_METHOD_OPLINE_BP | PHPDBG_HAS_FILE_OPLINE_BP)

sapi/phpdbg/phpdbg_help.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,7 @@ phpdbg_help_text_t phpdbg_help_text[] = {
390390
" **-a** **-a**192.168.0.3 Setup remote console bind address" CR
391391
" **-x** Enable xml output (instead of normal text output)" CR
392392
" **-p** **-p**, **-p=func**, **-p* ** Output opcodes and quit" CR
393+
" **-o** Display opline addresses in opline dumps" CR
393394
" **-h** Print the help overview" CR
394395
" **-V** Print version number" CR
395396
" **--** **--** arg1 arg2 Use to delimit phpdbg arguments and php $argv; append any $argv "

sapi/phpdbg/phpdbg_opcode.c

Lines changed: 67 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "zend_compile.h"
2424
#include "phpdbg_opcode.h"
2525
#include "phpdbg_utils.h"
26+
#include "ext/standard/php_string.h"
2627

2728
ZEND_EXTERN_MODULE_GLOBALS(phpdbg);
2829

@@ -63,9 +64,59 @@ static inline char *phpdbg_decode_op(zend_op_array *ops, znode_op *op, uint32_t
6364
asprintf(&decode, "@" ZEND_ULONG_FMT, id);
6465
} break;
6566

66-
case IS_CONST:
67-
asprintf(&decode, "C%u", phpdbg_decode_literal(ops, RT_CONSTANT(ops, *op)));
68-
break;
67+
case IS_CONST: {
68+
zval *literal = RT_CONSTANT(ops, *op);
69+
switch (Z_TYPE_P(literal)) {
70+
case IS_UNDEF:
71+
decode = zend_strndup("", 0);
72+
break;
73+
case IS_NULL:
74+
decode = zend_strndup(ZEND_STRL("null"));
75+
break;
76+
case IS_FALSE:
77+
decode = zend_strndup(ZEND_STRL("false"));
78+
break;
79+
case IS_TRUE:
80+
decode = zend_strndup(ZEND_STRL("true"));
81+
break;
82+
case IS_LONG:
83+
asprintf(&decode, "%lld", Z_LVAL_P(literal));
84+
break;
85+
case IS_DOUBLE:
86+
asprintf(&decode, "%.*G", 14, Z_DVAL_P(literal));
87+
break;
88+
case IS_STRING: {
89+
int i;
90+
zend_string *str = php_addcslashes(Z_STR_P(literal), 0, "\\\"", 2);
91+
for (i = 0; i < str->len; i++) {
92+
if (str->val[i] < 32) {
93+
str->val[i] = ' ';
94+
}
95+
}
96+
asprintf(&decode, "\"%.*s\"%c", str->len <= 18 ? (int) str->len : 17, str->val, str->len <= 18 ? 0 : '+');
97+
zend_string_release(str);
98+
} break;
99+
case IS_RESOURCE:
100+
asprintf(&decode, "Rsrc #%d", Z_RES_HANDLE_P(literal));
101+
break;
102+
case IS_ARRAY:
103+
asprintf(&decode, "array(%d)", zend_hash_num_elements(Z_ARR_P(literal)));
104+
break;
105+
case IS_OBJECT: {
106+
zend_string *str = Z_OBJCE_P(literal)->name;
107+
asprintf(&decode, "%.*s%c", str->len <= 18 ? (int) str->len : 18, str->val, str->len <= 18 ? 0 : '+');
108+
} break;
109+
case IS_CONSTANT:
110+
decode = zend_strndup(ZEND_STRL("<constant>"));
111+
break;
112+
case IS_CONSTANT_AST:
113+
decode = zend_strndup(ZEND_STRL("<ast>"));
114+
break;
115+
default:
116+
asprintf(&decode, "unknown type: %d", Z_TYPE_P(literal));
117+
break;
118+
}
119+
} break;
69120

70121
case IS_UNUSED:
71122
asprintf(&decode, "<unused>");
@@ -80,47 +131,39 @@ char *phpdbg_decode_opline(zend_op_array *ops, zend_op *op, HashTable *vars) /*{
80131

81132
switch (op->opcode) {
82133
case ZEND_JMP:
83-
#ifdef ZEND_GOTO
84134
case ZEND_GOTO:
85-
#endif
86-
#ifdef ZEND_FAST_CALL
87135
case ZEND_FAST_CALL:
88-
#endif
89-
asprintf(&decode[1], "J%ld", OP_JMP_ADDR(op, op->op1) - ops->opcodes);
136+
asprintf(&decode[1], "J%ld", OP_JMP_ADDR(op, op->op1) - ops->opcodes);
90137
goto format;
91138

92139
case ZEND_JMPZNZ:
93-
decode[1] = phpdbg_decode_op(ops, &op->op1, op->op1_type, vars);
94-
asprintf(&decode[2], "J%u or J%" PRIu32, op->op2.opline_num, op->extended_value);
140+
decode[1] = phpdbg_decode_op(ops, &op->op1, op->op1_type, vars);
141+
asprintf(&decode[2], "J%u or J%" PRIu32, op->op2.opline_num, op->extended_value);
95142
goto result;
96143

97144
case ZEND_JMPZ:
98145
case ZEND_JMPNZ:
99146
case ZEND_JMPZ_EX:
100147
case ZEND_JMPNZ_EX:
101-
102-
#ifdef ZEND_JMP_SET
103148
case ZEND_JMP_SET:
104-
#endif
105149
decode[1] = phpdbg_decode_op(ops, &op->op1, op->op1_type, vars);
106150
asprintf(&decode[2], "J%ld", OP_JMP_ADDR(op, op->op2) - ops->opcodes);
107-
goto result;
151+
goto result;
108152

109153
case ZEND_RECV_INIT:
110154
goto result;
111155

112-
default: {
113-
decode[1] = phpdbg_decode_op(ops, &op->op1, op->op1_type, vars);
114-
decode[2] = phpdbg_decode_op(ops, &op->op2, op->op2_type, vars);
156+
default:
157+
decode[1] = phpdbg_decode_op(ops, &op->op1, op->op1_type, vars);
158+
decode[2] = phpdbg_decode_op(ops, &op->op2, op->op2_type, vars);
115159
result:
116-
decode[3] = phpdbg_decode_op(ops, &op->result, op->result_type, vars);
160+
decode[3] = phpdbg_decode_op(ops, &op->result, op->result_type, vars);
117161
format:
118-
asprintf(&decode[0],
119-
"%-20s %-20s %-20s",
120-
decode[1] ? decode[1] : "",
121-
decode[2] ? decode[2] : "",
122-
decode[3] ? decode[3] : "");
123-
}
162+
asprintf(&decode[0],
163+
"%-20s %-20s %-20s",
164+
decode[1] ? decode[1] : "",
165+
decode[2] ? decode[2] : "",
166+
decode[3] ? decode[3] : "");
124167
}
125168

126169
if (decode[1])

sapi/phpdbg/phpdbg_print.c

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,14 @@ static inline void phpdbg_print_function_helper(zend_function *method) /* {{{ */
6363
end = op_array->last-1;
6464

6565
if (method->common.scope) {
66-
phpdbg_writeln("printoplineinfo", "type=\"User\" startline=\"%d\" endline=\"%d\" method=\"%s::%s\" file=\"%s\"", "\tL%d-%d %s::%s() %s",
66+
phpdbg_writeln("printoplineinfo", "type=\"User\" startline=\"%d\" endline=\"%d\" method=\"%s::%s\" file=\"%s\"", "L%d-%d %s::%s() %s",
6767
op_array->line_start,
6868
op_array->line_end,
6969
method->common.scope->name->val,
7070
method->common.function_name->val,
7171
op_array->filename ? op_array->filename->val : "unknown");
7272
} else {
73-
phpdbg_writeln("printoplineinfo", "type=\"User\" startline=\"%d\" endline=\"%d\" function=\"%s\" file=\"%s\"", "\tL%d-%d %s() %s",
73+
phpdbg_writeln("printoplineinfo", "type=\"User\" startline=\"%d\" endline=\"%d\" function=\"%s\" file=\"%s\"", "L%d-%d %s() %s",
7474
method->common.function_name ? op_array->line_start : 0,
7575
method->common.function_name ? op_array->line_end : 0,
7676
method->common.function_name ? method->common.function_name->val : "{main}",
@@ -81,14 +81,21 @@ static inline void phpdbg_print_function_helper(zend_function *method) /* {{{ */
8181
do {
8282
char *decode = phpdbg_decode_opline(op_array, opline, &vars);
8383
if (decode != NULL) {
84-
phpdbg_writeln("print", "line=\"%u\" opline=\"%p\" opcode=\"%s\" op=\"%s\"", "\t\tL%u\t%p %-30s %s",
85-
opline->lineno,
86-
opline,
87-
phpdbg_decode_opcode(opline->opcode),
88-
decode);
84+
if (PHPDBG_G(flags) & PHPDBG_PRINT_OPLINE_ADDR) {
85+
phpdbg_writeln("print", "line=\"%u\" opline=\"%p\" opcode=\"%s\" op=\"%s\"", " L%-5u %p %-36s %s",
86+
opline->lineno,
87+
opline,
88+
phpdbg_decode_opcode(opline->opcode),
89+
decode);
90+
} else {
91+
phpdbg_writeln("print", "line=\"%u\" opcode=\"%s\" op=\"%s\"", " L%-5u %-36s %s",
92+
opline->lineno,
93+
phpdbg_decode_opcode(opline->opcode),
94+
decode);
95+
}
8996
free(decode);
9097
} else {
91-
phpdbg_error("print", "type=\"decodefailure\" opline=\"%16p\"", "\tFailed to decode opline %16p", opline);
98+
phpdbg_error("print", "type=\"decodefailure\" opline=\"%16p\"", "Failed to decode opline %16p", opline);
9299
}
93100
opline++;
94101
} while (opcode++ < end);

0 commit comments

Comments
 (0)