Skip to contentSkip to conversation

New attempt at adding an disassembler to perf

PATCH In review Andi Kleen 3 participants 13 messages Jan 19, 2017 – Jan 24, 2017 perf events

lore
Share & tools
lore

Andi Kleen

author
#0 lore raw

A native disassembler in perf is very useful, in particular with perf script to trace instruction streams, but also for other analysis. Previously I attempted to do this using the udis86 library, but that was rejected because:
- udis86 was not maintained anymore and lacking recent instructions
- udis86 is dynamically linked and gives a runtime dependency.
Doing this needs a full disassembler, not just a decoder, so the existing instruction decoder cannot be used without major changes.

This patchkit addresses these issues. Intel recently released an open source version of the XED disassembler library, which is used in many other Intel software. It is very well maintained, uptodate, and supports static linking, so there is no runtime dependency. This version adds XED support to perf, and uses it to implement assembler output in perf script. It also fixes a range of issues in the previous version, see the individual change logs.

Available in
git://git.kernel.org/pub/scm/linux/kernel/git/ak/linux-misc.git perf/xed-6

v1: First post of XED version
v2: Change probing to not be default and support XED_DIR. Other cleanups based on review.
v3: Use FEATURE_FLAGS_BASIC for probing

-Andi

author
#1 lore raw

PATCH 1/5 perf, tools: Add probing for xed

From: Andi Kleen <[email protected]>

Add autoprobing for the xed disassembler library.

Can be downloaded from https://github.com/intelxed/xed

v2: Hide. Require XED=1 to enable. Add XED_DIR
v3: Remove -lxed from probe all. Don't touch FEATURE_DISPLAY.
v4: Move to FEATURE_FLAGS_BASIC

---
 tools/build/Makefile.feature   |  4 +++-
 tools/build/feature/Makefile   |  6 +++++-
 tools/build/feature/test-all.c |  5 +++++
 tools/build/feature/test-xed.c |  9 +++++++++
 tools/perf/Makefile.config     | 16 ++++++++++++++++
 tools/perf/Makefile.perf       |  3 +++
 6 files changed, 41 insertions(+), 2 deletions(-)
 create mode 100644 tools/build/feature/test-xed.c
tools/build/Makefile.feature +3 -1
index e3fb5ecbdcb6..c5012b8a49fe 100644
@@ -63,7 +63,8 @@ FEATURE_TESTS_BASIC :=                  \
         lzma                            \
         get_cpuid                       \
         bpf                             \
-        sdt
+        sdt				\
+        xed
 
 # FEATURE_TESTS_BASIC + FEATURE_TESTS_EXTRA is the complete list
 # of all feature tests
@@ -140,6 +141,7 @@ ifeq ($(feature-all), 1)
   $(call feature_check,compile-x32)
   $(call feature_check,bionic)
   $(call feature_check,libbabeltrace)
+  $(call feature_check,xed)
 else
   $(foreach feat,$(FEATURE_TESTS),$(call feature_check,$(feat)))
 endif
tools/build/feature/Makefile +5 -1
index b564a2eea039..4f1aa82b867a 100644
@@ -48,7 +48,8 @@ FILES=                                          \
          test-get_cpuid.bin                     \
          test-sdt.bin                           \
          test-cxx.bin                           \
-         test-jvmti.bin
+         test-jvmti.bin				\
+         test-xed.bin
 
 FILES := $(addprefix $(OUTPUT),$(FILES))
 
@@ -123,6 +124,9 @@ $(OUTPUT)test-numa_num_possible_cpus.bin:
 $(OUTPUT)test-libunwind.bin:
 	$(BUILD) -lelf
 
+$(OUTPUT)test-xed.bin:
+	$(BUILD) -lxed
+
 $(OUTPUT)test-libunwind-debug-frame.bin:
 	$(BUILD) -lelf
 $(OUTPUT)test-libunwind-x86.bin:
tools/build/feature/test-all.c +5 -0
index 699e43627397..4a6dd1d1ff49 100644
@@ -149,6 +149,10 @@
 # include "test-sdt.c"
 #undef main
 
+#define main main_test_xed
+#  include "test-xed.c"
+#endif
+
 int main(int argc, char *argv[])
 {
 	main_test_libpython();
@@ -183,6 +187,7 @@ int main(int argc, char *argv[])
 	main_test_bpf();
 	main_test_libcrypto();
 	main_test_sdt();
+	main_test_xed();
 
 	return 0;
 }
tools/build/feature/test-xed.c new file +9 -0
new file mode 100644
index 000000000000..ef9aebf1559d
@@ -0,0 +1,9 @@
+#include <xed/xed-interface.h>
+#include <xed/xed-decode.h>
+#include <xed/xed-decoded-inst-api.h>
+
+int main(void)
+{
+	xed_tables_init();
+	return 0;
+}
tools/perf/Makefile.config +16 -0
index 03cf947755b9..9996027e7a52 100644
@@ -684,6 +684,22 @@ ifndef NO_ZLIB
   endif
 endif
 
+ifdef XED
+  ifdef XED_DIR
+    FEATURE_CHECK_CFLAGS-xed := -I $(XED_DIR)/include
+    # override for lib64?
+    FEATURE_CHECK_LDFLAGS-xed := -L $(XED_DIR)/lib -lxed
+  else
+    FEATURE_CHECK_CFLAGS-xed :=
+    FEATURE_CHECK_LDFLAGS-xed := -lxed
+  endif
+  $(call feature_check,xed)
+  ifeq ($(feature-xed), 1)
+    EXTLIBS += -lxed
+    $(call detected,CONFIG_XED)
+  endif
+endif
+
 ifndef NO_LZMA
   ifeq ($(feature-lzma), 1)
     CFLAGS += -DHAVE_LZMA_SUPPORT
tools/perf/Makefile.perf +3 -0
index 4da19b6ba94a..f6760309edd3 100644
@@ -84,6 +84,9 @@ include ../scripts/utilities.mak
 # Define NO_SDT if you do not want to define SDT event in perf tools,
 # note that it doesn't disable SDT scanning support.
 #
+# Define XED=1 to build WITH the XED disassembler for perf script
+# Can also set XED_DIR=/path to set XED directory.
+#
 # Define FEATURES_DUMP to provide features detection dump file
 # and bypass the feature detection
 #
Signature · 1 line
2.9.3
author
#2 lore raw

PATCH 2/5 perf, tools: Add one liner warning for disabled features

In reply to Andi Kleen

From: Andi Kleen <[email protected]>

Add a one liner warning for perf features that need to be enabled explicitly by the user, so that they know they are missing something. Currently enabled for XED and BABELTRACE.

---
 tools/perf/Makefile.config | 10 ++++++++++
 1 file changed, 10 insertions(+)
tools/perf/Makefile.config +10 -0
index 9996027e7a52..39b90ffd9412 100644
@@ -65,6 +65,8 @@ ifneq ($(ARCH),$(filter $(ARCH),x86 arm))
   NO_LIBDW_DWARF_UNWIND := 1
 endif
 
+DISABLED_FEATURES =
+
 ifeq ($(LIBUNWIND_LIBS),)
   NO_LIBUNWIND := 1
 endif
@@ -698,6 +700,8 @@ ifdef XED
     EXTLIBS += -lxed
     $(call detected,CONFIG_XED)
   endif
+else
+  DISABLED_FEATURES += XED
 endif
 
 ifndef NO_LZMA
@@ -772,6 +776,8 @@ ifdef LIBBABELTRACE
   else
     msg := $(warning No libbabeltrace found, disables 'perf data' CTF format support, please install libbabeltrace-dev[el]/libbabeltrace-ctf-dev);
   endif
+else
+  DISABLED_FEATURES += LIBBABELTRACE
 endif
 
 ifndef NO_AUXTRACE
@@ -843,6 +849,10 @@ ifdef LIBCLANGLLVM
   endif
 endif
 
+ifneq ($(DISABLED_FEATURES),)
+  $(warning Disabled features, need explicit enabling by user: $(DISABLED_FEATURES))
+endif
+
 # Among the variables below, these:
 #   perfexecdir
 #   template_dir
Signature · 1 line
2.9.3
author
#3 lore raw

PATCH 3/5 perf, tools: Add disassembler for x86 using the XED library

In reply to Andi Kleen

From: Andi Kleen <[email protected]>

Add a generic disassembler function for x86 using the XED library, and a fallback function for architectures that don't implement one. Other architectures can implement their own disassembler functions.

The previous version of this patch used udis86, but was rejected because udis86 was unmaintained and a runtime dependency. Using the recently released xed avoids both of these problems:
- XED is well maintained, uptodate, and used by many Intel tools
- XED is linked statically so there is no runtime dependency.

The XED library can be downloaded from http://github.com/intelxed/xed

v2: Clean up includes.

---
 tools/perf/arch/x86/util/Build |  1 +
 tools/perf/arch/x86/util/dis.c | 86 ++++++++++++++++++++++++++++++++++++++++++
 tools/perf/util/Build          |  1 +
 tools/perf/util/dis.c          | 15 ++++++++
 tools/perf/util/dis.h          | 20 ++++++++++
 5 files changed, 123 insertions(+)
 create mode 100644 tools/perf/arch/x86/util/dis.c
 create mode 100644 tools/perf/util/dis.c
 create mode 100644 tools/perf/util/dis.h
tools/perf/arch/x86/util/Build +1 -0
index f95e6f46ef0d..93490009ea6a 100644
@@ -14,3 +14,4 @@ libperf-$(CONFIG_LIBDW_DWARF_UNWIND) += unwind-libdw.o
 libperf-$(CONFIG_AUXTRACE) += auxtrace.o
 libperf-$(CONFIG_AUXTRACE) += intel-pt.o
 libperf-$(CONFIG_AUXTRACE) += intel-bts.o
+libperf-$(CONFIG_XED) += dis.o
tools/perf/arch/x86/util/dis.c new file +86 -0

86 lines not on this page · raw message

tools/perf/util/Build +1 -0
index 5da376bc1afc..cdaeb4764fee 100644
@@ -88,6 +88,7 @@ libperf-y += mem-events.o
 libperf-y += vsprintf.o
 libperf-y += drv_configs.o
 libperf-y += time-utils.o
+libperf-y += dis.o
 
 libperf-$(CONFIG_LIBBPF) += bpf-loader.o
 libperf-$(CONFIG_BPF_PROLOGUE) += bpf-prologue.o
tools/perf/util/dis.c new file +15 -0
new file mode 100644
index 000000000000..89c9b84051b8
@@ -0,0 +1,15 @@
+#include "perf.h"
+#include "dis.h"
+#include "util.h"
+
+/* Fallback for architectures with no disassembler */
+
+__weak char *disas_inst(struct perf_dis *x, uint64_t ip __maybe_unused,
+		u8 *inbuf __maybe_unused, int inlen __maybe_unused,
+		int *lenp)
+{
+	if (lenp)
+		*lenp = 0;
+	strcpy(x->out, "?");
+	return x->out;
+}
tools/perf/util/dis.h new file +20 -0
new file mode 100644
index 000000000000..ffda324cbc1a
@@ -0,0 +1,20 @@
+#ifndef DIS_H
+#define DIS_H 1
+
+#define MAXINSN 15
+
+struct perf_dis {
+	/* Initialized by callers: */
+	struct thread *thread;
+	u8 cpumode;
+	int cpu;
+	bool is64bit;
+	/* Temporary */
+	char out[256];
+};
+
+char *disas_inst(struct perf_dis *x, uint64_t ip, u8 *inbuf, int inlen,
+		 int *lenp);
+
+
+#endif
Signature · 1 line
2.9.3
author
#4 lore raw

PATCH 4/5 perf, tools, script: Add support for printing assembler

In reply to Andi Kleen

From: Andi Kleen <[email protected]>

When dumping PT traces with perf script it is very useful to see the assembler for each sample, so that it is easily possible to follow the control flow.

As using objdump is difficult and inefficient from perf script this patch uses the Intel xed library to implement assembler output. The library can be downloaded from http://github.com/intelxed/xed

The previous version of this patch used udis86, but was rejected because udis86 was unmaintained and a runtime dependency. Using the recently released xed avoids both of these problems:
- XED is well maintained and used by many Intel tools
- XED is linked statically so there is no runtime dependency.

The library is probed as an external dependency in the usual way. Then perf script calls into it when needed, and handles callbacks to resolve symbols.

% perf record -e intel_pt//u true
% perf script -F sym,symoff,ip,asm --itrace=i0ns | head

     7fc7188b4190 _start+0x0	mov %rsp, %rdi
     7fc7188b4193 _start+0x3	call _dl_start
     7fc7188b7710 _dl_start+0x0	push %rbp
     7fc7188b7711 _dl_start+0x1	mov %rsp, %rbp
     7fc7188b7714 _dl_start+0x4	push %r15
     7fc7188b7716 _dl_start+0x6	push %r14
     7fc7188b7718 _dl_start+0x8	push %r13
     7fc7188b771a _dl_start+0xa	push %r12
     7fc7188b771c _dl_start+0xc	mov %rdi, %r12
     7fc7188b771f _dl_start+0xf	push %rbx

v2:
Converted to use XED instead of udis86.
Separate disassembler interface into separate arch specific file. Lots of cleanups and improvements.

---
 tools/perf/Documentation/perf-script.txt |  4 +-
 tools/perf/builtin-script.c              | 72 +++++++++++++++++++++++++++-----
 2 files changed, 64 insertions(+), 12 deletions(-)
tools/perf/Documentation/perf-script.txt +3 -1
index 4ed5f239ba7d..497989ea9768 100644
@@ -116,7 +116,7 @@ OPTIONS
 --fields::
         Comma separated list of fields to print. Options are:
         comm, tid, pid, time, cpu, event, trace, ip, sym, dso, addr, symoff,
-        srcline, period, iregs, brstack, brstacksym, flags, bpf-output,
+        srcline, period, iregs, brstack, brstacksym, flags, bpf-output, asm.
         callindent, insn, insnlen. Field list can be prepended with the type, trace, sw or hw,
         to indicate to which event type the field list applies.
         e.g., -F sw:comm,tid,time,ip,sym  and -F trace:time,cpu,trace
@@ -198,6 +198,8 @@ OPTIONS
 
 	The brstacksym is identical to brstack, except that the FROM and TO addresses are printed in a symbolic form if possible.
 
+	When asm is specified the assembler instruction of each sample is printed in disassembled form.
+
 -k::
 --vmlinux=<file>::
         vmlinux pathname
tools/perf/builtin-script.c +61 -11

154 lines not on this page · raw message

Signature · 1 line
2.9.3
#5 lore raw

Re: [PATCH 4/5] perf, tools, script: Add support for printing assembler

Em Wed, Jan 18, 2017 at 05:41:49PM -0800, Andi Kleen escreveu:

Quoted 9 lines

From: Andi Kleen <[email protected]>

When dumping PT traces with perf script it is very useful to see the assembler for each sample, so that it is easily possible to follow the control flow.

As using objdump is difficult and inefficient from perf script this patch uses the Intel xed library to implement assembler output. The library can be downloaded from http://github.com/intelxed/xed

What I have, and there are multiple changes to make this build, is available at the tmp.perf/xed branch of my tree at:

git://git.kernel.org/pub/scm/linux/kernel/git/acme/linux.git

https://git.kernel.org/cgit/linux/kernel/git/acme/linux.git/log/?h=tmp.perf/xed

So, while testing this I noticed some differences from what objdump -d produces to what is produced with this patch, see below, where it is different I added the bytes from the objdump output, have to lookup the tables manually to check each case, and some seem harmless.

Do you know if there is any tool comparing the output of objdump -d to what is produced by a similar xed based tool?

Disassembly of perf_evsel__enable() bits referenced in the samples collected for this test:

'perf script' with this patch: DIFF objdump -d

  4b8506  jz 0x4b84d0 <perf_evsel__enable+0x70>      74 c8                 je     4b84d0 <perf_evsel__enable+0x70>
  4b84d0  add $0x1, %r14                                                   add    $0x1,%r14
  4b84d4  cmp %r14d, %ebx                                                  cmp    %r14d,%ebx
  4b84d7  jle 0x4b8530 <perf_evsel__enable+0xd0>                           jle    4b8530 <perf_evsel__enable+0xd0>
  4b8530  add $0x1, %r12                                                   add    $0x1,%r12
  4b8534  cmp %r12d, %r13d                                                 cmp    %r12d,%r13d
  4b8537  jnle 0x4b84c2 <perf_evsel__enable+0x62>    7f 89                 jg     4b84c2 <perf_evsel__enable+0x62>
  4b84c2  xor %r14d, %r14d                                                 xor    %r14d,%r14d
  4b84c5  test %ebx, %ebx                                                  test   %ebx,%ebx
  4b84c7  jnle 0x4b84d9 <perf_evsel__enable+0x79>    7f 10                 jg     4b84d9 <perf_evsel__enable+0x79>
  4b84d9  movq  0x90(%r15), %rax                     49 8b 87 90 00 00 00  mov    0x90(%r15),%rax
  4b84e0  mov %r12, %rdx                                                   mov    %r12,%rdx
  4b84e3  mov %r14, %rcx                                                   mov    %r14,%rcx
  4b84e6  mov $0x2400, %esi                                                mov    $0x2400,%esi
  4b84eb  imulq  (%rax), %rdx                        48 0f af 10           imul   (%rax),%rdx
  4b84ef  imulq  0x8(%rax), %rcx                     48 0f af 48 08        imul   0x8(%rax),%rcx
  4b84f4  add %rdx, %rax                                                   add    %rdx,%rax
  4b84f7  xor %edx, %edx                                                   xor    %edx,%edx
  4b84f9  movl  0x18(%rcx,%rax,1), %edi              8b 7c 01 18           mov    0x18(%rcx,%rax,1),%edi
  4b84fd  xor %eax, %eax                                                   xor    %eax,%eax
  4b84ff  callq  0x42d990 <ioctl@plt>                                      callq  42d990 <ioctl@plt>
  4b8504  test %eax, %eax                                                  test   %eax,%eax
  4b8506  jz 0x4b84d0 <perf_evsel__enable+0x70>      74 c8                 je     4b84d0 <perf_evsel__enable+0x70>

Built today using xed from:

https://github.com/intelxed/xed

[acme@jouet xed]$ git log --oneline -5
4507b57ba629 rebase tests to account for new operand sorting c7c1777216f5 generator: xed operand ordering sort was wrong eb45a282de28 convert LOOPNE/E comments to XED COMMENT field 22427e1a4027 generator.py: duplicate iform check now checks isa-set conflicts 4a0a09a1542e Disambiguate iforms for VFPCLASS{PD,PS} mem forms, suffix with VL [acme@jouet xed]$

Quoted 230 lines

The previous version of this patch used udis86, but was rejected because udis86 was unmaintained and a runtime dependency. Using the recently released xed avoids both of these problems:
- XED is well maintained and used by many Intel tools
- XED is linked statically so there is no runtime dependency.

The library is probed as an external dependency in the usual way. Then perf script calls into it when needed, and handles callbacks to resolve symbols.

% perf record -e intel_pt//u true
% perf script -F sym,symoff,ip,asm --itrace=i0ns | head

     7fc7188b4190 _start+0x0	mov %rsp, %rdi
     7fc7188b4193 _start+0x3	call _dl_start
     7fc7188b7710 _dl_start+0x0	push %rbp
     7fc7188b7711 _dl_start+0x1	mov %rsp, %rbp
     7fc7188b7714 _dl_start+0x4	push %r15
     7fc7188b7716 _dl_start+0x6	push %r14
     7fc7188b7718 _dl_start+0x8	push %r13
     7fc7188b771a _dl_start+0xa	push %r12
     7fc7188b771c _dl_start+0xc	mov %rdi, %r12
     7fc7188b771f _dl_start+0xf	push %rbx

v2:
Converted to use XED instead of udis86.
Separate disassembler interface into separate arch specific file. Lots of cleanups and improvements.

---
 tools/perf/Documentation/perf-script.txt |  4 +-
 tools/perf/builtin-script.c              | 72 +++++++++++++++++++++++++++-----
 2 files changed, 64 insertions(+), 12 deletions(-)
tools/perf/Documentation/perf-script.txt +3 -1
index 4ed5f239ba7d..497989ea9768 100644
@@ -116,7 +116,7 @@ OPTIONS
 --fields::
         Comma separated list of fields to print. Options are:
         comm, tid, pid, time, cpu, event, trace, ip, sym, dso, addr, symoff,
-        srcline, period, iregs, brstack, brstacksym, flags, bpf-output,
+        srcline, period, iregs, brstack, brstacksym, flags, bpf-output, asm.
         callindent, insn, insnlen. Field list can be prepended with the type, trace, sw or hw,
         to indicate to which event type the field list applies.
         e.g., -F sw:comm,tid,time,ip,sym  and -F trace:time,cpu,trace
@@ -198,6 +198,8 @@ OPTIONS
 
 	The brstacksym is identical to brstack, except that the FROM and TO addresses are printed in a symbolic form if possible.
 
+	When asm is specified the assembler instruction of each sample is printed in disassembled form.
+
 -k::
 --vmlinux=<file>::
         vmlinux pathname
tools/perf/builtin-script.c +61 -11
index c0783b4f7b6c..7a09c4f7df3f 100644
@@ -28,6 +28,7 @@
 #include <linux/time64.h>
 #include "asm/bug.h"
 #include "util/mem-events.h"
+#include "util/dis.h"
 
 static char const		*script_name;
 static char const		*generate_script_lang;
@@ -69,6 +70,7 @@ enum perf_output_field {
 	PERF_OUTPUT_CALLINDENT	    = 1U << 20,
 	PERF_OUTPUT_INSN	    = 1U << 21,
 	PERF_OUTPUT_INSNLEN	    = 1U << 22,
+	PERF_OUTPUT_ASM		    = 1U << 23,
 };
 
 struct output_option {
@@ -98,6 +100,7 @@ struct output_option {
 	{.str = "callindent", .field = PERF_OUTPUT_CALLINDENT},
 	{.str = "insn", .field = PERF_OUTPUT_INSN},
 	{.str = "insnlen", .field = PERF_OUTPUT_INSNLEN},
+	{.str = "asm", .field = PERF_OUTPUT_ASM},
 };
 
 /* default set to maintain compatibility with current format */
@@ -292,7 +295,11 @@ static int perf_evsel__check_attr(struct perf_evsel *evsel,
 		       "selected. Hence, no address to lookup the source line number.\n");
 		return -EINVAL;
 	}
-
+	if (PRINT_FIELD(ASM) && !PRINT_FIELD(IP)) {
+		pr_err("Display of assembler requested but sample IP is not\n"
+		       "selected.\n");
+		return -EINVAL;
+	}
 	if ((PRINT_FIELD(PID) || PRINT_FIELD(TID)) &&
 		perf_evsel__check_stype(evsel, PERF_SAMPLE_TID, "TID",
 					PERF_OUTPUT_TID|PERF_OUTPUT_PID))
@@ -436,6 +443,39 @@ static void print_sample_iregs(struct perf_sample *sample,
 	}
 }
 
+static void print_sample_asm(union perf_event *event,
+			     struct perf_sample *sample,
+			     struct thread *thread,
+			     struct addr_location *al,
+			     struct machine *machine)
+{
+	struct perf_dis x;
+	u8 buffer[32];
+	int len;
+	u64 offset;
+
+	x.thread = thread;
+	x.cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
+	x.cpu = sample->cpu;
+
+	if (!al->map || !al->map->dso)
+		return;
+	if (al->map->dso->data.status == DSO_DATA_STATUS_ERROR)
+		return;
+
+	/* Load maps to ensure dso->is_64_bit has been updated */
+	map__load(al->map);
+	x.is64bit = al->map->dso->is_64_bit;
+
+	offset = al->map->map_ip(al->map, sample->ip);
+	len = dso__data_read_offset(al->map->dso, machine,
+				    offset, buffer, MAXINSN);
+	if (len <= 0)
+		return;
+
+	printf("\t%s", disas_inst(&x, sample->ip, buffer, len, NULL));
+}
+
 static void print_sample_start(struct perf_sample *sample,
 			       struct thread *thread,
 			       struct perf_evsel *evsel)
@@ -631,8 +671,12 @@ static void print_sample_callindent(struct perf_sample *sample,
 		printf("%*s", spacing - len, "");
 }
 
-static void print_insn(struct perf_sample *sample,
-		       struct perf_event_attr *attr)
+static void print_insn(union perf_event *event,
+		       struct perf_sample *sample,
+		       struct perf_event_attr *attr,
+		       struct thread *thread,
+		       struct addr_location *al,
+		       struct machine *machine)
 {
 	if (PRINT_FIELD(INSNLEN))
 		printf(" ilen: %d", sample->insn_len);
@@ -643,12 +687,16 @@ static void print_insn(struct perf_sample *sample,
 		for (i = 0; i < sample->insn_len; i++)
 			printf(" %02x", (unsigned char)sample->insn[i]);
 	}
+	if (PRINT_FIELD(ASM))
+		print_sample_asm(event, sample, thread, al, machine);
 }
 
-static void print_sample_bts(struct perf_sample *sample,
+static void print_sample_bts(union perf_event *event,
+			     struct perf_sample *sample,
 			     struct perf_evsel *evsel,
 			     struct thread *thread,
-			     struct addr_location *al)
+			     struct addr_location *al,
+			     struct machine *machine)
 {
 	struct perf_event_attr *attr = &evsel->attr;
 	bool print_srcline_last = false;
@@ -689,7 +737,7 @@ static void print_sample_bts(struct perf_sample *sample,
 	if (print_srcline_last)
 		map__fprintf_srcline(al->map, al->addr, "\n  ", stdout);
 
-	print_insn(sample, attr);
+	print_insn(event, sample, attr, thread, al, machine);
 
 	printf("\n");
 }
@@ -871,7 +919,9 @@ static size_t data_src__printf(u64 data_src)
 
 static void process_event(struct perf_script *script,
 			  struct perf_sample *sample, struct perf_evsel *evsel,
-			  struct addr_location *al)
+			  struct addr_location *al,
+			  struct machine *machine,
+			  union perf_event *event)
 {
 	struct thread *thread = al->thread;
 	struct perf_event_attr *attr = &evsel->attr;
@@ -898,7 +948,7 @@ static void process_event(struct perf_script *script,
 		print_sample_flags(sample->flags);
 
 	if (is_bts_event(attr)) {
-		print_sample_bts(sample, evsel, thread, al);
+		print_sample_bts(event, sample, evsel, thread, al, machine);
 		return;
 	}
 
@@ -936,7 +986,7 @@ static void process_event(struct perf_script *script,
 
 	if (perf_evsel__is_bpf_output(evsel) && PRINT_FIELD(BPF_OUTPUT))
 		print_sample_bpf_output(sample);
-	print_insn(sample, attr);
+	print_insn(event, sample, attr, thread, al, machine);
 	printf("\n");
 }
 
@@ -1046,7 +1096,7 @@ static int process_sample_event(struct perf_tool *tool,
 	if (scripting_ops)
 		scripting_ops->process_event(event, sample, evsel, &al);
 	else
-		process_event(scr, sample, evsel, &al);
+		process_event(scr, sample, evsel, &al, machine, event);
 
 out_put:
 	addr_location__put(&al);
@@ -2152,7 +2202,7 @@ int cmd_script(int argc, const char **argv, const char *prefix __maybe_unused)
 		     "Valid types: hw,sw,trace,raw. "
 		     "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso,"
 		     "addr,symoff,period,iregs,brstack,brstacksym,flags,"
-		     "bpf-output,callindent,insn,insnlen", parse_output_fields),
+		     "bpf-output,callindent,insn,insnlen,asm", parse_output_fields),
 	OPT_BOOLEAN('a', "all-cpus", &system_wide,
 		    "system-wide collection from all CPUs"),
 	OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
Signature · 1 line
2.9.3
author
#6 lore raw

Re: [PATCH 4/5] perf, tools, script: Add support for printing assembler

Quoted 2 lines

Do you know if there is any tool comparing the output of objdump -d to what is produced by a similar xed based tool?

I'm not aware of such a tool, but could be written using the "xed" tool in the xed distribution. However I would trust xed over objdump, it is used widely in Intel tools with likely far more testing than binutils

Quoted 15 lines
  4b8506  jz 0x4b84d0 <perf_evsel__enable+0x70>      74 c8                 je     4b84d0 <perf_evsel__enable+0x70>
  4b84d0  add $0x1, %r14                                                   add    $0x1,%r14
  4b84d4  cmp %r14d, %ebx                                                  cmp    %r14d,%ebx
  4b84d7  jle 0x4b8530 <perf_evsel__enable+0xd0>                           jle    4b8530 <perf_evsel__enable+0xd0>
  4b8530  add $0x1, %r12                                                   add    $0x1,%r12
  4b8534  cmp %r12d, %r13d                                                 cmp    %r12d,%r13d
  4b8537  jnle 0x4b84c2 <perf_evsel__enable+0x62>    7f 89                 jg     4b84c2 <perf_evsel__enable+0x62>
  4b84c2  xor %r14d, %r14d                                                 xor    %r14d,%r14d
  4b84c5  test %ebx, %ebx                                                  test   %ebx,%ebx
  4b84c7  jnle 0x4b84d9 <perf_evsel__enable+0x79>    7f 10                 jg     4b84d9 <perf_evsel__enable+0x79>
  4b84d9  movq  0x90(%r15), %rax                     49 8b 87 90 00 00 00  mov    0x90(%r15),%rax
  4b84e0  mov %r12, %rdx                                                   mov    %r12,%rdx
  4b84e3  mov %r14, %rcx                                                   mov    %r14,%rcx
  4b84e6  mov $0x2400, %esi                                                mov    $0x2400,%esi
  4b84eb  imulq  (%rax), %rdx                        48 0f af 10           imul   (%rax),%rdx
Quoted 8 lines
  4b84ef  imulq  0x8(%rax), %rcx                     48 0f af 48 08        imul   0x8(%rax),%rcx
  4b84f4  add %rdx, %rax                                                   add    %rdx,%rax
  4b84f7  xor %edx, %edx                                                   xor    %edx,%edx
  4b84f9  movl  0x18(%rcx,%rax,1), %edi              8b 7c 01 18           mov    0x18(%rcx,%rax,1),%edi
  4b84fd  xor %eax, %eax                                                   xor    %eax,%eax
  4b84ff  callq  0x42d990 <ioctl@plt>                                      callq  42d990 <ioctl@plt>
  4b8504  test %eax, %eax                                                  test   %eax,%eax
  4b8506  jz 0x4b84d0 <perf_evsel__enable+0x70>      74 c8                 je     4b84d0 <perf_evsel__enable+0x70>

Yes all the differences are ok. It's just synonyms of the instructions.

-Andi

#7 lore raw

Re: [PATCH 4/5] perf, tools, script: Add support for printing assembler

Em Mon, Jan 23, 2017 at 11:55:21AM -0800, Andi Kleen escreveu:

Quoted 5 lines
Quoted 2 lines

Do you know if there is any tool comparing the output of objdump -d to what is produced by a similar xed based tool?

I'm not aware of such a tool, but could be written using the "xed" tool in the xed distribution. However I would trust xed over objdump,

It would go both ways, and I was not implying which one is "better", just checking differences in the output.

Quoted 30 lines

it is used widely in Intel tools with likely far more testing than binutils

Quoted 15 lines
  4b8506  jz 0x4b84d0 <perf_evsel__enable+0x70>      74 c8                 je     4b84d0 <perf_evsel__enable+0x70>
  4b84d0  add $0x1, %r14                                                   add    $0x1,%r14
  4b84d4  cmp %r14d, %ebx                                                  cmp    %r14d,%ebx
  4b84d7  jle 0x4b8530 <perf_evsel__enable+0xd0>                           jle    4b8530 <perf_evsel__enable+0xd0>
  4b8530  add $0x1, %r12                                                   add    $0x1,%r12
  4b8534  cmp %r12d, %r13d                                                 cmp    %r12d,%r13d
  4b8537  jnle 0x4b84c2 <perf_evsel__enable+0x62>    7f 89                 jg     4b84c2 <perf_evsel__enable+0x62>
  4b84c2  xor %r14d, %r14d                                                 xor    %r14d,%r14d
  4b84c5  test %ebx, %ebx                                                  test   %ebx,%ebx
  4b84c7  jnle 0x4b84d9 <perf_evsel__enable+0x79>    7f 10                 jg     4b84d9 <perf_evsel__enable+0x79>
  4b84d9  movq  0x90(%r15), %rax                     49 8b 87 90 00 00 00  mov    0x90(%r15),%rax
  4b84e0  mov %r12, %rdx                                                   mov    %r12,%rdx
  4b84e3  mov %r14, %rcx                                                   mov    %r14,%rcx
  4b84e6  mov $0x2400, %esi                                                mov    $0x2400,%esi
  4b84eb  imulq  (%rax), %rdx                        48 0f af 10           imul   (%rax),%rdx
Quoted 8 lines
  4b84ef  imulq  0x8(%rax), %rcx                     48 0f af 48 08        imul   0x8(%rax),%rcx
  4b84f4  add %rdx, %rax                                                   add    %rdx,%rax
  4b84f7  xor %edx, %edx                                                   xor    %edx,%edx
  4b84f9  movl  0x18(%rcx,%rax,1), %edi              8b 7c 01 18           mov    0x18(%rcx,%rax,1),%edi
  4b84fd  xor %eax, %eax                                                   xor    %eax,%eax
  4b84ff  callq  0x42d990 <ioctl@plt>                                      callq  42d990 <ioctl@plt>
  4b8504  test %eax, %eax                                                  test   %eax,%eax
  4b8506  jz 0x4b84d0 <perf_evsel__enable+0x70>      74 c8                 je     4b84d0 <perf_evsel__enable+0x70>

Yes all the differences are ok. It's just synonyms of the instructions.

-Andi

author
#8 lore raw

PATCH 5/5 perf, tools, script: Add brstackasm output for branch stacks

In reply to Andi Kleen

From: Andi Kleen <[email protected]>

Implement printing full disassembled sequences for branch stacks in perf script. This allows to directly print hot paths for individual samples, together with branch misprediction and cycle count / IPC information if available (on Skylake systems). This only works when no special branch filters are specified.

% perf record -b ...
% perf script -F brstackasm
...

        000055b55d1147d0        pushq  %rbp
        000055b55d1147d1        pushq  %r15
        000055b55d1147d3        pushq  %r14
        000055b55d1147d5        pushq  %r13
        000055b55d1147d7        pushq  %r12
        000055b55d1147d9        pushq  %rbx
        000055b55d1147da        sub $0x18, %rsp
        000055b55d1147de        mov %r8, %r13
        000055b55d1147e1        mov %rcx, %rbp
        000055b55d1147e4        mov %rdx, %r14
        000055b55d1147e7        mov %rsi, %r15
        000055b55d1147ea        mov %rdi, %rbx
        000055b55d1147ed        movl  $0x0, 0xc(%rsp)
        000055b55d1147f5        movq  (%rbp), %rax
        000055b55d1147f9        test $0x1, %al
        000055b55d1147fb        jnz 0x55b55d114890              # PRED 4 cycles 3.75 IPC
        000055b55d114890        mov %eax, %ecx
        000055b55d114892        and $0x3, %ecx
        000055b55d114895        cmp $0x1, %rcx
        000055b55d114899        jnz 0x55b55d1148f8
        000055b55d11489b        movq  -0x1(%rax), %rcx
        000055b55d11489f        cmpb  $0x81, 0xb(%rcx)
        000055b55d1148a3        jnz 0x55b55d1148fe              # PRED 1 cycles 6.00 IPC

...

Occasionally the path does not reach up to the sample IP, as the LBRs may be frozen before executing a final jump. In this case we print a special message.

v2:
Use low level abstracted disassembler interface.
Print symbols and source lines as labels.
Print first jump in LBR too.
Patch up blocks with filtered ring transfers.
Show IPC
Lots of cleanups and improvements.
v3:
Print special message for branches frozen early.

---
 tools/perf/Documentation/perf-script.txt |  13 +-
 tools/perf/builtin-script.c              | 267 +++++++++++++++++++++++++++++++
 2 files changed, 278 insertions(+), 2 deletions(-)
tools/perf/Documentation/perf-script.txt +11 -2
index 497989ea9768..15a80815941e 100644
@@ -116,7 +116,7 @@ OPTIONS
 --fields::
         Comma separated list of fields to print. Options are:
         comm, tid, pid, time, cpu, event, trace, ip, sym, dso, addr, symoff,
-        srcline, period, iregs, brstack, brstacksym, flags, bpf-output, asm.
+        srcline, period, iregs, brstack, brstacksym, flags, bpf-output, asm, brstackasm,
         callindent, insn, insnlen. Field list can be prepended with the type, trace, sw or hw,
         to indicate to which event type the field list applies.
         e.g., -F sw:comm,tid,time,ip,sym  and -F trace:time,cpu,trace
@@ -189,17 +189,22 @@ OPTIONS
 	i.e., -F "" is not allowed.
 
 	The brstack output includes branch related information with raw addresses using the
-	/v/v/v/v/ syntax in the following order:
+	/v/v/v/v/cycles syntax in the following order:
 	FROM: branch source instruction
 	TO  : branch target instruction
         M/P/-: M=branch target mispredicted or branch direction was mispredicted, P=target predicted or direction predicted, -=not supported
 	X/- : X=branch inside a transactional region, -=not in transaction region or not supported
 	A/- : A=TSX abort entry, -=not aborted region or not supported
+	cycles
 
 	The brstacksym is identical to brstack, except that the FROM and TO addresses are printed in a symbolic form if possible.
 
 	When asm is specified the assembler instruction of each sample is printed in disassembled form.
 
+	When brstackasm is specified the full assembler sequences of branch sequences for each sample
+	is printed. This is the full execution path leading to the sample. This is only supported when the
+	sample was recorded with perf record -b or -j any.
+
 -k::
 --vmlinux=<file>::
         vmlinux pathname
@@ -301,6 +306,10 @@ include::itrace.txt[]
 	stop time is not given (i.e, time string is 'x.y,') then analysis goes
 	to end of file.
 
+--max-blocks::
+	Set the maximum number of program blocks to print with brstackasm for
+	each sample.
+
 SEE ALSO
 --------
 linkperf:perf-record[1], linkperf:perf-script-perl[1],
tools/perf/builtin-script.c +267 -0

309 lines not on this page · raw message

Signature · 1 line
2.9.3
#9 lore raw

Re: [PATCH 5/5] perf, tools, script: Add brstackasm output for branch stacks

Em Wed, Jan 18, 2017 at 05:41:50PM -0800, Andi Kleen escreveu:

tools/perf/builtin-script.c static void print_sample_brstacksym(struct perf_sample *sample, · line 638

Quoted 9 lines
tools/perf/builtin-script.c +9 -0
+	memset(&al, 0, sizeof(al));
+	if (end - start > MAXBB - MAXINSN) {
+		if (last)
+			printf("\tbrstack does not reach to final jump (%" PRIx64 "-%" PRIx64 ")\n",
+					start, end);
+		else
+			printf("\tblock %" PRIx64 "-%" PRIx64 " (%ld) too long to dump\n",
+					start, end, end - start);
+		return 0;

Those are multiline if/else blocks, one has to use {}, additionally (end
- start) should be, just like the other bits PRI, fixing...

This actually broke the build in some distros, like fedora:24 crossbuilding to ARC-uClibc.

- Arnaldo

#10 lore raw
In reply to Andi Kleen
Quoted 15 lines · Andi Kleen

A native disassembler in perf is very useful, in particular with perf script to trace instruction streams, but also for other analysis. Previously I attempted to do this using the udis86 library, but that was rejected because:
- udis86 was not maintained anymore and lacking recent instructions
- udis86 is dynamically linked and gives a runtime dependency.
Doing this needs a full disassembler, not just a decoder, so the existing instruction decoder cannot be used without major changes.

This patchkit addresses these issues. Intel recently released an open source version of the XED disassembler library, which is used in many other Intel software. It is very well maintained, uptodate, and supports static linking, so there is no runtime dependency. This version adds XED support to perf, and uses it to implement assembler output in perf script. It also fixes a range of issues in the previous version, see the individual change logs.

Quoted 2 lines

Available in
git://git.kernel.org/pub/scm/linux/kernel/git/ak/linux-misc.git perf/xed-6

i dont see that...

[jolsa@krava perf]$ git branch -r | grep xed-
ak/perf/xed-3
ak/perf/xed-4

jirka

Quoted 7 lines

v1: First post of XED version
v2: Change probing to not be default and support XED_DIR. Other cleanups based on review.
v3: Use FEATURE_FLAGS_BASIC for probing

-Andi

author
#11 lore raw
Quoted 4 lines

[jolsa@krava perf]$ git branch -r | grep xed-
ak/perf/xed-3
ak/perf/xed-4

Pushed.

-Andi

Acked-by
#12 lore raw
In reply to Andi Kleen
Quoted 15 lines · Andi Kleen

A native disassembler in perf is very useful, in particular with perf script to trace instruction streams, but also for other analysis. Previously I attempted to do this using the udis86 library, but that was rejected because:
- udis86 was not maintained anymore and lacking recent instructions
- udis86 is dynamically linked and gives a runtime dependency.
Doing this needs a full disassembler, not just a decoder, so the existing instruction decoder cannot be used without major changes.

This patchkit addresses these issues. Intel recently released an open source version of the XED disassembler library, which is used in many other Intel software. It is very well maintained, uptodate, and supports static linking, so there is no runtime dependency. This version adds XED support to perf, and uses it to implement assembler output in perf script. It also fixes a range of issues in the previous version, see the individual change logs.

Quoted 2 lines

Available in
git://git.kernel.org/pub/scm/linux/kernel/git/ak/linux-misc.git perf/xed-6

the perf/xed-6 branch looks good to me..
not sure if that matches what was posted ;-)

thanks,
jirka