All of lore.kernel.org
 help / color / mirror / Atom feed
From: He Kuang <hekuang@huawei.com>
To: <ast@plumgrid.com>, <davem@davemloft.net>, <acme@kernel.org>,
	<mingo@redhat.com>, <a.p.zijlstra@chello.nl>,
	<masami.hiramatsu.pt@hitachi.com>, <jolsa@kernel.org>
Cc: <wangnan0@huawei.com>, <lizefan@kernel.org>,
	<linux-kernel@vger.kernel.org>, <pi3orama@163.com>
Subject: [RFC PATCH 4/6] perf bpf: Convert arglist to bpf prologue
Date: Tue, 5 May 2015 10:10:10 +0000	[thread overview]
Message-ID: <1430820612-84443-5-git-send-email-hekuang@huawei.com> (raw)
In-Reply-To: <1430820612-84443-1-git-send-email-hekuang@huawei.com>

When all arguments in bpf config section are collected in register and
offset form, this patch will fetch them from bpf context register and
place them as bpf input parameters.

Bpf prologue is generated as the following steps:
1. alloc dst address in stack -> r1
2. set size -> r2
3. fetch base register and offset -> r3
4. call BPF_FUNC_probe_read
5. loop 1
6. save intermediate result and process next arg
7. restore intermediate result to arg2~5

Signed-off-by: He Kuang <hekuang@huawei.com>
---
 tools/perf/util/probe-event.c | 94 +++++++++++++++++++++++++++++++++++++++++++
 tools/perf/util/probe-event.h |  6 +++
 2 files changed, 100 insertions(+)

diff --git a/tools/perf/util/probe-event.c b/tools/perf/util/probe-event.c
index d05b77c..c307cd7 100644
--- a/tools/perf/util/probe-event.c
+++ b/tools/perf/util/probe-event.c
@@ -46,6 +46,7 @@
 #include "probe-event.h"
 #include "probe-finder.h"
 #include "session.h"
+#include "bpf-loader.h"
 
 #define MAX_CMDLEN 256
 #define PERFPROBE_GROUP "probe"
@@ -1684,6 +1685,99 @@ static int synthesize_probe_trace_arg(struct probe_trace_arg *arg,
 	return buf - tmp;
 }
 
+#define BPF_STRBUF_ADD(buf, statement)		\
+	strbuf_add(buf,				\
+		&statement,			\
+		sizeof(struct bpf_insn))
+
+#define BPF_STRBUF_ADD_BUF(statement) BPF_STRBUF_ADD(buf, statement)
+
+int synthesize_probe_trace_arg_bpf_begin(struct strbuf *buf)
+{
+	/* save arg1 to ctx */
+	BPF_STRBUF_ADD_BUF(BPF_MOV64_REG(BPF_REG_CTX, BPF_REG_ARG1));
+	return 0;
+}
+
+int synthesize_probe_trace_arg_bpf_end(struct probe_trace_arg *arg,
+				struct strbuf *buf, int num)
+{
+	int i;
+
+	for (i = 0; i < num; i++) {
+		/* restore r7~10 to arg2~5*/
+		BPF_STRBUF_ADD_BUF(
+			BPF_MOV64_REG(BPF_REG_ARG2 + i, BPF_REG_7 + i));
+	}
+
+	/* assign result to tvar */
+	arg->insns = buf->buf;
+	arg->insns_cnt = (int)(buf->len/sizeof(struct bpf_insn));
+
+	return 0;
+}
+
+int synthesize_probe_trace_arg_bpf(struct probe_trace_arg *arg,
+				struct strbuf *buf, int index)
+{
+	struct probe_trace_arg_ref *ref = arg->ref;
+	int depth = 0;
+	int size = arg->reg_size;
+
+	if (size < 0)
+		return -EINVAL;
+
+	/* Special case: @XXX */
+	if (arg->value[0] == '@' && arg->ref)
+			ref = ref->next;
+
+	/* Print argument value */
+	if (arg->value[0] == '@' && arg->ref)
+		/* TODO parse other arguments */
+		pr_debug("%d%+ld", arg->regn,
+				 arg->ref->offset);
+	else {
+		/* load ctx to r3 */
+		BPF_STRBUF_ADD_BUF(
+			BPF_LDX_MEM(BPF_DW, BPF_REG_3, BPF_REG_CTX,
+				arg->regn * size));
+	}
+
+	/* parse and traslate to bpf bytecode */
+	while (ref) {
+		/* r3 offset */
+		BPF_STRBUF_ADD_BUF(
+			BPF_ALU64_IMM(BPF_ADD, BPF_REG_3, ref->offset));
+
+		/* clear stack space */
+		BPF_STRBUF_ADD_BUF(BPF_ALU64_IMM(BPF_MOV, BPF_REG_1, 0));
+		BPF_STRBUF_ADD_BUF(
+			BPF_STX_MEM(BPF_DW, BPF_REG_FP, BPF_REG_1, -size));
+
+		/* set stack space to r1 */
+		BPF_STRBUF_ADD_BUF(BPF_MOV64_REG(BPF_REG_1, BPF_REG_FP));
+		BPF_STRBUF_ADD_BUF(BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -size));
+
+		/* r2 for size */
+		BPF_STRBUF_ADD_BUF(BPF_ALU64_IMM(BPF_MOV, BPF_REG_2, size));
+
+		/* call BPF_FUNC_probe_read */
+		BPF_STRBUF_ADD_BUF(BPF_EMIT_CALL(BPF_FUNC_probe_read));
+
+		/* result in stack, move to r3 */
+		BPF_STRBUF_ADD_BUF(
+			BPF_LDX_MEM(BPF_DW, BPF_REG_3, BPF_REG_FP, -size));
+
+		depth++;
+		ref = ref->next;
+	};
+
+	/* store intermediate results to r7~10 */
+	BPF_STRBUF_ADD_BUF(BPF_MOV64_REG(BPF_REG_7 + index, BPF_REG_3));
+
+	return 0;
+}
+
 char *synthesize_probe_trace_command(struct probe_trace_event *tev)
 {
 	struct probe_trace_point *tp = &tev->point;
diff --git a/tools/perf/util/probe-event.h b/tools/perf/util/probe-event.h
index 842d6c4..3b6c284 100644
--- a/tools/perf/util/probe-event.h
+++ b/tools/perf/util/probe-event.h
@@ -109,6 +109,12 @@ extern char *synthesize_perf_probe_command(struct perf_probe_event *pev);
 extern char *synthesize_probe_trace_command(struct probe_trace_event *tev);
 extern int synthesize_perf_probe_arg(struct perf_probe_arg *pa, char *buf,
 				     size_t len);
+/* generate bpf prologue from debuginfo */
+extern int synthesize_probe_trace_arg_bpf(struct probe_trace_arg *arg,
+					struct strbuf *buf, int index);
+extern int synthesize_probe_trace_arg_bpf_begin(struct strbuf *buf);
+extern int synthesize_probe_trace_arg_bpf_end(struct probe_trace_arg *arg,
+					struct strbuf *buf, int num);
 
 /* Check the perf_probe_event needs debuginfo */
 extern bool perf_probe_event_need_dwarf(struct perf_probe_event *pev);
-- 
1.8.5.2


  parent reply	other threads:[~2015-05-05 10:11 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-05-05 10:10 [RFC PATCH 0/6] perf bpf: Probing with local variable He Kuang
2015-05-05 10:10 ` [RFC PATCH 1/6] perf bpf: Add headers for generate bpf bytecode He Kuang
2015-05-05 22:38   ` Alexei Starovoitov
2015-05-05 10:10 ` [RFC PATCH 2/6] perf bpf: Add pt_regs convert table for x86 He Kuang
2015-05-05 22:44   ` Alexei Starovoitov
2015-05-08  8:02   ` Masami Hiramatsu
2015-05-05 10:10 ` [RFC PATCH 3/6] perf bpf: Save pt_regs info from debuginfo He Kuang
2015-05-05 10:10 ` He Kuang [this message]
2015-05-05 22:54   ` [RFC PATCH 4/6] perf bpf: Convert arglist to bpf prologue Alexei Starovoitov
2015-05-05 10:10 ` [RFC PATCH 5/6] perf bpf: Process debuginfo for generating " He Kuang
2015-05-05 10:10 ` [RFC PATCH 6/6] perf bpf: Generate bpf prologue for arguments He Kuang
2015-05-05 22:31 ` [RFC PATCH 0/6] perf bpf: Probing with local variable Alexei Starovoitov
2015-05-06  3:58   ` Wang Nan
2015-05-06  4:10     ` Alexei Starovoitov
2015-05-06  4:41       ` Wang Nan
2015-05-06  1:37 ` Masami Hiramatsu
2015-05-06  1:44   ` Arnaldo Carvalho de Melo

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=1430820612-84443-5-git-send-email-hekuang@huawei.com \
    --to=hekuang@huawei.com \
    --cc=a.p.zijlstra@chello.nl \
    --cc=acme@kernel.org \
    --cc=ast@plumgrid.com \
    --cc=davem@davemloft.net \
    --cc=jolsa@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lizefan@kernel.org \
    --cc=masami.hiramatsu.pt@hitachi.com \
    --cc=mingo@redhat.com \
    --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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.