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