Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Jerry Zhang Jian <jerry.zhangjian@sifive.com>
To: gdb-patches@sourceware.org, kito.cheng@sifive.com
Cc: Jerry Zhang Jian <jerry.zhangjian@sifive.com>
Subject: [PATCH] gdb: invalidate register cache after monitor commands
Date: Tue, 11 Aug 2026 11:52:05 +0800	[thread overview]
Message-ID: <20260811035205.26485-1-jerry.zhangjian@sifive.com> (raw)

A monitor command is opaque to GDB: the stub can halt, resume, or
reset the target behind GDB's back, even if it later reports an
error, and the remote protocol has no way to tell GDB that happened.
For example, "monitor reset halt" was leaving GDB reporting the
pre-reset $pc until a later step/continue forced a refetch.

Invalidate the register cache after every monitor command via
SCOPE_EXIT, so it still runs on the error path.  Scope it to the
inferior's own process_stratum_target, matching registers_changed_thread()
and the target_wait()/target_stop() lookup pattern elsewhere in this
file, rather than wiping every inferior's cache with registers_changed().
Hold a strong reference to the target across the call in case it gets
unpushed/detached, the same idiom used in target_detach().

Signed-off-by: Jerry Zhang Jian <jerry.zhangjian@sifive.com>
---
 gdb/target.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/gdb/target.c b/gdb/target.c
index 5d937f3ae85..5c4684d81b5 100644
--- a/gdb/target.c
+++ b/gdb/target.c
@@ -4262,6 +4262,18 @@ default_rcmd (struct target_ops *self, const char *command,
 static void
 do_monitor_command (const char *cmd, int from_tty)
 {
+  process_target_ops_ref proc_target_ref;
+  if (process_stratum_target *proc_target
+	= current_inferior ()->process_target ())
+    proc_target_ref = process_target_ops_ref::new_reference (proc_target);
+
+  /* Monitor commands may change target state behind GDB's back.  */
+  SCOPE_EXIT
+    {
+      if (proc_target_ref != nullptr)
+	registers_changed_ptid (proc_target_ref.get (), minus_one_ptid);
+    };
+
   target_rcmd (cmd, gdb_stdtarg);
 }
 
-- 
2.53.0


             reply	other threads:[~2026-08-11  3:53 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-11  3:52 Jerry Zhang Jian [this message]
2026-08-11  8:57 ` Andrew Burgess
2026-08-12 20:34   ` Tom Tromey
2026-08-17  3:28     ` Jerry Zhang Jian
2026-08-21 16:44       ` 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=20260811035205.26485-1-jerry.zhangjian@sifive.com \
    --to=jerry.zhangjian@sifive.com \
    --cc=gdb-patches@sourceware.org \
    --cc=kito.cheng@sifive.com \
    /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