linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Arnaldo Carvalho de Melo <acme@kernel.org>
To: Ingo Molnar <mingo@kernel.org>
Cc: linux-kernel@vger.kernel.org, Wang Nan <wangnan0@huawei.com>,
	Brendan Gregg <brendan.d.gregg@gmail.com>,
	Jiri Olsa <jolsa@kernel.org>, Li Zefan <lizefan@huawei.com>,
	Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>,
	Namhyung Kim <namhyung@kernel.org>,
	pi3orama@163.com, Arnaldo Carvalho de Melo <acme@redhat.com>
Subject: [PATCH 13/31] perf script: Print bpf-output events in 'perf script'
Date: Wed, 24 Feb 2016 22:58:08 -0300	[thread overview]
Message-ID: <1456365506-5492-14-git-send-email-acme@kernel.org> (raw)
In-Reply-To: <1456365506-5492-1-git-send-email-acme@kernel.org>

From: Wang Nan <wangnan0@huawei.com>

This patch allows 'perf script' output messages from BPF program.  For
example, use test_bpf_output_3.c at the end of this commit message,

  # ./perf record -e bpf-output/no-inherit,name=evt/ \
                 -e ./test_bpf_output_3.c/map:channel.event=evt/ \
                 usleep 100000

  # ./perf script
          usleep  4882 21384.532523:                       evt:  ffffffff810e97d1 sys_nanosleep ([kernel.kallsyms])
      BPF output: 0000: 52 61 69 73 65 20 61 20  Raise a
                  0008: 42 50 46 20 65 76 65 6e  BPF even
                  0010: 74 21 00 00              t!..
      BPF string: "Raise a BPF event!"

          usleep  4882 21384.632606:                       evt:  ffffffff8105c609 kretprobe_trampoline_holder ([kernel.kallsyms
      BPF output: 0000: 52 61 69 73 65 20 61 20  Raise a
                  0008: 42 50 46 20 65 76 65 6e  BPF even
                  0010: 74 21 00 00              t!..
      BPF string: "Raise a BPF event!"

Two samples from BPF output are printed by both binary and string
format.

If BPF program output something unprintable, string format is
suppressed.

  /************************ BEGIN **************************/
  #include <uapi/linux/bpf.h>
  struct bpf_map_def {
         unsigned int type;
         unsigned int key_size;
         unsigned int value_size;
         unsigned int max_entries;
  };
  #define SEC(NAME) __attribute__((section(NAME), used))
  static u64 (*ktime_get_ns)(void) =
         (void *)BPF_FUNC_ktime_get_ns;
  static int (*trace_printk)(const char *fmt, int fmt_size, ...) =
         (void *)BPF_FUNC_trace_printk;
  static int (*get_smp_processor_id)(void) =
         (void *)BPF_FUNC_get_smp_processor_id;
  static int (*perf_event_output)(void *, struct bpf_map_def *, int, void *, unsigned long) =
         (void *)BPF_FUNC_perf_event_output;

  struct bpf_map_def SEC("maps") channel = {
         .type = BPF_MAP_TYPE_PERF_EVENT_ARRAY,
         .key_size = sizeof(int),
         .value_size = sizeof(u32),
         .max_entries = __NR_CPUS__,
  };

  static inline int __attribute__((always_inline))
  func(void *ctx, int type)
  {
         char output_str[] = "Raise a BPF event!";

         perf_event_output(ctx, &channel, get_smp_processor_id(),
                           &output_str, sizeof(output_str));
         return 0;
  }
  SEC("func_begin=sys_nanosleep")
  int func_begin(void *ctx) {return func(ctx, 1);}
  SEC("func_end=sys_nanosleep%return")
  int func_end(void *ctx) { return func(ctx, 2);}
  char _license[] SEC("license") = "GPL";
  int _version SEC("version") = LINUX_VERSION_CODE;
  /************************* END ***************************/

Signed-off-by: Wang Nan <wangnan0@huawei.com>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Brendan Gregg <brendan.d.gregg@gmail.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Li Zefan <lizefan@huawei.com>
Cc: Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: pi3orama@163.com
Link: http://lkml.kernel.org/r/1456312845-111583-3-git-send-email-wangnan0@huawei.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
---
 tools/perf/builtin-script.c | 93 ++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 88 insertions(+), 5 deletions(-)

diff --git a/tools/perf/builtin-script.c b/tools/perf/builtin-script.c
index 8ff5ff0fe38c..ec4fbd410a4b 100644
--- a/tools/perf/builtin-script.c
+++ b/tools/perf/builtin-script.c
@@ -61,6 +61,7 @@ enum perf_output_field {
 	PERF_OUTPUT_BRSTACKSYM	    = 1U << 16,
 	PERF_OUTPUT_DATA_SRC	    = 1U << 17,
 	PERF_OUTPUT_WEIGHT	    = 1U << 18,
+	PERF_OUTPUT_BPF_OUTPUT	    = 1U << 19,
 };
 
 struct output_option {
@@ -86,6 +87,7 @@ struct output_option {
 	{.str = "brstacksym", .field = PERF_OUTPUT_BRSTACKSYM},
 	{.str = "data_src", .field = PERF_OUTPUT_DATA_SRC},
 	{.str = "weight",   .field = PERF_OUTPUT_WEIGHT},
+	{.str = "bpf-output",   .field = PERF_OUTPUT_BPF_OUTPUT},
 };
 
 /* default set to maintain compatibility with current format */
@@ -106,7 +108,7 @@ static struct {
 			      PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
 			      PERF_OUTPUT_PERIOD,
 
-		.invalid_fields = PERF_OUTPUT_TRACE,
+		.invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
 	},
 
 	[PERF_TYPE_SOFTWARE] = {
@@ -116,7 +118,7 @@ static struct {
 			      PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
 			      PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
 			      PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
-			      PERF_OUTPUT_PERIOD,
+			      PERF_OUTPUT_PERIOD | PERF_OUTPUT_BPF_OUTPUT,
 
 		.invalid_fields = PERF_OUTPUT_TRACE,
 	},
@@ -126,7 +128,7 @@ static struct {
 
 		.fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
 				  PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
-				  PERF_OUTPUT_EVNAME | PERF_OUTPUT_TRACE,
+				  PERF_OUTPUT_EVNAME | PERF_OUTPUT_TRACE
 	},
 
 	[PERF_TYPE_RAW] = {
@@ -139,7 +141,7 @@ static struct {
 			      PERF_OUTPUT_PERIOD |  PERF_OUTPUT_ADDR |
 			      PERF_OUTPUT_DATA_SRC | PERF_OUTPUT_WEIGHT,
 
-		.invalid_fields = PERF_OUTPUT_TRACE,
+		.invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
 	},
 
 	[PERF_TYPE_BREAKPOINT] = {
@@ -151,7 +153,7 @@ static struct {
 			      PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
 			      PERF_OUTPUT_PERIOD,
 
-		.invalid_fields = PERF_OUTPUT_TRACE,
+		.invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
 	},
 };
 
@@ -624,6 +626,84 @@ static void print_sample_flags(u32 flags)
 	printf("  %-4s ", str);
 }
 
+struct printer_data {
+	int line_no;
+	bool hit_nul;
+	bool is_printable;
+};
+
+static void
+print_sample_bpf_output_printer(enum binary_printer_ops op,
+				unsigned int val,
+				void *extra)
+{
+	unsigned char ch = (unsigned char)val;
+	struct printer_data *printer_data = extra;
+
+	switch (op) {
+	case BINARY_PRINT_DATA_BEGIN:
+		printf("\n");
+		break;
+	case BINARY_PRINT_LINE_BEGIN:
+		printf("%17s", !printer_data->line_no ? "BPF output:" :
+						        "           ");
+		break;
+	case BINARY_PRINT_ADDR:
+		printf(" %04x:", val);
+		break;
+	case BINARY_PRINT_NUM_DATA:
+		printf(" %02x", val);
+		break;
+	case BINARY_PRINT_NUM_PAD:
+		printf("   ");
+		break;
+	case BINARY_PRINT_SEP:
+		printf("  ");
+		break;
+	case BINARY_PRINT_CHAR_DATA:
+		if (printer_data->hit_nul && ch)
+			printer_data->is_printable = false;
+
+		if (!isprint(ch)) {
+			printf("%c", '.');
+
+			if (!printer_data->is_printable)
+				break;
+
+			if (ch == '\0')
+				printer_data->hit_nul = true;
+			else
+				printer_data->is_printable = false;
+		} else {
+			printf("%c", ch);
+		}
+		break;
+	case BINARY_PRINT_CHAR_PAD:
+		printf(" ");
+		break;
+	case BINARY_PRINT_LINE_END:
+		printf("\n");
+		printer_data->line_no++;
+		break;
+	case BINARY_PRINT_DATA_END:
+	default:
+		break;
+	}
+}
+
+static void print_sample_bpf_output(struct perf_sample *sample)
+{
+	unsigned int nr_bytes = sample->raw_size;
+	struct printer_data printer_data = {0, false, true};
+
+	print_binary(sample->raw_data, nr_bytes, 8,
+		     print_sample_bpf_output_printer, &printer_data);
+
+	if (printer_data.is_printable && printer_data.hit_nul)
+		printf("%17s \"%s\"\n", "BPF string:",
+		       (char *)(sample->raw_data));
+}
+
 struct perf_script {
 	struct perf_tool	tool;
 	struct perf_session	*session;
@@ -731,6 +811,9 @@ static void process_event(struct perf_script *script, union perf_event *event,
 	else if (PRINT_FIELD(BRSTACKSYM))
 		print_sample_brstacksym(event, sample, thread, attr);
 
+	if (perf_evsel__is_bpf_output(evsel) && PRINT_FIELD(BPF_OUTPUT))
+		print_sample_bpf_output(sample);
+
 	printf("\n");
 }
 
-- 
2.5.0

  parent reply	other threads:[~2016-02-25  2:02 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-02-25  1:57 [GIT PULL 00/31] perf/core improvements and fixes Arnaldo Carvalho de Melo
2016-02-25  1:57 ` [PATCH 01/31] perf mem record: Check for memory events support Arnaldo Carvalho de Melo
2016-02-25  1:57 ` [PATCH 02/31] perf mem: Introduce perf_mem_events__name function Arnaldo Carvalho de Melo
2016-02-25  1:57 ` [PATCH 03/31] perf tools: Introduce perf_mem__tlb_scnprintf function Arnaldo Carvalho de Melo
2016-02-25  1:57 ` [PATCH 04/31] perf tools: Introduce perf_mem__lvl_scnprintf function Arnaldo Carvalho de Melo
2016-02-25  1:58 ` [PATCH 05/31] perf tools: Introduce perf_mem__snp_scnprintf function Arnaldo Carvalho de Melo
2016-02-25  1:58 ` [PATCH 06/31] perf tools: Introduce perf_mem__lck_scnprintf function Arnaldo Carvalho de Melo
2016-02-25  1:58 ` [PATCH 07/31] perf tools: Change perf_mem__tlb_scnprintf to return nb of displayed bytes Arnaldo Carvalho de Melo
2016-02-25  1:58 ` [PATCH 08/31] perf tools: Change perf_mem__lvl_scnprintf " Arnaldo Carvalho de Melo
2016-02-25  1:58 ` [PATCH 09/31] perf tools: Change perf_mem__snp_scnprintf " Arnaldo Carvalho de Melo
2016-02-25  1:58 ` [PATCH 10/31] perf tools: Change perf_mem__lck_scnprintf " Arnaldo Carvalho de Melo
2016-02-25  1:58 ` [PATCH 11/31] perf script: Display data_src values Arnaldo Carvalho de Melo
2016-02-25  1:58 ` [PATCH 12/31] perf tools: Make binary data printer code in trace_event public available Arnaldo Carvalho de Melo
2016-02-25  1:58 ` Arnaldo Carvalho de Melo [this message]
2016-02-25  1:58 ` [PATCH 14/31] perf tools: Add helper functions for some sort keys Arnaldo Carvalho de Melo
2016-02-25  1:58 ` [PATCH 15/31] perf hists: Basic support of hierarchical report view Arnaldo Carvalho de Melo
2016-02-25  1:58 ` [PATCH 16/31] perf hists: Resort hist entries with hierarchy Arnaldo Carvalho de Melo
2016-02-25  1:58 ` [PATCH 17/31] perf hists: Add helper functions for hierarchy mode Arnaldo Carvalho de Melo
2016-02-25  1:58 ` [PATCH 18/31] perf hists: Introduce hist_entry__filter() Arnaldo Carvalho de Melo
2016-02-25  1:58 ` [PATCH 19/31] perf hists: Support filtering in hierarchy mode Arnaldo Carvalho de Melo
2016-02-25  1:58 ` [PATCH 20/31] perf hists: Resort after filtering hierarchy Arnaldo Carvalho de Melo
2016-02-25  1:58 ` [PATCH 21/31] perf hists: Count number of sort keys Arnaldo Carvalho de Melo
2016-02-25  1:58 ` [PATCH 22/31] perf ui/stdio: Implement hierarchy output mode Arnaldo Carvalho de Melo
2016-02-25  1:58 ` [PATCH 23/31] perf ui/stdio: Align column header for hierarchy output Arnaldo Carvalho de Melo
2016-02-25  1:58 ` [PATCH 24/31] perf hists browser: Count number of hierarchy entries Arnaldo Carvalho de Melo
2016-02-25  1:58 ` [PATCH 25/31] perf hists browser: Support collapsing/expanding whole entries in hierarchy Arnaldo Carvalho de Melo
2016-02-25  1:58 ` [PATCH 26/31] perf hists browser: Implement hierarchy output Arnaldo Carvalho de Melo
2016-02-25  1:58 ` [PATCH 27/31] perf hists browser: Align column header in hierarchy mode Arnaldo Carvalho de Melo
2016-02-25  1:58 ` [PATCH 28/31] perf ui/gtk: Implement hierarchy output mode Arnaldo Carvalho de Melo
2016-02-25  1:58 ` [PATCH 29/31] perf report: Add --hierarchy option Arnaldo Carvalho de Melo
2016-02-25  1:58 ` [PATCH 30/31] perf hists: Support decaying in hierarchy mode Arnaldo Carvalho de Melo
2016-02-25  1:58 ` [PATCH 31/31] perf top: Add --hierarchy option Arnaldo Carvalho de Melo
2016-02-25  7:22 ` [GIT PULL 00/31] perf/core improvements and fixes Ingo Molnar

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1456365506-5492-14-git-send-email-acme@kernel.org \
    --to=acme@kernel.org \
    --cc=acme@redhat.com \
    --cc=brendan.d.gregg@gmail.com \
    --cc=jolsa@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lizefan@huawei.com \
    --cc=masami.hiramatsu.pt@hitachi.com \
    --cc=mingo@kernel.org \
    --cc=namhyung@kernel.org \
    --cc=pi3orama@163.com \
    --cc=wangnan0@huawei.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).