From patchwork Wed Dec 14 10:28:32 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Burgess X-Patchwork-Id: 61923 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 8ED4C3872204 for ; Wed, 14 Dec 2022 10:29:38 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 8ED4C3872204 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1671013778; bh=0rdOBxZtL9kzQz+RY0SVGPhvgfQbz4Z5BBgzstZhlsA=; h=To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=KiTuS/8D/vIXnT2G6k0PwlL1kbpIBtjDXa/YPDBAh3mm7xjkWaQqiTz+4WNZ4w8sL GNlPBXf0ngDhRUq1YIxBH4359sdPFzAKMwZvwPujwMlNJuiPGAHK12sDDR2J/94P31 Sc11X3ndTMgjN6HuLtgruX+P33hKRhIBIsP3FSkY= X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 997F4383FF64 for ; Wed, 14 Dec 2022 10:28:42 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 997F4383FF64 Received: from mail-wm1-f72.google.com (mail-wm1-f72.google.com [209.85.128.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-378-GWJgf4yPMNu9rHEWurKTbQ-1; Wed, 14 Dec 2022 05:28:41 -0500 X-MC-Unique: GWJgf4yPMNu9rHEWurKTbQ-1 Received: by mail-wm1-f72.google.com with SMTP id c126-20020a1c3584000000b003cfffcf7c1aso7035408wma.0 for ; Wed, 14 Dec 2022 02:28:40 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=0rdOBxZtL9kzQz+RY0SVGPhvgfQbz4Z5BBgzstZhlsA=; b=N0s45h2kOtztDuWtV/2wWm5rXQpmYDultBgMWnbH61UYMe+I74LXroHzhGiqFKe8y2 iuj5idINAVVls98Jvpf8o1bP6aCYla7WWxFH9V55x8awdeYL99CWLv99ePah+pFm0+/A kFQg1zOO3NrpmDO+Hqsjzc0Qs4p2TqiH6JwimtkTKs93N0HWPHJGUzfhXHIghNZsK1zB IDveOM6sI0jixp3/FAlOMn3HGEM9+aciPpqZNz+9hOqyK8hz8BS96LPFH04hMgD2iCR4 NaEWEV6JonFSNJ4O0iuQAcjOu6c8Wn+b5qb40ZF1kgKAC87/RGxQY8ccBB8jmwbugigR hHCQ== X-Gm-Message-State: ANoB5plnKE7pgXYSfBJX4vL+9caYgIOSOYUz7zqWpFSjgb454pHbeV/b OSiDa+P4IlyOQh5rJXauIHxK9TNWglKbQ0nZMIObs2A89EhWzdsEDb5ycHVyQpDvEeuQlRmKdtt yR0y+M6ZwSVYndQ/2PjPl+LO4x1BeEmXM67L9CxKTnpvsajtncfMVpbSjIHKsw892rL1tapB49Q == X-Received: by 2002:a05:600c:3592:b0:3d0:1b21:597 with SMTP id p18-20020a05600c359200b003d01b210597mr18160065wmq.1.1671013719867; Wed, 14 Dec 2022 02:28:39 -0800 (PST) X-Google-Smtp-Source: AA0mqf4dO/xqOQVQkelMuPN246Zp0n/SHCAd+bqZTTlHKymljCBv7tZj6SFWe8F6xJWo/83+azqkVQ== X-Received: by 2002:a05:600c:3592:b0:3d0:1b21:597 with SMTP id p18-20020a05600c359200b003d01b210597mr18160053wmq.1.1671013719623; Wed, 14 Dec 2022 02:28:39 -0800 (PST) Received: from localhost ([31.111.84.238]) by smtp.gmail.com with ESMTPSA id c6-20020a05600c0a4600b003d1e3b1624dsm2402126wmq.2.2022.12.14.02.28.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Dec 2022 02:28:39 -0800 (PST) To: gdb-patches@sourceware.org Cc: Andrew Burgess Subject: [PATCH 1/2] gdb: use gdb_assert not internal_error Date: Wed, 14 Dec 2022 10:28:32 +0000 Message-Id: <7de91e1876385d0b1d025f3ef4c7cd10bc3f1a2e.1671013621.git.aburgess@redhat.com> X-Mailer: git-send-email 2.25.4 In-Reply-To: References: MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.7 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Andrew Burgess via Gdb-patches From: Andrew Burgess Reply-To: Andrew Burgess Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org Sender: "Gdb-patches" Spotted a couple of places in findvar.c where we use: if ( ! CONDITION ) internal_error ("..."); this commit changes these to be: gdb_assert ( CONDITION ); which I think is better. Unless we happen to hit the internal_error calls (which was bad) there should be no user visible changes after this commit. --- gdb/findvar.c | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/gdb/findvar.c b/gdb/findvar.c index 91de3fd5c3e..e609358df08 100644 --- a/gdb/findvar.c +++ b/gdb/findvar.c @@ -152,10 +152,7 @@ extract_long_unsigned_integer (const gdb_byte *addr, int orig_len, CORE_ADDR extract_typed_address (const gdb_byte *buf, struct type *type) { - if (!type->is_pointer_or_reference ()) - internal_error (_("extract_typed_address: " - "type is not a pointer or reference")); - + gdb_assert (type->is_pointer_or_reference ()); return gdbarch_pointer_to_address (type->arch (), type, buf); } @@ -204,10 +201,7 @@ template void store_integer (gdb_byte *addr, int len, void store_typed_address (gdb_byte *buf, struct type *type, CORE_ADDR addr) { - if (!type->is_pointer_or_reference ()) - internal_error (_("store_typed_address: " - "type is not a pointer or reference")); - + gdb_assert (type->is_pointer_or_reference ()); gdbarch_address_to_pointer (type->arch (), type, buf, addr); } From patchwork Wed Dec 14 10:28:33 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Burgess X-Patchwork-Id: 61922 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 5ACDA3840106 for ; Wed, 14 Dec 2022 10:29:10 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 5ACDA3840106 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1671013750; bh=YBjSfEWjnmBwpbgYD7IQWoc9xp2bWoI3IEmIqt3Jc5s=; h=To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=c6JfvKXDL29nrEDZfbCOc/I4XjxlkcOt93eydKG18Qm0SRwOSr0Bw2oWKlybClqp0 IBU07WDpdKKWiFJ+zST+I1oNxy3/6ntJTd3P+cq6mVaJGpZRAy9iFUk9dsz7nBE5Te Bm/YbY/NTIz+1hNWZsINJ6ozB8LDSzz+crDX1t00= X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 3372B3835563 for ; Wed, 14 Dec 2022 10:28:44 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 3372B3835563 Received: from mail-wm1-f70.google.com (mail-wm1-f70.google.com [209.85.128.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-454-t89Wt84mPtusxYYDX3MOhw-1; Wed, 14 Dec 2022 05:28:42 -0500 X-MC-Unique: t89Wt84mPtusxYYDX3MOhw-1 Received: by mail-wm1-f70.google.com with SMTP id o5-20020a05600c510500b003cfca1a327fso6215590wms.8 for ; Wed, 14 Dec 2022 02:28:42 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=YBjSfEWjnmBwpbgYD7IQWoc9xp2bWoI3IEmIqt3Jc5s=; b=cQIwrln4da/c1hxjPPJlXkpAAP/mrF8YQAfwXEVN3QPTT6MtsDloNO2pUXU3KHS0pq 4RDqm8OKF/d5qMh2cm0RXgUGC6CshoCYhwZkI6e72QjqPBkM6wSjwJm/2tK8+sFABGDR +tIyhDi9tl3BlFfAcLbrxKdvhnM+PXV8E/fZ4MX2/zZY5yvfvrKCc0YJrhtTk5DUwiiI 4r5fc32xx85RaaOZC4giEUqzx42unlqIfQ/RSH7FEO8sBmD0G+BmM5uE8NAF2zI2rm/+ 6culWdDjmumfIBvGcCq3jnA4dWIHEVRMoLbSXsrL+PL9ysXZYL0fh6Q7gkJMGReJUHip MyLg== X-Gm-Message-State: ANoB5pnqvHsuRtGxVDZBfZyT6FVybEGeKbhi/I8GbKQkLs3VGOGRrSPA kSx6EqSFcyQau8CVv9q9C56tu3CsJHfz/6wi5ZvvoQrvHhT++ftGAtanvG8t1QLBEJCA1AZGZJF +8IKWUgWOjLJ8b6ycRYTro/u+pls3s5BN+WW78uwkbNe33yV8nGWoKunZMYVQ1WSBmnonC52E8g == X-Received: by 2002:a05:6000:81b:b0:242:5a80:79b8 with SMTP id bt27-20020a056000081b00b002425a8079b8mr14254144wrb.20.1671013721467; Wed, 14 Dec 2022 02:28:41 -0800 (PST) X-Google-Smtp-Source: AA0mqf5v3tVjBpENQE1n+2A/W50NFXTu02wvxR9JwCNI/Iyuxj4/17Zf7t13l0lmwiSIrKZcQQCGnA== X-Received: by 2002:a05:6000:81b:b0:242:5a80:79b8 with SMTP id bt27-20020a056000081b00b002425a8079b8mr14254129wrb.20.1671013721081; Wed, 14 Dec 2022 02:28:41 -0800 (PST) Received: from localhost ([31.111.84.238]) by smtp.gmail.com with ESMTPSA id v26-20020a5d591a000000b0022cc0a2cbecsm2520565wrd.15.2022.12.14.02.28.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Dec 2022 02:28:40 -0800 (PST) To: gdb-patches@sourceware.org Cc: Andrew Burgess Subject: [PATCH 2/2] gdb: fix crash when getting the value of a label symbol Date: Wed, 14 Dec 2022 10:28:33 +0000 Message-Id: <9ec9a8db6a02169c4b18248f131715c54811550a.1671013621.git.aburgess@redhat.com> X-Mailer: git-send-email 2.25.4 In-Reply-To: References: MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-10.9 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, KAM_STOCKGEN, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Andrew Burgess via Gdb-patches From: Andrew Burgess Reply-To: Andrew Burgess Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org Sender: "Gdb-patches" When the source program contains a goto label, it turns out it's actually pretty hard for a user to find out more about that label. For example: (gdb) p some_label No symbol "some_label" in current context. (gdb) disassemble some_label No symbol "some_label" in current context. (gdb) x/10i some_label No symbol "some_label" in current context. (gdb) break some_label Breakpoint 2 at 0x401135: file /tmp/py-label-symbol-value.c, line 35. In all cases, some_label is a goto label within the current frame. Only placing a breakpoint on the label worked. This all seems a little strange to me, it feels like asking about a goto label would not be an unreasonable thing for a user to do. This commit doesn't fix any of the above issues, I mention them just to provide a little context for why the following issue has probably not been seen before. It turns out there is one way a user can access the symbol for a goto label, through the Python API: python frame = gdb.selected_frame() python frame_pc = frame.pc() python block = gdb.current_progspace().block_for_pc(frame_pc) python symbol,_ = gdb.lookup_symbol('some_label', block, gdb.SYMBOL_LABEL_DOMAIN) python print(str(symbol.value())) ../../src/gdb/findvar.c:204: internal-error: store_typed_address: Assertion `type->is_pointer_or_reference ()' failed. The problem is that label symbols are created using the builtin_core_addr type, which is a pure integer type. When GDB tries to fetch the value of a label symbol then we end up in findvar.c, in the function language_defn::read_var_value, in the LOC_LABEL case. From here store_typed_address is called to store the address of the label into a value object with builtin_core_addr type. The problem is that store_typed_address requires that the destination type be a pointer or reference, which the builtin_core_addr type is not. The fix I propose is to change the type used for the value of the label symbol, instead of using builtin_core_addr type, we could use builtin_func_ptr, after this calling store_typed_address is fine (the call to store_typed_address is done indirectly through value_from_pointer). After this asking for the value of a label symbol works just fine: (gdb) python print(str(symbol.value())) 0x401135 --- gdb/findvar.c | 8 ++-- .../gdb.python/py-label-symbol-value.c | 38 +++++++++++++++++++ .../gdb.python/py-label-symbol-value.exp | 38 +++++++++++++++++++ 3 files changed, 80 insertions(+), 4 deletions(-) create mode 100644 gdb/testsuite/gdb.python/py-label-symbol-value.c create mode 100644 gdb/testsuite/gdb.python/py-label-symbol-value.exp diff --git a/gdb/findvar.c b/gdb/findvar.c index e609358df08..cc4e14616c0 100644 --- a/gdb/findvar.c +++ b/gdb/findvar.c @@ -594,17 +594,17 @@ language_defn::read_var_value (struct symbol *var, case LOC_LABEL: /* Put the constant back in target format. */ - v = allocate_value (type); if (overlay_debugging) { struct objfile *var_objfile = var->objfile (); addr = symbol_overlayed_address (var->value_address (), var->obj_section (var_objfile)); - store_typed_address (value_contents_raw (v).data (), type, addr); } else - store_typed_address (value_contents_raw (v).data (), type, - var->value_address ()); + addr = var->value_address (); + + type = builtin_type (var->arch ())->builtin_func_ptr; + v = value_from_pointer (type, addr); VALUE_LVAL (v) = not_lval; return v; diff --git a/gdb/testsuite/gdb.python/py-label-symbol-value.c b/gdb/testsuite/gdb.python/py-label-symbol-value.c new file mode 100644 index 00000000000..94bdae6fd30 --- /dev/null +++ b/gdb/testsuite/gdb.python/py-label-symbol-value.c @@ -0,0 +1,38 @@ +/* This testcase is part of GDB, the GNU debugger. + + Copyright 2022 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 . */ + +volatile int global_var = 1; + +int +get_value () +{ + return global_var; +} + +int +main (void) +{ + int value = get_value (); + if (value > 0) + goto some_label; + + return 1; + + some_label: + + return 0; +} diff --git a/gdb/testsuite/gdb.python/py-label-symbol-value.exp b/gdb/testsuite/gdb.python/py-label-symbol-value.exp new file mode 100644 index 00000000000..ccdd4b239ac --- /dev/null +++ b/gdb/testsuite/gdb.python/py-label-symbol-value.exp @@ -0,0 +1,38 @@ +# Copyright 2022 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 . + +# Check that GDB handles the user asking for the value of a label +# symbol (i.e. a symbol for a goto label). + +load_lib gdb-python.exp +standard_testfile + +if { [prepare_for_testing "failed to prepare" ${testfile} ${srcfile}] } { + return -1 +} + +# Skip all tests if Python scripting is not enabled. +if { [skip_python_tests] } { continue } + +if ![runto_main] { + return -1 +} + +# Use Python to print the value of the 'some_label' symbol. +gdb_test "python frame = gdb.selected_frame()" +gdb_test "python frame_pc = frame.pc()" +gdb_test "python block = gdb.current_progspace().block_for_pc(frame_pc)" +gdb_test "python symbol,_ = gdb.lookup_symbol('some_label', block, gdb.SYMBOL_LABEL_DOMAIN)" +gdb_test "python print(str(symbol.value()))" "$hex "