php-src/phpdbg_cmd.c

541 lines
12 KiB
C
Raw Normal View History

/*
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2013 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Felipe Pena <felipe@php.net> |
| Authors: Joe Watkins <joe.watkins@live.co.uk> |
+----------------------------------------------------------------------+
*/
#include "phpdbg.h"
#include "phpdbg_cmd.h"
#include "phpdbg_utils.h"
2013-11-24 14:54:14 +00:00
#include "phpdbg_set.h"
ZEND_EXTERN_MODULE_GLOBALS(phpdbg);
2013-11-23 18:12:51 +00:00
PHPDBG_API const char *phpdbg_get_param_type(const 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";
2013-11-26 10:02:58 +00:00
}
}
2013-11-23 18:12:51 +00:00
PHPDBG_API 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;
2013-11-26 10:02:58 +00:00
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:
2013-11-26 10:02:58 +00:00
phpdbg_debug("phpdbg_parse_param(\"%s\", %lu): %s",
2013-11-18 21:59:15 +00:00
str, len, phpdbg_get_param_type(param TSRMLS_CC));
return param->type;
} /* }}} */
2013-11-23 18:12:51 +00:00
PHPDBG_API void phpdbg_clear_param(phpdbg_param_t *param TSRMLS_DC) /* {{{ */
{
2013-11-18 21:59:42 +00:00
if (param) {
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;
}
}
2013-11-19 21:02:38 +00:00
} /* }}} */
2013-12-03 00:15:33 +00:00
PHPDBG_API void phpdbg_copy_param(const phpdbg_param_t* src, phpdbg_param_t* dest TSRMLS_DC) /* {{{ */
{
switch ((dest->type = src->type)) {
case STR_PARAM:
dest->str = estrndup(src->str, src->len);
dest->len = src->len;
break;
case ADDR_PARAM:
dest->addr = src->addr;
break;
case NUMERIC_PARAM:
dest->num = src->num;
break;
case METHOD_PARAM:
dest->method.class = estrdup(src->method.class);
dest->method.name = estrdup(src->method.name);
break;
case FILE_PARAM:
dest->file.name = estrdup(src->file.name);
dest->file.line = src->file.line;
break;
case EMPTY_PARAM: { /* do nothing */ } break;
}
} /* }}} */
PHPDBG_API zend_ulong phpdbg_hash_param(const phpdbg_param_t *param TSRMLS_DC) /* {{{ */
{
zend_ulong hash = param->type;
switch (param->type) {
case STR_PARAM:
hash += zend_inline_hash_func(param->str, param->len);
break;
case METHOD_PARAM:
hash += zend_inline_hash_func(param->method.class, strlen(param->method.class));
hash += zend_inline_hash_func(param->method.name, strlen(param->method.name));
break;
case FILE_PARAM:
hash += zend_inline_hash_func(param->file.name, strlen(param->file.name));
hash += param->file.line;
break;
case ADDR_PARAM:
hash += param->addr;
break;
case NUMERIC_PARAM:
hash += param->num;
break;
case EMPTY_PARAM: { /* do nothing */ } break;
}
return hash;
} /* }}} */
PHPDBG_API zend_bool phpdbg_match_param(const phpdbg_param_t *l, const phpdbg_param_t *r TSRMLS_DC) /* {{{ */
{
if (l && r) {
if (l->type == r->type) {
switch (l->type) {
case STR_PARAM:
return (l->len == r->len) &&
(memcmp(l->str, r->str, l->len) == SUCCESS);
case NUMERIC_PARAM:
return (l->num == r->num);
case ADDR_PARAM:
return (l->addr == r->addr);
case FILE_PARAM: {
if (l->file.line == r->file.line) {
size_t lengths[2] = {
strlen(l->file.name), strlen(r->file.name)};
if (lengths[0] == lengths[1]) {
return (memcmp(
l->file.name, r->file.name, lengths[0]) == SUCCESS);
}
}
} break;
case METHOD_PARAM: {
size_t lengths[2] = {
strlen(l->method.class), strlen(r->method.class)};
if (lengths[0] == lengths[1]) {
if (memcmp(l->method.class, r->method.class, lengths[0]) == SUCCESS) {
lengths[0] = strlen(l->method.name);
lengths[1] = strlen(r->method.name);
if (lengths[0] == lengths[1]) {
return (memcmp(
l->method.name, r->method.name, lengths[0]) == SUCCESS);
}
}
}
} break;
case EMPTY_PARAM:
return 1;
}
}
}
return 0;
} /* }}} */
2013-11-23 18:12:51 +00:00
PHPDBG_API phpdbg_input_t **phpdbg_read_argv(char *buffer, int *argc TSRMLS_DC) /* {{{ */
2013-11-19 17:18:58 +00:00
{
2013-11-19 21:02:38 +00:00
char *p;
2013-11-19 19:10:09 +00:00
char b[PHPDBG_MAX_CMD];
int l=0;
2013-11-19 19:24:21 +00:00
enum states {
IN_BETWEEN,
2013-11-19 21:02:38 +00:00
IN_WORD,
IN_STRING
2013-11-19 17:18:58 +00:00
} state = IN_BETWEEN;
2013-11-19 19:10:09 +00:00
phpdbg_input_t **argv = NULL;
2013-11-19 21:02:38 +00:00
argv = (phpdbg_input_t**) emalloc(sizeof(phpdbg_input_t*));
2013-11-19 19:10:09 +00:00
(*argc) = 0;
2013-11-19 21:02:38 +00:00
2013-11-26 10:02:58 +00:00
#define RESET_STATE() do { \
phpdbg_input_t *arg = emalloc(sizeof(phpdbg_input_t)); \
if (arg) { \
b[l]=0; \
arg->length = l; \
arg->string = estrndup(b, arg->length); \
arg->argv = NULL; \
arg->argc = 0; \
argv = (phpdbg_input_t**) erealloc(argv, sizeof(phpdbg_input_t*) * ((*argc)+1)); \
argv[(*argc)++] = arg; \
l = 0; \
} \
state = IN_BETWEEN; \
} while (0)
2013-11-19 17:18:58 +00:00
for (p = buffer; *p != '\0'; p++) {
int c = (unsigned char) *p;
switch (state) {
case IN_BETWEEN:
if (isspace(c)) {
2013-11-26 10:02:58 +00:00
continue;
2013-11-19 17:18:58 +00:00
}
if (c == '"') {
2013-11-26 10:02:58 +00:00
state = IN_STRING;
continue;
2013-11-19 17:18:58 +00:00
}
state = IN_WORD;
2013-11-19 19:10:09 +00:00
b[l++]=c;
2013-11-19 17:18:58 +00:00
continue;
case IN_STRING:
2013-11-23 19:50:25 +00:00
if (c == '"') {
2013-11-19 19:10:09 +00:00
if (buffer[(p - buffer)-1] == '\\') {
b[l-1]=c;
continue;
}
2013-11-26 10:02:58 +00:00
RESET_STATE();
2013-11-19 19:10:09 +00:00
} else {
b[l++]=c;
2013-11-19 17:18:58 +00:00
}
continue;
case IN_WORD:
if (isspace(c)) {
2013-11-26 10:02:58 +00:00
RESET_STATE();
2013-11-19 19:10:09 +00:00
} else {
b[l++]=c;
2013-11-19 17:18:58 +00:00
}
continue;
}
}
2013-11-19 21:02:38 +00:00
2013-11-19 17:18:58 +00:00
switch (state) {
case IN_WORD: {
RESET_STATE();
} break;
2013-11-19 21:02:38 +00:00
2013-11-19 17:18:58 +00:00
case IN_STRING:
2013-11-19 19:11:18 +00:00
phpdbg_error(
2013-11-24 12:27:22 +00:00
"Malformed command line (unclosed quote) @ %ld: %s!",
2013-11-19 19:35:48 +00:00
(p - buffer)-1, &buffer[(p - buffer)-1]);
2013-11-19 17:18:58 +00:00
break;
2013-11-23 19:50:25 +00:00
case IN_BETWEEN:
break;
2013-11-19 17:18:58 +00:00
}
2013-11-19 21:02:38 +00:00
2013-11-19 19:35:48 +00:00
if ((*argc) == 0) {
/* not needed */
efree(argv);
2013-11-19 21:02:38 +00:00
2013-11-19 19:36:39 +00:00
/* to be sure */
return NULL;
2013-11-19 19:35:48 +00:00
}
2013-11-19 21:02:38 +00:00
2013-11-19 19:10:09 +00:00
return argv;
2013-11-19 17:18:58 +00:00
} /* }}} */
2013-11-23 18:12:51 +00:00
PHPDBG_API phpdbg_input_t *phpdbg_read_input(char *buffered TSRMLS_DC) /* {{{ */
{
2013-11-20 10:04:05 +00:00
phpdbg_input_t *buffer = NULL;
char *cmd = NULL;
2013-11-20 10:04:05 +00:00
if (!(PHPDBG_G(flags) & PHPDBG_IS_QUITTING)) {
2013-11-28 17:30:27 +00:00
if ((PHPDBG_G(flags) & PHPDBG_IS_REMOTE) &&
(buffered == NULL)) {
fflush(PHPDBG_G(io)[PHPDBG_STDOUT]);
}
2013-11-27 16:49:51 +00:00
2013-11-28 17:30:27 +00:00
if (buffered == NULL) {
#ifndef HAVE_LIBREADLINE
2013-11-20 10:04:05 +00:00
char buf[PHPDBG_MAX_CMD];
if ((!(PHPDBG_G(flags) & PHPDBG_IS_REMOTE) && !phpdbg_write(phpdbg_get_prompt(TSRMLS_C))) ||
2013-11-22 16:15:24 +00:00
!fgets(buf, PHPDBG_MAX_CMD, PHPDBG_G(io)[PHPDBG_STDIN])) {
2013-11-20 10:04:05 +00:00
/* the user has gone away */
phpdbg_error("Failed to read console !");
PHPDBG_G(flags) |= (PHPDBG_IS_QUITTING|PHPDBG_IS_DISCONNECTED);
2013-11-20 10:04:05 +00:00
zend_bailout();
return NULL;
}
2013-11-20 10:04:05 +00:00
cmd = buf;
#else
if ((PHPDBG_G(flags) & PHPDBG_IS_REMOTE)) {
char buf[PHPDBG_MAX_CMD];
if (fgets(buf, PHPDBG_MAX_CMD, PHPDBG_G(io)[PHPDBG_STDIN])) {
cmd = buf;
} else cmd = NULL;
} else cmd = readline(phpdbg_get_prompt(TSRMLS_C));
2013-11-20 10:04:05 +00:00
if (!cmd) {
/* the user has gone away */
phpdbg_error("Failed to read console !");
PHPDBG_G(flags) |= (PHPDBG_IS_QUITTING|PHPDBG_IS_DISCONNECTED);
2013-11-20 10:04:05 +00:00
zend_bailout();
return NULL;
}
if (!(PHPDBG_G(flags) & PHPDBG_IS_REMOTE)) {
add_history(cmd);
}
#endif
2013-11-20 10:04:05 +00:00
} else cmd = buffered;
/* allocate and sanitize buffer */
2013-11-27 09:55:16 +00:00
buffer = (phpdbg_input_t*) ecalloc(1, sizeof(phpdbg_input_t));
2013-11-19 21:26:40 +00:00
if (!buffer) {
return NULL;
}
buffer->string = phpdbg_trim(cmd, strlen(cmd), &buffer->length);
2013-11-19 21:02:38 +00:00
/* store constant pointer to start of buffer */
buffer->start = (char* const*) buffer->string;
2013-11-19 21:26:40 +00:00
buffer->argv = phpdbg_read_argv(
buffer->string, &buffer->argc TSRMLS_CC);
2013-11-19 21:26:40 +00:00
#ifdef PHPDBG_DEBUG
if (buffer->argc) {
int arg = 0;
2013-11-19 21:26:40 +00:00
while (arg < buffer->argc) {
phpdbg_debug(
"argv %d=%s", arg, buffer->argv[arg]->string);
arg++;
}
}
#endif
#ifdef HAVE_LIBREADLINE
if (!buffered && cmd &&
!(PHPDBG_G(flags) & PHPDBG_IS_REMOTE)) {
free(cmd);
}
#endif
return buffer;
}
2013-11-19 21:02:38 +00:00
return NULL;
} /* }}} */
2013-11-27 09:55:16 +00:00
PHPDBG_API void phpdbg_destroy_argv(phpdbg_input_t **argv, int argc TSRMLS_DC) /* {{{ */
2013-11-19 19:24:21 +00:00
{
2013-11-27 09:55:16 +00:00
if (argv) {
if (argc) {
2013-11-19 19:24:21 +00:00
int arg;
2013-11-27 09:55:16 +00:00
for (arg=0; arg<argc; arg++) {
2013-11-19 19:24:21 +00:00
phpdbg_destroy_input(
2013-11-27 09:55:16 +00:00
&argv[arg] TSRMLS_CC);
2013-11-19 19:24:21 +00:00
}
}
2013-11-27 09:55:16 +00:00
efree(argv);
}
} /* }}} */
2013-11-19 19:24:21 +00:00
2013-11-27 09:55:16 +00:00
PHPDBG_API void phpdbg_destroy_input(phpdbg_input_t **input TSRMLS_DC) /*{{{ */
{
if (*input) {
if ((*input)->string) {
efree((*input)->string);
2013-11-19 19:24:21 +00:00
}
2013-11-19 21:02:38 +00:00
2013-11-27 09:55:16 +00:00
phpdbg_destroy_argv(
(*input)->argv, (*input)->argc TSRMLS_CC);
2013-11-19 19:24:21 +00:00
efree(*input);
}
} /* }}} */
2013-11-23 18:12:51 +00:00
PHPDBG_API int phpdbg_do_cmd(const phpdbg_command_t *command, phpdbg_input_t *input TSRMLS_DC) /* {{{ */
2013-11-19 20:27:23 +00:00
{
int rc = FAILURE;
if (input->argc > 0) {
while (command && command->name && command->handler) {
2013-11-19 21:02:38 +00:00
if (((command->name_len == input->argv[0]->length) &&
2013-11-19 20:27:23 +00:00
(memcmp(command->name, input->argv[0]->string, command->name_len) == SUCCESS)) ||
(command->alias &&
2013-11-19 21:02:38 +00:00
(input->argv[0]->length == 1) &&
2013-11-19 20:27:23 +00:00
(command->alias == *input->argv[0]->string))) {
2013-11-19 22:09:19 +00:00
2013-11-19 21:54:58 +00:00
phpdbg_param_t param;
2013-11-19 22:09:19 +00:00
2013-11-19 21:54:58 +00:00
param.type = EMPTY_PARAM;
2013-11-21 00:06:44 +00:00
2013-11-19 21:54:58 +00:00
if (input->argc > 1) {
if (command->subs) {
2013-11-20 22:03:36 +00:00
phpdbg_input_t sub = *input;
2013-11-21 00:06:44 +00:00
sub.string += input->argv[0]->length;
sub.length -= input->argv[0]->length;
2013-11-21 00:06:44 +00:00
2013-11-20 22:03:36 +00:00
sub.string = phpdbg_trim(
sub.string, sub.length, &sub.length);
sub.argc--;
sub.argv++;
2013-11-21 00:06:44 +00:00
2013-11-19 21:54:58 +00:00
phpdbg_debug(
2013-11-19 22:09:19 +00:00
"trying sub commands in \"%s\" for \"%s\" with %d arguments",
2013-11-19 21:54:58 +00:00
command->name, sub.argv[0]->string, sub.argc-1);
2013-11-21 00:06:44 +00:00
2013-11-20 14:40:08 +00:00
if (phpdbg_do_cmd(command->subs, &sub TSRMLS_CC) == SUCCESS) {
efree(sub.string);
2013-11-20 14:40:08 +00:00
return SUCCESS;
}
2013-11-21 00:06:44 +00:00
efree(sub.string);
2013-11-20 22:23:34 +00:00
}
2013-11-21 00:06:44 +00:00
2013-11-20 22:23:34 +00:00
/* no sub command found */
{
char *store = input->string;
2013-11-21 00:06:44 +00:00
input->string += input->argv[0]->length;
input->length -= input->argv[0]->length;
2013-11-21 00:06:44 +00:00
2013-11-20 22:03:36 +00:00
input->string = phpdbg_trim(
input->string, input->length, &input->length);
2013-11-21 00:06:44 +00:00
efree(store);
2013-11-20 10:04:05 +00:00
}
2013-11-21 00:06:44 +00:00
2013-11-20 14:40:08 +00:00
/* pass parameter on */
phpdbg_parse_param(
input->string,
input->length,
2013-11-20 14:40:08 +00:00
&param TSRMLS_CC);
2013-11-19 20:27:23 +00:00
}
2013-11-21 00:06:44 +00:00
2013-11-19 20:27:23 +00:00
phpdbg_debug(
2013-11-19 21:02:38 +00:00
"found command %s for %s with %d arguments",
2013-11-19 20:27:23 +00:00
command->name, input->argv[0]->string, input->argc-1);
{
int arg;
for (arg=1; arg<input->argc; arg++) {
phpdbg_debug(
2013-12-01 17:03:39 +00:00
"\t#%d: [%s=%zu]",
2013-11-19 21:02:38 +00:00
arg,
2013-11-19 20:27:23 +00:00
input->argv[arg]->string,
input->argv[arg]->length);
}
}
2013-11-19 22:09:19 +00:00
rc = command->handler(&param, input TSRMLS_CC);
2013-11-24 14:54:14 +00:00
/* only set last command when it is worth it ! */
if ((rc != FAILURE) &&
!(PHPDBG_G(flags) & PHPDBG_IS_INITIALIZING)) {
PHPDBG_G(lcmd) = (phpdbg_command_t*) command;
phpdbg_clear_param(
&PHPDBG_G(lparam) TSRMLS_CC);
PHPDBG_G(lparam) = param;
}
2013-11-19 20:27:23 +00:00
break;
}
command++;
}
} else {
/* this should NEVER happen */
phpdbg_error(
"No function executed !!");
}
2013-11-20 18:07:45 +00:00
2013-11-19 20:27:23 +00:00
return rc;
} /* }}} */