xref: /PHP-7.0/sapi/phpdbg/phpdbg.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_H
22 #define PHPDBG_H
23 
24 #ifdef PHP_WIN32
25 # define PHPDBG_API __declspec(dllexport)
26 #elif defined(__GNUC__) && __GNUC__ >= 4
27 # define PHPDBG_API __attribute__ ((visibility("default")))
28 #else
29 # define PHPDBG_API
30 #endif
31 
32 #ifndef PHP_WIN32
33 #	include <stdint.h>
34 #	include <stddef.h>
35 #else
36 #	include "main/php_stdint.h"
37 #endif
38 #include "php.h"
39 #include "php_globals.h"
40 #include "php_variables.h"
41 #include "php_getopt.h"
42 #include "zend_builtin_functions.h"
43 #include "zend_extensions.h"
44 #include "zend_modules.h"
45 #include "zend_globals.h"
46 #include "zend_ini_scanner.h"
47 #include "zend_stream.h"
48 #ifndef _WIN32
49 #	include "zend_signal.h"
50 #endif
51 #include "SAPI.h"
52 #include <fcntl.h>
53 #include <sys/types.h>
54 #if defined(_WIN32) && !defined(__MINGW32__)
55 #	include <windows.h>
56 #	include "config.w32.h"
57 #	undef  strcasecmp
58 #	undef  strncasecmp
59 #	define strcasecmp _stricmp
60 #	define strncasecmp _strnicmp
61 #else
62 #	include "php_config.h"
63 #endif
64 #ifndef O_BINARY
65 #	define O_BINARY 0
66 #endif
67 #include "php_main.h"
68 
69 #ifdef ZTS
70 # include "TSRM.h"
71 #endif
72 
73 #undef zend_hash_str_add
74 #ifdef PHP_WIN32
75 #define zend_hash_str_add(...) \
76 	_zend_hash_str_add(__VA_ARGS__ ZEND_FILE_LINE_CC)
77 #else
78 #define zend_hash_str_add_tmp(ht, key, len, pData) \
79 	_zend_hash_str_add(ht, key, len, pData ZEND_FILE_LINE_CC)
80 #define zend_hash_str_add(...) zend_hash_str_add_tmp(__VA_ARGS__)
81 #endif
82 
83 #ifdef HAVE_LIBREADLINE
84 #	include <readline/readline.h>
85 #	include <readline/history.h>
86 #endif
87 #ifdef HAVE_LIBEDIT
88 #	include <editline/readline.h>
89 #endif
90 
91 /* {{{ remote console headers */
92 #ifndef _WIN32
93 #	include <sys/socket.h>
94 #	include <sys/un.h>
95 #	include <sys/select.h>
96 #	include <sys/types.h>
97 #	include <netdb.h>
98 #endif /* }}} */
99 
100 /* {{{ strings */
101 #define PHPDBG_NAME "phpdbg"
102 #define PHPDBG_AUTHORS "Felipe Pena, Joe Watkins and Bob Weinand" /* Ordered by last name */
103 #define PHPDBG_ISSUES "http://bugs.php.net/report.php"
104 #define PHPDBG_VERSION "0.5.0"
105 #define PHPDBG_INIT_FILENAME ".phpdbginit"
106 #define PHPDBG_DEFAULT_PROMPT "prompt>"
107 /* }}} */
108 
109 /* Hey, apple. One shouldn't define *functions* from the standard C library as marcos. */
110 #ifdef memcpy
111 #define memcpy_tmp(...) memcpy(__VA_ARGS__)
112 #undef memcpy
113 #define memcpy(...) memcpy_tmp(__VA_ARGS__)
114 #endif
115 
116 #if !defined(PHPDBG_WEBDATA_TRANSFER_H) && !defined(PHPDBG_WEBHELPER_H)
117 
118 #ifdef ZTS
119 # define PHPDBG_G(v) TSRMG(phpdbg_globals_id, zend_phpdbg_globals *, v)
120 #else
121 # define PHPDBG_G(v) (phpdbg_globals.v)
122 #endif
123 
124 #include "phpdbg_sigsafe.h"
125 #include "phpdbg_out.h"
126 #include "phpdbg_lexer.h"
127 #include "phpdbg_cmd.h"
128 #include "phpdbg_utils.h"
129 #include "phpdbg_btree.h"
130 #include "phpdbg_watch.h"
131 #include "phpdbg_bp.h"
132 #include "phpdbg_opcode.h"
133 #ifdef PHP_WIN32
134 # include "phpdbg_sigio_win32.h"
135 #endif
136 
137 int phpdbg_do_parse(phpdbg_param_t *stack, char *input);
138 
139 #define PHPDBG_NEXT   2
140 #define PHPDBG_UNTIL  3
141 #define PHPDBG_FINISH 4
142 #define PHPDBG_LEAVE  5
143 
144 /*
145  BEGIN: DO NOT CHANGE DO NOT CHANGE DO NOT CHANGE
146 */
147 
148 /* {{{ flags */
149 #define PHPDBG_HAS_FILE_BP            (1ULL<<1)
150 #define PHPDBG_HAS_PENDING_FILE_BP    (1ULL<<2)
151 #define PHPDBG_HAS_SYM_BP             (1ULL<<3)
152 #define PHPDBG_HAS_OPLINE_BP          (1ULL<<4)
153 #define PHPDBG_HAS_METHOD_BP          (1ULL<<5)
154 #define PHPDBG_HAS_COND_BP            (1ULL<<6)
155 #define PHPDBG_HAS_OPCODE_BP          (1ULL<<7)
156 #define PHPDBG_HAS_FUNCTION_OPLINE_BP (1ULL<<8)
157 #define PHPDBG_HAS_METHOD_OPLINE_BP   (1ULL<<9)
158 #define PHPDBG_HAS_FILE_OPLINE_BP     (1ULL<<10) /* }}} */
159 
160 /*
161  END: DO NOT CHANGE DO NOT CHANGE DO NOT CHANGE
162 */
163 
164 #define PHPDBG_IN_COND_BP             (1ULL<<11)
165 #define PHPDBG_IN_EVAL                (1ULL<<12)
166 
167 #define PHPDBG_IS_STEPPING            (1ULL<<13)
168 #define PHPDBG_STEP_OPCODE            (1ULL<<14)
169 #define PHPDBG_IS_QUIET               (1ULL<<15)
170 #define PHPDBG_IS_QUITTING            (1ULL<<16)
171 #define PHPDBG_IS_COLOURED            (1ULL<<17)
172 #define PHPDBG_IS_CLEANING            (1ULL<<18)
173 #define PHPDBG_IS_RUNNING             (1ULL<<19)
174 
175 #define PHPDBG_IN_UNTIL               (1ULL<<20)
176 #define PHPDBG_IN_FINISH              (1ULL<<21)
177 #define PHPDBG_IN_LEAVE               (1ULL<<22)
178 
179 #define PHPDBG_IS_REGISTERED          (1ULL<<23)
180 #define PHPDBG_IS_STEPONEVAL          (1ULL<<24)
181 #define PHPDBG_IS_INITIALIZING        (1ULL<<25)
182 #define PHPDBG_IS_SIGNALED            (1ULL<<26)
183 #define PHPDBG_IS_INTERACTIVE         (1ULL<<27)
184 #define PHPDBG_PREVENT_INTERACTIVE    (1ULL<<28)
185 #define PHPDBG_IS_BP_ENABLED          (1ULL<<29)
186 #define PHPDBG_IS_REMOTE              (1ULL<<30)
187 #define PHPDBG_IS_DISCONNECTED        (1ULL<<31)
188 #define PHPDBG_WRITE_XML              (1ULL<<32)
189 
190 #define PHPDBG_SHOW_REFCOUNTS         (1ULL<<33)
191 
192 #define PHPDBG_IN_SIGNAL_HANDLER      (1ULL<<34)
193 
194 #define PHPDBG_DISCARD_OUTPUT         (1ULL<<35)
195 
196 #define PHPDBG_HAS_PAGINATION         (1ULL<<36)
197 
198 #define PHPDBG_SEEK_MASK              (PHPDBG_IN_UNTIL | PHPDBG_IN_FINISH | PHPDBG_IN_LEAVE)
199 #define PHPDBG_BP_RESOLVE_MASK	      (PHPDBG_HAS_FUNCTION_OPLINE_BP | PHPDBG_HAS_METHOD_OPLINE_BP | PHPDBG_HAS_FILE_OPLINE_BP)
200 #define PHPDBG_BP_MASK                (PHPDBG_HAS_FILE_BP | PHPDBG_HAS_SYM_BP | PHPDBG_HAS_METHOD_BP | PHPDBG_HAS_OPLINE_BP | PHPDBG_HAS_COND_BP | PHPDBG_HAS_OPCODE_BP | PHPDBG_HAS_FUNCTION_OPLINE_BP | PHPDBG_HAS_METHOD_OPLINE_BP | PHPDBG_HAS_FILE_OPLINE_BP)
201 #define PHPDBG_IS_STOPPING            (PHPDBG_IS_QUITTING | PHPDBG_IS_CLEANING)
202 
203 #define PHPDBG_PRESERVE_FLAGS_MASK    (PHPDBG_SHOW_REFCOUNTS | PHPDBG_IS_STEPONEVAL | PHPDBG_IS_BP_ENABLED | PHPDBG_STEP_OPCODE | PHPDBG_IS_QUIET | PHPDBG_IS_COLOURED | PHPDBG_IS_REMOTE | PHPDBG_WRITE_XML | PHPDBG_IS_DISCONNECTED | PHPDBG_HAS_PAGINATION)
204 
205 #ifndef _WIN32
206 #	define PHPDBG_DEFAULT_FLAGS (PHPDBG_IS_QUIET | PHPDBG_IS_COLOURED | PHPDBG_IS_BP_ENABLED | PHPDBG_HAS_PAGINATION)
207 #else
208 #	define PHPDBG_DEFAULT_FLAGS (PHPDBG_IS_QUIET | PHPDBG_IS_BP_ENABLED | PHPDBG_HAS_PAGINATION)
209 #endif /* }}} */
210 
211 /* {{{ output descriptors */
212 #define PHPDBG_STDIN 			0
213 #define PHPDBG_STDOUT			1
214 #define PHPDBG_STDERR			2
215 #define PHPDBG_IO_FDS 			3 /* }}} */
216 
217 #define phpdbg_try_access \
218 	{                                                            \
219 		JMP_BUF *__orig_bailout = PHPDBG_G(sigsegv_bailout); \
220 		JMP_BUF __bailout;                                   \
221                                                                      \
222 		PHPDBG_G(sigsegv_bailout) = &__bailout;              \
223 		if (SETJMP(__bailout) == 0) {
224 #define phpdbg_catch_access \
225 		} else {                                             \
226 			PHPDBG_G(sigsegv_bailout) = __orig_bailout;
227 #define phpdbg_end_try_access() \
228 		}                                                    \
229 			PHPDBG_G(sigsegv_bailout) = __orig_bailout;  \
230 	}
231 
232 
233 void phpdbg_register_file_handles(void);
234 
235 /* {{{ structs */
236 ZEND_BEGIN_MODULE_GLOBALS(phpdbg)
237 	HashTable bp[PHPDBG_BREAK_TABLES];           /* break points */
238 	HashTable registered;                        /* registered */
239 	HashTable seek;                              /* seek oplines */
240 	zend_execute_data *seek_ex;                  /* call frame of oplines to seek to */
241 	zend_object *handled_exception;              /* last handled exception (prevent multiple handling of same exception) */
242 	phpdbg_frame_t frame;                        /* frame */
243 	uint32_t last_line;                          /* last executed line */
244 
245 	char *cur_command;                           /* current command */
246 	phpdbg_lexer_data lexer;                     /* lexer data */
247 	phpdbg_param_t *parser_stack;                /* param stack during lexer / parser phase */
248 
249 #ifndef _WIN32
250 	struct sigaction old_sigsegv_signal;         /* segv signal handler */
251 #endif
252 	phpdbg_btree watchpoint_tree;                /* tree with watchpoints */
253 	phpdbg_btree watch_HashTables;               /* tree with original dtors of watchpoints */
254 	HashTable watchpoints;                       /* watchpoints */
255 	HashTable watch_collisions;                  /* collision table to check if multiple watches share the same recursive watchpoint */
256 	zend_llist watchlist_mem;                    /* triggered watchpoints */
257 	zend_bool watchpoint_hit;                    /* a watchpoint was hit */
258 	void (*original_free_function)(void *);      /* the original AG(mm_heap)->_free function */
259 	phpdbg_watchpoint_t *watch_tmp;              /* temporary pointer for a watchpoint */
260 
261 	char *exec;                                  /* file to execute */
262 	size_t exec_len;                             /* size of exec */
263 	zend_op_array *ops;                 	     /* op_array */
264 	zval retval;                                 /* return value */
265 	int bp_count;                                /* breakpoint count */
266 	int vmret;                                   /* return from last opcode handler execution */
267 	zend_bool in_execution;                      /* in execution? */
268 	zend_bool unclean_eval;                      /* do not check for memory leaks when we needed to bail out during eval */
269 
270 	zend_op_array *(*compile_file)(zend_file_handle *file_handle, int type);
271 	zend_op_array *(*init_compile_file)(zend_file_handle *file_handle, int type);
272 	zend_op_array *(*compile_string)(zval *source_string, char *filename);
273 	HashTable file_sources;
274 
275 	FILE *oplog;                                 /* opline log */
276 	zend_arena *oplog_arena;                     /* arena for storing oplog */
277 	phpdbg_oplog_list *oplog_list;               /* list of oplog starts */
278 	phpdbg_oplog_entry *oplog_cur;               /* current oplog entry */
279 
280 	struct {
281 		FILE *ptr;
282 		int fd;
283 	} io[PHPDBG_IO_FDS];                         /* io */
284 	int eol;                                     /* type of line ending to use */
285 	size_t (*php_stdiop_write)(php_stream *, const char *, size_t);
286 	int in_script_xml;                           /* in <stream> output mode */
287 	struct {
288 		zend_bool active;
289 		int type;
290 		int fd;
291 		char *tag;
292 		char *msg;
293 		int msglen;
294 		char *xml;
295 		int xmllen;
296 	} err_buf;                                   /* error buffer */
297 	zend_ulong req_id;                           /* "request id" to keep track of commands */
298 
299 	char *prompt[2];                             /* prompt */
300 	const phpdbg_color_t *colors[PHPDBG_COLORS]; /* colors */
301 	char *buffer;                                /* buffer */
302 	zend_bool last_was_newline;                  /* check if we don't need to output a newline upon next phpdbg_error or phpdbg_notice */
303 
304 	FILE *stdin_file;                            /* FILE pointer to stdin source file */
305 	php_stream *(*orig_url_wrap_php)(php_stream_wrapper *wrapper, const char *path, const char *mode, int options, zend_string **opened_path, php_stream_context *context STREAMS_DC);
306 
307 	char input_buffer[PHPDBG_MAX_CMD];           /* stdin input buffer */
308 	int input_buflen;                            /* length of stdin input buffer */
309 	phpdbg_signal_safe_mem sigsafe_mem;          /* memory to use in async safe environment (only once!) */
310 
311 	JMP_BUF *sigsegv_bailout;                    /* bailout address for accesibility probing */
312 
313 	uint64_t flags;                              /* phpdbg flags */
314 
315 	char *socket_path;                           /* phpdbg.path ini setting */
316 	char *sapi_name_ptr;                         /* store sapi name to free it if necessary to not leak memory */
317 	int socket_fd;                               /* file descriptor to socket (wait command) (-1 if unused) */
318 	int socket_server_fd;                        /* file descriptor to master socket (wait command) (-1 if unused) */
319 #ifdef PHP_WIN32
320 	HANDLE sigio_watcher_thread;                 /* sigio watcher thread handle */
321 	struct win32_sigio_watcher_data swd;
322 #endif
323 	long lines;                                  /* max number of lines to display */
324 ZEND_END_MODULE_GLOBALS(phpdbg) /* }}} */
325 
326 #endif
327 
328 #endif /* PHPDBG_H */
329