2020-07-13 14:52:34 +03:00
// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2020, Oracle and/or its affiliates. */
# include <test_progs.h>
2021-05-14 03:36:23 +03:00
# include "trace_printk.lskel.h"
2020-07-13 14:52:34 +03:00
# define TRACEBUF " / sys / kernel / debug / tracing / trace_pipe"
# define SEARCHMSG "testing,testing"
2021-10-06 21:56:19 +03:00
void serial_test_trace_printk ( void )
2020-07-13 14:52:34 +03:00
{
2021-10-28 09:34:59 +03:00
struct trace_printk_lskel__bss * bss ;
2021-09-17 21:29:09 +03:00
int err = 0 , iter = 0 , found = 0 ;
2021-10-28 09:34:59 +03:00
struct trace_printk_lskel * skel ;
2020-07-13 14:52:34 +03:00
char * buf = NULL ;
FILE * fp = NULL ;
size_t buflen ;
2021-10-28 09:34:59 +03:00
skel = trace_printk_lskel__open ( ) ;
2021-09-17 21:29:09 +03:00
if ( ! ASSERT_OK_PTR ( skel , " trace_printk__open " ) )
2020-07-13 14:52:34 +03:00
return ;
2021-09-17 21:29:09 +03:00
ASSERT_EQ ( skel - > rodata - > fmt [ 0 ] , ' T ' , " skel->rodata->fmt[0] " ) ;
2021-05-14 03:36:22 +03:00
skel - > rodata - > fmt [ 0 ] = ' t ' ;
2021-10-28 09:34:59 +03:00
err = trace_printk_lskel__load ( skel ) ;
2021-09-17 21:29:09 +03:00
if ( ! ASSERT_OK ( err , " trace_printk__load " ) )
2020-07-13 14:52:34 +03:00
goto cleanup ;
bss = skel - > bss ;
2021-10-28 09:34:59 +03:00
err = trace_printk_lskel__attach ( skel ) ;
2021-09-17 21:29:09 +03:00
if ( ! ASSERT_OK ( err , " trace_printk__attach " ) )
2020-07-13 14:52:34 +03:00
goto cleanup ;
fp = fopen ( TRACEBUF , " r " ) ;
2021-09-17 21:29:09 +03:00
if ( ! ASSERT_OK_PTR ( fp , " fopen(TRACEBUF) " ) )
2020-07-13 14:52:34 +03: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 ) ;
2021-10-28 09:34:59 +03:00
trace_printk_lskel__detach ( skel ) ;
2020-07-13 14:52:34 +03:00
2021-09-17 21:29:09 +03:00
if ( ! ASSERT_GT ( bss - > trace_printk_ran , 0 , " bss->trace_printk_ran " ) )
2020-07-13 14:52:34 +03:00
goto cleanup ;
2021-09-17 21:29:09 +03:00
if ( ! ASSERT_GT ( bss - > trace_printk_ret , 0 , " bss->trace_printk_ret " ) )
2020-07-13 14:52:34 +03: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 21:29:09 +03:00
if ( ! ASSERT_EQ ( found , bss - > trace_printk_ran , " found " ) )
2020-07-13 14:52:34 +03:00
goto cleanup ;
cleanup :
2021-10-28 09:34:59 +03:00
trace_printk_lskel__destroy ( skel ) ;
2020-07-13 14:52:34 +03:00
free ( buf ) ;
if ( fp )
fclose ( fp ) ;
}