Fix building Loongarch BFD with a 32-bit compiler
[binutils-gdb.git] / gdb / extension.h
blob258a77dbfccb9a421626c27cff828faab78afdce
1 /* Interface between gdb and its extension languages.
3 Copyright (C) 2014-2024 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
20 #ifndef EXTENSION_H
21 #define EXTENSION_H
23 #include "mi/mi-cmds.h"
24 #include "gdbsupport/array-view.h"
25 #include "hashtab.h"
26 #include <optional>
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;
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);
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);
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);
59 /* Enum of each extension(/scripting) language. */
61 enum extension_language
63 EXT_LANG_NONE,
64 EXT_LANG_GDB,
65 EXT_LANG_PYTHON,
66 EXT_LANG_GUILE
69 /* Extension language frame-filter status return values. */
71 enum ext_lang_bt_status
73 /* Return when an error has occurred in processing frame filters,
74 or when printing the stack. */
75 EXT_LANG_BT_ERROR = -1,
77 /* Return from internal routines to indicate that the function
78 succeeded. */
79 EXT_LANG_BT_OK = 1,
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
86 /* Flags to pass to apply_extlang_frame_filter. */
88 enum frame_filter_flag
90 /* Set this flag if frame level is to be printed. */
91 PRINT_LEVEL = 1 << 0,
93 /* Set this flag if frame information is to be printed. */
94 PRINT_FRAME_INFO = 1 << 1,
96 /* Set this flag if frame arguments are to be printed. */
97 PRINT_ARGS = 1 << 2,
99 /* Set this flag if frame locals are to be printed. */
100 PRINT_LOCALS = 1 << 3,
102 /* Set this flag if a "More frames" message is to be printed. */
103 PRINT_MORE_FRAMES = 1 << 4,
105 /* Set this flag if elided frames should not be printed. */
106 PRINT_HIDE = 1 << 5,
108 /* Set this flag if pretty printers for frame arguments should not
109 be invoked. */
110 PRINT_RAW_FRAME_ARGUMENTS = 1 << 6,
113 DEF_ENUM_FLAGS_TYPE (enum frame_filter_flag, frame_filter_flags);
115 /* A choice of the different frame argument printing strategies that
116 can occur in different cases of frame filter instantiation. */
118 enum ext_lang_frame_args
120 /* Print no values for arguments when invoked from the MI. */
121 NO_VALUES = PRINT_NO_VALUES,
123 MI_PRINT_ALL_VALUES = PRINT_ALL_VALUES,
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,
129 /* Print only scalar values for arguments when invoked from the CLI. */
130 CLI_SCALAR_VALUES,
132 /* Print all values for arguments when invoked from the CLI. */
133 CLI_ALL_VALUES,
135 /* Only indicate the presence of arguments when invoked from the CLI. */
136 CLI_PRESENCE
139 /* The possible results of
140 extension_language_ops.breakpoint_cond_says_stop. */
142 enum ext_lang_bp_stop
144 /* No "stop" condition is set. */
145 EXT_LANG_BP_STOP_UNSET,
147 /* A "stop" condition is set, and it says "don't stop". */
148 EXT_LANG_BP_STOP_NO,
150 /* A "stop" condition is set, and it says "stop". */
151 EXT_LANG_BP_STOP_YES
154 /* Table of type printers associated with the global typedef table. */
156 struct ext_lang_type_printers
158 ext_lang_type_printers ();
159 ~ext_lang_type_printers ();
161 DISABLE_COPY_AND_ASSIGN (ext_lang_type_printers);
163 /* Type-printers from Python. */
164 void *py_type_printers = nullptr;
167 /* The return code for some API calls. */
169 enum ext_lang_rc
171 /* The operation completed successfully. */
172 EXT_LANG_RC_OK,
174 /* The operation was not performed (e.g., no pretty-printer). */
175 EXT_LANG_RC_NOP,
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
187 /* A type which holds its extension language specific xmethod worker data. */
189 struct xmethod_worker
191 xmethod_worker (const extension_language_defn *extlang)
192 : m_extlang (extlang)
195 virtual ~xmethod_worker () = default;
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. */
200 virtual value *invoke (value *obj, gdb::array_view<value *> args) = 0;
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. */
206 std::vector<type *> get_arg_types ();
208 /* Return the type of the result of the xmethod encapsulated in this worker.
209 OBJECT and ARGS are the same as for invoke. */
211 type *get_result_type (value *object, gdb::array_view<value *> args);
213 private:
215 /* Return the types of the arguments the method takes. The types
216 are returned in TYPE_ARGS, one per argument. */
218 virtual enum ext_lang_rc do_get_arg_types
219 (std::vector<type *> *type_args) = 0;
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. */
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;
229 /* The language the xmethod worker is implemented in. */
231 const extension_language_defn *m_extlang;
234 typedef std::unique_ptr<xmethod_worker> xmethod_worker_up;
236 /* The interface for gdb's own extension(/scripting) language. */
237 extern const struct extension_language_defn extension_language_gdb;
239 extern const struct extension_language_defn *get_ext_lang_defn
240 (enum extension_language lang);
242 extern const struct extension_language_defn *get_ext_lang_of_file
243 (const char *file);
245 extern int ext_lang_present_p (const struct extension_language_defn *);
247 extern int ext_lang_initialized_p (const struct extension_language_defn *);
249 extern void throw_ext_lang_unsupported
250 (const struct extension_language_defn *);
252 /* Accessors for "public" attributes of the extension language definition. */
254 extern enum extension_language ext_lang_kind
255 (const struct extension_language_defn *);
257 extern const char *ext_lang_name (const struct extension_language_defn *);
259 extern const char *ext_lang_capitalized_name
260 (const struct extension_language_defn *);
262 extern const char *ext_lang_suffix (const struct extension_language_defn *);
264 extern const char *ext_lang_auto_load_suffix
265 (const struct extension_language_defn *);
267 extern script_sourcer_func *ext_lang_script_sourcer
268 (const struct extension_language_defn *);
270 extern objfile_script_sourcer_func *ext_lang_objfile_script_sourcer
271 (const struct extension_language_defn *);
273 extern objfile_script_executor_func *ext_lang_objfile_script_executor
274 (const struct extension_language_defn *);
276 /* Return true if auto-loading of EXTLANG scripts is enabled.
277 False is returned if support for this language isn't compiled in. */
279 extern bool ext_lang_auto_load_enabled (const struct extension_language_defn *);
281 /* Wrappers for each extension language API function that iterate over all
282 extension languages. */
284 extern void ext_lang_initialization (void);
286 /* Shut down all extension languages. */
287 extern void ext_lang_shutdown ();
289 extern void eval_ext_lang_from_control_command (struct command_line *cmd);
291 extern void auto_load_ext_lang_scripts_for_objfile (struct objfile *);
293 extern gdb::unique_xmalloc_ptr<char> apply_ext_lang_type_printers
294 (struct ext_lang_type_printers *, struct type *);
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);
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);
306 extern void preserve_ext_lang_values (struct objfile *, htab_t copied_types);
308 extern const struct extension_language_defn *get_breakpoint_cond_ext_lang
309 (struct breakpoint *b, enum extension_language skip_lang);
311 extern bool breakpoint_ext_lang_cond_says_stop (struct breakpoint *);
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. */
318 extern void get_matching_xmethod_workers
319 (struct type *type, const char *method_name,
320 std::vector<xmethod_worker_up> *workers);
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. */
327 extern std::optional<std::string> ext_lang_colorize
328 (const std::string &filename, const std::string &contents);
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. */
335 extern std::optional<std::string> ext_lang_colorize_disasm
336 (const std::string &content, gdbarch *gdbarch);
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).
342 All arguments are forwarded to extension_language_ops::print_insn, see
343 that function for a full description. */
345 extern std::optional<int> ext_lang_print_insn
346 (struct gdbarch *gdbarch, CORE_ADDR address, struct disassemble_info *info);
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.
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
363 /* Default result. The extension was unable to provide the missing debug
364 info. */
365 ext_lang_missing_debuginfo_result ()
366 { /* Nothing. */ }
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. */
372 ext_lang_missing_debuginfo_result (bool try_again)
373 : m_try_again (try_again)
374 { /* Nothing. */ }
376 /* Look in FILENAME for the missing debug info. */
377 ext_lang_missing_debuginfo_result (std::string &&filename)
378 : m_filename (std::move (filename))
379 { /* Nothing. */ }
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 &
384 filename () const
386 return m_filename;
389 /* Returns true if GDB should look again for the debug information. */
390 const bool
391 try_again () const
393 return m_try_again;
396 private:
397 /* The filename where the missing debuginfo can now be found. */
398 std::string m_filename;
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;
405 /* Called when GDB failed to find any debug information for OBJFILE. */
407 extern ext_lang_missing_debuginfo_result ext_lang_handle_missing_debuginfo
408 (struct objfile *objfile);
410 #if GDB_SELF_TEST
411 namespace selftests {
412 extern void (*hook_set_active_ext_lang) ();
414 #endif
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
421 public:
422 scoped_disable_cooperative_sigint_handling ();
423 ~scoped_disable_cooperative_sigint_handling ();
425 DISABLE_COPY_AND_ASSIGN (scoped_disable_cooperative_sigint_handling);
427 private:
428 struct active_ext_lang_state *m_prev_active_ext_lang_state;
429 bool m_prev_cooperative_sigint_handling_disabled;
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.
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.
442 These functions use the extension_language_ops API to allow extension
443 language(s) and GDB SIGINT handling to coexist seamlessly. */
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. */
450 extern bool check_quit_flag ();
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. */
460 extern void set_quit_flag ();
462 #endif /* EXTENSION_H */