replace XZALLOC with XCNEW
[external/binutils.git] / gdb / auto-load.c
1 /* GDB routines for supporting auto-loaded scripts.
2
3    Copyright (C) 2012-2014 Free Software Foundation, Inc.
4
5    This file is part of GDB.
6
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.
11
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.
16
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/>.  */
19
20 #include "defs.h"
21 #include "auto-load.h"
22 #include "progspace.h"
23 #include "python/python.h"
24 #include "gdb_regex.h"
25 #include "ui-out.h"
26 #include "filenames.h"
27 #include "command.h"
28 #include "observer.h"
29 #include "objfiles.h"
30 #include "exceptions.h"
31 #include "cli/cli-script.h"
32 #include "gdbcmd.h"
33 #include "cli/cli-cmds.h"
34 #include "cli/cli-decode.h"
35 #include "cli/cli-setshow.h"
36 #include "gdb_vecs.h"
37 #include "readline/tilde.h"
38 #include "completer.h"
39 #include "fnmatch.h"
40 #include "top.h"
41 #include "filestuff.h"
42
43 /* The section to look in for auto-loaded scripts (in file formats that
44    support sections).
45    Each entry in this section is a record that begins with a leading byte
46    identifying the record type.
47    At the moment we only support one record type: A leading byte of 1,
48    followed by the path of a python script to load.  */
49 #define AUTO_SECTION_NAME ".debug_gdb_scripts"
50
51 /* The suffix of per-objfile scripts to auto-load as non-Python command files.
52    E.g. When the program loads libfoo.so, look for libfoo-gdb.gdb.  */
53 #define GDB_AUTO_FILE_NAME "-gdb.gdb"
54
55 static void source_gdb_script_for_objfile (struct objfile *objfile, FILE *file,
56                                            const char *filename);
57
58 /* Value of the 'set debug auto-load' configuration variable.  */
59 static int debug_auto_load = 0;
60
61 /* "show" command for the debug_auto_load configuration variable.  */
62
63 static void
64 show_debug_auto_load (struct ui_file *file, int from_tty,
65                       struct cmd_list_element *c, const char *value)
66 {
67   fprintf_filtered (file, _("Debugging output for files "
68                             "of 'set auto-load ...' is %s.\n"),
69                     value);
70 }
71
72 /* User-settable option to enable/disable auto-loading of GDB_AUTO_FILE_NAME
73    scripts:
74    set auto-load gdb-scripts on|off
75    This is true if we should auto-load associated scripts when an objfile
76    is opened, false otherwise.  */
77 static int auto_load_gdb_scripts = 1;
78
79 /* "show" command for the auto_load_gdb_scripts configuration variable.  */
80
81 static void
82 show_auto_load_gdb_scripts (struct ui_file *file, int from_tty,
83                             struct cmd_list_element *c, const char *value)
84 {
85   fprintf_filtered (file, _("Auto-loading of canned sequences of commands "
86                             "scripts is %s.\n"),
87                     value);
88 }
89
90 /* Return non-zero if auto-loading gdb scripts is enabled.  */
91
92 static int
93 auto_load_gdb_scripts_enabled (void)
94 {
95   return auto_load_gdb_scripts;
96 }
97
98 /* Internal-use flag to enable/disable auto-loading.
99    This is true if we should auto-load python code when an objfile is opened,
100    false otherwise.
101
102    Both auto_load_scripts && global_auto_load must be true to enable
103    auto-loading.
104
105    This flag exists to facilitate deferring auto-loading during start-up
106    until after ./.gdbinit has been read; it may augment the search directories
107    used to find the scripts.  */
108 int global_auto_load = 1;
109
110 /* Auto-load .gdbinit file from the current directory?  */
111 int auto_load_local_gdbinit = 1;
112
113 /* Absolute pathname to the current directory .gdbinit, if it exists.  */
114 char *auto_load_local_gdbinit_pathname = NULL;
115
116 /* Boolean value if AUTO_LOAD_LOCAL_GDBINIT_PATHNAME has been loaded.  */
117 int auto_load_local_gdbinit_loaded = 0;
118
119 /* "show" command for the auto_load_local_gdbinit configuration variable.  */
120
121 static void
122 show_auto_load_local_gdbinit (struct ui_file *file, int from_tty,
123                               struct cmd_list_element *c, const char *value)
124 {
125   fprintf_filtered (file, _("Auto-loading of .gdbinit script from current "
126                             "directory is %s.\n"),
127                     value);
128 }
129
130 /* Directory list from which to load auto-loaded scripts.  It is not checked
131    for absolute paths but they are strongly recommended.  It is initialized by
132    _initialize_auto_load.  */
133 static char *auto_load_dir;
134
135 /* "set" command for the auto_load_dir configuration variable.  */
136
137 static void
138 set_auto_load_dir (char *args, int from_tty, struct cmd_list_element *c)
139 {
140   /* Setting the variable to "" resets it to the compile time defaults.  */
141   if (auto_load_dir[0] == '\0')
142     {
143       xfree (auto_load_dir);
144       auto_load_dir = xstrdup (AUTO_LOAD_DIR);
145     }
146 }
147
148 /* "show" command for the auto_load_dir configuration variable.  */
149
150 static void
151 show_auto_load_dir (struct ui_file *file, int from_tty,
152                     struct cmd_list_element *c, const char *value)
153 {
154   fprintf_filtered (file, _("List of directories from which to load "
155                             "auto-loaded scripts is %s.\n"),
156                     value);
157 }
158
159 /* Directory list safe to hold auto-loaded files.  It is not checked for
160    absolute paths but they are strongly recommended.  It is initialized by
161    _initialize_auto_load.  */
162 static char *auto_load_safe_path;
163
164 /* Vector of directory elements of AUTO_LOAD_SAFE_PATH with each one normalized
165    by tilde_expand and possibly each entries has added its gdb_realpath
166    counterpart.  */
167 static VEC (char_ptr) *auto_load_safe_path_vec;
168
169 /* Expand $datadir and $debugdir in STRING according to the rules of
170    substitute_path_component.  Return vector from dirnames_to_char_ptr_vec,
171    this vector must be freed by free_char_ptr_vec by the caller.  */
172
173 static VEC (char_ptr) *
174 auto_load_expand_dir_vars (const char *string)
175 {
176   VEC (char_ptr) *dir_vec;
177   char *s;
178
179   s = xstrdup (string);
180   substitute_path_component (&s, "$datadir", gdb_datadir);
181   substitute_path_component (&s, "$debugdir", debug_file_directory);
182
183   if (debug_auto_load && strcmp (s, string) != 0)
184     fprintf_unfiltered (gdb_stdlog,
185                         _("auto-load: Expanded $-variables to \"%s\".\n"), s);
186
187   dir_vec = dirnames_to_char_ptr_vec (s);
188   xfree(s);
189
190   return dir_vec;
191 }
192
193 /* Update auto_load_safe_path_vec from current AUTO_LOAD_SAFE_PATH.  */
194
195 static void
196 auto_load_safe_path_vec_update (void)
197 {
198   unsigned len;
199   int ix;
200
201   if (debug_auto_load)
202     fprintf_unfiltered (gdb_stdlog,
203                         _("auto-load: Updating directories of \"%s\".\n"),
204                         auto_load_safe_path);
205
206   free_char_ptr_vec (auto_load_safe_path_vec);
207
208   auto_load_safe_path_vec = auto_load_expand_dir_vars (auto_load_safe_path);
209   len = VEC_length (char_ptr, auto_load_safe_path_vec);
210
211   /* Apply tilde_expand and gdb_realpath to each AUTO_LOAD_SAFE_PATH_VEC
212      element.  */
213   for (ix = 0; ix < len; ix++)
214     {
215       char *dir = VEC_index (char_ptr, auto_load_safe_path_vec, ix);
216       char *expanded = tilde_expand (dir);
217       char *real_path = gdb_realpath (expanded);
218
219       /* Ensure the current entry is at least tilde_expand-ed.  */
220       VEC_replace (char_ptr, auto_load_safe_path_vec, ix, expanded);
221
222       if (debug_auto_load)
223         {
224           if (strcmp (expanded, dir) == 0)
225             fprintf_unfiltered (gdb_stdlog,
226                                 _("auto-load: Using directory \"%s\".\n"),
227                                 expanded);
228           else
229             fprintf_unfiltered (gdb_stdlog,
230                                 _("auto-load: Resolved directory \"%s\" "
231                                   "as \"%s\".\n"),
232                                 dir, expanded);
233         }
234       xfree (dir);
235
236       /* If gdb_realpath returns a different content, append it.  */
237       if (strcmp (real_path, expanded) == 0)
238         xfree (real_path);
239       else
240         {
241           VEC_safe_push (char_ptr, auto_load_safe_path_vec, real_path);
242
243           if (debug_auto_load)
244             fprintf_unfiltered (gdb_stdlog,
245                                 _("auto-load: And canonicalized as \"%s\".\n"),
246                                 real_path);
247         }
248     }
249 }
250
251 /* Variable gdb_datadir has been set.  Update content depending on $datadir.  */
252
253 static void
254 auto_load_gdb_datadir_changed (void)
255 {
256   auto_load_safe_path_vec_update ();
257 }
258
259 /* "set" command for the auto_load_safe_path configuration variable.  */
260
261 static void
262 set_auto_load_safe_path (char *args, int from_tty, struct cmd_list_element *c)
263 {
264   /* Setting the variable to "" resets it to the compile time defaults.  */
265   if (auto_load_safe_path[0] == '\0')
266     {
267       xfree (auto_load_safe_path);
268       auto_load_safe_path = xstrdup (AUTO_LOAD_SAFE_PATH);
269     }
270
271   auto_load_safe_path_vec_update ();
272 }
273
274 /* "show" command for the auto_load_safe_path configuration variable.  */
275
276 static void
277 show_auto_load_safe_path (struct ui_file *file, int from_tty,
278                           struct cmd_list_element *c, const char *value)
279 {
280   const char *cs;
281
282   /* Check if user has entered either "/" or for example ":".
283      But while more complicate content like ":/foo" would still also
284      permit any location do not hide those.  */
285
286   for (cs = value; *cs && (*cs == DIRNAME_SEPARATOR || IS_DIR_SEPARATOR (*cs));
287        cs++);
288   if (*cs == 0)
289     fprintf_filtered (file, _("Auto-load files are safe to load from any "
290                               "directory.\n"));
291   else
292     fprintf_filtered (file, _("List of directories from which it is safe to "
293                               "auto-load files is %s.\n"),
294                       value);
295 }
296
297 /* "add-auto-load-safe-path" command for the auto_load_safe_path configuration
298    variable.  */
299
300 static void
301 add_auto_load_safe_path (char *args, int from_tty)
302 {
303   char *s;
304
305   if (args == NULL || *args == 0)
306     error (_("\
307 Directory argument required.\n\
308 Use 'set auto-load safe-path /' for disabling the auto-load safe-path security.\
309 "));
310
311   s = xstrprintf ("%s%c%s", auto_load_safe_path, DIRNAME_SEPARATOR, args);
312   xfree (auto_load_safe_path);
313   auto_load_safe_path = s;
314
315   auto_load_safe_path_vec_update ();
316 }
317
318 /* Implementation for filename_is_in_pattern overwriting the caller's FILENAME
319    and PATTERN.  */
320
321 static int
322 filename_is_in_pattern_1 (char *filename, char *pattern)
323 {
324   size_t pattern_len = strlen (pattern);
325   size_t filename_len = strlen (filename);
326
327   if (debug_auto_load)
328     fprintf_unfiltered (gdb_stdlog, _("auto-load: Matching file \"%s\" "
329                                       "to pattern \"%s\"\n"),
330                         filename, pattern);
331
332   /* Trim trailing slashes ("/") from PATTERN.  Even for "d:\" paths as
333      trailing slashes are trimmed also from FILENAME it still matches
334      correctly.  */
335   while (pattern_len && IS_DIR_SEPARATOR (pattern[pattern_len - 1]))
336     pattern_len--;
337   pattern[pattern_len] = '\0';
338
339   /* Ensure auto_load_safe_path "/" matches any FILENAME.  On MS-Windows
340      platform FILENAME even after gdb_realpath does not have to start with
341      IS_DIR_SEPARATOR character, such as the 'C:\x.exe' filename.  */
342   if (pattern_len == 0)
343     {
344       if (debug_auto_load)
345         fprintf_unfiltered (gdb_stdlog,
346                             _("auto-load: Matched - empty pattern\n"));
347       return 1;
348     }
349
350   for (;;)
351     {
352       /* Trim trailing slashes ("/").  PATTERN also has slashes trimmed the
353          same way so they will match.  */
354       while (filename_len && IS_DIR_SEPARATOR (filename[filename_len - 1]))
355         filename_len--;
356       filename[filename_len] = '\0';
357       if (filename_len == 0)
358         {
359           if (debug_auto_load)
360             fprintf_unfiltered (gdb_stdlog,
361                                 _("auto-load: Not matched - pattern \"%s\".\n"),
362                                 pattern);
363           return 0;
364         }
365
366       if (gdb_filename_fnmatch (pattern, filename, FNM_FILE_NAME | FNM_NOESCAPE)
367           == 0)
368         {
369           if (debug_auto_load)
370             fprintf_unfiltered (gdb_stdlog, _("auto-load: Matched - file "
371                                               "\"%s\" to pattern \"%s\".\n"),
372                                 filename, pattern);
373           return 1;
374         }
375
376       /* Trim trailing FILENAME component.  */
377       while (filename_len > 0 && !IS_DIR_SEPARATOR (filename[filename_len - 1]))
378         filename_len--;
379     }
380 }
381
382 /* Return 1 if FILENAME matches PATTERN or if FILENAME resides in
383    a subdirectory of a directory that matches PATTERN.  Return 0 otherwise.
384    gdb_realpath normalization is never done here.  */
385
386 static ATTRIBUTE_PURE int
387 filename_is_in_pattern (const char *filename, const char *pattern)
388 {
389   char *filename_copy, *pattern_copy;
390
391   filename_copy = alloca (strlen (filename) + 1);
392   strcpy (filename_copy, filename);
393   pattern_copy = alloca (strlen (pattern) + 1);
394   strcpy (pattern_copy, pattern);
395
396   return filename_is_in_pattern_1 (filename_copy, pattern_copy);
397 }
398
399 /* Return 1 if FILENAME belongs to one of directory components of
400    AUTO_LOAD_SAFE_PATH_VEC.  Return 0 otherwise.
401    auto_load_safe_path_vec_update is never called.
402    *FILENAME_REALP may be updated by gdb_realpath of FILENAME - it has to be
403    freed by the caller.  */
404
405 static int
406 filename_is_in_auto_load_safe_path_vec (const char *filename,
407                                         char **filename_realp)
408 {
409   char *pattern;
410   int ix;
411
412   for (ix = 0; VEC_iterate (char_ptr, auto_load_safe_path_vec, ix, pattern);
413        ++ix)
414     if (*filename_realp == NULL && filename_is_in_pattern (filename, pattern))
415       break;
416   
417   if (pattern == NULL)
418     {
419       if (*filename_realp == NULL)
420         {
421           *filename_realp = gdb_realpath (filename);
422           if (debug_auto_load && strcmp (*filename_realp, filename) != 0)
423             fprintf_unfiltered (gdb_stdlog,
424                                 _("auto-load: Resolved "
425                                   "file \"%s\" as \"%s\".\n"),
426                                 filename, *filename_realp);
427         }
428
429       if (strcmp (*filename_realp, filename) != 0)
430         for (ix = 0;
431              VEC_iterate (char_ptr, auto_load_safe_path_vec, ix, pattern); ++ix)
432           if (filename_is_in_pattern (*filename_realp, pattern))
433             break;
434     }
435
436   if (pattern != NULL)
437     {
438       if (debug_auto_load)
439         fprintf_unfiltered (gdb_stdlog, _("auto-load: File \"%s\" matches "
440                                           "directory \"%s\".\n"),
441                             filename, pattern);
442       return 1;
443     }
444
445   return 0;
446 }
447
448 /* Return 1 if FILENAME is located in one of the directories of
449    AUTO_LOAD_SAFE_PATH.  Otherwise call warning and return 0.  FILENAME does
450    not have to be an absolute path.
451
452    Existence of FILENAME is not checked.  Function will still give a warning
453    even if the caller would quietly skip non-existing file in unsafe
454    directory.  */
455
456 int
457 file_is_auto_load_safe (const char *filename, const char *debug_fmt, ...)
458 {
459   char *filename_real = NULL;
460   struct cleanup *back_to;
461   static int advice_printed = 0;
462
463   if (debug_auto_load)
464     {
465       va_list debug_args;
466
467       va_start (debug_args, debug_fmt);
468       vfprintf_unfiltered (gdb_stdlog, debug_fmt, debug_args);
469       va_end (debug_args);
470     }
471
472   back_to = make_cleanup (free_current_contents, &filename_real);
473
474   if (filename_is_in_auto_load_safe_path_vec (filename, &filename_real))
475     {
476       do_cleanups (back_to);
477       return 1;
478     }
479
480   auto_load_safe_path_vec_update ();
481   if (filename_is_in_auto_load_safe_path_vec (filename, &filename_real))
482     {
483       do_cleanups (back_to);
484       return 1;
485     }
486
487   warning (_("File \"%s\" auto-loading has been declined by your "
488              "`auto-load safe-path' set to \"%s\"."),
489            filename_real, auto_load_safe_path);
490
491   if (!advice_printed)
492     {
493       const char *homedir = getenv ("HOME");
494       char *homeinit;
495
496       if (homedir == NULL)
497         homedir = "$HOME";
498       homeinit = xstrprintf ("%s/%s", homedir, gdbinit);
499       make_cleanup (xfree, homeinit);
500
501       printf_filtered (_("\
502 To enable execution of this file add\n\
503 \tadd-auto-load-safe-path %s\n\
504 line to your configuration file \"%s\".\n\
505 To completely disable this security protection add\n\
506 \tset auto-load safe-path /\n\
507 line to your configuration file \"%s\".\n\
508 For more information about this security protection see the\n\
509 \"Auto-loading safe path\" section in the GDB manual.  E.g., run from the shell:\n\
510 \tinfo \"(gdb)Auto-loading safe path\"\n"),
511                        filename_real, homeinit, homeinit);
512       advice_printed = 1;
513     }
514
515   do_cleanups (back_to);
516   return 0;
517 }
518
519 /* Definition of script language for GDB canned sequences of commands.  */
520
521 static const struct script_language script_language_gdb =
522 {
523   "gdb",
524   GDB_AUTO_FILE_NAME,
525   auto_load_gdb_scripts_enabled,
526   source_gdb_script_for_objfile
527 };
528
529 static void
530 source_gdb_script_for_objfile (struct objfile *objfile, FILE *file,
531                                const char *filename)
532 {
533   volatile struct gdb_exception e;
534
535   TRY_CATCH (e, RETURN_MASK_ALL)
536     {
537       script_from_file (file, filename);
538     }
539   exception_print (gdb_stderr, e);
540 }
541
542 /* For scripts specified in .debug_gdb_scripts, multiple objfiles may load
543    the same script.  There's no point in loading the script multiple times,
544    and there can be a lot of objfiles and scripts, so we keep track of scripts
545    loaded this way.  */
546
547 struct auto_load_pspace_info
548 {
549   /* For each program space we keep track of loaded scripts.  */
550   struct htab *loaded_scripts;
551
552   /* Non-zero if we've issued the warning about an auto-load script not being
553      found.  We only want to issue this warning once.  */
554   int script_not_found_warning_printed;
555 };
556
557 /* Objects of this type are stored in the loaded script hash table.  */
558
559 struct loaded_script
560 {
561   /* Name as provided by the objfile.  */
562   const char *name;
563
564   /* Full path name or NULL if script wasn't found (or was otherwise
565      inaccessible).  */
566   const char *full_path;
567
568   /* Non-zero if this script has been loaded.  */
569   int loaded;
570
571   const struct script_language *language;
572 };
573
574 /* Per-program-space data key.  */
575 static const struct program_space_data *auto_load_pspace_data;
576
577 static void
578 auto_load_pspace_data_cleanup (struct program_space *pspace, void *arg)
579 {
580   struct auto_load_pspace_info *info = arg;
581
582   if (info->loaded_scripts)
583     htab_delete (info->loaded_scripts);
584   xfree (info);
585 }
586
587 /* Get the current autoload data.  If none is found yet, add it now.  This
588    function always returns a valid object.  */
589
590 static struct auto_load_pspace_info *
591 get_auto_load_pspace_data (struct program_space *pspace)
592 {
593   struct auto_load_pspace_info *info;
594
595   info = program_space_data (pspace, auto_load_pspace_data);
596   if (info == NULL)
597     {
598       info = XCNEW (struct auto_load_pspace_info);
599       set_program_space_data (pspace, auto_load_pspace_data, info);
600     }
601
602   return info;
603 }
604
605 /* Hash function for the loaded script hash.  */
606
607 static hashval_t
608 hash_loaded_script_entry (const void *data)
609 {
610   const struct loaded_script *e = data;
611
612   return htab_hash_string (e->name) ^ htab_hash_pointer (e->language);
613 }
614
615 /* Equality function for the loaded script hash.  */
616
617 static int
618 eq_loaded_script_entry (const void *a, const void *b)
619 {
620   const struct loaded_script *ea = a;
621   const struct loaded_script *eb = b;
622
623   return strcmp (ea->name, eb->name) == 0 && ea->language == eb->language;
624 }
625
626 /* Initialize the table to track loaded scripts.
627    Each entry is hashed by the full path name.  */
628
629 static void
630 init_loaded_scripts_info (struct auto_load_pspace_info *pspace_info)
631 {
632   /* Choose 31 as the starting size of the hash table, somewhat arbitrarily.
633      Space for each entry is obtained with one malloc so we can free them
634      easily.  */
635
636   pspace_info->loaded_scripts = htab_create (31,
637                                              hash_loaded_script_entry,
638                                              eq_loaded_script_entry,
639                                              xfree);
640
641   pspace_info->script_not_found_warning_printed = FALSE;
642 }
643
644 /* Wrapper on get_auto_load_pspace_data to also allocate the hash table
645    for loading scripts.  */
646
647 struct auto_load_pspace_info *
648 get_auto_load_pspace_data_for_loading (struct program_space *pspace)
649 {
650   struct auto_load_pspace_info *info;
651
652   info = get_auto_load_pspace_data (pspace);
653   if (info->loaded_scripts == NULL)
654     init_loaded_scripts_info (info);
655
656   return info;
657 }
658
659 /* Add script NAME in LANGUAGE to hash table of PSPACE_INFO.  LOADED 1 if the
660    script has been (is going to) be loaded, 0 otherwise (such as if it has not
661    been found).  FULL_PATH is NULL if the script wasn't found.  The result is
662    true if the script was already in the hash table.  */
663
664 int
665 maybe_add_script (struct auto_load_pspace_info *pspace_info, int loaded,
666                   const char *name, const char *full_path,
667                   const struct script_language *language)
668 {
669   struct htab *htab = pspace_info->loaded_scripts;
670   struct loaded_script **slot, entry;
671   int in_hash_table;
672
673   entry.name = name;
674   entry.language = language;
675   slot = (struct loaded_script **) htab_find_slot (htab, &entry, INSERT);
676   in_hash_table = *slot != NULL;
677
678   /* If this script is not in the hash table, add it.  */
679
680   if (! in_hash_table)
681     {
682       char *p;
683
684       /* Allocate all space in one chunk so it's easier to free.  */
685       *slot = xmalloc (sizeof (**slot)
686                        + strlen (name) + 1
687                        + (full_path != NULL ? (strlen (full_path) + 1) : 0));
688       p = ((char*) *slot) + sizeof (**slot);
689       strcpy (p, name);
690       (*slot)->name = p;
691       if (full_path != NULL)
692         {
693           p += strlen (p) + 1;
694           strcpy (p, full_path);
695           (*slot)->full_path = p;
696         }
697       else
698         (*slot)->full_path = NULL;
699       (*slot)->loaded = loaded;
700       (*slot)->language = language;
701     }
702
703   return in_hash_table;
704 }
705
706 /* Clear the table of loaded section scripts.  */
707
708 static void
709 clear_section_scripts (void)
710 {
711   struct program_space *pspace = current_program_space;
712   struct auto_load_pspace_info *info;
713
714   info = program_space_data (pspace, auto_load_pspace_data);
715   if (info != NULL && info->loaded_scripts != NULL)
716     {
717       htab_delete (info->loaded_scripts);
718       info->loaded_scripts = NULL;
719       info->script_not_found_warning_printed = FALSE;
720     }
721 }
722
723 /* Look for the auto-load script in LANGUAGE associated with OBJFILE where
724    OBJFILE's gdb_realpath is REALNAME and load it.  Return 1 if we found any
725    matching script, return 0 otherwise.  */
726
727 static int
728 auto_load_objfile_script_1 (struct objfile *objfile, const char *realname,
729                             const struct script_language *language)
730 {
731   char *filename, *debugfile;
732   int len, retval;
733   FILE *input;
734   struct cleanup *cleanups;
735
736   len = strlen (realname);
737   filename = xmalloc (len + strlen (language->suffix) + 1);
738   memcpy (filename, realname, len);
739   strcpy (filename + len, language->suffix);
740
741   cleanups = make_cleanup (xfree, filename);
742
743   input = gdb_fopen_cloexec (filename, "r");
744   debugfile = filename;
745   if (debug_auto_load)
746     fprintf_unfiltered (gdb_stdlog, _("auto-load: Attempted file \"%s\" %s.\n"),
747                         debugfile, input ? _("exists") : _("does not exist"));
748
749   if (!input)
750     {
751       VEC (char_ptr) *vec;
752       int ix;
753       char *dir;
754
755       /* Also try the same file in a subdirectory of gdb's data
756          directory.  */
757
758       vec = auto_load_expand_dir_vars (auto_load_dir);
759       make_cleanup_free_char_ptr_vec (vec);
760
761       if (debug_auto_load)
762         fprintf_unfiltered (gdb_stdlog, _("auto-load: Searching 'set auto-load "
763                                           "scripts-directory' path \"%s\".\n"),
764                             auto_load_dir);
765
766       for (ix = 0; VEC_iterate (char_ptr, vec, ix, dir); ++ix)
767         {
768           debugfile = xmalloc (strlen (dir) + strlen (filename) + 1);
769           strcpy (debugfile, dir);
770
771           /* FILENAME is absolute, so we don't need a "/" here.  */
772           strcat (debugfile, filename);
773
774           make_cleanup (xfree, debugfile);
775           input = gdb_fopen_cloexec (debugfile, "r");
776           if (debug_auto_load)
777             fprintf_unfiltered (gdb_stdlog, _("auto-load: Attempted file "
778                                               "\"%s\" %s.\n"),
779                                 debugfile,
780                                 input ? _("exists") : _("does not exist"));
781           if (input != NULL)
782             break;
783         }
784     }
785
786   if (input)
787     {
788       int is_safe;
789       struct auto_load_pspace_info *pspace_info;
790
791       make_cleanup_fclose (input);
792
793       is_safe
794         = file_is_auto_load_safe (filename,
795                                   _("auto-load: Loading %s script \"%s\""
796                                     " by extension for objfile \"%s\".\n"),
797                                   language->name, filename,
798                                   objfile_name (objfile));
799
800       /* Add this script to the hash table too so
801          "info auto-load ${lang}-scripts" can print it.  */
802       pspace_info
803         = get_auto_load_pspace_data_for_loading (current_program_space);
804       maybe_add_script (pspace_info, is_safe, filename, filename, language);
805
806       /* To preserve existing behaviour we don't check for whether the
807          script was already in the table, and always load it.
808          It's highly unlikely that we'd ever load it twice,
809          and these scripts are required to be idempotent under multiple
810          loads anyway.  */
811       if (is_safe)
812         language->source_script_for_objfile (objfile, input, debugfile);
813
814       retval = 1;
815     }
816   else
817     retval = 0;
818
819   do_cleanups (cleanups);
820   return retval;
821 }
822
823 /* Look for the auto-load script in LANGUAGE associated with OBJFILE and load
824    it.  */
825
826 static void
827 auto_load_objfile_script (struct objfile *objfile,
828                           const struct script_language *language)
829 {
830   char *realname;
831   struct cleanup *cleanups;
832
833   /* Skip this script if support has not been compiled in or
834      auto-loading it has been disabled.  */
835   if (language == NULL
836       || !language->auto_load_enabled ())
837     return;
838
839   realname = gdb_realpath (objfile_name (objfile));
840   cleanups = make_cleanup (xfree, realname);
841
842   if (!auto_load_objfile_script_1 (objfile, realname, language))
843     {
844       /* For Windows/DOS .exe executables, strip the .exe suffix, so that
845          FOO-gdb.gdb could be used for FOO.exe, and try again.  */
846
847       size_t len = strlen (realname);
848       const size_t lexe = sizeof (".exe") - 1;
849
850       if (len > lexe && strcasecmp (realname + len - lexe, ".exe") == 0)
851         {
852           len -= lexe;
853           realname[len] = '\0';
854           if (debug_auto_load)
855             fprintf_unfiltered (gdb_stdlog, _("auto-load: Stripped .exe suffix, "
856                                               "retrying with \"%s\".\n"),
857                                 realname);
858           auto_load_objfile_script_1 (objfile, realname, language);
859         }
860     }
861
862   do_cleanups (cleanups);
863 }
864
865 /* Load scripts specified in OBJFILE.
866    START,END delimit a buffer containing a list of nul-terminated
867    file names.
868    SECTION_NAME is used in error messages.
869
870    Scripts are found per normal "source -s" command processing.
871    First the script is looked for in $cwd.  If not found there the
872    source search path is used.
873
874    The section contains a list of path names of script files to load.
875    Each path is null-terminated.  */
876
877 static void
878 source_section_scripts (struct objfile *objfile, const char *section_name,
879                         const char *start, const char *end)
880 {
881   const char *p;
882   struct auto_load_pspace_info *pspace_info;
883
884   pspace_info = get_auto_load_pspace_data_for_loading (current_program_space);
885
886   for (p = start; p < end; ++p)
887     {
888       const char *file;
889       FILE *stream;
890       char *full_path;
891       int opened, in_hash_table;
892       struct cleanup *back_to;
893       /* At the moment we only support python scripts in .debug_gdb_scripts,
894          but that can change.  */
895       const struct script_language *language = gdbpy_script_language_defn ();
896
897       if (*p != 1)
898         {
899           warning (_("Invalid entry in %s section"), section_name);
900           /* We could try various heuristics to find the next valid entry,
901              but it's safer to just punt.  */
902           break;
903         }
904       file = ++p;
905
906       while (p < end && *p != '\0')
907         ++p;
908       if (p == end)
909         {
910           char *buf = alloca (p - file + 1);
911
912           memcpy (buf, file, p - file);
913           buf[p - file] = '\0';
914           warning (_("Non-null-terminated path in %s: %s"),
915                    section_name, buf);
916           /* Don't load it.  */
917           break;
918         }
919       if (p == file)
920         {
921           warning (_("Empty path in %s"), section_name);
922           continue;
923         }
924
925       /* Skip this script if support has not been compiled in or
926          auto-loading it has been disabled.  */
927       if (language == NULL
928           || !language->auto_load_enabled ())
929         {
930           /* No message is printed, just skip it.  */
931           continue;
932         }
933
934       opened = find_and_open_script (file, 1 /*search_path*/,
935                                      &stream, &full_path);
936
937       back_to = make_cleanup (null_cleanup, NULL);
938       if (opened)
939         {
940           make_cleanup_fclose (stream);
941           make_cleanup (xfree, full_path);
942
943           if (!file_is_auto_load_safe (full_path,
944                                        _("auto-load: Loading %s script "
945                                          "\"%s\" from section \"%s\" of "
946                                          "objfile \"%s\".\n"),
947                                        language->name, full_path, section_name,
948                                        objfile_name (objfile)))
949             opened = 0;
950         }
951       else
952         {
953           full_path = NULL;
954
955           /* If one script isn't found it's not uncommon for more to not be
956              found either.  We don't want to print a message for each script,
957              too much noise.  Instead, we print the warning once and tell the
958              user how to find the list of scripts that weren't loaded.
959              We don't throw an error, the program is still debuggable.
960
961              IWBN if complaints.c were more general-purpose.  */
962
963           if (script_not_found_warning_print (pspace_info))
964             warning (_("Missing auto-load scripts referenced in section %s\n\
965 of file %s\n\
966 Use `info auto-load %s-scripts [REGEXP]' to list them."),
967                      section_name, objfile_name (objfile), language->name);
968         }
969
970       in_hash_table = maybe_add_script (pspace_info, opened, file, full_path,
971                                         language);
972
973       /* If this file is not currently loaded, load it.  */
974       if (opened && !in_hash_table)
975         {
976           gdb_assert (language->source_script_for_objfile != NULL);
977           language->source_script_for_objfile (objfile, stream, full_path);
978         }
979
980       do_cleanups (back_to);
981     }
982 }
983
984 /* Load scripts specified in section SECTION_NAME of OBJFILE.  */
985
986 static void
987 auto_load_section_scripts (struct objfile *objfile, const char *section_name)
988 {
989   bfd *abfd = objfile->obfd;
990   asection *scripts_sect;
991   bfd_byte *data = NULL;
992
993   scripts_sect = bfd_get_section_by_name (abfd, section_name);
994   if (scripts_sect == NULL)
995     return;
996
997   if (!bfd_get_full_section_contents (abfd, scripts_sect, &data))
998     warning (_("Couldn't read %s section of %s"),
999              section_name, bfd_get_filename (abfd));
1000   else
1001     {
1002       struct cleanup *cleanups;
1003       char *p = (char *) data;
1004
1005       cleanups = make_cleanup (xfree, p);
1006       source_section_scripts (objfile, section_name, p,
1007                               p + bfd_get_section_size (scripts_sect));
1008       do_cleanups (cleanups);
1009     }
1010 }
1011
1012 /* Load any auto-loaded scripts for OBJFILE.  */
1013
1014 void
1015 load_auto_scripts_for_objfile (struct objfile *objfile)
1016 {
1017   /* Return immediately if auto-loading has been globally disabled.
1018      This is to handle sequencing of operations during gdb startup.
1019      Also return immediately if OBJFILE is not actually a file.  */
1020   if (!global_auto_load || (objfile->flags & OBJF_NOT_FILENAME) != 0)
1021     return;
1022
1023   /* Load any scripts for this objfile. e.g. foo-gdb.gdb, foo-gdb.py.  */
1024   auto_load_objfile_script (objfile, &script_language_gdb);
1025   auto_load_objfile_script (objfile, gdbpy_script_language_defn ());
1026
1027   /* Load any scripts mentioned in AUTO_SECTION_NAME (.debug_gdb_scripts).  */
1028   auto_load_section_scripts (objfile, AUTO_SECTION_NAME);
1029 }
1030
1031 /* This is a new_objfile observer callback to auto-load scripts.
1032
1033    Two flavors of auto-loaded scripts are supported.
1034    1) based on the path to the objfile
1035    2) from .debug_gdb_scripts section  */
1036
1037 static void
1038 auto_load_new_objfile (struct objfile *objfile)
1039 {
1040   if (!objfile)
1041     {
1042       /* OBJFILE is NULL when loading a new "main" symbol-file.  */
1043       clear_section_scripts ();
1044       return;
1045     }
1046
1047   load_auto_scripts_for_objfile (objfile);
1048 }
1049
1050 /* Collect scripts to be printed in a vec.  */
1051
1052 typedef struct loaded_script *loaded_script_ptr;
1053 DEF_VEC_P (loaded_script_ptr);
1054
1055 struct collect_matching_scripts_data
1056 {
1057   VEC (loaded_script_ptr) **scripts_p;
1058
1059   const struct script_language *language;
1060 };
1061
1062 /* Traversal function for htab_traverse.
1063    Collect the entry if it matches the regexp.  */
1064
1065 static int
1066 collect_matching_scripts (void **slot, void *info)
1067 {
1068   struct loaded_script *script = *slot;
1069   struct collect_matching_scripts_data *data = info;
1070
1071   if (script->language == data->language && re_exec (script->name))
1072     VEC_safe_push (loaded_script_ptr, *data->scripts_p, script);
1073
1074   return 1;
1075 }
1076
1077 /* Print SCRIPT.  */
1078
1079 static void
1080 print_script (struct loaded_script *script)
1081 {
1082   struct ui_out *uiout = current_uiout;
1083   struct cleanup *chain;
1084
1085   chain = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
1086
1087   ui_out_field_string (uiout, "loaded", script->loaded ? "Yes" : "No");
1088   ui_out_field_string (uiout, "script", script->name);
1089   ui_out_text (uiout, "\n");
1090
1091   /* If the name isn't the full path, print it too.  */
1092   if (script->full_path != NULL
1093       && strcmp (script->name, script->full_path) != 0)
1094     {
1095       ui_out_text (uiout, "\tfull name: ");
1096       ui_out_field_string (uiout, "full_path", script->full_path);
1097       ui_out_text (uiout, "\n");
1098     }
1099
1100   do_cleanups (chain);
1101 }
1102
1103 /* Helper for info_auto_load_scripts to sort the scripts by name.  */
1104
1105 static int
1106 sort_scripts_by_name (const void *ap, const void *bp)
1107 {
1108   const struct loaded_script *a = *(const struct loaded_script **) ap;
1109   const struct loaded_script *b = *(const struct loaded_script **) bp;
1110
1111   return FILENAME_CMP (a->name, b->name);
1112 }
1113
1114 /* Special internal GDB value of auto_load_info_scripts's PATTERN identify
1115    the "info auto-load XXX" command has been executed through the general
1116    "info auto-load" invocation.  Extra newline will be printed if needed.  */
1117 char auto_load_info_scripts_pattern_nl[] = "";
1118
1119 /* Implementation for "info auto-load gdb-scripts"
1120    (and "info auto-load python-scripts").  List scripts in LANGUAGE matching
1121    PATTERN.  FROM_TTY is the usual GDB boolean for user interactivity.  */
1122
1123 void
1124 auto_load_info_scripts (char *pattern, int from_tty,
1125                         const struct script_language *language)
1126 {
1127   struct ui_out *uiout = current_uiout;
1128   struct auto_load_pspace_info *pspace_info;
1129   struct cleanup *script_chain;
1130   VEC (loaded_script_ptr) *scripts;
1131   int nr_scripts;
1132
1133   dont_repeat ();
1134
1135   pspace_info = get_auto_load_pspace_data (current_program_space);
1136
1137   if (pattern && *pattern)
1138     {
1139       char *re_err = re_comp (pattern);
1140
1141       if (re_err)
1142         error (_("Invalid regexp: %s"), re_err);
1143     }
1144   else
1145     {
1146       re_comp ("");
1147     }
1148
1149   /* We need to know the number of rows before we build the table.
1150      Plus we want to sort the scripts by name.
1151      So first traverse the hash table collecting the matching scripts.  */
1152
1153   scripts = VEC_alloc (loaded_script_ptr, 10);
1154   script_chain = make_cleanup (VEC_cleanup (loaded_script_ptr), &scripts);
1155
1156   if (pspace_info != NULL && pspace_info->loaded_scripts != NULL)
1157     {
1158       struct collect_matching_scripts_data data = { &scripts, language };
1159
1160       /* Pass a pointer to scripts as VEC_safe_push can realloc space.  */
1161       htab_traverse_noresize (pspace_info->loaded_scripts,
1162                               collect_matching_scripts, &data);
1163     }
1164
1165   nr_scripts = VEC_length (loaded_script_ptr, scripts);
1166
1167   /* Table header shifted right by preceding "gdb-scripts:  " would not match
1168      its columns.  */
1169   if (nr_scripts > 0 && pattern == auto_load_info_scripts_pattern_nl)
1170     ui_out_text (uiout, "\n");
1171
1172   make_cleanup_ui_out_table_begin_end (uiout, 2, nr_scripts,
1173                                        "AutoLoadedScriptsTable");
1174
1175   ui_out_table_header (uiout, 7, ui_left, "loaded", "Loaded");
1176   ui_out_table_header (uiout, 70, ui_left, "script", "Script");
1177   ui_out_table_body (uiout);
1178
1179   if (nr_scripts > 0)
1180     {
1181       int i;
1182       loaded_script_ptr script;
1183
1184       qsort (VEC_address (loaded_script_ptr, scripts),
1185              VEC_length (loaded_script_ptr, scripts),
1186              sizeof (loaded_script_ptr), sort_scripts_by_name);
1187       for (i = 0; VEC_iterate (loaded_script_ptr, scripts, i, script); ++i)
1188         print_script (script);
1189     }
1190
1191   do_cleanups (script_chain);
1192
1193   if (nr_scripts == 0)
1194     {
1195       if (pattern && *pattern)
1196         ui_out_message (uiout, 0, "No auto-load scripts matching %s.\n",
1197                         pattern);
1198       else
1199         ui_out_message (uiout, 0, "No auto-load scripts.\n");
1200     }
1201 }
1202
1203 /* Wrapper for "info auto-load gdb-scripts".  */
1204
1205 static void
1206 info_auto_load_gdb_scripts (char *pattern, int from_tty)
1207 {
1208   auto_load_info_scripts (pattern, from_tty, &script_language_gdb);
1209 }
1210
1211 /* Implement 'info auto-load local-gdbinit'.  */
1212
1213 static void
1214 info_auto_load_local_gdbinit (char *args, int from_tty)
1215 {
1216   if (auto_load_local_gdbinit_pathname == NULL)
1217     printf_filtered (_("Local .gdbinit file was not found.\n"));
1218   else if (auto_load_local_gdbinit_loaded)
1219     printf_filtered (_("Local .gdbinit file \"%s\" has been loaded.\n"),
1220                      auto_load_local_gdbinit_pathname);
1221   else
1222     printf_filtered (_("Local .gdbinit file \"%s\" has not been loaded.\n"),
1223                      auto_load_local_gdbinit_pathname);
1224 }
1225
1226 /* Return non-zero if SCRIPT_NOT_FOUND_WARNING_PRINTED of PSPACE_INFO was unset
1227    before calling this function.  Always set SCRIPT_NOT_FOUND_WARNING_PRINTED
1228    of PSPACE_INFO.  */
1229
1230 int
1231 script_not_found_warning_print (struct auto_load_pspace_info *pspace_info)
1232 {
1233   int retval = !pspace_info->script_not_found_warning_printed;
1234
1235   pspace_info->script_not_found_warning_printed = 1;
1236
1237   return retval;
1238 }
1239
1240 /* The only valid "set auto-load" argument is off|0|no|disable.  */
1241
1242 static void
1243 set_auto_load_cmd (char *args, int from_tty)
1244 {
1245   struct cmd_list_element *list;
1246   size_t length;
1247
1248   /* See parse_binary_operation in use by the sub-commands.  */
1249
1250   length = args ? strlen (args) : 0;
1251
1252   while (length > 0 && (args[length - 1] == ' ' || args[length - 1] == '\t'))
1253     length--;
1254
1255   if (length == 0 || (strncmp (args, "off", length) != 0
1256                       && strncmp (args, "0", length) != 0
1257                       && strncmp (args, "no", length) != 0
1258                       && strncmp (args, "disable", length) != 0))
1259     error (_("Valid is only global 'set auto-load no'; "
1260              "otherwise check the auto-load sub-commands."));
1261
1262   for (list = *auto_load_set_cmdlist_get (); list != NULL; list = list->next)
1263     if (list->var_type == var_boolean)
1264       {
1265         gdb_assert (list->type == set_cmd);
1266         do_set_command (args, from_tty, list);
1267       }
1268 }
1269
1270 /* Initialize "set auto-load " commands prefix and return it.  */
1271
1272 struct cmd_list_element **
1273 auto_load_set_cmdlist_get (void)
1274 {
1275   static struct cmd_list_element *retval;
1276
1277   if (retval == NULL)
1278     add_prefix_cmd ("auto-load", class_maintenance, set_auto_load_cmd, _("\
1279 Auto-loading specific settings.\n\
1280 Configure various auto-load-specific variables such as\n\
1281 automatic loading of Python scripts."),
1282                     &retval, "set auto-load ",
1283                     1/*allow-unknown*/, &setlist);
1284
1285   return &retval;
1286 }
1287
1288 /* Command "show auto-load" displays summary of all the current
1289    "show auto-load " settings.  */
1290
1291 static void
1292 show_auto_load_cmd (char *args, int from_tty)
1293 {
1294   cmd_show_list (*auto_load_show_cmdlist_get (), from_tty, "");
1295 }
1296
1297 /* Initialize "show auto-load " commands prefix and return it.  */
1298
1299 struct cmd_list_element **
1300 auto_load_show_cmdlist_get (void)
1301 {
1302   static struct cmd_list_element *retval;
1303
1304   if (retval == NULL)
1305     add_prefix_cmd ("auto-load", class_maintenance, show_auto_load_cmd, _("\
1306 Show auto-loading specific settings.\n\
1307 Show configuration of various auto-load-specific variables such as\n\
1308 automatic loading of Python scripts."),
1309                     &retval, "show auto-load ",
1310                     0/*allow-unknown*/, &showlist);
1311
1312   return &retval;
1313 }
1314
1315 /* Command "info auto-load" displays whether the various auto-load files have
1316    been loaded.  This is reimplementation of cmd_show_list which inserts
1317    newlines at proper places.  */
1318
1319 static void
1320 info_auto_load_cmd (char *args, int from_tty)
1321 {
1322   struct cmd_list_element *list;
1323   struct cleanup *infolist_chain;
1324   struct ui_out *uiout = current_uiout;
1325
1326   infolist_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "infolist");
1327
1328   for (list = *auto_load_info_cmdlist_get (); list != NULL; list = list->next)
1329     {
1330       struct cleanup *option_chain
1331         = make_cleanup_ui_out_tuple_begin_end (uiout, "option");
1332
1333       gdb_assert (!list->prefixlist);
1334       gdb_assert (list->type == not_set_cmd);
1335
1336       ui_out_field_string (uiout, "name", list->name);
1337       ui_out_text (uiout, ":  ");
1338       cmd_func (list, auto_load_info_scripts_pattern_nl, from_tty);
1339
1340       /* Close the tuple.  */
1341       do_cleanups (option_chain);
1342     }
1343
1344   /* Close the tuple.  */
1345   do_cleanups (infolist_chain);
1346 }
1347
1348 /* Initialize "info auto-load " commands prefix and return it.  */
1349
1350 struct cmd_list_element **
1351 auto_load_info_cmdlist_get (void)
1352 {
1353   static struct cmd_list_element *retval;
1354
1355   if (retval == NULL)
1356     add_prefix_cmd ("auto-load", class_info, info_auto_load_cmd, _("\
1357 Print current status of auto-loaded files.\n\
1358 Print whether various files like Python scripts or .gdbinit files have been\n\
1359 found and/or loaded."),
1360                     &retval, "info auto-load ",
1361                     0/*allow-unknown*/, &infolist);
1362
1363   return &retval;
1364 }
1365
1366 void _initialize_auto_load (void);
1367
1368 void
1369 _initialize_auto_load (void)
1370 {
1371   struct cmd_list_element *cmd;
1372   char *scripts_directory_help;
1373
1374   auto_load_pspace_data
1375     = register_program_space_data_with_cleanup (NULL,
1376                                                 auto_load_pspace_data_cleanup);
1377
1378   observer_attach_new_objfile (auto_load_new_objfile);
1379
1380   add_setshow_boolean_cmd ("gdb-scripts", class_support,
1381                            &auto_load_gdb_scripts, _("\
1382 Enable or disable auto-loading of canned sequences of commands scripts."), _("\
1383 Show whether auto-loading of canned sequences of commands scripts is enabled."),
1384                            _("\
1385 If enabled, canned sequences of commands are loaded when the debugger reads\n\
1386 an executable or shared library.\n\
1387 This options has security implications for untrusted inferiors."),
1388                            NULL, show_auto_load_gdb_scripts,
1389                            auto_load_set_cmdlist_get (),
1390                            auto_load_show_cmdlist_get ());
1391
1392   add_cmd ("gdb-scripts", class_info, info_auto_load_gdb_scripts,
1393            _("Print the list of automatically loaded sequences of commands.\n\
1394 Usage: info auto-load gdb-scripts [REGEXP]"),
1395            auto_load_info_cmdlist_get ());
1396
1397   add_setshow_boolean_cmd ("local-gdbinit", class_support,
1398                            &auto_load_local_gdbinit, _("\
1399 Enable or disable auto-loading of .gdbinit script in current directory."), _("\
1400 Show whether auto-loading .gdbinit script in current directory is enabled."),
1401                            _("\
1402 If enabled, canned sequences of commands are loaded when debugger starts\n\
1403 from .gdbinit file in current directory.  Such files are deprecated,\n\
1404 use a script associated with inferior executable file instead.\n\
1405 This options has security implications for untrusted inferiors."),
1406                            NULL, show_auto_load_local_gdbinit,
1407                            auto_load_set_cmdlist_get (),
1408                            auto_load_show_cmdlist_get ());
1409
1410   add_cmd ("local-gdbinit", class_info, info_auto_load_local_gdbinit,
1411            _("Print whether current directory .gdbinit file has been loaded.\n\
1412 Usage: info auto-load local-gdbinit"),
1413            auto_load_info_cmdlist_get ());
1414
1415   auto_load_dir = xstrdup (AUTO_LOAD_DIR);
1416   scripts_directory_help = xstrprintf (
1417 #ifdef HAVE_PYTHON
1418                                        _("\
1419 Automatically loaded Python scripts (named OBJFILE%s) and GDB scripts\n\
1420 (named OBJFILE%s) are located in one of the directories listed by this\n\
1421 option.\n\
1422 %s"),
1423                                        GDBPY_AUTO_FILE_NAME,
1424 #else
1425                                        _("\
1426 Automatically loaded GDB scripts (named OBJFILE%s) are located in one\n\
1427 of the directories listed by this option.\n\
1428 %s"),
1429 #endif
1430                                        GDB_AUTO_FILE_NAME,
1431                                        _("\
1432 This option is ignored for the kinds of scripts \
1433 having 'set auto-load ... off'.\n\
1434 Directories listed here need to be present also \
1435 in the 'set auto-load safe-path'\n\
1436 option."));
1437   add_setshow_optional_filename_cmd ("scripts-directory", class_support,
1438                                      &auto_load_dir, _("\
1439 Set the list of directories from which to load auto-loaded scripts."), _("\
1440 Show the list of directories from which to load auto-loaded scripts."),
1441                                      scripts_directory_help,
1442                                      set_auto_load_dir, show_auto_load_dir,
1443                                      auto_load_set_cmdlist_get (),
1444                                      auto_load_show_cmdlist_get ());
1445   xfree (scripts_directory_help);
1446
1447   auto_load_safe_path = xstrdup (AUTO_LOAD_SAFE_PATH);
1448   auto_load_safe_path_vec_update ();
1449   add_setshow_optional_filename_cmd ("safe-path", class_support,
1450                                      &auto_load_safe_path, _("\
1451 Set the list of files and directories that are safe for auto-loading."), _("\
1452 Show the list of files and directories that are safe for auto-loading."), _("\
1453 Various files loaded automatically for the 'set auto-load ...' options must\n\
1454 be located in one of the directories listed by this option.  Warning will be\n\
1455 printed and file will not be used otherwise.\n\
1456 You can mix both directory and filename entries.\n\
1457 Setting this parameter to an empty list resets it to its default value.\n\
1458 Setting this parameter to '/' (without the quotes) allows any file\n\
1459 for the 'set auto-load ...' options.  Each path entry can be also shell\n\
1460 wildcard pattern; '*' does not match directory separator.\n\
1461 This option is ignored for the kinds of files having 'set auto-load ... off'.\n\
1462 This options has security implications for untrusted inferiors."),
1463                                      set_auto_load_safe_path,
1464                                      show_auto_load_safe_path,
1465                                      auto_load_set_cmdlist_get (),
1466                                      auto_load_show_cmdlist_get ());
1467   observer_attach_gdb_datadir_changed (auto_load_gdb_datadir_changed);
1468
1469   cmd = add_cmd ("add-auto-load-safe-path", class_support,
1470                  add_auto_load_safe_path,
1471                  _("Add entries to the list of directories from which it is safe "
1472                    "to auto-load files.\n\
1473 See the commands 'set auto-load safe-path' and 'show auto-load safe-path' to\n\
1474 access the current full list setting."),
1475                  &cmdlist);
1476   set_cmd_completer (cmd, filename_completer);
1477
1478   add_setshow_boolean_cmd ("auto-load", class_maintenance,
1479                            &debug_auto_load, _("\
1480 Set auto-load verifications debugging."), _("\
1481 Show auto-load verifications debugging."), _("\
1482 When non-zero, debugging output for files of 'set auto-load ...'\n\
1483 is displayed."),
1484                             NULL, show_debug_auto_load,
1485                             &setdebuglist, &showdebuglist);
1486 }