phpdbg_cmd.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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_CMD_H
  19. #define PHPDBG_CMD_H
  20. #include "TSRM.h"
  21. #include "zend_generators.h"
  22. /* {{{ Command and Parameter */
  23. enum {
  24. NO_ARG = 0,
  25. REQUIRED_ARG,
  26. OPTIONAL_ARG
  27. };
  28. typedef enum {
  29. EMPTY_PARAM = 0,
  30. ADDR_PARAM,
  31. FILE_PARAM,
  32. NUMERIC_FILE_PARAM,
  33. METHOD_PARAM,
  34. STR_PARAM,
  35. NUMERIC_PARAM,
  36. NUMERIC_FUNCTION_PARAM,
  37. NUMERIC_METHOD_PARAM,
  38. STACK_PARAM,
  39. EVAL_PARAM,
  40. SHELL_PARAM,
  41. COND_PARAM,
  42. OP_PARAM,
  43. ORIG_PARAM,
  44. RUN_PARAM
  45. } phpdbg_param_type;
  46. typedef struct _phpdbg_param phpdbg_param_t;
  47. struct _phpdbg_param {
  48. phpdbg_param_type type;
  49. zend_long num;
  50. zend_ulong addr;
  51. struct {
  52. char *name;
  53. zend_ulong line;
  54. } file;
  55. struct {
  56. char *class;
  57. char *name;
  58. } method;
  59. char *str;
  60. size_t len;
  61. phpdbg_param_t *next;
  62. phpdbg_param_t *top;
  63. };
  64. #define phpdbg_init_param(v, t) do{ \
  65. (v)->type = (t); \
  66. (v)->addr = 0; \
  67. (v)->num = 0; \
  68. (v)->file.name = NULL; \
  69. (v)->file.line = 0; \
  70. (v)->method.class = NULL; \
  71. (v)->method.name = NULL; \
  72. (v)->str = NULL; \
  73. (v)->len = 0; \
  74. (v)->next = NULL; \
  75. (v)->top = NULL; \
  76. } while(0)
  77. #define PHPDBG_ASYNC_SAFE 1
  78. typedef int (*phpdbg_command_handler_t)(const phpdbg_param_t*);
  79. typedef struct _phpdbg_command_t phpdbg_command_t;
  80. struct _phpdbg_command_t {
  81. const char *name; /* Command name */
  82. size_t name_len; /* Command name length */
  83. const char *tip; /* Menu tip */
  84. size_t tip_len; /* Menu tip length */
  85. char alias; /* Alias */
  86. phpdbg_command_handler_t handler; /* Command handler */
  87. const phpdbg_command_t *subs; /* Sub Commands */
  88. char *args; /* Argument Spec */
  89. const phpdbg_command_t *parent; /* Parent Command */
  90. bool flags; /* General flags */
  91. };
  92. /* }}} */
  93. /* {{{ misc */
  94. #define PHPDBG_STRL(s) s, sizeof(s)-1
  95. #define PHPDBG_MAX_CMD 500
  96. #define PHPDBG_FRAME(v) (PHPDBG_G(frame).v)
  97. #define PHPDBG_EX(v) (EG(current_execute_data)->v)
  98. typedef struct {
  99. int num;
  100. zend_generator *generator;
  101. zend_execute_data *execute_data;
  102. } phpdbg_frame_t;
  103. /* }}} */
  104. /*
  105. * Workflow:
  106. * 1) the lexer/parser creates a stack of commands and arguments from input
  107. * 2) the commands at the top of the stack are resolved sensibly using aliases, abbreviations and case insensitive matching
  108. * 3) the remaining arguments in the stack are verified (optionally) against the handlers declared argument specification
  109. * 4) the handler is called passing the top of the stack as the only parameter
  110. * 5) the stack is destroyed upon return from the handler
  111. */
  112. /*
  113. * Input Management
  114. */
  115. PHPDBG_API char *phpdbg_read_input(const char *buffered);
  116. PHPDBG_API void phpdbg_destroy_input(char **input);
  117. PHPDBG_API int phpdbg_ask_user_permission(const char *question);
  118. /**
  119. * Stack Management
  120. */
  121. PHPDBG_API void phpdbg_stack_push(phpdbg_param_t *stack, phpdbg_param_t *param);
  122. PHPDBG_API void phpdbg_stack_separate(phpdbg_param_t *param);
  123. PHPDBG_API const phpdbg_command_t *phpdbg_stack_resolve(const phpdbg_command_t *commands, const phpdbg_command_t *parent, phpdbg_param_t **top);
  124. PHPDBG_API int phpdbg_stack_verify(const phpdbg_command_t *command, phpdbg_param_t **stack);
  125. PHPDBG_API int phpdbg_stack_execute(phpdbg_param_t *stack, bool allow_async_unsafe);
  126. PHPDBG_API void phpdbg_stack_free(phpdbg_param_t *stack);
  127. /*
  128. * Parameter Management
  129. */
  130. PHPDBG_API void phpdbg_clear_param(phpdbg_param_t*);
  131. PHPDBG_API void phpdbg_copy_param(const phpdbg_param_t*, phpdbg_param_t*);
  132. PHPDBG_API bool phpdbg_match_param(const phpdbg_param_t *, const phpdbg_param_t *);
  133. PHPDBG_API zend_ulong phpdbg_hash_param(const phpdbg_param_t *);
  134. PHPDBG_API const char* phpdbg_get_param_type(const phpdbg_param_t*);
  135. PHPDBG_API char* phpdbg_param_tostring(const phpdbg_param_t *param, char **pointer);
  136. PHPDBG_API void phpdbg_param_debug(const phpdbg_param_t *param, const char *msg);
  137. /**
  138. * Command Declarators
  139. */
  140. #define PHPDBG_COMMAND_HANDLER(name) phpdbg_do_##name
  141. #define PHPDBG_COMMAND_D_EXP(name, tip, alias, handler, children, args, parent, flags) \
  142. {PHPDBG_STRL(#name), tip, sizeof(tip)-1, alias, phpdbg_do_##handler, children, args, parent, flags}
  143. #define PHPDBG_COMMAND_D_EX(name, tip, alias, handler, children, args, flags) \
  144. {PHPDBG_STRL(#name), tip, sizeof(tip)-1, alias, phpdbg_do_##handler, children, args, NULL, flags}
  145. #define PHPDBG_COMMAND_D(name, tip, alias, children, args, flags) \
  146. {PHPDBG_STRL(#name), tip, sizeof(tip)-1, alias, phpdbg_do_##name, children, args, NULL, flags}
  147. #define PHPDBG_COMMAND(name) int phpdbg_do_##name(const phpdbg_param_t *param)
  148. #define PHPDBG_COMMAND_ARGS param
  149. #define PHPDBG_END_COMMAND {NULL, 0, NULL, 0, '\0', NULL, NULL, NULL, NULL, 0}
  150. /*
  151. * Default Switch Case
  152. */
  153. #define phpdbg_default_switch_case() \
  154. default: \
  155. phpdbg_error("Unsupported parameter type (%s) for command", phpdbg_get_param_type(param)); \
  156. break
  157. #endif /* PHPDBG_CMD_H */