sanitizer_stoptheworld_testlib.cc revision 3614c16084e8a0dc8ae3418402a2d0c6f8107e39
1//===-- sanitizer_stoptheworld_testlib.cc ---------------------------------===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// Dynamic library to test StopTheWorld functionality. 10// When loaded with LD_PRELOAD, it will periodically suspend all threads. 11//===----------------------------------------------------------------------===// 12/* Usage: 13clang++ -fno-exceptions -g -fPIC -I. \ 14 sanitizer_common/tests/sanitizer_stoptheworld_testlib.cc \ 15 sanitizer_common/sanitizer_*.cc -shared -lpthread -o teststoptheworld.so 16LD_PRELOAD=`pwd`/teststoptheworld.so /your/app 17*/ 18 19#ifdef __linux__ 20 21#include <dlfcn.h> 22#include <stddef.h> 23#include <stdio.h> 24#include <pthread.h> 25#include <unistd.h> 26 27#include "sanitizer_common/sanitizer_stoptheworld.h" 28 29namespace { 30const uptr kSuspendDuration = 3; 31const uptr kRunDuration = 3; 32 33void Callback(const SuspendedThreadsList &suspended_threads_list, 34 void *argument) { 35 sleep(kSuspendDuration); 36} 37 38void *SuspenderThread(void *argument) { 39 while (true) { 40 sleep(kRunDuration); 41 StopTheWorld(Callback, NULL); 42 } 43 return NULL; 44} 45 46__attribute__((constructor)) void StopTheWorldTestLibConstructor(void) { 47 pthread_t thread_id; 48 pthread_create(&thread_id, NULL, SuspenderThread, NULL); 49} 50} // namespace 51 52#endif // __linux__ 53