Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
* [PATCH] [gdb/exp] Fix ns var lookup when stopped at inlined fn call
@ 2026-06-04 11:19 Tom de Vries
  2026-06-18  7:54 ` [PING][PATCH] " Tom de Vries
  0 siblings, 1 reply; 3+ messages in thread
From: Tom de Vries @ 2026-06-04 11:19 UTC (permalink / raw)
  To: gdb-patches

Consider test.c:
...
     1	namespace mod_a {
     2	  int xxx = 10;
     3	}
     4
     5	static inline int __attribute__((always_inline))
     6	inlined () {
     7	  return 0;
     8	}
     9
    10	int main () {
    11	  using namespace mod_a;
    12	  int res = inlined ();
    13	  return res + xxx;
    14	}
...
compiled with "g++ test.c -g".

Trying to print variable xxx at line 12 fails:
...
$ gdb -q -batch a.out -ex start -ex "p xxx"
  ...
Temporary breakpoint 1, main () at test.c:12
12	  int res = inlined ();
No symbol "xxx" in current context.
...

The problem is here in function using_direct::valid_line:
...
      CORE_ADDR curr_pc = get_frame_pc (get_selected_frame (nullptr));
      symtab_and_line curr_sal = find_sal_for_pc (curr_pc, 0);
      return (decl_line <= curr_sal.line)
	     || (decl_line >= boundary);
...
where we're trying to decide whether "using namespace mod_a" is applicable.

The decl_line is 11, as expected.

If curr_sal.line were 12, decl_line <= curr_sal.line would be true, and
using_direct::valid_line would return true.

But instead, curr_sal.line is 7.

This is sort of correct, the current PC maps to that line.  It's just that gdb
steps into inlined functions in two steps, each with identical PC:
- once stopping at the call site (line 12 in this case)
- once stopping at the PC line (line 7 in this case)

The function using_direct::valid_line doesn't apply this logic, and
consequently line 7 is used for both cases.

Fix this by using find_frame_sal instead.

Tested on x86_64-linux.

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=34201
---
 gdb/namespace.c                     |  4 +--
 gdb/testsuite/gdb.cp/ns-inlined.c   | 41 +++++++++++++++++++++++
 gdb/testsuite/gdb.cp/ns-inlined.exp | 52 +++++++++++++++++++++++++++++
 3 files changed, 95 insertions(+), 2 deletions(-)
 create mode 100644 gdb/testsuite/gdb.cp/ns-inlined.c
 create mode 100644 gdb/testsuite/gdb.cp/ns-inlined.exp

diff --git a/gdb/namespace.c b/gdb/namespace.c
index 5b5749369ee..fec5e5cf67d 100644
--- a/gdb/namespace.c
+++ b/gdb/namespace.c
@@ -111,8 +111,8 @@ using_direct::valid_line (unsigned int boundary) const
 {
   try
     {
-      CORE_ADDR curr_pc = get_frame_pc (get_selected_frame (nullptr));
-      symtab_and_line curr_sal = find_sal_for_pc (curr_pc, 0);
+      frame_info_ptr frame = get_selected_frame ();
+      symtab_and_line curr_sal = find_frame_sal (frame);
       return (decl_line <= curr_sal.line)
 	     || (decl_line >= boundary);
     }
diff --git a/gdb/testsuite/gdb.cp/ns-inlined.c b/gdb/testsuite/gdb.cp/ns-inlined.c
new file mode 100644
index 00000000000..26e72f6ba3f
--- /dev/null
+++ b/gdb/testsuite/gdb.cp/ns-inlined.c
@@ -0,0 +1,41 @@
+/* This testcase is part of GDB, the GNU debugger.
+
+   Copyright 2026 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/>.  */
+
+namespace mod_a
+{
+  int xxx = 10;
+}
+
+static inline int __attribute__((always_inline))
+inlined ()
+{
+  return 0;
+}
+
+static void
+foo ()
+{
+}
+
+int
+main ()
+{
+  foo ();                   /* stop 1.  */
+  using namespace mod_a;
+  int res = inlined ();     /* stop 2.  */
+  return res + xxx;         /* stop 3.  */
+}
diff --git a/gdb/testsuite/gdb.cp/ns-inlined.exp b/gdb/testsuite/gdb.cp/ns-inlined.exp
new file mode 100644
index 00000000000..90cfbc0ff23
--- /dev/null
+++ b/gdb/testsuite/gdb.cp/ns-inlined.exp
@@ -0,0 +1,52 @@
+# Copyright 2026 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/>.
+
+standard_testfile .c
+
+set stop2_line [gdb_get_line_number "stop 2"]
+set stop3_line [gdb_get_line_number "stop 3"]
+
+if {[prepare_for_testing "failed to prepare" $testfile $srcfile \
+	 {debug c++}]} {
+    return
+}
+
+if {![runto_main]} {
+    return
+}
+
+with_test_prefix "stop 1" {
+    gdb_test "print xxx" \
+	[string_to_regexp {No symbol "xxx" in current context.}]
+}
+
+gdb_test "next" \
+    "\r\n$stop2_line\t\[^\r\n\]+" \
+    "next to stop 2"
+
+with_test_prefix "stop 2" {
+    # Regression test for PR exp/34201.
+    gdb_test "print xxx" \
+	"$valnum_re = 10"
+}
+
+gdb_test "next" \
+    "\r\n$stop3_line\t\[^\r\n\]+" \
+    "next to stop 3"
+
+with_test_prefix "stop 3" {
+    gdb_test "print xxx" \
+	"$valnum_re = 10"
+}

base-commit: 48997323b0f929d3255a4845f8a3800c6c00a53a
-- 
2.51.0


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

* [PING][PATCH] [gdb/exp] Fix ns var lookup when stopped at inlined fn call
  2026-06-04 11:19 [PATCH] [gdb/exp] Fix ns var lookup when stopped at inlined fn call Tom de Vries
@ 2026-06-18  7:54 ` Tom de Vries
  2026-07-13 17:10   ` Tom de Vries
  0 siblings, 1 reply; 3+ messages in thread
From: Tom de Vries @ 2026-06-18  7:54 UTC (permalink / raw)
  To: gdb-patches

On 6/4/26 1:19 PM, Tom de Vries wrote:
> Consider test.c:
> ...
>       1	namespace mod_a {
>       2	  int xxx = 10;
>       3	}
>       4
>       5	static inline int __attribute__((always_inline))
>       6	inlined () {
>       7	  return 0;
>       8	}
>       9
>      10	int main () {
>      11	  using namespace mod_a;
>      12	  int res = inlined ();
>      13	  return res + xxx;
>      14	}
> ...
> compiled with "g++ test.c -g".
> 
> Trying to print variable xxx at line 12 fails:
> ...
> $ gdb -q -batch a.out -ex start -ex "p xxx"
>    ...
> Temporary breakpoint 1, main () at test.c:12
> 12	  int res = inlined ();
> No symbol "xxx" in current context.
> ...
> 
> The problem is here in function using_direct::valid_line:
> ...
>        CORE_ADDR curr_pc = get_frame_pc (get_selected_frame (nullptr));
>        symtab_and_line curr_sal = find_sal_for_pc (curr_pc, 0);
>        return (decl_line <= curr_sal.line)
> 	     || (decl_line >= boundary);
> ...
> where we're trying to decide whether "using namespace mod_a" is applicable.
> 
> The decl_line is 11, as expected.
> 
> If curr_sal.line were 12, decl_line <= curr_sal.line would be true, and
> using_direct::valid_line would return true.
> 
> But instead, curr_sal.line is 7.
> 
> This is sort of correct, the current PC maps to that line.  It's just that gdb
> steps into inlined functions in two steps, each with identical PC:
> - once stopping at the call site (line 12 in this case)
> - once stopping at the PC line (line 7 in this case)
> 
> The function using_direct::valid_line doesn't apply this logic, and
> consequently line 7 is used for both cases.
> 
> Fix this by using find_frame_sal instead.
> 

Ping.

Thanks,
- Tom

> Tested on x86_64-linux.
> 
> Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=34201
> ---
>   gdb/namespace.c                     |  4 +--
>   gdb/testsuite/gdb.cp/ns-inlined.c   | 41 +++++++++++++++++++++++
>   gdb/testsuite/gdb.cp/ns-inlined.exp | 52 +++++++++++++++++++++++++++++
>   3 files changed, 95 insertions(+), 2 deletions(-)
>   create mode 100644 gdb/testsuite/gdb.cp/ns-inlined.c
>   create mode 100644 gdb/testsuite/gdb.cp/ns-inlined.exp
> 
> diff --git a/gdb/namespace.c b/gdb/namespace.c
> index 5b5749369ee..fec5e5cf67d 100644
> --- a/gdb/namespace.c
> +++ b/gdb/namespace.c
> @@ -111,8 +111,8 @@ using_direct::valid_line (unsigned int boundary) const
>   {
>     try
>       {
> -      CORE_ADDR curr_pc = get_frame_pc (get_selected_frame (nullptr));
> -      symtab_and_line curr_sal = find_sal_for_pc (curr_pc, 0);
> +      frame_info_ptr frame = get_selected_frame ();
> +      symtab_and_line curr_sal = find_frame_sal (frame);
>         return (decl_line <= curr_sal.line)
>   	     || (decl_line >= boundary);
>       }
> diff --git a/gdb/testsuite/gdb.cp/ns-inlined.c b/gdb/testsuite/gdb.cp/ns-inlined.c
> new file mode 100644
> index 00000000000..26e72f6ba3f
> --- /dev/null
> +++ b/gdb/testsuite/gdb.cp/ns-inlined.c
> @@ -0,0 +1,41 @@
> +/* This testcase is part of GDB, the GNU debugger.
> +
> +   Copyright 2026 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/>.  */
> +
> +namespace mod_a
> +{
> +  int xxx = 10;
> +}
> +
> +static inline int __attribute__((always_inline))
> +inlined ()
> +{
> +  return 0;
> +}
> +
> +static void
> +foo ()
> +{
> +}
> +
> +int
> +main ()
> +{
> +  foo ();                   /* stop 1.  */
> +  using namespace mod_a;
> +  int res = inlined ();     /* stop 2.  */
> +  return res + xxx;         /* stop 3.  */
> +}
> diff --git a/gdb/testsuite/gdb.cp/ns-inlined.exp b/gdb/testsuite/gdb.cp/ns-inlined.exp
> new file mode 100644
> index 00000000000..90cfbc0ff23
> --- /dev/null
> +++ b/gdb/testsuite/gdb.cp/ns-inlined.exp
> @@ -0,0 +1,52 @@
> +# Copyright 2026 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/>.
> +
> +standard_testfile .c
> +
> +set stop2_line [gdb_get_line_number "stop 2"]
> +set stop3_line [gdb_get_line_number "stop 3"]
> +
> +if {[prepare_for_testing "failed to prepare" $testfile $srcfile \
> +	 {debug c++}]} {
> +    return
> +}
> +
> +if {![runto_main]} {
> +    return
> +}
> +
> +with_test_prefix "stop 1" {
> +    gdb_test "print xxx" \
> +	[string_to_regexp {No symbol "xxx" in current context.}]
> +}
> +
> +gdb_test "next" \
> +    "\r\n$stop2_line\t\[^\r\n\]+" \
> +    "next to stop 2"
> +
> +with_test_prefix "stop 2" {
> +    # Regression test for PR exp/34201.
> +    gdb_test "print xxx" \
> +	"$valnum_re = 10"
> +}
> +
> +gdb_test "next" \
> +    "\r\n$stop3_line\t\[^\r\n\]+" \
> +    "next to stop 3"
> +
> +with_test_prefix "stop 3" {
> +    gdb_test "print xxx" \
> +	"$valnum_re = 10"
> +}
> 
> base-commit: 48997323b0f929d3255a4845f8a3800c6c00a53a


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

* Re: [PING][PATCH] [gdb/exp] Fix ns var lookup when stopped at inlined fn call
  2026-06-18  7:54 ` [PING][PATCH] " Tom de Vries
@ 2026-07-13 17:10   ` Tom de Vries
  0 siblings, 0 replies; 3+ messages in thread
From: Tom de Vries @ 2026-07-13 17:10 UTC (permalink / raw)
  To: gdb-patches

On 6/18/26 9:54 AM, Tom de Vries wrote:
> On 6/4/26 1:19 PM, Tom de Vries wrote:
>> Consider test.c:
>> ...
>>       1    namespace mod_a {
>>       2      int xxx = 10;
>>       3    }
>>       4
>>       5    static inline int __attribute__((always_inline))
>>       6    inlined () {
>>       7      return 0;
>>       8    }
>>       9
>>      10    int main () {
>>      11      using namespace mod_a;
>>      12      int res = inlined ();
>>      13      return res + xxx;
>>      14    }
>> ...
>> compiled with "g++ test.c -g".
>>
>> Trying to print variable xxx at line 12 fails:
>> ...
>> $ gdb -q -batch a.out -ex start -ex "p xxx"
>>    ...
>> Temporary breakpoint 1, main () at test.c:12
>> 12      int res = inlined ();
>> No symbol "xxx" in current context.
>> ...
>>
>> The problem is here in function using_direct::valid_line:
>> ...
>>        CORE_ADDR curr_pc = get_frame_pc (get_selected_frame (nullptr));
>>        symtab_and_line curr_sal = find_sal_for_pc (curr_pc, 0);
>>        return (decl_line <= curr_sal.line)
>>          || (decl_line >= boundary);
>> ...
>> where we're trying to decide whether "using namespace mod_a" is 
>> applicable.
>>
>> The decl_line is 11, as expected.
>>
>> If curr_sal.line were 12, decl_line <= curr_sal.line would be true, and
>> using_direct::valid_line would return true.
>>
>> But instead, curr_sal.line is 7.
>>
>> This is sort of correct, the current PC maps to that line.  It's just 
>> that gdb
>> steps into inlined functions in two steps, each with identical PC:
>> - once stopping at the call site (line 12 in this case)
>> - once stopping at the PC line (line 7 in this case)
>>
>> The function using_direct::valid_line doesn't apply this logic, and
>> consequently line 7 is used for both cases.
>>
>> Fix this by using find_frame_sal instead.
>>
> 
> Ping.
> 

I've pushed this, with a trivial update to the test-case to add an xfail 
for GCC PR debug/108716.

Thanks,
- Tom

> Thanks,
> - Tom
> 
>> Tested on x86_64-linux.
>>
>> Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=34201
>> ---
>>   gdb/namespace.c                     |  4 +--
>>   gdb/testsuite/gdb.cp/ns-inlined.c   | 41 +++++++++++++++++++++++
>>   gdb/testsuite/gdb.cp/ns-inlined.exp | 52 +++++++++++++++++++++++++++++
>>   3 files changed, 95 insertions(+), 2 deletions(-)
>>   create mode 100644 gdb/testsuite/gdb.cp/ns-inlined.c
>>   create mode 100644 gdb/testsuite/gdb.cp/ns-inlined.exp
>>
>> diff --git a/gdb/namespace.c b/gdb/namespace.c
>> index 5b5749369ee..fec5e5cf67d 100644
>> --- a/gdb/namespace.c
>> +++ b/gdb/namespace.c
>> @@ -111,8 +111,8 @@ using_direct::valid_line (unsigned int boundary) 
>> const
>>   {
>>     try
>>       {
>> -      CORE_ADDR curr_pc = get_frame_pc (get_selected_frame (nullptr));
>> -      symtab_and_line curr_sal = find_sal_for_pc (curr_pc, 0);
>> +      frame_info_ptr frame = get_selected_frame ();
>> +      symtab_and_line curr_sal = find_frame_sal (frame);
>>         return (decl_line <= curr_sal.line)
>>            || (decl_line >= boundary);
>>       }
>> diff --git a/gdb/testsuite/gdb.cp/ns-inlined.c b/gdb/testsuite/gdb.cp/ 
>> ns-inlined.c
>> new file mode 100644
>> index 00000000000..26e72f6ba3f
>> --- /dev/null
>> +++ b/gdb/testsuite/gdb.cp/ns-inlined.c
>> @@ -0,0 +1,41 @@
>> +/* This testcase is part of GDB, the GNU debugger.
>> +
>> +   Copyright 2026 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/>.  */
>> +
>> +namespace mod_a
>> +{
>> +  int xxx = 10;
>> +}
>> +
>> +static inline int __attribute__((always_inline))
>> +inlined ()
>> +{
>> +  return 0;
>> +}
>> +
>> +static void
>> +foo ()
>> +{
>> +}
>> +
>> +int
>> +main ()
>> +{
>> +  foo ();                   /* stop 1.  */
>> +  using namespace mod_a;
>> +  int res = inlined ();     /* stop 2.  */
>> +  return res + xxx;         /* stop 3.  */
>> +}
>> diff --git a/gdb/testsuite/gdb.cp/ns-inlined.exp b/gdb/testsuite/ 
>> gdb.cp/ns-inlined.exp
>> new file mode 100644
>> index 00000000000..90cfbc0ff23
>> --- /dev/null
>> +++ b/gdb/testsuite/gdb.cp/ns-inlined.exp
>> @@ -0,0 +1,52 @@
>> +# Copyright 2026 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/>.
>> +
>> +standard_testfile .c
>> +
>> +set stop2_line [gdb_get_line_number "stop 2"]
>> +set stop3_line [gdb_get_line_number "stop 3"]
>> +
>> +if {[prepare_for_testing "failed to prepare" $testfile $srcfile \
>> +     {debug c++}]} {
>> +    return
>> +}
>> +
>> +if {![runto_main]} {
>> +    return
>> +}
>> +
>> +with_test_prefix "stop 1" {
>> +    gdb_test "print xxx" \
>> +    [string_to_regexp {No symbol "xxx" in current context.}]
>> +}
>> +
>> +gdb_test "next" \
>> +    "\r\n$stop2_line\t\[^\r\n\]+" \
>> +    "next to stop 2"
>> +
>> +with_test_prefix "stop 2" {
>> +    # Regression test for PR exp/34201.
>> +    gdb_test "print xxx" \
>> +    "$valnum_re = 10"
>> +}
>> +
>> +gdb_test "next" \
>> +    "\r\n$stop3_line\t\[^\r\n\]+" \
>> +    "next to stop 3"
>> +
>> +with_test_prefix "stop 3" {
>> +    gdb_test "print xxx" \
>> +    "$valnum_re = 10"
>> +}
>>
>> base-commit: 48997323b0f929d3255a4845f8a3800c6c00a53a
> 


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

end of thread, other threads:[~2026-07-13 17:10 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-06-04 11:19 [PATCH] [gdb/exp] Fix ns var lookup when stopped at inlined fn call Tom de Vries
2026-06-18  7:54 ` [PING][PATCH] " Tom de Vries
2026-07-13 17:10   ` Tom de Vries

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