Add translations for various sub-directories
[binutils-gdb.git] / gdb / auto-load.c
bloba8f3a8d5fdd474b190a2f419f93487594019a883
1 /* GDB routines for supporting auto-loaded scripts.
3 Copyright (C) 2012-2024 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 <ctype.h>
21 #include "auto-load.h"
22 #include "gdbsupport/gdb_vecs.h"
23 #include "progspace.h"
24 #include "gdbsupport/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 "cli/cli-cmds.h"
32 #include "cli/cli-decode.h"
33 #include "cli/cli-setshow.h"
34 #include "readline/tilde.h"
35 #include "completer.h"
36 #include "fnmatch.h"
37 #include "top.h"
38 #include "gdbsupport/filestuff.h"
39 #include "extension.h"
40 #include "gdb/section-scripts.h"
41 #include <algorithm>
42 #include "gdbsupport/pathstuff.h"
43 #include "cli/cli-style.h"
45 /* The section to look in for auto-loaded scripts (in file formats that
46 support sections).
47 Each entry in this section is a record that begins with a leading byte
48 identifying the record type.
49 At the moment we only support one record type: A leading byte of 1,
50 followed by the path of a python script to load. */
51 #define AUTO_SECTION_NAME ".debug_gdb_scripts"
53 /* The section to look in for the name of a separate debug file. */
54 #define DEBUGLINK_SECTION_NAME ".gnu_debuglink"
56 static void maybe_print_unsupported_script_warning
57 (struct auto_load_pspace_info *, struct objfile *objfile,
58 const struct extension_language_defn *language,
59 const char *section_name, unsigned offset);
61 static void maybe_print_script_not_found_warning
62 (struct auto_load_pspace_info *, struct objfile *objfile,
63 const struct extension_language_defn *language,
64 const char *section_name, unsigned offset);
66 /* See auto-load.h. */
68 bool debug_auto_load = false;
70 /* "show" command for the debug_auto_load configuration variable. */
72 static void
73 show_debug_auto_load (struct ui_file *file, int from_tty,
74 struct cmd_list_element *c, const char *value)
76 gdb_printf (file, _("Debugging output for files "
77 "of 'set auto-load ...' is %s.\n"),
78 value);
81 /* User-settable option to enable/disable auto-loading of GDB_AUTO_FILE_NAME
82 scripts:
83 set auto-load gdb-scripts on|off
84 This is true if we should auto-load associated scripts when an objfile
85 is opened, false otherwise. */
86 static bool auto_load_gdb_scripts = true;
88 /* "show" command for the auto_load_gdb_scripts configuration variable. */
90 static void
91 show_auto_load_gdb_scripts (struct ui_file *file, int from_tty,
92 struct cmd_list_element *c, const char *value)
94 gdb_printf (file, _("Auto-loading of canned sequences of commands "
95 "scripts is %s.\n"),
96 value);
99 /* See auto-load.h. */
101 bool
102 auto_load_gdb_scripts_enabled (const struct extension_language_defn *extlang)
104 return auto_load_gdb_scripts;
107 /* Internal-use flag to enable/disable auto-loading.
108 This is true if we should auto-load python code when an objfile is opened,
109 false otherwise.
111 Both auto_load_scripts && global_auto_load must be true to enable
112 auto-loading.
114 This flag exists to facilitate deferring auto-loading during start-up
115 until after ./.gdbinit has been read; it may augment the search directories
116 used to find the scripts. */
117 bool global_auto_load = true;
119 /* Auto-load .gdbinit file from the current directory? */
120 bool auto_load_local_gdbinit = true;
122 /* Absolute pathname to the current directory .gdbinit, if it exists. */
123 char *auto_load_local_gdbinit_pathname = NULL;
125 /* if AUTO_LOAD_LOCAL_GDBINIT_PATHNAME has been loaded. */
126 bool auto_load_local_gdbinit_loaded = false;
128 /* "show" command for the auto_load_local_gdbinit configuration variable. */
130 static void
131 show_auto_load_local_gdbinit (struct ui_file *file, int from_tty,
132 struct cmd_list_element *c, const char *value)
134 gdb_printf (file, _("Auto-loading of .gdbinit script from current "
135 "directory is %s.\n"),
136 value);
139 /* Directory list from which to load auto-loaded scripts. It is not checked
140 for absolute paths but they are strongly recommended. It is initialized by
141 _initialize_auto_load. */
142 static std::string auto_load_dir = AUTO_LOAD_DIR;
144 /* "set" command for the auto_load_dir configuration variable. */
146 static void
147 set_auto_load_dir (const char *args, int from_tty, struct cmd_list_element *c)
149 /* Setting the variable to "" resets it to the compile time defaults. */
150 if (auto_load_dir.empty ())
151 auto_load_dir = AUTO_LOAD_DIR;
154 /* "show" command for the auto_load_dir configuration variable. */
156 static void
157 show_auto_load_dir (struct ui_file *file, int from_tty,
158 struct cmd_list_element *c, const char *value)
160 gdb_printf (file, _("List of directories from which to load "
161 "auto-loaded scripts is %s.\n"),
162 value);
165 /* Directory list safe to hold auto-loaded files. It is not checked for
166 absolute paths but they are strongly recommended. It is initialized by
167 _initialize_auto_load. */
168 static std::string auto_load_safe_path = AUTO_LOAD_SAFE_PATH;
170 /* Vector of directory elements of AUTO_LOAD_SAFE_PATH with each one normalized
171 by tilde_expand and possibly each entries has added its gdb_realpath
172 counterpart. */
173 static std::vector<gdb::unique_xmalloc_ptr<char>> auto_load_safe_path_vec;
175 /* Expand $datadir and $debugdir in STRING according to the rules of
176 substitute_path_component. */
178 static std::vector<gdb::unique_xmalloc_ptr<char>>
179 auto_load_expand_dir_vars (const char *string)
181 char *s = xstrdup (string);
182 substitute_path_component (&s, "$datadir", gdb_datadir.c_str ());
183 substitute_path_component (&s, "$debugdir", debug_file_directory.c_str ());
185 if (debug_auto_load && strcmp (s, string) != 0)
186 auto_load_debug_printf ("Expanded $-variables to \"%s\".", s);
188 std::vector<gdb::unique_xmalloc_ptr<char>> dir_vec
189 = dirnames_to_char_ptr_vec (s);
190 xfree(s);
192 return dir_vec;
195 /* Update auto_load_safe_path_vec from current AUTO_LOAD_SAFE_PATH. */
197 static void
198 auto_load_safe_path_vec_update (void)
200 auto_load_debug_printf ("Updating directories of \"%s\".",
201 auto_load_safe_path.c_str ());
203 auto_load_safe_path_vec
204 = auto_load_expand_dir_vars (auto_load_safe_path.c_str ());
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.empty ())
257 auto_load_safe_path = AUTO_LOAD_SAFE_PATH;
259 auto_load_safe_path_vec_update ();
262 /* "show" command for the auto_load_safe_path configuration variable. */
264 static void
265 show_auto_load_safe_path (struct ui_file *file, int from_tty,
266 struct cmd_list_element *c, const char *value)
268 const char *cs;
270 /* Check if user has entered either "/" or for example ":".
271 But while more complicate content like ":/foo" would still also
272 permit any location do not hide those. */
274 for (cs = value; *cs && (*cs == DIRNAME_SEPARATOR || IS_DIR_SEPARATOR (*cs));
275 cs++);
276 if (*cs == 0)
277 gdb_printf (file, _("Auto-load files are safe to load from any "
278 "directory.\n"));
279 else
280 gdb_printf (file, _("List of directories from which it is safe to "
281 "auto-load files is %s.\n"),
282 value);
285 /* "add-auto-load-safe-path" command for the auto_load_safe_path configuration
286 variable. */
288 static void
289 add_auto_load_safe_path (const char *args, int from_tty)
291 if (args == NULL || *args == 0)
292 error (_("\
293 Directory argument required.\n\
294 Use 'set auto-load safe-path /' for disabling the auto-load safe-path security.\
295 "));
297 auto_load_safe_path = string_printf ("%s%c%s", auto_load_safe_path.c_str (),
298 DIRNAME_SEPARATOR, args);
300 auto_load_safe_path_vec_update ();
303 /* "add-auto-load-scripts-directory" command for the auto_load_dir configuration
304 variable. */
306 static void
307 add_auto_load_dir (const char *args, int from_tty)
309 if (args == NULL || *args == 0)
310 error (_("Directory argument required."));
312 auto_load_dir = string_printf ("%s%c%s", auto_load_dir.c_str (),
313 DIRNAME_SEPARATOR, args);
316 /* Implementation for filename_is_in_pattern overwriting the caller's FILENAME
317 and PATTERN. */
319 static int
320 filename_is_in_pattern_1 (char *filename, char *pattern)
322 size_t pattern_len = strlen (pattern);
323 size_t filename_len = strlen (filename);
325 auto_load_debug_printf ("Matching file \"%s\" to pattern \"%s\"",
326 filename, pattern);
328 /* Trim trailing slashes ("/") from PATTERN. Even for "d:\" paths as
329 trailing slashes are trimmed also from FILENAME it still matches
330 correctly. */
331 while (pattern_len && IS_DIR_SEPARATOR (pattern[pattern_len - 1]))
332 pattern_len--;
333 pattern[pattern_len] = '\0';
335 /* Ensure auto_load_safe_path "/" matches any FILENAME. On MS-Windows
336 platform FILENAME even after gdb_realpath does not have to start with
337 IS_DIR_SEPARATOR character, such as the 'C:\x.exe' filename. */
338 if (pattern_len == 0)
340 auto_load_debug_printf ("Matched - empty pattern");
341 return 1;
344 for (;;)
346 /* Trim trailing slashes ("/"). PATTERN also has slashes trimmed the
347 same way so they will match. */
348 while (filename_len && IS_DIR_SEPARATOR (filename[filename_len - 1]))
349 filename_len--;
350 filename[filename_len] = '\0';
351 if (filename_len == 0)
353 auto_load_debug_printf ("Not matched - pattern \"%s\".", pattern);
354 return 0;
357 if (gdb_filename_fnmatch (pattern, filename, FNM_FILE_NAME | FNM_NOESCAPE)
358 == 0)
360 auto_load_debug_printf ("Matched - file \"%s\" to pattern \"%s\".",
361 filename, pattern);
362 return 1;
365 /* Trim trailing FILENAME component. */
366 while (filename_len > 0 && !IS_DIR_SEPARATOR (filename[filename_len - 1]))
367 filename_len--;
371 /* Return 1 if FILENAME matches PATTERN or if FILENAME resides in
372 a subdirectory of a directory that matches PATTERN. Return 0 otherwise.
373 gdb_realpath normalization is never done here. */
375 static ATTRIBUTE_PURE int
376 filename_is_in_pattern (const char *filename, const char *pattern)
378 char *filename_copy, *pattern_copy;
380 filename_copy = (char *) alloca (strlen (filename) + 1);
381 strcpy (filename_copy, filename);
382 pattern_copy = (char *) alloca (strlen (pattern) + 1);
383 strcpy (pattern_copy, pattern);
385 return filename_is_in_pattern_1 (filename_copy, pattern_copy);
388 /* Return 1 if FILENAME belongs to one of directory components of
389 AUTO_LOAD_SAFE_PATH_VEC. Return 0 otherwise.
390 auto_load_safe_path_vec_update is never called.
391 *FILENAME_REALP may be updated by gdb_realpath of FILENAME. */
393 static int
394 filename_is_in_auto_load_safe_path_vec (const char *filename,
395 gdb::unique_xmalloc_ptr<char> *filename_realp)
397 const char *pattern = NULL;
399 for (const gdb::unique_xmalloc_ptr<char> &p : auto_load_safe_path_vec)
400 if (*filename_realp == NULL && filename_is_in_pattern (filename, p.get ()))
402 pattern = p.get ();
403 break;
406 if (pattern == NULL)
408 if (*filename_realp == NULL)
410 *filename_realp = gdb_realpath (filename);
411 if (debug_auto_load && strcmp (filename_realp->get (), filename) != 0)
412 auto_load_debug_printf ("Resolved file \"%s\" as \"%s\".",
413 filename, filename_realp->get ());
416 if (strcmp (filename_realp->get (), filename) != 0)
417 for (const gdb::unique_xmalloc_ptr<char> &p : auto_load_safe_path_vec)
418 if (filename_is_in_pattern (filename_realp->get (), p.get ()))
420 pattern = p.get ();
421 break;
425 if (pattern != NULL)
427 auto_load_debug_printf ("File \"%s\" matches directory \"%s\".",
428 filename, pattern);
429 return 1;
432 return 0;
435 /* See auto-load.h. */
437 bool
438 file_is_auto_load_safe (const char *filename)
440 gdb::unique_xmalloc_ptr<char> filename_real;
441 static bool advice_printed = false;
443 if (filename_is_in_auto_load_safe_path_vec (filename, &filename_real))
444 return true;
446 auto_load_safe_path_vec_update ();
447 if (filename_is_in_auto_load_safe_path_vec (filename, &filename_real))
448 return true;
450 warning (_("File \"%ps\" auto-loading has been declined by your "
451 "`auto-load safe-path' set to \"%s\"."),
452 styled_string (file_name_style.style (), filename_real.get ()),
453 auto_load_safe_path.c_str ());
455 if (!advice_printed)
457 /* Find the existing home directory config file. */
458 struct stat buf;
459 std::string home_config = find_gdb_home_config_file (GDBINIT, &buf);
460 if (home_config.empty ())
462 /* The user doesn't have an existing home directory config file,
463 so we should suggest a suitable path for them to use. */
464 std::string config_dir_file
465 = get_standard_config_filename (GDBINIT);
466 if (!config_dir_file.empty ())
467 home_config = config_dir_file;
468 else
470 const char *homedir = getenv ("HOME");
471 if (homedir == nullptr)
472 homedir = "$HOME";
473 home_config = (std::string (homedir) + SLASH_STRING
474 + std::string (GDBINIT));
478 gdb_printf (_("\
479 To enable execution of this file add\n\
480 \t%p[add-auto-load-safe-path %s%p]\n\
481 line to your configuration file \"%ps\".\n\
482 To completely disable this security protection add\n\
483 \t%ps\n\
484 line to your configuration file \"%ps\".\n\
485 For more information about this security protection see the\n\
486 \"Auto-loading safe path\" section in the GDB manual. E.g., run from the shell:\n\
487 \tinfo \"(gdb)Auto-loading safe path\"\n"),
488 command_style.style ().ptr (),
489 filename_real.get (),
490 nullptr,
491 styled_string (file_name_style.style (),
492 home_config.c_str ()),
493 styled_string (command_style.style (),
494 "set auto-load safe-path /"),
495 styled_string (file_name_style.style (),
496 home_config.c_str ()));
497 advice_printed = true;
500 return false;
503 /* For scripts specified in .debug_gdb_scripts, multiple objfiles may load
504 the same script. There's no point in loading the script multiple times,
505 and there can be a lot of objfiles and scripts, so we keep track of scripts
506 loaded this way. */
508 struct auto_load_pspace_info
510 /* For each program space we keep track of loaded scripts, both when
511 specified as file names and as scripts to be executed directly. */
512 htab_up loaded_script_files;
513 htab_up loaded_script_texts;
515 /* Non-zero if we've issued the warning about an auto-load script not being
516 supported. We only want to issue this warning once. */
517 bool unsupported_script_warning_printed = false;
519 /* Non-zero if we've issued the warning about an auto-load script not being
520 found. We only want to issue this warning once. */
521 bool script_not_found_warning_printed = false;
524 /* Objects of this type are stored in the loaded_script hash table. */
526 struct loaded_script
528 /* Name as provided by the objfile. */
529 const char *name;
531 /* Full path name or NULL if script wasn't found (or was otherwise
532 inaccessible), or NULL for loaded_script_texts. */
533 const char *full_path;
535 /* True if this script has been loaded. */
536 bool loaded;
538 const struct extension_language_defn *language;
541 /* Per-program-space data key. */
542 static const registry<program_space>::key<auto_load_pspace_info>
543 auto_load_pspace_data;
545 /* Get the current autoload data. If none is found yet, add it now. This
546 function always returns a valid object. */
548 static struct auto_load_pspace_info *
549 get_auto_load_pspace_data (struct program_space *pspace)
551 struct auto_load_pspace_info *info;
553 info = auto_load_pspace_data.get (pspace);
554 if (info == NULL)
555 info = auto_load_pspace_data.emplace (pspace);
557 return info;
560 /* Hash function for the loaded script hash. */
562 static hashval_t
563 hash_loaded_script_entry (const void *data)
565 const struct loaded_script *e = (const struct loaded_script *) data;
567 return htab_hash_string (e->name) ^ htab_hash_pointer (e->language);
570 /* Equality function for the loaded script hash. */
572 static int
573 eq_loaded_script_entry (const void *a, const void *b)
575 const struct loaded_script *ea = (const struct loaded_script *) a;
576 const struct loaded_script *eb = (const struct loaded_script *) b;
578 return strcmp (ea->name, eb->name) == 0 && ea->language == eb->language;
581 /* Initialize the table to track loaded scripts.
582 Each entry is hashed by the full path name. */
584 static void
585 init_loaded_scripts_info (struct auto_load_pspace_info *pspace_info)
587 /* Choose 31 as the starting size of the hash table, somewhat arbitrarily.
588 Space for each entry is obtained with one malloc so we can free them
589 easily. */
591 pspace_info->loaded_script_files.reset
592 (htab_create (31,
593 hash_loaded_script_entry,
594 eq_loaded_script_entry,
595 xfree));
596 pspace_info->loaded_script_texts.reset
597 (htab_create (31,
598 hash_loaded_script_entry,
599 eq_loaded_script_entry,
600 xfree));
602 pspace_info->unsupported_script_warning_printed = false;
603 pspace_info->script_not_found_warning_printed = false;
606 /* Wrapper on get_auto_load_pspace_data to also allocate the hash table
607 for loading scripts. */
609 struct auto_load_pspace_info *
610 get_auto_load_pspace_data_for_loading (struct program_space *pspace)
612 struct auto_load_pspace_info *info;
614 info = get_auto_load_pspace_data (pspace);
615 if (info->loaded_script_files == NULL)
616 init_loaded_scripts_info (info);
618 return info;
621 /* Add script file NAME in LANGUAGE to hash table of PSPACE_INFO.
622 LOADED is true if the script has been (is going to) be loaded, false
623 otherwise (such as if it has not been found).
624 FULL_PATH is NULL if the script wasn't found.
626 The result is true if the script was already in the hash table. */
628 static bool
629 maybe_add_script_file (struct auto_load_pspace_info *pspace_info, bool loaded,
630 const char *name, const char *full_path,
631 const struct extension_language_defn *language)
633 struct htab *htab = pspace_info->loaded_script_files.get ();
634 struct loaded_script **slot, entry;
636 entry.name = name;
637 entry.language = language;
638 slot = (struct loaded_script **) htab_find_slot (htab, &entry, INSERT);
639 bool in_hash_table = *slot != NULL;
641 /* If this script is not in the hash table, add it. */
643 if (!in_hash_table)
645 char *p;
647 /* Allocate all space in one chunk so it's easier to free. */
648 *slot = ((struct loaded_script *)
649 xmalloc (sizeof (**slot)
650 + strlen (name) + 1
651 + (full_path != NULL ? (strlen (full_path) + 1) : 0)));
652 p = ((char*) *slot) + sizeof (**slot);
653 strcpy (p, name);
654 (*slot)->name = p;
655 if (full_path != NULL)
657 p += strlen (p) + 1;
658 strcpy (p, full_path);
659 (*slot)->full_path = p;
661 else
662 (*slot)->full_path = NULL;
663 (*slot)->loaded = loaded;
664 (*slot)->language = language;
667 return in_hash_table;
670 /* Add script contents NAME in LANGUAGE to hash table of PSPACE_INFO.
671 LOADED is true if the script has been (is going to) be loaded, false
672 otherwise (such as if it has not been found).
674 The result is true if the script was already in the hash table. */
676 static bool
677 maybe_add_script_text (struct auto_load_pspace_info *pspace_info,
678 bool loaded, const char *name,
679 const struct extension_language_defn *language)
681 struct htab *htab = pspace_info->loaded_script_texts.get ();
682 struct loaded_script **slot, entry;
684 entry.name = name;
685 entry.language = language;
686 slot = (struct loaded_script **) htab_find_slot (htab, &entry, INSERT);
687 bool in_hash_table = *slot != NULL;
689 /* If this script is not in the hash table, add it. */
691 if (!in_hash_table)
693 char *p;
695 /* Allocate all space in one chunk so it's easier to free. */
696 *slot = ((struct loaded_script *)
697 xmalloc (sizeof (**slot) + strlen (name) + 1));
698 p = ((char*) *slot) + sizeof (**slot);
699 strcpy (p, name);
700 (*slot)->name = p;
701 (*slot)->full_path = NULL;
702 (*slot)->loaded = loaded;
703 (*slot)->language = language;
706 return in_hash_table;
709 /* Clear the table of loaded section scripts. */
711 static void
712 clear_section_scripts (program_space *pspace)
714 auto_load_pspace_info *info = auto_load_pspace_data.get (pspace);
715 if (info != NULL && info->loaded_script_files != NULL)
716 auto_load_pspace_data.clear (pspace);
719 /* Look for the auto-load script in LANGUAGE associated with OBJFILE where
720 OBJFILE's gdb_realpath is REALNAME and load it. Return 1 if we found any
721 matching script, return 0 otherwise. */
723 static int
724 auto_load_objfile_script_1 (struct objfile *objfile, const char *realname,
725 const struct extension_language_defn *language)
727 const char *debugfile;
728 int retval;
729 const char *suffix = ext_lang_auto_load_suffix (language);
731 std::string filename = std::string (realname) + suffix;
733 gdb_file_up input = gdb_fopen_cloexec (filename.c_str (), "r");
734 debugfile = filename.c_str ();
736 auto_load_debug_printf ("Attempted file \"%ps\" %s.",
737 styled_string (file_name_style.style (), debugfile),
738 input != nullptr ? "exists" : "does not exist");
740 std::string debugfile_holder;
741 if (!input)
743 /* Also try the same file in a subdirectory of gdb's data
744 directory. */
746 std::vector<gdb::unique_xmalloc_ptr<char>> vec
747 = auto_load_expand_dir_vars (auto_load_dir.c_str ());
749 auto_load_debug_printf
750 ("Searching 'set auto-load scripts-directory' path \"%s\".",
751 auto_load_dir.c_str ());
753 /* Convert Windows file name from c:/dir/file to /c/dir/file. */
754 if (HAS_DRIVE_SPEC (debugfile))
755 filename = (std::string("\\") + debugfile[0]
756 + STRIP_DRIVE_SPEC (debugfile));
758 for (const gdb::unique_xmalloc_ptr<char> &dir : vec)
760 /* FILENAME is absolute, so we don't need a "/" here. */
761 debugfile_holder = dir.get () + filename;
762 debugfile = debugfile_holder.c_str ();
764 input = gdb_fopen_cloexec (debugfile, "r");
766 auto_load_debug_printf ("Attempted file \"%ps\" %s.",
767 styled_string (file_name_style.style (),
768 debugfile),
769 (input != nullptr
770 ? "exists"
771 : "does not exist"));
773 if (input != NULL)
774 break;
778 if (input)
780 struct auto_load_pspace_info *pspace_info;
782 auto_load_debug_printf
783 ("Loading %s script \"%s\" by extension for objfile \"%s\".",
784 ext_lang_name (language), debugfile, objfile_name (objfile));
786 bool is_safe = file_is_auto_load_safe (debugfile);
788 /* Add this script to the hash table too so
789 "info auto-load ${lang}-scripts" can print it. */
790 pspace_info
791 = get_auto_load_pspace_data_for_loading (objfile->pspace ());
792 maybe_add_script_file (pspace_info, is_safe, debugfile, debugfile,
793 language);
795 /* To preserve existing behavior we don't check for whether the
796 script was already in the table, and always load it.
797 It's highly unlikely that we'd ever load it twice,
798 and these scripts are required to be idempotent under multiple
799 loads anyway. */
800 if (is_safe)
802 objfile_script_sourcer_func *sourcer
803 = ext_lang_objfile_script_sourcer (language);
805 /* We shouldn't get here if support for the language isn't
806 compiled in. And the extension language is required to implement
807 this function. */
808 gdb_assert (sourcer != NULL);
809 sourcer (language, objfile, input.get (), debugfile);
812 retval = 1;
814 else
815 retval = 0;
817 return retval;
820 /* Look for the auto-load script in LANGUAGE associated with OBJFILE and load
821 it. */
823 void
824 auto_load_objfile_script (struct objfile *objfile,
825 const struct extension_language_defn *language)
827 gdb::unique_xmalloc_ptr<char> realname
828 = gdb_realpath (objfile_name (objfile));
830 if (auto_load_objfile_script_1 (objfile, realname.get (), language))
831 return;
833 /* For Windows/DOS .exe executables, strip the .exe suffix, so that
834 FOO-gdb.gdb could be used for FOO.exe, and try again. */
836 size_t len = strlen (realname.get ());
837 const size_t lexe = sizeof (".exe") - 1;
839 if (len > lexe && strcasecmp (realname.get () + len - lexe, ".exe") == 0)
841 len -= lexe;
842 realname.get ()[len] = '\0';
844 auto_load_debug_printf
845 ("Stripped .exe suffix, retrying with \"%s\".", realname.get ());
847 auto_load_objfile_script_1 (objfile, realname.get (), language);
848 return;
851 /* If OBJFILE is a separate debug file and its name does not match
852 the name given in the parent's .gnu_debuglink section, try to
853 find the auto-load script using the parent's path and the
854 debuglink name. */
856 struct objfile *parent = objfile->separate_debug_objfile_backlink;
857 if (parent != nullptr)
859 uint32_t crc32;
860 gdb::unique_xmalloc_ptr<char> debuglink
861 (bfd_get_debug_link_info (parent->obfd.get (), &crc32));
863 if (debuglink.get () != nullptr
864 && strcmp (debuglink.get (), lbasename (realname.get ())) != 0)
866 /* Replace the last component of the parent's path with the
867 debuglink name. */
869 std::string p_realname = gdb_realpath (objfile_name (parent)).get ();
870 size_t last = p_realname.find_last_of ('/');
872 if (last != std::string::npos)
874 p_realname.replace (last + 1, std::string::npos,
875 debuglink.get ());
877 auto_load_debug_printf
878 ("Debug filename mismatch, retrying with \"%s\".",
879 p_realname.c_str ());
881 auto_load_objfile_script_1 (objfile,
882 p_realname.c_str (), language);
888 /* Subroutine of source_section_scripts to simplify it.
889 Load FILE as a script in extension language LANGUAGE.
890 The script is from section SECTION_NAME in OBJFILE at offset OFFSET. */
892 static void
893 source_script_file (struct auto_load_pspace_info *pspace_info,
894 struct objfile *objfile,
895 const struct extension_language_defn *language,
896 const char *section_name, unsigned int offset,
897 const char *file)
899 objfile_script_sourcer_func *sourcer;
901 /* Skip this script if support is not compiled in. */
902 sourcer = ext_lang_objfile_script_sourcer (language);
903 if (sourcer == NULL)
905 /* We don't throw an error, the program is still debuggable. */
906 maybe_print_unsupported_script_warning (pspace_info, objfile, language,
907 section_name, offset);
908 /* We *could* still try to open it, but there's no point. */
909 maybe_add_script_file (pspace_info, 0, file, NULL, language);
910 return;
913 /* Skip this script if auto-loading it has been disabled. */
914 if (!ext_lang_auto_load_enabled (language))
916 /* No message is printed, just skip it. */
917 return;
920 std::optional<open_script> opened = find_and_open_script (file,
921 1 /*search_path*/);
923 if (opened)
925 auto_load_debug_printf
926 ("Loading %s script \"%s\" from section \"%s\" of objfile \"%s\".",
927 ext_lang_name (language), opened->full_path.get (),
928 section_name, objfile_name (objfile));
930 if (!file_is_auto_load_safe (opened->full_path.get ()))
931 opened.reset ();
933 else
935 /* If one script isn't found it's not uncommon for more to not be
936 found either. We don't want to print a message for each script,
937 too much noise. Instead, we print the warning once and tell the
938 user how to find the list of scripts that weren't loaded.
939 We don't throw an error, the program is still debuggable.
941 IWBN if complaints.c were more general-purpose. */
943 maybe_print_script_not_found_warning (pspace_info, objfile, language,
944 section_name, offset);
947 bool in_hash_table
948 = maybe_add_script_file (pspace_info, bool (opened), file,
949 (opened ? opened->full_path.get (): NULL),
950 language);
952 /* If this file is not currently loaded, load it. */
953 if (opened && !in_hash_table)
954 sourcer (language, objfile, opened->stream.get (),
955 opened->full_path.get ());
958 /* Subroutine of source_section_scripts to simplify it.
959 Execute SCRIPT as a script in extension language LANG.
960 The script is from section SECTION_NAME in OBJFILE at offset OFFSET. */
962 static void
963 execute_script_contents (struct auto_load_pspace_info *pspace_info,
964 struct objfile *objfile,
965 const struct extension_language_defn *language,
966 const char *section_name, unsigned int offset,
967 const char *script)
969 objfile_script_executor_func *executor;
970 const char *newline, *script_text;
971 const char *name;
973 /* The first line of the script is the name of the script.
974 It must not contain any kind of space character. */
975 name = NULL;
976 newline = strchr (script, '\n');
977 std::string name_holder;
978 if (newline != NULL)
980 const char *buf, *p;
982 /* Put the name in a buffer and validate it. */
983 name_holder = std::string (script, newline - script);
984 buf = name_holder.c_str ();
985 for (p = buf; *p != '\0'; ++p)
987 if (isspace (*p))
988 break;
990 /* We don't allow nameless scripts, they're not helpful to the user. */
991 if (p != buf && *p == '\0')
992 name = buf;
994 if (name == NULL)
996 /* We don't throw an error, the program is still debuggable. */
997 warning (_("\
998 Missing/bad script name in entry at offset %u in section %s\n\
999 of file %ps."),
1000 offset, section_name,
1001 styled_string (file_name_style.style (),
1002 objfile_name (objfile)));
1003 return;
1005 script_text = newline + 1;
1007 /* Skip this script if support is not compiled in. */
1008 executor = ext_lang_objfile_script_executor (language);
1009 if (executor == NULL)
1011 /* We don't throw an error, the program is still debuggable. */
1012 maybe_print_unsupported_script_warning (pspace_info, objfile, language,
1013 section_name, offset);
1014 maybe_add_script_text (pspace_info, 0, name, language);
1015 return;
1018 /* Skip this script if auto-loading it has been disabled. */
1019 if (!ext_lang_auto_load_enabled (language))
1021 /* No message is printed, just skip it. */
1022 return;
1025 auto_load_debug_printf
1026 ("Loading %s script \"%s\" from section \"%s\" of objfile \"%s\".",
1027 ext_lang_name (language), name, section_name, objfile_name (objfile));
1029 bool is_safe = file_is_auto_load_safe (objfile_name (objfile));
1031 bool in_hash_table
1032 = maybe_add_script_text (pspace_info, is_safe, name, language);
1034 /* If this file is not currently loaded, load it. */
1035 if (is_safe && !in_hash_table)
1036 executor (language, objfile, name, script_text);
1039 /* Load scripts specified in OBJFILE.
1040 START,END delimit a buffer containing a list of nul-terminated
1041 file names.
1042 SECTION_NAME is used in error messages.
1044 Scripts specified as file names are found per normal "source -s" command
1045 processing. First the script is looked for in $cwd. If not found there
1046 the source search path is used.
1048 The section contains a list of path names of script files to load or
1049 actual script contents. Each entry is nul-terminated. */
1051 static void
1052 source_section_scripts (struct objfile *objfile, const char *section_name,
1053 const char *start, const char *end)
1055 auto_load_pspace_info *pspace_info
1056 = get_auto_load_pspace_data_for_loading (objfile->pspace ());
1058 for (const char *p = start; p < end; ++p)
1060 const char *entry;
1061 const struct extension_language_defn *language;
1062 unsigned int offset = p - start;
1063 int code = *p;
1065 switch (code)
1067 case SECTION_SCRIPT_ID_PYTHON_FILE:
1068 case SECTION_SCRIPT_ID_PYTHON_TEXT:
1069 language = get_ext_lang_defn (EXT_LANG_PYTHON);
1070 break;
1071 case SECTION_SCRIPT_ID_SCHEME_FILE:
1072 case SECTION_SCRIPT_ID_SCHEME_TEXT:
1073 language = get_ext_lang_defn (EXT_LANG_GUILE);
1074 break;
1075 default:
1076 warning (_("Invalid entry in %s section"), section_name);
1077 /* We could try various heuristics to find the next valid entry,
1078 but it's safer to just punt. */
1079 return;
1081 entry = ++p;
1083 while (p < end && *p != '\0')
1084 ++p;
1085 if (p == end)
1087 warning (_("Non-nul-terminated entry in %s at offset %u"),
1088 section_name, offset);
1089 /* Don't load/execute it. */
1090 break;
1093 switch (code)
1095 case SECTION_SCRIPT_ID_PYTHON_FILE:
1096 case SECTION_SCRIPT_ID_SCHEME_FILE:
1097 if (p == entry)
1099 warning (_("Empty entry in %s at offset %u"),
1100 section_name, offset);
1101 continue;
1103 source_script_file (pspace_info, objfile, language,
1104 section_name, offset, entry);
1105 break;
1106 case SECTION_SCRIPT_ID_PYTHON_TEXT:
1107 case SECTION_SCRIPT_ID_SCHEME_TEXT:
1108 execute_script_contents (pspace_info, objfile, language,
1109 section_name, offset, entry);
1110 break;
1115 /* Load scripts specified in section SECTION_NAME of OBJFILE. */
1117 static void
1118 auto_load_section_scripts (struct objfile *objfile, const char *section_name)
1120 bfd *abfd = objfile->obfd.get ();
1121 asection *scripts_sect;
1123 scripts_sect = bfd_get_section_by_name (abfd, section_name);
1124 if (scripts_sect == NULL
1125 || (bfd_section_flags (scripts_sect) & SEC_HAS_CONTENTS) == 0)
1126 return;
1128 gdb::byte_vector data;
1129 if (!gdb_bfd_get_full_section_contents (abfd, scripts_sect, &data))
1130 warning (_("Couldn't read %s section of %ps"),
1131 section_name,
1132 styled_string (file_name_style.style (),
1133 bfd_get_filename (abfd)));
1134 else
1136 const char *p = (const char *) data.data ();
1137 source_section_scripts (objfile, section_name, p, p + data.size ());
1141 /* Load any auto-loaded scripts for OBJFILE.
1143 Two flavors of auto-loaded scripts are supported.
1144 1) based on the path to the objfile
1145 2) from .debug_gdb_scripts section */
1147 void
1148 load_auto_scripts_for_objfile (struct objfile *objfile)
1150 /* Return immediately if auto-loading has been globally disabled.
1151 This is to handle sequencing of operations during gdb startup.
1152 Also return immediately if OBJFILE was not created from a file
1153 on the local filesystem. */
1154 if (!global_auto_load
1155 || (objfile->flags & OBJF_NOT_FILENAME) != 0
1156 || is_target_filename (objfile->original_name))
1157 return;
1159 /* Load any extension language scripts for this objfile.
1160 E.g., foo-gdb.gdb, foo-gdb.py. */
1161 auto_load_ext_lang_scripts_for_objfile (objfile);
1163 /* Load any scripts mentioned in AUTO_SECTION_NAME (.debug_gdb_scripts). */
1164 auto_load_section_scripts (objfile, AUTO_SECTION_NAME);
1167 /* Collect scripts to be printed in a vec. */
1169 struct collect_matching_scripts_data
1171 collect_matching_scripts_data (std::vector<loaded_script *> *scripts_p_,
1172 const extension_language_defn *language_)
1173 : scripts_p (scripts_p_), language (language_)
1176 std::vector<loaded_script *> *scripts_p;
1177 const struct extension_language_defn *language;
1180 /* Traversal function for htab_traverse.
1181 Collect the entry if it matches the regexp. */
1183 static int
1184 collect_matching_scripts (void **slot, void *info)
1186 struct loaded_script *script = (struct loaded_script *) *slot;
1187 struct collect_matching_scripts_data *data
1188 = (struct collect_matching_scripts_data *) info;
1190 if (script->language == data->language && re_exec (script->name))
1191 data->scripts_p->push_back (script);
1193 return 1;
1196 /* Print SCRIPT. */
1198 static void
1199 print_script (struct loaded_script *script)
1201 struct ui_out *uiout = current_uiout;
1203 ui_out_emit_tuple tuple_emitter (uiout, NULL);
1205 uiout->field_string ("loaded", script->loaded ? "Yes" : "No");
1206 uiout->field_string ("script", script->name);
1207 uiout->text ("\n");
1209 /* If the name isn't the full path, print it too. */
1210 if (script->full_path != NULL
1211 && strcmp (script->name, script->full_path) != 0)
1213 uiout->text ("\tfull name: ");
1214 uiout->field_string ("full_path", script->full_path);
1215 uiout->text ("\n");
1219 /* Helper for info_auto_load_scripts to sort the scripts by name. */
1221 static bool
1222 sort_scripts_by_name (loaded_script *a, loaded_script *b)
1224 return FILENAME_CMP (a->name, b->name) < 0;
1227 /* Special internal GDB value of auto_load_info_scripts's PATTERN identify
1228 the "info auto-load XXX" command has been executed through the general
1229 "info auto-load" invocation. Extra newline will be printed if needed. */
1230 char auto_load_info_scripts_pattern_nl[] = "";
1232 /* Subroutine of auto_load_info_scripts to simplify it.
1233 Print SCRIPTS. */
1235 static void
1236 print_scripts (const std::vector<loaded_script *> &scripts)
1238 for (loaded_script *script : scripts)
1239 print_script (script);
1242 /* Implementation for "info auto-load gdb-scripts"
1243 (and "info auto-load python-scripts"). List scripts in LANGUAGE matching
1244 PATTERN. FROM_TTY is the usual GDB boolean for user interactivity. */
1246 void
1247 auto_load_info_scripts (program_space *pspace, const char *pattern,
1248 int from_tty, const extension_language_defn *language)
1250 struct ui_out *uiout = current_uiout;
1252 dont_repeat ();
1254 auto_load_pspace_info *pspace_info = get_auto_load_pspace_data (pspace);
1256 if (pattern && *pattern)
1258 char *re_err = re_comp (pattern);
1260 if (re_err)
1261 error (_("Invalid regexp: %s"), re_err);
1263 else
1265 re_comp ("");
1268 /* We need to know the number of rows before we build the table.
1269 Plus we want to sort the scripts by name.
1270 So first traverse the hash table collecting the matching scripts. */
1272 std::vector<loaded_script *> script_files, script_texts;
1274 if (pspace_info != NULL && pspace_info->loaded_script_files != NULL)
1276 collect_matching_scripts_data data (&script_files, language);
1278 /* Pass a pointer to scripts as VEC_safe_push can realloc space. */
1279 htab_traverse_noresize (pspace_info->loaded_script_files.get (),
1280 collect_matching_scripts, &data);
1282 std::sort (script_files.begin (), script_files.end (),
1283 sort_scripts_by_name);
1286 if (pspace_info != NULL && pspace_info->loaded_script_texts != NULL)
1288 collect_matching_scripts_data data (&script_texts, language);
1290 /* Pass a pointer to scripts as VEC_safe_push can realloc space. */
1291 htab_traverse_noresize (pspace_info->loaded_script_texts.get (),
1292 collect_matching_scripts, &data);
1294 std::sort (script_texts.begin (), script_texts.end (),
1295 sort_scripts_by_name);
1298 int nr_scripts = script_files.size () + script_texts.size ();
1300 /* Table header shifted right by preceding "gdb-scripts: " would not match
1301 its columns. */
1302 if (nr_scripts > 0 && pattern == auto_load_info_scripts_pattern_nl)
1303 uiout->text ("\n");
1306 ui_out_emit_table table_emitter (uiout, 2, nr_scripts,
1307 "AutoLoadedScriptsTable");
1309 uiout->table_header (7, ui_left, "loaded", "Loaded");
1310 uiout->table_header (70, ui_left, "script", "Script");
1311 uiout->table_body ();
1313 print_scripts (script_files);
1314 print_scripts (script_texts);
1317 if (nr_scripts == 0)
1319 if (pattern && *pattern)
1320 uiout->message ("No auto-load scripts matching %s.\n", pattern);
1321 else
1322 uiout->message ("No auto-load scripts.\n");
1326 /* Wrapper for "info auto-load gdb-scripts". */
1328 static void
1329 info_auto_load_gdb_scripts (const char *pattern, int from_tty)
1331 auto_load_info_scripts (current_program_space, pattern, from_tty,
1332 &extension_language_gdb);
1335 /* Implement 'info auto-load local-gdbinit'. */
1337 static void
1338 info_auto_load_local_gdbinit (const char *args, int from_tty)
1340 if (auto_load_local_gdbinit_pathname == NULL)
1341 gdb_printf (_("Local .gdbinit file was not found.\n"));
1342 else if (auto_load_local_gdbinit_loaded)
1343 gdb_printf (_("Local .gdbinit file \"%ps\" has been loaded.\n"),
1344 styled_string (file_name_style.style (),
1345 auto_load_local_gdbinit_pathname));
1346 else
1347 gdb_printf (_("Local .gdbinit file \"%ps\" has not been loaded.\n"),
1348 styled_string (file_name_style.style (),
1349 auto_load_local_gdbinit_pathname));
1352 /* Print an "unsupported script" warning if it has not already been printed.
1353 The script is in language LANGUAGE at offset OFFSET in section SECTION_NAME
1354 of OBJFILE. */
1356 static void
1357 maybe_print_unsupported_script_warning
1358 (struct auto_load_pspace_info *pspace_info,
1359 struct objfile *objfile, const struct extension_language_defn *language,
1360 const char *section_name, unsigned offset)
1362 if (!pspace_info->unsupported_script_warning_printed)
1364 warning (_("\
1365 Unsupported auto-load script at offset %u in section %s\n\
1366 of file %ps.\n\
1367 Use `info auto-load %s-scripts [REGEXP]' to list them."),
1368 offset, section_name,
1369 styled_string (file_name_style.style (),
1370 objfile_name (objfile)),
1371 ext_lang_name (language));
1372 pspace_info->unsupported_script_warning_printed = true;
1376 /* Return non-zero if SCRIPT_NOT_FOUND_WARNING_PRINTED of PSPACE_INFO was unset
1377 before calling this function. Always set SCRIPT_NOT_FOUND_WARNING_PRINTED
1378 of PSPACE_INFO. */
1380 static void
1381 maybe_print_script_not_found_warning
1382 (struct auto_load_pspace_info *pspace_info,
1383 struct objfile *objfile, const struct extension_language_defn *language,
1384 const char *section_name, unsigned offset)
1386 if (!pspace_info->script_not_found_warning_printed)
1388 warning (_("\
1389 Missing auto-load script at offset %u in section %s\n\
1390 of file %ps.\n\
1391 Use `info auto-load %s-scripts [REGEXP]' to list them."),
1392 offset, section_name,
1393 styled_string (file_name_style.style (),
1394 objfile_name (objfile)),
1395 ext_lang_name (language));
1396 pspace_info->script_not_found_warning_printed = true;
1400 /* The only valid "set auto-load" argument is off|0|no|disable. */
1402 static void
1403 set_auto_load_cmd (const char *args, int from_tty)
1405 struct cmd_list_element *list;
1406 size_t length;
1408 /* See parse_binary_operation in use by the sub-commands. */
1410 length = args ? strlen (args) : 0;
1412 while (length > 0 && (args[length - 1] == ' ' || args[length - 1] == '\t'))
1413 length--;
1415 if (length == 0 || (strncmp (args, "off", length) != 0
1416 && strncmp (args, "0", length) != 0
1417 && strncmp (args, "no", length) != 0
1418 && strncmp (args, "disable", length) != 0))
1419 error (_("Valid is only global 'set auto-load no'; "
1420 "otherwise check the auto-load sub-commands."));
1422 for (list = *auto_load_set_cmdlist_get (); list != NULL; list = list->next)
1423 if (list->var->type () == var_boolean)
1425 gdb_assert (list->type == set_cmd);
1426 do_set_command (args, from_tty, list);
1430 /* Initialize "set auto-load " commands prefix and return it. */
1432 struct cmd_list_element **
1433 auto_load_set_cmdlist_get (void)
1435 static struct cmd_list_element *retval;
1437 if (retval == NULL)
1438 add_prefix_cmd ("auto-load", class_maintenance, set_auto_load_cmd, _("\
1439 Auto-loading specific settings.\n\
1440 Configure various auto-load-specific variables such as\n\
1441 automatic loading of Python scripts."),
1442 &retval, 1/*allow-unknown*/, &setlist);
1444 return &retval;
1447 /* Initialize "show auto-load " commands prefix and return it. */
1449 struct cmd_list_element **
1450 auto_load_show_cmdlist_get (void)
1452 static struct cmd_list_element *retval;
1454 if (retval == NULL)
1455 add_show_prefix_cmd ("auto-load", class_maintenance, _("\
1456 Show auto-loading specific settings.\n\
1457 Show configuration of various auto-load-specific variables such as\n\
1458 automatic loading of Python scripts."),
1459 &retval, 0/*allow-unknown*/, &showlist);
1461 return &retval;
1464 /* Command "info auto-load" displays whether the various auto-load files have
1465 been loaded. This is reimplementation of cmd_show_list which inserts
1466 newlines at proper places. */
1468 static void
1469 info_auto_load_cmd (const char *args, int from_tty)
1471 struct cmd_list_element *list;
1472 struct ui_out *uiout = current_uiout;
1474 ui_out_emit_tuple tuple_emitter (uiout, "infolist");
1476 for (list = *auto_load_info_cmdlist_get (); list != NULL; list = list->next)
1478 ui_out_emit_tuple option_emitter (uiout, "option");
1480 gdb_assert (!list->is_prefix ());
1481 gdb_assert (list->type == not_set_cmd);
1483 uiout->field_string ("name", list->name);
1484 uiout->text (": ");
1485 cmd_func (list, auto_load_info_scripts_pattern_nl, from_tty);
1489 /* Initialize "info auto-load " commands prefix and return it. */
1491 struct cmd_list_element **
1492 auto_load_info_cmdlist_get (void)
1494 static struct cmd_list_element *retval;
1496 if (retval == NULL)
1497 add_prefix_cmd ("auto-load", class_info, info_auto_load_cmd, _("\
1498 Print current status of auto-loaded files.\n\
1499 Print whether various files like Python scripts or .gdbinit files have been\n\
1500 found and/or loaded."),
1501 &retval, 0/*allow-unknown*/, &infolist);
1503 return &retval;
1506 /* See auto-load.h. */
1508 gdb::observers::token auto_load_new_objfile_observer_token;
1510 void _initialize_auto_load ();
1511 void
1512 _initialize_auto_load ()
1514 struct cmd_list_element *cmd;
1515 gdb::unique_xmalloc_ptr<char> scripts_directory_help, gdb_name_help,
1516 python_name_help, guile_name_help;
1517 const char *suffix;
1519 gdb::observers::new_objfile.attach (load_auto_scripts_for_objfile,
1520 auto_load_new_objfile_observer_token,
1521 "auto-load");
1522 gdb::observers::all_objfiles_removed.attach (clear_section_scripts,
1523 "auto-load");
1524 add_setshow_boolean_cmd ("gdb-scripts", class_support,
1525 &auto_load_gdb_scripts, _("\
1526 Enable or disable auto-loading of canned sequences of commands scripts."), _("\
1527 Show whether auto-loading of canned sequences of commands scripts is enabled."),
1528 _("\
1529 If enabled, canned sequences of commands are loaded when the debugger reads\n\
1530 an executable or shared library.\n\
1531 This option has security implications for untrusted inferiors."),
1532 NULL, show_auto_load_gdb_scripts,
1533 auto_load_set_cmdlist_get (),
1534 auto_load_show_cmdlist_get ());
1536 add_cmd ("gdb-scripts", class_info, info_auto_load_gdb_scripts,
1537 _("Print the list of automatically loaded sequences of commands.\n\
1538 Usage: info auto-load gdb-scripts [REGEXP]"),
1539 auto_load_info_cmdlist_get ());
1541 add_setshow_boolean_cmd ("local-gdbinit", class_support,
1542 &auto_load_local_gdbinit, _("\
1543 Enable or disable auto-loading of .gdbinit script in current directory."), _("\
1544 Show whether auto-loading .gdbinit script in current directory is enabled."),
1545 _("\
1546 If enabled, canned sequences of commands are loaded when debugger starts\n\
1547 from .gdbinit file in current directory. Such files are deprecated,\n\
1548 use a script associated with inferior executable file instead.\n\
1549 This option has security implications for untrusted inferiors."),
1550 NULL, show_auto_load_local_gdbinit,
1551 auto_load_set_cmdlist_get (),
1552 auto_load_show_cmdlist_get ());
1554 add_cmd ("local-gdbinit", class_info, info_auto_load_local_gdbinit,
1555 _("Print whether current directory .gdbinit file has been loaded.\n\
1556 Usage: info auto-load local-gdbinit"),
1557 auto_load_info_cmdlist_get ());
1559 suffix = ext_lang_auto_load_suffix (get_ext_lang_defn (EXT_LANG_GDB));
1560 gdb_name_help
1561 = xstrprintf (_("\
1562 GDB scripts: OBJFILE%s\n"),
1563 suffix);
1564 python_name_help = NULL;
1565 #ifdef HAVE_PYTHON
1566 suffix = ext_lang_auto_load_suffix (get_ext_lang_defn (EXT_LANG_PYTHON));
1567 python_name_help
1568 = xstrprintf (_("\
1569 Python scripts: OBJFILE%s\n"),
1570 suffix);
1571 #endif
1572 guile_name_help = NULL;
1573 #ifdef HAVE_GUILE
1574 suffix = ext_lang_auto_load_suffix (get_ext_lang_defn (EXT_LANG_GUILE));
1575 guile_name_help
1576 = xstrprintf (_("\
1577 Guile scripts: OBJFILE%s\n"),
1578 suffix);
1579 #endif
1580 scripts_directory_help
1581 = xstrprintf (_("\
1582 Automatically loaded scripts are located in one of the directories listed\n\
1583 by this option.\n\
1585 Script names:\n\
1586 %s%s%s\
1588 This option is ignored for the kinds of scripts \
1589 having 'set auto-load ... off'.\n\
1590 Directories listed here need to be present also \
1591 in the 'set auto-load safe-path'\n\
1592 option."),
1593 gdb_name_help.get (),
1594 python_name_help.get () ? python_name_help.get () : "",
1595 guile_name_help.get () ? guile_name_help.get () : "");
1597 add_setshow_optional_filename_cmd ("scripts-directory", class_support,
1598 &auto_load_dir, _("\
1599 Set the list of directories from which to load auto-loaded scripts."), _("\
1600 Show the list of directories from which to load auto-loaded scripts."),
1601 scripts_directory_help.get (),
1602 set_auto_load_dir, show_auto_load_dir,
1603 auto_load_set_cmdlist_get (),
1604 auto_load_show_cmdlist_get ());
1605 auto_load_safe_path_vec_update ();
1606 add_setshow_optional_filename_cmd ("safe-path", class_support,
1607 &auto_load_safe_path, _("\
1608 Set the list of files and directories that are safe for auto-loading."), _("\
1609 Show the list of files and directories that are safe for auto-loading."), _("\
1610 Various files loaded automatically for the 'set auto-load ...' options must\n\
1611 be located in one of the directories listed by this option. Warning will be\n\
1612 printed and file will not be used otherwise.\n\
1613 You can mix both directory and filename entries.\n\
1614 Setting this parameter to an empty list resets it to its default value.\n\
1615 Setting this parameter to '/' (without the quotes) allows any file\n\
1616 for the 'set auto-load ...' options. Each path entry can be also shell\n\
1617 wildcard pattern; '*' does not match directory separator.\n\
1618 This option is ignored for the kinds of files having 'set auto-load ... off'.\n\
1619 This option has security implications for untrusted inferiors."),
1620 set_auto_load_safe_path,
1621 show_auto_load_safe_path,
1622 auto_load_set_cmdlist_get (),
1623 auto_load_show_cmdlist_get ());
1624 gdb::observers::gdb_datadir_changed.attach (auto_load_gdb_datadir_changed,
1625 "auto-load");
1627 cmd = add_cmd ("add-auto-load-safe-path", class_support,
1628 add_auto_load_safe_path,
1629 _("Add entries to the list of directories from which it is safe "
1630 "to auto-load files.\n\
1631 See the commands 'set auto-load safe-path' and 'show auto-load safe-path' to\n\
1632 access the current full list setting."),
1633 &cmdlist);
1634 set_cmd_completer (cmd, deprecated_filename_completer);
1636 cmd = add_cmd ("add-auto-load-scripts-directory", class_support,
1637 add_auto_load_dir,
1638 _("Add entries to the list of directories from which to load "
1639 "auto-loaded scripts.\n\
1640 See the commands 'set auto-load scripts-directory' and\n\
1641 'show auto-load scripts-directory' to access the current full list setting."),
1642 &cmdlist);
1643 set_cmd_completer (cmd, deprecated_filename_completer);
1645 add_setshow_boolean_cmd ("auto-load", class_maintenance,
1646 &debug_auto_load, _("\
1647 Set auto-load verifications debugging."), _("\
1648 Show auto-load verifications debugging."), _("\
1649 When non-zero, debugging output for files of 'set auto-load ...'\n\
1650 is displayed."),
1651 NULL, show_debug_auto_load,
1652 &setdebuglist, &showdebuglist);