public inbox for gdb-cvs@sourceware.org
help / color / mirror / Atom feed
* [binutils-gdb] gdb: add maybe_switch_inferior function
@ 2023-04-17 17:57 Simon Marchi
  0 siblings, 0 replies; only message in thread
From: Simon Marchi @ 2023-04-17 17:57 UTC (permalink / raw)
  To: gdb-cvs

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=348da4565b5c901e9320c3e2d7f5b62793b48a38

commit 348da4565b5c901e9320c3e2d7f5b62793b48a38
Author: Simon Marchi <simon.marchi@efficios.com>
Date:   Mon Apr 3 14:52:05 2023 -0400

    gdb: add maybe_switch_inferior function
    
    Add the maybe_switch_inferior function, which ensures that the given
    inferior is the current one.  Return an instantiated
    scoped_restore_current_thread object only we actually needed to switch
    inferior.
    
    Returning a scoped_restore_current_thread requires it to be
    move-constructible, so give it a move constructor.
    
    Change-Id: I1231037102ed6166f2530399e8257ad937fb0569
    Reviewed-By: Pedro Alves <pedro@palves.net>

Diff:
---
 gdb/gdbthread.h |  2 ++
 gdb/inferior.c  | 15 +++++++++++++++
 gdb/inferior.h  |  7 +++++++
 gdb/thread.c    | 14 ++++++++++++++
 4 files changed, 38 insertions(+)

diff --git a/gdb/gdbthread.h b/gdb/gdbthread.h
index 65aab9d12c3..7135515bf45 100644
--- a/gdb/gdbthread.h
+++ b/gdb/gdbthread.h
@@ -851,6 +851,8 @@ public:
   scoped_restore_current_thread ();
   ~scoped_restore_current_thread ();
 
+  scoped_restore_current_thread (scoped_restore_current_thread &&rhs);
+
   DISABLE_COPY_AND_ASSIGN (scoped_restore_current_thread);
 
   /* Cancel restoring on scope exit.  */
diff --git a/gdb/inferior.c b/gdb/inferior.c
index 6eb9f3ff23b..9e37029d544 100644
--- a/gdb/inferior.c
+++ b/gdb/inferior.c
@@ -684,6 +684,21 @@ switch_to_inferior_no_thread (inferior *inf)
   set_current_program_space (inf->pspace);
 }
 
+/* See regcache.h.  */
+
+gdb::optional<scoped_restore_current_thread>
+maybe_switch_inferior (inferior *inf)
+{
+  gdb::optional<scoped_restore_current_thread> maybe_restore_thread;
+  if (inf != current_inferior ())
+    {
+      maybe_restore_thread.emplace ();
+      switch_to_inferior_no_thread (inf);
+    }
+
+  return maybe_restore_thread;
+}
+
 static void
 inferior_command (const char *args, int from_tty)
 {
diff --git a/gdb/inferior.h b/gdb/inferior.h
index 633916eb7c1..613ef28d660 100644
--- a/gdb/inferior.h
+++ b/gdb/inferior.h
@@ -340,6 +340,13 @@ extern void set_current_inferior (inferior *);
    selected.  */
 extern void switch_to_inferior_no_thread (inferior *inf);
 
+/* Ensure INF is the current inferior.
+
+   If the current inferior was changed, return an RAII object that will
+   restore the original current context.  */
+extern gdb::optional<scoped_restore_current_thread> maybe_switch_inferior
+  (inferior *inf);
+
 /* Info about an inferior's target description.  There's one of these
    for each inferior.  */
 
diff --git a/gdb/thread.c b/gdb/thread.c
index 4d97ed3f2d1..e9432f945e0 100644
--- a/gdb/thread.c
+++ b/gdb/thread.c
@@ -1378,6 +1378,20 @@ scoped_restore_current_thread::scoped_restore_current_thread ()
     }
 }
 
+scoped_restore_current_thread::scoped_restore_current_thread
+  (scoped_restore_current_thread &&rhs)
+  : m_dont_restore (std::move (rhs.m_dont_restore)),
+    m_thread (std::move (rhs.m_thread)),
+    m_inf (std::move (rhs.m_inf)),
+    m_selected_frame_id (std::move (rhs.m_selected_frame_id)),
+    m_selected_frame_level (std::move (rhs.m_selected_frame_level)),
+    m_was_stopped (std::move (rhs.m_was_stopped)),
+    m_lang (std::move (rhs.m_lang))
+{
+  /* Deactivate the rhs.  */
+  rhs.m_dont_restore = true;
+}
+
 /* See gdbthread.h.  */
 
 int

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2023-04-17 17:57 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-04-17 17:57 [binutils-gdb] gdb: add maybe_switch_inferior function Simon Marchi

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).