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 SEARCHMSG	"testing,testing"
9 
trace_pipe_cb(const char * str,void * data)10 static void trace_pipe_cb(const char *str, void *data)
11 {
12 	if (strstr(str, SEARCHMSG) != NULL)
13 		(*(int *)data)++;
14 }
15 
serial_test_trace_printk(void)16 void serial_test_trace_printk(void)
17 {
18 	struct trace_printk_lskel__bss *bss;
19 	struct trace_printk_lskel *skel;
20 	int err = 0, found = 0;
21 
22 	skel = trace_printk_lskel__open();
23 	if (!ASSERT_OK_PTR(skel, "trace_printk__open"))
24 		return;
25 
26 	ASSERT_EQ(skel->rodata->fmt[0], 'T', "skel->rodata->fmt[0]");
27 	skel->rodata->fmt[0] = 't';
28 
29 	err = trace_printk_lskel__load(skel);
30 	if (!ASSERT_OK(err, "trace_printk__load"))
31 		goto cleanup;
32 
33 	bss = skel->bss;
34 
35 	err = trace_printk_lskel__attach(skel);
36 	if (!ASSERT_OK(err, "trace_printk__attach"))
37 		goto cleanup;
38 
39 	/* wait for tracepoint to trigger */
40 	usleep(1);
41 	trace_printk_lskel__detach(skel);
42 
43 	if (!ASSERT_GT(bss->trace_printk_ran, 0, "bss->trace_printk_ran"))
44 		goto cleanup;
45 
46 	if (!ASSERT_GT(bss->trace_printk_ret, 0, "bss->trace_printk_ret"))
47 		goto cleanup;
48 
49 	/* verify our search string is in the trace buffer */
50 	ASSERT_OK(read_trace_pipe_iter(trace_pipe_cb, &found, 1000),
51 		 "read_trace_pipe_iter");
52 
53 	if (!ASSERT_EQ(found, bss->trace_printk_ran, "found"))
54 		goto cleanup;
55 
56 cleanup:
57 	trace_printk_lskel__destroy(skel);
58 }
59