From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-il1-x135.google.com (mail-il1-x135.google.com [IPv6:2607:f8b0:4864:20::135]) by sourceware.org (Postfix) with ESMTPS id 2C04D3858401 for ; Wed, 23 Aug 2023 17:11:32 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 2C04D3858401 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=adacore.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=adacore.com Received: by mail-il1-x135.google.com with SMTP id e9e14a558f8ab-34ca1bcb48fso12887405ab.2 for ; Wed, 23 Aug 2023 10:11:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=adacore.com; s=google; t=1692810691; x=1693415491; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=k91f86gNP5RyZERt6iJ/vyuNbbtOcUnyGoE83EDvP3k=; b=KcJdPj5OVm5mnhXR6dOK+anoRYqz9owN5gtLNdTP1Mu+9/8JIQhW32vLR/4ujk/kNp i2sF9q5Ldv/Rj547Km9oeYKO6UpY58XujrtICyWGEZnkKbednU2hg9fr+Fi7nzkWDYt0 8UyuivjgT7eYrFgGWuqk5M8Tpak2oLwbOhoDRqGLJ3AAou3rSdtuQmAjxlRaY4DTF7bF SPaQA+nXHczoyccuy523EfqgOa3+jSuaPbmv1S7/iV/NiU2dG/XBzSx2YCkDODpeULYd LXDQYd+KN2iD2OzKs+cqowvfa198CsMbgDEdajbBLnpqPk7lRZqK7CGg63hwhO9QnFRs 2LZg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692810691; x=1693415491; 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=k91f86gNP5RyZERt6iJ/vyuNbbtOcUnyGoE83EDvP3k=; b=GzylF1mlPGCi8McGtSj7dB4gY574kdC0y9MWAFOdbO2TJ8FSuiYjURdwb93xekXpSg uSO5NzZS/0qpasmRZCoOZcDSRI61RhNso2cMq+zm4x2jpMcZuVshKNcMt5/6zkJRg0FD l+AmLoV9HhoXlx7h2YF87YRRbSQf6WiPz+8ddoQki1zFgsVZv+WndbsWHE48P4llU7rA CWgtLjc+h3/mhHSXpfZZf6xZAiL9NLVeu66AWq50PH9Y5cZ9ojPrS9pBfPs36HBgoPJm FCySkl6uBgG96ZxBHXikfzE9t1VuAUH80/aRchzGYfmU6Fnl1hrlvZXcLeVvTR6W2xIb n+dA== X-Gm-Message-State: AOJu0YxmIxaF3sHzIa4yfBuO0RXMF+cimZWzJ+VRgRY/jioUSCCCCT0B /b/fg5m2QqC/9KZme1UbeR1SdatMYXKSSFK24e1klw== X-Google-Smtp-Source: AGHT+IHZJmHG0r8wGsxVAx3291FYgohEDFzrH0fzXcDxDP+J+V/jGcvg2CNQxIw5rjCnEPnbqiiJoQ== X-Received: by 2002:a05:6e02:13c1:b0:349:2bab:9e47 with SMTP id v1-20020a056e0213c100b003492bab9e47mr2937315ilj.12.1692810691292; Wed, 23 Aug 2023 10:11:31 -0700 (PDT) Received: from localhost.localdomain (75-166-142-177.hlrn.qwest.net. [75.166.142.177]) by smtp.gmail.com with ESMTPSA id gk14-20020a0566386a8e00b004332f6537e2sm2069635jab.83.2023.08.23.10.11.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 23 Aug 2023 10:11:30 -0700 (PDT) From: Tom Tromey To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [PATCH] Read Ada main name from executable, not inferior Date: Wed, 23 Aug 2023 11:11:20 -0600 Message-Id: <20230823171120.2566006-1-tromey@adacore.com> X-Mailer: git-send-email 2.40.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-11.6 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 autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: An upstream bug report points out this bug: if the user switches from one Ada executable to another without "kill"ing the inferior, then the "start" command will fail. What happens here is that the Ada "main" name is found in a constant string in the executable. But, if the inferior is running, then the process_stratum target reads from the inferior memory. This patch fixes the problem by changing the main name code to set trust-readonly-sections, causing the target stack to read from the executable instead. I looked briefly at changing GNAT to emit DW_AT_main_subprogram instead, but this looks to be pretty involved. Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=25811 --- gdb/ada-lang.c | 7 ++ gdb/target.c | 5 +- gdb/target.h | 4 ++ gdb/testsuite/gdb.ada/file-then-restart.exp | 69 +++++++++++++++++++ .../gdb.ada/file-then-restart/first.adb | 25 +++++++ .../gdb.ada/file-then-restart/second.adb | 25 +++++++ 6 files changed, 132 insertions(+), 3 deletions(-) create mode 100644 gdb/testsuite/gdb.ada/file-then-restart.exp create mode 100644 gdb/testsuite/gdb.ada/file-then-restart/first.adb create mode 100644 gdb/testsuite/gdb.ada/file-then-restart/second.adb diff --git a/gdb/ada-lang.c b/gdb/ada-lang.c index 575568cffb5..1419983b609 100644 --- a/gdb/ada-lang.c +++ b/gdb/ada-lang.c @@ -814,6 +814,13 @@ ada_main_name () if (main_program_name_addr == 0) error (_("Invalid address for Ada main program name.")); + /* Force trust_readonly, because we always want to fetch this + string from the executable, not from inferior memory. If the + user changes the exec-file and invokes "start", we want to + pick the "main" from the new executable, not one that may + come from the still-live inferior. */ + scoped_restore save_trust_readonly + = make_scoped_restore (&trust_readonly, true); main_program_name = target_read_string (main_program_name_addr, 1024); return main_program_name.get (); } diff --git a/gdb/target.c b/gdb/target.c index 16f43d072cd..516a8d23618 100644 --- a/gdb/target.c +++ b/gdb/target.c @@ -116,10 +116,9 @@ static struct target_ops *the_debug_target; static struct cmd_list_element *targetlist = NULL; -/* True if we should trust readonly sections from the - executable when reading memory. */ +/* See target.h. */ -static bool trust_readonly = false; +bool trust_readonly = false; /* Nonzero if we should show true memory content including memory breakpoint inserted by gdb. */ diff --git a/gdb/target.h b/gdb/target.h index 6ae400e2cc2..5ed3f314237 100644 --- a/gdb/target.h +++ b/gdb/target.h @@ -2466,6 +2466,10 @@ extern int remote_timeout; extern scoped_restore_tmpl make_scoped_restore_show_memory_breakpoints (int show); +/* True if we should trust readonly sections from the + executable when reading memory. */ +extern bool trust_readonly; + extern bool may_write_registers; extern bool may_write_memory; extern bool may_insert_breakpoints; diff --git a/gdb/testsuite/gdb.ada/file-then-restart.exp b/gdb/testsuite/gdb.ada/file-then-restart.exp new file mode 100644 index 00000000000..e2c76b666e3 --- /dev/null +++ b/gdb/testsuite/gdb.ada/file-then-restart.exp @@ -0,0 +1,69 @@ +# Copyright 2023 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 + +# This testcase verifies the behavior of the `start' command, which +# does not work when we use the gdb stub... +require !use_gdb_stub + +standard_ada_testfile first + +if {[gdb_compile_ada "${srcfile}" "${binfile}" executable {debug}] != ""} { + return -1 +} + +# Build the second test program +set srcfile2 ${srcdir}/${subdir}/${testdir}/second.adb +set binfile2 [standard_output_file second] + +if {[gdb_compile_ada "${srcfile2}" "${binfile2}" executable {debug}] != ""} { + return -1 +} + +foreach_with_prefix scenario {kill no-kill} { + clean_restart $binfile + + # Start the program, we should land in the program main procedure + if {[gdb_start_cmd] < 0} { + fail start + return -1 + } + + gdb_test "" \ + "first \\(\\) at .*first.adb.*" \ + "start first" + + gdb_test_no_output "set confirm off" + + if {$scenario == "kill"} { + gdb_test "kill" "Inferior $decimal .*killed.*" + } + + gdb_test "file $binfile2" "Reading symbols from .*" \ + "switch to second executable" + + # Start the program a second time, GDB should land in procedure + # Second this time. + if {[gdb_start_cmd] < 0} { + fail "start second" + } else { + gdb_test "" \ + "second \\(\\) at .*second.adb.*" \ + "start second" + } +} diff --git a/gdb/testsuite/gdb.ada/file-then-restart/first.adb b/gdb/testsuite/gdb.ada/file-then-restart/first.adb new file mode 100644 index 00000000000..9be08356dad --- /dev/null +++ b/gdb/testsuite/gdb.ada/file-then-restart/first.adb @@ -0,0 +1,25 @@ +-- Copyright 2005-2023 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 First is + + procedure Break_Me is + begin + null; + end Break_Me; + +begin + Break_Me; +end First; diff --git a/gdb/testsuite/gdb.ada/file-then-restart/second.adb b/gdb/testsuite/gdb.ada/file-then-restart/second.adb new file mode 100644 index 00000000000..7270cf781b3 --- /dev/null +++ b/gdb/testsuite/gdb.ada/file-then-restart/second.adb @@ -0,0 +1,25 @@ +-- Copyright 2005-2023 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 Second is + + procedure Break_Me is + begin + null; + end Break_Me; + +begin + Break_Me; +end Second; -- 2.40.1