Testsuite: Fix ambiguous "break" due to libinproctrace
[deliverable/binutils-gdb.git] / gdb / auto-load.c
... / ...
CommitLineData
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
53static 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
58static 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. */
64static int debug_auto_load = 0;
65
66/* "show" command for the debug_auto_load configuration variable. */
67
68static void
69show_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. */
82static int auto_load_gdb_scripts = 1;
83
84/* "show" command for the auto_load_gdb_scripts configuration variable. */
85
86static void
87show_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
97int
98auto_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. */
113int global_auto_load = 1;
114
115/* Auto-load .gdbinit file from the current directory? */
116int auto_load_local_gdbinit = 1;
117
118/* Absolute pathname to the current directory .gdbinit, if it exists. */
119char *auto_load_local_gdbinit_pathname = NULL;
120
121/* Boolean value if AUTO_LOAD_LOCAL_GDBINIT_PATHNAME has been loaded. */
122int auto_load_local_gdbinit_loaded = 0;
123
124/* "show" command for the auto_load_local_gdbinit configuration variable. */
125
126static void
127show_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. */
138static char *auto_load_dir;
139
140/* "set" command for the auto_load_dir configuration variable. */
141
142static void
143set_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
155static void
156show_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. */
167static 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. */
172std::vector<gdb::unique_xmalloc_ptr<char>> auto_load_safe_path_vec;
173
174/* Expand $datadir and $debugdir in STRING according to the rules of
175 substitute_path_component. */
176
177static std::vector<gdb::unique_xmalloc_ptr<char>>
178auto_load_expand_dir_vars (const char *string)
179{
180 char *s = xstrdup (string);
181 substitute_path_component (&s, "$datadir", gdb_datadir);
182 substitute_path_component (&s, "$debugdir", debug_file_directory);
183
184 if (debug_auto_load && strcmp (s, string) != 0)
185 fprintf_unfiltered (gdb_stdlog,
186 _("auto-load: Expanded $-variables to \"%s\".\n"), s);
187
188 std::vector<gdb::unique_xmalloc_ptr<char>> dir_vec
189 = dirnames_to_char_ptr_vec (s);
190 xfree(s);
191
192 return dir_vec;
193}
194
195/* Update auto_load_safe_path_vec from current AUTO_LOAD_SAFE_PATH. */
196
197static void
198auto_load_safe_path_vec_update (void)
199{
200 unsigned len;
201 int ix;
202
203 if (debug_auto_load)
204 fprintf_unfiltered (gdb_stdlog,
205 _("auto-load: Updating directories of \"%s\".\n"),
206 auto_load_safe_path);
207
208 auto_load_safe_path_vec = auto_load_expand_dir_vars (auto_load_safe_path);
209
210 /* Apply tilde_expand and gdb_realpath to each AUTO_LOAD_SAFE_PATH_VEC
211 element. */
212 for (gdb::unique_xmalloc_ptr<char> &in_vec : auto_load_safe_path_vec)
213 {
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
250static void
251auto_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
258static void
259set_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
274static void
275show_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
298static void
299add_auto_load_safe_path (const char *args, int from_tty)
300{
301 char *s;
302
303 if (args == NULL || *args == 0)
304 error (_("\
305Directory argument required.\n\
306Use '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
319static void
320add_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
335static int
336filename_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
400static ATTRIBUTE_PURE int
401filename_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
418static int
419filename_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
472int
473file_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 \"%s\" auto-loading has been declined by your "
495 "`auto-load safe-path' set to \"%s\"."),
496 filename_real.get (), auto_load_safe_path);
497
498 if (!advice_printed)
499 {
500 const char *homedir = getenv ("HOME");
501
502 if (homedir == NULL)
503 homedir = "$HOME";
504 std::string homeinit = string_printf ("%s/%s", homedir, gdbinit);
505
506 printf_filtered (_("\
507To enable execution of this file add\n\
508\tadd-auto-load-safe-path %s\n\
509line to your configuration file \"%s\".\n\
510To completely disable this security protection add\n\
511\tset auto-load safe-path /\n\
512line to your configuration file \"%s\".\n\
513For more information about this security protection see the\n\
514\"Auto-loading safe path\" section in the GDB manual. E.g., run from the shell:\n\
515\tinfo \"(gdb)Auto-loading safe path\"\n"),
516 filename_real.get (),
517 homeinit.c_str (), homeinit.c_str ());
518 advice_printed = 1;
519 }
520
521 return 0;
522}
523
524/* For scripts specified in .debug_gdb_scripts, multiple objfiles may load
525 the same script. There's no point in loading the script multiple times,
526 and there can be a lot of objfiles and scripts, so we keep track of scripts
527 loaded this way. */
528
529struct auto_load_pspace_info
530{
531 /* For each program space we keep track of loaded scripts, both when
532 specified as file names and as scripts to be executed directly. */
533 struct htab *loaded_script_files;
534 struct htab *loaded_script_texts;
535
536 /* Non-zero if we've issued the warning about an auto-load script not being
537 supported. We only want to issue this warning once. */
538 int unsupported_script_warning_printed;
539
540 /* Non-zero if we've issued the warning about an auto-load script not being
541 found. We only want to issue this warning once. */
542 int script_not_found_warning_printed;
543};
544
545/* Objects of this type are stored in the loaded_script hash table. */
546
547struct loaded_script
548{
549 /* Name as provided by the objfile. */
550 const char *name;
551
552 /* Full path name or NULL if script wasn't found (or was otherwise
553 inaccessible), or NULL for loaded_script_texts. */
554 const char *full_path;
555
556 /* Non-zero if this script has been loaded. */
557 int loaded;
558
559 const struct extension_language_defn *language;
560};
561
562/* Per-program-space data key. */
563static const struct program_space_data *auto_load_pspace_data;
564
565static void
566auto_load_pspace_data_cleanup (struct program_space *pspace, void *arg)
567{
568 struct auto_load_pspace_info *info = (struct auto_load_pspace_info *) arg;
569
570 if (info->loaded_script_files)
571 htab_delete (info->loaded_script_files);
572 if (info->loaded_script_texts)
573 htab_delete (info->loaded_script_texts);
574 xfree (info);
575}
576
577/* Get the current autoload data. If none is found yet, add it now. This
578 function always returns a valid object. */
579
580static struct auto_load_pspace_info *
581get_auto_load_pspace_data (struct program_space *pspace)
582{
583 struct auto_load_pspace_info *info;
584
585 info = ((struct auto_load_pspace_info *)
586 program_space_data (pspace, auto_load_pspace_data));
587 if (info == NULL)
588 {
589 info = XCNEW (struct auto_load_pspace_info);
590 set_program_space_data (pspace, auto_load_pspace_data, info);
591 }
592
593 return info;
594}
595
596/* Hash function for the loaded script hash. */
597
598static hashval_t
599hash_loaded_script_entry (const void *data)
600{
601 const struct loaded_script *e = (const struct loaded_script *) data;
602
603 return htab_hash_string (e->name) ^ htab_hash_pointer (e->language);
604}
605
606/* Equality function for the loaded script hash. */
607
608static int
609eq_loaded_script_entry (const void *a, const void *b)
610{
611 const struct loaded_script *ea = (const struct loaded_script *) a;
612 const struct loaded_script *eb = (const struct loaded_script *) b;
613
614 return strcmp (ea->name, eb->name) == 0 && ea->language == eb->language;
615}
616
617/* Initialize the table to track loaded scripts.
618 Each entry is hashed by the full path name. */
619
620static void
621init_loaded_scripts_info (struct auto_load_pspace_info *pspace_info)
622{
623 /* Choose 31 as the starting size of the hash table, somewhat arbitrarily.
624 Space for each entry is obtained with one malloc so we can free them
625 easily. */
626
627 pspace_info->loaded_script_files = htab_create (31,
628 hash_loaded_script_entry,
629 eq_loaded_script_entry,
630 xfree);
631 pspace_info->loaded_script_texts = htab_create (31,
632 hash_loaded_script_entry,
633 eq_loaded_script_entry,
634 xfree);
635
636 pspace_info->unsupported_script_warning_printed = FALSE;
637 pspace_info->script_not_found_warning_printed = FALSE;
638}
639
640/* Wrapper on get_auto_load_pspace_data to also allocate the hash table
641 for loading scripts. */
642
643struct auto_load_pspace_info *
644get_auto_load_pspace_data_for_loading (struct program_space *pspace)
645{
646 struct auto_load_pspace_info *info;
647
648 info = get_auto_load_pspace_data (pspace);
649 if (info->loaded_script_files == NULL)
650 init_loaded_scripts_info (info);
651
652 return info;
653}
654
655/* Add script file NAME in LANGUAGE to hash table of PSPACE_INFO.
656 LOADED 1 if the script has been (is going to) be loaded, 0 otherwise
657 (such as if it has not been found).
658 FULL_PATH is NULL if the script wasn't found.
659 The result is true if the script was already in the hash table. */
660
661static int
662maybe_add_script_file (struct auto_load_pspace_info *pspace_info, int loaded,
663 const char *name, const char *full_path,
664 const struct extension_language_defn *language)
665{
666 struct htab *htab = pspace_info->loaded_script_files;
667 struct loaded_script **slot, entry;
668 int in_hash_table;
669
670 entry.name = name;
671 entry.language = language;
672 slot = (struct loaded_script **) htab_find_slot (htab, &entry, INSERT);
673 in_hash_table = *slot != NULL;
674
675 /* If this script is not in the hash table, add it. */
676
677 if (!in_hash_table)
678 {
679 char *p;
680
681 /* Allocate all space in one chunk so it's easier to free. */
682 *slot = ((struct loaded_script *)
683 xmalloc (sizeof (**slot)
684 + strlen (name) + 1
685 + (full_path != NULL ? (strlen (full_path) + 1) : 0)));
686 p = ((char*) *slot) + sizeof (**slot);
687 strcpy (p, name);
688 (*slot)->name = p;
689 if (full_path != NULL)
690 {
691 p += strlen (p) + 1;
692 strcpy (p, full_path);
693 (*slot)->full_path = p;
694 }
695 else
696 (*slot)->full_path = NULL;
697 (*slot)->loaded = loaded;
698 (*slot)->language = language;
699 }
700
701 return in_hash_table;
702}
703
704/* Add script contents NAME in LANGUAGE to hash table of PSPACE_INFO.
705 LOADED 1 if the script has been (is going to) be loaded, 0 otherwise
706 (such as if it has not been found).
707 The result is true if the script was already in the hash table. */
708
709static int
710maybe_add_script_text (struct auto_load_pspace_info *pspace_info,
711 int loaded, const char *name,
712 const struct extension_language_defn *language)
713{
714 struct htab *htab = pspace_info->loaded_script_texts;
715 struct loaded_script **slot, entry;
716 int in_hash_table;
717
718 entry.name = name;
719 entry.language = language;
720 slot = (struct loaded_script **) htab_find_slot (htab, &entry, INSERT);
721 in_hash_table = *slot != NULL;
722
723 /* If this script is not in the hash table, add it. */
724
725 if (!in_hash_table)
726 {
727 char *p;
728
729 /* Allocate all space in one chunk so it's easier to free. */
730 *slot = ((struct loaded_script *)
731 xmalloc (sizeof (**slot) + strlen (name) + 1));
732 p = ((char*) *slot) + sizeof (**slot);
733 strcpy (p, name);
734 (*slot)->name = p;
735 (*slot)->full_path = NULL;
736 (*slot)->loaded = loaded;
737 (*slot)->language = language;
738 }
739
740 return in_hash_table;
741}
742
743/* Clear the table of loaded section scripts. */
744
745static void
746clear_section_scripts (void)
747{
748 struct program_space *pspace = current_program_space;
749 struct auto_load_pspace_info *info;
750
751 info = ((struct auto_load_pspace_info *)
752 program_space_data (pspace, auto_load_pspace_data));
753 if (info != NULL && info->loaded_script_files != NULL)
754 {
755 htab_delete (info->loaded_script_files);
756 htab_delete (info->loaded_script_texts);
757 info->loaded_script_files = NULL;
758 info->loaded_script_texts = NULL;
759 info->unsupported_script_warning_printed = FALSE;
760 info->script_not_found_warning_printed = FALSE;
761 }
762}
763
764/* Look for the auto-load script in LANGUAGE associated with OBJFILE where
765 OBJFILE's gdb_realpath is REALNAME and load it. Return 1 if we found any
766 matching script, return 0 otherwise. */
767
768static int
769auto_load_objfile_script_1 (struct objfile *objfile, const char *realname,
770 const struct extension_language_defn *language)
771{
772 const char *debugfile;
773 int retval;
774 const char *suffix = ext_lang_auto_load_suffix (language);
775
776 std::string filename = std::string (realname) + suffix;
777
778 gdb_file_up input = gdb_fopen_cloexec (filename.c_str (), "r");
779 debugfile = filename.c_str ();
780 if (debug_auto_load)
781 fprintf_unfiltered (gdb_stdlog, _("auto-load: Attempted file \"%s\" %s.\n"),
782 debugfile, input ? _("exists") : _("does not exist"));
783
784 std::string debugfile_holder;
785 if (!input)
786 {
787 /* Also try the same file in a subdirectory of gdb's data
788 directory. */
789
790 std::vector<gdb::unique_xmalloc_ptr<char>> vec
791 = auto_load_expand_dir_vars (auto_load_dir);
792
793 if (debug_auto_load)
794 fprintf_unfiltered (gdb_stdlog, _("auto-load: Searching 'set auto-load "
795 "scripts-directory' path \"%s\".\n"),
796 auto_load_dir);
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
862void
863auto_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
894static void
895source_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
967static void
968execute_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 (_("\
1004Missing/bad script name in entry at offset %u in section %s\n\
1005of file %s."),
1006 offset, section_name, objfile_name (objfile));
1007 return;
1008 }
1009 script_text = newline + 1;
1010
1011 /* Skip this script if support is not compiled in. */
1012 executor = ext_lang_objfile_script_executor (language);
1013 if (executor == NULL)
1014 {
1015 /* We don't throw an error, the program is still debuggable. */
1016 maybe_print_unsupported_script_warning (pspace_info, objfile, language,
1017 section_name, offset);
1018 maybe_add_script_text (pspace_info, 0, name, language);
1019 return;
1020 }
1021
1022 /* Skip this script if auto-loading it has been disabled. */
1023 if (!ext_lang_auto_load_enabled (language))
1024 {
1025 /* No message is printed, just skip it. */
1026 return;
1027 }
1028
1029 is_safe = file_is_auto_load_safe (objfile_name (objfile),
1030 _("auto-load: Loading %s script "
1031 "\"%s\" from section \"%s\" of "
1032 "objfile \"%s\".\n"),
1033 ext_lang_name (language), name,
1034 section_name, objfile_name (objfile));
1035
1036 in_hash_table = maybe_add_script_text (pspace_info, is_safe, name, language);
1037
1038 /* If this file is not currently loaded, load it. */
1039 if (is_safe && !in_hash_table)
1040 executor (language, objfile, name, script_text);
1041}
1042
1043/* Load scripts specified in OBJFILE.
1044 START,END delimit a buffer containing a list of nul-terminated
1045 file names.
1046 SECTION_NAME is used in error messages.
1047
1048 Scripts specified as file names are found per normal "source -s" command
1049 processing. First the script is looked for in $cwd. If not found there
1050 the source search path is used.
1051
1052 The section contains a list of path names of script files to load or
1053 actual script contents. Each entry is nul-terminated. */
1054
1055static void
1056source_section_scripts (struct objfile *objfile, const char *section_name,
1057 const char *start, const char *end)
1058{
1059 const char *p;
1060 struct auto_load_pspace_info *pspace_info;
1061
1062 pspace_info = get_auto_load_pspace_data_for_loading (current_program_space);
1063
1064 for (p = start; p < end; ++p)
1065 {
1066 const char *entry;
1067 const struct extension_language_defn *language;
1068 unsigned int offset = p - start;
1069 int code = *p;
1070
1071 switch (code)
1072 {
1073 case SECTION_SCRIPT_ID_PYTHON_FILE:
1074 case SECTION_SCRIPT_ID_PYTHON_TEXT:
1075 language = get_ext_lang_defn (EXT_LANG_PYTHON);
1076 break;
1077 case SECTION_SCRIPT_ID_SCHEME_FILE:
1078 case SECTION_SCRIPT_ID_SCHEME_TEXT:
1079 language = get_ext_lang_defn (EXT_LANG_GUILE);
1080 break;
1081 default:
1082 warning (_("Invalid entry in %s section"), section_name);
1083 /* We could try various heuristics to find the next valid entry,
1084 but it's safer to just punt. */
1085 return;
1086 }
1087 entry = ++p;
1088
1089 while (p < end && *p != '\0')
1090 ++p;
1091 if (p == end)
1092 {
1093 warning (_("Non-nul-terminated entry in %s at offset %u"),
1094 section_name, offset);
1095 /* Don't load/execute it. */
1096 break;
1097 }
1098
1099 switch (code)
1100 {
1101 case SECTION_SCRIPT_ID_PYTHON_FILE:
1102 case SECTION_SCRIPT_ID_SCHEME_FILE:
1103 if (p == entry)
1104 {
1105 warning (_("Empty entry in %s at offset %u"),
1106 section_name, offset);
1107 continue;
1108 }
1109 source_script_file (pspace_info, objfile, language,
1110 section_name, offset, entry);
1111 break;
1112 case SECTION_SCRIPT_ID_PYTHON_TEXT:
1113 case SECTION_SCRIPT_ID_SCHEME_TEXT:
1114 execute_script_contents (pspace_info, objfile, language,
1115 section_name, offset, entry);
1116 break;
1117 }
1118 }
1119}
1120
1121/* Load scripts specified in section SECTION_NAME of OBJFILE. */
1122
1123static void
1124auto_load_section_scripts (struct objfile *objfile, const char *section_name)
1125{
1126 bfd *abfd = objfile->obfd;
1127 asection *scripts_sect;
1128 bfd_byte *data = NULL;
1129
1130 scripts_sect = bfd_get_section_by_name (abfd, section_name);
1131 if (scripts_sect == NULL
1132 || (bfd_get_section_flags (abfd, scripts_sect) & SEC_HAS_CONTENTS) == 0)
1133 return;
1134
1135 if (!bfd_get_full_section_contents (abfd, scripts_sect, &data))
1136 warning (_("Couldn't read %s section of %s"),
1137 section_name, bfd_get_filename (abfd));
1138 else
1139 {
1140 gdb::unique_xmalloc_ptr<bfd_byte> data_holder (data);
1141
1142 char *p = (char *) data;
1143 source_section_scripts (objfile, section_name, p,
1144 p + bfd_get_section_size (scripts_sect));
1145 }
1146}
1147
1148/* Load any auto-loaded scripts for OBJFILE. */
1149
1150void
1151load_auto_scripts_for_objfile (struct objfile *objfile)
1152{
1153 /* Return immediately if auto-loading has been globally disabled.
1154 This is to handle sequencing of operations during gdb startup.
1155 Also return immediately if OBJFILE was not created from a file
1156 on the local filesystem. */
1157 if (!global_auto_load
1158 || (objfile->flags & OBJF_NOT_FILENAME) != 0
1159 || is_target_filename (objfile->original_name))
1160 return;
1161
1162 /* Load any extension language scripts for this objfile.
1163 E.g., foo-gdb.gdb, foo-gdb.py. */
1164 auto_load_ext_lang_scripts_for_objfile (objfile);
1165
1166 /* Load any scripts mentioned in AUTO_SECTION_NAME (.debug_gdb_scripts). */
1167 auto_load_section_scripts (objfile, AUTO_SECTION_NAME);
1168}
1169
1170/* This is a new_objfile observer callback to auto-load scripts.
1171
1172 Two flavors of auto-loaded scripts are supported.
1173 1) based on the path to the objfile
1174 2) from .debug_gdb_scripts section */
1175
1176static void
1177auto_load_new_objfile (struct objfile *objfile)
1178{
1179 if (!objfile)
1180 {
1181 /* OBJFILE is NULL when loading a new "main" symbol-file. */
1182 clear_section_scripts ();
1183 return;
1184 }
1185
1186 load_auto_scripts_for_objfile (objfile);
1187}
1188
1189/* Collect scripts to be printed in a vec. */
1190
1191struct collect_matching_scripts_data
1192{
1193 collect_matching_scripts_data (std::vector<loaded_script *> *scripts_p_,
1194 const extension_language_defn *language_)
1195 : scripts_p (scripts_p_), language (language_)
1196 {}
1197
1198 std::vector<loaded_script *> *scripts_p;
1199 const struct extension_language_defn *language;
1200};
1201
1202/* Traversal function for htab_traverse.
1203 Collect the entry if it matches the regexp. */
1204
1205static int
1206collect_matching_scripts (void **slot, void *info)
1207{
1208 struct loaded_script *script = (struct loaded_script *) *slot;
1209 struct collect_matching_scripts_data *data
1210 = (struct collect_matching_scripts_data *) info;
1211
1212 if (script->language == data->language && re_exec (script->name))
1213 data->scripts_p->push_back (script);
1214
1215 return 1;
1216}
1217
1218/* Print SCRIPT. */
1219
1220static void
1221print_script (struct loaded_script *script)
1222{
1223 struct ui_out *uiout = current_uiout;
1224
1225 ui_out_emit_tuple tuple_emitter (uiout, NULL);
1226
1227 uiout->field_string ("loaded", script->loaded ? "Yes" : "No");
1228 uiout->field_string ("script", script->name);
1229 uiout->text ("\n");
1230
1231 /* If the name isn't the full path, print it too. */
1232 if (script->full_path != NULL
1233 && strcmp (script->name, script->full_path) != 0)
1234 {
1235 uiout->text ("\tfull name: ");
1236 uiout->field_string ("full_path", script->full_path);
1237 uiout->text ("\n");
1238 }
1239}
1240
1241/* Helper for info_auto_load_scripts to sort the scripts by name. */
1242
1243static bool
1244sort_scripts_by_name (loaded_script *a, loaded_script *b)
1245{
1246 return FILENAME_CMP (a->name, b->name) < 0;
1247}
1248
1249/* Special internal GDB value of auto_load_info_scripts's PATTERN identify
1250 the "info auto-load XXX" command has been executed through the general
1251 "info auto-load" invocation. Extra newline will be printed if needed. */
1252char auto_load_info_scripts_pattern_nl[] = "";
1253
1254/* Subroutine of auto_load_info_scripts to simplify it.
1255 Print SCRIPTS. */
1256
1257static void
1258print_scripts (const std::vector<loaded_script *> &scripts)
1259{
1260 for (loaded_script *script : scripts)
1261 print_script (script);
1262}
1263
1264/* Implementation for "info auto-load gdb-scripts"
1265 (and "info auto-load python-scripts"). List scripts in LANGUAGE matching
1266 PATTERN. FROM_TTY is the usual GDB boolean for user interactivity. */
1267
1268void
1269auto_load_info_scripts (const char *pattern, int from_tty,
1270 const struct extension_language_defn *language)
1271{
1272 struct ui_out *uiout = current_uiout;
1273 struct auto_load_pspace_info *pspace_info;
1274
1275 dont_repeat ();
1276
1277 pspace_info = get_auto_load_pspace_data (current_program_space);
1278
1279 if (pattern && *pattern)
1280 {
1281 char *re_err = re_comp (pattern);
1282
1283 if (re_err)
1284 error (_("Invalid regexp: %s"), re_err);
1285 }
1286 else
1287 {
1288 re_comp ("");
1289 }
1290
1291 /* We need to know the number of rows before we build the table.
1292 Plus we want to sort the scripts by name.
1293 So first traverse the hash table collecting the matching scripts. */
1294
1295 std::vector<loaded_script *> script_files, script_texts;
1296
1297 if (pspace_info != NULL && pspace_info->loaded_script_files != NULL)
1298 {
1299 collect_matching_scripts_data data (&script_files, language);
1300
1301 /* Pass a pointer to scripts as VEC_safe_push can realloc space. */
1302 htab_traverse_noresize (pspace_info->loaded_script_files,
1303 collect_matching_scripts, &data);
1304
1305 std::sort (script_files.begin (), script_files.end (),
1306 sort_scripts_by_name);
1307 }
1308
1309 if (pspace_info != NULL && pspace_info->loaded_script_texts != NULL)
1310 {
1311 collect_matching_scripts_data data (&script_texts, language);
1312
1313 /* Pass a pointer to scripts as VEC_safe_push can realloc space. */
1314 htab_traverse_noresize (pspace_info->loaded_script_texts,
1315 collect_matching_scripts, &data);
1316
1317 std::sort (script_texts.begin (), script_texts.end (),
1318 sort_scripts_by_name);
1319 }
1320
1321 int nr_scripts = script_files.size () + script_texts.size ();
1322
1323 /* Table header shifted right by preceding "gdb-scripts: " would not match
1324 its columns. */
1325 if (nr_scripts > 0 && pattern == auto_load_info_scripts_pattern_nl)
1326 uiout->text ("\n");
1327
1328 {
1329 ui_out_emit_table table_emitter (uiout, 2, nr_scripts,
1330 "AutoLoadedScriptsTable");
1331
1332 uiout->table_header (7, ui_left, "loaded", "Loaded");
1333 uiout->table_header (70, ui_left, "script", "Script");
1334 uiout->table_body ();
1335
1336 print_scripts (script_files);
1337 print_scripts (script_texts);
1338 }
1339
1340 if (nr_scripts == 0)
1341 {
1342 if (pattern && *pattern)
1343 uiout->message ("No auto-load scripts matching %s.\n", pattern);
1344 else
1345 uiout->message ("No auto-load scripts.\n");
1346 }
1347}
1348
1349/* Wrapper for "info auto-load gdb-scripts". */
1350
1351static void
1352info_auto_load_gdb_scripts (const char *pattern, int from_tty)
1353{
1354 auto_load_info_scripts (pattern, from_tty, &extension_language_gdb);
1355}
1356
1357/* Implement 'info auto-load local-gdbinit'. */
1358
1359static void
1360info_auto_load_local_gdbinit (const char *args, int from_tty)
1361{
1362 if (auto_load_local_gdbinit_pathname == NULL)
1363 printf_filtered (_("Local .gdbinit file was not found.\n"));
1364 else if (auto_load_local_gdbinit_loaded)
1365 printf_filtered (_("Local .gdbinit file \"%s\" has been loaded.\n"),
1366 auto_load_local_gdbinit_pathname);
1367 else
1368 printf_filtered (_("Local .gdbinit file \"%s\" has not been loaded.\n"),
1369 auto_load_local_gdbinit_pathname);
1370}
1371
1372/* Print an "unsupported script" warning if it has not already been printed.
1373 The script is in language LANGUAGE at offset OFFSET in section SECTION_NAME
1374 of OBJFILE. */
1375
1376static void
1377maybe_print_unsupported_script_warning
1378 (struct auto_load_pspace_info *pspace_info,
1379 struct objfile *objfile, const struct extension_language_defn *language,
1380 const char *section_name, unsigned offset)
1381{
1382 if (!pspace_info->unsupported_script_warning_printed)
1383 {
1384 warning (_("\
1385Unsupported auto-load script at offset %u in section %s\n\
1386of file %s.\n\
1387Use `info auto-load %s-scripts [REGEXP]' to list them."),
1388 offset, section_name, objfile_name (objfile),
1389 ext_lang_name (language));
1390 pspace_info->unsupported_script_warning_printed = 1;
1391 }
1392}
1393
1394/* Return non-zero if SCRIPT_NOT_FOUND_WARNING_PRINTED of PSPACE_INFO was unset
1395 before calling this function. Always set SCRIPT_NOT_FOUND_WARNING_PRINTED
1396 of PSPACE_INFO. */
1397
1398static void
1399maybe_print_script_not_found_warning
1400 (struct auto_load_pspace_info *pspace_info,
1401 struct objfile *objfile, const struct extension_language_defn *language,
1402 const char *section_name, unsigned offset)
1403{
1404 if (!pspace_info->script_not_found_warning_printed)
1405 {
1406 warning (_("\
1407Missing auto-load script at offset %u in section %s\n\
1408of file %s.\n\
1409Use `info auto-load %s-scripts [REGEXP]' to list them."),
1410 offset, section_name, objfile_name (objfile),
1411 ext_lang_name (language));
1412 pspace_info->script_not_found_warning_printed = 1;
1413 }
1414}
1415
1416/* The only valid "set auto-load" argument is off|0|no|disable. */
1417
1418static void
1419set_auto_load_cmd (const char *args, int from_tty)
1420{
1421 struct cmd_list_element *list;
1422 size_t length;
1423
1424 /* See parse_binary_operation in use by the sub-commands. */
1425
1426 length = args ? strlen (args) : 0;
1427
1428 while (length > 0 && (args[length - 1] == ' ' || args[length - 1] == '\t'))
1429 length--;
1430
1431 if (length == 0 || (strncmp (args, "off", length) != 0
1432 && strncmp (args, "0", length) != 0
1433 && strncmp (args, "no", length) != 0
1434 && strncmp (args, "disable", length) != 0))
1435 error (_("Valid is only global 'set auto-load no'; "
1436 "otherwise check the auto-load sub-commands."));
1437
1438 for (list = *auto_load_set_cmdlist_get (); list != NULL; list = list->next)
1439 if (list->var_type == var_boolean)
1440 {
1441 gdb_assert (list->type == set_cmd);
1442 do_set_command (args, from_tty, list);
1443 }
1444}
1445
1446/* Initialize "set auto-load " commands prefix and return it. */
1447
1448struct cmd_list_element **
1449auto_load_set_cmdlist_get (void)
1450{
1451 static struct cmd_list_element *retval;
1452
1453 if (retval == NULL)
1454 add_prefix_cmd ("auto-load", class_maintenance, set_auto_load_cmd, _("\
1455Auto-loading specific settings.\n\
1456Configure various auto-load-specific variables such as\n\
1457automatic loading of Python scripts."),
1458 &retval, "set auto-load ",
1459 1/*allow-unknown*/, &setlist);
1460
1461 return &retval;
1462}
1463
1464/* Command "show auto-load" displays summary of all the current
1465 "show auto-load " settings. */
1466
1467static void
1468show_auto_load_cmd (const char *args, int from_tty)
1469{
1470 cmd_show_list (*auto_load_show_cmdlist_get (), from_tty, "");
1471}
1472
1473/* Initialize "show auto-load " commands prefix and return it. */
1474
1475struct cmd_list_element **
1476auto_load_show_cmdlist_get (void)
1477{
1478 static struct cmd_list_element *retval;
1479
1480 if (retval == NULL)
1481 add_prefix_cmd ("auto-load", class_maintenance, show_auto_load_cmd, _("\
1482Show auto-loading specific settings.\n\
1483Show configuration of various auto-load-specific variables such as\n\
1484automatic loading of Python scripts."),
1485 &retval, "show auto-load ",
1486 0/*allow-unknown*/, &showlist);
1487
1488 return &retval;
1489}
1490
1491/* Command "info auto-load" displays whether the various auto-load files have
1492 been loaded. This is reimplementation of cmd_show_list which inserts
1493 newlines at proper places. */
1494
1495static void
1496info_auto_load_cmd (const char *args, int from_tty)
1497{
1498 struct cmd_list_element *list;
1499 struct ui_out *uiout = current_uiout;
1500
1501 ui_out_emit_tuple tuple_emitter (uiout, "infolist");
1502
1503 for (list = *auto_load_info_cmdlist_get (); list != NULL; list = list->next)
1504 {
1505 ui_out_emit_tuple option_emitter (uiout, "option");
1506
1507 gdb_assert (!list->prefixlist);
1508 gdb_assert (list->type == not_set_cmd);
1509
1510 uiout->field_string ("name", list->name);
1511 uiout->text (": ");
1512 cmd_func (list, auto_load_info_scripts_pattern_nl, from_tty);
1513 }
1514}
1515
1516/* Initialize "info auto-load " commands prefix and return it. */
1517
1518struct cmd_list_element **
1519auto_load_info_cmdlist_get (void)
1520{
1521 static struct cmd_list_element *retval;
1522
1523 if (retval == NULL)
1524 add_prefix_cmd ("auto-load", class_info, info_auto_load_cmd, _("\
1525Print current status of auto-loaded files.\n\
1526Print whether various files like Python scripts or .gdbinit files have been\n\
1527found and/or loaded."),
1528 &retval, "info auto-load ",
1529 0/*allow-unknown*/, &infolist);
1530
1531 return &retval;
1532}
1533
1534void
1535_initialize_auto_load (void)
1536{
1537 struct cmd_list_element *cmd;
1538 char *scripts_directory_help, *gdb_name_help, *python_name_help;
1539 char *guile_name_help;
1540 const char *suffix;
1541
1542 auto_load_pspace_data
1543 = register_program_space_data_with_cleanup (NULL,
1544 auto_load_pspace_data_cleanup);
1545
1546 observer_attach_new_objfile (auto_load_new_objfile);
1547
1548 add_setshow_boolean_cmd ("gdb-scripts", class_support,
1549 &auto_load_gdb_scripts, _("\
1550Enable or disable auto-loading of canned sequences of commands scripts."), _("\
1551Show whether auto-loading of canned sequences of commands scripts is enabled."),
1552 _("\
1553If enabled, canned sequences of commands are loaded when the debugger reads\n\
1554an executable or shared library.\n\
1555This options has security implications for untrusted inferiors."),
1556 NULL, show_auto_load_gdb_scripts,
1557 auto_load_set_cmdlist_get (),
1558 auto_load_show_cmdlist_get ());
1559
1560 add_cmd ("gdb-scripts", class_info, info_auto_load_gdb_scripts,
1561 _("Print the list of automatically loaded sequences of commands.\n\
1562Usage: info auto-load gdb-scripts [REGEXP]"),
1563 auto_load_info_cmdlist_get ());
1564
1565 add_setshow_boolean_cmd ("local-gdbinit", class_support,
1566 &auto_load_local_gdbinit, _("\
1567Enable or disable auto-loading of .gdbinit script in current directory."), _("\
1568Show whether auto-loading .gdbinit script in current directory is enabled."),
1569 _("\
1570If enabled, canned sequences of commands are loaded when debugger starts\n\
1571from .gdbinit file in current directory. Such files are deprecated,\n\
1572use a script associated with inferior executable file instead.\n\
1573This options has security implications for untrusted inferiors."),
1574 NULL, show_auto_load_local_gdbinit,
1575 auto_load_set_cmdlist_get (),
1576 auto_load_show_cmdlist_get ());
1577
1578 add_cmd ("local-gdbinit", class_info, info_auto_load_local_gdbinit,
1579 _("Print whether current directory .gdbinit file has been loaded.\n\
1580Usage: info auto-load local-gdbinit"),
1581 auto_load_info_cmdlist_get ());
1582
1583 auto_load_dir = xstrdup (AUTO_LOAD_DIR);
1584
1585 suffix = ext_lang_auto_load_suffix (get_ext_lang_defn (EXT_LANG_GDB));
1586 gdb_name_help
1587 = xstrprintf (_("\
1588GDB scripts: OBJFILE%s\n"),
1589 suffix);
1590 python_name_help = NULL;
1591#ifdef HAVE_PYTHON
1592 suffix = ext_lang_auto_load_suffix (get_ext_lang_defn (EXT_LANG_PYTHON));
1593 python_name_help
1594 = xstrprintf (_("\
1595Python scripts: OBJFILE%s\n"),
1596 suffix);
1597#endif
1598 guile_name_help = NULL;
1599#ifdef HAVE_GUILE
1600 suffix = ext_lang_auto_load_suffix (get_ext_lang_defn (EXT_LANG_GUILE));
1601 guile_name_help
1602 = xstrprintf (_("\
1603Guile scripts: OBJFILE%s\n"),
1604 suffix);
1605#endif
1606 scripts_directory_help
1607 = xstrprintf (_("\
1608Automatically loaded scripts are located in one of the directories listed\n\
1609by this option.\n\
1610\n\
1611Script names:\n\
1612%s%s%s\
1613\n\
1614This option is ignored for the kinds of scripts \
1615having 'set auto-load ... off'.\n\
1616Directories listed here need to be present also \
1617in the 'set auto-load safe-path'\n\
1618option."),
1619 gdb_name_help,
1620 python_name_help ? python_name_help : "",
1621 guile_name_help ? guile_name_help : "");
1622
1623 add_setshow_optional_filename_cmd ("scripts-directory", class_support,
1624 &auto_load_dir, _("\
1625Set the list of directories from which to load auto-loaded scripts."), _("\
1626Show the list of directories from which to load auto-loaded scripts."),
1627 scripts_directory_help,
1628 set_auto_load_dir, show_auto_load_dir,
1629 auto_load_set_cmdlist_get (),
1630 auto_load_show_cmdlist_get ());
1631 xfree (scripts_directory_help);
1632 xfree (python_name_help);
1633 xfree (gdb_name_help);
1634 xfree (guile_name_help);
1635
1636 auto_load_safe_path = xstrdup (AUTO_LOAD_SAFE_PATH);
1637 auto_load_safe_path_vec_update ();
1638 add_setshow_optional_filename_cmd ("safe-path", class_support,
1639 &auto_load_safe_path, _("\
1640Set the list of files and directories that are safe for auto-loading."), _("\
1641Show the list of files and directories that are safe for auto-loading."), _("\
1642Various files loaded automatically for the 'set auto-load ...' options must\n\
1643be located in one of the directories listed by this option. Warning will be\n\
1644printed and file will not be used otherwise.\n\
1645You can mix both directory and filename entries.\n\
1646Setting this parameter to an empty list resets it to its default value.\n\
1647Setting this parameter to '/' (without the quotes) allows any file\n\
1648for the 'set auto-load ...' options. Each path entry can be also shell\n\
1649wildcard pattern; '*' does not match directory separator.\n\
1650This option is ignored for the kinds of files having 'set auto-load ... off'.\n\
1651This options has security implications for untrusted inferiors."),
1652 set_auto_load_safe_path,
1653 show_auto_load_safe_path,
1654 auto_load_set_cmdlist_get (),
1655 auto_load_show_cmdlist_get ());
1656 observer_attach_gdb_datadir_changed (auto_load_gdb_datadir_changed);
1657
1658 cmd = add_cmd ("add-auto-load-safe-path", class_support,
1659 add_auto_load_safe_path,
1660 _("Add entries to the list of directories from which it is safe "
1661 "to auto-load files.\n\
1662See the commands 'set auto-load safe-path' and 'show auto-load safe-path' to\n\
1663access the current full list setting."),
1664 &cmdlist);
1665 set_cmd_completer (cmd, filename_completer);
1666
1667 cmd = add_cmd ("add-auto-load-scripts-directory", class_support,
1668 add_auto_load_dir,
1669 _("Add entries to the list of directories from which to load "
1670 "auto-loaded scripts.\n\
1671See the commands 'set auto-load scripts-directory' and\n\
1672'show auto-load scripts-directory' to access the current full list setting."),
1673 &cmdlist);
1674 set_cmd_completer (cmd, filename_completer);
1675
1676 add_setshow_boolean_cmd ("auto-load", class_maintenance,
1677 &debug_auto_load, _("\
1678Set auto-load verifications debugging."), _("\
1679Show auto-load verifications debugging."), _("\
1680When non-zero, debugging output for files of 'set auto-load ...'\n\
1681is displayed."),
1682 NULL, show_debug_auto_load,
1683 &setdebuglist, &showdebuglist);
1684}
This page took 0.033015 seconds and 4 git commands to generate.