public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Simon Marchi <simon.marchi@efficios.com>
To: gdb-patches@sourceware.org
Cc: Simon Marchi <simon.marchi@efficios.com>, Pedro Alves <pedro@palves.net>
Subject: [PATCH 4/7] gdb: add maybe_switch_inferior function
Date: Mon,  3 Apr 2023 14:52:05 -0400	[thread overview]
Message-ID: <20230403185208.197965-5-simon.marchi@efficios.com> (raw)
In-Reply-To: <20230403185208.197965-1-simon.marchi@efficios.com>

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>
---
 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 848daa94410a..731c5e159e9f 100644
--- a/gdb/gdbthread.h
+++ b/gdb/gdbthread.h
@@ -858,6 +858,8 @@ class scoped_restore_current_thread
   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 a1e3c79d8a20..f6ed942c5053 100644
--- a/gdb/inferior.c
+++ b/gdb/inferior.c
@@ -672,6 +672,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 72034cc4ffbc..ab981b7b4b27 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 25d97cd60727..506f8481e17b 100644
--- a/gdb/thread.c
+++ b/gdb/thread.c
@@ -1403,6 +1403,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
-- 
2.40.0


  parent reply	other threads:[~2023-04-03 18:52 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-04-03 18:52 [PATCH 0/7] amdgpu: handle fork and exec Simon Marchi
2023-04-03 18:52 ` [PATCH 1/7] gdb: pass execing and following inferior to inferior_execd observers Simon Marchi
2023-04-03 18:52 ` [PATCH 2/7] gdb: add inferior_forked observable Simon Marchi
2023-04-03 18:52 ` [PATCH 3/7] gdb: remove regcache::target Simon Marchi
2023-04-03 18:52 ` Simon Marchi [this message]
2023-04-03 18:52 ` [PATCH 5/7] gdb: make regcache::raw_update switch to right inferior Simon Marchi
2023-04-03 18:52 ` [PATCH 6/7] gdb: switch to right inferior in fetch_inferior_event Simon Marchi
2023-04-03 18:52 ` [PATCH 7/7] gdb/amdgpu: add follow fork and exec support Simon Marchi
2023-04-13 15:54 ` [PATCH 0/7] amdgpu: handle fork and exec Simon Marchi
2023-04-17 17:57   ` Simon Marchi

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=20230403185208.197965-5-simon.marchi@efficios.com \
    --to=simon.marchi@efficios.com \
    --cc=gdb-patches@sourceware.org \
    --cc=pedro@palves.net \
    /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).