1 /* GDB routines for supporting auto-loaded scripts.
3 Copyright (C) 2010, 2011 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 "filenames.h"
22 #include "gdb_string.h"
23 #include "gdb_regex.h"
25 #include "exceptions.h"
29 #include "progspace.h"
32 #include "cli/cli-cmds.h"
34 /* Internal-use flag to enable/disable auto-loading.
35 This is true if we should auto-load python code when an objfile is opened,
38 Both auto_load_scripts && gdbpy_global_auto_load must be true to enable
41 This flag exists to facilitate deferring auto-loading during start-up
42 until after ./.gdbinit has been read; it may augment the search directories
43 used to find the scripts. */
44 int gdbpy_global_auto_load = 1;
48 #include "python-internal.h"
50 /* NOTE: It's trivial to also support auto-loading normal gdb scripts.
51 There has yet to be a need so it's not implemented. */
53 /* The suffix of per-objfile scripts to auto-load.
54 E.g. When the program loads libfoo.so, look for libfoo-gdb.py. */
55 #define GDBPY_AUTO_FILE_NAME "-gdb.py"
57 /* The section to look for scripts (in file formats that support sections).
58 Each entry in this section is a byte of value 1, and then the nul-terminated
59 name of the script. The script name may include a directory.
60 The leading byte is to allow upward compatible extensions. */
61 #define GDBPY_AUTO_SECTION_NAME ".debug_gdb_scripts"
63 /* For scripts specified in .debug_gdb_scripts, multiple objfiles may load
64 the same script. There's no point in loading the script multiple times,
65 and there can be a lot of objfiles and scripts, so we keep track of scripts
68 struct auto_load_pspace_info
70 /* For each program space we keep track of loaded scripts. */
71 struct htab *loaded_scripts;
73 /* Non-zero if we've issued the warning about an auto-load script not being
74 found. We only want to issue this warning once. */
75 int script_not_found_warning_printed;
78 /* Objects of this type are stored in the loaded script hash table. */
82 /* Name as provided by the objfile. */
84 /* Full path name or NULL if script wasn't found (or was otherwise
86 const char *full_path;
89 /* User-settable option to enable/disable auto-loading:
90 set auto-load-scripts on|off
91 This is true if we should auto-load associated scripts when an objfile
92 is opened, false otherwise.
93 At the moment, this only affects python scripts, but there's no reason
94 one couldn't also have other kinds of auto-loaded scripts, and there's
95 no reason to have them each controlled by a separate flag.
96 So we elide "python" from the name here and in the option.
97 The fact that it lives here is just an implementation detail. */
98 static int auto_load_scripts = 1;
100 /* Per-program-space data key. */
101 static const struct program_space_data *auto_load_pspace_data;
104 auto_load_pspace_data_cleanup (struct program_space *pspace, void *arg)
106 struct auto_load_pspace_info *info;
108 info = program_space_data (pspace, auto_load_pspace_data);
111 if (info->loaded_scripts)
112 htab_delete (info->loaded_scripts);
117 /* Get the current autoload data. If none is found yet, add it now. This
118 function always returns a valid object. */
120 static struct auto_load_pspace_info *
121 get_auto_load_pspace_data (struct program_space *pspace)
123 struct auto_load_pspace_info *info;
125 info = program_space_data (pspace, auto_load_pspace_data);
128 info = XZALLOC (struct auto_load_pspace_info);
129 set_program_space_data (pspace, auto_load_pspace_data, info);
135 /* Hash function for the loaded script hash. */
138 hash_loaded_script_entry (const void *data)
140 const struct loaded_script *e = data;
142 return htab_hash_string (e->name);
145 /* Equality function for the loaded script hash. */
148 eq_loaded_script_entry (const void *a, const void *b)
150 const struct loaded_script *ea = a;
151 const struct loaded_script *eb = b;
153 return strcmp (ea->name, eb->name) == 0;
156 /* Initialize the table to track loaded scripts.
157 Each entry is hashed by the full path name. */
160 init_loaded_scripts_info (struct auto_load_pspace_info *pspace_info)
162 /* Choose 31 as the starting size of the hash table, somewhat arbitrarily.
163 Space for each entry is obtained with one malloc so we can free them
166 pspace_info->loaded_scripts = htab_create (31,
167 hash_loaded_script_entry,
168 eq_loaded_script_entry,
171 pspace_info->script_not_found_warning_printed = FALSE;
174 /* Wrapper on get_auto_load_pspace_data to also allocate the hash table
175 for loading scripts. */
177 static struct auto_load_pspace_info *
178 get_auto_load_pspace_data_for_loading (struct program_space *pspace)
180 struct auto_load_pspace_info *info;
182 info = get_auto_load_pspace_data (pspace);
183 if (info->loaded_scripts == NULL)
184 init_loaded_scripts_info (info);
189 /* Add script NAME to hash table HTAB.
190 FULL_PATH is NULL if the script wasn't found.
191 The result is true if the script was already in the hash table. */
194 maybe_add_script (struct htab *htab, const char *name, const char *full_path)
196 struct loaded_script **slot, entry;
200 entry.full_path = full_path;
201 slot = (struct loaded_script **) htab_find_slot (htab, &entry, INSERT);
202 in_hash_table = *slot != NULL;
204 /* If this script is not in the hash table, add it. */
210 /* Allocate all space in one chunk so it's easier to free. */
211 *slot = xmalloc (sizeof (**slot)
213 + (full_path != NULL ? (strlen (full_path) + 1) : 0));
214 p = ((char*) *slot) + sizeof (**slot);
217 if (full_path != NULL)
220 strcpy (p, full_path);
221 (*slot)->full_path = p;
224 (*slot)->full_path = NULL;
227 return in_hash_table;
230 /* Load scripts specified in OBJFILE.
231 START,END delimit a buffer containing a list of nul-terminated
233 SOURCE_NAME is used in error messages.
235 Scripts are found per normal "source -s" command processing.
236 First the script is looked for in $cwd. If not found there the
237 source search path is used.
239 The section contains a list of path names of files containing
240 python code to load. Each path is null-terminated. */
243 source_section_scripts (struct objfile *objfile, const char *source_name,
244 const char *start, const char *end)
247 struct auto_load_pspace_info *pspace_info;
249 pspace_info = get_auto_load_pspace_data_for_loading (current_program_space);
251 for (p = start; p < end; ++p)
256 int opened, in_hash_table;
260 warning (_("Invalid entry in %s section"), GDBPY_AUTO_SECTION_NAME);
261 /* We could try various heuristics to find the next valid entry,
262 but it's safer to just punt. */
267 while (p < end && *p != '\0')
271 char *buf = alloca (p - file + 1);
273 memcpy (buf, file, p - file);
274 buf[p - file] = '\0';
275 warning (_("Non-null-terminated path in %s: %s"),
282 warning (_("Empty path in %s"), source_name);
286 opened = find_and_open_script (file, 1 /*search_path*/,
287 &stream, &full_path);
289 /* If one script isn't found it's not uncommon for more to not be
290 found either. We don't want to print an error message for each
291 script, too much noise. Instead, we print the warning once and tell
292 the user how to find the list of scripts that weren't loaded.
294 IWBN if complaints.c were more general-purpose. */
296 in_hash_table = maybe_add_script (pspace_info->loaded_scripts, file,
297 opened ? full_path : NULL);
304 /* We don't throw an error, the program is still debuggable. */
305 if (! pspace_info->script_not_found_warning_printed)
307 warning (_("Missing auto-load scripts referenced in %s.\n\
308 Use `info auto-load-scripts [REGEXP]' to list them."),
309 GDBPY_AUTO_SECTION_NAME);
310 pspace_info->script_not_found_warning_printed = TRUE;
315 /* If this file is not currently loaded, load it. */
317 source_python_script_for_objfile (objfile, stream, file);
321 /* Load scripts specified in section SECTION_NAME of OBJFILE. */
324 auto_load_section_scripts (struct objfile *objfile, const char *section_name)
326 bfd *abfd = objfile->obfd;
327 asection *scripts_sect;
330 struct cleanup *cleanups;
332 scripts_sect = bfd_get_section_by_name (abfd, section_name);
333 if (scripts_sect == NULL)
336 size = bfd_get_section_size (scripts_sect);
339 cleanups = make_cleanup (xfree, p);
341 if (bfd_get_section_contents (abfd, scripts_sect, p, (file_ptr) 0, size))
342 source_section_scripts (objfile, section_name, p, p + size);
344 warning (_("Couldn't read %s section of %s"),
345 section_name, bfd_get_filename (abfd));
347 do_cleanups (cleanups);
350 /* Clear the table of loaded section scripts. */
353 clear_section_scripts (void)
355 struct program_space *pspace = current_program_space;
356 struct auto_load_pspace_info *info;
358 info = program_space_data (pspace, auto_load_pspace_data);
359 if (info != NULL && info->loaded_scripts != NULL)
361 htab_delete (info->loaded_scripts);
362 info->loaded_scripts = NULL;
363 info->script_not_found_warning_printed = FALSE;
367 /* Look for the auto-load script associated with OBJFILE and load it. */
370 auto_load_objfile_script (struct objfile *objfile, const char *suffix)
373 char *filename, *debugfile;
376 struct cleanup *cleanups;
378 realname = gdb_realpath (objfile->name);
379 len = strlen (realname);
380 filename = xmalloc (len + strlen (suffix) + 1);
381 memcpy (filename, realname, len);
382 strcpy (filename + len, suffix);
384 cleanups = make_cleanup (xfree, filename);
385 make_cleanup (xfree, realname);
387 input = fopen (filename, "r");
388 debugfile = filename;
390 if (!input && debug_file_directory)
392 /* Also try the same file in the separate debug info directory. */
393 debugfile = xmalloc (strlen (filename)
394 + strlen (debug_file_directory) + 1);
395 strcpy (debugfile, debug_file_directory);
396 /* FILENAME is absolute, so we don't need a "/" here. */
397 strcat (debugfile, filename);
399 make_cleanup (xfree, debugfile);
400 input = fopen (debugfile, "r");
403 if (!input && gdb_datadir)
405 /* Also try the same file in a subdirectory of gdb's data
407 debugfile = xmalloc (strlen (gdb_datadir) + strlen (filename)
408 + strlen ("/auto-load") + 1);
409 strcpy (debugfile, gdb_datadir);
410 strcat (debugfile, "/auto-load");
411 /* FILENAME is absolute, so we don't need a "/" here. */
412 strcat (debugfile, filename);
414 make_cleanup (xfree, debugfile);
415 input = fopen (debugfile, "r");
420 struct auto_load_pspace_info *pspace_info;
422 /* Add this script to the hash table too so "info auto-load-scripts"
425 get_auto_load_pspace_data_for_loading (current_program_space);
426 maybe_add_script (pspace_info->loaded_scripts, debugfile, debugfile);
428 /* To preserve existing behaviour we don't check for whether the
429 script was already in the table, and always load it.
430 It's highly unlikely that we'd ever load it twice,
431 and these scripts are required to be idempotent under multiple
433 source_python_script_for_objfile (objfile, input, debugfile);
437 do_cleanups (cleanups);
440 /* This is a new_objfile observer callback to auto-load scripts.
442 Two flavors of auto-loaded scripts are supported.
443 1) based on the path to the objfile
444 2) from .debug_gdb_scripts section */
447 auto_load_new_objfile (struct objfile *objfile)
451 /* OBJFILE is NULL when loading a new "main" symbol-file. */
452 clear_section_scripts ();
456 load_auto_scripts_for_objfile (objfile);
459 /* Load any auto-loaded scripts for OBJFILE. */
462 load_auto_scripts_for_objfile (struct objfile *objfile)
464 if (auto_load_scripts && gdbpy_global_auto_load)
466 auto_load_objfile_script (objfile, GDBPY_AUTO_FILE_NAME);
467 auto_load_section_scripts (objfile, GDBPY_AUTO_SECTION_NAME);
471 /* Collect scripts to be printed in a vec. */
473 typedef struct loaded_script *loaded_script_ptr;
474 DEF_VEC_P (loaded_script_ptr);
476 /* Traversal function for htab_traverse.
477 Collect the entry if it matches the regexp. */
480 collect_matching_scripts (void **slot, void *info)
482 struct loaded_script *script = *slot;
483 VEC (loaded_script_ptr) *scripts = info;
485 if (re_exec (script->name))
486 VEC_safe_push (loaded_script_ptr, scripts, script);
494 print_script (struct loaded_script *script)
496 struct cleanup *chain;
498 chain = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
500 ui_out_field_string (uiout, "loaded", script->full_path ? "Yes" : "Missing");
501 ui_out_field_string (uiout, "script", script->name);
502 ui_out_text (uiout, "\n");
504 /* If the name isn't the full path, print it too. */
505 if (script->full_path != NULL
506 && strcmp (script->name, script->full_path) != 0)
508 ui_out_text (uiout, "\tfull name: ");
509 ui_out_field_string (uiout, "full_path", script->full_path);
510 ui_out_text (uiout, "\n");
516 /* Helper for info_auto_load_scripts to sort the scripts by name. */
519 sort_scripts_by_name (const void *ap, const void *bp)
521 const struct loaded_script *a = *(const struct loaded_script **) ap;
522 const struct loaded_script *b = *(const struct loaded_script **) bp;
524 return FILENAME_CMP (a->name, b->name);
527 /* "info auto-load-scripts" command. */
530 info_auto_load_scripts (char *pattern, int from_tty)
532 struct auto_load_pspace_info *pspace_info;
533 struct cleanup *script_chain;
534 VEC (loaded_script_ptr) *scripts;
539 pspace_info = get_auto_load_pspace_data (current_program_space);
541 if (pattern && *pattern)
543 char *re_err = re_comp (pattern);
546 error (_("Invalid regexp: %s"), re_err);
553 /* We need to know the number of rows before we build the table.
554 Plus we want to sort the scripts by name.
555 So first traverse the hash table collecting the matching scripts. */
557 scripts = VEC_alloc (loaded_script_ptr, 10);
558 script_chain = make_cleanup (VEC_cleanup (loaded_script_ptr), &scripts);
560 if (pspace_info != NULL && pspace_info->loaded_scripts != NULL)
563 htab_traverse_noresize (pspace_info->loaded_scripts,
564 collect_matching_scripts, scripts);
568 nr_scripts = VEC_length (loaded_script_ptr, scripts);
569 make_cleanup_ui_out_table_begin_end (uiout, 2, nr_scripts,
570 "AutoLoadedScriptsTable");
572 ui_out_table_header (uiout, 7, ui_left, "loaded", "Loaded");
573 ui_out_table_header (uiout, 70, ui_left, "script", "Script");
574 ui_out_table_body (uiout);
579 loaded_script_ptr script;
581 qsort (VEC_address (loaded_script_ptr, scripts),
582 VEC_length (loaded_script_ptr, scripts),
583 sizeof (loaded_script_ptr), sort_scripts_by_name);
584 for (i = 0; VEC_iterate (loaded_script_ptr, scripts, i, script); ++i)
585 print_script (script);
588 do_cleanups (script_chain);
592 if (pattern && *pattern)
593 ui_out_message (uiout, 0, "No auto-load scripts matching %s.\n",
596 ui_out_message (uiout, 0, "No auto-load scripts.\n");
601 gdbpy_initialize_auto_load (void)
603 auto_load_pspace_data
604 = register_program_space_data_with_cleanup (auto_load_pspace_data_cleanup);
606 observer_attach_new_objfile (auto_load_new_objfile);
608 add_setshow_boolean_cmd ("auto-load-scripts", class_support,
609 &auto_load_scripts, _("\
610 Set the debugger's behaviour regarding auto-loaded scripts."), _("\
611 Show the debugger's behaviour regarding auto-loaded scripts."), _("\
612 If enabled, auto-loaded scripts are loaded when the debugger reads\n\
613 an executable or shared library."),
618 add_info ("auto-load-scripts",
619 info_auto_load_scripts,
620 _("Print the list of automatically loaded scripts.\n\
621 Usage: info auto-load-scripts [REGEXP]"));
624 #else /* ! HAVE_PYTHON */
627 load_auto_scripts_for_objfile (struct objfile *objfile)
631 #endif /* ! HAVE_PYTHON */