gdb: return true in TuiWindow.is_valid only if TUI is enabled

If the user implements a TUI window in Python, and this window
responds to GDB events and then redraws its window contents then there
is currently an edge case which can lead to problems.

The Python API documentation suggests that calling methods like erase
or write on a TUI window (from Python code) will raise an exception if
the window is not valid.

And the description for is_valid says:

  This method returns True when this window is valid. When the user
  changes the TUI layout, windows no longer visible in the new layout
  will be destroyed. At this point, the gdb.TuiWindow will no longer
  be valid, and methods (and attributes) other than is_valid will
  throw an exception.

From this I, as a user, would expect that if I did 'tui disable' to
switch back to CLI mode, then the window would no longer be valid.
However, this is not the case.

When the TUI is disabled the windows in the TUI are not deleted, they
are simply hidden.  As such, currently, the is_valid method continues
to return true.

This means that if the users Python code does something like:

  def event_handler (e):
    global tui_window_object
    if tui_window_object->is_valid ():
      tui_window_object->erase ()
      tui_window_object->write ("Hello World")
  gdb.events.stop.connect (event_handler)

Then when a stop event arrives GDB will try to draw the TUI window,
even when the TUI is disabled.

This exposes two bugs.  First, is_valid should be returning false in
this case, second, if the user forgot to add the is_valid call, then I
believe the erase and write calls should be throwing an
exception (when the TUI is disabled).

The solution to both of these issues is I think bound together, as it
depends on having a working 'is_valid' check.

There's a rogue assert added into tui-layout.c as part of this
commit.  While working on this commit I managed to break GDB such that
TUI_CMD_WIN was nullptr, this was causing GDB to abort.  I'm leaving
the assert in as it might help people catch issues in the future.

This patch is inspired by the work done here:

  https://sourceware.org/pipermail/gdb-patches/2020-December/174338.html

gdb/ChangeLog:

	* python/py-tui.c (gdbpy_tui_window) <is_valid>: New member
	function.
	(REQUIRE_WINDOW): Call is_valid member function.
	(REQUIRE_WINDOW_FOR_SETTER): New define.
	(gdbpy_tui_is_valid): Call is_valid member function.
	(gdbpy_tui_set_title): Call REQUIRE_WINDOW_FOR_SETTER instead.
	* tui/tui-data.h (struct tui_win_info) <is_visible>: Check
	tui_active too.
	* tui/tui-layout.c (tui_apply_current_layout): Add an assert.
	* tui/tui.c (tui_enable): Move setting of tui_active earlier in
	the function.

gdb/doc/ChangeLog:

	* python.texinfo (TUI Windows In Python): Extend description of
	TuiWindow.is_valid.

gdb/testsuite/ChangeLog:

	* gdb.python/tui-window-disabled.c: New file.
	* gdb.python/tui-window-disabled.exp: New file.
	* gdb.python/tui-window-disabled.py: New file.
This commit is contained in:
Andrew Burgess 2021-01-15 10:31:19 +00:00
parent e0c23e11da
commit 29db1eb339
11 changed files with 394 additions and 15 deletions

View file

@ -47,6 +47,9 @@ struct gdbpy_tui_window
/* The TUI window, or nullptr if the window has been deleted. */
tui_py_window *window;
/* Return true if this object is valid. */
bool is_valid () const;
};
extern PyTypeObject gdbpy_tui_window_object_type
@ -137,6 +140,14 @@ private:
gdbpy_ref<gdbpy_tui_window> m_wrapper;
};
/* See gdbpy_tui_window declaration above. */
bool
gdbpy_tui_window::is_valid () const
{
return window != nullptr && tui_active;
}
tui_py_window::~tui_py_window ()
{
gdbpy_enter enter_py (get_current_arch (), current_language);
@ -344,11 +355,23 @@ gdbpy_register_tui_window (PyObject *self, PyObject *args, PyObject *kw)
#define REQUIRE_WINDOW(Window) \
do { \
if ((Window)->window == nullptr) \
if (!(Window)->is_valid ()) \
return PyErr_Format (PyExc_RuntimeError, \
_("TUI window is invalid.")); \
} while (0)
/* Require that "Window" be a valid window. */
#define REQUIRE_WINDOW_FOR_SETTER(Window) \
do { \
if (!(Window)->is_valid ()) \
{ \
PyErr_Format (PyExc_RuntimeError, \
_("TUI window is invalid.")); \
return -1; \
} \
} while (0)
/* Python function which checks the validity of a TUI window
object. */
static PyObject *
@ -356,7 +379,7 @@ gdbpy_tui_is_valid (PyObject *self, PyObject *args)
{
gdbpy_tui_window *win = (gdbpy_tui_window *) self;
if (win->window != nullptr)
if (win->is_valid ())
Py_RETURN_TRUE;
Py_RETURN_FALSE;
}
@ -428,11 +451,7 @@ gdbpy_tui_set_title (PyObject *self, PyObject *newvalue, void *closure)
{
gdbpy_tui_window *win = (gdbpy_tui_window *) self;
if (win->window == nullptr)
{
PyErr_Format (PyExc_RuntimeError, _("TUI window is invalid."));
return -1;
}
REQUIRE_WINDOW_FOR_SETTER (win);
if (newvalue == nullptr)
{