From f9e262e1cb7af35aeaafae7d078b134f8ab8b3cf Mon Sep 17 00:00:00 2001 From: Simon Marchi Date: Thu, 23 Oct 2025 12:34:23 -0400 Subject: [PATCH] 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. 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 --- gdb/auto-load.c | 4 ++-- gdb/breakpoint.c | 6 ++---- gdb/cli/cli-cmds.c | 6 +++--- gdb/cli/cli-dump.c | 3 +-- gdb/cli/cli-setshow.c | 10 ++++------ gdb/compile/compile-object-load.c | 3 +-- gdb/completer.c | 12 +++++++----- gdb/corelow.c | 1 - gdb/exec.c | 4 ++-- gdb/gcore.c | 3 +-- gdb/jit.c | 4 ++-- gdb/psymtab.c | 3 +-- gdb/python/python.c | 1 - gdb/solib.c | 8 ++++---- gdb/source.c | 6 ++---- gdb/symfile.c | 9 +++++---- gdb/symmisc.c | 5 ++--- gdb/target-descriptions.c | 3 +-- gdb/tracefile-tfile.c | 3 +-- gdb/utils.c | 9 +++++++++ gdb/utils.h | 6 +++++- 21 files changed, 55 insertions(+), 54 deletions(-) diff --git a/gdb/auto-load.c b/gdb/auto-load.c index 169a7359b23..23ec927bb72 100644 --- a/gdb/auto-load.c +++ b/gdb/auto-load.c @@ -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 &in_vec = auto_load_safe_path_vec[i]; - gdb::unique_xmalloc_ptr expanded (tilde_expand (in_vec.get ())); + gdb::unique_xmalloc_ptr expanded + = gdb_rl_tilde_expand (in_vec.get ()); gdb::unique_xmalloc_ptr real_path = gdb_realpath (expanded.get ()); /* Ensure the current entry is at least tilde_expand-ed. ORIGINAL makes diff --git a/gdb/breakpoint.c b/gdb/breakpoint.c index a70410a25ed..382d4706839 100644 --- a/gdb/breakpoint.c +++ b/gdb/breakpoint.c @@ -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 expanded_filename (tilde_expand (filename)); + gdb::unique_xmalloc_ptr expanded_filename + = gdb_rl_tilde_expand (filename); stdio_file fp; diff --git a/gdb/cli/cli-cmds.c b/gdb/cli/cli-cmds.c index 1eda034ff12..1374481cc99 100644 --- a/gdb/cli/cli-cmds.c +++ b/gdb/cli/cli-cmds.c @@ -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 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 opened; - gdb::unique_xmalloc_ptr file (tilde_expand (script_file)); + gdb::unique_xmalloc_ptr file + = gdb_rl_tilde_expand (script_file); if (search_path) search_flags |= OPF_SEARCH_IN_PATH; diff --git a/gdb/cli/cli-dump.c b/gdb/cli/cli-dump.c index b2fb13983e7..b33a1b9ac99 100644 --- a/gdb/cli/cli-dump.c +++ b/gdb/cli/cli-dump.c @@ -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 (tilde_expand (filename.get ())); + return gdb_rl_tilde_expand (filename.get ()); } static gdb_bfd_ref_ptr diff --git a/gdb/cli/cli-setshow.c b/gdb/cli/cli-setshow.c index d92d14e7bb4..a97cd1b392b 100644 --- a/gdb/cli/cli-setshow.c +++ b/gdb/cli/cli-setshow.c @@ -15,7 +15,6 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . */ -#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 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 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 (val)); - xfree (val); + = c->var->set (std::string (val.get ())); } break; case var_boolean: diff --git a/gdb/compile/compile-object-load.c b/gdb/compile/compile-object-load.c index 6bee32ec31b..d9b0539cf7f 100644 --- a/gdb/compile/compile-object-load.c +++ b/gdb/compile/compile-object-load.c @@ -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 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) diff --git a/gdb/completer.c b/gdb/completer.c index df9feffb68e..677b9d15650 100644 --- a/gdb/completer.c +++ b/gdb/completer.c @@ -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 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); diff --git a/gdb/corelow.c b/gdb/corelow.c index 218504bcdc8..a9098e6e00e 100644 --- a/gdb/corelow.c +++ b/gdb/corelow.c @@ -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" diff --git a/gdb/exec.c b/gdb/exec.c index a415abc13f4..581ec6e70fc 100644 --- a/gdb/exec.c +++ b/gdb/exec.c @@ -39,7 +39,6 @@ #include "build-id.h" #include -#include "readline/tilde.h" #include "gdbcore.h" #include @@ -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 filename (tilde_expand (*argv)); + gdb::unique_xmalloc_ptr filename + = gdb_rl_tilde_expand (*argv); exec_file_attach (filename.get (), from_tty); } else diff --git a/gdb/gcore.c b/gdb/gcore.c index 59c2afdbe15..507c4631e15 100644 --- a/gdb/gcore.c +++ b/gdb/gcore.c @@ -32,7 +32,6 @@ #include "regcache.h" #include "regset.h" #include "gdb_bfd.h" -#include "readline/tilde.h" #include #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". */ diff --git a/gdb/jit.c b/gdb/jit.c index ac5bf397345..b2b08005feb 100644 --- a/gdb/jit.c +++ b/gdb/jit.c @@ -40,7 +40,6 @@ #include "gdbsupport/gdb-dlfcn.h" #include #include "gdb_bfd.h" -#include "readline/tilde.h" #include "completer.h" #include @@ -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 file (tilde_expand (args)); + gdb::unique_xmalloc_ptr file + = gdb_rl_tilde_expand (args); if (loaded_jit_reader != NULL) error (_("JIT reader already loaded. Run jit-reader-unload first.")); diff --git a/gdb/psymtab.c b/gdb/psymtab.c index 413212d25ed..bc5123e7bcb 100644 --- a/gdb/psymtab.c +++ b/gdb/psymtab.c @@ -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 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; diff --git a/gdb/python/python.c b/gdb/python/python.c index 7e1975682f5..0bf53cac2a9 100644 --- a/gdb/python/python.c +++ b/gdb/python/python.c @@ -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" diff --git a/gdb/solib.c b/gdb/solib.c index f45e3668b89..dade2a76a48 100644 --- a/gdb/solib.c +++ b/gdb/solib.c @@ -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 filename (tilde_expand (so.name.c_str ())); + gdb::unique_xmalloc_ptr 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 filename ( - tilde_expand (so.original_name.c_str ())); + gdb::unique_xmalloc_ptr filename + = gdb_rl_tilde_expand (so.original_name.c_str ()); gdb_bfd_ref_ptr abfd = so.ops ().bfd_open (filename.get ()); if (abfd != NULL) diff --git a/gdb/source.c b/gdb/source.c index c2f21279dad..e56f2b5d736 100644 --- a/gdb/source.c +++ b/gdb/source.c @@ -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 @@ -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 (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 (tilde_expand (dir)).get (); + filename = gdb_rl_tilde_expand (dir).get (); } else { diff --git a/gdb/symfile.c b/gdb/symfile.c index acf5edaaadb..806547d9d6f 100644 --- a/gdb/symfile.c +++ b/gdb/symfile.c @@ -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 absolute_name; if (!is_target_filename (name)) { - gdb::unique_xmalloc_ptr expanded_name (tilde_expand (name)); + gdb::unique_xmalloc_ptr 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 filename (tilde_expand (argv[0])); + gdb::unique_xmalloc_ptr 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) { diff --git a/gdb/symmisc.c b/gdb/symmisc.c index 06b11a47784..815a51af82c 100644 --- a/gdb/symmisc.c +++ b/gdb/symmisc.c @@ -37,7 +37,6 @@ #include "typeprint.h" #include "cli/cli-cmds.h" #include "source.h" -#include "readline/tilde.h" #include #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 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 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; diff --git a/gdb/target-descriptions.c b/gdb/target-descriptions.c index 5a5d03ec5d8..57177202f1a 100644 --- a/gdb/target-descriptions.c +++ b/gdb/target-descriptions.c @@ -34,7 +34,6 @@ #include "inferior.h" #include #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 dir1 (tilde_expand (dir)); + gdb::unique_xmalloc_ptr dir1 = gdb_rl_tilde_expand (dir); std::string feature_dir (dir1.get ()); unsigned int failed = 0; diff --git a/gdb/tracefile-tfile.c b/gdb/tracefile-tfile.c index a45001cf318..ffc429f4f56 100644 --- a/gdb/tracefile-tfile.c +++ b/gdb/tracefile-tfile.c @@ -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)"), diff --git a/gdb/utils.c b/gdb/utils.c index 8f00badfd78..ffe0d639e3a 100644 --- a/gdb/utils.c +++ b/gdb/utils.c @@ -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 +gdb_rl_tilde_expand (const char *path) +{ + return gdb::unique_xmalloc_ptr (tilde_expand (path)); +} + +/* See utils.h. */ + void copy_bitwise (gdb_byte *dest, ULONGEST dest_offset, const gdb_byte *source, ULONGEST source_offset, diff --git a/gdb/utils.h b/gdb/utils.h index b37e8f7d7a1..ddeac46b1da 100644 --- a/gdb/utils.h +++ b/gdb/utils.h @@ -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 gdb_rl_tilde_expand (const char *path); + /* GDB output, ui_file utilities. */ struct ui_file; -- 2.47.3