linux/tools/testing/selftests/bpf/prog_tests/trace_printk.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0
   2/* Copyright (c) 2020, Oracle and/or its affiliates. */
   3
   4#include <test_progs.h>
   5
   6#include "trace_printk.lskel.h"
   7
   8#define TRACEBUF        "/sys/kernel/debug/tracing/trace_pipe"
   9#define SEARCHMSG       "testing,testing"
  10
  11void test_trace_printk(void)
  12{
  13        int err, iter = 0, duration = 0, found = 0;
  14        struct trace_printk__bss *bss;
  15        struct trace_printk *skel;
  16        char *buf = NULL;
  17        FILE *fp = NULL;
  18        size_t buflen;
  19
  20        skel = trace_printk__open();
  21        if (CHECK(!skel, "skel_open", "failed to open skeleton\n"))
  22                return;
  23
  24        ASSERT_EQ(skel->rodata->fmt[0], 'T', "invalid printk fmt string");
  25        skel->rodata->fmt[0] = 't';
  26
  27        err = trace_printk__load(skel);
  28        if (CHECK(err, "skel_load", "failed to load skeleton: %d\n", err))
  29                goto cleanup;
  30
  31        bss = skel->bss;
  32
  33        err = trace_printk__attach(skel);
  34        if (CHECK(err, "skel_attach", "skeleton attach failed: %d\n", err))
  35                goto cleanup;
  36
  37        fp = fopen(TRACEBUF, "r");
  38        if (CHECK(fp == NULL, "could not open trace buffer",
  39                  "error %d opening %s", errno, TRACEBUF))
  40                goto cleanup;
  41
  42        /* We do not want to wait forever if this test fails... */
  43        fcntl(fileno(fp), F_SETFL, O_NONBLOCK);
  44
  45        /* wait for tracepoint to trigger */
  46        usleep(1);
  47        trace_printk__detach(skel);
  48
  49        if (CHECK(bss->trace_printk_ran == 0,
  50                  "bpf_trace_printk never ran",
  51                  "ran == %d", bss->trace_printk_ran))
  52                goto cleanup;
  53
  54        if (CHECK(bss->trace_printk_ret <= 0,
  55                  "bpf_trace_printk returned <= 0 value",
  56                  "got %d", bss->trace_printk_ret))
  57                goto cleanup;
  58
  59        /* verify our search string is in the trace buffer */
  60        while (getline(&buf, &buflen, fp) >= 0 || errno == EAGAIN) {
  61                if (strstr(buf, SEARCHMSG) != NULL)
  62                        found++;
  63                if (found == bss->trace_printk_ran)
  64                        break;
  65                if (++iter > 1000)
  66                        break;
  67        }
  68
  69        if (CHECK(!found, "message from bpf_trace_printk not found",
  70                  "no instance of %s in %s", SEARCHMSG, TRACEBUF))
  71                goto cleanup;
  72
  73cleanup:
  74        trace_printk__destroy(skel);
  75        free(buf);
  76        if (fp)
  77                fclose(fp);
  78}
  79