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