phpdbg.h 12 KB

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