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*) = NULL;
21 static int (*raw_mutex_lock)(pthread_mutex_t*) = NULL;
22 static int (*raw_mutex_trylock)(pthread_mutex_t*) = NULL;
23 static int (*raw_mutex_unlock)(pthread_mutex_t*) = NULL;
24 static int (*raw_mutex_destroy)(pthread_mutex_t*) = NULL;
25 static sem_t* (*raw_sem_open)(const char*, int) = NULL;
26 static int (*raw_sem_init)(sem_t*, int, unsigned int) = NULL;
27 static int (*raw_sem_wait)(sem_t*) = NULL;
28 static int (*raw_sem_post)(sem_t*) = NULL;
29 static void intercepter_init()
31 raw_pthread_create = (typeof(raw_pthread_create))dlsym(RTLD_NEXT, "pthread_create");
32 raw_pthread_join = (typeof(raw_pthread_join))dlsym(RTLD_NEXT, "pthread_join");
33 raw_mutex_init = (int (*)(pthread_mutex_t*, const pthread_mutexattr_t*))dlsym(RTLD_NEXT, "pthread_mutex_init");
34 raw_mutex_lock = (int (*)(pthread_mutex_t*))dlsym(RTLD_NEXT, "pthread_mutex_lock");
35 raw_mutex_trylock = (int (*)(pthread_mutex_t*))dlsym(RTLD_NEXT, "pthread_mutex_trylock");
36 raw_mutex_unlock = (int (*)(pthread_mutex_t*))dlsym(RTLD_NEXT, "pthread_mutex_unlock");
37 raw_mutex_destroy = (int (*)(pthread_mutex_t*))dlsym(RTLD_NEXT, "pthread_mutex_destroy");
39 raw_sem_open = (sem_t * (*)(const char*, int)) dlsym(RTLD_NEXT, "sem_open");
40 raw_sem_init = (int (*)(sem_t*, int, unsigned int))dlsym(RTLD_NEXT, "sem_init");
41 raw_sem_wait = (int (*)(sem_t*))dlsym(RTLD_NEXT, "sem_wait");
42 raw_sem_post = (int (*)(sem_t*))dlsym(RTLD_NEXT, "sem_post");
45 static int sthread_inside_simgrid = 1;
46 void sthread_enable(void)
47 { // Start intercepting all pthread calls
48 sthread_inside_simgrid = 0;
50 void sthread_disable(void)
51 { // Stop intercepting all pthread calls
52 sthread_inside_simgrid = 1;
54 int pthread_create(pthread_t* thread, const pthread_attr_t* attr, void* (*start_routine)(void*), void* arg)
56 if (raw_pthread_create == NULL)
59 if (sthread_inside_simgrid)
60 return raw_pthread_create(thread, attr, start_routine, arg);
62 sthread_inside_simgrid = 1;
63 int res = sthread_create(thread, attr, start_routine, arg);
64 sthread_inside_simgrid = 0;
67 int pthread_join(pthread_t thread, void** retval)
69 if (raw_pthread_join == NULL)
72 if (sthread_inside_simgrid)
73 return raw_pthread_join(thread, retval);
75 sthread_inside_simgrid = 1;
76 int res = sthread_join(thread, retval);
77 sthread_inside_simgrid = 0;
81 int pthread_mutex_init(pthread_mutex_t* mutex, const pthread_mutexattr_t* attr)
83 if (raw_mutex_init == NULL)
86 if (sthread_inside_simgrid)
87 return raw_mutex_init(mutex, attr);
89 sthread_inside_simgrid = 1;
90 int res = sthread_mutex_init((sthread_mutex_t*)mutex, attr);
91 sthread_inside_simgrid = 0;
95 int pthread_mutex_lock(pthread_mutex_t* mutex)
97 if (raw_mutex_lock == NULL)
100 if (sthread_inside_simgrid)
101 return raw_mutex_lock(mutex);
103 sthread_inside_simgrid = 1;
104 int res = sthread_mutex_lock((sthread_mutex_t*)mutex);
105 sthread_inside_simgrid = 0;
109 int pthread_mutex_trylock(pthread_mutex_t* mutex)
111 if (raw_mutex_trylock == NULL)
114 if (sthread_inside_simgrid)
115 return raw_mutex_trylock(mutex);
117 sthread_inside_simgrid = 1;
118 int res = sthread_mutex_trylock((sthread_mutex_t*)mutex);
119 sthread_inside_simgrid = 0;
123 int pthread_mutex_unlock(pthread_mutex_t* mutex)
125 if (raw_mutex_unlock == NULL)
128 if (sthread_inside_simgrid)
129 return raw_mutex_unlock(mutex);
131 sthread_inside_simgrid = 1;
132 int res = sthread_mutex_unlock((sthread_mutex_t*)mutex);
133 sthread_inside_simgrid = 0;
136 int pthread_mutex_destroy(pthread_mutex_t* mutex)
138 if (raw_mutex_destroy == NULL)
141 if (sthread_inside_simgrid)
142 return raw_mutex_destroy(mutex);
144 sthread_inside_simgrid = 1;
145 int res = sthread_mutex_destroy((sthread_mutex_t*)mutex);
146 sthread_inside_simgrid = 0;
150 int gettimeofday(struct timeval* tv, void* tz)
152 return sthread_gettimeofday(tv, tz);
155 unsigned int sleep(unsigned int seconds)
157 sthread_sleep(seconds);
161 int usleep(useconds_t usec)
163 sthread_sleep(((double)usec) / 1000000.);
168 int sem_init(sem_t *sem, int pshared, unsigned int value) {
171 res=raw_sem_init(sem,pshared,value);
175 int sem_wait(sem_t *sem) {
178 res = raw_sem_wait(sem);
182 int sem_post(sem_t *sem) {
183 return raw_sem_post(sem);
186 int pthread_join(pthread_t thread, void **retval) {
187 sg_actor_join(thread, -1);
191 int pthread_cond_init(pthread_cond_t *cond, pthread_condattr_t *cond_attr) {
192 *cond = sg_cond_init();
196 int pthread_cond_signal(pthread_cond_t *cond) {
197 sg_cond_notify_one(*cond);
201 int pthread_cond_broadcast(pthread_cond_t *cond) {
202 sg_cond_notify_all(*cond);
206 int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex) {
207 sg_cond_wait(*cond, *mutex);
211 int pthread_cond_destroy(pthread_cond_t *cond) {
212 sg_cond_destroy(*cond);
217 /* Trampoline for the real main() */
218 static int (*raw_main)(int, char**, char**);
220 /* Our fake main() that gets called by __libc_start_main() */
221 static int main_hook(int argc, char** argv, char** envp)
223 return sthread_main(argc, argv, envp, raw_main);
226 /* Wrapper for __libc_start_main() that replaces the real main function with our hooked version. */
227 int __libc_start_main(int (*main)(int, char**, char**), int argc, char** argv, int (*init)(int, char**, char**),
228 void (*fini)(void), void (*rtld_fini)(void), void* stack_end);
230 int __libc_start_main(int (*main)(int, char**, char**), int argc, char** argv, int (*init)(int, char**, char**),
231 void (*fini)(void), void (*rtld_fini)(void), void* stack_end)
233 /* Save the real main function address */
236 /* Find the real __libc_start_main()... */
237 typeof(&__libc_start_main) orig = dlsym(RTLD_NEXT, "__libc_start_main");
238 /* ... and call it with our custom main function */
240 /* ... unless valgrind is used, and this instance is not the target program (but the valgrind launcher) */
241 if (getenv("VALGRIND_LIB") && !RUNNING_ON_VALGRIND)
242 return orig(raw_main, argc, argv, init, fini, rtld_fini, stack_end);
244 return orig(main_hook, argc, argv, init, fini, rtld_fini, stack_end);