[PATCH v2 08/12] btrace, linux: Enable ptwrite packets.

Felix Willgerodt via Gdb-patches
2021-06-14T15:06:25Z

Enable ptwrite in the PT config, if it is supported by the kernel.

gdb/ChangeLog:
2021-06-14 Felix Willgerodt <felix.willgerodt@intel.com>

* nat/linux-btrace.c (linux_supports_ptw): New function.
(linux_enable_pt): Set attr.config if ptwrite is supported.
* record-btrace.c (_initialize_record_btrace): Initialize ptwrite
in record_btrace_conf.pt.
---
gdb/nat/linux-btrace.c | 29 +++++++++++++++++++++++++++++
gdb/record-btrace.c | 5 +++++
2 files changed, 34 insertions(+)

diff --git a/gdb/nat/linux-btrace.c b/gdb/nat/linux-btrace.c
index 324f7ef0407..5a48f397908 100644
--- a/gdb/nat/linux-btrace.c
+++ b/gdb/nat/linux-btrace.c
@@ -415,6 +415,29 @@ cpu_supports_bts (void)
}
}

+/* 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;
+}
+
/* The perf_event_open syscall failed. Try to print a helpful error
message. */

@@ -607,6 +630,12 @@ linux_enable_pt (ptid_t ptid, const struct btrace_config_pt *conf)
pt->attr.exclude_hv = 1;
pt->attr.exclude_idle = 1;

+ if (conf->ptwrite && linux_supports_ptwrite ())
+ {
+ pt->attr.config |= 0x1000;
+ tinfo->conf.pt.ptwrite = conf->ptwrite;
+ }
+
errno = 0;
scoped_fd fd (syscall (SYS_perf_event_open, &pt->attr, pid, -1, -1, 0));
if (fd.get () < 0)
diff --git a/gdb/record-btrace.c b/gdb/record-btrace.c
index 3d0c344e8e6..99d962455ea 100644
--- a/gdb/record-btrace.c
+++ b/gdb/record-btrace.c
@@ -3312,4 +3312,9 @@ to see the actual buffer size."), NULL, show_record_pt_buffer_size_value,

record_btrace_conf.bts.size = 64 * 1024;
record_btrace_conf.pt.size = 16 * 1024;
+#if (LIBIPT_VERSION >= 0x200)
+ record_btrace_conf.pt.ptwrite = true;
+#else
+ record_btrace_conf.pt.ptwrite = false;
+#endif