1 /* GDB routines for supporting auto-loaded scripts.
3 Copyright (C) 2012 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/>. */
21 #include "auto-load.h"
22 #include "progspace.h"
23 #include "python/python.h"
24 #include "gdb_regex.h"
26 #include "filenames.h"
30 #include "exceptions.h"
31 #include "cli/cli-script.h"
33 #include "cli/cli-decode.h"
34 #include "cli/cli-setshow.h"
36 #include "readline/tilde.h"
37 #include "completer.h"
39 /* The suffix of per-objfile scripts to auto-load as non-Python command files.
40 E.g. When the program loads libfoo.so, look for libfoo-gdb.gdb. */
41 #define GDB_AUTO_FILE_NAME "-gdb.gdb"
43 static void source_gdb_script_for_objfile (struct objfile *objfile, FILE *file,
44 const char *filename);
46 /* Value of the 'set debug auto-load' configuration variable. */
47 static int debug_auto_load = 0;
49 /* "show" command for the debug_auto_load configuration variable. */
52 show_debug_auto_load (struct ui_file *file, int from_tty,
53 struct cmd_list_element *c, const char *value)
55 fprintf_filtered (file, _("Debugging output for files "
56 "of 'set auto-load ...' is %s.\n"),
60 /* User-settable option to enable/disable auto-loading of GDB_AUTO_FILE_NAME
62 set auto-load gdb-scripts on|off
63 This is true if we should auto-load associated scripts when an objfile
64 is opened, false otherwise. */
65 static int auto_load_gdb_scripts = 1;
67 /* "show" command for the auto_load_gdb_scripts configuration variable. */
70 show_auto_load_gdb_scripts (struct ui_file *file, int from_tty,
71 struct cmd_list_element *c, const char *value)
73 fprintf_filtered (file, _("Auto-loading of canned sequences of commands "
78 /* Internal-use flag to enable/disable auto-loading.
79 This is true if we should auto-load python code when an objfile is opened,
82 Both auto_load_scripts && global_auto_load must be true to enable
85 This flag exists to facilitate deferring auto-loading during start-up
86 until after ./.gdbinit has been read; it may augment the search directories
87 used to find the scripts. */
88 int global_auto_load = 1;
90 /* Auto-load .gdbinit file from the current directory? */
91 int auto_load_local_gdbinit = 1;
93 /* Absolute pathname to the current directory .gdbinit, if it exists. */
94 char *auto_load_local_gdbinit_pathname = NULL;
96 /* Boolean value if AUTO_LOAD_LOCAL_GDBINIT_PATHNAME has been loaded. */
97 int auto_load_local_gdbinit_loaded = 0;
99 /* "show" command for the auto_load_local_gdbinit configuration variable. */
102 show_auto_load_local_gdbinit (struct ui_file *file, int from_tty,
103 struct cmd_list_element *c, const char *value)
105 fprintf_filtered (file, _("Auto-loading of .gdbinit script from current "
106 "directory is %s.\n"),
110 /* Directory list safe to hold auto-loaded files. It is not checked for
111 absolute paths but they are strongly recommended. It is initialized by
112 _initialize_auto_load. */
113 static char *auto_load_safe_path;
115 /* Vector of directory elements of AUTO_LOAD_SAFE_PATH with each one normalized
116 by tilde_expand and possibly each entries has added its gdb_realpath
118 static VEC (char_ptr) *auto_load_safe_path_vec;
120 /* Update auto_load_safe_path_vec from current AUTO_LOAD_SAFE_PATH. */
123 auto_load_safe_path_vec_update (void)
125 VEC (char_ptr) *dir_vec = NULL;
130 fprintf_unfiltered (gdb_stdlog,
131 _("auto-load: Updating directories of \"%s\".\n"),
132 auto_load_safe_path);
134 free_char_ptr_vec (auto_load_safe_path_vec);
136 auto_load_safe_path_vec = dirnames_to_char_ptr_vec (auto_load_safe_path);
137 len = VEC_length (char_ptr, auto_load_safe_path_vec);
139 /* Apply tilde_expand and gdb_realpath to each AUTO_LOAD_SAFE_PATH_VEC
141 for (ix = 0; ix < len; ix++)
143 char *dir = VEC_index (char_ptr, auto_load_safe_path_vec, ix);
144 char *expanded = tilde_expand (dir);
145 char *real_path = gdb_realpath (expanded);
147 /* Ensure the current entry is at least tilde_expand-ed. */
148 VEC_replace (char_ptr, auto_load_safe_path_vec, ix, expanded);
152 if (strcmp (expanded, dir) == 0)
153 fprintf_unfiltered (gdb_stdlog,
154 _("auto-load: Using directory \"%s\".\n"),
157 fprintf_unfiltered (gdb_stdlog,
158 _("auto-load: Resolved directory \"%s\" "
164 /* If gdb_realpath returns a different content, append it. */
165 if (strcmp (real_path, expanded) == 0)
169 VEC_safe_push (char_ptr, auto_load_safe_path_vec, real_path);
172 fprintf_unfiltered (gdb_stdlog,
173 _("auto-load: And canonicalized as \"%s\".\n"),
179 /* "set" command for the auto_load_safe_path configuration variable. */
182 set_auto_load_safe_path (char *args, int from_tty, struct cmd_list_element *c)
184 if (auto_load_safe_path[0] == '\0')
186 xfree (auto_load_safe_path);
187 auto_load_safe_path = xstrdup (DEFAULT_AUTO_LOAD_SAFE_PATH);
190 auto_load_safe_path_vec_update ();
193 /* "show" command for the auto_load_safe_path configuration variable. */
196 show_auto_load_safe_path (struct ui_file *file, int from_tty,
197 struct cmd_list_element *c, const char *value)
199 if (strcmp (value, "/") == 0)
200 fprintf_filtered (file, _("Auto-load files are safe to load from any "
203 fprintf_filtered (file, _("List of directories from which it is safe to "
204 "auto-load files is %s.\n"),
208 /* "add-auto-load-safe-path" command for the auto_load_safe_path configuration
212 add_auto_load_safe_path (char *args, int from_tty)
216 if (args == NULL || *args == 0)
218 Directory argument required.\n\
219 Use 'set auto-load safe-path /' for disabling the auto-load safe-path security.\
222 s = xstrprintf ("%s%c%s", auto_load_safe_path, DIRNAME_SEPARATOR, args);
223 xfree (auto_load_safe_path);
224 auto_load_safe_path = s;
226 auto_load_safe_path_vec_update ();
229 /* Return 1 if FILENAME is equal to DIR or if FILENAME belongs to the
230 subdirectory DIR. Return 0 otherwise. gdb_realpath normalization is never
233 static ATTRIBUTE_PURE int
234 filename_is_in_dir (const char *filename, const char *dir)
236 size_t dir_len = strlen (dir);
238 while (dir_len && IS_DIR_SEPARATOR (dir[dir_len - 1]))
241 /* Ensure auto_load_safe_path "/" matches any FILENAME. On MS-Windows
242 platform FILENAME even after gdb_realpath does not have to start with
243 IS_DIR_SEPARATOR character, such as the 'C:\x.exe' filename. */
247 return (filename_ncmp (dir, filename, dir_len) == 0
248 && (IS_DIR_SEPARATOR (filename[dir_len])
249 || filename[dir_len] == '\0'));
252 /* Return 1 if FILENAME belongs to one of directory components of
253 AUTO_LOAD_SAFE_PATH_VEC. Return 0 otherwise.
254 auto_load_safe_path_vec_update is never called.
255 *FILENAME_REALP may be updated by gdb_realpath of FILENAME - it has to be
256 freed by the caller. */
259 filename_is_in_auto_load_safe_path_vec (const char *filename,
260 char **filename_realp)
265 for (ix = 0; VEC_iterate (char_ptr, auto_load_safe_path_vec, ix, dir); ++ix)
266 if (*filename_realp == NULL && filename_is_in_dir (filename, dir))
271 if (*filename_realp == NULL)
273 *filename_realp = gdb_realpath (filename);
274 if (debug_auto_load && strcmp (*filename_realp, filename) != 0)
275 fprintf_unfiltered (gdb_stdlog,
276 _("auto-load: Resolved "
277 "file \"%s\" as \"%s\".\n"),
278 filename, *filename_realp);
281 if (strcmp (*filename_realp, filename) != 0)
282 for (ix = 0; VEC_iterate (char_ptr, auto_load_safe_path_vec, ix, dir);
284 if (filename_is_in_dir (*filename_realp, dir))
291 fprintf_unfiltered (gdb_stdlog, _("auto-load: File \"%s\" matches "
292 "directory \"%s\".\n"),
300 /* Return 1 if FILENAME is located in one of the directories of
301 AUTO_LOAD_SAFE_PATH. Otherwise call warning and return 0. FILENAME does
302 not have to be an absolute path.
304 Existence of FILENAME is not checked. Function will still give a warning
305 even if the caller would quietly skip non-existing file in unsafe
309 file_is_auto_load_safe (const char *filename, const char *debug_fmt, ...)
311 char *filename_real = NULL;
312 struct cleanup *back_to;
318 va_start (debug_args, debug_fmt);
319 vfprintf_unfiltered (gdb_stdlog, debug_fmt, debug_args);
323 back_to = make_cleanup (free_current_contents, &filename_real);
325 if (filename_is_in_auto_load_safe_path_vec (filename, &filename_real))
327 do_cleanups (back_to);
331 auto_load_safe_path_vec_update ();
332 if (filename_is_in_auto_load_safe_path_vec (filename, &filename_real))
334 do_cleanups (back_to);
338 warning (_("File \"%s\" auto-loading has been declined by your "
339 "`auto-load safe-path' set to \"%s\"."),
340 filename_real, auto_load_safe_path);
342 do_cleanups (back_to);
346 /* Definition of script language for GDB canned sequences of commands. */
348 static const struct script_language script_language_gdb
349 = { GDB_AUTO_FILE_NAME, source_gdb_script_for_objfile };
352 source_gdb_script_for_objfile (struct objfile *objfile, FILE *file,
353 const char *filename)
356 struct auto_load_pspace_info *pspace_info;
357 volatile struct gdb_exception e;
359 is_safe = file_is_auto_load_safe (filename, _("auto-load: Loading canned "
360 "sequences of commands script "
361 "\"%s\" for objfile \"%s\".\n"),
362 filename, objfile->name);
364 /* Add this script to the hash table too so "info auto-load gdb-scripts"
366 pspace_info = get_auto_load_pspace_data_for_loading (current_program_space);
367 maybe_add_script (pspace_info, is_safe, filename, filename,
368 &script_language_gdb);
373 TRY_CATCH (e, RETURN_MASK_ALL)
375 script_from_file (file, filename);
377 exception_print (gdb_stderr, e);
380 /* For scripts specified in .debug_gdb_scripts, multiple objfiles may load
381 the same script. There's no point in loading the script multiple times,
382 and there can be a lot of objfiles and scripts, so we keep track of scripts
385 struct auto_load_pspace_info
387 /* For each program space we keep track of loaded scripts. */
388 struct htab *loaded_scripts;
390 /* Non-zero if we've issued the warning about an auto-load script not being
391 found. We only want to issue this warning once. */
392 int script_not_found_warning_printed;
395 /* Objects of this type are stored in the loaded script hash table. */
399 /* Name as provided by the objfile. */
402 /* Full path name or NULL if script wasn't found (or was otherwise
404 const char *full_path;
406 /* Non-zero if this script has been loaded. */
409 const struct script_language *language;
412 /* Per-program-space data key. */
413 static const struct program_space_data *auto_load_pspace_data;
416 auto_load_pspace_data_cleanup (struct program_space *pspace, void *arg)
418 struct auto_load_pspace_info *info;
420 info = program_space_data (pspace, auto_load_pspace_data);
423 if (info->loaded_scripts)
424 htab_delete (info->loaded_scripts);
429 /* Get the current autoload data. If none is found yet, add it now. This
430 function always returns a valid object. */
432 static struct auto_load_pspace_info *
433 get_auto_load_pspace_data (struct program_space *pspace)
435 struct auto_load_pspace_info *info;
437 info = program_space_data (pspace, auto_load_pspace_data);
440 info = XZALLOC (struct auto_load_pspace_info);
441 set_program_space_data (pspace, auto_load_pspace_data, info);
447 /* Hash function for the loaded script hash. */
450 hash_loaded_script_entry (const void *data)
452 const struct loaded_script *e = data;
454 return htab_hash_string (e->name) ^ htab_hash_pointer (e->language);
457 /* Equality function for the loaded script hash. */
460 eq_loaded_script_entry (const void *a, const void *b)
462 const struct loaded_script *ea = a;
463 const struct loaded_script *eb = b;
465 return strcmp (ea->name, eb->name) == 0 && ea->language == eb->language;
468 /* Initialize the table to track loaded scripts.
469 Each entry is hashed by the full path name. */
472 init_loaded_scripts_info (struct auto_load_pspace_info *pspace_info)
474 /* Choose 31 as the starting size of the hash table, somewhat arbitrarily.
475 Space for each entry is obtained with one malloc so we can free them
478 pspace_info->loaded_scripts = htab_create (31,
479 hash_loaded_script_entry,
480 eq_loaded_script_entry,
483 pspace_info->script_not_found_warning_printed = FALSE;
486 /* Wrapper on get_auto_load_pspace_data to also allocate the hash table
487 for loading scripts. */
489 struct auto_load_pspace_info *
490 get_auto_load_pspace_data_for_loading (struct program_space *pspace)
492 struct auto_load_pspace_info *info;
494 info = get_auto_load_pspace_data (pspace);
495 if (info->loaded_scripts == NULL)
496 init_loaded_scripts_info (info);
501 /* Add script NAME in LANGUAGE to hash table of PSPACE_INFO. LOADED 1 if the
502 script has been (is going to) be loaded, 0 otherwise (such as if it has not
503 been found). FULL_PATH is NULL if the script wasn't found. The result is
504 true if the script was already in the hash table. */
507 maybe_add_script (struct auto_load_pspace_info *pspace_info, int loaded,
508 const char *name, const char *full_path,
509 const struct script_language *language)
511 struct htab *htab = pspace_info->loaded_scripts;
512 struct loaded_script **slot, entry;
516 entry.language = language;
517 slot = (struct loaded_script **) htab_find_slot (htab, &entry, INSERT);
518 in_hash_table = *slot != NULL;
520 /* If this script is not in the hash table, add it. */
526 /* Allocate all space in one chunk so it's easier to free. */
527 *slot = xmalloc (sizeof (**slot)
529 + (full_path != NULL ? (strlen (full_path) + 1) : 0));
530 p = ((char*) *slot) + sizeof (**slot);
533 if (full_path != NULL)
536 strcpy (p, full_path);
537 (*slot)->full_path = p;
540 (*slot)->full_path = NULL;
541 (*slot)->loaded = loaded;
542 (*slot)->language = language;
545 return in_hash_table;
548 /* Clear the table of loaded section scripts. */
551 clear_section_scripts (void)
553 struct program_space *pspace = current_program_space;
554 struct auto_load_pspace_info *info;
556 info = program_space_data (pspace, auto_load_pspace_data);
557 if (info != NULL && info->loaded_scripts != NULL)
559 htab_delete (info->loaded_scripts);
560 info->loaded_scripts = NULL;
561 info->script_not_found_warning_printed = FALSE;
565 /* Look for the auto-load script in LANGUAGE associated with OBJFILE and load
569 auto_load_objfile_script (struct objfile *objfile,
570 const struct script_language *language)
573 char *filename, *debugfile;
576 struct cleanup *cleanups;
578 realname = gdb_realpath (objfile->name);
579 len = strlen (realname);
580 filename = xmalloc (len + strlen (language->suffix) + 1);
581 memcpy (filename, realname, len);
582 strcpy (filename + len, language->suffix);
584 cleanups = make_cleanup (xfree, filename);
585 make_cleanup (xfree, realname);
587 input = fopen (filename, "r");
588 debugfile = filename;
593 VEC (char_ptr) *debugdir_vec;
596 debugdir_vec = dirnames_to_char_ptr_vec (debug_file_directory);
597 make_cleanup_free_char_ptr_vec (debugdir_vec);
599 for (ix = 0; VEC_iterate (char_ptr, debugdir_vec, ix, debugdir); ++ix)
601 /* Also try the same file in the separate debug info directory. */
602 debugfile = xmalloc (strlen (debugdir) + strlen (filename) + 1);
603 strcpy (debugfile, debugdir);
605 /* FILENAME is absolute, so we don't need a "/" here. */
606 strcat (debugfile, filename);
608 make_cleanup (xfree, debugfile);
609 input = fopen (debugfile, "r");
615 if (!input && gdb_datadir)
617 /* Also try the same file in a subdirectory of gdb's data
619 debugfile = xmalloc (strlen (gdb_datadir) + strlen (filename)
620 + strlen ("/auto-load") + 1);
621 strcpy (debugfile, gdb_datadir);
622 strcat (debugfile, "/auto-load");
623 /* FILENAME is absolute, so we don't need a "/" here. */
624 strcat (debugfile, filename);
626 make_cleanup (xfree, debugfile);
627 input = fopen (debugfile, "r");
632 make_cleanup_fclose (input);
634 /* To preserve existing behaviour we don't check for whether the
635 script was already in the table, and always load it.
636 It's highly unlikely that we'd ever load it twice,
637 and these scripts are required to be idempotent under multiple
639 language->source_script_for_objfile (objfile, input, debugfile);
642 do_cleanups (cleanups);
645 /* Load any auto-loaded scripts for OBJFILE. */
648 load_auto_scripts_for_objfile (struct objfile *objfile)
650 if (!global_auto_load)
653 if (auto_load_gdb_scripts)
654 auto_load_objfile_script (objfile, &script_language_gdb);
656 gdbpy_load_auto_scripts_for_objfile (objfile);
659 /* This is a new_objfile observer callback to auto-load scripts.
661 Two flavors of auto-loaded scripts are supported.
662 1) based on the path to the objfile
663 2) from .debug_gdb_scripts section */
666 auto_load_new_objfile (struct objfile *objfile)
670 /* OBJFILE is NULL when loading a new "main" symbol-file. */
671 clear_section_scripts ();
675 load_auto_scripts_for_objfile (objfile);
678 /* Collect scripts to be printed in a vec. */
680 typedef struct loaded_script *loaded_script_ptr;
681 DEF_VEC_P (loaded_script_ptr);
683 struct collect_matching_scripts_data
685 VEC (loaded_script_ptr) **scripts_p;
687 const struct script_language *language;
690 /* Traversal function for htab_traverse.
691 Collect the entry if it matches the regexp. */
694 collect_matching_scripts (void **slot, void *info)
696 struct loaded_script *script = *slot;
697 struct collect_matching_scripts_data *data = info;
699 if (script->language == data->language && re_exec (script->name))
700 VEC_safe_push (loaded_script_ptr, *data->scripts_p, script);
708 print_script (struct loaded_script *script)
710 struct ui_out *uiout = current_uiout;
711 struct cleanup *chain;
713 chain = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
715 ui_out_field_string (uiout, "loaded", script->loaded ? "Yes" : "No");
716 ui_out_field_string (uiout, "script", script->name);
717 ui_out_text (uiout, "\n");
719 /* If the name isn't the full path, print it too. */
720 if (script->full_path != NULL
721 && strcmp (script->name, script->full_path) != 0)
723 ui_out_text (uiout, "\tfull name: ");
724 ui_out_field_string (uiout, "full_path", script->full_path);
725 ui_out_text (uiout, "\n");
731 /* Helper for info_auto_load_scripts to sort the scripts by name. */
734 sort_scripts_by_name (const void *ap, const void *bp)
736 const struct loaded_script *a = *(const struct loaded_script **) ap;
737 const struct loaded_script *b = *(const struct loaded_script **) bp;
739 return FILENAME_CMP (a->name, b->name);
742 /* Special internal GDB value of auto_load_info_scripts's PATTERN identify
743 the "info auto-load XXX" command has been executed through the general
744 "info auto-load" invocation. Extra newline will be printed if needed. */
745 char auto_load_info_scripts_pattern_nl[] = "";
747 /* Implementation for "info auto-load gdb-scripts"
748 (and "info auto-load python-scripts"). List scripts in LANGUAGE matching
749 PATTERN. FROM_TTY is the usual GDB boolean for user interactivity. */
752 auto_load_info_scripts (char *pattern, int from_tty,
753 const struct script_language *language)
755 struct ui_out *uiout = current_uiout;
756 struct auto_load_pspace_info *pspace_info;
757 struct cleanup *script_chain;
758 VEC (loaded_script_ptr) *scripts;
763 pspace_info = get_auto_load_pspace_data (current_program_space);
765 if (pattern && *pattern)
767 char *re_err = re_comp (pattern);
770 error (_("Invalid regexp: %s"), re_err);
777 /* We need to know the number of rows before we build the table.
778 Plus we want to sort the scripts by name.
779 So first traverse the hash table collecting the matching scripts. */
781 scripts = VEC_alloc (loaded_script_ptr, 10);
782 script_chain = make_cleanup (VEC_cleanup (loaded_script_ptr), &scripts);
784 if (pspace_info != NULL && pspace_info->loaded_scripts != NULL)
786 struct collect_matching_scripts_data data = { &scripts, language };
789 /* Pass a pointer to scripts as VEC_safe_push can realloc space. */
790 htab_traverse_noresize (pspace_info->loaded_scripts,
791 collect_matching_scripts, &data);
795 nr_scripts = VEC_length (loaded_script_ptr, scripts);
797 /* Table header shifted right by preceding "gdb-scripts: " would not match
799 if (nr_scripts > 0 && pattern == auto_load_info_scripts_pattern_nl)
800 ui_out_text (uiout, "\n");
802 make_cleanup_ui_out_table_begin_end (uiout, 2, nr_scripts,
803 "AutoLoadedScriptsTable");
805 ui_out_table_header (uiout, 7, ui_left, "loaded", "Loaded");
806 ui_out_table_header (uiout, 70, ui_left, "script", "Script");
807 ui_out_table_body (uiout);
812 loaded_script_ptr script;
814 qsort (VEC_address (loaded_script_ptr, scripts),
815 VEC_length (loaded_script_ptr, scripts),
816 sizeof (loaded_script_ptr), sort_scripts_by_name);
817 for (i = 0; VEC_iterate (loaded_script_ptr, scripts, i, script); ++i)
818 print_script (script);
821 do_cleanups (script_chain);
825 if (pattern && *pattern)
826 ui_out_message (uiout, 0, "No auto-load scripts matching %s.\n",
829 ui_out_message (uiout, 0, "No auto-load scripts.\n");
833 /* Wrapper for "info auto-load gdb-scripts". */
836 info_auto_load_gdb_scripts (char *pattern, int from_tty)
838 auto_load_info_scripts (pattern, from_tty, &script_language_gdb);
841 /* Implement 'info auto-load local-gdbinit'. */
844 info_auto_load_local_gdbinit (char *args, int from_tty)
846 if (auto_load_local_gdbinit_pathname == NULL)
847 printf_filtered (_("Local .gdbinit file was not found.\n"));
848 else if (auto_load_local_gdbinit_loaded)
849 printf_filtered (_("Local .gdbinit file \"%s\" has been loaded.\n"),
850 auto_load_local_gdbinit_pathname);
852 printf_filtered (_("Local .gdbinit file \"%s\" has not been loaded.\n"),
853 auto_load_local_gdbinit_pathname);
856 /* Return non-zero if SCRIPT_NOT_FOUND_WARNING_PRINTED of PSPACE_INFO was unset
857 before calling this function. Always set SCRIPT_NOT_FOUND_WARNING_PRINTED
861 script_not_found_warning_print (struct auto_load_pspace_info *pspace_info)
863 int retval = !pspace_info->script_not_found_warning_printed;
865 pspace_info->script_not_found_warning_printed = 1;
870 /* The only valid "set auto-load" argument is off|0|no|disable. */
873 set_auto_load_cmd (char *args, int from_tty)
875 struct cmd_list_element *list;
878 /* See parse_binary_operation in use by the sub-commands. */
880 length = args ? strlen (args) : 0;
882 while (length > 0 && (args[length - 1] == ' ' || args[length - 1] == '\t'))
885 if (length == 0 || (strncmp (args, "off", length) != 0
886 && strncmp (args, "0", length) != 0
887 && strncmp (args, "no", length) != 0
888 && strncmp (args, "disable", length) != 0))
889 error (_("Valid is only global 'set auto-load no'; "
890 "otherwise check the auto-load sub-commands."));
892 for (list = *auto_load_set_cmdlist_get (); list != NULL; list = list->next)
893 if (list->var_type == var_boolean)
895 gdb_assert (list->type == set_cmd);
896 do_setshow_command (args, from_tty, list);
900 /* Initialize "set auto-load " commands prefix and return it. */
902 struct cmd_list_element **
903 auto_load_set_cmdlist_get (void)
905 static struct cmd_list_element *retval;
908 add_prefix_cmd ("auto-load", class_maintenance, set_auto_load_cmd, _("\
909 Auto-loading specific settings.\n\
910 Configure various auto-load-specific variables such as\n\
911 automatic loading of Python scripts."),
912 &retval, "set auto-load ",
913 1/*allow-unknown*/, &setlist);
918 /* Command "show auto-load" displays summary of all the current
919 "show auto-load " settings. */
922 show_auto_load_cmd (char *args, int from_tty)
924 cmd_show_list (*auto_load_show_cmdlist_get (), from_tty, "");
927 /* Initialize "show auto-load " commands prefix and return it. */
929 struct cmd_list_element **
930 auto_load_show_cmdlist_get (void)
932 static struct cmd_list_element *retval;
935 add_prefix_cmd ("auto-load", class_maintenance, show_auto_load_cmd, _("\
936 Show auto-loading specific settings.\n\
937 Show configuration of various auto-load-specific variables such as\n\
938 automatic loading of Python scripts."),
939 &retval, "show auto-load ",
940 0/*allow-unknown*/, &showlist);
945 /* Command "info auto-load" displays whether the various auto-load files have
946 been loaded. This is reimplementation of cmd_show_list which inserts
947 newlines at proper places. */
950 info_auto_load_cmd (char *args, int from_tty)
952 struct cmd_list_element *list;
953 struct cleanup *infolist_chain;
954 struct ui_out *uiout = current_uiout;
956 infolist_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "infolist");
958 for (list = *auto_load_info_cmdlist_get (); list != NULL; list = list->next)
960 struct cleanup *option_chain
961 = make_cleanup_ui_out_tuple_begin_end (uiout, "option");
963 gdb_assert (!list->prefixlist);
964 gdb_assert (list->type == not_set_cmd);
966 ui_out_field_string (uiout, "name", list->name);
967 ui_out_text (uiout, ": ");
968 cmd_func (list, auto_load_info_scripts_pattern_nl, from_tty);
970 /* Close the tuple. */
971 do_cleanups (option_chain);
974 /* Close the tuple. */
975 do_cleanups (infolist_chain);
978 /* Initialize "info auto-load " commands prefix and return it. */
980 struct cmd_list_element **
981 auto_load_info_cmdlist_get (void)
983 static struct cmd_list_element *retval;
986 add_prefix_cmd ("auto-load", class_info, info_auto_load_cmd, _("\
987 Print current status of auto-loaded files.\n\
988 Print whether various files like Python scripts or .gdbinit files have been\n\
989 found and/or loaded."),
990 &retval, "info auto-load ",
991 0/*allow-unknown*/, &infolist);
996 void _initialize_auto_load (void);
999 _initialize_auto_load (void)
1001 struct cmd_list_element *cmd;
1003 auto_load_pspace_data
1004 = register_program_space_data_with_cleanup (auto_load_pspace_data_cleanup);
1006 observer_attach_new_objfile (auto_load_new_objfile);
1008 add_setshow_boolean_cmd ("gdb-scripts", class_support,
1009 &auto_load_gdb_scripts, _("\
1010 Enable or disable auto-loading of canned sequences of commands scripts."), _("\
1011 Show whether auto-loading of canned sequences of commands scripts is enabled."),
1013 If enabled, canned sequences of commands are loaded when the debugger reads\n\
1014 an executable or shared library.\n\
1015 This options has security implications for untrusted inferiors."),
1016 NULL, show_auto_load_gdb_scripts,
1017 auto_load_set_cmdlist_get (),
1018 auto_load_show_cmdlist_get ());
1020 add_cmd ("gdb-scripts", class_info, info_auto_load_gdb_scripts,
1021 _("Print the list of automatically loaded sequences of commands.\n\
1022 Usage: info auto-load gdb-scripts [REGEXP]"),
1023 auto_load_info_cmdlist_get ());
1025 add_setshow_boolean_cmd ("local-gdbinit", class_support,
1026 &auto_load_local_gdbinit, _("\
1027 Enable or disable auto-loading of .gdbinit script in current directory."), _("\
1028 Show whether auto-loading .gdbinit script in current directory is enabled."),
1030 If enabled, canned sequences of commands are loaded when debugger starts\n\
1031 from .gdbinit file in current directory. Such files are deprecated,\n\
1032 use a script associated with inferior executable file instead.\n\
1033 This options has security implications for untrusted inferiors."),
1034 NULL, show_auto_load_local_gdbinit,
1035 auto_load_set_cmdlist_get (),
1036 auto_load_show_cmdlist_get ());
1038 add_cmd ("local-gdbinit", class_info, info_auto_load_local_gdbinit,
1039 _("Print whether current directory .gdbinit file has been loaded.\n\
1040 Usage: info auto-load local-gdbinit"),
1041 auto_load_info_cmdlist_get ());
1043 auto_load_safe_path = xstrdup (DEFAULT_AUTO_LOAD_SAFE_PATH);
1044 auto_load_safe_path_vec_update ();
1045 add_setshow_optional_filename_cmd ("safe-path", class_support,
1046 &auto_load_safe_path, _("\
1047 Set the list of directories from which it is safe to auto-load files."), _("\
1048 Show the list of directories from which it is safe to auto-load files."), _("\
1049 Various files loaded automatically for the 'set auto-load ...' options must\n\
1050 be located in one of the directories listed by this option. Warning will be\n\
1051 printed and file will not be used otherwise.\n\
1052 Setting this parameter to an empty list resets it to its default value.\n\
1053 Setting this parameter to '/' (without the quotes) allows any file\n\
1054 for the 'set auto-load ...' options.\n\
1055 This option is ignored for the kinds of files having 'set auto-load ... off'.\n\
1056 This options has security implications for untrusted inferiors."),
1057 set_auto_load_safe_path,
1058 show_auto_load_safe_path,
1059 auto_load_set_cmdlist_get (),
1060 auto_load_show_cmdlist_get ());
1062 cmd = add_cmd ("add-auto-load-safe-path", class_support,
1063 add_auto_load_safe_path,
1064 _("Add entries to the list of directories from which it is safe "
1065 "to auto-load files.\n\
1066 See the commands 'set auto-load safe-path' and 'show auto-load safe-path' to\n\
1067 access the current full list setting."),
1069 set_cmd_completer (cmd, filename_completer);
1071 add_setshow_boolean_cmd ("auto-load", class_maintenance,
1072 &debug_auto_load, _("\
1073 Set auto-load verifications debugging."), _("\
1074 Show auto-load verifications debugging."), _("\
1075 When non-zero, debugging output for files of 'set auto-load ...'\n\
1077 NULL, show_debug_auto_load,
1078 &setdebuglist, &showdebuglist);