1 /* GDB routines for supporting auto-loaded scripts.
3 Copyright (C) 2012-2013 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 #include "auto-load.h"
22 #include "progspace.h"
23 #include "python/python.h"
24 #include "gdb_regex.h"
26 #include "filenames.h"
30 #include "exceptions.h"
31 #include "cli/cli-script.h"
33 #include "cli/cli-decode.h"
34 #include "cli/cli-setshow.h"
36 #include "readline/tilde.h"
37 #include "completer.h"
41 #include "filestuff.h"
43 /* The suffix of per-objfile scripts to auto-load as non-Python command files.
44 E.g. When the program loads libfoo.so, look for libfoo-gdb.gdb. */
45 #define GDB_AUTO_FILE_NAME "-gdb.gdb"
47 static void source_gdb_script_for_objfile (struct objfile
*objfile
, FILE *file
,
48 const char *filename
);
50 /* Value of the 'set debug auto-load' configuration variable. */
51 static int debug_auto_load
= 0;
53 /* "show" command for the debug_auto_load configuration variable. */
56 show_debug_auto_load (struct ui_file
*file
, int from_tty
,
57 struct cmd_list_element
*c
, const char *value
)
59 fprintf_filtered (file
, _("Debugging output for files "
60 "of 'set auto-load ...' is %s.\n"),
64 /* User-settable option to enable/disable auto-loading of GDB_AUTO_FILE_NAME
66 set auto-load gdb-scripts on|off
67 This is true if we should auto-load associated scripts when an objfile
68 is opened, false otherwise. */
69 static int auto_load_gdb_scripts
= 1;
71 /* "show" command for the auto_load_gdb_scripts configuration variable. */
74 show_auto_load_gdb_scripts (struct ui_file
*file
, int from_tty
,
75 struct cmd_list_element
*c
, const char *value
)
77 fprintf_filtered (file
, _("Auto-loading of canned sequences of commands "
82 /* Internal-use flag to enable/disable auto-loading.
83 This is true if we should auto-load python code when an objfile is opened,
86 Both auto_load_scripts && global_auto_load must be true to enable
89 This flag exists to facilitate deferring auto-loading during start-up
90 until after ./.gdbinit has been read; it may augment the search directories
91 used to find the scripts. */
92 int global_auto_load
= 1;
94 /* Auto-load .gdbinit file from the current directory? */
95 int auto_load_local_gdbinit
= 1;
97 /* Absolute pathname to the current directory .gdbinit, if it exists. */
98 char *auto_load_local_gdbinit_pathname
= NULL
;
100 /* Boolean value if AUTO_LOAD_LOCAL_GDBINIT_PATHNAME has been loaded. */
101 int auto_load_local_gdbinit_loaded
= 0;
103 /* "show" command for the auto_load_local_gdbinit configuration variable. */
106 show_auto_load_local_gdbinit (struct ui_file
*file
, int from_tty
,
107 struct cmd_list_element
*c
, const char *value
)
109 fprintf_filtered (file
, _("Auto-loading of .gdbinit script from current "
110 "directory is %s.\n"),
114 /* Directory list from which to load auto-loaded scripts. It is not checked
115 for absolute paths but they are strongly recommended. It is initialized by
116 _initialize_auto_load. */
117 static char *auto_load_dir
;
119 /* "set" command for the auto_load_dir configuration variable. */
122 set_auto_load_dir (char *args
, int from_tty
, struct cmd_list_element
*c
)
124 /* Setting the variable to "" resets it to the compile time defaults. */
125 if (auto_load_dir
[0] == '\0')
127 xfree (auto_load_dir
);
128 auto_load_dir
= xstrdup (AUTO_LOAD_DIR
);
132 /* "show" command for the auto_load_dir configuration variable. */
135 show_auto_load_dir (struct ui_file
*file
, int from_tty
,
136 struct cmd_list_element
*c
, const char *value
)
138 fprintf_filtered (file
, _("List of directories from which to load "
139 "auto-loaded scripts is %s.\n"),
143 /* Directory list safe to hold auto-loaded files. It is not checked for
144 absolute paths but they are strongly recommended. It is initialized by
145 _initialize_auto_load. */
146 static char *auto_load_safe_path
;
148 /* Vector of directory elements of AUTO_LOAD_SAFE_PATH with each one normalized
149 by tilde_expand and possibly each entries has added its gdb_realpath
151 static VEC (char_ptr
) *auto_load_safe_path_vec
;
153 /* Expand $datadir and $debugdir in STRING according to the rules of
154 substitute_path_component. Return vector from dirnames_to_char_ptr_vec,
155 this vector must be freed by free_char_ptr_vec by the caller. */
157 static VEC (char_ptr
) *
158 auto_load_expand_dir_vars (const char *string
)
160 VEC (char_ptr
) *dir_vec
;
163 s
= xstrdup (string
);
164 substitute_path_component (&s
, "$datadir", gdb_datadir
);
165 substitute_path_component (&s
, "$debugdir", debug_file_directory
);
167 if (debug_auto_load
&& strcmp (s
, string
) != 0)
168 fprintf_unfiltered (gdb_stdlog
,
169 _("auto-load: Expanded $-variables to \"%s\".\n"), s
);
171 dir_vec
= dirnames_to_char_ptr_vec (s
);
177 /* Update auto_load_safe_path_vec from current AUTO_LOAD_SAFE_PATH. */
180 auto_load_safe_path_vec_update (void)
186 fprintf_unfiltered (gdb_stdlog
,
187 _("auto-load: Updating directories of \"%s\".\n"),
188 auto_load_safe_path
);
190 free_char_ptr_vec (auto_load_safe_path_vec
);
192 auto_load_safe_path_vec
= auto_load_expand_dir_vars (auto_load_safe_path
);
193 len
= VEC_length (char_ptr
, auto_load_safe_path_vec
);
195 /* Apply tilde_expand and gdb_realpath to each AUTO_LOAD_SAFE_PATH_VEC
197 for (ix
= 0; ix
< len
; ix
++)
199 char *dir
= VEC_index (char_ptr
, auto_load_safe_path_vec
, ix
);
200 char *expanded
= tilde_expand (dir
);
201 char *real_path
= gdb_realpath (expanded
);
203 /* Ensure the current entry is at least tilde_expand-ed. */
204 VEC_replace (char_ptr
, auto_load_safe_path_vec
, ix
, expanded
);
208 if (strcmp (expanded
, dir
) == 0)
209 fprintf_unfiltered (gdb_stdlog
,
210 _("auto-load: Using directory \"%s\".\n"),
213 fprintf_unfiltered (gdb_stdlog
,
214 _("auto-load: Resolved directory \"%s\" "
220 /* If gdb_realpath returns a different content, append it. */
221 if (strcmp (real_path
, expanded
) == 0)
225 VEC_safe_push (char_ptr
, auto_load_safe_path_vec
, real_path
);
228 fprintf_unfiltered (gdb_stdlog
,
229 _("auto-load: And canonicalized as \"%s\".\n"),
235 /* Variable gdb_datadir has been set. Update content depending on $datadir. */
238 auto_load_gdb_datadir_changed (void)
240 auto_load_safe_path_vec_update ();
243 /* "set" command for the auto_load_safe_path configuration variable. */
246 set_auto_load_safe_path (char *args
, int from_tty
, struct cmd_list_element
*c
)
248 /* Setting the variable to "" resets it to the compile time defaults. */
249 if (auto_load_safe_path
[0] == '\0')
251 xfree (auto_load_safe_path
);
252 auto_load_safe_path
= xstrdup (AUTO_LOAD_SAFE_PATH
);
255 auto_load_safe_path_vec_update ();
258 /* "show" command for the auto_load_safe_path configuration variable. */
261 show_auto_load_safe_path (struct ui_file
*file
, int from_tty
,
262 struct cmd_list_element
*c
, const char *value
)
266 /* Check if user has entered either "/" or for example ":".
267 But while more complicate content like ":/foo" would still also
268 permit any location do not hide those. */
270 for (cs
= value
; *cs
&& (*cs
== DIRNAME_SEPARATOR
|| IS_DIR_SEPARATOR (*cs
));
273 fprintf_filtered (file
, _("Auto-load files are safe to load from any "
276 fprintf_filtered (file
, _("List of directories from which it is safe to "
277 "auto-load files is %s.\n"),
281 /* "add-auto-load-safe-path" command for the auto_load_safe_path configuration
285 add_auto_load_safe_path (char *args
, int from_tty
)
289 if (args
== NULL
|| *args
== 0)
291 Directory argument required.\n\
292 Use 'set auto-load safe-path /' for disabling the auto-load safe-path security.\
295 s
= xstrprintf ("%s%c%s", auto_load_safe_path
, DIRNAME_SEPARATOR
, args
);
296 xfree (auto_load_safe_path
);
297 auto_load_safe_path
= s
;
299 auto_load_safe_path_vec_update ();
302 /* Implementation for filename_is_in_pattern overwriting the caller's FILENAME
306 filename_is_in_pattern_1 (char *filename
, char *pattern
)
308 size_t pattern_len
= strlen (pattern
);
309 size_t filename_len
= strlen (filename
);
312 fprintf_unfiltered (gdb_stdlog
, _("auto-load: Matching file \"%s\" "
313 "to pattern \"%s\"\n"),
316 /* Trim trailing slashes ("/") from PATTERN. Even for "d:\" paths as
317 trailing slashes are trimmed also from FILENAME it still matches
319 while (pattern_len
&& IS_DIR_SEPARATOR (pattern
[pattern_len
- 1]))
321 pattern
[pattern_len
] = '\0';
323 /* Ensure auto_load_safe_path "/" matches any FILENAME. On MS-Windows
324 platform FILENAME even after gdb_realpath does not have to start with
325 IS_DIR_SEPARATOR character, such as the 'C:\x.exe' filename. */
326 if (pattern_len
== 0)
329 fprintf_unfiltered (gdb_stdlog
,
330 _("auto-load: Matched - empty pattern\n"));
336 /* Trim trailing slashes ("/"). PATTERN also has slashes trimmed the
337 same way so they will match. */
338 while (filename_len
&& IS_DIR_SEPARATOR (filename
[filename_len
- 1]))
340 filename
[filename_len
] = '\0';
341 if (filename_len
== 0)
344 fprintf_unfiltered (gdb_stdlog
,
345 _("auto-load: Not matched - pattern \"%s\".\n"),
350 if (gdb_filename_fnmatch (pattern
, filename
, FNM_FILE_NAME
| FNM_NOESCAPE
)
354 fprintf_unfiltered (gdb_stdlog
, _("auto-load: Matched - file "
355 "\"%s\" to pattern \"%s\".\n"),
360 /* Trim trailing FILENAME component. */
361 while (filename_len
> 0 && !IS_DIR_SEPARATOR (filename
[filename_len
- 1]))
366 /* Return 1 if FILENAME matches PATTERN or if FILENAME resides in
367 a subdirectory of a directory that matches PATTERN. Return 0 otherwise.
368 gdb_realpath normalization is never done here. */
370 static ATTRIBUTE_PURE
int
371 filename_is_in_pattern (const char *filename
, const char *pattern
)
373 char *filename_copy
, *pattern_copy
;
375 filename_copy
= alloca (strlen (filename
) + 1);
376 strcpy (filename_copy
, filename
);
377 pattern_copy
= alloca (strlen (pattern
) + 1);
378 strcpy (pattern_copy
, pattern
);
380 return filename_is_in_pattern_1 (filename_copy
, pattern_copy
);
383 /* Return 1 if FILENAME belongs to one of directory components of
384 AUTO_LOAD_SAFE_PATH_VEC. Return 0 otherwise.
385 auto_load_safe_path_vec_update is never called.
386 *FILENAME_REALP may be updated by gdb_realpath of FILENAME - it has to be
387 freed by the caller. */
390 filename_is_in_auto_load_safe_path_vec (const char *filename
,
391 char **filename_realp
)
396 for (ix
= 0; VEC_iterate (char_ptr
, auto_load_safe_path_vec
, ix
, pattern
);
398 if (*filename_realp
== NULL
&& filename_is_in_pattern (filename
, pattern
))
403 if (*filename_realp
== NULL
)
405 *filename_realp
= gdb_realpath (filename
);
406 if (debug_auto_load
&& strcmp (*filename_realp
, filename
) != 0)
407 fprintf_unfiltered (gdb_stdlog
,
408 _("auto-load: Resolved "
409 "file \"%s\" as \"%s\".\n"),
410 filename
, *filename_realp
);
413 if (strcmp (*filename_realp
, filename
) != 0)
415 VEC_iterate (char_ptr
, auto_load_safe_path_vec
, ix
, pattern
); ++ix
)
416 if (filename_is_in_pattern (*filename_realp
, pattern
))
423 fprintf_unfiltered (gdb_stdlog
, _("auto-load: File \"%s\" matches "
424 "directory \"%s\".\n"),
432 /* Return 1 if FILENAME is located in one of the directories of
433 AUTO_LOAD_SAFE_PATH. Otherwise call warning and return 0. FILENAME does
434 not have to be an absolute path.
436 Existence of FILENAME is not checked. Function will still give a warning
437 even if the caller would quietly skip non-existing file in unsafe
441 file_is_auto_load_safe (const char *filename
, const char *debug_fmt
, ...)
443 char *filename_real
= NULL
;
444 struct cleanup
*back_to
;
445 static int advice_printed
= 0;
451 va_start (debug_args
, debug_fmt
);
452 vfprintf_unfiltered (gdb_stdlog
, debug_fmt
, debug_args
);
456 back_to
= make_cleanup (free_current_contents
, &filename_real
);
458 if (filename_is_in_auto_load_safe_path_vec (filename
, &filename_real
))
460 do_cleanups (back_to
);
464 auto_load_safe_path_vec_update ();
465 if (filename_is_in_auto_load_safe_path_vec (filename
, &filename_real
))
467 do_cleanups (back_to
);
471 warning (_("File \"%s\" auto-loading has been declined by your "
472 "`auto-load safe-path' set to \"%s\"."),
473 filename_real
, auto_load_safe_path
);
477 const char *homedir
= getenv ("HOME");
482 homeinit
= xstrprintf ("%s/%s", homedir
, gdbinit
);
483 make_cleanup (xfree
, homeinit
);
485 printf_filtered (_("\
486 To enable execution of this file add\n\
487 \tadd-auto-load-safe-path %s\n\
488 line to your configuration file \"%s\".\n\
489 To completely disable this security protection add\n\
490 \tset auto-load safe-path /\n\
491 line to your configuration file \"%s\".\n\
492 For more information about this security protection see the\n\
493 \"Auto-loading safe path\" section in the GDB manual. E.g., run from the shell:\n\
494 \tinfo \"(gdb)Auto-loading safe path\"\n"),
495 filename_real
, homeinit
, homeinit
);
499 do_cleanups (back_to
);
503 /* Definition of script language for GDB canned sequences of commands. */
505 static const struct script_language script_language_gdb
506 = { GDB_AUTO_FILE_NAME
, source_gdb_script_for_objfile
};
509 source_gdb_script_for_objfile (struct objfile
*objfile
, FILE *file
,
510 const char *filename
)
513 struct auto_load_pspace_info
*pspace_info
;
514 volatile struct gdb_exception e
;
516 is_safe
= file_is_auto_load_safe (filename
, _("auto-load: Loading canned "
517 "sequences of commands script "
518 "\"%s\" for objfile \"%s\".\n"),
519 filename
, objfile
->name
);
521 /* Add this script to the hash table too so "info auto-load gdb-scripts"
523 pspace_info
= get_auto_load_pspace_data_for_loading (current_program_space
);
524 maybe_add_script (pspace_info
, is_safe
, filename
, filename
,
525 &script_language_gdb
);
530 TRY_CATCH (e
, RETURN_MASK_ALL
)
532 script_from_file (file
, filename
);
534 exception_print (gdb_stderr
, e
);
537 /* For scripts specified in .debug_gdb_scripts, multiple objfiles may load
538 the same script. There's no point in loading the script multiple times,
539 and there can be a lot of objfiles and scripts, so we keep track of scripts
542 struct auto_load_pspace_info
544 /* For each program space we keep track of loaded scripts. */
545 struct htab
*loaded_scripts
;
547 /* Non-zero if we've issued the warning about an auto-load script not being
548 found. We only want to issue this warning once. */
549 int script_not_found_warning_printed
;
552 /* Objects of this type are stored in the loaded script hash table. */
556 /* Name as provided by the objfile. */
559 /* Full path name or NULL if script wasn't found (or was otherwise
561 const char *full_path
;
563 /* Non-zero if this script has been loaded. */
566 const struct script_language
*language
;
569 /* Per-program-space data key. */
570 static const struct program_space_data
*auto_load_pspace_data
;
573 auto_load_pspace_data_cleanup (struct program_space
*pspace
, void *arg
)
575 struct auto_load_pspace_info
*info
;
577 info
= program_space_data (pspace
, auto_load_pspace_data
);
580 if (info
->loaded_scripts
)
581 htab_delete (info
->loaded_scripts
);
586 /* Get the current autoload data. If none is found yet, add it now. This
587 function always returns a valid object. */
589 static struct auto_load_pspace_info
*
590 get_auto_load_pspace_data (struct program_space
*pspace
)
592 struct auto_load_pspace_info
*info
;
594 info
= program_space_data (pspace
, auto_load_pspace_data
);
597 info
= XZALLOC (struct auto_load_pspace_info
);
598 set_program_space_data (pspace
, auto_load_pspace_data
, info
);
604 /* Hash function for the loaded script hash. */
607 hash_loaded_script_entry (const void *data
)
609 const struct loaded_script
*e
= data
;
611 return htab_hash_string (e
->name
) ^ htab_hash_pointer (e
->language
);
614 /* Equality function for the loaded script hash. */
617 eq_loaded_script_entry (const void *a
, const void *b
)
619 const struct loaded_script
*ea
= a
;
620 const struct loaded_script
*eb
= b
;
622 return strcmp (ea
->name
, eb
->name
) == 0 && ea
->language
== eb
->language
;
625 /* Initialize the table to track loaded scripts.
626 Each entry is hashed by the full path name. */
629 init_loaded_scripts_info (struct auto_load_pspace_info
*pspace_info
)
631 /* Choose 31 as the starting size of the hash table, somewhat arbitrarily.
632 Space for each entry is obtained with one malloc so we can free them
635 pspace_info
->loaded_scripts
= htab_create (31,
636 hash_loaded_script_entry
,
637 eq_loaded_script_entry
,
640 pspace_info
->script_not_found_warning_printed
= FALSE
;
643 /* Wrapper on get_auto_load_pspace_data to also allocate the hash table
644 for loading scripts. */
646 struct auto_load_pspace_info
*
647 get_auto_load_pspace_data_for_loading (struct program_space
*pspace
)
649 struct auto_load_pspace_info
*info
;
651 info
= get_auto_load_pspace_data (pspace
);
652 if (info
->loaded_scripts
== NULL
)
653 init_loaded_scripts_info (info
);
658 /* Add script NAME in LANGUAGE to hash table of PSPACE_INFO. LOADED 1 if the
659 script has been (is going to) be loaded, 0 otherwise (such as if it has not
660 been found). FULL_PATH is NULL if the script wasn't found. The result is
661 true if the script was already in the hash table. */
664 maybe_add_script (struct auto_load_pspace_info
*pspace_info
, int loaded
,
665 const char *name
, const char *full_path
,
666 const struct script_language
*language
)
668 struct htab
*htab
= pspace_info
->loaded_scripts
;
669 struct loaded_script
**slot
, entry
;
673 entry
.language
= language
;
674 slot
= (struct loaded_script
**) htab_find_slot (htab
, &entry
, INSERT
);
675 in_hash_table
= *slot
!= NULL
;
677 /* If this script is not in the hash table, add it. */
683 /* Allocate all space in one chunk so it's easier to free. */
684 *slot
= xmalloc (sizeof (**slot
)
686 + (full_path
!= NULL
? (strlen (full_path
) + 1) : 0));
687 p
= ((char*) *slot
) + sizeof (**slot
);
690 if (full_path
!= NULL
)
693 strcpy (p
, full_path
);
694 (*slot
)->full_path
= p
;
697 (*slot
)->full_path
= NULL
;
698 (*slot
)->loaded
= loaded
;
699 (*slot
)->language
= language
;
702 return in_hash_table
;
705 /* Clear the table of loaded section scripts. */
708 clear_section_scripts (void)
710 struct program_space
*pspace
= current_program_space
;
711 struct auto_load_pspace_info
*info
;
713 info
= program_space_data (pspace
, auto_load_pspace_data
);
714 if (info
!= NULL
&& info
->loaded_scripts
!= NULL
)
716 htab_delete (info
->loaded_scripts
);
717 info
->loaded_scripts
= NULL
;
718 info
->script_not_found_warning_printed
= FALSE
;
722 /* Look for the auto-load script in LANGUAGE associated with OBJFILE where
723 OBJFILE's gdb_realpath is REALNAME and load it. Return 1 if we found any
724 matching script, return 0 otherwise. */
727 auto_load_objfile_script_1 (struct objfile
*objfile
, const char *realname
,
728 const struct script_language
*language
)
730 char *filename
, *debugfile
;
733 struct cleanup
*cleanups
;
735 len
= strlen (realname
);
736 filename
= xmalloc (len
+ strlen (language
->suffix
) + 1);
737 memcpy (filename
, realname
, len
);
738 strcpy (filename
+ len
, language
->suffix
);
740 cleanups
= make_cleanup (xfree
, filename
);
742 input
= gdb_fopen_cloexec (filename
, "r");
743 debugfile
= filename
;
745 fprintf_unfiltered (gdb_stdlog
, _("auto-load: Attempted file \"%s\" %s.\n"),
746 debugfile
, input
? _("exists") : _("does not exist"));
754 /* Also try the same file in a subdirectory of gdb's data
757 vec
= auto_load_expand_dir_vars (auto_load_dir
);
758 make_cleanup_free_char_ptr_vec (vec
);
761 fprintf_unfiltered (gdb_stdlog
, _("auto-load: Searching 'set auto-load "
762 "scripts-directory' path \"%s\".\n"),
765 for (ix
= 0; VEC_iterate (char_ptr
, vec
, ix
, dir
); ++ix
)
767 debugfile
= xmalloc (strlen (dir
) + strlen (filename
) + 1);
768 strcpy (debugfile
, dir
);
770 /* FILENAME is absolute, so we don't need a "/" here. */
771 strcat (debugfile
, filename
);
773 make_cleanup (xfree
, debugfile
);
774 input
= gdb_fopen_cloexec (debugfile
, "r");
776 fprintf_unfiltered (gdb_stdlog
, _("auto-load: Attempted file "
779 input
? _("exists") : _("does not exist"));
787 make_cleanup_fclose (input
);
789 /* To preserve existing behaviour we don't check for whether the
790 script was already in the table, and always load it.
791 It's highly unlikely that we'd ever load it twice,
792 and these scripts are required to be idempotent under multiple
794 language
->source_script_for_objfile (objfile
, input
, debugfile
);
801 do_cleanups (cleanups
);
805 /* Look for the auto-load script in LANGUAGE associated with OBJFILE and load
809 auto_load_objfile_script (struct objfile
*objfile
,
810 const struct script_language
*language
)
812 char *realname
= gdb_realpath (objfile
->name
);
813 struct cleanup
*cleanups
= make_cleanup (xfree
, realname
);
815 if (!auto_load_objfile_script_1 (objfile
, realname
, language
))
817 /* For Windows/DOS .exe executables, strip the .exe suffix, so that
818 FOO-gdb.gdb could be used for FOO.exe, and try again. */
820 size_t len
= strlen (realname
);
821 const size_t lexe
= sizeof (".exe") - 1;
823 if (len
> lexe
&& strcasecmp (realname
+ len
- lexe
, ".exe") == 0)
826 realname
[len
] = '\0';
828 fprintf_unfiltered (gdb_stdlog
, _("auto-load: Stripped .exe suffix, "
829 "retrying with \"%s\".\n"),
831 auto_load_objfile_script_1 (objfile
, realname
, language
);
835 do_cleanups (cleanups
);
838 /* Load any auto-loaded scripts for OBJFILE. */
841 load_auto_scripts_for_objfile (struct objfile
*objfile
)
843 if (!global_auto_load
)
846 if (auto_load_gdb_scripts
)
847 auto_load_objfile_script (objfile
, &script_language_gdb
);
849 gdbpy_load_auto_scripts_for_objfile (objfile
);
852 /* This is a new_objfile observer callback to auto-load scripts.
854 Two flavors of auto-loaded scripts are supported.
855 1) based on the path to the objfile
856 2) from .debug_gdb_scripts section */
859 auto_load_new_objfile (struct objfile
*objfile
)
863 /* OBJFILE is NULL when loading a new "main" symbol-file. */
864 clear_section_scripts ();
868 load_auto_scripts_for_objfile (objfile
);
871 /* Collect scripts to be printed in a vec. */
873 typedef struct loaded_script
*loaded_script_ptr
;
874 DEF_VEC_P (loaded_script_ptr
);
876 struct collect_matching_scripts_data
878 VEC (loaded_script_ptr
) **scripts_p
;
880 const struct script_language
*language
;
883 /* Traversal function for htab_traverse.
884 Collect the entry if it matches the regexp. */
887 collect_matching_scripts (void **slot
, void *info
)
889 struct loaded_script
*script
= *slot
;
890 struct collect_matching_scripts_data
*data
= info
;
892 if (script
->language
== data
->language
&& re_exec (script
->name
))
893 VEC_safe_push (loaded_script_ptr
, *data
->scripts_p
, script
);
901 print_script (struct loaded_script
*script
)
903 struct ui_out
*uiout
= current_uiout
;
904 struct cleanup
*chain
;
906 chain
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
908 ui_out_field_string (uiout
, "loaded", script
->loaded
? "Yes" : "No");
909 ui_out_field_string (uiout
, "script", script
->name
);
910 ui_out_text (uiout
, "\n");
912 /* If the name isn't the full path, print it too. */
913 if (script
->full_path
!= NULL
914 && strcmp (script
->name
, script
->full_path
) != 0)
916 ui_out_text (uiout
, "\tfull name: ");
917 ui_out_field_string (uiout
, "full_path", script
->full_path
);
918 ui_out_text (uiout
, "\n");
924 /* Helper for info_auto_load_scripts to sort the scripts by name. */
927 sort_scripts_by_name (const void *ap
, const void *bp
)
929 const struct loaded_script
*a
= *(const struct loaded_script
**) ap
;
930 const struct loaded_script
*b
= *(const struct loaded_script
**) bp
;
932 return FILENAME_CMP (a
->name
, b
->name
);
935 /* Special internal GDB value of auto_load_info_scripts's PATTERN identify
936 the "info auto-load XXX" command has been executed through the general
937 "info auto-load" invocation. Extra newline will be printed if needed. */
938 char auto_load_info_scripts_pattern_nl
[] = "";
940 /* Implementation for "info auto-load gdb-scripts"
941 (and "info auto-load python-scripts"). List scripts in LANGUAGE matching
942 PATTERN. FROM_TTY is the usual GDB boolean for user interactivity. */
945 auto_load_info_scripts (char *pattern
, int from_tty
,
946 const struct script_language
*language
)
948 struct ui_out
*uiout
= current_uiout
;
949 struct auto_load_pspace_info
*pspace_info
;
950 struct cleanup
*script_chain
;
951 VEC (loaded_script_ptr
) *scripts
;
956 pspace_info
= get_auto_load_pspace_data (current_program_space
);
958 if (pattern
&& *pattern
)
960 char *re_err
= re_comp (pattern
);
963 error (_("Invalid regexp: %s"), re_err
);
970 /* We need to know the number of rows before we build the table.
971 Plus we want to sort the scripts by name.
972 So first traverse the hash table collecting the matching scripts. */
974 scripts
= VEC_alloc (loaded_script_ptr
, 10);
975 script_chain
= make_cleanup (VEC_cleanup (loaded_script_ptr
), &scripts
);
977 if (pspace_info
!= NULL
&& pspace_info
->loaded_scripts
!= NULL
)
979 struct collect_matching_scripts_data data
= { &scripts
, language
};
981 /* Pass a pointer to scripts as VEC_safe_push can realloc space. */
982 htab_traverse_noresize (pspace_info
->loaded_scripts
,
983 collect_matching_scripts
, &data
);
986 nr_scripts
= VEC_length (loaded_script_ptr
, scripts
);
988 /* Table header shifted right by preceding "gdb-scripts: " would not match
990 if (nr_scripts
> 0 && pattern
== auto_load_info_scripts_pattern_nl
)
991 ui_out_text (uiout
, "\n");
993 make_cleanup_ui_out_table_begin_end (uiout
, 2, nr_scripts
,
994 "AutoLoadedScriptsTable");
996 ui_out_table_header (uiout
, 7, ui_left
, "loaded", "Loaded");
997 ui_out_table_header (uiout
, 70, ui_left
, "script", "Script");
998 ui_out_table_body (uiout
);
1003 loaded_script_ptr script
;
1005 qsort (VEC_address (loaded_script_ptr
, scripts
),
1006 VEC_length (loaded_script_ptr
, scripts
),
1007 sizeof (loaded_script_ptr
), sort_scripts_by_name
);
1008 for (i
= 0; VEC_iterate (loaded_script_ptr
, scripts
, i
, script
); ++i
)
1009 print_script (script
);
1012 do_cleanups (script_chain
);
1014 if (nr_scripts
== 0)
1016 if (pattern
&& *pattern
)
1017 ui_out_message (uiout
, 0, "No auto-load scripts matching %s.\n",
1020 ui_out_message (uiout
, 0, "No auto-load scripts.\n");
1024 /* Wrapper for "info auto-load gdb-scripts". */
1027 info_auto_load_gdb_scripts (char *pattern
, int from_tty
)
1029 auto_load_info_scripts (pattern
, from_tty
, &script_language_gdb
);
1032 /* Implement 'info auto-load local-gdbinit'. */
1035 info_auto_load_local_gdbinit (char *args
, int from_tty
)
1037 if (auto_load_local_gdbinit_pathname
== NULL
)
1038 printf_filtered (_("Local .gdbinit file was not found.\n"));
1039 else if (auto_load_local_gdbinit_loaded
)
1040 printf_filtered (_("Local .gdbinit file \"%s\" has been loaded.\n"),
1041 auto_load_local_gdbinit_pathname
);
1043 printf_filtered (_("Local .gdbinit file \"%s\" has not been loaded.\n"),
1044 auto_load_local_gdbinit_pathname
);
1047 /* Return non-zero if SCRIPT_NOT_FOUND_WARNING_PRINTED of PSPACE_INFO was unset
1048 before calling this function. Always set SCRIPT_NOT_FOUND_WARNING_PRINTED
1052 script_not_found_warning_print (struct auto_load_pspace_info
*pspace_info
)
1054 int retval
= !pspace_info
->script_not_found_warning_printed
;
1056 pspace_info
->script_not_found_warning_printed
= 1;
1061 /* The only valid "set auto-load" argument is off|0|no|disable. */
1064 set_auto_load_cmd (char *args
, int from_tty
)
1066 struct cmd_list_element
*list
;
1069 /* See parse_binary_operation in use by the sub-commands. */
1071 length
= args
? strlen (args
) : 0;
1073 while (length
> 0 && (args
[length
- 1] == ' ' || args
[length
- 1] == '\t'))
1076 if (length
== 0 || (strncmp (args
, "off", length
) != 0
1077 && strncmp (args
, "0", length
) != 0
1078 && strncmp (args
, "no", length
) != 0
1079 && strncmp (args
, "disable", length
) != 0))
1080 error (_("Valid is only global 'set auto-load no'; "
1081 "otherwise check the auto-load sub-commands."));
1083 for (list
= *auto_load_set_cmdlist_get (); list
!= NULL
; list
= list
->next
)
1084 if (list
->var_type
== var_boolean
)
1086 gdb_assert (list
->type
== set_cmd
);
1087 do_set_command (args
, from_tty
, list
);
1091 /* Initialize "set auto-load " commands prefix and return it. */
1093 struct cmd_list_element
**
1094 auto_load_set_cmdlist_get (void)
1096 static struct cmd_list_element
*retval
;
1099 add_prefix_cmd ("auto-load", class_maintenance
, set_auto_load_cmd
, _("\
1100 Auto-loading specific settings.\n\
1101 Configure various auto-load-specific variables such as\n\
1102 automatic loading of Python scripts."),
1103 &retval
, "set auto-load ",
1104 1/*allow-unknown*/, &setlist
);
1109 /* Command "show auto-load" displays summary of all the current
1110 "show auto-load " settings. */
1113 show_auto_load_cmd (char *args
, int from_tty
)
1115 cmd_show_list (*auto_load_show_cmdlist_get (), from_tty
, "");
1118 /* Initialize "show auto-load " commands prefix and return it. */
1120 struct cmd_list_element
**
1121 auto_load_show_cmdlist_get (void)
1123 static struct cmd_list_element
*retval
;
1126 add_prefix_cmd ("auto-load", class_maintenance
, show_auto_load_cmd
, _("\
1127 Show auto-loading specific settings.\n\
1128 Show configuration of various auto-load-specific variables such as\n\
1129 automatic loading of Python scripts."),
1130 &retval
, "show auto-load ",
1131 0/*allow-unknown*/, &showlist
);
1136 /* Command "info auto-load" displays whether the various auto-load files have
1137 been loaded. This is reimplementation of cmd_show_list which inserts
1138 newlines at proper places. */
1141 info_auto_load_cmd (char *args
, int from_tty
)
1143 struct cmd_list_element
*list
;
1144 struct cleanup
*infolist_chain
;
1145 struct ui_out
*uiout
= current_uiout
;
1147 infolist_chain
= make_cleanup_ui_out_tuple_begin_end (uiout
, "infolist");
1149 for (list
= *auto_load_info_cmdlist_get (); list
!= NULL
; list
= list
->next
)
1151 struct cleanup
*option_chain
1152 = make_cleanup_ui_out_tuple_begin_end (uiout
, "option");
1154 gdb_assert (!list
->prefixlist
);
1155 gdb_assert (list
->type
== not_set_cmd
);
1157 ui_out_field_string (uiout
, "name", list
->name
);
1158 ui_out_text (uiout
, ": ");
1159 cmd_func (list
, auto_load_info_scripts_pattern_nl
, from_tty
);
1161 /* Close the tuple. */
1162 do_cleanups (option_chain
);
1165 /* Close the tuple. */
1166 do_cleanups (infolist_chain
);
1169 /* Initialize "info auto-load " commands prefix and return it. */
1171 struct cmd_list_element
**
1172 auto_load_info_cmdlist_get (void)
1174 static struct cmd_list_element
*retval
;
1177 add_prefix_cmd ("auto-load", class_info
, info_auto_load_cmd
, _("\
1178 Print current status of auto-loaded files.\n\
1179 Print whether various files like Python scripts or .gdbinit files have been\n\
1180 found and/or loaded."),
1181 &retval
, "info auto-load ",
1182 0/*allow-unknown*/, &infolist
);
1187 void _initialize_auto_load (void);
1190 _initialize_auto_load (void)
1192 struct cmd_list_element
*cmd
;
1193 char *scripts_directory_help
;
1195 auto_load_pspace_data
1196 = register_program_space_data_with_cleanup (NULL
,
1197 auto_load_pspace_data_cleanup
);
1199 observer_attach_new_objfile (auto_load_new_objfile
);
1201 add_setshow_boolean_cmd ("gdb-scripts", class_support
,
1202 &auto_load_gdb_scripts
, _("\
1203 Enable or disable auto-loading of canned sequences of commands scripts."), _("\
1204 Show whether auto-loading of canned sequences of commands scripts is enabled."),
1206 If enabled, canned sequences of commands are loaded when the debugger reads\n\
1207 an executable or shared library.\n\
1208 This options has security implications for untrusted inferiors."),
1209 NULL
, show_auto_load_gdb_scripts
,
1210 auto_load_set_cmdlist_get (),
1211 auto_load_show_cmdlist_get ());
1213 add_cmd ("gdb-scripts", class_info
, info_auto_load_gdb_scripts
,
1214 _("Print the list of automatically loaded sequences of commands.\n\
1215 Usage: info auto-load gdb-scripts [REGEXP]"),
1216 auto_load_info_cmdlist_get ());
1218 add_setshow_boolean_cmd ("local-gdbinit", class_support
,
1219 &auto_load_local_gdbinit
, _("\
1220 Enable or disable auto-loading of .gdbinit script in current directory."), _("\
1221 Show whether auto-loading .gdbinit script in current directory is enabled."),
1223 If enabled, canned sequences of commands are loaded when debugger starts\n\
1224 from .gdbinit file in current directory. Such files are deprecated,\n\
1225 use a script associated with inferior executable file instead.\n\
1226 This options has security implications for untrusted inferiors."),
1227 NULL
, show_auto_load_local_gdbinit
,
1228 auto_load_set_cmdlist_get (),
1229 auto_load_show_cmdlist_get ());
1231 add_cmd ("local-gdbinit", class_info
, info_auto_load_local_gdbinit
,
1232 _("Print whether current directory .gdbinit file has been loaded.\n\
1233 Usage: info auto-load local-gdbinit"),
1234 auto_load_info_cmdlist_get ());
1236 auto_load_dir
= xstrdup (AUTO_LOAD_DIR
);
1237 scripts_directory_help
= xstrprintf (
1240 Automatically loaded Python scripts (named OBJFILE%s) and GDB scripts\n\
1241 (named OBJFILE%s) are located in one of the directories listed by this\n\
1244 GDBPY_AUTO_FILE_NAME
,
1247 Automatically loaded GDB scripts (named OBJFILE%s) are located in one\n\
1248 of the directories listed by this option.\n\
1253 This option is ignored for the kinds of scripts \
1254 having 'set auto-load ... off'.\n\
1255 Directories listed here need to be present also \
1256 in the 'set auto-load safe-path'\n\
1258 add_setshow_optional_filename_cmd ("scripts-directory", class_support
,
1259 &auto_load_dir
, _("\
1260 Set the list of directories from which to load auto-loaded scripts."), _("\
1261 Show the list of directories from which to load auto-loaded scripts."),
1262 scripts_directory_help
,
1263 set_auto_load_dir
, show_auto_load_dir
,
1264 auto_load_set_cmdlist_get (),
1265 auto_load_show_cmdlist_get ());
1266 xfree (scripts_directory_help
);
1268 auto_load_safe_path
= xstrdup (AUTO_LOAD_SAFE_PATH
);
1269 auto_load_safe_path_vec_update ();
1270 add_setshow_optional_filename_cmd ("safe-path", class_support
,
1271 &auto_load_safe_path
, _("\
1272 Set the list of files and directories that are safe for auto-loading."), _("\
1273 Show the list of files and directories that are safe for auto-loading."), _("\
1274 Various files loaded automatically for the 'set auto-load ...' options must\n\
1275 be located in one of the directories listed by this option. Warning will be\n\
1276 printed and file will not be used otherwise.\n\
1277 You can mix both directory and filename entries.\n\
1278 Setting this parameter to an empty list resets it to its default value.\n\
1279 Setting this parameter to '/' (without the quotes) allows any file\n\
1280 for the 'set auto-load ...' options. Each path entry can be also shell\n\
1281 wildcard pattern; '*' does not match directory separator.\n\
1282 This option is ignored for the kinds of files having 'set auto-load ... off'.\n\
1283 This options has security implications for untrusted inferiors."),
1284 set_auto_load_safe_path
,
1285 show_auto_load_safe_path
,
1286 auto_load_set_cmdlist_get (),
1287 auto_load_show_cmdlist_get ());
1288 observer_attach_gdb_datadir_changed (auto_load_gdb_datadir_changed
);
1290 cmd
= add_cmd ("add-auto-load-safe-path", class_support
,
1291 add_auto_load_safe_path
,
1292 _("Add entries to the list of directories from which it is safe "
1293 "to auto-load files.\n\
1294 See the commands 'set auto-load safe-path' and 'show auto-load safe-path' to\n\
1295 access the current full list setting."),
1297 set_cmd_completer (cmd
, filename_completer
);
1299 add_setshow_boolean_cmd ("auto-load", class_maintenance
,
1300 &debug_auto_load
, _("\
1301 Set auto-load verifications debugging."), _("\
1302 Show auto-load verifications debugging."), _("\
1303 When non-zero, debugging output for files of 'set auto-load ...'\n\
1305 NULL
, show_debug_auto_load
,
1306 &setdebuglist
, &showdebuglist
);