]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
btrace: kernel address filtering
authorMarkus Metzger <markus.t.metzger@intel.com>
Tue, 11 Aug 2015 09:05:58 +0000 (11:05 +0200)
committerMarkus Metzger <markus.t.metzger@intel.com>
Wed, 9 Sep 2015 08:35:35 +0000 (10:35 +0200)
For the BTS recording format, we sometimes get a FROM->TO record where the
FROM address lies in the kernel and the TO address lies in user space at
whatever address the user process was resumed.

GDB has a heuristic to filter out such records based on looking at the most
significant bit in the PC.  This works fine for 64-bit systems but it doesn't
always work for 32-bit systems.  Libraries that are loaded at fairly high
addresses might be mistaken for kernel code and branches inside the library
are filtered out.

Change the heuristic to (again heuristically) try to determine the lowest
address in kernel space.  Any PC that is smaller than that should be in
user space.

On today's systems, there should be a symbol "_text" at that address.
Read /proc/kallsyms and search for that symbol.

It is not guaranteed that /proc/kallsyms is readable on all systems.  On
64-bit systems, we fall back to check the most significant bit.  On 32-bit
systems, we refrain from filtering out addresses.

The filtering should really be done by the kernel.  And it soon will be:
https://lkml.org/lkml/2015/8/31/212.

gdb/
* nat/linux-btrace.h (struct btrace_target_info) <ptr_bits>: Remove.
* nat/linux-btrace.c: Include filestuff.h and inttypes.h.
Remove include of sys/utsname.h.
(linux_determine_kernel_ptr_bits): Remove.
(linux_determine_kernel_start): New.
(perf_event_is_kernel_addr): Remove tinfo argument.  Update users.
Update check.
(perf_event_skip_bts_record): Remove tinfo argument.  Update users.
(linux_enable_bts, linux_enable_pt): Remove tinfo->ptr_bits
initialization.
* x86-linux-nat.c (x86_linux_enable_btrace): Remove ptr_bits
assignment.

gdbserver/
* linux-low.c (linux_low_enable_btrace): Remove.
(linux_target_ops): Replace linux_low_enable_btrace with
linux_enable_btrace.

gdb/ChangeLog
gdb/gdbserver/ChangeLog
gdb/gdbserver/linux-low.c
gdb/nat/linux-btrace.c
gdb/nat/linux-btrace.h
gdb/x86-linux-nat.c

index 8fca48e177c6cef74331fb25e9e67edc164cbaf7..bed048a3b35ccaebdcbf69ac69c7f585b686865d 100644 (file)
@@ -1,3 +1,18 @@
+2015-09-09  Markus Metzger  <markus.t.metzger@intel.com>
+
+       * nat/linux-btrace.h (struct btrace_target_info) <ptr_bits>: Remove.
+       * nat/linux-btrace.c: Include filestuff.h and inttypes.h.
+       Remove include of sys/utsname.h.
+       (linux_determine_kernel_ptr_bits): Remove.
+       (linux_determine_kernel_start): New.
+       (perf_event_is_kernel_addr): Remove tinfo argument.  Update users.
+       Update check.
+       (perf_event_skip_bts_record): Remove tinfo argument.  Update users.
+       (linux_enable_bts, linux_enable_pt): Remove tinfo->ptr_bits
+       initialization.
+       * x86-linux-nat.c (x86_linux_enable_btrace): Remove ptr_bits
+       assignment.
+
 2015-09-07  Pedro Alves  <palves@redhat.com>
 
        * guile/guile-internal.h (as_a_scm_t_subr): New.
index 58a46b7e5cbe35c8cbc616cfe48f0adb946e0fe7..c6e9e344b0efe337ce55dc0238ad579d92cdfa05 100644 (file)
@@ -1,3 +1,9 @@
+2015-09-09  Markus Metzger  <markus.t.metzger@intel.com>
+
+       * linux-low.c (linux_low_enable_btrace): Remove.
+       (linux_target_ops): Replace linux_low_enable_btrace with
+       linux_enable_btrace.
+
 2015-09-03  Yao Qi  <yao.qi@linaro.org>
 
        * linux-aarch64-low.c (aarch64_insert_point): Call
index f4c602962a28ade227c49aeb26a4a30273d8eae1..4256bc56bce52225a069e4319d53e680d6a466b8 100644 (file)
@@ -6663,26 +6663,6 @@ linux_qxfer_libraries_svr4 (const char *annex, unsigned char *readbuf,
 
 #ifdef HAVE_LINUX_BTRACE
 
-/* See to_enable_btrace target method.  */
-
-static struct btrace_target_info *
-linux_low_enable_btrace (ptid_t ptid, const struct btrace_config *conf)
-{
-  struct btrace_target_info *tinfo;
-
-  tinfo = linux_enable_btrace (ptid, conf);
-
-  if (tinfo != NULL && tinfo->ptr_bits == 0)
-    {
-      struct thread_info *thread = find_thread_ptid (ptid);
-      struct regcache *regcache = get_thread_regcache (thread, 0);
-
-      tinfo->ptr_bits = register_size (regcache->tdesc, 0) * 8;
-    }
-
-  return tinfo;
-}
-
 /* See to_disable_btrace target method.  */
 
 static int
@@ -6936,7 +6916,7 @@ static struct target_ops linux_target_ops = {
   linux_supports_agent,
 #ifdef HAVE_LINUX_BTRACE
   linux_supports_btrace,
-  linux_low_enable_btrace,
+  linux_enable_btrace,
   linux_low_disable_btrace,
   linux_low_read_btrace,
   linux_low_btrace_conf,
index 51725ff4a2a1939748e3036c8d982ea44a674590..08478d88c732936e29943c70a1d046194663f2e0 100644 (file)
@@ -24,6 +24,9 @@
 #include "common-regcache.h"
 #include "gdb_wait.h"
 #include "x86-cpuid.h"
+#include "filestuff.h"
+
+#include <inttypes.h>
 
 #ifdef HAVE_SYS_SYSCALL_H
 #include <sys/syscall.h>
@@ -36,7 +39,6 @@
 #include "nat/gdb_ptrace.h"
 #include <sys/types.h>
 #include <signal.h>
-#include <sys/utsname.h>
 
 /* A branch trace record in perf_event.  */
 struct perf_event_bts
@@ -189,56 +191,75 @@ perf_event_pt_event_type (int *type)
   return -1;
 }
 
-static int
-linux_determine_kernel_ptr_bits (void)
+/* Try to determine the start address of the Linux kernel.  */
+
+static uint64_t
+linux_determine_kernel_start (void)
 {
-  struct utsname utsn;
-  int errcode;
+  static uint64_t kernel_start;
+  static int cached;
+  FILE *file;
 
-  memset (&utsn, 0, sizeof (utsn));
+  if (cached != 0)
+    return kernel_start;
 
-  errcode = uname (&utsn);
-  if (errcode < 0)
-    return 0;
+  cached = 1;
 
-  /* We only need to handle the 64-bit host case, here.  For 32-bit host,
-     the pointer size can be filled in later based on the inferior.  */
-  if (strcmp (utsn.machine, "x86_64") == 0)
-    return 64;
+  file = gdb_fopen_cloexec ("/proc/kallsyms", "r");
+  if (file == NULL)
+    return kernel_start;
 
-  return 0;
+  while (!feof (file))
+    {
+      char buffer[1024], symbol[8], *line;
+      uint64_t addr;
+      int match;
+
+      line = fgets (buffer, sizeof (buffer), file);
+      if (line == NULL)
+       break;
+
+      match = sscanf (line, "%" SCNx64 " %*[tT] %7s", &addr, symbol);
+      if (match != 2)
+       continue;
+
+      if (strcmp (symbol, "_text") == 0)
+       {
+         kernel_start = addr;
+         break;
+       }
+    }
+
+  fclose (file);
+
+  return kernel_start;
 }
 
 /* Check whether an address is in the kernel.  */
 
 static inline int
-perf_event_is_kernel_addr (const struct btrace_target_info *tinfo,
-                          uint64_t addr)
+perf_event_is_kernel_addr (uint64_t addr)
 {
-  uint64_t mask;
-
-  /* If we don't know the size of a pointer, we can't check.  Let's assume it's
-     not a kernel address in this case.  */
-  if (tinfo->ptr_bits == 0)
-    return 0;
+  uint64_t kernel_start;
 
-  /* A bit mask for the most significant bit in an address.  */
-  mask = (uint64_t) 1 << (tinfo->ptr_bits - 1);
+  kernel_start = linux_determine_kernel_start ();
+  if (kernel_start != 0ull)
+    return (addr >= kernel_start);
 
-  /* Check whether the most significant bit in the address is set.  */
-  return (addr & mask) != 0;
+  /* If we don't know the kernel's start address, let's check the most
+     significant bit.  This will work at least for 64-bit kernels.  */
+  return ((addr & (1ull << 63)) != 0);
 }
 
 /* Check whether a perf event record should be skipped.  */
 
 static inline int
-perf_event_skip_bts_record (const struct btrace_target_info *tinfo,
-                           const struct perf_event_bts *bts)
+perf_event_skip_bts_record (const struct perf_event_bts *bts)
 {
   /* The hardware may report branches from kernel into user space.  Branches
      from user into kernel space will be suppressed.  We filter the former to
      provide a consistent branch trace excluding kernel.  */
-  return perf_event_is_kernel_addr (tinfo, bts->from);
+  return perf_event_is_kernel_addr (bts->from);
 }
 
 /* Perform a few consistency checks on a perf event sample record.  This is
@@ -335,7 +356,7 @@ perf_event_read_bts (struct btrace_target_info* tinfo, const uint8_t *begin,
          break;
        }
 
-      if (perf_event_skip_bts_record (tinfo, &psample->bts))
+      if (perf_event_skip_bts_record (&psample->bts))
        continue;
 
       /* We found a valid sample, so we can complete the current block.  */
@@ -649,7 +670,6 @@ linux_enable_bts (ptid_t ptid, const struct btrace_config_bts *conf)
 
   tinfo = XCNEW (struct btrace_target_info);
   tinfo->ptid = ptid;
-  tinfo->ptr_bits = linux_determine_kernel_ptr_bits ();
 
   tinfo->conf.format = BTRACE_FORMAT_BTS;
   bts = &tinfo->variant.bts;
@@ -782,7 +802,6 @@ linux_enable_pt (ptid_t ptid, const struct btrace_config_pt *conf)
 
   tinfo = XCNEW (struct btrace_target_info);
   tinfo->ptid = ptid;
-  tinfo->ptr_bits = 0;
 
   tinfo->conf.format = BTRACE_FORMAT_PT;
   pt = &tinfo->variant.pt;
index 5ea87a8f5fd2cee5108760cc7db8d49cb81127cf..042878c34f354f621795e38b35f1e9b70a6173e2 100644 (file)
@@ -100,11 +100,6 @@ struct btrace_target_info
     struct btrace_tinfo_pt pt;
   } variant;
 #endif /* HAVE_LINUX_PERF_EVENT_H */
-
-  /* The size of a pointer in bits for this thread.
-     The information is used to identify kernel addresses in order to skip
-     records from/to kernel space.  */
-  int ptr_bits;
 };
 
 /* See to_supports_btrace in target.h.  */
index fe52c1f2ecc2c045151c8c7e78eba7f72473ac60..fa5ef300489b8e9bbb26c1dde74b8b587afc01a5 100644 (file)
@@ -264,12 +264,6 @@ x86_linux_enable_btrace (struct target_ops *self, ptid_t ptid,
     error (_("Could not enable branch tracing for %s: %s."),
           target_pid_to_str (ptid), safe_strerror (errno));
 
-  /* Fill in the size of a pointer in bits.  */
-  if (tinfo->ptr_bits == 0)
-    {
-      gdbarch = target_thread_architecture (ptid);
-      tinfo->ptr_bits = gdbarch_ptr_bit (gdbarch);
-    }
   return tinfo;
 }