extension-priv.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /* Private implementation details of interface between gdb and its
  2. extension languages.
  3. Copyright (C) 2014-2022 Free Software Foundation, Inc.
  4. This file is part of GDB.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. #ifndef EXTENSION_PRIV_H
  16. #define EXTENSION_PRIV_H
  17. #include "extension.h"
  18. #include <signal.h>
  19. #include "cli/cli-script.h"
  20. /* High level description of an extension/scripting language.
  21. An entry for each is compiled into GDB regardless of whether the support
  22. is present. This is done so that we can issue meaningful errors if the
  23. support is not compiled in. */
  24. struct extension_language_defn
  25. {
  26. /* Enum of the extension language. */
  27. enum extension_language language;
  28. /* The name of the extension language, lowercase. E.g., python. */
  29. const char *name;
  30. /* The capitalized name of the extension language.
  31. For python this is "Python". For gdb this is "GDB". */
  32. const char *capitalized_name;
  33. /* The file suffix of this extension language. E.g., ".py". */
  34. const char *suffix;
  35. /* The suffix of per-objfile scripts to auto-load.
  36. E.g., When the program loads libfoo.so, look for libfoo.so-gdb.py. */
  37. const char *auto_load_suffix;
  38. /* We support embedding external extension language code in GDB's own
  39. scripting language. We do this by having a special command that begins
  40. the extension language snippet, and terminate it with "end".
  41. This specifies the control type used to implement this. */
  42. enum command_control_type cli_control_type;
  43. /* A pointer to the "methods" to load scripts in this language,
  44. or NULL if the support is not compiled into GDB. */
  45. const struct extension_language_script_ops *script_ops;
  46. /* Either a pointer to the "methods" of the extension language interface
  47. or NULL if the support is not compiled into GDB.
  48. This is also NULL for GDB's own scripting language which is relatively
  49. primitive, and doesn't provide these features. */
  50. const struct extension_language_ops *ops;
  51. };
  52. /* The interface for loading scripts from external extension languages,
  53. as well as GDB's own scripting language.
  54. All of these methods are required to be implemented.
  55. By convention all of these functions take a pseudo-this parameter
  56. as the first argument. */
  57. struct extension_language_script_ops
  58. {
  59. /* Load a script. This is called, e.g., via the "source" command.
  60. If there's an error while processing the script this function may,
  61. but is not required to, throw an error. */
  62. script_sourcer_func *script_sourcer;
  63. /* Load a script attached to an objfile.
  64. If there's an error while processing the script this function may,
  65. but is not required to, throw an error. */
  66. objfile_script_sourcer_func *objfile_script_sourcer;
  67. /* Execute a script attached to an objfile.
  68. If there's an error while processing the script this function may,
  69. but is not required to, throw an error. */
  70. objfile_script_executor_func *objfile_script_executor;
  71. /* Return non-zero if auto-loading scripts in this extension language
  72. is enabled. */
  73. bool (*auto_load_enabled) (const struct extension_language_defn *);
  74. };
  75. /* The interface for making calls from GDB to an external extension
  76. language. This is for non-script-loading related functionality, like
  77. pretty-printing, etc. The reason these are separated out is GDB's own
  78. scripting language makes use of extension_language_script_opts, but it
  79. makes no use of these. There is no (current) intention to split
  80. extension_language_ops up any further.
  81. All of these methods are optional and may be NULL, except where
  82. otherwise indicated.
  83. By convention all of these functions take a pseudo-this parameter
  84. as the first argument. */
  85. struct extension_language_ops
  86. {
  87. /* Called after GDB has processed the early initialization settings
  88. files. This is when the extension language should be initialized. By
  89. the time this is called all of the earlier initialization functions
  90. have already been called. */
  91. void (*initialize) (const struct extension_language_defn *);
  92. /* Return non-zero if the extension language successfully initialized.
  93. This method is required. */
  94. int (*initialized) (const struct extension_language_defn *);
  95. /* Process a sequence of commands embedded in GDB's own scripting language.
  96. E.g.,
  97. python
  98. print 42
  99. end */
  100. void (*eval_from_control_command) (const struct extension_language_defn *,
  101. struct command_line *);
  102. /* Type-printing support:
  103. start_type_printers, apply_type_printers, free_type_printers.
  104. These methods are optional and may be NULL, but if one of them is
  105. implemented then they all must be. */
  106. /* Called before printing a type. */
  107. void (*start_type_printers) (const struct extension_language_defn *,
  108. struct ext_lang_type_printers *);
  109. /* Try to pretty-print TYPE. If successful the pretty-printed type is
  110. stored in *PRETTIED_TYPE, and the caller must free it.
  111. Returns EXT_LANG_RC_OK upon success, EXT_LANG_RC_NOP if the type
  112. is not recognized, and EXT_LANG_RC_ERROR if an error was encountered.
  113. This function has a bit of a funny name, since it actually applies
  114. recognizers, but this seemed clearer given the start_type_printers
  115. and free_type_printers functions. */
  116. enum ext_lang_rc (*apply_type_printers)
  117. (const struct extension_language_defn *,
  118. const struct ext_lang_type_printers *,
  119. struct type *, char **prettied_type);
  120. /* Called after a type has been printed to give the type pretty-printer
  121. mechanism an opportunity to clean up. */
  122. void (*free_type_printers) (const struct extension_language_defn *,
  123. struct ext_lang_type_printers *);
  124. /* Try to pretty-print a value, onto stdio stream STREAM according
  125. to OPTIONS. VAL is the object to print. Returns EXT_LANG_RC_OK
  126. upon success, EXT_LANG_RC_NOP if the value is not recognized, and
  127. EXT_LANG_RC_ERROR if an error was encountered. */
  128. enum ext_lang_rc (*apply_val_pretty_printer)
  129. (const struct extension_language_defn *,
  130. struct value *val, struct ui_file *stream, int recurse,
  131. const struct value_print_options *options,
  132. const struct language_defn *language);
  133. /* GDB access to the "frame filter" feature.
  134. FRAME is the source frame to start frame-filter invocation. FLAGS is an
  135. integer holding the flags for printing. The following elements of
  136. the FRAME_FILTER_FLAGS enum denotes the make-up of FLAGS:
  137. PRINT_LEVEL is a flag indicating whether to print the frame's
  138. relative level in the output. PRINT_FRAME_INFO is a flag that
  139. indicates whether this function should print the frame
  140. information, PRINT_ARGS is a flag that indicates whether to print
  141. frame arguments, and PRINT_LOCALS, likewise, with frame local
  142. variables. ARGS_TYPE is an enumerator describing the argument
  143. format, OUT is the output stream to print. FRAME_LOW is the
  144. beginning of the slice of frames to print, and FRAME_HIGH is the
  145. upper limit of the frames to count. Returns SCR_BT_ERROR on error,
  146. or SCR_BT_COMPLETED on success. */
  147. enum ext_lang_bt_status (*apply_frame_filter)
  148. (const struct extension_language_defn *,
  149. struct frame_info *frame, frame_filter_flags flags,
  150. enum ext_lang_frame_args args_type,
  151. struct ui_out *out, int frame_low, int frame_high);
  152. /* Update values held by the extension language when OBJFILE is discarded.
  153. New global types must be created for every such value, which must then be
  154. updated to use the new types.
  155. This function typically just iterates over all appropriate values and
  156. calls preserve_one_value for each one.
  157. COPIED_TYPES is used to prevent cycles / duplicates and is passed to
  158. preserve_one_value. */
  159. void (*preserve_values) (const struct extension_language_defn *,
  160. struct objfile *objfile, htab_t copied_types);
  161. /* Return non-zero if there is a stop condition for the breakpoint.
  162. This is used to implement the restriction that a breakpoint may have
  163. at most one condition. */
  164. int (*breakpoint_has_cond) (const struct extension_language_defn *,
  165. struct breakpoint *);
  166. /* Return a value of enum ext_lang_bp_stop indicating if there is a stop
  167. condition for the breakpoint, and if so whether the program should
  168. stop. This is called when the program has stopped at the specified
  169. breakpoint.
  170. While breakpoints can have at most one condition, this is called for
  171. every extension language, even if another extension language has a
  172. "stop" method: other kinds of breakpoints may be implemented using
  173. this method, e.g., "finish breakpoints" in Python. */
  174. enum ext_lang_bp_stop (*breakpoint_cond_says_stop)
  175. (const struct extension_language_defn *, struct breakpoint *);
  176. /* The next two are used to connect GDB's SIGINT handling with the
  177. extension language's.
  178. Terminology: If an extension language can use GDB's SIGINT handling then
  179. we say the extension language has "cooperative SIGINT handling".
  180. Python is an example of this.
  181. These need not be implemented, but if one of them is implemented
  182. then they all must be. */
  183. /* Set the SIGINT indicator.
  184. This is called by GDB's SIGINT handler and must be async-safe. */
  185. void (*set_quit_flag) (const struct extension_language_defn *);
  186. /* Return non-zero if a SIGINT has occurred.
  187. This is expected to also clear the indicator. */
  188. int (*check_quit_flag) (const struct extension_language_defn *);
  189. /* Called before gdb prints its prompt, giving extension languages an
  190. opportunity to change it with set_prompt.
  191. Returns EXT_LANG_RC_OK if the prompt was changed, EXT_LANG_RC_NOP if
  192. the prompt was not changed, and EXT_LANG_RC_ERROR if an error was
  193. encountered.
  194. Extension languages are called in order, and once the prompt is
  195. changed or an error occurs no further languages are called. */
  196. enum ext_lang_rc (*before_prompt) (const struct extension_language_defn *,
  197. const char *current_gdb_prompt);
  198. /* Return a vector of matching xmethod workers defined in this
  199. extension language. The workers service methods with name
  200. METHOD_NAME on objects of type OBJ_TYPE. The vector is returned
  201. in DM_VEC.
  202. This field may be NULL if the extension language does not support
  203. xmethods. */
  204. enum ext_lang_rc (*get_matching_xmethod_workers)
  205. (const struct extension_language_defn *extlang,
  206. struct type *obj_type,
  207. const char *method_name,
  208. std::vector<xmethod_worker_up> *dm_vec);
  209. /* Colorize a source file. NAME is the source file's name, and
  210. CONTENTS is the contents of the file. This should either return
  211. colorized (using ANSI terminal escapes) version of the contents,
  212. or an empty option. */
  213. gdb::optional<std::string> (*colorize) (const std::string &name,
  214. const std::string &contents);
  215. /* Colorize a single line of disassembler output, CONTENT. This should
  216. either return colorized (using ANSI terminal escapes) version of the
  217. contents, or an empty optional. */
  218. gdb::optional<std::string> (*colorize_disasm) (const std::string &content,
  219. gdbarch *gdbarch);
  220. };
  221. /* State necessary to restore a signal handler to its previous value. */
  222. struct signal_handler
  223. {
  224. /* Non-zero if "handler" has been set. */
  225. int handler_saved;
  226. /* The signal handler. */
  227. sighandler_t handler;
  228. };
  229. /* State necessary to restore the currently active extension language
  230. to its previous value. */
  231. struct active_ext_lang_state
  232. {
  233. /* The previously active extension language. */
  234. const struct extension_language_defn *ext_lang;
  235. /* Its SIGINT handler. */
  236. struct signal_handler sigint_handler;
  237. };
  238. extern const struct extension_language_defn *get_active_ext_lang (void);
  239. extern struct active_ext_lang_state *set_active_ext_lang
  240. (const struct extension_language_defn *);
  241. extern void restore_active_ext_lang (struct active_ext_lang_state *previous);
  242. #endif /* EXTENSION_PRIV_H */