From patchwork Mon Nov 9 17:59:29 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joel Brobecker X-Patchwork-Id: 9614 Received: (qmail 77840 invoked by alias); 9 Nov 2015 17:59:38 -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 77735 invoked by uid 89); 9 Nov 2015 17:59:37 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-0.9 required=5.0 tests=AWL, BAYES_50, RCVD_IN_DNSWL_LOW, RP_MATCHES_RCVD, SPF_PASS 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; Mon, 09 Nov 2015 17:59:35 +0000 Received: from localhost (localhost.localdomain [127.0.0.1]) by filtered-rock.gnat.com (Postfix) with ESMTP id D27CE11631E for ; Mon, 9 Nov 2015 12:59:32 -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 SaOiDT4ILRwm for ; Mon, 9 Nov 2015 12:59:32 -0500 (EST) Received: from joel.gnat.com (localhost.localdomain [127.0.0.1]) by rock.gnat.com (Postfix) with ESMTP id 9A60E1162AF for ; Mon, 9 Nov 2015 12:59:32 -0500 (EST) Received: by joel.gnat.com (Postfix, from userid 1000) id F0E32405BA; Mon, 9 Nov 2015 09:59:30 -0800 (PST) From: Joel Brobecker To: gdb-patches@sourceware.org Subject: [commit/Ada] GDB crash during "finish" of function with out parameters Date: Mon, 9 Nov 2015 09:59:29 -0800 Message-Id: <1447091969-19644-1-git-send-email-brobecker@adacore.com> Hello, Consider a function with the following signature... function F (R : out Rec_Type) return Enum_Type; ... where Rec_Type is a simple record: type Rec_Type is record Cur : Integer; end record; Trying to "finish" from that function causes GDB to SEGV: (gdb) fin Run till exit from #0 bar.f (r=...) at bar.adb:5 0x00000000004022fe in foo () at foo.adb:5 5 I : Enum_Type := F (R); [1] 18949 segmentation fault (core dumped) /[..]/gdb This is related to the fact that funtion F has a parameter (R) which is an "out" parameter being passed by copy. For those, GNAT transforms the return value to be a record with multiple fields: The first one is called "RETVAL" and contains the return value shown in the source, and the remaining fields have the same name as the "out" or "in out" parameters which are passed by copy. So, in the example above, function F returns a struct that has one field who name is "r". Because "RETVAL" starts with "R", GDB thinks it's a wrapper field, because it looks like the encoding used for variant records: -- member_name ::= {choice} | others_choice -- choice ::= simple_choice | range_choice -- simple_choice ::= S number -- range_choice ::= R number T number <<<<<----- here -- number ::= {decimal_digit} [m] -- others_choice ::= O (upper case letter O) See ada_is_wrapper_field: return (name != NULL && (startswith (name, "PARENT") || strcmp (name, "REP") == 0 || startswith (name, "_parent") || name[0] == 'S' || name[0] == 'R' || name[0] == 'O')); As a result of this, when trying to print the RETURN value, we think that RETVAL is a wrapper, and thus recurse into print_field_values... if (ada_is_wrapper_field (type, i)) { comma_needed = print_field_values (TYPE_FIELD_TYPE (type, i), valaddr, (offset + TYPE_FIELD_BITPOS (type, i) / HOST_CHAR_BIT), stream, recurse, val, options, comma_needed, type, offset, language); ... which is a problem since print_field_values assumes that the type it is given ("TYPE_FIELD_TYPE (type, i)" here), is also a record type. However, that's not the case, since RETVAL is an enum. That eventually leads GDB to a NULL type when trying to extract fields out of the enum, which then leads to a SEGV when trying to dereference it. Ideally, we'd want to be a little more careful in identifying wrapper fields, by enhancing ada_is_wrapper_field to be a little more complete in its analysis of the field name before declaring it a variant record wrapper. However, it's not super easy to do so, considering that the choices can be combined together when complex choices are used. Eg: -- [...] the choice 1 .. 4 | 7 | -10 would be represented by -- R1T4S7S10m Given that we are working towards getting rid of GNAT encodings, which means that the above will eventually disappear, we took the more pragmatic approach is just treating RETVAL as a special case. gdb/ChangeLog: * ada-lang.c (ada_is_wrapper_field): Add special handling for fields called "RETVAL". gdb/testsuite/ChangeLog: * gdb.ada/fin_fun_out: New testcase. Tested on x86_64-linux, and pushed. Thanks, diff --git a/gdb/ChangeLog b/gdb/ChangeLog index e198c93..beb604f 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,3 +1,8 @@ +2015-11-09 Joel Brobecker + + * ada-lang.c (ada_is_wrapper_field): Add special handling + for fields called "RETVAL". + 2015-11-09 Yao Qi * arm-tdep.c (arm_exidx_new_objfile): Use diff --git a/gdb/ada-lang.c b/gdb/ada-lang.c index fff4862..1f2d014 100644 --- a/gdb/ada-lang.c +++ b/gdb/ada-lang.c @@ -6984,6 +6984,17 @@ ada_is_wrapper_field (struct type *type, int field_num) { const char *name = TYPE_FIELD_NAME (type, field_num); + if (name != NULL && strcmp (name, "RETVAL") == 0) + { + /* This happens in functions with "out" or "in out" parameters + which are passed by copy. For such functions, GNAT describes + the function's return type as being a struct where the return + value is in a field called RETVAL, and where the other "out" + or "in out" parameters are fields of that struct. This is not + a wrapper. */ + return 0; + } + return (name != NULL && (startswith (name, "PARENT") || strcmp (name, "REP") == 0 diff --git a/gdb/testsuite/ChangeLog b/gdb/testsuite/ChangeLog index 31a59f4..dfc9b77 100644 --- a/gdb/testsuite/ChangeLog +++ b/gdb/testsuite/ChangeLog @@ -1,3 +1,7 @@ +2015-11-09 Joel Brobecker + + * gdb.ada/fin_fun_out: New testcase. + 2015-11-07 Kevin Buettner * gdb.dwarf2/data-loc.exp (Dwarf::assemble): Don't hardcode diff --git a/gdb/testsuite/gdb.ada/fin_fun_out.exp b/gdb/testsuite/gdb.ada/fin_fun_out.exp new file mode 100644 index 0000000..e989d87 --- /dev/null +++ b/gdb/testsuite/gdb.ada/fin_fun_out.exp @@ -0,0 +1,38 @@ +# 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 foo_o525_013 + +if {[gdb_compile_ada "${srcfile}" "${binfile}" executable [list debug ]] != "" } { + return -1 +} + +clean_restart ${testfile} + +runto "bar.f" + +# Perform a "finish". The output, and in particular the value of +# the return value depends on the target, as sometime the compiler +# will transform it into a struct, which we may or may not be able +# to display, depending on the ABI. The objective of the test is +# to verify that we don't crash, so keep the expected output simple... +gdb_test "finish" \ + ".*Value returned.*" + +# Verify that GDB is still alive... +gdb_test "print 1" \ + "= 1" diff --git a/gdb/testsuite/gdb.ada/fin_fun_out/bar.adb b/gdb/testsuite/gdb.ada/fin_fun_out/bar.adb new file mode 100644 index 0000000..1b74f13 --- /dev/null +++ b/gdb/testsuite/gdb.ada/fin_fun_out/bar.adb @@ -0,0 +1,23 @@ +-- 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 Bar is + function F (R : out Rec_Type) return Enum_Type + is + begin + R.Cur := 0; + return A; + end F; +end Bar; diff --git a/gdb/testsuite/gdb.ada/fin_fun_out/bar.ads b/gdb/testsuite/gdb.ada/fin_fun_out/bar.ads new file mode 100644 index 0000000..cbd6504 --- /dev/null +++ b/gdb/testsuite/gdb.ada/fin_fun_out/bar.ads @@ -0,0 +1,25 @@ +-- 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 Bar is + type Enum_Type is (A, B, C); + + type Rec_Type is record + Cur : Integer; + end record; + + function F (R : out Rec_Type) return Enum_Type; + +end Bar; diff --git a/gdb/testsuite/gdb.ada/fin_fun_out/foo_o525_013.adb b/gdb/testsuite/gdb.ada/fin_fun_out/foo_o525_013.adb new file mode 100644 index 0000000..46f89ec --- /dev/null +++ b/gdb/testsuite/gdb.ada/fin_fun_out/foo_o525_013.adb @@ -0,0 +1,23 @@ +-- 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 Bar; use Bar; + +procedure Foo_O525_013 is + R : Rec_Type; + I : Enum_Type := F (R); +begin + null; +end Foo_O525_013;