Skip to contentSkip to conversation

perf intel-pt: Add support for emulated ptwrite

PATCH In review Adrian Hunter 2 participants 6 messages May 9, 2022 – May 17, 2022 perf events

lore
Share & tools
lore

Adrian Hunter

author reviewer
#0 lore raw

cover perf intel-pt: Add support for emulated ptwrite

Hi

Here are 3 patches (on top of "perf intel-pt: Add link to the perf wiki's Intel PT page") to add support for decoding emulated ptwrite i.e. for hardware that does not have the ptwrite instruction.

Adrian Hunter (3):
perf intel-pt: Add support for emulated ptwrite
perf script: Print Intel ptwrite value as a string if it is ASCII
perf scripts python: intel-pt-events.py: Print ptwrite value as a string if it is ASCII

 tools/perf/Documentation/perf-intel-pt.txt         | 88 +++++++++++++++++++
 tools/perf/builtin-script.c                        | 32 ++++++-
 tools/perf/scripts/python/intel-pt-events.py       |  8 +-
 .../perf/util/intel-pt-decoder/intel-pt-decoder.c  | 99 +++++++++++++++++++++-
 .../perf/util/intel-pt-decoder/intel-pt-decoder.h  |  1 +
 .../util/intel-pt-decoder/intel-pt-insn-decoder.c  |  1 +
 .../util/intel-pt-decoder/intel-pt-insn-decoder.h  |  1 +
 tools/perf/util/intel-pt.c                         | 37 +++++++-
 8 files changed, 261 insertions(+), 6 deletions(-)

Regards
Adrian

author reviewer
#1 lore raw

PATCH 1/3 perf intel-pt: Add support for emulated ptwrite

ptwrite is an Intel x86 instruction that writes arbitrary values into an Intel PT trace. It is not supported on all hardware, so provide an alternative that makes use of TNT packets to convey the payload data. TNT packets encode Taken/Not-taken conditional branch information, so taking branches based on the payload value will encode the value into the TNT packet. Refer to the changes to the documentation file perf-intel-pt.txt in this patch for an example.

---
 tools/perf/Documentation/perf-intel-pt.txt    | 88 +++++++++++++++++
 .../util/intel-pt-decoder/intel-pt-decoder.c  | 99 ++++++++++++++++++-
 .../util/intel-pt-decoder/intel-pt-decoder.h  |  1 +
 .../intel-pt-decoder/intel-pt-insn-decoder.c  |  1 +
 .../intel-pt-decoder/intel-pt-insn-decoder.h  |  1 +
 tools/perf/util/intel-pt.c                    | 37 ++++++-
 6 files changed, 224 insertions(+), 3 deletions(-)
tools/perf/Documentation/perf-intel-pt.txt +88 -0
index 92532d0d3618..8acd704e8a39 100644
@@ -468,6 +468,8 @@ ptw		Enable PTWRITE packets which are produced when a ptwrite instruction
 		which contains "1" if the feature is supported and
 		"0" otherwise.
 
+		As an alternative, refer to "Emulated PTWRITE" further below.
+
 fup_on_ptw	Enable a FUP packet to follow the PTWRITE packet.  The FUP packet
 		provides the address of the ptwrite instruction.  In the absence of
 		fup_on_ptw, the decoder will use the address of the previous branch
@@ -1471,6 +1473,92 @@ In that case the --itrace q option is forced because walking executable code
 to reconstruct the control flow is not possible.
 
 
+Emulated PTWRITE
+----------------
+
+Later perf tools support a method to emulate the ptwrite instruction, which
+can be useful if hardware does not support the ptwrite instruction.
+
+Instead of using the ptwrite instruction, a function is used which produces
+a trace that encodes the payload data into TNT packets.  Here is an example
+of the function:
+
+ #include <stdint.h>
+
+ void perf_emulate_ptwrite(uint64_t x)
+ __attribute__((externally_visible, noipa, no_instrument_function, naked));
+
+ #define PERF_EMULATE_PTWRITE_8_BITS \
+                 "1: shl %rax\n"     \
+                 "   jc 1f\n"        \
+                 "1: shl %rax\n"     \
+                 "   jc 1f\n"        \
+                 "1: shl %rax\n"     \
+                 "   jc 1f\n"        \
+                 "1: shl %rax\n"     \
+                 "   jc 1f\n"        \
+                 "1: shl %rax\n"     \
+                 "   jc 1f\n"        \
+                 "1: shl %rax\n"     \
+                 "   jc 1f\n"        \
+                 "1: shl %rax\n"     \
+                 "   jc 1f\n"        \
+                 "1: shl %rax\n"     \
+                 "   jc 1f\n"
+
+ /* Undefined instruction */
+ #define PERF_EMULATE_PTWRITE_UD2        ".byte 0x0f, 0x0b\n"
+
+ #define PERF_EMULATE_PTWRITE_MAGIC        PERF_EMULATE_PTWRITE_UD2 ".ascii \"perf,ptwrite  \"\n"
+
+ void perf_emulate_ptwrite(uint64_t x __attribute__ ((__unused__)))
+ {
+          /* Assumes SysV ABI : x passed in rdi */
+         __asm__ volatile (
+                 "jmp 1f\n"
+                 PERF_EMULATE_PTWRITE_MAGIC
+                 "1: mov %rdi, %rax\n"
+                 PERF_EMULATE_PTWRITE_8_BITS
+                 PERF_EMULATE_PTWRITE_8_BITS
+                 PERF_EMULATE_PTWRITE_8_BITS
+                 PERF_EMULATE_PTWRITE_8_BITS
+                 PERF_EMULATE_PTWRITE_8_BITS
+                 PERF_EMULATE_PTWRITE_8_BITS
+                 PERF_EMULATE_PTWRITE_8_BITS
+                 PERF_EMULATE_PTWRITE_8_BITS
+                 "1: ret\n"
+         );
+ }
+
+For example, a test program with the function above:
+
+ #include <stdio.h>
+ #include <stdint.h>
+ #include <stdlib.h>
+
+ #include "perf_emulate_ptwrite.h"
+
+ int main(int argc, char *argv[])
+ {
+         uint64_t x = 0;
+
+         if (argc > 1)
+                 x = strtoull(argv[1], NULL, 0);
+         perf_emulate_ptwrite(x);
+         return 0;
+ }
+
+Can be compiled and traced:
+
+ $ gcc -Wall -Wextra -O3 -g -o eg_ptw eg_ptw.c
+ $ perf record -e intel_pt//u ./eg_ptw 0x1234567890abcdef
+ [ perf record: Woken up 1 times to write data ]
+ [ perf record: Captured and wrote 0.017 MB perf.data ]
+ $ perf script --itrace=ew
+           eg_ptw 19875 [007]  8061.235912:     ptwrite:  IP: 0 payload: 0x1234567890abcdef      55701249a196 perf_emulate_ptwrite+0x16 (/home/user/eg_ptw)
+ $
+
+
 EXAMPLE
 -------
 
tools/perf/util/intel-pt-decoder/intel-pt-decoder.c +97 -2

132 lines not on this page · raw message

tools/perf/util/intel-pt-decoder/intel-pt-decoder.h +1 -0
index efb2cb3ae0ca..c773028df80e 100644
@@ -58,6 +58,7 @@ enum {
 	INTEL_PT_ERR_LOST,
 	INTEL_PT_ERR_UNK,
 	INTEL_PT_ERR_NELOOP,
+	INTEL_PT_ERR_EPTW,
 	INTEL_PT_ERR_MAX,
 };
 
tools/perf/util/intel-pt-decoder/intel-pt-insn-decoder.c +1 -0
index 9d5e65cec89b..1376077183f7 100644
@@ -32,6 +32,7 @@ static void intel_pt_insn_decoder(struct insn *insn,
 	int ext;
 
 	intel_pt_insn->rel = 0;
+	intel_pt_insn->emulated_ptwrite = false;
 
 	if (insn_is_avx(insn)) {
 		intel_pt_insn->op = INTEL_PT_OP_OTHER;
tools/perf/util/intel-pt-decoder/intel-pt-insn-decoder.h +1 -0
index c2861cfdd768..e3338b56a75f 100644
@@ -37,6 +37,7 @@ enum intel_pt_insn_branch {
 struct intel_pt_insn {
 	enum intel_pt_insn_op		op;
 	enum intel_pt_insn_branch	branch;
+	bool				emulated_ptwrite;
 	int				length;
 	int32_t				rel;
 	unsigned char			buf[INTEL_PT_INSN_BUF_SZ];
tools/perf/util/intel-pt.c +36 -1
index ec43d364d0de..c7e115fefe7d 100644
@@ -530,6 +530,7 @@ struct intel_pt_cache_entry {
 	u64				byte_cnt;
 	enum intel_pt_insn_op		op;
 	enum intel_pt_insn_branch	branch;
+	bool				emulated_ptwrite;
 	int				length;
 	int32_t				rel;
 	char				insn[INTEL_PT_INSN_BUF_SZ];
@@ -616,6 +617,7 @@ static int intel_pt_cache_add(struct dso *dso, struct machine *machine,
 	e->byte_cnt = byte_cnt;
 	e->op = intel_pt_insn->op;
 	e->branch = intel_pt_insn->branch;
+	e->emulated_ptwrite = intel_pt_insn->emulated_ptwrite;
 	e->length = intel_pt_insn->length;
 	e->rel = intel_pt_insn->rel;
 	memcpy(e->insn, intel_pt_insn->buf, INTEL_PT_INSN_BUF_SZ);
@@ -702,6 +704,28 @@ static int intel_pt_get_guest(struct intel_pt_queue *ptq)
 	return 0;
 }
 
+static inline bool intel_pt_jmp_16(struct intel_pt_insn *intel_pt_insn)
+{
+	return intel_pt_insn->rel == 16 && intel_pt_insn->branch == INTEL_PT_BR_UNCONDITIONAL;
+}
+
+#define PTWRITE_MAGIC		"\x0f\x0bperf,ptwrite  "
+#define PTWRITE_MAGIC_LEN	16
+
+static bool intel_pt_emulated_ptwrite(struct dso *dso, struct machine *machine, u64 offset)
+{
+	unsigned char buf[PTWRITE_MAGIC_LEN];
+	ssize_t len;
+
+	len = dso__data_read_offset(dso, machine, offset, buf, PTWRITE_MAGIC_LEN);
+	if (len == PTWRITE_MAGIC_LEN && !memcmp(buf, PTWRITE_MAGIC, PTWRITE_MAGIC_LEN)) {
+		intel_pt_log("Emulated ptwrite signature found\n");
+		return true;
+	}
+	intel_pt_log("Emulated ptwrite signature not found\n");
+	return false;
+}
+
 static int intel_pt_walk_next_insn(struct intel_pt_insn *intel_pt_insn,
 				   uint64_t *insn_cnt_ptr, uint64_t *ip,
 				   uint64_t to_ip, uint64_t max_insn_cnt,
@@ -764,6 +788,7 @@ static int intel_pt_walk_next_insn(struct intel_pt_insn *intel_pt_insn,
 				*ip += e->byte_cnt;
 				intel_pt_insn->op = e->op;
 				intel_pt_insn->branch = e->branch;
+				intel_pt_insn->emulated_ptwrite = e->emulated_ptwrite;
 				intel_pt_insn->length = e->length;
 				intel_pt_insn->rel = e->rel;
 				memcpy(intel_pt_insn->buf, e->insn,
@@ -795,8 +820,18 @@ static int intel_pt_walk_next_insn(struct intel_pt_insn *intel_pt_insn,
 
 			insn_cnt += 1;
 
-			if (intel_pt_insn->branch != INTEL_PT_BR_NO_BRANCH)
+			if (intel_pt_insn->branch != INTEL_PT_BR_NO_BRANCH) {
+				bool eptw;
+				u64 offs;
+
+				if (!intel_pt_jmp_16(intel_pt_insn))
+					goto out;
+				/* Check for emulated ptwrite */
+				offs = offset + intel_pt_insn->length;
+				eptw = intel_pt_emulated_ptwrite(al.map->dso, machine, offs);
+				intel_pt_insn->emulated_ptwrite = eptw;
 				goto out;
+			}
 
 			if (max_insn_cnt && insn_cnt >= max_insn_cnt)
 				goto out_no_cache;
Signature · 1 line
2.25.1
author reviewer
#2 lore raw

PATCH 2/3 perf script: Print Intel ptwrite value as a string if it is ASCII

In reply to Adrian Hunter

It can be convenient to put a string value into a ptwrite payload as a quick and easy way to identify what is being printed.

To make that useful, if the Intel ptwrite payload value contains only printable ASCII characters padded with NULLs, then print it also as a string.

Using the example program from the "Emulated PTWRITE" section of tools/perf/Documentation/perf-intel-pt.txt:

 $ echo -n "Hello" | od -t x8
 0000000 0000006f6c6c6548
 0000005
 $ perf record -e intel_pt//u ./eg_ptw 0x0000006f6c6c6548
 [ perf record: Woken up 1 times to write data ]
 [ perf record: Captured and wrote 0.016 MB perf.data ]
 $ perf script --itrace=ew
           eg_ptw 35563 [005] 18256.087338:     ptwrite:  IP: 0 payload: 0x6f6c6c6548 Hello      55e764db5196 perf_emulate_ptwrite+0x16 (/home/user/eg_ptw)
 $
---
 tools/perf/builtin-script.c | 32 ++++++++++++++++++++++++++++++--
 1 file changed, 30 insertions(+), 2 deletions(-)
tools/perf/builtin-script.c +30 -2
index cf5eab5431b4..ae6d216df438 100644
@@ -1742,16 +1742,44 @@ static int perf_sample__fprintf_pt_spacing(int len, FILE *fp)
 	return perf_sample__fprintf_spacing(len, 34, fp);
 }
 
+/* If a value contains only printable ASCII characters padded with NULLs */
+static bool ptw_is_prt(u64 val)
+{
+	char c;
+	u32 i;
+
+	for (i = 0; i < sizeof(val); i++) {
+		c = ((char *)&val)[i];
+		if (!c)
+			break;
+		if (!isprint(c) || !isascii(c))
+			return false;
+	}
+	for (; i < sizeof(val); i++) {
+		c = ((char *)&val)[i];
+		if (c)
+			return false;
+	}
+	return true;
+}
+
 static int perf_sample__fprintf_synth_ptwrite(struct perf_sample *sample, FILE *fp)
 {
 	struct perf_synth_intel_ptwrite *data = perf_sample__synth_ptr(sample);
+	char str[sizeof(u64) + 1] = "";
 	int len;
+	u64 val;
 
 	if (perf_sample__bad_synth_size(sample, *data))
 		return 0;
 
-	len = fprintf(fp, " IP: %u payload: %#" PRIx64 " ",
-		     data->ip, le64_to_cpu(data->payload));
+	val = le64_to_cpu(data->payload);
+	if (ptw_is_prt(val)) {
+		memcpy(str, &val, sizeof(val));
+		str[sizeof(val)] = 0;
+	}
+	len = fprintf(fp, " IP: %u payload: %#" PRIx64 " %s ",
+		      data->ip, val, str);
 	return len + perf_sample__fprintf_pt_spacing(len, fp);
 }
 
Signature · 1 line
2.25.1
author reviewer
#3 lore raw

PATCH 3/3 perf scripts python: intel-pt-events.py: Print ptwrite value as a string if it is ASCII

In reply to Adrian Hunter

It can be convenient to put a string value into a ptwrite payload as a quick and easy way to identify what is being printed.

To make that useful, if the Intel ptwrite payload value contains only printable ASCII characters padded with NULLs, then print it also as a string.

Using the example program from the "Emulated PTWRITE" section of tools/perf/Documentation/perf-intel-pt.txt:

 $ echo -n "Hello" | od -t x8
 0000000 0000006f6c6c6548
 0000005
 $ perf record -e intel_pt//u ./eg_ptw 0x0000006f6c6c6548
 [ perf record: Woken up 1 times to write data ]
 [ perf record: Captured and wrote 0.016 MB perf.data ]
 $ perf script --itrace=ew intel-pt-events.py
 Intel PT Branch Trace, Power Events, Event Trace and PTWRITE
      Switch In   38524/38524 [001]     24166.044995916     0/0
           eg_ptw 38524/38524 [001]     24166.045380004   ptwrite  jmp                   IP: 0 payload: 0x6f6c6c6548 Hello     56532c7ce196 perf_emulate_ptwrite+0x16 (/home/ahunter/git/work/eg_ptw)
 End
---
 tools/perf/scripts/python/intel-pt-events.py | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
tools/perf/scripts/python/intel-pt-events.py +7 -1
index 973bd12b7b40..9b7746b89381 100644
@@ -104,7 +104,13 @@ def print_ptwrite(raw_buf):
 	flags = data[0]
 	payload = data[1]
 	exact_ip = flags & 1
-	print("IP: %u payload: %#x" % (exact_ip, payload), end=' ')
+	try:
+		s = payload.to_bytes(8, "little").decode("ascii").rstrip("\x00")
+		if not s.isprintable():
+			s = ""
+	except:
+		s = ""
+	print("IP: %u payload: %#x" % (exact_ip, payload), s, end=' ')
 
 def print_cbr(raw_buf):
 	data = struct.unpack_from("<BBBBII", raw_buf)
Signature · 1 line
2.25.1
author reviewer
#4 lore raw
In reply to Adrian Hunter
Quoted 5 lines · Adrian Hunter

Hi

Here are 3 patches (on top of "perf intel-pt: Add link to the perf wiki's Intel PT page") to add support for decoding emulated ptwrite i.e. for hardware that does not have the ptwrite instruction.

Is this OK?

Quoted 20 lines

Adrian Hunter (3):
perf intel-pt: Add support for emulated ptwrite
perf script: Print Intel ptwrite value as a string if it is ASCII
perf scripts python: intel-pt-events.py: Print ptwrite value as a string if it is ASCII

 tools/perf/Documentation/perf-intel-pt.txt         | 88 +++++++++++++++++++
 tools/perf/builtin-script.c                        | 32 ++++++-
 tools/perf/scripts/python/intel-pt-events.py       |  8 +-
 .../perf/util/intel-pt-decoder/intel-pt-decoder.c  | 99 +++++++++++++++++++++-
 .../perf/util/intel-pt-decoder/intel-pt-decoder.h  |  1 +
 .../util/intel-pt-decoder/intel-pt-insn-decoder.c  |  1 +
 .../util/intel-pt-decoder/intel-pt-insn-decoder.h  |  1 +
 tools/perf/util/intel-pt.c                         | 37 +++++++-
 8 files changed, 261 insertions(+), 6 deletions(-)

Regards
Adrian

#5 lore raw

Em Tue, May 17, 2022 at 04:13:14PM +0300, Adrian Hunter escreveu:

Quoted 8 lines · Adrian Hunter
Quoted 5 lines

Hi

Here are 3 patches (on top of "perf intel-pt: Add link to the perf wiki's Intel PT page") to add support for decoding emulated ptwrite i.e. for hardware that does not have the ptwrite instruction.

Is this OK?

yeah, thanks, applied.

- Arnaldo

Quoted 20 lines
Quoted 20 lines

Adrian Hunter (3):
perf intel-pt: Add support for emulated ptwrite
perf script: Print Intel ptwrite value as a string if it is ASCII
perf scripts python: intel-pt-events.py: Print ptwrite value as a string if it is ASCII

 tools/perf/Documentation/perf-intel-pt.txt         | 88 +++++++++++++++++++
 tools/perf/builtin-script.c                        | 32 ++++++-
 tools/perf/scripts/python/intel-pt-events.py       |  8 +-
 .../perf/util/intel-pt-decoder/intel-pt-decoder.c  | 99 +++++++++++++++++++++-
 .../perf/util/intel-pt-decoder/intel-pt-decoder.h  |  1 +
 .../util/intel-pt-decoder/intel-pt-insn-decoder.c  |  1 +
 .../util/intel-pt-decoder/intel-pt-insn-decoder.h  |  1 +
 tools/perf/util/intel-pt.c                         | 37 +++++++-
 8 files changed, 261 insertions(+), 6 deletions(-)

Regards
Adrian

Signature · 1 line
- Arnaldo