gdb/tui: fix for debuginfod prompt while enabling the TUI

PR tui/31449 reports a SIGFPE when the debuginfod query happens while
enabling TUI using the "tui enable" command:

  Thread 1 "gdb" received signal SIGFPE, Arithmetic exception.
  0x0000000001021084 in tui_inject_newline_into_command_window () at /data/vries/gdb/src/gdb/tui/tui-io.c:1096
  1096        py += px / tui_cmd_win ()->width;

due to divide-by-zero because tui_cmd_win ()->width == 0.

The corresponding backtrace is:

  (gdb) bt
  #0  0x0000000001021084 in tui_inject_newline_into_command_window () at gdb/tui/tui-io.c:1096
  #1  0x0000000000fe65fd in gdb_readline_wrapper_line (line=...) at gdb/top.c:939
  #2  0x0000000000944eef in gdb_rl_callback_handler (rl=0x2cc865a0 "n") at gdb/event-top.c:288
  #3  0x0000000001175779 in rl_callback_read_char () at readline/readline/callback.c:302
  #4  0x0000000000944bc3 in gdb_rl_callback_read_char_wrapper_sjlj () at gdb/event-top.c:197
  #5  0x0000000000944cd4 in gdb_rl_callback_read_char_wrapper_noexcept () at gdb/event-top.c:240
  #6  0x0000000000944d52 in gdb_rl_callback_read_char_wrapper (...) at gdb/event-top.c:252
  #7  0x0000000001062352 in stdin_event_handler (error=0, client_data=0x2c865150) at gdb/ui.c:154
  #8  0x0000000001a04edf in handle_file_event (file_ptr=0x2ccf8850, ready_mask=1) at gdbsupport/event-loop.cc:551
  #9  0x0000000001a05522 in gdb_wait_for_event (block=1) at gdbsupport/event-loop.cc:672
  #10 0x0000000001a043ff in gdb_do_one_event (mstimeout=-1) at gdbsupport/event-loop.cc:263
  #11 0x00000000006d5480 in interp::do_one_event (this=0x2cc2af20, mstimeout=-1) at gdb/interps.h:93
  #12 0x0000000000fe670d in gdb_readline_wrapper (prompt=0x2ccca4e0 "Enable debuginfod for this session? (y or [n]) ") at gdb/top.c:1033
  #13 0x00000000010c6853 in defaulted_query(...) (...) at gdb/utils.c:844
  #14 0x00000000010c6b8a in nquery (...) at gdb/utils.c:901
  #15 0x00000000007a9324 in debuginfod_is_enabled () at gdb/debuginfod-support.c:268
  #16 0x00000000007a950d in debuginfod_source_query (...) at gdb/debuginfod-support.c:311
  #17 0x0000000000efc2c7 in open_source_file (s=0x2cc8f4b0) at gdb/source.c:1152
  #18 0x0000000000efc619 in symtab_to_fullname (...) at gdb/source.c:1214
  #19 0x0000000000f5ebb3 in find_line_symtab (...) at gdb/symtab.c:3287
  #20 0x0000000000f5f0e5 in find_pc_for_line (...) at gdb/symtab.c:3391
  #21 0x0000000001011f54 in tui_get_begin_asm_address (...) at gdb/tui/tui-disasm.c:404
  #22 0x000000000104888d in tui_source_window_base::rerender (this=0x2cbdc570) at gdb/tui/tui-winsource.c:474
  #23 0x0000000001028e81 in tui_win_info::resize (this=0x2cbdc570, height_=21, width_=127, origin_x_=0, origin_y_=0) at gdb/tui/tui-layout.c:299
  #24 0x00000000010297d0 in tui_layout_window::apply (this=0x2cc50350, x_=0, y_=0, width_=127, height_=21, preserve_cmd_win_size_p=false) at gdb/tui/tui-layout.c:432
  #25 0x000000000102bfea in tui_layout_split::apply (this=0x2caea920, x_=0, y_=0, width_=127, height_=33, preserve_cmd_win_size_p=false) at gdb/tui/tui-layout.c:1026
  #26 0x0000000001028267 in tui_apply_current_layout (...) at gdb/tui/tui-layout.c:68
  #27 0x0000000001028737 in tui_set_layout (layout=0x2c9b9e90) at gdb/tui/tui-layout.c:133
  #28 0x0000000001028af5 in tui_set_initial_layout () at gdb/tui/tui-layout.c:209
  #29 0x000000000104b795 in tui_enable () at gdb/tui/tui.c:496
  #30 0x000000000104bab3 in tui_enable_command (args=0x0, from_tty=1) at gdb/tui/tui.c:591
  #31 0x00000000006c5ffe in do_simple_func (args=0x0, from_tty=1, c=0x2c9bb2f0) at gdb/cli/cli-decode.c:94
  #32 0x00000000006cc94f in cmd_func (cmd=0x2c9bb2f0, args=0x0, from_tty=1) at gdb/cli/cli-decode.c:2831
  #33 0x0000000000fe53ad in execute_command (p=0x2c86699a "", from_tty=1) at gdb/top.c:563
  #34 0x000000000094584d in command_handler (command=0x2c866990 "tui enable") at gdb/event-top.c:611
  #35 0x0000000000945dfe in command_line_handler (rl=...) at gdb/event-top.c:844
  #36 0x000000000101e916 in tui_command_line_handler (rl=...) at gdb/tui/tui-interp.c:101
  #37 0x0000000000944eef in gdb_rl_callback_handler (rl=0x2cc86a30 "tui enable") at gdb/event-top.c:288
  #38 0x0000000001175779 in rl_callback_read_char () at readline/readline/callback.c:302
  #39 0x0000000000944bc3 in gdb_rl_callback_read_char_wrapper_sjlj () at gdb/event-top.c:197
  #40 0x0000000000944cd4 in gdb_rl_callback_read_char_wrapper_noexcept () at gdb/event-top.c:240
  #41 0x0000000000944d52 in gdb_rl_callback_read_char_wrapper (...) at gdb/event-top.c:252
  #42 0x0000000001062352 in stdin_event_handler (error=0, client_data=0x2c865150) at gdb/ui.c:154
  #43 0x0000000001a04edf in handle_file_event (file_ptr=0x2ccf8850, ready_mask=1) at gdbsupport/event-loop.cc:551
  #44 0x0000000001a05522 in gdb_wait_for_event (block=1) at gdbsupport/event-loop.cc:672
  #45 0x0000000001a043ff in gdb_do_one_event (mstimeout=-1) at gdbsupport/event-loop.cc:263
  #46 0x00000000006d5480 in interp::do_one_event (this=0x2cc2af20, mstimeout=-1) at gdb/interps.h:93
  #47 0x0000000000b77f25 in start_event_loop () at gdb/main.c:403
  #48 0x0000000000b78113 in captured_command_loop () at gdb/main.c:468
  #49 0x0000000000b7a07c in captured_main (context=0x7fff660b9e60) at gdb/main.c:1381
  #50 0x0000000000b7a178 in gdb_main (args=0x7fff660b9e60) at gdb/main.c:1400
  #51 0x0000000000419705 in main (argc=5, argv=0x7fff660b9f98) at gdb/gdb.c:38
  (gdb)

The problem is that while the TUI is being enabled for the first time,
none of the TUI windows yet exist.  As each window is created its
contents are rendered (i.e. filled in based on GDB's state), which for
some windows can trigger an interactive prompt, in this case a missing
source file triggers a debuginfod prompt while trying to render the
`src` window.

The interactive prompt will be written to the `cmd` window, but at
this point the `cmd` window has not yet been created.

There have been several different attempts to fix this issue:

  1. https://inbox.sourceware.org/gdb-patches/20240312215334.37888-1-amerey@redhat.com
  2. https://inbox.sourceware.org/gdb-patches/20260114172833.1824823-1-tdevries@suse.de
  3. https://inbox.sourceware.org/gdb-patches/20260116104313.2704994-1-tdevries@suse.de
  4. https://inbox.sourceware.org/gdb-patches/20260221101818.2678136-1-tdevries@suse.de
  5. https://inbox.sourceware.org/gdb-patches/20260314173737.1436116-1-tdevries@suse.de
  6. https://inbox.sourceware.org/gdb-patches/20260417075719.852558-1-tdevries@suse.de

The patch presented here is similar to what was presented in (3)
above, but I think the implementation is maybe a little simpler.

Additionally, all but (6) of the above patches don't address issues
related to using multi-key combinations like 'C-x C-a' to enable TUI
mode, and that patch just takes the (admittedly safe) approach of
preventing the user from activating the TUI using a multi-key
combination if debuginfod is in ASK mode (and so could trigger an
interactive prompt).

This patch doesn't address the multi-key problem, that is left for the
next patch in this series.

This patch ensures that the `cmd` window always exists before
rendering the windows (i.e. filling in their content).  This is done
by introducing a tui_defer_rerender global which is set in tui_enable,
and checked in tui_win_info::resize.  Then, prior to the prompt being
displayed, if the flag is set, we render the contents of all visible
windows.  This can trigger a secondary prompt (e.g. the debuginfod
prompt), but by this point the `cmd` window exists, and can display
the prompt.

The debuginfod-query.exp test included here is based on the test Tom
de Vries wrote for one of his patches listed above, but extended to
cover some additional cases.  The activate-with-key-combo.exp test is
new for this series.

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=31449
Co-Authored-By: Tom de Vries <tdevries@suse.de>
8 files changed