From patchwork Tue Feb 13 23:39:04 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 25930 Received: (qmail 35515 invoked by alias); 13 Feb 2018 23:39:19 -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 35409 invoked by uid 89); 13 Feb 2018 23:39:18 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-25.7 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, SPF_HELO_PASS autolearn=ham version=3.3.2 spammy= X-HELO: gateway21.websitewelcome.com Received: from gateway21.websitewelcome.com (HELO gateway21.websitewelcome.com) (192.185.46.109) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Tue, 13 Feb 2018 23:39:16 +0000 Received: from cm12.websitewelcome.com (cm12.websitewelcome.com [100.42.49.8]) by gateway21.websitewelcome.com (Postfix) with ESMTP id 0602A400E909B for ; Tue, 13 Feb 2018 17:39:15 -0600 (CST) Received: from box5379.bluehost.com ([162.241.216.53]) by cmsmtp with SMTP id lkAMedQKyzzFjlkAMeFp0g; Tue, 13 Feb 2018 17:39:15 -0600 Received: from 174-29-60-18.hlrn.qwest.net ([174.29.60.18]:52440 helo=bapiya.Home) by box5379.bluehost.com with esmtpsa (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.89) (envelope-from ) id 1elkAM-004Amp-Nh; Tue, 13 Feb 2018 17:39:14 -0600 From: Tom Tromey To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [RFA 1/4] Return unique_xmalloc_ptr from some solib.c functions Date: Tue, 13 Feb 2018 16:39:04 -0700 Message-Id: <20180213233907.11259-2-tom@tromey.com> In-Reply-To: <20180213233907.11259-1-tom@tromey.com> References: <20180213233907.11259-1-tom@tromey.com> X-BWhitelist: no X-Source-L: No X-Exim-ID: 1elkAM-004Amp-Nh X-Source-Sender: 174-29-60-18.hlrn.qwest.net (bapiya.Home) [174.29.60.18]:52440 X-Source-Auth: tom+tromey.com X-Email-Count: 2 X-Source-Cap: ZWx5bnJvYmk7ZWx5bnJvYmk7Ym94NTM3OS5ibHVlaG9zdC5jb20= X-Local-Domain: yes This changes a couple of solib.c functions -- exec_file_find and solib_find -- to return a unique_xmalloc_ptr, and then fixes up the users. This allows the removal of some cleanups. This also changes solib_bfd_open to not take ownership of its argument. I think this change is somewhat cleaner. gdb/ChangeLog 2018-02-13 Tom Tromey * solist.h (exec_file_find, solib_find): Return unique_xmalloc_ptr. (solib_bfd_fopen): Take a const char *. * solib.c (solib_find_1): Return unique_xmalloc_ptr. (exec_file_find, solib_find): Likewise. (solib_bfd_fopen): Do not take ownership of "pathname". (solib_bfd_open): Use unique_xmalloc_ptr. * solib-darwin.c (darwin_bfd_open): Use unique_xmalloc_ptr. * solib-aix.c (solib_aix_bfd_open): Use unique_xmalloc_ptr. * infrun.c (follow_exec): Use unique_xmalloc_ptr. * exec.c (exec_file_locate_attach): Use unique_xmalloc_ptr. --- gdb/ChangeLog | 14 ++++++++++++++ gdb/exec.c | 10 ++++------ gdb/infrun.c | 10 +++------- gdb/solib-aix.c | 7 ++++--- gdb/solib-darwin.c | 6 +++--- gdb/solib.c | 34 ++++++++++++++++------------------ gdb/solist.h | 8 +++++--- 7 files changed, 49 insertions(+), 40 deletions(-) diff --git a/gdb/exec.c b/gdb/exec.c index c8c32ecc27..15f85a278f 100644 --- a/gdb/exec.c +++ b/gdb/exec.c @@ -190,8 +190,7 @@ try_open_exec_file (const char *exec_file_host, struct inferior *inf, void exec_file_locate_attach (int pid, int defer_bp_reset, int from_tty) { - char *exec_file_target, *exec_file_host; - struct cleanup *old_chain; + char *exec_file_target; symfile_add_flags add_flags = 0; /* Do nothing if we already have an executable filename. */ @@ -209,8 +208,8 @@ exec_file_locate_attach (int pid, int defer_bp_reset, int from_tty) return; } - exec_file_host = exec_file_find (exec_file_target, NULL); - old_chain = make_cleanup (xfree, exec_file_host); + gdb::unique_xmalloc_ptr exec_file_host + = exec_file_find (exec_file_target, NULL); if (defer_bp_reset) add_flags |= SYMFILE_DEFER_BP_RESET; @@ -219,8 +218,7 @@ exec_file_locate_attach (int pid, int defer_bp_reset, int from_tty) add_flags |= SYMFILE_VERBOSE; /* Attempt to open the exec file. */ - try_open_exec_file (exec_file_host, current_inferior (), add_flags); - do_cleanups (old_chain); + try_open_exec_file (exec_file_host.get (), current_inferior (), add_flags); } /* Set FILENAME as the new exec file. diff --git a/gdb/infrun.c b/gdb/infrun.c index 45fe36a717..1bc860b6f3 100644 --- a/gdb/infrun.c +++ b/gdb/infrun.c @@ -1081,8 +1081,6 @@ follow_exec (ptid_t ptid, char *exec_file_target) struct inferior *inf = current_inferior (); int pid = ptid_get_pid (ptid); ptid_t process_ptid; - char *exec_file_host; - struct cleanup *old_chain; /* This is an exec event that we actually wish to pay attention to. Refresh our symbol table to the newly exec'd program, remove any @@ -1161,8 +1159,8 @@ follow_exec (ptid_t ptid, char *exec_file_target) breakpoint_init_inferior (inf_execd); - exec_file_host = exec_file_find (exec_file_target, NULL); - old_chain = make_cleanup (xfree, exec_file_host); + gdb::unique_xmalloc_ptr exec_file_host + = exec_file_find (exec_file_target, NULL); /* If we were unable to map the executable target pathname onto a host pathname, tell the user that. Otherwise GDB's subsequent behavior @@ -1216,9 +1214,7 @@ follow_exec (ptid_t ptid, char *exec_file_target) Executable) main symbol file will only be computed by solib_create_inferior_hook below. breakpoint_re_set would fail to insert the breakpoints with the zero displacement. */ - try_open_exec_file (exec_file_host, inf, SYMFILE_DEFER_BP_RESET); - - do_cleanups (old_chain); + try_open_exec_file (exec_file_host.get (), inf, SYMFILE_DEFER_BP_RESET); /* If the target can specify a description, read it. Must do this after flipping to the new executable (because the target supplied diff --git a/gdb/solib-aix.c b/gdb/solib-aix.c index 463f060845..aa0e0d2dfb 100644 --- a/gdb/solib-aix.c +++ b/gdb/solib-aix.c @@ -614,7 +614,6 @@ solib_aix_bfd_open (char *pathname) char *sep; int filename_len; int found_file; - char *found_pathname; if (pathname[path_len - 1] != ')') return solib_bfd_open (pathname); @@ -638,10 +637,12 @@ solib_aix_bfd_open (char *pathname) /* Calling solib_find makes certain that sysroot path is set properly if program has a dependency on .a archive and sysroot is set via set sysroot command. */ - found_pathname = solib_find (filename.c_str (), &found_file); + gdb::unique_xmalloc_ptr found_pathname + = solib_find (filename.c_str (), &found_file); if (found_pathname == NULL) perror_with_name (pathname); - gdb_bfd_ref_ptr archive_bfd (solib_bfd_fopen (found_pathname, found_file)); + gdb_bfd_ref_ptr archive_bfd (solib_bfd_fopen (found_pathname.get (), + found_file)); if (archive_bfd == NULL) { warning (_("Could not open `%s' as an executable file: %s"), diff --git a/gdb/solib-darwin.c b/gdb/solib-darwin.c index 121a7135c0..ed8f9da257 100644 --- a/gdb/solib-darwin.c +++ b/gdb/solib-darwin.c @@ -619,16 +619,16 @@ darwin_lookup_lib_symbol (struct objfile *objfile, static gdb_bfd_ref_ptr darwin_bfd_open (char *pathname) { - char *found_pathname; int found_file; /* Search for shared library file. */ - found_pathname = solib_find (pathname, &found_file); + gdb::unique_xmalloc_ptr found_pathname + = solib_find (pathname, &found_file); if (found_pathname == NULL) perror_with_name (pathname); /* Open bfd for shared library. */ - gdb_bfd_ref_ptr abfd (solib_bfd_fopen (found_pathname, found_file)); + gdb_bfd_ref_ptr abfd (solib_bfd_fopen (found_pathname.get (), found_file)); gdb_bfd_ref_ptr res (gdb_bfd_mach_o_fat_extract (abfd.get (), bfd_object, diff --git a/gdb/solib.c b/gdb/solib.c index 40fdfc59e6..46f2324c48 100644 --- a/gdb/solib.c +++ b/gdb/solib.c @@ -148,7 +148,7 @@ show_solib_search_path (struct ui_file *file, int from_tty, * machines since a sysroot will almost always be set. */ -static char * +static gdb::unique_xmalloc_ptr solib_find_1 (const char *in_pathname, int *fd, int is_solib) { const struct target_so_ops *ops = solib_ops (target_gdbarch ()); @@ -251,7 +251,7 @@ solib_find_1 (const char *in_pathname, int *fd, int is_solib) { if (fd != NULL) *fd = -1; - return temp_pathname; + return gdb::unique_xmalloc_ptr (temp_pathname); } /* Now see if we can open it. */ @@ -367,7 +367,7 @@ solib_find_1 (const char *in_pathname, int *fd, int is_solib) else *fd = found_file; - return temp_pathname; + return gdb::unique_xmalloc_ptr (temp_pathname); } /* Return the full pathname of the main executable, or NULL if not @@ -375,10 +375,10 @@ solib_find_1 (const char *in_pathname, int *fd, int is_solib) the caller. If FD is non-NULL, *FD is set to either -1 or an open file handle for the main executable. */ -char * +gdb::unique_xmalloc_ptr exec_file_find (const char *in_pathname, int *fd) { - char *result; + gdb::unique_xmalloc_ptr result; const char *fskind = effective_target_file_system_kind (); if (in_pathname == NULL) @@ -409,8 +409,11 @@ exec_file_find (const char *in_pathname, int *fd) (If that fails, we'll just fall back on the original filename. Not much more we can do...) */ - if (!source_full_path_of (in_pathname, &result)) - result = xstrdup (in_pathname); + char *full_path = NULL; + if (source_full_path_of (in_pathname, &full_path)) + result.reset (full_path); + else + result.reset (xstrdup (in_pathname)); if (fd != NULL) *fd = -1; } @@ -426,7 +429,7 @@ exec_file_find (const char *in_pathname, int *fd) The search algorithm used is described in solib_find_1's comment above. */ -char * +gdb::unique_xmalloc_ptr solib_find (const char *in_pathname, int *fd) { const char *solib_symbols_extension @@ -463,12 +466,10 @@ solib_find (const char *in_pathname, int *fd) it is used as file handle to open the file. Throws an error if the file could not be opened. Handles both local and remote file access. - PATHNAME must be malloc'ed by the caller. It will be freed by this - function. If unsuccessful, the FD will be closed (unless FD was - -1). */ + If unsuccessful, the FD will be closed (unless FD was -1). */ gdb_bfd_ref_ptr -solib_bfd_fopen (char *pathname, int fd) +solib_bfd_fopen (const char *pathname, int fd) { gdb_bfd_ref_ptr abfd (gdb_bfd_open (pathname, gnutarget, fd)); @@ -478,13 +479,10 @@ solib_bfd_fopen (char *pathname, int fd) if (abfd == NULL) { /* Arrange to free PATHNAME when the error is thrown. */ - gdb::unique_xmalloc_ptr free_pathname (pathname); error (_("Could not open `%s' as an executable file: %s"), pathname, bfd_errmsg (bfd_get_error ())); } - xfree (pathname); - return abfd; } @@ -493,12 +491,12 @@ solib_bfd_fopen (char *pathname, int fd) gdb_bfd_ref_ptr solib_bfd_open (char *pathname) { - char *found_pathname; int found_file; const struct bfd_arch_info *b; /* Search for shared library file. */ - found_pathname = solib_find (pathname, &found_file); + gdb::unique_xmalloc_ptr found_pathname + = solib_find (pathname, &found_file); if (found_pathname == NULL) { /* Return failure if the file could not be found, so that we can @@ -510,7 +508,7 @@ solib_bfd_open (char *pathname) } /* Open bfd for shared library. */ - gdb_bfd_ref_ptr abfd (solib_bfd_fopen (found_pathname, found_file)); + gdb_bfd_ref_ptr abfd (solib_bfd_fopen (found_pathname.get (), found_file)); /* Check bfd format. */ if (!bfd_check_format (abfd.get (), bfd_object)) diff --git a/gdb/solist.h b/gdb/solist.h index 14459139e5..aba00ebb21 100644 --- a/gdb/solist.h +++ b/gdb/solist.h @@ -192,13 +192,15 @@ typedef std::unique_ptr so_list_up; struct so_list *master_so_list (void); /* Find main executable binary file. */ -extern char *exec_file_find (const char *in_pathname, int *fd); +extern gdb::unique_xmalloc_ptr exec_file_find (const char *in_pathname, + int *fd); /* Find shared library binary file. */ -extern char *solib_find (const char *in_pathname, int *fd); +extern gdb::unique_xmalloc_ptr solib_find (const char *in_pathname, + int *fd); /* Open BFD for shared library file. */ -extern gdb_bfd_ref_ptr solib_bfd_fopen (char *pathname, int fd); +extern gdb_bfd_ref_ptr solib_bfd_fopen (const char *pathname, int fd); /* Find solib binary file and open it. */ extern gdb_bfd_ref_ptr solib_bfd_open (char *in_pathname);