From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id wlTGBr1yCmFhDwAAWB0awg (envelope-from ) for ; Wed, 04 Aug 2021 06:58:05 -0400 Received: by simark.ca (Postfix, from userid 112) id 09D5B1EDE3; Wed, 4 Aug 2021 06:58:05 -0400 (EDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-1.1 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,MAILING_LIST_MULTI,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.2 Received: from sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id A08431E4A3 for ; Wed, 4 Aug 2021 06:58:03 -0400 (EDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id E36F0393C035 for ; Wed, 4 Aug 2021 10:58:02 +0000 (GMT) Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by sourceware.org (Postfix) with ESMTPS id BB90B3858C27 for ; Wed, 4 Aug 2021 10:57:51 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org BB90B3858C27 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=suse.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=suse.de Received: from imap1.suse-dmz.suse.de (imap1.suse-dmz.suse.de [192.168.254.73]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id D191020129 for ; Wed, 4 Aug 2021 10:57:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1628074670; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=44MkURAiat5ei5eRnPCtnxA7tqizPXTw0Ba861PyuvI=; b=o/ZURwQRJsknvAtZ7LBmSLBiRABqCiEWLpi5KF8bxSQAb988ljkpKJwMRelBU938J+e4eP OVODxZf3JJ2kdn6xfJZdQ4ejypuxfFiEA4m20W2xvrnVVRutvgGMO7xfQgl051ZTmuu+cv l5ithf/SXdivHeByVD3Z1v5pApUESdI= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1628074670; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=44MkURAiat5ei5eRnPCtnxA7tqizPXTw0Ba861PyuvI=; b=NTkJua2y/PAmOTZj0yIYcKy602kmJJ3DYd0OBd4HXjnAyanqCLRADZMDKo9nv3MaD3C0kI VFCxa7TlM3tJcGDQ== Received: from imap1.suse-dmz.suse.de (imap1.suse-dmz.suse.de [192.168.254.73]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap1.suse-dmz.suse.de (Postfix) with ESMTPS id BB8BE13942 for ; Wed, 4 Aug 2021 10:57:50 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap1.suse-dmz.suse.de with ESMTPSA id pCNFLK5yCmElCwAAGKfGzw (envelope-from ) for ; Wed, 04 Aug 2021 10:57:50 +0000 Subject: [committed][PATCH][gdb/symtab] Implement addrmap_mutable_find From: Tom de Vries To: gdb-patches@sourceware.org References: <20210803144656.GA8040@delia> Message-ID: <713c45a3-bb9d-c60b-e8eb-236d0ab7aade@suse.de> Date: Wed, 4 Aug 2021 12:57:50 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.11.0 MIME-Version: 1.0 In-Reply-To: <20210803144656.GA8040@delia> Content-Type: multipart/mixed; boundary="------------D4E1F87014A8EB312E1219AC" Content-Language: en-US X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces+public-inbox=simark.ca@sourceware.org Sender: "Gdb-patches" This is a multi-part message in MIME format. --------------D4E1F87014A8EB312E1219AC Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit On 8/3/21 4:46 PM, Tom de Vries wrote: > Hi, > > Currently addrmap_mutable_find is not implemented: > ... > static void * > addrmap_mutable_find (struct addrmap *self, CORE_ADDR addr) > { > /* Not needed yet. */ > internal_error (__FILE__, __LINE__, > _("addrmap_find is not implemented yet " > "for mutable addrmaps")); > } > ... > > I implemented this because I needed this during debugging, to be able to do: > ... > (gdb) p ((dwarf2_psymtab *)addrmap_find (map, addr))->filename > ... > before and after a call to addrmap_set_empty. > > Rebuild on x86_64-linux, tested by using it during debugging session. Which doesn't prevent bitrot, so I've added unit tests. Committed. Thanks, - Tom --------------D4E1F87014A8EB312E1219AC Content-Type: text/x-patch; charset=UTF-8; name="0001-gdb-symtab-Implement-addrmap_mutable_find.patch" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="0001-gdb-symtab-Implement-addrmap_mutable_find.patch" [gdb/symtab] Implement addrmap_mutable_find Currently addrmap_mutable_find is not implemented: ... static void * addrmap_mutable_find (struct addrmap *self, CORE_ADDR addr) { /* Not needed yet. */ internal_error (__FILE__, __LINE__, _("addrmap_find is not implemented yet " "for mutable addrmaps")); } ... I implemented this because I needed it during debugging, to be able to do: ... (gdb) p ((dwarf2_psymtab *)addrmap_find (map, addr))->filename ... before and after a call to addrmap_set_empty. Since this is not used otherwise, added addrmap unit test. Build on x86_64-linux, tested by doing: ... $ gdb -q -batch -ex "maint selftest addrmap" Running selftest addrmap. Ran 1 unit tests, 0 failed ... gdb/ChangeLog: 2021-08-03 Tom de Vries * gdb/addrmap.c (addrmap_mutable_find): Implement [GDB_SELF_TESTS] (CHECK_ADDRMAP_FIND): New macro. [GDB_SELF_TESTS] (core_addr, addrmap_foreach_check, test_addrmap) (_initialize_addrmap): New function. --- gdb/addrmap.c | 130 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 126 insertions(+), 4 deletions(-) diff --git a/gdb/addrmap.c b/gdb/addrmap.c index 9bd924e8382..2cb472604f4 100644 --- a/gdb/addrmap.c +++ b/gdb/addrmap.c @@ -21,6 +21,7 @@ #include "splay-tree.h" #include "gdb_obstack.h" #include "addrmap.h" +#include "gdbsupport/selftest.h" /* Make sure splay trees can actually hold the values we want to store in them. */ @@ -390,10 +391,22 @@ addrmap_mutable_set_empty (struct addrmap *self, static void * addrmap_mutable_find (struct addrmap *self, CORE_ADDR addr) { - /* Not needed yet. */ - internal_error (__FILE__, __LINE__, - _("addrmap_find is not implemented yet " - "for mutable addrmaps")); + struct addrmap_mutable *map = (struct addrmap_mutable *) self; + splay_tree_node n = addrmap_splay_tree_lookup (map, addr); + if (n != nullptr) + { + gdb_assert (addrmap_node_key (n) == addr); + return addrmap_node_value (n); + } + + n = addrmap_splay_tree_predecessor (map, addr); + if (n != nullptr) + { + gdb_assert (addrmap_node_key (n) < addr); + return addrmap_node_value (n); + } + + return nullptr; } @@ -576,3 +589,112 @@ addrmap_create_mutable (struct obstack *obstack) return (struct addrmap *) map; } + +#if GDB_SELF_TEST +namespace selftests { + +/* Convert P to CORE_ADDR. */ + +static CORE_ADDR +core_addr (void *p) +{ + return (CORE_ADDR)(uintptr_t)p; +} + +/* Check that &ARRAY[LOW]..&ARRAY[HIGH] has VAL in MAP. */ + +#define CHECK_ADDRMAP_FIND(MAP, ARRAY, LOW, HIGH, VAL) \ + do \ + { \ + for (unsigned i = LOW; i <= HIGH; ++i) \ + SELF_CHECK (addrmap_find (MAP, core_addr (&ARRAY[i])) == VAL); \ + } \ + while (0) + +/* We'll verify using the addresses of the elements of this array. */ +static char *array; +/* We'll verify using these values stored into the map. */ +static void *val1; +static void *val2; + +/* Callback for addrmap_foreach to check transitions. */ + +static int +addrmap_foreach_check (CORE_ADDR start_addr, void *obj) +{ + if (start_addr == core_addr (nullptr)) + SELF_CHECK (obj == nullptr); + else if (start_addr == core_addr (&array[10])) + SELF_CHECK (obj == val1); + else if (start_addr == core_addr (&array[13])) + SELF_CHECK (obj == nullptr); + else + SELF_CHECK (false); + return 0; +} + +/* Entry point for addrmap unit tests. */ + +static void +test_addrmap () +{ + /* Initialize static variables. */ + char local_array[20]; + array = local_array; + val1 = &array[1]; + val2 = &array[2]; + + /* Create mutable addrmap. */ + static struct obstack temp_obstack; + obstack_init (&temp_obstack); + struct addrmap *map = addrmap_create_mutable (&temp_obstack); + SELF_CHECK (map != nullptr); + + /* Check initial state. */ + CHECK_ADDRMAP_FIND (map, array, 0, 19, nullptr); + + /* Insert address range into mutable addrmap. */ + addrmap_set_empty (map, core_addr (&array[10]), core_addr (&array[12]), + val1); + CHECK_ADDRMAP_FIND (map, array, 0, 9, nullptr); + CHECK_ADDRMAP_FIND (map, array, 10, 12, val1); + CHECK_ADDRMAP_FIND (map, array, 13, 19, nullptr); + + /* Create corresponding fixed addrmap. */ + struct addrmap *map2 = addrmap_create_fixed (map, &temp_obstack); + SELF_CHECK (map2 != nullptr); + CHECK_ADDRMAP_FIND (map2, array, 0, 9, nullptr); + CHECK_ADDRMAP_FIND (map2, array, 10, 12, val1); + CHECK_ADDRMAP_FIND (map2, array, 13, 19, nullptr); + + /* Iterate over both addrmaps. */ + SELF_CHECK (addrmap_foreach (map, addrmap_foreach_check) == 0); + SELF_CHECK (addrmap_foreach (map2, addrmap_foreach_check) == 0); + + /* Relocate fixed addrmap. */ + addrmap_relocate (map2, 1); + CHECK_ADDRMAP_FIND (map2, array, 0, 10, nullptr); + CHECK_ADDRMAP_FIND (map2, array, 11, 13, val1); + CHECK_ADDRMAP_FIND (map2, array, 14, 19, nullptr); + + /* Insert partially overlapping address range into mutable addrmap. */ + addrmap_set_empty (map, core_addr (&array[11]), core_addr (&array[13]), + val2); + CHECK_ADDRMAP_FIND (map, array, 0, 9, nullptr); + CHECK_ADDRMAP_FIND (map, array, 10, 12, val1); + CHECK_ADDRMAP_FIND (map, array, 13, 13, val2); + CHECK_ADDRMAP_FIND (map, array, 14, 19, nullptr); + + /* Cleanup. */ + obstack_free (&temp_obstack, NULL); +} + +} // namespace selftests + +void _initialize_addrmap (); +void +_initialize_addrmap () +{ + selftests::register_test ("addrmap", selftests::test_addrmap); +} +#endif /* GDB_SELF_TEST */ --------------D4E1F87014A8EB312E1219AC--