From: Eric Yan <eric.yan@oppo.com>
To: <ast@kernel.org>, <daniel@iogearbox.net>, <andrii@kernel.org>,
<martin.lau@linux.dev>, <yonghong.song@linux.dev>,
<song@kernel.org>, <john.fastabend@gmail.com>,
<kpsingh@kernel.org>, <sdf@fomichev.me>, <haoluo@google.com>,
<jolsa@kernel.org>
Cc: <bpf@vger.kernel.org>, <linux-kernel@vger.kernel.org>,
<eric.yan@oppo.com>
Subject: [PATCH] Add BPF Kernel Function bpf_ptrace_vprintk
Date: Wed, 25 Sep 2024 18:02:54 +0800 [thread overview]
Message-ID: <20240925100254.436-1-eric.yan@oppo.com> (raw)
add a kfunc 'bpf_ptrace_vprintk' printing bpf msg with trace_marker
format requirement so that these msgs can be retrieved by android
perfetto by default and well represented in perfetto UI.
[testing prog]
const volatile bool ptrace_enabled = true;
extern int bpf_ptrace_vprintk(char *fmt, u32 fmt_size, const void *args, u32 args__sz) __ksym;
({ \
if (!ptrace_enabled) { \
bpf_printk(fmt, __VA_ARGS__); \
} else { \
char __fmt[] = fmt; \
_Pragma("GCC diagnostic push") \
_Pragma("GCC diagnostic ignored \"-Wint-conversion\"") \
u64 __params[] = { __VA_ARGS__ }; \
_Pragma("GCC diagnostic pop") \
bpf_ptrace_vprintk(__fmt, sizeof(__fmt), __params, sizeof(__params)); \
} \
})
SEC("perf_event")
int do_sample(struct bpf_perf_event_data *ctx)
{
u64 ip = PT_REGS_IP(&ctx->regs);
u64 id = bpf_get_current_pid_tgid();
s32 pid = id >> 32;
s32 tid = id;
debug_printk("N|%d|BPRF-%d|BPRF:%llx", pid, tid, ip);
return 0;
}
[output]:
app-3151 [000] d.h1. 6059.904239: tracing_mark_write: N|2491|BPRF-3151|BPRF:58750d0eec
Signed-off-by: Eric Yan <eric.yan@oppo.com>
---
kernel/bpf/helpers.c | 34 ++++++++++++++++++++++++++++++++++
1 file changed, 34 insertions(+)
diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c
index 1a43d06eab28..e3d17d3b17c5 100644
--- a/kernel/bpf/helpers.c
+++ b/kernel/bpf/helpers.c
@@ -2521,6 +2521,39 @@ __bpf_kfunc struct task_struct *bpf_task_from_pid(s32 pid)
return p;
}
+static noinline void tracing_mark_write(char *buf)
+{
+ trace_printk(buf);
+}
+
+/**
+ * same as bpf_trace_vprintk, except for a trace_marker format requirement
+ */
+__bpf_kfunc int bpf_ptrace_vprintk(char *fmt, u32 fmt_size, const void *args, u32 args__sz)
+{
+ struct bpf_bprintf_data data = {
+ .get_bin_args = true,
+ .get_buf = true,
+ };
+ int ret, num_args;
+
+ if (args__sz & 7 || args__sz > MAX_BPRINTF_VARARGS * 8 || (args__sz && !args))
+ return -EINVAL;
+ num_args = args__sz / 8;
+
+ ret = bpf_bprintf_prepare(fmt, fmt_size, args, num_args, &data);
+ if (ret < 0)
+ return ret;
+
+ ret = bstr_printf(data.buf, MAX_BPRINTF_BUF, fmt, data.bin_args);
+
+ tracing_mark_write(data.buf);
+
+ bpf_bprintf_cleanup(&data);
+
+ return ret;
+}
+
/**
* bpf_dynptr_slice() - Obtain a read-only pointer to the dynptr data.
* @p: The dynptr whose data slice to retrieve
@@ -3090,6 +3123,7 @@ BTF_ID_FLAGS(func, bpf_iter_bits_new, KF_ITER_NEW)
BTF_ID_FLAGS(func, bpf_iter_bits_next, KF_ITER_NEXT | KF_RET_NULL)
BTF_ID_FLAGS(func, bpf_iter_bits_destroy, KF_ITER_DESTROY)
BTF_ID_FLAGS(func, bpf_copy_from_user_str, KF_SLEEPABLE)
+BTF_ID_FLAGS(func, bpf_ptrace_vprintk)
BTF_KFUNCS_END(common_btf_ids)
static const struct btf_kfunc_id_set common_kfunc_set = {
--
2.34.1
next reply other threads:[~2024-09-25 10:03 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-09-25 10:02 Eric Yan [this message]
2024-09-26 3:34 ` [PATCH] Add BPF Kernel Function bpf_ptrace_vprintk kernel test robot
2024-09-26 7:27 ` [PATCH v2] " Eric Yan
2024-09-29 17:10 ` Alexei Starovoitov
2024-09-30 8:29 ` 答复: " 燕青洲(Eric Yan)
2024-09-30 23:17 ` Andrii Nakryiko
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=20240925100254.436-1-eric.yan@oppo.com \
--to=eric.yan@oppo.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=haoluo@google.com \
--cc=john.fastabend@gmail.com \
--cc=jolsa@kernel.org \
--cc=kpsingh@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=martin.lau@linux.dev \
--cc=sdf@fomichev.me \
--cc=song@kernel.org \
--cc=yonghong.song@linux.dev \
/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