Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
* [PATCH] gdb: new 'maint print frame-id' command
@ 2022-08-16 17:18 Andrew Burgess via Gdb-patches
  2022-08-17  9:21 ` Bruno Larsen via Gdb-patches
                   ` (3 more replies)
  0 siblings, 4 replies; 6+ messages in thread
From: Andrew Burgess via Gdb-patches @ 2022-08-16 17:18 UTC (permalink / raw)
  To: gdb-patches; +Cc: Andrew Burgess

When debugging a certain class of GDB bug, I often end up wanting to
know what GDB thinks the frame-id is in a particular frame.  It's
not too hard to pull this from some debug output, but I thought it
might be nice if there was a maintenance command that could tell us.

This commit adds 'maint print frame-id' which prints the frame-id of
the currently selected frame.  You can also pass a frame level number
to find the frame-id for a specific frame.

There's a new test too.
---
 gdb/NEWS                                      |  4 ++
 gdb/doc/gdb.texinfo                           | 17 +++++
 gdb/frame.c                                   | 28 ++++++++
 gdb/testsuite/gdb.base/maint-print-frame-id.c | 40 +++++++++++
 .../gdb.base/maint-print-frame-id.exp         | 68 +++++++++++++++++++
 5 files changed, 157 insertions(+)
 create mode 100644 gdb/testsuite/gdb.base/maint-print-frame-id.c
 create mode 100644 gdb/testsuite/gdb.base/maint-print-frame-id.exp

diff --git a/gdb/NEWS b/gdb/NEWS
index f2040e26c0c..49ac714efda 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -91,6 +91,10 @@ show style disassembler symbol
   The 'disassembler address' and 'disassembler symbol' styles are
   aliases for the 'address' and 'function' styles respectively.
 
+maintenance print frame-id [ LEVEL ]
+  Print GDB's internal frame-id for the frame at LEVEL.  If LEVEL is
+  not given, then print the frame-id for the currently selected frame.
+
 * Changed commands
 
 maintenance info line-table
diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
index 82cb1c8d668..ca958173b57 100644
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -40142,6 +40142,23 @@
 
 Takes an optional file parameter.
 
+@kindex maint print frame-id
+@item maint print frame-id
+@itemx maint print frame-id @var{level}
+Print @value{GDBN}'s internal frame-id for the frame at relative
+@var{level}, or for the currently selected frame when @var{level} is
+not given.
+
+If used, @var{level} should be an integer, as displayed in the
+@command{backtrace} output.
+
+@smallexample
+(@value{GDBP}) maint print frame-id
+frame-id for frame #0: {stack=0x7fffffffac70,code=0x0000000000401106,!special}
+(@value{GDBP}) maint print frame-id 2
+frame-id for frame #2: {stack=0x7fffffffac90,code=0x000000000040111c,!special}
+@end smallexample
+
 @kindex maint print registers
 @kindex maint print raw-registers
 @kindex maint print cooked-registers
diff --git a/gdb/frame.c b/gdb/frame.c
index ae45e22d613..e6bebf575ca 100644
--- a/gdb/frame.c
+++ b/gdb/frame.c
@@ -3116,6 +3116,30 @@ the rest of the stack trace."),
   },
 };
 
+/* Implement the 'maintenance print frame-id' command.  */
+
+static void
+maintenance_print_frame_id (const char *args, int from_tty)
+{
+  struct frame_info *frame;
+
+  /* Use the currently selected frame, or select a frame based on the level
+     number passed by the user.  */
+  if (args == nullptr)
+    frame = get_selected_frame ("No frame selected");
+  else
+    {
+      int level = value_as_long (parse_and_eval (args));
+      frame = find_relative_frame (get_current_frame (), &level);
+    }
+
+  /* Print the frame-id.  */
+  gdb_assert (frame != nullptr);
+  gdb_printf ("frame-id for frame #%d: %s\n",
+	      frame_relative_level (frame),
+	      get_frame_id (frame).to_string ().c_str ());
+}
+
 void _initialize_frame ();
 void
 _initialize_frame ()
@@ -3160,4 +3184,8 @@ When non-zero, frame specific internal debugging is enabled."),
 			   NULL,
 			   show_frame_debug,
 			   &setdebuglist, &showdebuglist);
+
+  add_cmd ("frame-id", class_maintenance, maintenance_print_frame_id,
+	   _("Print the current frame-id."),
+	   &maintenanceprintlist);
 }
diff --git a/gdb/testsuite/gdb.base/maint-print-frame-id.c b/gdb/testsuite/gdb.base/maint-print-frame-id.c
new file mode 100644
index 00000000000..bc9eb3000e7
--- /dev/null
+++ b/gdb/testsuite/gdb.base/maint-print-frame-id.c
@@ -0,0 +1,40 @@
+/* This testcase is part of GDB, the GNU debugger.
+
+   Copyright 2022 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
+foo (void)
+{
+  return 0;
+}
+
+int
+bar (void)
+{
+  return foo ();
+}
+
+int
+baz (void)
+{
+  return bar ();
+}
+
+int
+main (void)
+{
+  return baz ();
+}
diff --git a/gdb/testsuite/gdb.base/maint-print-frame-id.exp b/gdb/testsuite/gdb.base/maint-print-frame-id.exp
new file mode 100644
index 00000000000..395927a22c4
--- /dev/null
+++ b/gdb/testsuite/gdb.base/maint-print-frame-id.exp
@@ -0,0 +1,68 @@
+# Copyright 2022 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 the 'maint print frame-id' command.
+
+standard_testfile
+
+if {[prepare_for_testing "failed to prepare" $testfile \
+	 $srcfile {debug}]} {
+    return -1
+}
+
+if ![runto_main] then {
+    return -1
+}
+
+gdb_breakpoint foo
+gdb_continue_to_breakpoint "run to foo"
+
+proc get_frame_id { level } {
+    set id "**unknown**"
+
+    gdb_test_multiple "maint print frame-id ${level}" "" {
+	-re "^maint print frame-id\[^\r\n\]+\r\n" {
+	    exp_continue
+	}
+
+	-wrap -re "^frame-id for frame #\[0-9\]+: (\[^\r\n\]+)" {
+	    set id $expect_out(1,string)
+	    pass $gdb_test_name
+	}
+    }
+
+    return $id
+}
+
+# Get the frame-id for each frame using the frame level.
+array set ids {}
+with_test_prefix "get id by level" {
+    for { set i 0 } { $i < 4 } { incr i } {
+	set ids($i) [get_frame_id $i]
+    }
+}
+
+# Now get the frame-id of the currently selected frame, and check it
+# matches the frame-id we got earlier.  Then move up the stack,
+# selecting a new frame.
+for { set i 0 } { $i < 4 } { incr i } {
+    with_test_prefix "frame $i is current" {
+	set id [get_frame_id ""]
+	gdb_assert { [string equal $id $ids($i)] } \
+	    "check frame-id matches"
+    }
+    gdb_test "up" ".*" \
+	"move up from frame $i"
+}
-- 
2.25.4


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

* Re: [PATCH] gdb: new 'maint print frame-id' command
  2022-08-16 17:18 [PATCH] gdb: new 'maint print frame-id' command Andrew Burgess via Gdb-patches
@ 2022-08-17  9:21 ` Bruno Larsen via Gdb-patches
  2022-08-17 13:20   ` Andrew Burgess via Gdb-patches
  2022-08-17 11:07 ` Eli Zaretskii via Gdb-patches
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 6+ messages in thread
From: Bruno Larsen via Gdb-patches @ 2022-08-17  9:21 UTC (permalink / raw)
  To: Andrew Burgess, gdb-patches


On 16/08/2022 19:18, Andrew Burgess via Gdb-patches wrote:
> When debugging a certain class of GDB bug, I often end up wanting to
> know what GDB thinks the frame-id is in a particular frame.  It's
> not too hard to pull this from some debug output, but I thought it
> might be nice if there was a maintenance command that could tell us.
>
> This commit adds 'maint print frame-id' which prints the frame-id of
> the currently selected frame.  You can also pass a frame level number
> to find the frame-id for a specific frame.
>
> There's a new test too.

Hi Andrew,

Thanks for this, it coincidentally really helps my current situation! 
All the C code looks fine to me, but the texinfo failed to compile 
(details below). Fixing that, this patch looks great, I'd encourage you 
to approve your patch!

> ---
>   gdb/NEWS                                      |  4 ++
>   gdb/doc/gdb.texinfo                           | 17 +++++
>   gdb/frame.c                                   | 28 ++++++++
>   gdb/testsuite/gdb.base/maint-print-frame-id.c | 40 +++++++++++
>   .../gdb.base/maint-print-frame-id.exp         | 68 +++++++++++++++++++
>   5 files changed, 157 insertions(+)
>   create mode 100644 gdb/testsuite/gdb.base/maint-print-frame-id.c
>   create mode 100644 gdb/testsuite/gdb.base/maint-print-frame-id.exp
>
> diff --git a/gdb/NEWS b/gdb/NEWS
> index f2040e26c0c..49ac714efda 100644
> --- a/gdb/NEWS
> +++ b/gdb/NEWS
> @@ -91,6 +91,10 @@ show style disassembler symbol
>     The 'disassembler address' and 'disassembler symbol' styles are
>     aliases for the 'address' and 'function' styles respectively.
>   
> +maintenance print frame-id [ LEVEL ]
> +  Print GDB's internal frame-id for the frame at LEVEL.  If LEVEL is
> +  not given, then print the frame-id for the currently selected frame.
> +
>   * Changed commands
>   
>   maintenance info line-table
> diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
> index 82cb1c8d668..ca958173b57 100644
> --- a/gdb/doc/gdb.texinfo
> +++ b/gdb/doc/gdb.texinfo
> @@ -40142,6 +40142,23 @@
>   
>   Takes an optional file parameter.
>   
> +@kindex maint print frame-id
> +@item maint print frame-id
> +@itemx maint print frame-id @var{level}
> +Print @value{GDBN}'s internal frame-id for the frame at relative
> +@var{level}, or for the currently selected frame when @var{level} is
> +not given.
> +
> +If used, @var{level} should be an integer, as displayed in the
> +@command{backtrace} output.
> +
> +@smallexample
> +(@value{GDBP}) maint print frame-id
> +frame-id for frame #0: {stack=0x7fffffffac70,code=0x0000000000401106,!special}
> +(@value{GDBP}) maint print frame-id 2
> +frame-id for frame #2: {stack=0x7fffffffac90,code=0x000000000040111c,!special}

In this line and 2 lines above, makeinfo was complaining about misplaced 
{ and };

Looking at the example above, it looks like it should be

frame-id for frame #2: 
@{stack=0x7fffffffac90,code=0x000000000040111c,!special@}

-- 
Cheers,
Bruno


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

* Re: [PATCH] gdb: new 'maint print frame-id' command
  2022-08-16 17:18 [PATCH] gdb: new 'maint print frame-id' command Andrew Burgess via Gdb-patches
  2022-08-17  9:21 ` Bruno Larsen via Gdb-patches
@ 2022-08-17 11:07 ` Eli Zaretskii via Gdb-patches
  2022-08-17 14:11 ` Luis Machado via Gdb-patches
  2022-08-24 13:45 ` Andrew Burgess via Gdb-patches
  3 siblings, 0 replies; 6+ messages in thread
From: Eli Zaretskii via Gdb-patches @ 2022-08-17 11:07 UTC (permalink / raw)
  To: Andrew Burgess; +Cc: gdb-patches

> Date: Tue, 16 Aug 2022 18:18:22 +0100
> From: Andrew Burgess via Gdb-patches <gdb-patches@sourceware.org>
> Cc: Andrew Burgess <aburgess@redhat.com>
> 
> When debugging a certain class of GDB bug, I often end up wanting to
> know what GDB thinks the frame-id is in a particular frame.  It's
> not too hard to pull this from some debug output, but I thought it
> might be nice if there was a maintenance command that could tell us.
> 
> This commit adds 'maint print frame-id' which prints the frame-id of
> the currently selected frame.  You can also pass a frame level number
> to find the frame-id for a specific frame.
> 
> There's a new test too.
> ---
>  gdb/NEWS                                      |  4 ++
>  gdb/doc/gdb.texinfo                           | 17 +++++
>  gdb/frame.c                                   | 28 ++++++++
>  gdb/testsuite/gdb.base/maint-print-frame-id.c | 40 +++++++++++
>  .../gdb.base/maint-print-frame-id.exp         | 68 +++++++++++++++++++
>  5 files changed, 157 insertions(+)
>  create mode 100644 gdb/testsuite/gdb.base/maint-print-frame-id.c
>  create mode 100644 gdb/testsuite/gdb.base/maint-print-frame-id.exp

Thanks, the documentation parts are OK.

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

* Re: [PATCH] gdb: new 'maint print frame-id' command
  2022-08-17  9:21 ` Bruno Larsen via Gdb-patches
@ 2022-08-17 13:20   ` Andrew Burgess via Gdb-patches
  0 siblings, 0 replies; 6+ messages in thread
From: Andrew Burgess via Gdb-patches @ 2022-08-17 13:20 UTC (permalink / raw)
  To: Bruno Larsen, gdb-patches

Bruno Larsen via Gdb-patches <gdb-patches@sourceware.org> writes:

> On 16/08/2022 19:18, Andrew Burgess via Gdb-patches wrote:
>> When debugging a certain class of GDB bug, I often end up wanting to
>> know what GDB thinks the frame-id is in a particular frame.  It's
>> not too hard to pull this from some debug output, but I thought it
>> might be nice if there was a maintenance command that could tell us.
>>
>> This commit adds 'maint print frame-id' which prints the frame-id of
>> the currently selected frame.  You can also pass a frame level number
>> to find the frame-id for a specific frame.
>>
>> There's a new test too.
>
> Hi Andrew,
>
> Thanks for this, it coincidentally really helps my current situation! 
> All the C code looks fine to me, but the texinfo failed to compile 
> (details below). Fixing that, this patch looks great, I'd encourage you 
> to approve your patch!
>
>> ---
>>   gdb/NEWS                                      |  4 ++
>>   gdb/doc/gdb.texinfo                           | 17 +++++
>>   gdb/frame.c                                   | 28 ++++++++
>>   gdb/testsuite/gdb.base/maint-print-frame-id.c | 40 +++++++++++
>>   .../gdb.base/maint-print-frame-id.exp         | 68 +++++++++++++++++++
>>   5 files changed, 157 insertions(+)
>>   create mode 100644 gdb/testsuite/gdb.base/maint-print-frame-id.c
>>   create mode 100644 gdb/testsuite/gdb.base/maint-print-frame-id.exp
>>
>> diff --git a/gdb/NEWS b/gdb/NEWS
>> index f2040e26c0c..49ac714efda 100644
>> --- a/gdb/NEWS
>> +++ b/gdb/NEWS
>> @@ -91,6 +91,10 @@ show style disassembler symbol
>>     The 'disassembler address' and 'disassembler symbol' styles are
>>     aliases for the 'address' and 'function' styles respectively.
>>   
>> +maintenance print frame-id [ LEVEL ]
>> +  Print GDB's internal frame-id for the frame at LEVEL.  If LEVEL is
>> +  not given, then print the frame-id for the currently selected frame.
>> +
>>   * Changed commands
>>   
>>   maintenance info line-table
>> diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
>> index 82cb1c8d668..ca958173b57 100644
>> --- a/gdb/doc/gdb.texinfo
>> +++ b/gdb/doc/gdb.texinfo
>> @@ -40142,6 +40142,23 @@
>>   
>>   Takes an optional file parameter.
>>   
>> +@kindex maint print frame-id
>> +@item maint print frame-id
>> +@itemx maint print frame-id @var{level}
>> +Print @value{GDBN}'s internal frame-id for the frame at relative
>> +@var{level}, or for the currently selected frame when @var{level} is
>> +not given.
>> +
>> +If used, @var{level} should be an integer, as displayed in the
>> +@command{backtrace} output.
>> +
>> +@smallexample
>> +(@value{GDBP}) maint print frame-id
>> +frame-id for frame #0: {stack=0x7fffffffac70,code=0x0000000000401106,!special}
>> +(@value{GDBP}) maint print frame-id 2
>> +frame-id for frame #2: {stack=0x7fffffffac90,code=0x000000000040111c,!special}
>
> In this line and 2 lines above, makeinfo was complaining about misplaced 
> { and };
>
> Looking at the example above, it looks like it should be
>
> frame-id for frame #2: 
> @{stack=0x7fffffffac90,code=0x000000000040111c,!special@}

Thanks.  I've fixed this locally, and will include this fix when I check
this in (after giving others a chance to comment).

Thanks,
Andrew


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

* Re: [PATCH] gdb: new 'maint print frame-id' command
  2022-08-16 17:18 [PATCH] gdb: new 'maint print frame-id' command Andrew Burgess via Gdb-patches
  2022-08-17  9:21 ` Bruno Larsen via Gdb-patches
  2022-08-17 11:07 ` Eli Zaretskii via Gdb-patches
@ 2022-08-17 14:11 ` Luis Machado via Gdb-patches
  2022-08-24 13:45 ` Andrew Burgess via Gdb-patches
  3 siblings, 0 replies; 6+ messages in thread
From: Luis Machado via Gdb-patches @ 2022-08-17 14:11 UTC (permalink / raw)
  To: Andrew Burgess, gdb-patches

On 8/16/22 18:18, Andrew Burgess via Gdb-patches wrote:
> When debugging a certain class of GDB bug, I often end up wanting to
> know what GDB thinks the frame-id is in a particular frame.  It's
> not too hard to pull this from some debug output, but I thought it
> might be nice if there was a maintenance command that could tell us.
> 
> This commit adds 'maint print frame-id' which prints the frame-id of
> the currently selected frame.  You can also pass a frame level number
> to find the frame-id for a specific frame.
> 
> There's a new test too.
> ---
>   gdb/NEWS                                      |  4 ++
>   gdb/doc/gdb.texinfo                           | 17 +++++
>   gdb/frame.c                                   | 28 ++++++++
>   gdb/testsuite/gdb.base/maint-print-frame-id.c | 40 +++++++++++
>   .../gdb.base/maint-print-frame-id.exp         | 68 +++++++++++++++++++
>   5 files changed, 157 insertions(+)
>   create mode 100644 gdb/testsuite/gdb.base/maint-print-frame-id.c
>   create mode 100644 gdb/testsuite/gdb.base/maint-print-frame-id.exp
> 
> diff --git a/gdb/NEWS b/gdb/NEWS
> index f2040e26c0c..49ac714efda 100644
> --- a/gdb/NEWS
> +++ b/gdb/NEWS
> @@ -91,6 +91,10 @@ show style disassembler symbol
>     The 'disassembler address' and 'disassembler symbol' styles are
>     aliases for the 'address' and 'function' styles respectively.
>   
> +maintenance print frame-id [ LEVEL ]
> +  Print GDB's internal frame-id for the frame at LEVEL.  If LEVEL is
> +  not given, then print the frame-id for the currently selected frame.
> +
>   * Changed commands
>   
>   maintenance info line-table
> diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
> index 82cb1c8d668..ca958173b57 100644
> --- a/gdb/doc/gdb.texinfo
> +++ b/gdb/doc/gdb.texinfo
> @@ -40142,6 +40142,23 @@
>   
>   Takes an optional file parameter.
>   
> +@kindex maint print frame-id
> +@item maint print frame-id
> +@itemx maint print frame-id @var{level}
> +Print @value{GDBN}'s internal frame-id for the frame at relative
> +@var{level}, or for the currently selected frame when @var{level} is
> +not given.
> +
> +If used, @var{level} should be an integer, as displayed in the
> +@command{backtrace} output.
> +
> +@smallexample
> +(@value{GDBP}) maint print frame-id
> +frame-id for frame #0: {stack=0x7fffffffac70,code=0x0000000000401106,!special}
> +(@value{GDBP}) maint print frame-id 2
> +frame-id for frame #2: {stack=0x7fffffffac90,code=0x000000000040111c,!special}
> +@end smallexample
> +
>   @kindex maint print registers
>   @kindex maint print raw-registers
>   @kindex maint print cooked-registers
> diff --git a/gdb/frame.c b/gdb/frame.c
> index ae45e22d613..e6bebf575ca 100644
> --- a/gdb/frame.c
> +++ b/gdb/frame.c
> @@ -3116,6 +3116,30 @@ the rest of the stack trace."),
>     },
>   };
>   
> +/* Implement the 'maintenance print frame-id' command.  */
> +
> +static void
> +maintenance_print_frame_id (const char *args, int from_tty)
> +{
> +  struct frame_info *frame;
> +
> +  /* Use the currently selected frame, or select a frame based on the level
> +     number passed by the user.  */
> +  if (args == nullptr)
> +    frame = get_selected_frame ("No frame selected");
> +  else
> +    {
> +      int level = value_as_long (parse_and_eval (args));
> +      frame = find_relative_frame (get_current_frame (), &level);
> +    }
> +
> +  /* Print the frame-id.  */
> +  gdb_assert (frame != nullptr);
> +  gdb_printf ("frame-id for frame #%d: %s\n",
> +	      frame_relative_level (frame),
> +	      get_frame_id (frame).to_string ().c_str ());
> +}
> +
>   void _initialize_frame ();
>   void
>   _initialize_frame ()
> @@ -3160,4 +3184,8 @@ When non-zero, frame specific internal debugging is enabled."),
>   			   NULL,
>   			   show_frame_debug,
>   			   &setdebuglist, &showdebuglist);
> +
> +  add_cmd ("frame-id", class_maintenance, maintenance_print_frame_id,
> +	   _("Print the current frame-id."),
> +	   &maintenanceprintlist);
>   }
> diff --git a/gdb/testsuite/gdb.base/maint-print-frame-id.c b/gdb/testsuite/gdb.base/maint-print-frame-id.c
> new file mode 100644
> index 00000000000..bc9eb3000e7
> --- /dev/null
> +++ b/gdb/testsuite/gdb.base/maint-print-frame-id.c
> @@ -0,0 +1,40 @@
> +/* This testcase is part of GDB, the GNU debugger.
> +
> +   Copyright 2022 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
> +foo (void)
> +{
> +  return 0;
> +}
> +
> +int
> +bar (void)
> +{
> +  return foo ();
> +}
> +
> +int
> +baz (void)
> +{
> +  return bar ();
> +}
> +
> +int
> +main (void)
> +{
> +  return baz ();
> +}
> diff --git a/gdb/testsuite/gdb.base/maint-print-frame-id.exp b/gdb/testsuite/gdb.base/maint-print-frame-id.exp
> new file mode 100644
> index 00000000000..395927a22c4
> --- /dev/null
> +++ b/gdb/testsuite/gdb.base/maint-print-frame-id.exp
> @@ -0,0 +1,68 @@
> +# Copyright 2022 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 the 'maint print frame-id' command.
> +
> +standard_testfile
> +
> +if {[prepare_for_testing "failed to prepare" $testfile \
> +	 $srcfile {debug}]} {
> +    return -1
> +}
> +
> +if ![runto_main] then {
> +    return -1
> +}
> +
> +gdb_breakpoint foo
> +gdb_continue_to_breakpoint "run to foo"
> +
> +proc get_frame_id { level } {
> +    set id "**unknown**"
> +
> +    gdb_test_multiple "maint print frame-id ${level}" "" {
> +	-re "^maint print frame-id\[^\r\n\]+\r\n" {
> +	    exp_continue
> +	}
> +
> +	-wrap -re "^frame-id for frame #\[0-9\]+: (\[^\r\n\]+)" {
> +	    set id $expect_out(1,string)
> +	    pass $gdb_test_name
> +	}
> +    }
> +
> +    return $id
> +}
> +
> +# Get the frame-id for each frame using the frame level.
> +array set ids {}
> +with_test_prefix "get id by level" {
> +    for { set i 0 } { $i < 4 } { incr i } {
> +	set ids($i) [get_frame_id $i]
> +    }
> +}
> +
> +# Now get the frame-id of the currently selected frame, and check it
> +# matches the frame-id we got earlier.  Then move up the stack,
> +# selecting a new frame.
> +for { set i 0 } { $i < 4 } { incr i } {
> +    with_test_prefix "frame $i is current" {
> +	set id [get_frame_id ""]
> +	gdb_assert { [string equal $id $ids($i)] } \
> +	    "check frame-id matches"
> +    }
> +    gdb_test "up" ".*" \
> +	"move up from frame $i"
> +}

LGTM. I think it's very useful.

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

* Re: [PATCH] gdb: new 'maint print frame-id' command
  2022-08-16 17:18 [PATCH] gdb: new 'maint print frame-id' command Andrew Burgess via Gdb-patches
                   ` (2 preceding siblings ...)
  2022-08-17 14:11 ` Luis Machado via Gdb-patches
@ 2022-08-24 13:45 ` Andrew Burgess via Gdb-patches
  3 siblings, 0 replies; 6+ messages in thread
From: Andrew Burgess via Gdb-patches @ 2022-08-24 13:45 UTC (permalink / raw)
  To: gdb-patches

Andrew Burgess <aburgess@redhat.com> writes:

> When debugging a certain class of GDB bug, I often end up wanting to
> know what GDB thinks the frame-id is in a particular frame.  It's
> not too hard to pull this from some debug output, but I thought it
> might be nice if there was a maintenance command that could tell us.
>
> This commit adds 'maint print frame-id' which prints the frame-id of
> the currently selected frame.  You can also pass a frame level number
> to find the frame-id for a specific frame.
>
> There's a new test too.

I've now pushed this patch.

Thanks,
Andrew

> ---
>  gdb/NEWS                                      |  4 ++
>  gdb/doc/gdb.texinfo                           | 17 +++++
>  gdb/frame.c                                   | 28 ++++++++
>  gdb/testsuite/gdb.base/maint-print-frame-id.c | 40 +++++++++++
>  .../gdb.base/maint-print-frame-id.exp         | 68 +++++++++++++++++++
>  5 files changed, 157 insertions(+)
>  create mode 100644 gdb/testsuite/gdb.base/maint-print-frame-id.c
>  create mode 100644 gdb/testsuite/gdb.base/maint-print-frame-id.exp
>
> diff --git a/gdb/NEWS b/gdb/NEWS
> index f2040e26c0c..49ac714efda 100644
> --- a/gdb/NEWS
> +++ b/gdb/NEWS
> @@ -91,6 +91,10 @@ show style disassembler symbol
>    The 'disassembler address' and 'disassembler symbol' styles are
>    aliases for the 'address' and 'function' styles respectively.
>  
> +maintenance print frame-id [ LEVEL ]
> +  Print GDB's internal frame-id for the frame at LEVEL.  If LEVEL is
> +  not given, then print the frame-id for the currently selected frame.
> +
>  * Changed commands
>  
>  maintenance info line-table
> diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
> index 82cb1c8d668..ca958173b57 100644
> --- a/gdb/doc/gdb.texinfo
> +++ b/gdb/doc/gdb.texinfo
> @@ -40142,6 +40142,23 @@
>  
>  Takes an optional file parameter.
>  
> +@kindex maint print frame-id
> +@item maint print frame-id
> +@itemx maint print frame-id @var{level}
> +Print @value{GDBN}'s internal frame-id for the frame at relative
> +@var{level}, or for the currently selected frame when @var{level} is
> +not given.
> +
> +If used, @var{level} should be an integer, as displayed in the
> +@command{backtrace} output.
> +
> +@smallexample
> +(@value{GDBP}) maint print frame-id
> +frame-id for frame #0: {stack=0x7fffffffac70,code=0x0000000000401106,!special}
> +(@value{GDBP}) maint print frame-id 2
> +frame-id for frame #2: {stack=0x7fffffffac90,code=0x000000000040111c,!special}
> +@end smallexample
> +
>  @kindex maint print registers
>  @kindex maint print raw-registers
>  @kindex maint print cooked-registers
> diff --git a/gdb/frame.c b/gdb/frame.c
> index ae45e22d613..e6bebf575ca 100644
> --- a/gdb/frame.c
> +++ b/gdb/frame.c
> @@ -3116,6 +3116,30 @@ the rest of the stack trace."),
>    },
>  };
>  
> +/* Implement the 'maintenance print frame-id' command.  */
> +
> +static void
> +maintenance_print_frame_id (const char *args, int from_tty)
> +{
> +  struct frame_info *frame;
> +
> +  /* Use the currently selected frame, or select a frame based on the level
> +     number passed by the user.  */
> +  if (args == nullptr)
> +    frame = get_selected_frame ("No frame selected");
> +  else
> +    {
> +      int level = value_as_long (parse_and_eval (args));
> +      frame = find_relative_frame (get_current_frame (), &level);
> +    }
> +
> +  /* Print the frame-id.  */
> +  gdb_assert (frame != nullptr);
> +  gdb_printf ("frame-id for frame #%d: %s\n",
> +	      frame_relative_level (frame),
> +	      get_frame_id (frame).to_string ().c_str ());
> +}
> +
>  void _initialize_frame ();
>  void
>  _initialize_frame ()
> @@ -3160,4 +3184,8 @@ When non-zero, frame specific internal debugging is enabled."),
>  			   NULL,
>  			   show_frame_debug,
>  			   &setdebuglist, &showdebuglist);
> +
> +  add_cmd ("frame-id", class_maintenance, maintenance_print_frame_id,
> +	   _("Print the current frame-id."),
> +	   &maintenanceprintlist);
>  }
> diff --git a/gdb/testsuite/gdb.base/maint-print-frame-id.c b/gdb/testsuite/gdb.base/maint-print-frame-id.c
> new file mode 100644
> index 00000000000..bc9eb3000e7
> --- /dev/null
> +++ b/gdb/testsuite/gdb.base/maint-print-frame-id.c
> @@ -0,0 +1,40 @@
> +/* This testcase is part of GDB, the GNU debugger.
> +
> +   Copyright 2022 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
> +foo (void)
> +{
> +  return 0;
> +}
> +
> +int
> +bar (void)
> +{
> +  return foo ();
> +}
> +
> +int
> +baz (void)
> +{
> +  return bar ();
> +}
> +
> +int
> +main (void)
> +{
> +  return baz ();
> +}
> diff --git a/gdb/testsuite/gdb.base/maint-print-frame-id.exp b/gdb/testsuite/gdb.base/maint-print-frame-id.exp
> new file mode 100644
> index 00000000000..395927a22c4
> --- /dev/null
> +++ b/gdb/testsuite/gdb.base/maint-print-frame-id.exp
> @@ -0,0 +1,68 @@
> +# Copyright 2022 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 the 'maint print frame-id' command.
> +
> +standard_testfile
> +
> +if {[prepare_for_testing "failed to prepare" $testfile \
> +	 $srcfile {debug}]} {
> +    return -1
> +}
> +
> +if ![runto_main] then {
> +    return -1
> +}
> +
> +gdb_breakpoint foo
> +gdb_continue_to_breakpoint "run to foo"
> +
> +proc get_frame_id { level } {
> +    set id "**unknown**"
> +
> +    gdb_test_multiple "maint print frame-id ${level}" "" {
> +	-re "^maint print frame-id\[^\r\n\]+\r\n" {
> +	    exp_continue
> +	}
> +
> +	-wrap -re "^frame-id for frame #\[0-9\]+: (\[^\r\n\]+)" {
> +	    set id $expect_out(1,string)
> +	    pass $gdb_test_name
> +	}
> +    }
> +
> +    return $id
> +}
> +
> +# Get the frame-id for each frame using the frame level.
> +array set ids {}
> +with_test_prefix "get id by level" {
> +    for { set i 0 } { $i < 4 } { incr i } {
> +	set ids($i) [get_frame_id $i]
> +    }
> +}
> +
> +# Now get the frame-id of the currently selected frame, and check it
> +# matches the frame-id we got earlier.  Then move up the stack,
> +# selecting a new frame.
> +for { set i 0 } { $i < 4 } { incr i } {
> +    with_test_prefix "frame $i is current" {
> +	set id [get_frame_id ""]
> +	gdb_assert { [string equal $id $ids($i)] } \
> +	    "check frame-id matches"
> +    }
> +    gdb_test "up" ".*" \
> +	"move up from frame $i"
> +}
> -- 
> 2.25.4


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

end of thread, other threads:[~2022-08-24 13:45 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-08-16 17:18 [PATCH] gdb: new 'maint print frame-id' command Andrew Burgess via Gdb-patches
2022-08-17  9:21 ` Bruno Larsen via Gdb-patches
2022-08-17 13:20   ` Andrew Burgess via Gdb-patches
2022-08-17 11:07 ` Eli Zaretskii via Gdb-patches
2022-08-17 14:11 ` Luis Machado via Gdb-patches
2022-08-24 13:45 ` Andrew Burgess via Gdb-patches

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