1 /* Interface between gdb and its extension languages.
2 
3    Copyright (C) 2014-2024 Free Software Foundation, Inc.
4 
5    This file is part of GDB.
6 
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11 
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16 
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19 
20 #ifndef EXTENSION_H
21 #define EXTENSION_H
22 
23 #include "mi/mi-cmds.h"
24 #include "gdbsupport/array-view.h"
25 #include "hashtab.h"
26 #include <optional>
27 
28 struct breakpoint;
29 struct command_line;
30 class frame_info_ptr;
31 struct language_defn;
32 struct objfile;
33 struct extension_language_defn;
34 struct type;
35 struct ui_file;
36 struct ui_out;
37 struct value;
38 struct value_print_options;
39 
40 /* A function to load and process a script file.
41    The file has been opened and is ready to be read from the beginning.
42    Any exceptions are not caught, and are passed to the caller.  */
43 typedef void script_sourcer_func (const struct extension_language_defn *,
44                                           FILE *stream, const char *filename);
45 
46 /* A function to load and process a script for an objfile.
47    The file has been opened and is ready to be read from the beginning.
48    Any exceptions are not caught, and are passed to the caller.  */
49 typedef void objfile_script_sourcer_func
50   (const struct extension_language_defn *,
51    struct objfile *, FILE *stream, const char *filename);
52 
53 /* A function to execute a script for an objfile.
54    Any exceptions are not caught, and are passed to the caller.  */
55 typedef void objfile_script_executor_func
56   (const struct extension_language_defn *,
57    struct objfile *, const char *name, const char *script);
58 
59 /* Enum of each extension(/scripting) language.  */
60 
61 enum extension_language
62   {
63     EXT_LANG_NONE,
64     EXT_LANG_GDB,
65     EXT_LANG_PYTHON,
66     EXT_LANG_GUILE
67   };
68 
69 /* Extension language frame-filter status return values.  */
70 
71 enum ext_lang_bt_status
72   {
73     /* Return when an error has occurred in processing frame filters,
74        or when printing the stack.  */
75     EXT_LANG_BT_ERROR = -1,
76 
77     /* Return from internal routines to indicate that the function
78        succeeded.  */
79     EXT_LANG_BT_OK = 1,
80 
81     /* Return when the frame filter process is complete, but there
82        were no filter registered and enabled to process.  */
83     EXT_LANG_BT_NO_FILTERS = 2
84   };
85 
86 /* Flags to pass to apply_extlang_frame_filter.  */
87 
88 enum frame_filter_flag
89   {
90     /* Set this flag if frame level is to be printed.  */
91     PRINT_LEVEL = 1 << 0,
92 
93     /* Set this flag if frame information is to be printed.  */
94     PRINT_FRAME_INFO = 1 << 1,
95 
96     /* Set this flag if frame arguments are to be printed.  */
97     PRINT_ARGS = 1 << 2,
98 
99     /* Set this flag if frame locals are to be printed.  */
100     PRINT_LOCALS = 1 << 3,
101 
102     /* Set this flag if a "More frames" message is to be printed.  */
103     PRINT_MORE_FRAMES = 1 << 4,
104 
105     /* Set this flag if elided frames should not be printed.  */
106     PRINT_HIDE = 1 << 5,
107 
108     /* Set this flag if pretty printers for frame arguments should not
109        be invoked.  */
110     PRINT_RAW_FRAME_ARGUMENTS = 1 << 6,
111   };
112 
113 DEF_ENUM_FLAGS_TYPE (enum frame_filter_flag, frame_filter_flags);
114 
115 /* A choice of the different frame argument printing strategies that
116    can occur in different cases of frame filter instantiation.  */
117 
118 enum ext_lang_frame_args
119   {
120     /* Print no values for arguments when invoked from the MI. */
121     NO_VALUES = PRINT_NO_VALUES,
122 
123     MI_PRINT_ALL_VALUES = PRINT_ALL_VALUES,
124 
125     /* Print only simple values (what MI defines as "simple") for
126        arguments when invoked from the MI. */
127     MI_PRINT_SIMPLE_VALUES = PRINT_SIMPLE_VALUES,
128 
129     /* Print only scalar values for arguments when invoked from the CLI. */
130     CLI_SCALAR_VALUES,
131 
132     /* Print all values for arguments when invoked from the CLI. */
133     CLI_ALL_VALUES,
134 
135     /* Only indicate the presence of arguments when invoked from the CLI.  */
136     CLI_PRESENCE
137   };
138 
139 /* The possible results of
140    extension_language_ops.breakpoint_cond_says_stop.  */
141 
142 enum ext_lang_bp_stop
143   {
144     /* No "stop" condition is set.  */
145     EXT_LANG_BP_STOP_UNSET,
146 
147     /* A "stop" condition is set, and it says "don't stop".  */
148     EXT_LANG_BP_STOP_NO,
149 
150     /* A "stop" condition is set, and it says "stop".  */
151     EXT_LANG_BP_STOP_YES
152   };
153 
154 /* Table of type printers associated with the global typedef table.  */
155 
156 struct ext_lang_type_printers
157 {
158   ext_lang_type_printers ();
159   ~ext_lang_type_printers ();
160 
161   DISABLE_COPY_AND_ASSIGN (ext_lang_type_printers);
162 
163   /* Type-printers from Python.  */
164   void *py_type_printers = nullptr;
165 };
166 
167 /* The return code for some API calls.  */
168 
169 enum ext_lang_rc
170 {
171   /* The operation completed successfully.  */
172   EXT_LANG_RC_OK,
173 
174   /* The operation was not performed (e.g., no pretty-printer).  */
175   EXT_LANG_RC_NOP,
176 
177   /* There was an error (e.g., Python error while printing a value).
178      When an error occurs no further extension languages are tried.
179      This is to preserve existing behaviour, and because it's convenient
180      for Python developers.
181      Note: This is different than encountering a memory error trying to read
182      a value for pretty-printing.  Here we're referring to, e.g., programming
183      errors that trigger an exception in the extension language.  */
184   EXT_LANG_RC_ERROR
185 };
186 
187 /* A type which holds its extension language specific xmethod worker data.  */
188 
189 struct xmethod_worker
190 {
xmethod_workerxmethod_worker191   xmethod_worker (const extension_language_defn *extlang)
192   : m_extlang (extlang)
193   {}
194 
195   virtual ~xmethod_worker () = default;
196 
197   /* Invoke the xmethod encapsulated in this worker and return the result.
198      The method is invoked on OBJ with arguments in the ARGS array.  */
199 
200   virtual value *invoke (value *obj, gdb::array_view<value *> args) = 0;
201 
202   /* Return the arg types of the xmethod encapsulated in this worker.
203      The type of the 'this' object is returned as the first element of
204      the vector.  */
205 
206   std::vector<type *> get_arg_types ();
207 
208   /* Return the type of the result of the xmethod encapsulated in this worker.
209      OBJECT and ARGS are the same as for invoke.  */
210 
211   type *get_result_type (value *object, gdb::array_view<value *> args);
212 
213 private:
214 
215   /* Return the types of the arguments the method takes.  The types
216      are returned in TYPE_ARGS, one per argument.  */
217 
218   virtual enum ext_lang_rc do_get_arg_types
219     (std::vector<type *> *type_args) = 0;
220 
221   /* Fetch the type of the result of the method implemented by this
222      worker.  OBJECT and ARGS are the same as for the invoked method.
223      The result type is stored in *RESULT_TYPE.  */
224 
225   virtual enum ext_lang_rc do_get_result_type
226     (struct value *obj, gdb::array_view<value *> args,
227      struct type **result_type_ptr) = 0;
228 
229   /* The language the xmethod worker is implemented in.  */
230 
231   const extension_language_defn *m_extlang;
232 };
233 
234 typedef std::unique_ptr<xmethod_worker> xmethod_worker_up;
235 
236 /* The interface for gdb's own extension(/scripting) language.  */
237 extern const struct extension_language_defn extension_language_gdb;
238 
239 extern const struct extension_language_defn *get_ext_lang_defn
240   (enum extension_language lang);
241 
242 extern const struct extension_language_defn *get_ext_lang_of_file
243   (const char *file);
244 
245 extern int ext_lang_present_p (const struct extension_language_defn *);
246 
247 extern int ext_lang_initialized_p (const struct extension_language_defn *);
248 
249 extern void throw_ext_lang_unsupported
250   (const struct extension_language_defn *);
251 
252 /* Accessors for "public" attributes of the extension language definition.  */
253 
254 extern enum extension_language ext_lang_kind
255   (const struct extension_language_defn *);
256 
257 extern const char *ext_lang_name (const struct extension_language_defn *);
258 
259 extern const char *ext_lang_capitalized_name
260   (const struct extension_language_defn *);
261 
262 extern const char *ext_lang_suffix (const struct extension_language_defn *);
263 
264 extern const char *ext_lang_auto_load_suffix
265   (const struct extension_language_defn *);
266 
267 extern script_sourcer_func *ext_lang_script_sourcer
268   (const struct extension_language_defn *);
269 
270 extern objfile_script_sourcer_func *ext_lang_objfile_script_sourcer
271   (const struct extension_language_defn *);
272 
273 extern objfile_script_executor_func *ext_lang_objfile_script_executor
274   (const struct extension_language_defn *);
275 
276 /* Return true if auto-loading of EXTLANG scripts is enabled.
277    False is returned if support for this language isn't compiled in.  */
278 
279 extern bool ext_lang_auto_load_enabled (const struct extension_language_defn *);
280 
281 /* Wrappers for each extension language API function that iterate over all
282    extension languages.  */
283 
284 extern void ext_lang_initialization (void);
285 
286 /* Shut down all extension languages.  */
287 extern void ext_lang_shutdown ();
288 
289 extern void eval_ext_lang_from_control_command (struct command_line *cmd);
290 
291 extern void auto_load_ext_lang_scripts_for_objfile (struct objfile *);
292 
293 extern gdb::unique_xmalloc_ptr<char> apply_ext_lang_type_printers
294      (struct ext_lang_type_printers *, struct type *);
295 
296 extern int apply_ext_lang_val_pretty_printer
297   (struct value *value, struct ui_file *stream, int recurse,
298    const struct value_print_options *options,
299    const struct language_defn *language);
300 
301 extern enum ext_lang_bt_status apply_ext_lang_frame_filter
302   (const frame_info_ptr &frame, frame_filter_flags flags,
303    enum ext_lang_frame_args args_type,
304    struct ui_out *out, int frame_low, int frame_high);
305 
306 extern void preserve_ext_lang_values (struct objfile *, htab_t copied_types);
307 
308 extern const struct extension_language_defn *get_breakpoint_cond_ext_lang
309   (struct breakpoint *b, enum extension_language skip_lang);
310 
311 extern bool breakpoint_ext_lang_cond_says_stop (struct breakpoint *);
312 
313 /* If a method with name METHOD_NAME is to be invoked on an object of type
314    TYPE, then all extension languages are searched for implementations of
315    methods with name METHOD_NAME.  All matches found are appended to the WORKERS
316    vector.  */
317 
318 extern void get_matching_xmethod_workers
319   (struct type *type, const char *method_name,
320    std::vector<xmethod_worker_up> *workers);
321 
322 /* Try to colorize some source code.  FILENAME is the name of the file
323    holding the code.  CONTENTS is the source code itself.  This will
324    either a colorized (using ANSI terminal escapes) version of the
325    source code, or an empty value if colorizing could not be done.  */
326 
327 extern std::optional<std::string> ext_lang_colorize
328   (const std::string &filename, const std::string &contents);
329 
330 /* Try to colorize a single line of disassembler output, CONTENT for
331    GDBARCH.  This will return either a colorized (using ANSI terminal
332    escapes) version of CONTENT, or an empty value if colorizing could not
333    be done.  */
334 
335 extern std::optional<std::string> ext_lang_colorize_disasm
336   (const std::string &content, gdbarch *gdbarch);
337 
338 /* Calls extension_language_ops::print_insn for each extension language,
339    returning the result from the first extension language that returns a
340    non-empty result (any further extension languages are not then called).
341 
342    All arguments are forwarded to extension_language_ops::print_insn, see
343    that function for a full description.  */
344 
345 extern std::optional<int> ext_lang_print_insn
346   (struct gdbarch *gdbarch, CORE_ADDR address, struct disassemble_info *info);
347 
348 /* When GDB calls into an extension language because an objfile was
349    discovered for which GDB couldn't find any debug information, this
350    structure holds the result that the extension language returns.
351 
352    There are three possible actions that might be returned by an extension;
353    first an extension can return a filename, this is the path to the file
354    containing the required debug  information.  The second possibility is
355    to return a flag indicating that GDB should check again for the missing
356    debug information, this would imply that the extension has installed
357    the debug information into a location where GDB can be expected to find
358    it.  And the third option is for the extension to just return a null
359    result, indication there is nothing the extension can do to provide the
360    missing debug information.  */
361 struct ext_lang_missing_debuginfo_result
362 {
363   /* Default result.  The extension was unable to provide the missing debug
364      info.  */
ext_lang_missing_debuginfo_resultext_lang_missing_debuginfo_result365   ext_lang_missing_debuginfo_result ()
366   { /* Nothing.  */ }
367 
368   /* When TRY_AGAIN is true GDB should try searching again, the extension
369      may have installed the missing debug info into a suitable location.
370      When TRY_AGAIN is false this is equivalent to the default, no
371      argument, constructor.  */
ext_lang_missing_debuginfo_resultext_lang_missing_debuginfo_result372   ext_lang_missing_debuginfo_result (bool try_again)
373     : m_try_again (try_again)
374   { /* Nothing.  */ }
375 
376   /* Look in FILENAME for the missing debug info.  */
ext_lang_missing_debuginfo_resultext_lang_missing_debuginfo_result377   ext_lang_missing_debuginfo_result (std::string &&filename)
378     : m_filename (std::move (filename))
379   { /* Nothing.  */ }
380 
381   /* The filename where GDB can find the missing debuginfo.  This is empty
382      if the extension didn't suggest a file that can be used.  */
383   const std::string &
filenameext_lang_missing_debuginfo_result384   filename () const
385   {
386     return m_filename;
387   }
388 
389   /* Returns true if GDB should look again for the debug information.  */
390   const bool
try_againext_lang_missing_debuginfo_result391   try_again () const
392   {
393     return m_try_again;
394   }
395 
396 private:
397   /* The filename where the missing debuginfo can now be found.  */
398   std::string m_filename;
399 
400   /* When true GDB will search again for the debuginfo using its standard
401      techniques.  When false GDB will not search again.  */
402   bool m_try_again = false;
403 };
404 
405 /* Called when GDB failed to find any debug information for OBJFILE.  */
406 
407 extern ext_lang_missing_debuginfo_result ext_lang_handle_missing_debuginfo
408   (struct objfile *objfile);
409 
410 #if GDB_SELF_TEST
411 namespace selftests {
412 extern void (*hook_set_active_ext_lang) ();
413 }
414 #endif
415 
416 /* Temporarily disable cooperative SIGINT handling.  Needed when we
417    don't want a SIGINT to interrupt the currently active extension
418    language.  */
419 class scoped_disable_cooperative_sigint_handling
420 {
421 public:
422   scoped_disable_cooperative_sigint_handling ();
423   ~scoped_disable_cooperative_sigint_handling ();
424 
425   DISABLE_COPY_AND_ASSIGN (scoped_disable_cooperative_sigint_handling);
426 
427 private:
428   struct active_ext_lang_state *m_prev_active_ext_lang_state;
429   bool m_prev_cooperative_sigint_handling_disabled;
430 };
431 
432 /* GDB's SIGINT handler basically sets a flag; code that might take a
433    long time before it gets back to the event loop, and which ought to
434    be interruptible, checks this flag using the QUIT macro, which, if
435    GDB has the terminal, throws a quit exception.
436 
437    In addition to setting a flag, the SIGINT handler also marks a
438    select/poll-able file descriptor as read-ready.  That is used by
439    interruptible_select in order to support interrupting blocking I/O
440    in a race-free manner.
441 
442    These functions use the extension_language_ops API to allow extension
443    language(s) and GDB SIGINT handling to coexist seamlessly.  */
444 
445 /* Return true if the quit flag has been set, false otherwise.
446    Note: The flag is cleared as a side-effect.
447    The flag is checked in all extension languages that support cooperative
448    SIGINT handling, not just the current one.  This simplifies transitions.  */
449 
450 extern bool check_quit_flag ();
451 
452 /* Set the quit flag.
453    This only sets the flag in the currently active extension language.
454    If the currently active extension language does not have cooperative
455    SIGINT handling, then GDB's global flag is set, and it is up to the
456    extension language to call check_quit_flag.  The extension language
457    is free to install its own SIGINT handler, but we still need to handle
458    the transition.  */
459 
460 extern void set_quit_flag ();
461 
462 #endif /* EXTENSION_H */
463