e2e_shlib.t (2563B)
1 # 2 # Check that instrumenting and loading a shared library works. 3 # 4 use File::Which; 5 use Modern::Perl; 6 use Test::Cmd; 7 use Test::Differences; 8 use Test::More; 9 10 use lib 't'; 11 require utils; 12 13 if ($^O eq 'MSWin32') { 14 plan skip_all => '.so platforms only'; 15 } 16 plan tests => 15; 17 18 19 my $wrap = Test::Cmd->new( prog => 'bin/citrun_wrap', workdir => '' ); 20 21 $wrap->write( 'main.c', <<EOF ); 22 #include "lib.h" 23 24 int 25 main(void) 26 { 27 libfunc(); 28 return 0; 29 } 30 EOF 31 32 $wrap->write( 'lib.h', <<EOF ); 33 int libfunc(void); 34 EOF 35 36 $wrap->write( 'lib.c', <<EOF ); 37 #include <stdio.h> 38 39 int 40 libfunc(void) 41 { 42 printf("lib called"); 43 return 0; 44 } 45 EOF 46 47 $wrap->run( args => 'cc -c -fpic lib.c', chdir => $wrap->curdir ); 48 is( $wrap->stdout, '', 'citrun_wrap cc -c stdout silent' ); 49 is( $wrap->stderr, '', 'citrun_wrap cc -c stderr silent' ); 50 is( $? >> 8, 0, 'citrun_wrap cc -c exit code 0' ); 51 52 $wrap->run( args => 'cc -shared -o liblib.so lib.o', chdir => $wrap->curdir ); 53 is( $wrap->stdout, '', 'citrun_wrap cc -shared stdout silent' ); 54 is( $wrap->stderr, '', 'citrun_wrap cc -shared stderr silent' ); 55 is( $? >> 8, 0, 'citrun_wrap cc -shared exit code 0' ); 56 57 # Don't instrument main to make sure the library can stand on its own. 58 my ($compiler) = which 'cc'; 59 $wrap->run( prog => $compiler, args => '-L. -o main main.c -llib', chdir => $wrap->curdir ); 60 is( $wrap->stdout, '', 'cc -o main -llib stdout silent' ); 61 is( $wrap->stderr, '', 'cc -o main -llib stderr silent' ); 62 is( $? >> 8, 0, 'cc -o main -llib exit code 0' ); 63 64 my $log_good = <<EOF; 65 Summary: 66 2 Rewrite tool runs 67 1 Source files used as input 68 1 Application link commands 69 1 Successful modified source compiles 70 71 Totals: 72 9 Lines of source code 73 1 Function definitions 74 1 Return statement values 75 1 Call expressions 76 9 Total statements 77 EOF 78 79 $wrap->run( prog => 'bin/citrun_report', args => 'citrun.log', 80 chdir => $wrap->curdir ); 81 my $wrap_stdout = $wrap->stdout; 82 $wrap_stdout =~ s/.*Milliseconds spent.*\n//; 83 eq_or_diff( $wrap_stdout, $log_good, 'is citrun_report output identical', { context => 3 } ); 84 is( $wrap->stderr, '', 'citrun_report stderr silent' ); 85 is( $? >> 8, 0, 'citrun_report exit code 0' ); 86 87 $ENV{LD_LIBRARY_PATH} = $wrap->workdir; 88 89 $wrap->run( prog => $wrap->workdir . '/main', chdir => $wrap->curdir ); 90 is( $wrap->stdout, 'lib called', 'inst program stdout silent' ); 91 isnt( $wrap->stderr, '', 'inst program stderr not silent' ); 92 is( $? >> 8, 0, 'inst program exit code 0' ); 93 unlink "/tmp/citrun.out"; 94 #my $file = get_one_shmfile( $wrap->workdir );