attach2.c File Reference

Include dependency graph for attach2.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 attach2.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_create_eventset", retval );
00095 
00096     /* Here we are testing that this does not cause a fail */
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     retval = PAPI_attach( EventSet1, ( unsigned long ) pid );
00104     if ( retval != PAPI_OK )
00105         test_fail_exit( __FILE__, __LINE__, "PAPI_attach", retval );
00106 
00107     retval = PAPI_add_event(EventSet1, PAPI_TOT_CYC);
00108     if ( retval != PAPI_OK )
00109         test_fail_exit( __FILE__, __LINE__, "PAPI_add_event", retval );
00110 
00111 
00112     strcpy(event_name,"PAPI_FP_INS");
00113 
00114     retval = PAPI_add_named_event(EventSet1, event_name);
00115     if ( retval == PAPI_ENOEVNT ) {
00116         strcpy(event_name,"PAPI_TOT_INS");
00117         retval = PAPI_add_named_event(EventSet1, event_name);
00118     }
00119 
00120     if ( retval != PAPI_OK ) {
00121         test_fail_exit( __FILE__, __LINE__, "PAPI_add_event", retval );
00122     }
00123 
00124     values = allocate_test_space( 1, 2);
00125 
00126     elapsed_us = PAPI_get_real_usec(  );
00127 
00128     elapsed_cyc = PAPI_get_real_cyc(  );
00129 
00130     elapsed_virt_us = PAPI_get_virt_usec(  );
00131 
00132     elapsed_virt_cyc = PAPI_get_virt_cyc(  );
00133 
00134     printf("must_ptrace is %d\n",cmpinfo->attach_must_ptrace);
00135     pid_t  child = wait( &status );
00136     printf( "Debugger exited wait() with %d\n",child );
00137       if (WIFSTOPPED( status ))
00138         {
00139           printf( "Child has stopped due to signal %d (%s)\n",
00140               WSTOPSIG( status ), strsignal(WSTOPSIG( status )) );
00141         }
00142       if (WIFSIGNALED( status ))
00143         {
00144           printf( "Child %ld received signal %d (%s)\n",
00145               (long)child,
00146               WTERMSIG(status) , strsignal(WTERMSIG( status )) );
00147         }
00148     printf("After %d\n",retval);
00149 
00150     retval = PAPI_start( EventSet1 );
00151     if ( retval != PAPI_OK )
00152         test_fail_exit( __FILE__, __LINE__, "PAPI_start", retval );
00153 
00154     printf("Continuing\n");
00155 #if defined(__FreeBSD__)
00156     if ( ptrace( PT_CONTINUE, pid, (caddr_t) 1, 0 ) == -1 ) {
00157 #else
00158     if ( ptrace( PTRACE_CONT, pid, NULL, NULL ) == -1 ) {
00159 #endif
00160       perror( "ptrace(PTRACE_CONT)" );
00161       return 1;
00162     }
00163 
00164 
00165     do {
00166       child = wait( &status );
00167       printf( "Debugger exited wait() with %d\n", child);
00168       if (WIFSTOPPED( status ))
00169         {
00170           printf( "Child has stopped due to signal %d (%s)\n",
00171               WSTOPSIG( status ), strsignal(WSTOPSIG( status )) );
00172         }
00173       if (WIFSIGNALED( status ))
00174         {
00175           printf( "Child %ld received signal %d (%s)\n",
00176               (long)child,
00177               WTERMSIG(status) , strsignal(WTERMSIG( status )) );
00178         }
00179     } while (!WIFEXITED( status ));
00180 
00181     printf("Child exited with value %d\n",WEXITSTATUS(status));
00182     if (WEXITSTATUS(status) != 0) 
00183       test_fail_exit( __FILE__, __LINE__, "Exit status of child to attach to", PAPI_EMISC);
00184 
00185     retval = PAPI_stop( EventSet1, values[0] );
00186     if ( retval != PAPI_OK )
00187       test_fail_exit( __FILE__, __LINE__, "PAPI_stop", retval );
00188 
00189     elapsed_virt_us = PAPI_get_virt_usec(  ) - elapsed_virt_us;
00190 
00191     elapsed_virt_cyc = PAPI_get_virt_cyc(  ) - elapsed_virt_cyc;
00192 
00193     elapsed_us = PAPI_get_real_usec(  ) - elapsed_us;
00194 
00195     elapsed_cyc = PAPI_get_real_cyc(  ) - elapsed_cyc;
00196 
00197     retval = PAPI_cleanup_eventset(EventSet1);
00198     if (retval != PAPI_OK)
00199       test_fail_exit( __FILE__, __LINE__, "PAPI_cleanup_eventset", retval );
00200 
00201     retval = PAPI_destroy_eventset(&EventSet1);
00202     if (retval != PAPI_OK)
00203       test_fail_exit( __FILE__, __LINE__, "PAPI_destroy_eventset", retval );
00204 
00205     printf( "Test case: 3rd party attach start, stop.\n" );
00206     printf( "-----------------------------------------------\n" );
00207     tmp = PAPI_get_opt( PAPI_DEFDOM, NULL );
00208     printf( "Default domain is: %d (%s)\n", tmp, stringify_all_domains( tmp ) );
00209     tmp = PAPI_get_opt( PAPI_DEFGRN, NULL );
00210     printf( "Default granularity is: %d (%s)\n", tmp,
00211             stringify_granularity( tmp ) );
00212     printf( "Using %d iterations of c += a*b\n", NUM_FLOPS );
00213     printf
00214         ( "-------------------------------------------------------------------------\n" );
00215 
00216     printf( "Test type    : \t           1\n" );
00217 
00218     printf( TAB1, "PAPI_TOT_CYC : \t", ( values[0] )[0] );
00219     printf( "%s : \t %12lld\n",event_name, ( values[0] )[1]);
00220     printf( TAB1, "Real usec    : \t", elapsed_us );
00221     printf( TAB1, "Real cycles  : \t", elapsed_cyc );
00222     printf( TAB1, "Virt usec    : \t", elapsed_virt_us );
00223     printf( TAB1, "Virt cycles  : \t", elapsed_virt_cyc );
00224 
00225     printf
00226         ( "-------------------------------------------------------------------------\n" );
00227 
00228     printf( "Verification: none\n" );
00229 
00230     test_pass( __FILE__, values, num_tests );
00231     exit( 1 );
00232 }

Here is the call graph for this function:

int wait_for_attach_and_loop ( void   ) 

Definition at line 31 of file attach2.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:

Here is the caller graph for this function:


Generated on 26 Jan 2016 for PAPI by  doxygen 1.6.1