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;
26 static unsigned int (*raw_sleep)(unsigned int) = NULL;
27 static int (*raw_usleep)(useconds_t) = NULL;
28 static int (*raw_gettimeofday)(struct timeval*, void*) = NULL;
30 static sem_t* (*raw_sem_open)(const char*, int) = NULL;
31 static int (*raw_sem_init)(sem_t*, int, unsigned int) = NULL;
32 static int (*raw_sem_wait)(sem_t*) = NULL;
33 static int (*raw_sem_post)(sem_t*) = NULL;
35 static void intercepter_init()
37 raw_pthread_create = (typeof(raw_pthread_create))dlsym(RTLD_NEXT, "pthread_create");
38 raw_pthread_join = (typeof(raw_pthread_join))dlsym(RTLD_NEXT, "pthread_join");
39 raw_mutex_init = (int (*)(pthread_mutex_t*, const pthread_mutexattr_t*))dlsym(RTLD_NEXT, "pthread_mutex_init");
40 raw_mutex_lock = (int (*)(pthread_mutex_t*))dlsym(RTLD_NEXT, "pthread_mutex_lock");
41 raw_mutex_trylock = (int (*)(pthread_mutex_t*))dlsym(RTLD_NEXT, "pthread_mutex_trylock");
42 raw_mutex_unlock = (int (*)(pthread_mutex_t*))dlsym(RTLD_NEXT, "pthread_mutex_unlock");
43 raw_mutex_destroy = (int (*)(pthread_mutex_t*))dlsym(RTLD_NEXT, "pthread_mutex_destroy");
45 raw_sleep = (unsigned int (*)(unsigned int))dlsym(RTLD_NEXT, "sleep");
46 raw_usleep = (int (*)(useconds_t usec))dlsym(RTLD_NEXT, "usleep");
47 raw_gettimeofday = (int (*)(struct timeval*, void*))dlsym(RTLD_NEXT, "gettimeofday");
49 raw_sem_open = (sem_t * (*)(const char*, int)) dlsym(RTLD_NEXT, "sem_open");
50 raw_sem_init = (int (*)(sem_t*, int, unsigned int))dlsym(RTLD_NEXT, "sem_init");
51 raw_sem_wait = (int (*)(sem_t*))dlsym(RTLD_NEXT, "sem_wait");
52 raw_sem_post = (int (*)(sem_t*))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);
72 sthread_inside_simgrid = 1;
73 int res = sthread_create((sthread_t*)thread, attr, start_routine, arg);
74 sthread_inside_simgrid = 0;
77 int pthread_join(pthread_t thread, void** retval)
79 if (raw_pthread_join == NULL)
82 if (sthread_inside_simgrid)
83 return raw_pthread_join(thread, retval);
85 sthread_inside_simgrid = 1;
86 int res = sthread_join((sthread_t)thread, retval);
87 sthread_inside_simgrid = 0;
91 int pthread_mutex_init(pthread_mutex_t* mutex, const pthread_mutexattr_t* attr)
93 if (raw_mutex_init == NULL)
96 if (sthread_inside_simgrid)
97 return raw_mutex_init(mutex, attr);
99 sthread_inside_simgrid = 1;
100 int res = sthread_mutex_init((sthread_mutex_t*)mutex, attr);
101 sthread_inside_simgrid = 0;
105 int pthread_mutex_lock(pthread_mutex_t* mutex)
107 if (raw_mutex_lock == NULL)
110 if (sthread_inside_simgrid)
111 return raw_mutex_lock(mutex);
113 sthread_inside_simgrid = 1;
114 int res = sthread_mutex_lock((sthread_mutex_t*)mutex);
115 sthread_inside_simgrid = 0;
119 int pthread_mutex_trylock(pthread_mutex_t* mutex)
121 if (raw_mutex_trylock == NULL)
124 if (sthread_inside_simgrid)
125 return raw_mutex_trylock(mutex);
127 sthread_inside_simgrid = 1;
128 int res = sthread_mutex_trylock((sthread_mutex_t*)mutex);
129 sthread_inside_simgrid = 0;
133 int pthread_mutex_unlock(pthread_mutex_t* mutex)
135 if (raw_mutex_unlock == NULL)
138 if (sthread_inside_simgrid)
139 return raw_mutex_unlock(mutex);
141 sthread_inside_simgrid = 1;
142 int res = sthread_mutex_unlock((sthread_mutex_t*)mutex);
143 sthread_inside_simgrid = 0;
146 int pthread_mutex_destroy(pthread_mutex_t* mutex)
148 if (raw_mutex_destroy == NULL)
151 if (sthread_inside_simgrid)
152 return raw_mutex_destroy(mutex);
154 sthread_inside_simgrid = 1;
155 int res = sthread_mutex_destroy((sthread_mutex_t*)mutex);
156 sthread_inside_simgrid = 0;
160 /* Glibc < 2.31 uses type "struct timezone *" for the second parameter of gettimeofday.
161 Other implementations use "void *" instead. */
163 #if !__GLIBC_PREREQ(2, 31)
164 #define TIMEZONE_TYPE struct timezone
167 #ifndef TIMEZONE_TYPE
168 #define TIMEZONE_TYPE void
171 int gettimeofday(struct timeval* tv, XBT_ATTRIB_UNUSED TIMEZONE_TYPE* tz)
173 if (raw_gettimeofday == NULL)
176 if (sthread_inside_simgrid)
177 return raw_gettimeofday(tv, tz);
179 sthread_inside_simgrid = 1;
180 int res = sthread_gettimeofday(tv);
181 sthread_inside_simgrid = 0;
185 unsigned int sleep(unsigned int seconds)
187 if (raw_sleep == NULL)
190 if (sthread_inside_simgrid)
191 return raw_sleep(seconds);
193 sthread_inside_simgrid = 1;
194 sthread_sleep(seconds);
195 sthread_inside_simgrid = 0;
199 int usleep(useconds_t usec)
201 if (raw_usleep == NULL)
204 if (sthread_inside_simgrid)
205 return raw_usleep(usec);
207 sthread_inside_simgrid = 1;
208 sthread_sleep(((double)usec) / 1000000.);
209 sthread_inside_simgrid = 0;
214 int sem_init(sem_t *sem, int pshared, unsigned int value) {
217 res=raw_sem_init(sem,pshared,value);
221 int sem_wait(sem_t *sem) {
224 res = raw_sem_wait(sem);
228 int sem_post(sem_t *sem) {
229 return raw_sem_post(sem);
232 int pthread_cond_init(pthread_cond_t *cond, pthread_condattr_t *cond_attr) {
233 *cond = sg_cond_init();
237 int pthread_cond_signal(pthread_cond_t *cond) {
238 sg_cond_notify_one(*cond);
242 int pthread_cond_broadcast(pthread_cond_t *cond) {
243 sg_cond_notify_all(*cond);
247 int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex) {
248 sg_cond_wait(*cond, *mutex);
252 int pthread_cond_destroy(pthread_cond_t *cond) {
253 sg_cond_destroy(*cond);
258 /* Trampoline for the real main() */
259 static int (*raw_main)(int, char**, char**);
261 /* Our fake main() that gets called by __libc_start_main() */
262 static int main_hook(int argc, char** argv, char** envp)
264 return sthread_main(argc, argv, envp, raw_main);
267 /* Wrapper for __libc_start_main() that replaces the real main function with our hooked version. */
268 int __libc_start_main(int (*main)(int, char**, char**), int argc, char** argv, int (*init)(int, char**, char**),
269 void (*fini)(void), void (*rtld_fini)(void), void* stack_end);
271 int __libc_start_main(int (*main)(int, char**, char**), int argc, char** argv, int (*init)(int, char**, char**),
272 void (*fini)(void), void (*rtld_fini)(void), void* stack_end)
274 /* Save the real main function address */
277 /* Find the real __libc_start_main()... */
278 typeof(&__libc_start_main) orig = dlsym(RTLD_NEXT, "__libc_start_main");
279 /* ... and call it with our custom main function */
281 /* ... unless valgrind is used, and this instance is not the target program (but the valgrind launcher) */
282 if (getenv("VALGRIND_LIB") && !RUNNING_ON_VALGRIND)
283 return orig(raw_main, argc, argv, init, fini, rtld_fini, stack_end);
285 return orig(main_hook, argc, argv, init, fini, rtld_fini, stack_end);