From patchwork Wed Aug 12 12:42:38 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Palka X-Patchwork-Id: 8148 Received: (qmail 76584 invoked by alias); 12 Aug 2015 12:42: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 76365 invoked by uid 89); 12 Aug 2015 12:42:47 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.5 required=5.0 tests=AWL, BAYES_00, KAM_LAZY_DOMAIN_SECURITY, RCVD_IN_DNSWL_LOW autolearn=no version=3.3.2 X-HELO: mail-qg0-f41.google.com Received: from mail-qg0-f41.google.com (HELO mail-qg0-f41.google.com) (209.85.192.41) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES128-GCM-SHA256 encrypted) ESMTPS; Wed, 12 Aug 2015 12:42:46 +0000 Received: by qgdd90 with SMTP id d90so9140421qgd.3 for ; Wed, 12 Aug 2015 05:42:44 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=lK/fE66is+9NrBFt1Ph1DrJ9xpcU/IA2QspEoUqDymE=; b=hBnAOZypFUaiU2wxXuXX93P5WVLB5BOoBlr+IVXzea98lC0BGT8y5s0XfSDGIESV6a xrHj6dnW6g2WXB/PiMUi8PWx6lj/a+y46VunSPvPBlFZ7xg/8EQrGcUq8Ex87TpWHH8H 19JhwI+SFGygti3BEXAttcy7vqNgDxJOnMR+inLC/XEAOtpxCUUCsxlu0vBi8i48ZHEF XD2HO6NNl/kDqn80pHzvJ7Bu5J8kgCz/UhfFjvvqxL5mU/ctuHGWC8DcZdso7VGyAv6X jQrVnGPKqssMAtHOLhQrafTpbqFKKfKW5wAfqWHr92kUtllzWDE8y7zd0w3N3iQ+bYm5 cTlQ== X-Gm-Message-State: ALoCoQnnvrrufMjHqwtB3cXXvIZUVOfQhf/d7b7DHQevfAqPfjfGROg9QIm9x1vqt26+XCFIsqDg X-Received: by 10.140.231.211 with SMTP id b202mr62412723qhc.83.1439383363964; Wed, 12 Aug 2015 05:42:43 -0700 (PDT) Received: from localhost.localdomain (ool-4353acd8.dyn.optonline.net. [67.83.172.216]) by smtp.gmail.com with ESMTPSA id k18sm3049764qhc.34.2015.08.12.05.42.43 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 12 Aug 2015 05:42:43 -0700 (PDT) From: Patrick Palka To: gdb-patches@sourceware.org Cc: Patrick Palka Subject: [COMMITTED] Introduce save_vars, a testsuite proc for safely manipulating globals Date: Wed, 12 Aug 2015 08:42:38 -0400 Message-Id: <1439383358-21248-1-git-send-email-patrick@parcs.ath.cx> Approved a few months ago here: https://sourceware.org/ml/gdb-patches/2015-06/msg00486.html gdb/testsuite/ChangeLog: * lib/gdb.exp (save_vars): New proc. --- gdb/testsuite/lib/gdb.exp | 67 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/gdb/testsuite/lib/gdb.exp b/gdb/testsuite/lib/gdb.exp index 5ecef1a..c5b7a06 100644 --- a/gdb/testsuite/lib/gdb.exp +++ b/gdb/testsuite/lib/gdb.exp @@ -1897,6 +1897,73 @@ proc with_test_prefix { prefix body } { } } +# Run BODY in the context of the caller. After BODY is run, the variables +# listed in VARS will be reset to the values they had before BODY was run. +# +# This is useful for providing a scope in which it is safe to temporarily +# modify global variables, e.g. +# +# global INTERNAL_GDBFLAGS +# global env +# +# set foo GDBHISTSIZE +# +# save_vars { INTERNAL_GDBFLAGS env($foo) env(HOME) } { +# append INTERNAL_GDBFLAGS " -nx" +# unset -nocomplain env(GDBHISTSIZE) +# gdb_start +# gdb_test ... +# } +# +# Here, although INTERNAL_GDBFLAGS, env(GDBHISTSIZE) and env(HOME) may be +# modified inside BODY, this proc guarantees that the modifications will be +# undone after BODY finishes executing. + +proc save_vars { vars body } { + array set saved_scalars { } + array set saved_arrays { } + set unset_vars { } + + foreach var $vars { + # First evaluate VAR in the context of the caller in case the variable + # name may be a not-yet-interpolated string like env($foo) + set var [uplevel 1 list $var] + + if [uplevel 1 [list info exists $var]] { + if [uplevel 1 [list array exists $var]] { + set saved_arrays($var) [uplevel 1 [list array get $var]] + } else { + set saved_scalars($var) [uplevel 1 [list set $var]] + } + } else { + lappend unset_vars $var + } + } + + set code [catch {uplevel 1 $body} result] + + foreach {var value} [array get saved_scalars] { + uplevel 1 [list set $var $value] + } + + foreach {var value} [array get saved_arrays] { + uplevel 1 [list unset $var] + uplevel 1 [list array set $var $value] + } + + foreach var $unset_vars { + uplevel 1 [list unset -nocomplain $var] + } + + if {$code == 1} { + global errorInfo errorCode + return -code $code -errorinfo $errorInfo -errorcode $errorCode $result + } else { + return -code $code $result + } +} + + # Run tests in BODY with GDB prompt and variable $gdb_prompt set to # PROMPT. When BODY is finished, restore GDB prompt and variable # $gdb_prompt.