nm: Add --quiet to suppress "no symbols" diagnostic
[binutils-gdb.git] / gdb / auto-load.c
blob1dfcf21eeebf220853371342e1c0c92bf78f7f65
1 /* GDB routines for supporting auto-loaded scripts.
3 Copyright (C) 2012-2021 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/>. */
20 #include "defs.h"
21 #include <ctype.h>
22 #include "auto-load.h"
23 #include "progspace.h"
24 #include "gdb_regex.h"
25 #include "ui-out.h"
26 #include "filenames.h"
27 #include "command.h"
28 #include "observable.h"
29 #include "objfiles.h"
30 #include "cli/cli-script.h"
31 #include "gdbcmd.h"
32 #include "cli/cli-cmds.h"
33 #include "cli/cli-decode.h"
34 #include "cli/cli-setshow.h"
35 #include "readline/tilde.h"
36 #include "completer.h"
37 #include "fnmatch.h"
38 #include "top.h"
39 #include "gdbsupport/filestuff.h"
40 #include "extension.h"
41 #include "gdb/section-scripts.h"
42 #include <algorithm>
43 #include "gdbsupport/pathstuff.h"
44 #include "cli/cli-style.h"
46 /* The section to look in for auto-loaded scripts (in file formats that
47 support sections).
48 Each entry in this section is a record that begins with a leading byte
49 identifying the record type.
50 At the moment we only support one record type: A leading byte of 1,
51 followed by the path of a python script to load. */
52 #define AUTO_SECTION_NAME ".debug_gdb_scripts"
54 static void maybe_print_unsupported_script_warning
55 (struct auto_load_pspace_info *, struct objfile *objfile,
56 const struct extension_language_defn *language,
57 const char *section_name, unsigned offset);
59 static void maybe_print_script_not_found_warning
60 (struct auto_load_pspace_info *, struct objfile *objfile,
61 const struct extension_language_defn *language,
62 const char *section_name, unsigned offset);
64 /* See auto-load.h. */
66 bool debug_auto_load = false;
68 /* "show" command for the debug_auto_load configuration variable. */
70 static void
71 show_debug_auto_load (struct ui_file *file, int from_tty,
72 struct cmd_list_element *c, const char *value)
74 fprintf_filtered (file, _("Debugging output for files "
75 "of 'set auto-load ...' is %s.\n"),
76 value);
79 /* User-settable option to enable/disable auto-loading of GDB_AUTO_FILE_NAME
80 scripts:
81 set auto-load gdb-scripts on|off
82 This is true if we should auto-load associated scripts when an objfile
83 is opened, false otherwise. */
84 static bool auto_load_gdb_scripts = true;
86 /* "show" command for the auto_load_gdb_scripts configuration variable. */
88 static void
89 show_auto_load_gdb_scripts (struct ui_file *file, int from_tty,
90 struct cmd_list_element *c, const char *value)
92 fprintf_filtered (file, _("Auto-loading of canned sequences of commands "
93 "scripts is %s.\n"),
94 value);
97 /* See auto-load.h. */
99 bool
100 auto_load_gdb_scripts_enabled (const struct extension_language_defn *extlang)
102 return auto_load_gdb_scripts;
105 /* Internal-use flag to enable/disable auto-loading.
106 This is true if we should auto-load python code when an objfile is opened,
107 false otherwise.
109 Both auto_load_scripts && global_auto_load must be true to enable
110 auto-loading.
112 This flag exists to facilitate deferring auto-loading during start-up
113 until after ./.gdbinit has been read; it may augment the search directories
114 used to find the scripts. */
115 bool global_auto_load = true;
117 /* Auto-load .gdbinit file from the current directory? */
118 bool auto_load_local_gdbinit = true;
120 /* Absolute pathname to the current directory .gdbinit, if it exists. */
121 char *auto_load_local_gdbinit_pathname = NULL;
123 /* if AUTO_LOAD_LOCAL_GDBINIT_PATHNAME has been loaded. */
124 bool auto_load_local_gdbinit_loaded = false;
126 /* "show" command for the auto_load_local_gdbinit configuration variable. */
128 static void
129 show_auto_load_local_gdbinit (struct ui_file *file, int from_tty,
130 struct cmd_list_element *c, const char *value)
132 fprintf_filtered (file, _("Auto-loading of .gdbinit script from current "
133 "directory is %s.\n"),
134 value);
137 /* Directory list from which to load auto-loaded scripts. It is not checked
138 for absolute paths but they are strongly recommended. It is initialized by
139 _initialize_auto_load. */
140 static char *auto_load_dir;
142 /* "set" command for the auto_load_dir configuration variable. */
144 static void
145 set_auto_load_dir (const char *args, int from_tty, struct cmd_list_element *c)
147 /* Setting the variable to "" resets it to the compile time defaults. */
148 if (auto_load_dir[0] == '\0')
150 xfree (auto_load_dir);
151 auto_load_dir = xstrdup (AUTO_LOAD_DIR);
155 /* "show" command for the auto_load_dir configuration variable. */
157 static void
158 show_auto_load_dir (struct ui_file *file, int from_tty,
159 struct cmd_list_element *c, const char *value)
161 fprintf_filtered (file, _("List of directories from which to load "
162 "auto-loaded scripts is %s.\n"),
163 value);
166 /* Directory list safe to hold auto-loaded files. It is not checked for
167 absolute paths but they are strongly recommended. It is initialized by
168 _initialize_auto_load. */
169 static char *auto_load_safe_path;
171 /* Vector of directory elements of AUTO_LOAD_SAFE_PATH with each one normalized
172 by tilde_expand and possibly each entries has added its gdb_realpath
173 counterpart. */
174 static std::vector<gdb::unique_xmalloc_ptr<char>> auto_load_safe_path_vec;
176 /* Expand $datadir and $debugdir in STRING according to the rules of
177 substitute_path_component. */
179 static std::vector<gdb::unique_xmalloc_ptr<char>>
180 auto_load_expand_dir_vars (const char *string)
182 char *s = xstrdup (string);
183 substitute_path_component (&s, "$datadir", gdb_datadir.c_str ());
184 substitute_path_component (&s, "$debugdir", debug_file_directory);
186 if (debug_auto_load && strcmp (s, string) != 0)
187 auto_load_debug_printf ("Expanded $-variables to \"%s\".", s);
189 std::vector<gdb::unique_xmalloc_ptr<char>> dir_vec
190 = dirnames_to_char_ptr_vec (s);
191 xfree(s);
193 return dir_vec;
196 /* Update auto_load_safe_path_vec from current AUTO_LOAD_SAFE_PATH. */
198 static void
199 auto_load_safe_path_vec_update (void)
201 auto_load_debug_printf ("Updating directories of \"%s\".",
202 auto_load_safe_path);
204 auto_load_safe_path_vec = auto_load_expand_dir_vars (auto_load_safe_path);
205 size_t len = auto_load_safe_path_vec.size ();
207 /* Apply tilde_expand and gdb_realpath to each AUTO_LOAD_SAFE_PATH_VEC
208 element. */
209 for (size_t i = 0; i < len; i++)
211 gdb::unique_xmalloc_ptr<char> &in_vec = auto_load_safe_path_vec[i];
212 gdb::unique_xmalloc_ptr<char> expanded (tilde_expand (in_vec.get ()));
213 gdb::unique_xmalloc_ptr<char> real_path = gdb_realpath (expanded.get ());
215 /* Ensure the current entry is at least tilde_expand-ed. ORIGINAL makes
216 sure we free the original string. */
217 gdb::unique_xmalloc_ptr<char> original = std::move (in_vec);
218 in_vec = std::move (expanded);
220 if (debug_auto_load)
222 if (strcmp (in_vec.get (), original.get ()) == 0)
223 auto_load_debug_printf ("Using directory \"%s\".",
224 in_vec.get ());
225 else
226 auto_load_debug_printf ("Resolved directory \"%s\" as \"%s\".",
227 original.get (), in_vec.get ());
230 /* If gdb_realpath returns a different content, append it. */
231 if (strcmp (real_path.get (), in_vec.get ()) != 0)
233 auto_load_debug_printf ("And canonicalized as \"%s\".",
234 real_path.get ());
236 auto_load_safe_path_vec.push_back (std::move (real_path));
241 /* Variable gdb_datadir has been set. Update content depending on $datadir. */
243 static void
244 auto_load_gdb_datadir_changed (void)
246 auto_load_safe_path_vec_update ();
249 /* "set" command for the auto_load_safe_path configuration variable. */
251 static void
252 set_auto_load_safe_path (const char *args,
253 int from_tty, struct cmd_list_element *c)
255 /* Setting the variable to "" resets it to the compile time defaults. */
256 if (auto_load_safe_path[0] == '\0')
258 xfree (auto_load_safe_path);
259 auto_load_safe_path = xstrdup (AUTO_LOAD_SAFE_PATH);
262 auto_load_safe_path_vec_update ();
265 /* "show" command for the auto_load_safe_path configuration variable. */
267 static void
268 show_auto_load_safe_path (struct ui_file *file, int from_tty,
269 struct cmd_list_element *c, const char *value)
271 const char *cs;
273 /* Check if user has entered either "/" or for example ":".
274 But while more complicate content like ":/foo" would still also
275 permit any location do not hide those. */
277 for (cs = value; *cs && (*cs == DIRNAME_SEPARATOR || IS_DIR_SEPARATOR (*cs));
278 cs++);
279 if (*cs == 0)
280 fprintf_filtered (file, _("Auto-load files are safe to load from any "
281 "directory.\n"));
282 else
283 fprintf_filtered (file, _("List of directories from which it is safe to "
284 "auto-load files is %s.\n"),
285 value);
288 /* "add-auto-load-safe-path" command for the auto_load_safe_path configuration
289 variable. */
291 static void
292 add_auto_load_safe_path (const char *args, int from_tty)
294 char *s;
296 if (args == NULL || *args == 0)
297 error (_("\
298 Directory argument required.\n\
299 Use 'set auto-load safe-path /' for disabling the auto-load safe-path security.\
300 "));
302 s = xstrprintf ("%s%c%s", auto_load_safe_path, DIRNAME_SEPARATOR, args);
303 xfree (auto_load_safe_path);
304 auto_load_safe_path = s;
306 auto_load_safe_path_vec_update ();
309 /* "add-auto-load-scripts-directory" command for the auto_load_dir configuration
310 variable. */
312 static void
313 add_auto_load_dir (const char *args, int from_tty)
315 char *s;
317 if (args == NULL || *args == 0)
318 error (_("Directory argument required."));
320 s = xstrprintf ("%s%c%s", auto_load_dir, DIRNAME_SEPARATOR, args);
321 xfree (auto_load_dir);
322 auto_load_dir = s;
325 /* Implementation for filename_is_in_pattern overwriting the caller's FILENAME
326 and PATTERN. */
328 static int
329 filename_is_in_pattern_1 (char *filename, char *pattern)
331 size_t pattern_len = strlen (pattern);
332 size_t filename_len = strlen (filename);
334 auto_load_debug_printf ("Matching file \"%s\" to pattern \"%s\"",
335 filename, pattern);
337 /* Trim trailing slashes ("/") from PATTERN. Even for "d:\" paths as
338 trailing slashes are trimmed also from FILENAME it still matches
339 correctly. */
340 while (pattern_len && IS_DIR_SEPARATOR (pattern[pattern_len - 1]))
341 pattern_len--;
342 pattern[pattern_len] = '\0';
344 /* Ensure auto_load_safe_path "/" matches any FILENAME. On MS-Windows
345 platform FILENAME even after gdb_realpath does not have to start with
346 IS_DIR_SEPARATOR character, such as the 'C:\x.exe' filename. */
347 if (pattern_len == 0)
349 auto_load_debug_printf ("Matched - empty pattern");
350 return 1;
353 for (;;)
355 /* Trim trailing slashes ("/"). PATTERN also has slashes trimmed the
356 same way so they will match. */
357 while (filename_len && IS_DIR_SEPARATOR (filename[filename_len - 1]))
358 filename_len--;
359 filename[filename_len] = '\0';
360 if (filename_len == 0)
362 auto_load_debug_printf ("Not matched - pattern \"%s\".", pattern);
363 return 0;
366 if (gdb_filename_fnmatch (pattern, filename, FNM_FILE_NAME | FNM_NOESCAPE)
367 == 0)
369 auto_load_debug_printf ("Matched - file \"%s\" to pattern \"%s\".",
370 filename, pattern);
371 return 1;
374 /* Trim trailing FILENAME component. */
375 while (filename_len > 0 && !IS_DIR_SEPARATOR (filename[filename_len - 1]))
376 filename_len--;
380 /* Return 1 if FILENAME matches PATTERN or if FILENAME resides in
381 a subdirectory of a directory that matches PATTERN. Return 0 otherwise.
382 gdb_realpath normalization is never done here. */
384 static ATTRIBUTE_PURE int
385 filename_is_in_pattern (const char *filename, const char *pattern)
387 char *filename_copy, *pattern_copy;
389 filename_copy = (char *) alloca (strlen (filename) + 1);
390 strcpy (filename_copy, filename);
391 pattern_copy = (char *) alloca (strlen (pattern) + 1);
392 strcpy (pattern_copy, pattern);
394 return filename_is_in_pattern_1 (filename_copy, pattern_copy);
397 /* Return 1 if FILENAME belongs to one of directory components of
398 AUTO_LOAD_SAFE_PATH_VEC. Return 0 otherwise.
399 auto_load_safe_path_vec_update is never called.
400 *FILENAME_REALP may be updated by gdb_realpath of FILENAME. */
402 static int
403 filename_is_in_auto_load_safe_path_vec (const char *filename,
404 gdb::unique_xmalloc_ptr<char> *filename_realp)
406 const char *pattern = NULL;
408 for (const gdb::unique_xmalloc_ptr<char> &p : auto_load_safe_path_vec)
409 if (*filename_realp == NULL && filename_is_in_pattern (filename, p.get ()))
411 pattern = p.get ();
412 break;
415 if (pattern == NULL)
417 if (*filename_realp == NULL)
419 *filename_realp = gdb_realpath (filename);
420 if (debug_auto_load && strcmp (filename_realp->get (), filename) != 0)
421 auto_load_debug_printf ("Resolved file \"%s\" as \"%s\".",
422 filename, filename_realp->get ());
425 if (strcmp (filename_realp->get (), filename) != 0)
426 for (const gdb::unique_xmalloc_ptr<char> &p : auto_load_safe_path_vec)
427 if (filename_is_in_pattern (filename_realp->get (), p.get ()))
429 pattern = p.get ();
430 break;
434 if (pattern != NULL)
436 auto_load_debug_printf ("File \"%s\" matches directory \"%s\".",
437 filename, pattern);
438 return 1;
441 return 0;
444 /* See auto-load.h. */
446 bool
447 file_is_auto_load_safe (const char *filename)
449 gdb::unique_xmalloc_ptr<char> filename_real;
450 static bool advice_printed = false;
452 if (filename_is_in_auto_load_safe_path_vec (filename, &filename_real))
453 return true;
455 auto_load_safe_path_vec_update ();
456 if (filename_is_in_auto_load_safe_path_vec (filename, &filename_real))
457 return true;
459 warning (_("File \"%ps\" auto-loading has been declined by your "
460 "`auto-load safe-path' set to \"%s\"."),
461 styled_string (file_name_style.style (), filename_real.get ()),
462 auto_load_safe_path);
464 if (!advice_printed)
466 /* Find the existing home directory config file. */
467 struct stat buf;
468 std::string home_config = find_gdb_home_config_file (GDBINIT, &buf);
469 if (home_config.empty ())
471 /* The user doesn't have an existing home directory config file,
472 so we should suggest a suitable path for them to use. */
473 std::string config_dir_file
474 = get_standard_config_filename (GDBINIT);
475 if (!config_dir_file.empty ())
476 home_config = config_dir_file;
477 else
479 const char *homedir = getenv ("HOME");
480 if (homedir == nullptr)
481 homedir = "$HOME";
482 home_config = (std::string (homedir) + SLASH_STRING
483 + std::string (GDBINIT));
487 printf_filtered (_("\
488 To enable execution of this file add\n\
489 \tadd-auto-load-safe-path %s\n\
490 line to your configuration file \"%s\".\n\
491 To completely disable this security protection add\n\
492 \tset auto-load safe-path /\n\
493 line to your configuration file \"%s\".\n\
494 For more information about this security protection see the\n\
495 \"Auto-loading safe path\" section in the GDB manual. E.g., run from the shell:\n\
496 \tinfo \"(gdb)Auto-loading safe path\"\n"),
497 filename_real.get (),
498 home_config.c_str (), home_config.c_str ());
499 advice_printed = true;
502 return false;
505 /* For scripts specified in .debug_gdb_scripts, multiple objfiles may load
506 the same script. There's no point in loading the script multiple times,
507 and there can be a lot of objfiles and scripts, so we keep track of scripts
508 loaded this way. */
510 struct auto_load_pspace_info
512 /* For each program space we keep track of loaded scripts, both when
513 specified as file names and as scripts to be executed directly. */
514 htab_up loaded_script_files;
515 htab_up loaded_script_texts;
517 /* Non-zero if we've issued the warning about an auto-load script not being
518 supported. We only want to issue this warning once. */
519 bool unsupported_script_warning_printed = false;
521 /* Non-zero if we've issued the warning about an auto-load script not being
522 found. We only want to issue this warning once. */
523 bool script_not_found_warning_printed = false;
526 /* Objects of this type are stored in the loaded_script hash table. */
528 struct loaded_script
530 /* Name as provided by the objfile. */
531 const char *name;
533 /* Full path name or NULL if script wasn't found (or was otherwise
534 inaccessible), or NULL for loaded_script_texts. */
535 const char *full_path;
537 /* True if this script has been loaded. */
538 bool loaded;
540 const struct extension_language_defn *language;
543 /* Per-program-space data key. */
544 static const struct program_space_key<struct auto_load_pspace_info>
545 auto_load_pspace_data;
547 /* Get the current autoload data. If none is found yet, add it now. This
548 function always returns a valid object. */
550 static struct auto_load_pspace_info *
551 get_auto_load_pspace_data (struct program_space *pspace)
553 struct auto_load_pspace_info *info;
555 info = auto_load_pspace_data.get (pspace);
556 if (info == NULL)
557 info = auto_load_pspace_data.emplace (pspace);
559 return info;
562 /* Hash function for the loaded script hash. */
564 static hashval_t
565 hash_loaded_script_entry (const void *data)
567 const struct loaded_script *e = (const struct loaded_script *) data;
569 return htab_hash_string (e->name) ^ htab_hash_pointer (e->language);
572 /* Equality function for the loaded script hash. */
574 static int
575 eq_loaded_script_entry (const void *a, const void *b)
577 const struct loaded_script *ea = (const struct loaded_script *) a;
578 const struct loaded_script *eb = (const struct loaded_script *) b;
580 return strcmp (ea->name, eb->name) == 0 && ea->language == eb->language;
583 /* Initialize the table to track loaded scripts.
584 Each entry is hashed by the full path name. */
586 static void
587 init_loaded_scripts_info (struct auto_load_pspace_info *pspace_info)
589 /* Choose 31 as the starting size of the hash table, somewhat arbitrarily.
590 Space for each entry is obtained with one malloc so we can free them
591 easily. */
593 pspace_info->loaded_script_files.reset
594 (htab_create (31,
595 hash_loaded_script_entry,
596 eq_loaded_script_entry,
597 xfree));
598 pspace_info->loaded_script_texts.reset
599 (htab_create (31,
600 hash_loaded_script_entry,
601 eq_loaded_script_entry,
602 xfree));
604 pspace_info->unsupported_script_warning_printed = false;
605 pspace_info->script_not_found_warning_printed = false;
608 /* Wrapper on get_auto_load_pspace_data to also allocate the hash table
609 for loading scripts. */
611 struct auto_load_pspace_info *
612 get_auto_load_pspace_data_for_loading (struct program_space *pspace)
614 struct auto_load_pspace_info *info;
616 info = get_auto_load_pspace_data (pspace);
617 if (info->loaded_script_files == NULL)
618 init_loaded_scripts_info (info);
620 return info;
623 /* Add script file NAME in LANGUAGE to hash table of PSPACE_INFO.
624 LOADED is true if the script has been (is going to) be loaded, false
625 otherwise (such as if it has not been found).
626 FULL_PATH is NULL if the script wasn't found.
628 The result is true if the script was already in the hash table. */
630 static bool
631 maybe_add_script_file (struct auto_load_pspace_info *pspace_info, bool loaded,
632 const char *name, const char *full_path,
633 const struct extension_language_defn *language)
635 struct htab *htab = pspace_info->loaded_script_files.get ();
636 struct loaded_script **slot, entry;
638 entry.name = name;
639 entry.language = language;
640 slot = (struct loaded_script **) htab_find_slot (htab, &entry, INSERT);
641 bool in_hash_table = *slot != NULL;
643 /* If this script is not in the hash table, add it. */
645 if (!in_hash_table)
647 char *p;
649 /* Allocate all space in one chunk so it's easier to free. */
650 *slot = ((struct loaded_script *)
651 xmalloc (sizeof (**slot)
652 + strlen (name) + 1
653 + (full_path != NULL ? (strlen (full_path) + 1) : 0)));
654 p = ((char*) *slot) + sizeof (**slot);
655 strcpy (p, name);
656 (*slot)->name = p;
657 if (full_path != NULL)
659 p += strlen (p) + 1;
660 strcpy (p, full_path);
661 (*slot)->full_path = p;
663 else
664 (*slot)->full_path = NULL;
665 (*slot)->loaded = loaded;
666 (*slot)->language = language;
669 return in_hash_table;
672 /* Add script contents NAME in LANGUAGE to hash table of PSPACE_INFO.
673 LOADED is true if the script has been (is going to) be loaded, false
674 otherwise (such as if it has not been found).
676 The result is true if the script was already in the hash table. */
678 static bool
679 maybe_add_script_text (struct auto_load_pspace_info *pspace_info,
680 bool loaded, const char *name,
681 const struct extension_language_defn *language)
683 struct htab *htab = pspace_info->loaded_script_texts.get ();
684 struct loaded_script **slot, entry;
686 entry.name = name;
687 entry.language = language;
688 slot = (struct loaded_script **) htab_find_slot (htab, &entry, INSERT);
689 bool in_hash_table = *slot != NULL;
691 /* If this script is not in the hash table, add it. */
693 if (!in_hash_table)
695 char *p;
697 /* Allocate all space in one chunk so it's easier to free. */
698 *slot = ((struct loaded_script *)
699 xmalloc (sizeof (**slot) + strlen (name) + 1));
700 p = ((char*) *slot) + sizeof (**slot);
701 strcpy (p, name);
702 (*slot)->name = p;
703 (*slot)->full_path = NULL;
704 (*slot)->loaded = loaded;
705 (*slot)->language = language;
708 return in_hash_table;
711 /* Clear the table of loaded section scripts. */
713 static void
714 clear_section_scripts (void)
716 struct program_space *pspace = current_program_space;
717 struct auto_load_pspace_info *info;
719 info = auto_load_pspace_data.get (pspace);
720 if (info != NULL && info->loaded_script_files != NULL)
721 auto_load_pspace_data.clear (pspace);
724 /* Look for the auto-load script in LANGUAGE associated with OBJFILE where
725 OBJFILE's gdb_realpath is REALNAME and load it. Return 1 if we found any
726 matching script, return 0 otherwise. */
728 static int
729 auto_load_objfile_script_1 (struct objfile *objfile, const char *realname,
730 const struct extension_language_defn *language)
732 const char *debugfile;
733 int retval;
734 const char *suffix = ext_lang_auto_load_suffix (language);
736 std::string filename = std::string (realname) + suffix;
738 gdb_file_up input = gdb_fopen_cloexec (filename.c_str (), "r");
739 debugfile = filename.c_str ();
741 auto_load_debug_printf ("Attempted file \"%s\" %s.",
742 debugfile,
743 input != nullptr ? "exists" : "does not exist");
745 std::string debugfile_holder;
746 if (!input)
748 /* Also try the same file in a subdirectory of gdb's data
749 directory. */
751 std::vector<gdb::unique_xmalloc_ptr<char>> vec
752 = auto_load_expand_dir_vars (auto_load_dir);
754 auto_load_debug_printf
755 ("Searching 'set auto-load scripts-directory' path \"%s\".",
756 auto_load_dir);
758 /* Convert Windows file name from c:/dir/file to /c/dir/file. */
759 if (HAS_DRIVE_SPEC (debugfile))
760 filename = (std::string("\\") + debugfile[0]
761 + STRIP_DRIVE_SPEC (debugfile));
763 for (const gdb::unique_xmalloc_ptr<char> &dir : vec)
765 /* FILENAME is absolute, so we don't need a "/" here. */
766 debugfile_holder = dir.get () + filename;
767 debugfile = debugfile_holder.c_str ();
769 input = gdb_fopen_cloexec (debugfile, "r");
771 auto_load_debug_printf ("Attempted file \"%s\" %s.",
772 debugfile,
773 (input != nullptr
774 ? "exists"
775 : "does not exist"));
777 if (input != NULL)
778 break;
782 if (input)
784 struct auto_load_pspace_info *pspace_info;
786 auto_load_debug_printf
787 ("Loading %s script \"%s\" by extension for objfile \"%s\".",
788 ext_lang_name (language), debugfile, objfile_name (objfile));
790 bool is_safe = file_is_auto_load_safe (debugfile);
792 /* Add this script to the hash table too so
793 "info auto-load ${lang}-scripts" can print it. */
794 pspace_info
795 = get_auto_load_pspace_data_for_loading (current_program_space);
796 maybe_add_script_file (pspace_info, is_safe, debugfile, debugfile,
797 language);
799 /* To preserve existing behaviour we don't check for whether the
800 script was already in the table, and always load it.
801 It's highly unlikely that we'd ever load it twice,
802 and these scripts are required to be idempotent under multiple
803 loads anyway. */
804 if (is_safe)
806 objfile_script_sourcer_func *sourcer
807 = ext_lang_objfile_script_sourcer (language);
809 /* We shouldn't get here if support for the language isn't
810 compiled in. And the extension language is required to implement
811 this function. */
812 gdb_assert (sourcer != NULL);
813 sourcer (language, objfile, input.get (), debugfile);
816 retval = 1;
818 else
819 retval = 0;
821 return retval;
824 /* Look for the auto-load script in LANGUAGE associated with OBJFILE and load
825 it. */
827 void
828 auto_load_objfile_script (struct objfile *objfile,
829 const struct extension_language_defn *language)
831 gdb::unique_xmalloc_ptr<char> realname
832 = gdb_realpath (objfile_name (objfile));
834 if (!auto_load_objfile_script_1 (objfile, realname.get (), language))
836 /* For Windows/DOS .exe executables, strip the .exe suffix, so that
837 FOO-gdb.gdb could be used for FOO.exe, and try again. */
839 size_t len = strlen (realname.get ());
840 const size_t lexe = sizeof (".exe") - 1;
842 if (len > lexe && strcasecmp (realname.get () + len - lexe, ".exe") == 0)
844 len -= lexe;
845 realname.get ()[len] = '\0';
847 auto_load_debug_printf
848 ("auto-load: Stripped .exe suffix, retrying with \"%s\".",
849 realname.get ());
851 auto_load_objfile_script_1 (objfile, realname.get (), language);
856 /* Subroutine of source_section_scripts to simplify it.
857 Load FILE as a script in extension language LANGUAGE.
858 The script is from section SECTION_NAME in OBJFILE at offset OFFSET. */
860 static void
861 source_script_file (struct auto_load_pspace_info *pspace_info,
862 struct objfile *objfile,
863 const struct extension_language_defn *language,
864 const char *section_name, unsigned int offset,
865 const char *file)
867 objfile_script_sourcer_func *sourcer;
869 /* Skip this script if support is not compiled in. */
870 sourcer = ext_lang_objfile_script_sourcer (language);
871 if (sourcer == NULL)
873 /* We don't throw an error, the program is still debuggable. */
874 maybe_print_unsupported_script_warning (pspace_info, objfile, language,
875 section_name, offset);
876 /* We *could* still try to open it, but there's no point. */
877 maybe_add_script_file (pspace_info, 0, file, NULL, language);
878 return;
881 /* Skip this script if auto-loading it has been disabled. */
882 if (!ext_lang_auto_load_enabled (language))
884 /* No message is printed, just skip it. */
885 return;
888 gdb::optional<open_script> opened = find_and_open_script (file,
889 1 /*search_path*/);
891 if (opened)
893 auto_load_debug_printf
894 ("Loading %s script \"%s\" from section \"%s\" of objfile \"%s\".",
895 ext_lang_name (language), opened->full_path.get (),
896 section_name, objfile_name (objfile));
898 if (!file_is_auto_load_safe (opened->full_path.get ()))
899 opened.reset ();
901 else
903 /* If one script isn't found it's not uncommon for more to not be
904 found either. We don't want to print a message for each script,
905 too much noise. Instead, we print the warning once and tell the
906 user how to find the list of scripts that weren't loaded.
907 We don't throw an error, the program is still debuggable.
909 IWBN if complaints.c were more general-purpose. */
911 maybe_print_script_not_found_warning (pspace_info, objfile, language,
912 section_name, offset);
915 bool in_hash_table
916 = maybe_add_script_file (pspace_info, bool (opened), file,
917 (opened ? opened->full_path.get (): NULL),
918 language);
920 /* If this file is not currently loaded, load it. */
921 if (opened && !in_hash_table)
922 sourcer (language, objfile, opened->stream.get (),
923 opened->full_path.get ());
926 /* Subroutine of source_section_scripts to simplify it.
927 Execute SCRIPT as a script in extension language LANG.
928 The script is from section SECTION_NAME in OBJFILE at offset OFFSET. */
930 static void
931 execute_script_contents (struct auto_load_pspace_info *pspace_info,
932 struct objfile *objfile,
933 const struct extension_language_defn *language,
934 const char *section_name, unsigned int offset,
935 const char *script)
937 objfile_script_executor_func *executor;
938 const char *newline, *script_text;
939 const char *name;
941 /* The first line of the script is the name of the script.
942 It must not contain any kind of space character. */
943 name = NULL;
944 newline = strchr (script, '\n');
945 std::string name_holder;
946 if (newline != NULL)
948 const char *buf, *p;
950 /* Put the name in a buffer and validate it. */
951 name_holder = std::string (script, newline - script);
952 buf = name_holder.c_str ();
953 for (p = buf; *p != '\0'; ++p)
955 if (isspace (*p))
956 break;
958 /* We don't allow nameless scripts, they're not helpful to the user. */
959 if (p != buf && *p == '\0')
960 name = buf;
962 if (name == NULL)
964 /* We don't throw an error, the program is still debuggable. */
965 warning (_("\
966 Missing/bad script name in entry at offset %u in section %s\n\
967 of file %ps."),
968 offset, section_name,
969 styled_string (file_name_style.style (),
970 objfile_name (objfile)));
971 return;
973 script_text = newline + 1;
975 /* Skip this script if support is not compiled in. */
976 executor = ext_lang_objfile_script_executor (language);
977 if (executor == NULL)
979 /* We don't throw an error, the program is still debuggable. */
980 maybe_print_unsupported_script_warning (pspace_info, objfile, language,
981 section_name, offset);
982 maybe_add_script_text (pspace_info, 0, name, language);
983 return;
986 /* Skip this script if auto-loading it has been disabled. */
987 if (!ext_lang_auto_load_enabled (language))
989 /* No message is printed, just skip it. */
990 return;
993 auto_load_debug_printf
994 ("Loading %s script \"%s\" from section \"%s\" of objfile \"%s\".",
995 ext_lang_name (language), name, section_name, objfile_name (objfile));
997 bool is_safe = file_is_auto_load_safe (objfile_name (objfile));
999 bool in_hash_table
1000 = maybe_add_script_text (pspace_info, is_safe, name, language);
1002 /* If this file is not currently loaded, load it. */
1003 if (is_safe && !in_hash_table)
1004 executor (language, objfile, name, script_text);
1007 /* Load scripts specified in OBJFILE.
1008 START,END delimit a buffer containing a list of nul-terminated
1009 file names.
1010 SECTION_NAME is used in error messages.
1012 Scripts specified as file names are found per normal "source -s" command
1013 processing. First the script is looked for in $cwd. If not found there
1014 the source search path is used.
1016 The section contains a list of path names of script files to load or
1017 actual script contents. Each entry is nul-terminated. */
1019 static void
1020 source_section_scripts (struct objfile *objfile, const char *section_name,
1021 const char *start, const char *end)
1023 const char *p;
1024 struct auto_load_pspace_info *pspace_info;
1026 pspace_info = get_auto_load_pspace_data_for_loading (current_program_space);
1028 for (p = start; p < end; ++p)
1030 const char *entry;
1031 const struct extension_language_defn *language;
1032 unsigned int offset = p - start;
1033 int code = *p;
1035 switch (code)
1037 case SECTION_SCRIPT_ID_PYTHON_FILE:
1038 case SECTION_SCRIPT_ID_PYTHON_TEXT:
1039 language = get_ext_lang_defn (EXT_LANG_PYTHON);
1040 break;
1041 case SECTION_SCRIPT_ID_SCHEME_FILE:
1042 case SECTION_SCRIPT_ID_SCHEME_TEXT:
1043 language = get_ext_lang_defn (EXT_LANG_GUILE);
1044 break;
1045 default:
1046 warning (_("Invalid entry in %s section"), section_name);
1047 /* We could try various heuristics to find the next valid entry,
1048 but it's safer to just punt. */
1049 return;
1051 entry = ++p;
1053 while (p < end && *p != '\0')
1054 ++p;
1055 if (p == end)
1057 warning (_("Non-nul-terminated entry in %s at offset %u"),
1058 section_name, offset);
1059 /* Don't load/execute it. */
1060 break;
1063 switch (code)
1065 case SECTION_SCRIPT_ID_PYTHON_FILE:
1066 case SECTION_SCRIPT_ID_SCHEME_FILE:
1067 if (p == entry)
1069 warning (_("Empty entry in %s at offset %u"),
1070 section_name, offset);
1071 continue;
1073 source_script_file (pspace_info, objfile, language,
1074 section_name, offset, entry);
1075 break;
1076 case SECTION_SCRIPT_ID_PYTHON_TEXT:
1077 case SECTION_SCRIPT_ID_SCHEME_TEXT:
1078 execute_script_contents (pspace_info, objfile, language,
1079 section_name, offset, entry);
1080 break;
1085 /* Load scripts specified in section SECTION_NAME of OBJFILE. */
1087 static void
1088 auto_load_section_scripts (struct objfile *objfile, const char *section_name)
1090 bfd *abfd = objfile->obfd;
1091 asection *scripts_sect;
1092 bfd_byte *data = NULL;
1094 scripts_sect = bfd_get_section_by_name (abfd, section_name);
1095 if (scripts_sect == NULL
1096 || (bfd_section_flags (scripts_sect) & SEC_HAS_CONTENTS) == 0)
1097 return;
1099 if (!bfd_get_full_section_contents (abfd, scripts_sect, &data))
1100 warning (_("Couldn't read %s section of %ps"),
1101 section_name,
1102 styled_string (file_name_style.style (),
1103 bfd_get_filename (abfd)));
1104 else
1106 gdb::unique_xmalloc_ptr<bfd_byte> data_holder (data);
1108 char *p = (char *) data;
1109 source_section_scripts (objfile, section_name, p,
1110 p + bfd_section_size (scripts_sect));
1114 /* Load any auto-loaded scripts for OBJFILE. */
1116 void
1117 load_auto_scripts_for_objfile (struct objfile *objfile)
1119 /* Return immediately if auto-loading has been globally disabled.
1120 This is to handle sequencing of operations during gdb startup.
1121 Also return immediately if OBJFILE was not created from a file
1122 on the local filesystem. */
1123 if (!global_auto_load
1124 || (objfile->flags & OBJF_NOT_FILENAME) != 0
1125 || is_target_filename (objfile->original_name))
1126 return;
1128 /* Load any extension language scripts for this objfile.
1129 E.g., foo-gdb.gdb, foo-gdb.py. */
1130 auto_load_ext_lang_scripts_for_objfile (objfile);
1132 /* Load any scripts mentioned in AUTO_SECTION_NAME (.debug_gdb_scripts). */
1133 auto_load_section_scripts (objfile, AUTO_SECTION_NAME);
1136 /* This is a new_objfile observer callback to auto-load scripts.
1138 Two flavors of auto-loaded scripts are supported.
1139 1) based on the path to the objfile
1140 2) from .debug_gdb_scripts section */
1142 static void
1143 auto_load_new_objfile (struct objfile *objfile)
1145 if (!objfile)
1147 /* OBJFILE is NULL when loading a new "main" symbol-file. */
1148 clear_section_scripts ();
1149 return;
1152 load_auto_scripts_for_objfile (objfile);
1155 /* Collect scripts to be printed in a vec. */
1157 struct collect_matching_scripts_data
1159 collect_matching_scripts_data (std::vector<loaded_script *> *scripts_p_,
1160 const extension_language_defn *language_)
1161 : scripts_p (scripts_p_), language (language_)
1164 std::vector<loaded_script *> *scripts_p;
1165 const struct extension_language_defn *language;
1168 /* Traversal function for htab_traverse.
1169 Collect the entry if it matches the regexp. */
1171 static int
1172 collect_matching_scripts (void **slot, void *info)
1174 struct loaded_script *script = (struct loaded_script *) *slot;
1175 struct collect_matching_scripts_data *data
1176 = (struct collect_matching_scripts_data *) info;
1178 if (script->language == data->language && re_exec (script->name))
1179 data->scripts_p->push_back (script);
1181 return 1;
1184 /* Print SCRIPT. */
1186 static void
1187 print_script (struct loaded_script *script)
1189 struct ui_out *uiout = current_uiout;
1191 ui_out_emit_tuple tuple_emitter (uiout, NULL);
1193 uiout->field_string ("loaded", script->loaded ? "Yes" : "No");
1194 uiout->field_string ("script", script->name);
1195 uiout->text ("\n");
1197 /* If the name isn't the full path, print it too. */
1198 if (script->full_path != NULL
1199 && strcmp (script->name, script->full_path) != 0)
1201 uiout->text ("\tfull name: ");
1202 uiout->field_string ("full_path", script->full_path);
1203 uiout->text ("\n");
1207 /* Helper for info_auto_load_scripts to sort the scripts by name. */
1209 static bool
1210 sort_scripts_by_name (loaded_script *a, loaded_script *b)
1212 return FILENAME_CMP (a->name, b->name) < 0;
1215 /* Special internal GDB value of auto_load_info_scripts's PATTERN identify
1216 the "info auto-load XXX" command has been executed through the general
1217 "info auto-load" invocation. Extra newline will be printed if needed. */
1218 char auto_load_info_scripts_pattern_nl[] = "";
1220 /* Subroutine of auto_load_info_scripts to simplify it.
1221 Print SCRIPTS. */
1223 static void
1224 print_scripts (const std::vector<loaded_script *> &scripts)
1226 for (loaded_script *script : scripts)
1227 print_script (script);
1230 /* Implementation for "info auto-load gdb-scripts"
1231 (and "info auto-load python-scripts"). List scripts in LANGUAGE matching
1232 PATTERN. FROM_TTY is the usual GDB boolean for user interactivity. */
1234 void
1235 auto_load_info_scripts (const char *pattern, int from_tty,
1236 const struct extension_language_defn *language)
1238 struct ui_out *uiout = current_uiout;
1239 struct auto_load_pspace_info *pspace_info;
1241 dont_repeat ();
1243 pspace_info = get_auto_load_pspace_data (current_program_space);
1245 if (pattern && *pattern)
1247 char *re_err = re_comp (pattern);
1249 if (re_err)
1250 error (_("Invalid regexp: %s"), re_err);
1252 else
1254 re_comp ("");
1257 /* We need to know the number of rows before we build the table.
1258 Plus we want to sort the scripts by name.
1259 So first traverse the hash table collecting the matching scripts. */
1261 std::vector<loaded_script *> script_files, script_texts;
1263 if (pspace_info != NULL && pspace_info->loaded_script_files != NULL)
1265 collect_matching_scripts_data data (&script_files, language);
1267 /* Pass a pointer to scripts as VEC_safe_push can realloc space. */
1268 htab_traverse_noresize (pspace_info->loaded_script_files.get (),
1269 collect_matching_scripts, &data);
1271 std::sort (script_files.begin (), script_files.end (),
1272 sort_scripts_by_name);
1275 if (pspace_info != NULL && pspace_info->loaded_script_texts != NULL)
1277 collect_matching_scripts_data data (&script_texts, language);
1279 /* Pass a pointer to scripts as VEC_safe_push can realloc space. */
1280 htab_traverse_noresize (pspace_info->loaded_script_texts.get (),
1281 collect_matching_scripts, &data);
1283 std::sort (script_texts.begin (), script_texts.end (),
1284 sort_scripts_by_name);
1287 int nr_scripts = script_files.size () + script_texts.size ();
1289 /* Table header shifted right by preceding "gdb-scripts: " would not match
1290 its columns. */
1291 if (nr_scripts > 0 && pattern == auto_load_info_scripts_pattern_nl)
1292 uiout->text ("\n");
1295 ui_out_emit_table table_emitter (uiout, 2, nr_scripts,
1296 "AutoLoadedScriptsTable");
1298 uiout->table_header (7, ui_left, "loaded", "Loaded");
1299 uiout->table_header (70, ui_left, "script", "Script");
1300 uiout->table_body ();
1302 print_scripts (script_files);
1303 print_scripts (script_texts);
1306 if (nr_scripts == 0)
1308 if (pattern && *pattern)
1309 uiout->message ("No auto-load scripts matching %s.\n", pattern);
1310 else
1311 uiout->message ("No auto-load scripts.\n");
1315 /* Wrapper for "info auto-load gdb-scripts". */
1317 static void
1318 info_auto_load_gdb_scripts (const char *pattern, int from_tty)
1320 auto_load_info_scripts (pattern, from_tty, &extension_language_gdb);
1323 /* Implement 'info auto-load local-gdbinit'. */
1325 static void
1326 info_auto_load_local_gdbinit (const char *args, int from_tty)
1328 if (auto_load_local_gdbinit_pathname == NULL)
1329 printf_filtered (_("Local .gdbinit file was not found.\n"));
1330 else if (auto_load_local_gdbinit_loaded)
1331 printf_filtered (_("Local .gdbinit file \"%ps\" has been loaded.\n"),
1332 styled_string (file_name_style.style (),
1333 auto_load_local_gdbinit_pathname));
1334 else
1335 printf_filtered (_("Local .gdbinit file \"%ps\" has not been loaded.\n"),
1336 styled_string (file_name_style.style (),
1337 auto_load_local_gdbinit_pathname));
1340 /* Print an "unsupported script" warning if it has not already been printed.
1341 The script is in language LANGUAGE at offset OFFSET in section SECTION_NAME
1342 of OBJFILE. */
1344 static void
1345 maybe_print_unsupported_script_warning
1346 (struct auto_load_pspace_info *pspace_info,
1347 struct objfile *objfile, const struct extension_language_defn *language,
1348 const char *section_name, unsigned offset)
1350 if (!pspace_info->unsupported_script_warning_printed)
1352 warning (_("\
1353 Unsupported auto-load script at offset %u in section %s\n\
1354 of file %ps.\n\
1355 Use `info auto-load %s-scripts [REGEXP]' to list them."),
1356 offset, section_name,
1357 styled_string (file_name_style.style (),
1358 objfile_name (objfile)),
1359 ext_lang_name (language));
1360 pspace_info->unsupported_script_warning_printed = true;
1364 /* Return non-zero if SCRIPT_NOT_FOUND_WARNING_PRINTED of PSPACE_INFO was unset
1365 before calling this function. Always set SCRIPT_NOT_FOUND_WARNING_PRINTED
1366 of PSPACE_INFO. */
1368 static void
1369 maybe_print_script_not_found_warning
1370 (struct auto_load_pspace_info *pspace_info,
1371 struct objfile *objfile, const struct extension_language_defn *language,
1372 const char *section_name, unsigned offset)
1374 if (!pspace_info->script_not_found_warning_printed)
1376 warning (_("\
1377 Missing auto-load script at offset %u in section %s\n\
1378 of file %ps.\n\
1379 Use `info auto-load %s-scripts [REGEXP]' to list them."),
1380 offset, section_name,
1381 styled_string (file_name_style.style (),
1382 objfile_name (objfile)),
1383 ext_lang_name (language));
1384 pspace_info->script_not_found_warning_printed = true;
1388 /* The only valid "set auto-load" argument is off|0|no|disable. */
1390 static void
1391 set_auto_load_cmd (const char *args, int from_tty)
1393 struct cmd_list_element *list;
1394 size_t length;
1396 /* See parse_binary_operation in use by the sub-commands. */
1398 length = args ? strlen (args) : 0;
1400 while (length > 0 && (args[length - 1] == ' ' || args[length - 1] == '\t'))
1401 length--;
1403 if (length == 0 || (strncmp (args, "off", length) != 0
1404 && strncmp (args, "0", length) != 0
1405 && strncmp (args, "no", length) != 0
1406 && strncmp (args, "disable", length) != 0))
1407 error (_("Valid is only global 'set auto-load no'; "
1408 "otherwise check the auto-load sub-commands."));
1410 for (list = *auto_load_set_cmdlist_get (); list != NULL; list = list->next)
1411 if (list->var_type == var_boolean)
1413 gdb_assert (list->type == set_cmd);
1414 do_set_command (args, from_tty, list);
1418 /* Initialize "set auto-load " commands prefix and return it. */
1420 struct cmd_list_element **
1421 auto_load_set_cmdlist_get (void)
1423 static struct cmd_list_element *retval;
1425 if (retval == NULL)
1426 add_prefix_cmd ("auto-load", class_maintenance, set_auto_load_cmd, _("\
1427 Auto-loading specific settings.\n\
1428 Configure various auto-load-specific variables such as\n\
1429 automatic loading of Python scripts."),
1430 &retval, "set auto-load ",
1431 1/*allow-unknown*/, &setlist);
1433 return &retval;
1436 /* Initialize "show auto-load " commands prefix and return it. */
1438 struct cmd_list_element **
1439 auto_load_show_cmdlist_get (void)
1441 static struct cmd_list_element *retval;
1443 if (retval == NULL)
1444 add_show_prefix_cmd ("auto-load", class_maintenance, _("\
1445 Show auto-loading specific settings.\n\
1446 Show configuration of various auto-load-specific variables such as\n\
1447 automatic loading of Python scripts."),
1448 &retval, "show auto-load ",
1449 0/*allow-unknown*/, &showlist);
1451 return &retval;
1454 /* Command "info auto-load" displays whether the various auto-load files have
1455 been loaded. This is reimplementation of cmd_show_list which inserts
1456 newlines at proper places. */
1458 static void
1459 info_auto_load_cmd (const char *args, int from_tty)
1461 struct cmd_list_element *list;
1462 struct ui_out *uiout = current_uiout;
1464 ui_out_emit_tuple tuple_emitter (uiout, "infolist");
1466 for (list = *auto_load_info_cmdlist_get (); list != NULL; list = list->next)
1468 ui_out_emit_tuple option_emitter (uiout, "option");
1470 gdb_assert (!list->prefixlist);
1471 gdb_assert (list->type == not_set_cmd);
1473 uiout->field_string ("name", list->name);
1474 uiout->text (": ");
1475 cmd_func (list, auto_load_info_scripts_pattern_nl, from_tty);
1479 /* Initialize "info auto-load " commands prefix and return it. */
1481 struct cmd_list_element **
1482 auto_load_info_cmdlist_get (void)
1484 static struct cmd_list_element *retval;
1486 if (retval == NULL)
1487 add_prefix_cmd ("auto-load", class_info, info_auto_load_cmd, _("\
1488 Print current status of auto-loaded files.\n\
1489 Print whether various files like Python scripts or .gdbinit files have been\n\
1490 found and/or loaded."),
1491 &retval, "info auto-load ",
1492 0/*allow-unknown*/, &infolist);
1494 return &retval;
1497 void _initialize_auto_load ();
1498 void
1499 _initialize_auto_load ()
1501 struct cmd_list_element *cmd;
1502 char *scripts_directory_help, *gdb_name_help, *python_name_help;
1503 char *guile_name_help;
1504 const char *suffix;
1506 gdb::observers::new_objfile.attach (auto_load_new_objfile);
1508 add_setshow_boolean_cmd ("gdb-scripts", class_support,
1509 &auto_load_gdb_scripts, _("\
1510 Enable or disable auto-loading of canned sequences of commands scripts."), _("\
1511 Show whether auto-loading of canned sequences of commands scripts is enabled."),
1512 _("\
1513 If enabled, canned sequences of commands are loaded when the debugger reads\n\
1514 an executable or shared library.\n\
1515 This option has security implications for untrusted inferiors."),
1516 NULL, show_auto_load_gdb_scripts,
1517 auto_load_set_cmdlist_get (),
1518 auto_load_show_cmdlist_get ());
1520 add_cmd ("gdb-scripts", class_info, info_auto_load_gdb_scripts,
1521 _("Print the list of automatically loaded sequences of commands.\n\
1522 Usage: info auto-load gdb-scripts [REGEXP]"),
1523 auto_load_info_cmdlist_get ());
1525 add_setshow_boolean_cmd ("local-gdbinit", class_support,
1526 &auto_load_local_gdbinit, _("\
1527 Enable or disable auto-loading of .gdbinit script in current directory."), _("\
1528 Show whether auto-loading .gdbinit script in current directory is enabled."),
1529 _("\
1530 If enabled, canned sequences of commands are loaded when debugger starts\n\
1531 from .gdbinit file in current directory. Such files are deprecated,\n\
1532 use a script associated with inferior executable file instead.\n\
1533 This option has security implications for untrusted inferiors."),
1534 NULL, show_auto_load_local_gdbinit,
1535 auto_load_set_cmdlist_get (),
1536 auto_load_show_cmdlist_get ());
1538 add_cmd ("local-gdbinit", class_info, info_auto_load_local_gdbinit,
1539 _("Print whether current directory .gdbinit file has been loaded.\n\
1540 Usage: info auto-load local-gdbinit"),
1541 auto_load_info_cmdlist_get ());
1543 auto_load_dir = xstrdup (AUTO_LOAD_DIR);
1545 suffix = ext_lang_auto_load_suffix (get_ext_lang_defn (EXT_LANG_GDB));
1546 gdb_name_help
1547 = xstrprintf (_("\
1548 GDB scripts: OBJFILE%s\n"),
1549 suffix);
1550 python_name_help = NULL;
1551 #ifdef HAVE_PYTHON
1552 suffix = ext_lang_auto_load_suffix (get_ext_lang_defn (EXT_LANG_PYTHON));
1553 python_name_help
1554 = xstrprintf (_("\
1555 Python scripts: OBJFILE%s\n"),
1556 suffix);
1557 #endif
1558 guile_name_help = NULL;
1559 #ifdef HAVE_GUILE
1560 suffix = ext_lang_auto_load_suffix (get_ext_lang_defn (EXT_LANG_GUILE));
1561 guile_name_help
1562 = xstrprintf (_("\
1563 Guile scripts: OBJFILE%s\n"),
1564 suffix);
1565 #endif
1566 scripts_directory_help
1567 = xstrprintf (_("\
1568 Automatically loaded scripts are located in one of the directories listed\n\
1569 by this option.\n\
1571 Script names:\n\
1572 %s%s%s\
1574 This option is ignored for the kinds of scripts \
1575 having 'set auto-load ... off'.\n\
1576 Directories listed here need to be present also \
1577 in the 'set auto-load safe-path'\n\
1578 option."),
1579 gdb_name_help,
1580 python_name_help ? python_name_help : "",
1581 guile_name_help ? guile_name_help : "");
1583 add_setshow_optional_filename_cmd ("scripts-directory", class_support,
1584 &auto_load_dir, _("\
1585 Set the list of directories from which to load auto-loaded scripts."), _("\
1586 Show the list of directories from which to load auto-loaded scripts."),
1587 scripts_directory_help,
1588 set_auto_load_dir, show_auto_load_dir,
1589 auto_load_set_cmdlist_get (),
1590 auto_load_show_cmdlist_get ());
1591 xfree (scripts_directory_help);
1592 xfree (python_name_help);
1593 xfree (gdb_name_help);
1594 xfree (guile_name_help);
1596 auto_load_safe_path = xstrdup (AUTO_LOAD_SAFE_PATH);
1597 auto_load_safe_path_vec_update ();
1598 add_setshow_optional_filename_cmd ("safe-path", class_support,
1599 &auto_load_safe_path, _("\
1600 Set the list of files and directories that are safe for auto-loading."), _("\
1601 Show the list of files and directories that are safe for auto-loading."), _("\
1602 Various files loaded automatically for the 'set auto-load ...' options must\n\
1603 be located in one of the directories listed by this option. Warning will be\n\
1604 printed and file will not be used otherwise.\n\
1605 You can mix both directory and filename entries.\n\
1606 Setting this parameter to an empty list resets it to its default value.\n\
1607 Setting this parameter to '/' (without the quotes) allows any file\n\
1608 for the 'set auto-load ...' options. Each path entry can be also shell\n\
1609 wildcard pattern; '*' does not match directory separator.\n\
1610 This option is ignored for the kinds of files having 'set auto-load ... off'.\n\
1611 This option has security implications for untrusted inferiors."),
1612 set_auto_load_safe_path,
1613 show_auto_load_safe_path,
1614 auto_load_set_cmdlist_get (),
1615 auto_load_show_cmdlist_get ());
1616 gdb::observers::gdb_datadir_changed.attach (auto_load_gdb_datadir_changed);
1618 cmd = add_cmd ("add-auto-load-safe-path", class_support,
1619 add_auto_load_safe_path,
1620 _("Add entries to the list of directories from which it is safe "
1621 "to auto-load files.\n\
1622 See the commands 'set auto-load safe-path' and 'show auto-load safe-path' to\n\
1623 access the current full list setting."),
1624 &cmdlist);
1625 set_cmd_completer (cmd, filename_completer);
1627 cmd = add_cmd ("add-auto-load-scripts-directory", class_support,
1628 add_auto_load_dir,
1629 _("Add entries to the list of directories from which to load "
1630 "auto-loaded scripts.\n\
1631 See the commands 'set auto-load scripts-directory' and\n\
1632 'show auto-load scripts-directory' to access the current full list setting."),
1633 &cmdlist);
1634 set_cmd_completer (cmd, filename_completer);
1636 add_setshow_boolean_cmd ("auto-load", class_maintenance,
1637 &debug_auto_load, _("\
1638 Set auto-load verifications debugging."), _("\
1639 Show auto-load verifications debugging."), _("\
1640 When non-zero, debugging output for files of 'set auto-load ...'\n\
1641 is displayed."),
1642 NULL, show_debug_auto_load,
1643 &setdebuglist, &showdebuglist);