Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Markus Metzger <markus.t.metzger@intel.com>
To: gdb-patches@sourceware.org
Subject: [PATCH 2/5] gdb, btrace: refactor ftrace_add_pt()
Date: Fri, 30 Oct 2026 14:57:41 +0100	[thread overview]
Message-ID: <20261030135744.921562-3-markus.t.metzger@intel.com> (raw)
In-Reply-To: <20261030135744.921562-1-markus.t.metzger@intel.com>

Extract the main decode loop and generating a decode error warning out of
ftrace_add_pt() to prepare for a subsequent patch to extend decode error
handling.

There should be no functional change except that the decode error warning
would now print pc=? if an error was added after a preceding gap or
auxiliary instruction instead of the PC of the last real instruction.
The PC value now matches the instruction offset that is also printed with
the decode error warning.
---
 gdb/btrace.c | 112 +++++++++++++++++++++++++++++++--------------------
 1 file changed, 69 insertions(+), 43 deletions(-)

diff --git a/gdb/btrace.c b/gdb/btrace.c
index ddc7b2551df..5c47f9129ae 100644
--- a/gdb/btrace.c
+++ b/gdb/btrace.c
@@ -1710,26 +1710,83 @@ handle_pt_insn_event_flags (struct btrace_thread_info *btinfo,
 #endif /* defined (HAVE_STRUCT_PT_INSN_RESYNCED) */
 }
 
+/* Generate a decode error warning.  */
+
+static void
+ftrace_warn_pt (btrace_thread_info *btinfo, pt_insn_decoder *decoder,
+		btrace_function *bfun, int status)
+{
+  gdb_assert (bfun != nullptr);
+
+  unsigned int ioffset = bfun->insn_offset - 1;
+  uint64_t offset;
+  pt_insn_get_offset (decoder, &offset);
+
+  btrace_insn_iterator it;
+  int found = btrace_find_insn_by_number (&it, btinfo, ioffset);
+  const btrace_insn *binsn = (found != 0) ? btrace_insn_get (&it) : nullptr;
+
+  warning (_("Decode error (%d) at instruction %u (offset = 0x%" PRIx64
+	     ", pc = %s): %s."), status, ioffset, offset,
+	   (binsn != nullptr) ? hex_string (binsn->pc) : "?",
+	   pt_errstr (pt_errcode (status)));
+}
+
+/* Decode the trace and handle events as long as there is no error.
+   Returns the last decoder status.  */
+
+static int
+ftrace_decode_pt (btrace_thread_info *btinfo,
+		  pt_insn_decoder *decoder,
+		  int *plevel,
+		  std::vector<unsigned int> &gaps,
+		  int status)
+{
+  gdb_assert (status >= 0);
+  for (;;)
+    {
+#if defined (HAVE_PT_INSN_EVENT)
+      /* Handle events from the previous iteration or synchronization.  */
+      while (status & pts_event_pending)
+	{
+	  status = handle_pt_insn_event (btinfo, decoder, gaps);
+	  if (status < 0)
+	    return status;
+	}
+#endif
+      pt_insn insn;
+      status = pt_insn_next (decoder, &insn, sizeof (insn));
+      if (status < 0)
+	return status;
+
+      /* Handle events indicated by flags in INSN.  */
+      handle_pt_insn_event_flags (btinfo, decoder, insn, gaps);
+
+      btrace_function *bfun
+	= ftrace_update_function (btinfo,
+				  std::make_optional<CORE_ADDR> (insn.ip));
+
+      /* Maintain the function level offset.  */
+      *plevel = std::min (*plevel, bfun->level);
+
+      ftrace_update_insns (bfun, pt_btrace_insn (insn));
+    }
+}
+
 /* Add function branch trace to BTINFO using DECODER.  */
 
 static void
-ftrace_add_pt (struct btrace_thread_info *btinfo,
-	       struct pt_insn_decoder *decoder,
+ftrace_add_pt (btrace_thread_info *btinfo,
+	       pt_insn_decoder *decoder,
 	       int *plevel,
 	       std::vector<unsigned int> &gaps)
 {
-  struct btrace_function *bfun;
-  uint64_t offset;
-  int status;
-
   /* Register the ptwrite filter.  */
   apply_ext_lang_ptwrite_filter (btinfo);
 
   for (;;)
     {
-      struct pt_insn insn;
-
-      status = pt_insn_sync_forward (decoder);
+      int status = pt_insn_sync_forward (decoder);
       if (status < 0)
 	{
 	  if (status != -pte_eos)
@@ -1738,44 +1795,13 @@ ftrace_add_pt (struct btrace_thread_info *btinfo,
 	  break;
 	}
 
-      for (;;)
-	{
-#if defined (HAVE_PT_INSN_EVENT)
-	  /* Handle events from the previous iteration or synchronization.  */
-	  while ((status != 0) && (status & pts_event_pending))
-	    status = handle_pt_insn_event (btinfo, decoder, gaps);
-#endif
-	  if (status < 0)
-	    break;
-
-	  status = pt_insn_next (decoder, &insn, sizeof(insn));
-	  if (status < 0)
-	    break;
-
-	  /* Handle events indicated by flags in INSN.  */
-	  handle_pt_insn_event_flags (btinfo, decoder, insn, gaps);
-
-	  bfun
-	    = ftrace_update_function (btinfo,
-				      std::make_optional<CORE_ADDR> (insn.ip));
-
-	  /* Maintain the function level offset.  */
-	  *plevel = std::min (*plevel, bfun->level);
-
-	  ftrace_update_insns (bfun, pt_btrace_insn (insn));
-	}
-
+      status = ftrace_decode_pt (btinfo, decoder, plevel, gaps, status);
       if (status == -pte_eos)
 	break;
 
       /* Indicate the gap in the trace.  */
-      bfun = ftrace_new_gap (btinfo, status, gaps);
-
-      pt_insn_get_offset (decoder, &offset);
-
-      warning (_("Decode error (%d) at instruction %u (offset = 0x%" PRIx64
-		 ", pc = 0x%" PRIx64 "): %s."), status, bfun->insn_offset - 1,
-	       offset, insn.ip, pt_errstr (pt_errcode (status)));
+      btrace_function *bfun = ftrace_new_gap (btinfo, status, gaps);
+      ftrace_warn_pt (btinfo, decoder, bfun, status);
     }
 }
 
-- 
2.53.0

________________________________________
Intel Deutschland GmbH 

Registered Address: Dornacher Strasse 1, 85622 Feldkirchen, Germany 

Tel: +49 (89) 99143-0 

www.intel.de 

Managing Directors: Candice Moore, Jeffrey Schneiderman, Ramachandran Sitaraman

Chairperson of the Supervisory Board: Sonja Pierer

Registered Seat: Munich Commercial Register B: Amtsgericht Munich HRB 186928

This e-mail and any attachments may contain confidential material for
the sole use of the intended recipient(s). Any review or distribution
by others is strictly prohibited. If you are not the intended
recipient, please contact the sender and delete all copies.


  parent reply	other threads:[~2026-10-30 14:00 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-30 13:57 [PATCH 0/5] gdb, gdbserver, btrace: enhanced resync on decode error Markus Metzger
2026-10-30 13:57 ` [PATCH 1/5] gdb, btrace: refactor handle_pt_insn_events() Markus Metzger
2026-10-30 13:57 ` Markus Metzger [this message]
2026-10-30 13:57 ` [PATCH 3/5] gdb, btrace: fix btrace remote configuration Markus Metzger
2026-10-30 13:57 ` [PATCH 4/5] gdb, gdbserver, btrace: allow disabling return compression Markus Metzger
2026-10-30 14:30   ` Eli Zaretskii
2026-10-30 13:57 ` [PATCH 5/5] gdb, gdbserver, btrace: resync on error Markus Metzger
2026-10-30 14:31   ` Eli Zaretskii

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=20261030135744.921562-3-markus.t.metzger@intel.com \
    --to=markus.t.metzger@intel.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