rtld - do not allow both dynamic DTV index and static TLS offset
[dragonfly.git] / contrib / gdb-7 / gdb / auto-load.c
blobb12995b652a15c160d3faa8bb2b73ed8e7348f77
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/>. */
20 #include "defs.h"
21 #include "auto-load.h"
22 #include "progspace.h"
23 #include "python/python.h"
24 #include "gdb_regex.h"
25 #include "ui-out.h"
26 #include "filenames.h"
27 #include "command.h"
28 #include "observer.h"
29 #include "objfiles.h"
30 #include "exceptions.h"
31 #include "cli/cli-script.h"
32 #include "gdbcmd.h"
33 #include "cli/cli-decode.h"
34 #include "cli/cli-setshow.h"
35 #include "gdb_vecs.h"
36 #include "readline/tilde.h"
37 #include "completer.h"
38 #include "observer.h"
39 #include "fnmatch.h"
40 #include "top.h"
42 /* The suffix of per-objfile scripts to auto-load as non-Python command files.
43 E.g. When the program loads libfoo.so, look for libfoo-gdb.gdb. */
44 #define GDB_AUTO_FILE_NAME "-gdb.gdb"
46 static void source_gdb_script_for_objfile (struct objfile *objfile, FILE *file,
47 const char *filename);
49 /* Value of the 'set debug auto-load' configuration variable. */
50 static int debug_auto_load = 0;
52 /* "show" command for the debug_auto_load configuration variable. */
54 static void
55 show_debug_auto_load (struct ui_file *file, int from_tty,
56 struct cmd_list_element *c, const char *value)
58 fprintf_filtered (file, _("Debugging output for files "
59 "of 'set auto-load ...' is %s.\n"),
60 value);
63 /* User-settable option to enable/disable auto-loading of GDB_AUTO_FILE_NAME
64 scripts:
65 set auto-load gdb-scripts on|off
66 This is true if we should auto-load associated scripts when an objfile
67 is opened, false otherwise. */
68 static int auto_load_gdb_scripts = 1;
70 /* "show" command for the auto_load_gdb_scripts configuration variable. */
72 static void
73 show_auto_load_gdb_scripts (struct ui_file *file, int from_tty,
74 struct cmd_list_element *c, const char *value)
76 fprintf_filtered (file, _("Auto-loading of canned sequences of commands "
77 "scripts is %s.\n"),
78 value);
81 /* Internal-use flag to enable/disable auto-loading.
82 This is true if we should auto-load python code when an objfile is opened,
83 false otherwise.
85 Both auto_load_scripts && global_auto_load must be true to enable
86 auto-loading.
88 This flag exists to facilitate deferring auto-loading during start-up
89 until after ./.gdbinit has been read; it may augment the search directories
90 used to find the scripts. */
91 int global_auto_load = 1;
93 /* Auto-load .gdbinit file from the current directory? */
94 int auto_load_local_gdbinit = 1;
96 /* Absolute pathname to the current directory .gdbinit, if it exists. */
97 char *auto_load_local_gdbinit_pathname = NULL;
99 /* Boolean value if AUTO_LOAD_LOCAL_GDBINIT_PATHNAME has been loaded. */
100 int auto_load_local_gdbinit_loaded = 0;
102 /* "show" command for the auto_load_local_gdbinit configuration variable. */
104 static void
105 show_auto_load_local_gdbinit (struct ui_file *file, int from_tty,
106 struct cmd_list_element *c, const char *value)
108 fprintf_filtered (file, _("Auto-loading of .gdbinit script from current "
109 "directory is %s.\n"),
110 value);
113 /* Directory list from which to load auto-loaded scripts. It is not checked
114 for absolute paths but they are strongly recommended. It is initialized by
115 _initialize_auto_load. */
116 static char *auto_load_dir;
118 /* "set" command for the auto_load_dir configuration variable. */
120 static void
121 set_auto_load_dir (char *args, int from_tty, struct cmd_list_element *c)
123 /* Setting the variable to "" resets it to the compile time defaults. */
124 if (auto_load_dir[0] == '\0')
126 xfree (auto_load_dir);
127 auto_load_dir = xstrdup (AUTO_LOAD_DIR);
131 /* "show" command for the auto_load_dir configuration variable. */
133 static void
134 show_auto_load_dir (struct ui_file *file, int from_tty,
135 struct cmd_list_element *c, const char *value)
137 fprintf_filtered (file, _("List of directories from which to load "
138 "auto-loaded scripts is %s.\n"),
139 value);
142 /* Directory list safe to hold auto-loaded files. It is not checked for
143 absolute paths but they are strongly recommended. It is initialized by
144 _initialize_auto_load. */
145 static char *auto_load_safe_path;
147 /* Vector of directory elements of AUTO_LOAD_SAFE_PATH with each one normalized
148 by tilde_expand and possibly each entries has added its gdb_realpath
149 counterpart. */
150 static VEC (char_ptr) *auto_load_safe_path_vec;
152 /* Expand $datadir and $debugdir in STRING according to the rules of
153 substitute_path_component. Return vector from dirnames_to_char_ptr_vec,
154 this vector must be freed by free_char_ptr_vec by the caller. */
156 static VEC (char_ptr) *
157 auto_load_expand_dir_vars (const char *string)
159 VEC (char_ptr) *dir_vec;
160 char *s;
162 s = xstrdup (string);
163 substitute_path_component (&s, "$datadir", gdb_datadir);
164 substitute_path_component (&s, "$debugdir", debug_file_directory);
166 if (debug_auto_load && strcmp (s, string) != 0)
167 fprintf_unfiltered (gdb_stdlog,
168 _("auto-load: Expanded $-variables to \"%s\".\n"), s);
170 dir_vec = dirnames_to_char_ptr_vec (s);
171 xfree(s);
173 return dir_vec;
176 /* Update auto_load_safe_path_vec from current AUTO_LOAD_SAFE_PATH. */
178 static void
179 auto_load_safe_path_vec_update (void)
181 unsigned len;
182 int ix;
184 if (debug_auto_load)
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
195 element. */
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);
205 if (debug_auto_load)
207 if (strcmp (expanded, dir) == 0)
208 fprintf_unfiltered (gdb_stdlog,
209 _("auto-load: Using directory \"%s\".\n"),
210 expanded);
211 else
212 fprintf_unfiltered (gdb_stdlog,
213 _("auto-load: Resolved directory \"%s\" "
214 "as \"%s\".\n"),
215 dir, expanded);
217 xfree (dir);
219 /* If gdb_realpath returns a different content, append it. */
220 if (strcmp (real_path, expanded) == 0)
221 xfree (real_path);
222 else
224 VEC_safe_push (char_ptr, auto_load_safe_path_vec, real_path);
226 if (debug_auto_load)
227 fprintf_unfiltered (gdb_stdlog,
228 _("auto-load: And canonicalized as \"%s\".\n"),
229 real_path);
234 /* Variable gdb_datadir has been set. Update content depending on $datadir. */
236 static void
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. */
244 static void
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. */
259 static void
260 show_auto_load_safe_path (struct ui_file *file, int from_tty,
261 struct cmd_list_element *c, const char *value)
263 const char *cs;
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));
270 cs++);
271 if (*cs == 0)
272 fprintf_filtered (file, _("Auto-load files are safe to load from any "
273 "directory.\n"));
274 else
275 fprintf_filtered (file, _("List of directories from which it is safe to "
276 "auto-load files is %s.\n"),
277 value);
280 /* "add-auto-load-safe-path" command for the auto_load_safe_path configuration
281 variable. */
283 static void
284 add_auto_load_safe_path (char *args, int from_tty)
286 char *s;
288 if (args == NULL || *args == 0)
289 error (_("\
290 Directory argument required.\n\
291 Use 'set auto-load safe-path /' for disabling the auto-load safe-path security.\
292 "));
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
302 and PATTERN. */
304 static int
305 filename_is_in_pattern_1 (char *filename, char *pattern)
307 size_t pattern_len = strlen (pattern);
308 size_t filename_len = strlen (filename);
310 if (debug_auto_load)
311 fprintf_unfiltered (gdb_stdlog, _("auto-load: Matching file \"%s\" "
312 "to pattern \"%s\"\n"),
313 filename, pattern);
315 /* Trim trailing slashes ("/") from PATTERN. Even for "d:\" paths as
316 trailing slashes are trimmed also from FILENAME it still matches
317 correctly. */
318 while (pattern_len && IS_DIR_SEPARATOR (pattern[pattern_len - 1]))
319 pattern_len--;
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)
327 if (debug_auto_load)
328 fprintf_unfiltered (gdb_stdlog,
329 _("auto-load: Matched - empty pattern\n"));
330 return 1;
333 for (;;)
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]))
338 filename_len--;
339 filename[filename_len] = '\0';
340 if (filename_len == 0)
342 if (debug_auto_load)
343 fprintf_unfiltered (gdb_stdlog,
344 _("auto-load: Not matched - pattern \"%s\".\n"),
345 pattern);
346 return 0;
349 if (gdb_filename_fnmatch (pattern, filename, FNM_FILE_NAME | FNM_NOESCAPE)
350 == 0)
352 if (debug_auto_load)
353 fprintf_unfiltered (gdb_stdlog, _("auto-load: Matched - file "
354 "\"%s\" to pattern \"%s\".\n"),
355 filename, pattern);
356 return 1;
359 /* Trim trailing FILENAME component. */
360 while (filename_len > 0 && !IS_DIR_SEPARATOR (filename[filename_len - 1]))
361 filename_len--;
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. */
388 static int
389 filename_is_in_auto_load_safe_path_vec (const char *filename,
390 char **filename_realp)
392 char *pattern;
393 int ix;
395 for (ix = 0; VEC_iterate (char_ptr, auto_load_safe_path_vec, ix, pattern);
396 ++ix)
397 if (*filename_realp == NULL && filename_is_in_pattern (filename, pattern))
398 break;
400 if (pattern == NULL)
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)
413 for (ix = 0;
414 VEC_iterate (char_ptr, auto_load_safe_path_vec, ix, pattern); ++ix)
415 if (filename_is_in_pattern (*filename_realp, pattern))
416 break;
419 if (pattern != NULL)
421 if (debug_auto_load)
422 fprintf_unfiltered (gdb_stdlog, _("auto-load: File \"%s\" matches "
423 "directory \"%s\".\n"),
424 filename, pattern);
425 return 1;
428 return 0;
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
437 directory. */
440 file_is_auto_load_safe (const char *filename, const char *debug_fmt, ...)
442 char *filename_real = NULL;
443 struct cleanup *back_to;
444 static int advice_printed = 0;
446 if (debug_auto_load)
448 va_list debug_args;
450 va_start (debug_args, debug_fmt);
451 vfprintf_unfiltered (gdb_stdlog, debug_fmt, debug_args);
452 va_end (debug_args);
455 back_to = make_cleanup (free_current_contents, &filename_real);
457 if (filename_is_in_auto_load_safe_path_vec (filename, &filename_real))
459 do_cleanups (back_to);
460 return 1;
463 auto_load_safe_path_vec_update ();
464 if (filename_is_in_auto_load_safe_path_vec (filename, &filename_real))
466 do_cleanups (back_to);
467 return 1;
470 warning (_("File \"%s\" auto-loading has been declined by your "
471 "`auto-load safe-path' set to \"%s\"."),
472 filename_real, auto_load_safe_path);
474 if (!advice_printed)
476 const char *homedir = getenv ("HOME");
477 char *homeinit;
479 if (homedir == NULL)
480 homedir = "$HOME";
481 homeinit = xstrprintf ("%s/%s", homedir, gdbinit);
482 make_cleanup (xfree, homeinit);
484 printf_filtered (_("\
485 To enable execution of this file add\n\
486 \tadd-auto-load-safe-path %s\n\
487 line to your configuration file \"%s\".\n\
488 To completely disable this security protection add\n\
489 \tset auto-load safe-path /\n\
490 line to your configuration file \"%s\".\n\
491 For more information about this security protection see the\n\
492 \"Auto-loading safe path\" section in the GDB manual. E.g., run from the shell:\n\
493 \tinfo \"(gdb)Auto-loading safe path\"\n"),
494 filename_real, homeinit, homeinit);
495 advice_printed = 1;
498 do_cleanups (back_to);
499 return 0;
502 /* Definition of script language for GDB canned sequences of commands. */
504 static const struct script_language script_language_gdb
505 = { GDB_AUTO_FILE_NAME, source_gdb_script_for_objfile };
507 static void
508 source_gdb_script_for_objfile (struct objfile *objfile, FILE *file,
509 const char *filename)
511 int is_safe;
512 struct auto_load_pspace_info *pspace_info;
513 volatile struct gdb_exception e;
515 is_safe = file_is_auto_load_safe (filename, _("auto-load: Loading canned "
516 "sequences of commands script "
517 "\"%s\" for objfile \"%s\".\n"),
518 filename, objfile->name);
520 /* Add this script to the hash table too so "info auto-load gdb-scripts"
521 can print it. */
522 pspace_info = get_auto_load_pspace_data_for_loading (current_program_space);
523 maybe_add_script (pspace_info, is_safe, filename, filename,
524 &script_language_gdb);
526 if (!is_safe)
527 return;
529 TRY_CATCH (e, RETURN_MASK_ALL)
531 script_from_file (file, filename);
533 exception_print (gdb_stderr, e);
536 /* For scripts specified in .debug_gdb_scripts, multiple objfiles may load
537 the same script. There's no point in loading the script multiple times,
538 and there can be a lot of objfiles and scripts, so we keep track of scripts
539 loaded this way. */
541 struct auto_load_pspace_info
543 /* For each program space we keep track of loaded scripts. */
544 struct htab *loaded_scripts;
546 /* Non-zero if we've issued the warning about an auto-load script not being
547 found. We only want to issue this warning once. */
548 int script_not_found_warning_printed;
551 /* Objects of this type are stored in the loaded script hash table. */
553 struct loaded_script
555 /* Name as provided by the objfile. */
556 const char *name;
558 /* Full path name or NULL if script wasn't found (or was otherwise
559 inaccessible). */
560 const char *full_path;
562 /* Non-zero if this script has been loaded. */
563 int loaded;
565 const struct script_language *language;
568 /* Per-program-space data key. */
569 static const struct program_space_data *auto_load_pspace_data;
571 static void
572 auto_load_pspace_data_cleanup (struct program_space *pspace, void *arg)
574 struct auto_load_pspace_info *info;
576 info = program_space_data (pspace, auto_load_pspace_data);
577 if (info != NULL)
579 if (info->loaded_scripts)
580 htab_delete (info->loaded_scripts);
581 xfree (info);
585 /* Get the current autoload data. If none is found yet, add it now. This
586 function always returns a valid object. */
588 static struct auto_load_pspace_info *
589 get_auto_load_pspace_data (struct program_space *pspace)
591 struct auto_load_pspace_info *info;
593 info = program_space_data (pspace, auto_load_pspace_data);
594 if (info == NULL)
596 info = XZALLOC (struct auto_load_pspace_info);
597 set_program_space_data (pspace, auto_load_pspace_data, info);
600 return info;
603 /* Hash function for the loaded script hash. */
605 static hashval_t
606 hash_loaded_script_entry (const void *data)
608 const struct loaded_script *e = data;
610 return htab_hash_string (e->name) ^ htab_hash_pointer (e->language);
613 /* Equality function for the loaded script hash. */
615 static int
616 eq_loaded_script_entry (const void *a, const void *b)
618 const struct loaded_script *ea = a;
619 const struct loaded_script *eb = b;
621 return strcmp (ea->name, eb->name) == 0 && ea->language == eb->language;
624 /* Initialize the table to track loaded scripts.
625 Each entry is hashed by the full path name. */
627 static void
628 init_loaded_scripts_info (struct auto_load_pspace_info *pspace_info)
630 /* Choose 31 as the starting size of the hash table, somewhat arbitrarily.
631 Space for each entry is obtained with one malloc so we can free them
632 easily. */
634 pspace_info->loaded_scripts = htab_create (31,
635 hash_loaded_script_entry,
636 eq_loaded_script_entry,
637 xfree);
639 pspace_info->script_not_found_warning_printed = FALSE;
642 /* Wrapper on get_auto_load_pspace_data to also allocate the hash table
643 for loading scripts. */
645 struct auto_load_pspace_info *
646 get_auto_load_pspace_data_for_loading (struct program_space *pspace)
648 struct auto_load_pspace_info *info;
650 info = get_auto_load_pspace_data (pspace);
651 if (info->loaded_scripts == NULL)
652 init_loaded_scripts_info (info);
654 return info;
657 /* Add script NAME in LANGUAGE to hash table of PSPACE_INFO. LOADED 1 if the
658 script has been (is going to) be loaded, 0 otherwise (such as if it has not
659 been found). FULL_PATH is NULL if the script wasn't found. The result is
660 true if the script was already in the hash table. */
663 maybe_add_script (struct auto_load_pspace_info *pspace_info, int loaded,
664 const char *name, const char *full_path,
665 const struct script_language *language)
667 struct htab *htab = pspace_info->loaded_scripts;
668 struct loaded_script **slot, entry;
669 int in_hash_table;
671 entry.name = name;
672 entry.language = language;
673 slot = (struct loaded_script **) htab_find_slot (htab, &entry, INSERT);
674 in_hash_table = *slot != NULL;
676 /* If this script is not in the hash table, add it. */
678 if (! in_hash_table)
680 char *p;
682 /* Allocate all space in one chunk so it's easier to free. */
683 *slot = xmalloc (sizeof (**slot)
684 + strlen (name) + 1
685 + (full_path != NULL ? (strlen (full_path) + 1) : 0));
686 p = ((char*) *slot) + sizeof (**slot);
687 strcpy (p, name);
688 (*slot)->name = p;
689 if (full_path != NULL)
691 p += strlen (p) + 1;
692 strcpy (p, full_path);
693 (*slot)->full_path = p;
695 else
696 (*slot)->full_path = NULL;
697 (*slot)->loaded = loaded;
698 (*slot)->language = language;
701 return in_hash_table;
704 /* Clear the table of loaded section scripts. */
706 static void
707 clear_section_scripts (void)
709 struct program_space *pspace = current_program_space;
710 struct auto_load_pspace_info *info;
712 info = program_space_data (pspace, auto_load_pspace_data);
713 if (info != NULL && info->loaded_scripts != NULL)
715 htab_delete (info->loaded_scripts);
716 info->loaded_scripts = NULL;
717 info->script_not_found_warning_printed = FALSE;
721 /* Look for the auto-load script in LANGUAGE associated with OBJFILE where
722 OBJFILE's gdb_realpath is REALNAME and load it. Return 1 if we found any
723 matching script, return 0 otherwise. */
725 static int
726 auto_load_objfile_script_1 (struct objfile *objfile, const char *realname,
727 const struct script_language *language)
729 char *filename, *debugfile;
730 int len, retval;
731 FILE *input;
732 struct cleanup *cleanups;
734 len = strlen (realname);
735 filename = xmalloc (len + strlen (language->suffix) + 1);
736 memcpy (filename, realname, len);
737 strcpy (filename + len, language->suffix);
739 cleanups = make_cleanup (xfree, filename);
741 input = fopen (filename, "r");
742 debugfile = filename;
743 if (debug_auto_load)
744 fprintf_unfiltered (gdb_stdlog, _("auto-load: Attempted file \"%s\" %s.\n"),
745 debugfile, input ? _("exists") : _("does not exist"));
747 if (!input)
749 VEC (char_ptr) *vec;
750 int ix;
751 char *dir;
753 /* Also try the same file in a subdirectory of gdb's data
754 directory. */
756 vec = auto_load_expand_dir_vars (auto_load_dir);
757 make_cleanup_free_char_ptr_vec (vec);
759 if (debug_auto_load)
760 fprintf_unfiltered (gdb_stdlog, _("auto-load: Searching 'set auto-load "
761 "scripts-directory' path \"%s\".\n"),
762 auto_load_dir);
764 for (ix = 0; VEC_iterate (char_ptr, vec, ix, dir); ++ix)
766 debugfile = xmalloc (strlen (dir) + strlen (filename) + 1);
767 strcpy (debugfile, dir);
769 /* FILENAME is absolute, so we don't need a "/" here. */
770 strcat (debugfile, filename);
772 make_cleanup (xfree, debugfile);
773 input = fopen (debugfile, "r");
774 if (debug_auto_load)
775 fprintf_unfiltered (gdb_stdlog, _("auto-load: Attempted file "
776 "\"%s\" %s.\n"),
777 debugfile,
778 input ? _("exists") : _("does not exist"));
779 if (input != NULL)
780 break;
784 if (input)
786 make_cleanup_fclose (input);
788 /* To preserve existing behaviour we don't check for whether the
789 script was already in the table, and always load it.
790 It's highly unlikely that we'd ever load it twice,
791 and these scripts are required to be idempotent under multiple
792 loads anyway. */
793 language->source_script_for_objfile (objfile, input, debugfile);
795 retval = 1;
797 else
798 retval = 0;
800 do_cleanups (cleanups);
801 return retval;
804 /* Look for the auto-load script in LANGUAGE associated with OBJFILE and load
805 it. */
807 void
808 auto_load_objfile_script (struct objfile *objfile,
809 const struct script_language *language)
811 char *realname = gdb_realpath (objfile->name);
812 struct cleanup *cleanups = make_cleanup (xfree, realname);
814 if (!auto_load_objfile_script_1 (objfile, realname, language))
816 /* For Windows/DOS .exe executables, strip the .exe suffix, so that
817 FOO-gdb.gdb could be used for FOO.exe, and try again. */
819 size_t len = strlen (realname);
820 const size_t lexe = sizeof (".exe") - 1;
822 if (len > lexe && strcasecmp (realname + len - lexe, ".exe") == 0)
824 len -= lexe;
825 realname[len] = '\0';
826 if (debug_auto_load)
827 fprintf_unfiltered (gdb_stdlog, _("auto-load: Stripped .exe suffix, "
828 "retrying with \"%s\".\n"),
829 realname);
830 auto_load_objfile_script_1 (objfile, realname, language);
834 do_cleanups (cleanups);
837 /* Load any auto-loaded scripts for OBJFILE. */
839 void
840 load_auto_scripts_for_objfile (struct objfile *objfile)
842 if (!global_auto_load)
843 return;
845 if (auto_load_gdb_scripts)
846 auto_load_objfile_script (objfile, &script_language_gdb);
848 gdbpy_load_auto_scripts_for_objfile (objfile);
851 /* This is a new_objfile observer callback to auto-load scripts.
853 Two flavors of auto-loaded scripts are supported.
854 1) based on the path to the objfile
855 2) from .debug_gdb_scripts section */
857 static void
858 auto_load_new_objfile (struct objfile *objfile)
860 if (!objfile)
862 /* OBJFILE is NULL when loading a new "main" symbol-file. */
863 clear_section_scripts ();
864 return;
867 load_auto_scripts_for_objfile (objfile);
870 /* Collect scripts to be printed in a vec. */
872 typedef struct loaded_script *loaded_script_ptr;
873 DEF_VEC_P (loaded_script_ptr);
875 struct collect_matching_scripts_data
877 VEC (loaded_script_ptr) **scripts_p;
879 const struct script_language *language;
882 /* Traversal function for htab_traverse.
883 Collect the entry if it matches the regexp. */
885 static int
886 collect_matching_scripts (void **slot, void *info)
888 struct loaded_script *script = *slot;
889 struct collect_matching_scripts_data *data = info;
891 if (script->language == data->language && re_exec (script->name))
892 VEC_safe_push (loaded_script_ptr, *data->scripts_p, script);
894 return 1;
897 /* Print SCRIPT. */
899 static void
900 print_script (struct loaded_script *script)
902 struct ui_out *uiout = current_uiout;
903 struct cleanup *chain;
905 chain = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
907 ui_out_field_string (uiout, "loaded", script->loaded ? "Yes" : "No");
908 ui_out_field_string (uiout, "script", script->name);
909 ui_out_text (uiout, "\n");
911 /* If the name isn't the full path, print it too. */
912 if (script->full_path != NULL
913 && strcmp (script->name, script->full_path) != 0)
915 ui_out_text (uiout, "\tfull name: ");
916 ui_out_field_string (uiout, "full_path", script->full_path);
917 ui_out_text (uiout, "\n");
920 do_cleanups (chain);
923 /* Helper for info_auto_load_scripts to sort the scripts by name. */
925 static int
926 sort_scripts_by_name (const void *ap, const void *bp)
928 const struct loaded_script *a = *(const struct loaded_script **) ap;
929 const struct loaded_script *b = *(const struct loaded_script **) bp;
931 return FILENAME_CMP (a->name, b->name);
934 /* Special internal GDB value of auto_load_info_scripts's PATTERN identify
935 the "info auto-load XXX" command has been executed through the general
936 "info auto-load" invocation. Extra newline will be printed if needed. */
937 char auto_load_info_scripts_pattern_nl[] = "";
939 /* Implementation for "info auto-load gdb-scripts"
940 (and "info auto-load python-scripts"). List scripts in LANGUAGE matching
941 PATTERN. FROM_TTY is the usual GDB boolean for user interactivity. */
943 void
944 auto_load_info_scripts (char *pattern, int from_tty,
945 const struct script_language *language)
947 struct ui_out *uiout = current_uiout;
948 struct auto_load_pspace_info *pspace_info;
949 struct cleanup *script_chain;
950 VEC (loaded_script_ptr) *scripts;
951 int nr_scripts;
953 dont_repeat ();
955 pspace_info = get_auto_load_pspace_data (current_program_space);
957 if (pattern && *pattern)
959 char *re_err = re_comp (pattern);
961 if (re_err)
962 error (_("Invalid regexp: %s"), re_err);
964 else
966 re_comp ("");
969 /* We need to know the number of rows before we build the table.
970 Plus we want to sort the scripts by name.
971 So first traverse the hash table collecting the matching scripts. */
973 scripts = VEC_alloc (loaded_script_ptr, 10);
974 script_chain = make_cleanup (VEC_cleanup (loaded_script_ptr), &scripts);
976 if (pspace_info != NULL && pspace_info->loaded_scripts != NULL)
978 struct collect_matching_scripts_data data = { &scripts, language };
980 /* Pass a pointer to scripts as VEC_safe_push can realloc space. */
981 htab_traverse_noresize (pspace_info->loaded_scripts,
982 collect_matching_scripts, &data);
985 nr_scripts = VEC_length (loaded_script_ptr, scripts);
987 /* Table header shifted right by preceding "gdb-scripts: " would not match
988 its columns. */
989 if (nr_scripts > 0 && pattern == auto_load_info_scripts_pattern_nl)
990 ui_out_text (uiout, "\n");
992 make_cleanup_ui_out_table_begin_end (uiout, 2, nr_scripts,
993 "AutoLoadedScriptsTable");
995 ui_out_table_header (uiout, 7, ui_left, "loaded", "Loaded");
996 ui_out_table_header (uiout, 70, ui_left, "script", "Script");
997 ui_out_table_body (uiout);
999 if (nr_scripts > 0)
1001 int i;
1002 loaded_script_ptr script;
1004 qsort (VEC_address (loaded_script_ptr, scripts),
1005 VEC_length (loaded_script_ptr, scripts),
1006 sizeof (loaded_script_ptr), sort_scripts_by_name);
1007 for (i = 0; VEC_iterate (loaded_script_ptr, scripts, i, script); ++i)
1008 print_script (script);
1011 do_cleanups (script_chain);
1013 if (nr_scripts == 0)
1015 if (pattern && *pattern)
1016 ui_out_message (uiout, 0, "No auto-load scripts matching %s.\n",
1017 pattern);
1018 else
1019 ui_out_message (uiout, 0, "No auto-load scripts.\n");
1023 /* Wrapper for "info auto-load gdb-scripts". */
1025 static void
1026 info_auto_load_gdb_scripts (char *pattern, int from_tty)
1028 auto_load_info_scripts (pattern, from_tty, &script_language_gdb);
1031 /* Implement 'info auto-load local-gdbinit'. */
1033 static void
1034 info_auto_load_local_gdbinit (char *args, int from_tty)
1036 if (auto_load_local_gdbinit_pathname == NULL)
1037 printf_filtered (_("Local .gdbinit file was not found.\n"));
1038 else if (auto_load_local_gdbinit_loaded)
1039 printf_filtered (_("Local .gdbinit file \"%s\" has been loaded.\n"),
1040 auto_load_local_gdbinit_pathname);
1041 else
1042 printf_filtered (_("Local .gdbinit file \"%s\" has not been loaded.\n"),
1043 auto_load_local_gdbinit_pathname);
1046 /* Return non-zero if SCRIPT_NOT_FOUND_WARNING_PRINTED of PSPACE_INFO was unset
1047 before calling this function. Always set SCRIPT_NOT_FOUND_WARNING_PRINTED
1048 of PSPACE_INFO. */
1051 script_not_found_warning_print (struct auto_load_pspace_info *pspace_info)
1053 int retval = !pspace_info->script_not_found_warning_printed;
1055 pspace_info->script_not_found_warning_printed = 1;
1057 return retval;
1060 /* The only valid "set auto-load" argument is off|0|no|disable. */
1062 static void
1063 set_auto_load_cmd (char *args, int from_tty)
1065 struct cmd_list_element *list;
1066 size_t length;
1068 /* See parse_binary_operation in use by the sub-commands. */
1070 length = args ? strlen (args) : 0;
1072 while (length > 0 && (args[length - 1] == ' ' || args[length - 1] == '\t'))
1073 length--;
1075 if (length == 0 || (strncmp (args, "off", length) != 0
1076 && strncmp (args, "0", length) != 0
1077 && strncmp (args, "no", length) != 0
1078 && strncmp (args, "disable", length) != 0))
1079 error (_("Valid is only global 'set auto-load no'; "
1080 "otherwise check the auto-load sub-commands."));
1082 for (list = *auto_load_set_cmdlist_get (); list != NULL; list = list->next)
1083 if (list->var_type == var_boolean)
1085 gdb_assert (list->type == set_cmd);
1086 do_set_command (args, from_tty, list);
1090 /* Initialize "set auto-load " commands prefix and return it. */
1092 struct cmd_list_element **
1093 auto_load_set_cmdlist_get (void)
1095 static struct cmd_list_element *retval;
1097 if (retval == NULL)
1098 add_prefix_cmd ("auto-load", class_maintenance, set_auto_load_cmd, _("\
1099 Auto-loading specific settings.\n\
1100 Configure various auto-load-specific variables such as\n\
1101 automatic loading of Python scripts."),
1102 &retval, "set auto-load ",
1103 1/*allow-unknown*/, &setlist);
1105 return &retval;
1108 /* Command "show auto-load" displays summary of all the current
1109 "show auto-load " settings. */
1111 static void
1112 show_auto_load_cmd (char *args, int from_tty)
1114 cmd_show_list (*auto_load_show_cmdlist_get (), from_tty, "");
1117 /* Initialize "show auto-load " commands prefix and return it. */
1119 struct cmd_list_element **
1120 auto_load_show_cmdlist_get (void)
1122 static struct cmd_list_element *retval;
1124 if (retval == NULL)
1125 add_prefix_cmd ("auto-load", class_maintenance, show_auto_load_cmd, _("\
1126 Show auto-loading specific settings.\n\
1127 Show configuration of various auto-load-specific variables such as\n\
1128 automatic loading of Python scripts."),
1129 &retval, "show auto-load ",
1130 0/*allow-unknown*/, &showlist);
1132 return &retval;
1135 /* Command "info auto-load" displays whether the various auto-load files have
1136 been loaded. This is reimplementation of cmd_show_list which inserts
1137 newlines at proper places. */
1139 static void
1140 info_auto_load_cmd (char *args, int from_tty)
1142 struct cmd_list_element *list;
1143 struct cleanup *infolist_chain;
1144 struct ui_out *uiout = current_uiout;
1146 infolist_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "infolist");
1148 for (list = *auto_load_info_cmdlist_get (); list != NULL; list = list->next)
1150 struct cleanup *option_chain
1151 = make_cleanup_ui_out_tuple_begin_end (uiout, "option");
1153 gdb_assert (!list->prefixlist);
1154 gdb_assert (list->type == not_set_cmd);
1156 ui_out_field_string (uiout, "name", list->name);
1157 ui_out_text (uiout, ": ");
1158 cmd_func (list, auto_load_info_scripts_pattern_nl, from_tty);
1160 /* Close the tuple. */
1161 do_cleanups (option_chain);
1164 /* Close the tuple. */
1165 do_cleanups (infolist_chain);
1168 /* Initialize "info auto-load " commands prefix and return it. */
1170 struct cmd_list_element **
1171 auto_load_info_cmdlist_get (void)
1173 static struct cmd_list_element *retval;
1175 if (retval == NULL)
1176 add_prefix_cmd ("auto-load", class_info, info_auto_load_cmd, _("\
1177 Print current status of auto-loaded files.\n\
1178 Print whether various files like Python scripts or .gdbinit files have been\n\
1179 found and/or loaded."),
1180 &retval, "info auto-load ",
1181 0/*allow-unknown*/, &infolist);
1183 return &retval;
1186 void _initialize_auto_load (void);
1188 void
1189 _initialize_auto_load (void)
1191 struct cmd_list_element *cmd;
1192 char *scripts_directory_help;
1194 auto_load_pspace_data
1195 = register_program_space_data_with_cleanup (NULL,
1196 auto_load_pspace_data_cleanup);
1198 observer_attach_new_objfile (auto_load_new_objfile);
1200 add_setshow_boolean_cmd ("gdb-scripts", class_support,
1201 &auto_load_gdb_scripts, _("\
1202 Enable or disable auto-loading of canned sequences of commands scripts."), _("\
1203 Show whether auto-loading of canned sequences of commands scripts is enabled."),
1204 _("\
1205 If enabled, canned sequences of commands are loaded when the debugger reads\n\
1206 an executable or shared library.\n\
1207 This options has security implications for untrusted inferiors."),
1208 NULL, show_auto_load_gdb_scripts,
1209 auto_load_set_cmdlist_get (),
1210 auto_load_show_cmdlist_get ());
1212 add_cmd ("gdb-scripts", class_info, info_auto_load_gdb_scripts,
1213 _("Print the list of automatically loaded sequences of commands.\n\
1214 Usage: info auto-load gdb-scripts [REGEXP]"),
1215 auto_load_info_cmdlist_get ());
1217 add_setshow_boolean_cmd ("local-gdbinit", class_support,
1218 &auto_load_local_gdbinit, _("\
1219 Enable or disable auto-loading of .gdbinit script in current directory."), _("\
1220 Show whether auto-loading .gdbinit script in current directory is enabled."),
1221 _("\
1222 If enabled, canned sequences of commands are loaded when debugger starts\n\
1223 from .gdbinit file in current directory. Such files are deprecated,\n\
1224 use a script associated with inferior executable file instead.\n\
1225 This options has security implications for untrusted inferiors."),
1226 NULL, show_auto_load_local_gdbinit,
1227 auto_load_set_cmdlist_get (),
1228 auto_load_show_cmdlist_get ());
1230 add_cmd ("local-gdbinit", class_info, info_auto_load_local_gdbinit,
1231 _("Print whether current directory .gdbinit file has been loaded.\n\
1232 Usage: info auto-load local-gdbinit"),
1233 auto_load_info_cmdlist_get ());
1235 auto_load_dir = xstrdup (AUTO_LOAD_DIR);
1236 scripts_directory_help = xstrprintf (
1237 #ifdef HAVE_PYTHON
1238 _("\
1239 Automatically loaded Python scripts (named OBJFILE%s) and GDB scripts\n\
1240 (named OBJFILE%s) are located in one of the directories listed by this\n\
1241 option.\n\
1242 %s"),
1243 GDBPY_AUTO_FILE_NAME,
1244 #else
1245 _("\
1246 Automatically loaded GDB scripts (named OBJFILE%s) are located in one\n\
1247 of the directories listed by this option.\n\
1248 %s"),
1249 #endif
1250 GDB_AUTO_FILE_NAME,
1251 _("\
1252 This option is ignored for the kinds of scripts \
1253 having 'set auto-load ... off'.\n\
1254 Directories listed here need to be present also \
1255 in the 'set auto-load safe-path'\n\
1256 option."));
1257 add_setshow_optional_filename_cmd ("scripts-directory", class_support,
1258 &auto_load_dir, _("\
1259 Set the list of directories from which to load auto-loaded scripts."), _("\
1260 Show the list of directories from which to load auto-loaded scripts."),
1261 scripts_directory_help,
1262 set_auto_load_dir, show_auto_load_dir,
1263 auto_load_set_cmdlist_get (),
1264 auto_load_show_cmdlist_get ());
1265 xfree (scripts_directory_help);
1267 auto_load_safe_path = xstrdup (AUTO_LOAD_SAFE_PATH);
1268 auto_load_safe_path_vec_update ();
1269 add_setshow_optional_filename_cmd ("safe-path", class_support,
1270 &auto_load_safe_path, _("\
1271 Set the list of files and directories that are safe for auto-loading."), _("\
1272 Show the list of files and directories that are safe for auto-loading."), _("\
1273 Various files loaded automatically for the 'set auto-load ...' options must\n\
1274 be located in one of the directories listed by this option. Warning will be\n\
1275 printed and file will not be used otherwise.\n\
1276 You can mix both directory and filename entries.\n\
1277 Setting this parameter to an empty list resets it to its default value.\n\
1278 Setting this parameter to '/' (without the quotes) allows any file\n\
1279 for the 'set auto-load ...' options. Each path entry can be also shell\n\
1280 wildcard pattern; '*' does not match directory separator.\n\
1281 This option is ignored for the kinds of files having 'set auto-load ... off'.\n\
1282 This options has security implications for untrusted inferiors."),
1283 set_auto_load_safe_path,
1284 show_auto_load_safe_path,
1285 auto_load_set_cmdlist_get (),
1286 auto_load_show_cmdlist_get ());
1287 observer_attach_gdb_datadir_changed (auto_load_gdb_datadir_changed);
1289 cmd = add_cmd ("add-auto-load-safe-path", class_support,
1290 add_auto_load_safe_path,
1291 _("Add entries to the list of directories from which it is safe "
1292 "to auto-load files.\n\
1293 See the commands 'set auto-load safe-path' and 'show auto-load safe-path' to\n\
1294 access the current full list setting."),
1295 &cmdlist);
1296 set_cmd_completer (cmd, filename_completer);
1298 add_setshow_boolean_cmd ("auto-load", class_maintenance,
1299 &debug_auto_load, _("\
1300 Set auto-load verifications debugging."), _("\
1301 Show auto-load verifications debugging."), _("\
1302 When non-zero, debugging output for files of 'set auto-load ...'\n\
1303 is displayed."),
1304 NULL, show_debug_auto_load,
1305 &setdebuglist, &showdebuglist);