1 /* GDB routines for supporting auto-loaded scripts.
3 Copyright (C) 2012 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"
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"
44 static void source_gdb_script_for_objfile (struct objfile
*objfile
, FILE *file
,
45 const char *filename
);
47 /* Value of the 'set debug auto-load' configuration variable. */
48 static int debug_auto_load
= 0;
50 /* "show" command for the debug_auto_load configuration variable. */
53 show_debug_auto_load (struct ui_file
*file
, int from_tty
,
54 struct cmd_list_element
*c
, const char *value
)
56 fprintf_filtered (file
, _("Debugging output for files "
57 "of 'set auto-load ...' is %s.\n"),
61 /* User-settable option to enable/disable auto-loading of GDB_AUTO_FILE_NAME
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;
68 /* "show" command for the auto_load_gdb_scripts configuration variable. */
71 show_auto_load_gdb_scripts (struct ui_file
*file
, int from_tty
,
72 struct cmd_list_element
*c
, const char *value
)
74 fprintf_filtered (file
, _("Auto-loading of canned sequences of commands "
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,
83 Both auto_load_scripts && global_auto_load must be true to enable
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;
91 /* Auto-load .gdbinit file from the current directory? */
92 int auto_load_local_gdbinit
= 1;
94 /* Absolute pathname to the current directory .gdbinit, if it exists. */
95 char *auto_load_local_gdbinit_pathname
= NULL
;
97 /* Boolean value if AUTO_LOAD_LOCAL_GDBINIT_PATHNAME has been loaded. */
98 int auto_load_local_gdbinit_loaded
= 0;
100 /* "show" command for the auto_load_local_gdbinit configuration variable. */
103 show_auto_load_local_gdbinit (struct ui_file
*file
, int from_tty
,
104 struct cmd_list_element
*c
, const char *value
)
106 fprintf_filtered (file
, _("Auto-loading of .gdbinit script from current "
107 "directory is %s.\n"),
111 /* Directory list from which to load auto-loaded scripts. It is not checked
112 for absolute paths but they are strongly recommended. It is initialized by
113 _initialize_auto_load. */
114 static char *auto_load_dir
;
116 /* "set" command for the auto_load_dir configuration variable. */
119 set_auto_load_dir (char *args
, int from_tty
, struct cmd_list_element
*c
)
121 /* Setting the variable to "" resets it to the compile time defaults. */
122 if (auto_load_dir
[0] == '\0')
124 xfree (auto_load_dir
);
125 auto_load_dir
= xstrdup (AUTO_LOAD_DIR
);
129 /* "show" command for the auto_load_dir configuration variable. */
132 show_auto_load_dir (struct ui_file
*file
, int from_tty
,
133 struct cmd_list_element
*c
, const char *value
)
135 fprintf_filtered (file
, _("List of directories from which to load "
136 "auto-loaded scripts is %s.\n"),
140 /* Directory list safe to hold auto-loaded files. It is not checked for
141 absolute paths but they are strongly recommended. It is initialized by
142 _initialize_auto_load. */
143 static char *auto_load_safe_path
;
145 /* Vector of directory elements of AUTO_LOAD_SAFE_PATH with each one normalized
146 by tilde_expand and possibly each entries has added its gdb_realpath
148 static VEC (char_ptr
) *auto_load_safe_path_vec
;
150 /* Update auto_load_safe_path_vec from current AUTO_LOAD_SAFE_PATH. */
153 auto_load_safe_path_vec_update (void)
155 VEC (char_ptr
) *dir_vec
= NULL
;
160 fprintf_unfiltered (gdb_stdlog
,
161 _("auto-load: Updating directories of \"%s\".\n"),
162 auto_load_safe_path
);
164 free_char_ptr_vec (auto_load_safe_path_vec
);
166 auto_load_safe_path_vec
= dirnames_to_char_ptr_vec (auto_load_safe_path
);
167 len
= VEC_length (char_ptr
, auto_load_safe_path_vec
);
169 /* Apply tilde_expand and gdb_realpath to each AUTO_LOAD_SAFE_PATH_VEC
171 for (ix
= 0; ix
< len
; ix
++)
173 char *dir
= VEC_index (char_ptr
, auto_load_safe_path_vec
, ix
);
174 char *ddir_subst
, *expanded
, *real_path
;
176 ddir_subst
= xstrdup (dir
);
177 substitute_path_component (&ddir_subst
, "$datadir", gdb_datadir
);
178 expanded
= tilde_expand (ddir_subst
);
180 real_path
= gdb_realpath (expanded
);
182 /* Ensure the current entry is at least a valid path (therefore
183 $datadir-expanded and tilde-expanded). */
184 VEC_replace (char_ptr
, auto_load_safe_path_vec
, ix
, expanded
);
188 if (strcmp (expanded
, dir
) == 0)
189 fprintf_unfiltered (gdb_stdlog
,
190 _("auto-load: Using directory \"%s\".\n"),
193 fprintf_unfiltered (gdb_stdlog
,
194 _("auto-load: Resolved directory \"%s\" "
200 /* If gdb_realpath returns a different content, append it. */
201 if (strcmp (real_path
, expanded
) == 0)
205 VEC_safe_push (char_ptr
, auto_load_safe_path_vec
, real_path
);
208 fprintf_unfiltered (gdb_stdlog
,
209 _("auto-load: And canonicalized as \"%s\".\n"),
215 /* Variable gdb_datadir has been set. Update content depending on $datadir. */
218 auto_load_gdb_datadir_changed (void)
220 auto_load_safe_path_vec_update ();
223 /* "set" command for the auto_load_safe_path configuration variable. */
226 set_auto_load_safe_path (char *args
, int from_tty
, struct cmd_list_element
*c
)
228 /* Setting the variable to "" resets it to the compile time defaults. */
229 if (auto_load_safe_path
[0] == '\0')
231 xfree (auto_load_safe_path
);
232 auto_load_safe_path
= xstrdup (AUTO_LOAD_SAFE_PATH
);
235 auto_load_safe_path_vec_update ();
238 /* "show" command for the auto_load_safe_path configuration variable. */
241 show_auto_load_safe_path (struct ui_file
*file
, int from_tty
,
242 struct cmd_list_element
*c
, const char *value
)
246 /* Check if user has entered either "/" or for example ":".
247 But while more complicate content like ":/foo" would still also
248 permit any location do not hide those. */
250 for (cs
= value
; *cs
&& (*cs
== DIRNAME_SEPARATOR
|| IS_DIR_SEPARATOR (*cs
));
253 fprintf_filtered (file
, _("Auto-load files are safe to load from any "
256 fprintf_filtered (file
, _("List of directories from which it is safe to "
257 "auto-load files is %s.\n"),
261 /* "add-auto-load-safe-path" command for the auto_load_safe_path configuration
265 add_auto_load_safe_path (char *args
, int from_tty
)
269 if (args
== NULL
|| *args
== 0)
271 Directory argument required.\n\
272 Use 'set auto-load safe-path /' for disabling the auto-load safe-path security.\
275 s
= xstrprintf ("%s%c%s", auto_load_safe_path
, DIRNAME_SEPARATOR
, args
);
276 xfree (auto_load_safe_path
);
277 auto_load_safe_path
= s
;
279 auto_load_safe_path_vec_update ();
282 /* Return 1 if FILENAME is equal to DIR or if FILENAME belongs to the
283 subdirectory DIR. Return 0 otherwise. gdb_realpath normalization is never
286 static ATTRIBUTE_PURE
int
287 filename_is_in_dir (const char *filename
, const char *dir
)
289 size_t dir_len
= strlen (dir
);
291 while (dir_len
&& IS_DIR_SEPARATOR (dir
[dir_len
- 1]))
294 /* Ensure auto_load_safe_path "/" matches any FILENAME. On MS-Windows
295 platform FILENAME even after gdb_realpath does not have to start with
296 IS_DIR_SEPARATOR character, such as the 'C:\x.exe' filename. */
300 return (filename_ncmp (dir
, filename
, dir_len
) == 0
301 && (IS_DIR_SEPARATOR (filename
[dir_len
])
302 || filename
[dir_len
] == '\0'));
305 /* Return 1 if FILENAME belongs to one of directory components of
306 AUTO_LOAD_SAFE_PATH_VEC. Return 0 otherwise.
307 auto_load_safe_path_vec_update is never called.
308 *FILENAME_REALP may be updated by gdb_realpath of FILENAME - it has to be
309 freed by the caller. */
312 filename_is_in_auto_load_safe_path_vec (const char *filename
,
313 char **filename_realp
)
318 for (ix
= 0; VEC_iterate (char_ptr
, auto_load_safe_path_vec
, ix
, dir
); ++ix
)
319 if (*filename_realp
== NULL
&& filename_is_in_dir (filename
, dir
))
324 if (*filename_realp
== NULL
)
326 *filename_realp
= gdb_realpath (filename
);
327 if (debug_auto_load
&& strcmp (*filename_realp
, filename
) != 0)
328 fprintf_unfiltered (gdb_stdlog
,
329 _("auto-load: Resolved "
330 "file \"%s\" as \"%s\".\n"),
331 filename
, *filename_realp
);
334 if (strcmp (*filename_realp
, filename
) != 0)
335 for (ix
= 0; VEC_iterate (char_ptr
, auto_load_safe_path_vec
, ix
, dir
);
337 if (filename_is_in_dir (*filename_realp
, dir
))
344 fprintf_unfiltered (gdb_stdlog
, _("auto-load: File \"%s\" matches "
345 "directory \"%s\".\n"),
353 /* Return 1 if FILENAME is located in one of the directories of
354 AUTO_LOAD_SAFE_PATH. Otherwise call warning and return 0. FILENAME does
355 not have to be an absolute path.
357 Existence of FILENAME is not checked. Function will still give a warning
358 even if the caller would quietly skip non-existing file in unsafe
362 file_is_auto_load_safe (const char *filename
, const char *debug_fmt
, ...)
364 char *filename_real
= NULL
;
365 struct cleanup
*back_to
;
371 va_start (debug_args
, debug_fmt
);
372 vfprintf_unfiltered (gdb_stdlog
, debug_fmt
, debug_args
);
376 back_to
= make_cleanup (free_current_contents
, &filename_real
);
378 if (filename_is_in_auto_load_safe_path_vec (filename
, &filename_real
))
380 do_cleanups (back_to
);
384 auto_load_safe_path_vec_update ();
385 if (filename_is_in_auto_load_safe_path_vec (filename
, &filename_real
))
387 do_cleanups (back_to
);
391 warning (_("File \"%s\" auto-loading has been declined by your "
392 "`auto-load safe-path' set to \"%s\"."),
393 filename_real
, auto_load_safe_path
);
395 do_cleanups (back_to
);
399 /* Definition of script language for GDB canned sequences of commands. */
401 static const struct script_language script_language_gdb
402 = { GDB_AUTO_FILE_NAME
, source_gdb_script_for_objfile
};
405 source_gdb_script_for_objfile (struct objfile
*objfile
, FILE *file
,
406 const char *filename
)
409 struct auto_load_pspace_info
*pspace_info
;
410 volatile struct gdb_exception e
;
412 is_safe
= file_is_auto_load_safe (filename
, _("auto-load: Loading canned "
413 "sequences of commands script "
414 "\"%s\" for objfile \"%s\".\n"),
415 filename
, objfile
->name
);
417 /* Add this script to the hash table too so "info auto-load gdb-scripts"
419 pspace_info
= get_auto_load_pspace_data_for_loading (current_program_space
);
420 maybe_add_script (pspace_info
, is_safe
, filename
, filename
,
421 &script_language_gdb
);
426 TRY_CATCH (e
, RETURN_MASK_ALL
)
428 script_from_file (file
, filename
);
430 exception_print (gdb_stderr
, e
);
433 /* For scripts specified in .debug_gdb_scripts, multiple objfiles may load
434 the same script. There's no point in loading the script multiple times,
435 and there can be a lot of objfiles and scripts, so we keep track of scripts
438 struct auto_load_pspace_info
440 /* For each program space we keep track of loaded scripts. */
441 struct htab
*loaded_scripts
;
443 /* Non-zero if we've issued the warning about an auto-load script not being
444 found. We only want to issue this warning once. */
445 int script_not_found_warning_printed
;
448 /* Objects of this type are stored in the loaded script hash table. */
452 /* Name as provided by the objfile. */
455 /* Full path name or NULL if script wasn't found (or was otherwise
457 const char *full_path
;
459 /* Non-zero if this script has been loaded. */
462 const struct script_language
*language
;
465 /* Per-program-space data key. */
466 static const struct program_space_data
*auto_load_pspace_data
;
469 auto_load_pspace_data_cleanup (struct program_space
*pspace
, void *arg
)
471 struct auto_load_pspace_info
*info
;
473 info
= program_space_data (pspace
, auto_load_pspace_data
);
476 if (info
->loaded_scripts
)
477 htab_delete (info
->loaded_scripts
);
482 /* Get the current autoload data. If none is found yet, add it now. This
483 function always returns a valid object. */
485 static struct auto_load_pspace_info
*
486 get_auto_load_pspace_data (struct program_space
*pspace
)
488 struct auto_load_pspace_info
*info
;
490 info
= program_space_data (pspace
, auto_load_pspace_data
);
493 info
= XZALLOC (struct auto_load_pspace_info
);
494 set_program_space_data (pspace
, auto_load_pspace_data
, info
);
500 /* Hash function for the loaded script hash. */
503 hash_loaded_script_entry (const void *data
)
505 const struct loaded_script
*e
= data
;
507 return htab_hash_string (e
->name
) ^ htab_hash_pointer (e
->language
);
510 /* Equality function for the loaded script hash. */
513 eq_loaded_script_entry (const void *a
, const void *b
)
515 const struct loaded_script
*ea
= a
;
516 const struct loaded_script
*eb
= b
;
518 return strcmp (ea
->name
, eb
->name
) == 0 && ea
->language
== eb
->language
;
521 /* Initialize the table to track loaded scripts.
522 Each entry is hashed by the full path name. */
525 init_loaded_scripts_info (struct auto_load_pspace_info
*pspace_info
)
527 /* Choose 31 as the starting size of the hash table, somewhat arbitrarily.
528 Space for each entry is obtained with one malloc so we can free them
531 pspace_info
->loaded_scripts
= htab_create (31,
532 hash_loaded_script_entry
,
533 eq_loaded_script_entry
,
536 pspace_info
->script_not_found_warning_printed
= FALSE
;
539 /* Wrapper on get_auto_load_pspace_data to also allocate the hash table
540 for loading scripts. */
542 struct auto_load_pspace_info
*
543 get_auto_load_pspace_data_for_loading (struct program_space
*pspace
)
545 struct auto_load_pspace_info
*info
;
547 info
= get_auto_load_pspace_data (pspace
);
548 if (info
->loaded_scripts
== NULL
)
549 init_loaded_scripts_info (info
);
554 /* Add script NAME in LANGUAGE to hash table of PSPACE_INFO. LOADED 1 if the
555 script has been (is going to) be loaded, 0 otherwise (such as if it has not
556 been found). FULL_PATH is NULL if the script wasn't found. The result is
557 true if the script was already in the hash table. */
560 maybe_add_script (struct auto_load_pspace_info
*pspace_info
, int loaded
,
561 const char *name
, const char *full_path
,
562 const struct script_language
*language
)
564 struct htab
*htab
= pspace_info
->loaded_scripts
;
565 struct loaded_script
**slot
, entry
;
569 entry
.language
= language
;
570 slot
= (struct loaded_script
**) htab_find_slot (htab
, &entry
, INSERT
);
571 in_hash_table
= *slot
!= NULL
;
573 /* If this script is not in the hash table, add it. */
579 /* Allocate all space in one chunk so it's easier to free. */
580 *slot
= xmalloc (sizeof (**slot
)
582 + (full_path
!= NULL
? (strlen (full_path
) + 1) : 0));
583 p
= ((char*) *slot
) + sizeof (**slot
);
586 if (full_path
!= NULL
)
589 strcpy (p
, full_path
);
590 (*slot
)->full_path
= p
;
593 (*slot
)->full_path
= NULL
;
594 (*slot
)->loaded
= loaded
;
595 (*slot
)->language
= language
;
598 return in_hash_table
;
601 /* Clear the table of loaded section scripts. */
604 clear_section_scripts (void)
606 struct program_space
*pspace
= current_program_space
;
607 struct auto_load_pspace_info
*info
;
609 info
= program_space_data (pspace
, auto_load_pspace_data
);
610 if (info
!= NULL
&& info
->loaded_scripts
!= NULL
)
612 htab_delete (info
->loaded_scripts
);
613 info
->loaded_scripts
= NULL
;
614 info
->script_not_found_warning_printed
= FALSE
;
618 /* Look for the auto-load script in LANGUAGE associated with OBJFILE and load
622 auto_load_objfile_script (struct objfile
*objfile
,
623 const struct script_language
*language
)
626 char *filename
, *debugfile
;
629 struct cleanup
*cleanups
;
631 realname
= gdb_realpath (objfile
->name
);
632 len
= strlen (realname
);
633 filename
= xmalloc (len
+ strlen (language
->suffix
) + 1);
634 memcpy (filename
, realname
, len
);
635 strcpy (filename
+ len
, language
->suffix
);
637 cleanups
= make_cleanup (xfree
, filename
);
638 make_cleanup (xfree
, realname
);
640 input
= fopen (filename
, "r");
641 debugfile
= filename
;
643 fprintf_unfiltered (gdb_stdlog
, _("auto-load: Attempted file \"%s\" %s.\n"),
644 debugfile
, input
? _("exists") : _("does not exist"));
649 VEC (char_ptr
) *debugdir_vec
;
652 debugdir_vec
= dirnames_to_char_ptr_vec (debug_file_directory
);
653 make_cleanup_free_char_ptr_vec (debugdir_vec
);
656 fprintf_unfiltered (gdb_stdlog
,
657 _("auto-load: Searching 'set debug-file-directory' "
659 debug_file_directory
);
661 for (ix
= 0; VEC_iterate (char_ptr
, debugdir_vec
, ix
, debugdir
); ++ix
)
663 /* Also try the same file in the separate debug info directory. */
664 debugfile
= xmalloc (strlen (debugdir
) + strlen (filename
) + 1);
665 strcpy (debugfile
, debugdir
);
667 /* FILENAME is absolute, so we don't need a "/" here. */
668 strcat (debugfile
, filename
);
670 make_cleanup (xfree
, debugfile
);
671 input
= fopen (debugfile
, "r");
673 fprintf_unfiltered (gdb_stdlog
, _("auto-load: Attempted file "
676 input
? _("exists") : _("does not exist"));
688 /* Also try the same file in a subdirectory of gdb's data
691 vec
= dirnames_to_char_ptr_vec (auto_load_dir
);
692 make_cleanup_free_char_ptr_vec (vec
);
695 fprintf_unfiltered (gdb_stdlog
, _("auto-load: Searching 'set auto-load "
696 "scripts-directory' path \"%s\".\n"),
699 for (ix
= 0; VEC_iterate (char_ptr
, vec
, ix
, dir
); ++ix
)
701 debugfile
= xstrdup (dir
);
702 substitute_path_component (&debugfile
, "$datadir", gdb_datadir
);
703 debugfile
= xrealloc (debugfile
, (strlen (debugfile
)
704 + strlen (filename
) + 1));
706 /* FILENAME is absolute, so we don't need a "/" here. */
707 strcat (debugfile
, filename
);
709 make_cleanup (xfree
, debugfile
);
710 input
= fopen (debugfile
, "r");
712 fprintf_unfiltered (gdb_stdlog
, _("auto-load: Attempted file "
715 input
? _("exists") : _("does not exist"));
723 make_cleanup_fclose (input
);
725 /* To preserve existing behaviour we don't check for whether the
726 script was already in the table, and always load it.
727 It's highly unlikely that we'd ever load it twice,
728 and these scripts are required to be idempotent under multiple
730 language
->source_script_for_objfile (objfile
, input
, debugfile
);
733 do_cleanups (cleanups
);
736 /* Load any auto-loaded scripts for OBJFILE. */
739 load_auto_scripts_for_objfile (struct objfile
*objfile
)
741 if (!global_auto_load
)
744 if (auto_load_gdb_scripts
)
745 auto_load_objfile_script (objfile
, &script_language_gdb
);
747 gdbpy_load_auto_scripts_for_objfile (objfile
);
750 /* This is a new_objfile observer callback to auto-load scripts.
752 Two flavors of auto-loaded scripts are supported.
753 1) based on the path to the objfile
754 2) from .debug_gdb_scripts section */
757 auto_load_new_objfile (struct objfile
*objfile
)
761 /* OBJFILE is NULL when loading a new "main" symbol-file. */
762 clear_section_scripts ();
766 load_auto_scripts_for_objfile (objfile
);
769 /* Collect scripts to be printed in a vec. */
771 typedef struct loaded_script
*loaded_script_ptr
;
772 DEF_VEC_P (loaded_script_ptr
);
774 struct collect_matching_scripts_data
776 VEC (loaded_script_ptr
) **scripts_p
;
778 const struct script_language
*language
;
781 /* Traversal function for htab_traverse.
782 Collect the entry if it matches the regexp. */
785 collect_matching_scripts (void **slot
, void *info
)
787 struct loaded_script
*script
= *slot
;
788 struct collect_matching_scripts_data
*data
= info
;
790 if (script
->language
== data
->language
&& re_exec (script
->name
))
791 VEC_safe_push (loaded_script_ptr
, *data
->scripts_p
, script
);
799 print_script (struct loaded_script
*script
)
801 struct ui_out
*uiout
= current_uiout
;
802 struct cleanup
*chain
;
804 chain
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
806 ui_out_field_string (uiout
, "loaded", script
->loaded
? "Yes" : "No");
807 ui_out_field_string (uiout
, "script", script
->name
);
808 ui_out_text (uiout
, "\n");
810 /* If the name isn't the full path, print it too. */
811 if (script
->full_path
!= NULL
812 && strcmp (script
->name
, script
->full_path
) != 0)
814 ui_out_text (uiout
, "\tfull name: ");
815 ui_out_field_string (uiout
, "full_path", script
->full_path
);
816 ui_out_text (uiout
, "\n");
822 /* Helper for info_auto_load_scripts to sort the scripts by name. */
825 sort_scripts_by_name (const void *ap
, const void *bp
)
827 const struct loaded_script
*a
= *(const struct loaded_script
**) ap
;
828 const struct loaded_script
*b
= *(const struct loaded_script
**) bp
;
830 return FILENAME_CMP (a
->name
, b
->name
);
833 /* Special internal GDB value of auto_load_info_scripts's PATTERN identify
834 the "info auto-load XXX" command has been executed through the general
835 "info auto-load" invocation. Extra newline will be printed if needed. */
836 char auto_load_info_scripts_pattern_nl
[] = "";
838 /* Implementation for "info auto-load gdb-scripts"
839 (and "info auto-load python-scripts"). List scripts in LANGUAGE matching
840 PATTERN. FROM_TTY is the usual GDB boolean for user interactivity. */
843 auto_load_info_scripts (char *pattern
, int from_tty
,
844 const struct script_language
*language
)
846 struct ui_out
*uiout
= current_uiout
;
847 struct auto_load_pspace_info
*pspace_info
;
848 struct cleanup
*script_chain
;
849 VEC (loaded_script_ptr
) *scripts
;
854 pspace_info
= get_auto_load_pspace_data (current_program_space
);
856 if (pattern
&& *pattern
)
858 char *re_err
= re_comp (pattern
);
861 error (_("Invalid regexp: %s"), re_err
);
868 /* We need to know the number of rows before we build the table.
869 Plus we want to sort the scripts by name.
870 So first traverse the hash table collecting the matching scripts. */
872 scripts
= VEC_alloc (loaded_script_ptr
, 10);
873 script_chain
= make_cleanup (VEC_cleanup (loaded_script_ptr
), &scripts
);
875 if (pspace_info
!= NULL
&& pspace_info
->loaded_scripts
!= NULL
)
877 struct collect_matching_scripts_data data
= { &scripts
, language
};
880 /* Pass a pointer to scripts as VEC_safe_push can realloc space. */
881 htab_traverse_noresize (pspace_info
->loaded_scripts
,
882 collect_matching_scripts
, &data
);
886 nr_scripts
= VEC_length (loaded_script_ptr
, scripts
);
888 /* Table header shifted right by preceding "gdb-scripts: " would not match
890 if (nr_scripts
> 0 && pattern
== auto_load_info_scripts_pattern_nl
)
891 ui_out_text (uiout
, "\n");
893 make_cleanup_ui_out_table_begin_end (uiout
, 2, nr_scripts
,
894 "AutoLoadedScriptsTable");
896 ui_out_table_header (uiout
, 7, ui_left
, "loaded", "Loaded");
897 ui_out_table_header (uiout
, 70, ui_left
, "script", "Script");
898 ui_out_table_body (uiout
);
903 loaded_script_ptr script
;
905 qsort (VEC_address (loaded_script_ptr
, scripts
),
906 VEC_length (loaded_script_ptr
, scripts
),
907 sizeof (loaded_script_ptr
), sort_scripts_by_name
);
908 for (i
= 0; VEC_iterate (loaded_script_ptr
, scripts
, i
, script
); ++i
)
909 print_script (script
);
912 do_cleanups (script_chain
);
916 if (pattern
&& *pattern
)
917 ui_out_message (uiout
, 0, "No auto-load scripts matching %s.\n",
920 ui_out_message (uiout
, 0, "No auto-load scripts.\n");
924 /* Wrapper for "info auto-load gdb-scripts". */
927 info_auto_load_gdb_scripts (char *pattern
, int from_tty
)
929 auto_load_info_scripts (pattern
, from_tty
, &script_language_gdb
);
932 /* Implement 'info auto-load local-gdbinit'. */
935 info_auto_load_local_gdbinit (char *args
, int from_tty
)
937 if (auto_load_local_gdbinit_pathname
== NULL
)
938 printf_filtered (_("Local .gdbinit file was not found.\n"));
939 else if (auto_load_local_gdbinit_loaded
)
940 printf_filtered (_("Local .gdbinit file \"%s\" has been loaded.\n"),
941 auto_load_local_gdbinit_pathname
);
943 printf_filtered (_("Local .gdbinit file \"%s\" has not been loaded.\n"),
944 auto_load_local_gdbinit_pathname
);
947 /* Return non-zero if SCRIPT_NOT_FOUND_WARNING_PRINTED of PSPACE_INFO was unset
948 before calling this function. Always set SCRIPT_NOT_FOUND_WARNING_PRINTED
952 script_not_found_warning_print (struct auto_load_pspace_info
*pspace_info
)
954 int retval
= !pspace_info
->script_not_found_warning_printed
;
956 pspace_info
->script_not_found_warning_printed
= 1;
961 /* The only valid "set auto-load" argument is off|0|no|disable. */
964 set_auto_load_cmd (char *args
, int from_tty
)
966 struct cmd_list_element
*list
;
969 /* See parse_binary_operation in use by the sub-commands. */
971 length
= args
? strlen (args
) : 0;
973 while (length
> 0 && (args
[length
- 1] == ' ' || args
[length
- 1] == '\t'))
976 if (length
== 0 || (strncmp (args
, "off", length
) != 0
977 && strncmp (args
, "0", length
) != 0
978 && strncmp (args
, "no", length
) != 0
979 && strncmp (args
, "disable", length
) != 0))
980 error (_("Valid is only global 'set auto-load no'; "
981 "otherwise check the auto-load sub-commands."));
983 for (list
= *auto_load_set_cmdlist_get (); list
!= NULL
; list
= list
->next
)
984 if (list
->var_type
== var_boolean
)
986 gdb_assert (list
->type
== set_cmd
);
987 do_setshow_command (args
, from_tty
, list
);
991 /* Initialize "set auto-load " commands prefix and return it. */
993 struct cmd_list_element
**
994 auto_load_set_cmdlist_get (void)
996 static struct cmd_list_element
*retval
;
999 add_prefix_cmd ("auto-load", class_maintenance
, set_auto_load_cmd
, _("\
1000 Auto-loading specific settings.\n\
1001 Configure various auto-load-specific variables such as\n\
1002 automatic loading of Python scripts."),
1003 &retval
, "set auto-load ",
1004 1/*allow-unknown*/, &setlist
);
1009 /* Command "show auto-load" displays summary of all the current
1010 "show auto-load " settings. */
1013 show_auto_load_cmd (char *args
, int from_tty
)
1015 cmd_show_list (*auto_load_show_cmdlist_get (), from_tty
, "");
1018 /* Initialize "show auto-load " commands prefix and return it. */
1020 struct cmd_list_element
**
1021 auto_load_show_cmdlist_get (void)
1023 static struct cmd_list_element
*retval
;
1026 add_prefix_cmd ("auto-load", class_maintenance
, show_auto_load_cmd
, _("\
1027 Show auto-loading specific settings.\n\
1028 Show configuration of various auto-load-specific variables such as\n\
1029 automatic loading of Python scripts."),
1030 &retval
, "show auto-load ",
1031 0/*allow-unknown*/, &showlist
);
1036 /* Command "info auto-load" displays whether the various auto-load files have
1037 been loaded. This is reimplementation of cmd_show_list which inserts
1038 newlines at proper places. */
1041 info_auto_load_cmd (char *args
, int from_tty
)
1043 struct cmd_list_element
*list
;
1044 struct cleanup
*infolist_chain
;
1045 struct ui_out
*uiout
= current_uiout
;
1047 infolist_chain
= make_cleanup_ui_out_tuple_begin_end (uiout
, "infolist");
1049 for (list
= *auto_load_info_cmdlist_get (); list
!= NULL
; list
= list
->next
)
1051 struct cleanup
*option_chain
1052 = make_cleanup_ui_out_tuple_begin_end (uiout
, "option");
1054 gdb_assert (!list
->prefixlist
);
1055 gdb_assert (list
->type
== not_set_cmd
);
1057 ui_out_field_string (uiout
, "name", list
->name
);
1058 ui_out_text (uiout
, ": ");
1059 cmd_func (list
, auto_load_info_scripts_pattern_nl
, from_tty
);
1061 /* Close the tuple. */
1062 do_cleanups (option_chain
);
1065 /* Close the tuple. */
1066 do_cleanups (infolist_chain
);
1069 /* Initialize "info auto-load " commands prefix and return it. */
1071 struct cmd_list_element
**
1072 auto_load_info_cmdlist_get (void)
1074 static struct cmd_list_element
*retval
;
1077 add_prefix_cmd ("auto-load", class_info
, info_auto_load_cmd
, _("\
1078 Print current status of auto-loaded files.\n\
1079 Print whether various files like Python scripts or .gdbinit files have been\n\
1080 found and/or loaded."),
1081 &retval
, "info auto-load ",
1082 0/*allow-unknown*/, &infolist
);
1087 void _initialize_auto_load (void);
1090 _initialize_auto_load (void)
1092 struct cmd_list_element
*cmd
;
1094 auto_load_pspace_data
1095 = register_program_space_data_with_cleanup (auto_load_pspace_data_cleanup
);
1097 observer_attach_new_objfile (auto_load_new_objfile
);
1099 add_setshow_boolean_cmd ("gdb-scripts", class_support
,
1100 &auto_load_gdb_scripts
, _("\
1101 Enable or disable auto-loading of canned sequences of commands scripts."), _("\
1102 Show whether auto-loading of canned sequences of commands scripts is enabled."),
1104 If enabled, canned sequences of commands are loaded when the debugger reads\n\
1105 an executable or shared library.\n\
1106 This options has security implications for untrusted inferiors."),
1107 NULL
, show_auto_load_gdb_scripts
,
1108 auto_load_set_cmdlist_get (),
1109 auto_load_show_cmdlist_get ());
1111 add_cmd ("gdb-scripts", class_info
, info_auto_load_gdb_scripts
,
1112 _("Print the list of automatically loaded sequences of commands.\n\
1113 Usage: info auto-load gdb-scripts [REGEXP]"),
1114 auto_load_info_cmdlist_get ());
1116 add_setshow_boolean_cmd ("local-gdbinit", class_support
,
1117 &auto_load_local_gdbinit
, _("\
1118 Enable or disable auto-loading of .gdbinit script in current directory."), _("\
1119 Show whether auto-loading .gdbinit script in current directory is enabled."),
1121 If enabled, canned sequences of commands are loaded when debugger starts\n\
1122 from .gdbinit file in current directory. Such files are deprecated,\n\
1123 use a script associated with inferior executable file instead.\n\
1124 This options has security implications for untrusted inferiors."),
1125 NULL
, show_auto_load_local_gdbinit
,
1126 auto_load_set_cmdlist_get (),
1127 auto_load_show_cmdlist_get ());
1129 add_cmd ("local-gdbinit", class_info
, info_auto_load_local_gdbinit
,
1130 _("Print whether current directory .gdbinit file has been loaded.\n\
1131 Usage: info auto-load local-gdbinit"),
1132 auto_load_info_cmdlist_get ());
1134 auto_load_dir
= xstrdup (AUTO_LOAD_DIR
);
1135 add_setshow_optional_filename_cmd ("scripts-directory", class_support
,
1136 &auto_load_dir
, _("\
1137 Set the list of directories from which to load auto-loaded scripts."), _("\
1138 Show the list of directories from which to load auto-loaded scripts."), _("\
1139 Automatically loaded Python scripts and GDB scripts are located in one of the\n\
1140 directories listed by this option. This option is ignored for the kinds of\n\
1141 scripts having 'set auto-load ... off'. Directories listed here need to be\n\
1142 present also in the 'set auto-load safe-path' option."),
1143 set_auto_load_dir
, show_auto_load_dir
,
1144 auto_load_set_cmdlist_get (),
1145 auto_load_show_cmdlist_get ());
1147 auto_load_safe_path
= xstrdup (AUTO_LOAD_SAFE_PATH
);
1148 auto_load_safe_path_vec_update ();
1149 add_setshow_optional_filename_cmd ("safe-path", class_support
,
1150 &auto_load_safe_path
, _("\
1151 Set the list of directories from which it is safe to auto-load files."), _("\
1152 Show the list of directories from which it is safe to auto-load files."), _("\
1153 Various files loaded automatically for the 'set auto-load ...' options must\n\
1154 be located in one of the directories listed by this option. Warning will be\n\
1155 printed and file will not be used otherwise.\n\
1156 Setting this parameter to an empty list resets it to its default value.\n\
1157 Setting this parameter to '/' (without the quotes) allows any file\n\
1158 for the 'set auto-load ...' options.\n\
1159 This option is ignored for the kinds of files having 'set auto-load ... off'.\n\
1160 This options has security implications for untrusted inferiors."),
1161 set_auto_load_safe_path
,
1162 show_auto_load_safe_path
,
1163 auto_load_set_cmdlist_get (),
1164 auto_load_show_cmdlist_get ());
1165 observer_attach_gdb_datadir_changed (auto_load_gdb_datadir_changed
);
1167 cmd
= add_cmd ("add-auto-load-safe-path", class_support
,
1168 add_auto_load_safe_path
,
1169 _("Add entries to the list of directories from which it is safe "
1170 "to auto-load files.\n\
1171 See the commands 'set auto-load safe-path' and 'show auto-load safe-path' to\n\
1172 access the current full list setting."),
1174 set_cmd_completer (cmd
, filename_completer
);
1176 add_setshow_boolean_cmd ("auto-load", class_maintenance
,
1177 &debug_auto_load
, _("\
1178 Set auto-load verifications debugging."), _("\
1179 Show auto-load verifications debugging."), _("\
1180 When non-zero, debugging output for files of 'set auto-load ...'\n\
1182 NULL
, show_debug_auto_load
,
1183 &setdebuglist
, &showdebuglist
);