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
);
301 /* We don't throw an error, the program is still debuggable. */
302 if (! pspace_info
->script_not_found_warning_printed
)
304 warning (_("Missing auto-load scripts referenced in section %s\n\
306 Use `info auto-load-scripts [REGEXP]' to list them."),
307 GDBPY_AUTO_SECTION_NAME
, objfile
->name
);
308 pspace_info
->script_not_found_warning_printed
= TRUE
;
313 /* If this file is not currently loaded, load it. */
315 source_python_script_for_objfile (objfile
, stream
, file
);
322 /* Load scripts specified in section SECTION_NAME of OBJFILE. */
325 auto_load_section_scripts (struct objfile
*objfile
, const char *section_name
)
327 bfd
*abfd
= objfile
->obfd
;
328 asection
*scripts_sect
;
331 struct cleanup
*cleanups
;
333 scripts_sect
= bfd_get_section_by_name (abfd
, section_name
);
334 if (scripts_sect
== NULL
)
337 size
= bfd_get_section_size (scripts_sect
);
340 cleanups
= make_cleanup (xfree
, p
);
342 if (bfd_get_section_contents (abfd
, scripts_sect
, p
, (file_ptr
) 0, size
))
343 source_section_scripts (objfile
, section_name
, p
, p
+ size
);
345 warning (_("Couldn't read %s section of %s"),
346 section_name
, bfd_get_filename (abfd
));
348 do_cleanups (cleanups
);
351 /* Clear the table of loaded section scripts. */
354 clear_section_scripts (void)
356 struct program_space
*pspace
= current_program_space
;
357 struct auto_load_pspace_info
*info
;
359 info
= program_space_data (pspace
, auto_load_pspace_data
);
360 if (info
!= NULL
&& info
->loaded_scripts
!= NULL
)
362 htab_delete (info
->loaded_scripts
);
363 info
->loaded_scripts
= NULL
;
364 info
->script_not_found_warning_printed
= FALSE
;
368 /* Look for the auto-load script associated with OBJFILE and load it. */
371 auto_load_objfile_script (struct objfile
*objfile
, const char *suffix
)
374 char *filename
, *debugfile
;
377 struct cleanup
*cleanups
;
379 realname
= gdb_realpath (objfile
->name
);
380 len
= strlen (realname
);
381 filename
= xmalloc (len
+ strlen (suffix
) + 1);
382 memcpy (filename
, realname
, len
);
383 strcpy (filename
+ len
, suffix
);
385 cleanups
= make_cleanup (xfree
, filename
);
386 make_cleanup (xfree
, realname
);
388 input
= fopen (filename
, "r");
389 debugfile
= filename
;
391 if (!input
&& debug_file_directory
)
393 /* Also try the same file in the separate debug info directory. */
394 debugfile
= xmalloc (strlen (filename
)
395 + strlen (debug_file_directory
) + 1);
396 strcpy (debugfile
, debug_file_directory
);
397 /* FILENAME is absolute, so we don't need a "/" here. */
398 strcat (debugfile
, filename
);
400 make_cleanup (xfree
, debugfile
);
401 input
= fopen (debugfile
, "r");
404 if (!input
&& gdb_datadir
)
406 /* Also try the same file in a subdirectory of gdb's data
408 debugfile
= xmalloc (strlen (gdb_datadir
) + strlen (filename
)
409 + strlen ("/auto-load") + 1);
410 strcpy (debugfile
, gdb_datadir
);
411 strcat (debugfile
, "/auto-load");
412 /* FILENAME is absolute, so we don't need a "/" here. */
413 strcat (debugfile
, filename
);
415 make_cleanup (xfree
, debugfile
);
416 input
= fopen (debugfile
, "r");
421 struct auto_load_pspace_info
*pspace_info
;
423 /* Add this script to the hash table too so "info auto-load-scripts"
426 get_auto_load_pspace_data_for_loading (current_program_space
);
427 maybe_add_script (pspace_info
->loaded_scripts
, debugfile
, debugfile
);
429 /* To preserve existing behaviour we don't check for whether the
430 script was already in the table, and always load it.
431 It's highly unlikely that we'd ever load it twice,
432 and these scripts are required to be idempotent under multiple
434 source_python_script_for_objfile (objfile
, input
, debugfile
);
438 do_cleanups (cleanups
);
441 /* This is a new_objfile observer callback to auto-load scripts.
443 Two flavors of auto-loaded scripts are supported.
444 1) based on the path to the objfile
445 2) from .debug_gdb_scripts section */
448 auto_load_new_objfile (struct objfile
*objfile
)
452 /* OBJFILE is NULL when loading a new "main" symbol-file. */
453 clear_section_scripts ();
457 load_auto_scripts_for_objfile (objfile
);
460 /* Load any auto-loaded scripts for OBJFILE. */
463 load_auto_scripts_for_objfile (struct objfile
*objfile
)
465 if (auto_load_scripts
&& gdbpy_global_auto_load
)
467 auto_load_objfile_script (objfile
, GDBPY_AUTO_FILE_NAME
);
468 auto_load_section_scripts (objfile
, GDBPY_AUTO_SECTION_NAME
);
472 /* Collect scripts to be printed in a vec. */
474 typedef struct loaded_script
*loaded_script_ptr
;
475 DEF_VEC_P (loaded_script_ptr
);
477 /* Traversal function for htab_traverse.
478 Collect the entry if it matches the regexp. */
481 collect_matching_scripts (void **slot
, void *info
)
483 struct loaded_script
*script
= *slot
;
484 VEC (loaded_script_ptr
) *scripts
= info
;
486 if (re_exec (script
->name
))
487 VEC_safe_push (loaded_script_ptr
, scripts
, script
);
495 print_script (struct loaded_script
*script
)
497 struct ui_out
*uiout
= current_uiout
;
498 struct cleanup
*chain
;
500 chain
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
502 ui_out_field_string (uiout
, "loaded", script
->full_path
? "Yes" : "Missing");
503 ui_out_field_string (uiout
, "script", script
->name
);
504 ui_out_text (uiout
, "\n");
506 /* If the name isn't the full path, print it too. */
507 if (script
->full_path
!= NULL
508 && strcmp (script
->name
, script
->full_path
) != 0)
510 ui_out_text (uiout
, "\tfull name: ");
511 ui_out_field_string (uiout
, "full_path", script
->full_path
);
512 ui_out_text (uiout
, "\n");
518 /* Helper for info_auto_load_scripts to sort the scripts by name. */
521 sort_scripts_by_name (const void *ap
, const void *bp
)
523 const struct loaded_script
*a
= *(const struct loaded_script
**) ap
;
524 const struct loaded_script
*b
= *(const struct loaded_script
**) bp
;
526 return FILENAME_CMP (a
->name
, b
->name
);
529 /* "info auto-load-scripts" command. */
532 info_auto_load_scripts (char *pattern
, int from_tty
)
534 struct ui_out
*uiout
= current_uiout
;
535 struct auto_load_pspace_info
*pspace_info
;
536 struct cleanup
*script_chain
;
537 VEC (loaded_script_ptr
) *scripts
;
542 pspace_info
= get_auto_load_pspace_data (current_program_space
);
544 if (pattern
&& *pattern
)
546 char *re_err
= re_comp (pattern
);
549 error (_("Invalid regexp: %s"), re_err
);
556 /* We need to know the number of rows before we build the table.
557 Plus we want to sort the scripts by name.
558 So first traverse the hash table collecting the matching scripts. */
560 scripts
= VEC_alloc (loaded_script_ptr
, 10);
561 script_chain
= make_cleanup (VEC_cleanup (loaded_script_ptr
), &scripts
);
563 if (pspace_info
!= NULL
&& pspace_info
->loaded_scripts
!= NULL
)
566 htab_traverse_noresize (pspace_info
->loaded_scripts
,
567 collect_matching_scripts
, scripts
);
571 nr_scripts
= VEC_length (loaded_script_ptr
, scripts
);
572 make_cleanup_ui_out_table_begin_end (uiout
, 2, nr_scripts
,
573 "AutoLoadedScriptsTable");
575 ui_out_table_header (uiout
, 7, ui_left
, "loaded", "Loaded");
576 ui_out_table_header (uiout
, 70, ui_left
, "script", "Script");
577 ui_out_table_body (uiout
);
582 loaded_script_ptr script
;
584 qsort (VEC_address (loaded_script_ptr
, scripts
),
585 VEC_length (loaded_script_ptr
, scripts
),
586 sizeof (loaded_script_ptr
), sort_scripts_by_name
);
587 for (i
= 0; VEC_iterate (loaded_script_ptr
, scripts
, i
, script
); ++i
)
588 print_script (script
);
591 do_cleanups (script_chain
);
595 if (pattern
&& *pattern
)
596 ui_out_message (uiout
, 0, "No auto-load scripts matching %s.\n",
599 ui_out_message (uiout
, 0, "No auto-load scripts.\n");
604 gdbpy_initialize_auto_load (void)
606 auto_load_pspace_data
607 = register_program_space_data_with_cleanup (auto_load_pspace_data_cleanup
);
609 observer_attach_new_objfile (auto_load_new_objfile
);
611 add_setshow_boolean_cmd ("auto-load-scripts", class_support
,
612 &auto_load_scripts
, _("\
613 Set the debugger's behaviour regarding auto-loaded scripts."), _("\
614 Show the debugger's behaviour regarding auto-loaded scripts."), _("\
615 If enabled, auto-loaded scripts are loaded when the debugger reads\n\
616 an executable or shared library."),
621 add_info ("auto-load-scripts",
622 info_auto_load_scripts
,
623 _("Print the list of automatically loaded scripts.\n\
624 Usage: info auto-load-scripts [REGEXP]"));
627 #else /* ! HAVE_PYTHON */
630 load_auto_scripts_for_objfile (struct objfile
*objfile
)
634 #endif /* ! HAVE_PYTHON */