Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: markus.t.metzger@intel.com
To: gdb-patches@sourceware.org
Cc: markus.t.metzger@gmail.com, jan.kratochvil@redhat.com,
	palves@redhat.com,        tromey@redhat.com, kettenis@gnu.org,
	       Markus Metzger <markus.t.metzger@intel.com>
Subject: [patch v5 06/12] remote, btrace: add branch trace remote ops
Date: Fri, 07 Dec 2012 10:37:00 -0000	[thread overview]
Message-ID: <1354876644-25749-7-git-send-email-markus.t.metzger@intel.com> (raw)
In-Reply-To: <1354876644-25749-1-git-send-email-markus.t.metzger@intel.com>

From: Markus Metzger <markus.t.metzger@intel.com>

Add the gdb remote target operations for branch tracing. We define the following
packets:

  qbtrace:<ptid>  query if new trace data is available for a thread
                  returns "yes" or "no" or "Enn"

  Qbtrace:<ptid>:on  enable branch tracing for one thread
                     returns "OK" or "Enn"

  Qbtrace:<ptid>:off  disable branch tracing for one thread
                      returns "OK" or "Enn"

  qXfer:btrace:read   read the full branch trace data for the current thread

2012-12-07 Markus Metzger <markus.t.metzger@intel.com>

	* target.h (enum target_object): Add TARGET_OBJECT_BTRACE.
	* remote.c: Include btrace.h.
	(struct btrace_target_info): New struct.
	(remote_supports_btrace): New function.
	(send_Qbtrace): New function.
	(remote_enable_btrace): New function.
	(remote_disable_btrace): New function.
	(remote_btrace_has_changed): New function.
	(remote_read_btrace): New function.
	(init_remote_ops): Add btrace ops.
	(enum <unnamed>): Add btrace packets.
	(struct protocol_feature remote_protocol_features[]): Add btrace packets.
	(_initialize_remote): Add packet configuration for branch tracing.


---
 gdb/remote.c |  171 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 gdb/target.h |    4 +-
 2 files changed, 174 insertions(+), 1 deletions(-)
 mode change 100644 => 100755 gdb/remote.c

diff --git a/gdb/remote.c b/gdb/remote.c
old mode 100644
new mode 100755
index 929d4f5..a502c2c
--- a/gdb/remote.c
+++ b/gdb/remote.c
@@ -67,6 +67,7 @@
 #include "ax.h"
 #include "ax-gdb.h"
 #include "agent.h"
+#include "btrace.h"
 
 /* Temp hacks for tracepoint encoding migration.  */
 static char *target_buf;
@@ -1292,6 +1293,9 @@ enum {
   PACKET_qXfer_fdpic,
   PACKET_QDisableRandomization,
   PACKET_QAgent,
+  PACKET_qbtrace,
+  PACKET_Qbtrace,
+  PACKET_qXfer_btrace,
   PACKET_MAX
 };
 
@@ -3946,6 +3950,10 @@ static struct protocol_feature remote_protocol_features[] = {
   { "QAgent", PACKET_DISABLE, remote_supported_packet, PACKET_QAgent},
   { "tracenz", PACKET_DISABLE,
     remote_string_tracing_feature, -1 },
+  { "qbtrace", PACKET_DISABLE, remote_supported_packet, PACKET_qbtrace },
+  { "Qbtrace", PACKET_DISABLE, remote_supported_packet, PACKET_Qbtrace },
+  { "qXfer:btrace:read", PACKET_DISABLE, remote_supported_packet,
+    PACKET_qXfer_btrace }
 };
 
 static char *remote_support_xml;
@@ -8682,6 +8690,10 @@ remote_xfer_partial (struct target_ops *ops, enum target_object object,
       return remote_read_qxfer (ops, "uib", annex, readbuf, offset, len,
 				&remote_protocol_packets[PACKET_qXfer_uib]);
 
+    case TARGET_OBJECT_BTRACE:
+      return remote_read_qxfer (ops, "btrace", annex, readbuf, offset, len,
+        &remote_protocol_packets[PACKET_qXfer_btrace]);
+
     default:
       return -1;
     }
@@ -11001,6 +11013,151 @@ remote_can_use_agent (void)
   return (remote_protocol_packets[PACKET_QAgent].support != PACKET_DISABLE);
 }
 
+struct btrace_target_info
+{
+  /* The ptid of the traced thread.  */
+  ptid_t ptid;
+};
+
+/* Check whether the target supports branch tracing.  */
+
+static int
+remote_supports_btrace (void)
+{
+  if (remote_protocol_packets[PACKET_qbtrace].support != PACKET_ENABLE)
+    return 0;
+  if (remote_protocol_packets[PACKET_Qbtrace].support != PACKET_ENABLE)
+    return 0;
+  if (remote_protocol_packets[PACKET_qXfer_btrace].support != PACKET_ENABLE)
+    return 0;
+
+  return 1;
+}
+
+/* Send the Qbtrace packet and check the response. */
+
+static void
+send_Qbtrace (ptid_t ptid, int enable)
+{
+  struct packet_config *packet = &remote_protocol_packets[PACKET_Qbtrace];
+  struct remote_state *rs = get_remote_state ();
+  char *buf = rs->buf;
+  char *endbuf = rs->buf + get_remote_packet_size ();
+
+  if (packet->support != PACKET_ENABLE)
+    error (_("Target does not support branch tracing."));
+
+  set_general_thread (ptid);
+
+  buf += xsnprintf (buf, endbuf - buf, "%s:", packet->name);
+  buf += xsnprintf (buf, endbuf - buf, enable ? "on" : "off");
+  putpkt (rs->buf);
+  getpkt (&rs->buf, &rs->buf_size, 0);
+
+  if (packet_ok (rs->buf, packet) == PACKET_ERROR)
+    error (_("Could not %s branch tracing for %s: %s"),
+           enable ? "enable" : "disable", target_pid_to_str (ptid), rs->buf);
+}
+
+/* Enable branch tracing.  */
+
+static struct btrace_target_info *
+remote_enable_btrace (ptid_t ptid)
+{
+  struct btrace_target_info *tinfo = NULL;
+
+  /* This will throw an error if enabling failed.  */
+  send_Qbtrace (ptid, 1);
+
+  tinfo = xzalloc (sizeof (*tinfo));
+  tinfo->ptid = ptid;
+
+  return tinfo;
+}
+
+/* Disable branch tracing.  */
+
+static void
+remote_disable_btrace (struct btrace_target_info *tinfo)
+{
+  /* This will throw an error if disabling failed.  */
+  send_Qbtrace (tinfo->ptid, 0);
+
+  xfree (tinfo);
+}
+
+/* Check whether branch trace data has changed.  */
+
+static int
+remote_btrace_has_changed (struct btrace_target_info *tinfo)
+{
+  struct packet_config *packet = &remote_protocol_packets[PACKET_qbtrace];
+  struct remote_state *rs = get_remote_state ();
+  char *buf = rs->buf;
+  char *endbuf = rs->buf + get_remote_packet_size ();
+
+  if (packet->support != PACKET_ENABLE)
+    error (_("Target does not support branch tracing."));
+
+  buf += xsnprintf (buf, endbuf - buf, "%s:", packet->name);
+  buf = write_ptid (buf, endbuf, tinfo->ptid);
+  putpkt (rs->buf);
+  getpkt (&rs->buf, &rs->buf_size, 0);
+
+  switch (packet_ok (rs->buf, packet))
+    {
+    case PACKET_OK:
+      break;
+
+    case PACKET_UNKNOWN:
+      return 0;
+
+    case PACKET_ERROR:
+      error (_("Failed to check for branch trace data for %s: %s."),
+             target_pid_to_str (tinfo->ptid), rs->buf);
+    }
+
+  if (strcmp (rs->buf, "yes") == 0)
+	return 1;
+
+  if (strcmp (rs->buf, "no") == 0)
+	return 0;
+
+  error (_("Bad remote reply: %s."), rs->buf);
+
+  return 0;
+}
+
+/* Read the branch trace.  */
+
+static VEC (btrace_block_s) *
+remote_read_btrace (struct btrace_target_info *tinfo)
+{
+  struct packet_config *packet = &remote_protocol_packets[PACKET_qXfer_btrace];
+  struct remote_state *rs = get_remote_state ();
+  VEC (btrace_block_s) *btrace = NULL;
+  char *xml;
+
+  if (packet->support != PACKET_ENABLE)
+    error (_("Target does not support branch tracing."));
+
+#if !defined(HAVE_LIBEXPAT)
+  error (_("Cannot process branch tracing result. XML parsing not supported."));
+#endif
+
+  xml = target_read_stralloc (&current_target,
+                              TARGET_OBJECT_BTRACE, NULL);
+  if (xml)
+    {
+      struct cleanup *cleanup = make_cleanup (xfree, xml);
+
+      btrace = parse_xml_btrace (xml);
+      do_cleanups (cleanup);
+    }
+
+  return btrace;
+}
+
 static void
 init_remote_ops (void)
 {
@@ -11117,6 +11274,11 @@ Specify the serial device it is connected to\n\
   remote_ops.to_traceframe_info = remote_traceframe_info;
   remote_ops.to_use_agent = remote_use_agent;
   remote_ops.to_can_use_agent = remote_can_use_agent;
+  remote_ops.to_supports_btrace = remote_supports_btrace;
+  remote_ops.to_enable_btrace = remote_enable_btrace;
+  remote_ops.to_disable_btrace = remote_disable_btrace;
+  remote_ops.to_btrace_has_changed = remote_btrace_has_changed;
+  remote_ops.to_read_btrace = remote_read_btrace;
 }
 
 /* Set up the extended remote vector by making a copy of the standard
@@ -11643,6 +11805,15 @@ Show the maximum size of the address (in bits) in a memory packet."), NULL,
   add_packet_config_cmd (&remote_protocol_packets[PACKET_QAgent],
 			 "QAgent", "agent", 0);
 
+  add_packet_config_cmd (&remote_protocol_packets[PACKET_qbtrace],
+       "qbtrace", "query-btrace", 0);
+
+  add_packet_config_cmd (&remote_protocol_packets[PACKET_Qbtrace],
+       "Qbtrace", "enable-btrace", 0);
+
+  add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_btrace],
+       "qXfer:btrace", "read-btrace", 0);
+
   /* Keep the old ``set remote Z-packet ...'' working.  Each individual
      Z sub-packet has its own set and show commands, but users may
      have sets to this variable in their .gdbinit files (or in their
diff --git a/gdb/target.h b/gdb/target.h
index 4889ce4..ffdd512 100644
--- a/gdb/target.h
+++ b/gdb/target.h
@@ -287,7 +287,9 @@ enum target_object
   /* Darwin dynamic linker info data.  */
   TARGET_OBJECT_DARWIN_DYLD_INFO,
   /* OpenVMS Unwind Information Block.  */
-  TARGET_OBJECT_OPENVMS_UIB
+  TARGET_OBJECT_OPENVMS_UIB,
+  /* Branch trace data, in XML format.  */
+  TARGET_OBJECT_BTRACE
   /* Possible future objects: TARGET_OBJECT_FILE, ...  */
 };
 
-- 
1.7.1


  parent reply	other threads:[~2012-12-07 10:37 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-12-07 10:37 [patch v5 00/12] branch tracing support for Atom markus.t.metzger
2012-12-07 10:37 ` [patch v5 09/12] gdbserver, linux, btrace: add btrace support for linux-low markus.t.metzger
2012-12-07 10:37 ` markus.t.metzger [this message]
2012-12-07 10:38 ` [patch v5 12/12] btrace, x86: disable on some processors markus.t.metzger
2012-12-07 10:38 ` [patch v5 07/12] btrace, doc: document remote serial protocol markus.t.metzger
2012-12-07 15:09   ` Eli Zaretskii
2012-12-13  8:43     ` Metzger, Markus T
2012-12-13 17:02       ` Eli Zaretskii
2012-12-13 17:09       ` Pedro Alves
2012-12-14  7:53         ` Metzger, Markus T
2012-12-07 10:38 ` [patch v5 04/12] linux, i386, amd64: enable btrace for 32bit and 64bit linux native markus.t.metzger
2012-12-07 10:38 ` [patch v5 08/12] gdbserver, btrace: add generic btrace support markus.t.metzger
2012-12-07 10:38 ` [patch v5 10/12] test, btrace: add branch tracing tests markus.t.metzger
2012-12-07 10:38 ` [patch v5 05/12] xml, btrace: define btrace xml document style markus.t.metzger
2012-12-07 10:38 ` [patch v5 03/12] linux, btrace: perf_event based branch tracing markus.t.metzger
2012-12-07 10:38 ` [patch v5 01/12] thread, btrace: add generic branch trace support markus.t.metzger
2012-12-07 10:38 ` [patch v5 02/12] cli, btrace: add btrace cli markus.t.metzger
2012-12-07 15:16   ` Eli Zaretskii
2012-12-13  9:14     ` Metzger, Markus T
2012-12-07 10:39 ` [patch v5 11/12] test, btrace: more branch tracing tests markus.t.metzger

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=1354876644-25749-7-git-send-email-markus.t.metzger@intel.com \
    --to=markus.t.metzger@intel.com \
    --cc=gdb-patches@sourceware.org \
    --cc=jan.kratochvil@redhat.com \
    --cc=kettenis@gnu.org \
    --cc=markus.t.metzger@gmail.com \
    --cc=palves@redhat.com \
    --cc=tromey@redhat.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