attach3.c File Reference

Include dependency graph for attach3.c:

Go to the source code of this file.

Functions

int wait_for_attach_and_loop (void)
int main (int argc, char **argv)

Function Documentation

int main ( int  argc,
char **  argv 
)

Definition at line 49 of file attach3.c.

00050 {
00051     int status, retval, num_tests = 1, tmp;
00052     int EventSet1 = PAPI_NULL;
00053     long long **values;
00054     long long elapsed_us, elapsed_cyc, elapsed_virt_us, elapsed_virt_cyc;
00055     char event_name[PAPI_MAX_STR_LEN];;
00056     const PAPI_hw_info_t *hw_info;
00057     const PAPI_component_info_t *cmpinfo;
00058     pid_t pid;
00059 
00060     /* Fork before doing anything with the PMU */
00061 
00062     setbuf(stdout,NULL);
00063     pid = fork(  );
00064     if ( pid < 0 )
00065         test_fail( __FILE__, __LINE__, "fork()", PAPI_ESYS );
00066     if ( pid == 0 )
00067         exit( wait_for_attach_and_loop(  ) );
00068 
00069     tests_quiet( argc, argv );  /* Set TESTS_QUIET variable */
00070 
00071 
00072     /* Master only process below here */
00073 
00074     retval = PAPI_library_init( PAPI_VER_CURRENT );
00075     if ( retval != PAPI_VER_CURRENT )
00076         test_fail_exit( __FILE__, __LINE__, "PAPI_library_init", retval );
00077 
00078     if ( ( cmpinfo = PAPI_get_component_info( 0 ) ) == NULL )
00079         test_fail_exit( __FILE__, __LINE__, "PAPI_get_component_info", 0 );
00080 
00081     if ( cmpinfo->attach == 0 )
00082         test_skip( __FILE__, __LINE__, "Platform does not support attaching",
00083                    0 );
00084 
00085     hw_info = PAPI_get_hardware_info(  );
00086     if ( hw_info == NULL )
00087         test_fail_exit( __FILE__, __LINE__, "PAPI_get_hardware_info", 0 );
00088 
00089     /* add PAPI_TOT_CYC and one of the events in PAPI_FP_INS, PAPI_FP_OPS or
00090        PAPI_TOT_INS, depending on the availability of the event on the
00091        platform */
00092     retval = PAPI_create_eventset(&EventSet1);
00093     if ( retval != PAPI_OK )
00094         test_fail_exit( __FILE__, __LINE__, "PAPI_attach", retval );
00095 
00096     /* Force addition of component */
00097 
00098     retval = PAPI_assign_eventset_component( EventSet1, 0 );
00099     if ( retval != PAPI_OK )
00100         test_fail( __FILE__, __LINE__, "PAPI_assign_eventset_component",
00101                    retval );
00102 
00103     /* The following call causes this test to fail for perf_events */
00104 
00105     retval = PAPI_attach( EventSet1, ( unsigned long ) pid );
00106     if ( retval != PAPI_OK )
00107         test_fail_exit( __FILE__, __LINE__, "PAPI_attach", retval );
00108 
00109 
00110 
00111     retval = PAPI_add_event(EventSet1, PAPI_TOT_CYC);
00112     if ( retval != PAPI_OK )
00113         test_fail_exit( __FILE__, __LINE__, "PAPI_add_event", retval );
00114 
00115 
00116     strcpy(event_name,"PAPI_FP_INS");
00117     retval = PAPI_add_named_event(EventSet1, event_name);
00118     if ( retval == PAPI_ENOEVNT ) {
00119         strcpy(event_name,"PAPI_TOT_INS");
00120         retval = PAPI_add_named_event(EventSet1, event_name);
00121     }
00122 
00123     if ( retval != PAPI_OK ) {
00124         test_fail_exit( __FILE__, __LINE__, "PAPI_add_event", retval );
00125     }
00126 
00127     values = allocate_test_space( 1, 2);
00128 
00129     elapsed_us = PAPI_get_real_usec(  );
00130 
00131     elapsed_cyc = PAPI_get_real_cyc(  );
00132 
00133     elapsed_virt_us = PAPI_get_virt_usec(  );
00134 
00135     elapsed_virt_cyc = PAPI_get_virt_cyc(  );
00136 
00137     printf("must_ptrace is %d\n",cmpinfo->attach_must_ptrace);
00138     pid_t  child = wait( &status );
00139     printf( "Debugger exited wait() with %d\n",child );
00140       if (WIFSTOPPED( status ))
00141         {
00142           printf( "Child has stopped due to signal %d (%s)\n",
00143               WSTOPSIG( status ), strsignal(WSTOPSIG( status )) );
00144         }
00145       if (WIFSIGNALED( status ))
00146         {
00147           printf( "Child %ld received signal %d (%s)\n",
00148               (long)child,
00149               WTERMSIG(status) , strsignal(WTERMSIG( status )) );
00150         }
00151     printf("After %d\n",retval);
00152 
00153     retval = PAPI_start( EventSet1 );
00154     if ( retval != PAPI_OK )
00155         test_fail_exit( __FILE__, __LINE__, "PAPI_start", retval );
00156 
00157     printf("Continuing\n");
00158 #if defined(__FreeBSD__)
00159     if ( ptrace( PT_CONTINUE, pid, (caddr_t) 1, 0 ) == -1 ) {
00160 #else
00161     if ( ptrace( PTRACE_CONT, pid, NULL, NULL ) == -1 ) {
00162 #endif
00163       perror( "ptrace(PTRACE_CONT)" );
00164       return 1;
00165     }
00166 
00167 
00168     do {
00169       child = wait( &status );
00170       printf( "Debugger exited wait() with %d\n", child);
00171       if (WIFSTOPPED( status ))
00172         {
00173           printf( "Child has stopped due to signal %d (%s)\n",
00174               WSTOPSIG( status ), strsignal(WSTOPSIG( status )) );
00175         }
00176       if (WIFSIGNALED( status ))
00177         {
00178           printf( "Child %ld received signal %d (%s)\n",
00179               (long)child,
00180               WTERMSIG(status) , strsignal(WTERMSIG( status )) );
00181         }
00182     } while (!WIFEXITED( status ));
00183 
00184     printf("Child exited with value %d\n",WEXITSTATUS(status));
00185     if (WEXITSTATUS(status) != 0) 
00186       test_fail_exit( __FILE__, __LINE__, "Exit status of child to attach to", PAPI_EMISC);
00187 
00188     retval = PAPI_stop( EventSet1, values[0] );
00189     if ( retval != PAPI_OK )
00190       test_fail_exit( __FILE__, __LINE__, "PAPI_stop", retval );
00191 
00192     elapsed_virt_us = PAPI_get_virt_usec(  ) - elapsed_virt_us;
00193 
00194     elapsed_virt_cyc = PAPI_get_virt_cyc(  ) - elapsed_virt_cyc;
00195 
00196     elapsed_us = PAPI_get_real_usec(  ) - elapsed_us;
00197 
00198     elapsed_cyc = PAPI_get_real_cyc(  ) - elapsed_cyc;
00199 
00200     retval = PAPI_cleanup_eventset(EventSet1);
00201     if (retval != PAPI_OK)
00202       test_fail_exit( __FILE__, __LINE__, "PAPI_cleanup_eventset", retval );
00203 
00204     retval = PAPI_destroy_eventset(&EventSet1);
00205     if (retval != PAPI_OK)
00206       test_fail_exit( __FILE__, __LINE__, "PAPI_destroy_eventset", retval );
00207 
00208     printf( "Test case: 3rd party attach start, stop.\n" );
00209     printf( "-----------------------------------------------\n" );
00210     tmp = PAPI_get_opt( PAPI_DEFDOM, NULL );
00211     printf( "Default domain is: %d (%s)\n", tmp, stringify_all_domains( tmp ) );
00212     tmp = PAPI_get_opt( PAPI_DEFGRN, NULL );
00213     printf( "Default granularity is: %d (%s)\n", tmp,
00214             stringify_granularity( tmp ) );
00215     printf( "Using %d iterations of c += a*b\n", NUM_FLOPS );
00216     printf
00217         ( "-------------------------------------------------------------------------\n" );
00218 
00219     printf( "Test type    : \t           1\n" );
00220 
00221     printf( TAB1, "PAPI_TOT_CYC : \t", ( values[0] )[0] );
00222     printf( "%s : \t %12lld\n", event_name, ( values[0] )[1] );
00223     printf( TAB1, "Real usec    : \t", elapsed_us );
00224     printf( TAB1, "Real cycles  : \t", elapsed_cyc );
00225     printf( TAB1, "Virt usec    : \t", elapsed_virt_us );
00226     printf( TAB1, "Virt cycles  : \t", elapsed_virt_cyc );
00227 
00228     printf
00229         ( "-------------------------------------------------------------------------\n" );
00230 
00231     printf( "Verification: none\n" );
00232 
00233     test_pass( __FILE__, values, num_tests );
00234     exit( 1 );
00235 }

Here is the call graph for this function:

int wait_for_attach_and_loop ( void   ) 

Definition at line 31 of file attach3.c.

00032 {
00033   char *path;
00034   char newpath[PATH_MAX];
00035   path = getenv("PATH");
00036 
00037   sprintf(newpath, "PATH=./:%s", (path)?path:'\0' );
00038   putenv(newpath);
00039 
00040   if (ptrace(PTRACE_TRACEME, 0, 0, 0) == 0) {
00041     execlp("attach_target","attach_target","100000000",NULL); 
00042     perror("execl(attach_target) failed");
00043   }
00044   perror("PTRACE_TRACEME");
00045   return ( 1 );
00046 }

Here is the call graph for this function:


Generated on 26 Jan 2016 for PAPI by  doxygen 1.6.1