public inbox for gdb-cvs@sourceware.org
help / color / mirror / Atom feed
* [binutils-gdb] GDB: Favor full symbol main name for backtrace stop
@ 2023-03-31 14:33 Maciej W. Rozycki
  0 siblings, 0 replies; only message in thread
From: Maciej W. Rozycki @ 2023-03-31 14:33 UTC (permalink / raw)
  To: gdb-cvs

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=571eb2644c7a52a78250ce9906d9c5127006e164

commit 571eb2644c7a52a78250ce9906d9c5127006e164
Author: Richard Bunt <richard.bunt@linaro.org>
Date:   Fri Mar 31 15:31:40 2023 +0100

    GDB: Favor full symbol main name for backtrace stop
    
    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 <macro@embecosm.com>

Diff:
---
 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(-)

diff --git a/gdb/frame.c b/gdb/frame.c
index de3c7de440d..4f420f138f6 100644
--- a/gdb/frame.c
+++ b/gdb/frame.c
@@ -2542,29 +2542,34 @@ inside_main_func (frame_info_ptr this_frame)
   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.  */
diff --git a/gdb/testsuite/gdb.fortran/backtrace.exp b/gdb/testsuite/gdb.fortran/backtrace.exp
new file mode 100644
index 00000000000..8553c31abbf
--- /dev/null
+++ b/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 <http://www.gnu.org/licenses/>.
+
+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\]+" ]
diff --git a/gdb/testsuite/gdb.fortran/backtrace.f90 b/gdb/testsuite/gdb.fortran/backtrace.f90
new file mode 100644
index 00000000000..a5f03f14f56
--- /dev/null
+++ b/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 <http://www.gnu.org/licenses/>.
+
+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

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2023-03-31 14:33 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-03-31 14:33 [binutils-gdb] GDB: Favor full symbol main name for backtrace stop Maciej W. Rozycki

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).