[1/3] gdb: Fix missed inline callsite when the frame has changed

Message ID 20240201152118.598375-2-tlloyddavies@undo.io
State New
Headers
Series Fix inline function stepping |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gdb_build--master-aarch64 success Testing passed
linaro-tcwg-bot/tcwg_gdb_build--master-arm success Testing passed
linaro-tcwg-bot/tcwg_gdb_check--master-aarch64 success Testing passed
linaro-tcwg-bot/tcwg_gdb_check--master-arm success Testing passed

Commit Message

Toby Lloyd Davies Feb. 1, 2024, 3:21 p.m. UTC
  When the program has just returned from a function, gdb
could incorrectly step over an inline callsite. This would occur when
the line from the innermost inline function did not have is_stmt set.

e.g. with the following assembly:

foo ();
    call foo
bar ();
    global_var++; /* start of bar. is_stmt false */

When stepping out of foo, gdb will not stop the program at the callsite
of bar.

This problem was occurring because the first part of logic that checked
for "calls" to inlined functions was guarded by a check that the we were
still stepping through the same function. This meant that it wasn't used
when the frame had changed due to returning from a function. In this
case we would incorrectly use the symtab_and_line from the innermost
inline function which would mean we wouldn't stop if this line didn't
have is_stmt set.

This guard was necessary with the logic for detecting if we had stepped
into another inline function being done later than the check for inline
callsites. This is because otherwise we could decide to stop at an
inline callsite before we detect that we have also stepped into a
different inlined function we should step over. So fix the bug by
swapping the order of the checks.
---
 gdb/infrun.c                                  |  50 +++----
 .../gdb.dwarf2/dw2-inline-stepping-2.c        |  49 +++++++
 .../gdb.dwarf2/dw2-inline-stepping-2.exp      | 127 ++++++++++++++++++
 3 files changed, 202 insertions(+), 24 deletions(-)
 create mode 100644 gdb/testsuite/gdb.dwarf2/dw2-inline-stepping-2.c
 create mode 100644 gdb/testsuite/gdb.dwarf2/dw2-inline-stepping-2.exp
  

Patch

diff --git a/gdb/infrun.c b/gdb/infrun.c
index e99a1a83070..a782c115cad 100644
--- a/gdb/infrun.c
+++ b/gdb/infrun.c
@@ -8090,12 +8090,31 @@  process_event_stop_test (struct execution_control_state *ecs)
       return;
     }
 
-  /* Look for "calls" to inlined functions, part one.  If the inline
+  /* Look for "calls" to inlined functions, part one.  If we are still
+     in the same real function we were stepping through, but we have
+     to go further up to find the exact frame ID, we are stepping
+     through a more inlined call beyond its call site.  */
+
+  if (get_frame_type (get_current_frame ()) == INLINE_FRAME
+      && (*curr_frame_id != original_frame_id)
+      && stepped_in_from (get_current_frame (),
+			  ecs->event_thread->control.step_frame_id))
+    {
+      infrun_debug_printf ("stepping through inlined function");
+
+      if (ecs->event_thread->control.step_over_calls == STEP_OVER_ALL
+	  || inline_frame_is_marked_for_skip (false, ecs->event_thread))
+	keep_going (ecs);
+      else
+	end_stepping_range (ecs);
+      return;
+    }
+
+  /* Look for "calls" to inlined functions, part two.  If the inline
      frame machinery detected some skipped call sites, we have entered
      a new inline function.  */
 
-  if ((*curr_frame_id == original_frame_id)
-      && inline_skipped_frames (ecs->event_thread))
+  if (inline_skipped_frames (ecs->event_thread))
     {
       infrun_debug_printf ("stepped into inlined function");
 
@@ -8108,7 +8127,8 @@  process_event_stop_test (struct execution_control_state *ecs)
 	     we were previously stepping, go down into the function
 	     first.  Otherwise stop at the call site.  */
 
-	  if (call_sal.line == ecs->event_thread->current_line
+	  if (*curr_frame_id == original_frame_id
+	      && call_sal.line == ecs->event_thread->current_line
 	      && call_sal.symtab == ecs->event_thread->current_symtab)
 	    {
 	      step_into_inline_frame (ecs->event_thread);
@@ -8127,7 +8147,8 @@  process_event_stop_test (struct execution_control_state *ecs)
 	  /* For "next", we should stop at the call site if it is on a
 	     different source line.  Otherwise continue through the
 	     inlined function.  */
-	  if (call_sal.line == ecs->event_thread->current_line
+	  if (*curr_frame_id == original_frame_id
+	      && call_sal.line == ecs->event_thread->current_line
 	      && call_sal.symtab == ecs->event_thread->current_symtab)
 	    keep_going (ecs);
 	  else
@@ -8136,25 +8157,6 @@  process_event_stop_test (struct execution_control_state *ecs)
 	}
     }
 
-  /* Look for "calls" to inlined functions, part two.  If we are still
-     in the same real function we were stepping through, but we have
-     to go further up to find the exact frame ID, we are stepping
-     through a more inlined call beyond its call site.  */
-
-  if (get_frame_type (get_current_frame ()) == INLINE_FRAME
-      && (*curr_frame_id != original_frame_id)
-      && stepped_in_from (get_current_frame (), original_frame_id))
-    {
-      infrun_debug_printf ("stepping through inlined function");
-
-      if (ecs->event_thread->control.step_over_calls == STEP_OVER_ALL
-	  || inline_frame_is_marked_for_skip (false, ecs->event_thread))
-	keep_going (ecs);
-      else
-	end_stepping_range (ecs);
-      return;
-    }
-
   bool refresh_step_info = true;
   if ((ecs->event_thread->stop_pc () == stop_pc_sal.pc)
       && (ecs->event_thread->current_line != stop_pc_sal.line
diff --git a/gdb/testsuite/gdb.dwarf2/dw2-inline-stepping-2.c b/gdb/testsuite/gdb.dwarf2/dw2-inline-stepping-2.c
new file mode 100644
index 00000000000..9c2b3f95a26
--- /dev/null
+++ b/gdb/testsuite/gdb.dwarf2/dw2-inline-stepping-2.c
@@ -0,0 +1,49 @@ 
+/* Copyright 2019-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/>.  */
+
+/* This test relies on bar being inlined into main and foo not being
+   inlined. */
+
+volatile int global_var;
+
+static inline int  __attribute__ ((always_inline))
+bar ()
+{
+  asm ("bar_label: .globl bar_label");
+  global_var++;					/* bar inc global_var*/
+  asm ("bar_label2: .globl bar_label2");
+  return global_var;				/* bar return global_var */
+}						/* bar end */
+
+void
+foo (void)
+{						/* foo prologue */
+  asm ("foo_label: .globl foo_label");
+  global_var++;					/* foo inc global_var */
+}						/* foo end */
+
+int
+main ()
+{						/* main prologue */
+  int ans;
+  asm ("main_label: .globl main_label");
+  global_var = 0;				/* main set global_var */
+  asm ("main_label2: .globl main_label2");
+  foo ();                                        /* main call foo */
+  asm ("main_label3: .globl main_label3");
+  ans = bar ();					/* main call bar */
+  asm ("main_label4: .globl main_label4");
+  return ans;
+}						/* main end */
diff --git a/gdb/testsuite/gdb.dwarf2/dw2-inline-stepping-2.exp b/gdb/testsuite/gdb.dwarf2/dw2-inline-stepping-2.exp
new file mode 100644
index 00000000000..a97a1bfc44b
--- /dev/null
+++ b/gdb/testsuite/gdb.dwarf2/dw2-inline-stepping-2.exp
@@ -0,0 +1,127 @@ 
+# Copyright 2019-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/>.
+#
+# This test checks that we stop at an inline callsite when returning
+# from a non-inlined function. Specifically when the first line of the
+# inline function does not have is_stmt set.
+
+load_lib dwarf.exp
+
+# This test can only be run on targets which support DWARF-2 and use gas.
+require dwarf2_support
+
+# The .c files use __attribute__.
+require is_c_compiler_gcc
+
+standard_testfile .c .S
+
+set asm_file [standard_output_file $srcfile2]
+Dwarf::assemble $asm_file {
+    global srcdir subdir srcfile srcfile2
+    declare_labels ranges_label lines_label bar_prog
+
+    lassign [function_range main [list ${srcdir}/${subdir}/$srcfile]] \
+	main_start main_len
+    set main_end "$main_start + $main_len"
+    lassign [function_range foo [list ${srcdir}/${subdir}/$srcfile]] \
+	foo_start foo_len
+    set foo_end "$foo_start + $foo_len"
+
+    set bar_call_line [gdb_get_line_number "main call bar"]
+
+    cu {} {
+	compile_unit {
+	    {language @DW_LANG_C}
+	    {name dw2-inline-stepping-2.c}
+	    {low_pc 0 addr}
+	    {stmt_list ${lines_label} DW_FORM_sec_offset}
+	    {ranges ${ranges_label} DW_FORM_sec_offset}
+	} {
+	    subprogram {
+		{external 1 flag}
+		{MACRO_AT_func {foo}}
+	    }
+	    bar_prog: subprogram {
+		{name bar}
+		{inline 3 data1}
+	    }
+	    subprogram {
+		{external 1 flag}
+		{name main}
+		{low_pc $main_start addr}
+		{high_pc "$main_start + $main_len" addr}
+	    } {
+		inlined_subroutine {
+		    {abstract_origin %$bar_prog}
+		    {low_pc main_label3 addr}
+		    {high_pc main_label4 addr}
+		    {call_file 1 data1}
+		    {call_line $bar_call_line data1}
+		}
+	    }
+	}
+    }
+
+    lines {version 2} lines_label {
+	include_dir "${srcdir}/${subdir}"
+	file_name "$srcfile" 1
+
+	program {
+	    DW_LNE_set_address foo_label
+	    line [gdb_get_line_number "foo inc global_var"]
+	    DW_LNS_copy
+	    DW_LNE_set_address $foo_end
+	    DW_LNE_end_sequence
+
+	    DW_LNE_set_address main_label3
+	    line [gdb_get_line_number "bar inc global_var"]
+	    DW_LNS_negate_stmt
+	    DW_LNS_copy
+
+	    DW_LNS_negate_stmt
+	    DW_LNE_set_address bar_label2
+	    line [gdb_get_line_number "bar return global_var"]
+	    DW_LNS_copy
+
+	    DW_LNE_set_address main_label4
+	    line [gdb_get_line_number "return ans"]
+	    DW_LNS_copy
+	    DW_LNE_set_address $main_end
+	    DW_LNE_end_sequence
+	}
+    }
+
+    ranges {is_64 [is_64_target]} {
+	ranges_label: sequence {
+	    range ${main_start} ${main_end}
+	    range ${foo_start} ${foo_end}
+	}
+    }
+}
+
+if { [prepare_for_testing "failed to prepare" ${testfile} \
+	  [list $srcfile $asm_file] {nodebug}] } {
+    return -1
+}
+
+if ![runto_main] {
+    return -1
+}
+
+gdb_breakpoint "foo"
+gdb_continue_to_breakpoint "foo"
+
+# We should stop at bar callsite when returning from foo.
+gdb_test "step" ".*main call bar.*" "step to bar callsite"