gdb/
[deliverable/binutils-gdb.git] / gdb / auto-load.c
index 9d19179feb94a23f90cb1408224b3fbd218a59bc..171c9b1e200db5a87b7984ef7411387e027d2a9e 100644 (file)
@@ -35,6 +35,7 @@
 #include "gdb_vecs.h"
 #include "readline/tilde.h"
 #include "completer.h"
+#include "observer.h"
 
 /* The suffix of per-objfile scripts to auto-load as non-Python command files.
    E.g. When the program loads libfoo.so, look for libfoo-gdb.gdb.  */
@@ -141,10 +142,16 @@ auto_load_safe_path_vec_update (void)
   for (ix = 0; ix < len; ix++)
     {
       char *dir = VEC_index (char_ptr, auto_load_safe_path_vec, ix);
-      char *expanded = tilde_expand (dir);
-      char *real_path = gdb_realpath (expanded);
+      char *ddir_subst, *expanded, *real_path;
+
+      ddir_subst = xstrdup (dir);
+      substitute_path_component (&ddir_subst, "$ddir", gdb_datadir);
+      expanded = tilde_expand (ddir_subst);
+      xfree (ddir_subst);
+      real_path = gdb_realpath (expanded);
 
-      /* Ensure the current entry is at least tilde_expand-ed.  */
+      /* Ensure the current entry is at least a valid path (therefore
+        $ddir-expanded and tilde-expanded).  */
       VEC_replace (char_ptr, auto_load_safe_path_vec, ix, expanded);
 
       if (debug_auto_load)
@@ -176,11 +183,26 @@ auto_load_safe_path_vec_update (void)
     }
 }
 
+/* Variable gdb_datadir has been set.  Update content depending on $ddir.  */
+
+static void
+auto_load_gdb_datadir_changed (void)
+{
+  auto_load_safe_path_vec_update ();
+}
+
 /* "set" command for the auto_load_safe_path configuration variable.  */
 
 static void
 set_auto_load_safe_path (char *args, int from_tty, struct cmd_list_element *c)
 {
+  /* Setting the variable to "" resets it to the compile time defaults.  */
+  if (auto_load_safe_path[0] == '\0')
+    {
+      xfree (auto_load_safe_path);
+      auto_load_safe_path = xstrdup (AUTO_LOAD_SAFE_PATH);
+    }
+
   auto_load_safe_path_vec_update ();
 }
 
@@ -190,7 +212,7 @@ static void
 show_auto_load_safe_path (struct ui_file *file, int from_tty,
                          struct cmd_list_element *c, const char *value)
 {
-  if (*value == 0)
+  if (strcmp (value, "/") == 0)
     fprintf_filtered (file, _("Auto-load files are safe to load from any "
                              "directory.\n"));
   else
@@ -209,8 +231,9 @@ add_auto_load_safe_path (char *args, int from_tty)
 
   if (args == NULL || *args == 0)
     error (_("\
-Adding empty directory element disables the auto-load safe-path security.  \
-Use 'set auto-load safe-path' instead if you mean that."));
+Directory argument required.\n\
+Use 'set auto-load safe-path /' for disabling the auto-load safe-path security.\
+"));
 
   s = xstrprintf ("%s%c%s", auto_load_safe_path, DIRNAME_SEPARATOR, args);
   xfree (auto_load_safe_path);
@@ -231,6 +254,12 @@ filename_is_in_dir (const char *filename, const char *dir)
   while (dir_len && IS_DIR_SEPARATOR (dir[dir_len - 1]))
     dir_len--;
 
+  /* Ensure auto_load_safe_path "/" matches any FILENAME.  On MS-Windows
+     platform FILENAME even after gdb_realpath does not have to start with
+     IS_DIR_SEPARATOR character, such as the 'C:\x.exe' filename.  */
+  if (dir_len == 0)
+    return 1;
+
   return (filename_ncmp (dir, filename, dir_len) == 0
          && (IS_DIR_SEPARATOR (filename[dir_len])
              || filename[dir_len] == '\0'));
@@ -574,17 +603,29 @@ auto_load_objfile_script (struct objfile *objfile,
   input = fopen (filename, "r");
   debugfile = filename;
 
-  if (!input && debug_file_directory)
+  if (!input)
     {
-      /* Also try the same file in the separate debug info directory.  */
-      debugfile = xmalloc (strlen (filename)
-                          + strlen (debug_file_directory) + 1);
-      strcpy (debugfile, debug_file_directory);
-      /* FILENAME is absolute, so we don't need a "/" here.  */
-      strcat (debugfile, filename);
+      char *debugdir;
+      VEC (char_ptr) *debugdir_vec;
+      int ix;
 
-      make_cleanup (xfree, debugfile);
-      input = fopen (debugfile, "r");
+      debugdir_vec = dirnames_to_char_ptr_vec (debug_file_directory);
+      make_cleanup_free_char_ptr_vec (debugdir_vec);
+
+      for (ix = 0; VEC_iterate (char_ptr, debugdir_vec, ix, debugdir); ++ix)
+       {
+         /* Also try the same file in the separate debug info directory.  */
+         debugfile = xmalloc (strlen (debugdir) + strlen (filename) + 1);
+         strcpy (debugfile, debugdir);
+
+         /* FILENAME is absolute, so we don't need a "/" here.  */
+         strcat (debugfile, filename);
+
+         make_cleanup (xfree, debugfile);
+         input = fopen (debugfile, "r");
+         if (input != NULL)
+           break;
+       }
     }
 
   if (!input && gdb_datadir)
@@ -1015,7 +1056,7 @@ This options has security implications for untrusted inferiors."),
 Usage: info auto-load local-gdbinit"),
           auto_load_info_cmdlist_get ());
 
-  auto_load_safe_path = xstrdup (DEFAULT_AUTO_LOAD_SAFE_PATH);
+  auto_load_safe_path = xstrdup (AUTO_LOAD_SAFE_PATH);
   auto_load_safe_path_vec_update ();
   add_setshow_optional_filename_cmd ("safe-path", class_support,
                                     &auto_load_safe_path, _("\
@@ -1023,14 +1064,17 @@ Set the list of directories from which it is safe to auto-load files."), _("\
 Show the list of directories from which it is safe to auto-load files."), _("\
 Various files loaded automatically for the 'set auto-load ...' options must\n\
 be located in one of the directories listed by this option.  Warning will be\n\
-printed and file will not be used otherwise.  Use empty string (or even\n\
-empty directory entry) to allow any file for the 'set auto-load ...' options.\n\
+printed and file will not be used otherwise.\n\
+Setting this parameter to an empty list resets it to its default value.\n\
+Setting this parameter to '/' (without the quotes) allows any file\n\
+for the 'set auto-load ...' options.\n\
 This option is ignored for the kinds of files having 'set auto-load ... off'.\n\
 This options has security implications for untrusted inferiors."),
                                     set_auto_load_safe_path,
                                     show_auto_load_safe_path,
                                     auto_load_set_cmdlist_get (),
                                     auto_load_show_cmdlist_get ());
+  observer_attach_gdb_datadir_changed (auto_load_gdb_datadir_changed);
 
   cmd = add_cmd ("add-auto-load-safe-path", class_support,
                 add_auto_load_safe_path,
This page took 0.02506 seconds and 4 git commands to generate.