From patchwork Thu Jan 15 09:59:55 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joel Brobecker X-Patchwork-Id: 4697 Received: (qmail 23665 invoked by alias); 15 Jan 2015 10:00:14 -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 23262 invoked by uid 89); 15 Jan 2015 10:00:10 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.7 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; Thu, 15 Jan 2015 10:00:06 +0000 Received: from localhost (localhost.localdomain [127.0.0.1]) by filtered-rock.gnat.com (Postfix) with ESMTP id 18E9A1164B8 for ; Thu, 15 Jan 2015 05:00:05 -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 7dbsm1EV8bhR for ; Thu, 15 Jan 2015 05:00:05 -0500 (EST) Received: from joel.gnat.com (localhost.localdomain [127.0.0.1]) by rock.gnat.com (Postfix) with ESMTP id 8166D1163F2 for ; Thu, 15 Jan 2015 05:00:03 -0500 (EST) Received: by joel.gnat.com (Postfix, from userid 1000) id D778348E8D; Thu, 15 Jan 2015 14:00:00 +0400 (RET) From: Joel Brobecker To: gdb-patches@sourceware.org Subject: [PATCH 2/3] [Ada/varobj] number of children of null pointer to dynamic array. Date: Thu, 15 Jan 2015 13:59:55 +0400 Message-Id: <1421315996-22616-3-git-send-email-brobecker@adacore.com> In-Reply-To: <1421315996-22616-1-git-send-email-brobecker@adacore.com> References: <1421315996-22616-1-git-send-email-brobecker@adacore.com> This is preparation work to avoid a regression in the Ada/varobj. An upcoming patch is going to add support for types in DWARF which have dynamic properties whose value is a reference to another DIE. Consider for instance the following declaration: type Variant_Type (N : Int := 0) is record F : String(1 .. N) := (others => 'x'); end record; type Variant_Type_Access is access all Variant_Type; VTA : Variant_Type_Access := null; This declares a variable "VTA" which is an access (=pointer) to a variant record Variant_Type. This record contains two components, the first being "N" (the discriminant), and the second being "F", an array whose lower bound is 1, and whose upper bound depends on the value of "N" (the discriminant). Of interest to us, here, is that second component ("F"), and in particular its bounds. The debugging info, and in particular the info for the array looks like the following... .uleb128 0x9 # (DIE (0x91) DW_TAG_array_type) .long .LASF16 # DW_AT_name: "bar__variant_type__T2b" .long 0xac # DW_AT_GNAT_descriptive_type .long 0x2cb # DW_AT_type .long 0xac # DW_AT_sibling .uleb128 0xa # (DIE (0xa2) DW_TAG_subrange_type) .long 0xc4 # DW_AT_type .long 0x87 # DW_AT_upper_bound .byte 0 # end of children of DIE 0x91 ... where the upper bound of the array's subrange type is a reference to "n"'s DIE (0x87): .uleb128 0x8 # (DIE (0x87) DW_TAG_member) .ascii "n\0" # DW_AT_name [...] Once the patch to handle this dynamic property gets applied, this is what happens when creating a varobj for variable "VTA" (whose value is null), and then trying to list its children: (gdb) -var-create vta * vta ^done,name="vta",numchild="2",value="0x0", type="bar.variant_type_access",has_more="0" (gdb) -var-list-children 1 vta ^done,numchild="2", children=[child={name="vta.n",[...]}, child={name="vta.f",exp="f", numchild="43877616", <<<<----- value="[43877616]", <<<<----- type="array (1 .. n) of character"}], has_more="0" It has an odd number of children. In this case, we cannot really determine the number of children, since that number depends on the value of a field in a record for which we do not have a value. Up to now, the value we've been displaying is zero - meaning we have an empty array. What happens in this case, is that, because the VTA is a null pointer, we're not able to resolve the pointer's target type, and therefore end up asking ada_varobj_get_array_number_of_children to return the number of elements in that array; for that, it relies blindly on get_array_bounds, which assumes the type is no longer dynamic, and therefore the reads the bound without seeing that it's value is actually a reference rather than a resolved constant. This patch prevents the issue by explicitly handling the case of dynamic arrays, and returning zero child in that case. gdb/ChangeLog: * ada-varobj.c (ada_varobj_get_array_number_of_children): Return zero if PARENT_VALUE is NULL and parent_type's range type is dynamic. gdb/testsuite/ChangeLog: * gdb.ada/mi_var_array: New testcase. Tested on x86_64-linux. I'll push in a few days unless there are comments... Thanks, --- Joel --- gdb/ada-varobj.c | 12 +++++++ gdb/testsuite/gdb.ada/mi_var_array.exp | 52 ++++++++++++++++++++++++++++++ gdb/testsuite/gdb.ada/mi_var_array/bar.adb | 29 +++++++++++++++++ gdb/testsuite/gdb.ada/mi_var_array/pck.adb | 21 ++++++++++++ gdb/testsuite/gdb.ada/mi_var_array/pck.ads | 19 +++++++++++ 5 files changed, 133 insertions(+) create mode 100644 gdb/testsuite/gdb.ada/mi_var_array.exp create mode 100644 gdb/testsuite/gdb.ada/mi_var_array/bar.adb create mode 100644 gdb/testsuite/gdb.ada/mi_var_array/pck.adb create mode 100644 gdb/testsuite/gdb.ada/mi_var_array/pck.ads diff --git a/gdb/ada-varobj.c b/gdb/ada-varobj.c index 25b1a38..690ee49 100644 --- a/gdb/ada-varobj.c +++ b/gdb/ada-varobj.c @@ -240,6 +240,18 @@ ada_varobj_get_array_number_of_children (struct value *parent_value, { LONGEST lo, hi; + if (parent_value == NULL + && is_dynamic_type (TYPE_INDEX_TYPE (parent_type))) + { + /* This happens when listing the children of an object + which does not exist in memory (Eg: when requesting + the children of a null pointer, which is allowed by + varobj). The array index type being dynamic, we cannot + determine how many elements this array has. Just assume + it has none. */ + return 0; + } + if (!get_array_bounds (parent_type, &lo, &hi)) { /* Could not get the array bounds. Pretend this is an empty array. */ diff --git a/gdb/testsuite/gdb.ada/mi_var_array.exp b/gdb/testsuite/gdb.ada/mi_var_array.exp new file mode 100644 index 0000000..836e5fe --- /dev/null +++ b/gdb/testsuite/gdb.ada/mi_var_array.exp @@ -0,0 +1,52 @@ +# Copyright 2015 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 bar + +if {[gdb_compile_ada "${srcfile}" "${binfile}" executable [list debug]] != "" } { + return -1 +} + +load_lib mi-support.exp +set MIFLAGS "-i=mi" + +gdb_exit +if [mi_gdb_start] { + continue +} + +mi_delete_breakpoints +mi_gdb_reinitialize_dir $srcdir/$subdir +mi_gdb_load ${binfile} + +if ![mi_run_to_main] then { + fail "Cannot run to main, testcase aborted" + return 0 +} + +set bp_location [gdb_get_line_number "STOP" ${testdir}/bar.adb] +mi_continue_to_line \ + "bar.adb:$bp_location" \ + "stop at start of main Ada procedure" + +mi_gdb_test "-var-create vta * vta" \ + "\\^done,name=\"vta\",numchild=\"2\",.*" \ + "Create bt varobj" + +mi_gdb_test "-var-list-children vta" \ + "\\^done,numchild=\"2\",children=\\\[child={name=\"vta.n\",exp=\"n\",numchild=\"0\",type=\"bar\\.int\"},child={name=\"vta.f\",exp=\"f\",numchild=\"0\",type=\"array \\(1 .. n\\) of character\"}\\\],.*" \ + "list vta's children" diff --git a/gdb/testsuite/gdb.ada/mi_var_array/bar.adb b/gdb/testsuite/gdb.ada/mi_var_array/bar.adb new file mode 100644 index 0000000..b39d97c --- /dev/null +++ b/gdb/testsuite/gdb.ada/mi_var_array/bar.adb @@ -0,0 +1,29 @@ +-- Copyright 2015 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 Bar is + M : Integer := 35; + subtype Int is Integer range 0 .. M; + type Variant_Type (N : Int := 0) is record + F : String(1 .. N) := (others => 'x'); + end record; + type Variant_Type_Access is access all Variant_Type; + + VTA : Variant_Type_Access; +begin + Do_Nothing (VTA'Address); -- STOP +end Bar; diff --git a/gdb/testsuite/gdb.ada/mi_var_array/pck.adb b/gdb/testsuite/gdb.ada/mi_var_array/pck.adb new file mode 100644 index 0000000..3f490ee --- /dev/null +++ b/gdb/testsuite/gdb.ada/mi_var_array/pck.adb @@ -0,0 +1,21 @@ +-- Copyright 2015 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 Do_Nothing (A : System.Address) is + begin + null; + end Do_Nothing; +end Pck; diff --git a/gdb/testsuite/gdb.ada/mi_var_array/pck.ads b/gdb/testsuite/gdb.ada/mi_var_array/pck.ads new file mode 100644 index 0000000..0ee9ef9 --- /dev/null +++ b/gdb/testsuite/gdb.ada/mi_var_array/pck.ads @@ -0,0 +1,19 @@ +-- Copyright 2015 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 System; +package Pck is + procedure Do_Nothing (A : System.Address); +end Pck;