Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Abdul Basit Ijaz <abdul.b.ijaz@intel.com>
To: gdb-patches@sourceware.org
Cc: abdul.b.ijaz@intel.com, JiniSusan.George@amd.com, tom@tromey.com,
	eliz@gnu.org
Subject: [PATCH v6 05/10] gdb: Skip trampoline functions for the finish and reverse-finish commands.
Date: Thu, 28 Mar 2024 13:05:23 +0100	[thread overview]
Message-ID: <20240328120528.30382-6-abdul.b.ijaz@intel.com> (raw)
In-Reply-To: <20240328120528.30382-1-abdul.b.ijaz@intel.com>

From: "Ijaz, Abdul B" <abdul.b.ijaz@intel.com>

This change skips trampoline functions when the option
'skip-trampoline-functions' is set to 'on' for the finish or reverse-finish
command.  Before this change, for these commands GDB returns to the trampoline
function indicated by the compiler with DIE "DW_AT_trampoline".  For better
user experience, all such frames can be hidden from the user.

In this example the IFX compiler emits "DW_AT_trampoline" tag for the 'first'
and 'second' trampoline functions like following:

function second (x, y) result(z)
  integer, intent(in) :: x, y
  integer :: z
  z = x * y ! breakpt-finish
end function second

function first (num1, num2) result(total)
  integer, intent(in) :: num1, num2
  integer  :: total
  total = second (num1 + 4, num2 * 3) ! first-breakpt
  total = total + 30
end function first

Related Dwarf:

0x0000013f:   DW_TAG_subprogram
                DW_AT_low_pc    (0x0000000000404350)
                DW_AT_high_pc   (0x000000000040435f)
                DW_AT_frame_base        (DW_OP_reg6 RBP)
                DW_AT_linkage_name      ("second_.t74p.t75p")
                DW_AT_name      ("second_.t74p.t75p")
                DW_AT_trampoline        ("second_")

0x0000015a:   DW_TAG_subprogram
                DW_AT_low_pc    (0x00000000004044a0)
                DW_AT_high_pc   (0x00000000004044af)
                DW_AT_frame_base        (DW_OP_reg6 RBP)
                DW_AT_linkage_name      ("first_.t104p.t105p")
                DW_AT_name      ("first_.t104p.t105p")
                DW_AT_trampoline        ("first_")

Before this change, the finish command output looks like:

'''
(gdb) finish
Run till exit from #0  second (x=20, y=9) at test.f90:4
0x0000000000405209 in second_.t74p.t75p () at test.f90:12
12      end function first
Value returned is $1 = 180
'''

The reverse-finish command output in this test before the change:

'''
(gdb) reverse-finish
Run back to call of #0  second (x=20, y=9) at test.f90:4
0x0000000000405204 in second_.t74p.t75p () at test.f90:12
12      end function first
'''

After this change:

'''
(gdb) finish
Run till exit from #0  second (x=20, y=9) at test.f90:4
0x00000000004051e3 in first (num1=16, num2=3) at test.f90:10
10        total = second (num1 + 4, num2 * 3) ! first-breakpt
Value returned is $1 = 180

(gdb) reverse-finish
Run back to call of #0  second (x=20, y=9) at test.f90:4
0x00000000004051de in first (num1=16, num2=3) at test.f90:10
10        total = second (num1 + 4, num2 * 3) ! first-breakpt
'''

The test gdb.fortran/func-trampoline.exp is updated for testing the finish
command.  The test gdb.reverse/finish-reverse-trampoline.exp is
added for testing the reverse-finish command.

2024-03-28 Ijaz, Abdul B <abdul.b.ijaz@intel.com>
---
 gdb/NEWS                                      |  5 +-
 gdb/doc/gdb.texinfo                           |  4 ++
 gdb/infcmd.c                                  | 12 ++++
 gdb/infrun.c                                  |  8 +++
 gdb/infrun.h                                  |  5 ++
 gdb/testsuite/gdb.fortran/func-trampoline.exp | 13 ++++-
 .../gdb.reverse/finish-reverse-trampoline.exp | 56 +++++++++++++++++++
 7 files changed, 99 insertions(+), 4 deletions(-)
 create mode 100644 gdb/testsuite/gdb.reverse/finish-reverse-trampoline.exp

diff --git a/gdb/NEWS b/gdb/NEWS
index 025f2e1a17e..b8577c58af7 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -898,8 +898,9 @@ show skip-trampoline-functions
   recognize function calls that have been marked as trampolines in the debug
   info.  It improves stepping behavior in that it steps over the trampoline
   code and hides it from the user.  It improves the printing of the stack by
-  hiding trampoline functions from the backtrace.  Currently, only DWARF
-  trampolines are supported.
+  hiding the trampoline frames from the backtrace and skips trampoline
+  functions while returning from the target function of a trampoline call.
+  Currently, only DWARF trampolines are supported.
 
   If this is turned off, GDB gdb will handle trampoline functions the same
   as any other function.
diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
index 6117f6e889f..5782e06b91d 100644
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -6475,6 +6475,10 @@ DWARF trampolines marked via DW_AT_trampoline are supported by this.
 When issuing a @code{backtrace}, if @code{skip-trampoline-functions} is set,
 @value{GDBN} will skip trampoline frames while printing the stack.
 
+When issuing a @code{finish} or @code{reverse-finish}, if
+@code{skip-trampoline-functions} is set, @value{GDBN} will skip trampoline
+frames while returning from the target function.
+
 Currently, only DWARF trampolines marked via DW_AT_trampoline are supported by
 this.
 
diff --git a/gdb/infcmd.c b/gdb/infcmd.c
index 600c90c055a..cd9baa03ecd 100644
--- a/gdb/infcmd.c
+++ b/gdb/infcmd.c
@@ -1801,6 +1801,18 @@ finish_command (const char *arg, int from_tty)
   if (frame == 0)
     error (_("\"finish\" not meaningful in the outermost frame."));
 
+  if (skip_trampoline_functions)
+    {
+      for (int i = 0; (SAFE_TRAMPOLINE_CHAIN (i, frame)
+		       && in_trampoline_frame (frame)); ++i)
+	frame = get_prev_frame (frame);
+
+      if (frame == nullptr)
+	error (_("\"finish\" not meaningful in the outermost non-trampoline \
+frame.  Consider running \"set skip-trampoline-functions off\", to stop in \
+trampoline frames for the \"finish\" command."));
+    }
+
   clear_proceed_status (0);
 
   tp = inferior_thread ();
diff --git a/gdb/infrun.c b/gdb/infrun.c
index 1c5711b8c41..5419d58bf3b 100644
--- a/gdb/infrun.c
+++ b/gdb/infrun.c
@@ -8095,6 +8095,14 @@ process_event_stop_test (struct execution_control_state *ecs)
 	  keep_going (ecs);
 	  return;
 	}
+      else if (skip_trampoline_functions && in_trampoline_function (stop_pc))
+	{
+	  /* While reverse stepping if we are in a trampoline function call
+	     we will just continue single step in the hope of leaving the
+	     trampoline again soon.  */
+	  keep_going (ecs);
+	  return;
+	}
     }
 
   /* This always returns the sal for the inner-most frame when we are in a
diff --git a/gdb/infrun.h b/gdb/infrun.h
index fbe88ad4595..3873cca7619 100644
--- a/gdb/infrun.h
+++ b/gdb/infrun.h
@@ -81,6 +81,11 @@ infrun_debug_show_threads (const char *title, ThreadRange threads)
    information.  */
 #define MAX_TRAMPOLINE_CHAIN_SIZE 10
 
+/* True if the trampoline index "i" is less then the maximum allowed size
+   of a trampoline chain.  */
+#define SAFE_TRAMPOLINE_CHAIN(i, frame) \
+  (i < MAX_TRAMPOLINE_CHAIN_SIZE && (frame != nullptr))
+
 /* Nonzero if we want to give control to the user when we're notified
    of shared library events by the dynamic linker.  */
 extern int stop_on_solib_events;
diff --git a/gdb/testsuite/gdb.fortran/func-trampoline.exp b/gdb/testsuite/gdb.fortran/func-trampoline.exp
index bfa0002cf0e..4cb9e4f4919 100644
--- a/gdb/testsuite/gdb.fortran/func-trampoline.exp
+++ b/gdb/testsuite/gdb.fortran/func-trampoline.exp
@@ -13,8 +13,8 @@
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/> .
 
-# Test "backtrace" and "backtrace -n" commands for functions with trampoline
-# calls.
+# Test "backtrace", "backtrace -n" and "finish"  commands for functions with
+# trampoline calls.
 
 require allow_fortran_tests
 
@@ -75,3 +75,12 @@ with_test_prefix "backtrace outerframes" {
 	"#$decimal.* $middle_desc" \
 	"#$decimal.* $outer_desc.*"]
 }
+
+with_test_prefix "finish" {
+    init_test
+
+    gdb_test "finish" [multi_line \
+	"Run till exit from #0 $fill second \\(x=20, y=9\\) $fill" \
+	"${fill}first \\(num1=16, num2=3\\)${fill}" \
+	"${fill}(\r\nValue returned is $valnum_re = 180)"]
+}
diff --git a/gdb/testsuite/gdb.reverse/finish-reverse-trampoline.exp b/gdb/testsuite/gdb.reverse/finish-reverse-trampoline.exp
new file mode 100644
index 00000000000..3a23a30ad0d
--- /dev/null
+++ b/gdb/testsuite/gdb.reverse/finish-reverse-trampoline.exp
@@ -0,0 +1,56 @@
+# 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 <http://www.gnu.org/licenses/>.
+
+# Test "reverse-finish" command for functions with trampoline.
+
+require supports_reverse allow_fortran_tests
+
+if {![test_compiler_info {ifx-*} f90]} {
+    untested "This test is only applicable for IFX, which emits the\
+	trampoline DIE in Dwarf."
+    return -1
+}
+
+load_lib fortran.exp
+
+set testfile finish-reverse-trampoline
+set srcfile "${srcdir}/gdb.fortran/func-trampoline.f90"
+set binfile [standard_output_file $testfile]
+
+if {[prepare_for_testing "failed to prepare" ${testfile} ${srcfile} \
+    {debug f90}]} {
+    return -1
+}
+
+if {![fortran_runto_main]} {
+    return -1
+}
+
+set inner_loc [gdb_get_line_number "second-breakpt"]
+
+if [supports_process_record] {
+    # Activate process record/replay
+    gdb_test_no_output "record" "turn on process record"
+}
+
+# Set breakpoint inside the innermost function 'second'.
+gdb_breakpoint "$srcfile:$inner_loc"
+gdb_continue_to_breakpoint "innermost-body" ".*$srcfile:$inner_loc.*"
+
+gdb_test "reverse-finish" [multi_line \
+    "Run back to call of #0  second \\(x=20, y=9\\).*" \
+    ".*in first \\(num1=16, num2=3\\).*"]
+
+gdb_test "frame" "#0.*first.*" "Frame 0 shows first function"
-- 
2.34.1

Intel Deutschland GmbH
Registered Address: Am Campeon 10, 85579 Neubiberg, Germany
Tel: +49 89 99 8853-0, www.intel.de <http://www.intel.de>
Managing Directors: Christin Eisenschmid, Sharon Heck, Tiffany Doon Silva  
Chairperson of the Supervisory Board: Nicole Lau
Registered Office: Munich
Commercial Register: Amtsgericht Muenchen HRB 186928


  parent reply	other threads:[~2024-03-28 12:07 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-03-28 12:05 [PATCH v6 00/10] GDB support for DW_AT_trampoline Abdul Basit Ijaz
2024-03-28 12:05 ` [PATCH v6 01/10] gdb, dwarf: add support for DW_AT_trampoline in DWARF reader Abdul Basit Ijaz
2024-03-28 12:05 ` [PATCH v6 02/10] gdb/symtab: add lookup for trampoline functions Abdul Basit Ijaz
2024-03-28 12:05 ` [PATCH v6 03/10] gdb: handle stepping through functions with DW_AT_trampoline Abdul Basit Ijaz
2024-03-28 13:58   ` Eli Zaretskii
2024-03-28 14:31     ` Ijaz, Abdul B
2024-03-28 12:05 ` [PATCH v6 04/10] gdb: Skip trampoline frames for the backtrace command Abdul Basit Ijaz
2024-03-28 12:05 ` Abdul Basit Ijaz [this message]
2024-03-28 13:59   ` [PATCH v6 05/10] gdb: Skip trampoline functions for the finish and reverse-finish commands Eli Zaretskii
2024-03-28 12:05 ` [PATCH v6 06/10] gdb: Skip trampoline functions for the up command Abdul Basit Ijaz
2024-03-28 14:01   ` Eli Zaretskii
2024-03-28 12:05 ` [PATCH v6 07/10] gdb: Skip trampoline functions for the return command Abdul Basit Ijaz
2024-03-28 14:01   ` Eli Zaretskii
2024-03-28 12:05 ` [PATCH v6 08/10] gdb, mi: Skip trampoline functions for the -stack-list-frames command Abdul Basit Ijaz
2024-03-28 14:02   ` Eli Zaretskii
2024-03-28 12:05 ` [PATCH v6 09/10] gdb, mi: Skip trampoline functions for the -stack-list-arguments command Abdul Basit Ijaz
2024-03-28 14:03   ` Eli Zaretskii
2024-03-28 12:05 ` [PATCH v6 10/10] gdb: Filter trampoline frames in backtrace when using Python frame-filters Abdul Basit Ijaz

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240328120528.30382-6-abdul.b.ijaz@intel.com \
    --to=abdul.b.ijaz@intel.com \
    --cc=JiniSusan.George@amd.com \
    --cc=eliz@gnu.org \
    --cc=gdb-patches@sourceware.org \
    --cc=tom@tromey.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox