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