From: krakjoe Date: Sat, 16 Nov 2013 13:15:00 +0000 (+0000) Subject: move phpdbg_param_t to phpdbg.c|h X-Git-Tag: php-5.6.0alpha1~110^2~288 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=10ec0ee8b1381451d16a73719f66e0b22d52f101;p=php move phpdbg_param_t to phpdbg.c|h add string length and type to param_t (made struct instead of union) typedef enumerated param types add some error messages when command recieves unsupported values --- diff --git a/phpdbg.c b/phpdbg.c index df86c3a18c..40a2432b50 100644 --- a/phpdbg.c +++ b/phpdbg.c @@ -30,6 +30,97 @@ void (*zend_execute_old)(zend_execute_data *execute_data TSRMLS_DC); void (*zend_execute_old)(zend_op_array *op_array TSRMLS_DC); #endif +const char* phpdbg_get_param_type(phpdbg_param_t *param TSRMLS_DC) { + switch (param->type) { + case EMPTY_PARAM: + return "empty"; + case ADDR_PARAM: + return "address"; + case NUMERIC_PARAM: + return "numeric"; + case METHOD_PARAM: + return "method"; + case FILE_PARAM: + return "file"; + case STR_PARAM: + return "string"; + + default: /* this is bad */ + return "unknown"; + } +} + +phpdbg_param_type phpdbg_parse_param(const char *str, size_t len, phpdbg_param_t *param TSRMLS_DC) /* {{{ */ +{ + char *class_name, *func_name; + + if (len == 0) { + param->type = EMPTY_PARAM; + goto parsed; + } + + if (phpdbg_is_addr(str)) { + + param->addr = strtoul(str, 0, 16); + param->type = ADDR_PARAM; + goto parsed; + + } else if (phpdbg_is_numeric(str)) { + + param->num = strtol(str, NULL, 0); + param->type = NUMERIC_PARAM; + goto parsed; + + } else if (phpdbg_is_class_method(str, len+1, &class_name, &func_name)) { + + param->method.class = class_name; + param->method.name = func_name; + param->type = METHOD_PARAM; + goto parsed; + + } else { + const char *line_pos = strchr(str, ':'); + + if (line_pos && phpdbg_is_numeric(line_pos+1)) { + char path[MAXPATHLEN]; + + memcpy(path, str, line_pos - str); + path[line_pos - str] = 0; + + param->file.name = phpdbg_resolve_path(path TSRMLS_CC); + param->file.line = strtol(line_pos+1, NULL, 0); + param->type = FILE_PARAM; + goto parsed; + } + } + + param->str = estrndup(str, len); + param->len = len; + param->type = STR_PARAM; + +parsed: + phpdbg_debug("phpdbg_parse_param(\"%s\", %lu): %s", str, len, phpdbg_get_param_type(param TSRMLS_CC)); + return param->type; +} /* }}} */ + +void phpdbg_clear_param(phpdbg_param_t *param TSRMLS_DC) /* {{{ */ +{ + switch (param->type) { + case FILE_PARAM: + efree(param->file.name); + break; + case METHOD_PARAM: + efree(param->method.class); + efree(param->method.name); + break; + case STR_PARAM: + efree(param->str); + break; + default: + break; + } +} /* }}} */ + static inline void php_phpdbg_globals_ctor(zend_phpdbg_globals *pg) /* {{{ */ { pg->exec = NULL; diff --git a/phpdbg.h b/phpdbg.h index 3d3cdc0260..39d55d1a5b 100644 --- a/phpdbg.h +++ b/phpdbg.h @@ -112,4 +112,46 @@ ZEND_BEGIN_MODULE_GLOBALS(phpdbg) FILE *oplog; /* opline log */ ZEND_END_MODULE_GLOBALS(phpdbg) +/* {{{ Command and Parameter */ +typedef enum { + EMPTY_PARAM = 0, + ADDR_PARAM, + FILE_PARAM, + METHOD_PARAM, + STR_PARAM, + NUMERIC_PARAM +} phpdbg_param_type; + +typedef struct _phpdbg_param { + phpdbg_param_type type; + long num; + zend_ulong addr; + struct { + char *name; + long line; + } file; + struct { + char *class; + char *name; + } method; + char *str; + size_t len; +} phpdbg_param_t; + +typedef int (*phpdbg_command_handler_t)(const char* expr, size_t expr_len TSRMLS_DC); + +struct _phpdbg_command_t { + const char *name; /* Command name */ + size_t name_len; /* Command name length */ + const char *tip; /* Menu tip */ + size_t tip_len; /* Menu tip length */ + char alias; /* Alias */ + phpdbg_command_handler_t handler; /* Command handler */ +}; + +phpdbg_param_type phpdbg_parse_param(const char*, size_t, phpdbg_param_t* TSRMLS_DC); +void phpdbg_clear_param(phpdbg_param_t * TSRMLS_DC); +const char* phpdbg_get_param_type(phpdbg_param_t *param TSRMLS_DC); +/* }}} */ + #endif /* PHPDBG_H */ diff --git a/phpdbg_break.c b/phpdbg_break.c index 66f973fd0b..035d71ac89 100644 --- a/phpdbg_break.c +++ b/phpdbg_break.c @@ -29,9 +29,8 @@ ZEND_EXTERN_MODULE_GLOBALS(phpdbg); PHPDBG_BREAK(file) /* {{{ */ { phpdbg_param_t param; - int type; - - switch ((type=phpdbg_parse_param(expr, expr_len, ¶m TSRMLS_CC))) { + + switch (phpdbg_parse_param(expr, expr_len, ¶m TSRMLS_CC)) { case EMPTY_PARAM: phpdbg_error("No expression provided"); break; @@ -39,11 +38,11 @@ PHPDBG_BREAK(file) /* {{{ */ phpdbg_set_breakpoint_file(param.file.name, param.file.line TSRMLS_CC); break; default: - phpdbg_error("Wrong parameter"); + phpdbg_error("Unsupported parameter type (%s) for function", phpdbg_get_param_type(¶m TSRMLS_CC)); break; } - phpdbg_clear_param(type, ¶m TSRMLS_CC); + phpdbg_clear_param(¶m TSRMLS_CC); return SUCCESS; } /* }}} */ @@ -51,21 +50,21 @@ PHPDBG_BREAK(file) /* {{{ */ PHPDBG_BREAK(method) /* {{{ */ { phpdbg_param_t param; - int type; - - switch ((type=phpdbg_parse_param(expr, expr_len, ¶m TSRMLS_CC))) { + + switch (phpdbg_parse_param(expr, expr_len, ¶m TSRMLS_CC)) { case EMPTY_PARAM: phpdbg_error("No expression provided"); break; case METHOD_PARAM: phpdbg_set_breakpoint_method(param.method.class, param.method.name TSRMLS_CC); break; + default: - phpdbg_error("Wrong parameter"); + phpdbg_error("Unsupported parameter type (%s) for function", phpdbg_get_param_type(¶m TSRMLS_CC)); break; } - phpdbg_clear_param(type, ¶m TSRMLS_CC); + phpdbg_clear_param(¶m TSRMLS_CC); return SUCCESS; } /* }}} */ @@ -73,9 +72,8 @@ PHPDBG_BREAK(method) /* {{{ */ PHPDBG_BREAK(address) /* {{{ */ { phpdbg_param_t param; - int type; - switch ((type=phpdbg_parse_param(expr, expr_len, ¶m TSRMLS_CC))) { + switch (phpdbg_parse_param(expr, expr_len, ¶m TSRMLS_CC)) { case EMPTY_PARAM: phpdbg_error("No expression provided"); break; @@ -83,11 +81,11 @@ PHPDBG_BREAK(address) /* {{{ */ phpdbg_set_breakpoint_opline(param.addr TSRMLS_CC); break; default: - phpdbg_error("Wrong parameter"); + phpdbg_error("Unsupported parameter type (%s) for function", phpdbg_get_param_type(¶m TSRMLS_CC)); break; } - phpdbg_clear_param(type, ¶m TSRMLS_CC); + phpdbg_clear_param(¶m TSRMLS_CC); return SUCCESS; } /* }}} */ @@ -105,14 +103,13 @@ PHPDBG_BREAK(on) /* {{{ */ PHPDBG_BREAK(lineno) /* {{{ */ { phpdbg_param_t param; - int type; - + if (!PHPDBG_G(exec)) { phpdbg_error("Not file context found!"); return SUCCESS; } - switch ((type=phpdbg_parse_param(expr, expr_len, ¶m TSRMLS_CC))) { + switch (phpdbg_parse_param(expr, expr_len, ¶m TSRMLS_CC)) { case EMPTY_PARAM: phpdbg_error("No expression provided!"); break; @@ -120,11 +117,11 @@ PHPDBG_BREAK(lineno) /* {{{ */ phpdbg_set_breakpoint_file(phpdbg_current_file(TSRMLS_C), param.num TSRMLS_CC); break; default: - phpdbg_error("Wrong parameter"); + phpdbg_error("Unsupported parameter type (%s) for function", phpdbg_get_param_type(¶m TSRMLS_CC)); break; } - phpdbg_clear_param(type, ¶m TSRMLS_CC); + phpdbg_clear_param(¶m TSRMLS_CC); return SUCCESS; } /* }}} */ @@ -132,9 +129,8 @@ PHPDBG_BREAK(lineno) /* {{{ */ PHPDBG_BREAK(func) /* {{{ */ { phpdbg_param_t param; - int type; - switch ((type=phpdbg_parse_param(expr, expr_len, ¶m TSRMLS_CC))) { + switch (phpdbg_parse_param(expr, expr_len, ¶m TSRMLS_CC)) { case EMPTY_PARAM: phpdbg_error("No expression provided!"); break; @@ -142,11 +138,11 @@ PHPDBG_BREAK(func) /* {{{ */ phpdbg_set_breakpoint_symbol(param.str TSRMLS_CC); break; default: - phpdbg_error("Wrong parameter"); + phpdbg_error("Unsupported parameter type (%s) for function", phpdbg_get_param_type(¶m TSRMLS_CC)); break; } - phpdbg_clear_param(type, ¶m TSRMLS_CC); + phpdbg_clear_param(¶m TSRMLS_CC); return SUCCESS; } /* }}} */ diff --git a/phpdbg_list.c b/phpdbg_list.c index 2e4f5e79b3..e85074be0e 100644 --- a/phpdbg_list.c +++ b/phpdbg_list.c @@ -67,24 +67,22 @@ static inline void i_phpdbg_list_func(const char *str TSRMLS_DC) PHPDBG_LIST(lines) /* {{{ */ { phpdbg_param_t param; - int type = phpdbg_parse_param( - expr, expr_len, ¶m TSRMLS_CC); - switch (type) { + switch (phpdbg_parse_param(expr, expr_len, ¶m TSRMLS_CC)) { case NUMERIC_PARAM: case EMPTY_PARAM: { if (PHPDBG_G(exec) || zend_is_executing(TSRMLS_C)) { - if (type == EMPTY_PARAM) { + if (param.type == EMPTY_PARAM) { phpdbg_list_file(phpdbg_current_file(TSRMLS_C), 0, 0 TSRMLS_CC); } else phpdbg_list_file(phpdbg_current_file(TSRMLS_C), param.num, 0 TSRMLS_CC); } else phpdbg_error("Not executing, and execution context not set"); } break; default: - phpdbg_error("Unsupported parameter type (%d) for command", type); + phpdbg_error("Unsupported parameter type (%s) for function", phpdbg_get_param_type(¶m TSRMLS_CC)); } - phpdbg_clear_param(type, ¶m TSRMLS_CC); + phpdbg_clear_param(¶m TSRMLS_CC); return SUCCESS; } /* }}} */ @@ -92,26 +90,27 @@ PHPDBG_LIST(lines) /* {{{ */ PHPDBG_LIST(func) /* {{{ */ { phpdbg_param_t param; - int type = phpdbg_parse_param( - expr, expr_len, ¶m TSRMLS_CC); - - if (type == STR_PARAM) { + + if (phpdbg_parse_param(expr, expr_len, ¶m TSRMLS_CC) == STR_PARAM) { i_phpdbg_list_func( param.str TSRMLS_CC); + } else { + phpdbg_error( + "Unsupported parameter type (%s) for function", phpdbg_get_param_type(¶m TSRMLS_CC)); } - phpdbg_clear_param(type, ¶m TSRMLS_CC); + phpdbg_clear_param(¶m TSRMLS_CC); return SUCCESS; } /* }}} */ -void phpdbg_list_dispatch(int type, phpdbg_param_t *param TSRMLS_DC) /* {{{ */ +void phpdbg_list_dispatch(phpdbg_param_t *param TSRMLS_DC) /* {{{ */ { - switch (type) { + switch (param->type) { case NUMERIC_PARAM: case EMPTY_PARAM: { if (PHPDBG_G(exec) || zend_is_executing(TSRMLS_C)) { - if (type == EMPTY_PARAM) { + if (param->type == EMPTY_PARAM) { phpdbg_list_file(phpdbg_current_file(TSRMLS_C), 0, 0 TSRMLS_CC); } else phpdbg_list_file(phpdbg_current_file(TSRMLS_C), param->num, 0 TSRMLS_CC); } else phpdbg_error("Not executing, and execution context not set"); @@ -126,7 +125,8 @@ void phpdbg_list_dispatch(int type, phpdbg_param_t *param TSRMLS_DC) /* {{{ */ } break; default: - phpdbg_error("Unsupported input type (%d) for command", type); + phpdbg_error( + "Unsupported parameter type (%s) for function", phpdbg_get_param_type(param TSRMLS_CC)); break; } } /* }}} */ diff --git a/phpdbg_list.h b/phpdbg_list.h index 96323fc0ea..bb8f34cbfc 100644 --- a/phpdbg_list.h +++ b/phpdbg_list.h @@ -39,7 +39,7 @@ PHPDBG_LIST(func); void phpdbg_list_function(const zend_function* TSRMLS_DC); void phpdbg_list_file(const char*, long, long TSRMLS_DC); -void phpdbg_list_dispatch(int type, phpdbg_param_t *param TSRMLS_DC); +void phpdbg_list_dispatch(phpdbg_param_t *param TSRMLS_DC); static const phpdbg_command_t phpdbg_list_commands[] = { PHPDBG_LIST_EX_D(lines, "lists the specified lines", 'l'), diff --git a/phpdbg_prompt.c b/phpdbg_prompt.c index 58e37add98..aa75fefa48 100644 --- a/phpdbg_prompt.c +++ b/phpdbg_prompt.c @@ -449,7 +449,6 @@ static PHPDBG_COMMAND(print) /* {{{ */ static PHPDBG_COMMAND(break) /* {{{ */ { phpdbg_param_t param; - int type; if (expr_len == 0) { phpdbg_error("No expression found"); @@ -461,7 +460,7 @@ static PHPDBG_COMMAND(break) /* {{{ */ return SUCCESS; } - switch ((type=phpdbg_parse_param(expr, expr_len, ¶m TSRMLS_CC))) { + switch (phpdbg_parse_param(expr, expr_len, ¶m TSRMLS_CC)) { case ADDR_PARAM: phpdbg_set_breakpoint_opline(param.addr TSRMLS_CC); break; @@ -481,7 +480,7 @@ static PHPDBG_COMMAND(break) /* {{{ */ break; } - phpdbg_clear_param(type, ¶m TSRMLS_CC); + phpdbg_clear_param(¶m TSRMLS_CC); return SUCCESS; } /* }}} */ @@ -658,18 +657,15 @@ static PHPDBG_COMMAND(quiet) { /* {{{ */ static PHPDBG_COMMAND(list) /* {{{ */ { phpdbg_param_t param; - int type = 0; /* allow advanced listers to run */ if (phpdbg_do_cmd(phpdbg_list_commands, (char*)expr, expr_len TSRMLS_CC) == SUCCESS) { return SUCCESS; } - - phpdbg_list_dispatch( - phpdbg_parse_param(expr, expr_len, ¶m TSRMLS_CC), - ¶m TSRMLS_CC); - - phpdbg_clear_param(type, ¶m TSRMLS_CC); + + phpdbg_parse_param(expr, expr_len, ¶m TSRMLS_CC); + phpdbg_list_dispatch(¶m TSRMLS_CC); + phpdbg_clear_param(¶m TSRMLS_CC); return SUCCESS; } /* }}} */ diff --git a/phpdbg_prompt.h b/phpdbg_prompt.h index 5b45ffc700..2eae44c786 100644 --- a/phpdbg_prompt.h +++ b/phpdbg_prompt.h @@ -30,23 +30,6 @@ #define PHPDBG_NEXT 2 #define PHPDBG_UNTIL 3 -/** - * Command handler - */ -typedef int (*phpdbg_command_handler_t)(const char* expr, size_t expr_len TSRMLS_DC); - -/** - * Command representation - */ -struct _phpdbg_command_t { - const char *name; /* Command name */ - size_t name_len; /* Command name length */ - const char *tip; /* Menu tip */ - size_t tip_len; /* Menu tip length */ - char alias; /* Alias */ - phpdbg_command_handler_t handler; /* Command handler */ -} ; - /** * Command Executor */ diff --git a/phpdbg_utils.c b/phpdbg_utils.c index 7538c774dc..1804fd9773 100644 --- a/phpdbg_utils.c +++ b/phpdbg_utils.c @@ -104,61 +104,6 @@ const char *phpdbg_current_file(TSRMLS_D) /* {{{ */ return file; } /* }}} */ -int phpdbg_parse_param(const char *str, size_t len, phpdbg_param_t *param TSRMLS_DC) /* {{{ */ -{ - char *class_name, *func_name; - - if (len == 0) { - return EMPTY_PARAM; - } - - if (phpdbg_is_addr(str)) { - param->addr = strtoul(str, 0, 16); - return ADDR_PARAM; - } else if (phpdbg_is_numeric(str)) { - param->num = strtol(str, NULL, 0); - return NUMERIC_PARAM; - } else if (phpdbg_is_class_method(str, len+1, &class_name, &func_name)) { - param->method.class = class_name; - param->method.name = func_name; - return METHOD_PARAM; - } else { - const char *line_pos = strchr(str, ':'); - - if (line_pos && phpdbg_is_numeric(line_pos+1)) { - char path[MAXPATHLEN]; - - memcpy(path, str, line_pos - str); - path[line_pos - str] = 0; - - param->file.name = phpdbg_resolve_path(path TSRMLS_CC); - param->file.line = strtol(line_pos+1, NULL, 0); - return FILE_PARAM; - } - } - - param->str = estrndup(str, len); - return STR_PARAM; -} /* }}} */ - -void phpdbg_clear_param(int type, phpdbg_param_t *param TSRMLS_DC) /* {{{ */ -{ - switch (type) { - case FILE_PARAM: - efree(param->file.name); - break; - case METHOD_PARAM: - efree(param->method.class); - efree(param->method.name); - break; - case STR_PARAM: - efree(param->str); - break; - default: - break; - } -} /* }}} */ - int phpdbg_print(int type TSRMLS_DC, FILE *fp, const char *format, ...) /* {{{ */ { int rc = 0; diff --git a/phpdbg_utils.h b/phpdbg_utils.h index badb4cb12d..3de4010ca0 100644 --- a/phpdbg_utils.h +++ b/phpdbg_utils.h @@ -27,35 +27,6 @@ int phpdbg_is_numeric(const char*); int phpdbg_is_empty(const char*); int phpdbg_is_addr(const char*); int phpdbg_is_class_method(const char*, size_t, char**, char**); - -/** - * Parameter parsing stuff - */ -enum { - EMPTY_PARAM = 0, - ADDR_PARAM, - FILE_PARAM, - METHOD_PARAM, - STR_PARAM, - NUMERIC_PARAM -}; - -typedef union _phpdbg_param { - long num; - zend_ulong addr; - struct { - char *name; - long line; - } file; - struct { - char *class; - char *name; - } method; - char *str; -} phpdbg_param_t; - -int phpdbg_parse_param(const char*, size_t, phpdbg_param_t* TSRMLS_DC); -void phpdbg_clear_param(int, phpdbg_param_t * TSRMLS_DC); const char *phpdbg_current_file(TSRMLS_D); char *phpdbg_resolve_path(const char* TSRMLS_DC);