From patchwork Tue Feb 13 19:32:03 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 85679 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 8FA843857C52 for ; Tue, 13 Feb 2024 19:32:41 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mail-io1-xd30.google.com (mail-io1-xd30.google.com [IPv6:2607:f8b0:4864:20::d30]) by sourceware.org (Postfix) with ESMTPS id CEAC83858C5F for ; Tue, 13 Feb 2024 19:32:14 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org CEAC83858C5F Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=adacore.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=adacore.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org CEAC83858C5F Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::d30 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1707852736; cv=none; b=ejhrDxdcGe48hf5RfppnfkevH2nIzMIg4D4LRIQTxFgGAC/BwYRMjG9QZvW72l7+QcOQv4PAG27/rTMBJpVXjzgmMGkbFe8oNnNmbAR3/etV0feXBXmEH0xZrw8DmV8y0/6Bpb35uNMnZwi5Du818xCkKqdXBof2qospLYR24aQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1707852736; c=relaxed/simple; bh=ysaDhObRmrsrSWPTcQkHHMSCdnz5WN/6IeCLfeqiXBY=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=br9O6M6YqYcd4k6DSVJiDU3kE7pb+da5NaUWXZ3id78Wv4GxFYZ2hqjJ4z17Dfbn7+LKyphoUZJMxHfz8Y8eYubcMMhbI1cjz+6MPdkRGYBtCHQN91Iw8eDDFT1NG24qnIQTbZ9b665Jc9OgXL2fBee6FTMJfDfPbicqdn8Zy4I= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-io1-xd30.google.com with SMTP id ca18e2360f4ac-7c4359c5d33so190956939f.1 for ; Tue, 13 Feb 2024 11:32:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=adacore.com; s=google; t=1707852734; x=1708457534; darn=sourceware.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=ukM4njgtPeFwuM5N6eJyT4fv9XnY4nMszf93wafo7OM=; b=IHu/cLLZOUzfljDmmgB6Qsj2WoxIdApF0qUjGO53xA8tR6J6EAcovsOLsDtA4bBJ9n H/Y005RN3plKfDXNg9irlbXquEW3zQlZxdKnOCvnZ4zhIoWgpgUWYvPwS1fH8AKClS9p MLV8U+Gi9aea3P2Uh8j0lLMhZJPfbF7v/EzZkL7A2A5lgf+Op520nhQYzXxVduly70Kd lUMJ7DS69Mc51SELt04n+uKl6Haw3rFGnVeUiISpl9HLx4JbYXQM4aX51ZeP3Zzg3Wqb B/WtTI8D26lb2dpSkPkA44TNngJdU9wVWYPb9d3kyBPbH15Yy3jBIKosk6A70nWHgBAH dvHg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707852734; x=1708457534; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=ukM4njgtPeFwuM5N6eJyT4fv9XnY4nMszf93wafo7OM=; b=fecKoO9rB9hZ7Nxpyk6nJoCN++R3kKsnjxYGXv/qJhKt8+S5HqquwObmmhaihxYt2C oq706KlrF70Xglxbp7y5BjPs8pTAj8yLPYxN+3Qxkuw/6Y/hsWdRXV9f6a77o+lkemv8 eGjSdDANyOOVAqxQZwZzwVyUhT+kUQ5G6lNaEorqc6ZjepX7HZbX9Vxcd92VEecEAJwd CceXAFNnDbH0q77am/O6AL1vX50lf+iloUwtYKUEkF9sETu9mERh+ZCCYGugwciLX2GS zq+O15E5PiUe2i9L86/qI5mWF1WIS6Dru89J483ks2zPJGAajMvve9OJ0NX6mMk+zeQv ABUg== X-Gm-Message-State: AOJu0YxmjlKOeqWF5MeHuR+tKoMOCgXhMfvtL6TJDzriHpr1AwHF//8k 10Zjnk0SeB0KWa/jJXbNVsFs4Gys0e9b+CmKEGVXpa5tUnKJFpuooJHvHh66rxosXrYHsTgFw5g = X-Google-Smtp-Source: AGHT+IFvTYtWdQMv2IOBzExJEtcyEg/pNqu3I42tnKjgPT/EaGy2ZhMN4Ndjcbi5lSG3fRFj8jls+A== X-Received: by 2002:a5e:a70c:0:b0:7c4:1df1:14d7 with SMTP id b12-20020a5ea70c000000b007c41df114d7mr813874iod.6.1707852734019; Tue, 13 Feb 2024 11:32:14 -0800 (PST) Received: from localhost.localdomain (71-211-170-195.hlrn.qwest.net. [71.211.170.195]) by smtp.gmail.com with ESMTPSA id z68-20020a6bc947000000b007c450e8eef6sm2209577iof.3.2024.02.13.11.32.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 13 Feb 2024 11:32:13 -0800 (PST) From: Tom Tromey To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [PATCH] Special case NULL pointers in dynamic type resolution Date: Tue, 13 Feb 2024 12:32:03 -0700 Message-ID: <20240213193203.2239399-1-tromey@adacore.com> X-Mailer: git-send-email 2.43.0 MIME-Version: 1.0 X-Spam-Status: No, score=-11.2 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE 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.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org commit f18fc7e5 ("gdb, types: Resolve pointer types dynamically") caused a regression on a test case in the AdaCore internal test suite. The issue here is that gdb would try to resolve the type of a dynamic pointer that happened to be NULL. In this case, the "Location address is not set." error would end up being thrown from the DWARF expression evaluator. I think it makes more sense to special-case NULL pointers and not try to resolve their target type, as that type can't really be accessed anyway. This patch implements this idea, and also adds the missing Ada test case. --- gdb/gdbtypes.c | 13 +++++-- gdb/testsuite/gdb.ada/recursive-variant.exp | 31 +++++++++++++++ .../gdb.ada/recursive-variant/main.adb | 38 +++++++++++++++++++ gdb/testsuite/gdb.cp/vla-cxx.exp | 4 +- 4 files changed, 80 insertions(+), 6 deletions(-) create mode 100644 gdb/testsuite/gdb.ada/recursive-variant.exp create mode 100644 gdb/testsuite/gdb.ada/recursive-variant/main.adb diff --git a/gdb/gdbtypes.c b/gdb/gdbtypes.c index dcd7321d979..c46bf3ae39e 100644 --- a/gdb/gdbtypes.c +++ b/gdb/gdbtypes.c @@ -2809,10 +2809,15 @@ resolve_dynamic_type_internal (struct type *type, pinfo.addr = read_memory_typed_address (addr_stack->addr, type); pinfo.next = addr_stack; - resolved_type = copy_type (type); - resolved_type->set_target_type - (resolve_dynamic_type_internal (type->target_type (), - &pinfo, frame, top_level)); + /* Special case a NULL pointer here -- we don't want to + dereference it. */ + if (pinfo.addr != 0) + { + resolved_type = copy_type (type); + resolved_type->set_target_type + (resolve_dynamic_type_internal (type->target_type (), + &pinfo, frame, true)); + } break; } diff --git a/gdb/testsuite/gdb.ada/recursive-variant.exp b/gdb/testsuite/gdb.ada/recursive-variant.exp new file mode 100644 index 00000000000..ea13f83f2d5 --- /dev/null +++ b/gdb/testsuite/gdb.ada/recursive-variant.exp @@ -0,0 +1,31 @@ +# Copyright 2024 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" + +require allow_ada_tests + +standard_ada_testfile main + +if {[gdb_compile_ada "${srcfile}" "${binfile}" executable debug] != ""} { + return -1 +} + +clean_restart ${testfile} + +set bp_location [gdb_get_line_number "STOP" ${testdir}/main.adb] +runto "main.adb:$bp_location" + +gdb_test "print *instance" [string_to_regexp "= (x => false, link => 0x0)"] diff --git a/gdb/testsuite/gdb.ada/recursive-variant/main.adb b/gdb/testsuite/gdb.ada/recursive-variant/main.adb new file mode 100644 index 00000000000..93b47e5002a --- /dev/null +++ b/gdb/testsuite/gdb.ada/recursive-variant/main.adb @@ -0,0 +1,38 @@ +-- Copyright 2024 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 . + +procedure Main is + + type Rec_Type; + + type Rec_Type_Ref is access all Rec_Type; + + type Rec_Type (X : Boolean) is record + Link : Rec_Type_Ref; + case X is + when True => + Value : Integer; + when False => + null; + end case; + end record; + + Instance : Rec_Type_Ref := new Rec_Type'(X => False, Link => null); + +begin + + null; -- STOP + +end Main; diff --git a/gdb/testsuite/gdb.cp/vla-cxx.exp b/gdb/testsuite/gdb.cp/vla-cxx.exp index 0033a968268..bf3ca792d58 100644 --- a/gdb/testsuite/gdb.cp/vla-cxx.exp +++ b/gdb/testsuite/gdb.cp/vla-cxx.exp @@ -26,10 +26,10 @@ if ![runto_main] { gdb_breakpoint [gdb_get_line_number "Before pointer assignment"] gdb_continue_to_breakpoint "Before pointer assignment" -gdb_test "ptype ptr" "= int \\(\\*\\)\\\[3\\\]" \ +gdb_test "ptype ptr" "= int \\(\\*\\)\\\[variable length\\\]" \ "ptype ptr, before pointer assignment" -gdb_test "print ptr" "= \\(int \\(\\*\\)\\\[3\\\]\\) 0x0" \ +gdb_test "print ptr" "= \\(int \\(\\*\\)\\\[variable length\\\]\\) 0x0" \ "print ptr, before pointer assignment" gdb_test "print *ptr" "Cannot access memory at address 0x0" \