[PATCH V6 07/17] perf tools: Add Intel PT decoder

2015-05-29 Thread Adrian Hunter
Add support for decoding an Intel Processor Trace.

Intel PT trace data must be 'decoded' which involves walking
the object code and matching the trace data packets.

The decoder requests a buffer of binary data via a get_trace()
call-back, which it decodes using instruction information which
it gets via another call-back walk_insn().

Signed-off-by: Adrian Hunter 
---
 tools/perf/util/intel-pt-decoder/Build |2 +-
 .../perf/util/intel-pt-decoder/intel-pt-decoder.c  | 1759 
 .../perf/util/intel-pt-decoder/intel-pt-decoder.h  |  102 ++
 3 files changed, 1862 insertions(+), 1 deletion(-)
 create mode 100644 tools/perf/util/intel-pt-decoder/intel-pt-decoder.c
 create mode 100644 tools/perf/util/intel-pt-decoder/intel-pt-decoder.h

diff --git a/tools/perf/util/intel-pt-decoder/Build 
b/tools/perf/util/intel-pt-decoder/Build
index 587321a..fa12eac 100644
--- a/tools/perf/util/intel-pt-decoder/Build
+++ b/tools/perf/util/intel-pt-decoder/Build
@@ -1,4 +1,4 @@
-libperf-$(CONFIG_AUXTRACE) += intel-pt-pkt-decoder.o intel-pt-insn-decoder.o 
intel-pt-log.o
+libperf-$(CONFIG_AUXTRACE) += intel-pt-pkt-decoder.o intel-pt-insn-decoder.o 
intel-pt-log.o intel-pt-decoder.o
 
 inat_tables_script = ../../arch/x86/tools/gen-insn-attr-x86.awk
 inat_tables_maps = ../../arch/x86/lib/x86-opcode-map.txt
diff --git a/tools/perf/util/intel-pt-decoder/intel-pt-decoder.c 
b/tools/perf/util/intel-pt-decoder/intel-pt-decoder.c
new file mode 100644
index 000..748a7a0
--- /dev/null
+++ b/tools/perf/util/intel-pt-decoder/intel-pt-decoder.c
@@ -0,0 +1,1759 @@
+/*
+ * intel_pt_decoder.c: Intel Processor Trace support
+ * Copyright (c) 2013-2014, Intel Corporation.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU General Public License,
+ * version 2, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
+ * more details.
+ *
+ */
+
+#ifndef _GNU_SOURCE
+#define _GNU_SOURCE
+#endif
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#include "../cache.h"
+#include "../util.h"
+
+#include "intel-pt-insn-decoder.h"
+#include "intel-pt-pkt-decoder.h"
+#include "intel-pt-decoder.h"
+#include "intel-pt-log.h"
+
+#define INTEL_PT_BLK_SIZE 1024
+
+#define BIT63 (((uint64_t)1 << 63))
+
+#define INTEL_PT_RETURN 1
+
+struct intel_pt_blk {
+   struct intel_pt_blk *prev;
+   uint64_t ip[INTEL_PT_BLK_SIZE];
+};
+
+struct intel_pt_stack {
+   struct intel_pt_blk *blk;
+   struct intel_pt_blk *spare;
+   int pos;
+};
+
+enum intel_pt_pkt_state {
+   INTEL_PT_STATE_NO_PSB,
+   INTEL_PT_STATE_NO_IP,
+   INTEL_PT_STATE_ERR_RESYNC,
+   INTEL_PT_STATE_IN_SYNC,
+   INTEL_PT_STATE_TNT,
+   INTEL_PT_STATE_TIP,
+   INTEL_PT_STATE_TIP_PGD,
+   INTEL_PT_STATE_FUP,
+   INTEL_PT_STATE_FUP_NO_TIP,
+};
+
+#ifdef INTEL_PT_STRICT
+#define INTEL_PT_STATE_ERR1INTEL_PT_STATE_NO_PSB
+#define INTEL_PT_STATE_ERR2INTEL_PT_STATE_NO_PSB
+#define INTEL_PT_STATE_ERR3INTEL_PT_STATE_NO_PSB
+#define INTEL_PT_STATE_ERR4INTEL_PT_STATE_NO_PSB
+#else
+#define INTEL_PT_STATE_ERR1(decoder->pkt_state)
+#define INTEL_PT_STATE_ERR2INTEL_PT_STATE_NO_IP
+#define INTEL_PT_STATE_ERR3INTEL_PT_STATE_ERR_RESYNC
+#define INTEL_PT_STATE_ERR4INTEL_PT_STATE_IN_SYNC
+#endif
+
+struct intel_pt_decoder {
+   int (*get_trace)(struct intel_pt_buffer *buffer, void *data);
+   int (*walk_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, void *data);
+   void *data;
+   struct intel_pt_state state;
+   const unsigned char *buf;
+   size_t len;
+   bool return_compression;
+   bool pge;
+   uint64_t pos;
+   uint64_t last_ip;
+   uint64_t ip;
+   uint64_t cr3;
+   uint64_t timestamp;
+   uint64_t tsc_timestamp;
+   uint64_t ref_timestamp;
+   uint64_t ret_addr;
+   struct intel_pt_stack stack;
+   enum intel_pt_pkt_state pkt_state;
+   struct intel_pt_pkt packet;
+   struct intel_pt_pkt tnt;
+   int pkt_step;
+   int pkt_len;
+   unsigned int cbr;
+   int exec_mode;
+   unsigned int insn_bytes;
+   uint64_t sign_bit;
+   uint64_t sign_bits;
+   uint64_t period;
+   enum intel_pt_period_type period_type;
+   uint64_t period_insn_cnt;
+   uint64_t period_mask;
+   uint64_t period_ticks;
+   uint64_t last_masked_timestamp;
+   bool continuous_period;
+   bool overflow;
+   bool set_fup_tx_flags;
+   unsigned int fup_tx_flags;
+   unsigned int tx_flags;
+   uint64_t timestamp_insn_cnt;
+   const unsigned char 

[PATCH V6 07/17] perf tools: Add Intel PT decoder

2015-05-29 Thread Adrian Hunter
Add support for decoding an Intel Processor Trace.

Intel PT trace data must be 'decoded' which involves walking
the object code and matching the trace data packets.

The decoder requests a buffer of binary data via a get_trace()
call-back, which it decodes using instruction information which
it gets via another call-back walk_insn().

Signed-off-by: Adrian Hunter adrian.hun...@intel.com
---
 tools/perf/util/intel-pt-decoder/Build |2 +-
 .../perf/util/intel-pt-decoder/intel-pt-decoder.c  | 1759 
 .../perf/util/intel-pt-decoder/intel-pt-decoder.h  |  102 ++
 3 files changed, 1862 insertions(+), 1 deletion(-)
 create mode 100644 tools/perf/util/intel-pt-decoder/intel-pt-decoder.c
 create mode 100644 tools/perf/util/intel-pt-decoder/intel-pt-decoder.h

diff --git a/tools/perf/util/intel-pt-decoder/Build 
b/tools/perf/util/intel-pt-decoder/Build
index 587321a..fa12eac 100644
--- a/tools/perf/util/intel-pt-decoder/Build
+++ b/tools/perf/util/intel-pt-decoder/Build
@@ -1,4 +1,4 @@
-libperf-$(CONFIG_AUXTRACE) += intel-pt-pkt-decoder.o intel-pt-insn-decoder.o 
intel-pt-log.o
+libperf-$(CONFIG_AUXTRACE) += intel-pt-pkt-decoder.o intel-pt-insn-decoder.o 
intel-pt-log.o intel-pt-decoder.o
 
 inat_tables_script = ../../arch/x86/tools/gen-insn-attr-x86.awk
 inat_tables_maps = ../../arch/x86/lib/x86-opcode-map.txt
diff --git a/tools/perf/util/intel-pt-decoder/intel-pt-decoder.c 
b/tools/perf/util/intel-pt-decoder/intel-pt-decoder.c
new file mode 100644
index 000..748a7a0
--- /dev/null
+++ b/tools/perf/util/intel-pt-decoder/intel-pt-decoder.c
@@ -0,0 +1,1759 @@
+/*
+ * intel_pt_decoder.c: Intel Processor Trace support
+ * Copyright (c) 2013-2014, Intel Corporation.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU General Public License,
+ * version 2, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
+ * more details.
+ *
+ */
+
+#ifndef _GNU_SOURCE
+#define _GNU_SOURCE
+#endif
+#include stdlib.h
+#include stdbool.h
+#include string.h
+#include errno.h
+#include stdint.h
+#include inttypes.h
+
+#include ../cache.h
+#include ../util.h
+
+#include intel-pt-insn-decoder.h
+#include intel-pt-pkt-decoder.h
+#include intel-pt-decoder.h
+#include intel-pt-log.h
+
+#define INTEL_PT_BLK_SIZE 1024
+
+#define BIT63 (((uint64_t)1  63))
+
+#define INTEL_PT_RETURN 1
+
+struct intel_pt_blk {
+   struct intel_pt_blk *prev;
+   uint64_t ip[INTEL_PT_BLK_SIZE];
+};
+
+struct intel_pt_stack {
+   struct intel_pt_blk *blk;
+   struct intel_pt_blk *spare;
+   int pos;
+};
+
+enum intel_pt_pkt_state {
+   INTEL_PT_STATE_NO_PSB,
+   INTEL_PT_STATE_NO_IP,
+   INTEL_PT_STATE_ERR_RESYNC,
+   INTEL_PT_STATE_IN_SYNC,
+   INTEL_PT_STATE_TNT,
+   INTEL_PT_STATE_TIP,
+   INTEL_PT_STATE_TIP_PGD,
+   INTEL_PT_STATE_FUP,
+   INTEL_PT_STATE_FUP_NO_TIP,
+};
+
+#ifdef INTEL_PT_STRICT
+#define INTEL_PT_STATE_ERR1INTEL_PT_STATE_NO_PSB
+#define INTEL_PT_STATE_ERR2INTEL_PT_STATE_NO_PSB
+#define INTEL_PT_STATE_ERR3INTEL_PT_STATE_NO_PSB
+#define INTEL_PT_STATE_ERR4INTEL_PT_STATE_NO_PSB
+#else
+#define INTEL_PT_STATE_ERR1(decoder-pkt_state)
+#define INTEL_PT_STATE_ERR2INTEL_PT_STATE_NO_IP
+#define INTEL_PT_STATE_ERR3INTEL_PT_STATE_ERR_RESYNC
+#define INTEL_PT_STATE_ERR4INTEL_PT_STATE_IN_SYNC
+#endif
+
+struct intel_pt_decoder {
+   int (*get_trace)(struct intel_pt_buffer *buffer, void *data);
+   int (*walk_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, void *data);
+   void *data;
+   struct intel_pt_state state;
+   const unsigned char *buf;
+   size_t len;
+   bool return_compression;
+   bool pge;
+   uint64_t pos;
+   uint64_t last_ip;
+   uint64_t ip;
+   uint64_t cr3;
+   uint64_t timestamp;
+   uint64_t tsc_timestamp;
+   uint64_t ref_timestamp;
+   uint64_t ret_addr;
+   struct intel_pt_stack stack;
+   enum intel_pt_pkt_state pkt_state;
+   struct intel_pt_pkt packet;
+   struct intel_pt_pkt tnt;
+   int pkt_step;
+   int pkt_len;
+   unsigned int cbr;
+   int exec_mode;
+   unsigned int insn_bytes;
+   uint64_t sign_bit;
+   uint64_t sign_bits;
+   uint64_t period;
+   enum intel_pt_period_type period_type;
+   uint64_t period_insn_cnt;
+   uint64_t period_mask;
+   uint64_t period_ticks;
+   uint64_t last_masked_timestamp;
+   bool continuous_period;
+   bool overflow;
+   bool set_fup_tx_flags;
+   unsigned int fup_tx_flags;
+   unsigned int tx_flags;
+