GDB (xrefs)
/tmp/gdb-8.1/gdb/auto-load.c
Go to the documentation of this file.
1 /* GDB routines for supporting auto-loaded scripts.
2 
3  Copyright (C) 2012-2018 Free Software Foundation, Inc.
4 
5  This file is part of GDB.
6 
7  This program is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation; either version 3 of the License, or
10  (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 
20 #include "defs.h"
21 #include <ctype.h>
22 #include "auto-load.h"
23 #include "progspace.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 "cli/cli-script.h"
31 #include "gdbcmd.h"
32 #include "cli/cli-cmds.h"
33 #include "cli/cli-decode.h"
34 #include "cli/cli-setshow.h"
35 #include "readline/tilde.h"
36 #include "completer.h"
37 #include "fnmatch.h"
38 #include "top.h"
39 #include "filestuff.h"
40 #include "extension.h"
41 #include "gdb/section-scripts.h"
42 #include <algorithm>
43 
44 /* The section to look in for auto-loaded scripts (in file formats that
45  support sections).
46  Each entry in this section is a record that begins with a leading byte
47  identifying the record type.
48  At the moment we only support one record type: A leading byte of 1,
49  followed by the path of a python script to load. */
50 #define AUTO_SECTION_NAME ".debug_gdb_scripts"
51 
53  (struct auto_load_pspace_info *, struct objfile *objfile,
54  const struct extension_language_defn *language,
55  const char *section_name, unsigned offset);
56 
58  (struct auto_load_pspace_info *, struct objfile *objfile,
59  const struct extension_language_defn *language,
60  const char *section_name, unsigned offset);
61 
62 /* Value of the 'set debug auto-load' configuration variable. */
63 static int debug_auto_load = 0;
64 
65 /* "show" command for the debug_auto_load configuration variable. */
66 
67 static void
68 show_debug_auto_load (struct ui_file *file, int from_tty,
69  struct cmd_list_element *c, const char *value)
70 {
71  fprintf_filtered (file, _("Debugging output for files "
72  "of 'set auto-load ...' is %s.\n"),
73  value);
74 }
75 
76 /* User-settable option to enable/disable auto-loading of GDB_AUTO_FILE_NAME
77  scripts:
78  set auto-load gdb-scripts on|off
79  This is true if we should auto-load associated scripts when an objfile
80  is opened, false otherwise. */
81 static int auto_load_gdb_scripts = 1;
82 
83 /* "show" command for the auto_load_gdb_scripts configuration variable. */
84 
85 static void
86 show_auto_load_gdb_scripts (struct ui_file *file, int from_tty,
87  struct cmd_list_element *c, const char *value)
88 {
89  fprintf_filtered (file, _("Auto-loading of canned sequences of commands "
90  "scripts is %s.\n"),
91  value);
92 }
93 
94 /* Return non-zero if auto-loading gdb scripts is enabled. */
95 
96 int
98 {
99  return auto_load_gdb_scripts;
100 }
101 
102 /* Internal-use flag to enable/disable auto-loading.
103  This is true if we should auto-load python code when an objfile is opened,
104  false otherwise.
105 
106  Both auto_load_scripts && global_auto_load must be true to enable
107  auto-loading.
108 
109  This flag exists to facilitate deferring auto-loading during start-up
110  until after ./.gdbinit has been read; it may augment the search directories
111  used to find the scripts. */
113 
114 /* Auto-load .gdbinit file from the current directory? */
116 
117 /* Absolute pathname to the current directory .gdbinit, if it exists. */
119 
120 /* Boolean value if AUTO_LOAD_LOCAL_GDBINIT_PATHNAME has been loaded. */
122 
123 /* "show" command for the auto_load_local_gdbinit configuration variable. */
124 
125 static void
126 show_auto_load_local_gdbinit (struct ui_file *file, int from_tty,
127  struct cmd_list_element *c, const char *value)
128 {
129  fprintf_filtered (file, _("Auto-loading of .gdbinit script from current "
130  "directory is %s.\n"),
131  value);
132 }
133 
134 /* Directory list from which to load auto-loaded scripts. It is not checked
135  for absolute paths but they are strongly recommended. It is initialized by
136  _initialize_auto_load. */
137 static char *auto_load_dir;
138 
139 /* "set" command for the auto_load_dir configuration variable. */
140 
141 static void
142 set_auto_load_dir (const char *args, int from_tty, struct cmd_list_element *c)
143 {
144  /* Setting the variable to "" resets it to the compile time defaults. */
145  if (auto_load_dir[0] == '\0')
146  {
148  auto_load_dir = xstrdup (AUTO_LOAD_DIR);
149  }
150 }
151 
152 /* "show" command for the auto_load_dir configuration variable. */
153 
154 static void
155 show_auto_load_dir (struct ui_file *file, int from_tty,
156  struct cmd_list_element *c, const char *value)
157 {
158  fprintf_filtered (file, _("List of directories from which to load "
159  "auto-loaded scripts is %s.\n"),
160  value);
161 }
162 
163 /* Directory list safe to hold auto-loaded files. It is not checked for
164  absolute paths but they are strongly recommended. It is initialized by
165  _initialize_auto_load. */
166 static char *auto_load_safe_path;
167 
168 /* Vector of directory elements of AUTO_LOAD_SAFE_PATH with each one normalized
169  by tilde_expand and possibly each entries has added its gdb_realpath
170  counterpart. */
171 static VEC (char_ptr) *auto_load_safe_path_vec;
172 
173 /* Expand $datadir and $debugdir in STRING according to the rules of
174  substitute_path_component. Return vector from dirnames_to_char_ptr_vec,
175  this vector must be freed by free_char_ptr_vec by the caller. */
176 
177 static VEC (char_ptr) *
178 auto_load_expand_dir_vars (const char *string)
179 {
180  VEC (char_ptr) *dir_vec;
181  char *s;
182 
183  s = xstrdup (string);
184  substitute_path_component (&s, "$datadir", gdb_datadir);
186 
187  if (debug_auto_load && strcmp (s, string) != 0)
189  _("auto-load: Expanded $-variables to \"%s\".\n"), s);
190 
191  dir_vec = dirnames_to_char_ptr_vec (s);
192  xfree(s);
193 
194  return dir_vec;
195 }
196 
197 /* Update auto_load_safe_path_vec from current AUTO_LOAD_SAFE_PATH. */
198 
199 static void
201 {
202  unsigned len;
203  int ix;
204 
205  if (debug_auto_load)
207  _("auto-load: Updating directories of \"%s\".\n"),
209 
210  free_char_ptr_vec (auto_load_safe_path_vec);
211 
212  auto_load_safe_path_vec = auto_load_expand_dir_vars (auto_load_safe_path);
213  len = VEC_length (char_ptr, auto_load_safe_path_vec);
214 
215  /* Apply tilde_expand and gdb_realpath to each AUTO_LOAD_SAFE_PATH_VEC
216  element. */
217  for (ix = 0; ix < len; ix++)
218  {
219  char *dir = VEC_index (char_ptr, auto_load_safe_path_vec, ix);
220  char *expanded = tilde_expand (dir);
221  gdb::unique_xmalloc_ptr<char> real_path = gdb_realpath (expanded);
222 
223  /* Ensure the current entry is at least tilde_expand-ed. */
224  VEC_replace (char_ptr, auto_load_safe_path_vec, ix, expanded);
225 
226  if (debug_auto_load)
227  {
228  if (strcmp (expanded, dir) == 0)
230  _("auto-load: Using directory \"%s\".\n"),
231  expanded);
232  else
234  _("auto-load: Resolved directory \"%s\" "
235  "as \"%s\".\n"),
236  dir, expanded);
237  }
238  xfree (dir);
239 
240  /* If gdb_realpath returns a different content, append it. */
241  if (strcmp (real_path.get (), expanded) != 0)
242  {
243  if (debug_auto_load)
245  _("auto-load: And canonicalized as \"%s\".\n"),
246  real_path.get ());
247 
248  VEC_safe_push (char_ptr, auto_load_safe_path_vec,
249  real_path.release ());
250  }
251  }
252 }
253 
254 /* Variable gdb_datadir has been set. Update content depending on $datadir. */
255 
256 static void
258 {
260 }
261 
262 /* "set" command for the auto_load_safe_path configuration variable. */
263 
264 static void
265 set_auto_load_safe_path (const char *args,
266  int from_tty, struct cmd_list_element *c)
267 {
268  /* Setting the variable to "" resets it to the compile time defaults. */
269  if (auto_load_safe_path[0] == '\0')
270  {
273  }
274 
276 }
277 
278 /* "show" command for the auto_load_safe_path configuration variable. */
279 
280 static void
281 show_auto_load_safe_path (struct ui_file *file, int from_tty,
282  struct cmd_list_element *c, const char *value)
283 {
284  const char *cs;
285 
286  /* Check if user has entered either "/" or for example ":".
287  But while more complicate content like ":/foo" would still also
288  permit any location do not hide those. */
289 
290  for (cs = value; *cs && (*cs == DIRNAME_SEPARATOR || IS_DIR_SEPARATOR (*cs));
291  cs++);
292  if (*cs == 0)
293  fprintf_filtered (file, _("Auto-load files are safe to load from any "
294  "directory.\n"));
295  else
296  fprintf_filtered (file, _("List of directories from which it is safe to "
297  "auto-load files is %s.\n"),
298  value);
299 }
300 
301 /* "add-auto-load-safe-path" command for the auto_load_safe_path configuration
302  variable. */
303 
304 static void
305 add_auto_load_safe_path (const char *args, int from_tty)
306 {
307  char *s;
308 
309  if (args == NULL || *args == 0)
310  error (_("\
311 Directory argument required.\n\
312 Use 'set auto-load safe-path /' for disabling the auto-load safe-path security.\
313 "));
314 
315  s = xstrprintf ("%s%c%s", auto_load_safe_path, DIRNAME_SEPARATOR, args);
318 
320 }
321 
322 /* "add-auto-load-scripts-directory" command for the auto_load_dir configuration
323  variable. */
324 
325 static void
326 add_auto_load_dir (const char *args, int from_tty)
327 {
328  char *s;
329 
330  if (args == NULL || *args == 0)
331  error (_("Directory argument required."));
332 
333  s = xstrprintf ("%s%c%s", auto_load_dir, DIRNAME_SEPARATOR, args);
335  auto_load_dir = s;
336 }
337 
338 /* Implementation for filename_is_in_pattern overwriting the caller's FILENAME
339  and PATTERN. */
340 
341 static int
342 filename_is_in_pattern_1 (char *filename, char *pattern)
343 {
344  size_t pattern_len = strlen (pattern);
345  size_t filename_len = strlen (filename);
346 
347  if (debug_auto_load)
348  fprintf_unfiltered (gdb_stdlog, _("auto-load: Matching file \"%s\" "
349  "to pattern \"%s\"\n"),
350  filename, pattern);
351 
352  /* Trim trailing slashes ("/") from PATTERN. Even for "d:\" paths as
353  trailing slashes are trimmed also from FILENAME it still matches
354  correctly. */
355  while (pattern_len && IS_DIR_SEPARATOR (pattern[pattern_len - 1]))
356  pattern_len--;
357  pattern[pattern_len] = '\0';
358 
359  /* Ensure auto_load_safe_path "/" matches any FILENAME. On MS-Windows
360  platform FILENAME even after gdb_realpath does not have to start with
361  IS_DIR_SEPARATOR character, such as the 'C:\x.exe' filename. */
362  if (pattern_len == 0)
363  {
364  if (debug_auto_load)
366  _("auto-load: Matched - empty pattern\n"));
367  return 1;
368  }
369 
370  for (;;)
371  {
372  /* Trim trailing slashes ("/"). PATTERN also has slashes trimmed the
373  same way so they will match. */
374  while (filename_len && IS_DIR_SEPARATOR (filename[filename_len - 1]))
375  filename_len--;
376  filename[filename_len] = '\0';
377  if (filename_len == 0)
378  {
379  if (debug_auto_load)
381  _("auto-load: Not matched - pattern \"%s\".\n"),
382  pattern);
383  return 0;
384  }
385 
386  if (gdb_filename_fnmatch (pattern, filename, FNM_FILE_NAME | FNM_NOESCAPE)
387  == 0)
388  {
389  if (debug_auto_load)
390  fprintf_unfiltered (gdb_stdlog, _("auto-load: Matched - file "
391  "\"%s\" to pattern \"%s\".\n"),
392  filename, pattern);
393  return 1;
394  }
395 
396  /* Trim trailing FILENAME component. */
397  while (filename_len > 0 && !IS_DIR_SEPARATOR (filename[filename_len - 1]))
398  filename_len--;
399  }
400 }
401 
402 /* Return 1 if FILENAME matches PATTERN or if FILENAME resides in
403  a subdirectory of a directory that matches PATTERN. Return 0 otherwise.
404  gdb_realpath normalization is never done here. */
405 
406 static ATTRIBUTE_PURE int
407 filename_is_in_pattern (const char *filename, const char *pattern)
408 {
409  char *filename_copy, *pattern_copy;
410 
411  filename_copy = (char *) alloca (strlen (filename) + 1);
412  strcpy (filename_copy, filename);
413  pattern_copy = (char *) alloca (strlen (pattern) + 1);
414  strcpy (pattern_copy, pattern);
415 
416  return filename_is_in_pattern_1 (filename_copy, pattern_copy);
417 }
418 
419 /* Return 1 if FILENAME belongs to one of directory components of
420  AUTO_LOAD_SAFE_PATH_VEC. Return 0 otherwise.
421  auto_load_safe_path_vec_update is never called.
422  *FILENAME_REALP may be updated by gdb_realpath of FILENAME. */
423 
424 static int
426  gdb::unique_xmalloc_ptr<char> *filename_realp)
427 {
428  char *pattern;
429  int ix;
430 
431  for (ix = 0; VEC_iterate (char_ptr, auto_load_safe_path_vec, ix, pattern);
432  ++ix)
433  if (*filename_realp == NULL && filename_is_in_pattern (filename, pattern))
434  break;
435 
436  if (pattern == NULL)
437  {
438  if (*filename_realp == NULL)
439  {
440  *filename_realp = gdb_realpath (filename);
441  if (debug_auto_load && strcmp (filename_realp->get (), filename) != 0)
443  _("auto-load: Resolved "
444  "file \"%s\" as \"%s\".\n"),
445  filename, filename_realp->get ());
446  }
447 
448  if (strcmp (filename_realp->get (), filename) != 0)
449  for (ix = 0;
450  VEC_iterate (char_ptr, auto_load_safe_path_vec, ix, pattern); ++ix)
451  if (filename_is_in_pattern (filename_realp->get (), pattern))
452  break;
453  }
454 
455  if (pattern != NULL)
456  {
457  if (debug_auto_load)
458  fprintf_unfiltered (gdb_stdlog, _("auto-load: File \"%s\" matches "
459  "directory \"%s\".\n"),
460  filename, pattern);
461  return 1;
462  }
463 
464  return 0;
465 }
466 
467 /* Return 1 if FILENAME is located in one of the directories of
468  AUTO_LOAD_SAFE_PATH. Otherwise call warning and return 0. FILENAME does
469  not have to be an absolute path.
470 
471  Existence of FILENAME is not checked. Function will still give a warning
472  even if the caller would quietly skip non-existing file in unsafe
473  directory. */
474 
475 int
476 file_is_auto_load_safe (const char *filename, const char *debug_fmt, ...)
477 {
478  gdb::unique_xmalloc_ptr<char> filename_real;
479  static int advice_printed = 0;
480 
481  if (debug_auto_load)
482  {
483  va_list debug_args;
484 
485  va_start (debug_args, debug_fmt);
486  vfprintf_unfiltered (gdb_stdlog, debug_fmt, debug_args);
487  va_end (debug_args);
488  }
489 
490  if (filename_is_in_auto_load_safe_path_vec (filename, &filename_real))
491  return 1;
492 
494  if (filename_is_in_auto_load_safe_path_vec (filename, &filename_real))
495  return 1;
496 
497  warning (_("File \"%s\" auto-loading has been declined by your "
498  "`auto-load safe-path' set to \"%s\"."),
499  filename_real.get (), auto_load_safe_path);
500 
501  if (!advice_printed)
502  {
503  const char *homedir = getenv ("HOME");
504 
505  if (homedir == NULL)
506  homedir = "$HOME";
507  std::string homeinit = string_printf ("%s/%s", homedir, gdbinit);
508 
509  printf_filtered (_("\
510 To enable execution of this file add\n\
511 \tadd-auto-load-safe-path %s\n\
512 line to your configuration file \"%s\".\n\
513 To completely disable this security protection add\n\
514 \tset auto-load safe-path /\n\
515 line to your configuration file \"%s\".\n\
516 For more information about this security protection see the\n\
517 \"Auto-loading safe path\" section in the GDB manual. E.g., run from the shell:\n\
518 \tinfo \"(gdb)Auto-loading safe path\"\n"),
519  filename_real.get (),
520  homeinit.c_str (), homeinit.c_str ());
521  advice_printed = 1;
522  }
523 
524  return 0;
525 }
526 
527 /* For scripts specified in .debug_gdb_scripts, multiple objfiles may load
528  the same script. There's no point in loading the script multiple times,
529  and there can be a lot of objfiles and scripts, so we keep track of scripts
530  loaded this way. */
531 
533 {
534  /* For each program space we keep track of loaded scripts, both when
535  specified as file names and as scripts to be executed directly. */
536  struct htab *loaded_script_files;
537  struct htab *loaded_script_texts;
538 
539  /* Non-zero if we've issued the warning about an auto-load script not being
540  supported. We only want to issue this warning once. */
542 
543  /* Non-zero if we've issued the warning about an auto-load script not being
544  found. We only want to issue this warning once. */
546 };
547 
548 /* Objects of this type are stored in the loaded_script hash table. */
549 
551 {
552  /* Name as provided by the objfile. */
553  const char *name;
554 
555  /* Full path name or NULL if script wasn't found (or was otherwise
556  inaccessible), or NULL for loaded_script_texts. */
557  const char *full_path;
558 
559  /* Non-zero if this script has been loaded. */
560  int loaded;
561 
563 };
564 
565 /* Per-program-space data key. */
566 static const struct program_space_data *auto_load_pspace_data;
567 
568 static void
569 auto_load_pspace_data_cleanup (struct program_space *pspace, void *arg)
570 {
571  struct auto_load_pspace_info *info = (struct auto_load_pspace_info *) arg;
572 
573  if (info->loaded_script_files)
574  htab_delete (info->loaded_script_files);
575  if (info->loaded_script_texts)
576  htab_delete (info->loaded_script_texts);
577  xfree (info);
578 }
579 
580 /* Get the current autoload data. If none is found yet, add it now. This
581  function always returns a valid object. */
582 
583 static struct auto_load_pspace_info *
585 {
586  struct auto_load_pspace_info *info;
587 
588  info = ((struct auto_load_pspace_info *)
589  program_space_data (pspace, auto_load_pspace_data));
590  if (info == NULL)
591  {
592  info = XCNEW (struct auto_load_pspace_info);
593  set_program_space_data (pspace, auto_load_pspace_data, info);
594  }
595 
596  return info;
597 }
598 
599 /* Hash function for the loaded script hash. */
600 
601 static hashval_t
602 hash_loaded_script_entry (const void *data)
603 {
604  const struct loaded_script *e = (const struct loaded_script *) data;
605 
606  return htab_hash_string (e->name) ^ htab_hash_pointer (e->language);
607 }
608 
609 /* Equality function for the loaded script hash. */
610 
611 static int
612 eq_loaded_script_entry (const void *a, const void *b)
613 {
614  const struct loaded_script *ea = (const struct loaded_script *) a;
615  const struct loaded_script *eb = (const struct loaded_script *) b;
616 
617  return strcmp (ea->name, eb->name) == 0 && ea->language == eb->language;
618 }
619 
620 /* Initialize the table to track loaded scripts.
621  Each entry is hashed by the full path name. */
622 
623 static void
625 {
626  /* Choose 31 as the starting size of the hash table, somewhat arbitrarily.
627  Space for each entry is obtained with one malloc so we can free them
628  easily. */
629 
630  pspace_info->loaded_script_files = htab_create (31,
633  xfree);
634  pspace_info->loaded_script_texts = htab_create (31,
637  xfree);
638 
639  pspace_info->unsupported_script_warning_printed = FALSE;
640  pspace_info->script_not_found_warning_printed = FALSE;
641 }
642 
643 /* Wrapper on get_auto_load_pspace_data to also allocate the hash table
644  for loading scripts. */
645 
646 struct auto_load_pspace_info *
648 {
649  struct auto_load_pspace_info *info;
650 
651  info = get_auto_load_pspace_data (pspace);
652  if (info->loaded_script_files == NULL)
654 
655  return info;
656 }
657 
658 /* Add script file NAME in LANGUAGE to hash table of PSPACE_INFO.
659  LOADED 1 if the script has been (is going to) be loaded, 0 otherwise
660  (such as if it has not been found).
661  FULL_PATH is NULL if the script wasn't found.
662  The result is true if the script was already in the hash table. */
663 
664 static int
665 maybe_add_script_file (struct auto_load_pspace_info *pspace_info, int loaded,
666  const char *name, const char *full_path,
667  const struct extension_language_defn *language)
668 {
669  struct htab *htab = pspace_info->loaded_script_files;
670  struct loaded_script **slot, entry;
671  int in_hash_table;
672 
673  entry.name = name;
674  entry.language = language;
675  slot = (struct loaded_script **) htab_find_slot (htab, &entry, INSERT);
676  in_hash_table = *slot != NULL;
677 
678  /* If this script is not in the hash table, add it. */
679 
680  if (!in_hash_table)
681  {
682  char *p;
683 
684  /* Allocate all space in one chunk so it's easier to free. */
685  *slot = ((struct loaded_script *)
686  xmalloc (sizeof (**slot)
687  + strlen (name) + 1
688  + (full_path != NULL ? (strlen (full_path) + 1) : 0)));
689  p = ((char*) *slot) + sizeof (**slot);
690  strcpy (p, name);
691  (*slot)->name = p;
692  if (full_path != NULL)
693  {
694  p += strlen (p) + 1;
695  strcpy (p, full_path);
696  (*slot)->full_path = p;
697  }
698  else
699  (*slot)->full_path = NULL;
700  (*slot)->loaded = loaded;
701  (*slot)->language = language;
702  }
703 
704  return in_hash_table;
705 }
706 
707 /* Add script contents NAME in LANGUAGE to hash table of PSPACE_INFO.
708  LOADED 1 if the script has been (is going to) be loaded, 0 otherwise
709  (such as if it has not been found).
710  The result is true if the script was already in the hash table. */
711 
712 static int
714  int loaded, const char *name,
715  const struct extension_language_defn *language)
716 {
717  struct htab *htab = pspace_info->loaded_script_texts;
718  struct loaded_script **slot, entry;
719  int in_hash_table;
720 
721  entry.name = name;
722  entry.language = language;
723  slot = (struct loaded_script **) htab_find_slot (htab, &entry, INSERT);
724  in_hash_table = *slot != NULL;
725 
726  /* If this script is not in the hash table, add it. */
727 
728  if (!in_hash_table)
729  {
730  char *p;
731 
732  /* Allocate all space in one chunk so it's easier to free. */
733  *slot = ((struct loaded_script *)
734  xmalloc (sizeof (**slot) + strlen (name) + 1));
735  p = ((char*) *slot) + sizeof (**slot);
736  strcpy (p, name);
737  (*slot)->name = p;
738  (*slot)->full_path = NULL;
739  (*slot)->loaded = loaded;
740  (*slot)->language = language;
741  }
742 
743  return in_hash_table;
744 }
745 
746 /* Clear the table of loaded section scripts. */
747 
748 static void
750 {
751  struct program_space *pspace = current_program_space;
752  struct auto_load_pspace_info *info;
753 
754  info = ((struct auto_load_pspace_info *)
755  program_space_data (pspace, auto_load_pspace_data));
756  if (info != NULL && info->loaded_script_files != NULL)
757  {
758  htab_delete (info->loaded_script_files);
759  htab_delete (info->loaded_script_texts);
760  info->loaded_script_files = NULL;
761  info->loaded_script_texts = NULL;
763  info->script_not_found_warning_printed = FALSE;
764  }
765 }
766 
767 /* Look for the auto-load script in LANGUAGE associated with OBJFILE where
768  OBJFILE's gdb_realpath is REALNAME and load it. Return 1 if we found any
769  matching script, return 0 otherwise. */
770 
771 static int
772 auto_load_objfile_script_1 (struct objfile *objfile, const char *realname,
773  const struct extension_language_defn *language)
774 {
775  char *filename, *debugfile;
776  int len, retval;
777  struct cleanup *cleanups;
778  const char *suffix = ext_lang_auto_load_suffix (language);
779 
780  len = strlen (realname);
781  filename = (char *) xmalloc (len + strlen (suffix) + 1);
782  memcpy (filename, realname, len);
783  strcpy (filename + len, suffix);
784 
785  cleanups = make_cleanup (xfree, filename);
786 
787  gdb_file_up input = gdb_fopen_cloexec (filename, "r");
788  debugfile = filename;
789  if (debug_auto_load)
790  fprintf_unfiltered (gdb_stdlog, _("auto-load: Attempted file \"%s\" %s.\n"),
791  debugfile, input ? _("exists") : _("does not exist"));
792 
793  if (!input)
794  {
795  VEC (char_ptr) *vec;
796  int ix;
797  char *dir;
798 
799  /* Also try the same file in a subdirectory of gdb's data
800  directory. */
801 
802  vec = auto_load_expand_dir_vars (auto_load_dir);
804 
805  if (debug_auto_load)
806  fprintf_unfiltered (gdb_stdlog, _("auto-load: Searching 'set auto-load "
807  "scripts-directory' path \"%s\".\n"),
808  auto_load_dir);
809 
810  for (ix = 0; VEC_iterate (char_ptr, vec, ix, dir); ++ix)
811  {
812  debugfile = (char *) xmalloc (strlen (dir) + strlen (filename) + 1);
813  strcpy (debugfile, dir);
814 
815  /* FILENAME is absolute, so we don't need a "/" here. */
816  strcat (debugfile, filename);
817 
818  make_cleanup (xfree, debugfile);
819  input = gdb_fopen_cloexec (debugfile, "r");
820  if (debug_auto_load)
821  fprintf_unfiltered (gdb_stdlog, _("auto-load: Attempted file "
822  "\"%s\" %s.\n"),
823  debugfile,
824  input ? _("exists") : _("does not exist"));
825  if (input != NULL)
826  break;
827  }
828  }
829 
830  if (input)
831  {
832  int is_safe;
833  struct auto_load_pspace_info *pspace_info;
834 
835  is_safe
836  = file_is_auto_load_safe (debugfile,
837  _("auto-load: Loading %s script \"%s\""
838  " by extension for objfile \"%s\".\n"),
840  debugfile, objfile_name (objfile));
841 
842  /* Add this script to the hash table too so
843  "info auto-load ${lang}-scripts" can print it. */
844  pspace_info
846  maybe_add_script_file (pspace_info, is_safe, debugfile, debugfile,
847  language);
848 
849  /* To preserve existing behaviour we don't check for whether the
850  script was already in the table, and always load it.
851  It's highly unlikely that we'd ever load it twice,
852  and these scripts are required to be idempotent under multiple
853  loads anyway. */
854  if (is_safe)
855  {
858 
859  /* We shouldn't get here if support for the language isn't
860  compiled in. And the extension language is required to implement
861  this function. */
862  gdb_assert (sourcer != NULL);
863  sourcer (language, objfile, input.get (), debugfile);
864  }
865 
866  retval = 1;
867  }
868  else
869  retval = 0;
870 
871  do_cleanups (cleanups);
872  return retval;
873 }
874 
875 /* Look for the auto-load script in LANGUAGE associated with OBJFILE and load
876  it. */
877 
878 void
880  const struct extension_language_defn *language)
881 {
884 
885  if (!auto_load_objfile_script_1 (objfile, realname.get (), language))
886  {
887  /* For Windows/DOS .exe executables, strip the .exe suffix, so that
888  FOO-gdb.gdb could be used for FOO.exe, and try again. */
889 
890  size_t len = strlen (realname.get ());
891  const size_t lexe = sizeof (".exe") - 1;
892 
893  if (len > lexe && strcasecmp (realname.get () + len - lexe, ".exe") == 0)
894  {
895  len -= lexe;
896  realname.get ()[len] = '\0';
897  if (debug_auto_load)
898  fprintf_unfiltered (gdb_stdlog, _("auto-load: Stripped .exe suffix, "
899  "retrying with \"%s\".\n"),
900  realname.get ());
901  auto_load_objfile_script_1 (objfile, realname.get (), language);
902  }
903  }
904 }
905 
906 /* Subroutine of source_section_scripts to simplify it.
907  Load FILE as a script in extension language LANGUAGE.
908  The script is from section SECTION_NAME in OBJFILE at offset OFFSET. */
909 
910 static void
912  struct objfile *objfile,
913  const struct extension_language_defn *language,
914  const char *section_name, unsigned int offset,
915  const char *file)
916 {
917  int in_hash_table;
919 
920  /* Skip this script if support is not compiled in. */
922  if (sourcer == NULL)
923  {
924  /* We don't throw an error, the program is still debuggable. */
926  section_name, offset);
927  /* We *could* still try to open it, but there's no point. */
928  maybe_add_script_file (pspace_info, 0, file, NULL, language);
929  return;
930  }
931 
932  /* Skip this script if auto-loading it has been disabled. */
934  {
935  /* No message is printed, just skip it. */
936  return;
937  }
938 
940  1 /*search_path*/);
941 
942  if (opened)
943  {
944  if (!file_is_auto_load_safe (opened->full_path.get (),
945  _("auto-load: Loading %s script "
946  "\"%s\" from section \"%s\" of "
947  "objfile \"%s\".\n"),
949  opened->full_path.get (),
950  section_name, objfile_name (objfile)))
951  opened.reset ();
952  }
953  else
954  {
955  /* If one script isn't found it's not uncommon for more to not be
956  found either. We don't want to print a message for each script,
957  too much noise. Instead, we print the warning once and tell the
958  user how to find the list of scripts that weren't loaded.
959  We don't throw an error, the program is still debuggable.
960 
961  IWBN if complaints.c were more general-purpose. */
962 
964  section_name, offset);
965  }
966 
967  in_hash_table = maybe_add_script_file (pspace_info, bool (opened), file,
968  (opened
969  ? opened->full_path.get ()
970  : NULL),
971  language);
972 
973  /* If this file is not currently loaded, load it. */
974  if (opened && !in_hash_table)
975  sourcer (language, objfile, opened->stream.get (),
976  opened->full_path.get ());
977 }
978 
979 /* Subroutine of source_section_scripts to simplify it.
980  Execute SCRIPT as a script in extension language LANG.
981  The script is from section SECTION_NAME in OBJFILE at offset OFFSET. */
982 
983 static void
985  struct objfile *objfile,
986  const struct extension_language_defn *language,
987  const char *section_name, unsigned int offset,
988  const char *script)
989 {
991  const char *newline, *script_text;
992  char *name;
993  int is_safe, in_hash_table;
994  struct cleanup *cleanups;
995 
996  cleanups = make_cleanup (null_cleanup, NULL);
997 
998  /* The first line of the script is the name of the script.
999  It must not contain any kind of space character. */
1000  name = NULL;
1001  newline = strchr (script, '\n');
1002  if (newline != NULL)
1003  {
1004  char *buf, *p;
1005 
1006  /* Put the name in a buffer and validate it. */
1007  buf = xstrndup (script, newline - script);
1008  make_cleanup (xfree, buf);
1009  for (p = buf; *p != '\0'; ++p)
1010  {
1011  if (isspace (*p))
1012  break;
1013  }
1014  /* We don't allow nameless scripts, they're not helpful to the user. */
1015  if (p != buf && *p == '\0')
1016  name = buf;
1017  }
1018  if (name == NULL)
1019  {
1020  /* We don't throw an error, the program is still debuggable. */
1021  warning (_("\
1022 Missing/bad script name in entry at offset %u in section %s\n\
1023 of file %s."),
1024  offset, section_name, objfile_name (objfile));
1025  do_cleanups (cleanups);
1026  return;
1027  }
1028  script_text = newline + 1;
1029 
1030  /* Skip this script if support is not compiled in. */
1032  if (executor == NULL)
1033  {
1034  /* We don't throw an error, the program is still debuggable. */
1036  section_name, offset);
1037  maybe_add_script_text (pspace_info, 0, name, language);
1038  do_cleanups (cleanups);
1039  return;
1040  }
1041 
1042  /* Skip this script if auto-loading it has been disabled. */
1044  {
1045  /* No message is printed, just skip it. */
1046  do_cleanups (cleanups);
1047  return;
1048  }
1049 
1051  _("auto-load: Loading %s script "
1052  "\"%s\" from section \"%s\" of "
1053  "objfile \"%s\".\n"),
1055  section_name, objfile_name (objfile));
1056 
1057  in_hash_table = maybe_add_script_text (pspace_info, is_safe, name, language);
1058 
1059  /* If this file is not currently loaded, load it. */
1060  if (is_safe && !in_hash_table)
1061  executor (language, objfile, name, script_text);
1062 
1063  do_cleanups (cleanups);
1064 }
1065 
1066 /* Load scripts specified in OBJFILE.
1067  START,END delimit a buffer containing a list of nul-terminated
1068  file names.
1069  SECTION_NAME is used in error messages.
1070 
1071  Scripts specified as file names are found per normal "source -s" command
1072  processing. First the script is looked for in $cwd. If not found there
1073  the source search path is used.
1074 
1075  The section contains a list of path names of script files to load or
1076  actual script contents. Each entry is nul-terminated. */
1077 
1078 static void
1079 source_section_scripts (struct objfile *objfile, const char *section_name,
1080  const char *start, const char *end)
1081 {
1082  const char *p;
1083  struct auto_load_pspace_info *pspace_info;
1084 
1086 
1087  for (p = start; p < end; ++p)
1088  {
1089  const char *entry;
1090  const struct extension_language_defn *language;
1091  unsigned int offset = p - start;
1092  int code = *p;
1093 
1094  switch (code)
1095  {
1096  case SECTION_SCRIPT_ID_PYTHON_FILE:
1097  case SECTION_SCRIPT_ID_PYTHON_TEXT:
1099  break;
1100  case SECTION_SCRIPT_ID_SCHEME_FILE:
1101  case SECTION_SCRIPT_ID_SCHEME_TEXT:
1103  break;
1104  default:
1105  warning (_("Invalid entry in %s section"), section_name);
1106  /* We could try various heuristics to find the next valid entry,
1107  but it's safer to just punt. */
1108  return;
1109  }
1110  entry = ++p;
1111 
1112  while (p < end && *p != '\0')
1113  ++p;
1114  if (p == end)
1115  {
1116  warning (_("Non-nul-terminated entry in %s at offset %u"),
1117  section_name, offset);
1118  /* Don't load/execute it. */
1119  break;
1120  }
1121 
1122  switch (code)
1123  {
1124  case SECTION_SCRIPT_ID_PYTHON_FILE:
1125  case SECTION_SCRIPT_ID_SCHEME_FILE:
1126  if (p == entry)
1127  {
1128  warning (_("Empty entry in %s at offset %u"),
1129  section_name, offset);
1130  continue;
1131  }
1132  source_script_file (pspace_info, objfile, language,
1133  section_name, offset, entry);
1134  break;
1135  case SECTION_SCRIPT_ID_PYTHON_TEXT:
1136  case SECTION_SCRIPT_ID_SCHEME_TEXT:
1137  execute_script_contents (pspace_info, objfile, language,
1138  section_name, offset, entry);
1139  break;
1140  }
1141  }
1142 }
1143 
1144 /* Load scripts specified in section SECTION_NAME of OBJFILE. */
1145 
1146 static void
1147 auto_load_section_scripts (struct objfile *objfile, const char *section_name)
1148 {
1149  bfd *abfd = objfile->obfd;
1150  asection *scripts_sect;
1151  bfd_byte *data = NULL;
1152 
1153  scripts_sect = bfd_get_section_by_name (abfd, section_name);
1154  if (scripts_sect == NULL
1155  || (bfd_get_section_flags (abfd, scripts_sect) & SEC_HAS_CONTENTS) == 0)
1156  return;
1157 
1158  if (!bfd_get_full_section_contents (abfd, scripts_sect, &data))
1159  warning (_("Couldn't read %s section of %s"),
1160  section_name, bfd_get_filename (abfd));
1161  else
1162  {
1163  struct cleanup *cleanups;
1164  char *p = (char *) data;
1165 
1166  cleanups = make_cleanup (xfree, p);
1167  source_section_scripts (objfile, section_name, p,
1168  p + bfd_get_section_size (scripts_sect));
1169  do_cleanups (cleanups);
1170  }
1171 }
1172 
1173 /* Load any auto-loaded scripts for OBJFILE. */
1174 
1175 void
1177 {
1178  /* Return immediately if auto-loading has been globally disabled.
1179  This is to handle sequencing of operations during gdb startup.
1180  Also return immediately if OBJFILE was not created from a file
1181  on the local filesystem. */
1182  if (!global_auto_load
1183  || (objfile->flags & OBJF_NOT_FILENAME) != 0
1185  return;
1186 
1187  /* Load any extension language scripts for this objfile.
1188  E.g., foo-gdb.gdb, foo-gdb.py. */
1190 
1191  /* Load any scripts mentioned in AUTO_SECTION_NAME (.debug_gdb_scripts). */
1193 }
1194 
1195 /* This is a new_objfile observer callback to auto-load scripts.
1196 
1197  Two flavors of auto-loaded scripts are supported.
1198  1) based on the path to the objfile
1199  2) from .debug_gdb_scripts section */
1200 
1201 static void
1203 {
1204  if (!objfile)
1205  {
1206  /* OBJFILE is NULL when loading a new "main" symbol-file. */
1208  return;
1209  }
1210 
1212 }
1213 
1214 /* Collect scripts to be printed in a vec. */
1215 
1217 {
1218  collect_matching_scripts_data (std::vector<loaded_script *> *scripts_p_,
1219  const extension_language_defn *language_)
1220  : scripts_p (scripts_p_), language (language_)
1221  {}
1222 
1223  std::vector<loaded_script *> *scripts_p;
1225 };
1226 
1227 /* Traversal function for htab_traverse.
1228  Collect the entry if it matches the regexp. */
1229 
1230 static int
1231 collect_matching_scripts (void **slot, void *info)
1232 {
1233  struct loaded_script *script = (struct loaded_script *) *slot;
1234  struct collect_matching_scripts_data *data
1235  = (struct collect_matching_scripts_data *) info;
1236 
1237  if (script->language == data->language && re_exec (script->name))
1238  data->scripts_p->push_back (script);
1239 
1240  return 1;
1241 }
1242 
1243 /* Print SCRIPT. */
1244 
1245 static void
1246 print_script (struct loaded_script *script)
1247 {
1248  struct ui_out *uiout = current_uiout;
1249 
1250  ui_out_emit_tuple tuple_emitter (uiout, NULL);
1251 
1252  uiout->field_string ("loaded", script->loaded ? "Yes" : "No");
1253  uiout->field_string ("script", script->name);
1254  uiout->text ("\n");
1255 
1256  /* If the name isn't the full path, print it too. */
1257  if (script->full_path != NULL
1258  && strcmp (script->name, script->full_path) != 0)
1259  {
1260  uiout->text ("\tfull name: ");
1261  uiout->field_string ("full_path", script->full_path);
1262  uiout->text ("\n");
1263  }
1264 }
1265 
1266 /* Helper for info_auto_load_scripts to sort the scripts by name. */
1267 
1268 static bool
1270 {
1271  return FILENAME_CMP (a->name, b->name) < 0;
1272 }
1273 
1274 /* Special internal GDB value of auto_load_info_scripts's PATTERN identify
1275  the "info auto-load XXX" command has been executed through the general
1276  "info auto-load" invocation. Extra newline will be printed if needed. */
1278 
1279 /* Subroutine of auto_load_info_scripts to simplify it.
1280  Print SCRIPTS. */
1281 
1282 static void
1283 print_scripts (const std::vector<loaded_script *> &scripts)
1284 {
1285  for (loaded_script *script : scripts)
1286  print_script (script);
1287 }
1288 
1289 /* Implementation for "info auto-load gdb-scripts"
1290  (and "info auto-load python-scripts"). List scripts in LANGUAGE matching
1291  PATTERN. FROM_TTY is the usual GDB boolean for user interactivity. */
1292 
1293 void
1294 auto_load_info_scripts (const char *pattern, int from_tty,
1295  const struct extension_language_defn *language)
1296 {
1297  struct ui_out *uiout = current_uiout;
1298  struct auto_load_pspace_info *pspace_info;
1299 
1300  dont_repeat ();
1301 
1303 
1304  if (pattern && *pattern)
1305  {
1306  char *re_err = re_comp (pattern);
1307 
1308  if (re_err)
1309  error (_("Invalid regexp: %s"), re_err);
1310  }
1311  else
1312  {
1313  re_comp ("");
1314  }
1315 
1316  /* We need to know the number of rows before we build the table.
1317  Plus we want to sort the scripts by name.
1318  So first traverse the hash table collecting the matching scripts. */
1319 
1320  std::vector<loaded_script *> script_files, script_texts;
1321 
1322  if (pspace_info != NULL && pspace_info->loaded_script_files != NULL)
1323  {
1324  collect_matching_scripts_data data (&script_files, language);
1325 
1326  /* Pass a pointer to scripts as VEC_safe_push can realloc space. */
1327  htab_traverse_noresize (pspace_info->loaded_script_files,
1328  collect_matching_scripts, &data);
1329 
1330  std::sort (script_files.begin (), script_files.end (),
1332  }
1333 
1334  if (pspace_info != NULL && pspace_info->loaded_script_texts != NULL)
1335  {
1336  collect_matching_scripts_data data (&script_texts, language);
1337 
1338  /* Pass a pointer to scripts as VEC_safe_push can realloc space. */
1339  htab_traverse_noresize (pspace_info->loaded_script_texts,
1340  collect_matching_scripts, &data);
1341 
1342  std::sort (script_texts.begin (), script_texts.end (),
1344  }
1345 
1346  int nr_scripts = script_files.size () + script_texts.size ();
1347 
1348  /* Table header shifted right by preceding "gdb-scripts: " would not match
1349  its columns. */
1350  if (nr_scripts > 0 && pattern == auto_load_info_scripts_pattern_nl)
1351  uiout->text ("\n");
1352 
1353  {
1354  ui_out_emit_table table_emitter (uiout, 2, nr_scripts,
1355  "AutoLoadedScriptsTable");
1356 
1357  uiout->table_header (7, ui_left, "loaded", "Loaded");
1358  uiout->table_header (70, ui_left, "script", "Script");
1359  uiout->table_body ();
1360 
1361  print_scripts (script_files);
1362  print_scripts (script_texts);
1363  }
1364 
1365  if (nr_scripts == 0)
1366  {
1367  if (pattern && *pattern)
1368  uiout->message ("No auto-load scripts matching %s.\n", pattern);
1369  else
1370  uiout->message ("No auto-load scripts.\n");
1371  }
1372 }
1373 
1374 /* Wrapper for "info auto-load gdb-scripts". */
1375 
1376 static void
1377 info_auto_load_gdb_scripts (const char *pattern, int from_tty)
1378 {
1379  auto_load_info_scripts (pattern, from_tty, &extension_language_gdb);
1380 }
1381 
1382 /* Implement 'info auto-load local-gdbinit'. */
1383 
1384 static void
1385 info_auto_load_local_gdbinit (const char *args, int from_tty)
1386 {
1388  printf_filtered (_("Local .gdbinit file was not found.\n"));
1390  printf_filtered (_("Local .gdbinit file \"%s\" has been loaded.\n"),
1392  else
1393  printf_filtered (_("Local .gdbinit file \"%s\" has not been loaded.\n"),
1395 }
1396 
1397 /* Print an "unsupported script" warning if it has not already been printed.
1398  The script is in language LANGUAGE at offset OFFSET in section SECTION_NAME
1399  of OBJFILE. */
1400 
1401 static void
1403  (struct auto_load_pspace_info *pspace_info,
1404  struct objfile *objfile, const struct extension_language_defn *language,
1405  const char *section_name, unsigned offset)
1406 {
1407  if (!pspace_info->unsupported_script_warning_printed)
1408  {
1409  warning (_("\
1410 Unsupported auto-load script at offset %u in section %s\n\
1411 of file %s.\n\
1412 Use `info auto-load %s-scripts [REGEXP]' to list them."),
1413  offset, section_name, objfile_name (objfile),
1415  pspace_info->unsupported_script_warning_printed = 1;
1416  }
1417 }
1418 
1419 /* Return non-zero if SCRIPT_NOT_FOUND_WARNING_PRINTED of PSPACE_INFO was unset
1420  before calling this function. Always set SCRIPT_NOT_FOUND_WARNING_PRINTED
1421  of PSPACE_INFO. */
1422 
1423 static void
1425  (struct auto_load_pspace_info *pspace_info,
1426  struct objfile *objfile, const struct extension_language_defn *language,
1427  const char *section_name, unsigned offset)
1428 {
1429  if (!pspace_info->script_not_found_warning_printed)
1430  {
1431  warning (_("\
1432 Missing auto-load script at offset %u in section %s\n\
1433 of file %s.\n\
1434 Use `info auto-load %s-scripts [REGEXP]' to list them."),
1435  offset, section_name, objfile_name (objfile),
1437  pspace_info->script_not_found_warning_printed = 1;
1438  }
1439 }
1440 
1441 /* The only valid "set auto-load" argument is off|0|no|disable. */
1442 
1443 static void
1444 set_auto_load_cmd (const char *args, int from_tty)
1445 {
1446  struct cmd_list_element *list;
1447  size_t length;
1448 
1449  /* See parse_binary_operation in use by the sub-commands. */
1450 
1451  length = args ? strlen (args) : 0;
1452 
1453  while (length > 0 && (args[length - 1] == ' ' || args[length - 1] == '\t'))
1454  length--;
1455 
1456  if (length == 0 || (strncmp (args, "off", length) != 0
1457  && strncmp (args, "0", length) != 0
1458  && strncmp (args, "no", length) != 0
1459  && strncmp (args, "disable", length) != 0))
1460  error (_("Valid is only global 'set auto-load no'; "
1461  "otherwise check the auto-load sub-commands."));
1462 
1463  for (list = *auto_load_set_cmdlist_get (); list != NULL; list = list->next)
1464  if (list->var_type == var_boolean)
1465  {
1466  gdb_assert (list->type == set_cmd);
1467  do_set_command (args, from_tty, list);
1468  }
1469 }
1470 
1471 /* Initialize "set auto-load " commands prefix and return it. */
1472 
1473 struct cmd_list_element **
1475 {
1476  static struct cmd_list_element *retval;
1477 
1478  if (retval == NULL)
1480 Auto-loading specific settings.\n\
1481 Configure various auto-load-specific variables such as\n\
1482 automatic loading of Python scripts."),
1483  &retval, "set auto-load ",
1484  1/*allow-unknown*/, &setlist);
1485 
1486  return &retval;
1487 }
1488 
1489 /* Command "show auto-load" displays summary of all the current
1490  "show auto-load " settings. */
1491 
1492 static void
1493 show_auto_load_cmd (const char *args, int from_tty)
1494 {
1495  cmd_show_list (*auto_load_show_cmdlist_get (), from_tty, "");
1496 }
1497 
1498 /* Initialize "show auto-load " commands prefix and return it. */
1499 
1500 struct cmd_list_element **
1502 {
1503  static struct cmd_list_element *retval;
1504 
1505  if (retval == NULL)
1507 Show auto-loading specific settings.\n\
1508 Show configuration of various auto-load-specific variables such as\n\
1509 automatic loading of Python scripts."),
1510  &retval, "show auto-load ",
1511  0/*allow-unknown*/, &showlist);
1512 
1513  return &retval;
1514 }
1515 
1516 /* Command "info auto-load" displays whether the various auto-load files have
1517  been loaded. This is reimplementation of cmd_show_list which inserts
1518  newlines at proper places. */
1519 
1520 static void
1521 info_auto_load_cmd (const char *args, int from_tty)
1522 {
1523  struct cmd_list_element *list;
1524  struct ui_out *uiout = current_uiout;
1525 
1526  ui_out_emit_tuple tuple_emitter (uiout, "infolist");
1527 
1528  for (list = *auto_load_info_cmdlist_get (); list != NULL; list = list->next)
1529  {
1530  ui_out_emit_tuple option_emitter (uiout, "option");
1531 
1532  gdb_assert (!list->prefixlist);
1533  gdb_assert (list->type == not_set_cmd);
1534 
1535  uiout->field_string ("name", list->name);
1536  uiout->text (": ");
1537  cmd_func (list, auto_load_info_scripts_pattern_nl, from_tty);
1538  }
1539 }
1540 
1541 /* Initialize "info auto-load " commands prefix and return it. */
1542 
1543 struct cmd_list_element **
1545 {
1546  static struct cmd_list_element *retval;
1547 
1548  if (retval == NULL)
1549  add_prefix_cmd ("auto-load", class_info, info_auto_load_cmd, _("\
1550 Print current status of auto-loaded files.\n\
1551 Print whether various files like Python scripts or .gdbinit files have been\n\
1552 found and/or loaded."),
1553  &retval, "info auto-load ",
1554  0/*allow-unknown*/, &infolist);
1555 
1556  return &retval;
1557 }
1558 
1559 void
1561 {
1562  struct cmd_list_element *cmd;
1563  char *scripts_directory_help, *gdb_name_help, *python_name_help;
1564  char *guile_name_help;
1565  const char *suffix;
1566 
1568  = register_program_space_data_with_cleanup (NULL,
1570 
1572 
1573  add_setshow_boolean_cmd ("gdb-scripts", class_support,
1575 Enable or disable auto-loading of canned sequences of commands scripts."), _("\
1576 Show whether auto-loading of canned sequences of commands scripts is enabled."),
1577  _("\
1578 If enabled, canned sequences of commands are loaded when the debugger reads\n\
1579 an executable or shared library.\n\
1580 This options has security implications for untrusted inferiors."),
1584 
1586  _("Print the list of automatically loaded sequences of commands.\n\
1587 Usage: info auto-load gdb-scripts [REGEXP]"),
1589 
1590  add_setshow_boolean_cmd ("local-gdbinit", class_support,
1592 Enable or disable auto-loading of .gdbinit script in current directory."), _("\
1593 Show whether auto-loading .gdbinit script in current directory is enabled."),
1594  _("\
1595 If enabled, canned sequences of commands are loaded when debugger starts\n\
1596 from .gdbinit file in current directory. Such files are deprecated,\n\
1597 use a script associated with inferior executable file instead.\n\
1598 This options has security implications for untrusted inferiors."),
1602 
1603  add_cmd ("local-gdbinit", class_info, info_auto_load_local_gdbinit,
1604  _("Print whether current directory .gdbinit file has been loaded.\n\
1605 Usage: info auto-load local-gdbinit"),
1607 
1608  auto_load_dir = xstrdup (AUTO_LOAD_DIR);
1609 
1611  gdb_name_help
1612  = xstrprintf (_("\
1613 GDB scripts: OBJFILE%s\n"),
1614  suffix);
1615  python_name_help = NULL;
1616 #ifdef HAVE_PYTHON
1618  python_name_help
1619  = xstrprintf (_("\
1620 Python scripts: OBJFILE%s\n"),
1621  suffix);
1622 #endif
1623  guile_name_help = NULL;
1624 #ifdef HAVE_GUILE
1626  guile_name_help
1627  = xstrprintf (_("\
1628 Guile scripts: OBJFILE%s\n"),
1629  suffix);
1630 #endif
1631  scripts_directory_help
1632  = xstrprintf (_("\
1633 Automatically loaded scripts are located in one of the directories listed\n\
1634 by this option.\n\
1635 \n\
1636 Script names:\n\
1637 %s%s%s\
1638 \n\
1639 This option is ignored for the kinds of scripts \
1640 having 'set auto-load ... off'.\n\
1641 Directories listed here need to be present also \
1642 in the 'set auto-load safe-path'\n\
1643 option."),
1644  gdb_name_help,
1645  python_name_help ? python_name_help : "",
1646  guile_name_help ? guile_name_help : "");
1647 
1648  add_setshow_optional_filename_cmd ("scripts-directory", class_support,
1649  &auto_load_dir, _("\
1650 Set the list of directories from which to load auto-loaded scripts."), _("\
1651 Show the list of directories from which to load auto-loaded scripts."),
1652  scripts_directory_help,
1656  xfree (scripts_directory_help);
1657  xfree (python_name_help);
1658  xfree (gdb_name_help);
1659  xfree (guile_name_help);
1660 
1664  &auto_load_safe_path, _("\
1665 Set the list of files and directories that are safe for auto-loading."), _("\
1666 Show the list of files and directories that are safe for auto-loading."), _("\
1667 Various files loaded automatically for the 'set auto-load ...' options must\n\
1668 be located in one of the directories listed by this option. Warning will be\n\
1669 printed and file will not be used otherwise.\n\
1670 You can mix both directory and filename entries.\n\
1671 Setting this parameter to an empty list resets it to its default value.\n\
1672 Setting this parameter to '/' (without the quotes) allows any file\n\
1673 for the 'set auto-load ...' options. Each path entry can be also shell\n\
1674 wildcard pattern; '*' does not match directory separator.\n\
1675 This option is ignored for the kinds of files having 'set auto-load ... off'.\n\
1676 This options has security implications for untrusted inferiors."),
1682 
1683  cmd = add_cmd ("add-auto-load-safe-path", class_support,
1685  _("Add entries to the list of directories from which it is safe "
1686  "to auto-load files.\n\
1687 See the commands 'set auto-load safe-path' and 'show auto-load safe-path' to\n\
1688 access the current full list setting."),
1689  &cmdlist);
1691 
1692  cmd = add_cmd ("add-auto-load-scripts-directory", class_support,
1694  _("Add entries to the list of directories from which to load "
1695  "auto-loaded scripts.\n\
1696 See the commands 'set auto-load scripts-directory' and\n\
1697 'show auto-load scripts-directory' to access the current full list setting."),
1698  &cmdlist);
1700 
1702  &debug_auto_load, _("\
1703 Set auto-load verifications debugging."), _("\
1704 Show auto-load verifications debugging."), _("\
1705 When non-zero, debugging output for files of 'set auto-load ...'\n\
1706 is displayed."),
1707  NULL, show_debug_auto_load,
1709 }
objfile_flags flags
Definition: objfiles.h:311
const char * string
Definition: signals.c:50
int unsupported_script_warning_printed
Definition: auto-load.c:541
const char * name
Definition: cli-decode.h:52
int script_not_found_warning_printed
Definition: auto-load.c:545
static void auto_load_safe_path_vec_update(void)
Definition: auto-load.c:200
static void show_auto_load_local_gdbinit(struct ui_file *file, int from_tty, struct cmd_list_element *c, const char *value)
Definition: auto-load.c:126
struct cmd_list_element * next
Definition: cli-decode.h:49
static void show_debug_auto_load(struct ui_file *file, int from_tty, struct cmd_list_element *c, const char *value)
Definition: auto-load.c:68
bfd * obfd
Definition: objfiles.h:342
#define VEC_replace(T, V, I, O)
Definition: vec.h:318
void field_string(const char *fldname, const char *string)
Definition: ui-out.c:544
const struct extension_language_defn extension_language_gdb
Definition: extension.c:69
static void execute_script_contents(struct auto_load_pspace_info *pspace_info, struct objfile *objfile, const struct extension_language_defn *language, const char *section_name, unsigned int offset, const char *script)
Definition: auto-load.c:984
static void maybe_print_unsupported_script_warning(struct auto_load_pspace_info *, struct objfile *objfile, const struct extension_language_defn *language, const char *section_name, unsigned offset)
Definition: auto-load.c:1403
static int auto_load_objfile_script_1(struct objfile *objfile, const char *realname, const struct extension_language_defn *language)
Definition: auto-load.c:772
void xfree(void *)
__extension__ enum cmd_types type
Definition: cli-decode.h:100
void warning(const char *fmt,...)
Definition: errors.c:26
static bool sort_scripts_by_name(loaded_script *a, loaded_script *b)
Definition: auto-load.c:1269
const struct extension_language_defn * language
Definition: auto-load.c:562
int auto_load_local_gdbinit_loaded
Definition: auto-load.c:121
void objfile_script_executor_func(const struct extension_language_defn *, struct objfile *, const char *name, const char *script)
Definition: extension.h:54
std::vector< loaded_script * > * scripts_p
Definition: auto-load.c:1223
struct cmd_list_element ** auto_load_set_cmdlist_get(void)
Definition: auto-load.c:1474
void table_header(int width, ui_align align, const std::string &col_name, const std::string &col_hdr)
Definition: ui-out.c:365
#define AUTO_LOAD_SAFE_PATH
Definition: config.h:11
static void add_auto_load_safe_path(const char *args, int from_tty)
Definition: auto-load.c:305
struct cmd_list_element * add_cmd(const char *name, enum command_class theclass, const char *doc, struct cmd_list_element **list)
Definition: cli-decode.c:262
char auto_load_info_scripts_pattern_nl[]
Definition: auto-load.c:1277
#define DIRNAME_SEPARATOR
Definition: host-defs.h:54
void filename_completer(struct cmd_list_element *ignore, completion_tracker &tracker, const char *text, const char *word)
Definition: completer.c:152
#define AUTO_SECTION_NAME
Definition: auto-load.c:50
static ATTRIBUTE_PURE int filename_is_in_pattern(const char *filename, const char *pattern)
Definition: auto-load.c:407
#define VEC_safe_push(T, V, O)
Definition: vec.h:276
static void add_auto_load_dir(const char *args, int from_tty)
Definition: auto-load.c:326
struct cmd_list_element * cmdlist
Definition: cli-cmds.c:79
gdb::unique_xmalloc_ptr< char > gdb_realpath(const char *filename)
Definition: utils.c:2842
char * auto_load_local_gdbinit_pathname
Definition: auto-load.c:118
#define _(String)
Definition: gdb_locale.h:35
const char gdbinit[]
const char * ext_lang_name(const struct extension_language_defn *extlang)
Definition: extension.c:227
static void info_auto_load_cmd(const char *args, int from_tty)
Definition: auto-load.c:1521
Definition: ui-out.h:44
char * original_name
Definition: objfiles.h:305
char * char_ptr
Definition: gdb_vecs.h:25
void substitute_path_component(char **stringp, const char *from, const char *to)
Definition: utils.c:3203
collect_matching_scripts_data(std::vector< loaded_script *> *scripts_p_, const extension_language_defn *language_)
Definition: auto-load.c:1218
static struct auto_load_pspace_info * get_auto_load_pspace_data(struct program_space *pspace)
Definition: auto-load.c:584
void text(const char *string)
Definition: ui-out.c:581
char * debug_file_directory
Definition: symfile.c:1414
static void show_auto_load_dir(struct ui_file *file, int from_tty, struct cmd_list_element *c, const char *value)
Definition: auto-load.c:155
void printf_filtered(const char *format,...)
Definition: utils.c:2045
gdb_file_up stream
Definition: cli-cmds.h:126
struct cmd_list_element * add_prefix_cmd(const char *name, enum command_class theclass, cmd_const_cfunc_ftype *fun, const char *doc, struct cmd_list_element **prefixlist, const char *prefixname, int allow_unknown, struct cmd_list_element **list)
Definition: cli-decode.c:367
void null_cleanup(void *arg)
Definition: cleanups.c:294
void auto_load_objfile_script(struct objfile *objfile, const struct extension_language_defn *language)
Definition: auto-load.c:879
static void auto_load_new_objfile(struct objfile *objfile)
Definition: auto-load.c:1202
struct cmd_list_element * infolist
Definition: cli-cmds.c:83
void _initialize_auto_load(void)
Definition: auto-load.c:1560
struct cmd_list_element * setlist
Definition: cli-cmds.c:111
const char *const name
Definition: aarch64-tdep.c:76
#define VEC_iterate(T, V, I, P)
Definition: vec.h:181
static void print_scripts(const std::vector< loaded_script *> &scripts)
Definition: auto-load.c:1283
struct htab * loaded_script_files
Definition: auto-load.c:536
int auto_load_local_gdbinit
Definition: auto-load.c:115
std::unique_ptr< FILE, gdb_file_deleter > gdb_file_up
Definition: filestuff.h:59
static int debug_auto_load
Definition: auto-load.c:63
void objfile_script_sourcer_func(const struct extension_language_defn *, struct objfile *, FILE *stream, const char *filename)
Definition: extension.h:48
static void source_section_scripts(struct objfile *objfile, const char *section_name, const char *start, const char *end)
Definition: auto-load.c:1079
std::unique_ptr< T, xfree_deleter< T > > unique_xmalloc_ptr
void load_auto_scripts_for_objfile(struct objfile *objfile)
Definition: auto-load.c:1176
void cmd_func(struct cmd_list_element *cmd, const char *args, int from_tty)
Definition: cli-decode.c:1877
void fprintf_filtered(struct ui_file *stream, const char *format,...)
Definition: utils.c:2008
static int filename_is_in_pattern_1(char *filename, char *pattern)
Definition: auto-load.c:342
const struct extension_language_defn * language
Definition: auto-load.c:1224
void free_char_ptr_vec(VEC(char_ptr) *char_ptr_vec)
Definition: gdb_vecs.c:32
void set_cmd_completer(struct cmd_list_element *cmd, completer_ftype *completer)
Definition: cli-decode.c:160
void fprintf_unfiltered(struct ui_file *stream, const char *format,...)
Definition: utils.c:2018
static int maybe_add_script_text(struct auto_load_pspace_info *pspace_info, int loaded, const char *name, const struct extension_language_defn *language)
Definition: auto-load.c:713
void vfprintf_unfiltered(struct ui_file *stream, const char *format, va_list args)
Definition: utils.c:1969
struct cmd_list_element * showlist
Definition: cli-cmds.c:119
static void show_auto_load_cmd(const char *args, int from_tty)
Definition: auto-load.c:1493
#define VEC_length(T, V)
Definition: vec.h:140
#define current_uiout
Definition: ui-out.h:39
const char * name
Definition: auto-load.c:553
static void source_script_file(struct auto_load_pspace_info *pspace_info, struct objfile *objfile, const struct extension_language_defn *language, const char *section_name, unsigned int offset, const char *file)
Definition: auto-load.c:911
__extension__ enum var_types var_type
Definition: cli-decode.h:103
struct cleanup * make_cleanup(make_cleanup_ftype *function, void *arg)
Definition: cleanups.c:116
static void set_auto_load_dir(const char *args, int from_tty, struct cmd_list_element *c)
Definition: auto-load.c:142
#define VEC_index(T, V, I)
Definition: vec.h:167
static void auto_load_gdb_datadir_changed(void)
Definition: auto-load.c:257
char * gdb_datadir
Definition: main.c:62
static void info_auto_load_local_gdbinit(const char *args, int from_tty)
Definition: auto-load.c:1385
gdb_file_up gdb_fopen_cloexec(const char *filename, const char *opentype)
Definition: filestuff.c:296
static char * auto_load_dir
Definition: auto-load.c:137
static int collect_matching_scripts(void **slot, void *info)
Definition: auto-load.c:1231
static void set_auto_load_safe_path(const char *args, int from_tty, struct cmd_list_element *c)
Definition: auto-load.c:265
char * xstrprintf(const char *format,...)
Definition: common-utils.c:107
const char * full_path
Definition: auto-load.c:557
int file_is_auto_load_safe(const char *filename, const char *debug_fmt,...)
Definition: auto-load.c:476
struct cmd_list_element * setdebuglist
Definition: cli-cmds.c:153
int global_auto_load
Definition: auto-load.c:112
const char * objfile_name(const struct objfile *objfile)
Definition: objfiles.c:1557
void * xmalloc(YYSIZE_T)
void cmd_show_list(struct cmd_list_element *list, int from_tty, const char *prefix)
Definition: cli-setshow.c:657
static int maybe_add_script_file(struct auto_load_pspace_info *pspace_info, int loaded, const char *name, const char *full_path, const struct extension_language_defn *language)
Definition: auto-load.c:665
const struct extension_language_defn * get_ext_lang_defn(enum extension_language lang)
Definition: extension.c:116
#define gdb_assert(expr)
Definition: gdb_assert.h:32
Definition: value.c:169
static void show_auto_load_safe_path(struct ui_file *file, int from_tty, struct cmd_list_element *c, const char *value)
Definition: auto-load.c:281
Definition: ui-out.h:77
static const struct program_space_data * auto_load_pspace_data
Definition: auto-load.c:566
struct auto_load_pspace_info * get_auto_load_pspace_data_for_loading(struct program_space *pspace)
Definition: auto-load.c:647
static char * auto_load_safe_path
Definition: auto-load.c:166
static int eq_loaded_script_entry(const void *a, const void *b)
Definition: auto-load.c:612
static void info_auto_load_gdb_scripts(const char *pattern, int from_tty)
Definition: auto-load.c:1377
static void init_loaded_scripts_info(struct auto_load_pspace_info *pspace_info)
Definition: auto-load.c:624
#define XCNEW(T)
Definition: poison.h:121
void table_body()
Definition: ui-out.c:379
int auto_load_gdb_scripts_enabled(const struct extension_language_defn *extlang)
Definition: auto-load.c:97
const char * ext_lang_auto_load_suffix(const struct extension_language_defn *extlang)
Definition: extension.c:251
int offset
Definition: agent.c:65
void add_setshow_boolean_cmd(const char *name, enum command_class theclass, int *var, const char *set_doc, const char *show_doc, const char *help_doc, cmd_const_sfunc_ftype *set_func, show_value_ftype *show_func, struct cmd_list_element **set_list, struct cmd_list_element **show_list)
Definition: cli-decode.c:569
int code
Definition: ser-unix.c:239
#define AUTO_LOAD_DIR
Definition: config.h:8
void message(const char *format,...) ATTRIBUTE_PRINTF(2
Definition: ui-out.c:587
static VEC(char_ptr)
Definition: auto-load.c:171
std::string string_printf(const char *fmt,...)
Definition: common-utils.c:150
int gdb_filename_fnmatch(const char *pattern, const char *string, int flags)
Definition: utils.c:3321
static int filename_is_in_auto_load_safe_path_vec(const char *filename, gdb::unique_xmalloc_ptr< char > *filename_realp)
Definition: auto-load.c:425
EXTERN_C char * re_comp(const char *)
struct program_space * current_program_space
Definition: progspace.c:35
void reset() noexcept
Definition: gdb_optional.h:186
language
Definition: defs.h:203
struct observer * observer_attach_new_objfile(observer_new_objfile_ftype *f)
#define gdb_stdlog
Definition: utils.h:349
void add_setshow_optional_filename_cmd(const char *name, enum command_class theclass, char **var, const char *set_doc, const char *show_doc, const char *help_doc, cmd_const_sfunc_ftype *set_func, show_value_ftype *show_func, struct cmd_list_element **set_list, struct cmd_list_element **show_list)
Definition: cli-decode.c:654
void do_set_command(const char *arg, int from_tty, struct cmd_list_element *c)
Definition: cli-setshow.c:150
void auto_load_ext_lang_scripts_for_objfile(struct objfile *objfile)
Definition: extension.c:383
struct cmd_list_element ** prefixlist
Definition: cli-decode.h:151
struct cmd_list_element * showdebuglist
Definition: cli-cmds.c:155
int ext_lang_auto_load_enabled(const struct extension_language_defn *extlang)
Definition: extension.c:310
struct htab * loaded_script_texts
Definition: auto-load.c:537
gdb::unique_xmalloc_ptr< char > full_path
Definition: cli-cmds.h:127
objfile_script_executor_func * ext_lang_objfile_script_executor(const struct extension_language_defn *extlang)
Definition: extension.c:298
static void set_auto_load_cmd(const char *args, int from_tty)
Definition: auto-load.c:1444
struct cleanup * make_cleanup_free_char_ptr_vec(VEC(char_ptr) *char_ptr_vec)
Definition: utils.c:3192
static void clear_section_scripts(void)
Definition: auto-load.c:749
void dont_repeat(void)
Definition: top.c:695
static int auto_load_gdb_scripts
Definition: auto-load.c:81
static void auto_load_section_scripts(struct objfile *objfile, const char *section_name)
Definition: auto-load.c:1147
objfile_script_sourcer_func * ext_lang_objfile_script_sourcer(const struct extension_language_defn *extlang)
Definition: extension.c:280
struct observer * observer_attach_gdb_datadir_changed(observer_gdb_datadir_changed_ftype *f)
struct cmd_list_element ** auto_load_show_cmdlist_get(void)
Definition: auto-load.c:1501
static void auto_load_pspace_data_cleanup(struct program_space *pspace, void *arg)
Definition: auto-load.c:569
int is_target_filename(const char *name)
Definition: gdb_bfd.c:208
static hashval_t hash_loaded_script_entry(const void *data)
Definition: auto-load.c:602
void auto_load_info_scripts(const char *pattern, int from_tty, const struct extension_language_defn *language)
Definition: auto-load.c:1294
static void print_script(struct loaded_script *script)
Definition: auto-load.c:1246
void error(const char *fmt,...)
Definition: errors.c:38
gdb::optional< open_script > find_and_open_script(const char *script_file, int search_path)
Definition: cli-cmds.c:496
void do_cleanups(struct cleanup *old_chain)
Definition: cleanups.c:174
static void show_auto_load_gdb_scripts(struct ui_file *file, int from_tty, struct cmd_list_element *c, const char *value)
Definition: auto-load.c:86
struct cmd_list_element ** auto_load_info_cmdlist_get(void)
Definition: auto-load.c:1544
static void maybe_print_script_not_found_warning(struct auto_load_pspace_info *, struct objfile *objfile, const struct extension_language_defn *language, const char *section_name, unsigned offset)
Definition: auto-load.c:1425