From patchwork Tue Apr 21 15:44:22 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joel Brobecker X-Patchwork-Id: 6344 Received: (qmail 41869 invoked by alias); 21 Apr 2015 15:44:39 -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 41818 invoked by uid 89); 21 Apr 2015 15:44:39 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.8 required=5.0 tests=AWL, BAYES_00, SPF_PASS, T_RP_MATCHES_RCVD autolearn=ham version=3.3.2 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 (AES256-SHA encrypted) ESMTPS; Tue, 21 Apr 2015 15:44:37 +0000 Received: from localhost (localhost.localdomain [127.0.0.1]) by filtered-rock.gnat.com (Postfix) with ESMTP id B6C4011653D for ; Tue, 21 Apr 2015 11:44:35 -0400 (EDT) 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 bQ0STSrJv3yW for ; Tue, 21 Apr 2015 11:44:35 -0400 (EDT) Received: from joel.gnat.com (localhost.localdomain [127.0.0.1]) by rock.gnat.com (Postfix) with ESMTP id 8666A11648E for ; Tue, 21 Apr 2015 11:44:35 -0400 (EDT) Received: by joel.gnat.com (Postfix, from userid 1000) id 88EEC40EAD; Tue, 21 Apr 2015 08:44:35 -0700 (PDT) From: Joel Brobecker To: gdb-patches@sourceware.org Subject: [FYI 4/6] [Ada] array of variant record subscripting Date: Tue, 21 Apr 2015 08:44:22 -0700 Message-Id: <1429631064-4196-5-git-send-email-brobecker@adacore.com> In-Reply-To: <1429631064-4196-1-git-send-email-brobecker@adacore.com> References: <1429631064-4196-1-git-send-email-brobecker@adacore.com> Consider the following (Ada) array... A1 : Array_Type := (1 => (I => 0, S => <>), 2 => (I => 1, S => "A"), 3 => (I => 2, S => "AB")); ... where Array_Type is declared as follow: subtype Small_Type is Integer range 0 .. 10; type Record_Type (I : Small_Type := 0) is record S : String (1 .. I); end record; type Array_Type is array (Integer range <>) of Record_Type; Trying to print the value of each element individually does not always work. Printing the value of the first one does: (gdb) p a1(1) $1 = (i => 0, s => "") But printing the value of the subsequent ones often does not. For instance: (gdb) p a1(2) $2 = (i => 1, s => "") <<<--- s should be "A" (gdb) p a1(3) $3 = (i => 2, s => "") <<<--- s should be "AB" I traced the problem to ada_value_primitive_packed_val, which is trying to perform the array subscripting by extracting the value of the corresponding array element into a buffer where the contents is now byte-aligned. The element type that ada_value_primitive_packed_val gets passed is a dynamic type. As it happens, that dynamic type can get resolved thanks to: v = value_at (type, value_address (obj)); type = value_type (v); However, obj represents the array, so the address given in the call to value_at represents the value of the first element. As a result, the solution of component S's upper bound always gets resolved based on the value of component I in the first element of the array, whose value is 0, thus leading to GDB mistakely resolving the element type where S's upper bound is always 0. The proper fix would be to systematically resolve the element type first. But, this requires us to extract-and-realign the element's value so as to be able to pass it as "valaddr" to resolve_dynamic_type. In the meantime, it's easy to make the situation a little better by passing "value_address (obj) + offset" as the object address. This only works when BIT_OFFSET is nul, but that should be the case when the element type is anything but a scalar, which seems to be the only situation where it seems important to resolve the type now. And we're not that worse off otherwise. But we'll try to find a better solution in a separate patch. gdb/ChangeLog: * ada-lang.c (ada_value_primitive_packed_val): Use a more correct address in call to value_at. Adjust call to value_address accordingly. --- gdb/ada-lang.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/gdb/ada-lang.c b/gdb/ada-lang.c index 9926cfb..d71a243 100644 --- a/gdb/ada-lang.c +++ b/gdb/ada-lang.c @@ -2417,10 +2417,10 @@ ada_value_primitive_packed_val (struct value *obj, const gdb_byte *valaddr, } else if (VALUE_LVAL (obj) == lval_memory && value_lazy (obj)) { - v = value_at (type, value_address (obj)); + v = value_at (type, value_address (obj) + offset); type = value_type (v); bytes = (unsigned char *) alloca (len); - read_memory (value_address (v) + offset, bytes, len); + read_memory (value_address (v), bytes, len); } else {