2020-07-13 12:52:34 +01:00
// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2020, Oracle and/or its affiliates. */
# include <test_progs.h>
2021-05-13 17:36:23 -07:00
# include "trace_printk.lskel.h"
2020-07-13 12:52:34 +01:00
# define TRACEBUF " / sys / kernel / debug / tracing / trace_pipe"
# define SEARCHMSG "testing,testing"
2021-10-06 11:56:19 -07:00
void serial_test_trace_printk ( void )
2020-07-13 12:52:34 +01:00
{
2021-09-17 11:29:09 -07:00
int err = 0 , iter = 0 , found = 0 ;
2020-07-13 12:52:34 +01:00
struct trace_printk__bss * bss ;
struct trace_printk * skel ;
char * buf = NULL ;
FILE * fp = NULL ;
size_t buflen ;
skel = trace_printk__open ( ) ;
2021-09-17 11:29:09 -07:00
if ( ! ASSERT_OK_PTR ( skel , " trace_printk__open " ) )
2020-07-13 12:52:34 +01:00
return ;
2021-09-17 11:29:09 -07:00
ASSERT_EQ ( skel - > rodata - > fmt [ 0 ] , ' T ' , " skel->rodata->fmt[0] " ) ;
2021-05-13 17:36:22 -07:00
skel - > rodata - > fmt [ 0 ] = ' t ' ;
2020-07-13 12:52:34 +01:00
err = trace_printk__load ( skel ) ;
2021-09-17 11:29:09 -07:00
if ( ! ASSERT_OK ( err , " trace_printk__load " ) )
2020-07-13 12:52:34 +01:00
goto cleanup ;
bss = skel - > bss ;
err = trace_printk__attach ( skel ) ;
2021-09-17 11:29:09 -07:00
if ( ! ASSERT_OK ( err , " trace_printk__attach " ) )
2020-07-13 12:52:34 +01:00
goto cleanup ;
fp = fopen ( TRACEBUF , " r " ) ;
2021-09-17 11:29:09 -07:00
if ( ! ASSERT_OK_PTR ( fp , " fopen(TRACEBUF) " ) )
2020-07-13 12:52:34 +01:00
goto cleanup ;
/* We do not want to wait forever if this test fails... */
fcntl ( fileno ( fp ) , F_SETFL , O_NONBLOCK ) ;
/* wait for tracepoint to trigger */
usleep ( 1 ) ;
trace_printk__detach ( skel ) ;
2021-09-17 11:29:09 -07:00
if ( ! ASSERT_GT ( bss - > trace_printk_ran , 0 , " bss->trace_printk_ran " ) )
2020-07-13 12:52:34 +01:00
goto cleanup ;
2021-09-17 11:29:09 -07:00
if ( ! ASSERT_GT ( bss - > trace_printk_ret , 0 , " bss->trace_printk_ret " ) )
2020-07-13 12:52:34 +01:00
goto cleanup ;
/* verify our search string is in the trace buffer */
while ( getline ( & buf , & buflen , fp ) > = 0 | | errno = = EAGAIN ) {
if ( strstr ( buf , SEARCHMSG ) ! = NULL )
found + + ;
if ( found = = bss - > trace_printk_ran )
break ;
if ( + + iter > 1000 )
break ;
}
2021-09-17 11:29:09 -07:00
if ( ! ASSERT_EQ ( found , bss - > trace_printk_ran , " found " ) )
2020-07-13 12:52:34 +01:00
goto cleanup ;
cleanup :
trace_printk__destroy ( skel ) ;
free ( buf ) ;
if ( fp )
fclose ( fp ) ;
}