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