1 /* SimGrid's pthread interposer. Redefinition of the pthread symbols (see the comment in sthread.h) */
4 #include "src/sthread/sthread.h"
5 #include "src/internal_config.h"
14 #include <valgrind/valgrind.h>
17 /* We don't want to intercept pthread within simgrid. Instead we should provide the real implem to simgrid */
18 static int (*raw_pthread_create)(pthread_t*, const pthread_attr_t*, void* (*)(void*), void*);
19 static int (*raw_pthread_join)(pthread_t, void**);
20 static int (*raw_mutex_init)(pthread_mutex_t*, const pthread_mutexattr_t*);
21 static int (*raw_mutex_lock)(pthread_mutex_t*);
22 static int (*raw_mutex_trylock)(pthread_mutex_t*);
23 static int (*raw_mutex_unlock)(pthread_mutex_t*);
24 static int (*raw_mutex_destroy)(pthread_mutex_t*);
26 static unsigned int (*raw_sleep)(unsigned int);
27 static int (*raw_usleep)(useconds_t);
28 static int (*raw_gettimeofday)(struct timeval*, void*);
30 static sem_t* (*raw_sem_open)(const char*, int);
31 static int (*raw_sem_init)(sem_t*, int, unsigned int);
32 static int (*raw_sem_wait)(sem_t*);
33 static int (*raw_sem_post)(sem_t*);
35 static void intercepter_init()
37 raw_pthread_create = dlsym(RTLD_NEXT, "pthread_create");
38 raw_pthread_join = dlsym(RTLD_NEXT, "pthread_join");
39 raw_mutex_init = dlsym(RTLD_NEXT, "pthread_mutex_init");
40 raw_mutex_lock = dlsym(RTLD_NEXT, "pthread_mutex_lock");
41 raw_mutex_trylock = dlsym(RTLD_NEXT, "pthread_mutex_trylock");
42 raw_mutex_unlock = dlsym(RTLD_NEXT, "pthread_mutex_unlock");
43 raw_mutex_destroy = dlsym(RTLD_NEXT, "pthread_mutex_destroy");
45 raw_sleep = dlsym(RTLD_NEXT, "sleep");
46 raw_usleep = dlsym(RTLD_NEXT, "usleep");
47 raw_gettimeofday = dlsym(RTLD_NEXT, "gettimeofday");
49 raw_sem_open = dlsym(RTLD_NEXT, "sem_open");
50 raw_sem_init = dlsym(RTLD_NEXT, "sem_init");
51 raw_sem_wait = dlsym(RTLD_NEXT, "sem_wait");
52 raw_sem_post = dlsym(RTLD_NEXT, "sem_post");
55 static int sthread_inside_simgrid = 1;
56 void sthread_enable(void)
57 { // Start intercepting all pthread calls
58 sthread_inside_simgrid = 0;
60 void sthread_disable(void)
61 { // Stop intercepting all pthread calls
62 sthread_inside_simgrid = 1;
64 int pthread_create(pthread_t* thread, const pthread_attr_t* attr, void* (*start_routine)(void*), void* arg)
66 if (raw_pthread_create == NULL)
69 if (sthread_inside_simgrid)
70 return raw_pthread_create(thread, attr, start_routine, arg);
73 int res = sthread_create((sthread_t*)thread, attr, start_routine, arg);
77 int pthread_join(pthread_t thread, void** retval)
79 if (raw_pthread_join == NULL)
81 if (sthread_inside_simgrid)
82 return raw_pthread_join(thread, retval);
85 int res = sthread_join((sthread_t)thread, retval);
90 int pthread_mutex_init(pthread_mutex_t* mutex, const pthread_mutexattr_t* attr)
92 if (raw_mutex_init == NULL)
95 if (sthread_inside_simgrid)
96 return raw_mutex_init(mutex, attr);
99 int res = sthread_mutex_init((sthread_mutex_t*)mutex, attr);
104 int pthread_mutex_lock(pthread_mutex_t* mutex)
106 if (raw_mutex_lock == NULL)
109 if (sthread_inside_simgrid)
110 return raw_mutex_lock(mutex);
113 int res = sthread_mutex_lock((sthread_mutex_t*)mutex);
118 int pthread_mutex_trylock(pthread_mutex_t* mutex)
120 if (raw_mutex_trylock == NULL)
123 if (sthread_inside_simgrid)
124 return raw_mutex_trylock(mutex);
127 int res = sthread_mutex_trylock((sthread_mutex_t*)mutex);
132 int pthread_mutex_unlock(pthread_mutex_t* mutex)
134 if (raw_mutex_unlock == NULL)
137 if (sthread_inside_simgrid)
138 return raw_mutex_unlock(mutex);
141 int res = sthread_mutex_unlock((sthread_mutex_t*)mutex);
145 int pthread_mutex_destroy(pthread_mutex_t* mutex)
147 if (raw_mutex_destroy == NULL)
150 if (sthread_inside_simgrid)
151 return raw_mutex_destroy(mutex);
154 int res = sthread_mutex_destroy((sthread_mutex_t*)mutex);
159 /* Glibc < 2.31 uses type "struct timezone *" for the second parameter of gettimeofday.
160 Other implementations use "void *" instead. */
162 #if !__GLIBC_PREREQ(2, 31)
163 #define TIMEZONE_TYPE struct timezone
166 #ifndef TIMEZONE_TYPE
167 #define TIMEZONE_TYPE void
170 int gettimeofday(struct timeval* tv, XBT_ATTRIB_UNUSED TIMEZONE_TYPE* tz)
172 if (raw_gettimeofday == NULL)
175 if (sthread_inside_simgrid)
176 return raw_gettimeofday(tv, tz);
179 int res = sthread_gettimeofday(tv);
184 unsigned int sleep(unsigned int seconds)
186 if (raw_sleep == NULL)
189 if (sthread_inside_simgrid)
190 return raw_sleep(seconds);
193 sthread_sleep(seconds);
198 int usleep(useconds_t usec)
200 if (raw_usleep == NULL)
203 if (sthread_inside_simgrid)
204 return raw_usleep(usec);
207 sthread_sleep(((double)usec) / 1000000.);
213 int sem_init(sem_t *sem, int pshared, unsigned int value) {
216 res=raw_sem_init(sem,pshared,value);
220 int sem_wait(sem_t *sem) {
223 res = raw_sem_wait(sem);
227 int sem_post(sem_t *sem) {
228 return raw_sem_post(sem);
231 int pthread_cond_init(pthread_cond_t *cond, pthread_condattr_t *cond_attr) {
232 *cond = sg_cond_init();
236 int pthread_cond_signal(pthread_cond_t *cond) {
237 sg_cond_notify_one(*cond);
241 int pthread_cond_broadcast(pthread_cond_t *cond) {
242 sg_cond_notify_all(*cond);
246 int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex) {
247 sg_cond_wait(*cond, *mutex);
251 int pthread_cond_destroy(pthread_cond_t *cond) {
252 sg_cond_destroy(*cond);
257 /* Trampoline for the real main() */
258 static int (*raw_main)(int, char**, char**);
260 /* Our fake main() that gets called by __libc_start_main() */
261 static int main_hook(int argc, char** argv, char** envp)
263 return sthread_main(argc, argv, envp, raw_main);
266 /* Wrapper for __libc_start_main() that replaces the real main function with our hooked version. */
267 int __libc_start_main(int (*main)(int, char**, char**), int argc, char** argv, int (*init)(int, char**, char**),
268 void (*fini)(void), void (*rtld_fini)(void), void* stack_end);
270 int __libc_start_main(int (*main)(int, char**, char**), int argc, char** argv, int (*init)(int, char**, char**),
271 void (*fini)(void), void (*rtld_fini)(void), void* stack_end)
273 /* Save the real main function address */
276 /* Find the real __libc_start_main()... */
277 typeof(&__libc_start_main) orig = dlsym(RTLD_NEXT, "__libc_start_main");
278 /* ... and call it with our custom main function */
280 /* ... unless valgrind is used, and this instance is not the target program (but the valgrind launcher) */
281 if (getenv("VALGRIND_LIB") && !RUNNING_ON_VALGRIND)
282 return orig(raw_main, argc, argv, init, fini, rtld_fini, stack_end);
284 return orig(main_hook, argc, argv, init, fini, rtld_fini, stack_end);