Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
* [PATCH] [gdb] Notice when stepping into different file
@ 2024-08-02  5:57 Tom de Vries
  2024-08-02  7:15 ` Kévin Le Gouguec
  2024-08-05  0:44 ` Kevin Buettner
  0 siblings, 2 replies; 3+ messages in thread
From: Tom de Vries @ 2024-08-02  5:57 UTC (permalink / raw)
  To: gdb-patches

Consider the following test-case:
...
$ cat test.c
int var;

int
foo (void)
{
  var = 1;
}

int
main ()
{
  return foo ();
}
$ cat test.h
  return 1;
$ gcc test.c -g
...

When stepping through the test-case, gdb doesn't make it explicit that line 1
is not in test.c:
...
Temporary breakpoint 1, main () at test.c:13
13	  return foo ();
(gdb) step
foo () at test.c:6
6	  var = 1;
(gdb) n
1	  return 1;
(gdb)
8	}
(gdb)
...
which makes it easy to misinterpret the output.

This is with the default "print frame-info" == auto, with documented
behaviour [1]:
...
stepi will switch between source-line and source-and-location depending on the
program counter.
...

What is actually implemented is that source-line is used unless stepping into
or out of a function.

The problem can be worked around by using
"set print frame-info source-and-location", but that's a bit verbose.

Instead, change the behaviour of "print frame-info" == auto to also use
source-and-location when stepping into another file, which gets us:
...
(gdb) n
foo () at test.h:1
1	  return 1;
...

Tested on x86_64-linux.

PR gdb/32011
Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=32011

[1] https://sourceware.org/gdb/current/onlinedocs/gdb.html/Print-Settings.html#index-set-print-frame_002dinfo
---
 gdb/infrun.c                                  | 18 ++++++++--
 gdb/testsuite/gdb.base/step-into-other-file.c | 31 ++++++++++++++++
 .../gdb.base/step-into-other-file.exp         | 36 +++++++++++++++++++
 gdb/testsuite/gdb.base/step-into-other-file.h | 18 ++++++++++
 4 files changed, 100 insertions(+), 3 deletions(-)
 create mode 100644 gdb/testsuite/gdb.base/step-into-other-file.c
 create mode 100644 gdb/testsuite/gdb.base/step-into-other-file.exp
 create mode 100644 gdb/testsuite/gdb.base/step-into-other-file.h

diff --git a/gdb/infrun.c b/gdb/infrun.c
index 06b454bf78f..05e81a08e03 100644
--- a/gdb/infrun.c
+++ b/gdb/infrun.c
@@ -9296,12 +9296,24 @@ print_stop_location (const target_waitstatus &ws)
 	  && (tp->control.step_start_function
 	      == find_pc_function (tp->stop_pc ())))
 	{
-	  /* Finished step, just print source line.  */
-	  source_flag = SRC_LINE;
+	  symtab_and_line sal = find_frame_sal (get_selected_frame (nullptr));
+	  if (sal.symtab != tp->current_symtab)
+	    {
+	      /* Finished step in same frame but into different file, print
+		 location and source line.  */
+	      source_flag = SRC_AND_LOC;
+	    }
+	  else
+	    {
+	      /* Finished step in same frame and same file, just print source
+		 line.  */
+	      source_flag = SRC_LINE;
+	    }
 	}
       else
 	{
-	  /* Print location and source line.  */
+	  /* Finished step into different frame, print location and source
+	     line.  */
 	  source_flag = SRC_AND_LOC;
 	}
       break;
diff --git a/gdb/testsuite/gdb.base/step-into-other-file.c b/gdb/testsuite/gdb.base/step-into-other-file.c
new file mode 100644
index 00000000000..5ec7c332739
--- /dev/null
+++ b/gdb/testsuite/gdb.base/step-into-other-file.c
@@ -0,0 +1,31 @@
+/* This testcase is part of GDB, the GNU debugger.
+
+   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/>.  */
+
+int var;
+
+int
+foo (void)
+{
+  var = 1;
+#include "step-into-other-file.h"
+}
+
+int
+main ()
+{
+  return foo ();
+}
diff --git a/gdb/testsuite/gdb.base/step-into-other-file.exp b/gdb/testsuite/gdb.base/step-into-other-file.exp
new file mode 100644
index 00000000000..f0e8c3f0870
--- /dev/null
+++ b/gdb/testsuite/gdb.base/step-into-other-file.exp
@@ -0,0 +1,36 @@
+# Copyright (C) 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/>.
+
+# Check that when stepping into another file, the file is shown.
+
+standard_testfile .c .h
+
+set flags {}
+lappend flags debug
+lappend_include_file flags $srcdir/$subdir/$srcfile2
+
+if { [prepare_for_testing "failed to prepare" $testfile $srcfile \
+	  $flags] == -1 } {
+    return -1
+}
+
+if ![runto_main] {
+    return -1
+}
+
+gdb_test step $srcfile:$decimal\r\n.*
+
+# Regression test for PR32011.
+gdb_test next $srcfile2:$decimal\r\n.*
diff --git a/gdb/testsuite/gdb.base/step-into-other-file.h b/gdb/testsuite/gdb.base/step-into-other-file.h
new file mode 100644
index 00000000000..60b481670f7
--- /dev/null
+++ b/gdb/testsuite/gdb.base/step-into-other-file.h
@@ -0,0 +1,18 @@
+/* This testcase is part of GDB, the GNU debugger.
+
+   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/>.  */
+
+  return 1;

base-commit: 94223333026f06dc5d78266dd9b6082544641f07
-- 
2.35.3


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] [gdb] Notice when stepping into different file
  2024-08-02  5:57 [PATCH] [gdb] Notice when stepping into different file Tom de Vries
@ 2024-08-02  7:15 ` Kévin Le Gouguec
  2024-08-05  0:44 ` Kevin Buettner
  1 sibling, 0 replies; 3+ messages in thread
From: Kévin Le Gouguec @ 2024-08-02  7:15 UTC (permalink / raw)
  To: Tom de Vries; +Cc: gdb-patches

Tom de Vries <tdevries@suse.de> writes:

> Consider the following test-case:
> ...
> $ cat test.c
> int var;
>
> int
> foo (void)
> {
>   var = 1;
> }

That puzzled me for a few seconds, until I looked at the patch:

> diff --git a/gdb/testsuite/gdb.base/step-into-other-file.c b/gdb/testsuite/gdb.base/step-into-other-file.c
> new file mode 100644
> index 00000000000..5ec7c332739
> --- /dev/null
> +++ b/gdb/testsuite/gdb.base/step-into-other-file.c

> +int var;
> +
> +int
> +foo (void)
> +{
> +  var = 1;
> +#include "step-into-other-file.h"
> +}

Is this Git's core.commentChar acting up?  FWIW, I've found ';' to be a
less fraught comment starter, but YMMV (indenting code samples also
works around the issue, if memory serves).

Changes themselves look good to me in principle, tho not the most
knowledgeable in that area.

Reviewed-By: Kévin Le Gouguec <legouguec@adacore.com>

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] [gdb] Notice when stepping into different file
  2024-08-02  5:57 [PATCH] [gdb] Notice when stepping into different file Tom de Vries
  2024-08-02  7:15 ` Kévin Le Gouguec
@ 2024-08-05  0:44 ` Kevin Buettner
  1 sibling, 0 replies; 3+ messages in thread
From: Kevin Buettner @ 2024-08-05  0:44 UTC (permalink / raw)
  To: Tom de Vries; +Cc: gdb-patches

On Fri,  2 Aug 2024 07:57:30 +0200
Tom de Vries <tdevries@suse.de> wrote:

> Instead, change the behaviour of "print frame-info" == auto to also use
> source-and-location when stepping into another file, which gets us:
> ...
> (gdb) n
> foo () at test.h:1
> 1	  return 1;
> ...

LGTM.  (Aside from git eating the #include line in the commit remarks.)

Reviewed-by: Kevin Buettner <kevinb@redhat.com>


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2024-08-05  0:45 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-08-02  5:57 [PATCH] [gdb] Notice when stepping into different file Tom de Vries
2024-08-02  7:15 ` Kévin Le Gouguec
2024-08-05  0:44 ` Kevin Buettner

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox