From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id mL34FQAJXmaShyUAWB0awg (envelope-from ) for ; Mon, 03 Jun 2024 14:18:40 -0400 Authentication-Results: simark.ca; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=NcOpUAve; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id 5669F1E0C0; Mon, 3 Jun 2024 14:18:40 -0400 (EDT) Received: from server2.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 ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id 434221E0BB for ; Mon, 3 Jun 2024 14:18:38 -0400 (EDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id E880F382AA9E for ; Mon, 3 Jun 2024 18:18:37 +0000 (GMT) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 077C8382AA88 for ; Mon, 3 Jun 2024 18:17:08 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 077C8382AA88 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 077C8382AA88 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1717438632; cv=none; b=TG+SFz2nsUGZfQ06D4LCULlYe1zfD/+hrHQFC54XnszBj/lclrm8lKZ3wFXb85SM+XlppHyr7IEdfSWqG7vcCS+zijF+sA3xWo3fVNPl29R+xC3QvWcHDCwcTJEzzR5K3rfdYw/lTqzXiSBXDMepjnbvecVSgz3Xp2pS/qPz6po= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1717438632; c=relaxed/simple; bh=rwIq2IeX8w/v02x5e+bDbM+96ddAcVxK34fG/u3s4Vk=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=Y2J3V1M5TQiQ2HBKu0MnPnPqw+quLTby78X2uWa1YO6KQaQKVwtLRbQWk8ZVZd6kd6ZZE+qNBe9J33aIP8yd1JvfRlPgITxb4xiFI/blmkUY05jUqdFC7dBHeqR+mDqWXj3HYEXNkxoyIKzciBRgXGs4n5qdlhwWUB3Opd7JJQ4= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1717438627; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=trhKNKVFZCJcodJqdkssm+pOgC9G8XXlcHFr176oO6I=; b=NcOpUAve0JQNwx6OSphUvmOPi+rftsEjqT+JIcruhkvW6taB17zuqS2/F1ANjcr2H9rOos oAr7LDEOD7j/mU1N3AR1vW0owz5c83sH+1GH+7Yry5LGIIbyh/euYtpBIOWT3oDa1zMWyz YsYe1jZOsK1AMHdAAGQcxqquDv5pyGE= Received: from mail-wm1-f72.google.com (mail-wm1-f72.google.com [209.85.128.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-688-novMWvlTMgSDPuoToK8oCA-1; Mon, 03 Jun 2024 14:17:06 -0400 X-MC-Unique: novMWvlTMgSDPuoToK8oCA-1 Received: by mail-wm1-f72.google.com with SMTP id 5b1f17b1804b1-421292df2adso828565e9.3 for ; Mon, 03 Jun 2024 11:17:06 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717438624; x=1718043424; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=trhKNKVFZCJcodJqdkssm+pOgC9G8XXlcHFr176oO6I=; b=gWkmOOGeHeCZPgi4XdVP5JpBzz80GjKS5u9Yzjw2bzsLzgnlFhOmk4RaH6SGor0IWy 5+tnp1M4wNdNzuUiKE9Pv75Nis0wFELzTE605mMtdz6S1qwobdAvE/ItkIiTGQDbRsOq mn3mYzVRJnZaolYMQhPXIFn9ivFXN+BB+MHhTV9gE3UdbN5uD0PWS7NO/rnv8ESXRMgD 64/JKc93wl8wvxrmSrb3UzhMW5It0QLLkCp2kdcPCRpKKC/yypYEgfM/8Jz64XPTk6HK SwZU/Ex7p3TYQj6aeW1Vapjo1Ct1doz/N5EqsofbN7JEGBEjewFYETc+pRnAeDMtxlXP trSg== X-Gm-Message-State: AOJu0YzmfZo4c0oLQHk18fc/EcWF7PLYQMDZ/COu9AmYKAMnU8NpCuod tB6rMNOGgTfm2hbTDWNH5zTshGXeNb0zdNnEvTRbED+jtxpdZbadCt7D0mIhrf/UZQlAgz8Ce2F /p9ISFSj04+MuBEEHmilPpABDckIS/DmqMqGw0MeC/ABsWbjgADJw/hhVUy7RcIQ6YoiXXkkb9P pLqwMECku6/zYSkknuiQ7V40U9Ifa7UcOYBkdWp5lABqA= X-Received: by 2002:adf:efd1:0:b0:354:f3eb:798f with SMTP id ffacd0b85a97d-35e0f271879mr7020100f8f.24.1717438624259; Mon, 03 Jun 2024 11:17:04 -0700 (PDT) X-Google-Smtp-Source: AGHT+IF+ocUfdvgu/+NdSGOawJgxr+I+dLxvyQ8uD1BUaRqCojoZdqjb1wPGO9nxqqbi9akf6U5dsw== X-Received: by 2002:adf:efd1:0:b0:354:f3eb:798f with SMTP id ffacd0b85a97d-35e0f271879mr7020086f8f.24.1717438623766; Mon, 03 Jun 2024 11:17:03 -0700 (PDT) Received: from localhost (92.40.185.136.threembb.co.uk. [92.40.185.136]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-35dd04cad2csm9432553f8f.36.2024.06.03.11.17.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 03 Jun 2024 11:17:03 -0700 (PDT) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess Subject: [PATCH 2/4] gdb/testsuite: improve with_override Date: Mon, 3 Jun 2024 19:16:53 +0100 Message-Id: X-Mailer: git-send-email 2.25.4 In-Reply-To: References: MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true X-Spam-Status: No, score=-8.3 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_ABUSEAT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, RCVD_IN_SBL_CSS, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 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 I wanted to use 'with_override' to override a proc, but within the overridden proc I wanted to call the original function. I could just write my own version of 'with_override' that does what I want... ... or I could extend the existing 'with_override' to include this new functionality, which is what I've done in this commit. You can now do this: with_override some_proc new_proc save_name { ... body .... } Now, while BODY is executing calls to 'some_proc' will actually result in calling 'new_proc'. However, calling 'save_name' will call the original definition of 'some_proc'. If 'save_name' already exists when 'with_override' is called then the original value of 'save_name' will be backed up and then restored once the with_override has completed. If you don't need the new functionality then the old behaviour still works just fine, i.e.: with_override some_proc new_proc { ... body ... } My use of this new functionality will appear in a later commit, but for now I've added some unit-tests for the new functionality. --- gdb/testsuite/gdb.testsuite/with-override.exp | 44 ++++++++++++++ gdb/testsuite/lib/gdb.exp | 60 +++++++++++++++++-- 2 files changed, 99 insertions(+), 5 deletions(-) diff --git a/gdb/testsuite/gdb.testsuite/with-override.exp b/gdb/testsuite/gdb.testsuite/with-override.exp index a0a49625372..12467061ba7 100644 --- a/gdb/testsuite/gdb.testsuite/with-override.exp +++ b/gdb/testsuite/gdb.testsuite/with-override.exp @@ -26,10 +26,18 @@ proc foo2 {} { return 2 } +# Ensure that 'old_foo' doesn't exist. +if { [info procs old_foo] != "" } { + rename old_foo "" +} + with_test_prefix no-args { with_test_prefix before { gdb_assert { [foo] == 0 } + gdb_assert { [foo1] == 1 } + gdb_assert { [foo2] == 2 } + gdb_assert { [info procs old_foo] == "" } "old_foo does not exist" } with_override foo foo1 { @@ -44,8 +52,30 @@ with_test_prefix no-args { } } + with_override foo foo1 old_foo { + with_test_prefix old_foo { + with_test_prefix before { + gdb_assert { [old_foo] == 0 } + gdb_assert { [foo] == 1 } + } + + with_override foo foo2 old_foo { + gdb_assert { [old_foo] == 1 } + gdb_assert { [foo] == 2 } + } + + with_test_prefix after { + gdb_assert { [old_foo] == 0 } + gdb_assert { [foo] == 1 } + } + } + } + with_test_prefix after { gdb_assert { [foo] == 0 } + gdb_assert { [foo1] == 1 } + gdb_assert { [foo2] == 2 } + gdb_assert { [info procs old_foo] == "" } "old_foo does not exist" } } @@ -63,6 +93,7 @@ with_test_prefix default-arg { gdb_assert { [foo] == 1 } gdb_assert { [foo 0] == 1 } gdb_assert { [foo 1] == 2 } + gdb_assert { [info procs old_foo] == "" } "old_foo does not exist" } with_override foo foo_plus_1 { @@ -73,9 +104,22 @@ with_test_prefix default-arg { } } + with_override foo foo_plus_1 old_foo { + with_test_prefix old_foo { + gdb_assert { [foo] == 2 } + gdb_assert { [foo 0] == 2 } + gdb_assert { [foo 1] == 3 } + + gdb_assert { [old_foo] == 1 } + gdb_assert { [old_foo 0] == 1 } + gdb_assert { [old_foo 1] == 2 } + } + } + with_test_prefix after { gdb_assert { [foo] == 1 } gdb_assert { [foo 0] == 1 } gdb_assert { [foo 1] == 2 } + gdb_assert { [info procs old_foo] == "" } "old_foo does not exist" } } diff --git a/gdb/testsuite/lib/gdb.exp b/gdb/testsuite/lib/gdb.exp index cdc3721a1cd..8235d4f28eb 100644 --- a/gdb/testsuite/lib/gdb.exp +++ b/gdb/testsuite/lib/gdb.exp @@ -9726,10 +9726,18 @@ proc info_args_with_defaults { name } { return $args } +# Use as either: +# +# with_override NAME OVERRIDE BODY +# with_override NAME OVERRIDE SAVE_NAME BODY +# # Override proc NAME to proc OVERRIDE for the duration of the execution of -# BODY. +# a BODY. +# +# If the SAVE_NAME form is used then NAME will be available as +# SAVE_NAME for the duration of BODY. -proc with_override { name override body } { +proc with_override { name override args } { # Implementation note: It's possible to implement the override using # rename, like this: # rename $name save_$name @@ -9742,11 +9750,43 @@ proc with_override { name override body } { # - the override is no longer available under its original name during # the override # So, we use this more elaborate but cleaner mechanism. + # + # When the SAVE_NAME argument is provided to with_override then we + # do use rename, but we first backup any existing proc called + # SAVE_NAME, delete the existing SAVE_NAME, and only then do the + # rename. + + if { [llength $args] == 1 } { + set save_name "" + set body [lindex $args 0] + } elseif { [llength $args] == 2 } { + set save_name [lindex $args 0] + set body [lindex $args 1] + } else { + perror "invalid argument count to with_override: [llength $args]" + return + } + + # If the user wants to save the original proc, but the name they'd + # like to save into already exists then capture details of the + # thing we're about to overwrite. + if { $save_name != "" && [info procs $save_name] != "" } { + set save_name_args [info_args_with_defaults $save_name] + set save_name_body [info body $save_name] + rename $save_name "" + set save_name_existed true + } else { + set save_name_existed false + } # Save the old proc, if it exists. if { [info procs $name] != "" } { - set old_args [info_args_with_defaults $name] - set old_body [info body $name] + if { $save_name != "" } { + rename $name $save_name + } else { + set old_args [info_args_with_defaults $name] + set old_body [info body $name] + } set existed true } else { set existed false @@ -9762,11 +9802,21 @@ proc with_override { name override body } { # Restore old proc if it existed on entry, else delete it. if { $existed } { - eval proc $name {$old_args} {$old_body} + if { $save_name != "" } { + rename $name "" + rename $save_name $name + } else { + eval proc $name {$old_args} {$old_body} + } } else { rename $name "" } + # Restore the proc we saved over, if necessary. + if { $save_name_existed } { + eval proc $save_name {$save_name_args} {$save_name_body} + } + # Return as appropriate. if { $code == 1 } { global errorInfo errorCode -- 2.25.4