1 /* GDB routines for supporting auto-loaded scripts.
3 Copyright (C) 2010 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 "gdb_string.h"
22 #include "gdb_regex.h"
24 #include "exceptions.h"
28 #include "progspace.h"
31 #include "cli/cli-cmds.h"
33 /* Internal-use flag to enable/disable auto-loading.
34 This is true if we should auto-load python code when an objfile is opened,
37 Both gdbpy_auto_load && gdbpy_global_auto_load must be true to enable
40 This flag exists to facilitate deferring auto-loading during start-up
41 until after ./.gdbinit has been read; it may augment the search directories
42 used to find the scripts. */
43 int gdbpy_global_auto_load
= 1;
47 #include "python-internal.h"
49 /* NOTE: It's trivial to also support auto-loading normal gdb scripts.
50 There has yet to be a need so it's not implemented. */
52 /* The suffix of per-objfile scripts to auto-load.
53 E.g. When the program loads libfoo.so, look for libfoo-gdb.py. */
54 #define GDBPY_AUTO_FILE_NAME "-gdb.py"
56 /* The section to look for scripts (in file formats that support sections).
57 Each entry in this section is a byte of value 1, and then the nul-terminated
58 name of the script. The script name may include a directory.
59 The leading byte is to allow upward compatible extensions. */
60 #define GDBPY_AUTO_SECTION_NAME ".debug_gdb_scripts"
62 /* For scripts specified in .debug_gdb_scripts, multiple objfiles may load
63 the same script. There's no point in loading the script multiple times,
64 and there can be a lot of objfiles and scripts, so we keep track of scripts
67 struct auto_load_pspace_info
69 /* For each program space we keep track of loaded scripts. */
70 struct htab
*loaded_scripts
;
73 /* Objects of this type are stored in the loaded script hash table. */
75 struct loaded_script_entry
77 /* Name as provided by the objfile. */
79 /* Full path name or NULL if script wasn't found (or was otherwise
81 const char *full_path
;
84 /* User-settable option to enable/disable auto-loading:
85 maint set python auto-load on|off
86 This is true if we should auto-load python code when an objfile is opened,
88 static int gdbpy_auto_load
= 1;
90 /* Per-program-space data key. */
91 static const struct program_space_data
*auto_load_pspace_data
;
94 auto_load_pspace_data_cleanup (struct program_space
*pspace
, void *arg
)
96 struct auto_load_pspace_info
*info
;
98 info
= program_space_data (pspace
, auto_load_pspace_data
);
101 if (info
->loaded_scripts
)
102 htab_delete (info
->loaded_scripts
);
107 /* Get the current autoload data. If none is found yet, add it now. This
108 function always returns a valid object. */
110 static struct auto_load_pspace_info
*
111 get_auto_load_pspace_data (struct program_space
*pspace
)
113 struct auto_load_pspace_info
*info
;
115 info
= program_space_data (pspace
, auto_load_pspace_data
);
118 info
= XZALLOC (struct auto_load_pspace_info
);
119 set_program_space_data (pspace
, auto_load_pspace_data
, info
);
125 /* Hash function for the loaded script hash. */
128 hash_loaded_script_entry (const void *data
)
130 const struct loaded_script_entry
*e
= data
;
132 return htab_hash_string (e
->name
);
135 /* Equality function for the loaded script hash. */
138 eq_loaded_script_entry (const void *a
, const void *b
)
140 const struct loaded_script_entry
*ea
= a
;
141 const struct loaded_script_entry
*eb
= b
;
143 return strcmp (ea
->name
, eb
->name
) == 0;
146 /* Create the hash table used for loaded scripts.
147 Each entry is hashed by the full path name. */
150 create_loaded_scripts_hash (struct auto_load_pspace_info
*pspace_info
)
152 /* Choose 31 as the starting size of the hash table, somewhat arbitrarily.
153 Space for each entry is obtained with one malloc so we can free them
156 pspace_info
->loaded_scripts
= htab_create (31,
157 hash_loaded_script_entry
,
158 eq_loaded_script_entry
,
162 /* Load scripts specified in OBJFILE.
163 START,END delimit a buffer containing a list of nul-terminated
165 SOURCE_NAME is used in error messages.
167 Scripts are found per normal "source -s" command processing.
168 First the script is looked for in $cwd. If not found there the
169 source search path is used.
171 The section contains a list of path names of files containing
172 python code to load. Each path is null-terminated. */
175 source_section_scripts (struct objfile
*objfile
, const char *source_name
,
176 const char *start
, const char *end
)
179 struct auto_load_pspace_info
*pspace_info
;
180 struct loaded_script_entry
**slot
, entry
;
182 pspace_info
= get_auto_load_pspace_data (current_program_space
);
183 if (pspace_info
->loaded_scripts
== NULL
)
184 create_loaded_scripts_hash (pspace_info
);
186 for (p
= start
; p
< end
; ++p
)
191 int opened
, in_hash_table
;
195 warning (_("Invalid entry in %s section"), GDBPY_AUTO_SECTION_NAME
);
196 /* We could try various heuristics to find the next valid entry,
197 but it's safer to just punt. */
202 while (p
< end
&& *p
!= '\0')
206 char *buf
= alloca (p
- file
+ 1);
208 memcpy (buf
, file
, p
- file
);
209 buf
[p
- file
] = '\0';
210 warning (_("Non-null-terminated path in %s: %s"),
217 warning (_("Empty path in %s"), source_name
);
221 opened
= find_and_open_script (file
, 1 /*search_path*/,
222 &stream
, &full_path
);
224 /* If the file is not found, we still record the file in the hash table,
225 we only want to print an error message once.
226 IWBN if complaints.c were more general-purpose. */
230 entry
.full_path
= full_path
;
232 entry
.full_path
= NULL
;
233 slot
= ((struct loaded_script_entry
**)
234 htab_find_slot (pspace_info
->loaded_scripts
,
236 in_hash_table
= *slot
!= NULL
;
238 /* If this file is not in the hash table, add it. */
243 *slot
= xmalloc (sizeof (**slot
)
245 + (opened
? (strlen (full_path
) + 1) : 0));
246 p
= ((char*) *slot
) + sizeof (**slot
);
252 strcpy (p
, full_path
);
253 (*slot
)->full_path
= p
;
256 (*slot
)->full_path
= NULL
;
264 /* We don't throw an error, the program is still debuggable.
265 Check in_hash_table to only print the warning once. */
267 warning (_("%s (referenced in %s): %s"),
268 file
, GDBPY_AUTO_SECTION_NAME
, safe_strerror (errno
));
272 /* If this file is not currently loaded, load it. */
274 source_python_script_for_objfile (objfile
, stream
, file
);
278 /* Load scripts specified in section SECTION_NAME of OBJFILE. */
281 auto_load_section_scripts (struct objfile
*objfile
, const char *section_name
)
283 bfd
*abfd
= objfile
->obfd
;
284 asection
*scripts_sect
;
287 struct cleanup
*cleanups
;
289 scripts_sect
= bfd_get_section_by_name (abfd
, section_name
);
290 if (scripts_sect
== NULL
)
293 size
= bfd_get_section_size (scripts_sect
);
296 cleanups
= make_cleanup (xfree
, p
);
298 if (bfd_get_section_contents (abfd
, scripts_sect
, p
, (file_ptr
) 0, size
))
299 source_section_scripts (objfile
, section_name
, p
, p
+ size
);
301 warning (_("Couldn't read %s section of %s"),
302 section_name
, bfd_get_filename (abfd
));
304 do_cleanups (cleanups
);
307 /* Clear the table of loaded section scripts. */
310 clear_section_scripts (void)
312 struct program_space
*pspace
= current_program_space
;
313 struct auto_load_pspace_info
*info
;
315 info
= program_space_data (pspace
, auto_load_pspace_data
);
316 if (info
!= NULL
&& info
->loaded_scripts
!= NULL
)
318 htab_delete (info
->loaded_scripts
);
319 info
->loaded_scripts
= NULL
;
323 /* Look for the auto-load script associated with OBJFILE and load it. */
326 auto_load_objfile_script (struct objfile
*objfile
, const char *suffix
)
329 char *filename
, *debugfile
;
332 struct cleanup
*cleanups
;
334 realname
= gdb_realpath (objfile
->name
);
335 len
= strlen (realname
);
336 filename
= xmalloc (len
+ strlen (suffix
) + 1);
337 memcpy (filename
, realname
, len
);
338 strcpy (filename
+ len
, suffix
);
340 cleanups
= make_cleanup (xfree
, filename
);
341 make_cleanup (xfree
, realname
);
343 input
= fopen (filename
, "r");
344 debugfile
= filename
;
346 if (!input
&& debug_file_directory
)
348 /* Also try the same file in the separate debug info directory. */
349 debugfile
= xmalloc (strlen (filename
)
350 + strlen (debug_file_directory
) + 1);
351 strcpy (debugfile
, debug_file_directory
);
352 /* FILENAME is absolute, so we don't need a "/" here. */
353 strcat (debugfile
, filename
);
355 make_cleanup (xfree
, debugfile
);
356 input
= fopen (debugfile
, "r");
359 if (!input
&& gdb_datadir
)
361 /* Also try the same file in a subdirectory of gdb's data
363 debugfile
= xmalloc (strlen (gdb_datadir
) + strlen (filename
)
364 + strlen ("/auto-load") + 1);
365 strcpy (debugfile
, gdb_datadir
);
366 strcat (debugfile
, "/auto-load");
367 /* FILENAME is absolute, so we don't need a "/" here. */
368 strcat (debugfile
, filename
);
370 make_cleanup (xfree
, debugfile
);
371 input
= fopen (debugfile
, "r");
376 source_python_script_for_objfile (objfile
, input
, debugfile
);
380 do_cleanups (cleanups
);
383 /* This is a new_objfile observer callback to auto-load scripts.
385 Two flavors of auto-loaded scripts are supported.
386 1) based on the path to the objfile
387 2) from .debug_gdb_scripts section */
390 auto_load_new_objfile (struct objfile
*objfile
)
394 /* OBJFILE is NULL when loading a new "main" symbol-file. */
395 clear_section_scripts ();
399 load_auto_scripts_for_objfile (objfile
);
402 /* Load any auto-loaded scripts for OBJFILE. */
405 load_auto_scripts_for_objfile (struct objfile
*objfile
)
407 if (gdbpy_auto_load
&& gdbpy_global_auto_load
)
409 auto_load_objfile_script (objfile
, GDBPY_AUTO_FILE_NAME
);
410 auto_load_section_scripts (objfile
, GDBPY_AUTO_SECTION_NAME
);
414 /* Traversal function for htab_traverse.
415 Print the entry if specified in the regex. */
418 maybe_print_section_script (void **slot
, void *info
)
420 struct loaded_script_entry
*entry
= *slot
;
422 if (re_exec (entry
->name
))
424 printf_filtered (_("Script name: %s\n"), entry
->name
);
425 printf_filtered (_(" Full name: %s\n"),
426 entry
->full_path
? entry
->full_path
: _("unknown"));
432 /* "maint print section-scripts" command. */
435 maintenance_print_section_scripts (char *pattern
, int from_tty
)
437 struct auto_load_pspace_info
*pspace_info
;
441 if (pattern
&& *pattern
)
443 char *re_err
= re_comp (pattern
);
446 error (_("Invalid regexp: %s"), re_err
);
448 printf_filtered (_("Objfile scripts matching %s:\n"), pattern
);
453 printf_filtered (_("Objfile scripts:\n"));
456 pspace_info
= get_auto_load_pspace_data (current_program_space
);
457 if (pspace_info
== NULL
|| pspace_info
->loaded_scripts
== NULL
)
461 htab_traverse_noresize (pspace_info
->loaded_scripts
,
462 maybe_print_section_script
, NULL
);
467 gdbpy_initialize_auto_load (void)
469 auto_load_pspace_data
470 = register_program_space_data_with_cleanup (auto_load_pspace_data_cleanup
);
472 observer_attach_new_objfile (auto_load_new_objfile
);
474 add_setshow_boolean_cmd ("auto-load", class_maintenance
,
475 &gdbpy_auto_load
, _("\
476 Enable or disable auto-loading of Python code when an object is opened."), _("\
477 Show whether Python code will be auto-loaded when an object is opened."), _("\
478 Enables or disables auto-loading of Python code when an object is opened."),
483 add_cmd ("section-scripts", class_maintenance
, maintenance_print_section_scripts
,
484 _("Print dump of auto-loaded section scripts matching REGEXP."),
485 &maintenanceprintlist
);
488 #else /* ! HAVE_PYTHON */
491 load_auto_scripts_for_objfile (struct objfile
*objfile
)
495 #endif /* ! HAVE_PYTHON */