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