public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] gdb/python: add gdb.history_count function
@ 2022-01-24 22:34 Andrew Burgess
  2022-01-26 15:13 ` Tom Tromey
  0 siblings, 1 reply; 3+ messages in thread
From: Andrew Burgess @ 2022-01-24 22:34 UTC (permalink / raw)
  To: gdb-patches; +Cc: Andrew Burgess

Add a new function gdb.history_count to the Python api, this function
returns an integer, the number of items in GDB's value history.

This is useful if you want to pull items from the history by their
absolute number, for example, if you wanted to show a complete history
list.  Previously we could figure out how many items are in the
history list by trying to fetch the items, and then catching the
exception when the item is not available, but having this function
seems nicer.
---
 gdb/NEWS                              |  3 +++
 gdb/doc/python.texi                   |  5 +++++
 gdb/python/py-value.c                 |  8 ++++++++
 gdb/python/python-internal.h          |  1 +
 gdb/python/python.c                   |  2 ++
 gdb/testsuite/gdb.python/py-value.exp | 12 ++++++++++++
 gdb/value.c                           |  8 ++++++++
 gdb/value.h                           |  4 ++++
 8 files changed, 43 insertions(+)

diff --git a/gdb/NEWS b/gdb/NEWS
index 8c13cefb22f..057cd9af6c5 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -109,6 +109,9 @@ show debug lin-lwp
      integer, the index of the new item in the history list, is
      returned.
 
+  ** New function gdb.history_count(), which returns the number of
+     values in GDB's value history.
+
   ** New gdb.events.gdb_exiting event.  This event is called with a
      gdb.GdbExitingEvent object which has the read-only attribute
      'exit_code', which contains the value of the GDB exit code.  This
diff --git a/gdb/doc/python.texi b/gdb/doc/python.texi
index f02380a27f4..f7d9e92571d 100644
--- a/gdb/doc/python.texi
+++ b/gdb/doc/python.texi
@@ -361,6 +361,11 @@
 user convenient access to those values via CLI history facilities.
 @end defun
 
+@defun gdb.history_count ()
+Return an integer indicating the number of values in @value{GDBN}'s
+value history (@pxref{Value History}).
+@end defun
+
 @findex gdb.convenience_variable
 @defun gdb.convenience_variable (name)
 Return the value of the convenience variable (@pxref{Convenience
diff --git a/gdb/python/py-value.c b/gdb/python/py-value.c
index 70b33d5a27b..2f7e0f29c8a 100644
--- a/gdb/python/py-value.c
+++ b/gdb/python/py-value.c
@@ -2009,6 +2009,14 @@ gdbpy_add_history (PyObject *self, PyObject *args)
   return nullptr;
 }
 
+/* Return an integer, the number of items in GDB's history.  */
+
+PyObject *
+gdbpy_history_count (PyObject *self, PyObject *args)
+{
+  return gdb_py_object_from_ulongest (value_history_count ()).release ();
+}
+
 /* Return the value of a convenience variable.  */
 PyObject *
 gdbpy_convenience_variable (PyObject *self, PyObject *args)
diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h
index 583989c5a6d..979d203f8ec 100644
--- a/gdb/python/python-internal.h
+++ b/gdb/python/python-internal.h
@@ -412,6 +412,7 @@ extern enum ext_lang_rc gdbpy_get_matching_xmethod_workers
 \f
 PyObject *gdbpy_history (PyObject *self, PyObject *args);
 PyObject *gdbpy_add_history (PyObject *self, PyObject *args);
+extern PyObject *gdbpy_history_count (PyObject *self, PyObject *args);
 PyObject *gdbpy_convenience_variable (PyObject *self, PyObject *args);
 PyObject *gdbpy_set_convenience_variable (PyObject *self, PyObject *args);
 PyObject *gdbpy_breakpoints (PyObject *, PyObject *);
diff --git a/gdb/python/python.c b/gdb/python/python.c
index 4dcda53d9ab..0df632f0de9 100644
--- a/gdb/python/python.c
+++ b/gdb/python/python.c
@@ -2198,6 +2198,8 @@ PyMethodDef python_GdbMethods[] =
     "Get a value from history" },
   { "add_history", gdbpy_add_history, METH_VARARGS,
     "Add a value to the value history list" },
+  { "history_count", gdbpy_history_count, METH_NOARGS,
+    "Return an integer, the number of values in GDB's value history" },
   { "execute", (PyCFunction) execute_gdb_command, METH_VARARGS | METH_KEYWORDS,
     "execute (command [, from_tty] [, to_string]) -> [String]\n\
 Evaluate command, a string, as a gdb CLI command.  Optionally returns\n\
diff --git a/gdb/testsuite/gdb.python/py-value.exp b/gdb/testsuite/gdb.python/py-value.exp
index 9ee3c5f223d..60039c9373f 100644
--- a/gdb/testsuite/gdb.python/py-value.exp
+++ b/gdb/testsuite/gdb.python/py-value.exp
@@ -647,6 +647,17 @@ proc test_value_sub_classes {} {
 	"check printing of MyValue when initiaized with a type"
 }
 
+# Test the history count.  This must be the first thing called after
+# starting GDB as it depends on there being nothing in the value
+# history.
+proc test_history_count {} {
+    for { set i 0 } { $i < 5 } { incr i } {
+	gdb_test "python print('history count is %d' % gdb.history_count())" \
+	    "history count is $i" "history count is $i"
+	gdb_test "print $i" " = $i"
+    }
+}
+
 # Build C version of executable.  C++ is built later.
 if { [build_inferior "${binfile}" "c"] < 0 } {
     return -1
@@ -658,6 +669,7 @@ clean_restart ${binfile}
 # Skip all tests if Python scripting is not enabled.
 if { [skip_python_tests] } { continue }
 
+test_history_count
 test_value_creation
 test_value_reinit
 test_value_numeric_ops
diff --git a/gdb/value.c b/gdb/value.c
index 37c949f0853..7bd9891b3e9 100644
--- a/gdb/value.c
+++ b/gdb/value.c
@@ -1909,6 +1909,14 @@ access_value_history (int num)
   return value_copy (value_history[absnum].get ());
 }
 
+/* See value.h.  */
+
+ULONGEST
+value_history_count ()
+{
+  return value_history.size ();
+}
+
 static void
 show_values (const char *num_exp, int from_tty)
 {
diff --git a/gdb/value.h b/gdb/value.h
index f91c5a56144..0de4b5f3aef 100644
--- a/gdb/value.h
+++ b/gdb/value.h
@@ -948,6 +948,10 @@ extern void binop_promote (const struct language_defn *language,
 
 extern struct value *access_value_history (int num);
 
+/* Return the number of items in the value history.  */
+
+extern ULONGEST value_history_count ();
+
 extern struct value *value_of_internalvar (struct gdbarch *gdbarch,
 					   struct internalvar *var);
 
-- 
2.25.4


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

* Re: [PATCH] gdb/python: add gdb.history_count function
  2022-01-24 22:34 [PATCH] gdb/python: add gdb.history_count function Andrew Burgess
@ 2022-01-26 15:13 ` Tom Tromey
  2022-01-26 21:59   ` Andrew Burgess
  0 siblings, 1 reply; 3+ messages in thread
From: Tom Tromey @ 2022-01-26 15:13 UTC (permalink / raw)
  To: Andrew Burgess via Gdb-patches; +Cc: Andrew Burgess

>>>>> "Andrew" == Andrew Burgess via Gdb-patches <gdb-patches@sourceware.org> writes:

Andrew> Add a new function gdb.history_count to the Python api, this function
Andrew> returns an integer, the number of items in GDB's value history.

Andrew> This is useful if you want to pull items from the history by their
Andrew> absolute number, for example, if you wanted to show a complete history
Andrew> list.  Previously we could figure out how many items are in the
Andrew> history list by trying to fetch the items, and then catching the
Andrew> exception when the item is not available, but having this function
Andrew> seems nicer.

Looks good to me, thank you.

Tom

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

* Re: [PATCH] gdb/python: add gdb.history_count function
  2022-01-26 15:13 ` Tom Tromey
@ 2022-01-26 21:59   ` Andrew Burgess
  0 siblings, 0 replies; 3+ messages in thread
From: Andrew Burgess @ 2022-01-26 21:59 UTC (permalink / raw)
  To: Tom Tromey, Andrew Burgess via Gdb-patches

Tom Tromey <tom@tromey.com> writes:

>>>>>> "Andrew" == Andrew Burgess via Gdb-patches <gdb-patches@sourceware.org> writes:
>
> Andrew> Add a new function gdb.history_count to the Python api, this function
> Andrew> returns an integer, the number of items in GDB's value history.
>
> Andrew> This is useful if you want to pull items from the history by their
> Andrew> absolute number, for example, if you wanted to show a complete history
> Andrew> list.  Previously we could figure out how many items are in the
> Andrew> history list by trying to fetch the items, and then catching the
> Andrew> exception when the item is not available, but having this function
> Andrew> seems nicer.
>
> Looks good to me, thank you.

Thanks, pushed.

Andrew


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

end of thread, other threads:[~2022-01-26 21:59 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-01-24 22:34 [PATCH] gdb/python: add gdb.history_count function Andrew Burgess
2022-01-26 15:13 ` Tom Tromey
2022-01-26 21:59   ` Andrew Burgess

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).