Skip to content

Commit

Permalink
Merge branch 'PHP-7.0' into PHP-7.1
Browse files Browse the repository at this point in the history
(cherry picked from commit 37ae5f3)
  • Loading branch information
bwoebi authored and weltling committed Oct 13, 2016
1 parent a8a11b6 commit 11e7447
Show file tree
Hide file tree
Showing 11 changed files with 304 additions and 46 deletions.
2 changes: 1 addition & 1 deletion run-tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@
$phpdbg = $cwd . '/sapi/phpdbg/phpdbg';

if (file_exists($phpdbg)) {
putenv("TEST_PHP_CGI_EXECUTABLE=$phpdbg");
putenv("TEST_PHPDBG_EXECUTABLE=$phpdbg");
} else {
$phpdbg = null;
}
Expand Down
80 changes: 68 additions & 12 deletions sapi/phpdbg/phpdbg.c
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ static void php_phpdbg_destroy_file_source(zval *data) /* {{{ */
if (source->buf) {
efree(source->buf);
}
efree(source->filename);
efree(source);
} /* }}} */

Expand Down Expand Up @@ -373,7 +374,7 @@ static PHP_FUNCTION(phpdbg_break_file)
return;
}

phpdbg_set_breakpoint_file(file, line);
phpdbg_set_breakpoint_file(file, 0, line);
} /* }}} */

/* {{{ proto void phpdbg_break_method(string class, string method) */
Expand Down Expand Up @@ -1076,6 +1077,7 @@ const opt_struct OPTIONS[] = { /* {{{ */
{'r', 0, "run"},
{'e', 0, "generate ext_stmt opcodes"},
{'E', 0, "step-through-eval"},
{'s', 1, "script from stdin"},
{'S', 1, "sapi-name"},
#ifndef _WIN32
{'l', 1, "listen"},
Expand Down Expand Up @@ -1374,6 +1376,8 @@ int main(int argc, char **argv) /* {{{ */
zend_bool ext_stmt = 0;
zend_bool is_exit;
int exit_status;
char *read_from_stdin = NULL;
zend_string *backup_phpdbg_compile = NULL;

#ifndef _WIN32
struct sigaction sigio_struct;
Expand Down Expand Up @@ -1482,6 +1486,12 @@ int main(int argc, char **argv) /* {{{ */

/* begin phpdbg options */

case 's': { /* read script from stdin */
if (settings == NULL) {
read_from_stdin = strdup(php_optarg);
}
} break;

case 'S': { /* set SAPI name */
sapi_name = strdup(php_optarg);
} break;
Expand Down Expand Up @@ -1593,8 +1603,10 @@ int main(int argc, char **argv) /* {{{ */
php_optarg = NULL;
}

quit_immediately = phpdbg_startup_run > 1;

/* set exec if present on command line */
if (argc > php_optind && (strcmp(argv[php_optind-1], "--") != SUCCESS)) {
if (!read_from_stdin && argc > php_optind && (strcmp(argv[php_optind-1], "--") != SUCCESS)) {
if (!exec && strlen(argv[php_optind])) {
exec = strdup(argv[php_optind]);
}
Expand Down Expand Up @@ -1849,13 +1861,6 @@ int main(int argc, char **argv) /* {{{ */
if (init_file) {
phpdbg_init(init_file, init_file_len, init_file_default);
}
if (bp_tmp) {
PHPDBG_G(flags) |= PHPDBG_DISCARD_OUTPUT;
phpdbg_string_init(bp_tmp);
free(bp_tmp);
bp_tmp = NULL;
PHPDBG_G(flags) &= ~PHPDBG_DISCARD_OUTPUT;
}
} zend_end_try();
PHPDBG_G(flags) &= ~PHPDBG_IS_INITIALIZING;

Expand All @@ -1865,18 +1870,55 @@ int main(int argc, char **argv) /* {{{ */
}

/* auto compile */
if (PHPDBG_G(exec)) {
if (read_from_stdin) {
if (!read_from_stdin[0]) {
if (!quit_immediately) {
phpdbg_error("error", "", "Impossible to not specify a stdin delimiter without -rr");
PHPDBG_G(flags) |= PHPDBG_IS_QUITTING;
goto phpdbg_out;
}
}
if (show_banner || read_from_stdin[0]) {
phpdbg_notice("stdin", "delimiter=\"%s\"", "Reading input from stdin; put '%s' followed by a newline on an own line after code to end input", read_from_stdin);
}

if (phpdbg_startup_run > 0) {
PHPDBG_G(flags) |= PHPDBG_DISCARD_OUTPUT;
}

zend_try {
phpdbg_param_t cmd;
cmd.str = read_from_stdin;
cmd.len = strlen(read_from_stdin);
PHPDBG_COMMAND_HANDLER(stdin)(&cmd);
} zend_end_try();

PHPDBG_G(flags) &= ~PHPDBG_DISCARD_OUTPUT;
} else if (PHPDBG_G(exec)) {
if (settings || phpdbg_startup_run > 0) {
PHPDBG_G(flags) |= PHPDBG_DISCARD_OUTPUT;
}

zend_try {
phpdbg_compile();
if (backup_phpdbg_compile) {
phpdbg_compile_stdin(backup_phpdbg_compile);
} else {
phpdbg_compile();
}
} zend_end_try();
backup_phpdbg_compile = NULL;

PHPDBG_G(flags) &= ~PHPDBG_DISCARD_OUTPUT;
}

if (bp_tmp) {
PHPDBG_G(flags) |= PHPDBG_DISCARD_OUTPUT | PHPDBG_IS_INITIALIZING;
phpdbg_string_init(bp_tmp);
free(bp_tmp);
bp_tmp = NULL;
PHPDBG_G(flags) &= ~PHPDBG_DISCARD_OUTPUT & ~PHPDBG_IS_INITIALIZING;
}

if (settings == (void *) 0x1) {
if (PHPDBG_G(ops)) {
phpdbg_print_opcodes(print_opline_func);
Expand All @@ -1898,7 +1940,6 @@ int main(int argc, char **argv) /* {{{ */
do {
zend_try {
if (phpdbg_startup_run) {
quit_immediately = phpdbg_startup_run > 1;
phpdbg_startup_run = 0;
if (quit_immediately) {
PHPDBG_G(flags) = (PHPDBG_G(flags) & ~PHPDBG_HAS_PAGINATION) | PHPDBG_IS_INTERACTIVE | PHPDBG_PREVENT_INTERACTIVE;
Expand Down Expand Up @@ -2073,6 +2114,12 @@ int main(int argc, char **argv) /* {{{ */
wrapper->wops->stream_opener = PHPDBG_G(orig_url_wrap_php);
}

if (PHPDBG_G(exec) && !memcmp("-", PHPDBG_G(exec), 2)) { /* i.e. execution context has been read from stdin - back it up */
phpdbg_file_source *data = zend_hash_str_find_ptr(&PHPDBG_G(file_sources), PHPDBG_G(exec), PHPDBG_G(exec_len));
backup_phpdbg_compile = zend_string_alloc(data->len + 2, 1);
sprintf(ZSTR_VAL(backup_phpdbg_compile), "?>%.*s", (int) data->len, data->buf);
}

zend_try {
php_module_shutdown();
} zend_end_try();
Expand All @@ -2097,6 +2144,11 @@ int main(int argc, char **argv) /* {{{ */
free(sapi_name);
}

if (read_from_stdin) {
free(read_from_stdin);
read_from_stdin = NULL;
}

#ifdef ZTS
tsrm_shutdown();
#endif
Expand All @@ -2108,6 +2160,10 @@ int main(int argc, char **argv) /* {{{ */
goto phpdbg_main;
}

if (backup_phpdbg_compile) {
zend_string_free(backup_phpdbg_compile);
}

#ifndef _WIN32
if (address) {
free(address);
Expand Down
23 changes: 8 additions & 15 deletions sapi/phpdbg/phpdbg_bp.c
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ PHPDBG_API void phpdbg_export_breakpoints_to_string(char **str) /* {{{ */
}
} /* }}} */

PHPDBG_API void phpdbg_set_breakpoint_file(const char *path, long line_num) /* {{{ */
PHPDBG_API void phpdbg_set_breakpoint_file(const char *path, size_t path_len, long line_num) /* {{{ */
{
php_stream_statbuf ssb;
char realpath[MAXPATHLEN];
Expand All @@ -240,10 +240,11 @@ PHPDBG_API void phpdbg_set_breakpoint_file(const char *path, long line_num) /* {

HashTable *broken, *file_breaks = &PHPDBG_G(bp)[PHPDBG_BREAK_FILE];
phpdbg_breakfile_t new_break;
size_t path_len = 0L;

if (VCWD_REALPATH(path, realpath)) {
path = realpath;
if (!path_len) {
if (VCWD_REALPATH(path, realpath)) {
path = realpath;
}
}
path_len = strlen(path);

Expand Down Expand Up @@ -886,21 +887,13 @@ static inline phpdbg_breakbase_t *phpdbg_find_breakpoint_file(zend_op_array *op_
{
HashTable *breaks;
phpdbg_breakbase_t *brake;
size_t path_len;
char realpath[MAXPATHLEN];
const char *path = ZSTR_VAL(op_array->filename);

if (VCWD_REALPATH(path, realpath)) {
path = realpath;
}

path_len = strlen(path);

#if 0
phpdbg_debug("Op at: %.*s %d\n", path_len, path, (*EG(opline_ptr))->lineno);
phpdbg_debug("Op at: %.*s %d\n", ZSTR_LEN(op_array->filename), ZSTR_VAL(op_array->filename), (*EG(opline_ptr))->lineno);
#endif

if (!(breaks = zend_hash_str_find_ptr(&PHPDBG_G(bp)[PHPDBG_BREAK_FILE], path, path_len))) {
/* NOTE: realpath resolution should have happened at compile time - no reason to do it here again */
if (!(breaks = zend_hash_find_ptr(&PHPDBG_G(bp)[PHPDBG_BREAK_FILE], op_array->filename))) {
return NULL;
}

Expand Down
2 changes: 1 addition & 1 deletion sapi/phpdbg/phpdbg_bp.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ PHPDBG_API HashTable *phpdbg_resolve_pending_file_break_ex(const char *file, uin
PHPDBG_API void phpdbg_resolve_pending_file_break(const char *file); /* }}} */

/* {{{ Breakpoint Creation API */
PHPDBG_API void phpdbg_set_breakpoint_file(const char* filename, long lineno);
PHPDBG_API void phpdbg_set_breakpoint_file(const char* filename, size_t path_len, long lineno);
PHPDBG_API void phpdbg_set_breakpoint_symbol(const char* func_name, size_t func_name_len);
PHPDBG_API void phpdbg_set_breakpoint_method(const char* class_name, const char* func_name);
PHPDBG_API void phpdbg_set_breakpoint_opcode(const char* opname, size_t opname_len);
Expand Down
24 changes: 24 additions & 0 deletions sapi/phpdbg/phpdbg_help.c
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@ phpdbg_help_text_t phpdbg_help_text[] = {

"**Starting and Stopping Execution**" CR
" **exec** set execution context" CR
" **stdin** set executing script from stdin" CR
" **run** attempt execution" CR
" **step** continue execution until other line is reached" CR
" **continue** continue execution" CR
Expand Down Expand Up @@ -387,6 +388,7 @@ phpdbg_help_text_t phpdbg_help_text[] = {
" **-rr** Run execution context and quit after execution (not respecting breakpoints)" CR
" **-e** Generate extended information for debugger/profiler" CR
" **-E** Enable step through eval, careful!" CR
" **-s** **-s=**, **-s**=foo Read code to execute from stdin with an optional delimiter" CR
" **-S** **-S**cli Override SAPI name, careful!" CR
" **-l** **-l**4000 Setup remote console ports" CR
" **-a** **-a**192.168.0.3 Setup remote console bind address" CR
Expand All @@ -397,6 +399,13 @@ phpdbg_help_text_t phpdbg_help_text[] = {
" **--** **--** arg1 arg2 Use to delimit phpdbg arguments and php $argv; append any $argv "
"argument after it" CR CR

"**Reading from stdin**" CR CR

"The **-s** option allows inputting a script to execute directly from stdin. The given delimiter "
"(\"foo\" in the example) needs to be specified at the end of the input on its own line, followed by "
"a line break. If **-rr** has been specified, it is allowed to omit the delimiter (**-s=**) and "
"it will read until EOF. See also the help entry for the **stdin** command." CR CR

"**Remote Console Mode**" CR CR

"This mode is enabled by specifying the **-a** option. Phpdbg will bind only to the loopback "
Expand Down Expand Up @@ -635,6 +644,21 @@ phpdbg_help_text_t phpdbg_help_text[] = {
" Set the execution context to **/tmp/script.php**"
},

{"stdin",
"The **stdin** command takes a string serving as delimiter. It will then read all the input from "
"stdin until encountering the given delimiter on a standalone line. It can also be passed at "
"startup using the **-s=** command line option (the delimiter then is optional if **-rr** is "
"also passed - in that case it will just read until EOF)." CR
"This input will be then compiled as PHP code and set as execution context." CR CR

"**Example**" CR CR

" $P stdin foo" CR
" <?php" CR
" echo \"Hello, world!\\n\";" CR
" foo"
},

//*********** Does F skip any breakpoints lower stack frames or only the current??
{"finish",
"The **finish** command causes control to be passed back to the vm, continuing execution. Any "
Expand Down
34 changes: 22 additions & 12 deletions sapi/phpdbg/phpdbg_list.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,15 @@ PHPDBG_LIST(lines) /* {{{ */
} break;

case FILE_PARAM: {
zend_string *file = zend_string_init(param->file.name, strlen(param->file.name), 0);
zend_string *file;
char resolved_path_buf[MAXPATHLEN];
const char *abspath = param->file.name;
if (VCWD_REALPATH(abspath, resolved_path_buf)) {
abspath = resolved_path_buf;
}
file = zend_string_init(abspath, strlen(abspath), 0);
phpdbg_list_file(file, param->file.line, 0, 0);
efree(file);
zend_string_release(file);
} break;

phpdbg_default_switch_case();
Expand Down Expand Up @@ -127,16 +133,8 @@ void phpdbg_list_file(zend_string *filename, uint count, int offset, uint highli
{
uint line, lastline;
phpdbg_file_source *data;
char resolved_path_buf[MAXPATHLEN];
const char *abspath;

if (VCWD_REALPATH(ZSTR_VAL(filename), resolved_path_buf)) {
abspath = resolved_path_buf;
} else {
abspath = ZSTR_VAL(filename);
}

if (!(data = zend_hash_str_find_ptr(&PHPDBG_G(file_sources), abspath, strlen(abspath)))) {
if (!(data = zend_hash_find_ptr(&PHPDBG_G(file_sources), filename))) {
phpdbg_error("list", "type=\"unknownfile\"", "Could not find information about included file...");
return;
}
Expand Down Expand Up @@ -288,6 +286,7 @@ zend_op_array *phpdbg_compile_file(zend_file_handle *file, int type) {
return NULL;
}

dataptr->filename = estrdup(dataptr->filename);
dataptr = erealloc(dataptr, sizeof(phpdbg_file_source) + sizeof(uint) * line);
zend_hash_str_add_ptr(&PHPDBG_G(file_sources), filename, strlen(filename), dataptr);
phpdbg_resolve_pending_file_break(filename);
Expand All @@ -306,6 +305,17 @@ zend_op_array *phpdbg_init_compile_file(zend_file_handle *file, int type) {

if (VCWD_REALPATH(filename, resolved_path_buf)) {
filename = resolved_path_buf;

if (file->opened_path) {
zend_string_release(file->opened_path);
file->opened_path = zend_string_init(filename, strlen(filename), 0);
} else {
if (file->free_filename) {
efree((char *) file->filename);
}
file->free_filename = 1;
file->filename = estrdup(filename);
}
}

op_array = PHPDBG_G(init_compile_file)(file, type);
Expand Down Expand Up @@ -356,7 +366,7 @@ zend_op_array *phpdbg_compile_string(zval *source_string, char *filename) {
return NULL;
}

fake_name = strpprintf(0, "%s\0%p", filename, op_array->opcodes);
fake_name = strpprintf(0, "%s%c%p", filename, 0, op_array->opcodes);

dataptr = erealloc(dataptr, sizeof(phpdbg_file_source) + sizeof(uint) * line);
zend_hash_add_ptr(&PHPDBG_G(file_sources), fake_name, dataptr);
Expand Down
Loading

0 comments on commit 11e7447

Please sign in to comment.