Blame SOURCES/gdb-linux_perf-bundle.patch

0b3064
From FEDORA_PATCHES Mon Sep 17 00:00:00 2001
0b3064
From: Fedora GDB patches <invalid@email.com>
0b3064
Date: Fri, 27 Oct 2017 21:07:50 +0200
0b3064
Subject: gdb-linux_perf-bundle.patch
0b3064
0b3064
;; [dts+el7] [x86*] Bundle linux_perf.h for libipt (RH BZ 1256513).
0b3064
;;=fedora
0b3064
0b3064
diff --git a/gdb/configure b/gdb/configure
0b3064
--- a/gdb/configure
0b3064
+++ b/gdb/configure
0b3064
@@ -11905,7 +11905,7 @@ else
0b3064
 
0b3064
 #include <linux/perf_event.h>
0b3064
 #ifndef PERF_ATTR_SIZE_VER5
0b3064
-# error
0b3064
+// error // PERF_ATTR_SIZE_VER5_BUNDLE is not available here - Fedora+RHEL
0b3064
 #endif
0b3064
 
0b3064
 _ACEOF
0b3064
diff --git a/gdb/configure.ac b/gdb/configure.ac
0b3064
--- a/gdb/configure.ac
0b3064
+++ b/gdb/configure.ac
0b3064
@@ -1414,7 +1414,7 @@ else
0b3064
   AC_PREPROC_IFELSE([AC_LANG_SOURCE([[
0b3064
 #include <linux/perf_event.h>
0b3064
 #ifndef PERF_ATTR_SIZE_VER5
0b3064
-# error
0b3064
+// error // PERF_ATTR_SIZE_VER5_BUNDLE is not available here - Fedora+RHEL
0b3064
 #endif
0b3064
   ]])], [perf_event=yes], [perf_event=no])
0b3064
   if test "$perf_event" != yes; then
0b3064
diff --git a/gdb/gdb.c b/gdb/gdb.c
0b3064
--- a/gdb/gdb.c
0b3064
+++ b/gdb/gdb.c
0b3064
@@ -20,11 +20,19 @@
0b3064
 #include "main.h"
0b3064
 #include "interps.h"
0b3064
 
0b3064
+#ifdef PERF_ATTR_SIZE_VER5_BUNDLE
0b3064
+extern "C" void __libipt_init(void);
0b3064
+#endif
0b3064
+
0b3064
 int
0b3064
 main (int argc, char **argv)
0b3064
 {
0b3064
   struct captured_main_args args;
0b3064
 
0b3064
+#ifdef PERF_ATTR_SIZE_VER5_BUNDLE
0b3064
+  __libipt_init();
0b3064
+#endif
0b3064
+
0b3064
   memset (&args, 0, sizeof args);
0b3064
   args.argc = argc;
0b3064
   args.argv = argv;
0b3064
diff --git a/gdb/nat/linux-btrace.h b/gdb/nat/linux-btrace.h
0b3064
--- a/gdb/nat/linux-btrace.h
0b3064
+++ b/gdb/nat/linux-btrace.h
0b3064
@@ -27,6 +27,177 @@
0b3064
 #  include <linux/perf_event.h>
0b3064
 #endif
0b3064
 
0b3064
+#ifdef PERF_ATTR_SIZE_VER5_BUNDLE
0b3064
+#ifndef HAVE_LINUX_PERF_EVENT_H
0b3064
+# error "PERF_ATTR_SIZE_VER5_BUNDLE && !HAVE_LINUX_PERF_EVENT_H"
0b3064
+#endif
0b3064
+#ifndef PERF_ATTR_SIZE_VER5
0b3064
+#define PERF_ATTR_SIZE_VER5
0b3064
+#define perf_event_mmap_page perf_event_mmap_page_bundle
0b3064
+// kernel-headers-3.10.0-493.el7.x86_64/usr/include/linux/perf_event.h
0b3064
+/*
0b3064
+ * Structure of the page that can be mapped via mmap
0b3064
+ */
0b3064
+struct perf_event_mmap_page {
0b3064
+	__u32	version;		/* version number of this structure */
0b3064
+	__u32	compat_version;		/* lowest version this is compat with */
0b3064
+
0b3064
+	/*
0b3064
+	 * Bits needed to read the hw events in user-space.
0b3064
+	 *
0b3064
+	 *   u32 seq, time_mult, time_shift, index, width;
0b3064
+	 *   u64 count, enabled, running;
0b3064
+	 *   u64 cyc, time_offset;
0b3064
+	 *   s64 pmc = 0;
0b3064
+	 *
0b3064
+	 *   do {
0b3064
+	 *     seq = pc->lock;
0b3064
+	 *     barrier()
0b3064
+	 *
0b3064
+	 *     enabled = pc->time_enabled;
0b3064
+	 *     running = pc->time_running;
0b3064
+	 *
0b3064
+	 *     if (pc->cap_usr_time && enabled != running) {
0b3064
+	 *       cyc = rdtsc();
0b3064
+	 *       time_offset = pc->time_offset;
0b3064
+	 *       time_mult   = pc->time_mult;
0b3064
+	 *       time_shift  = pc->time_shift;
0b3064
+	 *     }
0b3064
+	 *
0b3064
+	 *     index = pc->index;
0b3064
+	 *     count = pc->offset;
0b3064
+	 *     if (pc->cap_user_rdpmc && index) {
0b3064
+	 *       width = pc->pmc_width;
0b3064
+	 *       pmc = rdpmc(index - 1);
0b3064
+	 *     }
0b3064
+	 *
0b3064
+	 *     barrier();
0b3064
+	 *   } while (pc->lock != seq);
0b3064
+	 *
0b3064
+	 * NOTE: for obvious reason this only works on self-monitoring
0b3064
+	 *       processes.
0b3064
+	 */
0b3064
+	__u32	lock;			/* seqlock for synchronization */
0b3064
+	__u32	index;			/* hardware event identifier */
0b3064
+	__s64	offset;			/* add to hardware event value */
0b3064
+	__u64	time_enabled;		/* time event active */
0b3064
+	__u64	time_running;		/* time event on cpu */
0b3064
+	union {
0b3064
+		__u64	capabilities;
0b3064
+		struct {
0b3064
+			__u64	cap_bit0		: 1, /* Always 0, deprecated, see commit 860f085b74e9 */
0b3064
+				cap_bit0_is_deprecated	: 1, /* Always 1, signals that bit 0 is zero */
0b3064
+
0b3064
+				cap_user_rdpmc		: 1, /* The RDPMC instruction can be used to read counts */
0b3064
+				cap_user_time		: 1, /* The time_* fields are used */
0b3064
+				cap_user_time_zero	: 1, /* The time_zero field is used */
0b3064
+				cap_____res		: 59;
0b3064
+		};
0b3064
+	};
0b3064
+
0b3064
+	/*
0b3064
+	 * If cap_user_rdpmc this field provides the bit-width of the value
0b3064
+	 * read using the rdpmc() or equivalent instruction. This can be used
0b3064
+	 * to sign extend the result like:
0b3064
+	 *
0b3064
+	 *   pmc <<= 64 - width;
0b3064
+	 *   pmc >>= 64 - width; // signed shift right
0b3064
+	 *   count += pmc;
0b3064
+	 */
0b3064
+	__u16	pmc_width;
0b3064
+
0b3064
+	/*
0b3064
+	 * If cap_usr_time the below fields can be used to compute the time
0b3064
+	 * delta since time_enabled (in ns) using rdtsc or similar.
0b3064
+	 *
0b3064
+	 *   u64 quot, rem;
0b3064
+	 *   u64 delta;
0b3064
+	 *
0b3064
+	 *   quot = (cyc >> time_shift);
0b3064
+	 *   rem = cyc & (((u64)1 << time_shift) - 1);
0b3064
+	 *   delta = time_offset + quot * time_mult +
0b3064
+	 *              ((rem * time_mult) >> time_shift);
0b3064
+	 *
0b3064
+	 * Where time_offset,time_mult,time_shift and cyc are read in the
0b3064
+	 * seqcount loop described above. This delta can then be added to
0b3064
+	 * enabled and possible running (if index), improving the scaling:
0b3064
+	 *
0b3064
+	 *   enabled += delta;
0b3064
+	 *   if (index)
0b3064
+	 *     running += delta;
0b3064
+	 *
0b3064
+	 *   quot = count / running;
0b3064
+	 *   rem  = count % running;
0b3064
+	 *   count = quot * enabled + (rem * enabled) / running;
0b3064
+	 */
0b3064
+	__u16	time_shift;
0b3064
+	__u32	time_mult;
0b3064
+	__u64	time_offset;
0b3064
+	/*
0b3064
+	 * If cap_usr_time_zero, the hardware clock (e.g. TSC) can be calculated
0b3064
+	 * from sample timestamps.
0b3064
+	 *
0b3064
+	 *   time = timestamp - time_zero;
0b3064
+	 *   quot = time / time_mult;
0b3064
+	 *   rem  = time % time_mult;
0b3064
+	 *   cyc = (quot << time_shift) + (rem << time_shift) / time_mult;
0b3064
+	 *
0b3064
+	 * And vice versa:
0b3064
+	 *
0b3064
+	 *   quot = cyc >> time_shift;
0b3064
+	 *   rem  = cyc & (((u64)1 << time_shift) - 1);
0b3064
+	 *   timestamp = time_zero + quot * time_mult +
0b3064
+	 *               ((rem * time_mult) >> time_shift);
0b3064
+	 */
0b3064
+	__u64	time_zero;
0b3064
+	__u32	size;			/* Header size up to __reserved[] fields. */
0b3064
+
0b3064
+		/*
0b3064
+		 * Hole for extension of the self monitor capabilities
0b3064
+		 */
0b3064
+
0b3064
+	__u8	__reserved[118*8+4];	/* align to 1k. */
0b3064
+
0b3064
+	/*
0b3064
+	 * Control data for the mmap() data buffer.
0b3064
+	 *
0b3064
+	 * User-space reading the @data_head value should issue an smp_rmb(),
0b3064
+	 * after reading this value.
0b3064
+	 *
0b3064
+	 * When the mapping is PROT_WRITE the @data_tail value should be
0b3064
+	 * written by userspace to reflect the last read data, after issueing
0b3064
+	 * an smp_mb() to separate the data read from the ->data_tail store.
0b3064
+	 * In this case the kernel will not over-write unread data.
0b3064
+	 *
0b3064
+	 * See perf_output_put_handle() for the data ordering.
0b3064
+	 *
0b3064
+	 * data_{offset,size} indicate the location and size of the perf record
0b3064
+	 * buffer within the mmapped area.
0b3064
+	 */
0b3064
+	__u64   data_head;		/* head in the data section */
0b3064
+	__u64	data_tail;		/* user-space written tail */
0b3064
+	__u64	data_offset;		/* where the buffer starts */
0b3064
+	__u64	data_size;		/* data buffer size */
0b3064
+
0b3064
+	/*
0b3064
+	 * AUX area is defined by aux_{offset,size} fields that should be set
0b3064
+	 * by the userspace, so that
0b3064
+	 *
0b3064
+	 *   aux_offset >= data_offset + data_size
0b3064
+	 *
0b3064
+	 * prior to mmap()ing it. Size of the mmap()ed area should be aux_size.
0b3064
+	 *
0b3064
+	 * Ring buffer pointers aux_{head,tail} have the same semantics as
0b3064
+	 * data_{head,tail} and same ordering rules apply.
0b3064
+	 */
0b3064
+	__u64	aux_head;
0b3064
+	__u64	aux_tail;
0b3064
+	__u64	aux_offset;
0b3064
+	__u64	aux_size;
0b3064
+};
0b3064
+#endif // PERF_ATTR_SIZE_VER5
0b3064
+#endif // PERF_ATTR_SIZE_VER5_BUNDLE
0b3064
+
0b3064
 struct target_ops;
0b3064
 
0b3064
 #if HAVE_LINUX_PERF_EVENT_H