1 /* GDB routines for supporting auto-loaded scripts.
3 Copyright (C) 2012-2014 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-cmds.h"
34 #include "cli/cli-decode.h"
35 #include "cli/cli-setshow.h"
37 #include "readline/tilde.h"
38 #include "completer.h"
41 #include "filestuff.h"
43 /* The section to look in for auto-loaded scripts (in file formats that
45 Each entry in this section is a record that begins with a leading byte
46 identifying the record type.
47 At the moment we only support one record type: A leading byte of 1,
48 followed by the path of a python script to load. */
49 #define AUTO_SECTION_NAME ".debug_gdb_scripts"
51 /* The suffix of per-objfile scripts to auto-load as non-Python command files.
52 E.g. When the program loads libfoo.so, look for libfoo-gdb.gdb. */
53 #define GDB_AUTO_FILE_NAME "-gdb.gdb"
55 static void source_gdb_script_for_objfile (struct objfile *objfile, FILE *file,
56 const char *filename);
58 /* Value of the 'set debug auto-load' configuration variable. */
59 static int debug_auto_load = 0;
61 /* "show" command for the debug_auto_load configuration variable. */
64 show_debug_auto_load (struct ui_file *file, int from_tty,
65 struct cmd_list_element *c, const char *value)
67 fprintf_filtered (file, _("Debugging output for files "
68 "of 'set auto-load ...' is %s.\n"),
72 /* User-settable option to enable/disable auto-loading of GDB_AUTO_FILE_NAME
74 set auto-load gdb-scripts on|off
75 This is true if we should auto-load associated scripts when an objfile
76 is opened, false otherwise. */
77 static int auto_load_gdb_scripts = 1;
79 /* "show" command for the auto_load_gdb_scripts configuration variable. */
82 show_auto_load_gdb_scripts (struct ui_file *file, int from_tty,
83 struct cmd_list_element *c, const char *value)
85 fprintf_filtered (file, _("Auto-loading of canned sequences of commands "
90 /* Return non-zero if auto-loading gdb scripts is enabled. */
93 auto_load_gdb_scripts_enabled (void)
95 return auto_load_gdb_scripts;
98 /* Internal-use flag to enable/disable auto-loading.
99 This is true if we should auto-load python code when an objfile is opened,
102 Both auto_load_scripts && global_auto_load must be true to enable
105 This flag exists to facilitate deferring auto-loading during start-up
106 until after ./.gdbinit has been read; it may augment the search directories
107 used to find the scripts. */
108 int global_auto_load = 1;
110 /* Auto-load .gdbinit file from the current directory? */
111 int auto_load_local_gdbinit = 1;
113 /* Absolute pathname to the current directory .gdbinit, if it exists. */
114 char *auto_load_local_gdbinit_pathname = NULL;
116 /* Boolean value if AUTO_LOAD_LOCAL_GDBINIT_PATHNAME has been loaded. */
117 int auto_load_local_gdbinit_loaded = 0;
119 /* "show" command for the auto_load_local_gdbinit configuration variable. */
122 show_auto_load_local_gdbinit (struct ui_file *file, int from_tty,
123 struct cmd_list_element *c, const char *value)
125 fprintf_filtered (file, _("Auto-loading of .gdbinit script from current "
126 "directory is %s.\n"),
130 /* Directory list from which to load auto-loaded scripts. It is not checked
131 for absolute paths but they are strongly recommended. It is initialized by
132 _initialize_auto_load. */
133 static char *auto_load_dir;
135 /* "set" command for the auto_load_dir configuration variable. */
138 set_auto_load_dir (char *args, int from_tty, struct cmd_list_element *c)
140 /* Setting the variable to "" resets it to the compile time defaults. */
141 if (auto_load_dir[0] == '\0')
143 xfree (auto_load_dir);
144 auto_load_dir = xstrdup (AUTO_LOAD_DIR);
148 /* "show" command for the auto_load_dir configuration variable. */
151 show_auto_load_dir (struct ui_file *file, int from_tty,
152 struct cmd_list_element *c, const char *value)
154 fprintf_filtered (file, _("List of directories from which to load "
155 "auto-loaded scripts is %s.\n"),
159 /* Directory list safe to hold auto-loaded files. It is not checked for
160 absolute paths but they are strongly recommended. It is initialized by
161 _initialize_auto_load. */
162 static char *auto_load_safe_path;
164 /* Vector of directory elements of AUTO_LOAD_SAFE_PATH with each one normalized
165 by tilde_expand and possibly each entries has added its gdb_realpath
167 static VEC (char_ptr) *auto_load_safe_path_vec;
169 /* Expand $datadir and $debugdir in STRING according to the rules of
170 substitute_path_component. Return vector from dirnames_to_char_ptr_vec,
171 this vector must be freed by free_char_ptr_vec by the caller. */
173 static VEC (char_ptr) *
174 auto_load_expand_dir_vars (const char *string)
176 VEC (char_ptr) *dir_vec;
179 s = xstrdup (string);
180 substitute_path_component (&s, "$datadir", gdb_datadir);
181 substitute_path_component (&s, "$debugdir", debug_file_directory);
183 if (debug_auto_load && strcmp (s, string) != 0)
184 fprintf_unfiltered (gdb_stdlog,
185 _("auto-load: Expanded $-variables to \"%s\".\n"), s);
187 dir_vec = dirnames_to_char_ptr_vec (s);
193 /* Update auto_load_safe_path_vec from current AUTO_LOAD_SAFE_PATH. */
196 auto_load_safe_path_vec_update (void)
202 fprintf_unfiltered (gdb_stdlog,
203 _("auto-load: Updating directories of \"%s\".\n"),
204 auto_load_safe_path);
206 free_char_ptr_vec (auto_load_safe_path_vec);
208 auto_load_safe_path_vec = auto_load_expand_dir_vars (auto_load_safe_path);
209 len = VEC_length (char_ptr, auto_load_safe_path_vec);
211 /* Apply tilde_expand and gdb_realpath to each AUTO_LOAD_SAFE_PATH_VEC
213 for (ix = 0; ix < len; ix++)
215 char *dir = VEC_index (char_ptr, auto_load_safe_path_vec, ix);
216 char *expanded = tilde_expand (dir);
217 char *real_path = gdb_realpath (expanded);
219 /* Ensure the current entry is at least tilde_expand-ed. */
220 VEC_replace (char_ptr, auto_load_safe_path_vec, ix, expanded);
224 if (strcmp (expanded, dir) == 0)
225 fprintf_unfiltered (gdb_stdlog,
226 _("auto-load: Using directory \"%s\".\n"),
229 fprintf_unfiltered (gdb_stdlog,
230 _("auto-load: Resolved directory \"%s\" "
236 /* If gdb_realpath returns a different content, append it. */
237 if (strcmp (real_path, expanded) == 0)
241 VEC_safe_push (char_ptr, auto_load_safe_path_vec, real_path);
244 fprintf_unfiltered (gdb_stdlog,
245 _("auto-load: And canonicalized as \"%s\".\n"),
251 /* Variable gdb_datadir has been set. Update content depending on $datadir. */
254 auto_load_gdb_datadir_changed (void)
256 auto_load_safe_path_vec_update ();
259 /* "set" command for the auto_load_safe_path configuration variable. */
262 set_auto_load_safe_path (char *args, int from_tty, struct cmd_list_element *c)
264 /* Setting the variable to "" resets it to the compile time defaults. */
265 if (auto_load_safe_path[0] == '\0')
267 xfree (auto_load_safe_path);
268 auto_load_safe_path = xstrdup (AUTO_LOAD_SAFE_PATH);
271 auto_load_safe_path_vec_update ();
274 /* "show" command for the auto_load_safe_path configuration variable. */
277 show_auto_load_safe_path (struct ui_file *file, int from_tty,
278 struct cmd_list_element *c, const char *value)
282 /* Check if user has entered either "/" or for example ":".
283 But while more complicate content like ":/foo" would still also
284 permit any location do not hide those. */
286 for (cs = value; *cs && (*cs == DIRNAME_SEPARATOR || IS_DIR_SEPARATOR (*cs));
289 fprintf_filtered (file, _("Auto-load files are safe to load from any "
292 fprintf_filtered (file, _("List of directories from which it is safe to "
293 "auto-load files is %s.\n"),
297 /* "add-auto-load-safe-path" command for the auto_load_safe_path configuration
301 add_auto_load_safe_path (char *args, int from_tty)
305 if (args == NULL || *args == 0)
307 Directory argument required.\n\
308 Use 'set auto-load safe-path /' for disabling the auto-load safe-path security.\
311 s = xstrprintf ("%s%c%s", auto_load_safe_path, DIRNAME_SEPARATOR, args);
312 xfree (auto_load_safe_path);
313 auto_load_safe_path = s;
315 auto_load_safe_path_vec_update ();
318 /* Implementation for filename_is_in_pattern overwriting the caller's FILENAME
322 filename_is_in_pattern_1 (char *filename, char *pattern)
324 size_t pattern_len = strlen (pattern);
325 size_t filename_len = strlen (filename);
328 fprintf_unfiltered (gdb_stdlog, _("auto-load: Matching file \"%s\" "
329 "to pattern \"%s\"\n"),
332 /* Trim trailing slashes ("/") from PATTERN. Even for "d:\" paths as
333 trailing slashes are trimmed also from FILENAME it still matches
335 while (pattern_len && IS_DIR_SEPARATOR (pattern[pattern_len - 1]))
337 pattern[pattern_len] = '\0';
339 /* Ensure auto_load_safe_path "/" matches any FILENAME. On MS-Windows
340 platform FILENAME even after gdb_realpath does not have to start with
341 IS_DIR_SEPARATOR character, such as the 'C:\x.exe' filename. */
342 if (pattern_len == 0)
345 fprintf_unfiltered (gdb_stdlog,
346 _("auto-load: Matched - empty pattern\n"));
352 /* Trim trailing slashes ("/"). PATTERN also has slashes trimmed the
353 same way so they will match. */
354 while (filename_len && IS_DIR_SEPARATOR (filename[filename_len - 1]))
356 filename[filename_len] = '\0';
357 if (filename_len == 0)
360 fprintf_unfiltered (gdb_stdlog,
361 _("auto-load: Not matched - pattern \"%s\".\n"),
366 if (gdb_filename_fnmatch (pattern, filename, FNM_FILE_NAME | FNM_NOESCAPE)
370 fprintf_unfiltered (gdb_stdlog, _("auto-load: Matched - file "
371 "\"%s\" to pattern \"%s\".\n"),
376 /* Trim trailing FILENAME component. */
377 while (filename_len > 0 && !IS_DIR_SEPARATOR (filename[filename_len - 1]))
382 /* Return 1 if FILENAME matches PATTERN or if FILENAME resides in
383 a subdirectory of a directory that matches PATTERN. Return 0 otherwise.
384 gdb_realpath normalization is never done here. */
386 static ATTRIBUTE_PURE int
387 filename_is_in_pattern (const char *filename, const char *pattern)
389 char *filename_copy, *pattern_copy;
391 filename_copy = alloca (strlen (filename) + 1);
392 strcpy (filename_copy, filename);
393 pattern_copy = alloca (strlen (pattern) + 1);
394 strcpy (pattern_copy, pattern);
396 return filename_is_in_pattern_1 (filename_copy, pattern_copy);
399 /* Return 1 if FILENAME belongs to one of directory components of
400 AUTO_LOAD_SAFE_PATH_VEC. Return 0 otherwise.
401 auto_load_safe_path_vec_update is never called.
402 *FILENAME_REALP may be updated by gdb_realpath of FILENAME - it has to be
403 freed by the caller. */
406 filename_is_in_auto_load_safe_path_vec (const char *filename,
407 char **filename_realp)
412 for (ix = 0; VEC_iterate (char_ptr, auto_load_safe_path_vec, ix, pattern);
414 if (*filename_realp == NULL && filename_is_in_pattern (filename, pattern))
419 if (*filename_realp == NULL)
421 *filename_realp = gdb_realpath (filename);
422 if (debug_auto_load && strcmp (*filename_realp, filename) != 0)
423 fprintf_unfiltered (gdb_stdlog,
424 _("auto-load: Resolved "
425 "file \"%s\" as \"%s\".\n"),
426 filename, *filename_realp);
429 if (strcmp (*filename_realp, filename) != 0)
431 VEC_iterate (char_ptr, auto_load_safe_path_vec, ix, pattern); ++ix)
432 if (filename_is_in_pattern (*filename_realp, pattern))
439 fprintf_unfiltered (gdb_stdlog, _("auto-load: File \"%s\" matches "
440 "directory \"%s\".\n"),
448 /* Return 1 if FILENAME is located in one of the directories of
449 AUTO_LOAD_SAFE_PATH. Otherwise call warning and return 0. FILENAME does
450 not have to be an absolute path.
452 Existence of FILENAME is not checked. Function will still give a warning
453 even if the caller would quietly skip non-existing file in unsafe
457 file_is_auto_load_safe (const char *filename, const char *debug_fmt, ...)
459 char *filename_real = NULL;
460 struct cleanup *back_to;
461 static int advice_printed = 0;
467 va_start (debug_args, debug_fmt);
468 vfprintf_unfiltered (gdb_stdlog, debug_fmt, debug_args);
472 back_to = make_cleanup (free_current_contents, &filename_real);
474 if (filename_is_in_auto_load_safe_path_vec (filename, &filename_real))
476 do_cleanups (back_to);
480 auto_load_safe_path_vec_update ();
481 if (filename_is_in_auto_load_safe_path_vec (filename, &filename_real))
483 do_cleanups (back_to);
487 warning (_("File \"%s\" auto-loading has been declined by your "
488 "`auto-load safe-path' set to \"%s\"."),
489 filename_real, auto_load_safe_path);
493 const char *homedir = getenv ("HOME");
498 homeinit = xstrprintf ("%s/%s", homedir, gdbinit);
499 make_cleanup (xfree, homeinit);
501 printf_filtered (_("\
502 To enable execution of this file add\n\
503 \tadd-auto-load-safe-path %s\n\
504 line to your configuration file \"%s\".\n\
505 To completely disable this security protection add\n\
506 \tset auto-load safe-path /\n\
507 line to your configuration file \"%s\".\n\
508 For more information about this security protection see the\n\
509 \"Auto-loading safe path\" section in the GDB manual. E.g., run from the shell:\n\
510 \tinfo \"(gdb)Auto-loading safe path\"\n"),
511 filename_real, homeinit, homeinit);
515 do_cleanups (back_to);
519 /* Definition of script language for GDB canned sequences of commands. */
521 static const struct script_language script_language_gdb =
525 auto_load_gdb_scripts_enabled,
526 source_gdb_script_for_objfile
530 source_gdb_script_for_objfile (struct objfile *objfile, FILE *file,
531 const char *filename)
533 volatile struct gdb_exception e;
535 TRY_CATCH (e, RETURN_MASK_ALL)
537 script_from_file (file, filename);
539 exception_print (gdb_stderr, e);
542 /* For scripts specified in .debug_gdb_scripts, multiple objfiles may load
543 the same script. There's no point in loading the script multiple times,
544 and there can be a lot of objfiles and scripts, so we keep track of scripts
547 struct auto_load_pspace_info
549 /* For each program space we keep track of loaded scripts. */
550 struct htab *loaded_scripts;
552 /* Non-zero if we've issued the warning about an auto-load script not being
553 found. We only want to issue this warning once. */
554 int script_not_found_warning_printed;
557 /* Objects of this type are stored in the loaded script hash table. */
561 /* Name as provided by the objfile. */
564 /* Full path name or NULL if script wasn't found (or was otherwise
566 const char *full_path;
568 /* Non-zero if this script has been loaded. */
571 const struct script_language *language;
574 /* Per-program-space data key. */
575 static const struct program_space_data *auto_load_pspace_data;
578 auto_load_pspace_data_cleanup (struct program_space *pspace, void *arg)
580 struct auto_load_pspace_info *info = arg;
582 if (info->loaded_scripts)
583 htab_delete (info->loaded_scripts);
587 /* Get the current autoload data. If none is found yet, add it now. This
588 function always returns a valid object. */
590 static struct auto_load_pspace_info *
591 get_auto_load_pspace_data (struct program_space *pspace)
593 struct auto_load_pspace_info *info;
595 info = program_space_data (pspace, auto_load_pspace_data);
598 info = XCNEW (struct auto_load_pspace_info);
599 set_program_space_data (pspace, auto_load_pspace_data, info);
605 /* Hash function for the loaded script hash. */
608 hash_loaded_script_entry (const void *data)
610 const struct loaded_script *e = data;
612 return htab_hash_string (e->name) ^ htab_hash_pointer (e->language);
615 /* Equality function for the loaded script hash. */
618 eq_loaded_script_entry (const void *a, const void *b)
620 const struct loaded_script *ea = a;
621 const struct loaded_script *eb = b;
623 return strcmp (ea->name, eb->name) == 0 && ea->language == eb->language;
626 /* Initialize the table to track loaded scripts.
627 Each entry is hashed by the full path name. */
630 init_loaded_scripts_info (struct auto_load_pspace_info *pspace_info)
632 /* Choose 31 as the starting size of the hash table, somewhat arbitrarily.
633 Space for each entry is obtained with one malloc so we can free them
636 pspace_info->loaded_scripts = htab_create (31,
637 hash_loaded_script_entry,
638 eq_loaded_script_entry,
641 pspace_info->script_not_found_warning_printed = FALSE;
644 /* Wrapper on get_auto_load_pspace_data to also allocate the hash table
645 for loading scripts. */
647 struct auto_load_pspace_info *
648 get_auto_load_pspace_data_for_loading (struct program_space *pspace)
650 struct auto_load_pspace_info *info;
652 info = get_auto_load_pspace_data (pspace);
653 if (info->loaded_scripts == NULL)
654 init_loaded_scripts_info (info);
659 /* Add script NAME in LANGUAGE to hash table of PSPACE_INFO. LOADED 1 if the
660 script has been (is going to) be loaded, 0 otherwise (such as if it has not
661 been found). FULL_PATH is NULL if the script wasn't found. The result is
662 true if the script was already in the hash table. */
665 maybe_add_script (struct auto_load_pspace_info *pspace_info, int loaded,
666 const char *name, const char *full_path,
667 const struct script_language *language)
669 struct htab *htab = pspace_info->loaded_scripts;
670 struct loaded_script **slot, entry;
674 entry.language = language;
675 slot = (struct loaded_script **) htab_find_slot (htab, &entry, INSERT);
676 in_hash_table = *slot != NULL;
678 /* If this script is not in the hash table, add it. */
684 /* Allocate all space in one chunk so it's easier to free. */
685 *slot = xmalloc (sizeof (**slot)
687 + (full_path != NULL ? (strlen (full_path) + 1) : 0));
688 p = ((char*) *slot) + sizeof (**slot);
691 if (full_path != NULL)
694 strcpy (p, full_path);
695 (*slot)->full_path = p;
698 (*slot)->full_path = NULL;
699 (*slot)->loaded = loaded;
700 (*slot)->language = language;
703 return in_hash_table;
706 /* Clear the table of loaded section scripts. */
709 clear_section_scripts (void)
711 struct program_space *pspace = current_program_space;
712 struct auto_load_pspace_info *info;
714 info = program_space_data (pspace, auto_load_pspace_data);
715 if (info != NULL && info->loaded_scripts != NULL)
717 htab_delete (info->loaded_scripts);
718 info->loaded_scripts = NULL;
719 info->script_not_found_warning_printed = FALSE;
723 /* Look for the auto-load script in LANGUAGE associated with OBJFILE where
724 OBJFILE's gdb_realpath is REALNAME and load it. Return 1 if we found any
725 matching script, return 0 otherwise. */
728 auto_load_objfile_script_1 (struct objfile *objfile, const char *realname,
729 const struct script_language *language)
731 char *filename, *debugfile;
734 struct cleanup *cleanups;
736 len = strlen (realname);
737 filename = xmalloc (len + strlen (language->suffix) + 1);
738 memcpy (filename, realname, len);
739 strcpy (filename + len, language->suffix);
741 cleanups = make_cleanup (xfree, filename);
743 input = gdb_fopen_cloexec (filename, "r");
744 debugfile = filename;
746 fprintf_unfiltered (gdb_stdlog, _("auto-load: Attempted file \"%s\" %s.\n"),
747 debugfile, input ? _("exists") : _("does not exist"));
755 /* Also try the same file in a subdirectory of gdb's data
758 vec = auto_load_expand_dir_vars (auto_load_dir);
759 make_cleanup_free_char_ptr_vec (vec);
762 fprintf_unfiltered (gdb_stdlog, _("auto-load: Searching 'set auto-load "
763 "scripts-directory' path \"%s\".\n"),
766 for (ix = 0; VEC_iterate (char_ptr, vec, ix, dir); ++ix)
768 debugfile = xmalloc (strlen (dir) + strlen (filename) + 1);
769 strcpy (debugfile, dir);
771 /* FILENAME is absolute, so we don't need a "/" here. */
772 strcat (debugfile, filename);
774 make_cleanup (xfree, debugfile);
775 input = gdb_fopen_cloexec (debugfile, "r");
777 fprintf_unfiltered (gdb_stdlog, _("auto-load: Attempted file "
780 input ? _("exists") : _("does not exist"));
789 struct auto_load_pspace_info *pspace_info;
791 make_cleanup_fclose (input);
794 = file_is_auto_load_safe (filename,
795 _("auto-load: Loading %s script \"%s\""
796 " by extension for objfile \"%s\".\n"),
797 language->name, filename,
798 objfile_name (objfile));
800 /* Add this script to the hash table too so
801 "info auto-load ${lang}-scripts" can print it. */
803 = get_auto_load_pspace_data_for_loading (current_program_space);
804 maybe_add_script (pspace_info, is_safe, filename, filename, language);
806 /* To preserve existing behaviour we don't check for whether the
807 script was already in the table, and always load it.
808 It's highly unlikely that we'd ever load it twice,
809 and these scripts are required to be idempotent under multiple
812 language->source_script_for_objfile (objfile, input, debugfile);
819 do_cleanups (cleanups);
823 /* Look for the auto-load script in LANGUAGE associated with OBJFILE and load
827 auto_load_objfile_script (struct objfile *objfile,
828 const struct script_language *language)
831 struct cleanup *cleanups;
833 /* Skip this script if support has not been compiled in or
834 auto-loading it has been disabled. */
836 || !language->auto_load_enabled ())
839 realname = gdb_realpath (objfile_name (objfile));
840 cleanups = make_cleanup (xfree, realname);
842 if (!auto_load_objfile_script_1 (objfile, realname, language))
844 /* For Windows/DOS .exe executables, strip the .exe suffix, so that
845 FOO-gdb.gdb could be used for FOO.exe, and try again. */
847 size_t len = strlen (realname);
848 const size_t lexe = sizeof (".exe") - 1;
850 if (len > lexe && strcasecmp (realname + len - lexe, ".exe") == 0)
853 realname[len] = '\0';
855 fprintf_unfiltered (gdb_stdlog, _("auto-load: Stripped .exe suffix, "
856 "retrying with \"%s\".\n"),
858 auto_load_objfile_script_1 (objfile, realname, language);
862 do_cleanups (cleanups);
865 /* Load scripts specified in OBJFILE.
866 START,END delimit a buffer containing a list of nul-terminated
868 SECTION_NAME is used in error messages.
870 Scripts are found per normal "source -s" command processing.
871 First the script is looked for in $cwd. If not found there the
872 source search path is used.
874 The section contains a list of path names of script files to load.
875 Each path is null-terminated. */
878 source_section_scripts (struct objfile *objfile, const char *section_name,
879 const char *start, const char *end)
882 struct auto_load_pspace_info *pspace_info;
884 pspace_info = get_auto_load_pspace_data_for_loading (current_program_space);
886 for (p = start; p < end; ++p)
891 int opened, in_hash_table;
892 struct cleanup *back_to;
893 /* At the moment we only support python scripts in .debug_gdb_scripts,
894 but that can change. */
895 const struct script_language *language = gdbpy_script_language_defn ();
899 warning (_("Invalid entry in %s section"), section_name);
900 /* We could try various heuristics to find the next valid entry,
901 but it's safer to just punt. */
906 while (p < end && *p != '\0')
910 char *buf = alloca (p - file + 1);
912 memcpy (buf, file, p - file);
913 buf[p - file] = '\0';
914 warning (_("Non-null-terminated path in %s: %s"),
921 warning (_("Empty path in %s"), section_name);
925 /* Skip this script if support has not been compiled in or
926 auto-loading it has been disabled. */
928 || !language->auto_load_enabled ())
930 /* No message is printed, just skip it. */
934 opened = find_and_open_script (file, 1 /*search_path*/,
935 &stream, &full_path);
937 back_to = make_cleanup (null_cleanup, NULL);
940 make_cleanup_fclose (stream);
941 make_cleanup (xfree, full_path);
943 if (!file_is_auto_load_safe (full_path,
944 _("auto-load: Loading %s script "
945 "\"%s\" from section \"%s\" of "
946 "objfile \"%s\".\n"),
947 language->name, full_path, section_name,
948 objfile_name (objfile)))
955 /* If one script isn't found it's not uncommon for more to not be
956 found either. We don't want to print a message for each script,
957 too much noise. Instead, we print the warning once and tell the
958 user how to find the list of scripts that weren't loaded.
959 We don't throw an error, the program is still debuggable.
961 IWBN if complaints.c were more general-purpose. */
963 if (script_not_found_warning_print (pspace_info))
964 warning (_("Missing auto-load scripts referenced in section %s\n\
966 Use `info auto-load %s-scripts [REGEXP]' to list them."),
967 section_name, objfile_name (objfile), language->name);
970 in_hash_table = maybe_add_script (pspace_info, opened, file, full_path,
973 /* If this file is not currently loaded, load it. */
974 if (opened && !in_hash_table)
976 gdb_assert (language->source_script_for_objfile != NULL);
977 language->source_script_for_objfile (objfile, stream, full_path);
980 do_cleanups (back_to);
984 /* Load scripts specified in section SECTION_NAME of OBJFILE. */
987 auto_load_section_scripts (struct objfile *objfile, const char *section_name)
989 bfd *abfd = objfile->obfd;
990 asection *scripts_sect;
991 bfd_byte *data = NULL;
993 scripts_sect = bfd_get_section_by_name (abfd, section_name);
994 if (scripts_sect == NULL)
997 if (!bfd_get_full_section_contents (abfd, scripts_sect, &data))
998 warning (_("Couldn't read %s section of %s"),
999 section_name, bfd_get_filename (abfd));
1002 struct cleanup *cleanups;
1003 char *p = (char *) data;
1005 cleanups = make_cleanup (xfree, p);
1006 source_section_scripts (objfile, section_name, p,
1007 p + bfd_get_section_size (scripts_sect));
1008 do_cleanups (cleanups);
1012 /* Load any auto-loaded scripts for OBJFILE. */
1015 load_auto_scripts_for_objfile (struct objfile *objfile)
1017 /* Return immediately if auto-loading has been globally disabled.
1018 This is to handle sequencing of operations during gdb startup.
1019 Also return immediately if OBJFILE is not actually a file. */
1020 if (!global_auto_load || (objfile->flags & OBJF_NOT_FILENAME) != 0)
1023 /* Load any scripts for this objfile. e.g. foo-gdb.gdb, foo-gdb.py. */
1024 auto_load_objfile_script (objfile, &script_language_gdb);
1025 auto_load_objfile_script (objfile, gdbpy_script_language_defn ());
1027 /* Load any scripts mentioned in AUTO_SECTION_NAME (.debug_gdb_scripts). */
1028 auto_load_section_scripts (objfile, AUTO_SECTION_NAME);
1031 /* This is a new_objfile observer callback to auto-load scripts.
1033 Two flavors of auto-loaded scripts are supported.
1034 1) based on the path to the objfile
1035 2) from .debug_gdb_scripts section */
1038 auto_load_new_objfile (struct objfile *objfile)
1042 /* OBJFILE is NULL when loading a new "main" symbol-file. */
1043 clear_section_scripts ();
1047 load_auto_scripts_for_objfile (objfile);
1050 /* Collect scripts to be printed in a vec. */
1052 typedef struct loaded_script *loaded_script_ptr;
1053 DEF_VEC_P (loaded_script_ptr);
1055 struct collect_matching_scripts_data
1057 VEC (loaded_script_ptr) **scripts_p;
1059 const struct script_language *language;
1062 /* Traversal function for htab_traverse.
1063 Collect the entry if it matches the regexp. */
1066 collect_matching_scripts (void **slot, void *info)
1068 struct loaded_script *script = *slot;
1069 struct collect_matching_scripts_data *data = info;
1071 if (script->language == data->language && re_exec (script->name))
1072 VEC_safe_push (loaded_script_ptr, *data->scripts_p, script);
1080 print_script (struct loaded_script *script)
1082 struct ui_out *uiout = current_uiout;
1083 struct cleanup *chain;
1085 chain = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
1087 ui_out_field_string (uiout, "loaded", script->loaded ? "Yes" : "No");
1088 ui_out_field_string (uiout, "script", script->name);
1089 ui_out_text (uiout, "\n");
1091 /* If the name isn't the full path, print it too. */
1092 if (script->full_path != NULL
1093 && strcmp (script->name, script->full_path) != 0)
1095 ui_out_text (uiout, "\tfull name: ");
1096 ui_out_field_string (uiout, "full_path", script->full_path);
1097 ui_out_text (uiout, "\n");
1100 do_cleanups (chain);
1103 /* Helper for info_auto_load_scripts to sort the scripts by name. */
1106 sort_scripts_by_name (const void *ap, const void *bp)
1108 const struct loaded_script *a = *(const struct loaded_script **) ap;
1109 const struct loaded_script *b = *(const struct loaded_script **) bp;
1111 return FILENAME_CMP (a->name, b->name);
1114 /* Special internal GDB value of auto_load_info_scripts's PATTERN identify
1115 the "info auto-load XXX" command has been executed through the general
1116 "info auto-load" invocation. Extra newline will be printed if needed. */
1117 char auto_load_info_scripts_pattern_nl[] = "";
1119 /* Implementation for "info auto-load gdb-scripts"
1120 (and "info auto-load python-scripts"). List scripts in LANGUAGE matching
1121 PATTERN. FROM_TTY is the usual GDB boolean for user interactivity. */
1124 auto_load_info_scripts (char *pattern, int from_tty,
1125 const struct script_language *language)
1127 struct ui_out *uiout = current_uiout;
1128 struct auto_load_pspace_info *pspace_info;
1129 struct cleanup *script_chain;
1130 VEC (loaded_script_ptr) *scripts;
1135 pspace_info = get_auto_load_pspace_data (current_program_space);
1137 if (pattern && *pattern)
1139 char *re_err = re_comp (pattern);
1142 error (_("Invalid regexp: %s"), re_err);
1149 /* We need to know the number of rows before we build the table.
1150 Plus we want to sort the scripts by name.
1151 So first traverse the hash table collecting the matching scripts. */
1153 scripts = VEC_alloc (loaded_script_ptr, 10);
1154 script_chain = make_cleanup (VEC_cleanup (loaded_script_ptr), &scripts);
1156 if (pspace_info != NULL && pspace_info->loaded_scripts != NULL)
1158 struct collect_matching_scripts_data data = { &scripts, language };
1160 /* Pass a pointer to scripts as VEC_safe_push can realloc space. */
1161 htab_traverse_noresize (pspace_info->loaded_scripts,
1162 collect_matching_scripts, &data);
1165 nr_scripts = VEC_length (loaded_script_ptr, scripts);
1167 /* Table header shifted right by preceding "gdb-scripts: " would not match
1169 if (nr_scripts > 0 && pattern == auto_load_info_scripts_pattern_nl)
1170 ui_out_text (uiout, "\n");
1172 make_cleanup_ui_out_table_begin_end (uiout, 2, nr_scripts,
1173 "AutoLoadedScriptsTable");
1175 ui_out_table_header (uiout, 7, ui_left, "loaded", "Loaded");
1176 ui_out_table_header (uiout, 70, ui_left, "script", "Script");
1177 ui_out_table_body (uiout);
1182 loaded_script_ptr script;
1184 qsort (VEC_address (loaded_script_ptr, scripts),
1185 VEC_length (loaded_script_ptr, scripts),
1186 sizeof (loaded_script_ptr), sort_scripts_by_name);
1187 for (i = 0; VEC_iterate (loaded_script_ptr, scripts, i, script); ++i)
1188 print_script (script);
1191 do_cleanups (script_chain);
1193 if (nr_scripts == 0)
1195 if (pattern && *pattern)
1196 ui_out_message (uiout, 0, "No auto-load scripts matching %s.\n",
1199 ui_out_message (uiout, 0, "No auto-load scripts.\n");
1203 /* Wrapper for "info auto-load gdb-scripts". */
1206 info_auto_load_gdb_scripts (char *pattern, int from_tty)
1208 auto_load_info_scripts (pattern, from_tty, &script_language_gdb);
1211 /* Implement 'info auto-load local-gdbinit'. */
1214 info_auto_load_local_gdbinit (char *args, int from_tty)
1216 if (auto_load_local_gdbinit_pathname == NULL)
1217 printf_filtered (_("Local .gdbinit file was not found.\n"));
1218 else if (auto_load_local_gdbinit_loaded)
1219 printf_filtered (_("Local .gdbinit file \"%s\" has been loaded.\n"),
1220 auto_load_local_gdbinit_pathname);
1222 printf_filtered (_("Local .gdbinit file \"%s\" has not been loaded.\n"),
1223 auto_load_local_gdbinit_pathname);
1226 /* Return non-zero if SCRIPT_NOT_FOUND_WARNING_PRINTED of PSPACE_INFO was unset
1227 before calling this function. Always set SCRIPT_NOT_FOUND_WARNING_PRINTED
1231 script_not_found_warning_print (struct auto_load_pspace_info *pspace_info)
1233 int retval = !pspace_info->script_not_found_warning_printed;
1235 pspace_info->script_not_found_warning_printed = 1;
1240 /* The only valid "set auto-load" argument is off|0|no|disable. */
1243 set_auto_load_cmd (char *args, int from_tty)
1245 struct cmd_list_element *list;
1248 /* See parse_binary_operation in use by the sub-commands. */
1250 length = args ? strlen (args) : 0;
1252 while (length > 0 && (args[length - 1] == ' ' || args[length - 1] == '\t'))
1255 if (length == 0 || (strncmp (args, "off", length) != 0
1256 && strncmp (args, "0", length) != 0
1257 && strncmp (args, "no", length) != 0
1258 && strncmp (args, "disable", length) != 0))
1259 error (_("Valid is only global 'set auto-load no'; "
1260 "otherwise check the auto-load sub-commands."));
1262 for (list = *auto_load_set_cmdlist_get (); list != NULL; list = list->next)
1263 if (list->var_type == var_boolean)
1265 gdb_assert (list->type == set_cmd);
1266 do_set_command (args, from_tty, list);
1270 /* Initialize "set auto-load " commands prefix and return it. */
1272 struct cmd_list_element **
1273 auto_load_set_cmdlist_get (void)
1275 static struct cmd_list_element *retval;
1278 add_prefix_cmd ("auto-load", class_maintenance, set_auto_load_cmd, _("\
1279 Auto-loading specific settings.\n\
1280 Configure various auto-load-specific variables such as\n\
1281 automatic loading of Python scripts."),
1282 &retval, "set auto-load ",
1283 1/*allow-unknown*/, &setlist);
1288 /* Command "show auto-load" displays summary of all the current
1289 "show auto-load " settings. */
1292 show_auto_load_cmd (char *args, int from_tty)
1294 cmd_show_list (*auto_load_show_cmdlist_get (), from_tty, "");
1297 /* Initialize "show auto-load " commands prefix and return it. */
1299 struct cmd_list_element **
1300 auto_load_show_cmdlist_get (void)
1302 static struct cmd_list_element *retval;
1305 add_prefix_cmd ("auto-load", class_maintenance, show_auto_load_cmd, _("\
1306 Show auto-loading specific settings.\n\
1307 Show configuration of various auto-load-specific variables such as\n\
1308 automatic loading of Python scripts."),
1309 &retval, "show auto-load ",
1310 0/*allow-unknown*/, &showlist);
1315 /* Command "info auto-load" displays whether the various auto-load files have
1316 been loaded. This is reimplementation of cmd_show_list which inserts
1317 newlines at proper places. */
1320 info_auto_load_cmd (char *args, int from_tty)
1322 struct cmd_list_element *list;
1323 struct cleanup *infolist_chain;
1324 struct ui_out *uiout = current_uiout;
1326 infolist_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "infolist");
1328 for (list = *auto_load_info_cmdlist_get (); list != NULL; list = list->next)
1330 struct cleanup *option_chain
1331 = make_cleanup_ui_out_tuple_begin_end (uiout, "option");
1333 gdb_assert (!list->prefixlist);
1334 gdb_assert (list->type == not_set_cmd);
1336 ui_out_field_string (uiout, "name", list->name);
1337 ui_out_text (uiout, ": ");
1338 cmd_func (list, auto_load_info_scripts_pattern_nl, from_tty);
1340 /* Close the tuple. */
1341 do_cleanups (option_chain);
1344 /* Close the tuple. */
1345 do_cleanups (infolist_chain);
1348 /* Initialize "info auto-load " commands prefix and return it. */
1350 struct cmd_list_element **
1351 auto_load_info_cmdlist_get (void)
1353 static struct cmd_list_element *retval;
1356 add_prefix_cmd ("auto-load", class_info, info_auto_load_cmd, _("\
1357 Print current status of auto-loaded files.\n\
1358 Print whether various files like Python scripts or .gdbinit files have been\n\
1359 found and/or loaded."),
1360 &retval, "info auto-load ",
1361 0/*allow-unknown*/, &infolist);
1366 void _initialize_auto_load (void);
1369 _initialize_auto_load (void)
1371 struct cmd_list_element *cmd;
1372 char *scripts_directory_help;
1374 auto_load_pspace_data
1375 = register_program_space_data_with_cleanup (NULL,
1376 auto_load_pspace_data_cleanup);
1378 observer_attach_new_objfile (auto_load_new_objfile);
1380 add_setshow_boolean_cmd ("gdb-scripts", class_support,
1381 &auto_load_gdb_scripts, _("\
1382 Enable or disable auto-loading of canned sequences of commands scripts."), _("\
1383 Show whether auto-loading of canned sequences of commands scripts is enabled."),
1385 If enabled, canned sequences of commands are loaded when the debugger reads\n\
1386 an executable or shared library.\n\
1387 This options has security implications for untrusted inferiors."),
1388 NULL, show_auto_load_gdb_scripts,
1389 auto_load_set_cmdlist_get (),
1390 auto_load_show_cmdlist_get ());
1392 add_cmd ("gdb-scripts", class_info, info_auto_load_gdb_scripts,
1393 _("Print the list of automatically loaded sequences of commands.\n\
1394 Usage: info auto-load gdb-scripts [REGEXP]"),
1395 auto_load_info_cmdlist_get ());
1397 add_setshow_boolean_cmd ("local-gdbinit", class_support,
1398 &auto_load_local_gdbinit, _("\
1399 Enable or disable auto-loading of .gdbinit script in current directory."), _("\
1400 Show whether auto-loading .gdbinit script in current directory is enabled."),
1402 If enabled, canned sequences of commands are loaded when debugger starts\n\
1403 from .gdbinit file in current directory. Such files are deprecated,\n\
1404 use a script associated with inferior executable file instead.\n\
1405 This options has security implications for untrusted inferiors."),
1406 NULL, show_auto_load_local_gdbinit,
1407 auto_load_set_cmdlist_get (),
1408 auto_load_show_cmdlist_get ());
1410 add_cmd ("local-gdbinit", class_info, info_auto_load_local_gdbinit,
1411 _("Print whether current directory .gdbinit file has been loaded.\n\
1412 Usage: info auto-load local-gdbinit"),
1413 auto_load_info_cmdlist_get ());
1415 auto_load_dir = xstrdup (AUTO_LOAD_DIR);
1416 scripts_directory_help = xstrprintf (
1419 Automatically loaded Python scripts (named OBJFILE%s) and GDB scripts\n\
1420 (named OBJFILE%s) are located in one of the directories listed by this\n\
1423 GDBPY_AUTO_FILE_NAME,
1426 Automatically loaded GDB scripts (named OBJFILE%s) are located in one\n\
1427 of the directories listed by this option.\n\
1432 This option is ignored for the kinds of scripts \
1433 having 'set auto-load ... off'.\n\
1434 Directories listed here need to be present also \
1435 in the 'set auto-load safe-path'\n\
1437 add_setshow_optional_filename_cmd ("scripts-directory", class_support,
1438 &auto_load_dir, _("\
1439 Set the list of directories from which to load auto-loaded scripts."), _("\
1440 Show the list of directories from which to load auto-loaded scripts."),
1441 scripts_directory_help,
1442 set_auto_load_dir, show_auto_load_dir,
1443 auto_load_set_cmdlist_get (),
1444 auto_load_show_cmdlist_get ());
1445 xfree (scripts_directory_help);
1447 auto_load_safe_path = xstrdup (AUTO_LOAD_SAFE_PATH);
1448 auto_load_safe_path_vec_update ();
1449 add_setshow_optional_filename_cmd ("safe-path", class_support,
1450 &auto_load_safe_path, _("\
1451 Set the list of files and directories that are safe for auto-loading."), _("\
1452 Show the list of files and directories that are safe for auto-loading."), _("\
1453 Various files loaded automatically for the 'set auto-load ...' options must\n\
1454 be located in one of the directories listed by this option. Warning will be\n\
1455 printed and file will not be used otherwise.\n\
1456 You can mix both directory and filename entries.\n\
1457 Setting this parameter to an empty list resets it to its default value.\n\
1458 Setting this parameter to '/' (without the quotes) allows any file\n\
1459 for the 'set auto-load ...' options. Each path entry can be also shell\n\
1460 wildcard pattern; '*' does not match directory separator.\n\
1461 This option is ignored for the kinds of files having 'set auto-load ... off'.\n\
1462 This options has security implications for untrusted inferiors."),
1463 set_auto_load_safe_path,
1464 show_auto_load_safe_path,
1465 auto_load_set_cmdlist_get (),
1466 auto_load_show_cmdlist_get ());
1467 observer_attach_gdb_datadir_changed (auto_load_gdb_datadir_changed);
1469 cmd = add_cmd ("add-auto-load-safe-path", class_support,
1470 add_auto_load_safe_path,
1471 _("Add entries to the list of directories from which it is safe "
1472 "to auto-load files.\n\
1473 See the commands 'set auto-load safe-path' and 'show auto-load safe-path' to\n\
1474 access the current full list setting."),
1476 set_cmd_completer (cmd, filename_completer);
1478 add_setshow_boolean_cmd ("auto-load", class_maintenance,
1479 &debug_auto_load, _("\
1480 Set auto-load verifications debugging."), _("\
1481 Show auto-load verifications debugging."), _("\
1482 When non-zero, debugging output for files of 'set auto-load ...'\n\
1484 NULL, show_debug_auto_load,
1485 &setdebuglist, &showdebuglist);