public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Tom Tromey <tom@tromey.com>
To: gdb-patches@sourceware.org
Subject: [PATCH v3 15/18] Lazy language setting
Date: Wed, 22 Nov 2023 22:32:24 -0700	[thread overview]
Message-ID: <20231122-t-bg-dwarf-reading-v3-15-fc3180de63c4@tromey.com> (raw)
In-Reply-To: <20231122-t-bg-dwarf-reading-v3-0-fc3180de63c4@tromey.com>

When gdb starts up with a symbol file, it uses the program's "main" to
decide the "static context" and the initial language.  With background
DWARF reading, this means that gdb has to wait for a significant
amount of DWARF to be read synchronously.

This patch introduces lazy language setting.  The idea here is that in
many cases, the prompt can show up early, making gdb feel more
responsive.
---
 gdb/gdbthread.h |  3 ++-
 gdb/language.c  | 35 +++++++++++++++++++++++++++++++++++
 gdb/language.h  | 27 ++++++++++++++++++++-------
 gdb/symfile.c   | 18 +++++++++++++-----
 gdb/thread.c    |  8 +++-----
 5 files changed, 73 insertions(+), 18 deletions(-)

diff --git a/gdb/gdbthread.h b/gdb/gdbthread.h
index b2c6baf4432..a8ad11b3468 100644
--- a/gdb/gdbthread.h
+++ b/gdb/gdbthread.h
@@ -36,6 +36,7 @@ struct symtab;
 #include "displaced-stepping.h"
 #include "gdbsupport/intrusive_list.h"
 #include "thread-fsm.h"
+#include "language.h"
 
 struct inferior;
 struct process_stratum_target;
@@ -904,7 +905,7 @@ class scoped_restore_current_thread
   /* Save/restore the language as well, because selecting a frame
      changes the current language to the frame's language if "set
      language auto".  */
-  enum language m_lang;
+  scoped_restore_current_language m_lang;
 };
 
 /* Returns a pointer into the thread_info corresponding to
diff --git a/gdb/language.c b/gdb/language.c
index 12a953eaa96..3fbd735a697 100644
--- a/gdb/language.c
+++ b/gdb/language.c
@@ -80,6 +80,7 @@ enum case_sensitivity case_sensitivity = case_sensitive_on;
 /* The current language and language_mode (see language.h).  */
 
 static const struct language_defn *global_current_language;
+static lazily_set_language_ftype *lazy_language_setter;
 enum language_mode language_mode = language_mode_auto;
 
 /* See language.h.  */
@@ -87,9 +88,41 @@ enum language_mode language_mode = language_mode_auto;
 const struct language_defn *
 get_current_language ()
 {
+  if (lazy_language_setter != nullptr)
+    {
+      /* Avoid recursive calls -- set_language refers to
+	 current_language.  */
+      lazily_set_language_ftype *call = lazy_language_setter;
+      lazy_language_setter = nullptr;
+      call ();
+    }
   return global_current_language;
 }
 
+void
+lazily_set_language (lazily_set_language_ftype *fun)
+{
+  lazy_language_setter = fun;
+}
+
+scoped_restore_current_language::scoped_restore_current_language ()
+  : m_lang (global_current_language),
+    m_fun (lazy_language_setter)
+{
+}
+
+scoped_restore_current_language::~scoped_restore_current_language ()
+{
+  /* If both are NULL, then that means dont_restore was called.  */
+  if (m_lang != nullptr || m_fun != nullptr)
+    {
+      global_current_language = m_lang;
+      lazy_language_setter = m_fun;
+      if (lazy_language_setter == nullptr)
+	set_range_case ();
+    }
+}
+
 /* The language that the user expects to be typing in (the language
    of main(), or the last language we notified them about, or C).  */
 
@@ -185,6 +218,7 @@ set_language (const char *language)
 
       /* Found it!  Go into manual mode, and use this language.  */
       language_mode = language_mode_manual;
+      lazy_language_setter = nullptr;
       global_current_language = lang;
       set_range_case ();
       expected_language = lang;
@@ -372,6 +406,7 @@ set_range_case (void)
 void
 set_language (enum language lang)
 {
+  lazy_language_setter = nullptr;
   global_current_language = language_def (lang);
   set_range_case ();
 }
diff --git a/gdb/language.h b/gdb/language.h
index 6ff6f5eb95f..6cbcd119dac 100644
--- a/gdb/language.h
+++ b/gdb/language.h
@@ -787,6 +787,10 @@ extern void language_info ();
 
 extern void set_language (enum language lang);
 
+typedef void lazily_set_language_ftype ();
+extern void lazily_set_language (lazily_set_language_ftype *fun);
+\f
+
 /* Test a character to decide whether it can be printed in literal form
    or needs to be printed in another representation.  For example,
    in C the literal form of the character with octal value 141 is 'a'
@@ -837,14 +841,14 @@ class scoped_restore_current_language
 {
 public:
 
-  explicit scoped_restore_current_language ()
-    : m_lang (current_language->la_language)
-  {
-  }
+  scoped_restore_current_language ();
+  ~scoped_restore_current_language ();
 
-  ~scoped_restore_current_language ()
+  scoped_restore_current_language (scoped_restore_current_language &&other)
   {
-    set_language (m_lang);
+    m_lang = other.m_lang;
+    m_fun = other.m_fun;
+    other.dont_restore ();
   }
 
   scoped_restore_current_language (const scoped_restore_current_language &)
@@ -852,9 +856,18 @@ class scoped_restore_current_language
   scoped_restore_current_language &operator=
       (const scoped_restore_current_language &) = delete;
 
+  /* Cancel restoring on scope exit.  */
+  void dont_restore ()
+  {
+    /* This is implemented using a sentinel value.  */
+    m_lang = nullptr;
+    m_fun = nullptr;
+  }
+
 private:
 
-  enum language m_lang;
+  const language_defn *m_lang;
+  lazily_set_language_ftype *m_fun;
 };
 
 /* If language_mode is language_mode_auto,
diff --git a/gdb/symfile.c b/gdb/symfile.c
index 7cbd993be8f..2ff2cbad29b 100644
--- a/gdb/symfile.c
+++ b/gdb/symfile.c
@@ -1682,13 +1682,11 @@ symbol_file_command (const char *args, int from_tty)
     }
 }
 
-/* Set the initial language.  */
+/* Lazily set the initial language.  */
 
-void
-set_initial_language (void)
+static void
+set_initial_language_callback ()
 {
-  if (language_mode == language_mode_manual)
-    return;
   enum language lang = main_language ();
   /* Make C the default language.  */
   enum language default_lang = language_c;
@@ -1713,6 +1711,16 @@ set_initial_language (void)
   expected_language = current_language; /* Don't warn the user.  */
 }
 
+/* Set the initial language.  */
+
+void
+set_initial_language (void)
+{
+  if (language_mode == language_mode_manual)
+    return;
+  lazily_set_language (set_initial_language_callback);
+}
+
 /* Open the file specified by NAME and hand it off to BFD for
    preliminary analysis.  Return a newly initialized bfd *, which
    includes a newly malloc'd` copy of NAME (tilde-expanded and made
diff --git a/gdb/thread.c b/gdb/thread.c
index c0ed64e5f8b..b27d8d721b2 100644
--- a/gdb/thread.c
+++ b/gdb/thread.c
@@ -1428,13 +1428,13 @@ scoped_restore_current_thread::restore ()
       && target_has_stack ()
       && target_has_memory ())
     restore_selected_frame (m_selected_frame_id, m_selected_frame_level);
-
-  set_language (m_lang);
 }
 
 scoped_restore_current_thread::~scoped_restore_current_thread ()
 {
-  if (!m_dont_restore)
+  if (m_dont_restore)
+    m_lang.dont_restore ();
+  else
     restore ();
 }
 
@@ -1442,8 +1442,6 @@ scoped_restore_current_thread::scoped_restore_current_thread ()
 {
   m_inf = inferior_ref::new_reference (current_inferior ());
 
-  m_lang = current_language->la_language;
-
   if (inferior_ptid != null_ptid)
     {
       m_thread = thread_info_ref::new_reference (inferior_thread ());

-- 
2.41.0


  parent reply	other threads:[~2023-11-23  5:32 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-23  5:32 [PATCH v3 00/18] Index DWARF in the background Tom Tromey
2023-11-23  5:32 ` [PATCH v3 01/18] Don't use objfile::intern in DWO code Tom Tromey
2023-11-23  5:32 ` [PATCH v3 02/18] Pre-read DWZ section data Tom Tromey
2023-11-23  5:32 ` [PATCH v3 03/18] Add a couple of bfd_cache_close calls Tom Tromey
2023-11-23  5:32 ` [PATCH v3 04/18] Add thread-safety to gdb's BFD wrappers Tom Tromey
2023-11-23  5:32 ` [PATCH v3 05/18] Refactor complaint thread-safety approach Tom Tromey
2023-11-23  5:32 ` [PATCH v3 06/18] Add quick_symbol_functions::compute_main_name Tom Tromey
2023-11-23  5:32 ` [PATCH v3 07/18] Add gdb::task_group Tom Tromey
2023-11-23  5:32 ` [PATCH v3 08/18] Move cooked_index_storage to cooked-index.h Tom Tromey
2023-11-23  5:32 ` [PATCH v3 09/18] Add "maint set dwarf synchronous" Tom Tromey
2023-11-23  7:02   ` Eli Zaretskii
2023-11-23  5:32 ` [PATCH v3 10/18] Change how cooked index waits for threads Tom Tromey
2023-11-23  5:32 ` [PATCH v3 11/18] Do more DWARF reading in the background Tom Tromey
2023-11-23  5:32 ` [PATCH v3 12/18] Simplify the public DWARF API Tom Tromey
2023-11-23  5:32 ` [PATCH v3 13/18] Remove two quick_symbol_functions methods Tom Tromey
2023-11-23  5:32 ` [PATCH v3 14/18] Change current_language to be a macro Tom Tromey
2023-11-23  5:32 ` Tom Tromey [this message]
2023-11-23  5:32 ` [PATCH v3 16/18] Optimize lookup_minimal_symbol_text Tom Tromey
2023-11-23  5:32 ` [PATCH v3 17/18] Avoid language-based lookups in startup path Tom Tromey
2023-11-23  5:32 ` [PATCH v3 18/18] Back out some parallel_for_each features Tom Tromey
2023-11-27 15:31 ` [PATCH v3 00/18] Index DWARF in the background Tom de Vries
2023-12-10 17:19 ` Tom Tromey

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20231122-t-bg-dwarf-reading-v3-15-fc3180de63c4@tromey.com \
    --to=tom@tromey.com \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).