GDB (API)
|
00001 /* GDB routines for supporting auto-loaded scripts. 00002 00003 Copyright (C) 2012-2013 Free Software Foundation, Inc. 00004 00005 This file is part of GDB. 00006 00007 This program is free software; you can redistribute it and/or modify 00008 it under the terms of the GNU General Public License as published by 00009 the Free Software Foundation; either version 3 of the License, or 00010 (at your option) any later version. 00011 00012 This program is distributed in the hope that it will be useful, 00013 but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 GNU General Public License for more details. 00016 00017 You should have received a copy of the GNU General Public License 00018 along with this program. If not, see <http://www.gnu.org/licenses/>. */ 00019 00020 #include "defs.h" 00021 #include "auto-load.h" 00022 #include "progspace.h" 00023 #include "python/python.h" 00024 #include "gdb_regex.h" 00025 #include "ui-out.h" 00026 #include "filenames.h" 00027 #include "command.h" 00028 #include "observer.h" 00029 #include "objfiles.h" 00030 #include "exceptions.h" 00031 #include "cli/cli-script.h" 00032 #include "gdbcmd.h" 00033 #include "cli/cli-decode.h" 00034 #include "cli/cli-setshow.h" 00035 #include "gdb_vecs.h" 00036 #include "readline/tilde.h" 00037 #include "completer.h" 00038 #include "observer.h" 00039 #include "fnmatch.h" 00040 #include "top.h" 00041 #include "filestuff.h" 00042 00043 /* The suffix of per-objfile scripts to auto-load as non-Python command files. 00044 E.g. When the program loads libfoo.so, look for libfoo-gdb.gdb. */ 00045 #define GDB_AUTO_FILE_NAME "-gdb.gdb" 00046 00047 static void source_gdb_script_for_objfile (struct objfile *objfile, FILE *file, 00048 const char *filename); 00049 00050 /* Value of the 'set debug auto-load' configuration variable. */ 00051 static int debug_auto_load = 0; 00052 00053 /* "show" command for the debug_auto_load configuration variable. */ 00054 00055 static void 00056 show_debug_auto_load (struct ui_file *file, int from_tty, 00057 struct cmd_list_element *c, const char *value) 00058 { 00059 fprintf_filtered (file, _("Debugging output for files " 00060 "of 'set auto-load ...' is %s.\n"), 00061 value); 00062 } 00063 00064 /* User-settable option to enable/disable auto-loading of GDB_AUTO_FILE_NAME 00065 scripts: 00066 set auto-load gdb-scripts on|off 00067 This is true if we should auto-load associated scripts when an objfile 00068 is opened, false otherwise. */ 00069 static int auto_load_gdb_scripts = 1; 00070 00071 /* "show" command for the auto_load_gdb_scripts configuration variable. */ 00072 00073 static void 00074 show_auto_load_gdb_scripts (struct ui_file *file, int from_tty, 00075 struct cmd_list_element *c, const char *value) 00076 { 00077 fprintf_filtered (file, _("Auto-loading of canned sequences of commands " 00078 "scripts is %s.\n"), 00079 value); 00080 } 00081 00082 /* Internal-use flag to enable/disable auto-loading. 00083 This is true if we should auto-load python code when an objfile is opened, 00084 false otherwise. 00085 00086 Both auto_load_scripts && global_auto_load must be true to enable 00087 auto-loading. 00088 00089 This flag exists to facilitate deferring auto-loading during start-up 00090 until after ./.gdbinit has been read; it may augment the search directories 00091 used to find the scripts. */ 00092 int global_auto_load = 1; 00093 00094 /* Auto-load .gdbinit file from the current directory? */ 00095 int auto_load_local_gdbinit = 1; 00096 00097 /* Absolute pathname to the current directory .gdbinit, if it exists. */ 00098 char *auto_load_local_gdbinit_pathname = NULL; 00099 00100 /* Boolean value if AUTO_LOAD_LOCAL_GDBINIT_PATHNAME has been loaded. */ 00101 int auto_load_local_gdbinit_loaded = 0; 00102 00103 /* "show" command for the auto_load_local_gdbinit configuration variable. */ 00104 00105 static void 00106 show_auto_load_local_gdbinit (struct ui_file *file, int from_tty, 00107 struct cmd_list_element *c, const char *value) 00108 { 00109 fprintf_filtered (file, _("Auto-loading of .gdbinit script from current " 00110 "directory is %s.\n"), 00111 value); 00112 } 00113 00114 /* Directory list from which to load auto-loaded scripts. It is not checked 00115 for absolute paths but they are strongly recommended. It is initialized by 00116 _initialize_auto_load. */ 00117 static char *auto_load_dir; 00118 00119 /* "set" command for the auto_load_dir configuration variable. */ 00120 00121 static void 00122 set_auto_load_dir (char *args, int from_tty, struct cmd_list_element *c) 00123 { 00124 /* Setting the variable to "" resets it to the compile time defaults. */ 00125 if (auto_load_dir[0] == '\0') 00126 { 00127 xfree (auto_load_dir); 00128 auto_load_dir = xstrdup (AUTO_LOAD_DIR); 00129 } 00130 } 00131 00132 /* "show" command for the auto_load_dir configuration variable. */ 00133 00134 static void 00135 show_auto_load_dir (struct ui_file *file, int from_tty, 00136 struct cmd_list_element *c, const char *value) 00137 { 00138 fprintf_filtered (file, _("List of directories from which to load " 00139 "auto-loaded scripts is %s.\n"), 00140 value); 00141 } 00142 00143 /* Directory list safe to hold auto-loaded files. It is not checked for 00144 absolute paths but they are strongly recommended. It is initialized by 00145 _initialize_auto_load. */ 00146 static char *auto_load_safe_path; 00147 00148 /* Vector of directory elements of AUTO_LOAD_SAFE_PATH with each one normalized 00149 by tilde_expand and possibly each entries has added its gdb_realpath 00150 counterpart. */ 00151 static VEC (char_ptr) *auto_load_safe_path_vec; 00152 00153 /* Expand $datadir and $debugdir in STRING according to the rules of 00154 substitute_path_component. Return vector from dirnames_to_char_ptr_vec, 00155 this vector must be freed by free_char_ptr_vec by the caller. */ 00156 00157 static VEC (char_ptr) * 00158 auto_load_expand_dir_vars (const char *string) 00159 { 00160 VEC (char_ptr) *dir_vec; 00161 char *s; 00162 00163 s = xstrdup (string); 00164 substitute_path_component (&s, "$datadir", gdb_datadir); 00165 substitute_path_component (&s, "$debugdir", debug_file_directory); 00166 00167 if (debug_auto_load && strcmp (s, string) != 0) 00168 fprintf_unfiltered (gdb_stdlog, 00169 _("auto-load: Expanded $-variables to \"%s\".\n"), s); 00170 00171 dir_vec = dirnames_to_char_ptr_vec (s); 00172 xfree(s); 00173 00174 return dir_vec; 00175 } 00176 00177 /* Update auto_load_safe_path_vec from current AUTO_LOAD_SAFE_PATH. */ 00178 00179 static void 00180 auto_load_safe_path_vec_update (void) 00181 { 00182 unsigned len; 00183 int ix; 00184 00185 if (debug_auto_load) 00186 fprintf_unfiltered (gdb_stdlog, 00187 _("auto-load: Updating directories of \"%s\".\n"), 00188 auto_load_safe_path); 00189 00190 free_char_ptr_vec (auto_load_safe_path_vec); 00191 00192 auto_load_safe_path_vec = auto_load_expand_dir_vars (auto_load_safe_path); 00193 len = VEC_length (char_ptr, auto_load_safe_path_vec); 00194 00195 /* Apply tilde_expand and gdb_realpath to each AUTO_LOAD_SAFE_PATH_VEC 00196 element. */ 00197 for (ix = 0; ix < len; ix++) 00198 { 00199 char *dir = VEC_index (char_ptr, auto_load_safe_path_vec, ix); 00200 char *expanded = tilde_expand (dir); 00201 char *real_path = gdb_realpath (expanded); 00202 00203 /* Ensure the current entry is at least tilde_expand-ed. */ 00204 VEC_replace (char_ptr, auto_load_safe_path_vec, ix, expanded); 00205 00206 if (debug_auto_load) 00207 { 00208 if (strcmp (expanded, dir) == 0) 00209 fprintf_unfiltered (gdb_stdlog, 00210 _("auto-load: Using directory \"%s\".\n"), 00211 expanded); 00212 else 00213 fprintf_unfiltered (gdb_stdlog, 00214 _("auto-load: Resolved directory \"%s\" " 00215 "as \"%s\".\n"), 00216 dir, expanded); 00217 } 00218 xfree (dir); 00219 00220 /* If gdb_realpath returns a different content, append it. */ 00221 if (strcmp (real_path, expanded) == 0) 00222 xfree (real_path); 00223 else 00224 { 00225 VEC_safe_push (char_ptr, auto_load_safe_path_vec, real_path); 00226 00227 if (debug_auto_load) 00228 fprintf_unfiltered (gdb_stdlog, 00229 _("auto-load: And canonicalized as \"%s\".\n"), 00230 real_path); 00231 } 00232 } 00233 } 00234 00235 /* Variable gdb_datadir has been set. Update content depending on $datadir. */ 00236 00237 static void 00238 auto_load_gdb_datadir_changed (void) 00239 { 00240 auto_load_safe_path_vec_update (); 00241 } 00242 00243 /* "set" command for the auto_load_safe_path configuration variable. */ 00244 00245 static void 00246 set_auto_load_safe_path (char *args, int from_tty, struct cmd_list_element *c) 00247 { 00248 /* Setting the variable to "" resets it to the compile time defaults. */ 00249 if (auto_load_safe_path[0] == '\0') 00250 { 00251 xfree (auto_load_safe_path); 00252 auto_load_safe_path = xstrdup (AUTO_LOAD_SAFE_PATH); 00253 } 00254 00255 auto_load_safe_path_vec_update (); 00256 } 00257 00258 /* "show" command for the auto_load_safe_path configuration variable. */ 00259 00260 static void 00261 show_auto_load_safe_path (struct ui_file *file, int from_tty, 00262 struct cmd_list_element *c, const char *value) 00263 { 00264 const char *cs; 00265 00266 /* Check if user has entered either "/" or for example ":". 00267 But while more complicate content like ":/foo" would still also 00268 permit any location do not hide those. */ 00269 00270 for (cs = value; *cs && (*cs == DIRNAME_SEPARATOR || IS_DIR_SEPARATOR (*cs)); 00271 cs++); 00272 if (*cs == 0) 00273 fprintf_filtered (file, _("Auto-load files are safe to load from any " 00274 "directory.\n")); 00275 else 00276 fprintf_filtered (file, _("List of directories from which it is safe to " 00277 "auto-load files is %s.\n"), 00278 value); 00279 } 00280 00281 /* "add-auto-load-safe-path" command for the auto_load_safe_path configuration 00282 variable. */ 00283 00284 static void 00285 add_auto_load_safe_path (char *args, int from_tty) 00286 { 00287 char *s; 00288 00289 if (args == NULL || *args == 0) 00290 error (_("\ 00291 Directory argument required.\n\ 00292 Use 'set auto-load safe-path /' for disabling the auto-load safe-path security.\ 00293 ")); 00294 00295 s = xstrprintf ("%s%c%s", auto_load_safe_path, DIRNAME_SEPARATOR, args); 00296 xfree (auto_load_safe_path); 00297 auto_load_safe_path = s; 00298 00299 auto_load_safe_path_vec_update (); 00300 } 00301 00302 /* Implementation for filename_is_in_pattern overwriting the caller's FILENAME 00303 and PATTERN. */ 00304 00305 static int 00306 filename_is_in_pattern_1 (char *filename, char *pattern) 00307 { 00308 size_t pattern_len = strlen (pattern); 00309 size_t filename_len = strlen (filename); 00310 00311 if (debug_auto_load) 00312 fprintf_unfiltered (gdb_stdlog, _("auto-load: Matching file \"%s\" " 00313 "to pattern \"%s\"\n"), 00314 filename, pattern); 00315 00316 /* Trim trailing slashes ("/") from PATTERN. Even for "d:\" paths as 00317 trailing slashes are trimmed also from FILENAME it still matches 00318 correctly. */ 00319 while (pattern_len && IS_DIR_SEPARATOR (pattern[pattern_len - 1])) 00320 pattern_len--; 00321 pattern[pattern_len] = '\0'; 00322 00323 /* Ensure auto_load_safe_path "/" matches any FILENAME. On MS-Windows 00324 platform FILENAME even after gdb_realpath does not have to start with 00325 IS_DIR_SEPARATOR character, such as the 'C:\x.exe' filename. */ 00326 if (pattern_len == 0) 00327 { 00328 if (debug_auto_load) 00329 fprintf_unfiltered (gdb_stdlog, 00330 _("auto-load: Matched - empty pattern\n")); 00331 return 1; 00332 } 00333 00334 for (;;) 00335 { 00336 /* Trim trailing slashes ("/"). PATTERN also has slashes trimmed the 00337 same way so they will match. */ 00338 while (filename_len && IS_DIR_SEPARATOR (filename[filename_len - 1])) 00339 filename_len--; 00340 filename[filename_len] = '\0'; 00341 if (filename_len == 0) 00342 { 00343 if (debug_auto_load) 00344 fprintf_unfiltered (gdb_stdlog, 00345 _("auto-load: Not matched - pattern \"%s\".\n"), 00346 pattern); 00347 return 0; 00348 } 00349 00350 if (gdb_filename_fnmatch (pattern, filename, FNM_FILE_NAME | FNM_NOESCAPE) 00351 == 0) 00352 { 00353 if (debug_auto_load) 00354 fprintf_unfiltered (gdb_stdlog, _("auto-load: Matched - file " 00355 "\"%s\" to pattern \"%s\".\n"), 00356 filename, pattern); 00357 return 1; 00358 } 00359 00360 /* Trim trailing FILENAME component. */ 00361 while (filename_len > 0 && !IS_DIR_SEPARATOR (filename[filename_len - 1])) 00362 filename_len--; 00363 } 00364 } 00365 00366 /* Return 1 if FILENAME matches PATTERN or if FILENAME resides in 00367 a subdirectory of a directory that matches PATTERN. Return 0 otherwise. 00368 gdb_realpath normalization is never done here. */ 00369 00370 static ATTRIBUTE_PURE int 00371 filename_is_in_pattern (const char *filename, const char *pattern) 00372 { 00373 char *filename_copy, *pattern_copy; 00374 00375 filename_copy = alloca (strlen (filename) + 1); 00376 strcpy (filename_copy, filename); 00377 pattern_copy = alloca (strlen (pattern) + 1); 00378 strcpy (pattern_copy, pattern); 00379 00380 return filename_is_in_pattern_1 (filename_copy, pattern_copy); 00381 } 00382 00383 /* Return 1 if FILENAME belongs to one of directory components of 00384 AUTO_LOAD_SAFE_PATH_VEC. Return 0 otherwise. 00385 auto_load_safe_path_vec_update is never called. 00386 *FILENAME_REALP may be updated by gdb_realpath of FILENAME - it has to be 00387 freed by the caller. */ 00388 00389 static int 00390 filename_is_in_auto_load_safe_path_vec (const char *filename, 00391 char **filename_realp) 00392 { 00393 char *pattern; 00394 int ix; 00395 00396 for (ix = 0; VEC_iterate (char_ptr, auto_load_safe_path_vec, ix, pattern); 00397 ++ix) 00398 if (*filename_realp == NULL && filename_is_in_pattern (filename, pattern)) 00399 break; 00400 00401 if (pattern == NULL) 00402 { 00403 if (*filename_realp == NULL) 00404 { 00405 *filename_realp = gdb_realpath (filename); 00406 if (debug_auto_load && strcmp (*filename_realp, filename) != 0) 00407 fprintf_unfiltered (gdb_stdlog, 00408 _("auto-load: Resolved " 00409 "file \"%s\" as \"%s\".\n"), 00410 filename, *filename_realp); 00411 } 00412 00413 if (strcmp (*filename_realp, filename) != 0) 00414 for (ix = 0; 00415 VEC_iterate (char_ptr, auto_load_safe_path_vec, ix, pattern); ++ix) 00416 if (filename_is_in_pattern (*filename_realp, pattern)) 00417 break; 00418 } 00419 00420 if (pattern != NULL) 00421 { 00422 if (debug_auto_load) 00423 fprintf_unfiltered (gdb_stdlog, _("auto-load: File \"%s\" matches " 00424 "directory \"%s\".\n"), 00425 filename, pattern); 00426 return 1; 00427 } 00428 00429 return 0; 00430 } 00431 00432 /* Return 1 if FILENAME is located in one of the directories of 00433 AUTO_LOAD_SAFE_PATH. Otherwise call warning and return 0. FILENAME does 00434 not have to be an absolute path. 00435 00436 Existence of FILENAME is not checked. Function will still give a warning 00437 even if the caller would quietly skip non-existing file in unsafe 00438 directory. */ 00439 00440 int 00441 file_is_auto_load_safe (const char *filename, const char *debug_fmt, ...) 00442 { 00443 char *filename_real = NULL; 00444 struct cleanup *back_to; 00445 static int advice_printed = 0; 00446 00447 if (debug_auto_load) 00448 { 00449 va_list debug_args; 00450 00451 va_start (debug_args, debug_fmt); 00452 vfprintf_unfiltered (gdb_stdlog, debug_fmt, debug_args); 00453 va_end (debug_args); 00454 } 00455 00456 back_to = make_cleanup (free_current_contents, &filename_real); 00457 00458 if (filename_is_in_auto_load_safe_path_vec (filename, &filename_real)) 00459 { 00460 do_cleanups (back_to); 00461 return 1; 00462 } 00463 00464 auto_load_safe_path_vec_update (); 00465 if (filename_is_in_auto_load_safe_path_vec (filename, &filename_real)) 00466 { 00467 do_cleanups (back_to); 00468 return 1; 00469 } 00470 00471 warning (_("File \"%s\" auto-loading has been declined by your " 00472 "`auto-load safe-path' set to \"%s\"."), 00473 filename_real, auto_load_safe_path); 00474 00475 if (!advice_printed) 00476 { 00477 const char *homedir = getenv ("HOME"); 00478 char *homeinit; 00479 00480 if (homedir == NULL) 00481 homedir = "$HOME"; 00482 homeinit = xstrprintf ("%s/%s", homedir, gdbinit); 00483 make_cleanup (xfree, homeinit); 00484 00485 printf_filtered (_("\ 00486 To enable execution of this file add\n\ 00487 \tadd-auto-load-safe-path %s\n\ 00488 line to your configuration file \"%s\".\n\ 00489 To completely disable this security protection add\n\ 00490 \tset auto-load safe-path /\n\ 00491 line to your configuration file \"%s\".\n\ 00492 For more information about this security protection see the\n\ 00493 \"Auto-loading safe path\" section in the GDB manual. E.g., run from the shell:\n\ 00494 \tinfo \"(gdb)Auto-loading safe path\"\n"), 00495 filename_real, homeinit, homeinit); 00496 advice_printed = 1; 00497 } 00498 00499 do_cleanups (back_to); 00500 return 0; 00501 } 00502 00503 /* Definition of script language for GDB canned sequences of commands. */ 00504 00505 static const struct script_language script_language_gdb 00506 = { GDB_AUTO_FILE_NAME, source_gdb_script_for_objfile }; 00507 00508 static void 00509 source_gdb_script_for_objfile (struct objfile *objfile, FILE *file, 00510 const char *filename) 00511 { 00512 int is_safe; 00513 struct auto_load_pspace_info *pspace_info; 00514 volatile struct gdb_exception e; 00515 00516 is_safe = file_is_auto_load_safe (filename, _("auto-load: Loading canned " 00517 "sequences of commands script " 00518 "\"%s\" for objfile \"%s\".\n"), 00519 filename, objfile_name (objfile)); 00520 00521 /* Add this script to the hash table too so "info auto-load gdb-scripts" 00522 can print it. */ 00523 pspace_info = get_auto_load_pspace_data_for_loading (current_program_space); 00524 maybe_add_script (pspace_info, is_safe, filename, filename, 00525 &script_language_gdb); 00526 00527 if (!is_safe) 00528 return; 00529 00530 TRY_CATCH (e, RETURN_MASK_ALL) 00531 { 00532 script_from_file (file, filename); 00533 } 00534 exception_print (gdb_stderr, e); 00535 } 00536 00537 /* For scripts specified in .debug_gdb_scripts, multiple objfiles may load 00538 the same script. There's no point in loading the script multiple times, 00539 and there can be a lot of objfiles and scripts, so we keep track of scripts 00540 loaded this way. */ 00541 00542 struct auto_load_pspace_info 00543 { 00544 /* For each program space we keep track of loaded scripts. */ 00545 struct htab *loaded_scripts; 00546 00547 /* Non-zero if we've issued the warning about an auto-load script not being 00548 found. We only want to issue this warning once. */ 00549 int script_not_found_warning_printed; 00550 }; 00551 00552 /* Objects of this type are stored in the loaded script hash table. */ 00553 00554 struct loaded_script 00555 { 00556 /* Name as provided by the objfile. */ 00557 const char *name; 00558 00559 /* Full path name or NULL if script wasn't found (or was otherwise 00560 inaccessible). */ 00561 const char *full_path; 00562 00563 /* Non-zero if this script has been loaded. */ 00564 int loaded; 00565 00566 const struct script_language *language; 00567 }; 00568 00569 /* Per-program-space data key. */ 00570 static const struct program_space_data *auto_load_pspace_data; 00571 00572 static void 00573 auto_load_pspace_data_cleanup (struct program_space *pspace, void *arg) 00574 { 00575 struct auto_load_pspace_info *info; 00576 00577 info = program_space_data (pspace, auto_load_pspace_data); 00578 if (info != NULL) 00579 { 00580 if (info->loaded_scripts) 00581 htab_delete (info->loaded_scripts); 00582 xfree (info); 00583 } 00584 } 00585 00586 /* Get the current autoload data. If none is found yet, add it now. This 00587 function always returns a valid object. */ 00588 00589 static struct auto_load_pspace_info * 00590 get_auto_load_pspace_data (struct program_space *pspace) 00591 { 00592 struct auto_load_pspace_info *info; 00593 00594 info = program_space_data (pspace, auto_load_pspace_data); 00595 if (info == NULL) 00596 { 00597 info = XZALLOC (struct auto_load_pspace_info); 00598 set_program_space_data (pspace, auto_load_pspace_data, info); 00599 } 00600 00601 return info; 00602 } 00603 00604 /* Hash function for the loaded script hash. */ 00605 00606 static hashval_t 00607 hash_loaded_script_entry (const void *data) 00608 { 00609 const struct loaded_script *e = data; 00610 00611 return htab_hash_string (e->name) ^ htab_hash_pointer (e->language); 00612 } 00613 00614 /* Equality function for the loaded script hash. */ 00615 00616 static int 00617 eq_loaded_script_entry (const void *a, const void *b) 00618 { 00619 const struct loaded_script *ea = a; 00620 const struct loaded_script *eb = b; 00621 00622 return strcmp (ea->name, eb->name) == 0 && ea->language == eb->language; 00623 } 00624 00625 /* Initialize the table to track loaded scripts. 00626 Each entry is hashed by the full path name. */ 00627 00628 static void 00629 init_loaded_scripts_info (struct auto_load_pspace_info *pspace_info) 00630 { 00631 /* Choose 31 as the starting size of the hash table, somewhat arbitrarily. 00632 Space for each entry is obtained with one malloc so we can free them 00633 easily. */ 00634 00635 pspace_info->loaded_scripts = htab_create (31, 00636 hash_loaded_script_entry, 00637 eq_loaded_script_entry, 00638 xfree); 00639 00640 pspace_info->script_not_found_warning_printed = FALSE; 00641 } 00642 00643 /* Wrapper on get_auto_load_pspace_data to also allocate the hash table 00644 for loading scripts. */ 00645 00646 struct auto_load_pspace_info * 00647 get_auto_load_pspace_data_for_loading (struct program_space *pspace) 00648 { 00649 struct auto_load_pspace_info *info; 00650 00651 info = get_auto_load_pspace_data (pspace); 00652 if (info->loaded_scripts == NULL) 00653 init_loaded_scripts_info (info); 00654 00655 return info; 00656 } 00657 00658 /* Add script NAME in LANGUAGE to hash table of PSPACE_INFO. LOADED 1 if the 00659 script has been (is going to) be loaded, 0 otherwise (such as if it has not 00660 been found). FULL_PATH is NULL if the script wasn't found. The result is 00661 true if the script was already in the hash table. */ 00662 00663 int 00664 maybe_add_script (struct auto_load_pspace_info *pspace_info, int loaded, 00665 const char *name, const char *full_path, 00666 const struct script_language *language) 00667 { 00668 struct htab *htab = pspace_info->loaded_scripts; 00669 struct loaded_script **slot, entry; 00670 int in_hash_table; 00671 00672 entry.name = name; 00673 entry.language = language; 00674 slot = (struct loaded_script **) htab_find_slot (htab, &entry, INSERT); 00675 in_hash_table = *slot != NULL; 00676 00677 /* If this script is not in the hash table, add it. */ 00678 00679 if (! in_hash_table) 00680 { 00681 char *p; 00682 00683 /* Allocate all space in one chunk so it's easier to free. */ 00684 *slot = xmalloc (sizeof (**slot) 00685 + strlen (name) + 1 00686 + (full_path != NULL ? (strlen (full_path) + 1) : 0)); 00687 p = ((char*) *slot) + sizeof (**slot); 00688 strcpy (p, name); 00689 (*slot)->name = p; 00690 if (full_path != NULL) 00691 { 00692 p += strlen (p) + 1; 00693 strcpy (p, full_path); 00694 (*slot)->full_path = p; 00695 } 00696 else 00697 (*slot)->full_path = NULL; 00698 (*slot)->loaded = loaded; 00699 (*slot)->language = language; 00700 } 00701 00702 return in_hash_table; 00703 } 00704 00705 /* Clear the table of loaded section scripts. */ 00706 00707 static void 00708 clear_section_scripts (void) 00709 { 00710 struct program_space *pspace = current_program_space; 00711 struct auto_load_pspace_info *info; 00712 00713 info = program_space_data (pspace, auto_load_pspace_data); 00714 if (info != NULL && info->loaded_scripts != NULL) 00715 { 00716 htab_delete (info->loaded_scripts); 00717 info->loaded_scripts = NULL; 00718 info->script_not_found_warning_printed = FALSE; 00719 } 00720 } 00721 00722 /* Look for the auto-load script in LANGUAGE associated with OBJFILE where 00723 OBJFILE's gdb_realpath is REALNAME and load it. Return 1 if we found any 00724 matching script, return 0 otherwise. */ 00725 00726 static int 00727 auto_load_objfile_script_1 (struct objfile *objfile, const char *realname, 00728 const struct script_language *language) 00729 { 00730 char *filename, *debugfile; 00731 int len, retval; 00732 FILE *input; 00733 struct cleanup *cleanups; 00734 00735 len = strlen (realname); 00736 filename = xmalloc (len + strlen (language->suffix) + 1); 00737 memcpy (filename, realname, len); 00738 strcpy (filename + len, language->suffix); 00739 00740 cleanups = make_cleanup (xfree, filename); 00741 00742 input = gdb_fopen_cloexec (filename, "r"); 00743 debugfile = filename; 00744 if (debug_auto_load) 00745 fprintf_unfiltered (gdb_stdlog, _("auto-load: Attempted file \"%s\" %s.\n"), 00746 debugfile, input ? _("exists") : _("does not exist")); 00747 00748 if (!input) 00749 { 00750 VEC (char_ptr) *vec; 00751 int ix; 00752 char *dir; 00753 00754 /* Also try the same file in a subdirectory of gdb's data 00755 directory. */ 00756 00757 vec = auto_load_expand_dir_vars (auto_load_dir); 00758 make_cleanup_free_char_ptr_vec (vec); 00759 00760 if (debug_auto_load) 00761 fprintf_unfiltered (gdb_stdlog, _("auto-load: Searching 'set auto-load " 00762 "scripts-directory' path \"%s\".\n"), 00763 auto_load_dir); 00764 00765 for (ix = 0; VEC_iterate (char_ptr, vec, ix, dir); ++ix) 00766 { 00767 debugfile = xmalloc (strlen (dir) + strlen (filename) + 1); 00768 strcpy (debugfile, dir); 00769 00770 /* FILENAME is absolute, so we don't need a "/" here. */ 00771 strcat (debugfile, filename); 00772 00773 make_cleanup (xfree, debugfile); 00774 input = gdb_fopen_cloexec (debugfile, "r"); 00775 if (debug_auto_load) 00776 fprintf_unfiltered (gdb_stdlog, _("auto-load: Attempted file " 00777 "\"%s\" %s.\n"), 00778 debugfile, 00779 input ? _("exists") : _("does not exist")); 00780 if (input != NULL) 00781 break; 00782 } 00783 } 00784 00785 if (input) 00786 { 00787 make_cleanup_fclose (input); 00788 00789 /* To preserve existing behaviour we don't check for whether the 00790 script was already in the table, and always load it. 00791 It's highly unlikely that we'd ever load it twice, 00792 and these scripts are required to be idempotent under multiple 00793 loads anyway. */ 00794 language->source_script_for_objfile (objfile, input, debugfile); 00795 00796 retval = 1; 00797 } 00798 else 00799 retval = 0; 00800 00801 do_cleanups (cleanups); 00802 return retval; 00803 } 00804 00805 /* Look for the auto-load script in LANGUAGE associated with OBJFILE and load 00806 it. */ 00807 00808 void 00809 auto_load_objfile_script (struct objfile *objfile, 00810 const struct script_language *language) 00811 { 00812 char *realname = gdb_realpath (objfile_name (objfile)); 00813 struct cleanup *cleanups = make_cleanup (xfree, realname); 00814 00815 if (!auto_load_objfile_script_1 (objfile, realname, language)) 00816 { 00817 /* For Windows/DOS .exe executables, strip the .exe suffix, so that 00818 FOO-gdb.gdb could be used for FOO.exe, and try again. */ 00819 00820 size_t len = strlen (realname); 00821 const size_t lexe = sizeof (".exe") - 1; 00822 00823 if (len > lexe && strcasecmp (realname + len - lexe, ".exe") == 0) 00824 { 00825 len -= lexe; 00826 realname[len] = '\0'; 00827 if (debug_auto_load) 00828 fprintf_unfiltered (gdb_stdlog, _("auto-load: Stripped .exe suffix, " 00829 "retrying with \"%s\".\n"), 00830 realname); 00831 auto_load_objfile_script_1 (objfile, realname, language); 00832 } 00833 } 00834 00835 do_cleanups (cleanups); 00836 } 00837 00838 /* Load any auto-loaded scripts for OBJFILE. */ 00839 00840 void 00841 load_auto_scripts_for_objfile (struct objfile *objfile) 00842 { 00843 if (!global_auto_load || (objfile->flags & OBJF_NOT_FILENAME) != 0) 00844 return; 00845 00846 if (auto_load_gdb_scripts) 00847 auto_load_objfile_script (objfile, &script_language_gdb); 00848 00849 gdbpy_load_auto_scripts_for_objfile (objfile); 00850 } 00851 00852 /* This is a new_objfile observer callback to auto-load scripts. 00853 00854 Two flavors of auto-loaded scripts are supported. 00855 1) based on the path to the objfile 00856 2) from .debug_gdb_scripts section */ 00857 00858 static void 00859 auto_load_new_objfile (struct objfile *objfile) 00860 { 00861 if (!objfile) 00862 { 00863 /* OBJFILE is NULL when loading a new "main" symbol-file. */ 00864 clear_section_scripts (); 00865 return; 00866 } 00867 00868 load_auto_scripts_for_objfile (objfile); 00869 } 00870 00871 /* Collect scripts to be printed in a vec. */ 00872 00873 typedef struct loaded_script *loaded_script_ptr; 00874 DEF_VEC_P (loaded_script_ptr); 00875 00876 struct collect_matching_scripts_data 00877 { 00878 VEC (loaded_script_ptr) **scripts_p; 00879 00880 const struct script_language *language; 00881 }; 00882 00883 /* Traversal function for htab_traverse. 00884 Collect the entry if it matches the regexp. */ 00885 00886 static int 00887 collect_matching_scripts (void **slot, void *info) 00888 { 00889 struct loaded_script *script = *slot; 00890 struct collect_matching_scripts_data *data = info; 00891 00892 if (script->language == data->language && re_exec (script->name)) 00893 VEC_safe_push (loaded_script_ptr, *data->scripts_p, script); 00894 00895 return 1; 00896 } 00897 00898 /* Print SCRIPT. */ 00899 00900 static void 00901 print_script (struct loaded_script *script) 00902 { 00903 struct ui_out *uiout = current_uiout; 00904 struct cleanup *chain; 00905 00906 chain = make_cleanup_ui_out_tuple_begin_end (uiout, NULL); 00907 00908 ui_out_field_string (uiout, "loaded", script->loaded ? "Yes" : "No"); 00909 ui_out_field_string (uiout, "script", script->name); 00910 ui_out_text (uiout, "\n"); 00911 00912 /* If the name isn't the full path, print it too. */ 00913 if (script->full_path != NULL 00914 && strcmp (script->name, script->full_path) != 0) 00915 { 00916 ui_out_text (uiout, "\tfull name: "); 00917 ui_out_field_string (uiout, "full_path", script->full_path); 00918 ui_out_text (uiout, "\n"); 00919 } 00920 00921 do_cleanups (chain); 00922 } 00923 00924 /* Helper for info_auto_load_scripts to sort the scripts by name. */ 00925 00926 static int 00927 sort_scripts_by_name (const void *ap, const void *bp) 00928 { 00929 const struct loaded_script *a = *(const struct loaded_script **) ap; 00930 const struct loaded_script *b = *(const struct loaded_script **) bp; 00931 00932 return FILENAME_CMP (a->name, b->name); 00933 } 00934 00935 /* Special internal GDB value of auto_load_info_scripts's PATTERN identify 00936 the "info auto-load XXX" command has been executed through the general 00937 "info auto-load" invocation. Extra newline will be printed if needed. */ 00938 char auto_load_info_scripts_pattern_nl[] = ""; 00939 00940 /* Implementation for "info auto-load gdb-scripts" 00941 (and "info auto-load python-scripts"). List scripts in LANGUAGE matching 00942 PATTERN. FROM_TTY is the usual GDB boolean for user interactivity. */ 00943 00944 void 00945 auto_load_info_scripts (char *pattern, int from_tty, 00946 const struct script_language *language) 00947 { 00948 struct ui_out *uiout = current_uiout; 00949 struct auto_load_pspace_info *pspace_info; 00950 struct cleanup *script_chain; 00951 VEC (loaded_script_ptr) *scripts; 00952 int nr_scripts; 00953 00954 dont_repeat (); 00955 00956 pspace_info = get_auto_load_pspace_data (current_program_space); 00957 00958 if (pattern && *pattern) 00959 { 00960 char *re_err = re_comp (pattern); 00961 00962 if (re_err) 00963 error (_("Invalid regexp: %s"), re_err); 00964 } 00965 else 00966 { 00967 re_comp (""); 00968 } 00969 00970 /* We need to know the number of rows before we build the table. 00971 Plus we want to sort the scripts by name. 00972 So first traverse the hash table collecting the matching scripts. */ 00973 00974 scripts = VEC_alloc (loaded_script_ptr, 10); 00975 script_chain = make_cleanup (VEC_cleanup (loaded_script_ptr), &scripts); 00976 00977 if (pspace_info != NULL && pspace_info->loaded_scripts != NULL) 00978 { 00979 struct collect_matching_scripts_data data = { &scripts, language }; 00980 00981 /* Pass a pointer to scripts as VEC_safe_push can realloc space. */ 00982 htab_traverse_noresize (pspace_info->loaded_scripts, 00983 collect_matching_scripts, &data); 00984 } 00985 00986 nr_scripts = VEC_length (loaded_script_ptr, scripts); 00987 00988 /* Table header shifted right by preceding "gdb-scripts: " would not match 00989 its columns. */ 00990 if (nr_scripts > 0 && pattern == auto_load_info_scripts_pattern_nl) 00991 ui_out_text (uiout, "\n"); 00992 00993 make_cleanup_ui_out_table_begin_end (uiout, 2, nr_scripts, 00994 "AutoLoadedScriptsTable"); 00995 00996 ui_out_table_header (uiout, 7, ui_left, "loaded", "Loaded"); 00997 ui_out_table_header (uiout, 70, ui_left, "script", "Script"); 00998 ui_out_table_body (uiout); 00999 01000 if (nr_scripts > 0) 01001 { 01002 int i; 01003 loaded_script_ptr script; 01004 01005 qsort (VEC_address (loaded_script_ptr, scripts), 01006 VEC_length (loaded_script_ptr, scripts), 01007 sizeof (loaded_script_ptr), sort_scripts_by_name); 01008 for (i = 0; VEC_iterate (loaded_script_ptr, scripts, i, script); ++i) 01009 print_script (script); 01010 } 01011 01012 do_cleanups (script_chain); 01013 01014 if (nr_scripts == 0) 01015 { 01016 if (pattern && *pattern) 01017 ui_out_message (uiout, 0, "No auto-load scripts matching %s.\n", 01018 pattern); 01019 else 01020 ui_out_message (uiout, 0, "No auto-load scripts.\n"); 01021 } 01022 } 01023 01024 /* Wrapper for "info auto-load gdb-scripts". */ 01025 01026 static void 01027 info_auto_load_gdb_scripts (char *pattern, int from_tty) 01028 { 01029 auto_load_info_scripts (pattern, from_tty, &script_language_gdb); 01030 } 01031 01032 /* Implement 'info auto-load local-gdbinit'. */ 01033 01034 static void 01035 info_auto_load_local_gdbinit (char *args, int from_tty) 01036 { 01037 if (auto_load_local_gdbinit_pathname == NULL) 01038 printf_filtered (_("Local .gdbinit file was not found.\n")); 01039 else if (auto_load_local_gdbinit_loaded) 01040 printf_filtered (_("Local .gdbinit file \"%s\" has been loaded.\n"), 01041 auto_load_local_gdbinit_pathname); 01042 else 01043 printf_filtered (_("Local .gdbinit file \"%s\" has not been loaded.\n"), 01044 auto_load_local_gdbinit_pathname); 01045 } 01046 01047 /* Return non-zero if SCRIPT_NOT_FOUND_WARNING_PRINTED of PSPACE_INFO was unset 01048 before calling this function. Always set SCRIPT_NOT_FOUND_WARNING_PRINTED 01049 of PSPACE_INFO. */ 01050 01051 int 01052 script_not_found_warning_print (struct auto_load_pspace_info *pspace_info) 01053 { 01054 int retval = !pspace_info->script_not_found_warning_printed; 01055 01056 pspace_info->script_not_found_warning_printed = 1; 01057 01058 return retval; 01059 } 01060 01061 /* The only valid "set auto-load" argument is off|0|no|disable. */ 01062 01063 static void 01064 set_auto_load_cmd (char *args, int from_tty) 01065 { 01066 struct cmd_list_element *list; 01067 size_t length; 01068 01069 /* See parse_binary_operation in use by the sub-commands. */ 01070 01071 length = args ? strlen (args) : 0; 01072 01073 while (length > 0 && (args[length - 1] == ' ' || args[length - 1] == '\t')) 01074 length--; 01075 01076 if (length == 0 || (strncmp (args, "off", length) != 0 01077 && strncmp (args, "0", length) != 0 01078 && strncmp (args, "no", length) != 0 01079 && strncmp (args, "disable", length) != 0)) 01080 error (_("Valid is only global 'set auto-load no'; " 01081 "otherwise check the auto-load sub-commands.")); 01082 01083 for (list = *auto_load_set_cmdlist_get (); list != NULL; list = list->next) 01084 if (list->var_type == var_boolean) 01085 { 01086 gdb_assert (list->type == set_cmd); 01087 do_set_command (args, from_tty, list); 01088 } 01089 } 01090 01091 /* Initialize "set auto-load " commands prefix and return it. */ 01092 01093 struct cmd_list_element ** 01094 auto_load_set_cmdlist_get (void) 01095 { 01096 static struct cmd_list_element *retval; 01097 01098 if (retval == NULL) 01099 add_prefix_cmd ("auto-load", class_maintenance, set_auto_load_cmd, _("\ 01100 Auto-loading specific settings.\n\ 01101 Configure various auto-load-specific variables such as\n\ 01102 automatic loading of Python scripts."), 01103 &retval, "set auto-load ", 01104 1/*allow-unknown*/, &setlist); 01105 01106 return &retval; 01107 } 01108 01109 /* Command "show auto-load" displays summary of all the current 01110 "show auto-load " settings. */ 01111 01112 static void 01113 show_auto_load_cmd (char *args, int from_tty) 01114 { 01115 cmd_show_list (*auto_load_show_cmdlist_get (), from_tty, ""); 01116 } 01117 01118 /* Initialize "show auto-load " commands prefix and return it. */ 01119 01120 struct cmd_list_element ** 01121 auto_load_show_cmdlist_get (void) 01122 { 01123 static struct cmd_list_element *retval; 01124 01125 if (retval == NULL) 01126 add_prefix_cmd ("auto-load", class_maintenance, show_auto_load_cmd, _("\ 01127 Show auto-loading specific settings.\n\ 01128 Show configuration of various auto-load-specific variables such as\n\ 01129 automatic loading of Python scripts."), 01130 &retval, "show auto-load ", 01131 0/*allow-unknown*/, &showlist); 01132 01133 return &retval; 01134 } 01135 01136 /* Command "info auto-load" displays whether the various auto-load files have 01137 been loaded. This is reimplementation of cmd_show_list which inserts 01138 newlines at proper places. */ 01139 01140 static void 01141 info_auto_load_cmd (char *args, int from_tty) 01142 { 01143 struct cmd_list_element *list; 01144 struct cleanup *infolist_chain; 01145 struct ui_out *uiout = current_uiout; 01146 01147 infolist_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "infolist"); 01148 01149 for (list = *auto_load_info_cmdlist_get (); list != NULL; list = list->next) 01150 { 01151 struct cleanup *option_chain 01152 = make_cleanup_ui_out_tuple_begin_end (uiout, "option"); 01153 01154 gdb_assert (!list->prefixlist); 01155 gdb_assert (list->type == not_set_cmd); 01156 01157 ui_out_field_string (uiout, "name", list->name); 01158 ui_out_text (uiout, ": "); 01159 cmd_func (list, auto_load_info_scripts_pattern_nl, from_tty); 01160 01161 /* Close the tuple. */ 01162 do_cleanups (option_chain); 01163 } 01164 01165 /* Close the tuple. */ 01166 do_cleanups (infolist_chain); 01167 } 01168 01169 /* Initialize "info auto-load " commands prefix and return it. */ 01170 01171 struct cmd_list_element ** 01172 auto_load_info_cmdlist_get (void) 01173 { 01174 static struct cmd_list_element *retval; 01175 01176 if (retval == NULL) 01177 add_prefix_cmd ("auto-load", class_info, info_auto_load_cmd, _("\ 01178 Print current status of auto-loaded files.\n\ 01179 Print whether various files like Python scripts or .gdbinit files have been\n\ 01180 found and/or loaded."), 01181 &retval, "info auto-load ", 01182 0/*allow-unknown*/, &infolist); 01183 01184 return &retval; 01185 } 01186 01187 void _initialize_auto_load (void); 01188 01189 void 01190 _initialize_auto_load (void) 01191 { 01192 struct cmd_list_element *cmd; 01193 char *scripts_directory_help; 01194 01195 auto_load_pspace_data 01196 = register_program_space_data_with_cleanup (NULL, 01197 auto_load_pspace_data_cleanup); 01198 01199 observer_attach_new_objfile (auto_load_new_objfile); 01200 01201 add_setshow_boolean_cmd ("gdb-scripts", class_support, 01202 &auto_load_gdb_scripts, _("\ 01203 Enable or disable auto-loading of canned sequences of commands scripts."), _("\ 01204 Show whether auto-loading of canned sequences of commands scripts is enabled."), 01205 _("\ 01206 If enabled, canned sequences of commands are loaded when the debugger reads\n\ 01207 an executable or shared library.\n\ 01208 This options has security implications for untrusted inferiors."), 01209 NULL, show_auto_load_gdb_scripts, 01210 auto_load_set_cmdlist_get (), 01211 auto_load_show_cmdlist_get ()); 01212 01213 add_cmd ("gdb-scripts", class_info, info_auto_load_gdb_scripts, 01214 _("Print the list of automatically loaded sequences of commands.\n\ 01215 Usage: info auto-load gdb-scripts [REGEXP]"), 01216 auto_load_info_cmdlist_get ()); 01217 01218 add_setshow_boolean_cmd ("local-gdbinit", class_support, 01219 &auto_load_local_gdbinit, _("\ 01220 Enable or disable auto-loading of .gdbinit script in current directory."), _("\ 01221 Show whether auto-loading .gdbinit script in current directory is enabled."), 01222 _("\ 01223 If enabled, canned sequences of commands are loaded when debugger starts\n\ 01224 from .gdbinit file in current directory. Such files are deprecated,\n\ 01225 use a script associated with inferior executable file instead.\n\ 01226 This options has security implications for untrusted inferiors."), 01227 NULL, show_auto_load_local_gdbinit, 01228 auto_load_set_cmdlist_get (), 01229 auto_load_show_cmdlist_get ()); 01230 01231 add_cmd ("local-gdbinit", class_info, info_auto_load_local_gdbinit, 01232 _("Print whether current directory .gdbinit file has been loaded.\n\ 01233 Usage: info auto-load local-gdbinit"), 01234 auto_load_info_cmdlist_get ()); 01235 01236 auto_load_dir = xstrdup (AUTO_LOAD_DIR); 01237 scripts_directory_help = xstrprintf ( 01238 #ifdef HAVE_PYTHON 01239 _("\ 01240 Automatically loaded Python scripts (named OBJFILE%s) and GDB scripts\n\ 01241 (named OBJFILE%s) are located in one of the directories listed by this\n\ 01242 option.\n\ 01243 %s"), 01244 GDBPY_AUTO_FILE_NAME, 01245 #else 01246 _("\ 01247 Automatically loaded GDB scripts (named OBJFILE%s) are located in one\n\ 01248 of the directories listed by this option.\n\ 01249 %s"), 01250 #endif 01251 GDB_AUTO_FILE_NAME, 01252 _("\ 01253 This option is ignored for the kinds of scripts \ 01254 having 'set auto-load ... off'.\n\ 01255 Directories listed here need to be present also \ 01256 in the 'set auto-load safe-path'\n\ 01257 option.")); 01258 add_setshow_optional_filename_cmd ("scripts-directory", class_support, 01259 &auto_load_dir, _("\ 01260 Set the list of directories from which to load auto-loaded scripts."), _("\ 01261 Show the list of directories from which to load auto-loaded scripts."), 01262 scripts_directory_help, 01263 set_auto_load_dir, show_auto_load_dir, 01264 auto_load_set_cmdlist_get (), 01265 auto_load_show_cmdlist_get ()); 01266 xfree (scripts_directory_help); 01267 01268 auto_load_safe_path = xstrdup (AUTO_LOAD_SAFE_PATH); 01269 auto_load_safe_path_vec_update (); 01270 add_setshow_optional_filename_cmd ("safe-path", class_support, 01271 &auto_load_safe_path, _("\ 01272 Set the list of files and directories that are safe for auto-loading."), _("\ 01273 Show the list of files and directories that are safe for auto-loading."), _("\ 01274 Various files loaded automatically for the 'set auto-load ...' options must\n\ 01275 be located in one of the directories listed by this option. Warning will be\n\ 01276 printed and file will not be used otherwise.\n\ 01277 You can mix both directory and filename entries.\n\ 01278 Setting this parameter to an empty list resets it to its default value.\n\ 01279 Setting this parameter to '/' (without the quotes) allows any file\n\ 01280 for the 'set auto-load ...' options. Each path entry can be also shell\n\ 01281 wildcard pattern; '*' does not match directory separator.\n\ 01282 This option is ignored for the kinds of files having 'set auto-load ... off'.\n\ 01283 This options has security implications for untrusted inferiors."), 01284 set_auto_load_safe_path, 01285 show_auto_load_safe_path, 01286 auto_load_set_cmdlist_get (), 01287 auto_load_show_cmdlist_get ()); 01288 observer_attach_gdb_datadir_changed (auto_load_gdb_datadir_changed); 01289 01290 cmd = add_cmd ("add-auto-load-safe-path", class_support, 01291 add_auto_load_safe_path, 01292 _("Add entries to the list of directories from which it is safe " 01293 "to auto-load files.\n\ 01294 See the commands 'set auto-load safe-path' and 'show auto-load safe-path' to\n\ 01295 access the current full list setting."), 01296 &cmdlist); 01297 set_cmd_completer (cmd, filename_completer); 01298 01299 add_setshow_boolean_cmd ("auto-load", class_maintenance, 01300 &debug_auto_load, _("\ 01301 Set auto-load verifications debugging."), _("\ 01302 Show auto-load verifications debugging."), _("\ 01303 When non-zero, debugging output for files of 'set auto-load ...'\n\ 01304 is displayed."), 01305 NULL, show_debug_auto_load, 01306 &setdebuglist, &showdebuglist); 01307 }