Re: [PATCH v2 bpf-next 2/2] selftests/bpf: add selftests verifying bpf_trace_printk() behaviour

From: Andrii Nakryiko
Date: Fri Jul 10 2020 - 16:55:24 EST


On Fri, Jul 10, 2020 at 7:25 AM Alan Maguire <alan.maguire@xxxxxxxxxx> wrote:
>
> Simple selftests that verifies bpf_trace_printk() returns a sensible
> value and tracing messages appear.
>
> Signed-off-by: Alan Maguire <alan.maguire@xxxxxxxxxx>
> ---

see pedantic note below, but I don't think that's an issue in practice

Acked-by: Andrii Nakryiko <andriin@xxxxxx>

> .../selftests/bpf/prog_tests/trace_printk.c | 74 ++++++++++++++++++++++
> tools/testing/selftests/bpf/progs/trace_printk.c | 21 ++++++
> 2 files changed, 95 insertions(+)
> create mode 100644 tools/testing/selftests/bpf/prog_tests/trace_printk.c
> create mode 100644 tools/testing/selftests/bpf/progs/trace_printk.c
>

[...]

> +
> + /* verify our search string is in the trace buffer */
> + while (read(fd, buf, sizeof(buf)) >= 0 || errno == EAGAIN) {

There is a minor chance that "testing,testing" won't be found, if it
so happened that the first part is in the first read buffer, and the
second is in the second. I don't think it's ever the case for our CI
and for my local testing setup, but could be a cause of some
instability if there is something else emitting data to trace_pipe,
right?

Maybe line-based reading would be more reliable (unless printk can
intermix, not sure about that, in which case there is simply no way to
solve this 100% reliably).


> + if (strstr(buf, SEARCHMSG) != NULL)
> + found++;
> + if (found == bss->trace_printk_ran)
> + break;
> + if (++iter > 1000)
> + break;
> + }
> +
> + if (CHECK(!found, "message from bpf_trace_printk not found",
> + "no instance of %s in %s", SEARCHMSG, TRACEBUF))
> + goto cleanup;
> +
> + printf("ran %d times; last return value %d, with %d instances of msg\n",
> + bss->trace_printk_ran, bss->trace_printk_ret, found);

Is this needed or it's some debug leftover?

> +cleanup:
> + trace_printk__destroy(skel);
> + if (fd != -1)
> + close(fd);
> +}

[...]