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"
41 /* The suffix of per-objfile scripts to auto-load as non-Python command files.
42 E.g. When the program loads libfoo.so, look for libfoo-gdb.gdb. */
43 #define GDB_AUTO_FILE_NAME "-gdb.gdb"
45 static void source_gdb_script_for_objfile (struct objfile
*objfile
, FILE *file
,
46 const char *filename
);
48 /* Value of the 'set debug auto-load' configuration variable. */
49 static int debug_auto_load
= 0;
51 /* "show" command for the debug_auto_load configuration variable. */
54 show_debug_auto_load (struct ui_file
*file
, int from_tty
,
55 struct cmd_list_element
*c
, const char *value
)
57 fprintf_filtered (file
, _("Debugging output for files "
58 "of 'set auto-load ...' is %s.\n"),
62 /* User-settable option to enable/disable auto-loading of GDB_AUTO_FILE_NAME
64 set auto-load gdb-scripts on|off
65 This is true if we should auto-load associated scripts when an objfile
66 is opened, false otherwise. */
67 static int auto_load_gdb_scripts
= 1;
69 /* "show" command for the auto_load_gdb_scripts configuration variable. */
72 show_auto_load_gdb_scripts (struct ui_file
*file
, int from_tty
,
73 struct cmd_list_element
*c
, const char *value
)
75 fprintf_filtered (file
, _("Auto-loading of canned sequences of commands "
80 /* Internal-use flag to enable/disable auto-loading.
81 This is true if we should auto-load python code when an objfile is opened,
84 Both auto_load_scripts && global_auto_load must be true to enable
87 This flag exists to facilitate deferring auto-loading during start-up
88 until after ./.gdbinit has been read; it may augment the search directories
89 used to find the scripts. */
90 int global_auto_load
= 1;
92 /* Auto-load .gdbinit file from the current directory? */
93 int auto_load_local_gdbinit
= 1;
95 /* Absolute pathname to the current directory .gdbinit, if it exists. */
96 char *auto_load_local_gdbinit_pathname
= NULL
;
98 /* Boolean value if AUTO_LOAD_LOCAL_GDBINIT_PATHNAME has been loaded. */
99 int auto_load_local_gdbinit_loaded
= 0;
101 /* "show" command for the auto_load_local_gdbinit configuration variable. */
104 show_auto_load_local_gdbinit (struct ui_file
*file
, int from_tty
,
105 struct cmd_list_element
*c
, const char *value
)
107 fprintf_filtered (file
, _("Auto-loading of .gdbinit script from current "
108 "directory is %s.\n"),
112 /* Directory list from which to load auto-loaded scripts. It is not checked
113 for absolute paths but they are strongly recommended. It is initialized by
114 _initialize_auto_load. */
115 static char *auto_load_dir
;
117 /* "set" command for the auto_load_dir configuration variable. */
120 set_auto_load_dir (char *args
, int from_tty
, struct cmd_list_element
*c
)
122 /* Setting the variable to "" resets it to the compile time defaults. */
123 if (auto_load_dir
[0] == '\0')
125 xfree (auto_load_dir
);
126 auto_load_dir
= xstrdup (AUTO_LOAD_DIR
);
130 /* "show" command for the auto_load_dir configuration variable. */
133 show_auto_load_dir (struct ui_file
*file
, int from_tty
,
134 struct cmd_list_element
*c
, const char *value
)
136 fprintf_filtered (file
, _("List of directories from which to load "
137 "auto-loaded scripts is %s.\n"),
141 /* Directory list safe to hold auto-loaded files. It is not checked for
142 absolute paths but they are strongly recommended. It is initialized by
143 _initialize_auto_load. */
144 static char *auto_load_safe_path
;
146 /* Vector of directory elements of AUTO_LOAD_SAFE_PATH with each one normalized
147 by tilde_expand and possibly each entries has added its gdb_realpath
149 static VEC (char_ptr
) *auto_load_safe_path_vec
;
151 /* Expand $datadir and $debugdir in STRING according to the rules of
152 substitute_path_component. Return vector from dirnames_to_char_ptr_vec,
153 this vector must be freed by free_char_ptr_vec by the caller. */
155 static VEC (char_ptr
) *
156 auto_load_expand_dir_vars (const char *string
)
158 VEC (char_ptr
) *dir_vec
;
161 s
= xstrdup (string
);
162 substitute_path_component (&s
, "$datadir", gdb_datadir
);
163 substitute_path_component (&s
, "$debugdir", debug_file_directory
);
165 if (debug_auto_load
&& strcmp (s
, string
) != 0)
166 fprintf_unfiltered (gdb_stdlog
,
167 _("auto-load: Expanded $-variables to \"%s\".\n"), s
);
169 dir_vec
= dirnames_to_char_ptr_vec (s
);
175 /* Update auto_load_safe_path_vec from current AUTO_LOAD_SAFE_PATH. */
178 auto_load_safe_path_vec_update (void)
180 VEC (char_ptr
) *dir_vec
= NULL
;
185 fprintf_unfiltered (gdb_stdlog
,
186 _("auto-load: Updating directories of \"%s\".\n"),
187 auto_load_safe_path
);
189 free_char_ptr_vec (auto_load_safe_path_vec
);
191 auto_load_safe_path_vec
= auto_load_expand_dir_vars (auto_load_safe_path
);
192 len
= VEC_length (char_ptr
, auto_load_safe_path_vec
);
194 /* Apply tilde_expand and gdb_realpath to each AUTO_LOAD_SAFE_PATH_VEC
196 for (ix
= 0; ix
< len
; ix
++)
198 char *dir
= VEC_index (char_ptr
, auto_load_safe_path_vec
, ix
);
199 char *expanded
= tilde_expand (dir
);
200 char *real_path
= gdb_realpath (expanded
);
202 /* Ensure the current entry is at least tilde_expand-ed. */
203 VEC_replace (char_ptr
, auto_load_safe_path_vec
, ix
, expanded
);
207 if (strcmp (expanded
, dir
) == 0)
208 fprintf_unfiltered (gdb_stdlog
,
209 _("auto-load: Using directory \"%s\".\n"),
212 fprintf_unfiltered (gdb_stdlog
,
213 _("auto-load: Resolved directory \"%s\" "
219 /* If gdb_realpath returns a different content, append it. */
220 if (strcmp (real_path
, expanded
) == 0)
224 VEC_safe_push (char_ptr
, auto_load_safe_path_vec
, real_path
);
227 fprintf_unfiltered (gdb_stdlog
,
228 _("auto-load: And canonicalized as \"%s\".\n"),
234 /* Variable gdb_datadir has been set. Update content depending on $datadir. */
237 auto_load_gdb_datadir_changed (void)
239 auto_load_safe_path_vec_update ();
242 /* "set" command for the auto_load_safe_path configuration variable. */
245 set_auto_load_safe_path (char *args
, int from_tty
, struct cmd_list_element
*c
)
247 /* Setting the variable to "" resets it to the compile time defaults. */
248 if (auto_load_safe_path
[0] == '\0')
250 xfree (auto_load_safe_path
);
251 auto_load_safe_path
= xstrdup (AUTO_LOAD_SAFE_PATH
);
254 auto_load_safe_path_vec_update ();
257 /* "show" command for the auto_load_safe_path configuration variable. */
260 show_auto_load_safe_path (struct ui_file
*file
, int from_tty
,
261 struct cmd_list_element
*c
, const char *value
)
265 /* Check if user has entered either "/" or for example ":".
266 But while more complicate content like ":/foo" would still also
267 permit any location do not hide those. */
269 for (cs
= value
; *cs
&& (*cs
== DIRNAME_SEPARATOR
|| IS_DIR_SEPARATOR (*cs
));
272 fprintf_filtered (file
, _("Auto-load files are safe to load from any "
275 fprintf_filtered (file
, _("List of directories from which it is safe to "
276 "auto-load files is %s.\n"),
280 /* "add-auto-load-safe-path" command for the auto_load_safe_path configuration
284 add_auto_load_safe_path (char *args
, int from_tty
)
288 if (args
== NULL
|| *args
== 0)
290 Directory argument required.\n\
291 Use 'set auto-load safe-path /' for disabling the auto-load safe-path security.\
294 s
= xstrprintf ("%s%c%s", auto_load_safe_path
, DIRNAME_SEPARATOR
, args
);
295 xfree (auto_load_safe_path
);
296 auto_load_safe_path
= s
;
298 auto_load_safe_path_vec_update ();
301 /* Implementation for filename_is_in_pattern overwriting the caller's FILENAME
305 filename_is_in_pattern_1 (char *filename
, char *pattern
)
307 size_t pattern_len
= strlen (pattern
);
308 size_t filename_len
= strlen (filename
);
311 fprintf_unfiltered (gdb_stdlog
, _("auto-load: Matching file \"%s\" "
312 "to pattern \"%s\"\n"),
315 /* Trim trailing slashes ("/") from PATTERN. Even for "d:\" paths as
316 trailing slashes are trimmed also from FILENAME it still matches
318 while (pattern_len
&& IS_DIR_SEPARATOR (pattern
[pattern_len
- 1]))
320 pattern
[pattern_len
] = '\0';
322 /* Ensure auto_load_safe_path "/" matches any FILENAME. On MS-Windows
323 platform FILENAME even after gdb_realpath does not have to start with
324 IS_DIR_SEPARATOR character, such as the 'C:\x.exe' filename. */
325 if (pattern_len
== 0)
328 fprintf_unfiltered (gdb_stdlog
,
329 _("auto-load: Matched - empty pattern\n"));
335 /* Trim trailing slashes ("/"). PATTERN also has slashes trimmed the
336 same way so they will match. */
337 while (filename_len
&& IS_DIR_SEPARATOR (filename
[filename_len
- 1]))
339 filename
[filename_len
] = '\0';
340 if (filename_len
== 0)
343 fprintf_unfiltered (gdb_stdlog
,
344 _("auto-load: Not matched - pattern \"%s\".\n"),
349 if (gdb_filename_fnmatch (pattern
, filename
, FNM_FILE_NAME
| FNM_NOESCAPE
)
353 fprintf_unfiltered (gdb_stdlog
, _("auto-load: Matched - file "
354 "\"%s\" to pattern \"%s\".\n"),
359 /* Trim trailing FILENAME component. */
360 while (filename_len
> 0 && !IS_DIR_SEPARATOR (filename
[filename_len
- 1]))
365 /* Return 1 if FILENAME matches PATTERN or if FILENAME resides in
366 a subdirectory of a directory that matches PATTERN. Return 0 otherwise.
367 gdb_realpath normalization is never done here. */
369 static ATTRIBUTE_PURE
int
370 filename_is_in_pattern (const char *filename
, const char *pattern
)
372 char *filename_copy
, *pattern_copy
;
374 filename_copy
= alloca (strlen (filename
) + 1);
375 strcpy (filename_copy
, filename
);
376 pattern_copy
= alloca (strlen (pattern
) + 1);
377 strcpy (pattern_copy
, pattern
);
379 return filename_is_in_pattern_1 (filename_copy
, pattern_copy
);
382 /* Return 1 if FILENAME belongs to one of directory components of
383 AUTO_LOAD_SAFE_PATH_VEC. Return 0 otherwise.
384 auto_load_safe_path_vec_update is never called.
385 *FILENAME_REALP may be updated by gdb_realpath of FILENAME - it has to be
386 freed by the caller. */
389 filename_is_in_auto_load_safe_path_vec (const char *filename
,
390 char **filename_realp
)
395 for (ix
= 0; VEC_iterate (char_ptr
, auto_load_safe_path_vec
, ix
, pattern
);
397 if (*filename_realp
== NULL
&& filename_is_in_pattern (filename
, pattern
))
402 if (*filename_realp
== NULL
)
404 *filename_realp
= gdb_realpath (filename
);
405 if (debug_auto_load
&& strcmp (*filename_realp
, filename
) != 0)
406 fprintf_unfiltered (gdb_stdlog
,
407 _("auto-load: Resolved "
408 "file \"%s\" as \"%s\".\n"),
409 filename
, *filename_realp
);
412 if (strcmp (*filename_realp
, filename
) != 0)
414 VEC_iterate (char_ptr
, auto_load_safe_path_vec
, ix
, pattern
); ++ix
)
415 if (filename_is_in_pattern (*filename_realp
, pattern
))
422 fprintf_unfiltered (gdb_stdlog
, _("auto-load: File \"%s\" matches "
423 "directory \"%s\".\n"),
431 /* Return 1 if FILENAME is located in one of the directories of
432 AUTO_LOAD_SAFE_PATH. Otherwise call warning and return 0. FILENAME does
433 not have to be an absolute path.
435 Existence of FILENAME is not checked. Function will still give a warning
436 even if the caller would quietly skip non-existing file in unsafe
440 file_is_auto_load_safe (const char *filename
, const char *debug_fmt
, ...)
442 char *filename_real
= NULL
;
443 struct cleanup
*back_to
;
449 va_start (debug_args
, debug_fmt
);
450 vfprintf_unfiltered (gdb_stdlog
, debug_fmt
, debug_args
);
454 back_to
= make_cleanup (free_current_contents
, &filename_real
);
456 if (filename_is_in_auto_load_safe_path_vec (filename
, &filename_real
))
458 do_cleanups (back_to
);
462 auto_load_safe_path_vec_update ();
463 if (filename_is_in_auto_load_safe_path_vec (filename
, &filename_real
))
465 do_cleanups (back_to
);
469 warning (_("File \"%s\" auto-loading has been declined by your "
470 "`auto-load safe-path' set to \"%s\"."),
471 filename_real
, auto_load_safe_path
);
473 do_cleanups (back_to
);
477 /* Definition of script language for GDB canned sequences of commands. */
479 static const struct script_language script_language_gdb
480 = { GDB_AUTO_FILE_NAME
, source_gdb_script_for_objfile
};
483 source_gdb_script_for_objfile (struct objfile
*objfile
, FILE *file
,
484 const char *filename
)
487 struct auto_load_pspace_info
*pspace_info
;
488 volatile struct gdb_exception e
;
490 is_safe
= file_is_auto_load_safe (filename
, _("auto-load: Loading canned "
491 "sequences of commands script "
492 "\"%s\" for objfile \"%s\".\n"),
493 filename
, objfile
->name
);
495 /* Add this script to the hash table too so "info auto-load gdb-scripts"
497 pspace_info
= get_auto_load_pspace_data_for_loading (current_program_space
);
498 maybe_add_script (pspace_info
, is_safe
, filename
, filename
,
499 &script_language_gdb
);
504 TRY_CATCH (e
, RETURN_MASK_ALL
)
506 script_from_file (file
, filename
);
508 exception_print (gdb_stderr
, e
);
511 /* For scripts specified in .debug_gdb_scripts, multiple objfiles may load
512 the same script. There's no point in loading the script multiple times,
513 and there can be a lot of objfiles and scripts, so we keep track of scripts
516 struct auto_load_pspace_info
518 /* For each program space we keep track of loaded scripts. */
519 struct htab
*loaded_scripts
;
521 /* Non-zero if we've issued the warning about an auto-load script not being
522 found. We only want to issue this warning once. */
523 int script_not_found_warning_printed
;
526 /* Objects of this type are stored in the loaded script hash table. */
530 /* Name as provided by the objfile. */
533 /* Full path name or NULL if script wasn't found (or was otherwise
535 const char *full_path
;
537 /* Non-zero if this script has been loaded. */
540 const struct script_language
*language
;
543 /* Per-program-space data key. */
544 static const struct program_space_data
*auto_load_pspace_data
;
547 auto_load_pspace_data_cleanup (struct program_space
*pspace
, void *arg
)
549 struct auto_load_pspace_info
*info
;
551 info
= program_space_data (pspace
, auto_load_pspace_data
);
554 if (info
->loaded_scripts
)
555 htab_delete (info
->loaded_scripts
);
560 /* Get the current autoload data. If none is found yet, add it now. This
561 function always returns a valid object. */
563 static struct auto_load_pspace_info
*
564 get_auto_load_pspace_data (struct program_space
*pspace
)
566 struct auto_load_pspace_info
*info
;
568 info
= program_space_data (pspace
, auto_load_pspace_data
);
571 info
= XZALLOC (struct auto_load_pspace_info
);
572 set_program_space_data (pspace
, auto_load_pspace_data
, info
);
578 /* Hash function for the loaded script hash. */
581 hash_loaded_script_entry (const void *data
)
583 const struct loaded_script
*e
= data
;
585 return htab_hash_string (e
->name
) ^ htab_hash_pointer (e
->language
);
588 /* Equality function for the loaded script hash. */
591 eq_loaded_script_entry (const void *a
, const void *b
)
593 const struct loaded_script
*ea
= a
;
594 const struct loaded_script
*eb
= b
;
596 return strcmp (ea
->name
, eb
->name
) == 0 && ea
->language
== eb
->language
;
599 /* Initialize the table to track loaded scripts.
600 Each entry is hashed by the full path name. */
603 init_loaded_scripts_info (struct auto_load_pspace_info
*pspace_info
)
605 /* Choose 31 as the starting size of the hash table, somewhat arbitrarily.
606 Space for each entry is obtained with one malloc so we can free them
609 pspace_info
->loaded_scripts
= htab_create (31,
610 hash_loaded_script_entry
,
611 eq_loaded_script_entry
,
614 pspace_info
->script_not_found_warning_printed
= FALSE
;
617 /* Wrapper on get_auto_load_pspace_data to also allocate the hash table
618 for loading scripts. */
620 struct auto_load_pspace_info
*
621 get_auto_load_pspace_data_for_loading (struct program_space
*pspace
)
623 struct auto_load_pspace_info
*info
;
625 info
= get_auto_load_pspace_data (pspace
);
626 if (info
->loaded_scripts
== NULL
)
627 init_loaded_scripts_info (info
);
632 /* Add script NAME in LANGUAGE to hash table of PSPACE_INFO. LOADED 1 if the
633 script has been (is going to) be loaded, 0 otherwise (such as if it has not
634 been found). FULL_PATH is NULL if the script wasn't found. The result is
635 true if the script was already in the hash table. */
638 maybe_add_script (struct auto_load_pspace_info
*pspace_info
, int loaded
,
639 const char *name
, const char *full_path
,
640 const struct script_language
*language
)
642 struct htab
*htab
= pspace_info
->loaded_scripts
;
643 struct loaded_script
**slot
, entry
;
647 entry
.language
= language
;
648 slot
= (struct loaded_script
**) htab_find_slot (htab
, &entry
, INSERT
);
649 in_hash_table
= *slot
!= NULL
;
651 /* If this script is not in the hash table, add it. */
657 /* Allocate all space in one chunk so it's easier to free. */
658 *slot
= xmalloc (sizeof (**slot
)
660 + (full_path
!= NULL
? (strlen (full_path
) + 1) : 0));
661 p
= ((char*) *slot
) + sizeof (**slot
);
664 if (full_path
!= NULL
)
667 strcpy (p
, full_path
);
668 (*slot
)->full_path
= p
;
671 (*slot
)->full_path
= NULL
;
672 (*slot
)->loaded
= loaded
;
673 (*slot
)->language
= language
;
676 return in_hash_table
;
679 /* Clear the table of loaded section scripts. */
682 clear_section_scripts (void)
684 struct program_space
*pspace
= current_program_space
;
685 struct auto_load_pspace_info
*info
;
687 info
= program_space_data (pspace
, auto_load_pspace_data
);
688 if (info
!= NULL
&& info
->loaded_scripts
!= NULL
)
690 htab_delete (info
->loaded_scripts
);
691 info
->loaded_scripts
= NULL
;
692 info
->script_not_found_warning_printed
= FALSE
;
696 /* Look for the auto-load script in LANGUAGE associated with OBJFILE where
697 OBJFILE's gdb_realpath is REALNAME and load it. Return 1 if we found any
698 matching script, return 0 otherwise. */
701 auto_load_objfile_script_1 (struct objfile
*objfile
, const char *realname
,
702 const struct script_language
*language
)
704 char *filename
, *debugfile
;
707 struct cleanup
*cleanups
;
709 len
= strlen (realname
);
710 filename
= xmalloc (len
+ strlen (language
->suffix
) + 1);
711 memcpy (filename
, realname
, len
);
712 strcpy (filename
+ len
, language
->suffix
);
714 cleanups
= make_cleanup (xfree
, filename
);
716 input
= fopen (filename
, "r");
717 debugfile
= filename
;
719 fprintf_unfiltered (gdb_stdlog
, _("auto-load: Attempted file \"%s\" %s.\n"),
720 debugfile
, input
? _("exists") : _("does not exist"));
728 /* Also try the same file in a subdirectory of gdb's data
731 vec
= auto_load_expand_dir_vars (auto_load_dir
);
732 make_cleanup_free_char_ptr_vec (vec
);
735 fprintf_unfiltered (gdb_stdlog
, _("auto-load: Searching 'set auto-load "
736 "scripts-directory' path \"%s\".\n"),
739 for (ix
= 0; VEC_iterate (char_ptr
, vec
, ix
, dir
); ++ix
)
741 debugfile
= xmalloc (strlen (dir
) + strlen (filename
) + 1);
742 strcpy (debugfile
, dir
);
744 /* FILENAME is absolute, so we don't need a "/" here. */
745 strcat (debugfile
, filename
);
747 make_cleanup (xfree
, debugfile
);
748 input
= fopen (debugfile
, "r");
750 fprintf_unfiltered (gdb_stdlog
, _("auto-load: Attempted file "
753 input
? _("exists") : _("does not exist"));
761 make_cleanup_fclose (input
);
763 /* To preserve existing behaviour we don't check for whether the
764 script was already in the table, and always load it.
765 It's highly unlikely that we'd ever load it twice,
766 and these scripts are required to be idempotent under multiple
768 language
->source_script_for_objfile (objfile
, input
, debugfile
);
775 do_cleanups (cleanups
);
779 /* Look for the auto-load script in LANGUAGE associated with OBJFILE and load
783 auto_load_objfile_script (struct objfile
*objfile
,
784 const struct script_language
*language
)
786 char *realname
= gdb_realpath (objfile
->name
);
787 struct cleanup
*cleanups
= make_cleanup (xfree
, realname
);
789 if (!auto_load_objfile_script_1 (objfile
, realname
, language
))
791 /* For Windows/DOS .exe executables, strip the .exe suffix, so that
792 FOO-gdb.gdb could be used for FOO.exe, and try again. */
794 size_t len
= strlen (realname
);
795 const size_t lexe
= sizeof (".exe") - 1;
797 if (len
> lexe
&& strcasecmp (realname
+ len
- lexe
, ".exe") == 0)
800 realname
[len
] = '\0';
802 fprintf_unfiltered (gdb_stdlog
, _("auto-load: Stripped .exe suffix, "
803 "retrying with \"%s\".\n"),
805 auto_load_objfile_script_1 (objfile
, realname
, language
);
809 do_cleanups (cleanups
);
812 /* Load any auto-loaded scripts for OBJFILE. */
815 load_auto_scripts_for_objfile (struct objfile
*objfile
)
817 if (!global_auto_load
)
820 if (auto_load_gdb_scripts
)
821 auto_load_objfile_script (objfile
, &script_language_gdb
);
823 gdbpy_load_auto_scripts_for_objfile (objfile
);
826 /* This is a new_objfile observer callback to auto-load scripts.
828 Two flavors of auto-loaded scripts are supported.
829 1) based on the path to the objfile
830 2) from .debug_gdb_scripts section */
833 auto_load_new_objfile (struct objfile
*objfile
)
837 /* OBJFILE is NULL when loading a new "main" symbol-file. */
838 clear_section_scripts ();
842 load_auto_scripts_for_objfile (objfile
);
845 /* Collect scripts to be printed in a vec. */
847 typedef struct loaded_script
*loaded_script_ptr
;
848 DEF_VEC_P (loaded_script_ptr
);
850 struct collect_matching_scripts_data
852 VEC (loaded_script_ptr
) **scripts_p
;
854 const struct script_language
*language
;
857 /* Traversal function for htab_traverse.
858 Collect the entry if it matches the regexp. */
861 collect_matching_scripts (void **slot
, void *info
)
863 struct loaded_script
*script
= *slot
;
864 struct collect_matching_scripts_data
*data
= info
;
866 if (script
->language
== data
->language
&& re_exec (script
->name
))
867 VEC_safe_push (loaded_script_ptr
, *data
->scripts_p
, script
);
875 print_script (struct loaded_script
*script
)
877 struct ui_out
*uiout
= current_uiout
;
878 struct cleanup
*chain
;
880 chain
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
882 ui_out_field_string (uiout
, "loaded", script
->loaded
? "Yes" : "No");
883 ui_out_field_string (uiout
, "script", script
->name
);
884 ui_out_text (uiout
, "\n");
886 /* If the name isn't the full path, print it too. */
887 if (script
->full_path
!= NULL
888 && strcmp (script
->name
, script
->full_path
) != 0)
890 ui_out_text (uiout
, "\tfull name: ");
891 ui_out_field_string (uiout
, "full_path", script
->full_path
);
892 ui_out_text (uiout
, "\n");
898 /* Helper for info_auto_load_scripts to sort the scripts by name. */
901 sort_scripts_by_name (const void *ap
, const void *bp
)
903 const struct loaded_script
*a
= *(const struct loaded_script
**) ap
;
904 const struct loaded_script
*b
= *(const struct loaded_script
**) bp
;
906 return FILENAME_CMP (a
->name
, b
->name
);
909 /* Special internal GDB value of auto_load_info_scripts's PATTERN identify
910 the "info auto-load XXX" command has been executed through the general
911 "info auto-load" invocation. Extra newline will be printed if needed. */
912 char auto_load_info_scripts_pattern_nl
[] = "";
914 /* Implementation for "info auto-load gdb-scripts"
915 (and "info auto-load python-scripts"). List scripts in LANGUAGE matching
916 PATTERN. FROM_TTY is the usual GDB boolean for user interactivity. */
919 auto_load_info_scripts (char *pattern
, int from_tty
,
920 const struct script_language
*language
)
922 struct ui_out
*uiout
= current_uiout
;
923 struct auto_load_pspace_info
*pspace_info
;
924 struct cleanup
*script_chain
;
925 VEC (loaded_script_ptr
) *scripts
;
930 pspace_info
= get_auto_load_pspace_data (current_program_space
);
932 if (pattern
&& *pattern
)
934 char *re_err
= re_comp (pattern
);
937 error (_("Invalid regexp: %s"), re_err
);
944 /* We need to know the number of rows before we build the table.
945 Plus we want to sort the scripts by name.
946 So first traverse the hash table collecting the matching scripts. */
948 scripts
= VEC_alloc (loaded_script_ptr
, 10);
949 script_chain
= make_cleanup (VEC_cleanup (loaded_script_ptr
), &scripts
);
951 if (pspace_info
!= NULL
&& pspace_info
->loaded_scripts
!= NULL
)
953 struct collect_matching_scripts_data data
= { &scripts
, language
};
955 /* Pass a pointer to scripts as VEC_safe_push can realloc space. */
956 htab_traverse_noresize (pspace_info
->loaded_scripts
,
957 collect_matching_scripts
, &data
);
960 nr_scripts
= VEC_length (loaded_script_ptr
, scripts
);
962 /* Table header shifted right by preceding "gdb-scripts: " would not match
964 if (nr_scripts
> 0 && pattern
== auto_load_info_scripts_pattern_nl
)
965 ui_out_text (uiout
, "\n");
967 make_cleanup_ui_out_table_begin_end (uiout
, 2, nr_scripts
,
968 "AutoLoadedScriptsTable");
970 ui_out_table_header (uiout
, 7, ui_left
, "loaded", "Loaded");
971 ui_out_table_header (uiout
, 70, ui_left
, "script", "Script");
972 ui_out_table_body (uiout
);
977 loaded_script_ptr script
;
979 qsort (VEC_address (loaded_script_ptr
, scripts
),
980 VEC_length (loaded_script_ptr
, scripts
),
981 sizeof (loaded_script_ptr
), sort_scripts_by_name
);
982 for (i
= 0; VEC_iterate (loaded_script_ptr
, scripts
, i
, script
); ++i
)
983 print_script (script
);
986 do_cleanups (script_chain
);
990 if (pattern
&& *pattern
)
991 ui_out_message (uiout
, 0, "No auto-load scripts matching %s.\n",
994 ui_out_message (uiout
, 0, "No auto-load scripts.\n");
998 /* Wrapper for "info auto-load gdb-scripts". */
1001 info_auto_load_gdb_scripts (char *pattern
, int from_tty
)
1003 auto_load_info_scripts (pattern
, from_tty
, &script_language_gdb
);
1006 /* Implement 'info auto-load local-gdbinit'. */
1009 info_auto_load_local_gdbinit (char *args
, int from_tty
)
1011 if (auto_load_local_gdbinit_pathname
== NULL
)
1012 printf_filtered (_("Local .gdbinit file was not found.\n"));
1013 else if (auto_load_local_gdbinit_loaded
)
1014 printf_filtered (_("Local .gdbinit file \"%s\" has been loaded.\n"),
1015 auto_load_local_gdbinit_pathname
);
1017 printf_filtered (_("Local .gdbinit file \"%s\" has not been loaded.\n"),
1018 auto_load_local_gdbinit_pathname
);
1021 /* Return non-zero if SCRIPT_NOT_FOUND_WARNING_PRINTED of PSPACE_INFO was unset
1022 before calling this function. Always set SCRIPT_NOT_FOUND_WARNING_PRINTED
1026 script_not_found_warning_print (struct auto_load_pspace_info
*pspace_info
)
1028 int retval
= !pspace_info
->script_not_found_warning_printed
;
1030 pspace_info
->script_not_found_warning_printed
= 1;
1035 /* The only valid "set auto-load" argument is off|0|no|disable. */
1038 set_auto_load_cmd (char *args
, int from_tty
)
1040 struct cmd_list_element
*list
;
1043 /* See parse_binary_operation in use by the sub-commands. */
1045 length
= args
? strlen (args
) : 0;
1047 while (length
> 0 && (args
[length
- 1] == ' ' || args
[length
- 1] == '\t'))
1050 if (length
== 0 || (strncmp (args
, "off", length
) != 0
1051 && strncmp (args
, "0", length
) != 0
1052 && strncmp (args
, "no", length
) != 0
1053 && strncmp (args
, "disable", length
) != 0))
1054 error (_("Valid is only global 'set auto-load no'; "
1055 "otherwise check the auto-load sub-commands."));
1057 for (list
= *auto_load_set_cmdlist_get (); list
!= NULL
; list
= list
->next
)
1058 if (list
->var_type
== var_boolean
)
1060 gdb_assert (list
->type
== set_cmd
);
1061 do_set_command (args
, from_tty
, list
);
1065 /* Initialize "set auto-load " commands prefix and return it. */
1067 struct cmd_list_element
**
1068 auto_load_set_cmdlist_get (void)
1070 static struct cmd_list_element
*retval
;
1073 add_prefix_cmd ("auto-load", class_maintenance
, set_auto_load_cmd
, _("\
1074 Auto-loading specific settings.\n\
1075 Configure various auto-load-specific variables such as\n\
1076 automatic loading of Python scripts."),
1077 &retval
, "set auto-load ",
1078 1/*allow-unknown*/, &setlist
);
1083 /* Command "show auto-load" displays summary of all the current
1084 "show auto-load " settings. */
1087 show_auto_load_cmd (char *args
, int from_tty
)
1089 cmd_show_list (*auto_load_show_cmdlist_get (), from_tty
, "");
1092 /* Initialize "show auto-load " commands prefix and return it. */
1094 struct cmd_list_element
**
1095 auto_load_show_cmdlist_get (void)
1097 static struct cmd_list_element
*retval
;
1100 add_prefix_cmd ("auto-load", class_maintenance
, show_auto_load_cmd
, _("\
1101 Show auto-loading specific settings.\n\
1102 Show configuration of various auto-load-specific variables such as\n\
1103 automatic loading of Python scripts."),
1104 &retval
, "show auto-load ",
1105 0/*allow-unknown*/, &showlist
);
1110 /* Command "info auto-load" displays whether the various auto-load files have
1111 been loaded. This is reimplementation of cmd_show_list which inserts
1112 newlines at proper places. */
1115 info_auto_load_cmd (char *args
, int from_tty
)
1117 struct cmd_list_element
*list
;
1118 struct cleanup
*infolist_chain
;
1119 struct ui_out
*uiout
= current_uiout
;
1121 infolist_chain
= make_cleanup_ui_out_tuple_begin_end (uiout
, "infolist");
1123 for (list
= *auto_load_info_cmdlist_get (); list
!= NULL
; list
= list
->next
)
1125 struct cleanup
*option_chain
1126 = make_cleanup_ui_out_tuple_begin_end (uiout
, "option");
1128 gdb_assert (!list
->prefixlist
);
1129 gdb_assert (list
->type
== not_set_cmd
);
1131 ui_out_field_string (uiout
, "name", list
->name
);
1132 ui_out_text (uiout
, ": ");
1133 cmd_func (list
, auto_load_info_scripts_pattern_nl
, from_tty
);
1135 /* Close the tuple. */
1136 do_cleanups (option_chain
);
1139 /* Close the tuple. */
1140 do_cleanups (infolist_chain
);
1143 /* Initialize "info auto-load " commands prefix and return it. */
1145 struct cmd_list_element
**
1146 auto_load_info_cmdlist_get (void)
1148 static struct cmd_list_element
*retval
;
1151 add_prefix_cmd ("auto-load", class_info
, info_auto_load_cmd
, _("\
1152 Print current status of auto-loaded files.\n\
1153 Print whether various files like Python scripts or .gdbinit files have been\n\
1154 found and/or loaded."),
1155 &retval
, "info auto-load ",
1156 0/*allow-unknown*/, &infolist
);
1161 void _initialize_auto_load (void);
1164 _initialize_auto_load (void)
1166 struct cmd_list_element
*cmd
;
1168 auto_load_pspace_data
1169 = register_program_space_data_with_cleanup (NULL
,
1170 auto_load_pspace_data_cleanup
);
1172 observer_attach_new_objfile (auto_load_new_objfile
);
1174 add_setshow_boolean_cmd ("gdb-scripts", class_support
,
1175 &auto_load_gdb_scripts
, _("\
1176 Enable or disable auto-loading of canned sequences of commands scripts."), _("\
1177 Show whether auto-loading of canned sequences of commands scripts is enabled."),
1179 If enabled, canned sequences of commands are loaded when the debugger reads\n\
1180 an executable or shared library.\n\
1181 This options has security implications for untrusted inferiors."),
1182 NULL
, show_auto_load_gdb_scripts
,
1183 auto_load_set_cmdlist_get (),
1184 auto_load_show_cmdlist_get ());
1186 add_cmd ("gdb-scripts", class_info
, info_auto_load_gdb_scripts
,
1187 _("Print the list of automatically loaded sequences of commands.\n\
1188 Usage: info auto-load gdb-scripts [REGEXP]"),
1189 auto_load_info_cmdlist_get ());
1191 add_setshow_boolean_cmd ("local-gdbinit", class_support
,
1192 &auto_load_local_gdbinit
, _("\
1193 Enable or disable auto-loading of .gdbinit script in current directory."), _("\
1194 Show whether auto-loading .gdbinit script in current directory is enabled."),
1196 If enabled, canned sequences of commands are loaded when debugger starts\n\
1197 from .gdbinit file in current directory. Such files are deprecated,\n\
1198 use a script associated with inferior executable file instead.\n\
1199 This options has security implications for untrusted inferiors."),
1200 NULL
, show_auto_load_local_gdbinit
,
1201 auto_load_set_cmdlist_get (),
1202 auto_load_show_cmdlist_get ());
1204 add_cmd ("local-gdbinit", class_info
, info_auto_load_local_gdbinit
,
1205 _("Print whether current directory .gdbinit file has been loaded.\n\
1206 Usage: info auto-load local-gdbinit"),
1207 auto_load_info_cmdlist_get ());
1209 auto_load_dir
= xstrdup (AUTO_LOAD_DIR
);
1210 add_setshow_optional_filename_cmd ("scripts-directory", class_support
,
1211 &auto_load_dir
, _("\
1212 Set the list of directories from which to load auto-loaded scripts."), _("\
1213 Show the list of directories from which to load auto-loaded scripts."), _("\
1214 Automatically loaded Python scripts and GDB scripts are located in one of the\n\
1215 directories listed by this option. This option is ignored for the kinds of\n\
1216 scripts having 'set auto-load ... off'. Directories listed here need to be\n\
1217 present also in the 'set auto-load safe-path' option."),
1218 set_auto_load_dir
, show_auto_load_dir
,
1219 auto_load_set_cmdlist_get (),
1220 auto_load_show_cmdlist_get ());
1222 auto_load_safe_path
= xstrdup (AUTO_LOAD_SAFE_PATH
);
1223 auto_load_safe_path_vec_update ();
1224 add_setshow_optional_filename_cmd ("safe-path", class_support
,
1225 &auto_load_safe_path
, _("\
1226 Set the list of directories from which it is safe to auto-load files."), _("\
1227 Show the list of directories from which it is safe to auto-load files."), _("\
1228 Various files loaded automatically for the 'set auto-load ...' options must\n\
1229 be located in one of the directories listed by this option. Warning will be\n\
1230 printed and file will not be used otherwise.\n\
1231 Setting this parameter to an empty list resets it to its default value.\n\
1232 Setting this parameter to '/' (without the quotes) allows any file\n\
1233 for the 'set auto-load ...' options. Each directory can be also shell\n\
1234 wildcard pattern; '*' does not match directory separator.\n\
1235 This option is ignored for the kinds of files having 'set auto-load ... off'.\n\
1236 This options has security implications for untrusted inferiors."),
1237 set_auto_load_safe_path
,
1238 show_auto_load_safe_path
,
1239 auto_load_set_cmdlist_get (),
1240 auto_load_show_cmdlist_get ());
1241 observer_attach_gdb_datadir_changed (auto_load_gdb_datadir_changed
);
1243 cmd
= add_cmd ("add-auto-load-safe-path", class_support
,
1244 add_auto_load_safe_path
,
1245 _("Add entries to the list of directories from which it is safe "
1246 "to auto-load files.\n\
1247 See the commands 'set auto-load safe-path' and 'show auto-load safe-path' to\n\
1248 access the current full list setting."),
1250 set_cmd_completer (cmd
, filename_completer
);
1252 add_setshow_boolean_cmd ("auto-load", class_maintenance
,
1253 &debug_auto_load
, _("\
1254 Set auto-load verifications debugging."), _("\
1255 Show auto-load verifications debugging."), _("\
1256 When non-zero, debugging output for files of 'set auto-load ...'\n\
1258 NULL
, show_debug_auto_load
,
1259 &setdebuglist
, &showdebuglist
);