Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /* Copyright (c) 2020, Oracle and/or its affiliates. */
0003 
0004 #include <test_progs.h>
0005 
0006 #include "trace_printk.lskel.h"
0007 
0008 #define TRACEBUF    "/sys/kernel/debug/tracing/trace_pipe"
0009 #define SEARCHMSG   "testing,testing"
0010 
0011 void serial_test_trace_printk(void)
0012 {
0013     struct trace_printk_lskel__bss *bss;
0014     int err = 0, iter = 0, found = 0;
0015     struct trace_printk_lskel *skel;
0016     char *buf = NULL;
0017     FILE *fp = NULL;
0018     size_t buflen;
0019 
0020     skel = trace_printk_lskel__open();
0021     if (!ASSERT_OK_PTR(skel, "trace_printk__open"))
0022         return;
0023 
0024     ASSERT_EQ(skel->rodata->fmt[0], 'T', "skel->rodata->fmt[0]");
0025     skel->rodata->fmt[0] = 't';
0026 
0027     err = trace_printk_lskel__load(skel);
0028     if (!ASSERT_OK(err, "trace_printk__load"))
0029         goto cleanup;
0030 
0031     bss = skel->bss;
0032 
0033     err = trace_printk_lskel__attach(skel);
0034     if (!ASSERT_OK(err, "trace_printk__attach"))
0035         goto cleanup;
0036 
0037     fp = fopen(TRACEBUF, "r");
0038     if (!ASSERT_OK_PTR(fp, "fopen(TRACEBUF)"))
0039         goto cleanup;
0040 
0041     /* We do not want to wait forever if this test fails... */
0042     fcntl(fileno(fp), F_SETFL, O_NONBLOCK);
0043 
0044     /* wait for tracepoint to trigger */
0045     usleep(1);
0046     trace_printk_lskel__detach(skel);
0047 
0048     if (!ASSERT_GT(bss->trace_printk_ran, 0, "bss->trace_printk_ran"))
0049         goto cleanup;
0050 
0051     if (!ASSERT_GT(bss->trace_printk_ret, 0, "bss->trace_printk_ret"))
0052         goto cleanup;
0053 
0054     /* verify our search string is in the trace buffer */
0055     while (getline(&buf, &buflen, fp) >= 0 || errno == EAGAIN) {
0056         if (strstr(buf, SEARCHMSG) != NULL)
0057             found++;
0058         if (found == bss->trace_printk_ran)
0059             break;
0060         if (++iter > 1000)
0061             break;
0062     }
0063 
0064     if (!ASSERT_EQ(found, bss->trace_printk_ran, "found"))
0065         goto cleanup;
0066 
0067 cleanup:
0068     trace_printk_lskel__destroy(skel);
0069     free(buf);
0070     if (fp)
0071         fclose(fp);
0072 }