From patchwork Wed Mar 29 19:10:58 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Maciej W. Rozycki" X-Patchwork-Id: 67097 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 A519D3858413 for ; Wed, 29 Mar 2023 19:11:17 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mail-ed1-x52d.google.com (mail-ed1-x52d.google.com [IPv6:2a00:1450:4864:20::52d]) by sourceware.org (Postfix) with ESMTPS id 2DA53385802F for ; Wed, 29 Mar 2023 19:11:02 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 2DA53385802F Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=embecosm.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=embecosm.com Received: by mail-ed1-x52d.google.com with SMTP id cn12so67476464edb.4 for ; Wed, 29 Mar 2023 12:11:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=embecosm.com; s=google; t=1680117061; h=mime-version:user-agent:references:message-id:in-reply-to:subject :cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=yEflwrg7e92BsfGjVeEuGOduKwF/kEFxrOxoq+0LNRM=; b=CnXsPe4YcpMBGsNvDMCNBMOfR3bKb7A48zAhylsESDqfKKS6Nnwab5ZDdCWeLhyQZ0 98YLUk45fXXEj4lmn+7Zy5Qd1vuUnSVf8qVkSZ7qEUeqVvetaKjQu23Wb6wtwSSVUorg OPA9hztQgCv/DQqmlBmKy98Er1u/ZYpory9CpfQIJCmZeH+MMdep1fgZQCdHyDWYl2S7 pIf9IaDgsgjo6yxJsV6QdUHQGFjrT+K+2TVJ4x1J0vO2E+vVVx3mMwxWDDIUcnPiq/pf ZiBDQ57sV5ut4hsXYtdE+e7GB0Gq7DmZ5+cZoF55/odCuzvOSQJCiU+QDMy05xVYZ5OY tP9g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680117061; h=mime-version:user-agent:references:message-id:in-reply-to:subject :cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=yEflwrg7e92BsfGjVeEuGOduKwF/kEFxrOxoq+0LNRM=; b=b6j7s4Er5k7eKLTXvzSQWTRVQ7Uo754bCgDemQN7h7GIFcJH+DoXZ4Kn6rh42iFsil gIA5T1seqZ0nDhxL4xKOnikUCPtv8CE0lfZf4floHbkyY3cPZPpjU0y7U1zdQhFUkx5B VFgsKoteTfb2FHlnsz4zFiJcgS09VUP0TMSUHXZsnopUK0HmPg2hMK9mu5uJ09kcehWu 1yWdkcA4RmliHhfgdGIwrxysMox0Cjqqlin/AlCyPMazS0JawUzOwO98FtZlAEhgezqa TXKE8pmc8e18HI7rTRGeAJnMilYy6urZnfeRWD+uoFjRocBycYM4JoblIrD8FO8jGx9/ DC1A== X-Gm-Message-State: AAQBX9etkdvPaPJKntmUOKlC+ISpZts8Djnqi3A/3N75Daujfd5VYB4y RAem3Kpq2lKujy2soR8hgy8Bfxdvhl3KOUa+1pA= X-Google-Smtp-Source: AKy350aaJ7K03PFYWtt9VIhvM3/T1DdaVk6MsztXH1EmfDVRc0rCLsR4s1Q7rTfQvop5iLfBPVSlLQ== X-Received: by 2002:a17:906:395:b0:93b:6da8:539a with SMTP id b21-20020a170906039500b0093b6da8539amr20407978eja.18.1680117060834; Wed, 29 Mar 2023 12:11:00 -0700 (PDT) Received: from annie.orcam.me.uk (annie.orcam.me.uk. [2001:4190:8020::48]) by smtp.gmail.com with ESMTPSA id a11-20020a17090680cb00b00930d22474dbsm16852752ejx.97.2023.03.29.12.10.59 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 29 Mar 2023 12:11:00 -0700 (PDT) Date: Wed, 29 Mar 2023 20:10:58 +0100 (BST) From: "Maciej W. Rozycki" To: gdb-patches@sourceware.org cc: Tom Tromey , Richard Bunt Subject: [PATCH v3 1/3] GDB: Favor full symbol main name for backtrace stop In-Reply-To: Message-ID: References: User-Agent: Alpine 2.20 (DEB 67 2015-01-07) MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, KAM_ASCII_DIVIDERS, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=no 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: , Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org Sender: "Gdb-patches" From: Richard Bunt In the case where a Fortran program has a program name of "main" and there is also a minimal symbol called main, such as with programs built with GCC version 4.4.7 or below, the backtrace will erroneously stop at the minimal symbol rather than the user specified main, e.g.: (gdb) bt #0 bar () at .../gdb/testsuite/gdb.fortran/backtrace.f90:17 #1 0x0000000000402556 in foo () at .../gdb/testsuite/gdb.fortran/backtrace.f90:21 #2 0x0000000000402575 in main () at .../gdb/testsuite/gdb.fortran/backtrace.f90:31 #3 0x00000000004025aa in main () (gdb) This patch fixes this issue by increasing the precedence of the full symbol when the language of the current frame is Fortran. Newer versions of GCC transform the program name to "MAIN__" in this case, avoiding the problem. Co-Authored-By: Maciej W. Rozycki --- No changes from v2. Changes from v1: - Remove testsuite status mistakenly copied to the change description. --- gdb/frame.c | 25 ++++++++++++--------- gdb/testsuite/gdb.fortran/backtrace.exp | 38 ++++++++++++++++++++++++++++++++ gdb/testsuite/gdb.fortran/backtrace.f90 | 32 ++++++++++++++++++++++++++ 3 files changed, 85 insertions(+), 10 deletions(-) gdb-richard-favour-full-symbol-main-name-for-backtrace-stop.diff Index: src/gdb/frame.c =================================================================== --- src.orig/gdb/frame.c +++ src/gdb/frame.c @@ -2542,29 +2542,34 @@ inside_main_func (frame_info_ptr this_fr bound_minimal_symbol msymbol = lookup_minimal_symbol (name, NULL, current_program_space->symfile_object_file); - if (msymbol.minsym == nullptr) + + if (msymbol.minsym != nullptr) + sym_addr = msymbol.value_address (); + + /* Favor a full symbol in Fortran, for the case where the Fortran main + is also called "main". */ + if (msymbol.minsym == nullptr + || get_frame_language (this_frame) == language_fortran) { /* In some language (for example Fortran) there will be no minimal symbol with the name of the main function. In this case we should search the full symbols to see if we can find a match. */ struct block_symbol bs = lookup_symbol (name, NULL, VAR_DOMAIN, 0); - if (bs.symbol == nullptr) - return false; /* We might have found some unrelated symbol. For example, the Rust compiler can emit both a subprogram and a namespace with the same name in the same scope; and due to how gdb's symbol tables currently work, we can't request the one we'd prefer. */ - if (bs.symbol->aclass () != LOC_BLOCK) + if (bs.symbol != nullptr && bs.symbol->aclass () == LOC_BLOCK) + { + const struct block *block = bs.symbol->value_block (); + gdb_assert (block != nullptr); + sym_addr = block->start (); + } + else if (msymbol.minsym == nullptr) return false; - - const struct block *block = bs.symbol->value_block (); - gdb_assert (block != nullptr); - sym_addr = block->start (); } - else - sym_addr = msymbol.value_address (); /* Convert any function descriptor addresses into the actual function code address. */ Index: src/gdb/testsuite/gdb.fortran/backtrace.exp =================================================================== --- /dev/null +++ src/gdb/testsuite/gdb.fortran/backtrace.exp @@ -0,0 +1,38 @@ +# 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 . + +require allow_fortran_tests + +load_lib fortran.exp + +standard_testfile .f90 + +if {[prepare_for_testing ${testfile}.exp ${testfile} ${srcfile} {debug f90}]} { + return -1 +} + +if {![fortran_runto_main]} { + perror "Could not run to main." + return +} + +gdb_breakpoint [gdb_get_line_number "Break here"] +gdb_continue_to_breakpoint "continue to bar" + +gdb_test "bt" \ + [multi_line \ + "#0\[ \t\]*bar \\(\\) at \[^\r\n\]+" \ + "#1\[ \t\]*$hex in foo \\(\\) at \[^\r\n\]+" \ + "#2\[ \t\]*$hex in \(?:MAIN__\|main\) \\(\\) at \[^\r\n\]+" ] Index: src/gdb/testsuite/gdb.fortran/backtrace.f90 =================================================================== --- /dev/null +++ src/gdb/testsuite/gdb.fortran/backtrace.f90 @@ -0,0 +1,32 @@ +! 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 . + +subroutine bar () + return ! Break here +end subroutine bar + +subroutine foo () + call bar () +end subroutine foo + +program main + interface + subroutine bar () + end subroutine bar + subroutine foo () + end subroutine foo + end interface + call foo () +end program main