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