bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Kumar Kartikeya Dwivedi <memxor@gmail.com>
To: bpf@vger.kernel.org
Cc: Alexei Starovoitov <ast@kernel.org>,
	Andrii Nakryiko <andrii@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>,
	Martin KaFai Lau <martin.lau@kernel.org>,
	Eduard Zingerman <eddyz87@gmail.com>,
	Emil Tsalapatis <emil@etsalapatis.com>,
	Barret Rhoden <brho@google.com>,
	Matt Bobrowski <mattbobrowski@google.com>,
	kkd@meta.com, kernel-team@meta.com
Subject: [PATCH bpf-next v3 03/12] bpf: Add function to extract program source info
Date: Mon, 23 Jun 2025 20:12:43 -0700	[thread overview]
Message-ID: <20250624031252.2966759-4-memxor@gmail.com> (raw)
In-Reply-To: <20250624031252.2966759-1-memxor@gmail.com>

Prepare a function for use in future patches that can extract the file
info, line info, and the source line number for a given BPF program
provided it's program counter.

Only the basename of the file path is provided, given it can be
excessively long in some cases.

This will be used in later patches to print source info to the BPF
stream.

Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
---
 include/linux/bpf.h |  3 +++
 kernel/bpf/core.c   | 47 +++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 50 insertions(+)

diff --git a/include/linux/bpf.h b/include/linux/bpf.h
index cdd726cfe622..f30697c72ba9 100644
--- a/include/linux/bpf.h
+++ b/include/linux/bpf.h
@@ -3667,4 +3667,7 @@ static inline bool bpf_is_subprog(const struct bpf_prog *prog)
 	return prog->aux->func_idx != 0;
 }
 
+int bpf_prog_get_file_line(struct bpf_prog *prog, unsigned long ip, const char **filep,
+			   const char **linep, int *nump);
+
 #endif /* _LINUX_BPF_H */
diff --git a/kernel/bpf/core.c b/kernel/bpf/core.c
index f0def24573ae..5c6e9fbb5508 100644
--- a/kernel/bpf/core.c
+++ b/kernel/bpf/core.c
@@ -3213,3 +3213,50 @@ EXPORT_SYMBOL(bpf_stats_enabled_key);
 
 EXPORT_TRACEPOINT_SYMBOL_GPL(xdp_exception);
 EXPORT_TRACEPOINT_SYMBOL_GPL(xdp_bulk_tx);
+
+#ifdef CONFIG_BPF_SYSCALL
+
+int bpf_prog_get_file_line(struct bpf_prog *prog, unsigned long ip, const char **filep,
+			   const char **linep, int *nump)
+{
+	int idx = -1, insn_start, insn_end, len;
+	struct bpf_line_info *linfo;
+	void **jited_linfo;
+	struct btf *btf;
+
+	btf = prog->aux->btf;
+	linfo = prog->aux->linfo;
+	jited_linfo = prog->aux->jited_linfo;
+
+	if (!btf || !linfo || !prog->aux->jited_linfo)
+		return -EINVAL;
+	len = prog->aux->func ? prog->aux->func[prog->aux->func_idx]->len : prog->len;
+
+	linfo = &prog->aux->linfo[prog->aux->linfo_idx];
+	jited_linfo = &prog->aux->jited_linfo[prog->aux->linfo_idx];
+
+	insn_start = linfo[0].insn_off;
+	insn_end = insn_start + len;
+
+	for (int i = 0; i < prog->aux->nr_linfo &&
+	     linfo[i].insn_off >= insn_start && linfo[i].insn_off < insn_end; i++) {
+		if (jited_linfo[i] >= (void *)ip)
+			break;
+		idx = i;
+	}
+
+	if (idx == -1)
+		return -ENOENT;
+
+	/* Get base component of the file path. */
+	*filep = btf_name_by_offset(btf, linfo[idx].file_name_off);
+	*filep = kbasename(*filep);
+	/* Obtain the source line, and strip whitespace in prefix. */
+	*linep = btf_name_by_offset(btf, linfo[idx].line_off);
+	while (isspace(**linep))
+		*linep += 1;
+	*nump = BPF_LINE_INFO_LINE_NUM(linfo[idx].line_col);
+	return 0;
+}
+
+#endif
-- 
2.47.1


  parent reply	other threads:[~2025-06-24  3:12 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-06-24  3:12 [PATCH bpf-next v3 00/12] BPF Standard Streams Kumar Kartikeya Dwivedi
2025-06-24  3:12 ` [PATCH bpf-next v3 01/12] bpf: Refactor bprintf buffer support Kumar Kartikeya Dwivedi
2025-06-24  3:12 ` [PATCH bpf-next v3 02/12] bpf: Introduce BPF standard streams Kumar Kartikeya Dwivedi
2025-06-24 12:01   ` Jiri Olsa
2025-06-24 12:15     ` Kumar Kartikeya Dwivedi
2025-06-24 13:34       ` Jiri Olsa
2025-06-24 16:03         ` Alexei Starovoitov
2025-06-24 18:06   ` Alexei Starovoitov
2025-06-24  3:12 ` Kumar Kartikeya Dwivedi [this message]
2025-06-24  3:12 ` [PATCH bpf-next v3 04/12] bpf: Ensure RCU lock is held around bpf_prog_ksym_find Kumar Kartikeya Dwivedi
2025-06-24 17:45   ` Alexei Starovoitov
2025-06-24  3:12 ` [PATCH bpf-next v3 05/12] bpf: Add function to find program from stack trace Kumar Kartikeya Dwivedi
2025-06-24 17:46   ` Alexei Starovoitov
2025-06-24  3:12 ` [PATCH bpf-next v3 06/12] bpf: Add dump_stack() analogue to print to BPF stderr Kumar Kartikeya Dwivedi
2025-06-24 11:38   ` Jiri Olsa
2025-06-24 11:49     ` Kumar Kartikeya Dwivedi
2025-06-24  3:12 ` [PATCH bpf-next v3 07/12] bpf: Report may_goto timeout " Kumar Kartikeya Dwivedi
2025-06-24  3:12 ` [PATCH bpf-next v3 08/12] bpf: Report rqspinlock deadlocks/timeout " Kumar Kartikeya Dwivedi
2025-06-24  3:12 ` [PATCH bpf-next v3 09/12] libbpf: Add bpf_stream_printk() macro Kumar Kartikeya Dwivedi
2025-06-25 20:01   ` Andrii Nakryiko
2025-06-24  3:12 ` [PATCH bpf-next v3 10/12] libbpf: Introduce bpf_prog_stream_read() API Kumar Kartikeya Dwivedi
2025-06-24  3:12 ` [PATCH bpf-next v3 11/12] bpftool: Add support for dumping streams Kumar Kartikeya Dwivedi
2025-06-24  3:12 ` [PATCH bpf-next v3 12/12] selftests/bpf: Add tests for prog streams Kumar Kartikeya Dwivedi

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=20250624031252.2966759-4-memxor@gmail.com \
    --to=memxor@gmail.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=brho@google.com \
    --cc=daniel@iogearbox.net \
    --cc=eddyz87@gmail.com \
    --cc=emil@etsalapatis.com \
    --cc=kernel-team@meta.com \
    --cc=kkd@meta.com \
    --cc=martin.lau@kernel.org \
    --cc=mattbobrowski@google.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).