1 /* GDB routines for supporting auto-loaded scripts.
3 Copyright (C) 2012 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
21 #include "auto-load.h"
22 #include "progspace.h"
23 #include "python/python.h"
24 #include "gdb_regex.h"
26 #include "filenames.h"
30 #include "exceptions.h"
31 #include "cli/cli-script.h"
33 #include "cli/cli-decode.h"
34 #include "cli/cli-setshow.h"
36 /* The suffix of per-objfile scripts to auto-load as non-Python command files.
37 E.g. When the program loads libfoo.so, look for libfoo-gdb.gdb. */
38 #define GDB_AUTO_FILE_NAME "-gdb.gdb"
40 static void source_gdb_script_for_objfile (struct objfile
*objfile
, FILE *file
,
41 const char *filename
);
43 /* User-settable option to enable/disable auto-loading of GDB_AUTO_FILE_NAME
45 set auto-load gdb-scripts on|off
46 This is true if we should auto-load associated scripts when an objfile
47 is opened, false otherwise. */
48 static int auto_load_gdb_scripts
= 1;
50 /* "show" command for the auto_load_gdb_scripts configuration variable. */
53 show_auto_load_gdb_scripts (struct ui_file
*file
, int from_tty
,
54 struct cmd_list_element
*c
, const char *value
)
56 fprintf_filtered (file
, _("Auto-loading of canned sequences of commands "
61 /* Internal-use flag to enable/disable auto-loading.
62 This is true if we should auto-load python code when an objfile is opened,
65 Both auto_load_scripts && global_auto_load must be true to enable
68 This flag exists to facilitate deferring auto-loading during start-up
69 until after ./.gdbinit has been read; it may augment the search directories
70 used to find the scripts. */
71 int global_auto_load
= 1;
73 /* Auto-load .gdbinit file from the current directory? */
74 int auto_load_local_gdbinit
= 1;
76 /* Absolute pathname to the current directory .gdbinit, if it exists. */
77 char *auto_load_local_gdbinit_pathname
= NULL
;
79 /* Boolean value if AUTO_LOAD_LOCAL_GDBINIT_PATHNAME has been loaded. */
80 int auto_load_local_gdbinit_loaded
= 0;
82 /* "show" command for the auto_load_local_gdbinit configuration variable. */
85 show_auto_load_local_gdbinit (struct ui_file
*file
, int from_tty
,
86 struct cmd_list_element
*c
, const char *value
)
88 fprintf_filtered (file
, _("Auto-loading of .gdbinit script from current "
89 "directory is %s.\n"),
93 /* Definition of script language for GDB canned sequences of commands. */
95 static const struct script_language script_language_gdb
96 = { GDB_AUTO_FILE_NAME
, source_gdb_script_for_objfile
};
99 source_gdb_script_for_objfile (struct objfile
*objfile
, FILE *file
,
100 const char *filename
)
102 struct auto_load_pspace_info
*pspace_info
;
103 volatile struct gdb_exception e
;
105 /* Add this script to the hash table too so "info auto-load gdb-scripts"
107 pspace_info
= get_auto_load_pspace_data_for_loading (current_program_space
);
108 maybe_add_script (pspace_info
, filename
, filename
, &script_language_gdb
);
110 TRY_CATCH (e
, RETURN_MASK_ALL
)
112 script_from_file (file
, filename
);
114 exception_print (gdb_stderr
, e
);
117 /* For scripts specified in .debug_gdb_scripts, multiple objfiles may load
118 the same script. There's no point in loading the script multiple times,
119 and there can be a lot of objfiles and scripts, so we keep track of scripts
122 struct auto_load_pspace_info
124 /* For each program space we keep track of loaded scripts. */
125 struct htab
*loaded_scripts
;
127 /* Non-zero if we've issued the warning about an auto-load script not being
128 found. We only want to issue this warning once. */
129 int script_not_found_warning_printed
;
132 /* Objects of this type are stored in the loaded script hash table. */
136 /* Name as provided by the objfile. */
139 /* Full path name or NULL if script wasn't found (or was otherwise
141 const char *full_path
;
143 const struct script_language
*language
;
146 /* Per-program-space data key. */
147 static const struct program_space_data
*auto_load_pspace_data
;
150 auto_load_pspace_data_cleanup (struct program_space
*pspace
, void *arg
)
152 struct auto_load_pspace_info
*info
;
154 info
= program_space_data (pspace
, auto_load_pspace_data
);
157 if (info
->loaded_scripts
)
158 htab_delete (info
->loaded_scripts
);
163 /* Get the current autoload data. If none is found yet, add it now. This
164 function always returns a valid object. */
166 static struct auto_load_pspace_info
*
167 get_auto_load_pspace_data (struct program_space
*pspace
)
169 struct auto_load_pspace_info
*info
;
171 info
= program_space_data (pspace
, auto_load_pspace_data
);
174 info
= XZALLOC (struct auto_load_pspace_info
);
175 set_program_space_data (pspace
, auto_load_pspace_data
, info
);
181 /* Hash function for the loaded script hash. */
184 hash_loaded_script_entry (const void *data
)
186 const struct loaded_script
*e
= data
;
188 return htab_hash_string (e
->name
) ^ htab_hash_pointer (e
->language
);
191 /* Equality function for the loaded script hash. */
194 eq_loaded_script_entry (const void *a
, const void *b
)
196 const struct loaded_script
*ea
= a
;
197 const struct loaded_script
*eb
= b
;
199 return strcmp (ea
->name
, eb
->name
) == 0 && ea
->language
== eb
->language
;
202 /* Initialize the table to track loaded scripts.
203 Each entry is hashed by the full path name. */
206 init_loaded_scripts_info (struct auto_load_pspace_info
*pspace_info
)
208 /* Choose 31 as the starting size of the hash table, somewhat arbitrarily.
209 Space for each entry is obtained with one malloc so we can free them
212 pspace_info
->loaded_scripts
= htab_create (31,
213 hash_loaded_script_entry
,
214 eq_loaded_script_entry
,
217 pspace_info
->script_not_found_warning_printed
= FALSE
;
220 /* Wrapper on get_auto_load_pspace_data to also allocate the hash table
221 for loading scripts. */
223 struct auto_load_pspace_info
*
224 get_auto_load_pspace_data_for_loading (struct program_space
*pspace
)
226 struct auto_load_pspace_info
*info
;
228 info
= get_auto_load_pspace_data (pspace
);
229 if (info
->loaded_scripts
== NULL
)
230 init_loaded_scripts_info (info
);
235 /* Add script NAME in LANGUAGE to hash table of PSPACE_INFO.
236 FULL_PATH is NULL if the script wasn't found. The result is
237 true if the script was already in the hash table. */
240 maybe_add_script (struct auto_load_pspace_info
*pspace_info
,
241 const char *name
, const char *full_path
,
242 const struct script_language
*language
)
244 struct htab
*htab
= pspace_info
->loaded_scripts
;
245 struct loaded_script
**slot
, entry
;
249 entry
.language
= language
;
250 slot
= (struct loaded_script
**) htab_find_slot (htab
, &entry
, INSERT
);
251 in_hash_table
= *slot
!= NULL
;
253 /* If this script is not in the hash table, add it. */
259 /* Allocate all space in one chunk so it's easier to free. */
260 *slot
= xmalloc (sizeof (**slot
)
262 + (full_path
!= NULL
? (strlen (full_path
) + 1) : 0));
263 p
= ((char*) *slot
) + sizeof (**slot
);
266 if (full_path
!= NULL
)
269 strcpy (p
, full_path
);
270 (*slot
)->full_path
= p
;
273 (*slot
)->full_path
= NULL
;
274 (*slot
)->language
= language
;
277 return in_hash_table
;
280 /* Clear the table of loaded section scripts. */
283 clear_section_scripts (void)
285 struct program_space
*pspace
= current_program_space
;
286 struct auto_load_pspace_info
*info
;
288 info
= program_space_data (pspace
, auto_load_pspace_data
);
289 if (info
!= NULL
&& info
->loaded_scripts
!= NULL
)
291 htab_delete (info
->loaded_scripts
);
292 info
->loaded_scripts
= NULL
;
293 info
->script_not_found_warning_printed
= FALSE
;
297 /* Look for the auto-load script in LANGUAGE associated with OBJFILE and load
301 auto_load_objfile_script (struct objfile
*objfile
,
302 const struct script_language
*language
)
305 char *filename
, *debugfile
;
308 struct cleanup
*cleanups
;
310 realname
= gdb_realpath (objfile
->name
);
311 len
= strlen (realname
);
312 filename
= xmalloc (len
+ strlen (language
->suffix
) + 1);
313 memcpy (filename
, realname
, len
);
314 strcpy (filename
+ len
, language
->suffix
);
316 cleanups
= make_cleanup (xfree
, filename
);
317 make_cleanup (xfree
, realname
);
319 input
= fopen (filename
, "r");
320 debugfile
= filename
;
322 if (!input
&& debug_file_directory
)
324 /* Also try the same file in the separate debug info directory. */
325 debugfile
= xmalloc (strlen (filename
)
326 + strlen (debug_file_directory
) + 1);
327 strcpy (debugfile
, debug_file_directory
);
328 /* FILENAME is absolute, so we don't need a "/" here. */
329 strcat (debugfile
, filename
);
331 make_cleanup (xfree
, debugfile
);
332 input
= fopen (debugfile
, "r");
335 if (!input
&& gdb_datadir
)
337 /* Also try the same file in a subdirectory of gdb's data
339 debugfile
= xmalloc (strlen (gdb_datadir
) + strlen (filename
)
340 + strlen ("/auto-load") + 1);
341 strcpy (debugfile
, gdb_datadir
);
342 strcat (debugfile
, "/auto-load");
343 /* FILENAME is absolute, so we don't need a "/" here. */
344 strcat (debugfile
, filename
);
346 make_cleanup (xfree
, debugfile
);
347 input
= fopen (debugfile
, "r");
352 make_cleanup_fclose (input
);
354 /* To preserve existing behaviour we don't check for whether the
355 script was already in the table, and always load it.
356 It's highly unlikely that we'd ever load it twice,
357 and these scripts are required to be idempotent under multiple
359 language
->source_script_for_objfile (objfile
, input
, debugfile
);
362 do_cleanups (cleanups
);
365 /* Load any auto-loaded scripts for OBJFILE. */
368 load_auto_scripts_for_objfile (struct objfile
*objfile
)
370 if (!global_auto_load
)
373 if (auto_load_gdb_scripts
)
374 auto_load_objfile_script (objfile
, &script_language_gdb
);
376 gdbpy_load_auto_scripts_for_objfile (objfile
);
379 /* This is a new_objfile observer callback to auto-load scripts.
381 Two flavors of auto-loaded scripts are supported.
382 1) based on the path to the objfile
383 2) from .debug_gdb_scripts section */
386 auto_load_new_objfile (struct objfile
*objfile
)
390 /* OBJFILE is NULL when loading a new "main" symbol-file. */
391 clear_section_scripts ();
395 load_auto_scripts_for_objfile (objfile
);
398 /* Collect scripts to be printed in a vec. */
400 typedef struct loaded_script
*loaded_script_ptr
;
401 DEF_VEC_P (loaded_script_ptr
);
403 struct collect_matching_scripts_data
405 VEC (loaded_script_ptr
) **scripts_p
;
407 const struct script_language
*language
;
410 /* Traversal function for htab_traverse.
411 Collect the entry if it matches the regexp. */
414 collect_matching_scripts (void **slot
, void *info
)
416 struct loaded_script
*script
= *slot
;
417 struct collect_matching_scripts_data
*data
= info
;
419 if (script
->language
== data
->language
&& re_exec (script
->name
))
420 VEC_safe_push (loaded_script_ptr
, *data
->scripts_p
, script
);
428 print_script (struct loaded_script
*script
)
430 struct ui_out
*uiout
= current_uiout
;
431 struct cleanup
*chain
;
433 chain
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
435 ui_out_field_string (uiout
, "loaded", script
->full_path
? "Yes" : "Missing");
436 ui_out_field_string (uiout
, "script", script
->name
);
437 ui_out_text (uiout
, "\n");
439 /* If the name isn't the full path, print it too. */
440 if (script
->full_path
!= NULL
441 && strcmp (script
->name
, script
->full_path
) != 0)
443 ui_out_text (uiout
, "\tfull name: ");
444 ui_out_field_string (uiout
, "full_path", script
->full_path
);
445 ui_out_text (uiout
, "\n");
451 /* Helper for info_auto_load_scripts to sort the scripts by name. */
454 sort_scripts_by_name (const void *ap
, const void *bp
)
456 const struct loaded_script
*a
= *(const struct loaded_script
**) ap
;
457 const struct loaded_script
*b
= *(const struct loaded_script
**) bp
;
459 return FILENAME_CMP (a
->name
, b
->name
);
462 /* Special internal GDB value of auto_load_info_scripts's PATTERN identify
463 the "info auto-load XXX" command has been executed through the general
464 "info auto-load" invocation. Extra newline will be printed if needed. */
465 char auto_load_info_scripts_pattern_nl
[] = "";
467 /* Implementation for "info auto-load gdb-scripts"
468 (and "info auto-load python-scripts"). List scripts in LANGUAGE matching
469 PATTERN. FROM_TTY is the usual GDB boolean for user interactivity. */
472 auto_load_info_scripts (char *pattern
, int from_tty
,
473 const struct script_language
*language
)
475 struct ui_out
*uiout
= current_uiout
;
476 struct auto_load_pspace_info
*pspace_info
;
477 struct cleanup
*script_chain
;
478 VEC (loaded_script_ptr
) *scripts
;
483 pspace_info
= get_auto_load_pspace_data (current_program_space
);
485 if (pattern
&& *pattern
)
487 char *re_err
= re_comp (pattern
);
490 error (_("Invalid regexp: %s"), re_err
);
497 /* We need to know the number of rows before we build the table.
498 Plus we want to sort the scripts by name.
499 So first traverse the hash table collecting the matching scripts. */
501 scripts
= VEC_alloc (loaded_script_ptr
, 10);
502 script_chain
= make_cleanup (VEC_cleanup (loaded_script_ptr
), &scripts
);
504 if (pspace_info
!= NULL
&& pspace_info
->loaded_scripts
!= NULL
)
506 struct collect_matching_scripts_data data
= { &scripts
, language
};
509 /* Pass a pointer to scripts as VEC_safe_push can realloc space. */
510 htab_traverse_noresize (pspace_info
->loaded_scripts
,
511 collect_matching_scripts
, &data
);
515 nr_scripts
= VEC_length (loaded_script_ptr
, scripts
);
517 /* Table header shifted right by preceding "gdb-scripts: " would not match
519 if (nr_scripts
> 0 && pattern
== auto_load_info_scripts_pattern_nl
)
520 ui_out_text (uiout
, "\n");
522 make_cleanup_ui_out_table_begin_end (uiout
, 2, nr_scripts
,
523 "AutoLoadedScriptsTable");
525 ui_out_table_header (uiout
, 7, ui_left
, "loaded", "Loaded");
526 ui_out_table_header (uiout
, 70, ui_left
, "script", "Script");
527 ui_out_table_body (uiout
);
532 loaded_script_ptr script
;
534 qsort (VEC_address (loaded_script_ptr
, scripts
),
535 VEC_length (loaded_script_ptr
, scripts
),
536 sizeof (loaded_script_ptr
), sort_scripts_by_name
);
537 for (i
= 0; VEC_iterate (loaded_script_ptr
, scripts
, i
, script
); ++i
)
538 print_script (script
);
541 do_cleanups (script_chain
);
545 if (pattern
&& *pattern
)
546 ui_out_message (uiout
, 0, "No auto-load scripts matching %s.\n",
549 ui_out_message (uiout
, 0, "No auto-load scripts.\n");
553 /* Wrapper for "info auto-load gdb-scripts". */
556 info_auto_load_gdb_scripts (char *pattern
, int from_tty
)
558 auto_load_info_scripts (pattern
, from_tty
, &script_language_gdb
);
561 /* Implement 'info auto-load local-gdbinit'. */
564 info_auto_load_local_gdbinit (char *args
, int from_tty
)
566 if (auto_load_local_gdbinit_pathname
== NULL
)
567 printf_filtered (_("Local .gdbinit file was not found.\n"));
568 else if (auto_load_local_gdbinit_loaded
)
569 printf_filtered (_("Local .gdbinit file \"%s\" has been loaded.\n"),
570 auto_load_local_gdbinit_pathname
);
572 printf_filtered (_("Local .gdbinit file \"%s\" has not been loaded.\n"),
573 auto_load_local_gdbinit_pathname
);
576 /* Return non-zero if SCRIPT_NOT_FOUND_WARNING_PRINTED of PSPACE_INFO was unset
577 before calling this function. Always set SCRIPT_NOT_FOUND_WARNING_PRINTED
581 script_not_found_warning_print (struct auto_load_pspace_info
*pspace_info
)
583 int retval
= !pspace_info
->script_not_found_warning_printed
;
585 pspace_info
->script_not_found_warning_printed
= 1;
590 /* The only valid "set auto-load" argument is off|0|no|disable. */
593 set_auto_load_cmd (char *args
, int from_tty
)
595 struct cmd_list_element
*list
;
598 /* See parse_binary_operation in use by the sub-commands. */
600 length
= args
? strlen (args
) : 0;
602 while (length
> 0 && (args
[length
- 1] == ' ' || args
[length
- 1] == '\t'))
605 if (length
== 0 || (strncmp (args
, "off", length
) != 0
606 && strncmp (args
, "0", length
) != 0
607 && strncmp (args
, "no", length
) != 0
608 && strncmp (args
, "disable", length
) != 0))
609 error (_("Valid is only global 'set auto-load no'; "
610 "otherwise check the auto-load sub-commands."));
612 for (list
= *auto_load_set_cmdlist_get (); list
!= NULL
; list
= list
->next
)
613 if (list
->var_type
== var_boolean
)
615 gdb_assert (list
->type
== set_cmd
);
616 do_setshow_command (args
, from_tty
, list
);
620 /* Initialize "set auto-load " commands prefix and return it. */
622 struct cmd_list_element
**
623 auto_load_set_cmdlist_get (void)
625 static struct cmd_list_element
*retval
;
628 add_prefix_cmd ("auto-load", class_maintenance
, set_auto_load_cmd
, _("\
629 Auto-loading specific settings.\n\
630 Configure various auto-load-specific variables such as\n\
631 automatic loading of Python scripts."),
632 &retval
, "set auto-load ",
633 1/*allow-unknown*/, &setlist
);
638 /* Command "show auto-load" displays summary of all the current
639 "show auto-load " settings. */
642 show_auto_load_cmd (char *args
, int from_tty
)
644 cmd_show_list (*auto_load_show_cmdlist_get (), from_tty
, "");
647 /* Initialize "show auto-load " commands prefix and return it. */
649 struct cmd_list_element
**
650 auto_load_show_cmdlist_get (void)
652 static struct cmd_list_element
*retval
;
655 add_prefix_cmd ("auto-load", class_maintenance
, show_auto_load_cmd
, _("\
656 Show auto-loading specific settings.\n\
657 Show configuration of various auto-load-specific variables such as\n\
658 automatic loading of Python scripts."),
659 &retval
, "show auto-load ",
660 0/*allow-unknown*/, &showlist
);
665 /* Command "info auto-load" displays whether the various auto-load files have
666 been loaded. This is reimplementation of cmd_show_list which inserts
667 newlines at proper places. */
670 info_auto_load_cmd (char *args
, int from_tty
)
672 struct cmd_list_element
*list
;
673 struct cleanup
*infolist_chain
;
674 struct ui_out
*uiout
= current_uiout
;
676 infolist_chain
= make_cleanup_ui_out_tuple_begin_end (uiout
, "infolist");
678 for (list
= *auto_load_info_cmdlist_get (); list
!= NULL
; list
= list
->next
)
680 struct cleanup
*option_chain
681 = make_cleanup_ui_out_tuple_begin_end (uiout
, "option");
683 gdb_assert (!list
->prefixlist
);
684 gdb_assert (list
->type
== not_set_cmd
);
686 ui_out_field_string (uiout
, "name", list
->name
);
687 ui_out_text (uiout
, ": ");
688 cmd_func (list
, auto_load_info_scripts_pattern_nl
, from_tty
);
690 /* Close the tuple. */
691 do_cleanups (option_chain
);
694 /* Close the tuple. */
695 do_cleanups (infolist_chain
);
698 /* Initialize "info auto-load " commands prefix and return it. */
700 struct cmd_list_element
**
701 auto_load_info_cmdlist_get (void)
703 static struct cmd_list_element
*retval
;
706 add_prefix_cmd ("auto-load", class_info
, info_auto_load_cmd
, _("\
707 Print current status of auto-loaded files.\n\
708 Print whether various files like Python scripts or .gdbinit files have been\n\
709 found and/or loaded."),
710 &retval
, "info auto-load ",
711 0/*allow-unknown*/, &infolist
);
716 void _initialize_auto_load (void);
719 _initialize_auto_load (void)
721 auto_load_pspace_data
722 = register_program_space_data_with_cleanup (auto_load_pspace_data_cleanup
);
724 observer_attach_new_objfile (auto_load_new_objfile
);
726 add_setshow_boolean_cmd ("gdb-scripts", class_support
,
727 &auto_load_gdb_scripts
, _("\
728 Enable or disable auto-loading of canned sequences of commands scripts."), _("\
729 Show whether auto-loading of canned sequences of commands scripts is enabled."),
731 If enabled, canned sequences of commands are loaded when the debugger reads\n\
732 an executable or shared library.\n\
733 This options has security implications for untrusted inferiors."),
734 NULL
, show_auto_load_gdb_scripts
,
735 auto_load_set_cmdlist_get (),
736 auto_load_show_cmdlist_get ());
738 add_cmd ("gdb-scripts", class_info
, info_auto_load_gdb_scripts
,
739 _("Print the list of automatically loaded sequences of commands.\n\
740 Usage: info auto-load gdb-scripts [REGEXP]"),
741 auto_load_info_cmdlist_get ());
743 add_setshow_boolean_cmd ("local-gdbinit", class_support
,
744 &auto_load_local_gdbinit
, _("\
745 Enable or disable auto-loading of .gdbinit script in current directory."), _("\
746 Show whether auto-loading .gdbinit script in current directory is enabled."),
748 If enabled, canned sequences of commands are loaded when debugger starts\n\
749 from .gdbinit file in current directory. Such files are deprecated,\n\
750 use a script associated with inferior executable file instead.\n\
751 This options has security implications for untrusted inferiors."),
752 NULL
, show_auto_load_local_gdbinit
,
753 auto_load_set_cmdlist_get (),
754 auto_load_show_cmdlist_get ());
756 add_cmd ("local-gdbinit", class_info
, info_auto_load_local_gdbinit
,
757 _("Print whether current directory .gdbinit file has been loaded.\n\
758 Usage: info auto-load local-gdbinit"),
759 auto_load_info_cmdlist_get ());