1 /* Interface between gdb and its extension languages.
3 Copyright (C) 2014-2020 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/>. */
23 #include "mi/mi-cmds.h" /* For PRINT_NO_VALUES, etc. */
24 #include "gdbsupport/array-view.h"
25 #include "gdbsupport/gdb_optional.h"
32 struct extension_language_defn
;
37 struct value_print_options
;
39 /* A function to load and process a script file.
40 The file has been opened and is ready to be read from the beginning.
41 Any exceptions are not caught, and are passed to the caller. */
42 typedef void script_sourcer_func (const struct extension_language_defn
*,
43 FILE *stream
, const char *filename
);
45 /* A function to load and process a script for an objfile.
46 The file has been opened and is ready to be read from the beginning.
47 Any exceptions are not caught, and are passed to the caller. */
48 typedef void objfile_script_sourcer_func
49 (const struct extension_language_defn
*,
50 struct objfile
*, FILE *stream
, const char *filename
);
52 /* A function to execute a script for an objfile.
53 Any exceptions are not caught, and are passed to the caller. */
54 typedef void objfile_script_executor_func
55 (const struct extension_language_defn
*,
56 struct objfile
*, const char *name
, const char *script
);
58 /* Enum of each extension(/scripting) language. */
60 enum extension_language
68 /* Extension language frame-filter status return values. */
70 enum ext_lang_bt_status
72 /* Return when an error has occurred in processing frame filters,
73 or when printing the stack. */
74 EXT_LANG_BT_ERROR
= -1,
76 /* Return from internal routines to indicate that the function
80 /* Return when the frame filter process is complete, but there
81 were no filter registered and enabled to process. */
82 EXT_LANG_BT_NO_FILTERS
= 2
85 /* Flags to pass to apply_extlang_frame_filter. */
87 enum frame_filter_flag
89 /* Set this flag if frame level is to be printed. */
92 /* Set this flag if frame information is to be printed. */
93 PRINT_FRAME_INFO
= 1 << 1,
95 /* Set this flag if frame arguments are to be printed. */
98 /* Set this flag if frame locals are to be printed. */
99 PRINT_LOCALS
= 1 << 3,
101 /* Set this flag if a "More frames" message is to be printed. */
102 PRINT_MORE_FRAMES
= 1 << 4,
104 /* Set this flag if elided frames should not be printed. */
108 DEF_ENUM_FLAGS_TYPE (enum frame_filter_flag
, frame_filter_flags
);
110 /* A choice of the different frame argument printing strategies that
111 can occur in different cases of frame filter instantiation. */
113 enum ext_lang_frame_args
115 /* Print no values for arguments when invoked from the MI. */
116 NO_VALUES
= PRINT_NO_VALUES
,
118 MI_PRINT_ALL_VALUES
= PRINT_ALL_VALUES
,
120 /* Print only simple values (what MI defines as "simple") for
121 arguments when invoked from the MI. */
122 MI_PRINT_SIMPLE_VALUES
= PRINT_SIMPLE_VALUES
,
124 /* Print only scalar values for arguments when invoked from the CLI. */
127 /* Print all values for arguments when invoked from the CLI. */
130 /* Only indicate the presence of arguments when invoked from the CLI. */
134 /* The possible results of
135 extension_language_ops.breakpoint_cond_says_stop. */
137 enum ext_lang_bp_stop
139 /* No "stop" condition is set. */
140 EXT_LANG_BP_STOP_UNSET
,
142 /* A "stop" condition is set, and it says "don't stop". */
145 /* A "stop" condition is set, and it says "stop". */
149 /* Table of type printers associated with the global typedef table. */
151 struct ext_lang_type_printers
153 ext_lang_type_printers ();
154 ~ext_lang_type_printers ();
156 DISABLE_COPY_AND_ASSIGN (ext_lang_type_printers
);
158 /* Type-printers from Python. */
159 void *py_type_printers
= nullptr;
162 /* The return code for some API calls. */
166 /* The operation completed successfully. */
169 /* The operation was not performed (e.g., no pretty-printer). */
172 /* There was an error (e.g., Python error while printing a value).
173 When an error occurs no further extension languages are tried.
174 This is to preserve existing behaviour, and because it's convenient
175 for Python developers.
176 Note: This is different than encountering a memory error trying to read
177 a value for pretty-printing. Here we're referring to, e.g., programming
178 errors that trigger an exception in the extension language. */
182 /* A type which holds its extension language specific xmethod worker data. */
184 struct xmethod_worker
186 xmethod_worker (const extension_language_defn
*extlang
)
187 : m_extlang (extlang
)
190 virtual ~xmethod_worker () = default;
192 /* Invoke the xmethod encapsulated in this worker and return the result.
193 The method is invoked on OBJ with arguments in the ARGS array. */
195 virtual value
*invoke (value
*obj
, gdb::array_view
<value
*> args
) = 0;
197 /* Return the arg types of the xmethod encapsulated in this worker.
198 The type of the 'this' object is returned as the first element of
201 std::vector
<type
*> get_arg_types ();
203 /* Return the type of the result of the xmethod encapsulated in this worker.
204 OBJECT and ARGS are the same as for invoke. */
206 type
*get_result_type (value
*object
, gdb::array_view
<value
*> args
);
210 /* Return the types of the arguments the method takes. The types
211 are returned in TYPE_ARGS, one per argument. */
213 virtual enum ext_lang_rc do_get_arg_types
214 (std::vector
<type
*> *type_args
) = 0;
216 /* Fetch the type of the result of the method implemented by this
217 worker. OBJECT and ARGS are the same as for the invoked method.
218 The result type is stored in *RESULT_TYPE. */
220 virtual enum ext_lang_rc do_get_result_type
221 (struct value
*obj
, gdb::array_view
<value
*> args
,
222 struct type
**result_type_ptr
) = 0;
224 /* The language the xmethod worker is implemented in. */
226 const extension_language_defn
*m_extlang
;
229 typedef std::unique_ptr
<xmethod_worker
> xmethod_worker_up
;
231 /* The interface for gdb's own extension(/scripting) language. */
232 extern const struct extension_language_defn extension_language_gdb
;
234 extern const struct extension_language_defn
*get_ext_lang_defn
235 (enum extension_language lang
);
237 extern const struct extension_language_defn
*get_ext_lang_of_file
240 extern int ext_lang_present_p (const struct extension_language_defn
*);
242 extern int ext_lang_initialized_p (const struct extension_language_defn
*);
244 extern void throw_ext_lang_unsupported
245 (const struct extension_language_defn
*);
247 /* Accessors for "public" attributes of the extension language definition. */
249 extern enum extension_language ext_lang_kind
250 (const struct extension_language_defn
*);
252 extern const char *ext_lang_name (const struct extension_language_defn
*);
254 extern const char *ext_lang_capitalized_name
255 (const struct extension_language_defn
*);
257 extern const char *ext_lang_suffix (const struct extension_language_defn
*);
259 extern const char *ext_lang_auto_load_suffix
260 (const struct extension_language_defn
*);
262 extern script_sourcer_func
*ext_lang_script_sourcer
263 (const struct extension_language_defn
*);
265 extern objfile_script_sourcer_func
*ext_lang_objfile_script_sourcer
266 (const struct extension_language_defn
*);
268 extern objfile_script_executor_func
*ext_lang_objfile_script_executor
269 (const struct extension_language_defn
*);
271 extern int ext_lang_auto_load_enabled (const struct extension_language_defn
*);
273 /* Wrappers for each extension language API function that iterate over all
274 extension languages. */
276 extern void finish_ext_lang_initialization (void);
278 extern void eval_ext_lang_from_control_command (struct command_line
*cmd
);
280 extern void auto_load_ext_lang_scripts_for_objfile (struct objfile
*);
282 extern char *apply_ext_lang_type_printers (struct ext_lang_type_printers
*,
285 extern int apply_ext_lang_val_pretty_printer
286 (struct value
*value
, struct ui_file
*stream
, int recurse
,
287 const struct value_print_options
*options
,
288 const struct language_defn
*language
);
290 extern enum ext_lang_bt_status apply_ext_lang_frame_filter
291 (struct frame_info
*frame
, frame_filter_flags flags
,
292 enum ext_lang_frame_args args_type
,
293 struct ui_out
*out
, int frame_low
, int frame_high
);
295 extern void preserve_ext_lang_values (struct objfile
*, htab_t copied_types
);
297 extern const struct extension_language_defn
*get_breakpoint_cond_ext_lang
298 (struct breakpoint
*b
, enum extension_language skip_lang
);
300 extern int breakpoint_ext_lang_cond_says_stop (struct breakpoint
*);
302 /* If a method with name METHOD_NAME is to be invoked on an object of type
303 TYPE, then all extension languages are searched for implementations of
304 methods with name METHOD_NAME. All matches found are appended to the WORKERS
307 extern void get_matching_xmethod_workers
308 (struct type
*type
, const char *method_name
,
309 std::vector
<xmethod_worker_up
> *workers
);
311 /* Try to colorize some source code. FILENAME is the name of the file
312 holding the code. CONTENTS is the source code itself. This will
313 either a colorized (using ANSI terminal escapes) version of the
314 source code, or an empty value if colorizing could not be done. */
316 extern gdb::optional
<std::string
> ext_lang_colorize
317 (const std::string
&filename
, const std::string
&contents
);
319 #endif /* EXTENSION_H */