]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
gdb: add gdb_rl_tilde_expand util
authorSimon Marchi <simon.marchi@efficios.com>
Thu, 23 Oct 2025 16:34:23 +0000 (12:34 -0400)
committerSimon Marchi <simon.marchi@efficios.com>
Fri, 24 Oct 2025 15:02:22 +0000 (11:02 -0400)
Add gdb_rl_tilde_expand, a wrapper around readline's tilde_expand that
returns a gdb::unique_xmalloc_ptr<char>.  Change all callers of
tilde_expand to use gdb_rl_tilde_expand (even the couple of spots that
release it immediatly, for consistency).  This simplifies a few callers.

The name gdb_tilde_expand is already taken by a home-made implementation
in gdbsupport/gdb_tilde_expand.{h.cc}.  I wonder if we could just use
that one instead of readline's tilde_expand, but that's an orthogonal
question.  I don't know how they differ, and I don't want to introduce
behavior changes in this patch.

Change-Id: I6d34eef19f86473226df4ae56d07dc01912e3131
Approved-By: Tom Tromey <tom@tromey.com>
21 files changed:
gdb/auto-load.c
gdb/breakpoint.c
gdb/cli/cli-cmds.c
gdb/cli/cli-dump.c
gdb/cli/cli-setshow.c
gdb/compile/compile-object-load.c
gdb/completer.c
gdb/corelow.c
gdb/exec.c
gdb/gcore.c
gdb/jit.c
gdb/psymtab.c
gdb/python/python.c
gdb/solib.c
gdb/source.c
gdb/symfile.c
gdb/symmisc.c
gdb/target-descriptions.c
gdb/tracefile-tfile.c
gdb/utils.c
gdb/utils.h

index 169a7359b238fb88f7f67f4a05231207bcbf6ecf..23ec927bb72b37362f46c1f42adc3f6927bf9cc3 100644 (file)
@@ -30,7 +30,6 @@
 #include "cli/cli-cmds.h"
 #include "cli/cli-decode.h"
 #include "cli/cli-setshow.h"
-#include "readline/tilde.h"
 #include "completer.h"
 #include "fnmatch.h"
 #include "top.h"
@@ -269,7 +268,8 @@ auto_load_safe_path_vec_update (void)
   for (size_t i = 0; i < len; i++)
     {
       gdb::unique_xmalloc_ptr<char> &in_vec = auto_load_safe_path_vec[i];
-      gdb::unique_xmalloc_ptr<char> expanded (tilde_expand (in_vec.get ()));
+      gdb::unique_xmalloc_ptr<char> expanded
+       = gdb_rl_tilde_expand (in_vec.get ());
       gdb::unique_xmalloc_ptr<char> real_path = gdb_realpath (expanded.get ());
 
       /* Ensure the current entry is at least tilde_expand-ed.  ORIGINAL makes
index a70410a25edc7c30854bc97eeb35d453f72af863..382d4706839da9b0dd2b671c1ba35129f39c4b2e 100644 (file)
@@ -70,9 +70,6 @@
 #include "cli/cli-decode.h"
 #include "break-cond-parse.h"
 
-/* readline include files */
-#include "readline/tilde.h"
-
 /* readline defines this.  */
 #undef savestring
 
@@ -14522,7 +14519,8 @@ save_breakpoints (const char *filename, int from_tty,
       return;
     }
 
-  gdb::unique_xmalloc_ptr<char> expanded_filename (tilde_expand (filename));
+  gdb::unique_xmalloc_ptr<char> expanded_filename
+    = gdb_rl_tilde_expand (filename);
 
   stdio_file fp;
 
index 1eda034ff12e850f9977d1170c9bb1a1a013be73..1374481cc9918a112ec3544307db4f7bca265bf9 100644 (file)
@@ -19,7 +19,6 @@
 
 #include "arch-utils.h"
 #include "exceptions.h"
-#include "readline/tilde.h"
 #include "completer.h"
 #include "target.h"
 #include "gdbsupport/gdb_wait.h"
@@ -520,7 +519,7 @@ cd_command (const char *dir, int from_tty)
   dont_repeat ();
 
   gdb::unique_xmalloc_ptr<char> dir_holder
-    (tilde_expand (dir != NULL ? dir : "~"));
+    = gdb_rl_tilde_expand (dir != NULL ? dir : "~");
   dir = dir_holder.get ();
 
   if (chdir (dir) < 0)
@@ -638,7 +637,8 @@ find_and_open_script (const char *script_file, int search_path)
   openp_flags search_flags = OPF_TRY_CWD_FIRST | OPF_RETURN_REALPATH;
   std::optional<open_script> opened;
 
-  gdb::unique_xmalloc_ptr<char> file (tilde_expand (script_file));
+  gdb::unique_xmalloc_ptr<char> file
+    = gdb_rl_tilde_expand (script_file);
 
   if (search_path)
     search_flags |= OPF_SEARCH_IN_PATH;
index b2fb13983e73bd9234596a820691f803f5fdb094..b33a1b9ac994440032170c6e6931b1e84b6fdbe5 100644 (file)
@@ -24,7 +24,6 @@
 #include "value.h"
 #include "completer.h"
 #include "target.h"
-#include "readline/tilde.h"
 #include "gdbcore.h"
 #include "cli/cli-utils.h"
 #include "gdb_bfd.h"
@@ -77,7 +76,7 @@ scan_filename (const char **cmd, const char *defname)
     }
   gdb_assert (filename != NULL);
 
-  return gdb::unique_xmalloc_ptr<char> (tilde_expand (filename.get ()));
+  return gdb_rl_tilde_expand (filename.get ());
 }
 
 static gdb_bfd_ref_ptr
index d92d14e7bb41f4873a311a8ae20b3f558c7fa553..a97cd1b392b254d53a741c804bed12da600303c7 100644 (file)
@@ -15,7 +15,6 @@
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
 
-#include "readline/tilde.h"
 #include "value.h"
 #include "arch-utils.h"
 #include "observable.h"
@@ -385,7 +384,7 @@ do_set_command (const char *arg, int from_tty, struct cmd_list_element *c)
       [[fallthrough]];
     case var_optional_filename:
       {
-       char *val = NULL;
+       gdb::unique_xmalloc_ptr<char> val;
 
        if (*arg != '\0')
          {
@@ -397,14 +396,13 @@ do_set_command (const char *arg, int from_tty, struct cmd_list_element *c)
            gdb::unique_xmalloc_ptr<char> copy
              = make_unique_xstrndup (arg, ptr + 1 - arg);
 
-           val = tilde_expand (copy.get ());
+           val = gdb_rl_tilde_expand (copy.get ());
          }
        else
-         val = xstrdup ("");
+         val = make_unique_xstrdup ("");
 
        option_changed
-         = c->var->set<std::string> (std::string (val));
-       xfree (val);
+         = c->var->set<std::string> (std::string (val.get ()));
       }
       break;
     case var_boolean:
index 6bee32ec31b49c5653c7b48203046c825330df2e..d9b0539cf7fe4570872f09544b0a0224181ee6a2 100644 (file)
@@ -22,7 +22,6 @@
 #include "command.h"
 #include "objfiles.h"
 #include "gdbcore.h"
-#include "readline/tilde.h"
 #include "bfdlink.h"
 #include "cli/cli-cmds.h"
 #include "regcache.h"
@@ -613,7 +612,7 @@ compile_object_load (const compile_file_names &file_names,
   struct type *expect_return_type;
 
   gdb::unique_xmalloc_ptr<char> filename
-    (tilde_expand (file_names.object_file ()));
+    = gdb_rl_tilde_expand (file_names.object_file ());
 
   gdb_bfd_ref_ptr abfd (gdb_bfd_open (filename.get (), gnutarget));
   if (abfd == NULL)
index df9feffb68e4a079ff6c794b17d1c2fc3e2a5372..677b9d1565029a379eb51d4cb14548187944a148 100644 (file)
@@ -3183,7 +3183,7 @@ gdb_print_filename (char *to_print, char *full_pathname, int prefix_bytes,
                    const struct match_list_displayer *displayer)
 {
   int printed_len, extension_char, slen, tlen;
-  char *s, c, *new_full_pathname;
+  char c, *new_full_pathname;
   const char *dn;
   extern int _rl_complete_mark_directories;
 
@@ -3220,7 +3220,7 @@ gdb_print_filename (char *to_print, char *full_pathname, int prefix_bytes,
            dn = "/";           /* don't turn /// into // */
          else
            dn = full_pathname;
-         s = tilde_expand (dn);
+         char *s = gdb_rl_tilde_expand (dn).release ();
          if (rl_directory_completion_hook)
            (*rl_directory_completion_hook) (&s);
 
@@ -3245,20 +3245,22 @@ gdb_print_filename (char *to_print, char *full_pathname, int prefix_bytes,
 
          xfree (new_full_pathname);
          to_print[-1] = c;
+
+         xfree (s);
        }
       else
        {
-         s = tilde_expand (full_pathname);
+         gdb::unique_xmalloc_ptr<char> s
+           = gdb_rl_tilde_expand (full_pathname);
 #if defined (VISIBLE_STATS)
          if (rl_visible_stats)
            extension_char = stat_char (s);
          else
 #endif
-           if (gdb_path_isdir (s))
+           if (gdb_path_isdir (s.get ()))
              extension_char = '/';
        }
 
-      xfree (s);
       if (extension_char)
        {
          displayer->putch (displayer, extension_char);
index 218504bcdc85db51e4731771cfd05deab9d75d90..a9098e6e00ee887c2dc3cb1b3e6dd9820671edc3 100644 (file)
@@ -35,7 +35,6 @@
 #include "regset.h"
 #include "symfile.h"
 #include "exec.h"
-#include "readline/tilde.h"
 #include "solib.h"
 #include "filenames.h"
 #include "progspace.h"
index a415abc13f4512f48ebaf1697ef1e42eb6efe2f9..581ec6e70fc61683984826737c0850bc6e1ee91a 100644 (file)
@@ -39,7 +39,6 @@
 #include "build-id.h"
 
 #include <fcntl.h>
-#include "readline/tilde.h"
 #include "gdbcore.h"
 
 #include <sys/stat.h>
@@ -558,7 +557,8 @@ exec_file_command (const char *args, int from_tty)
       if (*argv == NULL)
        error (_("No executable file name was specified"));
 
-      gdb::unique_xmalloc_ptr<char> filename (tilde_expand (*argv));
+      gdb::unique_xmalloc_ptr<char> filename
+       = gdb_rl_tilde_expand (*argv);
       exec_file_attach (filename.get (), from_tty);
     }
   else
index 59c2afdbe159191276f4dca3da5e406cc6dcd7e3..507c4631e15e654c6ac42b24951676eb9858f277 100644 (file)
@@ -32,7 +32,6 @@
 #include "regcache.h"
 #include "regset.h"
 #include "gdb_bfd.h"
-#include "readline/tilde.h"
 #include <algorithm>
 #include "gdbsupport/gdb_unlinker.h"
 #include "gdbsupport/byte-vector.h"
@@ -147,7 +146,7 @@ gcore_command (const char *args, int from_tty)
     noprocess ();
 
   if (args && *args)
-    corefilename.reset (tilde_expand (args));
+    corefilename = gdb_rl_tilde_expand (args);
   else
     {
       /* Default corefile name is "core.PID".  */
index ac5bf397345ab5d92dd43df16ad39116ab4f060b..b2b08005feb9821f5fcaaedcc70bc52b4466afa9 100644 (file)
--- a/gdb/jit.c
+++ b/gdb/jit.c
@@ -40,7 +40,6 @@
 #include "gdbsupport/gdb-dlfcn.h"
 #include <sys/stat.h>
 #include "gdb_bfd.h"
-#include "readline/tilde.h"
 #include "completer.h"
 #include <forward_list>
 
@@ -179,7 +178,8 @@ jit_reader_load_command (const char *args, int from_tty)
 {
   if (args == NULL)
     error (_("No reader name provided."));
-  gdb::unique_xmalloc_ptr<char> file (tilde_expand (args));
+  gdb::unique_xmalloc_ptr<char> file
+    = gdb_rl_tilde_expand (args);
 
   if (loaded_jit_reader != NULL)
     error (_("JIT reader already loaded.  Run jit-reader-unload first."));
index 413212d25ed74d8c9a9423973afb55899ad2b978..bc5123e7bcb413590e01e1f2755e058b75c1d719 100644 (file)
@@ -27,7 +27,6 @@
 #include "gdbtypes.h"
 #include "ui-out.h"
 #include "command.h"
-#include "readline/tilde.h"
 #include "gdbsupport/gdb_regex.h"
 #include "dictionary.h"
 #include "language.h"
@@ -1245,7 +1244,7 @@ maintenance_print_psymbols (const char *args, int from_tty)
       if (argv[outfile_idx + 1] != NULL)
        error (_("Junk at end of command"));
       gdb::unique_xmalloc_ptr<char> outfile_name
-       (tilde_expand (argv[outfile_idx]));
+       = gdb_rl_tilde_expand (argv[outfile_idx]);
       if (!arg_outfile.open (outfile_name.get (), FOPEN_WT))
        perror_with_name (outfile_name.get ());
       outfile = &arg_outfile;
index 7e1975682f5113ad94cd51a65c895f0e3002f116..0bf53cac2a92332a95319567292de75aabf223c8 100644 (file)
@@ -27,7 +27,6 @@
 #include "value.h"
 #include "language.h"
 #include "gdbsupport/event-loop.h"
-#include "readline/tilde.h"
 #include "python.h"
 #include "extension-priv.h"
 #include "cli/cli-utils.h"
index f45e3668b899b02c67872559ab70f0f0228838fc..dade2a76a48c394a2317de5dcbc2f8962924394e 100644 (file)
@@ -38,7 +38,6 @@
 #include "filenames.h"
 #include "exec.h"
 #include "observable.h"
-#include "readline/tilde.h"
 #include "solib.h"
 #include "interps.h"
 #include "filesystem.h"
@@ -500,7 +499,8 @@ solib_ops::iterate_over_objfiles_in_search_order
 static int
 solib_map_sections (solib &so)
 {
-  gdb::unique_xmalloc_ptr<char> filename (tilde_expand (so.name.c_str ()));
+  gdb::unique_xmalloc_ptr<char> filename
+    = gdb_rl_tilde_expand (so.name.c_str ());
   gdb_bfd_ref_ptr abfd (so.ops ().bfd_open (filename.get ()));
 
   /* If we have a core target then the core target might have some helpful
@@ -1418,8 +1418,8 @@ reload_shared_libraries_1 (int from_tty)
       if (from_tty)
        add_flags |= SYMFILE_VERBOSE;
 
-      gdb::unique_xmalloc_ptr<char> filename (
-       tilde_expand (so.original_name.c_str ()));
+      gdb::unique_xmalloc_ptr<char> filename
+       = gdb_rl_tilde_expand (so.original_name.c_str ());
 
       gdb_bfd_ref_ptr abfd = so.ops ().bfd_open (filename.get ());
       if (abfd != NULL)
index c2f21279dadae79c4c0d015858027f2f3bab627c..e56f2b5d73607d5f1338b828e5df4be54faa1024 100644 (file)
@@ -39,7 +39,6 @@
 #include "filenames.h"
 #include "completer.h"
 #include "ui-out.h"
-#include "readline/tilde.h"
 #include "gdbsupport/enum-flags.h"
 #include "gdbsupport/scoped_fd.h"
 #include <algorithm>
@@ -564,8 +563,7 @@ add_path (const char *dirname, char **which_path, int parse_separators)
       if (name[0] == '\0')
        goto skip_dup;
       if (name[0] == '~')
-       new_name_holder
-         = gdb::unique_xmalloc_ptr<char[]> (tilde_expand (name)).get ();
+       new_name_holder = gdb_rl_tilde_expand (name).get ();
 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
       else if (IS_ABSOLUTE_PATH (name) && p == name + 2) /* "d:" => "d:." */
        new_name_holder = std::string (name) + ".";
@@ -850,7 +848,7 @@ openp (const char *path, openp_flags opts, const char *string,
       else if (strchr (dir, '~'))
        {
          /* See whether we need to expand the tilde.  */
-         filename = gdb::unique_xmalloc_ptr<char> (tilde_expand (dir)).get ();
+         filename = gdb_rl_tilde_expand (dir).get ();
        }
       else
        {
index acf5edaaadbd5802faaa8ca945d4b9086e99d87a..806547d9d6fd978edc9818471d0308f619b9e9e4 100644 (file)
@@ -39,7 +39,6 @@
 #include "filenames.h"
 #include "gdbsupport/gdb_obstack.h"
 #include "completer.h"
-#include "readline/tilde.h"
 #include "block.h"
 #include "observable.h"
 #include "exec.h"
@@ -1685,7 +1684,8 @@ symfile_bfd_open (const char *name)
   gdb::unique_xmalloc_ptr<char> absolute_name;
   if (!is_target_filename (name))
     {
-      gdb::unique_xmalloc_ptr<char> expanded_name (tilde_expand (name));
+      gdb::unique_xmalloc_ptr<char> expanded_name
+       = gdb_rl_tilde_expand (name);
 
       /* Look down path for it, allocate 2nd new malloc'd copy.  */
       desc = openp (getenv ("PATH"),
@@ -2005,7 +2005,8 @@ generic_load (const char *args, int from_tty)
 
   gdb_argv argv (args);
 
-  gdb::unique_xmalloc_ptr<char> filename (tilde_expand (argv[0]));
+  gdb::unique_xmalloc_ptr<char> filename
+    = gdb_rl_tilde_expand (argv[0]);
 
   if (argv[1] != NULL)
     {
@@ -2222,7 +2223,7 @@ add_symbol_file_command (const char *args, int from_tty)
          if (filename == NULL)
            {
              /* First non-option argument is always the filename.  */
-             filename.reset (tilde_expand (arg));
+             filename = gdb_rl_tilde_expand (arg);
            }
          else if (!seen_addr)
            {
index 06b11a47784d10460d2f27e7ec0f53a14d130f28..815a51af82c0781617384b9b38b28b72f90fb419 100644 (file)
@@ -37,7 +37,6 @@
 #include "typeprint.h"
 #include "cli/cli-cmds.h"
 #include "source.h"
-#include "readline/tilde.h"
 #include <cli/cli-style.h>
 #include "gdbsupport/buildargv.h"
 
@@ -435,7 +434,7 @@ maintenance_print_symbols (const char *args, int from_tty)
       if (argv[outfile_idx + 1] != NULL)
        error (_("Junk at end of command"));
       gdb::unique_xmalloc_ptr<char> outfile_name
-       (tilde_expand (argv[outfile_idx]));
+       = gdb_rl_tilde_expand (argv[outfile_idx]);
       if (!arg_outfile.open (outfile_name.get (), FOPEN_WT))
        perror_with_name (outfile_name.get ());
       outfile = &arg_outfile;
@@ -698,7 +697,7 @@ maintenance_print_msymbols (const char *args, int from_tty)
       if (argv[outfile_idx + 1] != NULL)
        error (_("Junk at end of command"));
       gdb::unique_xmalloc_ptr<char> outfile_name
-       (tilde_expand (argv[outfile_idx]));
+       = gdb_rl_tilde_expand (argv[outfile_idx]);
       if (!arg_outfile.open (outfile_name.get (), FOPEN_WT))
        perror_with_name (outfile_name.get ());
       outfile = &arg_outfile;
index 5a5d03ec5d86e6ed723e98e227a872ef3ab94cc6..57177202f1ab5344332c459d950767517ef523ba 100644 (file)
@@ -34,7 +34,6 @@
 #include "inferior.h"
 #include <algorithm>
 #include "completer.h"
-#include "readline/tilde.h"
 #include "cli/cli-style.h"
 
 /* Types.  */
@@ -1860,7 +1859,7 @@ maintenance_check_xml_descriptions (const char *dir, int from_tty)
   if (dir == NULL)
     error (_("Missing dir name"));
 
-  gdb::unique_xmalloc_ptr<char> dir1 (tilde_expand (dir));
+  gdb::unique_xmalloc_ptr<char> dir1 = gdb_rl_tilde_expand (dir);
   std::string feature_dir (dir1.get ());
   unsigned int failed = 0;
 
index a45001cf318fdb2ff9c1c71fa118148248755ee8..ffc429f4f56ef0b409e4e11091eb665b37e7c463 100644 (file)
@@ -19,7 +19,6 @@
 
 #include "extract-store-integer.h"
 #include "tracefile.h"
-#include "readline/tilde.h"
 #include "gdbsupport/filestuff.h"
 #include "gdbsupport/rsp-low.h"
 #include "regcache.h"
@@ -121,7 +120,7 @@ tfile_start (struct trace_file_writer *self, const char *filename)
   struct tfile_trace_file_writer *writer
     = (struct tfile_trace_file_writer *) self;
 
-  writer->pathname = tilde_expand (filename);
+  writer->pathname = gdb_rl_tilde_expand (filename).release ();
   writer->fp = gdb_fopen_cloexec (writer->pathname, "wb").release ();
   if (writer->fp == NULL)
     error (_("Unable to open file '%s' for saving trace data (%s)"),
index 8f00badfd78619dede89762075d1a6e45bba41b5..ffe0d639e3a768bf8d4410b156dbd5e631f5c555 100644 (file)
@@ -80,6 +80,7 @@
 #include "run-on-main-thread.h"
 #include "gdbsupport/gdb_tilde_expand.h"
 #include "gdbsupport/eintr.h"
+#include "readline/tilde.h"
 
 void (*deprecated_error_begin_hook) (void);
 
@@ -3583,6 +3584,14 @@ strip_leading_path_elements (const char *path, int n)
 
 /* See utils.h.  */
 
+gdb::unique_xmalloc_ptr<char>
+gdb_rl_tilde_expand (const char *path)
+{
+  return gdb::unique_xmalloc_ptr<char> (tilde_expand (path));
+}
+
+/* See utils.h.  */
+
 void
 copy_bitwise (gdb_byte *dest, ULONGEST dest_offset,
              const gdb_byte *source, ULONGEST source_offset,
index b37e8f7d7a1719fa84fe99b94d90163b1e19a031..ddeac46b1da373992371bd7d0131ae77592bb4ce 100644 (file)
@@ -138,7 +138,11 @@ std::string gdb_ldirname (const char *filename);
 extern int count_path_elements (const char *path);
 
 extern const char *strip_leading_path_elements (const char *path, int n);
-\f
+
+/* Wrapper around readline's tilde_expand, to return a unique pointer.  */
+
+extern gdb::unique_xmalloc_ptr<char> gdb_rl_tilde_expand (const char *path);
+
 /* GDB output, ui_file utilities.  */
 
 struct ui_file;