1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2021 Facebook */
3 
4 #include <test_progs.h>
5 
6 #include "trace_vprintk.lskel.h"
7 
8 #define TRACEBUF	"/sys/kernel/debug/tracing/trace_pipe"
9 #define SEARCHMSG	"1,2,3,4,5,6,7,8,9,10"
10 
serial_test_trace_vprintk(void)11 void serial_test_trace_vprintk(void)
12 {
13 	struct trace_vprintk_lskel__bss *bss;
14 	int err = 0, iter = 0, found = 0;
15 	struct trace_vprintk_lskel *skel;
16 	char *buf = NULL;
17 	FILE *fp = NULL;
18 	size_t buflen;
19 
20 	skel = trace_vprintk_lskel__open_and_load();
21 	if (!ASSERT_OK_PTR(skel, "trace_vprintk__open_and_load"))
22 		goto cleanup;
23 
24 	bss = skel->bss;
25 
26 	err = trace_vprintk_lskel__attach(skel);
27 	if (!ASSERT_OK(err, "trace_vprintk__attach"))
28 		goto cleanup;
29 
30 	fp = fopen(TRACEBUF, "r");
31 	if (!ASSERT_OK_PTR(fp, "fopen(TRACEBUF)"))
32 		goto cleanup;
33 
34 	/* We do not want to wait forever if this test fails... */
35 	fcntl(fileno(fp), F_SETFL, O_NONBLOCK);
36 
37 	/* wait for tracepoint to trigger */
38 	usleep(1);
39 	trace_vprintk_lskel__detach(skel);
40 
41 	if (!ASSERT_GT(bss->trace_vprintk_ran, 0, "bss->trace_vprintk_ran"))
42 		goto cleanup;
43 
44 	if (!ASSERT_GT(bss->trace_vprintk_ret, 0, "bss->trace_vprintk_ret"))
45 		goto cleanup;
46 
47 	/* verify our search string is in the trace buffer */
48 	while (getline(&buf, &buflen, fp) >= 0 || errno == EAGAIN) {
49 		if (strstr(buf, SEARCHMSG) != NULL)
50 			found++;
51 		if (found == bss->trace_vprintk_ran)
52 			break;
53 		if (++iter > 1000)
54 			break;
55 	}
56 
57 	if (!ASSERT_EQ(found, bss->trace_vprintk_ran, "found"))
58 		goto cleanup;
59 
60 	if (!ASSERT_LT(bss->null_data_vprintk_ret, 0, "bss->null_data_vprintk_ret"))
61 		goto cleanup;
62 
63 cleanup:
64 	trace_vprintk_lskel__destroy(skel);
65 	free(buf);
66 	if (fp)
67 		fclose(fp);
68 }
69