int main(int argc, const char *argv[]) { intercept_signal(SIGSEGV, signal_handler, true/*sigstack*/); print("pre-DR init\n"); dr_app_setup(); assert(!dr_app_running_under_dynamorio()); dr_app_start(); assert(dr_app_running_under_dynamorio()); if (do_some_work() < 0) print("error in computation\n"); print("pre-DR stop\n"); dr_app_stop_and_cleanup(); assert(!dr_app_running_under_dynamorio()); print("all done\n"); return 0; }
int main(int argc, const char *argv[]) { static int outer_iters = 2048; /* We trace a 4-iter burst of execution. */ static int iter_start = outer_iters/3; static int iter_stop = iter_start + 4; if (!my_setenv("DYNAMORIO_OPTIONS", "-stderr_mask 0xc -client_lib ';;-offline'")) std::cerr << "failed to set env var!\n"; /* We use an outer loop to test re-attaching (i#2157). */ for (int j = 0; j < 3; ++j) { std::cerr << "pre-DR init\n"; dr_app_setup(); assert(!dr_app_running_under_dynamorio()); for (int i = 0; i < outer_iters; ++i) { if (i == iter_start) { std::cerr << "pre-DR start\n"; dr_app_start(); } if (i >= iter_start && i <= iter_stop) assert(dr_app_running_under_dynamorio()); else assert(!dr_app_running_under_dynamorio()); if (do_some_work(i) < 0) std::cerr << "error in computation\n"; if (i == iter_stop) { std::cerr << "pre-DR detach\n"; dr_app_stop_and_cleanup(); } } std::cerr << "all done\n"; } return 0; }
unsigned int __stdcall #else void * #endif thread_func(void *arg) { unsigned int idx = (unsigned int)(uintptr_t)arg; static const int reattach_iters = 4; static const int outer_iters = 2048; /* We trace a 4-iter burst of execution. */ static const int iter_start = outer_iters / 3; static const int iter_stop = iter_start + 4; #ifdef UNIX /* We test sigaltstack with attach+detach to avoid bugs like i#3116 */ stack_t sigstack; # define ALT_STACK_SIZE (SIGSTKSZ * 2) sigstack.ss_sp = (char *)malloc(ALT_STACK_SIZE); sigstack.ss_size = ALT_STACK_SIZE; sigstack.ss_flags = SS_ONSTACK; int res = sigaltstack(&sigstack, NULL); assert(res == 0); #endif /* We use an outer loop to test re-attaching (i#2157). */ for (int j = 0; j < reattach_iters; ++j) { if (idx == burst_owner) { std::cerr << "pre-DR init\n"; dr_app_setup(); assert(!dr_app_running_under_dynamorio()); } for (int i = 0; i < outer_iters; ++i) { if (idx == burst_owner && i == iter_start) { std::cerr << "pre-DR start\n"; dr_app_start(); } if (idx == burst_owner) { if (i >= iter_start && i <= iter_stop) assert(dr_app_running_under_dynamorio()); else assert(!dr_app_running_under_dynamorio()); } if (do_some_work(i) < 0) std::cerr << "error in computation\n"; if (idx == burst_owner && i == iter_stop) { std::cerr << "pre-DR detach\n"; dr_app_stop_and_cleanup(); } } } #ifdef UNIX stack_t check_stack; res = sigaltstack(NULL, &check_stack); assert(res == 0 && check_stack.ss_sp == sigstack.ss_sp && check_stack.ss_size == sigstack.ss_size); sigstack.ss_flags = SS_DISABLE; res = sigaltstack(&sigstack, NULL); assert(res == 0); free(sigstack.ss_sp); #endif if (idx == burst_owner) { signal_cond_var(burst_owner_finished); } else { /* Avoid having < 1 thread per core in the output. */ wait_cond_var(burst_owner_finished); } finished[idx] = true; return 0; }
int main(void) { double res = 0.; int i,j; void *stack = NULL; uint tid; #ifdef LINUX pthread_t pt[10]; /* On Linux, the tid. */ #else uintptr_t thread[10]; /* _beginthreadex doesn't return HANDLE? */ #endif /* Create spinning sideline threads. */ #ifdef LINUX pthread_create(&pt[0], NULL, sideline_spinner, (void*)func_0); pthread_create(&pt[1], NULL, sideline_spinner, (void*)func_1); pthread_create(&pt[2], NULL, sideline_spinner, (void*)func_2); pthread_create(&pt[3], NULL, sideline_spinner, (void*)func_3); pthread_create(&pt[4], NULL, sideline_spinner, (void*)func_4); pthread_create(&pt[5], NULL, sideline_spinner, (void*)func_5); pthread_create(&pt[6], NULL, sideline_spinner, (void*)func_6); pthread_create(&pt[7], NULL, sideline_spinner, (void*)func_7); pthread_create(&pt[8], NULL, sideline_spinner, (void*)func_8); pthread_create(&pt[9], NULL, sideline_spinner, (void*)func_9); #else thread[0] = _beginthreadex(NULL, 0, sideline_spinner, (void*)func_0, 0, &tid); thread[1] = _beginthreadex(NULL, 0, sideline_spinner, (void*)func_1, 0, &tid); thread[2] = _beginthreadex(NULL, 0, sideline_spinner, (void*)func_2, 0, &tid); thread[3] = _beginthreadex(NULL, 0, sideline_spinner, (void*)func_3, 0, &tid); thread[4] = _beginthreadex(NULL, 0, sideline_spinner, (void*)func_4, 0, &tid); thread[5] = _beginthreadex(NULL, 0, sideline_spinner, (void*)func_5, 0, &tid); thread[6] = _beginthreadex(NULL, 0, sideline_spinner, (void*)func_6, 0, &tid); thread[7] = _beginthreadex(NULL, 0, sideline_spinner, (void*)func_7, 0, &tid); thread[8] = _beginthreadex(NULL, 0, sideline_spinner, (void*)func_8, 0, &tid); thread[9] = _beginthreadex(NULL, 0, sideline_spinner, (void*)func_9, 0, &tid); #endif #ifdef USE_DYNAMO dr_app_setup(); /* XXX: Calling the client interface from the app is not supported. We're * just using it for testing. */ dr_register_bb_event(event_bb); #endif for (j=0; j<10; j++) { #ifdef USE_DYNAMO dr_app_start(); #endif for (i=0; i<ITERS; i++) { if (i % 2 == 0) { res += cos(1./(double)(i+1)); } else { res += sin(1./(double)(i+1)); } } foo(); #ifdef USE_DYNAMO /* FIXME i#95: On Linux dr_app_stop only makes the current thread run * native. We should revisit this while implementing full detach for * Linux. */ dr_app_stop(); #endif } /* PR : we get different floating point results on different platforms, * so we no longer print out res */ print("all done: %d iters\n", i); #ifdef USE_DYNAMO /* On x64 Linux it's OK if we call pthread_join natively, but x86-32 has * problems. We start and stop to bracket it. */ dr_app_start(); #endif should_spin = false; /* Break the loops. */ for (i = 0; i < 10; i++) { #ifdef LINUX pthread_join(pt[i], NULL); /* FIXME i#725: Windows needs attach in order to take over these * threads. */ if (!took_over_thread[i]) print("failed to take over thread %d!\n", i); #else WaitForSingleObject((HANDLE)thread[i], INFINITE); #endif } #ifdef USE_DYNAMO dr_app_stop(); #endif #ifdef USE_DYNAMO dr_app_cleanup(); #endif return 0; }