Add gdb.events.tui_status

Message ID 20240927194750.332476-1-tromey@adacore.com
State New
Headers
Series Add gdb.events.tui_status |

Checks

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

Commit Message

Tom Tromey Sept. 27, 2024, 7:47 p.m. UTC
  This adds a new event source so that Python scripts can track whether
or not the TUI is presently enabled.

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=32162
---
 gdb/NEWS                                |  2 ++
 gdb/doc/python.texi                     | 10 +++++++++
 gdb/observable.c                        |  1 +
 gdb/observable.h                        |  2 ++
 gdb/python/py-all-events.def            |  1 +
 gdb/python/py-event-types.def           |  5 +++++
 gdb/python/py-tui.c                     | 28 +++++++++++++++++++++++++
 gdb/testsuite/gdb.python/tui-window.exp |  8 +++++++
 gdb/testsuite/gdb.python/tui-window.py  | 11 ++++++++++
 gdb/tui/tui.c                           |  5 +++++
 10 files changed, 73 insertions(+)
  

Comments

Eli Zaretskii Sept. 28, 2024, 6:13 a.m. UTC | #1
> From: Tom Tromey <tromey@adacore.com>
> Cc: Tom Tromey <tromey@adacore.com>
> Date: Fri, 27 Sep 2024 13:47:50 -0600
> 
> This adds a new event source so that Python scripts can track whether
> or not the TUI is presently enabled.
> 
> Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=32162
> ---
>  gdb/NEWS                                |  2 ++
>  gdb/doc/python.texi                     | 10 +++++++++
>  gdb/observable.c                        |  1 +
>  gdb/observable.h                        |  2 ++
>  gdb/python/py-all-events.def            |  1 +
>  gdb/python/py-event-types.def           |  5 +++++
>  gdb/python/py-tui.c                     | 28 +++++++++++++++++++++++++
>  gdb/testsuite/gdb.python/tui-window.exp |  8 +++++++
>  gdb/testsuite/gdb.python/tui-window.py  | 11 ++++++++++
>  gdb/tui/tui.c                           |  5 +++++
>  10 files changed, 73 insertions(+)

Thanks, the documentation parts are approved.

Reviewed-By: Eli Zaretskii <eliz@gnu.org>
  

Patch

diff --git a/gdb/NEWS b/gdb/NEWS
index f9b24758ea8..922c6436395 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -54,6 +54,8 @@ 
   ** Added gdb.record.clear.  Clears the trace data of the current recording.
      This forces re-decoding of the trace for successive commands.
 
+  ** Added the new event source gdb.tui_status.
+
 * Debugger Adapter Protocol changes
 
   ** The "scopes" request will now return a scope holding global
diff --git a/gdb/doc/python.texi b/gdb/doc/python.texi
index 22f0e6c6d0a..507c2c8a332 100644
--- a/gdb/doc/python.texi
+++ b/gdb/doc/python.texi
@@ -4081,6 +4081,16 @@  The @code{gdb.Progspace} that is about to be removed from
 @value{GDBN}.
 @end defvar
 
+@item events.tui_status
+This is emitted when the TUI is enabled or disabled.  The event is of
+type @code{gdb.TuiStatusEvent}, which has a single read-only
+attribute:
+
+@defvar TuiStatusEvent.status
+If the TUI has just been enabled, this is @code{True}; otherwise it is
+@code{False}.
+@end defvar
+
 @end table
 
 @node Threads In Python
diff --git a/gdb/observable.c b/gdb/observable.c
index 7816d214ad8..e9027fb90ba 100644
--- a/gdb/observable.c
+++ b/gdb/observable.c
@@ -75,6 +75,7 @@  DEFINE_OBSERVABLE (target_pre_wait);
 DEFINE_OBSERVABLE (target_post_wait);
 DEFINE_OBSERVABLE (new_program_space);
 DEFINE_OBSERVABLE (free_program_space);
+DEFINE_OBSERVABLE (tui_status);
 
 } /* namespace observers */
 } /* namespace gdb */
diff --git a/gdb/observable.h b/gdb/observable.h
index 2aa3ef3fd4e..10ab0bb9769 100644
--- a/gdb/observable.h
+++ b/gdb/observable.h
@@ -253,6 +253,8 @@  extern observable <program_space */* pspace */> new_program_space;
 /* The program space PSPACE is about to be deleted.  */
 extern observable <program_space */* pspace */> free_program_space;
 
+extern observable<bool /* enabled */> tui_status;
+
 } /* namespace observers */
 
 } /* namespace gdb */
diff --git a/gdb/python/py-all-events.def b/gdb/python/py-all-events.def
index 4df5541f969..995d160e57f 100644
--- a/gdb/python/py-all-events.def
+++ b/gdb/python/py-all-events.def
@@ -45,3 +45,4 @@  GDB_PY_DEFINE_EVENT(connection_removed)
 GDB_PY_DEFINE_EVENT(executable_changed)
 GDB_PY_DEFINE_EVENT(new_progspace)
 GDB_PY_DEFINE_EVENT(free_progspace)
+GDB_PY_DEFINE_EVENT(tui_status)
diff --git a/gdb/python/py-event-types.def b/gdb/python/py-event-types.def
index f43a51b5ed4..84e1b26edce 100644
--- a/gdb/python/py-event-types.def
+++ b/gdb/python/py-event-types.def
@@ -140,3 +140,8 @@  GDB_PY_DEFINE_EVENT_TYPE (free_progspace,
 			  "FreeProgspaceEvent",
 			  "GDB free Progspace event object",
 			  event_object_type);
+
+GDB_PY_DEFINE_EVENT_TYPE (tui_status,
+			  "TuiStatusEvent",
+			  "GDB TUI status event object",
+			  event_object_type);
diff --git a/gdb/python/py-tui.c b/gdb/python/py-tui.c
index afa6f36baeb..d254685f10d 100644
--- a/gdb/python/py-tui.c
+++ b/gdb/python/py-tui.c
@@ -36,6 +36,9 @@ 
 #include "tui/tui-layout.h"
 #include "tui/tui-wingeneral.h"
 #include "tui/tui-winsource.h"
+#include "observable.h"
+#include "py-events.h"
+#include "py-event.h"
 
 class tui_py_window;
 
@@ -616,6 +619,29 @@  PyTypeObject gdbpy_tui_window_object_type =
   0,				  /* tp_alloc */
 };
 
+/* Called when TUI is enabled or disabled.  */
+
+static void
+gdbpy_tui_status (bool state)
+{
+  gdbpy_enter enter_py;
+
+  if (evregpy_no_listeners_p (gdb_py_events.tui_status))
+    return;
+
+  gdbpy_ref<> event_obj = create_event_object (&tui_status_event_object_type);
+  if (event_obj == nullptr)
+    {
+      gdbpy_print_stack ();
+      return;
+    }
+
+  gdbpy_ref<> code (PyBool_FromLong (state));
+  if (evpy_add_attribute (event_obj.get (), "status", code.get ()) < 0
+      || evpy_emit_event (event_obj.get (), gdb_py_events.tui_status) < 0)
+    gdbpy_print_stack ();
+}
+
 #endif /* TUI */
 
 /* Initialize this module.  */
@@ -627,6 +653,8 @@  gdbpy_initialize_tui ()
   gdbpy_tui_window_object_type.tp_new = PyType_GenericNew;
   if (gdbpy_type_ready (&gdbpy_tui_window_object_type) < 0)
     return -1;
+
+  gdb::observers::tui_status.attach (gdbpy_tui_status, "py-tui");
 #endif	/* TUI */
 
   return 0;
diff --git a/gdb/testsuite/gdb.python/tui-window.exp b/gdb/testsuite/gdb.python/tui-window.exp
index e7ff1621fe8..ea4b0f23f54 100644
--- a/gdb/testsuite/gdb.python/tui-window.exp
+++ b/gdb/testsuite/gdb.python/tui-window.exp
@@ -43,6 +43,9 @@  if {![Term::enter_tui]} {
     return
 }
 
+Term::command "python print('tui_status=' + str(tui_status))"
+Term::check_contents "tui start event" "tui_status=True"
+
 Term::command "layout test"
 Term::check_contents "test title" \
     "This Is The Title"
@@ -63,3 +66,8 @@  Term::resize 51 51
 Term::check_contents "Window was updated" "Test: 2"
 
 Term::command "layout fail"
+
+Term::command "tui disable"
+gdb_test "python print('tui_status=' + str(tui_status))" \
+    "tui_status=False" \
+    "tui disable event"
diff --git a/gdb/testsuite/gdb.python/tui-window.py b/gdb/testsuite/gdb.python/tui-window.py
index d031f728735..5a008a4358d 100644
--- a/gdb/testsuite/gdb.python/tui-window.py
+++ b/gdb/testsuite/gdb.python/tui-window.py
@@ -20,6 +20,9 @@  import gdb
 the_window = None
 
 
+tui_status = False
+
+
 class TestWindow:
     def __init__(self, win):
         global the_window
@@ -62,3 +65,11 @@  def change_window_title():
 
 
 gdb.register_window_type("fail", failwin)
+
+
+def set_tui_status(ev):
+    global tui_status
+    tui_status = ev.status
+
+
+gdb.events.tui_status.connect(set_tui_status)
diff --git a/gdb/tui/tui.c b/gdb/tui/tui.c
index bc96cd85d59..1fce6cf169f 100644
--- a/gdb/tui/tui.c
+++ b/gdb/tui/tui.c
@@ -40,6 +40,7 @@ 
 #include "terminal.h"
 #include "top.h"
 #include "ui.h"
+#include "observable.h"
 
 #include <fcntl.h>
 
@@ -509,6 +510,8 @@  tui_enable (void)
   /* Update gdb's knowledge of its terminal.  */
   gdb_save_tty_state ();
   tui_update_gdb_sizes ();
+
+  gdb::observers::tui_status.notify (true);
 }
 
 /* Leave the tui mode.
@@ -547,6 +550,8 @@  tui_disable (void)
 
   tui_active = false;
   tui_update_gdb_sizes ();
+
+  gdb::observers::tui_status.notify (false);
 }
 
 /* Command wrapper for enabling tui mode.  */