Support x32 siginfo
[deliverable/binutils-gdb.git] / gdb / auto-load.c
1 /* GDB routines for supporting auto-loaded scripts.
2
3 Copyright (C) 2012 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 "auto-load.h"
22 #include "progspace.h"
23 #include "python/python.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 "exceptions.h"
31 #include "cli/cli-script.h"
32 #include "gdbcmd.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 "observer.h"
39
40 /* The suffix of per-objfile scripts to auto-load as non-Python command files.
41 E.g. When the program loads libfoo.so, look for libfoo-gdb.gdb. */
42 #define GDB_AUTO_FILE_NAME "-gdb.gdb"
43
44 static void source_gdb_script_for_objfile (struct objfile *objfile, FILE *file,
45 const char *filename);
46
47 /* Value of the 'set debug auto-load' configuration variable. */
48 static int debug_auto_load = 0;
49
50 /* "show" command for the debug_auto_load configuration variable. */
51
52 static void
53 show_debug_auto_load (struct ui_file *file, int from_tty,
54 struct cmd_list_element *c, const char *value)
55 {
56 fprintf_filtered (file, _("Debugging output for files "
57 "of 'set auto-load ...' is %s.\n"),
58 value);
59 }
60
61 /* User-settable option to enable/disable auto-loading of GDB_AUTO_FILE_NAME
62 scripts:
63 set auto-load gdb-scripts on|off
64 This is true if we should auto-load associated scripts when an objfile
65 is opened, false otherwise. */
66 static int auto_load_gdb_scripts = 1;
67
68 /* "show" command for the auto_load_gdb_scripts configuration variable. */
69
70 static void
71 show_auto_load_gdb_scripts (struct ui_file *file, int from_tty,
72 struct cmd_list_element *c, const char *value)
73 {
74 fprintf_filtered (file, _("Auto-loading of canned sequences of commands "
75 "scripts is %s.\n"),
76 value);
77 }
78
79 /* Internal-use flag to enable/disable auto-loading.
80 This is true if we should auto-load python code when an objfile is opened,
81 false otherwise.
82
83 Both auto_load_scripts && global_auto_load must be true to enable
84 auto-loading.
85
86 This flag exists to facilitate deferring auto-loading during start-up
87 until after ./.gdbinit has been read; it may augment the search directories
88 used to find the scripts. */
89 int global_auto_load = 1;
90
91 /* Auto-load .gdbinit file from the current directory? */
92 int auto_load_local_gdbinit = 1;
93
94 /* Absolute pathname to the current directory .gdbinit, if it exists. */
95 char *auto_load_local_gdbinit_pathname = NULL;
96
97 /* Boolean value if AUTO_LOAD_LOCAL_GDBINIT_PATHNAME has been loaded. */
98 int auto_load_local_gdbinit_loaded = 0;
99
100 /* "show" command for the auto_load_local_gdbinit configuration variable. */
101
102 static void
103 show_auto_load_local_gdbinit (struct ui_file *file, int from_tty,
104 struct cmd_list_element *c, const char *value)
105 {
106 fprintf_filtered (file, _("Auto-loading of .gdbinit script from current "
107 "directory is %s.\n"),
108 value);
109 }
110
111 /* Directory list from which to load auto-loaded scripts. It is not checked
112 for absolute paths but they are strongly recommended. It is initialized by
113 _initialize_auto_load. */
114 static char *auto_load_dir;
115
116 /* "set" command for the auto_load_dir configuration variable. */
117
118 static void
119 set_auto_load_dir (char *args, int from_tty, struct cmd_list_element *c)
120 {
121 /* Setting the variable to "" resets it to the compile time defaults. */
122 if (auto_load_dir[0] == '\0')
123 {
124 xfree (auto_load_dir);
125 auto_load_dir = xstrdup (AUTO_LOAD_DIR);
126 }
127 }
128
129 /* "show" command for the auto_load_dir configuration variable. */
130
131 static void
132 show_auto_load_dir (struct ui_file *file, int from_tty,
133 struct cmd_list_element *c, const char *value)
134 {
135 fprintf_filtered (file, _("List of directories from which to load "
136 "auto-loaded scripts is %s.\n"),
137 value);
138 }
139
140 /* Directory list safe to hold auto-loaded files. It is not checked for
141 absolute paths but they are strongly recommended. It is initialized by
142 _initialize_auto_load. */
143 static char *auto_load_safe_path;
144
145 /* Vector of directory elements of AUTO_LOAD_SAFE_PATH with each one normalized
146 by tilde_expand and possibly each entries has added its gdb_realpath
147 counterpart. */
148 static VEC (char_ptr) *auto_load_safe_path_vec;
149
150 /* Expand $datadir and $debugdir in STRING according to the rules of
151 substitute_path_component. Return vector from dirnames_to_char_ptr_vec,
152 this vector must be freed by free_char_ptr_vec by the caller. */
153
154 static VEC (char_ptr) *
155 auto_load_expand_dir_vars (const char *string)
156 {
157 VEC (char_ptr) *dir_vec;
158 char *s;
159
160 s = xstrdup (string);
161 substitute_path_component (&s, "$datadir", gdb_datadir);
162 substitute_path_component (&s, "$debugdir", debug_file_directory);
163
164 if (debug_auto_load && strcmp (s, string) != 0)
165 fprintf_unfiltered (gdb_stdlog,
166 _("auto-load: Expanded $-variables to \"%s\".\n"), s);
167
168 dir_vec = dirnames_to_char_ptr_vec (s);
169 xfree(s);
170
171 return dir_vec;
172 }
173
174 /* Update auto_load_safe_path_vec from current AUTO_LOAD_SAFE_PATH. */
175
176 static void
177 auto_load_safe_path_vec_update (void)
178 {
179 VEC (char_ptr) *dir_vec = NULL;
180 unsigned len;
181 int ix;
182
183 if (debug_auto_load)
184 fprintf_unfiltered (gdb_stdlog,
185 _("auto-load: Updating directories of \"%s\".\n"),
186 auto_load_safe_path);
187
188 free_char_ptr_vec (auto_load_safe_path_vec);
189
190 auto_load_safe_path_vec = auto_load_expand_dir_vars (auto_load_safe_path);
191 len = VEC_length (char_ptr, auto_load_safe_path_vec);
192
193 /* Apply tilde_expand and gdb_realpath to each AUTO_LOAD_SAFE_PATH_VEC
194 element. */
195 for (ix = 0; ix < len; ix++)
196 {
197 char *dir = VEC_index (char_ptr, auto_load_safe_path_vec, ix);
198 char *expanded = tilde_expand (dir);
199 char *real_path = gdb_realpath (expanded);
200
201 /* Ensure the current entry is at least tilde_expand-ed. */
202 VEC_replace (char_ptr, auto_load_safe_path_vec, ix, expanded);
203
204 if (debug_auto_load)
205 {
206 if (strcmp (expanded, dir) == 0)
207 fprintf_unfiltered (gdb_stdlog,
208 _("auto-load: Using directory \"%s\".\n"),
209 expanded);
210 else
211 fprintf_unfiltered (gdb_stdlog,
212 _("auto-load: Resolved directory \"%s\" "
213 "as \"%s\".\n"),
214 dir, expanded);
215 }
216 xfree (dir);
217
218 /* If gdb_realpath returns a different content, append it. */
219 if (strcmp (real_path, expanded) == 0)
220 xfree (real_path);
221 else
222 {
223 VEC_safe_push (char_ptr, auto_load_safe_path_vec, real_path);
224
225 if (debug_auto_load)
226 fprintf_unfiltered (gdb_stdlog,
227 _("auto-load: And canonicalized as \"%s\".\n"),
228 real_path);
229 }
230 }
231 }
232
233 /* Variable gdb_datadir has been set. Update content depending on $datadir. */
234
235 static void
236 auto_load_gdb_datadir_changed (void)
237 {
238 auto_load_safe_path_vec_update ();
239 }
240
241 /* "set" command for the auto_load_safe_path configuration variable. */
242
243 static void
244 set_auto_load_safe_path (char *args, int from_tty, struct cmd_list_element *c)
245 {
246 /* Setting the variable to "" resets it to the compile time defaults. */
247 if (auto_load_safe_path[0] == '\0')
248 {
249 xfree (auto_load_safe_path);
250 auto_load_safe_path = xstrdup (AUTO_LOAD_SAFE_PATH);
251 }
252
253 auto_load_safe_path_vec_update ();
254 }
255
256 /* "show" command for the auto_load_safe_path configuration variable. */
257
258 static void
259 show_auto_load_safe_path (struct ui_file *file, int from_tty,
260 struct cmd_list_element *c, const char *value)
261 {
262 const char *cs;
263
264 /* Check if user has entered either "/" or for example ":".
265 But while more complicate content like ":/foo" would still also
266 permit any location do not hide those. */
267
268 for (cs = value; *cs && (*cs == DIRNAME_SEPARATOR || IS_DIR_SEPARATOR (*cs));
269 cs++);
270 if (*cs == 0)
271 fprintf_filtered (file, _("Auto-load files are safe to load from any "
272 "directory.\n"));
273 else
274 fprintf_filtered (file, _("List of directories from which it is safe to "
275 "auto-load files is %s.\n"),
276 value);
277 }
278
279 /* "add-auto-load-safe-path" command for the auto_load_safe_path configuration
280 variable. */
281
282 static void
283 add_auto_load_safe_path (char *args, int from_tty)
284 {
285 char *s;
286
287 if (args == NULL || *args == 0)
288 error (_("\
289 Directory argument required.\n\
290 Use 'set auto-load safe-path /' for disabling the auto-load safe-path security.\
291 "));
292
293 s = xstrprintf ("%s%c%s", auto_load_safe_path, DIRNAME_SEPARATOR, args);
294 xfree (auto_load_safe_path);
295 auto_load_safe_path = s;
296
297 auto_load_safe_path_vec_update ();
298 }
299
300 /* Return 1 if FILENAME is equal to DIR or if FILENAME belongs to the
301 subdirectory DIR. Return 0 otherwise. gdb_realpath normalization is never
302 done here. */
303
304 static ATTRIBUTE_PURE int
305 filename_is_in_dir (const char *filename, const char *dir)
306 {
307 size_t dir_len = strlen (dir);
308
309 while (dir_len && IS_DIR_SEPARATOR (dir[dir_len - 1]))
310 dir_len--;
311
312 /* Ensure auto_load_safe_path "/" matches any FILENAME. On MS-Windows
313 platform FILENAME even after gdb_realpath does not have to start with
314 IS_DIR_SEPARATOR character, such as the 'C:\x.exe' filename. */
315 if (dir_len == 0)
316 return 1;
317
318 return (filename_ncmp (dir, filename, dir_len) == 0
319 && (IS_DIR_SEPARATOR (filename[dir_len])
320 || filename[dir_len] == '\0'));
321 }
322
323 /* Return 1 if FILENAME belongs to one of directory components of
324 AUTO_LOAD_SAFE_PATH_VEC. Return 0 otherwise.
325 auto_load_safe_path_vec_update is never called.
326 *FILENAME_REALP may be updated by gdb_realpath of FILENAME - it has to be
327 freed by the caller. */
328
329 static int
330 filename_is_in_auto_load_safe_path_vec (const char *filename,
331 char **filename_realp)
332 {
333 char *dir;
334 int ix;
335
336 for (ix = 0; VEC_iterate (char_ptr, auto_load_safe_path_vec, ix, dir); ++ix)
337 if (*filename_realp == NULL && filename_is_in_dir (filename, dir))
338 break;
339
340 if (dir == NULL)
341 {
342 if (*filename_realp == NULL)
343 {
344 *filename_realp = gdb_realpath (filename);
345 if (debug_auto_load && strcmp (*filename_realp, filename) != 0)
346 fprintf_unfiltered (gdb_stdlog,
347 _("auto-load: Resolved "
348 "file \"%s\" as \"%s\".\n"),
349 filename, *filename_realp);
350 }
351
352 if (strcmp (*filename_realp, filename) != 0)
353 for (ix = 0; VEC_iterate (char_ptr, auto_load_safe_path_vec, ix, dir);
354 ++ix)
355 if (filename_is_in_dir (*filename_realp, dir))
356 break;
357 }
358
359 if (dir != NULL)
360 {
361 if (debug_auto_load)
362 fprintf_unfiltered (gdb_stdlog, _("auto-load: File \"%s\" matches "
363 "directory \"%s\".\n"),
364 filename, dir);
365 return 1;
366 }
367
368 return 0;
369 }
370
371 /* Return 1 if FILENAME is located in one of the directories of
372 AUTO_LOAD_SAFE_PATH. Otherwise call warning and return 0. FILENAME does
373 not have to be an absolute path.
374
375 Existence of FILENAME is not checked. Function will still give a warning
376 even if the caller would quietly skip non-existing file in unsafe
377 directory. */
378
379 int
380 file_is_auto_load_safe (const char *filename, const char *debug_fmt, ...)
381 {
382 char *filename_real = NULL;
383 struct cleanup *back_to;
384
385 if (debug_auto_load)
386 {
387 va_list debug_args;
388
389 va_start (debug_args, debug_fmt);
390 vfprintf_unfiltered (gdb_stdlog, debug_fmt, debug_args);
391 va_end (debug_args);
392 }
393
394 back_to = make_cleanup (free_current_contents, &filename_real);
395
396 if (filename_is_in_auto_load_safe_path_vec (filename, &filename_real))
397 {
398 do_cleanups (back_to);
399 return 1;
400 }
401
402 auto_load_safe_path_vec_update ();
403 if (filename_is_in_auto_load_safe_path_vec (filename, &filename_real))
404 {
405 do_cleanups (back_to);
406 return 1;
407 }
408
409 warning (_("File \"%s\" auto-loading has been declined by your "
410 "`auto-load safe-path' set to \"%s\"."),
411 filename_real, auto_load_safe_path);
412
413 do_cleanups (back_to);
414 return 0;
415 }
416
417 /* Definition of script language for GDB canned sequences of commands. */
418
419 static const struct script_language script_language_gdb
420 = { GDB_AUTO_FILE_NAME, source_gdb_script_for_objfile };
421
422 static void
423 source_gdb_script_for_objfile (struct objfile *objfile, FILE *file,
424 const char *filename)
425 {
426 int is_safe;
427 struct auto_load_pspace_info *pspace_info;
428 volatile struct gdb_exception e;
429
430 is_safe = file_is_auto_load_safe (filename, _("auto-load: Loading canned "
431 "sequences of commands script "
432 "\"%s\" for objfile \"%s\".\n"),
433 filename, objfile->name);
434
435 /* Add this script to the hash table too so "info auto-load gdb-scripts"
436 can print it. */
437 pspace_info = get_auto_load_pspace_data_for_loading (current_program_space);
438 maybe_add_script (pspace_info, is_safe, filename, filename,
439 &script_language_gdb);
440
441 if (!is_safe)
442 return;
443
444 TRY_CATCH (e, RETURN_MASK_ALL)
445 {
446 script_from_file (file, filename);
447 }
448 exception_print (gdb_stderr, e);
449 }
450
451 /* For scripts specified in .debug_gdb_scripts, multiple objfiles may load
452 the same script. There's no point in loading the script multiple times,
453 and there can be a lot of objfiles and scripts, so we keep track of scripts
454 loaded this way. */
455
456 struct auto_load_pspace_info
457 {
458 /* For each program space we keep track of loaded scripts. */
459 struct htab *loaded_scripts;
460
461 /* Non-zero if we've issued the warning about an auto-load script not being
462 found. We only want to issue this warning once. */
463 int script_not_found_warning_printed;
464 };
465
466 /* Objects of this type are stored in the loaded script hash table. */
467
468 struct loaded_script
469 {
470 /* Name as provided by the objfile. */
471 const char *name;
472
473 /* Full path name or NULL if script wasn't found (or was otherwise
474 inaccessible). */
475 const char *full_path;
476
477 /* Non-zero if this script has been loaded. */
478 int loaded;
479
480 const struct script_language *language;
481 };
482
483 /* Per-program-space data key. */
484 static const struct program_space_data *auto_load_pspace_data;
485
486 static void
487 auto_load_pspace_data_cleanup (struct program_space *pspace, void *arg)
488 {
489 struct auto_load_pspace_info *info;
490
491 info = program_space_data (pspace, auto_load_pspace_data);
492 if (info != NULL)
493 {
494 if (info->loaded_scripts)
495 htab_delete (info->loaded_scripts);
496 xfree (info);
497 }
498 }
499
500 /* Get the current autoload data. If none is found yet, add it now. This
501 function always returns a valid object. */
502
503 static struct auto_load_pspace_info *
504 get_auto_load_pspace_data (struct program_space *pspace)
505 {
506 struct auto_load_pspace_info *info;
507
508 info = program_space_data (pspace, auto_load_pspace_data);
509 if (info == NULL)
510 {
511 info = XZALLOC (struct auto_load_pspace_info);
512 set_program_space_data (pspace, auto_load_pspace_data, info);
513 }
514
515 return info;
516 }
517
518 /* Hash function for the loaded script hash. */
519
520 static hashval_t
521 hash_loaded_script_entry (const void *data)
522 {
523 const struct loaded_script *e = data;
524
525 return htab_hash_string (e->name) ^ htab_hash_pointer (e->language);
526 }
527
528 /* Equality function for the loaded script hash. */
529
530 static int
531 eq_loaded_script_entry (const void *a, const void *b)
532 {
533 const struct loaded_script *ea = a;
534 const struct loaded_script *eb = b;
535
536 return strcmp (ea->name, eb->name) == 0 && ea->language == eb->language;
537 }
538
539 /* Initialize the table to track loaded scripts.
540 Each entry is hashed by the full path name. */
541
542 static void
543 init_loaded_scripts_info (struct auto_load_pspace_info *pspace_info)
544 {
545 /* Choose 31 as the starting size of the hash table, somewhat arbitrarily.
546 Space for each entry is obtained with one malloc so we can free them
547 easily. */
548
549 pspace_info->loaded_scripts = htab_create (31,
550 hash_loaded_script_entry,
551 eq_loaded_script_entry,
552 xfree);
553
554 pspace_info->script_not_found_warning_printed = FALSE;
555 }
556
557 /* Wrapper on get_auto_load_pspace_data to also allocate the hash table
558 for loading scripts. */
559
560 struct auto_load_pspace_info *
561 get_auto_load_pspace_data_for_loading (struct program_space *pspace)
562 {
563 struct auto_load_pspace_info *info;
564
565 info = get_auto_load_pspace_data (pspace);
566 if (info->loaded_scripts == NULL)
567 init_loaded_scripts_info (info);
568
569 return info;
570 }
571
572 /* Add script NAME in LANGUAGE to hash table of PSPACE_INFO. LOADED 1 if the
573 script has been (is going to) be loaded, 0 otherwise (such as if it has not
574 been found). FULL_PATH is NULL if the script wasn't found. The result is
575 true if the script was already in the hash table. */
576
577 int
578 maybe_add_script (struct auto_load_pspace_info *pspace_info, int loaded,
579 const char *name, const char *full_path,
580 const struct script_language *language)
581 {
582 struct htab *htab = pspace_info->loaded_scripts;
583 struct loaded_script **slot, entry;
584 int in_hash_table;
585
586 entry.name = name;
587 entry.language = language;
588 slot = (struct loaded_script **) htab_find_slot (htab, &entry, INSERT);
589 in_hash_table = *slot != NULL;
590
591 /* If this script is not in the hash table, add it. */
592
593 if (! in_hash_table)
594 {
595 char *p;
596
597 /* Allocate all space in one chunk so it's easier to free. */
598 *slot = xmalloc (sizeof (**slot)
599 + strlen (name) + 1
600 + (full_path != NULL ? (strlen (full_path) + 1) : 0));
601 p = ((char*) *slot) + sizeof (**slot);
602 strcpy (p, name);
603 (*slot)->name = p;
604 if (full_path != NULL)
605 {
606 p += strlen (p) + 1;
607 strcpy (p, full_path);
608 (*slot)->full_path = p;
609 }
610 else
611 (*slot)->full_path = NULL;
612 (*slot)->loaded = loaded;
613 (*slot)->language = language;
614 }
615
616 return in_hash_table;
617 }
618
619 /* Clear the table of loaded section scripts. */
620
621 static void
622 clear_section_scripts (void)
623 {
624 struct program_space *pspace = current_program_space;
625 struct auto_load_pspace_info *info;
626
627 info = program_space_data (pspace, auto_load_pspace_data);
628 if (info != NULL && info->loaded_scripts != NULL)
629 {
630 htab_delete (info->loaded_scripts);
631 info->loaded_scripts = NULL;
632 info->script_not_found_warning_printed = FALSE;
633 }
634 }
635
636 /* Look for the auto-load script in LANGUAGE associated with OBJFILE and load
637 it. */
638
639 void
640 auto_load_objfile_script (struct objfile *objfile,
641 const struct script_language *language)
642 {
643 char *realname;
644 char *filename, *debugfile;
645 int len;
646 FILE *input;
647 struct cleanup *cleanups;
648
649 realname = gdb_realpath (objfile->name);
650 len = strlen (realname);
651 filename = xmalloc (len + strlen (language->suffix) + 1);
652 memcpy (filename, realname, len);
653 strcpy (filename + len, language->suffix);
654
655 cleanups = make_cleanup (xfree, filename);
656 make_cleanup (xfree, realname);
657
658 input = fopen (filename, "r");
659 debugfile = filename;
660 if (debug_auto_load)
661 fprintf_unfiltered (gdb_stdlog, _("auto-load: Attempted file \"%s\" %s.\n"),
662 debugfile, input ? _("exists") : _("does not exist"));
663
664 if (!input)
665 {
666 VEC (char_ptr) *vec;
667 int ix;
668 char *dir;
669
670 /* Also try the same file in a subdirectory of gdb's data
671 directory. */
672
673 vec = auto_load_expand_dir_vars (auto_load_dir);
674 make_cleanup_free_char_ptr_vec (vec);
675
676 if (debug_auto_load)
677 fprintf_unfiltered (gdb_stdlog, _("auto-load: Searching 'set auto-load "
678 "scripts-directory' path \"%s\".\n"),
679 auto_load_dir);
680
681 for (ix = 0; VEC_iterate (char_ptr, vec, ix, dir); ++ix)
682 {
683 debugfile = xmalloc (strlen (dir) + strlen (filename) + 1);
684 strcpy (debugfile, dir);
685
686 /* FILENAME is absolute, so we don't need a "/" here. */
687 strcat (debugfile, filename);
688
689 make_cleanup (xfree, debugfile);
690 input = fopen (debugfile, "r");
691 if (debug_auto_load)
692 fprintf_unfiltered (gdb_stdlog, _("auto-load: Attempted file "
693 "\"%s\" %s.\n"),
694 debugfile,
695 input ? _("exists") : _("does not exist"));
696 if (input != NULL)
697 break;
698 }
699 }
700
701 if (input)
702 {
703 make_cleanup_fclose (input);
704
705 /* To preserve existing behaviour we don't check for whether the
706 script was already in the table, and always load it.
707 It's highly unlikely that we'd ever load it twice,
708 and these scripts are required to be idempotent under multiple
709 loads anyway. */
710 language->source_script_for_objfile (objfile, input, debugfile);
711 }
712
713 do_cleanups (cleanups);
714 }
715
716 /* Load any auto-loaded scripts for OBJFILE. */
717
718 void
719 load_auto_scripts_for_objfile (struct objfile *objfile)
720 {
721 if (!global_auto_load)
722 return;
723
724 if (auto_load_gdb_scripts)
725 auto_load_objfile_script (objfile, &script_language_gdb);
726
727 gdbpy_load_auto_scripts_for_objfile (objfile);
728 }
729
730 /* This is a new_objfile observer callback to auto-load scripts.
731
732 Two flavors of auto-loaded scripts are supported.
733 1) based on the path to the objfile
734 2) from .debug_gdb_scripts section */
735
736 static void
737 auto_load_new_objfile (struct objfile *objfile)
738 {
739 if (!objfile)
740 {
741 /* OBJFILE is NULL when loading a new "main" symbol-file. */
742 clear_section_scripts ();
743 return;
744 }
745
746 load_auto_scripts_for_objfile (objfile);
747 }
748
749 /* Collect scripts to be printed in a vec. */
750
751 typedef struct loaded_script *loaded_script_ptr;
752 DEF_VEC_P (loaded_script_ptr);
753
754 struct collect_matching_scripts_data
755 {
756 VEC (loaded_script_ptr) **scripts_p;
757
758 const struct script_language *language;
759 };
760
761 /* Traversal function for htab_traverse.
762 Collect the entry if it matches the regexp. */
763
764 static int
765 collect_matching_scripts (void **slot, void *info)
766 {
767 struct loaded_script *script = *slot;
768 struct collect_matching_scripts_data *data = info;
769
770 if (script->language == data->language && re_exec (script->name))
771 VEC_safe_push (loaded_script_ptr, *data->scripts_p, script);
772
773 return 1;
774 }
775
776 /* Print SCRIPT. */
777
778 static void
779 print_script (struct loaded_script *script)
780 {
781 struct ui_out *uiout = current_uiout;
782 struct cleanup *chain;
783
784 chain = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
785
786 ui_out_field_string (uiout, "loaded", script->loaded ? "Yes" : "No");
787 ui_out_field_string (uiout, "script", script->name);
788 ui_out_text (uiout, "\n");
789
790 /* If the name isn't the full path, print it too. */
791 if (script->full_path != NULL
792 && strcmp (script->name, script->full_path) != 0)
793 {
794 ui_out_text (uiout, "\tfull name: ");
795 ui_out_field_string (uiout, "full_path", script->full_path);
796 ui_out_text (uiout, "\n");
797 }
798
799 do_cleanups (chain);
800 }
801
802 /* Helper for info_auto_load_scripts to sort the scripts by name. */
803
804 static int
805 sort_scripts_by_name (const void *ap, const void *bp)
806 {
807 const struct loaded_script *a = *(const struct loaded_script **) ap;
808 const struct loaded_script *b = *(const struct loaded_script **) bp;
809
810 return FILENAME_CMP (a->name, b->name);
811 }
812
813 /* Special internal GDB value of auto_load_info_scripts's PATTERN identify
814 the "info auto-load XXX" command has been executed through the general
815 "info auto-load" invocation. Extra newline will be printed if needed. */
816 char auto_load_info_scripts_pattern_nl[] = "";
817
818 /* Implementation for "info auto-load gdb-scripts"
819 (and "info auto-load python-scripts"). List scripts in LANGUAGE matching
820 PATTERN. FROM_TTY is the usual GDB boolean for user interactivity. */
821
822 void
823 auto_load_info_scripts (char *pattern, int from_tty,
824 const struct script_language *language)
825 {
826 struct ui_out *uiout = current_uiout;
827 struct auto_load_pspace_info *pspace_info;
828 struct cleanup *script_chain;
829 VEC (loaded_script_ptr) *scripts;
830 int nr_scripts;
831
832 dont_repeat ();
833
834 pspace_info = get_auto_load_pspace_data (current_program_space);
835
836 if (pattern && *pattern)
837 {
838 char *re_err = re_comp (pattern);
839
840 if (re_err)
841 error (_("Invalid regexp: %s"), re_err);
842 }
843 else
844 {
845 re_comp ("");
846 }
847
848 /* We need to know the number of rows before we build the table.
849 Plus we want to sort the scripts by name.
850 So first traverse the hash table collecting the matching scripts. */
851
852 scripts = VEC_alloc (loaded_script_ptr, 10);
853 script_chain = make_cleanup (VEC_cleanup (loaded_script_ptr), &scripts);
854
855 if (pspace_info != NULL && pspace_info->loaded_scripts != NULL)
856 {
857 struct collect_matching_scripts_data data = { &scripts, language };
858
859 immediate_quit++;
860 /* Pass a pointer to scripts as VEC_safe_push can realloc space. */
861 htab_traverse_noresize (pspace_info->loaded_scripts,
862 collect_matching_scripts, &data);
863 immediate_quit--;
864 }
865
866 nr_scripts = VEC_length (loaded_script_ptr, scripts);
867
868 /* Table header shifted right by preceding "gdb-scripts: " would not match
869 its columns. */
870 if (nr_scripts > 0 && pattern == auto_load_info_scripts_pattern_nl)
871 ui_out_text (uiout, "\n");
872
873 make_cleanup_ui_out_table_begin_end (uiout, 2, nr_scripts,
874 "AutoLoadedScriptsTable");
875
876 ui_out_table_header (uiout, 7, ui_left, "loaded", "Loaded");
877 ui_out_table_header (uiout, 70, ui_left, "script", "Script");
878 ui_out_table_body (uiout);
879
880 if (nr_scripts > 0)
881 {
882 int i;
883 loaded_script_ptr script;
884
885 qsort (VEC_address (loaded_script_ptr, scripts),
886 VEC_length (loaded_script_ptr, scripts),
887 sizeof (loaded_script_ptr), sort_scripts_by_name);
888 for (i = 0; VEC_iterate (loaded_script_ptr, scripts, i, script); ++i)
889 print_script (script);
890 }
891
892 do_cleanups (script_chain);
893
894 if (nr_scripts == 0)
895 {
896 if (pattern && *pattern)
897 ui_out_message (uiout, 0, "No auto-load scripts matching %s.\n",
898 pattern);
899 else
900 ui_out_message (uiout, 0, "No auto-load scripts.\n");
901 }
902 }
903
904 /* Wrapper for "info auto-load gdb-scripts". */
905
906 static void
907 info_auto_load_gdb_scripts (char *pattern, int from_tty)
908 {
909 auto_load_info_scripts (pattern, from_tty, &script_language_gdb);
910 }
911
912 /* Implement 'info auto-load local-gdbinit'. */
913
914 static void
915 info_auto_load_local_gdbinit (char *args, int from_tty)
916 {
917 if (auto_load_local_gdbinit_pathname == NULL)
918 printf_filtered (_("Local .gdbinit file was not found.\n"));
919 else if (auto_load_local_gdbinit_loaded)
920 printf_filtered (_("Local .gdbinit file \"%s\" has been loaded.\n"),
921 auto_load_local_gdbinit_pathname);
922 else
923 printf_filtered (_("Local .gdbinit file \"%s\" has not been loaded.\n"),
924 auto_load_local_gdbinit_pathname);
925 }
926
927 /* Return non-zero if SCRIPT_NOT_FOUND_WARNING_PRINTED of PSPACE_INFO was unset
928 before calling this function. Always set SCRIPT_NOT_FOUND_WARNING_PRINTED
929 of PSPACE_INFO. */
930
931 int
932 script_not_found_warning_print (struct auto_load_pspace_info *pspace_info)
933 {
934 int retval = !pspace_info->script_not_found_warning_printed;
935
936 pspace_info->script_not_found_warning_printed = 1;
937
938 return retval;
939 }
940
941 /* The only valid "set auto-load" argument is off|0|no|disable. */
942
943 static void
944 set_auto_load_cmd (char *args, int from_tty)
945 {
946 struct cmd_list_element *list;
947 size_t length;
948
949 /* See parse_binary_operation in use by the sub-commands. */
950
951 length = args ? strlen (args) : 0;
952
953 while (length > 0 && (args[length - 1] == ' ' || args[length - 1] == '\t'))
954 length--;
955
956 if (length == 0 || (strncmp (args, "off", length) != 0
957 && strncmp (args, "0", length) != 0
958 && strncmp (args, "no", length) != 0
959 && strncmp (args, "disable", length) != 0))
960 error (_("Valid is only global 'set auto-load no'; "
961 "otherwise check the auto-load sub-commands."));
962
963 for (list = *auto_load_set_cmdlist_get (); list != NULL; list = list->next)
964 if (list->var_type == var_boolean)
965 {
966 gdb_assert (list->type == set_cmd);
967 do_setshow_command (args, from_tty, list);
968 }
969 }
970
971 /* Initialize "set auto-load " commands prefix and return it. */
972
973 struct cmd_list_element **
974 auto_load_set_cmdlist_get (void)
975 {
976 static struct cmd_list_element *retval;
977
978 if (retval == NULL)
979 add_prefix_cmd ("auto-load", class_maintenance, set_auto_load_cmd, _("\
980 Auto-loading specific settings.\n\
981 Configure various auto-load-specific variables such as\n\
982 automatic loading of Python scripts."),
983 &retval, "set auto-load ",
984 1/*allow-unknown*/, &setlist);
985
986 return &retval;
987 }
988
989 /* Command "show auto-load" displays summary of all the current
990 "show auto-load " settings. */
991
992 static void
993 show_auto_load_cmd (char *args, int from_tty)
994 {
995 cmd_show_list (*auto_load_show_cmdlist_get (), from_tty, "");
996 }
997
998 /* Initialize "show auto-load " commands prefix and return it. */
999
1000 struct cmd_list_element **
1001 auto_load_show_cmdlist_get (void)
1002 {
1003 static struct cmd_list_element *retval;
1004
1005 if (retval == NULL)
1006 add_prefix_cmd ("auto-load", class_maintenance, show_auto_load_cmd, _("\
1007 Show auto-loading specific settings.\n\
1008 Show configuration of various auto-load-specific variables such as\n\
1009 automatic loading of Python scripts."),
1010 &retval, "show auto-load ",
1011 0/*allow-unknown*/, &showlist);
1012
1013 return &retval;
1014 }
1015
1016 /* Command "info auto-load" displays whether the various auto-load files have
1017 been loaded. This is reimplementation of cmd_show_list which inserts
1018 newlines at proper places. */
1019
1020 static void
1021 info_auto_load_cmd (char *args, int from_tty)
1022 {
1023 struct cmd_list_element *list;
1024 struct cleanup *infolist_chain;
1025 struct ui_out *uiout = current_uiout;
1026
1027 infolist_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "infolist");
1028
1029 for (list = *auto_load_info_cmdlist_get (); list != NULL; list = list->next)
1030 {
1031 struct cleanup *option_chain
1032 = make_cleanup_ui_out_tuple_begin_end (uiout, "option");
1033
1034 gdb_assert (!list->prefixlist);
1035 gdb_assert (list->type == not_set_cmd);
1036
1037 ui_out_field_string (uiout, "name", list->name);
1038 ui_out_text (uiout, ": ");
1039 cmd_func (list, auto_load_info_scripts_pattern_nl, from_tty);
1040
1041 /* Close the tuple. */
1042 do_cleanups (option_chain);
1043 }
1044
1045 /* Close the tuple. */
1046 do_cleanups (infolist_chain);
1047 }
1048
1049 /* Initialize "info auto-load " commands prefix and return it. */
1050
1051 struct cmd_list_element **
1052 auto_load_info_cmdlist_get (void)
1053 {
1054 static struct cmd_list_element *retval;
1055
1056 if (retval == NULL)
1057 add_prefix_cmd ("auto-load", class_info, info_auto_load_cmd, _("\
1058 Print current status of auto-loaded files.\n\
1059 Print whether various files like Python scripts or .gdbinit files have been\n\
1060 found and/or loaded."),
1061 &retval, "info auto-load ",
1062 0/*allow-unknown*/, &infolist);
1063
1064 return &retval;
1065 }
1066
1067 void _initialize_auto_load (void);
1068
1069 void
1070 _initialize_auto_load (void)
1071 {
1072 struct cmd_list_element *cmd;
1073
1074 auto_load_pspace_data
1075 = register_program_space_data_with_cleanup (auto_load_pspace_data_cleanup);
1076
1077 observer_attach_new_objfile (auto_load_new_objfile);
1078
1079 add_setshow_boolean_cmd ("gdb-scripts", class_support,
1080 &auto_load_gdb_scripts, _("\
1081 Enable or disable auto-loading of canned sequences of commands scripts."), _("\
1082 Show whether auto-loading of canned sequences of commands scripts is enabled."),
1083 _("\
1084 If enabled, canned sequences of commands are loaded when the debugger reads\n\
1085 an executable or shared library.\n\
1086 This options has security implications for untrusted inferiors."),
1087 NULL, show_auto_load_gdb_scripts,
1088 auto_load_set_cmdlist_get (),
1089 auto_load_show_cmdlist_get ());
1090
1091 add_cmd ("gdb-scripts", class_info, info_auto_load_gdb_scripts,
1092 _("Print the list of automatically loaded sequences of commands.\n\
1093 Usage: info auto-load gdb-scripts [REGEXP]"),
1094 auto_load_info_cmdlist_get ());
1095
1096 add_setshow_boolean_cmd ("local-gdbinit", class_support,
1097 &auto_load_local_gdbinit, _("\
1098 Enable or disable auto-loading of .gdbinit script in current directory."), _("\
1099 Show whether auto-loading .gdbinit script in current directory is enabled."),
1100 _("\
1101 If enabled, canned sequences of commands are loaded when debugger starts\n\
1102 from .gdbinit file in current directory. Such files are deprecated,\n\
1103 use a script associated with inferior executable file instead.\n\
1104 This options has security implications for untrusted inferiors."),
1105 NULL, show_auto_load_local_gdbinit,
1106 auto_load_set_cmdlist_get (),
1107 auto_load_show_cmdlist_get ());
1108
1109 add_cmd ("local-gdbinit", class_info, info_auto_load_local_gdbinit,
1110 _("Print whether current directory .gdbinit file has been loaded.\n\
1111 Usage: info auto-load local-gdbinit"),
1112 auto_load_info_cmdlist_get ());
1113
1114 auto_load_dir = xstrdup (AUTO_LOAD_DIR);
1115 add_setshow_optional_filename_cmd ("scripts-directory", class_support,
1116 &auto_load_dir, _("\
1117 Set the list of directories from which to load auto-loaded scripts."), _("\
1118 Show the list of directories from which to load auto-loaded scripts."), _("\
1119 Automatically loaded Python scripts and GDB scripts are located in one of the\n\
1120 directories listed by this option. This option is ignored for the kinds of\n\
1121 scripts having 'set auto-load ... off'. Directories listed here need to be\n\
1122 present also in the 'set auto-load safe-path' option."),
1123 set_auto_load_dir, show_auto_load_dir,
1124 auto_load_set_cmdlist_get (),
1125 auto_load_show_cmdlist_get ());
1126
1127 auto_load_safe_path = xstrdup (AUTO_LOAD_SAFE_PATH);
1128 auto_load_safe_path_vec_update ();
1129 add_setshow_optional_filename_cmd ("safe-path", class_support,
1130 &auto_load_safe_path, _("\
1131 Set the list of directories from which it is safe to auto-load files."), _("\
1132 Show the list of directories from which it is safe to auto-load files."), _("\
1133 Various files loaded automatically for the 'set auto-load ...' options must\n\
1134 be located in one of the directories listed by this option. Warning will be\n\
1135 printed and file will not be used otherwise.\n\
1136 Setting this parameter to an empty list resets it to its default value.\n\
1137 Setting this parameter to '/' (without the quotes) allows any file\n\
1138 for the 'set auto-load ...' options.\n\
1139 This option is ignored for the kinds of files having 'set auto-load ... off'.\n\
1140 This options has security implications for untrusted inferiors."),
1141 set_auto_load_safe_path,
1142 show_auto_load_safe_path,
1143 auto_load_set_cmdlist_get (),
1144 auto_load_show_cmdlist_get ());
1145 observer_attach_gdb_datadir_changed (auto_load_gdb_datadir_changed);
1146
1147 cmd = add_cmd ("add-auto-load-safe-path", class_support,
1148 add_auto_load_safe_path,
1149 _("Add entries to the list of directories from which it is safe "
1150 "to auto-load files.\n\
1151 See the commands 'set auto-load safe-path' and 'show auto-load safe-path' to\n\
1152 access the current full list setting."),
1153 &cmdlist);
1154 set_cmd_completer (cmd, filename_completer);
1155
1156 add_setshow_boolean_cmd ("auto-load", class_maintenance,
1157 &debug_auto_load, _("\
1158 Set auto-load verifications debugging."), _("\
1159 Show auto-load verifications debugging."), _("\
1160 When non-zero, debugging output for files of 'set auto-load ...'\n\
1161 is displayed."),
1162 NULL, show_debug_auto_load,
1163 &setdebuglist, &showdebuglist);
1164 }
This page took 0.053364 seconds and 4 git commands to generate.