gdb: add gdb_rl_tilde_expand util

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>
This commit is contained in:
Simon Marchi
2025-10-23 12:34:23 -04:00
parent 5f71957b23
commit f9e262e1cb
21 changed files with 55 additions and 54 deletions

View 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

View 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;

View 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;

View 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

View 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:

View 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)

View 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);

View 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"

View 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

View 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". */

View File

@@ -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."));

View 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;

View 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"

View 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)

View 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
{

View 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)
{

View 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;

View 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;

View 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)"),

View 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,

View 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);
/* 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;