From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 111667 invoked by alias); 11 Apr 2017 15:01:42 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Received: (qmail 96155 invoked by uid 89); 11 Apr 2017 15:01:27 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-25.6 required=5.0 tests=AWL,BAYES_00,GIT_PATCH_0,GIT_PATCH_1,GIT_PATCH_2,GIT_PATCH_3,RCVD_IN_DNSWL_NONE,RCVD_IN_SORBS_SPAM,SPF_PASS autolearn=ham version=3.3.2 spammy= X-HELO: gproxy6-pub.mail.unifiedlayer.com Received: from gproxy6-pub.mail.unifiedlayer.com (HELO gproxy6-pub.mail.unifiedlayer.com) (67.222.39.168) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with SMTP; Tue, 11 Apr 2017 15:01:23 +0000 Received: (qmail 25650 invoked by uid 0); 11 Apr 2017 15:01:23 -0000 Received: from unknown (HELO cmgw2) (10.0.90.83) by gproxy6.mail.unifiedlayer.com with SMTP; 11 Apr 2017 15:01:23 -0000 Received: from box522.bluehost.com ([74.220.219.122]) by cmgw2 with id 731K1v01N2f2jeq0131NDe; Tue, 11 Apr 2017 09:01:22 -0600 X-Authority-Analysis: v=2.2 cv=LIwWeNe9 c=1 sm=1 tr=0 a=GsOEXm/OWkKvwdLVJsfwcA==:117 a=GsOEXm/OWkKvwdLVJsfwcA==:17 a=AzvcPWV-tVgA:10 a=zstS-IiYAAAA:8 a=Jov27RcLXCrAyuYfta0A:9 a=S_HVFEA2qnQA:10 a=4G6NA9xxw8l3yy4pmD5M:22 Received: from 75-166-65-226.hlrn.qwest.net ([75.166.65.226]:50042 helo=bapiya.Home) by box522.bluehost.com with esmtpsa (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.87) (envelope-from ) id 1cxxIF-0007Ml-Mq; Tue, 11 Apr 2017 09:01:19 -0600 From: Tom Tromey To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [RFA v2 04/17] Introduce gdb_dlhandle_up Date: Tue, 11 Apr 2017 15:01:00 -0000 Message-Id: <20170411150112.23207-5-tom@tromey.com> In-Reply-To: <20170411150112.23207-1-tom@tromey.com> References: <20170411150112.23207-1-tom@tromey.com> X-BWhitelist: no X-Exim-ID: 1cxxIF-0007Ml-Mq X-Source-Sender: 75-166-65-226.hlrn.qwest.net (bapiya.Home) [75.166.65.226]:50042 X-Source-Auth: tom+tromey.com X-Email-Count: 5 X-Source-Cap: ZWx5bnJvYmk7ZWx5bnJvYmk7Ym94NTIyLmJsdWVob3N0LmNvbQ== X-SW-Source: 2017-04/txt/msg00272.txt.bz2 This introduces gdb_dlhandle_up, a unique_ptr that can close a dlopen'd library. All the functions working with dlopen handles are updated to use this new type. I did not try to build this on Windows. gdb/ChangeLog 2017-04-11 Tom Tromey * jit.c (struct jit_reader): Declare separately. Add constructor and destructor. Change type of "handle". (loaded_jit_reader): Define separately. (jit_reader_load): Update. New "new". (jit_reader_unload_command): Use "delete". * gdb-dlfcn.h (struct dlclose_deleter): New. (gdb_dlhandle_up): New typedef. (gdb_dlopen, gdb_dlsym): Update types. (gdb_dlclose): Remove. * gdb-dlfcn.c (gdb_dlopen): Return a gdb_dlhandle_up. (gdb_dlsym): Change type of "handle". (make_cleanup_dlclose): Remove. (dlclose_deleter::operator()): Rename from gdb_dlclose. * compile/compile-c-support.c (load_libcc): Update. --- gdb/ChangeLog | 17 ++++++++++++++++ gdb/compile/compile-c-support.c | 6 ++++-- gdb/gdb-dlfcn.c | 45 ++++++++++++----------------------------- gdb/gdb-dlfcn.h | 24 ++++++++++++---------- gdb/jit.c | 42 +++++++++++++++++++++----------------- 5 files changed, 70 insertions(+), 64 deletions(-) diff --git a/gdb/ChangeLog b/gdb/ChangeLog index a71de4a..a888174 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,5 +1,22 @@ 2017-04-11 Tom Tromey + * jit.c (struct jit_reader): Declare separately. Add constructor + and destructor. Change type of "handle". + (loaded_jit_reader): Define separately. + (jit_reader_load): Update. New "new". + (jit_reader_unload_command): Use "delete". + * gdb-dlfcn.h (struct dlclose_deleter): New. + (gdb_dlhandle_up): New typedef. + (gdb_dlopen, gdb_dlsym): Update types. + (gdb_dlclose): Remove. + * gdb-dlfcn.c (gdb_dlopen): Return a gdb_dlhandle_up. + (gdb_dlsym): Change type of "handle". + (make_cleanup_dlclose): Remove. + (dlclose_deleter::operator()): Rename from gdb_dlclose. + * compile/compile-c-support.c (load_libcc): Update. + +2017-04-11 Tom Tromey + * symtab.h (find_pcs_for_symtab_line): Change return type. * symtab.c (find_pcs_for_symtab_line): Change return type. * python/py-linetable.c (build_line_table_tuple_from_pcs): Change diff --git a/gdb/compile/compile-c-support.c b/gdb/compile/compile-c-support.c index c969b42..2614570 100644 --- a/gdb/compile/compile-c-support.c +++ b/gdb/compile/compile-c-support.c @@ -75,12 +75,11 @@ c_get_range_decl_name (const struct dynamic_prop *prop) static gcc_c_fe_context_function * load_libcc (void) { - void *handle; gcc_c_fe_context_function *func; /* gdb_dlopen will call error () on an error, so no need to check value. */ - handle = gdb_dlopen (STRINGIFY (GCC_C_FE_LIBCC)); + gdb_dlhandle_up handle = gdb_dlopen (STRINGIFY (GCC_C_FE_LIBCC)); func = (gcc_c_fe_context_function *) gdb_dlsym (handle, STRINGIFY (GCC_C_FE_CONTEXT)); @@ -88,6 +87,9 @@ load_libcc (void) error (_("could not find symbol %s in library %s"), STRINGIFY (GCC_C_FE_CONTEXT), STRINGIFY (GCC_C_FE_LIBCC)); + + /* Leave the library open. */ + handle.release (); return func; } diff --git a/gdb/gdb-dlfcn.c b/gdb/gdb-dlfcn.c index a11e396..7485a38 100644 --- a/gdb/gdb-dlfcn.c +++ b/gdb/gdb-dlfcn.c @@ -31,27 +31,20 @@ #ifdef NO_SHARED_LIB -void * +gdb_dlhandle_up gdb_dlopen (const char *filename) { gdb_assert_not_reached ("gdb_dlopen should not be called on this platform."); } void * -gdb_dlsym (void *handle, const char *symbol) +gdb_dlsym (const gdb_dlhandle_up &handle, const char *symbol) { gdb_assert_not_reached ("gdb_dlsym should not be called on this platform."); } -struct cleanup * -make_cleanup_dlclose (void *handle) -{ - gdb_assert_not_reached ("make_cleanup_dlclose should not be called on this " - "platform."); -} - -int -gdb_dlclose (void *handle) +void +dlclose_deleter::operator() (void *handle) const { gdb_assert_not_reached ("gdb_dlclose should not be called on this platform."); } @@ -64,7 +57,7 @@ is_dl_available (void) #else /* NO_SHARED_LIB */ -void * +gdb_dlhandle_up gdb_dlopen (const char *filename) { void *result; @@ -74,7 +67,7 @@ gdb_dlopen (const char *filename) result = (void *) LoadLibrary (filename); #endif if (result != NULL) - return result; + return gdb_dlhandle_up (result); #ifdef HAVE_DLFCN_H error (_("Could not load %s: %s"), filename, dlerror()); @@ -97,37 +90,25 @@ gdb_dlopen (const char *filename) } void * -gdb_dlsym (void *handle, const char *symbol) +gdb_dlsym (const gdb_dlhandle_up &handle, const char *symbol) { #ifdef HAVE_DLFCN_H - return dlsym (handle, symbol); + return dlsym (handle.get (), symbol); #elif __MINGW32__ - return (void *) GetProcAddress ((HMODULE) handle, symbol); + return (void *) GetProcAddress ((HMODULE) handle.get (), symbol); #endif } -int -gdb_dlclose (void *handle) +void +dlclose_deleter::operator() (void *handle) const { #ifdef HAVE_DLFCN_H - return dlclose (handle); + dlclose (handle); #elif __MINGW32__ - return !((int) FreeLibrary ((HMODULE) handle)); + FreeLibrary ((HMODULE) handle); #endif } -static void -do_dlclose_cleanup (void *handle) -{ - gdb_dlclose (handle); -} - -struct cleanup * -make_cleanup_dlclose (void *handle) -{ - return make_cleanup (do_dlclose_cleanup, handle); -} - int is_dl_available (void) { diff --git a/gdb/gdb-dlfcn.h b/gdb/gdb-dlfcn.h index 31709a9..021f759 100644 --- a/gdb/gdb-dlfcn.h +++ b/gdb/gdb-dlfcn.h @@ -20,26 +20,28 @@ #ifndef GDB_DLFCN_H #define GDB_DLFCN_H +/* A deleter that closes an open dynamic library. */ + +struct dlclose_deleter +{ + void operator() (void *handle) const; +}; + +/* A unique pointer that points to a dynamic library. */ + +typedef std::unique_ptr gdb_dlhandle_up; + /* Load the dynamic library file named FILENAME, and return a handle for that dynamic library. Return NULL if the loading fails for any reason. */ -void *gdb_dlopen (const char *filename); +gdb_dlhandle_up gdb_dlopen (const char *filename); /* Return the address of the symbol named SYMBOL inside the shared library whose handle is HANDLE. Return NULL when the symbol could not be found. */ -void *gdb_dlsym (void *handle, const char *symbol); - -/* Install a cleanup routine which closes the handle HANDLE. */ - -struct cleanup *make_cleanup_dlclose (void *handle); - -/* Cleanup the shared object pointed to by HANDLE. Return 0 on success - and nonzero on failure. */ - -int gdb_dlclose (void *handle); +void *gdb_dlsym (const gdb_dlhandle_up &handle, const char *symbol); /* Return non-zero if the dynamic library functions are available on this platform. */ diff --git a/gdb/jit.c b/gdb/jit.c index 158d6d8..ddf1005 100644 --- a/gdb/jit.c +++ b/gdb/jit.c @@ -151,14 +151,29 @@ bfd_open_from_target_memory (CORE_ADDR addr, ULONGEST size, char *target) mem_bfd_iovec_stat); } +struct jit_reader +{ + jit_reader (struct gdb_reader_funcs *f, gdb_dlhandle_up &&h) + : functions (f), handle (std::move (h)) + { + } + + ~jit_reader () + { + functions->destroy (functions); + } + + jit_reader (const jit_reader &) = delete; + jit_reader &operator= (const jit_reader &) = delete; + + struct gdb_reader_funcs *functions; + gdb_dlhandle_up handle; +}; + /* One reader that has been loaded successfully, and can potentially be used to parse debug info. */ -static struct jit_reader -{ - struct gdb_reader_funcs *functions; - void *handle; -} *loaded_jit_reader = NULL; +static struct jit_reader *loaded_jit_reader = NULL; typedef struct gdb_reader_funcs * (reader_init_fn_type) (void); static const char *reader_init_fn_sym = "gdb_init_reader"; @@ -168,17 +183,13 @@ static const char *reader_init_fn_sym = "gdb_init_reader"; static struct jit_reader * jit_reader_load (const char *file_name) { - void *so; reader_init_fn_type *init_fn; - struct jit_reader *new_reader = NULL; struct gdb_reader_funcs *funcs = NULL; - struct cleanup *old_cleanups; if (jit_debug) fprintf_unfiltered (gdb_stdlog, _("Opening shared object %s.\n"), file_name); - so = gdb_dlopen (file_name); - old_cleanups = make_cleanup_dlclose (so); + gdb_dlhandle_up so = gdb_dlopen (file_name); init_fn = (reader_init_fn_type *) gdb_dlsym (so, reader_init_fn_sym); if (!init_fn) @@ -192,12 +203,7 @@ jit_reader_load (const char *file_name) if (funcs->reader_version != GDB_READER_INTERFACE_VERSION) error (_("Reader version does not match GDB version.")); - new_reader = XCNEW (struct jit_reader); - new_reader->functions = funcs; - new_reader->handle = so; - - discard_cleanups (old_cleanups); - return new_reader; + return new jit_reader (funcs, std::move (so)); } /* Provides the jit-reader-load command. */ @@ -240,10 +246,8 @@ jit_reader_unload_command (char *args, int from_tty) reinit_frame_cache (); jit_inferior_exit_hook (current_inferior ()); - loaded_jit_reader->functions->destroy (loaded_jit_reader->functions); - gdb_dlclose (loaded_jit_reader->handle); - xfree (loaded_jit_reader); + delete loaded_jit_reader; loaded_jit_reader = NULL; } -- 2.9.3