Add support for auto-loading scripts from .debug_gdb_scripts section.
[deliverable/binutils-gdb.git] / gdb / python / py-auto-load.c
1 /* GDB routines for supporting auto-loaded scripts.
2
3 Copyright (C) 2010 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 "gdb_string.h"
22 #include "gdb_regex.h"
23 #include "top.h"
24 #include "exceptions.h"
25 #include "command.h"
26 #include "gdbcmd.h"
27 #include "observer.h"
28 #include "progspace.h"
29 #include "objfiles.h"
30 #include "python.h"
31 #include "python-internal.h"
32 #include "cli/cli-cmds.h"
33
34 /* NOTE: It's trivial to also support auto-loading normal gdb scripts.
35 There has yet to be a need so it's not implemented. */
36
37 /* The suffix of per-objfile scripts to auto-load.
38 E.g. When the program loads libfoo.so, look for libfoo-gdb.py. */
39 #define GDBPY_AUTO_FILE_NAME "-gdb.py"
40
41 /* The section to look for scripts (in file formats that support sections).
42 Each entry in this section is a byte of value 1, and then the nul-terminated
43 name of the script. The script name may include a directory.
44 The leading byte is to allow upward compatible extensions. */
45 #define GDBPY_AUTO_SECTION_NAME ".debug_gdb_scripts"
46
47 /* For scripts specified in .debug_gdb_scripts, multiple objfiles may load
48 the same script. There's no point in loading the script multiple times,
49 and there can be a lot of objfiles and scripts, so we keep track of scripts
50 loaded this way. */
51
52 struct auto_load_pspace_info
53 {
54 /* For each program space we keep track of loaded scripts. */
55 struct htab *loaded_scripts;
56 };
57
58 /* Objects of this type are stored in the loaded script hash table. */
59
60 struct loaded_script_entry
61 {
62 /* Name as provided by the objfile. */
63 const char *name;
64 /* Full path name or NULL if script wasn't found (or was otherwise
65 inaccessible). */
66 const char *full_path;
67 };
68
69 /* This is true if we should auto-load python code when an objfile is opened,
70 false otherwise. */
71 static int gdbpy_auto_load = 1;
72
73 /* Per-program-space data key. */
74 static const struct program_space_data *auto_load_pspace_data;
75
76 static void
77 auto_load_pspace_data_cleanup (struct program_space *pspace, void *arg)
78 {
79 struct auto_load_pspace_info *info;
80
81 info = program_space_data (pspace, auto_load_pspace_data);
82 if (info != NULL)
83 {
84 if (info->loaded_scripts)
85 htab_delete (info->loaded_scripts);
86 xfree (info);
87 }
88 }
89
90 /* Get the current autoload data. If none is found yet, add it now. This
91 function always returns a valid object. */
92
93 static struct auto_load_pspace_info *
94 get_auto_load_pspace_data (struct program_space *pspace)
95 {
96 struct auto_load_pspace_info *info;
97
98 info = program_space_data (pspace, auto_load_pspace_data);
99 if (info == NULL)
100 {
101 info = XZALLOC (struct auto_load_pspace_info);
102 set_program_space_data (pspace, auto_load_pspace_data, info);
103 }
104
105 return info;
106 }
107
108 /* Hash function for the loaded script hash. */
109
110 static hashval_t
111 hash_loaded_script_entry (const void *data)
112 {
113 const struct loaded_script_entry *e = data;
114 return htab_hash_string (e->name);
115 }
116
117 /* Equality function for the loaded script hash. */
118
119 static int
120 eq_loaded_script_entry (const void *a, const void *b)
121 {
122 const struct loaded_script_entry *ea = a;
123 const struct loaded_script_entry *eb = b;
124 return strcmp (ea->name, eb->name) == 0;
125 }
126
127 /* Create the hash table used for loaded scripts.
128 Each entry is hashed by the full path name. */
129
130 static void
131 create_loaded_scripts_hash (struct auto_load_pspace_info *pspace_info)
132 {
133 /* Choose 31 as the starting size of the hash table, somewhat arbitrarily.
134 Space for each entry is obtained with one malloc so we can free them
135 easily. */
136
137 pspace_info->loaded_scripts = htab_create (31,
138 hash_loaded_script_entry,
139 eq_loaded_script_entry,
140 xfree);
141 }
142
143 /* Load scripts specified in OBJFILE.
144 START,END delimit a buffer containing a list of nul-terminated
145 file names.
146 SOURCE_NAME is used in error messages.
147
148 Scripts are found per normal "source -s" command processing.
149 First the script is looked for in $cwd. If not found there the
150 source search path is used.
151
152 The section contains a list of path names of files containing
153 python code to load. Each path is null-terminated. */
154
155 static void
156 source_section_scripts (struct objfile *objfile, const char *source_name,
157 const char *start, const char *end)
158 {
159 const char *p;
160 struct auto_load_pspace_info *pspace_info;
161 struct loaded_script_entry **slot, entry;
162
163 pspace_info = get_auto_load_pspace_data (current_program_space);
164 if (pspace_info->loaded_scripts == NULL)
165 create_loaded_scripts_hash (pspace_info);
166
167 for (p = start; p < end; ++p)
168 {
169 const char *file;
170 FILE *stream;
171 char *full_path;
172 int opened, in_hash_table;
173
174 if (*p != 1)
175 {
176 warning (_("Invalid entry in %s section"), GDBPY_AUTO_SECTION_NAME);
177 /* We could try various heuristics to find the next valid entry,
178 but it's safer to just punt. */
179 break;
180 }
181 file = ++p;
182
183 while (p < end && *p != '\0')
184 ++p;
185 if (p == end)
186 {
187 char *buf = alloca (p - file + 1);
188 memcpy (buf, file, p - file);
189 buf[p - file] = '\0';
190 warning (_("Non-null-terminated path in %s: %s"),
191 source_name, buf);
192 /* Don't load it. */
193 break;
194 }
195 if (p == file)
196 {
197 warning (_("Empty path in %s"), source_name);
198 continue;
199 }
200
201 opened = find_and_open_script (file, 1 /*search_path*/,
202 &stream, &full_path);
203
204 /* If the file is not found, we still record the file in the hash table,
205 we only want to print an error message once.
206 IWBN if complaints.c were more general-purpose. */
207
208 entry.name = file;
209 if (opened)
210 entry.full_path = full_path;
211 else
212 entry.full_path = NULL;
213 slot = ((struct loaded_script_entry **)
214 htab_find_slot (pspace_info->loaded_scripts,
215 &entry, INSERT));
216 in_hash_table = *slot != NULL;
217
218 /* If this file is not in the hash table, add it. */
219 if (! in_hash_table)
220 {
221 char *p;
222 *slot = xmalloc (sizeof (**slot)
223 + strlen (file) + 1
224 + (opened ? (strlen (full_path) + 1) : 0));
225 p = ((char*) *slot) + sizeof (**slot);
226 strcpy (p, file);
227 (*slot)->name = p;
228 if (opened)
229 {
230 p += strlen (p) + 1;
231 strcpy (p, full_path);
232 (*slot)->full_path = p;
233 }
234 else
235 (*slot)->full_path = NULL;
236 }
237
238 if (opened)
239 free (full_path);
240
241 if (! opened)
242 {
243 /* We don't throw an error, the program is still debuggable.
244 Check in_hash_table to only print the warning once. */
245 if (! in_hash_table)
246 warning (_("%s (referenced in %s): %s\n"),
247 file, GDBPY_AUTO_SECTION_NAME, safe_strerror (errno));
248 continue;
249 }
250
251 /* If this file is not currently loaded, load it. */
252 if (! in_hash_table)
253 source_python_script_for_objfile (objfile, stream, file);
254 }
255 }
256
257 /* Load scripts specified in section SECTION_NAME of OBJFILE. */
258
259 static void
260 auto_load_section_scripts (struct objfile *objfile, const char *section_name)
261 {
262 bfd *abfd = objfile->obfd;
263 asection *scripts_sect;
264 bfd_size_type size;
265 char *p;
266 struct cleanup *cleanups;
267
268 scripts_sect = bfd_get_section_by_name (abfd, section_name);
269 if (scripts_sect == NULL)
270 return;
271
272 size = bfd_get_section_size (scripts_sect);
273 p = xmalloc (size);
274
275 cleanups = make_cleanup (xfree, p);
276
277 if (bfd_get_section_contents (abfd, scripts_sect, p, (file_ptr) 0, size))
278 source_section_scripts (objfile, section_name, p, p + size);
279 else
280 warning (_("Couldn't read %s section of %s"),
281 section_name, bfd_get_filename (abfd));
282
283 do_cleanups (cleanups);
284 }
285
286 /* Clear the table of loaded section scripts. */
287
288 static void
289 clear_section_scripts (void)
290 {
291 struct program_space *pspace = current_program_space;
292 struct auto_load_pspace_info *info;
293
294 info = program_space_data (pspace, auto_load_pspace_data);
295 if (info != NULL && info->loaded_scripts != NULL)
296 {
297 htab_delete (info->loaded_scripts);
298 info->loaded_scripts = NULL;
299 }
300 }
301
302 /* Look for the auto-load script associated with OBJFILE and load it. */
303
304 static void
305 auto_load_objfile_script (struct objfile *objfile, const char *suffix)
306 {
307 char *realname;
308 char *filename, *debugfile;
309 int len;
310 FILE *input;
311 struct cleanup *cleanups;
312
313 realname = gdb_realpath (objfile->name);
314 len = strlen (realname);
315 filename = xmalloc (len + strlen (suffix) + 1);
316 memcpy (filename, realname, len);
317 strcpy (filename + len, suffix);
318
319 cleanups = make_cleanup (xfree, filename);
320 make_cleanup (xfree, realname);
321
322 input = fopen (filename, "r");
323 debugfile = filename;
324
325 if (!input && debug_file_directory)
326 {
327 /* Also try the same file in the separate debug info directory. */
328 debugfile = xmalloc (strlen (filename)
329 + strlen (debug_file_directory) + 1);
330 strcpy (debugfile, debug_file_directory);
331 /* FILENAME is absolute, so we don't need a "/" here. */
332 strcat (debugfile, filename);
333
334 make_cleanup (xfree, debugfile);
335 input = fopen (debugfile, "r");
336 }
337
338 if (!input && gdb_datadir)
339 {
340 /* Also try the same file in a subdirectory of gdb's data
341 directory. */
342 debugfile = xmalloc (strlen (gdb_datadir) + strlen (filename)
343 + strlen ("/auto-load") + 1);
344 strcpy (debugfile, gdb_datadir);
345 strcat (debugfile, "/auto-load");
346 /* FILENAME is absolute, so we don't need a "/" here. */
347 strcat (debugfile, filename);
348
349 make_cleanup (xfree, debugfile);
350 input = fopen (debugfile, "r");
351 }
352
353 if (input)
354 {
355 source_python_script_for_objfile (objfile, input, debugfile);
356 fclose (input);
357 }
358
359 do_cleanups (cleanups);
360 }
361
362 /* This is a new_objfile observer callback to auto-load scripts.
363
364 Two flavors of auto-loaded scripts are supported.
365 1) based on the path to the objfile
366 2) from .debug_gdb_scripts section */
367
368 static void
369 auto_load_new_objfile (struct objfile *objfile)
370 {
371 if (!objfile)
372 {
373 /* OBJFILE is NULL when loading a new "main" symbol-file. */
374 clear_section_scripts ();
375 return;
376 }
377 if (!objfile->name)
378 return;
379
380 if (gdbpy_auto_load)
381 {
382 auto_load_objfile_script (objfile, GDBPY_AUTO_FILE_NAME);
383 auto_load_section_scripts (objfile, GDBPY_AUTO_SECTION_NAME);
384 }
385 }
386 \f
387 /* Traversal function for htab_traverse.
388 Print the entry if specified in the regex. */
389
390 static int
391 maybe_print_section_script (void **slot, void *info)
392 {
393 struct loaded_script_entry *entry = *slot;
394
395 if (re_exec (entry->name))
396 {
397 printf_filtered (_("Script name: %s\n"), entry->name);
398 printf_filtered (_(" Full name: %s\n"),
399 entry->full_path ? entry->full_path : _("unknown"));
400 }
401
402 return 1;
403 }
404
405 /* "maint print section-scripts" command. */
406
407 static void
408 maintenance_print_section_scripts (char *pattern, int from_tty)
409 {
410 struct auto_load_pspace_info *pspace_info;
411
412 dont_repeat ();
413
414 if (pattern && *pattern)
415 {
416 char *re_err = re_comp (pattern);
417
418 if (re_err)
419 error (_("Invalid regexp: %s"), re_err);
420
421 printf_filtered (_("Objfile scripts matching %s:\n"), pattern);
422 }
423 else
424 {
425 re_comp ("");
426 printf_filtered (_("Objfile scripts:\n"));
427 }
428
429 pspace_info = get_auto_load_pspace_data (current_program_space);
430 if (pspace_info == NULL || pspace_info->loaded_scripts == NULL)
431 return;
432
433 immediate_quit++;
434 htab_traverse_noresize (pspace_info->loaded_scripts,
435 maybe_print_section_script, NULL);
436 immediate_quit--;
437 }
438 \f
439 void
440 gdbpy_initialize_auto_load (void)
441 {
442 auto_load_pspace_data
443 = register_program_space_data_with_cleanup (auto_load_pspace_data_cleanup);
444
445 observer_attach_new_objfile (auto_load_new_objfile);
446
447 add_setshow_boolean_cmd ("auto-load", class_maintenance,
448 &gdbpy_auto_load, _("\
449 Enable or disable auto-loading of Python code when an object is opened."), _("\
450 Show whether Python code will be auto-loaded when an object is opened."), _("\
451 Enables or disables auto-loading of Python code when an object is opened."),
452 NULL, NULL,
453 &set_python_list,
454 &show_python_list);
455
456 add_cmd ("section-scripts", class_maintenance, maintenance_print_section_scripts,
457 _("Print dump of auto-loaded section scripts matching REGEXP."),
458 &maintenanceprintlist);
459 }
This page took 0.040318 seconds and 5 git commands to generate.