xref: /PHP-7.0/sapi/phpdbg/phpdbg_utils.h (revision 478f119a)
1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 7                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 1997-2017 The PHP Group                                |
6    +----------------------------------------------------------------------+
7    | This source file is subject to version 3.01 of the PHP license,      |
8    | that is bundled with this package in the file LICENSE, and is        |
9    | available through the world-wide-web at the following url:           |
10    | http://www.php.net/license/3_01.txt                                  |
11    | If you did not receive a copy of the PHP license and are unable to   |
12    | obtain it through the world-wide-web, please send a note to          |
13    | license@php.net so we can mail you a copy immediately.               |
14    +----------------------------------------------------------------------+
15    | Authors: Felipe Pena <felipe@php.net>                                |
16    | Authors: Joe Watkins <joe.watkins@live.co.uk>                        |
17    | Authors: Bob Weinand <bwoebi@php.net>                                |
18    +----------------------------------------------------------------------+
19 */
20 
21 #ifndef PHPDBG_UTILS_H
22 #define PHPDBG_UTILS_H
23 
24 /**
25  * Input scan functions
26  */
27 PHPDBG_API int phpdbg_is_numeric(const char*);
28 PHPDBG_API int phpdbg_is_empty(const char*);
29 PHPDBG_API int phpdbg_is_addr(const char*);
30 PHPDBG_API int phpdbg_is_class_method(const char*, size_t, char**, char**);
31 PHPDBG_API const char *phpdbg_current_file(void);
32 PHPDBG_API char *phpdbg_resolve_path(const char*);
33 PHPDBG_API char *phpdbg_trim(const char*, size_t, size_t*);
34 PHPDBG_API const zend_function *phpdbg_get_function(const char*, const char*);
35 
36 /* {{{ Color Management */
37 #define PHPDBG_COLOR_LEN 12
38 #define PHPDBG_COLOR_D(color, code) \
39 	{color, sizeof(color)-1, code}
40 #define PHPDBG_COLOR_END \
41 	{NULL, 0L, {0}}
42 #define PHPDBG_ELEMENT_LEN 3
43 #define PHPDBG_ELEMENT_D(name, id) \
44 	{name, sizeof(name)-1, id}
45 #define PHPDBG_ELEMENT_END \
46 	{NULL, 0L, 0}
47 
48 #define PHPDBG_COLOR_INVALID	-1
49 #define PHPDBG_COLOR_PROMPT 	 0
50 #define PHPDBG_COLOR_ERROR		 1
51 #define PHPDBG_COLOR_NOTICE		 2
52 #define PHPDBG_COLORS			 3
53 
54 typedef struct _phpdbg_color_t {
55 	char       *name;
56 	size_t      name_length;
57 	const char  code[PHPDBG_COLOR_LEN];
58 } phpdbg_color_t;
59 
60 typedef struct _phpdbg_element_t {
61 	char		*name;
62 	size_t		name_length;
63 	int			id;
64 } phpdbg_element_t;
65 
66 PHPDBG_API const phpdbg_color_t *phpdbg_get_color(const char *name, size_t name_length);
67 PHPDBG_API void phpdbg_set_color(int element, const phpdbg_color_t *color);
68 PHPDBG_API void phpdbg_set_color_ex(int element, const char *name, size_t name_length);
69 PHPDBG_API const phpdbg_color_t *phpdbg_get_colors(void);
70 PHPDBG_API int phpdbg_get_element(const char *name, size_t len); /* }}} */
71 
72 /* {{{ Prompt Management */
73 PHPDBG_API void phpdbg_set_prompt(const char*);
74 PHPDBG_API const char *phpdbg_get_prompt(void); /* }}} */
75 
76 /* {{{ Console size */
77 PHPDBG_API int phpdbg_get_terminal_width(void);
78 PHPDBG_API int phpdbg_get_terminal_height(void); /* }}} */
79 
80 PHPDBG_API void phpdbg_set_async_io(int fd);
81 
82 int phpdbg_rebuild_symtable(void);
83 
84 int phpdbg_safe_class_lookup(const char *name, int name_length, zend_class_entry **ce);
85 
86 char *phpdbg_get_property_key(char *key);
87 
88 typedef int (*phpdbg_parse_var_func)(char *name, size_t len, char *keyname, size_t keylen, HashTable *parent, zval *zv);
89 typedef int (*phpdbg_parse_var_with_arg_func)(char *name, size_t len, char *keyname, size_t keylen, HashTable *parent, zval *zv, void *arg);
90 
91 PHPDBG_API int phpdbg_parse_variable(char *input, size_t len, HashTable *parent, size_t i, phpdbg_parse_var_func callback, zend_bool silent);
92 PHPDBG_API int phpdbg_parse_variable_with_arg(char *input, size_t len, HashTable *parent, size_t i, phpdbg_parse_var_with_arg_func callback, phpdbg_parse_var_with_arg_func step_cb, zend_bool silent, void *arg);
93 
94 int phpdbg_is_auto_global(char *name, int len);
95 
96 PHPDBG_API void phpdbg_xml_var_dump(zval *zv);
97 
98 char *phpdbg_short_zval_print(zval *zv, int maxlen);
99 
100 PHPDBG_API zend_bool phpdbg_check_caught_ex(zend_execute_data *ex, zend_object *exception);
101 
phpdbg_user_execute_data(zend_execute_data * ex)102 static zend_always_inline zend_execute_data *phpdbg_user_execute_data(zend_execute_data *ex) {
103 	while (!ex->func || !ZEND_USER_CODE(ex->func->common.type)) {
104 		ex = ex->prev_execute_data;
105 		ZEND_ASSERT(ex);
106 	}
107 	return ex;
108 }
109 
110 #ifdef ZTS
111 #define PHPDBG_OUTPUT_BACKUP_DEFINES() \
112 	zend_output_globals *output_globals_ptr; \
113 	zend_output_globals original_output_globals; \
114 	output_globals_ptr = (zend_output_globals *) (*((void ***) tsrm_get_ls_cache()))[TSRM_UNSHUFFLE_RSRC_ID(output_globals_id)];
115 #else
116 #define PHPDBG_OUTPUT_BACKUP_DEFINES() \
117 	zend_output_globals *output_globals_ptr; \
118 	zend_output_globals original_output_globals; \
119 	output_globals_ptr = &output_globals;
120 #endif
121 
122 #define PHPDBG_OUTPUT_BACKUP_SWAP() \
123 	original_output_globals = *output_globals_ptr; \
124 	memset(output_globals_ptr, 0, sizeof(zend_output_globals)); \
125 	php_output_activate();
126 
127 #define PHPDBG_OUTPUT_BACKUP() \
128 	PHPDBG_OUTPUT_BACKUP_DEFINES() \
129 	PHPDBG_OUTPUT_BACKUP_SWAP()
130 
131 #define PHPDBG_OUTPUT_BACKUP_RESTORE() \
132 	php_output_deactivate(); \
133 	*output_globals_ptr = original_output_globals;
134 
135 #endif /* PHPDBG_UTILS_H */
136