Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Make libsthtread valgrind-aware.
[simgrid.git] / src / sthread / sthread.c
index 7724bcf..24213e0 100644 (file)
@@ -1,12 +1,18 @@
 /* SimGrid's pthread interposer. Redefinition of the pthread symbols (see the comment in sthread.h) */
 
 #define _GNU_SOURCE
+#include "src/internal_config.h"
 #include "src/sthread/sthread.h"
 #include <dlfcn.h>
 #include <pthread.h>
 #include <semaphore.h>
 #include <stdio.h>
 
+#if HAVE_VALGRIND_H
+#include <stdlib.h>
+#include <valgrind/valgrind.h>
+#endif
+
 /* We don't want to intercept pthread within simgrid. Instead we should provide the real implem to simgrid */
 static int (*raw_pthread_create)(pthread_t*, const pthread_attr_t*, void* (*)(void*), void*);
 static int (*raw_pthread_join)(pthread_t, void**);
@@ -35,6 +41,15 @@ static void intercepter_init()
   raw_sem_post = (int (*)(sem_t*))dlsym(RTLD_NEXT, "sem_post");
 }
 
+static int sthread_inside_simgrid = 1;
+void sthread_enable(void)
+{ // Start intercepting all pthread calls
+  sthread_inside_simgrid = 0;
+}
+void sthread_disable(void)
+{ // Stop intercepting all pthread calls
+  sthread_inside_simgrid = 1;
+}
 int pthread_create(pthread_t* thread, const pthread_attr_t* attr, void* (*start_routine)(void*), void* arg)
 {
   if (raw_pthread_create == NULL)
@@ -68,7 +83,7 @@ int pthread_mutex_init(pthread_mutex_t* mutex, const pthread_mutexattr_t* attr)
     intercepter_init();
 
   if (sthread_inside_simgrid)
-    raw_mutex_init(mutex, attr);
+    return raw_mutex_init(mutex, attr);
 
   sthread_inside_simgrid = 1;
   int res                = sthread_mutex_init((sthread_mutex_t*)mutex, attr);
@@ -202,7 +217,11 @@ int __libc_start_main(int (*main)(int, char**, char**), int argc, char** argv, i
 
   /* Find the real __libc_start_main()... */
   typeof(&__libc_start_main) orig = dlsym(RTLD_NEXT, "__libc_start_main");
-  fprintf(stderr, "__libc_start_main\n");
   /* ... and call it with our custom main function */
+#if HAVE_VALGRIND_H
+  /* ... unless valgrind is used, and this instance is not the target program (but the valgrind launcher) */
+  if (getenv("VALGRIND_LIB") && !RUNNING_ON_VALGRIND)
+    return orig(raw_main, argc, argv, init, fini, rtld_fini, stack_end);
+#endif
   return orig(main_hook, argc, argv, init, fini, rtld_fini, stack_end);
-}
\ No newline at end of file
+}