[5/5] gdb, gdbserver, btrace: resync on error

Message ID 20261030135744.921562-6-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-aarch64 fail Patch failed to apply
linaro-tcwg-bot/tcwg_gdb_build--master-arm fail Patch failed to apply

Commit Message

Markus Metzger Oct. 30, 2026, 1:57 p.m. UTC
  Starting with libipt version 2.2, we can try to re-synchronize on the next
IP-providing packet instead of skipping everything until the next trace
stream synchronization packet on CPUs that do not support deferred TIPs.

Add a new setting

    set record btrace pt enhanced-resync on|off

to enable that on supported CPUs and extend the

    qXfer:btrace:read

response with a new attribute

    allow_resync

in the pt-config part of the response.

This should normally not be needed and the setting defaults to off.  When
running into decode errors, however, it may help as an advanced option to
make the most out of the recorded trace.
---
 gdb/NEWS                   | 12 +++++++
 gdb/btrace.c               | 68 +++++++++++++++++++++++++++++++++-----
 gdb/doc/gdb.texinfo        | 26 +++++++++++++++
 gdb/features/btrace.dtd    |  1 +
 gdb/nat/linux-btrace.c     |  1 +
 gdb/record-btrace.c        | 57 +++++++++++++++++++++++++++++++-
 gdb/record-btrace.h        |  5 +++
 gdb/remote.c               | 27 +++++++++++++--
 gdbserver/linux-low.cc     |  3 +-
 gdbsupport/btrace-common.h |  6 ++++
 10 files changed, 194 insertions(+), 12 deletions(-)
  

Comments

Eli Zaretskii Oct. 30, 2026, 2:31 p.m. UTC | #1
> From: Markus Metzger <markus.t.metzger@intel.com>
> Date: Fri, 30 Oct 2026 14:57:44 +0100
> 
> Starting with libipt version 2.2, we can try to re-synchronize on the next
> IP-providing packet instead of skipping everything until the next trace
> stream synchronization packet on CPUs that do not support deferred TIPs.
> 
> Add a new setting
> 
>     set record btrace pt enhanced-resync on|off
> 
> to enable that on supported CPUs and extend the
> 
>     qXfer:btrace:read
> 
> response with a new attribute
> 
>     allow_resync
> 
> in the pt-config part of the response.
> 
> This should normally not be needed and the setting defaults to off.  When
> running into decode errors, however, it may help as an advanced option to
> make the most out of the recorded trace.
> ---
>  gdb/NEWS                   | 12 +++++++
>  gdb/btrace.c               | 68 +++++++++++++++++++++++++++++++++-----
>  gdb/doc/gdb.texinfo        | 26 +++++++++++++++
>  gdb/features/btrace.dtd    |  1 +
>  gdb/nat/linux-btrace.c     |  1 +
>  gdb/record-btrace.c        | 57 +++++++++++++++++++++++++++++++-
>  gdb/record-btrace.h        |  5 +++
>  gdb/remote.c               | 27 +++++++++++++--
>  gdbserver/linux-low.cc     |  3 +-
>  gdbsupport/btrace-common.h |  6 ++++
>  10 files changed, 194 insertions(+), 12 deletions(-)

The documentation parts are okay, thanks.

Reviewed-By: Eli Zaretskii <eliz@gnu.org>
  

Patch

diff --git a/gdb/NEWS b/gdb/NEWS
index cddecf2040e..c582095068a 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -75,6 +75,11 @@  set record btrace pt return-compression on|off
 show record btrace pt return-compression
   Enable or disable Intel Processor Trace return compression (default: on).
 
+set record btrace pt enhanced-resync on|off
+show record btrace pt enhanced-resync
+  Enable or disable enhanced decoder synchronization after a trace
+  decoding error (default: off).
+
 * Changed commands
 
 set stop-on-solib-events on|off
@@ -92,6 +97,13 @@  info locals
 Qbtrace-conf:pt:return-compression
   Configure Intel Processor Trace return-compression settings.
 
+* Changed remote packets
+
+qXfer:btrace:read
+  The pt-config part of a pt response may now contain a new attribute
+  allow_resync to indicate to GDB that enhanced decoder synchronization
+  after a trace decoding error is supported on this processor.
+
 *** Changes in GDB 18
 
 * Support for the Common Trace Format (CTF) has been removed.  GDB now
diff --git a/gdb/btrace.c b/gdb/btrace.c
index 5c47f9129ae..b1c78075d0f 100644
--- a/gdb/btrace.c
+++ b/gdb/btrace.c
@@ -35,7 +35,7 @@ 
 #include "extension.h"
 #include "gdbarch.h"
 
-/* For maintenance commands.  */
+/* For maintenance commands and record_btrace_conf.  */
 #include "record-btrace.h"
 
 #include <inttypes.h>
@@ -1732,6 +1732,40 @@  ftrace_warn_pt (btrace_thread_info *btinfo, pt_insn_decoder *decoder,
 	   pt_errstr (pt_errcode (status)));
 }
 
+#if (LIBIPT_VERSION >= 0x202)
+/* Resynchronize at an IP-providing packet, if possible.  Returns the
+   decoder status, which is non-negative on success and negative
+   otherwise.  */
+
+static int
+ftrace_resync_pt (btrace_thread_info *btinfo, pt_insn_decoder *decoder,
+		  std::vector<unsigned int> &gaps)
+{
+  int status;
+
+  for (;;)
+    {
+      status = pt_insn_resync (decoder);
+      if (status >= 0)
+	return status;
+
+      if (status != -pte_event_ignored)
+	break;
+
+      /* This libipt version will HAVE_PT_INSN_EVENT.  */
+      status = handle_pt_insn_event (btinfo, decoder, gaps);
+      if (status < 0)
+	break;
+    }
+
+  if (status != -pte_eos)
+    warning (_("Resynchronization error (%d): %s"), status,
+	     pt_errstr (pt_errcode (status)));
+
+  return status;
+}
+#endif /* (LIBIPT_VERSION >= 0x202) */
+
 /* Decode the trace and handle events as long as there is no error.
    Returns the last decoder status.  */
 
@@ -1777,6 +1811,7 @@  ftrace_decode_pt (btrace_thread_info *btinfo,
 
 static void
 ftrace_add_pt (btrace_thread_info *btinfo,
+	       const btrace_data_pt_config *config,
 	       pt_insn_decoder *decoder,
 	       int *plevel,
 	       std::vector<unsigned int> &gaps)
@@ -1795,13 +1830,30 @@  ftrace_add_pt (btrace_thread_info *btinfo,
 	  break;
 	}
 
-      status = ftrace_decode_pt (btinfo, decoder, plevel, gaps, status);
-      if (status == -pte_eos)
-	break;
+      for (;;)
+	{
+	  status = ftrace_decode_pt (btinfo, decoder, plevel, gaps, status);
+	  gdb_assert (status < 0);
+	  if (status == -pte_eos)
+	    return;
+
+	  /* Indicate the gap in the trace.  */
+	  btrace_function *bfun = ftrace_new_gap (btinfo, status, gaps);
+	  ftrace_warn_pt (btinfo, decoder, bfun, status);
 
-      /* Indicate the gap in the trace.  */
-      btrace_function *bfun = ftrace_new_gap (btinfo, status, gaps);
-      ftrace_warn_pt (btinfo, decoder, bfun, status);
+#if (LIBIPT_VERSION >= 0x202)
+	  if (record_btrace_conf.pt.enhanced_resync
+	      && config->allow_resync)
+	    status = ftrace_resync_pt (btinfo, decoder, gaps);
+#endif
+	  if (status < 0)
+	    {
+	      if (status == -pte_eos)
+		return;
+
+	      break;
+	    }
+	}
     }
 }
 
@@ -1928,7 +1980,7 @@  btrace_compute_ftrace_pt (struct thread_info *tp,
 	error (_("Failed to configure the Intel Processor Trace decoder: "
 		 "%s."), pt_errstr (pt_errcode (errcode)));
 
-      ftrace_add_pt (btinfo, decoder, &level, gaps);
+      ftrace_add_pt (btinfo, &btrace->config, decoder, &level, gaps);
     }
   catch (const gdb_exception &error)
     {
diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
index 8cfaffd4fc7..9462dccc205 100644
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -8433,6 +8433,31 @@  overhead and trace size.  The default is on.
 @item show record btrace pt return-compression
 Show the current setting of Intel Processor Trace return compression.
 
+@item set record btrace pt enhanced-resync
+Enable or disable enhanced synchronization.  When disabled and trace
+decode fails with an error, the decoder synchronizes at the next trace
+synchronization packet sequence (PSB+).  This provides the initial
+decode state for the next trace section.  This is a safe option but
+may skip a fair amount of trace.
+
+When enabled and trace decode fails with an error, the decoder
+synchronizes at the next packet that provides a full instruction
+address on processors that do not use deferred TIP packets.  This
+usually skips a lot less trace, but it does not allow the decoder to
+maintain its decode state, which may lead to subsequent decode errors.
+
+Most notably, the call stack will not be maintained for direct calls,
+which may lead to decode errors when encountering a compressed return.
+Disabling return compression avoids that at the cost of increased
+trace size and recording overhead.
+
+The default is off.  The setting may be changed at any time.  When the
+setting is changed, threads stop replaying and trace is re-decoded
+with the new setting.
+
+@item show record btrace pt enhanced-resync
+Show the current setting of enhanced synchronization.
+
 @kindex info record
 @item info record
 Show various statistics about the recording depending on the recording
@@ -49400,6 +49425,7 @@  The formal DTD for the branch trace format is given below:
 <!ELEMENT pt (pt-config?, raw?)>
 
 <!ELEMENT pt-config (cpu?)>
+<!ATTLIST pt-config allow_resync (yes | no) #IMPLIED>
 
 <!ELEMENT cpu EMPTY>
 <!ATTLIST cpu vendor   CDATA #REQUIRED
diff --git a/gdb/features/btrace.dtd b/gdb/features/btrace.dtd
index a7d5db7faf6..9342bc0cbcc 100644
--- a/gdb/features/btrace.dtd
+++ b/gdb/features/btrace.dtd
@@ -14,6 +14,7 @@ 
 <!ELEMENT pt (pt-config?, raw?)>
 
 <!ELEMENT pt-config (cpu?)>
+<!ATTLIST pt-config allow_resync (yes | no) #IMPLIED>
 
 <!ELEMENT cpu EMPTY>
 <!ATTLIST cpu vendor   CDATA #REQUIRED
diff --git a/gdb/nat/linux-btrace.c b/gdb/nat/linux-btrace.c
index 8f0c4c1b442..38b617a82b4 100644
--- a/gdb/nat/linux-btrace.c
+++ b/gdb/nat/linux-btrace.c
@@ -947,6 +947,7 @@  static void
 linux_fill_btrace_pt_config (struct btrace_data_pt_config *conf)
 {
   conf->cpu = btrace_this_cpu ();
+  conf->allow_resync = linux_supports_pt_feature ("topa_multiple_entries");
 }
 
 /* Read branch trace data in Intel Processor Trace format for the thread
diff --git a/gdb/record-btrace.c b/gdb/record-btrace.c
index 30cd8c8b2b6..115a954e056 100644
--- a/gdb/record-btrace.c
+++ b/gdb/record-btrace.c
@@ -186,7 +186,7 @@  static struct async_event_handler *record_btrace_async_inferior_event_handler;
 static int record_btrace_generating_corefile;
 
 /* The current branch trace configuration.  */
-static struct btrace_config record_btrace_conf;
+btrace_config record_btrace_conf;
 
 /* Command list for "record btrace".  */
 static struct cmd_list_element *record_btrace_cmdlist;
@@ -3167,6 +3167,38 @@  set_record_pt_return_compression_value (const char *args, int from_tty,
   record_btrace_conf.pt.return_compression = return_compression;
 }
 
+static bool enhanced_resync = false;
+
+/* The "record pt enhanced-resync" show value function.  */
+
+static void
+show_record_pt_enhanced_resync_value (ui_file *file, int from_tty,
+				      cmd_list_element *c, const char *value)
+{
+  gdb_printf (file, _("record pt enhanced-resync is %s.\n"), value);
+}
+
+/* The "record pt enhanced-resync" set value function.  */
+
+static void
+set_record_pt_enhanced_resync_value (const char *args, int from_tty,
+				     cmd_list_element *c)
+{
+#if (LIBIPT_VERSION >= 0x202)
+  if (record_btrace_conf.pt.enhanced_resync != enhanced_resync)
+    {
+      record_btrace_conf.pt.enhanced_resync = enhanced_resync;
+
+      /* Clear the trace of all threads so we decode with the new
+	 settings.  This also stops replaying.  */
+      for (thread_info &tp : all_non_exited_threads ())
+	btrace_clear (&tp);
+    }
+#else
+  error (_("Enhanced synchronization is not supported by GDB.\n"));
+#endif
+}
+
 /* Initialize btrace commands.  */
 
 INIT_GDB_FILE (record_btrace)
@@ -3311,8 +3343,30 @@  has no effect on an active recording.  The default is \"on\"."),
 			   &set_record_btrace_pt_cmdlist,
 			   &show_record_btrace_pt_cmdlist);
 
+  add_setshow_boolean_cmd
+    ("enhanced-resync", no_class, &enhanced_resync,
+     _("Set enhanced synchronizing after a decode error."),
+     _("Show enhanced synchronizing after a decode error."),
+     _("\
+Use \"on\" to enable enhanced synchronization and \"off\" to disable it.\n\n\
+After a trace decoding error, decode synchronizes at special packets (PSB+)\n\
+in the trace stream that supply the initial decode state.  This may skip a\n\
+fair amount of trace.\n\
+With this option, the decoder tries to synchronize earlier at packets that\n\
+provide a full instruction address.  This usually skips much less trace, but\n\
+may result in subsequent decode errors because the decoder is not able to\n\
+update its state while skipping packets.  Disabling return compression may\n\
+help in this situation.\n\n\
+This setting is \"off\" by default and may be changed at any time.  When the\n\
+setting changes, stop replaying and re-decode the trace for all threads."),
+     set_record_pt_enhanced_resync_value,
+     show_record_pt_enhanced_resync_value,
+     &set_record_btrace_pt_cmdlist,
+     &show_record_btrace_pt_cmdlist);
+
   add_target (record_btrace_target_info, record_btrace_target_open);
 
+  memset (&record_btrace_conf, 0, sizeof (record_btrace_conf));
   record_btrace_conf.bts.size = 64 * 1024;
   record_btrace_conf.pt.size = 16 * 1024;
 #if (LIBIPT_VERSION >= 0x200)
@@ -3322,4 +3376,5 @@  has no effect on an active recording.  The default is \"on\"."),
 #endif
   record_btrace_conf.pt.event_tracing = false;
   record_btrace_conf.pt.return_compression = return_compression;
+  record_btrace_conf.pt.enhanced_resync = enhanced_resync;
 }
diff --git a/gdb/record-btrace.h b/gdb/record-btrace.h
index 99ea0741709..caa17aa7b2b 100644
--- a/gdb/record-btrace.h
+++ b/gdb/record-btrace.h
@@ -22,6 +22,8 @@ 
 #ifndef GDB_RECORD_BTRACE_H
 #define GDB_RECORD_BTRACE_H
 
+#include "gdbsupport/btrace-common.h"
+
 /* Push the record_btrace target.  */
 extern void record_btrace_push_target (void);
 
@@ -29,4 +31,7 @@  extern void record_btrace_push_target (void);
    NULL if the cpu was configured as auto.  */
 extern const struct btrace_cpu *record_btrace_get_cpu (void);
 
+/* The current branch trace configuration.  */
+extern btrace_config record_btrace_conf;
+
 #endif /* GDB_RECORD_BTRACE_H */
diff --git a/gdb/remote.c b/gdb/remote.c
index 3e564fdd91f..2cf735a1f2a 100644
--- a/gdb/remote.c
+++ b/gdb/remote.c
@@ -15228,6 +15228,22 @@  parse_xml_btrace_pt_config_cpu (struct gdb_xml_parser *parser,
   btrace->variant.pt.config.cpu.stepping = *stepping;
 }
 
+/* Parse a btrace pt-config xml record.  */
+
+static void
+parse_xml_btrace_pt_config (gdb_xml_parser *parser,
+			    const gdb_xml_element *element, void *user_data,
+			    std::vector<gdb_xml_value> &attributes)
+{
+  btrace_data *btrace = (btrace_data *) user_data;
+  gdb_xml_value *allow_resync = xml_find_attribute (attributes,
+						    "allow_resync");
+  btrace->variant.pt.config.allow_resync
+    = ((allow_resync != nullptr)
+       ? (bool) *(ULONGEST *) allow_resync->value.get ()
+       : false);
+}
+
 /* Parse a btrace pt "raw" xml record.  */
 
 static void
@@ -15255,6 +15271,7 @@  parse_xml_btrace_pt (struct gdb_xml_parser *parser,
   btrace = (struct btrace_data *) user_data;
   btrace->format = BTRACE_FORMAT_PT;
   btrace->variant.pt.config.cpu.vendor = CV_UNKNOWN;
+  btrace->variant.pt.config.allow_resync = false;
   btrace->variant.pt.data = NULL;
   btrace->variant.pt.size = 0;
 }
@@ -15273,6 +15290,12 @@  static const struct gdb_xml_attribute btrace_pt_config_cpu_attributes[] = {
   { NULL, GDB_XML_AF_NONE, NULL, NULL }
 };
 
+static const struct gdb_xml_attribute btrace_pt_config_attributes[] = {
+  { "allow_resync", GDB_XML_AF_OPTIONAL, gdb_xml_parse_attr_enum,
+    gdb_xml_enums_boolean },
+  { NULL, GDB_XML_AF_NONE, NULL, NULL }
+};
+
 static const struct gdb_xml_element btrace_pt_config_children[] = {
   { "cpu", btrace_pt_config_cpu_attributes, NULL, GDB_XML_EF_OPTIONAL,
     parse_xml_btrace_pt_config_cpu, NULL },
@@ -15280,8 +15303,8 @@  static const struct gdb_xml_element btrace_pt_config_children[] = {
 };
 
 static const struct gdb_xml_element btrace_pt_children[] = {
-  { "pt-config", NULL, btrace_pt_config_children, GDB_XML_EF_OPTIONAL, NULL,
-    NULL },
+  { "pt-config", btrace_pt_config_attributes, btrace_pt_config_children,
+    GDB_XML_EF_OPTIONAL, parse_xml_btrace_pt_config, NULL },
   { "raw", NULL, NULL, GDB_XML_EF_OPTIONAL, NULL, parse_xml_btrace_pt_raw },
   { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
 };
diff --git a/gdbserver/linux-low.cc b/gdbserver/linux-low.cc
index 7bcb85e641a..661447a8062 100644
--- a/gdbserver/linux-low.cc
+++ b/gdbserver/linux-low.cc
@@ -6889,7 +6889,8 @@  static void
 linux_low_encode_pt_config (std::string *buffer,
 			    const struct btrace_data_pt_config *config)
 {
-  *buffer += "<pt-config>\n";
+  string_xml_appendf (*buffer, "<pt-config allow_resync=\"%s\">\n",
+		      (config->allow_resync ? "yes" : "no"));
 
   switch (config->cpu.vendor)
     {
diff --git a/gdbsupport/btrace-common.h b/gdbsupport/btrace-common.h
index f2b274c79a7..10c58ef3c88 100644
--- a/gdbsupport/btrace-common.h
+++ b/gdbsupport/btrace-common.h
@@ -129,6 +129,9 @@  struct btrace_config_pt
 
   /* Return compression setting.  */
   bool return_compression;
+
+  /* Configuration bit for enhanced synchronization after decode errors.  */
+  bool enhanced_resync;
 };
 
 /* A branch tracing configuration.
@@ -164,6 +167,9 @@  struct btrace_data_pt_config
 {
   /* The processor on which the trace has been collected.  */
   struct btrace_cpu cpu;
+
+  /* Whether the processor supports resyncing on IP-providing packets.  */
+  bool allow_resync;
 };
 
 /* Branch trace in Intel Processor Trace format.  */