[PATCH 1/2] Make find_separate_debug_file* return std::string

Simon Marchi simon.marchi@polymtl.ca
Sat Mar 3 05:10:00 GMT 2018


This patch makes the find_separate_debug_file* functions return
std::string, which allows to get rid of some manual memory management
and one cleanup.

gdb/ChangeLog:

	* build-id.c (find_separate_debug_file_by_buildid): Return
	std::string.
	* build-id.h (find_separate_debug_file_by_buildid): Return
	std::string.
	* coffread.c (coff_symfile_read): Adjust to std::string.
	* elfread.c (elf_symfile_read): Adjust to std::string.
	* symfile.c (separate_debug_file_exists): Change parameter to
	std::string.
	(find_separate_debug_file): Return std::string.
	(find_separate_debug_file_by_debuglink): Return std::string.
	* symfile.h (find_separate_debug_file_by_debuglink): Return
	std::string.
---
 gdb/build-id.c |  7 +++---
 gdb/build-id.h |  8 +++----
 gdb/coffread.c | 15 +++++-------
 gdb/elfread.c  | 13 +++++-----
 gdb/symfile.c  | 76 +++++++++++++++++++++++-----------------------------------
 gdb/symfile.h  |  2 +-
 6 files changed, 51 insertions(+), 70 deletions(-)

diff --git a/gdb/build-id.c b/gdb/build-id.c
index 57d98c9618..a5d4e67971 100644
--- a/gdb/build-id.c
+++ b/gdb/build-id.c
@@ -136,7 +136,7 @@ build_id_to_debug_bfd (size_t build_id_len, const bfd_byte *build_id)
 
 /* See build-id.h.  */
 
-char *
+std::string
 find_separate_debug_file_by_buildid (struct objfile *objfile)
 {
   const struct bfd_build_id *build_id;
@@ -157,7 +157,8 @@ find_separate_debug_file_by_buildid (struct objfile *objfile)
 	warning (_("\"%s\": separate debug info file has no debug info"),
 		 bfd_get_filename (abfd.get ()));
       else if (abfd != NULL)
-	return xstrdup (bfd_get_filename (abfd.get ()));
+	return std::string (bfd_get_filename (abfd.get ()));
     }
-  return NULL;
+
+  return std::string ();
 }
diff --git a/gdb/build-id.h b/gdb/build-id.h
index 0f13c7d4cf..15fb609409 100644
--- a/gdb/build-id.h
+++ b/gdb/build-id.h
@@ -41,10 +41,10 @@ extern gdb_bfd_ref_ptr build_id_to_debug_bfd (size_t build_id_len,
 					      const bfd_byte *build_id);
 
 /* Find the separate debug file for OBJFILE, by using the build-id
-   associated with OBJFILE's BFD.  If successful, returns a malloc'd
-   file name for the separate debug file.  The caller must free this.
-   Otherwise, returns NULL.  */
+   associated with OBJFILE's BFD.  If successful, returns the file name for the
+   separate debug file, otherwise, return an empty string.  */
 
-extern char *find_separate_debug_file_by_buildid (struct objfile *objfile);
+extern std::string find_separate_debug_file_by_buildid
+  (struct objfile *objfile);
 
 #endif /* BUILD_ID_H */
diff --git a/gdb/coffread.c b/gdb/coffread.c
index fbbbb68f71..cad3e7e2f1 100644
--- a/gdb/coffread.c
+++ b/gdb/coffread.c
@@ -733,20 +733,17 @@ coff_symfile_read (struct objfile *objfile, symfile_add_flags symfile_flags)
   /* Try to add separate debug file if no symbols table found.   */
   if (!objfile_has_partial_symbols (objfile))
     {
-      char *debugfile;
+      std::string debugfile = find_separate_debug_file_by_buildid (objfile);
 
-      debugfile = find_separate_debug_file_by_buildid (objfile);
-
-      if (debugfile == NULL)
+      if (debugfile.empty ())
 	debugfile = find_separate_debug_file_by_debuglink (objfile);
-      make_cleanup (xfree, debugfile);
 
-      if (debugfile)
+      if (!debugfile.empty ())
 	{
-	  gdb_bfd_ref_ptr abfd (symfile_bfd_open (debugfile));
+	  gdb_bfd_ref_ptr abfd (symfile_bfd_open (debugfile.c_str ()));
 
-	  symbol_file_add_separate (abfd.get (), debugfile, symfile_flags,
-				    objfile);
+	  symbol_file_add_separate (abfd.get (), debugfile.c_str (),
+				    symfile_flags, objfile);
 	}
     }
 
diff --git a/gdb/elfread.c b/gdb/elfread.c
index 103b2144c3..260789062d 100644
--- a/gdb/elfread.c
+++ b/gdb/elfread.c
@@ -1259,17 +1259,16 @@ elf_symfile_read (struct objfile *objfile, symfile_add_flags symfile_flags)
 	   && objfile->separate_debug_objfile == NULL
 	   && objfile->separate_debug_objfile_backlink == NULL)
     {
-      gdb::unique_xmalloc_ptr<char> debugfile
-	(find_separate_debug_file_by_buildid (objfile));
+      std::string debugfile = find_separate_debug_file_by_buildid (objfile);
 
-      if (debugfile == NULL)
-	debugfile.reset (find_separate_debug_file_by_debuglink (objfile));
+      if (debugfile.empty ())
+	debugfile = find_separate_debug_file_by_debuglink (objfile);
 
-      if (debugfile != NULL)
+      if (!debugfile.empty ())
 	{
-	  gdb_bfd_ref_ptr abfd (symfile_bfd_open (debugfile.get ()));
+	  gdb_bfd_ref_ptr abfd (symfile_bfd_open (debugfile.c_str ()));
 
-	  symbol_file_add_separate (abfd.get (), debugfile.get (),
+	  symbol_file_add_separate (abfd.get (), debugfile.c_str (),
 				    symfile_flags, objfile);
 	}
     }
diff --git a/gdb/symfile.c b/gdb/symfile.c
index 72bf0d8451..58747d545b 100644
--- a/gdb/symfile.c
+++ b/gdb/symfile.c
@@ -1333,7 +1333,7 @@ symbol_file_clear (int from_tty)
 int separate_debug_file_debug = 0;
 
 static int
-separate_debug_file_exists (const char *name, unsigned long crc,
+separate_debug_file_exists (const std::string &name, unsigned long crc,
 			    struct objfile *parent_objfile)
 {
   unsigned long file_crc;
@@ -1347,13 +1347,13 @@ separate_debug_file_exists (const char *name, unsigned long crc,
      ".debug" suffix as "/usr/lib/debug/path/to/file" is a separate tree where
      the separate debug infos with the same basename can exist.  */
 
-  if (filename_cmp (name, objfile_name (parent_objfile)) == 0)
+  if (filename_cmp (name.c_str (), objfile_name (parent_objfile)) == 0)
     return 0;
 
   if (separate_debug_file_debug)
-    printf_unfiltered (_("  Trying %s\n"), name);
+    printf_unfiltered (_("  Trying %s\n"), name.c_str ());
 
-  gdb_bfd_ref_ptr abfd (gdb_bfd_open (name, gnutarget, -1));
+  gdb_bfd_ref_ptr abfd (gdb_bfd_open (name.c_str (), gnutarget, -1));
 
   if (abfd == NULL)
     return 0;
@@ -1403,7 +1403,7 @@ separate_debug_file_exists (const char *name, unsigned long crc,
       if (verified_as_different || parent_crc != file_crc)
 	warning (_("the debug information found in \"%s\""
 		   " does not match \"%s\" (CRC mismatch).\n"),
-		 name, objfile_name (parent_objfile));
+		 name.c_str (), objfile_name (parent_objfile));
 
       return 0;
     }
@@ -1431,46 +1431,31 @@ show_debug_file_directory (struct ui_file *file, int from_tty,
    dirname(objfile->name) due to symlinks), and DEBUGLINK as the file we are
    looking for.  CANON_DIR is the "realpath" form of DIR.
    DIR must contain a trailing '/'.
-   Returns the path of the file with separate debug info, of NULL.  */
+   Returns the path of the file with separate debug info, or an empty
+   string.  */
 
-static char *
+static std::string
 find_separate_debug_file (const char *dir,
 			  const char *canon_dir,
 			  const char *debuglink,
 			  unsigned long crc32, struct objfile *objfile)
 {
-  char *debugfile;
-  int i;
-
   if (separate_debug_file_debug)
     printf_unfiltered (_("\nLooking for separate debug info (debug link) for "
 		         "%s\n"), objfile_name (objfile));
 
-  /* Set I to std::max (strlen (canon_dir), strlen (dir)).  */
-  i = strlen (dir);
-  if (canon_dir != NULL && strlen (canon_dir) > i)
-    i = strlen (canon_dir);
-
-  debugfile
-    = (char *) xmalloc (strlen (debug_file_directory) + 1
-			+ i
-			+ strlen (DEBUG_SUBDIRECTORY)
-			+ strlen ("/")
-			+ strlen (debuglink)
-			+ 1);
-
   /* First try in the same directory as the original file.  */
-  strcpy (debugfile, dir);
-  strcat (debugfile, debuglink);
+  std::string debugfile = dir;
+  debugfile += debuglink;
 
   if (separate_debug_file_exists (debugfile, crc32, objfile))
     return debugfile;
 
   /* Then try in the subdirectory named DEBUG_SUBDIRECTORY.  */
-  strcpy (debugfile, dir);
-  strcat (debugfile, DEBUG_SUBDIRECTORY);
-  strcat (debugfile, "/");
-  strcat (debugfile, debuglink);
+  debugfile = dir;
+  debugfile += DEBUG_SUBDIRECTORY;
+  debugfile += "/";
+  debugfile += debuglink;
 
   if (separate_debug_file_exists (debugfile, crc32, objfile))
     return debugfile;
@@ -1485,10 +1470,10 @@ find_separate_debug_file (const char *dir,
 
   for (const gdb::unique_xmalloc_ptr<char> &debugdir : debugdir_vec)
     {
-      strcpy (debugfile, debugdir.get ());
-      strcat (debugfile, "/");
-      strcat (debugfile, dir);
-      strcat (debugfile, debuglink);
+      debugfile = debugdir.get ();
+      debugfile += "/";
+      debugfile += dir;
+      debugfile += debuglink;
 
       if (separate_debug_file_exists (debugfile, crc32, objfile))
 	return debugfile;
@@ -1500,18 +1485,17 @@ find_separate_debug_file (const char *dir,
 			    strlen (gdb_sysroot)) == 0
 	  && IS_DIR_SEPARATOR (canon_dir[strlen (gdb_sysroot)]))
 	{
-	  strcpy (debugfile, debugdir.get ());
-	  strcat (debugfile, canon_dir + strlen (gdb_sysroot));
-	  strcat (debugfile, "/");
-	  strcat (debugfile, debuglink);
+	  debugfile = debugdir.get ();
+	  debugfile += (canon_dir + strlen (gdb_sysroot));
+	  debugfile += "/";
+	  debugfile += debuglink;
 
 	  if (separate_debug_file_exists (debugfile, crc32, objfile))
 	    return debugfile;
 	}
     }
 
-  xfree (debugfile);
-  return NULL;
+  return std::string ();
 }
 
 /* Modify PATH to contain only "[/]directory/" part of PATH.
@@ -1534,12 +1518,11 @@ terminate_after_last_dir_separator (char *path)
 }
 
 /* Find separate debuginfo for OBJFILE (using .gnu_debuglink section).
-   Returns pathname, or NULL.  */
+   Returns pathname, or an empty string.  */
 
-char *
+std::string
 find_separate_debug_file_by_debuglink (struct objfile *objfile)
 {
-  char *debugfile;
   unsigned long crc32;
 
   gdb::unique_xmalloc_ptr<char> debuglink
@@ -1549,17 +1532,18 @@ find_separate_debug_file_by_debuglink (struct objfile *objfile)
     {
       /* There's no separate debug info, hence there's no way we could
 	 load it => no warning.  */
-      return NULL;
+      return std::string ();
     }
 
   std::string dir = objfile_name (objfile);
   terminate_after_last_dir_separator (&dir[0]);
   gdb::unique_xmalloc_ptr<char> canon_dir (lrealpath (dir.c_str ()));
 
-  debugfile = find_separate_debug_file (dir.c_str (), canon_dir.get (),
-					debuglink.get (), crc32, objfile);
+  std::string debugfile
+    = find_separate_debug_file (dir.c_str (), canon_dir.get (),
+				debuglink.get (), crc32, objfile);
 
-  if (debugfile == NULL)
+  if (debugfile.empty ())
     {
       /* For PR gdb/9538, try again with realpath (if different from the
 	 original).  */
diff --git a/gdb/symfile.h b/gdb/symfile.h
index 7c3fd8240a..8cd47d8811 100644
--- a/gdb/symfile.h
+++ b/gdb/symfile.h
@@ -426,7 +426,7 @@ extern struct objfile *symbol_file_add_from_bfd (bfd *, const char *, symfile_ad
 extern void symbol_file_add_separate (bfd *, const char *, symfile_add_flags,
 				      struct objfile *);
 
-extern char *find_separate_debug_file_by_debuglink (struct objfile *);
+extern std::string find_separate_debug_file_by_debuglink (struct objfile *);
 
 /* Create a new section_addr_info, with room for NUM_SECTIONS.  */
 
-- 
2.16.1



More information about the Gdb-patches mailing list