phpdbg.h 13 KB

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