From: Felipe Pena Date: Mon, 11 Nov 2013 23:40:03 +0000 (-0200) Subject: - Added initial code for list command X-Git-Tag: php-5.6.0alpha1~110^2~467^2~1 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=36d2329187074fa9fda2bfbc56c3c4360cd389b5;p=php - Added initial code for list command --- diff --git a/config.m4 b/config.m4 index bb9719015e..1a99db0f24 100644 --- a/config.m4 +++ b/config.m4 @@ -9,7 +9,7 @@ if test "$PHP_PHPDBG" != "no"; then AC_DEFINE(HAVE_PHPDBG, 1, [ ]) PHP_PHPDBG_CFLAGS="-I$abc_srcdir" - PHP_PHPDBG_FILES="phpdbg.c phpdbg_prompt.c phpdbg_help.c phpdbg_bp.c phpdbg_opcode.c" + PHP_PHPDBG_FILES="phpdbg.c phpdbg_prompt.c phpdbg_help.c phpdbg_bp.c phpdbg_opcode.c phpdbg_list.c" PHP_SUBST(PHP_PHPDBG_CFLAGS) PHP_SUBST(PHP_PHPDBG_FILES) @@ -27,7 +27,7 @@ if test "$PHP_PHPDBG" != "no"; then \$(PHPDBG_EXTRA_LIBS) \ \$(ZEND_EXTRA_LIBS) \ -o \$(BUILD_BINARY)" - + PHP_SUBST(BUILD_BINARY) PHP_SUBST(BUILD_PHPDBG) fi diff --git a/phpdbg_help.c b/phpdbg_help.c index ef8b6208b9..d91625e09e 100644 --- a/phpdbg_help.c +++ b/phpdbg_help.c @@ -120,7 +120,6 @@ PHPDBG_HELP(quiet) /* {{{ */ printf("Will silence OPLINE output, while\n"); printf("\tphpdbg> quiet 0\n"); printf("Will enable OPLINE output again\n"); - return SUCCESS; } /* }}} */ @@ -132,3 +131,11 @@ PHPDBG_HELP(back) /* {{{ */ printf("Will limit the number of frames to 5, the default is no limit\n"); return SUCCESS; } /* }}} */ + +PHPDBG_HELP(list) /* {{{ */ +{ + printf("The list command displays N line from current context file.\n"); + printf("\tphpdbg> list 2\n"); + printf("Will print next 2 lines from the current file\n"); + return SUCCESS; +} /* }}} */ diff --git a/phpdbg_help.h b/phpdbg_help.h index 449f4d1c58..9e187a8b39 100644 --- a/phpdbg_help.h +++ b/phpdbg_help.h @@ -20,6 +20,7 @@ #ifndef PHPDBG_HELP_H #define PHPDBG_HELP_H +#include "TSRM.h" #include "phpdbg_prompt.h" /** @@ -45,6 +46,7 @@ PHPDBG_HELP(clean); PHPDBG_HELP(clear); PHPDBG_HELP(back); PHPDBG_HELP(quiet); +PHPDBG_HELP(list); /** * Commands @@ -62,6 +64,7 @@ static const phpdbg_command_t phpdbg_help_commands[] = { PHPDBG_HELP_D(clear, "clearing breakpoints allows you to run code without interruption"), PHPDBG_HELP_D(back, "show debug backtrace information during execution"), PHPDBG_HELP_D(quiet, "be quiet during execution"), + PHPDBG_HELP_D(list, "list specified line"), {NULL, 0, 0} }; diff --git a/phpdbg_list.c b/phpdbg_list.c new file mode 100644 index 0000000000..9102a797fc --- /dev/null +++ b/phpdbg_list.c @@ -0,0 +1,72 @@ +/* + +----------------------------------------------------------------------+ + | 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 | + | Authors: Joe Watkins | + +----------------------------------------------------------------------+ +*/ + +#include +#include +#include +#include +#include +#include +#include "phpdbg_list.h" + +void phpdbg_list_file(const char *filename, long count, long offset) /* {{{ */ +{ + unsigned char *mem, *pos, *last_pos, *end_pos; + struct stat st; + int fd, all_content = (count == 0); + unsigned int line = 0, displayed = 0; + + if ((fd = open(filename, O_RDONLY)) == -1) { + printf("[Failed to open file to list]\n"); + return; + } + + fstat(fd, &st); + + last_pos = mem = mmap(0, st.st_size, PROT_READ, MAP_SHARED, fd, 0); + end_pos = mem + st.st_size; + + while (1) { + pos = memchr(last_pos, '\n', end_pos - last_pos); + + if (!pos) { + /* No more line breaks */ + break; + } + + ++line; + + if (!offset || offset <= line) { + /* Without offset, or offset reached */ + printf("%05u: %.*s\n", line, (int)(pos - last_pos), last_pos); + ++displayed; + } + + last_pos = pos + 1; + + if (!all_content && displayed == count) { + /* Reached max line to display */ + break; + } + } + + munmap(mem, st.st_size); + close(fd); + +} /* }}} */ diff --git a/phpdbg_list.h b/phpdbg_list.h new file mode 100644 index 0000000000..4531a65d10 --- /dev/null +++ b/phpdbg_list.h @@ -0,0 +1,25 @@ +/* + +----------------------------------------------------------------------+ + | 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 | + | Authors: Joe Watkins | + +----------------------------------------------------------------------+ +*/ + +#ifndef PHPDBG_LIST_H +#define PHPDBG_LIST_H + +void phpdbg_list_file(const char*, long, long); + +#endif /* PHPDBG_LIST_H */ diff --git a/phpdbg_prompt.c b/phpdbg_prompt.c index 306e929c5a..ab04a68eed 100644 --- a/phpdbg_prompt.c +++ b/phpdbg_prompt.c @@ -422,6 +422,19 @@ static PHPDBG_COMMAND(quiet) { /* {{{ */ return SUCCESS; } /* }}} */ +static PHPDBG_COMMAND(list) /* {{{ */ +{ + long offset = 0, count = strtol(expr, NULL, 0); + const char *filename = PHPDBG_G(exec); + + if (zend_is_executing(TSRMLS_C)) { + filename = zend_get_executed_filename(TSRMLS_C); + offset = zend_get_executed_lineno(TSRMLS_C); + } + + phpdbg_list_file(filename, count, offset); +} /* }}} */ + static const phpdbg_command_t phpdbg_prompt_commands[] = { PHPDBG_COMMAND_D(exec, "set execution context"), PHPDBG_COMMAND_D(compile, "attempt to pre-compile execution context"), @@ -432,6 +445,7 @@ static const phpdbg_command_t phpdbg_prompt_commands[] = { PHPDBG_COMMAND_D(print, "print something"), PHPDBG_COMMAND_D(break, "set breakpoint"), PHPDBG_COMMAND_D(back, "show backtrace"), + PHPDBG_COMMAND_D(list, "list specified line"), PHPDBG_COMMAND_D(clean, "clean the execution environment"), PHPDBG_COMMAND_D(clear, "clear breakpoints"), PHPDBG_COMMAND_D(help, "show help menu"),