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    | https://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 /* {{{ strings */
95 #define PHPDBG_NAME "phpdbg"
96 #define PHPDBG_AUTHORS "Felipe Pena, Joe Watkins and Bob Weinand" /* Ordered by last name */
97 #define PHPDBG_ISSUES "http://bugs.php.net/report.php"
98 #define PHPDBG_VERSION PHP_VERSION
99 #define PHPDBG_INIT_FILENAME ".phpdbginit"
100 #define PHPDBG_DEFAULT_PROMPT "prompt>"
101 /* }}} */
102 
103 #ifdef ZTS
104 # define PHPDBG_G(v) ZEND_TSRMG(phpdbg_globals_id, zend_phpdbg_globals *, v)
105 #else
106 # define PHPDBG_G(v) (phpdbg_globals.v)
107 #endif
108 
109 #include "phpdbg_sigsafe.h"
110 #include "phpdbg_out.h"
111 #include "phpdbg_lexer.h"
112 #include "phpdbg_cmd.h"
113 #include "phpdbg_utils.h"
114 #include "phpdbg_btree.h"
115 #include "phpdbg_watch.h"
116 #include "phpdbg_bp.h"
117 
118 int phpdbg_do_parse(phpdbg_param_t *stack, char *input);
119 
120 #define PHPDBG_NEXT   2
121 #define PHPDBG_UNTIL  3
122 #define PHPDBG_FINISH 4
123 #define PHPDBG_LEAVE  5
124 
125 /*
126  BEGIN: DO NOT CHANGE DO NOT CHANGE DO NOT CHANGE
127 */
128 
129 /* {{{ flags */
130 #define PHPDBG_HAS_FILE_BP            (1ULL<<1)
131 #define PHPDBG_HAS_PENDING_FILE_BP    (1ULL<<2)
132 #define PHPDBG_HAS_SYM_BP             (1ULL<<3)
133 #define PHPDBG_HAS_OPLINE_BP          (1ULL<<4)
134 #define PHPDBG_HAS_METHOD_BP          (1ULL<<5)
135 #define PHPDBG_HAS_COND_BP            (1ULL<<6)
136 #define PHPDBG_HAS_OPCODE_BP          (1ULL<<7)
137 #define PHPDBG_HAS_FUNCTION_OPLINE_BP (1ULL<<8)
138 #define PHPDBG_HAS_METHOD_OPLINE_BP   (1ULL<<9)
139 #define PHPDBG_HAS_FILE_OPLINE_BP     (1ULL<<10) /* }}} */
140 
141 /*
142  END: DO NOT CHANGE DO NOT CHANGE DO NOT CHANGE
143 */
144 
145 #define PHPDBG_IN_COND_BP             (1ULL<<11)
146 #define PHPDBG_IN_EVAL                (1ULL<<12)
147 
148 #define PHPDBG_IS_STEPPING            (1ULL<<13)
149 #define PHPDBG_STEP_OPCODE            (1ULL<<14)
150 #define PHPDBG_IS_QUIET               (1ULL<<15)
151 #define PHPDBG_IS_QUITTING            (1ULL<<16)
152 #define PHPDBG_IS_COLOURED            (1ULL<<17)
153 #define PHPDBG_IS_CLEANING            (1ULL<<18)
154 #define PHPDBG_IS_RUNNING             (1ULL<<19)
155 
156 #define PHPDBG_IN_UNTIL               (1ULL<<20)
157 #define PHPDBG_IN_FINISH              (1ULL<<21)
158 #define PHPDBG_IN_LEAVE               (1ULL<<22)
159 
160 #define PHPDBG_IS_REGISTERED          (1ULL<<23)
161 #define PHPDBG_IS_STEPONEVAL          (1ULL<<24)
162 #define PHPDBG_IS_INITIALIZING        (1ULL<<25)
163 #define PHPDBG_IS_SIGNALED            (1ULL<<26)
164 #define PHPDBG_IS_INTERACTIVE         (1ULL<<27)
165 #define PHPDBG_PREVENT_INTERACTIVE    (1ULL<<28)
166 #define PHPDBG_IS_BP_ENABLED          (1ULL<<29)
167 #define PHPDBG_SHOW_REFCOUNTS         (1ULL<<30)
168 #define PHPDBG_IN_SIGNAL_HANDLER      (1ULL<<31)
169 #define PHPDBG_DISCARD_OUTPUT         (1ULL<<32)
170 #define PHPDBG_HAS_PAGINATION         (1ULL<<33)
171 
172 #define PHPDBG_SEEK_MASK              (PHPDBG_IN_UNTIL | PHPDBG_IN_FINISH | PHPDBG_IN_LEAVE)
173 #define PHPDBG_BP_RESOLVE_MASK	      (PHPDBG_HAS_FUNCTION_OPLINE_BP | PHPDBG_HAS_METHOD_OPLINE_BP | PHPDBG_HAS_FILE_OPLINE_BP)
174 #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)
175 #define PHPDBG_IS_STOPPING            (PHPDBG_IS_QUITTING | PHPDBG_IS_CLEANING)
176 
177 #define PHPDBG_PRESERVE_FLAGS_MASK    \
178     (PHPDBG_SHOW_REFCOUNTS | \
179      PHPDBG_IS_STEPONEVAL | \
180      PHPDBG_IS_BP_ENABLED | \
181      PHPDBG_STEP_OPCODE | \
182      PHPDBG_IS_QUIET | \
183      PHPDBG_IS_COLOURED | \
184      PHPDBG_HAS_PAGINATION)
185 
186 #ifndef _WIN32
187 #	define PHPDBG_DEFAULT_FLAGS (PHPDBG_IS_QUIET | PHPDBG_IS_COLOURED | PHPDBG_IS_BP_ENABLED | PHPDBG_HAS_PAGINATION)
188 #else
189 #	define PHPDBG_DEFAULT_FLAGS (PHPDBG_IS_QUIET | PHPDBG_IS_BP_ENABLED | PHPDBG_HAS_PAGINATION)
190 #endif /* }}} */
191 
192 /* {{{ output descriptors */
193 #define PHPDBG_STDIN 			0
194 #define PHPDBG_STDOUT			1
195 #define PHPDBG_STDERR			2
196 #define PHPDBG_IO_FDS 			3 /* }}} */
197 
198 #define phpdbg_try_access \
199 	{                                                            \
200 		JMP_BUF *__orig_bailout = PHPDBG_G(sigsegv_bailout); \
201 		JMP_BUF __bailout;                                   \
202                                                                      \
203 		PHPDBG_G(sigsegv_bailout) = &__bailout;              \
204 		if (SETJMP(__bailout) == 0) {
205 #define phpdbg_catch_access \
206 		} else {                                             \
207 			PHPDBG_G(sigsegv_bailout) = __orig_bailout;
208 #define phpdbg_end_try_access() \
209 		}                                                    \
210 			PHPDBG_G(sigsegv_bailout) = __orig_bailout;  \
211 	}
212 
213 
214 void phpdbg_register_file_handles(void);
215 
216 typedef struct _phpdbg_oplog_entry phpdbg_oplog_entry;
217 struct _phpdbg_oplog_entry {
218 	phpdbg_oplog_entry *next;
219 	zend_string *function_name;
220 	zend_class_entry *scope;
221 	zend_string *filename;
222 	zend_op *opcodes;
223 	zend_op *op;
224 };
225 
226 typedef struct _phpdbg_oplog_list phpdbg_oplog_list;
227 struct _phpdbg_oplog_list {
228 	phpdbg_oplog_list *prev;
229 	phpdbg_oplog_entry start; /* Only "next" member used. */
230 };
231 
232 
233 /* {{{ structs */
234 ZEND_BEGIN_MODULE_GLOBALS(phpdbg)
235 	HashTable bp[PHPDBG_BREAK_TABLES];           /* break points */
236 	HashTable registered;                        /* registered */
237 	HashTable seek;                              /* seek oplines */
238 	zend_execute_data *seek_ex;                  /* call frame of oplines to seek to */
239 	zend_object *handled_exception;              /* last handled exception (prevent multiple handling of same exception) */
240 	phpdbg_frame_t frame;                        /* frame */
241 	uint32_t last_line;                          /* last executed line */
242 
243 	char *cur_command;                           /* current command */
244 	phpdbg_lexer_data lexer;                     /* lexer data */
245 	phpdbg_param_t *parser_stack;                /* param stack during lexer / parser phase */
246 
247 #ifndef _WIN32
248 	struct sigaction old_sigsegv_signal;         /* segv signal handler */
249 #endif
250 	phpdbg_btree watchpoint_tree;                /* tree with watchpoints */
251 	phpdbg_btree watch_HashTables;               /* tree with original dtors of watchpoints */
252 	HashTable watch_elements;                    /* user defined watch elements */
253 	HashTable watch_collisions;                  /* collision table to check if multiple watches share the same recursive watchpoint */
254 	HashTable watch_recreation;                  /* watch elements pending recreation of their respective watchpoints */
255 	HashTable watch_free;                        /* pointers to watch for being freed */
256 	HashTable *watchlist_mem;                    /* triggered watchpoints */
257 	HashTable *watchlist_mem_backup;             /* triggered watchpoints backup table while iterating over it */
258 	bool watchpoint_hit;                    /* a watchpoint was hit */
259 	void (*original_free_function)(void *);      /* the original AG(mm_heap)->_free function */
260 	phpdbg_watch_element *watch_tmp;             /* temporary pointer for a watch element */
261 
262 	char *exec;                                  /* file to execute */
263 	size_t exec_len;                             /* size of exec */
264 	zend_op_array *ops;                 	     /* op_array */
265 	zval retval;                                 /* return value */
266 	int bp_count;                                /* breakpoint count */
267 	int vmret;                                   /* return from last opcode handler execution */
268 	bool in_execution;                      /* in execution? */
269 	bool unclean_eval;                      /* do not check for memory leaks when we needed to bail out during eval */
270 
271 	zend_op_array *(*compile_file)(zend_file_handle *file_handle, int type);
272 	zend_op_array *(*init_compile_file)(zend_file_handle *file_handle, int type);
273 	zend_op_array *(*compile_string)(zend_string *source_string, const char *filename);
274 	HashTable file_sources;
275 
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 		int fd;
282 	} io[PHPDBG_IO_FDS];                         /* io */
283 	ssize_t (*php_stdiop_write)(php_stream *, const char *, size_t);
284 	struct {
285 		bool active;
286 		int type;
287 		int fd;
288 		char *msg;
289 		int msglen;
290 	} err_buf;                                   /* error buffer */
291 	zend_ulong req_id;                           /* "request id" to keep track of commands */
292 
293 	char *prompt[2];                             /* prompt */
294 	const phpdbg_color_t *colors[PHPDBG_COLORS]; /* colors */
295 	char *buffer;                                /* buffer */
296 	bool last_was_newline;                  /* check if we don't need to output a newline upon next phpdbg_error or phpdbg_notice */
297 
298 	FILE *stdin_file;                            /* FILE pointer to stdin source file */
299 	const php_stream_wrapper *orig_url_wrap_php;
300 
301 	char input_buffer[PHPDBG_MAX_CMD];           /* stdin input buffer */
302 	int input_buflen;                            /* length of stdin input buffer */
303 	phpdbg_signal_safe_mem sigsafe_mem;          /* memory to use in async safe environment (only once!) */
304 
305 	JMP_BUF *sigsegv_bailout;                    /* bailout address for accessibility probing */
306 
307 	uint64_t flags;                              /* phpdbg flags */
308 
309 	char *sapi_name_ptr;                         /* store sapi name to free it if necessary to not leak memory */
310 	zend_ulong lines;                                  /* max number of lines to display */
311 ZEND_END_MODULE_GLOBALS(phpdbg) /* }}} */
312 
313 #endif /* PHPDBG_H */
314