summaryrefslogtreecommitdiff
path: root/sapi/phpdbg/phpdbg_utils.c
diff options
context:
space:
mode:
Diffstat (limited to 'sapi/phpdbg/phpdbg_utils.c')
-rw-r--r--sapi/phpdbg/phpdbg_utils.c152
1 files changed, 120 insertions, 32 deletions
diff --git a/sapi/phpdbg/phpdbg_utils.c b/sapi/phpdbg/phpdbg_utils.c
index 4cd8ce2782..36537cb67c 100644
--- a/sapi/phpdbg/phpdbg_utils.c
+++ b/sapi/phpdbg/phpdbg_utils.c
@@ -2,7 +2,7 @@
+----------------------------------------------------------------------+
| PHP Version 7 |
+----------------------------------------------------------------------+
- | Copyright (c) 1997-2015 The PHP Group |
+ | Copyright (c) 1997-2016 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 |
@@ -24,15 +24,21 @@
#include "phpdbg.h"
#include "phpdbg_opcode.h"
#include "phpdbg_utils.h"
+#include "ext/standard/php_string.h"
-#if defined(HAVE_SYS_IOCTL_H)
-# include "sys/ioctl.h"
-# ifndef GWINSZ_IN_SYS_IOCTL
-# include <termios.h>
-# endif
+/* FASYNC under Solaris */
+#ifdef HAVE_SYS_FILE_H
+# include <sys/file.h>
#endif
-ZEND_EXTERN_MODULE_GLOBALS(phpdbg);
+#ifdef HAVE_SYS_IOCTL_H
+# include "sys/ioctl.h"
+# ifndef GWINSZ_IN_SYS_IOCTL
+# include <termios.h>
+# endif
+#endif
+
+ZEND_EXTERN_MODULE_GLOBALS(phpdbg)
/* {{{ color structures */
const static phpdbg_color_t colors[] = {
@@ -165,16 +171,20 @@ PHPDBG_API const char *phpdbg_current_file(void) /* {{{ */
PHPDBG_API const zend_function *phpdbg_get_function(const char *fname, const char *cname) /* {{{ */
{
zend_function *func = NULL;
- zend_string *lfname = zend_string_alloc(strlen(fname), 0);
- memcpy(lfname->val, zend_str_tolower_dup(fname, lfname->len), lfname->len + 1);
+ zend_string *lfname = zend_string_init(fname, strlen(fname), 0);
+ zend_string *tmp = zend_string_tolower(lfname);
+ zend_string_release(lfname);
+ lfname = tmp;
if (cname) {
zend_class_entry *ce;
- zend_string *lcname = zend_string_alloc(strlen(cname), 0);
- memcpy(lcname->val, zend_str_tolower_dup(cname, lcname->len), lcname->len + 1);
+ zend_string *lcname = zend_string_init(cname, strlen(cname), 0);
+ tmp = zend_string_tolower(lcname);
+ zend_string_release(lcname);
+ lcname = tmp;
ce = zend_lookup_class(lcname);
- efree(lcname);
+ zend_string_release(lcname);
if (ce) {
func = zend_hash_find_ptr(&ce->function_table, lfname);
@@ -183,7 +193,7 @@ PHPDBG_API const zend_function *phpdbg_get_function(const char *fname, const cha
func = zend_hash_find_ptr(EG(function_table), lfname);
}
- efree(lfname);
+ zend_string_release(lfname);
return func;
} /* }}} */
@@ -297,16 +307,13 @@ PHPDBG_API const char *phpdbg_get_prompt(void) /* {{{ */
#ifndef HAVE_LIBEDIT
/* TODO: libedit doesn't seems to support coloured prompt */
if ((PHPDBG_G(flags) & PHPDBG_IS_COLOURED)) {
- asprintf(
- &PHPDBG_G(prompt)[1], "\033[%sm%s\033[0m ",
+ ZEND_IGNORE_VALUE(asprintf(&PHPDBG_G(prompt)[1], "\033[%sm%s\033[0m ",
PHPDBG_G(colors)[PHPDBG_COLOR_PROMPT]->code,
- PHPDBG_G(prompt)[0]);
+ PHPDBG_G(prompt)[0]));
} else
#endif
{
- asprintf(
- &PHPDBG_G(prompt)[1], "%s ",
- PHPDBG_G(prompt)[0]);
+ ZEND_IGNORE_VALUE(asprintf(&PHPDBG_G(prompt)[1], "%s ", PHPDBG_G(prompt)[0]));
}
return PHPDBG_G(prompt)[1];
@@ -344,8 +351,26 @@ PHPDBG_API int phpdbg_get_terminal_width(void) /* {{{ */
return columns;
} /* }}} */
+PHPDBG_API int phpdbg_get_terminal_height(void) /* {{{ */
+{
+ int lines;
+#ifdef _WIN32
+ CONSOLE_SCREEN_BUFFER_INFO csbi;
+
+ GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
+ lines = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
+#elif defined(HAVE_SYS_IOCTL_H) && defined(TIOCGWINSZ)
+ struct winsize w;
+
+ lines = ioctl(fileno(stdout), TIOCGWINSZ, &w) == 0 ? w.ws_row : 40;
+#else
+ lines = 40;
+#endif
+ return lines;
+} /* }}} */
+
PHPDBG_API void phpdbg_set_async_io(int fd) {
-#ifndef _WIN32
+#if !defined(_WIN32) && defined(FASYNC)
int flags;
fcntl(STDIN_FILENO, F_SETOWN, getpid());
flags = fcntl(STDIN_FILENO, F_GETFL);
@@ -456,14 +481,14 @@ PHPDBG_API int phpdbg_parse_variable_with_arg(char *input, size_t len, HashTable
char *name;
char *keyname = estrndup(last_index, index_len);
if (strkey) {
- key = strkey->val;
- keylen = strkey->len;
+ key = ZSTR_VAL(strkey);
+ keylen = ZSTR_LEN(strkey);
} else {
- keylen = spprintf(&key, 0, "%llu", numkey);
+ keylen = spprintf(&key, 0, ZEND_ULONG_FMT, numkey);
}
propkey = phpdbg_get_property_key(key);
name = emalloc(i + keylen + 2);
- namelen = sprintf(name, "%.*s%.*s%s", (int) i, input, keylen - (propkey - key), propkey, input[len - 1] == ']'?"]":"");
+ namelen = sprintf(name, "%.*s%.*s%s", (int) i, input, (int) (keylen - (propkey - key)), propkey, input[len - 1] == ']'?"]":"");
if (!strkey) {
efree(key);
}
@@ -572,7 +597,7 @@ static int phpdbg_xml_array_element_dump(zval *zv, zend_string *key, zend_ulong
phpdbg_try_access {
if (key) { /* string key */
- phpdbg_xml(" name=\"%.*s\"", key->len, key->val);
+ phpdbg_xml(" name=\"%.*s\"", (int) ZSTR_LEN(key), ZSTR_VAL(key));
} else { /* numeric key */
phpdbg_xml(" name=\"%ld\"", num);
}
@@ -606,7 +631,7 @@ static int phpdbg_xml_object_property_dump(zval *zv, zend_string *key, zend_ulon
phpdbg_xml(" class=\"%s\" protection=\"private\"", class_name);
}
} else {
- phpdbg_xml(" name=\"%.*s\" protection=\"public\"", key->len, key->val);
+ phpdbg_xml(" name=\"%.*s\" protection=\"public\"", (int) ZSTR_LEN(key), ZSTR_VAL(key));
}
} else { /* numeric key */
phpdbg_xml(" name=\"%ld\" protection=\"public\"", num);
@@ -658,7 +683,7 @@ PHPDBG_API void phpdbg_xml_var_dump(zval *zv) {
phpdbg_xml("<float refstatus=\"%s\" value=\"%.*G\" />", COMMON, (int) EG(precision), Z_DVAL_P(zv));
break;
case IS_STRING:
- phpdbg_xml("<string refstatus=\"%s\" length=\"%d\" value=\"%.*s\" />", COMMON, Z_STRLEN_P(zv), Z_STRLEN_P(zv), Z_STRVAL_P(zv));
+ phpdbg_xml("<string refstatus=\"%s\" length=\"%zd\" value=\"%.*s\" />", COMMON, Z_STRLEN_P(zv), (int) Z_STRLEN_P(zv), Z_STRVAL_P(zv));
break;
case IS_ARRAY:
myht = Z_ARRVAL_P(zv);
@@ -680,7 +705,7 @@ PHPDBG_API void phpdbg_xml_var_dump(zval *zv) {
}
class_name = Z_OBJ_HANDLER_P(zv, get_class_name)(Z_OBJ_P(zv));
- phpdbg_xml("<object refstatus=\"%s\" class=\"%.*s\" id=\"%d\" num=\"%d\">", COMMON, class_name->len, class_name->val, Z_OBJ_HANDLE_P(zv), myht ? zend_hash_num_elements(myht) : 0);
+ phpdbg_xml("<object refstatus=\"%s\" class=\"%.*s\" id=\"%d\" num=\"%d\">", COMMON, (int) ZSTR_LEN(class_name), ZSTR_VAL(class_name), Z_OBJ_HANDLE_P(zv), myht ? zend_hash_num_elements(myht) : 0);
zend_string_release(class_name);
element_dump_func = phpdbg_xml_object_property_dump;
@@ -704,7 +729,7 @@ head_done:
break;
case IS_RESOURCE: {
const char *type_name = zend_rsrc_list_get_rsrc_type(Z_RES_P(zv));
- phpdbg_xml("<resource refstatus=\"%s\" id=\"%pd\" type=\"%ld\" />", COMMON, Z_RES_P(zv)->handle, type_name ? type_name : "unknown");
+ phpdbg_xml("<resource refstatus=\"%s\" id=\"%pd\" type=\"%s\" />", COMMON, Z_RES_P(zv)->handle, type_name ? type_name : "unknown");
break;
}
default:
@@ -727,11 +752,11 @@ PHPDBG_API zend_bool phpdbg_check_caught_ex(zend_execute_data *execute_data, zen
op_num = op - op_array->opcodes;
- for (i = 0; i < op_array->last_try_catch && op_array->try_catch_array[i].try_op < op_num; i++) {
+ for (i = 0; i < op_array->last_try_catch && op_array->try_catch_array[i].try_op <= op_num; i++) {
uint32_t catch = op_array->try_catch_array[i].catch_op, finally = op_array->try_catch_array[i].finally_op;
if (op_num <= catch || op_num <= finally) {
- if (finally && finally < catch) {
- return 0;
+ if (finally) {
+ return 1;
}
do {
@@ -756,3 +781,66 @@ PHPDBG_API zend_bool phpdbg_check_caught_ex(zend_execute_data *execute_data, zen
return op->opcode == ZEND_CATCH;
}
+
+char *phpdbg_short_zval_print(zval *zv, int maxlen) /* {{{ */
+{
+ char *decode = NULL;
+
+ switch (Z_TYPE_P(zv)) {
+ case IS_UNDEF:
+ decode = estrdup("");
+ break;
+ case IS_NULL:
+ decode = estrdup("null");
+ break;
+ case IS_FALSE:
+ decode = estrdup("false");
+ break;
+ case IS_TRUE:
+ decode = estrdup("true");
+ break;
+ case IS_LONG:
+ spprintf(&decode, 0, ZEND_LONG_FMT, Z_LVAL_P(zv));
+ break;
+ case IS_DOUBLE:
+ spprintf(&decode, 0, "%.*G", 14, Z_DVAL_P(zv));
+ break;
+ case IS_STRING: {
+ int i;
+ zend_string *str = php_addcslashes(Z_STR_P(zv), 0, "\\\"\n\t\0", 5);
+ for (i = 0; i < ZSTR_LEN(str); i++) {
+ if (ZSTR_VAL(str)[i] < 32) {
+ ZSTR_VAL(str)[i] = ' ';
+ }
+ }
+ spprintf(&decode, 0, "\"%.*s\"%c",
+ ZSTR_LEN(str) <= maxlen - 2 ? (int) ZSTR_LEN(str) : (maxlen - 3),
+ ZSTR_VAL(str), ZSTR_LEN(str) <= maxlen - 2 ? 0 : '+');
+ zend_string_release(str);
+ } break;
+ case IS_RESOURCE:
+ spprintf(&decode, 0, "Rsrc #%d", Z_RES_HANDLE_P(zv));
+ break;
+ case IS_ARRAY:
+ spprintf(&decode, 0, "array(%d)", zend_hash_num_elements(Z_ARR_P(zv)));
+ break;
+ case IS_OBJECT: {
+ zend_string *str = Z_OBJCE_P(zv)->name;
+ spprintf(&decode, 0, "%.*s%c",
+ ZSTR_LEN(str) <= maxlen ? (int) ZSTR_LEN(str) : maxlen - 1,
+ ZSTR_VAL(str), ZSTR_LEN(str) <= maxlen ? 0 : '+');
+ break;
+ }
+ case IS_CONSTANT:
+ decode = estrdup("<constant>");
+ break;
+ case IS_CONSTANT_AST:
+ decode = estrdup("<ast>");
+ break;
+ default:
+ spprintf(&decode, 0, "unknown type: %d", Z_TYPE_P(zv));
+ break;
+ }
+
+ return decode;
+} /* }}} */