Linux Audio

Check our new training course

Loading...
v6.2
 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 serial_test_trace_printk(void)
12{
13	struct trace_printk_lskel__bss *bss;
14	int err = 0, iter = 0, found = 0;
15	struct trace_printk_lskel *skel;
16	char *buf = NULL;
17	FILE *fp = NULL;
18	size_t buflen;
19
20	skel = trace_printk_lskel__open();
21	if (!ASSERT_OK_PTR(skel, "trace_printk__open"))
22		return;
23
24	ASSERT_EQ(skel->rodata->fmt[0], 'T', "skel->rodata->fmt[0]");
25	skel->rodata->fmt[0] = 't';
26
27	err = trace_printk_lskel__load(skel);
28	if (!ASSERT_OK(err, "trace_printk__load"))
29		goto cleanup;
30
31	bss = skel->bss;
32
33	err = trace_printk_lskel__attach(skel);
34	if (!ASSERT_OK(err, "trace_printk__attach"))
35		goto cleanup;
36
37	fp = fopen(TRACEBUF, "r");
38	if (!ASSERT_OK_PTR(fp, "fopen(TRACEBUF)"))
 
39		goto cleanup;
40
41	/* We do not want to wait forever if this test fails... */
42	fcntl(fileno(fp), F_SETFL, O_NONBLOCK);
43
44	/* wait for tracepoint to trigger */
45	usleep(1);
46	trace_printk_lskel__detach(skel);
47
48	if (!ASSERT_GT(bss->trace_printk_ran, 0, "bss->trace_printk_ran"))
 
 
49		goto cleanup;
50
51	if (!ASSERT_GT(bss->trace_printk_ret, 0, "bss->trace_printk_ret"))
 
 
52		goto cleanup;
53
54	/* verify our search string is in the trace buffer */
55	while (getline(&buf, &buflen, fp) >= 0 || errno == EAGAIN) {
56		if (strstr(buf, SEARCHMSG) != NULL)
57			found++;
58		if (found == bss->trace_printk_ran)
59			break;
60		if (++iter > 1000)
61			break;
62	}
63
64	if (!ASSERT_EQ(found, bss->trace_printk_ran, "found"))
 
65		goto cleanup;
66
67cleanup:
68	trace_printk_lskel__destroy(skel);
69	free(buf);
70	if (fp)
71		fclose(fp);
72}
v5.9
 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.skel.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	err = trace_printk__load(skel);
25	if (CHECK(err, "skel_load", "failed to load skeleton: %d\n", err))
 
 
 
26		goto cleanup;
27
28	bss = skel->bss;
29
30	err = trace_printk__attach(skel);
31	if (CHECK(err, "skel_attach", "skeleton attach failed: %d\n", err))
32		goto cleanup;
33
34	fp = fopen(TRACEBUF, "r");
35	if (CHECK(fp == NULL, "could not open trace buffer",
36		  "error %d opening %s", errno, TRACEBUF))
37		goto cleanup;
38
39	/* We do not want to wait forever if this test fails... */
40	fcntl(fileno(fp), F_SETFL, O_NONBLOCK);
41
42	/* wait for tracepoint to trigger */
43	usleep(1);
44	trace_printk__detach(skel);
45
46	if (CHECK(bss->trace_printk_ran == 0,
47		  "bpf_trace_printk never ran",
48		  "ran == %d", bss->trace_printk_ran))
49		goto cleanup;
50
51	if (CHECK(bss->trace_printk_ret <= 0,
52		  "bpf_trace_printk returned <= 0 value",
53		  "got %d", bss->trace_printk_ret))
54		goto cleanup;
55
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)
59			found++;
60		if (found == bss->trace_printk_ran)
61			break;
62		if (++iter > 1000)
63			break;
64	}
65
66	if (CHECK(!found, "message from bpf_trace_printk not found",
67		  "no instance of %s in %s", SEARCHMSG, TRACEBUF))
68		goto cleanup;
69
70cleanup:
71	trace_printk__destroy(skel);
72	free(buf);
73	if (fp)
74		fclose(fp);
75}