From patchwork Sat Jan 20 21:34:25 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Burgess X-Patchwork-Id: 25472 Received: (qmail 76391 invoked by alias); 20 Jan 2018 21:34:44 -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 76276 invoked by uid 89); 20 Jan 2018 21:34:43 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-25.4 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_PASS, URIBL_RED autolearn=ham version=3.3.2 spammy=pc, Track, 10031 X-HELO: mail-wr0-f180.google.com Received: from mail-wr0-f180.google.com (HELO mail-wr0-f180.google.com) (209.85.128.180) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Sat, 20 Jan 2018 21:34:38 +0000 Received: by mail-wr0-f180.google.com with SMTP id d9so4655975wre.3 for ; Sat, 20 Jan 2018 13:34:38 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:in-reply-to:references; bh=LXW8vALczzyJuDOxeklPqYS7Pf/VL8kscM7LT05QOzM=; b=D3ncbfYQwAbRePCBaXNLyGsy8G8IdlmLRGgyRleCNW7N1frR+VoL60M87M1+vBhPy0 yOWQEjxGQdQYYJSUeWzse+LZ/SnNrRzjykzkZm/f5BqcJWaXwNfWywStUdFzi9ebye2R DP/onDdmxxJ7VPOfLsWo+zxGhG78F+PqwJ5hmlxJwEdQ7TSQIOW/nhl4t7gMoQBfjINE Nn0q6gqXFqgKCQnlpz28InmSOxtPUpJcaoQ46WMTyB7MEgWJP2AI5PSlCs9j04M9OEMF C7F4ATlUnhdy6btw9nGgQ6dSIajhDdNCNfNzTuGdAOb+rEOcTQb2FGWShLwQnqy0r88E HxRw== X-Gm-Message-State: AKwxyte6MPF8+wQX03SPNFM+HGSDpc2OAuA7rIUYMNaBfbrY2SG93EZa R/z01n4LEALcFLmXYTecn7eMNbtM X-Google-Smtp-Source: AH8x225SokLyKecYsxezjEs09jpFr6ZLnhmgK0mOh9vmG4MH6r2C2/9hr5G14lnLdDprEsLNXr00HA== X-Received: by 10.223.185.21 with SMTP id k21mr2457622wrf.37.1516484075726; Sat, 20 Jan 2018 13:34:35 -0800 (PST) Received: from localhost (host86-164-103-151.range86-164.btcentralplus.com. [86.164.103.151]) by smtp.gmail.com with ESMTPSA id k3sm776800wmk.3.2018.01.20.13.34.34 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Sat, 20 Jan 2018 13:34:34 -0800 (PST) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: donb@codesourcery.com, simark@simark.ca, palves@redhat.com, Andrew Burgess Subject: [PATCHv6 3/5] gdb: PR mi/20395: Fix -var-update for registers in frames 1 and up Date: Sat, 20 Jan 2018 21:34:25 +0000 Message-Id: In-Reply-To: References: In-Reply-To: References: X-IsSubscribed: yes This patch fixes a problem with using the MI -var-update command to access the values of registers in frames other than the current frame. The patch includes a test that demonstrates the problem: * run so there are several frames on the stack * create a fixed varobj for $pc in each frame, #'s 1 and above * step one instruction, to modify the value of $pc * call -var-update for each of the previously created varobjs to verify that they are not reported as having changed. Without the patch, the -var-update command reported that $pc for all frames 1 and above had changed to the value of $pc in frame 0. A varobj is created as either fixed, the expression is evaluated within the context of a specific frame, or floating, the expression is evaluated within the current frame, whatever that may be. When a varobj is created by -var-create we set two fields of the varobj to track the context in which the varobj was created, these two fields are varobj->root->frame and var->root->valid_block. If a varobj is of type fixed, then, when we subsequently try to reevaluate the expression associated with the varobj we must determine if the original frame (and block) is still available, if it is not then the varobj can no longer be evaluated. The problem is that for register expressions varobj->root->valid_block is not set correctly. This block tracking is done using the global 'innermost_block' which is set in the various parser files (for example c-exp.y). However, this is not set for register expressions. The fix then seems like it should be to just update the innermost block when parsing register expressions, however, that solution causes several test regressions. The problem is that in some cases we rely on the expression parsing code not updating the innermost block for registers, one example is when we parse the expression for a 'display' command. The display commands treats registers like floating varobjs, but symbols are treated like fixed varobjs. So 'display $reg_name' will always show the value of '$reg_name' even as the user moves from frame to frame, while 'display my_variable' will only show 'my_variable' while it is in the current frame and/or block, when the user moves to a new frame and/or block (even one with a different 'my_variable' in) then the display of 'my_variable' stops. For the case of 'display', without the option to force fixed or floating expressions, the current behaviour is probably the best choice. For the varobj system though, we can choose between floating and fixed, and we should try to make this work for registers. There's only one existing test case that needs to be updated, in that test a fixed varobj is created using a register, the MI output now include the thread-id in which the varobj should be evaluated, which I believe is correct behaviour. I also added a new floating test case into the same test script, however, right now this also includes the thread-id in the expected output, which I believe is an existing gdb bug, which I plan to fix next. Tested on x86_64 Linux native and native-gdbserver, no regressions. gdb/ChangeLog: PR mi/20395 * ada-exp.y (write_var_from_sym): Pass extra parameter when updating innermost block. * parse.c (innermost_block_tracker::update): Take extra type parameter, and check types match before updating innermost block. (write_dollar_variable): Update innermost block for registers. * parser-defs.h (enum innermost_block_tracker::track_type): New. (innermost_block_tracker::reset): Take type parameter. (innermost_block_tracker::update): Take type parameter, and pass type through as needed. (innermost_block_tracker::type): New member. (operator|): New function for innermost_block_tracker::track_type. * varobj.c (varobj_create): Pass type when reseting innermost block. gdb/testsuite/ChangeLog: * gdb.mi/basics.c: Add new global. * gdb.mi/mi-frame-regs.exp: New file. * gdb.mi/mi-var-create-rtti.exp: Update expected results, add new case. --- gdb/ChangeLog | 17 +++ gdb/ada-exp.y | 2 +- gdb/parse.c | 9 +- gdb/parser-defs.h | 46 +++++-- gdb/testsuite/ChangeLog | 8 ++ gdb/testsuite/gdb.mi/basics.c | 2 + gdb/testsuite/gdb.mi/mi-frame-regs.exp | 186 ++++++++++++++++++++++++++++ gdb/testsuite/gdb.mi/mi-var-create-rtti.exp | 5 +- gdb/varobj.c | 3 +- 9 files changed, 264 insertions(+), 14 deletions(-) create mode 100644 gdb/testsuite/gdb.mi/mi-frame-regs.exp diff --git a/gdb/ChangeLog b/gdb/ChangeLog index 13733ea00e8..74789a1abae 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,3 +1,20 @@ +2017-10-19 Andrew Burgess + + PR mi/20395 + * ada-exp.y (write_var_from_sym): Pass extra parameter when + updating innermost block. + * parse.c (innermost_block_tracker::update): Take extra type + parameter, and check types match before updating innermost block. + (write_dollar_variable): Update innermost block for registers. + * parser-defs.h (enum innermost_block_tracker::track_type): New. + (innermost_block_tracker::reset): Take type parameter. + (innermost_block_tracker::update): Take type parameter, and pass + type through as needed. + (innermost_block_tracker::type): New member. + (operator|): New function for innermost_block_tracker::track_type. + * varobj.c (varobj_create): Pass type when reseting innermost + block. + 2017-10-19 Andrew Burgess * ada-exp.y (write_var_from_sym): Switch to innermost_block API. diff --git a/gdb/ada-exp.y b/gdb/ada-exp.y index 56113186b97..ac4c341bfe9 100644 --- a/gdb/ada-exp.y +++ b/gdb/ada-exp.y @@ -757,7 +757,7 @@ write_var_from_sym (struct parser_state *par_state, struct symbol *sym) { if (symbol_read_needs_frame (sym)) - innermost_block.update (block); + innermost_block.update (block, INNERMOST_BLOCK_FOR_SYMBOLS); write_exp_elt_opcode (par_state, OP_VAR_VALUE); write_exp_elt_block (par_state, block); diff --git a/gdb/parse.c b/gdb/parse.c index ca5eb023869..e3f1306a175 100644 --- a/gdb/parse.c +++ b/gdb/parse.c @@ -124,9 +124,12 @@ static expression_up parse_exp_in_context_1 (const char **, CORE_ADDR, /* Documented at it's declaration. */ void -innermost_block_tracker::update (const struct block *b) +innermost_block_tracker::update (const struct block *b, + innermost_block_tracker_types t) { - if (m_innermost_block == NULL || contained_in (b, m_innermost_block)) + if ((m_types & t) != 0 + && (m_innermost_block == NULL + || contained_in (b, m_innermost_block))) m_innermost_block = b; } @@ -691,6 +694,8 @@ handle_register: str.ptr++; write_exp_string (ps, str); write_exp_elt_opcode (ps, OP_REGISTER); + innermost_block.update (expression_context_block, + INNERMOST_BLOCK_FOR_REGISTERS); return; } diff --git a/gdb/parser-defs.h b/gdb/parser-defs.h index 01ac0cd363a..11865b42ba9 100644 --- a/gdb/parser-defs.h +++ b/gdb/parser-defs.h @@ -75,6 +75,24 @@ extern const struct block *expression_context_block; then look up the macro definitions active at that point. */ extern CORE_ADDR expression_context_pc; +/* While parsing expressions we need to track the innermost lexical block + that we encounter. In some situations we need to track the innermost + block just for symbols, and in other situations we want to track the + innermost block for symbols and registers. These flags are used by the + innermost block track to control which blocks we consider for the + innermost block, these flags can be combined together as needed. */ + +enum innermost_block_tracker_type +{ + /* Track the innermost block for symbols within an expression. */ + INNERMOST_BLOCK_FOR_SYMBOLS = (1 << 0), + + /* Track the innermost block for registers within an expression. */ + INNERMOST_BLOCK_FOR_REGISTERS = (1 << 1) +}; +DEF_ENUM_FLAGS_TYPE (enum innermost_block_tracker_type, + innermost_block_tracker_types); + /* When parsing expressions we track the innermost block that was referenced. */ @@ -82,24 +100,31 @@ class innermost_block_tracker { public: innermost_block_tracker () - : m_innermost_block (NULL) + : m_types (INNERMOST_BLOCK_FOR_SYMBOLS), + m_innermost_block (NULL) { /* Nothing. */ } /* Reset the currently stored innermost block. Usually called before - parsing a new expression. */ - void reset () + parsing a new expression. As the most common case is that we only + want to gather the innermost block for symbols in an expression, this + becomes the default block tracker type. */ + void reset (innermost_block_tracker_types t = INNERMOST_BLOCK_FOR_SYMBOLS) { - m_innermost_block = nullptr; + m_types = t; + m_innermost_block = NULL; } /* Update the stored innermost block if the new block B is more inner - than the currently stored block, or if no block is stored yet. */ - void update (const struct block *b); + than the currently stored block, or if no block is stored yet. The + type T tells us whether the block B was for a symbol or for a + register, the stored innermost block is only updated if the type T is + a type we are interested in, which was set during reset. */ + void update (const struct block *b, innermost_block_tracker_types t); /* Overload of main UPDATE method which extracts the block from BS. */ void update (const struct block_symbol &bs) { - update (bs.block); + update (bs.block, INNERMOST_BLOCK_FOR_SYMBOLS); } /* Return the stored innermost block. Can be nullptr if no symbols or @@ -111,13 +136,16 @@ public: } private: + /* The type of innermost block being looked for. */ + innermost_block_tracker_types m_types; + /* The currently stored innermost block found while parsing an expression. */ const struct block *m_innermost_block; }; -/* The innermost context required by the stack and register variables we've - encountered so far. This should be cleared before parsing an +/* The innermost context required by the stack and register variables + we've encountered so far. This should be cleared before parsing an expression, and queried once the parse is complete. */ extern innermost_block_tracker innermost_block; diff --git a/gdb/testsuite/ChangeLog b/gdb/testsuite/ChangeLog index 2b968010fd5..c65c4f7d328 100644 --- a/gdb/testsuite/ChangeLog +++ b/gdb/testsuite/ChangeLog @@ -1,3 +1,11 @@ +2017-10-19 Don Breazeal + Andrew Burgess + + * gdb.mi/basics.c: Add new global. + * gdb.mi/mi-frame-regs.exp: New file. + * gdb.mi/mi-var-create-rtti.exp: Update expected results, add new + case. + 2018-01-19 Tom Tromey * gdb.rust/modules.rs (TWENTY_THREE): New global. diff --git a/gdb/testsuite/gdb.mi/basics.c b/gdb/testsuite/gdb.mi/basics.c index 08d9ccae178..327f33c6eca 100644 --- a/gdb/testsuite/gdb.mi/basics.c +++ b/gdb/testsuite/gdb.mi/basics.c @@ -20,6 +20,8 @@ * on function calls. Useful to test printing frames, stepping, etc. */ +unsigned long long global_zero = 0; + int callee4 (void) { int A=1; diff --git a/gdb/testsuite/gdb.mi/mi-frame-regs.exp b/gdb/testsuite/gdb.mi/mi-frame-regs.exp new file mode 100644 index 00000000000..413fa5c3c92 --- /dev/null +++ b/gdb/testsuite/gdb.mi/mi-frame-regs.exp @@ -0,0 +1,186 @@ +# Copyright 2018 Free Software Foundation, Inc. + +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +# Test essential Machine interface (MI) operations +# +# Verify that -var-update will provide the correct values for floating +# and fixed varobjs that represent the pc register. +# + +load_lib mi-support.exp +set MIFLAGS "-i=mi" + +standard_testfile basics.c + +if {[gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" \ + executable {debug}] != "" } then { + untested mi-frame-regs.exp + return -1 +} + +# Return the address of the specified breakpoint. + +proc breakpoint_address {bpnum} { + global hex + global expect_out + global mi_gdb_prompt + + send_gdb "info breakpoint $bpnum\n" + gdb_expect { + -re ".*($hex).*$mi_gdb_prompt$" { + return $expect_out(1,string) + } + -re ".*$mi_gdb_prompt$" { + unresolved "get address of breakpoint $bpnum" + return "" + } + timeout { + unresolved "get address of breakpoint $bpnum (timeout)" + return "" + } + } +} + +# Test that a floating varobj representing $pc will provide the +# correct value via -var-update as the program stops at +# breakpoints in different functions. + +proc_with_prefix do_floating_varobj_test {} { + global srcfile + global hex + global expect_out + + gdb_exit + if {[mi_gdb_start]} then { + fail "couldn't start gdb" + return + } + + mi_run_to_main + + # Create a floating varobj for $pc. + mi_gdb_test "-var-create --thread 1 --frame 0 - @ \$pc" \ + "\\^done,.*value=\"$hex.*" \ + "create varobj for pc in frame 0" + + set nframes 4 + for {set i 1} {$i < $nframes} {incr i} { + + # Run to a breakpoint in each callee function in succession. + # Note that we can't use mi_runto because we need the + # breakpoint to be persistent, so we can use its address. + set bpnum [expr $i + 1] + mi_create_breakpoint \ + "basics.c:callee$i" \ + "insert breakpoint at basics.c:callee$i" \ + -number $bpnum -func callee$i -file ".*basics.c" + + mi_execute_to "exec-continue" "breakpoint-hit" \ + "callee$i" ".*" ".*${srcfile}" ".*" \ + { "" "disp=\"keep\"" } "breakpoint hit in callee$i" + + # Get the value of $pc from the floating varobj. + mi_gdb_test "-var-update 1 var1" \ + "\\^done,.*value=\"($hex) .*" \ + "-var-update for frame $i" + set pcval $expect_out(3,string) + + # Get the address of the current breakpoint. + set bpaddr [breakpoint_address $bpnum] + if {$bpaddr == ""} then { return } + + # Check that the addresses are the same. + gdb_assert [expr $bpaddr == $pcval] "\$pc equals address of breakpoint in callee$i" + } +} + +# Test that fixed varobjs representing $pc in different stack frames +# will provide the correct value via -var-update after the program +# counter changes (without substantially changing the stack). + +proc_with_prefix do_fixed_varobj_test {} { + global srcfile + global hex + + gdb_exit + if {[mi_gdb_start]} then { + fail "couldn't start gdb" + return + } + + mi_run_to_main + + # Run to the function 'callee3' so we have several frames. + mi_create_breakpoint "basics.c:callee3" \ + "insert breakpoint at basics.c:callee3" \ + -number 2 -func callee3 -file ".*basics.c" + + mi_execute_to "exec-continue" "breakpoint-hit" \ + "callee3" ".*" ".*${srcfile}" ".*" \ + { "" "disp=\"keep\"" } "breakpoint hit in callee3" + + # At the breakpoint in callee3 there are 4 frames. + # + # Create some varobj based on $pc in all frames. When we single + # step we expect the varobj for frame 0 to change, while the + # varobj for all other frames should be unchanged. + # + # Track in FIRST_UNCHANGING_VARNUM the number of the first varobj + # that is not in frame 0, varobj with a lower number we expect to + # change, while this and later varobj should not change. + # + # Track the number of the next varobj to be created in VARNUM. + set first_unchanging_varnum 0 + set varnum 1 + + for {set i 0} {$i < 4} {incr i} { + + if { $i == 1 } then { set first_unchanging_varnum $varnum } + + mi_gdb_test "-var-create --thread 1 --frame $i - \* \$pc" \ + "\\^done,.*value=\"$hex.*" \ + "create varobj for \$pc in frame $i" + incr varnum + + mi_gdb_test "-var-create --thread 1 --frame $i - \* \"global_zero + \$pc\"" \ + "\\^done,.*value=\"$hex.*" \ + "create varobj for 'global_zero + \$pc' in frame $i" + incr varnum + } + + # Step one instruction to change the program counter. + mi_execute_to "exec-next-instruction" "end-stepping-range" \ + "callee3" ".*" ".*${srcfile}" ".*" "" \ + "next instruction in callee3" + + # Check that -var-update reports that the values are changed for + # varobj in frame 0. + for {set i 1} {$i < $first_unchanging_varnum} {incr i} { + mi_gdb_test "-var-update 1 var$i" \ + "\\^done,(changelist=\\\[\{name=\"var$i\"\[^\\\]\]+\\\])" \ + "varobj var$i has changed" + } + + # Check that -var-update reports that the values are unchanged for + # varobj in frames other than 0. + for {set i $first_unchanging_varnum} {$i < $varnum} {incr i} { + mi_gdb_test "-var-update 1 var$i" \ + "\\^done,(changelist=\\\[\\\])" \ + "varobj var$i has not changed" + } +} + +do_fixed_varobj_test +do_floating_varobj_test diff --git a/gdb/testsuite/gdb.mi/mi-var-create-rtti.exp b/gdb/testsuite/gdb.mi/mi-var-create-rtti.exp index b61c495ab41..9ea5784bcad 100644 --- a/gdb/testsuite/gdb.mi/mi-var-create-rtti.exp +++ b/gdb/testsuite/gdb.mi/mi-var-create-rtti.exp @@ -49,6 +49,9 @@ mi_gdb_test "-gdb-set print object on" ".*" # We use a explicit cast to (void *) as that is the # type that caused the bug this testcase is testing for. mi_gdb_test "-var-create sp1 * ((void*)\$sp)" \ - "\\^done,name=\"sp1\",numchild=\"0\",value=\"$hex\",type=\"void \\*\",has_more=\"0\"" \ + "\\^done,name=\"sp1\",numchild=\"0\",value=\"$hex\",type=\"void \\*\",thread-id=\"$decimal\",has_more=\"0\"" \ "-var-create sp1 * \$sp" +mi_gdb_test "-var-create sp2 @ ((void*)\$sp)" \ + "\\^done,name=\"sp2\",numchild=\"0\",value=\"$hex\",type=\"void \\*\",thread-id=\"$decimal\",has_more=\"0\"" \ + "-var-create sp2 @ \$sp" gdb_exit diff --git a/gdb/varobj.c b/gdb/varobj.c index 20dd09bd585..6c0145402ab 100644 --- a/gdb/varobj.c +++ b/gdb/varobj.c @@ -311,7 +311,8 @@ varobj_create (const char *objname, } p = expression; - innermost_block.reset (); + innermost_block.reset (INNERMOST_BLOCK_FOR_SYMBOLS + | INNERMOST_BLOCK_FOR_REGISTERS); /* Wrap the call to parse expression, so we can return a sensible error. */ TRY