[PATCH 7/57][Arm][GAS] Add support for MVE instructions: vstr/vldr
[binutils-gdb.git] / gdb / auto-load.c
blob6833c21323ae93ee7914979095f3e17bfb26bc89
1 /* GDB routines for supporting auto-loaded scripts.
3 Copyright (C) 2012-2019 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 "common/filestuff.h"
40 #include "extension.h"
41 #include "gdb/section-scripts.h"
42 #include <algorithm>
43 #include "common/pathstuff.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 static void maybe_print_unsupported_script_warning
54 (struct auto_load_pspace_info *, struct objfile *objfile,
55 const struct extension_language_defn *language,
56 const char *section_name, unsigned offset);
58 static void maybe_print_script_not_found_warning
59 (struct auto_load_pspace_info *, struct objfile *objfile,
60 const struct extension_language_defn *language,
61 const char *section_name, unsigned offset);
63 /* Value of the 'set debug auto-load' configuration variable. */
64 static int debug_auto_load = 0;
66 /* "show" command for the debug_auto_load configuration variable. */
68 static void
69 show_debug_auto_load (struct ui_file *file, int from_tty,
70 struct cmd_list_element *c, const char *value)
72 fprintf_filtered (file, _("Debugging output for files "
73 "of 'set auto-load ...' is %s.\n"),
74 value);
77 /* User-settable option to enable/disable auto-loading of GDB_AUTO_FILE_NAME
78 scripts:
79 set auto-load gdb-scripts on|off
80 This is true if we should auto-load associated scripts when an objfile
81 is opened, false otherwise. */
82 static int auto_load_gdb_scripts = 1;
84 /* "show" command for the auto_load_gdb_scripts configuration variable. */
86 static void
87 show_auto_load_gdb_scripts (struct ui_file *file, int from_tty,
88 struct cmd_list_element *c, const char *value)
90 fprintf_filtered (file, _("Auto-loading of canned sequences of commands "
91 "scripts is %s.\n"),
92 value);
95 /* Return non-zero if auto-loading gdb scripts is enabled. */
97 int
98 auto_load_gdb_scripts_enabled (const struct extension_language_defn *extlang)
100 return auto_load_gdb_scripts;
103 /* Internal-use flag to enable/disable auto-loading.
104 This is true if we should auto-load python code when an objfile is opened,
105 false otherwise.
107 Both auto_load_scripts && global_auto_load must be true to enable
108 auto-loading.
110 This flag exists to facilitate deferring auto-loading during start-up
111 until after ./.gdbinit has been read; it may augment the search directories
112 used to find the scripts. */
113 int global_auto_load = 1;
115 /* Auto-load .gdbinit file from the current directory? */
116 int auto_load_local_gdbinit = 1;
118 /* Absolute pathname to the current directory .gdbinit, if it exists. */
119 char *auto_load_local_gdbinit_pathname = NULL;
121 /* Boolean value if AUTO_LOAD_LOCAL_GDBINIT_PATHNAME has been loaded. */
122 int auto_load_local_gdbinit_loaded = 0;
124 /* "show" command for the auto_load_local_gdbinit configuration variable. */
126 static void
127 show_auto_load_local_gdbinit (struct ui_file *file, int from_tty,
128 struct cmd_list_element *c, const char *value)
130 fprintf_filtered (file, _("Auto-loading of .gdbinit script from current "
131 "directory is %s.\n"),
132 value);
135 /* Directory list from which to load auto-loaded scripts. It is not checked
136 for absolute paths but they are strongly recommended. It is initialized by
137 _initialize_auto_load. */
138 static char *auto_load_dir;
140 /* "set" command for the auto_load_dir configuration variable. */
142 static void
143 set_auto_load_dir (const char *args, int from_tty, struct cmd_list_element *c)
145 /* Setting the variable to "" resets it to the compile time defaults. */
146 if (auto_load_dir[0] == '\0')
148 xfree (auto_load_dir);
149 auto_load_dir = xstrdup (AUTO_LOAD_DIR);
153 /* "show" command for the auto_load_dir configuration variable. */
155 static void
156 show_auto_load_dir (struct ui_file *file, int from_tty,
157 struct cmd_list_element *c, const char *value)
159 fprintf_filtered (file, _("List of directories from which to load "
160 "auto-loaded scripts is %s.\n"),
161 value);
164 /* Directory list safe to hold auto-loaded files. It is not checked for
165 absolute paths but they are strongly recommended. It is initialized by
166 _initialize_auto_load. */
167 static char *auto_load_safe_path;
169 /* Vector of directory elements of AUTO_LOAD_SAFE_PATH with each one normalized
170 by tilde_expand and possibly each entries has added its gdb_realpath
171 counterpart. */
172 std::vector<gdb::unique_xmalloc_ptr<char>> auto_load_safe_path_vec;
174 /* Expand $datadir and $debugdir in STRING according to the rules of
175 substitute_path_component. */
177 static std::vector<gdb::unique_xmalloc_ptr<char>>
178 auto_load_expand_dir_vars (const char *string)
180 char *s = xstrdup (string);
181 substitute_path_component (&s, "$datadir", gdb_datadir);
182 substitute_path_component (&s, "$debugdir", debug_file_directory);
184 if (debug_auto_load && strcmp (s, string) != 0)
185 fprintf_unfiltered (gdb_stdlog,
186 _("auto-load: Expanded $-variables to \"%s\".\n"), 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 if (debug_auto_load)
201 fprintf_unfiltered (gdb_stdlog,
202 _("auto-load: Updating directories of \"%s\".\n"),
203 auto_load_safe_path);
205 auto_load_safe_path_vec = auto_load_expand_dir_vars (auto_load_safe_path);
206 size_t len = auto_load_safe_path_vec.size ();
208 /* Apply tilde_expand and gdb_realpath to each AUTO_LOAD_SAFE_PATH_VEC
209 element. */
210 for (size_t i = 0; i < len; i++)
212 gdb::unique_xmalloc_ptr<char> &in_vec = auto_load_safe_path_vec[i];
213 gdb::unique_xmalloc_ptr<char> expanded (tilde_expand (in_vec.get ()));
214 gdb::unique_xmalloc_ptr<char> real_path = gdb_realpath (expanded.get ());
216 /* Ensure the current entry is at least tilde_expand-ed. ORIGINAL makes
217 sure we free the original string. */
218 gdb::unique_xmalloc_ptr<char> original = std::move (in_vec);
219 in_vec = std::move (expanded);
221 if (debug_auto_load)
223 if (strcmp (in_vec.get (), original.get ()) == 0)
224 fprintf_unfiltered (gdb_stdlog,
225 _("auto-load: Using directory \"%s\".\n"),
226 in_vec.get ());
227 else
228 fprintf_unfiltered (gdb_stdlog,
229 _("auto-load: Resolved directory \"%s\" "
230 "as \"%s\".\n"),
231 original.get (), in_vec.get ());
234 /* If gdb_realpath returns a different content, append it. */
235 if (strcmp (real_path.get (), in_vec.get ()) != 0)
237 if (debug_auto_load)
238 fprintf_unfiltered (gdb_stdlog,
239 _("auto-load: And canonicalized as \"%s\".\n"),
240 real_path.get ());
242 auto_load_safe_path_vec.push_back (std::move (real_path));
247 /* Variable gdb_datadir has been set. Update content depending on $datadir. */
249 static void
250 auto_load_gdb_datadir_changed (void)
252 auto_load_safe_path_vec_update ();
255 /* "set" command for the auto_load_safe_path configuration variable. */
257 static void
258 set_auto_load_safe_path (const char *args,
259 int from_tty, struct cmd_list_element *c)
261 /* Setting the variable to "" resets it to the compile time defaults. */
262 if (auto_load_safe_path[0] == '\0')
264 xfree (auto_load_safe_path);
265 auto_load_safe_path = xstrdup (AUTO_LOAD_SAFE_PATH);
268 auto_load_safe_path_vec_update ();
271 /* "show" command for the auto_load_safe_path configuration variable. */
273 static void
274 show_auto_load_safe_path (struct ui_file *file, int from_tty,
275 struct cmd_list_element *c, const char *value)
277 const char *cs;
279 /* Check if user has entered either "/" or for example ":".
280 But while more complicate content like ":/foo" would still also
281 permit any location do not hide those. */
283 for (cs = value; *cs && (*cs == DIRNAME_SEPARATOR || IS_DIR_SEPARATOR (*cs));
284 cs++);
285 if (*cs == 0)
286 fprintf_filtered (file, _("Auto-load files are safe to load from any "
287 "directory.\n"));
288 else
289 fprintf_filtered (file, _("List of directories from which it is safe to "
290 "auto-load files is %s.\n"),
291 value);
294 /* "add-auto-load-safe-path" command for the auto_load_safe_path configuration
295 variable. */
297 static void
298 add_auto_load_safe_path (const char *args, int from_tty)
300 char *s;
302 if (args == NULL || *args == 0)
303 error (_("\
304 Directory argument required.\n\
305 Use 'set auto-load safe-path /' for disabling the auto-load safe-path security.\
306 "));
308 s = xstrprintf ("%s%c%s", auto_load_safe_path, DIRNAME_SEPARATOR, args);
309 xfree (auto_load_safe_path);
310 auto_load_safe_path = s;
312 auto_load_safe_path_vec_update ();
315 /* "add-auto-load-scripts-directory" command for the auto_load_dir configuration
316 variable. */
318 static void
319 add_auto_load_dir (const char *args, int from_tty)
321 char *s;
323 if (args == NULL || *args == 0)
324 error (_("Directory argument required."));
326 s = xstrprintf ("%s%c%s", auto_load_dir, DIRNAME_SEPARATOR, args);
327 xfree (auto_load_dir);
328 auto_load_dir = s;
331 /* Implementation for filename_is_in_pattern overwriting the caller's FILENAME
332 and PATTERN. */
334 static int
335 filename_is_in_pattern_1 (char *filename, char *pattern)
337 size_t pattern_len = strlen (pattern);
338 size_t filename_len = strlen (filename);
340 if (debug_auto_load)
341 fprintf_unfiltered (gdb_stdlog, _("auto-load: Matching file \"%s\" "
342 "to pattern \"%s\"\n"),
343 filename, pattern);
345 /* Trim trailing slashes ("/") from PATTERN. Even for "d:\" paths as
346 trailing slashes are trimmed also from FILENAME it still matches
347 correctly. */
348 while (pattern_len && IS_DIR_SEPARATOR (pattern[pattern_len - 1]))
349 pattern_len--;
350 pattern[pattern_len] = '\0';
352 /* Ensure auto_load_safe_path "/" matches any FILENAME. On MS-Windows
353 platform FILENAME even after gdb_realpath does not have to start with
354 IS_DIR_SEPARATOR character, such as the 'C:\x.exe' filename. */
355 if (pattern_len == 0)
357 if (debug_auto_load)
358 fprintf_unfiltered (gdb_stdlog,
359 _("auto-load: Matched - empty pattern\n"));
360 return 1;
363 for (;;)
365 /* Trim trailing slashes ("/"). PATTERN also has slashes trimmed the
366 same way so they will match. */
367 while (filename_len && IS_DIR_SEPARATOR (filename[filename_len - 1]))
368 filename_len--;
369 filename[filename_len] = '\0';
370 if (filename_len == 0)
372 if (debug_auto_load)
373 fprintf_unfiltered (gdb_stdlog,
374 _("auto-load: Not matched - pattern \"%s\".\n"),
375 pattern);
376 return 0;
379 if (gdb_filename_fnmatch (pattern, filename, FNM_FILE_NAME | FNM_NOESCAPE)
380 == 0)
382 if (debug_auto_load)
383 fprintf_unfiltered (gdb_stdlog, _("auto-load: Matched - file "
384 "\"%s\" to pattern \"%s\".\n"),
385 filename, pattern);
386 return 1;
389 /* Trim trailing FILENAME component. */
390 while (filename_len > 0 && !IS_DIR_SEPARATOR (filename[filename_len - 1]))
391 filename_len--;
395 /* Return 1 if FILENAME matches PATTERN or if FILENAME resides in
396 a subdirectory of a directory that matches PATTERN. Return 0 otherwise.
397 gdb_realpath normalization is never done here. */
399 static ATTRIBUTE_PURE int
400 filename_is_in_pattern (const char *filename, const char *pattern)
402 char *filename_copy, *pattern_copy;
404 filename_copy = (char *) alloca (strlen (filename) + 1);
405 strcpy (filename_copy, filename);
406 pattern_copy = (char *) alloca (strlen (pattern) + 1);
407 strcpy (pattern_copy, pattern);
409 return filename_is_in_pattern_1 (filename_copy, pattern_copy);
412 /* Return 1 if FILENAME belongs to one of directory components of
413 AUTO_LOAD_SAFE_PATH_VEC. Return 0 otherwise.
414 auto_load_safe_path_vec_update is never called.
415 *FILENAME_REALP may be updated by gdb_realpath of FILENAME. */
417 static int
418 filename_is_in_auto_load_safe_path_vec (const char *filename,
419 gdb::unique_xmalloc_ptr<char> *filename_realp)
421 const char *pattern = NULL;
423 for (const gdb::unique_xmalloc_ptr<char> &p : auto_load_safe_path_vec)
424 if (*filename_realp == NULL && filename_is_in_pattern (filename, p.get ()))
426 pattern = p.get ();
427 break;
430 if (pattern == NULL)
432 if (*filename_realp == NULL)
434 *filename_realp = gdb_realpath (filename);
435 if (debug_auto_load && strcmp (filename_realp->get (), filename) != 0)
436 fprintf_unfiltered (gdb_stdlog,
437 _("auto-load: Resolved "
438 "file \"%s\" as \"%s\".\n"),
439 filename, filename_realp->get ());
442 if (strcmp (filename_realp->get (), filename) != 0)
443 for (const gdb::unique_xmalloc_ptr<char> &p : auto_load_safe_path_vec)
444 if (filename_is_in_pattern (filename_realp->get (), p.get ()))
446 pattern = p.get ();
447 break;
451 if (pattern != NULL)
453 if (debug_auto_load)
454 fprintf_unfiltered (gdb_stdlog, _("auto-load: File \"%s\" matches "
455 "directory \"%s\".\n"),
456 filename, pattern);
457 return 1;
460 return 0;
463 /* Return 1 if FILENAME is located in one of the directories of
464 AUTO_LOAD_SAFE_PATH. Otherwise call warning and return 0. FILENAME does
465 not have to be an absolute path.
467 Existence of FILENAME is not checked. Function will still give a warning
468 even if the caller would quietly skip non-existing file in unsafe
469 directory. */
472 file_is_auto_load_safe (const char *filename, const char *debug_fmt, ...)
474 gdb::unique_xmalloc_ptr<char> filename_real;
475 static int advice_printed = 0;
477 if (debug_auto_load)
479 va_list debug_args;
481 va_start (debug_args, debug_fmt);
482 vfprintf_unfiltered (gdb_stdlog, debug_fmt, debug_args);
483 va_end (debug_args);
486 if (filename_is_in_auto_load_safe_path_vec (filename, &filename_real))
487 return 1;
489 auto_load_safe_path_vec_update ();
490 if (filename_is_in_auto_load_safe_path_vec (filename, &filename_real))
491 return 1;
493 warning (_("File \"%s\" auto-loading has been declined by your "
494 "`auto-load safe-path' set to \"%s\"."),
495 filename_real.get (), auto_load_safe_path);
497 if (!advice_printed)
499 const char *homedir = getenv ("HOME");
501 if (homedir == NULL)
502 homedir = "$HOME";
503 std::string homeinit = string_printf ("%s/%s", homedir, gdbinit);
505 printf_filtered (_("\
506 To enable execution of this file add\n\
507 \tadd-auto-load-safe-path %s\n\
508 line to your configuration file \"%s\".\n\
509 To completely disable this security protection add\n\
510 \tset auto-load safe-path /\n\
511 line to your configuration file \"%s\".\n\
512 For more information about this security protection see the\n\
513 \"Auto-loading safe path\" section in the GDB manual. E.g., run from the shell:\n\
514 \tinfo \"(gdb)Auto-loading safe path\"\n"),
515 filename_real.get (),
516 homeinit.c_str (), homeinit.c_str ());
517 advice_printed = 1;
520 return 0;
523 /* For scripts specified in .debug_gdb_scripts, multiple objfiles may load
524 the same script. There's no point in loading the script multiple times,
525 and there can be a lot of objfiles and scripts, so we keep track of scripts
526 loaded this way. */
528 struct auto_load_pspace_info
530 auto_load_pspace_info () = default;
531 ~auto_load_pspace_info ();
533 /* For each program space we keep track of loaded scripts, both when
534 specified as file names and as scripts to be executed directly. */
535 struct htab *loaded_script_files = nullptr;
536 struct htab *loaded_script_texts = nullptr;
538 /* Non-zero if we've issued the warning about an auto-load script not being
539 supported. We only want to issue this warning once. */
540 bool unsupported_script_warning_printed = false;
542 /* Non-zero if we've issued the warning about an auto-load script not being
543 found. We only want to issue this warning once. */
544 bool script_not_found_warning_printed = false;
547 /* Objects of this type are stored in the loaded_script hash table. */
549 struct loaded_script
551 /* Name as provided by the objfile. */
552 const char *name;
554 /* Full path name or NULL if script wasn't found (or was otherwise
555 inaccessible), or NULL for loaded_script_texts. */
556 const char *full_path;
558 /* Non-zero if this script has been loaded. */
559 int loaded;
561 const struct extension_language_defn *language;
564 /* Per-program-space data key. */
565 static const struct program_space_key<struct auto_load_pspace_info>
566 auto_load_pspace_data;
568 auto_load_pspace_info::~auto_load_pspace_info ()
570 if (loaded_script_files)
571 htab_delete (loaded_script_files);
572 if (loaded_script_texts)
573 htab_delete (loaded_script_texts);
576 /* Get the current autoload data. If none is found yet, add it now. This
577 function always returns a valid object. */
579 static struct auto_load_pspace_info *
580 get_auto_load_pspace_data (struct program_space *pspace)
582 struct auto_load_pspace_info *info;
584 info = auto_load_pspace_data.get (pspace);
585 if (info == NULL)
586 info = auto_load_pspace_data.emplace (pspace);
588 return info;
591 /* Hash function for the loaded script hash. */
593 static hashval_t
594 hash_loaded_script_entry (const void *data)
596 const struct loaded_script *e = (const struct loaded_script *) data;
598 return htab_hash_string (e->name) ^ htab_hash_pointer (e->language);
601 /* Equality function for the loaded script hash. */
603 static int
604 eq_loaded_script_entry (const void *a, const void *b)
606 const struct loaded_script *ea = (const struct loaded_script *) a;
607 const struct loaded_script *eb = (const struct loaded_script *) b;
609 return strcmp (ea->name, eb->name) == 0 && ea->language == eb->language;
612 /* Initialize the table to track loaded scripts.
613 Each entry is hashed by the full path name. */
615 static void
616 init_loaded_scripts_info (struct auto_load_pspace_info *pspace_info)
618 /* Choose 31 as the starting size of the hash table, somewhat arbitrarily.
619 Space for each entry is obtained with one malloc so we can free them
620 easily. */
622 pspace_info->loaded_script_files = htab_create (31,
623 hash_loaded_script_entry,
624 eq_loaded_script_entry,
625 xfree);
626 pspace_info->loaded_script_texts = htab_create (31,
627 hash_loaded_script_entry,
628 eq_loaded_script_entry,
629 xfree);
631 pspace_info->unsupported_script_warning_printed = false;
632 pspace_info->script_not_found_warning_printed = false;
635 /* Wrapper on get_auto_load_pspace_data to also allocate the hash table
636 for loading scripts. */
638 struct auto_load_pspace_info *
639 get_auto_load_pspace_data_for_loading (struct program_space *pspace)
641 struct auto_load_pspace_info *info;
643 info = get_auto_load_pspace_data (pspace);
644 if (info->loaded_script_files == NULL)
645 init_loaded_scripts_info (info);
647 return info;
650 /* Add script file NAME in LANGUAGE to hash table of PSPACE_INFO.
651 LOADED 1 if the script has been (is going to) be loaded, 0 otherwise
652 (such as if it has not been found).
653 FULL_PATH is NULL if the script wasn't found.
654 The result is true if the script was already in the hash table. */
656 static int
657 maybe_add_script_file (struct auto_load_pspace_info *pspace_info, int loaded,
658 const char *name, const char *full_path,
659 const struct extension_language_defn *language)
661 struct htab *htab = pspace_info->loaded_script_files;
662 struct loaded_script **slot, entry;
663 int in_hash_table;
665 entry.name = name;
666 entry.language = language;
667 slot = (struct loaded_script **) htab_find_slot (htab, &entry, INSERT);
668 in_hash_table = *slot != NULL;
670 /* If this script is not in the hash table, add it. */
672 if (!in_hash_table)
674 char *p;
676 /* Allocate all space in one chunk so it's easier to free. */
677 *slot = ((struct loaded_script *)
678 xmalloc (sizeof (**slot)
679 + strlen (name) + 1
680 + (full_path != NULL ? (strlen (full_path) + 1) : 0)));
681 p = ((char*) *slot) + sizeof (**slot);
682 strcpy (p, name);
683 (*slot)->name = p;
684 if (full_path != NULL)
686 p += strlen (p) + 1;
687 strcpy (p, full_path);
688 (*slot)->full_path = p;
690 else
691 (*slot)->full_path = NULL;
692 (*slot)->loaded = loaded;
693 (*slot)->language = language;
696 return in_hash_table;
699 /* Add script contents NAME in LANGUAGE to hash table of PSPACE_INFO.
700 LOADED 1 if the script has been (is going to) be loaded, 0 otherwise
701 (such as if it has not been found).
702 The result is true if the script was already in the hash table. */
704 static int
705 maybe_add_script_text (struct auto_load_pspace_info *pspace_info,
706 int loaded, const char *name,
707 const struct extension_language_defn *language)
709 struct htab *htab = pspace_info->loaded_script_texts;
710 struct loaded_script **slot, entry;
711 int in_hash_table;
713 entry.name = name;
714 entry.language = language;
715 slot = (struct loaded_script **) htab_find_slot (htab, &entry, INSERT);
716 in_hash_table = *slot != NULL;
718 /* If this script is not in the hash table, add it. */
720 if (!in_hash_table)
722 char *p;
724 /* Allocate all space in one chunk so it's easier to free. */
725 *slot = ((struct loaded_script *)
726 xmalloc (sizeof (**slot) + strlen (name) + 1));
727 p = ((char*) *slot) + sizeof (**slot);
728 strcpy (p, name);
729 (*slot)->name = p;
730 (*slot)->full_path = NULL;
731 (*slot)->loaded = loaded;
732 (*slot)->language = language;
735 return in_hash_table;
738 /* Clear the table of loaded section scripts. */
740 static void
741 clear_section_scripts (void)
743 struct program_space *pspace = current_program_space;
744 struct auto_load_pspace_info *info;
746 info = auto_load_pspace_data.get (pspace);
747 if (info != NULL && info->loaded_script_files != NULL)
748 auto_load_pspace_data.clear (pspace);
751 /* Look for the auto-load script in LANGUAGE associated with OBJFILE where
752 OBJFILE's gdb_realpath is REALNAME and load it. Return 1 if we found any
753 matching script, return 0 otherwise. */
755 static int
756 auto_load_objfile_script_1 (struct objfile *objfile, const char *realname,
757 const struct extension_language_defn *language)
759 const char *debugfile;
760 int retval;
761 const char *suffix = ext_lang_auto_load_suffix (language);
763 std::string filename = std::string (realname) + suffix;
765 gdb_file_up input = gdb_fopen_cloexec (filename.c_str (), "r");
766 debugfile = filename.c_str ();
767 if (debug_auto_load)
768 fprintf_unfiltered (gdb_stdlog, _("auto-load: Attempted file \"%s\" %s.\n"),
769 debugfile, input ? _("exists") : _("does not exist"));
771 std::string debugfile_holder;
772 if (!input)
774 /* Also try the same file in a subdirectory of gdb's data
775 directory. */
777 std::vector<gdb::unique_xmalloc_ptr<char>> vec
778 = auto_load_expand_dir_vars (auto_load_dir);
780 if (debug_auto_load)
781 fprintf_unfiltered (gdb_stdlog, _("auto-load: Searching 'set auto-load "
782 "scripts-directory' path \"%s\".\n"),
783 auto_load_dir);
785 for (const gdb::unique_xmalloc_ptr<char> &dir : vec)
787 /* FILENAME is absolute, so we don't need a "/" here. */
788 debugfile_holder = dir.get () + filename;
789 debugfile = debugfile_holder.c_str ();
791 input = gdb_fopen_cloexec (debugfile, "r");
792 if (debug_auto_load)
793 fprintf_unfiltered (gdb_stdlog, _("auto-load: Attempted file "
794 "\"%s\" %s.\n"),
795 debugfile,
796 input ? _("exists") : _("does not exist"));
797 if (input != NULL)
798 break;
802 if (input)
804 int is_safe;
805 struct auto_load_pspace_info *pspace_info;
807 is_safe
808 = file_is_auto_load_safe (debugfile,
809 _("auto-load: Loading %s script \"%s\""
810 " by extension for objfile \"%s\".\n"),
811 ext_lang_name (language),
812 debugfile, objfile_name (objfile));
814 /* Add this script to the hash table too so
815 "info auto-load ${lang}-scripts" can print it. */
816 pspace_info
817 = get_auto_load_pspace_data_for_loading (current_program_space);
818 maybe_add_script_file (pspace_info, is_safe, debugfile, debugfile,
819 language);
821 /* To preserve existing behaviour we don't check for whether the
822 script was already in the table, and always load it.
823 It's highly unlikely that we'd ever load it twice,
824 and these scripts are required to be idempotent under multiple
825 loads anyway. */
826 if (is_safe)
828 objfile_script_sourcer_func *sourcer
829 = ext_lang_objfile_script_sourcer (language);
831 /* We shouldn't get here if support for the language isn't
832 compiled in. And the extension language is required to implement
833 this function. */
834 gdb_assert (sourcer != NULL);
835 sourcer (language, objfile, input.get (), debugfile);
838 retval = 1;
840 else
841 retval = 0;
843 return retval;
846 /* Look for the auto-load script in LANGUAGE associated with OBJFILE and load
847 it. */
849 void
850 auto_load_objfile_script (struct objfile *objfile,
851 const struct extension_language_defn *language)
853 gdb::unique_xmalloc_ptr<char> realname
854 = gdb_realpath (objfile_name (objfile));
856 if (!auto_load_objfile_script_1 (objfile, realname.get (), language))
858 /* For Windows/DOS .exe executables, strip the .exe suffix, so that
859 FOO-gdb.gdb could be used for FOO.exe, and try again. */
861 size_t len = strlen (realname.get ());
862 const size_t lexe = sizeof (".exe") - 1;
864 if (len > lexe && strcasecmp (realname.get () + len - lexe, ".exe") == 0)
866 len -= lexe;
867 realname.get ()[len] = '\0';
868 if (debug_auto_load)
869 fprintf_unfiltered (gdb_stdlog, _("auto-load: Stripped .exe suffix, "
870 "retrying with \"%s\".\n"),
871 realname.get ());
872 auto_load_objfile_script_1 (objfile, realname.get (), language);
877 /* Subroutine of source_section_scripts to simplify it.
878 Load FILE as a script in extension language LANGUAGE.
879 The script is from section SECTION_NAME in OBJFILE at offset OFFSET. */
881 static void
882 source_script_file (struct auto_load_pspace_info *pspace_info,
883 struct objfile *objfile,
884 const struct extension_language_defn *language,
885 const char *section_name, unsigned int offset,
886 const char *file)
888 int in_hash_table;
889 objfile_script_sourcer_func *sourcer;
891 /* Skip this script if support is not compiled in. */
892 sourcer = ext_lang_objfile_script_sourcer (language);
893 if (sourcer == NULL)
895 /* We don't throw an error, the program is still debuggable. */
896 maybe_print_unsupported_script_warning (pspace_info, objfile, language,
897 section_name, offset);
898 /* We *could* still try to open it, but there's no point. */
899 maybe_add_script_file (pspace_info, 0, file, NULL, language);
900 return;
903 /* Skip this script if auto-loading it has been disabled. */
904 if (!ext_lang_auto_load_enabled (language))
906 /* No message is printed, just skip it. */
907 return;
910 gdb::optional<open_script> opened = find_and_open_script (file,
911 1 /*search_path*/);
913 if (opened)
915 if (!file_is_auto_load_safe (opened->full_path.get (),
916 _("auto-load: Loading %s script "
917 "\"%s\" from section \"%s\" of "
918 "objfile \"%s\".\n"),
919 ext_lang_name (language),
920 opened->full_path.get (),
921 section_name, objfile_name (objfile)))
922 opened.reset ();
924 else
926 /* If one script isn't found it's not uncommon for more to not be
927 found either. We don't want to print a message for each script,
928 too much noise. Instead, we print the warning once and tell the
929 user how to find the list of scripts that weren't loaded.
930 We don't throw an error, the program is still debuggable.
932 IWBN if complaints.c were more general-purpose. */
934 maybe_print_script_not_found_warning (pspace_info, objfile, language,
935 section_name, offset);
938 in_hash_table = maybe_add_script_file (pspace_info, bool (opened), file,
939 (opened
940 ? opened->full_path.get ()
941 : NULL),
942 language);
944 /* If this file is not currently loaded, load it. */
945 if (opened && !in_hash_table)
946 sourcer (language, objfile, opened->stream.get (),
947 opened->full_path.get ());
950 /* Subroutine of source_section_scripts to simplify it.
951 Execute SCRIPT as a script in extension language LANG.
952 The script is from section SECTION_NAME in OBJFILE at offset OFFSET. */
954 static void
955 execute_script_contents (struct auto_load_pspace_info *pspace_info,
956 struct objfile *objfile,
957 const struct extension_language_defn *language,
958 const char *section_name, unsigned int offset,
959 const char *script)
961 objfile_script_executor_func *executor;
962 const char *newline, *script_text;
963 const char *name;
964 int is_safe, in_hash_table;
966 /* The first line of the script is the name of the script.
967 It must not contain any kind of space character. */
968 name = NULL;
969 newline = strchr (script, '\n');
970 std::string name_holder;
971 if (newline != NULL)
973 const char *buf, *p;
975 /* Put the name in a buffer and validate it. */
976 name_holder = std::string (script, newline - script);
977 buf = name_holder.c_str ();
978 for (p = buf; *p != '\0'; ++p)
980 if (isspace (*p))
981 break;
983 /* We don't allow nameless scripts, they're not helpful to the user. */
984 if (p != buf && *p == '\0')
985 name = buf;
987 if (name == NULL)
989 /* We don't throw an error, the program is still debuggable. */
990 warning (_("\
991 Missing/bad script name in entry at offset %u in section %s\n\
992 of file %s."),
993 offset, section_name, objfile_name (objfile));
994 return;
996 script_text = newline + 1;
998 /* Skip this script if support is not compiled in. */
999 executor = ext_lang_objfile_script_executor (language);
1000 if (executor == NULL)
1002 /* We don't throw an error, the program is still debuggable. */
1003 maybe_print_unsupported_script_warning (pspace_info, objfile, language,
1004 section_name, offset);
1005 maybe_add_script_text (pspace_info, 0, name, language);
1006 return;
1009 /* Skip this script if auto-loading it has been disabled. */
1010 if (!ext_lang_auto_load_enabled (language))
1012 /* No message is printed, just skip it. */
1013 return;
1016 is_safe = file_is_auto_load_safe (objfile_name (objfile),
1017 _("auto-load: Loading %s script "
1018 "\"%s\" from section \"%s\" of "
1019 "objfile \"%s\".\n"),
1020 ext_lang_name (language), name,
1021 section_name, objfile_name (objfile));
1023 in_hash_table = maybe_add_script_text (pspace_info, is_safe, name, language);
1025 /* If this file is not currently loaded, load it. */
1026 if (is_safe && !in_hash_table)
1027 executor (language, objfile, name, script_text);
1030 /* Load scripts specified in OBJFILE.
1031 START,END delimit a buffer containing a list of nul-terminated
1032 file names.
1033 SECTION_NAME is used in error messages.
1035 Scripts specified as file names are found per normal "source -s" command
1036 processing. First the script is looked for in $cwd. If not found there
1037 the source search path is used.
1039 The section contains a list of path names of script files to load or
1040 actual script contents. Each entry is nul-terminated. */
1042 static void
1043 source_section_scripts (struct objfile *objfile, const char *section_name,
1044 const char *start, const char *end)
1046 const char *p;
1047 struct auto_load_pspace_info *pspace_info;
1049 pspace_info = get_auto_load_pspace_data_for_loading (current_program_space);
1051 for (p = start; p < end; ++p)
1053 const char *entry;
1054 const struct extension_language_defn *language;
1055 unsigned int offset = p - start;
1056 int code = *p;
1058 switch (code)
1060 case SECTION_SCRIPT_ID_PYTHON_FILE:
1061 case SECTION_SCRIPT_ID_PYTHON_TEXT:
1062 language = get_ext_lang_defn (EXT_LANG_PYTHON);
1063 break;
1064 case SECTION_SCRIPT_ID_SCHEME_FILE:
1065 case SECTION_SCRIPT_ID_SCHEME_TEXT:
1066 language = get_ext_lang_defn (EXT_LANG_GUILE);
1067 break;
1068 default:
1069 warning (_("Invalid entry in %s section"), section_name);
1070 /* We could try various heuristics to find the next valid entry,
1071 but it's safer to just punt. */
1072 return;
1074 entry = ++p;
1076 while (p < end && *p != '\0')
1077 ++p;
1078 if (p == end)
1080 warning (_("Non-nul-terminated entry in %s at offset %u"),
1081 section_name, offset);
1082 /* Don't load/execute it. */
1083 break;
1086 switch (code)
1088 case SECTION_SCRIPT_ID_PYTHON_FILE:
1089 case SECTION_SCRIPT_ID_SCHEME_FILE:
1090 if (p == entry)
1092 warning (_("Empty entry in %s at offset %u"),
1093 section_name, offset);
1094 continue;
1096 source_script_file (pspace_info, objfile, language,
1097 section_name, offset, entry);
1098 break;
1099 case SECTION_SCRIPT_ID_PYTHON_TEXT:
1100 case SECTION_SCRIPT_ID_SCHEME_TEXT:
1101 execute_script_contents (pspace_info, objfile, language,
1102 section_name, offset, entry);
1103 break;
1108 /* Load scripts specified in section SECTION_NAME of OBJFILE. */
1110 static void
1111 auto_load_section_scripts (struct objfile *objfile, const char *section_name)
1113 bfd *abfd = objfile->obfd;
1114 asection *scripts_sect;
1115 bfd_byte *data = NULL;
1117 scripts_sect = bfd_get_section_by_name (abfd, section_name);
1118 if (scripts_sect == NULL
1119 || (bfd_get_section_flags (abfd, scripts_sect) & SEC_HAS_CONTENTS) == 0)
1120 return;
1122 if (!bfd_get_full_section_contents (abfd, scripts_sect, &data))
1123 warning (_("Couldn't read %s section of %s"),
1124 section_name, bfd_get_filename (abfd));
1125 else
1127 gdb::unique_xmalloc_ptr<bfd_byte> data_holder (data);
1129 char *p = (char *) data;
1130 source_section_scripts (objfile, section_name, p,
1131 p + bfd_get_section_size (scripts_sect));
1135 /* Load any auto-loaded scripts for OBJFILE. */
1137 void
1138 load_auto_scripts_for_objfile (struct objfile *objfile)
1140 /* Return immediately if auto-loading has been globally disabled.
1141 This is to handle sequencing of operations during gdb startup.
1142 Also return immediately if OBJFILE was not created from a file
1143 on the local filesystem. */
1144 if (!global_auto_load
1145 || (objfile->flags & OBJF_NOT_FILENAME) != 0
1146 || is_target_filename (objfile->original_name))
1147 return;
1149 /* Load any extension language scripts for this objfile.
1150 E.g., foo-gdb.gdb, foo-gdb.py. */
1151 auto_load_ext_lang_scripts_for_objfile (objfile);
1153 /* Load any scripts mentioned in AUTO_SECTION_NAME (.debug_gdb_scripts). */
1154 auto_load_section_scripts (objfile, AUTO_SECTION_NAME);
1157 /* This is a new_objfile observer callback to auto-load scripts.
1159 Two flavors of auto-loaded scripts are supported.
1160 1) based on the path to the objfile
1161 2) from .debug_gdb_scripts section */
1163 static void
1164 auto_load_new_objfile (struct objfile *objfile)
1166 if (!objfile)
1168 /* OBJFILE is NULL when loading a new "main" symbol-file. */
1169 clear_section_scripts ();
1170 return;
1173 load_auto_scripts_for_objfile (objfile);
1176 /* Collect scripts to be printed in a vec. */
1178 struct collect_matching_scripts_data
1180 collect_matching_scripts_data (std::vector<loaded_script *> *scripts_p_,
1181 const extension_language_defn *language_)
1182 : scripts_p (scripts_p_), language (language_)
1185 std::vector<loaded_script *> *scripts_p;
1186 const struct extension_language_defn *language;
1189 /* Traversal function for htab_traverse.
1190 Collect the entry if it matches the regexp. */
1192 static int
1193 collect_matching_scripts (void **slot, void *info)
1195 struct loaded_script *script = (struct loaded_script *) *slot;
1196 struct collect_matching_scripts_data *data
1197 = (struct collect_matching_scripts_data *) info;
1199 if (script->language == data->language && re_exec (script->name))
1200 data->scripts_p->push_back (script);
1202 return 1;
1205 /* Print SCRIPT. */
1207 static void
1208 print_script (struct loaded_script *script)
1210 struct ui_out *uiout = current_uiout;
1212 ui_out_emit_tuple tuple_emitter (uiout, NULL);
1214 uiout->field_string ("loaded", script->loaded ? "Yes" : "No");
1215 uiout->field_string ("script", script->name);
1216 uiout->text ("\n");
1218 /* If the name isn't the full path, print it too. */
1219 if (script->full_path != NULL
1220 && strcmp (script->name, script->full_path) != 0)
1222 uiout->text ("\tfull name: ");
1223 uiout->field_string ("full_path", script->full_path);
1224 uiout->text ("\n");
1228 /* Helper for info_auto_load_scripts to sort the scripts by name. */
1230 static bool
1231 sort_scripts_by_name (loaded_script *a, loaded_script *b)
1233 return FILENAME_CMP (a->name, b->name) < 0;
1236 /* Special internal GDB value of auto_load_info_scripts's PATTERN identify
1237 the "info auto-load XXX" command has been executed through the general
1238 "info auto-load" invocation. Extra newline will be printed if needed. */
1239 char auto_load_info_scripts_pattern_nl[] = "";
1241 /* Subroutine of auto_load_info_scripts to simplify it.
1242 Print SCRIPTS. */
1244 static void
1245 print_scripts (const std::vector<loaded_script *> &scripts)
1247 for (loaded_script *script : scripts)
1248 print_script (script);
1251 /* Implementation for "info auto-load gdb-scripts"
1252 (and "info auto-load python-scripts"). List scripts in LANGUAGE matching
1253 PATTERN. FROM_TTY is the usual GDB boolean for user interactivity. */
1255 void
1256 auto_load_info_scripts (const char *pattern, int from_tty,
1257 const struct extension_language_defn *language)
1259 struct ui_out *uiout = current_uiout;
1260 struct auto_load_pspace_info *pspace_info;
1262 dont_repeat ();
1264 pspace_info = get_auto_load_pspace_data (current_program_space);
1266 if (pattern && *pattern)
1268 char *re_err = re_comp (pattern);
1270 if (re_err)
1271 error (_("Invalid regexp: %s"), re_err);
1273 else
1275 re_comp ("");
1278 /* We need to know the number of rows before we build the table.
1279 Plus we want to sort the scripts by name.
1280 So first traverse the hash table collecting the matching scripts. */
1282 std::vector<loaded_script *> script_files, script_texts;
1284 if (pspace_info != NULL && pspace_info->loaded_script_files != NULL)
1286 collect_matching_scripts_data data (&script_files, language);
1288 /* Pass a pointer to scripts as VEC_safe_push can realloc space. */
1289 htab_traverse_noresize (pspace_info->loaded_script_files,
1290 collect_matching_scripts, &data);
1292 std::sort (script_files.begin (), script_files.end (),
1293 sort_scripts_by_name);
1296 if (pspace_info != NULL && pspace_info->loaded_script_texts != NULL)
1298 collect_matching_scripts_data data (&script_texts, language);
1300 /* Pass a pointer to scripts as VEC_safe_push can realloc space. */
1301 htab_traverse_noresize (pspace_info->loaded_script_texts,
1302 collect_matching_scripts, &data);
1304 std::sort (script_texts.begin (), script_texts.end (),
1305 sort_scripts_by_name);
1308 int nr_scripts = script_files.size () + script_texts.size ();
1310 /* Table header shifted right by preceding "gdb-scripts: " would not match
1311 its columns. */
1312 if (nr_scripts > 0 && pattern == auto_load_info_scripts_pattern_nl)
1313 uiout->text ("\n");
1316 ui_out_emit_table table_emitter (uiout, 2, nr_scripts,
1317 "AutoLoadedScriptsTable");
1319 uiout->table_header (7, ui_left, "loaded", "Loaded");
1320 uiout->table_header (70, ui_left, "script", "Script");
1321 uiout->table_body ();
1323 print_scripts (script_files);
1324 print_scripts (script_texts);
1327 if (nr_scripts == 0)
1329 if (pattern && *pattern)
1330 uiout->message ("No auto-load scripts matching %s.\n", pattern);
1331 else
1332 uiout->message ("No auto-load scripts.\n");
1336 /* Wrapper for "info auto-load gdb-scripts". */
1338 static void
1339 info_auto_load_gdb_scripts (const char *pattern, int from_tty)
1341 auto_load_info_scripts (pattern, from_tty, &extension_language_gdb);
1344 /* Implement 'info auto-load local-gdbinit'. */
1346 static void
1347 info_auto_load_local_gdbinit (const char *args, int from_tty)
1349 if (auto_load_local_gdbinit_pathname == NULL)
1350 printf_filtered (_("Local .gdbinit file was not found.\n"));
1351 else if (auto_load_local_gdbinit_loaded)
1352 printf_filtered (_("Local .gdbinit file \"%s\" has been loaded.\n"),
1353 auto_load_local_gdbinit_pathname);
1354 else
1355 printf_filtered (_("Local .gdbinit file \"%s\" has not been loaded.\n"),
1356 auto_load_local_gdbinit_pathname);
1359 /* Print an "unsupported script" warning if it has not already been printed.
1360 The script is in language LANGUAGE at offset OFFSET in section SECTION_NAME
1361 of OBJFILE. */
1363 static void
1364 maybe_print_unsupported_script_warning
1365 (struct auto_load_pspace_info *pspace_info,
1366 struct objfile *objfile, const struct extension_language_defn *language,
1367 const char *section_name, unsigned offset)
1369 if (!pspace_info->unsupported_script_warning_printed)
1371 warning (_("\
1372 Unsupported auto-load script at offset %u in section %s\n\
1373 of file %s.\n\
1374 Use `info auto-load %s-scripts [REGEXP]' to list them."),
1375 offset, section_name, objfile_name (objfile),
1376 ext_lang_name (language));
1377 pspace_info->unsupported_script_warning_printed = true;
1381 /* Return non-zero if SCRIPT_NOT_FOUND_WARNING_PRINTED of PSPACE_INFO was unset
1382 before calling this function. Always set SCRIPT_NOT_FOUND_WARNING_PRINTED
1383 of PSPACE_INFO. */
1385 static void
1386 maybe_print_script_not_found_warning
1387 (struct auto_load_pspace_info *pspace_info,
1388 struct objfile *objfile, const struct extension_language_defn *language,
1389 const char *section_name, unsigned offset)
1391 if (!pspace_info->script_not_found_warning_printed)
1393 warning (_("\
1394 Missing auto-load script at offset %u in section %s\n\
1395 of file %s.\n\
1396 Use `info auto-load %s-scripts [REGEXP]' to list them."),
1397 offset, section_name, objfile_name (objfile),
1398 ext_lang_name (language));
1399 pspace_info->script_not_found_warning_printed = true;
1403 /* The only valid "set auto-load" argument is off|0|no|disable. */
1405 static void
1406 set_auto_load_cmd (const char *args, int from_tty)
1408 struct cmd_list_element *list;
1409 size_t length;
1411 /* See parse_binary_operation in use by the sub-commands. */
1413 length = args ? strlen (args) : 0;
1415 while (length > 0 && (args[length - 1] == ' ' || args[length - 1] == '\t'))
1416 length--;
1418 if (length == 0 || (strncmp (args, "off", length) != 0
1419 && strncmp (args, "0", length) != 0
1420 && strncmp (args, "no", length) != 0
1421 && strncmp (args, "disable", length) != 0))
1422 error (_("Valid is only global 'set auto-load no'; "
1423 "otherwise check the auto-load sub-commands."));
1425 for (list = *auto_load_set_cmdlist_get (); list != NULL; list = list->next)
1426 if (list->var_type == var_boolean)
1428 gdb_assert (list->type == set_cmd);
1429 do_set_command (args, from_tty, list);
1433 /* Initialize "set auto-load " commands prefix and return it. */
1435 struct cmd_list_element **
1436 auto_load_set_cmdlist_get (void)
1438 static struct cmd_list_element *retval;
1440 if (retval == NULL)
1441 add_prefix_cmd ("auto-load", class_maintenance, set_auto_load_cmd, _("\
1442 Auto-loading specific settings.\n\
1443 Configure various auto-load-specific variables such as\n\
1444 automatic loading of Python scripts."),
1445 &retval, "set auto-load ",
1446 1/*allow-unknown*/, &setlist);
1448 return &retval;
1451 /* Command "show auto-load" displays summary of all the current
1452 "show auto-load " settings. */
1454 static void
1455 show_auto_load_cmd (const char *args, int from_tty)
1457 cmd_show_list (*auto_load_show_cmdlist_get (), from_tty, "");
1460 /* Initialize "show auto-load " commands prefix and return it. */
1462 struct cmd_list_element **
1463 auto_load_show_cmdlist_get (void)
1465 static struct cmd_list_element *retval;
1467 if (retval == NULL)
1468 add_prefix_cmd ("auto-load", class_maintenance, show_auto_load_cmd, _("\
1469 Show auto-loading specific settings.\n\
1470 Show configuration of various auto-load-specific variables such as\n\
1471 automatic loading of Python scripts."),
1472 &retval, "show auto-load ",
1473 0/*allow-unknown*/, &showlist);
1475 return &retval;
1478 /* Command "info auto-load" displays whether the various auto-load files have
1479 been loaded. This is reimplementation of cmd_show_list which inserts
1480 newlines at proper places. */
1482 static void
1483 info_auto_load_cmd (const char *args, int from_tty)
1485 struct cmd_list_element *list;
1486 struct ui_out *uiout = current_uiout;
1488 ui_out_emit_tuple tuple_emitter (uiout, "infolist");
1490 for (list = *auto_load_info_cmdlist_get (); list != NULL; list = list->next)
1492 ui_out_emit_tuple option_emitter (uiout, "option");
1494 gdb_assert (!list->prefixlist);
1495 gdb_assert (list->type == not_set_cmd);
1497 uiout->field_string ("name", list->name);
1498 uiout->text (": ");
1499 cmd_func (list, auto_load_info_scripts_pattern_nl, from_tty);
1503 /* Initialize "info auto-load " commands prefix and return it. */
1505 struct cmd_list_element **
1506 auto_load_info_cmdlist_get (void)
1508 static struct cmd_list_element *retval;
1510 if (retval == NULL)
1511 add_prefix_cmd ("auto-load", class_info, info_auto_load_cmd, _("\
1512 Print current status of auto-loaded files.\n\
1513 Print whether various files like Python scripts or .gdbinit files have been\n\
1514 found and/or loaded."),
1515 &retval, "info auto-load ",
1516 0/*allow-unknown*/, &infolist);
1518 return &retval;
1521 void
1522 _initialize_auto_load (void)
1524 struct cmd_list_element *cmd;
1525 char *scripts_directory_help, *gdb_name_help, *python_name_help;
1526 char *guile_name_help;
1527 const char *suffix;
1529 gdb::observers::new_objfile.attach (auto_load_new_objfile);
1531 add_setshow_boolean_cmd ("gdb-scripts", class_support,
1532 &auto_load_gdb_scripts, _("\
1533 Enable or disable auto-loading of canned sequences of commands scripts."), _("\
1534 Show whether auto-loading of canned sequences of commands scripts is enabled."),
1535 _("\
1536 If enabled, canned sequences of commands are loaded when the debugger reads\n\
1537 an executable or shared library.\n\
1538 This option has security implications for untrusted inferiors."),
1539 NULL, show_auto_load_gdb_scripts,
1540 auto_load_set_cmdlist_get (),
1541 auto_load_show_cmdlist_get ());
1543 add_cmd ("gdb-scripts", class_info, info_auto_load_gdb_scripts,
1544 _("Print the list of automatically loaded sequences of commands.\n\
1545 Usage: info auto-load gdb-scripts [REGEXP]"),
1546 auto_load_info_cmdlist_get ());
1548 add_setshow_boolean_cmd ("local-gdbinit", class_support,
1549 &auto_load_local_gdbinit, _("\
1550 Enable or disable auto-loading of .gdbinit script in current directory."), _("\
1551 Show whether auto-loading .gdbinit script in current directory is enabled."),
1552 _("\
1553 If enabled, canned sequences of commands are loaded when debugger starts\n\
1554 from .gdbinit file in current directory. Such files are deprecated,\n\
1555 use a script associated with inferior executable file instead.\n\
1556 This option has security implications for untrusted inferiors."),
1557 NULL, show_auto_load_local_gdbinit,
1558 auto_load_set_cmdlist_get (),
1559 auto_load_show_cmdlist_get ());
1561 add_cmd ("local-gdbinit", class_info, info_auto_load_local_gdbinit,
1562 _("Print whether current directory .gdbinit file has been loaded.\n\
1563 Usage: info auto-load local-gdbinit"),
1564 auto_load_info_cmdlist_get ());
1566 auto_load_dir = xstrdup (AUTO_LOAD_DIR);
1568 suffix = ext_lang_auto_load_suffix (get_ext_lang_defn (EXT_LANG_GDB));
1569 gdb_name_help
1570 = xstrprintf (_("\
1571 GDB scripts: OBJFILE%s\n"),
1572 suffix);
1573 python_name_help = NULL;
1574 #ifdef HAVE_PYTHON
1575 suffix = ext_lang_auto_load_suffix (get_ext_lang_defn (EXT_LANG_PYTHON));
1576 python_name_help
1577 = xstrprintf (_("\
1578 Python scripts: OBJFILE%s\n"),
1579 suffix);
1580 #endif
1581 guile_name_help = NULL;
1582 #ifdef HAVE_GUILE
1583 suffix = ext_lang_auto_load_suffix (get_ext_lang_defn (EXT_LANG_GUILE));
1584 guile_name_help
1585 = xstrprintf (_("\
1586 Guile scripts: OBJFILE%s\n"),
1587 suffix);
1588 #endif
1589 scripts_directory_help
1590 = xstrprintf (_("\
1591 Automatically loaded scripts are located in one of the directories listed\n\
1592 by this option.\n\
1594 Script names:\n\
1595 %s%s%s\
1597 This option is ignored for the kinds of scripts \
1598 having 'set auto-load ... off'.\n\
1599 Directories listed here need to be present also \
1600 in the 'set auto-load safe-path'\n\
1601 option."),
1602 gdb_name_help,
1603 python_name_help ? python_name_help : "",
1604 guile_name_help ? guile_name_help : "");
1606 add_setshow_optional_filename_cmd ("scripts-directory", class_support,
1607 &auto_load_dir, _("\
1608 Set the list of directories from which to load auto-loaded scripts."), _("\
1609 Show the list of directories from which to load auto-loaded scripts."),
1610 scripts_directory_help,
1611 set_auto_load_dir, show_auto_load_dir,
1612 auto_load_set_cmdlist_get (),
1613 auto_load_show_cmdlist_get ());
1614 xfree (scripts_directory_help);
1615 xfree (python_name_help);
1616 xfree (gdb_name_help);
1617 xfree (guile_name_help);
1619 auto_load_safe_path = xstrdup (AUTO_LOAD_SAFE_PATH);
1620 auto_load_safe_path_vec_update ();
1621 add_setshow_optional_filename_cmd ("safe-path", class_support,
1622 &auto_load_safe_path, _("\
1623 Set the list of files and directories that are safe for auto-loading."), _("\
1624 Show the list of files and directories that are safe for auto-loading."), _("\
1625 Various files loaded automatically for the 'set auto-load ...' options must\n\
1626 be located in one of the directories listed by this option. Warning will be\n\
1627 printed and file will not be used otherwise.\n\
1628 You can mix both directory and filename entries.\n\
1629 Setting this parameter to an empty list resets it to its default value.\n\
1630 Setting this parameter to '/' (without the quotes) allows any file\n\
1631 for the 'set auto-load ...' options. Each path entry can be also shell\n\
1632 wildcard pattern; '*' does not match directory separator.\n\
1633 This option is ignored for the kinds of files having 'set auto-load ... off'.\n\
1634 This option has security implications for untrusted inferiors."),
1635 set_auto_load_safe_path,
1636 show_auto_load_safe_path,
1637 auto_load_set_cmdlist_get (),
1638 auto_load_show_cmdlist_get ());
1639 gdb::observers::gdb_datadir_changed.attach (auto_load_gdb_datadir_changed);
1641 cmd = add_cmd ("add-auto-load-safe-path", class_support,
1642 add_auto_load_safe_path,
1643 _("Add entries to the list of directories from which it is safe "
1644 "to auto-load files.\n\
1645 See the commands 'set auto-load safe-path' and 'show auto-load safe-path' to\n\
1646 access the current full list setting."),
1647 &cmdlist);
1648 set_cmd_completer (cmd, filename_completer);
1650 cmd = add_cmd ("add-auto-load-scripts-directory", class_support,
1651 add_auto_load_dir,
1652 _("Add entries to the list of directories from which to load "
1653 "auto-loaded scripts.\n\
1654 See the commands 'set auto-load scripts-directory' and\n\
1655 'show auto-load scripts-directory' to access the current full list setting."),
1656 &cmdlist);
1657 set_cmd_completer (cmd, filename_completer);
1659 add_setshow_boolean_cmd ("auto-load", class_maintenance,
1660 &debug_auto_load, _("\
1661 Set auto-load verifications debugging."), _("\
1662 Show auto-load verifications debugging."), _("\
1663 When non-zero, debugging output for files of 'set auto-load ...'\n\
1664 is displayed."),
1665 NULL, show_debug_auto_load,
1666 &setdebuglist, &showdebuglist);