]> granicus.if.org Git - php/commitdiff
- Added initial code for list command
authorFelipe Pena <felipensp@gmail.com>
Mon, 11 Nov 2013 23:40:03 +0000 (21:40 -0200)
committerFelipe Pena <felipensp@gmail.com>
Mon, 11 Nov 2013 23:40:03 +0000 (21:40 -0200)
config.m4
phpdbg_help.c
phpdbg_help.h
phpdbg_list.c [new file with mode: 0644]
phpdbg_list.h [new file with mode: 0644]
phpdbg_prompt.c

index bb9719015e527da979180b121612a3887c10b970..1a99db0f24d886697c3224a33fde7feef969d022 100644 (file)
--- 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
index ef8b6208b95e69a614984f9da40cb5c2aed430b4..d91625e09e1743200ec451437e1757073d48ccc3 100644 (file)
@@ -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;
+} /* }}} */
index 449f4d1c5860ec020681e3560bf15e58a5246766..9e187a8b395ae99bbd486a468dee8fc07d5b7783 100644 (file)
@@ -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 (file)
index 0000000..9102a79
--- /dev/null
@@ -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 <felipe@php.net>                                |
+   | Authors: Joe Watkins <joe.watkins@live.co.uk>                        |
+   +----------------------------------------------------------------------+
+*/
+
+#include <stdio.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <sys/mman.h>
+#include <unistd.h>
+#include <fcntl.h>
+#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 (file)
index 0000000..4531a65
--- /dev/null
@@ -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 <felipe@php.net>                                |
+   | Authors: Joe Watkins <joe.watkins@live.co.uk>                        |
+   +----------------------------------------------------------------------+
+*/
+
+#ifndef PHPDBG_LIST_H
+#define PHPDBG_LIST_H
+
+void phpdbg_list_file(const char*, long, long);
+
+#endif /* PHPDBG_LIST_H */
index 306e929c5a1c78fbb6afe2ed28ca9a34d43a0bbc..ab04a68eedab598d772ef04c977b4e6e7561f2fc 100644 (file)
@@ -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"),