samples, bpf: Move read_trace_pipe to trace_helpers
authorDaniel T. Lee <danieltimlee@gmail.com>
Sat, 21 Mar 2020 10:04:23 +0000 (19:04 +0900)
committerDaniel Borkmann <daniel@iogearbox.net>
Mon, 23 Mar 2020 21:27:51 +0000 (22:27 +0100)
To reduce the reliance of trace samples (trace*_user) on bpf_load,
move read_trace_pipe to trace_helpers. By moving this bpf_loader helper
elsewhere, trace functions can be easily migrated to libbbpf.

Signed-off-by: Daniel T. Lee <danieltimlee@gmail.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Acked-by: Andrii Nakryiko <andriin@fb.com>
Link: https://lore.kernel.org/bpf/20200321100424.1593964-2-danieltimlee@gmail.com
samples/bpf/Makefile
samples/bpf/bpf_load.c
samples/bpf/bpf_load.h
samples/bpf/tracex1_user.c
samples/bpf/tracex5_user.c
tools/testing/selftests/bpf/trace_helpers.c
tools/testing/selftests/bpf/trace_helpers.h

index 79b0fee6943bbeb02f247a684014826aa8e24516..ff0061467dd3145acdcea0738d6513c692408434 100644 (file)
@@ -64,11 +64,11 @@ fds_example-objs := fds_example.o
 sockex1-objs := sockex1_user.o
 sockex2-objs := sockex2_user.o
 sockex3-objs := bpf_load.o sockex3_user.o
-tracex1-objs := bpf_load.o tracex1_user.o
+tracex1-objs := bpf_load.o tracex1_user.o $(TRACE_HELPERS)
 tracex2-objs := bpf_load.o tracex2_user.o
 tracex3-objs := bpf_load.o tracex3_user.o
 tracex4-objs := bpf_load.o tracex4_user.o
-tracex5-objs := bpf_load.o tracex5_user.o
+tracex5-objs := bpf_load.o tracex5_user.o $(TRACE_HELPERS)
 tracex6-objs := bpf_load.o tracex6_user.o
 tracex7-objs := bpf_load.o tracex7_user.o
 test_probe_write_user-objs := bpf_load.o test_probe_write_user_user.o
index 4574b1939e49b137d9086ec719080d6cd73c91ef..c5ad528f046e038a3939180676d64c0a4363cd0e 100644 (file)
@@ -665,23 +665,3 @@ int load_bpf_file_fixup_map(const char *path, fixup_map_cb fixup_map)
 {
        return do_load_bpf_file(path, fixup_map);
 }
-
-void read_trace_pipe(void)
-{
-       int trace_fd;
-
-       trace_fd = open(DEBUGFS "trace_pipe", O_RDONLY, 0);
-       if (trace_fd < 0)
-               return;
-
-       while (1) {
-               static char buf[4096];
-               ssize_t sz;
-
-               sz = read(trace_fd, buf, sizeof(buf) - 1);
-               if (sz > 0) {
-                       buf[sz] = 0;
-                       puts(buf);
-               }
-       }
-}
index 814894a129745a2c699665adf6b9c07513b9b0aa..4fcd258c616fde26873bcefac702f56d5e696793 100644 (file)
@@ -53,6 +53,5 @@ extern int map_data_count;
 int load_bpf_file(char *path);
 int load_bpf_file_fixup_map(const char *path, fixup_map_cb fixup_map);
 
-void read_trace_pipe(void);
 int bpf_set_link_xdp_fd(int ifindex, int fd, __u32 flags);
 #endif
index af8c20608ab562eb2b65c8e6638ed2799e71b2cc..55fddbd0870292390dc8f3ae9e60387e3ba38cda 100644 (file)
@@ -4,6 +4,7 @@
 #include <unistd.h>
 #include <bpf/bpf.h>
 #include "bpf_load.h"
+#include "trace_helpers.h"
 
 int main(int ac, char **argv)
 {
index c4ab91c894940e2c03d3406a7b55170250b4e624..c2317b39e0d25d5812ff6800553cd6bac9044b4c 100644 (file)
@@ -8,6 +8,7 @@
 #include <bpf/bpf.h>
 #include "bpf_load.h"
 #include <sys/resource.h>
+#include "trace_helpers.h"
 
 /* install fake seccomp program to enable seccomp code path inside the kernel,
  * so that our kprobe attached to seccomp_phase1() can be triggered
index 7f989b3e4e2279dd89c3a73c43ce283fe645920d..4d0e913bbb227f057ac83811aa0ddf15e1dcdb83 100644 (file)
@@ -4,12 +4,15 @@
 #include <string.h>
 #include <assert.h>
 #include <errno.h>
+#include <fcntl.h>
 #include <poll.h>
 #include <unistd.h>
 #include <linux/perf_event.h>
 #include <sys/mman.h>
 #include "trace_helpers.h"
 
+#define DEBUGFS "/sys/kernel/debug/tracing/"
+
 #define MAX_SYMS 300000
 static struct ksym syms[MAX_SYMS];
 static int sym_cnt;
@@ -86,3 +89,23 @@ long ksym_get_addr(const char *name)
 
        return 0;
 }
+
+void read_trace_pipe(void)
+{
+       int trace_fd;
+
+       trace_fd = open(DEBUGFS "trace_pipe", O_RDONLY, 0);
+       if (trace_fd < 0)
+               return;
+
+       while (1) {
+               static char buf[4096];
+               ssize_t sz;
+
+               sz = read(trace_fd, buf, sizeof(buf) - 1);
+               if (sz > 0) {
+                       buf[sz] = 0;
+                       puts(buf);
+               }
+       }
+}
index 0383c9b8adc187271af96204e62251874baccdf3..25ef597dd03fdb1264d4ebcdb12c28137f72def9 100644 (file)
@@ -12,5 +12,6 @@ struct ksym {
 int load_kallsyms(void);
 struct ksym *ksym_search(long key);
 long ksym_get_addr(const char *name);
+void read_trace_pipe(void);
 
 #endif