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