/* Interface between gdb and its extension languages.
- Copyright (C) 2014-2018 Free Software Foundation, Inc.
+ Copyright (C) 2014-2021 Free Software Foundation, Inc.
This file is part of GDB.
#include "event-top.h"
#include "extension.h"
#include "extension-priv.h"
-#include "observer.h"
+#include "observable.h"
#include "cli/cli-script.h"
#include "python/python.h"
#include "guile/guile.h"
-
-/* Iterate over all external extension languages, regardless of whether the
- support has been compiled in or not.
- This does not include GDB's own scripting language. */
-
-#define ALL_EXTENSION_LANGUAGES(i, extlang) \
- for (/*int*/ i = 0, extlang = extension_languages[0]; \
- extlang != NULL; \
- extlang = extension_languages[++i])
-
-/* Iterate over all external extension languages that are supported.
- This does not include GDB's own scripting language. */
-
-#define ALL_ENABLED_EXTENSION_LANGUAGES(i, extlang) \
- for (/*int*/ i = 0, extlang = extension_languages[0]; \
- extlang != NULL; \
- extlang = extension_languages[++i]) \
- if (extlang->ops != NULL)
+#include <array>
static script_sourcer_func source_gdb_script;
static objfile_script_sourcer_func source_gdb_objfile_script;
pretty-printed value is the one that is used. This algorithm is employed
throughout. */
-static const struct extension_language_defn * const extension_languages[] =
+static const std::array<const extension_language_defn *, 2> extension_languages
{
/* To preserve existing behaviour, python should always appear first. */
&extension_language_python,
&extension_language_guile,
- NULL
};
/* Return a pointer to the struct extension_language_defn object of
const struct extension_language_defn *
get_ext_lang_defn (enum extension_language lang)
{
- int i;
- const struct extension_language_defn *extlang;
-
gdb_assert (lang != EXT_LANG_NONE);
if (lang == EXT_LANG_GDB)
return &extension_language_gdb;
- ALL_EXTENSION_LANGUAGES (i, extlang)
+ for (const struct extension_language_defn *extlang : extension_languages)
{
if (extlang->language == lang)
return extlang;
const struct extension_language_defn *
get_ext_lang_of_file (const char *file)
{
- int i;
- const struct extension_language_defn *extlang;
+ if (has_extension (file, extension_language_gdb.suffix))
+ return &extension_language_gdb;
- ALL_EXTENSION_LANGUAGES (i, extlang)
+ for (const struct extension_language_defn *extlang : extension_languages)
{
if (has_extension (file, extlang->suffix))
return extlang;
return extlang->script_ops->objfile_script_executor;
}
-/* Return non-zero if auto-loading of EXTLANG scripts is enabled.
- Zero is returned if support for this language isn't compiled in. */
+/* See extension.h. */
-int
+bool
ext_lang_auto_load_enabled (const struct extension_language_defn *extlang)
{
if (extlang->script_ops == NULL)
- return 0;
+ return false;
/* The extension language is required to implement this function. */
gdb_assert (extlang->script_ops->auto_load_enabled != NULL);
void
finish_ext_lang_initialization (void)
{
- int i;
- const struct extension_language_defn *extlang;
-
- ALL_ENABLED_EXTENSION_LANGUAGES (i, extlang)
+ for (const struct extension_language_defn *extlang : extension_languages)
{
- if (extlang->ops->finish_initialization != NULL)
+ if (extlang->ops != nullptr
+ && extlang->ops->finish_initialization != NULL)
extlang->ops->finish_initialization (extlang);
}
}
void
eval_ext_lang_from_control_command (struct command_line *cmd)
{
- int i;
- const struct extension_language_defn *extlang;
-
- ALL_EXTENSION_LANGUAGES (i, extlang)
+ for (const struct extension_language_defn *extlang : extension_languages)
{
if (extlang->cli_control_type == cmd->control_type)
{
void
auto_load_ext_lang_scripts_for_objfile (struct objfile *objfile)
{
- int i;
- const struct extension_language_defn *extlang;
-
- extlang = &extension_language_gdb;
- if (ext_lang_auto_load_enabled (extlang))
- auto_load_objfile_script (objfile, extlang);
+ const struct extension_language_defn *gdb = &extension_language_gdb;
+ if (ext_lang_auto_load_enabled (gdb))
+ auto_load_objfile_script (objfile, gdb);
- ALL_ENABLED_EXTENSION_LANGUAGES (i, extlang)
+ for (const struct extension_language_defn *extlang : extension_languages)
{
- if (ext_lang_auto_load_enabled (extlang))
+ if (extlang->ops != nullptr
+ && ext_lang_auto_load_enabled (extlang))
auto_load_objfile_script (objfile, extlang);
}
}
We don't know in advance which extension language will provide a
pretty-printer for the type, so all are initialized. */
-struct ext_lang_type_printers *
-start_ext_lang_type_printers (void)
+ext_lang_type_printers::ext_lang_type_printers ()
{
- struct ext_lang_type_printers *printers
- = XCNEW (struct ext_lang_type_printers);
- int i;
- const struct extension_language_defn *extlang;
-
- ALL_ENABLED_EXTENSION_LANGUAGES (i, extlang)
+ for (const struct extension_language_defn *extlang : extension_languages)
{
- if (extlang->ops->start_type_printers != NULL)
- extlang->ops->start_type_printers (extlang, printers);
+ if (extlang->ops != nullptr
+ && extlang->ops->start_type_printers != NULL)
+ extlang->ops->start_type_printers (extlang, this);
}
-
- return printers;
}
/* Iteratively try the type pretty-printers specified by PRINTERS
apply_ext_lang_type_printers (struct ext_lang_type_printers *printers,
struct type *type)
{
- int i;
- const struct extension_language_defn *extlang;
-
- ALL_ENABLED_EXTENSION_LANGUAGES (i, extlang)
+ for (const struct extension_language_defn *extlang : extension_languages)
{
char *result = NULL;
enum ext_lang_rc rc;
- if (extlang->ops->apply_type_printers == NULL)
+ if (extlang->ops == nullptr
+ || extlang->ops->apply_type_printers == NULL)
continue;
rc = extlang->ops->apply_type_printers (extlang, printers, type,
&result);
return NULL;
}
-/* Call this after pretty-printing a type to release all memory held
- by PRINTERS. */
-
-void
-free_ext_lang_type_printers (struct ext_lang_type_printers *printers)
+ext_lang_type_printers::~ext_lang_type_printers ()
{
- int i;
- const struct extension_language_defn *extlang;
-
- ALL_ENABLED_EXTENSION_LANGUAGES (i, extlang)
+ for (const struct extension_language_defn *extlang : extension_languages)
{
- if (extlang->ops->free_type_printers != NULL)
- extlang->ops->free_type_printers (extlang, printers);
+ if (extlang->ops != nullptr
+ && extlang->ops->free_type_printers != NULL)
+ extlang->ops->free_type_printers (extlang, this);
}
-
- xfree (printers);
}
\f
-/* Try to pretty-print a value of type TYPE located at VAL's contents
- buffer + EMBEDDED_OFFSET, which came from the inferior at address
- ADDRESS + EMBEDDED_OFFSET, onto stdio stream STREAM according to
- OPTIONS.
- VAL is the whole object that came from ADDRESS.
- Returns non-zero if the value was successfully pretty-printed.
+/* Try to pretty-print a value onto stdio stream STREAM according to
+ OPTIONS. VAL is the object to print. Returns non-zero if the
+ value was successfully pretty-printed.
Extension languages are tried in the order specified by
extension_languages. The first one to provide a pretty-printed
errors that trigger an exception in the extension language. */
int
-apply_ext_lang_val_pretty_printer (struct type *type,
- LONGEST embedded_offset, CORE_ADDR address,
+apply_ext_lang_val_pretty_printer (struct value *val,
struct ui_file *stream, int recurse,
- struct value *val,
const struct value_print_options *options,
const struct language_defn *language)
{
- int i;
- const struct extension_language_defn *extlang;
-
- ALL_ENABLED_EXTENSION_LANGUAGES (i, extlang)
+ for (const struct extension_language_defn *extlang : extension_languages)
{
enum ext_lang_rc rc;
- if (extlang->ops->apply_val_pretty_printer == NULL)
+ if (extlang->ops == nullptr
+ || extlang->ops->apply_val_pretty_printer == NULL)
continue;
- rc = extlang->ops->apply_val_pretty_printer (extlang, type,
- embedded_offset, address,
- stream, recurse, val,
- options, language);
+ rc = extlang->ops->apply_val_pretty_printer (extlang, val, stream,
+ recurse, options, language);
switch (rc)
{
case EXT_LANG_RC_OK:
rather than trying filters in other extension languages. */
enum ext_lang_bt_status
-apply_ext_lang_frame_filter (struct frame_info *frame, int flags,
+apply_ext_lang_frame_filter (struct frame_info *frame,
+ frame_filter_flags flags,
enum ext_lang_frame_args args_type,
struct ui_out *out,
int frame_low, int frame_high)
{
- int i;
- const struct extension_language_defn *extlang;
-
- ALL_ENABLED_EXTENSION_LANGUAGES (i, extlang)
+ for (const struct extension_language_defn *extlang : extension_languages)
{
enum ext_lang_bt_status status;
- if (extlang->ops->apply_frame_filter == NULL)
+ if (extlang->ops == nullptr
+ || extlang->ops->apply_frame_filter == NULL)
continue;
status = extlang->ops->apply_frame_filter (extlang, frame, flags,
args_type, out,
void
preserve_ext_lang_values (struct objfile *objfile, htab_t copied_types)
{
- int i;
- const struct extension_language_defn *extlang;
-
- ALL_ENABLED_EXTENSION_LANGUAGES (i, extlang)
+ for (const struct extension_language_defn *extlang : extension_languages)
{
- if (extlang->ops->preserve_values != NULL)
+ if (extlang->ops != nullptr
+ && extlang->ops->preserve_values != NULL)
extlang->ops->preserve_values (extlang, objfile, copied_types);
}
}
get_breakpoint_cond_ext_lang (struct breakpoint *b,
enum extension_language skip_lang)
{
- int i;
- const struct extension_language_defn *extlang;
-
- ALL_ENABLED_EXTENSION_LANGUAGES (i, extlang)
+ for (const struct extension_language_defn *extlang : extension_languages)
{
- if (extlang->language != skip_lang
+ if (extlang->ops != nullptr
+ && extlang->language != skip_lang
&& extlang->ops->breakpoint_has_cond != NULL
&& extlang->ops->breakpoint_has_cond (extlang, b))
return extlang;
int
breakpoint_ext_lang_cond_says_stop (struct breakpoint *b)
{
- int i;
- const struct extension_language_defn *extlang;
enum ext_lang_bp_stop stop = EXT_LANG_BP_STOP_UNSET;
- ALL_ENABLED_EXTENSION_LANGUAGES (i, extlang)
+ for (const struct extension_language_defn *extlang : extension_languages)
{
/* There is a rule that a breakpoint can have at most one of any of a
CLI or extension language condition. However, Python hacks in "finish
breakpoints" on top of the "stop" check, so we have to call this for
every language, even if we could first determine whether a "stop"
method exists. */
- if (extlang->ops->breakpoint_cond_says_stop != NULL)
+ if (extlang->ops != nullptr
+ && extlang->ops->breakpoint_cond_says_stop != NULL)
{
enum ext_lang_bp_stop this_stop
= extlang->ops->breakpoint_cond_says_stop (extlang, b);
int
check_quit_flag (void)
{
- int i, result = 0;
- const struct extension_language_defn *extlang;
+ int result = 0;
- ALL_ENABLED_EXTENSION_LANGUAGES (i, extlang)
+ for (const struct extension_language_defn *extlang : extension_languages)
{
- if (extlang->ops->check_quit_flag != NULL)
+ if (extlang->ops != nullptr
+ && extlang->ops->check_quit_flag != NULL)
if (extlang->ops->check_quit_flag (extlang) != 0)
result = 1;
}
get_matching_xmethod_workers (struct type *type, const char *method_name,
std::vector<xmethod_worker_up> *workers)
{
- int i;
- const struct extension_language_defn *extlang;
-
- ALL_ENABLED_EXTENSION_LANGUAGES (i, extlang)
+ for (const struct extension_language_defn *extlang : extension_languages)
{
enum ext_lang_rc rc;
/* If an extension language does not support xmethods, ignore
it. */
- if (extlang->ops->get_matching_xmethod_workers == NULL)
+ if (extlang->ops == nullptr
+ || extlang->ops->get_matching_xmethod_workers == NULL)
continue;
rc = extlang->ops->get_matching_xmethod_workers (extlang,
/* See extension.h. */
-type **
-xmethod_worker::get_arg_types (int *nargs)
+std::vector<type *>
+xmethod_worker::get_arg_types ()
{
- type **type_array = NULL;
+ std::vector<type *> type_array;
- ext_lang_rc rc = do_get_arg_types (nargs, &type_array);
+ ext_lang_rc rc = do_get_arg_types (&type_array);
if (rc == EXT_LANG_RC_ERROR)
error (_("Error while looking for arg types of a xmethod worker "
"defined in %s."), m_extlang->capitalized_name);
/* See extension.h. */
struct type *
-xmethod_worker::get_result_type (value *object, value **args, int nargs)
+xmethod_worker::get_result_type (value *object, gdb::array_view<value *> args)
{
type *result_type;
- ext_lang_rc rc = do_get_result_type (object, args, nargs, &result_type);
+ ext_lang_rc rc = do_get_result_type (object, args, &result_type);
if (rc == EXT_LANG_RC_ERROR)
{
error (_("Error while fetching result type of an xmethod worker "
return result_type;
}
+/* See extension.h. */
+
+gdb::optional<std::string>
+ext_lang_colorize (const std::string &filename, const std::string &contents)
+{
+ gdb::optional<std::string> result;
+
+ for (const struct extension_language_defn *extlang : extension_languages)
+ {
+ if (extlang->ops == nullptr
+ || extlang->ops->colorize == nullptr)
+ continue;
+ result = extlang->ops->colorize (filename, contents);
+ if (result.has_value ())
+ return result;
+ }
+
+ return result;
+}
+
/* Called via an observer before gdb prints its prompt.
Iterate over the extension languages giving them a chance to
change the prompt. The first one to change the prompt wins,
static void
ext_lang_before_prompt (const char *current_gdb_prompt)
{
- int i;
- const struct extension_language_defn *extlang;
-
- ALL_ENABLED_EXTENSION_LANGUAGES (i, extlang)
+ for (const struct extension_language_defn *extlang : extension_languages)
{
enum ext_lang_rc rc;
- if (extlang->ops->before_prompt == NULL)
+ if (extlang->ops == nullptr
+ || extlang->ops->before_prompt == NULL)
continue;
rc = extlang->ops->before_prompt (extlang, current_gdb_prompt);
switch (rc)
}
}
+void _initialize_extension ();
void
-_initialize_extension (void)
+_initialize_extension ()
{
- observer_attach_before_prompt (ext_lang_before_prompt);
+ gdb::observers::before_prompt.attach (ext_lang_before_prompt, "extension");
}