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