Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Markus Metzger <markus.t.metzger@intel.com>
To: jan.kratochvil@redhat.com
Cc: gdb-patches@sourceware.org, markus.t.metzger@gmail.com
Subject: [patch v9 09/23] btrace, x86: disable on some processors
Date: Mon, 04 Mar 2013 17:08:00 -0000	[thread overview]
Message-ID: <1362416770-19750-10-git-send-email-markus.t.metzger@intel.com> (raw)
In-Reply-To: <1362416770-19750-1-git-send-email-markus.t.metzger@intel.com>

LBR, BTM, or BTS records may have incorrect branch "from" information afer an
EIST transition, T-states, C1E, or Adaptive Thermal Throttling (AAJ122).

This results in sporadic test fails. Disable btrace on those processors.

I added a kernel check before checking cpuid requested by Mark Kettenis.
I added the cpuid vendor check requested by HJ.

Approved by Jan Kratochvil.
Approved by Mark Kettenis.

2013-03-04  Markus Metzger  <markus.t.metzger@intel.com>

	* common/linux-btrace.c: Include sys/ptrace, sys/types, sys/wait.h,
	and signal.h.
	(linux_supports_btrace): Add kernel and
	cpuid check.
	(kernel_supports_btrace): New function.
	(cpu_supports_btrace): New function.
	(intel_supports_btrace): New function.


---
 gdb/common/linux-btrace.c |  187 ++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 186 insertions(+), 1 deletions(-)

diff --git a/gdb/common/linux-btrace.c b/gdb/common/linux-btrace.c
index 44eb6dc..238d7ba 100644
--- a/gdb/common/linux-btrace.c
+++ b/gdb/common/linux-btrace.c
@@ -40,6 +40,10 @@
 #include <sys/syscall.h>
 #include <sys/mman.h>
 #include <sys/user.h>
+#include <sys/ptrace.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <signal.h>
 
 #if defined(__GNUC__)
 #  define memory_barrier() asm volatile ("" : : : "memory")
@@ -253,12 +257,193 @@ perf_event_read_bts (struct btrace_target_info* tinfo, const uint8_t *begin,
   return btrace;
 }
 
+/* Check whether the kernel supports branch tracing.  */
+
+static int
+kernel_supports_btrace (void)
+{
+  struct perf_event_attr attr;
+  pid_t child, pid;
+  int status, file;
+
+  errno = 0;
+  child = fork ();
+  switch (child)
+    {
+    case -1:
+      warning (_("test branch tracing: cannot fork: %s."), strerror (errno));
+      return 0;
+
+    case 0:
+      status = ptrace (PTRACE_TRACEME, 0, NULL, NULL);
+      if (status != 0)
+	{
+	  warning (_("test branch tracing: cannot PTRACE_TRACEME: %s."),
+		   strerror (errno));
+	  _exit (1);
+	}
+
+      status = raise (SIGTRAP);
+      if (status != 0)
+	{
+	  warning (_("test branch tracing: cannot raise SIGTRAP: %s."),
+		   strerror (errno));
+	  _exit (1);
+	}
+
+      _exit (1);
+
+    default:
+      pid = waitpid (child, &status, 0);
+      if (pid != child)
+	{
+	  warning (_("test branch tracing: bad pid %ld, error: %s."),
+		   (long) pid, strerror (errno));
+	  return 0;
+	}
+
+      if (!WIFSTOPPED (status))
+	{
+	  warning (_("test branch tracing: expected stop. status: %d."),
+		   status);
+	  return 0;
+	}
+
+      memset (&attr, 0, sizeof (attr));
+
+      attr.type = PERF_TYPE_HARDWARE;
+      attr.config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS;
+      attr.sample_period = 1;
+      attr.sample_type = PERF_SAMPLE_IP | PERF_SAMPLE_ADDR;
+      attr.exclude_kernel = 1;
+      attr.exclude_hv = 1;
+      attr.exclude_idle = 1;
+
+      file = syscall (SYS_perf_event_open, &attr, child, -1, -1, 0);
+      if (file >= 0)
+	close (file);
+
+      kill (child, SIGKILL);
+      ptrace (PTRACE_KILL, child, NULL, NULL);
+
+      pid = waitpid (child, &status, 0);
+      if (pid != child)
+	{
+	  warning (_("test branch tracing: bad pid %ld, error: %s."),
+		   (long) pid, strerror (errno));
+	  if (!WIFSIGNALED (status))
+	    warning (_("test branch tracing: expected killed. status: %d."),
+		     status);
+	}
+
+      return (file >= 0);
+    }
+}
+
+/* Check whether an Intel cpu supports branch tracing.  */
+
+static int
+intel_supports_btrace (void)
+{
+#if defined __i386__ || defined __x86_64__
+    unsigned int cpuid, model, family;
+
+    __asm__ __volatile__ ("movl   $1, %%eax;"
+			  "cpuid;"
+			  : "=a" (cpuid)
+			  :: "%ebx", "%ecx", "%edx");
+
+    family = (cpuid >> 8) & 0xf;
+    model = (cpuid >> 4) & 0xf;
+
+    switch (family)
+      {
+      case 0x6:
+	model += (cpuid >> 12) & 0xf0;
+
+	switch (model)
+	  {
+	  case 0x1a: /* Nehalem */
+	  case 0x1f:
+	  case 0x1e:
+	  case 0x2e:
+	  case 0x25: /* Westmere */
+	  case 0x2c:
+	  case 0x2f:
+	  case 0x2a: /* Sandy Bridge */
+	  case 0x2d:
+	  case 0x3a: /* Ivy Bridge */
+
+	    /* AAJ122: LBR, BTM, or BTS records may have incorrect branch
+	       "from" information afer an EIST transition, T-states, C1E, or
+	       Adaptive Thermal Throttling.  */
+	    return 0;
+	  }
+      }
+
+  return 1;
+
+#else /* !defined __i386__ && !defined __x86_64__ */
+
+  return 0;
+
+#endif /* !defined __i386__ && !defined __x86_64__ */
+}
+
+/* Check whether the cpu supports branch tracing.  */
+
+static int
+cpu_supports_btrace (void)
+{
+#if defined __i386__ || defined __x86_64__
+  char vendor[13];
+
+  __asm__ __volatile__ ("xorl   %%ebx, %%ebx;"
+			"xorl   %%ecx, %%ecx;"
+			"xorl   %%edx, %%edx;"
+			"movl   $0,    %%eax;"
+			"cpuid;"
+			"movl   %%ebx,  %0;"
+			"movl   %%edx,  %1;"
+			"movl   %%ecx,  %2;"
+			: "=m" (vendor[0]),
+			  "=m" (vendor[4]),
+			  "=m" (vendor[8])
+			:
+			: "%eax", "%ebx", "%ecx", "%edx");
+  vendor[12] = '\0';
+
+  if (strcmp (vendor, "GenuineIntel") == 0)
+    return intel_supports_btrace ();
+
+  /* Don't know about others.  Let's assume they do.  */
+  return 1;
+
+#else /* !defined __i386__ && !defined __x86_64__ */
+
+  return 0;
+
+#endif /* !defined __i386__ && !defined __x86_64__ */
+}
+
 /* See linux-btrace.h.  */
 
 int
 linux_supports_btrace (void)
 {
-  return 1;
+  static int cached;
+
+  if (cached == 0)
+    {
+      if (!kernel_supports_btrace ())
+	cached = -1;
+      else if (!cpu_supports_btrace ())
+	cached = -1;
+      else
+	cached = 1;
+    }
+
+  return cached > 0;
 }
 
 /* See linux-btrace.h.  */
-- 
1.7.1


  parent reply	other threads:[~2013-03-04 17:08 UTC|newest]

Thread overview: 102+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-03-04 17:07 [patch v9 00/23] branch tracing support for Atom Markus Metzger
2013-03-04 17:07 ` [patch v9 16/23] record: default target methods Markus Metzger
2013-03-05 20:10   ` Jan Kratochvil
2013-03-04 17:07 ` [patch v9 01/23] thread, btrace: add generic branch trace support Markus Metzger
2013-03-05 20:06   ` Jan Kratochvil
2013-03-05 22:02     ` Tom Tromey
2013-03-06 21:11   ` Doug Evans
2013-03-07  7:50     ` Metzger, Markus T
2013-03-07 22:57       ` Doug Evans
2013-03-04 17:07 ` [patch v9 08/23] gdbserver, linux, btrace: add btrace support for linux-low Markus Metzger
2013-03-05 20:09   ` Jan Kratochvil
2013-03-06  9:17     ` Metzger, Markus T
2013-03-06 13:33   ` Jan Kratochvil
2013-03-04 17:07 ` [patch v9 03/23] linux, i386, amd64: enable btrace for 32bit and 64bit linux native Markus Metzger
2013-03-05 20:06   ` Jan Kratochvil
2013-03-04 17:07 ` [patch v9 05/23] remote, btrace: add branch trace remote ops Markus Metzger
2013-03-05 20:07   ` Jan Kratochvil
2013-03-06  9:00     ` Metzger, Markus T
2013-03-04 17:07 ` [patch v9 04/23] xml, btrace: define btrace xml document style Markus Metzger
2013-03-05 20:07   ` Jan Kratochvil
2013-03-04 17:07 ` [patch v9 02/23] linux, btrace: perf_event based branch tracing Markus Metzger
2013-03-05 20:06   ` Jan Kratochvil
2013-03-06 10:11   ` Mark Kettenis
2013-03-06 10:29     ` Metzger, Markus T
2013-03-04 17:07 ` [patch v9 10/23] remote, btrace: add branch tracing protocol to Qbtrace packet Markus Metzger
2013-03-05 20:09   ` Jan Kratochvil
2013-03-06  9:19     ` Metzger, Markus T
2013-03-04 17:07 ` [patch v9 07/23] gdbserver, btrace: add generic btrace support Markus Metzger
2013-03-05 20:08   ` Jan Kratochvil
2013-03-06  9:15     ` Metzger, Markus T
2013-03-06 13:22   ` Jan Kratochvil
2013-03-04 17:08 ` [patch v9 20/23] record-btrace, disas: omit pc prefix Markus Metzger
2013-03-05 20:13   ` Jan Kratochvil
2013-03-04 17:08 ` Markus Metzger [this message]
2013-03-05 20:09   ` [patch v9 09/23] btrace, x86: disable on some processors Jan Kratochvil
2013-03-04 17:08 ` [patch v9 19/23] record, btrace: add record-btrace target Markus Metzger
2013-03-05 20:13   ` Jan Kratochvil
2013-03-06  9:57     ` Metzger, Markus T
2013-03-06 13:35       ` Jan Kratochvil
2013-03-06 14:01         ` Metzger, Markus T
2013-03-06 16:28   ` Jan Kratochvil
2013-03-04 17:08 ` [patch v9 11/23] target: add add_deprecated_target_alias Markus Metzger
2013-03-05 20:09   ` Jan Kratochvil
2013-03-04 17:08 ` [patch v9 06/23] btrace, doc: document remote serial protocol Markus Metzger
2013-03-04 18:08   ` Eli Zaretskii
2013-03-05 20:08   ` Jan Kratochvil
2013-03-06  9:06     ` Metzger, Markus T
2013-03-06  9:50       ` Jan Kratochvil
2013-03-06 10:01         ` Metzger, Markus T
2013-03-06 12:07           ` Jan Kratochvil
2013-03-06 12:13             ` Metzger, Markus T
2013-03-06 12:17               ` Jan Kratochvil
2013-03-04 17:08 ` [patch v9 22/23] testsuite, gdb.btrace: add btrace tests Markus Metzger
2013-03-04 19:47   ` Jan Kratochvil
2013-03-05  6:39     ` Metzger, Markus T
2013-03-05 20:14   ` Jan Kratochvil
2013-03-06 15:32     ` christian.himpel
2013-03-06 16:35       ` Jan Kratochvil
2013-03-04 17:09 ` [patch v9 23/23] btrace, remote: drop qbtrace packet Markus Metzger
2013-03-04 18:15   ` Eli Zaretskii
2013-03-05 20:15   ` Jan Kratochvil
2013-03-04 17:09 ` [patch v9 18/23] record: add "record function-call-history" command Markus Metzger
2013-03-04 18:07   ` Eli Zaretskii
2013-03-05 20:12   ` Jan Kratochvil
2013-03-04 17:09 ` [patch v9 21/23] doc, record: document record changes Markus Metzger
2013-03-04 18:13   ` Eli Zaretskii
2013-03-05 20:13   ` Jan Kratochvil
2013-03-04 17:09 ` [patch v9 15/23] record-full.h: rename record_ into record_full_ Markus Metzger
2013-03-05 20:10   ` Jan Kratochvil
2013-03-04 17:09 ` [patch v9 13/23] record: make it build again Markus Metzger
2013-03-05 20:10   ` Jan Kratochvil
2013-03-04 17:09 ` [patch v9 14/23] record-full.c: rename record_ in record_full_ Markus Metzger
2013-03-05 20:10   ` Jan Kratochvil
2013-03-04 17:09 ` [patch v9 17/23] record: add "record instruction-history" command Markus Metzger
2013-03-04 18:14   ` Eli Zaretskii
2013-03-05 20:11   ` Jan Kratochvil
2013-03-04 17:10 ` [patch v9 12/23] record: split record Markus Metzger
2013-03-05 20:09   ` Jan Kratochvil
2013-03-06 12:43 ` Crash of GDB with gdbserver btrace enabled [Re: [patch v9 00/23] branch tracing support for Atom] Jan Kratochvil
2013-03-06 14:40   ` Metzger, Markus T
2013-03-06 15:31     ` Metzger, Markus T
2013-03-06 17:06       ` Jan Kratochvil
2013-03-06 18:08         ` Metzger, Markus T
2013-03-07  9:06           ` Jan Kratochvil
2013-03-07  9:41             ` Metzger, Markus T
2013-03-07 10:00               ` Metzger, Markus T
2013-03-07 10:14                 ` Jan Kratochvil
2013-03-07 10:33                   ` Metzger, Markus T
2013-03-07 12:07                     ` Jan Kratochvil
2013-03-07 12:33                       ` Metzger, Markus T
2013-03-07 14:45                         ` Jan Kratochvil
2013-03-07 15:22                           ` Metzger, Markus T
2013-03-07 15:46                             ` Jan Kratochvil
2013-03-07 15:12                         ` Pedro Alves
2013-03-07 15:33                           ` Metzger, Markus T
2013-03-07 15:39                             ` Jan Kratochvil
2013-03-07 15:41                             ` Pedro Alves
     [not found]                           ` <20130318170643.GA15625@host2.jankratochvil.net>
     [not found]                             ` <514758DA.2060905@redhat.com>
2013-03-20 15:52                               ` [commit] [patch] Code cleanup: Remove parameter quitting [Re: Crash of GDB with gdbserver btrace enabled] Jan Kratochvil
     [not found]                               ` <20130318192604.GA2786@host2.jankratochvil.net>
     [not found]                                 ` <51477828.30000@redhat.com>
2013-03-26  0:15                                   ` Jan Kratochvil
2013-04-03 18:05                                     ` Pedro Alves
2013-03-07 10:08               ` Crash of GDB with gdbserver btrace enabled [Re: [patch v9 00/23] branch tracing support for Atom] Jan Kratochvil
2013-03-06 15:41   ` Jan Kratochvil

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=1362416770-19750-10-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=markus.t.metzger@gmail.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