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