From patchwork Fri Nov 17 00:47:58 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joel Brobecker X-Patchwork-Id: 24303 Received: (qmail 86686 invoked by alias); 17 Nov 2017 00:48:34 -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 86552 invoked by uid 89); 17 Nov 2017 00:48:15 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-25.2 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_SHORT, KAM_STOCKGEN, KB_WAM_FROM_NAME_SINGLEWORD, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.2 spammy=Enhance, value_lval, gdbada, UD:ada X-HELO: rock.gnat.com Received: from rock.gnat.com (HELO rock.gnat.com) (205.232.38.15) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Fri, 17 Nov 2017 00:48:03 +0000 Received: from localhost (localhost.localdomain [127.0.0.1]) by filtered-rock.gnat.com (Postfix) with ESMTP id A2465116328; Thu, 16 Nov 2017 19:48:01 -0500 (EST) Received: from rock.gnat.com ([127.0.0.1]) by localhost (rock.gnat.com [127.0.0.1]) (amavisd-new, port 10024) with LMTP id A95Iwmlc5v0w; Thu, 16 Nov 2017 19:48:01 -0500 (EST) Received: from tron.gnat.com (tron.gnat.com [IPv6:2620:20:4000:0:46a8:42ff:fe0e:e294]) by rock.gnat.com (Postfix) with ESMTP id 929AF116327; Thu, 16 Nov 2017 19:48:01 -0500 (EST) Received: by tron.gnat.com (Postfix, from userid 4233) id 8E8D3517; Thu, 16 Nov 2017 19:48:01 -0500 (EST) From: Joel Brobecker To: gdb-patches@sourceware.org Cc: Xavier Roirand , Pedro Alves Subject: [RFA/Ada] fix handling of minimal symbols (UNOP_CAST and UNOP_ADDR) Date: Thu, 16 Nov 2017 19:47:58 -0500 Message-Id: <1510879678-5150-1-git-send-email-brobecker@adacore.com> Hello, Consider a program which provides a symbol without debugging information. For instance, compiling the following code without -g: Some_Minimal_Symbol : Integer := 1234; pragma Export (C, Some_Minimal_Symbol, "some_minsym"); Trying to print this variable with GDB now causes an error, which is now expected: (gdb) p some_minsym 'some_minsym' has unknown type; cast it to its declared type However, trying to cast this symbol, or to take its address does not work: (gdb) p integer(some_minsym) 'some_minsym' has unknown type; cast it to its declared type (gdb) p &some_minsym 'some_minsym' has unknown type; cast it to its declared type Another manisfestation of this issue can be seen when trying to insert an Ada exception catchpoint for a specific standard exception (this only occurs if the Ada runtime is built without debugging information, which is the default). For instance: $ (gdb) catch exception constraint_error warning: failed to reevaluate internal exception condition for catchpoint 0: 'constraint_error' has unknown type; cast it to its declared type This is because, internally, the cachtpoint uses a condition referencing a minimal symbol, more precisely: long_integer (e) = long_integer (&constraint_error) This patch fixes all issues listed above: 1. resolve_subexp: Special-case the handling of OP_VAR_MSYM_VALUE expression elements, where there are no ambiguities to be resolved in that situation; 2. ada_evaluate_subexp: Enhance the handling of the UNOP_CAST handling so as to process the case where the target of the cast is a minimal symbol (as well as a symbol with debugging information). This mimicks what's done in C. gdb/ChangeLog: * ada-lang.c (resolve_subexp): Add handling of OP_VAR_MSYM_VALUE. (ada_evaluate_subexp_for_cast): New function. (ada_evaluate_subexp) : Replace code by call to ada_evaluate_subexp_for_cast. (ada_evaluate_subexp) : Replace code by call to eval_skip_value. * eval.c (evaluate_var_value): Make non-static. (evaluate_var_msym_value, eval_skip_value): Likewise. * value.h (evaluate_var_value, evaluate_var_msym_value) (eval_skip_value): Declare. gdb/testsuite/ChangeLog: * gdb.ada/minsyms: New testcase. Tested on x86_64-linux. No regression. Fixes the following failures: catch_ex.exp: continuing to Program_Error exception catch_ex.exp: continuing to failed assertion catch_ex.exp: continuing to unhandled exception catch_ex.exp: continuing to program completion complete.exp: p complete.exp: p Exported_Capitalized complete.exp: p exported_capitalized mi_catch_ex.exp: catch Program_Error (unexpected output) mi_catch_ex.exp: continue to exception catchpoint hit (unknown output after running) mi_catch_ex.exp: continue to assert failure catchpoint hit (unknown output after running) mi_catch_ex.exp: continue to unhandled exception catchpoint hit (unknown output after running) mi_ex_cond.exp: catch C_E if i = 2 (unexpected output) OK to push? Thank you! diff --git a/gdb/ada-lang.c b/gdb/ada-lang.c index f5dc530..3265c21 100644 --- a/gdb/ada-lang.c +++ b/gdb/ada-lang.c @@ -3603,7 +3603,12 @@ resolve_subexp (struct expression **expp, int *pos, int deprocedure_p, } *pos = pc; - return evaluate_subexp_type (exp, pos); + if (exp->elts[pc].opcode == OP_VAR_MSYM_VALUE) + return evaluate_var_msym_value (EVAL_AVOID_SIDE_EFFECTS, + exp->elts[pc + 1].objfile, + exp->elts[pc + 2].msymbol); + else + return evaluate_subexp_type (exp, pos); } /* Return non-zero if formal type FTYPE matches actual type ATYPE. If @@ -10313,6 +10318,58 @@ ada_value_cast (struct type *type, struct value *arg2) entity. Results in this case are unpredictable, as we usually read past the buffer containing the data =:-o. */ +/* Evaluate a subexpression of EXP, at index *POS, and return a value + for that subexpression cast to TO_TYPE. Advance *POS over the + subexpression. */ + +static value * +ada_evaluate_subexp_for_cast (expression *exp, int *pos, + enum noside noside, struct type *to_type) +{ + int pc = *pos; + + if (exp->elts[pc].opcode == OP_VAR_MSYM_VALUE + || exp->elts[pc].opcode == OP_VAR_VALUE) + { + (*pos) += 4; + + value *val; + if (exp->elts[pc].opcode == OP_VAR_MSYM_VALUE) + { + if (noside == EVAL_AVOID_SIDE_EFFECTS) + return value_zero (to_type, not_lval); + + val = evaluate_var_msym_value (noside, + exp->elts[pc + 1].objfile, + exp->elts[pc + 2].msymbol); + } + else + val = evaluate_var_value (noside, + exp->elts[pc + 1].block, + exp->elts[pc + 2].symbol); + + if (noside == EVAL_SKIP) + return eval_skip_value (exp); + + val = ada_value_cast (to_type, val); + + /* Follow the Ada language semantics that do not allow taking + an address of the result of a cast (view conversion in Ada). */ + if (VALUE_LVAL (val) == lval_memory) + { + if (value_lazy (val)) + value_fetch_lazy (val); + VALUE_LVAL (val) = not_lval; + } + return val; + } + + value *val = evaluate_subexp (to_type, exp, pos, noside); + if (noside == EVAL_SKIP) + return eval_skip_value (exp); + return ada_value_cast (to_type, val); +} + /* Implement the evaluate_exp routine in the exp_descriptor structure for the Ada language. */ @@ -10371,11 +10428,7 @@ ada_evaluate_subexp (struct type *expect_type, struct expression *exp, case UNOP_CAST: (*pos) += 2; type = exp->elts[pc + 1].type; - arg1 = evaluate_subexp (type, exp, pos, noside); - if (noside == EVAL_SKIP) - goto nosideret; - arg1 = ada_value_cast (type, arg1); - return arg1; + return ada_evaluate_subexp_for_cast (exp, pos, noside, type); case UNOP_QUAL: (*pos) += 2; @@ -11347,7 +11400,7 @@ ada_evaluate_subexp (struct type *expect_type, struct expression *exp, } nosideret: - return value_from_longest (builtin_type (exp->gdbarch)->builtin_int, 1); + return eval_skip_value (exp); } diff --git a/gdb/eval.c b/gdb/eval.c index 94ddfdb..14a3e05 100644 --- a/gdb/eval.c +++ b/gdb/eval.c @@ -705,7 +705,7 @@ fake_method::~fake_method () /* Helper for evaluating an OP_VAR_VALUE. */ -static value * +value * evaluate_var_value (enum noside noside, const block *blk, symbol *var) { /* JYG: We used to just return value_zero of the symbol type if @@ -738,7 +738,7 @@ evaluate_var_value (enum noside noside, const block *blk, symbol *var) /* Helper for evaluating an OP_VAR_MSYM_VALUE. */ -static value * +value * evaluate_var_msym_value (enum noside noside, struct objfile *objfile, minimal_symbol *msymbol) { @@ -757,7 +757,7 @@ evaluate_var_msym_value (enum noside noside, /* Helper for returning a value when handling EVAL_SKIP. */ -static value * +value * eval_skip_value (expression *exp) { return value_from_longest (builtin_type (exp->gdbarch)->builtin_int, 1); diff --git a/gdb/testsuite/gdb.ada/minsyms.exp b/gdb/testsuite/gdb.ada/minsyms.exp new file mode 100644 index 0000000..7d0c337 --- /dev/null +++ b/gdb/testsuite/gdb.ada/minsyms.exp @@ -0,0 +1,41 @@ +# Copyright 2017 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 . + +load_lib "ada.exp" + +standard_ada_testfile foo_qb07_057 + +if {[gdb_compile_ada "${srcfile}" "${binfile}" executable [list nodebug ]] != "" } { + return -1 +} + +clean_restart ${testfile} + +if ![runto "_ada_foo_qb07_057" ] then { + perror "Couldn't run ${testfile}" + return +} + +gdb_test "print some_minsym" \ + "'some_minsym' has unknown type; cast it to its declared type" + +gdb_test "print integer(some_minsym)" \ + " = 1234" + +gdb_test "print &some_minsym" \ + " = \\(access \\) 0x62c2f8 " + +gdb_test "print /x integer(&some_minsym)" \ + " = $hex" diff --git a/gdb/testsuite/gdb.ada/minsyms/foo_qb07_057.adb b/gdb/testsuite/gdb.ada/minsyms/foo_qb07_057.adb new file mode 100644 index 0000000..5a081dc --- /dev/null +++ b/gdb/testsuite/gdb.ada/minsyms/foo_qb07_057.adb @@ -0,0 +1,20 @@ +-- Copyright 2017 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 . + +with Pck; use Pck; +procedure Foo_QB07_057 is +begin + Increment (Some_Minimal_Symbol); +end Foo_QB07_057; diff --git a/gdb/testsuite/gdb.ada/minsyms/pck.adb b/gdb/testsuite/gdb.ada/minsyms/pck.adb new file mode 100644 index 0000000..10e8c9b --- /dev/null +++ b/gdb/testsuite/gdb.ada/minsyms/pck.adb @@ -0,0 +1,21 @@ +-- Copyright 2017 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 . + +package body Pck is + procedure Increment (I: in out Integer) is + begin + I := I + 1; + end Increment; +end Pck; diff --git a/gdb/testsuite/gdb.ada/minsyms/pck.ads b/gdb/testsuite/gdb.ada/minsyms/pck.ads new file mode 100644 index 0000000..4ad4c07 --- /dev/null +++ b/gdb/testsuite/gdb.ada/minsyms/pck.ads @@ -0,0 +1,21 @@ +-- Copyright 2017 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 . + +package Pck is + Some_Minimal_Symbol : Integer := 1234; + pragma Export (C, Some_Minimal_Symbol, "some_minsym"); + + procedure Increment (I: in out Integer); +end Pck; diff --git a/gdb/value.h b/gdb/value.h index b1b8c6d..2b6ae35 100644 --- a/gdb/value.h +++ b/gdb/value.h @@ -875,6 +875,15 @@ extern struct value *evaluate_subexp (struct type *expect_type, extern struct value *evaluate_subexpression_type (struct expression *exp, int subexp); +extern value *evaluate_var_value (enum noside noside, const block *blk, + symbol *var); + +extern value *evaluate_var_msym_value (enum noside noside, + struct objfile *objfile, + minimal_symbol *msymbol); + +extern value *eval_skip_value (expression *exp); + extern void fetch_subexp_value (struct expression *exp, int *pc, struct value **valp, struct value **resultp, struct value **val_chain,