Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion BPF-CHECKPOINT-COMMIT
Original file line number Diff line number Diff line change
@@ -1 +1 @@
e34a79b96ab9d49ed8b605fee11099cf3efbb428
bf4807c89d8f92c47404b1e4eeeefb42259d1b50
2 changes: 1 addition & 1 deletion CHECKPOINT-COMMIT
Original file line number Diff line number Diff line change
@@ -1 +1 @@
212ec92295673a362453f8ede36033b61c7983fa
e860a98c8aebd8de82c0ee901acf5a759acd4570
16 changes: 15 additions & 1 deletion bash-completion/bpftool
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,21 @@ _bpftool()
esac
;;
tracelog)
return 0
case $prev in
$command)
COMPREPLY+=( $( compgen -W "stdout stderr" -- \
"$cur" ) )
return 0
;;
stdout|stderr)
COMPREPLY=( $( compgen -W "$PROG_TYPE" -- \
"$cur" ) )
return 0
;;
*)
return 0
;;
esac
;;
profile)
case $cword in
Expand Down
7 changes: 7 additions & 0 deletions docs/bpftool-prog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ PROG COMMANDS
| **bpftool** **prog attach** *PROG* *ATTACH_TYPE* [*MAP*]
| **bpftool** **prog detach** *PROG* *ATTACH_TYPE* [*MAP*]
| **bpftool** **prog tracelog**
| **bpftool** **prog tracelog** [ { **stdout** | **stderr** } *PROG* ]
| **bpftool** **prog run** *PROG* **data_in** *FILE* [**data_out** *FILE* [**data_size_out** *L*]] [**ctx_in** *FILE* [**ctx_out** *FILE* [**ctx_size_out** *M*]]] [**repeat** *N*]
| **bpftool** **prog profile** *PROG* [**duration** *DURATION*] *METRICs*
| **bpftool** **prog help**
Expand Down Expand Up @@ -179,6 +180,12 @@ bpftool prog tracelog
purposes. For streaming data from BPF programs to user space, one can use
perf events (see also **bpftool-map**\ (8)).

bpftool prog tracelog { stdout | stderr } *PROG*
Dump the BPF stream of the program. BPF programs can write to these streams
at runtime with the **bpf_stream_vprintk**\ () kfunc. The kernel may write
error messages to the standard error stream. This facility should be used
only for debugging purposes.

bpftool prog run *PROG* data_in *FILE* [data_out *FILE* [data_size_out *L*]] [ctx_in *FILE* [ctx_out *FILE* [ctx_size_out *M*]]] [repeat *N*]
Run BPF program *PROG* in the kernel testing infrastructure for BPF,
meaning that the program works on the data and context provided by the
Expand Down
24 changes: 24 additions & 0 deletions include/uapi/linux/bpf.h
Original file line number Diff line number Diff line change
Expand Up @@ -906,6 +906,17 @@ union bpf_iter_link_info {
* A new file descriptor (a nonnegative integer), or -1 if an
* error occurred (in which case, *errno* is set appropriately).
*
* BPF_PROG_STREAM_READ_BY_FD
* Description
* Read data of a program's BPF stream. The program is identified
* by *prog_fd*, and the stream is identified by the *stream_id*.
* The data is copied to a buffer pointed to by *stream_buf*, and
* filled less than or equal to *stream_buf_len* bytes.
*
* Return
* Number of bytes read from the stream on success, or -1 if an
* error occurred (in which case, *errno* is set appropriately).
*
* NOTES
* eBPF objects (maps and programs) can be shared between processes.
*
Expand Down Expand Up @@ -961,6 +972,7 @@ enum bpf_cmd {
BPF_LINK_DETACH,
BPF_PROG_BIND_MAP,
BPF_TOKEN_CREATE,
BPF_PROG_STREAM_READ_BY_FD,
__MAX_BPF_CMD,
};

Expand Down Expand Up @@ -1463,6 +1475,11 @@ struct bpf_stack_build_id {

#define BPF_OBJ_NAME_LEN 16U

enum {
BPF_STREAM_STDOUT = 1,
BPF_STREAM_STDERR = 2,
};

union bpf_attr {
struct { /* anonymous struct used by BPF_MAP_CREATE command */
__u32 map_type; /* one of enum bpf_map_type */
Expand Down Expand Up @@ -1849,6 +1866,13 @@ union bpf_attr {
__u32 bpffs_fd;
} token_create;

struct {
__aligned_u64 stream_buf;
__u32 stream_buf_len;
__u32 stream_id;
__u32 prog_fd;
} prog_stream_read;

} __attribute__((aligned(8)));

/* The description below is an attempt at providing documentation to eBPF
Expand Down
2 changes: 1 addition & 1 deletion libbpf
49 changes: 48 additions & 1 deletion src/prog.c
Original file line number Diff line number Diff line change
Expand Up @@ -1113,6 +1113,52 @@ static int do_detach(int argc, char **argv)
return 0;
}

enum prog_tracelog_mode {
TRACE_STDOUT,
TRACE_STDERR,
};

static int
prog_tracelog_stream(int prog_fd, enum prog_tracelog_mode mode)
{
FILE *file = mode == TRACE_STDOUT ? stdout : stderr;
int stream_id = mode == TRACE_STDOUT ? 1 : 2;
char buf[512];
int ret;

ret = 0;
do {
ret = bpf_prog_stream_read(prog_fd, stream_id, buf, sizeof(buf), NULL);
if (ret > 0)
fwrite(buf, sizeof(buf[0]), ret, file);
} while (ret > 0);

fflush(file);
return ret ? -1 : 0;
}

static int do_tracelog_any(int argc, char **argv)
{
enum prog_tracelog_mode mode;
int fd;

if (argc == 0)
return do_tracelog(argc, argv);
if (!is_prefix(*argv, "stdout") && !is_prefix(*argv, "stderr"))
usage();
mode = is_prefix(*argv, "stdout") ? TRACE_STDOUT : TRACE_STDERR;
NEXT_ARG();

if (!REQ_ARGS(2))
return -1;

fd = prog_parse_fd(&argc, &argv);
if (fd < 0)
return -1;

return prog_tracelog_stream(fd, mode);
}

static int check_single_stdin(char *file_data_in, char *file_ctx_in)
{
if (file_data_in && file_ctx_in &&
Expand Down Expand Up @@ -2493,6 +2539,7 @@ static int do_help(int argc, char **argv)
" [repeat N]\n"
" %1$s %2$s profile PROG [duration DURATION] METRICs\n"
" %1$s %2$s tracelog\n"
" %1$s %2$s tracelog { stdout | stderr } PROG\n"
" %1$s %2$s help\n"
"\n"
" " HELP_SPEC_MAP "\n"
Expand Down Expand Up @@ -2532,7 +2579,7 @@ static const struct cmd cmds[] = {
{ "loadall", do_loadall },
{ "attach", do_attach },
{ "detach", do_detach },
{ "tracelog", do_tracelog },
{ "tracelog", do_tracelog_any },
{ "run", do_run },
{ "profile", do_profile },
{ 0 }
Expand Down
Loading