| 1 | /* GDB routines for supporting auto-loaded scripts. |
| 2 | |
| 3 | Copyright (C) 2012-2014 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 "auto-load.h" |
| 22 | #include "progspace.h" |
| 23 | #include "gdb_regex.h" |
| 24 | #include "ui-out.h" |
| 25 | #include "filenames.h" |
| 26 | #include "command.h" |
| 27 | #include "observer.h" |
| 28 | #include "objfiles.h" |
| 29 | #include "exceptions.h" |
| 30 | #include "cli/cli-script.h" |
| 31 | #include "gdbcmd.h" |
| 32 | #include "cli/cli-cmds.h" |
| 33 | #include "cli/cli-decode.h" |
| 34 | #include "cli/cli-setshow.h" |
| 35 | #include "gdb_vecs.h" |
| 36 | #include "readline/tilde.h" |
| 37 | #include "completer.h" |
| 38 | #include "fnmatch.h" |
| 39 | #include "top.h" |
| 40 | #include "filestuff.h" |
| 41 | #include "extension.h" |
| 42 | #include "gdb/section-scripts.h" |
| 43 | |
| 44 | /* The section to look in for auto-loaded scripts (in file formats that |
| 45 | support sections). |
| 46 | Each entry in this section is a record that begins with a leading byte |
| 47 | identifying the record type. |
| 48 | At the moment we only support one record type: A leading byte of 1, |
| 49 | followed by the path of a python script to load. */ |
| 50 | #define AUTO_SECTION_NAME ".debug_gdb_scripts" |
| 51 | |
| 52 | static int maybe_add_script (struct auto_load_pspace_info *pspace_info, |
| 53 | int loaded, const char *name, |
| 54 | const char *full_path, |
| 55 | const struct extension_language_defn *language); |
| 56 | |
| 57 | static int unsupported_script_warning_print (struct auto_load_pspace_info *); |
| 58 | |
| 59 | static int script_not_found_warning_print (struct auto_load_pspace_info *); |
| 60 | |
| 61 | /* Value of the 'set debug auto-load' configuration variable. */ |
| 62 | static int debug_auto_load = 0; |
| 63 | |
| 64 | /* "show" command for the debug_auto_load configuration variable. */ |
| 65 | |
| 66 | static void |
| 67 | show_debug_auto_load (struct ui_file *file, int from_tty, |
| 68 | struct cmd_list_element *c, const char *value) |
| 69 | { |
| 70 | fprintf_filtered (file, _("Debugging output for files " |
| 71 | "of 'set auto-load ...' is %s.\n"), |
| 72 | value); |
| 73 | } |
| 74 | |
| 75 | /* User-settable option to enable/disable auto-loading of GDB_AUTO_FILE_NAME |
| 76 | scripts: |
| 77 | set auto-load gdb-scripts on|off |
| 78 | This is true if we should auto-load associated scripts when an objfile |
| 79 | is opened, false otherwise. */ |
| 80 | static int auto_load_gdb_scripts = 1; |
| 81 | |
| 82 | /* "show" command for the auto_load_gdb_scripts configuration variable. */ |
| 83 | |
| 84 | static void |
| 85 | show_auto_load_gdb_scripts (struct ui_file *file, int from_tty, |
| 86 | struct cmd_list_element *c, const char *value) |
| 87 | { |
| 88 | fprintf_filtered (file, _("Auto-loading of canned sequences of commands " |
| 89 | "scripts is %s.\n"), |
| 90 | value); |
| 91 | } |
| 92 | |
| 93 | /* Return non-zero if auto-loading gdb scripts is enabled. */ |
| 94 | |
| 95 | int |
| 96 | auto_load_gdb_scripts_enabled (const struct extension_language_defn *extlang) |
| 97 | { |
| 98 | return auto_load_gdb_scripts; |
| 99 | } |
| 100 | |
| 101 | /* Internal-use flag to enable/disable auto-loading. |
| 102 | This is true if we should auto-load python code when an objfile is opened, |
| 103 | false otherwise. |
| 104 | |
| 105 | Both auto_load_scripts && global_auto_load must be true to enable |
| 106 | auto-loading. |
| 107 | |
| 108 | This flag exists to facilitate deferring auto-loading during start-up |
| 109 | until after ./.gdbinit has been read; it may augment the search directories |
| 110 | used to find the scripts. */ |
| 111 | int global_auto_load = 1; |
| 112 | |
| 113 | /* Auto-load .gdbinit file from the current directory? */ |
| 114 | int auto_load_local_gdbinit = 1; |
| 115 | |
| 116 | /* Absolute pathname to the current directory .gdbinit, if it exists. */ |
| 117 | char *auto_load_local_gdbinit_pathname = NULL; |
| 118 | |
| 119 | /* Boolean value if AUTO_LOAD_LOCAL_GDBINIT_PATHNAME has been loaded. */ |
| 120 | int auto_load_local_gdbinit_loaded = 0; |
| 121 | |
| 122 | /* "show" command for the auto_load_local_gdbinit configuration variable. */ |
| 123 | |
| 124 | static void |
| 125 | show_auto_load_local_gdbinit (struct ui_file *file, int from_tty, |
| 126 | struct cmd_list_element *c, const char *value) |
| 127 | { |
| 128 | fprintf_filtered (file, _("Auto-loading of .gdbinit script from current " |
| 129 | "directory is %s.\n"), |
| 130 | value); |
| 131 | } |
| 132 | |
| 133 | /* Directory list from which to load auto-loaded scripts. It is not checked |
| 134 | for absolute paths but they are strongly recommended. It is initialized by |
| 135 | _initialize_auto_load. */ |
| 136 | static char *auto_load_dir; |
| 137 | |
| 138 | /* "set" command for the auto_load_dir configuration variable. */ |
| 139 | |
| 140 | static void |
| 141 | set_auto_load_dir (char *args, int from_tty, struct cmd_list_element *c) |
| 142 | { |
| 143 | /* Setting the variable to "" resets it to the compile time defaults. */ |
| 144 | if (auto_load_dir[0] == '\0') |
| 145 | { |
| 146 | xfree (auto_load_dir); |
| 147 | auto_load_dir = xstrdup (AUTO_LOAD_DIR); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | /* "show" command for the auto_load_dir configuration variable. */ |
| 152 | |
| 153 | static void |
| 154 | show_auto_load_dir (struct ui_file *file, int from_tty, |
| 155 | struct cmd_list_element *c, const char *value) |
| 156 | { |
| 157 | fprintf_filtered (file, _("List of directories from which to load " |
| 158 | "auto-loaded scripts is %s.\n"), |
| 159 | value); |
| 160 | } |
| 161 | |
| 162 | /* Directory list safe to hold auto-loaded files. It is not checked for |
| 163 | absolute paths but they are strongly recommended. It is initialized by |
| 164 | _initialize_auto_load. */ |
| 165 | static char *auto_load_safe_path; |
| 166 | |
| 167 | /* Vector of directory elements of AUTO_LOAD_SAFE_PATH with each one normalized |
| 168 | by tilde_expand and possibly each entries has added its gdb_realpath |
| 169 | counterpart. */ |
| 170 | static VEC (char_ptr) *auto_load_safe_path_vec; |
| 171 | |
| 172 | /* Expand $datadir and $debugdir in STRING according to the rules of |
| 173 | substitute_path_component. Return vector from dirnames_to_char_ptr_vec, |
| 174 | this vector must be freed by free_char_ptr_vec by the caller. */ |
| 175 | |
| 176 | static VEC (char_ptr) * |
| 177 | auto_load_expand_dir_vars (const char *string) |
| 178 | { |
| 179 | VEC (char_ptr) *dir_vec; |
| 180 | char *s; |
| 181 | |
| 182 | s = xstrdup (string); |
| 183 | substitute_path_component (&s, "$datadir", gdb_datadir); |
| 184 | substitute_path_component (&s, "$debugdir", debug_file_directory); |
| 185 | |
| 186 | if (debug_auto_load && strcmp (s, string) != 0) |
| 187 | fprintf_unfiltered (gdb_stdlog, |
| 188 | _("auto-load: Expanded $-variables to \"%s\".\n"), s); |
| 189 | |
| 190 | dir_vec = dirnames_to_char_ptr_vec (s); |
| 191 | xfree(s); |
| 192 | |
| 193 | return dir_vec; |
| 194 | } |
| 195 | |
| 196 | /* Update auto_load_safe_path_vec from current AUTO_LOAD_SAFE_PATH. */ |
| 197 | |
| 198 | static void |
| 199 | auto_load_safe_path_vec_update (void) |
| 200 | { |
| 201 | unsigned len; |
| 202 | int ix; |
| 203 | |
| 204 | if (debug_auto_load) |
| 205 | fprintf_unfiltered (gdb_stdlog, |
| 206 | _("auto-load: Updating directories of \"%s\".\n"), |
| 207 | auto_load_safe_path); |
| 208 | |
| 209 | free_char_ptr_vec (auto_load_safe_path_vec); |
| 210 | |
| 211 | auto_load_safe_path_vec = auto_load_expand_dir_vars (auto_load_safe_path); |
| 212 | len = VEC_length (char_ptr, auto_load_safe_path_vec); |
| 213 | |
| 214 | /* Apply tilde_expand and gdb_realpath to each AUTO_LOAD_SAFE_PATH_VEC |
| 215 | element. */ |
| 216 | for (ix = 0; ix < len; ix++) |
| 217 | { |
| 218 | char *dir = VEC_index (char_ptr, auto_load_safe_path_vec, ix); |
| 219 | char *expanded = tilde_expand (dir); |
| 220 | char *real_path = gdb_realpath (expanded); |
| 221 | |
| 222 | /* Ensure the current entry is at least tilde_expand-ed. */ |
| 223 | VEC_replace (char_ptr, auto_load_safe_path_vec, ix, expanded); |
| 224 | |
| 225 | if (debug_auto_load) |
| 226 | { |
| 227 | if (strcmp (expanded, dir) == 0) |
| 228 | fprintf_unfiltered (gdb_stdlog, |
| 229 | _("auto-load: Using directory \"%s\".\n"), |
| 230 | expanded); |
| 231 | else |
| 232 | fprintf_unfiltered (gdb_stdlog, |
| 233 | _("auto-load: Resolved directory \"%s\" " |
| 234 | "as \"%s\".\n"), |
| 235 | dir, expanded); |
| 236 | } |
| 237 | xfree (dir); |
| 238 | |
| 239 | /* If gdb_realpath returns a different content, append it. */ |
| 240 | if (strcmp (real_path, expanded) == 0) |
| 241 | xfree (real_path); |
| 242 | else |
| 243 | { |
| 244 | VEC_safe_push (char_ptr, auto_load_safe_path_vec, real_path); |
| 245 | |
| 246 | if (debug_auto_load) |
| 247 | fprintf_unfiltered (gdb_stdlog, |
| 248 | _("auto-load: And canonicalized as \"%s\".\n"), |
| 249 | real_path); |
| 250 | } |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | /* Variable gdb_datadir has been set. Update content depending on $datadir. */ |
| 255 | |
| 256 | static void |
| 257 | auto_load_gdb_datadir_changed (void) |
| 258 | { |
| 259 | auto_load_safe_path_vec_update (); |
| 260 | } |
| 261 | |
| 262 | /* "set" command for the auto_load_safe_path configuration variable. */ |
| 263 | |
| 264 | static void |
| 265 | set_auto_load_safe_path (char *args, int from_tty, struct cmd_list_element *c) |
| 266 | { |
| 267 | /* Setting the variable to "" resets it to the compile time defaults. */ |
| 268 | if (auto_load_safe_path[0] == '\0') |
| 269 | { |
| 270 | xfree (auto_load_safe_path); |
| 271 | auto_load_safe_path = xstrdup (AUTO_LOAD_SAFE_PATH); |
| 272 | } |
| 273 | |
| 274 | auto_load_safe_path_vec_update (); |
| 275 | } |
| 276 | |
| 277 | /* "show" command for the auto_load_safe_path configuration variable. */ |
| 278 | |
| 279 | static void |
| 280 | show_auto_load_safe_path (struct ui_file *file, int from_tty, |
| 281 | struct cmd_list_element *c, const char *value) |
| 282 | { |
| 283 | const char *cs; |
| 284 | |
| 285 | /* Check if user has entered either "/" or for example ":". |
| 286 | But while more complicate content like ":/foo" would still also |
| 287 | permit any location do not hide those. */ |
| 288 | |
| 289 | for (cs = value; *cs && (*cs == DIRNAME_SEPARATOR || IS_DIR_SEPARATOR (*cs)); |
| 290 | cs++); |
| 291 | if (*cs == 0) |
| 292 | fprintf_filtered (file, _("Auto-load files are safe to load from any " |
| 293 | "directory.\n")); |
| 294 | else |
| 295 | fprintf_filtered (file, _("List of directories from which it is safe to " |
| 296 | "auto-load files is %s.\n"), |
| 297 | value); |
| 298 | } |
| 299 | |
| 300 | /* "add-auto-load-safe-path" command for the auto_load_safe_path configuration |
| 301 | variable. */ |
| 302 | |
| 303 | static void |
| 304 | add_auto_load_safe_path (char *args, int from_tty) |
| 305 | { |
| 306 | char *s; |
| 307 | |
| 308 | if (args == NULL || *args == 0) |
| 309 | error (_("\ |
| 310 | Directory argument required.\n\ |
| 311 | Use 'set auto-load safe-path /' for disabling the auto-load safe-path security.\ |
| 312 | ")); |
| 313 | |
| 314 | s = xstrprintf ("%s%c%s", auto_load_safe_path, DIRNAME_SEPARATOR, args); |
| 315 | xfree (auto_load_safe_path); |
| 316 | auto_load_safe_path = s; |
| 317 | |
| 318 | auto_load_safe_path_vec_update (); |
| 319 | } |
| 320 | |
| 321 | /* Implementation for filename_is_in_pattern overwriting the caller's FILENAME |
| 322 | and PATTERN. */ |
| 323 | |
| 324 | static int |
| 325 | filename_is_in_pattern_1 (char *filename, char *pattern) |
| 326 | { |
| 327 | size_t pattern_len = strlen (pattern); |
| 328 | size_t filename_len = strlen (filename); |
| 329 | |
| 330 | if (debug_auto_load) |
| 331 | fprintf_unfiltered (gdb_stdlog, _("auto-load: Matching file \"%s\" " |
| 332 | "to pattern \"%s\"\n"), |
| 333 | filename, pattern); |
| 334 | |
| 335 | /* Trim trailing slashes ("/") from PATTERN. Even for "d:\" paths as |
| 336 | trailing slashes are trimmed also from FILENAME it still matches |
| 337 | correctly. */ |
| 338 | while (pattern_len && IS_DIR_SEPARATOR (pattern[pattern_len - 1])) |
| 339 | pattern_len--; |
| 340 | pattern[pattern_len] = '\0'; |
| 341 | |
| 342 | /* Ensure auto_load_safe_path "/" matches any FILENAME. On MS-Windows |
| 343 | platform FILENAME even after gdb_realpath does not have to start with |
| 344 | IS_DIR_SEPARATOR character, such as the 'C:\x.exe' filename. */ |
| 345 | if (pattern_len == 0) |
| 346 | { |
| 347 | if (debug_auto_load) |
| 348 | fprintf_unfiltered (gdb_stdlog, |
| 349 | _("auto-load: Matched - empty pattern\n")); |
| 350 | return 1; |
| 351 | } |
| 352 | |
| 353 | for (;;) |
| 354 | { |
| 355 | /* Trim trailing slashes ("/"). PATTERN also has slashes trimmed the |
| 356 | same way so they will match. */ |
| 357 | while (filename_len && IS_DIR_SEPARATOR (filename[filename_len - 1])) |
| 358 | filename_len--; |
| 359 | filename[filename_len] = '\0'; |
| 360 | if (filename_len == 0) |
| 361 | { |
| 362 | if (debug_auto_load) |
| 363 | fprintf_unfiltered (gdb_stdlog, |
| 364 | _("auto-load: Not matched - pattern \"%s\".\n"), |
| 365 | pattern); |
| 366 | return 0; |
| 367 | } |
| 368 | |
| 369 | if (gdb_filename_fnmatch (pattern, filename, FNM_FILE_NAME | FNM_NOESCAPE) |
| 370 | == 0) |
| 371 | { |
| 372 | if (debug_auto_load) |
| 373 | fprintf_unfiltered (gdb_stdlog, _("auto-load: Matched - file " |
| 374 | "\"%s\" to pattern \"%s\".\n"), |
| 375 | filename, pattern); |
| 376 | return 1; |
| 377 | } |
| 378 | |
| 379 | /* Trim trailing FILENAME component. */ |
| 380 | while (filename_len > 0 && !IS_DIR_SEPARATOR (filename[filename_len - 1])) |
| 381 | filename_len--; |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | /* Return 1 if FILENAME matches PATTERN or if FILENAME resides in |
| 386 | a subdirectory of a directory that matches PATTERN. Return 0 otherwise. |
| 387 | gdb_realpath normalization is never done here. */ |
| 388 | |
| 389 | static ATTRIBUTE_PURE int |
| 390 | filename_is_in_pattern (const char *filename, const char *pattern) |
| 391 | { |
| 392 | char *filename_copy, *pattern_copy; |
| 393 | |
| 394 | filename_copy = alloca (strlen (filename) + 1); |
| 395 | strcpy (filename_copy, filename); |
| 396 | pattern_copy = alloca (strlen (pattern) + 1); |
| 397 | strcpy (pattern_copy, pattern); |
| 398 | |
| 399 | return filename_is_in_pattern_1 (filename_copy, pattern_copy); |
| 400 | } |
| 401 | |
| 402 | /* Return 1 if FILENAME belongs to one of directory components of |
| 403 | AUTO_LOAD_SAFE_PATH_VEC. Return 0 otherwise. |
| 404 | auto_load_safe_path_vec_update is never called. |
| 405 | *FILENAME_REALP may be updated by gdb_realpath of FILENAME - it has to be |
| 406 | freed by the caller. */ |
| 407 | |
| 408 | static int |
| 409 | filename_is_in_auto_load_safe_path_vec (const char *filename, |
| 410 | char **filename_realp) |
| 411 | { |
| 412 | char *pattern; |
| 413 | int ix; |
| 414 | |
| 415 | for (ix = 0; VEC_iterate (char_ptr, auto_load_safe_path_vec, ix, pattern); |
| 416 | ++ix) |
| 417 | if (*filename_realp == NULL && filename_is_in_pattern (filename, pattern)) |
| 418 | break; |
| 419 | |
| 420 | if (pattern == NULL) |
| 421 | { |
| 422 | if (*filename_realp == NULL) |
| 423 | { |
| 424 | *filename_realp = gdb_realpath (filename); |
| 425 | if (debug_auto_load && strcmp (*filename_realp, filename) != 0) |
| 426 | fprintf_unfiltered (gdb_stdlog, |
| 427 | _("auto-load: Resolved " |
| 428 | "file \"%s\" as \"%s\".\n"), |
| 429 | filename, *filename_realp); |
| 430 | } |
| 431 | |
| 432 | if (strcmp (*filename_realp, filename) != 0) |
| 433 | for (ix = 0; |
| 434 | VEC_iterate (char_ptr, auto_load_safe_path_vec, ix, pattern); ++ix) |
| 435 | if (filename_is_in_pattern (*filename_realp, pattern)) |
| 436 | break; |
| 437 | } |
| 438 | |
| 439 | if (pattern != NULL) |
| 440 | { |
| 441 | if (debug_auto_load) |
| 442 | fprintf_unfiltered (gdb_stdlog, _("auto-load: File \"%s\" matches " |
| 443 | "directory \"%s\".\n"), |
| 444 | filename, pattern); |
| 445 | return 1; |
| 446 | } |
| 447 | |
| 448 | return 0; |
| 449 | } |
| 450 | |
| 451 | /* Return 1 if FILENAME is located in one of the directories of |
| 452 | AUTO_LOAD_SAFE_PATH. Otherwise call warning and return 0. FILENAME does |
| 453 | not have to be an absolute path. |
| 454 | |
| 455 | Existence of FILENAME is not checked. Function will still give a warning |
| 456 | even if the caller would quietly skip non-existing file in unsafe |
| 457 | directory. */ |
| 458 | |
| 459 | int |
| 460 | file_is_auto_load_safe (const char *filename, const char *debug_fmt, ...) |
| 461 | { |
| 462 | char *filename_real = NULL; |
| 463 | struct cleanup *back_to; |
| 464 | static int advice_printed = 0; |
| 465 | |
| 466 | if (debug_auto_load) |
| 467 | { |
| 468 | va_list debug_args; |
| 469 | |
| 470 | va_start (debug_args, debug_fmt); |
| 471 | vfprintf_unfiltered (gdb_stdlog, debug_fmt, debug_args); |
| 472 | va_end (debug_args); |
| 473 | } |
| 474 | |
| 475 | back_to = make_cleanup (free_current_contents, &filename_real); |
| 476 | |
| 477 | if (filename_is_in_auto_load_safe_path_vec (filename, &filename_real)) |
| 478 | { |
| 479 | do_cleanups (back_to); |
| 480 | return 1; |
| 481 | } |
| 482 | |
| 483 | auto_load_safe_path_vec_update (); |
| 484 | if (filename_is_in_auto_load_safe_path_vec (filename, &filename_real)) |
| 485 | { |
| 486 | do_cleanups (back_to); |
| 487 | return 1; |
| 488 | } |
| 489 | |
| 490 | warning (_("File \"%s\" auto-loading has been declined by your " |
| 491 | "`auto-load safe-path' set to \"%s\"."), |
| 492 | filename_real, auto_load_safe_path); |
| 493 | |
| 494 | if (!advice_printed) |
| 495 | { |
| 496 | const char *homedir = getenv ("HOME"); |
| 497 | char *homeinit; |
| 498 | |
| 499 | if (homedir == NULL) |
| 500 | homedir = "$HOME"; |
| 501 | homeinit = xstrprintf ("%s/%s", homedir, gdbinit); |
| 502 | make_cleanup (xfree, homeinit); |
| 503 | |
| 504 | printf_filtered (_("\ |
| 505 | To enable execution of this file add\n\ |
| 506 | \tadd-auto-load-safe-path %s\n\ |
| 507 | line to your configuration file \"%s\".\n\ |
| 508 | To completely disable this security protection add\n\ |
| 509 | \tset auto-load safe-path /\n\ |
| 510 | line to your configuration file \"%s\".\n\ |
| 511 | For more information about this security protection see the\n\ |
| 512 | \"Auto-loading safe path\" section in the GDB manual. E.g., run from the shell:\n\ |
| 513 | \tinfo \"(gdb)Auto-loading safe path\"\n"), |
| 514 | filename_real, homeinit, homeinit); |
| 515 | advice_printed = 1; |
| 516 | } |
| 517 | |
| 518 | do_cleanups (back_to); |
| 519 | return 0; |
| 520 | } |
| 521 | |
| 522 | /* For scripts specified in .debug_gdb_scripts, multiple objfiles may load |
| 523 | the same script. There's no point in loading the script multiple times, |
| 524 | and there can be a lot of objfiles and scripts, so we keep track of scripts |
| 525 | loaded this way. */ |
| 526 | |
| 527 | struct auto_load_pspace_info |
| 528 | { |
| 529 | /* For each program space we keep track of loaded scripts. */ |
| 530 | struct htab *loaded_scripts; |
| 531 | |
| 532 | /* Non-zero if we've issued the warning about an auto-load script not being |
| 533 | supported. We only want to issue this warning once. */ |
| 534 | int unsupported_script_warning_printed; |
| 535 | |
| 536 | /* Non-zero if we've issued the warning about an auto-load script not being |
| 537 | found. We only want to issue this warning once. */ |
| 538 | int script_not_found_warning_printed; |
| 539 | }; |
| 540 | |
| 541 | /* Objects of this type are stored in the loaded script hash table. */ |
| 542 | |
| 543 | struct loaded_script |
| 544 | { |
| 545 | /* Name as provided by the objfile. */ |
| 546 | const char *name; |
| 547 | |
| 548 | /* Full path name or NULL if script wasn't found (or was otherwise |
| 549 | inaccessible). */ |
| 550 | const char *full_path; |
| 551 | |
| 552 | /* Non-zero if this script has been loaded. */ |
| 553 | int loaded; |
| 554 | |
| 555 | const struct extension_language_defn *language; |
| 556 | }; |
| 557 | |
| 558 | /* Per-program-space data key. */ |
| 559 | static const struct program_space_data *auto_load_pspace_data; |
| 560 | |
| 561 | static void |
| 562 | auto_load_pspace_data_cleanup (struct program_space *pspace, void *arg) |
| 563 | { |
| 564 | struct auto_load_pspace_info *info = arg; |
| 565 | |
| 566 | if (info->loaded_scripts) |
| 567 | htab_delete (info->loaded_scripts); |
| 568 | xfree (info); |
| 569 | } |
| 570 | |
| 571 | /* Get the current autoload data. If none is found yet, add it now. This |
| 572 | function always returns a valid object. */ |
| 573 | |
| 574 | static struct auto_load_pspace_info * |
| 575 | get_auto_load_pspace_data (struct program_space *pspace) |
| 576 | { |
| 577 | struct auto_load_pspace_info *info; |
| 578 | |
| 579 | info = program_space_data (pspace, auto_load_pspace_data); |
| 580 | if (info == NULL) |
| 581 | { |
| 582 | info = XCNEW (struct auto_load_pspace_info); |
| 583 | set_program_space_data (pspace, auto_load_pspace_data, info); |
| 584 | } |
| 585 | |
| 586 | return info; |
| 587 | } |
| 588 | |
| 589 | /* Hash function for the loaded script hash. */ |
| 590 | |
| 591 | static hashval_t |
| 592 | hash_loaded_script_entry (const void *data) |
| 593 | { |
| 594 | const struct loaded_script *e = data; |
| 595 | |
| 596 | return htab_hash_string (e->name) ^ htab_hash_pointer (e->language); |
| 597 | } |
| 598 | |
| 599 | /* Equality function for the loaded script hash. */ |
| 600 | |
| 601 | static int |
| 602 | eq_loaded_script_entry (const void *a, const void *b) |
| 603 | { |
| 604 | const struct loaded_script *ea = a; |
| 605 | const struct loaded_script *eb = b; |
| 606 | |
| 607 | return strcmp (ea->name, eb->name) == 0 && ea->language == eb->language; |
| 608 | } |
| 609 | |
| 610 | /* Initialize the table to track loaded scripts. |
| 611 | Each entry is hashed by the full path name. */ |
| 612 | |
| 613 | static void |
| 614 | init_loaded_scripts_info (struct auto_load_pspace_info *pspace_info) |
| 615 | { |
| 616 | /* Choose 31 as the starting size of the hash table, somewhat arbitrarily. |
| 617 | Space for each entry is obtained with one malloc so we can free them |
| 618 | easily. */ |
| 619 | |
| 620 | pspace_info->loaded_scripts = htab_create (31, |
| 621 | hash_loaded_script_entry, |
| 622 | eq_loaded_script_entry, |
| 623 | xfree); |
| 624 | |
| 625 | pspace_info->unsupported_script_warning_printed = FALSE; |
| 626 | pspace_info->script_not_found_warning_printed = FALSE; |
| 627 | } |
| 628 | |
| 629 | /* Wrapper on get_auto_load_pspace_data to also allocate the hash table |
| 630 | for loading scripts. */ |
| 631 | |
| 632 | struct auto_load_pspace_info * |
| 633 | get_auto_load_pspace_data_for_loading (struct program_space *pspace) |
| 634 | { |
| 635 | struct auto_load_pspace_info *info; |
| 636 | |
| 637 | info = get_auto_load_pspace_data (pspace); |
| 638 | if (info->loaded_scripts == NULL) |
| 639 | init_loaded_scripts_info (info); |
| 640 | |
| 641 | return info; |
| 642 | } |
| 643 | |
| 644 | /* Add script NAME in LANGUAGE to hash table of PSPACE_INFO. LOADED 1 if the |
| 645 | script has been (is going to) be loaded, 0 otherwise (such as if it has not |
| 646 | been found). FULL_PATH is NULL if the script wasn't found. The result is |
| 647 | true if the script was already in the hash table. */ |
| 648 | |
| 649 | static int |
| 650 | maybe_add_script (struct auto_load_pspace_info *pspace_info, int loaded, |
| 651 | const char *name, const char *full_path, |
| 652 | const struct extension_language_defn *language) |
| 653 | { |
| 654 | struct htab *htab = pspace_info->loaded_scripts; |
| 655 | struct loaded_script **slot, entry; |
| 656 | int in_hash_table; |
| 657 | |
| 658 | entry.name = name; |
| 659 | entry.language = language; |
| 660 | slot = (struct loaded_script **) htab_find_slot (htab, &entry, INSERT); |
| 661 | in_hash_table = *slot != NULL; |
| 662 | |
| 663 | /* If this script is not in the hash table, add it. */ |
| 664 | |
| 665 | if (! in_hash_table) |
| 666 | { |
| 667 | char *p; |
| 668 | |
| 669 | /* Allocate all space in one chunk so it's easier to free. */ |
| 670 | *slot = xmalloc (sizeof (**slot) |
| 671 | + strlen (name) + 1 |
| 672 | + (full_path != NULL ? (strlen (full_path) + 1) : 0)); |
| 673 | p = ((char*) *slot) + sizeof (**slot); |
| 674 | strcpy (p, name); |
| 675 | (*slot)->name = p; |
| 676 | if (full_path != NULL) |
| 677 | { |
| 678 | p += strlen (p) + 1; |
| 679 | strcpy (p, full_path); |
| 680 | (*slot)->full_path = p; |
| 681 | } |
| 682 | else |
| 683 | (*slot)->full_path = NULL; |
| 684 | (*slot)->loaded = loaded; |
| 685 | (*slot)->language = language; |
| 686 | } |
| 687 | |
| 688 | return in_hash_table; |
| 689 | } |
| 690 | |
| 691 | /* Clear the table of loaded section scripts. */ |
| 692 | |
| 693 | static void |
| 694 | clear_section_scripts (void) |
| 695 | { |
| 696 | struct program_space *pspace = current_program_space; |
| 697 | struct auto_load_pspace_info *info; |
| 698 | |
| 699 | info = program_space_data (pspace, auto_load_pspace_data); |
| 700 | if (info != NULL && info->loaded_scripts != NULL) |
| 701 | { |
| 702 | htab_delete (info->loaded_scripts); |
| 703 | info->loaded_scripts = NULL; |
| 704 | info->unsupported_script_warning_printed = FALSE; |
| 705 | info->script_not_found_warning_printed = FALSE; |
| 706 | } |
| 707 | } |
| 708 | |
| 709 | /* Look for the auto-load script in LANGUAGE associated with OBJFILE where |
| 710 | OBJFILE's gdb_realpath is REALNAME and load it. Return 1 if we found any |
| 711 | matching script, return 0 otherwise. */ |
| 712 | |
| 713 | static int |
| 714 | auto_load_objfile_script_1 (struct objfile *objfile, const char *realname, |
| 715 | const struct extension_language_defn *language) |
| 716 | { |
| 717 | char *filename, *debugfile; |
| 718 | int len, retval; |
| 719 | FILE *input; |
| 720 | struct cleanup *cleanups; |
| 721 | const char *suffix = ext_lang_auto_load_suffix (language); |
| 722 | |
| 723 | len = strlen (realname); |
| 724 | filename = xmalloc (len + strlen (suffix) + 1); |
| 725 | memcpy (filename, realname, len); |
| 726 | strcpy (filename + len, suffix); |
| 727 | |
| 728 | cleanups = make_cleanup (xfree, filename); |
| 729 | |
| 730 | input = gdb_fopen_cloexec (filename, "r"); |
| 731 | debugfile = filename; |
| 732 | if (debug_auto_load) |
| 733 | fprintf_unfiltered (gdb_stdlog, _("auto-load: Attempted file \"%s\" %s.\n"), |
| 734 | debugfile, input ? _("exists") : _("does not exist")); |
| 735 | |
| 736 | if (!input) |
| 737 | { |
| 738 | VEC (char_ptr) *vec; |
| 739 | int ix; |
| 740 | char *dir; |
| 741 | |
| 742 | /* Also try the same file in a subdirectory of gdb's data |
| 743 | directory. */ |
| 744 | |
| 745 | vec = auto_load_expand_dir_vars (auto_load_dir); |
| 746 | make_cleanup_free_char_ptr_vec (vec); |
| 747 | |
| 748 | if (debug_auto_load) |
| 749 | fprintf_unfiltered (gdb_stdlog, _("auto-load: Searching 'set auto-load " |
| 750 | "scripts-directory' path \"%s\".\n"), |
| 751 | auto_load_dir); |
| 752 | |
| 753 | for (ix = 0; VEC_iterate (char_ptr, vec, ix, dir); ++ix) |
| 754 | { |
| 755 | debugfile = xmalloc (strlen (dir) + strlen (filename) + 1); |
| 756 | strcpy (debugfile, dir); |
| 757 | |
| 758 | /* FILENAME is absolute, so we don't need a "/" here. */ |
| 759 | strcat (debugfile, filename); |
| 760 | |
| 761 | make_cleanup (xfree, debugfile); |
| 762 | input = gdb_fopen_cloexec (debugfile, "r"); |
| 763 | if (debug_auto_load) |
| 764 | fprintf_unfiltered (gdb_stdlog, _("auto-load: Attempted file " |
| 765 | "\"%s\" %s.\n"), |
| 766 | debugfile, |
| 767 | input ? _("exists") : _("does not exist")); |
| 768 | if (input != NULL) |
| 769 | break; |
| 770 | } |
| 771 | } |
| 772 | |
| 773 | if (input) |
| 774 | { |
| 775 | int is_safe; |
| 776 | struct auto_load_pspace_info *pspace_info; |
| 777 | |
| 778 | make_cleanup_fclose (input); |
| 779 | |
| 780 | is_safe |
| 781 | = file_is_auto_load_safe (filename, |
| 782 | _("auto-load: Loading %s script \"%s\"" |
| 783 | " by extension for objfile \"%s\".\n"), |
| 784 | ext_lang_name (language), |
| 785 | filename, objfile_name (objfile)); |
| 786 | |
| 787 | /* Add this script to the hash table too so |
| 788 | "info auto-load ${lang}-scripts" can print it. */ |
| 789 | pspace_info |
| 790 | = get_auto_load_pspace_data_for_loading (current_program_space); |
| 791 | maybe_add_script (pspace_info, is_safe, filename, filename, language); |
| 792 | |
| 793 | /* To preserve existing behaviour we don't check for whether the |
| 794 | script was already in the table, and always load it. |
| 795 | It's highly unlikely that we'd ever load it twice, |
| 796 | and these scripts are required to be idempotent under multiple |
| 797 | loads anyway. */ |
| 798 | if (is_safe) |
| 799 | { |
| 800 | objfile_script_sourcer_func *sourcer |
| 801 | = ext_lang_objfile_script_sourcer (language); |
| 802 | |
| 803 | /* We shouldn't get here if support for the language isn't |
| 804 | compiled in. And the extension language is required to implement |
| 805 | this function. */ |
| 806 | gdb_assert (sourcer != NULL); |
| 807 | sourcer (language, objfile, input, debugfile); |
| 808 | } |
| 809 | |
| 810 | retval = 1; |
| 811 | } |
| 812 | else |
| 813 | retval = 0; |
| 814 | |
| 815 | do_cleanups (cleanups); |
| 816 | return retval; |
| 817 | } |
| 818 | |
| 819 | /* Look for the auto-load script in LANGUAGE associated with OBJFILE and load |
| 820 | it. */ |
| 821 | |
| 822 | void |
| 823 | auto_load_objfile_script (struct objfile *objfile, |
| 824 | const struct extension_language_defn *language) |
| 825 | { |
| 826 | char *realname = gdb_realpath (objfile_name (objfile)); |
| 827 | struct cleanup *cleanups = make_cleanup (xfree, realname); |
| 828 | |
| 829 | if (!auto_load_objfile_script_1 (objfile, realname, language)) |
| 830 | { |
| 831 | /* For Windows/DOS .exe executables, strip the .exe suffix, so that |
| 832 | FOO-gdb.gdb could be used for FOO.exe, and try again. */ |
| 833 | |
| 834 | size_t len = strlen (realname); |
| 835 | const size_t lexe = sizeof (".exe") - 1; |
| 836 | |
| 837 | if (len > lexe && strcasecmp (realname + len - lexe, ".exe") == 0) |
| 838 | { |
| 839 | len -= lexe; |
| 840 | realname[len] = '\0'; |
| 841 | if (debug_auto_load) |
| 842 | fprintf_unfiltered (gdb_stdlog, _("auto-load: Stripped .exe suffix, " |
| 843 | "retrying with \"%s\".\n"), |
| 844 | realname); |
| 845 | auto_load_objfile_script_1 (objfile, realname, language); |
| 846 | } |
| 847 | } |
| 848 | |
| 849 | do_cleanups (cleanups); |
| 850 | } |
| 851 | |
| 852 | /* Load scripts specified in OBJFILE. |
| 853 | START,END delimit a buffer containing a list of nul-terminated |
| 854 | file names. |
| 855 | SECTION_NAME is used in error messages. |
| 856 | |
| 857 | Scripts are found per normal "source -s" command processing. |
| 858 | First the script is looked for in $cwd. If not found there the |
| 859 | source search path is used. |
| 860 | |
| 861 | The section contains a list of path names of script files to load. |
| 862 | Each path is null-terminated. */ |
| 863 | |
| 864 | static void |
| 865 | source_section_scripts (struct objfile *objfile, const char *section_name, |
| 866 | const char *start, const char *end) |
| 867 | { |
| 868 | const char *p; |
| 869 | struct auto_load_pspace_info *pspace_info; |
| 870 | |
| 871 | pspace_info = get_auto_load_pspace_data_for_loading (current_program_space); |
| 872 | |
| 873 | for (p = start; p < end; ++p) |
| 874 | { |
| 875 | const char *file; |
| 876 | FILE *stream; |
| 877 | char *full_path; |
| 878 | int opened, in_hash_table; |
| 879 | struct cleanup *back_to; |
| 880 | const struct extension_language_defn *language; |
| 881 | objfile_script_sourcer_func *sourcer; |
| 882 | |
| 883 | switch (*p) |
| 884 | { |
| 885 | case SECTION_SCRIPT_ID_PYTHON_FILE: |
| 886 | language = get_ext_lang_defn (EXT_LANG_PYTHON); |
| 887 | break; |
| 888 | case SECTION_SCRIPT_ID_SCHEME_FILE: |
| 889 | language = get_ext_lang_defn (EXT_LANG_GUILE); |
| 890 | break; |
| 891 | default: |
| 892 | warning (_("Invalid entry in %s section"), section_name); |
| 893 | /* We could try various heuristics to find the next valid entry, |
| 894 | but it's safer to just punt. */ |
| 895 | return; |
| 896 | } |
| 897 | file = ++p; |
| 898 | |
| 899 | while (p < end && *p != '\0') |
| 900 | ++p; |
| 901 | if (p == end) |
| 902 | { |
| 903 | char *buf = alloca (p - file + 1); |
| 904 | |
| 905 | memcpy (buf, file, p - file); |
| 906 | buf[p - file] = '\0'; |
| 907 | warning (_("Non-null-terminated path in %s: %s"), |
| 908 | section_name, buf); |
| 909 | /* Don't load it. */ |
| 910 | break; |
| 911 | } |
| 912 | if (p == file) |
| 913 | { |
| 914 | warning (_("Empty path in %s"), section_name); |
| 915 | continue; |
| 916 | } |
| 917 | |
| 918 | /* Until we support more types of records in .debug_gdb_scripts we do |
| 919 | all the processing here. The expectation is to add a new |
| 920 | extension_language_script_ops "method" that handles all the records |
| 921 | for the language. For now we can just use |
| 922 | extension_language_script_ops.objfile_script_sourcer. */ |
| 923 | |
| 924 | /* Skip this script if support is not compiled in. */ |
| 925 | sourcer = ext_lang_objfile_script_sourcer (language); |
| 926 | if (sourcer == NULL) |
| 927 | { |
| 928 | /* We don't throw an error, the program is still debuggable. */ |
| 929 | if (!unsupported_script_warning_print (pspace_info)) |
| 930 | { |
| 931 | warning (_("Unsupported auto-load scripts referenced in" |
| 932 | " %s section\n" |
| 933 | "of file %s.\n" |
| 934 | "Use `info auto-load %s-scripts [REGEXP]'" |
| 935 | " to list them."), |
| 936 | section_name, objfile_name (objfile), |
| 937 | ext_lang_name (language)); |
| 938 | } |
| 939 | /* We *could* still try to open it, but there's no point. */ |
| 940 | maybe_add_script (pspace_info, 0, file, NULL, language); |
| 941 | continue; |
| 942 | } |
| 943 | |
| 944 | /* Skip this script if auto-loading it has been disabled. */ |
| 945 | if (!ext_lang_auto_load_enabled (language)) |
| 946 | { |
| 947 | /* No message is printed, just skip it. */ |
| 948 | continue; |
| 949 | } |
| 950 | |
| 951 | opened = find_and_open_script (file, 1 /*search_path*/, |
| 952 | &stream, &full_path); |
| 953 | |
| 954 | back_to = make_cleanup (null_cleanup, NULL); |
| 955 | if (opened) |
| 956 | { |
| 957 | make_cleanup_fclose (stream); |
| 958 | make_cleanup (xfree, full_path); |
| 959 | |
| 960 | if (!file_is_auto_load_safe (full_path, |
| 961 | _("auto-load: Loading %s script " |
| 962 | "\"%s\" from section \"%s\" of " |
| 963 | "objfile \"%s\".\n"), |
| 964 | ext_lang_name (language), full_path, |
| 965 | section_name, objfile_name (objfile))) |
| 966 | opened = 0; |
| 967 | } |
| 968 | else |
| 969 | { |
| 970 | full_path = NULL; |
| 971 | |
| 972 | /* If one script isn't found it's not uncommon for more to not be |
| 973 | found either. We don't want to print a message for each script, |
| 974 | too much noise. Instead, we print the warning once and tell the |
| 975 | user how to find the list of scripts that weren't loaded. |
| 976 | We don't throw an error, the program is still debuggable. |
| 977 | |
| 978 | IWBN if complaints.c were more general-purpose. */ |
| 979 | |
| 980 | if (script_not_found_warning_print (pspace_info)) |
| 981 | warning (_("Missing auto-load scripts referenced in section %s\n\ |
| 982 | of file %s\n\ |
| 983 | Use `info auto-load %s-scripts [REGEXP]' to list them."), |
| 984 | section_name, objfile_name (objfile), |
| 985 | ext_lang_name (language)); |
| 986 | } |
| 987 | |
| 988 | in_hash_table = maybe_add_script (pspace_info, opened, file, full_path, |
| 989 | language); |
| 990 | |
| 991 | /* If this file is not currently loaded, load it. */ |
| 992 | if (opened && !in_hash_table) |
| 993 | sourcer (language, objfile, stream, full_path); |
| 994 | |
| 995 | do_cleanups (back_to); |
| 996 | } |
| 997 | } |
| 998 | |
| 999 | /* Load scripts specified in section SECTION_NAME of OBJFILE. */ |
| 1000 | |
| 1001 | static void |
| 1002 | auto_load_section_scripts (struct objfile *objfile, const char *section_name) |
| 1003 | { |
| 1004 | bfd *abfd = objfile->obfd; |
| 1005 | asection *scripts_sect; |
| 1006 | bfd_byte *data = NULL; |
| 1007 | |
| 1008 | scripts_sect = bfd_get_section_by_name (abfd, section_name); |
| 1009 | if (scripts_sect == NULL) |
| 1010 | return; |
| 1011 | |
| 1012 | if (!bfd_get_full_section_contents (abfd, scripts_sect, &data)) |
| 1013 | warning (_("Couldn't read %s section of %s"), |
| 1014 | section_name, bfd_get_filename (abfd)); |
| 1015 | else |
| 1016 | { |
| 1017 | struct cleanup *cleanups; |
| 1018 | char *p = (char *) data; |
| 1019 | |
| 1020 | cleanups = make_cleanup (xfree, p); |
| 1021 | source_section_scripts (objfile, section_name, p, |
| 1022 | p + bfd_get_section_size (scripts_sect)); |
| 1023 | do_cleanups (cleanups); |
| 1024 | } |
| 1025 | } |
| 1026 | |
| 1027 | /* Load any auto-loaded scripts for OBJFILE. */ |
| 1028 | |
| 1029 | void |
| 1030 | load_auto_scripts_for_objfile (struct objfile *objfile) |
| 1031 | { |
| 1032 | /* Return immediately if auto-loading has been globally disabled. |
| 1033 | This is to handle sequencing of operations during gdb startup. |
| 1034 | Also return immediately if OBJFILE is not actually a file. */ |
| 1035 | if (!global_auto_load || (objfile->flags & OBJF_NOT_FILENAME) != 0) |
| 1036 | return; |
| 1037 | |
| 1038 | /* Load any extension language scripts for this objfile. |
| 1039 | E.g., foo-gdb.gdb, foo-gdb.py. */ |
| 1040 | auto_load_ext_lang_scripts_for_objfile (objfile); |
| 1041 | |
| 1042 | /* Load any scripts mentioned in AUTO_SECTION_NAME (.debug_gdb_scripts). */ |
| 1043 | auto_load_section_scripts (objfile, AUTO_SECTION_NAME); |
| 1044 | } |
| 1045 | |
| 1046 | /* This is a new_objfile observer callback to auto-load scripts. |
| 1047 | |
| 1048 | Two flavors of auto-loaded scripts are supported. |
| 1049 | 1) based on the path to the objfile |
| 1050 | 2) from .debug_gdb_scripts section */ |
| 1051 | |
| 1052 | static void |
| 1053 | auto_load_new_objfile (struct objfile *objfile) |
| 1054 | { |
| 1055 | if (!objfile) |
| 1056 | { |
| 1057 | /* OBJFILE is NULL when loading a new "main" symbol-file. */ |
| 1058 | clear_section_scripts (); |
| 1059 | return; |
| 1060 | } |
| 1061 | |
| 1062 | load_auto_scripts_for_objfile (objfile); |
| 1063 | } |
| 1064 | |
| 1065 | /* Collect scripts to be printed in a vec. */ |
| 1066 | |
| 1067 | typedef struct loaded_script *loaded_script_ptr; |
| 1068 | DEF_VEC_P (loaded_script_ptr); |
| 1069 | |
| 1070 | struct collect_matching_scripts_data |
| 1071 | { |
| 1072 | VEC (loaded_script_ptr) **scripts_p; |
| 1073 | |
| 1074 | const struct extension_language_defn *language; |
| 1075 | }; |
| 1076 | |
| 1077 | /* Traversal function for htab_traverse. |
| 1078 | Collect the entry if it matches the regexp. */ |
| 1079 | |
| 1080 | static int |
| 1081 | collect_matching_scripts (void **slot, void *info) |
| 1082 | { |
| 1083 | struct loaded_script *script = *slot; |
| 1084 | struct collect_matching_scripts_data *data = info; |
| 1085 | |
| 1086 | if (script->language == data->language && re_exec (script->name)) |
| 1087 | VEC_safe_push (loaded_script_ptr, *data->scripts_p, script); |
| 1088 | |
| 1089 | return 1; |
| 1090 | } |
| 1091 | |
| 1092 | /* Print SCRIPT. */ |
| 1093 | |
| 1094 | static void |
| 1095 | print_script (struct loaded_script *script) |
| 1096 | { |
| 1097 | struct ui_out *uiout = current_uiout; |
| 1098 | struct cleanup *chain; |
| 1099 | |
| 1100 | chain = make_cleanup_ui_out_tuple_begin_end (uiout, NULL); |
| 1101 | |
| 1102 | ui_out_field_string (uiout, "loaded", script->loaded ? "Yes" : "No"); |
| 1103 | ui_out_field_string (uiout, "script", script->name); |
| 1104 | ui_out_text (uiout, "\n"); |
| 1105 | |
| 1106 | /* If the name isn't the full path, print it too. */ |
| 1107 | if (script->full_path != NULL |
| 1108 | && strcmp (script->name, script->full_path) != 0) |
| 1109 | { |
| 1110 | ui_out_text (uiout, "\tfull name: "); |
| 1111 | ui_out_field_string (uiout, "full_path", script->full_path); |
| 1112 | ui_out_text (uiout, "\n"); |
| 1113 | } |
| 1114 | |
| 1115 | do_cleanups (chain); |
| 1116 | } |
| 1117 | |
| 1118 | /* Helper for info_auto_load_scripts to sort the scripts by name. */ |
| 1119 | |
| 1120 | static int |
| 1121 | sort_scripts_by_name (const void *ap, const void *bp) |
| 1122 | { |
| 1123 | const struct loaded_script *a = *(const struct loaded_script **) ap; |
| 1124 | const struct loaded_script *b = *(const struct loaded_script **) bp; |
| 1125 | |
| 1126 | return FILENAME_CMP (a->name, b->name); |
| 1127 | } |
| 1128 | |
| 1129 | /* Special internal GDB value of auto_load_info_scripts's PATTERN identify |
| 1130 | the "info auto-load XXX" command has been executed through the general |
| 1131 | "info auto-load" invocation. Extra newline will be printed if needed. */ |
| 1132 | char auto_load_info_scripts_pattern_nl[] = ""; |
| 1133 | |
| 1134 | /* Implementation for "info auto-load gdb-scripts" |
| 1135 | (and "info auto-load python-scripts"). List scripts in LANGUAGE matching |
| 1136 | PATTERN. FROM_TTY is the usual GDB boolean for user interactivity. */ |
| 1137 | |
| 1138 | void |
| 1139 | auto_load_info_scripts (char *pattern, int from_tty, |
| 1140 | const struct extension_language_defn *language) |
| 1141 | { |
| 1142 | struct ui_out *uiout = current_uiout; |
| 1143 | struct auto_load_pspace_info *pspace_info; |
| 1144 | struct cleanup *script_chain; |
| 1145 | VEC (loaded_script_ptr) *scripts; |
| 1146 | int nr_scripts; |
| 1147 | |
| 1148 | dont_repeat (); |
| 1149 | |
| 1150 | pspace_info = get_auto_load_pspace_data (current_program_space); |
| 1151 | |
| 1152 | if (pattern && *pattern) |
| 1153 | { |
| 1154 | char *re_err = re_comp (pattern); |
| 1155 | |
| 1156 | if (re_err) |
| 1157 | error (_("Invalid regexp: %s"), re_err); |
| 1158 | } |
| 1159 | else |
| 1160 | { |
| 1161 | re_comp (""); |
| 1162 | } |
| 1163 | |
| 1164 | /* We need to know the number of rows before we build the table. |
| 1165 | Plus we want to sort the scripts by name. |
| 1166 | So first traverse the hash table collecting the matching scripts. */ |
| 1167 | |
| 1168 | scripts = VEC_alloc (loaded_script_ptr, 10); |
| 1169 | script_chain = make_cleanup (VEC_cleanup (loaded_script_ptr), &scripts); |
| 1170 | |
| 1171 | if (pspace_info != NULL && pspace_info->loaded_scripts != NULL) |
| 1172 | { |
| 1173 | struct collect_matching_scripts_data data = { &scripts, language }; |
| 1174 | |
| 1175 | /* Pass a pointer to scripts as VEC_safe_push can realloc space. */ |
| 1176 | htab_traverse_noresize (pspace_info->loaded_scripts, |
| 1177 | collect_matching_scripts, &data); |
| 1178 | } |
| 1179 | |
| 1180 | nr_scripts = VEC_length (loaded_script_ptr, scripts); |
| 1181 | |
| 1182 | /* Table header shifted right by preceding "gdb-scripts: " would not match |
| 1183 | its columns. */ |
| 1184 | if (nr_scripts > 0 && pattern == auto_load_info_scripts_pattern_nl) |
| 1185 | ui_out_text (uiout, "\n"); |
| 1186 | |
| 1187 | make_cleanup_ui_out_table_begin_end (uiout, 2, nr_scripts, |
| 1188 | "AutoLoadedScriptsTable"); |
| 1189 | |
| 1190 | ui_out_table_header (uiout, 7, ui_left, "loaded", "Loaded"); |
| 1191 | ui_out_table_header (uiout, 70, ui_left, "script", "Script"); |
| 1192 | ui_out_table_body (uiout); |
| 1193 | |
| 1194 | if (nr_scripts > 0) |
| 1195 | { |
| 1196 | int i; |
| 1197 | loaded_script_ptr script; |
| 1198 | |
| 1199 | qsort (VEC_address (loaded_script_ptr, scripts), |
| 1200 | VEC_length (loaded_script_ptr, scripts), |
| 1201 | sizeof (loaded_script_ptr), sort_scripts_by_name); |
| 1202 | for (i = 0; VEC_iterate (loaded_script_ptr, scripts, i, script); ++i) |
| 1203 | print_script (script); |
| 1204 | } |
| 1205 | |
| 1206 | do_cleanups (script_chain); |
| 1207 | |
| 1208 | if (nr_scripts == 0) |
| 1209 | { |
| 1210 | if (pattern && *pattern) |
| 1211 | ui_out_message (uiout, 0, "No auto-load scripts matching %s.\n", |
| 1212 | pattern); |
| 1213 | else |
| 1214 | ui_out_message (uiout, 0, "No auto-load scripts.\n"); |
| 1215 | } |
| 1216 | } |
| 1217 | |
| 1218 | /* Wrapper for "info auto-load gdb-scripts". */ |
| 1219 | |
| 1220 | static void |
| 1221 | info_auto_load_gdb_scripts (char *pattern, int from_tty) |
| 1222 | { |
| 1223 | auto_load_info_scripts (pattern, from_tty, &extension_language_gdb); |
| 1224 | } |
| 1225 | |
| 1226 | /* Implement 'info auto-load local-gdbinit'. */ |
| 1227 | |
| 1228 | static void |
| 1229 | info_auto_load_local_gdbinit (char *args, int from_tty) |
| 1230 | { |
| 1231 | if (auto_load_local_gdbinit_pathname == NULL) |
| 1232 | printf_filtered (_("Local .gdbinit file was not found.\n")); |
| 1233 | else if (auto_load_local_gdbinit_loaded) |
| 1234 | printf_filtered (_("Local .gdbinit file \"%s\" has been loaded.\n"), |
| 1235 | auto_load_local_gdbinit_pathname); |
| 1236 | else |
| 1237 | printf_filtered (_("Local .gdbinit file \"%s\" has not been loaded.\n"), |
| 1238 | auto_load_local_gdbinit_pathname); |
| 1239 | } |
| 1240 | |
| 1241 | /* Return non-zero if UNSUPPORTED_SCRIPT_WARNING_PRINTED of PSPACE_INFO was |
| 1242 | unset before calling this function. Always set |
| 1243 | UNSUPPORTED_SCRIPT_WARNING_PRINTED of PSPACE_INFO. */ |
| 1244 | |
| 1245 | static int |
| 1246 | unsupported_script_warning_print (struct auto_load_pspace_info *pspace_info) |
| 1247 | { |
| 1248 | int retval = !pspace_info->unsupported_script_warning_printed; |
| 1249 | |
| 1250 | pspace_info->unsupported_script_warning_printed = 1; |
| 1251 | |
| 1252 | return retval; |
| 1253 | } |
| 1254 | |
| 1255 | /* Return non-zero if SCRIPT_NOT_FOUND_WARNING_PRINTED of PSPACE_INFO was unset |
| 1256 | before calling this function. Always set SCRIPT_NOT_FOUND_WARNING_PRINTED |
| 1257 | of PSPACE_INFO. */ |
| 1258 | |
| 1259 | static int |
| 1260 | script_not_found_warning_print (struct auto_load_pspace_info *pspace_info) |
| 1261 | { |
| 1262 | int retval = !pspace_info->script_not_found_warning_printed; |
| 1263 | |
| 1264 | pspace_info->script_not_found_warning_printed = 1; |
| 1265 | |
| 1266 | return retval; |
| 1267 | } |
| 1268 | |
| 1269 | /* The only valid "set auto-load" argument is off|0|no|disable. */ |
| 1270 | |
| 1271 | static void |
| 1272 | set_auto_load_cmd (char *args, int from_tty) |
| 1273 | { |
| 1274 | struct cmd_list_element *list; |
| 1275 | size_t length; |
| 1276 | |
| 1277 | /* See parse_binary_operation in use by the sub-commands. */ |
| 1278 | |
| 1279 | length = args ? strlen (args) : 0; |
| 1280 | |
| 1281 | while (length > 0 && (args[length - 1] == ' ' || args[length - 1] == '\t')) |
| 1282 | length--; |
| 1283 | |
| 1284 | if (length == 0 || (strncmp (args, "off", length) != 0 |
| 1285 | && strncmp (args, "0", length) != 0 |
| 1286 | && strncmp (args, "no", length) != 0 |
| 1287 | && strncmp (args, "disable", length) != 0)) |
| 1288 | error (_("Valid is only global 'set auto-load no'; " |
| 1289 | "otherwise check the auto-load sub-commands.")); |
| 1290 | |
| 1291 | for (list = *auto_load_set_cmdlist_get (); list != NULL; list = list->next) |
| 1292 | if (list->var_type == var_boolean) |
| 1293 | { |
| 1294 | gdb_assert (list->type == set_cmd); |
| 1295 | do_set_command (args, from_tty, list); |
| 1296 | } |
| 1297 | } |
| 1298 | |
| 1299 | /* Initialize "set auto-load " commands prefix and return it. */ |
| 1300 | |
| 1301 | struct cmd_list_element ** |
| 1302 | auto_load_set_cmdlist_get (void) |
| 1303 | { |
| 1304 | static struct cmd_list_element *retval; |
| 1305 | |
| 1306 | if (retval == NULL) |
| 1307 | add_prefix_cmd ("auto-load", class_maintenance, set_auto_load_cmd, _("\ |
| 1308 | Auto-loading specific settings.\n\ |
| 1309 | Configure various auto-load-specific variables such as\n\ |
| 1310 | automatic loading of Python scripts."), |
| 1311 | &retval, "set auto-load ", |
| 1312 | 1/*allow-unknown*/, &setlist); |
| 1313 | |
| 1314 | return &retval; |
| 1315 | } |
| 1316 | |
| 1317 | /* Command "show auto-load" displays summary of all the current |
| 1318 | "show auto-load " settings. */ |
| 1319 | |
| 1320 | static void |
| 1321 | show_auto_load_cmd (char *args, int from_tty) |
| 1322 | { |
| 1323 | cmd_show_list (*auto_load_show_cmdlist_get (), from_tty, ""); |
| 1324 | } |
| 1325 | |
| 1326 | /* Initialize "show auto-load " commands prefix and return it. */ |
| 1327 | |
| 1328 | struct cmd_list_element ** |
| 1329 | auto_load_show_cmdlist_get (void) |
| 1330 | { |
| 1331 | static struct cmd_list_element *retval; |
| 1332 | |
| 1333 | if (retval == NULL) |
| 1334 | add_prefix_cmd ("auto-load", class_maintenance, show_auto_load_cmd, _("\ |
| 1335 | Show auto-loading specific settings.\n\ |
| 1336 | Show configuration of various auto-load-specific variables such as\n\ |
| 1337 | automatic loading of Python scripts."), |
| 1338 | &retval, "show auto-load ", |
| 1339 | 0/*allow-unknown*/, &showlist); |
| 1340 | |
| 1341 | return &retval; |
| 1342 | } |
| 1343 | |
| 1344 | /* Command "info auto-load" displays whether the various auto-load files have |
| 1345 | been loaded. This is reimplementation of cmd_show_list which inserts |
| 1346 | newlines at proper places. */ |
| 1347 | |
| 1348 | static void |
| 1349 | info_auto_load_cmd (char *args, int from_tty) |
| 1350 | { |
| 1351 | struct cmd_list_element *list; |
| 1352 | struct cleanup *infolist_chain; |
| 1353 | struct ui_out *uiout = current_uiout; |
| 1354 | |
| 1355 | infolist_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "infolist"); |
| 1356 | |
| 1357 | for (list = *auto_load_info_cmdlist_get (); list != NULL; list = list->next) |
| 1358 | { |
| 1359 | struct cleanup *option_chain |
| 1360 | = make_cleanup_ui_out_tuple_begin_end (uiout, "option"); |
| 1361 | |
| 1362 | gdb_assert (!list->prefixlist); |
| 1363 | gdb_assert (list->type == not_set_cmd); |
| 1364 | |
| 1365 | ui_out_field_string (uiout, "name", list->name); |
| 1366 | ui_out_text (uiout, ": "); |
| 1367 | cmd_func (list, auto_load_info_scripts_pattern_nl, from_tty); |
| 1368 | |
| 1369 | /* Close the tuple. */ |
| 1370 | do_cleanups (option_chain); |
| 1371 | } |
| 1372 | |
| 1373 | /* Close the tuple. */ |
| 1374 | do_cleanups (infolist_chain); |
| 1375 | } |
| 1376 | |
| 1377 | /* Initialize "info auto-load " commands prefix and return it. */ |
| 1378 | |
| 1379 | struct cmd_list_element ** |
| 1380 | auto_load_info_cmdlist_get (void) |
| 1381 | { |
| 1382 | static struct cmd_list_element *retval; |
| 1383 | |
| 1384 | if (retval == NULL) |
| 1385 | add_prefix_cmd ("auto-load", class_info, info_auto_load_cmd, _("\ |
| 1386 | Print current status of auto-loaded files.\n\ |
| 1387 | Print whether various files like Python scripts or .gdbinit files have been\n\ |
| 1388 | found and/or loaded."), |
| 1389 | &retval, "info auto-load ", |
| 1390 | 0/*allow-unknown*/, &infolist); |
| 1391 | |
| 1392 | return &retval; |
| 1393 | } |
| 1394 | |
| 1395 | void _initialize_auto_load (void); |
| 1396 | |
| 1397 | void |
| 1398 | _initialize_auto_load (void) |
| 1399 | { |
| 1400 | struct cmd_list_element *cmd; |
| 1401 | char *scripts_directory_help, *gdb_name_help, *python_name_help; |
| 1402 | char *guile_name_help; |
| 1403 | const char *suffix; |
| 1404 | |
| 1405 | auto_load_pspace_data |
| 1406 | = register_program_space_data_with_cleanup (NULL, |
| 1407 | auto_load_pspace_data_cleanup); |
| 1408 | |
| 1409 | observer_attach_new_objfile (auto_load_new_objfile); |
| 1410 | |
| 1411 | add_setshow_boolean_cmd ("gdb-scripts", class_support, |
| 1412 | &auto_load_gdb_scripts, _("\ |
| 1413 | Enable or disable auto-loading of canned sequences of commands scripts."), _("\ |
| 1414 | Show whether auto-loading of canned sequences of commands scripts is enabled."), |
| 1415 | _("\ |
| 1416 | If enabled, canned sequences of commands are loaded when the debugger reads\n\ |
| 1417 | an executable or shared library.\n\ |
| 1418 | This options has security implications for untrusted inferiors."), |
| 1419 | NULL, show_auto_load_gdb_scripts, |
| 1420 | auto_load_set_cmdlist_get (), |
| 1421 | auto_load_show_cmdlist_get ()); |
| 1422 | |
| 1423 | add_cmd ("gdb-scripts", class_info, info_auto_load_gdb_scripts, |
| 1424 | _("Print the list of automatically loaded sequences of commands.\n\ |
| 1425 | Usage: info auto-load gdb-scripts [REGEXP]"), |
| 1426 | auto_load_info_cmdlist_get ()); |
| 1427 | |
| 1428 | add_setshow_boolean_cmd ("local-gdbinit", class_support, |
| 1429 | &auto_load_local_gdbinit, _("\ |
| 1430 | Enable or disable auto-loading of .gdbinit script in current directory."), _("\ |
| 1431 | Show whether auto-loading .gdbinit script in current directory is enabled."), |
| 1432 | _("\ |
| 1433 | If enabled, canned sequences of commands are loaded when debugger starts\n\ |
| 1434 | from .gdbinit file in current directory. Such files are deprecated,\n\ |
| 1435 | use a script associated with inferior executable file instead.\n\ |
| 1436 | This options has security implications for untrusted inferiors."), |
| 1437 | NULL, show_auto_load_local_gdbinit, |
| 1438 | auto_load_set_cmdlist_get (), |
| 1439 | auto_load_show_cmdlist_get ()); |
| 1440 | |
| 1441 | add_cmd ("local-gdbinit", class_info, info_auto_load_local_gdbinit, |
| 1442 | _("Print whether current directory .gdbinit file has been loaded.\n\ |
| 1443 | Usage: info auto-load local-gdbinit"), |
| 1444 | auto_load_info_cmdlist_get ()); |
| 1445 | |
| 1446 | auto_load_dir = xstrdup (AUTO_LOAD_DIR); |
| 1447 | |
| 1448 | suffix = ext_lang_auto_load_suffix (get_ext_lang_defn (EXT_LANG_GDB)); |
| 1449 | gdb_name_help |
| 1450 | = xstrprintf (_("\ |
| 1451 | GDB scripts: OBJFILE%s\n"), |
| 1452 | suffix); |
| 1453 | python_name_help = NULL; |
| 1454 | #ifdef HAVE_PYTHON |
| 1455 | suffix = ext_lang_auto_load_suffix (get_ext_lang_defn (EXT_LANG_PYTHON)); |
| 1456 | python_name_help |
| 1457 | = xstrprintf (_("\ |
| 1458 | Python scripts: OBJFILE%s\n"), |
| 1459 | suffix); |
| 1460 | #endif |
| 1461 | guile_name_help = NULL; |
| 1462 | #ifdef HAVE_GUILE |
| 1463 | suffix = ext_lang_auto_load_suffix (get_ext_lang_defn (EXT_LANG_GUILE)); |
| 1464 | guile_name_help |
| 1465 | = xstrprintf (_("\ |
| 1466 | Guile scripts: OBJFILE%s\n"), |
| 1467 | suffix); |
| 1468 | #endif |
| 1469 | scripts_directory_help |
| 1470 | = xstrprintf (_("\ |
| 1471 | Automatically loaded scripts are located in one of the directories listed\n\ |
| 1472 | by this option.\n\ |
| 1473 | \n\ |
| 1474 | Script names:\n\ |
| 1475 | %s%s%s\ |
| 1476 | \n\ |
| 1477 | This option is ignored for the kinds of scripts \ |
| 1478 | having 'set auto-load ... off'.\n\ |
| 1479 | Directories listed here need to be present also \ |
| 1480 | in the 'set auto-load safe-path'\n\ |
| 1481 | option."), |
| 1482 | gdb_name_help, |
| 1483 | python_name_help ? python_name_help : "", |
| 1484 | guile_name_help ? guile_name_help : ""); |
| 1485 | |
| 1486 | add_setshow_optional_filename_cmd ("scripts-directory", class_support, |
| 1487 | &auto_load_dir, _("\ |
| 1488 | Set the list of directories from which to load auto-loaded scripts."), _("\ |
| 1489 | Show the list of directories from which to load auto-loaded scripts."), |
| 1490 | scripts_directory_help, |
| 1491 | set_auto_load_dir, show_auto_load_dir, |
| 1492 | auto_load_set_cmdlist_get (), |
| 1493 | auto_load_show_cmdlist_get ()); |
| 1494 | xfree (scripts_directory_help); |
| 1495 | xfree (python_name_help); |
| 1496 | xfree (gdb_name_help); |
| 1497 | xfree (guile_name_help); |
| 1498 | |
| 1499 | auto_load_safe_path = xstrdup (AUTO_LOAD_SAFE_PATH); |
| 1500 | auto_load_safe_path_vec_update (); |
| 1501 | add_setshow_optional_filename_cmd ("safe-path", class_support, |
| 1502 | &auto_load_safe_path, _("\ |
| 1503 | Set the list of files and directories that are safe for auto-loading."), _("\ |
| 1504 | Show the list of files and directories that are safe for auto-loading."), _("\ |
| 1505 | Various files loaded automatically for the 'set auto-load ...' options must\n\ |
| 1506 | be located in one of the directories listed by this option. Warning will be\n\ |
| 1507 | printed and file will not be used otherwise.\n\ |
| 1508 | You can mix both directory and filename entries.\n\ |
| 1509 | Setting this parameter to an empty list resets it to its default value.\n\ |
| 1510 | Setting this parameter to '/' (without the quotes) allows any file\n\ |
| 1511 | for the 'set auto-load ...' options. Each path entry can be also shell\n\ |
| 1512 | wildcard pattern; '*' does not match directory separator.\n\ |
| 1513 | This option is ignored for the kinds of files having 'set auto-load ... off'.\n\ |
| 1514 | This options has security implications for untrusted inferiors."), |
| 1515 | set_auto_load_safe_path, |
| 1516 | show_auto_load_safe_path, |
| 1517 | auto_load_set_cmdlist_get (), |
| 1518 | auto_load_show_cmdlist_get ()); |
| 1519 | observer_attach_gdb_datadir_changed (auto_load_gdb_datadir_changed); |
| 1520 | |
| 1521 | cmd = add_cmd ("add-auto-load-safe-path", class_support, |
| 1522 | add_auto_load_safe_path, |
| 1523 | _("Add entries to the list of directories from which it is safe " |
| 1524 | "to auto-load files.\n\ |
| 1525 | See the commands 'set auto-load safe-path' and 'show auto-load safe-path' to\n\ |
| 1526 | access the current full list setting."), |
| 1527 | &cmdlist); |
| 1528 | set_cmd_completer (cmd, filename_completer); |
| 1529 | |
| 1530 | add_setshow_boolean_cmd ("auto-load", class_maintenance, |
| 1531 | &debug_auto_load, _("\ |
| 1532 | Set auto-load verifications debugging."), _("\ |
| 1533 | Show auto-load verifications debugging."), _("\ |
| 1534 | When non-zero, debugging output for files of 'set auto-load ...'\n\ |
| 1535 | is displayed."), |
| 1536 | NULL, show_debug_auto_load, |
| 1537 | &setdebuglist, &showdebuglist); |
| 1538 | } |