On Fri, Jul 3, 2020 at 7:45 AM Alan Maguire <alan.maguire@xxxxxxxxxx> wrote: > > Simple selftest that verifies bpf_trace_printk() returns a sensible > value and tracing messages appear. > > Signed-off-by: Alan Maguire <alan.maguire@xxxxxxxxxx> > --- > .../selftests/bpf/prog_tests/trace_printk.c | 71 ++++++++++++++++++++++ > tools/testing/selftests/bpf/progs/trace_printk.c | 21 +++++++ > 2 files changed, 92 insertions(+) > create mode 100644 tools/testing/selftests/bpf/prog_tests/trace_printk.c > create mode 100644 tools/testing/selftests/bpf/progs/trace_printk.c > [...] > + fd = open(TRACEBUF, O_RDONLY); > + if (CHECK(fd < 0, "could not open trace buffer", > + "error %d opening %s", errno, TRACEBUF)) > + goto cleanup; > + > + /* We do not want to wait forever if this test fails... */ > + fcntl(fd, F_SETFL, O_NONBLOCK); > + > + /* wait for tracepoint to trigger */ > + sleep(1); that's a long sleep, it's better to use tp/raw_syscalls/sys_enter tracepoint to trigger BPF program and then just usleep(1) > + trace_printk__detach(skel); > + > + if (CHECK(bss->trace_printk_ran == 0, > + "bpf_trace_printk never ran", > + "ran == %d", bss->trace_printk_ran)) > + goto cleanup; > + [...] > + > +int trace_printk_ret = 0; > +int trace_printk_ran = 0; > + > +SEC("tracepoint/sched/sched_switch") see above, probably better to stick to something like tp/raw_syscalls/sys_enter or raw_tp/sys_enter. Also, to not overwhelm trace_pipe output, might want to filter by PID and emit messages for test_prog's PID only. > +int sched_switch(void *ctx) > +{ > + static const char fmt[] = "testing,testing %d\n"; > + > + trace_printk_ret = bpf_trace_printk(fmt, sizeof(fmt), > + ++trace_printk_ran); > + return 0; > +} > -- > 1.8.3.1 >