Introduce and use make_unique_xstrdup
[deliverable/binutils-gdb.git] / gdb / symfile.c
index 4875cbf7f5a71c57b0b176d5f2fbd8c501ae9a95..26762d289cae7df0dea1d02b78b549cc8e05648e 100644 (file)
@@ -57,7 +57,8 @@
 #include "gdb_bfd.h"
 #include "cli/cli-utils.h"
 #include "common/byte-vector.h"
-#include "selftest.h"
+#include "common/pathstuff.h"
+#include "common/selftest.h"
 #include "cli/cli-style.h"
 #include "common/forward-scope-exit.h"
 
@@ -1440,34 +1441,77 @@ find_separate_debug_file (const char *dir,
   const char *dir_notarget = target_prefix ? dir + strlen ("target:") : dir;
   std::vector<gdb::unique_xmalloc_ptr<char>> debugdir_vec
     = dirnames_to_char_ptr_vec (debug_file_directory);
+  gdb::unique_xmalloc_ptr<char> canon_sysroot = gdb_realpath (gdb_sysroot);
+
+ /* MS-Windows/MS-DOS don't allow colons in file names; we must
+    convert the drive letter into a one-letter directory, so that the
+    file name resulting from splicing below will be valid.
+
+    FIXME: The below only works when GDB runs on MS-Windows/MS-DOS.
+    There are various remote-debugging scenarios where such a
+    transformation of the drive letter might be required when GDB runs
+    on a Posix host, see
+
+    https://sourceware.org/ml/gdb-patches/2019-04/msg00605.html
+
+    If some of those scenarions need to be supported, we will need to
+    use a different condition for HAS_DRIVE_SPEC and a different macro
+    instead of STRIP_DRIVE_SPEC, which work on Posix systems as well.  */
+  std::string drive;
+  if (HAS_DRIVE_SPEC (dir_notarget))
+    {
+      drive = dir_notarget[0];
+      dir_notarget = STRIP_DRIVE_SPEC (dir_notarget);
+    }
 
   for (const gdb::unique_xmalloc_ptr<char> &debugdir : debugdir_vec)
     {
       debugfile = target_prefix ? "target:" : "";
       debugfile += debugdir.get ();
       debugfile += "/";
+      debugfile += drive;
       debugfile += dir_notarget;
       debugfile += debuglink;
 
       if (separate_debug_file_exists (debugfile, crc32, objfile))
        return debugfile;
 
-      /* If the file is in the sysroot, try using its base path in the
-        global debugfile directory.  */
-      if (canon_dir != NULL
-         && filename_ncmp (canon_dir, gdb_sysroot,
-                           strlen (gdb_sysroot)) == 0
-         && IS_DIR_SEPARATOR (canon_dir[strlen (gdb_sysroot)]))
+      const char *base_path = NULL;
+      if (canon_dir != NULL)
        {
+         if (canon_sysroot.get () != NULL)
+           base_path = child_path (canon_sysroot.get (), canon_dir);
+         else
+           base_path = child_path (gdb_sysroot, canon_dir);
+       }
+      if (base_path != NULL)
+       {
+         /* If the file is in the sysroot, try using its base path in
+            the global debugfile directory.  */
+         debugfile = target_prefix ? "target:" : "";
+         debugfile += debugdir.get ();
+         debugfile += "/";
+         debugfile += base_path;
+         debugfile += "/";
+         debugfile += debuglink;
+
+         if (separate_debug_file_exists (debugfile, crc32, objfile))
+           return debugfile;
+
+         /* If the file is in the sysroot, try using its base path in
+            the sysroot's global debugfile directory.  */
          debugfile = target_prefix ? "target:" : "";
+         debugfile += gdb_sysroot;
          debugfile += debugdir.get ();
-         debugfile += (canon_dir + strlen (gdb_sysroot));
+         debugfile += "/";
+         debugfile += base_path;
          debugfile += "/";
          debugfile += debuglink;
 
          if (separate_debug_file_exists (debugfile, crc32, objfile))
            return debugfile;
        }
+
     }
 
   return std::string ();
@@ -2405,19 +2449,12 @@ remove_symbol_file_command (const char *args, int from_tty)
 void
 reread_symbols (void)
 {
-  struct objfile *objfile;
   long new_modtime;
   struct stat new_statbuf;
   int res;
   std::vector<struct objfile *> new_objfiles;
 
-  /* With the addition of shared libraries, this should be modified,
-     the load time should be saved in the partial symbol tables, since
-     different tables may come from different source files.  FIXME.
-     This routine should then walk down each partial symbol table
-     and see if the symbol table that it originates from has been changed.  */
-
-  for (objfile = object_files; objfile; objfile = objfile->next)
+  for (objfile *objfile : current_program_space->objfiles ())
     {
       if (objfile->obfd == NULL)
        continue;
@@ -2534,7 +2571,7 @@ reread_symbols (void)
          objfile->sections = NULL;
          objfile->compunit_symtabs = NULL;
          objfile->template_symbols = NULL;
-         objfile->static_links = NULL;
+         objfile->static_links.reset (nullptr);
 
          /* obstack_init also initializes the obstack so it is
             empty.  We could use obstack_specify_allocation but
@@ -2558,7 +2595,6 @@ reread_symbols (void)
          objfile_set_sym_fns (objfile, find_sym_fns (objfile->obfd));
 
          build_objfile_section_table (objfile);
-         terminate_minimal_symbol_table (objfile);
 
          /* We use the same section offsets as from last time.  I'm not
             sure whether that is always correct for shared libraries.  */
@@ -2773,8 +2809,8 @@ allocate_symtab (struct compunit_symtab *cust, const char *filename)
     = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symtab);
 
   symtab->filename
-    = (const char *) bcache (filename, strlen (filename) + 1,
-                            objfile->per_bfd->filename_cache);
+    = ((const char *) objfile->per_bfd->filename_cache.insert
+       (filename, strlen (filename) + 1));
   symtab->fullname = NULL;
   symtab->language = deduce_language_from_filename (filename);
 
@@ -2880,12 +2916,6 @@ clear_symtab_users (symfile_add_flags add_flags)
   clear_pc_function_cache ();
   gdb::observers::new_objfile.notify (NULL);
 
-  /* Clear globals which might have pointed into a removed objfile.
-     FIXME: It's not clear which of these are supposed to persist
-     between expressions and which ought to be reset each time.  */
-  expression_context_block = NULL;
-  innermost_block.reset ();
-
   /* Varobj may refer to old symbols, perform a cleanup.  */
   varobj_invalidate ();
 
@@ -3847,7 +3877,7 @@ test_set_ext_lang_command ()
   SELF_CHECK (lang == language_unknown);
 
   /* Test adding a new extension using the CLI command.  */
-  gdb::unique_xmalloc_ptr<char> args_holder (xstrdup (".hello rust"));
+  auto args_holder = make_unique_xstrdup (".hello rust");
   ext_args = args_holder.get ();
   set_ext_lang_command (NULL, 1, NULL);
 
This page took 0.027395 seconds and 4 git commands to generate.