[07/10] btrace, linux: Enable ptwrite packets.

Message ID 1559119673-30516-8-git-send-email-felix.willgerodt@intel.com
State New, archived
Headers

Commit Message

Willgerodt, Felix May 29, 2019, 8:47 a.m. UTC
  From: Felix Willgerodt <felix.willgerodt@intel.com>

Enable ptwrite in the PT config if the kernel supports it.

2019-05-29  Felix Willgerodt  <felix.willgerodt@intel.com>

gdb/ChangeLog:
	* nat/linux-btrace.c (linux_supports_ptw): New function.
	(linux_enable_pt): Set attr.config if ptwrite is supported.

---
 gdb/nat/linux-btrace.c | 32 ++++++++++++++++++++++++++++++++
 1 file changed, 32 insertions(+)
  

Comments

Metzger, Markus T June 4, 2019, 12:36 p.m. UTC | #1
Hello Felix,

> diff --git a/gdb/nat/linux-btrace.c b/gdb/nat/linux-btrace.c
> index ef291ec2310..6eb5334e0ae 100644
> --- a/gdb/nat/linux-btrace.c
> +++ b/gdb/nat/linux-btrace.c
> @@ -32,6 +32,10 @@
> 
>  #include <sys/syscall.h>
> 
> +#if defined (HAVE_LIBIPT)
> +#include <intel-pt.h>
> +#endif

This code is used for gdbserver, as well, which doesn't link against libipt.so.  Also, we don't
seem to be using anything from that header - and we shouldn't.

> +
>  #if HAVE_LINUX_PERF_EVENT_H && defined(SYS_perf_event_open)
>  #include <unistd.h>
>  #include <sys/mman.h>
> @@ -409,6 +413,31 @@ cpu_supports_bts (void)
>      }
>  }
> 
> +#if defined (PERF_ATTR_SIZE_VER5)

This guard shouldn't be needed.  We're not using anything from struct perf_event_attr here.

> +/* Check whether the linux target supports Intel Processor Trace PTWRITE.  */
> +
> +static bool
> +linux_supports_ptwrite ()
> +{
> +  static const char filename[]
> +      = "/sys/bus/event_source/devices/intel_pt/caps/ptwrite";
> +  gdb_file_up file = gdb_fopen_cloexec (filename, "r");
> +
> +  if (file.get () == nullptr)
> +    return false;
> +
> +  int status, found = fscanf (file.get (), "%d", &status);
> +
> +  if (found != 1)
> +    {
> +      warning (_("Failed to determine ptwrite support from %s."), filename);
> +      return false;
> +    }
> +
> +  return status == 1;
> +}
> +#endif /* defined (PERF_ATTR_SIZE_VER5) */
> +
>  /* The perf_event_open syscall failed.  Try to print a helpful error
>     message.  */
> 
> @@ -601,6 +630,9 @@ linux_enable_pt (ptid_t ptid, const struct
> btrace_config_pt *conf)
>    pt->attr.exclude_hv = 1;
>    pt->attr.exclude_idle = 1;
> 
> +  if (linux_supports_ptwrite ())
> +    pt->attr.config |= 0x1000;

Doing this unconditionally may break GDBs that have been built with an old libipt that
doesn't support the PTW packet.

We should probably have GDB request PTW via CONF.  This may need extending RSP.

> +
>    errno = 0;
>    scoped_fd fd (syscall (SYS_perf_event_open, &pt->attr, pid, -1, -1, 0));
>    if (fd.get () < 0)
> --
> 2.20.1

Thanks,
Markus.
Intel Deutschland GmbH
Registered Address: Am Campeon 10-12, 85579 Neubiberg, Germany
Tel: +49 89 99 8853-0, www.intel.de
Managing Directors: Christin Eisenschmid, Gary Kershaw
Chairperson of the Supervisory Board: Nicole Lau
Registered Office: Munich
Commercial Register: Amtsgericht Muenchen HRB 186928
  

Patch

diff --git a/gdb/nat/linux-btrace.c b/gdb/nat/linux-btrace.c
index ef291ec2310..6eb5334e0ae 100644
--- a/gdb/nat/linux-btrace.c
+++ b/gdb/nat/linux-btrace.c
@@ -32,6 +32,10 @@ 
 
 #include <sys/syscall.h>
 
+#if defined (HAVE_LIBIPT)
+#include <intel-pt.h>
+#endif
+
 #if HAVE_LINUX_PERF_EVENT_H && defined(SYS_perf_event_open)
 #include <unistd.h>
 #include <sys/mman.h>
@@ -409,6 +413,31 @@  cpu_supports_bts (void)
     }
 }
 
+#if defined (PERF_ATTR_SIZE_VER5)
+/* Check whether the linux target supports Intel Processor Trace PTWRITE.  */
+
+static bool
+linux_supports_ptwrite ()
+{
+  static const char filename[]
+      = "/sys/bus/event_source/devices/intel_pt/caps/ptwrite";
+  gdb_file_up file = gdb_fopen_cloexec (filename, "r");
+
+  if (file.get () == nullptr)
+    return false;
+
+  int status, found = fscanf (file.get (), "%d", &status);
+
+  if (found != 1)
+    {
+      warning (_("Failed to determine ptwrite support from %s."), filename);
+      return false;
+    }
+
+  return status == 1;
+}
+#endif /* defined (PERF_ATTR_SIZE_VER5) */
+
 /* The perf_event_open syscall failed.  Try to print a helpful error
    message.  */
 
@@ -601,6 +630,9 @@  linux_enable_pt (ptid_t ptid, const struct btrace_config_pt *conf)
   pt->attr.exclude_hv = 1;
   pt->attr.exclude_idle = 1;
 
+  if (linux_supports_ptwrite ())
+    pt->attr.config |= 0x1000;
+
   errno = 0;
   scoped_fd fd (syscall (SYS_perf_event_open, &pt->attr, pid, -1, -1, 0));
   if (fd.get () < 0)