gdb/tui: Fix crash from y/n prompt during TUI initialization

Message ID 20240312215334.37888-1-amerey@redhat.com
State New
Headers
Series gdb/tui: Fix crash from y/n prompt during TUI initialization |

Checks

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

Commit Message

Aaron Merey March 12, 2024, 9:53 p.m. UTC
  A y/n prompt during TUI initialization can cause a crash due to
attempting to divide by a screen width of 0 in
tui_inject_newline_into_command_window.

If 'debuginfod enabled' is set to 'ask', a y/n prompt can occur
during TUI initialization if gdb queries debuginfod for a source
file to be displayed in the TUI source window.

Fix this by raising the prompt at the beginning of tui_enable_command,
before leaving the current interpreter.

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=31449
---
 gdb/debuginfod-support.c                      | 10 +++++---
 gdb/debuginfod-support.h                      |  5 ++++
 .../gdb.debuginfod/fetch_src_and_symbols.exp  | 23 +++++++++++++++++++
 gdb/tui/tui.c                                 |  6 +++++
 4 files changed, 41 insertions(+), 3 deletions(-)
  

Patch

diff --git a/gdb/debuginfod-support.c b/gdb/debuginfod-support.c
index 9bb3748c8c3..aea0a85df0c 100644
--- a/gdb/debuginfod-support.c
+++ b/gdb/debuginfod-support.c
@@ -101,6 +101,11 @@  debuginfod_section_query (const unsigned char *build_id,
 {
   return scoped_fd (-ENOSYS);
 }
+
+bool debuginfod_is_enabled (void)
+{
+  return false;
+}
 #define NO_IMPL _("Support for debuginfod is not compiled into GDB.")
 
 #else
@@ -223,10 +228,9 @@  get_debuginfod_client ()
   return global_client;
 }
 
-/* Check if debuginfod is enabled.  If configured to do so, ask the user
-   whether to enable debuginfod.  */
+/* See debuginfod-support.h.  */
 
-static bool
+bool
 debuginfod_is_enabled ()
 {
   const char *urls = skip_spaces (getenv (DEBUGINFOD_URLS_ENV_VAR));
diff --git a/gdb/debuginfod-support.h b/gdb/debuginfod-support.h
index 2b816fd4b82..a4965a74eaa 100644
--- a/gdb/debuginfod-support.h
+++ b/gdb/debuginfod-support.h
@@ -105,4 +105,9 @@  extern scoped_fd debuginfod_section_query (const unsigned char *build_id,
 					   const char *section_name,
 					   gdb::unique_xmalloc_ptr<char>
 					     *destname);
+
+/* Check if debuginfod is enabled.  If configured to do so, ask the user
+   whether to enable debuginfod.  */
+
+bool debuginfod_is_enabled ();
 #endif /* DEBUGINFOD_SUPPORT_H */
diff --git a/gdb/testsuite/gdb.debuginfod/fetch_src_and_symbols.exp b/gdb/testsuite/gdb.debuginfod/fetch_src_and_symbols.exp
index 401af0df0d2..62f0a14dbc3 100644
--- a/gdb/testsuite/gdb.debuginfod/fetch_src_and_symbols.exp
+++ b/gdb/testsuite/gdb.debuginfod/fetch_src_and_symbols.exp
@@ -21,6 +21,7 @@  load_lib dwarf.exp
 load_lib debuginfod-support.exp
 
 require allow_debuginfod_tests
+tuiterm_env
 
 set sourcetmp [standard_output_file tmp-${srcfile}]
 set outputdir [standard_output_file {}]
@@ -256,6 +257,28 @@  proc_with_prefix local_url { } {
 	"file [file tail ${binfile}_alt.o]" \
 	$enable_debuginfod_question "y"
 
+    # Enabling TUI from CLI should trigger a debuginfod prompt.
+    if {[Term::prepare_for_tui] } {
+      Term::clean_restart 24 80
+      send_gdb "tui disable\n"
+
+      # Verify that the prompt is set to appear.
+      gdb_test "show debuginfod enabled" \
+	"Debuginfod functionality is currently set to \"ask\"\." \
+	"enabled ask tui"
+      send_gdb "tui enable\n"
+
+      # Answer the prompt.
+      send_gdb  "y\n"
+
+      # Confirm that debuginfod was enabled along with tui.
+      Term::command "show debuginfod enabled"
+      Term::check_contents "enabled on tui" \
+	"Debuginfod functionality is currently set to \"on\"\."
+    } else {
+      unsupported "TUI not supported"
+    }
+
     # Configure debuginfod with commands.
     unsetenv DEBUGINFOD_URLS
     clean_restart
diff --git a/gdb/tui/tui.c b/gdb/tui/tui.c
index dea6ffbf954..8272ae4623e 100644
--- a/gdb/tui/tui.c
+++ b/gdb/tui/tui.c
@@ -36,6 +36,7 @@ 
 #include "target.h"
 #include "frame.h"
 #include "breakpoint.h"
+#include "debuginfod-support.h"
 #include "inferior.h"
 #include "symtab.h"
 #include "source.h"
@@ -559,6 +560,11 @@  tui_disable (void)
 static void
 tui_enable_command (const char *args, int from_tty)
 {
+  /* Trigger any debuginfod-related y/n prompts now to avoid having
+     it occur during tui initialization.  Handling the prompt while
+     tui windows are initializing can cause crashes.  */
+  debuginfod_is_enabled ();
+
   tui_enable ();
 }