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