+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.
/* 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;
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);
}
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)
{
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. */
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;
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;
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);
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. */
{
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;
*/
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;
|| 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,
&& 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);
}
}
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
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
(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;
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)
>>>> 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;
{
/* 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;
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;
filename, O_RDONLY, full_pathname);
if (fd < 0)
{
- *full_pathname = NULL;
+ full_pathname->reset (NULL);
return 0;
}
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;
/* 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;
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.
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);
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 **);
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. */
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. */
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));
error (_("`%s': can't read symbols: %s."), name,
bfd_errmsg (bfd_get_error ()));
- do_cleanups (back_to);
-
return sym_bfd;
}