From patchwork Fri Aug 21 21:22:44 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jan Kratochvil X-Patchwork-Id: 8367 Received: (qmail 114261 invoked by alias); 21 Aug 2015 21:22:54 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 114251 invoked by uid 89); 21 Aug 2015 21:22:54 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-3.6 required=5.0 tests=AWL, BAYES_00, KAM_LAZY_DOMAIN_SECURITY, RP_MATCHES_RCVD, SPF_HELO_PASS autolearn=no version=3.3.2 X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES256-GCM-SHA384 encrypted) ESMTPS; Fri, 21 Aug 2015 21:22:48 +0000 Received: from int-mx14.intmail.prod.int.phx2.redhat.com (int-mx14.intmail.prod.int.phx2.redhat.com [10.5.11.27]) by mx1.redhat.com (Postfix) with ESMTPS id 056F98CF7E for ; Fri, 21 Aug 2015 21:22:47 +0000 (UTC) Received: from host1.jankratochvil.net (ovpn-116-22.ams2.redhat.com [10.36.116.22]) by int-mx14.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id t7LLMjWD019428 for ; Fri, 21 Aug 2015 17:22:45 -0400 Subject: [PATCH v12 19/32] solib_find: Make it use file_location From: Jan Kratochvil To: gdb-patches@sourceware.org Date: Fri, 21 Aug 2015 23:22:44 +0200 Message-ID: <20150821212244.6673.6004.stgit@host1.jankratochvil.net> In-Reply-To: <20150821212006.6673.35100.stgit@host1.jankratochvil.net> References: <20150821212006.6673.35100.stgit@host1.jankratochvil.net> User-Agent: StGit/0.17.1-dirty MIME-Version: 1.0 X-IsSubscribed: yes Hi, refactor the solib*() code to use file_location functions. Jan gdb/ChangeLog 2015-08-20 Jan Kratochvil * nto-tdep.c: Include source.h. (nto_find_and_open_solib): Return file_location, remove parameter temp_pathname. Change openp to openp_file. * nto-tdep.h (nto_find_and_open_solib): Update prototype. * solib.c (solib_find_2): Rename to ... (solib_find_3): ... here, return file_location, change parameter fd to opts. Change gdb_open_cloexec to file_location_from_filename and openp to openp_file calls. (solib_find_1): Rename to ... (solib_find_2): ... here, return file_location, change parameter fd to opts. (solib_find_1): New function. (solib_find): Rename to ... (solib_find_file) ... here, return file_location, change parameter fd to opts. (solib_find): New function. * solist.h (find_and_open_solib): Return file_location, remove parameter temp_pathname. --- 0 files changed diff --git a/gdb/nto-tdep.c b/gdb/nto-tdep.c index 7c8b4ce..9f9e3bc 100644 --- a/gdb/nto-tdep.c +++ b/gdb/nto-tdep.c @@ -31,6 +31,7 @@ #include "solib-svr4.h" #include "gdbcore.h" #include "objfiles.h" +#include "source.h" #ifdef __CYGWIN__ #include @@ -81,14 +82,15 @@ nto_map_arch_to_cputype (const char *arch) return CPUTYPE_UNKNOWN; } -int -nto_find_and_open_solib (char *solib, char **temp_pathname) +struct file_location +nto_find_and_open_solib (char *solib) { char *buf, *arch_path, *nto_root; const char *endian; const char *base; const char *arch; - int arch_len, len, ret; + int arch_len, len; + struct file_location file; #define PATH_FMT \ "%s/lib:%s/usr/lib:%s/usr/photon/lib:%s/usr/photon/dll:%s/lib/dll" @@ -127,22 +129,13 @@ nto_find_and_open_solib (char *solib, char **temp_pathname) arch_path); base = lbasename (solib); - ret = openp (buf, OPF_TRY_CWD_FIRST, base, temp_pathname); - if (ret < 0 && base != solib) - { - xsnprintf (arch_path, arch_len, "/%s", solib); - ret = open (arch_path, O_RDONLY | O_BINARY); - if (temp_pathname) - { - if (ret >= 0) - *temp_pathname = xstrdup (arch_path); - else - *temp_pathname = NULL; - } - } - if (ret >= 0) - *temp_pathname = gdb_realpath_and_xfree (*temp_pathname); - return ret; + file = openp_file (buf, OPF_TRY_CWD_FIRST | OPF_IS_BFD, base); + if (file_location_is_valid (&file) || base == solib) + return file; + file_location_free (&file); + + xsnprintf (arch_path, arch_len, "/%s", solib); + return file_location_from_filename (arch_path, OPF_IS_BFD); } void diff --git a/gdb/nto-tdep.h b/gdb/nto-tdep.h index 1f8045a..5828765 100644 --- a/gdb/nto-tdep.h +++ b/gdb/nto-tdep.h @@ -154,7 +154,7 @@ void nto_relocate_section_addresses (struct so_list *, int nto_map_arch_to_cputype (const char *); -int nto_find_and_open_solib (char *, char **); +struct file_location nto_find_and_open_solib (char *); enum gdb_osabi nto_elf_osabi_sniffer (bfd *abfd); diff --git a/gdb/solib.c b/gdb/solib.c index b16f47b..4fe4cb3 100644 --- a/gdb/solib.c +++ b/gdb/solib.c @@ -113,11 +113,7 @@ show_solib_search_path (struct ui_file *file, int from_tty, # define DOS_BASED_FILE_SYSTEM 0 #endif -/* Return the full pathname of a binary file (the main executable - or a shared library file), or NULL if not found. The returned - pathname is malloc'ed and must be freed by the caller. If FD - is non-NULL, *FD is set to either -1 or an open file handle for - the binary file. +/* Find a binary file (the main executable or a shared library file). Parameter SYSROOT is used as a prefix directory to search for binary files if they have an absolute path. @@ -150,15 +146,16 @@ show_solib_search_path (struct ui_file *file, int from_tty, * machines since a sysroot will almost always be set. */ -static char * -solib_find_2 (char *in_pathname, int *fd, int is_solib, const char *sysroot) +static struct file_location +solib_find_3 (char *in_pathname, enum openp_flags opts, int is_solib, + const char *sysroot) { const struct target_so_ops *ops = solib_ops (target_gdbarch ()); - int found_file = -1; - char *temp_pathname = NULL; + char *temp_pathname; const char *fskind = effective_target_file_system_kind (); struct cleanup *old_chain = make_cleanup (null_cleanup, NULL); - int prefix_len, orig_prefix_len, temp_pathname_is_realpath; + int prefix_len, orig_prefix_len; + struct file_location file; /* If the absolute prefix starts with "target:" but the filesystem accessed by the target_fileio_* methods is the local filesystem @@ -249,27 +246,23 @@ solib_find_2 (char *in_pathname, int *fd, int is_solib, const char *sysroot) in_pathname, (char *) NULL); } - /* Handle files to be accessed via the target. */ - if (is_target_filename (temp_pathname)) + /* Now see if we can open it. */ + file = file_location_from_filename (temp_pathname, opts); + if (file_location_is_valid (&file)) { - if (fd != NULL) - *fd = -1; do_cleanups (old_chain); - return temp_pathname; + return file; } + file_location_free (&file); - /* 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); + xfree (temp_pathname); /* 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, and retrying in the sysroot: c:/foo/bar.dll ==> /sysroot/c/foo/bar.dll. */ - if (found_file < 0 - && sysroot != NULL + if (sysroot != NULL && HAS_TARGET_DRIVE_SPEC (fskind, in_pathname)) { int need_dir_separator = !IS_DIR_SEPARATOR (in_pathname[2]); @@ -282,47 +275,46 @@ solib_find_2 (char *in_pathname, int *fd, int is_solib, const char *sysroot) in_pathname + 2, (char *) NULL); xfree (drive); - found_file = gdb_open_cloexec (temp_pathname, O_RDONLY | O_BINARY, 0); - if (found_file < 0) + file = file_location_from_filename (temp_pathname, opts); + if (file_location_is_valid (&file)) { - xfree (temp_pathname); + do_cleanups (old_chain); + return file; + } + file_location_free (&file); + + 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. + /* 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. */ + c:/foo/bar.dll ==> /sysroot/foo/bar.dll. */ - temp_pathname = concat (sysroot, - need_dir_separator ? SLASH_STRING : "", - in_pathname + 2, (char *) NULL); + temp_pathname = 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); + file = file_location_from_filename (temp_pathname, opts); + if (file_location_is_valid (&file)) + { + do_cleanups (old_chain); + return file; } + file_location_free (&file); + + xfree (temp_pathname); } do_cleanups (old_chain); - /* We try to find the library in various ways. After each attempt, - either found_file >= 0 and temp_pathname is a malloc'd string, or - found_file < 0 and temp_pathname does not point to storage that - needs to be freed. */ - - if (found_file < 0) - { - temp_pathname = NULL; - temp_pathname_is_realpath = 1; - } - else - temp_pathname_is_realpath = 0; + /* We try to find the library in various ways. */ /* 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 file according to its absolute path otherwise, which is not what we want.) Affects subsequent searches for this solib. */ - if (found_file < 0 && IS_TARGET_ABSOLUTE_PATH (fskind, in_pathname)) + if (IS_TARGET_ABSOLUTE_PATH (fskind, in_pathname)) { /* First, get rid of any drive letters etc. */ while (!IS_TARGET_DIR_SEPARATOR (fskind, *in_pathname)) @@ -335,59 +327,87 @@ solib_find_2 (char *in_pathname, int *fd, int is_solib, const char *sysroot) /* If not found, and we're looking for a solib, search the solib_search_path (if any). */ - if (is_solib && found_file < 0 && solib_search_path != NULL) - found_file = openp (solib_search_path, OPF_TRY_CWD_FIRST, - in_pathname, &temp_pathname); + if (is_solib && solib_search_path != NULL) + { + file = openp_file (solib_search_path, OPF_TRY_CWD_FIRST | opts, + in_pathname); + if (file_location_is_valid (&file)) + { + file.filename = gdb_realpath_and_xfree (file.filename); + return file; + } + file_location_free (&file); + } /* If not found, and we're looking for a solib, next search the solib_search_path (if any) for the basename only (ignoring the path). This is to allow reading solibs from a path that differs from the opened path. */ - if (is_solib && found_file < 0 && solib_search_path != NULL) - found_file = openp (solib_search_path, OPF_TRY_CWD_FIRST, - target_lbasename (fskind, in_pathname), &temp_pathname); + if (is_solib && solib_search_path != NULL) + { + file = openp_file (solib_search_path, OPF_TRY_CWD_FIRST | opts, + target_lbasename (fskind, in_pathname)); + if (file_location_is_valid (&file)) + { + file.filename = gdb_realpath_and_xfree (file.filename); + return file; + } + file_location_free (&file); + } /* If not found, and we're looking for a solib, try to use target supplied solib search method. */ - if (is_solib && found_file < 0 && ops->find_and_open_solib) - found_file = ops->find_and_open_solib (in_pathname, &temp_pathname); + if (is_solib && ops->find_and_open_solib) + { + file = ops->find_and_open_solib (in_pathname); + if (file_location_is_valid (&file)) + return file; + file_location_free (&file); + } /* If not found, next search the inferior's $PATH environment variable. */ - if (found_file < 0 && sysroot == NULL) - found_file = openp (get_in_environ (current_inferior ()->environment, - "PATH"), - OPF_TRY_CWD_FIRST, in_pathname, &temp_pathname); + if (sysroot == NULL) + { + file = openp_file (get_in_environ (current_inferior ()->environment, + "PATH"), + OPF_TRY_CWD_FIRST | opts, in_pathname); + if (file_location_is_valid (&file)) + { + file.filename = gdb_realpath_and_xfree (file.filename); + return file; + } + file_location_free (&file); + } /* If not found, and we're looking for a solib, next search the inferior's $LD_LIBRARY_PATH environment variable. */ - if (is_solib && found_file < 0 && sysroot == NULL) - found_file = openp (get_in_environ (current_inferior ()->environment, - "LD_LIBRARY_PATH"), - OPF_TRY_CWD_FIRST, in_pathname, &temp_pathname); - - if (found_file >= 0 && temp_pathname_is_realpath) - temp_pathname = gdb_realpath_and_xfree (temp_pathname); - - if (fd == NULL) + if (is_solib && sysroot == NULL) { - if (found_file >= 0) - close (found_file); + file = openp_file (get_in_environ (current_inferior ()->environment, + "LD_LIBRARY_PATH"), + OPF_TRY_CWD_FIRST | opts, in_pathname); + if (file_location_is_valid (&file)) + { + file.filename = gdb_realpath_and_xfree (file.filename); + return file; + } + file_location_free (&file); } - else - *fd = found_file; - return temp_pathname; + file_location_enoent (&file); + return file; } -/* It is an solib_find_2 wrapper handling multiple directory components +/* It is an solib_find_3 wrapper handling multiple directory components of GDB_SYSROOT. */ -static char * -solib_find_1 (char *in_pathname, int *fd, int is_solib) +static struct file_location +solib_find_2 (char *in_pathname, enum openp_flags opts, int is_solib) { VEC (char_ptr) *sysroot_vec; struct cleanup *back_to; - char *retval, *sysroot; + struct file_location file; + char *sysroot; int ix; sysroot_vec = dirnames_to_char_ptr_vec_target_exc (gdb_sysroot); @@ -395,12 +415,45 @@ solib_find_1 (char *in_pathname, int *fd, int is_solib) for (ix = 0; VEC_iterate (char_ptr, sysroot_vec, ix, sysroot); ++ix) { - retval = solib_find_2 (in_pathname, fd, is_solib, sysroot); - if (retval != NULL) - break; + file = solib_find_3 (in_pathname, opts, is_solib, sysroot); + if (file_location_is_valid (&file)) + { + do_cleanups (back_to); + return file; + } + file_location_free (&file); } do_cleanups (back_to); + return file; +} + +/* It is an solib_find_2 wrapper returning filename and optionally FD + for functions not yet converted to the file_location style. */ + +static char * +solib_find_1 (char *in_pathname, int *fd, int is_solib) +{ + struct file_location file = solib_find_2 (in_pathname, OPF_NONE, is_solib); + char *retval; + + if (!file_location_is_valid (&file)) + { + retval = NULL; + if (fd != NULL) + *fd = -1; + } + else + { + retval = xstrdup (file.filename); + if (fd != NULL) + { + gdb_assert (file.fd != -1); + *fd = file.fd; + file.fd = -1; + } + } + file_location_free (&file); return retval; } @@ -444,8 +497,8 @@ exec_file_find (char *in_pathname, int *fd) The search algorithm used is described in solib_find_1's comment above. */ -char * -solib_find (char *in_pathname, int *fd) +static struct file_location +solib_find_file (char *in_pathname, enum openp_flags opts) { const char *solib_symbols_extension = gdbarch_solib_symbols_extension (target_gdbarch ()); @@ -473,7 +526,36 @@ solib_find (char *in_pathname, int *fd) } } - return solib_find_1 (in_pathname, fd, 1); + return solib_find_2 (in_pathname, opts, 1 /* is_solib */); +} + +/* It is an solib_find_file wrapper returning filename and optionally FD + for functions not yet converted to the file_location style. */ + +char * +solib_find (char *in_pathname, int *fd) +{ + struct file_location file = solib_find_file (in_pathname, OPF_NONE); + char *retval; + + if (!file_location_is_valid (&file)) + { + retval = NULL; + if (fd != NULL) + *fd = -1; + } + else + { + retval = xstrdup (file.filename); + if (fd != NULL) + { + gdb_assert (file.fd != -1); + *fd = file.fd; + file.fd = -1; + } + } + file_location_free (&file); + return retval; } /* Open and return a BFD for the shared library PATHNAME. If FD is not -1, diff --git a/gdb/solist.h b/gdb/solist.h index 47e9d88..7d0a21e 100644 --- a/gdb/solist.h +++ b/gdb/solist.h @@ -142,11 +142,8 @@ struct target_so_ops /* Find and open shared library binary file. */ bfd *(*bfd_open) (char *pathname); - /* Optional extra hook for finding and opening a solib. - If TEMP_PATHNAME is non-NULL: If the file is successfully opened a - pointer to a malloc'd and realpath'd copy of SONAME is stored there, - otherwise NULL is stored there. */ - int (*find_and_open_solib) (char *soname, char **temp_pathname); + /* Optional extra hook for finding and opening a solib. */ + struct file_location (*find_and_open_solib) (char *soname); /* Hook for looking up global symbols in a library-specific way. */ struct block_symbol (*lookup_lib_global_symbol)