1 /* SimGrid's pthread interposer. Redefinition of the pthread symbols (see the comment in sthread.h) */
4 #include "src/internal_config.h"
5 #include "src/sthread/sthread.h"
13 #include <valgrind/valgrind.h>
16 /* We don't want to intercept pthread within simgrid. Instead we should provide the real implem to simgrid */
17 static int (*raw_pthread_create)(pthread_t*, const pthread_attr_t*, void* (*)(void*), void*);
18 static int (*raw_pthread_join)(pthread_t, void**);
19 static int (*raw_mutex_init)(pthread_mutex_t*, const pthread_mutexattr_t*) = NULL;
20 static int (*raw_mutex_lock)(pthread_mutex_t*) = NULL;
21 static int (*raw_mutex_trylock)(pthread_mutex_t*) = NULL;
22 static int (*raw_mutex_unlock)(pthread_mutex_t*) = NULL;
23 static int (*raw_mutex_destroy)(pthread_mutex_t*) = NULL;
24 static sem_t* (*raw_sem_open)(const char*, int) = NULL;
25 static int (*raw_sem_init)(sem_t*, int, unsigned int) = NULL;
26 static int (*raw_sem_wait)(sem_t*) = NULL;
27 static int (*raw_sem_post)(sem_t*) = NULL;
28 static void intercepter_init()
30 raw_pthread_create = (typeof(raw_pthread_create))dlsym(RTLD_NEXT, "pthread_create");
31 raw_pthread_join = (typeof(raw_pthread_join))dlsym(RTLD_NEXT, "pthread_join");
32 raw_mutex_init = (int (*)(pthread_mutex_t*, const pthread_mutexattr_t*))dlsym(RTLD_NEXT, "pthread_mutex_init");
33 raw_mutex_lock = (int (*)(pthread_mutex_t*))dlsym(RTLD_NEXT, "pthread_mutex_lock");
34 raw_mutex_trylock = (int (*)(pthread_mutex_t*))dlsym(RTLD_NEXT, "pthread_mutex_trylock");
35 raw_mutex_unlock = (int (*)(pthread_mutex_t*))dlsym(RTLD_NEXT, "pthread_mutex_unlock");
36 raw_mutex_destroy = (int (*)(pthread_mutex_t*))dlsym(RTLD_NEXT, "pthread_mutex_destroy");
38 raw_sem_open = (sem_t * (*)(const char*, int)) dlsym(RTLD_NEXT, "sem_open");
39 raw_sem_init = (int (*)(sem_t*, int, unsigned int))dlsym(RTLD_NEXT, "sem_init");
40 raw_sem_wait = (int (*)(sem_t*))dlsym(RTLD_NEXT, "sem_wait");
41 raw_sem_post = (int (*)(sem_t*))dlsym(RTLD_NEXT, "sem_post");
44 static int sthread_inside_simgrid = 1;
45 void sthread_enable(void)
46 { // Start intercepting all pthread calls
47 sthread_inside_simgrid = 0;
49 void sthread_disable(void)
50 { // Stop intercepting all pthread calls
51 sthread_inside_simgrid = 1;
53 int pthread_create(pthread_t* thread, const pthread_attr_t* attr, void* (*start_routine)(void*), void* arg)
55 if (raw_pthread_create == NULL)
58 if (sthread_inside_simgrid)
59 return raw_pthread_create(thread, attr, start_routine, arg);
61 sthread_inside_simgrid = 1;
62 int res = sthread_create(thread, attr, start_routine, arg);
63 sthread_inside_simgrid = 0;
66 int pthread_join(pthread_t thread, void** retval)
68 if (raw_pthread_join == NULL)
71 if (sthread_inside_simgrid)
72 return raw_pthread_join(thread, retval);
74 sthread_inside_simgrid = 1;
75 int res = sthread_join(thread, retval);
76 sthread_inside_simgrid = 0;
80 int pthread_mutex_init(pthread_mutex_t* mutex, const pthread_mutexattr_t* attr)
82 if (raw_mutex_init == NULL)
85 if (sthread_inside_simgrid)
86 return raw_mutex_init(mutex, attr);
88 sthread_inside_simgrid = 1;
89 int res = sthread_mutex_init((sthread_mutex_t*)mutex, attr);
90 sthread_inside_simgrid = 0;
94 int pthread_mutex_lock(pthread_mutex_t* mutex)
96 if (raw_mutex_lock == NULL)
99 if (sthread_inside_simgrid)
100 return raw_mutex_lock(mutex);
102 sthread_inside_simgrid = 1;
103 int res = sthread_mutex_lock((sthread_mutex_t*)mutex);
104 sthread_inside_simgrid = 0;
108 int pthread_mutex_trylock(pthread_mutex_t* mutex)
110 if (raw_mutex_trylock == NULL)
113 if (sthread_inside_simgrid)
114 return raw_mutex_trylock(mutex);
116 sthread_inside_simgrid = 1;
117 int res = sthread_mutex_trylock((sthread_mutex_t*)mutex);
118 sthread_inside_simgrid = 0;
122 int pthread_mutex_unlock(pthread_mutex_t* mutex)
124 if (raw_mutex_unlock == NULL)
127 if (sthread_inside_simgrid)
128 return raw_mutex_unlock(mutex);
130 sthread_inside_simgrid = 1;
131 int res = sthread_mutex_unlock((sthread_mutex_t*)mutex);
132 sthread_inside_simgrid = 0;
135 int pthread_mutex_destroy(pthread_mutex_t* mutex)
137 if (raw_mutex_destroy == NULL)
140 if (sthread_inside_simgrid)
141 return raw_mutex_destroy(mutex);
143 sthread_inside_simgrid = 1;
144 int res = sthread_mutex_destroy((sthread_mutex_t*)mutex);
145 sthread_inside_simgrid = 0;
150 int sem_init(sem_t *sem, int pshared, unsigned int value) {
153 res=raw_sem_init(sem,pshared,value);
157 int sem_wait(sem_t *sem) {
160 res = raw_sem_wait(sem);
164 int sem_post(sem_t *sem) {
165 return raw_sem_post(sem);
168 int pthread_join(pthread_t thread, void **retval) {
169 sg_actor_join(thread, -1);
173 int pthread_cond_init(pthread_cond_t *cond, pthread_condattr_t *cond_attr) {
174 *cond = sg_cond_init();
178 int pthread_cond_signal(pthread_cond_t *cond) {
179 sg_cond_notify_one(*cond);
183 int pthread_cond_broadcast(pthread_cond_t *cond) {
184 sg_cond_notify_all(*cond);
188 int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex) {
189 sg_cond_wait(*cond, *mutex);
193 int pthread_cond_destroy(pthread_cond_t *cond) {
194 sg_cond_destroy(*cond);
199 /* Trampoline for the real main() */
200 static int (*raw_main)(int, char**, char**);
202 /* Our fake main() that gets called by __libc_start_main() */
203 static int main_hook(int argc, char** argv, char** envp)
205 return sthread_main(argc, argv, envp, raw_main);
208 /* Wrapper for __libc_start_main() that replaces the real main function with our hooked version. */
209 int __libc_start_main(int (*main)(int, char**, char**), int argc, char** argv, int (*init)(int, char**, char**),
210 void (*fini)(void), void (*rtld_fini)(void), void* stack_end);
212 int __libc_start_main(int (*main)(int, char**, char**), int argc, char** argv, int (*init)(int, char**, char**),
213 void (*fini)(void), void (*rtld_fini)(void), void* stack_end)
215 /* Save the real main function address */
218 /* Find the real __libc_start_main()... */
219 typeof(&__libc_start_main) orig = dlsym(RTLD_NEXT, "__libc_start_main");
220 /* ... and call it with our custom main function */
222 /* ... unless valgrind is used, and this instance is not the target program (but the valgrind launcher) */
223 if (getenv("VALGRIND_LIB") && !RUNNING_ON_VALGRIND)
224 return orig(raw_main, argc, argv, init, fini, rtld_fini, stack_end);
226 return orig(main_hook, argc, argv, init, fini, rtld_fini, stack_end);