1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2020, Oracle and/or its affiliates. */
4 #include <test_progs.h>
6 #include "trace_printk.skel.h"
8 #define TRACEBUF "/sys/kernel/debug/tracing/trace_pipe"
9 #define SEARCHMSG "testing,testing"
11 void test_trace_printk(void)
13 int err, iter = 0, duration = 0, found = 0;
14 struct trace_printk__bss *bss;
15 struct trace_printk *skel;
20 skel = trace_printk__open();
21 if (CHECK(!skel, "skel_open", "failed to open skeleton\n"))
24 err = trace_printk__load(skel);
25 if (CHECK(err, "skel_load", "failed to load skeleton: %d\n", err))
30 err = trace_printk__attach(skel);
31 if (CHECK(err, "skel_attach", "skeleton attach failed: %d\n", err))
34 fp = fopen(TRACEBUF, "r");
35 if (CHECK(fp == NULL, "could not open trace buffer",
36 "error %d opening %s", errno, TRACEBUF))
39 /* We do not want to wait forever if this test fails... */
40 fcntl(fileno(fp), F_SETFL, O_NONBLOCK);
42 /* wait for tracepoint to trigger */
44 trace_printk__detach(skel);
46 if (CHECK(bss->trace_printk_ran == 0,
47 "bpf_trace_printk never ran",
48 "ran == %d", bss->trace_printk_ran))
51 if (CHECK(bss->trace_printk_ret <= 0,
52 "bpf_trace_printk returned <= 0 value",
53 "got %d", bss->trace_printk_ret))
56 /* verify our search string is in the trace buffer */
57 while (getline(&buf, &buflen, fp) >= 0 || errno == EAGAIN) {
58 if (strstr(buf, SEARCHMSG) != NULL)
60 if (found == bss->trace_printk_ran)
66 if (CHECK(!found, "message from bpf_trace_printk not found",
67 "no instance of %s in %s", SEARCHMSG, TRACEBUF))
71 trace_printk__destroy(skel);