From: Bob Weinand Date: Mon, 20 Apr 2015 10:22:55 +0000 (+0200) Subject: Shorten opline dump lines and show literals X-Git-Tag: PRE_PHP7_NSAPI_REMOVAL~210 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=1a249bd6745131cc68f72b2cfc9b8ca789b221ee;p=php Shorten opline dump lines and show literals --- diff --git a/sapi/phpdbg/phpdbg.c b/sapi/phpdbg/phpdbg.c index a1af1e0ea9..a93a062d2e 100644 --- a/sapi/phpdbg/phpdbg.c +++ b/sapi/phpdbg/phpdbg.c @@ -763,6 +763,7 @@ const opt_struct OPTIONS[] = { /* {{{ */ #endif {'x', 0, "xml output"}, {'p', 2, "show opcodes"}, + {'o', 0, "display opline addresses"}, {'h', 0, "help"}, {'V', 0, "version"}, {'-', 0, NULL} @@ -1192,7 +1193,7 @@ phpdbg_main: if (sscanf(php_optarg, "%d", &listen) != 1) { listen = 8000; } - break; + break; case 'a': { /* set bind address */ free(address); @@ -1213,6 +1214,10 @@ phpdbg_main: settings = (void *) 0x1; } break; + case 'o': + flags |= PHPDBG_PRINT_OPLINE_ADDR; + break; + case 'h': { sapi_startup(phpdbg); phpdbg->startup(phpdbg); diff --git a/sapi/phpdbg/phpdbg.h b/sapi/phpdbg/phpdbg.h index c636a7c791..c76e460495 100644 --- a/sapi/phpdbg/phpdbg.h +++ b/sapi/phpdbg/phpdbg.h @@ -187,10 +187,11 @@ int phpdbg_do_parse(phpdbg_param_t *stack, char *input); #define PHPDBG_WRITE_XML (1ULL<<31) #define PHPDBG_SHOW_REFCOUNTS (1ULL<<32) +#define PHPDBG_PRINT_OPLINE_ADDR (1ULL<<33) -#define PHPDBG_IN_SIGNAL_HANDLER (1ULL<<33) +#define PHPDBG_IN_SIGNAL_HANDLER (1ULL<<34) -#define PHPDBG_DISCARD_OUTPUT (1ULL<<34) +#define PHPDBG_DISCARD_OUTPUT (1ULL<<35) #define PHPDBG_SEEK_MASK (PHPDBG_IN_UNTIL | PHPDBG_IN_FINISH | PHPDBG_IN_LEAVE) #define PHPDBG_BP_RESOLVE_MASK (PHPDBG_HAS_FUNCTION_OPLINE_BP | PHPDBG_HAS_METHOD_OPLINE_BP | PHPDBG_HAS_FILE_OPLINE_BP) diff --git a/sapi/phpdbg/phpdbg_help.c b/sapi/phpdbg/phpdbg_help.c index 6b10551192..022a919f52 100644 --- a/sapi/phpdbg/phpdbg_help.c +++ b/sapi/phpdbg/phpdbg_help.c @@ -390,6 +390,7 @@ phpdbg_help_text_t phpdbg_help_text[] = { " **-a** **-a**192.168.0.3 Setup remote console bind address" CR " **-x** Enable xml output (instead of normal text output)" CR " **-p** **-p**, **-p=func**, **-p* ** Output opcodes and quit" CR +" **-o** Display opline addresses in opline dumps" CR " **-h** Print the help overview" CR " **-V** Print version number" CR " **--** **--** arg1 arg2 Use to delimit phpdbg arguments and php $argv; append any $argv " diff --git a/sapi/phpdbg/phpdbg_opcode.c b/sapi/phpdbg/phpdbg_opcode.c index 76f3cf3a4b..9622fd661b 100644 --- a/sapi/phpdbg/phpdbg_opcode.c +++ b/sapi/phpdbg/phpdbg_opcode.c @@ -23,6 +23,7 @@ #include "zend_compile.h" #include "phpdbg_opcode.h" #include "phpdbg_utils.h" +#include "ext/standard/php_string.h" ZEND_EXTERN_MODULE_GLOBALS(phpdbg); @@ -63,9 +64,59 @@ static inline char *phpdbg_decode_op(zend_op_array *ops, znode_op *op, uint32_t asprintf(&decode, "@" ZEND_ULONG_FMT, id); } break; - case IS_CONST: - asprintf(&decode, "C%u", phpdbg_decode_literal(ops, RT_CONSTANT(ops, *op))); - break; + case IS_CONST: { + zval *literal = RT_CONSTANT(ops, *op); + switch (Z_TYPE_P(literal)) { + case IS_UNDEF: + decode = zend_strndup("", 0); + break; + case IS_NULL: + decode = zend_strndup(ZEND_STRL("null")); + break; + case IS_FALSE: + decode = zend_strndup(ZEND_STRL("false")); + break; + case IS_TRUE: + decode = zend_strndup(ZEND_STRL("true")); + break; + case IS_LONG: + asprintf(&decode, "%lld", Z_LVAL_P(literal)); + break; + case IS_DOUBLE: + asprintf(&decode, "%.*G", 14, Z_DVAL_P(literal)); + break; + case IS_STRING: { + int i; + zend_string *str = php_addcslashes(Z_STR_P(literal), 0, "\\\"", 2); + for (i = 0; i < str->len; i++) { + if (str->val[i] < 32) { + str->val[i] = ' '; + } + } + asprintf(&decode, "\"%.*s\"%c", str->len <= 18 ? (int) str->len : 17, str->val, str->len <= 18 ? 0 : '+'); + zend_string_release(str); + } break; + case IS_RESOURCE: + asprintf(&decode, "Rsrc #%d", Z_RES_HANDLE_P(literal)); + break; + case IS_ARRAY: + asprintf(&decode, "array(%d)", zend_hash_num_elements(Z_ARR_P(literal))); + break; + case IS_OBJECT: { + zend_string *str = Z_OBJCE_P(literal)->name; + asprintf(&decode, "%.*s%c", str->len <= 18 ? (int) str->len : 18, str->val, str->len <= 18 ? 0 : '+'); + } break; + case IS_CONSTANT: + decode = zend_strndup(ZEND_STRL("")); + break; + case IS_CONSTANT_AST: + decode = zend_strndup(ZEND_STRL("")); + break; + default: + asprintf(&decode, "unknown type: %d", Z_TYPE_P(literal)); + break; + } + } break; case IS_UNUSED: asprintf(&decode, ""); @@ -80,47 +131,39 @@ char *phpdbg_decode_opline(zend_op_array *ops, zend_op *op, HashTable *vars) /*{ switch (op->opcode) { case ZEND_JMP: -#ifdef ZEND_GOTO case ZEND_GOTO: -#endif -#ifdef ZEND_FAST_CALL case ZEND_FAST_CALL: -#endif - asprintf(&decode[1], "J%ld", OP_JMP_ADDR(op, op->op1) - ops->opcodes); + asprintf(&decode[1], "J%ld", OP_JMP_ADDR(op, op->op1) - ops->opcodes); goto format; case ZEND_JMPZNZ: - decode[1] = phpdbg_decode_op(ops, &op->op1, op->op1_type, vars); - asprintf(&decode[2], "J%u or J%" PRIu32, op->op2.opline_num, op->extended_value); + decode[1] = phpdbg_decode_op(ops, &op->op1, op->op1_type, vars); + asprintf(&decode[2], "J%u or J%" PRIu32, op->op2.opline_num, op->extended_value); goto result; case ZEND_JMPZ: case ZEND_JMPNZ: case ZEND_JMPZ_EX: case ZEND_JMPNZ_EX: - -#ifdef ZEND_JMP_SET case ZEND_JMP_SET: -#endif decode[1] = phpdbg_decode_op(ops, &op->op1, op->op1_type, vars); asprintf(&decode[2], "J%ld", OP_JMP_ADDR(op, op->op2) - ops->opcodes); - goto result; + goto result; case ZEND_RECV_INIT: goto result; - default: { - decode[1] = phpdbg_decode_op(ops, &op->op1, op->op1_type, vars); - decode[2] = phpdbg_decode_op(ops, &op->op2, op->op2_type, vars); + default: + decode[1] = phpdbg_decode_op(ops, &op->op1, op->op1_type, vars); + decode[2] = phpdbg_decode_op(ops, &op->op2, op->op2_type, vars); result: - decode[3] = phpdbg_decode_op(ops, &op->result, op->result_type, vars); + decode[3] = phpdbg_decode_op(ops, &op->result, op->result_type, vars); format: - asprintf(&decode[0], - "%-20s %-20s %-20s", - decode[1] ? decode[1] : "", - decode[2] ? decode[2] : "", - decode[3] ? decode[3] : ""); - } + asprintf(&decode[0], + "%-20s %-20s %-20s", + decode[1] ? decode[1] : "", + decode[2] ? decode[2] : "", + decode[3] ? decode[3] : ""); } if (decode[1]) diff --git a/sapi/phpdbg/phpdbg_print.c b/sapi/phpdbg/phpdbg_print.c index 052ac12884..ffdf325b5b 100644 --- a/sapi/phpdbg/phpdbg_print.c +++ b/sapi/phpdbg/phpdbg_print.c @@ -63,14 +63,14 @@ static inline void phpdbg_print_function_helper(zend_function *method) /* {{{ */ end = op_array->last-1; if (method->common.scope) { - phpdbg_writeln("printoplineinfo", "type=\"User\" startline=\"%d\" endline=\"%d\" method=\"%s::%s\" file=\"%s\"", "\tL%d-%d %s::%s() %s", + phpdbg_writeln("printoplineinfo", "type=\"User\" startline=\"%d\" endline=\"%d\" method=\"%s::%s\" file=\"%s\"", "L%d-%d %s::%s() %s", op_array->line_start, op_array->line_end, method->common.scope->name->val, method->common.function_name->val, op_array->filename ? op_array->filename->val : "unknown"); } else { - phpdbg_writeln("printoplineinfo", "type=\"User\" startline=\"%d\" endline=\"%d\" function=\"%s\" file=\"%s\"", "\tL%d-%d %s() %s", + phpdbg_writeln("printoplineinfo", "type=\"User\" startline=\"%d\" endline=\"%d\" function=\"%s\" file=\"%s\"", "L%d-%d %s() %s", method->common.function_name ? op_array->line_start : 0, method->common.function_name ? op_array->line_end : 0, method->common.function_name ? method->common.function_name->val : "{main}", @@ -81,14 +81,21 @@ static inline void phpdbg_print_function_helper(zend_function *method) /* {{{ */ do { char *decode = phpdbg_decode_opline(op_array, opline, &vars); if (decode != NULL) { - phpdbg_writeln("print", "line=\"%u\" opline=\"%p\" opcode=\"%s\" op=\"%s\"", "\t\tL%u\t%p %-30s %s", - opline->lineno, - opline, - phpdbg_decode_opcode(opline->opcode), - decode); + if (PHPDBG_G(flags) & PHPDBG_PRINT_OPLINE_ADDR) { + phpdbg_writeln("print", "line=\"%u\" opline=\"%p\" opcode=\"%s\" op=\"%s\"", " L%-5u %p %-36s %s", + opline->lineno, + opline, + phpdbg_decode_opcode(opline->opcode), + decode); + } else { + phpdbg_writeln("print", "line=\"%u\" opcode=\"%s\" op=\"%s\"", " L%-5u %-36s %s", + opline->lineno, + phpdbg_decode_opcode(opline->opcode), + decode); + } free(decode); } else { - phpdbg_error("print", "type=\"decodefailure\" opline=\"%16p\"", "\tFailed to decode opline %16p", opline); + phpdbg_error("print", "type=\"decodefailure\" opline=\"%16p\"", "Failed to decode opline %16p", opline); } opline++; } while (opcode++ < end);