gdb/
[external/binutils.git] / gdb / auto-load.c
1 /* GDB routines for supporting auto-loaded scripts.
2
3    Copyright (C) 2012 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-decode.h"
34 #include "cli/cli-setshow.h"
35 #include "gdb_vecs.h"
36 #include "readline/tilde.h"
37 #include "completer.h"
38 #include "observer.h"
39
40 /* The suffix of per-objfile scripts to auto-load as non-Python command files.
41    E.g. When the program loads libfoo.so, look for libfoo-gdb.gdb.  */
42 #define GDB_AUTO_FILE_NAME "-gdb.gdb"
43
44 static void source_gdb_script_for_objfile (struct objfile *objfile, FILE *file,
45                                            const char *filename);
46
47 /* Value of the 'set debug auto-load' configuration variable.  */
48 static int debug_auto_load = 0;
49
50 /* "show" command for the debug_auto_load configuration variable.  */
51
52 static void
53 show_debug_auto_load (struct ui_file *file, int from_tty,
54                       struct cmd_list_element *c, const char *value)
55 {
56   fprintf_filtered (file, _("Debugging output for files "
57                             "of 'set auto-load ...' is %s.\n"),
58                     value);
59 }
60
61 /* User-settable option to enable/disable auto-loading of GDB_AUTO_FILE_NAME
62    scripts:
63    set auto-load gdb-scripts on|off
64    This is true if we should auto-load associated scripts when an objfile
65    is opened, false otherwise.  */
66 static int auto_load_gdb_scripts = 1;
67
68 /* "show" command for the auto_load_gdb_scripts configuration variable.  */
69
70 static void
71 show_auto_load_gdb_scripts (struct ui_file *file, int from_tty,
72                             struct cmd_list_element *c, const char *value)
73 {
74   fprintf_filtered (file, _("Auto-loading of canned sequences of commands "
75                             "scripts is %s.\n"),
76                     value);
77 }
78
79 /* Internal-use flag to enable/disable auto-loading.
80    This is true if we should auto-load python code when an objfile is opened,
81    false otherwise.
82
83    Both auto_load_scripts && global_auto_load must be true to enable
84    auto-loading.
85
86    This flag exists to facilitate deferring auto-loading during start-up
87    until after ./.gdbinit has been read; it may augment the search directories
88    used to find the scripts.  */
89 int global_auto_load = 1;
90
91 /* Auto-load .gdbinit file from the current directory?  */
92 int auto_load_local_gdbinit = 1;
93
94 /* Absolute pathname to the current directory .gdbinit, if it exists.  */
95 char *auto_load_local_gdbinit_pathname = NULL;
96
97 /* Boolean value if AUTO_LOAD_LOCAL_GDBINIT_PATHNAME has been loaded.  */
98 int auto_load_local_gdbinit_loaded = 0;
99
100 /* "show" command for the auto_load_local_gdbinit configuration variable.  */
101
102 static void
103 show_auto_load_local_gdbinit (struct ui_file *file, int from_tty,
104                               struct cmd_list_element *c, const char *value)
105 {
106   fprintf_filtered (file, _("Auto-loading of .gdbinit script from current "
107                             "directory is %s.\n"),
108                     value);
109 }
110
111 /* Directory list safe to hold auto-loaded files.  It is not checked for
112    absolute paths but they are strongly recommended.  It is initialized by
113    _initialize_auto_load.  */
114 static char *auto_load_safe_path;
115
116 /* Vector of directory elements of AUTO_LOAD_SAFE_PATH with each one normalized
117    by tilde_expand and possibly each entries has added its gdb_realpath
118    counterpart.  */
119 static VEC (char_ptr) *auto_load_safe_path_vec;
120
121 /* Update auto_load_safe_path_vec from current AUTO_LOAD_SAFE_PATH.  */
122
123 static void
124 auto_load_safe_path_vec_update (void)
125 {
126   VEC (char_ptr) *dir_vec = NULL;
127   unsigned len;
128   int ix;
129
130   if (debug_auto_load)
131     fprintf_unfiltered (gdb_stdlog,
132                         _("auto-load: Updating directories of \"%s\".\n"),
133                         auto_load_safe_path);
134
135   free_char_ptr_vec (auto_load_safe_path_vec);
136
137   auto_load_safe_path_vec = dirnames_to_char_ptr_vec (auto_load_safe_path);
138   len = VEC_length (char_ptr, auto_load_safe_path_vec);
139
140   /* Apply tilde_expand and gdb_realpath to each AUTO_LOAD_SAFE_PATH_VEC
141      element.  */
142   for (ix = 0; ix < len; ix++)
143     {
144       char *dir = VEC_index (char_ptr, auto_load_safe_path_vec, ix);
145       char *ddir_subst, *expanded, *real_path;
146
147       ddir_subst = xstrdup (dir);
148       substitute_path_component (&ddir_subst, "$ddir", gdb_datadir);
149       expanded = tilde_expand (ddir_subst);
150       xfree (ddir_subst);
151       real_path = gdb_realpath (expanded);
152
153       /* Ensure the current entry is at least a valid path (therefore
154          $ddir-expanded and tilde-expanded).  */
155       VEC_replace (char_ptr, auto_load_safe_path_vec, ix, expanded);
156
157       if (debug_auto_load)
158         {
159           if (strcmp (expanded, dir) == 0)
160             fprintf_unfiltered (gdb_stdlog,
161                                 _("auto-load: Using directory \"%s\".\n"),
162                                 expanded);
163           else
164             fprintf_unfiltered (gdb_stdlog,
165                                 _("auto-load: Resolved directory \"%s\" "
166                                   "as \"%s\".\n"),
167                                 dir, expanded);
168         }
169       xfree (dir);
170
171       /* If gdb_realpath returns a different content, append it.  */
172       if (strcmp (real_path, expanded) == 0)
173         xfree (real_path);
174       else
175         {
176           VEC_safe_push (char_ptr, auto_load_safe_path_vec, real_path);
177
178           if (debug_auto_load)
179             fprintf_unfiltered (gdb_stdlog,
180                                 _("auto-load: And canonicalized as \"%s\".\n"),
181                                 real_path);
182         }
183     }
184 }
185
186 /* Variable gdb_datadir has been set.  Update content depending on $ddir.  */
187
188 static void
189 auto_load_gdb_datadir_changed (void)
190 {
191   auto_load_safe_path_vec_update ();
192 }
193
194 /* "set" command for the auto_load_safe_path configuration variable.  */
195
196 static void
197 set_auto_load_safe_path (char *args, int from_tty, struct cmd_list_element *c)
198 {
199   /* Setting the variable to "" resets it to the compile time defaults.  */
200   if (auto_load_safe_path[0] == '\0')
201     {
202       xfree (auto_load_safe_path);
203       auto_load_safe_path = xstrdup (AUTO_LOAD_SAFE_PATH);
204     }
205
206   auto_load_safe_path_vec_update ();
207 }
208
209 /* "show" command for the auto_load_safe_path configuration variable.  */
210
211 static void
212 show_auto_load_safe_path (struct ui_file *file, int from_tty,
213                           struct cmd_list_element *c, const char *value)
214 {
215   if (strcmp (value, "/") == 0)
216     fprintf_filtered (file, _("Auto-load files are safe to load from any "
217                               "directory.\n"));
218   else
219     fprintf_filtered (file, _("List of directories from which it is safe to "
220                               "auto-load files is %s.\n"),
221                       value);
222 }
223
224 /* "add-auto-load-safe-path" command for the auto_load_safe_path configuration
225    variable.  */
226
227 static void
228 add_auto_load_safe_path (char *args, int from_tty)
229 {
230   char *s;
231
232   if (args == NULL || *args == 0)
233     error (_("\
234 Directory argument required.\n\
235 Use 'set auto-load safe-path /' for disabling the auto-load safe-path security.\
236 "));
237
238   s = xstrprintf ("%s%c%s", auto_load_safe_path, DIRNAME_SEPARATOR, args);
239   xfree (auto_load_safe_path);
240   auto_load_safe_path = s;
241
242   auto_load_safe_path_vec_update ();
243 }
244
245 /* Return 1 if FILENAME is equal to DIR or if FILENAME belongs to the
246    subdirectory DIR.  Return 0 otherwise.  gdb_realpath normalization is never
247    done here.  */
248
249 static ATTRIBUTE_PURE int
250 filename_is_in_dir (const char *filename, const char *dir)
251 {
252   size_t dir_len = strlen (dir);
253
254   while (dir_len && IS_DIR_SEPARATOR (dir[dir_len - 1]))
255     dir_len--;
256
257   /* Ensure auto_load_safe_path "/" matches any FILENAME.  On MS-Windows
258      platform FILENAME even after gdb_realpath does not have to start with
259      IS_DIR_SEPARATOR character, such as the 'C:\x.exe' filename.  */
260   if (dir_len == 0)
261     return 1;
262
263   return (filename_ncmp (dir, filename, dir_len) == 0
264           && (IS_DIR_SEPARATOR (filename[dir_len])
265               || filename[dir_len] == '\0'));
266 }
267
268 /* Return 1 if FILENAME belongs to one of directory components of
269    AUTO_LOAD_SAFE_PATH_VEC.  Return 0 otherwise.
270    auto_load_safe_path_vec_update is never called.
271    *FILENAME_REALP may be updated by gdb_realpath of FILENAME - it has to be
272    freed by the caller.  */
273
274 static int
275 filename_is_in_auto_load_safe_path_vec (const char *filename,
276                                         char **filename_realp)
277 {
278   char *dir;
279   int ix;
280
281   for (ix = 0; VEC_iterate (char_ptr, auto_load_safe_path_vec, ix, dir); ++ix)
282     if (*filename_realp == NULL && filename_is_in_dir (filename, dir))
283       break;
284   
285   if (dir == NULL)
286     {
287       if (*filename_realp == NULL)
288         {
289           *filename_realp = gdb_realpath (filename);
290           if (debug_auto_load && strcmp (*filename_realp, filename) != 0)
291             fprintf_unfiltered (gdb_stdlog,
292                                 _("auto-load: Resolved "
293                                   "file \"%s\" as \"%s\".\n"),
294                                 filename, *filename_realp);
295         }
296
297       if (strcmp (*filename_realp, filename) != 0)
298         for (ix = 0; VEC_iterate (char_ptr, auto_load_safe_path_vec, ix, dir);
299              ++ix)
300           if (filename_is_in_dir (*filename_realp, dir))
301             break;
302     }
303
304   if (dir != NULL)
305     {
306       if (debug_auto_load)
307         fprintf_unfiltered (gdb_stdlog, _("auto-load: File \"%s\" matches "
308                                           "directory \"%s\".\n"),
309                             filename, dir);
310       return 1;
311     }
312
313   return 0;
314 }
315
316 /* Return 1 if FILENAME is located in one of the directories of
317    AUTO_LOAD_SAFE_PATH.  Otherwise call warning and return 0.  FILENAME does
318    not have to be an absolute path.
319
320    Existence of FILENAME is not checked.  Function will still give a warning
321    even if the caller would quietly skip non-existing file in unsafe
322    directory.  */
323
324 int
325 file_is_auto_load_safe (const char *filename, const char *debug_fmt, ...)
326 {
327   char *filename_real = NULL;
328   struct cleanup *back_to;
329
330   if (debug_auto_load)
331     {
332       va_list debug_args;
333
334       va_start (debug_args, debug_fmt);
335       vfprintf_unfiltered (gdb_stdlog, debug_fmt, debug_args);
336       va_end (debug_args);
337     }
338
339   back_to = make_cleanup (free_current_contents, &filename_real);
340
341   if (filename_is_in_auto_load_safe_path_vec (filename, &filename_real))
342     {
343       do_cleanups (back_to);
344       return 1;
345     }
346
347   auto_load_safe_path_vec_update ();
348   if (filename_is_in_auto_load_safe_path_vec (filename, &filename_real))
349     {
350       do_cleanups (back_to);
351       return 1;
352     }
353
354   warning (_("File \"%s\" auto-loading has been declined by your "
355              "`auto-load safe-path' set to \"%s\"."),
356            filename_real, auto_load_safe_path);
357
358   do_cleanups (back_to);
359   return 0;
360 }
361
362 /* Definition of script language for GDB canned sequences of commands.  */
363
364 static const struct script_language script_language_gdb
365   = { GDB_AUTO_FILE_NAME, source_gdb_script_for_objfile };
366
367 static void
368 source_gdb_script_for_objfile (struct objfile *objfile, FILE *file,
369                                const char *filename)
370 {
371   int is_safe;
372   struct auto_load_pspace_info *pspace_info;
373   volatile struct gdb_exception e;
374
375   is_safe = file_is_auto_load_safe (filename, _("auto-load: Loading canned "
376                                                 "sequences of commands script "
377                                                 "\"%s\" for objfile \"%s\".\n"),
378                                     filename, objfile->name);
379
380   /* Add this script to the hash table too so "info auto-load gdb-scripts"
381      can print it.  */
382   pspace_info = get_auto_load_pspace_data_for_loading (current_program_space);
383   maybe_add_script (pspace_info, is_safe, filename, filename,
384                     &script_language_gdb);
385
386   if (!is_safe)
387     return;
388
389   TRY_CATCH (e, RETURN_MASK_ALL)
390     {
391       script_from_file (file, filename);
392     }
393   exception_print (gdb_stderr, e);
394 }
395
396 /* For scripts specified in .debug_gdb_scripts, multiple objfiles may load
397    the same script.  There's no point in loading the script multiple times,
398    and there can be a lot of objfiles and scripts, so we keep track of scripts
399    loaded this way.  */
400
401 struct auto_load_pspace_info
402 {
403   /* For each program space we keep track of loaded scripts.  */
404   struct htab *loaded_scripts;
405
406   /* Non-zero if we've issued the warning about an auto-load script not being
407      found.  We only want to issue this warning once.  */
408   int script_not_found_warning_printed;
409 };
410
411 /* Objects of this type are stored in the loaded script hash table.  */
412
413 struct loaded_script
414 {
415   /* Name as provided by the objfile.  */
416   const char *name;
417
418   /* Full path name or NULL if script wasn't found (or was otherwise
419      inaccessible).  */
420   const char *full_path;
421
422   /* Non-zero if this script has been loaded.  */
423   int loaded;
424
425   const struct script_language *language;
426 };
427
428 /* Per-program-space data key.  */
429 static const struct program_space_data *auto_load_pspace_data;
430
431 static void
432 auto_load_pspace_data_cleanup (struct program_space *pspace, void *arg)
433 {
434   struct auto_load_pspace_info *info;
435
436   info = program_space_data (pspace, auto_load_pspace_data);
437   if (info != NULL)
438     {
439       if (info->loaded_scripts)
440         htab_delete (info->loaded_scripts);
441       xfree (info);
442     }
443 }
444
445 /* Get the current autoload data.  If none is found yet, add it now.  This
446    function always returns a valid object.  */
447
448 static struct auto_load_pspace_info *
449 get_auto_load_pspace_data (struct program_space *pspace)
450 {
451   struct auto_load_pspace_info *info;
452
453   info = program_space_data (pspace, auto_load_pspace_data);
454   if (info == NULL)
455     {
456       info = XZALLOC (struct auto_load_pspace_info);
457       set_program_space_data (pspace, auto_load_pspace_data, info);
458     }
459
460   return info;
461 }
462
463 /* Hash function for the loaded script hash.  */
464
465 static hashval_t
466 hash_loaded_script_entry (const void *data)
467 {
468   const struct loaded_script *e = data;
469
470   return htab_hash_string (e->name) ^ htab_hash_pointer (e->language);
471 }
472
473 /* Equality function for the loaded script hash.  */
474
475 static int
476 eq_loaded_script_entry (const void *a, const void *b)
477 {
478   const struct loaded_script *ea = a;
479   const struct loaded_script *eb = b;
480
481   return strcmp (ea->name, eb->name) == 0 && ea->language == eb->language;
482 }
483
484 /* Initialize the table to track loaded scripts.
485    Each entry is hashed by the full path name.  */
486
487 static void
488 init_loaded_scripts_info (struct auto_load_pspace_info *pspace_info)
489 {
490   /* Choose 31 as the starting size of the hash table, somewhat arbitrarily.
491      Space for each entry is obtained with one malloc so we can free them
492      easily.  */
493
494   pspace_info->loaded_scripts = htab_create (31,
495                                              hash_loaded_script_entry,
496                                              eq_loaded_script_entry,
497                                              xfree);
498
499   pspace_info->script_not_found_warning_printed = FALSE;
500 }
501
502 /* Wrapper on get_auto_load_pspace_data to also allocate the hash table
503    for loading scripts.  */
504
505 struct auto_load_pspace_info *
506 get_auto_load_pspace_data_for_loading (struct program_space *pspace)
507 {
508   struct auto_load_pspace_info *info;
509
510   info = get_auto_load_pspace_data (pspace);
511   if (info->loaded_scripts == NULL)
512     init_loaded_scripts_info (info);
513
514   return info;
515 }
516
517 /* Add script NAME in LANGUAGE to hash table of PSPACE_INFO.  LOADED 1 if the
518    script has been (is going to) be loaded, 0 otherwise (such as if it has not
519    been found).  FULL_PATH is NULL if the script wasn't found.  The result is
520    true if the script was already in the hash table.  */
521
522 int
523 maybe_add_script (struct auto_load_pspace_info *pspace_info, int loaded,
524                   const char *name, const char *full_path,
525                   const struct script_language *language)
526 {
527   struct htab *htab = pspace_info->loaded_scripts;
528   struct loaded_script **slot, entry;
529   int in_hash_table;
530
531   entry.name = name;
532   entry.language = language;
533   slot = (struct loaded_script **) htab_find_slot (htab, &entry, INSERT);
534   in_hash_table = *slot != NULL;
535
536   /* If this script is not in the hash table, add it.  */
537
538   if (! in_hash_table)
539     {
540       char *p;
541
542       /* Allocate all space in one chunk so it's easier to free.  */
543       *slot = xmalloc (sizeof (**slot)
544                        + strlen (name) + 1
545                        + (full_path != NULL ? (strlen (full_path) + 1) : 0));
546       p = ((char*) *slot) + sizeof (**slot);
547       strcpy (p, name);
548       (*slot)->name = p;
549       if (full_path != NULL)
550         {
551           p += strlen (p) + 1;
552           strcpy (p, full_path);
553           (*slot)->full_path = p;
554         }
555       else
556         (*slot)->full_path = NULL;
557       (*slot)->loaded = loaded;
558       (*slot)->language = language;
559     }
560
561   return in_hash_table;
562 }
563
564 /* Clear the table of loaded section scripts.  */
565
566 static void
567 clear_section_scripts (void)
568 {
569   struct program_space *pspace = current_program_space;
570   struct auto_load_pspace_info *info;
571
572   info = program_space_data (pspace, auto_load_pspace_data);
573   if (info != NULL && info->loaded_scripts != NULL)
574     {
575       htab_delete (info->loaded_scripts);
576       info->loaded_scripts = NULL;
577       info->script_not_found_warning_printed = FALSE;
578     }
579 }
580
581 /* Look for the auto-load script in LANGUAGE associated with OBJFILE and load
582    it.  */
583
584 void
585 auto_load_objfile_script (struct objfile *objfile,
586                           const struct script_language *language)
587 {
588   char *realname;
589   char *filename, *debugfile;
590   int len;
591   FILE *input;
592   struct cleanup *cleanups;
593
594   realname = gdb_realpath (objfile->name);
595   len = strlen (realname);
596   filename = xmalloc (len + strlen (language->suffix) + 1);
597   memcpy (filename, realname, len);
598   strcpy (filename + len, language->suffix);
599
600   cleanups = make_cleanup (xfree, filename);
601   make_cleanup (xfree, realname);
602
603   input = fopen (filename, "r");
604   debugfile = filename;
605
606   if (!input)
607     {
608       char *debugdir;
609       VEC (char_ptr) *debugdir_vec;
610       int ix;
611
612       debugdir_vec = dirnames_to_char_ptr_vec (debug_file_directory);
613       make_cleanup_free_char_ptr_vec (debugdir_vec);
614
615       for (ix = 0; VEC_iterate (char_ptr, debugdir_vec, ix, debugdir); ++ix)
616         {
617           /* Also try the same file in the separate debug info directory.  */
618           debugfile = xmalloc (strlen (debugdir) + strlen (filename) + 1);
619           strcpy (debugfile, debugdir);
620
621           /* FILENAME is absolute, so we don't need a "/" here.  */
622           strcat (debugfile, filename);
623
624           make_cleanup (xfree, debugfile);
625           input = fopen (debugfile, "r");
626           if (input != NULL)
627             break;
628         }
629     }
630
631   if (!input && gdb_datadir)
632     {
633       /* Also try the same file in a subdirectory of gdb's data
634          directory.  */
635       debugfile = xmalloc (strlen (gdb_datadir) + strlen (filename)
636                            + strlen ("/auto-load") + 1);
637       strcpy (debugfile, gdb_datadir);
638       strcat (debugfile, "/auto-load");
639       /* FILENAME is absolute, so we don't need a "/" here.  */
640       strcat (debugfile, filename);
641
642       make_cleanup (xfree, debugfile);
643       input = fopen (debugfile, "r");
644     }
645
646   if (input)
647     {
648       make_cleanup_fclose (input);
649
650       /* To preserve existing behaviour we don't check for whether the
651          script was already in the table, and always load it.
652          It's highly unlikely that we'd ever load it twice,
653          and these scripts are required to be idempotent under multiple
654          loads anyway.  */
655       language->source_script_for_objfile (objfile, input, debugfile);
656     }
657
658   do_cleanups (cleanups);
659 }
660
661 /* Load any auto-loaded scripts for OBJFILE.  */
662
663 void
664 load_auto_scripts_for_objfile (struct objfile *objfile)
665 {
666   if (!global_auto_load)
667     return;
668
669   if (auto_load_gdb_scripts)
670     auto_load_objfile_script (objfile, &script_language_gdb);
671
672   gdbpy_load_auto_scripts_for_objfile (objfile);
673 }
674
675 /* This is a new_objfile observer callback to auto-load scripts.
676
677    Two flavors of auto-loaded scripts are supported.
678    1) based on the path to the objfile
679    2) from .debug_gdb_scripts section  */
680
681 static void
682 auto_load_new_objfile (struct objfile *objfile)
683 {
684   if (!objfile)
685     {
686       /* OBJFILE is NULL when loading a new "main" symbol-file.  */
687       clear_section_scripts ();
688       return;
689     }
690
691   load_auto_scripts_for_objfile (objfile);
692 }
693
694 /* Collect scripts to be printed in a vec.  */
695
696 typedef struct loaded_script *loaded_script_ptr;
697 DEF_VEC_P (loaded_script_ptr);
698
699 struct collect_matching_scripts_data
700 {
701   VEC (loaded_script_ptr) **scripts_p;
702
703   const struct script_language *language;
704 };
705
706 /* Traversal function for htab_traverse.
707    Collect the entry if it matches the regexp.  */
708
709 static int
710 collect_matching_scripts (void **slot, void *info)
711 {
712   struct loaded_script *script = *slot;
713   struct collect_matching_scripts_data *data = info;
714
715   if (script->language == data->language && re_exec (script->name))
716     VEC_safe_push (loaded_script_ptr, *data->scripts_p, script);
717
718   return 1;
719 }
720
721 /* Print SCRIPT.  */
722
723 static void
724 print_script (struct loaded_script *script)
725 {
726   struct ui_out *uiout = current_uiout;
727   struct cleanup *chain;
728
729   chain = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
730
731   ui_out_field_string (uiout, "loaded", script->loaded ? "Yes" : "No");
732   ui_out_field_string (uiout, "script", script->name);
733   ui_out_text (uiout, "\n");
734
735   /* If the name isn't the full path, print it too.  */
736   if (script->full_path != NULL
737       && strcmp (script->name, script->full_path) != 0)
738     {
739       ui_out_text (uiout, "\tfull name: ");
740       ui_out_field_string (uiout, "full_path", script->full_path);
741       ui_out_text (uiout, "\n");
742     }
743
744   do_cleanups (chain);
745 }
746
747 /* Helper for info_auto_load_scripts to sort the scripts by name.  */
748
749 static int
750 sort_scripts_by_name (const void *ap, const void *bp)
751 {
752   const struct loaded_script *a = *(const struct loaded_script **) ap;
753   const struct loaded_script *b = *(const struct loaded_script **) bp;
754
755   return FILENAME_CMP (a->name, b->name);
756 }
757
758 /* Special internal GDB value of auto_load_info_scripts's PATTERN identify
759    the "info auto-load XXX" command has been executed through the general
760    "info auto-load" invocation.  Extra newline will be printed if needed.  */
761 char auto_load_info_scripts_pattern_nl[] = "";
762
763 /* Implementation for "info auto-load gdb-scripts"
764    (and "info auto-load python-scripts").  List scripts in LANGUAGE matching
765    PATTERN.  FROM_TTY is the usual GDB boolean for user interactivity.  */
766
767 void
768 auto_load_info_scripts (char *pattern, int from_tty,
769                         const struct script_language *language)
770 {
771   struct ui_out *uiout = current_uiout;
772   struct auto_load_pspace_info *pspace_info;
773   struct cleanup *script_chain;
774   VEC (loaded_script_ptr) *scripts;
775   int nr_scripts;
776
777   dont_repeat ();
778
779   pspace_info = get_auto_load_pspace_data (current_program_space);
780
781   if (pattern && *pattern)
782     {
783       char *re_err = re_comp (pattern);
784
785       if (re_err)
786         error (_("Invalid regexp: %s"), re_err);
787     }
788   else
789     {
790       re_comp ("");
791     }
792
793   /* We need to know the number of rows before we build the table.
794      Plus we want to sort the scripts by name.
795      So first traverse the hash table collecting the matching scripts.  */
796
797   scripts = VEC_alloc (loaded_script_ptr, 10);
798   script_chain = make_cleanup (VEC_cleanup (loaded_script_ptr), &scripts);
799
800   if (pspace_info != NULL && pspace_info->loaded_scripts != NULL)
801     {
802       struct collect_matching_scripts_data data = { &scripts, language };
803
804       immediate_quit++;
805       /* Pass a pointer to scripts as VEC_safe_push can realloc space.  */
806       htab_traverse_noresize (pspace_info->loaded_scripts,
807                               collect_matching_scripts, &data);
808       immediate_quit--;
809     }
810
811   nr_scripts = VEC_length (loaded_script_ptr, scripts);
812
813   /* Table header shifted right by preceding "gdb-scripts:  " would not match
814      its columns.  */
815   if (nr_scripts > 0 && pattern == auto_load_info_scripts_pattern_nl)
816     ui_out_text (uiout, "\n");
817
818   make_cleanup_ui_out_table_begin_end (uiout, 2, nr_scripts,
819                                        "AutoLoadedScriptsTable");
820
821   ui_out_table_header (uiout, 7, ui_left, "loaded", "Loaded");
822   ui_out_table_header (uiout, 70, ui_left, "script", "Script");
823   ui_out_table_body (uiout);
824
825   if (nr_scripts > 0)
826     {
827       int i;
828       loaded_script_ptr script;
829
830       qsort (VEC_address (loaded_script_ptr, scripts),
831              VEC_length (loaded_script_ptr, scripts),
832              sizeof (loaded_script_ptr), sort_scripts_by_name);
833       for (i = 0; VEC_iterate (loaded_script_ptr, scripts, i, script); ++i)
834         print_script (script);
835     }
836
837   do_cleanups (script_chain);
838
839   if (nr_scripts == 0)
840     {
841       if (pattern && *pattern)
842         ui_out_message (uiout, 0, "No auto-load scripts matching %s.\n",
843                         pattern);
844       else
845         ui_out_message (uiout, 0, "No auto-load scripts.\n");
846     }
847 }
848
849 /* Wrapper for "info auto-load gdb-scripts".  */
850
851 static void
852 info_auto_load_gdb_scripts (char *pattern, int from_tty)
853 {
854   auto_load_info_scripts (pattern, from_tty, &script_language_gdb);
855 }
856
857 /* Implement 'info auto-load local-gdbinit'.  */
858
859 static void
860 info_auto_load_local_gdbinit (char *args, int from_tty)
861 {
862   if (auto_load_local_gdbinit_pathname == NULL)
863     printf_filtered (_("Local .gdbinit file was not found.\n"));
864   else if (auto_load_local_gdbinit_loaded)
865     printf_filtered (_("Local .gdbinit file \"%s\" has been loaded.\n"),
866                      auto_load_local_gdbinit_pathname);
867   else
868     printf_filtered (_("Local .gdbinit file \"%s\" has not been loaded.\n"),
869                      auto_load_local_gdbinit_pathname);
870 }
871
872 /* Return non-zero if SCRIPT_NOT_FOUND_WARNING_PRINTED of PSPACE_INFO was unset
873    before calling this function.  Always set SCRIPT_NOT_FOUND_WARNING_PRINTED
874    of PSPACE_INFO.  */
875
876 int
877 script_not_found_warning_print (struct auto_load_pspace_info *pspace_info)
878 {
879   int retval = !pspace_info->script_not_found_warning_printed;
880
881   pspace_info->script_not_found_warning_printed = 1;
882
883   return retval;
884 }
885
886 /* The only valid "set auto-load" argument is off|0|no|disable.  */
887
888 static void
889 set_auto_load_cmd (char *args, int from_tty)
890 {
891   struct cmd_list_element *list;
892   size_t length;
893
894   /* See parse_binary_operation in use by the sub-commands.  */
895
896   length = args ? strlen (args) : 0;
897
898   while (length > 0 && (args[length - 1] == ' ' || args[length - 1] == '\t'))
899     length--;
900
901   if (length == 0 || (strncmp (args, "off", length) != 0
902                       && strncmp (args, "0", length) != 0
903                       && strncmp (args, "no", length) != 0
904                       && strncmp (args, "disable", length) != 0))
905     error (_("Valid is only global 'set auto-load no'; "
906              "otherwise check the auto-load sub-commands."));
907
908   for (list = *auto_load_set_cmdlist_get (); list != NULL; list = list->next)
909     if (list->var_type == var_boolean)
910       {
911         gdb_assert (list->type == set_cmd);
912         do_setshow_command (args, from_tty, list);
913       }
914 }
915
916 /* Initialize "set auto-load " commands prefix and return it.  */
917
918 struct cmd_list_element **
919 auto_load_set_cmdlist_get (void)
920 {
921   static struct cmd_list_element *retval;
922
923   if (retval == NULL)
924     add_prefix_cmd ("auto-load", class_maintenance, set_auto_load_cmd, _("\
925 Auto-loading specific settings.\n\
926 Configure various auto-load-specific variables such as\n\
927 automatic loading of Python scripts."),
928                     &retval, "set auto-load ",
929                     1/*allow-unknown*/, &setlist);
930
931   return &retval;
932 }
933
934 /* Command "show auto-load" displays summary of all the current
935    "show auto-load " settings.  */
936
937 static void
938 show_auto_load_cmd (char *args, int from_tty)
939 {
940   cmd_show_list (*auto_load_show_cmdlist_get (), from_tty, "");
941 }
942
943 /* Initialize "show auto-load " commands prefix and return it.  */
944
945 struct cmd_list_element **
946 auto_load_show_cmdlist_get (void)
947 {
948   static struct cmd_list_element *retval;
949
950   if (retval == NULL)
951     add_prefix_cmd ("auto-load", class_maintenance, show_auto_load_cmd, _("\
952 Show auto-loading specific settings.\n\
953 Show configuration of various auto-load-specific variables such as\n\
954 automatic loading of Python scripts."),
955                     &retval, "show auto-load ",
956                     0/*allow-unknown*/, &showlist);
957
958   return &retval;
959 }
960
961 /* Command "info auto-load" displays whether the various auto-load files have
962    been loaded.  This is reimplementation of cmd_show_list which inserts
963    newlines at proper places.  */
964
965 static void
966 info_auto_load_cmd (char *args, int from_tty)
967 {
968   struct cmd_list_element *list;
969   struct cleanup *infolist_chain;
970   struct ui_out *uiout = current_uiout;
971
972   infolist_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "infolist");
973
974   for (list = *auto_load_info_cmdlist_get (); list != NULL; list = list->next)
975     {
976       struct cleanup *option_chain
977         = make_cleanup_ui_out_tuple_begin_end (uiout, "option");
978
979       gdb_assert (!list->prefixlist);
980       gdb_assert (list->type == not_set_cmd);
981
982       ui_out_field_string (uiout, "name", list->name);
983       ui_out_text (uiout, ":  ");
984       cmd_func (list, auto_load_info_scripts_pattern_nl, from_tty);
985
986       /* Close the tuple.  */
987       do_cleanups (option_chain);
988     }
989
990   /* Close the tuple.  */
991   do_cleanups (infolist_chain);
992 }
993
994 /* Initialize "info auto-load " commands prefix and return it.  */
995
996 struct cmd_list_element **
997 auto_load_info_cmdlist_get (void)
998 {
999   static struct cmd_list_element *retval;
1000
1001   if (retval == NULL)
1002     add_prefix_cmd ("auto-load", class_info, info_auto_load_cmd, _("\
1003 Print current status of auto-loaded files.\n\
1004 Print whether various files like Python scripts or .gdbinit files have been\n\
1005 found and/or loaded."),
1006                     &retval, "info auto-load ",
1007                     0/*allow-unknown*/, &infolist);
1008
1009   return &retval;
1010 }
1011
1012 void _initialize_auto_load (void);
1013
1014 void
1015 _initialize_auto_load (void)
1016 {
1017   struct cmd_list_element *cmd;
1018
1019   auto_load_pspace_data
1020     = register_program_space_data_with_cleanup (auto_load_pspace_data_cleanup);
1021
1022   observer_attach_new_objfile (auto_load_new_objfile);
1023
1024   add_setshow_boolean_cmd ("gdb-scripts", class_support,
1025                            &auto_load_gdb_scripts, _("\
1026 Enable or disable auto-loading of canned sequences of commands scripts."), _("\
1027 Show whether auto-loading of canned sequences of commands scripts is enabled."),
1028                            _("\
1029 If enabled, canned sequences of commands are loaded when the debugger reads\n\
1030 an executable or shared library.\n\
1031 This options has security implications for untrusted inferiors."),
1032                            NULL, show_auto_load_gdb_scripts,
1033                            auto_load_set_cmdlist_get (),
1034                            auto_load_show_cmdlist_get ());
1035
1036   add_cmd ("gdb-scripts", class_info, info_auto_load_gdb_scripts,
1037            _("Print the list of automatically loaded sequences of commands.\n\
1038 Usage: info auto-load gdb-scripts [REGEXP]"),
1039            auto_load_info_cmdlist_get ());
1040
1041   add_setshow_boolean_cmd ("local-gdbinit", class_support,
1042                            &auto_load_local_gdbinit, _("\
1043 Enable or disable auto-loading of .gdbinit script in current directory."), _("\
1044 Show whether auto-loading .gdbinit script in current directory is enabled."),
1045                            _("\
1046 If enabled, canned sequences of commands are loaded when debugger starts\n\
1047 from .gdbinit file in current directory.  Such files are deprecated,\n\
1048 use a script associated with inferior executable file instead.\n\
1049 This options has security implications for untrusted inferiors."),
1050                            NULL, show_auto_load_local_gdbinit,
1051                            auto_load_set_cmdlist_get (),
1052                            auto_load_show_cmdlist_get ());
1053
1054   add_cmd ("local-gdbinit", class_info, info_auto_load_local_gdbinit,
1055            _("Print whether current directory .gdbinit file has been loaded.\n\
1056 Usage: info auto-load local-gdbinit"),
1057            auto_load_info_cmdlist_get ());
1058
1059   auto_load_safe_path = xstrdup (AUTO_LOAD_SAFE_PATH);
1060   auto_load_safe_path_vec_update ();
1061   add_setshow_optional_filename_cmd ("safe-path", class_support,
1062                                      &auto_load_safe_path, _("\
1063 Set the list of directories from which it is safe to auto-load files."), _("\
1064 Show the list of directories from which it is safe to auto-load files."), _("\
1065 Various files loaded automatically for the 'set auto-load ...' options must\n\
1066 be located in one of the directories listed by this option.  Warning will be\n\
1067 printed and file will not be used otherwise.\n\
1068 Setting this parameter to an empty list resets it to its default value.\n\
1069 Setting this parameter to '/' (without the quotes) allows any file\n\
1070 for the 'set auto-load ...' options.\n\
1071 This option is ignored for the kinds of files having 'set auto-load ... off'.\n\
1072 This options has security implications for untrusted inferiors."),
1073                                      set_auto_load_safe_path,
1074                                      show_auto_load_safe_path,
1075                                      auto_load_set_cmdlist_get (),
1076                                      auto_load_show_cmdlist_get ());
1077   observer_attach_gdb_datadir_changed (auto_load_gdb_datadir_changed);
1078
1079   cmd = add_cmd ("add-auto-load-safe-path", class_support,
1080                  add_auto_load_safe_path,
1081                  _("Add entries to the list of directories from which it is safe "
1082                    "to auto-load files.\n\
1083 See the commands 'set auto-load safe-path' and 'show auto-load safe-path' to\n\
1084 access the current full list setting."),
1085                  &cmdlist);
1086   set_cmd_completer (cmd, filename_completer);
1087
1088   add_setshow_boolean_cmd ("auto-load", class_maintenance,
1089                            &debug_auto_load, _("\
1090 Set auto-load verifications debugging."), _("\
1091 Show auto-load verifications debugging."), _("\
1092 When non-zero, debugging output for files of 'set auto-load ...'\n\
1093 is displayed."),
1094                             NULL, show_debug_auto_load,
1095                             &setdebuglist, &showdebuglist);
1096 }