From patchwork Mon Aug 26 21:23:32 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Terekhov, Mikhail via Gdb-patches" X-Patchwork-Id: 34276 Received: (qmail 840 invoked by alias); 26 Aug 2019 21:23:47 -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 827 invoked by uid 89); 26 Aug 2019 21:23:47 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-21.8 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_STOCKGEN, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.1 spammy= X-HELO: mail-qt1-f201.google.com Received: from mail-qt1-f201.google.com (HELO mail-qt1-f201.google.com) (209.85.160.201) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Mon, 26 Aug 2019 21:23:45 +0000 Received: by mail-qt1-f201.google.com with SMTP id 91so18875701qtf.13 for ; Mon, 26 Aug 2019 14:23:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=ya2ONcEiPt3HEMAI9UhZUTF/xLjmMz9YKcWXxDJnPqs=; b=Bz0rmEq9wQvT8jMLsL69Z/KDfq6tw1M5Wxyb0yz1Idl8iMoybbuctpj9iyZ3HQm6rw +7Hn8qVn/ZiVwYQAamjCV1TFvekr63k5FYuFkLnYDlqpbReHKtO2P10upwdY0YqoFTBZ +++LdBQjhtOEhrVZ4B0henTq1R0SbDy1PQWfQHhzcgqx159MqZvQc0oPOUj6afFzWreE zyME0GN6jDMtjLmlplkjNk97CvU/mCWEVzQGFURsHYe9v/oU/ivF9+e/2CnpI1R3DrER osbs/e4Zmupf84MuhpovCIxs1JtUjTxvRpTMVrCIfhphew1s9uhHiuBSMAyASIGGAVzb N6OQ== Date: Mon, 26 Aug 2019 16:23:32 -0500 In-Reply-To: Message-Id: <20190826212332.209303-1-cbiesinger@google.com> Mime-Version: 1.0 References: Subject: [PATCH] Factor out the common code in lookup_{static, global}_symbol X-Patchwork-Original-From: "Christian Biesinger via gdb-patches" From: "Terekhov, Mikhail via Gdb-patches" Reply-To: Christian Biesinger To: gdb-patches@sourceware.org Cc: Christian Biesinger X-IsSubscribed: yes [Thanks, pushing this patch now, which has the two comments fixed.] The two functions are extremely similar; this factors out their code into a shared _internal function. gdb/ChangeLog: 2019-08-26 Christian Biesinger * symtab.c (lookup_static_symbol): Call the new function (and move it down to be next to lookup_global_symbol). (struct global_sym_lookup_data): Add block_enum member and rename to... (struct global_or_static_sym_lookup_data): ...this. (lookup_symbol_global_iterator_cb): Pass block_index instead of GLOBAL_BLOCK to lookup_symbol_in_objfile and rename to... (lookup_symbol_global_or_static_iterator_cb): ...this. (lookup_global_or_static_symbol): New function. (lookup_global_symbol): Call new function. --- gdb/symtab.c | 102 +++++++++++++++++++++++---------------------------- 1 file changed, 45 insertions(+), 57 deletions(-) diff --git a/gdb/symtab.c b/gdb/symtab.c index d85c77b4ce..787ecfe33b 100644 --- a/gdb/symtab.c +++ b/gdb/symtab.c @@ -2566,47 +2566,9 @@ lookup_symbol_in_objfile (struct objfile *objfile, enum block_enum block_index, return result; } -/* See symtab.h. */ - -struct block_symbol -lookup_static_symbol (const char *name, const domain_enum domain) -{ - struct symbol_cache *cache = get_symbol_cache (current_program_space); - struct block_symbol result; - struct block_symbol_cache *bsc; - struct symbol_cache_slot *slot; - - /* Lookup in STATIC_BLOCK is not current-objfile-dependent, so just pass - NULL for OBJFILE_CONTEXT. */ - result = symbol_cache_lookup (cache, NULL, STATIC_BLOCK, name, domain, - &bsc, &slot); - if (result.symbol != NULL) - { - if (SYMBOL_LOOKUP_FAILED_P (result)) - return {}; - return result; - } - - for (objfile *objfile : current_program_space->objfiles ()) - { - result = lookup_symbol_in_objfile (objfile, STATIC_BLOCK, name, domain); - if (result.symbol != NULL) - { - /* Still pass NULL for OBJFILE_CONTEXT here. */ - symbol_cache_mark_found (bsc, slot, NULL, result.symbol, - result.block); - return result; - } - } - - /* Still pass NULL for OBJFILE_CONTEXT here. */ - symbol_cache_mark_not_found (bsc, slot, NULL, name, domain); - return {}; -} - /* Private data to be used with lookup_symbol_global_iterator_cb. */ -struct global_sym_lookup_data +struct global_or_static_sym_lookup_data { /* The name of the symbol we are searching for. */ const char *name; @@ -2614,27 +2576,30 @@ struct global_sym_lookup_data /* The domain to use for our search. */ domain_enum domain; + /* The block index in which to search. */ + enum block_enum block_index; + /* The field where the callback should store the symbol if found. It should be initialized to {NULL, NULL} before the search is started. */ struct block_symbol result; }; /* A callback function for gdbarch_iterate_over_objfiles_in_search_order. - It searches by name for a symbol in the GLOBAL_BLOCK of the given - OBJFILE. The arguments for the search are passed via CB_DATA, - which in reality is a pointer to struct global_sym_lookup_data. */ + It searches by name for a symbol in the block given by BLOCK_INDEX of the + given OBJFILE. The arguments for the search are passed via CB_DATA, which + in reality is a pointer to struct global_or_static_sym_lookup_data. */ static int -lookup_symbol_global_iterator_cb (struct objfile *objfile, - void *cb_data) +lookup_symbol_global_or_static_iterator_cb (struct objfile *objfile, + void *cb_data) { - struct global_sym_lookup_data *data = - (struct global_sym_lookup_data *) cb_data; + struct global_or_static_sym_lookup_data *data = + (struct global_or_static_sym_lookup_data *) cb_data; gdb_assert (data->result.symbol == NULL && data->result.block == NULL); - data->result = lookup_symbol_in_objfile (objfile, GLOBAL_BLOCK, + data->result = lookup_symbol_in_objfile (objfile, data->block_index, data->name, data->domain); /* If we found a match, tell the iterator to stop. Otherwise, @@ -2642,25 +2607,28 @@ lookup_symbol_global_iterator_cb (struct objfile *objfile, return (data->result.symbol != NULL); } -/* See symtab.h. */ +/* This function contains the common code of lookup_{global,static}_symbol. + OBJFILE is only used if BLOCK_INDEX is GLOBAL_SCOPE, in which case it is + the objfile to start the lookup in. */ -struct block_symbol -lookup_global_symbol (const char *name, - const struct block *block, - const domain_enum domain) +static struct block_symbol +lookup_global_or_static_symbol (const char *name, + enum block_enum block_index, + struct objfile *objfile, + const domain_enum domain) { struct symbol_cache *cache = get_symbol_cache (current_program_space); struct block_symbol result; - struct objfile *objfile; - struct global_sym_lookup_data lookup_data; + struct global_or_static_sym_lookup_data lookup_data; struct block_symbol_cache *bsc; struct symbol_cache_slot *slot; - objfile = lookup_objfile_from_block (block); + gdb_assert (block_index == GLOBAL_BLOCK || block_index == STATIC_BLOCK); + gdb_assert (objfile == nullptr || block_index == GLOBAL_BLOCK); /* First see if we can find the symbol in the cache. This works because we use the current objfile to qualify the lookup. */ - result = symbol_cache_lookup (cache, objfile, GLOBAL_BLOCK, name, domain, + result = symbol_cache_lookup (cache, objfile, block_index, name, domain, &bsc, &slot); if (result.symbol != NULL) { @@ -2678,10 +2646,11 @@ lookup_global_symbol (const char *name, { memset (&lookup_data, 0, sizeof (lookup_data)); lookup_data.name = name; + lookup_data.block_index = block_index; lookup_data.domain = domain; gdbarch_iterate_over_objfiles_in_search_order (objfile != NULL ? get_objfile_arch (objfile) : target_gdbarch (), - lookup_symbol_global_iterator_cb, &lookup_data, objfile); + lookup_symbol_global_or_static_iterator_cb, &lookup_data, objfile); result = lookup_data.result; } @@ -2693,6 +2662,25 @@ lookup_global_symbol (const char *name, return result; } +/* See symtab.h. */ + +struct block_symbol +lookup_static_symbol (const char *name, const domain_enum domain) +{ + return lookup_global_or_static_symbol (name, STATIC_BLOCK, nullptr, domain); +} + +/* See symtab.h. */ + +struct block_symbol +lookup_global_symbol (const char *name, + const struct block *block, + const domain_enum domain) +{ + struct objfile *objfile = lookup_objfile_from_block (block); + return lookup_global_or_static_symbol (name, GLOBAL_BLOCK, objfile, domain); +} + int symbol_matches_domain (enum language symbol_language, domain_enum symbol_domain,