]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/python/py-auto-load.c
* python/py-autoload.c (print_script): Print "Missing" instead of
[thirdparty/binutils-gdb.git] / gdb / python / py-auto-load.c
1 /* GDB routines for supporting auto-loaded scripts.
2
3 Copyright (C) 2010, 2011 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 "filenames.h"
22 #include "gdb_string.h"
23 #include "gdb_regex.h"
24 #include "top.h"
25 #include "exceptions.h"
26 #include "command.h"
27 #include "gdbcmd.h"
28 #include "observer.h"
29 #include "progspace.h"
30 #include "objfiles.h"
31 #include "python.h"
32 #include "cli/cli-cmds.h"
33
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,
36 false otherwise.
37
38 Both auto_load_scripts && gdbpy_global_auto_load must be true to enable
39 auto-loading.
40
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;
45
46 #ifdef HAVE_PYTHON
47
48 #include "python-internal.h"
49
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. */
52
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"
56
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"
62
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
66 loaded this way. */
67
68 struct auto_load_pspace_info
69 {
70 /* For each program space we keep track of loaded scripts. */
71 struct htab *loaded_scripts;
72
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;
76 };
77
78 /* Objects of this type are stored in the loaded script hash table. */
79
80 struct loaded_script
81 {
82 /* Name as provided by the objfile. */
83 const char *name;
84 /* Full path name or NULL if script wasn't found (or was otherwise
85 inaccessible). */
86 const char *full_path;
87 };
88
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;
99
100 /* Per-program-space data key. */
101 static const struct program_space_data *auto_load_pspace_data;
102
103 static void
104 auto_load_pspace_data_cleanup (struct program_space *pspace, void *arg)
105 {
106 struct auto_load_pspace_info *info;
107
108 info = program_space_data (pspace, auto_load_pspace_data);
109 if (info != NULL)
110 {
111 if (info->loaded_scripts)
112 htab_delete (info->loaded_scripts);
113 xfree (info);
114 }
115 }
116
117 /* Get the current autoload data. If none is found yet, add it now. This
118 function always returns a valid object. */
119
120 static struct auto_load_pspace_info *
121 get_auto_load_pspace_data (struct program_space *pspace)
122 {
123 struct auto_load_pspace_info *info;
124
125 info = program_space_data (pspace, auto_load_pspace_data);
126 if (info == NULL)
127 {
128 info = XZALLOC (struct auto_load_pspace_info);
129 set_program_space_data (pspace, auto_load_pspace_data, info);
130 }
131
132 return info;
133 }
134
135 /* Hash function for the loaded script hash. */
136
137 static hashval_t
138 hash_loaded_script_entry (const void *data)
139 {
140 const struct loaded_script *e = data;
141
142 return htab_hash_string (e->name);
143 }
144
145 /* Equality function for the loaded script hash. */
146
147 static int
148 eq_loaded_script_entry (const void *a, const void *b)
149 {
150 const struct loaded_script *ea = a;
151 const struct loaded_script *eb = b;
152
153 return strcmp (ea->name, eb->name) == 0;
154 }
155
156 /* Initialize the table to track loaded scripts.
157 Each entry is hashed by the full path name. */
158
159 static void
160 init_loaded_scripts_info (struct auto_load_pspace_info *pspace_info)
161 {
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
164 easily. */
165
166 pspace_info->loaded_scripts = htab_create (31,
167 hash_loaded_script_entry,
168 eq_loaded_script_entry,
169 xfree);
170
171 pspace_info->script_not_found_warning_printed = FALSE;
172 }
173
174 /* Wrapper on get_auto_load_pspace_data to also allocate the hash table
175 for loading scripts. */
176
177 static struct auto_load_pspace_info *
178 get_auto_load_pspace_data_for_loading (struct program_space *pspace)
179 {
180 struct auto_load_pspace_info *info;
181
182 info = get_auto_load_pspace_data (pspace);
183 if (info->loaded_scripts == NULL)
184 init_loaded_scripts_info (info);
185
186 return info;
187 }
188
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. */
192
193 static int
194 maybe_add_script (struct htab *htab, const char *name, const char *full_path)
195 {
196 struct loaded_script **slot, entry;
197 int in_hash_table;
198
199 entry.name = name;
200 entry.full_path = full_path;
201 slot = (struct loaded_script **) htab_find_slot (htab, &entry, INSERT);
202 in_hash_table = *slot != NULL;
203
204 /* If this script is not in the hash table, add it. */
205
206 if (! in_hash_table)
207 {
208 char *p;
209
210 /* Allocate all space in one chunk so it's easier to free. */
211 *slot = xmalloc (sizeof (**slot)
212 + strlen (name) + 1
213 + (full_path != NULL ? (strlen (full_path) + 1) : 0));
214 p = ((char*) *slot) + sizeof (**slot);
215 strcpy (p, name);
216 (*slot)->name = p;
217 if (full_path != NULL)
218 {
219 p += strlen (p) + 1;
220 strcpy (p, full_path);
221 (*slot)->full_path = p;
222 }
223 else
224 (*slot)->full_path = NULL;
225 }
226
227 return in_hash_table;
228 }
229
230 /* Load scripts specified in OBJFILE.
231 START,END delimit a buffer containing a list of nul-terminated
232 file names.
233 SOURCE_NAME is used in error messages.
234
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.
238
239 The section contains a list of path names of files containing
240 python code to load. Each path is null-terminated. */
241
242 static void
243 source_section_scripts (struct objfile *objfile, const char *source_name,
244 const char *start, const char *end)
245 {
246 const char *p;
247 struct auto_load_pspace_info *pspace_info;
248
249 pspace_info = get_auto_load_pspace_data_for_loading (current_program_space);
250
251 for (p = start; p < end; ++p)
252 {
253 const char *file;
254 FILE *stream;
255 char *full_path;
256 int opened, in_hash_table;
257
258 if (*p != 1)
259 {
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. */
263 break;
264 }
265 file = ++p;
266
267 while (p < end && *p != '\0')
268 ++p;
269 if (p == end)
270 {
271 char *buf = alloca (p - file + 1);
272
273 memcpy (buf, file, p - file);
274 buf[p - file] = '\0';
275 warning (_("Non-null-terminated path in %s: %s"),
276 source_name, buf);
277 /* Don't load it. */
278 break;
279 }
280 if (p == file)
281 {
282 warning (_("Empty path in %s"), source_name);
283 continue;
284 }
285
286 opened = find_and_open_script (file, 1 /*search_path*/,
287 &stream, &full_path);
288
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.
293
294 IWBN if complaints.c were more general-purpose. */
295
296 in_hash_table = maybe_add_script (pspace_info->loaded_scripts, file,
297 opened ? full_path : NULL);
298
299 if (opened)
300 free (full_path);
301
302 if (! opened)
303 {
304 /* We don't throw an error, the program is still debuggable. */
305 if (! pspace_info->script_not_found_warning_printed)
306 {
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;
311 }
312 continue;
313 }
314
315 /* If this file is not currently loaded, load it. */
316 if (! in_hash_table)
317 source_python_script_for_objfile (objfile, stream, file);
318 }
319 }
320
321 /* Load scripts specified in section SECTION_NAME of OBJFILE. */
322
323 static void
324 auto_load_section_scripts (struct objfile *objfile, const char *section_name)
325 {
326 bfd *abfd = objfile->obfd;
327 asection *scripts_sect;
328 bfd_size_type size;
329 char *p;
330 struct cleanup *cleanups;
331
332 scripts_sect = bfd_get_section_by_name (abfd, section_name);
333 if (scripts_sect == NULL)
334 return;
335
336 size = bfd_get_section_size (scripts_sect);
337 p = xmalloc (size);
338
339 cleanups = make_cleanup (xfree, p);
340
341 if (bfd_get_section_contents (abfd, scripts_sect, p, (file_ptr) 0, size))
342 source_section_scripts (objfile, section_name, p, p + size);
343 else
344 warning (_("Couldn't read %s section of %s"),
345 section_name, bfd_get_filename (abfd));
346
347 do_cleanups (cleanups);
348 }
349
350 /* Clear the table of loaded section scripts. */
351
352 static void
353 clear_section_scripts (void)
354 {
355 struct program_space *pspace = current_program_space;
356 struct auto_load_pspace_info *info;
357
358 info = program_space_data (pspace, auto_load_pspace_data);
359 if (info != NULL && info->loaded_scripts != NULL)
360 {
361 htab_delete (info->loaded_scripts);
362 info->loaded_scripts = NULL;
363 info->script_not_found_warning_printed = FALSE;
364 }
365 }
366
367 /* Look for the auto-load script associated with OBJFILE and load it. */
368
369 static void
370 auto_load_objfile_script (struct objfile *objfile, const char *suffix)
371 {
372 char *realname;
373 char *filename, *debugfile;
374 int len;
375 FILE *input;
376 struct cleanup *cleanups;
377
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);
383
384 cleanups = make_cleanup (xfree, filename);
385 make_cleanup (xfree, realname);
386
387 input = fopen (filename, "r");
388 debugfile = filename;
389
390 if (!input && debug_file_directory)
391 {
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);
398
399 make_cleanup (xfree, debugfile);
400 input = fopen (debugfile, "r");
401 }
402
403 if (!input && gdb_datadir)
404 {
405 /* Also try the same file in a subdirectory of gdb's data
406 directory. */
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);
413
414 make_cleanup (xfree, debugfile);
415 input = fopen (debugfile, "r");
416 }
417
418 if (input)
419 {
420 struct auto_load_pspace_info *pspace_info;
421
422 /* Add this script to the hash table too so "info auto-load-scripts"
423 can print it. */
424 pspace_info =
425 get_auto_load_pspace_data_for_loading (current_program_space);
426 maybe_add_script (pspace_info->loaded_scripts, debugfile, debugfile);
427
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
432 loads anyway. */
433 source_python_script_for_objfile (objfile, input, debugfile);
434 fclose (input);
435 }
436
437 do_cleanups (cleanups);
438 }
439
440 /* This is a new_objfile observer callback to auto-load scripts.
441
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 */
445
446 static void
447 auto_load_new_objfile (struct objfile *objfile)
448 {
449 if (!objfile)
450 {
451 /* OBJFILE is NULL when loading a new "main" symbol-file. */
452 clear_section_scripts ();
453 return;
454 }
455
456 load_auto_scripts_for_objfile (objfile);
457 }
458
459 /* Load any auto-loaded scripts for OBJFILE. */
460
461 void
462 load_auto_scripts_for_objfile (struct objfile *objfile)
463 {
464 if (auto_load_scripts && gdbpy_global_auto_load)
465 {
466 auto_load_objfile_script (objfile, GDBPY_AUTO_FILE_NAME);
467 auto_load_section_scripts (objfile, GDBPY_AUTO_SECTION_NAME);
468 }
469 }
470 \f
471 /* Collect scripts to be printed in a vec. */
472
473 typedef struct loaded_script *loaded_script_ptr;
474 DEF_VEC_P (loaded_script_ptr);
475
476 /* Traversal function for htab_traverse.
477 Collect the entry if it matches the regexp. */
478
479 static int
480 collect_matching_scripts (void **slot, void *info)
481 {
482 struct loaded_script *script = *slot;
483 VEC (loaded_script_ptr) *scripts = info;
484
485 if (re_exec (script->name))
486 VEC_safe_push (loaded_script_ptr, scripts, script);
487
488 return 1;
489 }
490
491 /* Print SCRIPT. */
492
493 static void
494 print_script (struct loaded_script *script)
495 {
496 struct cleanup *chain;
497
498 chain = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
499
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");
503
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)
507 {
508 ui_out_text (uiout, "\tfull name: ");
509 ui_out_field_string (uiout, "full_path", script->full_path);
510 ui_out_text (uiout, "\n");
511 }
512
513 do_cleanups (chain);
514 }
515
516 /* Helper for info_auto_load_scripts to sort the scripts by name. */
517
518 static int
519 sort_scripts_by_name (const void *ap, const void *bp)
520 {
521 const struct loaded_script *a = *(const struct loaded_script **) ap;
522 const struct loaded_script *b = *(const struct loaded_script **) bp;
523
524 return FILENAME_CMP (a->name, b->name);
525 }
526
527 /* "info auto-load-scripts" command. */
528
529 static void
530 info_auto_load_scripts (char *pattern, int from_tty)
531 {
532 struct auto_load_pspace_info *pspace_info;
533 struct cleanup *script_chain;
534 VEC (loaded_script_ptr) *scripts;
535 int nr_scripts;
536
537 dont_repeat ();
538
539 pspace_info = get_auto_load_pspace_data (current_program_space);
540
541 if (pattern && *pattern)
542 {
543 char *re_err = re_comp (pattern);
544
545 if (re_err)
546 error (_("Invalid regexp: %s"), re_err);
547 }
548 else
549 {
550 re_comp ("");
551 }
552
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. */
556
557 scripts = VEC_alloc (loaded_script_ptr, 10);
558 script_chain = make_cleanup (VEC_cleanup (loaded_script_ptr), &scripts);
559
560 if (pspace_info != NULL && pspace_info->loaded_scripts != NULL)
561 {
562 immediate_quit++;
563 htab_traverse_noresize (pspace_info->loaded_scripts,
564 collect_matching_scripts, scripts);
565 immediate_quit--;
566 }
567
568 nr_scripts = VEC_length (loaded_script_ptr, scripts);
569 make_cleanup_ui_out_table_begin_end (uiout, 2, nr_scripts,
570 "AutoLoadedScriptsTable");
571
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);
575
576 if (nr_scripts > 0)
577 {
578 int i;
579 loaded_script_ptr script;
580
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);
586 }
587
588 do_cleanups (script_chain);
589
590 if (nr_scripts == 0)
591 {
592 if (pattern && *pattern)
593 ui_out_message (uiout, 0, "No auto-load scripts matching %s.\n",
594 pattern);
595 else
596 ui_out_message (uiout, 0, "No auto-load scripts.\n");
597 }
598 }
599 \f
600 void
601 gdbpy_initialize_auto_load (void)
602 {
603 auto_load_pspace_data
604 = register_program_space_data_with_cleanup (auto_load_pspace_data_cleanup);
605
606 observer_attach_new_objfile (auto_load_new_objfile);
607
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."),
614 NULL, NULL,
615 &setlist,
616 &showlist);
617
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]"));
622 }
623
624 #else /* ! HAVE_PYTHON */
625
626 void
627 load_auto_scripts_for_objfile (struct objfile *objfile)
628 {
629 }
630
631 #endif /* ! HAVE_PYTHON */