Return unique_xmalloc_ptr from some solib.c functions
authorTom Tromey <tom@tromey.com>
Fri, 10 Nov 2017 20:07:46 +0000 (13:07 -0700)
committerTom Tromey <tom@tromey.com>
Wed, 14 Feb 2018 15:09:52 +0000 (08:09 -0700)
This changes a couple of solib.c functions -- exec_file_find and
solib_find -- to return a unique_xmalloc_ptr, and then fixes up the
users.  This allows the removal of some cleanups.

This also changes solib_bfd_open to not take ownership of its
argument.  I think this change is somewhat cleaner.

gdb/ChangeLog
2018-02-14  Tom Tromey  <tom@tromey.com>

* solist.h (exec_file_find, solib_find): Return
unique_xmalloc_ptr.
(solib_bfd_fopen): Take a const char *.
* solib.c (solib_find_1): Return unique_xmalloc_ptr.
(exec_file_find, solib_find): Likewise.
(solib_bfd_fopen): Do not take ownership of "pathname".
(solib_bfd_open): Use unique_xmalloc_ptr.
* solib-darwin.c (darwin_bfd_open): Use unique_xmalloc_ptr.
* solib-aix.c (solib_aix_bfd_open): Use unique_xmalloc_ptr.
* infrun.c (follow_exec): Use unique_xmalloc_ptr.
* exec.c (exec_file_locate_attach): Use unique_xmalloc_ptr.

gdb/ChangeLog
gdb/exec.c
gdb/infrun.c
gdb/solib-aix.c
gdb/solib-darwin.c
gdb/solib.c
gdb/solist.h

index fd7682c25300462a86555f287b147241b12ade5c..697ec49523b374cc1b859fd7a741acd42a56312f 100644 (file)
@@ -1,3 +1,17 @@
+2018-02-14  Tom Tromey  <tom@tromey.com>
+
+       * solist.h (exec_file_find, solib_find): Return
+       unique_xmalloc_ptr.
+       (solib_bfd_fopen): Take a const char *.
+       * solib.c (solib_find_1): Return unique_xmalloc_ptr.
+       (exec_file_find, solib_find): Likewise.
+       (solib_bfd_fopen): Do not take ownership of "pathname".
+       (solib_bfd_open): Use unique_xmalloc_ptr.
+       * solib-darwin.c (darwin_bfd_open): Use unique_xmalloc_ptr.
+       * solib-aix.c (solib_aix_bfd_open): Use unique_xmalloc_ptr.
+       * infrun.c (follow_exec): Use unique_xmalloc_ptr.
+       * exec.c (exec_file_locate_attach): Use unique_xmalloc_ptr.
+
 2018-02-14  Joel Brobecker  <brobecker@adacore.com>
 
        * ada-lang.c (name_match_type_from_name): Remove reference to
index c8c32ecc27540c8bc3d349edb87b1e7269ad76a6..15f85a278ff4d7679153a4d2d19cb5fd12ec1004 100644 (file)
@@ -190,8 +190,7 @@ try_open_exec_file (const char *exec_file_host, struct inferior *inf,
 void
 exec_file_locate_attach (int pid, int defer_bp_reset, int from_tty)
 {
-  char *exec_file_target, *exec_file_host;
-  struct cleanup *old_chain;
+  char *exec_file_target;
   symfile_add_flags add_flags = 0;
 
   /* Do nothing if we already have an executable filename.  */
@@ -209,8 +208,8 @@ exec_file_locate_attach (int pid, int defer_bp_reset, int from_tty)
       return;
     }
 
-  exec_file_host = exec_file_find (exec_file_target, NULL);
-  old_chain = make_cleanup (xfree, exec_file_host);
+  gdb::unique_xmalloc_ptr<char> exec_file_host
+    = exec_file_find (exec_file_target, NULL);
 
   if (defer_bp_reset)
     add_flags |= SYMFILE_DEFER_BP_RESET;
@@ -219,8 +218,7 @@ exec_file_locate_attach (int pid, int defer_bp_reset, int from_tty)
     add_flags |= SYMFILE_VERBOSE;
 
   /* Attempt to open the exec file.  */
-  try_open_exec_file (exec_file_host, current_inferior (), add_flags);
-  do_cleanups (old_chain);
+  try_open_exec_file (exec_file_host.get (), current_inferior (), add_flags);
 }
 
 /* Set FILENAME as the new exec file.
index 45fe36a7175d13515668c8f3a68ad85ce54023d1..1bc860b6f3b3771f5461d13b9430daedea1a0135 100644 (file)
@@ -1081,8 +1081,6 @@ follow_exec (ptid_t ptid, char *exec_file_target)
   struct inferior *inf = current_inferior ();
   int pid = ptid_get_pid (ptid);
   ptid_t process_ptid;
-  char *exec_file_host;
-  struct cleanup *old_chain;
 
   /* This is an exec event that we actually wish to pay attention to.
      Refresh our symbol table to the newly exec'd program, remove any
@@ -1161,8 +1159,8 @@ follow_exec (ptid_t ptid, char *exec_file_target)
 
   breakpoint_init_inferior (inf_execd);
 
-  exec_file_host = exec_file_find (exec_file_target, NULL);
-  old_chain = make_cleanup (xfree, exec_file_host);
+  gdb::unique_xmalloc_ptr<char> exec_file_host
+    = exec_file_find (exec_file_target, NULL);
 
   /* If we were unable to map the executable target pathname onto a host
      pathname, tell the user that.  Otherwise GDB's subsequent behavior
@@ -1216,9 +1214,7 @@ follow_exec (ptid_t ptid, char *exec_file_target)
      Executable) main symbol file will only be computed by
      solib_create_inferior_hook below.  breakpoint_re_set would fail
      to insert the breakpoints with the zero displacement.  */
-  try_open_exec_file (exec_file_host, inf, SYMFILE_DEFER_BP_RESET);
-
-  do_cleanups (old_chain);
+  try_open_exec_file (exec_file_host.get (), inf, SYMFILE_DEFER_BP_RESET);
 
   /* If the target can specify a description, read it.  Must do this
      after flipping to the new executable (because the target supplied
index 463f0608450a1479d849cd3ed8a3585cf1da2e05..aa0e0d2dfbd1ad15e46b2927541ad71c6729e9da 100644 (file)
@@ -614,7 +614,6 @@ solib_aix_bfd_open (char *pathname)
   char *sep;
   int filename_len;
   int found_file;
-  char *found_pathname;
 
   if (pathname[path_len - 1] != ')')
     return solib_bfd_open (pathname);
@@ -638,10 +637,12 @@ solib_aix_bfd_open (char *pathname)
   /* Calling solib_find makes certain that sysroot path is set properly
      if program has a dependency on .a archive and sysroot is set via
      set sysroot command.  */
-  found_pathname = solib_find (filename.c_str (), &found_file);
+  gdb::unique_xmalloc_ptr<char> found_pathname
+    = solib_find (filename.c_str (), &found_file);
   if (found_pathname == NULL)
       perror_with_name (pathname);
-  gdb_bfd_ref_ptr archive_bfd (solib_bfd_fopen (found_pathname, found_file));
+  gdb_bfd_ref_ptr archive_bfd (solib_bfd_fopen (found_pathname.get (),
+                                               found_file));
   if (archive_bfd == NULL)
     {
       warning (_("Could not open `%s' as an executable file: %s"),
index 121a7135c05130760d2067411a0a1df4001dc6d3..ed8f9da2573ca62f3979f7ebe3fc1b2714df39bb 100644 (file)
@@ -619,16 +619,16 @@ darwin_lookup_lib_symbol (struct objfile *objfile,
 static gdb_bfd_ref_ptr
 darwin_bfd_open (char *pathname)
 {
-  char *found_pathname;
   int found_file;
 
   /* Search for shared library file.  */
-  found_pathname = solib_find (pathname, &found_file);
+  gdb::unique_xmalloc_ptr<char> found_pathname
+    = solib_find (pathname, &found_file);
   if (found_pathname == NULL)
     perror_with_name (pathname);
 
   /* Open bfd for shared library.  */
-  gdb_bfd_ref_ptr abfd (solib_bfd_fopen (found_pathname, found_file));
+  gdb_bfd_ref_ptr abfd (solib_bfd_fopen (found_pathname.get (), found_file));
 
   gdb_bfd_ref_ptr res
     (gdb_bfd_mach_o_fat_extract (abfd.get (), bfd_object,
index 40fdfc59e653db990986a7e66f5993034fa21d10..b8f726bc10d0518e6cdbb2d2a7ccb194d606e1f8 100644 (file)
@@ -111,11 +111,9 @@ show_solib_search_path (struct ui_file *file, int from_tty,
 #  define DOS_BASED_FILE_SYSTEM 0
 #endif
 
-/* Return the full pathname of a binary file (the main executable
-   or a shared library file), or NULL if not found.  The returned
-   pathname is malloc'ed and must be freed by the caller.  If FD
-   is non-NULL, *FD is set to either -1 or an open file handle for
-   the binary file.
+/* Return the full pathname of a binary file (the main executable or a
+   shared library file), or NULL if not found.  If FD is non-NULL, *FD
+   is set to either -1 or an open file handle for the binary file.
 
    Global variable GDB_SYSROOT is used as a prefix directory
    to search for binary files if they have an absolute path.
@@ -148,7 +146,7 @@ show_solib_search_path (struct ui_file *file, int from_tty,
    * machines since a sysroot will almost always be set.
 */
 
-static char *
+static gdb::unique_xmalloc_ptr<char>
 solib_find_1 (const char *in_pathname, int *fd, int is_solib)
 {
   const struct target_so_ops *ops = solib_ops (target_gdbarch ());
@@ -251,7 +249,7 @@ solib_find_1 (const char *in_pathname, int *fd, int is_solib)
     {
       if (fd != NULL)
        *fd = -1;
-      return temp_pathname;
+      return gdb::unique_xmalloc_ptr<char> (temp_pathname);
     }
 
   /* Now see if we can open it.  */
@@ -367,18 +365,17 @@ solib_find_1 (const char *in_pathname, int *fd, int is_solib)
   else
     *fd = found_file;
 
-  return temp_pathname;
+  return gdb::unique_xmalloc_ptr<char> (temp_pathname);
 }
 
 /* Return the full pathname of the main executable, or NULL if not
-   found.  The returned pathname is malloc'ed and must be freed by
-   the caller.  If FD is non-NULL, *FD is set to either -1 or an open
-   file handle for the main executable.  */
+   found.  If FD is non-NULL, *FD is set to either -1 or an open file
+   handle for the main executable.  */
 
-char *
+gdb::unique_xmalloc_ptr<char>
 exec_file_find (const char *in_pathname, int *fd)
 {
-  char *result;
+  gdb::unique_xmalloc_ptr<char> result;
   const char *fskind = effective_target_file_system_kind ();
 
   if (in_pathname == NULL)
@@ -409,8 +406,11 @@ exec_file_find (const char *in_pathname, int *fd)
         (If that fails, we'll just fall back on the original
         filename.  Not much more we can do...)  */
 
-      if (!source_full_path_of (in_pathname, &result))
-       result = xstrdup (in_pathname);
+      char *full_path = NULL;
+      if (source_full_path_of (in_pathname, &full_path))
+       result.reset (full_path);
+      else
+       result.reset (xstrdup (in_pathname));
       if (fd != NULL)
        *fd = -1;
     }
@@ -419,14 +419,13 @@ exec_file_find (const char *in_pathname, int *fd)
 }
 
 /* Return the full pathname of a shared library file, or NULL if not
-   found.  The returned pathname is malloc'ed and must be freed by
-   the caller.  If FD is non-NULL, *FD is set to either -1 or an open
-   file handle for the shared library.
+   found.  If FD is non-NULL, *FD is set to either -1 or an open file
+   handle for the shared library.
 
    The search algorithm used is described in solib_find_1's comment
    above.  */
 
-char *
+gdb::unique_xmalloc_ptr<char>
 solib_find (const char *in_pathname, int *fd)
 {
   const char *solib_symbols_extension
@@ -463,12 +462,10 @@ solib_find (const char *in_pathname, int *fd)
    it is used as file handle to open the file.  Throws an error if the file
    could not be opened.  Handles both local and remote file access.
 
-   PATHNAME must be malloc'ed by the caller.  It will be freed by this
-   function.  If unsuccessful, the FD will be closed (unless FD was
-   -1).  */
+   If unsuccessful, the FD will be closed (unless FD was -1).  */
 
 gdb_bfd_ref_ptr
-solib_bfd_fopen (char *pathname, int fd)
+solib_bfd_fopen (const char *pathname, int fd)
 {
   gdb_bfd_ref_ptr abfd (gdb_bfd_open (pathname, gnutarget, fd));
 
@@ -478,13 +475,10 @@ solib_bfd_fopen (char *pathname, int fd)
   if (abfd == NULL)
     {
       /* Arrange to free PATHNAME when the error is thrown.  */
-      gdb::unique_xmalloc_ptr<char> free_pathname (pathname);
       error (_("Could not open `%s' as an executable file: %s"),
             pathname, bfd_errmsg (bfd_get_error ()));
     }
 
-  xfree (pathname);
-
   return abfd;
 }
 
@@ -493,12 +487,12 @@ solib_bfd_fopen (char *pathname, int fd)
 gdb_bfd_ref_ptr
 solib_bfd_open (char *pathname)
 {
-  char *found_pathname;
   int found_file;
   const struct bfd_arch_info *b;
 
   /* Search for shared library file.  */
-  found_pathname = solib_find (pathname, &found_file);
+  gdb::unique_xmalloc_ptr<char> found_pathname
+    = solib_find (pathname, &found_file);
   if (found_pathname == NULL)
     {
       /* Return failure if the file could not be found, so that we can
@@ -510,7 +504,7 @@ solib_bfd_open (char *pathname)
     }
 
   /* Open bfd for shared library.  */
-  gdb_bfd_ref_ptr abfd (solib_bfd_fopen (found_pathname, found_file));
+  gdb_bfd_ref_ptr abfd (solib_bfd_fopen (found_pathname.get (), found_file));
 
   /* Check bfd format.  */
   if (!bfd_check_format (abfd.get (), bfd_object))
index 14459139e5dae9fd72a13c321fa1661334eac1d9..aba00ebb2156bee2bf2c0d0f87fc781c4765c032 100644 (file)
@@ -192,13 +192,15 @@ typedef std::unique_ptr<so_list, so_deleter> so_list_up;
 struct so_list *master_so_list (void);
 
 /* Find main executable binary file.  */
-extern char *exec_file_find (const char *in_pathname, int *fd);
+extern gdb::unique_xmalloc_ptr<char> exec_file_find (const char *in_pathname,
+                                                    int *fd);
 
 /* Find shared library binary file.  */
-extern char *solib_find (const char *in_pathname, int *fd);
+extern gdb::unique_xmalloc_ptr<char> solib_find (const char *in_pathname,
+                                                int *fd);
 
 /* Open BFD for shared library file.  */
-extern gdb_bfd_ref_ptr solib_bfd_fopen (char *pathname, int fd);
+extern gdb_bfd_ref_ptr solib_bfd_fopen (const char *pathname, int fd);
 
 /* Find solib binary file and open it.  */
 extern gdb_bfd_ref_ptr solib_bfd_open (char *in_pathname);
This page took 0.033367 seconds and 4 git commands to generate.