mirror of
https://github.com/espressif/binutils-gdb.git
synced 2025-06-17 16:05:56 +08:00
gdbsupport: make gdb_abspath return an std::string
I'm trying to switch these functions to use std::string instead of char arrays, as much as possible. Some callers benefit from it (can avoid doing a copy of the result), while others suffer (have to make one more copy). Change-Id: Iced49b8ee2f189744c5072a3b217aab5af17a993
This commit is contained in:

committed by
Simon Marchi

parent
e0c3463701
commit
7ab2607f97
@ -297,8 +297,8 @@ compile_file_command (const char *args, int from_tty)
|
|||||||
error (_("You must provide a filename for this command."));
|
error (_("You must provide a filename for this command."));
|
||||||
|
|
||||||
args = skip_spaces (args);
|
args = skip_spaces (args);
|
||||||
gdb::unique_xmalloc_ptr<char> abspath = gdb_abspath (args);
|
std::string abspath = gdb_abspath (args);
|
||||||
std::string buffer = string_printf ("#include \"%s\"\n", abspath.get ());
|
std::string buffer = string_printf ("#include \"%s\"\n", abspath.c_str ());
|
||||||
eval_compile_command (NULL, buffer.c_str (), scope, NULL);
|
eval_compile_command (NULL, buffer.c_str (), scope, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -469,7 +469,7 @@ core_target_open (const char *arg, int from_tty)
|
|||||||
gdb::unique_xmalloc_ptr<char> filename (tilde_expand (arg));
|
gdb::unique_xmalloc_ptr<char> filename (tilde_expand (arg));
|
||||||
if (strlen (filename.get ()) != 0
|
if (strlen (filename.get ()) != 0
|
||||||
&& !IS_ABSOLUTE_PATH (filename.get ()))
|
&& !IS_ABSOLUTE_PATH (filename.get ()))
|
||||||
filename = gdb_abspath (filename.get ());
|
filename = make_unique_xstrdup (gdb_abspath (filename.get ()).c_str ());
|
||||||
|
|
||||||
flags = O_BINARY | O_LARGEFILE;
|
flags = O_BINARY | O_LARGEFILE;
|
||||||
if (write_files)
|
if (write_files)
|
||||||
|
@ -298,9 +298,7 @@ set_index_cache_directory_command (const char *arg, int from_tty,
|
|||||||
cmd_list_element *element)
|
cmd_list_element *element)
|
||||||
{
|
{
|
||||||
/* Make sure the index cache directory is absolute and tilde-expanded. */
|
/* Make sure the index cache directory is absolute and tilde-expanded. */
|
||||||
gdb::unique_xmalloc_ptr<char> abs
|
index_cache_directory = gdb_abspath (index_cache_directory.c_str ());
|
||||||
= gdb_abspath (index_cache_directory.c_str ());
|
|
||||||
index_cache_directory = abs.get ();
|
|
||||||
global_index_cache.set_directory (index_cache_directory);
|
global_index_cache.set_directory (index_cache_directory);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -133,12 +133,7 @@ set_gdb_data_directory (const char *new_datadir)
|
|||||||
"../foo" and "../foo" doesn't exist then we'll record $(pwd)/../foo which
|
"../foo" and "../foo" doesn't exist then we'll record $(pwd)/../foo which
|
||||||
isn't canonical, but that's ok. */
|
isn't canonical, but that's ok. */
|
||||||
if (!IS_ABSOLUTE_PATH (gdb_datadir.c_str ()))
|
if (!IS_ABSOLUTE_PATH (gdb_datadir.c_str ()))
|
||||||
{
|
gdb_datadir = gdb_abspath (gdb_datadir.c_str ());
|
||||||
gdb::unique_xmalloc_ptr<char> abs_datadir
|
|
||||||
= gdb_abspath (gdb_datadir.c_str ());
|
|
||||||
|
|
||||||
gdb_datadir = abs_datadir.get ();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Relocate a file or directory. PROGNAME is the name by which gdb
|
/* Relocate a file or directory. PROGNAME is the name by which gdb
|
||||||
|
@ -331,7 +331,7 @@ objfile::objfile (bfd *abfd, const char *name, objfile_flags flags_)
|
|||||||
|
|
||||||
objfile_alloc_data (this);
|
objfile_alloc_data (this);
|
||||||
|
|
||||||
gdb::unique_xmalloc_ptr<char> name_holder;
|
std::string name_holder;
|
||||||
if (name == NULL)
|
if (name == NULL)
|
||||||
{
|
{
|
||||||
gdb_assert (abfd == NULL);
|
gdb_assert (abfd == NULL);
|
||||||
@ -344,7 +344,7 @@ objfile::objfile (bfd *abfd, const char *name, objfile_flags flags_)
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
name_holder = gdb_abspath (name);
|
name_holder = gdb_abspath (name);
|
||||||
expanded_name = name_holder.get ();
|
expanded_name = name_holder.c_str ();
|
||||||
}
|
}
|
||||||
original_name = obstack_strdup (&objfile_obstack, expanded_name);
|
original_name = obstack_strdup (&objfile_obstack, expanded_name);
|
||||||
|
|
||||||
|
19
gdb/source.c
19
gdb/source.c
@ -537,15 +537,15 @@ add_path (const char *dirname, char **which_path, int parse_separators)
|
|||||||
|
|
||||||
for (const gdb::unique_xmalloc_ptr<char> &name_up : dir_vec)
|
for (const gdb::unique_xmalloc_ptr<char> &name_up : dir_vec)
|
||||||
{
|
{
|
||||||
char *name = name_up.get ();
|
const char *name = name_up.get ();
|
||||||
char *p;
|
char *p;
|
||||||
struct stat st;
|
struct stat st;
|
||||||
gdb::unique_xmalloc_ptr<char> new_name_holder;
|
std::string new_name_holder;
|
||||||
|
|
||||||
/* Spaces and tabs will have been removed by buildargv().
|
/* Spaces and tabs will have been removed by buildargv().
|
||||||
NAME is the start of the directory.
|
NAME is the start of the directory.
|
||||||
P is the '\0' following the end. */
|
P is the '\0' following the end. */
|
||||||
p = name + strlen (name);
|
p = name_up.get () + strlen (name);
|
||||||
|
|
||||||
while (!(IS_DIR_SEPARATOR (*name) && p <= name + 1) /* "/" */
|
while (!(IS_DIR_SEPARATOR (*name) && p <= name + 1) /* "/" */
|
||||||
#ifdef HAVE_DOS_BASED_FILE_SYSTEM
|
#ifdef HAVE_DOS_BASED_FILE_SYSTEM
|
||||||
@ -589,16 +589,18 @@ add_path (const char *dirname, char **which_path, int parse_separators)
|
|||||||
if (name[0] == '\0')
|
if (name[0] == '\0')
|
||||||
goto skip_dup;
|
goto skip_dup;
|
||||||
if (name[0] == '~')
|
if (name[0] == '~')
|
||||||
new_name_holder.reset (tilde_expand (name));
|
new_name_holder
|
||||||
|
= gdb::unique_xmalloc_ptr<char[]> (tilde_expand (name)).get ();
|
||||||
#ifdef HAVE_DOS_BASED_FILE_SYSTEM
|
#ifdef HAVE_DOS_BASED_FILE_SYSTEM
|
||||||
else if (IS_ABSOLUTE_PATH (name) && p == name + 2) /* "d:" => "d:." */
|
else if (IS_ABSOLUTE_PATH (name) && p == name + 2) /* "d:" => "d:." */
|
||||||
new_name_holder.reset (concat (name, ".", (char *) NULL));
|
new_name_holder = std::string (name) + ".";
|
||||||
#endif
|
#endif
|
||||||
else if (!IS_ABSOLUTE_PATH (name) && name[0] != '$')
|
else if (!IS_ABSOLUTE_PATH (name) && name[0] != '$')
|
||||||
new_name_holder = gdb_abspath (name);
|
new_name_holder = gdb_abspath (name);
|
||||||
else
|
else
|
||||||
new_name_holder.reset (savestring (name, p - name));
|
new_name_holder = std::string (name, p - name);
|
||||||
name = new_name_holder.get ();
|
|
||||||
|
name = new_name_holder.c_str ();
|
||||||
|
|
||||||
/* Unless it's a variable, check existence. */
|
/* Unless it's a variable, check existence. */
|
||||||
if (name[0] != '$')
|
if (name[0] != '$')
|
||||||
@ -950,7 +952,8 @@ done:
|
|||||||
else if ((opts & OPF_RETURN_REALPATH) != 0)
|
else if ((opts & OPF_RETURN_REALPATH) != 0)
|
||||||
*filename_opened = gdb_realpath (filename);
|
*filename_opened = gdb_realpath (filename);
|
||||||
else
|
else
|
||||||
*filename_opened = gdb_abspath (filename);
|
*filename_opened
|
||||||
|
= make_unique_xstrdup (gdb_abspath (filename).c_str ());
|
||||||
}
|
}
|
||||||
|
|
||||||
errno = last_errno;
|
errno = last_errno;
|
||||||
|
10
gdb/top.c
10
gdb/top.c
@ -2167,12 +2167,7 @@ set_history_filename (const char *args,
|
|||||||
that was read. */
|
that was read. */
|
||||||
if (!history_filename.empty ()
|
if (!history_filename.empty ()
|
||||||
&& !IS_ABSOLUTE_PATH (history_filename.c_str ()))
|
&& !IS_ABSOLUTE_PATH (history_filename.c_str ()))
|
||||||
{
|
history_filename = gdb_abspath (history_filename.c_str ());
|
||||||
gdb::unique_xmalloc_ptr<char> temp
|
|
||||||
(gdb_abspath (history_filename.c_str ()));
|
|
||||||
|
|
||||||
history_filename = temp.get ();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Whether we're in quiet startup mode. */
|
/* Whether we're in quiet startup mode. */
|
||||||
@ -2444,7 +2439,6 @@ _initialize_top ()
|
|||||||
const char *fname = ".gdb_history";
|
const char *fname = ".gdb_history";
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
gdb::unique_xmalloc_ptr<char> temp (gdb_abspath (fname));
|
history_filename = gdb_abspath (fname);
|
||||||
history_filename = temp.get ();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -471,7 +471,7 @@ tfile_target_open (const char *arg, int from_tty)
|
|||||||
|
|
||||||
gdb::unique_xmalloc_ptr<char> filename (tilde_expand (arg));
|
gdb::unique_xmalloc_ptr<char> filename (tilde_expand (arg));
|
||||||
if (!IS_ABSOLUTE_PATH (filename.get ()))
|
if (!IS_ABSOLUTE_PATH (filename.get ()))
|
||||||
filename = gdb_abspath (filename.get ());
|
filename = make_unique_xstrdup (gdb_abspath (filename.get ()).c_str ());
|
||||||
|
|
||||||
flags = O_BINARY | O_LARGEFILE;
|
flags = O_BINARY | O_LARGEFILE;
|
||||||
flags |= O_RDONLY;
|
flags |= O_RDONLY;
|
||||||
|
@ -90,31 +90,31 @@ bool non_stop;
|
|||||||
static struct {
|
static struct {
|
||||||
/* Set the PROGRAM_PATH. Here we adjust the path of the provided
|
/* Set the PROGRAM_PATH. Here we adjust the path of the provided
|
||||||
binary if needed. */
|
binary if needed. */
|
||||||
void set (gdb::unique_xmalloc_ptr<char> &&path)
|
void set (const char *path)
|
||||||
{
|
{
|
||||||
m_path = std::move (path);
|
m_path = path;
|
||||||
|
|
||||||
/* Make sure we're using the absolute path of the inferior when
|
/* Make sure we're using the absolute path of the inferior when
|
||||||
creating it. */
|
creating it. */
|
||||||
if (!contains_dir_separator (m_path.get ()))
|
if (!contains_dir_separator (m_path.c_str ()))
|
||||||
{
|
{
|
||||||
int reg_file_errno;
|
int reg_file_errno;
|
||||||
|
|
||||||
/* Check if the file is in our CWD. If it is, then we prefix
|
/* Check if the file is in our CWD. If it is, then we prefix
|
||||||
its name with CURRENT_DIRECTORY. Otherwise, we leave the
|
its name with CURRENT_DIRECTORY. Otherwise, we leave the
|
||||||
name as-is because we'll try searching for it in $PATH. */
|
name as-is because we'll try searching for it in $PATH. */
|
||||||
if (is_regular_file (m_path.get (), ®_file_errno))
|
if (is_regular_file (m_path.c_str (), ®_file_errno))
|
||||||
m_path = gdb_abspath (m_path.get ());
|
m_path = gdb_abspath (m_path.c_str ());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Return the PROGRAM_PATH. */
|
/* Return the PROGRAM_PATH. */
|
||||||
char *get ()
|
const char *get ()
|
||||||
{ return m_path.get (); }
|
{ return m_path.empty () ? nullptr : m_path.c_str (); }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
/* The program name, adjusted if needed. */
|
/* The program name, adjusted if needed. */
|
||||||
gdb::unique_xmalloc_ptr<char> m_path;
|
std::string m_path;
|
||||||
} program_path;
|
} program_path;
|
||||||
static std::vector<char *> program_args;
|
static std::vector<char *> program_args;
|
||||||
static std::string wrapper_argv;
|
static std::string wrapper_argv;
|
||||||
@ -3076,7 +3076,7 @@ handle_v_run (char *own_buf)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
program_path.set (gdb::unique_xmalloc_ptr<char> (new_program_name));
|
program_path.set (new_program_name);
|
||||||
|
|
||||||
/* Free the old argv and install the new one. */
|
/* Free the old argv and install the new one. */
|
||||||
free_vector_argv (program_args);
|
free_vector_argv (program_args);
|
||||||
@ -3933,7 +3933,7 @@ captured_main (int argc, char *argv[])
|
|||||||
int i, n;
|
int i, n;
|
||||||
|
|
||||||
n = argc - (next_arg - argv);
|
n = argc - (next_arg - argv);
|
||||||
program_path.set (make_unique_xstrdup (next_arg[0]));
|
program_path.set (next_arg[0]);
|
||||||
for (i = 1; i < n; i++)
|
for (i = 1; i < n; i++)
|
||||||
program_args.push_back (xstrdup (next_arg[i]));
|
program_args.push_back (xstrdup (next_arg[i]));
|
||||||
|
|
||||||
|
@ -130,23 +130,23 @@ gdb_realpath_keepfile (const char *filename)
|
|||||||
|
|
||||||
/* See gdbsupport/pathstuff.h. */
|
/* See gdbsupport/pathstuff.h. */
|
||||||
|
|
||||||
gdb::unique_xmalloc_ptr<char>
|
std::string
|
||||||
gdb_abspath (const char *path)
|
gdb_abspath (const char *path)
|
||||||
{
|
{
|
||||||
gdb_assert (path != NULL && path[0] != '\0');
|
gdb_assert (path != NULL && path[0] != '\0');
|
||||||
|
|
||||||
if (path[0] == '~')
|
if (path[0] == '~')
|
||||||
return gdb_tilde_expand_up (path);
|
return gdb_tilde_expand (path);
|
||||||
|
|
||||||
if (IS_ABSOLUTE_PATH (path) || current_directory == NULL)
|
if (IS_ABSOLUTE_PATH (path) || current_directory == NULL)
|
||||||
return make_unique_xstrdup (path);
|
return path;
|
||||||
|
|
||||||
/* Beware the // my son, the Emacs barfs, the botch that catch... */
|
/* Beware the // my son, the Emacs barfs, the botch that catch... */
|
||||||
return gdb::unique_xmalloc_ptr<char>
|
return string_printf
|
||||||
(concat (current_directory,
|
("%s%s%s", current_directory,
|
||||||
IS_DIR_SEPARATOR (current_directory[strlen (current_directory) - 1])
|
(IS_DIR_SEPARATOR (current_directory[strlen (current_directory) - 1])
|
||||||
? "" : SLASH_STRING,
|
? "" : SLASH_STRING),
|
||||||
path, (char *) NULL));
|
path);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* See gdbsupport/pathstuff.h. */
|
/* See gdbsupport/pathstuff.h. */
|
||||||
@ -229,8 +229,8 @@ get_standard_cache_dir ()
|
|||||||
if (xdg_cache_home != NULL && xdg_cache_home[0] != '\0')
|
if (xdg_cache_home != NULL && xdg_cache_home[0] != '\0')
|
||||||
{
|
{
|
||||||
/* Make sure the path is absolute and tilde-expanded. */
|
/* Make sure the path is absolute and tilde-expanded. */
|
||||||
gdb::unique_xmalloc_ptr<char> abs (gdb_abspath (xdg_cache_home));
|
std::string abs = gdb_abspath (xdg_cache_home);
|
||||||
return string_printf ("%s/gdb", abs.get ());
|
return string_printf ("%s/gdb", abs.c_str ());
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@ -238,8 +238,8 @@ get_standard_cache_dir ()
|
|||||||
if (home != NULL && home[0] != '\0')
|
if (home != NULL && home[0] != '\0')
|
||||||
{
|
{
|
||||||
/* Make sure the path is absolute and tilde-expanded. */
|
/* Make sure the path is absolute and tilde-expanded. */
|
||||||
gdb::unique_xmalloc_ptr<char> abs (gdb_abspath (home));
|
std::string abs = gdb_abspath (home);
|
||||||
return string_printf ("%s/" HOME_CACHE_DIR "/gdb", abs.get ());
|
return string_printf ("%s/" HOME_CACHE_DIR "/gdb", abs.c_str ());
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef WIN32
|
#ifdef WIN32
|
||||||
@ -247,8 +247,8 @@ get_standard_cache_dir ()
|
|||||||
if (win_home != NULL && win_home[0] != '\0')
|
if (win_home != NULL && win_home[0] != '\0')
|
||||||
{
|
{
|
||||||
/* Make sure the path is absolute and tilde-expanded. */
|
/* Make sure the path is absolute and tilde-expanded. */
|
||||||
gdb::unique_xmalloc_ptr<char> abs (gdb_abspath (win_home));
|
std::string abs = gdb_abspath (win_home);
|
||||||
return string_printf ("%s/gdb", abs.get ());
|
return string_printf ("%s/gdb", abs.c_str ());
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@ -296,8 +296,8 @@ get_standard_config_dir ()
|
|||||||
if (xdg_config_home != NULL && xdg_config_home[0] != '\0')
|
if (xdg_config_home != NULL && xdg_config_home[0] != '\0')
|
||||||
{
|
{
|
||||||
/* Make sure the path is absolute and tilde-expanded. */
|
/* Make sure the path is absolute and tilde-expanded. */
|
||||||
gdb::unique_xmalloc_ptr<char> abs (gdb_abspath (xdg_config_home));
|
std::string abs = gdb_abspath (xdg_config_home);
|
||||||
return string_printf ("%s/gdb", abs.get ());
|
return string_printf ("%s/gdb", abs.c_str ());
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@ -305,8 +305,8 @@ get_standard_config_dir ()
|
|||||||
if (home != NULL && home[0] != '\0')
|
if (home != NULL && home[0] != '\0')
|
||||||
{
|
{
|
||||||
/* Make sure the path is absolute and tilde-expanded. */
|
/* Make sure the path is absolute and tilde-expanded. */
|
||||||
gdb::unique_xmalloc_ptr<char> abs (gdb_abspath (home));
|
std::string abs = gdb_abspath (home);
|
||||||
return string_printf ("%s/" HOME_CONFIG_DIR "/gdb", abs.get ());
|
return string_printf ("%s/" HOME_CONFIG_DIR "/gdb", abs.c_str ());
|
||||||
}
|
}
|
||||||
|
|
||||||
return {};
|
return {};
|
||||||
@ -347,9 +347,8 @@ find_gdb_home_config_file (const char *name, struct stat *buf)
|
|||||||
if (homedir != nullptr && homedir[0] != '\0')
|
if (homedir != nullptr && homedir[0] != '\0')
|
||||||
{
|
{
|
||||||
/* Make sure the path is absolute and tilde-expanded. */
|
/* Make sure the path is absolute and tilde-expanded. */
|
||||||
gdb::unique_xmalloc_ptr<char> abs (gdb_abspath (homedir));
|
std::string abs = gdb_abspath (homedir);
|
||||||
std::string path = (std::string (abs.get ()) + SLASH_STRING
|
std::string path = string_printf ("%s/%s", abs.c_str (), name);
|
||||||
+ std::string (name));
|
|
||||||
if (stat (path.c_str (), buf) == 0)
|
if (stat (path.c_str (), buf) == 0)
|
||||||
return path;
|
return path;
|
||||||
}
|
}
|
||||||
|
@ -53,7 +53,7 @@ extern gdb::unique_xmalloc_ptr<char>
|
|||||||
If CURRENT_DIRECTORY is NULL, this function returns a copy of
|
If CURRENT_DIRECTORY is NULL, this function returns a copy of
|
||||||
PATH. */
|
PATH. */
|
||||||
|
|
||||||
extern gdb::unique_xmalloc_ptr<char> gdb_abspath (const char *path);
|
extern std::string gdb_abspath (const char *path);
|
||||||
|
|
||||||
/* If the path in CHILD is a child of the path in PARENT, return a
|
/* If the path in CHILD is a child of the path in PARENT, return a
|
||||||
pointer to the first component in the CHILD's pathname below the
|
pointer to the first component in the CHILD's pathname below the
|
||||||
|
Reference in New Issue
Block a user