From 6dddc817c1680fc97756cbcb017fcf306fa5d07c Mon Sep 17 00:00:00 2001 From: Doug Evans Date: Wed, 5 Feb 2014 19:27:58 -0800 Subject: Extension Language API * configure.ac (libpython checking): Remove all but python.o from CONFIG_OBS. Remove all but python.c from CONFIG_SRCS. * configure: Regenerate. * Makefile.in (SFILES): Add extension.c. (HFILES_NO_SRCDIR): Add extension.h, extension-priv.h (COMMON_OBS): Add extension.o. * extension.h: New file. * extension-priv.h: New file. * extension.c: New file. * python/python-internal.h: #include "extension.h". (gdbpy_auto_load_enabled): Declare. (gdbpy_apply_val_pretty_printer): Declare. (gdbpy_apply_frame_filter): Declare. (gdbpy_preserve_values): Declare. (gdbpy_breakpoint_cond_says_stop): Declare. (gdbpy_breakpoint_has_cond): Declare. (void source_python_script_for_objfile): Delete. * python/python.c: #include "extension-priv.h". Delete inclusion of "observer.h". (extension_language_python): Moved here and renamed from script_language_python in py-auto-load.c. Redefined to be of type extension_language_defn. (python_extension_script_ops): New global. (python_extension_ops): New global. (struct python_env): New member previous_active. (restore_python_env): Call restore_active_ext_lang. (ensure_python_env): Call set_active_ext_lang. (gdbpy_clear_quit_flag): Renamed from clear_quit_flag, made static. New arg extlang. (gdbpy_set_quit_flag): Renamed from set_quit_flag, made static. New arg extlang. (gdbpy_check_quit_flag): Renamed from check_quit_flag, made static. New arg extlang. (gdbpy_eval_from_control_command): Renamed from eval_python_from_control_command, made static. New arg extlang. (gdbpy_source_script) Renamed from source_python_script, made static. New arg extlang. (gdbpy_before_prompt_hook): Renamed from before_prompt_hook. Change result to int. New arg extlang. (gdbpy_source_objfile_script): Renamed from source_python_script_for_objfile, made static. New arg extlang. (gdbpy_start_type_printers): Renamed from start_type_printers, made static. New args extlang, extlang_printers. Change result type to "void". (gdbpy_apply_type_printers): Renamed from apply_type_printers, made static. New arg extlang. Rename arg printers to extlang_printers and change type to ext_lang_type_printers *. (gdbpy_free_type_printers): Renamed from free_type_printers, made static. Replace argument arg with extlang, extlang_printers. (!HAVE_PYTHON, eval_python_from_control_command): Delete. (!HAVE_PYTHON, source_python_script): Delete. (!HAVE_PYTHON, gdbpy_should_stop): Delete. (!HAVE_PYTHON, gdbpy_breakpoint_has_py_cond): Delete. (!HAVE_PYTHON, start_type_printers): Delete. (!HAVE_PYTHON, apply_type_printers): Delete. (!HAVE_PYTHON, free_type_printers): Delete. (_initialize_python): Delete call to observer_attach_before_prompt. (finalize_python): Set/restore active extension language. (gdbpy_finish_initialization) Renamed from finish_python_initialization, made static. New arg extlang. (gdbpy_initialized): New function. * python/python.h: #include "extension.h". Delete #include "value.h", "mi/mi-cmds.h". (extension_language_python): Declare. (GDBPY_AUTO_FILE_NAME): Delete. (enum py_bt_status): Moved to extension.h and renamed to ext_lang_bt_status. (enum frame_filter_flags): Moved to extension.h. (enum py_frame_args): Moved to extension.h and renamed to ext_lang_frame_args. (finish_python_initialization): Delete. (eval_python_from_control_command): Delete. (source_python_script): Delete. (apply_val_pretty_printer): Delete. (apply_frame_filter): Delete. (preserve_python_values): Delete. (gdbpy_script_language_defn): Delete. (gdbpy_should_stop, gdbpy_breakpoint_has_py_cond): Delete. (start_type_printers, apply_type_printers, free_type_printers): Delete. * auto-load.c: #include "extension.h". (GDB_AUTO_FILE_NAME): Delete. (auto_load_gdb_scripts_enabled): Make public. New arg extlang. (script_language_gdb): Delete, moved to extension.c and renamed to extension_language_gdb. (source_gdb_script_for_objfile): Delete. (auto_load_pspace_info): New member unsupported_script_warning_printed. (loaded_script): Change type of language member to struct extension_language_defn *. (init_loaded_scripts_info): Initialize unsupported_script_warning_printed. (maybe_add_script): Make static. Change type of language arg to struct extension_language_defn *. (clear_section_scripts): Reset unsupported_script_warning_printed. (auto_load_objfile_script_1): Rewrite to use extension language API. (auto_load_objfile_script): Make public. Remove support-compiled-in and auto-load-enabled checks, moved to auto_load_scripts_for_objfile. (source_section_scripts): Rewrite to use extension language API. (load_auto_scripts_for_objfile): Rewrite to use auto_load_scripts_for_objfile. (collect_matching_scripts_data): Change type of language member to struct extension_language_defn *. (auto_load_info_scripts): Change type of language arg to struct extension_language_defn *. (unsupported_script_warning_print): New function. (script_not_found_warning_print): Make static. (_initialize_auto_load): Rewrite construction of scripts-directory help. * auto-load.h (struct objfile): Add forward decl. (struct script_language): Delete. (struct auto_load_pspace_info): Add forward decl. (struct extension_language_defn): Add forward decl. (maybe_add_script): Delete. (auto_load_objfile_script): Declare. (script_not_found_warning_print): Delete. (auto_load_info_scripts): Update prototype. (auto_load_gdb_scripts_enabled): Declare. * python/py-auto-load.c (gdbpy_auto_load_enabled): Renamed from auto_load_python_scripts_enabled and made public. (script_language_python): Delete, moved to python.c. (gdbpy_script_language_defn): Delete. (info_auto_load_python_scripts): Update to use extension_language_python. * breakpoint.c (condition_command): Replace call to gdbpy_breakpoint_has_py_cond with call to get_breakpoint_cond_ext_lang. (bpstat_check_breakpoint_conditions): Replace call to gdbpy_should_stop with call to breakpoint_ext_lang_cond_says_stop. * python/py-breakpoint.c (gdbpy_breakpoint_cond_says_stop): Renamed from gdbpy_should_stop. Change result type to enum scr_bp_stop. New arg slang. Return SCR_BP_STOP_UNSET if py_bp_object is NULL. (gdbpy_breakpoint_has_cond): Renamed from gdbpy_breakpoint_has_py_cond. New arg slang. (local_setattro): Print name of extension language with existing stop condition. * valprint.c (val_print, value_print): Update to call apply_ext_lang_val_pretty_printer. * cp-valprint.c (cp_print_value): Update call to apply_ext_lang_val_pretty_printer. * python/py-prettyprint.c: Remove #ifdef HAVE_PYTHON. (gdbpy_apply_val_pretty_printer): Renamed from apply_val_pretty_printer. New arg extlang. (!HAVE_PYTHON, apply_val_pretty_printer): Delete. * cli/cli-cmds.c (source_script_from_stream): Rewrite to use extension language API. * cli/cli-script.c (execute_control_command): Update to call eval_ext_lang_from_control_command. * mi/mi-cmd-stack.c (mi_cmd_stack_list_frames): Update to use enum ext_lang_bt_status values. Update call to apply_ext_lang_frame_filter. (mi_cmd_stack_list_locals): Ditto. (mi_cmd_stack_list_args): Ditto. (mi_cmd_stack_list_variables): Ditto. * mi/mi-main.c: Delete #include "python/python-internal.h". Add #include "extension.h". (mi_cmd_list_features): Replace reference to python internal variable gdb_python_initialized with call to ext_lang_initialized_p. * stack.c (backtrace_command_1): Update to use enum ext_lang_bt_status. Update to use enum ext_lang_frame_args. Update to call apply_ext_lang_frame_filter. * python/py-framefilter.c (extract_sym): Update to use enum ext_lang_bt_status. (extract_value, py_print_type, py_print_value): Ditto. (py_print_single_arg, enumerate_args, enumerate_locals): Ditto. (py_mi_print_variables, py_print_locals, py_print_args): Ditto. (py_print_frame): Ditto. (gdbpy_apply_frame_filter): Renamed from apply_frame_filter. New arg extlang. Update to use enum ext_lang_bt_status. * top.c (gdb_init): Delete #ifdef HAVE_PYTHON call to finish_python_initialization. Replace with call to finish_ext_lang_initialization. * typeprint.c (do_free_global_table): Update to call free_ext_lang_type_printers. (create_global_typedef_table): Update to call start_ext_lang_type_printers. (find_global_typedef): Update to call apply_ext_lang_type_printers. * typeprint.h (struct ext_lang_type_printers): Add forward decl. (type_print_options): Change type of global_printers from "void *" to "struct ext_lang_type_printers *". * value.c (preserve_values): Update to call preserve_ext_lang_values. * python/py-value.c: Remove #ifdef HAVE_PYTHON. (gdbpy_preserve_values): Renamed from preserve_python_values. New arg extlang. (!HAVE_PYTHON, preserve_python_values): Delete. * utils.c (quit_flag): Delete, moved to extension.c. (clear_quit_flag, set_quit_flag, check_quit_flag): Delete, moved to extension.c. * eval.c: Delete #include "python/python.h". * main.c: Delete #include "python/python.h". * defs.h: Update comment. testsuite/ * gdb.python/py-breakpoint.exp (test_bkpt_eval_funcs): Update expected output. * gdb.gdb/python-interrupts.exp: New file. --- gdb/auto-load.c | 210 ++++++++++++++++++++++++++++++++------------------------ 1 file changed, 122 insertions(+), 88 deletions(-) (limited to 'gdb/auto-load.c') diff --git a/gdb/auto-load.c b/gdb/auto-load.c index 46d063c9dd3..a2f6fb92022 100644 --- a/gdb/auto-load.c +++ b/gdb/auto-load.c @@ -20,7 +20,6 @@ #include "defs.h" #include "auto-load.h" #include "progspace.h" -#include "python/python.h" #include "gdb_regex.h" #include "ui-out.h" #include "filenames.h" @@ -39,6 +38,8 @@ #include "fnmatch.h" #include "top.h" #include "filestuff.h" +#include "extension.h" +#include "python/python.h" /* The section to look in for auto-loaded scripts (in file formats that support sections). @@ -48,12 +49,14 @@ followed by the path of a python script to load. */ #define AUTO_SECTION_NAME ".debug_gdb_scripts" -/* The suffix of per-objfile scripts to auto-load as non-Python command files. - E.g. When the program loads libfoo.so, look for libfoo-gdb.gdb. */ -#define GDB_AUTO_FILE_NAME "-gdb.gdb" +static int maybe_add_script (struct auto_load_pspace_info *pspace_info, + int loaded, const char *name, + const char *full_path, + const struct extension_language_defn *language); + +static int unsupported_script_warning_print (struct auto_load_pspace_info *); -static void source_gdb_script_for_objfile (struct objfile *objfile, FILE *file, - const char *filename); +static int script_not_found_warning_print (struct auto_load_pspace_info *); /* Value of the 'set debug auto-load' configuration variable. */ static int debug_auto_load = 0; @@ -89,8 +92,8 @@ show_auto_load_gdb_scripts (struct ui_file *file, int from_tty, /* Return non-zero if auto-loading gdb scripts is enabled. */ -static int -auto_load_gdb_scripts_enabled (void) +int +auto_load_gdb_scripts_enabled (const struct extension_language_defn *extlang) { return auto_load_gdb_scripts; } @@ -516,29 +519,6 @@ For more information about this security protection see the\n\ return 0; } -/* Definition of script language for GDB canned sequences of commands. */ - -static const struct script_language script_language_gdb = -{ - "gdb", - GDB_AUTO_FILE_NAME, - auto_load_gdb_scripts_enabled, - source_gdb_script_for_objfile -}; - -static void -source_gdb_script_for_objfile (struct objfile *objfile, FILE *file, - const char *filename) -{ - volatile struct gdb_exception e; - - TRY_CATCH (e, RETURN_MASK_ALL) - { - script_from_file (file, filename); - } - exception_print (gdb_stderr, e); -} - /* For scripts specified in .debug_gdb_scripts, multiple objfiles may load the same script. There's no point in loading the script multiple times, and there can be a lot of objfiles and scripts, so we keep track of scripts @@ -549,6 +529,10 @@ struct auto_load_pspace_info /* For each program space we keep track of loaded scripts. */ struct htab *loaded_scripts; + /* Non-zero if we've issued the warning about an auto-load script not being + supported. We only want to issue this warning once. */ + int unsupported_script_warning_printed; + /* Non-zero if we've issued the warning about an auto-load script not being found. We only want to issue this warning once. */ int script_not_found_warning_printed; @@ -568,7 +552,7 @@ struct loaded_script /* Non-zero if this script has been loaded. */ int loaded; - const struct script_language *language; + const struct extension_language_defn *language; }; /* Per-program-space data key. */ @@ -638,6 +622,7 @@ init_loaded_scripts_info (struct auto_load_pspace_info *pspace_info) eq_loaded_script_entry, xfree); + pspace_info->unsupported_script_warning_printed = FALSE; pspace_info->script_not_found_warning_printed = FALSE; } @@ -661,10 +646,10 @@ get_auto_load_pspace_data_for_loading (struct program_space *pspace) been found). FULL_PATH is NULL if the script wasn't found. The result is true if the script was already in the hash table. */ -int +static int maybe_add_script (struct auto_load_pspace_info *pspace_info, int loaded, const char *name, const char *full_path, - const struct script_language *language) + const struct extension_language_defn *language) { struct htab *htab = pspace_info->loaded_scripts; struct loaded_script **slot, entry; @@ -716,6 +701,7 @@ clear_section_scripts (void) { htab_delete (info->loaded_scripts); info->loaded_scripts = NULL; + info->unsupported_script_warning_printed = FALSE; info->script_not_found_warning_printed = FALSE; } } @@ -726,17 +712,18 @@ clear_section_scripts (void) static int auto_load_objfile_script_1 (struct objfile *objfile, const char *realname, - const struct script_language *language) + const struct extension_language_defn *language) { char *filename, *debugfile; int len, retval; FILE *input; struct cleanup *cleanups; + const char *suffix = ext_lang_auto_load_suffix (language); len = strlen (realname); - filename = xmalloc (len + strlen (language->suffix) + 1); + filename = xmalloc (len + strlen (suffix) + 1); memcpy (filename, realname, len); - strcpy (filename + len, language->suffix); + strcpy (filename + len, suffix); cleanups = make_cleanup (xfree, filename); @@ -794,8 +781,8 @@ auto_load_objfile_script_1 (struct objfile *objfile, const char *realname, = file_is_auto_load_safe (filename, _("auto-load: Loading %s script \"%s\"" " by extension for objfile \"%s\".\n"), - language->name, filename, - objfile_name (objfile)); + ext_lang_name (language), + filename, objfile_name (objfile)); /* Add this script to the hash table too so "info auto-load ${lang}-scripts" can print it. */ @@ -809,7 +796,16 @@ auto_load_objfile_script_1 (struct objfile *objfile, const char *realname, and these scripts are required to be idempotent under multiple loads anyway. */ if (is_safe) - language->source_script_for_objfile (objfile, input, debugfile); + { + objfile_script_sourcer_func *sourcer + = ext_lang_objfile_script_sourcer (language); + + /* We shouldn't get here if support for the language isn't + compiled in. And the extension language is required to implement + this function. */ + gdb_assert (sourcer != NULL); + sourcer (language, objfile, input, debugfile); + } retval = 1; } @@ -823,21 +819,12 @@ auto_load_objfile_script_1 (struct objfile *objfile, const char *realname, /* Look for the auto-load script in LANGUAGE associated with OBJFILE and load it. */ -static void +void auto_load_objfile_script (struct objfile *objfile, - const struct script_language *language) + const struct extension_language_defn *language) { - char *realname; - struct cleanup *cleanups; - - /* Skip this script if support has not been compiled in or - auto-loading it has been disabled. */ - if (language == NULL - || !language->auto_load_enabled ()) - return; - - realname = gdb_realpath (objfile_name (objfile)); - cleanups = make_cleanup (xfree, realname); + char *realname = gdb_realpath (objfile_name (objfile)); + struct cleanup *cleanups = make_cleanup (xfree, realname); if (!auto_load_objfile_script_1 (objfile, realname, language)) { @@ -892,7 +879,9 @@ source_section_scripts (struct objfile *objfile, const char *section_name, struct cleanup *back_to; /* At the moment we only support python scripts in .debug_gdb_scripts, but that can change. */ - const struct script_language *language = gdbpy_script_language_defn (); + const struct extension_language_defn *language + = &extension_language_python; + objfile_script_sourcer_func *sourcer; if (*p != 1) { @@ -922,10 +911,34 @@ source_section_scripts (struct objfile *objfile, const char *section_name, continue; } - /* Skip this script if support has not been compiled in or - auto-loading it has been disabled. */ - if (language == NULL - || !language->auto_load_enabled ()) + /* Until we support more types of records in .debug_gdb_scripts we do + all the processing here. The expectation is to add a new + extension_language_script_ops "method" that handles all the records + for the language. For now we can just use + extension_language_script_ops.objfile_script_sourcer. */ + + /* Skip this script if support is not compiled in. */ + sourcer = ext_lang_objfile_script_sourcer (language); + if (sourcer == NULL) + { + /* We don't throw an error, the program is still debuggable. */ + if (!unsupported_script_warning_print (pspace_info)) + { + warning (_("Unsupported auto-load scripts referenced in" + " %s section\n" + "of file %s.\n" + "Use `info auto-load %s-scripts [REGEXP]'" + " to list them."), + section_name, objfile_name (objfile), + ext_lang_name (language)); + } + /* We *could* still try to open it, but there's no point. */ + maybe_add_script (pspace_info, 0, file, NULL, language); + continue; + } + + /* Skip this script if auto-loading it has been disabled. */ + if (!ext_lang_auto_load_enabled (language)) { /* No message is printed, just skip it. */ continue; @@ -944,8 +957,8 @@ source_section_scripts (struct objfile *objfile, const char *section_name, _("auto-load: Loading %s script " "\"%s\" from section \"%s\" of " "objfile \"%s\".\n"), - language->name, full_path, section_name, - objfile_name (objfile))) + ext_lang_name (language), full_path, + section_name, objfile_name (objfile))) opened = 0; } else @@ -964,7 +977,8 @@ source_section_scripts (struct objfile *objfile, const char *section_name, warning (_("Missing auto-load scripts referenced in section %s\n\ of file %s\n\ Use `info auto-load %s-scripts [REGEXP]' to list them."), - section_name, objfile_name (objfile), language->name); + section_name, objfile_name (objfile), + ext_lang_name (language)); } in_hash_table = maybe_add_script (pspace_info, opened, file, full_path, @@ -972,10 +986,7 @@ Use `info auto-load %s-scripts [REGEXP]' to list them."), /* If this file is not currently loaded, load it. */ if (opened && !in_hash_table) - { - gdb_assert (language->source_script_for_objfile != NULL); - language->source_script_for_objfile (objfile, stream, full_path); - } + sourcer (language, objfile, stream, full_path); do_cleanups (back_to); } @@ -1020,9 +1031,9 @@ load_auto_scripts_for_objfile (struct objfile *objfile) if (!global_auto_load || (objfile->flags & OBJF_NOT_FILENAME) != 0) return; - /* Load any scripts for this objfile. e.g. foo-gdb.gdb, foo-gdb.py. */ - auto_load_objfile_script (objfile, &script_language_gdb); - auto_load_objfile_script (objfile, gdbpy_script_language_defn ()); + /* Load any extension language scripts for this objfile. + E.g., foo-gdb.gdb, foo-gdb.py. */ + auto_load_ext_lang_scripts_for_objfile (objfile); /* Load any scripts mentioned in AUTO_SECTION_NAME (.debug_gdb_scripts). */ auto_load_section_scripts (objfile, AUTO_SECTION_NAME); @@ -1056,7 +1067,7 @@ struct collect_matching_scripts_data { VEC (loaded_script_ptr) **scripts_p; - const struct script_language *language; + const struct extension_language_defn *language; }; /* Traversal function for htab_traverse. @@ -1122,7 +1133,7 @@ char auto_load_info_scripts_pattern_nl[] = ""; void auto_load_info_scripts (char *pattern, int from_tty, - const struct script_language *language) + const struct extension_language_defn *language) { struct ui_out *uiout = current_uiout; struct auto_load_pspace_info *pspace_info; @@ -1205,7 +1216,7 @@ auto_load_info_scripts (char *pattern, int from_tty, static void info_auto_load_gdb_scripts (char *pattern, int from_tty) { - auto_load_info_scripts (pattern, from_tty, &script_language_gdb); + auto_load_info_scripts (pattern, from_tty, &extension_language_gdb); } /* Implement 'info auto-load local-gdbinit'. */ @@ -1223,11 +1234,25 @@ info_auto_load_local_gdbinit (char *args, int from_tty) auto_load_local_gdbinit_pathname); } +/* Return non-zero if UNSUPPORTED_SCRIPT_WARNING_PRINTED of PSPACE_INFO was + unset before calling this function. Always set + UNSUPPORTED_SCRIPT_WARNING_PRINTED of PSPACE_INFO. */ + +static int +unsupported_script_warning_print (struct auto_load_pspace_info *pspace_info) +{ + int retval = !pspace_info->unsupported_script_warning_printed; + + pspace_info->unsupported_script_warning_printed = 1; + + return retval; +} + /* Return non-zero if SCRIPT_NOT_FOUND_WARNING_PRINTED of PSPACE_INFO was unset before calling this function. Always set SCRIPT_NOT_FOUND_WARNING_PRINTED of PSPACE_INFO. */ -int +static int script_not_found_warning_print (struct auto_load_pspace_info *pspace_info) { int retval = !pspace_info->script_not_found_warning_printed; @@ -1369,7 +1394,7 @@ void _initialize_auto_load (void) { struct cmd_list_element *cmd; - char *scripts_directory_help; + char *scripts_directory_help, *gdb_name_help, *python_name_help; auto_load_pspace_data = register_program_space_data_with_cleanup (NULL, @@ -1413,27 +1438,34 @@ Usage: info auto-load local-gdbinit"), auto_load_info_cmdlist_get ()); auto_load_dir = xstrdup (AUTO_LOAD_DIR); - scripts_directory_help = xstrprintf ( + + gdb_name_help + = xstrprintf (_("\ +GDB scripts: OBJFILE%s\n"), + ext_lang_auto_load_suffix (&extension_language_gdb)); + python_name_help = NULL; #ifdef HAVE_PYTHON - _("\ -Automatically loaded Python scripts (named OBJFILE%s) and GDB scripts\n\ -(named OBJFILE%s) are located in one of the directories listed by this\n\ -option.\n\ -%s"), - GDBPY_AUTO_FILE_NAME, -#else - _("\ -Automatically loaded GDB scripts (named OBJFILE%s) are located in one\n\ -of the directories listed by this option.\n\ -%s"), + python_name_help + = xstrprintf (_("\ +Python scripts: OBJFILE%s\n"), + ext_lang_auto_load_suffix (&extension_language_python)); #endif - GDB_AUTO_FILE_NAME, - _("\ + scripts_directory_help + = xstrprintf (_("\ +Automatically loaded scripts are located in one of the directories listed\n\ +by this option.\n\ +\n\ +Script names:\n\ +%s%s\ +\n\ This option is ignored for the kinds of scripts \ having 'set auto-load ... off'.\n\ Directories listed here need to be present also \ in the 'set auto-load safe-path'\n\ -option.")); +option."), + gdb_name_help, + python_name_help ? python_name_help : ""); + add_setshow_optional_filename_cmd ("scripts-directory", class_support, &auto_load_dir, _("\ Set the list of directories from which to load auto-loaded scripts."), _("\ @@ -1443,6 +1475,8 @@ Show the list of directories from which to load auto-loaded scripts."), auto_load_set_cmdlist_get (), auto_load_show_cmdlist_get ()); xfree (scripts_directory_help); + xfree (python_name_help); + xfree (gdb_name_help); auto_load_safe_path = xstrdup (AUTO_LOAD_SAFE_PATH); auto_load_safe_path_vec_update (); -- cgit v1.2.1