extension.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /* Interface between gdb and its extension languages.
  2. Copyright (C) 2014-2022 Free Software Foundation, Inc.
  3. This file is part of GDB.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #ifndef EXTENSION_H
  15. #define EXTENSION_H
  16. #include "mi/mi-cmds.h" /* For PRINT_NO_VALUES, etc. */
  17. #include "gdbsupport/array-view.h"
  18. #include "gdbsupport/gdb_optional.h"
  19. struct breakpoint;
  20. struct command_line;
  21. struct frame_info;
  22. struct language_defn;
  23. struct objfile;
  24. struct extension_language_defn;
  25. struct type;
  26. struct ui_file;
  27. struct ui_out;
  28. struct value;
  29. struct value_print_options;
  30. /* A function to load and process a script file.
  31. The file has been opened and is ready to be read from the beginning.
  32. Any exceptions are not caught, and are passed to the caller. */
  33. typedef void script_sourcer_func (const struct extension_language_defn *,
  34. FILE *stream, const char *filename);
  35. /* A function to load and process a script for an objfile.
  36. The file has been opened and is ready to be read from the beginning.
  37. Any exceptions are not caught, and are passed to the caller. */
  38. typedef void objfile_script_sourcer_func
  39. (const struct extension_language_defn *,
  40. struct objfile *, FILE *stream, const char *filename);
  41. /* A function to execute a script for an objfile.
  42. Any exceptions are not caught, and are passed to the caller. */
  43. typedef void objfile_script_executor_func
  44. (const struct extension_language_defn *,
  45. struct objfile *, const char *name, const char *script);
  46. /* Enum of each extension(/scripting) language. */
  47. enum extension_language
  48. {
  49. EXT_LANG_NONE,
  50. EXT_LANG_GDB,
  51. EXT_LANG_PYTHON,
  52. EXT_LANG_GUILE
  53. };
  54. /* Extension language frame-filter status return values. */
  55. enum ext_lang_bt_status
  56. {
  57. /* Return when an error has occurred in processing frame filters,
  58. or when printing the stack. */
  59. EXT_LANG_BT_ERROR = -1,
  60. /* Return from internal routines to indicate that the function
  61. succeeded. */
  62. EXT_LANG_BT_OK = 1,
  63. /* Return when the frame filter process is complete, but there
  64. were no filter registered and enabled to process. */
  65. EXT_LANG_BT_NO_FILTERS = 2
  66. };
  67. /* Flags to pass to apply_extlang_frame_filter. */
  68. enum frame_filter_flag
  69. {
  70. /* Set this flag if frame level is to be printed. */
  71. PRINT_LEVEL = 1 << 0,
  72. /* Set this flag if frame information is to be printed. */
  73. PRINT_FRAME_INFO = 1 << 1,
  74. /* Set this flag if frame arguments are to be printed. */
  75. PRINT_ARGS = 1 << 2,
  76. /* Set this flag if frame locals are to be printed. */
  77. PRINT_LOCALS = 1 << 3,
  78. /* Set this flag if a "More frames" message is to be printed. */
  79. PRINT_MORE_FRAMES = 1 << 4,
  80. /* Set this flag if elided frames should not be printed. */
  81. PRINT_HIDE = 1 << 5,
  82. };
  83. DEF_ENUM_FLAGS_TYPE (enum frame_filter_flag, frame_filter_flags);
  84. /* A choice of the different frame argument printing strategies that
  85. can occur in different cases of frame filter instantiation. */
  86. enum ext_lang_frame_args
  87. {
  88. /* Print no values for arguments when invoked from the MI. */
  89. NO_VALUES = PRINT_NO_VALUES,
  90. MI_PRINT_ALL_VALUES = PRINT_ALL_VALUES,
  91. /* Print only simple values (what MI defines as "simple") for
  92. arguments when invoked from the MI. */
  93. MI_PRINT_SIMPLE_VALUES = PRINT_SIMPLE_VALUES,
  94. /* Print only scalar values for arguments when invoked from the CLI. */
  95. CLI_SCALAR_VALUES,
  96. /* Print all values for arguments when invoked from the CLI. */
  97. CLI_ALL_VALUES,
  98. /* Only indicate the presence of arguments when invoked from the CLI. */
  99. CLI_PRESENCE
  100. };
  101. /* The possible results of
  102. extension_language_ops.breakpoint_cond_says_stop. */
  103. enum ext_lang_bp_stop
  104. {
  105. /* No "stop" condition is set. */
  106. EXT_LANG_BP_STOP_UNSET,
  107. /* A "stop" condition is set, and it says "don't stop". */
  108. EXT_LANG_BP_STOP_NO,
  109. /* A "stop" condition is set, and it says "stop". */
  110. EXT_LANG_BP_STOP_YES
  111. };
  112. /* Table of type printers associated with the global typedef table. */
  113. struct ext_lang_type_printers
  114. {
  115. ext_lang_type_printers ();
  116. ~ext_lang_type_printers ();
  117. DISABLE_COPY_AND_ASSIGN (ext_lang_type_printers);
  118. /* Type-printers from Python. */
  119. void *py_type_printers = nullptr;
  120. };
  121. /* The return code for some API calls. */
  122. enum ext_lang_rc
  123. {
  124. /* The operation completed successfully. */
  125. EXT_LANG_RC_OK,
  126. /* The operation was not performed (e.g., no pretty-printer). */
  127. EXT_LANG_RC_NOP,
  128. /* There was an error (e.g., Python error while printing a value).
  129. When an error occurs no further extension languages are tried.
  130. This is to preserve existing behaviour, and because it's convenient
  131. for Python developers.
  132. Note: This is different than encountering a memory error trying to read
  133. a value for pretty-printing. Here we're referring to, e.g., programming
  134. errors that trigger an exception in the extension language. */
  135. EXT_LANG_RC_ERROR
  136. };
  137. /* A type which holds its extension language specific xmethod worker data. */
  138. struct xmethod_worker
  139. {
  140. xmethod_worker (const extension_language_defn *extlang)
  141. : m_extlang (extlang)
  142. {}
  143. virtual ~xmethod_worker () = default;
  144. /* Invoke the xmethod encapsulated in this worker and return the result.
  145. The method is invoked on OBJ with arguments in the ARGS array. */
  146. virtual value *invoke (value *obj, gdb::array_view<value *> args) = 0;
  147. /* Return the arg types of the xmethod encapsulated in this worker.
  148. The type of the 'this' object is returned as the first element of
  149. the vector. */
  150. std::vector<type *> get_arg_types ();
  151. /* Return the type of the result of the xmethod encapsulated in this worker.
  152. OBJECT and ARGS are the same as for invoke. */
  153. type *get_result_type (value *object, gdb::array_view<value *> args);
  154. private:
  155. /* Return the types of the arguments the method takes. The types
  156. are returned in TYPE_ARGS, one per argument. */
  157. virtual enum ext_lang_rc do_get_arg_types
  158. (std::vector<type *> *type_args) = 0;
  159. /* Fetch the type of the result of the method implemented by this
  160. worker. OBJECT and ARGS are the same as for the invoked method.
  161. The result type is stored in *RESULT_TYPE. */
  162. virtual enum ext_lang_rc do_get_result_type
  163. (struct value *obj, gdb::array_view<value *> args,
  164. struct type **result_type_ptr) = 0;
  165. /* The language the xmethod worker is implemented in. */
  166. const extension_language_defn *m_extlang;
  167. };
  168. typedef std::unique_ptr<xmethod_worker> xmethod_worker_up;
  169. /* The interface for gdb's own extension(/scripting) language. */
  170. extern const struct extension_language_defn extension_language_gdb;
  171. extern const struct extension_language_defn *get_ext_lang_defn
  172. (enum extension_language lang);
  173. extern const struct extension_language_defn *get_ext_lang_of_file
  174. (const char *file);
  175. extern int ext_lang_present_p (const struct extension_language_defn *);
  176. extern int ext_lang_initialized_p (const struct extension_language_defn *);
  177. extern void throw_ext_lang_unsupported
  178. (const struct extension_language_defn *);
  179. /* Accessors for "public" attributes of the extension language definition. */
  180. extern enum extension_language ext_lang_kind
  181. (const struct extension_language_defn *);
  182. extern const char *ext_lang_name (const struct extension_language_defn *);
  183. extern const char *ext_lang_capitalized_name
  184. (const struct extension_language_defn *);
  185. extern const char *ext_lang_suffix (const struct extension_language_defn *);
  186. extern const char *ext_lang_auto_load_suffix
  187. (const struct extension_language_defn *);
  188. extern script_sourcer_func *ext_lang_script_sourcer
  189. (const struct extension_language_defn *);
  190. extern objfile_script_sourcer_func *ext_lang_objfile_script_sourcer
  191. (const struct extension_language_defn *);
  192. extern objfile_script_executor_func *ext_lang_objfile_script_executor
  193. (const struct extension_language_defn *);
  194. /* Return true if auto-loading of EXTLANG scripts is enabled.
  195. False is returned if support for this language isn't compiled in. */
  196. extern bool ext_lang_auto_load_enabled (const struct extension_language_defn *);
  197. /* Wrappers for each extension language API function that iterate over all
  198. extension languages. */
  199. extern void ext_lang_initialization (void);
  200. extern void eval_ext_lang_from_control_command (struct command_line *cmd);
  201. extern void auto_load_ext_lang_scripts_for_objfile (struct objfile *);
  202. extern char *apply_ext_lang_type_printers (struct ext_lang_type_printers *,
  203. struct type *);
  204. extern int apply_ext_lang_val_pretty_printer
  205. (struct value *value, struct ui_file *stream, int recurse,
  206. const struct value_print_options *options,
  207. const struct language_defn *language);
  208. extern enum ext_lang_bt_status apply_ext_lang_frame_filter
  209. (struct frame_info *frame, frame_filter_flags flags,
  210. enum ext_lang_frame_args args_type,
  211. struct ui_out *out, int frame_low, int frame_high);
  212. extern void preserve_ext_lang_values (struct objfile *, htab_t copied_types);
  213. extern const struct extension_language_defn *get_breakpoint_cond_ext_lang
  214. (struct breakpoint *b, enum extension_language skip_lang);
  215. extern int breakpoint_ext_lang_cond_says_stop (struct breakpoint *);
  216. /* If a method with name METHOD_NAME is to be invoked on an object of type
  217. TYPE, then all extension languages are searched for implementations of
  218. methods with name METHOD_NAME. All matches found are appended to the WORKERS
  219. vector. */
  220. extern void get_matching_xmethod_workers
  221. (struct type *type, const char *method_name,
  222. std::vector<xmethod_worker_up> *workers);
  223. /* Try to colorize some source code. FILENAME is the name of the file
  224. holding the code. CONTENTS is the source code itself. This will
  225. either a colorized (using ANSI terminal escapes) version of the
  226. source code, or an empty value if colorizing could not be done. */
  227. extern gdb::optional<std::string> ext_lang_colorize
  228. (const std::string &filename, const std::string &contents);
  229. /* Try to colorize a single line of disassembler output, CONTENT for
  230. GDBARCH. This will return either a colorized (using ANSI terminal
  231. escapes) version of CONTENT, or an empty value if colorizing could not
  232. be done. */
  233. extern gdb::optional<std::string> ext_lang_colorize_disasm
  234. (const std::string &content, gdbarch *gdbarch);
  235. #if GDB_SELF_TEST
  236. namespace selftests {
  237. extern void (*hook_set_active_ext_lang) ();
  238. }
  239. #endif
  240. #endif /* EXTENSION_H */