[2/5] gdb, btrace: refactor ftrace_add_pt()

Message ID 20261030135744.921562-3-markus.t.metzger@intel.com
State New
Headers
Series gdb, gdbserver, btrace: enhanced resync on decode error |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gdb_build--master-arm fail Patch failed to apply
linaro-tcwg-bot/tcwg_gdb_build--master-aarch64 fail Patch failed to apply

Commit Message

Markus Metzger Oct. 30, 2026, 1:57 p.m. UTC
  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(-)
  

Patch

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);
     }
 }