Change openp et al to use a unique_xmalloc_ptr
authorTom Tromey <tom@tromey.com>
Fri, 10 Nov 2017 20:47:05 +0000 (13:47 -0700)
committerTom Tromey <tom@tromey.com>
Wed, 14 Feb 2018 15:09:53 +0000 (08:09 -0700)
This changes openp, source_full_path_of, and find_and_open_source to
take a unique_xmalloc_ptr, rather than a char*, as an outgoing
argument type.  This simplifies the API, ownership-wise, and allows
for the removal of some cleanups.

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

* symfile.c (symfile_bfd_open): Update.
* source.h (openp, source_full_path_of, find_and_open_source):
Change argument type to unique_xmalloc_ptr.
* source.c (openp): Take a unique_xmalloc_ptr.
(source_full_path_of, find_and_open_source): Likewise.
(open_source_file, symtab_to_fullname): Update.
* solist.h (struct target_so_ops) <find_and_open_solib>: Take a
unique_xmalloc_ptr.
* solib.c (solib_find_1): Use unique_xmalloc_ptr.
(exec_file_find): Update.
* psymtab.c (psymtab_to_fullname): Update.
* nto-tdep.h (nto_find_and_open_solib): Update.
* nto-tdep.c (nto_find_and_open_solib): Change temp_path to a
unique_xmalloc_ptr.
* exec.c (exec_file_attach): Update.
* dwarf2read.c (try_open_dwop_file): Use unique_xmalloc_ptr.
* cli/cli-cmds.c (find_and_open_script): Use unique_xmalloc_ptr.

12 files changed:
gdb/ChangeLog
gdb/cli/cli-cmds.c
gdb/dwarf2read.c
gdb/exec.c
gdb/nto-tdep.c
gdb/nto-tdep.h
gdb/psymtab.c
gdb/solib.c
gdb/solist.h
gdb/source.c
gdb/source.h
gdb/symfile.c

index cfab252a7d10d3d03a3c59091c9d70813a811eaa..76c160acabd692979df98ce81c4dc323d8406a67 100644 (file)
@@ -1,3 +1,23 @@
+2018-02-14  Tom Tromey  <tom@tromey.com>
+
+       * symfile.c (symfile_bfd_open): Update.
+       * source.h (openp, source_full_path_of, find_and_open_source):
+       Change argument type to unique_xmalloc_ptr.
+       * source.c (openp): Take a unique_xmalloc_ptr.
+       (source_full_path_of, find_and_open_source): Likewise.
+       (open_source_file, symtab_to_fullname): Update.
+       * solist.h (struct target_so_ops) <find_and_open_solib>: Take a
+       unique_xmalloc_ptr.
+       * solib.c (solib_find_1): Use unique_xmalloc_ptr.
+       (exec_file_find): Update.
+       * psymtab.c (psymtab_to_fullname): Update.
+       * nto-tdep.h (nto_find_and_open_solib): Update.
+       * nto-tdep.c (nto_find_and_open_solib): Change temp_path to a
+       unique_xmalloc_ptr.
+       * exec.c (exec_file_attach): Update.
+       * dwarf2read.c (try_open_dwop_file): Use unique_xmalloc_ptr.
+       * cli/cli-cmds.c (find_and_open_script): Use unique_xmalloc_ptr.
+
 2018-02-14  Tom Tromey  <tom@tromey.com>
 
        * solib.c: Include source.h.
index c3962e939ee18548210768866e4623cff28ae687..95bd58ce545493dcbdc611cef0a6506c51f0b327 100644 (file)
@@ -506,10 +506,9 @@ find_and_open_script (const char *script_file, int search_path)
 
   /* Search for and open 'file' on the search path used for source
      files.  Put the full location in *FULL_PATHP.  */
-  char *temp_path;
+  gdb::unique_xmalloc_ptr<char> full_path;
   fd = openp (source_path, search_flags,
-             file.get (), O_RDONLY, &temp_path);
-  gdb::unique_xmalloc_ptr<char> full_path (temp_path);
+             file.get (), O_RDONLY, &full_path);
 
   if (fd == -1)
     return opened;
index 93453431c155273e66c653a55f40dedc6de68aba..d6d3d4a20fd16ba5329c103c4b9bc2390bb7657a 100644 (file)
@@ -12881,35 +12881,40 @@ try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
                    const char *file_name, int is_dwp, int search_cwd)
 {
   int desc;
-  char *absolute_name;
   /* Blech.  OPF_TRY_CWD_FIRST also disables searching the path list if
      FILE_NAME contains a '/'.  So we can't use it.  Instead prepend "."
      to debug_file_directory.  */
-  char *search_path;
+  const char *search_path;
   static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
 
+  gdb::unique_xmalloc_ptr<char> search_path_holder;
   if (search_cwd)
     {
       if (*debug_file_directory != '\0')
-       search_path = concat (".", dirname_separator_string,
-                             debug_file_directory, (char *) NULL);
+       {
+         search_path_holder.reset (concat (".", dirname_separator_string,
+                                           debug_file_directory,
+                                           (char *) NULL));
+         search_path = search_path_holder.get ();
+       }
       else
-       search_path = xstrdup (".");
+       search_path = ".";
     }
   else
-    search_path = xstrdup (debug_file_directory);
+    search_path = debug_file_directory;
 
   openp_flags flags = OPF_RETURN_REALPATH;
   if (is_dwp)
     flags |= OPF_SEARCH_IN_PATH;
+
+  gdb::unique_xmalloc_ptr<char> absolute_name;
   desc = openp (search_path, flags, file_name,
                O_RDONLY | O_BINARY, &absolute_name);
-  xfree (search_path);
   if (desc < 0)
     return NULL;
 
-  gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name, gnutarget, desc));
-  xfree (absolute_name);
+  gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
+                                        gnutarget, desc));
   if (sym_bfd == NULL)
     return NULL;
   bfd_set_cacheable (sym_bfd.get (), 1);
index 79baf9c5fbbcdd4fc8b8b13c1e7e0fcb1772ca6c..0b4237ff63cbe948afed8fb8b6ece40983f94185 100644 (file)
@@ -290,12 +290,10 @@ exec_file_attach (const char *filename, int from_tty)
        }
       else
        {
-         char *temp_pathname;
-
          scratch_chan = openp (getenv ("PATH"), OPF_TRY_CWD_FIRST,
                                filename, write_files ?
                                O_RDWR | O_BINARY : O_RDONLY | O_BINARY,
-                               &temp_pathname);
+                               &scratch_storage);
 #if defined(__GO32__) || defined(_WIN32) || defined(__CYGWIN__)
          if (scratch_chan < 0)
            {
@@ -306,14 +304,13 @@ exec_file_attach (const char *filename, int from_tty)
                                    exename, write_files ?
                                    O_RDWR | O_BINARY
                                    : O_RDONLY | O_BINARY,
-                                   &temp_pathname);
+                                   &scratch_storage);
            }
 #endif
          if (scratch_chan < 0)
            perror_with_name (filename);
 
-         scratch_storage.reset (temp_pathname);
-         scratch_pathname = temp_pathname;
+         scratch_pathname = scratch_storage.get ();
 
          /* gdb_bfd_open (and its variants) prefers canonicalized
             pathname for better BFD caching.  */
index 30db99c759d37e2a2765a33b4981016015929946..8eb864b871b2f1d43ac2b22b4abcc8f3b1f94149 100644 (file)
@@ -89,7 +89,7 @@ nto_map_arch_to_cputype (const char *arch)
 
 int
 nto_find_and_open_solib (const char *solib, unsigned o_flags,
-                        char **temp_pathname)
+                        gdb::unique_xmalloc_ptr<char> *temp_pathname)
 {
   char *buf, *arch_path, *nto_root;
   const char *endian;
@@ -143,9 +143,9 @@ nto_find_and_open_solib (const char *solib, unsigned o_flags,
       if (temp_pathname)
        {
          if (ret >= 0)
-           *temp_pathname = gdb_realpath (arch_path).release ();
+           *temp_pathname = gdb_realpath (arch_path);
          else
-           *temp_pathname = NULL;
+           temp_pathname->reset (NULL);
        }
     }
   return ret;
index 4eaea48c5d488fd4bd07534f829915d14d357375..f82cad1306a5446d8b6a5e2f34c3aa94b05ba276 100644 (file)
@@ -170,7 +170,8 @@ void nto_relocate_section_addresses (struct so_list *,
 
 int nto_map_arch_to_cputype (const char *);
 
-int nto_find_and_open_solib (const char *, unsigned, char **);
+int nto_find_and_open_solib (const char *, unsigned,
+                            gdb::unique_xmalloc_ptr<char> *);
 
 enum gdb_osabi nto_elf_osabi_sniffer (bfd *abfd);
 
index dc13fd560d8972de72b843b53ad8c46b9d22e52c..ac0ee0a5a64c592b2c2807d354b6d06bac012a8b 100644 (file)
@@ -1203,14 +1203,14 @@ psymtab_to_fullname (struct partial_symtab *ps)
      to handle cases like the file being moved.  */
   if (ps->fullname == NULL)
     {
-      int fd = find_and_open_source (ps->filename, ps->dirname, &ps->fullname);
+      gdb::unique_xmalloc_ptr<char> fullname;
+      int fd = find_and_open_source (ps->filename, ps->dirname, &fullname);
+      ps->fullname = fullname.release ();
 
       if (fd >= 0)
        close (fd);
       else
        {
-         gdb::unique_xmalloc_ptr<char> fullname;
-
          /* rewrite_source_path would be applied by find_and_open_source, we
             should report the pathname where GDB tried to find the file.  */
 
index 67a490e8d737261f8708ffb1fb19a5fe434bec0a..41fba902f833d67c95c3f1ecb4b9310d03c16094 100644 (file)
@@ -152,7 +152,7 @@ solib_find_1 (const char *in_pathname, int *fd, int is_solib)
 {
   const struct target_so_ops *ops = solib_ops (target_gdbarch ());
   int found_file = -1;
-  char *temp_pathname = NULL;
+  gdb::unique_xmalloc_ptr<char> temp_pathname;
   const char *fskind = effective_target_file_system_kind ();
   const char *sysroot = gdb_sysroot;
   int prefix_len, orig_prefix_len;
@@ -214,7 +214,7 @@ solib_find_1 (const char *in_pathname, int *fd, int is_solib)
   */
 
   if (!IS_TARGET_ABSOLUTE_PATH (fskind, in_pathname) || sysroot == NULL)
-    temp_pathname = xstrdup (in_pathname);
+    temp_pathname.reset (xstrdup (in_pathname));
   else
     {
       int need_dir_separator;
@@ -240,23 +240,21 @@ solib_find_1 (const char *in_pathname, int *fd, int is_solib)
                             || strcmp (TARGET_SYSROOT_PREFIX, sysroot) == 0);
 
       /* Cat the prefixed pathname together.  */
-      temp_pathname = concat (sysroot,
-                             need_dir_separator ? SLASH_STRING : "",
-                             in_pathname, (char *) NULL);
+      temp_pathname.reset (concat (sysroot,
+                                  need_dir_separator ? SLASH_STRING : "",
+                                  in_pathname, (char *) NULL));
     }
 
   /* Handle files to be accessed via the target.  */
-  if (is_target_filename (temp_pathname))
+  if (is_target_filename (temp_pathname.get ()))
     {
       if (fd != NULL)
        *fd = -1;
-      return gdb::unique_xmalloc_ptr<char> (temp_pathname);
+      return temp_pathname;
     }
 
   /* Now see if we can open it.  */
-  found_file = gdb_open_cloexec (temp_pathname, O_RDONLY | O_BINARY, 0);
-  if (found_file < 0)
-    xfree (temp_pathname);
+  found_file = gdb_open_cloexec (temp_pathname.get (), O_RDONLY | O_BINARY, 0);
 
   /* If the search in gdb_sysroot failed, and the path name has a
      drive spec (e.g, c:/foo), try stripping ':' from the drive spec,
@@ -268,33 +266,30 @@ solib_find_1 (const char *in_pathname, int *fd, int is_solib)
       && HAS_TARGET_DRIVE_SPEC (fskind, in_pathname))
     {
       int need_dir_separator = !IS_DIR_SEPARATOR (in_pathname[2]);
-      char *drive = savestring (in_pathname, 1);
+      char drive[2] = { in_pathname[0], '\0' };
 
-      temp_pathname = concat (sysroot,
-                             SLASH_STRING,
-                             drive,
-                             need_dir_separator ? SLASH_STRING : "",
-                             in_pathname + 2, (char *) NULL);
-      xfree (drive);
+      temp_pathname.reset (concat (sysroot,
+                                  SLASH_STRING,
+                                  drive,
+                                  need_dir_separator ? SLASH_STRING : "",
+                                  in_pathname + 2, (char *) NULL));
 
-      found_file = gdb_open_cloexec (temp_pathname, O_RDONLY | O_BINARY, 0);
+      found_file = gdb_open_cloexec (temp_pathname.get (),
+                                    O_RDONLY | O_BINARY, 0);
       if (found_file < 0)
        {
-         xfree (temp_pathname);
-
          /* If the search in gdb_sysroot still failed, try fully
             stripping the drive spec, and trying once more in the
             sysroot before giving up.
 
             c:/foo/bar.dll ==> /sysroot/foo/bar.dll.  */
 
-         temp_pathname = concat (sysroot,
-                                 need_dir_separator ? SLASH_STRING : "",
-                                 in_pathname + 2, (char *) NULL);
+         temp_pathname.reset (concat (sysroot,
+                                      need_dir_separator ? SLASH_STRING : "",
+                                      in_pathname + 2, (char *) NULL));
 
-         found_file = gdb_open_cloexec (temp_pathname, O_RDONLY | O_BINARY, 0);
-         if (found_file < 0)
-           xfree (temp_pathname);
+         found_file = gdb_open_cloexec (temp_pathname.get (),
+                                        O_RDONLY | O_BINARY, 0);
        }
     }
 
@@ -304,7 +299,7 @@ solib_find_1 (const char *in_pathname, int *fd, int is_solib)
      needs to be freed.  */
 
   if (found_file < 0)
-    temp_pathname = NULL;
+    temp_pathname.reset (NULL);
 
   /* If the search in gdb_sysroot failed, and the path name is
      absolute at this point, make it relative.  (openp will try and open the
@@ -366,7 +361,7 @@ solib_find_1 (const char *in_pathname, int *fd, int is_solib)
   else
     *fd = found_file;
 
-  return gdb::unique_xmalloc_ptr<char> (temp_pathname);
+  return temp_pathname;
 }
 
 /* Return the full pathname of the main executable, or NULL if not
@@ -407,10 +402,7 @@ 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...)  */
 
-      char *full_path = NULL;
-      if (source_full_path_of (in_pathname, &full_path))
-       result.reset (full_path);
-      else
+      if (!source_full_path_of (in_pathname, &result))
        result.reset (xstrdup (in_pathname));
       if (fd != NULL)
        *fd = -1;
index aba00ebb2156bee2bf2c0d0f87fc781c4765c032..1df45e4d9327cdae69d2dafaf743602c21458d58 100644 (file)
@@ -136,7 +136,8 @@ struct target_so_ops
        pointer to a malloc'd and realpath'd copy of SONAME is stored there,
        otherwise NULL is stored there.  */
     int (*find_and_open_solib) (const char *soname,
-        unsigned o_flags, char **temp_pathname);
+                               unsigned o_flags,
+                               gdb::unique_xmalloc_ptr<char> *temp_pathname);
 
     /* Hook for looking up global symbols in a library-specific way.  */
     struct block_symbol (*lookup_lib_global_symbol)
index eba06f6e6baa725c8af46a58f77b7dac00b0d2a7..8a27b2e666fbf3eabe3100100cffab354eea2798 100644 (file)
@@ -737,7 +737,7 @@ is_regular_file (const char *name, int *errno_ptr)
     >>>>  eg executable, non-directory.  */
 int
 openp (const char *path, openp_flags opts, const char *string,
-       int mode, char **filename_opened)
+       int mode, gdb::unique_xmalloc_ptr<char> *filename_opened)
 {
   int fd;
   char *filename;
@@ -896,11 +896,11 @@ done:
     {
       /* If a file was opened, canonicalize its filename.  */
       if (fd < 0)
-       *filename_opened = NULL;
+       filename_opened->reset (NULL);
       else if ((opts & OPF_RETURN_REALPATH) != 0)
-       *filename_opened = gdb_realpath (filename).release ();
+       *filename_opened = gdb_realpath (filename);
       else
-       *filename_opened = gdb_abspath (filename).release ();
+       *filename_opened = gdb_abspath (filename);
     }
 
   errno = last_errno;
@@ -920,7 +920,8 @@ done:
 
    Else, this functions returns 0, and FULL_PATHNAME is set to NULL.  */
 int
-source_full_path_of (const char *filename, char **full_pathname)
+source_full_path_of (const char *filename,
+                    gdb::unique_xmalloc_ptr<char> *full_pathname)
 {
   int fd;
 
@@ -929,7 +930,7 @@ source_full_path_of (const char *filename, char **full_pathname)
              filename, O_RDONLY, full_pathname);
   if (fd < 0)
     {
-      *full_pathname = NULL;
+      full_pathname->reset (NULL);
       return 0;
     }
 
@@ -1011,7 +1012,7 @@ rewrite_source_path (const char *path)
 int
 find_and_open_source (const char *filename,
                      const char *dirname,
-                     char **fullname)
+                     gdb::unique_xmalloc_ptr<char> *fullname)
 {
   char *path = source_path;
   const char *p;
@@ -1024,27 +1025,21 @@ find_and_open_source (const char *filename,
       /* The user may have requested that source paths be rewritten
          according to substitution rules he provided.  If a substitution
          rule applies to this path, then apply it.  */
-      char *rewritten_fullname = rewrite_source_path (*fullname).release ();
+      gdb::unique_xmalloc_ptr<char> rewritten_fullname
+       = rewrite_source_path (fullname->get ());
 
       if (rewritten_fullname != NULL)
-        {
-          xfree (*fullname);
-          *fullname = rewritten_fullname;
-        }
+       *fullname = std::move (rewritten_fullname);
 
-      result = gdb_open_cloexec (*fullname, OPEN_MODE, 0);
+      result = gdb_open_cloexec (fullname->get (), OPEN_MODE, 0);
       if (result >= 0)
        {
-         char *lpath = gdb_realpath (*fullname).release ();
-
-         xfree (*fullname);
-         *fullname = lpath;
+         *fullname = gdb_realpath (fullname->get ());
          return result;
        }
 
       /* Didn't work -- free old one, try again.  */
-      xfree (*fullname);
-      *fullname = NULL;
+      fullname->reset (NULL);
     }
 
   gdb::unique_xmalloc_ptr<char> rewritten_dirname;
@@ -1115,7 +1110,10 @@ open_source_file (struct symtab *s)
   if (!s)
     return -1;
 
-  return find_and_open_source (s->filename, SYMTAB_DIRNAME (s), &s->fullname);
+  gdb::unique_xmalloc_ptr<char> fullname;
+  int fd = find_and_open_source (s->filename, SYMTAB_DIRNAME (s), &fullname);
+  s->fullname = fullname.release ();
+  return fd;
 }
 
 /* Finds the fullname that a symtab represents.
@@ -1135,8 +1133,7 @@ symtab_to_fullname (struct symtab *s)
      to handle cases like the file being moved.  */
   if (s->fullname == NULL)
     {
-      int fd = find_and_open_source (s->filename, SYMTAB_DIRNAME (s),
-                                    &s->fullname);
+      int fd = open_source_file (s);
 
       if (fd >= 0)
        close (fd);
index a21e919b28bd0c7aaf3fbfe98a656560437378af..a8769506a0e7082eba096e8cd1fea1ba40cdd43b 100644 (file)
@@ -32,9 +32,10 @@ enum openp_flag
 
 DEF_ENUM_FLAGS_TYPE(openp_flag, openp_flags);
 
-extern int openp (const char *, openp_flags, const char *, int, char **);
+extern int openp (const char *, openp_flags, const char *, int,
+                 gdb::unique_xmalloc_ptr<char> *);
 
-extern int source_full_path_of (const char *, char **);
+extern int source_full_path_of (const char *, gdb::unique_xmalloc_ptr<char> *);
 
 extern void mod_path (const char *, char **);
 
@@ -67,7 +68,7 @@ extern void init_source_path (void);
      FULLNAME is set to NULL.  */
 extern int find_and_open_source (const char *filename,
                                 const char *dirname,
-                                char **fullname);
+                                gdb::unique_xmalloc_ptr<char> *fullname);
 
 /* Open a source file given a symtab S.  Returns a file descriptor or
    negative number for error.  */
index ab6ec1cdadf5b80ee56cc543415a8f25ecf192be..699d9e6fe06e82102a8bc0f887c4c9c9bc8208ac 100644 (file)
@@ -1719,12 +1719,10 @@ gdb_bfd_ref_ptr
 symfile_bfd_open (const char *name)
 {
   int desc = -1;
-  struct cleanup *back_to = make_cleanup (null_cleanup, 0);
 
+  gdb::unique_xmalloc_ptr<char> absolute_name;
   if (!is_target_filename (name))
     {
-      char *absolute_name;
-
       gdb::unique_xmalloc_ptr<char> expanded_name (tilde_expand (name));
 
       /* Look down path for it, allocate 2nd new malloc'd copy.  */
@@ -1745,8 +1743,7 @@ symfile_bfd_open (const char *name)
       if (desc < 0)
        perror_with_name (expanded_name.get ());
 
-      make_cleanup (xfree, absolute_name);
-      name = absolute_name;
+      name = absolute_name.get ();
     }
 
   gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (name, gnutarget, desc));
@@ -1761,8 +1758,6 @@ symfile_bfd_open (const char *name)
     error (_("`%s': can't read symbols: %s."), name,
           bfd_errmsg (bfd_get_error ()));
 
-  do_cleanups (back_to);
-
   return sym_bfd;
 }
 
This page took 0.045371 seconds and 4 git commands to generate.