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